Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / cp / parser.c
blob963ad7ac9cc5e2e1b717af3da1efb37b0ea41996
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 /* A helpfer function to check if the given expression (EXPR) is of POD type.
8103 Note that if the expression's type is NULL (e.g. when its type depends on
8104 template parameters), we return false. */
8106 static bool
8107 expr_is_pod (tree expr)
8109 return TREE_TYPE (expr) && pod_type_p (TREE_TYPE (expr));
8112 /* Parse an assignment-expression.
8114 assignment-expression:
8115 conditional-expression
8116 logical-or-expression assignment-operator assignment_expression
8117 throw-expression
8119 CAST_P is true if this expression is the target of a cast.
8120 DECLTYPE_P is true if this expression is the operand of decltype.
8122 Returns a representation for the expression. */
8124 static tree
8125 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8126 bool decltype_p, cp_id_kind * pidk)
8128 tree expr;
8130 /* If the next token is the `throw' keyword, then we're looking at
8131 a throw-expression. */
8132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8133 expr = cp_parser_throw_expression (parser);
8134 /* Otherwise, it must be that we are looking at a
8135 logical-or-expression. */
8136 else
8138 /* Parse the binary expressions (logical-or-expression). */
8139 expr = cp_parser_binary_expression (parser, cast_p, false,
8140 decltype_p,
8141 PREC_NOT_OPERATOR, pidk);
8142 /* If the next token is a `?' then we're actually looking at a
8143 conditional-expression. */
8144 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8145 return cp_parser_question_colon_clause (parser, expr);
8146 else
8148 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8150 /* If it's an assignment-operator, we're using the second
8151 production. */
8152 enum tree_code assignment_operator
8153 = cp_parser_assignment_operator_opt (parser);
8154 if (assignment_operator != ERROR_MARK)
8156 bool non_constant_p;
8157 location_t saved_input_location;
8159 /* Parse the right-hand side of the assignment. */
8160 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8162 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8165 /* An assignment may not appear in a
8166 constant-expression. */
8167 if (cp_parser_non_integral_constant_expression (parser,
8168 NIC_ASSIGNMENT))
8169 return error_mark_node;
8171 /* Check for and warn about self-assignment if -Wself-assign is
8172 enabled and the assignment operator is "=".
8173 Checking for non-POD self-assignment will be performed only
8174 when -Wself-assign-non-pod is enabled. */
8175 if (warn_self_assign
8176 && assignment_operator == NOP_EXPR
8177 && (warn_self_assign_non_pod || expr_is_pod (expr)))
8178 check_for_self_assign (input_location, expr, rhs);
8180 /* Build the assignment expression. Its default
8181 location is the location of the '=' token. */
8182 saved_input_location = input_location;
8183 input_location = loc;
8184 expr = build_x_modify_expr (loc, expr,
8185 assignment_operator,
8186 rhs,
8187 complain_flags (decltype_p));
8188 input_location = saved_input_location;
8193 return expr;
8196 static tree
8197 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8198 cp_id_kind * pidk)
8200 return cp_parser_assignment_expression (parser, cast_p,
8201 /*decltype*/false, pidk);
8204 /* Parse an (optional) assignment-operator.
8206 assignment-operator: one of
8207 = *= /= %= += -= >>= <<= &= ^= |=
8209 GNU Extension:
8211 assignment-operator: one of
8212 <?= >?=
8214 If the next token is an assignment operator, the corresponding tree
8215 code is returned, and the token is consumed. For example, for
8216 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8217 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8218 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8219 operator, ERROR_MARK is returned. */
8221 static enum tree_code
8222 cp_parser_assignment_operator_opt (cp_parser* parser)
8224 enum tree_code op;
8225 cp_token *token;
8227 /* Peek at the next token. */
8228 token = cp_lexer_peek_token (parser->lexer);
8230 switch (token->type)
8232 case CPP_EQ:
8233 op = NOP_EXPR;
8234 break;
8236 case CPP_MULT_EQ:
8237 op = MULT_EXPR;
8238 break;
8240 case CPP_DIV_EQ:
8241 op = TRUNC_DIV_EXPR;
8242 break;
8244 case CPP_MOD_EQ:
8245 op = TRUNC_MOD_EXPR;
8246 break;
8248 case CPP_PLUS_EQ:
8249 op = PLUS_EXPR;
8250 break;
8252 case CPP_MINUS_EQ:
8253 op = MINUS_EXPR;
8254 break;
8256 case CPP_RSHIFT_EQ:
8257 op = RSHIFT_EXPR;
8258 break;
8260 case CPP_LSHIFT_EQ:
8261 op = LSHIFT_EXPR;
8262 break;
8264 case CPP_AND_EQ:
8265 op = BIT_AND_EXPR;
8266 break;
8268 case CPP_XOR_EQ:
8269 op = BIT_XOR_EXPR;
8270 break;
8272 case CPP_OR_EQ:
8273 op = BIT_IOR_EXPR;
8274 break;
8276 default:
8277 /* Nothing else is an assignment operator. */
8278 op = ERROR_MARK;
8281 /* If it was an assignment operator, consume it. */
8282 if (op != ERROR_MARK)
8283 cp_lexer_consume_token (parser->lexer);
8285 return op;
8288 /* Parse an expression.
8290 expression:
8291 assignment-expression
8292 expression , assignment-expression
8294 CAST_P is true if this expression is the target of a cast.
8295 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8296 except possibly parenthesized or on the RHS of a comma (N3276).
8298 Returns a representation of the expression. */
8300 static tree
8301 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8302 cp_id_kind * pidk)
8304 tree expression = NULL_TREE;
8305 location_t loc = UNKNOWN_LOCATION;
8307 while (true)
8309 tree assignment_expression;
8311 /* Parse the next assignment-expression. */
8312 assignment_expression
8313 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8315 /* We don't create a temporary for a call that is the immediate operand
8316 of decltype or on the RHS of a comma. But when we see a comma, we
8317 need to create a temporary for a call on the LHS. */
8318 if (decltype_p && !processing_template_decl
8319 && TREE_CODE (assignment_expression) == CALL_EXPR
8320 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8321 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8322 assignment_expression
8323 = build_cplus_new (TREE_TYPE (assignment_expression),
8324 assignment_expression, tf_warning_or_error);
8326 /* If this is the first assignment-expression, we can just
8327 save it away. */
8328 if (!expression)
8329 expression = assignment_expression;
8330 else
8331 expression = build_x_compound_expr (loc, expression,
8332 assignment_expression,
8333 complain_flags (decltype_p));
8334 /* If the next token is not a comma, then we are done with the
8335 expression. */
8336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8337 break;
8338 /* Consume the `,'. */
8339 loc = cp_lexer_peek_token (parser->lexer)->location;
8340 cp_lexer_consume_token (parser->lexer);
8341 /* A comma operator cannot appear in a constant-expression. */
8342 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8343 expression = error_mark_node;
8346 return expression;
8349 static inline tree
8350 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8352 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8355 /* Parse a constant-expression.
8357 constant-expression:
8358 conditional-expression
8360 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8361 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8362 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8363 is false, NON_CONSTANT_P should be NULL. */
8365 static tree
8366 cp_parser_constant_expression (cp_parser* parser,
8367 bool allow_non_constant_p,
8368 bool *non_constant_p)
8370 bool saved_integral_constant_expression_p;
8371 bool saved_allow_non_integral_constant_expression_p;
8372 bool saved_non_integral_constant_expression_p;
8373 tree expression;
8375 /* It might seem that we could simply parse the
8376 conditional-expression, and then check to see if it were
8377 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8378 one that the compiler can figure out is constant, possibly after
8379 doing some simplifications or optimizations. The standard has a
8380 precise definition of constant-expression, and we must honor
8381 that, even though it is somewhat more restrictive.
8383 For example:
8385 int i[(2, 3)];
8387 is not a legal declaration, because `(2, 3)' is not a
8388 constant-expression. The `,' operator is forbidden in a
8389 constant-expression. However, GCC's constant-folding machinery
8390 will fold this operation to an INTEGER_CST for `3'. */
8392 /* Save the old settings. */
8393 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8394 saved_allow_non_integral_constant_expression_p
8395 = parser->allow_non_integral_constant_expression_p;
8396 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8397 /* We are now parsing a constant-expression. */
8398 parser->integral_constant_expression_p = true;
8399 parser->allow_non_integral_constant_expression_p
8400 = (allow_non_constant_p || cxx_dialect >= cxx11);
8401 parser->non_integral_constant_expression_p = false;
8402 /* Although the grammar says "conditional-expression", we parse an
8403 "assignment-expression", which also permits "throw-expression"
8404 and the use of assignment operators. In the case that
8405 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8406 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8407 actually essential that we look for an assignment-expression.
8408 For example, cp_parser_initializer_clauses uses this function to
8409 determine whether a particular assignment-expression is in fact
8410 constant. */
8411 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8412 /* Restore the old settings. */
8413 parser->integral_constant_expression_p
8414 = saved_integral_constant_expression_p;
8415 parser->allow_non_integral_constant_expression_p
8416 = saved_allow_non_integral_constant_expression_p;
8417 if (cxx_dialect >= cxx11)
8419 /* Require an rvalue constant expression here; that's what our
8420 callers expect. Reference constant expressions are handled
8421 separately in e.g. cp_parser_template_argument. */
8422 bool is_const = potential_rvalue_constant_expression (expression);
8423 parser->non_integral_constant_expression_p = !is_const;
8424 if (!is_const && !allow_non_constant_p)
8425 require_potential_rvalue_constant_expression (expression);
8427 if (allow_non_constant_p)
8428 *non_constant_p = parser->non_integral_constant_expression_p;
8429 parser->non_integral_constant_expression_p
8430 = saved_non_integral_constant_expression_p;
8432 return expression;
8435 /* Parse __builtin_offsetof.
8437 offsetof-expression:
8438 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8440 offsetof-member-designator:
8441 id-expression
8442 | offsetof-member-designator "." id-expression
8443 | offsetof-member-designator "[" expression "]"
8444 | offsetof-member-designator "->" id-expression */
8446 static tree
8447 cp_parser_builtin_offsetof (cp_parser *parser)
8449 int save_ice_p, save_non_ice_p;
8450 tree type, expr;
8451 cp_id_kind dummy;
8452 cp_token *token;
8454 /* We're about to accept non-integral-constant things, but will
8455 definitely yield an integral constant expression. Save and
8456 restore these values around our local parsing. */
8457 save_ice_p = parser->integral_constant_expression_p;
8458 save_non_ice_p = parser->non_integral_constant_expression_p;
8460 /* Consume the "__builtin_offsetof" token. */
8461 cp_lexer_consume_token (parser->lexer);
8462 /* Consume the opening `('. */
8463 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8464 /* Parse the type-id. */
8465 type = cp_parser_type_id (parser);
8466 /* Look for the `,'. */
8467 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8468 token = cp_lexer_peek_token (parser->lexer);
8470 /* Build the (type *)null that begins the traditional offsetof macro. */
8471 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8472 tf_warning_or_error);
8474 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8475 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8476 true, &dummy, token->location);
8477 while (true)
8479 token = cp_lexer_peek_token (parser->lexer);
8480 switch (token->type)
8482 case CPP_OPEN_SQUARE:
8483 /* offsetof-member-designator "[" expression "]" */
8484 expr = cp_parser_postfix_open_square_expression (parser, expr,
8485 true, false);
8486 break;
8488 case CPP_DEREF:
8489 /* offsetof-member-designator "->" identifier */
8490 expr = grok_array_decl (token->location, expr,
8491 integer_zero_node, false);
8492 /* FALLTHRU */
8494 case CPP_DOT:
8495 /* offsetof-member-designator "." identifier */
8496 cp_lexer_consume_token (parser->lexer);
8497 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8498 expr, true, &dummy,
8499 token->location);
8500 break;
8502 case CPP_CLOSE_PAREN:
8503 /* Consume the ")" token. */
8504 cp_lexer_consume_token (parser->lexer);
8505 goto success;
8507 default:
8508 /* Error. We know the following require will fail, but
8509 that gives the proper error message. */
8510 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8511 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8512 expr = error_mark_node;
8513 goto failure;
8517 success:
8518 /* If we're processing a template, we can't finish the semantics yet.
8519 Otherwise we can fold the entire expression now. */
8520 if (processing_template_decl)
8521 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8522 else
8523 expr = finish_offsetof (expr);
8525 failure:
8526 parser->integral_constant_expression_p = save_ice_p;
8527 parser->non_integral_constant_expression_p = save_non_ice_p;
8529 return expr;
8532 /* Parse a trait expression.
8534 Returns a representation of the expression, the underlying type
8535 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8537 static tree
8538 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8540 cp_trait_kind kind;
8541 tree type1, type2 = NULL_TREE;
8542 bool binary = false;
8543 cp_decl_specifier_seq decl_specs;
8545 switch (keyword)
8547 case RID_HAS_NOTHROW_ASSIGN:
8548 kind = CPTK_HAS_NOTHROW_ASSIGN;
8549 break;
8550 case RID_HAS_NOTHROW_CONSTRUCTOR:
8551 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8552 break;
8553 case RID_HAS_NOTHROW_COPY:
8554 kind = CPTK_HAS_NOTHROW_COPY;
8555 break;
8556 case RID_HAS_TRIVIAL_ASSIGN:
8557 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8558 break;
8559 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8560 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8561 break;
8562 case RID_HAS_TRIVIAL_COPY:
8563 kind = CPTK_HAS_TRIVIAL_COPY;
8564 break;
8565 case RID_HAS_TRIVIAL_DESTRUCTOR:
8566 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8567 break;
8568 case RID_HAS_VIRTUAL_DESTRUCTOR:
8569 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8570 break;
8571 case RID_IS_ABSTRACT:
8572 kind = CPTK_IS_ABSTRACT;
8573 break;
8574 case RID_IS_BASE_OF:
8575 kind = CPTK_IS_BASE_OF;
8576 binary = true;
8577 break;
8578 case RID_IS_CLASS:
8579 kind = CPTK_IS_CLASS;
8580 break;
8581 case RID_IS_CONVERTIBLE_TO:
8582 kind = CPTK_IS_CONVERTIBLE_TO;
8583 binary = true;
8584 break;
8585 case RID_IS_EMPTY:
8586 kind = CPTK_IS_EMPTY;
8587 break;
8588 case RID_IS_ENUM:
8589 kind = CPTK_IS_ENUM;
8590 break;
8591 case RID_IS_FINAL:
8592 kind = CPTK_IS_FINAL;
8593 break;
8594 case RID_IS_LITERAL_TYPE:
8595 kind = CPTK_IS_LITERAL_TYPE;
8596 break;
8597 case RID_IS_POD:
8598 kind = CPTK_IS_POD;
8599 break;
8600 case RID_IS_POLYMORPHIC:
8601 kind = CPTK_IS_POLYMORPHIC;
8602 break;
8603 case RID_IS_STD_LAYOUT:
8604 kind = CPTK_IS_STD_LAYOUT;
8605 break;
8606 case RID_IS_TRIVIAL:
8607 kind = CPTK_IS_TRIVIAL;
8608 break;
8609 case RID_IS_UNION:
8610 kind = CPTK_IS_UNION;
8611 break;
8612 case RID_UNDERLYING_TYPE:
8613 kind = CPTK_UNDERLYING_TYPE;
8614 break;
8615 case RID_BASES:
8616 kind = CPTK_BASES;
8617 break;
8618 case RID_DIRECT_BASES:
8619 kind = CPTK_DIRECT_BASES;
8620 break;
8621 default:
8622 gcc_unreachable ();
8625 /* Consume the token. */
8626 cp_lexer_consume_token (parser->lexer);
8628 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8630 type1 = cp_parser_type_id (parser);
8632 if (type1 == error_mark_node)
8633 return error_mark_node;
8635 /* Build a trivial decl-specifier-seq. */
8636 clear_decl_specs (&decl_specs);
8637 decl_specs.type = type1;
8639 /* Call grokdeclarator to figure out what type this is. */
8640 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8641 /*initialized=*/0, /*attrlist=*/NULL);
8643 if (binary)
8645 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8647 type2 = cp_parser_type_id (parser);
8649 if (type2 == error_mark_node)
8650 return error_mark_node;
8652 /* Build a trivial decl-specifier-seq. */
8653 clear_decl_specs (&decl_specs);
8654 decl_specs.type = type2;
8656 /* Call grokdeclarator to figure out what type this is. */
8657 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8658 /*initialized=*/0, /*attrlist=*/NULL);
8661 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8663 /* Complete the trait expression, which may mean either processing
8664 the trait expr now or saving it for template instantiation. */
8665 switch(kind)
8667 case CPTK_UNDERLYING_TYPE:
8668 return finish_underlying_type (type1);
8669 case CPTK_BASES:
8670 return finish_bases (type1, false);
8671 case CPTK_DIRECT_BASES:
8672 return finish_bases (type1, true);
8673 default:
8674 return finish_trait_expr (kind, type1, type2);
8678 /* Lambdas that appear in variable initializer or default argument scope
8679 get that in their mangling, so we need to record it. We might as well
8680 use the count for function and namespace scopes as well. */
8681 static GTY(()) tree lambda_scope;
8682 static GTY(()) int lambda_count;
8683 typedef struct GTY(()) tree_int
8685 tree t;
8686 int i;
8687 } tree_int;
8688 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8690 static void
8691 start_lambda_scope (tree decl)
8693 tree_int ti;
8694 gcc_assert (decl);
8695 /* Once we're inside a function, we ignore other scopes and just push
8696 the function again so that popping works properly. */
8697 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8698 decl = current_function_decl;
8699 ti.t = lambda_scope;
8700 ti.i = lambda_count;
8701 vec_safe_push (lambda_scope_stack, ti);
8702 if (lambda_scope != decl)
8704 /* Don't reset the count if we're still in the same function. */
8705 lambda_scope = decl;
8706 lambda_count = 0;
8710 static void
8711 record_lambda_scope (tree lambda)
8713 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8714 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8717 static void
8718 finish_lambda_scope (void)
8720 tree_int *p = &lambda_scope_stack->last ();
8721 if (lambda_scope != p->t)
8723 lambda_scope = p->t;
8724 lambda_count = p->i;
8726 lambda_scope_stack->pop ();
8729 /* Parse a lambda expression.
8731 lambda-expression:
8732 lambda-introducer lambda-declarator [opt] compound-statement
8734 Returns a representation of the expression. */
8736 static tree
8737 cp_parser_lambda_expression (cp_parser* parser)
8739 tree lambda_expr = build_lambda_expr ();
8740 tree type;
8741 bool ok = true;
8743 LAMBDA_EXPR_LOCATION (lambda_expr)
8744 = cp_lexer_peek_token (parser->lexer)->location;
8746 if (cp_unevaluated_operand)
8748 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8749 "lambda-expression in unevaluated context");
8750 ok = false;
8753 /* We may be in the middle of deferred access check. Disable
8754 it now. */
8755 push_deferring_access_checks (dk_no_deferred);
8757 cp_parser_lambda_introducer (parser, lambda_expr);
8759 type = begin_lambda_type (lambda_expr);
8760 if (type == error_mark_node)
8761 return error_mark_node;
8763 record_lambda_scope (lambda_expr);
8765 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8766 determine_visibility (TYPE_NAME (type));
8768 /* Now that we've started the type, add the capture fields for any
8769 explicit captures. */
8770 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8773 /* Inside the class, surrounding template-parameter-lists do not apply. */
8774 unsigned int saved_num_template_parameter_lists
8775 = parser->num_template_parameter_lists;
8776 unsigned char in_statement = parser->in_statement;
8777 bool in_switch_statement_p = parser->in_switch_statement_p;
8778 bool fully_implicit_function_template_p
8779 = parser->fully_implicit_function_template_p;
8780 tree implicit_template_parms = parser->implicit_template_parms;
8781 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8782 bool auto_is_implicit_function_template_parm_p
8783 = parser->auto_is_implicit_function_template_parm_p;
8785 parser->num_template_parameter_lists = 0;
8786 parser->in_statement = 0;
8787 parser->in_switch_statement_p = false;
8788 parser->fully_implicit_function_template_p = false;
8789 parser->implicit_template_parms = 0;
8790 parser->implicit_template_scope = 0;
8791 parser->auto_is_implicit_function_template_parm_p = false;
8793 /* By virtue of defining a local class, a lambda expression has access to
8794 the private variables of enclosing classes. */
8796 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8798 if (ok)
8799 cp_parser_lambda_body (parser, lambda_expr);
8800 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8802 if (cp_parser_skip_to_closing_brace (parser))
8803 cp_lexer_consume_token (parser->lexer);
8806 /* The capture list was built up in reverse order; fix that now. */
8807 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8808 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8810 if (ok)
8811 maybe_add_lambda_conv_op (type);
8813 type = finish_struct (type, /*attributes=*/NULL_TREE);
8815 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8816 parser->in_statement = in_statement;
8817 parser->in_switch_statement_p = in_switch_statement_p;
8818 parser->fully_implicit_function_template_p
8819 = fully_implicit_function_template_p;
8820 parser->implicit_template_parms = implicit_template_parms;
8821 parser->implicit_template_scope = implicit_template_scope;
8822 parser->auto_is_implicit_function_template_parm_p
8823 = auto_is_implicit_function_template_parm_p;
8826 pop_deferring_access_checks ();
8828 /* This field is only used during parsing of the lambda. */
8829 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8831 /* This lambda shouldn't have any proxies left at this point. */
8832 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8833 /* And now that we're done, push proxies for an enclosing lambda. */
8834 insert_pending_capture_proxies ();
8836 if (ok)
8837 return build_lambda_object (lambda_expr);
8838 else
8839 return error_mark_node;
8842 /* Parse the beginning of a lambda expression.
8844 lambda-introducer:
8845 [ lambda-capture [opt] ]
8847 LAMBDA_EXPR is the current representation of the lambda expression. */
8849 static void
8850 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8852 /* Need commas after the first capture. */
8853 bool first = true;
8855 /* Eat the leading `['. */
8856 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8858 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8859 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8860 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8861 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8862 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8863 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8865 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8867 cp_lexer_consume_token (parser->lexer);
8868 first = false;
8871 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8873 cp_token* capture_token;
8874 tree capture_id;
8875 tree capture_init_expr;
8876 cp_id_kind idk = CP_ID_KIND_NONE;
8877 bool explicit_init_p = false;
8879 enum capture_kind_type
8881 BY_COPY,
8882 BY_REFERENCE
8884 enum capture_kind_type capture_kind = BY_COPY;
8886 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8888 error ("expected end of capture-list");
8889 return;
8892 if (first)
8893 first = false;
8894 else
8895 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8897 /* Possibly capture `this'. */
8898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8900 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8901 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8902 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8903 "with by-copy capture default");
8904 cp_lexer_consume_token (parser->lexer);
8905 add_capture (lambda_expr,
8906 /*id=*/this_identifier,
8907 /*initializer=*/finish_this_expr(),
8908 /*by_reference_p=*/false,
8909 explicit_init_p);
8910 continue;
8913 /* Remember whether we want to capture as a reference or not. */
8914 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8916 capture_kind = BY_REFERENCE;
8917 cp_lexer_consume_token (parser->lexer);
8920 /* Get the identifier. */
8921 capture_token = cp_lexer_peek_token (parser->lexer);
8922 capture_id = cp_parser_identifier (parser);
8924 if (capture_id == error_mark_node)
8925 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8926 delimiters, but I modified this to stop on unnested ']' as well. It
8927 was already changed to stop on unnested '}', so the
8928 "closing_parenthesis" name is no more misleading with my change. */
8930 cp_parser_skip_to_closing_parenthesis (parser,
8931 /*recovering=*/true,
8932 /*or_comma=*/true,
8933 /*consume_paren=*/true);
8934 break;
8937 /* Find the initializer for this capture. */
8938 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8939 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8940 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8942 bool direct, non_constant;
8943 /* An explicit initializer exists. */
8944 if (cxx_dialect < cxx1y)
8945 pedwarn (input_location, 0,
8946 "lambda capture initializers "
8947 "only available with -std=c++1y or -std=gnu++1y");
8948 capture_init_expr = cp_parser_initializer (parser, &direct,
8949 &non_constant);
8950 explicit_init_p = true;
8951 if (capture_init_expr == NULL_TREE)
8953 error ("empty initializer for lambda init-capture");
8954 capture_init_expr = error_mark_node;
8957 else
8959 const char* error_msg;
8961 /* Turn the identifier into an id-expression. */
8962 capture_init_expr
8963 = cp_parser_lookup_name_simple (parser, capture_id,
8964 capture_token->location);
8966 if (capture_init_expr == error_mark_node)
8968 unqualified_name_lookup_error (capture_id);
8969 continue;
8971 else if (DECL_P (capture_init_expr)
8972 && (!VAR_P (capture_init_expr)
8973 && TREE_CODE (capture_init_expr) != PARM_DECL))
8975 error_at (capture_token->location,
8976 "capture of non-variable %qD ",
8977 capture_init_expr);
8978 inform (0, "%q+#D declared here", capture_init_expr);
8979 continue;
8981 if (VAR_P (capture_init_expr)
8982 && decl_storage_duration (capture_init_expr) != dk_auto)
8984 pedwarn (capture_token->location, 0, "capture of variable "
8985 "%qD with non-automatic storage duration",
8986 capture_init_expr);
8987 inform (0, "%q+#D declared here", capture_init_expr);
8988 continue;
8991 capture_init_expr
8992 = finish_id_expression
8993 (capture_id,
8994 capture_init_expr,
8995 parser->scope,
8996 &idk,
8997 /*integral_constant_expression_p=*/false,
8998 /*allow_non_integral_constant_expression_p=*/false,
8999 /*non_integral_constant_expression_p=*/NULL,
9000 /*template_p=*/false,
9001 /*done=*/true,
9002 /*address_p=*/false,
9003 /*template_arg_p=*/false,
9004 &error_msg,
9005 capture_token->location);
9007 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9009 cp_lexer_consume_token (parser->lexer);
9010 capture_init_expr = make_pack_expansion (capture_init_expr);
9012 else
9013 check_for_bare_parameter_packs (capture_init_expr);
9016 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9017 && !explicit_init_p)
9019 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9020 && capture_kind == BY_COPY)
9021 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9022 "of %qD redundant with by-copy capture default",
9023 capture_id);
9024 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9025 && capture_kind == BY_REFERENCE)
9026 pedwarn (capture_token->location, 0, "explicit by-reference "
9027 "capture of %qD redundant with by-reference capture "
9028 "default", capture_id);
9031 add_capture (lambda_expr,
9032 capture_id,
9033 capture_init_expr,
9034 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9035 explicit_init_p);
9038 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9041 /* Parse the (optional) middle of a lambda expression.
9043 lambda-declarator:
9044 < template-parameter-list [opt] >
9045 ( parameter-declaration-clause [opt] )
9046 attribute-specifier [opt]
9047 mutable [opt]
9048 exception-specification [opt]
9049 lambda-return-type-clause [opt]
9051 LAMBDA_EXPR is the current representation of the lambda expression. */
9053 static bool
9054 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9056 /* 5.1.1.4 of the standard says:
9057 If a lambda-expression does not include a lambda-declarator, it is as if
9058 the lambda-declarator were ().
9059 This means an empty parameter list, no attributes, and no exception
9060 specification. */
9061 tree param_list = void_list_node;
9062 tree attributes = NULL_TREE;
9063 tree exception_spec = NULL_TREE;
9064 tree template_param_list = NULL_TREE;
9066 /* The template-parameter-list is optional, but must begin with
9067 an opening angle if present. */
9068 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9070 if (cxx_dialect < cxx1y)
9071 pedwarn (parser->lexer->next_token->location, 0,
9072 "lambda templates are only available with "
9073 "-std=c++1y or -std=gnu++1y");
9075 cp_lexer_consume_token (parser->lexer);
9077 template_param_list = cp_parser_template_parameter_list (parser);
9079 cp_parser_skip_to_end_of_template_parameter_list (parser);
9081 /* We just processed one more parameter list. */
9082 ++parser->num_template_parameter_lists;
9085 /* The parameter-declaration-clause is optional (unless
9086 template-parameter-list was given), but must begin with an
9087 opening parenthesis if present. */
9088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9090 cp_lexer_consume_token (parser->lexer);
9092 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9094 /* Parse parameters. */
9095 param_list = cp_parser_parameter_declaration_clause (parser);
9097 /* Default arguments shall not be specified in the
9098 parameter-declaration-clause of a lambda-declarator. */
9099 for (tree t = param_list; t; t = TREE_CHAIN (t))
9100 if (TREE_PURPOSE (t))
9101 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9102 "default argument specified for lambda parameter");
9104 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9106 attributes = cp_parser_attributes_opt (parser);
9108 /* Parse optional `mutable' keyword. */
9109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9111 cp_lexer_consume_token (parser->lexer);
9112 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9115 /* Parse optional exception specification. */
9116 exception_spec = cp_parser_exception_specification_opt (parser);
9118 /* Parse optional trailing return type. */
9119 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9121 cp_lexer_consume_token (parser->lexer);
9122 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9123 = cp_parser_trailing_type_id (parser);
9126 /* The function parameters must be in scope all the way until after the
9127 trailing-return-type in case of decltype. */
9128 pop_bindings_and_leave_scope ();
9130 else if (template_param_list != NULL_TREE) // generate diagnostic
9131 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9133 /* Create the function call operator.
9135 Messing with declarators like this is no uglier than building up the
9136 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9137 other code. */
9139 cp_decl_specifier_seq return_type_specs;
9140 cp_declarator* declarator;
9141 tree fco;
9142 int quals;
9143 void *p;
9145 clear_decl_specs (&return_type_specs);
9146 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9147 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9148 else
9149 /* Maybe we will deduce the return type later. */
9150 return_type_specs.type = make_auto ();
9152 p = obstack_alloc (&declarator_obstack, 0);
9154 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9155 sfk_none);
9157 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9158 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9159 declarator = make_call_declarator (declarator, param_list, quals,
9160 VIRT_SPEC_UNSPECIFIED,
9161 REF_QUAL_NONE,
9162 exception_spec,
9163 /*late_return_type=*/NULL_TREE);
9164 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9166 fco = grokmethod (&return_type_specs,
9167 declarator,
9168 attributes);
9169 if (fco != error_mark_node)
9171 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9172 DECL_ARTIFICIAL (fco) = 1;
9173 /* Give the object parameter a different name. */
9174 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9176 if (template_param_list)
9178 fco = finish_member_template_decl (fco);
9179 finish_template_decl (template_param_list);
9180 --parser->num_template_parameter_lists;
9182 else if (parser->fully_implicit_function_template_p)
9183 fco = finish_fully_implicit_template (parser, fco);
9185 finish_member_declaration (fco);
9187 obstack_free (&declarator_obstack, p);
9189 return (fco != error_mark_node);
9193 /* Parse the body of a lambda expression, which is simply
9195 compound-statement
9197 but which requires special handling.
9198 LAMBDA_EXPR is the current representation of the lambda expression. */
9200 static void
9201 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9203 bool nested = (current_function_decl != NULL_TREE);
9204 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9205 if (nested)
9206 push_function_context ();
9207 else
9208 /* Still increment function_depth so that we don't GC in the
9209 middle of an expression. */
9210 ++function_depth;
9211 /* Clear this in case we're in the middle of a default argument. */
9212 parser->local_variables_forbidden_p = false;
9214 /* Finish the function call operator
9215 - class_specifier
9216 + late_parsing_for_member
9217 + function_definition_after_declarator
9218 + ctor_initializer_opt_and_function_body */
9220 tree fco = lambda_function (lambda_expr);
9221 tree body;
9222 bool done = false;
9223 tree compound_stmt;
9224 tree cap;
9226 /* Let the front end know that we are going to be defining this
9227 function. */
9228 start_preparsed_function (fco,
9229 NULL_TREE,
9230 SF_PRE_PARSED | SF_INCLASS_INLINE);
9232 start_lambda_scope (fco);
9233 body = begin_function_body ();
9235 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9236 goto out;
9238 /* Push the proxies for any explicit captures. */
9239 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9240 cap = TREE_CHAIN (cap))
9241 build_capture_proxy (TREE_PURPOSE (cap));
9243 compound_stmt = begin_compound_stmt (0);
9245 /* 5.1.1.4 of the standard says:
9246 If a lambda-expression does not include a trailing-return-type, it
9247 is as if the trailing-return-type denotes the following type:
9248 * if the compound-statement is of the form
9249 { return attribute-specifier [opt] expression ; }
9250 the type of the returned expression after lvalue-to-rvalue
9251 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9252 (_conv.array_ 4.2), and function-to-pointer conversion
9253 (_conv.func_ 4.3);
9254 * otherwise, void. */
9256 /* In a lambda that has neither a lambda-return-type-clause
9257 nor a deducible form, errors should be reported for return statements
9258 in the body. Since we used void as the placeholder return type, parsing
9259 the body as usual will give such desired behavior. */
9260 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9261 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9262 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9264 tree expr = NULL_TREE;
9265 cp_id_kind idk = CP_ID_KIND_NONE;
9267 /* Parse tentatively in case there's more after the initial return
9268 statement. */
9269 cp_parser_parse_tentatively (parser);
9271 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9273 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9275 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9276 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9278 if (cp_parser_parse_definitely (parser))
9280 if (!processing_template_decl)
9281 apply_deduced_return_type (fco, lambda_return_type (expr));
9283 /* Will get error here if type not deduced yet. */
9284 finish_return_stmt (expr);
9286 done = true;
9290 if (!done)
9292 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9293 cp_parser_label_declaration (parser);
9294 cp_parser_statement_seq_opt (parser, NULL_TREE);
9295 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9298 finish_compound_stmt (compound_stmt);
9300 out:
9301 finish_function_body (body);
9302 finish_lambda_scope ();
9304 /* Finish the function and generate code for it if necessary. */
9305 tree fn = finish_function (/*inline*/2);
9307 /* Only expand if the call op is not a template. */
9308 if (!DECL_TEMPLATE_INFO (fco))
9309 expand_or_defer_fn (fn);
9312 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9313 if (nested)
9314 pop_function_context();
9315 else
9316 --function_depth;
9319 /* Statements [gram.stmt.stmt] */
9321 /* Parse a statement.
9323 statement:
9324 labeled-statement
9325 expression-statement
9326 compound-statement
9327 selection-statement
9328 iteration-statement
9329 jump-statement
9330 declaration-statement
9331 try-block
9333 C++11:
9335 statement:
9336 labeled-statement
9337 attribute-specifier-seq (opt) expression-statement
9338 attribute-specifier-seq (opt) compound-statement
9339 attribute-specifier-seq (opt) selection-statement
9340 attribute-specifier-seq (opt) iteration-statement
9341 attribute-specifier-seq (opt) jump-statement
9342 declaration-statement
9343 attribute-specifier-seq (opt) try-block
9345 TM Extension:
9347 statement:
9348 atomic-statement
9350 IN_COMPOUND is true when the statement is nested inside a
9351 cp_parser_compound_statement; this matters for certain pragmas.
9353 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9354 is a (possibly labeled) if statement which is not enclosed in braces
9355 and has an else clause. This is used to implement -Wparentheses. */
9357 static void
9358 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9359 bool in_compound, bool *if_p)
9361 tree statement, std_attrs = NULL_TREE;
9362 cp_token *token;
9363 location_t statement_location, attrs_location;
9365 restart:
9366 if (if_p != NULL)
9367 *if_p = false;
9368 /* There is no statement yet. */
9369 statement = NULL_TREE;
9371 cp_lexer_save_tokens (parser->lexer);
9372 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9373 if (c_dialect_objc ())
9374 /* In obj-c++, seeing '[[' might be the either the beginning of
9375 c++11 attributes, or a nested objc-message-expression. So
9376 let's parse the c++11 attributes tentatively. */
9377 cp_parser_parse_tentatively (parser);
9378 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9379 if (c_dialect_objc ())
9381 if (!cp_parser_parse_definitely (parser))
9382 std_attrs = NULL_TREE;
9385 /* Peek at the next token. */
9386 token = cp_lexer_peek_token (parser->lexer);
9387 /* Remember the location of the first token in the statement. */
9388 statement_location = token->location;
9389 /* If this is a keyword, then that will often determine what kind of
9390 statement we have. */
9391 if (token->type == CPP_KEYWORD)
9393 enum rid keyword = token->keyword;
9395 switch (keyword)
9397 case RID_CASE:
9398 case RID_DEFAULT:
9399 /* Looks like a labeled-statement with a case label.
9400 Parse the label, and then use tail recursion to parse
9401 the statement. */
9402 cp_parser_label_for_labeled_statement (parser, std_attrs);
9403 goto restart;
9405 case RID_IF:
9406 case RID_SWITCH:
9407 statement = cp_parser_selection_statement (parser, if_p);
9408 break;
9410 case RID_WHILE:
9411 case RID_DO:
9412 case RID_FOR:
9413 statement = cp_parser_iteration_statement (parser, false);
9414 break;
9416 case RID_BREAK:
9417 case RID_CONTINUE:
9418 case RID_RETURN:
9419 case RID_GOTO:
9420 statement = cp_parser_jump_statement (parser);
9421 break;
9423 /* Objective-C++ exception-handling constructs. */
9424 case RID_AT_TRY:
9425 case RID_AT_CATCH:
9426 case RID_AT_FINALLY:
9427 case RID_AT_SYNCHRONIZED:
9428 case RID_AT_THROW:
9429 statement = cp_parser_objc_statement (parser);
9430 break;
9432 case RID_TRY:
9433 statement = cp_parser_try_block (parser);
9434 break;
9436 case RID_NAMESPACE:
9437 /* This must be a namespace alias definition. */
9438 cp_parser_declaration_statement (parser);
9439 return;
9441 case RID_TRANSACTION_ATOMIC:
9442 case RID_TRANSACTION_RELAXED:
9443 statement = cp_parser_transaction (parser, keyword);
9444 break;
9445 case RID_TRANSACTION_CANCEL:
9446 statement = cp_parser_transaction_cancel (parser);
9447 break;
9449 default:
9450 /* It might be a keyword like `int' that can start a
9451 declaration-statement. */
9452 break;
9455 else if (token->type == CPP_NAME)
9457 /* If the next token is a `:', then we are looking at a
9458 labeled-statement. */
9459 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9460 if (token->type == CPP_COLON)
9462 /* Looks like a labeled-statement with an ordinary label.
9463 Parse the label, and then use tail recursion to parse
9464 the statement. */
9466 cp_parser_label_for_labeled_statement (parser, std_attrs);
9467 goto restart;
9470 /* Anything that starts with a `{' must be a compound-statement. */
9471 else if (token->type == CPP_OPEN_BRACE)
9472 statement = cp_parser_compound_statement (parser, NULL, false, false);
9473 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9474 a statement all its own. */
9475 else if (token->type == CPP_PRAGMA)
9477 /* Only certain OpenMP pragmas are attached to statements, and thus
9478 are considered statements themselves. All others are not. In
9479 the context of a compound, accept the pragma as a "statement" and
9480 return so that we can check for a close brace. Otherwise we
9481 require a real statement and must go back and read one. */
9482 if (in_compound)
9483 cp_parser_pragma (parser, pragma_compound);
9484 else if (!cp_parser_pragma (parser, pragma_stmt))
9485 goto restart;
9486 return;
9488 else if (token->type == CPP_EOF)
9490 cp_parser_error (parser, "expected statement");
9491 return;
9494 /* Everything else must be a declaration-statement or an
9495 expression-statement. Try for the declaration-statement
9496 first, unless we are looking at a `;', in which case we know that
9497 we have an expression-statement. */
9498 if (!statement)
9500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9502 if (std_attrs != NULL_TREE)
9504 /* Attributes should be parsed as part of the the
9505 declaration, so let's un-parse them. */
9506 cp_lexer_rollback_tokens (parser->lexer);
9507 std_attrs = NULL_TREE;
9510 cp_parser_parse_tentatively (parser);
9511 /* Try to parse the declaration-statement. */
9512 cp_parser_declaration_statement (parser);
9513 /* If that worked, we're done. */
9514 if (cp_parser_parse_definitely (parser))
9515 return;
9517 /* Look for an expression-statement instead. */
9518 statement = cp_parser_expression_statement (parser, in_statement_expr);
9521 /* Set the line number for the statement. */
9522 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9523 SET_EXPR_LOCATION (statement, statement_location);
9525 /* Note that for now, we don't do anything with c++11 statements
9526 parsed at this level. */
9527 if (std_attrs != NULL_TREE)
9528 warning_at (attrs_location,
9529 OPT_Wattributes,
9530 "attributes at the beginning of statement are ignored");
9533 /* Parse the label for a labeled-statement, i.e.
9535 identifier :
9536 case constant-expression :
9537 default :
9539 GNU Extension:
9540 case constant-expression ... constant-expression : statement
9542 When a label is parsed without errors, the label is added to the
9543 parse tree by the finish_* functions, so this function doesn't
9544 have to return the label. */
9546 static void
9547 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9549 cp_token *token;
9550 tree label = NULL_TREE;
9551 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9553 /* The next token should be an identifier. */
9554 token = cp_lexer_peek_token (parser->lexer);
9555 if (token->type != CPP_NAME
9556 && token->type != CPP_KEYWORD)
9558 cp_parser_error (parser, "expected labeled-statement");
9559 return;
9562 parser->colon_corrects_to_scope_p = false;
9563 switch (token->keyword)
9565 case RID_CASE:
9567 tree expr, expr_hi;
9568 cp_token *ellipsis;
9570 /* Consume the `case' token. */
9571 cp_lexer_consume_token (parser->lexer);
9572 /* Parse the constant-expression. */
9573 expr = cp_parser_constant_expression (parser,
9574 /*allow_non_constant_p=*/false,
9575 NULL);
9577 ellipsis = cp_lexer_peek_token (parser->lexer);
9578 if (ellipsis->type == CPP_ELLIPSIS)
9580 /* Consume the `...' token. */
9581 cp_lexer_consume_token (parser->lexer);
9582 expr_hi =
9583 cp_parser_constant_expression (parser,
9584 /*allow_non_constant_p=*/false,
9585 NULL);
9586 /* We don't need to emit warnings here, as the common code
9587 will do this for us. */
9589 else
9590 expr_hi = NULL_TREE;
9592 if (parser->in_switch_statement_p)
9593 finish_case_label (token->location, expr, expr_hi);
9594 else
9595 error_at (token->location,
9596 "case label %qE not within a switch statement",
9597 expr);
9599 break;
9601 case RID_DEFAULT:
9602 /* Consume the `default' token. */
9603 cp_lexer_consume_token (parser->lexer);
9605 if (parser->in_switch_statement_p)
9606 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9607 else
9608 error_at (token->location, "case label not within a switch statement");
9609 break;
9611 default:
9612 /* Anything else must be an ordinary label. */
9613 label = finish_label_stmt (cp_parser_identifier (parser));
9614 break;
9617 /* Require the `:' token. */
9618 cp_parser_require (parser, CPP_COLON, RT_COLON);
9620 /* An ordinary label may optionally be followed by attributes.
9621 However, this is only permitted if the attributes are then
9622 followed by a semicolon. This is because, for backward
9623 compatibility, when parsing
9624 lab: __attribute__ ((unused)) int i;
9625 we want the attribute to attach to "i", not "lab". */
9626 if (label != NULL_TREE
9627 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9629 tree attrs;
9630 cp_parser_parse_tentatively (parser);
9631 attrs = cp_parser_gnu_attributes_opt (parser);
9632 if (attrs == NULL_TREE
9633 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9634 cp_parser_abort_tentative_parse (parser);
9635 else if (!cp_parser_parse_definitely (parser))
9637 else
9638 attributes = chainon (attributes, attrs);
9641 if (attributes != NULL_TREE)
9642 cplus_decl_attributes (&label, attributes, 0);
9644 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9647 /* Parse an expression-statement.
9649 expression-statement:
9650 expression [opt] ;
9652 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9653 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9654 indicates whether this expression-statement is part of an
9655 expression statement. */
9657 static tree
9658 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9660 tree statement = NULL_TREE;
9661 cp_token *token = cp_lexer_peek_token (parser->lexer);
9663 /* If the next token is a ';', then there is no expression
9664 statement. */
9665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9667 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9668 if (statement == error_mark_node
9669 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9671 cp_parser_skip_to_end_of_block_or_statement (parser);
9672 return error_mark_node;
9676 /* Give a helpful message for "A<T>::type t;" and the like. */
9677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9678 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9680 if (TREE_CODE (statement) == SCOPE_REF)
9681 error_at (token->location, "need %<typename%> before %qE because "
9682 "%qT is a dependent scope",
9683 statement, TREE_OPERAND (statement, 0));
9684 else if (is_overloaded_fn (statement)
9685 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9687 /* A::A a; */
9688 tree fn = get_first_fn (statement);
9689 error_at (token->location,
9690 "%<%T::%D%> names the constructor, not the type",
9691 DECL_CONTEXT (fn), DECL_NAME (fn));
9695 /* Consume the final `;'. */
9696 cp_parser_consume_semicolon_at_end_of_statement (parser);
9698 if (in_statement_expr
9699 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9700 /* This is the final expression statement of a statement
9701 expression. */
9702 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9703 else if (statement)
9704 statement = finish_expr_stmt (statement);
9706 return statement;
9709 /* Parse a compound-statement.
9711 compound-statement:
9712 { statement-seq [opt] }
9714 GNU extension:
9716 compound-statement:
9717 { label-declaration-seq [opt] statement-seq [opt] }
9719 label-declaration-seq:
9720 label-declaration
9721 label-declaration-seq label-declaration
9723 Returns a tree representing the statement. */
9725 static tree
9726 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9727 bool in_try, bool function_body)
9729 tree compound_stmt;
9731 /* Consume the `{'. */
9732 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9733 return error_mark_node;
9734 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9735 && !function_body)
9736 pedwarn (input_location, OPT_Wpedantic,
9737 "compound-statement in constexpr function");
9738 /* Begin the compound-statement. */
9739 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9740 /* If the next keyword is `__label__' we have a label declaration. */
9741 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9742 cp_parser_label_declaration (parser);
9743 /* Parse an (optional) statement-seq. */
9744 cp_parser_statement_seq_opt (parser, in_statement_expr);
9745 /* Finish the compound-statement. */
9746 finish_compound_stmt (compound_stmt);
9747 /* Consume the `}'. */
9748 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9750 return compound_stmt;
9753 /* Parse an (optional) statement-seq.
9755 statement-seq:
9756 statement
9757 statement-seq [opt] statement */
9759 static void
9760 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9762 /* Scan statements until there aren't any more. */
9763 while (true)
9765 cp_token *token = cp_lexer_peek_token (parser->lexer);
9767 /* If we are looking at a `}', then we have run out of
9768 statements; the same is true if we have reached the end
9769 of file, or have stumbled upon a stray '@end'. */
9770 if (token->type == CPP_CLOSE_BRACE
9771 || token->type == CPP_EOF
9772 || token->type == CPP_PRAGMA_EOL
9773 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9774 break;
9776 /* If we are in a compound statement and find 'else' then
9777 something went wrong. */
9778 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9780 if (parser->in_statement & IN_IF_STMT)
9781 break;
9782 else
9784 token = cp_lexer_consume_token (parser->lexer);
9785 error_at (token->location, "%<else%> without a previous %<if%>");
9789 /* Parse the statement. */
9790 cp_parser_statement (parser, in_statement_expr, true, NULL);
9794 /* Parse a selection-statement.
9796 selection-statement:
9797 if ( condition ) statement
9798 if ( condition ) statement else statement
9799 switch ( condition ) statement
9801 Returns the new IF_STMT or SWITCH_STMT.
9803 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9804 is a (possibly labeled) if statement which is not enclosed in
9805 braces and has an else clause. This is used to implement
9806 -Wparentheses. */
9808 static tree
9809 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9811 cp_token *token;
9812 enum rid keyword;
9814 if (if_p != NULL)
9815 *if_p = false;
9817 /* Peek at the next token. */
9818 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9820 /* See what kind of keyword it is. */
9821 keyword = token->keyword;
9822 switch (keyword)
9824 case RID_IF:
9825 case RID_SWITCH:
9827 tree statement;
9828 tree condition;
9830 /* Look for the `('. */
9831 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9833 cp_parser_skip_to_end_of_statement (parser);
9834 return error_mark_node;
9837 /* Begin the selection-statement. */
9838 if (keyword == RID_IF)
9839 statement = begin_if_stmt ();
9840 else
9841 statement = begin_switch_stmt ();
9843 /* Parse the condition. */
9844 condition = cp_parser_condition (parser);
9845 /* Look for the `)'. */
9846 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9847 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9848 /*consume_paren=*/true);
9850 if (keyword == RID_IF)
9852 bool nested_if;
9853 unsigned char in_statement;
9855 /* Add the condition. */
9856 finish_if_stmt_cond (condition, statement);
9858 /* Parse the then-clause. */
9859 in_statement = parser->in_statement;
9860 parser->in_statement |= IN_IF_STMT;
9861 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9863 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9864 add_stmt (build_empty_stmt (loc));
9865 cp_lexer_consume_token (parser->lexer);
9866 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9867 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9868 "empty body in an %<if%> statement");
9869 nested_if = false;
9871 else
9872 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9873 parser->in_statement = in_statement;
9875 finish_then_clause (statement);
9877 /* If the next token is `else', parse the else-clause. */
9878 if (cp_lexer_next_token_is_keyword (parser->lexer,
9879 RID_ELSE))
9881 /* Consume the `else' keyword. */
9882 cp_lexer_consume_token (parser->lexer);
9883 begin_else_clause (statement);
9884 /* Parse the else-clause. */
9885 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9887 location_t loc;
9888 loc = cp_lexer_peek_token (parser->lexer)->location;
9889 warning_at (loc,
9890 OPT_Wempty_body, "suggest braces around "
9891 "empty body in an %<else%> statement");
9892 add_stmt (build_empty_stmt (loc));
9893 cp_lexer_consume_token (parser->lexer);
9895 else
9896 cp_parser_implicitly_scoped_statement (parser, NULL);
9898 finish_else_clause (statement);
9900 /* If we are currently parsing a then-clause, then
9901 IF_P will not be NULL. We set it to true to
9902 indicate that this if statement has an else clause.
9903 This may trigger the Wparentheses warning below
9904 when we get back up to the parent if statement. */
9905 if (if_p != NULL)
9906 *if_p = true;
9908 else
9910 /* This if statement does not have an else clause. If
9911 NESTED_IF is true, then the then-clause is an if
9912 statement which does have an else clause. We warn
9913 about the potential ambiguity. */
9914 if (nested_if)
9915 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9916 "suggest explicit braces to avoid ambiguous"
9917 " %<else%>");
9920 /* Now we're all done with the if-statement. */
9921 finish_if_stmt (statement);
9923 else
9925 bool in_switch_statement_p;
9926 unsigned char in_statement;
9928 /* Add the condition. */
9929 finish_switch_cond (condition, statement);
9931 /* Parse the body of the switch-statement. */
9932 in_switch_statement_p = parser->in_switch_statement_p;
9933 in_statement = parser->in_statement;
9934 parser->in_switch_statement_p = true;
9935 parser->in_statement |= IN_SWITCH_STMT;
9936 cp_parser_implicitly_scoped_statement (parser, NULL);
9937 parser->in_switch_statement_p = in_switch_statement_p;
9938 parser->in_statement = in_statement;
9940 /* Now we're all done with the switch-statement. */
9941 finish_switch_stmt (statement);
9944 return statement;
9946 break;
9948 default:
9949 cp_parser_error (parser, "expected selection-statement");
9950 return error_mark_node;
9954 /* Parse a condition.
9956 condition:
9957 expression
9958 type-specifier-seq declarator = initializer-clause
9959 type-specifier-seq declarator braced-init-list
9961 GNU Extension:
9963 condition:
9964 type-specifier-seq declarator asm-specification [opt]
9965 attributes [opt] = assignment-expression
9967 Returns the expression that should be tested. */
9969 static tree
9970 cp_parser_condition (cp_parser* parser)
9972 cp_decl_specifier_seq type_specifiers;
9973 const char *saved_message;
9974 int declares_class_or_enum;
9976 /* Try the declaration first. */
9977 cp_parser_parse_tentatively (parser);
9978 /* New types are not allowed in the type-specifier-seq for a
9979 condition. */
9980 saved_message = parser->type_definition_forbidden_message;
9981 parser->type_definition_forbidden_message
9982 = G_("types may not be defined in conditions");
9983 /* Parse the type-specifier-seq. */
9984 cp_parser_decl_specifier_seq (parser,
9985 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9986 &type_specifiers,
9987 &declares_class_or_enum);
9988 /* Restore the saved message. */
9989 parser->type_definition_forbidden_message = saved_message;
9990 /* If all is well, we might be looking at a declaration. */
9991 if (!cp_parser_error_occurred (parser))
9993 tree decl;
9994 tree asm_specification;
9995 tree attributes;
9996 cp_declarator *declarator;
9997 tree initializer = NULL_TREE;
9999 /* Parse the declarator. */
10000 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10001 /*ctor_dtor_or_conv_p=*/NULL,
10002 /*parenthesized_p=*/NULL,
10003 /*member_p=*/false);
10004 /* Parse the attributes. */
10005 attributes = cp_parser_attributes_opt (parser);
10006 /* Parse the asm-specification. */
10007 asm_specification = cp_parser_asm_specification_opt (parser);
10008 /* If the next token is not an `=' or '{', then we might still be
10009 looking at an expression. For example:
10011 if (A(a).x)
10013 looks like a decl-specifier-seq and a declarator -- but then
10014 there is no `=', so this is an expression. */
10015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10016 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10017 cp_parser_simulate_error (parser);
10019 /* If we did see an `=' or '{', then we are looking at a declaration
10020 for sure. */
10021 if (cp_parser_parse_definitely (parser))
10023 tree pushed_scope;
10024 bool non_constant_p;
10025 bool flags = LOOKUP_ONLYCONVERTING;
10027 /* Create the declaration. */
10028 decl = start_decl (declarator, &type_specifiers,
10029 /*initialized_p=*/true,
10030 attributes, /*prefix_attributes=*/NULL_TREE,
10031 &pushed_scope);
10033 /* Parse the initializer. */
10034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10036 initializer = cp_parser_braced_list (parser, &non_constant_p);
10037 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10038 flags = 0;
10040 else
10042 /* Consume the `='. */
10043 cp_parser_require (parser, CPP_EQ, RT_EQ);
10044 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10046 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10047 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10049 /* Process the initializer. */
10050 cp_finish_decl (decl,
10051 initializer, !non_constant_p,
10052 asm_specification,
10053 flags);
10055 if (pushed_scope)
10056 pop_scope (pushed_scope);
10058 return convert_from_reference (decl);
10061 /* If we didn't even get past the declarator successfully, we are
10062 definitely not looking at a declaration. */
10063 else
10064 cp_parser_abort_tentative_parse (parser);
10066 /* Otherwise, we are looking at an expression. */
10067 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10070 /* Parses a for-statement or range-for-statement until the closing ')',
10071 not included. */
10073 static tree
10074 cp_parser_for (cp_parser *parser, bool ivdep)
10076 tree init, scope, decl;
10077 bool is_range_for;
10079 /* Begin the for-statement. */
10080 scope = begin_for_scope (&init);
10082 /* Parse the initialization. */
10083 is_range_for = cp_parser_for_init_statement (parser, &decl);
10085 if (is_range_for)
10086 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10087 else
10088 return cp_parser_c_for (parser, scope, init, ivdep);
10091 static tree
10092 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10094 /* Normal for loop */
10095 tree condition = NULL_TREE;
10096 tree expression = NULL_TREE;
10097 tree stmt;
10099 stmt = begin_for_stmt (scope, init);
10100 /* The for-init-statement has already been parsed in
10101 cp_parser_for_init_statement, so no work is needed here. */
10102 finish_for_init_stmt (stmt);
10104 /* If there's a condition, process it. */
10105 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10106 condition = cp_parser_condition (parser);
10107 else if (ivdep)
10109 cp_parser_error (parser, "missing loop condition in loop with "
10110 "%<GCC ivdep%> pragma");
10111 condition = error_mark_node;
10113 finish_for_cond (condition, stmt, ivdep);
10114 /* Look for the `;'. */
10115 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10117 /* If there's an expression, process it. */
10118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10119 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10120 finish_for_expr (expression, stmt);
10122 return stmt;
10125 /* Tries to parse a range-based for-statement:
10127 range-based-for:
10128 decl-specifier-seq declarator : expression
10130 The decl-specifier-seq declarator and the `:' are already parsed by
10131 cp_parser_for_init_statement. If processing_template_decl it returns a
10132 newly created RANGE_FOR_STMT; if not, it is converted to a
10133 regular FOR_STMT. */
10135 static tree
10136 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10137 bool ivdep)
10139 tree stmt, range_expr;
10141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10143 bool expr_non_constant_p;
10144 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10146 else
10147 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10149 /* If in template, STMT is converted to a normal for-statement
10150 at instantiation. If not, it is done just ahead. */
10151 if (processing_template_decl)
10153 if (check_for_bare_parameter_packs (range_expr))
10154 range_expr = error_mark_node;
10155 stmt = begin_range_for_stmt (scope, init);
10156 if (ivdep)
10157 RANGE_FOR_IVDEP (stmt) = 1;
10158 finish_range_for_decl (stmt, range_decl, range_expr);
10159 if (!type_dependent_expression_p (range_expr)
10160 /* do_auto_deduction doesn't mess with template init-lists. */
10161 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10162 do_range_for_auto_deduction (range_decl, range_expr);
10164 else
10166 stmt = begin_for_stmt (scope, init);
10167 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10169 return stmt;
10172 /* Subroutine of cp_convert_range_for: given the initializer expression,
10173 builds up the range temporary. */
10175 static tree
10176 build_range_temp (tree range_expr)
10178 tree range_type, range_temp;
10180 /* Find out the type deduced by the declaration
10181 `auto &&__range = range_expr'. */
10182 range_type = cp_build_reference_type (make_auto (), true);
10183 range_type = do_auto_deduction (range_type, range_expr,
10184 type_uses_auto (range_type));
10186 /* Create the __range variable. */
10187 range_temp = build_decl (input_location, VAR_DECL,
10188 get_identifier ("__for_range"), range_type);
10189 TREE_USED (range_temp) = 1;
10190 DECL_ARTIFICIAL (range_temp) = 1;
10192 return range_temp;
10195 /* Used by cp_parser_range_for in template context: we aren't going to
10196 do a full conversion yet, but we still need to resolve auto in the
10197 type of the for-range-declaration if present. This is basically
10198 a shortcut version of cp_convert_range_for. */
10200 static void
10201 do_range_for_auto_deduction (tree decl, tree range_expr)
10203 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10204 if (auto_node)
10206 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10207 range_temp = convert_from_reference (build_range_temp (range_expr));
10208 iter_type = (cp_parser_perform_range_for_lookup
10209 (range_temp, &begin_dummy, &end_dummy));
10210 if (iter_type)
10212 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10213 iter_type);
10214 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10215 tf_warning_or_error);
10216 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10217 iter_decl, auto_node);
10222 /* Converts a range-based for-statement into a normal
10223 for-statement, as per the definition.
10225 for (RANGE_DECL : RANGE_EXPR)
10226 BLOCK
10228 should be equivalent to:
10231 auto &&__range = RANGE_EXPR;
10232 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10233 __begin != __end;
10234 ++__begin)
10236 RANGE_DECL = *__begin;
10237 BLOCK
10241 If RANGE_EXPR is an array:
10242 BEGIN_EXPR = __range
10243 END_EXPR = __range + ARRAY_SIZE(__range)
10244 Else if RANGE_EXPR has a member 'begin' or 'end':
10245 BEGIN_EXPR = __range.begin()
10246 END_EXPR = __range.end()
10247 Else:
10248 BEGIN_EXPR = begin(__range)
10249 END_EXPR = end(__range);
10251 If __range has a member 'begin' but not 'end', or vice versa, we must
10252 still use the second alternative (it will surely fail, however).
10253 When calling begin()/end() in the third alternative we must use
10254 argument dependent lookup, but always considering 'std' as an associated
10255 namespace. */
10257 tree
10258 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10259 bool ivdep)
10261 tree begin, end;
10262 tree iter_type, begin_expr, end_expr;
10263 tree condition, expression;
10265 if (range_decl == error_mark_node || range_expr == error_mark_node)
10266 /* If an error happened previously do nothing or else a lot of
10267 unhelpful errors would be issued. */
10268 begin_expr = end_expr = iter_type = error_mark_node;
10269 else
10271 tree range_temp;
10273 if (TREE_CODE (range_expr) == VAR_DECL
10274 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10275 /* Can't bind a reference to an array of runtime bound. */
10276 range_temp = range_expr;
10277 else
10279 range_temp = build_range_temp (range_expr);
10280 pushdecl (range_temp);
10281 cp_finish_decl (range_temp, range_expr,
10282 /*is_constant_init*/false, NULL_TREE,
10283 LOOKUP_ONLYCONVERTING);
10284 range_temp = convert_from_reference (range_temp);
10286 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10287 &begin_expr, &end_expr);
10290 /* The new for initialization statement. */
10291 begin = build_decl (input_location, VAR_DECL,
10292 get_identifier ("__for_begin"), iter_type);
10293 TREE_USED (begin) = 1;
10294 DECL_ARTIFICIAL (begin) = 1;
10295 pushdecl (begin);
10296 cp_finish_decl (begin, begin_expr,
10297 /*is_constant_init*/false, NULL_TREE,
10298 LOOKUP_ONLYCONVERTING);
10300 end = build_decl (input_location, VAR_DECL,
10301 get_identifier ("__for_end"), iter_type);
10302 TREE_USED (end) = 1;
10303 DECL_ARTIFICIAL (end) = 1;
10304 pushdecl (end);
10305 cp_finish_decl (end, end_expr,
10306 /*is_constant_init*/false, NULL_TREE,
10307 LOOKUP_ONLYCONVERTING);
10309 finish_for_init_stmt (statement);
10311 /* The new for condition. */
10312 condition = build_x_binary_op (input_location, NE_EXPR,
10313 begin, ERROR_MARK,
10314 end, ERROR_MARK,
10315 NULL, tf_warning_or_error);
10316 finish_for_cond (condition, statement, ivdep);
10318 /* The new increment expression. */
10319 expression = finish_unary_op_expr (input_location,
10320 PREINCREMENT_EXPR, begin,
10321 tf_warning_or_error);
10322 finish_for_expr (expression, statement);
10324 /* The declaration is initialized with *__begin inside the loop body. */
10325 cp_finish_decl (range_decl,
10326 build_x_indirect_ref (input_location, begin, RO_NULL,
10327 tf_warning_or_error),
10328 /*is_constant_init*/false, NULL_TREE,
10329 LOOKUP_ONLYCONVERTING);
10331 return statement;
10334 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10335 We need to solve both at the same time because the method used
10336 depends on the existence of members begin or end.
10337 Returns the type deduced for the iterator expression. */
10339 static tree
10340 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10342 if (error_operand_p (range))
10344 *begin = *end = error_mark_node;
10345 return error_mark_node;
10348 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10350 error ("range-based %<for%> expression of type %qT "
10351 "has incomplete type", TREE_TYPE (range));
10352 *begin = *end = error_mark_node;
10353 return error_mark_node;
10355 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10357 /* If RANGE is an array, we will use pointer arithmetic. */
10358 *begin = range;
10359 *end = build_binary_op (input_location, PLUS_EXPR,
10360 range,
10361 array_type_nelts_top (TREE_TYPE (range)),
10363 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10365 else
10367 /* If it is not an array, we must do a bit of magic. */
10368 tree id_begin, id_end;
10369 tree member_begin, member_end;
10371 *begin = *end = error_mark_node;
10373 id_begin = get_identifier ("begin");
10374 id_end = get_identifier ("end");
10375 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10376 /*protect=*/2, /*want_type=*/false,
10377 tf_warning_or_error);
10378 member_end = lookup_member (TREE_TYPE (range), id_end,
10379 /*protect=*/2, /*want_type=*/false,
10380 tf_warning_or_error);
10382 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10384 /* Use the member functions. */
10385 if (member_begin != NULL_TREE)
10386 *begin = cp_parser_range_for_member_function (range, id_begin);
10387 else
10388 error ("range-based %<for%> expression of type %qT has an "
10389 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10391 if (member_end != NULL_TREE)
10392 *end = cp_parser_range_for_member_function (range, id_end);
10393 else
10394 error ("range-based %<for%> expression of type %qT has a "
10395 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10397 else
10399 /* Use global functions with ADL. */
10400 vec<tree, va_gc> *vec;
10401 vec = make_tree_vector ();
10403 vec_safe_push (vec, range);
10405 member_begin = perform_koenig_lookup (id_begin, vec,
10406 tf_warning_or_error);
10407 *begin = finish_call_expr (member_begin, &vec, false, true,
10408 tf_warning_or_error);
10409 member_end = perform_koenig_lookup (id_end, vec,
10410 tf_warning_or_error);
10411 *end = finish_call_expr (member_end, &vec, false, true,
10412 tf_warning_or_error);
10414 release_tree_vector (vec);
10417 /* Last common checks. */
10418 if (*begin == error_mark_node || *end == error_mark_node)
10420 /* If one of the expressions is an error do no more checks. */
10421 *begin = *end = error_mark_node;
10422 return error_mark_node;
10424 else if (type_dependent_expression_p (*begin)
10425 || type_dependent_expression_p (*end))
10426 /* Can happen, when, eg, in a template context, Koenig lookup
10427 can't resolve begin/end (c++/58503). */
10428 return NULL_TREE;
10429 else
10431 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10432 /* The unqualified type of the __begin and __end temporaries should
10433 be the same, as required by the multiple auto declaration. */
10434 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10435 error ("inconsistent begin/end types in range-based %<for%> "
10436 "statement: %qT and %qT",
10437 TREE_TYPE (*begin), TREE_TYPE (*end));
10438 return iter_type;
10443 /* Helper function for cp_parser_perform_range_for_lookup.
10444 Builds a tree for RANGE.IDENTIFIER(). */
10446 static tree
10447 cp_parser_range_for_member_function (tree range, tree identifier)
10449 tree member, res;
10450 vec<tree, va_gc> *vec;
10452 member = finish_class_member_access_expr (range, identifier,
10453 false, tf_warning_or_error);
10454 if (member == error_mark_node)
10455 return error_mark_node;
10457 vec = make_tree_vector ();
10458 res = finish_call_expr (member, &vec,
10459 /*disallow_virtual=*/false,
10460 /*koenig_p=*/false,
10461 tf_warning_or_error);
10462 release_tree_vector (vec);
10463 return res;
10466 /* Parse an iteration-statement.
10468 iteration-statement:
10469 while ( condition ) statement
10470 do statement while ( expression ) ;
10471 for ( for-init-statement condition [opt] ; expression [opt] )
10472 statement
10474 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10476 static tree
10477 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10479 cp_token *token;
10480 enum rid keyword;
10481 tree statement;
10482 unsigned char in_statement;
10484 /* Peek at the next token. */
10485 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10486 if (!token)
10487 return error_mark_node;
10489 /* Remember whether or not we are already within an iteration
10490 statement. */
10491 in_statement = parser->in_statement;
10493 /* See what kind of keyword it is. */
10494 keyword = token->keyword;
10495 switch (keyword)
10497 case RID_WHILE:
10499 tree condition;
10501 /* Begin the while-statement. */
10502 statement = begin_while_stmt ();
10503 /* Look for the `('. */
10504 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10505 /* Parse the condition. */
10506 condition = cp_parser_condition (parser);
10507 finish_while_stmt_cond (condition, statement, ivdep);
10508 /* Look for the `)'. */
10509 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10510 /* Parse the dependent statement. */
10511 parser->in_statement = IN_ITERATION_STMT;
10512 cp_parser_already_scoped_statement (parser);
10513 parser->in_statement = in_statement;
10514 /* We're done with the while-statement. */
10515 finish_while_stmt (statement);
10517 break;
10519 case RID_DO:
10521 tree expression;
10523 /* Begin the do-statement. */
10524 statement = begin_do_stmt ();
10525 /* Parse the body of the do-statement. */
10526 parser->in_statement = IN_ITERATION_STMT;
10527 cp_parser_implicitly_scoped_statement (parser, NULL);
10528 parser->in_statement = in_statement;
10529 finish_do_body (statement);
10530 /* Look for the `while' keyword. */
10531 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10532 /* Look for the `('. */
10533 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10534 /* Parse the expression. */
10535 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10536 /* We're done with the do-statement. */
10537 finish_do_stmt (expression, statement, ivdep);
10538 /* Look for the `)'. */
10539 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10540 /* Look for the `;'. */
10541 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10543 break;
10545 case RID_FOR:
10547 /* Look for the `('. */
10548 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10550 statement = cp_parser_for (parser, ivdep);
10552 /* Look for the `)'. */
10553 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10555 /* Parse the body of the for-statement. */
10556 parser->in_statement = IN_ITERATION_STMT;
10557 cp_parser_already_scoped_statement (parser);
10558 parser->in_statement = in_statement;
10560 /* We're done with the for-statement. */
10561 finish_for_stmt (statement);
10563 break;
10565 default:
10566 cp_parser_error (parser, "expected iteration-statement");
10567 statement = error_mark_node;
10568 break;
10571 return statement;
10574 /* Parse a for-init-statement or the declarator of a range-based-for.
10575 Returns true if a range-based-for declaration is seen.
10577 for-init-statement:
10578 expression-statement
10579 simple-declaration */
10581 static bool
10582 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10584 /* If the next token is a `;', then we have an empty
10585 expression-statement. Grammatically, this is also a
10586 simple-declaration, but an invalid one, because it does not
10587 declare anything. Therefore, if we did not handle this case
10588 specially, we would issue an error message about an invalid
10589 declaration. */
10590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10592 bool is_range_for = false;
10593 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10595 parser->colon_corrects_to_scope_p = false;
10597 /* We're going to speculatively look for a declaration, falling back
10598 to an expression, if necessary. */
10599 cp_parser_parse_tentatively (parser);
10600 /* Parse the declaration. */
10601 cp_parser_simple_declaration (parser,
10602 /*function_definition_allowed_p=*/false,
10603 decl);
10604 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10605 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10607 /* It is a range-for, consume the ':' */
10608 cp_lexer_consume_token (parser->lexer);
10609 is_range_for = true;
10610 if (cxx_dialect < cxx11)
10612 error_at (cp_lexer_peek_token (parser->lexer)->location,
10613 "range-based %<for%> loops are not allowed "
10614 "in C++98 mode");
10615 *decl = error_mark_node;
10618 else
10619 /* The ';' is not consumed yet because we told
10620 cp_parser_simple_declaration not to. */
10621 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10623 if (cp_parser_parse_definitely (parser))
10624 return is_range_for;
10625 /* If the tentative parse failed, then we shall need to look for an
10626 expression-statement. */
10628 /* If we are here, it is an expression-statement. */
10629 cp_parser_expression_statement (parser, NULL_TREE);
10630 return false;
10633 /* Parse a jump-statement.
10635 jump-statement:
10636 break ;
10637 continue ;
10638 return expression [opt] ;
10639 return braced-init-list ;
10640 goto identifier ;
10642 GNU extension:
10644 jump-statement:
10645 goto * expression ;
10647 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10649 static tree
10650 cp_parser_jump_statement (cp_parser* parser)
10652 tree statement = error_mark_node;
10653 cp_token *token;
10654 enum rid keyword;
10655 unsigned char in_statement;
10657 /* Peek at the next token. */
10658 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10659 if (!token)
10660 return error_mark_node;
10662 /* See what kind of keyword it is. */
10663 keyword = token->keyword;
10664 switch (keyword)
10666 case RID_BREAK:
10667 in_statement = parser->in_statement & ~IN_IF_STMT;
10668 switch (in_statement)
10670 case 0:
10671 error_at (token->location, "break statement not within loop or switch");
10672 break;
10673 default:
10674 gcc_assert ((in_statement & IN_SWITCH_STMT)
10675 || in_statement == IN_ITERATION_STMT);
10676 statement = finish_break_stmt ();
10677 if (in_statement == IN_ITERATION_STMT)
10678 break_maybe_infinite_loop ();
10679 break;
10680 case IN_OMP_BLOCK:
10681 error_at (token->location, "invalid exit from OpenMP structured block");
10682 break;
10683 case IN_OMP_FOR:
10684 error_at (token->location, "break statement used with OpenMP for loop");
10685 break;
10686 case IN_CILK_SIMD_FOR:
10687 error_at (token->location, "break statement used with Cilk Plus for loop");
10688 break;
10690 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10691 break;
10693 case RID_CONTINUE:
10694 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10696 case 0:
10697 error_at (token->location, "continue statement not within a loop");
10698 break;
10699 case IN_CILK_SIMD_FOR:
10700 error_at (token->location,
10701 "continue statement within %<#pragma simd%> loop body");
10702 /* Fall through. */
10703 case IN_ITERATION_STMT:
10704 case IN_OMP_FOR:
10705 statement = finish_continue_stmt ();
10706 break;
10707 case IN_OMP_BLOCK:
10708 error_at (token->location, "invalid exit from OpenMP structured block");
10709 break;
10710 default:
10711 gcc_unreachable ();
10713 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10714 break;
10716 case RID_RETURN:
10718 tree expr;
10719 bool expr_non_constant_p;
10721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10723 cp_lexer_set_source_position (parser->lexer);
10724 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10725 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10727 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10728 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10729 else
10730 /* If the next token is a `;', then there is no
10731 expression. */
10732 expr = NULL_TREE;
10733 /* Build the return-statement. */
10734 statement = finish_return_stmt (expr);
10735 /* Look for the final `;'. */
10736 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10738 break;
10740 case RID_GOTO:
10741 /* Create the goto-statement. */
10742 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10744 /* Issue a warning about this use of a GNU extension. */
10745 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10746 /* Consume the '*' token. */
10747 cp_lexer_consume_token (parser->lexer);
10748 /* Parse the dependent expression. */
10749 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10751 else
10752 finish_goto_stmt (cp_parser_identifier (parser));
10753 /* Look for the final `;'. */
10754 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10755 break;
10757 default:
10758 cp_parser_error (parser, "expected jump-statement");
10759 break;
10762 return statement;
10765 /* Parse a declaration-statement.
10767 declaration-statement:
10768 block-declaration */
10770 static void
10771 cp_parser_declaration_statement (cp_parser* parser)
10773 void *p;
10775 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10776 p = obstack_alloc (&declarator_obstack, 0);
10778 /* Parse the block-declaration. */
10779 cp_parser_block_declaration (parser, /*statement_p=*/true);
10781 /* Free any declarators allocated. */
10782 obstack_free (&declarator_obstack, p);
10785 /* Some dependent statements (like `if (cond) statement'), are
10786 implicitly in their own scope. In other words, if the statement is
10787 a single statement (as opposed to a compound-statement), it is
10788 none-the-less treated as if it were enclosed in braces. Any
10789 declarations appearing in the dependent statement are out of scope
10790 after control passes that point. This function parses a statement,
10791 but ensures that is in its own scope, even if it is not a
10792 compound-statement.
10794 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10795 is a (possibly labeled) if statement which is not enclosed in
10796 braces and has an else clause. This is used to implement
10797 -Wparentheses.
10799 Returns the new statement. */
10801 static tree
10802 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10804 tree statement;
10806 if (if_p != NULL)
10807 *if_p = false;
10809 /* Mark if () ; with a special NOP_EXPR. */
10810 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10812 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10813 cp_lexer_consume_token (parser->lexer);
10814 statement = add_stmt (build_empty_stmt (loc));
10816 /* if a compound is opened, we simply parse the statement directly. */
10817 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10818 statement = cp_parser_compound_statement (parser, NULL, false, false);
10819 /* If the token is not a `{', then we must take special action. */
10820 else
10822 /* Create a compound-statement. */
10823 statement = begin_compound_stmt (0);
10824 /* Parse the dependent-statement. */
10825 cp_parser_statement (parser, NULL_TREE, false, if_p);
10826 /* Finish the dummy compound-statement. */
10827 finish_compound_stmt (statement);
10830 /* Return the statement. */
10831 return statement;
10834 /* For some dependent statements (like `while (cond) statement'), we
10835 have already created a scope. Therefore, even if the dependent
10836 statement is a compound-statement, we do not want to create another
10837 scope. */
10839 static void
10840 cp_parser_already_scoped_statement (cp_parser* parser)
10842 /* If the token is a `{', then we must take special action. */
10843 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10844 cp_parser_statement (parser, NULL_TREE, false, NULL);
10845 else
10847 /* Avoid calling cp_parser_compound_statement, so that we
10848 don't create a new scope. Do everything else by hand. */
10849 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10850 /* If the next keyword is `__label__' we have a label declaration. */
10851 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10852 cp_parser_label_declaration (parser);
10853 /* Parse an (optional) statement-seq. */
10854 cp_parser_statement_seq_opt (parser, NULL_TREE);
10855 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10859 /* Declarations [gram.dcl.dcl] */
10861 /* Parse an optional declaration-sequence.
10863 declaration-seq:
10864 declaration
10865 declaration-seq declaration */
10867 static void
10868 cp_parser_declaration_seq_opt (cp_parser* parser)
10870 while (true)
10872 cp_token *token;
10874 token = cp_lexer_peek_token (parser->lexer);
10876 if (token->type == CPP_CLOSE_BRACE
10877 || token->type == CPP_EOF
10878 || token->type == CPP_PRAGMA_EOL)
10879 break;
10881 if (token->type == CPP_SEMICOLON)
10883 /* A declaration consisting of a single semicolon is
10884 invalid. Allow it unless we're being pedantic. */
10885 cp_lexer_consume_token (parser->lexer);
10886 if (!in_system_header_at (input_location))
10887 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10888 continue;
10891 /* If we're entering or exiting a region that's implicitly
10892 extern "C", modify the lang context appropriately. */
10893 if (!parser->implicit_extern_c && token->implicit_extern_c)
10895 push_lang_context (lang_name_c);
10896 parser->implicit_extern_c = true;
10898 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10900 pop_lang_context ();
10901 parser->implicit_extern_c = false;
10904 if (token->type == CPP_PRAGMA)
10906 /* A top-level declaration can consist solely of a #pragma.
10907 A nested declaration cannot, so this is done here and not
10908 in cp_parser_declaration. (A #pragma at block scope is
10909 handled in cp_parser_statement.) */
10910 cp_parser_pragma (parser, pragma_external);
10911 continue;
10914 /* Parse the declaration itself. */
10915 cp_parser_declaration (parser);
10919 /* Parse a declaration.
10921 declaration:
10922 block-declaration
10923 function-definition
10924 template-declaration
10925 explicit-instantiation
10926 explicit-specialization
10927 linkage-specification
10928 namespace-definition
10930 GNU extension:
10932 declaration:
10933 __extension__ declaration */
10935 static void
10936 cp_parser_declaration (cp_parser* parser)
10938 cp_token token1;
10939 cp_token token2;
10940 int saved_pedantic;
10941 void *p;
10942 tree attributes = NULL_TREE;
10944 /* Check for the `__extension__' keyword. */
10945 if (cp_parser_extension_opt (parser, &saved_pedantic))
10947 /* Parse the qualified declaration. */
10948 cp_parser_declaration (parser);
10949 /* Restore the PEDANTIC flag. */
10950 pedantic = saved_pedantic;
10952 return;
10955 /* Try to figure out what kind of declaration is present. */
10956 token1 = *cp_lexer_peek_token (parser->lexer);
10958 if (token1.type != CPP_EOF)
10959 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10960 else
10962 token2.type = CPP_EOF;
10963 token2.keyword = RID_MAX;
10966 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10967 p = obstack_alloc (&declarator_obstack, 0);
10969 /* If the next token is `extern' and the following token is a string
10970 literal, then we have a linkage specification. */
10971 if (token1.keyword == RID_EXTERN
10972 && cp_parser_is_pure_string_literal (&token2))
10973 cp_parser_linkage_specification (parser);
10974 /* If the next token is `template', then we have either a template
10975 declaration, an explicit instantiation, or an explicit
10976 specialization. */
10977 else if (token1.keyword == RID_TEMPLATE)
10979 /* `template <>' indicates a template specialization. */
10980 if (token2.type == CPP_LESS
10981 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10982 cp_parser_explicit_specialization (parser);
10983 /* `template <' indicates a template declaration. */
10984 else if (token2.type == CPP_LESS)
10985 cp_parser_template_declaration (parser, /*member_p=*/false);
10986 /* Anything else must be an explicit instantiation. */
10987 else
10988 cp_parser_explicit_instantiation (parser);
10990 /* If the next token is `export', then we have a template
10991 declaration. */
10992 else if (token1.keyword == RID_EXPORT)
10993 cp_parser_template_declaration (parser, /*member_p=*/false);
10994 /* If the next token is `extern', 'static' or 'inline' and the one
10995 after that is `template', we have a GNU extended explicit
10996 instantiation directive. */
10997 else if (cp_parser_allow_gnu_extensions_p (parser)
10998 && (token1.keyword == RID_EXTERN
10999 || token1.keyword == RID_STATIC
11000 || token1.keyword == RID_INLINE)
11001 && token2.keyword == RID_TEMPLATE)
11002 cp_parser_explicit_instantiation (parser);
11003 /* If the next token is `namespace', check for a named or unnamed
11004 namespace definition. */
11005 else if (token1.keyword == RID_NAMESPACE
11006 && (/* A named namespace definition. */
11007 (token2.type == CPP_NAME
11008 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11009 != CPP_EQ))
11010 /* An unnamed namespace definition. */
11011 || token2.type == CPP_OPEN_BRACE
11012 || token2.keyword == RID_ATTRIBUTE))
11013 cp_parser_namespace_definition (parser);
11014 /* An inline (associated) namespace definition. */
11015 else if (token1.keyword == RID_INLINE
11016 && token2.keyword == RID_NAMESPACE)
11017 cp_parser_namespace_definition (parser);
11018 /* Objective-C++ declaration/definition. */
11019 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11020 cp_parser_objc_declaration (parser, NULL_TREE);
11021 else if (c_dialect_objc ()
11022 && token1.keyword == RID_ATTRIBUTE
11023 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11024 cp_parser_objc_declaration (parser, attributes);
11025 /* We must have either a block declaration or a function
11026 definition. */
11027 else
11028 /* Try to parse a block-declaration, or a function-definition. */
11029 cp_parser_block_declaration (parser, /*statement_p=*/false);
11031 /* Free any declarators allocated. */
11032 obstack_free (&declarator_obstack, p);
11035 /* Parse a block-declaration.
11037 block-declaration:
11038 simple-declaration
11039 asm-definition
11040 namespace-alias-definition
11041 using-declaration
11042 using-directive
11044 GNU Extension:
11046 block-declaration:
11047 __extension__ block-declaration
11049 C++0x Extension:
11051 block-declaration:
11052 static_assert-declaration
11054 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11055 part of a declaration-statement. */
11057 static void
11058 cp_parser_block_declaration (cp_parser *parser,
11059 bool statement_p)
11061 cp_token *token1;
11062 int saved_pedantic;
11064 /* Check for the `__extension__' keyword. */
11065 if (cp_parser_extension_opt (parser, &saved_pedantic))
11067 /* Parse the qualified declaration. */
11068 cp_parser_block_declaration (parser, statement_p);
11069 /* Restore the PEDANTIC flag. */
11070 pedantic = saved_pedantic;
11072 return;
11075 /* Peek at the next token to figure out which kind of declaration is
11076 present. */
11077 token1 = cp_lexer_peek_token (parser->lexer);
11079 /* If the next keyword is `asm', we have an asm-definition. */
11080 if (token1->keyword == RID_ASM)
11082 if (statement_p)
11083 cp_parser_commit_to_tentative_parse (parser);
11084 cp_parser_asm_definition (parser);
11086 /* If the next keyword is `namespace', we have a
11087 namespace-alias-definition. */
11088 else if (token1->keyword == RID_NAMESPACE)
11089 cp_parser_namespace_alias_definition (parser);
11090 /* If the next keyword is `using', we have a
11091 using-declaration, a using-directive, or an alias-declaration. */
11092 else if (token1->keyword == RID_USING)
11094 cp_token *token2;
11096 if (statement_p)
11097 cp_parser_commit_to_tentative_parse (parser);
11098 /* If the token after `using' is `namespace', then we have a
11099 using-directive. */
11100 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11101 if (token2->keyword == RID_NAMESPACE)
11102 cp_parser_using_directive (parser);
11103 /* If the second token after 'using' is '=', then we have an
11104 alias-declaration. */
11105 else if (cxx_dialect >= cxx11
11106 && token2->type == CPP_NAME
11107 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11108 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11109 cp_parser_alias_declaration (parser);
11110 /* Otherwise, it's a using-declaration. */
11111 else
11112 cp_parser_using_declaration (parser,
11113 /*access_declaration_p=*/false);
11115 /* If the next keyword is `__label__' we have a misplaced label
11116 declaration. */
11117 else if (token1->keyword == RID_LABEL)
11119 cp_lexer_consume_token (parser->lexer);
11120 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11121 cp_parser_skip_to_end_of_statement (parser);
11122 /* If the next token is now a `;', consume it. */
11123 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11124 cp_lexer_consume_token (parser->lexer);
11126 /* If the next token is `static_assert' we have a static assertion. */
11127 else if (token1->keyword == RID_STATIC_ASSERT)
11128 cp_parser_static_assert (parser, /*member_p=*/false);
11129 /* Anything else must be a simple-declaration. */
11130 else
11131 cp_parser_simple_declaration (parser, !statement_p,
11132 /*maybe_range_for_decl*/NULL);
11135 /* Parse a simple-declaration.
11137 simple-declaration:
11138 decl-specifier-seq [opt] init-declarator-list [opt] ;
11140 init-declarator-list:
11141 init-declarator
11142 init-declarator-list , init-declarator
11144 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11145 function-definition as a simple-declaration.
11147 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11148 parsed declaration if it is an uninitialized single declarator not followed
11149 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11150 if present, will not be consumed. */
11152 static void
11153 cp_parser_simple_declaration (cp_parser* parser,
11154 bool function_definition_allowed_p,
11155 tree *maybe_range_for_decl)
11157 cp_decl_specifier_seq decl_specifiers;
11158 int declares_class_or_enum;
11159 bool saw_declarator;
11161 if (maybe_range_for_decl)
11162 *maybe_range_for_decl = NULL_TREE;
11164 /* Defer access checks until we know what is being declared; the
11165 checks for names appearing in the decl-specifier-seq should be
11166 done as if we were in the scope of the thing being declared. */
11167 push_deferring_access_checks (dk_deferred);
11169 /* Parse the decl-specifier-seq. We have to keep track of whether
11170 or not the decl-specifier-seq declares a named class or
11171 enumeration type, since that is the only case in which the
11172 init-declarator-list is allowed to be empty.
11174 [dcl.dcl]
11176 In a simple-declaration, the optional init-declarator-list can be
11177 omitted only when declaring a class or enumeration, that is when
11178 the decl-specifier-seq contains either a class-specifier, an
11179 elaborated-type-specifier, or an enum-specifier. */
11180 cp_parser_decl_specifier_seq (parser,
11181 CP_PARSER_FLAGS_OPTIONAL,
11182 &decl_specifiers,
11183 &declares_class_or_enum);
11184 /* We no longer need to defer access checks. */
11185 stop_deferring_access_checks ();
11187 /* In a block scope, a valid declaration must always have a
11188 decl-specifier-seq. By not trying to parse declarators, we can
11189 resolve the declaration/expression ambiguity more quickly. */
11190 if (!function_definition_allowed_p
11191 && !decl_specifiers.any_specifiers_p)
11193 cp_parser_error (parser, "expected declaration");
11194 goto done;
11197 /* If the next two tokens are both identifiers, the code is
11198 erroneous. The usual cause of this situation is code like:
11200 T t;
11202 where "T" should name a type -- but does not. */
11203 if (!decl_specifiers.any_type_specifiers_p
11204 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11206 /* If parsing tentatively, we should commit; we really are
11207 looking at a declaration. */
11208 cp_parser_commit_to_tentative_parse (parser);
11209 /* Give up. */
11210 goto done;
11213 /* If we have seen at least one decl-specifier, and the next token
11214 is not a parenthesis, then we must be looking at a declaration.
11215 (After "int (" we might be looking at a functional cast.) */
11216 if (decl_specifiers.any_specifiers_p
11217 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11218 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11219 && !cp_parser_error_occurred (parser))
11220 cp_parser_commit_to_tentative_parse (parser);
11222 /* Keep going until we hit the `;' at the end of the simple
11223 declaration. */
11224 saw_declarator = false;
11225 while (cp_lexer_next_token_is_not (parser->lexer,
11226 CPP_SEMICOLON))
11228 cp_token *token;
11229 bool function_definition_p;
11230 tree decl;
11232 if (saw_declarator)
11234 /* If we are processing next declarator, coma is expected */
11235 token = cp_lexer_peek_token (parser->lexer);
11236 gcc_assert (token->type == CPP_COMMA);
11237 cp_lexer_consume_token (parser->lexer);
11238 if (maybe_range_for_decl)
11239 *maybe_range_for_decl = error_mark_node;
11241 else
11242 saw_declarator = true;
11244 /* Parse the init-declarator. */
11245 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11246 /*checks=*/NULL,
11247 function_definition_allowed_p,
11248 /*member_p=*/false,
11249 declares_class_or_enum,
11250 &function_definition_p,
11251 maybe_range_for_decl);
11252 /* If an error occurred while parsing tentatively, exit quickly.
11253 (That usually happens when in the body of a function; each
11254 statement is treated as a declaration-statement until proven
11255 otherwise.) */
11256 if (cp_parser_error_occurred (parser))
11257 goto done;
11258 /* Handle function definitions specially. */
11259 if (function_definition_p)
11261 /* If the next token is a `,', then we are probably
11262 processing something like:
11264 void f() {}, *p;
11266 which is erroneous. */
11267 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11269 cp_token *token = cp_lexer_peek_token (parser->lexer);
11270 error_at (token->location,
11271 "mixing"
11272 " declarations and function-definitions is forbidden");
11274 /* Otherwise, we're done with the list of declarators. */
11275 else
11277 pop_deferring_access_checks ();
11278 return;
11281 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11282 *maybe_range_for_decl = decl;
11283 /* The next token should be either a `,' or a `;'. */
11284 token = cp_lexer_peek_token (parser->lexer);
11285 /* If it's a `,', there are more declarators to come. */
11286 if (token->type == CPP_COMMA)
11287 /* will be consumed next time around */;
11288 /* If it's a `;', we are done. */
11289 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11290 break;
11291 /* Anything else is an error. */
11292 else
11294 /* If we have already issued an error message we don't need
11295 to issue another one. */
11296 if (decl != error_mark_node
11297 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11298 cp_parser_error (parser, "expected %<,%> or %<;%>");
11299 /* Skip tokens until we reach the end of the statement. */
11300 cp_parser_skip_to_end_of_statement (parser);
11301 /* If the next token is now a `;', consume it. */
11302 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11303 cp_lexer_consume_token (parser->lexer);
11304 goto done;
11306 /* After the first time around, a function-definition is not
11307 allowed -- even if it was OK at first. For example:
11309 int i, f() {}
11311 is not valid. */
11312 function_definition_allowed_p = false;
11315 /* Issue an error message if no declarators are present, and the
11316 decl-specifier-seq does not itself declare a class or
11317 enumeration: [dcl.dcl]/3. */
11318 if (!saw_declarator)
11320 if (cp_parser_declares_only_class_p (parser))
11322 if (!declares_class_or_enum
11323 && decl_specifiers.type
11324 && OVERLOAD_TYPE_P (decl_specifiers.type))
11325 /* Ensure an error is issued anyway when finish_decltype_type,
11326 called via cp_parser_decl_specifier_seq, returns a class or
11327 an enumeration (c++/51786). */
11328 decl_specifiers.type = NULL_TREE;
11329 shadow_tag (&decl_specifiers);
11331 /* Perform any deferred access checks. */
11332 perform_deferred_access_checks (tf_warning_or_error);
11335 /* Consume the `;'. */
11336 if (!maybe_range_for_decl)
11337 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11339 done:
11340 pop_deferring_access_checks ();
11343 /* Parse a decl-specifier-seq.
11345 decl-specifier-seq:
11346 decl-specifier-seq [opt] decl-specifier
11347 decl-specifier attribute-specifier-seq [opt] (C++11)
11349 decl-specifier:
11350 storage-class-specifier
11351 type-specifier
11352 function-specifier
11353 friend
11354 typedef
11356 GNU Extension:
11358 decl-specifier:
11359 attributes
11361 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11363 The parser flags FLAGS is used to control type-specifier parsing.
11365 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11366 flags:
11368 1: one of the decl-specifiers is an elaborated-type-specifier
11369 (i.e., a type declaration)
11370 2: one of the decl-specifiers is an enum-specifier or a
11371 class-specifier (i.e., a type definition)
11375 static void
11376 cp_parser_decl_specifier_seq (cp_parser* parser,
11377 cp_parser_flags flags,
11378 cp_decl_specifier_seq *decl_specs,
11379 int* declares_class_or_enum)
11381 bool constructor_possible_p = !parser->in_declarator_p;
11382 bool found_decl_spec = false;
11383 cp_token *start_token = NULL;
11384 cp_decl_spec ds;
11386 /* Clear DECL_SPECS. */
11387 clear_decl_specs (decl_specs);
11389 /* Assume no class or enumeration type is declared. */
11390 *declares_class_or_enum = 0;
11392 /* Keep reading specifiers until there are no more to read. */
11393 while (true)
11395 bool constructor_p;
11396 cp_token *token;
11397 ds = ds_last;
11399 /* Peek at the next token. */
11400 token = cp_lexer_peek_token (parser->lexer);
11402 /* Save the first token of the decl spec list for error
11403 reporting. */
11404 if (!start_token)
11405 start_token = token;
11406 /* Handle attributes. */
11407 if (cp_next_tokens_can_be_attribute_p (parser))
11409 /* Parse the attributes. */
11410 tree attrs = cp_parser_attributes_opt (parser);
11412 /* In a sequence of declaration specifiers, c++11 attributes
11413 appertain to the type that precede them. In that case
11414 [dcl.spec]/1 says:
11416 The attribute-specifier-seq affects the type only for
11417 the declaration it appears in, not other declarations
11418 involving the same type.
11420 But for now let's force the user to position the
11421 attribute either at the beginning of the declaration or
11422 after the declarator-id, which would clearly mean that it
11423 applies to the declarator. */
11424 if (cxx11_attribute_p (attrs))
11426 if (!found_decl_spec)
11427 /* The c++11 attribute is at the beginning of the
11428 declaration. It appertains to the entity being
11429 declared. */;
11430 else
11432 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11434 /* This is an attribute following a
11435 class-specifier. */
11436 if (decl_specs->type_definition_p)
11437 warn_misplaced_attr_for_class_type (token->location,
11438 decl_specs->type);
11439 attrs = NULL_TREE;
11441 else
11443 decl_specs->std_attributes
11444 = chainon (decl_specs->std_attributes,
11445 attrs);
11446 if (decl_specs->locations[ds_std_attribute] == 0)
11447 decl_specs->locations[ds_std_attribute] = token->location;
11449 continue;
11453 decl_specs->attributes
11454 = chainon (decl_specs->attributes,
11455 attrs);
11456 if (decl_specs->locations[ds_attribute] == 0)
11457 decl_specs->locations[ds_attribute] = token->location;
11458 continue;
11460 /* Assume we will find a decl-specifier keyword. */
11461 found_decl_spec = true;
11462 /* If the next token is an appropriate keyword, we can simply
11463 add it to the list. */
11464 switch (token->keyword)
11466 /* decl-specifier:
11467 friend
11468 constexpr */
11469 case RID_FRIEND:
11470 if (!at_class_scope_p ())
11472 error_at (token->location, "%<friend%> used outside of class");
11473 cp_lexer_purge_token (parser->lexer);
11475 else
11477 ds = ds_friend;
11478 /* Consume the token. */
11479 cp_lexer_consume_token (parser->lexer);
11481 break;
11483 case RID_CONSTEXPR:
11484 ds = ds_constexpr;
11485 cp_lexer_consume_token (parser->lexer);
11486 break;
11488 /* function-specifier:
11489 inline
11490 virtual
11491 explicit */
11492 case RID_INLINE:
11493 case RID_VIRTUAL:
11494 case RID_EXPLICIT:
11495 cp_parser_function_specifier_opt (parser, decl_specs);
11496 break;
11498 /* decl-specifier:
11499 typedef */
11500 case RID_TYPEDEF:
11501 ds = ds_typedef;
11502 /* Consume the token. */
11503 cp_lexer_consume_token (parser->lexer);
11504 /* A constructor declarator cannot appear in a typedef. */
11505 constructor_possible_p = false;
11506 /* The "typedef" keyword can only occur in a declaration; we
11507 may as well commit at this point. */
11508 cp_parser_commit_to_tentative_parse (parser);
11510 if (decl_specs->storage_class != sc_none)
11511 decl_specs->conflicting_specifiers_p = true;
11512 break;
11514 /* storage-class-specifier:
11515 auto
11516 register
11517 static
11518 extern
11519 mutable
11521 GNU Extension:
11522 thread */
11523 case RID_AUTO:
11524 if (cxx_dialect == cxx98)
11526 /* Consume the token. */
11527 cp_lexer_consume_token (parser->lexer);
11529 /* Complain about `auto' as a storage specifier, if
11530 we're complaining about C++0x compatibility. */
11531 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11532 " changes meaning in C++11; please remove it");
11534 /* Set the storage class anyway. */
11535 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11536 token);
11538 else
11539 /* C++0x auto type-specifier. */
11540 found_decl_spec = false;
11541 break;
11543 case RID_REGISTER:
11544 case RID_STATIC:
11545 case RID_EXTERN:
11546 case RID_MUTABLE:
11547 /* Consume the token. */
11548 cp_lexer_consume_token (parser->lexer);
11549 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11550 token);
11551 break;
11552 case RID_THREAD:
11553 /* Consume the token. */
11554 ds = ds_thread;
11555 cp_lexer_consume_token (parser->lexer);
11556 break;
11558 default:
11559 /* We did not yet find a decl-specifier yet. */
11560 found_decl_spec = false;
11561 break;
11564 if (found_decl_spec
11565 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11566 && token->keyword != RID_CONSTEXPR)
11567 error ("decl-specifier invalid in condition");
11569 if (ds != ds_last)
11570 set_and_check_decl_spec_loc (decl_specs, ds, token);
11572 /* Constructors are a special case. The `S' in `S()' is not a
11573 decl-specifier; it is the beginning of the declarator. */
11574 constructor_p
11575 = (!found_decl_spec
11576 && constructor_possible_p
11577 && (cp_parser_constructor_declarator_p
11578 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11580 /* If we don't have a DECL_SPEC yet, then we must be looking at
11581 a type-specifier. */
11582 if (!found_decl_spec && !constructor_p)
11584 int decl_spec_declares_class_or_enum;
11585 bool is_cv_qualifier;
11586 tree type_spec;
11588 type_spec
11589 = cp_parser_type_specifier (parser, flags,
11590 decl_specs,
11591 /*is_declaration=*/true,
11592 &decl_spec_declares_class_or_enum,
11593 &is_cv_qualifier);
11594 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11596 /* If this type-specifier referenced a user-defined type
11597 (a typedef, class-name, etc.), then we can't allow any
11598 more such type-specifiers henceforth.
11600 [dcl.spec]
11602 The longest sequence of decl-specifiers that could
11603 possibly be a type name is taken as the
11604 decl-specifier-seq of a declaration. The sequence shall
11605 be self-consistent as described below.
11607 [dcl.type]
11609 As a general rule, at most one type-specifier is allowed
11610 in the complete decl-specifier-seq of a declaration. The
11611 only exceptions are the following:
11613 -- const or volatile can be combined with any other
11614 type-specifier.
11616 -- signed or unsigned can be combined with char, long,
11617 short, or int.
11619 -- ..
11621 Example:
11623 typedef char* Pc;
11624 void g (const int Pc);
11626 Here, Pc is *not* part of the decl-specifier seq; it's
11627 the declarator. Therefore, once we see a type-specifier
11628 (other than a cv-qualifier), we forbid any additional
11629 user-defined types. We *do* still allow things like `int
11630 int' to be considered a decl-specifier-seq, and issue the
11631 error message later. */
11632 if (type_spec && !is_cv_qualifier)
11633 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11634 /* A constructor declarator cannot follow a type-specifier. */
11635 if (type_spec)
11637 constructor_possible_p = false;
11638 found_decl_spec = true;
11639 if (!is_cv_qualifier)
11640 decl_specs->any_type_specifiers_p = true;
11644 /* If we still do not have a DECL_SPEC, then there are no more
11645 decl-specifiers. */
11646 if (!found_decl_spec)
11647 break;
11649 decl_specs->any_specifiers_p = true;
11650 /* After we see one decl-specifier, further decl-specifiers are
11651 always optional. */
11652 flags |= CP_PARSER_FLAGS_OPTIONAL;
11655 /* Don't allow a friend specifier with a class definition. */
11656 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11657 && (*declares_class_or_enum & 2))
11658 error_at (decl_specs->locations[ds_friend],
11659 "class definition may not be declared a friend");
11662 /* Parse an (optional) storage-class-specifier.
11664 storage-class-specifier:
11665 auto
11666 register
11667 static
11668 extern
11669 mutable
11671 GNU Extension:
11673 storage-class-specifier:
11674 thread
11676 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11678 static tree
11679 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11681 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11683 case RID_AUTO:
11684 if (cxx_dialect != cxx98)
11685 return NULL_TREE;
11686 /* Fall through for C++98. */
11688 case RID_REGISTER:
11689 case RID_STATIC:
11690 case RID_EXTERN:
11691 case RID_MUTABLE:
11692 case RID_THREAD:
11693 /* Consume the token. */
11694 return cp_lexer_consume_token (parser->lexer)->u.value;
11696 default:
11697 return NULL_TREE;
11701 /* Parse an (optional) function-specifier.
11703 function-specifier:
11704 inline
11705 virtual
11706 explicit
11708 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11709 Updates DECL_SPECS, if it is non-NULL. */
11711 static tree
11712 cp_parser_function_specifier_opt (cp_parser* parser,
11713 cp_decl_specifier_seq *decl_specs)
11715 cp_token *token = cp_lexer_peek_token (parser->lexer);
11716 switch (token->keyword)
11718 case RID_INLINE:
11719 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11720 break;
11722 case RID_VIRTUAL:
11723 /* 14.5.2.3 [temp.mem]
11725 A member function template shall not be virtual. */
11726 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11727 error_at (token->location, "templates may not be %<virtual%>");
11728 else
11729 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11730 break;
11732 case RID_EXPLICIT:
11733 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11734 break;
11736 default:
11737 return NULL_TREE;
11740 /* Consume the token. */
11741 return cp_lexer_consume_token (parser->lexer)->u.value;
11744 /* Parse a linkage-specification.
11746 linkage-specification:
11747 extern string-literal { declaration-seq [opt] }
11748 extern string-literal declaration */
11750 static void
11751 cp_parser_linkage_specification (cp_parser* parser)
11753 tree linkage;
11755 /* Look for the `extern' keyword. */
11756 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11758 /* Look for the string-literal. */
11759 linkage = cp_parser_string_literal (parser, false, false);
11761 /* Transform the literal into an identifier. If the literal is a
11762 wide-character string, or contains embedded NULs, then we can't
11763 handle it as the user wants. */
11764 if (strlen (TREE_STRING_POINTER (linkage))
11765 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11767 cp_parser_error (parser, "invalid linkage-specification");
11768 /* Assume C++ linkage. */
11769 linkage = lang_name_cplusplus;
11771 else
11772 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11774 /* We're now using the new linkage. */
11775 push_lang_context (linkage);
11777 /* If the next token is a `{', then we're using the first
11778 production. */
11779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11781 cp_ensure_no_omp_declare_simd (parser);
11783 /* Consume the `{' token. */
11784 cp_lexer_consume_token (parser->lexer);
11785 /* Parse the declarations. */
11786 cp_parser_declaration_seq_opt (parser);
11787 /* Look for the closing `}'. */
11788 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11790 /* Otherwise, there's just one declaration. */
11791 else
11793 bool saved_in_unbraced_linkage_specification_p;
11795 saved_in_unbraced_linkage_specification_p
11796 = parser->in_unbraced_linkage_specification_p;
11797 parser->in_unbraced_linkage_specification_p = true;
11798 cp_parser_declaration (parser);
11799 parser->in_unbraced_linkage_specification_p
11800 = saved_in_unbraced_linkage_specification_p;
11803 /* We're done with the linkage-specification. */
11804 pop_lang_context ();
11807 /* Parse a static_assert-declaration.
11809 static_assert-declaration:
11810 static_assert ( constant-expression , string-literal ) ;
11812 If MEMBER_P, this static_assert is a class member. */
11814 static void
11815 cp_parser_static_assert(cp_parser *parser, bool member_p)
11817 tree condition;
11818 tree message;
11819 cp_token *token;
11820 location_t saved_loc;
11821 bool dummy;
11823 /* Peek at the `static_assert' token so we can keep track of exactly
11824 where the static assertion started. */
11825 token = cp_lexer_peek_token (parser->lexer);
11826 saved_loc = token->location;
11828 /* Look for the `static_assert' keyword. */
11829 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11830 RT_STATIC_ASSERT))
11831 return;
11833 /* We know we are in a static assertion; commit to any tentative
11834 parse. */
11835 if (cp_parser_parsing_tentatively (parser))
11836 cp_parser_commit_to_tentative_parse (parser);
11838 /* Parse the `(' starting the static assertion condition. */
11839 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11841 /* Parse the constant-expression. Allow a non-constant expression
11842 here in order to give better diagnostics in finish_static_assert. */
11843 condition =
11844 cp_parser_constant_expression (parser,
11845 /*allow_non_constant_p=*/true,
11846 /*non_constant_p=*/&dummy);
11848 /* Parse the separating `,'. */
11849 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11851 /* Parse the string-literal message. */
11852 message = cp_parser_string_literal (parser,
11853 /*translate=*/false,
11854 /*wide_ok=*/true);
11856 /* A `)' completes the static assertion. */
11857 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11858 cp_parser_skip_to_closing_parenthesis (parser,
11859 /*recovering=*/true,
11860 /*or_comma=*/false,
11861 /*consume_paren=*/true);
11863 /* A semicolon terminates the declaration. */
11864 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11866 /* Complete the static assertion, which may mean either processing
11867 the static assert now or saving it for template instantiation. */
11868 finish_static_assert (condition, message, saved_loc, member_p);
11871 /* Parse the expression in decltype ( expression ). */
11873 static tree
11874 cp_parser_decltype_expr (cp_parser *parser,
11875 bool &id_expression_or_member_access_p)
11877 cp_token *id_expr_start_token;
11878 tree expr;
11880 /* First, try parsing an id-expression. */
11881 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11882 cp_parser_parse_tentatively (parser);
11883 expr = cp_parser_id_expression (parser,
11884 /*template_keyword_p=*/false,
11885 /*check_dependency_p=*/true,
11886 /*template_p=*/NULL,
11887 /*declarator_p=*/false,
11888 /*optional_p=*/false);
11890 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11892 bool non_integral_constant_expression_p = false;
11893 tree id_expression = expr;
11894 cp_id_kind idk;
11895 const char *error_msg;
11897 if (identifier_p (expr))
11898 /* Lookup the name we got back from the id-expression. */
11899 expr = cp_parser_lookup_name_simple (parser, expr,
11900 id_expr_start_token->location);
11902 if (expr
11903 && expr != error_mark_node
11904 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11905 && TREE_CODE (expr) != TYPE_DECL
11906 && (TREE_CODE (expr) != BIT_NOT_EXPR
11907 || !TYPE_P (TREE_OPERAND (expr, 0)))
11908 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11910 /* Complete lookup of the id-expression. */
11911 expr = (finish_id_expression
11912 (id_expression, expr, parser->scope, &idk,
11913 /*integral_constant_expression_p=*/false,
11914 /*allow_non_integral_constant_expression_p=*/true,
11915 &non_integral_constant_expression_p,
11916 /*template_p=*/false,
11917 /*done=*/true,
11918 /*address_p=*/false,
11919 /*template_arg_p=*/false,
11920 &error_msg,
11921 id_expr_start_token->location));
11923 if (expr == error_mark_node)
11924 /* We found an id-expression, but it was something that we
11925 should not have found. This is an error, not something
11926 we can recover from, so note that we found an
11927 id-expression and we'll recover as gracefully as
11928 possible. */
11929 id_expression_or_member_access_p = true;
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)
11941 /* Abort the id-expression parse. */
11942 cp_parser_abort_tentative_parse (parser);
11944 /* Parsing tentatively, again. */
11945 cp_parser_parse_tentatively (parser);
11947 /* Parse a class member access. */
11948 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11949 /*cast_p=*/false, /*decltype*/true,
11950 /*member_access_only_p=*/true, NULL);
11952 if (expr
11953 && expr != error_mark_node
11954 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11955 /* We have an id-expression. */
11956 id_expression_or_member_access_p = true;
11959 if (id_expression_or_member_access_p)
11960 /* We have parsed the complete id-expression or member access. */
11961 cp_parser_parse_definitely (parser);
11962 else
11964 /* Abort our attempt to parse an id-expression or member access
11965 expression. */
11966 cp_parser_abort_tentative_parse (parser);
11968 /* Parse a full expression. */
11969 expr = cp_parser_expression (parser, /*cast_p=*/false,
11970 /*decltype*/true, NULL);
11973 return expr;
11976 /* Parse a `decltype' type. Returns the type.
11978 simple-type-specifier:
11979 decltype ( expression )
11980 C++14 proposal:
11981 decltype ( auto ) */
11983 static tree
11984 cp_parser_decltype (cp_parser *parser)
11986 tree expr;
11987 bool id_expression_or_member_access_p = false;
11988 const char *saved_message;
11989 bool saved_integral_constant_expression_p;
11990 bool saved_non_integral_constant_expression_p;
11991 bool saved_greater_than_is_operator_p;
11992 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11994 if (start_token->type == CPP_DECLTYPE)
11996 /* Already parsed. */
11997 cp_lexer_consume_token (parser->lexer);
11998 return start_token->u.value;
12001 /* Look for the `decltype' token. */
12002 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12003 return error_mark_node;
12005 /* Parse the opening `('. */
12006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12007 return error_mark_node;
12009 /* decltype (auto) */
12010 if (cxx_dialect >= cxx1y
12011 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12013 cp_lexer_consume_token (parser->lexer);
12014 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12015 return error_mark_node;
12016 expr = make_decltype_auto ();
12017 AUTO_IS_DECLTYPE (expr) = true;
12018 goto rewrite;
12021 /* Types cannot be defined in a `decltype' expression. Save away the
12022 old message. */
12023 saved_message = parser->type_definition_forbidden_message;
12025 /* And create the new one. */
12026 parser->type_definition_forbidden_message
12027 = G_("types may not be defined in %<decltype%> expressions");
12029 /* The restrictions on constant-expressions do not apply inside
12030 decltype expressions. */
12031 saved_integral_constant_expression_p
12032 = parser->integral_constant_expression_p;
12033 saved_non_integral_constant_expression_p
12034 = parser->non_integral_constant_expression_p;
12035 parser->integral_constant_expression_p = false;
12037 /* Within a parenthesized expression, a `>' token is always
12038 the greater-than operator. */
12039 saved_greater_than_is_operator_p
12040 = parser->greater_than_is_operator_p;
12041 parser->greater_than_is_operator_p = true;
12043 /* Do not actually evaluate the expression. */
12044 ++cp_unevaluated_operand;
12046 /* Do not warn about problems with the expression. */
12047 ++c_inhibit_evaluation_warnings;
12049 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12051 /* Go back to evaluating expressions. */
12052 --cp_unevaluated_operand;
12053 --c_inhibit_evaluation_warnings;
12055 /* The `>' token might be the end of a template-id or
12056 template-parameter-list now. */
12057 parser->greater_than_is_operator_p
12058 = saved_greater_than_is_operator_p;
12060 /* Restore the old message and the integral constant expression
12061 flags. */
12062 parser->type_definition_forbidden_message = saved_message;
12063 parser->integral_constant_expression_p
12064 = saved_integral_constant_expression_p;
12065 parser->non_integral_constant_expression_p
12066 = saved_non_integral_constant_expression_p;
12068 /* Parse to the closing `)'. */
12069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12071 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12072 /*consume_paren=*/true);
12073 return error_mark_node;
12076 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12077 tf_warning_or_error);
12079 rewrite:
12080 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12081 it again. */
12082 start_token->type = CPP_DECLTYPE;
12083 start_token->u.value = expr;
12084 start_token->keyword = RID_MAX;
12085 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12087 return expr;
12090 /* Special member functions [gram.special] */
12092 /* Parse a conversion-function-id.
12094 conversion-function-id:
12095 operator conversion-type-id
12097 Returns an IDENTIFIER_NODE representing the operator. */
12099 static tree
12100 cp_parser_conversion_function_id (cp_parser* parser)
12102 tree type;
12103 tree saved_scope;
12104 tree saved_qualifying_scope;
12105 tree saved_object_scope;
12106 tree pushed_scope = NULL_TREE;
12108 /* Look for the `operator' token. */
12109 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12110 return error_mark_node;
12111 /* When we parse the conversion-type-id, the current scope will be
12112 reset. However, we need that information in able to look up the
12113 conversion function later, so we save it here. */
12114 saved_scope = parser->scope;
12115 saved_qualifying_scope = parser->qualifying_scope;
12116 saved_object_scope = parser->object_scope;
12117 /* We must enter the scope of the class so that the names of
12118 entities declared within the class are available in the
12119 conversion-type-id. For example, consider:
12121 struct S {
12122 typedef int I;
12123 operator I();
12126 S::operator I() { ... }
12128 In order to see that `I' is a type-name in the definition, we
12129 must be in the scope of `S'. */
12130 if (saved_scope)
12131 pushed_scope = push_scope (saved_scope);
12132 /* Parse the conversion-type-id. */
12133 type = cp_parser_conversion_type_id (parser);
12134 /* Leave the scope of the class, if any. */
12135 if (pushed_scope)
12136 pop_scope (pushed_scope);
12137 /* Restore the saved scope. */
12138 parser->scope = saved_scope;
12139 parser->qualifying_scope = saved_qualifying_scope;
12140 parser->object_scope = saved_object_scope;
12141 /* If the TYPE is invalid, indicate failure. */
12142 if (type == error_mark_node)
12143 return error_mark_node;
12144 return mangle_conv_op_name_for_type (type);
12147 /* Parse a conversion-type-id:
12149 conversion-type-id:
12150 type-specifier-seq conversion-declarator [opt]
12152 Returns the TYPE specified. */
12154 static tree
12155 cp_parser_conversion_type_id (cp_parser* parser)
12157 tree attributes;
12158 cp_decl_specifier_seq type_specifiers;
12159 cp_declarator *declarator;
12160 tree type_specified;
12161 const char *saved_message;
12163 /* Parse the attributes. */
12164 attributes = cp_parser_attributes_opt (parser);
12166 saved_message = parser->type_definition_forbidden_message;
12167 parser->type_definition_forbidden_message
12168 = G_("types may not be defined in a conversion-type-id");
12170 /* Parse the type-specifiers. */
12171 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12172 /*is_trailing_return=*/false,
12173 &type_specifiers);
12175 parser->type_definition_forbidden_message = saved_message;
12177 /* If that didn't work, stop. */
12178 if (type_specifiers.type == error_mark_node)
12179 return error_mark_node;
12180 /* Parse the conversion-declarator. */
12181 declarator = cp_parser_conversion_declarator_opt (parser);
12183 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12184 /*initialized=*/0, &attributes);
12185 if (attributes)
12186 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12188 /* Don't give this error when parsing tentatively. This happens to
12189 work because we always parse this definitively once. */
12190 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12191 && type_uses_auto (type_specified))
12193 if (cxx_dialect < cxx1y)
12195 error ("invalid use of %<auto%> in conversion operator");
12196 return error_mark_node;
12198 else if (template_parm_scope_p ())
12199 warning (0, "use of %<auto%> in member template "
12200 "conversion operator can never be deduced");
12203 return type_specified;
12206 /* Parse an (optional) conversion-declarator.
12208 conversion-declarator:
12209 ptr-operator conversion-declarator [opt]
12213 static cp_declarator *
12214 cp_parser_conversion_declarator_opt (cp_parser* parser)
12216 enum tree_code code;
12217 tree class_type, std_attributes = NULL_TREE;
12218 cp_cv_quals cv_quals;
12220 /* We don't know if there's a ptr-operator next, or not. */
12221 cp_parser_parse_tentatively (parser);
12222 /* Try the ptr-operator. */
12223 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12224 &std_attributes);
12225 /* If it worked, look for more conversion-declarators. */
12226 if (cp_parser_parse_definitely (parser))
12228 cp_declarator *declarator;
12230 /* Parse another optional declarator. */
12231 declarator = cp_parser_conversion_declarator_opt (parser);
12233 declarator = cp_parser_make_indirect_declarator
12234 (code, class_type, cv_quals, declarator, std_attributes);
12236 return declarator;
12239 return NULL;
12242 /* Parse an (optional) ctor-initializer.
12244 ctor-initializer:
12245 : mem-initializer-list
12247 Returns TRUE iff the ctor-initializer was actually present. */
12249 static bool
12250 cp_parser_ctor_initializer_opt (cp_parser* parser)
12252 /* If the next token is not a `:', then there is no
12253 ctor-initializer. */
12254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12256 /* Do default initialization of any bases and members. */
12257 if (DECL_CONSTRUCTOR_P (current_function_decl))
12258 finish_mem_initializers (NULL_TREE);
12260 return false;
12263 /* Consume the `:' token. */
12264 cp_lexer_consume_token (parser->lexer);
12265 /* And the mem-initializer-list. */
12266 cp_parser_mem_initializer_list (parser);
12268 return true;
12271 /* Parse a mem-initializer-list.
12273 mem-initializer-list:
12274 mem-initializer ... [opt]
12275 mem-initializer ... [opt] , mem-initializer-list */
12277 static void
12278 cp_parser_mem_initializer_list (cp_parser* parser)
12280 tree mem_initializer_list = NULL_TREE;
12281 tree target_ctor = error_mark_node;
12282 cp_token *token = cp_lexer_peek_token (parser->lexer);
12284 /* Let the semantic analysis code know that we are starting the
12285 mem-initializer-list. */
12286 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12287 error_at (token->location,
12288 "only constructors take member initializers");
12290 /* Loop through the list. */
12291 while (true)
12293 tree mem_initializer;
12295 token = cp_lexer_peek_token (parser->lexer);
12296 /* Parse the mem-initializer. */
12297 mem_initializer = cp_parser_mem_initializer (parser);
12298 /* If the next token is a `...', we're expanding member initializers. */
12299 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12301 /* Consume the `...'. */
12302 cp_lexer_consume_token (parser->lexer);
12304 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12305 can be expanded but members cannot. */
12306 if (mem_initializer != error_mark_node
12307 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12309 error_at (token->location,
12310 "cannot expand initializer for member %<%D%>",
12311 TREE_PURPOSE (mem_initializer));
12312 mem_initializer = error_mark_node;
12315 /* Construct the pack expansion type. */
12316 if (mem_initializer != error_mark_node)
12317 mem_initializer = make_pack_expansion (mem_initializer);
12319 if (target_ctor != error_mark_node
12320 && mem_initializer != error_mark_node)
12322 error ("mem-initializer for %qD follows constructor delegation",
12323 TREE_PURPOSE (mem_initializer));
12324 mem_initializer = error_mark_node;
12326 /* Look for a target constructor. */
12327 if (mem_initializer != error_mark_node
12328 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12329 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12331 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12332 if (mem_initializer_list)
12334 error ("constructor delegation follows mem-initializer for %qD",
12335 TREE_PURPOSE (mem_initializer_list));
12336 mem_initializer = error_mark_node;
12338 target_ctor = mem_initializer;
12340 /* Add it to the list, unless it was erroneous. */
12341 if (mem_initializer != error_mark_node)
12343 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12344 mem_initializer_list = mem_initializer;
12346 /* If the next token is not a `,', we're done. */
12347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12348 break;
12349 /* Consume the `,' token. */
12350 cp_lexer_consume_token (parser->lexer);
12353 /* Perform semantic analysis. */
12354 if (DECL_CONSTRUCTOR_P (current_function_decl))
12355 finish_mem_initializers (mem_initializer_list);
12358 /* Parse a mem-initializer.
12360 mem-initializer:
12361 mem-initializer-id ( expression-list [opt] )
12362 mem-initializer-id braced-init-list
12364 GNU extension:
12366 mem-initializer:
12367 ( expression-list [opt] )
12369 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12370 class) or FIELD_DECL (for a non-static data member) to initialize;
12371 the TREE_VALUE is the expression-list. An empty initialization
12372 list is represented by void_list_node. */
12374 static tree
12375 cp_parser_mem_initializer (cp_parser* parser)
12377 tree mem_initializer_id;
12378 tree expression_list;
12379 tree member;
12380 cp_token *token = cp_lexer_peek_token (parser->lexer);
12382 /* Find out what is being initialized. */
12383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12385 permerror (token->location,
12386 "anachronistic old-style base class initializer");
12387 mem_initializer_id = NULL_TREE;
12389 else
12391 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12392 if (mem_initializer_id == error_mark_node)
12393 return mem_initializer_id;
12395 member = expand_member_init (mem_initializer_id);
12396 if (member && !DECL_P (member))
12397 in_base_initializer = 1;
12399 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12401 bool expr_non_constant_p;
12402 cp_lexer_set_source_position (parser->lexer);
12403 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12404 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12405 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12406 expression_list = build_tree_list (NULL_TREE, expression_list);
12408 else
12410 vec<tree, va_gc> *vec;
12411 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12412 /*cast_p=*/false,
12413 /*allow_expansion_p=*/true,
12414 /*non_constant_p=*/NULL);
12415 if (vec == NULL)
12416 return error_mark_node;
12417 expression_list = build_tree_list_vec (vec);
12418 release_tree_vector (vec);
12421 if (expression_list == error_mark_node)
12422 return error_mark_node;
12423 if (!expression_list)
12424 expression_list = void_type_node;
12426 in_base_initializer = 0;
12428 return member ? build_tree_list (member, expression_list) : error_mark_node;
12431 /* Parse a mem-initializer-id.
12433 mem-initializer-id:
12434 :: [opt] nested-name-specifier [opt] class-name
12435 identifier
12437 Returns a TYPE indicating the class to be initializer for the first
12438 production. Returns an IDENTIFIER_NODE indicating the data member
12439 to be initialized for the second production. */
12441 static tree
12442 cp_parser_mem_initializer_id (cp_parser* parser)
12444 bool global_scope_p;
12445 bool nested_name_specifier_p;
12446 bool template_p = false;
12447 tree id;
12449 cp_token *token = cp_lexer_peek_token (parser->lexer);
12451 /* `typename' is not allowed in this context ([temp.res]). */
12452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12454 error_at (token->location,
12455 "keyword %<typename%> not allowed in this context (a qualified "
12456 "member initializer is implicitly a type)");
12457 cp_lexer_consume_token (parser->lexer);
12459 /* Look for the optional `::' operator. */
12460 global_scope_p
12461 = (cp_parser_global_scope_opt (parser,
12462 /*current_scope_valid_p=*/false)
12463 != NULL_TREE);
12464 /* Look for the optional nested-name-specifier. The simplest way to
12465 implement:
12467 [temp.res]
12469 The keyword `typename' is not permitted in a base-specifier or
12470 mem-initializer; in these contexts a qualified name that
12471 depends on a template-parameter is implicitly assumed to be a
12472 type name.
12474 is to assume that we have seen the `typename' keyword at this
12475 point. */
12476 nested_name_specifier_p
12477 = (cp_parser_nested_name_specifier_opt (parser,
12478 /*typename_keyword_p=*/true,
12479 /*check_dependency_p=*/true,
12480 /*type_p=*/true,
12481 /*is_declaration=*/true)
12482 != NULL_TREE);
12483 if (nested_name_specifier_p)
12484 template_p = cp_parser_optional_template_keyword (parser);
12485 /* If there is a `::' operator or a nested-name-specifier, then we
12486 are definitely looking for a class-name. */
12487 if (global_scope_p || nested_name_specifier_p)
12488 return cp_parser_class_name (parser,
12489 /*typename_keyword_p=*/true,
12490 /*template_keyword_p=*/template_p,
12491 typename_type,
12492 /*check_dependency_p=*/true,
12493 /*class_head_p=*/false,
12494 /*is_declaration=*/true);
12495 /* Otherwise, we could also be looking for an ordinary identifier. */
12496 cp_parser_parse_tentatively (parser);
12497 /* Try a class-name. */
12498 id = cp_parser_class_name (parser,
12499 /*typename_keyword_p=*/true,
12500 /*template_keyword_p=*/false,
12501 none_type,
12502 /*check_dependency_p=*/true,
12503 /*class_head_p=*/false,
12504 /*is_declaration=*/true);
12505 /* If we found one, we're done. */
12506 if (cp_parser_parse_definitely (parser))
12507 return id;
12508 /* Otherwise, look for an ordinary identifier. */
12509 return cp_parser_identifier (parser);
12512 /* Overloading [gram.over] */
12514 /* Parse an operator-function-id.
12516 operator-function-id:
12517 operator operator
12519 Returns an IDENTIFIER_NODE for the operator which is a
12520 human-readable spelling of the identifier, e.g., `operator +'. */
12522 static tree
12523 cp_parser_operator_function_id (cp_parser* parser)
12525 /* Look for the `operator' keyword. */
12526 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12527 return error_mark_node;
12528 /* And then the name of the operator itself. */
12529 return cp_parser_operator (parser);
12532 /* Return an identifier node for a user-defined literal operator.
12533 The suffix identifier is chained to the operator name identifier. */
12535 static tree
12536 cp_literal_operator_id (const char* name)
12538 tree identifier;
12539 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12540 + strlen (name) + 10);
12541 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12542 identifier = get_identifier (buffer);
12544 return identifier;
12547 /* Parse an operator.
12549 operator:
12550 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12551 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12552 || ++ -- , ->* -> () []
12554 GNU Extensions:
12556 operator:
12557 <? >? <?= >?=
12559 Returns an IDENTIFIER_NODE for the operator which is a
12560 human-readable spelling of the identifier, e.g., `operator +'. */
12562 static tree
12563 cp_parser_operator (cp_parser* parser)
12565 tree id = NULL_TREE;
12566 cp_token *token;
12567 bool bad_encoding_prefix = false;
12569 /* Peek at the next token. */
12570 token = cp_lexer_peek_token (parser->lexer);
12571 /* Figure out which operator we have. */
12572 switch (token->type)
12574 case CPP_KEYWORD:
12576 enum tree_code op;
12578 /* The keyword should be either `new' or `delete'. */
12579 if (token->keyword == RID_NEW)
12580 op = NEW_EXPR;
12581 else if (token->keyword == RID_DELETE)
12582 op = DELETE_EXPR;
12583 else
12584 break;
12586 /* Consume the `new' or `delete' token. */
12587 cp_lexer_consume_token (parser->lexer);
12589 /* Peek at the next token. */
12590 token = cp_lexer_peek_token (parser->lexer);
12591 /* If it's a `[' token then this is the array variant of the
12592 operator. */
12593 if (token->type == CPP_OPEN_SQUARE)
12595 /* Consume the `[' token. */
12596 cp_lexer_consume_token (parser->lexer);
12597 /* Look for the `]' token. */
12598 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12599 id = ansi_opname (op == NEW_EXPR
12600 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12602 /* Otherwise, we have the non-array variant. */
12603 else
12604 id = ansi_opname (op);
12606 return id;
12609 case CPP_PLUS:
12610 id = ansi_opname (PLUS_EXPR);
12611 break;
12613 case CPP_MINUS:
12614 id = ansi_opname (MINUS_EXPR);
12615 break;
12617 case CPP_MULT:
12618 id = ansi_opname (MULT_EXPR);
12619 break;
12621 case CPP_DIV:
12622 id = ansi_opname (TRUNC_DIV_EXPR);
12623 break;
12625 case CPP_MOD:
12626 id = ansi_opname (TRUNC_MOD_EXPR);
12627 break;
12629 case CPP_XOR:
12630 id = ansi_opname (BIT_XOR_EXPR);
12631 break;
12633 case CPP_AND:
12634 id = ansi_opname (BIT_AND_EXPR);
12635 break;
12637 case CPP_OR:
12638 id = ansi_opname (BIT_IOR_EXPR);
12639 break;
12641 case CPP_COMPL:
12642 id = ansi_opname (BIT_NOT_EXPR);
12643 break;
12645 case CPP_NOT:
12646 id = ansi_opname (TRUTH_NOT_EXPR);
12647 break;
12649 case CPP_EQ:
12650 id = ansi_assopname (NOP_EXPR);
12651 break;
12653 case CPP_LESS:
12654 id = ansi_opname (LT_EXPR);
12655 break;
12657 case CPP_GREATER:
12658 id = ansi_opname (GT_EXPR);
12659 break;
12661 case CPP_PLUS_EQ:
12662 id = ansi_assopname (PLUS_EXPR);
12663 break;
12665 case CPP_MINUS_EQ:
12666 id = ansi_assopname (MINUS_EXPR);
12667 break;
12669 case CPP_MULT_EQ:
12670 id = ansi_assopname (MULT_EXPR);
12671 break;
12673 case CPP_DIV_EQ:
12674 id = ansi_assopname (TRUNC_DIV_EXPR);
12675 break;
12677 case CPP_MOD_EQ:
12678 id = ansi_assopname (TRUNC_MOD_EXPR);
12679 break;
12681 case CPP_XOR_EQ:
12682 id = ansi_assopname (BIT_XOR_EXPR);
12683 break;
12685 case CPP_AND_EQ:
12686 id = ansi_assopname (BIT_AND_EXPR);
12687 break;
12689 case CPP_OR_EQ:
12690 id = ansi_assopname (BIT_IOR_EXPR);
12691 break;
12693 case CPP_LSHIFT:
12694 id = ansi_opname (LSHIFT_EXPR);
12695 break;
12697 case CPP_RSHIFT:
12698 id = ansi_opname (RSHIFT_EXPR);
12699 break;
12701 case CPP_LSHIFT_EQ:
12702 id = ansi_assopname (LSHIFT_EXPR);
12703 break;
12705 case CPP_RSHIFT_EQ:
12706 id = ansi_assopname (RSHIFT_EXPR);
12707 break;
12709 case CPP_EQ_EQ:
12710 id = ansi_opname (EQ_EXPR);
12711 break;
12713 case CPP_NOT_EQ:
12714 id = ansi_opname (NE_EXPR);
12715 break;
12717 case CPP_LESS_EQ:
12718 id = ansi_opname (LE_EXPR);
12719 break;
12721 case CPP_GREATER_EQ:
12722 id = ansi_opname (GE_EXPR);
12723 break;
12725 case CPP_AND_AND:
12726 id = ansi_opname (TRUTH_ANDIF_EXPR);
12727 break;
12729 case CPP_OR_OR:
12730 id = ansi_opname (TRUTH_ORIF_EXPR);
12731 break;
12733 case CPP_PLUS_PLUS:
12734 id = ansi_opname (POSTINCREMENT_EXPR);
12735 break;
12737 case CPP_MINUS_MINUS:
12738 id = ansi_opname (PREDECREMENT_EXPR);
12739 break;
12741 case CPP_COMMA:
12742 id = ansi_opname (COMPOUND_EXPR);
12743 break;
12745 case CPP_DEREF_STAR:
12746 id = ansi_opname (MEMBER_REF);
12747 break;
12749 case CPP_DEREF:
12750 id = ansi_opname (COMPONENT_REF);
12751 break;
12753 case CPP_OPEN_PAREN:
12754 /* Consume the `('. */
12755 cp_lexer_consume_token (parser->lexer);
12756 /* Look for the matching `)'. */
12757 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12758 return ansi_opname (CALL_EXPR);
12760 case CPP_OPEN_SQUARE:
12761 /* Consume the `['. */
12762 cp_lexer_consume_token (parser->lexer);
12763 /* Look for the matching `]'. */
12764 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12765 return ansi_opname (ARRAY_REF);
12767 case CPP_WSTRING:
12768 case CPP_STRING16:
12769 case CPP_STRING32:
12770 case CPP_UTF8STRING:
12771 bad_encoding_prefix = true;
12772 /* Fall through. */
12774 case CPP_STRING:
12775 if (cxx_dialect == cxx98)
12776 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12777 if (bad_encoding_prefix)
12779 error ("invalid encoding prefix in literal operator");
12780 return error_mark_node;
12782 if (TREE_STRING_LENGTH (token->u.value) > 2)
12784 error ("expected empty string after %<operator%> keyword");
12785 return error_mark_node;
12787 /* Consume the string. */
12788 cp_lexer_consume_token (parser->lexer);
12789 /* Look for the suffix identifier. */
12790 token = cp_lexer_peek_token (parser->lexer);
12791 if (token->type == CPP_NAME)
12793 id = cp_parser_identifier (parser);
12794 if (id != error_mark_node)
12796 const char *name = IDENTIFIER_POINTER (id);
12797 return cp_literal_operator_id (name);
12800 else if (token->type == CPP_KEYWORD)
12802 error ("unexpected keyword;"
12803 " remove space between quotes and suffix identifier");
12804 return error_mark_node;
12806 else
12808 error ("expected suffix identifier");
12809 return error_mark_node;
12812 case CPP_WSTRING_USERDEF:
12813 case CPP_STRING16_USERDEF:
12814 case CPP_STRING32_USERDEF:
12815 case CPP_UTF8STRING_USERDEF:
12816 bad_encoding_prefix = true;
12817 /* Fall through. */
12819 case CPP_STRING_USERDEF:
12820 if (cxx_dialect == cxx98)
12821 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12822 if (bad_encoding_prefix)
12824 error ("invalid encoding prefix in literal operator");
12825 return error_mark_node;
12828 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12829 if (TREE_STRING_LENGTH (string_tree) > 2)
12831 error ("expected empty string after %<operator%> keyword");
12832 return error_mark_node;
12834 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12835 /* Consume the user-defined string literal. */
12836 cp_lexer_consume_token (parser->lexer);
12837 if (id != error_mark_node)
12839 const char *name = IDENTIFIER_POINTER (id);
12840 return cp_literal_operator_id (name);
12842 else
12843 return error_mark_node;
12846 default:
12847 /* Anything else is an error. */
12848 break;
12851 /* If we have selected an identifier, we need to consume the
12852 operator token. */
12853 if (id)
12854 cp_lexer_consume_token (parser->lexer);
12855 /* Otherwise, no valid operator name was present. */
12856 else
12858 cp_parser_error (parser, "expected operator");
12859 id = error_mark_node;
12862 return id;
12865 /* Parse a template-declaration.
12867 template-declaration:
12868 export [opt] template < template-parameter-list > declaration
12870 If MEMBER_P is TRUE, this template-declaration occurs within a
12871 class-specifier.
12873 The grammar rule given by the standard isn't correct. What
12874 is really meant is:
12876 template-declaration:
12877 export [opt] template-parameter-list-seq
12878 decl-specifier-seq [opt] init-declarator [opt] ;
12879 export [opt] template-parameter-list-seq
12880 function-definition
12882 template-parameter-list-seq:
12883 template-parameter-list-seq [opt]
12884 template < template-parameter-list > */
12886 static void
12887 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12889 /* A hack to disable -Wself-assign warning in template parsing. */
12890 int old_warn_self_assign = warn_self_assign;
12891 warn_self_assign = 0;
12892 /* Check for `export'. */
12893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12895 /* Consume the `export' token. */
12896 cp_lexer_consume_token (parser->lexer);
12897 /* Warn that we do not support `export'. */
12898 warning (0, "keyword %<export%> not implemented, and will be ignored");
12901 cp_parser_template_declaration_after_export (parser, member_p);
12902 warn_self_assign = old_warn_self_assign;
12905 /* Parse a template-parameter-list.
12907 template-parameter-list:
12908 template-parameter
12909 template-parameter-list , template-parameter
12911 Returns a TREE_LIST. Each node represents a template parameter.
12912 The nodes are connected via their TREE_CHAINs. */
12914 static tree
12915 cp_parser_template_parameter_list (cp_parser* parser)
12917 tree parameter_list = NULL_TREE;
12919 begin_template_parm_list ();
12921 /* The loop below parses the template parms. We first need to know
12922 the total number of template parms to be able to compute proper
12923 canonical types of each dependent type. So after the loop, when
12924 we know the total number of template parms,
12925 end_template_parm_list computes the proper canonical types and
12926 fixes up the dependent types accordingly. */
12927 while (true)
12929 tree parameter;
12930 bool is_non_type;
12931 bool is_parameter_pack;
12932 location_t parm_loc;
12934 /* Parse the template-parameter. */
12935 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12936 parameter = cp_parser_template_parameter (parser,
12937 &is_non_type,
12938 &is_parameter_pack);
12939 /* Add it to the list. */
12940 if (parameter != error_mark_node)
12941 parameter_list = process_template_parm (parameter_list,
12942 parm_loc,
12943 parameter,
12944 is_non_type,
12945 is_parameter_pack);
12946 else
12948 tree err_parm = build_tree_list (parameter, parameter);
12949 parameter_list = chainon (parameter_list, err_parm);
12952 /* If the next token is not a `,', we're done. */
12953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12954 break;
12955 /* Otherwise, consume the `,' token. */
12956 cp_lexer_consume_token (parser->lexer);
12959 return end_template_parm_list (parameter_list);
12962 /* Parse a template-parameter.
12964 template-parameter:
12965 type-parameter
12966 parameter-declaration
12968 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12969 the parameter. The TREE_PURPOSE is the default value, if any.
12970 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12971 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12972 set to true iff this parameter is a parameter pack. */
12974 static tree
12975 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12976 bool *is_parameter_pack)
12978 cp_token *token;
12979 cp_parameter_declarator *parameter_declarator;
12980 cp_declarator *id_declarator;
12981 tree parm;
12983 /* Assume it is a type parameter or a template parameter. */
12984 *is_non_type = false;
12985 /* Assume it not a parameter pack. */
12986 *is_parameter_pack = false;
12987 /* Peek at the next token. */
12988 token = cp_lexer_peek_token (parser->lexer);
12989 /* If it is `class' or `template', we have a type-parameter. */
12990 if (token->keyword == RID_TEMPLATE)
12991 return cp_parser_type_parameter (parser, is_parameter_pack);
12992 /* If it is `class' or `typename' we do not know yet whether it is a
12993 type parameter or a non-type parameter. Consider:
12995 template <typename T, typename T::X X> ...
12999 template <class C, class D*> ...
13001 Here, the first parameter is a type parameter, and the second is
13002 a non-type parameter. We can tell by looking at the token after
13003 the identifier -- if it is a `,', `=', or `>' then we have a type
13004 parameter. */
13005 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13007 /* Peek at the token after `class' or `typename'. */
13008 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13009 /* If it's an ellipsis, we have a template type parameter
13010 pack. */
13011 if (token->type == CPP_ELLIPSIS)
13012 return cp_parser_type_parameter (parser, is_parameter_pack);
13013 /* If it's an identifier, skip it. */
13014 if (token->type == CPP_NAME)
13015 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13016 /* Now, see if the token looks like the end of a template
13017 parameter. */
13018 if (token->type == CPP_COMMA
13019 || token->type == CPP_EQ
13020 || token->type == CPP_GREATER)
13021 return cp_parser_type_parameter (parser, is_parameter_pack);
13024 /* Otherwise, it is a non-type parameter.
13026 [temp.param]
13028 When parsing a default template-argument for a non-type
13029 template-parameter, the first non-nested `>' is taken as the end
13030 of the template parameter-list rather than a greater-than
13031 operator. */
13032 *is_non_type = true;
13033 parameter_declarator
13034 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13035 /*parenthesized_p=*/NULL);
13037 if (!parameter_declarator)
13038 return error_mark_node;
13040 /* If the parameter declaration is marked as a parameter pack, set
13041 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13042 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13043 grokdeclarator. */
13044 if (parameter_declarator->declarator
13045 && parameter_declarator->declarator->parameter_pack_p)
13047 *is_parameter_pack = true;
13048 parameter_declarator->declarator->parameter_pack_p = false;
13051 if (parameter_declarator->default_argument)
13053 /* Can happen in some cases of erroneous input (c++/34892). */
13054 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13055 /* Consume the `...' for better error recovery. */
13056 cp_lexer_consume_token (parser->lexer);
13058 /* If the next token is an ellipsis, and we don't already have it
13059 marked as a parameter pack, then we have a parameter pack (that
13060 has no declarator). */
13061 else if (!*is_parameter_pack
13062 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13063 && (declarator_can_be_parameter_pack
13064 (parameter_declarator->declarator)))
13066 /* Consume the `...'. */
13067 cp_lexer_consume_token (parser->lexer);
13068 maybe_warn_variadic_templates ();
13070 *is_parameter_pack = true;
13072 /* We might end up with a pack expansion as the type of the non-type
13073 template parameter, in which case this is a non-type template
13074 parameter pack. */
13075 else if (parameter_declarator->decl_specifiers.type
13076 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13078 *is_parameter_pack = true;
13079 parameter_declarator->decl_specifiers.type =
13080 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13083 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13085 /* Parameter packs cannot have default arguments. However, a
13086 user may try to do so, so we'll parse them and give an
13087 appropriate diagnostic here. */
13089 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13091 /* Find the name of the parameter pack. */
13092 id_declarator = parameter_declarator->declarator;
13093 while (id_declarator && id_declarator->kind != cdk_id)
13094 id_declarator = id_declarator->declarator;
13096 if (id_declarator && id_declarator->kind == cdk_id)
13097 error_at (start_token->location,
13098 "template parameter pack %qD cannot have a default argument",
13099 id_declarator->u.id.unqualified_name);
13100 else
13101 error_at (start_token->location,
13102 "template parameter pack cannot have a default argument");
13104 /* Parse the default argument, but throw away the result. */
13105 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13108 parm = grokdeclarator (parameter_declarator->declarator,
13109 &parameter_declarator->decl_specifiers,
13110 TPARM, /*initialized=*/0,
13111 /*attrlist=*/NULL);
13112 if (parm == error_mark_node)
13113 return error_mark_node;
13115 return build_tree_list (parameter_declarator->default_argument, parm);
13118 /* Parse a type-parameter.
13120 type-parameter:
13121 class identifier [opt]
13122 class identifier [opt] = type-id
13123 typename identifier [opt]
13124 typename identifier [opt] = type-id
13125 template < template-parameter-list > class identifier [opt]
13126 template < template-parameter-list > class identifier [opt]
13127 = id-expression
13129 GNU Extension (variadic templates):
13131 type-parameter:
13132 class ... identifier [opt]
13133 typename ... identifier [opt]
13135 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13136 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13137 the declaration of the parameter.
13139 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13141 static tree
13142 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13144 cp_token *token;
13145 tree parameter;
13147 /* Look for a keyword to tell us what kind of parameter this is. */
13148 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13149 if (!token)
13150 return error_mark_node;
13152 switch (token->keyword)
13154 case RID_CLASS:
13155 case RID_TYPENAME:
13157 tree identifier;
13158 tree default_argument;
13160 /* If the next token is an ellipsis, we have a template
13161 argument pack. */
13162 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13164 /* Consume the `...' token. */
13165 cp_lexer_consume_token (parser->lexer);
13166 maybe_warn_variadic_templates ();
13168 *is_parameter_pack = true;
13171 /* If the next token is an identifier, then it names the
13172 parameter. */
13173 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13174 identifier = cp_parser_identifier (parser);
13175 else
13176 identifier = NULL_TREE;
13178 /* Create the parameter. */
13179 parameter = finish_template_type_parm (class_type_node, identifier);
13181 /* If the next token is an `=', we have a default argument. */
13182 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13184 /* Consume the `=' token. */
13185 cp_lexer_consume_token (parser->lexer);
13186 /* Parse the default-argument. */
13187 push_deferring_access_checks (dk_no_deferred);
13188 default_argument = cp_parser_type_id (parser);
13190 /* Template parameter packs cannot have default
13191 arguments. */
13192 if (*is_parameter_pack)
13194 if (identifier)
13195 error_at (token->location,
13196 "template parameter pack %qD cannot have a "
13197 "default argument", identifier);
13198 else
13199 error_at (token->location,
13200 "template parameter packs cannot have "
13201 "default arguments");
13202 default_argument = NULL_TREE;
13204 pop_deferring_access_checks ();
13206 else
13207 default_argument = NULL_TREE;
13209 /* Create the combined representation of the parameter and the
13210 default argument. */
13211 parameter = build_tree_list (default_argument, parameter);
13213 break;
13215 case RID_TEMPLATE:
13217 tree identifier;
13218 tree default_argument;
13220 /* Look for the `<'. */
13221 cp_parser_require (parser, CPP_LESS, RT_LESS);
13222 /* Parse the template-parameter-list. */
13223 cp_parser_template_parameter_list (parser);
13224 /* Look for the `>'. */
13225 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13226 /* Look for the `class' keyword. */
13227 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13228 /* If the next token is an ellipsis, we have a template
13229 argument pack. */
13230 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13232 /* Consume the `...' token. */
13233 cp_lexer_consume_token (parser->lexer);
13234 maybe_warn_variadic_templates ();
13236 *is_parameter_pack = true;
13238 /* If the next token is an `=', then there is a
13239 default-argument. If the next token is a `>', we are at
13240 the end of the parameter-list. If the next token is a `,',
13241 then we are at the end of this parameter. */
13242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13243 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13244 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13246 identifier = cp_parser_identifier (parser);
13247 /* Treat invalid names as if the parameter were nameless. */
13248 if (identifier == error_mark_node)
13249 identifier = NULL_TREE;
13251 else
13252 identifier = NULL_TREE;
13254 /* Create the template parameter. */
13255 parameter = finish_template_template_parm (class_type_node,
13256 identifier);
13258 /* If the next token is an `=', then there is a
13259 default-argument. */
13260 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13262 bool is_template;
13264 /* Consume the `='. */
13265 cp_lexer_consume_token (parser->lexer);
13266 /* Parse the id-expression. */
13267 push_deferring_access_checks (dk_no_deferred);
13268 /* save token before parsing the id-expression, for error
13269 reporting */
13270 token = cp_lexer_peek_token (parser->lexer);
13271 default_argument
13272 = cp_parser_id_expression (parser,
13273 /*template_keyword_p=*/false,
13274 /*check_dependency_p=*/true,
13275 /*template_p=*/&is_template,
13276 /*declarator_p=*/false,
13277 /*optional_p=*/false);
13278 if (TREE_CODE (default_argument) == TYPE_DECL)
13279 /* If the id-expression was a template-id that refers to
13280 a template-class, we already have the declaration here,
13281 so no further lookup is needed. */
13283 else
13284 /* Look up the name. */
13285 default_argument
13286 = cp_parser_lookup_name (parser, default_argument,
13287 none_type,
13288 /*is_template=*/is_template,
13289 /*is_namespace=*/false,
13290 /*check_dependency=*/true,
13291 /*ambiguous_decls=*/NULL,
13292 token->location);
13293 /* See if the default argument is valid. */
13294 default_argument
13295 = check_template_template_default_arg (default_argument);
13297 /* Template parameter packs cannot have default
13298 arguments. */
13299 if (*is_parameter_pack)
13301 if (identifier)
13302 error_at (token->location,
13303 "template parameter pack %qD cannot "
13304 "have a default argument",
13305 identifier);
13306 else
13307 error_at (token->location, "template parameter packs cannot "
13308 "have default arguments");
13309 default_argument = NULL_TREE;
13311 pop_deferring_access_checks ();
13313 else
13314 default_argument = NULL_TREE;
13316 /* Create the combined representation of the parameter and the
13317 default argument. */
13318 parameter = build_tree_list (default_argument, parameter);
13320 break;
13322 default:
13323 gcc_unreachable ();
13324 break;
13327 return parameter;
13330 /* Parse a template-id.
13332 template-id:
13333 template-name < template-argument-list [opt] >
13335 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13336 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13337 returned. Otherwise, if the template-name names a function, or set
13338 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13339 names a class, returns a TYPE_DECL for the specialization.
13341 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13342 uninstantiated templates. */
13344 static tree
13345 cp_parser_template_id (cp_parser *parser,
13346 bool template_keyword_p,
13347 bool check_dependency_p,
13348 enum tag_types tag_type,
13349 bool is_declaration)
13351 int i;
13352 tree templ;
13353 tree arguments;
13354 tree template_id;
13355 cp_token_position start_of_id = 0;
13356 deferred_access_check *chk;
13357 vec<deferred_access_check, va_gc> *access_check;
13358 cp_token *next_token = NULL, *next_token_2 = NULL;
13359 bool is_identifier;
13361 /* If the next token corresponds to a template-id, there is no need
13362 to reparse it. */
13363 next_token = cp_lexer_peek_token (parser->lexer);
13364 if (next_token->type == CPP_TEMPLATE_ID)
13366 struct tree_check *check_value;
13368 /* Get the stored value. */
13369 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13370 /* Perform any access checks that were deferred. */
13371 access_check = check_value->checks;
13372 if (access_check)
13374 FOR_EACH_VEC_ELT (*access_check, i, chk)
13375 perform_or_defer_access_check (chk->binfo,
13376 chk->decl,
13377 chk->diag_decl,
13378 tf_warning_or_error);
13380 /* Return the stored value. */
13381 return check_value->value;
13384 /* Avoid performing name lookup if there is no possibility of
13385 finding a template-id. */
13386 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13387 || (next_token->type == CPP_NAME
13388 && !cp_parser_nth_token_starts_template_argument_list_p
13389 (parser, 2)))
13391 cp_parser_error (parser, "expected template-id");
13392 return error_mark_node;
13395 /* Remember where the template-id starts. */
13396 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13397 start_of_id = cp_lexer_token_position (parser->lexer, false);
13399 push_deferring_access_checks (dk_deferred);
13401 /* Parse the template-name. */
13402 is_identifier = false;
13403 templ = cp_parser_template_name (parser, template_keyword_p,
13404 check_dependency_p,
13405 is_declaration,
13406 tag_type,
13407 &is_identifier);
13408 if (templ == error_mark_node || is_identifier)
13410 pop_deferring_access_checks ();
13411 return templ;
13414 /* If we find the sequence `[:' after a template-name, it's probably
13415 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13416 parse correctly the argument list. */
13417 next_token = cp_lexer_peek_token (parser->lexer);
13418 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13419 if (next_token->type == CPP_OPEN_SQUARE
13420 && next_token->flags & DIGRAPH
13421 && next_token_2->type == CPP_COLON
13422 && !(next_token_2->flags & PREV_WHITE))
13424 cp_parser_parse_tentatively (parser);
13425 /* Change `:' into `::'. */
13426 next_token_2->type = CPP_SCOPE;
13427 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13428 CPP_LESS. */
13429 cp_lexer_consume_token (parser->lexer);
13431 /* Parse the arguments. */
13432 arguments = cp_parser_enclosed_template_argument_list (parser);
13433 if (!cp_parser_parse_definitely (parser))
13435 /* If we couldn't parse an argument list, then we revert our changes
13436 and return simply an error. Maybe this is not a template-id
13437 after all. */
13438 next_token_2->type = CPP_COLON;
13439 cp_parser_error (parser, "expected %<<%>");
13440 pop_deferring_access_checks ();
13441 return error_mark_node;
13443 /* Otherwise, emit an error about the invalid digraph, but continue
13444 parsing because we got our argument list. */
13445 if (permerror (next_token->location,
13446 "%<<::%> cannot begin a template-argument list"))
13448 static bool hint = false;
13449 inform (next_token->location,
13450 "%<<:%> is an alternate spelling for %<[%>."
13451 " Insert whitespace between %<<%> and %<::%>");
13452 if (!hint && !flag_permissive)
13454 inform (next_token->location, "(if you use %<-fpermissive%> "
13455 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13456 "accept your code)");
13457 hint = true;
13461 else
13463 /* Look for the `<' that starts the template-argument-list. */
13464 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13466 pop_deferring_access_checks ();
13467 return error_mark_node;
13469 /* Parse the arguments. */
13470 arguments = cp_parser_enclosed_template_argument_list (parser);
13473 /* Build a representation of the specialization. */
13474 if (identifier_p (templ))
13475 template_id = build_min_nt_loc (next_token->location,
13476 TEMPLATE_ID_EXPR,
13477 templ, arguments);
13478 else if (DECL_TYPE_TEMPLATE_P (templ)
13479 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13481 bool entering_scope;
13482 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13483 template (rather than some instantiation thereof) only if
13484 is not nested within some other construct. For example, in
13485 "template <typename T> void f(T) { A<T>::", A<T> is just an
13486 instantiation of A. */
13487 entering_scope = (template_parm_scope_p ()
13488 && cp_lexer_next_token_is (parser->lexer,
13489 CPP_SCOPE));
13490 template_id
13491 = finish_template_type (templ, arguments, entering_scope);
13493 else
13495 /* If it's not a class-template or a template-template, it should be
13496 a function-template. */
13497 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13498 || TREE_CODE (templ) == OVERLOAD
13499 || BASELINK_P (templ)));
13501 template_id = lookup_template_function (templ, arguments);
13504 /* If parsing tentatively, replace the sequence of tokens that makes
13505 up the template-id with a CPP_TEMPLATE_ID token. That way,
13506 should we re-parse the token stream, we will not have to repeat
13507 the effort required to do the parse, nor will we issue duplicate
13508 error messages about problems during instantiation of the
13509 template. */
13510 if (start_of_id
13511 /* Don't do this if we had a parse error in a declarator; re-parsing
13512 might succeed if a name changes meaning (60361). */
13513 && !(cp_parser_error_occurred (parser)
13514 && cp_parser_parsing_tentatively (parser)
13515 && parser->in_declarator_p))
13517 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13519 /* Reset the contents of the START_OF_ID token. */
13520 token->type = CPP_TEMPLATE_ID;
13521 /* Retrieve any deferred checks. Do not pop this access checks yet
13522 so the memory will not be reclaimed during token replacing below. */
13523 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13524 token->u.tree_check_value->value = template_id;
13525 token->u.tree_check_value->checks = get_deferred_access_checks ();
13526 token->keyword = RID_MAX;
13528 /* Purge all subsequent tokens. */
13529 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13531 /* ??? Can we actually assume that, if template_id ==
13532 error_mark_node, we will have issued a diagnostic to the
13533 user, as opposed to simply marking the tentative parse as
13534 failed? */
13535 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13536 error_at (token->location, "parse error in template argument list");
13539 pop_to_parent_deferring_access_checks ();
13540 return template_id;
13543 /* Parse a template-name.
13545 template-name:
13546 identifier
13548 The standard should actually say:
13550 template-name:
13551 identifier
13552 operator-function-id
13554 A defect report has been filed about this issue.
13556 A conversion-function-id cannot be a template name because they cannot
13557 be part of a template-id. In fact, looking at this code:
13559 a.operator K<int>()
13561 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13562 It is impossible to call a templated conversion-function-id with an
13563 explicit argument list, since the only allowed template parameter is
13564 the type to which it is converting.
13566 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13567 `template' keyword, in a construction like:
13569 T::template f<3>()
13571 In that case `f' is taken to be a template-name, even though there
13572 is no way of knowing for sure.
13574 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13575 name refers to a set of overloaded functions, at least one of which
13576 is a template, or an IDENTIFIER_NODE with the name of the template,
13577 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13578 names are looked up inside uninstantiated templates. */
13580 static tree
13581 cp_parser_template_name (cp_parser* parser,
13582 bool template_keyword_p,
13583 bool check_dependency_p,
13584 bool is_declaration,
13585 enum tag_types tag_type,
13586 bool *is_identifier)
13588 tree identifier;
13589 tree decl;
13590 tree fns;
13591 cp_token *token = cp_lexer_peek_token (parser->lexer);
13593 /* If the next token is `operator', then we have either an
13594 operator-function-id or a conversion-function-id. */
13595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13597 /* We don't know whether we're looking at an
13598 operator-function-id or a conversion-function-id. */
13599 cp_parser_parse_tentatively (parser);
13600 /* Try an operator-function-id. */
13601 identifier = cp_parser_operator_function_id (parser);
13602 /* If that didn't work, try a conversion-function-id. */
13603 if (!cp_parser_parse_definitely (parser))
13605 cp_parser_error (parser, "expected template-name");
13606 return error_mark_node;
13609 /* Look for the identifier. */
13610 else
13611 identifier = cp_parser_identifier (parser);
13613 /* If we didn't find an identifier, we don't have a template-id. */
13614 if (identifier == error_mark_node)
13615 return error_mark_node;
13617 /* If the name immediately followed the `template' keyword, then it
13618 is a template-name. However, if the next token is not `<', then
13619 we do not treat it as a template-name, since it is not being used
13620 as part of a template-id. This enables us to handle constructs
13621 like:
13623 template <typename T> struct S { S(); };
13624 template <typename T> S<T>::S();
13626 correctly. We would treat `S' as a template -- if it were `S<T>'
13627 -- but we do not if there is no `<'. */
13629 if (processing_template_decl
13630 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13632 /* In a declaration, in a dependent context, we pretend that the
13633 "template" keyword was present in order to improve error
13634 recovery. For example, given:
13636 template <typename T> void f(T::X<int>);
13638 we want to treat "X<int>" as a template-id. */
13639 if (is_declaration
13640 && !template_keyword_p
13641 && parser->scope && TYPE_P (parser->scope)
13642 && check_dependency_p
13643 && dependent_scope_p (parser->scope)
13644 /* Do not do this for dtors (or ctors), since they never
13645 need the template keyword before their name. */
13646 && !constructor_name_p (identifier, parser->scope))
13648 cp_token_position start = 0;
13650 /* Explain what went wrong. */
13651 error_at (token->location, "non-template %qD used as template",
13652 identifier);
13653 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13654 parser->scope, identifier);
13655 /* If parsing tentatively, find the location of the "<" token. */
13656 if (cp_parser_simulate_error (parser))
13657 start = cp_lexer_token_position (parser->lexer, true);
13658 /* Parse the template arguments so that we can issue error
13659 messages about them. */
13660 cp_lexer_consume_token (parser->lexer);
13661 cp_parser_enclosed_template_argument_list (parser);
13662 /* Skip tokens until we find a good place from which to
13663 continue parsing. */
13664 cp_parser_skip_to_closing_parenthesis (parser,
13665 /*recovering=*/true,
13666 /*or_comma=*/true,
13667 /*consume_paren=*/false);
13668 /* If parsing tentatively, permanently remove the
13669 template argument list. That will prevent duplicate
13670 error messages from being issued about the missing
13671 "template" keyword. */
13672 if (start)
13673 cp_lexer_purge_tokens_after (parser->lexer, start);
13674 if (is_identifier)
13675 *is_identifier = true;
13676 return identifier;
13679 /* If the "template" keyword is present, then there is generally
13680 no point in doing name-lookup, so we just return IDENTIFIER.
13681 But, if the qualifying scope is non-dependent then we can
13682 (and must) do name-lookup normally. */
13683 if (template_keyword_p
13684 && (!parser->scope
13685 || (TYPE_P (parser->scope)
13686 && dependent_type_p (parser->scope))))
13687 return identifier;
13690 /* Look up the name. */
13691 decl = cp_parser_lookup_name (parser, identifier,
13692 tag_type,
13693 /*is_template=*/true,
13694 /*is_namespace=*/false,
13695 check_dependency_p,
13696 /*ambiguous_decls=*/NULL,
13697 token->location);
13699 /* If DECL is a template, then the name was a template-name. */
13700 if (TREE_CODE (decl) == TEMPLATE_DECL)
13702 else
13704 tree fn = NULL_TREE;
13706 /* The standard does not explicitly indicate whether a name that
13707 names a set of overloaded declarations, some of which are
13708 templates, is a template-name. However, such a name should
13709 be a template-name; otherwise, there is no way to form a
13710 template-id for the overloaded templates. */
13711 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13712 if (TREE_CODE (fns) == OVERLOAD)
13713 for (fn = fns; fn; fn = OVL_NEXT (fn))
13714 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13715 break;
13717 if (!fn)
13719 /* The name does not name a template. */
13720 cp_parser_error (parser, "expected template-name");
13721 return error_mark_node;
13725 /* If DECL is dependent, and refers to a function, then just return
13726 its name; we will look it up again during template instantiation. */
13727 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13729 tree scope = ovl_scope (decl);
13730 if (TYPE_P (scope) && dependent_type_p (scope))
13731 return identifier;
13734 return decl;
13737 /* Parse a template-argument-list.
13739 template-argument-list:
13740 template-argument ... [opt]
13741 template-argument-list , template-argument ... [opt]
13743 Returns a TREE_VEC containing the arguments. */
13745 static tree
13746 cp_parser_template_argument_list (cp_parser* parser)
13748 tree fixed_args[10];
13749 unsigned n_args = 0;
13750 unsigned alloced = 10;
13751 tree *arg_ary = fixed_args;
13752 tree vec;
13753 bool saved_in_template_argument_list_p;
13754 bool saved_ice_p;
13755 bool saved_non_ice_p;
13757 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13758 parser->in_template_argument_list_p = true;
13759 /* Even if the template-id appears in an integral
13760 constant-expression, the contents of the argument list do
13761 not. */
13762 saved_ice_p = parser->integral_constant_expression_p;
13763 parser->integral_constant_expression_p = false;
13764 saved_non_ice_p = parser->non_integral_constant_expression_p;
13765 parser->non_integral_constant_expression_p = false;
13767 /* Parse the arguments. */
13770 tree argument;
13772 if (n_args)
13773 /* Consume the comma. */
13774 cp_lexer_consume_token (parser->lexer);
13776 /* Parse the template-argument. */
13777 argument = cp_parser_template_argument (parser);
13779 /* If the next token is an ellipsis, we're expanding a template
13780 argument pack. */
13781 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13783 if (argument == error_mark_node)
13785 cp_token *token = cp_lexer_peek_token (parser->lexer);
13786 error_at (token->location,
13787 "expected parameter pack before %<...%>");
13789 /* Consume the `...' token. */
13790 cp_lexer_consume_token (parser->lexer);
13792 /* Make the argument into a TYPE_PACK_EXPANSION or
13793 EXPR_PACK_EXPANSION. */
13794 argument = make_pack_expansion (argument);
13797 if (n_args == alloced)
13799 alloced *= 2;
13801 if (arg_ary == fixed_args)
13803 arg_ary = XNEWVEC (tree, alloced);
13804 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13806 else
13807 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13809 arg_ary[n_args++] = argument;
13811 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13813 vec = make_tree_vec (n_args);
13815 while (n_args--)
13816 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13818 if (arg_ary != fixed_args)
13819 free (arg_ary);
13820 parser->non_integral_constant_expression_p = saved_non_ice_p;
13821 parser->integral_constant_expression_p = saved_ice_p;
13822 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13823 #ifdef ENABLE_CHECKING
13824 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13825 #endif
13826 return vec;
13829 /* Parse a template-argument.
13831 template-argument:
13832 assignment-expression
13833 type-id
13834 id-expression
13836 The representation is that of an assignment-expression, type-id, or
13837 id-expression -- except that the qualified id-expression is
13838 evaluated, so that the value returned is either a DECL or an
13839 OVERLOAD.
13841 Although the standard says "assignment-expression", it forbids
13842 throw-expressions or assignments in the template argument.
13843 Therefore, we use "conditional-expression" instead. */
13845 static tree
13846 cp_parser_template_argument (cp_parser* parser)
13848 tree argument;
13849 bool template_p;
13850 bool address_p;
13851 bool maybe_type_id = false;
13852 cp_token *token = NULL, *argument_start_token = NULL;
13853 location_t loc = 0;
13854 cp_id_kind idk;
13856 /* There's really no way to know what we're looking at, so we just
13857 try each alternative in order.
13859 [temp.arg]
13861 In a template-argument, an ambiguity between a type-id and an
13862 expression is resolved to a type-id, regardless of the form of
13863 the corresponding template-parameter.
13865 Therefore, we try a type-id first. */
13866 cp_parser_parse_tentatively (parser);
13867 argument = cp_parser_template_type_arg (parser);
13868 /* If there was no error parsing the type-id but the next token is a
13869 '>>', our behavior depends on which dialect of C++ we're
13870 parsing. In C++98, we probably found a typo for '> >'. But there
13871 are type-id which are also valid expressions. For instance:
13873 struct X { int operator >> (int); };
13874 template <int V> struct Foo {};
13875 Foo<X () >> 5> r;
13877 Here 'X()' is a valid type-id of a function type, but the user just
13878 wanted to write the expression "X() >> 5". Thus, we remember that we
13879 found a valid type-id, but we still try to parse the argument as an
13880 expression to see what happens.
13882 In C++0x, the '>>' will be considered two separate '>'
13883 tokens. */
13884 if (!cp_parser_error_occurred (parser)
13885 && cxx_dialect == cxx98
13886 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13888 maybe_type_id = true;
13889 cp_parser_abort_tentative_parse (parser);
13891 else
13893 /* If the next token isn't a `,' or a `>', then this argument wasn't
13894 really finished. This means that the argument is not a valid
13895 type-id. */
13896 if (!cp_parser_next_token_ends_template_argument_p (parser))
13897 cp_parser_error (parser, "expected template-argument");
13898 /* If that worked, we're done. */
13899 if (cp_parser_parse_definitely (parser))
13900 return argument;
13902 /* We're still not sure what the argument will be. */
13903 cp_parser_parse_tentatively (parser);
13904 /* Try a template. */
13905 argument_start_token = cp_lexer_peek_token (parser->lexer);
13906 argument = cp_parser_id_expression (parser,
13907 /*template_keyword_p=*/false,
13908 /*check_dependency_p=*/true,
13909 &template_p,
13910 /*declarator_p=*/false,
13911 /*optional_p=*/false);
13912 /* If the next token isn't a `,' or a `>', then this argument wasn't
13913 really finished. */
13914 if (!cp_parser_next_token_ends_template_argument_p (parser))
13915 cp_parser_error (parser, "expected template-argument");
13916 if (!cp_parser_error_occurred (parser))
13918 /* Figure out what is being referred to. If the id-expression
13919 was for a class template specialization, then we will have a
13920 TYPE_DECL at this point. There is no need to do name lookup
13921 at this point in that case. */
13922 if (TREE_CODE (argument) != TYPE_DECL)
13923 argument = cp_parser_lookup_name (parser, argument,
13924 none_type,
13925 /*is_template=*/template_p,
13926 /*is_namespace=*/false,
13927 /*check_dependency=*/true,
13928 /*ambiguous_decls=*/NULL,
13929 argument_start_token->location);
13930 if (TREE_CODE (argument) != TEMPLATE_DECL
13931 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13932 cp_parser_error (parser, "expected template-name");
13934 if (cp_parser_parse_definitely (parser))
13935 return argument;
13936 /* It must be a non-type argument. There permitted cases are given
13937 in [temp.arg.nontype]:
13939 -- an integral constant-expression of integral or enumeration
13940 type; or
13942 -- the name of a non-type template-parameter; or
13944 -- the name of an object or function with external linkage...
13946 -- the address of an object or function with external linkage...
13948 -- a pointer to member... */
13949 /* Look for a non-type template parameter. */
13950 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13952 cp_parser_parse_tentatively (parser);
13953 argument = cp_parser_primary_expression (parser,
13954 /*address_p=*/false,
13955 /*cast_p=*/false,
13956 /*template_arg_p=*/true,
13957 &idk);
13958 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13959 || !cp_parser_next_token_ends_template_argument_p (parser))
13960 cp_parser_simulate_error (parser);
13961 if (cp_parser_parse_definitely (parser))
13962 return argument;
13965 /* If the next token is "&", the argument must be the address of an
13966 object or function with external linkage. */
13967 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13968 if (address_p)
13970 loc = cp_lexer_peek_token (parser->lexer)->location;
13971 cp_lexer_consume_token (parser->lexer);
13973 /* See if we might have an id-expression. */
13974 token = cp_lexer_peek_token (parser->lexer);
13975 if (token->type == CPP_NAME
13976 || token->keyword == RID_OPERATOR
13977 || token->type == CPP_SCOPE
13978 || token->type == CPP_TEMPLATE_ID
13979 || token->type == CPP_NESTED_NAME_SPECIFIER)
13981 cp_parser_parse_tentatively (parser);
13982 argument = cp_parser_primary_expression (parser,
13983 address_p,
13984 /*cast_p=*/false,
13985 /*template_arg_p=*/true,
13986 &idk);
13987 if (cp_parser_error_occurred (parser)
13988 || !cp_parser_next_token_ends_template_argument_p (parser))
13989 cp_parser_abort_tentative_parse (parser);
13990 else
13992 tree probe;
13994 if (INDIRECT_REF_P (argument))
13996 /* Strip the dereference temporarily. */
13997 gcc_assert (REFERENCE_REF_P (argument));
13998 argument = TREE_OPERAND (argument, 0);
14001 /* If we're in a template, we represent a qualified-id referring
14002 to a static data member as a SCOPE_REF even if the scope isn't
14003 dependent so that we can check access control later. */
14004 probe = argument;
14005 if (TREE_CODE (probe) == SCOPE_REF)
14006 probe = TREE_OPERAND (probe, 1);
14007 if (VAR_P (probe))
14009 /* A variable without external linkage might still be a
14010 valid constant-expression, so no error is issued here
14011 if the external-linkage check fails. */
14012 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14013 cp_parser_simulate_error (parser);
14015 else if (is_overloaded_fn (argument))
14016 /* All overloaded functions are allowed; if the external
14017 linkage test does not pass, an error will be issued
14018 later. */
14020 else if (address_p
14021 && (TREE_CODE (argument) == OFFSET_REF
14022 || TREE_CODE (argument) == SCOPE_REF))
14023 /* A pointer-to-member. */
14025 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14027 else
14028 cp_parser_simulate_error (parser);
14030 if (cp_parser_parse_definitely (parser))
14032 if (address_p)
14033 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14034 tf_warning_or_error);
14035 else
14036 argument = convert_from_reference (argument);
14037 return argument;
14041 /* If the argument started with "&", there are no other valid
14042 alternatives at this point. */
14043 if (address_p)
14045 cp_parser_error (parser, "invalid non-type template argument");
14046 return error_mark_node;
14049 /* If the argument wasn't successfully parsed as a type-id followed
14050 by '>>', the argument can only be a constant expression now.
14051 Otherwise, we try parsing the constant-expression tentatively,
14052 because the argument could really be a type-id. */
14053 if (maybe_type_id)
14054 cp_parser_parse_tentatively (parser);
14055 argument = cp_parser_constant_expression (parser,
14056 /*allow_non_constant_p=*/false,
14057 /*non_constant_p=*/NULL);
14058 if (!maybe_type_id)
14059 return argument;
14060 if (!cp_parser_next_token_ends_template_argument_p (parser))
14061 cp_parser_error (parser, "expected template-argument");
14062 if (cp_parser_parse_definitely (parser))
14063 return argument;
14064 /* We did our best to parse the argument as a non type-id, but that
14065 was the only alternative that matched (albeit with a '>' after
14066 it). We can assume it's just a typo from the user, and a
14067 diagnostic will then be issued. */
14068 return cp_parser_template_type_arg (parser);
14071 /* Parse an explicit-instantiation.
14073 explicit-instantiation:
14074 template declaration
14076 Although the standard says `declaration', what it really means is:
14078 explicit-instantiation:
14079 template decl-specifier-seq [opt] declarator [opt] ;
14081 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14082 supposed to be allowed. A defect report has been filed about this
14083 issue.
14085 GNU Extension:
14087 explicit-instantiation:
14088 storage-class-specifier template
14089 decl-specifier-seq [opt] declarator [opt] ;
14090 function-specifier template
14091 decl-specifier-seq [opt] declarator [opt] ; */
14093 static void
14094 cp_parser_explicit_instantiation (cp_parser* parser)
14096 int declares_class_or_enum;
14097 cp_decl_specifier_seq decl_specifiers;
14098 tree extension_specifier = NULL_TREE;
14100 timevar_push (TV_TEMPLATE_INST);
14102 /* Look for an (optional) storage-class-specifier or
14103 function-specifier. */
14104 if (cp_parser_allow_gnu_extensions_p (parser))
14106 extension_specifier
14107 = cp_parser_storage_class_specifier_opt (parser);
14108 if (!extension_specifier)
14109 extension_specifier
14110 = cp_parser_function_specifier_opt (parser,
14111 /*decl_specs=*/NULL);
14114 /* Look for the `template' keyword. */
14115 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14116 /* Let the front end know that we are processing an explicit
14117 instantiation. */
14118 begin_explicit_instantiation ();
14119 /* [temp.explicit] says that we are supposed to ignore access
14120 control while processing explicit instantiation directives. */
14121 push_deferring_access_checks (dk_no_check);
14122 /* Parse a decl-specifier-seq. */
14123 cp_parser_decl_specifier_seq (parser,
14124 CP_PARSER_FLAGS_OPTIONAL,
14125 &decl_specifiers,
14126 &declares_class_or_enum);
14127 /* If there was exactly one decl-specifier, and it declared a class,
14128 and there's no declarator, then we have an explicit type
14129 instantiation. */
14130 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14132 tree type;
14134 type = check_tag_decl (&decl_specifiers,
14135 /*explicit_type_instantiation_p=*/true);
14136 /* Turn access control back on for names used during
14137 template instantiation. */
14138 pop_deferring_access_checks ();
14139 if (type)
14140 do_type_instantiation (type, extension_specifier,
14141 /*complain=*/tf_error);
14143 else
14145 cp_declarator *declarator;
14146 tree decl;
14148 /* Parse the declarator. */
14149 declarator
14150 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14151 /*ctor_dtor_or_conv_p=*/NULL,
14152 /*parenthesized_p=*/NULL,
14153 /*member_p=*/false);
14154 if (declares_class_or_enum & 2)
14155 cp_parser_check_for_definition_in_return_type (declarator,
14156 decl_specifiers.type,
14157 decl_specifiers.locations[ds_type_spec]);
14158 if (declarator != cp_error_declarator)
14160 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14161 permerror (decl_specifiers.locations[ds_inline],
14162 "explicit instantiation shall not use"
14163 " %<inline%> specifier");
14164 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14165 permerror (decl_specifiers.locations[ds_constexpr],
14166 "explicit instantiation shall not use"
14167 " %<constexpr%> specifier");
14169 decl = grokdeclarator (declarator, &decl_specifiers,
14170 NORMAL, 0, &decl_specifiers.attributes);
14171 /* Turn access control back on for names used during
14172 template instantiation. */
14173 pop_deferring_access_checks ();
14174 /* Do the explicit instantiation. */
14175 do_decl_instantiation (decl, extension_specifier);
14177 else
14179 pop_deferring_access_checks ();
14180 /* Skip the body of the explicit instantiation. */
14181 cp_parser_skip_to_end_of_statement (parser);
14184 /* We're done with the instantiation. */
14185 end_explicit_instantiation ();
14187 cp_parser_consume_semicolon_at_end_of_statement (parser);
14189 timevar_pop (TV_TEMPLATE_INST);
14192 /* Parse an explicit-specialization.
14194 explicit-specialization:
14195 template < > declaration
14197 Although the standard says `declaration', what it really means is:
14199 explicit-specialization:
14200 template <> decl-specifier [opt] init-declarator [opt] ;
14201 template <> function-definition
14202 template <> explicit-specialization
14203 template <> template-declaration */
14205 static void
14206 cp_parser_explicit_specialization (cp_parser* parser)
14208 bool need_lang_pop;
14209 cp_token *token = cp_lexer_peek_token (parser->lexer);
14211 /* Look for the `template' keyword. */
14212 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14213 /* Look for the `<'. */
14214 cp_parser_require (parser, CPP_LESS, RT_LESS);
14215 /* Look for the `>'. */
14216 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14217 /* We have processed another parameter list. */
14218 ++parser->num_template_parameter_lists;
14219 /* [temp]
14221 A template ... explicit specialization ... shall not have C
14222 linkage. */
14223 if (current_lang_name == lang_name_c)
14225 error_at (token->location, "template specialization with C linkage");
14226 /* Give it C++ linkage to avoid confusing other parts of the
14227 front end. */
14228 push_lang_context (lang_name_cplusplus);
14229 need_lang_pop = true;
14231 else
14232 need_lang_pop = false;
14233 /* Let the front end know that we are beginning a specialization. */
14234 if (!begin_specialization ())
14236 end_specialization ();
14237 return;
14240 /* If the next keyword is `template', we need to figure out whether
14241 or not we're looking a template-declaration. */
14242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14244 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14245 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14246 cp_parser_template_declaration_after_export (parser,
14247 /*member_p=*/false);
14248 else
14249 cp_parser_explicit_specialization (parser);
14251 else
14252 /* Parse the dependent declaration. */
14253 cp_parser_single_declaration (parser,
14254 /*checks=*/NULL,
14255 /*member_p=*/false,
14256 /*explicit_specialization_p=*/true,
14257 /*friend_p=*/NULL);
14258 /* We're done with the specialization. */
14259 end_specialization ();
14260 /* For the erroneous case of a template with C linkage, we pushed an
14261 implicit C++ linkage scope; exit that scope now. */
14262 if (need_lang_pop)
14263 pop_lang_context ();
14264 /* We're done with this parameter list. */
14265 --parser->num_template_parameter_lists;
14268 /* Parse a type-specifier.
14270 type-specifier:
14271 simple-type-specifier
14272 class-specifier
14273 enum-specifier
14274 elaborated-type-specifier
14275 cv-qualifier
14277 GNU Extension:
14279 type-specifier:
14280 __complex__
14282 Returns a representation of the type-specifier. For a
14283 class-specifier, enum-specifier, or elaborated-type-specifier, a
14284 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14286 The parser flags FLAGS is used to control type-specifier parsing.
14288 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14289 in a decl-specifier-seq.
14291 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14292 class-specifier, enum-specifier, or elaborated-type-specifier, then
14293 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14294 if a type is declared; 2 if it is defined. Otherwise, it is set to
14295 zero.
14297 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14298 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14299 is set to FALSE. */
14301 static tree
14302 cp_parser_type_specifier (cp_parser* parser,
14303 cp_parser_flags flags,
14304 cp_decl_specifier_seq *decl_specs,
14305 bool is_declaration,
14306 int* declares_class_or_enum,
14307 bool* is_cv_qualifier)
14309 tree type_spec = NULL_TREE;
14310 cp_token *token;
14311 enum rid keyword;
14312 cp_decl_spec ds = ds_last;
14314 /* Assume this type-specifier does not declare a new type. */
14315 if (declares_class_or_enum)
14316 *declares_class_or_enum = 0;
14317 /* And that it does not specify a cv-qualifier. */
14318 if (is_cv_qualifier)
14319 *is_cv_qualifier = false;
14320 /* Peek at the next token. */
14321 token = cp_lexer_peek_token (parser->lexer);
14323 /* If we're looking at a keyword, we can use that to guide the
14324 production we choose. */
14325 keyword = token->keyword;
14326 switch (keyword)
14328 case RID_ENUM:
14329 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14330 goto elaborated_type_specifier;
14332 /* Look for the enum-specifier. */
14333 type_spec = cp_parser_enum_specifier (parser);
14334 /* If that worked, we're done. */
14335 if (type_spec)
14337 if (declares_class_or_enum)
14338 *declares_class_or_enum = 2;
14339 if (decl_specs)
14340 cp_parser_set_decl_spec_type (decl_specs,
14341 type_spec,
14342 token,
14343 /*type_definition_p=*/true);
14344 return type_spec;
14346 else
14347 goto elaborated_type_specifier;
14349 /* Any of these indicate either a class-specifier, or an
14350 elaborated-type-specifier. */
14351 case RID_CLASS:
14352 case RID_STRUCT:
14353 case RID_UNION:
14354 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14355 goto elaborated_type_specifier;
14357 /* Parse tentatively so that we can back up if we don't find a
14358 class-specifier. */
14359 cp_parser_parse_tentatively (parser);
14360 /* Look for the class-specifier. */
14361 type_spec = cp_parser_class_specifier (parser);
14362 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14363 /* If that worked, we're done. */
14364 if (cp_parser_parse_definitely (parser))
14366 if (declares_class_or_enum)
14367 *declares_class_or_enum = 2;
14368 if (decl_specs)
14369 cp_parser_set_decl_spec_type (decl_specs,
14370 type_spec,
14371 token,
14372 /*type_definition_p=*/true);
14373 return type_spec;
14376 /* Fall through. */
14377 elaborated_type_specifier:
14378 /* We're declaring (not defining) a class or enum. */
14379 if (declares_class_or_enum)
14380 *declares_class_or_enum = 1;
14382 /* Fall through. */
14383 case RID_TYPENAME:
14384 /* Look for an elaborated-type-specifier. */
14385 type_spec
14386 = (cp_parser_elaborated_type_specifier
14387 (parser,
14388 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14389 is_declaration));
14390 if (decl_specs)
14391 cp_parser_set_decl_spec_type (decl_specs,
14392 type_spec,
14393 token,
14394 /*type_definition_p=*/false);
14395 return type_spec;
14397 case RID_CONST:
14398 ds = ds_const;
14399 if (is_cv_qualifier)
14400 *is_cv_qualifier = true;
14401 break;
14403 case RID_VOLATILE:
14404 ds = ds_volatile;
14405 if (is_cv_qualifier)
14406 *is_cv_qualifier = true;
14407 break;
14409 case RID_RESTRICT:
14410 ds = ds_restrict;
14411 if (is_cv_qualifier)
14412 *is_cv_qualifier = true;
14413 break;
14415 case RID_COMPLEX:
14416 /* The `__complex__' keyword is a GNU extension. */
14417 ds = ds_complex;
14418 break;
14420 default:
14421 break;
14424 /* Handle simple keywords. */
14425 if (ds != ds_last)
14427 if (decl_specs)
14429 set_and_check_decl_spec_loc (decl_specs, ds, token);
14430 decl_specs->any_specifiers_p = true;
14432 return cp_lexer_consume_token (parser->lexer)->u.value;
14435 /* If we do not already have a type-specifier, assume we are looking
14436 at a simple-type-specifier. */
14437 type_spec = cp_parser_simple_type_specifier (parser,
14438 decl_specs,
14439 flags);
14441 /* If we didn't find a type-specifier, and a type-specifier was not
14442 optional in this context, issue an error message. */
14443 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14445 cp_parser_error (parser, "expected type specifier");
14446 return error_mark_node;
14449 return type_spec;
14452 /* Parse a simple-type-specifier.
14454 simple-type-specifier:
14455 :: [opt] nested-name-specifier [opt] type-name
14456 :: [opt] nested-name-specifier template template-id
14457 char
14458 wchar_t
14459 bool
14460 short
14462 long
14463 signed
14464 unsigned
14465 float
14466 double
14467 void
14469 C++0x Extension:
14471 simple-type-specifier:
14472 auto
14473 decltype ( expression )
14474 char16_t
14475 char32_t
14476 __underlying_type ( type-id )
14478 GNU Extension:
14480 simple-type-specifier:
14481 __int128
14482 __typeof__ unary-expression
14483 __typeof__ ( type-id )
14484 __typeof__ ( type-id ) { initializer-list , [opt] }
14486 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14487 appropriately updated. */
14489 static tree
14490 cp_parser_simple_type_specifier (cp_parser* parser,
14491 cp_decl_specifier_seq *decl_specs,
14492 cp_parser_flags flags)
14494 tree type = NULL_TREE;
14495 cp_token *token;
14497 /* Peek at the next token. */
14498 token = cp_lexer_peek_token (parser->lexer);
14500 /* If we're looking at a keyword, things are easy. */
14501 switch (token->keyword)
14503 case RID_CHAR:
14504 if (decl_specs)
14505 decl_specs->explicit_char_p = true;
14506 type = char_type_node;
14507 break;
14508 case RID_CHAR16:
14509 type = char16_type_node;
14510 break;
14511 case RID_CHAR32:
14512 type = char32_type_node;
14513 break;
14514 case RID_WCHAR:
14515 type = wchar_type_node;
14516 break;
14517 case RID_BOOL:
14518 type = boolean_type_node;
14519 break;
14520 case RID_SHORT:
14521 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14522 type = short_integer_type_node;
14523 break;
14524 case RID_INT:
14525 if (decl_specs)
14526 decl_specs->explicit_int_p = true;
14527 type = integer_type_node;
14528 break;
14529 case RID_INT128:
14530 if (!int128_integer_type_node)
14531 break;
14532 if (decl_specs)
14533 decl_specs->explicit_int128_p = true;
14534 type = int128_integer_type_node;
14535 break;
14536 case RID_LONG:
14537 if (decl_specs)
14538 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14539 type = long_integer_type_node;
14540 break;
14541 case RID_SIGNED:
14542 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14543 type = integer_type_node;
14544 break;
14545 case RID_UNSIGNED:
14546 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14547 type = unsigned_type_node;
14548 break;
14549 case RID_FLOAT:
14550 type = float_type_node;
14551 break;
14552 case RID_DOUBLE:
14553 type = double_type_node;
14554 break;
14555 case RID_VOID:
14556 type = void_type_node;
14557 break;
14559 case RID_AUTO:
14560 maybe_warn_cpp0x (CPP0X_AUTO);
14561 if (parser->auto_is_implicit_function_template_parm_p)
14563 type = synthesize_implicit_template_parm (parser);
14565 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14567 if (cxx_dialect < cxx1y)
14568 pedwarn (location_of (type), 0,
14569 "use of %<auto%> in lambda parameter declaration "
14570 "only available with "
14571 "-std=c++1y or -std=gnu++1y");
14573 else if (cxx_dialect < cxx1y)
14574 pedwarn (location_of (type), 0,
14575 "use of %<auto%> in parameter declaration "
14576 "only available with "
14577 "-std=c++1y or -std=gnu++1y");
14578 else
14579 pedwarn (location_of (type), OPT_Wpedantic,
14580 "ISO C++ forbids use of %<auto%> in parameter "
14581 "declaration");
14583 else
14584 type = make_auto ();
14585 break;
14587 case RID_DECLTYPE:
14588 /* Since DR 743, decltype can either be a simple-type-specifier by
14589 itself or begin a nested-name-specifier. Parsing it will replace
14590 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14591 handling below decide what to do. */
14592 cp_parser_decltype (parser);
14593 cp_lexer_set_token_position (parser->lexer, token);
14594 break;
14596 case RID_TYPEOF:
14597 /* Consume the `typeof' token. */
14598 cp_lexer_consume_token (parser->lexer);
14599 /* Parse the operand to `typeof'. */
14600 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14601 /* If it is not already a TYPE, take its type. */
14602 if (!TYPE_P (type))
14603 type = finish_typeof (type);
14605 if (decl_specs)
14606 cp_parser_set_decl_spec_type (decl_specs, type,
14607 token,
14608 /*type_definition_p=*/false);
14610 return type;
14612 case RID_UNDERLYING_TYPE:
14613 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14614 if (decl_specs)
14615 cp_parser_set_decl_spec_type (decl_specs, type,
14616 token,
14617 /*type_definition_p=*/false);
14619 return type;
14621 case RID_BASES:
14622 case RID_DIRECT_BASES:
14623 type = cp_parser_trait_expr (parser, token->keyword);
14624 if (decl_specs)
14625 cp_parser_set_decl_spec_type (decl_specs, type,
14626 token,
14627 /*type_definition_p=*/false);
14628 return type;
14629 default:
14630 break;
14633 /* If token is an already-parsed decltype not followed by ::,
14634 it's a simple-type-specifier. */
14635 if (token->type == CPP_DECLTYPE
14636 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14638 type = token->u.value;
14639 if (decl_specs)
14640 cp_parser_set_decl_spec_type (decl_specs, type,
14641 token,
14642 /*type_definition_p=*/false);
14643 cp_lexer_consume_token (parser->lexer);
14644 return type;
14647 /* If the type-specifier was for a built-in type, we're done. */
14648 if (type)
14650 /* Record the type. */
14651 if (decl_specs
14652 && (token->keyword != RID_SIGNED
14653 && token->keyword != RID_UNSIGNED
14654 && token->keyword != RID_SHORT
14655 && token->keyword != RID_LONG))
14656 cp_parser_set_decl_spec_type (decl_specs,
14657 type,
14658 token,
14659 /*type_definition_p=*/false);
14660 if (decl_specs)
14661 decl_specs->any_specifiers_p = true;
14663 /* Consume the token. */
14664 cp_lexer_consume_token (parser->lexer);
14666 /* There is no valid C++ program where a non-template type is
14667 followed by a "<". That usually indicates that the user thought
14668 that the type was a template. */
14669 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14670 token->location);
14672 return TYPE_NAME (type);
14675 /* The type-specifier must be a user-defined type. */
14676 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14678 bool qualified_p;
14679 bool global_p;
14681 /* Don't gobble tokens or issue error messages if this is an
14682 optional type-specifier. */
14683 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14684 cp_parser_parse_tentatively (parser);
14686 /* Look for the optional `::' operator. */
14687 global_p
14688 = (cp_parser_global_scope_opt (parser,
14689 /*current_scope_valid_p=*/false)
14690 != NULL_TREE);
14691 /* Look for the nested-name specifier. */
14692 qualified_p
14693 = (cp_parser_nested_name_specifier_opt (parser,
14694 /*typename_keyword_p=*/false,
14695 /*check_dependency_p=*/true,
14696 /*type_p=*/false,
14697 /*is_declaration=*/false)
14698 != NULL_TREE);
14699 token = cp_lexer_peek_token (parser->lexer);
14700 /* If we have seen a nested-name-specifier, and the next token
14701 is `template', then we are using the template-id production. */
14702 if (parser->scope
14703 && cp_parser_optional_template_keyword (parser))
14705 /* Look for the template-id. */
14706 type = cp_parser_template_id (parser,
14707 /*template_keyword_p=*/true,
14708 /*check_dependency_p=*/true,
14709 none_type,
14710 /*is_declaration=*/false);
14711 /* If the template-id did not name a type, we are out of
14712 luck. */
14713 if (TREE_CODE (type) != TYPE_DECL)
14715 cp_parser_error (parser, "expected template-id for type");
14716 type = NULL_TREE;
14719 /* Otherwise, look for a type-name. */
14720 else
14721 type = cp_parser_type_name (parser);
14722 /* Keep track of all name-lookups performed in class scopes. */
14723 if (type
14724 && !global_p
14725 && !qualified_p
14726 && TREE_CODE (type) == TYPE_DECL
14727 && identifier_p (DECL_NAME (type)))
14728 maybe_note_name_used_in_class (DECL_NAME (type), type);
14729 /* If it didn't work out, we don't have a TYPE. */
14730 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14731 && !cp_parser_parse_definitely (parser))
14732 type = NULL_TREE;
14733 if (type && decl_specs)
14734 cp_parser_set_decl_spec_type (decl_specs, type,
14735 token,
14736 /*type_definition_p=*/false);
14739 /* If we didn't get a type-name, issue an error message. */
14740 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14742 cp_parser_error (parser, "expected type-name");
14743 return error_mark_node;
14746 if (type && type != error_mark_node)
14748 /* See if TYPE is an Objective-C type, and if so, parse and
14749 accept any protocol references following it. Do this before
14750 the cp_parser_check_for_invalid_template_id() call, because
14751 Objective-C types can be followed by '<...>' which would
14752 enclose protocol names rather than template arguments, and so
14753 everything is fine. */
14754 if (c_dialect_objc () && !parser->scope
14755 && (objc_is_id (type) || objc_is_class_name (type)))
14757 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14758 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14760 /* Clobber the "unqualified" type previously entered into
14761 DECL_SPECS with the new, improved protocol-qualified version. */
14762 if (decl_specs)
14763 decl_specs->type = qual_type;
14765 return qual_type;
14768 /* There is no valid C++ program where a non-template type is
14769 followed by a "<". That usually indicates that the user
14770 thought that the type was a template. */
14771 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14772 none_type,
14773 token->location);
14776 return type;
14779 /* Parse a type-name.
14781 type-name:
14782 class-name
14783 enum-name
14784 typedef-name
14785 simple-template-id [in c++0x]
14787 enum-name:
14788 identifier
14790 typedef-name:
14791 identifier
14793 Returns a TYPE_DECL for the type. */
14795 static tree
14796 cp_parser_type_name (cp_parser* parser)
14798 tree type_decl;
14800 /* We can't know yet whether it is a class-name or not. */
14801 cp_parser_parse_tentatively (parser);
14802 /* Try a class-name. */
14803 type_decl = cp_parser_class_name (parser,
14804 /*typename_keyword_p=*/false,
14805 /*template_keyword_p=*/false,
14806 none_type,
14807 /*check_dependency_p=*/true,
14808 /*class_head_p=*/false,
14809 /*is_declaration=*/false);
14810 /* If it's not a class-name, keep looking. */
14811 if (!cp_parser_parse_definitely (parser))
14813 if (cxx_dialect < cxx11)
14814 /* It must be a typedef-name or an enum-name. */
14815 return cp_parser_nonclass_name (parser);
14817 cp_parser_parse_tentatively (parser);
14818 /* It is either a simple-template-id representing an
14819 instantiation of an alias template... */
14820 type_decl = cp_parser_template_id (parser,
14821 /*template_keyword_p=*/false,
14822 /*check_dependency_p=*/true,
14823 none_type,
14824 /*is_declaration=*/false);
14825 /* Note that this must be an instantiation of an alias template
14826 because [temp.names]/6 says:
14828 A template-id that names an alias template specialization
14829 is a type-name.
14831 Whereas [temp.names]/7 says:
14833 A simple-template-id that names a class template
14834 specialization is a class-name. */
14835 if (type_decl != NULL_TREE
14836 && TREE_CODE (type_decl) == TYPE_DECL
14837 && TYPE_DECL_ALIAS_P (type_decl))
14838 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14839 else
14840 cp_parser_simulate_error (parser);
14842 if (!cp_parser_parse_definitely (parser))
14843 /* ... Or a typedef-name or an enum-name. */
14844 return cp_parser_nonclass_name (parser);
14847 return type_decl;
14850 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14852 enum-name:
14853 identifier
14855 typedef-name:
14856 identifier
14858 Returns a TYPE_DECL for the type. */
14860 static tree
14861 cp_parser_nonclass_name (cp_parser* parser)
14863 tree type_decl;
14864 tree identifier;
14866 cp_token *token = cp_lexer_peek_token (parser->lexer);
14867 identifier = cp_parser_identifier (parser);
14868 if (identifier == error_mark_node)
14869 return error_mark_node;
14871 /* Look up the type-name. */
14872 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14874 type_decl = strip_using_decl (type_decl);
14876 if (TREE_CODE (type_decl) != TYPE_DECL
14877 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14879 /* See if this is an Objective-C type. */
14880 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14881 tree type = objc_get_protocol_qualified_type (identifier, protos);
14882 if (type)
14883 type_decl = TYPE_NAME (type);
14886 /* Issue an error if we did not find a type-name. */
14887 if (TREE_CODE (type_decl) != TYPE_DECL
14888 /* In Objective-C, we have the complication that class names are
14889 normally type names and start declarations (eg, the
14890 "NSObject" in "NSObject *object;"), but can be used in an
14891 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14892 is an expression. So, a classname followed by a dot is not a
14893 valid type-name. */
14894 || (objc_is_class_name (TREE_TYPE (type_decl))
14895 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14897 if (!cp_parser_simulate_error (parser))
14898 cp_parser_name_lookup_error (parser, identifier, type_decl,
14899 NLE_TYPE, token->location);
14900 return error_mark_node;
14902 /* Remember that the name was used in the definition of the
14903 current class so that we can check later to see if the
14904 meaning would have been different after the class was
14905 entirely defined. */
14906 else if (type_decl != error_mark_node
14907 && !parser->scope)
14908 maybe_note_name_used_in_class (identifier, type_decl);
14910 return type_decl;
14913 /* Parse an elaborated-type-specifier. Note that the grammar given
14914 here incorporates the resolution to DR68.
14916 elaborated-type-specifier:
14917 class-key :: [opt] nested-name-specifier [opt] identifier
14918 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14919 enum-key :: [opt] nested-name-specifier [opt] identifier
14920 typename :: [opt] nested-name-specifier identifier
14921 typename :: [opt] nested-name-specifier template [opt]
14922 template-id
14924 GNU extension:
14926 elaborated-type-specifier:
14927 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14928 class-key attributes :: [opt] nested-name-specifier [opt]
14929 template [opt] template-id
14930 enum attributes :: [opt] nested-name-specifier [opt] identifier
14932 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14933 declared `friend'. If IS_DECLARATION is TRUE, then this
14934 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14935 something is being declared.
14937 Returns the TYPE specified. */
14939 static tree
14940 cp_parser_elaborated_type_specifier (cp_parser* parser,
14941 bool is_friend,
14942 bool is_declaration)
14944 enum tag_types tag_type;
14945 tree identifier;
14946 tree type = NULL_TREE;
14947 tree attributes = NULL_TREE;
14948 tree globalscope;
14949 cp_token *token = NULL;
14951 /* See if we're looking at the `enum' keyword. */
14952 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14954 /* Consume the `enum' token. */
14955 cp_lexer_consume_token (parser->lexer);
14956 /* Remember that it's an enumeration type. */
14957 tag_type = enum_type;
14958 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14959 enums) is used here. */
14960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14961 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14963 pedwarn (input_location, 0, "elaborated-type-specifier "
14964 "for a scoped enum must not use the %<%D%> keyword",
14965 cp_lexer_peek_token (parser->lexer)->u.value);
14966 /* Consume the `struct' or `class' and parse it anyway. */
14967 cp_lexer_consume_token (parser->lexer);
14969 /* Parse the attributes. */
14970 attributes = cp_parser_attributes_opt (parser);
14972 /* Or, it might be `typename'. */
14973 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14974 RID_TYPENAME))
14976 /* Consume the `typename' token. */
14977 cp_lexer_consume_token (parser->lexer);
14978 /* Remember that it's a `typename' type. */
14979 tag_type = typename_type;
14981 /* Otherwise it must be a class-key. */
14982 else
14984 tag_type = cp_parser_class_key (parser);
14985 if (tag_type == none_type)
14986 return error_mark_node;
14987 /* Parse the attributes. */
14988 attributes = cp_parser_attributes_opt (parser);
14991 /* Look for the `::' operator. */
14992 globalscope = cp_parser_global_scope_opt (parser,
14993 /*current_scope_valid_p=*/false);
14994 /* Look for the nested-name-specifier. */
14995 if (tag_type == typename_type && !globalscope)
14997 if (!cp_parser_nested_name_specifier (parser,
14998 /*typename_keyword_p=*/true,
14999 /*check_dependency_p=*/true,
15000 /*type_p=*/true,
15001 is_declaration))
15002 return error_mark_node;
15004 else
15005 /* Even though `typename' is not present, the proposed resolution
15006 to Core Issue 180 says that in `class A<T>::B', `B' should be
15007 considered a type-name, even if `A<T>' is dependent. */
15008 cp_parser_nested_name_specifier_opt (parser,
15009 /*typename_keyword_p=*/true,
15010 /*check_dependency_p=*/true,
15011 /*type_p=*/true,
15012 is_declaration);
15013 /* For everything but enumeration types, consider a template-id.
15014 For an enumeration type, consider only a plain identifier. */
15015 if (tag_type != enum_type)
15017 bool template_p = false;
15018 tree decl;
15020 /* Allow the `template' keyword. */
15021 template_p = cp_parser_optional_template_keyword (parser);
15022 /* If we didn't see `template', we don't know if there's a
15023 template-id or not. */
15024 if (!template_p)
15025 cp_parser_parse_tentatively (parser);
15026 /* Parse the template-id. */
15027 token = cp_lexer_peek_token (parser->lexer);
15028 decl = cp_parser_template_id (parser, template_p,
15029 /*check_dependency_p=*/true,
15030 tag_type,
15031 is_declaration);
15032 /* If we didn't find a template-id, look for an ordinary
15033 identifier. */
15034 if (!template_p && !cp_parser_parse_definitely (parser))
15036 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15037 in effect, then we must assume that, upon instantiation, the
15038 template will correspond to a class. */
15039 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15040 && tag_type == typename_type)
15041 type = make_typename_type (parser->scope, decl,
15042 typename_type,
15043 /*complain=*/tf_error);
15044 /* If the `typename' keyword is in effect and DECL is not a type
15045 decl, then type is non existent. */
15046 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15048 else if (TREE_CODE (decl) == TYPE_DECL)
15049 type = check_elaborated_type_specifier (tag_type, decl,
15050 /*allow_template_p=*/true);
15051 else if (decl == error_mark_node)
15052 type = error_mark_node;
15055 if (!type)
15057 token = cp_lexer_peek_token (parser->lexer);
15058 identifier = cp_parser_identifier (parser);
15060 if (identifier == error_mark_node)
15062 parser->scope = NULL_TREE;
15063 return error_mark_node;
15066 /* For a `typename', we needn't call xref_tag. */
15067 if (tag_type == typename_type
15068 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15069 return cp_parser_make_typename_type (parser, parser->scope,
15070 identifier,
15071 token->location);
15072 /* Look up a qualified name in the usual way. */
15073 if (parser->scope)
15075 tree decl;
15076 tree ambiguous_decls;
15078 decl = cp_parser_lookup_name (parser, identifier,
15079 tag_type,
15080 /*is_template=*/false,
15081 /*is_namespace=*/false,
15082 /*check_dependency=*/true,
15083 &ambiguous_decls,
15084 token->location);
15086 /* If the lookup was ambiguous, an error will already have been
15087 issued. */
15088 if (ambiguous_decls)
15089 return error_mark_node;
15091 /* If we are parsing friend declaration, DECL may be a
15092 TEMPLATE_DECL tree node here. However, we need to check
15093 whether this TEMPLATE_DECL results in valid code. Consider
15094 the following example:
15096 namespace N {
15097 template <class T> class C {};
15099 class X {
15100 template <class T> friend class N::C; // #1, valid code
15102 template <class T> class Y {
15103 friend class N::C; // #2, invalid code
15106 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15107 name lookup of `N::C'. We see that friend declaration must
15108 be template for the code to be valid. Note that
15109 processing_template_decl does not work here since it is
15110 always 1 for the above two cases. */
15112 decl = (cp_parser_maybe_treat_template_as_class
15113 (decl, /*tag_name_p=*/is_friend
15114 && parser->num_template_parameter_lists));
15116 if (TREE_CODE (decl) != TYPE_DECL)
15118 cp_parser_diagnose_invalid_type_name (parser,
15119 parser->scope,
15120 identifier,
15121 token->location);
15122 return error_mark_node;
15125 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15127 bool allow_template = (parser->num_template_parameter_lists
15128 || DECL_SELF_REFERENCE_P (decl));
15129 type = check_elaborated_type_specifier (tag_type, decl,
15130 allow_template);
15132 if (type == error_mark_node)
15133 return error_mark_node;
15136 /* Forward declarations of nested types, such as
15138 class C1::C2;
15139 class C1::C2::C3;
15141 are invalid unless all components preceding the final '::'
15142 are complete. If all enclosing types are complete, these
15143 declarations become merely pointless.
15145 Invalid forward declarations of nested types are errors
15146 caught elsewhere in parsing. Those that are pointless arrive
15147 here. */
15149 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15150 && !is_friend && !processing_explicit_instantiation)
15151 warning (0, "declaration %qD does not declare anything", decl);
15153 type = TREE_TYPE (decl);
15155 else
15157 /* An elaborated-type-specifier sometimes introduces a new type and
15158 sometimes names an existing type. Normally, the rule is that it
15159 introduces a new type only if there is not an existing type of
15160 the same name already in scope. For example, given:
15162 struct S {};
15163 void f() { struct S s; }
15165 the `struct S' in the body of `f' is the same `struct S' as in
15166 the global scope; the existing definition is used. However, if
15167 there were no global declaration, this would introduce a new
15168 local class named `S'.
15170 An exception to this rule applies to the following code:
15172 namespace N { struct S; }
15174 Here, the elaborated-type-specifier names a new type
15175 unconditionally; even if there is already an `S' in the
15176 containing scope this declaration names a new type.
15177 This exception only applies if the elaborated-type-specifier
15178 forms the complete declaration:
15180 [class.name]
15182 A declaration consisting solely of `class-key identifier ;' is
15183 either a redeclaration of the name in the current scope or a
15184 forward declaration of the identifier as a class name. It
15185 introduces the name into the current scope.
15187 We are in this situation precisely when the next token is a `;'.
15189 An exception to the exception is that a `friend' declaration does
15190 *not* name a new type; i.e., given:
15192 struct S { friend struct T; };
15194 `T' is not a new type in the scope of `S'.
15196 Also, `new struct S' or `sizeof (struct S)' never results in the
15197 definition of a new type; a new type can only be declared in a
15198 declaration context. */
15200 tag_scope ts;
15201 bool template_p;
15203 if (is_friend)
15204 /* Friends have special name lookup rules. */
15205 ts = ts_within_enclosing_non_class;
15206 else if (is_declaration
15207 && cp_lexer_next_token_is (parser->lexer,
15208 CPP_SEMICOLON))
15209 /* This is a `class-key identifier ;' */
15210 ts = ts_current;
15211 else
15212 ts = ts_global;
15214 template_p =
15215 (parser->num_template_parameter_lists
15216 && (cp_parser_next_token_starts_class_definition_p (parser)
15217 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15218 /* An unqualified name was used to reference this type, so
15219 there were no qualifying templates. */
15220 if (!cp_parser_check_template_parameters (parser,
15221 /*num_templates=*/0,
15222 token->location,
15223 /*declarator=*/NULL))
15224 return error_mark_node;
15225 type = xref_tag (tag_type, identifier, ts, template_p);
15229 if (type == error_mark_node)
15230 return error_mark_node;
15232 /* Allow attributes on forward declarations of classes. */
15233 if (attributes)
15235 if (TREE_CODE (type) == TYPENAME_TYPE)
15236 warning (OPT_Wattributes,
15237 "attributes ignored on uninstantiated type");
15238 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15239 && ! processing_explicit_instantiation)
15240 warning (OPT_Wattributes,
15241 "attributes ignored on template instantiation");
15242 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15243 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15244 else
15245 warning (OPT_Wattributes,
15246 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15249 if (tag_type != enum_type)
15251 /* Indicate whether this class was declared as a `class' or as a
15252 `struct'. */
15253 if (TREE_CODE (type) == RECORD_TYPE)
15254 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15255 cp_parser_check_class_key (tag_type, type);
15258 /* A "<" cannot follow an elaborated type specifier. If that
15259 happens, the user was probably trying to form a template-id. */
15260 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15261 token->location);
15263 return type;
15266 /* Parse an enum-specifier.
15268 enum-specifier:
15269 enum-head { enumerator-list [opt] }
15270 enum-head { enumerator-list , } [C++0x]
15272 enum-head:
15273 enum-key identifier [opt] enum-base [opt]
15274 enum-key nested-name-specifier identifier enum-base [opt]
15276 enum-key:
15277 enum
15278 enum class [C++0x]
15279 enum struct [C++0x]
15281 enum-base: [C++0x]
15282 : type-specifier-seq
15284 opaque-enum-specifier:
15285 enum-key identifier enum-base [opt] ;
15287 GNU Extensions:
15288 enum-key attributes[opt] identifier [opt] enum-base [opt]
15289 { enumerator-list [opt] }attributes[opt]
15290 enum-key attributes[opt] identifier [opt] enum-base [opt]
15291 { enumerator-list, }attributes[opt] [C++0x]
15293 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15294 if the token stream isn't an enum-specifier after all. */
15296 static tree
15297 cp_parser_enum_specifier (cp_parser* parser)
15299 tree identifier;
15300 tree type = NULL_TREE;
15301 tree prev_scope;
15302 tree nested_name_specifier = NULL_TREE;
15303 tree attributes;
15304 bool scoped_enum_p = false;
15305 bool has_underlying_type = false;
15306 bool nested_being_defined = false;
15307 bool new_value_list = false;
15308 bool is_new_type = false;
15309 bool is_anonymous = false;
15310 tree underlying_type = NULL_TREE;
15311 cp_token *type_start_token = NULL;
15312 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15314 parser->colon_corrects_to_scope_p = false;
15316 /* Parse tentatively so that we can back up if we don't find a
15317 enum-specifier. */
15318 cp_parser_parse_tentatively (parser);
15320 /* Caller guarantees that the current token is 'enum', an identifier
15321 possibly follows, and the token after that is an opening brace.
15322 If we don't have an identifier, fabricate an anonymous name for
15323 the enumeration being defined. */
15324 cp_lexer_consume_token (parser->lexer);
15326 /* Parse the "class" or "struct", which indicates a scoped
15327 enumeration type in C++0x. */
15328 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15329 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15331 if (cxx_dialect < cxx11)
15332 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15334 /* Consume the `struct' or `class' token. */
15335 cp_lexer_consume_token (parser->lexer);
15337 scoped_enum_p = true;
15340 attributes = cp_parser_attributes_opt (parser);
15342 /* Clear the qualification. */
15343 parser->scope = NULL_TREE;
15344 parser->qualifying_scope = NULL_TREE;
15345 parser->object_scope = NULL_TREE;
15347 /* Figure out in what scope the declaration is being placed. */
15348 prev_scope = current_scope ();
15350 type_start_token = cp_lexer_peek_token (parser->lexer);
15352 push_deferring_access_checks (dk_no_check);
15353 nested_name_specifier
15354 = cp_parser_nested_name_specifier_opt (parser,
15355 /*typename_keyword_p=*/true,
15356 /*check_dependency_p=*/false,
15357 /*type_p=*/false,
15358 /*is_declaration=*/false);
15360 if (nested_name_specifier)
15362 tree name;
15364 identifier = cp_parser_identifier (parser);
15365 name = cp_parser_lookup_name (parser, identifier,
15366 enum_type,
15367 /*is_template=*/false,
15368 /*is_namespace=*/false,
15369 /*check_dependency=*/true,
15370 /*ambiguous_decls=*/NULL,
15371 input_location);
15372 if (name && name != error_mark_node)
15374 type = TREE_TYPE (name);
15375 if (TREE_CODE (type) == TYPENAME_TYPE)
15377 /* Are template enums allowed in ISO? */
15378 if (template_parm_scope_p ())
15379 pedwarn (type_start_token->location, OPT_Wpedantic,
15380 "%qD is an enumeration template", name);
15381 /* ignore a typename reference, for it will be solved by name
15382 in start_enum. */
15383 type = NULL_TREE;
15386 else if (nested_name_specifier == error_mark_node)
15387 /* We already issued an error. */;
15388 else
15389 error_at (type_start_token->location,
15390 "%qD is not an enumerator-name", identifier);
15392 else
15394 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15395 identifier = cp_parser_identifier (parser);
15396 else
15398 identifier = make_anon_name ();
15399 is_anonymous = true;
15400 if (scoped_enum_p)
15401 error_at (type_start_token->location,
15402 "anonymous scoped enum is not allowed");
15405 pop_deferring_access_checks ();
15407 /* Check for the `:' that denotes a specified underlying type in C++0x.
15408 Note that a ':' could also indicate a bitfield width, however. */
15409 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15411 cp_decl_specifier_seq type_specifiers;
15413 /* Consume the `:'. */
15414 cp_lexer_consume_token (parser->lexer);
15416 /* Parse the type-specifier-seq. */
15417 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15418 /*is_trailing_return=*/false,
15419 &type_specifiers);
15421 /* At this point this is surely not elaborated type specifier. */
15422 if (!cp_parser_parse_definitely (parser))
15423 return NULL_TREE;
15425 if (cxx_dialect < cxx11)
15426 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15428 has_underlying_type = true;
15430 /* If that didn't work, stop. */
15431 if (type_specifiers.type != error_mark_node)
15433 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15434 /*initialized=*/0, NULL);
15435 if (underlying_type == error_mark_node
15436 || check_for_bare_parameter_packs (underlying_type))
15437 underlying_type = NULL_TREE;
15441 /* Look for the `{' but don't consume it yet. */
15442 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15444 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15446 cp_parser_error (parser, "expected %<{%>");
15447 if (has_underlying_type)
15449 type = NULL_TREE;
15450 goto out;
15453 /* An opaque-enum-specifier must have a ';' here. */
15454 if ((scoped_enum_p || underlying_type)
15455 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15457 cp_parser_error (parser, "expected %<;%> or %<{%>");
15458 if (has_underlying_type)
15460 type = NULL_TREE;
15461 goto out;
15466 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15467 return NULL_TREE;
15469 if (nested_name_specifier)
15471 if (CLASS_TYPE_P (nested_name_specifier))
15473 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15474 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15475 push_scope (nested_name_specifier);
15477 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15479 push_nested_namespace (nested_name_specifier);
15483 /* Issue an error message if type-definitions are forbidden here. */
15484 if (!cp_parser_check_type_definition (parser))
15485 type = error_mark_node;
15486 else
15487 /* Create the new type. We do this before consuming the opening
15488 brace so the enum will be recorded as being on the line of its
15489 tag (or the 'enum' keyword, if there is no tag). */
15490 type = start_enum (identifier, type, underlying_type,
15491 scoped_enum_p, &is_new_type);
15493 /* If the next token is not '{' it is an opaque-enum-specifier or an
15494 elaborated-type-specifier. */
15495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15497 timevar_push (TV_PARSE_ENUM);
15498 if (nested_name_specifier
15499 && nested_name_specifier != error_mark_node)
15501 /* The following catches invalid code such as:
15502 enum class S<int>::E { A, B, C }; */
15503 if (!processing_specialization
15504 && CLASS_TYPE_P (nested_name_specifier)
15505 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15506 error_at (type_start_token->location, "cannot add an enumerator "
15507 "list to a template instantiation");
15509 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15511 error_at (type_start_token->location,
15512 "%<%T::%E%> has not been declared",
15513 TYPE_CONTEXT (nested_name_specifier),
15514 nested_name_specifier);
15515 type = error_mark_node;
15517 /* If that scope does not contain the scope in which the
15518 class was originally declared, the program is invalid. */
15519 else if (prev_scope && !is_ancestor (prev_scope,
15520 nested_name_specifier))
15522 if (at_namespace_scope_p ())
15523 error_at (type_start_token->location,
15524 "declaration of %qD in namespace %qD which does not "
15525 "enclose %qD",
15526 type, prev_scope, nested_name_specifier);
15527 else
15528 error_at (type_start_token->location,
15529 "declaration of %qD in %qD which does not "
15530 "enclose %qD",
15531 type, prev_scope, nested_name_specifier);
15532 type = error_mark_node;
15536 if (scoped_enum_p)
15537 begin_scope (sk_scoped_enum, type);
15539 /* Consume the opening brace. */
15540 cp_lexer_consume_token (parser->lexer);
15542 if (type == error_mark_node)
15543 ; /* Nothing to add */
15544 else if (OPAQUE_ENUM_P (type)
15545 || (cxx_dialect > cxx98 && processing_specialization))
15547 new_value_list = true;
15548 SET_OPAQUE_ENUM_P (type, false);
15549 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15551 else
15553 error_at (type_start_token->location, "multiple definition of %q#T", type);
15554 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15555 "previous definition here");
15556 type = error_mark_node;
15559 if (type == error_mark_node)
15560 cp_parser_skip_to_end_of_block_or_statement (parser);
15561 /* If the next token is not '}', then there are some enumerators. */
15562 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15564 if (is_anonymous && !scoped_enum_p)
15565 pedwarn (type_start_token->location, OPT_Wpedantic,
15566 "ISO C++ forbids empty anonymous enum");
15568 else
15569 cp_parser_enumerator_list (parser, type);
15571 /* Consume the final '}'. */
15572 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15574 if (scoped_enum_p)
15575 finish_scope ();
15576 timevar_pop (TV_PARSE_ENUM);
15578 else
15580 /* If a ';' follows, then it is an opaque-enum-specifier
15581 and additional restrictions apply. */
15582 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15584 if (is_anonymous)
15585 error_at (type_start_token->location,
15586 "opaque-enum-specifier without name");
15587 else if (nested_name_specifier)
15588 error_at (type_start_token->location,
15589 "opaque-enum-specifier must use a simple identifier");
15593 /* Look for trailing attributes to apply to this enumeration, and
15594 apply them if appropriate. */
15595 if (cp_parser_allow_gnu_extensions_p (parser))
15597 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15598 trailing_attr = chainon (trailing_attr, attributes);
15599 cplus_decl_attributes (&type,
15600 trailing_attr,
15601 (int) ATTR_FLAG_TYPE_IN_PLACE);
15604 /* Finish up the enumeration. */
15605 if (type != error_mark_node)
15607 if (new_value_list)
15608 finish_enum_value_list (type);
15609 if (is_new_type)
15610 finish_enum (type);
15613 if (nested_name_specifier)
15615 if (CLASS_TYPE_P (nested_name_specifier))
15617 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15618 pop_scope (nested_name_specifier);
15620 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15622 pop_nested_namespace (nested_name_specifier);
15625 out:
15626 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15627 return type;
15630 /* Parse an enumerator-list. The enumerators all have the indicated
15631 TYPE.
15633 enumerator-list:
15634 enumerator-definition
15635 enumerator-list , enumerator-definition */
15637 static void
15638 cp_parser_enumerator_list (cp_parser* parser, tree type)
15640 while (true)
15642 /* Parse an enumerator-definition. */
15643 cp_parser_enumerator_definition (parser, type);
15645 /* If the next token is not a ',', we've reached the end of
15646 the list. */
15647 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15648 break;
15649 /* Otherwise, consume the `,' and keep going. */
15650 cp_lexer_consume_token (parser->lexer);
15651 /* If the next token is a `}', there is a trailing comma. */
15652 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15654 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15655 pedwarn (input_location, OPT_Wpedantic,
15656 "comma at end of enumerator list");
15657 break;
15662 /* Parse an enumerator-definition. The enumerator has the indicated
15663 TYPE.
15665 enumerator-definition:
15666 enumerator
15667 enumerator = constant-expression
15669 enumerator:
15670 identifier */
15672 static void
15673 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15675 tree identifier;
15676 tree value;
15677 location_t loc;
15679 /* Save the input location because we are interested in the location
15680 of the identifier and not the location of the explicit value. */
15681 loc = cp_lexer_peek_token (parser->lexer)->location;
15683 /* Look for the identifier. */
15684 identifier = cp_parser_identifier (parser);
15685 if (identifier == error_mark_node)
15686 return;
15688 /* If the next token is an '=', then there is an explicit value. */
15689 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15691 /* Consume the `=' token. */
15692 cp_lexer_consume_token (parser->lexer);
15693 /* Parse the value. */
15694 value = cp_parser_constant_expression (parser,
15695 /*allow_non_constant_p=*/false,
15696 NULL);
15698 else
15699 value = NULL_TREE;
15701 /* If we are processing a template, make sure the initializer of the
15702 enumerator doesn't contain any bare template parameter pack. */
15703 if (check_for_bare_parameter_packs (value))
15704 value = error_mark_node;
15706 /* integral_constant_value will pull out this expression, so make sure
15707 it's folded as appropriate. */
15708 value = fold_non_dependent_expr (value);
15710 /* Create the enumerator. */
15711 build_enumerator (identifier, value, type, loc);
15714 /* Parse a namespace-name.
15716 namespace-name:
15717 original-namespace-name
15718 namespace-alias
15720 Returns the NAMESPACE_DECL for the namespace. */
15722 static tree
15723 cp_parser_namespace_name (cp_parser* parser)
15725 tree identifier;
15726 tree namespace_decl;
15728 cp_token *token = cp_lexer_peek_token (parser->lexer);
15730 /* Get the name of the namespace. */
15731 identifier = cp_parser_identifier (parser);
15732 if (identifier == error_mark_node)
15733 return error_mark_node;
15735 /* Look up the identifier in the currently active scope. Look only
15736 for namespaces, due to:
15738 [basic.lookup.udir]
15740 When looking up a namespace-name in a using-directive or alias
15741 definition, only namespace names are considered.
15743 And:
15745 [basic.lookup.qual]
15747 During the lookup of a name preceding the :: scope resolution
15748 operator, object, function, and enumerator names are ignored.
15750 (Note that cp_parser_qualifying_entity only calls this
15751 function if the token after the name is the scope resolution
15752 operator.) */
15753 namespace_decl = cp_parser_lookup_name (parser, identifier,
15754 none_type,
15755 /*is_template=*/false,
15756 /*is_namespace=*/true,
15757 /*check_dependency=*/true,
15758 /*ambiguous_decls=*/NULL,
15759 token->location);
15760 /* If it's not a namespace, issue an error. */
15761 if (namespace_decl == error_mark_node
15762 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15764 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15765 error_at (token->location, "%qD is not a namespace-name", identifier);
15766 cp_parser_error (parser, "expected namespace-name");
15767 namespace_decl = error_mark_node;
15770 return namespace_decl;
15773 /* Parse a namespace-definition.
15775 namespace-definition:
15776 named-namespace-definition
15777 unnamed-namespace-definition
15779 named-namespace-definition:
15780 original-namespace-definition
15781 extension-namespace-definition
15783 original-namespace-definition:
15784 namespace identifier { namespace-body }
15786 extension-namespace-definition:
15787 namespace original-namespace-name { namespace-body }
15789 unnamed-namespace-definition:
15790 namespace { namespace-body } */
15792 static void
15793 cp_parser_namespace_definition (cp_parser* parser)
15795 tree identifier, attribs;
15796 bool has_visibility;
15797 bool is_inline;
15799 cp_ensure_no_omp_declare_simd (parser);
15800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15802 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15803 is_inline = true;
15804 cp_lexer_consume_token (parser->lexer);
15806 else
15807 is_inline = false;
15809 /* Look for the `namespace' keyword. */
15810 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15812 /* Get the name of the namespace. We do not attempt to distinguish
15813 between an original-namespace-definition and an
15814 extension-namespace-definition at this point. The semantic
15815 analysis routines are responsible for that. */
15816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15817 identifier = cp_parser_identifier (parser);
15818 else
15819 identifier = NULL_TREE;
15821 /* Parse any specified attributes. */
15822 attribs = cp_parser_attributes_opt (parser);
15824 /* Look for the `{' to start the namespace. */
15825 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15826 /* Start the namespace. */
15827 push_namespace (identifier);
15829 /* "inline namespace" is equivalent to a stub namespace definition
15830 followed by a strong using directive. */
15831 if (is_inline)
15833 tree name_space = current_namespace;
15834 /* Set up namespace association. */
15835 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15836 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15837 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15838 /* Import the contents of the inline namespace. */
15839 pop_namespace ();
15840 do_using_directive (name_space);
15841 push_namespace (identifier);
15844 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15846 /* Parse the body of the namespace. */
15847 cp_parser_namespace_body (parser);
15849 if (has_visibility)
15850 pop_visibility (1);
15852 /* Finish the namespace. */
15853 pop_namespace ();
15854 /* Look for the final `}'. */
15855 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15858 /* Parse a namespace-body.
15860 namespace-body:
15861 declaration-seq [opt] */
15863 static void
15864 cp_parser_namespace_body (cp_parser* parser)
15866 cp_parser_declaration_seq_opt (parser);
15869 /* Parse a namespace-alias-definition.
15871 namespace-alias-definition:
15872 namespace identifier = qualified-namespace-specifier ; */
15874 static void
15875 cp_parser_namespace_alias_definition (cp_parser* parser)
15877 tree identifier;
15878 tree namespace_specifier;
15880 cp_token *token = cp_lexer_peek_token (parser->lexer);
15882 /* Look for the `namespace' keyword. */
15883 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15884 /* Look for the identifier. */
15885 identifier = cp_parser_identifier (parser);
15886 if (identifier == error_mark_node)
15887 return;
15888 /* Look for the `=' token. */
15889 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15890 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15892 error_at (token->location, "%<namespace%> definition is not allowed here");
15893 /* Skip the definition. */
15894 cp_lexer_consume_token (parser->lexer);
15895 if (cp_parser_skip_to_closing_brace (parser))
15896 cp_lexer_consume_token (parser->lexer);
15897 return;
15899 cp_parser_require (parser, CPP_EQ, RT_EQ);
15900 /* Look for the qualified-namespace-specifier. */
15901 namespace_specifier
15902 = cp_parser_qualified_namespace_specifier (parser);
15903 /* Look for the `;' token. */
15904 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15906 /* Register the alias in the symbol table. */
15907 do_namespace_alias (identifier, namespace_specifier);
15910 /* Parse a qualified-namespace-specifier.
15912 qualified-namespace-specifier:
15913 :: [opt] nested-name-specifier [opt] namespace-name
15915 Returns a NAMESPACE_DECL corresponding to the specified
15916 namespace. */
15918 static tree
15919 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15921 /* Look for the optional `::'. */
15922 cp_parser_global_scope_opt (parser,
15923 /*current_scope_valid_p=*/false);
15925 /* Look for the optional nested-name-specifier. */
15926 cp_parser_nested_name_specifier_opt (parser,
15927 /*typename_keyword_p=*/false,
15928 /*check_dependency_p=*/true,
15929 /*type_p=*/false,
15930 /*is_declaration=*/true);
15932 return cp_parser_namespace_name (parser);
15935 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15936 access declaration.
15938 using-declaration:
15939 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15940 using :: unqualified-id ;
15942 access-declaration:
15943 qualified-id ;
15947 static bool
15948 cp_parser_using_declaration (cp_parser* parser,
15949 bool access_declaration_p)
15951 cp_token *token;
15952 bool typename_p = false;
15953 bool global_scope_p;
15954 tree decl;
15955 tree identifier;
15956 tree qscope;
15957 int oldcount = errorcount;
15958 cp_token *diag_token = NULL;
15960 if (access_declaration_p)
15962 diag_token = cp_lexer_peek_token (parser->lexer);
15963 cp_parser_parse_tentatively (parser);
15965 else
15967 /* Look for the `using' keyword. */
15968 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15970 /* Peek at the next token. */
15971 token = cp_lexer_peek_token (parser->lexer);
15972 /* See if it's `typename'. */
15973 if (token->keyword == RID_TYPENAME)
15975 /* Remember that we've seen it. */
15976 typename_p = true;
15977 /* Consume the `typename' token. */
15978 cp_lexer_consume_token (parser->lexer);
15982 /* Look for the optional global scope qualification. */
15983 global_scope_p
15984 = (cp_parser_global_scope_opt (parser,
15985 /*current_scope_valid_p=*/false)
15986 != NULL_TREE);
15988 /* If we saw `typename', or didn't see `::', then there must be a
15989 nested-name-specifier present. */
15990 if (typename_p || !global_scope_p)
15992 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15993 /*check_dependency_p=*/true,
15994 /*type_p=*/false,
15995 /*is_declaration=*/true);
15996 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
15998 cp_parser_skip_to_end_of_block_or_statement (parser);
15999 return false;
16002 /* Otherwise, we could be in either of the two productions. In that
16003 case, treat the nested-name-specifier as optional. */
16004 else
16005 qscope = cp_parser_nested_name_specifier_opt (parser,
16006 /*typename_keyword_p=*/false,
16007 /*check_dependency_p=*/true,
16008 /*type_p=*/false,
16009 /*is_declaration=*/true);
16010 if (!qscope)
16011 qscope = global_namespace;
16013 if (access_declaration_p && cp_parser_error_occurred (parser))
16014 /* Something has already gone wrong; there's no need to parse
16015 further. Since an error has occurred, the return value of
16016 cp_parser_parse_definitely will be false, as required. */
16017 return cp_parser_parse_definitely (parser);
16019 token = cp_lexer_peek_token (parser->lexer);
16020 /* Parse the unqualified-id. */
16021 identifier = cp_parser_unqualified_id (parser,
16022 /*template_keyword_p=*/false,
16023 /*check_dependency_p=*/true,
16024 /*declarator_p=*/true,
16025 /*optional_p=*/false);
16027 if (access_declaration_p)
16029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16030 cp_parser_simulate_error (parser);
16031 if (!cp_parser_parse_definitely (parser))
16032 return false;
16035 /* The function we call to handle a using-declaration is different
16036 depending on what scope we are in. */
16037 if (qscope == error_mark_node || identifier == error_mark_node)
16039 else if (!identifier_p (identifier)
16040 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16041 /* [namespace.udecl]
16043 A using declaration shall not name a template-id. */
16044 error_at (token->location,
16045 "a template-id may not appear in a using-declaration");
16046 else
16048 if (at_class_scope_p ())
16050 /* Create the USING_DECL. */
16051 decl = do_class_using_decl (parser->scope, identifier);
16053 if (decl && typename_p)
16054 USING_DECL_TYPENAME_P (decl) = 1;
16056 if (check_for_bare_parameter_packs (decl))
16058 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16059 return false;
16061 else
16062 /* Add it to the list of members in this class. */
16063 finish_member_declaration (decl);
16065 else
16067 decl = cp_parser_lookup_name_simple (parser,
16068 identifier,
16069 token->location);
16070 if (decl == error_mark_node)
16071 cp_parser_name_lookup_error (parser, identifier,
16072 decl, NLE_NULL,
16073 token->location);
16074 else if (check_for_bare_parameter_packs (decl))
16076 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16077 return false;
16079 else if (!at_namespace_scope_p ())
16080 do_local_using_decl (decl, qscope, identifier);
16081 else
16082 do_toplevel_using_decl (decl, qscope, identifier);
16086 /* Look for the final `;'. */
16087 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16089 if (access_declaration_p && errorcount == oldcount)
16090 warning_at (diag_token->location, OPT_Wdeprecated,
16091 "access declarations are deprecated "
16092 "in favour of using-declarations; "
16093 "suggestion: add the %<using%> keyword");
16095 return true;
16098 /* Parse an alias-declaration.
16100 alias-declaration:
16101 using identifier attribute-specifier-seq [opt] = type-id */
16103 static tree
16104 cp_parser_alias_declaration (cp_parser* parser)
16106 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16107 location_t id_location;
16108 cp_declarator *declarator;
16109 cp_decl_specifier_seq decl_specs;
16110 bool member_p;
16111 const char *saved_message = NULL;
16113 /* Look for the `using' keyword. */
16114 cp_token *using_token
16115 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16116 if (using_token == NULL)
16117 return error_mark_node;
16119 id_location = cp_lexer_peek_token (parser->lexer)->location;
16120 id = cp_parser_identifier (parser);
16121 if (id == error_mark_node)
16122 return error_mark_node;
16123 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16124 attributes = cp_parser_attributes_opt (parser);
16125 if (attributes == error_mark_node)
16126 return error_mark_node;
16128 cp_parser_require (parser, CPP_EQ, RT_EQ);
16130 if (cp_parser_error_occurred (parser))
16131 return error_mark_node;
16133 cp_parser_commit_to_tentative_parse (parser);
16135 /* Now we are going to parse the type-id of the declaration. */
16138 [dcl.type]/3 says:
16140 "A type-specifier-seq shall not define a class or enumeration
16141 unless it appears in the type-id of an alias-declaration (7.1.3) that
16142 is not the declaration of a template-declaration."
16144 In other words, if we currently are in an alias template, the
16145 type-id should not define a type.
16147 So let's set parser->type_definition_forbidden_message in that
16148 case; cp_parser_check_type_definition (called by
16149 cp_parser_class_specifier) will then emit an error if a type is
16150 defined in the type-id. */
16151 if (parser->num_template_parameter_lists)
16153 saved_message = parser->type_definition_forbidden_message;
16154 parser->type_definition_forbidden_message =
16155 G_("types may not be defined in alias template declarations");
16158 type = cp_parser_type_id (parser);
16160 /* Restore the error message if need be. */
16161 if (parser->num_template_parameter_lists)
16162 parser->type_definition_forbidden_message = saved_message;
16164 if (type == error_mark_node)
16166 cp_parser_skip_to_end_of_block_or_statement (parser);
16167 return error_mark_node;
16170 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16172 if (cp_parser_error_occurred (parser))
16174 cp_parser_skip_to_end_of_block_or_statement (parser);
16175 return error_mark_node;
16178 /* A typedef-name can also be introduced by an alias-declaration. The
16179 identifier following the using keyword becomes a typedef-name. It has
16180 the same semantics as if it were introduced by the typedef
16181 specifier. In particular, it does not define a new type and it shall
16182 not appear in the type-id. */
16184 clear_decl_specs (&decl_specs);
16185 decl_specs.type = type;
16186 if (attributes != NULL_TREE)
16188 decl_specs.attributes = attributes;
16189 set_and_check_decl_spec_loc (&decl_specs,
16190 ds_attribute,
16191 attrs_token);
16193 set_and_check_decl_spec_loc (&decl_specs,
16194 ds_typedef,
16195 using_token);
16196 set_and_check_decl_spec_loc (&decl_specs,
16197 ds_alias,
16198 using_token);
16200 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16201 declarator->id_loc = id_location;
16203 member_p = at_class_scope_p ();
16204 if (member_p)
16205 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16206 NULL_TREE, attributes);
16207 else
16208 decl = start_decl (declarator, &decl_specs, 0,
16209 attributes, NULL_TREE, &pushed_scope);
16210 if (decl == error_mark_node)
16211 return decl;
16213 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16215 if (pushed_scope)
16216 pop_scope (pushed_scope);
16218 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16219 added into the symbol table; otherwise, return the TYPE_DECL. */
16220 if (DECL_LANG_SPECIFIC (decl)
16221 && DECL_TEMPLATE_INFO (decl)
16222 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16224 decl = DECL_TI_TEMPLATE (decl);
16225 if (member_p)
16226 check_member_template (decl);
16229 return decl;
16232 /* Parse a using-directive.
16234 using-directive:
16235 using namespace :: [opt] nested-name-specifier [opt]
16236 namespace-name ; */
16238 static void
16239 cp_parser_using_directive (cp_parser* parser)
16241 tree namespace_decl;
16242 tree attribs;
16244 /* Look for the `using' keyword. */
16245 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16246 /* And the `namespace' keyword. */
16247 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16248 /* Look for the optional `::' operator. */
16249 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16250 /* And the optional nested-name-specifier. */
16251 cp_parser_nested_name_specifier_opt (parser,
16252 /*typename_keyword_p=*/false,
16253 /*check_dependency_p=*/true,
16254 /*type_p=*/false,
16255 /*is_declaration=*/true);
16256 /* Get the namespace being used. */
16257 namespace_decl = cp_parser_namespace_name (parser);
16258 /* And any specified attributes. */
16259 attribs = cp_parser_attributes_opt (parser);
16260 /* Update the symbol table. */
16261 parse_using_directive (namespace_decl, attribs);
16262 /* Look for the final `;'. */
16263 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16266 /* Parse an asm-definition.
16268 asm-definition:
16269 asm ( string-literal ) ;
16271 GNU Extension:
16273 asm-definition:
16274 asm volatile [opt] ( string-literal ) ;
16275 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16276 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16277 : asm-operand-list [opt] ) ;
16278 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16279 : asm-operand-list [opt]
16280 : asm-clobber-list [opt] ) ;
16281 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16282 : asm-clobber-list [opt]
16283 : asm-goto-list ) ; */
16285 static void
16286 cp_parser_asm_definition (cp_parser* parser)
16288 tree string;
16289 tree outputs = NULL_TREE;
16290 tree inputs = NULL_TREE;
16291 tree clobbers = NULL_TREE;
16292 tree labels = NULL_TREE;
16293 tree asm_stmt;
16294 bool volatile_p = false;
16295 bool extended_p = false;
16296 bool invalid_inputs_p = false;
16297 bool invalid_outputs_p = false;
16298 bool goto_p = false;
16299 required_token missing = RT_NONE;
16301 /* Look for the `asm' keyword. */
16302 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16303 /* See if the next token is `volatile'. */
16304 if (cp_parser_allow_gnu_extensions_p (parser)
16305 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16307 /* Remember that we saw the `volatile' keyword. */
16308 volatile_p = true;
16309 /* Consume the token. */
16310 cp_lexer_consume_token (parser->lexer);
16312 if (cp_parser_allow_gnu_extensions_p (parser)
16313 && parser->in_function_body
16314 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16316 /* Remember that we saw the `goto' keyword. */
16317 goto_p = true;
16318 /* Consume the token. */
16319 cp_lexer_consume_token (parser->lexer);
16321 /* Look for the opening `('. */
16322 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16323 return;
16324 /* Look for the string. */
16325 string = cp_parser_string_literal (parser, false, false);
16326 if (string == error_mark_node)
16328 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16329 /*consume_paren=*/true);
16330 return;
16333 /* If we're allowing GNU extensions, check for the extended assembly
16334 syntax. Unfortunately, the `:' tokens need not be separated by
16335 a space in C, and so, for compatibility, we tolerate that here
16336 too. Doing that means that we have to treat the `::' operator as
16337 two `:' tokens. */
16338 if (cp_parser_allow_gnu_extensions_p (parser)
16339 && parser->in_function_body
16340 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16341 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16343 bool inputs_p = false;
16344 bool clobbers_p = false;
16345 bool labels_p = false;
16347 /* The extended syntax was used. */
16348 extended_p = true;
16350 /* Look for outputs. */
16351 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16353 /* Consume the `:'. */
16354 cp_lexer_consume_token (parser->lexer);
16355 /* Parse the output-operands. */
16356 if (cp_lexer_next_token_is_not (parser->lexer,
16357 CPP_COLON)
16358 && cp_lexer_next_token_is_not (parser->lexer,
16359 CPP_SCOPE)
16360 && cp_lexer_next_token_is_not (parser->lexer,
16361 CPP_CLOSE_PAREN)
16362 && !goto_p)
16363 outputs = cp_parser_asm_operand_list (parser);
16365 if (outputs == error_mark_node)
16366 invalid_outputs_p = true;
16368 /* If the next token is `::', there are no outputs, and the
16369 next token is the beginning of the inputs. */
16370 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16371 /* The inputs are coming next. */
16372 inputs_p = true;
16374 /* Look for inputs. */
16375 if (inputs_p
16376 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16378 /* Consume the `:' or `::'. */
16379 cp_lexer_consume_token (parser->lexer);
16380 /* Parse the output-operands. */
16381 if (cp_lexer_next_token_is_not (parser->lexer,
16382 CPP_COLON)
16383 && cp_lexer_next_token_is_not (parser->lexer,
16384 CPP_SCOPE)
16385 && cp_lexer_next_token_is_not (parser->lexer,
16386 CPP_CLOSE_PAREN))
16387 inputs = cp_parser_asm_operand_list (parser);
16389 if (inputs == error_mark_node)
16390 invalid_inputs_p = true;
16392 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16393 /* The clobbers are coming next. */
16394 clobbers_p = true;
16396 /* Look for clobbers. */
16397 if (clobbers_p
16398 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16400 clobbers_p = true;
16401 /* Consume the `:' or `::'. */
16402 cp_lexer_consume_token (parser->lexer);
16403 /* Parse the clobbers. */
16404 if (cp_lexer_next_token_is_not (parser->lexer,
16405 CPP_COLON)
16406 && cp_lexer_next_token_is_not (parser->lexer,
16407 CPP_CLOSE_PAREN))
16408 clobbers = cp_parser_asm_clobber_list (parser);
16410 else if (goto_p
16411 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16412 /* The labels are coming next. */
16413 labels_p = true;
16415 /* Look for labels. */
16416 if (labels_p
16417 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16419 labels_p = true;
16420 /* Consume the `:' or `::'. */
16421 cp_lexer_consume_token (parser->lexer);
16422 /* Parse the labels. */
16423 labels = cp_parser_asm_label_list (parser);
16426 if (goto_p && !labels_p)
16427 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16429 else if (goto_p)
16430 missing = RT_COLON_SCOPE;
16432 /* Look for the closing `)'. */
16433 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16434 missing ? missing : RT_CLOSE_PAREN))
16435 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16436 /*consume_paren=*/true);
16437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16439 if (!invalid_inputs_p && !invalid_outputs_p)
16441 /* Create the ASM_EXPR. */
16442 if (parser->in_function_body)
16444 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16445 inputs, clobbers, labels);
16446 /* If the extended syntax was not used, mark the ASM_EXPR. */
16447 if (!extended_p)
16449 tree temp = asm_stmt;
16450 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16451 temp = TREE_OPERAND (temp, 0);
16453 ASM_INPUT_P (temp) = 1;
16456 else
16457 add_asm_node (string);
16461 /* Declarators [gram.dcl.decl] */
16463 /* Parse an init-declarator.
16465 init-declarator:
16466 declarator initializer [opt]
16468 GNU Extension:
16470 init-declarator:
16471 declarator asm-specification [opt] attributes [opt] initializer [opt]
16473 function-definition:
16474 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16475 function-body
16476 decl-specifier-seq [opt] declarator function-try-block
16478 GNU Extension:
16480 function-definition:
16481 __extension__ function-definition
16483 TM Extension:
16485 function-definition:
16486 decl-specifier-seq [opt] declarator function-transaction-block
16488 The DECL_SPECIFIERS apply to this declarator. Returns a
16489 representation of the entity declared. If MEMBER_P is TRUE, then
16490 this declarator appears in a class scope. The new DECL created by
16491 this declarator is returned.
16493 The CHECKS are access checks that should be performed once we know
16494 what entity is being declared (and, therefore, what classes have
16495 befriended it).
16497 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16498 for a function-definition here as well. If the declarator is a
16499 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16500 be TRUE upon return. By that point, the function-definition will
16501 have been completely parsed.
16503 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16504 is FALSE.
16506 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16507 parsed declaration if it is an uninitialized single declarator not followed
16508 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16509 if present, will not be consumed. If returned, this declarator will be
16510 created with SD_INITIALIZED but will not call cp_finish_decl. */
16512 static tree
16513 cp_parser_init_declarator (cp_parser* parser,
16514 cp_decl_specifier_seq *decl_specifiers,
16515 vec<deferred_access_check, va_gc> *checks,
16516 bool function_definition_allowed_p,
16517 bool member_p,
16518 int declares_class_or_enum,
16519 bool* function_definition_p,
16520 tree* maybe_range_for_decl)
16522 cp_token *token = NULL, *asm_spec_start_token = NULL,
16523 *attributes_start_token = NULL;
16524 cp_declarator *declarator;
16525 tree prefix_attributes;
16526 tree attributes = NULL;
16527 tree asm_specification;
16528 tree initializer;
16529 tree decl = NULL_TREE;
16530 tree scope;
16531 int is_initialized;
16532 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16533 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16534 "(...)". */
16535 enum cpp_ttype initialization_kind;
16536 bool is_direct_init = false;
16537 bool is_non_constant_init;
16538 int ctor_dtor_or_conv_p;
16539 bool friend_p;
16540 tree pushed_scope = NULL_TREE;
16541 bool range_for_decl_p = false;
16542 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16544 /* Gather the attributes that were provided with the
16545 decl-specifiers. */
16546 prefix_attributes = decl_specifiers->attributes;
16548 /* Assume that this is not the declarator for a function
16549 definition. */
16550 if (function_definition_p)
16551 *function_definition_p = false;
16553 /* Default arguments are only permitted for function parameters. */
16554 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16555 parser->default_arg_ok_p = false;
16557 /* Defer access checks while parsing the declarator; we cannot know
16558 what names are accessible until we know what is being
16559 declared. */
16560 resume_deferring_access_checks ();
16562 /* Parse the declarator. */
16563 token = cp_lexer_peek_token (parser->lexer);
16564 declarator
16565 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16566 &ctor_dtor_or_conv_p,
16567 /*parenthesized_p=*/NULL,
16568 member_p);
16569 /* Gather up the deferred checks. */
16570 stop_deferring_access_checks ();
16572 parser->default_arg_ok_p = saved_default_arg_ok_p;
16574 /* If the DECLARATOR was erroneous, there's no need to go
16575 further. */
16576 if (declarator == cp_error_declarator)
16577 return error_mark_node;
16579 /* Check that the number of template-parameter-lists is OK. */
16580 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16581 token->location))
16582 return error_mark_node;
16584 if (declares_class_or_enum & 2)
16585 cp_parser_check_for_definition_in_return_type (declarator,
16586 decl_specifiers->type,
16587 decl_specifiers->locations[ds_type_spec]);
16589 /* Figure out what scope the entity declared by the DECLARATOR is
16590 located in. `grokdeclarator' sometimes changes the scope, so
16591 we compute it now. */
16592 scope = get_scope_of_declarator (declarator);
16594 /* Perform any lookups in the declared type which were thought to be
16595 dependent, but are not in the scope of the declarator. */
16596 decl_specifiers->type
16597 = maybe_update_decl_type (decl_specifiers->type, scope);
16599 /* If we're allowing GNU extensions, look for an
16600 asm-specification. */
16601 if (cp_parser_allow_gnu_extensions_p (parser))
16603 /* Look for an asm-specification. */
16604 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16605 asm_specification = cp_parser_asm_specification_opt (parser);
16607 else
16608 asm_specification = NULL_TREE;
16610 /* Look for attributes. */
16611 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16612 attributes = cp_parser_attributes_opt (parser);
16614 /* Peek at the next token. */
16615 token = cp_lexer_peek_token (parser->lexer);
16617 if (function_declarator_p (declarator))
16619 /* Check to see if the token indicates the start of a
16620 function-definition. */
16621 if (cp_parser_token_starts_function_definition_p (token))
16623 if (!function_definition_allowed_p)
16625 /* If a function-definition should not appear here, issue an
16626 error message. */
16627 cp_parser_error (parser,
16628 "a function-definition is not allowed here");
16629 return error_mark_node;
16632 location_t func_brace_location
16633 = cp_lexer_peek_token (parser->lexer)->location;
16635 /* Neither attributes nor an asm-specification are allowed
16636 on a function-definition. */
16637 if (asm_specification)
16638 error_at (asm_spec_start_token->location,
16639 "an asm-specification is not allowed "
16640 "on a function-definition");
16641 if (attributes)
16642 error_at (attributes_start_token->location,
16643 "attributes are not allowed "
16644 "on a function-definition");
16645 /* This is a function-definition. */
16646 *function_definition_p = true;
16648 /* Parse the function definition. */
16649 if (member_p)
16650 decl = cp_parser_save_member_function_body (parser,
16651 decl_specifiers,
16652 declarator,
16653 prefix_attributes);
16654 else
16655 decl =
16656 (cp_parser_function_definition_from_specifiers_and_declarator
16657 (parser, decl_specifiers, prefix_attributes, declarator));
16659 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16661 /* This is where the prologue starts... */
16662 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16663 = func_brace_location;
16666 return decl;
16670 /* [dcl.dcl]
16672 Only in function declarations for constructors, destructors, and
16673 type conversions can the decl-specifier-seq be omitted.
16675 We explicitly postpone this check past the point where we handle
16676 function-definitions because we tolerate function-definitions
16677 that are missing their return types in some modes. */
16678 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16680 cp_parser_error (parser,
16681 "expected constructor, destructor, or type conversion");
16682 return error_mark_node;
16685 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16686 if (token->type == CPP_EQ
16687 || token->type == CPP_OPEN_PAREN
16688 || token->type == CPP_OPEN_BRACE)
16690 is_initialized = SD_INITIALIZED;
16691 initialization_kind = token->type;
16692 if (maybe_range_for_decl)
16693 *maybe_range_for_decl = error_mark_node;
16695 if (token->type == CPP_EQ
16696 && function_declarator_p (declarator))
16698 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16699 if (t2->keyword == RID_DEFAULT)
16700 is_initialized = SD_DEFAULTED;
16701 else if (t2->keyword == RID_DELETE)
16702 is_initialized = SD_DELETED;
16705 else
16707 /* If the init-declarator isn't initialized and isn't followed by a
16708 `,' or `;', it's not a valid init-declarator. */
16709 if (token->type != CPP_COMMA
16710 && token->type != CPP_SEMICOLON)
16712 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16713 range_for_decl_p = true;
16714 else
16716 cp_parser_error (parser, "expected initializer");
16717 return error_mark_node;
16720 is_initialized = SD_UNINITIALIZED;
16721 initialization_kind = CPP_EOF;
16724 /* Because start_decl has side-effects, we should only call it if we
16725 know we're going ahead. By this point, we know that we cannot
16726 possibly be looking at any other construct. */
16727 cp_parser_commit_to_tentative_parse (parser);
16729 /* If the decl specifiers were bad, issue an error now that we're
16730 sure this was intended to be a declarator. Then continue
16731 declaring the variable(s), as int, to try to cut down on further
16732 errors. */
16733 if (decl_specifiers->any_specifiers_p
16734 && decl_specifiers->type == error_mark_node)
16736 cp_parser_error (parser, "invalid type in declaration");
16737 decl_specifiers->type = integer_type_node;
16740 /* Check to see whether or not this declaration is a friend. */
16741 friend_p = cp_parser_friend_p (decl_specifiers);
16743 /* Enter the newly declared entry in the symbol table. If we're
16744 processing a declaration in a class-specifier, we wait until
16745 after processing the initializer. */
16746 if (!member_p)
16748 if (parser->in_unbraced_linkage_specification_p)
16749 decl_specifiers->storage_class = sc_extern;
16750 decl = start_decl (declarator, decl_specifiers,
16751 range_for_decl_p? SD_INITIALIZED : is_initialized,
16752 attributes, prefix_attributes, &pushed_scope);
16753 cp_finalize_omp_declare_simd (parser, decl);
16754 /* Adjust location of decl if declarator->id_loc is more appropriate:
16755 set, and decl wasn't merged with another decl, in which case its
16756 location would be different from input_location, and more accurate. */
16757 if (DECL_P (decl)
16758 && declarator->id_loc != UNKNOWN_LOCATION
16759 && DECL_SOURCE_LOCATION (decl) == input_location)
16760 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16762 else if (scope)
16763 /* Enter the SCOPE. That way unqualified names appearing in the
16764 initializer will be looked up in SCOPE. */
16765 pushed_scope = push_scope (scope);
16767 /* Perform deferred access control checks, now that we know in which
16768 SCOPE the declared entity resides. */
16769 if (!member_p && decl)
16771 tree saved_current_function_decl = NULL_TREE;
16773 /* If the entity being declared is a function, pretend that we
16774 are in its scope. If it is a `friend', it may have access to
16775 things that would not otherwise be accessible. */
16776 if (TREE_CODE (decl) == FUNCTION_DECL)
16778 saved_current_function_decl = current_function_decl;
16779 current_function_decl = decl;
16782 /* Perform access checks for template parameters. */
16783 cp_parser_perform_template_parameter_access_checks (checks);
16785 /* Perform the access control checks for the declarator and the
16786 decl-specifiers. */
16787 perform_deferred_access_checks (tf_warning_or_error);
16789 /* Restore the saved value. */
16790 if (TREE_CODE (decl) == FUNCTION_DECL)
16791 current_function_decl = saved_current_function_decl;
16794 /* Parse the initializer. */
16795 initializer = NULL_TREE;
16796 is_direct_init = false;
16797 is_non_constant_init = true;
16798 if (is_initialized)
16800 if (function_declarator_p (declarator))
16802 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16803 if (initialization_kind == CPP_EQ)
16804 initializer = cp_parser_pure_specifier (parser);
16805 else
16807 /* If the declaration was erroneous, we don't really
16808 know what the user intended, so just silently
16809 consume the initializer. */
16810 if (decl != error_mark_node)
16811 error_at (initializer_start_token->location,
16812 "initializer provided for function");
16813 cp_parser_skip_to_closing_parenthesis (parser,
16814 /*recovering=*/true,
16815 /*or_comma=*/false,
16816 /*consume_paren=*/true);
16819 else
16821 /* We want to record the extra mangling scope for in-class
16822 initializers of class members and initializers of static data
16823 member templates. The former involves deferring
16824 parsing of the initializer until end of class as with default
16825 arguments. So right here we only handle the latter. */
16826 if (!member_p && processing_template_decl)
16827 start_lambda_scope (decl);
16828 initializer = cp_parser_initializer (parser,
16829 &is_direct_init,
16830 &is_non_constant_init);
16831 if (!member_p && processing_template_decl)
16832 finish_lambda_scope ();
16833 if (initializer == error_mark_node)
16834 cp_parser_skip_to_end_of_statement (parser);
16838 /* The old parser allows attributes to appear after a parenthesized
16839 initializer. Mark Mitchell proposed removing this functionality
16840 on the GCC mailing lists on 2002-08-13. This parser accepts the
16841 attributes -- but ignores them. */
16842 if (cp_parser_allow_gnu_extensions_p (parser)
16843 && initialization_kind == CPP_OPEN_PAREN)
16844 if (cp_parser_attributes_opt (parser))
16845 warning (OPT_Wattributes,
16846 "attributes after parenthesized initializer ignored");
16848 /* A non-template declaration involving a function parameter list containing
16849 an implicit template parameter will have been made into a template. If it
16850 turns out that the resulting declaration is not an actual function then
16851 finish the template declaration here. An error message will already have
16852 been issued. */
16853 if (parser->fully_implicit_function_template_p)
16854 if (!function_declarator_p (declarator))
16855 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16857 /* For an in-class declaration, use `grokfield' to create the
16858 declaration. */
16859 if (member_p)
16861 if (pushed_scope)
16863 pop_scope (pushed_scope);
16864 pushed_scope = NULL_TREE;
16866 decl = grokfield (declarator, decl_specifiers,
16867 initializer, !is_non_constant_init,
16868 /*asmspec=*/NULL_TREE,
16869 chainon (attributes, prefix_attributes));
16870 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16871 cp_parser_save_default_args (parser, decl);
16872 cp_finalize_omp_declare_simd (parser, decl);
16875 /* Finish processing the declaration. But, skip member
16876 declarations. */
16877 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16879 cp_finish_decl (decl,
16880 initializer, !is_non_constant_init,
16881 asm_specification,
16882 /* If the initializer is in parentheses, then this is
16883 a direct-initialization, which means that an
16884 `explicit' constructor is OK. Otherwise, an
16885 `explicit' constructor cannot be used. */
16886 ((is_direct_init || !is_initialized)
16887 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16888 /* Check for and warn about self-initialization if -Wself-assign is
16889 enabled. */
16890 if (warn_self_assign && initializer)
16891 check_for_self_assign (input_location, decl, initializer);
16893 else if ((cxx_dialect != cxx98) && friend_p
16894 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16895 /* Core issue #226 (C++0x only): A default template-argument
16896 shall not be specified in a friend class template
16897 declaration. */
16898 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16899 /*is_partial=*/false, /*is_friend_decl=*/1);
16901 if (!friend_p && pushed_scope)
16902 pop_scope (pushed_scope);
16904 if (function_declarator_p (declarator)
16905 && parser->fully_implicit_function_template_p)
16907 if (member_p)
16908 decl = finish_fully_implicit_template (parser, decl);
16909 else
16910 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16913 return decl;
16916 /* Parse a declarator.
16918 declarator:
16919 direct-declarator
16920 ptr-operator declarator
16922 abstract-declarator:
16923 ptr-operator abstract-declarator [opt]
16924 direct-abstract-declarator
16926 GNU Extensions:
16928 declarator:
16929 attributes [opt] direct-declarator
16930 attributes [opt] ptr-operator declarator
16932 abstract-declarator:
16933 attributes [opt] ptr-operator abstract-declarator [opt]
16934 attributes [opt] direct-abstract-declarator
16936 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16937 detect constructor, destructor or conversion operators. It is set
16938 to -1 if the declarator is a name, and +1 if it is a
16939 function. Otherwise it is set to zero. Usually you just want to
16940 test for >0, but internally the negative value is used.
16942 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16943 a decl-specifier-seq unless it declares a constructor, destructor,
16944 or conversion. It might seem that we could check this condition in
16945 semantic analysis, rather than parsing, but that makes it difficult
16946 to handle something like `f()'. We want to notice that there are
16947 no decl-specifiers, and therefore realize that this is an
16948 expression, not a declaration.)
16950 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16951 the declarator is a direct-declarator of the form "(...)".
16953 MEMBER_P is true iff this declarator is a member-declarator. */
16955 static cp_declarator *
16956 cp_parser_declarator (cp_parser* parser,
16957 cp_parser_declarator_kind dcl_kind,
16958 int* ctor_dtor_or_conv_p,
16959 bool* parenthesized_p,
16960 bool member_p)
16962 cp_declarator *declarator;
16963 enum tree_code code;
16964 cp_cv_quals cv_quals;
16965 tree class_type;
16966 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16968 /* Assume this is not a constructor, destructor, or type-conversion
16969 operator. */
16970 if (ctor_dtor_or_conv_p)
16971 *ctor_dtor_or_conv_p = 0;
16973 if (cp_parser_allow_gnu_extensions_p (parser))
16974 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16976 /* Check for the ptr-operator production. */
16977 cp_parser_parse_tentatively (parser);
16978 /* Parse the ptr-operator. */
16979 code = cp_parser_ptr_operator (parser,
16980 &class_type,
16981 &cv_quals,
16982 &std_attributes);
16984 /* If that worked, then we have a ptr-operator. */
16985 if (cp_parser_parse_definitely (parser))
16987 /* If a ptr-operator was found, then this declarator was not
16988 parenthesized. */
16989 if (parenthesized_p)
16990 *parenthesized_p = true;
16991 /* The dependent declarator is optional if we are parsing an
16992 abstract-declarator. */
16993 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16994 cp_parser_parse_tentatively (parser);
16996 /* Parse the dependent declarator. */
16997 declarator = cp_parser_declarator (parser, dcl_kind,
16998 /*ctor_dtor_or_conv_p=*/NULL,
16999 /*parenthesized_p=*/NULL,
17000 /*member_p=*/false);
17002 /* If we are parsing an abstract-declarator, we must handle the
17003 case where the dependent declarator is absent. */
17004 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17005 && !cp_parser_parse_definitely (parser))
17006 declarator = NULL;
17008 declarator = cp_parser_make_indirect_declarator
17009 (code, class_type, cv_quals, declarator, std_attributes);
17011 /* Everything else is a direct-declarator. */
17012 else
17014 if (parenthesized_p)
17015 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17016 CPP_OPEN_PAREN);
17017 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17018 ctor_dtor_or_conv_p,
17019 member_p);
17022 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17023 declarator->attributes = gnu_attributes;
17024 return declarator;
17027 /* Parse a direct-declarator or direct-abstract-declarator.
17029 direct-declarator:
17030 declarator-id
17031 direct-declarator ( parameter-declaration-clause )
17032 cv-qualifier-seq [opt]
17033 ref-qualifier [opt]
17034 exception-specification [opt]
17035 direct-declarator [ constant-expression [opt] ]
17036 ( declarator )
17038 direct-abstract-declarator:
17039 direct-abstract-declarator [opt]
17040 ( parameter-declaration-clause )
17041 cv-qualifier-seq [opt]
17042 ref-qualifier [opt]
17043 exception-specification [opt]
17044 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17045 ( abstract-declarator )
17047 Returns a representation of the declarator. DCL_KIND is
17048 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17049 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17050 we are parsing a direct-declarator. It is
17051 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17052 of ambiguity we prefer an abstract declarator, as per
17053 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17054 cp_parser_declarator. */
17056 static cp_declarator *
17057 cp_parser_direct_declarator (cp_parser* parser,
17058 cp_parser_declarator_kind dcl_kind,
17059 int* ctor_dtor_or_conv_p,
17060 bool member_p)
17062 cp_token *token;
17063 cp_declarator *declarator = NULL;
17064 tree scope = NULL_TREE;
17065 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17066 bool saved_in_declarator_p = parser->in_declarator_p;
17067 bool first = true;
17068 tree pushed_scope = NULL_TREE;
17070 while (true)
17072 /* Peek at the next token. */
17073 token = cp_lexer_peek_token (parser->lexer);
17074 if (token->type == CPP_OPEN_PAREN)
17076 /* This is either a parameter-declaration-clause, or a
17077 parenthesized declarator. When we know we are parsing a
17078 named declarator, it must be a parenthesized declarator
17079 if FIRST is true. For instance, `(int)' is a
17080 parameter-declaration-clause, with an omitted
17081 direct-abstract-declarator. But `((*))', is a
17082 parenthesized abstract declarator. Finally, when T is a
17083 template parameter `(T)' is a
17084 parameter-declaration-clause, and not a parenthesized
17085 named declarator.
17087 We first try and parse a parameter-declaration-clause,
17088 and then try a nested declarator (if FIRST is true).
17090 It is not an error for it not to be a
17091 parameter-declaration-clause, even when FIRST is
17092 false. Consider,
17094 int i (int);
17095 int i (3);
17097 The first is the declaration of a function while the
17098 second is the definition of a variable, including its
17099 initializer.
17101 Having seen only the parenthesis, we cannot know which of
17102 these two alternatives should be selected. Even more
17103 complex are examples like:
17105 int i (int (a));
17106 int i (int (3));
17108 The former is a function-declaration; the latter is a
17109 variable initialization.
17111 Thus again, we try a parameter-declaration-clause, and if
17112 that fails, we back out and return. */
17114 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17116 tree params;
17117 bool is_declarator = false;
17119 /* In a member-declarator, the only valid interpretation
17120 of a parenthesis is the start of a
17121 parameter-declaration-clause. (It is invalid to
17122 initialize a static data member with a parenthesized
17123 initializer; only the "=" form of initialization is
17124 permitted.) */
17125 if (!member_p)
17126 cp_parser_parse_tentatively (parser);
17128 /* Consume the `('. */
17129 cp_lexer_consume_token (parser->lexer);
17130 if (first)
17132 /* If this is going to be an abstract declarator, we're
17133 in a declarator and we can't have default args. */
17134 parser->default_arg_ok_p = false;
17135 parser->in_declarator_p = true;
17138 begin_scope (sk_function_parms, NULL_TREE);
17140 /* Parse the parameter-declaration-clause. */
17141 params = cp_parser_parameter_declaration_clause (parser);
17143 /* Consume the `)'. */
17144 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17146 /* If all went well, parse the cv-qualifier-seq,
17147 ref-qualifier and the exception-specification. */
17148 if (member_p || cp_parser_parse_definitely (parser))
17150 cp_cv_quals cv_quals;
17151 cp_virt_specifiers virt_specifiers;
17152 cp_ref_qualifier ref_qual;
17153 tree exception_specification;
17154 tree late_return;
17155 tree attrs;
17156 bool memfn = (member_p || (pushed_scope
17157 && CLASS_TYPE_P (pushed_scope)));
17159 is_declarator = true;
17161 if (ctor_dtor_or_conv_p)
17162 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17163 first = false;
17165 /* Parse the cv-qualifier-seq. */
17166 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17167 /* Parse the ref-qualifier. */
17168 ref_qual = cp_parser_ref_qualifier_opt (parser);
17169 /* And the exception-specification. */
17170 exception_specification
17171 = cp_parser_exception_specification_opt (parser);
17173 attrs = cp_parser_std_attribute_spec_seq (parser);
17175 /* In here, we handle cases where attribute is used after
17176 the function declaration. For example:
17177 void func (int x) __attribute__((vector(..))); */
17178 if (flag_cilkplus
17179 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17181 cp_parser_parse_tentatively (parser);
17182 tree attr = cp_parser_gnu_attributes_opt (parser);
17183 if (cp_lexer_next_token_is_not (parser->lexer,
17184 CPP_SEMICOLON)
17185 && cp_lexer_next_token_is_not (parser->lexer,
17186 CPP_OPEN_BRACE))
17187 cp_parser_abort_tentative_parse (parser);
17188 else if (!cp_parser_parse_definitely (parser))
17190 else
17191 attrs = chainon (attr, attrs);
17193 late_return = (cp_parser_late_return_type_opt
17194 (parser, declarator,
17195 memfn ? cv_quals : -1));
17198 /* Parse the virt-specifier-seq. */
17199 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17201 /* Create the function-declarator. */
17202 declarator = make_call_declarator (declarator,
17203 params,
17204 cv_quals,
17205 virt_specifiers,
17206 ref_qual,
17207 exception_specification,
17208 late_return);
17209 declarator->std_attributes = attrs;
17210 /* Any subsequent parameter lists are to do with
17211 return type, so are not those of the declared
17212 function. */
17213 parser->default_arg_ok_p = false;
17216 /* Remove the function parms from scope. */
17217 pop_bindings_and_leave_scope ();
17219 if (is_declarator)
17220 /* Repeat the main loop. */
17221 continue;
17224 /* If this is the first, we can try a parenthesized
17225 declarator. */
17226 if (first)
17228 bool saved_in_type_id_in_expr_p;
17230 parser->default_arg_ok_p = saved_default_arg_ok_p;
17231 parser->in_declarator_p = saved_in_declarator_p;
17233 /* Consume the `('. */
17234 cp_lexer_consume_token (parser->lexer);
17235 /* Parse the nested declarator. */
17236 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17237 parser->in_type_id_in_expr_p = true;
17238 declarator
17239 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17240 /*parenthesized_p=*/NULL,
17241 member_p);
17242 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17243 first = false;
17244 /* Expect a `)'. */
17245 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17246 declarator = cp_error_declarator;
17247 if (declarator == cp_error_declarator)
17248 break;
17250 goto handle_declarator;
17252 /* Otherwise, we must be done. */
17253 else
17254 break;
17256 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17257 && token->type == CPP_OPEN_SQUARE
17258 && !cp_next_tokens_can_be_attribute_p (parser))
17260 /* Parse an array-declarator. */
17261 tree bounds, attrs;
17263 if (ctor_dtor_or_conv_p)
17264 *ctor_dtor_or_conv_p = 0;
17266 first = false;
17267 parser->default_arg_ok_p = false;
17268 parser->in_declarator_p = true;
17269 /* Consume the `['. */
17270 cp_lexer_consume_token (parser->lexer);
17271 /* Peek at the next token. */
17272 token = cp_lexer_peek_token (parser->lexer);
17273 /* If the next token is `]', then there is no
17274 constant-expression. */
17275 if (token->type != CPP_CLOSE_SQUARE)
17277 bool non_constant_p;
17278 bounds
17279 = cp_parser_constant_expression (parser,
17280 /*allow_non_constant=*/true,
17281 &non_constant_p);
17282 if (!non_constant_p)
17283 /* OK */;
17284 else if (error_operand_p (bounds))
17285 /* Already gave an error. */;
17286 else if (!parser->in_function_body
17287 || current_binding_level->kind == sk_function_parms)
17289 /* Normally, the array bound must be an integral constant
17290 expression. However, as an extension, we allow VLAs
17291 in function scopes as long as they aren't part of a
17292 parameter declaration. */
17293 cp_parser_error (parser,
17294 "array bound is not an integer constant");
17295 bounds = error_mark_node;
17297 else if (processing_template_decl
17298 && !type_dependent_expression_p (bounds))
17300 /* Remember this wasn't a constant-expression. */
17301 bounds = build_nop (TREE_TYPE (bounds), bounds);
17302 TREE_SIDE_EFFECTS (bounds) = 1;
17305 else
17306 bounds = NULL_TREE;
17307 /* Look for the closing `]'. */
17308 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17310 declarator = cp_error_declarator;
17311 break;
17314 attrs = cp_parser_std_attribute_spec_seq (parser);
17315 declarator = make_array_declarator (declarator, bounds);
17316 declarator->std_attributes = attrs;
17318 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17321 tree qualifying_scope;
17322 tree unqualified_name;
17323 tree attrs;
17324 special_function_kind sfk;
17325 bool abstract_ok;
17326 bool pack_expansion_p = false;
17327 cp_token *declarator_id_start_token;
17329 /* Parse a declarator-id */
17330 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17331 if (abstract_ok)
17333 cp_parser_parse_tentatively (parser);
17335 /* If we see an ellipsis, we should be looking at a
17336 parameter pack. */
17337 if (token->type == CPP_ELLIPSIS)
17339 /* Consume the `...' */
17340 cp_lexer_consume_token (parser->lexer);
17342 pack_expansion_p = true;
17346 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17347 unqualified_name
17348 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17349 qualifying_scope = parser->scope;
17350 if (abstract_ok)
17352 bool okay = false;
17354 if (!unqualified_name && pack_expansion_p)
17356 /* Check whether an error occurred. */
17357 okay = !cp_parser_error_occurred (parser);
17359 /* We already consumed the ellipsis to mark a
17360 parameter pack, but we have no way to report it,
17361 so abort the tentative parse. We will be exiting
17362 immediately anyway. */
17363 cp_parser_abort_tentative_parse (parser);
17365 else
17366 okay = cp_parser_parse_definitely (parser);
17368 if (!okay)
17369 unqualified_name = error_mark_node;
17370 else if (unqualified_name
17371 && (qualifying_scope
17372 || (!identifier_p (unqualified_name))))
17374 cp_parser_error (parser, "expected unqualified-id");
17375 unqualified_name = error_mark_node;
17379 if (!unqualified_name)
17380 return NULL;
17381 if (unqualified_name == error_mark_node)
17383 declarator = cp_error_declarator;
17384 pack_expansion_p = false;
17385 declarator->parameter_pack_p = false;
17386 break;
17389 attrs = cp_parser_std_attribute_spec_seq (parser);
17391 if (qualifying_scope && at_namespace_scope_p ()
17392 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17394 /* In the declaration of a member of a template class
17395 outside of the class itself, the SCOPE will sometimes
17396 be a TYPENAME_TYPE. For example, given:
17398 template <typename T>
17399 int S<T>::R::i = 3;
17401 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17402 this context, we must resolve S<T>::R to an ordinary
17403 type, rather than a typename type.
17405 The reason we normally avoid resolving TYPENAME_TYPEs
17406 is that a specialization of `S' might render
17407 `S<T>::R' not a type. However, if `S' is
17408 specialized, then this `i' will not be used, so there
17409 is no harm in resolving the types here. */
17410 tree type;
17412 /* Resolve the TYPENAME_TYPE. */
17413 type = resolve_typename_type (qualifying_scope,
17414 /*only_current_p=*/false);
17415 /* If that failed, the declarator is invalid. */
17416 if (TREE_CODE (type) == TYPENAME_TYPE)
17418 if (typedef_variant_p (type))
17419 error_at (declarator_id_start_token->location,
17420 "cannot define member of dependent typedef "
17421 "%qT", type);
17422 else
17423 error_at (declarator_id_start_token->location,
17424 "%<%T::%E%> is not a type",
17425 TYPE_CONTEXT (qualifying_scope),
17426 TYPE_IDENTIFIER (qualifying_scope));
17428 qualifying_scope = type;
17431 sfk = sfk_none;
17433 if (unqualified_name)
17435 tree class_type;
17437 if (qualifying_scope
17438 && CLASS_TYPE_P (qualifying_scope))
17439 class_type = qualifying_scope;
17440 else
17441 class_type = current_class_type;
17443 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17445 tree name_type = TREE_TYPE (unqualified_name);
17446 if (class_type && same_type_p (name_type, class_type))
17448 if (qualifying_scope
17449 && CLASSTYPE_USE_TEMPLATE (name_type))
17451 error_at (declarator_id_start_token->location,
17452 "invalid use of constructor as a template");
17453 inform (declarator_id_start_token->location,
17454 "use %<%T::%D%> instead of %<%T::%D%> to "
17455 "name the constructor in a qualified name",
17456 class_type,
17457 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17458 class_type, name_type);
17459 declarator = cp_error_declarator;
17460 break;
17462 else
17463 unqualified_name = constructor_name (class_type);
17465 else
17467 /* We do not attempt to print the declarator
17468 here because we do not have enough
17469 information about its original syntactic
17470 form. */
17471 cp_parser_error (parser, "invalid declarator");
17472 declarator = cp_error_declarator;
17473 break;
17477 if (class_type)
17479 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17480 sfk = sfk_destructor;
17481 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17482 sfk = sfk_conversion;
17483 else if (/* There's no way to declare a constructor
17484 for an anonymous type, even if the type
17485 got a name for linkage purposes. */
17486 !TYPE_WAS_ANONYMOUS (class_type)
17487 && constructor_name_p (unqualified_name,
17488 class_type))
17490 unqualified_name = constructor_name (class_type);
17491 sfk = sfk_constructor;
17493 else if (is_overloaded_fn (unqualified_name)
17494 && DECL_CONSTRUCTOR_P (get_first_fn
17495 (unqualified_name)))
17496 sfk = sfk_constructor;
17498 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17499 *ctor_dtor_or_conv_p = -1;
17502 declarator = make_id_declarator (qualifying_scope,
17503 unqualified_name,
17504 sfk);
17505 declarator->std_attributes = attrs;
17506 declarator->id_loc = token->location;
17507 declarator->parameter_pack_p = pack_expansion_p;
17509 if (pack_expansion_p)
17510 maybe_warn_variadic_templates ();
17513 handle_declarator:;
17514 scope = get_scope_of_declarator (declarator);
17515 if (scope)
17517 /* Any names that appear after the declarator-id for a
17518 member are looked up in the containing scope. */
17519 if (at_function_scope_p ())
17521 /* But declarations with qualified-ids can't appear in a
17522 function. */
17523 cp_parser_error (parser, "qualified-id in declaration");
17524 declarator = cp_error_declarator;
17525 break;
17527 pushed_scope = push_scope (scope);
17529 parser->in_declarator_p = true;
17530 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17531 || (declarator && declarator->kind == cdk_id))
17532 /* Default args are only allowed on function
17533 declarations. */
17534 parser->default_arg_ok_p = saved_default_arg_ok_p;
17535 else
17536 parser->default_arg_ok_p = false;
17538 first = false;
17540 /* We're done. */
17541 else
17542 break;
17545 /* For an abstract declarator, we might wind up with nothing at this
17546 point. That's an error; the declarator is not optional. */
17547 if (!declarator)
17548 cp_parser_error (parser, "expected declarator");
17550 /* If we entered a scope, we must exit it now. */
17551 if (pushed_scope)
17552 pop_scope (pushed_scope);
17554 parser->default_arg_ok_p = saved_default_arg_ok_p;
17555 parser->in_declarator_p = saved_in_declarator_p;
17557 return declarator;
17560 /* Parse a ptr-operator.
17562 ptr-operator:
17563 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17564 * cv-qualifier-seq [opt]
17566 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17567 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17569 GNU Extension:
17571 ptr-operator:
17572 & cv-qualifier-seq [opt]
17574 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17575 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17576 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17577 filled in with the TYPE containing the member. *CV_QUALS is
17578 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17579 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17580 Note that the tree codes returned by this function have nothing
17581 to do with the types of trees that will be eventually be created
17582 to represent the pointer or reference type being parsed. They are
17583 just constants with suggestive names. */
17584 static enum tree_code
17585 cp_parser_ptr_operator (cp_parser* parser,
17586 tree* type,
17587 cp_cv_quals *cv_quals,
17588 tree *attributes)
17590 enum tree_code code = ERROR_MARK;
17591 cp_token *token;
17592 tree attrs = NULL_TREE;
17594 /* Assume that it's not a pointer-to-member. */
17595 *type = NULL_TREE;
17596 /* And that there are no cv-qualifiers. */
17597 *cv_quals = TYPE_UNQUALIFIED;
17599 /* Peek at the next token. */
17600 token = cp_lexer_peek_token (parser->lexer);
17602 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17603 if (token->type == CPP_MULT)
17604 code = INDIRECT_REF;
17605 else if (token->type == CPP_AND)
17606 code = ADDR_EXPR;
17607 else if ((cxx_dialect != cxx98) &&
17608 token->type == CPP_AND_AND) /* C++0x only */
17609 code = NON_LVALUE_EXPR;
17611 if (code != ERROR_MARK)
17613 /* Consume the `*', `&' or `&&'. */
17614 cp_lexer_consume_token (parser->lexer);
17616 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17617 `&', if we are allowing GNU extensions. (The only qualifier
17618 that can legally appear after `&' is `restrict', but that is
17619 enforced during semantic analysis. */
17620 if (code == INDIRECT_REF
17621 || cp_parser_allow_gnu_extensions_p (parser))
17622 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17624 attrs = cp_parser_std_attribute_spec_seq (parser);
17625 if (attributes != NULL)
17626 *attributes = attrs;
17628 else
17630 /* Try the pointer-to-member case. */
17631 cp_parser_parse_tentatively (parser);
17632 /* Look for the optional `::' operator. */
17633 cp_parser_global_scope_opt (parser,
17634 /*current_scope_valid_p=*/false);
17635 /* Look for the nested-name specifier. */
17636 token = cp_lexer_peek_token (parser->lexer);
17637 cp_parser_nested_name_specifier (parser,
17638 /*typename_keyword_p=*/false,
17639 /*check_dependency_p=*/true,
17640 /*type_p=*/false,
17641 /*is_declaration=*/false);
17642 /* If we found it, and the next token is a `*', then we are
17643 indeed looking at a pointer-to-member operator. */
17644 if (!cp_parser_error_occurred (parser)
17645 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17647 /* Indicate that the `*' operator was used. */
17648 code = INDIRECT_REF;
17650 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17651 error_at (token->location, "%qD is a namespace", parser->scope);
17652 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17653 error_at (token->location, "cannot form pointer to member of "
17654 "non-class %q#T", parser->scope);
17655 else
17657 /* The type of which the member is a member is given by the
17658 current SCOPE. */
17659 *type = parser->scope;
17660 /* The next name will not be qualified. */
17661 parser->scope = NULL_TREE;
17662 parser->qualifying_scope = NULL_TREE;
17663 parser->object_scope = NULL_TREE;
17664 /* Look for optional c++11 attributes. */
17665 attrs = cp_parser_std_attribute_spec_seq (parser);
17666 if (attributes != NULL)
17667 *attributes = attrs;
17668 /* Look for the optional cv-qualifier-seq. */
17669 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17672 /* If that didn't work we don't have a ptr-operator. */
17673 if (!cp_parser_parse_definitely (parser))
17674 cp_parser_error (parser, "expected ptr-operator");
17677 return code;
17680 /* Parse an (optional) cv-qualifier-seq.
17682 cv-qualifier-seq:
17683 cv-qualifier cv-qualifier-seq [opt]
17685 cv-qualifier:
17686 const
17687 volatile
17689 GNU Extension:
17691 cv-qualifier:
17692 __restrict__
17694 Returns a bitmask representing the cv-qualifiers. */
17696 static cp_cv_quals
17697 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17699 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17701 while (true)
17703 cp_token *token;
17704 cp_cv_quals cv_qualifier;
17706 /* Peek at the next token. */
17707 token = cp_lexer_peek_token (parser->lexer);
17708 /* See if it's a cv-qualifier. */
17709 switch (token->keyword)
17711 case RID_CONST:
17712 cv_qualifier = TYPE_QUAL_CONST;
17713 break;
17715 case RID_VOLATILE:
17716 cv_qualifier = TYPE_QUAL_VOLATILE;
17717 break;
17719 case RID_RESTRICT:
17720 cv_qualifier = TYPE_QUAL_RESTRICT;
17721 break;
17723 default:
17724 cv_qualifier = TYPE_UNQUALIFIED;
17725 break;
17728 if (!cv_qualifier)
17729 break;
17731 if (cv_quals & cv_qualifier)
17733 error_at (token->location, "duplicate cv-qualifier");
17734 cp_lexer_purge_token (parser->lexer);
17736 else
17738 cp_lexer_consume_token (parser->lexer);
17739 cv_quals |= cv_qualifier;
17743 return cv_quals;
17746 /* Parse an (optional) ref-qualifier
17748 ref-qualifier:
17752 Returns cp_ref_qualifier representing ref-qualifier. */
17754 static cp_ref_qualifier
17755 cp_parser_ref_qualifier_opt (cp_parser* parser)
17757 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17759 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17760 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17761 return ref_qual;
17763 while (true)
17765 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17766 cp_token *token = cp_lexer_peek_token (parser->lexer);
17768 switch (token->type)
17770 case CPP_AND:
17771 curr_ref_qual = REF_QUAL_LVALUE;
17772 break;
17774 case CPP_AND_AND:
17775 curr_ref_qual = REF_QUAL_RVALUE;
17776 break;
17778 default:
17779 curr_ref_qual = REF_QUAL_NONE;
17780 break;
17783 if (!curr_ref_qual)
17784 break;
17785 else if (ref_qual)
17787 error_at (token->location, "multiple ref-qualifiers");
17788 cp_lexer_purge_token (parser->lexer);
17790 else
17792 ref_qual = curr_ref_qual;
17793 cp_lexer_consume_token (parser->lexer);
17797 return ref_qual;
17800 /* Parse an (optional) virt-specifier-seq.
17802 virt-specifier-seq:
17803 virt-specifier virt-specifier-seq [opt]
17805 virt-specifier:
17806 override
17807 final
17809 Returns a bitmask representing the virt-specifiers. */
17811 static cp_virt_specifiers
17812 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17814 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17816 while (true)
17818 cp_token *token;
17819 cp_virt_specifiers virt_specifier;
17821 /* Peek at the next token. */
17822 token = cp_lexer_peek_token (parser->lexer);
17823 /* See if it's a virt-specifier-qualifier. */
17824 if (token->type != CPP_NAME)
17825 break;
17826 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17828 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17829 virt_specifier = VIRT_SPEC_OVERRIDE;
17831 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17833 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17834 virt_specifier = VIRT_SPEC_FINAL;
17836 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17838 virt_specifier = VIRT_SPEC_FINAL;
17840 else
17841 break;
17843 if (virt_specifiers & virt_specifier)
17845 error_at (token->location, "duplicate virt-specifier");
17846 cp_lexer_purge_token (parser->lexer);
17848 else
17850 cp_lexer_consume_token (parser->lexer);
17851 virt_specifiers |= virt_specifier;
17854 return virt_specifiers;
17857 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17858 is in scope even though it isn't real. */
17860 static void
17861 inject_this_parameter (tree ctype, cp_cv_quals quals)
17863 tree this_parm;
17865 if (current_class_ptr)
17867 /* We don't clear this between NSDMIs. Is it already what we want? */
17868 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17869 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17870 && cp_type_quals (type) == quals)
17871 return;
17874 this_parm = build_this_parm (ctype, quals);
17875 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17876 current_class_ptr = NULL_TREE;
17877 current_class_ref
17878 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17879 current_class_ptr = this_parm;
17882 /* Return true iff our current scope is a non-static data member
17883 initializer. */
17885 bool
17886 parsing_nsdmi (void)
17888 /* We recognize NSDMI context by the context-less 'this' pointer set up
17889 by the function above. */
17890 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17891 return true;
17892 return false;
17895 /* Parse a late-specified return type, if any. This is not a separate
17896 non-terminal, but part of a function declarator, which looks like
17898 -> trailing-type-specifier-seq abstract-declarator(opt)
17900 Returns the type indicated by the type-id.
17902 In addition to this this parses any queued up omp declare simd
17903 clauses and Cilk Plus SIMD-enabled function's vector attributes.
17905 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17906 function. */
17908 static tree
17909 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17910 cp_cv_quals quals)
17912 cp_token *token;
17913 tree type = NULL_TREE;
17914 bool declare_simd_p = (parser->omp_declare_simd
17915 && declarator
17916 && declarator->kind == cdk_id);
17918 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
17919 && declarator && declarator->kind == cdk_id);
17921 /* Peek at the next token. */
17922 token = cp_lexer_peek_token (parser->lexer);
17923 /* A late-specified return type is indicated by an initial '->'. */
17924 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
17925 return NULL_TREE;
17927 tree save_ccp = current_class_ptr;
17928 tree save_ccr = current_class_ref;
17929 if (quals >= 0)
17931 /* DR 1207: 'this' is in scope in the trailing return type. */
17932 inject_this_parameter (current_class_type, quals);
17935 if (token->type == CPP_DEREF)
17937 /* Consume the ->. */
17938 cp_lexer_consume_token (parser->lexer);
17940 type = cp_parser_trailing_type_id (parser);
17943 if (cilk_simd_fn_vector_p)
17944 declarator->std_attributes
17945 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
17946 declarator->std_attributes);
17947 if (declare_simd_p)
17948 declarator->std_attributes
17949 = cp_parser_late_parsing_omp_declare_simd (parser,
17950 declarator->std_attributes);
17952 if (quals >= 0)
17954 current_class_ptr = save_ccp;
17955 current_class_ref = save_ccr;
17958 return type;
17961 /* Parse a declarator-id.
17963 declarator-id:
17964 id-expression
17965 :: [opt] nested-name-specifier [opt] type-name
17967 In the `id-expression' case, the value returned is as for
17968 cp_parser_id_expression if the id-expression was an unqualified-id.
17969 If the id-expression was a qualified-id, then a SCOPE_REF is
17970 returned. The first operand is the scope (either a NAMESPACE_DECL
17971 or TREE_TYPE), but the second is still just a representation of an
17972 unqualified-id. */
17974 static tree
17975 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17977 tree id;
17978 /* The expression must be an id-expression. Assume that qualified
17979 names are the names of types so that:
17981 template <class T>
17982 int S<T>::R::i = 3;
17984 will work; we must treat `S<T>::R' as the name of a type.
17985 Similarly, assume that qualified names are templates, where
17986 required, so that:
17988 template <class T>
17989 int S<T>::R<T>::i = 3;
17991 will work, too. */
17992 id = cp_parser_id_expression (parser,
17993 /*template_keyword_p=*/false,
17994 /*check_dependency_p=*/false,
17995 /*template_p=*/NULL,
17996 /*declarator_p=*/true,
17997 optional_p);
17998 if (id && BASELINK_P (id))
17999 id = BASELINK_FUNCTIONS (id);
18000 return id;
18003 /* Parse a type-id.
18005 type-id:
18006 type-specifier-seq abstract-declarator [opt]
18008 Returns the TYPE specified. */
18010 static tree
18011 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18012 bool is_trailing_return)
18014 cp_decl_specifier_seq type_specifier_seq;
18015 cp_declarator *abstract_declarator;
18017 /* Parse the type-specifier-seq. */
18018 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18019 is_trailing_return,
18020 &type_specifier_seq);
18021 if (type_specifier_seq.type == error_mark_node)
18022 return error_mark_node;
18024 /* There might or might not be an abstract declarator. */
18025 cp_parser_parse_tentatively (parser);
18026 /* Look for the declarator. */
18027 abstract_declarator
18028 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18029 /*parenthesized_p=*/NULL,
18030 /*member_p=*/false);
18031 /* Check to see if there really was a declarator. */
18032 if (!cp_parser_parse_definitely (parser))
18033 abstract_declarator = NULL;
18035 if (type_specifier_seq.type
18036 /* None of the valid uses of 'auto' in C++14 involve the type-id
18037 nonterminal, but it is valid in a trailing-return-type. */
18038 && !(cxx_dialect >= cxx1y && is_trailing_return)
18039 && type_uses_auto (type_specifier_seq.type))
18041 /* A type-id with type 'auto' is only ok if the abstract declarator
18042 is a function declarator with a late-specified return type. */
18043 if (abstract_declarator
18044 && abstract_declarator->kind == cdk_function
18045 && abstract_declarator->u.function.late_return_type)
18046 /* OK */;
18047 else
18049 error ("invalid use of %<auto%>");
18050 return error_mark_node;
18054 return groktypename (&type_specifier_seq, abstract_declarator,
18055 is_template_arg);
18058 static tree cp_parser_type_id (cp_parser *parser)
18060 return cp_parser_type_id_1 (parser, false, false);
18063 static tree cp_parser_template_type_arg (cp_parser *parser)
18065 tree r;
18066 const char *saved_message = parser->type_definition_forbidden_message;
18067 parser->type_definition_forbidden_message
18068 = G_("types may not be defined in template arguments");
18069 r = cp_parser_type_id_1 (parser, true, false);
18070 parser->type_definition_forbidden_message = saved_message;
18071 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18073 error ("invalid use of %<auto%> in template argument");
18074 r = error_mark_node;
18076 return r;
18079 static tree cp_parser_trailing_type_id (cp_parser *parser)
18081 return cp_parser_type_id_1 (parser, false, true);
18084 /* Parse a type-specifier-seq.
18086 type-specifier-seq:
18087 type-specifier type-specifier-seq [opt]
18089 GNU extension:
18091 type-specifier-seq:
18092 attributes type-specifier-seq [opt]
18094 If IS_DECLARATION is true, we are at the start of a "condition" or
18095 exception-declaration, so we might be followed by a declarator-id.
18097 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18098 i.e. we've just seen "->".
18100 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18102 static void
18103 cp_parser_type_specifier_seq (cp_parser* parser,
18104 bool is_declaration,
18105 bool is_trailing_return,
18106 cp_decl_specifier_seq *type_specifier_seq)
18108 bool seen_type_specifier = false;
18109 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18110 cp_token *start_token = NULL;
18112 /* Clear the TYPE_SPECIFIER_SEQ. */
18113 clear_decl_specs (type_specifier_seq);
18115 /* In the context of a trailing return type, enum E { } is an
18116 elaborated-type-specifier followed by a function-body, not an
18117 enum-specifier. */
18118 if (is_trailing_return)
18119 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18121 /* Parse the type-specifiers and attributes. */
18122 while (true)
18124 tree type_specifier;
18125 bool is_cv_qualifier;
18127 /* Check for attributes first. */
18128 if (cp_next_tokens_can_be_attribute_p (parser))
18130 type_specifier_seq->attributes =
18131 chainon (type_specifier_seq->attributes,
18132 cp_parser_attributes_opt (parser));
18133 continue;
18136 /* record the token of the beginning of the type specifier seq,
18137 for error reporting purposes*/
18138 if (!start_token)
18139 start_token = cp_lexer_peek_token (parser->lexer);
18141 /* Look for the type-specifier. */
18142 type_specifier = cp_parser_type_specifier (parser,
18143 flags,
18144 type_specifier_seq,
18145 /*is_declaration=*/false,
18146 NULL,
18147 &is_cv_qualifier);
18148 if (!type_specifier)
18150 /* If the first type-specifier could not be found, this is not a
18151 type-specifier-seq at all. */
18152 if (!seen_type_specifier)
18154 /* Set in_declarator_p to avoid skipping to the semicolon. */
18155 int in_decl = parser->in_declarator_p;
18156 parser->in_declarator_p = true;
18158 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18159 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18160 cp_parser_error (parser, "expected type-specifier");
18162 parser->in_declarator_p = in_decl;
18164 type_specifier_seq->type = error_mark_node;
18165 return;
18167 /* If subsequent type-specifiers could not be found, the
18168 type-specifier-seq is complete. */
18169 break;
18172 seen_type_specifier = true;
18173 /* The standard says that a condition can be:
18175 type-specifier-seq declarator = assignment-expression
18177 However, given:
18179 struct S {};
18180 if (int S = ...)
18182 we should treat the "S" as a declarator, not as a
18183 type-specifier. The standard doesn't say that explicitly for
18184 type-specifier-seq, but it does say that for
18185 decl-specifier-seq in an ordinary declaration. Perhaps it
18186 would be clearer just to allow a decl-specifier-seq here, and
18187 then add a semantic restriction that if any decl-specifiers
18188 that are not type-specifiers appear, the program is invalid. */
18189 if (is_declaration && !is_cv_qualifier)
18190 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18194 /* Return whether the function currently being declared has an associated
18195 template parameter list. */
18197 static bool
18198 function_being_declared_is_template_p (cp_parser* parser)
18200 if (!current_template_parms || processing_template_parmlist)
18201 return false;
18203 if (parser->implicit_template_scope)
18204 return true;
18206 if (at_class_scope_p ()
18207 && TYPE_BEING_DEFINED (current_class_type))
18208 return parser->num_template_parameter_lists != 0;
18210 return ((int) parser->num_template_parameter_lists > template_class_depth
18211 (current_class_type));
18214 /* Parse a parameter-declaration-clause.
18216 parameter-declaration-clause:
18217 parameter-declaration-list [opt] ... [opt]
18218 parameter-declaration-list , ...
18220 Returns a representation for the parameter declarations. A return
18221 value of NULL indicates a parameter-declaration-clause consisting
18222 only of an ellipsis. */
18224 static tree
18225 cp_parser_parameter_declaration_clause (cp_parser* parser)
18227 tree parameters;
18228 cp_token *token;
18229 bool ellipsis_p;
18230 bool is_error;
18232 struct cleanup {
18233 cp_parser* parser;
18234 int auto_is_implicit_function_template_parm_p;
18235 ~cleanup() {
18236 parser->auto_is_implicit_function_template_parm_p
18237 = auto_is_implicit_function_template_parm_p;
18239 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18241 (void) cleanup;
18243 if (!processing_specialization
18244 && !processing_template_parmlist
18245 && !processing_explicit_instantiation)
18246 if (!current_function_decl
18247 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18248 parser->auto_is_implicit_function_template_parm_p = true;
18250 /* Peek at the next token. */
18251 token = cp_lexer_peek_token (parser->lexer);
18252 /* Check for trivial parameter-declaration-clauses. */
18253 if (token->type == CPP_ELLIPSIS)
18255 /* Consume the `...' token. */
18256 cp_lexer_consume_token (parser->lexer);
18257 return NULL_TREE;
18259 else if (token->type == CPP_CLOSE_PAREN)
18260 /* There are no parameters. */
18262 #ifndef NO_IMPLICIT_EXTERN_C
18263 if (in_system_header_at (input_location)
18264 && current_class_type == NULL
18265 && current_lang_name == lang_name_c)
18266 return NULL_TREE;
18267 else
18268 #endif
18269 return void_list_node;
18271 /* Check for `(void)', too, which is a special case. */
18272 else if (token->keyword == RID_VOID
18273 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18274 == CPP_CLOSE_PAREN))
18276 /* Consume the `void' token. */
18277 cp_lexer_consume_token (parser->lexer);
18278 /* There are no parameters. */
18279 return void_list_node;
18282 /* Parse the parameter-declaration-list. */
18283 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18284 /* If a parse error occurred while parsing the
18285 parameter-declaration-list, then the entire
18286 parameter-declaration-clause is erroneous. */
18287 if (is_error)
18288 return NULL;
18290 /* Peek at the next token. */
18291 token = cp_lexer_peek_token (parser->lexer);
18292 /* If it's a `,', the clause should terminate with an ellipsis. */
18293 if (token->type == CPP_COMMA)
18295 /* Consume the `,'. */
18296 cp_lexer_consume_token (parser->lexer);
18297 /* Expect an ellipsis. */
18298 ellipsis_p
18299 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18301 /* It might also be `...' if the optional trailing `,' was
18302 omitted. */
18303 else if (token->type == CPP_ELLIPSIS)
18305 /* Consume the `...' token. */
18306 cp_lexer_consume_token (parser->lexer);
18307 /* And remember that we saw it. */
18308 ellipsis_p = true;
18310 else
18311 ellipsis_p = false;
18313 /* Finish the parameter list. */
18314 if (!ellipsis_p)
18315 parameters = chainon (parameters, void_list_node);
18317 return parameters;
18320 /* Parse a parameter-declaration-list.
18322 parameter-declaration-list:
18323 parameter-declaration
18324 parameter-declaration-list , parameter-declaration
18326 Returns a representation of the parameter-declaration-list, as for
18327 cp_parser_parameter_declaration_clause. However, the
18328 `void_list_node' is never appended to the list. Upon return,
18329 *IS_ERROR will be true iff an error occurred. */
18331 static tree
18332 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18334 tree parameters = NULL_TREE;
18335 tree *tail = &parameters;
18336 bool saved_in_unbraced_linkage_specification_p;
18337 int index = 0;
18339 /* Assume all will go well. */
18340 *is_error = false;
18341 /* The special considerations that apply to a function within an
18342 unbraced linkage specifications do not apply to the parameters
18343 to the function. */
18344 saved_in_unbraced_linkage_specification_p
18345 = parser->in_unbraced_linkage_specification_p;
18346 parser->in_unbraced_linkage_specification_p = false;
18348 /* Look for more parameters. */
18349 while (true)
18351 cp_parameter_declarator *parameter;
18352 tree decl = error_mark_node;
18353 bool parenthesized_p = false;
18354 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18355 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18356 (current_template_parms)) : 0);
18358 /* Parse the parameter. */
18359 parameter
18360 = cp_parser_parameter_declaration (parser,
18361 /*template_parm_p=*/false,
18362 &parenthesized_p);
18364 /* We don't know yet if the enclosing context is deprecated, so wait
18365 and warn in grokparms if appropriate. */
18366 deprecated_state = DEPRECATED_SUPPRESS;
18368 if (parameter)
18370 /* If a function parameter pack was specified and an implicit template
18371 parameter was introduced during cp_parser_parameter_declaration,
18372 change any implicit parameters introduced into packs. */
18373 if (parser->implicit_template_parms
18374 && parameter->declarator
18375 && parameter->declarator->parameter_pack_p)
18377 int latest_template_parm_idx = TREE_VEC_LENGTH
18378 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18380 if (latest_template_parm_idx != template_parm_idx)
18381 parameter->decl_specifiers.type = convert_generic_types_to_packs
18382 (parameter->decl_specifiers.type,
18383 template_parm_idx, latest_template_parm_idx);
18386 decl = grokdeclarator (parameter->declarator,
18387 &parameter->decl_specifiers,
18388 PARM,
18389 parameter->default_argument != NULL_TREE,
18390 &parameter->decl_specifiers.attributes);
18393 deprecated_state = DEPRECATED_NORMAL;
18395 /* If a parse error occurred parsing the parameter declaration,
18396 then the entire parameter-declaration-list is erroneous. */
18397 if (decl == error_mark_node)
18399 *is_error = true;
18400 parameters = error_mark_node;
18401 break;
18404 if (parameter->decl_specifiers.attributes)
18405 cplus_decl_attributes (&decl,
18406 parameter->decl_specifiers.attributes,
18408 if (DECL_NAME (decl))
18409 decl = pushdecl (decl);
18411 if (decl != error_mark_node)
18413 retrofit_lang_decl (decl);
18414 DECL_PARM_INDEX (decl) = ++index;
18415 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18418 /* Add the new parameter to the list. */
18419 *tail = build_tree_list (parameter->default_argument, decl);
18420 tail = &TREE_CHAIN (*tail);
18422 /* Peek at the next token. */
18423 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18424 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18425 /* These are for Objective-C++ */
18426 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18427 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18428 /* The parameter-declaration-list is complete. */
18429 break;
18430 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18432 cp_token *token;
18434 /* Peek at the next token. */
18435 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18436 /* If it's an ellipsis, then the list is complete. */
18437 if (token->type == CPP_ELLIPSIS)
18438 break;
18439 /* Otherwise, there must be more parameters. Consume the
18440 `,'. */
18441 cp_lexer_consume_token (parser->lexer);
18442 /* When parsing something like:
18444 int i(float f, double d)
18446 we can tell after seeing the declaration for "f" that we
18447 are not looking at an initialization of a variable "i",
18448 but rather at the declaration of a function "i".
18450 Due to the fact that the parsing of template arguments
18451 (as specified to a template-id) requires backtracking we
18452 cannot use this technique when inside a template argument
18453 list. */
18454 if (!parser->in_template_argument_list_p
18455 && !parser->in_type_id_in_expr_p
18456 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18457 /* However, a parameter-declaration of the form
18458 "float(f)" (which is a valid declaration of a
18459 parameter "f") can also be interpreted as an
18460 expression (the conversion of "f" to "float"). */
18461 && !parenthesized_p)
18462 cp_parser_commit_to_tentative_parse (parser);
18464 else
18466 cp_parser_error (parser, "expected %<,%> or %<...%>");
18467 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18468 cp_parser_skip_to_closing_parenthesis (parser,
18469 /*recovering=*/true,
18470 /*or_comma=*/false,
18471 /*consume_paren=*/false);
18472 break;
18476 parser->in_unbraced_linkage_specification_p
18477 = saved_in_unbraced_linkage_specification_p;
18479 /* Reset implicit_template_scope if we are about to leave the function
18480 parameter list that introduced it. Note that for out-of-line member
18481 definitions, there will be one or more class scopes before we get to
18482 the template parameter scope. */
18484 if (cp_binding_level *its = parser->implicit_template_scope)
18485 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18487 while (maybe_its->kind == sk_class)
18488 maybe_its = maybe_its->level_chain;
18489 if (maybe_its == its)
18491 parser->implicit_template_parms = 0;
18492 parser->implicit_template_scope = 0;
18496 return parameters;
18499 /* Parse a parameter declaration.
18501 parameter-declaration:
18502 decl-specifier-seq ... [opt] declarator
18503 decl-specifier-seq declarator = assignment-expression
18504 decl-specifier-seq ... [opt] abstract-declarator [opt]
18505 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18507 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18508 declares a template parameter. (In that case, a non-nested `>'
18509 token encountered during the parsing of the assignment-expression
18510 is not interpreted as a greater-than operator.)
18512 Returns a representation of the parameter, or NULL if an error
18513 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18514 true iff the declarator is of the form "(p)". */
18516 static cp_parameter_declarator *
18517 cp_parser_parameter_declaration (cp_parser *parser,
18518 bool template_parm_p,
18519 bool *parenthesized_p)
18521 int declares_class_or_enum;
18522 cp_decl_specifier_seq decl_specifiers;
18523 cp_declarator *declarator;
18524 tree default_argument;
18525 cp_token *token = NULL, *declarator_token_start = NULL;
18526 const char *saved_message;
18528 /* In a template parameter, `>' is not an operator.
18530 [temp.param]
18532 When parsing a default template-argument for a non-type
18533 template-parameter, the first non-nested `>' is taken as the end
18534 of the template parameter-list rather than a greater-than
18535 operator. */
18537 /* Type definitions may not appear in parameter types. */
18538 saved_message = parser->type_definition_forbidden_message;
18539 parser->type_definition_forbidden_message
18540 = G_("types may not be defined in parameter types");
18542 /* Parse the declaration-specifiers. */
18543 cp_parser_decl_specifier_seq (parser,
18544 CP_PARSER_FLAGS_NONE,
18545 &decl_specifiers,
18546 &declares_class_or_enum);
18548 /* Complain about missing 'typename' or other invalid type names. */
18549 if (!decl_specifiers.any_type_specifiers_p
18550 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18551 decl_specifiers.type = error_mark_node;
18553 /* If an error occurred, there's no reason to attempt to parse the
18554 rest of the declaration. */
18555 if (cp_parser_error_occurred (parser))
18557 parser->type_definition_forbidden_message = saved_message;
18558 return NULL;
18561 /* Peek at the next token. */
18562 token = cp_lexer_peek_token (parser->lexer);
18564 /* If the next token is a `)', `,', `=', `>', or `...', then there
18565 is no declarator. However, when variadic templates are enabled,
18566 there may be a declarator following `...'. */
18567 if (token->type == CPP_CLOSE_PAREN
18568 || token->type == CPP_COMMA
18569 || token->type == CPP_EQ
18570 || token->type == CPP_GREATER)
18572 declarator = NULL;
18573 if (parenthesized_p)
18574 *parenthesized_p = false;
18576 /* Otherwise, there should be a declarator. */
18577 else
18579 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18580 parser->default_arg_ok_p = false;
18582 /* After seeing a decl-specifier-seq, if the next token is not a
18583 "(", there is no possibility that the code is a valid
18584 expression. Therefore, if parsing tentatively, we commit at
18585 this point. */
18586 if (!parser->in_template_argument_list_p
18587 /* In an expression context, having seen:
18589 (int((char ...
18591 we cannot be sure whether we are looking at a
18592 function-type (taking a "char" as a parameter) or a cast
18593 of some object of type "char" to "int". */
18594 && !parser->in_type_id_in_expr_p
18595 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18596 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18597 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18598 cp_parser_commit_to_tentative_parse (parser);
18599 /* Parse the declarator. */
18600 declarator_token_start = token;
18601 declarator = cp_parser_declarator (parser,
18602 CP_PARSER_DECLARATOR_EITHER,
18603 /*ctor_dtor_or_conv_p=*/NULL,
18604 parenthesized_p,
18605 /*member_p=*/false);
18606 parser->default_arg_ok_p = saved_default_arg_ok_p;
18607 /* After the declarator, allow more attributes. */
18608 decl_specifiers.attributes
18609 = chainon (decl_specifiers.attributes,
18610 cp_parser_attributes_opt (parser));
18613 /* If the next token is an ellipsis, and we have not seen a
18614 declarator name, and the type of the declarator contains parameter
18615 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18616 a parameter pack expansion expression. Otherwise, leave the
18617 ellipsis for a C-style variadic function. */
18618 token = cp_lexer_peek_token (parser->lexer);
18619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18621 tree type = decl_specifiers.type;
18623 if (type && DECL_P (type))
18624 type = TREE_TYPE (type);
18626 if (type
18627 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18628 && declarator_can_be_parameter_pack (declarator)
18629 && (!declarator || !declarator->parameter_pack_p)
18630 && uses_parameter_packs (type))
18632 /* Consume the `...'. */
18633 cp_lexer_consume_token (parser->lexer);
18634 maybe_warn_variadic_templates ();
18636 /* Build a pack expansion type */
18637 if (declarator)
18638 declarator->parameter_pack_p = true;
18639 else
18640 decl_specifiers.type = make_pack_expansion (type);
18644 /* The restriction on defining new types applies only to the type
18645 of the parameter, not to the default argument. */
18646 parser->type_definition_forbidden_message = saved_message;
18648 /* If the next token is `=', then process a default argument. */
18649 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18651 token = cp_lexer_peek_token (parser->lexer);
18652 /* If we are defining a class, then the tokens that make up the
18653 default argument must be saved and processed later. */
18654 if (!template_parm_p && at_class_scope_p ()
18655 && TYPE_BEING_DEFINED (current_class_type)
18656 && !LAMBDA_TYPE_P (current_class_type))
18657 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18658 /* Outside of a class definition, we can just parse the
18659 assignment-expression. */
18660 else
18661 default_argument
18662 = cp_parser_default_argument (parser, template_parm_p);
18664 if (!parser->default_arg_ok_p)
18666 if (flag_permissive)
18667 warning (0, "deprecated use of default argument for parameter of non-function");
18668 else
18670 error_at (token->location,
18671 "default arguments are only "
18672 "permitted for function parameters");
18673 default_argument = NULL_TREE;
18676 else if ((declarator && declarator->parameter_pack_p)
18677 || (decl_specifiers.type
18678 && PACK_EXPANSION_P (decl_specifiers.type)))
18680 /* Find the name of the parameter pack. */
18681 cp_declarator *id_declarator = declarator;
18682 while (id_declarator && id_declarator->kind != cdk_id)
18683 id_declarator = id_declarator->declarator;
18685 if (id_declarator && id_declarator->kind == cdk_id)
18686 error_at (declarator_token_start->location,
18687 template_parm_p
18688 ? G_("template parameter pack %qD "
18689 "cannot have a default argument")
18690 : G_("parameter pack %qD cannot have "
18691 "a default argument"),
18692 id_declarator->u.id.unqualified_name);
18693 else
18694 error_at (declarator_token_start->location,
18695 template_parm_p
18696 ? G_("template parameter pack cannot have "
18697 "a default argument")
18698 : G_("parameter pack cannot have a "
18699 "default argument"));
18701 default_argument = NULL_TREE;
18704 else
18705 default_argument = NULL_TREE;
18707 return make_parameter_declarator (&decl_specifiers,
18708 declarator,
18709 default_argument);
18712 /* Parse a default argument and return it.
18714 TEMPLATE_PARM_P is true if this is a default argument for a
18715 non-type template parameter. */
18716 static tree
18717 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18719 tree default_argument = NULL_TREE;
18720 bool saved_greater_than_is_operator_p;
18721 bool saved_local_variables_forbidden_p;
18722 bool non_constant_p, is_direct_init;
18724 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18725 set correctly. */
18726 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18727 parser->greater_than_is_operator_p = !template_parm_p;
18728 /* Local variable names (and the `this' keyword) may not
18729 appear in a default argument. */
18730 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18731 parser->local_variables_forbidden_p = true;
18732 /* Parse the assignment-expression. */
18733 if (template_parm_p)
18734 push_deferring_access_checks (dk_no_deferred);
18735 tree saved_class_ptr = NULL_TREE;
18736 tree saved_class_ref = NULL_TREE;
18737 /* The "this" pointer is not valid in a default argument. */
18738 if (cfun)
18740 saved_class_ptr = current_class_ptr;
18741 cp_function_chain->x_current_class_ptr = NULL_TREE;
18742 saved_class_ref = current_class_ref;
18743 cp_function_chain->x_current_class_ref = NULL_TREE;
18745 default_argument
18746 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18747 /* Restore the "this" pointer. */
18748 if (cfun)
18750 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18751 cp_function_chain->x_current_class_ref = saved_class_ref;
18753 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18755 if (template_parm_p)
18756 pop_deferring_access_checks ();
18757 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18758 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18760 return default_argument;
18763 /* Parse a function-body.
18765 function-body:
18766 compound_statement */
18768 static void
18769 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18771 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18774 /* Parse a ctor-initializer-opt followed by a function-body. Return
18775 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18776 is true we are parsing a function-try-block. */
18778 static bool
18779 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18780 bool in_function_try_block)
18782 tree body, list;
18783 bool ctor_initializer_p;
18784 const bool check_body_p =
18785 DECL_CONSTRUCTOR_P (current_function_decl)
18786 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18787 tree last = NULL;
18789 /* Begin the function body. */
18790 body = begin_function_body ();
18791 /* Parse the optional ctor-initializer. */
18792 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18794 /* If we're parsing a constexpr constructor definition, we need
18795 to check that the constructor body is indeed empty. However,
18796 before we get to cp_parser_function_body lot of junk has been
18797 generated, so we can't just check that we have an empty block.
18798 Rather we take a snapshot of the outermost block, and check whether
18799 cp_parser_function_body changed its state. */
18800 if (check_body_p)
18802 list = cur_stmt_list;
18803 if (STATEMENT_LIST_TAIL (list))
18804 last = STATEMENT_LIST_TAIL (list)->stmt;
18806 /* Parse the function-body. */
18807 cp_parser_function_body (parser, in_function_try_block);
18808 if (check_body_p)
18809 check_constexpr_ctor_body (last, list);
18810 /* Finish the function body. */
18811 finish_function_body (body);
18813 return ctor_initializer_p;
18816 /* Parse an initializer.
18818 initializer:
18819 = initializer-clause
18820 ( expression-list )
18822 Returns an expression representing the initializer. If no
18823 initializer is present, NULL_TREE is returned.
18825 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18826 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18827 set to TRUE if there is no initializer present. If there is an
18828 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18829 is set to true; otherwise it is set to false. */
18831 static tree
18832 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18833 bool* non_constant_p)
18835 cp_token *token;
18836 tree init;
18838 /* Peek at the next token. */
18839 token = cp_lexer_peek_token (parser->lexer);
18841 /* Let our caller know whether or not this initializer was
18842 parenthesized. */
18843 *is_direct_init = (token->type != CPP_EQ);
18844 /* Assume that the initializer is constant. */
18845 *non_constant_p = false;
18847 if (token->type == CPP_EQ)
18849 /* Consume the `='. */
18850 cp_lexer_consume_token (parser->lexer);
18851 /* Parse the initializer-clause. */
18852 init = cp_parser_initializer_clause (parser, non_constant_p);
18854 else if (token->type == CPP_OPEN_PAREN)
18856 vec<tree, va_gc> *vec;
18857 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18858 /*cast_p=*/false,
18859 /*allow_expansion_p=*/true,
18860 non_constant_p);
18861 if (vec == NULL)
18862 return error_mark_node;
18863 init = build_tree_list_vec (vec);
18864 release_tree_vector (vec);
18866 else if (token->type == CPP_OPEN_BRACE)
18868 cp_lexer_set_source_position (parser->lexer);
18869 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18870 init = cp_parser_braced_list (parser, non_constant_p);
18871 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18873 else
18875 /* Anything else is an error. */
18876 cp_parser_error (parser, "expected initializer");
18877 init = error_mark_node;
18880 return init;
18883 /* Parse an initializer-clause.
18885 initializer-clause:
18886 assignment-expression
18887 braced-init-list
18889 Returns an expression representing the initializer.
18891 If the `assignment-expression' production is used the value
18892 returned is simply a representation for the expression.
18894 Otherwise, calls cp_parser_braced_list. */
18896 static tree
18897 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18899 tree initializer;
18901 /* Assume the expression is constant. */
18902 *non_constant_p = false;
18904 /* If it is not a `{', then we are looking at an
18905 assignment-expression. */
18906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18908 initializer
18909 = cp_parser_constant_expression (parser,
18910 /*allow_non_constant_p=*/true,
18911 non_constant_p);
18913 else
18914 initializer = cp_parser_braced_list (parser, non_constant_p);
18916 return initializer;
18919 /* Parse a brace-enclosed initializer list.
18921 braced-init-list:
18922 { initializer-list , [opt] }
18925 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18926 the elements of the initializer-list (or NULL, if the last
18927 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18928 NULL_TREE. There is no way to detect whether or not the optional
18929 trailing `,' was provided. NON_CONSTANT_P is as for
18930 cp_parser_initializer. */
18932 static tree
18933 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18935 tree initializer;
18937 /* Consume the `{' token. */
18938 cp_lexer_consume_token (parser->lexer);
18939 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18940 initializer = make_node (CONSTRUCTOR);
18941 /* If it's not a `}', then there is a non-trivial initializer. */
18942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18944 /* Parse the initializer list. */
18945 CONSTRUCTOR_ELTS (initializer)
18946 = cp_parser_initializer_list (parser, non_constant_p);
18947 /* A trailing `,' token is allowed. */
18948 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18949 cp_lexer_consume_token (parser->lexer);
18951 else
18952 *non_constant_p = false;
18953 /* Now, there should be a trailing `}'. */
18954 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18955 TREE_TYPE (initializer) = init_list_type_node;
18956 return initializer;
18959 /* Parse an initializer-list.
18961 initializer-list:
18962 initializer-clause ... [opt]
18963 initializer-list , initializer-clause ... [opt]
18965 GNU Extension:
18967 initializer-list:
18968 designation initializer-clause ...[opt]
18969 initializer-list , designation initializer-clause ...[opt]
18971 designation:
18972 . identifier =
18973 identifier :
18974 [ constant-expression ] =
18976 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18977 for the initializer. If the INDEX of the elt is non-NULL, it is the
18978 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18979 as for cp_parser_initializer. */
18981 static vec<constructor_elt, va_gc> *
18982 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18984 vec<constructor_elt, va_gc> *v = NULL;
18986 /* Assume all of the expressions are constant. */
18987 *non_constant_p = false;
18989 /* Parse the rest of the list. */
18990 while (true)
18992 cp_token *token;
18993 tree designator;
18994 tree initializer;
18995 bool clause_non_constant_p;
18997 /* If the next token is an identifier and the following one is a
18998 colon, we are looking at the GNU designated-initializer
18999 syntax. */
19000 if (cp_parser_allow_gnu_extensions_p (parser)
19001 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19002 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19004 /* Warn the user that they are using an extension. */
19005 pedwarn (input_location, OPT_Wpedantic,
19006 "ISO C++ does not allow designated initializers");
19007 /* Consume the identifier. */
19008 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19009 /* Consume the `:'. */
19010 cp_lexer_consume_token (parser->lexer);
19012 /* Also handle the C99 syntax, '. id ='. */
19013 else if (cp_parser_allow_gnu_extensions_p (parser)
19014 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19015 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19016 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19018 /* Warn the user that they are using an extension. */
19019 pedwarn (input_location, OPT_Wpedantic,
19020 "ISO C++ does not allow C99 designated initializers");
19021 /* Consume the `.'. */
19022 cp_lexer_consume_token (parser->lexer);
19023 /* Consume the identifier. */
19024 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19025 /* Consume the `='. */
19026 cp_lexer_consume_token (parser->lexer);
19028 /* Also handle C99 array designators, '[ const ] ='. */
19029 else if (cp_parser_allow_gnu_extensions_p (parser)
19030 && !c_dialect_objc ()
19031 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19033 /* In C++11, [ could start a lambda-introducer. */
19034 bool non_const = false;
19036 cp_parser_parse_tentatively (parser);
19037 cp_lexer_consume_token (parser->lexer);
19038 designator = cp_parser_constant_expression (parser, true, &non_const);
19039 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19040 cp_parser_require (parser, CPP_EQ, RT_EQ);
19041 if (!cp_parser_parse_definitely (parser))
19042 designator = NULL_TREE;
19043 else if (non_const)
19044 require_potential_rvalue_constant_expression (designator);
19046 else
19047 designator = NULL_TREE;
19049 /* Parse the initializer. */
19050 initializer = cp_parser_initializer_clause (parser,
19051 &clause_non_constant_p);
19052 /* If any clause is non-constant, so is the entire initializer. */
19053 if (clause_non_constant_p)
19054 *non_constant_p = true;
19056 /* If we have an ellipsis, this is an initializer pack
19057 expansion. */
19058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19060 /* Consume the `...'. */
19061 cp_lexer_consume_token (parser->lexer);
19063 /* Turn the initializer into an initializer expansion. */
19064 initializer = make_pack_expansion (initializer);
19067 /* Add it to the vector. */
19068 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19070 /* If the next token is not a comma, we have reached the end of
19071 the list. */
19072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19073 break;
19075 /* Peek at the next token. */
19076 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19077 /* If the next token is a `}', then we're still done. An
19078 initializer-clause can have a trailing `,' after the
19079 initializer-list and before the closing `}'. */
19080 if (token->type == CPP_CLOSE_BRACE)
19081 break;
19083 /* Consume the `,' token. */
19084 cp_lexer_consume_token (parser->lexer);
19087 return v;
19090 /* Classes [gram.class] */
19092 /* Parse a class-name.
19094 class-name:
19095 identifier
19096 template-id
19098 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19099 to indicate that names looked up in dependent types should be
19100 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19101 keyword has been used to indicate that the name that appears next
19102 is a template. TAG_TYPE indicates the explicit tag given before
19103 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19104 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19105 is the class being defined in a class-head.
19107 Returns the TYPE_DECL representing the class. */
19109 static tree
19110 cp_parser_class_name (cp_parser *parser,
19111 bool typename_keyword_p,
19112 bool template_keyword_p,
19113 enum tag_types tag_type,
19114 bool check_dependency_p,
19115 bool class_head_p,
19116 bool is_declaration)
19118 tree decl;
19119 tree scope;
19120 bool typename_p;
19121 cp_token *token;
19122 tree identifier = NULL_TREE;
19124 /* All class-names start with an identifier. */
19125 token = cp_lexer_peek_token (parser->lexer);
19126 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19128 cp_parser_error (parser, "expected class-name");
19129 return error_mark_node;
19132 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19133 to a template-id, so we save it here. */
19134 scope = parser->scope;
19135 if (scope == error_mark_node)
19136 return error_mark_node;
19138 /* Any name names a type if we're following the `typename' keyword
19139 in a qualified name where the enclosing scope is type-dependent. */
19140 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19141 && dependent_type_p (scope));
19142 /* Handle the common case (an identifier, but not a template-id)
19143 efficiently. */
19144 if (token->type == CPP_NAME
19145 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19147 cp_token *identifier_token;
19148 bool ambiguous_p;
19150 /* Look for the identifier. */
19151 identifier_token = cp_lexer_peek_token (parser->lexer);
19152 ambiguous_p = identifier_token->ambiguous_p;
19153 identifier = cp_parser_identifier (parser);
19154 /* If the next token isn't an identifier, we are certainly not
19155 looking at a class-name. */
19156 if (identifier == error_mark_node)
19157 decl = error_mark_node;
19158 /* If we know this is a type-name, there's no need to look it
19159 up. */
19160 else if (typename_p)
19161 decl = identifier;
19162 else
19164 tree ambiguous_decls;
19165 /* If we already know that this lookup is ambiguous, then
19166 we've already issued an error message; there's no reason
19167 to check again. */
19168 if (ambiguous_p)
19170 cp_parser_simulate_error (parser);
19171 return error_mark_node;
19173 /* If the next token is a `::', then the name must be a type
19174 name.
19176 [basic.lookup.qual]
19178 During the lookup for a name preceding the :: scope
19179 resolution operator, object, function, and enumerator
19180 names are ignored. */
19181 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19182 tag_type = typename_type;
19183 /* Look up the name. */
19184 decl = cp_parser_lookup_name (parser, identifier,
19185 tag_type,
19186 /*is_template=*/false,
19187 /*is_namespace=*/false,
19188 check_dependency_p,
19189 &ambiguous_decls,
19190 identifier_token->location);
19191 if (ambiguous_decls)
19193 if (cp_parser_parsing_tentatively (parser))
19194 cp_parser_simulate_error (parser);
19195 return error_mark_node;
19199 else
19201 /* Try a template-id. */
19202 decl = cp_parser_template_id (parser, template_keyword_p,
19203 check_dependency_p,
19204 tag_type,
19205 is_declaration);
19206 if (decl == error_mark_node)
19207 return error_mark_node;
19210 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19212 /* If this is a typename, create a TYPENAME_TYPE. */
19213 if (typename_p && decl != error_mark_node)
19215 decl = make_typename_type (scope, decl, typename_type,
19216 /*complain=*/tf_error);
19217 if (decl != error_mark_node)
19218 decl = TYPE_NAME (decl);
19221 decl = strip_using_decl (decl);
19223 /* Check to see that it is really the name of a class. */
19224 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19225 && identifier_p (TREE_OPERAND (decl, 0))
19226 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19227 /* Situations like this:
19229 template <typename T> struct A {
19230 typename T::template X<int>::I i;
19233 are problematic. Is `T::template X<int>' a class-name? The
19234 standard does not seem to be definitive, but there is no other
19235 valid interpretation of the following `::'. Therefore, those
19236 names are considered class-names. */
19238 decl = make_typename_type (scope, decl, tag_type, tf_error);
19239 if (decl != error_mark_node)
19240 decl = TYPE_NAME (decl);
19242 else if (TREE_CODE (decl) != TYPE_DECL
19243 || TREE_TYPE (decl) == error_mark_node
19244 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19245 /* In Objective-C 2.0, a classname followed by '.' starts a
19246 dot-syntax expression, and it's not a type-name. */
19247 || (c_dialect_objc ()
19248 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19249 && objc_is_class_name (decl)))
19250 decl = error_mark_node;
19252 if (decl == error_mark_node)
19253 cp_parser_error (parser, "expected class-name");
19254 else if (identifier && !parser->scope)
19255 maybe_note_name_used_in_class (identifier, decl);
19257 return decl;
19260 /* Parse a class-specifier.
19262 class-specifier:
19263 class-head { member-specification [opt] }
19265 Returns the TREE_TYPE representing the class. */
19267 static tree
19268 cp_parser_class_specifier_1 (cp_parser* parser)
19270 tree type;
19271 tree attributes = NULL_TREE;
19272 bool nested_name_specifier_p;
19273 unsigned saved_num_template_parameter_lists;
19274 bool saved_in_function_body;
19275 unsigned char in_statement;
19276 bool in_switch_statement_p;
19277 bool saved_in_unbraced_linkage_specification_p;
19278 tree old_scope = NULL_TREE;
19279 tree scope = NULL_TREE;
19280 cp_token *closing_brace;
19282 push_deferring_access_checks (dk_no_deferred);
19284 /* Parse the class-head. */
19285 type = cp_parser_class_head (parser,
19286 &nested_name_specifier_p);
19287 /* If the class-head was a semantic disaster, skip the entire body
19288 of the class. */
19289 if (!type)
19291 cp_parser_skip_to_end_of_block_or_statement (parser);
19292 pop_deferring_access_checks ();
19293 return error_mark_node;
19296 /* Look for the `{'. */
19297 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19299 pop_deferring_access_checks ();
19300 return error_mark_node;
19303 cp_ensure_no_omp_declare_simd (parser);
19305 /* Issue an error message if type-definitions are forbidden here. */
19306 cp_parser_check_type_definition (parser);
19307 /* Remember that we are defining one more class. */
19308 ++parser->num_classes_being_defined;
19309 /* Inside the class, surrounding template-parameter-lists do not
19310 apply. */
19311 saved_num_template_parameter_lists
19312 = parser->num_template_parameter_lists;
19313 parser->num_template_parameter_lists = 0;
19314 /* We are not in a function body. */
19315 saved_in_function_body = parser->in_function_body;
19316 parser->in_function_body = false;
19317 /* Or in a loop. */
19318 in_statement = parser->in_statement;
19319 parser->in_statement = 0;
19320 /* Or in a switch. */
19321 in_switch_statement_p = parser->in_switch_statement_p;
19322 parser->in_switch_statement_p = false;
19323 /* We are not immediately inside an extern "lang" block. */
19324 saved_in_unbraced_linkage_specification_p
19325 = parser->in_unbraced_linkage_specification_p;
19326 parser->in_unbraced_linkage_specification_p = false;
19328 /* Start the class. */
19329 if (nested_name_specifier_p)
19331 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19332 old_scope = push_inner_scope (scope);
19334 type = begin_class_definition (type);
19336 if (type == error_mark_node)
19337 /* If the type is erroneous, skip the entire body of the class. */
19338 cp_parser_skip_to_closing_brace (parser);
19339 else
19340 /* Parse the member-specification. */
19341 cp_parser_member_specification_opt (parser);
19343 /* Look for the trailing `}'. */
19344 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19345 /* Look for trailing attributes to apply to this class. */
19346 if (cp_parser_allow_gnu_extensions_p (parser))
19347 attributes = cp_parser_gnu_attributes_opt (parser);
19348 if (type != error_mark_node)
19349 type = finish_struct (type, attributes);
19350 if (nested_name_specifier_p)
19351 pop_inner_scope (old_scope, scope);
19353 /* We've finished a type definition. Check for the common syntax
19354 error of forgetting a semicolon after the definition. We need to
19355 be careful, as we can't just check for not-a-semicolon and be done
19356 with it; the user might have typed:
19358 class X { } c = ...;
19359 class X { } *p = ...;
19361 and so forth. Instead, enumerate all the possible tokens that
19362 might follow this production; if we don't see one of them, then
19363 complain and silently insert the semicolon. */
19365 cp_token *token = cp_lexer_peek_token (parser->lexer);
19366 bool want_semicolon = true;
19368 if (cp_next_tokens_can_be_std_attribute_p (parser))
19369 /* Don't try to parse c++11 attributes here. As per the
19370 grammar, that should be a task for
19371 cp_parser_decl_specifier_seq. */
19372 want_semicolon = false;
19374 switch (token->type)
19376 case CPP_NAME:
19377 case CPP_SEMICOLON:
19378 case CPP_MULT:
19379 case CPP_AND:
19380 case CPP_OPEN_PAREN:
19381 case CPP_CLOSE_PAREN:
19382 case CPP_COMMA:
19383 want_semicolon = false;
19384 break;
19386 /* While it's legal for type qualifiers and storage class
19387 specifiers to follow type definitions in the grammar, only
19388 compiler testsuites contain code like that. Assume that if
19389 we see such code, then what we're really seeing is a case
19390 like:
19392 class X { }
19393 const <type> var = ...;
19397 class Y { }
19398 static <type> func (...) ...
19400 i.e. the qualifier or specifier applies to the next
19401 declaration. To do so, however, we need to look ahead one
19402 more token to see if *that* token is a type specifier.
19404 This code could be improved to handle:
19406 class Z { }
19407 static const <type> var = ...; */
19408 case CPP_KEYWORD:
19409 if (keyword_is_decl_specifier (token->keyword))
19411 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19413 /* Handling user-defined types here would be nice, but very
19414 tricky. */
19415 want_semicolon
19416 = (lookahead->type == CPP_KEYWORD
19417 && keyword_begins_type_specifier (lookahead->keyword));
19419 break;
19420 default:
19421 break;
19424 /* If we don't have a type, then something is very wrong and we
19425 shouldn't try to do anything clever. Likewise for not seeing the
19426 closing brace. */
19427 if (closing_brace && TYPE_P (type) && want_semicolon)
19429 cp_token_position prev
19430 = cp_lexer_previous_token_position (parser->lexer);
19431 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19432 location_t loc = prev_token->location;
19434 if (CLASSTYPE_DECLARED_CLASS (type))
19435 error_at (loc, "expected %<;%> after class definition");
19436 else if (TREE_CODE (type) == RECORD_TYPE)
19437 error_at (loc, "expected %<;%> after struct definition");
19438 else if (TREE_CODE (type) == UNION_TYPE)
19439 error_at (loc, "expected %<;%> after union definition");
19440 else
19441 gcc_unreachable ();
19443 /* Unget one token and smash it to look as though we encountered
19444 a semicolon in the input stream. */
19445 cp_lexer_set_token_position (parser->lexer, prev);
19446 token = cp_lexer_peek_token (parser->lexer);
19447 token->type = CPP_SEMICOLON;
19448 token->keyword = RID_MAX;
19452 /* If this class is not itself within the scope of another class,
19453 then we need to parse the bodies of all of the queued function
19454 definitions. Note that the queued functions defined in a class
19455 are not always processed immediately following the
19456 class-specifier for that class. Consider:
19458 struct A {
19459 struct B { void f() { sizeof (A); } };
19462 If `f' were processed before the processing of `A' were
19463 completed, there would be no way to compute the size of `A'.
19464 Note that the nesting we are interested in here is lexical --
19465 not the semantic nesting given by TYPE_CONTEXT. In particular,
19466 for:
19468 struct A { struct B; };
19469 struct A::B { void f() { } };
19471 there is no need to delay the parsing of `A::B::f'. */
19472 if (--parser->num_classes_being_defined == 0)
19474 tree decl;
19475 tree class_type = NULL_TREE;
19476 tree pushed_scope = NULL_TREE;
19477 unsigned ix;
19478 cp_default_arg_entry *e;
19479 tree save_ccp, save_ccr;
19481 /* In a first pass, parse default arguments to the functions.
19482 Then, in a second pass, parse the bodies of the functions.
19483 This two-phased approach handles cases like:
19485 struct S {
19486 void f() { g(); }
19487 void g(int i = 3);
19491 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19493 decl = e->decl;
19494 /* If there are default arguments that have not yet been processed,
19495 take care of them now. */
19496 if (class_type != e->class_type)
19498 if (pushed_scope)
19499 pop_scope (pushed_scope);
19500 class_type = e->class_type;
19501 pushed_scope = push_scope (class_type);
19503 /* Make sure that any template parameters are in scope. */
19504 maybe_begin_member_template_processing (decl);
19505 /* Parse the default argument expressions. */
19506 cp_parser_late_parsing_default_args (parser, decl);
19507 /* Remove any template parameters from the symbol table. */
19508 maybe_end_member_template_processing ();
19510 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19511 /* Now parse any NSDMIs. */
19512 save_ccp = current_class_ptr;
19513 save_ccr = current_class_ref;
19514 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19516 if (class_type != DECL_CONTEXT (decl))
19518 if (pushed_scope)
19519 pop_scope (pushed_scope);
19520 class_type = DECL_CONTEXT (decl);
19521 pushed_scope = push_scope (class_type);
19523 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19524 cp_parser_late_parsing_nsdmi (parser, decl);
19526 vec_safe_truncate (unparsed_nsdmis, 0);
19527 current_class_ptr = save_ccp;
19528 current_class_ref = save_ccr;
19529 if (pushed_scope)
19530 pop_scope (pushed_scope);
19531 /* Now parse the body of the functions. */
19532 if (flag_openmp)
19534 /* OpenMP UDRs need to be parsed before all other functions. */
19535 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19536 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19537 cp_parser_late_parsing_for_member (parser, decl);
19538 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19539 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19540 cp_parser_late_parsing_for_member (parser, decl);
19542 else
19543 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19544 cp_parser_late_parsing_for_member (parser, decl);
19545 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19548 /* Put back any saved access checks. */
19549 pop_deferring_access_checks ();
19551 /* Restore saved state. */
19552 parser->in_switch_statement_p = in_switch_statement_p;
19553 parser->in_statement = in_statement;
19554 parser->in_function_body = saved_in_function_body;
19555 parser->num_template_parameter_lists
19556 = saved_num_template_parameter_lists;
19557 parser->in_unbraced_linkage_specification_p
19558 = saved_in_unbraced_linkage_specification_p;
19560 return type;
19563 static tree
19564 cp_parser_class_specifier (cp_parser* parser)
19566 tree ret;
19567 timevar_push (TV_PARSE_STRUCT);
19568 ret = cp_parser_class_specifier_1 (parser);
19569 timevar_pop (TV_PARSE_STRUCT);
19570 return ret;
19573 /* Parse a class-head.
19575 class-head:
19576 class-key identifier [opt] base-clause [opt]
19577 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19578 class-key nested-name-specifier [opt] template-id
19579 base-clause [opt]
19581 class-virt-specifier:
19582 final
19584 GNU Extensions:
19585 class-key attributes identifier [opt] base-clause [opt]
19586 class-key attributes nested-name-specifier identifier base-clause [opt]
19587 class-key attributes nested-name-specifier [opt] template-id
19588 base-clause [opt]
19590 Upon return BASES is initialized to the list of base classes (or
19591 NULL, if there are none) in the same form returned by
19592 cp_parser_base_clause.
19594 Returns the TYPE of the indicated class. Sets
19595 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19596 involving a nested-name-specifier was used, and FALSE otherwise.
19598 Returns error_mark_node if this is not a class-head.
19600 Returns NULL_TREE if the class-head is syntactically valid, but
19601 semantically invalid in a way that means we should skip the entire
19602 body of the class. */
19604 static tree
19605 cp_parser_class_head (cp_parser* parser,
19606 bool* nested_name_specifier_p)
19608 tree nested_name_specifier;
19609 enum tag_types class_key;
19610 tree id = NULL_TREE;
19611 tree type = NULL_TREE;
19612 tree attributes;
19613 tree bases;
19614 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19615 bool template_id_p = false;
19616 bool qualified_p = false;
19617 bool invalid_nested_name_p = false;
19618 bool invalid_explicit_specialization_p = false;
19619 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19620 tree pushed_scope = NULL_TREE;
19621 unsigned num_templates;
19622 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19623 /* Assume no nested-name-specifier will be present. */
19624 *nested_name_specifier_p = false;
19625 /* Assume no template parameter lists will be used in defining the
19626 type. */
19627 num_templates = 0;
19628 parser->colon_corrects_to_scope_p = false;
19630 /* Look for the class-key. */
19631 class_key = cp_parser_class_key (parser);
19632 if (class_key == none_type)
19633 return error_mark_node;
19635 /* Parse the attributes. */
19636 attributes = cp_parser_attributes_opt (parser);
19638 /* If the next token is `::', that is invalid -- but sometimes
19639 people do try to write:
19641 struct ::S {};
19643 Handle this gracefully by accepting the extra qualifier, and then
19644 issuing an error about it later if this really is a
19645 class-head. If it turns out just to be an elaborated type
19646 specifier, remain silent. */
19647 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19648 qualified_p = true;
19650 push_deferring_access_checks (dk_no_check);
19652 /* Determine the name of the class. Begin by looking for an
19653 optional nested-name-specifier. */
19654 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19655 nested_name_specifier
19656 = cp_parser_nested_name_specifier_opt (parser,
19657 /*typename_keyword_p=*/false,
19658 /*check_dependency_p=*/false,
19659 /*type_p=*/true,
19660 /*is_declaration=*/false);
19661 /* If there was a nested-name-specifier, then there *must* be an
19662 identifier. */
19663 if (nested_name_specifier)
19665 type_start_token = cp_lexer_peek_token (parser->lexer);
19666 /* Although the grammar says `identifier', it really means
19667 `class-name' or `template-name'. You are only allowed to
19668 define a class that has already been declared with this
19669 syntax.
19671 The proposed resolution for Core Issue 180 says that wherever
19672 you see `class T::X' you should treat `X' as a type-name.
19674 It is OK to define an inaccessible class; for example:
19676 class A { class B; };
19677 class A::B {};
19679 We do not know if we will see a class-name, or a
19680 template-name. We look for a class-name first, in case the
19681 class-name is a template-id; if we looked for the
19682 template-name first we would stop after the template-name. */
19683 cp_parser_parse_tentatively (parser);
19684 type = cp_parser_class_name (parser,
19685 /*typename_keyword_p=*/false,
19686 /*template_keyword_p=*/false,
19687 class_type,
19688 /*check_dependency_p=*/false,
19689 /*class_head_p=*/true,
19690 /*is_declaration=*/false);
19691 /* If that didn't work, ignore the nested-name-specifier. */
19692 if (!cp_parser_parse_definitely (parser))
19694 invalid_nested_name_p = true;
19695 type_start_token = cp_lexer_peek_token (parser->lexer);
19696 id = cp_parser_identifier (parser);
19697 if (id == error_mark_node)
19698 id = NULL_TREE;
19700 /* If we could not find a corresponding TYPE, treat this
19701 declaration like an unqualified declaration. */
19702 if (type == error_mark_node)
19703 nested_name_specifier = NULL_TREE;
19704 /* Otherwise, count the number of templates used in TYPE and its
19705 containing scopes. */
19706 else
19708 tree scope;
19710 for (scope = TREE_TYPE (type);
19711 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19712 scope = get_containing_scope (scope))
19713 if (TYPE_P (scope)
19714 && CLASS_TYPE_P (scope)
19715 && CLASSTYPE_TEMPLATE_INFO (scope)
19716 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19717 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19718 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19719 ++num_templates;
19722 /* Otherwise, the identifier is optional. */
19723 else
19725 /* We don't know whether what comes next is a template-id,
19726 an identifier, or nothing at all. */
19727 cp_parser_parse_tentatively (parser);
19728 /* Check for a template-id. */
19729 type_start_token = cp_lexer_peek_token (parser->lexer);
19730 id = cp_parser_template_id (parser,
19731 /*template_keyword_p=*/false,
19732 /*check_dependency_p=*/true,
19733 class_key,
19734 /*is_declaration=*/true);
19735 /* If that didn't work, it could still be an identifier. */
19736 if (!cp_parser_parse_definitely (parser))
19738 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19740 type_start_token = cp_lexer_peek_token (parser->lexer);
19741 id = cp_parser_identifier (parser);
19743 else
19744 id = NULL_TREE;
19746 else
19748 template_id_p = true;
19749 ++num_templates;
19753 pop_deferring_access_checks ();
19755 if (id)
19757 cp_parser_check_for_invalid_template_id (parser, id,
19758 class_key,
19759 type_start_token->location);
19761 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19763 /* If it's not a `:' or a `{' then we can't really be looking at a
19764 class-head, since a class-head only appears as part of a
19765 class-specifier. We have to detect this situation before calling
19766 xref_tag, since that has irreversible side-effects. */
19767 if (!cp_parser_next_token_starts_class_definition_p (parser))
19769 cp_parser_error (parser, "expected %<{%> or %<:%>");
19770 type = error_mark_node;
19771 goto out;
19774 /* At this point, we're going ahead with the class-specifier, even
19775 if some other problem occurs. */
19776 cp_parser_commit_to_tentative_parse (parser);
19777 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19779 cp_parser_error (parser,
19780 "cannot specify %<override%> for a class");
19781 type = error_mark_node;
19782 goto out;
19784 /* Issue the error about the overly-qualified name now. */
19785 if (qualified_p)
19787 cp_parser_error (parser,
19788 "global qualification of class name is invalid");
19789 type = error_mark_node;
19790 goto out;
19792 else if (invalid_nested_name_p)
19794 cp_parser_error (parser,
19795 "qualified name does not name a class");
19796 type = error_mark_node;
19797 goto out;
19799 else if (nested_name_specifier)
19801 tree scope;
19803 /* Reject typedef-names in class heads. */
19804 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19806 error_at (type_start_token->location,
19807 "invalid class name in declaration of %qD",
19808 type);
19809 type = NULL_TREE;
19810 goto done;
19813 /* Figure out in what scope the declaration is being placed. */
19814 scope = current_scope ();
19815 /* If that scope does not contain the scope in which the
19816 class was originally declared, the program is invalid. */
19817 if (scope && !is_ancestor (scope, nested_name_specifier))
19819 if (at_namespace_scope_p ())
19820 error_at (type_start_token->location,
19821 "declaration of %qD in namespace %qD which does not "
19822 "enclose %qD",
19823 type, scope, nested_name_specifier);
19824 else
19825 error_at (type_start_token->location,
19826 "declaration of %qD in %qD which does not enclose %qD",
19827 type, scope, nested_name_specifier);
19828 type = NULL_TREE;
19829 goto done;
19831 /* [dcl.meaning]
19833 A declarator-id shall not be qualified except for the
19834 definition of a ... nested class outside of its class
19835 ... [or] the definition or explicit instantiation of a
19836 class member of a namespace outside of its namespace. */
19837 if (scope == nested_name_specifier)
19839 permerror (nested_name_specifier_token_start->location,
19840 "extra qualification not allowed");
19841 nested_name_specifier = NULL_TREE;
19842 num_templates = 0;
19845 /* An explicit-specialization must be preceded by "template <>". If
19846 it is not, try to recover gracefully. */
19847 if (at_namespace_scope_p ()
19848 && parser->num_template_parameter_lists == 0
19849 && template_id_p)
19851 error_at (type_start_token->location,
19852 "an explicit specialization must be preceded by %<template <>%>");
19853 invalid_explicit_specialization_p = true;
19854 /* Take the same action that would have been taken by
19855 cp_parser_explicit_specialization. */
19856 ++parser->num_template_parameter_lists;
19857 begin_specialization ();
19859 /* There must be no "return" statements between this point and the
19860 end of this function; set "type "to the correct return value and
19861 use "goto done;" to return. */
19862 /* Make sure that the right number of template parameters were
19863 present. */
19864 if (!cp_parser_check_template_parameters (parser, num_templates,
19865 type_start_token->location,
19866 /*declarator=*/NULL))
19868 /* If something went wrong, there is no point in even trying to
19869 process the class-definition. */
19870 type = NULL_TREE;
19871 goto done;
19874 /* Look up the type. */
19875 if (template_id_p)
19877 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19878 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19879 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19881 error_at (type_start_token->location,
19882 "function template %qD redeclared as a class template", id);
19883 type = error_mark_node;
19885 else
19887 type = TREE_TYPE (id);
19888 type = maybe_process_partial_specialization (type);
19890 if (nested_name_specifier)
19891 pushed_scope = push_scope (nested_name_specifier);
19893 else if (nested_name_specifier)
19895 tree class_type;
19897 /* Given:
19899 template <typename T> struct S { struct T };
19900 template <typename T> struct S<T>::T { };
19902 we will get a TYPENAME_TYPE when processing the definition of
19903 `S::T'. We need to resolve it to the actual type before we
19904 try to define it. */
19905 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19907 class_type = resolve_typename_type (TREE_TYPE (type),
19908 /*only_current_p=*/false);
19909 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19910 type = TYPE_NAME (class_type);
19911 else
19913 cp_parser_error (parser, "could not resolve typename type");
19914 type = error_mark_node;
19918 if (maybe_process_partial_specialization (TREE_TYPE (type))
19919 == error_mark_node)
19921 type = NULL_TREE;
19922 goto done;
19925 class_type = current_class_type;
19926 /* Enter the scope indicated by the nested-name-specifier. */
19927 pushed_scope = push_scope (nested_name_specifier);
19928 /* Get the canonical version of this type. */
19929 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19930 /* Call push_template_decl if it seems like we should be defining a
19931 template either from the template headers or the type we're
19932 defining, so that we diagnose both extra and missing headers. */
19933 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
19934 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
19935 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
19936 (TREE_TYPE (type)))))
19937 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19939 type = push_template_decl (type);
19940 if (type == error_mark_node)
19942 type = NULL_TREE;
19943 goto done;
19947 type = TREE_TYPE (type);
19948 *nested_name_specifier_p = true;
19950 else /* The name is not a nested name. */
19952 /* If the class was unnamed, create a dummy name. */
19953 if (!id)
19954 id = make_anon_name ();
19955 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19956 parser->num_template_parameter_lists);
19959 /* Indicate whether this class was declared as a `class' or as a
19960 `struct'. */
19961 if (TREE_CODE (type) == RECORD_TYPE)
19962 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19963 cp_parser_check_class_key (class_key, type);
19965 /* If this type was already complete, and we see another definition,
19966 that's an error. */
19967 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19969 error_at (type_start_token->location, "redefinition of %q#T",
19970 type);
19971 error_at (type_start_token->location, "previous definition of %q+#T",
19972 type);
19973 type = NULL_TREE;
19974 goto done;
19976 else if (type == error_mark_node)
19977 type = NULL_TREE;
19979 if (type)
19981 /* Apply attributes now, before any use of the class as a template
19982 argument in its base list. */
19983 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19984 fixup_attribute_variants (type);
19987 /* We will have entered the scope containing the class; the names of
19988 base classes should be looked up in that context. For example:
19990 struct A { struct B {}; struct C; };
19991 struct A::C : B {};
19993 is valid. */
19995 /* Get the list of base-classes, if there is one. */
19996 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19998 /* PR59482: enter the class scope so that base-specifiers are looked
19999 up correctly. */
20000 if (type)
20001 pushclass (type);
20002 bases = cp_parser_base_clause (parser);
20003 /* PR59482: get out of the previously pushed class scope so that the
20004 subsequent pops pop the right thing. */
20005 if (type)
20006 popclass ();
20008 else
20009 bases = NULL_TREE;
20011 /* If we're really defining a class, process the base classes.
20012 If they're invalid, fail. */
20013 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20014 && !xref_basetypes (type, bases))
20015 type = NULL_TREE;
20017 done:
20018 /* Leave the scope given by the nested-name-specifier. We will
20019 enter the class scope itself while processing the members. */
20020 if (pushed_scope)
20021 pop_scope (pushed_scope);
20023 if (invalid_explicit_specialization_p)
20025 end_specialization ();
20026 --parser->num_template_parameter_lists;
20029 if (type)
20030 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20031 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20032 CLASSTYPE_FINAL (type) = 1;
20033 out:
20034 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20035 return type;
20038 /* Parse a class-key.
20040 class-key:
20041 class
20042 struct
20043 union
20045 Returns the kind of class-key specified, or none_type to indicate
20046 error. */
20048 static enum tag_types
20049 cp_parser_class_key (cp_parser* parser)
20051 cp_token *token;
20052 enum tag_types tag_type;
20054 /* Look for the class-key. */
20055 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20056 if (!token)
20057 return none_type;
20059 /* Check to see if the TOKEN is a class-key. */
20060 tag_type = cp_parser_token_is_class_key (token);
20061 if (!tag_type)
20062 cp_parser_error (parser, "expected class-key");
20063 return tag_type;
20066 /* Parse an (optional) member-specification.
20068 member-specification:
20069 member-declaration member-specification [opt]
20070 access-specifier : member-specification [opt] */
20072 static void
20073 cp_parser_member_specification_opt (cp_parser* parser)
20075 while (true)
20077 cp_token *token;
20078 enum rid keyword;
20080 /* Peek at the next token. */
20081 token = cp_lexer_peek_token (parser->lexer);
20082 /* If it's a `}', or EOF then we've seen all the members. */
20083 if (token->type == CPP_CLOSE_BRACE
20084 || token->type == CPP_EOF
20085 || token->type == CPP_PRAGMA_EOL)
20086 break;
20088 /* See if this token is a keyword. */
20089 keyword = token->keyword;
20090 switch (keyword)
20092 case RID_PUBLIC:
20093 case RID_PROTECTED:
20094 case RID_PRIVATE:
20095 /* Consume the access-specifier. */
20096 cp_lexer_consume_token (parser->lexer);
20097 /* Remember which access-specifier is active. */
20098 current_access_specifier = token->u.value;
20099 /* Look for the `:'. */
20100 cp_parser_require (parser, CPP_COLON, RT_COLON);
20101 break;
20103 default:
20104 /* Accept #pragmas at class scope. */
20105 if (token->type == CPP_PRAGMA)
20107 cp_parser_pragma (parser, pragma_member);
20108 break;
20111 /* Otherwise, the next construction must be a
20112 member-declaration. */
20113 cp_parser_member_declaration (parser);
20118 /* Parse a member-declaration.
20120 member-declaration:
20121 decl-specifier-seq [opt] member-declarator-list [opt] ;
20122 function-definition ; [opt]
20123 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20124 using-declaration
20125 template-declaration
20126 alias-declaration
20128 member-declarator-list:
20129 member-declarator
20130 member-declarator-list , member-declarator
20132 member-declarator:
20133 declarator pure-specifier [opt]
20134 declarator constant-initializer [opt]
20135 identifier [opt] : constant-expression
20137 GNU Extensions:
20139 member-declaration:
20140 __extension__ member-declaration
20142 member-declarator:
20143 declarator attributes [opt] pure-specifier [opt]
20144 declarator attributes [opt] constant-initializer [opt]
20145 identifier [opt] attributes [opt] : constant-expression
20147 C++0x Extensions:
20149 member-declaration:
20150 static_assert-declaration */
20152 static void
20153 cp_parser_member_declaration (cp_parser* parser)
20155 cp_decl_specifier_seq decl_specifiers;
20156 tree prefix_attributes;
20157 tree decl;
20158 int declares_class_or_enum;
20159 bool friend_p;
20160 cp_token *token = NULL;
20161 cp_token *decl_spec_token_start = NULL;
20162 cp_token *initializer_token_start = NULL;
20163 int saved_pedantic;
20164 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20166 /* Check for the `__extension__' keyword. */
20167 if (cp_parser_extension_opt (parser, &saved_pedantic))
20169 /* Recurse. */
20170 cp_parser_member_declaration (parser);
20171 /* Restore the old value of the PEDANTIC flag. */
20172 pedantic = saved_pedantic;
20174 return;
20177 /* Check for a template-declaration. */
20178 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20180 /* An explicit specialization here is an error condition, and we
20181 expect the specialization handler to detect and report this. */
20182 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20183 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20184 cp_parser_explicit_specialization (parser);
20185 else
20186 cp_parser_template_declaration (parser, /*member_p=*/true);
20188 return;
20191 /* Check for a using-declaration. */
20192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20194 if (cxx_dialect < cxx11)
20196 /* Parse the using-declaration. */
20197 cp_parser_using_declaration (parser,
20198 /*access_declaration_p=*/false);
20199 return;
20201 else
20203 tree decl;
20204 bool alias_decl_expected;
20205 cp_parser_parse_tentatively (parser);
20206 decl = cp_parser_alias_declaration (parser);
20207 /* Note that if we actually see the '=' token after the
20208 identifier, cp_parser_alias_declaration commits the
20209 tentative parse. In that case, we really expects an
20210 alias-declaration. Otherwise, we expect a using
20211 declaration. */
20212 alias_decl_expected =
20213 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20214 cp_parser_parse_definitely (parser);
20216 if (alias_decl_expected)
20217 finish_member_declaration (decl);
20218 else
20219 cp_parser_using_declaration (parser,
20220 /*access_declaration_p=*/false);
20221 return;
20225 /* Check for @defs. */
20226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20228 tree ivar, member;
20229 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20230 ivar = ivar_chains;
20231 while (ivar)
20233 member = ivar;
20234 ivar = TREE_CHAIN (member);
20235 TREE_CHAIN (member) = NULL_TREE;
20236 finish_member_declaration (member);
20238 return;
20241 /* If the next token is `static_assert' we have a static assertion. */
20242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20244 cp_parser_static_assert (parser, /*member_p=*/true);
20245 return;
20248 parser->colon_corrects_to_scope_p = false;
20250 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20251 goto out;
20253 /* Parse the decl-specifier-seq. */
20254 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20255 cp_parser_decl_specifier_seq (parser,
20256 CP_PARSER_FLAGS_OPTIONAL,
20257 &decl_specifiers,
20258 &declares_class_or_enum);
20259 /* Check for an invalid type-name. */
20260 if (!decl_specifiers.any_type_specifiers_p
20261 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20262 goto out;
20263 /* If there is no declarator, then the decl-specifier-seq should
20264 specify a type. */
20265 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20267 /* If there was no decl-specifier-seq, and the next token is a
20268 `;', then we have something like:
20270 struct S { ; };
20272 [class.mem]
20274 Each member-declaration shall declare at least one member
20275 name of the class. */
20276 if (!decl_specifiers.any_specifiers_p)
20278 cp_token *token = cp_lexer_peek_token (parser->lexer);
20279 if (!in_system_header_at (token->location))
20280 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20282 else
20284 tree type;
20286 /* See if this declaration is a friend. */
20287 friend_p = cp_parser_friend_p (&decl_specifiers);
20288 /* If there were decl-specifiers, check to see if there was
20289 a class-declaration. */
20290 type = check_tag_decl (&decl_specifiers,
20291 /*explicit_type_instantiation_p=*/false);
20292 /* Nested classes have already been added to the class, but
20293 a `friend' needs to be explicitly registered. */
20294 if (friend_p)
20296 /* If the `friend' keyword was present, the friend must
20297 be introduced with a class-key. */
20298 if (!declares_class_or_enum && cxx_dialect < cxx11)
20299 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20300 "in C++03 a class-key must be used "
20301 "when declaring a friend");
20302 /* In this case:
20304 template <typename T> struct A {
20305 friend struct A<T>::B;
20308 A<T>::B will be represented by a TYPENAME_TYPE, and
20309 therefore not recognized by check_tag_decl. */
20310 if (!type)
20312 type = decl_specifiers.type;
20313 if (type && TREE_CODE (type) == TYPE_DECL)
20314 type = TREE_TYPE (type);
20316 if (!type || !TYPE_P (type))
20317 error_at (decl_spec_token_start->location,
20318 "friend declaration does not name a class or "
20319 "function");
20320 else
20321 make_friend_class (current_class_type, type,
20322 /*complain=*/true);
20324 /* If there is no TYPE, an error message will already have
20325 been issued. */
20326 else if (!type || type == error_mark_node)
20328 /* An anonymous aggregate has to be handled specially; such
20329 a declaration really declares a data member (with a
20330 particular type), as opposed to a nested class. */
20331 else if (ANON_AGGR_TYPE_P (type))
20333 /* C++11 9.5/6. */
20334 if (decl_specifiers.storage_class != sc_none)
20335 error_at (decl_spec_token_start->location,
20336 "a storage class on an anonymous aggregate "
20337 "in class scope is not allowed");
20339 /* Remove constructors and such from TYPE, now that we
20340 know it is an anonymous aggregate. */
20341 fixup_anonymous_aggr (type);
20342 /* And make the corresponding data member. */
20343 decl = build_decl (decl_spec_token_start->location,
20344 FIELD_DECL, NULL_TREE, type);
20345 /* Add it to the class. */
20346 finish_member_declaration (decl);
20348 else
20349 cp_parser_check_access_in_redeclaration
20350 (TYPE_NAME (type),
20351 decl_spec_token_start->location);
20354 else
20356 bool assume_semicolon = false;
20358 /* Clear attributes from the decl_specifiers but keep them
20359 around as prefix attributes that apply them to the entity
20360 being declared. */
20361 prefix_attributes = decl_specifiers.attributes;
20362 decl_specifiers.attributes = NULL_TREE;
20364 /* See if these declarations will be friends. */
20365 friend_p = cp_parser_friend_p (&decl_specifiers);
20367 /* Keep going until we hit the `;' at the end of the
20368 declaration. */
20369 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20371 tree attributes = NULL_TREE;
20372 tree first_attribute;
20374 /* Peek at the next token. */
20375 token = cp_lexer_peek_token (parser->lexer);
20377 /* Check for a bitfield declaration. */
20378 if (token->type == CPP_COLON
20379 || (token->type == CPP_NAME
20380 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20381 == CPP_COLON))
20383 tree identifier;
20384 tree width;
20386 /* Get the name of the bitfield. Note that we cannot just
20387 check TOKEN here because it may have been invalidated by
20388 the call to cp_lexer_peek_nth_token above. */
20389 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20390 identifier = cp_parser_identifier (parser);
20391 else
20392 identifier = NULL_TREE;
20394 /* Consume the `:' token. */
20395 cp_lexer_consume_token (parser->lexer);
20396 /* Get the width of the bitfield. */
20397 width
20398 = cp_parser_constant_expression (parser,
20399 /*allow_non_constant=*/false,
20400 NULL);
20402 /* Look for attributes that apply to the bitfield. */
20403 attributes = cp_parser_attributes_opt (parser);
20404 /* Remember which attributes are prefix attributes and
20405 which are not. */
20406 first_attribute = attributes;
20407 /* Combine the attributes. */
20408 attributes = chainon (prefix_attributes, attributes);
20410 /* Create the bitfield declaration. */
20411 decl = grokbitfield (identifier
20412 ? make_id_declarator (NULL_TREE,
20413 identifier,
20414 sfk_none)
20415 : NULL,
20416 &decl_specifiers,
20417 width,
20418 attributes);
20420 else
20422 cp_declarator *declarator;
20423 tree initializer;
20424 tree asm_specification;
20425 int ctor_dtor_or_conv_p;
20427 /* Parse the declarator. */
20428 declarator
20429 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20430 &ctor_dtor_or_conv_p,
20431 /*parenthesized_p=*/NULL,
20432 /*member_p=*/true);
20434 /* If something went wrong parsing the declarator, make sure
20435 that we at least consume some tokens. */
20436 if (declarator == cp_error_declarator)
20438 /* Skip to the end of the statement. */
20439 cp_parser_skip_to_end_of_statement (parser);
20440 /* If the next token is not a semicolon, that is
20441 probably because we just skipped over the body of
20442 a function. So, we consume a semicolon if
20443 present, but do not issue an error message if it
20444 is not present. */
20445 if (cp_lexer_next_token_is (parser->lexer,
20446 CPP_SEMICOLON))
20447 cp_lexer_consume_token (parser->lexer);
20448 goto out;
20451 if (declares_class_or_enum & 2)
20452 cp_parser_check_for_definition_in_return_type
20453 (declarator, decl_specifiers.type,
20454 decl_specifiers.locations[ds_type_spec]);
20456 /* Look for an asm-specification. */
20457 asm_specification = cp_parser_asm_specification_opt (parser);
20458 /* Look for attributes that apply to the declaration. */
20459 attributes = cp_parser_attributes_opt (parser);
20460 /* Remember which attributes are prefix attributes and
20461 which are not. */
20462 first_attribute = attributes;
20463 /* Combine the attributes. */
20464 attributes = chainon (prefix_attributes, attributes);
20466 /* If it's an `=', then we have a constant-initializer or a
20467 pure-specifier. It is not correct to parse the
20468 initializer before registering the member declaration
20469 since the member declaration should be in scope while
20470 its initializer is processed. However, the rest of the
20471 front end does not yet provide an interface that allows
20472 us to handle this correctly. */
20473 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20475 /* In [class.mem]:
20477 A pure-specifier shall be used only in the declaration of
20478 a virtual function.
20480 A member-declarator can contain a constant-initializer
20481 only if it declares a static member of integral or
20482 enumeration type.
20484 Therefore, if the DECLARATOR is for a function, we look
20485 for a pure-specifier; otherwise, we look for a
20486 constant-initializer. When we call `grokfield', it will
20487 perform more stringent semantics checks. */
20488 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20489 if (function_declarator_p (declarator)
20490 || (decl_specifiers.type
20491 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20492 && declarator->kind == cdk_id
20493 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20494 == FUNCTION_TYPE)))
20495 initializer = cp_parser_pure_specifier (parser);
20496 else if (decl_specifiers.storage_class != sc_static)
20497 initializer = cp_parser_save_nsdmi (parser);
20498 else if (cxx_dialect >= cxx11)
20500 bool nonconst;
20501 /* Don't require a constant rvalue in C++11, since we
20502 might want a reference constant. We'll enforce
20503 constancy later. */
20504 cp_lexer_consume_token (parser->lexer);
20505 /* Parse the initializer. */
20506 initializer = cp_parser_initializer_clause (parser,
20507 &nonconst);
20509 else
20510 /* Parse the initializer. */
20511 initializer = cp_parser_constant_initializer (parser);
20513 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20514 && !function_declarator_p (declarator))
20516 bool x;
20517 if (decl_specifiers.storage_class != sc_static)
20518 initializer = cp_parser_save_nsdmi (parser);
20519 else
20520 initializer = cp_parser_initializer (parser, &x, &x);
20522 /* Otherwise, there is no initializer. */
20523 else
20524 initializer = NULL_TREE;
20526 /* See if we are probably looking at a function
20527 definition. We are certainly not looking at a
20528 member-declarator. Calling `grokfield' has
20529 side-effects, so we must not do it unless we are sure
20530 that we are looking at a member-declarator. */
20531 if (cp_parser_token_starts_function_definition_p
20532 (cp_lexer_peek_token (parser->lexer)))
20534 /* The grammar does not allow a pure-specifier to be
20535 used when a member function is defined. (It is
20536 possible that this fact is an oversight in the
20537 standard, since a pure function may be defined
20538 outside of the class-specifier. */
20539 if (initializer && initializer_token_start)
20540 error_at (initializer_token_start->location,
20541 "pure-specifier on function-definition");
20542 decl = cp_parser_save_member_function_body (parser,
20543 &decl_specifiers,
20544 declarator,
20545 attributes);
20546 if (parser->fully_implicit_function_template_p)
20547 decl = finish_fully_implicit_template (parser, decl);
20548 /* If the member was not a friend, declare it here. */
20549 if (!friend_p)
20550 finish_member_declaration (decl);
20551 /* Peek at the next token. */
20552 token = cp_lexer_peek_token (parser->lexer);
20553 /* If the next token is a semicolon, consume it. */
20554 if (token->type == CPP_SEMICOLON)
20555 cp_lexer_consume_token (parser->lexer);
20556 goto out;
20558 else
20559 if (declarator->kind == cdk_function)
20560 declarator->id_loc = token->location;
20561 /* Create the declaration. */
20562 decl = grokfield (declarator, &decl_specifiers,
20563 initializer, /*init_const_expr_p=*/true,
20564 asm_specification, attributes);
20565 if (parser->fully_implicit_function_template_p)
20567 if (friend_p)
20568 finish_fully_implicit_template (parser, 0);
20569 else
20570 decl = finish_fully_implicit_template (parser, decl);
20574 cp_finalize_omp_declare_simd (parser, decl);
20576 /* Reset PREFIX_ATTRIBUTES. */
20577 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20578 attributes = TREE_CHAIN (attributes);
20579 if (attributes)
20580 TREE_CHAIN (attributes) = NULL_TREE;
20582 /* If there is any qualification still in effect, clear it
20583 now; we will be starting fresh with the next declarator. */
20584 parser->scope = NULL_TREE;
20585 parser->qualifying_scope = NULL_TREE;
20586 parser->object_scope = NULL_TREE;
20587 /* If it's a `,', then there are more declarators. */
20588 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20590 cp_lexer_consume_token (parser->lexer);
20591 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20593 cp_token *token = cp_lexer_previous_token (parser->lexer);
20594 error_at (token->location,
20595 "stray %<,%> at end of member declaration");
20598 /* If the next token isn't a `;', then we have a parse error. */
20599 else if (cp_lexer_next_token_is_not (parser->lexer,
20600 CPP_SEMICOLON))
20602 /* The next token might be a ways away from where the
20603 actual semicolon is missing. Find the previous token
20604 and use that for our error position. */
20605 cp_token *token = cp_lexer_previous_token (parser->lexer);
20606 error_at (token->location,
20607 "expected %<;%> at end of member declaration");
20609 /* Assume that the user meant to provide a semicolon. If
20610 we were to cp_parser_skip_to_end_of_statement, we might
20611 skip to a semicolon inside a member function definition
20612 and issue nonsensical error messages. */
20613 assume_semicolon = true;
20616 if (decl)
20618 /* Add DECL to the list of members. */
20619 if (!friend_p)
20620 finish_member_declaration (decl);
20622 if (TREE_CODE (decl) == FUNCTION_DECL)
20623 cp_parser_save_default_args (parser, decl);
20624 else if (TREE_CODE (decl) == FIELD_DECL
20625 && !DECL_C_BIT_FIELD (decl)
20626 && DECL_INITIAL (decl))
20627 /* Add DECL to the queue of NSDMI to be parsed later. */
20628 vec_safe_push (unparsed_nsdmis, decl);
20631 if (assume_semicolon)
20632 goto out;
20636 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20637 out:
20638 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20641 /* Parse a pure-specifier.
20643 pure-specifier:
20646 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20647 Otherwise, ERROR_MARK_NODE is returned. */
20649 static tree
20650 cp_parser_pure_specifier (cp_parser* parser)
20652 cp_token *token;
20654 /* Look for the `=' token. */
20655 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20656 return error_mark_node;
20657 /* Look for the `0' token. */
20658 token = cp_lexer_peek_token (parser->lexer);
20660 if (token->type == CPP_EOF
20661 || token->type == CPP_PRAGMA_EOL)
20662 return error_mark_node;
20664 cp_lexer_consume_token (parser->lexer);
20666 /* Accept = default or = delete in c++0x mode. */
20667 if (token->keyword == RID_DEFAULT
20668 || token->keyword == RID_DELETE)
20670 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20671 return token->u.value;
20674 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20675 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20677 cp_parser_error (parser,
20678 "invalid pure specifier (only %<= 0%> is allowed)");
20679 cp_parser_skip_to_end_of_statement (parser);
20680 return error_mark_node;
20682 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20684 error_at (token->location, "templates may not be %<virtual%>");
20685 return error_mark_node;
20688 return integer_zero_node;
20691 /* Parse a constant-initializer.
20693 constant-initializer:
20694 = constant-expression
20696 Returns a representation of the constant-expression. */
20698 static tree
20699 cp_parser_constant_initializer (cp_parser* parser)
20701 /* Look for the `=' token. */
20702 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20703 return error_mark_node;
20705 /* It is invalid to write:
20707 struct S { static const int i = { 7 }; };
20710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20712 cp_parser_error (parser,
20713 "a brace-enclosed initializer is not allowed here");
20714 /* Consume the opening brace. */
20715 cp_lexer_consume_token (parser->lexer);
20716 /* Skip the initializer. */
20717 cp_parser_skip_to_closing_brace (parser);
20718 /* Look for the trailing `}'. */
20719 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20721 return error_mark_node;
20724 return cp_parser_constant_expression (parser,
20725 /*allow_non_constant=*/false,
20726 NULL);
20729 /* Derived classes [gram.class.derived] */
20731 /* Parse a base-clause.
20733 base-clause:
20734 : base-specifier-list
20736 base-specifier-list:
20737 base-specifier ... [opt]
20738 base-specifier-list , base-specifier ... [opt]
20740 Returns a TREE_LIST representing the base-classes, in the order in
20741 which they were declared. The representation of each node is as
20742 described by cp_parser_base_specifier.
20744 In the case that no bases are specified, this function will return
20745 NULL_TREE, not ERROR_MARK_NODE. */
20747 static tree
20748 cp_parser_base_clause (cp_parser* parser)
20750 tree bases = NULL_TREE;
20752 /* Look for the `:' that begins the list. */
20753 cp_parser_require (parser, CPP_COLON, RT_COLON);
20755 /* Scan the base-specifier-list. */
20756 while (true)
20758 cp_token *token;
20759 tree base;
20760 bool pack_expansion_p = false;
20762 /* Look for the base-specifier. */
20763 base = cp_parser_base_specifier (parser);
20764 /* Look for the (optional) ellipsis. */
20765 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20767 /* Consume the `...'. */
20768 cp_lexer_consume_token (parser->lexer);
20770 pack_expansion_p = true;
20773 /* Add BASE to the front of the list. */
20774 if (base && base != error_mark_node)
20776 if (pack_expansion_p)
20777 /* Make this a pack expansion type. */
20778 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20780 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20782 TREE_CHAIN (base) = bases;
20783 bases = base;
20786 /* Peek at the next token. */
20787 token = cp_lexer_peek_token (parser->lexer);
20788 /* If it's not a comma, then the list is complete. */
20789 if (token->type != CPP_COMMA)
20790 break;
20791 /* Consume the `,'. */
20792 cp_lexer_consume_token (parser->lexer);
20795 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20796 base class had a qualified name. However, the next name that
20797 appears is certainly not qualified. */
20798 parser->scope = NULL_TREE;
20799 parser->qualifying_scope = NULL_TREE;
20800 parser->object_scope = NULL_TREE;
20802 return nreverse (bases);
20805 /* Parse a base-specifier.
20807 base-specifier:
20808 :: [opt] nested-name-specifier [opt] class-name
20809 virtual access-specifier [opt] :: [opt] nested-name-specifier
20810 [opt] class-name
20811 access-specifier virtual [opt] :: [opt] nested-name-specifier
20812 [opt] class-name
20814 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20815 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20816 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20817 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20819 static tree
20820 cp_parser_base_specifier (cp_parser* parser)
20822 cp_token *token;
20823 bool done = false;
20824 bool virtual_p = false;
20825 bool duplicate_virtual_error_issued_p = false;
20826 bool duplicate_access_error_issued_p = false;
20827 bool class_scope_p, template_p;
20828 tree access = access_default_node;
20829 tree type;
20831 /* Process the optional `virtual' and `access-specifier'. */
20832 while (!done)
20834 /* Peek at the next token. */
20835 token = cp_lexer_peek_token (parser->lexer);
20836 /* Process `virtual'. */
20837 switch (token->keyword)
20839 case RID_VIRTUAL:
20840 /* If `virtual' appears more than once, issue an error. */
20841 if (virtual_p && !duplicate_virtual_error_issued_p)
20843 cp_parser_error (parser,
20844 "%<virtual%> specified more than once in base-specified");
20845 duplicate_virtual_error_issued_p = true;
20848 virtual_p = true;
20850 /* Consume the `virtual' token. */
20851 cp_lexer_consume_token (parser->lexer);
20853 break;
20855 case RID_PUBLIC:
20856 case RID_PROTECTED:
20857 case RID_PRIVATE:
20858 /* If more than one access specifier appears, issue an
20859 error. */
20860 if (access != access_default_node
20861 && !duplicate_access_error_issued_p)
20863 cp_parser_error (parser,
20864 "more than one access specifier in base-specified");
20865 duplicate_access_error_issued_p = true;
20868 access = ridpointers[(int) token->keyword];
20870 /* Consume the access-specifier. */
20871 cp_lexer_consume_token (parser->lexer);
20873 break;
20875 default:
20876 done = true;
20877 break;
20880 /* It is not uncommon to see programs mechanically, erroneously, use
20881 the 'typename' keyword to denote (dependent) qualified types
20882 as base classes. */
20883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20885 token = cp_lexer_peek_token (parser->lexer);
20886 if (!processing_template_decl)
20887 error_at (token->location,
20888 "keyword %<typename%> not allowed outside of templates");
20889 else
20890 error_at (token->location,
20891 "keyword %<typename%> not allowed in this context "
20892 "(the base class is implicitly a type)");
20893 cp_lexer_consume_token (parser->lexer);
20896 /* Look for the optional `::' operator. */
20897 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20898 /* Look for the nested-name-specifier. The simplest way to
20899 implement:
20901 [temp.res]
20903 The keyword `typename' is not permitted in a base-specifier or
20904 mem-initializer; in these contexts a qualified name that
20905 depends on a template-parameter is implicitly assumed to be a
20906 type name.
20908 is to pretend that we have seen the `typename' keyword at this
20909 point. */
20910 cp_parser_nested_name_specifier_opt (parser,
20911 /*typename_keyword_p=*/true,
20912 /*check_dependency_p=*/true,
20913 typename_type,
20914 /*is_declaration=*/true);
20915 /* If the base class is given by a qualified name, assume that names
20916 we see are type names or templates, as appropriate. */
20917 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20918 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20920 if (!parser->scope
20921 && cp_lexer_next_token_is_decltype (parser->lexer))
20922 /* DR 950 allows decltype as a base-specifier. */
20923 type = cp_parser_decltype (parser);
20924 else
20926 /* Otherwise, look for the class-name. */
20927 type = cp_parser_class_name (parser,
20928 class_scope_p,
20929 template_p,
20930 typename_type,
20931 /*check_dependency_p=*/true,
20932 /*class_head_p=*/false,
20933 /*is_declaration=*/true);
20934 type = TREE_TYPE (type);
20937 if (type == error_mark_node)
20938 return error_mark_node;
20940 return finish_base_specifier (type, access, virtual_p);
20943 /* Exception handling [gram.exception] */
20945 /* Parse an (optional) noexcept-specification.
20947 noexcept-specification:
20948 noexcept ( constant-expression ) [opt]
20950 If no noexcept-specification is present, returns NULL_TREE.
20951 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20952 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20953 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20954 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20955 in which case a boolean condition is returned instead. */
20957 static tree
20958 cp_parser_noexcept_specification_opt (cp_parser* parser,
20959 bool require_constexpr,
20960 bool* consumed_expr,
20961 bool return_cond)
20963 cp_token *token;
20964 const char *saved_message;
20966 /* Peek at the next token. */
20967 token = cp_lexer_peek_token (parser->lexer);
20969 /* Is it a noexcept-specification? */
20970 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20972 tree expr;
20973 cp_lexer_consume_token (parser->lexer);
20975 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20977 cp_lexer_consume_token (parser->lexer);
20979 if (require_constexpr)
20981 /* Types may not be defined in an exception-specification. */
20982 saved_message = parser->type_definition_forbidden_message;
20983 parser->type_definition_forbidden_message
20984 = G_("types may not be defined in an exception-specification");
20986 expr = cp_parser_constant_expression (parser, false, NULL);
20988 /* Restore the saved message. */
20989 parser->type_definition_forbidden_message = saved_message;
20991 else
20993 expr = cp_parser_expression (parser, false, NULL);
20994 *consumed_expr = true;
20997 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20999 else
21001 expr = boolean_true_node;
21002 if (!require_constexpr)
21003 *consumed_expr = false;
21006 /* We cannot build a noexcept-spec right away because this will check
21007 that expr is a constexpr. */
21008 if (!return_cond)
21009 return build_noexcept_spec (expr, tf_warning_or_error);
21010 else
21011 return expr;
21013 else
21014 return NULL_TREE;
21017 /* Parse an (optional) exception-specification.
21019 exception-specification:
21020 throw ( type-id-list [opt] )
21022 Returns a TREE_LIST representing the exception-specification. The
21023 TREE_VALUE of each node is a type. */
21025 static tree
21026 cp_parser_exception_specification_opt (cp_parser* parser)
21028 cp_token *token;
21029 tree type_id_list;
21030 const char *saved_message;
21032 /* Peek at the next token. */
21033 token = cp_lexer_peek_token (parser->lexer);
21035 /* Is it a noexcept-specification? */
21036 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21037 false);
21038 if (type_id_list != NULL_TREE)
21039 return type_id_list;
21041 /* If it's not `throw', then there's no exception-specification. */
21042 if (!cp_parser_is_keyword (token, RID_THROW))
21043 return NULL_TREE;
21045 #if 0
21046 /* Enable this once a lot of code has transitioned to noexcept? */
21047 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21048 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21049 "deprecated in C++0x; use %<noexcept%> instead");
21050 #endif
21052 /* Consume the `throw'. */
21053 cp_lexer_consume_token (parser->lexer);
21055 /* Look for the `('. */
21056 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21058 /* Peek at the next token. */
21059 token = cp_lexer_peek_token (parser->lexer);
21060 /* If it's not a `)', then there is a type-id-list. */
21061 if (token->type != CPP_CLOSE_PAREN)
21063 /* Types may not be defined in an exception-specification. */
21064 saved_message = parser->type_definition_forbidden_message;
21065 parser->type_definition_forbidden_message
21066 = G_("types may not be defined in an exception-specification");
21067 /* Parse the type-id-list. */
21068 type_id_list = cp_parser_type_id_list (parser);
21069 /* Restore the saved message. */
21070 parser->type_definition_forbidden_message = saved_message;
21072 else
21073 type_id_list = empty_except_spec;
21075 /* Look for the `)'. */
21076 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21078 return type_id_list;
21081 /* Parse an (optional) type-id-list.
21083 type-id-list:
21084 type-id ... [opt]
21085 type-id-list , type-id ... [opt]
21087 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21088 in the order that the types were presented. */
21090 static tree
21091 cp_parser_type_id_list (cp_parser* parser)
21093 tree types = NULL_TREE;
21095 while (true)
21097 cp_token *token;
21098 tree type;
21100 /* Get the next type-id. */
21101 type = cp_parser_type_id (parser);
21102 /* Parse the optional ellipsis. */
21103 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21105 /* Consume the `...'. */
21106 cp_lexer_consume_token (parser->lexer);
21108 /* Turn the type into a pack expansion expression. */
21109 type = make_pack_expansion (type);
21111 /* Add it to the list. */
21112 types = add_exception_specifier (types, type, /*complain=*/1);
21113 /* Peek at the next token. */
21114 token = cp_lexer_peek_token (parser->lexer);
21115 /* If it is not a `,', we are done. */
21116 if (token->type != CPP_COMMA)
21117 break;
21118 /* Consume the `,'. */
21119 cp_lexer_consume_token (parser->lexer);
21122 return nreverse (types);
21125 /* Parse a try-block.
21127 try-block:
21128 try compound-statement handler-seq */
21130 static tree
21131 cp_parser_try_block (cp_parser* parser)
21133 tree try_block;
21135 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21136 try_block = begin_try_block ();
21137 cp_parser_compound_statement (parser, NULL, true, false);
21138 finish_try_block (try_block);
21139 cp_parser_handler_seq (parser);
21140 finish_handler_sequence (try_block);
21142 return try_block;
21145 /* Parse a function-try-block.
21147 function-try-block:
21148 try ctor-initializer [opt] function-body handler-seq */
21150 static bool
21151 cp_parser_function_try_block (cp_parser* parser)
21153 tree compound_stmt;
21154 tree try_block;
21155 bool ctor_initializer_p;
21157 /* Look for the `try' keyword. */
21158 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21159 return false;
21160 /* Let the rest of the front end know where we are. */
21161 try_block = begin_function_try_block (&compound_stmt);
21162 /* Parse the function-body. */
21163 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21164 (parser, /*in_function_try_block=*/true);
21165 /* We're done with the `try' part. */
21166 finish_function_try_block (try_block);
21167 /* Parse the handlers. */
21168 cp_parser_handler_seq (parser);
21169 /* We're done with the handlers. */
21170 finish_function_handler_sequence (try_block, compound_stmt);
21172 return ctor_initializer_p;
21175 /* Parse a handler-seq.
21177 handler-seq:
21178 handler handler-seq [opt] */
21180 static void
21181 cp_parser_handler_seq (cp_parser* parser)
21183 while (true)
21185 cp_token *token;
21187 /* Parse the handler. */
21188 cp_parser_handler (parser);
21189 /* Peek at the next token. */
21190 token = cp_lexer_peek_token (parser->lexer);
21191 /* If it's not `catch' then there are no more handlers. */
21192 if (!cp_parser_is_keyword (token, RID_CATCH))
21193 break;
21197 /* Parse a handler.
21199 handler:
21200 catch ( exception-declaration ) compound-statement */
21202 static void
21203 cp_parser_handler (cp_parser* parser)
21205 tree handler;
21206 tree declaration;
21208 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21209 handler = begin_handler ();
21210 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21211 declaration = cp_parser_exception_declaration (parser);
21212 finish_handler_parms (declaration, handler);
21213 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21214 cp_parser_compound_statement (parser, NULL, false, false);
21215 finish_handler (handler);
21218 /* Parse an exception-declaration.
21220 exception-declaration:
21221 type-specifier-seq declarator
21222 type-specifier-seq abstract-declarator
21223 type-specifier-seq
21226 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21227 ellipsis variant is used. */
21229 static tree
21230 cp_parser_exception_declaration (cp_parser* parser)
21232 cp_decl_specifier_seq type_specifiers;
21233 cp_declarator *declarator;
21234 const char *saved_message;
21236 /* If it's an ellipsis, it's easy to handle. */
21237 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21239 /* Consume the `...' token. */
21240 cp_lexer_consume_token (parser->lexer);
21241 return NULL_TREE;
21244 /* Types may not be defined in exception-declarations. */
21245 saved_message = parser->type_definition_forbidden_message;
21246 parser->type_definition_forbidden_message
21247 = G_("types may not be defined in exception-declarations");
21249 /* Parse the type-specifier-seq. */
21250 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21251 /*is_trailing_return=*/false,
21252 &type_specifiers);
21253 /* If it's a `)', then there is no declarator. */
21254 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21255 declarator = NULL;
21256 else
21257 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21258 /*ctor_dtor_or_conv_p=*/NULL,
21259 /*parenthesized_p=*/NULL,
21260 /*member_p=*/false);
21262 /* Restore the saved message. */
21263 parser->type_definition_forbidden_message = saved_message;
21265 if (!type_specifiers.any_specifiers_p)
21266 return error_mark_node;
21268 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21271 /* Parse a throw-expression.
21273 throw-expression:
21274 throw assignment-expression [opt]
21276 Returns a THROW_EXPR representing the throw-expression. */
21278 static tree
21279 cp_parser_throw_expression (cp_parser* parser)
21281 tree expression;
21282 cp_token* token;
21284 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21285 token = cp_lexer_peek_token (parser->lexer);
21286 /* Figure out whether or not there is an assignment-expression
21287 following the "throw" keyword. */
21288 if (token->type == CPP_COMMA
21289 || token->type == CPP_SEMICOLON
21290 || token->type == CPP_CLOSE_PAREN
21291 || token->type == CPP_CLOSE_SQUARE
21292 || token->type == CPP_CLOSE_BRACE
21293 || token->type == CPP_COLON)
21294 expression = NULL_TREE;
21295 else
21296 expression = cp_parser_assignment_expression (parser,
21297 /*cast_p=*/false, NULL);
21299 return build_throw (expression);
21302 /* GNU Extensions */
21304 /* Parse an (optional) asm-specification.
21306 asm-specification:
21307 asm ( string-literal )
21309 If the asm-specification is present, returns a STRING_CST
21310 corresponding to the string-literal. Otherwise, returns
21311 NULL_TREE. */
21313 static tree
21314 cp_parser_asm_specification_opt (cp_parser* parser)
21316 cp_token *token;
21317 tree asm_specification;
21319 /* Peek at the next token. */
21320 token = cp_lexer_peek_token (parser->lexer);
21321 /* If the next token isn't the `asm' keyword, then there's no
21322 asm-specification. */
21323 if (!cp_parser_is_keyword (token, RID_ASM))
21324 return NULL_TREE;
21326 /* Consume the `asm' token. */
21327 cp_lexer_consume_token (parser->lexer);
21328 /* Look for the `('. */
21329 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21331 /* Look for the string-literal. */
21332 asm_specification = cp_parser_string_literal (parser, false, false);
21334 /* Look for the `)'. */
21335 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21337 return asm_specification;
21340 /* Parse an asm-operand-list.
21342 asm-operand-list:
21343 asm-operand
21344 asm-operand-list , asm-operand
21346 asm-operand:
21347 string-literal ( expression )
21348 [ string-literal ] string-literal ( expression )
21350 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21351 each node is the expression. The TREE_PURPOSE is itself a
21352 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21353 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21354 is a STRING_CST for the string literal before the parenthesis. Returns
21355 ERROR_MARK_NODE if any of the operands are invalid. */
21357 static tree
21358 cp_parser_asm_operand_list (cp_parser* parser)
21360 tree asm_operands = NULL_TREE;
21361 bool invalid_operands = false;
21363 while (true)
21365 tree string_literal;
21366 tree expression;
21367 tree name;
21369 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21371 /* Consume the `[' token. */
21372 cp_lexer_consume_token (parser->lexer);
21373 /* Read the operand name. */
21374 name = cp_parser_identifier (parser);
21375 if (name != error_mark_node)
21376 name = build_string (IDENTIFIER_LENGTH (name),
21377 IDENTIFIER_POINTER (name));
21378 /* Look for the closing `]'. */
21379 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21381 else
21382 name = NULL_TREE;
21383 /* Look for the string-literal. */
21384 string_literal = cp_parser_string_literal (parser, false, false);
21386 /* Look for the `('. */
21387 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21388 /* Parse the expression. */
21389 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21390 /* Look for the `)'. */
21391 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21393 if (name == error_mark_node
21394 || string_literal == error_mark_node
21395 || expression == error_mark_node)
21396 invalid_operands = true;
21398 /* Add this operand to the list. */
21399 asm_operands = tree_cons (build_tree_list (name, string_literal),
21400 expression,
21401 asm_operands);
21402 /* If the next token is not a `,', there are no more
21403 operands. */
21404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21405 break;
21406 /* Consume the `,'. */
21407 cp_lexer_consume_token (parser->lexer);
21410 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21413 /* Parse an asm-clobber-list.
21415 asm-clobber-list:
21416 string-literal
21417 asm-clobber-list , string-literal
21419 Returns a TREE_LIST, indicating the clobbers in the order that they
21420 appeared. The TREE_VALUE of each node is a STRING_CST. */
21422 static tree
21423 cp_parser_asm_clobber_list (cp_parser* parser)
21425 tree clobbers = NULL_TREE;
21427 while (true)
21429 tree string_literal;
21431 /* Look for the string literal. */
21432 string_literal = cp_parser_string_literal (parser, false, false);
21433 /* Add it to the list. */
21434 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21435 /* If the next token is not a `,', then the list is
21436 complete. */
21437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21438 break;
21439 /* Consume the `,' token. */
21440 cp_lexer_consume_token (parser->lexer);
21443 return clobbers;
21446 /* Parse an asm-label-list.
21448 asm-label-list:
21449 identifier
21450 asm-label-list , identifier
21452 Returns a TREE_LIST, indicating the labels in the order that they
21453 appeared. The TREE_VALUE of each node is a label. */
21455 static tree
21456 cp_parser_asm_label_list (cp_parser* parser)
21458 tree labels = NULL_TREE;
21460 while (true)
21462 tree identifier, label, name;
21464 /* Look for the identifier. */
21465 identifier = cp_parser_identifier (parser);
21466 if (!error_operand_p (identifier))
21468 label = lookup_label (identifier);
21469 if (TREE_CODE (label) == LABEL_DECL)
21471 TREE_USED (label) = 1;
21472 check_goto (label);
21473 name = build_string (IDENTIFIER_LENGTH (identifier),
21474 IDENTIFIER_POINTER (identifier));
21475 labels = tree_cons (name, label, labels);
21478 /* If the next token is not a `,', then the list is
21479 complete. */
21480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21481 break;
21482 /* Consume the `,' token. */
21483 cp_lexer_consume_token (parser->lexer);
21486 return nreverse (labels);
21489 /* Return TRUE iff the next tokens in the stream are possibly the
21490 beginning of a GNU extension attribute. */
21492 static bool
21493 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21495 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21498 /* Return TRUE iff the next tokens in the stream are possibly the
21499 beginning of a standard C++-11 attribute specifier. */
21501 static bool
21502 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21504 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21507 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21508 beginning of a standard C++-11 attribute specifier. */
21510 static bool
21511 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21513 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21515 return (cxx_dialect >= cxx11
21516 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21517 || (token->type == CPP_OPEN_SQUARE
21518 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21519 && token->type == CPP_OPEN_SQUARE)));
21522 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21523 beginning of a GNU extension attribute. */
21525 static bool
21526 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21528 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21530 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21533 /* Return true iff the next tokens can be the beginning of either a
21534 GNU attribute list, or a standard C++11 attribute sequence. */
21536 static bool
21537 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21539 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21540 || cp_next_tokens_can_be_std_attribute_p (parser));
21543 /* Return true iff the next Nth tokens can be the beginning of either
21544 a GNU attribute list, or a standard C++11 attribute sequence. */
21546 static bool
21547 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21549 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21550 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21553 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21554 of GNU attributes, or return NULL. */
21556 static tree
21557 cp_parser_attributes_opt (cp_parser *parser)
21559 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21560 return cp_parser_gnu_attributes_opt (parser);
21561 return cp_parser_std_attribute_spec_seq (parser);
21564 #define CILK_SIMD_FN_CLAUSE_MASK \
21565 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21566 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21567 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21568 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21569 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21571 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21572 vector [(<clauses>)] */
21574 static void
21575 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21577 bool first_p = parser->cilk_simd_fn_info == NULL;
21578 cp_token *token = v_token;
21579 if (first_p)
21581 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21582 parser->cilk_simd_fn_info->error_seen = false;
21583 parser->cilk_simd_fn_info->fndecl_seen = false;
21584 parser->cilk_simd_fn_info->tokens = vNULL;
21586 int paren_scope = 0;
21587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21589 cp_lexer_consume_token (parser->lexer);
21590 v_token = cp_lexer_peek_token (parser->lexer);
21591 paren_scope++;
21593 while (paren_scope > 0)
21595 token = cp_lexer_peek_token (parser->lexer);
21596 if (token->type == CPP_OPEN_PAREN)
21597 paren_scope++;
21598 else if (token->type == CPP_CLOSE_PAREN)
21599 paren_scope--;
21600 /* Do not push the last ')' */
21601 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21602 cp_lexer_consume_token (parser->lexer);
21605 token->type = CPP_PRAGMA_EOL;
21606 parser->lexer->next_token = token;
21607 cp_lexer_consume_token (parser->lexer);
21609 struct cp_token_cache *cp
21610 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21611 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21614 /* Parse an (optional) series of attributes.
21616 attributes:
21617 attributes attribute
21619 attribute:
21620 __attribute__ (( attribute-list [opt] ))
21622 The return value is as for cp_parser_gnu_attribute_list. */
21624 static tree
21625 cp_parser_gnu_attributes_opt (cp_parser* parser)
21627 tree attributes = NULL_TREE;
21629 while (true)
21631 cp_token *token;
21632 tree attribute_list;
21633 bool ok = true;
21635 /* Peek at the next token. */
21636 token = cp_lexer_peek_token (parser->lexer);
21637 /* If it's not `__attribute__', then we're done. */
21638 if (token->keyword != RID_ATTRIBUTE)
21639 break;
21641 /* Consume the `__attribute__' keyword. */
21642 cp_lexer_consume_token (parser->lexer);
21643 /* Look for the two `(' tokens. */
21644 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21645 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21647 /* Peek at the next token. */
21648 token = cp_lexer_peek_token (parser->lexer);
21649 if (token->type != CPP_CLOSE_PAREN)
21650 /* Parse the attribute-list. */
21651 attribute_list = cp_parser_gnu_attribute_list (parser);
21652 else
21653 /* If the next token is a `)', then there is no attribute
21654 list. */
21655 attribute_list = NULL;
21657 /* Look for the two `)' tokens. */
21658 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21659 ok = false;
21660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21661 ok = false;
21662 if (!ok)
21663 cp_parser_skip_to_end_of_statement (parser);
21665 /* Add these new attributes to the list. */
21666 attributes = chainon (attributes, attribute_list);
21669 return attributes;
21672 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21673 "__vector" or "__vector__." */
21675 static inline bool
21676 is_cilkplus_vector_p (tree name)
21678 if (flag_cilkplus && is_attribute_p ("vector", name))
21679 return true;
21680 return false;
21683 /* Parse a GNU attribute-list.
21685 attribute-list:
21686 attribute
21687 attribute-list , attribute
21689 attribute:
21690 identifier
21691 identifier ( identifier )
21692 identifier ( identifier , expression-list )
21693 identifier ( expression-list )
21695 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21696 to an attribute. The TREE_PURPOSE of each node is the identifier
21697 indicating which attribute is in use. The TREE_VALUE represents
21698 the arguments, if any. */
21700 static tree
21701 cp_parser_gnu_attribute_list (cp_parser* parser)
21703 tree attribute_list = NULL_TREE;
21704 bool save_translate_strings_p = parser->translate_strings_p;
21706 parser->translate_strings_p = false;
21707 while (true)
21709 cp_token *token;
21710 tree identifier;
21711 tree attribute;
21713 /* Look for the identifier. We also allow keywords here; for
21714 example `__attribute__ ((const))' is legal. */
21715 token = cp_lexer_peek_token (parser->lexer);
21716 if (token->type == CPP_NAME
21717 || token->type == CPP_KEYWORD)
21719 tree arguments = NULL_TREE;
21721 /* Consume the token, but save it since we need it for the
21722 SIMD enabled function parsing. */
21723 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21725 /* Save away the identifier that indicates which attribute
21726 this is. */
21727 identifier = (token->type == CPP_KEYWORD)
21728 /* For keywords, use the canonical spelling, not the
21729 parsed identifier. */
21730 ? ridpointers[(int) token->keyword]
21731 : id_token->u.value;
21733 attribute = build_tree_list (identifier, NULL_TREE);
21735 /* Peek at the next token. */
21736 token = cp_lexer_peek_token (parser->lexer);
21737 /* If it's an `(', then parse the attribute arguments. */
21738 if (token->type == CPP_OPEN_PAREN)
21740 vec<tree, va_gc> *vec;
21741 int attr_flag = (attribute_takes_identifier_p (identifier)
21742 ? id_attr : normal_attr);
21743 if (is_cilkplus_vector_p (identifier))
21745 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21746 continue;
21748 else
21749 vec = cp_parser_parenthesized_expression_list
21750 (parser, attr_flag, /*cast_p=*/false,
21751 /*allow_expansion_p=*/false,
21752 /*non_constant_p=*/NULL);
21753 if (vec == NULL)
21754 arguments = error_mark_node;
21755 else
21757 arguments = build_tree_list_vec (vec);
21758 release_tree_vector (vec);
21760 /* Save the arguments away. */
21761 TREE_VALUE (attribute) = arguments;
21763 else if (is_cilkplus_vector_p (identifier))
21765 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21766 continue;
21769 if (arguments != error_mark_node)
21771 /* Add this attribute to the list. */
21772 TREE_CHAIN (attribute) = attribute_list;
21773 attribute_list = attribute;
21776 token = cp_lexer_peek_token (parser->lexer);
21778 /* Now, look for more attributes. If the next token isn't a
21779 `,', we're done. */
21780 if (token->type != CPP_COMMA)
21781 break;
21783 /* Consume the comma and keep going. */
21784 cp_lexer_consume_token (parser->lexer);
21786 parser->translate_strings_p = save_translate_strings_p;
21788 /* We built up the list in reverse order. */
21789 return nreverse (attribute_list);
21792 /* Parse a standard C++11 attribute.
21794 The returned representation is a TREE_LIST which TREE_PURPOSE is
21795 the scoped name of the attribute, and the TREE_VALUE is its
21796 arguments list.
21798 Note that the scoped name of the attribute is itself a TREE_LIST
21799 which TREE_PURPOSE is the namespace of the attribute, and
21800 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21801 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21802 and which TREE_PURPOSE is directly the attribute name.
21804 Clients of the attribute code should use get_attribute_namespace
21805 and get_attribute_name to get the actual namespace and name of
21806 attributes, regardless of their being GNU or C++11 attributes.
21808 attribute:
21809 attribute-token attribute-argument-clause [opt]
21811 attribute-token:
21812 identifier
21813 attribute-scoped-token
21815 attribute-scoped-token:
21816 attribute-namespace :: identifier
21818 attribute-namespace:
21819 identifier
21821 attribute-argument-clause:
21822 ( balanced-token-seq )
21824 balanced-token-seq:
21825 balanced-token [opt]
21826 balanced-token-seq balanced-token
21828 balanced-token:
21829 ( balanced-token-seq )
21830 [ balanced-token-seq ]
21831 { balanced-token-seq }. */
21833 static tree
21834 cp_parser_std_attribute (cp_parser *parser)
21836 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21837 cp_token *token;
21839 /* First, parse name of the the attribute, a.k.a
21840 attribute-token. */
21842 token = cp_lexer_peek_token (parser->lexer);
21843 if (token->type == CPP_NAME)
21844 attr_id = token->u.value;
21845 else if (token->type == CPP_KEYWORD)
21846 attr_id = ridpointers[(int) token->keyword];
21847 else if (token->flags & NAMED_OP)
21848 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21850 if (attr_id == NULL_TREE)
21851 return NULL_TREE;
21853 cp_lexer_consume_token (parser->lexer);
21855 token = cp_lexer_peek_token (parser->lexer);
21856 if (token->type == CPP_SCOPE)
21858 /* We are seeing a scoped attribute token. */
21860 cp_lexer_consume_token (parser->lexer);
21861 attr_ns = attr_id;
21863 token = cp_lexer_consume_token (parser->lexer);
21864 if (token->type == CPP_NAME)
21865 attr_id = token->u.value;
21866 else if (token->type == CPP_KEYWORD)
21867 attr_id = ridpointers[(int) token->keyword];
21868 else
21870 error_at (token->location,
21871 "expected an identifier for the attribute name");
21872 return error_mark_node;
21874 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21875 NULL_TREE);
21876 token = cp_lexer_peek_token (parser->lexer);
21878 else
21880 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21881 NULL_TREE);
21882 /* C++11 noreturn attribute is equivalent to GNU's. */
21883 if (is_attribute_p ("noreturn", attr_id))
21884 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21885 /* C++14 deprecated attribute is equivalent to GNU's. */
21886 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21887 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21890 /* Now parse the optional argument clause of the attribute. */
21892 if (token->type != CPP_OPEN_PAREN)
21893 return attribute;
21896 vec<tree, va_gc> *vec;
21897 int attr_flag = normal_attr;
21899 if (attr_ns == get_identifier ("gnu")
21900 && attribute_takes_identifier_p (attr_id))
21901 /* A GNU attribute that takes an identifier in parameter. */
21902 attr_flag = id_attr;
21904 vec = cp_parser_parenthesized_expression_list
21905 (parser, attr_flag, /*cast_p=*/false,
21906 /*allow_expansion_p=*/true,
21907 /*non_constant_p=*/NULL);
21908 if (vec == NULL)
21909 arguments = error_mark_node;
21910 else
21912 arguments = build_tree_list_vec (vec);
21913 release_tree_vector (vec);
21916 if (arguments == error_mark_node)
21917 attribute = error_mark_node;
21918 else
21919 TREE_VALUE (attribute) = arguments;
21922 return attribute;
21925 /* Parse a list of standard C++-11 attributes.
21927 attribute-list:
21928 attribute [opt]
21929 attribute-list , attribute[opt]
21930 attribute ...
21931 attribute-list , attribute ...
21934 static tree
21935 cp_parser_std_attribute_list (cp_parser *parser)
21937 tree attributes = NULL_TREE, attribute = NULL_TREE;
21938 cp_token *token = NULL;
21940 while (true)
21942 attribute = cp_parser_std_attribute (parser);
21943 if (attribute == error_mark_node)
21944 break;
21945 if (attribute != NULL_TREE)
21947 TREE_CHAIN (attribute) = attributes;
21948 attributes = attribute;
21950 token = cp_lexer_peek_token (parser->lexer);
21951 if (token->type != CPP_COMMA)
21952 break;
21953 cp_lexer_consume_token (parser->lexer);
21955 attributes = nreverse (attributes);
21956 return attributes;
21959 /* Parse a standard C++-11 attribute specifier.
21961 attribute-specifier:
21962 [ [ attribute-list ] ]
21963 alignment-specifier
21965 alignment-specifier:
21966 alignas ( type-id ... [opt] )
21967 alignas ( alignment-expression ... [opt] ). */
21969 static tree
21970 cp_parser_std_attribute_spec (cp_parser *parser)
21972 tree attributes = NULL_TREE;
21973 cp_token *token = cp_lexer_peek_token (parser->lexer);
21975 if (token->type == CPP_OPEN_SQUARE
21976 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21978 cp_lexer_consume_token (parser->lexer);
21979 cp_lexer_consume_token (parser->lexer);
21981 attributes = cp_parser_std_attribute_list (parser);
21983 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21984 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21985 cp_parser_skip_to_end_of_statement (parser);
21986 else
21987 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21988 when we are sure that we have actually parsed them. */
21989 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21991 else
21993 tree alignas_expr;
21995 /* Look for an alignment-specifier. */
21997 token = cp_lexer_peek_token (parser->lexer);
21999 if (token->type != CPP_KEYWORD
22000 || token->keyword != RID_ALIGNAS)
22001 return NULL_TREE;
22003 cp_lexer_consume_token (parser->lexer);
22004 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22006 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22008 cp_parser_error (parser, "expected %<(%>");
22009 return error_mark_node;
22012 cp_parser_parse_tentatively (parser);
22013 alignas_expr = cp_parser_type_id (parser);
22015 if (!cp_parser_parse_definitely (parser))
22017 gcc_assert (alignas_expr == error_mark_node
22018 || alignas_expr == NULL_TREE);
22020 alignas_expr =
22021 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22022 /**cp_id_kind=*/NULL);
22023 if (alignas_expr == error_mark_node)
22024 cp_parser_skip_to_end_of_statement (parser);
22025 if (alignas_expr == NULL_TREE
22026 || alignas_expr == error_mark_node)
22027 return alignas_expr;
22030 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22032 cp_parser_error (parser, "expected %<)%>");
22033 return error_mark_node;
22036 alignas_expr = cxx_alignas_expr (alignas_expr);
22038 /* Build the C++-11 representation of an 'aligned'
22039 attribute. */
22040 attributes =
22041 build_tree_list (build_tree_list (get_identifier ("gnu"),
22042 get_identifier ("aligned")),
22043 build_tree_list (NULL_TREE, alignas_expr));
22046 return attributes;
22049 /* Parse a standard C++-11 attribute-specifier-seq.
22051 attribute-specifier-seq:
22052 attribute-specifier-seq [opt] attribute-specifier
22055 static tree
22056 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22058 tree attr_specs = NULL;
22060 while (true)
22062 tree attr_spec = cp_parser_std_attribute_spec (parser);
22063 if (attr_spec == NULL_TREE)
22064 break;
22065 if (attr_spec == error_mark_node)
22066 return error_mark_node;
22068 TREE_CHAIN (attr_spec) = attr_specs;
22069 attr_specs = attr_spec;
22072 attr_specs = nreverse (attr_specs);
22073 return attr_specs;
22076 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22077 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22078 current value of the PEDANTIC flag, regardless of whether or not
22079 the `__extension__' keyword is present. The caller is responsible
22080 for restoring the value of the PEDANTIC flag. */
22082 static bool
22083 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22085 /* Save the old value of the PEDANTIC flag. */
22086 *saved_pedantic = pedantic;
22088 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22090 /* Consume the `__extension__' token. */
22091 cp_lexer_consume_token (parser->lexer);
22092 /* We're not being pedantic while the `__extension__' keyword is
22093 in effect. */
22094 pedantic = 0;
22096 return true;
22099 return false;
22102 /* Parse a label declaration.
22104 label-declaration:
22105 __label__ label-declarator-seq ;
22107 label-declarator-seq:
22108 identifier , label-declarator-seq
22109 identifier */
22111 static void
22112 cp_parser_label_declaration (cp_parser* parser)
22114 /* Look for the `__label__' keyword. */
22115 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22117 while (true)
22119 tree identifier;
22121 /* Look for an identifier. */
22122 identifier = cp_parser_identifier (parser);
22123 /* If we failed, stop. */
22124 if (identifier == error_mark_node)
22125 break;
22126 /* Declare it as a label. */
22127 finish_label_decl (identifier);
22128 /* If the next token is a `;', stop. */
22129 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22130 break;
22131 /* Look for the `,' separating the label declarations. */
22132 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22135 /* Look for the final `;'. */
22136 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22139 /* Support Functions */
22141 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22142 NAME should have one of the representations used for an
22143 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22144 is returned. If PARSER->SCOPE is a dependent type, then a
22145 SCOPE_REF is returned.
22147 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22148 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22149 was formed. Abstractly, such entities should not be passed to this
22150 function, because they do not need to be looked up, but it is
22151 simpler to check for this special case here, rather than at the
22152 call-sites.
22154 In cases not explicitly covered above, this function returns a
22155 DECL, OVERLOAD, or baselink representing the result of the lookup.
22156 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22157 is returned.
22159 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22160 (e.g., "struct") that was used. In that case bindings that do not
22161 refer to types are ignored.
22163 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22164 ignored.
22166 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22167 are ignored.
22169 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22170 types.
22172 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22173 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22174 NULL_TREE otherwise. */
22176 static tree
22177 cp_parser_lookup_name (cp_parser *parser, tree name,
22178 enum tag_types tag_type,
22179 bool is_template,
22180 bool is_namespace,
22181 bool check_dependency,
22182 tree *ambiguous_decls,
22183 location_t name_location)
22185 tree decl;
22186 tree object_type = parser->context->object_type;
22188 /* Assume that the lookup will be unambiguous. */
22189 if (ambiguous_decls)
22190 *ambiguous_decls = NULL_TREE;
22192 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22193 no longer valid. Note that if we are parsing tentatively, and
22194 the parse fails, OBJECT_TYPE will be automatically restored. */
22195 parser->context->object_type = NULL_TREE;
22197 if (name == error_mark_node)
22198 return error_mark_node;
22200 /* A template-id has already been resolved; there is no lookup to
22201 do. */
22202 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22203 return name;
22204 if (BASELINK_P (name))
22206 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22207 == TEMPLATE_ID_EXPR);
22208 return name;
22211 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22212 it should already have been checked to make sure that the name
22213 used matches the type being destroyed. */
22214 if (TREE_CODE (name) == BIT_NOT_EXPR)
22216 tree type;
22218 /* Figure out to which type this destructor applies. */
22219 if (parser->scope)
22220 type = parser->scope;
22221 else if (object_type)
22222 type = object_type;
22223 else
22224 type = current_class_type;
22225 /* If that's not a class type, there is no destructor. */
22226 if (!type || !CLASS_TYPE_P (type))
22227 return error_mark_node;
22228 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22229 lazily_declare_fn (sfk_destructor, type);
22230 if (!CLASSTYPE_DESTRUCTORS (type))
22231 return error_mark_node;
22232 /* If it was a class type, return the destructor. */
22233 return CLASSTYPE_DESTRUCTORS (type);
22236 /* By this point, the NAME should be an ordinary identifier. If
22237 the id-expression was a qualified name, the qualifying scope is
22238 stored in PARSER->SCOPE at this point. */
22239 gcc_assert (identifier_p (name));
22241 /* Perform the lookup. */
22242 if (parser->scope)
22244 bool dependent_p;
22246 if (parser->scope == error_mark_node)
22247 return error_mark_node;
22249 /* If the SCOPE is dependent, the lookup must be deferred until
22250 the template is instantiated -- unless we are explicitly
22251 looking up names in uninstantiated templates. Even then, we
22252 cannot look up the name if the scope is not a class type; it
22253 might, for example, be a template type parameter. */
22254 dependent_p = (TYPE_P (parser->scope)
22255 && dependent_scope_p (parser->scope));
22256 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22257 && dependent_p)
22258 /* Defer lookup. */
22259 decl = error_mark_node;
22260 else
22262 tree pushed_scope = NULL_TREE;
22264 /* If PARSER->SCOPE is a dependent type, then it must be a
22265 class type, and we must not be checking dependencies;
22266 otherwise, we would have processed this lookup above. So
22267 that PARSER->SCOPE is not considered a dependent base by
22268 lookup_member, we must enter the scope here. */
22269 if (dependent_p)
22270 pushed_scope = push_scope (parser->scope);
22272 /* If the PARSER->SCOPE is a template specialization, it
22273 may be instantiated during name lookup. In that case,
22274 errors may be issued. Even if we rollback the current
22275 tentative parse, those errors are valid. */
22276 decl = lookup_qualified_name (parser->scope, name,
22277 tag_type != none_type,
22278 /*complain=*/true);
22280 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22281 lookup result and the nested-name-specifier nominates a class C:
22282 * if the name specified after the nested-name-specifier, when
22283 looked up in C, is the injected-class-name of C (Clause 9), or
22284 * if the name specified after the nested-name-specifier is the
22285 same as the identifier or the simple-template-id's template-
22286 name in the last component of the nested-name-specifier,
22287 the name is instead considered to name the constructor of
22288 class C. [ Note: for example, the constructor is not an
22289 acceptable lookup result in an elaborated-type-specifier so
22290 the constructor would not be used in place of the
22291 injected-class-name. --end note ] Such a constructor name
22292 shall be used only in the declarator-id of a declaration that
22293 names a constructor or in a using-declaration. */
22294 if (tag_type == none_type
22295 && DECL_SELF_REFERENCE_P (decl)
22296 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22297 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22298 tag_type != none_type,
22299 /*complain=*/true);
22301 /* If we have a single function from a using decl, pull it out. */
22302 if (TREE_CODE (decl) == OVERLOAD
22303 && !really_overloaded_fn (decl))
22304 decl = OVL_FUNCTION (decl);
22306 if (pushed_scope)
22307 pop_scope (pushed_scope);
22310 /* If the scope is a dependent type and either we deferred lookup or
22311 we did lookup but didn't find the name, rememeber the name. */
22312 if (decl == error_mark_node && TYPE_P (parser->scope)
22313 && dependent_type_p (parser->scope))
22315 if (tag_type)
22317 tree type;
22319 /* The resolution to Core Issue 180 says that `struct
22320 A::B' should be considered a type-name, even if `A'
22321 is dependent. */
22322 type = make_typename_type (parser->scope, name, tag_type,
22323 /*complain=*/tf_error);
22324 if (type != error_mark_node)
22325 decl = TYPE_NAME (type);
22327 else if (is_template
22328 && (cp_parser_next_token_ends_template_argument_p (parser)
22329 || cp_lexer_next_token_is (parser->lexer,
22330 CPP_CLOSE_PAREN)))
22331 decl = make_unbound_class_template (parser->scope,
22332 name, NULL_TREE,
22333 /*complain=*/tf_error);
22334 else
22335 decl = build_qualified_name (/*type=*/NULL_TREE,
22336 parser->scope, name,
22337 is_template);
22339 parser->qualifying_scope = parser->scope;
22340 parser->object_scope = NULL_TREE;
22342 else if (object_type)
22344 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22345 OBJECT_TYPE is not a class. */
22346 if (CLASS_TYPE_P (object_type))
22347 /* If the OBJECT_TYPE is a template specialization, it may
22348 be instantiated during name lookup. In that case, errors
22349 may be issued. Even if we rollback the current tentative
22350 parse, those errors are valid. */
22351 decl = lookup_member (object_type,
22352 name,
22353 /*protect=*/0,
22354 tag_type != none_type,
22355 tf_warning_or_error);
22356 else
22357 decl = NULL_TREE;
22359 if (!decl)
22360 /* Look it up in the enclosing context. */
22361 decl = lookup_name_real (name, tag_type != none_type,
22362 /*nonclass=*/0,
22363 /*block_p=*/true, is_namespace, 0);
22364 parser->object_scope = object_type;
22365 parser->qualifying_scope = NULL_TREE;
22367 else
22369 decl = lookup_name_real (name, tag_type != none_type,
22370 /*nonclass=*/0,
22371 /*block_p=*/true, is_namespace, 0);
22372 parser->qualifying_scope = NULL_TREE;
22373 parser->object_scope = NULL_TREE;
22376 /* If the lookup failed, let our caller know. */
22377 if (!decl || decl == error_mark_node)
22378 return error_mark_node;
22380 /* Pull out the template from an injected-class-name (or multiple). */
22381 if (is_template)
22382 decl = maybe_get_template_decl_from_type_decl (decl);
22384 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22385 if (TREE_CODE (decl) == TREE_LIST)
22387 if (ambiguous_decls)
22388 *ambiguous_decls = decl;
22389 /* The error message we have to print is too complicated for
22390 cp_parser_error, so we incorporate its actions directly. */
22391 if (!cp_parser_simulate_error (parser))
22393 error_at (name_location, "reference to %qD is ambiguous",
22394 name);
22395 print_candidates (decl);
22397 return error_mark_node;
22400 gcc_assert (DECL_P (decl)
22401 || TREE_CODE (decl) == OVERLOAD
22402 || TREE_CODE (decl) == SCOPE_REF
22403 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22404 || BASELINK_P (decl));
22406 /* If we have resolved the name of a member declaration, check to
22407 see if the declaration is accessible. When the name resolves to
22408 set of overloaded functions, accessibility is checked when
22409 overload resolution is done.
22411 During an explicit instantiation, access is not checked at all,
22412 as per [temp.explicit]. */
22413 if (DECL_P (decl))
22414 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22416 maybe_record_typedef_use (decl);
22418 return decl;
22421 /* Like cp_parser_lookup_name, but for use in the typical case where
22422 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22423 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22425 static tree
22426 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22428 return cp_parser_lookup_name (parser, name,
22429 none_type,
22430 /*is_template=*/false,
22431 /*is_namespace=*/false,
22432 /*check_dependency=*/true,
22433 /*ambiguous_decls=*/NULL,
22434 location);
22437 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22438 the current context, return the TYPE_DECL. If TAG_NAME_P is
22439 true, the DECL indicates the class being defined in a class-head,
22440 or declared in an elaborated-type-specifier.
22442 Otherwise, return DECL. */
22444 static tree
22445 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22447 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22448 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22450 struct A {
22451 template <typename T> struct B;
22454 template <typename T> struct A::B {};
22456 Similarly, in an elaborated-type-specifier:
22458 namespace N { struct X{}; }
22460 struct A {
22461 template <typename T> friend struct N::X;
22464 However, if the DECL refers to a class type, and we are in
22465 the scope of the class, then the name lookup automatically
22466 finds the TYPE_DECL created by build_self_reference rather
22467 than a TEMPLATE_DECL. For example, in:
22469 template <class T> struct S {
22470 S s;
22473 there is no need to handle such case. */
22475 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22476 return DECL_TEMPLATE_RESULT (decl);
22478 return decl;
22481 /* If too many, or too few, template-parameter lists apply to the
22482 declarator, issue an error message. Returns TRUE if all went well,
22483 and FALSE otherwise. */
22485 static bool
22486 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22487 cp_declarator *declarator,
22488 location_t declarator_location)
22490 switch (declarator->kind)
22492 case cdk_id:
22494 unsigned num_templates = 0;
22495 tree scope = declarator->u.id.qualifying_scope;
22497 if (scope)
22498 num_templates = num_template_headers_for_class (scope);
22499 else if (TREE_CODE (declarator->u.id.unqualified_name)
22500 == TEMPLATE_ID_EXPR)
22501 /* If the DECLARATOR has the form `X<y>' then it uses one
22502 additional level of template parameters. */
22503 ++num_templates;
22505 return cp_parser_check_template_parameters
22506 (parser, num_templates, declarator_location, declarator);
22509 case cdk_function:
22510 case cdk_array:
22511 case cdk_pointer:
22512 case cdk_reference:
22513 case cdk_ptrmem:
22514 return (cp_parser_check_declarator_template_parameters
22515 (parser, declarator->declarator, declarator_location));
22517 case cdk_error:
22518 return true;
22520 default:
22521 gcc_unreachable ();
22523 return false;
22526 /* NUM_TEMPLATES were used in the current declaration. If that is
22527 invalid, return FALSE and issue an error messages. Otherwise,
22528 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22529 declarator and we can print more accurate diagnostics. */
22531 static bool
22532 cp_parser_check_template_parameters (cp_parser* parser,
22533 unsigned num_templates,
22534 location_t location,
22535 cp_declarator *declarator)
22537 /* If there are the same number of template classes and parameter
22538 lists, that's OK. */
22539 if (parser->num_template_parameter_lists == num_templates)
22540 return true;
22541 /* If there are more, but only one more, then we are referring to a
22542 member template. That's OK too. */
22543 if (parser->num_template_parameter_lists == num_templates + 1)
22544 return true;
22545 /* If there are more template classes than parameter lists, we have
22546 something like:
22548 template <class T> void S<T>::R<T>::f (); */
22549 if (parser->num_template_parameter_lists < num_templates)
22551 if (declarator && !current_function_decl)
22552 error_at (location, "specializing member %<%T::%E%> "
22553 "requires %<template<>%> syntax",
22554 declarator->u.id.qualifying_scope,
22555 declarator->u.id.unqualified_name);
22556 else if (declarator)
22557 error_at (location, "invalid declaration of %<%T::%E%>",
22558 declarator->u.id.qualifying_scope,
22559 declarator->u.id.unqualified_name);
22560 else
22561 error_at (location, "too few template-parameter-lists");
22562 return false;
22564 /* Otherwise, there are too many template parameter lists. We have
22565 something like:
22567 template <class T> template <class U> void S::f(); */
22568 error_at (location, "too many template-parameter-lists");
22569 return false;
22572 /* Parse an optional `::' token indicating that the following name is
22573 from the global namespace. If so, PARSER->SCOPE is set to the
22574 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22575 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22576 Returns the new value of PARSER->SCOPE, if the `::' token is
22577 present, and NULL_TREE otherwise. */
22579 static tree
22580 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22582 cp_token *token;
22584 /* Peek at the next token. */
22585 token = cp_lexer_peek_token (parser->lexer);
22586 /* If we're looking at a `::' token then we're starting from the
22587 global namespace, not our current location. */
22588 if (token->type == CPP_SCOPE)
22590 /* Consume the `::' token. */
22591 cp_lexer_consume_token (parser->lexer);
22592 /* Set the SCOPE so that we know where to start the lookup. */
22593 parser->scope = global_namespace;
22594 parser->qualifying_scope = global_namespace;
22595 parser->object_scope = NULL_TREE;
22597 return parser->scope;
22599 else if (!current_scope_valid_p)
22601 parser->scope = NULL_TREE;
22602 parser->qualifying_scope = NULL_TREE;
22603 parser->object_scope = NULL_TREE;
22606 return NULL_TREE;
22609 /* Returns TRUE if the upcoming token sequence is the start of a
22610 constructor declarator. If FRIEND_P is true, the declarator is
22611 preceded by the `friend' specifier. */
22613 static bool
22614 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22616 bool constructor_p;
22617 bool outside_class_specifier_p;
22618 tree nested_name_specifier;
22619 cp_token *next_token;
22621 /* The common case is that this is not a constructor declarator, so
22622 try to avoid doing lots of work if at all possible. It's not
22623 valid declare a constructor at function scope. */
22624 if (parser->in_function_body)
22625 return false;
22626 /* And only certain tokens can begin a constructor declarator. */
22627 next_token = cp_lexer_peek_token (parser->lexer);
22628 if (next_token->type != CPP_NAME
22629 && next_token->type != CPP_SCOPE
22630 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22631 && next_token->type != CPP_TEMPLATE_ID)
22632 return false;
22634 /* Parse tentatively; we are going to roll back all of the tokens
22635 consumed here. */
22636 cp_parser_parse_tentatively (parser);
22637 /* Assume that we are looking at a constructor declarator. */
22638 constructor_p = true;
22640 /* Look for the optional `::' operator. */
22641 cp_parser_global_scope_opt (parser,
22642 /*current_scope_valid_p=*/false);
22643 /* Look for the nested-name-specifier. */
22644 nested_name_specifier
22645 = (cp_parser_nested_name_specifier_opt (parser,
22646 /*typename_keyword_p=*/false,
22647 /*check_dependency_p=*/false,
22648 /*type_p=*/false,
22649 /*is_declaration=*/false));
22651 outside_class_specifier_p = (!at_class_scope_p ()
22652 || !TYPE_BEING_DEFINED (current_class_type)
22653 || friend_p);
22655 /* Outside of a class-specifier, there must be a
22656 nested-name-specifier. */
22657 if (!nested_name_specifier && outside_class_specifier_p)
22658 constructor_p = false;
22659 else if (nested_name_specifier == error_mark_node)
22660 constructor_p = false;
22662 /* If we have a class scope, this is easy; DR 147 says that S::S always
22663 names the constructor, and no other qualified name could. */
22664 if (constructor_p && nested_name_specifier
22665 && CLASS_TYPE_P (nested_name_specifier))
22667 tree id = cp_parser_unqualified_id (parser,
22668 /*template_keyword_p=*/false,
22669 /*check_dependency_p=*/false,
22670 /*declarator_p=*/true,
22671 /*optional_p=*/false);
22672 if (is_overloaded_fn (id))
22673 id = DECL_NAME (get_first_fn (id));
22674 if (!constructor_name_p (id, nested_name_specifier))
22675 constructor_p = false;
22677 /* If we still think that this might be a constructor-declarator,
22678 look for a class-name. */
22679 else if (constructor_p)
22681 /* If we have:
22683 template <typename T> struct S {
22684 S();
22687 we must recognize that the nested `S' names a class. */
22688 tree type_decl;
22689 type_decl = cp_parser_class_name (parser,
22690 /*typename_keyword_p=*/false,
22691 /*template_keyword_p=*/false,
22692 none_type,
22693 /*check_dependency_p=*/false,
22694 /*class_head_p=*/false,
22695 /*is_declaration=*/false);
22696 /* If there was no class-name, then this is not a constructor.
22697 Otherwise, if we are in a class-specifier and we aren't
22698 handling a friend declaration, check that its type matches
22699 current_class_type (c++/38313). Note: error_mark_node
22700 is left alone for error recovery purposes. */
22701 constructor_p = (!cp_parser_error_occurred (parser)
22702 && (outside_class_specifier_p
22703 || type_decl == error_mark_node
22704 || same_type_p (current_class_type,
22705 TREE_TYPE (type_decl))));
22707 /* If we're still considering a constructor, we have to see a `(',
22708 to begin the parameter-declaration-clause, followed by either a
22709 `)', an `...', or a decl-specifier. We need to check for a
22710 type-specifier to avoid being fooled into thinking that:
22712 S (f) (int);
22714 is a constructor. (It is actually a function named `f' that
22715 takes one parameter (of type `int') and returns a value of type
22716 `S'. */
22717 if (constructor_p
22718 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22719 constructor_p = false;
22721 if (constructor_p
22722 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22723 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22724 /* A parameter declaration begins with a decl-specifier,
22725 which is either the "attribute" keyword, a storage class
22726 specifier, or (usually) a type-specifier. */
22727 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22729 tree type;
22730 tree pushed_scope = NULL_TREE;
22731 unsigned saved_num_template_parameter_lists;
22733 /* Names appearing in the type-specifier should be looked up
22734 in the scope of the class. */
22735 if (current_class_type)
22736 type = NULL_TREE;
22737 else
22739 type = TREE_TYPE (type_decl);
22740 if (TREE_CODE (type) == TYPENAME_TYPE)
22742 type = resolve_typename_type (type,
22743 /*only_current_p=*/false);
22744 if (TREE_CODE (type) == TYPENAME_TYPE)
22746 cp_parser_abort_tentative_parse (parser);
22747 return false;
22750 pushed_scope = push_scope (type);
22753 /* Inside the constructor parameter list, surrounding
22754 template-parameter-lists do not apply. */
22755 saved_num_template_parameter_lists
22756 = parser->num_template_parameter_lists;
22757 parser->num_template_parameter_lists = 0;
22759 /* Look for the type-specifier. */
22760 cp_parser_type_specifier (parser,
22761 CP_PARSER_FLAGS_NONE,
22762 /*decl_specs=*/NULL,
22763 /*is_declarator=*/true,
22764 /*declares_class_or_enum=*/NULL,
22765 /*is_cv_qualifier=*/NULL);
22767 parser->num_template_parameter_lists
22768 = saved_num_template_parameter_lists;
22770 /* Leave the scope of the class. */
22771 if (pushed_scope)
22772 pop_scope (pushed_scope);
22774 constructor_p = !cp_parser_error_occurred (parser);
22778 /* We did not really want to consume any tokens. */
22779 cp_parser_abort_tentative_parse (parser);
22781 return constructor_p;
22784 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22785 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22786 they must be performed once we are in the scope of the function.
22788 Returns the function defined. */
22790 static tree
22791 cp_parser_function_definition_from_specifiers_and_declarator
22792 (cp_parser* parser,
22793 cp_decl_specifier_seq *decl_specifiers,
22794 tree attributes,
22795 const cp_declarator *declarator)
22797 tree fn;
22798 bool success_p;
22800 /* Begin the function-definition. */
22801 success_p = start_function (decl_specifiers, declarator, attributes);
22803 /* The things we're about to see are not directly qualified by any
22804 template headers we've seen thus far. */
22805 reset_specialization ();
22807 /* If there were names looked up in the decl-specifier-seq that we
22808 did not check, check them now. We must wait until we are in the
22809 scope of the function to perform the checks, since the function
22810 might be a friend. */
22811 perform_deferred_access_checks (tf_warning_or_error);
22813 if (success_p)
22815 cp_finalize_omp_declare_simd (parser, current_function_decl);
22816 parser->omp_declare_simd = NULL;
22819 if (!success_p)
22821 /* Skip the entire function. */
22822 cp_parser_skip_to_end_of_block_or_statement (parser);
22823 fn = error_mark_node;
22825 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22827 /* Seen already, skip it. An error message has already been output. */
22828 cp_parser_skip_to_end_of_block_or_statement (parser);
22829 fn = current_function_decl;
22830 current_function_decl = NULL_TREE;
22831 /* If this is a function from a class, pop the nested class. */
22832 if (current_class_name)
22833 pop_nested_class ();
22835 else
22837 timevar_id_t tv;
22838 if (DECL_DECLARED_INLINE_P (current_function_decl))
22839 tv = TV_PARSE_INLINE;
22840 else
22841 tv = TV_PARSE_FUNC;
22842 timevar_push (tv);
22843 fn = cp_parser_function_definition_after_declarator (parser,
22844 /*inline_p=*/false);
22845 timevar_pop (tv);
22848 return fn;
22851 /* Parse the part of a function-definition that follows the
22852 declarator. INLINE_P is TRUE iff this function is an inline
22853 function defined within a class-specifier.
22855 Returns the function defined. */
22857 static tree
22858 cp_parser_function_definition_after_declarator (cp_parser* parser,
22859 bool inline_p)
22861 tree fn;
22862 bool ctor_initializer_p = false;
22863 bool saved_in_unbraced_linkage_specification_p;
22864 bool saved_in_function_body;
22865 unsigned saved_num_template_parameter_lists;
22866 cp_token *token;
22867 bool fully_implicit_function_template_p
22868 = parser->fully_implicit_function_template_p;
22869 parser->fully_implicit_function_template_p = false;
22870 tree implicit_template_parms
22871 = parser->implicit_template_parms;
22872 parser->implicit_template_parms = 0;
22873 cp_binding_level* implicit_template_scope
22874 = parser->implicit_template_scope;
22875 parser->implicit_template_scope = 0;
22877 saved_in_function_body = parser->in_function_body;
22878 parser->in_function_body = true;
22879 /* If the next token is `return', then the code may be trying to
22880 make use of the "named return value" extension that G++ used to
22881 support. */
22882 token = cp_lexer_peek_token (parser->lexer);
22883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22885 /* Consume the `return' keyword. */
22886 cp_lexer_consume_token (parser->lexer);
22887 /* Look for the identifier that indicates what value is to be
22888 returned. */
22889 cp_parser_identifier (parser);
22890 /* Issue an error message. */
22891 error_at (token->location,
22892 "named return values are no longer supported");
22893 /* Skip tokens until we reach the start of the function body. */
22894 while (true)
22896 cp_token *token = cp_lexer_peek_token (parser->lexer);
22897 if (token->type == CPP_OPEN_BRACE
22898 || token->type == CPP_EOF
22899 || token->type == CPP_PRAGMA_EOL)
22900 break;
22901 cp_lexer_consume_token (parser->lexer);
22904 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22905 anything declared inside `f'. */
22906 saved_in_unbraced_linkage_specification_p
22907 = parser->in_unbraced_linkage_specification_p;
22908 parser->in_unbraced_linkage_specification_p = false;
22909 /* Inside the function, surrounding template-parameter-lists do not
22910 apply. */
22911 saved_num_template_parameter_lists
22912 = parser->num_template_parameter_lists;
22913 parser->num_template_parameter_lists = 0;
22915 start_lambda_scope (current_function_decl);
22917 /* If the next token is `try', `__transaction_atomic', or
22918 `__transaction_relaxed`, then we are looking at either function-try-block
22919 or function-transaction-block. Note that all of these include the
22920 function-body. */
22921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22922 ctor_initializer_p = cp_parser_function_transaction (parser,
22923 RID_TRANSACTION_ATOMIC);
22924 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22925 RID_TRANSACTION_RELAXED))
22926 ctor_initializer_p = cp_parser_function_transaction (parser,
22927 RID_TRANSACTION_RELAXED);
22928 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22929 ctor_initializer_p = cp_parser_function_try_block (parser);
22930 else
22931 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22932 (parser, /*in_function_try_block=*/false);
22934 finish_lambda_scope ();
22936 /* Finish the function. */
22937 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22938 (inline_p ? 2 : 0));
22939 /* Generate code for it, if necessary. */
22940 expand_or_defer_fn (fn);
22941 /* Restore the saved values. */
22942 parser->in_unbraced_linkage_specification_p
22943 = saved_in_unbraced_linkage_specification_p;
22944 parser->num_template_parameter_lists
22945 = saved_num_template_parameter_lists;
22946 parser->in_function_body = saved_in_function_body;
22948 parser->fully_implicit_function_template_p
22949 = fully_implicit_function_template_p;
22950 parser->implicit_template_parms
22951 = implicit_template_parms;
22952 parser->implicit_template_scope
22953 = implicit_template_scope;
22955 if (parser->fully_implicit_function_template_p)
22956 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22958 return fn;
22961 /* Parse a template-declaration, assuming that the `export' (and
22962 `extern') keywords, if present, has already been scanned. MEMBER_P
22963 is as for cp_parser_template_declaration. */
22965 static void
22966 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22968 tree decl = NULL_TREE;
22969 vec<deferred_access_check, va_gc> *checks;
22970 tree parameter_list;
22971 bool friend_p = false;
22972 bool need_lang_pop;
22973 cp_token *token;
22975 /* Look for the `template' keyword. */
22976 token = cp_lexer_peek_token (parser->lexer);
22977 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22978 return;
22980 /* And the `<'. */
22981 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22982 return;
22983 if (at_class_scope_p () && current_function_decl)
22985 /* 14.5.2.2 [temp.mem]
22987 A local class shall not have member templates. */
22988 error_at (token->location,
22989 "invalid declaration of member template in local class");
22990 cp_parser_skip_to_end_of_block_or_statement (parser);
22991 return;
22993 /* [temp]
22995 A template ... shall not have C linkage. */
22996 if (current_lang_name == lang_name_c)
22998 error_at (token->location, "template with C linkage");
22999 /* Give it C++ linkage to avoid confusing other parts of the
23000 front end. */
23001 push_lang_context (lang_name_cplusplus);
23002 need_lang_pop = true;
23004 else
23005 need_lang_pop = false;
23007 /* We cannot perform access checks on the template parameter
23008 declarations until we know what is being declared, just as we
23009 cannot check the decl-specifier list. */
23010 push_deferring_access_checks (dk_deferred);
23012 /* If the next token is `>', then we have an invalid
23013 specialization. Rather than complain about an invalid template
23014 parameter, issue an error message here. */
23015 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23017 cp_parser_error (parser, "invalid explicit specialization");
23018 begin_specialization ();
23019 parameter_list = NULL_TREE;
23021 else
23023 /* Parse the template parameters. */
23024 parameter_list = cp_parser_template_parameter_list (parser);
23027 /* Get the deferred access checks from the parameter list. These
23028 will be checked once we know what is being declared, as for a
23029 member template the checks must be performed in the scope of the
23030 class containing the member. */
23031 checks = get_deferred_access_checks ();
23033 /* Look for the `>'. */
23034 cp_parser_skip_to_end_of_template_parameter_list (parser);
23035 /* We just processed one more parameter list. */
23036 ++parser->num_template_parameter_lists;
23037 /* If the next token is `template', there are more template
23038 parameters. */
23039 if (cp_lexer_next_token_is_keyword (parser->lexer,
23040 RID_TEMPLATE))
23041 cp_parser_template_declaration_after_export (parser, member_p);
23042 else if (cxx_dialect >= cxx11
23043 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23044 decl = cp_parser_alias_declaration (parser);
23045 else
23047 /* There are no access checks when parsing a template, as we do not
23048 know if a specialization will be a friend. */
23049 push_deferring_access_checks (dk_no_check);
23050 token = cp_lexer_peek_token (parser->lexer);
23051 decl = cp_parser_single_declaration (parser,
23052 checks,
23053 member_p,
23054 /*explicit_specialization_p=*/false,
23055 &friend_p);
23056 pop_deferring_access_checks ();
23058 /* If this is a member template declaration, let the front
23059 end know. */
23060 if (member_p && !friend_p && decl)
23062 if (TREE_CODE (decl) == TYPE_DECL)
23063 cp_parser_check_access_in_redeclaration (decl, token->location);
23065 decl = finish_member_template_decl (decl);
23067 else if (friend_p && decl
23068 && DECL_DECLARES_TYPE_P (decl))
23069 make_friend_class (current_class_type, TREE_TYPE (decl),
23070 /*complain=*/true);
23072 /* We are done with the current parameter list. */
23073 --parser->num_template_parameter_lists;
23075 pop_deferring_access_checks ();
23077 /* Finish up. */
23078 finish_template_decl (parameter_list);
23080 /* Check the template arguments for a literal operator template. */
23081 if (decl
23082 && DECL_DECLARES_FUNCTION_P (decl)
23083 && UDLIT_OPER_P (DECL_NAME (decl)))
23085 bool ok = true;
23086 if (parameter_list == NULL_TREE)
23087 ok = false;
23088 else
23090 int num_parms = TREE_VEC_LENGTH (parameter_list);
23091 if (num_parms == 1)
23093 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23094 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23095 if (TREE_TYPE (parm) != char_type_node
23096 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23097 ok = false;
23099 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23101 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23102 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23103 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23104 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23105 if (TREE_TYPE (parm) != TREE_TYPE (type)
23106 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23107 ok = false;
23109 else
23110 ok = false;
23112 if (!ok)
23113 error ("literal operator template %qD has invalid parameter list."
23114 " Expected non-type template argument pack <char...>"
23115 " or <typename CharT, CharT...>",
23116 decl);
23118 /* Register member declarations. */
23119 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23120 finish_member_declaration (decl);
23121 /* For the erroneous case of a template with C linkage, we pushed an
23122 implicit C++ linkage scope; exit that scope now. */
23123 if (need_lang_pop)
23124 pop_lang_context ();
23125 /* If DECL is a function template, we must return to parse it later.
23126 (Even though there is no definition, there might be default
23127 arguments that need handling.) */
23128 if (member_p && decl
23129 && DECL_DECLARES_FUNCTION_P (decl))
23130 vec_safe_push (unparsed_funs_with_definitions, decl);
23133 /* Perform the deferred access checks from a template-parameter-list.
23134 CHECKS is a TREE_LIST of access checks, as returned by
23135 get_deferred_access_checks. */
23137 static void
23138 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23140 ++processing_template_parmlist;
23141 perform_access_checks (checks, tf_warning_or_error);
23142 --processing_template_parmlist;
23145 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23146 `function-definition' sequence that follows a template header.
23147 If MEMBER_P is true, this declaration appears in a class scope.
23149 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23150 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23152 static tree
23153 cp_parser_single_declaration (cp_parser* parser,
23154 vec<deferred_access_check, va_gc> *checks,
23155 bool member_p,
23156 bool explicit_specialization_p,
23157 bool* friend_p)
23159 int declares_class_or_enum;
23160 tree decl = NULL_TREE;
23161 cp_decl_specifier_seq decl_specifiers;
23162 bool function_definition_p = false;
23163 cp_token *decl_spec_token_start;
23165 /* This function is only used when processing a template
23166 declaration. */
23167 gcc_assert (innermost_scope_kind () == sk_template_parms
23168 || innermost_scope_kind () == sk_template_spec);
23170 /* Defer access checks until we know what is being declared. */
23171 push_deferring_access_checks (dk_deferred);
23173 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23174 alternative. */
23175 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23176 cp_parser_decl_specifier_seq (parser,
23177 CP_PARSER_FLAGS_OPTIONAL,
23178 &decl_specifiers,
23179 &declares_class_or_enum);
23180 if (friend_p)
23181 *friend_p = cp_parser_friend_p (&decl_specifiers);
23183 /* There are no template typedefs. */
23184 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23186 error_at (decl_spec_token_start->location,
23187 "template declaration of %<typedef%>");
23188 decl = error_mark_node;
23191 /* Gather up the access checks that occurred the
23192 decl-specifier-seq. */
23193 stop_deferring_access_checks ();
23195 /* Check for the declaration of a template class. */
23196 if (declares_class_or_enum)
23198 if (cp_parser_declares_only_class_p (parser))
23200 decl = shadow_tag (&decl_specifiers);
23202 /* In this case:
23204 struct C {
23205 friend template <typename T> struct A<T>::B;
23208 A<T>::B will be represented by a TYPENAME_TYPE, and
23209 therefore not recognized by shadow_tag. */
23210 if (friend_p && *friend_p
23211 && !decl
23212 && decl_specifiers.type
23213 && TYPE_P (decl_specifiers.type))
23214 decl = decl_specifiers.type;
23216 if (decl && decl != error_mark_node)
23217 decl = TYPE_NAME (decl);
23218 else
23219 decl = error_mark_node;
23221 /* Perform access checks for template parameters. */
23222 cp_parser_perform_template_parameter_access_checks (checks);
23226 /* Complain about missing 'typename' or other invalid type names. */
23227 if (!decl_specifiers.any_type_specifiers_p
23228 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23230 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23231 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23232 the rest of this declaration. */
23233 decl = error_mark_node;
23234 goto out;
23237 /* If it's not a template class, try for a template function. If
23238 the next token is a `;', then this declaration does not declare
23239 anything. But, if there were errors in the decl-specifiers, then
23240 the error might well have come from an attempted class-specifier.
23241 In that case, there's no need to warn about a missing declarator. */
23242 if (!decl
23243 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23244 || decl_specifiers.type != error_mark_node))
23246 decl = cp_parser_init_declarator (parser,
23247 &decl_specifiers,
23248 checks,
23249 /*function_definition_allowed_p=*/true,
23250 member_p,
23251 declares_class_or_enum,
23252 &function_definition_p,
23253 NULL);
23255 /* 7.1.1-1 [dcl.stc]
23257 A storage-class-specifier shall not be specified in an explicit
23258 specialization... */
23259 if (decl
23260 && explicit_specialization_p
23261 && decl_specifiers.storage_class != sc_none)
23263 error_at (decl_spec_token_start->location,
23264 "explicit template specialization cannot have a storage class");
23265 decl = error_mark_node;
23268 if (decl && VAR_P (decl))
23269 check_template_variable (decl);
23272 /* Look for a trailing `;' after the declaration. */
23273 if (!function_definition_p
23274 && (decl == error_mark_node
23275 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23276 cp_parser_skip_to_end_of_block_or_statement (parser);
23278 out:
23279 pop_deferring_access_checks ();
23281 /* Clear any current qualification; whatever comes next is the start
23282 of something new. */
23283 parser->scope = NULL_TREE;
23284 parser->qualifying_scope = NULL_TREE;
23285 parser->object_scope = NULL_TREE;
23287 return decl;
23290 /* Parse a cast-expression that is not the operand of a unary "&". */
23292 static tree
23293 cp_parser_simple_cast_expression (cp_parser *parser)
23295 return cp_parser_cast_expression (parser, /*address_p=*/false,
23296 /*cast_p=*/false, /*decltype*/false, NULL);
23299 /* Parse a functional cast to TYPE. Returns an expression
23300 representing the cast. */
23302 static tree
23303 cp_parser_functional_cast (cp_parser* parser, tree type)
23305 vec<tree, va_gc> *vec;
23306 tree expression_list;
23307 tree cast;
23308 bool nonconst_p;
23310 if (!type)
23311 type = error_mark_node;
23313 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23315 cp_lexer_set_source_position (parser->lexer);
23316 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23317 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23318 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23319 if (TREE_CODE (type) == TYPE_DECL)
23320 type = TREE_TYPE (type);
23321 return finish_compound_literal (type, expression_list,
23322 tf_warning_or_error);
23326 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23327 /*cast_p=*/true,
23328 /*allow_expansion_p=*/true,
23329 /*non_constant_p=*/NULL);
23330 if (vec == NULL)
23331 expression_list = error_mark_node;
23332 else
23334 expression_list = build_tree_list_vec (vec);
23335 release_tree_vector (vec);
23338 cast = build_functional_cast (type, expression_list,
23339 tf_warning_or_error);
23340 /* [expr.const]/1: In an integral constant expression "only type
23341 conversions to integral or enumeration type can be used". */
23342 if (TREE_CODE (type) == TYPE_DECL)
23343 type = TREE_TYPE (type);
23344 if (cast != error_mark_node
23345 && !cast_valid_in_integral_constant_expression_p (type)
23346 && cp_parser_non_integral_constant_expression (parser,
23347 NIC_CONSTRUCTOR))
23348 return error_mark_node;
23349 return cast;
23352 /* Save the tokens that make up the body of a member function defined
23353 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23354 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23355 specifiers applied to the declaration. Returns the FUNCTION_DECL
23356 for the member function. */
23358 static tree
23359 cp_parser_save_member_function_body (cp_parser* parser,
23360 cp_decl_specifier_seq *decl_specifiers,
23361 cp_declarator *declarator,
23362 tree attributes)
23364 cp_token *first;
23365 cp_token *last;
23366 tree fn;
23368 /* Create the FUNCTION_DECL. */
23369 fn = grokmethod (decl_specifiers, declarator, attributes);
23370 cp_finalize_omp_declare_simd (parser, fn);
23371 /* If something went badly wrong, bail out now. */
23372 if (fn == error_mark_node)
23374 /* If there's a function-body, skip it. */
23375 if (cp_parser_token_starts_function_definition_p
23376 (cp_lexer_peek_token (parser->lexer)))
23377 cp_parser_skip_to_end_of_block_or_statement (parser);
23378 return error_mark_node;
23381 /* Remember it, if there default args to post process. */
23382 cp_parser_save_default_args (parser, fn);
23384 /* Save away the tokens that make up the body of the
23385 function. */
23386 first = parser->lexer->next_token;
23387 /* Handle function try blocks. */
23388 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23389 cp_lexer_consume_token (parser->lexer);
23390 /* We can have braced-init-list mem-initializers before the fn body. */
23391 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23393 cp_lexer_consume_token (parser->lexer);
23394 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23396 /* cache_group will stop after an un-nested { } pair, too. */
23397 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23398 break;
23400 /* variadic mem-inits have ... after the ')'. */
23401 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23402 cp_lexer_consume_token (parser->lexer);
23405 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23406 /* Handle function try blocks. */
23407 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23408 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23409 last = parser->lexer->next_token;
23411 /* Save away the inline definition; we will process it when the
23412 class is complete. */
23413 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23414 DECL_PENDING_INLINE_P (fn) = 1;
23416 /* We need to know that this was defined in the class, so that
23417 friend templates are handled correctly. */
23418 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23420 /* Add FN to the queue of functions to be parsed later. */
23421 vec_safe_push (unparsed_funs_with_definitions, fn);
23423 return fn;
23426 /* Save the tokens that make up the in-class initializer for a non-static
23427 data member. Returns a DEFAULT_ARG. */
23429 static tree
23430 cp_parser_save_nsdmi (cp_parser* parser)
23432 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23435 /* Parse a template-argument-list, as well as the trailing ">" (but
23436 not the opening "<"). See cp_parser_template_argument_list for the
23437 return value. */
23439 static tree
23440 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23442 tree arguments;
23443 tree saved_scope;
23444 tree saved_qualifying_scope;
23445 tree saved_object_scope;
23446 bool saved_greater_than_is_operator_p;
23447 int saved_unevaluated_operand;
23448 int saved_inhibit_evaluation_warnings;
23450 /* [temp.names]
23452 When parsing a template-id, the first non-nested `>' is taken as
23453 the end of the template-argument-list rather than a greater-than
23454 operator. */
23455 saved_greater_than_is_operator_p
23456 = parser->greater_than_is_operator_p;
23457 parser->greater_than_is_operator_p = false;
23458 /* Parsing the argument list may modify SCOPE, so we save it
23459 here. */
23460 saved_scope = parser->scope;
23461 saved_qualifying_scope = parser->qualifying_scope;
23462 saved_object_scope = parser->object_scope;
23463 /* We need to evaluate the template arguments, even though this
23464 template-id may be nested within a "sizeof". */
23465 saved_unevaluated_operand = cp_unevaluated_operand;
23466 cp_unevaluated_operand = 0;
23467 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23468 c_inhibit_evaluation_warnings = 0;
23469 /* Parse the template-argument-list itself. */
23470 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23471 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23472 arguments = NULL_TREE;
23473 else
23474 arguments = cp_parser_template_argument_list (parser);
23475 /* Look for the `>' that ends the template-argument-list. If we find
23476 a '>>' instead, it's probably just a typo. */
23477 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23479 if (cxx_dialect != cxx98)
23481 /* In C++0x, a `>>' in a template argument list or cast
23482 expression is considered to be two separate `>'
23483 tokens. So, change the current token to a `>', but don't
23484 consume it: it will be consumed later when the outer
23485 template argument list (or cast expression) is parsed.
23486 Note that this replacement of `>' for `>>' is necessary
23487 even if we are parsing tentatively: in the tentative
23488 case, after calling
23489 cp_parser_enclosed_template_argument_list we will always
23490 throw away all of the template arguments and the first
23491 closing `>', either because the template argument list
23492 was erroneous or because we are replacing those tokens
23493 with a CPP_TEMPLATE_ID token. The second `>' (which will
23494 not have been thrown away) is needed either to close an
23495 outer template argument list or to complete a new-style
23496 cast. */
23497 cp_token *token = cp_lexer_peek_token (parser->lexer);
23498 token->type = CPP_GREATER;
23500 else if (!saved_greater_than_is_operator_p)
23502 /* If we're in a nested template argument list, the '>>' has
23503 to be a typo for '> >'. We emit the error message, but we
23504 continue parsing and we push a '>' as next token, so that
23505 the argument list will be parsed correctly. Note that the
23506 global source location is still on the token before the
23507 '>>', so we need to say explicitly where we want it. */
23508 cp_token *token = cp_lexer_peek_token (parser->lexer);
23509 error_at (token->location, "%<>>%> should be %<> >%> "
23510 "within a nested template argument list");
23512 token->type = CPP_GREATER;
23514 else
23516 /* If this is not a nested template argument list, the '>>'
23517 is a typo for '>'. Emit an error message and continue.
23518 Same deal about the token location, but here we can get it
23519 right by consuming the '>>' before issuing the diagnostic. */
23520 cp_token *token = cp_lexer_consume_token (parser->lexer);
23521 error_at (token->location,
23522 "spurious %<>>%>, use %<>%> to terminate "
23523 "a template argument list");
23526 else
23527 cp_parser_skip_to_end_of_template_parameter_list (parser);
23528 /* The `>' token might be a greater-than operator again now. */
23529 parser->greater_than_is_operator_p
23530 = saved_greater_than_is_operator_p;
23531 /* Restore the SAVED_SCOPE. */
23532 parser->scope = saved_scope;
23533 parser->qualifying_scope = saved_qualifying_scope;
23534 parser->object_scope = saved_object_scope;
23535 cp_unevaluated_operand = saved_unevaluated_operand;
23536 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23538 return arguments;
23541 /* MEMBER_FUNCTION is a member function, or a friend. If default
23542 arguments, or the body of the function have not yet been parsed,
23543 parse them now. */
23545 static void
23546 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23548 timevar_push (TV_PARSE_INMETH);
23549 /* If this member is a template, get the underlying
23550 FUNCTION_DECL. */
23551 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23552 member_function = DECL_TEMPLATE_RESULT (member_function);
23554 /* There should not be any class definitions in progress at this
23555 point; the bodies of members are only parsed outside of all class
23556 definitions. */
23557 gcc_assert (parser->num_classes_being_defined == 0);
23558 /* While we're parsing the member functions we might encounter more
23559 classes. We want to handle them right away, but we don't want
23560 them getting mixed up with functions that are currently in the
23561 queue. */
23562 push_unparsed_function_queues (parser);
23564 /* Make sure that any template parameters are in scope. */
23565 maybe_begin_member_template_processing (member_function);
23567 /* If the body of the function has not yet been parsed, parse it
23568 now. */
23569 if (DECL_PENDING_INLINE_P (member_function))
23571 tree function_scope;
23572 cp_token_cache *tokens;
23574 /* The function is no longer pending; we are processing it. */
23575 tokens = DECL_PENDING_INLINE_INFO (member_function);
23576 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23577 DECL_PENDING_INLINE_P (member_function) = 0;
23579 /* If this is a local class, enter the scope of the containing
23580 function. */
23581 function_scope = current_function_decl;
23582 if (function_scope)
23583 push_function_context ();
23585 /* Push the body of the function onto the lexer stack. */
23586 cp_parser_push_lexer_for_tokens (parser, tokens);
23588 /* Let the front end know that we going to be defining this
23589 function. */
23590 start_preparsed_function (member_function, NULL_TREE,
23591 SF_PRE_PARSED | SF_INCLASS_INLINE);
23593 /* Don't do access checking if it is a templated function. */
23594 if (processing_template_decl)
23595 push_deferring_access_checks (dk_no_check);
23597 /* #pragma omp declare reduction needs special parsing. */
23598 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23600 parser->lexer->in_pragma = true;
23601 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23602 finish_function (/*inline*/2);
23603 cp_check_omp_declare_reduction (member_function);
23605 else
23606 /* Now, parse the body of the function. */
23607 cp_parser_function_definition_after_declarator (parser,
23608 /*inline_p=*/true);
23610 if (processing_template_decl)
23611 pop_deferring_access_checks ();
23613 /* Leave the scope of the containing function. */
23614 if (function_scope)
23615 pop_function_context ();
23616 cp_parser_pop_lexer (parser);
23619 /* Remove any template parameters from the symbol table. */
23620 maybe_end_member_template_processing ();
23622 /* Restore the queue. */
23623 pop_unparsed_function_queues (parser);
23624 timevar_pop (TV_PARSE_INMETH);
23627 /* If DECL contains any default args, remember it on the unparsed
23628 functions queue. */
23630 static void
23631 cp_parser_save_default_args (cp_parser* parser, tree decl)
23633 tree probe;
23635 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23636 probe;
23637 probe = TREE_CHAIN (probe))
23638 if (TREE_PURPOSE (probe))
23640 cp_default_arg_entry entry = {current_class_type, decl};
23641 vec_safe_push (unparsed_funs_with_default_args, entry);
23642 break;
23646 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23647 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23648 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23649 from the parameter-type-list. */
23651 static tree
23652 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23653 tree default_arg, tree parmtype)
23655 cp_token_cache *tokens;
23656 tree parsed_arg;
23657 bool dummy;
23659 if (default_arg == error_mark_node)
23660 return error_mark_node;
23662 /* Push the saved tokens for the default argument onto the parser's
23663 lexer stack. */
23664 tokens = DEFARG_TOKENS (default_arg);
23665 cp_parser_push_lexer_for_tokens (parser, tokens);
23667 start_lambda_scope (decl);
23669 /* Parse the default argument. */
23670 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23671 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23672 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23674 finish_lambda_scope ();
23676 if (parsed_arg == error_mark_node)
23677 cp_parser_skip_to_end_of_statement (parser);
23679 if (!processing_template_decl)
23681 /* In a non-template class, check conversions now. In a template,
23682 we'll wait and instantiate these as needed. */
23683 if (TREE_CODE (decl) == PARM_DECL)
23684 parsed_arg = check_default_argument (parmtype, parsed_arg,
23685 tf_warning_or_error);
23686 else
23688 int flags = LOOKUP_IMPLICIT;
23689 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
23690 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
23691 flags = LOOKUP_NORMAL;
23692 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23693 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23694 /* This represents the whole initialization. */
23695 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23699 /* If the token stream has not been completely used up, then
23700 there was extra junk after the end of the default
23701 argument. */
23702 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23704 if (TREE_CODE (decl) == PARM_DECL)
23705 cp_parser_error (parser, "expected %<,%>");
23706 else
23707 cp_parser_error (parser, "expected %<;%>");
23710 /* Revert to the main lexer. */
23711 cp_parser_pop_lexer (parser);
23713 return parsed_arg;
23716 /* FIELD is a non-static data member with an initializer which we saved for
23717 later; parse it now. */
23719 static void
23720 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23722 tree def;
23724 maybe_begin_member_template_processing (field);
23726 push_unparsed_function_queues (parser);
23727 def = cp_parser_late_parse_one_default_arg (parser, field,
23728 DECL_INITIAL (field),
23729 NULL_TREE);
23730 pop_unparsed_function_queues (parser);
23732 maybe_end_member_template_processing ();
23734 DECL_INITIAL (field) = def;
23737 /* FN is a FUNCTION_DECL which may contains a parameter with an
23738 unparsed DEFAULT_ARG. Parse the default args now. This function
23739 assumes that the current scope is the scope in which the default
23740 argument should be processed. */
23742 static void
23743 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23745 bool saved_local_variables_forbidden_p;
23746 tree parm, parmdecl;
23748 /* While we're parsing the default args, we might (due to the
23749 statement expression extension) encounter more classes. We want
23750 to handle them right away, but we don't want them getting mixed
23751 up with default args that are currently in the queue. */
23752 push_unparsed_function_queues (parser);
23754 /* Local variable names (and the `this' keyword) may not appear
23755 in a default argument. */
23756 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23757 parser->local_variables_forbidden_p = true;
23759 push_defarg_context (fn);
23761 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23762 parmdecl = DECL_ARGUMENTS (fn);
23763 parm && parm != void_list_node;
23764 parm = TREE_CHAIN (parm),
23765 parmdecl = DECL_CHAIN (parmdecl))
23767 tree default_arg = TREE_PURPOSE (parm);
23768 tree parsed_arg;
23769 vec<tree, va_gc> *insts;
23770 tree copy;
23771 unsigned ix;
23773 if (!default_arg)
23774 continue;
23776 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23777 /* This can happen for a friend declaration for a function
23778 already declared with default arguments. */
23779 continue;
23781 parsed_arg
23782 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23783 default_arg,
23784 TREE_VALUE (parm));
23785 if (parsed_arg == error_mark_node)
23787 continue;
23790 TREE_PURPOSE (parm) = parsed_arg;
23792 /* Update any instantiations we've already created. */
23793 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23794 vec_safe_iterate (insts, ix, &copy); ix++)
23795 TREE_PURPOSE (copy) = parsed_arg;
23798 pop_defarg_context ();
23800 /* Make sure no default arg is missing. */
23801 check_default_args (fn);
23803 /* Restore the state of local_variables_forbidden_p. */
23804 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23806 /* Restore the queue. */
23807 pop_unparsed_function_queues (parser);
23810 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23812 sizeof ... ( identifier )
23814 where the 'sizeof' token has already been consumed. */
23816 static tree
23817 cp_parser_sizeof_pack (cp_parser *parser)
23819 /* Consume the `...'. */
23820 cp_lexer_consume_token (parser->lexer);
23821 maybe_warn_variadic_templates ();
23823 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23824 if (paren)
23825 cp_lexer_consume_token (parser->lexer);
23826 else
23827 permerror (cp_lexer_peek_token (parser->lexer)->location,
23828 "%<sizeof...%> argument must be surrounded by parentheses");
23830 cp_token *token = cp_lexer_peek_token (parser->lexer);
23831 tree name = cp_parser_identifier (parser);
23832 if (name == error_mark_node)
23833 return error_mark_node;
23834 /* The name is not qualified. */
23835 parser->scope = NULL_TREE;
23836 parser->qualifying_scope = NULL_TREE;
23837 parser->object_scope = NULL_TREE;
23838 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23839 if (expr == error_mark_node)
23840 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23841 token->location);
23842 if (TREE_CODE (expr) == TYPE_DECL)
23843 expr = TREE_TYPE (expr);
23844 else if (TREE_CODE (expr) == CONST_DECL)
23845 expr = DECL_INITIAL (expr);
23846 expr = make_pack_expansion (expr);
23848 if (paren)
23849 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23851 return expr;
23854 /* Parse the operand of `sizeof' (or a similar operator). Returns
23855 either a TYPE or an expression, depending on the form of the
23856 input. The KEYWORD indicates which kind of expression we have
23857 encountered. */
23859 static tree
23860 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23862 tree expr = NULL_TREE;
23863 const char *saved_message;
23864 char *tmp;
23865 bool saved_integral_constant_expression_p;
23866 bool saved_non_integral_constant_expression_p;
23868 /* If it's a `...', then we are computing the length of a parameter
23869 pack. */
23870 if (keyword == RID_SIZEOF
23871 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23872 return cp_parser_sizeof_pack (parser);
23874 /* Types cannot be defined in a `sizeof' expression. Save away the
23875 old message. */
23876 saved_message = parser->type_definition_forbidden_message;
23877 /* And create the new one. */
23878 tmp = concat ("types may not be defined in %<",
23879 IDENTIFIER_POINTER (ridpointers[keyword]),
23880 "%> expressions", NULL);
23881 parser->type_definition_forbidden_message = tmp;
23883 /* The restrictions on constant-expressions do not apply inside
23884 sizeof expressions. */
23885 saved_integral_constant_expression_p
23886 = parser->integral_constant_expression_p;
23887 saved_non_integral_constant_expression_p
23888 = parser->non_integral_constant_expression_p;
23889 parser->integral_constant_expression_p = false;
23891 /* Do not actually evaluate the expression. */
23892 ++cp_unevaluated_operand;
23893 ++c_inhibit_evaluation_warnings;
23894 /* If it's a `(', then we might be looking at the type-id
23895 construction. */
23896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23898 tree type = NULL_TREE;
23899 bool compound_literal_p;
23901 /* We can't be sure yet whether we're looking at a type-id or an
23902 expression. */
23903 cp_parser_parse_tentatively (parser);
23904 /* Consume the `('. */
23905 cp_lexer_consume_token (parser->lexer);
23906 /* Note: as a GNU Extension, compound literals are considered
23907 postfix-expressions as they are in C99, so they are valid
23908 arguments to sizeof. See comment in cp_parser_cast_expression
23909 for details. */
23910 cp_lexer_save_tokens (parser->lexer);
23911 /* Skip tokens until the next token is a closing parenthesis.
23912 If we find the closing `)', and the next token is a `{', then
23913 we are looking at a compound-literal. */
23914 compound_literal_p
23915 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23916 /*consume_paren=*/true)
23917 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23918 /* Roll back the tokens we skipped. */
23919 cp_lexer_rollback_tokens (parser->lexer);
23920 /* If we were looking at a compound-literal, simulate an error
23921 so that the call to cp_parser_parse_definitely below will
23922 fail. */
23923 if (compound_literal_p)
23924 cp_parser_simulate_error (parser);
23925 else
23927 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23928 parser->in_type_id_in_expr_p = true;
23929 /* Look for the type-id. */
23930 type = cp_parser_type_id (parser);
23931 /* Look for the closing `)'. */
23932 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23933 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23936 /* If all went well, then we're done. */
23937 if (cp_parser_parse_definitely (parser))
23939 cp_decl_specifier_seq decl_specs;
23941 /* Build a trivial decl-specifier-seq. */
23942 clear_decl_specs (&decl_specs);
23943 decl_specs.type = type;
23945 /* Call grokdeclarator to figure out what type this is. */
23946 expr = grokdeclarator (NULL,
23947 &decl_specs,
23948 TYPENAME,
23949 /*initialized=*/0,
23950 /*attrlist=*/NULL);
23954 /* If the type-id production did not work out, then we must be
23955 looking at the unary-expression production. */
23956 if (!expr)
23957 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23958 /*cast_p=*/false, NULL);
23960 /* Go back to evaluating expressions. */
23961 --cp_unevaluated_operand;
23962 --c_inhibit_evaluation_warnings;
23964 /* Free the message we created. */
23965 free (tmp);
23966 /* And restore the old one. */
23967 parser->type_definition_forbidden_message = saved_message;
23968 parser->integral_constant_expression_p
23969 = saved_integral_constant_expression_p;
23970 parser->non_integral_constant_expression_p
23971 = saved_non_integral_constant_expression_p;
23973 return expr;
23976 /* If the current declaration has no declarator, return true. */
23978 static bool
23979 cp_parser_declares_only_class_p (cp_parser *parser)
23981 /* If the next token is a `;' or a `,' then there is no
23982 declarator. */
23983 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23984 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23987 /* Update the DECL_SPECS to reflect the storage class indicated by
23988 KEYWORD. */
23990 static void
23991 cp_parser_set_storage_class (cp_parser *parser,
23992 cp_decl_specifier_seq *decl_specs,
23993 enum rid keyword,
23994 cp_token *token)
23996 cp_storage_class storage_class;
23998 if (parser->in_unbraced_linkage_specification_p)
24000 error_at (token->location, "invalid use of %qD in linkage specification",
24001 ridpointers[keyword]);
24002 return;
24004 else if (decl_specs->storage_class != sc_none)
24006 decl_specs->conflicting_specifiers_p = true;
24007 return;
24010 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24011 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24012 && decl_specs->gnu_thread_keyword_p)
24014 pedwarn (decl_specs->locations[ds_thread], 0,
24015 "%<__thread%> before %qD", ridpointers[keyword]);
24018 switch (keyword)
24020 case RID_AUTO:
24021 storage_class = sc_auto;
24022 break;
24023 case RID_REGISTER:
24024 storage_class = sc_register;
24025 break;
24026 case RID_STATIC:
24027 storage_class = sc_static;
24028 break;
24029 case RID_EXTERN:
24030 storage_class = sc_extern;
24031 break;
24032 case RID_MUTABLE:
24033 storage_class = sc_mutable;
24034 break;
24035 default:
24036 gcc_unreachable ();
24038 decl_specs->storage_class = storage_class;
24039 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24041 /* A storage class specifier cannot be applied alongside a typedef
24042 specifier. If there is a typedef specifier present then set
24043 conflicting_specifiers_p which will trigger an error later
24044 on in grokdeclarator. */
24045 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24046 decl_specs->conflicting_specifiers_p = true;
24049 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24050 is true, the type is a class or enum definition. */
24052 static void
24053 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24054 tree type_spec,
24055 cp_token *token,
24056 bool type_definition_p)
24058 decl_specs->any_specifiers_p = true;
24060 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24061 (with, for example, in "typedef int wchar_t;") we remember that
24062 this is what happened. In system headers, we ignore these
24063 declarations so that G++ can work with system headers that are not
24064 C++-safe. */
24065 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24066 && !type_definition_p
24067 && (type_spec == boolean_type_node
24068 || type_spec == char16_type_node
24069 || type_spec == char32_type_node
24070 || type_spec == wchar_type_node)
24071 && (decl_specs->type
24072 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24073 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24074 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24075 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24077 decl_specs->redefined_builtin_type = type_spec;
24078 set_and_check_decl_spec_loc (decl_specs,
24079 ds_redefined_builtin_type_spec,
24080 token);
24081 if (!decl_specs->type)
24083 decl_specs->type = type_spec;
24084 decl_specs->type_definition_p = false;
24085 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24088 else if (decl_specs->type)
24089 decl_specs->multiple_types_p = true;
24090 else
24092 decl_specs->type = type_spec;
24093 decl_specs->type_definition_p = type_definition_p;
24094 decl_specs->redefined_builtin_type = NULL_TREE;
24095 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24099 /* True iff TOKEN is the GNU keyword __thread. */
24101 static bool
24102 token_is__thread (cp_token *token)
24104 gcc_assert (token->keyword == RID_THREAD);
24105 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24108 /* Set the location for a declarator specifier and check if it is
24109 duplicated.
24111 DECL_SPECS is the sequence of declarator specifiers onto which to
24112 set the location.
24114 DS is the single declarator specifier to set which location is to
24115 be set onto the existing sequence of declarators.
24117 LOCATION is the location for the declarator specifier to
24118 consider. */
24120 static void
24121 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24122 cp_decl_spec ds, cp_token *token)
24124 gcc_assert (ds < ds_last);
24126 if (decl_specs == NULL)
24127 return;
24129 source_location location = token->location;
24131 if (decl_specs->locations[ds] == 0)
24133 decl_specs->locations[ds] = location;
24134 if (ds == ds_thread)
24135 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24137 else
24139 if (ds == ds_long)
24141 if (decl_specs->locations[ds_long_long] != 0)
24142 error_at (location,
24143 "%<long long long%> is too long for GCC");
24144 else
24146 decl_specs->locations[ds_long_long] = location;
24147 pedwarn_cxx98 (location,
24148 OPT_Wlong_long,
24149 "ISO C++ 1998 does not support %<long long%>");
24152 else if (ds == ds_thread)
24154 bool gnu = token_is__thread (token);
24155 if (gnu != decl_specs->gnu_thread_keyword_p)
24156 error_at (location,
24157 "both %<__thread%> and %<thread_local%> specified");
24158 else
24159 error_at (location, "duplicate %qD", token->u.value);
24161 else
24163 static const char *const decl_spec_names[] = {
24164 "signed",
24165 "unsigned",
24166 "short",
24167 "long",
24168 "const",
24169 "volatile",
24170 "restrict",
24171 "inline",
24172 "virtual",
24173 "explicit",
24174 "friend",
24175 "typedef",
24176 "using",
24177 "constexpr",
24178 "__complex"
24180 error_at (location,
24181 "duplicate %qs", decl_spec_names[ds]);
24186 /* Return true iff the declarator specifier DS is present in the
24187 sequence of declarator specifiers DECL_SPECS. */
24189 bool
24190 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24191 cp_decl_spec ds)
24193 gcc_assert (ds < ds_last);
24195 if (decl_specs == NULL)
24196 return false;
24198 return decl_specs->locations[ds] != 0;
24201 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24202 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24204 static bool
24205 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24207 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24210 /* Issue an error message indicating that TOKEN_DESC was expected.
24211 If KEYWORD is true, it indicated this function is called by
24212 cp_parser_require_keword and the required token can only be
24213 a indicated keyword. */
24215 static void
24216 cp_parser_required_error (cp_parser *parser,
24217 required_token token_desc,
24218 bool keyword)
24220 switch (token_desc)
24222 case RT_NEW:
24223 cp_parser_error (parser, "expected %<new%>");
24224 return;
24225 case RT_DELETE:
24226 cp_parser_error (parser, "expected %<delete%>");
24227 return;
24228 case RT_RETURN:
24229 cp_parser_error (parser, "expected %<return%>");
24230 return;
24231 case RT_WHILE:
24232 cp_parser_error (parser, "expected %<while%>");
24233 return;
24234 case RT_EXTERN:
24235 cp_parser_error (parser, "expected %<extern%>");
24236 return;
24237 case RT_STATIC_ASSERT:
24238 cp_parser_error (parser, "expected %<static_assert%>");
24239 return;
24240 case RT_DECLTYPE:
24241 cp_parser_error (parser, "expected %<decltype%>");
24242 return;
24243 case RT_OPERATOR:
24244 cp_parser_error (parser, "expected %<operator%>");
24245 return;
24246 case RT_CLASS:
24247 cp_parser_error (parser, "expected %<class%>");
24248 return;
24249 case RT_TEMPLATE:
24250 cp_parser_error (parser, "expected %<template%>");
24251 return;
24252 case RT_NAMESPACE:
24253 cp_parser_error (parser, "expected %<namespace%>");
24254 return;
24255 case RT_USING:
24256 cp_parser_error (parser, "expected %<using%>");
24257 return;
24258 case RT_ASM:
24259 cp_parser_error (parser, "expected %<asm%>");
24260 return;
24261 case RT_TRY:
24262 cp_parser_error (parser, "expected %<try%>");
24263 return;
24264 case RT_CATCH:
24265 cp_parser_error (parser, "expected %<catch%>");
24266 return;
24267 case RT_THROW:
24268 cp_parser_error (parser, "expected %<throw%>");
24269 return;
24270 case RT_LABEL:
24271 cp_parser_error (parser, "expected %<__label__%>");
24272 return;
24273 case RT_AT_TRY:
24274 cp_parser_error (parser, "expected %<@try%>");
24275 return;
24276 case RT_AT_SYNCHRONIZED:
24277 cp_parser_error (parser, "expected %<@synchronized%>");
24278 return;
24279 case RT_AT_THROW:
24280 cp_parser_error (parser, "expected %<@throw%>");
24281 return;
24282 case RT_TRANSACTION_ATOMIC:
24283 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24284 return;
24285 case RT_TRANSACTION_RELAXED:
24286 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24287 return;
24288 default:
24289 break;
24291 if (!keyword)
24293 switch (token_desc)
24295 case RT_SEMICOLON:
24296 cp_parser_error (parser, "expected %<;%>");
24297 return;
24298 case RT_OPEN_PAREN:
24299 cp_parser_error (parser, "expected %<(%>");
24300 return;
24301 case RT_CLOSE_BRACE:
24302 cp_parser_error (parser, "expected %<}%>");
24303 return;
24304 case RT_OPEN_BRACE:
24305 cp_parser_error (parser, "expected %<{%>");
24306 return;
24307 case RT_CLOSE_SQUARE:
24308 cp_parser_error (parser, "expected %<]%>");
24309 return;
24310 case RT_OPEN_SQUARE:
24311 cp_parser_error (parser, "expected %<[%>");
24312 return;
24313 case RT_COMMA:
24314 cp_parser_error (parser, "expected %<,%>");
24315 return;
24316 case RT_SCOPE:
24317 cp_parser_error (parser, "expected %<::%>");
24318 return;
24319 case RT_LESS:
24320 cp_parser_error (parser, "expected %<<%>");
24321 return;
24322 case RT_GREATER:
24323 cp_parser_error (parser, "expected %<>%>");
24324 return;
24325 case RT_EQ:
24326 cp_parser_error (parser, "expected %<=%>");
24327 return;
24328 case RT_ELLIPSIS:
24329 cp_parser_error (parser, "expected %<...%>");
24330 return;
24331 case RT_MULT:
24332 cp_parser_error (parser, "expected %<*%>");
24333 return;
24334 case RT_COMPL:
24335 cp_parser_error (parser, "expected %<~%>");
24336 return;
24337 case RT_COLON:
24338 cp_parser_error (parser, "expected %<:%>");
24339 return;
24340 case RT_COLON_SCOPE:
24341 cp_parser_error (parser, "expected %<:%> or %<::%>");
24342 return;
24343 case RT_CLOSE_PAREN:
24344 cp_parser_error (parser, "expected %<)%>");
24345 return;
24346 case RT_COMMA_CLOSE_PAREN:
24347 cp_parser_error (parser, "expected %<,%> or %<)%>");
24348 return;
24349 case RT_PRAGMA_EOL:
24350 cp_parser_error (parser, "expected end of line");
24351 return;
24352 case RT_NAME:
24353 cp_parser_error (parser, "expected identifier");
24354 return;
24355 case RT_SELECT:
24356 cp_parser_error (parser, "expected selection-statement");
24357 return;
24358 case RT_INTERATION:
24359 cp_parser_error (parser, "expected iteration-statement");
24360 return;
24361 case RT_JUMP:
24362 cp_parser_error (parser, "expected jump-statement");
24363 return;
24364 case RT_CLASS_KEY:
24365 cp_parser_error (parser, "expected class-key");
24366 return;
24367 case RT_CLASS_TYPENAME_TEMPLATE:
24368 cp_parser_error (parser,
24369 "expected %<class%>, %<typename%>, or %<template%>");
24370 return;
24371 default:
24372 gcc_unreachable ();
24375 else
24376 gcc_unreachable ();
24381 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24382 issue an error message indicating that TOKEN_DESC was expected.
24384 Returns the token consumed, if the token had the appropriate type.
24385 Otherwise, returns NULL. */
24387 static cp_token *
24388 cp_parser_require (cp_parser* parser,
24389 enum cpp_ttype type,
24390 required_token token_desc)
24392 if (cp_lexer_next_token_is (parser->lexer, type))
24393 return cp_lexer_consume_token (parser->lexer);
24394 else
24396 /* Output the MESSAGE -- unless we're parsing tentatively. */
24397 if (!cp_parser_simulate_error (parser))
24398 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24399 return NULL;
24403 /* An error message is produced if the next token is not '>'.
24404 All further tokens are skipped until the desired token is
24405 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24407 static void
24408 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24410 /* Current level of '< ... >'. */
24411 unsigned level = 0;
24412 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24413 unsigned nesting_depth = 0;
24415 /* Are we ready, yet? If not, issue error message. */
24416 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24417 return;
24419 /* Skip tokens until the desired token is found. */
24420 while (true)
24422 /* Peek at the next token. */
24423 switch (cp_lexer_peek_token (parser->lexer)->type)
24425 case CPP_LESS:
24426 if (!nesting_depth)
24427 ++level;
24428 break;
24430 case CPP_RSHIFT:
24431 if (cxx_dialect == cxx98)
24432 /* C++0x views the `>>' operator as two `>' tokens, but
24433 C++98 does not. */
24434 break;
24435 else if (!nesting_depth && level-- == 0)
24437 /* We've hit a `>>' where the first `>' closes the
24438 template argument list, and the second `>' is
24439 spurious. Just consume the `>>' and stop; we've
24440 already produced at least one error. */
24441 cp_lexer_consume_token (parser->lexer);
24442 return;
24444 /* Fall through for C++0x, so we handle the second `>' in
24445 the `>>'. */
24447 case CPP_GREATER:
24448 if (!nesting_depth && level-- == 0)
24450 /* We've reached the token we want, consume it and stop. */
24451 cp_lexer_consume_token (parser->lexer);
24452 return;
24454 break;
24456 case CPP_OPEN_PAREN:
24457 case CPP_OPEN_SQUARE:
24458 ++nesting_depth;
24459 break;
24461 case CPP_CLOSE_PAREN:
24462 case CPP_CLOSE_SQUARE:
24463 if (nesting_depth-- == 0)
24464 return;
24465 break;
24467 case CPP_EOF:
24468 case CPP_PRAGMA_EOL:
24469 case CPP_SEMICOLON:
24470 case CPP_OPEN_BRACE:
24471 case CPP_CLOSE_BRACE:
24472 /* The '>' was probably forgotten, don't look further. */
24473 return;
24475 default:
24476 break;
24479 /* Consume this token. */
24480 cp_lexer_consume_token (parser->lexer);
24484 /* If the next token is the indicated keyword, consume it. Otherwise,
24485 issue an error message indicating that TOKEN_DESC was expected.
24487 Returns the token consumed, if the token had the appropriate type.
24488 Otherwise, returns NULL. */
24490 static cp_token *
24491 cp_parser_require_keyword (cp_parser* parser,
24492 enum rid keyword,
24493 required_token token_desc)
24495 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24497 if (token && token->keyword != keyword)
24499 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24500 return NULL;
24503 return token;
24506 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24507 function-definition. */
24509 static bool
24510 cp_parser_token_starts_function_definition_p (cp_token* token)
24512 return (/* An ordinary function-body begins with an `{'. */
24513 token->type == CPP_OPEN_BRACE
24514 /* A ctor-initializer begins with a `:'. */
24515 || token->type == CPP_COLON
24516 /* A function-try-block begins with `try'. */
24517 || token->keyword == RID_TRY
24518 /* A function-transaction-block begins with `__transaction_atomic'
24519 or `__transaction_relaxed'. */
24520 || token->keyword == RID_TRANSACTION_ATOMIC
24521 || token->keyword == RID_TRANSACTION_RELAXED
24522 /* The named return value extension begins with `return'. */
24523 || token->keyword == RID_RETURN);
24526 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24527 definition. */
24529 static bool
24530 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24532 cp_token *token;
24534 token = cp_lexer_peek_token (parser->lexer);
24535 return (token->type == CPP_OPEN_BRACE
24536 || (token->type == CPP_COLON
24537 && !parser->colon_doesnt_start_class_def_p));
24540 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24541 C++0x) ending a template-argument. */
24543 static bool
24544 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24546 cp_token *token;
24548 token = cp_lexer_peek_token (parser->lexer);
24549 return (token->type == CPP_COMMA
24550 || token->type == CPP_GREATER
24551 || token->type == CPP_ELLIPSIS
24552 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24555 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24556 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24558 static bool
24559 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24560 size_t n)
24562 cp_token *token;
24564 token = cp_lexer_peek_nth_token (parser->lexer, n);
24565 if (token->type == CPP_LESS)
24566 return true;
24567 /* Check for the sequence `<::' in the original code. It would be lexed as
24568 `[:', where `[' is a digraph, and there is no whitespace before
24569 `:'. */
24570 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24572 cp_token *token2;
24573 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24574 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24575 return true;
24577 return false;
24580 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24581 or none_type otherwise. */
24583 static enum tag_types
24584 cp_parser_token_is_class_key (cp_token* token)
24586 switch (token->keyword)
24588 case RID_CLASS:
24589 return class_type;
24590 case RID_STRUCT:
24591 return record_type;
24592 case RID_UNION:
24593 return union_type;
24595 default:
24596 return none_type;
24600 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24602 static void
24603 cp_parser_check_class_key (enum tag_types class_key, tree type)
24605 if (type == error_mark_node)
24606 return;
24607 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24609 if (permerror (input_location, "%qs tag used in naming %q#T",
24610 class_key == union_type ? "union"
24611 : class_key == record_type ? "struct" : "class",
24612 type))
24613 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24614 "%q#T was previously declared here", type);
24618 /* Issue an error message if DECL is redeclared with different
24619 access than its original declaration [class.access.spec/3].
24620 This applies to nested classes and nested class templates.
24621 [class.mem/1]. */
24623 static void
24624 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24626 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24627 return;
24629 if ((TREE_PRIVATE (decl)
24630 != (current_access_specifier == access_private_node))
24631 || (TREE_PROTECTED (decl)
24632 != (current_access_specifier == access_protected_node)))
24633 error_at (location, "%qD redeclared with different access", decl);
24636 /* Look for the `template' keyword, as a syntactic disambiguator.
24637 Return TRUE iff it is present, in which case it will be
24638 consumed. */
24640 static bool
24641 cp_parser_optional_template_keyword (cp_parser *parser)
24643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24645 /* In C++98 the `template' keyword can only be used within templates;
24646 outside templates the parser can always figure out what is a
24647 template and what is not. In C++11, per the resolution of DR 468,
24648 `template' is allowed in cases where it is not strictly necessary. */
24649 if (!processing_template_decl
24650 && pedantic && cxx_dialect == cxx98)
24652 cp_token *token = cp_lexer_peek_token (parser->lexer);
24653 pedwarn (token->location, OPT_Wpedantic,
24654 "in C++98 %<template%> (as a disambiguator) is only "
24655 "allowed within templates");
24656 /* If this part of the token stream is rescanned, the same
24657 error message would be generated. So, we purge the token
24658 from the stream. */
24659 cp_lexer_purge_token (parser->lexer);
24660 return false;
24662 else
24664 /* Consume the `template' keyword. */
24665 cp_lexer_consume_token (parser->lexer);
24666 return true;
24669 return false;
24672 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24673 set PARSER->SCOPE, and perform other related actions. */
24675 static void
24676 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24678 int i;
24679 struct tree_check *check_value;
24680 deferred_access_check *chk;
24681 vec<deferred_access_check, va_gc> *checks;
24683 /* Get the stored value. */
24684 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24685 /* Perform any access checks that were deferred. */
24686 checks = check_value->checks;
24687 if (checks)
24689 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24690 perform_or_defer_access_check (chk->binfo,
24691 chk->decl,
24692 chk->diag_decl, tf_warning_or_error);
24694 /* Set the scope from the stored value. */
24695 parser->scope = check_value->value;
24696 parser->qualifying_scope = check_value->qualifying_scope;
24697 parser->object_scope = NULL_TREE;
24700 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24701 encounter the end of a block before what we were looking for. */
24703 static bool
24704 cp_parser_cache_group (cp_parser *parser,
24705 enum cpp_ttype end,
24706 unsigned depth)
24708 while (true)
24710 cp_token *token = cp_lexer_peek_token (parser->lexer);
24712 /* Abort a parenthesized expression if we encounter a semicolon. */
24713 if ((end == CPP_CLOSE_PAREN || depth == 0)
24714 && token->type == CPP_SEMICOLON)
24715 return true;
24716 /* If we've reached the end of the file, stop. */
24717 if (token->type == CPP_EOF
24718 || (end != CPP_PRAGMA_EOL
24719 && token->type == CPP_PRAGMA_EOL))
24720 return true;
24721 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24722 /* We've hit the end of an enclosing block, so there's been some
24723 kind of syntax error. */
24724 return true;
24726 /* Consume the token. */
24727 cp_lexer_consume_token (parser->lexer);
24728 /* See if it starts a new group. */
24729 if (token->type == CPP_OPEN_BRACE)
24731 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24732 /* In theory this should probably check end == '}', but
24733 cp_parser_save_member_function_body needs it to exit
24734 after either '}' or ')' when called with ')'. */
24735 if (depth == 0)
24736 return false;
24738 else if (token->type == CPP_OPEN_PAREN)
24740 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24741 if (depth == 0 && end == CPP_CLOSE_PAREN)
24742 return false;
24744 else if (token->type == CPP_PRAGMA)
24745 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24746 else if (token->type == end)
24747 return false;
24751 /* Like above, for caching a default argument or NSDMI. Both of these are
24752 terminated by a non-nested comma, but it can be unclear whether or not a
24753 comma is nested in a template argument list unless we do more parsing.
24754 In order to handle this ambiguity, when we encounter a ',' after a '<'
24755 we try to parse what follows as a parameter-declaration-list (in the
24756 case of a default argument) or a member-declarator (in the case of an
24757 NSDMI). If that succeeds, then we stop caching. */
24759 static tree
24760 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24762 unsigned depth = 0;
24763 int maybe_template_id = 0;
24764 cp_token *first_token;
24765 cp_token *token;
24766 tree default_argument;
24768 /* Add tokens until we have processed the entire default
24769 argument. We add the range [first_token, token). */
24770 first_token = cp_lexer_peek_token (parser->lexer);
24771 if (first_token->type == CPP_OPEN_BRACE)
24773 /* For list-initialization, this is straightforward. */
24774 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24775 token = cp_lexer_peek_token (parser->lexer);
24777 else while (true)
24779 bool done = false;
24781 /* Peek at the next token. */
24782 token = cp_lexer_peek_token (parser->lexer);
24783 /* What we do depends on what token we have. */
24784 switch (token->type)
24786 /* In valid code, a default argument must be
24787 immediately followed by a `,' `)', or `...'. */
24788 case CPP_COMMA:
24789 if (depth == 0 && maybe_template_id)
24791 /* If we've seen a '<', we might be in a
24792 template-argument-list. Until Core issue 325 is
24793 resolved, we don't know how this situation ought
24794 to be handled, so try to DTRT. We check whether
24795 what comes after the comma is a valid parameter
24796 declaration list. If it is, then the comma ends
24797 the default argument; otherwise the default
24798 argument continues. */
24799 bool error = false;
24801 /* Set ITALP so cp_parser_parameter_declaration_list
24802 doesn't decide to commit to this parse. */
24803 bool saved_italp = parser->in_template_argument_list_p;
24804 parser->in_template_argument_list_p = true;
24806 cp_parser_parse_tentatively (parser);
24807 cp_lexer_consume_token (parser->lexer);
24809 if (nsdmi)
24811 int ctor_dtor_or_conv_p;
24812 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24813 &ctor_dtor_or_conv_p,
24814 /*parenthesized_p=*/NULL,
24815 /*member_p=*/true);
24817 else
24819 begin_scope (sk_function_parms, NULL_TREE);
24820 cp_parser_parameter_declaration_list (parser, &error);
24821 pop_bindings_and_leave_scope ();
24823 if (!cp_parser_error_occurred (parser) && !error)
24824 done = true;
24825 cp_parser_abort_tentative_parse (parser);
24827 parser->in_template_argument_list_p = saved_italp;
24828 break;
24830 case CPP_CLOSE_PAREN:
24831 case CPP_ELLIPSIS:
24832 /* If we run into a non-nested `;', `}', or `]',
24833 then the code is invalid -- but the default
24834 argument is certainly over. */
24835 case CPP_SEMICOLON:
24836 case CPP_CLOSE_BRACE:
24837 case CPP_CLOSE_SQUARE:
24838 if (depth == 0
24839 /* Handle correctly int n = sizeof ... ( p ); */
24840 && token->type != CPP_ELLIPSIS)
24841 done = true;
24842 /* Update DEPTH, if necessary. */
24843 else if (token->type == CPP_CLOSE_PAREN
24844 || token->type == CPP_CLOSE_BRACE
24845 || token->type == CPP_CLOSE_SQUARE)
24846 --depth;
24847 break;
24849 case CPP_OPEN_PAREN:
24850 case CPP_OPEN_SQUARE:
24851 case CPP_OPEN_BRACE:
24852 ++depth;
24853 break;
24855 case CPP_LESS:
24856 if (depth == 0)
24857 /* This might be the comparison operator, or it might
24858 start a template argument list. */
24859 ++maybe_template_id;
24860 break;
24862 case CPP_RSHIFT:
24863 if (cxx_dialect == cxx98)
24864 break;
24865 /* Fall through for C++0x, which treats the `>>'
24866 operator like two `>' tokens in certain
24867 cases. */
24869 case CPP_GREATER:
24870 if (depth == 0)
24872 /* This might be an operator, or it might close a
24873 template argument list. But if a previous '<'
24874 started a template argument list, this will have
24875 closed it, so we can't be in one anymore. */
24876 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24877 if (maybe_template_id < 0)
24878 maybe_template_id = 0;
24880 break;
24882 /* If we run out of tokens, issue an error message. */
24883 case CPP_EOF:
24884 case CPP_PRAGMA_EOL:
24885 error_at (token->location, "file ends in default argument");
24886 done = true;
24887 break;
24889 case CPP_NAME:
24890 case CPP_SCOPE:
24891 /* In these cases, we should look for template-ids.
24892 For example, if the default argument is
24893 `X<int, double>()', we need to do name lookup to
24894 figure out whether or not `X' is a template; if
24895 so, the `,' does not end the default argument.
24897 That is not yet done. */
24898 break;
24900 default:
24901 break;
24904 /* If we've reached the end, stop. */
24905 if (done)
24906 break;
24908 /* Add the token to the token block. */
24909 token = cp_lexer_consume_token (parser->lexer);
24912 /* Create a DEFAULT_ARG to represent the unparsed default
24913 argument. */
24914 default_argument = make_node (DEFAULT_ARG);
24915 DEFARG_TOKENS (default_argument)
24916 = cp_token_cache_new (first_token, token);
24917 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24919 return default_argument;
24922 /* Begin parsing tentatively. We always save tokens while parsing
24923 tentatively so that if the tentative parsing fails we can restore the
24924 tokens. */
24926 static void
24927 cp_parser_parse_tentatively (cp_parser* parser)
24929 /* Enter a new parsing context. */
24930 parser->context = cp_parser_context_new (parser->context);
24931 /* Begin saving tokens. */
24932 cp_lexer_save_tokens (parser->lexer);
24933 /* In order to avoid repetitive access control error messages,
24934 access checks are queued up until we are no longer parsing
24935 tentatively. */
24936 push_deferring_access_checks (dk_deferred);
24939 /* Commit to the currently active tentative parse. */
24941 static void
24942 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24944 cp_parser_context *context;
24945 cp_lexer *lexer;
24947 /* Mark all of the levels as committed. */
24948 lexer = parser->lexer;
24949 for (context = parser->context; context->next; context = context->next)
24951 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24952 break;
24953 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24954 while (!cp_lexer_saving_tokens (lexer))
24955 lexer = lexer->next;
24956 cp_lexer_commit_tokens (lexer);
24960 /* Commit to the topmost currently active tentative parse.
24962 Note that this function shouldn't be called when there are
24963 irreversible side-effects while in a tentative state. For
24964 example, we shouldn't create a permanent entry in the symbol
24965 table, or issue an error message that might not apply if the
24966 tentative parse is aborted. */
24968 static void
24969 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24971 cp_parser_context *context = parser->context;
24972 cp_lexer *lexer = parser->lexer;
24974 if (context)
24976 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24977 return;
24978 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24980 while (!cp_lexer_saving_tokens (lexer))
24981 lexer = lexer->next;
24982 cp_lexer_commit_tokens (lexer);
24986 /* Abort the currently active tentative parse. All consumed tokens
24987 will be rolled back, and no diagnostics will be issued. */
24989 static void
24990 cp_parser_abort_tentative_parse (cp_parser* parser)
24992 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24993 || errorcount > 0);
24994 cp_parser_simulate_error (parser);
24995 /* Now, pretend that we want to see if the construct was
24996 successfully parsed. */
24997 cp_parser_parse_definitely (parser);
25000 /* Stop parsing tentatively. If a parse error has occurred, restore the
25001 token stream. Otherwise, commit to the tokens we have consumed.
25002 Returns true if no error occurred; false otherwise. */
25004 static bool
25005 cp_parser_parse_definitely (cp_parser* parser)
25007 bool error_occurred;
25008 cp_parser_context *context;
25010 /* Remember whether or not an error occurred, since we are about to
25011 destroy that information. */
25012 error_occurred = cp_parser_error_occurred (parser);
25013 /* Remove the topmost context from the stack. */
25014 context = parser->context;
25015 parser->context = context->next;
25016 /* If no parse errors occurred, commit to the tentative parse. */
25017 if (!error_occurred)
25019 /* Commit to the tokens read tentatively, unless that was
25020 already done. */
25021 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25022 cp_lexer_commit_tokens (parser->lexer);
25024 pop_to_parent_deferring_access_checks ();
25026 /* Otherwise, if errors occurred, roll back our state so that things
25027 are just as they were before we began the tentative parse. */
25028 else
25030 cp_lexer_rollback_tokens (parser->lexer);
25031 pop_deferring_access_checks ();
25033 /* Add the context to the front of the free list. */
25034 context->next = cp_parser_context_free_list;
25035 cp_parser_context_free_list = context;
25037 return !error_occurred;
25040 /* Returns true if we are parsing tentatively and are not committed to
25041 this tentative parse. */
25043 static bool
25044 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25046 return (cp_parser_parsing_tentatively (parser)
25047 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25050 /* Returns nonzero iff an error has occurred during the most recent
25051 tentative parse. */
25053 static bool
25054 cp_parser_error_occurred (cp_parser* parser)
25056 return (cp_parser_parsing_tentatively (parser)
25057 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25060 /* Returns nonzero if GNU extensions are allowed. */
25062 static bool
25063 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25065 return parser->allow_gnu_extensions_p;
25068 /* Objective-C++ Productions */
25071 /* Parse an Objective-C expression, which feeds into a primary-expression
25072 above.
25074 objc-expression:
25075 objc-message-expression
25076 objc-string-literal
25077 objc-encode-expression
25078 objc-protocol-expression
25079 objc-selector-expression
25081 Returns a tree representation of the expression. */
25083 static tree
25084 cp_parser_objc_expression (cp_parser* parser)
25086 /* Try to figure out what kind of declaration is present. */
25087 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25089 switch (kwd->type)
25091 case CPP_OPEN_SQUARE:
25092 return cp_parser_objc_message_expression (parser);
25094 case CPP_OBJC_STRING:
25095 kwd = cp_lexer_consume_token (parser->lexer);
25096 return objc_build_string_object (kwd->u.value);
25098 case CPP_KEYWORD:
25099 switch (kwd->keyword)
25101 case RID_AT_ENCODE:
25102 return cp_parser_objc_encode_expression (parser);
25104 case RID_AT_PROTOCOL:
25105 return cp_parser_objc_protocol_expression (parser);
25107 case RID_AT_SELECTOR:
25108 return cp_parser_objc_selector_expression (parser);
25110 default:
25111 break;
25113 default:
25114 error_at (kwd->location,
25115 "misplaced %<@%D%> Objective-C++ construct",
25116 kwd->u.value);
25117 cp_parser_skip_to_end_of_block_or_statement (parser);
25120 return error_mark_node;
25123 /* Parse an Objective-C message expression.
25125 objc-message-expression:
25126 [ objc-message-receiver objc-message-args ]
25128 Returns a representation of an Objective-C message. */
25130 static tree
25131 cp_parser_objc_message_expression (cp_parser* parser)
25133 tree receiver, messageargs;
25135 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25136 receiver = cp_parser_objc_message_receiver (parser);
25137 messageargs = cp_parser_objc_message_args (parser);
25138 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25140 return objc_build_message_expr (receiver, messageargs);
25143 /* Parse an objc-message-receiver.
25145 objc-message-receiver:
25146 expression
25147 simple-type-specifier
25149 Returns a representation of the type or expression. */
25151 static tree
25152 cp_parser_objc_message_receiver (cp_parser* parser)
25154 tree rcv;
25156 /* An Objective-C message receiver may be either (1) a type
25157 or (2) an expression. */
25158 cp_parser_parse_tentatively (parser);
25159 rcv = cp_parser_expression (parser, false, NULL);
25161 if (cp_parser_parse_definitely (parser))
25162 return rcv;
25164 rcv = cp_parser_simple_type_specifier (parser,
25165 /*decl_specs=*/NULL,
25166 CP_PARSER_FLAGS_NONE);
25168 return objc_get_class_reference (rcv);
25171 /* Parse the arguments and selectors comprising an Objective-C message.
25173 objc-message-args:
25174 objc-selector
25175 objc-selector-args
25176 objc-selector-args , objc-comma-args
25178 objc-selector-args:
25179 objc-selector [opt] : assignment-expression
25180 objc-selector-args objc-selector [opt] : assignment-expression
25182 objc-comma-args:
25183 assignment-expression
25184 objc-comma-args , assignment-expression
25186 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25187 selector arguments and TREE_VALUE containing a list of comma
25188 arguments. */
25190 static tree
25191 cp_parser_objc_message_args (cp_parser* parser)
25193 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25194 bool maybe_unary_selector_p = true;
25195 cp_token *token = cp_lexer_peek_token (parser->lexer);
25197 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25199 tree selector = NULL_TREE, arg;
25201 if (token->type != CPP_COLON)
25202 selector = cp_parser_objc_selector (parser);
25204 /* Detect if we have a unary selector. */
25205 if (maybe_unary_selector_p
25206 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25207 return build_tree_list (selector, NULL_TREE);
25209 maybe_unary_selector_p = false;
25210 cp_parser_require (parser, CPP_COLON, RT_COLON);
25211 arg = cp_parser_assignment_expression (parser, false, NULL);
25213 sel_args
25214 = chainon (sel_args,
25215 build_tree_list (selector, arg));
25217 token = cp_lexer_peek_token (parser->lexer);
25220 /* Handle non-selector arguments, if any. */
25221 while (token->type == CPP_COMMA)
25223 tree arg;
25225 cp_lexer_consume_token (parser->lexer);
25226 arg = cp_parser_assignment_expression (parser, false, NULL);
25228 addl_args
25229 = chainon (addl_args,
25230 build_tree_list (NULL_TREE, arg));
25232 token = cp_lexer_peek_token (parser->lexer);
25235 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25237 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25238 return build_tree_list (error_mark_node, error_mark_node);
25241 return build_tree_list (sel_args, addl_args);
25244 /* Parse an Objective-C encode expression.
25246 objc-encode-expression:
25247 @encode objc-typename
25249 Returns an encoded representation of the type argument. */
25251 static tree
25252 cp_parser_objc_encode_expression (cp_parser* parser)
25254 tree type;
25255 cp_token *token;
25257 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25258 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25259 token = cp_lexer_peek_token (parser->lexer);
25260 type = complete_type (cp_parser_type_id (parser));
25261 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25263 if (!type)
25265 error_at (token->location,
25266 "%<@encode%> must specify a type as an argument");
25267 return error_mark_node;
25270 /* This happens if we find @encode(T) (where T is a template
25271 typename or something dependent on a template typename) when
25272 parsing a template. In that case, we can't compile it
25273 immediately, but we rather create an AT_ENCODE_EXPR which will
25274 need to be instantiated when the template is used.
25276 if (dependent_type_p (type))
25278 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25279 TREE_READONLY (value) = 1;
25280 return value;
25283 return objc_build_encode_expr (type);
25286 /* Parse an Objective-C @defs expression. */
25288 static tree
25289 cp_parser_objc_defs_expression (cp_parser *parser)
25291 tree name;
25293 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25294 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25295 name = cp_parser_identifier (parser);
25296 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25298 return objc_get_class_ivars (name);
25301 /* Parse an Objective-C protocol expression.
25303 objc-protocol-expression:
25304 @protocol ( identifier )
25306 Returns a representation of the protocol expression. */
25308 static tree
25309 cp_parser_objc_protocol_expression (cp_parser* parser)
25311 tree proto;
25313 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25314 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25315 proto = cp_parser_identifier (parser);
25316 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25318 return objc_build_protocol_expr (proto);
25321 /* Parse an Objective-C selector expression.
25323 objc-selector-expression:
25324 @selector ( objc-method-signature )
25326 objc-method-signature:
25327 objc-selector
25328 objc-selector-seq
25330 objc-selector-seq:
25331 objc-selector :
25332 objc-selector-seq objc-selector :
25334 Returns a representation of the method selector. */
25336 static tree
25337 cp_parser_objc_selector_expression (cp_parser* parser)
25339 tree sel_seq = NULL_TREE;
25340 bool maybe_unary_selector_p = true;
25341 cp_token *token;
25342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25344 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25345 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25346 token = cp_lexer_peek_token (parser->lexer);
25348 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25349 || token->type == CPP_SCOPE)
25351 tree selector = NULL_TREE;
25353 if (token->type != CPP_COLON
25354 || token->type == CPP_SCOPE)
25355 selector = cp_parser_objc_selector (parser);
25357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25358 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25360 /* Detect if we have a unary selector. */
25361 if (maybe_unary_selector_p)
25363 sel_seq = selector;
25364 goto finish_selector;
25366 else
25368 cp_parser_error (parser, "expected %<:%>");
25371 maybe_unary_selector_p = false;
25372 token = cp_lexer_consume_token (parser->lexer);
25374 if (token->type == CPP_SCOPE)
25376 sel_seq
25377 = chainon (sel_seq,
25378 build_tree_list (selector, NULL_TREE));
25379 sel_seq
25380 = chainon (sel_seq,
25381 build_tree_list (NULL_TREE, NULL_TREE));
25383 else
25384 sel_seq
25385 = chainon (sel_seq,
25386 build_tree_list (selector, NULL_TREE));
25388 token = cp_lexer_peek_token (parser->lexer);
25391 finish_selector:
25392 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25394 return objc_build_selector_expr (loc, sel_seq);
25397 /* Parse a list of identifiers.
25399 objc-identifier-list:
25400 identifier
25401 objc-identifier-list , identifier
25403 Returns a TREE_LIST of identifier nodes. */
25405 static tree
25406 cp_parser_objc_identifier_list (cp_parser* parser)
25408 tree identifier;
25409 tree list;
25410 cp_token *sep;
25412 identifier = cp_parser_identifier (parser);
25413 if (identifier == error_mark_node)
25414 return error_mark_node;
25416 list = build_tree_list (NULL_TREE, identifier);
25417 sep = cp_lexer_peek_token (parser->lexer);
25419 while (sep->type == CPP_COMMA)
25421 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25422 identifier = cp_parser_identifier (parser);
25423 if (identifier == error_mark_node)
25424 return list;
25426 list = chainon (list, build_tree_list (NULL_TREE,
25427 identifier));
25428 sep = cp_lexer_peek_token (parser->lexer);
25431 return list;
25434 /* Parse an Objective-C alias declaration.
25436 objc-alias-declaration:
25437 @compatibility_alias identifier identifier ;
25439 This function registers the alias mapping with the Objective-C front end.
25440 It returns nothing. */
25442 static void
25443 cp_parser_objc_alias_declaration (cp_parser* parser)
25445 tree alias, orig;
25447 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25448 alias = cp_parser_identifier (parser);
25449 orig = cp_parser_identifier (parser);
25450 objc_declare_alias (alias, orig);
25451 cp_parser_consume_semicolon_at_end_of_statement (parser);
25454 /* Parse an Objective-C class forward-declaration.
25456 objc-class-declaration:
25457 @class objc-identifier-list ;
25459 The function registers the forward declarations with the Objective-C
25460 front end. It returns nothing. */
25462 static void
25463 cp_parser_objc_class_declaration (cp_parser* parser)
25465 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25466 while (true)
25468 tree id;
25470 id = cp_parser_identifier (parser);
25471 if (id == error_mark_node)
25472 break;
25474 objc_declare_class (id);
25476 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25477 cp_lexer_consume_token (parser->lexer);
25478 else
25479 break;
25481 cp_parser_consume_semicolon_at_end_of_statement (parser);
25484 /* Parse a list of Objective-C protocol references.
25486 objc-protocol-refs-opt:
25487 objc-protocol-refs [opt]
25489 objc-protocol-refs:
25490 < objc-identifier-list >
25492 Returns a TREE_LIST of identifiers, if any. */
25494 static tree
25495 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25497 tree protorefs = NULL_TREE;
25499 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25501 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25502 protorefs = cp_parser_objc_identifier_list (parser);
25503 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25506 return protorefs;
25509 /* Parse a Objective-C visibility specification. */
25511 static void
25512 cp_parser_objc_visibility_spec (cp_parser* parser)
25514 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25516 switch (vis->keyword)
25518 case RID_AT_PRIVATE:
25519 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25520 break;
25521 case RID_AT_PROTECTED:
25522 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25523 break;
25524 case RID_AT_PUBLIC:
25525 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25526 break;
25527 case RID_AT_PACKAGE:
25528 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25529 break;
25530 default:
25531 return;
25534 /* Eat '@private'/'@protected'/'@public'. */
25535 cp_lexer_consume_token (parser->lexer);
25538 /* Parse an Objective-C method type. Return 'true' if it is a class
25539 (+) method, and 'false' if it is an instance (-) method. */
25541 static inline bool
25542 cp_parser_objc_method_type (cp_parser* parser)
25544 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25545 return true;
25546 else
25547 return false;
25550 /* Parse an Objective-C protocol qualifier. */
25552 static tree
25553 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25555 tree quals = NULL_TREE, node;
25556 cp_token *token = cp_lexer_peek_token (parser->lexer);
25558 node = token->u.value;
25560 while (node && identifier_p (node)
25561 && (node == ridpointers [(int) RID_IN]
25562 || node == ridpointers [(int) RID_OUT]
25563 || node == ridpointers [(int) RID_INOUT]
25564 || node == ridpointers [(int) RID_BYCOPY]
25565 || node == ridpointers [(int) RID_BYREF]
25566 || node == ridpointers [(int) RID_ONEWAY]))
25568 quals = tree_cons (NULL_TREE, node, quals);
25569 cp_lexer_consume_token (parser->lexer);
25570 token = cp_lexer_peek_token (parser->lexer);
25571 node = token->u.value;
25574 return quals;
25577 /* Parse an Objective-C typename. */
25579 static tree
25580 cp_parser_objc_typename (cp_parser* parser)
25582 tree type_name = NULL_TREE;
25584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25586 tree proto_quals, cp_type = NULL_TREE;
25588 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25589 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25591 /* An ObjC type name may consist of just protocol qualifiers, in which
25592 case the type shall default to 'id'. */
25593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25595 cp_type = cp_parser_type_id (parser);
25597 /* If the type could not be parsed, an error has already
25598 been produced. For error recovery, behave as if it had
25599 not been specified, which will use the default type
25600 'id'. */
25601 if (cp_type == error_mark_node)
25603 cp_type = NULL_TREE;
25604 /* We need to skip to the closing parenthesis as
25605 cp_parser_type_id() does not seem to do it for
25606 us. */
25607 cp_parser_skip_to_closing_parenthesis (parser,
25608 /*recovering=*/true,
25609 /*or_comma=*/false,
25610 /*consume_paren=*/false);
25614 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25615 type_name = build_tree_list (proto_quals, cp_type);
25618 return type_name;
25621 /* Check to see if TYPE refers to an Objective-C selector name. */
25623 static bool
25624 cp_parser_objc_selector_p (enum cpp_ttype type)
25626 return (type == CPP_NAME || type == CPP_KEYWORD
25627 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25628 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25629 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25630 || type == CPP_XOR || type == CPP_XOR_EQ);
25633 /* Parse an Objective-C selector. */
25635 static tree
25636 cp_parser_objc_selector (cp_parser* parser)
25638 cp_token *token = cp_lexer_consume_token (parser->lexer);
25640 if (!cp_parser_objc_selector_p (token->type))
25642 error_at (token->location, "invalid Objective-C++ selector name");
25643 return error_mark_node;
25646 /* C++ operator names are allowed to appear in ObjC selectors. */
25647 switch (token->type)
25649 case CPP_AND_AND: return get_identifier ("and");
25650 case CPP_AND_EQ: return get_identifier ("and_eq");
25651 case CPP_AND: return get_identifier ("bitand");
25652 case CPP_OR: return get_identifier ("bitor");
25653 case CPP_COMPL: return get_identifier ("compl");
25654 case CPP_NOT: return get_identifier ("not");
25655 case CPP_NOT_EQ: return get_identifier ("not_eq");
25656 case CPP_OR_OR: return get_identifier ("or");
25657 case CPP_OR_EQ: return get_identifier ("or_eq");
25658 case CPP_XOR: return get_identifier ("xor");
25659 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25660 default: return token->u.value;
25664 /* Parse an Objective-C params list. */
25666 static tree
25667 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25669 tree params = NULL_TREE;
25670 bool maybe_unary_selector_p = true;
25671 cp_token *token = cp_lexer_peek_token (parser->lexer);
25673 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25675 tree selector = NULL_TREE, type_name, identifier;
25676 tree parm_attr = NULL_TREE;
25678 if (token->keyword == RID_ATTRIBUTE)
25679 break;
25681 if (token->type != CPP_COLON)
25682 selector = cp_parser_objc_selector (parser);
25684 /* Detect if we have a unary selector. */
25685 if (maybe_unary_selector_p
25686 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25688 params = selector; /* Might be followed by attributes. */
25689 break;
25692 maybe_unary_selector_p = false;
25693 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25695 /* Something went quite wrong. There should be a colon
25696 here, but there is not. Stop parsing parameters. */
25697 break;
25699 type_name = cp_parser_objc_typename (parser);
25700 /* New ObjC allows attributes on parameters too. */
25701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25702 parm_attr = cp_parser_attributes_opt (parser);
25703 identifier = cp_parser_identifier (parser);
25705 params
25706 = chainon (params,
25707 objc_build_keyword_decl (selector,
25708 type_name,
25709 identifier,
25710 parm_attr));
25712 token = cp_lexer_peek_token (parser->lexer);
25715 if (params == NULL_TREE)
25717 cp_parser_error (parser, "objective-c++ method declaration is expected");
25718 return error_mark_node;
25721 /* We allow tail attributes for the method. */
25722 if (token->keyword == RID_ATTRIBUTE)
25724 *attributes = cp_parser_attributes_opt (parser);
25725 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25726 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25727 return params;
25728 cp_parser_error (parser,
25729 "method attributes must be specified at the end");
25730 return error_mark_node;
25733 if (params == NULL_TREE)
25735 cp_parser_error (parser, "objective-c++ method declaration is expected");
25736 return error_mark_node;
25738 return params;
25741 /* Parse the non-keyword Objective-C params. */
25743 static tree
25744 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25745 tree* attributes)
25747 tree params = make_node (TREE_LIST);
25748 cp_token *token = cp_lexer_peek_token (parser->lexer);
25749 *ellipsisp = false; /* Initially, assume no ellipsis. */
25751 while (token->type == CPP_COMMA)
25753 cp_parameter_declarator *parmdecl;
25754 tree parm;
25756 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25757 token = cp_lexer_peek_token (parser->lexer);
25759 if (token->type == CPP_ELLIPSIS)
25761 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25762 *ellipsisp = true;
25763 token = cp_lexer_peek_token (parser->lexer);
25764 break;
25767 /* TODO: parse attributes for tail parameters. */
25768 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25769 parm = grokdeclarator (parmdecl->declarator,
25770 &parmdecl->decl_specifiers,
25771 PARM, /*initialized=*/0,
25772 /*attrlist=*/NULL);
25774 chainon (params, build_tree_list (NULL_TREE, parm));
25775 token = cp_lexer_peek_token (parser->lexer);
25778 /* We allow tail attributes for the method. */
25779 if (token->keyword == RID_ATTRIBUTE)
25781 if (*attributes == NULL_TREE)
25783 *attributes = cp_parser_attributes_opt (parser);
25784 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25785 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25786 return params;
25788 else
25789 /* We have an error, but parse the attributes, so that we can
25790 carry on. */
25791 *attributes = cp_parser_attributes_opt (parser);
25793 cp_parser_error (parser,
25794 "method attributes must be specified at the end");
25795 return error_mark_node;
25798 return params;
25801 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25803 static void
25804 cp_parser_objc_interstitial_code (cp_parser* parser)
25806 cp_token *token = cp_lexer_peek_token (parser->lexer);
25808 /* If the next token is `extern' and the following token is a string
25809 literal, then we have a linkage specification. */
25810 if (token->keyword == RID_EXTERN
25811 && cp_parser_is_pure_string_literal
25812 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25813 cp_parser_linkage_specification (parser);
25814 /* Handle #pragma, if any. */
25815 else if (token->type == CPP_PRAGMA)
25816 cp_parser_pragma (parser, pragma_objc_icode);
25817 /* Allow stray semicolons. */
25818 else if (token->type == CPP_SEMICOLON)
25819 cp_lexer_consume_token (parser->lexer);
25820 /* Mark methods as optional or required, when building protocols. */
25821 else if (token->keyword == RID_AT_OPTIONAL)
25823 cp_lexer_consume_token (parser->lexer);
25824 objc_set_method_opt (true);
25826 else if (token->keyword == RID_AT_REQUIRED)
25828 cp_lexer_consume_token (parser->lexer);
25829 objc_set_method_opt (false);
25831 else if (token->keyword == RID_NAMESPACE)
25832 cp_parser_namespace_definition (parser);
25833 /* Other stray characters must generate errors. */
25834 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25836 cp_lexer_consume_token (parser->lexer);
25837 error ("stray %qs between Objective-C++ methods",
25838 token->type == CPP_OPEN_BRACE ? "{" : "}");
25840 /* Finally, try to parse a block-declaration, or a function-definition. */
25841 else
25842 cp_parser_block_declaration (parser, /*statement_p=*/false);
25845 /* Parse a method signature. */
25847 static tree
25848 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25850 tree rettype, kwdparms, optparms;
25851 bool ellipsis = false;
25852 bool is_class_method;
25854 is_class_method = cp_parser_objc_method_type (parser);
25855 rettype = cp_parser_objc_typename (parser);
25856 *attributes = NULL_TREE;
25857 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25858 if (kwdparms == error_mark_node)
25859 return error_mark_node;
25860 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25861 if (optparms == error_mark_node)
25862 return error_mark_node;
25864 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25867 static bool
25868 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25870 tree tattr;
25871 cp_lexer_save_tokens (parser->lexer);
25872 tattr = cp_parser_attributes_opt (parser);
25873 gcc_assert (tattr) ;
25875 /* If the attributes are followed by a method introducer, this is not allowed.
25876 Dump the attributes and flag the situation. */
25877 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25878 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25879 return true;
25881 /* Otherwise, the attributes introduce some interstitial code, possibly so
25882 rewind to allow that check. */
25883 cp_lexer_rollback_tokens (parser->lexer);
25884 return false;
25887 /* Parse an Objective-C method prototype list. */
25889 static void
25890 cp_parser_objc_method_prototype_list (cp_parser* parser)
25892 cp_token *token = cp_lexer_peek_token (parser->lexer);
25894 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25896 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25898 tree attributes, sig;
25899 bool is_class_method;
25900 if (token->type == CPP_PLUS)
25901 is_class_method = true;
25902 else
25903 is_class_method = false;
25904 sig = cp_parser_objc_method_signature (parser, &attributes);
25905 if (sig == error_mark_node)
25907 cp_parser_skip_to_end_of_block_or_statement (parser);
25908 token = cp_lexer_peek_token (parser->lexer);
25909 continue;
25911 objc_add_method_declaration (is_class_method, sig, attributes);
25912 cp_parser_consume_semicolon_at_end_of_statement (parser);
25914 else if (token->keyword == RID_AT_PROPERTY)
25915 cp_parser_objc_at_property_declaration (parser);
25916 else if (token->keyword == RID_ATTRIBUTE
25917 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25918 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25919 OPT_Wattributes,
25920 "prefix attributes are ignored for methods");
25921 else
25922 /* Allow for interspersed non-ObjC++ code. */
25923 cp_parser_objc_interstitial_code (parser);
25925 token = cp_lexer_peek_token (parser->lexer);
25928 if (token->type != CPP_EOF)
25929 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25930 else
25931 cp_parser_error (parser, "expected %<@end%>");
25933 objc_finish_interface ();
25936 /* Parse an Objective-C method definition list. */
25938 static void
25939 cp_parser_objc_method_definition_list (cp_parser* parser)
25941 cp_token *token = cp_lexer_peek_token (parser->lexer);
25943 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25945 tree meth;
25947 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25949 cp_token *ptk;
25950 tree sig, attribute;
25951 bool is_class_method;
25952 if (token->type == CPP_PLUS)
25953 is_class_method = true;
25954 else
25955 is_class_method = false;
25956 push_deferring_access_checks (dk_deferred);
25957 sig = cp_parser_objc_method_signature (parser, &attribute);
25958 if (sig == error_mark_node)
25960 cp_parser_skip_to_end_of_block_or_statement (parser);
25961 token = cp_lexer_peek_token (parser->lexer);
25962 continue;
25964 objc_start_method_definition (is_class_method, sig, attribute,
25965 NULL_TREE);
25967 /* For historical reasons, we accept an optional semicolon. */
25968 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25969 cp_lexer_consume_token (parser->lexer);
25971 ptk = cp_lexer_peek_token (parser->lexer);
25972 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25973 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25975 perform_deferred_access_checks (tf_warning_or_error);
25976 stop_deferring_access_checks ();
25977 meth = cp_parser_function_definition_after_declarator (parser,
25978 false);
25979 pop_deferring_access_checks ();
25980 objc_finish_method_definition (meth);
25983 /* The following case will be removed once @synthesize is
25984 completely implemented. */
25985 else if (token->keyword == RID_AT_PROPERTY)
25986 cp_parser_objc_at_property_declaration (parser);
25987 else if (token->keyword == RID_AT_SYNTHESIZE)
25988 cp_parser_objc_at_synthesize_declaration (parser);
25989 else if (token->keyword == RID_AT_DYNAMIC)
25990 cp_parser_objc_at_dynamic_declaration (parser);
25991 else if (token->keyword == RID_ATTRIBUTE
25992 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25993 warning_at (token->location, OPT_Wattributes,
25994 "prefix attributes are ignored for methods");
25995 else
25996 /* Allow for interspersed non-ObjC++ code. */
25997 cp_parser_objc_interstitial_code (parser);
25999 token = cp_lexer_peek_token (parser->lexer);
26002 if (token->type != CPP_EOF)
26003 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26004 else
26005 cp_parser_error (parser, "expected %<@end%>");
26007 objc_finish_implementation ();
26010 /* Parse Objective-C ivars. */
26012 static void
26013 cp_parser_objc_class_ivars (cp_parser* parser)
26015 cp_token *token = cp_lexer_peek_token (parser->lexer);
26017 if (token->type != CPP_OPEN_BRACE)
26018 return; /* No ivars specified. */
26020 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26021 token = cp_lexer_peek_token (parser->lexer);
26023 while (token->type != CPP_CLOSE_BRACE
26024 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26026 cp_decl_specifier_seq declspecs;
26027 int decl_class_or_enum_p;
26028 tree prefix_attributes;
26030 cp_parser_objc_visibility_spec (parser);
26032 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26033 break;
26035 cp_parser_decl_specifier_seq (parser,
26036 CP_PARSER_FLAGS_OPTIONAL,
26037 &declspecs,
26038 &decl_class_or_enum_p);
26040 /* auto, register, static, extern, mutable. */
26041 if (declspecs.storage_class != sc_none)
26043 cp_parser_error (parser, "invalid type for instance variable");
26044 declspecs.storage_class = sc_none;
26047 /* thread_local. */
26048 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26050 cp_parser_error (parser, "invalid type for instance variable");
26051 declspecs.locations[ds_thread] = 0;
26054 /* typedef. */
26055 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26057 cp_parser_error (parser, "invalid type for instance variable");
26058 declspecs.locations[ds_typedef] = 0;
26061 prefix_attributes = declspecs.attributes;
26062 declspecs.attributes = NULL_TREE;
26064 /* Keep going until we hit the `;' at the end of the
26065 declaration. */
26066 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26068 tree width = NULL_TREE, attributes, first_attribute, decl;
26069 cp_declarator *declarator = NULL;
26070 int ctor_dtor_or_conv_p;
26072 /* Check for a (possibly unnamed) bitfield declaration. */
26073 token = cp_lexer_peek_token (parser->lexer);
26074 if (token->type == CPP_COLON)
26075 goto eat_colon;
26077 if (token->type == CPP_NAME
26078 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26079 == CPP_COLON))
26081 /* Get the name of the bitfield. */
26082 declarator = make_id_declarator (NULL_TREE,
26083 cp_parser_identifier (parser),
26084 sfk_none);
26086 eat_colon:
26087 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26088 /* Get the width of the bitfield. */
26089 width
26090 = cp_parser_constant_expression (parser,
26091 /*allow_non_constant=*/false,
26092 NULL);
26094 else
26096 /* Parse the declarator. */
26097 declarator
26098 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26099 &ctor_dtor_or_conv_p,
26100 /*parenthesized_p=*/NULL,
26101 /*member_p=*/false);
26104 /* Look for attributes that apply to the ivar. */
26105 attributes = cp_parser_attributes_opt (parser);
26106 /* Remember which attributes are prefix attributes and
26107 which are not. */
26108 first_attribute = attributes;
26109 /* Combine the attributes. */
26110 attributes = chainon (prefix_attributes, attributes);
26112 if (width)
26113 /* Create the bitfield declaration. */
26114 decl = grokbitfield (declarator, &declspecs,
26115 width,
26116 attributes);
26117 else
26118 decl = grokfield (declarator, &declspecs,
26119 NULL_TREE, /*init_const_expr_p=*/false,
26120 NULL_TREE, attributes);
26122 /* Add the instance variable. */
26123 if (decl != error_mark_node && decl != NULL_TREE)
26124 objc_add_instance_variable (decl);
26126 /* Reset PREFIX_ATTRIBUTES. */
26127 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26128 attributes = TREE_CHAIN (attributes);
26129 if (attributes)
26130 TREE_CHAIN (attributes) = NULL_TREE;
26132 token = cp_lexer_peek_token (parser->lexer);
26134 if (token->type == CPP_COMMA)
26136 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26137 continue;
26139 break;
26142 cp_parser_consume_semicolon_at_end_of_statement (parser);
26143 token = cp_lexer_peek_token (parser->lexer);
26146 if (token->keyword == RID_AT_END)
26147 cp_parser_error (parser, "expected %<}%>");
26149 /* Do not consume the RID_AT_END, so it will be read again as terminating
26150 the @interface of @implementation. */
26151 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26152 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26154 /* For historical reasons, we accept an optional semicolon. */
26155 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26156 cp_lexer_consume_token (parser->lexer);
26159 /* Parse an Objective-C protocol declaration. */
26161 static void
26162 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26164 tree proto, protorefs;
26165 cp_token *tok;
26167 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26170 tok = cp_lexer_peek_token (parser->lexer);
26171 error_at (tok->location, "identifier expected after %<@protocol%>");
26172 cp_parser_consume_semicolon_at_end_of_statement (parser);
26173 return;
26176 /* See if we have a forward declaration or a definition. */
26177 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26179 /* Try a forward declaration first. */
26180 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26182 while (true)
26184 tree id;
26186 id = cp_parser_identifier (parser);
26187 if (id == error_mark_node)
26188 break;
26190 objc_declare_protocol (id, attributes);
26192 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26193 cp_lexer_consume_token (parser->lexer);
26194 else
26195 break;
26197 cp_parser_consume_semicolon_at_end_of_statement (parser);
26200 /* Ok, we got a full-fledged definition (or at least should). */
26201 else
26203 proto = cp_parser_identifier (parser);
26204 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26205 objc_start_protocol (proto, protorefs, attributes);
26206 cp_parser_objc_method_prototype_list (parser);
26210 /* Parse an Objective-C superclass or category. */
26212 static void
26213 cp_parser_objc_superclass_or_category (cp_parser *parser,
26214 bool iface_p,
26215 tree *super,
26216 tree *categ, bool *is_class_extension)
26218 cp_token *next = cp_lexer_peek_token (parser->lexer);
26220 *super = *categ = NULL_TREE;
26221 *is_class_extension = false;
26222 if (next->type == CPP_COLON)
26224 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26225 *super = cp_parser_identifier (parser);
26227 else if (next->type == CPP_OPEN_PAREN)
26229 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26231 /* If there is no category name, and this is an @interface, we
26232 have a class extension. */
26233 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26235 *categ = NULL_TREE;
26236 *is_class_extension = true;
26238 else
26239 *categ = cp_parser_identifier (parser);
26241 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26245 /* Parse an Objective-C class interface. */
26247 static void
26248 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26250 tree name, super, categ, protos;
26251 bool is_class_extension;
26253 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26254 name = cp_parser_identifier (parser);
26255 if (name == error_mark_node)
26257 /* It's hard to recover because even if valid @interface stuff
26258 is to follow, we can't compile it (or validate it) if we
26259 don't even know which class it refers to. Let's assume this
26260 was a stray '@interface' token in the stream and skip it.
26262 return;
26264 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26265 &is_class_extension);
26266 protos = cp_parser_objc_protocol_refs_opt (parser);
26268 /* We have either a class or a category on our hands. */
26269 if (categ || is_class_extension)
26270 objc_start_category_interface (name, categ, protos, attributes);
26271 else
26273 objc_start_class_interface (name, super, protos, attributes);
26274 /* Handle instance variable declarations, if any. */
26275 cp_parser_objc_class_ivars (parser);
26276 objc_continue_interface ();
26279 cp_parser_objc_method_prototype_list (parser);
26282 /* Parse an Objective-C class implementation. */
26284 static void
26285 cp_parser_objc_class_implementation (cp_parser* parser)
26287 tree name, super, categ;
26288 bool is_class_extension;
26290 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26291 name = cp_parser_identifier (parser);
26292 if (name == error_mark_node)
26294 /* It's hard to recover because even if valid @implementation
26295 stuff is to follow, we can't compile it (or validate it) if
26296 we don't even know which class it refers to. Let's assume
26297 this was a stray '@implementation' token in the stream and
26298 skip it.
26300 return;
26302 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26303 &is_class_extension);
26305 /* We have either a class or a category on our hands. */
26306 if (categ)
26307 objc_start_category_implementation (name, categ);
26308 else
26310 objc_start_class_implementation (name, super);
26311 /* Handle instance variable declarations, if any. */
26312 cp_parser_objc_class_ivars (parser);
26313 objc_continue_implementation ();
26316 cp_parser_objc_method_definition_list (parser);
26319 /* Consume the @end token and finish off the implementation. */
26321 static void
26322 cp_parser_objc_end_implementation (cp_parser* parser)
26324 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26325 objc_finish_implementation ();
26328 /* Parse an Objective-C declaration. */
26330 static void
26331 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26333 /* Try to figure out what kind of declaration is present. */
26334 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26336 if (attributes)
26337 switch (kwd->keyword)
26339 case RID_AT_ALIAS:
26340 case RID_AT_CLASS:
26341 case RID_AT_END:
26342 error_at (kwd->location, "attributes may not be specified before"
26343 " the %<@%D%> Objective-C++ keyword",
26344 kwd->u.value);
26345 attributes = NULL;
26346 break;
26347 case RID_AT_IMPLEMENTATION:
26348 warning_at (kwd->location, OPT_Wattributes,
26349 "prefix attributes are ignored before %<@%D%>",
26350 kwd->u.value);
26351 attributes = NULL;
26352 default:
26353 break;
26356 switch (kwd->keyword)
26358 case RID_AT_ALIAS:
26359 cp_parser_objc_alias_declaration (parser);
26360 break;
26361 case RID_AT_CLASS:
26362 cp_parser_objc_class_declaration (parser);
26363 break;
26364 case RID_AT_PROTOCOL:
26365 cp_parser_objc_protocol_declaration (parser, attributes);
26366 break;
26367 case RID_AT_INTERFACE:
26368 cp_parser_objc_class_interface (parser, attributes);
26369 break;
26370 case RID_AT_IMPLEMENTATION:
26371 cp_parser_objc_class_implementation (parser);
26372 break;
26373 case RID_AT_END:
26374 cp_parser_objc_end_implementation (parser);
26375 break;
26376 default:
26377 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26378 kwd->u.value);
26379 cp_parser_skip_to_end_of_block_or_statement (parser);
26383 /* Parse an Objective-C try-catch-finally statement.
26385 objc-try-catch-finally-stmt:
26386 @try compound-statement objc-catch-clause-seq [opt]
26387 objc-finally-clause [opt]
26389 objc-catch-clause-seq:
26390 objc-catch-clause objc-catch-clause-seq [opt]
26392 objc-catch-clause:
26393 @catch ( objc-exception-declaration ) compound-statement
26395 objc-finally-clause:
26396 @finally compound-statement
26398 objc-exception-declaration:
26399 parameter-declaration
26400 '...'
26402 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26404 Returns NULL_TREE.
26406 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26407 for C. Keep them in sync. */
26409 static tree
26410 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26412 location_t location;
26413 tree stmt;
26415 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26416 location = cp_lexer_peek_token (parser->lexer)->location;
26417 objc_maybe_warn_exceptions (location);
26418 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26419 node, lest it get absorbed into the surrounding block. */
26420 stmt = push_stmt_list ();
26421 cp_parser_compound_statement (parser, NULL, false, false);
26422 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26424 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26426 cp_parameter_declarator *parm;
26427 tree parameter_declaration = error_mark_node;
26428 bool seen_open_paren = false;
26430 cp_lexer_consume_token (parser->lexer);
26431 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26432 seen_open_paren = true;
26433 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26435 /* We have "@catch (...)" (where the '...' are literally
26436 what is in the code). Skip the '...'.
26437 parameter_declaration is set to NULL_TREE, and
26438 objc_being_catch_clauses() knows that that means
26439 '...'. */
26440 cp_lexer_consume_token (parser->lexer);
26441 parameter_declaration = NULL_TREE;
26443 else
26445 /* We have "@catch (NSException *exception)" or something
26446 like that. Parse the parameter declaration. */
26447 parm = cp_parser_parameter_declaration (parser, false, NULL);
26448 if (parm == NULL)
26449 parameter_declaration = error_mark_node;
26450 else
26451 parameter_declaration = grokdeclarator (parm->declarator,
26452 &parm->decl_specifiers,
26453 PARM, /*initialized=*/0,
26454 /*attrlist=*/NULL);
26456 if (seen_open_paren)
26457 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26458 else
26460 /* If there was no open parenthesis, we are recovering from
26461 an error, and we are trying to figure out what mistake
26462 the user has made. */
26464 /* If there is an immediate closing parenthesis, the user
26465 probably forgot the opening one (ie, they typed "@catch
26466 NSException *e)". Parse the closing parenthesis and keep
26467 going. */
26468 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26469 cp_lexer_consume_token (parser->lexer);
26471 /* If these is no immediate closing parenthesis, the user
26472 probably doesn't know that parenthesis are required at
26473 all (ie, they typed "@catch NSException *e"). So, just
26474 forget about the closing parenthesis and keep going. */
26476 objc_begin_catch_clause (parameter_declaration);
26477 cp_parser_compound_statement (parser, NULL, false, false);
26478 objc_finish_catch_clause ();
26480 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26482 cp_lexer_consume_token (parser->lexer);
26483 location = cp_lexer_peek_token (parser->lexer)->location;
26484 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26485 node, lest it get absorbed into the surrounding block. */
26486 stmt = push_stmt_list ();
26487 cp_parser_compound_statement (parser, NULL, false, false);
26488 objc_build_finally_clause (location, pop_stmt_list (stmt));
26491 return objc_finish_try_stmt ();
26494 /* Parse an Objective-C synchronized statement.
26496 objc-synchronized-stmt:
26497 @synchronized ( expression ) compound-statement
26499 Returns NULL_TREE. */
26501 static tree
26502 cp_parser_objc_synchronized_statement (cp_parser *parser)
26504 location_t location;
26505 tree lock, stmt;
26507 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26509 location = cp_lexer_peek_token (parser->lexer)->location;
26510 objc_maybe_warn_exceptions (location);
26511 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26512 lock = cp_parser_expression (parser, false, NULL);
26513 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26515 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26516 node, lest it get absorbed into the surrounding block. */
26517 stmt = push_stmt_list ();
26518 cp_parser_compound_statement (parser, NULL, false, false);
26520 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26523 /* Parse an Objective-C throw statement.
26525 objc-throw-stmt:
26526 @throw assignment-expression [opt] ;
26528 Returns a constructed '@throw' statement. */
26530 static tree
26531 cp_parser_objc_throw_statement (cp_parser *parser)
26533 tree expr = NULL_TREE;
26534 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26536 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26539 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26541 cp_parser_consume_semicolon_at_end_of_statement (parser);
26543 return objc_build_throw_stmt (loc, expr);
26546 /* Parse an Objective-C statement. */
26548 static tree
26549 cp_parser_objc_statement (cp_parser * parser)
26551 /* Try to figure out what kind of declaration is present. */
26552 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26554 switch (kwd->keyword)
26556 case RID_AT_TRY:
26557 return cp_parser_objc_try_catch_finally_statement (parser);
26558 case RID_AT_SYNCHRONIZED:
26559 return cp_parser_objc_synchronized_statement (parser);
26560 case RID_AT_THROW:
26561 return cp_parser_objc_throw_statement (parser);
26562 default:
26563 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26564 kwd->u.value);
26565 cp_parser_skip_to_end_of_block_or_statement (parser);
26568 return error_mark_node;
26571 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26572 look ahead to see if an objc keyword follows the attributes. This
26573 is to detect the use of prefix attributes on ObjC @interface and
26574 @protocol. */
26576 static bool
26577 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26579 cp_lexer_save_tokens (parser->lexer);
26580 *attrib = cp_parser_attributes_opt (parser);
26581 gcc_assert (*attrib);
26582 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26584 cp_lexer_commit_tokens (parser->lexer);
26585 return true;
26587 cp_lexer_rollback_tokens (parser->lexer);
26588 return false;
26591 /* This routine is a minimal replacement for
26592 c_parser_struct_declaration () used when parsing the list of
26593 types/names or ObjC++ properties. For example, when parsing the
26594 code
26596 @property (readonly) int a, b, c;
26598 this function is responsible for parsing "int a, int b, int c" and
26599 returning the declarations as CHAIN of DECLs.
26601 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26602 similar parsing. */
26603 static tree
26604 cp_parser_objc_struct_declaration (cp_parser *parser)
26606 tree decls = NULL_TREE;
26607 cp_decl_specifier_seq declspecs;
26608 int decl_class_or_enum_p;
26609 tree prefix_attributes;
26611 cp_parser_decl_specifier_seq (parser,
26612 CP_PARSER_FLAGS_NONE,
26613 &declspecs,
26614 &decl_class_or_enum_p);
26616 if (declspecs.type == error_mark_node)
26617 return error_mark_node;
26619 /* auto, register, static, extern, mutable. */
26620 if (declspecs.storage_class != sc_none)
26622 cp_parser_error (parser, "invalid type for property");
26623 declspecs.storage_class = sc_none;
26626 /* thread_local. */
26627 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26629 cp_parser_error (parser, "invalid type for property");
26630 declspecs.locations[ds_thread] = 0;
26633 /* typedef. */
26634 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26636 cp_parser_error (parser, "invalid type for property");
26637 declspecs.locations[ds_typedef] = 0;
26640 prefix_attributes = declspecs.attributes;
26641 declspecs.attributes = NULL_TREE;
26643 /* Keep going until we hit the `;' at the end of the declaration. */
26644 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26646 tree attributes, first_attribute, decl;
26647 cp_declarator *declarator;
26648 cp_token *token;
26650 /* Parse the declarator. */
26651 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26652 NULL, NULL, false);
26654 /* Look for attributes that apply to the ivar. */
26655 attributes = cp_parser_attributes_opt (parser);
26656 /* Remember which attributes are prefix attributes and
26657 which are not. */
26658 first_attribute = attributes;
26659 /* Combine the attributes. */
26660 attributes = chainon (prefix_attributes, attributes);
26662 decl = grokfield (declarator, &declspecs,
26663 NULL_TREE, /*init_const_expr_p=*/false,
26664 NULL_TREE, attributes);
26666 if (decl == error_mark_node || decl == NULL_TREE)
26667 return error_mark_node;
26669 /* Reset PREFIX_ATTRIBUTES. */
26670 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26671 attributes = TREE_CHAIN (attributes);
26672 if (attributes)
26673 TREE_CHAIN (attributes) = NULL_TREE;
26675 DECL_CHAIN (decl) = decls;
26676 decls = decl;
26678 token = cp_lexer_peek_token (parser->lexer);
26679 if (token->type == CPP_COMMA)
26681 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26682 continue;
26684 else
26685 break;
26687 return decls;
26690 /* Parse an Objective-C @property declaration. The syntax is:
26692 objc-property-declaration:
26693 '@property' objc-property-attributes[opt] struct-declaration ;
26695 objc-property-attributes:
26696 '(' objc-property-attribute-list ')'
26698 objc-property-attribute-list:
26699 objc-property-attribute
26700 objc-property-attribute-list, objc-property-attribute
26702 objc-property-attribute
26703 'getter' = identifier
26704 'setter' = identifier
26705 'readonly'
26706 'readwrite'
26707 'assign'
26708 'retain'
26709 'copy'
26710 'nonatomic'
26712 For example:
26713 @property NSString *name;
26714 @property (readonly) id object;
26715 @property (retain, nonatomic, getter=getTheName) id name;
26716 @property int a, b, c;
26718 PS: This function is identical to
26719 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26720 static void
26721 cp_parser_objc_at_property_declaration (cp_parser *parser)
26723 /* The following variables hold the attributes of the properties as
26724 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26725 seen. When we see an attribute, we set them to 'true' (if they
26726 are boolean properties) or to the identifier (if they have an
26727 argument, ie, for getter and setter). Note that here we only
26728 parse the list of attributes, check the syntax and accumulate the
26729 attributes that we find. objc_add_property_declaration() will
26730 then process the information. */
26731 bool property_assign = false;
26732 bool property_copy = false;
26733 tree property_getter_ident = NULL_TREE;
26734 bool property_nonatomic = false;
26735 bool property_readonly = false;
26736 bool property_readwrite = false;
26737 bool property_retain = false;
26738 tree property_setter_ident = NULL_TREE;
26740 /* 'properties' is the list of properties that we read. Usually a
26741 single one, but maybe more (eg, in "@property int a, b, c;" there
26742 are three). */
26743 tree properties;
26744 location_t loc;
26746 loc = cp_lexer_peek_token (parser->lexer)->location;
26748 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26750 /* Parse the optional attribute list... */
26751 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26753 /* Eat the '('. */
26754 cp_lexer_consume_token (parser->lexer);
26756 while (true)
26758 bool syntax_error = false;
26759 cp_token *token = cp_lexer_peek_token (parser->lexer);
26760 enum rid keyword;
26762 if (token->type != CPP_NAME)
26764 cp_parser_error (parser, "expected identifier");
26765 break;
26767 keyword = C_RID_CODE (token->u.value);
26768 cp_lexer_consume_token (parser->lexer);
26769 switch (keyword)
26771 case RID_ASSIGN: property_assign = true; break;
26772 case RID_COPY: property_copy = true; break;
26773 case RID_NONATOMIC: property_nonatomic = true; break;
26774 case RID_READONLY: property_readonly = true; break;
26775 case RID_READWRITE: property_readwrite = true; break;
26776 case RID_RETAIN: property_retain = true; break;
26778 case RID_GETTER:
26779 case RID_SETTER:
26780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26782 if (keyword == RID_GETTER)
26783 cp_parser_error (parser,
26784 "missing %<=%> (after %<getter%> attribute)");
26785 else
26786 cp_parser_error (parser,
26787 "missing %<=%> (after %<setter%> attribute)");
26788 syntax_error = true;
26789 break;
26791 cp_lexer_consume_token (parser->lexer); /* eat the = */
26792 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26794 cp_parser_error (parser, "expected identifier");
26795 syntax_error = true;
26796 break;
26798 if (keyword == RID_SETTER)
26800 if (property_setter_ident != NULL_TREE)
26802 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26803 cp_lexer_consume_token (parser->lexer);
26805 else
26806 property_setter_ident = cp_parser_objc_selector (parser);
26807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26808 cp_parser_error (parser, "setter name must terminate with %<:%>");
26809 else
26810 cp_lexer_consume_token (parser->lexer);
26812 else
26814 if (property_getter_ident != NULL_TREE)
26816 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26817 cp_lexer_consume_token (parser->lexer);
26819 else
26820 property_getter_ident = cp_parser_objc_selector (parser);
26822 break;
26823 default:
26824 cp_parser_error (parser, "unknown property attribute");
26825 syntax_error = true;
26826 break;
26829 if (syntax_error)
26830 break;
26832 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26833 cp_lexer_consume_token (parser->lexer);
26834 else
26835 break;
26838 /* FIXME: "@property (setter, assign);" will generate a spurious
26839 "error: expected ‘)’ before ‘,’ token". This is because
26840 cp_parser_require, unlike the C counterpart, will produce an
26841 error even if we are in error recovery. */
26842 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26844 cp_parser_skip_to_closing_parenthesis (parser,
26845 /*recovering=*/true,
26846 /*or_comma=*/false,
26847 /*consume_paren=*/true);
26851 /* ... and the property declaration(s). */
26852 properties = cp_parser_objc_struct_declaration (parser);
26854 if (properties == error_mark_node)
26856 cp_parser_skip_to_end_of_statement (parser);
26857 /* If the next token is now a `;', consume it. */
26858 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26859 cp_lexer_consume_token (parser->lexer);
26860 return;
26863 if (properties == NULL_TREE)
26864 cp_parser_error (parser, "expected identifier");
26865 else
26867 /* Comma-separated properties are chained together in
26868 reverse order; add them one by one. */
26869 properties = nreverse (properties);
26871 for (; properties; properties = TREE_CHAIN (properties))
26872 objc_add_property_declaration (loc, copy_node (properties),
26873 property_readonly, property_readwrite,
26874 property_assign, property_retain,
26875 property_copy, property_nonatomic,
26876 property_getter_ident, property_setter_ident);
26879 cp_parser_consume_semicolon_at_end_of_statement (parser);
26882 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26884 objc-synthesize-declaration:
26885 @synthesize objc-synthesize-identifier-list ;
26887 objc-synthesize-identifier-list:
26888 objc-synthesize-identifier
26889 objc-synthesize-identifier-list, objc-synthesize-identifier
26891 objc-synthesize-identifier
26892 identifier
26893 identifier = identifier
26895 For example:
26896 @synthesize MyProperty;
26897 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26899 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26900 for C. Keep them in sync.
26902 static void
26903 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26905 tree list = NULL_TREE;
26906 location_t loc;
26907 loc = cp_lexer_peek_token (parser->lexer)->location;
26909 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26910 while (true)
26912 tree property, ivar;
26913 property = cp_parser_identifier (parser);
26914 if (property == error_mark_node)
26916 cp_parser_consume_semicolon_at_end_of_statement (parser);
26917 return;
26919 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26921 cp_lexer_consume_token (parser->lexer);
26922 ivar = cp_parser_identifier (parser);
26923 if (ivar == error_mark_node)
26925 cp_parser_consume_semicolon_at_end_of_statement (parser);
26926 return;
26929 else
26930 ivar = NULL_TREE;
26931 list = chainon (list, build_tree_list (ivar, property));
26932 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26933 cp_lexer_consume_token (parser->lexer);
26934 else
26935 break;
26937 cp_parser_consume_semicolon_at_end_of_statement (parser);
26938 objc_add_synthesize_declaration (loc, list);
26941 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26943 objc-dynamic-declaration:
26944 @dynamic identifier-list ;
26946 For example:
26947 @dynamic MyProperty;
26948 @dynamic MyProperty, AnotherProperty;
26950 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26951 for C. Keep them in sync.
26953 static void
26954 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26956 tree list = NULL_TREE;
26957 location_t loc;
26958 loc = cp_lexer_peek_token (parser->lexer)->location;
26960 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26961 while (true)
26963 tree property;
26964 property = cp_parser_identifier (parser);
26965 if (property == error_mark_node)
26967 cp_parser_consume_semicolon_at_end_of_statement (parser);
26968 return;
26970 list = chainon (list, build_tree_list (NULL, property));
26971 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26972 cp_lexer_consume_token (parser->lexer);
26973 else
26974 break;
26976 cp_parser_consume_semicolon_at_end_of_statement (parser);
26977 objc_add_dynamic_declaration (loc, list);
26981 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26983 /* Returns name of the next clause.
26984 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26985 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26986 returned and the token is consumed. */
26988 static pragma_omp_clause
26989 cp_parser_omp_clause_name (cp_parser *parser)
26991 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26993 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26994 result = PRAGMA_OMP_CLAUSE_IF;
26995 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26996 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26997 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26998 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26999 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27000 result = PRAGMA_OMP_CLAUSE_FOR;
27001 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27003 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27004 const char *p = IDENTIFIER_POINTER (id);
27006 switch (p[0])
27008 case 'a':
27009 if (!strcmp ("aligned", p))
27010 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27011 break;
27012 case 'c':
27013 if (!strcmp ("collapse", p))
27014 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27015 else if (!strcmp ("copyin", p))
27016 result = PRAGMA_OMP_CLAUSE_COPYIN;
27017 else if (!strcmp ("copyprivate", p))
27018 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27019 break;
27020 case 'd':
27021 if (!strcmp ("depend", p))
27022 result = PRAGMA_OMP_CLAUSE_DEPEND;
27023 else if (!strcmp ("device", p))
27024 result = PRAGMA_OMP_CLAUSE_DEVICE;
27025 else if (!strcmp ("dist_schedule", p))
27026 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27027 break;
27028 case 'f':
27029 if (!strcmp ("final", p))
27030 result = PRAGMA_OMP_CLAUSE_FINAL;
27031 else if (!strcmp ("firstprivate", p))
27032 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27033 else if (!strcmp ("from", p))
27034 result = PRAGMA_OMP_CLAUSE_FROM;
27035 break;
27036 case 'i':
27037 if (!strcmp ("inbranch", p))
27038 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27039 break;
27040 case 'l':
27041 if (!strcmp ("lastprivate", p))
27042 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27043 else if (!strcmp ("linear", p))
27044 result = PRAGMA_OMP_CLAUSE_LINEAR;
27045 break;
27046 case 'm':
27047 if (!strcmp ("map", p))
27048 result = PRAGMA_OMP_CLAUSE_MAP;
27049 else if (!strcmp ("mergeable", p))
27050 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27051 else if (flag_cilkplus && !strcmp ("mask", p))
27052 result = PRAGMA_CILK_CLAUSE_MASK;
27053 break;
27054 case 'n':
27055 if (!strcmp ("notinbranch", p))
27056 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27057 else if (!strcmp ("nowait", p))
27058 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27059 else if (flag_cilkplus && !strcmp ("nomask", p))
27060 result = PRAGMA_CILK_CLAUSE_NOMASK;
27061 else if (!strcmp ("num_teams", p))
27062 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27063 else if (!strcmp ("num_threads", p))
27064 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27065 break;
27066 case 'o':
27067 if (!strcmp ("ordered", p))
27068 result = PRAGMA_OMP_CLAUSE_ORDERED;
27069 break;
27070 case 'p':
27071 if (!strcmp ("parallel", p))
27072 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27073 else if (!strcmp ("proc_bind", p))
27074 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27075 break;
27076 case 'r':
27077 if (!strcmp ("reduction", p))
27078 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27079 break;
27080 case 's':
27081 if (!strcmp ("safelen", p))
27082 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27083 else if (!strcmp ("schedule", p))
27084 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27085 else if (!strcmp ("sections", p))
27086 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27087 else if (!strcmp ("shared", p))
27088 result = PRAGMA_OMP_CLAUSE_SHARED;
27089 else if (!strcmp ("simdlen", p))
27090 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27091 break;
27092 case 't':
27093 if (!strcmp ("taskgroup", p))
27094 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27095 else if (!strcmp ("thread_limit", p))
27096 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27097 else if (!strcmp ("to", p))
27098 result = PRAGMA_OMP_CLAUSE_TO;
27099 break;
27100 case 'u':
27101 if (!strcmp ("uniform", p))
27102 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27103 else if (!strcmp ("untied", p))
27104 result = PRAGMA_OMP_CLAUSE_UNTIED;
27105 break;
27106 case 'v':
27107 if (flag_cilkplus && !strcmp ("vectorlength", p))
27108 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27109 break;
27113 if (result != PRAGMA_OMP_CLAUSE_NONE)
27114 cp_lexer_consume_token (parser->lexer);
27116 return result;
27119 /* Validate that a clause of the given type does not already exist. */
27121 static void
27122 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27123 const char *name, location_t location)
27125 tree c;
27127 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27128 if (OMP_CLAUSE_CODE (c) == code)
27130 error_at (location, "too many %qs clauses", name);
27131 break;
27135 /* OpenMP 2.5:
27136 variable-list:
27137 identifier
27138 variable-list , identifier
27140 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27141 colon). An opening parenthesis will have been consumed by the caller.
27143 If KIND is nonzero, create the appropriate node and install the decl
27144 in OMP_CLAUSE_DECL and add the node to the head of the list.
27146 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27147 return the list created.
27149 COLON can be NULL if only closing parenthesis should end the list,
27150 or pointer to bool which will receive false if the list is terminated
27151 by closing parenthesis or true if the list is terminated by colon. */
27153 static tree
27154 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27155 tree list, bool *colon)
27157 cp_token *token;
27158 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27159 if (colon)
27161 parser->colon_corrects_to_scope_p = false;
27162 *colon = false;
27164 while (1)
27166 tree name, decl;
27168 token = cp_lexer_peek_token (parser->lexer);
27169 name = cp_parser_id_expression (parser, /*template_p=*/false,
27170 /*check_dependency_p=*/true,
27171 /*template_p=*/NULL,
27172 /*declarator_p=*/false,
27173 /*optional_p=*/false);
27174 if (name == error_mark_node)
27175 goto skip_comma;
27177 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27178 if (decl == error_mark_node)
27179 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27180 token->location);
27181 else if (kind != 0)
27183 switch (kind)
27185 case OMP_CLAUSE_MAP:
27186 case OMP_CLAUSE_FROM:
27187 case OMP_CLAUSE_TO:
27188 case OMP_CLAUSE_DEPEND:
27189 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27191 tree low_bound = NULL_TREE, length = NULL_TREE;
27193 parser->colon_corrects_to_scope_p = false;
27194 cp_lexer_consume_token (parser->lexer);
27195 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27196 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27197 NULL);
27198 if (!colon)
27199 parser->colon_corrects_to_scope_p
27200 = saved_colon_corrects_to_scope_p;
27201 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27202 length = integer_one_node;
27203 else
27205 /* Look for `:'. */
27206 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27207 goto skip_comma;
27208 if (!cp_lexer_next_token_is (parser->lexer,
27209 CPP_CLOSE_SQUARE))
27210 length = cp_parser_expression (parser,
27211 /*cast_p=*/false,
27212 NULL);
27214 /* Look for the closing `]'. */
27215 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27216 RT_CLOSE_SQUARE))
27217 goto skip_comma;
27218 decl = tree_cons (low_bound, length, decl);
27220 break;
27221 default:
27222 break;
27225 tree u = build_omp_clause (token->location, kind);
27226 OMP_CLAUSE_DECL (u) = decl;
27227 OMP_CLAUSE_CHAIN (u) = list;
27228 list = u;
27230 else
27231 list = tree_cons (decl, NULL_TREE, list);
27233 get_comma:
27234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27235 break;
27236 cp_lexer_consume_token (parser->lexer);
27239 if (colon)
27240 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27242 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27244 *colon = true;
27245 cp_parser_require (parser, CPP_COLON, RT_COLON);
27246 return list;
27249 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27251 int ending;
27253 /* Try to resync to an unnested comma. Copied from
27254 cp_parser_parenthesized_expression_list. */
27255 skip_comma:
27256 if (colon)
27257 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27258 ending = cp_parser_skip_to_closing_parenthesis (parser,
27259 /*recovering=*/true,
27260 /*or_comma=*/true,
27261 /*consume_paren=*/true);
27262 if (ending < 0)
27263 goto get_comma;
27266 return list;
27269 /* Similarly, but expect leading and trailing parenthesis. This is a very
27270 common case for omp clauses. */
27272 static tree
27273 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27275 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27276 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27277 return list;
27280 /* OpenMP 3.0:
27281 collapse ( constant-expression ) */
27283 static tree
27284 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27286 tree c, num;
27287 location_t loc;
27288 HOST_WIDE_INT n;
27290 loc = cp_lexer_peek_token (parser->lexer)->location;
27291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27292 return list;
27294 num = cp_parser_constant_expression (parser, false, NULL);
27296 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27297 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27298 /*or_comma=*/false,
27299 /*consume_paren=*/true);
27301 if (num == error_mark_node)
27302 return list;
27303 num = fold_non_dependent_expr (num);
27304 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27305 || !tree_fits_shwi_p (num)
27306 || (n = tree_to_shwi (num)) <= 0
27307 || (int) n != n)
27309 error_at (loc, "collapse argument needs positive constant integer expression");
27310 return list;
27313 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27314 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27315 OMP_CLAUSE_CHAIN (c) = list;
27316 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27318 return c;
27321 /* OpenMP 2.5:
27322 default ( shared | none ) */
27324 static tree
27325 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27327 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27328 tree c;
27330 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27331 return list;
27332 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27334 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27335 const char *p = IDENTIFIER_POINTER (id);
27337 switch (p[0])
27339 case 'n':
27340 if (strcmp ("none", p) != 0)
27341 goto invalid_kind;
27342 kind = OMP_CLAUSE_DEFAULT_NONE;
27343 break;
27345 case 's':
27346 if (strcmp ("shared", p) != 0)
27347 goto invalid_kind;
27348 kind = OMP_CLAUSE_DEFAULT_SHARED;
27349 break;
27351 default:
27352 goto invalid_kind;
27355 cp_lexer_consume_token (parser->lexer);
27357 else
27359 invalid_kind:
27360 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27363 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27364 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27365 /*or_comma=*/false,
27366 /*consume_paren=*/true);
27368 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27369 return list;
27371 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27372 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27373 OMP_CLAUSE_CHAIN (c) = list;
27374 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27376 return c;
27379 /* OpenMP 3.1:
27380 final ( expression ) */
27382 static tree
27383 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27385 tree t, c;
27387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27388 return list;
27390 t = cp_parser_condition (parser);
27392 if (t == error_mark_node
27393 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27394 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27395 /*or_comma=*/false,
27396 /*consume_paren=*/true);
27398 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27400 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27401 OMP_CLAUSE_FINAL_EXPR (c) = t;
27402 OMP_CLAUSE_CHAIN (c) = list;
27404 return c;
27407 /* OpenMP 2.5:
27408 if ( expression ) */
27410 static tree
27411 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27413 tree t, c;
27415 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27416 return list;
27418 t = cp_parser_condition (parser);
27420 if (t == error_mark_node
27421 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27422 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27423 /*or_comma=*/false,
27424 /*consume_paren=*/true);
27426 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27428 c = build_omp_clause (location, OMP_CLAUSE_IF);
27429 OMP_CLAUSE_IF_EXPR (c) = t;
27430 OMP_CLAUSE_CHAIN (c) = list;
27432 return c;
27435 /* OpenMP 3.1:
27436 mergeable */
27438 static tree
27439 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27440 tree list, location_t location)
27442 tree c;
27444 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27445 location);
27447 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27448 OMP_CLAUSE_CHAIN (c) = list;
27449 return c;
27452 /* OpenMP 2.5:
27453 nowait */
27455 static tree
27456 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27457 tree list, location_t location)
27459 tree c;
27461 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27463 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27464 OMP_CLAUSE_CHAIN (c) = list;
27465 return c;
27468 /* OpenMP 2.5:
27469 num_threads ( expression ) */
27471 static tree
27472 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27473 location_t location)
27475 tree t, c;
27477 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27478 return list;
27480 t = cp_parser_expression (parser, false, NULL);
27482 if (t == error_mark_node
27483 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27484 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27485 /*or_comma=*/false,
27486 /*consume_paren=*/true);
27488 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27489 "num_threads", location);
27491 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27492 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27493 OMP_CLAUSE_CHAIN (c) = list;
27495 return c;
27498 /* OpenMP 2.5:
27499 ordered */
27501 static tree
27502 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27503 tree list, location_t location)
27505 tree c;
27507 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27508 "ordered", location);
27510 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27511 OMP_CLAUSE_CHAIN (c) = list;
27512 return c;
27515 /* OpenMP 2.5:
27516 reduction ( reduction-operator : variable-list )
27518 reduction-operator:
27519 One of: + * - & ^ | && ||
27521 OpenMP 3.1:
27523 reduction-operator:
27524 One of: + * - & ^ | && || min max
27526 OpenMP 4.0:
27528 reduction-operator:
27529 One of: + * - & ^ | && ||
27530 id-expression */
27532 static tree
27533 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27535 enum tree_code code = ERROR_MARK;
27536 tree nlist, c, id = NULL_TREE;
27538 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27539 return list;
27541 switch (cp_lexer_peek_token (parser->lexer)->type)
27543 case CPP_PLUS: code = PLUS_EXPR; break;
27544 case CPP_MULT: code = MULT_EXPR; break;
27545 case CPP_MINUS: code = MINUS_EXPR; break;
27546 case CPP_AND: code = BIT_AND_EXPR; break;
27547 case CPP_XOR: code = BIT_XOR_EXPR; break;
27548 case CPP_OR: code = BIT_IOR_EXPR; break;
27549 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27550 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27551 default: break;
27554 if (code != ERROR_MARK)
27555 cp_lexer_consume_token (parser->lexer);
27556 else
27558 bool saved_colon_corrects_to_scope_p;
27559 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27560 parser->colon_corrects_to_scope_p = false;
27561 id = cp_parser_id_expression (parser, /*template_p=*/false,
27562 /*check_dependency_p=*/true,
27563 /*template_p=*/NULL,
27564 /*declarator_p=*/false,
27565 /*optional_p=*/false);
27566 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27567 if (identifier_p (id))
27569 const char *p = IDENTIFIER_POINTER (id);
27571 if (strcmp (p, "min") == 0)
27572 code = MIN_EXPR;
27573 else if (strcmp (p, "max") == 0)
27574 code = MAX_EXPR;
27575 else if (id == ansi_opname (PLUS_EXPR))
27576 code = PLUS_EXPR;
27577 else if (id == ansi_opname (MULT_EXPR))
27578 code = MULT_EXPR;
27579 else if (id == ansi_opname (MINUS_EXPR))
27580 code = MINUS_EXPR;
27581 else if (id == ansi_opname (BIT_AND_EXPR))
27582 code = BIT_AND_EXPR;
27583 else if (id == ansi_opname (BIT_IOR_EXPR))
27584 code = BIT_IOR_EXPR;
27585 else if (id == ansi_opname (BIT_XOR_EXPR))
27586 code = BIT_XOR_EXPR;
27587 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27588 code = TRUTH_ANDIF_EXPR;
27589 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27590 code = TRUTH_ORIF_EXPR;
27591 id = omp_reduction_id (code, id, NULL_TREE);
27592 tree scope = parser->scope;
27593 if (scope)
27594 id = build_qualified_name (NULL_TREE, scope, id, false);
27595 parser->scope = NULL_TREE;
27596 parser->qualifying_scope = NULL_TREE;
27597 parser->object_scope = NULL_TREE;
27599 else
27601 error ("invalid reduction-identifier");
27602 resync_fail:
27603 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27604 /*or_comma=*/false,
27605 /*consume_paren=*/true);
27606 return list;
27610 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27611 goto resync_fail;
27613 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27614 NULL);
27615 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27617 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27618 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27621 return nlist;
27624 /* OpenMP 2.5:
27625 schedule ( schedule-kind )
27626 schedule ( schedule-kind , expression )
27628 schedule-kind:
27629 static | dynamic | guided | runtime | auto */
27631 static tree
27632 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27634 tree c, t;
27636 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27637 return list;
27639 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27644 const char *p = IDENTIFIER_POINTER (id);
27646 switch (p[0])
27648 case 'd':
27649 if (strcmp ("dynamic", p) != 0)
27650 goto invalid_kind;
27651 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27652 break;
27654 case 'g':
27655 if (strcmp ("guided", p) != 0)
27656 goto invalid_kind;
27657 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27658 break;
27660 case 'r':
27661 if (strcmp ("runtime", p) != 0)
27662 goto invalid_kind;
27663 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27664 break;
27666 default:
27667 goto invalid_kind;
27670 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27671 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27672 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27673 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27674 else
27675 goto invalid_kind;
27676 cp_lexer_consume_token (parser->lexer);
27678 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27680 cp_token *token;
27681 cp_lexer_consume_token (parser->lexer);
27683 token = cp_lexer_peek_token (parser->lexer);
27684 t = cp_parser_assignment_expression (parser, false, NULL);
27686 if (t == error_mark_node)
27687 goto resync_fail;
27688 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27689 error_at (token->location, "schedule %<runtime%> does not take "
27690 "a %<chunk_size%> parameter");
27691 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27692 error_at (token->location, "schedule %<auto%> does not take "
27693 "a %<chunk_size%> parameter");
27694 else
27695 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27697 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27698 goto resync_fail;
27700 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27701 goto resync_fail;
27703 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27704 OMP_CLAUSE_CHAIN (c) = list;
27705 return c;
27707 invalid_kind:
27708 cp_parser_error (parser, "invalid schedule kind");
27709 resync_fail:
27710 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27711 /*or_comma=*/false,
27712 /*consume_paren=*/true);
27713 return list;
27716 /* OpenMP 3.0:
27717 untied */
27719 static tree
27720 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27721 tree list, location_t location)
27723 tree c;
27725 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27727 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27728 OMP_CLAUSE_CHAIN (c) = list;
27729 return c;
27732 /* OpenMP 4.0:
27733 inbranch
27734 notinbranch */
27736 static tree
27737 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27738 tree list, location_t location)
27740 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27741 tree c = build_omp_clause (location, code);
27742 OMP_CLAUSE_CHAIN (c) = list;
27743 return c;
27746 /* OpenMP 4.0:
27747 parallel
27749 sections
27750 taskgroup */
27752 static tree
27753 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27754 enum omp_clause_code code,
27755 tree list, location_t location)
27757 tree c = build_omp_clause (location, code);
27758 OMP_CLAUSE_CHAIN (c) = list;
27759 return c;
27762 /* OpenMP 4.0:
27763 num_teams ( expression ) */
27765 static tree
27766 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27767 location_t location)
27769 tree t, c;
27771 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27772 return list;
27774 t = cp_parser_expression (parser, false, NULL);
27776 if (t == error_mark_node
27777 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27778 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27779 /*or_comma=*/false,
27780 /*consume_paren=*/true);
27782 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27783 "num_teams", location);
27785 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27786 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27787 OMP_CLAUSE_CHAIN (c) = list;
27789 return c;
27792 /* OpenMP 4.0:
27793 thread_limit ( expression ) */
27795 static tree
27796 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27797 location_t location)
27799 tree t, c;
27801 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27802 return list;
27804 t = cp_parser_expression (parser, false, NULL);
27806 if (t == error_mark_node
27807 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27808 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27809 /*or_comma=*/false,
27810 /*consume_paren=*/true);
27812 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27813 "thread_limit", location);
27815 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27816 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27817 OMP_CLAUSE_CHAIN (c) = list;
27819 return c;
27822 /* OpenMP 4.0:
27823 aligned ( variable-list )
27824 aligned ( variable-list : constant-expression ) */
27826 static tree
27827 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27829 tree nlist, c, alignment = NULL_TREE;
27830 bool colon;
27832 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27833 return list;
27835 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27836 &colon);
27838 if (colon)
27840 alignment = cp_parser_constant_expression (parser, false, NULL);
27842 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27843 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27844 /*or_comma=*/false,
27845 /*consume_paren=*/true);
27847 if (alignment == error_mark_node)
27848 alignment = NULL_TREE;
27851 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27852 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27854 return nlist;
27857 /* OpenMP 4.0:
27858 linear ( variable-list )
27859 linear ( variable-list : expression ) */
27861 static tree
27862 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
27863 bool is_cilk_simd_fn)
27865 tree nlist, c, step = integer_one_node;
27866 bool colon;
27868 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27869 return list;
27871 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27872 &colon);
27874 if (colon)
27876 step = cp_parser_expression (parser, false, NULL);
27878 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
27880 sorry ("using parameters for %<linear%> step is not supported yet");
27881 step = integer_one_node;
27883 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27884 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27885 /*or_comma=*/false,
27886 /*consume_paren=*/true);
27888 if (step == error_mark_node)
27889 return list;
27892 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27893 OMP_CLAUSE_LINEAR_STEP (c) = step;
27895 return nlist;
27898 /* OpenMP 4.0:
27899 safelen ( constant-expression ) */
27901 static tree
27902 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27903 location_t location)
27905 tree t, c;
27907 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27908 return list;
27910 t = cp_parser_constant_expression (parser, false, NULL);
27912 if (t == error_mark_node
27913 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27914 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27915 /*or_comma=*/false,
27916 /*consume_paren=*/true);
27918 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27920 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27921 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27922 OMP_CLAUSE_CHAIN (c) = list;
27924 return c;
27927 /* OpenMP 4.0:
27928 simdlen ( constant-expression ) */
27930 static tree
27931 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27932 location_t location)
27934 tree t, c;
27936 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27937 return list;
27939 t = cp_parser_constant_expression (parser, false, NULL);
27941 if (t == error_mark_node
27942 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27943 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27944 /*or_comma=*/false,
27945 /*consume_paren=*/true);
27947 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27949 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27950 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27951 OMP_CLAUSE_CHAIN (c) = list;
27953 return c;
27956 /* OpenMP 4.0:
27957 depend ( depend-kind : variable-list )
27959 depend-kind:
27960 in | out | inout */
27962 static tree
27963 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27965 tree nlist, c;
27966 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27968 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27969 return list;
27971 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27973 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27974 const char *p = IDENTIFIER_POINTER (id);
27976 if (strcmp ("in", p) == 0)
27977 kind = OMP_CLAUSE_DEPEND_IN;
27978 else if (strcmp ("inout", p) == 0)
27979 kind = OMP_CLAUSE_DEPEND_INOUT;
27980 else if (strcmp ("out", p) == 0)
27981 kind = OMP_CLAUSE_DEPEND_OUT;
27982 else
27983 goto invalid_kind;
27985 else
27986 goto invalid_kind;
27988 cp_lexer_consume_token (parser->lexer);
27989 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27990 goto resync_fail;
27992 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27993 NULL);
27995 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27996 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27998 return nlist;
28000 invalid_kind:
28001 cp_parser_error (parser, "invalid depend kind");
28002 resync_fail:
28003 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28004 /*or_comma=*/false,
28005 /*consume_paren=*/true);
28006 return list;
28009 /* OpenMP 4.0:
28010 map ( map-kind : variable-list )
28011 map ( variable-list )
28013 map-kind:
28014 alloc | to | from | tofrom */
28016 static tree
28017 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28019 tree nlist, c;
28020 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28022 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28023 return list;
28025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28026 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28028 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28029 const char *p = IDENTIFIER_POINTER (id);
28031 if (strcmp ("alloc", p) == 0)
28032 kind = OMP_CLAUSE_MAP_ALLOC;
28033 else if (strcmp ("to", p) == 0)
28034 kind = OMP_CLAUSE_MAP_TO;
28035 else if (strcmp ("from", p) == 0)
28036 kind = OMP_CLAUSE_MAP_FROM;
28037 else if (strcmp ("tofrom", p) == 0)
28038 kind = OMP_CLAUSE_MAP_TOFROM;
28039 else
28041 cp_parser_error (parser, "invalid map kind");
28042 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28043 /*or_comma=*/false,
28044 /*consume_paren=*/true);
28045 return list;
28047 cp_lexer_consume_token (parser->lexer);
28048 cp_lexer_consume_token (parser->lexer);
28051 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28052 NULL);
28054 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28055 OMP_CLAUSE_MAP_KIND (c) = kind;
28057 return nlist;
28060 /* OpenMP 4.0:
28061 device ( expression ) */
28063 static tree
28064 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28065 location_t location)
28067 tree t, c;
28069 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28070 return list;
28072 t = cp_parser_expression (parser, false, NULL);
28074 if (t == error_mark_node
28075 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28076 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28077 /*or_comma=*/false,
28078 /*consume_paren=*/true);
28080 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28081 "device", location);
28083 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28084 OMP_CLAUSE_DEVICE_ID (c) = t;
28085 OMP_CLAUSE_CHAIN (c) = list;
28087 return c;
28090 /* OpenMP 4.0:
28091 dist_schedule ( static )
28092 dist_schedule ( static , expression ) */
28094 static tree
28095 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28096 location_t location)
28098 tree c, t;
28100 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28101 return list;
28103 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28105 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28106 goto invalid_kind;
28107 cp_lexer_consume_token (parser->lexer);
28109 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28111 cp_lexer_consume_token (parser->lexer);
28113 t = cp_parser_assignment_expression (parser, false, NULL);
28115 if (t == error_mark_node)
28116 goto resync_fail;
28117 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28119 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28120 goto resync_fail;
28122 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28123 goto resync_fail;
28125 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28126 location);
28127 OMP_CLAUSE_CHAIN (c) = list;
28128 return c;
28130 invalid_kind:
28131 cp_parser_error (parser, "invalid dist_schedule kind");
28132 resync_fail:
28133 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28134 /*or_comma=*/false,
28135 /*consume_paren=*/true);
28136 return list;
28139 /* OpenMP 4.0:
28140 proc_bind ( proc-bind-kind )
28142 proc-bind-kind:
28143 master | close | spread */
28145 static tree
28146 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28147 location_t location)
28149 tree c;
28150 enum omp_clause_proc_bind_kind kind;
28152 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28153 return list;
28155 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28157 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28158 const char *p = IDENTIFIER_POINTER (id);
28160 if (strcmp ("master", p) == 0)
28161 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28162 else if (strcmp ("close", p) == 0)
28163 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28164 else if (strcmp ("spread", p) == 0)
28165 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28166 else
28167 goto invalid_kind;
28169 else
28170 goto invalid_kind;
28172 cp_lexer_consume_token (parser->lexer);
28173 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28174 goto resync_fail;
28176 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28177 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28178 location);
28179 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28180 OMP_CLAUSE_CHAIN (c) = list;
28181 return c;
28183 invalid_kind:
28184 cp_parser_error (parser, "invalid depend kind");
28185 resync_fail:
28186 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28187 /*or_comma=*/false,
28188 /*consume_paren=*/true);
28189 return list;
28192 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28193 is a bitmask in MASK. Return the list of clauses found; the result
28194 of clause default goes in *pdefault. */
28196 static tree
28197 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28198 const char *where, cp_token *pragma_tok,
28199 bool finish_p = true)
28201 tree clauses = NULL;
28202 bool first = true;
28203 cp_token *token = NULL;
28204 bool cilk_simd_fn = false;
28206 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28208 pragma_omp_clause c_kind;
28209 const char *c_name;
28210 tree prev = clauses;
28212 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28213 cp_lexer_consume_token (parser->lexer);
28215 token = cp_lexer_peek_token (parser->lexer);
28216 c_kind = cp_parser_omp_clause_name (parser);
28218 switch (c_kind)
28220 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28221 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28222 token->location);
28223 c_name = "collapse";
28224 break;
28225 case PRAGMA_OMP_CLAUSE_COPYIN:
28226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28227 c_name = "copyin";
28228 break;
28229 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28230 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28231 clauses);
28232 c_name = "copyprivate";
28233 break;
28234 case PRAGMA_OMP_CLAUSE_DEFAULT:
28235 clauses = cp_parser_omp_clause_default (parser, clauses,
28236 token->location);
28237 c_name = "default";
28238 break;
28239 case PRAGMA_OMP_CLAUSE_FINAL:
28240 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28241 c_name = "final";
28242 break;
28243 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28244 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28245 clauses);
28246 c_name = "firstprivate";
28247 break;
28248 case PRAGMA_OMP_CLAUSE_IF:
28249 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28250 c_name = "if";
28251 break;
28252 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28253 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28254 clauses);
28255 c_name = "lastprivate";
28256 break;
28257 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28258 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28259 token->location);
28260 c_name = "mergeable";
28261 break;
28262 case PRAGMA_OMP_CLAUSE_NOWAIT:
28263 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28264 c_name = "nowait";
28265 break;
28266 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28267 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28268 token->location);
28269 c_name = "num_threads";
28270 break;
28271 case PRAGMA_OMP_CLAUSE_ORDERED:
28272 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28273 token->location);
28274 c_name = "ordered";
28275 break;
28276 case PRAGMA_OMP_CLAUSE_PRIVATE:
28277 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28278 clauses);
28279 c_name = "private";
28280 break;
28281 case PRAGMA_OMP_CLAUSE_REDUCTION:
28282 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28283 c_name = "reduction";
28284 break;
28285 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28286 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28287 token->location);
28288 c_name = "schedule";
28289 break;
28290 case PRAGMA_OMP_CLAUSE_SHARED:
28291 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28292 clauses);
28293 c_name = "shared";
28294 break;
28295 case PRAGMA_OMP_CLAUSE_UNTIED:
28296 clauses = cp_parser_omp_clause_untied (parser, clauses,
28297 token->location);
28298 c_name = "untied";
28299 break;
28300 case PRAGMA_OMP_CLAUSE_INBRANCH:
28301 case PRAGMA_CILK_CLAUSE_MASK:
28302 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28303 clauses, token->location);
28304 c_name = "inbranch";
28305 break;
28306 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28307 case PRAGMA_CILK_CLAUSE_NOMASK:
28308 clauses = cp_parser_omp_clause_branch (parser,
28309 OMP_CLAUSE_NOTINBRANCH,
28310 clauses, token->location);
28311 c_name = "notinbranch";
28312 break;
28313 case PRAGMA_OMP_CLAUSE_PARALLEL:
28314 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28315 clauses, token->location);
28316 c_name = "parallel";
28317 if (!first)
28319 clause_not_first:
28320 error_at (token->location, "%qs must be the first clause of %qs",
28321 c_name, where);
28322 clauses = prev;
28324 break;
28325 case PRAGMA_OMP_CLAUSE_FOR:
28326 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28327 clauses, token->location);
28328 c_name = "for";
28329 if (!first)
28330 goto clause_not_first;
28331 break;
28332 case PRAGMA_OMP_CLAUSE_SECTIONS:
28333 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28334 clauses, token->location);
28335 c_name = "sections";
28336 if (!first)
28337 goto clause_not_first;
28338 break;
28339 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28340 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28341 clauses, token->location);
28342 c_name = "taskgroup";
28343 if (!first)
28344 goto clause_not_first;
28345 break;
28346 case PRAGMA_OMP_CLAUSE_TO:
28347 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28348 clauses);
28349 c_name = "to";
28350 break;
28351 case PRAGMA_OMP_CLAUSE_FROM:
28352 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28353 clauses);
28354 c_name = "from";
28355 break;
28356 case PRAGMA_OMP_CLAUSE_UNIFORM:
28357 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28358 clauses);
28359 c_name = "uniform";
28360 break;
28361 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28362 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28363 token->location);
28364 c_name = "num_teams";
28365 break;
28366 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28367 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28368 token->location);
28369 c_name = "thread_limit";
28370 break;
28371 case PRAGMA_OMP_CLAUSE_ALIGNED:
28372 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28373 c_name = "aligned";
28374 break;
28375 case PRAGMA_OMP_CLAUSE_LINEAR:
28376 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28377 cilk_simd_fn = true;
28378 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28379 c_name = "linear";
28380 break;
28381 case PRAGMA_OMP_CLAUSE_DEPEND:
28382 clauses = cp_parser_omp_clause_depend (parser, clauses);
28383 c_name = "depend";
28384 break;
28385 case PRAGMA_OMP_CLAUSE_MAP:
28386 clauses = cp_parser_omp_clause_map (parser, clauses);
28387 c_name = "map";
28388 break;
28389 case PRAGMA_OMP_CLAUSE_DEVICE:
28390 clauses = cp_parser_omp_clause_device (parser, clauses,
28391 token->location);
28392 c_name = "device";
28393 break;
28394 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28395 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28396 token->location);
28397 c_name = "dist_schedule";
28398 break;
28399 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28400 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28401 token->location);
28402 c_name = "proc_bind";
28403 break;
28404 case PRAGMA_OMP_CLAUSE_SAFELEN:
28405 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28406 token->location);
28407 c_name = "safelen";
28408 break;
28409 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28410 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28411 token->location);
28412 c_name = "simdlen";
28413 break;
28414 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28415 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28416 c_name = "simdlen";
28417 break;
28418 default:
28419 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28420 goto saw_error;
28423 first = false;
28425 if (((mask >> c_kind) & 1) == 0)
28427 /* Remove the invalid clause(s) from the list to avoid
28428 confusing the rest of the compiler. */
28429 clauses = prev;
28430 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28433 saw_error:
28434 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28435 no reason to skip to the end. */
28436 if (!(flag_cilkplus && pragma_tok == NULL))
28437 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28438 if (finish_p)
28439 return finish_omp_clauses (clauses);
28440 return clauses;
28443 /* OpenMP 2.5:
28444 structured-block:
28445 statement
28447 In practice, we're also interested in adding the statement to an
28448 outer node. So it is convenient if we work around the fact that
28449 cp_parser_statement calls add_stmt. */
28451 static unsigned
28452 cp_parser_begin_omp_structured_block (cp_parser *parser)
28454 unsigned save = parser->in_statement;
28456 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28457 This preserves the "not within loop or switch" style error messages
28458 for nonsense cases like
28459 void foo() {
28460 #pragma omp single
28461 break;
28464 if (parser->in_statement)
28465 parser->in_statement = IN_OMP_BLOCK;
28467 return save;
28470 static void
28471 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28473 parser->in_statement = save;
28476 static tree
28477 cp_parser_omp_structured_block (cp_parser *parser)
28479 tree stmt = begin_omp_structured_block ();
28480 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28482 cp_parser_statement (parser, NULL_TREE, false, NULL);
28484 cp_parser_end_omp_structured_block (parser, save);
28485 return finish_omp_structured_block (stmt);
28488 /* OpenMP 2.5:
28489 # pragma omp atomic new-line
28490 expression-stmt
28492 expression-stmt:
28493 x binop= expr | x++ | ++x | x-- | --x
28494 binop:
28495 +, *, -, /, &, ^, |, <<, >>
28497 where x is an lvalue expression with scalar type.
28499 OpenMP 3.1:
28500 # pragma omp atomic new-line
28501 update-stmt
28503 # pragma omp atomic read new-line
28504 read-stmt
28506 # pragma omp atomic write new-line
28507 write-stmt
28509 # pragma omp atomic update new-line
28510 update-stmt
28512 # pragma omp atomic capture new-line
28513 capture-stmt
28515 # pragma omp atomic capture new-line
28516 capture-block
28518 read-stmt:
28519 v = x
28520 write-stmt:
28521 x = expr
28522 update-stmt:
28523 expression-stmt | x = x binop expr
28524 capture-stmt:
28525 v = expression-stmt
28526 capture-block:
28527 { v = x; update-stmt; } | { update-stmt; v = x; }
28529 OpenMP 4.0:
28530 update-stmt:
28531 expression-stmt | x = x binop expr | x = expr binop x
28532 capture-stmt:
28533 v = update-stmt
28534 capture-block:
28535 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28537 where x and v are lvalue expressions with scalar type. */
28539 static void
28540 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28542 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28543 tree rhs1 = NULL_TREE, orig_lhs;
28544 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28545 bool structured_block = false;
28546 bool seq_cst = false;
28548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28551 const char *p = IDENTIFIER_POINTER (id);
28553 if (!strcmp (p, "read"))
28554 code = OMP_ATOMIC_READ;
28555 else if (!strcmp (p, "write"))
28556 code = NOP_EXPR;
28557 else if (!strcmp (p, "update"))
28558 code = OMP_ATOMIC;
28559 else if (!strcmp (p, "capture"))
28560 code = OMP_ATOMIC_CAPTURE_NEW;
28561 else
28562 p = NULL;
28563 if (p)
28564 cp_lexer_consume_token (parser->lexer);
28567 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28569 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28570 const char *p = IDENTIFIER_POINTER (id);
28572 if (!strcmp (p, "seq_cst"))
28574 seq_cst = true;
28575 cp_lexer_consume_token (parser->lexer);
28578 cp_parser_require_pragma_eol (parser, pragma_tok);
28580 switch (code)
28582 case OMP_ATOMIC_READ:
28583 case NOP_EXPR: /* atomic write */
28584 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28585 /*cast_p=*/false, NULL);
28586 if (v == error_mark_node)
28587 goto saw_error;
28588 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28589 goto saw_error;
28590 if (code == NOP_EXPR)
28591 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28592 else
28593 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28594 /*cast_p=*/false, NULL);
28595 if (lhs == error_mark_node)
28596 goto saw_error;
28597 if (code == NOP_EXPR)
28599 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28600 opcode. */
28601 code = OMP_ATOMIC;
28602 rhs = lhs;
28603 lhs = v;
28604 v = NULL_TREE;
28606 goto done;
28607 case OMP_ATOMIC_CAPTURE_NEW:
28608 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28610 cp_lexer_consume_token (parser->lexer);
28611 structured_block = true;
28613 else
28615 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28616 /*cast_p=*/false, NULL);
28617 if (v == error_mark_node)
28618 goto saw_error;
28619 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28620 goto saw_error;
28622 default:
28623 break;
28626 restart:
28627 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28628 /*cast_p=*/false, NULL);
28629 orig_lhs = lhs;
28630 switch (TREE_CODE (lhs))
28632 case ERROR_MARK:
28633 goto saw_error;
28635 case POSTINCREMENT_EXPR:
28636 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28637 code = OMP_ATOMIC_CAPTURE_OLD;
28638 /* FALLTHROUGH */
28639 case PREINCREMENT_EXPR:
28640 lhs = TREE_OPERAND (lhs, 0);
28641 opcode = PLUS_EXPR;
28642 rhs = integer_one_node;
28643 break;
28645 case POSTDECREMENT_EXPR:
28646 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28647 code = OMP_ATOMIC_CAPTURE_OLD;
28648 /* FALLTHROUGH */
28649 case PREDECREMENT_EXPR:
28650 lhs = TREE_OPERAND (lhs, 0);
28651 opcode = MINUS_EXPR;
28652 rhs = integer_one_node;
28653 break;
28655 case COMPOUND_EXPR:
28656 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28657 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28658 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28659 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28660 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28661 (TREE_OPERAND (lhs, 1), 0), 0)))
28662 == BOOLEAN_TYPE)
28663 /* Undo effects of boolean_increment for post {in,de}crement. */
28664 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28665 /* FALLTHRU */
28666 case MODIFY_EXPR:
28667 if (TREE_CODE (lhs) == MODIFY_EXPR
28668 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28670 /* Undo effects of boolean_increment. */
28671 if (integer_onep (TREE_OPERAND (lhs, 1)))
28673 /* This is pre or post increment. */
28674 rhs = TREE_OPERAND (lhs, 1);
28675 lhs = TREE_OPERAND (lhs, 0);
28676 opcode = NOP_EXPR;
28677 if (code == OMP_ATOMIC_CAPTURE_NEW
28678 && !structured_block
28679 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28680 code = OMP_ATOMIC_CAPTURE_OLD;
28681 break;
28684 /* FALLTHRU */
28685 default:
28686 switch (cp_lexer_peek_token (parser->lexer)->type)
28688 case CPP_MULT_EQ:
28689 opcode = MULT_EXPR;
28690 break;
28691 case CPP_DIV_EQ:
28692 opcode = TRUNC_DIV_EXPR;
28693 break;
28694 case CPP_PLUS_EQ:
28695 opcode = PLUS_EXPR;
28696 break;
28697 case CPP_MINUS_EQ:
28698 opcode = MINUS_EXPR;
28699 break;
28700 case CPP_LSHIFT_EQ:
28701 opcode = LSHIFT_EXPR;
28702 break;
28703 case CPP_RSHIFT_EQ:
28704 opcode = RSHIFT_EXPR;
28705 break;
28706 case CPP_AND_EQ:
28707 opcode = BIT_AND_EXPR;
28708 break;
28709 case CPP_OR_EQ:
28710 opcode = BIT_IOR_EXPR;
28711 break;
28712 case CPP_XOR_EQ:
28713 opcode = BIT_XOR_EXPR;
28714 break;
28715 case CPP_EQ:
28716 enum cp_parser_prec oprec;
28717 cp_token *token;
28718 cp_lexer_consume_token (parser->lexer);
28719 cp_parser_parse_tentatively (parser);
28720 rhs1 = cp_parser_simple_cast_expression (parser);
28721 if (rhs1 == error_mark_node)
28723 cp_parser_abort_tentative_parse (parser);
28724 cp_parser_simple_cast_expression (parser);
28725 goto saw_error;
28727 token = cp_lexer_peek_token (parser->lexer);
28728 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28730 cp_parser_abort_tentative_parse (parser);
28731 cp_parser_parse_tentatively (parser);
28732 rhs = cp_parser_binary_expression (parser, false, true,
28733 PREC_NOT_OPERATOR, NULL);
28734 if (rhs == error_mark_node)
28736 cp_parser_abort_tentative_parse (parser);
28737 cp_parser_binary_expression (parser, false, true,
28738 PREC_NOT_OPERATOR, NULL);
28739 goto saw_error;
28741 switch (TREE_CODE (rhs))
28743 case MULT_EXPR:
28744 case TRUNC_DIV_EXPR:
28745 case PLUS_EXPR:
28746 case MINUS_EXPR:
28747 case LSHIFT_EXPR:
28748 case RSHIFT_EXPR:
28749 case BIT_AND_EXPR:
28750 case BIT_IOR_EXPR:
28751 case BIT_XOR_EXPR:
28752 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28754 if (cp_parser_parse_definitely (parser))
28756 opcode = TREE_CODE (rhs);
28757 rhs1 = TREE_OPERAND (rhs, 0);
28758 rhs = TREE_OPERAND (rhs, 1);
28759 goto stmt_done;
28761 else
28762 goto saw_error;
28764 break;
28765 default:
28766 break;
28768 cp_parser_abort_tentative_parse (parser);
28769 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28771 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28772 if (rhs == error_mark_node)
28773 goto saw_error;
28774 opcode = NOP_EXPR;
28775 rhs1 = NULL_TREE;
28776 goto stmt_done;
28778 cp_parser_error (parser,
28779 "invalid form of %<#pragma omp atomic%>");
28780 goto saw_error;
28782 if (!cp_parser_parse_definitely (parser))
28783 goto saw_error;
28784 switch (token->type)
28786 case CPP_SEMICOLON:
28787 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28789 code = OMP_ATOMIC_CAPTURE_OLD;
28790 v = lhs;
28791 lhs = NULL_TREE;
28792 lhs1 = rhs1;
28793 rhs1 = NULL_TREE;
28794 cp_lexer_consume_token (parser->lexer);
28795 goto restart;
28797 else if (structured_block)
28799 opcode = NOP_EXPR;
28800 rhs = rhs1;
28801 rhs1 = NULL_TREE;
28802 goto stmt_done;
28804 cp_parser_error (parser,
28805 "invalid form of %<#pragma omp atomic%>");
28806 goto saw_error;
28807 case CPP_MULT:
28808 opcode = MULT_EXPR;
28809 break;
28810 case CPP_DIV:
28811 opcode = TRUNC_DIV_EXPR;
28812 break;
28813 case CPP_PLUS:
28814 opcode = PLUS_EXPR;
28815 break;
28816 case CPP_MINUS:
28817 opcode = MINUS_EXPR;
28818 break;
28819 case CPP_LSHIFT:
28820 opcode = LSHIFT_EXPR;
28821 break;
28822 case CPP_RSHIFT:
28823 opcode = RSHIFT_EXPR;
28824 break;
28825 case CPP_AND:
28826 opcode = BIT_AND_EXPR;
28827 break;
28828 case CPP_OR:
28829 opcode = BIT_IOR_EXPR;
28830 break;
28831 case CPP_XOR:
28832 opcode = BIT_XOR_EXPR;
28833 break;
28834 default:
28835 cp_parser_error (parser,
28836 "invalid operator for %<#pragma omp atomic%>");
28837 goto saw_error;
28839 oprec = TOKEN_PRECEDENCE (token);
28840 gcc_assert (oprec != PREC_NOT_OPERATOR);
28841 if (commutative_tree_code (opcode))
28842 oprec = (enum cp_parser_prec) (oprec - 1);
28843 cp_lexer_consume_token (parser->lexer);
28844 rhs = cp_parser_binary_expression (parser, false, false,
28845 oprec, NULL);
28846 if (rhs == error_mark_node)
28847 goto saw_error;
28848 goto stmt_done;
28849 /* FALLTHROUGH */
28850 default:
28851 cp_parser_error (parser,
28852 "invalid operator for %<#pragma omp atomic%>");
28853 goto saw_error;
28855 cp_lexer_consume_token (parser->lexer);
28857 rhs = cp_parser_expression (parser, false, NULL);
28858 if (rhs == error_mark_node)
28859 goto saw_error;
28860 break;
28862 stmt_done:
28863 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28865 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28866 goto saw_error;
28867 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28868 /*cast_p=*/false, NULL);
28869 if (v == error_mark_node)
28870 goto saw_error;
28871 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28872 goto saw_error;
28873 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28874 /*cast_p=*/false, NULL);
28875 if (lhs1 == error_mark_node)
28876 goto saw_error;
28878 if (structured_block)
28880 cp_parser_consume_semicolon_at_end_of_statement (parser);
28881 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28883 done:
28884 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28885 if (!structured_block)
28886 cp_parser_consume_semicolon_at_end_of_statement (parser);
28887 return;
28889 saw_error:
28890 cp_parser_skip_to_end_of_block_or_statement (parser);
28891 if (structured_block)
28893 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28894 cp_lexer_consume_token (parser->lexer);
28895 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28897 cp_parser_skip_to_end_of_block_or_statement (parser);
28898 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28899 cp_lexer_consume_token (parser->lexer);
28905 /* OpenMP 2.5:
28906 # pragma omp barrier new-line */
28908 static void
28909 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28911 cp_parser_require_pragma_eol (parser, pragma_tok);
28912 finish_omp_barrier ();
28915 /* OpenMP 2.5:
28916 # pragma omp critical [(name)] new-line
28917 structured-block */
28919 static tree
28920 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28922 tree stmt, name = NULL;
28924 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28926 cp_lexer_consume_token (parser->lexer);
28928 name = cp_parser_identifier (parser);
28930 if (name == error_mark_node
28931 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28932 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28933 /*or_comma=*/false,
28934 /*consume_paren=*/true);
28935 if (name == error_mark_node)
28936 name = NULL;
28938 cp_parser_require_pragma_eol (parser, pragma_tok);
28940 stmt = cp_parser_omp_structured_block (parser);
28941 return c_finish_omp_critical (input_location, stmt, name);
28944 /* OpenMP 2.5:
28945 # pragma omp flush flush-vars[opt] new-line
28947 flush-vars:
28948 ( variable-list ) */
28950 static void
28951 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28954 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28955 cp_parser_require_pragma_eol (parser, pragma_tok);
28957 finish_omp_flush ();
28960 /* Helper function, to parse omp for increment expression. */
28962 static tree
28963 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
28965 tree cond = cp_parser_binary_expression (parser, false, true,
28966 PREC_NOT_OPERATOR, NULL);
28967 if (cond == error_mark_node
28968 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28970 cp_parser_skip_to_end_of_statement (parser);
28971 return error_mark_node;
28974 switch (TREE_CODE (cond))
28976 case GT_EXPR:
28977 case GE_EXPR:
28978 case LT_EXPR:
28979 case LE_EXPR:
28980 break;
28981 case NE_EXPR:
28982 if (code == CILK_SIMD)
28983 break;
28984 /* Fall through: OpenMP disallows NE_EXPR. */
28985 default:
28986 return error_mark_node;
28989 /* If decl is an iterator, preserve LHS and RHS of the relational
28990 expr until finish_omp_for. */
28991 if (decl
28992 && (type_dependent_expression_p (decl)
28993 || CLASS_TYPE_P (TREE_TYPE (decl))))
28994 return cond;
28996 return build_x_binary_op (input_location, TREE_CODE (cond),
28997 TREE_OPERAND (cond, 0), ERROR_MARK,
28998 TREE_OPERAND (cond, 1), ERROR_MARK,
28999 /*overload=*/NULL, tf_warning_or_error);
29002 /* Helper function, to parse omp for increment expression. */
29004 static tree
29005 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29007 cp_token *token = cp_lexer_peek_token (parser->lexer);
29008 enum tree_code op;
29009 tree lhs, rhs;
29010 cp_id_kind idk;
29011 bool decl_first;
29013 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29015 op = (token->type == CPP_PLUS_PLUS
29016 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29017 cp_lexer_consume_token (parser->lexer);
29018 lhs = cp_parser_simple_cast_expression (parser);
29019 if (lhs != decl)
29020 return error_mark_node;
29021 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29024 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29025 if (lhs != decl)
29026 return error_mark_node;
29028 token = cp_lexer_peek_token (parser->lexer);
29029 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29031 op = (token->type == CPP_PLUS_PLUS
29032 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29033 cp_lexer_consume_token (parser->lexer);
29034 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29037 op = cp_parser_assignment_operator_opt (parser);
29038 if (op == ERROR_MARK)
29039 return error_mark_node;
29041 if (op != NOP_EXPR)
29043 rhs = cp_parser_assignment_expression (parser, false, NULL);
29044 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29045 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29048 lhs = cp_parser_binary_expression (parser, false, false,
29049 PREC_ADDITIVE_EXPRESSION, NULL);
29050 token = cp_lexer_peek_token (parser->lexer);
29051 decl_first = lhs == decl;
29052 if (decl_first)
29053 lhs = NULL_TREE;
29054 if (token->type != CPP_PLUS
29055 && token->type != CPP_MINUS)
29056 return error_mark_node;
29060 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29061 cp_lexer_consume_token (parser->lexer);
29062 rhs = cp_parser_binary_expression (parser, false, false,
29063 PREC_ADDITIVE_EXPRESSION, NULL);
29064 token = cp_lexer_peek_token (parser->lexer);
29065 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29067 if (lhs == NULL_TREE)
29069 if (op == PLUS_EXPR)
29070 lhs = rhs;
29071 else
29072 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29073 tf_warning_or_error);
29075 else
29076 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29077 ERROR_MARK, NULL, tf_warning_or_error);
29080 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29082 if (!decl_first)
29084 if (rhs != decl || op == MINUS_EXPR)
29085 return error_mark_node;
29086 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29088 else
29089 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29091 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29094 /* Parse the initialization statement of either an OpenMP for loop or
29095 a Cilk Plus for loop.
29097 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29098 Plus.
29100 Return true if the resulting construct should have an
29101 OMP_CLAUSE_PRIVATE added to it. */
29103 static bool
29104 cp_parser_omp_for_loop_init (cp_parser *parser,
29105 bool parsing_openmp,
29106 tree &this_pre_body,
29107 vec<tree, va_gc> *for_block,
29108 tree &init,
29109 tree &decl,
29110 tree &real_decl)
29112 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29113 return false;
29115 bool add_private_clause = false;
29117 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29119 init-expr:
29120 var = lb
29121 integer-type var = lb
29122 random-access-iterator-type var = lb
29123 pointer-type var = lb
29125 cp_decl_specifier_seq type_specifiers;
29127 /* First, try to parse as an initialized declaration. See
29128 cp_parser_condition, from whence the bulk of this is copied. */
29130 cp_parser_parse_tentatively (parser);
29131 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29132 /*is_trailing_return=*/false,
29133 &type_specifiers);
29134 if (cp_parser_parse_definitely (parser))
29136 /* If parsing a type specifier seq succeeded, then this
29137 MUST be a initialized declaration. */
29138 tree asm_specification, attributes;
29139 cp_declarator *declarator;
29141 declarator = cp_parser_declarator (parser,
29142 CP_PARSER_DECLARATOR_NAMED,
29143 /*ctor_dtor_or_conv_p=*/NULL,
29144 /*parenthesized_p=*/NULL,
29145 /*member_p=*/false);
29146 attributes = cp_parser_attributes_opt (parser);
29147 asm_specification = cp_parser_asm_specification_opt (parser);
29149 if (declarator == cp_error_declarator)
29150 cp_parser_skip_to_end_of_statement (parser);
29152 else
29154 tree pushed_scope, auto_node;
29156 decl = start_decl (declarator, &type_specifiers,
29157 SD_INITIALIZED, attributes,
29158 /*prefix_attributes=*/NULL_TREE,
29159 &pushed_scope);
29161 auto_node = type_uses_auto (TREE_TYPE (decl));
29162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29164 if (cp_lexer_next_token_is (parser->lexer,
29165 CPP_OPEN_PAREN))
29167 if (parsing_openmp)
29168 error ("parenthesized initialization is not allowed in "
29169 "OpenMP %<for%> loop");
29170 else
29171 error ("parenthesized initialization is "
29172 "not allowed in for-loop");
29174 else
29175 /* Trigger an error. */
29176 cp_parser_require (parser, CPP_EQ, RT_EQ);
29178 init = error_mark_node;
29179 cp_parser_skip_to_end_of_statement (parser);
29181 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29182 || type_dependent_expression_p (decl)
29183 || auto_node)
29185 bool is_direct_init, is_non_constant_init;
29187 init = cp_parser_initializer (parser,
29188 &is_direct_init,
29189 &is_non_constant_init);
29191 if (auto_node)
29193 TREE_TYPE (decl)
29194 = do_auto_deduction (TREE_TYPE (decl), init,
29195 auto_node);
29197 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29198 && !type_dependent_expression_p (decl))
29199 goto non_class;
29202 cp_finish_decl (decl, init, !is_non_constant_init,
29203 asm_specification,
29204 LOOKUP_ONLYCONVERTING);
29205 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29207 vec_safe_push (for_block, this_pre_body);
29208 init = NULL_TREE;
29210 else
29211 init = pop_stmt_list (this_pre_body);
29212 this_pre_body = NULL_TREE;
29214 else
29216 /* Consume '='. */
29217 cp_lexer_consume_token (parser->lexer);
29218 init = cp_parser_assignment_expression (parser, false, NULL);
29220 non_class:
29221 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29222 init = error_mark_node;
29223 else
29224 cp_finish_decl (decl, NULL_TREE,
29225 /*init_const_expr_p=*/false,
29226 asm_specification,
29227 LOOKUP_ONLYCONVERTING);
29230 if (pushed_scope)
29231 pop_scope (pushed_scope);
29234 else
29236 cp_id_kind idk;
29237 /* If parsing a type specifier sequence failed, then
29238 this MUST be a simple expression. */
29239 cp_parser_parse_tentatively (parser);
29240 decl = cp_parser_primary_expression (parser, false, false,
29241 false, &idk);
29242 if (!cp_parser_error_occurred (parser)
29243 && decl
29244 && DECL_P (decl)
29245 && CLASS_TYPE_P (TREE_TYPE (decl)))
29247 tree rhs;
29249 cp_parser_parse_definitely (parser);
29250 cp_parser_require (parser, CPP_EQ, RT_EQ);
29251 rhs = cp_parser_assignment_expression (parser, false, NULL);
29252 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29253 decl, NOP_EXPR,
29254 rhs,
29255 tf_warning_or_error));
29256 add_private_clause = true;
29258 else
29260 decl = NULL;
29261 cp_parser_abort_tentative_parse (parser);
29262 init = cp_parser_expression (parser, false, NULL);
29263 if (init)
29265 if (TREE_CODE (init) == MODIFY_EXPR
29266 || TREE_CODE (init) == MODOP_EXPR)
29267 real_decl = TREE_OPERAND (init, 0);
29271 return add_private_clause;
29274 /* Parse the restricted form of the for statement allowed by OpenMP. */
29276 static tree
29277 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29278 tree *cclauses)
29280 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29281 tree real_decl, initv, condv, incrv, declv;
29282 tree this_pre_body, cl;
29283 location_t loc_first;
29284 bool collapse_err = false;
29285 int i, collapse = 1, nbraces = 0;
29286 vec<tree, va_gc> *for_block = make_tree_vector ();
29288 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29289 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29290 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29292 gcc_assert (collapse >= 1);
29294 declv = make_tree_vec (collapse);
29295 initv = make_tree_vec (collapse);
29296 condv = make_tree_vec (collapse);
29297 incrv = make_tree_vec (collapse);
29299 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29301 for (i = 0; i < collapse; i++)
29303 int bracecount = 0;
29304 bool add_private_clause = false;
29305 location_t loc;
29307 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29309 cp_parser_error (parser, "for statement expected");
29310 return NULL;
29312 loc = cp_lexer_consume_token (parser->lexer)->location;
29314 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29315 return NULL;
29317 init = decl = real_decl = NULL;
29318 this_pre_body = push_stmt_list ();
29320 add_private_clause
29321 |= cp_parser_omp_for_loop_init (parser,
29322 /*parsing_openmp=*/code != CILK_SIMD,
29323 this_pre_body, for_block,
29324 init, decl, real_decl);
29326 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29327 if (this_pre_body)
29329 this_pre_body = pop_stmt_list (this_pre_body);
29330 if (pre_body)
29332 tree t = pre_body;
29333 pre_body = push_stmt_list ();
29334 add_stmt (t);
29335 add_stmt (this_pre_body);
29336 pre_body = pop_stmt_list (pre_body);
29338 else
29339 pre_body = this_pre_body;
29342 if (decl)
29343 real_decl = decl;
29344 if (cclauses != NULL
29345 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29346 && real_decl != NULL_TREE)
29348 tree *c;
29349 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29350 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29351 && OMP_CLAUSE_DECL (*c) == real_decl)
29353 error_at (loc, "iteration variable %qD"
29354 " should not be firstprivate", real_decl);
29355 *c = OMP_CLAUSE_CHAIN (*c);
29357 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29358 && OMP_CLAUSE_DECL (*c) == real_decl)
29360 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29361 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29362 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29363 OMP_CLAUSE_DECL (l) = real_decl;
29364 OMP_CLAUSE_CHAIN (l) = clauses;
29365 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29366 clauses = l;
29367 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29368 CP_OMP_CLAUSE_INFO (*c) = NULL;
29369 add_private_clause = false;
29371 else
29373 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29374 && OMP_CLAUSE_DECL (*c) == real_decl)
29375 add_private_clause = false;
29376 c = &OMP_CLAUSE_CHAIN (*c);
29380 if (add_private_clause)
29382 tree c;
29383 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29385 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29386 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29387 && OMP_CLAUSE_DECL (c) == decl)
29388 break;
29389 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29390 && OMP_CLAUSE_DECL (c) == decl)
29391 error_at (loc, "iteration variable %qD "
29392 "should not be firstprivate",
29393 decl);
29394 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29395 && OMP_CLAUSE_DECL (c) == decl)
29396 error_at (loc, "iteration variable %qD should not be reduction",
29397 decl);
29399 if (c == NULL)
29401 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29402 OMP_CLAUSE_DECL (c) = decl;
29403 c = finish_omp_clauses (c);
29404 if (c)
29406 OMP_CLAUSE_CHAIN (c) = clauses;
29407 clauses = c;
29412 cond = NULL;
29413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29414 cond = cp_parser_omp_for_cond (parser, decl, code);
29415 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29417 incr = NULL;
29418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29420 /* If decl is an iterator, preserve the operator on decl
29421 until finish_omp_for. */
29422 if (real_decl
29423 && ((processing_template_decl
29424 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29425 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29426 incr = cp_parser_omp_for_incr (parser, real_decl);
29427 else
29428 incr = cp_parser_expression (parser, false, NULL);
29429 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29430 SET_EXPR_LOCATION (incr, input_location);
29433 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29434 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29435 /*or_comma=*/false,
29436 /*consume_paren=*/true);
29438 TREE_VEC_ELT (declv, i) = decl;
29439 TREE_VEC_ELT (initv, i) = init;
29440 TREE_VEC_ELT (condv, i) = cond;
29441 TREE_VEC_ELT (incrv, i) = incr;
29443 if (i == collapse - 1)
29444 break;
29446 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29447 in between the collapsed for loops to be still considered perfectly
29448 nested. Hopefully the final version clarifies this.
29449 For now handle (multiple) {'s and empty statements. */
29450 cp_parser_parse_tentatively (parser);
29453 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29454 break;
29455 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29457 cp_lexer_consume_token (parser->lexer);
29458 bracecount++;
29460 else if (bracecount
29461 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29462 cp_lexer_consume_token (parser->lexer);
29463 else
29465 loc = cp_lexer_peek_token (parser->lexer)->location;
29466 error_at (loc, "not enough collapsed for loops");
29467 collapse_err = true;
29468 cp_parser_abort_tentative_parse (parser);
29469 declv = NULL_TREE;
29470 break;
29473 while (1);
29475 if (declv)
29477 cp_parser_parse_definitely (parser);
29478 nbraces += bracecount;
29482 /* Note that we saved the original contents of this flag when we entered
29483 the structured block, and so we don't need to re-save it here. */
29484 if (code == CILK_SIMD)
29485 parser->in_statement = IN_CILK_SIMD_FOR;
29486 else
29487 parser->in_statement = IN_OMP_FOR;
29489 /* Note that the grammar doesn't call for a structured block here,
29490 though the loop as a whole is a structured block. */
29491 body = push_stmt_list ();
29492 cp_parser_statement (parser, NULL_TREE, false, NULL);
29493 body = pop_stmt_list (body);
29495 if (declv == NULL_TREE)
29496 ret = NULL_TREE;
29497 else
29498 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29499 pre_body, clauses);
29501 while (nbraces)
29503 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29505 cp_lexer_consume_token (parser->lexer);
29506 nbraces--;
29508 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29509 cp_lexer_consume_token (parser->lexer);
29510 else
29512 if (!collapse_err)
29514 error_at (cp_lexer_peek_token (parser->lexer)->location,
29515 "collapsed loops not perfectly nested");
29517 collapse_err = true;
29518 cp_parser_statement_seq_opt (parser, NULL);
29519 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29520 break;
29524 while (!for_block->is_empty ())
29525 add_stmt (pop_stmt_list (for_block->pop ()));
29526 release_tree_vector (for_block);
29528 return ret;
29531 /* Helper function for OpenMP parsing, split clauses and call
29532 finish_omp_clauses on each of the set of clauses afterwards. */
29534 static void
29535 cp_omp_split_clauses (location_t loc, enum tree_code code,
29536 omp_clause_mask mask, tree clauses, tree *cclauses)
29538 int i;
29539 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29540 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29541 if (cclauses[i])
29542 cclauses[i] = finish_omp_clauses (cclauses[i]);
29545 /* OpenMP 4.0:
29546 #pragma omp simd simd-clause[optseq] new-line
29547 for-loop */
29549 #define OMP_SIMD_CLAUSE_MASK \
29550 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29558 static tree
29559 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29560 char *p_name, omp_clause_mask mask, tree *cclauses)
29562 tree clauses, sb, ret;
29563 unsigned int save;
29564 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29566 strcat (p_name, " simd");
29567 mask |= OMP_SIMD_CLAUSE_MASK;
29568 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29570 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29571 cclauses == NULL);
29572 if (cclauses)
29574 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29575 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29578 sb = begin_omp_structured_block ();
29579 save = cp_parser_begin_omp_structured_block (parser);
29581 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29583 cp_parser_end_omp_structured_block (parser, save);
29584 add_stmt (finish_omp_structured_block (sb));
29586 return ret;
29589 /* OpenMP 2.5:
29590 #pragma omp for for-clause[optseq] new-line
29591 for-loop
29593 OpenMP 4.0:
29594 #pragma omp for simd for-simd-clause[optseq] new-line
29595 for-loop */
29597 #define OMP_FOR_CLAUSE_MASK \
29598 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29607 static tree
29608 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29609 char *p_name, omp_clause_mask mask, tree *cclauses)
29611 tree clauses, sb, ret;
29612 unsigned int save;
29613 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29615 strcat (p_name, " for");
29616 mask |= OMP_FOR_CLAUSE_MASK;
29617 if (cclauses)
29618 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29622 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29623 const char *p = IDENTIFIER_POINTER (id);
29625 if (strcmp (p, "simd") == 0)
29627 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29628 if (cclauses == NULL)
29629 cclauses = cclauses_buf;
29631 cp_lexer_consume_token (parser->lexer);
29632 if (!flag_openmp) /* flag_openmp_simd */
29633 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29634 cclauses);
29635 sb = begin_omp_structured_block ();
29636 save = cp_parser_begin_omp_structured_block (parser);
29637 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29638 cclauses);
29639 cp_parser_end_omp_structured_block (parser, save);
29640 tree body = finish_omp_structured_block (sb);
29641 if (ret == NULL)
29642 return ret;
29643 ret = make_node (OMP_FOR);
29644 TREE_TYPE (ret) = void_type_node;
29645 OMP_FOR_BODY (ret) = body;
29646 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29647 SET_EXPR_LOCATION (ret, loc);
29648 add_stmt (ret);
29649 return ret;
29652 if (!flag_openmp) /* flag_openmp_simd */
29654 cp_parser_require_pragma_eol (parser, pragma_tok);
29655 return NULL_TREE;
29658 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29659 cclauses == NULL);
29660 if (cclauses)
29662 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29663 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29666 sb = begin_omp_structured_block ();
29667 save = cp_parser_begin_omp_structured_block (parser);
29669 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29671 cp_parser_end_omp_structured_block (parser, save);
29672 add_stmt (finish_omp_structured_block (sb));
29674 return ret;
29677 /* OpenMP 2.5:
29678 # pragma omp master new-line
29679 structured-block */
29681 static tree
29682 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29684 cp_parser_require_pragma_eol (parser, pragma_tok);
29685 return c_finish_omp_master (input_location,
29686 cp_parser_omp_structured_block (parser));
29689 /* OpenMP 2.5:
29690 # pragma omp ordered new-line
29691 structured-block */
29693 static tree
29694 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29696 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29697 cp_parser_require_pragma_eol (parser, pragma_tok);
29698 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29701 /* OpenMP 2.5:
29703 section-scope:
29704 { section-sequence }
29706 section-sequence:
29707 section-directive[opt] structured-block
29708 section-sequence section-directive structured-block */
29710 static tree
29711 cp_parser_omp_sections_scope (cp_parser *parser)
29713 tree stmt, substmt;
29714 bool error_suppress = false;
29715 cp_token *tok;
29717 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29718 return NULL_TREE;
29720 stmt = push_stmt_list ();
29722 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29724 substmt = cp_parser_omp_structured_block (parser);
29725 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29726 add_stmt (substmt);
29729 while (1)
29731 tok = cp_lexer_peek_token (parser->lexer);
29732 if (tok->type == CPP_CLOSE_BRACE)
29733 break;
29734 if (tok->type == CPP_EOF)
29735 break;
29737 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29739 cp_lexer_consume_token (parser->lexer);
29740 cp_parser_require_pragma_eol (parser, tok);
29741 error_suppress = false;
29743 else if (!error_suppress)
29745 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29746 error_suppress = true;
29749 substmt = cp_parser_omp_structured_block (parser);
29750 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29751 add_stmt (substmt);
29753 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29755 substmt = pop_stmt_list (stmt);
29757 stmt = make_node (OMP_SECTIONS);
29758 TREE_TYPE (stmt) = void_type_node;
29759 OMP_SECTIONS_BODY (stmt) = substmt;
29761 add_stmt (stmt);
29762 return stmt;
29765 /* OpenMP 2.5:
29766 # pragma omp sections sections-clause[optseq] newline
29767 sections-scope */
29769 #define OMP_SECTIONS_CLAUSE_MASK \
29770 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29776 static tree
29777 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29778 char *p_name, omp_clause_mask mask, tree *cclauses)
29780 tree clauses, ret;
29781 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29783 strcat (p_name, " sections");
29784 mask |= OMP_SECTIONS_CLAUSE_MASK;
29785 if (cclauses)
29786 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29788 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29789 cclauses == NULL);
29790 if (cclauses)
29792 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29793 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29796 ret = cp_parser_omp_sections_scope (parser);
29797 if (ret)
29798 OMP_SECTIONS_CLAUSES (ret) = clauses;
29800 return ret;
29803 /* OpenMP 2.5:
29804 # pragma omp parallel parallel-clause[optseq] new-line
29805 structured-block
29806 # pragma omp parallel for parallel-for-clause[optseq] new-line
29807 structured-block
29808 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29809 structured-block
29811 OpenMP 4.0:
29812 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29813 structured-block */
29815 #define OMP_PARALLEL_CLAUSE_MASK \
29816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29826 static tree
29827 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29828 char *p_name, omp_clause_mask mask, tree *cclauses)
29830 tree stmt, clauses, block;
29831 unsigned int save;
29832 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29834 strcat (p_name, " parallel");
29835 mask |= OMP_PARALLEL_CLAUSE_MASK;
29837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29839 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29840 if (cclauses == NULL)
29841 cclauses = cclauses_buf;
29843 cp_lexer_consume_token (parser->lexer);
29844 if (!flag_openmp) /* flag_openmp_simd */
29845 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29846 block = begin_omp_parallel ();
29847 save = cp_parser_begin_omp_structured_block (parser);
29848 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29849 cp_parser_end_omp_structured_block (parser, save);
29850 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29851 block);
29852 OMP_PARALLEL_COMBINED (stmt) = 1;
29853 return stmt;
29855 else if (cclauses)
29857 error_at (loc, "expected %<for%> after %qs", p_name);
29858 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29859 return NULL_TREE;
29861 else if (!flag_openmp) /* flag_openmp_simd */
29863 cp_parser_require_pragma_eol (parser, pragma_tok);
29864 return NULL_TREE;
29866 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29868 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29869 const char *p = IDENTIFIER_POINTER (id);
29870 if (strcmp (p, "sections") == 0)
29872 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29873 cclauses = cclauses_buf;
29875 cp_lexer_consume_token (parser->lexer);
29876 block = begin_omp_parallel ();
29877 save = cp_parser_begin_omp_structured_block (parser);
29878 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29879 cp_parser_end_omp_structured_block (parser, save);
29880 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29881 block);
29882 OMP_PARALLEL_COMBINED (stmt) = 1;
29883 return stmt;
29887 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29889 block = begin_omp_parallel ();
29890 save = cp_parser_begin_omp_structured_block (parser);
29891 cp_parser_statement (parser, NULL_TREE, false, NULL);
29892 cp_parser_end_omp_structured_block (parser, save);
29893 stmt = finish_omp_parallel (clauses, block);
29894 return stmt;
29897 /* OpenMP 2.5:
29898 # pragma omp single single-clause[optseq] new-line
29899 structured-block */
29901 #define OMP_SINGLE_CLAUSE_MASK \
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_COPYPRIVATE) \
29905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29907 static tree
29908 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29910 tree stmt = make_node (OMP_SINGLE);
29911 TREE_TYPE (stmt) = void_type_node;
29913 OMP_SINGLE_CLAUSES (stmt)
29914 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29915 "#pragma omp single", pragma_tok);
29916 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29918 return add_stmt (stmt);
29921 /* OpenMP 3.0:
29922 # pragma omp task task-clause[optseq] new-line
29923 structured-block */
29925 #define OMP_TASK_CLAUSE_MASK \
29926 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29936 static tree
29937 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29939 tree clauses, block;
29940 unsigned int save;
29942 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29943 "#pragma omp task", pragma_tok);
29944 block = begin_omp_task ();
29945 save = cp_parser_begin_omp_structured_block (parser);
29946 cp_parser_statement (parser, NULL_TREE, false, NULL);
29947 cp_parser_end_omp_structured_block (parser, save);
29948 return finish_omp_task (clauses, block);
29951 /* OpenMP 3.0:
29952 # pragma omp taskwait new-line */
29954 static void
29955 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29957 cp_parser_require_pragma_eol (parser, pragma_tok);
29958 finish_omp_taskwait ();
29961 /* OpenMP 3.1:
29962 # pragma omp taskyield new-line */
29964 static void
29965 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29967 cp_parser_require_pragma_eol (parser, pragma_tok);
29968 finish_omp_taskyield ();
29971 /* OpenMP 4.0:
29972 # pragma omp taskgroup new-line
29973 structured-block */
29975 static tree
29976 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29978 cp_parser_require_pragma_eol (parser, pragma_tok);
29979 return c_finish_omp_taskgroup (input_location,
29980 cp_parser_omp_structured_block (parser));
29984 /* OpenMP 2.5:
29985 # pragma omp threadprivate (variable-list) */
29987 static void
29988 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
29990 tree vars;
29992 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29993 cp_parser_require_pragma_eol (parser, pragma_tok);
29995 finish_omp_threadprivate (vars);
29998 /* OpenMP 4.0:
29999 # pragma omp cancel cancel-clause[optseq] new-line */
30001 #define OMP_CANCEL_CLAUSE_MASK \
30002 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30008 static void
30009 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30011 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30012 "#pragma omp cancel", pragma_tok);
30013 finish_omp_cancel (clauses);
30016 /* OpenMP 4.0:
30017 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30019 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30020 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30025 static void
30026 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30028 tree clauses;
30029 bool point_seen = false;
30031 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30033 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30034 const char *p = IDENTIFIER_POINTER (id);
30036 if (strcmp (p, "point") == 0)
30038 cp_lexer_consume_token (parser->lexer);
30039 point_seen = true;
30042 if (!point_seen)
30044 cp_parser_error (parser, "expected %<point%>");
30045 cp_parser_require_pragma_eol (parser, pragma_tok);
30046 return;
30049 clauses = cp_parser_omp_all_clauses (parser,
30050 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30051 "#pragma omp cancellation point",
30052 pragma_tok);
30053 finish_omp_cancellation_point (clauses);
30056 /* OpenMP 4.0:
30057 #pragma omp distribute distribute-clause[optseq] new-line
30058 for-loop */
30060 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30061 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30066 static tree
30067 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30068 char *p_name, omp_clause_mask mask, tree *cclauses)
30070 tree clauses, sb, ret;
30071 unsigned int save;
30072 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30074 strcat (p_name, " distribute");
30075 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30077 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30079 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30080 const char *p = IDENTIFIER_POINTER (id);
30081 bool simd = false;
30082 bool parallel = false;
30084 if (strcmp (p, "simd") == 0)
30085 simd = true;
30086 else
30087 parallel = strcmp (p, "parallel") == 0;
30088 if (parallel || simd)
30090 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30091 if (cclauses == NULL)
30092 cclauses = cclauses_buf;
30093 cp_lexer_consume_token (parser->lexer);
30094 if (!flag_openmp) /* flag_openmp_simd */
30096 if (simd)
30097 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30098 cclauses);
30099 else
30100 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30101 cclauses);
30103 sb = begin_omp_structured_block ();
30104 save = cp_parser_begin_omp_structured_block (parser);
30105 if (simd)
30106 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30107 cclauses);
30108 else
30109 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30110 cclauses);
30111 cp_parser_end_omp_structured_block (parser, save);
30112 tree body = finish_omp_structured_block (sb);
30113 if (ret == NULL)
30114 return ret;
30115 ret = make_node (OMP_DISTRIBUTE);
30116 TREE_TYPE (ret) = void_type_node;
30117 OMP_FOR_BODY (ret) = body;
30118 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30119 SET_EXPR_LOCATION (ret, loc);
30120 add_stmt (ret);
30121 return ret;
30124 if (!flag_openmp) /* flag_openmp_simd */
30126 cp_parser_require_pragma_eol (parser, pragma_tok);
30127 return NULL_TREE;
30130 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30131 cclauses == NULL);
30132 if (cclauses)
30134 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30135 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30138 sb = begin_omp_structured_block ();
30139 save = cp_parser_begin_omp_structured_block (parser);
30141 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30143 cp_parser_end_omp_structured_block (parser, save);
30144 add_stmt (finish_omp_structured_block (sb));
30146 return ret;
30149 /* OpenMP 4.0:
30150 # pragma omp teams teams-clause[optseq] new-line
30151 structured-block */
30153 #define OMP_TEAMS_CLAUSE_MASK \
30154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30162 static tree
30163 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30164 char *p_name, omp_clause_mask mask, tree *cclauses)
30166 tree clauses, sb, ret;
30167 unsigned int save;
30168 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30170 strcat (p_name, " teams");
30171 mask |= OMP_TEAMS_CLAUSE_MASK;
30173 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30175 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30176 const char *p = IDENTIFIER_POINTER (id);
30177 if (strcmp (p, "distribute") == 0)
30179 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30180 if (cclauses == NULL)
30181 cclauses = cclauses_buf;
30183 cp_lexer_consume_token (parser->lexer);
30184 if (!flag_openmp) /* flag_openmp_simd */
30185 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30186 cclauses);
30187 sb = begin_omp_structured_block ();
30188 save = cp_parser_begin_omp_structured_block (parser);
30189 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30190 cclauses);
30191 cp_parser_end_omp_structured_block (parser, save);
30192 tree body = finish_omp_structured_block (sb);
30193 if (ret == NULL)
30194 return ret;
30195 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30196 ret = make_node (OMP_TEAMS);
30197 TREE_TYPE (ret) = void_type_node;
30198 OMP_TEAMS_CLAUSES (ret) = clauses;
30199 OMP_TEAMS_BODY (ret) = body;
30200 return add_stmt (ret);
30203 if (!flag_openmp) /* flag_openmp_simd */
30205 cp_parser_require_pragma_eol (parser, pragma_tok);
30206 return NULL_TREE;
30209 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30210 cclauses == NULL);
30211 if (cclauses)
30213 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30214 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30217 tree stmt = make_node (OMP_TEAMS);
30218 TREE_TYPE (stmt) = void_type_node;
30219 OMP_TEAMS_CLAUSES (stmt) = clauses;
30220 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30222 return add_stmt (stmt);
30225 /* OpenMP 4.0:
30226 # pragma omp target data target-data-clause[optseq] new-line
30227 structured-block */
30229 #define OMP_TARGET_DATA_CLAUSE_MASK \
30230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30234 static tree
30235 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30237 tree stmt = make_node (OMP_TARGET_DATA);
30238 TREE_TYPE (stmt) = void_type_node;
30240 OMP_TARGET_DATA_CLAUSES (stmt)
30241 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30242 "#pragma omp target data", pragma_tok);
30243 keep_next_level (true);
30244 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30246 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30247 return add_stmt (stmt);
30250 /* OpenMP 4.0:
30251 # pragma omp target update target-update-clause[optseq] new-line */
30253 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30259 static bool
30260 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30261 enum pragma_context context)
30263 if (context == pragma_stmt)
30265 error_at (pragma_tok->location,
30266 "%<#pragma omp target update%> may only be "
30267 "used in compound statements");
30268 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30269 return false;
30272 tree clauses
30273 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30274 "#pragma omp target update", pragma_tok);
30275 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30276 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30278 error_at (pragma_tok->location,
30279 "%<#pragma omp target update must contain at least one "
30280 "%<from%> or %<to%> clauses");
30281 return false;
30284 tree stmt = make_node (OMP_TARGET_UPDATE);
30285 TREE_TYPE (stmt) = void_type_node;
30286 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30287 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30288 add_stmt (stmt);
30289 return false;
30292 /* OpenMP 4.0:
30293 # pragma omp target target-clause[optseq] new-line
30294 structured-block */
30296 #define OMP_TARGET_CLAUSE_MASK \
30297 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30301 static bool
30302 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30303 enum pragma_context context)
30305 if (context != pragma_stmt && context != pragma_compound)
30307 cp_parser_error (parser, "expected declaration specifiers");
30308 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30309 return false;
30312 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30314 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30315 const char *p = IDENTIFIER_POINTER (id);
30317 if (strcmp (p, "teams") == 0)
30319 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30320 char p_name[sizeof ("#pragma omp target teams distribute "
30321 "parallel for simd")];
30323 cp_lexer_consume_token (parser->lexer);
30324 strcpy (p_name, "#pragma omp target");
30325 if (!flag_openmp) /* flag_openmp_simd */
30326 return cp_parser_omp_teams (parser, pragma_tok, p_name,
30327 OMP_TARGET_CLAUSE_MASK, cclauses);
30328 keep_next_level (true);
30329 tree sb = begin_omp_structured_block ();
30330 unsigned save = cp_parser_begin_omp_structured_block (parser);
30331 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30332 OMP_TARGET_CLAUSE_MASK, cclauses);
30333 cp_parser_end_omp_structured_block (parser, save);
30334 tree body = finish_omp_structured_block (sb);
30335 if (ret == NULL)
30336 return ret;
30337 tree stmt = make_node (OMP_TARGET);
30338 TREE_TYPE (stmt) = void_type_node;
30339 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30340 OMP_TARGET_BODY (stmt) = body;
30341 add_stmt (stmt);
30342 return true;
30344 else if (!flag_openmp) /* flag_openmp_simd */
30346 cp_parser_require_pragma_eol (parser, pragma_tok);
30347 return NULL_TREE;
30349 else if (strcmp (p, "data") == 0)
30351 cp_lexer_consume_token (parser->lexer);
30352 cp_parser_omp_target_data (parser, pragma_tok);
30353 return true;
30355 else if (strcmp (p, "update") == 0)
30357 cp_lexer_consume_token (parser->lexer);
30358 return cp_parser_omp_target_update (parser, pragma_tok, context);
30362 tree stmt = make_node (OMP_TARGET);
30363 TREE_TYPE (stmt) = void_type_node;
30365 OMP_TARGET_CLAUSES (stmt)
30366 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30367 "#pragma omp target", pragma_tok);
30368 keep_next_level (true);
30369 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30371 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30372 add_stmt (stmt);
30373 return true;
30376 /* OpenMP 4.0:
30377 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30379 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30380 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30387 static void
30388 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30389 enum pragma_context context)
30391 bool first_p = parser->omp_declare_simd == NULL;
30392 cp_omp_declare_simd_data data;
30393 if (first_p)
30395 data.error_seen = false;
30396 data.fndecl_seen = false;
30397 data.tokens = vNULL;
30398 parser->omp_declare_simd = &data;
30400 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30401 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30402 cp_lexer_consume_token (parser->lexer);
30403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30404 parser->omp_declare_simd->error_seen = true;
30405 cp_parser_require_pragma_eol (parser, pragma_tok);
30406 struct cp_token_cache *cp
30407 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30408 parser->omp_declare_simd->tokens.safe_push (cp);
30409 if (first_p)
30411 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30412 cp_parser_pragma (parser, context);
30413 switch (context)
30415 case pragma_external:
30416 cp_parser_declaration (parser);
30417 break;
30418 case pragma_member:
30419 cp_parser_member_declaration (parser);
30420 break;
30421 case pragma_objc_icode:
30422 cp_parser_block_declaration (parser, /*statement_p=*/false);
30423 break;
30424 default:
30425 cp_parser_declaration_statement (parser);
30426 break;
30428 if (parser->omp_declare_simd
30429 && !parser->omp_declare_simd->error_seen
30430 && !parser->omp_declare_simd->fndecl_seen)
30431 error_at (pragma_tok->location,
30432 "%<#pragma omp declare simd%> not immediately followed by "
30433 "function declaration or definition");
30434 data.tokens.release ();
30435 parser->omp_declare_simd = NULL;
30439 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30440 This function is modelled similar to the late parsing of omp declare
30441 simd. */
30443 static tree
30444 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30446 struct cp_token_cache *ce;
30447 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30448 int ii = 0;
30450 if (parser->omp_declare_simd != NULL)
30452 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30453 " marked as a Cilk Plus SIMD-enabled function");
30454 XDELETE (parser->cilk_simd_fn_info);
30455 parser->cilk_simd_fn_info = NULL;
30456 return attrs;
30458 if (!info->error_seen && info->fndecl_seen)
30460 error ("vector attribute not immediately followed by a single function"
30461 " declaration or definition");
30462 info->error_seen = true;
30464 if (info->error_seen)
30465 return attrs;
30467 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30469 tree c, cl;
30471 cp_parser_push_lexer_for_tokens (parser, ce);
30472 parser->lexer->in_pragma = true;
30473 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30474 "SIMD-enabled functions attribute",
30475 NULL);
30476 cp_parser_pop_lexer (parser);
30477 if (cl)
30478 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30480 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30481 TREE_CHAIN (c) = attrs;
30482 attrs = c;
30484 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30485 TREE_CHAIN (c) = attrs;
30486 if (processing_template_decl)
30487 ATTR_IS_DEPENDENT (c) = 1;
30488 attrs = c;
30490 info->fndecl_seen = true;
30491 XDELETE (parser->cilk_simd_fn_info);
30492 parser->cilk_simd_fn_info = NULL;
30493 return attrs;
30496 /* Finalize #pragma omp declare simd clauses after direct declarator has
30497 been parsed, and put that into "omp declare simd" attribute. */
30499 static tree
30500 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30502 struct cp_token_cache *ce;
30503 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30504 int i;
30506 if (!data->error_seen && data->fndecl_seen)
30508 error ("%<#pragma omp declare simd%> not immediately followed by "
30509 "a single function declaration or definition");
30510 data->error_seen = true;
30511 return attrs;
30513 if (data->error_seen)
30514 return attrs;
30516 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30518 tree c, cl;
30520 cp_parser_push_lexer_for_tokens (parser, ce);
30521 parser->lexer->in_pragma = true;
30522 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30523 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30524 cp_lexer_consume_token (parser->lexer);
30525 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30526 "#pragma omp declare simd", pragma_tok);
30527 cp_parser_pop_lexer (parser);
30528 if (cl)
30529 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30530 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30531 TREE_CHAIN (c) = attrs;
30532 if (processing_template_decl)
30533 ATTR_IS_DEPENDENT (c) = 1;
30534 attrs = c;
30537 data->fndecl_seen = true;
30538 return attrs;
30542 /* OpenMP 4.0:
30543 # pragma omp declare target new-line
30544 declarations and definitions
30545 # pragma omp end declare target new-line */
30547 static void
30548 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30550 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30551 scope_chain->omp_declare_target_attribute++;
30554 static void
30555 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30557 const char *p = "";
30558 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30560 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30561 p = IDENTIFIER_POINTER (id);
30563 if (strcmp (p, "declare") == 0)
30565 cp_lexer_consume_token (parser->lexer);
30566 p = "";
30567 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30569 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30570 p = IDENTIFIER_POINTER (id);
30572 if (strcmp (p, "target") == 0)
30573 cp_lexer_consume_token (parser->lexer);
30574 else
30576 cp_parser_error (parser, "expected %<target%>");
30577 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30578 return;
30581 else
30583 cp_parser_error (parser, "expected %<declare%>");
30584 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30585 return;
30587 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30588 if (!scope_chain->omp_declare_target_attribute)
30589 error_at (pragma_tok->location,
30590 "%<#pragma omp end declare target%> without corresponding "
30591 "%<#pragma omp declare target%>");
30592 else
30593 scope_chain->omp_declare_target_attribute--;
30596 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30597 expression and optional initializer clause of
30598 #pragma omp declare reduction. We store the expression(s) as
30599 either 3, 6 or 7 special statements inside of the artificial function's
30600 body. The first two statements are DECL_EXPRs for the artificial
30601 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30602 expression that uses those variables.
30603 If there was any INITIALIZER clause, this is followed by further statements,
30604 the fourth and fifth statements are DECL_EXPRs for the artificial
30605 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30606 constructor variant (first token after open paren is not omp_priv),
30607 then the sixth statement is a statement with the function call expression
30608 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30609 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30610 to initialize the OMP_PRIV artificial variable and there is seventh
30611 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30613 static bool
30614 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30616 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30617 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30618 type = TREE_TYPE (type);
30619 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30620 DECL_ARTIFICIAL (omp_out) = 1;
30621 pushdecl (omp_out);
30622 add_decl_expr (omp_out);
30623 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30624 DECL_ARTIFICIAL (omp_in) = 1;
30625 pushdecl (omp_in);
30626 add_decl_expr (omp_in);
30627 tree combiner;
30628 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30630 keep_next_level (true);
30631 tree block = begin_omp_structured_block ();
30632 combiner = cp_parser_expression (parser, false, NULL);
30633 finish_expr_stmt (combiner);
30634 block = finish_omp_structured_block (block);
30635 add_stmt (block);
30637 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30638 return false;
30640 const char *p = "";
30641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30644 p = IDENTIFIER_POINTER (id);
30647 if (strcmp (p, "initializer") == 0)
30649 cp_lexer_consume_token (parser->lexer);
30650 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30651 return false;
30653 p = "";
30654 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30656 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30657 p = IDENTIFIER_POINTER (id);
30660 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30661 DECL_ARTIFICIAL (omp_priv) = 1;
30662 pushdecl (omp_priv);
30663 add_decl_expr (omp_priv);
30664 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30665 DECL_ARTIFICIAL (omp_orig) = 1;
30666 pushdecl (omp_orig);
30667 add_decl_expr (omp_orig);
30669 keep_next_level (true);
30670 block = begin_omp_structured_block ();
30672 bool ctor = false;
30673 if (strcmp (p, "omp_priv") == 0)
30675 bool is_direct_init, is_non_constant_init;
30676 ctor = true;
30677 cp_lexer_consume_token (parser->lexer);
30678 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30680 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30681 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30682 == CPP_CLOSE_PAREN
30683 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30684 == CPP_CLOSE_PAREN))
30686 finish_omp_structured_block (block);
30687 error ("invalid initializer clause");
30688 return false;
30690 initializer = cp_parser_initializer (parser, &is_direct_init,
30691 &is_non_constant_init);
30692 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30693 NULL_TREE, LOOKUP_ONLYCONVERTING);
30695 else
30697 cp_parser_parse_tentatively (parser);
30698 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30699 /*check_dependency_p=*/true,
30700 /*template_p=*/NULL,
30701 /*declarator_p=*/false,
30702 /*optional_p=*/false);
30703 vec<tree, va_gc> *args;
30704 if (fn_name == error_mark_node
30705 || cp_parser_error_occurred (parser)
30706 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30707 || ((args = cp_parser_parenthesized_expression_list
30708 (parser, non_attr, /*cast_p=*/false,
30709 /*allow_expansion_p=*/true,
30710 /*non_constant_p=*/NULL)),
30711 cp_parser_error_occurred (parser)))
30713 finish_omp_structured_block (block);
30714 cp_parser_abort_tentative_parse (parser);
30715 cp_parser_error (parser, "expected id-expression (arguments)");
30716 return false;
30718 unsigned int i;
30719 tree arg;
30720 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30721 if (arg == omp_priv
30722 || (TREE_CODE (arg) == ADDR_EXPR
30723 && TREE_OPERAND (arg, 0) == omp_priv))
30724 break;
30725 cp_parser_abort_tentative_parse (parser);
30726 if (arg == NULL_TREE)
30727 error ("one of the initializer call arguments should be %<omp_priv%>"
30728 " or %<&omp_priv%>");
30729 initializer = cp_parser_postfix_expression (parser, false, false, false,
30730 false, NULL);
30731 finish_expr_stmt (initializer);
30734 block = finish_omp_structured_block (block);
30735 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30736 finish_expr_stmt (block);
30738 if (ctor)
30739 add_decl_expr (omp_orig);
30741 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30742 return false;
30745 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30746 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30748 return true;
30751 /* OpenMP 4.0
30752 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30753 initializer-clause[opt] new-line
30755 initializer-clause:
30756 initializer (omp_priv initializer)
30757 initializer (function-name (argument-list)) */
30759 static void
30760 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30761 enum pragma_context)
30763 auto_vec<tree> types;
30764 enum tree_code reduc_code = ERROR_MARK;
30765 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30766 unsigned int i;
30767 cp_token *first_token;
30768 cp_token_cache *cp;
30769 int errs;
30770 void *p;
30772 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
30773 p = obstack_alloc (&declarator_obstack, 0);
30775 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30776 goto fail;
30778 switch (cp_lexer_peek_token (parser->lexer)->type)
30780 case CPP_PLUS:
30781 reduc_code = PLUS_EXPR;
30782 break;
30783 case CPP_MULT:
30784 reduc_code = MULT_EXPR;
30785 break;
30786 case CPP_MINUS:
30787 reduc_code = MINUS_EXPR;
30788 break;
30789 case CPP_AND:
30790 reduc_code = BIT_AND_EXPR;
30791 break;
30792 case CPP_XOR:
30793 reduc_code = BIT_XOR_EXPR;
30794 break;
30795 case CPP_OR:
30796 reduc_code = BIT_IOR_EXPR;
30797 break;
30798 case CPP_AND_AND:
30799 reduc_code = TRUTH_ANDIF_EXPR;
30800 break;
30801 case CPP_OR_OR:
30802 reduc_code = TRUTH_ORIF_EXPR;
30803 break;
30804 case CPP_NAME:
30805 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30806 break;
30807 default:
30808 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30809 "%<|%>, %<&&%>, %<||%> or identifier");
30810 goto fail;
30813 if (reduc_code != ERROR_MARK)
30814 cp_lexer_consume_token (parser->lexer);
30816 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30817 if (reduc_id == error_mark_node)
30818 goto fail;
30820 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30821 goto fail;
30823 /* Types may not be defined in declare reduction type list. */
30824 const char *saved_message;
30825 saved_message = parser->type_definition_forbidden_message;
30826 parser->type_definition_forbidden_message
30827 = G_("types may not be defined in declare reduction type list");
30828 bool saved_colon_corrects_to_scope_p;
30829 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30830 parser->colon_corrects_to_scope_p = false;
30831 bool saved_colon_doesnt_start_class_def_p;
30832 saved_colon_doesnt_start_class_def_p
30833 = parser->colon_doesnt_start_class_def_p;
30834 parser->colon_doesnt_start_class_def_p = true;
30836 while (true)
30838 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30839 type = cp_parser_type_id (parser);
30840 if (type == error_mark_node)
30842 else if (ARITHMETIC_TYPE_P (type)
30843 && (orig_reduc_id == NULL_TREE
30844 || (TREE_CODE (type) != COMPLEX_TYPE
30845 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30846 "min") == 0
30847 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30848 "max") == 0))))
30849 error_at (loc, "predeclared arithmetic type %qT in "
30850 "%<#pragma omp declare reduction%>", type);
30851 else if (TREE_CODE (type) == FUNCTION_TYPE
30852 || TREE_CODE (type) == METHOD_TYPE
30853 || TREE_CODE (type) == ARRAY_TYPE)
30854 error_at (loc, "function or array type %qT in "
30855 "%<#pragma omp declare reduction%>", type);
30856 else if (TREE_CODE (type) == REFERENCE_TYPE)
30857 error_at (loc, "reference type %qT in "
30858 "%<#pragma omp declare reduction%>", type);
30859 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30860 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30861 "%<#pragma omp declare reduction%>", type);
30862 else
30863 types.safe_push (type);
30865 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30866 cp_lexer_consume_token (parser->lexer);
30867 else
30868 break;
30871 /* Restore the saved message. */
30872 parser->type_definition_forbidden_message = saved_message;
30873 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30874 parser->colon_doesnt_start_class_def_p
30875 = saved_colon_doesnt_start_class_def_p;
30877 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30878 || types.is_empty ())
30880 fail:
30881 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30882 goto done;
30885 first_token = cp_lexer_peek_token (parser->lexer);
30886 cp = NULL;
30887 errs = errorcount;
30888 FOR_EACH_VEC_ELT (types, i, type)
30890 tree fntype
30891 = build_function_type_list (void_type_node,
30892 cp_build_reference_type (type, false),
30893 NULL_TREE);
30894 tree this_reduc_id = reduc_id;
30895 if (!dependent_type_p (type))
30896 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30897 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30898 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30899 DECL_ARTIFICIAL (fndecl) = 1;
30900 DECL_EXTERNAL (fndecl) = 1;
30901 DECL_DECLARED_INLINE_P (fndecl) = 1;
30902 DECL_IGNORED_P (fndecl) = 1;
30903 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30904 DECL_ATTRIBUTES (fndecl)
30905 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30906 DECL_ATTRIBUTES (fndecl));
30907 if (processing_template_decl)
30908 fndecl = push_template_decl (fndecl);
30909 bool block_scope = false;
30910 tree block = NULL_TREE;
30911 if (current_function_decl)
30913 block_scope = true;
30914 DECL_CONTEXT (fndecl) = global_namespace;
30915 if (!processing_template_decl)
30916 pushdecl (fndecl);
30918 else if (current_class_type)
30920 if (cp == NULL)
30922 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30923 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30924 cp_lexer_consume_token (parser->lexer);
30925 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30926 goto fail;
30927 cp = cp_token_cache_new (first_token,
30928 cp_lexer_peek_nth_token (parser->lexer,
30929 2));
30931 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30932 finish_member_declaration (fndecl);
30933 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30934 DECL_PENDING_INLINE_P (fndecl) = 1;
30935 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30936 continue;
30938 else
30940 DECL_CONTEXT (fndecl) = current_namespace;
30941 pushdecl (fndecl);
30943 if (!block_scope)
30944 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30945 else
30946 block = begin_omp_structured_block ();
30947 if (cp)
30949 cp_parser_push_lexer_for_tokens (parser, cp);
30950 parser->lexer->in_pragma = true;
30952 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30954 if (!block_scope)
30955 finish_function (0);
30956 else
30957 DECL_CONTEXT (fndecl) = current_function_decl;
30958 if (cp)
30959 cp_parser_pop_lexer (parser);
30960 goto fail;
30962 if (cp)
30963 cp_parser_pop_lexer (parser);
30964 if (!block_scope)
30965 finish_function (0);
30966 else
30968 DECL_CONTEXT (fndecl) = current_function_decl;
30969 block = finish_omp_structured_block (block);
30970 if (TREE_CODE (block) == BIND_EXPR)
30971 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30972 else if (TREE_CODE (block) == STATEMENT_LIST)
30973 DECL_SAVED_TREE (fndecl) = block;
30974 if (processing_template_decl)
30975 add_decl_expr (fndecl);
30977 cp_check_omp_declare_reduction (fndecl);
30978 if (cp == NULL && types.length () > 1)
30979 cp = cp_token_cache_new (first_token,
30980 cp_lexer_peek_nth_token (parser->lexer, 2));
30981 if (errs != errorcount)
30982 break;
30985 cp_parser_require_pragma_eol (parser, pragma_tok);
30987 done:
30988 /* Free any declarators allocated. */
30989 obstack_free (&declarator_obstack, p);
30992 /* OpenMP 4.0
30993 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30994 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30995 initializer-clause[opt] new-line
30996 #pragma omp declare target new-line */
30998 static void
30999 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31000 enum pragma_context context)
31002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31004 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31005 const char *p = IDENTIFIER_POINTER (id);
31007 if (strcmp (p, "simd") == 0)
31009 cp_lexer_consume_token (parser->lexer);
31010 cp_parser_omp_declare_simd (parser, pragma_tok,
31011 context);
31012 return;
31014 cp_ensure_no_omp_declare_simd (parser);
31015 if (strcmp (p, "reduction") == 0)
31017 cp_lexer_consume_token (parser->lexer);
31018 cp_parser_omp_declare_reduction (parser, pragma_tok,
31019 context);
31020 return;
31022 if (!flag_openmp) /* flag_openmp_simd */
31024 cp_parser_require_pragma_eol (parser, pragma_tok);
31025 return;
31027 if (strcmp (p, "target") == 0)
31029 cp_lexer_consume_token (parser->lexer);
31030 cp_parser_omp_declare_target (parser, pragma_tok);
31031 return;
31034 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31035 "or %<target%>");
31036 cp_parser_require_pragma_eol (parser, pragma_tok);
31039 /* Main entry point to OpenMP statement pragmas. */
31041 static void
31042 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31044 tree stmt;
31045 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31046 omp_clause_mask mask (0);
31048 switch (pragma_tok->pragma_kind)
31050 case PRAGMA_OMP_ATOMIC:
31051 cp_parser_omp_atomic (parser, pragma_tok);
31052 return;
31053 case PRAGMA_OMP_CRITICAL:
31054 stmt = cp_parser_omp_critical (parser, pragma_tok);
31055 break;
31056 case PRAGMA_OMP_DISTRIBUTE:
31057 strcpy (p_name, "#pragma omp");
31058 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31059 break;
31060 case PRAGMA_OMP_FOR:
31061 strcpy (p_name, "#pragma omp");
31062 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31063 break;
31064 case PRAGMA_OMP_MASTER:
31065 stmt = cp_parser_omp_master (parser, pragma_tok);
31066 break;
31067 case PRAGMA_OMP_ORDERED:
31068 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31069 break;
31070 case PRAGMA_OMP_PARALLEL:
31071 strcpy (p_name, "#pragma omp");
31072 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31073 break;
31074 case PRAGMA_OMP_SECTIONS:
31075 strcpy (p_name, "#pragma omp");
31076 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31077 break;
31078 case PRAGMA_OMP_SIMD:
31079 strcpy (p_name, "#pragma omp");
31080 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31081 break;
31082 case PRAGMA_OMP_SINGLE:
31083 stmt = cp_parser_omp_single (parser, pragma_tok);
31084 break;
31085 case PRAGMA_OMP_TASK:
31086 stmt = cp_parser_omp_task (parser, pragma_tok);
31087 break;
31088 case PRAGMA_OMP_TASKGROUP:
31089 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31090 break;
31091 case PRAGMA_OMP_TEAMS:
31092 strcpy (p_name, "#pragma omp");
31093 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31094 break;
31095 default:
31096 gcc_unreachable ();
31099 if (stmt)
31100 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31103 /* Transactional Memory parsing routines. */
31105 /* Parse a transaction attribute.
31107 txn-attribute:
31108 attribute
31109 [ [ identifier ] ]
31111 ??? Simplify this when C++0x bracket attributes are
31112 implemented properly. */
31114 static tree
31115 cp_parser_txn_attribute_opt (cp_parser *parser)
31117 cp_token *token;
31118 tree attr_name, attr = NULL;
31120 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31121 return cp_parser_attributes_opt (parser);
31123 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31124 return NULL_TREE;
31125 cp_lexer_consume_token (parser->lexer);
31126 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31127 goto error1;
31129 token = cp_lexer_peek_token (parser->lexer);
31130 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31132 token = cp_lexer_consume_token (parser->lexer);
31134 attr_name = (token->type == CPP_KEYWORD
31135 /* For keywords, use the canonical spelling,
31136 not the parsed identifier. */
31137 ? ridpointers[(int) token->keyword]
31138 : token->u.value);
31139 attr = build_tree_list (attr_name, NULL_TREE);
31141 else
31142 cp_parser_error (parser, "expected identifier");
31144 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31145 error1:
31146 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31147 return attr;
31150 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31152 transaction-statement:
31153 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31154 compound-statement
31155 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31158 static tree
31159 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31161 unsigned char old_in = parser->in_transaction;
31162 unsigned char this_in = 1, new_in;
31163 cp_token *token;
31164 tree stmt, attrs, noex;
31166 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31167 || keyword == RID_TRANSACTION_RELAXED);
31168 token = cp_parser_require_keyword (parser, keyword,
31169 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31170 : RT_TRANSACTION_RELAXED));
31171 gcc_assert (token != NULL);
31173 if (keyword == RID_TRANSACTION_RELAXED)
31174 this_in |= TM_STMT_ATTR_RELAXED;
31175 else
31177 attrs = cp_parser_txn_attribute_opt (parser);
31178 if (attrs)
31179 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31182 /* Parse a noexcept specification. */
31183 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31185 /* Keep track if we're in the lexical scope of an outer transaction. */
31186 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31188 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31190 parser->in_transaction = new_in;
31191 cp_parser_compound_statement (parser, NULL, false, false);
31192 parser->in_transaction = old_in;
31194 finish_transaction_stmt (stmt, NULL, this_in, noex);
31196 return stmt;
31199 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31201 transaction-expression:
31202 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31203 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31206 static tree
31207 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31209 unsigned char old_in = parser->in_transaction;
31210 unsigned char this_in = 1;
31211 cp_token *token;
31212 tree expr, noex;
31213 bool noex_expr;
31215 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31216 || keyword == RID_TRANSACTION_RELAXED);
31218 if (!flag_tm)
31219 error (keyword == RID_TRANSACTION_RELAXED
31220 ? G_("%<__transaction_relaxed%> without transactional memory "
31221 "support enabled")
31222 : G_("%<__transaction_atomic%> without transactional memory "
31223 "support enabled"));
31225 token = cp_parser_require_keyword (parser, keyword,
31226 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31227 : RT_TRANSACTION_RELAXED));
31228 gcc_assert (token != NULL);
31230 if (keyword == RID_TRANSACTION_RELAXED)
31231 this_in |= TM_STMT_ATTR_RELAXED;
31233 /* Set this early. This might mean that we allow transaction_cancel in
31234 an expression that we find out later actually has to be a constexpr.
31235 However, we expect that cxx_constant_value will be able to deal with
31236 this; also, if the noexcept has no constexpr, then what we parse next
31237 really is a transaction's body. */
31238 parser->in_transaction = this_in;
31240 /* Parse a noexcept specification. */
31241 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31242 true);
31244 if (!noex || !noex_expr
31245 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31247 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31249 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31250 expr = finish_parenthesized_expr (expr);
31252 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31254 else
31256 /* The only expression that is available got parsed for the noexcept
31257 already. noexcept is true then. */
31258 expr = noex;
31259 noex = boolean_true_node;
31262 expr = build_transaction_expr (token->location, expr, this_in, noex);
31263 parser->in_transaction = old_in;
31265 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31266 return error_mark_node;
31268 return (flag_tm ? expr : error_mark_node);
31271 /* Parse a function-transaction-block.
31273 function-transaction-block:
31274 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31275 function-body
31276 __transaction_atomic txn-attribute[opt] function-try-block
31277 __transaction_relaxed ctor-initializer[opt] function-body
31278 __transaction_relaxed function-try-block
31281 static bool
31282 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31284 unsigned char old_in = parser->in_transaction;
31285 unsigned char new_in = 1;
31286 tree compound_stmt, stmt, attrs;
31287 bool ctor_initializer_p;
31288 cp_token *token;
31290 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31291 || keyword == RID_TRANSACTION_RELAXED);
31292 token = cp_parser_require_keyword (parser, keyword,
31293 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31294 : RT_TRANSACTION_RELAXED));
31295 gcc_assert (token != NULL);
31297 if (keyword == RID_TRANSACTION_RELAXED)
31298 new_in |= TM_STMT_ATTR_RELAXED;
31299 else
31301 attrs = cp_parser_txn_attribute_opt (parser);
31302 if (attrs)
31303 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31306 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31308 parser->in_transaction = new_in;
31310 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31311 ctor_initializer_p = cp_parser_function_try_block (parser);
31312 else
31313 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31314 (parser, /*in_function_try_block=*/false);
31316 parser->in_transaction = old_in;
31318 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31320 return ctor_initializer_p;
31323 /* Parse a __transaction_cancel statement.
31325 cancel-statement:
31326 __transaction_cancel txn-attribute[opt] ;
31327 __transaction_cancel txn-attribute[opt] throw-expression ;
31329 ??? Cancel and throw is not yet implemented. */
31331 static tree
31332 cp_parser_transaction_cancel (cp_parser *parser)
31334 cp_token *token;
31335 bool is_outer = false;
31336 tree stmt, attrs;
31338 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31339 RT_TRANSACTION_CANCEL);
31340 gcc_assert (token != NULL);
31342 attrs = cp_parser_txn_attribute_opt (parser);
31343 if (attrs)
31344 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31346 /* ??? Parse cancel-and-throw here. */
31348 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31350 if (!flag_tm)
31352 error_at (token->location, "%<__transaction_cancel%> without "
31353 "transactional memory support enabled");
31354 return error_mark_node;
31356 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31358 error_at (token->location, "%<__transaction_cancel%> within a "
31359 "%<__transaction_relaxed%>");
31360 return error_mark_node;
31362 else if (is_outer)
31364 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31365 && !is_tm_may_cancel_outer (current_function_decl))
31367 error_at (token->location, "outer %<__transaction_cancel%> not "
31368 "within outer %<__transaction_atomic%>");
31369 error_at (token->location,
31370 " or a %<transaction_may_cancel_outer%> function");
31371 return error_mark_node;
31374 else if (parser->in_transaction == 0)
31376 error_at (token->location, "%<__transaction_cancel%> not within "
31377 "%<__transaction_atomic%>");
31378 return error_mark_node;
31381 stmt = build_tm_abort_call (token->location, is_outer);
31382 add_stmt (stmt);
31384 return stmt;
31387 /* The parser. */
31389 static GTY (()) cp_parser *the_parser;
31392 /* Special handling for the first token or line in the file. The first
31393 thing in the file might be #pragma GCC pch_preprocess, which loads a
31394 PCH file, which is a GC collection point. So we need to handle this
31395 first pragma without benefit of an existing lexer structure.
31397 Always returns one token to the caller in *FIRST_TOKEN. This is
31398 either the true first token of the file, or the first token after
31399 the initial pragma. */
31401 static void
31402 cp_parser_initial_pragma (cp_token *first_token)
31404 tree name = NULL;
31406 cp_lexer_get_preprocessor_token (NULL, first_token);
31407 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31408 return;
31410 cp_lexer_get_preprocessor_token (NULL, first_token);
31411 if (first_token->type == CPP_STRING)
31413 name = first_token->u.value;
31415 cp_lexer_get_preprocessor_token (NULL, first_token);
31416 if (first_token->type != CPP_PRAGMA_EOL)
31417 error_at (first_token->location,
31418 "junk at end of %<#pragma GCC pch_preprocess%>");
31420 else
31421 error_at (first_token->location, "expected string literal");
31423 /* Skip to the end of the pragma. */
31424 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31425 cp_lexer_get_preprocessor_token (NULL, first_token);
31427 /* Now actually load the PCH file. */
31428 if (name)
31429 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31431 /* Read one more token to return to our caller. We have to do this
31432 after reading the PCH file in, since its pointers have to be
31433 live. */
31434 cp_lexer_get_preprocessor_token (NULL, first_token);
31437 /* Normal parsing of a pragma token. Here we can (and must) use the
31438 regular lexer. */
31440 static bool
31441 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31443 cp_token *pragma_tok;
31444 unsigned int id;
31446 pragma_tok = cp_lexer_consume_token (parser->lexer);
31447 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31448 parser->lexer->in_pragma = true;
31450 id = pragma_tok->pragma_kind;
31451 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31452 cp_ensure_no_omp_declare_simd (parser);
31453 switch (id)
31455 case PRAGMA_GCC_PCH_PREPROCESS:
31456 error_at (pragma_tok->location,
31457 "%<#pragma GCC pch_preprocess%> must be first");
31458 break;
31460 case PRAGMA_OMP_BARRIER:
31461 switch (context)
31463 case pragma_compound:
31464 cp_parser_omp_barrier (parser, pragma_tok);
31465 return false;
31466 case pragma_stmt:
31467 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31468 "used in compound statements");
31469 break;
31470 default:
31471 goto bad_stmt;
31473 break;
31475 case PRAGMA_OMP_FLUSH:
31476 switch (context)
31478 case pragma_compound:
31479 cp_parser_omp_flush (parser, pragma_tok);
31480 return false;
31481 case pragma_stmt:
31482 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31483 "used in compound statements");
31484 break;
31485 default:
31486 goto bad_stmt;
31488 break;
31490 case PRAGMA_OMP_TASKWAIT:
31491 switch (context)
31493 case pragma_compound:
31494 cp_parser_omp_taskwait (parser, pragma_tok);
31495 return false;
31496 case pragma_stmt:
31497 error_at (pragma_tok->location,
31498 "%<#pragma omp taskwait%> may only be "
31499 "used in compound statements");
31500 break;
31501 default:
31502 goto bad_stmt;
31504 break;
31506 case PRAGMA_OMP_TASKYIELD:
31507 switch (context)
31509 case pragma_compound:
31510 cp_parser_omp_taskyield (parser, pragma_tok);
31511 return false;
31512 case pragma_stmt:
31513 error_at (pragma_tok->location,
31514 "%<#pragma omp taskyield%> may only be "
31515 "used in compound statements");
31516 break;
31517 default:
31518 goto bad_stmt;
31520 break;
31522 case PRAGMA_OMP_CANCEL:
31523 switch (context)
31525 case pragma_compound:
31526 cp_parser_omp_cancel (parser, pragma_tok);
31527 return false;
31528 case pragma_stmt:
31529 error_at (pragma_tok->location,
31530 "%<#pragma omp cancel%> may only be "
31531 "used in compound statements");
31532 break;
31533 default:
31534 goto bad_stmt;
31536 break;
31538 case PRAGMA_OMP_CANCELLATION_POINT:
31539 switch (context)
31541 case pragma_compound:
31542 cp_parser_omp_cancellation_point (parser, pragma_tok);
31543 return false;
31544 case pragma_stmt:
31545 error_at (pragma_tok->location,
31546 "%<#pragma omp cancellation point%> may only be "
31547 "used in compound statements");
31548 break;
31549 default:
31550 goto bad_stmt;
31552 break;
31554 case PRAGMA_OMP_THREADPRIVATE:
31555 cp_parser_omp_threadprivate (parser, pragma_tok);
31556 return false;
31558 case PRAGMA_OMP_DECLARE_REDUCTION:
31559 cp_parser_omp_declare (parser, pragma_tok, context);
31560 return false;
31562 case PRAGMA_OMP_ATOMIC:
31563 case PRAGMA_OMP_CRITICAL:
31564 case PRAGMA_OMP_DISTRIBUTE:
31565 case PRAGMA_OMP_FOR:
31566 case PRAGMA_OMP_MASTER:
31567 case PRAGMA_OMP_ORDERED:
31568 case PRAGMA_OMP_PARALLEL:
31569 case PRAGMA_OMP_SECTIONS:
31570 case PRAGMA_OMP_SIMD:
31571 case PRAGMA_OMP_SINGLE:
31572 case PRAGMA_OMP_TASK:
31573 case PRAGMA_OMP_TASKGROUP:
31574 case PRAGMA_OMP_TEAMS:
31575 if (context != pragma_stmt && context != pragma_compound)
31576 goto bad_stmt;
31577 cp_parser_omp_construct (parser, pragma_tok);
31578 return true;
31580 case PRAGMA_OMP_TARGET:
31581 return cp_parser_omp_target (parser, pragma_tok, context);
31583 case PRAGMA_OMP_END_DECLARE_TARGET:
31584 cp_parser_omp_end_declare_target (parser, pragma_tok);
31585 return false;
31587 case PRAGMA_OMP_SECTION:
31588 error_at (pragma_tok->location,
31589 "%<#pragma omp section%> may only be used in "
31590 "%<#pragma omp sections%> construct");
31591 break;
31593 case PRAGMA_IVDEP:
31595 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31596 cp_token *tok;
31597 tok = cp_lexer_peek_token (the_parser->lexer);
31598 if (tok->type != CPP_KEYWORD
31599 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31600 && tok->keyword != RID_DO))
31602 cp_parser_error (parser, "for, while or do statement expected");
31603 return false;
31605 cp_parser_iteration_statement (parser, true);
31606 return true;
31609 case PRAGMA_CILK_SIMD:
31610 if (context == pragma_external)
31612 error_at (pragma_tok->location,
31613 "%<#pragma simd%> must be inside a function");
31614 break;
31616 cp_parser_cilk_simd (parser, pragma_tok);
31617 return true;
31619 default:
31620 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31621 c_invoke_pragma_handler (id);
31622 break;
31624 bad_stmt:
31625 cp_parser_error (parser, "expected declaration specifiers");
31626 break;
31629 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31630 return false;
31633 /* The interface the pragma parsers have to the lexer. */
31635 enum cpp_ttype
31636 pragma_lex (tree *value)
31638 cp_token *tok;
31639 enum cpp_ttype ret;
31641 tok = cp_lexer_peek_token (the_parser->lexer);
31643 ret = tok->type;
31644 *value = tok->u.value;
31646 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31647 ret = CPP_EOF;
31648 else if (ret == CPP_STRING)
31649 *value = cp_parser_string_literal (the_parser, false, false);
31650 else
31652 cp_lexer_consume_token (the_parser->lexer);
31653 if (ret == CPP_KEYWORD)
31654 ret = CPP_NAME;
31657 return ret;
31661 /* External interface. */
31663 /* Parse one entire translation unit. */
31665 void
31666 c_parse_file (void)
31669 the_parser = cp_parser_new ();
31670 push_deferring_access_checks (flag_access_control
31671 ? dk_no_deferred : dk_no_check);
31672 cp_parser_translation_unit (the_parser);
31673 the_parser = NULL;
31676 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31677 vectorlength clause:
31678 Syntax:
31679 vectorlength ( constant-expression ) */
31681 static tree
31682 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31683 bool is_simd_fn)
31685 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31686 tree expr;
31687 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31688 safelen clause. Thus, vectorlength is represented as OMP 4.0
31689 safelen. For SIMD-enabled function it is represented by OMP 4.0
31690 simdlen. */
31691 if (!is_simd_fn)
31692 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31693 loc);
31694 else
31695 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31696 loc);
31698 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31699 return error_mark_node;
31701 expr = cp_parser_constant_expression (parser, false, NULL);
31702 expr = maybe_constant_value (expr);
31704 /* If expr == error_mark_node, then don't emit any errors nor
31705 create a clause. if any of the above functions returns
31706 error mark node then they would have emitted an error message. */
31707 if (expr == error_mark_node)
31709 else if (!TREE_TYPE (expr)
31710 || !TREE_CONSTANT (expr)
31711 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31712 error_at (loc, "vectorlength must be an integer constant");
31713 else if (TREE_CONSTANT (expr)
31714 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31715 error_at (loc, "vectorlength must be a power of 2");
31716 else
31718 tree c;
31719 if (!is_simd_fn)
31721 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31722 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31723 OMP_CLAUSE_CHAIN (c) = clauses;
31724 clauses = c;
31726 else
31728 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
31729 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
31730 OMP_CLAUSE_CHAIN (c) = clauses;
31731 clauses = c;
31735 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31736 return error_mark_node;
31737 return clauses;
31740 /* Handles the Cilk Plus #pragma simd linear clause.
31741 Syntax:
31742 linear ( simd-linear-variable-list )
31744 simd-linear-variable-list:
31745 simd-linear-variable
31746 simd-linear-variable-list , simd-linear-variable
31748 simd-linear-variable:
31749 id-expression
31750 id-expression : simd-linear-step
31752 simd-linear-step:
31753 conditional-expression */
31755 static tree
31756 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31758 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31760 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31761 return clauses;
31762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31764 cp_parser_error (parser, "expected identifier");
31765 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31766 return error_mark_node;
31769 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31770 parser->colon_corrects_to_scope_p = false;
31771 while (1)
31773 cp_token *token = cp_lexer_peek_token (parser->lexer);
31774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31776 cp_parser_error (parser, "expected variable-name");
31777 clauses = error_mark_node;
31778 break;
31781 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
31782 false, false);
31783 tree decl = cp_parser_lookup_name_simple (parser, var_name,
31784 token->location);
31785 if (decl == error_mark_node)
31787 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
31788 token->location);
31789 clauses = error_mark_node;
31791 else
31793 tree e = NULL_TREE;
31794 tree step_size = integer_one_node;
31796 /* If present, parse the linear step. Otherwise, assume the default
31797 value of 1. */
31798 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
31800 cp_lexer_consume_token (parser->lexer);
31802 e = cp_parser_assignment_expression (parser, false, NULL);
31803 e = maybe_constant_value (e);
31805 if (e == error_mark_node)
31807 /* If an error has occurred, then the whole pragma is
31808 considered ill-formed. Thus, no reason to keep
31809 parsing. */
31810 clauses = error_mark_node;
31811 break;
31813 else if (type_dependent_expression_p (e)
31814 || value_dependent_expression_p (e)
31815 || (TREE_TYPE (e)
31816 && INTEGRAL_TYPE_P (TREE_TYPE (e))
31817 && (TREE_CONSTANT (e)
31818 || DECL_P (e))))
31819 step_size = e;
31820 else
31821 cp_parser_error (parser,
31822 "step size must be an integer constant "
31823 "expression or an integer variable");
31826 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31827 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31828 OMP_CLAUSE_DECL (l) = decl;
31829 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
31830 OMP_CLAUSE_CHAIN (l) = clauses;
31831 clauses = l;
31833 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31834 cp_lexer_consume_token (parser->lexer);
31835 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31836 break;
31837 else
31839 error_at (cp_lexer_peek_token (parser->lexer)->location,
31840 "expected %<,%> or %<)%> after %qE", decl);
31841 clauses = error_mark_node;
31842 break;
31845 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31846 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31847 return clauses;
31850 /* Returns the name of the next clause. If the clause is not
31851 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31852 token is not consumed. Otherwise, the appropriate enum from the
31853 pragma_simd_clause is returned and the token is consumed. */
31855 static pragma_omp_clause
31856 cp_parser_cilk_simd_clause_name (cp_parser *parser)
31858 pragma_omp_clause clause_type;
31859 cp_token *token = cp_lexer_peek_token (parser->lexer);
31861 if (token->keyword == RID_PRIVATE)
31862 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
31863 else if (!token->u.value || token->type != CPP_NAME)
31864 return PRAGMA_CILK_CLAUSE_NONE;
31865 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
31866 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31867 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
31868 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
31869 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
31870 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
31871 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
31872 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
31873 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
31874 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
31875 else
31876 return PRAGMA_CILK_CLAUSE_NONE;
31878 cp_lexer_consume_token (parser->lexer);
31879 return clause_type;
31882 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31884 static tree
31885 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
31887 tree clauses = NULL_TREE;
31889 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31890 && clauses != error_mark_node)
31892 pragma_omp_clause c_kind;
31893 c_kind = cp_parser_cilk_simd_clause_name (parser);
31894 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
31895 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
31896 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
31897 clauses = cp_parser_cilk_simd_linear (parser, clauses);
31898 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
31899 /* Use the OpenMP 4.0 equivalent function. */
31900 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
31901 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
31902 /* Use the OpenMP 4.0 equivalent function. */
31903 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31904 clauses);
31905 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
31906 /* Use the OMP 4.0 equivalent function. */
31907 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31908 clauses);
31909 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
31910 /* Use the OMP 4.0 equivalent function. */
31911 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31912 else
31914 clauses = error_mark_node;
31915 cp_parser_error (parser, "expected %<#pragma simd%> clause");
31916 break;
31920 cp_parser_skip_to_pragma_eol (parser, pragma_token);
31922 if (clauses == error_mark_node)
31923 return error_mark_node;
31924 else
31925 return c_finish_cilk_clauses (clauses);
31928 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31930 static void
31931 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
31933 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
31935 if (clauses == error_mark_node)
31936 return;
31938 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
31940 error_at (cp_lexer_peek_token (parser->lexer)->location,
31941 "for statement expected");
31942 return;
31945 tree sb = begin_omp_structured_block ();
31946 int save = cp_parser_begin_omp_structured_block (parser);
31947 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
31948 if (ret)
31949 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
31950 cp_parser_end_omp_structured_block (parser, save);
31951 add_stmt (finish_omp_structured_block (sb));
31952 return;
31955 /* Create an identifier for a generic parameter type (a synthesized
31956 template parameter implied by `auto' or a concept identifier). */
31958 static GTY(()) int generic_parm_count;
31959 static tree
31960 make_generic_type_name ()
31962 char buf[32];
31963 sprintf (buf, "auto:%d", ++generic_parm_count);
31964 return get_identifier (buf);
31967 /* Predicate that behaves as is_auto_or_concept but matches the parent
31968 node of the generic type rather than the generic type itself. This
31969 allows for type transformation in add_implicit_template_parms. */
31971 static inline bool
31972 tree_type_is_auto_or_concept (const_tree t)
31974 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
31977 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
31978 (creating a new template parameter list if necessary). Returns the newly
31979 created template type parm. */
31981 tree
31982 synthesize_implicit_template_parm (cp_parser *parser)
31984 gcc_assert (current_binding_level->kind == sk_function_parms);
31986 /* We are either continuing a function template that already contains implicit
31987 template parameters, creating a new fully-implicit function template, or
31988 extending an existing explicit function template with implicit template
31989 parameters. */
31991 cp_binding_level *const entry_scope = current_binding_level;
31993 bool become_template = false;
31994 cp_binding_level *parent_scope = 0;
31996 if (parser->implicit_template_scope)
31998 gcc_assert (parser->implicit_template_parms);
32000 current_binding_level = parser->implicit_template_scope;
32002 else
32004 /* Roll back to the existing template parameter scope (in the case of
32005 extending an explicit function template) or introduce a new template
32006 parameter scope ahead of the function parameter scope (or class scope
32007 in the case of out-of-line member definitions). The function scope is
32008 added back after template parameter synthesis below. */
32010 cp_binding_level *scope = entry_scope;
32012 while (scope->kind == sk_function_parms)
32014 parent_scope = scope;
32015 scope = scope->level_chain;
32017 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32019 /* If not defining a class, then any class scope is a scope level in
32020 an out-of-line member definition. In this case simply wind back
32021 beyond the first such scope to inject the template parameter list.
32022 Otherwise wind back to the class being defined. The latter can
32023 occur in class member friend declarations such as:
32025 class A {
32026 void foo (auto);
32028 class B {
32029 friend void A::foo (auto);
32032 The template parameter list synthesized for the friend declaration
32033 must be injected in the scope of 'B'. This can also occur in
32034 erroneous cases such as:
32036 struct A {
32037 struct B {
32038 void foo (auto);
32040 void B::foo (auto) {}
32043 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32044 but, nevertheless, the template parameter list synthesized for the
32045 declarator should be injected into the scope of 'A' as if the
32046 ill-formed template was specified explicitly. */
32048 while (scope->kind == sk_class && !scope->defining_class_p)
32050 parent_scope = scope;
32051 scope = scope->level_chain;
32055 current_binding_level = scope;
32057 if (scope->kind != sk_template_parms
32058 || !function_being_declared_is_template_p (parser))
32060 /* Introduce a new template parameter list for implicit template
32061 parameters. */
32063 become_template = true;
32065 parser->implicit_template_scope
32066 = begin_scope (sk_template_parms, NULL);
32068 ++processing_template_decl;
32070 parser->fully_implicit_function_template_p = true;
32071 ++parser->num_template_parameter_lists;
32073 else
32075 /* Synthesize implicit template parameters at the end of the explicit
32076 template parameter list. */
32078 gcc_assert (current_template_parms);
32080 parser->implicit_template_scope = scope;
32082 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32083 parser->implicit_template_parms
32084 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32088 /* Synthesize a new template parameter and track the current template
32089 parameter chain with implicit_template_parms. */
32091 tree synth_id = make_generic_type_name ();
32092 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32093 synth_id);
32094 tree new_parm
32095 = process_template_parm (parser->implicit_template_parms,
32096 input_location,
32097 build_tree_list (NULL_TREE, synth_tmpl_parm),
32098 /*non_type=*/false,
32099 /*param_pack=*/false);
32102 if (parser->implicit_template_parms)
32103 parser->implicit_template_parms
32104 = TREE_CHAIN (parser->implicit_template_parms);
32105 else
32106 parser->implicit_template_parms = new_parm;
32108 tree new_type = TREE_TYPE (getdecls ());
32110 /* If creating a fully implicit function template, start the new implicit
32111 template parameter list with this synthesized type, otherwise grow the
32112 current template parameter list. */
32114 if (become_template)
32116 parent_scope->level_chain = current_binding_level;
32118 tree new_parms = make_tree_vec (1);
32119 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32120 current_template_parms = tree_cons (size_int (processing_template_decl),
32121 new_parms, current_template_parms);
32123 else
32125 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32126 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32127 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32128 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32131 current_binding_level = entry_scope;
32133 return new_type;
32136 /* Finish the declaration of a fully implicit function template. Such a
32137 template has no explicit template parameter list so has not been through the
32138 normal template head and tail processing. synthesize_implicit_template_parm
32139 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32140 provided if the declaration is a class member such that its template
32141 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32142 form is returned. Otherwise NULL_TREE is returned. */
32144 tree
32145 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32147 gcc_assert (parser->fully_implicit_function_template_p);
32149 if (member_decl_opt && member_decl_opt != error_mark_node
32150 && DECL_VIRTUAL_P (member_decl_opt))
32152 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32153 "implicit templates may not be %<virtual%>");
32154 DECL_VIRTUAL_P (member_decl_opt) = false;
32157 if (member_decl_opt)
32158 member_decl_opt = finish_member_template_decl (member_decl_opt);
32159 end_template_decl ();
32161 parser->fully_implicit_function_template_p = false;
32162 --parser->num_template_parameter_lists;
32164 return member_decl_opt;
32167 #include "gt-cp-parser.h"