PR c/49706
[official-gcc.git] / gcc / cp / parser.c
blob60e6cda98a42a5d341db28683380831dd7e9bd2e
1 /* C++ Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 } non_integral_constant;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 } name_lookup_error;
130 /* The various kinds of required token */
131 typedef enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_INTERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 } required_token;
186 /* Prototypes. */
188 static cp_lexer *cp_lexer_new_main
189 (void);
190 static cp_lexer *cp_lexer_new_from_tokens
191 (cp_token_cache *tokens);
192 static void cp_lexer_destroy
193 (cp_lexer *);
194 static int cp_lexer_saving_tokens
195 (const cp_lexer *);
196 static cp_token *cp_lexer_token_at
197 (cp_lexer *, cp_token_position);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer *, cp_token *);
200 static inline cp_token *cp_lexer_peek_token
201 (cp_lexer *);
202 static cp_token *cp_lexer_peek_nth_token
203 (cp_lexer *, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer *, enum cpp_ttype);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer *, enum cpp_ttype);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer *, enum rid);
210 static cp_token *cp_lexer_consume_token
211 (cp_lexer *);
212 static void cp_lexer_purge_token
213 (cp_lexer *);
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_save_tokens
217 (cp_lexer *);
218 static void cp_lexer_commit_tokens
219 (cp_lexer *);
220 static void cp_lexer_rollback_tokens
221 (cp_lexer *);
222 static void cp_lexer_print_token
223 (FILE *, cp_token *);
224 static inline bool cp_lexer_debugging_p
225 (cp_lexer *);
226 static void cp_lexer_start_debugging
227 (cp_lexer *) ATTRIBUTE_UNUSED;
228 static void cp_lexer_stop_debugging
229 (cp_lexer *) ATTRIBUTE_UNUSED;
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
234 static void cp_parser_initial_pragma
235 (cp_token *);
237 static tree cp_literal_operator_id
238 (const char *);
240 static void cp_parser_cilk_simd
241 (cp_parser *, cp_token *);
242 static bool cp_parser_omp_declare_reduction_exprs
243 (tree, cp_parser *);
244 static tree cp_parser_cilk_simd_vectorlength
245 (cp_parser *, tree, bool);
247 /* Manifest constants. */
248 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
249 #define CP_SAVED_TOKEN_STACK 5
251 /* Variables. */
253 /* The stream to which debugging output should be written. */
254 static FILE *cp_lexer_debug_stream;
256 /* Nonzero if we are parsing an unevaluated operand: an operand to
257 sizeof, typeof, or alignof. */
258 int cp_unevaluated_operand;
260 /* Dump up to NUM tokens in BUFFER to FILE starting with token
261 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
262 first token in BUFFER. If NUM is 0, dump all the tokens. If
263 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
264 highlighted by surrounding it in [[ ]]. */
266 static void
267 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
268 cp_token *start_token, unsigned num,
269 cp_token *curr_token)
271 unsigned i, nprinted;
272 cp_token *token;
273 bool do_print;
275 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
277 if (buffer == NULL)
278 return;
280 if (num == 0)
281 num = buffer->length ();
283 if (start_token == NULL)
284 start_token = buffer->address ();
286 if (start_token > buffer->address ())
288 cp_lexer_print_token (file, &(*buffer)[0]);
289 fprintf (file, " ... ");
292 do_print = false;
293 nprinted = 0;
294 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
296 if (token == start_token)
297 do_print = true;
299 if (!do_print)
300 continue;
302 nprinted++;
303 if (token == curr_token)
304 fprintf (file, "[[");
306 cp_lexer_print_token (file, token);
308 if (token == curr_token)
309 fprintf (file, "]]");
311 switch (token->type)
313 case CPP_SEMICOLON:
314 case CPP_OPEN_BRACE:
315 case CPP_CLOSE_BRACE:
316 case CPP_EOF:
317 fputc ('\n', file);
318 break;
320 default:
321 fputc (' ', file);
325 if (i == num && i < buffer->length ())
327 fprintf (file, " ... ");
328 cp_lexer_print_token (file, &buffer->last ());
331 fprintf (file, "\n");
335 /* Dump all tokens in BUFFER to stderr. */
337 void
338 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
340 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
343 DEBUG_FUNCTION void
344 debug (vec<cp_token, va_gc> &ref)
346 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
349 DEBUG_FUNCTION void
350 debug (vec<cp_token, va_gc> *ptr)
352 if (ptr)
353 debug (*ptr);
354 else
355 fprintf (stderr, "<nil>\n");
359 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
360 description for T. */
362 static void
363 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
365 if (t)
367 fprintf (file, "%s: ", desc);
368 print_node_brief (file, "", t, 0);
373 /* Dump parser context C to FILE. */
375 static void
376 cp_debug_print_context (FILE *file, cp_parser_context *c)
378 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
379 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
380 print_node_brief (file, "", c->object_type, 0);
381 fprintf (file, "}\n");
385 /* Print the stack of parsing contexts to FILE starting with FIRST. */
387 static void
388 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
390 unsigned i;
391 cp_parser_context *c;
393 fprintf (file, "Parsing context stack:\n");
394 for (i = 0, c = first; c; c = c->next, i++)
396 fprintf (file, "\t#%u: ", i);
397 cp_debug_print_context (file, c);
402 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
404 static void
405 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
407 if (flag)
408 fprintf (file, "%s: true\n", desc);
412 /* Print an unparsed function entry UF to FILE. */
414 static void
415 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
417 unsigned i;
418 cp_default_arg_entry *default_arg_fn;
419 tree fn;
421 fprintf (file, "\tFunctions with default args:\n");
422 for (i = 0;
423 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
424 i++)
426 fprintf (file, "\t\tClass type: ");
427 print_node_brief (file, "", default_arg_fn->class_type, 0);
428 fprintf (file, "\t\tDeclaration: ");
429 print_node_brief (file, "", default_arg_fn->decl, 0);
430 fprintf (file, "\n");
433 fprintf (file, "\n\tFunctions with definitions that require "
434 "post-processing\n\t\t");
435 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
437 print_node_brief (file, "", fn, 0);
438 fprintf (file, " ");
440 fprintf (file, "\n");
442 fprintf (file, "\n\tNon-static data members with initializers that require "
443 "post-processing\n\t\t");
444 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
446 print_node_brief (file, "", fn, 0);
447 fprintf (file, " ");
449 fprintf (file, "\n");
453 /* Print the stack of unparsed member functions S to FILE. */
455 static void
456 cp_debug_print_unparsed_queues (FILE *file,
457 vec<cp_unparsed_functions_entry, va_gc> *s)
459 unsigned i;
460 cp_unparsed_functions_entry *uf;
462 fprintf (file, "Unparsed functions\n");
463 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
465 fprintf (file, "#%u:\n", i);
466 cp_debug_print_unparsed_function (file, uf);
471 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
472 the given PARSER. If FILE is NULL, the output is printed on stderr. */
474 static void
475 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
477 cp_token *next_token, *first_token, *start_token;
479 if (file == NULL)
480 file = stderr;
482 next_token = parser->lexer->next_token;
483 first_token = parser->lexer->buffer->address ();
484 start_token = (next_token > first_token + window_size / 2)
485 ? next_token - window_size / 2
486 : first_token;
487 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
488 next_token);
492 /* Dump debugging information for the given PARSER. If FILE is NULL,
493 the output is printed on stderr. */
495 void
496 cp_debug_parser (FILE *file, cp_parser *parser)
498 const size_t window_size = 20;
499 cp_token *token;
500 expanded_location eloc;
502 if (file == NULL)
503 file = stderr;
505 fprintf (file, "Parser state\n\n");
506 fprintf (file, "Number of tokens: %u\n",
507 vec_safe_length (parser->lexer->buffer));
508 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
509 cp_debug_print_tree_if_set (file, "Object scope",
510 parser->object_scope);
511 cp_debug_print_tree_if_set (file, "Qualifying scope",
512 parser->qualifying_scope);
513 cp_debug_print_context_stack (file, parser->context);
514 cp_debug_print_flag (file, "Allow GNU extensions",
515 parser->allow_gnu_extensions_p);
516 cp_debug_print_flag (file, "'>' token is greater-than",
517 parser->greater_than_is_operator_p);
518 cp_debug_print_flag (file, "Default args allowed in current "
519 "parameter list", parser->default_arg_ok_p);
520 cp_debug_print_flag (file, "Parsing integral constant-expression",
521 parser->integral_constant_expression_p);
522 cp_debug_print_flag (file, "Allow non-constant expression in current "
523 "constant-expression",
524 parser->allow_non_integral_constant_expression_p);
525 cp_debug_print_flag (file, "Seen non-constant expression",
526 parser->non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
528 "current context",
529 parser->local_variables_forbidden_p);
530 cp_debug_print_flag (file, "In unbraced linkage specification",
531 parser->in_unbraced_linkage_specification_p);
532 cp_debug_print_flag (file, "Parsing a declarator",
533 parser->in_declarator_p);
534 cp_debug_print_flag (file, "In template argument list",
535 parser->in_template_argument_list_p);
536 cp_debug_print_flag (file, "Parsing an iteration statement",
537 parser->in_statement & IN_ITERATION_STMT);
538 cp_debug_print_flag (file, "Parsing a switch statement",
539 parser->in_statement & IN_SWITCH_STMT);
540 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
541 parser->in_statement & IN_OMP_BLOCK);
542 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
543 parser->in_statement & IN_CILK_SIMD_FOR);
544 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
545 parser->in_statement & IN_OMP_FOR);
546 cp_debug_print_flag (file, "Parsing an if statement",
547 parser->in_statement & IN_IF_STMT);
548 cp_debug_print_flag (file, "Parsing a type-id in an expression "
549 "context", parser->in_type_id_in_expr_p);
550 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
551 parser->implicit_extern_c);
552 cp_debug_print_flag (file, "String expressions should be translated "
553 "to execution character set",
554 parser->translate_strings_p);
555 cp_debug_print_flag (file, "Parsing function body outside of a "
556 "local class", parser->in_function_body);
557 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
558 parser->colon_corrects_to_scope_p);
559 cp_debug_print_flag (file, "Colon doesn't start a class definition",
560 parser->colon_doesnt_start_class_def_p);
561 if (parser->type_definition_forbidden_message)
562 fprintf (file, "Error message for forbidden type definitions: %s\n",
563 parser->type_definition_forbidden_message);
564 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
565 fprintf (file, "Number of class definitions in progress: %u\n",
566 parser->num_classes_being_defined);
567 fprintf (file, "Number of template parameter lists for the current "
568 "declaration: %u\n", parser->num_template_parameter_lists);
569 cp_debug_parser_tokens (file, parser, window_size);
570 token = parser->lexer->next_token;
571 fprintf (file, "Next token to parse:\n");
572 fprintf (file, "\tToken: ");
573 cp_lexer_print_token (file, token);
574 eloc = expand_location (token->location);
575 fprintf (file, "\n\tFile: %s\n", eloc.file);
576 fprintf (file, "\tLine: %d\n", eloc.line);
577 fprintf (file, "\tColumn: %d\n", eloc.column);
580 DEBUG_FUNCTION void
581 debug (cp_parser &ref)
583 cp_debug_parser (stderr, &ref);
586 DEBUG_FUNCTION void
587 debug (cp_parser *ptr)
589 if (ptr)
590 debug (*ptr);
591 else
592 fprintf (stderr, "<nil>\n");
595 /* Allocate memory for a new lexer object and return it. */
597 static cp_lexer *
598 cp_lexer_alloc (void)
600 cp_lexer *lexer;
602 c_common_no_more_pch ();
604 /* Allocate the memory. */
605 lexer = ggc_cleared_alloc<cp_lexer> ();
607 /* Initially we are not debugging. */
608 lexer->debugging_p = false;
610 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
612 /* Create the buffer. */
613 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
615 return lexer;
619 /* Create a new main C++ lexer, the lexer that gets tokens from the
620 preprocessor. */
622 static cp_lexer *
623 cp_lexer_new_main (void)
625 cp_lexer *lexer;
626 cp_token token;
628 /* It's possible that parsing the first pragma will load a PCH file,
629 which is a GC collection point. So we have to do that before
630 allocating any memory. */
631 cp_parser_initial_pragma (&token);
633 lexer = cp_lexer_alloc ();
635 /* Put the first token in the buffer. */
636 lexer->buffer->quick_push (token);
638 /* Get the remaining tokens from the preprocessor. */
639 while (token.type != CPP_EOF)
641 cp_lexer_get_preprocessor_token (lexer, &token);
642 vec_safe_push (lexer->buffer, token);
645 lexer->last_token = lexer->buffer->address ()
646 + lexer->buffer->length ()
647 - 1;
648 lexer->next_token = lexer->buffer->length ()
649 ? lexer->buffer->address ()
650 : &eof_token;
652 /* Subsequent preprocessor diagnostics should use compiler
653 diagnostic functions to get the compiler source location. */
654 done_lexing = true;
656 gcc_assert (!lexer->next_token->purged_p);
657 return lexer;
660 /* Create a new lexer whose token stream is primed with the tokens in
661 CACHE. When these tokens are exhausted, no new tokens will be read. */
663 static cp_lexer *
664 cp_lexer_new_from_tokens (cp_token_cache *cache)
666 cp_token *first = cache->first;
667 cp_token *last = cache->last;
668 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
670 /* We do not own the buffer. */
671 lexer->buffer = NULL;
672 lexer->next_token = first == last ? &eof_token : first;
673 lexer->last_token = last;
675 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
677 /* Initially we are not debugging. */
678 lexer->debugging_p = false;
680 gcc_assert (!lexer->next_token->purged_p);
681 return lexer;
684 /* Frees all resources associated with LEXER. */
686 static void
687 cp_lexer_destroy (cp_lexer *lexer)
689 vec_free (lexer->buffer);
690 lexer->saved_tokens.release ();
691 ggc_free (lexer);
694 /* Returns nonzero if debugging information should be output. */
696 static inline bool
697 cp_lexer_debugging_p (cp_lexer *lexer)
699 return lexer->debugging_p;
703 static inline cp_token_position
704 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
706 gcc_assert (!previous_p || lexer->next_token != &eof_token);
708 return lexer->next_token - previous_p;
711 static inline cp_token *
712 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
714 return pos;
717 static inline void
718 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
720 lexer->next_token = cp_lexer_token_at (lexer, pos);
723 static inline cp_token_position
724 cp_lexer_previous_token_position (cp_lexer *lexer)
726 if (lexer->next_token == &eof_token)
727 return lexer->last_token - 1;
728 else
729 return cp_lexer_token_position (lexer, true);
732 static inline cp_token *
733 cp_lexer_previous_token (cp_lexer *lexer)
735 cp_token_position tp = cp_lexer_previous_token_position (lexer);
737 return cp_lexer_token_at (lexer, tp);
740 /* nonzero if we are presently saving tokens. */
742 static inline int
743 cp_lexer_saving_tokens (const cp_lexer* lexer)
745 return lexer->saved_tokens.length () != 0;
748 /* Store the next token from the preprocessor in *TOKEN. Return true
749 if we reach EOF. If LEXER is NULL, assume we are handling an
750 initial #pragma pch_preprocess, and thus want the lexer to return
751 processed strings. */
753 static void
754 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
756 static int is_extern_c = 0;
758 /* Get a new token from the preprocessor. */
759 token->type
760 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
761 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
762 token->keyword = RID_MAX;
763 token->pragma_kind = PRAGMA_NONE;
764 token->purged_p = false;
765 token->error_reported = false;
767 /* On some systems, some header files are surrounded by an
768 implicit extern "C" block. Set a flag in the token if it
769 comes from such a header. */
770 is_extern_c += pending_lang_change;
771 pending_lang_change = 0;
772 token->implicit_extern_c = is_extern_c > 0;
774 /* Check to see if this token is a keyword. */
775 if (token->type == CPP_NAME)
777 if (C_IS_RESERVED_WORD (token->u.value))
779 /* Mark this token as a keyword. */
780 token->type = CPP_KEYWORD;
781 /* Record which keyword. */
782 token->keyword = C_RID_CODE (token->u.value);
784 else
786 if (warn_cxx0x_compat
787 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
788 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
790 /* Warn about the C++0x keyword (but still treat it as
791 an identifier). */
792 warning (OPT_Wc__0x_compat,
793 "identifier %qE is a keyword in C++11",
794 token->u.value);
796 /* Clear out the C_RID_CODE so we don't warn about this
797 particular identifier-turned-keyword again. */
798 C_SET_RID_CODE (token->u.value, RID_MAX);
801 token->keyword = RID_MAX;
804 else if (token->type == CPP_AT_NAME)
806 /* This only happens in Objective-C++; it must be a keyword. */
807 token->type = CPP_KEYWORD;
808 switch (C_RID_CODE (token->u.value))
810 /* Replace 'class' with '@class', 'private' with '@private',
811 etc. This prevents confusion with the C++ keyword
812 'class', and makes the tokens consistent with other
813 Objective-C 'AT' keywords. For example '@class' is
814 reported as RID_AT_CLASS which is consistent with
815 '@synchronized', which is reported as
816 RID_AT_SYNCHRONIZED.
818 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
819 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
820 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
821 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
822 case RID_THROW: token->keyword = RID_AT_THROW; break;
823 case RID_TRY: token->keyword = RID_AT_TRY; break;
824 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
825 default: token->keyword = C_RID_CODE (token->u.value);
828 else if (token->type == CPP_PRAGMA)
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token->pragma_kind = ((enum pragma_kind)
832 TREE_INT_CST_LOW (token->u.value));
833 token->u.value = NULL_TREE;
837 /* Update the globals input_location and the input file stack from TOKEN. */
838 static inline void
839 cp_lexer_set_source_position_from_token (cp_token *token)
841 if (token->type != CPP_EOF)
843 input_location = token->location;
847 /* Update the globals input_location and the input file stack from LEXER. */
848 static inline void
849 cp_lexer_set_source_position (cp_lexer *lexer)
851 cp_token *token = cp_lexer_peek_token (lexer);
852 cp_lexer_set_source_position_from_token (token);
855 /* Return a pointer to the next token in the token stream, but do not
856 consume it. */
858 static inline cp_token *
859 cp_lexer_peek_token (cp_lexer *lexer)
861 if (cp_lexer_debugging_p (lexer))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865 putc ('\n', cp_lexer_debug_stream);
867 return lexer->next_token;
870 /* Return true if the next token has the indicated TYPE. */
872 static inline bool
873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
875 return cp_lexer_peek_token (lexer)->type == type;
878 /* Return true if the next token does not have the indicated TYPE. */
880 static inline bool
881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
883 return !cp_lexer_next_token_is (lexer, type);
886 /* Return true if the next token is the indicated KEYWORD. */
888 static inline bool
889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
891 return cp_lexer_peek_token (lexer)->keyword == keyword;
894 static inline bool
895 cp_lexer_nth_token_is_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_cleared_alloc<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
1851 #define unparsed_classes \
1852 parser->unparsed_queues->last ().classes
1854 static void
1855 push_unparsed_function_queues (cp_parser *parser)
1857 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1858 vec_safe_push (parser->unparsed_queues, e);
1861 static void
1862 pop_unparsed_function_queues (cp_parser *parser)
1864 release_tree_vector (unparsed_funs_with_definitions);
1865 parser->unparsed_queues->pop ();
1868 /* Prototypes. */
1870 /* Constructors and destructors. */
1872 static cp_parser *cp_parser_new
1873 (void);
1875 /* Routines to parse various constructs.
1877 Those that return `tree' will return the error_mark_node (rather
1878 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1879 Sometimes, they will return an ordinary node if error-recovery was
1880 attempted, even though a parse error occurred. So, to check
1881 whether or not a parse error occurred, you should always use
1882 cp_parser_error_occurred. If the construct is optional (indicated
1883 either by an `_opt' in the name of the function that does the
1884 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1885 the construct is not present. */
1887 /* Lexical conventions [gram.lex] */
1889 static tree cp_parser_identifier
1890 (cp_parser *);
1891 static tree cp_parser_string_literal
1892 (cp_parser *, bool, bool);
1893 static tree cp_parser_userdef_char_literal
1894 (cp_parser *);
1895 static tree cp_parser_userdef_string_literal
1896 (cp_token *);
1897 static tree cp_parser_userdef_numeric_literal
1898 (cp_parser *);
1900 /* Basic concepts [gram.basic] */
1902 static bool cp_parser_translation_unit
1903 (cp_parser *);
1905 /* Expressions [gram.expr] */
1907 static tree cp_parser_primary_expression
1908 (cp_parser *, bool, bool, bool, cp_id_kind *);
1909 static tree cp_parser_id_expression
1910 (cp_parser *, bool, bool, bool *, bool, bool);
1911 static tree cp_parser_unqualified_id
1912 (cp_parser *, bool, bool, bool, bool);
1913 static tree cp_parser_nested_name_specifier_opt
1914 (cp_parser *, bool, bool, bool, bool);
1915 static tree cp_parser_nested_name_specifier
1916 (cp_parser *, bool, bool, bool, bool);
1917 static tree cp_parser_qualifying_entity
1918 (cp_parser *, bool, bool, bool, bool, bool);
1919 static tree cp_parser_postfix_expression
1920 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1921 static tree cp_parser_postfix_open_square_expression
1922 (cp_parser *, tree, bool, bool);
1923 static tree cp_parser_postfix_dot_deref_expression
1924 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1925 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1926 (cp_parser *, int, bool, bool, bool *);
1927 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1928 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1929 static void cp_parser_pseudo_destructor_name
1930 (cp_parser *, tree, tree *, tree *);
1931 static tree cp_parser_unary_expression
1932 (cp_parser *, bool, bool, cp_id_kind *);
1933 static enum tree_code cp_parser_unary_operator
1934 (cp_token *);
1935 static tree cp_parser_new_expression
1936 (cp_parser *);
1937 static vec<tree, va_gc> *cp_parser_new_placement
1938 (cp_parser *);
1939 static tree cp_parser_new_type_id
1940 (cp_parser *, tree *);
1941 static cp_declarator *cp_parser_new_declarator_opt
1942 (cp_parser *);
1943 static cp_declarator *cp_parser_direct_new_declarator
1944 (cp_parser *);
1945 static vec<tree, va_gc> *cp_parser_new_initializer
1946 (cp_parser *);
1947 static tree cp_parser_delete_expression
1948 (cp_parser *);
1949 static tree cp_parser_cast_expression
1950 (cp_parser *, bool, bool, bool, cp_id_kind *);
1951 static tree cp_parser_binary_expression
1952 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1953 static tree cp_parser_question_colon_clause
1954 (cp_parser *, tree);
1955 static tree cp_parser_assignment_expression
1956 (cp_parser *, bool, cp_id_kind *);
1957 static enum tree_code cp_parser_assignment_operator_opt
1958 (cp_parser *);
1959 static tree cp_parser_expression
1960 (cp_parser *, bool, cp_id_kind *);
1961 static tree cp_parser_expression
1962 (cp_parser *, bool, bool, cp_id_kind *);
1963 static tree cp_parser_constant_expression
1964 (cp_parser *, bool, bool *);
1965 static tree cp_parser_builtin_offsetof
1966 (cp_parser *);
1967 static tree cp_parser_lambda_expression
1968 (cp_parser *);
1969 static void cp_parser_lambda_introducer
1970 (cp_parser *, tree);
1971 static bool cp_parser_lambda_declarator_opt
1972 (cp_parser *, tree);
1973 static void cp_parser_lambda_body
1974 (cp_parser *, tree);
1976 /* Statements [gram.stmt.stmt] */
1978 static void cp_parser_statement
1979 (cp_parser *, tree, bool, bool *);
1980 static void cp_parser_label_for_labeled_statement
1981 (cp_parser *, tree);
1982 static tree cp_parser_expression_statement
1983 (cp_parser *, tree);
1984 static tree cp_parser_compound_statement
1985 (cp_parser *, tree, bool, bool);
1986 static void cp_parser_statement_seq_opt
1987 (cp_parser *, tree);
1988 static tree cp_parser_selection_statement
1989 (cp_parser *, bool *);
1990 static tree cp_parser_condition
1991 (cp_parser *);
1992 static tree cp_parser_iteration_statement
1993 (cp_parser *, bool);
1994 static bool cp_parser_for_init_statement
1995 (cp_parser *, tree *decl);
1996 static tree cp_parser_for
1997 (cp_parser *, bool);
1998 static tree cp_parser_c_for
1999 (cp_parser *, tree, tree, bool);
2000 static tree cp_parser_range_for
2001 (cp_parser *, tree, tree, tree, bool);
2002 static void do_range_for_auto_deduction
2003 (tree, tree);
2004 static tree cp_parser_perform_range_for_lookup
2005 (tree, tree *, tree *);
2006 static tree cp_parser_range_for_member_function
2007 (tree, tree);
2008 static tree cp_parser_jump_statement
2009 (cp_parser *);
2010 static void cp_parser_declaration_statement
2011 (cp_parser *);
2013 static tree cp_parser_implicitly_scoped_statement
2014 (cp_parser *, bool *);
2015 static void cp_parser_already_scoped_statement
2016 (cp_parser *);
2018 /* Declarations [gram.dcl.dcl] */
2020 static void cp_parser_declaration_seq_opt
2021 (cp_parser *);
2022 static void cp_parser_declaration
2023 (cp_parser *);
2024 static void cp_parser_block_declaration
2025 (cp_parser *, bool);
2026 static void cp_parser_simple_declaration
2027 (cp_parser *, bool, tree *);
2028 static void cp_parser_decl_specifier_seq
2029 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2030 static tree cp_parser_storage_class_specifier_opt
2031 (cp_parser *);
2032 static tree cp_parser_function_specifier_opt
2033 (cp_parser *, cp_decl_specifier_seq *);
2034 static tree cp_parser_type_specifier
2035 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2036 int *, bool *);
2037 static tree cp_parser_simple_type_specifier
2038 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2039 static tree cp_parser_type_name
2040 (cp_parser *);
2041 static tree cp_parser_nonclass_name
2042 (cp_parser* parser);
2043 static tree cp_parser_elaborated_type_specifier
2044 (cp_parser *, bool, bool);
2045 static tree cp_parser_enum_specifier
2046 (cp_parser *);
2047 static void cp_parser_enumerator_list
2048 (cp_parser *, tree);
2049 static void cp_parser_enumerator_definition
2050 (cp_parser *, tree);
2051 static tree cp_parser_namespace_name
2052 (cp_parser *);
2053 static void cp_parser_namespace_definition
2054 (cp_parser *);
2055 static void cp_parser_namespace_body
2056 (cp_parser *);
2057 static tree cp_parser_qualified_namespace_specifier
2058 (cp_parser *);
2059 static void cp_parser_namespace_alias_definition
2060 (cp_parser *);
2061 static bool cp_parser_using_declaration
2062 (cp_parser *, bool);
2063 static void cp_parser_using_directive
2064 (cp_parser *);
2065 static tree cp_parser_alias_declaration
2066 (cp_parser *);
2067 static void cp_parser_asm_definition
2068 (cp_parser *);
2069 static void cp_parser_linkage_specification
2070 (cp_parser *);
2071 static void cp_parser_static_assert
2072 (cp_parser *, bool);
2073 static tree cp_parser_decltype
2074 (cp_parser *);
2076 /* Declarators [gram.dcl.decl] */
2078 static tree cp_parser_init_declarator
2079 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2080 static cp_declarator *cp_parser_declarator
2081 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2082 static cp_declarator *cp_parser_direct_declarator
2083 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2084 static enum tree_code cp_parser_ptr_operator
2085 (cp_parser *, tree *, cp_cv_quals *, tree *);
2086 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2087 (cp_parser *);
2088 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2089 (cp_parser *);
2090 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2091 (cp_parser *);
2092 static tree cp_parser_late_return_type_opt
2093 (cp_parser *, cp_declarator *, cp_cv_quals);
2094 static tree cp_parser_declarator_id
2095 (cp_parser *, bool);
2096 static tree cp_parser_type_id
2097 (cp_parser *);
2098 static tree cp_parser_template_type_arg
2099 (cp_parser *);
2100 static tree cp_parser_trailing_type_id (cp_parser *);
2101 static tree cp_parser_type_id_1
2102 (cp_parser *, bool, bool);
2103 static void cp_parser_type_specifier_seq
2104 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2105 static tree cp_parser_parameter_declaration_clause
2106 (cp_parser *);
2107 static tree cp_parser_parameter_declaration_list
2108 (cp_parser *, bool *);
2109 static cp_parameter_declarator *cp_parser_parameter_declaration
2110 (cp_parser *, bool, bool *);
2111 static tree cp_parser_default_argument
2112 (cp_parser *, bool);
2113 static void cp_parser_function_body
2114 (cp_parser *, bool);
2115 static tree cp_parser_initializer
2116 (cp_parser *, bool *, bool *);
2117 static tree cp_parser_initializer_clause
2118 (cp_parser *, bool *);
2119 static tree cp_parser_braced_list
2120 (cp_parser*, bool*);
2121 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2122 (cp_parser *, bool *);
2124 static bool cp_parser_ctor_initializer_opt_and_function_body
2125 (cp_parser *, bool);
2127 static tree cp_parser_late_parsing_omp_declare_simd
2128 (cp_parser *, tree);
2130 static tree cp_parser_late_parsing_cilk_simd_fn_info
2131 (cp_parser *, tree);
2133 static tree synthesize_implicit_template_parm
2134 (cp_parser *);
2135 static tree finish_fully_implicit_template
2136 (cp_parser *, tree);
2138 /* Classes [gram.class] */
2140 static tree cp_parser_class_name
2141 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2142 static tree cp_parser_class_specifier
2143 (cp_parser *);
2144 static tree cp_parser_class_head
2145 (cp_parser *, bool *);
2146 static enum tag_types cp_parser_class_key
2147 (cp_parser *);
2148 static void cp_parser_member_specification_opt
2149 (cp_parser *);
2150 static void cp_parser_member_declaration
2151 (cp_parser *);
2152 static tree cp_parser_pure_specifier
2153 (cp_parser *);
2154 static tree cp_parser_constant_initializer
2155 (cp_parser *);
2157 /* Derived classes [gram.class.derived] */
2159 static tree cp_parser_base_clause
2160 (cp_parser *);
2161 static tree cp_parser_base_specifier
2162 (cp_parser *);
2164 /* Special member functions [gram.special] */
2166 static tree cp_parser_conversion_function_id
2167 (cp_parser *);
2168 static tree cp_parser_conversion_type_id
2169 (cp_parser *);
2170 static cp_declarator *cp_parser_conversion_declarator_opt
2171 (cp_parser *);
2172 static bool cp_parser_ctor_initializer_opt
2173 (cp_parser *);
2174 static void cp_parser_mem_initializer_list
2175 (cp_parser *);
2176 static tree cp_parser_mem_initializer
2177 (cp_parser *);
2178 static tree cp_parser_mem_initializer_id
2179 (cp_parser *);
2181 /* Overloading [gram.over] */
2183 static tree cp_parser_operator_function_id
2184 (cp_parser *);
2185 static tree cp_parser_operator
2186 (cp_parser *);
2188 /* Templates [gram.temp] */
2190 static void cp_parser_template_declaration
2191 (cp_parser *, bool);
2192 static tree cp_parser_template_parameter_list
2193 (cp_parser *);
2194 static tree cp_parser_template_parameter
2195 (cp_parser *, bool *, bool *);
2196 static tree cp_parser_type_parameter
2197 (cp_parser *, bool *);
2198 static tree cp_parser_template_id
2199 (cp_parser *, bool, bool, enum tag_types, bool);
2200 static tree cp_parser_template_name
2201 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2202 static tree cp_parser_template_argument_list
2203 (cp_parser *);
2204 static tree cp_parser_template_argument
2205 (cp_parser *);
2206 static void cp_parser_explicit_instantiation
2207 (cp_parser *);
2208 static void cp_parser_explicit_specialization
2209 (cp_parser *);
2211 /* Exception handling [gram.exception] */
2213 static tree cp_parser_try_block
2214 (cp_parser *);
2215 static bool cp_parser_function_try_block
2216 (cp_parser *);
2217 static void cp_parser_handler_seq
2218 (cp_parser *);
2219 static void cp_parser_handler
2220 (cp_parser *);
2221 static tree cp_parser_exception_declaration
2222 (cp_parser *);
2223 static tree cp_parser_throw_expression
2224 (cp_parser *);
2225 static tree cp_parser_exception_specification_opt
2226 (cp_parser *);
2227 static tree cp_parser_type_id_list
2228 (cp_parser *);
2230 /* GNU Extensions */
2232 static tree cp_parser_asm_specification_opt
2233 (cp_parser *);
2234 static tree cp_parser_asm_operand_list
2235 (cp_parser *);
2236 static tree cp_parser_asm_clobber_list
2237 (cp_parser *);
2238 static tree cp_parser_asm_label_list
2239 (cp_parser *);
2240 static bool cp_next_tokens_can_be_attribute_p
2241 (cp_parser *);
2242 static bool cp_next_tokens_can_be_gnu_attribute_p
2243 (cp_parser *);
2244 static bool cp_next_tokens_can_be_std_attribute_p
2245 (cp_parser *);
2246 static bool cp_nth_tokens_can_be_std_attribute_p
2247 (cp_parser *, size_t);
2248 static bool cp_nth_tokens_can_be_gnu_attribute_p
2249 (cp_parser *, size_t);
2250 static bool cp_nth_tokens_can_be_attribute_p
2251 (cp_parser *, size_t);
2252 static tree cp_parser_attributes_opt
2253 (cp_parser *);
2254 static tree cp_parser_gnu_attributes_opt
2255 (cp_parser *);
2256 static tree cp_parser_gnu_attribute_list
2257 (cp_parser *);
2258 static tree cp_parser_std_attribute
2259 (cp_parser *);
2260 static tree cp_parser_std_attribute_spec
2261 (cp_parser *);
2262 static tree cp_parser_std_attribute_spec_seq
2263 (cp_parser *);
2264 static bool cp_parser_extension_opt
2265 (cp_parser *, int *);
2266 static void cp_parser_label_declaration
2267 (cp_parser *);
2269 /* Transactional Memory Extensions */
2271 static tree cp_parser_transaction
2272 (cp_parser *, enum rid);
2273 static tree cp_parser_transaction_expression
2274 (cp_parser *, enum rid);
2275 static bool cp_parser_function_transaction
2276 (cp_parser *, enum rid);
2277 static tree cp_parser_transaction_cancel
2278 (cp_parser *);
2280 enum pragma_context {
2281 pragma_external,
2282 pragma_member,
2283 pragma_objc_icode,
2284 pragma_stmt,
2285 pragma_compound
2287 static bool cp_parser_pragma
2288 (cp_parser *, enum pragma_context);
2290 /* Objective-C++ Productions */
2292 static tree cp_parser_objc_message_receiver
2293 (cp_parser *);
2294 static tree cp_parser_objc_message_args
2295 (cp_parser *);
2296 static tree cp_parser_objc_message_expression
2297 (cp_parser *);
2298 static tree cp_parser_objc_encode_expression
2299 (cp_parser *);
2300 static tree cp_parser_objc_defs_expression
2301 (cp_parser *);
2302 static tree cp_parser_objc_protocol_expression
2303 (cp_parser *);
2304 static tree cp_parser_objc_selector_expression
2305 (cp_parser *);
2306 static tree cp_parser_objc_expression
2307 (cp_parser *);
2308 static bool cp_parser_objc_selector_p
2309 (enum cpp_ttype);
2310 static tree cp_parser_objc_selector
2311 (cp_parser *);
2312 static tree cp_parser_objc_protocol_refs_opt
2313 (cp_parser *);
2314 static void cp_parser_objc_declaration
2315 (cp_parser *, tree);
2316 static tree cp_parser_objc_statement
2317 (cp_parser *);
2318 static bool cp_parser_objc_valid_prefix_attributes
2319 (cp_parser *, tree *);
2320 static void cp_parser_objc_at_property_declaration
2321 (cp_parser *) ;
2322 static void cp_parser_objc_at_synthesize_declaration
2323 (cp_parser *) ;
2324 static void cp_parser_objc_at_dynamic_declaration
2325 (cp_parser *) ;
2326 static tree cp_parser_objc_struct_declaration
2327 (cp_parser *) ;
2329 /* Utility Routines */
2331 static tree cp_parser_lookup_name
2332 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2333 static tree cp_parser_lookup_name_simple
2334 (cp_parser *, tree, location_t);
2335 static tree cp_parser_maybe_treat_template_as_class
2336 (tree, bool);
2337 static bool cp_parser_check_declarator_template_parameters
2338 (cp_parser *, cp_declarator *, location_t);
2339 static bool cp_parser_check_template_parameters
2340 (cp_parser *, unsigned, location_t, cp_declarator *);
2341 static tree cp_parser_simple_cast_expression
2342 (cp_parser *);
2343 static tree cp_parser_global_scope_opt
2344 (cp_parser *, bool);
2345 static bool cp_parser_constructor_declarator_p
2346 (cp_parser *, bool);
2347 static tree cp_parser_function_definition_from_specifiers_and_declarator
2348 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2349 static tree cp_parser_function_definition_after_declarator
2350 (cp_parser *, bool);
2351 static void cp_parser_template_declaration_after_export
2352 (cp_parser *, bool);
2353 static void cp_parser_perform_template_parameter_access_checks
2354 (vec<deferred_access_check, va_gc> *);
2355 static tree cp_parser_single_declaration
2356 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2357 static tree cp_parser_functional_cast
2358 (cp_parser *, tree);
2359 static tree cp_parser_save_member_function_body
2360 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2361 static tree cp_parser_save_nsdmi
2362 (cp_parser *);
2363 static tree cp_parser_enclosed_template_argument_list
2364 (cp_parser *);
2365 static void cp_parser_save_default_args
2366 (cp_parser *, tree);
2367 static void cp_parser_late_parsing_for_member
2368 (cp_parser *, tree);
2369 static tree cp_parser_late_parse_one_default_arg
2370 (cp_parser *, tree, tree, tree);
2371 static void cp_parser_late_parsing_nsdmi
2372 (cp_parser *, tree);
2373 static void cp_parser_late_parsing_default_args
2374 (cp_parser *, tree);
2375 static tree cp_parser_sizeof_operand
2376 (cp_parser *, enum rid);
2377 static tree cp_parser_trait_expr
2378 (cp_parser *, enum rid);
2379 static bool cp_parser_declares_only_class_p
2380 (cp_parser *);
2381 static void cp_parser_set_storage_class
2382 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2383 static void cp_parser_set_decl_spec_type
2384 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2385 static void set_and_check_decl_spec_loc
2386 (cp_decl_specifier_seq *decl_specs,
2387 cp_decl_spec ds, cp_token *);
2388 static bool cp_parser_friend_p
2389 (const cp_decl_specifier_seq *);
2390 static void cp_parser_required_error
2391 (cp_parser *, required_token, bool);
2392 static cp_token *cp_parser_require
2393 (cp_parser *, enum cpp_ttype, required_token);
2394 static cp_token *cp_parser_require_keyword
2395 (cp_parser *, enum rid, required_token);
2396 static bool cp_parser_token_starts_function_definition_p
2397 (cp_token *);
2398 static bool cp_parser_next_token_starts_class_definition_p
2399 (cp_parser *);
2400 static bool cp_parser_next_token_ends_template_argument_p
2401 (cp_parser *);
2402 static bool cp_parser_nth_token_starts_template_argument_list_p
2403 (cp_parser *, size_t);
2404 static enum tag_types cp_parser_token_is_class_key
2405 (cp_token *);
2406 static void cp_parser_check_class_key
2407 (enum tag_types, tree type);
2408 static void cp_parser_check_access_in_redeclaration
2409 (tree type, location_t location);
2410 static bool cp_parser_optional_template_keyword
2411 (cp_parser *);
2412 static void cp_parser_pre_parsed_nested_name_specifier
2413 (cp_parser *);
2414 static bool cp_parser_cache_group
2415 (cp_parser *, enum cpp_ttype, unsigned);
2416 static tree cp_parser_cache_defarg
2417 (cp_parser *parser, bool nsdmi);
2418 static void cp_parser_parse_tentatively
2419 (cp_parser *);
2420 static void cp_parser_commit_to_tentative_parse
2421 (cp_parser *);
2422 static void cp_parser_commit_to_topmost_tentative_parse
2423 (cp_parser *);
2424 static void cp_parser_abort_tentative_parse
2425 (cp_parser *);
2426 static bool cp_parser_parse_definitely
2427 (cp_parser *);
2428 static inline bool cp_parser_parsing_tentatively
2429 (cp_parser *);
2430 static bool cp_parser_uncommitted_to_tentative_parse_p
2431 (cp_parser *);
2432 static void cp_parser_error
2433 (cp_parser *, const char *);
2434 static void cp_parser_name_lookup_error
2435 (cp_parser *, tree, tree, name_lookup_error, location_t);
2436 static bool cp_parser_simulate_error
2437 (cp_parser *);
2438 static bool cp_parser_check_type_definition
2439 (cp_parser *);
2440 static void cp_parser_check_for_definition_in_return_type
2441 (cp_declarator *, tree, location_t type_location);
2442 static void cp_parser_check_for_invalid_template_id
2443 (cp_parser *, tree, enum tag_types, location_t location);
2444 static bool cp_parser_non_integral_constant_expression
2445 (cp_parser *, non_integral_constant);
2446 static void cp_parser_diagnose_invalid_type_name
2447 (cp_parser *, tree, tree, location_t);
2448 static bool cp_parser_parse_and_diagnose_invalid_type_name
2449 (cp_parser *);
2450 static int cp_parser_skip_to_closing_parenthesis
2451 (cp_parser *, bool, bool, bool);
2452 static void cp_parser_skip_to_end_of_statement
2453 (cp_parser *);
2454 static void cp_parser_consume_semicolon_at_end_of_statement
2455 (cp_parser *);
2456 static void cp_parser_skip_to_end_of_block_or_statement
2457 (cp_parser *);
2458 static bool cp_parser_skip_to_closing_brace
2459 (cp_parser *);
2460 static void cp_parser_skip_to_end_of_template_parameter_list
2461 (cp_parser *);
2462 static void cp_parser_skip_to_pragma_eol
2463 (cp_parser*, cp_token *);
2464 static bool cp_parser_error_occurred
2465 (cp_parser *);
2466 static bool cp_parser_allow_gnu_extensions_p
2467 (cp_parser *);
2468 static bool cp_parser_is_pure_string_literal
2469 (cp_token *);
2470 static bool cp_parser_is_string_literal
2471 (cp_token *);
2472 static bool cp_parser_is_keyword
2473 (cp_token *, enum rid);
2474 static tree cp_parser_make_typename_type
2475 (cp_parser *, tree, tree, location_t location);
2476 static cp_declarator * cp_parser_make_indirect_declarator
2477 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2479 /* Returns nonzero if we are parsing tentatively. */
2481 static inline bool
2482 cp_parser_parsing_tentatively (cp_parser* parser)
2484 return parser->context->next != NULL;
2487 /* Returns nonzero if TOKEN is a string literal. */
2489 static bool
2490 cp_parser_is_pure_string_literal (cp_token* token)
2492 return (token->type == CPP_STRING ||
2493 token->type == CPP_STRING16 ||
2494 token->type == CPP_STRING32 ||
2495 token->type == CPP_WSTRING ||
2496 token->type == CPP_UTF8STRING);
2499 /* Returns nonzero if TOKEN is a string literal
2500 of a user-defined string literal. */
2502 static bool
2503 cp_parser_is_string_literal (cp_token* token)
2505 return (cp_parser_is_pure_string_literal (token) ||
2506 token->type == CPP_STRING_USERDEF ||
2507 token->type == CPP_STRING16_USERDEF ||
2508 token->type == CPP_STRING32_USERDEF ||
2509 token->type == CPP_WSTRING_USERDEF ||
2510 token->type == CPP_UTF8STRING_USERDEF);
2513 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2515 static bool
2516 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2518 return token->keyword == keyword;
2521 /* If not parsing tentatively, issue a diagnostic of the form
2522 FILE:LINE: MESSAGE before TOKEN
2523 where TOKEN is the next token in the input stream. MESSAGE
2524 (specified by the caller) is usually of the form "expected
2525 OTHER-TOKEN". */
2527 static void
2528 cp_parser_error (cp_parser* parser, const char* gmsgid)
2530 if (!cp_parser_simulate_error (parser))
2532 cp_token *token = cp_lexer_peek_token (parser->lexer);
2533 /* This diagnostic makes more sense if it is tagged to the line
2534 of the token we just peeked at. */
2535 cp_lexer_set_source_position_from_token (token);
2537 if (token->type == CPP_PRAGMA)
2539 error_at (token->location,
2540 "%<#pragma%> is not allowed here");
2541 cp_parser_skip_to_pragma_eol (parser, token);
2542 return;
2545 c_parse_error (gmsgid,
2546 /* Because c_parser_error does not understand
2547 CPP_KEYWORD, keywords are treated like
2548 identifiers. */
2549 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2550 token->u.value, token->flags);
2554 /* Issue an error about name-lookup failing. NAME is the
2555 IDENTIFIER_NODE DECL is the result of
2556 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2557 the thing that we hoped to find. */
2559 static void
2560 cp_parser_name_lookup_error (cp_parser* parser,
2561 tree name,
2562 tree decl,
2563 name_lookup_error desired,
2564 location_t location)
2566 /* If name lookup completely failed, tell the user that NAME was not
2567 declared. */
2568 if (decl == error_mark_node)
2570 if (parser->scope && parser->scope != global_namespace)
2571 error_at (location, "%<%E::%E%> has not been declared",
2572 parser->scope, name);
2573 else if (parser->scope == global_namespace)
2574 error_at (location, "%<::%E%> has not been declared", name);
2575 else if (parser->object_scope
2576 && !CLASS_TYPE_P (parser->object_scope))
2577 error_at (location, "request for member %qE in non-class type %qT",
2578 name, parser->object_scope);
2579 else if (parser->object_scope)
2580 error_at (location, "%<%T::%E%> has not been declared",
2581 parser->object_scope, name);
2582 else
2583 error_at (location, "%qE has not been declared", name);
2585 else if (parser->scope && parser->scope != global_namespace)
2587 switch (desired)
2589 case NLE_TYPE:
2590 error_at (location, "%<%E::%E%> is not a type",
2591 parser->scope, name);
2592 break;
2593 case NLE_CXX98:
2594 error_at (location, "%<%E::%E%> is not a class or namespace",
2595 parser->scope, name);
2596 break;
2597 case NLE_NOT_CXX98:
2598 error_at (location,
2599 "%<%E::%E%> is not a class, namespace, or enumeration",
2600 parser->scope, name);
2601 break;
2602 default:
2603 gcc_unreachable ();
2607 else if (parser->scope == global_namespace)
2609 switch (desired)
2611 case NLE_TYPE:
2612 error_at (location, "%<::%E%> is not a type", name);
2613 break;
2614 case NLE_CXX98:
2615 error_at (location, "%<::%E%> is not a class or namespace", name);
2616 break;
2617 case NLE_NOT_CXX98:
2618 error_at (location,
2619 "%<::%E%> is not a class, namespace, or enumeration",
2620 name);
2621 break;
2622 default:
2623 gcc_unreachable ();
2626 else
2628 switch (desired)
2630 case NLE_TYPE:
2631 error_at (location, "%qE is not a type", name);
2632 break;
2633 case NLE_CXX98:
2634 error_at (location, "%qE is not a class or namespace", name);
2635 break;
2636 case NLE_NOT_CXX98:
2637 error_at (location,
2638 "%qE is not a class, namespace, or enumeration", name);
2639 break;
2640 default:
2641 gcc_unreachable ();
2646 /* If we are parsing tentatively, remember that an error has occurred
2647 during this tentative parse. Returns true if the error was
2648 simulated; false if a message should be issued by the caller. */
2650 static bool
2651 cp_parser_simulate_error (cp_parser* parser)
2653 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2655 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2656 return true;
2658 return false;
2661 /* This function is called when a type is defined. If type
2662 definitions are forbidden at this point, an error message is
2663 issued. */
2665 static bool
2666 cp_parser_check_type_definition (cp_parser* parser)
2668 /* If types are forbidden here, issue a message. */
2669 if (parser->type_definition_forbidden_message)
2671 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2672 in the message need to be interpreted. */
2673 error (parser->type_definition_forbidden_message);
2674 return false;
2676 return true;
2679 /* This function is called when the DECLARATOR is processed. The TYPE
2680 was a type defined in the decl-specifiers. If it is invalid to
2681 define a type in the decl-specifiers for DECLARATOR, an error is
2682 issued. TYPE_LOCATION is the location of TYPE and is used
2683 for error reporting. */
2685 static void
2686 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2687 tree type, location_t type_location)
2689 /* [dcl.fct] forbids type definitions in return types.
2690 Unfortunately, it's not easy to know whether or not we are
2691 processing a return type until after the fact. */
2692 while (declarator
2693 && (declarator->kind == cdk_pointer
2694 || declarator->kind == cdk_reference
2695 || declarator->kind == cdk_ptrmem))
2696 declarator = declarator->declarator;
2697 if (declarator
2698 && declarator->kind == cdk_function)
2700 error_at (type_location,
2701 "new types may not be defined in a return type");
2702 inform (type_location,
2703 "(perhaps a semicolon is missing after the definition of %qT)",
2704 type);
2708 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2709 "<" in any valid C++ program. If the next token is indeed "<",
2710 issue a message warning the user about what appears to be an
2711 invalid attempt to form a template-id. LOCATION is the location
2712 of the type-specifier (TYPE) */
2714 static void
2715 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2716 tree type,
2717 enum tag_types tag_type,
2718 location_t location)
2720 cp_token_position start = 0;
2722 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2724 if (TYPE_P (type))
2725 error_at (location, "%qT is not a template", type);
2726 else if (identifier_p (type))
2728 if (tag_type != none_type)
2729 error_at (location, "%qE is not a class template", type);
2730 else
2731 error_at (location, "%qE is not a template", type);
2733 else
2734 error_at (location, "invalid template-id");
2735 /* Remember the location of the invalid "<". */
2736 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2737 start = cp_lexer_token_position (parser->lexer, true);
2738 /* Consume the "<". */
2739 cp_lexer_consume_token (parser->lexer);
2740 /* Parse the template arguments. */
2741 cp_parser_enclosed_template_argument_list (parser);
2742 /* Permanently remove the invalid template arguments so that
2743 this error message is not issued again. */
2744 if (start)
2745 cp_lexer_purge_tokens_after (parser->lexer, start);
2749 /* If parsing an integral constant-expression, issue an error message
2750 about the fact that THING appeared and return true. Otherwise,
2751 return false. In either case, set
2752 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2754 static bool
2755 cp_parser_non_integral_constant_expression (cp_parser *parser,
2756 non_integral_constant thing)
2758 parser->non_integral_constant_expression_p = true;
2759 if (parser->integral_constant_expression_p)
2761 if (!parser->allow_non_integral_constant_expression_p)
2763 const char *msg = NULL;
2764 switch (thing)
2766 case NIC_FLOAT:
2767 error ("floating-point literal "
2768 "cannot appear in a constant-expression");
2769 return true;
2770 case NIC_CAST:
2771 error ("a cast to a type other than an integral or "
2772 "enumeration type cannot appear in a "
2773 "constant-expression");
2774 return true;
2775 case NIC_TYPEID:
2776 error ("%<typeid%> operator "
2777 "cannot appear in a constant-expression");
2778 return true;
2779 case NIC_NCC:
2780 error ("non-constant compound literals "
2781 "cannot appear in a constant-expression");
2782 return true;
2783 case NIC_FUNC_CALL:
2784 error ("a function call "
2785 "cannot appear in a constant-expression");
2786 return true;
2787 case NIC_INC:
2788 error ("an increment "
2789 "cannot appear in a constant-expression");
2790 return true;
2791 case NIC_DEC:
2792 error ("an decrement "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_ARRAY_REF:
2796 error ("an array reference "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_ADDR_LABEL:
2800 error ("the address of a label "
2801 "cannot appear in a constant-expression");
2802 return true;
2803 case NIC_OVERLOADED:
2804 error ("calls to overloaded operators "
2805 "cannot appear in a constant-expression");
2806 return true;
2807 case NIC_ASSIGNMENT:
2808 error ("an assignment cannot appear in a constant-expression");
2809 return true;
2810 case NIC_COMMA:
2811 error ("a comma operator "
2812 "cannot appear in a constant-expression");
2813 return true;
2814 case NIC_CONSTRUCTOR:
2815 error ("a call to a constructor "
2816 "cannot appear in a constant-expression");
2817 return true;
2818 case NIC_TRANSACTION:
2819 error ("a transaction expression "
2820 "cannot appear in a constant-expression");
2821 return true;
2822 case NIC_THIS:
2823 msg = "this";
2824 break;
2825 case NIC_FUNC_NAME:
2826 msg = "__FUNCTION__";
2827 break;
2828 case NIC_PRETTY_FUNC:
2829 msg = "__PRETTY_FUNCTION__";
2830 break;
2831 case NIC_C99_FUNC:
2832 msg = "__func__";
2833 break;
2834 case NIC_VA_ARG:
2835 msg = "va_arg";
2836 break;
2837 case NIC_ARROW:
2838 msg = "->";
2839 break;
2840 case NIC_POINT:
2841 msg = ".";
2842 break;
2843 case NIC_STAR:
2844 msg = "*";
2845 break;
2846 case NIC_ADDR:
2847 msg = "&";
2848 break;
2849 case NIC_PREINCREMENT:
2850 msg = "++";
2851 break;
2852 case NIC_PREDECREMENT:
2853 msg = "--";
2854 break;
2855 case NIC_NEW:
2856 msg = "new";
2857 break;
2858 case NIC_DEL:
2859 msg = "delete";
2860 break;
2861 default:
2862 gcc_unreachable ();
2864 if (msg)
2865 error ("%qs cannot appear in a constant-expression", msg);
2866 return true;
2869 return false;
2872 /* Emit a diagnostic for an invalid type name. SCOPE is the
2873 qualifying scope (or NULL, if none) for ID. This function commits
2874 to the current active tentative parse, if any. (Otherwise, the
2875 problematic construct might be encountered again later, resulting
2876 in duplicate error messages.) LOCATION is the location of ID. */
2878 static void
2879 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2880 tree scope, tree id,
2881 location_t location)
2883 tree decl, old_scope, ambiguous_decls;
2884 cp_parser_commit_to_tentative_parse (parser);
2885 /* Try to lookup the identifier. */
2886 old_scope = parser->scope;
2887 parser->scope = scope;
2888 decl = cp_parser_lookup_name (parser, id, none_type,
2889 /*is_template=*/false,
2890 /*is_namespace=*/false,
2891 /*check_dependency=*/true,
2892 &ambiguous_decls, location);
2893 parser->scope = old_scope;
2894 if (ambiguous_decls)
2895 /* If the lookup was ambiguous, an error will already have
2896 been issued. */
2897 return;
2898 /* If the lookup found a template-name, it means that the user forgot
2899 to specify an argument list. Emit a useful error message. */
2900 if (TREE_CODE (decl) == TEMPLATE_DECL)
2901 error_at (location,
2902 "invalid use of template-name %qE without an argument list",
2903 decl);
2904 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2905 error_at (location, "invalid use of destructor %qD as a type", id);
2906 else if (TREE_CODE (decl) == TYPE_DECL)
2907 /* Something like 'unsigned A a;' */
2908 error_at (location, "invalid combination of multiple type-specifiers");
2909 else if (!parser->scope)
2911 /* Issue an error message. */
2912 error_at (location, "%qE does not name a type", id);
2913 /* If we're in a template class, it's possible that the user was
2914 referring to a type from a base class. For example:
2916 template <typename T> struct A { typedef T X; };
2917 template <typename T> struct B : public A<T> { X x; };
2919 The user should have said "typename A<T>::X". */
2920 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2921 inform (location, "C++11 %<constexpr%> only available with "
2922 "-std=c++11 or -std=gnu++11");
2923 else if (processing_template_decl && current_class_type
2924 && TYPE_BINFO (current_class_type))
2926 tree b;
2928 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2930 b = TREE_CHAIN (b))
2932 tree base_type = BINFO_TYPE (b);
2933 if (CLASS_TYPE_P (base_type)
2934 && dependent_type_p (base_type))
2936 tree field;
2937 /* Go from a particular instantiation of the
2938 template (which will have an empty TYPE_FIELDs),
2939 to the main version. */
2940 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2941 for (field = TYPE_FIELDS (base_type);
2942 field;
2943 field = DECL_CHAIN (field))
2944 if (TREE_CODE (field) == TYPE_DECL
2945 && DECL_NAME (field) == id)
2947 inform (location,
2948 "(perhaps %<typename %T::%E%> was intended)",
2949 BINFO_TYPE (b), id);
2950 break;
2952 if (field)
2953 break;
2958 /* Here we diagnose qualified-ids where the scope is actually correct,
2959 but the identifier does not resolve to a valid type name. */
2960 else if (parser->scope != error_mark_node)
2962 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2964 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2965 error_at (location_of (id),
2966 "%qE in namespace %qE does not name a template type",
2967 id, parser->scope);
2968 else
2969 error_at (location_of (id),
2970 "%qE in namespace %qE does not name a type",
2971 id, parser->scope);
2973 else if (CLASS_TYPE_P (parser->scope)
2974 && constructor_name_p (id, parser->scope))
2976 /* A<T>::A<T>() */
2977 error_at (location, "%<%T::%E%> names the constructor, not"
2978 " the type", parser->scope, id);
2979 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2980 error_at (location, "and %qT has no template constructors",
2981 parser->scope);
2983 else if (TYPE_P (parser->scope)
2984 && dependent_scope_p (parser->scope))
2985 error_at (location, "need %<typename%> before %<%T::%E%> because "
2986 "%qT is a dependent scope",
2987 parser->scope, id, parser->scope);
2988 else if (TYPE_P (parser->scope))
2990 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2991 error_at (location_of (id),
2992 "%qE in %q#T does not name a template type",
2993 id, parser->scope);
2994 else
2995 error_at (location_of (id),
2996 "%qE in %q#T does not name a type",
2997 id, parser->scope);
2999 else
3000 gcc_unreachable ();
3004 /* Check for a common situation where a type-name should be present,
3005 but is not, and issue a sensible error message. Returns true if an
3006 invalid type-name was detected.
3008 The situation handled by this function are variable declarations of the
3009 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3010 Usually, `ID' should name a type, but if we got here it means that it
3011 does not. We try to emit the best possible error message depending on
3012 how exactly the id-expression looks like. */
3014 static bool
3015 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3017 tree id;
3018 cp_token *token = cp_lexer_peek_token (parser->lexer);
3020 /* Avoid duplicate error about ambiguous lookup. */
3021 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3023 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3024 if (next->type == CPP_NAME && next->error_reported)
3025 goto out;
3028 cp_parser_parse_tentatively (parser);
3029 id = cp_parser_id_expression (parser,
3030 /*template_keyword_p=*/false,
3031 /*check_dependency_p=*/true,
3032 /*template_p=*/NULL,
3033 /*declarator_p=*/true,
3034 /*optional_p=*/false);
3035 /* If the next token is a (, this is a function with no explicit return
3036 type, i.e. constructor, destructor or conversion op. */
3037 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3038 || TREE_CODE (id) == TYPE_DECL)
3040 cp_parser_abort_tentative_parse (parser);
3041 return false;
3043 if (!cp_parser_parse_definitely (parser))
3044 return false;
3046 /* Emit a diagnostic for the invalid type. */
3047 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3048 id, token->location);
3049 out:
3050 /* If we aren't in the middle of a declarator (i.e. in a
3051 parameter-declaration-clause), skip to the end of the declaration;
3052 there's no point in trying to process it. */
3053 if (!parser->in_declarator_p)
3054 cp_parser_skip_to_end_of_block_or_statement (parser);
3055 return true;
3058 /* Consume tokens up to, and including, the next non-nested closing `)'.
3059 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3060 are doing error recovery. Returns -1 if OR_COMMA is true and we
3061 found an unnested comma. */
3063 static int
3064 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3065 bool recovering,
3066 bool or_comma,
3067 bool consume_paren)
3069 unsigned paren_depth = 0;
3070 unsigned brace_depth = 0;
3071 unsigned square_depth = 0;
3073 if (recovering && !or_comma
3074 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3075 return 0;
3077 while (true)
3079 cp_token * token = cp_lexer_peek_token (parser->lexer);
3081 switch (token->type)
3083 case CPP_EOF:
3084 case CPP_PRAGMA_EOL:
3085 /* If we've run out of tokens, then there is no closing `)'. */
3086 return 0;
3088 /* This is good for lambda expression capture-lists. */
3089 case CPP_OPEN_SQUARE:
3090 ++square_depth;
3091 break;
3092 case CPP_CLOSE_SQUARE:
3093 if (!square_depth--)
3094 return 0;
3095 break;
3097 case CPP_SEMICOLON:
3098 /* This matches the processing in skip_to_end_of_statement. */
3099 if (!brace_depth)
3100 return 0;
3101 break;
3103 case CPP_OPEN_BRACE:
3104 ++brace_depth;
3105 break;
3106 case CPP_CLOSE_BRACE:
3107 if (!brace_depth--)
3108 return 0;
3109 break;
3111 case CPP_COMMA:
3112 if (recovering && or_comma && !brace_depth && !paren_depth
3113 && !square_depth)
3114 return -1;
3115 break;
3117 case CPP_OPEN_PAREN:
3118 if (!brace_depth)
3119 ++paren_depth;
3120 break;
3122 case CPP_CLOSE_PAREN:
3123 if (!brace_depth && !paren_depth--)
3125 if (consume_paren)
3126 cp_lexer_consume_token (parser->lexer);
3127 return 1;
3129 break;
3131 default:
3132 break;
3135 /* Consume the token. */
3136 cp_lexer_consume_token (parser->lexer);
3140 /* Consume tokens until we reach the end of the current statement.
3141 Normally, that will be just before consuming a `;'. However, if a
3142 non-nested `}' comes first, then we stop before consuming that. */
3144 static void
3145 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3147 unsigned nesting_depth = 0;
3149 /* Unwind generic function template scope if necessary. */
3150 if (parser->fully_implicit_function_template_p)
3151 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3153 while (true)
3155 cp_token *token = cp_lexer_peek_token (parser->lexer);
3157 switch (token->type)
3159 case CPP_EOF:
3160 case CPP_PRAGMA_EOL:
3161 /* If we've run out of tokens, stop. */
3162 return;
3164 case CPP_SEMICOLON:
3165 /* If the next token is a `;', we have reached the end of the
3166 statement. */
3167 if (!nesting_depth)
3168 return;
3169 break;
3171 case CPP_CLOSE_BRACE:
3172 /* If this is a non-nested '}', stop before consuming it.
3173 That way, when confronted with something like:
3175 { 3 + }
3177 we stop before consuming the closing '}', even though we
3178 have not yet reached a `;'. */
3179 if (nesting_depth == 0)
3180 return;
3182 /* If it is the closing '}' for a block that we have
3183 scanned, stop -- but only after consuming the token.
3184 That way given:
3186 void f g () { ... }
3187 typedef int I;
3189 we will stop after the body of the erroneously declared
3190 function, but before consuming the following `typedef'
3191 declaration. */
3192 if (--nesting_depth == 0)
3194 cp_lexer_consume_token (parser->lexer);
3195 return;
3198 case CPP_OPEN_BRACE:
3199 ++nesting_depth;
3200 break;
3202 default:
3203 break;
3206 /* Consume the token. */
3207 cp_lexer_consume_token (parser->lexer);
3211 /* This function is called at the end of a statement or declaration.
3212 If the next token is a semicolon, it is consumed; otherwise, error
3213 recovery is attempted. */
3215 static void
3216 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3218 /* Look for the trailing `;'. */
3219 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3221 /* If there is additional (erroneous) input, skip to the end of
3222 the statement. */
3223 cp_parser_skip_to_end_of_statement (parser);
3224 /* If the next token is now a `;', consume it. */
3225 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3226 cp_lexer_consume_token (parser->lexer);
3230 /* Skip tokens until we have consumed an entire block, or until we
3231 have consumed a non-nested `;'. */
3233 static void
3234 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3236 int nesting_depth = 0;
3238 /* Unwind generic function template scope if necessary. */
3239 if (parser->fully_implicit_function_template_p)
3240 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3242 while (nesting_depth >= 0)
3244 cp_token *token = cp_lexer_peek_token (parser->lexer);
3246 switch (token->type)
3248 case CPP_EOF:
3249 case CPP_PRAGMA_EOL:
3250 /* If we've run out of tokens, stop. */
3251 return;
3253 case CPP_SEMICOLON:
3254 /* Stop if this is an unnested ';'. */
3255 if (!nesting_depth)
3256 nesting_depth = -1;
3257 break;
3259 case CPP_CLOSE_BRACE:
3260 /* Stop if this is an unnested '}', or closes the outermost
3261 nesting level. */
3262 nesting_depth--;
3263 if (nesting_depth < 0)
3264 return;
3265 if (!nesting_depth)
3266 nesting_depth = -1;
3267 break;
3269 case CPP_OPEN_BRACE:
3270 /* Nest. */
3271 nesting_depth++;
3272 break;
3274 default:
3275 break;
3278 /* Consume the token. */
3279 cp_lexer_consume_token (parser->lexer);
3283 /* Skip tokens until a non-nested closing curly brace is the next
3284 token, or there are no more tokens. Return true in the first case,
3285 false otherwise. */
3287 static bool
3288 cp_parser_skip_to_closing_brace (cp_parser *parser)
3290 unsigned nesting_depth = 0;
3292 while (true)
3294 cp_token *token = cp_lexer_peek_token (parser->lexer);
3296 switch (token->type)
3298 case CPP_EOF:
3299 case CPP_PRAGMA_EOL:
3300 /* If we've run out of tokens, stop. */
3301 return false;
3303 case CPP_CLOSE_BRACE:
3304 /* If the next token is a non-nested `}', then we have reached
3305 the end of the current block. */
3306 if (nesting_depth-- == 0)
3307 return true;
3308 break;
3310 case CPP_OPEN_BRACE:
3311 /* If it the next token is a `{', then we are entering a new
3312 block. Consume the entire block. */
3313 ++nesting_depth;
3314 break;
3316 default:
3317 break;
3320 /* Consume the token. */
3321 cp_lexer_consume_token (parser->lexer);
3325 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3326 parameter is the PRAGMA token, allowing us to purge the entire pragma
3327 sequence. */
3329 static void
3330 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3332 cp_token *token;
3334 parser->lexer->in_pragma = false;
3337 token = cp_lexer_consume_token (parser->lexer);
3338 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3340 /* Ensure that the pragma is not parsed again. */
3341 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3344 /* Require pragma end of line, resyncing with it as necessary. The
3345 arguments are as for cp_parser_skip_to_pragma_eol. */
3347 static void
3348 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3350 parser->lexer->in_pragma = false;
3351 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3352 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3355 /* This is a simple wrapper around make_typename_type. When the id is
3356 an unresolved identifier node, we can provide a superior diagnostic
3357 using cp_parser_diagnose_invalid_type_name. */
3359 static tree
3360 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3361 tree id, location_t id_location)
3363 tree result;
3364 if (identifier_p (id))
3366 result = make_typename_type (scope, id, typename_type,
3367 /*complain=*/tf_none);
3368 if (result == error_mark_node)
3369 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3370 return result;
3372 return make_typename_type (scope, id, typename_type, tf_error);
3375 /* This is a wrapper around the
3376 make_{pointer,ptrmem,reference}_declarator functions that decides
3377 which one to call based on the CODE and CLASS_TYPE arguments. The
3378 CODE argument should be one of the values returned by
3379 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3380 appertain to the pointer or reference. */
3382 static cp_declarator *
3383 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3384 cp_cv_quals cv_qualifiers,
3385 cp_declarator *target,
3386 tree attributes)
3388 if (code == ERROR_MARK)
3389 return cp_error_declarator;
3391 if (code == INDIRECT_REF)
3392 if (class_type == NULL_TREE)
3393 return make_pointer_declarator (cv_qualifiers, target, attributes);
3394 else
3395 return make_ptrmem_declarator (cv_qualifiers, class_type,
3396 target, attributes);
3397 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3398 return make_reference_declarator (cv_qualifiers, target,
3399 false, attributes);
3400 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3401 return make_reference_declarator (cv_qualifiers, target,
3402 true, attributes);
3403 gcc_unreachable ();
3406 /* Create a new C++ parser. */
3408 static cp_parser *
3409 cp_parser_new (void)
3411 cp_parser *parser;
3412 cp_lexer *lexer;
3413 unsigned i;
3415 /* cp_lexer_new_main is called before doing GC allocation because
3416 cp_lexer_new_main might load a PCH file. */
3417 lexer = cp_lexer_new_main ();
3419 /* Initialize the binops_by_token so that we can get the tree
3420 directly from the token. */
3421 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3422 binops_by_token[binops[i].token_type] = binops[i];
3424 parser = ggc_cleared_alloc<cp_parser> ();
3425 parser->lexer = lexer;
3426 parser->context = cp_parser_context_new (NULL);
3428 /* For now, we always accept GNU extensions. */
3429 parser->allow_gnu_extensions_p = 1;
3431 /* The `>' token is a greater-than operator, not the end of a
3432 template-id. */
3433 parser->greater_than_is_operator_p = true;
3435 parser->default_arg_ok_p = true;
3437 /* We are not parsing a constant-expression. */
3438 parser->integral_constant_expression_p = false;
3439 parser->allow_non_integral_constant_expression_p = false;
3440 parser->non_integral_constant_expression_p = false;
3442 /* Local variable names are not forbidden. */
3443 parser->local_variables_forbidden_p = false;
3445 /* We are not processing an `extern "C"' declaration. */
3446 parser->in_unbraced_linkage_specification_p = false;
3448 /* We are not processing a declarator. */
3449 parser->in_declarator_p = false;
3451 /* We are not processing a template-argument-list. */
3452 parser->in_template_argument_list_p = false;
3454 /* We are not in an iteration statement. */
3455 parser->in_statement = 0;
3457 /* We are not in a switch statement. */
3458 parser->in_switch_statement_p = false;
3460 /* We are not parsing a type-id inside an expression. */
3461 parser->in_type_id_in_expr_p = false;
3463 /* Declarations aren't implicitly extern "C". */
3464 parser->implicit_extern_c = false;
3466 /* String literals should be translated to the execution character set. */
3467 parser->translate_strings_p = true;
3469 /* We are not parsing a function body. */
3470 parser->in_function_body = false;
3472 /* We can correct until told otherwise. */
3473 parser->colon_corrects_to_scope_p = true;
3475 /* The unparsed function queue is empty. */
3476 push_unparsed_function_queues (parser);
3478 /* There are no classes being defined. */
3479 parser->num_classes_being_defined = 0;
3481 /* No template parameters apply. */
3482 parser->num_template_parameter_lists = 0;
3484 /* Not declaring an implicit function template. */
3485 parser->auto_is_implicit_function_template_parm_p = false;
3486 parser->fully_implicit_function_template_p = false;
3487 parser->implicit_template_parms = 0;
3488 parser->implicit_template_scope = 0;
3490 return parser;
3493 /* Create a cp_lexer structure which will emit the tokens in CACHE
3494 and push it onto the parser's lexer stack. This is used for delayed
3495 parsing of in-class method bodies and default arguments, and should
3496 not be confused with tentative parsing. */
3497 static void
3498 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3500 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3501 lexer->next = parser->lexer;
3502 parser->lexer = lexer;
3504 /* Move the current source position to that of the first token in the
3505 new lexer. */
3506 cp_lexer_set_source_position_from_token (lexer->next_token);
3509 /* Pop the top lexer off the parser stack. This is never used for the
3510 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3511 static void
3512 cp_parser_pop_lexer (cp_parser *parser)
3514 cp_lexer *lexer = parser->lexer;
3515 parser->lexer = lexer->next;
3516 cp_lexer_destroy (lexer);
3518 /* Put the current source position back where it was before this
3519 lexer was pushed. */
3520 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3523 /* Lexical conventions [gram.lex] */
3525 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3526 identifier. */
3528 static tree
3529 cp_parser_identifier (cp_parser* parser)
3531 cp_token *token;
3533 /* Look for the identifier. */
3534 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3535 /* Return the value. */
3536 return token ? token->u.value : error_mark_node;
3539 /* Parse a sequence of adjacent string constants. Returns a
3540 TREE_STRING representing the combined, nul-terminated string
3541 constant. If TRANSLATE is true, translate the string to the
3542 execution character set. If WIDE_OK is true, a wide string is
3543 invalid here.
3545 C++98 [lex.string] says that if a narrow string literal token is
3546 adjacent to a wide string literal token, the behavior is undefined.
3547 However, C99 6.4.5p4 says that this results in a wide string literal.
3548 We follow C99 here, for consistency with the C front end.
3550 This code is largely lifted from lex_string() in c-lex.c.
3552 FUTURE: ObjC++ will need to handle @-strings here. */
3553 static tree
3554 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3556 tree value;
3557 size_t count;
3558 struct obstack str_ob;
3559 cpp_string str, istr, *strs;
3560 cp_token *tok;
3561 enum cpp_ttype type, curr_type;
3562 int have_suffix_p = 0;
3563 tree string_tree;
3564 tree suffix_id = NULL_TREE;
3565 bool curr_tok_is_userdef_p = false;
3567 tok = cp_lexer_peek_token (parser->lexer);
3568 if (!cp_parser_is_string_literal (tok))
3570 cp_parser_error (parser, "expected string-literal");
3571 return error_mark_node;
3574 if (cpp_userdef_string_p (tok->type))
3576 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3577 curr_type = cpp_userdef_string_remove_type (tok->type);
3578 curr_tok_is_userdef_p = true;
3580 else
3582 string_tree = tok->u.value;
3583 curr_type = tok->type;
3585 type = curr_type;
3587 /* Try to avoid the overhead of creating and destroying an obstack
3588 for the common case of just one string. */
3589 if (!cp_parser_is_string_literal
3590 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3592 cp_lexer_consume_token (parser->lexer);
3594 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3595 str.len = TREE_STRING_LENGTH (string_tree);
3596 count = 1;
3598 if (curr_tok_is_userdef_p)
3600 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3601 have_suffix_p = 1;
3602 curr_type = cpp_userdef_string_remove_type (tok->type);
3604 else
3605 curr_type = tok->type;
3607 strs = &str;
3609 else
3611 gcc_obstack_init (&str_ob);
3612 count = 0;
3616 cp_lexer_consume_token (parser->lexer);
3617 count++;
3618 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3619 str.len = TREE_STRING_LENGTH (string_tree);
3621 if (curr_tok_is_userdef_p)
3623 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3624 if (have_suffix_p == 0)
3626 suffix_id = curr_suffix_id;
3627 have_suffix_p = 1;
3629 else if (have_suffix_p == 1
3630 && curr_suffix_id != suffix_id)
3632 error ("inconsistent user-defined literal suffixes"
3633 " %qD and %qD in string literal",
3634 suffix_id, curr_suffix_id);
3635 have_suffix_p = -1;
3637 curr_type = cpp_userdef_string_remove_type (tok->type);
3639 else
3640 curr_type = tok->type;
3642 if (type != curr_type)
3644 if (type == CPP_STRING)
3645 type = curr_type;
3646 else if (curr_type != CPP_STRING)
3647 error_at (tok->location,
3648 "unsupported non-standard concatenation "
3649 "of string literals");
3652 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3654 tok = cp_lexer_peek_token (parser->lexer);
3655 if (cpp_userdef_string_p (tok->type))
3657 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3658 curr_type = cpp_userdef_string_remove_type (tok->type);
3659 curr_tok_is_userdef_p = true;
3661 else
3663 string_tree = tok->u.value;
3664 curr_type = tok->type;
3665 curr_tok_is_userdef_p = false;
3668 while (cp_parser_is_string_literal (tok));
3670 strs = (cpp_string *) obstack_finish (&str_ob);
3673 if (type != CPP_STRING && !wide_ok)
3675 cp_parser_error (parser, "a wide string is invalid in this context");
3676 type = CPP_STRING;
3679 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3680 (parse_in, strs, count, &istr, type))
3682 value = build_string (istr.len, (const char *)istr.text);
3683 free (CONST_CAST (unsigned char *, istr.text));
3685 switch (type)
3687 default:
3688 case CPP_STRING:
3689 case CPP_UTF8STRING:
3690 TREE_TYPE (value) = char_array_type_node;
3691 break;
3692 case CPP_STRING16:
3693 TREE_TYPE (value) = char16_array_type_node;
3694 break;
3695 case CPP_STRING32:
3696 TREE_TYPE (value) = char32_array_type_node;
3697 break;
3698 case CPP_WSTRING:
3699 TREE_TYPE (value) = wchar_array_type_node;
3700 break;
3703 value = fix_string_type (value);
3705 if (have_suffix_p)
3707 tree literal = build_userdef_literal (suffix_id, value,
3708 OT_NONE, NULL_TREE);
3709 tok->u.value = literal;
3710 return cp_parser_userdef_string_literal (tok);
3713 else
3714 /* cpp_interpret_string has issued an error. */
3715 value = error_mark_node;
3717 if (count > 1)
3718 obstack_free (&str_ob, 0);
3720 return value;
3723 /* Look up a literal operator with the name and the exact arguments. */
3725 static tree
3726 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3728 tree decl, fns;
3729 decl = lookup_name (name);
3730 if (!decl || !is_overloaded_fn (decl))
3731 return error_mark_node;
3733 for (fns = decl; fns; fns = OVL_NEXT (fns))
3735 unsigned int ix;
3736 bool found = true;
3737 tree fn = OVL_CURRENT (fns);
3738 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3739 if (parmtypes != NULL_TREE)
3741 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3742 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3744 tree tparm = TREE_VALUE (parmtypes);
3745 tree targ = TREE_TYPE ((*args)[ix]);
3746 bool ptr = TYPE_PTR_P (tparm);
3747 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3748 if ((ptr || arr || !same_type_p (tparm, targ))
3749 && (!ptr || !arr
3750 || !same_type_p (TREE_TYPE (tparm),
3751 TREE_TYPE (targ))))
3752 found = false;
3754 if (found
3755 && ix == vec_safe_length (args)
3756 /* May be this should be sufficient_parms_p instead,
3757 depending on how exactly should user-defined literals
3758 work in presence of default arguments on the literal
3759 operator parameters. */
3760 && parmtypes == void_list_node)
3761 return fn;
3765 return error_mark_node;
3768 /* Parse a user-defined char constant. Returns a call to a user-defined
3769 literal operator taking the character as an argument. */
3771 static tree
3772 cp_parser_userdef_char_literal (cp_parser *parser)
3774 cp_token *token = cp_lexer_consume_token (parser->lexer);
3775 tree literal = token->u.value;
3776 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3777 tree value = USERDEF_LITERAL_VALUE (literal);
3778 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3779 tree decl, result;
3781 /* Build up a call to the user-defined operator */
3782 /* Lookup the name we got back from the id-expression. */
3783 vec<tree, va_gc> *args = make_tree_vector ();
3784 vec_safe_push (args, value);
3785 decl = lookup_literal_operator (name, args);
3786 if (!decl || decl == error_mark_node)
3788 error ("unable to find character literal operator %qD with %qT argument",
3789 name, TREE_TYPE (value));
3790 release_tree_vector (args);
3791 return error_mark_node;
3793 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3794 release_tree_vector (args);
3795 if (result != error_mark_node)
3796 return result;
3798 error ("unable to find character literal operator %qD with %qT argument",
3799 name, TREE_TYPE (value));
3800 return error_mark_node;
3803 /* A subroutine of cp_parser_userdef_numeric_literal to
3804 create a char... template parameter pack from a string node. */
3806 static tree
3807 make_char_string_pack (tree value)
3809 tree charvec;
3810 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3811 const char *str = TREE_STRING_POINTER (value);
3812 int i, len = TREE_STRING_LENGTH (value) - 1;
3813 tree argvec = make_tree_vec (1);
3815 /* Fill in CHARVEC with all of the parameters. */
3816 charvec = make_tree_vec (len);
3817 for (i = 0; i < len; ++i)
3818 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3820 /* Build the argument packs. */
3821 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3822 TREE_TYPE (argpack) = char_type_node;
3824 TREE_VEC_ELT (argvec, 0) = argpack;
3826 return argvec;
3829 /* A subroutine of cp_parser_userdef_numeric_literal to
3830 create a char... template parameter pack from a string node. */
3832 static tree
3833 make_string_pack (tree value)
3835 tree charvec;
3836 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3837 const unsigned char *str
3838 = (const unsigned char *) TREE_STRING_POINTER (value);
3839 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3840 int len = TREE_STRING_LENGTH (value) / sz - 1;
3841 tree argvec = make_tree_vec (2);
3843 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3844 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3846 /* First template parm is character type. */
3847 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3849 /* Fill in CHARVEC with all of the parameters. */
3850 charvec = make_tree_vec (len);
3851 for (int i = 0; i < len; ++i)
3852 TREE_VEC_ELT (charvec, i)
3853 = double_int_to_tree (str_char_type_node,
3854 double_int::from_buffer (str + i * sz, sz));
3856 /* Build the argument packs. */
3857 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3858 TREE_TYPE (argpack) = str_char_type_node;
3860 TREE_VEC_ELT (argvec, 1) = argpack;
3862 return argvec;
3865 /* Parse a user-defined numeric constant. returns a call to a user-defined
3866 literal operator. */
3868 static tree
3869 cp_parser_userdef_numeric_literal (cp_parser *parser)
3871 cp_token *token = cp_lexer_consume_token (parser->lexer);
3872 tree literal = token->u.value;
3873 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3874 tree value = USERDEF_LITERAL_VALUE (literal);
3875 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3876 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3877 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3878 tree decl, result;
3879 vec<tree, va_gc> *args;
3881 /* Look for a literal operator taking the exact type of numeric argument
3882 as the literal value. */
3883 args = make_tree_vector ();
3884 vec_safe_push (args, value);
3885 decl = lookup_literal_operator (name, args);
3886 if (decl && decl != error_mark_node)
3888 result = finish_call_expr (decl, &args, false, true, tf_none);
3889 if (result != error_mark_node)
3891 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3892 warning_at (token->location, OPT_Woverflow,
3893 "integer literal exceeds range of %qT type",
3894 long_long_unsigned_type_node);
3895 else
3897 if (overflow > 0)
3898 warning_at (token->location, OPT_Woverflow,
3899 "floating literal exceeds range of %qT type",
3900 long_double_type_node);
3901 else if (overflow < 0)
3902 warning_at (token->location, OPT_Woverflow,
3903 "floating literal truncated to zero");
3905 release_tree_vector (args);
3906 return result;
3909 release_tree_vector (args);
3911 /* If the numeric argument didn't work, look for a raw literal
3912 operator taking a const char* argument consisting of the number
3913 in string format. */
3914 args = make_tree_vector ();
3915 vec_safe_push (args, num_string);
3916 decl = lookup_literal_operator (name, args);
3917 if (decl && decl != error_mark_node)
3919 result = finish_call_expr (decl, &args, false, true, tf_none);
3920 if (result != error_mark_node)
3922 release_tree_vector (args);
3923 return result;
3926 release_tree_vector (args);
3928 /* If the raw literal didn't work, look for a non-type template
3929 function with parameter pack char.... Call the function with
3930 template parameter characters representing the number. */
3931 args = make_tree_vector ();
3932 decl = lookup_literal_operator (name, args);
3933 if (decl && decl != error_mark_node)
3935 tree tmpl_args = make_char_string_pack (num_string);
3936 decl = lookup_template_function (decl, tmpl_args);
3937 result = finish_call_expr (decl, &args, false, true, tf_none);
3938 if (result != error_mark_node)
3940 release_tree_vector (args);
3941 return result;
3944 release_tree_vector (args);
3946 error ("unable to find numeric literal operator %qD", name);
3947 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3948 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3949 "to enable more built-in suffixes");
3950 return error_mark_node;
3953 /* Parse a user-defined string constant. Returns a call to a user-defined
3954 literal operator taking a character pointer and the length of the string
3955 as arguments. */
3957 static tree
3958 cp_parser_userdef_string_literal (cp_token *token)
3960 tree literal = token->u.value;
3961 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3962 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3963 tree value = USERDEF_LITERAL_VALUE (literal);
3964 int len = TREE_STRING_LENGTH (value)
3965 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3966 tree decl, result;
3967 vec<tree, va_gc> *args;
3969 /* Look for a template function with typename parameter CharT
3970 and parameter pack CharT... Call the function with
3971 template parameter characters representing the string. */
3972 args = make_tree_vector ();
3973 decl = lookup_literal_operator (name, args);
3974 if (decl && decl != error_mark_node)
3976 tree tmpl_args = make_string_pack (value);
3977 decl = lookup_template_function (decl, tmpl_args);
3978 result = finish_call_expr (decl, &args, false, true, tf_none);
3979 if (result != error_mark_node)
3981 release_tree_vector (args);
3982 return result;
3985 release_tree_vector (args);
3987 /* Build up a call to the user-defined operator */
3988 /* Lookup the name we got back from the id-expression. */
3989 args = make_tree_vector ();
3990 vec_safe_push (args, value);
3991 vec_safe_push (args, build_int_cst (size_type_node, len));
3992 decl = lookup_name (name);
3993 if (!decl || decl == error_mark_node)
3995 error ("unable to find string literal operator %qD", name);
3996 release_tree_vector (args);
3997 return error_mark_node;
3999 result = finish_call_expr (decl, &args, false, true, tf_none);
4000 release_tree_vector (args);
4001 if (result != error_mark_node)
4002 return result;
4004 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4005 name, TREE_TYPE (value), size_type_node);
4006 return error_mark_node;
4010 /* Basic concepts [gram.basic] */
4012 /* Parse a translation-unit.
4014 translation-unit:
4015 declaration-seq [opt]
4017 Returns TRUE if all went well. */
4019 static bool
4020 cp_parser_translation_unit (cp_parser* parser)
4022 /* The address of the first non-permanent object on the declarator
4023 obstack. */
4024 static void *declarator_obstack_base;
4026 bool success;
4028 /* Create the declarator obstack, if necessary. */
4029 if (!cp_error_declarator)
4031 gcc_obstack_init (&declarator_obstack);
4032 /* Create the error declarator. */
4033 cp_error_declarator = make_declarator (cdk_error);
4034 /* Create the empty parameter list. */
4035 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4036 /* Remember where the base of the declarator obstack lies. */
4037 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4040 cp_parser_declaration_seq_opt (parser);
4042 /* If there are no tokens left then all went well. */
4043 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4045 /* Get rid of the token array; we don't need it any more. */
4046 cp_lexer_destroy (parser->lexer);
4047 parser->lexer = NULL;
4049 /* This file might have been a context that's implicitly extern
4050 "C". If so, pop the lang context. (Only relevant for PCH.) */
4051 if (parser->implicit_extern_c)
4053 pop_lang_context ();
4054 parser->implicit_extern_c = false;
4057 /* Finish up. */
4058 finish_translation_unit ();
4060 success = true;
4062 else
4064 cp_parser_error (parser, "expected declaration");
4065 success = false;
4068 /* Make sure the declarator obstack was fully cleaned up. */
4069 gcc_assert (obstack_next_free (&declarator_obstack)
4070 == declarator_obstack_base);
4072 /* All went well. */
4073 return success;
4076 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4077 decltype context. */
4079 static inline tsubst_flags_t
4080 complain_flags (bool decltype_p)
4082 tsubst_flags_t complain = tf_warning_or_error;
4083 if (decltype_p)
4084 complain |= tf_decltype;
4085 return complain;
4088 /* Expressions [gram.expr] */
4090 /* Parse a primary-expression.
4092 primary-expression:
4093 literal
4094 this
4095 ( expression )
4096 id-expression
4098 GNU Extensions:
4100 primary-expression:
4101 ( compound-statement )
4102 __builtin_va_arg ( assignment-expression , type-id )
4103 __builtin_offsetof ( type-id , offsetof-expression )
4105 C++ Extensions:
4106 __has_nothrow_assign ( type-id )
4107 __has_nothrow_constructor ( type-id )
4108 __has_nothrow_copy ( type-id )
4109 __has_trivial_assign ( type-id )
4110 __has_trivial_constructor ( type-id )
4111 __has_trivial_copy ( type-id )
4112 __has_trivial_destructor ( type-id )
4113 __has_virtual_destructor ( type-id )
4114 __is_abstract ( type-id )
4115 __is_base_of ( type-id , type-id )
4116 __is_class ( type-id )
4117 __is_convertible_to ( type-id , type-id )
4118 __is_empty ( type-id )
4119 __is_enum ( type-id )
4120 __is_final ( type-id )
4121 __is_literal_type ( type-id )
4122 __is_pod ( type-id )
4123 __is_polymorphic ( type-id )
4124 __is_std_layout ( type-id )
4125 __is_trivial ( type-id )
4126 __is_union ( type-id )
4128 Objective-C++ Extension:
4130 primary-expression:
4131 objc-expression
4133 literal:
4134 __null
4136 ADDRESS_P is true iff this expression was immediately preceded by
4137 "&" and therefore might denote a pointer-to-member. CAST_P is true
4138 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4139 true iff this expression is a template argument.
4141 Returns a representation of the expression. Upon return, *IDK
4142 indicates what kind of id-expression (if any) was present. */
4144 static tree
4145 cp_parser_primary_expression (cp_parser *parser,
4146 bool address_p,
4147 bool cast_p,
4148 bool template_arg_p,
4149 bool decltype_p,
4150 cp_id_kind *idk)
4152 cp_token *token = NULL;
4154 /* Assume the primary expression is not an id-expression. */
4155 *idk = CP_ID_KIND_NONE;
4157 /* Peek at the next token. */
4158 token = cp_lexer_peek_token (parser->lexer);
4159 switch (token->type)
4161 /* literal:
4162 integer-literal
4163 character-literal
4164 floating-literal
4165 string-literal
4166 boolean-literal
4167 pointer-literal
4168 user-defined-literal */
4169 case CPP_CHAR:
4170 case CPP_CHAR16:
4171 case CPP_CHAR32:
4172 case CPP_WCHAR:
4173 case CPP_NUMBER:
4174 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4175 return cp_parser_userdef_numeric_literal (parser);
4176 token = cp_lexer_consume_token (parser->lexer);
4177 if (TREE_CODE (token->u.value) == FIXED_CST)
4179 error_at (token->location,
4180 "fixed-point types not supported in C++");
4181 return error_mark_node;
4183 /* Floating-point literals are only allowed in an integral
4184 constant expression if they are cast to an integral or
4185 enumeration type. */
4186 if (TREE_CODE (token->u.value) == REAL_CST
4187 && parser->integral_constant_expression_p
4188 && pedantic)
4190 /* CAST_P will be set even in invalid code like "int(2.7 +
4191 ...)". Therefore, we have to check that the next token
4192 is sure to end the cast. */
4193 if (cast_p)
4195 cp_token *next_token;
4197 next_token = cp_lexer_peek_token (parser->lexer);
4198 if (/* The comma at the end of an
4199 enumerator-definition. */
4200 next_token->type != CPP_COMMA
4201 /* The curly brace at the end of an enum-specifier. */
4202 && next_token->type != CPP_CLOSE_BRACE
4203 /* The end of a statement. */
4204 && next_token->type != CPP_SEMICOLON
4205 /* The end of the cast-expression. */
4206 && next_token->type != CPP_CLOSE_PAREN
4207 /* The end of an array bound. */
4208 && next_token->type != CPP_CLOSE_SQUARE
4209 /* The closing ">" in a template-argument-list. */
4210 && (next_token->type != CPP_GREATER
4211 || parser->greater_than_is_operator_p)
4212 /* C++0x only: A ">>" treated like two ">" tokens,
4213 in a template-argument-list. */
4214 && (next_token->type != CPP_RSHIFT
4215 || (cxx_dialect == cxx98)
4216 || parser->greater_than_is_operator_p))
4217 cast_p = false;
4220 /* If we are within a cast, then the constraint that the
4221 cast is to an integral or enumeration type will be
4222 checked at that point. If we are not within a cast, then
4223 this code is invalid. */
4224 if (!cast_p)
4225 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4227 return token->u.value;
4229 case CPP_CHAR_USERDEF:
4230 case CPP_CHAR16_USERDEF:
4231 case CPP_CHAR32_USERDEF:
4232 case CPP_WCHAR_USERDEF:
4233 return cp_parser_userdef_char_literal (parser);
4235 case CPP_STRING:
4236 case CPP_STRING16:
4237 case CPP_STRING32:
4238 case CPP_WSTRING:
4239 case CPP_UTF8STRING:
4240 case CPP_STRING_USERDEF:
4241 case CPP_STRING16_USERDEF:
4242 case CPP_STRING32_USERDEF:
4243 case CPP_WSTRING_USERDEF:
4244 case CPP_UTF8STRING_USERDEF:
4245 /* ??? Should wide strings be allowed when parser->translate_strings_p
4246 is false (i.e. in attributes)? If not, we can kill the third
4247 argument to cp_parser_string_literal. */
4248 return cp_parser_string_literal (parser,
4249 parser->translate_strings_p,
4250 true);
4252 case CPP_OPEN_PAREN:
4254 tree expr;
4255 bool saved_greater_than_is_operator_p;
4257 /* Consume the `('. */
4258 cp_lexer_consume_token (parser->lexer);
4259 /* Within a parenthesized expression, a `>' token is always
4260 the greater-than operator. */
4261 saved_greater_than_is_operator_p
4262 = parser->greater_than_is_operator_p;
4263 parser->greater_than_is_operator_p = true;
4264 /* If we see `( { ' then we are looking at the beginning of
4265 a GNU statement-expression. */
4266 if (cp_parser_allow_gnu_extensions_p (parser)
4267 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4269 /* Statement-expressions are not allowed by the standard. */
4270 pedwarn (token->location, OPT_Wpedantic,
4271 "ISO C++ forbids braced-groups within expressions");
4273 /* And they're not allowed outside of a function-body; you
4274 cannot, for example, write:
4276 int i = ({ int j = 3; j + 1; });
4278 at class or namespace scope. */
4279 if (!parser->in_function_body
4280 || parser->in_template_argument_list_p)
4282 error_at (token->location,
4283 "statement-expressions are not allowed outside "
4284 "functions nor in template-argument lists");
4285 cp_parser_skip_to_end_of_block_or_statement (parser);
4286 expr = error_mark_node;
4288 else
4290 /* Start the statement-expression. */
4291 expr = begin_stmt_expr ();
4292 /* Parse the compound-statement. */
4293 cp_parser_compound_statement (parser, expr, false, false);
4294 /* Finish up. */
4295 expr = finish_stmt_expr (expr, false);
4298 else
4300 /* Parse the parenthesized expression. */
4301 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4302 /* Let the front end know that this expression was
4303 enclosed in parentheses. This matters in case, for
4304 example, the expression is of the form `A::B', since
4305 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4306 not. */
4307 expr = finish_parenthesized_expr (expr);
4308 /* DR 705: Wrapping an unqualified name in parentheses
4309 suppresses arg-dependent lookup. We want to pass back
4310 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4311 (c++/37862), but none of the others. */
4312 if (*idk != CP_ID_KIND_QUALIFIED)
4313 *idk = CP_ID_KIND_NONE;
4315 /* The `>' token might be the end of a template-id or
4316 template-parameter-list now. */
4317 parser->greater_than_is_operator_p
4318 = saved_greater_than_is_operator_p;
4319 /* Consume the `)'. */
4320 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4321 cp_parser_skip_to_end_of_statement (parser);
4323 return expr;
4326 case CPP_OPEN_SQUARE:
4327 if (c_dialect_objc ())
4328 /* We have an Objective-C++ message. */
4329 return cp_parser_objc_expression (parser);
4331 tree lam = cp_parser_lambda_expression (parser);
4332 /* Don't warn about a failed tentative parse. */
4333 if (cp_parser_error_occurred (parser))
4334 return error_mark_node;
4335 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4336 return lam;
4339 case CPP_OBJC_STRING:
4340 if (c_dialect_objc ())
4341 /* We have an Objective-C++ string literal. */
4342 return cp_parser_objc_expression (parser);
4343 cp_parser_error (parser, "expected primary-expression");
4344 return error_mark_node;
4346 case CPP_KEYWORD:
4347 switch (token->keyword)
4349 /* These two are the boolean literals. */
4350 case RID_TRUE:
4351 cp_lexer_consume_token (parser->lexer);
4352 return boolean_true_node;
4353 case RID_FALSE:
4354 cp_lexer_consume_token (parser->lexer);
4355 return boolean_false_node;
4357 /* The `__null' literal. */
4358 case RID_NULL:
4359 cp_lexer_consume_token (parser->lexer);
4360 return null_node;
4362 /* The `nullptr' literal. */
4363 case RID_NULLPTR:
4364 cp_lexer_consume_token (parser->lexer);
4365 return nullptr_node;
4367 /* Recognize the `this' keyword. */
4368 case RID_THIS:
4369 cp_lexer_consume_token (parser->lexer);
4370 if (parser->local_variables_forbidden_p)
4372 error_at (token->location,
4373 "%<this%> may not be used in this context");
4374 return error_mark_node;
4376 /* Pointers cannot appear in constant-expressions. */
4377 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4378 return error_mark_node;
4379 return finish_this_expr ();
4381 /* The `operator' keyword can be the beginning of an
4382 id-expression. */
4383 case RID_OPERATOR:
4384 goto id_expression;
4386 case RID_FUNCTION_NAME:
4387 case RID_PRETTY_FUNCTION_NAME:
4388 case RID_C99_FUNCTION_NAME:
4390 non_integral_constant name;
4392 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4393 __func__ are the names of variables -- but they are
4394 treated specially. Therefore, they are handled here,
4395 rather than relying on the generic id-expression logic
4396 below. Grammatically, these names are id-expressions.
4398 Consume the token. */
4399 token = cp_lexer_consume_token (parser->lexer);
4401 switch (token->keyword)
4403 case RID_FUNCTION_NAME:
4404 name = NIC_FUNC_NAME;
4405 break;
4406 case RID_PRETTY_FUNCTION_NAME:
4407 name = NIC_PRETTY_FUNC;
4408 break;
4409 case RID_C99_FUNCTION_NAME:
4410 name = NIC_C99_FUNC;
4411 break;
4412 default:
4413 gcc_unreachable ();
4416 if (cp_parser_non_integral_constant_expression (parser, name))
4417 return error_mark_node;
4419 /* Look up the name. */
4420 return finish_fname (token->u.value);
4423 case RID_VA_ARG:
4425 tree expression;
4426 tree type;
4427 source_location type_location;
4429 /* The `__builtin_va_arg' construct is used to handle
4430 `va_arg'. Consume the `__builtin_va_arg' token. */
4431 cp_lexer_consume_token (parser->lexer);
4432 /* Look for the opening `('. */
4433 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4434 /* Now, parse the assignment-expression. */
4435 expression = cp_parser_assignment_expression (parser,
4436 /*cast_p=*/false, NULL);
4437 /* Look for the `,'. */
4438 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4439 type_location = cp_lexer_peek_token (parser->lexer)->location;
4440 /* Parse the type-id. */
4441 type = cp_parser_type_id (parser);
4442 /* Look for the closing `)'. */
4443 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4444 /* Using `va_arg' in a constant-expression is not
4445 allowed. */
4446 if (cp_parser_non_integral_constant_expression (parser,
4447 NIC_VA_ARG))
4448 return error_mark_node;
4449 return build_x_va_arg (type_location, expression, type);
4452 case RID_OFFSETOF:
4453 return cp_parser_builtin_offsetof (parser);
4455 case RID_HAS_NOTHROW_ASSIGN:
4456 case RID_HAS_NOTHROW_CONSTRUCTOR:
4457 case RID_HAS_NOTHROW_COPY:
4458 case RID_HAS_TRIVIAL_ASSIGN:
4459 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4460 case RID_HAS_TRIVIAL_COPY:
4461 case RID_HAS_TRIVIAL_DESTRUCTOR:
4462 case RID_HAS_VIRTUAL_DESTRUCTOR:
4463 case RID_IS_ABSTRACT:
4464 case RID_IS_BASE_OF:
4465 case RID_IS_CLASS:
4466 case RID_IS_CONVERTIBLE_TO:
4467 case RID_IS_EMPTY:
4468 case RID_IS_ENUM:
4469 case RID_IS_FINAL:
4470 case RID_IS_LITERAL_TYPE:
4471 case RID_IS_POD:
4472 case RID_IS_POLYMORPHIC:
4473 case RID_IS_STD_LAYOUT:
4474 case RID_IS_TRIVIAL:
4475 case RID_IS_UNION:
4476 return cp_parser_trait_expr (parser, token->keyword);
4478 /* Objective-C++ expressions. */
4479 case RID_AT_ENCODE:
4480 case RID_AT_PROTOCOL:
4481 case RID_AT_SELECTOR:
4482 return cp_parser_objc_expression (parser);
4484 case RID_TEMPLATE:
4485 if (parser->in_function_body
4486 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4487 == CPP_LESS))
4489 error_at (token->location,
4490 "a template declaration cannot appear at block scope");
4491 cp_parser_skip_to_end_of_block_or_statement (parser);
4492 return error_mark_node;
4494 default:
4495 cp_parser_error (parser, "expected primary-expression");
4496 return error_mark_node;
4499 /* An id-expression can start with either an identifier, a
4500 `::' as the beginning of a qualified-id, or the "operator"
4501 keyword. */
4502 case CPP_NAME:
4503 case CPP_SCOPE:
4504 case CPP_TEMPLATE_ID:
4505 case CPP_NESTED_NAME_SPECIFIER:
4507 tree id_expression;
4508 tree decl;
4509 const char *error_msg;
4510 bool template_p;
4511 bool done;
4512 cp_token *id_expr_token;
4514 id_expression:
4515 /* Parse the id-expression. */
4516 id_expression
4517 = cp_parser_id_expression (parser,
4518 /*template_keyword_p=*/false,
4519 /*check_dependency_p=*/true,
4520 &template_p,
4521 /*declarator_p=*/false,
4522 /*optional_p=*/false);
4523 if (id_expression == error_mark_node)
4524 return error_mark_node;
4525 id_expr_token = token;
4526 token = cp_lexer_peek_token (parser->lexer);
4527 done = (token->type != CPP_OPEN_SQUARE
4528 && token->type != CPP_OPEN_PAREN
4529 && token->type != CPP_DOT
4530 && token->type != CPP_DEREF
4531 && token->type != CPP_PLUS_PLUS
4532 && token->type != CPP_MINUS_MINUS);
4533 /* If we have a template-id, then no further lookup is
4534 required. If the template-id was for a template-class, we
4535 will sometimes have a TYPE_DECL at this point. */
4536 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4537 || TREE_CODE (id_expression) == TYPE_DECL)
4538 decl = id_expression;
4539 /* Look up the name. */
4540 else
4542 tree ambiguous_decls;
4544 /* If we already know that this lookup is ambiguous, then
4545 we've already issued an error message; there's no reason
4546 to check again. */
4547 if (id_expr_token->type == CPP_NAME
4548 && id_expr_token->error_reported)
4550 cp_parser_simulate_error (parser);
4551 return error_mark_node;
4554 decl = cp_parser_lookup_name (parser, id_expression,
4555 none_type,
4556 template_p,
4557 /*is_namespace=*/false,
4558 /*check_dependency=*/true,
4559 &ambiguous_decls,
4560 id_expr_token->location);
4561 /* If the lookup was ambiguous, an error will already have
4562 been issued. */
4563 if (ambiguous_decls)
4564 return error_mark_node;
4566 /* In Objective-C++, we may have an Objective-C 2.0
4567 dot-syntax for classes here. */
4568 if (c_dialect_objc ()
4569 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4570 && TREE_CODE (decl) == TYPE_DECL
4571 && objc_is_class_name (decl))
4573 tree component;
4574 cp_lexer_consume_token (parser->lexer);
4575 component = cp_parser_identifier (parser);
4576 if (component == error_mark_node)
4577 return error_mark_node;
4579 return objc_build_class_component_ref (id_expression, component);
4582 /* In Objective-C++, an instance variable (ivar) may be preferred
4583 to whatever cp_parser_lookup_name() found. */
4584 decl = objc_lookup_ivar (decl, id_expression);
4586 /* If name lookup gives us a SCOPE_REF, then the
4587 qualifying scope was dependent. */
4588 if (TREE_CODE (decl) == SCOPE_REF)
4590 /* At this point, we do not know if DECL is a valid
4591 integral constant expression. We assume that it is
4592 in fact such an expression, so that code like:
4594 template <int N> struct A {
4595 int a[B<N>::i];
4598 is accepted. At template-instantiation time, we
4599 will check that B<N>::i is actually a constant. */
4600 return decl;
4602 /* Check to see if DECL is a local variable in a context
4603 where that is forbidden. */
4604 if (parser->local_variables_forbidden_p
4605 && local_variable_p (decl))
4607 /* It might be that we only found DECL because we are
4608 trying to be generous with pre-ISO scoping rules.
4609 For example, consider:
4611 int i;
4612 void g() {
4613 for (int i = 0; i < 10; ++i) {}
4614 extern void f(int j = i);
4617 Here, name look up will originally find the out
4618 of scope `i'. We need to issue a warning message,
4619 but then use the global `i'. */
4620 decl = check_for_out_of_scope_variable (decl);
4621 if (local_variable_p (decl))
4623 error_at (id_expr_token->location,
4624 "local variable %qD may not appear in this context",
4625 decl);
4626 return error_mark_node;
4631 decl = (finish_id_expression
4632 (id_expression, decl, parser->scope,
4633 idk,
4634 parser->integral_constant_expression_p,
4635 parser->allow_non_integral_constant_expression_p,
4636 &parser->non_integral_constant_expression_p,
4637 template_p, done, address_p,
4638 template_arg_p,
4639 &error_msg,
4640 id_expr_token->location));
4641 if (error_msg)
4642 cp_parser_error (parser, error_msg);
4643 return decl;
4646 /* Anything else is an error. */
4647 default:
4648 cp_parser_error (parser, "expected primary-expression");
4649 return error_mark_node;
4653 static inline tree
4654 cp_parser_primary_expression (cp_parser *parser,
4655 bool address_p,
4656 bool cast_p,
4657 bool template_arg_p,
4658 cp_id_kind *idk)
4660 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4661 /*decltype*/false, idk);
4664 /* Parse an id-expression.
4666 id-expression:
4667 unqualified-id
4668 qualified-id
4670 qualified-id:
4671 :: [opt] nested-name-specifier template [opt] unqualified-id
4672 :: identifier
4673 :: operator-function-id
4674 :: template-id
4676 Return a representation of the unqualified portion of the
4677 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4678 a `::' or nested-name-specifier.
4680 Often, if the id-expression was a qualified-id, the caller will
4681 want to make a SCOPE_REF to represent the qualified-id. This
4682 function does not do this in order to avoid wastefully creating
4683 SCOPE_REFs when they are not required.
4685 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4686 `template' keyword.
4688 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4689 uninstantiated templates.
4691 If *TEMPLATE_P is non-NULL, it is set to true iff the
4692 `template' keyword is used to explicitly indicate that the entity
4693 named is a template.
4695 If DECLARATOR_P is true, the id-expression is appearing as part of
4696 a declarator, rather than as part of an expression. */
4698 static tree
4699 cp_parser_id_expression (cp_parser *parser,
4700 bool template_keyword_p,
4701 bool check_dependency_p,
4702 bool *template_p,
4703 bool declarator_p,
4704 bool optional_p)
4706 bool global_scope_p;
4707 bool nested_name_specifier_p;
4709 /* Assume the `template' keyword was not used. */
4710 if (template_p)
4711 *template_p = template_keyword_p;
4713 /* Look for the optional `::' operator. */
4714 global_scope_p
4715 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4716 != NULL_TREE);
4717 /* Look for the optional nested-name-specifier. */
4718 nested_name_specifier_p
4719 = (cp_parser_nested_name_specifier_opt (parser,
4720 /*typename_keyword_p=*/false,
4721 check_dependency_p,
4722 /*type_p=*/false,
4723 declarator_p)
4724 != NULL_TREE);
4725 /* If there is a nested-name-specifier, then we are looking at
4726 the first qualified-id production. */
4727 if (nested_name_specifier_p)
4729 tree saved_scope;
4730 tree saved_object_scope;
4731 tree saved_qualifying_scope;
4732 tree unqualified_id;
4733 bool is_template;
4735 /* See if the next token is the `template' keyword. */
4736 if (!template_p)
4737 template_p = &is_template;
4738 *template_p = cp_parser_optional_template_keyword (parser);
4739 /* Name lookup we do during the processing of the
4740 unqualified-id might obliterate SCOPE. */
4741 saved_scope = parser->scope;
4742 saved_object_scope = parser->object_scope;
4743 saved_qualifying_scope = parser->qualifying_scope;
4744 /* Process the final unqualified-id. */
4745 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4746 check_dependency_p,
4747 declarator_p,
4748 /*optional_p=*/false);
4749 /* Restore the SAVED_SCOPE for our caller. */
4750 parser->scope = saved_scope;
4751 parser->object_scope = saved_object_scope;
4752 parser->qualifying_scope = saved_qualifying_scope;
4754 return unqualified_id;
4756 /* Otherwise, if we are in global scope, then we are looking at one
4757 of the other qualified-id productions. */
4758 else if (global_scope_p)
4760 cp_token *token;
4761 tree id;
4763 /* Peek at the next token. */
4764 token = cp_lexer_peek_token (parser->lexer);
4766 /* If it's an identifier, and the next token is not a "<", then
4767 we can avoid the template-id case. This is an optimization
4768 for this common case. */
4769 if (token->type == CPP_NAME
4770 && !cp_parser_nth_token_starts_template_argument_list_p
4771 (parser, 2))
4772 return cp_parser_identifier (parser);
4774 cp_parser_parse_tentatively (parser);
4775 /* Try a template-id. */
4776 id = cp_parser_template_id (parser,
4777 /*template_keyword_p=*/false,
4778 /*check_dependency_p=*/true,
4779 none_type,
4780 declarator_p);
4781 /* If that worked, we're done. */
4782 if (cp_parser_parse_definitely (parser))
4783 return id;
4785 /* Peek at the next token. (Changes in the token buffer may
4786 have invalidated the pointer obtained above.) */
4787 token = cp_lexer_peek_token (parser->lexer);
4789 switch (token->type)
4791 case CPP_NAME:
4792 return cp_parser_identifier (parser);
4794 case CPP_KEYWORD:
4795 if (token->keyword == RID_OPERATOR)
4796 return cp_parser_operator_function_id (parser);
4797 /* Fall through. */
4799 default:
4800 cp_parser_error (parser, "expected id-expression");
4801 return error_mark_node;
4804 else
4805 return cp_parser_unqualified_id (parser, template_keyword_p,
4806 /*check_dependency_p=*/true,
4807 declarator_p,
4808 optional_p);
4811 /* Parse an unqualified-id.
4813 unqualified-id:
4814 identifier
4815 operator-function-id
4816 conversion-function-id
4817 ~ class-name
4818 template-id
4820 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4821 keyword, in a construct like `A::template ...'.
4823 Returns a representation of unqualified-id. For the `identifier'
4824 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4825 production a BIT_NOT_EXPR is returned; the operand of the
4826 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4827 other productions, see the documentation accompanying the
4828 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4829 names are looked up in uninstantiated templates. If DECLARATOR_P
4830 is true, the unqualified-id is appearing as part of a declarator,
4831 rather than as part of an expression. */
4833 static tree
4834 cp_parser_unqualified_id (cp_parser* parser,
4835 bool template_keyword_p,
4836 bool check_dependency_p,
4837 bool declarator_p,
4838 bool optional_p)
4840 cp_token *token;
4842 /* Peek at the next token. */
4843 token = cp_lexer_peek_token (parser->lexer);
4845 switch (token->type)
4847 case CPP_NAME:
4849 tree id;
4851 /* We don't know yet whether or not this will be a
4852 template-id. */
4853 cp_parser_parse_tentatively (parser);
4854 /* Try a template-id. */
4855 id = cp_parser_template_id (parser, template_keyword_p,
4856 check_dependency_p,
4857 none_type,
4858 declarator_p);
4859 /* If it worked, we're done. */
4860 if (cp_parser_parse_definitely (parser))
4861 return id;
4862 /* Otherwise, it's an ordinary identifier. */
4863 return cp_parser_identifier (parser);
4866 case CPP_TEMPLATE_ID:
4867 return cp_parser_template_id (parser, template_keyword_p,
4868 check_dependency_p,
4869 none_type,
4870 declarator_p);
4872 case CPP_COMPL:
4874 tree type_decl;
4875 tree qualifying_scope;
4876 tree object_scope;
4877 tree scope;
4878 bool done;
4880 /* Consume the `~' token. */
4881 cp_lexer_consume_token (parser->lexer);
4882 /* Parse the class-name. The standard, as written, seems to
4883 say that:
4885 template <typename T> struct S { ~S (); };
4886 template <typename T> S<T>::~S() {}
4888 is invalid, since `~' must be followed by a class-name, but
4889 `S<T>' is dependent, and so not known to be a class.
4890 That's not right; we need to look in uninstantiated
4891 templates. A further complication arises from:
4893 template <typename T> void f(T t) {
4894 t.T::~T();
4897 Here, it is not possible to look up `T' in the scope of `T'
4898 itself. We must look in both the current scope, and the
4899 scope of the containing complete expression.
4901 Yet another issue is:
4903 struct S {
4904 int S;
4905 ~S();
4908 S::~S() {}
4910 The standard does not seem to say that the `S' in `~S'
4911 should refer to the type `S' and not the data member
4912 `S::S'. */
4914 /* DR 244 says that we look up the name after the "~" in the
4915 same scope as we looked up the qualifying name. That idea
4916 isn't fully worked out; it's more complicated than that. */
4917 scope = parser->scope;
4918 object_scope = parser->object_scope;
4919 qualifying_scope = parser->qualifying_scope;
4921 /* Check for invalid scopes. */
4922 if (scope == error_mark_node)
4924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4925 cp_lexer_consume_token (parser->lexer);
4926 return error_mark_node;
4928 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4930 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4931 error_at (token->location,
4932 "scope %qT before %<~%> is not a class-name",
4933 scope);
4934 cp_parser_simulate_error (parser);
4935 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4936 cp_lexer_consume_token (parser->lexer);
4937 return error_mark_node;
4939 gcc_assert (!scope || TYPE_P (scope));
4941 /* If the name is of the form "X::~X" it's OK even if X is a
4942 typedef. */
4943 token = cp_lexer_peek_token (parser->lexer);
4944 if (scope
4945 && token->type == CPP_NAME
4946 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4947 != CPP_LESS)
4948 && (token->u.value == TYPE_IDENTIFIER (scope)
4949 || (CLASS_TYPE_P (scope)
4950 && constructor_name_p (token->u.value, scope))))
4952 cp_lexer_consume_token (parser->lexer);
4953 return build_nt (BIT_NOT_EXPR, scope);
4956 /* ~auto means the destructor of whatever the object is. */
4957 if (cp_parser_is_keyword (token, RID_AUTO))
4959 if (cxx_dialect < cxx1y)
4960 pedwarn (input_location, 0,
4961 "%<~auto%> only available with "
4962 "-std=c++1y or -std=gnu++1y");
4963 cp_lexer_consume_token (parser->lexer);
4964 return build_nt (BIT_NOT_EXPR, make_auto ());
4967 /* If there was an explicit qualification (S::~T), first look
4968 in the scope given by the qualification (i.e., S).
4970 Note: in the calls to cp_parser_class_name below we pass
4971 typename_type so that lookup finds the injected-class-name
4972 rather than the constructor. */
4973 done = false;
4974 type_decl = NULL_TREE;
4975 if (scope)
4977 cp_parser_parse_tentatively (parser);
4978 type_decl = cp_parser_class_name (parser,
4979 /*typename_keyword_p=*/false,
4980 /*template_keyword_p=*/false,
4981 typename_type,
4982 /*check_dependency=*/false,
4983 /*class_head_p=*/false,
4984 declarator_p);
4985 if (cp_parser_parse_definitely (parser))
4986 done = true;
4988 /* In "N::S::~S", look in "N" as well. */
4989 if (!done && scope && qualifying_scope)
4991 cp_parser_parse_tentatively (parser);
4992 parser->scope = qualifying_scope;
4993 parser->object_scope = NULL_TREE;
4994 parser->qualifying_scope = NULL_TREE;
4995 type_decl
4996 = cp_parser_class_name (parser,
4997 /*typename_keyword_p=*/false,
4998 /*template_keyword_p=*/false,
4999 typename_type,
5000 /*check_dependency=*/false,
5001 /*class_head_p=*/false,
5002 declarator_p);
5003 if (cp_parser_parse_definitely (parser))
5004 done = true;
5006 /* In "p->S::~T", look in the scope given by "*p" as well. */
5007 else if (!done && object_scope)
5009 cp_parser_parse_tentatively (parser);
5010 parser->scope = object_scope;
5011 parser->object_scope = NULL_TREE;
5012 parser->qualifying_scope = NULL_TREE;
5013 type_decl
5014 = cp_parser_class_name (parser,
5015 /*typename_keyword_p=*/false,
5016 /*template_keyword_p=*/false,
5017 typename_type,
5018 /*check_dependency=*/false,
5019 /*class_head_p=*/false,
5020 declarator_p);
5021 if (cp_parser_parse_definitely (parser))
5022 done = true;
5024 /* Look in the surrounding context. */
5025 if (!done)
5027 parser->scope = NULL_TREE;
5028 parser->object_scope = NULL_TREE;
5029 parser->qualifying_scope = NULL_TREE;
5030 if (processing_template_decl)
5031 cp_parser_parse_tentatively (parser);
5032 type_decl
5033 = cp_parser_class_name (parser,
5034 /*typename_keyword_p=*/false,
5035 /*template_keyword_p=*/false,
5036 typename_type,
5037 /*check_dependency=*/false,
5038 /*class_head_p=*/false,
5039 declarator_p);
5040 if (processing_template_decl
5041 && ! cp_parser_parse_definitely (parser))
5043 /* We couldn't find a type with this name, so just accept
5044 it and check for a match at instantiation time. */
5045 type_decl = cp_parser_identifier (parser);
5046 if (type_decl != error_mark_node)
5047 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5048 return type_decl;
5051 /* If an error occurred, assume that the name of the
5052 destructor is the same as the name of the qualifying
5053 class. That allows us to keep parsing after running
5054 into ill-formed destructor names. */
5055 if (type_decl == error_mark_node && scope)
5056 return build_nt (BIT_NOT_EXPR, scope);
5057 else if (type_decl == error_mark_node)
5058 return error_mark_node;
5060 /* Check that destructor name and scope match. */
5061 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5063 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5064 error_at (token->location,
5065 "declaration of %<~%T%> as member of %qT",
5066 type_decl, scope);
5067 cp_parser_simulate_error (parser);
5068 return error_mark_node;
5071 /* [class.dtor]
5073 A typedef-name that names a class shall not be used as the
5074 identifier in the declarator for a destructor declaration. */
5075 if (declarator_p
5076 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5077 && !DECL_SELF_REFERENCE_P (type_decl)
5078 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5079 error_at (token->location,
5080 "typedef-name %qD used as destructor declarator",
5081 type_decl);
5083 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5086 case CPP_KEYWORD:
5087 if (token->keyword == RID_OPERATOR)
5089 tree id;
5091 /* This could be a template-id, so we try that first. */
5092 cp_parser_parse_tentatively (parser);
5093 /* Try a template-id. */
5094 id = cp_parser_template_id (parser, template_keyword_p,
5095 /*check_dependency_p=*/true,
5096 none_type,
5097 declarator_p);
5098 /* If that worked, we're done. */
5099 if (cp_parser_parse_definitely (parser))
5100 return id;
5101 /* We still don't know whether we're looking at an
5102 operator-function-id or a conversion-function-id. */
5103 cp_parser_parse_tentatively (parser);
5104 /* Try an operator-function-id. */
5105 id = cp_parser_operator_function_id (parser);
5106 /* If that didn't work, try a conversion-function-id. */
5107 if (!cp_parser_parse_definitely (parser))
5108 id = cp_parser_conversion_function_id (parser);
5109 else if (UDLIT_OPER_P (id))
5111 /* 17.6.3.3.5 */
5112 const char *name = UDLIT_OP_SUFFIX (id);
5113 if (name[0] != '_' && !in_system_header_at (input_location)
5114 && declarator_p)
5115 warning (0, "literal operator suffixes not preceded by %<_%>"
5116 " are reserved for future standardization");
5119 return id;
5121 /* Fall through. */
5123 default:
5124 if (optional_p)
5125 return NULL_TREE;
5126 cp_parser_error (parser, "expected unqualified-id");
5127 return error_mark_node;
5131 /* Parse an (optional) nested-name-specifier.
5133 nested-name-specifier: [C++98]
5134 class-or-namespace-name :: nested-name-specifier [opt]
5135 class-or-namespace-name :: template nested-name-specifier [opt]
5137 nested-name-specifier: [C++0x]
5138 type-name ::
5139 namespace-name ::
5140 nested-name-specifier identifier ::
5141 nested-name-specifier template [opt] simple-template-id ::
5143 PARSER->SCOPE should be set appropriately before this function is
5144 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5145 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5146 in name lookups.
5148 Sets PARSER->SCOPE to the class (TYPE) or namespace
5149 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5150 it unchanged if there is no nested-name-specifier. Returns the new
5151 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5153 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5154 part of a declaration and/or decl-specifier. */
5156 static tree
5157 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5158 bool typename_keyword_p,
5159 bool check_dependency_p,
5160 bool type_p,
5161 bool is_declaration)
5163 bool success = false;
5164 cp_token_position start = 0;
5165 cp_token *token;
5167 /* Remember where the nested-name-specifier starts. */
5168 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5170 start = cp_lexer_token_position (parser->lexer, false);
5171 push_deferring_access_checks (dk_deferred);
5174 while (true)
5176 tree new_scope;
5177 tree old_scope;
5178 tree saved_qualifying_scope;
5179 bool template_keyword_p;
5181 /* Spot cases that cannot be the beginning of a
5182 nested-name-specifier. */
5183 token = cp_lexer_peek_token (parser->lexer);
5185 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5186 the already parsed nested-name-specifier. */
5187 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5189 /* Grab the nested-name-specifier and continue the loop. */
5190 cp_parser_pre_parsed_nested_name_specifier (parser);
5191 /* If we originally encountered this nested-name-specifier
5192 with IS_DECLARATION set to false, we will not have
5193 resolved TYPENAME_TYPEs, so we must do so here. */
5194 if (is_declaration
5195 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5197 new_scope = resolve_typename_type (parser->scope,
5198 /*only_current_p=*/false);
5199 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5200 parser->scope = new_scope;
5202 success = true;
5203 continue;
5206 /* Spot cases that cannot be the beginning of a
5207 nested-name-specifier. On the second and subsequent times
5208 through the loop, we look for the `template' keyword. */
5209 if (success && token->keyword == RID_TEMPLATE)
5211 /* A template-id can start a nested-name-specifier. */
5212 else if (token->type == CPP_TEMPLATE_ID)
5214 /* DR 743: decltype can be used in a nested-name-specifier. */
5215 else if (token_is_decltype (token))
5217 else
5219 /* If the next token is not an identifier, then it is
5220 definitely not a type-name or namespace-name. */
5221 if (token->type != CPP_NAME)
5222 break;
5223 /* If the following token is neither a `<' (to begin a
5224 template-id), nor a `::', then we are not looking at a
5225 nested-name-specifier. */
5226 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5228 if (token->type == CPP_COLON
5229 && parser->colon_corrects_to_scope_p
5230 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5232 error_at (token->location,
5233 "found %<:%> in nested-name-specifier, expected %<::%>");
5234 token->type = CPP_SCOPE;
5237 if (token->type != CPP_SCOPE
5238 && !cp_parser_nth_token_starts_template_argument_list_p
5239 (parser, 2))
5240 break;
5243 /* The nested-name-specifier is optional, so we parse
5244 tentatively. */
5245 cp_parser_parse_tentatively (parser);
5247 /* Look for the optional `template' keyword, if this isn't the
5248 first time through the loop. */
5249 if (success)
5250 template_keyword_p = cp_parser_optional_template_keyword (parser);
5251 else
5252 template_keyword_p = false;
5254 /* Save the old scope since the name lookup we are about to do
5255 might destroy it. */
5256 old_scope = parser->scope;
5257 saved_qualifying_scope = parser->qualifying_scope;
5258 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5259 look up names in "X<T>::I" in order to determine that "Y" is
5260 a template. So, if we have a typename at this point, we make
5261 an effort to look through it. */
5262 if (is_declaration
5263 && !typename_keyword_p
5264 && parser->scope
5265 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5266 parser->scope = resolve_typename_type (parser->scope,
5267 /*only_current_p=*/false);
5268 /* Parse the qualifying entity. */
5269 new_scope
5270 = cp_parser_qualifying_entity (parser,
5271 typename_keyword_p,
5272 template_keyword_p,
5273 check_dependency_p,
5274 type_p,
5275 is_declaration);
5276 /* Look for the `::' token. */
5277 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5279 /* If we found what we wanted, we keep going; otherwise, we're
5280 done. */
5281 if (!cp_parser_parse_definitely (parser))
5283 bool error_p = false;
5285 /* Restore the OLD_SCOPE since it was valid before the
5286 failed attempt at finding the last
5287 class-or-namespace-name. */
5288 parser->scope = old_scope;
5289 parser->qualifying_scope = saved_qualifying_scope;
5291 /* If the next token is a decltype, and the one after that is a
5292 `::', then the decltype has failed to resolve to a class or
5293 enumeration type. Give this error even when parsing
5294 tentatively since it can't possibly be valid--and we're going
5295 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5296 won't get another chance.*/
5297 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5298 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5299 == CPP_SCOPE))
5301 token = cp_lexer_consume_token (parser->lexer);
5302 error_at (token->location, "decltype evaluates to %qT, "
5303 "which is not a class or enumeration type",
5304 token->u.value);
5305 parser->scope = error_mark_node;
5306 error_p = true;
5307 /* As below. */
5308 success = true;
5309 cp_lexer_consume_token (parser->lexer);
5312 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5313 break;
5314 /* If the next token is an identifier, and the one after
5315 that is a `::', then any valid interpretation would have
5316 found a class-or-namespace-name. */
5317 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5318 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5319 == CPP_SCOPE)
5320 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5321 != CPP_COMPL))
5323 token = cp_lexer_consume_token (parser->lexer);
5324 if (!error_p)
5326 if (!token->error_reported)
5328 tree decl;
5329 tree ambiguous_decls;
5331 decl = cp_parser_lookup_name (parser, token->u.value,
5332 none_type,
5333 /*is_template=*/false,
5334 /*is_namespace=*/false,
5335 /*check_dependency=*/true,
5336 &ambiguous_decls,
5337 token->location);
5338 if (TREE_CODE (decl) == TEMPLATE_DECL)
5339 error_at (token->location,
5340 "%qD used without template parameters",
5341 decl);
5342 else if (ambiguous_decls)
5344 // cp_parser_lookup_name has the same diagnostic,
5345 // thus make sure to emit it at most once.
5346 if (cp_parser_uncommitted_to_tentative_parse_p
5347 (parser))
5349 error_at (token->location,
5350 "reference to %qD is ambiguous",
5351 token->u.value);
5352 print_candidates (ambiguous_decls);
5354 decl = error_mark_node;
5356 else
5358 if (cxx_dialect != cxx98)
5359 cp_parser_name_lookup_error
5360 (parser, token->u.value, decl, NLE_NOT_CXX98,
5361 token->location);
5362 else
5363 cp_parser_name_lookup_error
5364 (parser, token->u.value, decl, NLE_CXX98,
5365 token->location);
5368 parser->scope = error_mark_node;
5369 error_p = true;
5370 /* Treat this as a successful nested-name-specifier
5371 due to:
5373 [basic.lookup.qual]
5375 If the name found is not a class-name (clause
5376 _class_) or namespace-name (_namespace.def_), the
5377 program is ill-formed. */
5378 success = true;
5380 cp_lexer_consume_token (parser->lexer);
5382 break;
5384 /* We've found one valid nested-name-specifier. */
5385 success = true;
5386 /* Name lookup always gives us a DECL. */
5387 if (TREE_CODE (new_scope) == TYPE_DECL)
5388 new_scope = TREE_TYPE (new_scope);
5389 /* Uses of "template" must be followed by actual templates. */
5390 if (template_keyword_p
5391 && !(CLASS_TYPE_P (new_scope)
5392 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5393 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5394 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5395 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5396 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5397 == TEMPLATE_ID_EXPR)))
5398 permerror (input_location, TYPE_P (new_scope)
5399 ? G_("%qT is not a template")
5400 : G_("%qD is not a template"),
5401 new_scope);
5402 /* If it is a class scope, try to complete it; we are about to
5403 be looking up names inside the class. */
5404 if (TYPE_P (new_scope)
5405 /* Since checking types for dependency can be expensive,
5406 avoid doing it if the type is already complete. */
5407 && !COMPLETE_TYPE_P (new_scope)
5408 /* Do not try to complete dependent types. */
5409 && !dependent_type_p (new_scope))
5411 new_scope = complete_type (new_scope);
5412 /* If it is a typedef to current class, use the current
5413 class instead, as the typedef won't have any names inside
5414 it yet. */
5415 if (!COMPLETE_TYPE_P (new_scope)
5416 && currently_open_class (new_scope))
5417 new_scope = TYPE_MAIN_VARIANT (new_scope);
5419 /* Make sure we look in the right scope the next time through
5420 the loop. */
5421 parser->scope = new_scope;
5424 /* If parsing tentatively, replace the sequence of tokens that makes
5425 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5426 token. That way, should we re-parse the token stream, we will
5427 not have to repeat the effort required to do the parse, nor will
5428 we issue duplicate error messages. */
5429 if (success && start)
5431 cp_token *token;
5433 token = cp_lexer_token_at (parser->lexer, start);
5434 /* Reset the contents of the START token. */
5435 token->type = CPP_NESTED_NAME_SPECIFIER;
5436 /* Retrieve any deferred checks. Do not pop this access checks yet
5437 so the memory will not be reclaimed during token replacing below. */
5438 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5439 token->u.tree_check_value->value = parser->scope;
5440 token->u.tree_check_value->checks = get_deferred_access_checks ();
5441 token->u.tree_check_value->qualifying_scope =
5442 parser->qualifying_scope;
5443 token->keyword = RID_MAX;
5445 /* Purge all subsequent tokens. */
5446 cp_lexer_purge_tokens_after (parser->lexer, start);
5449 if (start)
5450 pop_to_parent_deferring_access_checks ();
5452 return success ? parser->scope : NULL_TREE;
5455 /* Parse a nested-name-specifier. See
5456 cp_parser_nested_name_specifier_opt for details. This function
5457 behaves identically, except that it will an issue an error if no
5458 nested-name-specifier is present. */
5460 static tree
5461 cp_parser_nested_name_specifier (cp_parser *parser,
5462 bool typename_keyword_p,
5463 bool check_dependency_p,
5464 bool type_p,
5465 bool is_declaration)
5467 tree scope;
5469 /* Look for the nested-name-specifier. */
5470 scope = cp_parser_nested_name_specifier_opt (parser,
5471 typename_keyword_p,
5472 check_dependency_p,
5473 type_p,
5474 is_declaration);
5475 /* If it was not present, issue an error message. */
5476 if (!scope)
5478 cp_parser_error (parser, "expected nested-name-specifier");
5479 parser->scope = NULL_TREE;
5482 return scope;
5485 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5486 this is either a class-name or a namespace-name (which corresponds
5487 to the class-or-namespace-name production in the grammar). For
5488 C++0x, it can also be a type-name that refers to an enumeration
5489 type or a simple-template-id.
5491 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5492 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5493 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5494 TYPE_P is TRUE iff the next name should be taken as a class-name,
5495 even the same name is declared to be another entity in the same
5496 scope.
5498 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5499 specified by the class-or-namespace-name. If neither is found the
5500 ERROR_MARK_NODE is returned. */
5502 static tree
5503 cp_parser_qualifying_entity (cp_parser *parser,
5504 bool typename_keyword_p,
5505 bool template_keyword_p,
5506 bool check_dependency_p,
5507 bool type_p,
5508 bool is_declaration)
5510 tree saved_scope;
5511 tree saved_qualifying_scope;
5512 tree saved_object_scope;
5513 tree scope;
5514 bool only_class_p;
5515 bool successful_parse_p;
5517 /* DR 743: decltype can appear in a nested-name-specifier. */
5518 if (cp_lexer_next_token_is_decltype (parser->lexer))
5520 scope = cp_parser_decltype (parser);
5521 if (TREE_CODE (scope) != ENUMERAL_TYPE
5522 && !MAYBE_CLASS_TYPE_P (scope))
5524 cp_parser_simulate_error (parser);
5525 return error_mark_node;
5527 if (TYPE_NAME (scope))
5528 scope = TYPE_NAME (scope);
5529 return scope;
5532 /* Before we try to parse the class-name, we must save away the
5533 current PARSER->SCOPE since cp_parser_class_name will destroy
5534 it. */
5535 saved_scope = parser->scope;
5536 saved_qualifying_scope = parser->qualifying_scope;
5537 saved_object_scope = parser->object_scope;
5538 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5539 there is no need to look for a namespace-name. */
5540 only_class_p = template_keyword_p
5541 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5542 if (!only_class_p)
5543 cp_parser_parse_tentatively (parser);
5544 scope = cp_parser_class_name (parser,
5545 typename_keyword_p,
5546 template_keyword_p,
5547 type_p ? class_type : none_type,
5548 check_dependency_p,
5549 /*class_head_p=*/false,
5550 is_declaration);
5551 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5552 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5553 if (!only_class_p
5554 && cxx_dialect != cxx98
5555 && !successful_parse_p)
5557 /* Restore the saved scope. */
5558 parser->scope = saved_scope;
5559 parser->qualifying_scope = saved_qualifying_scope;
5560 parser->object_scope = saved_object_scope;
5562 /* Parse tentatively. */
5563 cp_parser_parse_tentatively (parser);
5565 /* Parse a type-name */
5566 scope = cp_parser_type_name (parser);
5568 /* "If the name found does not designate a namespace or a class,
5569 enumeration, or dependent type, the program is ill-formed."
5571 We cover classes and dependent types above and namespaces below,
5572 so this code is only looking for enums. */
5573 if (!scope || TREE_CODE (scope) != TYPE_DECL
5574 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5575 cp_parser_simulate_error (parser);
5577 successful_parse_p = cp_parser_parse_definitely (parser);
5579 /* If that didn't work, try for a namespace-name. */
5580 if (!only_class_p && !successful_parse_p)
5582 /* Restore the saved scope. */
5583 parser->scope = saved_scope;
5584 parser->qualifying_scope = saved_qualifying_scope;
5585 parser->object_scope = saved_object_scope;
5586 /* If we are not looking at an identifier followed by the scope
5587 resolution operator, then this is not part of a
5588 nested-name-specifier. (Note that this function is only used
5589 to parse the components of a nested-name-specifier.) */
5590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5591 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5592 return error_mark_node;
5593 scope = cp_parser_namespace_name (parser);
5596 return scope;
5599 /* Parse a postfix-expression.
5601 postfix-expression:
5602 primary-expression
5603 postfix-expression [ expression ]
5604 postfix-expression ( expression-list [opt] )
5605 simple-type-specifier ( expression-list [opt] )
5606 typename :: [opt] nested-name-specifier identifier
5607 ( expression-list [opt] )
5608 typename :: [opt] nested-name-specifier template [opt] template-id
5609 ( expression-list [opt] )
5610 postfix-expression . template [opt] id-expression
5611 postfix-expression -> template [opt] id-expression
5612 postfix-expression . pseudo-destructor-name
5613 postfix-expression -> pseudo-destructor-name
5614 postfix-expression ++
5615 postfix-expression --
5616 dynamic_cast < type-id > ( expression )
5617 static_cast < type-id > ( expression )
5618 reinterpret_cast < type-id > ( expression )
5619 const_cast < type-id > ( expression )
5620 typeid ( expression )
5621 typeid ( type-id )
5623 GNU Extension:
5625 postfix-expression:
5626 ( type-id ) { initializer-list , [opt] }
5628 This extension is a GNU version of the C99 compound-literal
5629 construct. (The C99 grammar uses `type-name' instead of `type-id',
5630 but they are essentially the same concept.)
5632 If ADDRESS_P is true, the postfix expression is the operand of the
5633 `&' operator. CAST_P is true if this expression is the target of a
5634 cast.
5636 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5637 class member access expressions [expr.ref].
5639 Returns a representation of the expression. */
5641 static tree
5642 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5643 bool member_access_only_p, bool decltype_p,
5644 cp_id_kind * pidk_return)
5646 cp_token *token;
5647 location_t loc;
5648 enum rid keyword;
5649 cp_id_kind idk = CP_ID_KIND_NONE;
5650 tree postfix_expression = NULL_TREE;
5651 bool is_member_access = false;
5652 int saved_in_statement = -1;
5654 /* Peek at the next token. */
5655 token = cp_lexer_peek_token (parser->lexer);
5656 loc = token->location;
5657 /* Some of the productions are determined by keywords. */
5658 keyword = token->keyword;
5659 switch (keyword)
5661 case RID_DYNCAST:
5662 case RID_STATCAST:
5663 case RID_REINTCAST:
5664 case RID_CONSTCAST:
5666 tree type;
5667 tree expression;
5668 const char *saved_message;
5669 bool saved_in_type_id_in_expr_p;
5671 /* All of these can be handled in the same way from the point
5672 of view of parsing. Begin by consuming the token
5673 identifying the cast. */
5674 cp_lexer_consume_token (parser->lexer);
5676 /* New types cannot be defined in the cast. */
5677 saved_message = parser->type_definition_forbidden_message;
5678 parser->type_definition_forbidden_message
5679 = G_("types may not be defined in casts");
5681 /* Look for the opening `<'. */
5682 cp_parser_require (parser, CPP_LESS, RT_LESS);
5683 /* Parse the type to which we are casting. */
5684 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5685 parser->in_type_id_in_expr_p = true;
5686 type = cp_parser_type_id (parser);
5687 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5688 /* Look for the closing `>'. */
5689 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5690 /* Restore the old message. */
5691 parser->type_definition_forbidden_message = saved_message;
5693 bool saved_greater_than_is_operator_p
5694 = parser->greater_than_is_operator_p;
5695 parser->greater_than_is_operator_p = true;
5697 /* And the expression which is being cast. */
5698 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5699 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5700 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5702 parser->greater_than_is_operator_p
5703 = saved_greater_than_is_operator_p;
5705 /* Only type conversions to integral or enumeration types
5706 can be used in constant-expressions. */
5707 if (!cast_valid_in_integral_constant_expression_p (type)
5708 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5709 return error_mark_node;
5711 switch (keyword)
5713 case RID_DYNCAST:
5714 postfix_expression
5715 = build_dynamic_cast (type, expression, tf_warning_or_error);
5716 break;
5717 case RID_STATCAST:
5718 postfix_expression
5719 = build_static_cast (type, expression, tf_warning_or_error);
5720 break;
5721 case RID_REINTCAST:
5722 postfix_expression
5723 = build_reinterpret_cast (type, expression,
5724 tf_warning_or_error);
5725 break;
5726 case RID_CONSTCAST:
5727 postfix_expression
5728 = build_const_cast (type, expression, tf_warning_or_error);
5729 break;
5730 default:
5731 gcc_unreachable ();
5734 break;
5736 case RID_TYPEID:
5738 tree type;
5739 const char *saved_message;
5740 bool saved_in_type_id_in_expr_p;
5742 /* Consume the `typeid' token. */
5743 cp_lexer_consume_token (parser->lexer);
5744 /* Look for the `(' token. */
5745 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5746 /* Types cannot be defined in a `typeid' expression. */
5747 saved_message = parser->type_definition_forbidden_message;
5748 parser->type_definition_forbidden_message
5749 = G_("types may not be defined in a %<typeid%> expression");
5750 /* We can't be sure yet whether we're looking at a type-id or an
5751 expression. */
5752 cp_parser_parse_tentatively (parser);
5753 /* Try a type-id first. */
5754 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5755 parser->in_type_id_in_expr_p = true;
5756 type = cp_parser_type_id (parser);
5757 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5758 /* Look for the `)' token. Otherwise, we can't be sure that
5759 we're not looking at an expression: consider `typeid (int
5760 (3))', for example. */
5761 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5762 /* If all went well, simply lookup the type-id. */
5763 if (cp_parser_parse_definitely (parser))
5764 postfix_expression = get_typeid (type, tf_warning_or_error);
5765 /* Otherwise, fall back to the expression variant. */
5766 else
5768 tree expression;
5770 /* Look for an expression. */
5771 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5772 /* Compute its typeid. */
5773 postfix_expression = build_typeid (expression, tf_warning_or_error);
5774 /* Look for the `)' token. */
5775 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5777 /* Restore the saved message. */
5778 parser->type_definition_forbidden_message = saved_message;
5779 /* `typeid' may not appear in an integral constant expression. */
5780 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5781 return error_mark_node;
5783 break;
5785 case RID_TYPENAME:
5787 tree type;
5788 /* The syntax permitted here is the same permitted for an
5789 elaborated-type-specifier. */
5790 type = cp_parser_elaborated_type_specifier (parser,
5791 /*is_friend=*/false,
5792 /*is_declaration=*/false);
5793 postfix_expression = cp_parser_functional_cast (parser, type);
5795 break;
5797 case RID_CILK_SPAWN:
5799 cp_lexer_consume_token (parser->lexer);
5800 token = cp_lexer_peek_token (parser->lexer);
5801 if (token->type == CPP_SEMICOLON)
5803 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5804 "an expression");
5805 postfix_expression = error_mark_node;
5806 break;
5808 else if (!current_function_decl)
5810 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5811 "inside a function");
5812 postfix_expression = error_mark_node;
5813 break;
5815 else
5817 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5818 saved_in_statement = parser->in_statement;
5819 parser->in_statement |= IN_CILK_SPAWN;
5821 cfun->calls_cilk_spawn = 1;
5822 postfix_expression =
5823 cp_parser_postfix_expression (parser, false, false,
5824 false, false, &idk);
5825 if (!flag_cilkplus)
5827 error_at (token->location, "-fcilkplus must be enabled to use"
5828 " %<_Cilk_spawn%>");
5829 cfun->calls_cilk_spawn = 0;
5831 else if (saved_in_statement & IN_CILK_SPAWN)
5833 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5834 "are not permitted");
5835 postfix_expression = error_mark_node;
5836 cfun->calls_cilk_spawn = 0;
5838 else
5840 postfix_expression = build_cilk_spawn (token->location,
5841 postfix_expression);
5842 if (postfix_expression != error_mark_node)
5843 SET_EXPR_LOCATION (postfix_expression, input_location);
5844 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5846 break;
5849 case RID_BUILTIN_SHUFFLE:
5851 vec<tree, va_gc> *vec;
5852 unsigned int i;
5853 tree p;
5855 cp_lexer_consume_token (parser->lexer);
5856 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5857 /*cast_p=*/false, /*allow_expansion_p=*/true,
5858 /*non_constant_p=*/NULL);
5859 if (vec == NULL)
5860 return error_mark_node;
5862 FOR_EACH_VEC_ELT (*vec, i, p)
5863 mark_exp_read (p);
5865 if (vec->length () == 2)
5866 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5867 tf_warning_or_error);
5868 else if (vec->length () == 3)
5869 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5870 tf_warning_or_error);
5871 else
5873 error_at (loc, "wrong number of arguments to "
5874 "%<__builtin_shuffle%>");
5875 return error_mark_node;
5877 break;
5880 default:
5882 tree type;
5884 /* If the next thing is a simple-type-specifier, we may be
5885 looking at a functional cast. We could also be looking at
5886 an id-expression. So, we try the functional cast, and if
5887 that doesn't work we fall back to the primary-expression. */
5888 cp_parser_parse_tentatively (parser);
5889 /* Look for the simple-type-specifier. */
5890 type = cp_parser_simple_type_specifier (parser,
5891 /*decl_specs=*/NULL,
5892 CP_PARSER_FLAGS_NONE);
5893 /* Parse the cast itself. */
5894 if (!cp_parser_error_occurred (parser))
5895 postfix_expression
5896 = cp_parser_functional_cast (parser, type);
5897 /* If that worked, we're done. */
5898 if (cp_parser_parse_definitely (parser))
5899 break;
5901 /* If the functional-cast didn't work out, try a
5902 compound-literal. */
5903 if (cp_parser_allow_gnu_extensions_p (parser)
5904 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5906 tree initializer = NULL_TREE;
5907 bool compound_literal_p;
5909 cp_parser_parse_tentatively (parser);
5910 /* Consume the `('. */
5911 cp_lexer_consume_token (parser->lexer);
5913 /* Avoid calling cp_parser_type_id pointlessly, see comment
5914 in cp_parser_cast_expression about c++/29234. */
5915 cp_lexer_save_tokens (parser->lexer);
5917 compound_literal_p
5918 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5919 /*consume_paren=*/true)
5920 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5922 /* Roll back the tokens we skipped. */
5923 cp_lexer_rollback_tokens (parser->lexer);
5925 if (!compound_literal_p)
5926 cp_parser_simulate_error (parser);
5927 else
5929 /* Parse the type. */
5930 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5931 parser->in_type_id_in_expr_p = true;
5932 type = cp_parser_type_id (parser);
5933 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5934 /* Look for the `)'. */
5935 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5938 /* If things aren't going well, there's no need to
5939 keep going. */
5940 if (!cp_parser_error_occurred (parser))
5942 bool non_constant_p;
5943 /* Parse the brace-enclosed initializer list. */
5944 initializer = cp_parser_braced_list (parser,
5945 &non_constant_p);
5947 /* If that worked, we're definitely looking at a
5948 compound-literal expression. */
5949 if (cp_parser_parse_definitely (parser))
5951 /* Warn the user that a compound literal is not
5952 allowed in standard C++. */
5953 pedwarn (input_location, OPT_Wpedantic,
5954 "ISO C++ forbids compound-literals");
5955 /* For simplicity, we disallow compound literals in
5956 constant-expressions. We could
5957 allow compound literals of integer type, whose
5958 initializer was a constant, in constant
5959 expressions. Permitting that usage, as a further
5960 extension, would not change the meaning of any
5961 currently accepted programs. (Of course, as
5962 compound literals are not part of ISO C++, the
5963 standard has nothing to say.) */
5964 if (cp_parser_non_integral_constant_expression (parser,
5965 NIC_NCC))
5967 postfix_expression = error_mark_node;
5968 break;
5970 /* Form the representation of the compound-literal. */
5971 postfix_expression
5972 = finish_compound_literal (type, initializer,
5973 tf_warning_or_error);
5974 break;
5978 /* It must be a primary-expression. */
5979 postfix_expression
5980 = cp_parser_primary_expression (parser, address_p, cast_p,
5981 /*template_arg_p=*/false,
5982 decltype_p,
5983 &idk);
5985 break;
5988 /* Note that we don't need to worry about calling build_cplus_new on a
5989 class-valued CALL_EXPR in decltype when it isn't the end of the
5990 postfix-expression; unary_complex_lvalue will take care of that for
5991 all these cases. */
5993 /* Keep looping until the postfix-expression is complete. */
5994 while (true)
5996 if (idk == CP_ID_KIND_UNQUALIFIED
5997 && identifier_p (postfix_expression)
5998 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5999 /* It is not a Koenig lookup function call. */
6000 postfix_expression
6001 = unqualified_name_lookup_error (postfix_expression);
6003 /* Peek at the next token. */
6004 token = cp_lexer_peek_token (parser->lexer);
6006 switch (token->type)
6008 case CPP_OPEN_SQUARE:
6009 if (cp_next_tokens_can_be_std_attribute_p (parser))
6011 cp_parser_error (parser,
6012 "two consecutive %<[%> shall "
6013 "only introduce an attribute");
6014 return error_mark_node;
6016 postfix_expression
6017 = cp_parser_postfix_open_square_expression (parser,
6018 postfix_expression,
6019 false,
6020 decltype_p);
6021 idk = CP_ID_KIND_NONE;
6022 is_member_access = false;
6023 break;
6025 case CPP_OPEN_PAREN:
6026 /* postfix-expression ( expression-list [opt] ) */
6028 bool koenig_p;
6029 bool is_builtin_constant_p;
6030 bool saved_integral_constant_expression_p = false;
6031 bool saved_non_integral_constant_expression_p = false;
6032 tsubst_flags_t complain = complain_flags (decltype_p);
6033 vec<tree, va_gc> *args;
6035 is_member_access = false;
6037 is_builtin_constant_p
6038 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6039 if (is_builtin_constant_p)
6041 /* The whole point of __builtin_constant_p is to allow
6042 non-constant expressions to appear as arguments. */
6043 saved_integral_constant_expression_p
6044 = parser->integral_constant_expression_p;
6045 saved_non_integral_constant_expression_p
6046 = parser->non_integral_constant_expression_p;
6047 parser->integral_constant_expression_p = false;
6049 args = (cp_parser_parenthesized_expression_list
6050 (parser, non_attr,
6051 /*cast_p=*/false, /*allow_expansion_p=*/true,
6052 /*non_constant_p=*/NULL));
6053 if (is_builtin_constant_p)
6055 parser->integral_constant_expression_p
6056 = saved_integral_constant_expression_p;
6057 parser->non_integral_constant_expression_p
6058 = saved_non_integral_constant_expression_p;
6061 if (args == NULL)
6063 postfix_expression = error_mark_node;
6064 break;
6067 /* Function calls are not permitted in
6068 constant-expressions. */
6069 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6070 && cp_parser_non_integral_constant_expression (parser,
6071 NIC_FUNC_CALL))
6073 postfix_expression = error_mark_node;
6074 release_tree_vector (args);
6075 break;
6078 koenig_p = false;
6079 if (idk == CP_ID_KIND_UNQUALIFIED
6080 || idk == CP_ID_KIND_TEMPLATE_ID)
6082 if (identifier_p (postfix_expression))
6084 if (!args->is_empty ())
6086 koenig_p = true;
6087 if (!any_type_dependent_arguments_p (args))
6088 postfix_expression
6089 = perform_koenig_lookup (postfix_expression, args,
6090 complain);
6092 else
6093 postfix_expression
6094 = unqualified_fn_lookup_error (postfix_expression);
6096 /* We do not perform argument-dependent lookup if
6097 normal lookup finds a non-function, in accordance
6098 with the expected resolution of DR 218. */
6099 else if (!args->is_empty ()
6100 && is_overloaded_fn (postfix_expression))
6102 tree fn = get_first_fn (postfix_expression);
6103 fn = STRIP_TEMPLATE (fn);
6105 /* Do not do argument dependent lookup if regular
6106 lookup finds a member function or a block-scope
6107 function declaration. [basic.lookup.argdep]/3 */
6108 if (!DECL_FUNCTION_MEMBER_P (fn)
6109 && !DECL_LOCAL_FUNCTION_P (fn))
6111 koenig_p = true;
6112 if (!any_type_dependent_arguments_p (args))
6113 postfix_expression
6114 = perform_koenig_lookup (postfix_expression, args,
6115 complain);
6120 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6122 tree instance = TREE_OPERAND (postfix_expression, 0);
6123 tree fn = TREE_OPERAND (postfix_expression, 1);
6125 if (processing_template_decl
6126 && (type_dependent_expression_p (instance)
6127 || (!BASELINK_P (fn)
6128 && TREE_CODE (fn) != FIELD_DECL)
6129 || type_dependent_expression_p (fn)
6130 || any_type_dependent_arguments_p (args)))
6132 postfix_expression
6133 = build_nt_call_vec (postfix_expression, args);
6134 release_tree_vector (args);
6135 break;
6138 if (BASELINK_P (fn))
6140 postfix_expression
6141 = (build_new_method_call
6142 (instance, fn, &args, NULL_TREE,
6143 (idk == CP_ID_KIND_QUALIFIED
6144 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6145 : LOOKUP_NORMAL),
6146 /*fn_p=*/NULL,
6147 complain));
6149 else
6150 postfix_expression
6151 = finish_call_expr (postfix_expression, &args,
6152 /*disallow_virtual=*/false,
6153 /*koenig_p=*/false,
6154 complain);
6156 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6157 || TREE_CODE (postfix_expression) == MEMBER_REF
6158 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6159 postfix_expression = (build_offset_ref_call_from_tree
6160 (postfix_expression, &args,
6161 complain));
6162 else if (idk == CP_ID_KIND_QUALIFIED)
6163 /* A call to a static class member, or a namespace-scope
6164 function. */
6165 postfix_expression
6166 = finish_call_expr (postfix_expression, &args,
6167 /*disallow_virtual=*/true,
6168 koenig_p,
6169 complain);
6170 else
6171 /* All other function calls. */
6172 postfix_expression
6173 = finish_call_expr (postfix_expression, &args,
6174 /*disallow_virtual=*/false,
6175 koenig_p,
6176 complain);
6178 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6179 idk = CP_ID_KIND_NONE;
6181 release_tree_vector (args);
6183 break;
6185 case CPP_DOT:
6186 case CPP_DEREF:
6187 /* postfix-expression . template [opt] id-expression
6188 postfix-expression . pseudo-destructor-name
6189 postfix-expression -> template [opt] id-expression
6190 postfix-expression -> pseudo-destructor-name */
6192 /* Consume the `.' or `->' operator. */
6193 cp_lexer_consume_token (parser->lexer);
6195 postfix_expression
6196 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6197 postfix_expression,
6198 false, &idk, loc);
6200 is_member_access = true;
6201 break;
6203 case CPP_PLUS_PLUS:
6204 /* postfix-expression ++ */
6205 /* Consume the `++' token. */
6206 cp_lexer_consume_token (parser->lexer);
6207 /* Generate a representation for the complete expression. */
6208 postfix_expression
6209 = finish_increment_expr (postfix_expression,
6210 POSTINCREMENT_EXPR);
6211 /* Increments may not appear in constant-expressions. */
6212 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6213 postfix_expression = error_mark_node;
6214 idk = CP_ID_KIND_NONE;
6215 is_member_access = false;
6216 break;
6218 case CPP_MINUS_MINUS:
6219 /* postfix-expression -- */
6220 /* Consume the `--' token. */
6221 cp_lexer_consume_token (parser->lexer);
6222 /* Generate a representation for the complete expression. */
6223 postfix_expression
6224 = finish_increment_expr (postfix_expression,
6225 POSTDECREMENT_EXPR);
6226 /* Decrements may not appear in constant-expressions. */
6227 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6228 postfix_expression = error_mark_node;
6229 idk = CP_ID_KIND_NONE;
6230 is_member_access = false;
6231 break;
6233 default:
6234 if (pidk_return != NULL)
6235 * pidk_return = idk;
6236 if (member_access_only_p)
6237 return is_member_access? postfix_expression : error_mark_node;
6238 else
6239 return postfix_expression;
6243 /* We should never get here. */
6244 gcc_unreachable ();
6245 return error_mark_node;
6248 /* This function parses Cilk Plus array notations. If a normal array expr. is
6249 parsed then the array index is passed back to the caller through *INIT_INDEX
6250 and the function returns a NULL_TREE. If array notation expr. is parsed,
6251 then *INIT_INDEX is ignored by the caller and the function returns
6252 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6253 error_mark_node. */
6255 static tree
6256 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6257 tree array_value)
6259 cp_token *token = NULL;
6260 tree length_index, stride = NULL_TREE, value_tree, array_type;
6261 if (!array_value || array_value == error_mark_node)
6263 cp_parser_skip_to_end_of_statement (parser);
6264 return error_mark_node;
6267 array_type = TREE_TYPE (array_value);
6269 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6270 parser->colon_corrects_to_scope_p = false;
6271 token = cp_lexer_peek_token (parser->lexer);
6273 if (!token)
6275 cp_parser_error (parser, "expected %<:%> or numeral");
6276 return error_mark_node;
6278 else if (token->type == CPP_COLON)
6280 /* Consume the ':'. */
6281 cp_lexer_consume_token (parser->lexer);
6283 /* If we are here, then we have a case like this A[:]. */
6284 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6286 cp_parser_error (parser, "expected %<]%>");
6287 cp_parser_skip_to_end_of_statement (parser);
6288 return error_mark_node;
6290 *init_index = NULL_TREE;
6291 stride = NULL_TREE;
6292 length_index = NULL_TREE;
6294 else
6296 /* If we are here, then there are three valid possibilities:
6297 1. ARRAY [ EXP ]
6298 2. ARRAY [ EXP : EXP ]
6299 3. ARRAY [ EXP : EXP : EXP ] */
6301 *init_index = cp_parser_expression (parser, false, NULL);
6302 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6304 /* This indicates that we have a normal array expression. */
6305 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6306 return NULL_TREE;
6309 /* Consume the ':'. */
6310 cp_lexer_consume_token (parser->lexer);
6311 length_index = cp_parser_expression (parser, false, NULL);
6312 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6314 cp_lexer_consume_token (parser->lexer);
6315 stride = cp_parser_expression (parser, false, NULL);
6318 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6320 if (*init_index == error_mark_node || length_index == error_mark_node
6321 || stride == error_mark_node)
6323 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6324 cp_lexer_consume_token (parser->lexer);
6325 return error_mark_node;
6327 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6329 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6330 length_index, stride, array_type);
6331 return value_tree;
6334 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6335 by cp_parser_builtin_offsetof. We're looking for
6337 postfix-expression [ expression ]
6338 postfix-expression [ braced-init-list ] (C++11)
6340 FOR_OFFSETOF is set if we're being called in that context, which
6341 changes how we deal with integer constant expressions. */
6343 static tree
6344 cp_parser_postfix_open_square_expression (cp_parser *parser,
6345 tree postfix_expression,
6346 bool for_offsetof,
6347 bool decltype_p)
6349 tree index = NULL_TREE;
6350 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6351 bool saved_greater_than_is_operator_p;
6353 /* Consume the `[' token. */
6354 cp_lexer_consume_token (parser->lexer);
6356 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6357 parser->greater_than_is_operator_p = true;
6359 /* Parse the index expression. */
6360 /* ??? For offsetof, there is a question of what to allow here. If
6361 offsetof is not being used in an integral constant expression context,
6362 then we *could* get the right answer by computing the value at runtime.
6363 If we are in an integral constant expression context, then we might
6364 could accept any constant expression; hard to say without analysis.
6365 Rather than open the barn door too wide right away, allow only integer
6366 constant expressions here. */
6367 if (for_offsetof)
6368 index = cp_parser_constant_expression (parser, false, NULL);
6369 else
6371 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6373 bool expr_nonconst_p;
6374 cp_lexer_set_source_position (parser->lexer);
6375 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6376 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6377 if (flag_cilkplus
6378 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6380 error_at (cp_lexer_peek_token (parser->lexer)->location,
6381 "braced list index is not allowed with array "
6382 "notation");
6383 cp_parser_skip_to_end_of_statement (parser);
6384 return error_mark_node;
6387 else if (flag_cilkplus)
6389 /* Here are have these two options:
6390 ARRAY[EXP : EXP] - Array notation expr with default
6391 stride of 1.
6392 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6393 stride. */
6394 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6395 postfix_expression);
6396 if (an_exp)
6397 return an_exp;
6399 else
6400 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6403 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6405 /* Look for the closing `]'. */
6406 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6408 /* Build the ARRAY_REF. */
6409 postfix_expression = grok_array_decl (loc, postfix_expression,
6410 index, decltype_p);
6412 /* When not doing offsetof, array references are not permitted in
6413 constant-expressions. */
6414 if (!for_offsetof
6415 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6416 postfix_expression = error_mark_node;
6418 return postfix_expression;
6421 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6422 by cp_parser_builtin_offsetof. We're looking for
6424 postfix-expression . template [opt] id-expression
6425 postfix-expression . pseudo-destructor-name
6426 postfix-expression -> template [opt] id-expression
6427 postfix-expression -> pseudo-destructor-name
6429 FOR_OFFSETOF is set if we're being called in that context. That sorta
6430 limits what of the above we'll actually accept, but nevermind.
6431 TOKEN_TYPE is the "." or "->" token, which will already have been
6432 removed from the stream. */
6434 static tree
6435 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6436 enum cpp_ttype token_type,
6437 tree postfix_expression,
6438 bool for_offsetof, cp_id_kind *idk,
6439 location_t location)
6441 tree name;
6442 bool dependent_p;
6443 bool pseudo_destructor_p;
6444 tree scope = NULL_TREE;
6446 /* If this is a `->' operator, dereference the pointer. */
6447 if (token_type == CPP_DEREF)
6448 postfix_expression = build_x_arrow (location, postfix_expression,
6449 tf_warning_or_error);
6450 /* Check to see whether or not the expression is type-dependent. */
6451 dependent_p = type_dependent_expression_p (postfix_expression);
6452 /* The identifier following the `->' or `.' is not qualified. */
6453 parser->scope = NULL_TREE;
6454 parser->qualifying_scope = NULL_TREE;
6455 parser->object_scope = NULL_TREE;
6456 *idk = CP_ID_KIND_NONE;
6458 /* Enter the scope corresponding to the type of the object
6459 given by the POSTFIX_EXPRESSION. */
6460 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6462 scope = TREE_TYPE (postfix_expression);
6463 /* According to the standard, no expression should ever have
6464 reference type. Unfortunately, we do not currently match
6465 the standard in this respect in that our internal representation
6466 of an expression may have reference type even when the standard
6467 says it does not. Therefore, we have to manually obtain the
6468 underlying type here. */
6469 scope = non_reference (scope);
6470 /* The type of the POSTFIX_EXPRESSION must be complete. */
6471 if (scope == unknown_type_node)
6473 error_at (location, "%qE does not have class type",
6474 postfix_expression);
6475 scope = NULL_TREE;
6477 /* Unlike the object expression in other contexts, *this is not
6478 required to be of complete type for purposes of class member
6479 access (5.2.5) outside the member function body. */
6480 else if (postfix_expression != current_class_ref
6481 && !(processing_template_decl && scope == current_class_type))
6482 scope = complete_type_or_else (scope, NULL_TREE);
6483 /* Let the name lookup machinery know that we are processing a
6484 class member access expression. */
6485 parser->context->object_type = scope;
6486 /* If something went wrong, we want to be able to discern that case,
6487 as opposed to the case where there was no SCOPE due to the type
6488 of expression being dependent. */
6489 if (!scope)
6490 scope = error_mark_node;
6491 /* If the SCOPE was erroneous, make the various semantic analysis
6492 functions exit quickly -- and without issuing additional error
6493 messages. */
6494 if (scope == error_mark_node)
6495 postfix_expression = error_mark_node;
6498 /* Assume this expression is not a pseudo-destructor access. */
6499 pseudo_destructor_p = false;
6501 /* If the SCOPE is a scalar type, then, if this is a valid program,
6502 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6503 is type dependent, it can be pseudo-destructor-name or something else.
6504 Try to parse it as pseudo-destructor-name first. */
6505 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6507 tree s;
6508 tree type;
6510 cp_parser_parse_tentatively (parser);
6511 /* Parse the pseudo-destructor-name. */
6512 s = NULL_TREE;
6513 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6514 &s, &type);
6515 if (dependent_p
6516 && (cp_parser_error_occurred (parser)
6517 || !SCALAR_TYPE_P (type)))
6518 cp_parser_abort_tentative_parse (parser);
6519 else if (cp_parser_parse_definitely (parser))
6521 pseudo_destructor_p = true;
6522 postfix_expression
6523 = finish_pseudo_destructor_expr (postfix_expression,
6524 s, type, location);
6528 if (!pseudo_destructor_p)
6530 /* If the SCOPE is not a scalar type, we are looking at an
6531 ordinary class member access expression, rather than a
6532 pseudo-destructor-name. */
6533 bool template_p;
6534 cp_token *token = cp_lexer_peek_token (parser->lexer);
6535 /* Parse the id-expression. */
6536 name = (cp_parser_id_expression
6537 (parser,
6538 cp_parser_optional_template_keyword (parser),
6539 /*check_dependency_p=*/true,
6540 &template_p,
6541 /*declarator_p=*/false,
6542 /*optional_p=*/false));
6543 /* In general, build a SCOPE_REF if the member name is qualified.
6544 However, if the name was not dependent and has already been
6545 resolved; there is no need to build the SCOPE_REF. For example;
6547 struct X { void f(); };
6548 template <typename T> void f(T* t) { t->X::f(); }
6550 Even though "t" is dependent, "X::f" is not and has been resolved
6551 to a BASELINK; there is no need to include scope information. */
6553 /* But we do need to remember that there was an explicit scope for
6554 virtual function calls. */
6555 if (parser->scope)
6556 *idk = CP_ID_KIND_QUALIFIED;
6558 /* If the name is a template-id that names a type, we will get a
6559 TYPE_DECL here. That is invalid code. */
6560 if (TREE_CODE (name) == TYPE_DECL)
6562 error_at (token->location, "invalid use of %qD", name);
6563 postfix_expression = error_mark_node;
6565 else
6567 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6569 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6571 error_at (token->location, "%<%D::%D%> is not a class member",
6572 parser->scope, name);
6573 postfix_expression = error_mark_node;
6575 else
6576 name = build_qualified_name (/*type=*/NULL_TREE,
6577 parser->scope,
6578 name,
6579 template_p);
6580 parser->scope = NULL_TREE;
6581 parser->qualifying_scope = NULL_TREE;
6582 parser->object_scope = NULL_TREE;
6584 if (parser->scope && name && BASELINK_P (name))
6585 adjust_result_of_qualified_name_lookup
6586 (name, parser->scope, scope);
6587 postfix_expression
6588 = finish_class_member_access_expr (postfix_expression, name,
6589 template_p,
6590 tf_warning_or_error);
6594 /* We no longer need to look up names in the scope of the object on
6595 the left-hand side of the `.' or `->' operator. */
6596 parser->context->object_type = NULL_TREE;
6598 /* Outside of offsetof, these operators may not appear in
6599 constant-expressions. */
6600 if (!for_offsetof
6601 && (cp_parser_non_integral_constant_expression
6602 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6603 postfix_expression = error_mark_node;
6605 return postfix_expression;
6608 /* Parse a parenthesized expression-list.
6610 expression-list:
6611 assignment-expression
6612 expression-list, assignment-expression
6614 attribute-list:
6615 expression-list
6616 identifier
6617 identifier, expression-list
6619 CAST_P is true if this expression is the target of a cast.
6621 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6622 argument pack.
6624 Returns a vector of trees. Each element is a representation of an
6625 assignment-expression. NULL is returned if the ( and or ) are
6626 missing. An empty, but allocated, vector is returned on no
6627 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6628 if we are parsing an attribute list for an attribute that wants a
6629 plain identifier argument, normal_attr for an attribute that wants
6630 an expression, or non_attr if we aren't parsing an attribute list. If
6631 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6632 not all of the expressions in the list were constant. */
6634 static vec<tree, va_gc> *
6635 cp_parser_parenthesized_expression_list (cp_parser* parser,
6636 int is_attribute_list,
6637 bool cast_p,
6638 bool allow_expansion_p,
6639 bool *non_constant_p)
6641 vec<tree, va_gc> *expression_list;
6642 bool fold_expr_p = is_attribute_list != non_attr;
6643 tree identifier = NULL_TREE;
6644 bool saved_greater_than_is_operator_p;
6646 /* Assume all the expressions will be constant. */
6647 if (non_constant_p)
6648 *non_constant_p = false;
6650 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6651 return NULL;
6653 expression_list = make_tree_vector ();
6655 /* Within a parenthesized expression, a `>' token is always
6656 the greater-than operator. */
6657 saved_greater_than_is_operator_p
6658 = parser->greater_than_is_operator_p;
6659 parser->greater_than_is_operator_p = true;
6661 /* Consume expressions until there are no more. */
6662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6663 while (true)
6665 tree expr;
6667 /* At the beginning of attribute lists, check to see if the
6668 next token is an identifier. */
6669 if (is_attribute_list == id_attr
6670 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6672 cp_token *token;
6674 /* Consume the identifier. */
6675 token = cp_lexer_consume_token (parser->lexer);
6676 /* Save the identifier. */
6677 identifier = token->u.value;
6679 else
6681 bool expr_non_constant_p;
6683 /* Parse the next assignment-expression. */
6684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6686 /* A braced-init-list. */
6687 cp_lexer_set_source_position (parser->lexer);
6688 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6689 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6690 if (non_constant_p && expr_non_constant_p)
6691 *non_constant_p = true;
6693 else if (non_constant_p)
6695 expr = (cp_parser_constant_expression
6696 (parser, /*allow_non_constant_p=*/true,
6697 &expr_non_constant_p));
6698 if (expr_non_constant_p)
6699 *non_constant_p = true;
6701 else
6702 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6704 if (fold_expr_p)
6705 expr = fold_non_dependent_expr (expr);
6707 /* If we have an ellipsis, then this is an expression
6708 expansion. */
6709 if (allow_expansion_p
6710 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6712 /* Consume the `...'. */
6713 cp_lexer_consume_token (parser->lexer);
6715 /* Build the argument pack. */
6716 expr = make_pack_expansion (expr);
6719 /* Add it to the list. We add error_mark_node
6720 expressions to the list, so that we can still tell if
6721 the correct form for a parenthesized expression-list
6722 is found. That gives better errors. */
6723 vec_safe_push (expression_list, expr);
6725 if (expr == error_mark_node)
6726 goto skip_comma;
6729 /* After the first item, attribute lists look the same as
6730 expression lists. */
6731 is_attribute_list = non_attr;
6733 get_comma:;
6734 /* If the next token isn't a `,', then we are done. */
6735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6736 break;
6738 /* Otherwise, consume the `,' and keep going. */
6739 cp_lexer_consume_token (parser->lexer);
6742 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6744 int ending;
6746 skip_comma:;
6747 /* We try and resync to an unnested comma, as that will give the
6748 user better diagnostics. */
6749 ending = cp_parser_skip_to_closing_parenthesis (parser,
6750 /*recovering=*/true,
6751 /*or_comma=*/true,
6752 /*consume_paren=*/true);
6753 if (ending < 0)
6754 goto get_comma;
6755 if (!ending)
6757 parser->greater_than_is_operator_p
6758 = saved_greater_than_is_operator_p;
6759 return NULL;
6763 parser->greater_than_is_operator_p
6764 = saved_greater_than_is_operator_p;
6766 if (identifier)
6767 vec_safe_insert (expression_list, 0, identifier);
6769 return expression_list;
6772 /* Parse a pseudo-destructor-name.
6774 pseudo-destructor-name:
6775 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6776 :: [opt] nested-name-specifier template template-id :: ~ type-name
6777 :: [opt] nested-name-specifier [opt] ~ type-name
6779 If either of the first two productions is used, sets *SCOPE to the
6780 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6781 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6782 or ERROR_MARK_NODE if the parse fails. */
6784 static void
6785 cp_parser_pseudo_destructor_name (cp_parser* parser,
6786 tree object,
6787 tree* scope,
6788 tree* type)
6790 bool nested_name_specifier_p;
6792 /* Handle ~auto. */
6793 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6794 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6795 && !type_dependent_expression_p (object))
6797 if (cxx_dialect < cxx1y)
6798 pedwarn (input_location, 0,
6799 "%<~auto%> only available with "
6800 "-std=c++1y or -std=gnu++1y");
6801 cp_lexer_consume_token (parser->lexer);
6802 cp_lexer_consume_token (parser->lexer);
6803 *scope = NULL_TREE;
6804 *type = TREE_TYPE (object);
6805 return;
6808 /* Assume that things will not work out. */
6809 *type = error_mark_node;
6811 /* Look for the optional `::' operator. */
6812 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6813 /* Look for the optional nested-name-specifier. */
6814 nested_name_specifier_p
6815 = (cp_parser_nested_name_specifier_opt (parser,
6816 /*typename_keyword_p=*/false,
6817 /*check_dependency_p=*/true,
6818 /*type_p=*/false,
6819 /*is_declaration=*/false)
6820 != NULL_TREE);
6821 /* Now, if we saw a nested-name-specifier, we might be doing the
6822 second production. */
6823 if (nested_name_specifier_p
6824 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6826 /* Consume the `template' keyword. */
6827 cp_lexer_consume_token (parser->lexer);
6828 /* Parse the template-id. */
6829 cp_parser_template_id (parser,
6830 /*template_keyword_p=*/true,
6831 /*check_dependency_p=*/false,
6832 class_type,
6833 /*is_declaration=*/true);
6834 /* Look for the `::' token. */
6835 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6837 /* If the next token is not a `~', then there might be some
6838 additional qualification. */
6839 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6841 /* At this point, we're looking for "type-name :: ~". The type-name
6842 must not be a class-name, since this is a pseudo-destructor. So,
6843 it must be either an enum-name, or a typedef-name -- both of which
6844 are just identifiers. So, we peek ahead to check that the "::"
6845 and "~" tokens are present; if they are not, then we can avoid
6846 calling type_name. */
6847 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6848 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6849 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6851 cp_parser_error (parser, "non-scalar type");
6852 return;
6855 /* Look for the type-name. */
6856 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6857 if (*scope == error_mark_node)
6858 return;
6860 /* Look for the `::' token. */
6861 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6863 else
6864 *scope = NULL_TREE;
6866 /* Look for the `~'. */
6867 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6869 /* Once we see the ~, this has to be a pseudo-destructor. */
6870 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6871 cp_parser_commit_to_topmost_tentative_parse (parser);
6873 /* Look for the type-name again. We are not responsible for
6874 checking that it matches the first type-name. */
6875 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6878 /* Parse a unary-expression.
6880 unary-expression:
6881 postfix-expression
6882 ++ cast-expression
6883 -- cast-expression
6884 unary-operator cast-expression
6885 sizeof unary-expression
6886 sizeof ( type-id )
6887 alignof ( type-id ) [C++0x]
6888 new-expression
6889 delete-expression
6891 GNU Extensions:
6893 unary-expression:
6894 __extension__ cast-expression
6895 __alignof__ unary-expression
6896 __alignof__ ( type-id )
6897 alignof unary-expression [C++0x]
6898 __real__ cast-expression
6899 __imag__ cast-expression
6900 && identifier
6901 sizeof ( type-id ) { initializer-list , [opt] }
6902 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6903 __alignof__ ( type-id ) { initializer-list , [opt] }
6905 ADDRESS_P is true iff the unary-expression is appearing as the
6906 operand of the `&' operator. CAST_P is true if this expression is
6907 the target of a cast.
6909 Returns a representation of the expression. */
6911 static tree
6912 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6913 bool decltype_p, cp_id_kind * pidk)
6915 cp_token *token;
6916 enum tree_code unary_operator;
6918 /* Peek at the next token. */
6919 token = cp_lexer_peek_token (parser->lexer);
6920 /* Some keywords give away the kind of expression. */
6921 if (token->type == CPP_KEYWORD)
6923 enum rid keyword = token->keyword;
6925 switch (keyword)
6927 case RID_ALIGNOF:
6928 case RID_SIZEOF:
6930 tree operand, ret;
6931 enum tree_code op;
6932 location_t first_loc;
6934 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6935 /* Consume the token. */
6936 cp_lexer_consume_token (parser->lexer);
6937 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6938 /* Parse the operand. */
6939 operand = cp_parser_sizeof_operand (parser, keyword);
6941 if (TYPE_P (operand))
6942 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6943 else
6945 /* ISO C++ defines alignof only with types, not with
6946 expressions. So pedwarn if alignof is used with a non-
6947 type expression. However, __alignof__ is ok. */
6948 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6949 pedwarn (token->location, OPT_Wpedantic,
6950 "ISO C++ does not allow %<alignof%> "
6951 "with a non-type");
6953 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6955 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6956 SIZEOF_EXPR with the original operand. */
6957 if (op == SIZEOF_EXPR && ret != error_mark_node)
6959 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6961 if (!processing_template_decl && TYPE_P (operand))
6963 ret = build_min (SIZEOF_EXPR, size_type_node,
6964 build1 (NOP_EXPR, operand,
6965 error_mark_node));
6966 SIZEOF_EXPR_TYPE_P (ret) = 1;
6968 else
6969 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6970 TREE_SIDE_EFFECTS (ret) = 0;
6971 TREE_READONLY (ret) = 1;
6973 SET_EXPR_LOCATION (ret, first_loc);
6975 return ret;
6978 case RID_NEW:
6979 return cp_parser_new_expression (parser);
6981 case RID_DELETE:
6982 return cp_parser_delete_expression (parser);
6984 case RID_EXTENSION:
6986 /* The saved value of the PEDANTIC flag. */
6987 int saved_pedantic;
6988 tree expr;
6990 /* Save away the PEDANTIC flag. */
6991 cp_parser_extension_opt (parser, &saved_pedantic);
6992 /* Parse the cast-expression. */
6993 expr = cp_parser_simple_cast_expression (parser);
6994 /* Restore the PEDANTIC flag. */
6995 pedantic = saved_pedantic;
6997 return expr;
7000 case RID_REALPART:
7001 case RID_IMAGPART:
7003 tree expression;
7005 /* Consume the `__real__' or `__imag__' token. */
7006 cp_lexer_consume_token (parser->lexer);
7007 /* Parse the cast-expression. */
7008 expression = cp_parser_simple_cast_expression (parser);
7009 /* Create the complete representation. */
7010 return build_x_unary_op (token->location,
7011 (keyword == RID_REALPART
7012 ? REALPART_EXPR : IMAGPART_EXPR),
7013 expression,
7014 tf_warning_or_error);
7016 break;
7018 case RID_TRANSACTION_ATOMIC:
7019 case RID_TRANSACTION_RELAXED:
7020 return cp_parser_transaction_expression (parser, keyword);
7022 case RID_NOEXCEPT:
7024 tree expr;
7025 const char *saved_message;
7026 bool saved_integral_constant_expression_p;
7027 bool saved_non_integral_constant_expression_p;
7028 bool saved_greater_than_is_operator_p;
7030 cp_lexer_consume_token (parser->lexer);
7031 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7033 saved_message = parser->type_definition_forbidden_message;
7034 parser->type_definition_forbidden_message
7035 = G_("types may not be defined in %<noexcept%> expressions");
7037 saved_integral_constant_expression_p
7038 = parser->integral_constant_expression_p;
7039 saved_non_integral_constant_expression_p
7040 = parser->non_integral_constant_expression_p;
7041 parser->integral_constant_expression_p = false;
7043 saved_greater_than_is_operator_p
7044 = parser->greater_than_is_operator_p;
7045 parser->greater_than_is_operator_p = true;
7047 ++cp_unevaluated_operand;
7048 ++c_inhibit_evaluation_warnings;
7049 expr = cp_parser_expression (parser, false, NULL);
7050 --c_inhibit_evaluation_warnings;
7051 --cp_unevaluated_operand;
7053 parser->greater_than_is_operator_p
7054 = saved_greater_than_is_operator_p;
7056 parser->integral_constant_expression_p
7057 = saved_integral_constant_expression_p;
7058 parser->non_integral_constant_expression_p
7059 = saved_non_integral_constant_expression_p;
7061 parser->type_definition_forbidden_message = saved_message;
7063 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7064 return finish_noexcept_expr (expr, tf_warning_or_error);
7067 default:
7068 break;
7072 /* Look for the `:: new' and `:: delete', which also signal the
7073 beginning of a new-expression, or delete-expression,
7074 respectively. If the next token is `::', then it might be one of
7075 these. */
7076 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7078 enum rid keyword;
7080 /* See if the token after the `::' is one of the keywords in
7081 which we're interested. */
7082 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7083 /* If it's `new', we have a new-expression. */
7084 if (keyword == RID_NEW)
7085 return cp_parser_new_expression (parser);
7086 /* Similarly, for `delete'. */
7087 else if (keyword == RID_DELETE)
7088 return cp_parser_delete_expression (parser);
7091 /* Look for a unary operator. */
7092 unary_operator = cp_parser_unary_operator (token);
7093 /* The `++' and `--' operators can be handled similarly, even though
7094 they are not technically unary-operators in the grammar. */
7095 if (unary_operator == ERROR_MARK)
7097 if (token->type == CPP_PLUS_PLUS)
7098 unary_operator = PREINCREMENT_EXPR;
7099 else if (token->type == CPP_MINUS_MINUS)
7100 unary_operator = PREDECREMENT_EXPR;
7101 /* Handle the GNU address-of-label extension. */
7102 else if (cp_parser_allow_gnu_extensions_p (parser)
7103 && token->type == CPP_AND_AND)
7105 tree identifier;
7106 tree expression;
7107 location_t loc = token->location;
7109 /* Consume the '&&' token. */
7110 cp_lexer_consume_token (parser->lexer);
7111 /* Look for the identifier. */
7112 identifier = cp_parser_identifier (parser);
7113 /* Create an expression representing the address. */
7114 expression = finish_label_address_expr (identifier, loc);
7115 if (cp_parser_non_integral_constant_expression (parser,
7116 NIC_ADDR_LABEL))
7117 expression = error_mark_node;
7118 return expression;
7121 if (unary_operator != ERROR_MARK)
7123 tree cast_expression;
7124 tree expression = error_mark_node;
7125 non_integral_constant non_constant_p = NIC_NONE;
7126 location_t loc = token->location;
7127 tsubst_flags_t complain = complain_flags (decltype_p);
7129 /* Consume the operator token. */
7130 token = cp_lexer_consume_token (parser->lexer);
7131 /* Parse the cast-expression. */
7132 cast_expression
7133 = cp_parser_cast_expression (parser,
7134 unary_operator == ADDR_EXPR,
7135 /*cast_p=*/false,
7136 /*decltype*/false,
7137 pidk);
7138 /* Now, build an appropriate representation. */
7139 switch (unary_operator)
7141 case INDIRECT_REF:
7142 non_constant_p = NIC_STAR;
7143 expression = build_x_indirect_ref (loc, cast_expression,
7144 RO_UNARY_STAR,
7145 complain);
7146 break;
7148 case ADDR_EXPR:
7149 non_constant_p = NIC_ADDR;
7150 /* Fall through. */
7151 case BIT_NOT_EXPR:
7152 expression = build_x_unary_op (loc, unary_operator,
7153 cast_expression,
7154 complain);
7155 break;
7157 case PREINCREMENT_EXPR:
7158 case PREDECREMENT_EXPR:
7159 non_constant_p = unary_operator == PREINCREMENT_EXPR
7160 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7161 /* Fall through. */
7162 case UNARY_PLUS_EXPR:
7163 case NEGATE_EXPR:
7164 case TRUTH_NOT_EXPR:
7165 expression = finish_unary_op_expr (loc, unary_operator,
7166 cast_expression, complain);
7167 break;
7169 default:
7170 gcc_unreachable ();
7173 if (non_constant_p != NIC_NONE
7174 && cp_parser_non_integral_constant_expression (parser,
7175 non_constant_p))
7176 expression = error_mark_node;
7178 return expression;
7181 return cp_parser_postfix_expression (parser, address_p, cast_p,
7182 /*member_access_only_p=*/false,
7183 decltype_p,
7184 pidk);
7187 static inline tree
7188 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7189 cp_id_kind * pidk)
7191 return cp_parser_unary_expression (parser, address_p, cast_p,
7192 /*decltype*/false, pidk);
7195 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7196 unary-operator, the corresponding tree code is returned. */
7198 static enum tree_code
7199 cp_parser_unary_operator (cp_token* token)
7201 switch (token->type)
7203 case CPP_MULT:
7204 return INDIRECT_REF;
7206 case CPP_AND:
7207 return ADDR_EXPR;
7209 case CPP_PLUS:
7210 return UNARY_PLUS_EXPR;
7212 case CPP_MINUS:
7213 return NEGATE_EXPR;
7215 case CPP_NOT:
7216 return TRUTH_NOT_EXPR;
7218 case CPP_COMPL:
7219 return BIT_NOT_EXPR;
7221 default:
7222 return ERROR_MARK;
7226 /* Parse a new-expression.
7228 new-expression:
7229 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7230 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7232 Returns a representation of the expression. */
7234 static tree
7235 cp_parser_new_expression (cp_parser* parser)
7237 bool global_scope_p;
7238 vec<tree, va_gc> *placement;
7239 tree type;
7240 vec<tree, va_gc> *initializer;
7241 tree nelts = NULL_TREE;
7242 tree ret;
7244 /* Look for the optional `::' operator. */
7245 global_scope_p
7246 = (cp_parser_global_scope_opt (parser,
7247 /*current_scope_valid_p=*/false)
7248 != NULL_TREE);
7249 /* Look for the `new' operator. */
7250 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7251 /* There's no easy way to tell a new-placement from the
7252 `( type-id )' construct. */
7253 cp_parser_parse_tentatively (parser);
7254 /* Look for a new-placement. */
7255 placement = cp_parser_new_placement (parser);
7256 /* If that didn't work out, there's no new-placement. */
7257 if (!cp_parser_parse_definitely (parser))
7259 if (placement != NULL)
7260 release_tree_vector (placement);
7261 placement = NULL;
7264 /* If the next token is a `(', then we have a parenthesized
7265 type-id. */
7266 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7268 cp_token *token;
7269 const char *saved_message = parser->type_definition_forbidden_message;
7271 /* Consume the `('. */
7272 cp_lexer_consume_token (parser->lexer);
7274 /* Parse the type-id. */
7275 parser->type_definition_forbidden_message
7276 = G_("types may not be defined in a new-expression");
7277 type = cp_parser_type_id (parser);
7278 parser->type_definition_forbidden_message = saved_message;
7280 /* Look for the closing `)'. */
7281 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7282 token = cp_lexer_peek_token (parser->lexer);
7283 /* There should not be a direct-new-declarator in this production,
7284 but GCC used to allowed this, so we check and emit a sensible error
7285 message for this case. */
7286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7288 error_at (token->location,
7289 "array bound forbidden after parenthesized type-id");
7290 inform (token->location,
7291 "try removing the parentheses around the type-id");
7292 cp_parser_direct_new_declarator (parser);
7295 /* Otherwise, there must be a new-type-id. */
7296 else
7297 type = cp_parser_new_type_id (parser, &nelts);
7299 /* If the next token is a `(' or '{', then we have a new-initializer. */
7300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7301 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7302 initializer = cp_parser_new_initializer (parser);
7303 else
7304 initializer = NULL;
7306 /* A new-expression may not appear in an integral constant
7307 expression. */
7308 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7309 ret = error_mark_node;
7310 else
7312 /* Create a representation of the new-expression. */
7313 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7314 tf_warning_or_error);
7317 if (placement != NULL)
7318 release_tree_vector (placement);
7319 if (initializer != NULL)
7320 release_tree_vector (initializer);
7322 return ret;
7325 /* Parse a new-placement.
7327 new-placement:
7328 ( expression-list )
7330 Returns the same representation as for an expression-list. */
7332 static vec<tree, va_gc> *
7333 cp_parser_new_placement (cp_parser* parser)
7335 vec<tree, va_gc> *expression_list;
7337 /* Parse the expression-list. */
7338 expression_list = (cp_parser_parenthesized_expression_list
7339 (parser, non_attr, /*cast_p=*/false,
7340 /*allow_expansion_p=*/true,
7341 /*non_constant_p=*/NULL));
7343 return expression_list;
7346 /* Parse a new-type-id.
7348 new-type-id:
7349 type-specifier-seq new-declarator [opt]
7351 Returns the TYPE allocated. If the new-type-id indicates an array
7352 type, *NELTS is set to the number of elements in the last array
7353 bound; the TYPE will not include the last array bound. */
7355 static tree
7356 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7358 cp_decl_specifier_seq type_specifier_seq;
7359 cp_declarator *new_declarator;
7360 cp_declarator *declarator;
7361 cp_declarator *outer_declarator;
7362 const char *saved_message;
7364 /* The type-specifier sequence must not contain type definitions.
7365 (It cannot contain declarations of new types either, but if they
7366 are not definitions we will catch that because they are not
7367 complete.) */
7368 saved_message = parser->type_definition_forbidden_message;
7369 parser->type_definition_forbidden_message
7370 = G_("types may not be defined in a new-type-id");
7371 /* Parse the type-specifier-seq. */
7372 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7373 /*is_trailing_return=*/false,
7374 &type_specifier_seq);
7375 /* Restore the old message. */
7376 parser->type_definition_forbidden_message = saved_message;
7378 if (type_specifier_seq.type == error_mark_node)
7379 return error_mark_node;
7381 /* Parse the new-declarator. */
7382 new_declarator = cp_parser_new_declarator_opt (parser);
7384 /* Determine the number of elements in the last array dimension, if
7385 any. */
7386 *nelts = NULL_TREE;
7387 /* Skip down to the last array dimension. */
7388 declarator = new_declarator;
7389 outer_declarator = NULL;
7390 while (declarator && (declarator->kind == cdk_pointer
7391 || declarator->kind == cdk_ptrmem))
7393 outer_declarator = declarator;
7394 declarator = declarator->declarator;
7396 while (declarator
7397 && declarator->kind == cdk_array
7398 && declarator->declarator
7399 && declarator->declarator->kind == cdk_array)
7401 outer_declarator = declarator;
7402 declarator = declarator->declarator;
7405 if (declarator && declarator->kind == cdk_array)
7407 *nelts = declarator->u.array.bounds;
7408 if (*nelts == error_mark_node)
7409 *nelts = integer_one_node;
7411 if (outer_declarator)
7412 outer_declarator->declarator = declarator->declarator;
7413 else
7414 new_declarator = NULL;
7417 return groktypename (&type_specifier_seq, new_declarator, false);
7420 /* Parse an (optional) new-declarator.
7422 new-declarator:
7423 ptr-operator new-declarator [opt]
7424 direct-new-declarator
7426 Returns the declarator. */
7428 static cp_declarator *
7429 cp_parser_new_declarator_opt (cp_parser* parser)
7431 enum tree_code code;
7432 tree type, std_attributes = NULL_TREE;
7433 cp_cv_quals cv_quals;
7435 /* We don't know if there's a ptr-operator next, or not. */
7436 cp_parser_parse_tentatively (parser);
7437 /* Look for a ptr-operator. */
7438 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7439 /* If that worked, look for more new-declarators. */
7440 if (cp_parser_parse_definitely (parser))
7442 cp_declarator *declarator;
7444 /* Parse another optional declarator. */
7445 declarator = cp_parser_new_declarator_opt (parser);
7447 declarator = cp_parser_make_indirect_declarator
7448 (code, type, cv_quals, declarator, std_attributes);
7450 return declarator;
7453 /* If the next token is a `[', there is a direct-new-declarator. */
7454 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7455 return cp_parser_direct_new_declarator (parser);
7457 return NULL;
7460 /* Parse a direct-new-declarator.
7462 direct-new-declarator:
7463 [ expression ]
7464 direct-new-declarator [constant-expression]
7468 static cp_declarator *
7469 cp_parser_direct_new_declarator (cp_parser* parser)
7471 cp_declarator *declarator = NULL;
7473 while (true)
7475 tree expression;
7476 cp_token *token;
7478 /* Look for the opening `['. */
7479 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7481 token = cp_lexer_peek_token (parser->lexer);
7482 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7483 /* The standard requires that the expression have integral
7484 type. DR 74 adds enumeration types. We believe that the
7485 real intent is that these expressions be handled like the
7486 expression in a `switch' condition, which also allows
7487 classes with a single conversion to integral or
7488 enumeration type. */
7489 if (!processing_template_decl)
7491 expression
7492 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7493 expression,
7494 /*complain=*/true);
7495 if (!expression)
7497 error_at (token->location,
7498 "expression in new-declarator must have integral "
7499 "or enumeration type");
7500 expression = error_mark_node;
7504 /* Look for the closing `]'. */
7505 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7507 /* Add this bound to the declarator. */
7508 declarator = make_array_declarator (declarator, expression);
7510 /* If the next token is not a `[', then there are no more
7511 bounds. */
7512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7513 break;
7516 return declarator;
7519 /* Parse a new-initializer.
7521 new-initializer:
7522 ( expression-list [opt] )
7523 braced-init-list
7525 Returns a representation of the expression-list. */
7527 static vec<tree, va_gc> *
7528 cp_parser_new_initializer (cp_parser* parser)
7530 vec<tree, va_gc> *expression_list;
7532 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7534 tree t;
7535 bool expr_non_constant_p;
7536 cp_lexer_set_source_position (parser->lexer);
7537 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7538 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7539 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7540 expression_list = make_tree_vector_single (t);
7542 else
7543 expression_list = (cp_parser_parenthesized_expression_list
7544 (parser, non_attr, /*cast_p=*/false,
7545 /*allow_expansion_p=*/true,
7546 /*non_constant_p=*/NULL));
7548 return expression_list;
7551 /* Parse a delete-expression.
7553 delete-expression:
7554 :: [opt] delete cast-expression
7555 :: [opt] delete [ ] cast-expression
7557 Returns a representation of the expression. */
7559 static tree
7560 cp_parser_delete_expression (cp_parser* parser)
7562 bool global_scope_p;
7563 bool array_p;
7564 tree expression;
7566 /* Look for the optional `::' operator. */
7567 global_scope_p
7568 = (cp_parser_global_scope_opt (parser,
7569 /*current_scope_valid_p=*/false)
7570 != NULL_TREE);
7571 /* Look for the `delete' keyword. */
7572 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7573 /* See if the array syntax is in use. */
7574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7576 /* Consume the `[' token. */
7577 cp_lexer_consume_token (parser->lexer);
7578 /* Look for the `]' token. */
7579 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7580 /* Remember that this is the `[]' construct. */
7581 array_p = true;
7583 else
7584 array_p = false;
7586 /* Parse the cast-expression. */
7587 expression = cp_parser_simple_cast_expression (parser);
7589 /* A delete-expression may not appear in an integral constant
7590 expression. */
7591 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7592 return error_mark_node;
7594 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7595 tf_warning_or_error);
7598 /* Returns true if TOKEN may start a cast-expression and false
7599 otherwise. */
7601 static bool
7602 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7604 cp_token *token = cp_lexer_peek_token (parser->lexer);
7605 switch (token->type)
7607 case CPP_COMMA:
7608 case CPP_SEMICOLON:
7609 case CPP_QUERY:
7610 case CPP_COLON:
7611 case CPP_CLOSE_SQUARE:
7612 case CPP_CLOSE_PAREN:
7613 case CPP_CLOSE_BRACE:
7614 case CPP_OPEN_BRACE:
7615 case CPP_DOT:
7616 case CPP_DOT_STAR:
7617 case CPP_DEREF:
7618 case CPP_DEREF_STAR:
7619 case CPP_DIV:
7620 case CPP_MOD:
7621 case CPP_LSHIFT:
7622 case CPP_RSHIFT:
7623 case CPP_LESS:
7624 case CPP_GREATER:
7625 case CPP_LESS_EQ:
7626 case CPP_GREATER_EQ:
7627 case CPP_EQ_EQ:
7628 case CPP_NOT_EQ:
7629 case CPP_EQ:
7630 case CPP_MULT_EQ:
7631 case CPP_DIV_EQ:
7632 case CPP_MOD_EQ:
7633 case CPP_PLUS_EQ:
7634 case CPP_MINUS_EQ:
7635 case CPP_RSHIFT_EQ:
7636 case CPP_LSHIFT_EQ:
7637 case CPP_AND_EQ:
7638 case CPP_XOR_EQ:
7639 case CPP_OR_EQ:
7640 case CPP_XOR:
7641 case CPP_OR:
7642 case CPP_OR_OR:
7643 case CPP_EOF:
7644 return false;
7646 case CPP_OPEN_PAREN:
7647 /* In ((type ()) () the last () isn't a valid cast-expression,
7648 so the whole must be parsed as postfix-expression. */
7649 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7650 != CPP_CLOSE_PAREN;
7652 /* '[' may start a primary-expression in obj-c++. */
7653 case CPP_OPEN_SQUARE:
7654 return c_dialect_objc ();
7656 default:
7657 return true;
7661 /* Parse a cast-expression.
7663 cast-expression:
7664 unary-expression
7665 ( type-id ) cast-expression
7667 ADDRESS_P is true iff the unary-expression is appearing as the
7668 operand of the `&' operator. CAST_P is true if this expression is
7669 the target of a cast.
7671 Returns a representation of the expression. */
7673 static tree
7674 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7675 bool decltype_p, cp_id_kind * pidk)
7677 /* If it's a `(', then we might be looking at a cast. */
7678 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7680 tree type = NULL_TREE;
7681 tree expr = NULL_TREE;
7682 bool cast_expression_p;
7683 const char *saved_message;
7685 /* There's no way to know yet whether or not this is a cast.
7686 For example, `(int (3))' is a unary-expression, while `(int)
7687 3' is a cast. So, we resort to parsing tentatively. */
7688 cp_parser_parse_tentatively (parser);
7689 /* Types may not be defined in a cast. */
7690 saved_message = parser->type_definition_forbidden_message;
7691 parser->type_definition_forbidden_message
7692 = G_("types may not be defined in casts");
7693 /* Consume the `('. */
7694 cp_lexer_consume_token (parser->lexer);
7695 /* A very tricky bit is that `(struct S) { 3 }' is a
7696 compound-literal (which we permit in C++ as an extension).
7697 But, that construct is not a cast-expression -- it is a
7698 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7699 is legal; if the compound-literal were a cast-expression,
7700 you'd need an extra set of parentheses.) But, if we parse
7701 the type-id, and it happens to be a class-specifier, then we
7702 will commit to the parse at that point, because we cannot
7703 undo the action that is done when creating a new class. So,
7704 then we cannot back up and do a postfix-expression.
7705 Another tricky case is the following (c++/29234):
7707 struct S { void operator () (); };
7709 void foo ()
7711 ( S()() );
7714 As a type-id we parse the parenthesized S()() as a function
7715 returning a function, groktypename complains and we cannot
7716 back up in this case either.
7718 Therefore, we scan ahead to the closing `)', and check to see
7719 if the tokens after the `)' can start a cast-expression. Otherwise
7720 we are dealing with an unary-expression, a postfix-expression
7721 or something else.
7723 Save tokens so that we can put them back. */
7724 cp_lexer_save_tokens (parser->lexer);
7726 /* We may be looking at a cast-expression. */
7727 cast_expression_p
7728 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7729 /*consume_paren=*/true)
7730 && cp_parser_tokens_start_cast_expression (parser));
7732 /* Roll back the tokens we skipped. */
7733 cp_lexer_rollback_tokens (parser->lexer);
7734 /* If we aren't looking at a cast-expression, simulate an error so
7735 that the call to cp_parser_parse_definitely below will fail. */
7736 if (!cast_expression_p)
7737 cp_parser_simulate_error (parser);
7738 else
7740 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7741 parser->in_type_id_in_expr_p = true;
7742 /* Look for the type-id. */
7743 type = cp_parser_type_id (parser);
7744 /* Look for the closing `)'. */
7745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7746 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7749 /* Restore the saved message. */
7750 parser->type_definition_forbidden_message = saved_message;
7752 /* At this point this can only be either a cast or a
7753 parenthesized ctor such as `(T ())' that looks like a cast to
7754 function returning T. */
7755 if (!cp_parser_error_occurred (parser))
7757 cp_parser_parse_definitely (parser);
7758 expr = cp_parser_cast_expression (parser,
7759 /*address_p=*/false,
7760 /*cast_p=*/true,
7761 /*decltype_p=*/false,
7762 pidk);
7764 /* Warn about old-style casts, if so requested. */
7765 if (warn_old_style_cast
7766 && !in_system_header_at (input_location)
7767 && !VOID_TYPE_P (type)
7768 && current_lang_name != lang_name_c)
7769 warning (OPT_Wold_style_cast, "use of old-style cast");
7771 /* Only type conversions to integral or enumeration types
7772 can be used in constant-expressions. */
7773 if (!cast_valid_in_integral_constant_expression_p (type)
7774 && cp_parser_non_integral_constant_expression (parser,
7775 NIC_CAST))
7776 return error_mark_node;
7778 /* Perform the cast. */
7779 expr = build_c_cast (input_location, type, expr);
7780 return expr;
7782 else
7783 cp_parser_abort_tentative_parse (parser);
7786 /* If we get here, then it's not a cast, so it must be a
7787 unary-expression. */
7788 return cp_parser_unary_expression (parser, address_p, cast_p,
7789 decltype_p, pidk);
7792 /* Parse a binary expression of the general form:
7794 pm-expression:
7795 cast-expression
7796 pm-expression .* cast-expression
7797 pm-expression ->* cast-expression
7799 multiplicative-expression:
7800 pm-expression
7801 multiplicative-expression * pm-expression
7802 multiplicative-expression / pm-expression
7803 multiplicative-expression % pm-expression
7805 additive-expression:
7806 multiplicative-expression
7807 additive-expression + multiplicative-expression
7808 additive-expression - multiplicative-expression
7810 shift-expression:
7811 additive-expression
7812 shift-expression << additive-expression
7813 shift-expression >> additive-expression
7815 relational-expression:
7816 shift-expression
7817 relational-expression < shift-expression
7818 relational-expression > shift-expression
7819 relational-expression <= shift-expression
7820 relational-expression >= shift-expression
7822 GNU Extension:
7824 relational-expression:
7825 relational-expression <? shift-expression
7826 relational-expression >? shift-expression
7828 equality-expression:
7829 relational-expression
7830 equality-expression == relational-expression
7831 equality-expression != relational-expression
7833 and-expression:
7834 equality-expression
7835 and-expression & equality-expression
7837 exclusive-or-expression:
7838 and-expression
7839 exclusive-or-expression ^ and-expression
7841 inclusive-or-expression:
7842 exclusive-or-expression
7843 inclusive-or-expression | exclusive-or-expression
7845 logical-and-expression:
7846 inclusive-or-expression
7847 logical-and-expression && inclusive-or-expression
7849 logical-or-expression:
7850 logical-and-expression
7851 logical-or-expression || logical-and-expression
7853 All these are implemented with a single function like:
7855 binary-expression:
7856 simple-cast-expression
7857 binary-expression <token> binary-expression
7859 CAST_P is true if this expression is the target of a cast.
7861 The binops_by_token map is used to get the tree codes for each <token> type.
7862 binary-expressions are associated according to a precedence table. */
7864 #define TOKEN_PRECEDENCE(token) \
7865 (((token->type == CPP_GREATER \
7866 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7867 && !parser->greater_than_is_operator_p) \
7868 ? PREC_NOT_OPERATOR \
7869 : binops_by_token[token->type].prec)
7871 static tree
7872 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7873 bool no_toplevel_fold_p,
7874 bool decltype_p,
7875 enum cp_parser_prec prec,
7876 cp_id_kind * pidk)
7878 cp_parser_expression_stack stack;
7879 cp_parser_expression_stack_entry *sp = &stack[0];
7880 cp_parser_expression_stack_entry current;
7881 tree rhs;
7882 cp_token *token;
7883 enum tree_code rhs_type;
7884 enum cp_parser_prec new_prec, lookahead_prec;
7885 tree overload;
7886 bool parenthesized_not_lhs_warn
7887 = cp_lexer_next_token_is (parser->lexer, CPP_NOT);
7889 /* Parse the first expression. */
7890 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7891 cast_p, decltype_p, pidk);
7892 current.lhs_type = ERROR_MARK;
7893 current.prec = prec;
7895 if (cp_parser_error_occurred (parser))
7896 return error_mark_node;
7898 for (;;)
7900 /* Get an operator token. */
7901 token = cp_lexer_peek_token (parser->lexer);
7903 if (warn_cxx0x_compat
7904 && token->type == CPP_RSHIFT
7905 && !parser->greater_than_is_operator_p)
7907 if (warning_at (token->location, OPT_Wc__0x_compat,
7908 "%<>>%> operator is treated"
7909 " as two right angle brackets in C++11"))
7910 inform (token->location,
7911 "suggest parentheses around %<>>%> expression");
7914 new_prec = TOKEN_PRECEDENCE (token);
7916 /* Popping an entry off the stack means we completed a subexpression:
7917 - either we found a token which is not an operator (`>' where it is not
7918 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7919 will happen repeatedly;
7920 - or, we found an operator which has lower priority. This is the case
7921 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7922 parsing `3 * 4'. */
7923 if (new_prec <= current.prec)
7925 if (sp == stack)
7926 break;
7927 else
7928 goto pop;
7931 get_rhs:
7932 current.tree_type = binops_by_token[token->type].tree_type;
7933 current.loc = token->location;
7935 /* We used the operator token. */
7936 cp_lexer_consume_token (parser->lexer);
7938 /* For "false && x" or "true || x", x will never be executed;
7939 disable warnings while evaluating it. */
7940 if (current.tree_type == TRUTH_ANDIF_EXPR)
7941 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7942 else if (current.tree_type == TRUTH_ORIF_EXPR)
7943 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7945 /* Extract another operand. It may be the RHS of this expression
7946 or the LHS of a new, higher priority expression. */
7947 rhs = cp_parser_simple_cast_expression (parser);
7948 rhs_type = ERROR_MARK;
7950 /* Get another operator token. Look up its precedence to avoid
7951 building a useless (immediately popped) stack entry for common
7952 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7953 token = cp_lexer_peek_token (parser->lexer);
7954 lookahead_prec = TOKEN_PRECEDENCE (token);
7955 if (lookahead_prec > new_prec)
7957 /* ... and prepare to parse the RHS of the new, higher priority
7958 expression. Since precedence levels on the stack are
7959 monotonically increasing, we do not have to care about
7960 stack overflows. */
7961 *sp = current;
7962 ++sp;
7963 current.lhs = rhs;
7964 current.lhs_type = rhs_type;
7965 current.prec = new_prec;
7966 new_prec = lookahead_prec;
7967 goto get_rhs;
7969 pop:
7970 lookahead_prec = new_prec;
7971 /* If the stack is not empty, we have parsed into LHS the right side
7972 (`4' in the example above) of an expression we had suspended.
7973 We can use the information on the stack to recover the LHS (`3')
7974 from the stack together with the tree code (`MULT_EXPR'), and
7975 the precedence of the higher level subexpression
7976 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7977 which will be used to actually build the additive expression. */
7978 rhs = current.lhs;
7979 rhs_type = current.lhs_type;
7980 --sp;
7981 current = *sp;
7984 /* Undo the disabling of warnings done above. */
7985 if (current.tree_type == TRUTH_ANDIF_EXPR)
7986 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7987 else if (current.tree_type == TRUTH_ORIF_EXPR)
7988 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7990 if (warn_logical_not_paren
7991 && parenthesized_not_lhs_warn)
7992 warn_logical_not_parentheses (current.loc, current.tree_type,
7993 TREE_OPERAND (current.lhs, 0), rhs);
7995 overload = NULL;
7996 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7997 ERROR_MARK for everything that is not a binary expression.
7998 This makes warn_about_parentheses miss some warnings that
7999 involve unary operators. For unary expressions we should
8000 pass the correct tree_code unless the unary expression was
8001 surrounded by parentheses.
8003 if (no_toplevel_fold_p
8004 && lookahead_prec <= current.prec
8005 && sp == stack)
8006 current.lhs = build2 (current.tree_type,
8007 TREE_CODE_CLASS (current.tree_type)
8008 == tcc_comparison
8009 ? boolean_type_node : TREE_TYPE (current.lhs),
8010 current.lhs, rhs);
8011 else
8012 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8013 current.lhs, current.lhs_type,
8014 rhs, rhs_type, &overload,
8015 complain_flags (decltype_p));
8016 current.lhs_type = current.tree_type;
8017 if (EXPR_P (current.lhs))
8018 SET_EXPR_LOCATION (current.lhs, current.loc);
8020 /* If the binary operator required the use of an overloaded operator,
8021 then this expression cannot be an integral constant-expression.
8022 An overloaded operator can be used even if both operands are
8023 otherwise permissible in an integral constant-expression if at
8024 least one of the operands is of enumeration type. */
8026 if (overload
8027 && cp_parser_non_integral_constant_expression (parser,
8028 NIC_OVERLOADED))
8029 return error_mark_node;
8032 return current.lhs;
8035 static tree
8036 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8037 bool no_toplevel_fold_p,
8038 enum cp_parser_prec prec,
8039 cp_id_kind * pidk)
8041 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8042 /*decltype*/false, prec, pidk);
8045 /* Parse the `? expression : assignment-expression' part of a
8046 conditional-expression. The LOGICAL_OR_EXPR is the
8047 logical-or-expression that started the conditional-expression.
8048 Returns a representation of the entire conditional-expression.
8050 This routine is used by cp_parser_assignment_expression.
8052 ? expression : assignment-expression
8054 GNU Extensions:
8056 ? : assignment-expression */
8058 static tree
8059 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8061 tree expr;
8062 tree assignment_expr;
8063 struct cp_token *token;
8064 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8066 /* Consume the `?' token. */
8067 cp_lexer_consume_token (parser->lexer);
8068 token = cp_lexer_peek_token (parser->lexer);
8069 if (cp_parser_allow_gnu_extensions_p (parser)
8070 && token->type == CPP_COLON)
8072 pedwarn (token->location, OPT_Wpedantic,
8073 "ISO C++ does not allow ?: with omitted middle operand");
8074 /* Implicit true clause. */
8075 expr = NULL_TREE;
8076 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8077 warn_for_omitted_condop (token->location, logical_or_expr);
8079 else
8081 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8082 parser->colon_corrects_to_scope_p = false;
8083 /* Parse the expression. */
8084 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8085 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8086 c_inhibit_evaluation_warnings +=
8087 ((logical_or_expr == truthvalue_true_node)
8088 - (logical_or_expr == truthvalue_false_node));
8089 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8092 /* The next token should be a `:'. */
8093 cp_parser_require (parser, CPP_COLON, RT_COLON);
8094 /* Parse the assignment-expression. */
8095 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8096 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8098 /* Build the conditional-expression. */
8099 return build_x_conditional_expr (loc, logical_or_expr,
8100 expr,
8101 assignment_expr,
8102 tf_warning_or_error);
8105 /* Parse an assignment-expression.
8107 assignment-expression:
8108 conditional-expression
8109 logical-or-expression assignment-operator assignment_expression
8110 throw-expression
8112 CAST_P is true if this expression is the target of a cast.
8113 DECLTYPE_P is true if this expression is the operand of decltype.
8115 Returns a representation for the expression. */
8117 static tree
8118 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8119 bool decltype_p, cp_id_kind * pidk)
8121 tree expr;
8123 /* If the next token is the `throw' keyword, then we're looking at
8124 a throw-expression. */
8125 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8126 expr = cp_parser_throw_expression (parser);
8127 /* Otherwise, it must be that we are looking at a
8128 logical-or-expression. */
8129 else
8131 /* Parse the binary expressions (logical-or-expression). */
8132 expr = cp_parser_binary_expression (parser, cast_p, false,
8133 decltype_p,
8134 PREC_NOT_OPERATOR, pidk);
8135 /* If the next token is a `?' then we're actually looking at a
8136 conditional-expression. */
8137 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8138 return cp_parser_question_colon_clause (parser, expr);
8139 else
8141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8143 /* If it's an assignment-operator, we're using the second
8144 production. */
8145 enum tree_code assignment_operator
8146 = cp_parser_assignment_operator_opt (parser);
8147 if (assignment_operator != ERROR_MARK)
8149 bool non_constant_p;
8150 location_t saved_input_location;
8152 /* Parse the right-hand side of the assignment. */
8153 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8155 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8156 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8158 /* An assignment may not appear in a
8159 constant-expression. */
8160 if (cp_parser_non_integral_constant_expression (parser,
8161 NIC_ASSIGNMENT))
8162 return error_mark_node;
8163 /* Build the assignment expression. Its default
8164 location is the location of the '=' token. */
8165 saved_input_location = input_location;
8166 input_location = loc;
8167 expr = build_x_modify_expr (loc, expr,
8168 assignment_operator,
8169 rhs,
8170 complain_flags (decltype_p));
8171 input_location = saved_input_location;
8176 return expr;
8179 static tree
8180 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8181 cp_id_kind * pidk)
8183 return cp_parser_assignment_expression (parser, cast_p,
8184 /*decltype*/false, pidk);
8187 /* Parse an (optional) assignment-operator.
8189 assignment-operator: one of
8190 = *= /= %= += -= >>= <<= &= ^= |=
8192 GNU Extension:
8194 assignment-operator: one of
8195 <?= >?=
8197 If the next token is an assignment operator, the corresponding tree
8198 code is returned, and the token is consumed. For example, for
8199 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8200 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8201 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8202 operator, ERROR_MARK is returned. */
8204 static enum tree_code
8205 cp_parser_assignment_operator_opt (cp_parser* parser)
8207 enum tree_code op;
8208 cp_token *token;
8210 /* Peek at the next token. */
8211 token = cp_lexer_peek_token (parser->lexer);
8213 switch (token->type)
8215 case CPP_EQ:
8216 op = NOP_EXPR;
8217 break;
8219 case CPP_MULT_EQ:
8220 op = MULT_EXPR;
8221 break;
8223 case CPP_DIV_EQ:
8224 op = TRUNC_DIV_EXPR;
8225 break;
8227 case CPP_MOD_EQ:
8228 op = TRUNC_MOD_EXPR;
8229 break;
8231 case CPP_PLUS_EQ:
8232 op = PLUS_EXPR;
8233 break;
8235 case CPP_MINUS_EQ:
8236 op = MINUS_EXPR;
8237 break;
8239 case CPP_RSHIFT_EQ:
8240 op = RSHIFT_EXPR;
8241 break;
8243 case CPP_LSHIFT_EQ:
8244 op = LSHIFT_EXPR;
8245 break;
8247 case CPP_AND_EQ:
8248 op = BIT_AND_EXPR;
8249 break;
8251 case CPP_XOR_EQ:
8252 op = BIT_XOR_EXPR;
8253 break;
8255 case CPP_OR_EQ:
8256 op = BIT_IOR_EXPR;
8257 break;
8259 default:
8260 /* Nothing else is an assignment operator. */
8261 op = ERROR_MARK;
8264 /* If it was an assignment operator, consume it. */
8265 if (op != ERROR_MARK)
8266 cp_lexer_consume_token (parser->lexer);
8268 return op;
8271 /* Parse an expression.
8273 expression:
8274 assignment-expression
8275 expression , assignment-expression
8277 CAST_P is true if this expression is the target of a cast.
8278 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8279 except possibly parenthesized or on the RHS of a comma (N3276).
8281 Returns a representation of the expression. */
8283 static tree
8284 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8285 cp_id_kind * pidk)
8287 tree expression = NULL_TREE;
8288 location_t loc = UNKNOWN_LOCATION;
8290 while (true)
8292 tree assignment_expression;
8294 /* Parse the next assignment-expression. */
8295 assignment_expression
8296 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8298 /* We don't create a temporary for a call that is the immediate operand
8299 of decltype or on the RHS of a comma. But when we see a comma, we
8300 need to create a temporary for a call on the LHS. */
8301 if (decltype_p && !processing_template_decl
8302 && TREE_CODE (assignment_expression) == CALL_EXPR
8303 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8304 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8305 assignment_expression
8306 = build_cplus_new (TREE_TYPE (assignment_expression),
8307 assignment_expression, tf_warning_or_error);
8309 /* If this is the first assignment-expression, we can just
8310 save it away. */
8311 if (!expression)
8312 expression = assignment_expression;
8313 else
8314 expression = build_x_compound_expr (loc, expression,
8315 assignment_expression,
8316 complain_flags (decltype_p));
8317 /* If the next token is not a comma, then we are done with the
8318 expression. */
8319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8320 break;
8321 /* Consume the `,'. */
8322 loc = cp_lexer_peek_token (parser->lexer)->location;
8323 cp_lexer_consume_token (parser->lexer);
8324 /* A comma operator cannot appear in a constant-expression. */
8325 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8326 expression = error_mark_node;
8329 return expression;
8332 static inline tree
8333 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8335 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8338 /* Parse a constant-expression.
8340 constant-expression:
8341 conditional-expression
8343 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8344 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8345 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8346 is false, NON_CONSTANT_P should be NULL. */
8348 static tree
8349 cp_parser_constant_expression (cp_parser* parser,
8350 bool allow_non_constant_p,
8351 bool *non_constant_p)
8353 bool saved_integral_constant_expression_p;
8354 bool saved_allow_non_integral_constant_expression_p;
8355 bool saved_non_integral_constant_expression_p;
8356 tree expression;
8358 /* It might seem that we could simply parse the
8359 conditional-expression, and then check to see if it were
8360 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8361 one that the compiler can figure out is constant, possibly after
8362 doing some simplifications or optimizations. The standard has a
8363 precise definition of constant-expression, and we must honor
8364 that, even though it is somewhat more restrictive.
8366 For example:
8368 int i[(2, 3)];
8370 is not a legal declaration, because `(2, 3)' is not a
8371 constant-expression. The `,' operator is forbidden in a
8372 constant-expression. However, GCC's constant-folding machinery
8373 will fold this operation to an INTEGER_CST for `3'. */
8375 /* Save the old settings. */
8376 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8377 saved_allow_non_integral_constant_expression_p
8378 = parser->allow_non_integral_constant_expression_p;
8379 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8380 /* We are now parsing a constant-expression. */
8381 parser->integral_constant_expression_p = true;
8382 parser->allow_non_integral_constant_expression_p
8383 = (allow_non_constant_p || cxx_dialect >= cxx11);
8384 parser->non_integral_constant_expression_p = false;
8385 /* Although the grammar says "conditional-expression", we parse an
8386 "assignment-expression", which also permits "throw-expression"
8387 and the use of assignment operators. In the case that
8388 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8389 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8390 actually essential that we look for an assignment-expression.
8391 For example, cp_parser_initializer_clauses uses this function to
8392 determine whether a particular assignment-expression is in fact
8393 constant. */
8394 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8395 /* Restore the old settings. */
8396 parser->integral_constant_expression_p
8397 = saved_integral_constant_expression_p;
8398 parser->allow_non_integral_constant_expression_p
8399 = saved_allow_non_integral_constant_expression_p;
8400 if (cxx_dialect >= cxx11)
8402 /* Require an rvalue constant expression here; that's what our
8403 callers expect. Reference constant expressions are handled
8404 separately in e.g. cp_parser_template_argument. */
8405 bool is_const = potential_rvalue_constant_expression (expression);
8406 parser->non_integral_constant_expression_p = !is_const;
8407 if (!is_const && !allow_non_constant_p)
8408 require_potential_rvalue_constant_expression (expression);
8410 if (allow_non_constant_p)
8411 *non_constant_p = parser->non_integral_constant_expression_p;
8412 parser->non_integral_constant_expression_p
8413 = saved_non_integral_constant_expression_p;
8415 return expression;
8418 /* Parse __builtin_offsetof.
8420 offsetof-expression:
8421 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8423 offsetof-member-designator:
8424 id-expression
8425 | offsetof-member-designator "." id-expression
8426 | offsetof-member-designator "[" expression "]"
8427 | offsetof-member-designator "->" id-expression */
8429 static tree
8430 cp_parser_builtin_offsetof (cp_parser *parser)
8432 int save_ice_p, save_non_ice_p;
8433 tree type, expr;
8434 cp_id_kind dummy;
8435 cp_token *token;
8437 /* We're about to accept non-integral-constant things, but will
8438 definitely yield an integral constant expression. Save and
8439 restore these values around our local parsing. */
8440 save_ice_p = parser->integral_constant_expression_p;
8441 save_non_ice_p = parser->non_integral_constant_expression_p;
8443 /* Consume the "__builtin_offsetof" token. */
8444 cp_lexer_consume_token (parser->lexer);
8445 /* Consume the opening `('. */
8446 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8447 /* Parse the type-id. */
8448 type = cp_parser_type_id (parser);
8449 /* Look for the `,'. */
8450 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8451 token = cp_lexer_peek_token (parser->lexer);
8453 /* Build the (type *)null that begins the traditional offsetof macro. */
8454 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8455 tf_warning_or_error);
8457 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8458 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8459 true, &dummy, token->location);
8460 while (true)
8462 token = cp_lexer_peek_token (parser->lexer);
8463 switch (token->type)
8465 case CPP_OPEN_SQUARE:
8466 /* offsetof-member-designator "[" expression "]" */
8467 expr = cp_parser_postfix_open_square_expression (parser, expr,
8468 true, false);
8469 break;
8471 case CPP_DEREF:
8472 /* offsetof-member-designator "->" identifier */
8473 expr = grok_array_decl (token->location, expr,
8474 integer_zero_node, false);
8475 /* FALLTHRU */
8477 case CPP_DOT:
8478 /* offsetof-member-designator "." identifier */
8479 cp_lexer_consume_token (parser->lexer);
8480 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8481 expr, true, &dummy,
8482 token->location);
8483 break;
8485 case CPP_CLOSE_PAREN:
8486 /* Consume the ")" token. */
8487 cp_lexer_consume_token (parser->lexer);
8488 goto success;
8490 default:
8491 /* Error. We know the following require will fail, but
8492 that gives the proper error message. */
8493 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8494 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8495 expr = error_mark_node;
8496 goto failure;
8500 success:
8501 /* If we're processing a template, we can't finish the semantics yet.
8502 Otherwise we can fold the entire expression now. */
8503 if (processing_template_decl)
8504 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8505 else
8506 expr = finish_offsetof (expr);
8508 failure:
8509 parser->integral_constant_expression_p = save_ice_p;
8510 parser->non_integral_constant_expression_p = save_non_ice_p;
8512 return expr;
8515 /* Parse a trait expression.
8517 Returns a representation of the expression, the underlying type
8518 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8520 static tree
8521 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8523 cp_trait_kind kind;
8524 tree type1, type2 = NULL_TREE;
8525 bool binary = false;
8526 cp_decl_specifier_seq decl_specs;
8528 switch (keyword)
8530 case RID_HAS_NOTHROW_ASSIGN:
8531 kind = CPTK_HAS_NOTHROW_ASSIGN;
8532 break;
8533 case RID_HAS_NOTHROW_CONSTRUCTOR:
8534 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8535 break;
8536 case RID_HAS_NOTHROW_COPY:
8537 kind = CPTK_HAS_NOTHROW_COPY;
8538 break;
8539 case RID_HAS_TRIVIAL_ASSIGN:
8540 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8541 break;
8542 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8543 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8544 break;
8545 case RID_HAS_TRIVIAL_COPY:
8546 kind = CPTK_HAS_TRIVIAL_COPY;
8547 break;
8548 case RID_HAS_TRIVIAL_DESTRUCTOR:
8549 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8550 break;
8551 case RID_HAS_VIRTUAL_DESTRUCTOR:
8552 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8553 break;
8554 case RID_IS_ABSTRACT:
8555 kind = CPTK_IS_ABSTRACT;
8556 break;
8557 case RID_IS_BASE_OF:
8558 kind = CPTK_IS_BASE_OF;
8559 binary = true;
8560 break;
8561 case RID_IS_CLASS:
8562 kind = CPTK_IS_CLASS;
8563 break;
8564 case RID_IS_CONVERTIBLE_TO:
8565 kind = CPTK_IS_CONVERTIBLE_TO;
8566 binary = true;
8567 break;
8568 case RID_IS_EMPTY:
8569 kind = CPTK_IS_EMPTY;
8570 break;
8571 case RID_IS_ENUM:
8572 kind = CPTK_IS_ENUM;
8573 break;
8574 case RID_IS_FINAL:
8575 kind = CPTK_IS_FINAL;
8576 break;
8577 case RID_IS_LITERAL_TYPE:
8578 kind = CPTK_IS_LITERAL_TYPE;
8579 break;
8580 case RID_IS_POD:
8581 kind = CPTK_IS_POD;
8582 break;
8583 case RID_IS_POLYMORPHIC:
8584 kind = CPTK_IS_POLYMORPHIC;
8585 break;
8586 case RID_IS_STD_LAYOUT:
8587 kind = CPTK_IS_STD_LAYOUT;
8588 break;
8589 case RID_IS_TRIVIAL:
8590 kind = CPTK_IS_TRIVIAL;
8591 break;
8592 case RID_IS_UNION:
8593 kind = CPTK_IS_UNION;
8594 break;
8595 case RID_UNDERLYING_TYPE:
8596 kind = CPTK_UNDERLYING_TYPE;
8597 break;
8598 case RID_BASES:
8599 kind = CPTK_BASES;
8600 break;
8601 case RID_DIRECT_BASES:
8602 kind = CPTK_DIRECT_BASES;
8603 break;
8604 default:
8605 gcc_unreachable ();
8608 /* Consume the token. */
8609 cp_lexer_consume_token (parser->lexer);
8611 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8613 type1 = cp_parser_type_id (parser);
8615 if (type1 == error_mark_node)
8616 return error_mark_node;
8618 /* Build a trivial decl-specifier-seq. */
8619 clear_decl_specs (&decl_specs);
8620 decl_specs.type = type1;
8622 /* Call grokdeclarator to figure out what type this is. */
8623 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8624 /*initialized=*/0, /*attrlist=*/NULL);
8626 if (binary)
8628 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8630 type2 = cp_parser_type_id (parser);
8632 if (type2 == 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 = type2;
8639 /* Call grokdeclarator to figure out what type this is. */
8640 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8641 /*initialized=*/0, /*attrlist=*/NULL);
8644 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8646 /* Complete the trait expression, which may mean either processing
8647 the trait expr now or saving it for template instantiation. */
8648 switch(kind)
8650 case CPTK_UNDERLYING_TYPE:
8651 return finish_underlying_type (type1);
8652 case CPTK_BASES:
8653 return finish_bases (type1, false);
8654 case CPTK_DIRECT_BASES:
8655 return finish_bases (type1, true);
8656 default:
8657 return finish_trait_expr (kind, type1, type2);
8661 /* Lambdas that appear in variable initializer or default argument scope
8662 get that in their mangling, so we need to record it. We might as well
8663 use the count for function and namespace scopes as well. */
8664 static GTY(()) tree lambda_scope;
8665 static GTY(()) int lambda_count;
8666 typedef struct GTY(()) tree_int
8668 tree t;
8669 int i;
8670 } tree_int;
8671 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8673 static void
8674 start_lambda_scope (tree decl)
8676 tree_int ti;
8677 gcc_assert (decl);
8678 /* Once we're inside a function, we ignore other scopes and just push
8679 the function again so that popping works properly. */
8680 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8681 decl = current_function_decl;
8682 ti.t = lambda_scope;
8683 ti.i = lambda_count;
8684 vec_safe_push (lambda_scope_stack, ti);
8685 if (lambda_scope != decl)
8687 /* Don't reset the count if we're still in the same function. */
8688 lambda_scope = decl;
8689 lambda_count = 0;
8693 static void
8694 record_lambda_scope (tree lambda)
8696 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8697 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8700 static void
8701 finish_lambda_scope (void)
8703 tree_int *p = &lambda_scope_stack->last ();
8704 if (lambda_scope != p->t)
8706 lambda_scope = p->t;
8707 lambda_count = p->i;
8709 lambda_scope_stack->pop ();
8712 /* Parse a lambda expression.
8714 lambda-expression:
8715 lambda-introducer lambda-declarator [opt] compound-statement
8717 Returns a representation of the expression. */
8719 static tree
8720 cp_parser_lambda_expression (cp_parser* parser)
8722 tree lambda_expr = build_lambda_expr ();
8723 tree type;
8724 bool ok = true;
8725 cp_token *token = cp_lexer_peek_token (parser->lexer);
8727 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8729 if (cp_unevaluated_operand)
8731 if (!token->error_reported)
8733 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8734 "lambda-expression in unevaluated context");
8735 token->error_reported = true;
8737 ok = false;
8740 /* We may be in the middle of deferred access check. Disable
8741 it now. */
8742 push_deferring_access_checks (dk_no_deferred);
8744 cp_parser_lambda_introducer (parser, lambda_expr);
8746 type = begin_lambda_type (lambda_expr);
8747 if (type == error_mark_node)
8748 return error_mark_node;
8750 record_lambda_scope (lambda_expr);
8752 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8753 determine_visibility (TYPE_NAME (type));
8755 /* Now that we've started the type, add the capture fields for any
8756 explicit captures. */
8757 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8760 /* Inside the class, surrounding template-parameter-lists do not apply. */
8761 unsigned int saved_num_template_parameter_lists
8762 = parser->num_template_parameter_lists;
8763 unsigned char in_statement = parser->in_statement;
8764 bool in_switch_statement_p = parser->in_switch_statement_p;
8765 bool fully_implicit_function_template_p
8766 = parser->fully_implicit_function_template_p;
8767 tree implicit_template_parms = parser->implicit_template_parms;
8768 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8769 bool auto_is_implicit_function_template_parm_p
8770 = parser->auto_is_implicit_function_template_parm_p;
8772 parser->num_template_parameter_lists = 0;
8773 parser->in_statement = 0;
8774 parser->in_switch_statement_p = false;
8775 parser->fully_implicit_function_template_p = false;
8776 parser->implicit_template_parms = 0;
8777 parser->implicit_template_scope = 0;
8778 parser->auto_is_implicit_function_template_parm_p = false;
8780 /* By virtue of defining a local class, a lambda expression has access to
8781 the private variables of enclosing classes. */
8783 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8785 if (ok)
8786 cp_parser_lambda_body (parser, lambda_expr);
8787 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8789 if (cp_parser_skip_to_closing_brace (parser))
8790 cp_lexer_consume_token (parser->lexer);
8793 /* The capture list was built up in reverse order; fix that now. */
8794 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8795 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8797 if (ok)
8798 maybe_add_lambda_conv_op (type);
8800 type = finish_struct (type, /*attributes=*/NULL_TREE);
8802 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8803 parser->in_statement = in_statement;
8804 parser->in_switch_statement_p = in_switch_statement_p;
8805 parser->fully_implicit_function_template_p
8806 = fully_implicit_function_template_p;
8807 parser->implicit_template_parms = implicit_template_parms;
8808 parser->implicit_template_scope = implicit_template_scope;
8809 parser->auto_is_implicit_function_template_parm_p
8810 = auto_is_implicit_function_template_parm_p;
8813 pop_deferring_access_checks ();
8815 /* This field is only used during parsing of the lambda. */
8816 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8818 /* This lambda shouldn't have any proxies left at this point. */
8819 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8820 /* And now that we're done, push proxies for an enclosing lambda. */
8821 insert_pending_capture_proxies ();
8823 if (ok)
8824 return build_lambda_object (lambda_expr);
8825 else
8826 return error_mark_node;
8829 /* Parse the beginning of a lambda expression.
8831 lambda-introducer:
8832 [ lambda-capture [opt] ]
8834 LAMBDA_EXPR is the current representation of the lambda expression. */
8836 static void
8837 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8839 /* Need commas after the first capture. */
8840 bool first = true;
8842 /* Eat the leading `['. */
8843 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8845 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8846 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8847 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8848 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8849 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8850 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8852 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8854 cp_lexer_consume_token (parser->lexer);
8855 first = false;
8858 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8860 cp_token* capture_token;
8861 tree capture_id;
8862 tree capture_init_expr;
8863 cp_id_kind idk = CP_ID_KIND_NONE;
8864 bool explicit_init_p = false;
8866 enum capture_kind_type
8868 BY_COPY,
8869 BY_REFERENCE
8871 enum capture_kind_type capture_kind = BY_COPY;
8873 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8875 error ("expected end of capture-list");
8876 return;
8879 if (first)
8880 first = false;
8881 else
8882 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8884 /* Possibly capture `this'. */
8885 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8887 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8888 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8889 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8890 "with by-copy capture default");
8891 cp_lexer_consume_token (parser->lexer);
8892 add_capture (lambda_expr,
8893 /*id=*/this_identifier,
8894 /*initializer=*/finish_this_expr(),
8895 /*by_reference_p=*/false,
8896 explicit_init_p);
8897 continue;
8900 /* Remember whether we want to capture as a reference or not. */
8901 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8903 capture_kind = BY_REFERENCE;
8904 cp_lexer_consume_token (parser->lexer);
8907 /* Get the identifier. */
8908 capture_token = cp_lexer_peek_token (parser->lexer);
8909 capture_id = cp_parser_identifier (parser);
8911 if (capture_id == error_mark_node)
8912 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8913 delimiters, but I modified this to stop on unnested ']' as well. It
8914 was already changed to stop on unnested '}', so the
8915 "closing_parenthesis" name is no more misleading with my change. */
8917 cp_parser_skip_to_closing_parenthesis (parser,
8918 /*recovering=*/true,
8919 /*or_comma=*/true,
8920 /*consume_paren=*/true);
8921 break;
8924 /* Find the initializer for this capture. */
8925 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8926 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8927 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8929 bool direct, non_constant;
8930 /* An explicit initializer exists. */
8931 if (cxx_dialect < cxx1y)
8932 pedwarn (input_location, 0,
8933 "lambda capture initializers "
8934 "only available with -std=c++1y or -std=gnu++1y");
8935 capture_init_expr = cp_parser_initializer (parser, &direct,
8936 &non_constant);
8937 explicit_init_p = true;
8938 if (capture_init_expr == NULL_TREE)
8940 error ("empty initializer for lambda init-capture");
8941 capture_init_expr = error_mark_node;
8944 else
8946 const char* error_msg;
8948 /* Turn the identifier into an id-expression. */
8949 capture_init_expr
8950 = cp_parser_lookup_name_simple (parser, capture_id,
8951 capture_token->location);
8953 if (capture_init_expr == error_mark_node)
8955 unqualified_name_lookup_error (capture_id);
8956 continue;
8958 else if (DECL_P (capture_init_expr)
8959 && (!VAR_P (capture_init_expr)
8960 && TREE_CODE (capture_init_expr) != PARM_DECL))
8962 error_at (capture_token->location,
8963 "capture of non-variable %qD ",
8964 capture_init_expr);
8965 inform (0, "%q+#D declared here", capture_init_expr);
8966 continue;
8968 if (VAR_P (capture_init_expr)
8969 && decl_storage_duration (capture_init_expr) != dk_auto)
8971 if (pedwarn (capture_token->location, 0, "capture of variable "
8972 "%qD with non-automatic storage duration",
8973 capture_init_expr))
8974 inform (0, "%q+#D declared here", capture_init_expr);
8975 continue;
8978 capture_init_expr
8979 = finish_id_expression
8980 (capture_id,
8981 capture_init_expr,
8982 parser->scope,
8983 &idk,
8984 /*integral_constant_expression_p=*/false,
8985 /*allow_non_integral_constant_expression_p=*/false,
8986 /*non_integral_constant_expression_p=*/NULL,
8987 /*template_p=*/false,
8988 /*done=*/true,
8989 /*address_p=*/false,
8990 /*template_arg_p=*/false,
8991 &error_msg,
8992 capture_token->location);
8994 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8996 cp_lexer_consume_token (parser->lexer);
8997 capture_init_expr = make_pack_expansion (capture_init_expr);
8999 else
9000 check_for_bare_parameter_packs (capture_init_expr);
9003 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9004 && !explicit_init_p)
9006 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9007 && capture_kind == BY_COPY)
9008 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9009 "of %qD redundant with by-copy capture default",
9010 capture_id);
9011 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9012 && capture_kind == BY_REFERENCE)
9013 pedwarn (capture_token->location, 0, "explicit by-reference "
9014 "capture of %qD redundant with by-reference capture "
9015 "default", capture_id);
9018 add_capture (lambda_expr,
9019 capture_id,
9020 capture_init_expr,
9021 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9022 explicit_init_p);
9025 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9028 /* Parse the (optional) middle of a lambda expression.
9030 lambda-declarator:
9031 < template-parameter-list [opt] >
9032 ( parameter-declaration-clause [opt] )
9033 attribute-specifier [opt]
9034 mutable [opt]
9035 exception-specification [opt]
9036 lambda-return-type-clause [opt]
9038 LAMBDA_EXPR is the current representation of the lambda expression. */
9040 static bool
9041 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9043 /* 5.1.1.4 of the standard says:
9044 If a lambda-expression does not include a lambda-declarator, it is as if
9045 the lambda-declarator were ().
9046 This means an empty parameter list, no attributes, and no exception
9047 specification. */
9048 tree param_list = void_list_node;
9049 tree attributes = NULL_TREE;
9050 tree exception_spec = NULL_TREE;
9051 tree template_param_list = NULL_TREE;
9053 /* The template-parameter-list is optional, but must begin with
9054 an opening angle if present. */
9055 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9057 if (cxx_dialect < cxx1y)
9058 pedwarn (parser->lexer->next_token->location, 0,
9059 "lambda templates are only available with "
9060 "-std=c++1y or -std=gnu++1y");
9062 cp_lexer_consume_token (parser->lexer);
9064 template_param_list = cp_parser_template_parameter_list (parser);
9066 cp_parser_skip_to_end_of_template_parameter_list (parser);
9068 /* We just processed one more parameter list. */
9069 ++parser->num_template_parameter_lists;
9072 /* The parameter-declaration-clause is optional (unless
9073 template-parameter-list was given), but must begin with an
9074 opening parenthesis if present. */
9075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9077 cp_lexer_consume_token (parser->lexer);
9079 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9081 /* Parse parameters. */
9082 param_list = cp_parser_parameter_declaration_clause (parser);
9084 /* Default arguments shall not be specified in the
9085 parameter-declaration-clause of a lambda-declarator. */
9086 for (tree t = param_list; t; t = TREE_CHAIN (t))
9087 if (TREE_PURPOSE (t))
9088 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9089 "default argument specified for lambda parameter");
9091 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9093 attributes = cp_parser_attributes_opt (parser);
9095 /* Parse optional `mutable' keyword. */
9096 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9098 cp_lexer_consume_token (parser->lexer);
9099 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9102 /* Parse optional exception specification. */
9103 exception_spec = cp_parser_exception_specification_opt (parser);
9105 /* Parse optional trailing return type. */
9106 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9108 cp_lexer_consume_token (parser->lexer);
9109 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9110 = cp_parser_trailing_type_id (parser);
9113 /* The function parameters must be in scope all the way until after the
9114 trailing-return-type in case of decltype. */
9115 pop_bindings_and_leave_scope ();
9117 else if (template_param_list != NULL_TREE) // generate diagnostic
9118 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9120 /* Create the function call operator.
9122 Messing with declarators like this is no uglier than building up the
9123 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9124 other code. */
9126 cp_decl_specifier_seq return_type_specs;
9127 cp_declarator* declarator;
9128 tree fco;
9129 int quals;
9130 void *p;
9132 clear_decl_specs (&return_type_specs);
9133 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9134 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9135 else
9136 /* Maybe we will deduce the return type later. */
9137 return_type_specs.type = make_auto ();
9139 p = obstack_alloc (&declarator_obstack, 0);
9141 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9142 sfk_none);
9144 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9145 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9146 declarator = make_call_declarator (declarator, param_list, quals,
9147 VIRT_SPEC_UNSPECIFIED,
9148 REF_QUAL_NONE,
9149 exception_spec,
9150 /*late_return_type=*/NULL_TREE);
9151 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9153 fco = grokmethod (&return_type_specs,
9154 declarator,
9155 attributes);
9156 if (fco != error_mark_node)
9158 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9159 DECL_ARTIFICIAL (fco) = 1;
9160 /* Give the object parameter a different name. */
9161 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9162 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9163 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9165 if (template_param_list)
9167 fco = finish_member_template_decl (fco);
9168 finish_template_decl (template_param_list);
9169 --parser->num_template_parameter_lists;
9171 else if (parser->fully_implicit_function_template_p)
9172 fco = finish_fully_implicit_template (parser, fco);
9174 finish_member_declaration (fco);
9176 obstack_free (&declarator_obstack, p);
9178 return (fco != error_mark_node);
9182 /* Parse the body of a lambda expression, which is simply
9184 compound-statement
9186 but which requires special handling.
9187 LAMBDA_EXPR is the current representation of the lambda expression. */
9189 static void
9190 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9192 bool nested = (current_function_decl != NULL_TREE);
9193 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9194 if (nested)
9195 push_function_context ();
9196 else
9197 /* Still increment function_depth so that we don't GC in the
9198 middle of an expression. */
9199 ++function_depth;
9200 /* Clear this in case we're in the middle of a default argument. */
9201 parser->local_variables_forbidden_p = false;
9203 /* Finish the function call operator
9204 - class_specifier
9205 + late_parsing_for_member
9206 + function_definition_after_declarator
9207 + ctor_initializer_opt_and_function_body */
9209 tree fco = lambda_function (lambda_expr);
9210 tree body;
9211 bool done = false;
9212 tree compound_stmt;
9213 tree cap;
9215 /* Let the front end know that we are going to be defining this
9216 function. */
9217 start_preparsed_function (fco,
9218 NULL_TREE,
9219 SF_PRE_PARSED | SF_INCLASS_INLINE);
9221 start_lambda_scope (fco);
9222 body = begin_function_body ();
9224 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9225 goto out;
9227 /* Push the proxies for any explicit captures. */
9228 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9229 cap = TREE_CHAIN (cap))
9230 build_capture_proxy (TREE_PURPOSE (cap));
9232 compound_stmt = begin_compound_stmt (0);
9234 /* 5.1.1.4 of the standard says:
9235 If a lambda-expression does not include a trailing-return-type, it
9236 is as if the trailing-return-type denotes the following type:
9237 * if the compound-statement is of the form
9238 { return attribute-specifier [opt] expression ; }
9239 the type of the returned expression after lvalue-to-rvalue
9240 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9241 (_conv.array_ 4.2), and function-to-pointer conversion
9242 (_conv.func_ 4.3);
9243 * otherwise, void. */
9245 /* In a lambda that has neither a lambda-return-type-clause
9246 nor a deducible form, errors should be reported for return statements
9247 in the body. Since we used void as the placeholder return type, parsing
9248 the body as usual will give such desired behavior. */
9249 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9250 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9251 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9253 tree expr = NULL_TREE;
9254 cp_id_kind idk = CP_ID_KIND_NONE;
9256 /* Parse tentatively in case there's more after the initial return
9257 statement. */
9258 cp_parser_parse_tentatively (parser);
9260 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9262 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9264 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9265 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9267 if (cp_parser_parse_definitely (parser))
9269 if (!processing_template_decl)
9270 apply_deduced_return_type (fco, lambda_return_type (expr));
9272 /* Will get error here if type not deduced yet. */
9273 finish_return_stmt (expr);
9275 done = true;
9279 if (!done)
9281 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9282 cp_parser_label_declaration (parser);
9283 cp_parser_statement_seq_opt (parser, NULL_TREE);
9284 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9287 finish_compound_stmt (compound_stmt);
9289 out:
9290 finish_function_body (body);
9291 finish_lambda_scope ();
9293 /* Finish the function and generate code for it if necessary. */
9294 tree fn = finish_function (/*inline*/2);
9296 /* Only expand if the call op is not a template. */
9297 if (!DECL_TEMPLATE_INFO (fco))
9298 expand_or_defer_fn (fn);
9301 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9302 if (nested)
9303 pop_function_context();
9304 else
9305 --function_depth;
9308 /* Statements [gram.stmt.stmt] */
9310 /* Parse a statement.
9312 statement:
9313 labeled-statement
9314 expression-statement
9315 compound-statement
9316 selection-statement
9317 iteration-statement
9318 jump-statement
9319 declaration-statement
9320 try-block
9322 C++11:
9324 statement:
9325 labeled-statement
9326 attribute-specifier-seq (opt) expression-statement
9327 attribute-specifier-seq (opt) compound-statement
9328 attribute-specifier-seq (opt) selection-statement
9329 attribute-specifier-seq (opt) iteration-statement
9330 attribute-specifier-seq (opt) jump-statement
9331 declaration-statement
9332 attribute-specifier-seq (opt) try-block
9334 TM Extension:
9336 statement:
9337 atomic-statement
9339 IN_COMPOUND is true when the statement is nested inside a
9340 cp_parser_compound_statement; this matters for certain pragmas.
9342 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9343 is a (possibly labeled) if statement which is not enclosed in braces
9344 and has an else clause. This is used to implement -Wparentheses. */
9346 static void
9347 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9348 bool in_compound, bool *if_p)
9350 tree statement, std_attrs = NULL_TREE;
9351 cp_token *token;
9352 location_t statement_location, attrs_location;
9354 restart:
9355 if (if_p != NULL)
9356 *if_p = false;
9357 /* There is no statement yet. */
9358 statement = NULL_TREE;
9360 cp_lexer_save_tokens (parser->lexer);
9361 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9362 if (c_dialect_objc ())
9363 /* In obj-c++, seeing '[[' might be the either the beginning of
9364 c++11 attributes, or a nested objc-message-expression. So
9365 let's parse the c++11 attributes tentatively. */
9366 cp_parser_parse_tentatively (parser);
9367 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9368 if (c_dialect_objc ())
9370 if (!cp_parser_parse_definitely (parser))
9371 std_attrs = NULL_TREE;
9374 /* Peek at the next token. */
9375 token = cp_lexer_peek_token (parser->lexer);
9376 /* Remember the location of the first token in the statement. */
9377 statement_location = token->location;
9378 /* If this is a keyword, then that will often determine what kind of
9379 statement we have. */
9380 if (token->type == CPP_KEYWORD)
9382 enum rid keyword = token->keyword;
9384 switch (keyword)
9386 case RID_CASE:
9387 case RID_DEFAULT:
9388 /* Looks like a labeled-statement with a case label.
9389 Parse the label, and then use tail recursion to parse
9390 the statement. */
9391 cp_parser_label_for_labeled_statement (parser, std_attrs);
9392 goto restart;
9394 case RID_IF:
9395 case RID_SWITCH:
9396 statement = cp_parser_selection_statement (parser, if_p);
9397 break;
9399 case RID_WHILE:
9400 case RID_DO:
9401 case RID_FOR:
9402 statement = cp_parser_iteration_statement (parser, false);
9403 break;
9405 case RID_BREAK:
9406 case RID_CONTINUE:
9407 case RID_RETURN:
9408 case RID_GOTO:
9409 statement = cp_parser_jump_statement (parser);
9410 break;
9412 case RID_CILK_SYNC:
9413 cp_lexer_consume_token (parser->lexer);
9414 if (flag_cilkplus)
9416 tree sync_expr = build_cilk_sync ();
9417 SET_EXPR_LOCATION (sync_expr,
9418 token->location);
9419 statement = finish_expr_stmt (sync_expr);
9421 else
9423 error_at (token->location, "-fcilkplus must be enabled to use"
9424 " %<_Cilk_sync%>");
9425 statement = error_mark_node;
9427 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9428 break;
9430 /* Objective-C++ exception-handling constructs. */
9431 case RID_AT_TRY:
9432 case RID_AT_CATCH:
9433 case RID_AT_FINALLY:
9434 case RID_AT_SYNCHRONIZED:
9435 case RID_AT_THROW:
9436 statement = cp_parser_objc_statement (parser);
9437 break;
9439 case RID_TRY:
9440 statement = cp_parser_try_block (parser);
9441 break;
9443 case RID_NAMESPACE:
9444 /* This must be a namespace alias definition. */
9445 cp_parser_declaration_statement (parser);
9446 return;
9448 case RID_TRANSACTION_ATOMIC:
9449 case RID_TRANSACTION_RELAXED:
9450 statement = cp_parser_transaction (parser, keyword);
9451 break;
9452 case RID_TRANSACTION_CANCEL:
9453 statement = cp_parser_transaction_cancel (parser);
9454 break;
9456 default:
9457 /* It might be a keyword like `int' that can start a
9458 declaration-statement. */
9459 break;
9462 else if (token->type == CPP_NAME)
9464 /* If the next token is a `:', then we are looking at a
9465 labeled-statement. */
9466 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9467 if (token->type == CPP_COLON)
9469 /* Looks like a labeled-statement with an ordinary label.
9470 Parse the label, and then use tail recursion to parse
9471 the statement. */
9473 cp_parser_label_for_labeled_statement (parser, std_attrs);
9474 goto restart;
9477 /* Anything that starts with a `{' must be a compound-statement. */
9478 else if (token->type == CPP_OPEN_BRACE)
9479 statement = cp_parser_compound_statement (parser, NULL, false, false);
9480 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9481 a statement all its own. */
9482 else if (token->type == CPP_PRAGMA)
9484 /* Only certain OpenMP pragmas are attached to statements, and thus
9485 are considered statements themselves. All others are not. In
9486 the context of a compound, accept the pragma as a "statement" and
9487 return so that we can check for a close brace. Otherwise we
9488 require a real statement and must go back and read one. */
9489 if (in_compound)
9490 cp_parser_pragma (parser, pragma_compound);
9491 else if (!cp_parser_pragma (parser, pragma_stmt))
9492 goto restart;
9493 return;
9495 else if (token->type == CPP_EOF)
9497 cp_parser_error (parser, "expected statement");
9498 return;
9501 /* Everything else must be a declaration-statement or an
9502 expression-statement. Try for the declaration-statement
9503 first, unless we are looking at a `;', in which case we know that
9504 we have an expression-statement. */
9505 if (!statement)
9507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9509 if (std_attrs != NULL_TREE)
9511 /* Attributes should be parsed as part of the the
9512 declaration, so let's un-parse them. */
9513 cp_lexer_rollback_tokens (parser->lexer);
9514 std_attrs = NULL_TREE;
9517 cp_parser_parse_tentatively (parser);
9518 /* Try to parse the declaration-statement. */
9519 cp_parser_declaration_statement (parser);
9520 /* If that worked, we're done. */
9521 if (cp_parser_parse_definitely (parser))
9522 return;
9524 /* Look for an expression-statement instead. */
9525 statement = cp_parser_expression_statement (parser, in_statement_expr);
9528 /* Set the line number for the statement. */
9529 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9530 SET_EXPR_LOCATION (statement, statement_location);
9532 /* Note that for now, we don't do anything with c++11 statements
9533 parsed at this level. */
9534 if (std_attrs != NULL_TREE)
9535 warning_at (attrs_location,
9536 OPT_Wattributes,
9537 "attributes at the beginning of statement are ignored");
9540 /* Parse the label for a labeled-statement, i.e.
9542 identifier :
9543 case constant-expression :
9544 default :
9546 GNU Extension:
9547 case constant-expression ... constant-expression : statement
9549 When a label is parsed without errors, the label is added to the
9550 parse tree by the finish_* functions, so this function doesn't
9551 have to return the label. */
9553 static void
9554 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9556 cp_token *token;
9557 tree label = NULL_TREE;
9558 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9560 /* The next token should be an identifier. */
9561 token = cp_lexer_peek_token (parser->lexer);
9562 if (token->type != CPP_NAME
9563 && token->type != CPP_KEYWORD)
9565 cp_parser_error (parser, "expected labeled-statement");
9566 return;
9569 parser->colon_corrects_to_scope_p = false;
9570 switch (token->keyword)
9572 case RID_CASE:
9574 tree expr, expr_hi;
9575 cp_token *ellipsis;
9577 /* Consume the `case' token. */
9578 cp_lexer_consume_token (parser->lexer);
9579 /* Parse the constant-expression. */
9580 expr = cp_parser_constant_expression (parser,
9581 /*allow_non_constant_p=*/false,
9582 NULL);
9584 ellipsis = cp_lexer_peek_token (parser->lexer);
9585 if (ellipsis->type == CPP_ELLIPSIS)
9587 /* Consume the `...' token. */
9588 cp_lexer_consume_token (parser->lexer);
9589 expr_hi =
9590 cp_parser_constant_expression (parser,
9591 /*allow_non_constant_p=*/false,
9592 NULL);
9593 /* We don't need to emit warnings here, as the common code
9594 will do this for us. */
9596 else
9597 expr_hi = NULL_TREE;
9599 if (parser->in_switch_statement_p)
9600 finish_case_label (token->location, expr, expr_hi);
9601 else
9602 error_at (token->location,
9603 "case label %qE not within a switch statement",
9604 expr);
9606 break;
9608 case RID_DEFAULT:
9609 /* Consume the `default' token. */
9610 cp_lexer_consume_token (parser->lexer);
9612 if (parser->in_switch_statement_p)
9613 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9614 else
9615 error_at (token->location, "case label not within a switch statement");
9616 break;
9618 default:
9619 /* Anything else must be an ordinary label. */
9620 label = finish_label_stmt (cp_parser_identifier (parser));
9621 break;
9624 /* Require the `:' token. */
9625 cp_parser_require (parser, CPP_COLON, RT_COLON);
9627 /* An ordinary label may optionally be followed by attributes.
9628 However, this is only permitted if the attributes are then
9629 followed by a semicolon. This is because, for backward
9630 compatibility, when parsing
9631 lab: __attribute__ ((unused)) int i;
9632 we want the attribute to attach to "i", not "lab". */
9633 if (label != NULL_TREE
9634 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9636 tree attrs;
9637 cp_parser_parse_tentatively (parser);
9638 attrs = cp_parser_gnu_attributes_opt (parser);
9639 if (attrs == NULL_TREE
9640 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9641 cp_parser_abort_tentative_parse (parser);
9642 else if (!cp_parser_parse_definitely (parser))
9644 else
9645 attributes = chainon (attributes, attrs);
9648 if (attributes != NULL_TREE)
9649 cplus_decl_attributes (&label, attributes, 0);
9651 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9654 /* Parse an expression-statement.
9656 expression-statement:
9657 expression [opt] ;
9659 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9660 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9661 indicates whether this expression-statement is part of an
9662 expression statement. */
9664 static tree
9665 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9667 tree statement = NULL_TREE;
9668 cp_token *token = cp_lexer_peek_token (parser->lexer);
9670 /* If the next token is a ';', then there is no expression
9671 statement. */
9672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9674 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9675 if (statement == error_mark_node
9676 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9678 cp_parser_skip_to_end_of_block_or_statement (parser);
9679 return error_mark_node;
9683 /* Give a helpful message for "A<T>::type t;" and the like. */
9684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9685 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9687 if (TREE_CODE (statement) == SCOPE_REF)
9688 error_at (token->location, "need %<typename%> before %qE because "
9689 "%qT is a dependent scope",
9690 statement, TREE_OPERAND (statement, 0));
9691 else if (is_overloaded_fn (statement)
9692 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9694 /* A::A a; */
9695 tree fn = get_first_fn (statement);
9696 error_at (token->location,
9697 "%<%T::%D%> names the constructor, not the type",
9698 DECL_CONTEXT (fn), DECL_NAME (fn));
9702 /* Consume the final `;'. */
9703 cp_parser_consume_semicolon_at_end_of_statement (parser);
9705 if (in_statement_expr
9706 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9707 /* This is the final expression statement of a statement
9708 expression. */
9709 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9710 else if (statement)
9711 statement = finish_expr_stmt (statement);
9713 return statement;
9716 /* Parse a compound-statement.
9718 compound-statement:
9719 { statement-seq [opt] }
9721 GNU extension:
9723 compound-statement:
9724 { label-declaration-seq [opt] statement-seq [opt] }
9726 label-declaration-seq:
9727 label-declaration
9728 label-declaration-seq label-declaration
9730 Returns a tree representing the statement. */
9732 static tree
9733 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9734 bool in_try, bool function_body)
9736 tree compound_stmt;
9738 /* Consume the `{'. */
9739 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9740 return error_mark_node;
9741 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9742 && !function_body)
9743 pedwarn (input_location, OPT_Wpedantic,
9744 "compound-statement in constexpr function");
9745 /* Begin the compound-statement. */
9746 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9747 /* If the next keyword is `__label__' we have a label declaration. */
9748 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9749 cp_parser_label_declaration (parser);
9750 /* Parse an (optional) statement-seq. */
9751 cp_parser_statement_seq_opt (parser, in_statement_expr);
9752 /* Finish the compound-statement. */
9753 finish_compound_stmt (compound_stmt);
9754 /* Consume the `}'. */
9755 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9757 return compound_stmt;
9760 /* Parse an (optional) statement-seq.
9762 statement-seq:
9763 statement
9764 statement-seq [opt] statement */
9766 static void
9767 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9769 /* Scan statements until there aren't any more. */
9770 while (true)
9772 cp_token *token = cp_lexer_peek_token (parser->lexer);
9774 /* If we are looking at a `}', then we have run out of
9775 statements; the same is true if we have reached the end
9776 of file, or have stumbled upon a stray '@end'. */
9777 if (token->type == CPP_CLOSE_BRACE
9778 || token->type == CPP_EOF
9779 || token->type == CPP_PRAGMA_EOL
9780 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9781 break;
9783 /* If we are in a compound statement and find 'else' then
9784 something went wrong. */
9785 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9787 if (parser->in_statement & IN_IF_STMT)
9788 break;
9789 else
9791 token = cp_lexer_consume_token (parser->lexer);
9792 error_at (token->location, "%<else%> without a previous %<if%>");
9796 /* Parse the statement. */
9797 cp_parser_statement (parser, in_statement_expr, true, NULL);
9801 /* Parse a selection-statement.
9803 selection-statement:
9804 if ( condition ) statement
9805 if ( condition ) statement else statement
9806 switch ( condition ) statement
9808 Returns the new IF_STMT or SWITCH_STMT.
9810 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9811 is a (possibly labeled) if statement which is not enclosed in
9812 braces and has an else clause. This is used to implement
9813 -Wparentheses. */
9815 static tree
9816 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9818 cp_token *token;
9819 enum rid keyword;
9821 if (if_p != NULL)
9822 *if_p = false;
9824 /* Peek at the next token. */
9825 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9827 /* See what kind of keyword it is. */
9828 keyword = token->keyword;
9829 switch (keyword)
9831 case RID_IF:
9832 case RID_SWITCH:
9834 tree statement;
9835 tree condition;
9837 /* Look for the `('. */
9838 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9840 cp_parser_skip_to_end_of_statement (parser);
9841 return error_mark_node;
9844 /* Begin the selection-statement. */
9845 if (keyword == RID_IF)
9846 statement = begin_if_stmt ();
9847 else
9848 statement = begin_switch_stmt ();
9850 /* Parse the condition. */
9851 condition = cp_parser_condition (parser);
9852 /* Look for the `)'. */
9853 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9854 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9855 /*consume_paren=*/true);
9857 if (keyword == RID_IF)
9859 bool nested_if;
9860 unsigned char in_statement;
9862 /* Add the condition. */
9863 finish_if_stmt_cond (condition, statement);
9865 /* Parse the then-clause. */
9866 in_statement = parser->in_statement;
9867 parser->in_statement |= IN_IF_STMT;
9868 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9870 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9871 add_stmt (build_empty_stmt (loc));
9872 cp_lexer_consume_token (parser->lexer);
9873 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9874 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9875 "empty body in an %<if%> statement");
9876 nested_if = false;
9878 else
9879 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9880 parser->in_statement = in_statement;
9882 finish_then_clause (statement);
9884 /* If the next token is `else', parse the else-clause. */
9885 if (cp_lexer_next_token_is_keyword (parser->lexer,
9886 RID_ELSE))
9888 /* Consume the `else' keyword. */
9889 cp_lexer_consume_token (parser->lexer);
9890 begin_else_clause (statement);
9891 /* Parse the else-clause. */
9892 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9894 location_t loc;
9895 loc = cp_lexer_peek_token (parser->lexer)->location;
9896 warning_at (loc,
9897 OPT_Wempty_body, "suggest braces around "
9898 "empty body in an %<else%> statement");
9899 add_stmt (build_empty_stmt (loc));
9900 cp_lexer_consume_token (parser->lexer);
9902 else
9903 cp_parser_implicitly_scoped_statement (parser, NULL);
9905 finish_else_clause (statement);
9907 /* If we are currently parsing a then-clause, then
9908 IF_P will not be NULL. We set it to true to
9909 indicate that this if statement has an else clause.
9910 This may trigger the Wparentheses warning below
9911 when we get back up to the parent if statement. */
9912 if (if_p != NULL)
9913 *if_p = true;
9915 else
9917 /* This if statement does not have an else clause. If
9918 NESTED_IF is true, then the then-clause is an if
9919 statement which does have an else clause. We warn
9920 about the potential ambiguity. */
9921 if (nested_if)
9922 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9923 "suggest explicit braces to avoid ambiguous"
9924 " %<else%>");
9927 /* Now we're all done with the if-statement. */
9928 finish_if_stmt (statement);
9930 else
9932 bool in_switch_statement_p;
9933 unsigned char in_statement;
9935 /* Add the condition. */
9936 finish_switch_cond (condition, statement);
9938 /* Parse the body of the switch-statement. */
9939 in_switch_statement_p = parser->in_switch_statement_p;
9940 in_statement = parser->in_statement;
9941 parser->in_switch_statement_p = true;
9942 parser->in_statement |= IN_SWITCH_STMT;
9943 cp_parser_implicitly_scoped_statement (parser, NULL);
9944 parser->in_switch_statement_p = in_switch_statement_p;
9945 parser->in_statement = in_statement;
9947 /* Now we're all done with the switch-statement. */
9948 finish_switch_stmt (statement);
9951 return statement;
9953 break;
9955 default:
9956 cp_parser_error (parser, "expected selection-statement");
9957 return error_mark_node;
9961 /* Parse a condition.
9963 condition:
9964 expression
9965 type-specifier-seq declarator = initializer-clause
9966 type-specifier-seq declarator braced-init-list
9968 GNU Extension:
9970 condition:
9971 type-specifier-seq declarator asm-specification [opt]
9972 attributes [opt] = assignment-expression
9974 Returns the expression that should be tested. */
9976 static tree
9977 cp_parser_condition (cp_parser* parser)
9979 cp_decl_specifier_seq type_specifiers;
9980 const char *saved_message;
9981 int declares_class_or_enum;
9983 /* Try the declaration first. */
9984 cp_parser_parse_tentatively (parser);
9985 /* New types are not allowed in the type-specifier-seq for a
9986 condition. */
9987 saved_message = parser->type_definition_forbidden_message;
9988 parser->type_definition_forbidden_message
9989 = G_("types may not be defined in conditions");
9990 /* Parse the type-specifier-seq. */
9991 cp_parser_decl_specifier_seq (parser,
9992 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9993 &type_specifiers,
9994 &declares_class_or_enum);
9995 /* Restore the saved message. */
9996 parser->type_definition_forbidden_message = saved_message;
9997 /* If all is well, we might be looking at a declaration. */
9998 if (!cp_parser_error_occurred (parser))
10000 tree decl;
10001 tree asm_specification;
10002 tree attributes;
10003 cp_declarator *declarator;
10004 tree initializer = NULL_TREE;
10006 /* Parse the declarator. */
10007 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10008 /*ctor_dtor_or_conv_p=*/NULL,
10009 /*parenthesized_p=*/NULL,
10010 /*member_p=*/false);
10011 /* Parse the attributes. */
10012 attributes = cp_parser_attributes_opt (parser);
10013 /* Parse the asm-specification. */
10014 asm_specification = cp_parser_asm_specification_opt (parser);
10015 /* If the next token is not an `=' or '{', then we might still be
10016 looking at an expression. For example:
10018 if (A(a).x)
10020 looks like a decl-specifier-seq and a declarator -- but then
10021 there is no `=', so this is an expression. */
10022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10023 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10024 cp_parser_simulate_error (parser);
10026 /* If we did see an `=' or '{', then we are looking at a declaration
10027 for sure. */
10028 if (cp_parser_parse_definitely (parser))
10030 tree pushed_scope;
10031 bool non_constant_p;
10032 bool flags = LOOKUP_ONLYCONVERTING;
10034 /* Create the declaration. */
10035 decl = start_decl (declarator, &type_specifiers,
10036 /*initialized_p=*/true,
10037 attributes, /*prefix_attributes=*/NULL_TREE,
10038 &pushed_scope);
10040 /* Parse the initializer. */
10041 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10043 initializer = cp_parser_braced_list (parser, &non_constant_p);
10044 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10045 flags = 0;
10047 else
10049 /* Consume the `='. */
10050 cp_parser_require (parser, CPP_EQ, RT_EQ);
10051 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10053 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10054 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10056 /* Process the initializer. */
10057 cp_finish_decl (decl,
10058 initializer, !non_constant_p,
10059 asm_specification,
10060 flags);
10062 if (pushed_scope)
10063 pop_scope (pushed_scope);
10065 return convert_from_reference (decl);
10068 /* If we didn't even get past the declarator successfully, we are
10069 definitely not looking at a declaration. */
10070 else
10071 cp_parser_abort_tentative_parse (parser);
10073 /* Otherwise, we are looking at an expression. */
10074 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10077 /* Parses a for-statement or range-for-statement until the closing ')',
10078 not included. */
10080 static tree
10081 cp_parser_for (cp_parser *parser, bool ivdep)
10083 tree init, scope, decl;
10084 bool is_range_for;
10086 /* Begin the for-statement. */
10087 scope = begin_for_scope (&init);
10089 /* Parse the initialization. */
10090 is_range_for = cp_parser_for_init_statement (parser, &decl);
10092 if (is_range_for)
10093 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10094 else
10095 return cp_parser_c_for (parser, scope, init, ivdep);
10098 static tree
10099 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10101 /* Normal for loop */
10102 tree condition = NULL_TREE;
10103 tree expression = NULL_TREE;
10104 tree stmt;
10106 stmt = begin_for_stmt (scope, init);
10107 /* The for-init-statement has already been parsed in
10108 cp_parser_for_init_statement, so no work is needed here. */
10109 finish_for_init_stmt (stmt);
10111 /* If there's a condition, process it. */
10112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10113 condition = cp_parser_condition (parser);
10114 else if (ivdep)
10116 cp_parser_error (parser, "missing loop condition in loop with "
10117 "%<GCC ivdep%> pragma");
10118 condition = error_mark_node;
10120 finish_for_cond (condition, stmt, ivdep);
10121 /* Look for the `;'. */
10122 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10124 /* If there's an expression, process it. */
10125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10126 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10127 finish_for_expr (expression, stmt);
10129 return stmt;
10132 /* Tries to parse a range-based for-statement:
10134 range-based-for:
10135 decl-specifier-seq declarator : expression
10137 The decl-specifier-seq declarator and the `:' are already parsed by
10138 cp_parser_for_init_statement. If processing_template_decl it returns a
10139 newly created RANGE_FOR_STMT; if not, it is converted to a
10140 regular FOR_STMT. */
10142 static tree
10143 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10144 bool ivdep)
10146 tree stmt, range_expr;
10148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10150 bool expr_non_constant_p;
10151 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10153 else
10154 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10156 /* If in template, STMT is converted to a normal for-statement
10157 at instantiation. If not, it is done just ahead. */
10158 if (processing_template_decl)
10160 if (check_for_bare_parameter_packs (range_expr))
10161 range_expr = error_mark_node;
10162 stmt = begin_range_for_stmt (scope, init);
10163 if (ivdep)
10164 RANGE_FOR_IVDEP (stmt) = 1;
10165 finish_range_for_decl (stmt, range_decl, range_expr);
10166 if (!type_dependent_expression_p (range_expr)
10167 /* do_auto_deduction doesn't mess with template init-lists. */
10168 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10169 do_range_for_auto_deduction (range_decl, range_expr);
10171 else
10173 stmt = begin_for_stmt (scope, init);
10174 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10176 return stmt;
10179 /* Subroutine of cp_convert_range_for: given the initializer expression,
10180 builds up the range temporary. */
10182 static tree
10183 build_range_temp (tree range_expr)
10185 tree range_type, range_temp;
10187 /* Find out the type deduced by the declaration
10188 `auto &&__range = range_expr'. */
10189 range_type = cp_build_reference_type (make_auto (), true);
10190 range_type = do_auto_deduction (range_type, range_expr,
10191 type_uses_auto (range_type));
10193 /* Create the __range variable. */
10194 range_temp = build_decl (input_location, VAR_DECL,
10195 get_identifier ("__for_range"), range_type);
10196 TREE_USED (range_temp) = 1;
10197 DECL_ARTIFICIAL (range_temp) = 1;
10199 return range_temp;
10202 /* Used by cp_parser_range_for in template context: we aren't going to
10203 do a full conversion yet, but we still need to resolve auto in the
10204 type of the for-range-declaration if present. This is basically
10205 a shortcut version of cp_convert_range_for. */
10207 static void
10208 do_range_for_auto_deduction (tree decl, tree range_expr)
10210 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10211 if (auto_node)
10213 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10214 range_temp = convert_from_reference (build_range_temp (range_expr));
10215 iter_type = (cp_parser_perform_range_for_lookup
10216 (range_temp, &begin_dummy, &end_dummy));
10217 if (iter_type)
10219 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10220 iter_type);
10221 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10222 tf_warning_or_error);
10223 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10224 iter_decl, auto_node);
10229 /* Converts a range-based for-statement into a normal
10230 for-statement, as per the definition.
10232 for (RANGE_DECL : RANGE_EXPR)
10233 BLOCK
10235 should be equivalent to:
10238 auto &&__range = RANGE_EXPR;
10239 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10240 __begin != __end;
10241 ++__begin)
10243 RANGE_DECL = *__begin;
10244 BLOCK
10248 If RANGE_EXPR is an array:
10249 BEGIN_EXPR = __range
10250 END_EXPR = __range + ARRAY_SIZE(__range)
10251 Else if RANGE_EXPR has a member 'begin' or 'end':
10252 BEGIN_EXPR = __range.begin()
10253 END_EXPR = __range.end()
10254 Else:
10255 BEGIN_EXPR = begin(__range)
10256 END_EXPR = end(__range);
10258 If __range has a member 'begin' but not 'end', or vice versa, we must
10259 still use the second alternative (it will surely fail, however).
10260 When calling begin()/end() in the third alternative we must use
10261 argument dependent lookup, but always considering 'std' as an associated
10262 namespace. */
10264 tree
10265 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10266 bool ivdep)
10268 tree begin, end;
10269 tree iter_type, begin_expr, end_expr;
10270 tree condition, expression;
10272 if (range_decl == error_mark_node || range_expr == error_mark_node)
10273 /* If an error happened previously do nothing or else a lot of
10274 unhelpful errors would be issued. */
10275 begin_expr = end_expr = iter_type = error_mark_node;
10276 else
10278 tree range_temp;
10280 if (TREE_CODE (range_expr) == VAR_DECL
10281 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10282 /* Can't bind a reference to an array of runtime bound. */
10283 range_temp = range_expr;
10284 else
10286 range_temp = build_range_temp (range_expr);
10287 pushdecl (range_temp);
10288 cp_finish_decl (range_temp, range_expr,
10289 /*is_constant_init*/false, NULL_TREE,
10290 LOOKUP_ONLYCONVERTING);
10291 range_temp = convert_from_reference (range_temp);
10293 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10294 &begin_expr, &end_expr);
10297 /* The new for initialization statement. */
10298 begin = build_decl (input_location, VAR_DECL,
10299 get_identifier ("__for_begin"), iter_type);
10300 TREE_USED (begin) = 1;
10301 DECL_ARTIFICIAL (begin) = 1;
10302 pushdecl (begin);
10303 cp_finish_decl (begin, begin_expr,
10304 /*is_constant_init*/false, NULL_TREE,
10305 LOOKUP_ONLYCONVERTING);
10307 end = build_decl (input_location, VAR_DECL,
10308 get_identifier ("__for_end"), iter_type);
10309 TREE_USED (end) = 1;
10310 DECL_ARTIFICIAL (end) = 1;
10311 pushdecl (end);
10312 cp_finish_decl (end, end_expr,
10313 /*is_constant_init*/false, NULL_TREE,
10314 LOOKUP_ONLYCONVERTING);
10316 finish_for_init_stmt (statement);
10318 /* The new for condition. */
10319 condition = build_x_binary_op (input_location, NE_EXPR,
10320 begin, ERROR_MARK,
10321 end, ERROR_MARK,
10322 NULL, tf_warning_or_error);
10323 finish_for_cond (condition, statement, ivdep);
10325 /* The new increment expression. */
10326 expression = finish_unary_op_expr (input_location,
10327 PREINCREMENT_EXPR, begin,
10328 tf_warning_or_error);
10329 finish_for_expr (expression, statement);
10331 /* The declaration is initialized with *__begin inside the loop body. */
10332 cp_finish_decl (range_decl,
10333 build_x_indirect_ref (input_location, begin, RO_NULL,
10334 tf_warning_or_error),
10335 /*is_constant_init*/false, NULL_TREE,
10336 LOOKUP_ONLYCONVERTING);
10338 return statement;
10341 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10342 We need to solve both at the same time because the method used
10343 depends on the existence of members begin or end.
10344 Returns the type deduced for the iterator expression. */
10346 static tree
10347 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10349 if (error_operand_p (range))
10351 *begin = *end = error_mark_node;
10352 return error_mark_node;
10355 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10357 error ("range-based %<for%> expression of type %qT "
10358 "has incomplete type", TREE_TYPE (range));
10359 *begin = *end = error_mark_node;
10360 return error_mark_node;
10362 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10364 /* If RANGE is an array, we will use pointer arithmetic. */
10365 *begin = range;
10366 *end = build_binary_op (input_location, PLUS_EXPR,
10367 range,
10368 array_type_nelts_top (TREE_TYPE (range)),
10370 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10372 else
10374 /* If it is not an array, we must do a bit of magic. */
10375 tree id_begin, id_end;
10376 tree member_begin, member_end;
10378 *begin = *end = error_mark_node;
10380 id_begin = get_identifier ("begin");
10381 id_end = get_identifier ("end");
10382 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10383 /*protect=*/2, /*want_type=*/false,
10384 tf_warning_or_error);
10385 member_end = lookup_member (TREE_TYPE (range), id_end,
10386 /*protect=*/2, /*want_type=*/false,
10387 tf_warning_or_error);
10389 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10391 /* Use the member functions. */
10392 if (member_begin != NULL_TREE)
10393 *begin = cp_parser_range_for_member_function (range, id_begin);
10394 else
10395 error ("range-based %<for%> expression of type %qT has an "
10396 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10398 if (member_end != NULL_TREE)
10399 *end = cp_parser_range_for_member_function (range, id_end);
10400 else
10401 error ("range-based %<for%> expression of type %qT has a "
10402 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10404 else
10406 /* Use global functions with ADL. */
10407 vec<tree, va_gc> *vec;
10408 vec = make_tree_vector ();
10410 vec_safe_push (vec, range);
10412 member_begin = perform_koenig_lookup (id_begin, vec,
10413 tf_warning_or_error);
10414 *begin = finish_call_expr (member_begin, &vec, false, true,
10415 tf_warning_or_error);
10416 member_end = perform_koenig_lookup (id_end, vec,
10417 tf_warning_or_error);
10418 *end = finish_call_expr (member_end, &vec, false, true,
10419 tf_warning_or_error);
10421 release_tree_vector (vec);
10424 /* Last common checks. */
10425 if (*begin == error_mark_node || *end == error_mark_node)
10427 /* If one of the expressions is an error do no more checks. */
10428 *begin = *end = error_mark_node;
10429 return error_mark_node;
10431 else if (type_dependent_expression_p (*begin)
10432 || type_dependent_expression_p (*end))
10433 /* Can happen, when, eg, in a template context, Koenig lookup
10434 can't resolve begin/end (c++/58503). */
10435 return NULL_TREE;
10436 else
10438 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10439 /* The unqualified type of the __begin and __end temporaries should
10440 be the same, as required by the multiple auto declaration. */
10441 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10442 error ("inconsistent begin/end types in range-based %<for%> "
10443 "statement: %qT and %qT",
10444 TREE_TYPE (*begin), TREE_TYPE (*end));
10445 return iter_type;
10450 /* Helper function for cp_parser_perform_range_for_lookup.
10451 Builds a tree for RANGE.IDENTIFIER(). */
10453 static tree
10454 cp_parser_range_for_member_function (tree range, tree identifier)
10456 tree member, res;
10457 vec<tree, va_gc> *vec;
10459 member = finish_class_member_access_expr (range, identifier,
10460 false, tf_warning_or_error);
10461 if (member == error_mark_node)
10462 return error_mark_node;
10464 vec = make_tree_vector ();
10465 res = finish_call_expr (member, &vec,
10466 /*disallow_virtual=*/false,
10467 /*koenig_p=*/false,
10468 tf_warning_or_error);
10469 release_tree_vector (vec);
10470 return res;
10473 /* Parse an iteration-statement.
10475 iteration-statement:
10476 while ( condition ) statement
10477 do statement while ( expression ) ;
10478 for ( for-init-statement condition [opt] ; expression [opt] )
10479 statement
10481 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10483 static tree
10484 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10486 cp_token *token;
10487 enum rid keyword;
10488 tree statement;
10489 unsigned char in_statement;
10491 /* Peek at the next token. */
10492 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10493 if (!token)
10494 return error_mark_node;
10496 /* Remember whether or not we are already within an iteration
10497 statement. */
10498 in_statement = parser->in_statement;
10500 /* See what kind of keyword it is. */
10501 keyword = token->keyword;
10502 switch (keyword)
10504 case RID_WHILE:
10506 tree condition;
10508 /* Begin the while-statement. */
10509 statement = begin_while_stmt ();
10510 /* Look for the `('. */
10511 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10512 /* Parse the condition. */
10513 condition = cp_parser_condition (parser);
10514 finish_while_stmt_cond (condition, statement, ivdep);
10515 /* Look for the `)'. */
10516 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10517 /* Parse the dependent statement. */
10518 parser->in_statement = IN_ITERATION_STMT;
10519 cp_parser_already_scoped_statement (parser);
10520 parser->in_statement = in_statement;
10521 /* We're done with the while-statement. */
10522 finish_while_stmt (statement);
10524 break;
10526 case RID_DO:
10528 tree expression;
10530 /* Begin the do-statement. */
10531 statement = begin_do_stmt ();
10532 /* Parse the body of the do-statement. */
10533 parser->in_statement = IN_ITERATION_STMT;
10534 cp_parser_implicitly_scoped_statement (parser, NULL);
10535 parser->in_statement = in_statement;
10536 finish_do_body (statement);
10537 /* Look for the `while' keyword. */
10538 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10539 /* Look for the `('. */
10540 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10541 /* Parse the expression. */
10542 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10543 /* We're done with the do-statement. */
10544 finish_do_stmt (expression, statement, ivdep);
10545 /* Look for the `)'. */
10546 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10547 /* Look for the `;'. */
10548 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10550 break;
10552 case RID_FOR:
10554 /* Look for the `('. */
10555 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10557 statement = cp_parser_for (parser, ivdep);
10559 /* Look for the `)'. */
10560 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10562 /* Parse the body of the for-statement. */
10563 parser->in_statement = IN_ITERATION_STMT;
10564 cp_parser_already_scoped_statement (parser);
10565 parser->in_statement = in_statement;
10567 /* We're done with the for-statement. */
10568 finish_for_stmt (statement);
10570 break;
10572 default:
10573 cp_parser_error (parser, "expected iteration-statement");
10574 statement = error_mark_node;
10575 break;
10578 return statement;
10581 /* Parse a for-init-statement or the declarator of a range-based-for.
10582 Returns true if a range-based-for declaration is seen.
10584 for-init-statement:
10585 expression-statement
10586 simple-declaration */
10588 static bool
10589 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10591 /* If the next token is a `;', then we have an empty
10592 expression-statement. Grammatically, this is also a
10593 simple-declaration, but an invalid one, because it does not
10594 declare anything. Therefore, if we did not handle this case
10595 specially, we would issue an error message about an invalid
10596 declaration. */
10597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10599 bool is_range_for = false;
10600 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10602 parser->colon_corrects_to_scope_p = false;
10604 /* We're going to speculatively look for a declaration, falling back
10605 to an expression, if necessary. */
10606 cp_parser_parse_tentatively (parser);
10607 /* Parse the declaration. */
10608 cp_parser_simple_declaration (parser,
10609 /*function_definition_allowed_p=*/false,
10610 decl);
10611 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10612 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10614 /* It is a range-for, consume the ':' */
10615 cp_lexer_consume_token (parser->lexer);
10616 is_range_for = true;
10617 if (cxx_dialect < cxx11)
10619 error_at (cp_lexer_peek_token (parser->lexer)->location,
10620 "range-based %<for%> loops are not allowed "
10621 "in C++98 mode");
10622 *decl = error_mark_node;
10625 else
10626 /* The ';' is not consumed yet because we told
10627 cp_parser_simple_declaration not to. */
10628 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10630 if (cp_parser_parse_definitely (parser))
10631 return is_range_for;
10632 /* If the tentative parse failed, then we shall need to look for an
10633 expression-statement. */
10635 /* If we are here, it is an expression-statement. */
10636 cp_parser_expression_statement (parser, NULL_TREE);
10637 return false;
10640 /* Parse a jump-statement.
10642 jump-statement:
10643 break ;
10644 continue ;
10645 return expression [opt] ;
10646 return braced-init-list ;
10647 goto identifier ;
10649 GNU extension:
10651 jump-statement:
10652 goto * expression ;
10654 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10656 static tree
10657 cp_parser_jump_statement (cp_parser* parser)
10659 tree statement = error_mark_node;
10660 cp_token *token;
10661 enum rid keyword;
10662 unsigned char in_statement;
10664 /* Peek at the next token. */
10665 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10666 if (!token)
10667 return error_mark_node;
10669 /* See what kind of keyword it is. */
10670 keyword = token->keyword;
10671 switch (keyword)
10673 case RID_BREAK:
10674 in_statement = parser->in_statement & ~IN_IF_STMT;
10675 switch (in_statement)
10677 case 0:
10678 error_at (token->location, "break statement not within loop or switch");
10679 break;
10680 default:
10681 gcc_assert ((in_statement & IN_SWITCH_STMT)
10682 || in_statement == IN_ITERATION_STMT);
10683 statement = finish_break_stmt ();
10684 if (in_statement == IN_ITERATION_STMT)
10685 break_maybe_infinite_loop ();
10686 break;
10687 case IN_OMP_BLOCK:
10688 error_at (token->location, "invalid exit from OpenMP structured block");
10689 break;
10690 case IN_OMP_FOR:
10691 error_at (token->location, "break statement used with OpenMP for loop");
10692 break;
10693 case IN_CILK_SIMD_FOR:
10694 error_at (token->location, "break statement used with Cilk Plus for loop");
10695 break;
10697 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10698 break;
10700 case RID_CONTINUE:
10701 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10703 case 0:
10704 error_at (token->location, "continue statement not within a loop");
10705 break;
10706 case IN_CILK_SIMD_FOR:
10707 error_at (token->location,
10708 "continue statement within %<#pragma simd%> loop body");
10709 /* Fall through. */
10710 case IN_ITERATION_STMT:
10711 case IN_OMP_FOR:
10712 statement = finish_continue_stmt ();
10713 break;
10714 case IN_OMP_BLOCK:
10715 error_at (token->location, "invalid exit from OpenMP structured block");
10716 break;
10717 default:
10718 gcc_unreachable ();
10720 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10721 break;
10723 case RID_RETURN:
10725 tree expr;
10726 bool expr_non_constant_p;
10728 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10730 cp_lexer_set_source_position (parser->lexer);
10731 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10732 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10734 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10735 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10736 else
10737 /* If the next token is a `;', then there is no
10738 expression. */
10739 expr = NULL_TREE;
10740 /* Build the return-statement. */
10741 statement = finish_return_stmt (expr);
10742 /* Look for the final `;'. */
10743 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10745 break;
10747 case RID_GOTO:
10748 /* Create the goto-statement. */
10749 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10751 /* Issue a warning about this use of a GNU extension. */
10752 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10753 /* Consume the '*' token. */
10754 cp_lexer_consume_token (parser->lexer);
10755 /* Parse the dependent expression. */
10756 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10758 else
10759 finish_goto_stmt (cp_parser_identifier (parser));
10760 /* Look for the final `;'. */
10761 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10762 break;
10764 default:
10765 cp_parser_error (parser, "expected jump-statement");
10766 break;
10769 return statement;
10772 /* Parse a declaration-statement.
10774 declaration-statement:
10775 block-declaration */
10777 static void
10778 cp_parser_declaration_statement (cp_parser* parser)
10780 void *p;
10782 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10783 p = obstack_alloc (&declarator_obstack, 0);
10785 /* Parse the block-declaration. */
10786 cp_parser_block_declaration (parser, /*statement_p=*/true);
10788 /* Free any declarators allocated. */
10789 obstack_free (&declarator_obstack, p);
10792 /* Some dependent statements (like `if (cond) statement'), are
10793 implicitly in their own scope. In other words, if the statement is
10794 a single statement (as opposed to a compound-statement), it is
10795 none-the-less treated as if it were enclosed in braces. Any
10796 declarations appearing in the dependent statement are out of scope
10797 after control passes that point. This function parses a statement,
10798 but ensures that is in its own scope, even if it is not a
10799 compound-statement.
10801 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10802 is a (possibly labeled) if statement which is not enclosed in
10803 braces and has an else clause. This is used to implement
10804 -Wparentheses.
10806 Returns the new statement. */
10808 static tree
10809 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10811 tree statement;
10813 if (if_p != NULL)
10814 *if_p = false;
10816 /* Mark if () ; with a special NOP_EXPR. */
10817 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10819 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10820 cp_lexer_consume_token (parser->lexer);
10821 statement = add_stmt (build_empty_stmt (loc));
10823 /* if a compound is opened, we simply parse the statement directly. */
10824 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10825 statement = cp_parser_compound_statement (parser, NULL, false, false);
10826 /* If the token is not a `{', then we must take special action. */
10827 else
10829 /* Create a compound-statement. */
10830 statement = begin_compound_stmt (0);
10831 /* Parse the dependent-statement. */
10832 cp_parser_statement (parser, NULL_TREE, false, if_p);
10833 /* Finish the dummy compound-statement. */
10834 finish_compound_stmt (statement);
10837 /* Return the statement. */
10838 return statement;
10841 /* For some dependent statements (like `while (cond) statement'), we
10842 have already created a scope. Therefore, even if the dependent
10843 statement is a compound-statement, we do not want to create another
10844 scope. */
10846 static void
10847 cp_parser_already_scoped_statement (cp_parser* parser)
10849 /* If the token is a `{', then we must take special action. */
10850 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10851 cp_parser_statement (parser, NULL_TREE, false, NULL);
10852 else
10854 /* Avoid calling cp_parser_compound_statement, so that we
10855 don't create a new scope. Do everything else by hand. */
10856 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10857 /* If the next keyword is `__label__' we have a label declaration. */
10858 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10859 cp_parser_label_declaration (parser);
10860 /* Parse an (optional) statement-seq. */
10861 cp_parser_statement_seq_opt (parser, NULL_TREE);
10862 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10866 /* Declarations [gram.dcl.dcl] */
10868 /* Parse an optional declaration-sequence.
10870 declaration-seq:
10871 declaration
10872 declaration-seq declaration */
10874 static void
10875 cp_parser_declaration_seq_opt (cp_parser* parser)
10877 while (true)
10879 cp_token *token;
10881 token = cp_lexer_peek_token (parser->lexer);
10883 if (token->type == CPP_CLOSE_BRACE
10884 || token->type == CPP_EOF
10885 || token->type == CPP_PRAGMA_EOL)
10886 break;
10888 if (token->type == CPP_SEMICOLON)
10890 /* A declaration consisting of a single semicolon is
10891 invalid. Allow it unless we're being pedantic. */
10892 cp_lexer_consume_token (parser->lexer);
10893 if (!in_system_header_at (input_location))
10894 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10895 continue;
10898 /* If we're entering or exiting a region that's implicitly
10899 extern "C", modify the lang context appropriately. */
10900 if (!parser->implicit_extern_c && token->implicit_extern_c)
10902 push_lang_context (lang_name_c);
10903 parser->implicit_extern_c = true;
10905 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10907 pop_lang_context ();
10908 parser->implicit_extern_c = false;
10911 if (token->type == CPP_PRAGMA)
10913 /* A top-level declaration can consist solely of a #pragma.
10914 A nested declaration cannot, so this is done here and not
10915 in cp_parser_declaration. (A #pragma at block scope is
10916 handled in cp_parser_statement.) */
10917 cp_parser_pragma (parser, pragma_external);
10918 continue;
10921 /* Parse the declaration itself. */
10922 cp_parser_declaration (parser);
10926 /* Parse a declaration.
10928 declaration:
10929 block-declaration
10930 function-definition
10931 template-declaration
10932 explicit-instantiation
10933 explicit-specialization
10934 linkage-specification
10935 namespace-definition
10937 GNU extension:
10939 declaration:
10940 __extension__ declaration */
10942 static void
10943 cp_parser_declaration (cp_parser* parser)
10945 cp_token token1;
10946 cp_token token2;
10947 int saved_pedantic;
10948 void *p;
10949 tree attributes = NULL_TREE;
10951 /* Check for the `__extension__' keyword. */
10952 if (cp_parser_extension_opt (parser, &saved_pedantic))
10954 /* Parse the qualified declaration. */
10955 cp_parser_declaration (parser);
10956 /* Restore the PEDANTIC flag. */
10957 pedantic = saved_pedantic;
10959 return;
10962 /* Try to figure out what kind of declaration is present. */
10963 token1 = *cp_lexer_peek_token (parser->lexer);
10965 if (token1.type != CPP_EOF)
10966 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10967 else
10969 token2.type = CPP_EOF;
10970 token2.keyword = RID_MAX;
10973 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10974 p = obstack_alloc (&declarator_obstack, 0);
10976 /* If the next token is `extern' and the following token is a string
10977 literal, then we have a linkage specification. */
10978 if (token1.keyword == RID_EXTERN
10979 && cp_parser_is_pure_string_literal (&token2))
10980 cp_parser_linkage_specification (parser);
10981 /* If the next token is `template', then we have either a template
10982 declaration, an explicit instantiation, or an explicit
10983 specialization. */
10984 else if (token1.keyword == RID_TEMPLATE)
10986 /* `template <>' indicates a template specialization. */
10987 if (token2.type == CPP_LESS
10988 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10989 cp_parser_explicit_specialization (parser);
10990 /* `template <' indicates a template declaration. */
10991 else if (token2.type == CPP_LESS)
10992 cp_parser_template_declaration (parser, /*member_p=*/false);
10993 /* Anything else must be an explicit instantiation. */
10994 else
10995 cp_parser_explicit_instantiation (parser);
10997 /* If the next token is `export', then we have a template
10998 declaration. */
10999 else if (token1.keyword == RID_EXPORT)
11000 cp_parser_template_declaration (parser, /*member_p=*/false);
11001 /* If the next token is `extern', 'static' or 'inline' and the one
11002 after that is `template', we have a GNU extended explicit
11003 instantiation directive. */
11004 else if (cp_parser_allow_gnu_extensions_p (parser)
11005 && (token1.keyword == RID_EXTERN
11006 || token1.keyword == RID_STATIC
11007 || token1.keyword == RID_INLINE)
11008 && token2.keyword == RID_TEMPLATE)
11009 cp_parser_explicit_instantiation (parser);
11010 /* If the next token is `namespace', check for a named or unnamed
11011 namespace definition. */
11012 else if (token1.keyword == RID_NAMESPACE
11013 && (/* A named namespace definition. */
11014 (token2.type == CPP_NAME
11015 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11016 != CPP_EQ))
11017 /* An unnamed namespace definition. */
11018 || token2.type == CPP_OPEN_BRACE
11019 || token2.keyword == RID_ATTRIBUTE))
11020 cp_parser_namespace_definition (parser);
11021 /* An inline (associated) namespace definition. */
11022 else if (token1.keyword == RID_INLINE
11023 && token2.keyword == RID_NAMESPACE)
11024 cp_parser_namespace_definition (parser);
11025 /* Objective-C++ declaration/definition. */
11026 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11027 cp_parser_objc_declaration (parser, NULL_TREE);
11028 else if (c_dialect_objc ()
11029 && token1.keyword == RID_ATTRIBUTE
11030 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11031 cp_parser_objc_declaration (parser, attributes);
11032 /* We must have either a block declaration or a function
11033 definition. */
11034 else
11035 /* Try to parse a block-declaration, or a function-definition. */
11036 cp_parser_block_declaration (parser, /*statement_p=*/false);
11038 /* Free any declarators allocated. */
11039 obstack_free (&declarator_obstack, p);
11042 /* Parse a block-declaration.
11044 block-declaration:
11045 simple-declaration
11046 asm-definition
11047 namespace-alias-definition
11048 using-declaration
11049 using-directive
11051 GNU Extension:
11053 block-declaration:
11054 __extension__ block-declaration
11056 C++0x Extension:
11058 block-declaration:
11059 static_assert-declaration
11061 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11062 part of a declaration-statement. */
11064 static void
11065 cp_parser_block_declaration (cp_parser *parser,
11066 bool statement_p)
11068 cp_token *token1;
11069 int saved_pedantic;
11071 /* Check for the `__extension__' keyword. */
11072 if (cp_parser_extension_opt (parser, &saved_pedantic))
11074 /* Parse the qualified declaration. */
11075 cp_parser_block_declaration (parser, statement_p);
11076 /* Restore the PEDANTIC flag. */
11077 pedantic = saved_pedantic;
11079 return;
11082 /* Peek at the next token to figure out which kind of declaration is
11083 present. */
11084 token1 = cp_lexer_peek_token (parser->lexer);
11086 /* If the next keyword is `asm', we have an asm-definition. */
11087 if (token1->keyword == RID_ASM)
11089 if (statement_p)
11090 cp_parser_commit_to_tentative_parse (parser);
11091 cp_parser_asm_definition (parser);
11093 /* If the next keyword is `namespace', we have a
11094 namespace-alias-definition. */
11095 else if (token1->keyword == RID_NAMESPACE)
11096 cp_parser_namespace_alias_definition (parser);
11097 /* If the next keyword is `using', we have a
11098 using-declaration, a using-directive, or an alias-declaration. */
11099 else if (token1->keyword == RID_USING)
11101 cp_token *token2;
11103 if (statement_p)
11104 cp_parser_commit_to_tentative_parse (parser);
11105 /* If the token after `using' is `namespace', then we have a
11106 using-directive. */
11107 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11108 if (token2->keyword == RID_NAMESPACE)
11109 cp_parser_using_directive (parser);
11110 /* If the second token after 'using' is '=', then we have an
11111 alias-declaration. */
11112 else if (cxx_dialect >= cxx11
11113 && token2->type == CPP_NAME
11114 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11115 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11116 cp_parser_alias_declaration (parser);
11117 /* Otherwise, it's a using-declaration. */
11118 else
11119 cp_parser_using_declaration (parser,
11120 /*access_declaration_p=*/false);
11122 /* If the next keyword is `__label__' we have a misplaced label
11123 declaration. */
11124 else if (token1->keyword == RID_LABEL)
11126 cp_lexer_consume_token (parser->lexer);
11127 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11128 cp_parser_skip_to_end_of_statement (parser);
11129 /* If the next token is now a `;', consume it. */
11130 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11131 cp_lexer_consume_token (parser->lexer);
11133 /* If the next token is `static_assert' we have a static assertion. */
11134 else if (token1->keyword == RID_STATIC_ASSERT)
11135 cp_parser_static_assert (parser, /*member_p=*/false);
11136 /* Anything else must be a simple-declaration. */
11137 else
11138 cp_parser_simple_declaration (parser, !statement_p,
11139 /*maybe_range_for_decl*/NULL);
11142 /* Parse a simple-declaration.
11144 simple-declaration:
11145 decl-specifier-seq [opt] init-declarator-list [opt] ;
11147 init-declarator-list:
11148 init-declarator
11149 init-declarator-list , init-declarator
11151 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11152 function-definition as a simple-declaration.
11154 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11155 parsed declaration if it is an uninitialized single declarator not followed
11156 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11157 if present, will not be consumed. */
11159 static void
11160 cp_parser_simple_declaration (cp_parser* parser,
11161 bool function_definition_allowed_p,
11162 tree *maybe_range_for_decl)
11164 cp_decl_specifier_seq decl_specifiers;
11165 int declares_class_or_enum;
11166 bool saw_declarator;
11168 if (maybe_range_for_decl)
11169 *maybe_range_for_decl = NULL_TREE;
11171 /* Defer access checks until we know what is being declared; the
11172 checks for names appearing in the decl-specifier-seq should be
11173 done as if we were in the scope of the thing being declared. */
11174 push_deferring_access_checks (dk_deferred);
11176 /* Parse the decl-specifier-seq. We have to keep track of whether
11177 or not the decl-specifier-seq declares a named class or
11178 enumeration type, since that is the only case in which the
11179 init-declarator-list is allowed to be empty.
11181 [dcl.dcl]
11183 In a simple-declaration, the optional init-declarator-list can be
11184 omitted only when declaring a class or enumeration, that is when
11185 the decl-specifier-seq contains either a class-specifier, an
11186 elaborated-type-specifier, or an enum-specifier. */
11187 cp_parser_decl_specifier_seq (parser,
11188 CP_PARSER_FLAGS_OPTIONAL,
11189 &decl_specifiers,
11190 &declares_class_or_enum);
11191 /* We no longer need to defer access checks. */
11192 stop_deferring_access_checks ();
11194 /* In a block scope, a valid declaration must always have a
11195 decl-specifier-seq. By not trying to parse declarators, we can
11196 resolve the declaration/expression ambiguity more quickly. */
11197 if (!function_definition_allowed_p
11198 && !decl_specifiers.any_specifiers_p)
11200 cp_parser_error (parser, "expected declaration");
11201 goto done;
11204 /* If the next two tokens are both identifiers, the code is
11205 erroneous. The usual cause of this situation is code like:
11207 T t;
11209 where "T" should name a type -- but does not. */
11210 if (!decl_specifiers.any_type_specifiers_p
11211 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11213 /* If parsing tentatively, we should commit; we really are
11214 looking at a declaration. */
11215 cp_parser_commit_to_tentative_parse (parser);
11216 /* Give up. */
11217 goto done;
11220 /* If we have seen at least one decl-specifier, and the next token
11221 is not a parenthesis, then we must be looking at a declaration.
11222 (After "int (" we might be looking at a functional cast.) */
11223 if (decl_specifiers.any_specifiers_p
11224 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11225 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11226 && !cp_parser_error_occurred (parser))
11227 cp_parser_commit_to_tentative_parse (parser);
11229 /* Keep going until we hit the `;' at the end of the simple
11230 declaration. */
11231 saw_declarator = false;
11232 while (cp_lexer_next_token_is_not (parser->lexer,
11233 CPP_SEMICOLON))
11235 cp_token *token;
11236 bool function_definition_p;
11237 tree decl;
11239 if (saw_declarator)
11241 /* If we are processing next declarator, coma is expected */
11242 token = cp_lexer_peek_token (parser->lexer);
11243 gcc_assert (token->type == CPP_COMMA);
11244 cp_lexer_consume_token (parser->lexer);
11245 if (maybe_range_for_decl)
11246 *maybe_range_for_decl = error_mark_node;
11248 else
11249 saw_declarator = true;
11251 /* Parse the init-declarator. */
11252 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11253 /*checks=*/NULL,
11254 function_definition_allowed_p,
11255 /*member_p=*/false,
11256 declares_class_or_enum,
11257 &function_definition_p,
11258 maybe_range_for_decl);
11259 /* If an error occurred while parsing tentatively, exit quickly.
11260 (That usually happens when in the body of a function; each
11261 statement is treated as a declaration-statement until proven
11262 otherwise.) */
11263 if (cp_parser_error_occurred (parser))
11264 goto done;
11265 /* Handle function definitions specially. */
11266 if (function_definition_p)
11268 /* If the next token is a `,', then we are probably
11269 processing something like:
11271 void f() {}, *p;
11273 which is erroneous. */
11274 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11276 cp_token *token = cp_lexer_peek_token (parser->lexer);
11277 error_at (token->location,
11278 "mixing"
11279 " declarations and function-definitions is forbidden");
11281 /* Otherwise, we're done with the list of declarators. */
11282 else
11284 pop_deferring_access_checks ();
11285 return;
11288 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11289 *maybe_range_for_decl = decl;
11290 /* The next token should be either a `,' or a `;'. */
11291 token = cp_lexer_peek_token (parser->lexer);
11292 /* If it's a `,', there are more declarators to come. */
11293 if (token->type == CPP_COMMA)
11294 /* will be consumed next time around */;
11295 /* If it's a `;', we are done. */
11296 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11297 break;
11298 /* Anything else is an error. */
11299 else
11301 /* If we have already issued an error message we don't need
11302 to issue another one. */
11303 if (decl != error_mark_node
11304 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11305 cp_parser_error (parser, "expected %<,%> or %<;%>");
11306 /* Skip tokens until we reach the end of the statement. */
11307 cp_parser_skip_to_end_of_statement (parser);
11308 /* If the next token is now a `;', consume it. */
11309 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11310 cp_lexer_consume_token (parser->lexer);
11311 goto done;
11313 /* After the first time around, a function-definition is not
11314 allowed -- even if it was OK at first. For example:
11316 int i, f() {}
11318 is not valid. */
11319 function_definition_allowed_p = false;
11322 /* Issue an error message if no declarators are present, and the
11323 decl-specifier-seq does not itself declare a class or
11324 enumeration: [dcl.dcl]/3. */
11325 if (!saw_declarator)
11327 if (cp_parser_declares_only_class_p (parser))
11329 if (!declares_class_or_enum
11330 && decl_specifiers.type
11331 && OVERLOAD_TYPE_P (decl_specifiers.type))
11332 /* Ensure an error is issued anyway when finish_decltype_type,
11333 called via cp_parser_decl_specifier_seq, returns a class or
11334 an enumeration (c++/51786). */
11335 decl_specifiers.type = NULL_TREE;
11336 shadow_tag (&decl_specifiers);
11338 /* Perform any deferred access checks. */
11339 perform_deferred_access_checks (tf_warning_or_error);
11342 /* Consume the `;'. */
11343 if (!maybe_range_for_decl)
11344 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11346 done:
11347 pop_deferring_access_checks ();
11350 /* Parse a decl-specifier-seq.
11352 decl-specifier-seq:
11353 decl-specifier-seq [opt] decl-specifier
11354 decl-specifier attribute-specifier-seq [opt] (C++11)
11356 decl-specifier:
11357 storage-class-specifier
11358 type-specifier
11359 function-specifier
11360 friend
11361 typedef
11363 GNU Extension:
11365 decl-specifier:
11366 attributes
11368 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11370 The parser flags FLAGS is used to control type-specifier parsing.
11372 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11373 flags:
11375 1: one of the decl-specifiers is an elaborated-type-specifier
11376 (i.e., a type declaration)
11377 2: one of the decl-specifiers is an enum-specifier or a
11378 class-specifier (i.e., a type definition)
11382 static void
11383 cp_parser_decl_specifier_seq (cp_parser* parser,
11384 cp_parser_flags flags,
11385 cp_decl_specifier_seq *decl_specs,
11386 int* declares_class_or_enum)
11388 bool constructor_possible_p = !parser->in_declarator_p;
11389 bool found_decl_spec = false;
11390 cp_token *start_token = NULL;
11391 cp_decl_spec ds;
11393 /* Clear DECL_SPECS. */
11394 clear_decl_specs (decl_specs);
11396 /* Assume no class or enumeration type is declared. */
11397 *declares_class_or_enum = 0;
11399 /* Keep reading specifiers until there are no more to read. */
11400 while (true)
11402 bool constructor_p;
11403 cp_token *token;
11404 ds = ds_last;
11406 /* Peek at the next token. */
11407 token = cp_lexer_peek_token (parser->lexer);
11409 /* Save the first token of the decl spec list for error
11410 reporting. */
11411 if (!start_token)
11412 start_token = token;
11413 /* Handle attributes. */
11414 if (cp_next_tokens_can_be_attribute_p (parser))
11416 /* Parse the attributes. */
11417 tree attrs = cp_parser_attributes_opt (parser);
11419 /* In a sequence of declaration specifiers, c++11 attributes
11420 appertain to the type that precede them. In that case
11421 [dcl.spec]/1 says:
11423 The attribute-specifier-seq affects the type only for
11424 the declaration it appears in, not other declarations
11425 involving the same type.
11427 But for now let's force the user to position the
11428 attribute either at the beginning of the declaration or
11429 after the declarator-id, which would clearly mean that it
11430 applies to the declarator. */
11431 if (cxx11_attribute_p (attrs))
11433 if (!found_decl_spec)
11434 /* The c++11 attribute is at the beginning of the
11435 declaration. It appertains to the entity being
11436 declared. */;
11437 else
11439 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11441 /* This is an attribute following a
11442 class-specifier. */
11443 if (decl_specs->type_definition_p)
11444 warn_misplaced_attr_for_class_type (token->location,
11445 decl_specs->type);
11446 attrs = NULL_TREE;
11448 else
11450 decl_specs->std_attributes
11451 = chainon (decl_specs->std_attributes,
11452 attrs);
11453 if (decl_specs->locations[ds_std_attribute] == 0)
11454 decl_specs->locations[ds_std_attribute] = token->location;
11456 continue;
11460 decl_specs->attributes
11461 = chainon (decl_specs->attributes,
11462 attrs);
11463 if (decl_specs->locations[ds_attribute] == 0)
11464 decl_specs->locations[ds_attribute] = token->location;
11465 continue;
11467 /* Assume we will find a decl-specifier keyword. */
11468 found_decl_spec = true;
11469 /* If the next token is an appropriate keyword, we can simply
11470 add it to the list. */
11471 switch (token->keyword)
11473 /* decl-specifier:
11474 friend
11475 constexpr */
11476 case RID_FRIEND:
11477 if (!at_class_scope_p ())
11479 error_at (token->location, "%<friend%> used outside of class");
11480 cp_lexer_purge_token (parser->lexer);
11482 else
11484 ds = ds_friend;
11485 /* Consume the token. */
11486 cp_lexer_consume_token (parser->lexer);
11488 break;
11490 case RID_CONSTEXPR:
11491 ds = ds_constexpr;
11492 cp_lexer_consume_token (parser->lexer);
11493 break;
11495 /* function-specifier:
11496 inline
11497 virtual
11498 explicit */
11499 case RID_INLINE:
11500 case RID_VIRTUAL:
11501 case RID_EXPLICIT:
11502 cp_parser_function_specifier_opt (parser, decl_specs);
11503 break;
11505 /* decl-specifier:
11506 typedef */
11507 case RID_TYPEDEF:
11508 ds = ds_typedef;
11509 /* Consume the token. */
11510 cp_lexer_consume_token (parser->lexer);
11511 /* A constructor declarator cannot appear in a typedef. */
11512 constructor_possible_p = false;
11513 /* The "typedef" keyword can only occur in a declaration; we
11514 may as well commit at this point. */
11515 cp_parser_commit_to_tentative_parse (parser);
11517 if (decl_specs->storage_class != sc_none)
11518 decl_specs->conflicting_specifiers_p = true;
11519 break;
11521 /* storage-class-specifier:
11522 auto
11523 register
11524 static
11525 extern
11526 mutable
11528 GNU Extension:
11529 thread */
11530 case RID_AUTO:
11531 if (cxx_dialect == cxx98)
11533 /* Consume the token. */
11534 cp_lexer_consume_token (parser->lexer);
11536 /* Complain about `auto' as a storage specifier, if
11537 we're complaining about C++0x compatibility. */
11538 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11539 " changes meaning in C++11; please remove it");
11541 /* Set the storage class anyway. */
11542 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11543 token);
11545 else
11546 /* C++0x auto type-specifier. */
11547 found_decl_spec = false;
11548 break;
11550 case RID_REGISTER:
11551 case RID_STATIC:
11552 case RID_EXTERN:
11553 case RID_MUTABLE:
11554 /* Consume the token. */
11555 cp_lexer_consume_token (parser->lexer);
11556 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11557 token);
11558 break;
11559 case RID_THREAD:
11560 /* Consume the token. */
11561 ds = ds_thread;
11562 cp_lexer_consume_token (parser->lexer);
11563 break;
11565 default:
11566 /* We did not yet find a decl-specifier yet. */
11567 found_decl_spec = false;
11568 break;
11571 if (found_decl_spec
11572 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11573 && token->keyword != RID_CONSTEXPR)
11574 error ("decl-specifier invalid in condition");
11576 if (ds != ds_last)
11577 set_and_check_decl_spec_loc (decl_specs, ds, token);
11579 /* Constructors are a special case. The `S' in `S()' is not a
11580 decl-specifier; it is the beginning of the declarator. */
11581 constructor_p
11582 = (!found_decl_spec
11583 && constructor_possible_p
11584 && (cp_parser_constructor_declarator_p
11585 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11587 /* If we don't have a DECL_SPEC yet, then we must be looking at
11588 a type-specifier. */
11589 if (!found_decl_spec && !constructor_p)
11591 int decl_spec_declares_class_or_enum;
11592 bool is_cv_qualifier;
11593 tree type_spec;
11595 type_spec
11596 = cp_parser_type_specifier (parser, flags,
11597 decl_specs,
11598 /*is_declaration=*/true,
11599 &decl_spec_declares_class_or_enum,
11600 &is_cv_qualifier);
11601 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11603 /* If this type-specifier referenced a user-defined type
11604 (a typedef, class-name, etc.), then we can't allow any
11605 more such type-specifiers henceforth.
11607 [dcl.spec]
11609 The longest sequence of decl-specifiers that could
11610 possibly be a type name is taken as the
11611 decl-specifier-seq of a declaration. The sequence shall
11612 be self-consistent as described below.
11614 [dcl.type]
11616 As a general rule, at most one type-specifier is allowed
11617 in the complete decl-specifier-seq of a declaration. The
11618 only exceptions are the following:
11620 -- const or volatile can be combined with any other
11621 type-specifier.
11623 -- signed or unsigned can be combined with char, long,
11624 short, or int.
11626 -- ..
11628 Example:
11630 typedef char* Pc;
11631 void g (const int Pc);
11633 Here, Pc is *not* part of the decl-specifier seq; it's
11634 the declarator. Therefore, once we see a type-specifier
11635 (other than a cv-qualifier), we forbid any additional
11636 user-defined types. We *do* still allow things like `int
11637 int' to be considered a decl-specifier-seq, and issue the
11638 error message later. */
11639 if (type_spec && !is_cv_qualifier)
11640 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11641 /* A constructor declarator cannot follow a type-specifier. */
11642 if (type_spec)
11644 constructor_possible_p = false;
11645 found_decl_spec = true;
11646 if (!is_cv_qualifier)
11647 decl_specs->any_type_specifiers_p = true;
11651 /* If we still do not have a DECL_SPEC, then there are no more
11652 decl-specifiers. */
11653 if (!found_decl_spec)
11654 break;
11656 decl_specs->any_specifiers_p = true;
11657 /* After we see one decl-specifier, further decl-specifiers are
11658 always optional. */
11659 flags |= CP_PARSER_FLAGS_OPTIONAL;
11662 /* Don't allow a friend specifier with a class definition. */
11663 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11664 && (*declares_class_or_enum & 2))
11665 error_at (decl_specs->locations[ds_friend],
11666 "class definition may not be declared a friend");
11669 /* Parse an (optional) storage-class-specifier.
11671 storage-class-specifier:
11672 auto
11673 register
11674 static
11675 extern
11676 mutable
11678 GNU Extension:
11680 storage-class-specifier:
11681 thread
11683 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11685 static tree
11686 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11688 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11690 case RID_AUTO:
11691 if (cxx_dialect != cxx98)
11692 return NULL_TREE;
11693 /* Fall through for C++98. */
11695 case RID_REGISTER:
11696 case RID_STATIC:
11697 case RID_EXTERN:
11698 case RID_MUTABLE:
11699 case RID_THREAD:
11700 /* Consume the token. */
11701 return cp_lexer_consume_token (parser->lexer)->u.value;
11703 default:
11704 return NULL_TREE;
11708 /* Parse an (optional) function-specifier.
11710 function-specifier:
11711 inline
11712 virtual
11713 explicit
11715 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11716 Updates DECL_SPECS, if it is non-NULL. */
11718 static tree
11719 cp_parser_function_specifier_opt (cp_parser* parser,
11720 cp_decl_specifier_seq *decl_specs)
11722 cp_token *token = cp_lexer_peek_token (parser->lexer);
11723 switch (token->keyword)
11725 case RID_INLINE:
11726 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11727 break;
11729 case RID_VIRTUAL:
11730 /* 14.5.2.3 [temp.mem]
11732 A member function template shall not be virtual. */
11733 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11734 error_at (token->location, "templates may not be %<virtual%>");
11735 else
11736 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11737 break;
11739 case RID_EXPLICIT:
11740 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11741 break;
11743 default:
11744 return NULL_TREE;
11747 /* Consume the token. */
11748 return cp_lexer_consume_token (parser->lexer)->u.value;
11751 /* Parse a linkage-specification.
11753 linkage-specification:
11754 extern string-literal { declaration-seq [opt] }
11755 extern string-literal declaration */
11757 static void
11758 cp_parser_linkage_specification (cp_parser* parser)
11760 tree linkage;
11762 /* Look for the `extern' keyword. */
11763 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11765 /* Look for the string-literal. */
11766 linkage = cp_parser_string_literal (parser, false, false);
11768 /* Transform the literal into an identifier. If the literal is a
11769 wide-character string, or contains embedded NULs, then we can't
11770 handle it as the user wants. */
11771 if (strlen (TREE_STRING_POINTER (linkage))
11772 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11774 cp_parser_error (parser, "invalid linkage-specification");
11775 /* Assume C++ linkage. */
11776 linkage = lang_name_cplusplus;
11778 else
11779 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11781 /* We're now using the new linkage. */
11782 push_lang_context (linkage);
11784 /* If the next token is a `{', then we're using the first
11785 production. */
11786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11788 cp_ensure_no_omp_declare_simd (parser);
11790 /* Consume the `{' token. */
11791 cp_lexer_consume_token (parser->lexer);
11792 /* Parse the declarations. */
11793 cp_parser_declaration_seq_opt (parser);
11794 /* Look for the closing `}'. */
11795 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11797 /* Otherwise, there's just one declaration. */
11798 else
11800 bool saved_in_unbraced_linkage_specification_p;
11802 saved_in_unbraced_linkage_specification_p
11803 = parser->in_unbraced_linkage_specification_p;
11804 parser->in_unbraced_linkage_specification_p = true;
11805 cp_parser_declaration (parser);
11806 parser->in_unbraced_linkage_specification_p
11807 = saved_in_unbraced_linkage_specification_p;
11810 /* We're done with the linkage-specification. */
11811 pop_lang_context ();
11814 /* Parse a static_assert-declaration.
11816 static_assert-declaration:
11817 static_assert ( constant-expression , string-literal ) ;
11819 If MEMBER_P, this static_assert is a class member. */
11821 static void
11822 cp_parser_static_assert(cp_parser *parser, bool member_p)
11824 tree condition;
11825 tree message;
11826 cp_token *token;
11827 location_t saved_loc;
11828 bool dummy;
11830 /* Peek at the `static_assert' token so we can keep track of exactly
11831 where the static assertion started. */
11832 token = cp_lexer_peek_token (parser->lexer);
11833 saved_loc = token->location;
11835 /* Look for the `static_assert' keyword. */
11836 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11837 RT_STATIC_ASSERT))
11838 return;
11840 /* We know we are in a static assertion; commit to any tentative
11841 parse. */
11842 if (cp_parser_parsing_tentatively (parser))
11843 cp_parser_commit_to_tentative_parse (parser);
11845 /* Parse the `(' starting the static assertion condition. */
11846 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11848 /* Parse the constant-expression. Allow a non-constant expression
11849 here in order to give better diagnostics in finish_static_assert. */
11850 condition =
11851 cp_parser_constant_expression (parser,
11852 /*allow_non_constant_p=*/true,
11853 /*non_constant_p=*/&dummy);
11855 /* Parse the separating `,'. */
11856 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11858 /* Parse the string-literal message. */
11859 message = cp_parser_string_literal (parser,
11860 /*translate=*/false,
11861 /*wide_ok=*/true);
11863 /* A `)' completes the static assertion. */
11864 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11865 cp_parser_skip_to_closing_parenthesis (parser,
11866 /*recovering=*/true,
11867 /*or_comma=*/false,
11868 /*consume_paren=*/true);
11870 /* A semicolon terminates the declaration. */
11871 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11873 /* Complete the static assertion, which may mean either processing
11874 the static assert now or saving it for template instantiation. */
11875 finish_static_assert (condition, message, saved_loc, member_p);
11878 /* Parse the expression in decltype ( expression ). */
11880 static tree
11881 cp_parser_decltype_expr (cp_parser *parser,
11882 bool &id_expression_or_member_access_p)
11884 cp_token *id_expr_start_token;
11885 tree expr;
11887 /* First, try parsing an id-expression. */
11888 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11889 cp_parser_parse_tentatively (parser);
11890 expr = cp_parser_id_expression (parser,
11891 /*template_keyword_p=*/false,
11892 /*check_dependency_p=*/true,
11893 /*template_p=*/NULL,
11894 /*declarator_p=*/false,
11895 /*optional_p=*/false);
11897 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11899 bool non_integral_constant_expression_p = false;
11900 tree id_expression = expr;
11901 cp_id_kind idk;
11902 const char *error_msg;
11904 if (identifier_p (expr))
11905 /* Lookup the name we got back from the id-expression. */
11906 expr = cp_parser_lookup_name_simple (parser, expr,
11907 id_expr_start_token->location);
11909 if (expr
11910 && expr != error_mark_node
11911 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11912 && TREE_CODE (expr) != TYPE_DECL
11913 && (TREE_CODE (expr) != BIT_NOT_EXPR
11914 || !TYPE_P (TREE_OPERAND (expr, 0)))
11915 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11917 /* Complete lookup of the id-expression. */
11918 expr = (finish_id_expression
11919 (id_expression, expr, parser->scope, &idk,
11920 /*integral_constant_expression_p=*/false,
11921 /*allow_non_integral_constant_expression_p=*/true,
11922 &non_integral_constant_expression_p,
11923 /*template_p=*/false,
11924 /*done=*/true,
11925 /*address_p=*/false,
11926 /*template_arg_p=*/false,
11927 &error_msg,
11928 id_expr_start_token->location));
11930 if (expr == error_mark_node)
11931 /* We found an id-expression, but it was something that we
11932 should not have found. This is an error, not something
11933 we can recover from, so note that we found an
11934 id-expression and we'll recover as gracefully as
11935 possible. */
11936 id_expression_or_member_access_p = true;
11939 if (expr
11940 && expr != error_mark_node
11941 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11942 /* We have an id-expression. */
11943 id_expression_or_member_access_p = true;
11946 if (!id_expression_or_member_access_p)
11948 /* Abort the id-expression parse. */
11949 cp_parser_abort_tentative_parse (parser);
11951 /* Parsing tentatively, again. */
11952 cp_parser_parse_tentatively (parser);
11954 /* Parse a class member access. */
11955 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11956 /*cast_p=*/false, /*decltype*/true,
11957 /*member_access_only_p=*/true, NULL);
11959 if (expr
11960 && expr != error_mark_node
11961 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11962 /* We have an id-expression. */
11963 id_expression_or_member_access_p = true;
11966 if (id_expression_or_member_access_p)
11967 /* We have parsed the complete id-expression or member access. */
11968 cp_parser_parse_definitely (parser);
11969 else
11971 /* Abort our attempt to parse an id-expression or member access
11972 expression. */
11973 cp_parser_abort_tentative_parse (parser);
11975 /* Parse a full expression. */
11976 expr = cp_parser_expression (parser, /*cast_p=*/false,
11977 /*decltype*/true, NULL);
11980 return expr;
11983 /* Parse a `decltype' type. Returns the type.
11985 simple-type-specifier:
11986 decltype ( expression )
11987 C++14 proposal:
11988 decltype ( auto ) */
11990 static tree
11991 cp_parser_decltype (cp_parser *parser)
11993 tree expr;
11994 bool id_expression_or_member_access_p = false;
11995 const char *saved_message;
11996 bool saved_integral_constant_expression_p;
11997 bool saved_non_integral_constant_expression_p;
11998 bool saved_greater_than_is_operator_p;
11999 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12001 if (start_token->type == CPP_DECLTYPE)
12003 /* Already parsed. */
12004 cp_lexer_consume_token (parser->lexer);
12005 return start_token->u.value;
12008 /* Look for the `decltype' token. */
12009 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12010 return error_mark_node;
12012 /* Parse the opening `('. */
12013 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12014 return error_mark_node;
12016 /* decltype (auto) */
12017 if (cxx_dialect >= cxx1y
12018 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12020 cp_lexer_consume_token (parser->lexer);
12021 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12022 return error_mark_node;
12023 expr = make_decltype_auto ();
12024 AUTO_IS_DECLTYPE (expr) = true;
12025 goto rewrite;
12028 /* Types cannot be defined in a `decltype' expression. Save away the
12029 old message. */
12030 saved_message = parser->type_definition_forbidden_message;
12032 /* And create the new one. */
12033 parser->type_definition_forbidden_message
12034 = G_("types may not be defined in %<decltype%> expressions");
12036 /* The restrictions on constant-expressions do not apply inside
12037 decltype expressions. */
12038 saved_integral_constant_expression_p
12039 = parser->integral_constant_expression_p;
12040 saved_non_integral_constant_expression_p
12041 = parser->non_integral_constant_expression_p;
12042 parser->integral_constant_expression_p = false;
12044 /* Within a parenthesized expression, a `>' token is always
12045 the greater-than operator. */
12046 saved_greater_than_is_operator_p
12047 = parser->greater_than_is_operator_p;
12048 parser->greater_than_is_operator_p = true;
12050 /* Do not actually evaluate the expression. */
12051 ++cp_unevaluated_operand;
12053 /* Do not warn about problems with the expression. */
12054 ++c_inhibit_evaluation_warnings;
12056 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12058 /* Go back to evaluating expressions. */
12059 --cp_unevaluated_operand;
12060 --c_inhibit_evaluation_warnings;
12062 /* The `>' token might be the end of a template-id or
12063 template-parameter-list now. */
12064 parser->greater_than_is_operator_p
12065 = saved_greater_than_is_operator_p;
12067 /* Restore the old message and the integral constant expression
12068 flags. */
12069 parser->type_definition_forbidden_message = saved_message;
12070 parser->integral_constant_expression_p
12071 = saved_integral_constant_expression_p;
12072 parser->non_integral_constant_expression_p
12073 = saved_non_integral_constant_expression_p;
12075 /* Parse to the closing `)'. */
12076 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12078 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12079 /*consume_paren=*/true);
12080 return error_mark_node;
12083 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12084 tf_warning_or_error);
12086 rewrite:
12087 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12088 it again. */
12089 start_token->type = CPP_DECLTYPE;
12090 start_token->u.value = expr;
12091 start_token->keyword = RID_MAX;
12092 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12094 return expr;
12097 /* Special member functions [gram.special] */
12099 /* Parse a conversion-function-id.
12101 conversion-function-id:
12102 operator conversion-type-id
12104 Returns an IDENTIFIER_NODE representing the operator. */
12106 static tree
12107 cp_parser_conversion_function_id (cp_parser* parser)
12109 tree type;
12110 tree saved_scope;
12111 tree saved_qualifying_scope;
12112 tree saved_object_scope;
12113 tree pushed_scope = NULL_TREE;
12115 /* Look for the `operator' token. */
12116 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12117 return error_mark_node;
12118 /* When we parse the conversion-type-id, the current scope will be
12119 reset. However, we need that information in able to look up the
12120 conversion function later, so we save it here. */
12121 saved_scope = parser->scope;
12122 saved_qualifying_scope = parser->qualifying_scope;
12123 saved_object_scope = parser->object_scope;
12124 /* We must enter the scope of the class so that the names of
12125 entities declared within the class are available in the
12126 conversion-type-id. For example, consider:
12128 struct S {
12129 typedef int I;
12130 operator I();
12133 S::operator I() { ... }
12135 In order to see that `I' is a type-name in the definition, we
12136 must be in the scope of `S'. */
12137 if (saved_scope)
12138 pushed_scope = push_scope (saved_scope);
12139 /* Parse the conversion-type-id. */
12140 type = cp_parser_conversion_type_id (parser);
12141 /* Leave the scope of the class, if any. */
12142 if (pushed_scope)
12143 pop_scope (pushed_scope);
12144 /* Restore the saved scope. */
12145 parser->scope = saved_scope;
12146 parser->qualifying_scope = saved_qualifying_scope;
12147 parser->object_scope = saved_object_scope;
12148 /* If the TYPE is invalid, indicate failure. */
12149 if (type == error_mark_node)
12150 return error_mark_node;
12151 return mangle_conv_op_name_for_type (type);
12154 /* Parse a conversion-type-id:
12156 conversion-type-id:
12157 type-specifier-seq conversion-declarator [opt]
12159 Returns the TYPE specified. */
12161 static tree
12162 cp_parser_conversion_type_id (cp_parser* parser)
12164 tree attributes;
12165 cp_decl_specifier_seq type_specifiers;
12166 cp_declarator *declarator;
12167 tree type_specified;
12168 const char *saved_message;
12170 /* Parse the attributes. */
12171 attributes = cp_parser_attributes_opt (parser);
12173 saved_message = parser->type_definition_forbidden_message;
12174 parser->type_definition_forbidden_message
12175 = G_("types may not be defined in a conversion-type-id");
12177 /* Parse the type-specifiers. */
12178 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12179 /*is_trailing_return=*/false,
12180 &type_specifiers);
12182 parser->type_definition_forbidden_message = saved_message;
12184 /* If that didn't work, stop. */
12185 if (type_specifiers.type == error_mark_node)
12186 return error_mark_node;
12187 /* Parse the conversion-declarator. */
12188 declarator = cp_parser_conversion_declarator_opt (parser);
12190 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12191 /*initialized=*/0, &attributes);
12192 if (attributes)
12193 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12195 /* Don't give this error when parsing tentatively. This happens to
12196 work because we always parse this definitively once. */
12197 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12198 && type_uses_auto (type_specified))
12200 if (cxx_dialect < cxx1y)
12202 error ("invalid use of %<auto%> in conversion operator");
12203 return error_mark_node;
12205 else if (template_parm_scope_p ())
12206 warning (0, "use of %<auto%> in member template "
12207 "conversion operator can never be deduced");
12210 return type_specified;
12213 /* Parse an (optional) conversion-declarator.
12215 conversion-declarator:
12216 ptr-operator conversion-declarator [opt]
12220 static cp_declarator *
12221 cp_parser_conversion_declarator_opt (cp_parser* parser)
12223 enum tree_code code;
12224 tree class_type, std_attributes = NULL_TREE;
12225 cp_cv_quals cv_quals;
12227 /* We don't know if there's a ptr-operator next, or not. */
12228 cp_parser_parse_tentatively (parser);
12229 /* Try the ptr-operator. */
12230 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12231 &std_attributes);
12232 /* If it worked, look for more conversion-declarators. */
12233 if (cp_parser_parse_definitely (parser))
12235 cp_declarator *declarator;
12237 /* Parse another optional declarator. */
12238 declarator = cp_parser_conversion_declarator_opt (parser);
12240 declarator = cp_parser_make_indirect_declarator
12241 (code, class_type, cv_quals, declarator, std_attributes);
12243 return declarator;
12246 return NULL;
12249 /* Parse an (optional) ctor-initializer.
12251 ctor-initializer:
12252 : mem-initializer-list
12254 Returns TRUE iff the ctor-initializer was actually present. */
12256 static bool
12257 cp_parser_ctor_initializer_opt (cp_parser* parser)
12259 /* If the next token is not a `:', then there is no
12260 ctor-initializer. */
12261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12263 /* Do default initialization of any bases and members. */
12264 if (DECL_CONSTRUCTOR_P (current_function_decl))
12265 finish_mem_initializers (NULL_TREE);
12267 return false;
12270 /* Consume the `:' token. */
12271 cp_lexer_consume_token (parser->lexer);
12272 /* And the mem-initializer-list. */
12273 cp_parser_mem_initializer_list (parser);
12275 return true;
12278 /* Parse a mem-initializer-list.
12280 mem-initializer-list:
12281 mem-initializer ... [opt]
12282 mem-initializer ... [opt] , mem-initializer-list */
12284 static void
12285 cp_parser_mem_initializer_list (cp_parser* parser)
12287 tree mem_initializer_list = NULL_TREE;
12288 tree target_ctor = error_mark_node;
12289 cp_token *token = cp_lexer_peek_token (parser->lexer);
12291 /* Let the semantic analysis code know that we are starting the
12292 mem-initializer-list. */
12293 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12294 error_at (token->location,
12295 "only constructors take member initializers");
12297 /* Loop through the list. */
12298 while (true)
12300 tree mem_initializer;
12302 token = cp_lexer_peek_token (parser->lexer);
12303 /* Parse the mem-initializer. */
12304 mem_initializer = cp_parser_mem_initializer (parser);
12305 /* If the next token is a `...', we're expanding member initializers. */
12306 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12308 /* Consume the `...'. */
12309 cp_lexer_consume_token (parser->lexer);
12311 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12312 can be expanded but members cannot. */
12313 if (mem_initializer != error_mark_node
12314 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12316 error_at (token->location,
12317 "cannot expand initializer for member %<%D%>",
12318 TREE_PURPOSE (mem_initializer));
12319 mem_initializer = error_mark_node;
12322 /* Construct the pack expansion type. */
12323 if (mem_initializer != error_mark_node)
12324 mem_initializer = make_pack_expansion (mem_initializer);
12326 if (target_ctor != error_mark_node
12327 && mem_initializer != error_mark_node)
12329 error ("mem-initializer for %qD follows constructor delegation",
12330 TREE_PURPOSE (mem_initializer));
12331 mem_initializer = error_mark_node;
12333 /* Look for a target constructor. */
12334 if (mem_initializer != error_mark_node
12335 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12336 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12338 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12339 if (mem_initializer_list)
12341 error ("constructor delegation follows mem-initializer for %qD",
12342 TREE_PURPOSE (mem_initializer_list));
12343 mem_initializer = error_mark_node;
12345 target_ctor = mem_initializer;
12347 /* Add it to the list, unless it was erroneous. */
12348 if (mem_initializer != error_mark_node)
12350 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12351 mem_initializer_list = mem_initializer;
12353 /* If the next token is not a `,', we're done. */
12354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12355 break;
12356 /* Consume the `,' token. */
12357 cp_lexer_consume_token (parser->lexer);
12360 /* Perform semantic analysis. */
12361 if (DECL_CONSTRUCTOR_P (current_function_decl))
12362 finish_mem_initializers (mem_initializer_list);
12365 /* Parse a mem-initializer.
12367 mem-initializer:
12368 mem-initializer-id ( expression-list [opt] )
12369 mem-initializer-id braced-init-list
12371 GNU extension:
12373 mem-initializer:
12374 ( expression-list [opt] )
12376 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12377 class) or FIELD_DECL (for a non-static data member) to initialize;
12378 the TREE_VALUE is the expression-list. An empty initialization
12379 list is represented by void_list_node. */
12381 static tree
12382 cp_parser_mem_initializer (cp_parser* parser)
12384 tree mem_initializer_id;
12385 tree expression_list;
12386 tree member;
12387 cp_token *token = cp_lexer_peek_token (parser->lexer);
12389 /* Find out what is being initialized. */
12390 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12392 permerror (token->location,
12393 "anachronistic old-style base class initializer");
12394 mem_initializer_id = NULL_TREE;
12396 else
12398 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12399 if (mem_initializer_id == error_mark_node)
12400 return mem_initializer_id;
12402 member = expand_member_init (mem_initializer_id);
12403 if (member && !DECL_P (member))
12404 in_base_initializer = 1;
12406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12408 bool expr_non_constant_p;
12409 cp_lexer_set_source_position (parser->lexer);
12410 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12411 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12412 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12413 expression_list = build_tree_list (NULL_TREE, expression_list);
12415 else
12417 vec<tree, va_gc> *vec;
12418 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12419 /*cast_p=*/false,
12420 /*allow_expansion_p=*/true,
12421 /*non_constant_p=*/NULL);
12422 if (vec == NULL)
12423 return error_mark_node;
12424 expression_list = build_tree_list_vec (vec);
12425 release_tree_vector (vec);
12428 if (expression_list == error_mark_node)
12429 return error_mark_node;
12430 if (!expression_list)
12431 expression_list = void_type_node;
12433 in_base_initializer = 0;
12435 return member ? build_tree_list (member, expression_list) : error_mark_node;
12438 /* Parse a mem-initializer-id.
12440 mem-initializer-id:
12441 :: [opt] nested-name-specifier [opt] class-name
12442 identifier
12444 Returns a TYPE indicating the class to be initializer for the first
12445 production. Returns an IDENTIFIER_NODE indicating the data member
12446 to be initialized for the second production. */
12448 static tree
12449 cp_parser_mem_initializer_id (cp_parser* parser)
12451 bool global_scope_p;
12452 bool nested_name_specifier_p;
12453 bool template_p = false;
12454 tree id;
12456 cp_token *token = cp_lexer_peek_token (parser->lexer);
12458 /* `typename' is not allowed in this context ([temp.res]). */
12459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12461 error_at (token->location,
12462 "keyword %<typename%> not allowed in this context (a qualified "
12463 "member initializer is implicitly a type)");
12464 cp_lexer_consume_token (parser->lexer);
12466 /* Look for the optional `::' operator. */
12467 global_scope_p
12468 = (cp_parser_global_scope_opt (parser,
12469 /*current_scope_valid_p=*/false)
12470 != NULL_TREE);
12471 /* Look for the optional nested-name-specifier. The simplest way to
12472 implement:
12474 [temp.res]
12476 The keyword `typename' is not permitted in a base-specifier or
12477 mem-initializer; in these contexts a qualified name that
12478 depends on a template-parameter is implicitly assumed to be a
12479 type name.
12481 is to assume that we have seen the `typename' keyword at this
12482 point. */
12483 nested_name_specifier_p
12484 = (cp_parser_nested_name_specifier_opt (parser,
12485 /*typename_keyword_p=*/true,
12486 /*check_dependency_p=*/true,
12487 /*type_p=*/true,
12488 /*is_declaration=*/true)
12489 != NULL_TREE);
12490 if (nested_name_specifier_p)
12491 template_p = cp_parser_optional_template_keyword (parser);
12492 /* If there is a `::' operator or a nested-name-specifier, then we
12493 are definitely looking for a class-name. */
12494 if (global_scope_p || nested_name_specifier_p)
12495 return cp_parser_class_name (parser,
12496 /*typename_keyword_p=*/true,
12497 /*template_keyword_p=*/template_p,
12498 typename_type,
12499 /*check_dependency_p=*/true,
12500 /*class_head_p=*/false,
12501 /*is_declaration=*/true);
12502 /* Otherwise, we could also be looking for an ordinary identifier. */
12503 cp_parser_parse_tentatively (parser);
12504 /* Try a class-name. */
12505 id = cp_parser_class_name (parser,
12506 /*typename_keyword_p=*/true,
12507 /*template_keyword_p=*/false,
12508 none_type,
12509 /*check_dependency_p=*/true,
12510 /*class_head_p=*/false,
12511 /*is_declaration=*/true);
12512 /* If we found one, we're done. */
12513 if (cp_parser_parse_definitely (parser))
12514 return id;
12515 /* Otherwise, look for an ordinary identifier. */
12516 return cp_parser_identifier (parser);
12519 /* Overloading [gram.over] */
12521 /* Parse an operator-function-id.
12523 operator-function-id:
12524 operator operator
12526 Returns an IDENTIFIER_NODE for the operator which is a
12527 human-readable spelling of the identifier, e.g., `operator +'. */
12529 static tree
12530 cp_parser_operator_function_id (cp_parser* parser)
12532 /* Look for the `operator' keyword. */
12533 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12534 return error_mark_node;
12535 /* And then the name of the operator itself. */
12536 return cp_parser_operator (parser);
12539 /* Return an identifier node for a user-defined literal operator.
12540 The suffix identifier is chained to the operator name identifier. */
12542 static tree
12543 cp_literal_operator_id (const char* name)
12545 tree identifier;
12546 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12547 + strlen (name) + 10);
12548 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12549 identifier = get_identifier (buffer);
12551 return identifier;
12554 /* Parse an operator.
12556 operator:
12557 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12558 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12559 || ++ -- , ->* -> () []
12561 GNU Extensions:
12563 operator:
12564 <? >? <?= >?=
12566 Returns an IDENTIFIER_NODE for the operator which is a
12567 human-readable spelling of the identifier, e.g., `operator +'. */
12569 static tree
12570 cp_parser_operator (cp_parser* parser)
12572 tree id = NULL_TREE;
12573 cp_token *token;
12574 bool bad_encoding_prefix = false;
12576 /* Peek at the next token. */
12577 token = cp_lexer_peek_token (parser->lexer);
12578 /* Figure out which operator we have. */
12579 switch (token->type)
12581 case CPP_KEYWORD:
12583 enum tree_code op;
12585 /* The keyword should be either `new' or `delete'. */
12586 if (token->keyword == RID_NEW)
12587 op = NEW_EXPR;
12588 else if (token->keyword == RID_DELETE)
12589 op = DELETE_EXPR;
12590 else
12591 break;
12593 /* Consume the `new' or `delete' token. */
12594 cp_lexer_consume_token (parser->lexer);
12596 /* Peek at the next token. */
12597 token = cp_lexer_peek_token (parser->lexer);
12598 /* If it's a `[' token then this is the array variant of the
12599 operator. */
12600 if (token->type == CPP_OPEN_SQUARE)
12602 /* Consume the `[' token. */
12603 cp_lexer_consume_token (parser->lexer);
12604 /* Look for the `]' token. */
12605 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12606 id = ansi_opname (op == NEW_EXPR
12607 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12609 /* Otherwise, we have the non-array variant. */
12610 else
12611 id = ansi_opname (op);
12613 return id;
12616 case CPP_PLUS:
12617 id = ansi_opname (PLUS_EXPR);
12618 break;
12620 case CPP_MINUS:
12621 id = ansi_opname (MINUS_EXPR);
12622 break;
12624 case CPP_MULT:
12625 id = ansi_opname (MULT_EXPR);
12626 break;
12628 case CPP_DIV:
12629 id = ansi_opname (TRUNC_DIV_EXPR);
12630 break;
12632 case CPP_MOD:
12633 id = ansi_opname (TRUNC_MOD_EXPR);
12634 break;
12636 case CPP_XOR:
12637 id = ansi_opname (BIT_XOR_EXPR);
12638 break;
12640 case CPP_AND:
12641 id = ansi_opname (BIT_AND_EXPR);
12642 break;
12644 case CPP_OR:
12645 id = ansi_opname (BIT_IOR_EXPR);
12646 break;
12648 case CPP_COMPL:
12649 id = ansi_opname (BIT_NOT_EXPR);
12650 break;
12652 case CPP_NOT:
12653 id = ansi_opname (TRUTH_NOT_EXPR);
12654 break;
12656 case CPP_EQ:
12657 id = ansi_assopname (NOP_EXPR);
12658 break;
12660 case CPP_LESS:
12661 id = ansi_opname (LT_EXPR);
12662 break;
12664 case CPP_GREATER:
12665 id = ansi_opname (GT_EXPR);
12666 break;
12668 case CPP_PLUS_EQ:
12669 id = ansi_assopname (PLUS_EXPR);
12670 break;
12672 case CPP_MINUS_EQ:
12673 id = ansi_assopname (MINUS_EXPR);
12674 break;
12676 case CPP_MULT_EQ:
12677 id = ansi_assopname (MULT_EXPR);
12678 break;
12680 case CPP_DIV_EQ:
12681 id = ansi_assopname (TRUNC_DIV_EXPR);
12682 break;
12684 case CPP_MOD_EQ:
12685 id = ansi_assopname (TRUNC_MOD_EXPR);
12686 break;
12688 case CPP_XOR_EQ:
12689 id = ansi_assopname (BIT_XOR_EXPR);
12690 break;
12692 case CPP_AND_EQ:
12693 id = ansi_assopname (BIT_AND_EXPR);
12694 break;
12696 case CPP_OR_EQ:
12697 id = ansi_assopname (BIT_IOR_EXPR);
12698 break;
12700 case CPP_LSHIFT:
12701 id = ansi_opname (LSHIFT_EXPR);
12702 break;
12704 case CPP_RSHIFT:
12705 id = ansi_opname (RSHIFT_EXPR);
12706 break;
12708 case CPP_LSHIFT_EQ:
12709 id = ansi_assopname (LSHIFT_EXPR);
12710 break;
12712 case CPP_RSHIFT_EQ:
12713 id = ansi_assopname (RSHIFT_EXPR);
12714 break;
12716 case CPP_EQ_EQ:
12717 id = ansi_opname (EQ_EXPR);
12718 break;
12720 case CPP_NOT_EQ:
12721 id = ansi_opname (NE_EXPR);
12722 break;
12724 case CPP_LESS_EQ:
12725 id = ansi_opname (LE_EXPR);
12726 break;
12728 case CPP_GREATER_EQ:
12729 id = ansi_opname (GE_EXPR);
12730 break;
12732 case CPP_AND_AND:
12733 id = ansi_opname (TRUTH_ANDIF_EXPR);
12734 break;
12736 case CPP_OR_OR:
12737 id = ansi_opname (TRUTH_ORIF_EXPR);
12738 break;
12740 case CPP_PLUS_PLUS:
12741 id = ansi_opname (POSTINCREMENT_EXPR);
12742 break;
12744 case CPP_MINUS_MINUS:
12745 id = ansi_opname (PREDECREMENT_EXPR);
12746 break;
12748 case CPP_COMMA:
12749 id = ansi_opname (COMPOUND_EXPR);
12750 break;
12752 case CPP_DEREF_STAR:
12753 id = ansi_opname (MEMBER_REF);
12754 break;
12756 case CPP_DEREF:
12757 id = ansi_opname (COMPONENT_REF);
12758 break;
12760 case CPP_OPEN_PAREN:
12761 /* Consume the `('. */
12762 cp_lexer_consume_token (parser->lexer);
12763 /* Look for the matching `)'. */
12764 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12765 return ansi_opname (CALL_EXPR);
12767 case CPP_OPEN_SQUARE:
12768 /* Consume the `['. */
12769 cp_lexer_consume_token (parser->lexer);
12770 /* Look for the matching `]'. */
12771 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12772 return ansi_opname (ARRAY_REF);
12774 case CPP_WSTRING:
12775 case CPP_STRING16:
12776 case CPP_STRING32:
12777 case CPP_UTF8STRING:
12778 bad_encoding_prefix = true;
12779 /* Fall through. */
12781 case CPP_STRING:
12782 if (cxx_dialect == cxx98)
12783 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12784 if (bad_encoding_prefix)
12786 error ("invalid encoding prefix in literal operator");
12787 return error_mark_node;
12789 if (TREE_STRING_LENGTH (token->u.value) > 2)
12791 error ("expected empty string after %<operator%> keyword");
12792 return error_mark_node;
12794 /* Consume the string. */
12795 cp_lexer_consume_token (parser->lexer);
12796 /* Look for the suffix identifier. */
12797 token = cp_lexer_peek_token (parser->lexer);
12798 if (token->type == CPP_NAME)
12800 id = cp_parser_identifier (parser);
12801 if (id != error_mark_node)
12803 const char *name = IDENTIFIER_POINTER (id);
12804 return cp_literal_operator_id (name);
12807 else if (token->type == CPP_KEYWORD)
12809 error ("unexpected keyword;"
12810 " remove space between quotes and suffix identifier");
12811 return error_mark_node;
12813 else
12815 error ("expected suffix identifier");
12816 return error_mark_node;
12819 case CPP_WSTRING_USERDEF:
12820 case CPP_STRING16_USERDEF:
12821 case CPP_STRING32_USERDEF:
12822 case CPP_UTF8STRING_USERDEF:
12823 bad_encoding_prefix = true;
12824 /* Fall through. */
12826 case CPP_STRING_USERDEF:
12827 if (cxx_dialect == cxx98)
12828 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12829 if (bad_encoding_prefix)
12831 error ("invalid encoding prefix in literal operator");
12832 return error_mark_node;
12835 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12836 if (TREE_STRING_LENGTH (string_tree) > 2)
12838 error ("expected empty string after %<operator%> keyword");
12839 return error_mark_node;
12841 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12842 /* Consume the user-defined string literal. */
12843 cp_lexer_consume_token (parser->lexer);
12844 if (id != error_mark_node)
12846 const char *name = IDENTIFIER_POINTER (id);
12847 return cp_literal_operator_id (name);
12849 else
12850 return error_mark_node;
12853 default:
12854 /* Anything else is an error. */
12855 break;
12858 /* If we have selected an identifier, we need to consume the
12859 operator token. */
12860 if (id)
12861 cp_lexer_consume_token (parser->lexer);
12862 /* Otherwise, no valid operator name was present. */
12863 else
12865 cp_parser_error (parser, "expected operator");
12866 id = error_mark_node;
12869 return id;
12872 /* Parse a template-declaration.
12874 template-declaration:
12875 export [opt] template < template-parameter-list > declaration
12877 If MEMBER_P is TRUE, this template-declaration occurs within a
12878 class-specifier.
12880 The grammar rule given by the standard isn't correct. What
12881 is really meant is:
12883 template-declaration:
12884 export [opt] template-parameter-list-seq
12885 decl-specifier-seq [opt] init-declarator [opt] ;
12886 export [opt] template-parameter-list-seq
12887 function-definition
12889 template-parameter-list-seq:
12890 template-parameter-list-seq [opt]
12891 template < template-parameter-list > */
12893 static void
12894 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12896 /* Check for `export'. */
12897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12899 /* Consume the `export' token. */
12900 cp_lexer_consume_token (parser->lexer);
12901 /* Warn that we do not support `export'. */
12902 warning (0, "keyword %<export%> not implemented, and will be ignored");
12905 cp_parser_template_declaration_after_export (parser, member_p);
12908 /* Parse a template-parameter-list.
12910 template-parameter-list:
12911 template-parameter
12912 template-parameter-list , template-parameter
12914 Returns a TREE_LIST. Each node represents a template parameter.
12915 The nodes are connected via their TREE_CHAINs. */
12917 static tree
12918 cp_parser_template_parameter_list (cp_parser* parser)
12920 tree parameter_list = NULL_TREE;
12922 begin_template_parm_list ();
12924 /* The loop below parses the template parms. We first need to know
12925 the total number of template parms to be able to compute proper
12926 canonical types of each dependent type. So after the loop, when
12927 we know the total number of template parms,
12928 end_template_parm_list computes the proper canonical types and
12929 fixes up the dependent types accordingly. */
12930 while (true)
12932 tree parameter;
12933 bool is_non_type;
12934 bool is_parameter_pack;
12935 location_t parm_loc;
12937 /* Parse the template-parameter. */
12938 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12939 parameter = cp_parser_template_parameter (parser,
12940 &is_non_type,
12941 &is_parameter_pack);
12942 /* Add it to the list. */
12943 if (parameter != error_mark_node)
12944 parameter_list = process_template_parm (parameter_list,
12945 parm_loc,
12946 parameter,
12947 is_non_type,
12948 is_parameter_pack);
12949 else
12951 tree err_parm = build_tree_list (parameter, parameter);
12952 parameter_list = chainon (parameter_list, err_parm);
12955 /* If the next token is not a `,', we're done. */
12956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12957 break;
12958 /* Otherwise, consume the `,' token. */
12959 cp_lexer_consume_token (parser->lexer);
12962 return end_template_parm_list (parameter_list);
12965 /* Parse a template-parameter.
12967 template-parameter:
12968 type-parameter
12969 parameter-declaration
12971 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12972 the parameter. The TREE_PURPOSE is the default value, if any.
12973 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12974 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12975 set to true iff this parameter is a parameter pack. */
12977 static tree
12978 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12979 bool *is_parameter_pack)
12981 cp_token *token;
12982 cp_parameter_declarator *parameter_declarator;
12983 cp_declarator *id_declarator;
12984 tree parm;
12986 /* Assume it is a type parameter or a template parameter. */
12987 *is_non_type = false;
12988 /* Assume it not a parameter pack. */
12989 *is_parameter_pack = false;
12990 /* Peek at the next token. */
12991 token = cp_lexer_peek_token (parser->lexer);
12992 /* If it is `class' or `template', we have a type-parameter. */
12993 if (token->keyword == RID_TEMPLATE)
12994 return cp_parser_type_parameter (parser, is_parameter_pack);
12995 /* If it is `class' or `typename' we do not know yet whether it is a
12996 type parameter or a non-type parameter. Consider:
12998 template <typename T, typename T::X X> ...
13002 template <class C, class D*> ...
13004 Here, the first parameter is a type parameter, and the second is
13005 a non-type parameter. We can tell by looking at the token after
13006 the identifier -- if it is a `,', `=', or `>' then we have a type
13007 parameter. */
13008 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13010 /* Peek at the token after `class' or `typename'. */
13011 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13012 /* If it's an ellipsis, we have a template type parameter
13013 pack. */
13014 if (token->type == CPP_ELLIPSIS)
13015 return cp_parser_type_parameter (parser, is_parameter_pack);
13016 /* If it's an identifier, skip it. */
13017 if (token->type == CPP_NAME)
13018 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13019 /* Now, see if the token looks like the end of a template
13020 parameter. */
13021 if (token->type == CPP_COMMA
13022 || token->type == CPP_EQ
13023 || token->type == CPP_GREATER)
13024 return cp_parser_type_parameter (parser, is_parameter_pack);
13027 /* Otherwise, it is a non-type parameter.
13029 [temp.param]
13031 When parsing a default template-argument for a non-type
13032 template-parameter, the first non-nested `>' is taken as the end
13033 of the template parameter-list rather than a greater-than
13034 operator. */
13035 *is_non_type = true;
13036 parameter_declarator
13037 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13038 /*parenthesized_p=*/NULL);
13040 if (!parameter_declarator)
13041 return error_mark_node;
13043 /* If the parameter declaration is marked as a parameter pack, set
13044 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13045 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13046 grokdeclarator. */
13047 if (parameter_declarator->declarator
13048 && parameter_declarator->declarator->parameter_pack_p)
13050 *is_parameter_pack = true;
13051 parameter_declarator->declarator->parameter_pack_p = false;
13054 if (parameter_declarator->default_argument)
13056 /* Can happen in some cases of erroneous input (c++/34892). */
13057 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13058 /* Consume the `...' for better error recovery. */
13059 cp_lexer_consume_token (parser->lexer);
13061 /* If the next token is an ellipsis, and we don't already have it
13062 marked as a parameter pack, then we have a parameter pack (that
13063 has no declarator). */
13064 else if (!*is_parameter_pack
13065 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13066 && (declarator_can_be_parameter_pack
13067 (parameter_declarator->declarator)))
13069 /* Consume the `...'. */
13070 cp_lexer_consume_token (parser->lexer);
13071 maybe_warn_variadic_templates ();
13073 *is_parameter_pack = true;
13075 /* We might end up with a pack expansion as the type of the non-type
13076 template parameter, in which case this is a non-type template
13077 parameter pack. */
13078 else if (parameter_declarator->decl_specifiers.type
13079 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13081 *is_parameter_pack = true;
13082 parameter_declarator->decl_specifiers.type =
13083 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13086 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13088 /* Parameter packs cannot have default arguments. However, a
13089 user may try to do so, so we'll parse them and give an
13090 appropriate diagnostic here. */
13092 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13094 /* Find the name of the parameter pack. */
13095 id_declarator = parameter_declarator->declarator;
13096 while (id_declarator && id_declarator->kind != cdk_id)
13097 id_declarator = id_declarator->declarator;
13099 if (id_declarator && id_declarator->kind == cdk_id)
13100 error_at (start_token->location,
13101 "template parameter pack %qD cannot have a default argument",
13102 id_declarator->u.id.unqualified_name);
13103 else
13104 error_at (start_token->location,
13105 "template parameter pack cannot have a default argument");
13107 /* Parse the default argument, but throw away the result. */
13108 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13111 parm = grokdeclarator (parameter_declarator->declarator,
13112 &parameter_declarator->decl_specifiers,
13113 TPARM, /*initialized=*/0,
13114 /*attrlist=*/NULL);
13115 if (parm == error_mark_node)
13116 return error_mark_node;
13118 return build_tree_list (parameter_declarator->default_argument, parm);
13121 /* Parse a type-parameter.
13123 type-parameter:
13124 class identifier [opt]
13125 class identifier [opt] = type-id
13126 typename identifier [opt]
13127 typename identifier [opt] = type-id
13128 template < template-parameter-list > class identifier [opt]
13129 template < template-parameter-list > class identifier [opt]
13130 = id-expression
13132 GNU Extension (variadic templates):
13134 type-parameter:
13135 class ... identifier [opt]
13136 typename ... identifier [opt]
13138 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13139 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13140 the declaration of the parameter.
13142 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13144 static tree
13145 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13147 cp_token *token;
13148 tree parameter;
13150 /* Look for a keyword to tell us what kind of parameter this is. */
13151 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13152 if (!token)
13153 return error_mark_node;
13155 switch (token->keyword)
13157 case RID_CLASS:
13158 case RID_TYPENAME:
13160 tree identifier;
13161 tree default_argument;
13163 /* If the next token is an ellipsis, we have a template
13164 argument pack. */
13165 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13167 /* Consume the `...' token. */
13168 cp_lexer_consume_token (parser->lexer);
13169 maybe_warn_variadic_templates ();
13171 *is_parameter_pack = true;
13174 /* If the next token is an identifier, then it names the
13175 parameter. */
13176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13177 identifier = cp_parser_identifier (parser);
13178 else
13179 identifier = NULL_TREE;
13181 /* Create the parameter. */
13182 parameter = finish_template_type_parm (class_type_node, identifier);
13184 /* If the next token is an `=', we have a default argument. */
13185 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13187 /* Consume the `=' token. */
13188 cp_lexer_consume_token (parser->lexer);
13189 /* Parse the default-argument. */
13190 push_deferring_access_checks (dk_no_deferred);
13191 default_argument = cp_parser_type_id (parser);
13193 /* Template parameter packs cannot have default
13194 arguments. */
13195 if (*is_parameter_pack)
13197 if (identifier)
13198 error_at (token->location,
13199 "template parameter pack %qD cannot have a "
13200 "default argument", identifier);
13201 else
13202 error_at (token->location,
13203 "template parameter packs cannot have "
13204 "default arguments");
13205 default_argument = NULL_TREE;
13207 pop_deferring_access_checks ();
13209 else
13210 default_argument = NULL_TREE;
13212 /* Create the combined representation of the parameter and the
13213 default argument. */
13214 parameter = build_tree_list (default_argument, parameter);
13216 break;
13218 case RID_TEMPLATE:
13220 tree identifier;
13221 tree default_argument;
13223 /* Look for the `<'. */
13224 cp_parser_require (parser, CPP_LESS, RT_LESS);
13225 /* Parse the template-parameter-list. */
13226 cp_parser_template_parameter_list (parser);
13227 /* Look for the `>'. */
13228 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13229 /* Look for the `class' keyword. */
13230 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13231 /* If the next token is an ellipsis, we have a template
13232 argument pack. */
13233 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13235 /* Consume the `...' token. */
13236 cp_lexer_consume_token (parser->lexer);
13237 maybe_warn_variadic_templates ();
13239 *is_parameter_pack = true;
13241 /* If the next token is an `=', then there is a
13242 default-argument. If the next token is a `>', we are at
13243 the end of the parameter-list. If the next token is a `,',
13244 then we are at the end of this parameter. */
13245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13246 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13247 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13249 identifier = cp_parser_identifier (parser);
13250 /* Treat invalid names as if the parameter were nameless. */
13251 if (identifier == error_mark_node)
13252 identifier = NULL_TREE;
13254 else
13255 identifier = NULL_TREE;
13257 /* Create the template parameter. */
13258 parameter = finish_template_template_parm (class_type_node,
13259 identifier);
13261 /* If the next token is an `=', then there is a
13262 default-argument. */
13263 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13265 bool is_template;
13267 /* Consume the `='. */
13268 cp_lexer_consume_token (parser->lexer);
13269 /* Parse the id-expression. */
13270 push_deferring_access_checks (dk_no_deferred);
13271 /* save token before parsing the id-expression, for error
13272 reporting */
13273 token = cp_lexer_peek_token (parser->lexer);
13274 default_argument
13275 = cp_parser_id_expression (parser,
13276 /*template_keyword_p=*/false,
13277 /*check_dependency_p=*/true,
13278 /*template_p=*/&is_template,
13279 /*declarator_p=*/false,
13280 /*optional_p=*/false);
13281 if (TREE_CODE (default_argument) == TYPE_DECL)
13282 /* If the id-expression was a template-id that refers to
13283 a template-class, we already have the declaration here,
13284 so no further lookup is needed. */
13286 else
13287 /* Look up the name. */
13288 default_argument
13289 = cp_parser_lookup_name (parser, default_argument,
13290 none_type,
13291 /*is_template=*/is_template,
13292 /*is_namespace=*/false,
13293 /*check_dependency=*/true,
13294 /*ambiguous_decls=*/NULL,
13295 token->location);
13296 /* See if the default argument is valid. */
13297 default_argument
13298 = check_template_template_default_arg (default_argument);
13300 /* Template parameter packs cannot have default
13301 arguments. */
13302 if (*is_parameter_pack)
13304 if (identifier)
13305 error_at (token->location,
13306 "template parameter pack %qD cannot "
13307 "have a default argument",
13308 identifier);
13309 else
13310 error_at (token->location, "template parameter packs cannot "
13311 "have default arguments");
13312 default_argument = NULL_TREE;
13314 pop_deferring_access_checks ();
13316 else
13317 default_argument = NULL_TREE;
13319 /* Create the combined representation of the parameter and the
13320 default argument. */
13321 parameter = build_tree_list (default_argument, parameter);
13323 break;
13325 default:
13326 gcc_unreachable ();
13327 break;
13330 return parameter;
13333 /* Parse a template-id.
13335 template-id:
13336 template-name < template-argument-list [opt] >
13338 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13339 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13340 returned. Otherwise, if the template-name names a function, or set
13341 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13342 names a class, returns a TYPE_DECL for the specialization.
13344 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13345 uninstantiated templates. */
13347 static tree
13348 cp_parser_template_id (cp_parser *parser,
13349 bool template_keyword_p,
13350 bool check_dependency_p,
13351 enum tag_types tag_type,
13352 bool is_declaration)
13354 int i;
13355 tree templ;
13356 tree arguments;
13357 tree template_id;
13358 cp_token_position start_of_id = 0;
13359 deferred_access_check *chk;
13360 vec<deferred_access_check, va_gc> *access_check;
13361 cp_token *next_token = NULL, *next_token_2 = NULL;
13362 bool is_identifier;
13364 /* If the next token corresponds to a template-id, there is no need
13365 to reparse it. */
13366 next_token = cp_lexer_peek_token (parser->lexer);
13367 if (next_token->type == CPP_TEMPLATE_ID)
13369 struct tree_check *check_value;
13371 /* Get the stored value. */
13372 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13373 /* Perform any access checks that were deferred. */
13374 access_check = check_value->checks;
13375 if (access_check)
13377 FOR_EACH_VEC_ELT (*access_check, i, chk)
13378 perform_or_defer_access_check (chk->binfo,
13379 chk->decl,
13380 chk->diag_decl,
13381 tf_warning_or_error);
13383 /* Return the stored value. */
13384 return check_value->value;
13387 /* Avoid performing name lookup if there is no possibility of
13388 finding a template-id. */
13389 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13390 || (next_token->type == CPP_NAME
13391 && !cp_parser_nth_token_starts_template_argument_list_p
13392 (parser, 2)))
13394 cp_parser_error (parser, "expected template-id");
13395 return error_mark_node;
13398 /* Remember where the template-id starts. */
13399 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13400 start_of_id = cp_lexer_token_position (parser->lexer, false);
13402 push_deferring_access_checks (dk_deferred);
13404 /* Parse the template-name. */
13405 is_identifier = false;
13406 templ = cp_parser_template_name (parser, template_keyword_p,
13407 check_dependency_p,
13408 is_declaration,
13409 tag_type,
13410 &is_identifier);
13411 if (templ == error_mark_node || is_identifier)
13413 pop_deferring_access_checks ();
13414 return templ;
13417 /* If we find the sequence `[:' after a template-name, it's probably
13418 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13419 parse correctly the argument list. */
13420 next_token = cp_lexer_peek_token (parser->lexer);
13421 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13422 if (next_token->type == CPP_OPEN_SQUARE
13423 && next_token->flags & DIGRAPH
13424 && next_token_2->type == CPP_COLON
13425 && !(next_token_2->flags & PREV_WHITE))
13427 cp_parser_parse_tentatively (parser);
13428 /* Change `:' into `::'. */
13429 next_token_2->type = CPP_SCOPE;
13430 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13431 CPP_LESS. */
13432 cp_lexer_consume_token (parser->lexer);
13434 /* Parse the arguments. */
13435 arguments = cp_parser_enclosed_template_argument_list (parser);
13436 if (!cp_parser_parse_definitely (parser))
13438 /* If we couldn't parse an argument list, then we revert our changes
13439 and return simply an error. Maybe this is not a template-id
13440 after all. */
13441 next_token_2->type = CPP_COLON;
13442 cp_parser_error (parser, "expected %<<%>");
13443 pop_deferring_access_checks ();
13444 return error_mark_node;
13446 /* Otherwise, emit an error about the invalid digraph, but continue
13447 parsing because we got our argument list. */
13448 if (permerror (next_token->location,
13449 "%<<::%> cannot begin a template-argument list"))
13451 static bool hint = false;
13452 inform (next_token->location,
13453 "%<<:%> is an alternate spelling for %<[%>."
13454 " Insert whitespace between %<<%> and %<::%>");
13455 if (!hint && !flag_permissive)
13457 inform (next_token->location, "(if you use %<-fpermissive%> "
13458 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13459 "accept your code)");
13460 hint = true;
13464 else
13466 /* Look for the `<' that starts the template-argument-list. */
13467 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13469 pop_deferring_access_checks ();
13470 return error_mark_node;
13472 /* Parse the arguments. */
13473 arguments = cp_parser_enclosed_template_argument_list (parser);
13476 /* Build a representation of the specialization. */
13477 if (identifier_p (templ))
13478 template_id = build_min_nt_loc (next_token->location,
13479 TEMPLATE_ID_EXPR,
13480 templ, arguments);
13481 else if (DECL_TYPE_TEMPLATE_P (templ)
13482 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13484 bool entering_scope;
13485 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13486 template (rather than some instantiation thereof) only if
13487 is not nested within some other construct. For example, in
13488 "template <typename T> void f(T) { A<T>::", A<T> is just an
13489 instantiation of A. */
13490 entering_scope = (template_parm_scope_p ()
13491 && cp_lexer_next_token_is (parser->lexer,
13492 CPP_SCOPE));
13493 template_id
13494 = finish_template_type (templ, arguments, entering_scope);
13496 else
13498 /* If it's not a class-template or a template-template, it should be
13499 a function-template. */
13500 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13501 || TREE_CODE (templ) == OVERLOAD
13502 || BASELINK_P (templ)));
13504 template_id = lookup_template_function (templ, arguments);
13507 /* If parsing tentatively, replace the sequence of tokens that makes
13508 up the template-id with a CPP_TEMPLATE_ID token. That way,
13509 should we re-parse the token stream, we will not have to repeat
13510 the effort required to do the parse, nor will we issue duplicate
13511 error messages about problems during instantiation of the
13512 template. */
13513 if (start_of_id
13514 /* Don't do this if we had a parse error in a declarator; re-parsing
13515 might succeed if a name changes meaning (60361). */
13516 && !(cp_parser_error_occurred (parser)
13517 && cp_parser_parsing_tentatively (parser)
13518 && parser->in_declarator_p))
13520 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13522 /* Reset the contents of the START_OF_ID token. */
13523 token->type = CPP_TEMPLATE_ID;
13524 /* Retrieve any deferred checks. Do not pop this access checks yet
13525 so the memory will not be reclaimed during token replacing below. */
13526 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13527 token->u.tree_check_value->value = template_id;
13528 token->u.tree_check_value->checks = get_deferred_access_checks ();
13529 token->keyword = RID_MAX;
13531 /* Purge all subsequent tokens. */
13532 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13534 /* ??? Can we actually assume that, if template_id ==
13535 error_mark_node, we will have issued a diagnostic to the
13536 user, as opposed to simply marking the tentative parse as
13537 failed? */
13538 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13539 error_at (token->location, "parse error in template argument list");
13542 pop_to_parent_deferring_access_checks ();
13543 return template_id;
13546 /* Parse a template-name.
13548 template-name:
13549 identifier
13551 The standard should actually say:
13553 template-name:
13554 identifier
13555 operator-function-id
13557 A defect report has been filed about this issue.
13559 A conversion-function-id cannot be a template name because they cannot
13560 be part of a template-id. In fact, looking at this code:
13562 a.operator K<int>()
13564 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13565 It is impossible to call a templated conversion-function-id with an
13566 explicit argument list, since the only allowed template parameter is
13567 the type to which it is converting.
13569 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13570 `template' keyword, in a construction like:
13572 T::template f<3>()
13574 In that case `f' is taken to be a template-name, even though there
13575 is no way of knowing for sure.
13577 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13578 name refers to a set of overloaded functions, at least one of which
13579 is a template, or an IDENTIFIER_NODE with the name of the template,
13580 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13581 names are looked up inside uninstantiated templates. */
13583 static tree
13584 cp_parser_template_name (cp_parser* parser,
13585 bool template_keyword_p,
13586 bool check_dependency_p,
13587 bool is_declaration,
13588 enum tag_types tag_type,
13589 bool *is_identifier)
13591 tree identifier;
13592 tree decl;
13593 tree fns;
13594 cp_token *token = cp_lexer_peek_token (parser->lexer);
13596 /* If the next token is `operator', then we have either an
13597 operator-function-id or a conversion-function-id. */
13598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13600 /* We don't know whether we're looking at an
13601 operator-function-id or a conversion-function-id. */
13602 cp_parser_parse_tentatively (parser);
13603 /* Try an operator-function-id. */
13604 identifier = cp_parser_operator_function_id (parser);
13605 /* If that didn't work, try a conversion-function-id. */
13606 if (!cp_parser_parse_definitely (parser))
13608 cp_parser_error (parser, "expected template-name");
13609 return error_mark_node;
13612 /* Look for the identifier. */
13613 else
13614 identifier = cp_parser_identifier (parser);
13616 /* If we didn't find an identifier, we don't have a template-id. */
13617 if (identifier == error_mark_node)
13618 return error_mark_node;
13620 /* If the name immediately followed the `template' keyword, then it
13621 is a template-name. However, if the next token is not `<', then
13622 we do not treat it as a template-name, since it is not being used
13623 as part of a template-id. This enables us to handle constructs
13624 like:
13626 template <typename T> struct S { S(); };
13627 template <typename T> S<T>::S();
13629 correctly. We would treat `S' as a template -- if it were `S<T>'
13630 -- but we do not if there is no `<'. */
13632 if (processing_template_decl
13633 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13635 /* In a declaration, in a dependent context, we pretend that the
13636 "template" keyword was present in order to improve error
13637 recovery. For example, given:
13639 template <typename T> void f(T::X<int>);
13641 we want to treat "X<int>" as a template-id. */
13642 if (is_declaration
13643 && !template_keyword_p
13644 && parser->scope && TYPE_P (parser->scope)
13645 && check_dependency_p
13646 && dependent_scope_p (parser->scope)
13647 /* Do not do this for dtors (or ctors), since they never
13648 need the template keyword before their name. */
13649 && !constructor_name_p (identifier, parser->scope))
13651 cp_token_position start = 0;
13653 /* Explain what went wrong. */
13654 error_at (token->location, "non-template %qD used as template",
13655 identifier);
13656 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13657 parser->scope, identifier);
13658 /* If parsing tentatively, find the location of the "<" token. */
13659 if (cp_parser_simulate_error (parser))
13660 start = cp_lexer_token_position (parser->lexer, true);
13661 /* Parse the template arguments so that we can issue error
13662 messages about them. */
13663 cp_lexer_consume_token (parser->lexer);
13664 cp_parser_enclosed_template_argument_list (parser);
13665 /* Skip tokens until we find a good place from which to
13666 continue parsing. */
13667 cp_parser_skip_to_closing_parenthesis (parser,
13668 /*recovering=*/true,
13669 /*or_comma=*/true,
13670 /*consume_paren=*/false);
13671 /* If parsing tentatively, permanently remove the
13672 template argument list. That will prevent duplicate
13673 error messages from being issued about the missing
13674 "template" keyword. */
13675 if (start)
13676 cp_lexer_purge_tokens_after (parser->lexer, start);
13677 if (is_identifier)
13678 *is_identifier = true;
13679 return identifier;
13682 /* If the "template" keyword is present, then there is generally
13683 no point in doing name-lookup, so we just return IDENTIFIER.
13684 But, if the qualifying scope is non-dependent then we can
13685 (and must) do name-lookup normally. */
13686 if (template_keyword_p
13687 && (!parser->scope
13688 || (TYPE_P (parser->scope)
13689 && dependent_type_p (parser->scope))))
13690 return identifier;
13693 /* Look up the name. */
13694 decl = cp_parser_lookup_name (parser, identifier,
13695 tag_type,
13696 /*is_template=*/true,
13697 /*is_namespace=*/false,
13698 check_dependency_p,
13699 /*ambiguous_decls=*/NULL,
13700 token->location);
13702 /* If DECL is a template, then the name was a template-name. */
13703 if (TREE_CODE (decl) == TEMPLATE_DECL)
13705 else
13707 tree fn = NULL_TREE;
13709 /* The standard does not explicitly indicate whether a name that
13710 names a set of overloaded declarations, some of which are
13711 templates, is a template-name. However, such a name should
13712 be a template-name; otherwise, there is no way to form a
13713 template-id for the overloaded templates. */
13714 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13715 if (TREE_CODE (fns) == OVERLOAD)
13716 for (fn = fns; fn; fn = OVL_NEXT (fn))
13717 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13718 break;
13720 if (!fn)
13722 /* The name does not name a template. */
13723 cp_parser_error (parser, "expected template-name");
13724 return error_mark_node;
13728 /* If DECL is dependent, and refers to a function, then just return
13729 its name; we will look it up again during template instantiation. */
13730 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13732 tree scope = ovl_scope (decl);
13733 if (TYPE_P (scope) && dependent_type_p (scope))
13734 return identifier;
13737 return decl;
13740 /* Parse a template-argument-list.
13742 template-argument-list:
13743 template-argument ... [opt]
13744 template-argument-list , template-argument ... [opt]
13746 Returns a TREE_VEC containing the arguments. */
13748 static tree
13749 cp_parser_template_argument_list (cp_parser* parser)
13751 tree fixed_args[10];
13752 unsigned n_args = 0;
13753 unsigned alloced = 10;
13754 tree *arg_ary = fixed_args;
13755 tree vec;
13756 bool saved_in_template_argument_list_p;
13757 bool saved_ice_p;
13758 bool saved_non_ice_p;
13760 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13761 parser->in_template_argument_list_p = true;
13762 /* Even if the template-id appears in an integral
13763 constant-expression, the contents of the argument list do
13764 not. */
13765 saved_ice_p = parser->integral_constant_expression_p;
13766 parser->integral_constant_expression_p = false;
13767 saved_non_ice_p = parser->non_integral_constant_expression_p;
13768 parser->non_integral_constant_expression_p = false;
13770 /* Parse the arguments. */
13773 tree argument;
13775 if (n_args)
13776 /* Consume the comma. */
13777 cp_lexer_consume_token (parser->lexer);
13779 /* Parse the template-argument. */
13780 argument = cp_parser_template_argument (parser);
13782 /* If the next token is an ellipsis, we're expanding a template
13783 argument pack. */
13784 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13786 if (argument == error_mark_node)
13788 cp_token *token = cp_lexer_peek_token (parser->lexer);
13789 error_at (token->location,
13790 "expected parameter pack before %<...%>");
13792 /* Consume the `...' token. */
13793 cp_lexer_consume_token (parser->lexer);
13795 /* Make the argument into a TYPE_PACK_EXPANSION or
13796 EXPR_PACK_EXPANSION. */
13797 argument = make_pack_expansion (argument);
13800 if (n_args == alloced)
13802 alloced *= 2;
13804 if (arg_ary == fixed_args)
13806 arg_ary = XNEWVEC (tree, alloced);
13807 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13809 else
13810 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13812 arg_ary[n_args++] = argument;
13814 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13816 vec = make_tree_vec (n_args);
13818 while (n_args--)
13819 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13821 if (arg_ary != fixed_args)
13822 free (arg_ary);
13823 parser->non_integral_constant_expression_p = saved_non_ice_p;
13824 parser->integral_constant_expression_p = saved_ice_p;
13825 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13826 #ifdef ENABLE_CHECKING
13827 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13828 #endif
13829 return vec;
13832 /* Parse a template-argument.
13834 template-argument:
13835 assignment-expression
13836 type-id
13837 id-expression
13839 The representation is that of an assignment-expression, type-id, or
13840 id-expression -- except that the qualified id-expression is
13841 evaluated, so that the value returned is either a DECL or an
13842 OVERLOAD.
13844 Although the standard says "assignment-expression", it forbids
13845 throw-expressions or assignments in the template argument.
13846 Therefore, we use "conditional-expression" instead. */
13848 static tree
13849 cp_parser_template_argument (cp_parser* parser)
13851 tree argument;
13852 bool template_p;
13853 bool address_p;
13854 bool maybe_type_id = false;
13855 cp_token *token = NULL, *argument_start_token = NULL;
13856 location_t loc = 0;
13857 cp_id_kind idk;
13859 /* There's really no way to know what we're looking at, so we just
13860 try each alternative in order.
13862 [temp.arg]
13864 In a template-argument, an ambiguity between a type-id and an
13865 expression is resolved to a type-id, regardless of the form of
13866 the corresponding template-parameter.
13868 Therefore, we try a type-id first. */
13869 cp_parser_parse_tentatively (parser);
13870 argument = cp_parser_template_type_arg (parser);
13871 /* If there was no error parsing the type-id but the next token is a
13872 '>>', our behavior depends on which dialect of C++ we're
13873 parsing. In C++98, we probably found a typo for '> >'. But there
13874 are type-id which are also valid expressions. For instance:
13876 struct X { int operator >> (int); };
13877 template <int V> struct Foo {};
13878 Foo<X () >> 5> r;
13880 Here 'X()' is a valid type-id of a function type, but the user just
13881 wanted to write the expression "X() >> 5". Thus, we remember that we
13882 found a valid type-id, but we still try to parse the argument as an
13883 expression to see what happens.
13885 In C++0x, the '>>' will be considered two separate '>'
13886 tokens. */
13887 if (!cp_parser_error_occurred (parser)
13888 && cxx_dialect == cxx98
13889 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13891 maybe_type_id = true;
13892 cp_parser_abort_tentative_parse (parser);
13894 else
13896 /* If the next token isn't a `,' or a `>', then this argument wasn't
13897 really finished. This means that the argument is not a valid
13898 type-id. */
13899 if (!cp_parser_next_token_ends_template_argument_p (parser))
13900 cp_parser_error (parser, "expected template-argument");
13901 /* If that worked, we're done. */
13902 if (cp_parser_parse_definitely (parser))
13903 return argument;
13905 /* We're still not sure what the argument will be. */
13906 cp_parser_parse_tentatively (parser);
13907 /* Try a template. */
13908 argument_start_token = cp_lexer_peek_token (parser->lexer);
13909 argument = cp_parser_id_expression (parser,
13910 /*template_keyword_p=*/false,
13911 /*check_dependency_p=*/true,
13912 &template_p,
13913 /*declarator_p=*/false,
13914 /*optional_p=*/false);
13915 /* If the next token isn't a `,' or a `>', then this argument wasn't
13916 really finished. */
13917 if (!cp_parser_next_token_ends_template_argument_p (parser))
13918 cp_parser_error (parser, "expected template-argument");
13919 if (!cp_parser_error_occurred (parser))
13921 /* Figure out what is being referred to. If the id-expression
13922 was for a class template specialization, then we will have a
13923 TYPE_DECL at this point. There is no need to do name lookup
13924 at this point in that case. */
13925 if (TREE_CODE (argument) != TYPE_DECL)
13926 argument = cp_parser_lookup_name (parser, argument,
13927 none_type,
13928 /*is_template=*/template_p,
13929 /*is_namespace=*/false,
13930 /*check_dependency=*/true,
13931 /*ambiguous_decls=*/NULL,
13932 argument_start_token->location);
13933 if (TREE_CODE (argument) != TEMPLATE_DECL
13934 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13935 cp_parser_error (parser, "expected template-name");
13937 if (cp_parser_parse_definitely (parser))
13938 return argument;
13939 /* It must be a non-type argument. There permitted cases are given
13940 in [temp.arg.nontype]:
13942 -- an integral constant-expression of integral or enumeration
13943 type; or
13945 -- the name of a non-type template-parameter; or
13947 -- the name of an object or function with external linkage...
13949 -- the address of an object or function with external linkage...
13951 -- a pointer to member... */
13952 /* Look for a non-type template parameter. */
13953 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13955 cp_parser_parse_tentatively (parser);
13956 argument = cp_parser_primary_expression (parser,
13957 /*address_p=*/false,
13958 /*cast_p=*/false,
13959 /*template_arg_p=*/true,
13960 &idk);
13961 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13962 || !cp_parser_next_token_ends_template_argument_p (parser))
13963 cp_parser_simulate_error (parser);
13964 if (cp_parser_parse_definitely (parser))
13965 return argument;
13968 /* If the next token is "&", the argument must be the address of an
13969 object or function with external linkage. */
13970 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13971 if (address_p)
13973 loc = cp_lexer_peek_token (parser->lexer)->location;
13974 cp_lexer_consume_token (parser->lexer);
13976 /* See if we might have an id-expression. */
13977 token = cp_lexer_peek_token (parser->lexer);
13978 if (token->type == CPP_NAME
13979 || token->keyword == RID_OPERATOR
13980 || token->type == CPP_SCOPE
13981 || token->type == CPP_TEMPLATE_ID
13982 || token->type == CPP_NESTED_NAME_SPECIFIER)
13984 cp_parser_parse_tentatively (parser);
13985 argument = cp_parser_primary_expression (parser,
13986 address_p,
13987 /*cast_p=*/false,
13988 /*template_arg_p=*/true,
13989 &idk);
13990 if (cp_parser_error_occurred (parser)
13991 || !cp_parser_next_token_ends_template_argument_p (parser))
13992 cp_parser_abort_tentative_parse (parser);
13993 else
13995 tree probe;
13997 if (INDIRECT_REF_P (argument))
13999 /* Strip the dereference temporarily. */
14000 gcc_assert (REFERENCE_REF_P (argument));
14001 argument = TREE_OPERAND (argument, 0);
14004 /* If we're in a template, we represent a qualified-id referring
14005 to a static data member as a SCOPE_REF even if the scope isn't
14006 dependent so that we can check access control later. */
14007 probe = argument;
14008 if (TREE_CODE (probe) == SCOPE_REF)
14009 probe = TREE_OPERAND (probe, 1);
14010 if (VAR_P (probe))
14012 /* A variable without external linkage might still be a
14013 valid constant-expression, so no error is issued here
14014 if the external-linkage check fails. */
14015 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14016 cp_parser_simulate_error (parser);
14018 else if (is_overloaded_fn (argument))
14019 /* All overloaded functions are allowed; if the external
14020 linkage test does not pass, an error will be issued
14021 later. */
14023 else if (address_p
14024 && (TREE_CODE (argument) == OFFSET_REF
14025 || TREE_CODE (argument) == SCOPE_REF))
14026 /* A pointer-to-member. */
14028 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14030 else
14031 cp_parser_simulate_error (parser);
14033 if (cp_parser_parse_definitely (parser))
14035 if (address_p)
14036 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14037 tf_warning_or_error);
14038 else
14039 argument = convert_from_reference (argument);
14040 return argument;
14044 /* If the argument started with "&", there are no other valid
14045 alternatives at this point. */
14046 if (address_p)
14048 cp_parser_error (parser, "invalid non-type template argument");
14049 return error_mark_node;
14052 /* If the argument wasn't successfully parsed as a type-id followed
14053 by '>>', the argument can only be a constant expression now.
14054 Otherwise, we try parsing the constant-expression tentatively,
14055 because the argument could really be a type-id. */
14056 if (maybe_type_id)
14057 cp_parser_parse_tentatively (parser);
14058 argument = cp_parser_constant_expression (parser,
14059 /*allow_non_constant_p=*/false,
14060 /*non_constant_p=*/NULL);
14061 if (!maybe_type_id)
14062 return argument;
14063 if (!cp_parser_next_token_ends_template_argument_p (parser))
14064 cp_parser_error (parser, "expected template-argument");
14065 if (cp_parser_parse_definitely (parser))
14066 return argument;
14067 /* We did our best to parse the argument as a non type-id, but that
14068 was the only alternative that matched (albeit with a '>' after
14069 it). We can assume it's just a typo from the user, and a
14070 diagnostic will then be issued. */
14071 return cp_parser_template_type_arg (parser);
14074 /* Parse an explicit-instantiation.
14076 explicit-instantiation:
14077 template declaration
14079 Although the standard says `declaration', what it really means is:
14081 explicit-instantiation:
14082 template decl-specifier-seq [opt] declarator [opt] ;
14084 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14085 supposed to be allowed. A defect report has been filed about this
14086 issue.
14088 GNU Extension:
14090 explicit-instantiation:
14091 storage-class-specifier template
14092 decl-specifier-seq [opt] declarator [opt] ;
14093 function-specifier template
14094 decl-specifier-seq [opt] declarator [opt] ; */
14096 static void
14097 cp_parser_explicit_instantiation (cp_parser* parser)
14099 int declares_class_or_enum;
14100 cp_decl_specifier_seq decl_specifiers;
14101 tree extension_specifier = NULL_TREE;
14103 timevar_push (TV_TEMPLATE_INST);
14105 /* Look for an (optional) storage-class-specifier or
14106 function-specifier. */
14107 if (cp_parser_allow_gnu_extensions_p (parser))
14109 extension_specifier
14110 = cp_parser_storage_class_specifier_opt (parser);
14111 if (!extension_specifier)
14112 extension_specifier
14113 = cp_parser_function_specifier_opt (parser,
14114 /*decl_specs=*/NULL);
14117 /* Look for the `template' keyword. */
14118 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14119 /* Let the front end know that we are processing an explicit
14120 instantiation. */
14121 begin_explicit_instantiation ();
14122 /* [temp.explicit] says that we are supposed to ignore access
14123 control while processing explicit instantiation directives. */
14124 push_deferring_access_checks (dk_no_check);
14125 /* Parse a decl-specifier-seq. */
14126 cp_parser_decl_specifier_seq (parser,
14127 CP_PARSER_FLAGS_OPTIONAL,
14128 &decl_specifiers,
14129 &declares_class_or_enum);
14130 /* If there was exactly one decl-specifier, and it declared a class,
14131 and there's no declarator, then we have an explicit type
14132 instantiation. */
14133 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14135 tree type;
14137 type = check_tag_decl (&decl_specifiers,
14138 /*explicit_type_instantiation_p=*/true);
14139 /* Turn access control back on for names used during
14140 template instantiation. */
14141 pop_deferring_access_checks ();
14142 if (type)
14143 do_type_instantiation (type, extension_specifier,
14144 /*complain=*/tf_error);
14146 else
14148 cp_declarator *declarator;
14149 tree decl;
14151 /* Parse the declarator. */
14152 declarator
14153 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14154 /*ctor_dtor_or_conv_p=*/NULL,
14155 /*parenthesized_p=*/NULL,
14156 /*member_p=*/false);
14157 if (declares_class_or_enum & 2)
14158 cp_parser_check_for_definition_in_return_type (declarator,
14159 decl_specifiers.type,
14160 decl_specifiers.locations[ds_type_spec]);
14161 if (declarator != cp_error_declarator)
14163 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14164 permerror (decl_specifiers.locations[ds_inline],
14165 "explicit instantiation shall not use"
14166 " %<inline%> specifier");
14167 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14168 permerror (decl_specifiers.locations[ds_constexpr],
14169 "explicit instantiation shall not use"
14170 " %<constexpr%> specifier");
14172 decl = grokdeclarator (declarator, &decl_specifiers,
14173 NORMAL, 0, &decl_specifiers.attributes);
14174 /* Turn access control back on for names used during
14175 template instantiation. */
14176 pop_deferring_access_checks ();
14177 /* Do the explicit instantiation. */
14178 do_decl_instantiation (decl, extension_specifier);
14180 else
14182 pop_deferring_access_checks ();
14183 /* Skip the body of the explicit instantiation. */
14184 cp_parser_skip_to_end_of_statement (parser);
14187 /* We're done with the instantiation. */
14188 end_explicit_instantiation ();
14190 cp_parser_consume_semicolon_at_end_of_statement (parser);
14192 timevar_pop (TV_TEMPLATE_INST);
14195 /* Parse an explicit-specialization.
14197 explicit-specialization:
14198 template < > declaration
14200 Although the standard says `declaration', what it really means is:
14202 explicit-specialization:
14203 template <> decl-specifier [opt] init-declarator [opt] ;
14204 template <> function-definition
14205 template <> explicit-specialization
14206 template <> template-declaration */
14208 static void
14209 cp_parser_explicit_specialization (cp_parser* parser)
14211 bool need_lang_pop;
14212 cp_token *token = cp_lexer_peek_token (parser->lexer);
14214 /* Look for the `template' keyword. */
14215 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14216 /* Look for the `<'. */
14217 cp_parser_require (parser, CPP_LESS, RT_LESS);
14218 /* Look for the `>'. */
14219 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14220 /* We have processed another parameter list. */
14221 ++parser->num_template_parameter_lists;
14222 /* [temp]
14224 A template ... explicit specialization ... shall not have C
14225 linkage. */
14226 if (current_lang_name == lang_name_c)
14228 error_at (token->location, "template specialization with C linkage");
14229 /* Give it C++ linkage to avoid confusing other parts of the
14230 front end. */
14231 push_lang_context (lang_name_cplusplus);
14232 need_lang_pop = true;
14234 else
14235 need_lang_pop = false;
14236 /* Let the front end know that we are beginning a specialization. */
14237 if (!begin_specialization ())
14239 end_specialization ();
14240 return;
14243 /* If the next keyword is `template', we need to figure out whether
14244 or not we're looking a template-declaration. */
14245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14247 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14248 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14249 cp_parser_template_declaration_after_export (parser,
14250 /*member_p=*/false);
14251 else
14252 cp_parser_explicit_specialization (parser);
14254 else
14255 /* Parse the dependent declaration. */
14256 cp_parser_single_declaration (parser,
14257 /*checks=*/NULL,
14258 /*member_p=*/false,
14259 /*explicit_specialization_p=*/true,
14260 /*friend_p=*/NULL);
14261 /* We're done with the specialization. */
14262 end_specialization ();
14263 /* For the erroneous case of a template with C linkage, we pushed an
14264 implicit C++ linkage scope; exit that scope now. */
14265 if (need_lang_pop)
14266 pop_lang_context ();
14267 /* We're done with this parameter list. */
14268 --parser->num_template_parameter_lists;
14271 /* Parse a type-specifier.
14273 type-specifier:
14274 simple-type-specifier
14275 class-specifier
14276 enum-specifier
14277 elaborated-type-specifier
14278 cv-qualifier
14280 GNU Extension:
14282 type-specifier:
14283 __complex__
14285 Returns a representation of the type-specifier. For a
14286 class-specifier, enum-specifier, or elaborated-type-specifier, a
14287 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14289 The parser flags FLAGS is used to control type-specifier parsing.
14291 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14292 in a decl-specifier-seq.
14294 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14295 class-specifier, enum-specifier, or elaborated-type-specifier, then
14296 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14297 if a type is declared; 2 if it is defined. Otherwise, it is set to
14298 zero.
14300 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14301 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14302 is set to FALSE. */
14304 static tree
14305 cp_parser_type_specifier (cp_parser* parser,
14306 cp_parser_flags flags,
14307 cp_decl_specifier_seq *decl_specs,
14308 bool is_declaration,
14309 int* declares_class_or_enum,
14310 bool* is_cv_qualifier)
14312 tree type_spec = NULL_TREE;
14313 cp_token *token;
14314 enum rid keyword;
14315 cp_decl_spec ds = ds_last;
14317 /* Assume this type-specifier does not declare a new type. */
14318 if (declares_class_or_enum)
14319 *declares_class_or_enum = 0;
14320 /* And that it does not specify a cv-qualifier. */
14321 if (is_cv_qualifier)
14322 *is_cv_qualifier = false;
14323 /* Peek at the next token. */
14324 token = cp_lexer_peek_token (parser->lexer);
14326 /* If we're looking at a keyword, we can use that to guide the
14327 production we choose. */
14328 keyword = token->keyword;
14329 switch (keyword)
14331 case RID_ENUM:
14332 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14333 goto elaborated_type_specifier;
14335 /* Look for the enum-specifier. */
14336 type_spec = cp_parser_enum_specifier (parser);
14337 /* If that worked, we're done. */
14338 if (type_spec)
14340 if (declares_class_or_enum)
14341 *declares_class_or_enum = 2;
14342 if (decl_specs)
14343 cp_parser_set_decl_spec_type (decl_specs,
14344 type_spec,
14345 token,
14346 /*type_definition_p=*/true);
14347 return type_spec;
14349 else
14350 goto elaborated_type_specifier;
14352 /* Any of these indicate either a class-specifier, or an
14353 elaborated-type-specifier. */
14354 case RID_CLASS:
14355 case RID_STRUCT:
14356 case RID_UNION:
14357 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14358 goto elaborated_type_specifier;
14360 /* Parse tentatively so that we can back up if we don't find a
14361 class-specifier. */
14362 cp_parser_parse_tentatively (parser);
14363 /* Look for the class-specifier. */
14364 type_spec = cp_parser_class_specifier (parser);
14365 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14366 /* If that worked, we're done. */
14367 if (cp_parser_parse_definitely (parser))
14369 if (declares_class_or_enum)
14370 *declares_class_or_enum = 2;
14371 if (decl_specs)
14372 cp_parser_set_decl_spec_type (decl_specs,
14373 type_spec,
14374 token,
14375 /*type_definition_p=*/true);
14376 return type_spec;
14379 /* Fall through. */
14380 elaborated_type_specifier:
14381 /* We're declaring (not defining) a class or enum. */
14382 if (declares_class_or_enum)
14383 *declares_class_or_enum = 1;
14385 /* Fall through. */
14386 case RID_TYPENAME:
14387 /* Look for an elaborated-type-specifier. */
14388 type_spec
14389 = (cp_parser_elaborated_type_specifier
14390 (parser,
14391 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14392 is_declaration));
14393 if (decl_specs)
14394 cp_parser_set_decl_spec_type (decl_specs,
14395 type_spec,
14396 token,
14397 /*type_definition_p=*/false);
14398 return type_spec;
14400 case RID_CONST:
14401 ds = ds_const;
14402 if (is_cv_qualifier)
14403 *is_cv_qualifier = true;
14404 break;
14406 case RID_VOLATILE:
14407 ds = ds_volatile;
14408 if (is_cv_qualifier)
14409 *is_cv_qualifier = true;
14410 break;
14412 case RID_RESTRICT:
14413 ds = ds_restrict;
14414 if (is_cv_qualifier)
14415 *is_cv_qualifier = true;
14416 break;
14418 case RID_COMPLEX:
14419 /* The `__complex__' keyword is a GNU extension. */
14420 ds = ds_complex;
14421 break;
14423 default:
14424 break;
14427 /* Handle simple keywords. */
14428 if (ds != ds_last)
14430 if (decl_specs)
14432 set_and_check_decl_spec_loc (decl_specs, ds, token);
14433 decl_specs->any_specifiers_p = true;
14435 return cp_lexer_consume_token (parser->lexer)->u.value;
14438 /* If we do not already have a type-specifier, assume we are looking
14439 at a simple-type-specifier. */
14440 type_spec = cp_parser_simple_type_specifier (parser,
14441 decl_specs,
14442 flags);
14444 /* If we didn't find a type-specifier, and a type-specifier was not
14445 optional in this context, issue an error message. */
14446 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14448 cp_parser_error (parser, "expected type specifier");
14449 return error_mark_node;
14452 return type_spec;
14455 /* Parse a simple-type-specifier.
14457 simple-type-specifier:
14458 :: [opt] nested-name-specifier [opt] type-name
14459 :: [opt] nested-name-specifier template template-id
14460 char
14461 wchar_t
14462 bool
14463 short
14465 long
14466 signed
14467 unsigned
14468 float
14469 double
14470 void
14472 C++0x Extension:
14474 simple-type-specifier:
14475 auto
14476 decltype ( expression )
14477 char16_t
14478 char32_t
14479 __underlying_type ( type-id )
14481 GNU Extension:
14483 simple-type-specifier:
14484 __int128
14485 __typeof__ unary-expression
14486 __typeof__ ( type-id )
14487 __typeof__ ( type-id ) { initializer-list , [opt] }
14489 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14490 appropriately updated. */
14492 static tree
14493 cp_parser_simple_type_specifier (cp_parser* parser,
14494 cp_decl_specifier_seq *decl_specs,
14495 cp_parser_flags flags)
14497 tree type = NULL_TREE;
14498 cp_token *token;
14500 /* Peek at the next token. */
14501 token = cp_lexer_peek_token (parser->lexer);
14503 /* If we're looking at a keyword, things are easy. */
14504 switch (token->keyword)
14506 case RID_CHAR:
14507 if (decl_specs)
14508 decl_specs->explicit_char_p = true;
14509 type = char_type_node;
14510 break;
14511 case RID_CHAR16:
14512 type = char16_type_node;
14513 break;
14514 case RID_CHAR32:
14515 type = char32_type_node;
14516 break;
14517 case RID_WCHAR:
14518 type = wchar_type_node;
14519 break;
14520 case RID_BOOL:
14521 type = boolean_type_node;
14522 break;
14523 case RID_SHORT:
14524 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14525 type = short_integer_type_node;
14526 break;
14527 case RID_INT:
14528 if (decl_specs)
14529 decl_specs->explicit_int_p = true;
14530 type = integer_type_node;
14531 break;
14532 case RID_INT128:
14533 if (!int128_integer_type_node)
14534 break;
14535 if (decl_specs)
14536 decl_specs->explicit_int128_p = true;
14537 type = int128_integer_type_node;
14538 break;
14539 case RID_LONG:
14540 if (decl_specs)
14541 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14542 type = long_integer_type_node;
14543 break;
14544 case RID_SIGNED:
14545 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14546 type = integer_type_node;
14547 break;
14548 case RID_UNSIGNED:
14549 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14550 type = unsigned_type_node;
14551 break;
14552 case RID_FLOAT:
14553 type = float_type_node;
14554 break;
14555 case RID_DOUBLE:
14556 type = double_type_node;
14557 break;
14558 case RID_VOID:
14559 type = void_type_node;
14560 break;
14562 case RID_AUTO:
14563 maybe_warn_cpp0x (CPP0X_AUTO);
14564 if (parser->auto_is_implicit_function_template_parm_p)
14566 type = synthesize_implicit_template_parm (parser);
14568 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14570 if (cxx_dialect < cxx1y)
14571 pedwarn (location_of (type), 0,
14572 "use of %<auto%> in lambda parameter declaration "
14573 "only available with "
14574 "-std=c++1y or -std=gnu++1y");
14576 else if (cxx_dialect < cxx1y)
14577 pedwarn (location_of (type), 0,
14578 "use of %<auto%> in parameter declaration "
14579 "only available with "
14580 "-std=c++1y or -std=gnu++1y");
14581 else
14582 pedwarn (location_of (type), OPT_Wpedantic,
14583 "ISO C++ forbids use of %<auto%> in parameter "
14584 "declaration");
14586 else
14587 type = make_auto ();
14588 break;
14590 case RID_DECLTYPE:
14591 /* Since DR 743, decltype can either be a simple-type-specifier by
14592 itself or begin a nested-name-specifier. Parsing it will replace
14593 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14594 handling below decide what to do. */
14595 cp_parser_decltype (parser);
14596 cp_lexer_set_token_position (parser->lexer, token);
14597 break;
14599 case RID_TYPEOF:
14600 /* Consume the `typeof' token. */
14601 cp_lexer_consume_token (parser->lexer);
14602 /* Parse the operand to `typeof'. */
14603 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14604 /* If it is not already a TYPE, take its type. */
14605 if (!TYPE_P (type))
14606 type = finish_typeof (type);
14608 if (decl_specs)
14609 cp_parser_set_decl_spec_type (decl_specs, type,
14610 token,
14611 /*type_definition_p=*/false);
14613 return type;
14615 case RID_UNDERLYING_TYPE:
14616 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14617 if (decl_specs)
14618 cp_parser_set_decl_spec_type (decl_specs, type,
14619 token,
14620 /*type_definition_p=*/false);
14622 return type;
14624 case RID_BASES:
14625 case RID_DIRECT_BASES:
14626 type = cp_parser_trait_expr (parser, token->keyword);
14627 if (decl_specs)
14628 cp_parser_set_decl_spec_type (decl_specs, type,
14629 token,
14630 /*type_definition_p=*/false);
14631 return type;
14632 default:
14633 break;
14636 /* If token is an already-parsed decltype not followed by ::,
14637 it's a simple-type-specifier. */
14638 if (token->type == CPP_DECLTYPE
14639 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14641 type = token->u.value;
14642 if (decl_specs)
14643 cp_parser_set_decl_spec_type (decl_specs, type,
14644 token,
14645 /*type_definition_p=*/false);
14646 cp_lexer_consume_token (parser->lexer);
14647 return type;
14650 /* If the type-specifier was for a built-in type, we're done. */
14651 if (type)
14653 /* Record the type. */
14654 if (decl_specs
14655 && (token->keyword != RID_SIGNED
14656 && token->keyword != RID_UNSIGNED
14657 && token->keyword != RID_SHORT
14658 && token->keyword != RID_LONG))
14659 cp_parser_set_decl_spec_type (decl_specs,
14660 type,
14661 token,
14662 /*type_definition_p=*/false);
14663 if (decl_specs)
14664 decl_specs->any_specifiers_p = true;
14666 /* Consume the token. */
14667 cp_lexer_consume_token (parser->lexer);
14669 /* There is no valid C++ program where a non-template type is
14670 followed by a "<". That usually indicates that the user thought
14671 that the type was a template. */
14672 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14673 token->location);
14675 return TYPE_NAME (type);
14678 /* The type-specifier must be a user-defined type. */
14679 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14681 bool qualified_p;
14682 bool global_p;
14684 /* Don't gobble tokens or issue error messages if this is an
14685 optional type-specifier. */
14686 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14687 cp_parser_parse_tentatively (parser);
14689 /* Look for the optional `::' operator. */
14690 global_p
14691 = (cp_parser_global_scope_opt (parser,
14692 /*current_scope_valid_p=*/false)
14693 != NULL_TREE);
14694 /* Look for the nested-name specifier. */
14695 qualified_p
14696 = (cp_parser_nested_name_specifier_opt (parser,
14697 /*typename_keyword_p=*/false,
14698 /*check_dependency_p=*/true,
14699 /*type_p=*/false,
14700 /*is_declaration=*/false)
14701 != NULL_TREE);
14702 token = cp_lexer_peek_token (parser->lexer);
14703 /* If we have seen a nested-name-specifier, and the next token
14704 is `template', then we are using the template-id production. */
14705 if (parser->scope
14706 && cp_parser_optional_template_keyword (parser))
14708 /* Look for the template-id. */
14709 type = cp_parser_template_id (parser,
14710 /*template_keyword_p=*/true,
14711 /*check_dependency_p=*/true,
14712 none_type,
14713 /*is_declaration=*/false);
14714 /* If the template-id did not name a type, we are out of
14715 luck. */
14716 if (TREE_CODE (type) != TYPE_DECL)
14718 cp_parser_error (parser, "expected template-id for type");
14719 type = NULL_TREE;
14722 /* Otherwise, look for a type-name. */
14723 else
14724 type = cp_parser_type_name (parser);
14725 /* Keep track of all name-lookups performed in class scopes. */
14726 if (type
14727 && !global_p
14728 && !qualified_p
14729 && TREE_CODE (type) == TYPE_DECL
14730 && identifier_p (DECL_NAME (type)))
14731 maybe_note_name_used_in_class (DECL_NAME (type), type);
14732 /* If it didn't work out, we don't have a TYPE. */
14733 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14734 && !cp_parser_parse_definitely (parser))
14735 type = NULL_TREE;
14736 if (type && decl_specs)
14737 cp_parser_set_decl_spec_type (decl_specs, type,
14738 token,
14739 /*type_definition_p=*/false);
14742 /* If we didn't get a type-name, issue an error message. */
14743 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14745 cp_parser_error (parser, "expected type-name");
14746 return error_mark_node;
14749 if (type && type != error_mark_node)
14751 /* See if TYPE is an Objective-C type, and if so, parse and
14752 accept any protocol references following it. Do this before
14753 the cp_parser_check_for_invalid_template_id() call, because
14754 Objective-C types can be followed by '<...>' which would
14755 enclose protocol names rather than template arguments, and so
14756 everything is fine. */
14757 if (c_dialect_objc () && !parser->scope
14758 && (objc_is_id (type) || objc_is_class_name (type)))
14760 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14761 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14763 /* Clobber the "unqualified" type previously entered into
14764 DECL_SPECS with the new, improved protocol-qualified version. */
14765 if (decl_specs)
14766 decl_specs->type = qual_type;
14768 return qual_type;
14771 /* There is no valid C++ program where a non-template type is
14772 followed by a "<". That usually indicates that the user
14773 thought that the type was a template. */
14774 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14775 none_type,
14776 token->location);
14779 return type;
14782 /* Parse a type-name.
14784 type-name:
14785 class-name
14786 enum-name
14787 typedef-name
14788 simple-template-id [in c++0x]
14790 enum-name:
14791 identifier
14793 typedef-name:
14794 identifier
14796 Returns a TYPE_DECL for the type. */
14798 static tree
14799 cp_parser_type_name (cp_parser* parser)
14801 tree type_decl;
14803 /* We can't know yet whether it is a class-name or not. */
14804 cp_parser_parse_tentatively (parser);
14805 /* Try a class-name. */
14806 type_decl = cp_parser_class_name (parser,
14807 /*typename_keyword_p=*/false,
14808 /*template_keyword_p=*/false,
14809 none_type,
14810 /*check_dependency_p=*/true,
14811 /*class_head_p=*/false,
14812 /*is_declaration=*/false);
14813 /* If it's not a class-name, keep looking. */
14814 if (!cp_parser_parse_definitely (parser))
14816 if (cxx_dialect < cxx11)
14817 /* It must be a typedef-name or an enum-name. */
14818 return cp_parser_nonclass_name (parser);
14820 cp_parser_parse_tentatively (parser);
14821 /* It is either a simple-template-id representing an
14822 instantiation of an alias template... */
14823 type_decl = cp_parser_template_id (parser,
14824 /*template_keyword_p=*/false,
14825 /*check_dependency_p=*/true,
14826 none_type,
14827 /*is_declaration=*/false);
14828 /* Note that this must be an instantiation of an alias template
14829 because [temp.names]/6 says:
14831 A template-id that names an alias template specialization
14832 is a type-name.
14834 Whereas [temp.names]/7 says:
14836 A simple-template-id that names a class template
14837 specialization is a class-name. */
14838 if (type_decl != NULL_TREE
14839 && TREE_CODE (type_decl) == TYPE_DECL
14840 && TYPE_DECL_ALIAS_P (type_decl))
14841 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14842 else
14843 cp_parser_simulate_error (parser);
14845 if (!cp_parser_parse_definitely (parser))
14846 /* ... Or a typedef-name or an enum-name. */
14847 return cp_parser_nonclass_name (parser);
14850 return type_decl;
14853 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14855 enum-name:
14856 identifier
14858 typedef-name:
14859 identifier
14861 Returns a TYPE_DECL for the type. */
14863 static tree
14864 cp_parser_nonclass_name (cp_parser* parser)
14866 tree type_decl;
14867 tree identifier;
14869 cp_token *token = cp_lexer_peek_token (parser->lexer);
14870 identifier = cp_parser_identifier (parser);
14871 if (identifier == error_mark_node)
14872 return error_mark_node;
14874 /* Look up the type-name. */
14875 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14877 type_decl = strip_using_decl (type_decl);
14879 if (TREE_CODE (type_decl) != TYPE_DECL
14880 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14882 /* See if this is an Objective-C type. */
14883 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14884 tree type = objc_get_protocol_qualified_type (identifier, protos);
14885 if (type)
14886 type_decl = TYPE_NAME (type);
14889 /* Issue an error if we did not find a type-name. */
14890 if (TREE_CODE (type_decl) != TYPE_DECL
14891 /* In Objective-C, we have the complication that class names are
14892 normally type names and start declarations (eg, the
14893 "NSObject" in "NSObject *object;"), but can be used in an
14894 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14895 is an expression. So, a classname followed by a dot is not a
14896 valid type-name. */
14897 || (objc_is_class_name (TREE_TYPE (type_decl))
14898 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14900 if (!cp_parser_simulate_error (parser))
14901 cp_parser_name_lookup_error (parser, identifier, type_decl,
14902 NLE_TYPE, token->location);
14903 return error_mark_node;
14905 /* Remember that the name was used in the definition of the
14906 current class so that we can check later to see if the
14907 meaning would have been different after the class was
14908 entirely defined. */
14909 else if (type_decl != error_mark_node
14910 && !parser->scope)
14911 maybe_note_name_used_in_class (identifier, type_decl);
14913 return type_decl;
14916 /* Parse an elaborated-type-specifier. Note that the grammar given
14917 here incorporates the resolution to DR68.
14919 elaborated-type-specifier:
14920 class-key :: [opt] nested-name-specifier [opt] identifier
14921 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14922 enum-key :: [opt] nested-name-specifier [opt] identifier
14923 typename :: [opt] nested-name-specifier identifier
14924 typename :: [opt] nested-name-specifier template [opt]
14925 template-id
14927 GNU extension:
14929 elaborated-type-specifier:
14930 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14931 class-key attributes :: [opt] nested-name-specifier [opt]
14932 template [opt] template-id
14933 enum attributes :: [opt] nested-name-specifier [opt] identifier
14935 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14936 declared `friend'. If IS_DECLARATION is TRUE, then this
14937 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14938 something is being declared.
14940 Returns the TYPE specified. */
14942 static tree
14943 cp_parser_elaborated_type_specifier (cp_parser* parser,
14944 bool is_friend,
14945 bool is_declaration)
14947 enum tag_types tag_type;
14948 tree identifier;
14949 tree type = NULL_TREE;
14950 tree attributes = NULL_TREE;
14951 tree globalscope;
14952 cp_token *token = NULL;
14954 /* See if we're looking at the `enum' keyword. */
14955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14957 /* Consume the `enum' token. */
14958 cp_lexer_consume_token (parser->lexer);
14959 /* Remember that it's an enumeration type. */
14960 tag_type = enum_type;
14961 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14962 enums) is used here. */
14963 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14964 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14966 pedwarn (input_location, 0, "elaborated-type-specifier "
14967 "for a scoped enum must not use the %<%D%> keyword",
14968 cp_lexer_peek_token (parser->lexer)->u.value);
14969 /* Consume the `struct' or `class' and parse it anyway. */
14970 cp_lexer_consume_token (parser->lexer);
14972 /* Parse the attributes. */
14973 attributes = cp_parser_attributes_opt (parser);
14975 /* Or, it might be `typename'. */
14976 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14977 RID_TYPENAME))
14979 /* Consume the `typename' token. */
14980 cp_lexer_consume_token (parser->lexer);
14981 /* Remember that it's a `typename' type. */
14982 tag_type = typename_type;
14984 /* Otherwise it must be a class-key. */
14985 else
14987 tag_type = cp_parser_class_key (parser);
14988 if (tag_type == none_type)
14989 return error_mark_node;
14990 /* Parse the attributes. */
14991 attributes = cp_parser_attributes_opt (parser);
14994 /* Look for the `::' operator. */
14995 globalscope = cp_parser_global_scope_opt (parser,
14996 /*current_scope_valid_p=*/false);
14997 /* Look for the nested-name-specifier. */
14998 if (tag_type == typename_type && !globalscope)
15000 if (!cp_parser_nested_name_specifier (parser,
15001 /*typename_keyword_p=*/true,
15002 /*check_dependency_p=*/true,
15003 /*type_p=*/true,
15004 is_declaration))
15005 return error_mark_node;
15007 else
15008 /* Even though `typename' is not present, the proposed resolution
15009 to Core Issue 180 says that in `class A<T>::B', `B' should be
15010 considered a type-name, even if `A<T>' is dependent. */
15011 cp_parser_nested_name_specifier_opt (parser,
15012 /*typename_keyword_p=*/true,
15013 /*check_dependency_p=*/true,
15014 /*type_p=*/true,
15015 is_declaration);
15016 /* For everything but enumeration types, consider a template-id.
15017 For an enumeration type, consider only a plain identifier. */
15018 if (tag_type != enum_type)
15020 bool template_p = false;
15021 tree decl;
15023 /* Allow the `template' keyword. */
15024 template_p = cp_parser_optional_template_keyword (parser);
15025 /* If we didn't see `template', we don't know if there's a
15026 template-id or not. */
15027 if (!template_p)
15028 cp_parser_parse_tentatively (parser);
15029 /* Parse the template-id. */
15030 token = cp_lexer_peek_token (parser->lexer);
15031 decl = cp_parser_template_id (parser, template_p,
15032 /*check_dependency_p=*/true,
15033 tag_type,
15034 is_declaration);
15035 /* If we didn't find a template-id, look for an ordinary
15036 identifier. */
15037 if (!template_p && !cp_parser_parse_definitely (parser))
15039 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15040 in effect, then we must assume that, upon instantiation, the
15041 template will correspond to a class. */
15042 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15043 && tag_type == typename_type)
15044 type = make_typename_type (parser->scope, decl,
15045 typename_type,
15046 /*complain=*/tf_error);
15047 /* If the `typename' keyword is in effect and DECL is not a type
15048 decl, then type is non existent. */
15049 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15051 else if (TREE_CODE (decl) == TYPE_DECL)
15052 type = check_elaborated_type_specifier (tag_type, decl,
15053 /*allow_template_p=*/true);
15054 else if (decl == error_mark_node)
15055 type = error_mark_node;
15058 if (!type)
15060 token = cp_lexer_peek_token (parser->lexer);
15061 identifier = cp_parser_identifier (parser);
15063 if (identifier == error_mark_node)
15065 parser->scope = NULL_TREE;
15066 return error_mark_node;
15069 /* For a `typename', we needn't call xref_tag. */
15070 if (tag_type == typename_type
15071 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15072 return cp_parser_make_typename_type (parser, parser->scope,
15073 identifier,
15074 token->location);
15075 /* Look up a qualified name in the usual way. */
15076 if (parser->scope)
15078 tree decl;
15079 tree ambiguous_decls;
15081 decl = cp_parser_lookup_name (parser, identifier,
15082 tag_type,
15083 /*is_template=*/false,
15084 /*is_namespace=*/false,
15085 /*check_dependency=*/true,
15086 &ambiguous_decls,
15087 token->location);
15089 /* If the lookup was ambiguous, an error will already have been
15090 issued. */
15091 if (ambiguous_decls)
15092 return error_mark_node;
15094 /* If we are parsing friend declaration, DECL may be a
15095 TEMPLATE_DECL tree node here. However, we need to check
15096 whether this TEMPLATE_DECL results in valid code. Consider
15097 the following example:
15099 namespace N {
15100 template <class T> class C {};
15102 class X {
15103 template <class T> friend class N::C; // #1, valid code
15105 template <class T> class Y {
15106 friend class N::C; // #2, invalid code
15109 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15110 name lookup of `N::C'. We see that friend declaration must
15111 be template for the code to be valid. Note that
15112 processing_template_decl does not work here since it is
15113 always 1 for the above two cases. */
15115 decl = (cp_parser_maybe_treat_template_as_class
15116 (decl, /*tag_name_p=*/is_friend
15117 && parser->num_template_parameter_lists));
15119 if (TREE_CODE (decl) != TYPE_DECL)
15121 cp_parser_diagnose_invalid_type_name (parser,
15122 parser->scope,
15123 identifier,
15124 token->location);
15125 return error_mark_node;
15128 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15130 bool allow_template = (parser->num_template_parameter_lists
15131 || DECL_SELF_REFERENCE_P (decl));
15132 type = check_elaborated_type_specifier (tag_type, decl,
15133 allow_template);
15135 if (type == error_mark_node)
15136 return error_mark_node;
15139 /* Forward declarations of nested types, such as
15141 class C1::C2;
15142 class C1::C2::C3;
15144 are invalid unless all components preceding the final '::'
15145 are complete. If all enclosing types are complete, these
15146 declarations become merely pointless.
15148 Invalid forward declarations of nested types are errors
15149 caught elsewhere in parsing. Those that are pointless arrive
15150 here. */
15152 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15153 && !is_friend && !processing_explicit_instantiation)
15154 warning (0, "declaration %qD does not declare anything", decl);
15156 type = TREE_TYPE (decl);
15158 else
15160 /* An elaborated-type-specifier sometimes introduces a new type and
15161 sometimes names an existing type. Normally, the rule is that it
15162 introduces a new type only if there is not an existing type of
15163 the same name already in scope. For example, given:
15165 struct S {};
15166 void f() { struct S s; }
15168 the `struct S' in the body of `f' is the same `struct S' as in
15169 the global scope; the existing definition is used. However, if
15170 there were no global declaration, this would introduce a new
15171 local class named `S'.
15173 An exception to this rule applies to the following code:
15175 namespace N { struct S; }
15177 Here, the elaborated-type-specifier names a new type
15178 unconditionally; even if there is already an `S' in the
15179 containing scope this declaration names a new type.
15180 This exception only applies if the elaborated-type-specifier
15181 forms the complete declaration:
15183 [class.name]
15185 A declaration consisting solely of `class-key identifier ;' is
15186 either a redeclaration of the name in the current scope or a
15187 forward declaration of the identifier as a class name. It
15188 introduces the name into the current scope.
15190 We are in this situation precisely when the next token is a `;'.
15192 An exception to the exception is that a `friend' declaration does
15193 *not* name a new type; i.e., given:
15195 struct S { friend struct T; };
15197 `T' is not a new type in the scope of `S'.
15199 Also, `new struct S' or `sizeof (struct S)' never results in the
15200 definition of a new type; a new type can only be declared in a
15201 declaration context. */
15203 tag_scope ts;
15204 bool template_p;
15206 if (is_friend)
15207 /* Friends have special name lookup rules. */
15208 ts = ts_within_enclosing_non_class;
15209 else if (is_declaration
15210 && cp_lexer_next_token_is (parser->lexer,
15211 CPP_SEMICOLON))
15212 /* This is a `class-key identifier ;' */
15213 ts = ts_current;
15214 else
15215 ts = ts_global;
15217 template_p =
15218 (parser->num_template_parameter_lists
15219 && (cp_parser_next_token_starts_class_definition_p (parser)
15220 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15221 /* An unqualified name was used to reference this type, so
15222 there were no qualifying templates. */
15223 if (!cp_parser_check_template_parameters (parser,
15224 /*num_templates=*/0,
15225 token->location,
15226 /*declarator=*/NULL))
15227 return error_mark_node;
15228 type = xref_tag (tag_type, identifier, ts, template_p);
15232 if (type == error_mark_node)
15233 return error_mark_node;
15235 /* Allow attributes on forward declarations of classes. */
15236 if (attributes)
15238 if (TREE_CODE (type) == TYPENAME_TYPE)
15239 warning (OPT_Wattributes,
15240 "attributes ignored on uninstantiated type");
15241 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15242 && ! processing_explicit_instantiation)
15243 warning (OPT_Wattributes,
15244 "attributes ignored on template instantiation");
15245 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15246 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15247 else
15248 warning (OPT_Wattributes,
15249 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15252 if (tag_type != enum_type)
15254 /* Indicate whether this class was declared as a `class' or as a
15255 `struct'. */
15256 if (TREE_CODE (type) == RECORD_TYPE)
15257 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15258 cp_parser_check_class_key (tag_type, type);
15261 /* A "<" cannot follow an elaborated type specifier. If that
15262 happens, the user was probably trying to form a template-id. */
15263 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15264 token->location);
15266 return type;
15269 /* Parse an enum-specifier.
15271 enum-specifier:
15272 enum-head { enumerator-list [opt] }
15273 enum-head { enumerator-list , } [C++0x]
15275 enum-head:
15276 enum-key identifier [opt] enum-base [opt]
15277 enum-key nested-name-specifier identifier enum-base [opt]
15279 enum-key:
15280 enum
15281 enum class [C++0x]
15282 enum struct [C++0x]
15284 enum-base: [C++0x]
15285 : type-specifier-seq
15287 opaque-enum-specifier:
15288 enum-key identifier enum-base [opt] ;
15290 GNU Extensions:
15291 enum-key attributes[opt] identifier [opt] enum-base [opt]
15292 { enumerator-list [opt] }attributes[opt]
15293 enum-key attributes[opt] identifier [opt] enum-base [opt]
15294 { enumerator-list, }attributes[opt] [C++0x]
15296 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15297 if the token stream isn't an enum-specifier after all. */
15299 static tree
15300 cp_parser_enum_specifier (cp_parser* parser)
15302 tree identifier;
15303 tree type = NULL_TREE;
15304 tree prev_scope;
15305 tree nested_name_specifier = NULL_TREE;
15306 tree attributes;
15307 bool scoped_enum_p = false;
15308 bool has_underlying_type = false;
15309 bool nested_being_defined = false;
15310 bool new_value_list = false;
15311 bool is_new_type = false;
15312 bool is_anonymous = false;
15313 tree underlying_type = NULL_TREE;
15314 cp_token *type_start_token = NULL;
15315 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15317 parser->colon_corrects_to_scope_p = false;
15319 /* Parse tentatively so that we can back up if we don't find a
15320 enum-specifier. */
15321 cp_parser_parse_tentatively (parser);
15323 /* Caller guarantees that the current token is 'enum', an identifier
15324 possibly follows, and the token after that is an opening brace.
15325 If we don't have an identifier, fabricate an anonymous name for
15326 the enumeration being defined. */
15327 cp_lexer_consume_token (parser->lexer);
15329 /* Parse the "class" or "struct", which indicates a scoped
15330 enumeration type in C++0x. */
15331 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15332 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15334 if (cxx_dialect < cxx11)
15335 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15337 /* Consume the `struct' or `class' token. */
15338 cp_lexer_consume_token (parser->lexer);
15340 scoped_enum_p = true;
15343 attributes = cp_parser_attributes_opt (parser);
15345 /* Clear the qualification. */
15346 parser->scope = NULL_TREE;
15347 parser->qualifying_scope = NULL_TREE;
15348 parser->object_scope = NULL_TREE;
15350 /* Figure out in what scope the declaration is being placed. */
15351 prev_scope = current_scope ();
15353 type_start_token = cp_lexer_peek_token (parser->lexer);
15355 push_deferring_access_checks (dk_no_check);
15356 nested_name_specifier
15357 = cp_parser_nested_name_specifier_opt (parser,
15358 /*typename_keyword_p=*/true,
15359 /*check_dependency_p=*/false,
15360 /*type_p=*/false,
15361 /*is_declaration=*/false);
15363 if (nested_name_specifier)
15365 tree name;
15367 identifier = cp_parser_identifier (parser);
15368 name = cp_parser_lookup_name (parser, identifier,
15369 enum_type,
15370 /*is_template=*/false,
15371 /*is_namespace=*/false,
15372 /*check_dependency=*/true,
15373 /*ambiguous_decls=*/NULL,
15374 input_location);
15375 if (name && name != error_mark_node)
15377 type = TREE_TYPE (name);
15378 if (TREE_CODE (type) == TYPENAME_TYPE)
15380 /* Are template enums allowed in ISO? */
15381 if (template_parm_scope_p ())
15382 pedwarn (type_start_token->location, OPT_Wpedantic,
15383 "%qD is an enumeration template", name);
15384 /* ignore a typename reference, for it will be solved by name
15385 in start_enum. */
15386 type = NULL_TREE;
15389 else if (nested_name_specifier == error_mark_node)
15390 /* We already issued an error. */;
15391 else
15392 error_at (type_start_token->location,
15393 "%qD is not an enumerator-name", identifier);
15395 else
15397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15398 identifier = cp_parser_identifier (parser);
15399 else
15401 identifier = make_anon_name ();
15402 is_anonymous = true;
15403 if (scoped_enum_p)
15404 error_at (type_start_token->location,
15405 "anonymous scoped enum is not allowed");
15408 pop_deferring_access_checks ();
15410 /* Check for the `:' that denotes a specified underlying type in C++0x.
15411 Note that a ':' could also indicate a bitfield width, however. */
15412 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15414 cp_decl_specifier_seq type_specifiers;
15416 /* Consume the `:'. */
15417 cp_lexer_consume_token (parser->lexer);
15419 /* Parse the type-specifier-seq. */
15420 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15421 /*is_trailing_return=*/false,
15422 &type_specifiers);
15424 /* At this point this is surely not elaborated type specifier. */
15425 if (!cp_parser_parse_definitely (parser))
15426 return NULL_TREE;
15428 if (cxx_dialect < cxx11)
15429 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15431 has_underlying_type = true;
15433 /* If that didn't work, stop. */
15434 if (type_specifiers.type != error_mark_node)
15436 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15437 /*initialized=*/0, NULL);
15438 if (underlying_type == error_mark_node
15439 || check_for_bare_parameter_packs (underlying_type))
15440 underlying_type = NULL_TREE;
15444 /* Look for the `{' but don't consume it yet. */
15445 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15447 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15449 cp_parser_error (parser, "expected %<{%>");
15450 if (has_underlying_type)
15452 type = NULL_TREE;
15453 goto out;
15456 /* An opaque-enum-specifier must have a ';' here. */
15457 if ((scoped_enum_p || underlying_type)
15458 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15460 cp_parser_error (parser, "expected %<;%> or %<{%>");
15461 if (has_underlying_type)
15463 type = NULL_TREE;
15464 goto out;
15469 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15470 return NULL_TREE;
15472 if (nested_name_specifier)
15474 if (CLASS_TYPE_P (nested_name_specifier))
15476 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15477 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15478 push_scope (nested_name_specifier);
15480 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15482 push_nested_namespace (nested_name_specifier);
15486 /* Issue an error message if type-definitions are forbidden here. */
15487 if (!cp_parser_check_type_definition (parser))
15488 type = error_mark_node;
15489 else
15490 /* Create the new type. We do this before consuming the opening
15491 brace so the enum will be recorded as being on the line of its
15492 tag (or the 'enum' keyword, if there is no tag). */
15493 type = start_enum (identifier, type, underlying_type,
15494 scoped_enum_p, &is_new_type);
15496 /* If the next token is not '{' it is an opaque-enum-specifier or an
15497 elaborated-type-specifier. */
15498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15500 timevar_push (TV_PARSE_ENUM);
15501 if (nested_name_specifier
15502 && nested_name_specifier != error_mark_node)
15504 /* The following catches invalid code such as:
15505 enum class S<int>::E { A, B, C }; */
15506 if (!processing_specialization
15507 && CLASS_TYPE_P (nested_name_specifier)
15508 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15509 error_at (type_start_token->location, "cannot add an enumerator "
15510 "list to a template instantiation");
15512 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15514 error_at (type_start_token->location,
15515 "%<%T::%E%> has not been declared",
15516 TYPE_CONTEXT (nested_name_specifier),
15517 nested_name_specifier);
15518 type = error_mark_node;
15520 /* If that scope does not contain the scope in which the
15521 class was originally declared, the program is invalid. */
15522 else if (prev_scope && !is_ancestor (prev_scope,
15523 nested_name_specifier))
15525 if (at_namespace_scope_p ())
15526 error_at (type_start_token->location,
15527 "declaration of %qD in namespace %qD which does not "
15528 "enclose %qD",
15529 type, prev_scope, nested_name_specifier);
15530 else
15531 error_at (type_start_token->location,
15532 "declaration of %qD in %qD which does not "
15533 "enclose %qD",
15534 type, prev_scope, nested_name_specifier);
15535 type = error_mark_node;
15539 if (scoped_enum_p)
15540 begin_scope (sk_scoped_enum, type);
15542 /* Consume the opening brace. */
15543 cp_lexer_consume_token (parser->lexer);
15545 if (type == error_mark_node)
15546 ; /* Nothing to add */
15547 else if (OPAQUE_ENUM_P (type)
15548 || (cxx_dialect > cxx98 && processing_specialization))
15550 new_value_list = true;
15551 SET_OPAQUE_ENUM_P (type, false);
15552 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15554 else
15556 error_at (type_start_token->location,
15557 "multiple definition of %q#T", type);
15558 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15559 "previous definition here");
15560 type = error_mark_node;
15563 if (type == error_mark_node)
15564 cp_parser_skip_to_end_of_block_or_statement (parser);
15565 /* If the next token is not '}', then there are some enumerators. */
15566 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15568 if (is_anonymous && !scoped_enum_p)
15569 pedwarn (type_start_token->location, OPT_Wpedantic,
15570 "ISO C++ forbids empty anonymous enum");
15572 else
15573 cp_parser_enumerator_list (parser, type);
15575 /* Consume the final '}'. */
15576 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15578 if (scoped_enum_p)
15579 finish_scope ();
15580 timevar_pop (TV_PARSE_ENUM);
15582 else
15584 /* If a ';' follows, then it is an opaque-enum-specifier
15585 and additional restrictions apply. */
15586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15588 if (is_anonymous)
15589 error_at (type_start_token->location,
15590 "opaque-enum-specifier without name");
15591 else if (nested_name_specifier)
15592 error_at (type_start_token->location,
15593 "opaque-enum-specifier must use a simple identifier");
15597 /* Look for trailing attributes to apply to this enumeration, and
15598 apply them if appropriate. */
15599 if (cp_parser_allow_gnu_extensions_p (parser))
15601 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15602 trailing_attr = chainon (trailing_attr, attributes);
15603 cplus_decl_attributes (&type,
15604 trailing_attr,
15605 (int) ATTR_FLAG_TYPE_IN_PLACE);
15608 /* Finish up the enumeration. */
15609 if (type != error_mark_node)
15611 if (new_value_list)
15612 finish_enum_value_list (type);
15613 if (is_new_type)
15614 finish_enum (type);
15617 if (nested_name_specifier)
15619 if (CLASS_TYPE_P (nested_name_specifier))
15621 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15622 pop_scope (nested_name_specifier);
15624 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15626 pop_nested_namespace (nested_name_specifier);
15629 out:
15630 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15631 return type;
15634 /* Parse an enumerator-list. The enumerators all have the indicated
15635 TYPE.
15637 enumerator-list:
15638 enumerator-definition
15639 enumerator-list , enumerator-definition */
15641 static void
15642 cp_parser_enumerator_list (cp_parser* parser, tree type)
15644 while (true)
15646 /* Parse an enumerator-definition. */
15647 cp_parser_enumerator_definition (parser, type);
15649 /* If the next token is not a ',', we've reached the end of
15650 the list. */
15651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15652 break;
15653 /* Otherwise, consume the `,' and keep going. */
15654 cp_lexer_consume_token (parser->lexer);
15655 /* If the next token is a `}', there is a trailing comma. */
15656 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15658 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15659 pedwarn (input_location, OPT_Wpedantic,
15660 "comma at end of enumerator list");
15661 break;
15666 /* Parse an enumerator-definition. The enumerator has the indicated
15667 TYPE.
15669 enumerator-definition:
15670 enumerator
15671 enumerator = constant-expression
15673 enumerator:
15674 identifier */
15676 static void
15677 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15679 tree identifier;
15680 tree value;
15681 location_t loc;
15683 /* Save the input location because we are interested in the location
15684 of the identifier and not the location of the explicit value. */
15685 loc = cp_lexer_peek_token (parser->lexer)->location;
15687 /* Look for the identifier. */
15688 identifier = cp_parser_identifier (parser);
15689 if (identifier == error_mark_node)
15690 return;
15692 /* If the next token is an '=', then there is an explicit value. */
15693 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15695 /* Consume the `=' token. */
15696 cp_lexer_consume_token (parser->lexer);
15697 /* Parse the value. */
15698 value = cp_parser_constant_expression (parser,
15699 /*allow_non_constant_p=*/false,
15700 NULL);
15702 else
15703 value = NULL_TREE;
15705 /* If we are processing a template, make sure the initializer of the
15706 enumerator doesn't contain any bare template parameter pack. */
15707 if (check_for_bare_parameter_packs (value))
15708 value = error_mark_node;
15710 /* integral_constant_value will pull out this expression, so make sure
15711 it's folded as appropriate. */
15712 value = fold_non_dependent_expr (value);
15714 /* Create the enumerator. */
15715 build_enumerator (identifier, value, type, loc);
15718 /* Parse a namespace-name.
15720 namespace-name:
15721 original-namespace-name
15722 namespace-alias
15724 Returns the NAMESPACE_DECL for the namespace. */
15726 static tree
15727 cp_parser_namespace_name (cp_parser* parser)
15729 tree identifier;
15730 tree namespace_decl;
15732 cp_token *token = cp_lexer_peek_token (parser->lexer);
15734 /* Get the name of the namespace. */
15735 identifier = cp_parser_identifier (parser);
15736 if (identifier == error_mark_node)
15737 return error_mark_node;
15739 /* Look up the identifier in the currently active scope. Look only
15740 for namespaces, due to:
15742 [basic.lookup.udir]
15744 When looking up a namespace-name in a using-directive or alias
15745 definition, only namespace names are considered.
15747 And:
15749 [basic.lookup.qual]
15751 During the lookup of a name preceding the :: scope resolution
15752 operator, object, function, and enumerator names are ignored.
15754 (Note that cp_parser_qualifying_entity only calls this
15755 function if the token after the name is the scope resolution
15756 operator.) */
15757 namespace_decl = cp_parser_lookup_name (parser, identifier,
15758 none_type,
15759 /*is_template=*/false,
15760 /*is_namespace=*/true,
15761 /*check_dependency=*/true,
15762 /*ambiguous_decls=*/NULL,
15763 token->location);
15764 /* If it's not a namespace, issue an error. */
15765 if (namespace_decl == error_mark_node
15766 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15768 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15769 error_at (token->location, "%qD is not a namespace-name", identifier);
15770 cp_parser_error (parser, "expected namespace-name");
15771 namespace_decl = error_mark_node;
15774 return namespace_decl;
15777 /* Parse a namespace-definition.
15779 namespace-definition:
15780 named-namespace-definition
15781 unnamed-namespace-definition
15783 named-namespace-definition:
15784 original-namespace-definition
15785 extension-namespace-definition
15787 original-namespace-definition:
15788 namespace identifier { namespace-body }
15790 extension-namespace-definition:
15791 namespace original-namespace-name { namespace-body }
15793 unnamed-namespace-definition:
15794 namespace { namespace-body } */
15796 static void
15797 cp_parser_namespace_definition (cp_parser* parser)
15799 tree identifier, attribs;
15800 bool has_visibility;
15801 bool is_inline;
15803 cp_ensure_no_omp_declare_simd (parser);
15804 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15806 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15807 is_inline = true;
15808 cp_lexer_consume_token (parser->lexer);
15810 else
15811 is_inline = false;
15813 /* Look for the `namespace' keyword. */
15814 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15816 /* Get the name of the namespace. We do not attempt to distinguish
15817 between an original-namespace-definition and an
15818 extension-namespace-definition at this point. The semantic
15819 analysis routines are responsible for that. */
15820 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15821 identifier = cp_parser_identifier (parser);
15822 else
15823 identifier = NULL_TREE;
15825 /* Parse any specified attributes. */
15826 attribs = cp_parser_attributes_opt (parser);
15828 /* Look for the `{' to start the namespace. */
15829 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15830 /* Start the namespace. */
15831 push_namespace (identifier);
15833 /* "inline namespace" is equivalent to a stub namespace definition
15834 followed by a strong using directive. */
15835 if (is_inline)
15837 tree name_space = current_namespace;
15838 /* Set up namespace association. */
15839 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15840 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15841 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15842 /* Import the contents of the inline namespace. */
15843 pop_namespace ();
15844 do_using_directive (name_space);
15845 push_namespace (identifier);
15848 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15850 /* Parse the body of the namespace. */
15851 cp_parser_namespace_body (parser);
15853 if (has_visibility)
15854 pop_visibility (1);
15856 /* Finish the namespace. */
15857 pop_namespace ();
15858 /* Look for the final `}'. */
15859 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15862 /* Parse a namespace-body.
15864 namespace-body:
15865 declaration-seq [opt] */
15867 static void
15868 cp_parser_namespace_body (cp_parser* parser)
15870 cp_parser_declaration_seq_opt (parser);
15873 /* Parse a namespace-alias-definition.
15875 namespace-alias-definition:
15876 namespace identifier = qualified-namespace-specifier ; */
15878 static void
15879 cp_parser_namespace_alias_definition (cp_parser* parser)
15881 tree identifier;
15882 tree namespace_specifier;
15884 cp_token *token = cp_lexer_peek_token (parser->lexer);
15886 /* Look for the `namespace' keyword. */
15887 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15888 /* Look for the identifier. */
15889 identifier = cp_parser_identifier (parser);
15890 if (identifier == error_mark_node)
15891 return;
15892 /* Look for the `=' token. */
15893 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15894 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15896 error_at (token->location, "%<namespace%> definition is not allowed here");
15897 /* Skip the definition. */
15898 cp_lexer_consume_token (parser->lexer);
15899 if (cp_parser_skip_to_closing_brace (parser))
15900 cp_lexer_consume_token (parser->lexer);
15901 return;
15903 cp_parser_require (parser, CPP_EQ, RT_EQ);
15904 /* Look for the qualified-namespace-specifier. */
15905 namespace_specifier
15906 = cp_parser_qualified_namespace_specifier (parser);
15907 /* Look for the `;' token. */
15908 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15910 /* Register the alias in the symbol table. */
15911 do_namespace_alias (identifier, namespace_specifier);
15914 /* Parse a qualified-namespace-specifier.
15916 qualified-namespace-specifier:
15917 :: [opt] nested-name-specifier [opt] namespace-name
15919 Returns a NAMESPACE_DECL corresponding to the specified
15920 namespace. */
15922 static tree
15923 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15925 /* Look for the optional `::'. */
15926 cp_parser_global_scope_opt (parser,
15927 /*current_scope_valid_p=*/false);
15929 /* Look for the optional nested-name-specifier. */
15930 cp_parser_nested_name_specifier_opt (parser,
15931 /*typename_keyword_p=*/false,
15932 /*check_dependency_p=*/true,
15933 /*type_p=*/false,
15934 /*is_declaration=*/true);
15936 return cp_parser_namespace_name (parser);
15939 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15940 access declaration.
15942 using-declaration:
15943 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15944 using :: unqualified-id ;
15946 access-declaration:
15947 qualified-id ;
15951 static bool
15952 cp_parser_using_declaration (cp_parser* parser,
15953 bool access_declaration_p)
15955 cp_token *token;
15956 bool typename_p = false;
15957 bool global_scope_p;
15958 tree decl;
15959 tree identifier;
15960 tree qscope;
15961 int oldcount = errorcount;
15962 cp_token *diag_token = NULL;
15964 if (access_declaration_p)
15966 diag_token = cp_lexer_peek_token (parser->lexer);
15967 cp_parser_parse_tentatively (parser);
15969 else
15971 /* Look for the `using' keyword. */
15972 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15974 /* Peek at the next token. */
15975 token = cp_lexer_peek_token (parser->lexer);
15976 /* See if it's `typename'. */
15977 if (token->keyword == RID_TYPENAME)
15979 /* Remember that we've seen it. */
15980 typename_p = true;
15981 /* Consume the `typename' token. */
15982 cp_lexer_consume_token (parser->lexer);
15986 /* Look for the optional global scope qualification. */
15987 global_scope_p
15988 = (cp_parser_global_scope_opt (parser,
15989 /*current_scope_valid_p=*/false)
15990 != NULL_TREE);
15992 /* If we saw `typename', or didn't see `::', then there must be a
15993 nested-name-specifier present. */
15994 if (typename_p || !global_scope_p)
15996 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15997 /*check_dependency_p=*/true,
15998 /*type_p=*/false,
15999 /*is_declaration=*/true);
16000 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16002 cp_parser_skip_to_end_of_block_or_statement (parser);
16003 return false;
16006 /* Otherwise, we could be in either of the two productions. In that
16007 case, treat the nested-name-specifier as optional. */
16008 else
16009 qscope = cp_parser_nested_name_specifier_opt (parser,
16010 /*typename_keyword_p=*/false,
16011 /*check_dependency_p=*/true,
16012 /*type_p=*/false,
16013 /*is_declaration=*/true);
16014 if (!qscope)
16015 qscope = global_namespace;
16017 if (access_declaration_p && cp_parser_error_occurred (parser))
16018 /* Something has already gone wrong; there's no need to parse
16019 further. Since an error has occurred, the return value of
16020 cp_parser_parse_definitely will be false, as required. */
16021 return cp_parser_parse_definitely (parser);
16023 token = cp_lexer_peek_token (parser->lexer);
16024 /* Parse the unqualified-id. */
16025 identifier = cp_parser_unqualified_id (parser,
16026 /*template_keyword_p=*/false,
16027 /*check_dependency_p=*/true,
16028 /*declarator_p=*/true,
16029 /*optional_p=*/false);
16031 if (access_declaration_p)
16033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16034 cp_parser_simulate_error (parser);
16035 if (!cp_parser_parse_definitely (parser))
16036 return false;
16039 /* The function we call to handle a using-declaration is different
16040 depending on what scope we are in. */
16041 if (qscope == error_mark_node || identifier == error_mark_node)
16043 else if (!identifier_p (identifier)
16044 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16045 /* [namespace.udecl]
16047 A using declaration shall not name a template-id. */
16048 error_at (token->location,
16049 "a template-id may not appear in a using-declaration");
16050 else
16052 if (at_class_scope_p ())
16054 /* Create the USING_DECL. */
16055 decl = do_class_using_decl (parser->scope, identifier);
16057 if (decl && typename_p)
16058 USING_DECL_TYPENAME_P (decl) = 1;
16060 if (check_for_bare_parameter_packs (decl))
16062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16063 return false;
16065 else
16066 /* Add it to the list of members in this class. */
16067 finish_member_declaration (decl);
16069 else
16071 decl = cp_parser_lookup_name_simple (parser,
16072 identifier,
16073 token->location);
16074 if (decl == error_mark_node)
16075 cp_parser_name_lookup_error (parser, identifier,
16076 decl, NLE_NULL,
16077 token->location);
16078 else if (check_for_bare_parameter_packs (decl))
16080 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16081 return false;
16083 else if (!at_namespace_scope_p ())
16084 do_local_using_decl (decl, qscope, identifier);
16085 else
16086 do_toplevel_using_decl (decl, qscope, identifier);
16090 /* Look for the final `;'. */
16091 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16093 if (access_declaration_p && errorcount == oldcount)
16094 warning_at (diag_token->location, OPT_Wdeprecated,
16095 "access declarations are deprecated "
16096 "in favour of using-declarations; "
16097 "suggestion: add the %<using%> keyword");
16099 return true;
16102 /* Parse an alias-declaration.
16104 alias-declaration:
16105 using identifier attribute-specifier-seq [opt] = type-id */
16107 static tree
16108 cp_parser_alias_declaration (cp_parser* parser)
16110 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16111 location_t id_location;
16112 cp_declarator *declarator;
16113 cp_decl_specifier_seq decl_specs;
16114 bool member_p;
16115 const char *saved_message = NULL;
16117 /* Look for the `using' keyword. */
16118 cp_token *using_token
16119 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16120 if (using_token == NULL)
16121 return error_mark_node;
16123 id_location = cp_lexer_peek_token (parser->lexer)->location;
16124 id = cp_parser_identifier (parser);
16125 if (id == error_mark_node)
16126 return error_mark_node;
16128 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16129 attributes = cp_parser_attributes_opt (parser);
16130 if (attributes == error_mark_node)
16131 return error_mark_node;
16133 cp_parser_require (parser, CPP_EQ, RT_EQ);
16135 if (cp_parser_error_occurred (parser))
16136 return error_mark_node;
16138 cp_parser_commit_to_tentative_parse (parser);
16140 /* Now we are going to parse the type-id of the declaration. */
16143 [dcl.type]/3 says:
16145 "A type-specifier-seq shall not define a class or enumeration
16146 unless it appears in the type-id of an alias-declaration (7.1.3) that
16147 is not the declaration of a template-declaration."
16149 In other words, if we currently are in an alias template, the
16150 type-id should not define a type.
16152 So let's set parser->type_definition_forbidden_message in that
16153 case; cp_parser_check_type_definition (called by
16154 cp_parser_class_specifier) will then emit an error if a type is
16155 defined in the type-id. */
16156 if (parser->num_template_parameter_lists)
16158 saved_message = parser->type_definition_forbidden_message;
16159 parser->type_definition_forbidden_message =
16160 G_("types may not be defined in alias template declarations");
16163 type = cp_parser_type_id (parser);
16165 /* Restore the error message if need be. */
16166 if (parser->num_template_parameter_lists)
16167 parser->type_definition_forbidden_message = saved_message;
16169 if (type == error_mark_node
16170 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16172 cp_parser_skip_to_end_of_block_or_statement (parser);
16173 return error_mark_node;
16176 /* A typedef-name can also be introduced by an alias-declaration. The
16177 identifier following the using keyword becomes a typedef-name. It has
16178 the same semantics as if it were introduced by the typedef
16179 specifier. In particular, it does not define a new type and it shall
16180 not appear in the type-id. */
16182 clear_decl_specs (&decl_specs);
16183 decl_specs.type = type;
16184 if (attributes != NULL_TREE)
16186 decl_specs.attributes = attributes;
16187 set_and_check_decl_spec_loc (&decl_specs,
16188 ds_attribute,
16189 attrs_token);
16191 set_and_check_decl_spec_loc (&decl_specs,
16192 ds_typedef,
16193 using_token);
16194 set_and_check_decl_spec_loc (&decl_specs,
16195 ds_alias,
16196 using_token);
16198 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16199 declarator->id_loc = id_location;
16201 member_p = at_class_scope_p ();
16202 if (member_p)
16203 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16204 NULL_TREE, attributes);
16205 else
16206 decl = start_decl (declarator, &decl_specs, 0,
16207 attributes, NULL_TREE, &pushed_scope);
16208 if (decl == error_mark_node)
16209 return decl;
16211 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16213 if (pushed_scope)
16214 pop_scope (pushed_scope);
16216 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16217 added into the symbol table; otherwise, return the TYPE_DECL. */
16218 if (DECL_LANG_SPECIFIC (decl)
16219 && DECL_TEMPLATE_INFO (decl)
16220 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16222 decl = DECL_TI_TEMPLATE (decl);
16223 if (member_p)
16224 check_member_template (decl);
16227 return decl;
16230 /* Parse a using-directive.
16232 using-directive:
16233 using namespace :: [opt] nested-name-specifier [opt]
16234 namespace-name ; */
16236 static void
16237 cp_parser_using_directive (cp_parser* parser)
16239 tree namespace_decl;
16240 tree attribs;
16242 /* Look for the `using' keyword. */
16243 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16244 /* And the `namespace' keyword. */
16245 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16246 /* Look for the optional `::' operator. */
16247 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16248 /* And the optional nested-name-specifier. */
16249 cp_parser_nested_name_specifier_opt (parser,
16250 /*typename_keyword_p=*/false,
16251 /*check_dependency_p=*/true,
16252 /*type_p=*/false,
16253 /*is_declaration=*/true);
16254 /* Get the namespace being used. */
16255 namespace_decl = cp_parser_namespace_name (parser);
16256 /* And any specified attributes. */
16257 attribs = cp_parser_attributes_opt (parser);
16258 /* Update the symbol table. */
16259 parse_using_directive (namespace_decl, attribs);
16260 /* Look for the final `;'. */
16261 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16264 /* Parse an asm-definition.
16266 asm-definition:
16267 asm ( string-literal ) ;
16269 GNU Extension:
16271 asm-definition:
16272 asm volatile [opt] ( string-literal ) ;
16273 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16274 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16275 : asm-operand-list [opt] ) ;
16276 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16277 : asm-operand-list [opt]
16278 : asm-clobber-list [opt] ) ;
16279 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16280 : asm-clobber-list [opt]
16281 : asm-goto-list ) ; */
16283 static void
16284 cp_parser_asm_definition (cp_parser* parser)
16286 tree string;
16287 tree outputs = NULL_TREE;
16288 tree inputs = NULL_TREE;
16289 tree clobbers = NULL_TREE;
16290 tree labels = NULL_TREE;
16291 tree asm_stmt;
16292 bool volatile_p = false;
16293 bool extended_p = false;
16294 bool invalid_inputs_p = false;
16295 bool invalid_outputs_p = false;
16296 bool goto_p = false;
16297 required_token missing = RT_NONE;
16299 /* Look for the `asm' keyword. */
16300 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16301 /* See if the next token is `volatile'. */
16302 if (cp_parser_allow_gnu_extensions_p (parser)
16303 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16305 /* Remember that we saw the `volatile' keyword. */
16306 volatile_p = true;
16307 /* Consume the token. */
16308 cp_lexer_consume_token (parser->lexer);
16310 if (cp_parser_allow_gnu_extensions_p (parser)
16311 && parser->in_function_body
16312 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16314 /* Remember that we saw the `goto' keyword. */
16315 goto_p = true;
16316 /* Consume the token. */
16317 cp_lexer_consume_token (parser->lexer);
16319 /* Look for the opening `('. */
16320 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16321 return;
16322 /* Look for the string. */
16323 string = cp_parser_string_literal (parser, false, false);
16324 if (string == error_mark_node)
16326 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16327 /*consume_paren=*/true);
16328 return;
16331 /* If we're allowing GNU extensions, check for the extended assembly
16332 syntax. Unfortunately, the `:' tokens need not be separated by
16333 a space in C, and so, for compatibility, we tolerate that here
16334 too. Doing that means that we have to treat the `::' operator as
16335 two `:' tokens. */
16336 if (cp_parser_allow_gnu_extensions_p (parser)
16337 && parser->in_function_body
16338 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16339 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16341 bool inputs_p = false;
16342 bool clobbers_p = false;
16343 bool labels_p = false;
16345 /* The extended syntax was used. */
16346 extended_p = true;
16348 /* Look for outputs. */
16349 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16351 /* Consume the `:'. */
16352 cp_lexer_consume_token (parser->lexer);
16353 /* Parse the output-operands. */
16354 if (cp_lexer_next_token_is_not (parser->lexer,
16355 CPP_COLON)
16356 && cp_lexer_next_token_is_not (parser->lexer,
16357 CPP_SCOPE)
16358 && cp_lexer_next_token_is_not (parser->lexer,
16359 CPP_CLOSE_PAREN)
16360 && !goto_p)
16361 outputs = cp_parser_asm_operand_list (parser);
16363 if (outputs == error_mark_node)
16364 invalid_outputs_p = true;
16366 /* If the next token is `::', there are no outputs, and the
16367 next token is the beginning of the inputs. */
16368 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16369 /* The inputs are coming next. */
16370 inputs_p = true;
16372 /* Look for inputs. */
16373 if (inputs_p
16374 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16376 /* Consume the `:' or `::'. */
16377 cp_lexer_consume_token (parser->lexer);
16378 /* Parse the output-operands. */
16379 if (cp_lexer_next_token_is_not (parser->lexer,
16380 CPP_COLON)
16381 && cp_lexer_next_token_is_not (parser->lexer,
16382 CPP_SCOPE)
16383 && cp_lexer_next_token_is_not (parser->lexer,
16384 CPP_CLOSE_PAREN))
16385 inputs = cp_parser_asm_operand_list (parser);
16387 if (inputs == error_mark_node)
16388 invalid_inputs_p = true;
16390 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16391 /* The clobbers are coming next. */
16392 clobbers_p = true;
16394 /* Look for clobbers. */
16395 if (clobbers_p
16396 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16398 clobbers_p = true;
16399 /* Consume the `:' or `::'. */
16400 cp_lexer_consume_token (parser->lexer);
16401 /* Parse the clobbers. */
16402 if (cp_lexer_next_token_is_not (parser->lexer,
16403 CPP_COLON)
16404 && cp_lexer_next_token_is_not (parser->lexer,
16405 CPP_CLOSE_PAREN))
16406 clobbers = cp_parser_asm_clobber_list (parser);
16408 else if (goto_p
16409 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16410 /* The labels are coming next. */
16411 labels_p = true;
16413 /* Look for labels. */
16414 if (labels_p
16415 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16417 labels_p = true;
16418 /* Consume the `:' or `::'. */
16419 cp_lexer_consume_token (parser->lexer);
16420 /* Parse the labels. */
16421 labels = cp_parser_asm_label_list (parser);
16424 if (goto_p && !labels_p)
16425 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16427 else if (goto_p)
16428 missing = RT_COLON_SCOPE;
16430 /* Look for the closing `)'. */
16431 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16432 missing ? missing : RT_CLOSE_PAREN))
16433 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16434 /*consume_paren=*/true);
16435 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16437 if (!invalid_inputs_p && !invalid_outputs_p)
16439 /* Create the ASM_EXPR. */
16440 if (parser->in_function_body)
16442 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16443 inputs, clobbers, labels);
16444 /* If the extended syntax was not used, mark the ASM_EXPR. */
16445 if (!extended_p)
16447 tree temp = asm_stmt;
16448 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16449 temp = TREE_OPERAND (temp, 0);
16451 ASM_INPUT_P (temp) = 1;
16454 else
16455 add_asm_node (string);
16459 /* Declarators [gram.dcl.decl] */
16461 /* Parse an init-declarator.
16463 init-declarator:
16464 declarator initializer [opt]
16466 GNU Extension:
16468 init-declarator:
16469 declarator asm-specification [opt] attributes [opt] initializer [opt]
16471 function-definition:
16472 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16473 function-body
16474 decl-specifier-seq [opt] declarator function-try-block
16476 GNU Extension:
16478 function-definition:
16479 __extension__ function-definition
16481 TM Extension:
16483 function-definition:
16484 decl-specifier-seq [opt] declarator function-transaction-block
16486 The DECL_SPECIFIERS apply to this declarator. Returns a
16487 representation of the entity declared. If MEMBER_P is TRUE, then
16488 this declarator appears in a class scope. The new DECL created by
16489 this declarator is returned.
16491 The CHECKS are access checks that should be performed once we know
16492 what entity is being declared (and, therefore, what classes have
16493 befriended it).
16495 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16496 for a function-definition here as well. If the declarator is a
16497 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16498 be TRUE upon return. By that point, the function-definition will
16499 have been completely parsed.
16501 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16502 is FALSE.
16504 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16505 parsed declaration if it is an uninitialized single declarator not followed
16506 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16507 if present, will not be consumed. If returned, this declarator will be
16508 created with SD_INITIALIZED but will not call cp_finish_decl. */
16510 static tree
16511 cp_parser_init_declarator (cp_parser* parser,
16512 cp_decl_specifier_seq *decl_specifiers,
16513 vec<deferred_access_check, va_gc> *checks,
16514 bool function_definition_allowed_p,
16515 bool member_p,
16516 int declares_class_or_enum,
16517 bool* function_definition_p,
16518 tree* maybe_range_for_decl)
16520 cp_token *token = NULL, *asm_spec_start_token = NULL,
16521 *attributes_start_token = NULL;
16522 cp_declarator *declarator;
16523 tree prefix_attributes;
16524 tree attributes = NULL;
16525 tree asm_specification;
16526 tree initializer;
16527 tree decl = NULL_TREE;
16528 tree scope;
16529 int is_initialized;
16530 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16531 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16532 "(...)". */
16533 enum cpp_ttype initialization_kind;
16534 bool is_direct_init = false;
16535 bool is_non_constant_init;
16536 int ctor_dtor_or_conv_p;
16537 bool friend_p;
16538 tree pushed_scope = NULL_TREE;
16539 bool range_for_decl_p = false;
16540 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16542 /* Gather the attributes that were provided with the
16543 decl-specifiers. */
16544 prefix_attributes = decl_specifiers->attributes;
16546 /* Assume that this is not the declarator for a function
16547 definition. */
16548 if (function_definition_p)
16549 *function_definition_p = false;
16551 /* Default arguments are only permitted for function parameters. */
16552 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16553 parser->default_arg_ok_p = false;
16555 /* Defer access checks while parsing the declarator; we cannot know
16556 what names are accessible until we know what is being
16557 declared. */
16558 resume_deferring_access_checks ();
16560 /* Parse the declarator. */
16561 token = cp_lexer_peek_token (parser->lexer);
16562 declarator
16563 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16564 &ctor_dtor_or_conv_p,
16565 /*parenthesized_p=*/NULL,
16566 member_p);
16567 /* Gather up the deferred checks. */
16568 stop_deferring_access_checks ();
16570 parser->default_arg_ok_p = saved_default_arg_ok_p;
16572 /* If the DECLARATOR was erroneous, there's no need to go
16573 further. */
16574 if (declarator == cp_error_declarator)
16575 return error_mark_node;
16577 /* Check that the number of template-parameter-lists is OK. */
16578 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16579 token->location))
16580 return error_mark_node;
16582 if (declares_class_or_enum & 2)
16583 cp_parser_check_for_definition_in_return_type (declarator,
16584 decl_specifiers->type,
16585 decl_specifiers->locations[ds_type_spec]);
16587 /* Figure out what scope the entity declared by the DECLARATOR is
16588 located in. `grokdeclarator' sometimes changes the scope, so
16589 we compute it now. */
16590 scope = get_scope_of_declarator (declarator);
16592 /* Perform any lookups in the declared type which were thought to be
16593 dependent, but are not in the scope of the declarator. */
16594 decl_specifiers->type
16595 = maybe_update_decl_type (decl_specifiers->type, scope);
16597 /* If we're allowing GNU extensions, look for an
16598 asm-specification. */
16599 if (cp_parser_allow_gnu_extensions_p (parser))
16601 /* Look for an asm-specification. */
16602 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16603 asm_specification = cp_parser_asm_specification_opt (parser);
16605 else
16606 asm_specification = NULL_TREE;
16608 /* Look for attributes. */
16609 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16610 attributes = cp_parser_attributes_opt (parser);
16612 /* Peek at the next token. */
16613 token = cp_lexer_peek_token (parser->lexer);
16615 if (function_declarator_p (declarator))
16617 /* Check to see if the token indicates the start of a
16618 function-definition. */
16619 if (cp_parser_token_starts_function_definition_p (token))
16621 if (!function_definition_allowed_p)
16623 /* If a function-definition should not appear here, issue an
16624 error message. */
16625 cp_parser_error (parser,
16626 "a function-definition is not allowed here");
16627 return error_mark_node;
16630 location_t func_brace_location
16631 = cp_lexer_peek_token (parser->lexer)->location;
16633 /* Neither attributes nor an asm-specification are allowed
16634 on a function-definition. */
16635 if (asm_specification)
16636 error_at (asm_spec_start_token->location,
16637 "an asm-specification is not allowed "
16638 "on a function-definition");
16639 if (attributes)
16640 error_at (attributes_start_token->location,
16641 "attributes are not allowed "
16642 "on a function-definition");
16643 /* This is a function-definition. */
16644 *function_definition_p = true;
16646 /* Parse the function definition. */
16647 if (member_p)
16648 decl = cp_parser_save_member_function_body (parser,
16649 decl_specifiers,
16650 declarator,
16651 prefix_attributes);
16652 else
16653 decl =
16654 (cp_parser_function_definition_from_specifiers_and_declarator
16655 (parser, decl_specifiers, prefix_attributes, declarator));
16657 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16659 /* This is where the prologue starts... */
16660 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16661 = func_brace_location;
16664 return decl;
16668 /* [dcl.dcl]
16670 Only in function declarations for constructors, destructors, and
16671 type conversions can the decl-specifier-seq be omitted.
16673 We explicitly postpone this check past the point where we handle
16674 function-definitions because we tolerate function-definitions
16675 that are missing their return types in some modes. */
16676 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16678 cp_parser_error (parser,
16679 "expected constructor, destructor, or type conversion");
16680 return error_mark_node;
16683 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16684 if (token->type == CPP_EQ
16685 || token->type == CPP_OPEN_PAREN
16686 || token->type == CPP_OPEN_BRACE)
16688 is_initialized = SD_INITIALIZED;
16689 initialization_kind = token->type;
16690 if (maybe_range_for_decl)
16691 *maybe_range_for_decl = error_mark_node;
16693 if (token->type == CPP_EQ
16694 && function_declarator_p (declarator))
16696 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16697 if (t2->keyword == RID_DEFAULT)
16698 is_initialized = SD_DEFAULTED;
16699 else if (t2->keyword == RID_DELETE)
16700 is_initialized = SD_DELETED;
16703 else
16705 /* If the init-declarator isn't initialized and isn't followed by a
16706 `,' or `;', it's not a valid init-declarator. */
16707 if (token->type != CPP_COMMA
16708 && token->type != CPP_SEMICOLON)
16710 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16711 range_for_decl_p = true;
16712 else
16714 cp_parser_error (parser, "expected initializer");
16715 return error_mark_node;
16718 is_initialized = SD_UNINITIALIZED;
16719 initialization_kind = CPP_EOF;
16722 /* Because start_decl has side-effects, we should only call it if we
16723 know we're going ahead. By this point, we know that we cannot
16724 possibly be looking at any other construct. */
16725 cp_parser_commit_to_tentative_parse (parser);
16727 /* If the decl specifiers were bad, issue an error now that we're
16728 sure this was intended to be a declarator. Then continue
16729 declaring the variable(s), as int, to try to cut down on further
16730 errors. */
16731 if (decl_specifiers->any_specifiers_p
16732 && decl_specifiers->type == error_mark_node)
16734 cp_parser_error (parser, "invalid type in declaration");
16735 decl_specifiers->type = integer_type_node;
16738 /* Check to see whether or not this declaration is a friend. */
16739 friend_p = cp_parser_friend_p (decl_specifiers);
16741 /* Enter the newly declared entry in the symbol table. If we're
16742 processing a declaration in a class-specifier, we wait until
16743 after processing the initializer. */
16744 if (!member_p)
16746 if (parser->in_unbraced_linkage_specification_p)
16747 decl_specifiers->storage_class = sc_extern;
16748 decl = start_decl (declarator, decl_specifiers,
16749 range_for_decl_p? SD_INITIALIZED : is_initialized,
16750 attributes, prefix_attributes, &pushed_scope);
16751 cp_finalize_omp_declare_simd (parser, decl);
16752 /* Adjust location of decl if declarator->id_loc is more appropriate:
16753 set, and decl wasn't merged with another decl, in which case its
16754 location would be different from input_location, and more accurate. */
16755 if (DECL_P (decl)
16756 && declarator->id_loc != UNKNOWN_LOCATION
16757 && DECL_SOURCE_LOCATION (decl) == input_location)
16758 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16760 else if (scope)
16761 /* Enter the SCOPE. That way unqualified names appearing in the
16762 initializer will be looked up in SCOPE. */
16763 pushed_scope = push_scope (scope);
16765 /* Perform deferred access control checks, now that we know in which
16766 SCOPE the declared entity resides. */
16767 if (!member_p && decl)
16769 tree saved_current_function_decl = NULL_TREE;
16771 /* If the entity being declared is a function, pretend that we
16772 are in its scope. If it is a `friend', it may have access to
16773 things that would not otherwise be accessible. */
16774 if (TREE_CODE (decl) == FUNCTION_DECL)
16776 saved_current_function_decl = current_function_decl;
16777 current_function_decl = decl;
16780 /* Perform access checks for template parameters. */
16781 cp_parser_perform_template_parameter_access_checks (checks);
16783 /* Perform the access control checks for the declarator and the
16784 decl-specifiers. */
16785 perform_deferred_access_checks (tf_warning_or_error);
16787 /* Restore the saved value. */
16788 if (TREE_CODE (decl) == FUNCTION_DECL)
16789 current_function_decl = saved_current_function_decl;
16792 /* Parse the initializer. */
16793 initializer = NULL_TREE;
16794 is_direct_init = false;
16795 is_non_constant_init = true;
16796 if (is_initialized)
16798 if (function_declarator_p (declarator))
16800 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16801 if (initialization_kind == CPP_EQ)
16802 initializer = cp_parser_pure_specifier (parser);
16803 else
16805 /* If the declaration was erroneous, we don't really
16806 know what the user intended, so just silently
16807 consume the initializer. */
16808 if (decl != error_mark_node)
16809 error_at (initializer_start_token->location,
16810 "initializer provided for function");
16811 cp_parser_skip_to_closing_parenthesis (parser,
16812 /*recovering=*/true,
16813 /*or_comma=*/false,
16814 /*consume_paren=*/true);
16817 else
16819 /* We want to record the extra mangling scope for in-class
16820 initializers of class members and initializers of static data
16821 member templates. The former involves deferring
16822 parsing of the initializer until end of class as with default
16823 arguments. So right here we only handle the latter. */
16824 if (!member_p && processing_template_decl)
16825 start_lambda_scope (decl);
16826 initializer = cp_parser_initializer (parser,
16827 &is_direct_init,
16828 &is_non_constant_init);
16829 if (!member_p && processing_template_decl)
16830 finish_lambda_scope ();
16831 if (initializer == error_mark_node)
16832 cp_parser_skip_to_end_of_statement (parser);
16836 /* The old parser allows attributes to appear after a parenthesized
16837 initializer. Mark Mitchell proposed removing this functionality
16838 on the GCC mailing lists on 2002-08-13. This parser accepts the
16839 attributes -- but ignores them. */
16840 if (cp_parser_allow_gnu_extensions_p (parser)
16841 && initialization_kind == CPP_OPEN_PAREN)
16842 if (cp_parser_attributes_opt (parser))
16843 warning (OPT_Wattributes,
16844 "attributes after parenthesized initializer ignored");
16846 /* A non-template declaration involving a function parameter list containing
16847 an implicit template parameter will have been made into a template. If it
16848 turns out that the resulting declaration is not an actual function then
16849 finish the template declaration here. An error message will already have
16850 been issued. */
16851 if (parser->fully_implicit_function_template_p)
16852 if (!function_declarator_p (declarator))
16854 if (pushed_scope)
16856 pop_scope (pushed_scope);
16857 pushed_scope = 0;
16859 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16862 /* For an in-class declaration, use `grokfield' to create the
16863 declaration. */
16864 if (member_p)
16866 if (pushed_scope)
16868 pop_scope (pushed_scope);
16869 pushed_scope = NULL_TREE;
16871 decl = grokfield (declarator, decl_specifiers,
16872 initializer, !is_non_constant_init,
16873 /*asmspec=*/NULL_TREE,
16874 chainon (attributes, prefix_attributes));
16875 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16876 cp_parser_save_default_args (parser, decl);
16877 cp_finalize_omp_declare_simd (parser, decl);
16880 /* Finish processing the declaration. But, skip member
16881 declarations. */
16882 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16884 cp_finish_decl (decl,
16885 initializer, !is_non_constant_init,
16886 asm_specification,
16887 /* If the initializer is in parentheses, then this is
16888 a direct-initialization, which means that an
16889 `explicit' constructor is OK. Otherwise, an
16890 `explicit' constructor cannot be used. */
16891 ((is_direct_init || !is_initialized)
16892 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16894 else if ((cxx_dialect != cxx98) && friend_p
16895 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16896 /* Core issue #226 (C++0x only): A default template-argument
16897 shall not be specified in a friend class template
16898 declaration. */
16899 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16900 /*is_partial=*/false, /*is_friend_decl=*/1);
16902 if (!friend_p && pushed_scope)
16903 pop_scope (pushed_scope);
16905 if (function_declarator_p (declarator)
16906 && parser->fully_implicit_function_template_p)
16908 if (member_p)
16909 decl = finish_fully_implicit_template (parser, decl);
16910 else
16911 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16914 return decl;
16917 /* Parse a declarator.
16919 declarator:
16920 direct-declarator
16921 ptr-operator declarator
16923 abstract-declarator:
16924 ptr-operator abstract-declarator [opt]
16925 direct-abstract-declarator
16927 GNU Extensions:
16929 declarator:
16930 attributes [opt] direct-declarator
16931 attributes [opt] ptr-operator declarator
16933 abstract-declarator:
16934 attributes [opt] ptr-operator abstract-declarator [opt]
16935 attributes [opt] direct-abstract-declarator
16937 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16938 detect constructor, destructor or conversion operators. It is set
16939 to -1 if the declarator is a name, and +1 if it is a
16940 function. Otherwise it is set to zero. Usually you just want to
16941 test for >0, but internally the negative value is used.
16943 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16944 a decl-specifier-seq unless it declares a constructor, destructor,
16945 or conversion. It might seem that we could check this condition in
16946 semantic analysis, rather than parsing, but that makes it difficult
16947 to handle something like `f()'. We want to notice that there are
16948 no decl-specifiers, and therefore realize that this is an
16949 expression, not a declaration.)
16951 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16952 the declarator is a direct-declarator of the form "(...)".
16954 MEMBER_P is true iff this declarator is a member-declarator. */
16956 static cp_declarator *
16957 cp_parser_declarator (cp_parser* parser,
16958 cp_parser_declarator_kind dcl_kind,
16959 int* ctor_dtor_or_conv_p,
16960 bool* parenthesized_p,
16961 bool member_p)
16963 cp_declarator *declarator;
16964 enum tree_code code;
16965 cp_cv_quals cv_quals;
16966 tree class_type;
16967 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16969 /* Assume this is not a constructor, destructor, or type-conversion
16970 operator. */
16971 if (ctor_dtor_or_conv_p)
16972 *ctor_dtor_or_conv_p = 0;
16974 if (cp_parser_allow_gnu_extensions_p (parser))
16975 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16977 /* Check for the ptr-operator production. */
16978 cp_parser_parse_tentatively (parser);
16979 /* Parse the ptr-operator. */
16980 code = cp_parser_ptr_operator (parser,
16981 &class_type,
16982 &cv_quals,
16983 &std_attributes);
16985 /* If that worked, then we have a ptr-operator. */
16986 if (cp_parser_parse_definitely (parser))
16988 /* If a ptr-operator was found, then this declarator was not
16989 parenthesized. */
16990 if (parenthesized_p)
16991 *parenthesized_p = true;
16992 /* The dependent declarator is optional if we are parsing an
16993 abstract-declarator. */
16994 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16995 cp_parser_parse_tentatively (parser);
16997 /* Parse the dependent declarator. */
16998 declarator = cp_parser_declarator (parser, dcl_kind,
16999 /*ctor_dtor_or_conv_p=*/NULL,
17000 /*parenthesized_p=*/NULL,
17001 /*member_p=*/false);
17003 /* If we are parsing an abstract-declarator, we must handle the
17004 case where the dependent declarator is absent. */
17005 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17006 && !cp_parser_parse_definitely (parser))
17007 declarator = NULL;
17009 declarator = cp_parser_make_indirect_declarator
17010 (code, class_type, cv_quals, declarator, std_attributes);
17012 /* Everything else is a direct-declarator. */
17013 else
17015 if (parenthesized_p)
17016 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17017 CPP_OPEN_PAREN);
17018 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17019 ctor_dtor_or_conv_p,
17020 member_p);
17023 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17024 declarator->attributes = gnu_attributes;
17025 return declarator;
17028 /* Parse a direct-declarator or direct-abstract-declarator.
17030 direct-declarator:
17031 declarator-id
17032 direct-declarator ( parameter-declaration-clause )
17033 cv-qualifier-seq [opt]
17034 ref-qualifier [opt]
17035 exception-specification [opt]
17036 direct-declarator [ constant-expression [opt] ]
17037 ( declarator )
17039 direct-abstract-declarator:
17040 direct-abstract-declarator [opt]
17041 ( parameter-declaration-clause )
17042 cv-qualifier-seq [opt]
17043 ref-qualifier [opt]
17044 exception-specification [opt]
17045 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17046 ( abstract-declarator )
17048 Returns a representation of the declarator. DCL_KIND is
17049 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17050 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17051 we are parsing a direct-declarator. It is
17052 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17053 of ambiguity we prefer an abstract declarator, as per
17054 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17055 cp_parser_declarator. */
17057 static cp_declarator *
17058 cp_parser_direct_declarator (cp_parser* parser,
17059 cp_parser_declarator_kind dcl_kind,
17060 int* ctor_dtor_or_conv_p,
17061 bool member_p)
17063 cp_token *token;
17064 cp_declarator *declarator = NULL;
17065 tree scope = NULL_TREE;
17066 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17067 bool saved_in_declarator_p = parser->in_declarator_p;
17068 bool first = true;
17069 tree pushed_scope = NULL_TREE;
17071 while (true)
17073 /* Peek at the next token. */
17074 token = cp_lexer_peek_token (parser->lexer);
17075 if (token->type == CPP_OPEN_PAREN)
17077 /* This is either a parameter-declaration-clause, or a
17078 parenthesized declarator. When we know we are parsing a
17079 named declarator, it must be a parenthesized declarator
17080 if FIRST is true. For instance, `(int)' is a
17081 parameter-declaration-clause, with an omitted
17082 direct-abstract-declarator. But `((*))', is a
17083 parenthesized abstract declarator. Finally, when T is a
17084 template parameter `(T)' is a
17085 parameter-declaration-clause, and not a parenthesized
17086 named declarator.
17088 We first try and parse a parameter-declaration-clause,
17089 and then try a nested declarator (if FIRST is true).
17091 It is not an error for it not to be a
17092 parameter-declaration-clause, even when FIRST is
17093 false. Consider,
17095 int i (int);
17096 int i (3);
17098 The first is the declaration of a function while the
17099 second is the definition of a variable, including its
17100 initializer.
17102 Having seen only the parenthesis, we cannot know which of
17103 these two alternatives should be selected. Even more
17104 complex are examples like:
17106 int i (int (a));
17107 int i (int (3));
17109 The former is a function-declaration; the latter is a
17110 variable initialization.
17112 Thus again, we try a parameter-declaration-clause, and if
17113 that fails, we back out and return. */
17115 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17117 tree params;
17118 bool is_declarator = false;
17120 /* In a member-declarator, the only valid interpretation
17121 of a parenthesis is the start of a
17122 parameter-declaration-clause. (It is invalid to
17123 initialize a static data member with a parenthesized
17124 initializer; only the "=" form of initialization is
17125 permitted.) */
17126 if (!member_p)
17127 cp_parser_parse_tentatively (parser);
17129 /* Consume the `('. */
17130 cp_lexer_consume_token (parser->lexer);
17131 if (first)
17133 /* If this is going to be an abstract declarator, we're
17134 in a declarator and we can't have default args. */
17135 parser->default_arg_ok_p = false;
17136 parser->in_declarator_p = true;
17139 begin_scope (sk_function_parms, NULL_TREE);
17141 /* Parse the parameter-declaration-clause. */
17142 params = cp_parser_parameter_declaration_clause (parser);
17144 /* Consume the `)'. */
17145 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17147 /* If all went well, parse the cv-qualifier-seq,
17148 ref-qualifier and the exception-specification. */
17149 if (member_p || cp_parser_parse_definitely (parser))
17151 cp_cv_quals cv_quals;
17152 cp_virt_specifiers virt_specifiers;
17153 cp_ref_qualifier ref_qual;
17154 tree exception_specification;
17155 tree late_return;
17156 tree attrs;
17157 bool memfn = (member_p || (pushed_scope
17158 && CLASS_TYPE_P (pushed_scope)));
17160 is_declarator = true;
17162 if (ctor_dtor_or_conv_p)
17163 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17164 first = false;
17166 /* Parse the cv-qualifier-seq. */
17167 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17168 /* Parse the ref-qualifier. */
17169 ref_qual = cp_parser_ref_qualifier_opt (parser);
17170 /* And the exception-specification. */
17171 exception_specification
17172 = cp_parser_exception_specification_opt (parser);
17174 attrs = cp_parser_std_attribute_spec_seq (parser);
17176 /* In here, we handle cases where attribute is used after
17177 the function declaration. For example:
17178 void func (int x) __attribute__((vector(..))); */
17179 if (flag_cilkplus
17180 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17182 cp_parser_parse_tentatively (parser);
17183 tree attr = cp_parser_gnu_attributes_opt (parser);
17184 if (cp_lexer_next_token_is_not (parser->lexer,
17185 CPP_SEMICOLON)
17186 && cp_lexer_next_token_is_not (parser->lexer,
17187 CPP_OPEN_BRACE))
17188 cp_parser_abort_tentative_parse (parser);
17189 else if (!cp_parser_parse_definitely (parser))
17191 else
17192 attrs = chainon (attr, attrs);
17194 late_return = (cp_parser_late_return_type_opt
17195 (parser, declarator,
17196 memfn ? cv_quals : -1));
17199 /* Parse the virt-specifier-seq. */
17200 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17202 /* Create the function-declarator. */
17203 declarator = make_call_declarator (declarator,
17204 params,
17205 cv_quals,
17206 virt_specifiers,
17207 ref_qual,
17208 exception_specification,
17209 late_return);
17210 declarator->std_attributes = attrs;
17211 /* Any subsequent parameter lists are to do with
17212 return type, so are not those of the declared
17213 function. */
17214 parser->default_arg_ok_p = false;
17217 /* Remove the function parms from scope. */
17218 pop_bindings_and_leave_scope ();
17220 if (is_declarator)
17221 /* Repeat the main loop. */
17222 continue;
17225 /* If this is the first, we can try a parenthesized
17226 declarator. */
17227 if (first)
17229 bool saved_in_type_id_in_expr_p;
17231 parser->default_arg_ok_p = saved_default_arg_ok_p;
17232 parser->in_declarator_p = saved_in_declarator_p;
17234 /* Consume the `('. */
17235 cp_lexer_consume_token (parser->lexer);
17236 /* Parse the nested declarator. */
17237 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17238 parser->in_type_id_in_expr_p = true;
17239 declarator
17240 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17241 /*parenthesized_p=*/NULL,
17242 member_p);
17243 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17244 first = false;
17245 /* Expect a `)'. */
17246 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17247 declarator = cp_error_declarator;
17248 if (declarator == cp_error_declarator)
17249 break;
17251 goto handle_declarator;
17253 /* Otherwise, we must be done. */
17254 else
17255 break;
17257 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17258 && token->type == CPP_OPEN_SQUARE
17259 && !cp_next_tokens_can_be_attribute_p (parser))
17261 /* Parse an array-declarator. */
17262 tree bounds, attrs;
17264 if (ctor_dtor_or_conv_p)
17265 *ctor_dtor_or_conv_p = 0;
17267 first = false;
17268 parser->default_arg_ok_p = false;
17269 parser->in_declarator_p = true;
17270 /* Consume the `['. */
17271 cp_lexer_consume_token (parser->lexer);
17272 /* Peek at the next token. */
17273 token = cp_lexer_peek_token (parser->lexer);
17274 /* If the next token is `]', then there is no
17275 constant-expression. */
17276 if (token->type != CPP_CLOSE_SQUARE)
17278 bool non_constant_p;
17279 bounds
17280 = cp_parser_constant_expression (parser,
17281 /*allow_non_constant=*/true,
17282 &non_constant_p);
17283 if (!non_constant_p)
17284 /* OK */;
17285 else if (error_operand_p (bounds))
17286 /* Already gave an error. */;
17287 else if (!parser->in_function_body
17288 || current_binding_level->kind == sk_function_parms)
17290 /* Normally, the array bound must be an integral constant
17291 expression. However, as an extension, we allow VLAs
17292 in function scopes as long as they aren't part of a
17293 parameter declaration. */
17294 cp_parser_error (parser,
17295 "array bound is not an integer constant");
17296 bounds = error_mark_node;
17298 else if (processing_template_decl
17299 && !type_dependent_expression_p (bounds))
17301 /* Remember this wasn't a constant-expression. */
17302 bounds = build_nop (TREE_TYPE (bounds), bounds);
17303 TREE_SIDE_EFFECTS (bounds) = 1;
17306 else
17307 bounds = NULL_TREE;
17308 /* Look for the closing `]'. */
17309 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17311 declarator = cp_error_declarator;
17312 break;
17315 attrs = cp_parser_std_attribute_spec_seq (parser);
17316 declarator = make_array_declarator (declarator, bounds);
17317 declarator->std_attributes = attrs;
17319 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17322 tree qualifying_scope;
17323 tree unqualified_name;
17324 tree attrs;
17325 special_function_kind sfk;
17326 bool abstract_ok;
17327 bool pack_expansion_p = false;
17328 cp_token *declarator_id_start_token;
17330 /* Parse a declarator-id */
17331 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17332 if (abstract_ok)
17334 cp_parser_parse_tentatively (parser);
17336 /* If we see an ellipsis, we should be looking at a
17337 parameter pack. */
17338 if (token->type == CPP_ELLIPSIS)
17340 /* Consume the `...' */
17341 cp_lexer_consume_token (parser->lexer);
17343 pack_expansion_p = true;
17347 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17348 unqualified_name
17349 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17350 qualifying_scope = parser->scope;
17351 if (abstract_ok)
17353 bool okay = false;
17355 if (!unqualified_name && pack_expansion_p)
17357 /* Check whether an error occurred. */
17358 okay = !cp_parser_error_occurred (parser);
17360 /* We already consumed the ellipsis to mark a
17361 parameter pack, but we have no way to report it,
17362 so abort the tentative parse. We will be exiting
17363 immediately anyway. */
17364 cp_parser_abort_tentative_parse (parser);
17366 else
17367 okay = cp_parser_parse_definitely (parser);
17369 if (!okay)
17370 unqualified_name = error_mark_node;
17371 else if (unqualified_name
17372 && (qualifying_scope
17373 || (!identifier_p (unqualified_name))))
17375 cp_parser_error (parser, "expected unqualified-id");
17376 unqualified_name = error_mark_node;
17380 if (!unqualified_name)
17381 return NULL;
17382 if (unqualified_name == error_mark_node)
17384 declarator = cp_error_declarator;
17385 pack_expansion_p = false;
17386 declarator->parameter_pack_p = false;
17387 break;
17390 attrs = cp_parser_std_attribute_spec_seq (parser);
17392 if (qualifying_scope && at_namespace_scope_p ()
17393 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17395 /* In the declaration of a member of a template class
17396 outside of the class itself, the SCOPE will sometimes
17397 be a TYPENAME_TYPE. For example, given:
17399 template <typename T>
17400 int S<T>::R::i = 3;
17402 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17403 this context, we must resolve S<T>::R to an ordinary
17404 type, rather than a typename type.
17406 The reason we normally avoid resolving TYPENAME_TYPEs
17407 is that a specialization of `S' might render
17408 `S<T>::R' not a type. However, if `S' is
17409 specialized, then this `i' will not be used, so there
17410 is no harm in resolving the types here. */
17411 tree type;
17413 /* Resolve the TYPENAME_TYPE. */
17414 type = resolve_typename_type (qualifying_scope,
17415 /*only_current_p=*/false);
17416 /* If that failed, the declarator is invalid. */
17417 if (TREE_CODE (type) == TYPENAME_TYPE)
17419 if (typedef_variant_p (type))
17420 error_at (declarator_id_start_token->location,
17421 "cannot define member of dependent typedef "
17422 "%qT", type);
17423 else
17424 error_at (declarator_id_start_token->location,
17425 "%<%T::%E%> is not a type",
17426 TYPE_CONTEXT (qualifying_scope),
17427 TYPE_IDENTIFIER (qualifying_scope));
17429 qualifying_scope = type;
17432 sfk = sfk_none;
17434 if (unqualified_name)
17436 tree class_type;
17438 if (qualifying_scope
17439 && CLASS_TYPE_P (qualifying_scope))
17440 class_type = qualifying_scope;
17441 else
17442 class_type = current_class_type;
17444 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17446 tree name_type = TREE_TYPE (unqualified_name);
17447 if (class_type && same_type_p (name_type, class_type))
17449 if (qualifying_scope
17450 && CLASSTYPE_USE_TEMPLATE (name_type))
17452 error_at (declarator_id_start_token->location,
17453 "invalid use of constructor as a template");
17454 inform (declarator_id_start_token->location,
17455 "use %<%T::%D%> instead of %<%T::%D%> to "
17456 "name the constructor in a qualified name",
17457 class_type,
17458 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17459 class_type, name_type);
17460 declarator = cp_error_declarator;
17461 break;
17463 else
17464 unqualified_name = constructor_name (class_type);
17466 else
17468 /* We do not attempt to print the declarator
17469 here because we do not have enough
17470 information about its original syntactic
17471 form. */
17472 cp_parser_error (parser, "invalid declarator");
17473 declarator = cp_error_declarator;
17474 break;
17478 if (class_type)
17480 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17481 sfk = sfk_destructor;
17482 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17483 sfk = sfk_conversion;
17484 else if (/* There's no way to declare a constructor
17485 for an anonymous type, even if the type
17486 got a name for linkage purposes. */
17487 !TYPE_WAS_ANONYMOUS (class_type)
17488 && constructor_name_p (unqualified_name,
17489 class_type))
17491 unqualified_name = constructor_name (class_type);
17492 sfk = sfk_constructor;
17494 else if (is_overloaded_fn (unqualified_name)
17495 && DECL_CONSTRUCTOR_P (get_first_fn
17496 (unqualified_name)))
17497 sfk = sfk_constructor;
17499 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17500 *ctor_dtor_or_conv_p = -1;
17503 declarator = make_id_declarator (qualifying_scope,
17504 unqualified_name,
17505 sfk);
17506 declarator->std_attributes = attrs;
17507 declarator->id_loc = token->location;
17508 declarator->parameter_pack_p = pack_expansion_p;
17510 if (pack_expansion_p)
17511 maybe_warn_variadic_templates ();
17514 handle_declarator:;
17515 scope = get_scope_of_declarator (declarator);
17516 if (scope)
17518 /* Any names that appear after the declarator-id for a
17519 member are looked up in the containing scope. */
17520 if (at_function_scope_p ())
17522 /* But declarations with qualified-ids can't appear in a
17523 function. */
17524 cp_parser_error (parser, "qualified-id in declaration");
17525 declarator = cp_error_declarator;
17526 break;
17528 pushed_scope = push_scope (scope);
17530 parser->in_declarator_p = true;
17531 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17532 || (declarator && declarator->kind == cdk_id))
17533 /* Default args are only allowed on function
17534 declarations. */
17535 parser->default_arg_ok_p = saved_default_arg_ok_p;
17536 else
17537 parser->default_arg_ok_p = false;
17539 first = false;
17541 /* We're done. */
17542 else
17543 break;
17546 /* For an abstract declarator, we might wind up with nothing at this
17547 point. That's an error; the declarator is not optional. */
17548 if (!declarator)
17549 cp_parser_error (parser, "expected declarator");
17551 /* If we entered a scope, we must exit it now. */
17552 if (pushed_scope)
17553 pop_scope (pushed_scope);
17555 parser->default_arg_ok_p = saved_default_arg_ok_p;
17556 parser->in_declarator_p = saved_in_declarator_p;
17558 return declarator;
17561 /* Parse a ptr-operator.
17563 ptr-operator:
17564 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17565 * cv-qualifier-seq [opt]
17567 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17568 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17570 GNU Extension:
17572 ptr-operator:
17573 & cv-qualifier-seq [opt]
17575 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17576 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17577 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17578 filled in with the TYPE containing the member. *CV_QUALS is
17579 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17580 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17581 Note that the tree codes returned by this function have nothing
17582 to do with the types of trees that will be eventually be created
17583 to represent the pointer or reference type being parsed. They are
17584 just constants with suggestive names. */
17585 static enum tree_code
17586 cp_parser_ptr_operator (cp_parser* parser,
17587 tree* type,
17588 cp_cv_quals *cv_quals,
17589 tree *attributes)
17591 enum tree_code code = ERROR_MARK;
17592 cp_token *token;
17593 tree attrs = NULL_TREE;
17595 /* Assume that it's not a pointer-to-member. */
17596 *type = NULL_TREE;
17597 /* And that there are no cv-qualifiers. */
17598 *cv_quals = TYPE_UNQUALIFIED;
17600 /* Peek at the next token. */
17601 token = cp_lexer_peek_token (parser->lexer);
17603 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17604 if (token->type == CPP_MULT)
17605 code = INDIRECT_REF;
17606 else if (token->type == CPP_AND)
17607 code = ADDR_EXPR;
17608 else if ((cxx_dialect != cxx98) &&
17609 token->type == CPP_AND_AND) /* C++0x only */
17610 code = NON_LVALUE_EXPR;
17612 if (code != ERROR_MARK)
17614 /* Consume the `*', `&' or `&&'. */
17615 cp_lexer_consume_token (parser->lexer);
17617 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17618 `&', if we are allowing GNU extensions. (The only qualifier
17619 that can legally appear after `&' is `restrict', but that is
17620 enforced during semantic analysis. */
17621 if (code == INDIRECT_REF
17622 || cp_parser_allow_gnu_extensions_p (parser))
17623 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17625 attrs = cp_parser_std_attribute_spec_seq (parser);
17626 if (attributes != NULL)
17627 *attributes = attrs;
17629 else
17631 /* Try the pointer-to-member case. */
17632 cp_parser_parse_tentatively (parser);
17633 /* Look for the optional `::' operator. */
17634 cp_parser_global_scope_opt (parser,
17635 /*current_scope_valid_p=*/false);
17636 /* Look for the nested-name specifier. */
17637 token = cp_lexer_peek_token (parser->lexer);
17638 cp_parser_nested_name_specifier (parser,
17639 /*typename_keyword_p=*/false,
17640 /*check_dependency_p=*/true,
17641 /*type_p=*/false,
17642 /*is_declaration=*/false);
17643 /* If we found it, and the next token is a `*', then we are
17644 indeed looking at a pointer-to-member operator. */
17645 if (!cp_parser_error_occurred (parser)
17646 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17648 /* Indicate that the `*' operator was used. */
17649 code = INDIRECT_REF;
17651 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17652 error_at (token->location, "%qD is a namespace", parser->scope);
17653 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17654 error_at (token->location, "cannot form pointer to member of "
17655 "non-class %q#T", parser->scope);
17656 else
17658 /* The type of which the member is a member is given by the
17659 current SCOPE. */
17660 *type = parser->scope;
17661 /* The next name will not be qualified. */
17662 parser->scope = NULL_TREE;
17663 parser->qualifying_scope = NULL_TREE;
17664 parser->object_scope = NULL_TREE;
17665 /* Look for optional c++11 attributes. */
17666 attrs = cp_parser_std_attribute_spec_seq (parser);
17667 if (attributes != NULL)
17668 *attributes = attrs;
17669 /* Look for the optional cv-qualifier-seq. */
17670 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17673 /* If that didn't work we don't have a ptr-operator. */
17674 if (!cp_parser_parse_definitely (parser))
17675 cp_parser_error (parser, "expected ptr-operator");
17678 return code;
17681 /* Parse an (optional) cv-qualifier-seq.
17683 cv-qualifier-seq:
17684 cv-qualifier cv-qualifier-seq [opt]
17686 cv-qualifier:
17687 const
17688 volatile
17690 GNU Extension:
17692 cv-qualifier:
17693 __restrict__
17695 Returns a bitmask representing the cv-qualifiers. */
17697 static cp_cv_quals
17698 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17700 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17702 while (true)
17704 cp_token *token;
17705 cp_cv_quals cv_qualifier;
17707 /* Peek at the next token. */
17708 token = cp_lexer_peek_token (parser->lexer);
17709 /* See if it's a cv-qualifier. */
17710 switch (token->keyword)
17712 case RID_CONST:
17713 cv_qualifier = TYPE_QUAL_CONST;
17714 break;
17716 case RID_VOLATILE:
17717 cv_qualifier = TYPE_QUAL_VOLATILE;
17718 break;
17720 case RID_RESTRICT:
17721 cv_qualifier = TYPE_QUAL_RESTRICT;
17722 break;
17724 default:
17725 cv_qualifier = TYPE_UNQUALIFIED;
17726 break;
17729 if (!cv_qualifier)
17730 break;
17732 if (cv_quals & cv_qualifier)
17734 error_at (token->location, "duplicate cv-qualifier");
17735 cp_lexer_purge_token (parser->lexer);
17737 else
17739 cp_lexer_consume_token (parser->lexer);
17740 cv_quals |= cv_qualifier;
17744 return cv_quals;
17747 /* Parse an (optional) ref-qualifier
17749 ref-qualifier:
17753 Returns cp_ref_qualifier representing ref-qualifier. */
17755 static cp_ref_qualifier
17756 cp_parser_ref_qualifier_opt (cp_parser* parser)
17758 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17760 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17761 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17762 return ref_qual;
17764 while (true)
17766 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17767 cp_token *token = cp_lexer_peek_token (parser->lexer);
17769 switch (token->type)
17771 case CPP_AND:
17772 curr_ref_qual = REF_QUAL_LVALUE;
17773 break;
17775 case CPP_AND_AND:
17776 curr_ref_qual = REF_QUAL_RVALUE;
17777 break;
17779 default:
17780 curr_ref_qual = REF_QUAL_NONE;
17781 break;
17784 if (!curr_ref_qual)
17785 break;
17786 else if (ref_qual)
17788 error_at (token->location, "multiple ref-qualifiers");
17789 cp_lexer_purge_token (parser->lexer);
17791 else
17793 ref_qual = curr_ref_qual;
17794 cp_lexer_consume_token (parser->lexer);
17798 return ref_qual;
17801 /* Parse an (optional) virt-specifier-seq.
17803 virt-specifier-seq:
17804 virt-specifier virt-specifier-seq [opt]
17806 virt-specifier:
17807 override
17808 final
17810 Returns a bitmask representing the virt-specifiers. */
17812 static cp_virt_specifiers
17813 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17815 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17817 while (true)
17819 cp_token *token;
17820 cp_virt_specifiers virt_specifier;
17822 /* Peek at the next token. */
17823 token = cp_lexer_peek_token (parser->lexer);
17824 /* See if it's a virt-specifier-qualifier. */
17825 if (token->type != CPP_NAME)
17826 break;
17827 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17829 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17830 virt_specifier = VIRT_SPEC_OVERRIDE;
17832 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17834 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17835 virt_specifier = VIRT_SPEC_FINAL;
17837 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17839 virt_specifier = VIRT_SPEC_FINAL;
17841 else
17842 break;
17844 if (virt_specifiers & virt_specifier)
17846 error_at (token->location, "duplicate virt-specifier");
17847 cp_lexer_purge_token (parser->lexer);
17849 else
17851 cp_lexer_consume_token (parser->lexer);
17852 virt_specifiers |= virt_specifier;
17855 return virt_specifiers;
17858 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17859 is in scope even though it isn't real. */
17861 void
17862 inject_this_parameter (tree ctype, cp_cv_quals quals)
17864 tree this_parm;
17866 if (current_class_ptr)
17868 /* We don't clear this between NSDMIs. Is it already what we want? */
17869 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17870 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17871 && cp_type_quals (type) == quals)
17872 return;
17875 this_parm = build_this_parm (ctype, quals);
17876 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17877 current_class_ptr = NULL_TREE;
17878 current_class_ref
17879 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17880 current_class_ptr = this_parm;
17883 /* Return true iff our current scope is a non-static data member
17884 initializer. */
17886 bool
17887 parsing_nsdmi (void)
17889 /* We recognize NSDMI context by the context-less 'this' pointer set up
17890 by the function above. */
17891 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17892 return true;
17893 return false;
17896 /* Parse a late-specified return type, if any. This is not a separate
17897 non-terminal, but part of a function declarator, which looks like
17899 -> trailing-type-specifier-seq abstract-declarator(opt)
17901 Returns the type indicated by the type-id.
17903 In addition to this this parses any queued up omp declare simd
17904 clauses and Cilk Plus SIMD-enabled function's vector attributes.
17906 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17907 function. */
17909 static tree
17910 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17911 cp_cv_quals quals)
17913 cp_token *token;
17914 tree type = NULL_TREE;
17915 bool declare_simd_p = (parser->omp_declare_simd
17916 && declarator
17917 && declarator->kind == cdk_id);
17919 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
17920 && declarator && declarator->kind == cdk_id);
17922 /* Peek at the next token. */
17923 token = cp_lexer_peek_token (parser->lexer);
17924 /* A late-specified return type is indicated by an initial '->'. */
17925 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
17926 return NULL_TREE;
17928 tree save_ccp = current_class_ptr;
17929 tree save_ccr = current_class_ref;
17930 if (quals >= 0)
17932 /* DR 1207: 'this' is in scope in the trailing return type. */
17933 inject_this_parameter (current_class_type, quals);
17936 if (token->type == CPP_DEREF)
17938 /* Consume the ->. */
17939 cp_lexer_consume_token (parser->lexer);
17941 type = cp_parser_trailing_type_id (parser);
17944 if (cilk_simd_fn_vector_p)
17945 declarator->std_attributes
17946 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
17947 declarator->std_attributes);
17948 if (declare_simd_p)
17949 declarator->std_attributes
17950 = cp_parser_late_parsing_omp_declare_simd (parser,
17951 declarator->std_attributes);
17953 if (quals >= 0)
17955 current_class_ptr = save_ccp;
17956 current_class_ref = save_ccr;
17959 return type;
17962 /* Parse a declarator-id.
17964 declarator-id:
17965 id-expression
17966 :: [opt] nested-name-specifier [opt] type-name
17968 In the `id-expression' case, the value returned is as for
17969 cp_parser_id_expression if the id-expression was an unqualified-id.
17970 If the id-expression was a qualified-id, then a SCOPE_REF is
17971 returned. The first operand is the scope (either a NAMESPACE_DECL
17972 or TREE_TYPE), but the second is still just a representation of an
17973 unqualified-id. */
17975 static tree
17976 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17978 tree id;
17979 /* The expression must be an id-expression. Assume that qualified
17980 names are the names of types so that:
17982 template <class T>
17983 int S<T>::R::i = 3;
17985 will work; we must treat `S<T>::R' as the name of a type.
17986 Similarly, assume that qualified names are templates, where
17987 required, so that:
17989 template <class T>
17990 int S<T>::R<T>::i = 3;
17992 will work, too. */
17993 id = cp_parser_id_expression (parser,
17994 /*template_keyword_p=*/false,
17995 /*check_dependency_p=*/false,
17996 /*template_p=*/NULL,
17997 /*declarator_p=*/true,
17998 optional_p);
17999 if (id && BASELINK_P (id))
18000 id = BASELINK_FUNCTIONS (id);
18001 return id;
18004 /* Parse a type-id.
18006 type-id:
18007 type-specifier-seq abstract-declarator [opt]
18009 Returns the TYPE specified. */
18011 static tree
18012 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18013 bool is_trailing_return)
18015 cp_decl_specifier_seq type_specifier_seq;
18016 cp_declarator *abstract_declarator;
18018 /* Parse the type-specifier-seq. */
18019 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18020 is_trailing_return,
18021 &type_specifier_seq);
18022 if (type_specifier_seq.type == error_mark_node)
18023 return error_mark_node;
18025 /* There might or might not be an abstract declarator. */
18026 cp_parser_parse_tentatively (parser);
18027 /* Look for the declarator. */
18028 abstract_declarator
18029 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18030 /*parenthesized_p=*/NULL,
18031 /*member_p=*/false);
18032 /* Check to see if there really was a declarator. */
18033 if (!cp_parser_parse_definitely (parser))
18034 abstract_declarator = NULL;
18036 if (type_specifier_seq.type
18037 /* None of the valid uses of 'auto' in C++14 involve the type-id
18038 nonterminal, but it is valid in a trailing-return-type. */
18039 && !(cxx_dialect >= cxx1y && is_trailing_return)
18040 && type_uses_auto (type_specifier_seq.type))
18042 /* A type-id with type 'auto' is only ok if the abstract declarator
18043 is a function declarator with a late-specified return type. */
18044 if (abstract_declarator
18045 && abstract_declarator->kind == cdk_function
18046 && abstract_declarator->u.function.late_return_type)
18047 /* OK */;
18048 else
18050 error ("invalid use of %<auto%>");
18051 return error_mark_node;
18055 return groktypename (&type_specifier_seq, abstract_declarator,
18056 is_template_arg);
18059 static tree cp_parser_type_id (cp_parser *parser)
18061 return cp_parser_type_id_1 (parser, false, false);
18064 static tree cp_parser_template_type_arg (cp_parser *parser)
18066 tree r;
18067 const char *saved_message = parser->type_definition_forbidden_message;
18068 parser->type_definition_forbidden_message
18069 = G_("types may not be defined in template arguments");
18070 r = cp_parser_type_id_1 (parser, true, false);
18071 parser->type_definition_forbidden_message = saved_message;
18072 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18074 error ("invalid use of %<auto%> in template argument");
18075 r = error_mark_node;
18077 return r;
18080 static tree cp_parser_trailing_type_id (cp_parser *parser)
18082 return cp_parser_type_id_1 (parser, false, true);
18085 /* Parse a type-specifier-seq.
18087 type-specifier-seq:
18088 type-specifier type-specifier-seq [opt]
18090 GNU extension:
18092 type-specifier-seq:
18093 attributes type-specifier-seq [opt]
18095 If IS_DECLARATION is true, we are at the start of a "condition" or
18096 exception-declaration, so we might be followed by a declarator-id.
18098 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18099 i.e. we've just seen "->".
18101 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18103 static void
18104 cp_parser_type_specifier_seq (cp_parser* parser,
18105 bool is_declaration,
18106 bool is_trailing_return,
18107 cp_decl_specifier_seq *type_specifier_seq)
18109 bool seen_type_specifier = false;
18110 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18111 cp_token *start_token = NULL;
18113 /* Clear the TYPE_SPECIFIER_SEQ. */
18114 clear_decl_specs (type_specifier_seq);
18116 /* In the context of a trailing return type, enum E { } is an
18117 elaborated-type-specifier followed by a function-body, not an
18118 enum-specifier. */
18119 if (is_trailing_return)
18120 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18122 /* Parse the type-specifiers and attributes. */
18123 while (true)
18125 tree type_specifier;
18126 bool is_cv_qualifier;
18128 /* Check for attributes first. */
18129 if (cp_next_tokens_can_be_attribute_p (parser))
18131 type_specifier_seq->attributes =
18132 chainon (type_specifier_seq->attributes,
18133 cp_parser_attributes_opt (parser));
18134 continue;
18137 /* record the token of the beginning of the type specifier seq,
18138 for error reporting purposes*/
18139 if (!start_token)
18140 start_token = cp_lexer_peek_token (parser->lexer);
18142 /* Look for the type-specifier. */
18143 type_specifier = cp_parser_type_specifier (parser,
18144 flags,
18145 type_specifier_seq,
18146 /*is_declaration=*/false,
18147 NULL,
18148 &is_cv_qualifier);
18149 if (!type_specifier)
18151 /* If the first type-specifier could not be found, this is not a
18152 type-specifier-seq at all. */
18153 if (!seen_type_specifier)
18155 /* Set in_declarator_p to avoid skipping to the semicolon. */
18156 int in_decl = parser->in_declarator_p;
18157 parser->in_declarator_p = true;
18159 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18160 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18161 cp_parser_error (parser, "expected type-specifier");
18163 parser->in_declarator_p = in_decl;
18165 type_specifier_seq->type = error_mark_node;
18166 return;
18168 /* If subsequent type-specifiers could not be found, the
18169 type-specifier-seq is complete. */
18170 break;
18173 seen_type_specifier = true;
18174 /* The standard says that a condition can be:
18176 type-specifier-seq declarator = assignment-expression
18178 However, given:
18180 struct S {};
18181 if (int S = ...)
18183 we should treat the "S" as a declarator, not as a
18184 type-specifier. The standard doesn't say that explicitly for
18185 type-specifier-seq, but it does say that for
18186 decl-specifier-seq in an ordinary declaration. Perhaps it
18187 would be clearer just to allow a decl-specifier-seq here, and
18188 then add a semantic restriction that if any decl-specifiers
18189 that are not type-specifiers appear, the program is invalid. */
18190 if (is_declaration && !is_cv_qualifier)
18191 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18195 /* Return whether the function currently being declared has an associated
18196 template parameter list. */
18198 static bool
18199 function_being_declared_is_template_p (cp_parser* parser)
18201 if (!current_template_parms || processing_template_parmlist)
18202 return false;
18204 if (parser->implicit_template_scope)
18205 return true;
18207 if (at_class_scope_p ()
18208 && TYPE_BEING_DEFINED (current_class_type))
18209 return parser->num_template_parameter_lists != 0;
18211 return ((int) parser->num_template_parameter_lists > template_class_depth
18212 (current_class_type));
18215 /* Parse a parameter-declaration-clause.
18217 parameter-declaration-clause:
18218 parameter-declaration-list [opt] ... [opt]
18219 parameter-declaration-list , ...
18221 Returns a representation for the parameter declarations. A return
18222 value of NULL indicates a parameter-declaration-clause consisting
18223 only of an ellipsis. */
18225 static tree
18226 cp_parser_parameter_declaration_clause (cp_parser* parser)
18228 tree parameters;
18229 cp_token *token;
18230 bool ellipsis_p;
18231 bool is_error;
18233 struct cleanup {
18234 cp_parser* parser;
18235 int auto_is_implicit_function_template_parm_p;
18236 ~cleanup() {
18237 parser->auto_is_implicit_function_template_parm_p
18238 = auto_is_implicit_function_template_parm_p;
18240 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18242 (void) cleanup;
18244 if (!processing_specialization
18245 && !processing_template_parmlist
18246 && !processing_explicit_instantiation)
18247 if (!current_function_decl
18248 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18249 parser->auto_is_implicit_function_template_parm_p = true;
18251 /* Peek at the next token. */
18252 token = cp_lexer_peek_token (parser->lexer);
18253 /* Check for trivial parameter-declaration-clauses. */
18254 if (token->type == CPP_ELLIPSIS)
18256 /* Consume the `...' token. */
18257 cp_lexer_consume_token (parser->lexer);
18258 return NULL_TREE;
18260 else if (token->type == CPP_CLOSE_PAREN)
18261 /* There are no parameters. */
18263 #ifndef NO_IMPLICIT_EXTERN_C
18264 if (in_system_header_at (input_location)
18265 && current_class_type == NULL
18266 && current_lang_name == lang_name_c)
18267 return NULL_TREE;
18268 else
18269 #endif
18270 return void_list_node;
18272 /* Check for `(void)', too, which is a special case. */
18273 else if (token->keyword == RID_VOID
18274 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18275 == CPP_CLOSE_PAREN))
18277 /* Consume the `void' token. */
18278 cp_lexer_consume_token (parser->lexer);
18279 /* There are no parameters. */
18280 return void_list_node;
18283 /* Parse the parameter-declaration-list. */
18284 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18285 /* If a parse error occurred while parsing the
18286 parameter-declaration-list, then the entire
18287 parameter-declaration-clause is erroneous. */
18288 if (is_error)
18289 return NULL;
18291 /* Peek at the next token. */
18292 token = cp_lexer_peek_token (parser->lexer);
18293 /* If it's a `,', the clause should terminate with an ellipsis. */
18294 if (token->type == CPP_COMMA)
18296 /* Consume the `,'. */
18297 cp_lexer_consume_token (parser->lexer);
18298 /* Expect an ellipsis. */
18299 ellipsis_p
18300 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18302 /* It might also be `...' if the optional trailing `,' was
18303 omitted. */
18304 else if (token->type == CPP_ELLIPSIS)
18306 /* Consume the `...' token. */
18307 cp_lexer_consume_token (parser->lexer);
18308 /* And remember that we saw it. */
18309 ellipsis_p = true;
18311 else
18312 ellipsis_p = false;
18314 /* Finish the parameter list. */
18315 if (!ellipsis_p)
18316 parameters = chainon (parameters, void_list_node);
18318 return parameters;
18321 /* Parse a parameter-declaration-list.
18323 parameter-declaration-list:
18324 parameter-declaration
18325 parameter-declaration-list , parameter-declaration
18327 Returns a representation of the parameter-declaration-list, as for
18328 cp_parser_parameter_declaration_clause. However, the
18329 `void_list_node' is never appended to the list. Upon return,
18330 *IS_ERROR will be true iff an error occurred. */
18332 static tree
18333 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18335 tree parameters = NULL_TREE;
18336 tree *tail = &parameters;
18337 bool saved_in_unbraced_linkage_specification_p;
18338 int index = 0;
18340 /* Assume all will go well. */
18341 *is_error = false;
18342 /* The special considerations that apply to a function within an
18343 unbraced linkage specifications do not apply to the parameters
18344 to the function. */
18345 saved_in_unbraced_linkage_specification_p
18346 = parser->in_unbraced_linkage_specification_p;
18347 parser->in_unbraced_linkage_specification_p = false;
18349 /* Look for more parameters. */
18350 while (true)
18352 cp_parameter_declarator *parameter;
18353 tree decl = error_mark_node;
18354 bool parenthesized_p = false;
18355 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18356 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18357 (current_template_parms)) : 0);
18359 /* Parse the parameter. */
18360 parameter
18361 = cp_parser_parameter_declaration (parser,
18362 /*template_parm_p=*/false,
18363 &parenthesized_p);
18365 /* We don't know yet if the enclosing context is deprecated, so wait
18366 and warn in grokparms if appropriate. */
18367 deprecated_state = DEPRECATED_SUPPRESS;
18369 if (parameter)
18371 /* If a function parameter pack was specified and an implicit template
18372 parameter was introduced during cp_parser_parameter_declaration,
18373 change any implicit parameters introduced into packs. */
18374 if (parser->implicit_template_parms
18375 && parameter->declarator
18376 && parameter->declarator->parameter_pack_p)
18378 int latest_template_parm_idx = TREE_VEC_LENGTH
18379 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18381 if (latest_template_parm_idx != template_parm_idx)
18382 parameter->decl_specifiers.type = convert_generic_types_to_packs
18383 (parameter->decl_specifiers.type,
18384 template_parm_idx, latest_template_parm_idx);
18387 decl = grokdeclarator (parameter->declarator,
18388 &parameter->decl_specifiers,
18389 PARM,
18390 parameter->default_argument != NULL_TREE,
18391 &parameter->decl_specifiers.attributes);
18394 deprecated_state = DEPRECATED_NORMAL;
18396 /* If a parse error occurred parsing the parameter declaration,
18397 then the entire parameter-declaration-list is erroneous. */
18398 if (decl == error_mark_node)
18400 *is_error = true;
18401 parameters = error_mark_node;
18402 break;
18405 if (parameter->decl_specifiers.attributes)
18406 cplus_decl_attributes (&decl,
18407 parameter->decl_specifiers.attributes,
18409 if (DECL_NAME (decl))
18410 decl = pushdecl (decl);
18412 if (decl != error_mark_node)
18414 retrofit_lang_decl (decl);
18415 DECL_PARM_INDEX (decl) = ++index;
18416 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18419 /* Add the new parameter to the list. */
18420 *tail = build_tree_list (parameter->default_argument, decl);
18421 tail = &TREE_CHAIN (*tail);
18423 /* Peek at the next token. */
18424 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18425 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18426 /* These are for Objective-C++ */
18427 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18428 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18429 /* The parameter-declaration-list is complete. */
18430 break;
18431 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18433 cp_token *token;
18435 /* Peek at the next token. */
18436 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18437 /* If it's an ellipsis, then the list is complete. */
18438 if (token->type == CPP_ELLIPSIS)
18439 break;
18440 /* Otherwise, there must be more parameters. Consume the
18441 `,'. */
18442 cp_lexer_consume_token (parser->lexer);
18443 /* When parsing something like:
18445 int i(float f, double d)
18447 we can tell after seeing the declaration for "f" that we
18448 are not looking at an initialization of a variable "i",
18449 but rather at the declaration of a function "i".
18451 Due to the fact that the parsing of template arguments
18452 (as specified to a template-id) requires backtracking we
18453 cannot use this technique when inside a template argument
18454 list. */
18455 if (!parser->in_template_argument_list_p
18456 && !parser->in_type_id_in_expr_p
18457 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18458 /* However, a parameter-declaration of the form
18459 "float(f)" (which is a valid declaration of a
18460 parameter "f") can also be interpreted as an
18461 expression (the conversion of "f" to "float"). */
18462 && !parenthesized_p)
18463 cp_parser_commit_to_tentative_parse (parser);
18465 else
18467 cp_parser_error (parser, "expected %<,%> or %<...%>");
18468 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18469 cp_parser_skip_to_closing_parenthesis (parser,
18470 /*recovering=*/true,
18471 /*or_comma=*/false,
18472 /*consume_paren=*/false);
18473 break;
18477 parser->in_unbraced_linkage_specification_p
18478 = saved_in_unbraced_linkage_specification_p;
18480 /* Reset implicit_template_scope if we are about to leave the function
18481 parameter list that introduced it. Note that for out-of-line member
18482 definitions, there will be one or more class scopes before we get to
18483 the template parameter scope. */
18485 if (cp_binding_level *its = parser->implicit_template_scope)
18486 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18488 while (maybe_its->kind == sk_class)
18489 maybe_its = maybe_its->level_chain;
18490 if (maybe_its == its)
18492 parser->implicit_template_parms = 0;
18493 parser->implicit_template_scope = 0;
18497 return parameters;
18500 /* Parse a parameter declaration.
18502 parameter-declaration:
18503 decl-specifier-seq ... [opt] declarator
18504 decl-specifier-seq declarator = assignment-expression
18505 decl-specifier-seq ... [opt] abstract-declarator [opt]
18506 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18508 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18509 declares a template parameter. (In that case, a non-nested `>'
18510 token encountered during the parsing of the assignment-expression
18511 is not interpreted as a greater-than operator.)
18513 Returns a representation of the parameter, or NULL if an error
18514 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18515 true iff the declarator is of the form "(p)". */
18517 static cp_parameter_declarator *
18518 cp_parser_parameter_declaration (cp_parser *parser,
18519 bool template_parm_p,
18520 bool *parenthesized_p)
18522 int declares_class_or_enum;
18523 cp_decl_specifier_seq decl_specifiers;
18524 cp_declarator *declarator;
18525 tree default_argument;
18526 cp_token *token = NULL, *declarator_token_start = NULL;
18527 const char *saved_message;
18529 /* In a template parameter, `>' is not an operator.
18531 [temp.param]
18533 When parsing a default template-argument for a non-type
18534 template-parameter, the first non-nested `>' is taken as the end
18535 of the template parameter-list rather than a greater-than
18536 operator. */
18538 /* Type definitions may not appear in parameter types. */
18539 saved_message = parser->type_definition_forbidden_message;
18540 parser->type_definition_forbidden_message
18541 = G_("types may not be defined in parameter types");
18543 /* Parse the declaration-specifiers. */
18544 cp_parser_decl_specifier_seq (parser,
18545 CP_PARSER_FLAGS_NONE,
18546 &decl_specifiers,
18547 &declares_class_or_enum);
18549 /* Complain about missing 'typename' or other invalid type names. */
18550 if (!decl_specifiers.any_type_specifiers_p
18551 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18552 decl_specifiers.type = error_mark_node;
18554 /* If an error occurred, there's no reason to attempt to parse the
18555 rest of the declaration. */
18556 if (cp_parser_error_occurred (parser))
18558 parser->type_definition_forbidden_message = saved_message;
18559 return NULL;
18562 /* Peek at the next token. */
18563 token = cp_lexer_peek_token (parser->lexer);
18565 /* If the next token is a `)', `,', `=', `>', or `...', then there
18566 is no declarator. However, when variadic templates are enabled,
18567 there may be a declarator following `...'. */
18568 if (token->type == CPP_CLOSE_PAREN
18569 || token->type == CPP_COMMA
18570 || token->type == CPP_EQ
18571 || token->type == CPP_GREATER)
18573 declarator = NULL;
18574 if (parenthesized_p)
18575 *parenthesized_p = false;
18577 /* Otherwise, there should be a declarator. */
18578 else
18580 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18581 parser->default_arg_ok_p = false;
18583 /* After seeing a decl-specifier-seq, if the next token is not a
18584 "(", there is no possibility that the code is a valid
18585 expression. Therefore, if parsing tentatively, we commit at
18586 this point. */
18587 if (!parser->in_template_argument_list_p
18588 /* In an expression context, having seen:
18590 (int((char ...
18592 we cannot be sure whether we are looking at a
18593 function-type (taking a "char" as a parameter) or a cast
18594 of some object of type "char" to "int". */
18595 && !parser->in_type_id_in_expr_p
18596 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18597 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18598 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18599 cp_parser_commit_to_tentative_parse (parser);
18600 /* Parse the declarator. */
18601 declarator_token_start = token;
18602 declarator = cp_parser_declarator (parser,
18603 CP_PARSER_DECLARATOR_EITHER,
18604 /*ctor_dtor_or_conv_p=*/NULL,
18605 parenthesized_p,
18606 /*member_p=*/false);
18607 parser->default_arg_ok_p = saved_default_arg_ok_p;
18608 /* After the declarator, allow more attributes. */
18609 decl_specifiers.attributes
18610 = chainon (decl_specifiers.attributes,
18611 cp_parser_attributes_opt (parser));
18614 /* If the next token is an ellipsis, and we have not seen a
18615 declarator name, and the type of the declarator contains parameter
18616 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18617 a parameter pack expansion expression. Otherwise, leave the
18618 ellipsis for a C-style variadic function. */
18619 token = cp_lexer_peek_token (parser->lexer);
18620 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18622 tree type = decl_specifiers.type;
18624 if (type && DECL_P (type))
18625 type = TREE_TYPE (type);
18627 if (type
18628 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18629 && declarator_can_be_parameter_pack (declarator)
18630 && (!declarator || !declarator->parameter_pack_p)
18631 && uses_parameter_packs (type))
18633 /* Consume the `...'. */
18634 cp_lexer_consume_token (parser->lexer);
18635 maybe_warn_variadic_templates ();
18637 /* Build a pack expansion type */
18638 if (declarator)
18639 declarator->parameter_pack_p = true;
18640 else
18641 decl_specifiers.type = make_pack_expansion (type);
18645 /* The restriction on defining new types applies only to the type
18646 of the parameter, not to the default argument. */
18647 parser->type_definition_forbidden_message = saved_message;
18649 /* If the next token is `=', then process a default argument. */
18650 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18652 token = cp_lexer_peek_token (parser->lexer);
18653 /* If we are defining a class, then the tokens that make up the
18654 default argument must be saved and processed later. */
18655 if (!template_parm_p && at_class_scope_p ()
18656 && TYPE_BEING_DEFINED (current_class_type)
18657 && !LAMBDA_TYPE_P (current_class_type))
18658 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18659 /* Outside of a class definition, we can just parse the
18660 assignment-expression. */
18661 else
18662 default_argument
18663 = cp_parser_default_argument (parser, template_parm_p);
18665 if (!parser->default_arg_ok_p)
18667 if (flag_permissive)
18668 warning (0, "deprecated use of default argument for parameter of non-function");
18669 else
18671 error_at (token->location,
18672 "default arguments are only "
18673 "permitted for function parameters");
18674 default_argument = NULL_TREE;
18677 else if ((declarator && declarator->parameter_pack_p)
18678 || (decl_specifiers.type
18679 && PACK_EXPANSION_P (decl_specifiers.type)))
18681 /* Find the name of the parameter pack. */
18682 cp_declarator *id_declarator = declarator;
18683 while (id_declarator && id_declarator->kind != cdk_id)
18684 id_declarator = id_declarator->declarator;
18686 if (id_declarator && id_declarator->kind == cdk_id)
18687 error_at (declarator_token_start->location,
18688 template_parm_p
18689 ? G_("template parameter pack %qD "
18690 "cannot have a default argument")
18691 : G_("parameter pack %qD cannot have "
18692 "a default argument"),
18693 id_declarator->u.id.unqualified_name);
18694 else
18695 error_at (declarator_token_start->location,
18696 template_parm_p
18697 ? G_("template parameter pack cannot have "
18698 "a default argument")
18699 : G_("parameter pack cannot have a "
18700 "default argument"));
18702 default_argument = NULL_TREE;
18705 else
18706 default_argument = NULL_TREE;
18708 return make_parameter_declarator (&decl_specifiers,
18709 declarator,
18710 default_argument);
18713 /* Parse a default argument and return it.
18715 TEMPLATE_PARM_P is true if this is a default argument for a
18716 non-type template parameter. */
18717 static tree
18718 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18720 tree default_argument = NULL_TREE;
18721 bool saved_greater_than_is_operator_p;
18722 bool saved_local_variables_forbidden_p;
18723 bool non_constant_p, is_direct_init;
18725 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18726 set correctly. */
18727 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18728 parser->greater_than_is_operator_p = !template_parm_p;
18729 /* Local variable names (and the `this' keyword) may not
18730 appear in a default argument. */
18731 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18732 parser->local_variables_forbidden_p = true;
18733 /* Parse the assignment-expression. */
18734 if (template_parm_p)
18735 push_deferring_access_checks (dk_no_deferred);
18736 tree saved_class_ptr = NULL_TREE;
18737 tree saved_class_ref = NULL_TREE;
18738 /* The "this" pointer is not valid in a default argument. */
18739 if (cfun)
18741 saved_class_ptr = current_class_ptr;
18742 cp_function_chain->x_current_class_ptr = NULL_TREE;
18743 saved_class_ref = current_class_ref;
18744 cp_function_chain->x_current_class_ref = NULL_TREE;
18746 default_argument
18747 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18748 /* Restore the "this" pointer. */
18749 if (cfun)
18751 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18752 cp_function_chain->x_current_class_ref = saved_class_ref;
18754 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18755 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18756 if (template_parm_p)
18757 pop_deferring_access_checks ();
18758 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18759 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18761 return default_argument;
18764 /* Parse a function-body.
18766 function-body:
18767 compound_statement */
18769 static void
18770 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18772 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18775 /* Parse a ctor-initializer-opt followed by a function-body. Return
18776 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18777 is true we are parsing a function-try-block. */
18779 static bool
18780 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18781 bool in_function_try_block)
18783 tree body, list;
18784 bool ctor_initializer_p;
18785 const bool check_body_p =
18786 DECL_CONSTRUCTOR_P (current_function_decl)
18787 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18788 tree last = NULL;
18790 /* Begin the function body. */
18791 body = begin_function_body ();
18792 /* Parse the optional ctor-initializer. */
18793 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18795 /* If we're parsing a constexpr constructor definition, we need
18796 to check that the constructor body is indeed empty. However,
18797 before we get to cp_parser_function_body lot of junk has been
18798 generated, so we can't just check that we have an empty block.
18799 Rather we take a snapshot of the outermost block, and check whether
18800 cp_parser_function_body changed its state. */
18801 if (check_body_p)
18803 list = cur_stmt_list;
18804 if (STATEMENT_LIST_TAIL (list))
18805 last = STATEMENT_LIST_TAIL (list)->stmt;
18807 /* Parse the function-body. */
18808 cp_parser_function_body (parser, in_function_try_block);
18809 if (check_body_p)
18810 check_constexpr_ctor_body (last, list);
18811 /* Finish the function body. */
18812 finish_function_body (body);
18814 return ctor_initializer_p;
18817 /* Parse an initializer.
18819 initializer:
18820 = initializer-clause
18821 ( expression-list )
18823 Returns an expression representing the initializer. If no
18824 initializer is present, NULL_TREE is returned.
18826 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18827 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18828 set to TRUE if there is no initializer present. If there is an
18829 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18830 is set to true; otherwise it is set to false. */
18832 static tree
18833 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18834 bool* non_constant_p)
18836 cp_token *token;
18837 tree init;
18839 /* Peek at the next token. */
18840 token = cp_lexer_peek_token (parser->lexer);
18842 /* Let our caller know whether or not this initializer was
18843 parenthesized. */
18844 *is_direct_init = (token->type != CPP_EQ);
18845 /* Assume that the initializer is constant. */
18846 *non_constant_p = false;
18848 if (token->type == CPP_EQ)
18850 /* Consume the `='. */
18851 cp_lexer_consume_token (parser->lexer);
18852 /* Parse the initializer-clause. */
18853 init = cp_parser_initializer_clause (parser, non_constant_p);
18855 else if (token->type == CPP_OPEN_PAREN)
18857 vec<tree, va_gc> *vec;
18858 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18859 /*cast_p=*/false,
18860 /*allow_expansion_p=*/true,
18861 non_constant_p);
18862 if (vec == NULL)
18863 return error_mark_node;
18864 init = build_tree_list_vec (vec);
18865 release_tree_vector (vec);
18867 else if (token->type == CPP_OPEN_BRACE)
18869 cp_lexer_set_source_position (parser->lexer);
18870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18871 init = cp_parser_braced_list (parser, non_constant_p);
18872 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18874 else
18876 /* Anything else is an error. */
18877 cp_parser_error (parser, "expected initializer");
18878 init = error_mark_node;
18881 return init;
18884 /* Parse an initializer-clause.
18886 initializer-clause:
18887 assignment-expression
18888 braced-init-list
18890 Returns an expression representing the initializer.
18892 If the `assignment-expression' production is used the value
18893 returned is simply a representation for the expression.
18895 Otherwise, calls cp_parser_braced_list. */
18897 static tree
18898 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18900 tree initializer;
18902 /* Assume the expression is constant. */
18903 *non_constant_p = false;
18905 /* If it is not a `{', then we are looking at an
18906 assignment-expression. */
18907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18909 initializer
18910 = cp_parser_constant_expression (parser,
18911 /*allow_non_constant_p=*/true,
18912 non_constant_p);
18914 else
18915 initializer = cp_parser_braced_list (parser, non_constant_p);
18917 return initializer;
18920 /* Parse a brace-enclosed initializer list.
18922 braced-init-list:
18923 { initializer-list , [opt] }
18926 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18927 the elements of the initializer-list (or NULL, if the last
18928 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18929 NULL_TREE. There is no way to detect whether or not the optional
18930 trailing `,' was provided. NON_CONSTANT_P is as for
18931 cp_parser_initializer. */
18933 static tree
18934 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18936 tree initializer;
18938 /* Consume the `{' token. */
18939 cp_lexer_consume_token (parser->lexer);
18940 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18941 initializer = make_node (CONSTRUCTOR);
18942 /* If it's not a `}', then there is a non-trivial initializer. */
18943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18945 /* Parse the initializer list. */
18946 CONSTRUCTOR_ELTS (initializer)
18947 = cp_parser_initializer_list (parser, non_constant_p);
18948 /* A trailing `,' token is allowed. */
18949 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18950 cp_lexer_consume_token (parser->lexer);
18952 else
18953 *non_constant_p = false;
18954 /* Now, there should be a trailing `}'. */
18955 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18956 TREE_TYPE (initializer) = init_list_type_node;
18957 return initializer;
18960 /* Parse an initializer-list.
18962 initializer-list:
18963 initializer-clause ... [opt]
18964 initializer-list , initializer-clause ... [opt]
18966 GNU Extension:
18968 initializer-list:
18969 designation initializer-clause ...[opt]
18970 initializer-list , designation initializer-clause ...[opt]
18972 designation:
18973 . identifier =
18974 identifier :
18975 [ constant-expression ] =
18977 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18978 for the initializer. If the INDEX of the elt is non-NULL, it is the
18979 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18980 as for cp_parser_initializer. */
18982 static vec<constructor_elt, va_gc> *
18983 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18985 vec<constructor_elt, va_gc> *v = NULL;
18987 /* Assume all of the expressions are constant. */
18988 *non_constant_p = false;
18990 /* Parse the rest of the list. */
18991 while (true)
18993 cp_token *token;
18994 tree designator;
18995 tree initializer;
18996 bool clause_non_constant_p;
18998 /* If the next token is an identifier and the following one is a
18999 colon, we are looking at the GNU designated-initializer
19000 syntax. */
19001 if (cp_parser_allow_gnu_extensions_p (parser)
19002 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19003 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19005 /* Warn the user that they are using an extension. */
19006 pedwarn (input_location, OPT_Wpedantic,
19007 "ISO C++ does not allow designated initializers");
19008 /* Consume the identifier. */
19009 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19010 /* Consume the `:'. */
19011 cp_lexer_consume_token (parser->lexer);
19013 /* Also handle the C99 syntax, '. id ='. */
19014 else if (cp_parser_allow_gnu_extensions_p (parser)
19015 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19016 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19017 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19019 /* Warn the user that they are using an extension. */
19020 pedwarn (input_location, OPT_Wpedantic,
19021 "ISO C++ does not allow C99 designated initializers");
19022 /* Consume the `.'. */
19023 cp_lexer_consume_token (parser->lexer);
19024 /* Consume the identifier. */
19025 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19026 /* Consume the `='. */
19027 cp_lexer_consume_token (parser->lexer);
19029 /* Also handle C99 array designators, '[ const ] ='. */
19030 else if (cp_parser_allow_gnu_extensions_p (parser)
19031 && !c_dialect_objc ()
19032 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19034 /* In C++11, [ could start a lambda-introducer. */
19035 bool non_const = false;
19037 cp_parser_parse_tentatively (parser);
19038 cp_lexer_consume_token (parser->lexer);
19039 designator = cp_parser_constant_expression (parser, true, &non_const);
19040 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19041 cp_parser_require (parser, CPP_EQ, RT_EQ);
19042 if (!cp_parser_parse_definitely (parser))
19043 designator = NULL_TREE;
19044 else if (non_const)
19045 require_potential_rvalue_constant_expression (designator);
19047 else
19048 designator = NULL_TREE;
19050 /* Parse the initializer. */
19051 initializer = cp_parser_initializer_clause (parser,
19052 &clause_non_constant_p);
19053 /* If any clause is non-constant, so is the entire initializer. */
19054 if (clause_non_constant_p)
19055 *non_constant_p = true;
19057 /* If we have an ellipsis, this is an initializer pack
19058 expansion. */
19059 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19061 /* Consume the `...'. */
19062 cp_lexer_consume_token (parser->lexer);
19064 /* Turn the initializer into an initializer expansion. */
19065 initializer = make_pack_expansion (initializer);
19068 /* Add it to the vector. */
19069 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19071 /* If the next token is not a comma, we have reached the end of
19072 the list. */
19073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19074 break;
19076 /* Peek at the next token. */
19077 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19078 /* If the next token is a `}', then we're still done. An
19079 initializer-clause can have a trailing `,' after the
19080 initializer-list and before the closing `}'. */
19081 if (token->type == CPP_CLOSE_BRACE)
19082 break;
19084 /* Consume the `,' token. */
19085 cp_lexer_consume_token (parser->lexer);
19088 return v;
19091 /* Classes [gram.class] */
19093 /* Parse a class-name.
19095 class-name:
19096 identifier
19097 template-id
19099 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19100 to indicate that names looked up in dependent types should be
19101 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19102 keyword has been used to indicate that the name that appears next
19103 is a template. TAG_TYPE indicates the explicit tag given before
19104 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19105 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19106 is the class being defined in a class-head.
19108 Returns the TYPE_DECL representing the class. */
19110 static tree
19111 cp_parser_class_name (cp_parser *parser,
19112 bool typename_keyword_p,
19113 bool template_keyword_p,
19114 enum tag_types tag_type,
19115 bool check_dependency_p,
19116 bool class_head_p,
19117 bool is_declaration)
19119 tree decl;
19120 tree scope;
19121 bool typename_p;
19122 cp_token *token;
19123 tree identifier = NULL_TREE;
19125 /* All class-names start with an identifier. */
19126 token = cp_lexer_peek_token (parser->lexer);
19127 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19129 cp_parser_error (parser, "expected class-name");
19130 return error_mark_node;
19133 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19134 to a template-id, so we save it here. */
19135 scope = parser->scope;
19136 if (scope == error_mark_node)
19137 return error_mark_node;
19139 /* Any name names a type if we're following the `typename' keyword
19140 in a qualified name where the enclosing scope is type-dependent. */
19141 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19142 && dependent_type_p (scope));
19143 /* Handle the common case (an identifier, but not a template-id)
19144 efficiently. */
19145 if (token->type == CPP_NAME
19146 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19148 cp_token *identifier_token;
19149 bool ambiguous_p;
19151 /* Look for the identifier. */
19152 identifier_token = cp_lexer_peek_token (parser->lexer);
19153 ambiguous_p = identifier_token->error_reported;
19154 identifier = cp_parser_identifier (parser);
19155 /* If the next token isn't an identifier, we are certainly not
19156 looking at a class-name. */
19157 if (identifier == error_mark_node)
19158 decl = error_mark_node;
19159 /* If we know this is a type-name, there's no need to look it
19160 up. */
19161 else if (typename_p)
19162 decl = identifier;
19163 else
19165 tree ambiguous_decls;
19166 /* If we already know that this lookup is ambiguous, then
19167 we've already issued an error message; there's no reason
19168 to check again. */
19169 if (ambiguous_p)
19171 cp_parser_simulate_error (parser);
19172 return error_mark_node;
19174 /* If the next token is a `::', then the name must be a type
19175 name.
19177 [basic.lookup.qual]
19179 During the lookup for a name preceding the :: scope
19180 resolution operator, object, function, and enumerator
19181 names are ignored. */
19182 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19183 tag_type = typename_type;
19184 /* Look up the name. */
19185 decl = cp_parser_lookup_name (parser, identifier,
19186 tag_type,
19187 /*is_template=*/false,
19188 /*is_namespace=*/false,
19189 check_dependency_p,
19190 &ambiguous_decls,
19191 identifier_token->location);
19192 if (ambiguous_decls)
19194 if (cp_parser_parsing_tentatively (parser))
19195 cp_parser_simulate_error (parser);
19196 return error_mark_node;
19200 else
19202 /* Try a template-id. */
19203 decl = cp_parser_template_id (parser, template_keyword_p,
19204 check_dependency_p,
19205 tag_type,
19206 is_declaration);
19207 if (decl == error_mark_node)
19208 return error_mark_node;
19211 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19213 /* If this is a typename, create a TYPENAME_TYPE. */
19214 if (typename_p && decl != error_mark_node)
19216 decl = make_typename_type (scope, decl, typename_type,
19217 /*complain=*/tf_error);
19218 if (decl != error_mark_node)
19219 decl = TYPE_NAME (decl);
19222 decl = strip_using_decl (decl);
19224 /* Check to see that it is really the name of a class. */
19225 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19226 && identifier_p (TREE_OPERAND (decl, 0))
19227 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19228 /* Situations like this:
19230 template <typename T> struct A {
19231 typename T::template X<int>::I i;
19234 are problematic. Is `T::template X<int>' a class-name? The
19235 standard does not seem to be definitive, but there is no other
19236 valid interpretation of the following `::'. Therefore, those
19237 names are considered class-names. */
19239 decl = make_typename_type (scope, decl, tag_type, tf_error);
19240 if (decl != error_mark_node)
19241 decl = TYPE_NAME (decl);
19243 else if (TREE_CODE (decl) != TYPE_DECL
19244 || TREE_TYPE (decl) == error_mark_node
19245 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19246 /* In Objective-C 2.0, a classname followed by '.' starts a
19247 dot-syntax expression, and it's not a type-name. */
19248 || (c_dialect_objc ()
19249 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19250 && objc_is_class_name (decl)))
19251 decl = error_mark_node;
19253 if (decl == error_mark_node)
19254 cp_parser_error (parser, "expected class-name");
19255 else if (identifier && !parser->scope)
19256 maybe_note_name_used_in_class (identifier, decl);
19258 return decl;
19261 /* Parse a class-specifier.
19263 class-specifier:
19264 class-head { member-specification [opt] }
19266 Returns the TREE_TYPE representing the class. */
19268 static tree
19269 cp_parser_class_specifier_1 (cp_parser* parser)
19271 tree type;
19272 tree attributes = NULL_TREE;
19273 bool nested_name_specifier_p;
19274 unsigned saved_num_template_parameter_lists;
19275 bool saved_in_function_body;
19276 unsigned char in_statement;
19277 bool in_switch_statement_p;
19278 bool saved_in_unbraced_linkage_specification_p;
19279 tree old_scope = NULL_TREE;
19280 tree scope = NULL_TREE;
19281 cp_token *closing_brace;
19283 push_deferring_access_checks (dk_no_deferred);
19285 /* Parse the class-head. */
19286 type = cp_parser_class_head (parser,
19287 &nested_name_specifier_p);
19288 /* If the class-head was a semantic disaster, skip the entire body
19289 of the class. */
19290 if (!type)
19292 cp_parser_skip_to_end_of_block_or_statement (parser);
19293 pop_deferring_access_checks ();
19294 return error_mark_node;
19297 /* Look for the `{'. */
19298 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19300 pop_deferring_access_checks ();
19301 return error_mark_node;
19304 cp_ensure_no_omp_declare_simd (parser);
19306 /* Issue an error message if type-definitions are forbidden here. */
19307 cp_parser_check_type_definition (parser);
19308 /* Remember that we are defining one more class. */
19309 ++parser->num_classes_being_defined;
19310 /* Inside the class, surrounding template-parameter-lists do not
19311 apply. */
19312 saved_num_template_parameter_lists
19313 = parser->num_template_parameter_lists;
19314 parser->num_template_parameter_lists = 0;
19315 /* We are not in a function body. */
19316 saved_in_function_body = parser->in_function_body;
19317 parser->in_function_body = false;
19318 /* Or in a loop. */
19319 in_statement = parser->in_statement;
19320 parser->in_statement = 0;
19321 /* Or in a switch. */
19322 in_switch_statement_p = parser->in_switch_statement_p;
19323 parser->in_switch_statement_p = false;
19324 /* We are not immediately inside an extern "lang" block. */
19325 saved_in_unbraced_linkage_specification_p
19326 = parser->in_unbraced_linkage_specification_p;
19327 parser->in_unbraced_linkage_specification_p = false;
19329 /* Start the class. */
19330 if (nested_name_specifier_p)
19332 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19333 old_scope = push_inner_scope (scope);
19335 type = begin_class_definition (type);
19337 if (type == error_mark_node)
19338 /* If the type is erroneous, skip the entire body of the class. */
19339 cp_parser_skip_to_closing_brace (parser);
19340 else
19341 /* Parse the member-specification. */
19342 cp_parser_member_specification_opt (parser);
19344 /* Look for the trailing `}'. */
19345 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19346 /* Look for trailing attributes to apply to this class. */
19347 if (cp_parser_allow_gnu_extensions_p (parser))
19348 attributes = cp_parser_gnu_attributes_opt (parser);
19349 if (type != error_mark_node)
19350 type = finish_struct (type, attributes);
19351 if (nested_name_specifier_p)
19352 pop_inner_scope (old_scope, scope);
19354 /* We've finished a type definition. Check for the common syntax
19355 error of forgetting a semicolon after the definition. We need to
19356 be careful, as we can't just check for not-a-semicolon and be done
19357 with it; the user might have typed:
19359 class X { } c = ...;
19360 class X { } *p = ...;
19362 and so forth. Instead, enumerate all the possible tokens that
19363 might follow this production; if we don't see one of them, then
19364 complain and silently insert the semicolon. */
19366 cp_token *token = cp_lexer_peek_token (parser->lexer);
19367 bool want_semicolon = true;
19369 if (cp_next_tokens_can_be_std_attribute_p (parser))
19370 /* Don't try to parse c++11 attributes here. As per the
19371 grammar, that should be a task for
19372 cp_parser_decl_specifier_seq. */
19373 want_semicolon = false;
19375 switch (token->type)
19377 case CPP_NAME:
19378 case CPP_SEMICOLON:
19379 case CPP_MULT:
19380 case CPP_AND:
19381 case CPP_OPEN_PAREN:
19382 case CPP_CLOSE_PAREN:
19383 case CPP_COMMA:
19384 want_semicolon = false;
19385 break;
19387 /* While it's legal for type qualifiers and storage class
19388 specifiers to follow type definitions in the grammar, only
19389 compiler testsuites contain code like that. Assume that if
19390 we see such code, then what we're really seeing is a case
19391 like:
19393 class X { }
19394 const <type> var = ...;
19398 class Y { }
19399 static <type> func (...) ...
19401 i.e. the qualifier or specifier applies to the next
19402 declaration. To do so, however, we need to look ahead one
19403 more token to see if *that* token is a type specifier.
19405 This code could be improved to handle:
19407 class Z { }
19408 static const <type> var = ...; */
19409 case CPP_KEYWORD:
19410 if (keyword_is_decl_specifier (token->keyword))
19412 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19414 /* Handling user-defined types here would be nice, but very
19415 tricky. */
19416 want_semicolon
19417 = (lookahead->type == CPP_KEYWORD
19418 && keyword_begins_type_specifier (lookahead->keyword));
19420 break;
19421 default:
19422 break;
19425 /* If we don't have a type, then something is very wrong and we
19426 shouldn't try to do anything clever. Likewise for not seeing the
19427 closing brace. */
19428 if (closing_brace && TYPE_P (type) && want_semicolon)
19430 cp_token_position prev
19431 = cp_lexer_previous_token_position (parser->lexer);
19432 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19433 location_t loc = prev_token->location;
19435 if (CLASSTYPE_DECLARED_CLASS (type))
19436 error_at (loc, "expected %<;%> after class definition");
19437 else if (TREE_CODE (type) == RECORD_TYPE)
19438 error_at (loc, "expected %<;%> after struct definition");
19439 else if (TREE_CODE (type) == UNION_TYPE)
19440 error_at (loc, "expected %<;%> after union definition");
19441 else
19442 gcc_unreachable ();
19444 /* Unget one token and smash it to look as though we encountered
19445 a semicolon in the input stream. */
19446 cp_lexer_set_token_position (parser->lexer, prev);
19447 token = cp_lexer_peek_token (parser->lexer);
19448 token->type = CPP_SEMICOLON;
19449 token->keyword = RID_MAX;
19453 /* If this class is not itself within the scope of another class,
19454 then we need to parse the bodies of all of the queued function
19455 definitions. Note that the queued functions defined in a class
19456 are not always processed immediately following the
19457 class-specifier for that class. Consider:
19459 struct A {
19460 struct B { void f() { sizeof (A); } };
19463 If `f' were processed before the processing of `A' were
19464 completed, there would be no way to compute the size of `A'.
19465 Note that the nesting we are interested in here is lexical --
19466 not the semantic nesting given by TYPE_CONTEXT. In particular,
19467 for:
19469 struct A { struct B; };
19470 struct A::B { void f() { } };
19472 there is no need to delay the parsing of `A::B::f'. */
19473 if (--parser->num_classes_being_defined == 0)
19475 tree decl;
19476 tree class_type = NULL_TREE;
19477 tree pushed_scope = NULL_TREE;
19478 unsigned ix;
19479 cp_default_arg_entry *e;
19480 tree save_ccp, save_ccr;
19482 /* In a first pass, parse default arguments to the functions.
19483 Then, in a second pass, parse the bodies of the functions.
19484 This two-phased approach handles cases like:
19486 struct S {
19487 void f() { g(); }
19488 void g(int i = 3);
19492 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19494 decl = e->decl;
19495 /* If there are default arguments that have not yet been processed,
19496 take care of them now. */
19497 if (class_type != e->class_type)
19499 if (pushed_scope)
19500 pop_scope (pushed_scope);
19501 class_type = e->class_type;
19502 pushed_scope = push_scope (class_type);
19504 /* Make sure that any template parameters are in scope. */
19505 maybe_begin_member_template_processing (decl);
19506 /* Parse the default argument expressions. */
19507 cp_parser_late_parsing_default_args (parser, decl);
19508 /* Remove any template parameters from the symbol table. */
19509 maybe_end_member_template_processing ();
19511 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19512 /* Now parse any NSDMIs. */
19513 save_ccp = current_class_ptr;
19514 save_ccr = current_class_ref;
19515 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19517 if (class_type != DECL_CONTEXT (decl))
19519 if (pushed_scope)
19520 pop_scope (pushed_scope);
19521 class_type = DECL_CONTEXT (decl);
19522 pushed_scope = push_scope (class_type);
19524 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19525 cp_parser_late_parsing_nsdmi (parser, decl);
19527 vec_safe_truncate (unparsed_nsdmis, 0);
19528 current_class_ptr = save_ccp;
19529 current_class_ref = save_ccr;
19530 if (pushed_scope)
19531 pop_scope (pushed_scope);
19533 /* Now do some post-NSDMI bookkeeping. */
19534 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19535 after_nsdmi_defaulted_late_checks (class_type);
19536 vec_safe_truncate (unparsed_classes, 0);
19537 after_nsdmi_defaulted_late_checks (type);
19539 /* Now parse the body of the functions. */
19540 if (flag_openmp)
19542 /* OpenMP UDRs need to be parsed before all other functions. */
19543 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19544 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19545 cp_parser_late_parsing_for_member (parser, decl);
19546 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19547 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19548 cp_parser_late_parsing_for_member (parser, decl);
19550 else
19551 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19552 cp_parser_late_parsing_for_member (parser, decl);
19553 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19555 else
19556 vec_safe_push (unparsed_classes, type);
19558 /* Put back any saved access checks. */
19559 pop_deferring_access_checks ();
19561 /* Restore saved state. */
19562 parser->in_switch_statement_p = in_switch_statement_p;
19563 parser->in_statement = in_statement;
19564 parser->in_function_body = saved_in_function_body;
19565 parser->num_template_parameter_lists
19566 = saved_num_template_parameter_lists;
19567 parser->in_unbraced_linkage_specification_p
19568 = saved_in_unbraced_linkage_specification_p;
19570 return type;
19573 static tree
19574 cp_parser_class_specifier (cp_parser* parser)
19576 tree ret;
19577 timevar_push (TV_PARSE_STRUCT);
19578 ret = cp_parser_class_specifier_1 (parser);
19579 timevar_pop (TV_PARSE_STRUCT);
19580 return ret;
19583 /* Parse a class-head.
19585 class-head:
19586 class-key identifier [opt] base-clause [opt]
19587 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19588 class-key nested-name-specifier [opt] template-id
19589 base-clause [opt]
19591 class-virt-specifier:
19592 final
19594 GNU Extensions:
19595 class-key attributes identifier [opt] base-clause [opt]
19596 class-key attributes nested-name-specifier identifier base-clause [opt]
19597 class-key attributes nested-name-specifier [opt] template-id
19598 base-clause [opt]
19600 Upon return BASES is initialized to the list of base classes (or
19601 NULL, if there are none) in the same form returned by
19602 cp_parser_base_clause.
19604 Returns the TYPE of the indicated class. Sets
19605 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19606 involving a nested-name-specifier was used, and FALSE otherwise.
19608 Returns error_mark_node if this is not a class-head.
19610 Returns NULL_TREE if the class-head is syntactically valid, but
19611 semantically invalid in a way that means we should skip the entire
19612 body of the class. */
19614 static tree
19615 cp_parser_class_head (cp_parser* parser,
19616 bool* nested_name_specifier_p)
19618 tree nested_name_specifier;
19619 enum tag_types class_key;
19620 tree id = NULL_TREE;
19621 tree type = NULL_TREE;
19622 tree attributes;
19623 tree bases;
19624 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19625 bool template_id_p = false;
19626 bool qualified_p = false;
19627 bool invalid_nested_name_p = false;
19628 bool invalid_explicit_specialization_p = false;
19629 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19630 tree pushed_scope = NULL_TREE;
19631 unsigned num_templates;
19632 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19633 /* Assume no nested-name-specifier will be present. */
19634 *nested_name_specifier_p = false;
19635 /* Assume no template parameter lists will be used in defining the
19636 type. */
19637 num_templates = 0;
19638 parser->colon_corrects_to_scope_p = false;
19640 /* Look for the class-key. */
19641 class_key = cp_parser_class_key (parser);
19642 if (class_key == none_type)
19643 return error_mark_node;
19645 /* Parse the attributes. */
19646 attributes = cp_parser_attributes_opt (parser);
19648 /* If the next token is `::', that is invalid -- but sometimes
19649 people do try to write:
19651 struct ::S {};
19653 Handle this gracefully by accepting the extra qualifier, and then
19654 issuing an error about it later if this really is a
19655 class-head. If it turns out just to be an elaborated type
19656 specifier, remain silent. */
19657 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19658 qualified_p = true;
19660 push_deferring_access_checks (dk_no_check);
19662 /* Determine the name of the class. Begin by looking for an
19663 optional nested-name-specifier. */
19664 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19665 nested_name_specifier
19666 = cp_parser_nested_name_specifier_opt (parser,
19667 /*typename_keyword_p=*/false,
19668 /*check_dependency_p=*/false,
19669 /*type_p=*/true,
19670 /*is_declaration=*/false);
19671 /* If there was a nested-name-specifier, then there *must* be an
19672 identifier. */
19673 if (nested_name_specifier)
19675 type_start_token = cp_lexer_peek_token (parser->lexer);
19676 /* Although the grammar says `identifier', it really means
19677 `class-name' or `template-name'. You are only allowed to
19678 define a class that has already been declared with this
19679 syntax.
19681 The proposed resolution for Core Issue 180 says that wherever
19682 you see `class T::X' you should treat `X' as a type-name.
19684 It is OK to define an inaccessible class; for example:
19686 class A { class B; };
19687 class A::B {};
19689 We do not know if we will see a class-name, or a
19690 template-name. We look for a class-name first, in case the
19691 class-name is a template-id; if we looked for the
19692 template-name first we would stop after the template-name. */
19693 cp_parser_parse_tentatively (parser);
19694 type = cp_parser_class_name (parser,
19695 /*typename_keyword_p=*/false,
19696 /*template_keyword_p=*/false,
19697 class_type,
19698 /*check_dependency_p=*/false,
19699 /*class_head_p=*/true,
19700 /*is_declaration=*/false);
19701 /* If that didn't work, ignore the nested-name-specifier. */
19702 if (!cp_parser_parse_definitely (parser))
19704 invalid_nested_name_p = true;
19705 type_start_token = cp_lexer_peek_token (parser->lexer);
19706 id = cp_parser_identifier (parser);
19707 if (id == error_mark_node)
19708 id = NULL_TREE;
19710 /* If we could not find a corresponding TYPE, treat this
19711 declaration like an unqualified declaration. */
19712 if (type == error_mark_node)
19713 nested_name_specifier = NULL_TREE;
19714 /* Otherwise, count the number of templates used in TYPE and its
19715 containing scopes. */
19716 else
19718 tree scope;
19720 for (scope = TREE_TYPE (type);
19721 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19722 scope = get_containing_scope (scope))
19723 if (TYPE_P (scope)
19724 && CLASS_TYPE_P (scope)
19725 && CLASSTYPE_TEMPLATE_INFO (scope)
19726 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19727 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19728 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19729 ++num_templates;
19732 /* Otherwise, the identifier is optional. */
19733 else
19735 /* We don't know whether what comes next is a template-id,
19736 an identifier, or nothing at all. */
19737 cp_parser_parse_tentatively (parser);
19738 /* Check for a template-id. */
19739 type_start_token = cp_lexer_peek_token (parser->lexer);
19740 id = cp_parser_template_id (parser,
19741 /*template_keyword_p=*/false,
19742 /*check_dependency_p=*/true,
19743 class_key,
19744 /*is_declaration=*/true);
19745 /* If that didn't work, it could still be an identifier. */
19746 if (!cp_parser_parse_definitely (parser))
19748 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19750 type_start_token = cp_lexer_peek_token (parser->lexer);
19751 id = cp_parser_identifier (parser);
19753 else
19754 id = NULL_TREE;
19756 else
19758 template_id_p = true;
19759 ++num_templates;
19763 pop_deferring_access_checks ();
19765 if (id)
19767 cp_parser_check_for_invalid_template_id (parser, id,
19768 class_key,
19769 type_start_token->location);
19771 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19773 /* If it's not a `:' or a `{' then we can't really be looking at a
19774 class-head, since a class-head only appears as part of a
19775 class-specifier. We have to detect this situation before calling
19776 xref_tag, since that has irreversible side-effects. */
19777 if (!cp_parser_next_token_starts_class_definition_p (parser))
19779 cp_parser_error (parser, "expected %<{%> or %<:%>");
19780 type = error_mark_node;
19781 goto out;
19784 /* At this point, we're going ahead with the class-specifier, even
19785 if some other problem occurs. */
19786 cp_parser_commit_to_tentative_parse (parser);
19787 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19789 cp_parser_error (parser,
19790 "cannot specify %<override%> for a class");
19791 type = error_mark_node;
19792 goto out;
19794 /* Issue the error about the overly-qualified name now. */
19795 if (qualified_p)
19797 cp_parser_error (parser,
19798 "global qualification of class name is invalid");
19799 type = error_mark_node;
19800 goto out;
19802 else if (invalid_nested_name_p)
19804 cp_parser_error (parser,
19805 "qualified name does not name a class");
19806 type = error_mark_node;
19807 goto out;
19809 else if (nested_name_specifier)
19811 tree scope;
19813 /* Reject typedef-names in class heads. */
19814 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19816 error_at (type_start_token->location,
19817 "invalid class name in declaration of %qD",
19818 type);
19819 type = NULL_TREE;
19820 goto done;
19823 /* Figure out in what scope the declaration is being placed. */
19824 scope = current_scope ();
19825 /* If that scope does not contain the scope in which the
19826 class was originally declared, the program is invalid. */
19827 if (scope && !is_ancestor (scope, nested_name_specifier))
19829 if (at_namespace_scope_p ())
19830 error_at (type_start_token->location,
19831 "declaration of %qD in namespace %qD which does not "
19832 "enclose %qD",
19833 type, scope, nested_name_specifier);
19834 else
19835 error_at (type_start_token->location,
19836 "declaration of %qD in %qD which does not enclose %qD",
19837 type, scope, nested_name_specifier);
19838 type = NULL_TREE;
19839 goto done;
19841 /* [dcl.meaning]
19843 A declarator-id shall not be qualified except for the
19844 definition of a ... nested class outside of its class
19845 ... [or] the definition or explicit instantiation of a
19846 class member of a namespace outside of its namespace. */
19847 if (scope == nested_name_specifier)
19849 permerror (nested_name_specifier_token_start->location,
19850 "extra qualification not allowed");
19851 nested_name_specifier = NULL_TREE;
19852 num_templates = 0;
19855 /* An explicit-specialization must be preceded by "template <>". If
19856 it is not, try to recover gracefully. */
19857 if (at_namespace_scope_p ()
19858 && parser->num_template_parameter_lists == 0
19859 && template_id_p)
19861 error_at (type_start_token->location,
19862 "an explicit specialization must be preceded by %<template <>%>");
19863 invalid_explicit_specialization_p = true;
19864 /* Take the same action that would have been taken by
19865 cp_parser_explicit_specialization. */
19866 ++parser->num_template_parameter_lists;
19867 begin_specialization ();
19869 /* There must be no "return" statements between this point and the
19870 end of this function; set "type "to the correct return value and
19871 use "goto done;" to return. */
19872 /* Make sure that the right number of template parameters were
19873 present. */
19874 if (!cp_parser_check_template_parameters (parser, num_templates,
19875 type_start_token->location,
19876 /*declarator=*/NULL))
19878 /* If something went wrong, there is no point in even trying to
19879 process the class-definition. */
19880 type = NULL_TREE;
19881 goto done;
19884 /* Look up the type. */
19885 if (template_id_p)
19887 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19888 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19889 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19891 error_at (type_start_token->location,
19892 "function template %qD redeclared as a class template", id);
19893 type = error_mark_node;
19895 else
19897 type = TREE_TYPE (id);
19898 type = maybe_process_partial_specialization (type);
19900 if (nested_name_specifier)
19901 pushed_scope = push_scope (nested_name_specifier);
19903 else if (nested_name_specifier)
19905 tree class_type;
19907 /* Given:
19909 template <typename T> struct S { struct T };
19910 template <typename T> struct S<T>::T { };
19912 we will get a TYPENAME_TYPE when processing the definition of
19913 `S::T'. We need to resolve it to the actual type before we
19914 try to define it. */
19915 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19917 class_type = resolve_typename_type (TREE_TYPE (type),
19918 /*only_current_p=*/false);
19919 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19920 type = TYPE_NAME (class_type);
19921 else
19923 cp_parser_error (parser, "could not resolve typename type");
19924 type = error_mark_node;
19928 if (maybe_process_partial_specialization (TREE_TYPE (type))
19929 == error_mark_node)
19931 type = NULL_TREE;
19932 goto done;
19935 class_type = current_class_type;
19936 /* Enter the scope indicated by the nested-name-specifier. */
19937 pushed_scope = push_scope (nested_name_specifier);
19938 /* Get the canonical version of this type. */
19939 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19940 /* Call push_template_decl if it seems like we should be defining a
19941 template either from the template headers or the type we're
19942 defining, so that we diagnose both extra and missing headers. */
19943 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
19944 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
19945 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
19946 (TREE_TYPE (type)))))
19947 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19949 type = push_template_decl (type);
19950 if (type == error_mark_node)
19952 type = NULL_TREE;
19953 goto done;
19957 type = TREE_TYPE (type);
19958 *nested_name_specifier_p = true;
19960 else /* The name is not a nested name. */
19962 /* If the class was unnamed, create a dummy name. */
19963 if (!id)
19964 id = make_anon_name ();
19965 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19966 parser->num_template_parameter_lists);
19969 /* Indicate whether this class was declared as a `class' or as a
19970 `struct'. */
19971 if (TREE_CODE (type) == RECORD_TYPE)
19972 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19973 cp_parser_check_class_key (class_key, type);
19975 /* If this type was already complete, and we see another definition,
19976 that's an error. */
19977 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19979 error_at (type_start_token->location, "redefinition of %q#T",
19980 type);
19981 error_at (type_start_token->location, "previous definition of %q+#T",
19982 type);
19983 type = NULL_TREE;
19984 goto done;
19986 else if (type == error_mark_node)
19987 type = NULL_TREE;
19989 if (type)
19991 /* Apply attributes now, before any use of the class as a template
19992 argument in its base list. */
19993 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19994 fixup_attribute_variants (type);
19997 /* We will have entered the scope containing the class; the names of
19998 base classes should be looked up in that context. For example:
20000 struct A { struct B {}; struct C; };
20001 struct A::C : B {};
20003 is valid. */
20005 /* Get the list of base-classes, if there is one. */
20006 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20008 /* PR59482: enter the class scope so that base-specifiers are looked
20009 up correctly. */
20010 if (type)
20011 pushclass (type);
20012 bases = cp_parser_base_clause (parser);
20013 /* PR59482: get out of the previously pushed class scope so that the
20014 subsequent pops pop the right thing. */
20015 if (type)
20016 popclass ();
20018 else
20019 bases = NULL_TREE;
20021 /* If we're really defining a class, process the base classes.
20022 If they're invalid, fail. */
20023 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20024 && !xref_basetypes (type, bases))
20025 type = NULL_TREE;
20027 done:
20028 /* Leave the scope given by the nested-name-specifier. We will
20029 enter the class scope itself while processing the members. */
20030 if (pushed_scope)
20031 pop_scope (pushed_scope);
20033 if (invalid_explicit_specialization_p)
20035 end_specialization ();
20036 --parser->num_template_parameter_lists;
20039 if (type)
20040 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20041 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20042 CLASSTYPE_FINAL (type) = 1;
20043 out:
20044 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20045 return type;
20048 /* Parse a class-key.
20050 class-key:
20051 class
20052 struct
20053 union
20055 Returns the kind of class-key specified, or none_type to indicate
20056 error. */
20058 static enum tag_types
20059 cp_parser_class_key (cp_parser* parser)
20061 cp_token *token;
20062 enum tag_types tag_type;
20064 /* Look for the class-key. */
20065 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20066 if (!token)
20067 return none_type;
20069 /* Check to see if the TOKEN is a class-key. */
20070 tag_type = cp_parser_token_is_class_key (token);
20071 if (!tag_type)
20072 cp_parser_error (parser, "expected class-key");
20073 return tag_type;
20076 /* Parse an (optional) member-specification.
20078 member-specification:
20079 member-declaration member-specification [opt]
20080 access-specifier : member-specification [opt] */
20082 static void
20083 cp_parser_member_specification_opt (cp_parser* parser)
20085 while (true)
20087 cp_token *token;
20088 enum rid keyword;
20090 /* Peek at the next token. */
20091 token = cp_lexer_peek_token (parser->lexer);
20092 /* If it's a `}', or EOF then we've seen all the members. */
20093 if (token->type == CPP_CLOSE_BRACE
20094 || token->type == CPP_EOF
20095 || token->type == CPP_PRAGMA_EOL)
20096 break;
20098 /* See if this token is a keyword. */
20099 keyword = token->keyword;
20100 switch (keyword)
20102 case RID_PUBLIC:
20103 case RID_PROTECTED:
20104 case RID_PRIVATE:
20105 /* Consume the access-specifier. */
20106 cp_lexer_consume_token (parser->lexer);
20107 /* Remember which access-specifier is active. */
20108 current_access_specifier = token->u.value;
20109 /* Look for the `:'. */
20110 cp_parser_require (parser, CPP_COLON, RT_COLON);
20111 break;
20113 default:
20114 /* Accept #pragmas at class scope. */
20115 if (token->type == CPP_PRAGMA)
20117 cp_parser_pragma (parser, pragma_member);
20118 break;
20121 /* Otherwise, the next construction must be a
20122 member-declaration. */
20123 cp_parser_member_declaration (parser);
20128 /* Parse a member-declaration.
20130 member-declaration:
20131 decl-specifier-seq [opt] member-declarator-list [opt] ;
20132 function-definition ; [opt]
20133 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20134 using-declaration
20135 template-declaration
20136 alias-declaration
20138 member-declarator-list:
20139 member-declarator
20140 member-declarator-list , member-declarator
20142 member-declarator:
20143 declarator pure-specifier [opt]
20144 declarator constant-initializer [opt]
20145 identifier [opt] : constant-expression
20147 GNU Extensions:
20149 member-declaration:
20150 __extension__ member-declaration
20152 member-declarator:
20153 declarator attributes [opt] pure-specifier [opt]
20154 declarator attributes [opt] constant-initializer [opt]
20155 identifier [opt] attributes [opt] : constant-expression
20157 C++0x Extensions:
20159 member-declaration:
20160 static_assert-declaration */
20162 static void
20163 cp_parser_member_declaration (cp_parser* parser)
20165 cp_decl_specifier_seq decl_specifiers;
20166 tree prefix_attributes;
20167 tree decl;
20168 int declares_class_or_enum;
20169 bool friend_p;
20170 cp_token *token = NULL;
20171 cp_token *decl_spec_token_start = NULL;
20172 cp_token *initializer_token_start = NULL;
20173 int saved_pedantic;
20174 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20176 /* Check for the `__extension__' keyword. */
20177 if (cp_parser_extension_opt (parser, &saved_pedantic))
20179 /* Recurse. */
20180 cp_parser_member_declaration (parser);
20181 /* Restore the old value of the PEDANTIC flag. */
20182 pedantic = saved_pedantic;
20184 return;
20187 /* Check for a template-declaration. */
20188 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20190 /* An explicit specialization here is an error condition, and we
20191 expect the specialization handler to detect and report this. */
20192 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20193 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20194 cp_parser_explicit_specialization (parser);
20195 else
20196 cp_parser_template_declaration (parser, /*member_p=*/true);
20198 return;
20201 /* Check for a using-declaration. */
20202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20204 if (cxx_dialect < cxx11)
20206 /* Parse the using-declaration. */
20207 cp_parser_using_declaration (parser,
20208 /*access_declaration_p=*/false);
20209 return;
20211 else
20213 tree decl;
20214 bool alias_decl_expected;
20215 cp_parser_parse_tentatively (parser);
20216 decl = cp_parser_alias_declaration (parser);
20217 /* Note that if we actually see the '=' token after the
20218 identifier, cp_parser_alias_declaration commits the
20219 tentative parse. In that case, we really expects an
20220 alias-declaration. Otherwise, we expect a using
20221 declaration. */
20222 alias_decl_expected =
20223 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20224 cp_parser_parse_definitely (parser);
20226 if (alias_decl_expected)
20227 finish_member_declaration (decl);
20228 else
20229 cp_parser_using_declaration (parser,
20230 /*access_declaration_p=*/false);
20231 return;
20235 /* Check for @defs. */
20236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20238 tree ivar, member;
20239 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20240 ivar = ivar_chains;
20241 while (ivar)
20243 member = ivar;
20244 ivar = TREE_CHAIN (member);
20245 TREE_CHAIN (member) = NULL_TREE;
20246 finish_member_declaration (member);
20248 return;
20251 /* If the next token is `static_assert' we have a static assertion. */
20252 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20254 cp_parser_static_assert (parser, /*member_p=*/true);
20255 return;
20258 parser->colon_corrects_to_scope_p = false;
20260 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20261 goto out;
20263 /* Parse the decl-specifier-seq. */
20264 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20265 cp_parser_decl_specifier_seq (parser,
20266 CP_PARSER_FLAGS_OPTIONAL,
20267 &decl_specifiers,
20268 &declares_class_or_enum);
20269 /* Check for an invalid type-name. */
20270 if (!decl_specifiers.any_type_specifiers_p
20271 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20272 goto out;
20273 /* If there is no declarator, then the decl-specifier-seq should
20274 specify a type. */
20275 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20277 /* If there was no decl-specifier-seq, and the next token is a
20278 `;', then we have something like:
20280 struct S { ; };
20282 [class.mem]
20284 Each member-declaration shall declare at least one member
20285 name of the class. */
20286 if (!decl_specifiers.any_specifiers_p)
20288 cp_token *token = cp_lexer_peek_token (parser->lexer);
20289 if (!in_system_header_at (token->location))
20290 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20292 else
20294 tree type;
20296 /* See if this declaration is a friend. */
20297 friend_p = cp_parser_friend_p (&decl_specifiers);
20298 /* If there were decl-specifiers, check to see if there was
20299 a class-declaration. */
20300 type = check_tag_decl (&decl_specifiers,
20301 /*explicit_type_instantiation_p=*/false);
20302 /* Nested classes have already been added to the class, but
20303 a `friend' needs to be explicitly registered. */
20304 if (friend_p)
20306 /* If the `friend' keyword was present, the friend must
20307 be introduced with a class-key. */
20308 if (!declares_class_or_enum && cxx_dialect < cxx11)
20309 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20310 "in C++03 a class-key must be used "
20311 "when declaring a friend");
20312 /* In this case:
20314 template <typename T> struct A {
20315 friend struct A<T>::B;
20318 A<T>::B will be represented by a TYPENAME_TYPE, and
20319 therefore not recognized by check_tag_decl. */
20320 if (!type)
20322 type = decl_specifiers.type;
20323 if (type && TREE_CODE (type) == TYPE_DECL)
20324 type = TREE_TYPE (type);
20326 if (!type || !TYPE_P (type))
20327 error_at (decl_spec_token_start->location,
20328 "friend declaration does not name a class or "
20329 "function");
20330 else
20331 make_friend_class (current_class_type, type,
20332 /*complain=*/true);
20334 /* If there is no TYPE, an error message will already have
20335 been issued. */
20336 else if (!type || type == error_mark_node)
20338 /* An anonymous aggregate has to be handled specially; such
20339 a declaration really declares a data member (with a
20340 particular type), as opposed to a nested class. */
20341 else if (ANON_AGGR_TYPE_P (type))
20343 /* C++11 9.5/6. */
20344 if (decl_specifiers.storage_class != sc_none)
20345 error_at (decl_spec_token_start->location,
20346 "a storage class on an anonymous aggregate "
20347 "in class scope is not allowed");
20349 /* Remove constructors and such from TYPE, now that we
20350 know it is an anonymous aggregate. */
20351 fixup_anonymous_aggr (type);
20352 /* And make the corresponding data member. */
20353 decl = build_decl (decl_spec_token_start->location,
20354 FIELD_DECL, NULL_TREE, type);
20355 /* Add it to the class. */
20356 finish_member_declaration (decl);
20358 else
20359 cp_parser_check_access_in_redeclaration
20360 (TYPE_NAME (type),
20361 decl_spec_token_start->location);
20364 else
20366 bool assume_semicolon = false;
20368 /* Clear attributes from the decl_specifiers but keep them
20369 around as prefix attributes that apply them to the entity
20370 being declared. */
20371 prefix_attributes = decl_specifiers.attributes;
20372 decl_specifiers.attributes = NULL_TREE;
20374 /* See if these declarations will be friends. */
20375 friend_p = cp_parser_friend_p (&decl_specifiers);
20377 /* Keep going until we hit the `;' at the end of the
20378 declaration. */
20379 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20381 tree attributes = NULL_TREE;
20382 tree first_attribute;
20384 /* Peek at the next token. */
20385 token = cp_lexer_peek_token (parser->lexer);
20387 /* Check for a bitfield declaration. */
20388 if (token->type == CPP_COLON
20389 || (token->type == CPP_NAME
20390 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20391 == CPP_COLON))
20393 tree identifier;
20394 tree width;
20396 /* Get the name of the bitfield. Note that we cannot just
20397 check TOKEN here because it may have been invalidated by
20398 the call to cp_lexer_peek_nth_token above. */
20399 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20400 identifier = cp_parser_identifier (parser);
20401 else
20402 identifier = NULL_TREE;
20404 /* Consume the `:' token. */
20405 cp_lexer_consume_token (parser->lexer);
20406 /* Get the width of the bitfield. */
20407 width
20408 = cp_parser_constant_expression (parser,
20409 /*allow_non_constant=*/false,
20410 NULL);
20412 /* Look for attributes that apply to the bitfield. */
20413 attributes = cp_parser_attributes_opt (parser);
20414 /* Remember which attributes are prefix attributes and
20415 which are not. */
20416 first_attribute = attributes;
20417 /* Combine the attributes. */
20418 attributes = chainon (prefix_attributes, attributes);
20420 /* Create the bitfield declaration. */
20421 decl = grokbitfield (identifier
20422 ? make_id_declarator (NULL_TREE,
20423 identifier,
20424 sfk_none)
20425 : NULL,
20426 &decl_specifiers,
20427 width,
20428 attributes);
20430 else
20432 cp_declarator *declarator;
20433 tree initializer;
20434 tree asm_specification;
20435 int ctor_dtor_or_conv_p;
20437 /* Parse the declarator. */
20438 declarator
20439 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20440 &ctor_dtor_or_conv_p,
20441 /*parenthesized_p=*/NULL,
20442 /*member_p=*/true);
20444 /* If something went wrong parsing the declarator, make sure
20445 that we at least consume some tokens. */
20446 if (declarator == cp_error_declarator)
20448 /* Skip to the end of the statement. */
20449 cp_parser_skip_to_end_of_statement (parser);
20450 /* If the next token is not a semicolon, that is
20451 probably because we just skipped over the body of
20452 a function. So, we consume a semicolon if
20453 present, but do not issue an error message if it
20454 is not present. */
20455 if (cp_lexer_next_token_is (parser->lexer,
20456 CPP_SEMICOLON))
20457 cp_lexer_consume_token (parser->lexer);
20458 goto out;
20461 if (declares_class_or_enum & 2)
20462 cp_parser_check_for_definition_in_return_type
20463 (declarator, decl_specifiers.type,
20464 decl_specifiers.locations[ds_type_spec]);
20466 /* Look for an asm-specification. */
20467 asm_specification = cp_parser_asm_specification_opt (parser);
20468 /* Look for attributes that apply to the declaration. */
20469 attributes = cp_parser_attributes_opt (parser);
20470 /* Remember which attributes are prefix attributes and
20471 which are not. */
20472 first_attribute = attributes;
20473 /* Combine the attributes. */
20474 attributes = chainon (prefix_attributes, attributes);
20476 /* If it's an `=', then we have a constant-initializer or a
20477 pure-specifier. It is not correct to parse the
20478 initializer before registering the member declaration
20479 since the member declaration should be in scope while
20480 its initializer is processed. However, the rest of the
20481 front end does not yet provide an interface that allows
20482 us to handle this correctly. */
20483 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20485 /* In [class.mem]:
20487 A pure-specifier shall be used only in the declaration of
20488 a virtual function.
20490 A member-declarator can contain a constant-initializer
20491 only if it declares a static member of integral or
20492 enumeration type.
20494 Therefore, if the DECLARATOR is for a function, we look
20495 for a pure-specifier; otherwise, we look for a
20496 constant-initializer. When we call `grokfield', it will
20497 perform more stringent semantics checks. */
20498 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20499 if (function_declarator_p (declarator)
20500 || (decl_specifiers.type
20501 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20502 && declarator->kind == cdk_id
20503 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20504 == FUNCTION_TYPE)))
20505 initializer = cp_parser_pure_specifier (parser);
20506 else if (decl_specifiers.storage_class != sc_static)
20507 initializer = cp_parser_save_nsdmi (parser);
20508 else if (cxx_dialect >= cxx11)
20510 bool nonconst;
20511 /* Don't require a constant rvalue in C++11, since we
20512 might want a reference constant. We'll enforce
20513 constancy later. */
20514 cp_lexer_consume_token (parser->lexer);
20515 /* Parse the initializer. */
20516 initializer = cp_parser_initializer_clause (parser,
20517 &nonconst);
20519 else
20520 /* Parse the initializer. */
20521 initializer = cp_parser_constant_initializer (parser);
20523 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20524 && !function_declarator_p (declarator))
20526 bool x;
20527 if (decl_specifiers.storage_class != sc_static)
20528 initializer = cp_parser_save_nsdmi (parser);
20529 else
20530 initializer = cp_parser_initializer (parser, &x, &x);
20532 /* Otherwise, there is no initializer. */
20533 else
20534 initializer = NULL_TREE;
20536 /* See if we are probably looking at a function
20537 definition. We are certainly not looking at a
20538 member-declarator. Calling `grokfield' has
20539 side-effects, so we must not do it unless we are sure
20540 that we are looking at a member-declarator. */
20541 if (cp_parser_token_starts_function_definition_p
20542 (cp_lexer_peek_token (parser->lexer)))
20544 /* The grammar does not allow a pure-specifier to be
20545 used when a member function is defined. (It is
20546 possible that this fact is an oversight in the
20547 standard, since a pure function may be defined
20548 outside of the class-specifier. */
20549 if (initializer && initializer_token_start)
20550 error_at (initializer_token_start->location,
20551 "pure-specifier on function-definition");
20552 decl = cp_parser_save_member_function_body (parser,
20553 &decl_specifiers,
20554 declarator,
20555 attributes);
20556 if (parser->fully_implicit_function_template_p)
20557 decl = finish_fully_implicit_template (parser, decl);
20558 /* If the member was not a friend, declare it here. */
20559 if (!friend_p)
20560 finish_member_declaration (decl);
20561 /* Peek at the next token. */
20562 token = cp_lexer_peek_token (parser->lexer);
20563 /* If the next token is a semicolon, consume it. */
20564 if (token->type == CPP_SEMICOLON)
20565 cp_lexer_consume_token (parser->lexer);
20566 goto out;
20568 else
20569 if (declarator->kind == cdk_function)
20570 declarator->id_loc = token->location;
20571 /* Create the declaration. */
20572 decl = grokfield (declarator, &decl_specifiers,
20573 initializer, /*init_const_expr_p=*/true,
20574 asm_specification, attributes);
20575 if (parser->fully_implicit_function_template_p)
20577 if (friend_p)
20578 finish_fully_implicit_template (parser, 0);
20579 else
20580 decl = finish_fully_implicit_template (parser, decl);
20584 cp_finalize_omp_declare_simd (parser, decl);
20586 /* Reset PREFIX_ATTRIBUTES. */
20587 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20588 attributes = TREE_CHAIN (attributes);
20589 if (attributes)
20590 TREE_CHAIN (attributes) = NULL_TREE;
20592 /* If there is any qualification still in effect, clear it
20593 now; we will be starting fresh with the next declarator. */
20594 parser->scope = NULL_TREE;
20595 parser->qualifying_scope = NULL_TREE;
20596 parser->object_scope = NULL_TREE;
20597 /* If it's a `,', then there are more declarators. */
20598 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20600 cp_lexer_consume_token (parser->lexer);
20601 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20603 cp_token *token = cp_lexer_previous_token (parser->lexer);
20604 error_at (token->location,
20605 "stray %<,%> at end of member declaration");
20608 /* If the next token isn't a `;', then we have a parse error. */
20609 else if (cp_lexer_next_token_is_not (parser->lexer,
20610 CPP_SEMICOLON))
20612 /* The next token might be a ways away from where the
20613 actual semicolon is missing. Find the previous token
20614 and use that for our error position. */
20615 cp_token *token = cp_lexer_previous_token (parser->lexer);
20616 error_at (token->location,
20617 "expected %<;%> at end of member declaration");
20619 /* Assume that the user meant to provide a semicolon. If
20620 we were to cp_parser_skip_to_end_of_statement, we might
20621 skip to a semicolon inside a member function definition
20622 and issue nonsensical error messages. */
20623 assume_semicolon = true;
20626 if (decl)
20628 /* Add DECL to the list of members. */
20629 if (!friend_p)
20630 finish_member_declaration (decl);
20632 if (TREE_CODE (decl) == FUNCTION_DECL)
20633 cp_parser_save_default_args (parser, decl);
20634 else if (TREE_CODE (decl) == FIELD_DECL
20635 && !DECL_C_BIT_FIELD (decl)
20636 && DECL_INITIAL (decl))
20637 /* Add DECL to the queue of NSDMI to be parsed later. */
20638 vec_safe_push (unparsed_nsdmis, decl);
20641 if (assume_semicolon)
20642 goto out;
20646 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20647 out:
20648 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20651 /* Parse a pure-specifier.
20653 pure-specifier:
20656 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20657 Otherwise, ERROR_MARK_NODE is returned. */
20659 static tree
20660 cp_parser_pure_specifier (cp_parser* parser)
20662 cp_token *token;
20664 /* Look for the `=' token. */
20665 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20666 return error_mark_node;
20667 /* Look for the `0' token. */
20668 token = cp_lexer_peek_token (parser->lexer);
20670 if (token->type == CPP_EOF
20671 || token->type == CPP_PRAGMA_EOL)
20672 return error_mark_node;
20674 cp_lexer_consume_token (parser->lexer);
20676 /* Accept = default or = delete in c++0x mode. */
20677 if (token->keyword == RID_DEFAULT
20678 || token->keyword == RID_DELETE)
20680 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20681 return token->u.value;
20684 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20685 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20687 cp_parser_error (parser,
20688 "invalid pure specifier (only %<= 0%> is allowed)");
20689 cp_parser_skip_to_end_of_statement (parser);
20690 return error_mark_node;
20692 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20694 error_at (token->location, "templates may not be %<virtual%>");
20695 return error_mark_node;
20698 return integer_zero_node;
20701 /* Parse a constant-initializer.
20703 constant-initializer:
20704 = constant-expression
20706 Returns a representation of the constant-expression. */
20708 static tree
20709 cp_parser_constant_initializer (cp_parser* parser)
20711 /* Look for the `=' token. */
20712 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20713 return error_mark_node;
20715 /* It is invalid to write:
20717 struct S { static const int i = { 7 }; };
20720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20722 cp_parser_error (parser,
20723 "a brace-enclosed initializer is not allowed here");
20724 /* Consume the opening brace. */
20725 cp_lexer_consume_token (parser->lexer);
20726 /* Skip the initializer. */
20727 cp_parser_skip_to_closing_brace (parser);
20728 /* Look for the trailing `}'. */
20729 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20731 return error_mark_node;
20734 return cp_parser_constant_expression (parser,
20735 /*allow_non_constant=*/false,
20736 NULL);
20739 /* Derived classes [gram.class.derived] */
20741 /* Parse a base-clause.
20743 base-clause:
20744 : base-specifier-list
20746 base-specifier-list:
20747 base-specifier ... [opt]
20748 base-specifier-list , base-specifier ... [opt]
20750 Returns a TREE_LIST representing the base-classes, in the order in
20751 which they were declared. The representation of each node is as
20752 described by cp_parser_base_specifier.
20754 In the case that no bases are specified, this function will return
20755 NULL_TREE, not ERROR_MARK_NODE. */
20757 static tree
20758 cp_parser_base_clause (cp_parser* parser)
20760 tree bases = NULL_TREE;
20762 /* Look for the `:' that begins the list. */
20763 cp_parser_require (parser, CPP_COLON, RT_COLON);
20765 /* Scan the base-specifier-list. */
20766 while (true)
20768 cp_token *token;
20769 tree base;
20770 bool pack_expansion_p = false;
20772 /* Look for the base-specifier. */
20773 base = cp_parser_base_specifier (parser);
20774 /* Look for the (optional) ellipsis. */
20775 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20777 /* Consume the `...'. */
20778 cp_lexer_consume_token (parser->lexer);
20780 pack_expansion_p = true;
20783 /* Add BASE to the front of the list. */
20784 if (base && base != error_mark_node)
20786 if (pack_expansion_p)
20787 /* Make this a pack expansion type. */
20788 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20790 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20792 TREE_CHAIN (base) = bases;
20793 bases = base;
20796 /* Peek at the next token. */
20797 token = cp_lexer_peek_token (parser->lexer);
20798 /* If it's not a comma, then the list is complete. */
20799 if (token->type != CPP_COMMA)
20800 break;
20801 /* Consume the `,'. */
20802 cp_lexer_consume_token (parser->lexer);
20805 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20806 base class had a qualified name. However, the next name that
20807 appears is certainly not qualified. */
20808 parser->scope = NULL_TREE;
20809 parser->qualifying_scope = NULL_TREE;
20810 parser->object_scope = NULL_TREE;
20812 return nreverse (bases);
20815 /* Parse a base-specifier.
20817 base-specifier:
20818 :: [opt] nested-name-specifier [opt] class-name
20819 virtual access-specifier [opt] :: [opt] nested-name-specifier
20820 [opt] class-name
20821 access-specifier virtual [opt] :: [opt] nested-name-specifier
20822 [opt] class-name
20824 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20825 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20826 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20827 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20829 static tree
20830 cp_parser_base_specifier (cp_parser* parser)
20832 cp_token *token;
20833 bool done = false;
20834 bool virtual_p = false;
20835 bool duplicate_virtual_error_issued_p = false;
20836 bool duplicate_access_error_issued_p = false;
20837 bool class_scope_p, template_p;
20838 tree access = access_default_node;
20839 tree type;
20841 /* Process the optional `virtual' and `access-specifier'. */
20842 while (!done)
20844 /* Peek at the next token. */
20845 token = cp_lexer_peek_token (parser->lexer);
20846 /* Process `virtual'. */
20847 switch (token->keyword)
20849 case RID_VIRTUAL:
20850 /* If `virtual' appears more than once, issue an error. */
20851 if (virtual_p && !duplicate_virtual_error_issued_p)
20853 cp_parser_error (parser,
20854 "%<virtual%> specified more than once in base-specified");
20855 duplicate_virtual_error_issued_p = true;
20858 virtual_p = true;
20860 /* Consume the `virtual' token. */
20861 cp_lexer_consume_token (parser->lexer);
20863 break;
20865 case RID_PUBLIC:
20866 case RID_PROTECTED:
20867 case RID_PRIVATE:
20868 /* If more than one access specifier appears, issue an
20869 error. */
20870 if (access != access_default_node
20871 && !duplicate_access_error_issued_p)
20873 cp_parser_error (parser,
20874 "more than one access specifier in base-specified");
20875 duplicate_access_error_issued_p = true;
20878 access = ridpointers[(int) token->keyword];
20880 /* Consume the access-specifier. */
20881 cp_lexer_consume_token (parser->lexer);
20883 break;
20885 default:
20886 done = true;
20887 break;
20890 /* It is not uncommon to see programs mechanically, erroneously, use
20891 the 'typename' keyword to denote (dependent) qualified types
20892 as base classes. */
20893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20895 token = cp_lexer_peek_token (parser->lexer);
20896 if (!processing_template_decl)
20897 error_at (token->location,
20898 "keyword %<typename%> not allowed outside of templates");
20899 else
20900 error_at (token->location,
20901 "keyword %<typename%> not allowed in this context "
20902 "(the base class is implicitly a type)");
20903 cp_lexer_consume_token (parser->lexer);
20906 /* Look for the optional `::' operator. */
20907 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20908 /* Look for the nested-name-specifier. The simplest way to
20909 implement:
20911 [temp.res]
20913 The keyword `typename' is not permitted in a base-specifier or
20914 mem-initializer; in these contexts a qualified name that
20915 depends on a template-parameter is implicitly assumed to be a
20916 type name.
20918 is to pretend that we have seen the `typename' keyword at this
20919 point. */
20920 cp_parser_nested_name_specifier_opt (parser,
20921 /*typename_keyword_p=*/true,
20922 /*check_dependency_p=*/true,
20923 typename_type,
20924 /*is_declaration=*/true);
20925 /* If the base class is given by a qualified name, assume that names
20926 we see are type names or templates, as appropriate. */
20927 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20928 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20930 if (!parser->scope
20931 && cp_lexer_next_token_is_decltype (parser->lexer))
20932 /* DR 950 allows decltype as a base-specifier. */
20933 type = cp_parser_decltype (parser);
20934 else
20936 /* Otherwise, look for the class-name. */
20937 type = cp_parser_class_name (parser,
20938 class_scope_p,
20939 template_p,
20940 typename_type,
20941 /*check_dependency_p=*/true,
20942 /*class_head_p=*/false,
20943 /*is_declaration=*/true);
20944 type = TREE_TYPE (type);
20947 if (type == error_mark_node)
20948 return error_mark_node;
20950 return finish_base_specifier (type, access, virtual_p);
20953 /* Exception handling [gram.exception] */
20955 /* Parse an (optional) noexcept-specification.
20957 noexcept-specification:
20958 noexcept ( constant-expression ) [opt]
20960 If no noexcept-specification is present, returns NULL_TREE.
20961 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20962 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20963 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20964 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20965 in which case a boolean condition is returned instead. */
20967 static tree
20968 cp_parser_noexcept_specification_opt (cp_parser* parser,
20969 bool require_constexpr,
20970 bool* consumed_expr,
20971 bool return_cond)
20973 cp_token *token;
20974 const char *saved_message;
20976 /* Peek at the next token. */
20977 token = cp_lexer_peek_token (parser->lexer);
20979 /* Is it a noexcept-specification? */
20980 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20982 tree expr;
20983 cp_lexer_consume_token (parser->lexer);
20985 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20987 cp_lexer_consume_token (parser->lexer);
20989 if (require_constexpr)
20991 /* Types may not be defined in an exception-specification. */
20992 saved_message = parser->type_definition_forbidden_message;
20993 parser->type_definition_forbidden_message
20994 = G_("types may not be defined in an exception-specification");
20996 expr = cp_parser_constant_expression (parser, false, NULL);
20998 /* Restore the saved message. */
20999 parser->type_definition_forbidden_message = saved_message;
21001 else
21003 expr = cp_parser_expression (parser, false, NULL);
21004 *consumed_expr = true;
21007 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21009 else
21011 expr = boolean_true_node;
21012 if (!require_constexpr)
21013 *consumed_expr = false;
21016 /* We cannot build a noexcept-spec right away because this will check
21017 that expr is a constexpr. */
21018 if (!return_cond)
21019 return build_noexcept_spec (expr, tf_warning_or_error);
21020 else
21021 return expr;
21023 else
21024 return NULL_TREE;
21027 /* Parse an (optional) exception-specification.
21029 exception-specification:
21030 throw ( type-id-list [opt] )
21032 Returns a TREE_LIST representing the exception-specification. The
21033 TREE_VALUE of each node is a type. */
21035 static tree
21036 cp_parser_exception_specification_opt (cp_parser* parser)
21038 cp_token *token;
21039 tree type_id_list;
21040 const char *saved_message;
21042 /* Peek at the next token. */
21043 token = cp_lexer_peek_token (parser->lexer);
21045 /* Is it a noexcept-specification? */
21046 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21047 false);
21048 if (type_id_list != NULL_TREE)
21049 return type_id_list;
21051 /* If it's not `throw', then there's no exception-specification. */
21052 if (!cp_parser_is_keyword (token, RID_THROW))
21053 return NULL_TREE;
21055 #if 0
21056 /* Enable this once a lot of code has transitioned to noexcept? */
21057 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21058 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21059 "deprecated in C++0x; use %<noexcept%> instead");
21060 #endif
21062 /* Consume the `throw'. */
21063 cp_lexer_consume_token (parser->lexer);
21065 /* Look for the `('. */
21066 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21068 /* Peek at the next token. */
21069 token = cp_lexer_peek_token (parser->lexer);
21070 /* If it's not a `)', then there is a type-id-list. */
21071 if (token->type != CPP_CLOSE_PAREN)
21073 /* Types may not be defined in an exception-specification. */
21074 saved_message = parser->type_definition_forbidden_message;
21075 parser->type_definition_forbidden_message
21076 = G_("types may not be defined in an exception-specification");
21077 /* Parse the type-id-list. */
21078 type_id_list = cp_parser_type_id_list (parser);
21079 /* Restore the saved message. */
21080 parser->type_definition_forbidden_message = saved_message;
21082 else
21083 type_id_list = empty_except_spec;
21085 /* Look for the `)'. */
21086 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21088 return type_id_list;
21091 /* Parse an (optional) type-id-list.
21093 type-id-list:
21094 type-id ... [opt]
21095 type-id-list , type-id ... [opt]
21097 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21098 in the order that the types were presented. */
21100 static tree
21101 cp_parser_type_id_list (cp_parser* parser)
21103 tree types = NULL_TREE;
21105 while (true)
21107 cp_token *token;
21108 tree type;
21110 /* Get the next type-id. */
21111 type = cp_parser_type_id (parser);
21112 /* Parse the optional ellipsis. */
21113 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21115 /* Consume the `...'. */
21116 cp_lexer_consume_token (parser->lexer);
21118 /* Turn the type into a pack expansion expression. */
21119 type = make_pack_expansion (type);
21121 /* Add it to the list. */
21122 types = add_exception_specifier (types, type, /*complain=*/1);
21123 /* Peek at the next token. */
21124 token = cp_lexer_peek_token (parser->lexer);
21125 /* If it is not a `,', we are done. */
21126 if (token->type != CPP_COMMA)
21127 break;
21128 /* Consume the `,'. */
21129 cp_lexer_consume_token (parser->lexer);
21132 return nreverse (types);
21135 /* Parse a try-block.
21137 try-block:
21138 try compound-statement handler-seq */
21140 static tree
21141 cp_parser_try_block (cp_parser* parser)
21143 tree try_block;
21145 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21146 try_block = begin_try_block ();
21147 cp_parser_compound_statement (parser, NULL, true, false);
21148 finish_try_block (try_block);
21149 cp_parser_handler_seq (parser);
21150 finish_handler_sequence (try_block);
21152 return try_block;
21155 /* Parse a function-try-block.
21157 function-try-block:
21158 try ctor-initializer [opt] function-body handler-seq */
21160 static bool
21161 cp_parser_function_try_block (cp_parser* parser)
21163 tree compound_stmt;
21164 tree try_block;
21165 bool ctor_initializer_p;
21167 /* Look for the `try' keyword. */
21168 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21169 return false;
21170 /* Let the rest of the front end know where we are. */
21171 try_block = begin_function_try_block (&compound_stmt);
21172 /* Parse the function-body. */
21173 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21174 (parser, /*in_function_try_block=*/true);
21175 /* We're done with the `try' part. */
21176 finish_function_try_block (try_block);
21177 /* Parse the handlers. */
21178 cp_parser_handler_seq (parser);
21179 /* We're done with the handlers. */
21180 finish_function_handler_sequence (try_block, compound_stmt);
21182 return ctor_initializer_p;
21185 /* Parse a handler-seq.
21187 handler-seq:
21188 handler handler-seq [opt] */
21190 static void
21191 cp_parser_handler_seq (cp_parser* parser)
21193 while (true)
21195 cp_token *token;
21197 /* Parse the handler. */
21198 cp_parser_handler (parser);
21199 /* Peek at the next token. */
21200 token = cp_lexer_peek_token (parser->lexer);
21201 /* If it's not `catch' then there are no more handlers. */
21202 if (!cp_parser_is_keyword (token, RID_CATCH))
21203 break;
21207 /* Parse a handler.
21209 handler:
21210 catch ( exception-declaration ) compound-statement */
21212 static void
21213 cp_parser_handler (cp_parser* parser)
21215 tree handler;
21216 tree declaration;
21218 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21219 handler = begin_handler ();
21220 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21221 declaration = cp_parser_exception_declaration (parser);
21222 finish_handler_parms (declaration, handler);
21223 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21224 cp_parser_compound_statement (parser, NULL, false, false);
21225 finish_handler (handler);
21228 /* Parse an exception-declaration.
21230 exception-declaration:
21231 type-specifier-seq declarator
21232 type-specifier-seq abstract-declarator
21233 type-specifier-seq
21236 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21237 ellipsis variant is used. */
21239 static tree
21240 cp_parser_exception_declaration (cp_parser* parser)
21242 cp_decl_specifier_seq type_specifiers;
21243 cp_declarator *declarator;
21244 const char *saved_message;
21246 /* If it's an ellipsis, it's easy to handle. */
21247 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21249 /* Consume the `...' token. */
21250 cp_lexer_consume_token (parser->lexer);
21251 return NULL_TREE;
21254 /* Types may not be defined in exception-declarations. */
21255 saved_message = parser->type_definition_forbidden_message;
21256 parser->type_definition_forbidden_message
21257 = G_("types may not be defined in exception-declarations");
21259 /* Parse the type-specifier-seq. */
21260 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21261 /*is_trailing_return=*/false,
21262 &type_specifiers);
21263 /* If it's a `)', then there is no declarator. */
21264 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21265 declarator = NULL;
21266 else
21267 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21268 /*ctor_dtor_or_conv_p=*/NULL,
21269 /*parenthesized_p=*/NULL,
21270 /*member_p=*/false);
21272 /* Restore the saved message. */
21273 parser->type_definition_forbidden_message = saved_message;
21275 if (!type_specifiers.any_specifiers_p)
21276 return error_mark_node;
21278 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21281 /* Parse a throw-expression.
21283 throw-expression:
21284 throw assignment-expression [opt]
21286 Returns a THROW_EXPR representing the throw-expression. */
21288 static tree
21289 cp_parser_throw_expression (cp_parser* parser)
21291 tree expression;
21292 cp_token* token;
21294 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21295 token = cp_lexer_peek_token (parser->lexer);
21296 /* Figure out whether or not there is an assignment-expression
21297 following the "throw" keyword. */
21298 if (token->type == CPP_COMMA
21299 || token->type == CPP_SEMICOLON
21300 || token->type == CPP_CLOSE_PAREN
21301 || token->type == CPP_CLOSE_SQUARE
21302 || token->type == CPP_CLOSE_BRACE
21303 || token->type == CPP_COLON)
21304 expression = NULL_TREE;
21305 else
21306 expression = cp_parser_assignment_expression (parser,
21307 /*cast_p=*/false, NULL);
21309 return build_throw (expression);
21312 /* GNU Extensions */
21314 /* Parse an (optional) asm-specification.
21316 asm-specification:
21317 asm ( string-literal )
21319 If the asm-specification is present, returns a STRING_CST
21320 corresponding to the string-literal. Otherwise, returns
21321 NULL_TREE. */
21323 static tree
21324 cp_parser_asm_specification_opt (cp_parser* parser)
21326 cp_token *token;
21327 tree asm_specification;
21329 /* Peek at the next token. */
21330 token = cp_lexer_peek_token (parser->lexer);
21331 /* If the next token isn't the `asm' keyword, then there's no
21332 asm-specification. */
21333 if (!cp_parser_is_keyword (token, RID_ASM))
21334 return NULL_TREE;
21336 /* Consume the `asm' token. */
21337 cp_lexer_consume_token (parser->lexer);
21338 /* Look for the `('. */
21339 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21341 /* Look for the string-literal. */
21342 asm_specification = cp_parser_string_literal (parser, false, false);
21344 /* Look for the `)'. */
21345 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21347 return asm_specification;
21350 /* Parse an asm-operand-list.
21352 asm-operand-list:
21353 asm-operand
21354 asm-operand-list , asm-operand
21356 asm-operand:
21357 string-literal ( expression )
21358 [ string-literal ] string-literal ( expression )
21360 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21361 each node is the expression. The TREE_PURPOSE is itself a
21362 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21363 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21364 is a STRING_CST for the string literal before the parenthesis. Returns
21365 ERROR_MARK_NODE if any of the operands are invalid. */
21367 static tree
21368 cp_parser_asm_operand_list (cp_parser* parser)
21370 tree asm_operands = NULL_TREE;
21371 bool invalid_operands = false;
21373 while (true)
21375 tree string_literal;
21376 tree expression;
21377 tree name;
21379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21381 /* Consume the `[' token. */
21382 cp_lexer_consume_token (parser->lexer);
21383 /* Read the operand name. */
21384 name = cp_parser_identifier (parser);
21385 if (name != error_mark_node)
21386 name = build_string (IDENTIFIER_LENGTH (name),
21387 IDENTIFIER_POINTER (name));
21388 /* Look for the closing `]'. */
21389 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21391 else
21392 name = NULL_TREE;
21393 /* Look for the string-literal. */
21394 string_literal = cp_parser_string_literal (parser, false, false);
21396 /* Look for the `('. */
21397 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21398 /* Parse the expression. */
21399 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21400 /* Look for the `)'. */
21401 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21403 if (name == error_mark_node
21404 || string_literal == error_mark_node
21405 || expression == error_mark_node)
21406 invalid_operands = true;
21408 /* Add this operand to the list. */
21409 asm_operands = tree_cons (build_tree_list (name, string_literal),
21410 expression,
21411 asm_operands);
21412 /* If the next token is not a `,', there are no more
21413 operands. */
21414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21415 break;
21416 /* Consume the `,'. */
21417 cp_lexer_consume_token (parser->lexer);
21420 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21423 /* Parse an asm-clobber-list.
21425 asm-clobber-list:
21426 string-literal
21427 asm-clobber-list , string-literal
21429 Returns a TREE_LIST, indicating the clobbers in the order that they
21430 appeared. The TREE_VALUE of each node is a STRING_CST. */
21432 static tree
21433 cp_parser_asm_clobber_list (cp_parser* parser)
21435 tree clobbers = NULL_TREE;
21437 while (true)
21439 tree string_literal;
21441 /* Look for the string literal. */
21442 string_literal = cp_parser_string_literal (parser, false, false);
21443 /* Add it to the list. */
21444 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21445 /* If the next token is not a `,', then the list is
21446 complete. */
21447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21448 break;
21449 /* Consume the `,' token. */
21450 cp_lexer_consume_token (parser->lexer);
21453 return clobbers;
21456 /* Parse an asm-label-list.
21458 asm-label-list:
21459 identifier
21460 asm-label-list , identifier
21462 Returns a TREE_LIST, indicating the labels in the order that they
21463 appeared. The TREE_VALUE of each node is a label. */
21465 static tree
21466 cp_parser_asm_label_list (cp_parser* parser)
21468 tree labels = NULL_TREE;
21470 while (true)
21472 tree identifier, label, name;
21474 /* Look for the identifier. */
21475 identifier = cp_parser_identifier (parser);
21476 if (!error_operand_p (identifier))
21478 label = lookup_label (identifier);
21479 if (TREE_CODE (label) == LABEL_DECL)
21481 TREE_USED (label) = 1;
21482 check_goto (label);
21483 name = build_string (IDENTIFIER_LENGTH (identifier),
21484 IDENTIFIER_POINTER (identifier));
21485 labels = tree_cons (name, label, labels);
21488 /* If the next token is not a `,', then the list is
21489 complete. */
21490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21491 break;
21492 /* Consume the `,' token. */
21493 cp_lexer_consume_token (parser->lexer);
21496 return nreverse (labels);
21499 /* Return TRUE iff the next tokens in the stream are possibly the
21500 beginning of a GNU extension attribute. */
21502 static bool
21503 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21505 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21508 /* Return TRUE iff the next tokens in the stream are possibly the
21509 beginning of a standard C++-11 attribute specifier. */
21511 static bool
21512 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21514 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21517 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21518 beginning of a standard C++-11 attribute specifier. */
21520 static bool
21521 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21523 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21525 return (cxx_dialect >= cxx11
21526 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21527 || (token->type == CPP_OPEN_SQUARE
21528 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21529 && token->type == CPP_OPEN_SQUARE)));
21532 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21533 beginning of a GNU extension attribute. */
21535 static bool
21536 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21538 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21540 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21543 /* Return true iff the next tokens can be the beginning of either a
21544 GNU attribute list, or a standard C++11 attribute sequence. */
21546 static bool
21547 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21549 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21550 || cp_next_tokens_can_be_std_attribute_p (parser));
21553 /* Return true iff the next Nth tokens can be the beginning of either
21554 a GNU attribute list, or a standard C++11 attribute sequence. */
21556 static bool
21557 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21559 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21560 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21563 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21564 of GNU attributes, or return NULL. */
21566 static tree
21567 cp_parser_attributes_opt (cp_parser *parser)
21569 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21570 return cp_parser_gnu_attributes_opt (parser);
21571 return cp_parser_std_attribute_spec_seq (parser);
21574 #define CILK_SIMD_FN_CLAUSE_MASK \
21575 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21576 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21577 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21578 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21579 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21581 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21582 vector [(<clauses>)] */
21584 static void
21585 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21587 bool first_p = parser->cilk_simd_fn_info == NULL;
21588 cp_token *token = v_token;
21589 if (first_p)
21591 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21592 parser->cilk_simd_fn_info->error_seen = false;
21593 parser->cilk_simd_fn_info->fndecl_seen = false;
21594 parser->cilk_simd_fn_info->tokens = vNULL;
21596 int paren_scope = 0;
21597 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21599 cp_lexer_consume_token (parser->lexer);
21600 v_token = cp_lexer_peek_token (parser->lexer);
21601 paren_scope++;
21603 while (paren_scope > 0)
21605 token = cp_lexer_peek_token (parser->lexer);
21606 if (token->type == CPP_OPEN_PAREN)
21607 paren_scope++;
21608 else if (token->type == CPP_CLOSE_PAREN)
21609 paren_scope--;
21610 /* Do not push the last ')' */
21611 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21612 cp_lexer_consume_token (parser->lexer);
21615 token->type = CPP_PRAGMA_EOL;
21616 parser->lexer->next_token = token;
21617 cp_lexer_consume_token (parser->lexer);
21619 struct cp_token_cache *cp
21620 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21621 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21624 /* Parse an (optional) series of attributes.
21626 attributes:
21627 attributes attribute
21629 attribute:
21630 __attribute__ (( attribute-list [opt] ))
21632 The return value is as for cp_parser_gnu_attribute_list. */
21634 static tree
21635 cp_parser_gnu_attributes_opt (cp_parser* parser)
21637 tree attributes = NULL_TREE;
21639 while (true)
21641 cp_token *token;
21642 tree attribute_list;
21643 bool ok = true;
21645 /* Peek at the next token. */
21646 token = cp_lexer_peek_token (parser->lexer);
21647 /* If it's not `__attribute__', then we're done. */
21648 if (token->keyword != RID_ATTRIBUTE)
21649 break;
21651 /* Consume the `__attribute__' keyword. */
21652 cp_lexer_consume_token (parser->lexer);
21653 /* Look for the two `(' tokens. */
21654 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21655 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21657 /* Peek at the next token. */
21658 token = cp_lexer_peek_token (parser->lexer);
21659 if (token->type != CPP_CLOSE_PAREN)
21660 /* Parse the attribute-list. */
21661 attribute_list = cp_parser_gnu_attribute_list (parser);
21662 else
21663 /* If the next token is a `)', then there is no attribute
21664 list. */
21665 attribute_list = NULL;
21667 /* Look for the two `)' tokens. */
21668 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21669 ok = false;
21670 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21671 ok = false;
21672 if (!ok)
21673 cp_parser_skip_to_end_of_statement (parser);
21675 /* Add these new attributes to the list. */
21676 attributes = chainon (attributes, attribute_list);
21679 return attributes;
21682 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21683 "__vector" or "__vector__." */
21685 static inline bool
21686 is_cilkplus_vector_p (tree name)
21688 if (flag_cilkplus && is_attribute_p ("vector", name))
21689 return true;
21690 return false;
21693 /* Parse a GNU attribute-list.
21695 attribute-list:
21696 attribute
21697 attribute-list , attribute
21699 attribute:
21700 identifier
21701 identifier ( identifier )
21702 identifier ( identifier , expression-list )
21703 identifier ( expression-list )
21705 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21706 to an attribute. The TREE_PURPOSE of each node is the identifier
21707 indicating which attribute is in use. The TREE_VALUE represents
21708 the arguments, if any. */
21710 static tree
21711 cp_parser_gnu_attribute_list (cp_parser* parser)
21713 tree attribute_list = NULL_TREE;
21714 bool save_translate_strings_p = parser->translate_strings_p;
21716 parser->translate_strings_p = false;
21717 while (true)
21719 cp_token *token;
21720 tree identifier;
21721 tree attribute;
21723 /* Look for the identifier. We also allow keywords here; for
21724 example `__attribute__ ((const))' is legal. */
21725 token = cp_lexer_peek_token (parser->lexer);
21726 if (token->type == CPP_NAME
21727 || token->type == CPP_KEYWORD)
21729 tree arguments = NULL_TREE;
21731 /* Consume the token, but save it since we need it for the
21732 SIMD enabled function parsing. */
21733 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21735 /* Save away the identifier that indicates which attribute
21736 this is. */
21737 identifier = (token->type == CPP_KEYWORD)
21738 /* For keywords, use the canonical spelling, not the
21739 parsed identifier. */
21740 ? ridpointers[(int) token->keyword]
21741 : id_token->u.value;
21743 attribute = build_tree_list (identifier, NULL_TREE);
21745 /* Peek at the next token. */
21746 token = cp_lexer_peek_token (parser->lexer);
21747 /* If it's an `(', then parse the attribute arguments. */
21748 if (token->type == CPP_OPEN_PAREN)
21750 vec<tree, va_gc> *vec;
21751 int attr_flag = (attribute_takes_identifier_p (identifier)
21752 ? id_attr : normal_attr);
21753 if (is_cilkplus_vector_p (identifier))
21755 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21756 continue;
21758 else
21759 vec = cp_parser_parenthesized_expression_list
21760 (parser, attr_flag, /*cast_p=*/false,
21761 /*allow_expansion_p=*/false,
21762 /*non_constant_p=*/NULL);
21763 if (vec == NULL)
21764 arguments = error_mark_node;
21765 else
21767 arguments = build_tree_list_vec (vec);
21768 release_tree_vector (vec);
21770 /* Save the arguments away. */
21771 TREE_VALUE (attribute) = arguments;
21773 else if (is_cilkplus_vector_p (identifier))
21775 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21776 continue;
21779 if (arguments != error_mark_node)
21781 /* Add this attribute to the list. */
21782 TREE_CHAIN (attribute) = attribute_list;
21783 attribute_list = attribute;
21786 token = cp_lexer_peek_token (parser->lexer);
21788 /* Now, look for more attributes. If the next token isn't a
21789 `,', we're done. */
21790 if (token->type != CPP_COMMA)
21791 break;
21793 /* Consume the comma and keep going. */
21794 cp_lexer_consume_token (parser->lexer);
21796 parser->translate_strings_p = save_translate_strings_p;
21798 /* We built up the list in reverse order. */
21799 return nreverse (attribute_list);
21802 /* Parse a standard C++11 attribute.
21804 The returned representation is a TREE_LIST which TREE_PURPOSE is
21805 the scoped name of the attribute, and the TREE_VALUE is its
21806 arguments list.
21808 Note that the scoped name of the attribute is itself a TREE_LIST
21809 which TREE_PURPOSE is the namespace of the attribute, and
21810 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21811 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21812 and which TREE_PURPOSE is directly the attribute name.
21814 Clients of the attribute code should use get_attribute_namespace
21815 and get_attribute_name to get the actual namespace and name of
21816 attributes, regardless of their being GNU or C++11 attributes.
21818 attribute:
21819 attribute-token attribute-argument-clause [opt]
21821 attribute-token:
21822 identifier
21823 attribute-scoped-token
21825 attribute-scoped-token:
21826 attribute-namespace :: identifier
21828 attribute-namespace:
21829 identifier
21831 attribute-argument-clause:
21832 ( balanced-token-seq )
21834 balanced-token-seq:
21835 balanced-token [opt]
21836 balanced-token-seq balanced-token
21838 balanced-token:
21839 ( balanced-token-seq )
21840 [ balanced-token-seq ]
21841 { balanced-token-seq }. */
21843 static tree
21844 cp_parser_std_attribute (cp_parser *parser)
21846 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21847 cp_token *token;
21849 /* First, parse name of the the attribute, a.k.a
21850 attribute-token. */
21852 token = cp_lexer_peek_token (parser->lexer);
21853 if (token->type == CPP_NAME)
21854 attr_id = token->u.value;
21855 else if (token->type == CPP_KEYWORD)
21856 attr_id = ridpointers[(int) token->keyword];
21857 else if (token->flags & NAMED_OP)
21858 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21860 if (attr_id == NULL_TREE)
21861 return NULL_TREE;
21863 cp_lexer_consume_token (parser->lexer);
21865 token = cp_lexer_peek_token (parser->lexer);
21866 if (token->type == CPP_SCOPE)
21868 /* We are seeing a scoped attribute token. */
21870 cp_lexer_consume_token (parser->lexer);
21871 attr_ns = attr_id;
21873 token = cp_lexer_consume_token (parser->lexer);
21874 if (token->type == CPP_NAME)
21875 attr_id = token->u.value;
21876 else if (token->type == CPP_KEYWORD)
21877 attr_id = ridpointers[(int) token->keyword];
21878 else
21880 error_at (token->location,
21881 "expected an identifier for the attribute name");
21882 return error_mark_node;
21884 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21885 NULL_TREE);
21886 token = cp_lexer_peek_token (parser->lexer);
21888 else
21890 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21891 NULL_TREE);
21892 /* C++11 noreturn attribute is equivalent to GNU's. */
21893 if (is_attribute_p ("noreturn", attr_id))
21894 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21895 /* C++14 deprecated attribute is equivalent to GNU's. */
21896 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21897 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21900 /* Now parse the optional argument clause of the attribute. */
21902 if (token->type != CPP_OPEN_PAREN)
21903 return attribute;
21906 vec<tree, va_gc> *vec;
21907 int attr_flag = normal_attr;
21909 if (attr_ns == get_identifier ("gnu")
21910 && attribute_takes_identifier_p (attr_id))
21911 /* A GNU attribute that takes an identifier in parameter. */
21912 attr_flag = id_attr;
21914 vec = cp_parser_parenthesized_expression_list
21915 (parser, attr_flag, /*cast_p=*/false,
21916 /*allow_expansion_p=*/true,
21917 /*non_constant_p=*/NULL);
21918 if (vec == NULL)
21919 arguments = error_mark_node;
21920 else
21922 arguments = build_tree_list_vec (vec);
21923 release_tree_vector (vec);
21926 if (arguments == error_mark_node)
21927 attribute = error_mark_node;
21928 else
21929 TREE_VALUE (attribute) = arguments;
21932 return attribute;
21935 /* Parse a list of standard C++-11 attributes.
21937 attribute-list:
21938 attribute [opt]
21939 attribute-list , attribute[opt]
21940 attribute ...
21941 attribute-list , attribute ...
21944 static tree
21945 cp_parser_std_attribute_list (cp_parser *parser)
21947 tree attributes = NULL_TREE, attribute = NULL_TREE;
21948 cp_token *token = NULL;
21950 while (true)
21952 attribute = cp_parser_std_attribute (parser);
21953 if (attribute == error_mark_node)
21954 break;
21955 if (attribute != NULL_TREE)
21957 TREE_CHAIN (attribute) = attributes;
21958 attributes = attribute;
21960 token = cp_lexer_peek_token (parser->lexer);
21961 if (token->type != CPP_COMMA)
21962 break;
21963 cp_lexer_consume_token (parser->lexer);
21965 attributes = nreverse (attributes);
21966 return attributes;
21969 /* Parse a standard C++-11 attribute specifier.
21971 attribute-specifier:
21972 [ [ attribute-list ] ]
21973 alignment-specifier
21975 alignment-specifier:
21976 alignas ( type-id ... [opt] )
21977 alignas ( alignment-expression ... [opt] ). */
21979 static tree
21980 cp_parser_std_attribute_spec (cp_parser *parser)
21982 tree attributes = NULL_TREE;
21983 cp_token *token = cp_lexer_peek_token (parser->lexer);
21985 if (token->type == CPP_OPEN_SQUARE
21986 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21988 cp_lexer_consume_token (parser->lexer);
21989 cp_lexer_consume_token (parser->lexer);
21991 attributes = cp_parser_std_attribute_list (parser);
21993 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21994 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21995 cp_parser_skip_to_end_of_statement (parser);
21996 else
21997 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21998 when we are sure that we have actually parsed them. */
21999 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22001 else
22003 tree alignas_expr;
22005 /* Look for an alignment-specifier. */
22007 token = cp_lexer_peek_token (parser->lexer);
22009 if (token->type != CPP_KEYWORD
22010 || token->keyword != RID_ALIGNAS)
22011 return NULL_TREE;
22013 cp_lexer_consume_token (parser->lexer);
22014 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22016 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22018 cp_parser_error (parser, "expected %<(%>");
22019 return error_mark_node;
22022 cp_parser_parse_tentatively (parser);
22023 alignas_expr = cp_parser_type_id (parser);
22025 if (!cp_parser_parse_definitely (parser))
22027 gcc_assert (alignas_expr == error_mark_node
22028 || alignas_expr == NULL_TREE);
22030 alignas_expr =
22031 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22032 /**cp_id_kind=*/NULL);
22033 if (alignas_expr == error_mark_node)
22034 cp_parser_skip_to_end_of_statement (parser);
22035 if (alignas_expr == NULL_TREE
22036 || alignas_expr == error_mark_node)
22037 return alignas_expr;
22040 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22042 cp_parser_error (parser, "expected %<)%>");
22043 return error_mark_node;
22046 alignas_expr = cxx_alignas_expr (alignas_expr);
22048 /* Build the C++-11 representation of an 'aligned'
22049 attribute. */
22050 attributes =
22051 build_tree_list (build_tree_list (get_identifier ("gnu"),
22052 get_identifier ("aligned")),
22053 build_tree_list (NULL_TREE, alignas_expr));
22056 return attributes;
22059 /* Parse a standard C++-11 attribute-specifier-seq.
22061 attribute-specifier-seq:
22062 attribute-specifier-seq [opt] attribute-specifier
22065 static tree
22066 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22068 tree attr_specs = NULL;
22070 while (true)
22072 tree attr_spec = cp_parser_std_attribute_spec (parser);
22073 if (attr_spec == NULL_TREE)
22074 break;
22075 if (attr_spec == error_mark_node)
22076 return error_mark_node;
22078 TREE_CHAIN (attr_spec) = attr_specs;
22079 attr_specs = attr_spec;
22082 attr_specs = nreverse (attr_specs);
22083 return attr_specs;
22086 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22087 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22088 current value of the PEDANTIC flag, regardless of whether or not
22089 the `__extension__' keyword is present. The caller is responsible
22090 for restoring the value of the PEDANTIC flag. */
22092 static bool
22093 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22095 /* Save the old value of the PEDANTIC flag. */
22096 *saved_pedantic = pedantic;
22098 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22100 /* Consume the `__extension__' token. */
22101 cp_lexer_consume_token (parser->lexer);
22102 /* We're not being pedantic while the `__extension__' keyword is
22103 in effect. */
22104 pedantic = 0;
22106 return true;
22109 return false;
22112 /* Parse a label declaration.
22114 label-declaration:
22115 __label__ label-declarator-seq ;
22117 label-declarator-seq:
22118 identifier , label-declarator-seq
22119 identifier */
22121 static void
22122 cp_parser_label_declaration (cp_parser* parser)
22124 /* Look for the `__label__' keyword. */
22125 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22127 while (true)
22129 tree identifier;
22131 /* Look for an identifier. */
22132 identifier = cp_parser_identifier (parser);
22133 /* If we failed, stop. */
22134 if (identifier == error_mark_node)
22135 break;
22136 /* Declare it as a label. */
22137 finish_label_decl (identifier);
22138 /* If the next token is a `;', stop. */
22139 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22140 break;
22141 /* Look for the `,' separating the label declarations. */
22142 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22145 /* Look for the final `;'. */
22146 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22149 /* Support Functions */
22151 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22152 NAME should have one of the representations used for an
22153 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22154 is returned. If PARSER->SCOPE is a dependent type, then a
22155 SCOPE_REF is returned.
22157 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22158 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22159 was formed. Abstractly, such entities should not be passed to this
22160 function, because they do not need to be looked up, but it is
22161 simpler to check for this special case here, rather than at the
22162 call-sites.
22164 In cases not explicitly covered above, this function returns a
22165 DECL, OVERLOAD, or baselink representing the result of the lookup.
22166 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22167 is returned.
22169 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22170 (e.g., "struct") that was used. In that case bindings that do not
22171 refer to types are ignored.
22173 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22174 ignored.
22176 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22177 are ignored.
22179 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22180 types.
22182 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22183 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22184 NULL_TREE otherwise. */
22186 static tree
22187 cp_parser_lookup_name (cp_parser *parser, tree name,
22188 enum tag_types tag_type,
22189 bool is_template,
22190 bool is_namespace,
22191 bool check_dependency,
22192 tree *ambiguous_decls,
22193 location_t name_location)
22195 tree decl;
22196 tree object_type = parser->context->object_type;
22198 /* Assume that the lookup will be unambiguous. */
22199 if (ambiguous_decls)
22200 *ambiguous_decls = NULL_TREE;
22202 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22203 no longer valid. Note that if we are parsing tentatively, and
22204 the parse fails, OBJECT_TYPE will be automatically restored. */
22205 parser->context->object_type = NULL_TREE;
22207 if (name == error_mark_node)
22208 return error_mark_node;
22210 /* A template-id has already been resolved; there is no lookup to
22211 do. */
22212 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22213 return name;
22214 if (BASELINK_P (name))
22216 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22217 == TEMPLATE_ID_EXPR);
22218 return name;
22221 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22222 it should already have been checked to make sure that the name
22223 used matches the type being destroyed. */
22224 if (TREE_CODE (name) == BIT_NOT_EXPR)
22226 tree type;
22228 /* Figure out to which type this destructor applies. */
22229 if (parser->scope)
22230 type = parser->scope;
22231 else if (object_type)
22232 type = object_type;
22233 else
22234 type = current_class_type;
22235 /* If that's not a class type, there is no destructor. */
22236 if (!type || !CLASS_TYPE_P (type))
22237 return error_mark_node;
22238 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22239 lazily_declare_fn (sfk_destructor, type);
22240 if (!CLASSTYPE_DESTRUCTORS (type))
22241 return error_mark_node;
22242 /* If it was a class type, return the destructor. */
22243 return CLASSTYPE_DESTRUCTORS (type);
22246 /* By this point, the NAME should be an ordinary identifier. If
22247 the id-expression was a qualified name, the qualifying scope is
22248 stored in PARSER->SCOPE at this point. */
22249 gcc_assert (identifier_p (name));
22251 /* Perform the lookup. */
22252 if (parser->scope)
22254 bool dependent_p;
22256 if (parser->scope == error_mark_node)
22257 return error_mark_node;
22259 /* If the SCOPE is dependent, the lookup must be deferred until
22260 the template is instantiated -- unless we are explicitly
22261 looking up names in uninstantiated templates. Even then, we
22262 cannot look up the name if the scope is not a class type; it
22263 might, for example, be a template type parameter. */
22264 dependent_p = (TYPE_P (parser->scope)
22265 && dependent_scope_p (parser->scope));
22266 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22267 && dependent_p)
22268 /* Defer lookup. */
22269 decl = error_mark_node;
22270 else
22272 tree pushed_scope = NULL_TREE;
22274 /* If PARSER->SCOPE is a dependent type, then it must be a
22275 class type, and we must not be checking dependencies;
22276 otherwise, we would have processed this lookup above. So
22277 that PARSER->SCOPE is not considered a dependent base by
22278 lookup_member, we must enter the scope here. */
22279 if (dependent_p)
22280 pushed_scope = push_scope (parser->scope);
22282 /* If the PARSER->SCOPE is a template specialization, it
22283 may be instantiated during name lookup. In that case,
22284 errors may be issued. Even if we rollback the current
22285 tentative parse, those errors are valid. */
22286 decl = lookup_qualified_name (parser->scope, name,
22287 tag_type != none_type,
22288 /*complain=*/true);
22290 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22291 lookup result and the nested-name-specifier nominates a class C:
22292 * if the name specified after the nested-name-specifier, when
22293 looked up in C, is the injected-class-name of C (Clause 9), or
22294 * if the name specified after the nested-name-specifier is the
22295 same as the identifier or the simple-template-id's template-
22296 name in the last component of the nested-name-specifier,
22297 the name is instead considered to name the constructor of
22298 class C. [ Note: for example, the constructor is not an
22299 acceptable lookup result in an elaborated-type-specifier so
22300 the constructor would not be used in place of the
22301 injected-class-name. --end note ] Such a constructor name
22302 shall be used only in the declarator-id of a declaration that
22303 names a constructor or in a using-declaration. */
22304 if (tag_type == none_type
22305 && DECL_SELF_REFERENCE_P (decl)
22306 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22307 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22308 tag_type != none_type,
22309 /*complain=*/true);
22311 /* If we have a single function from a using decl, pull it out. */
22312 if (TREE_CODE (decl) == OVERLOAD
22313 && !really_overloaded_fn (decl))
22314 decl = OVL_FUNCTION (decl);
22316 if (pushed_scope)
22317 pop_scope (pushed_scope);
22320 /* If the scope is a dependent type and either we deferred lookup or
22321 we did lookup but didn't find the name, rememeber the name. */
22322 if (decl == error_mark_node && TYPE_P (parser->scope)
22323 && dependent_type_p (parser->scope))
22325 if (tag_type)
22327 tree type;
22329 /* The resolution to Core Issue 180 says that `struct
22330 A::B' should be considered a type-name, even if `A'
22331 is dependent. */
22332 type = make_typename_type (parser->scope, name, tag_type,
22333 /*complain=*/tf_error);
22334 if (type != error_mark_node)
22335 decl = TYPE_NAME (type);
22337 else if (is_template
22338 && (cp_parser_next_token_ends_template_argument_p (parser)
22339 || cp_lexer_next_token_is (parser->lexer,
22340 CPP_CLOSE_PAREN)))
22341 decl = make_unbound_class_template (parser->scope,
22342 name, NULL_TREE,
22343 /*complain=*/tf_error);
22344 else
22345 decl = build_qualified_name (/*type=*/NULL_TREE,
22346 parser->scope, name,
22347 is_template);
22349 parser->qualifying_scope = parser->scope;
22350 parser->object_scope = NULL_TREE;
22352 else if (object_type)
22354 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22355 OBJECT_TYPE is not a class. */
22356 if (CLASS_TYPE_P (object_type))
22357 /* If the OBJECT_TYPE is a template specialization, it may
22358 be instantiated during name lookup. In that case, errors
22359 may be issued. Even if we rollback the current tentative
22360 parse, those errors are valid. */
22361 decl = lookup_member (object_type,
22362 name,
22363 /*protect=*/0,
22364 tag_type != none_type,
22365 tf_warning_or_error);
22366 else
22367 decl = NULL_TREE;
22369 if (!decl)
22370 /* Look it up in the enclosing context. */
22371 decl = lookup_name_real (name, tag_type != none_type,
22372 /*nonclass=*/0,
22373 /*block_p=*/true, is_namespace, 0);
22374 parser->object_scope = object_type;
22375 parser->qualifying_scope = NULL_TREE;
22377 else
22379 decl = lookup_name_real (name, tag_type != none_type,
22380 /*nonclass=*/0,
22381 /*block_p=*/true, is_namespace, 0);
22382 parser->qualifying_scope = NULL_TREE;
22383 parser->object_scope = NULL_TREE;
22386 /* If the lookup failed, let our caller know. */
22387 if (!decl || decl == error_mark_node)
22388 return error_mark_node;
22390 /* Pull out the template from an injected-class-name (or multiple). */
22391 if (is_template)
22392 decl = maybe_get_template_decl_from_type_decl (decl);
22394 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22395 if (TREE_CODE (decl) == TREE_LIST)
22397 if (ambiguous_decls)
22398 *ambiguous_decls = decl;
22399 /* The error message we have to print is too complicated for
22400 cp_parser_error, so we incorporate its actions directly. */
22401 if (!cp_parser_simulate_error (parser))
22403 error_at (name_location, "reference to %qD is ambiguous",
22404 name);
22405 print_candidates (decl);
22407 return error_mark_node;
22410 gcc_assert (DECL_P (decl)
22411 || TREE_CODE (decl) == OVERLOAD
22412 || TREE_CODE (decl) == SCOPE_REF
22413 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22414 || BASELINK_P (decl));
22416 /* If we have resolved the name of a member declaration, check to
22417 see if the declaration is accessible. When the name resolves to
22418 set of overloaded functions, accessibility is checked when
22419 overload resolution is done.
22421 During an explicit instantiation, access is not checked at all,
22422 as per [temp.explicit]. */
22423 if (DECL_P (decl))
22424 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22426 maybe_record_typedef_use (decl);
22428 return decl;
22431 /* Like cp_parser_lookup_name, but for use in the typical case where
22432 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22433 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22435 static tree
22436 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22438 return cp_parser_lookup_name (parser, name,
22439 none_type,
22440 /*is_template=*/false,
22441 /*is_namespace=*/false,
22442 /*check_dependency=*/true,
22443 /*ambiguous_decls=*/NULL,
22444 location);
22447 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22448 the current context, return the TYPE_DECL. If TAG_NAME_P is
22449 true, the DECL indicates the class being defined in a class-head,
22450 or declared in an elaborated-type-specifier.
22452 Otherwise, return DECL. */
22454 static tree
22455 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22457 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22458 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22460 struct A {
22461 template <typename T> struct B;
22464 template <typename T> struct A::B {};
22466 Similarly, in an elaborated-type-specifier:
22468 namespace N { struct X{}; }
22470 struct A {
22471 template <typename T> friend struct N::X;
22474 However, if the DECL refers to a class type, and we are in
22475 the scope of the class, then the name lookup automatically
22476 finds the TYPE_DECL created by build_self_reference rather
22477 than a TEMPLATE_DECL. For example, in:
22479 template <class T> struct S {
22480 S s;
22483 there is no need to handle such case. */
22485 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22486 return DECL_TEMPLATE_RESULT (decl);
22488 return decl;
22491 /* If too many, or too few, template-parameter lists apply to the
22492 declarator, issue an error message. Returns TRUE if all went well,
22493 and FALSE otherwise. */
22495 static bool
22496 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22497 cp_declarator *declarator,
22498 location_t declarator_location)
22500 switch (declarator->kind)
22502 case cdk_id:
22504 unsigned num_templates = 0;
22505 tree scope = declarator->u.id.qualifying_scope;
22507 if (scope)
22508 num_templates = num_template_headers_for_class (scope);
22509 else if (TREE_CODE (declarator->u.id.unqualified_name)
22510 == TEMPLATE_ID_EXPR)
22511 /* If the DECLARATOR has the form `X<y>' then it uses one
22512 additional level of template parameters. */
22513 ++num_templates;
22515 return cp_parser_check_template_parameters
22516 (parser, num_templates, declarator_location, declarator);
22519 case cdk_function:
22520 case cdk_array:
22521 case cdk_pointer:
22522 case cdk_reference:
22523 case cdk_ptrmem:
22524 return (cp_parser_check_declarator_template_parameters
22525 (parser, declarator->declarator, declarator_location));
22527 case cdk_error:
22528 return true;
22530 default:
22531 gcc_unreachable ();
22533 return false;
22536 /* NUM_TEMPLATES were used in the current declaration. If that is
22537 invalid, return FALSE and issue an error messages. Otherwise,
22538 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22539 declarator and we can print more accurate diagnostics. */
22541 static bool
22542 cp_parser_check_template_parameters (cp_parser* parser,
22543 unsigned num_templates,
22544 location_t location,
22545 cp_declarator *declarator)
22547 /* If there are the same number of template classes and parameter
22548 lists, that's OK. */
22549 if (parser->num_template_parameter_lists == num_templates)
22550 return true;
22551 /* If there are more, but only one more, then we are referring to a
22552 member template. That's OK too. */
22553 if (parser->num_template_parameter_lists == num_templates + 1)
22554 return true;
22555 /* If there are more template classes than parameter lists, we have
22556 something like:
22558 template <class T> void S<T>::R<T>::f (); */
22559 if (parser->num_template_parameter_lists < num_templates)
22561 if (declarator && !current_function_decl)
22562 error_at (location, "specializing member %<%T::%E%> "
22563 "requires %<template<>%> syntax",
22564 declarator->u.id.qualifying_scope,
22565 declarator->u.id.unqualified_name);
22566 else if (declarator)
22567 error_at (location, "invalid declaration of %<%T::%E%>",
22568 declarator->u.id.qualifying_scope,
22569 declarator->u.id.unqualified_name);
22570 else
22571 error_at (location, "too few template-parameter-lists");
22572 return false;
22574 /* Otherwise, there are too many template parameter lists. We have
22575 something like:
22577 template <class T> template <class U> void S::f(); */
22578 error_at (location, "too many template-parameter-lists");
22579 return false;
22582 /* Parse an optional `::' token indicating that the following name is
22583 from the global namespace. If so, PARSER->SCOPE is set to the
22584 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22585 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22586 Returns the new value of PARSER->SCOPE, if the `::' token is
22587 present, and NULL_TREE otherwise. */
22589 static tree
22590 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22592 cp_token *token;
22594 /* Peek at the next token. */
22595 token = cp_lexer_peek_token (parser->lexer);
22596 /* If we're looking at a `::' token then we're starting from the
22597 global namespace, not our current location. */
22598 if (token->type == CPP_SCOPE)
22600 /* Consume the `::' token. */
22601 cp_lexer_consume_token (parser->lexer);
22602 /* Set the SCOPE so that we know where to start the lookup. */
22603 parser->scope = global_namespace;
22604 parser->qualifying_scope = global_namespace;
22605 parser->object_scope = NULL_TREE;
22607 return parser->scope;
22609 else if (!current_scope_valid_p)
22611 parser->scope = NULL_TREE;
22612 parser->qualifying_scope = NULL_TREE;
22613 parser->object_scope = NULL_TREE;
22616 return NULL_TREE;
22619 /* Returns TRUE if the upcoming token sequence is the start of a
22620 constructor declarator. If FRIEND_P is true, the declarator is
22621 preceded by the `friend' specifier. */
22623 static bool
22624 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22626 bool constructor_p;
22627 bool outside_class_specifier_p;
22628 tree nested_name_specifier;
22629 cp_token *next_token;
22631 /* The common case is that this is not a constructor declarator, so
22632 try to avoid doing lots of work if at all possible. It's not
22633 valid declare a constructor at function scope. */
22634 if (parser->in_function_body)
22635 return false;
22636 /* And only certain tokens can begin a constructor declarator. */
22637 next_token = cp_lexer_peek_token (parser->lexer);
22638 if (next_token->type != CPP_NAME
22639 && next_token->type != CPP_SCOPE
22640 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22641 && next_token->type != CPP_TEMPLATE_ID)
22642 return false;
22644 /* Parse tentatively; we are going to roll back all of the tokens
22645 consumed here. */
22646 cp_parser_parse_tentatively (parser);
22647 /* Assume that we are looking at a constructor declarator. */
22648 constructor_p = true;
22650 /* Look for the optional `::' operator. */
22651 cp_parser_global_scope_opt (parser,
22652 /*current_scope_valid_p=*/false);
22653 /* Look for the nested-name-specifier. */
22654 nested_name_specifier
22655 = (cp_parser_nested_name_specifier_opt (parser,
22656 /*typename_keyword_p=*/false,
22657 /*check_dependency_p=*/false,
22658 /*type_p=*/false,
22659 /*is_declaration=*/false));
22661 outside_class_specifier_p = (!at_class_scope_p ()
22662 || !TYPE_BEING_DEFINED (current_class_type)
22663 || friend_p);
22665 /* Outside of a class-specifier, there must be a
22666 nested-name-specifier. */
22667 if (!nested_name_specifier && outside_class_specifier_p)
22668 constructor_p = false;
22669 else if (nested_name_specifier == error_mark_node)
22670 constructor_p = false;
22672 /* If we have a class scope, this is easy; DR 147 says that S::S always
22673 names the constructor, and no other qualified name could. */
22674 if (constructor_p && nested_name_specifier
22675 && CLASS_TYPE_P (nested_name_specifier))
22677 tree id = cp_parser_unqualified_id (parser,
22678 /*template_keyword_p=*/false,
22679 /*check_dependency_p=*/false,
22680 /*declarator_p=*/true,
22681 /*optional_p=*/false);
22682 if (is_overloaded_fn (id))
22683 id = DECL_NAME (get_first_fn (id));
22684 if (!constructor_name_p (id, nested_name_specifier))
22685 constructor_p = false;
22687 /* If we still think that this might be a constructor-declarator,
22688 look for a class-name. */
22689 else if (constructor_p)
22691 /* If we have:
22693 template <typename T> struct S {
22694 S();
22697 we must recognize that the nested `S' names a class. */
22698 tree type_decl;
22699 type_decl = cp_parser_class_name (parser,
22700 /*typename_keyword_p=*/false,
22701 /*template_keyword_p=*/false,
22702 none_type,
22703 /*check_dependency_p=*/false,
22704 /*class_head_p=*/false,
22705 /*is_declaration=*/false);
22706 /* If there was no class-name, then this is not a constructor.
22707 Otherwise, if we are in a class-specifier and we aren't
22708 handling a friend declaration, check that its type matches
22709 current_class_type (c++/38313). Note: error_mark_node
22710 is left alone for error recovery purposes. */
22711 constructor_p = (!cp_parser_error_occurred (parser)
22712 && (outside_class_specifier_p
22713 || type_decl == error_mark_node
22714 || same_type_p (current_class_type,
22715 TREE_TYPE (type_decl))));
22717 /* If we're still considering a constructor, we have to see a `(',
22718 to begin the parameter-declaration-clause, followed by either a
22719 `)', an `...', or a decl-specifier. We need to check for a
22720 type-specifier to avoid being fooled into thinking that:
22722 S (f) (int);
22724 is a constructor. (It is actually a function named `f' that
22725 takes one parameter (of type `int') and returns a value of type
22726 `S'. */
22727 if (constructor_p
22728 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22729 constructor_p = false;
22731 if (constructor_p
22732 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22733 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22734 /* A parameter declaration begins with a decl-specifier,
22735 which is either the "attribute" keyword, a storage class
22736 specifier, or (usually) a type-specifier. */
22737 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22739 tree type;
22740 tree pushed_scope = NULL_TREE;
22741 unsigned saved_num_template_parameter_lists;
22743 /* Names appearing in the type-specifier should be looked up
22744 in the scope of the class. */
22745 if (current_class_type)
22746 type = NULL_TREE;
22747 else
22749 type = TREE_TYPE (type_decl);
22750 if (TREE_CODE (type) == TYPENAME_TYPE)
22752 type = resolve_typename_type (type,
22753 /*only_current_p=*/false);
22754 if (TREE_CODE (type) == TYPENAME_TYPE)
22756 cp_parser_abort_tentative_parse (parser);
22757 return false;
22760 pushed_scope = push_scope (type);
22763 /* Inside the constructor parameter list, surrounding
22764 template-parameter-lists do not apply. */
22765 saved_num_template_parameter_lists
22766 = parser->num_template_parameter_lists;
22767 parser->num_template_parameter_lists = 0;
22769 /* Look for the type-specifier. */
22770 cp_parser_type_specifier (parser,
22771 CP_PARSER_FLAGS_NONE,
22772 /*decl_specs=*/NULL,
22773 /*is_declarator=*/true,
22774 /*declares_class_or_enum=*/NULL,
22775 /*is_cv_qualifier=*/NULL);
22777 parser->num_template_parameter_lists
22778 = saved_num_template_parameter_lists;
22780 /* Leave the scope of the class. */
22781 if (pushed_scope)
22782 pop_scope (pushed_scope);
22784 constructor_p = !cp_parser_error_occurred (parser);
22788 /* We did not really want to consume any tokens. */
22789 cp_parser_abort_tentative_parse (parser);
22791 return constructor_p;
22794 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22795 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22796 they must be performed once we are in the scope of the function.
22798 Returns the function defined. */
22800 static tree
22801 cp_parser_function_definition_from_specifiers_and_declarator
22802 (cp_parser* parser,
22803 cp_decl_specifier_seq *decl_specifiers,
22804 tree attributes,
22805 const cp_declarator *declarator)
22807 tree fn;
22808 bool success_p;
22810 /* Begin the function-definition. */
22811 success_p = start_function (decl_specifiers, declarator, attributes);
22813 /* The things we're about to see are not directly qualified by any
22814 template headers we've seen thus far. */
22815 reset_specialization ();
22817 /* If there were names looked up in the decl-specifier-seq that we
22818 did not check, check them now. We must wait until we are in the
22819 scope of the function to perform the checks, since the function
22820 might be a friend. */
22821 perform_deferred_access_checks (tf_warning_or_error);
22823 if (success_p)
22825 cp_finalize_omp_declare_simd (parser, current_function_decl);
22826 parser->omp_declare_simd = NULL;
22829 if (!success_p)
22831 /* Skip the entire function. */
22832 cp_parser_skip_to_end_of_block_or_statement (parser);
22833 fn = error_mark_node;
22835 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22837 /* Seen already, skip it. An error message has already been output. */
22838 cp_parser_skip_to_end_of_block_or_statement (parser);
22839 fn = current_function_decl;
22840 current_function_decl = NULL_TREE;
22841 /* If this is a function from a class, pop the nested class. */
22842 if (current_class_name)
22843 pop_nested_class ();
22845 else
22847 timevar_id_t tv;
22848 if (DECL_DECLARED_INLINE_P (current_function_decl))
22849 tv = TV_PARSE_INLINE;
22850 else
22851 tv = TV_PARSE_FUNC;
22852 timevar_push (tv);
22853 fn = cp_parser_function_definition_after_declarator (parser,
22854 /*inline_p=*/false);
22855 timevar_pop (tv);
22858 return fn;
22861 /* Parse the part of a function-definition that follows the
22862 declarator. INLINE_P is TRUE iff this function is an inline
22863 function defined within a class-specifier.
22865 Returns the function defined. */
22867 static tree
22868 cp_parser_function_definition_after_declarator (cp_parser* parser,
22869 bool inline_p)
22871 tree fn;
22872 bool ctor_initializer_p = false;
22873 bool saved_in_unbraced_linkage_specification_p;
22874 bool saved_in_function_body;
22875 unsigned saved_num_template_parameter_lists;
22876 cp_token *token;
22877 bool fully_implicit_function_template_p
22878 = parser->fully_implicit_function_template_p;
22879 parser->fully_implicit_function_template_p = false;
22880 tree implicit_template_parms
22881 = parser->implicit_template_parms;
22882 parser->implicit_template_parms = 0;
22883 cp_binding_level* implicit_template_scope
22884 = parser->implicit_template_scope;
22885 parser->implicit_template_scope = 0;
22887 saved_in_function_body = parser->in_function_body;
22888 parser->in_function_body = true;
22889 /* If the next token is `return', then the code may be trying to
22890 make use of the "named return value" extension that G++ used to
22891 support. */
22892 token = cp_lexer_peek_token (parser->lexer);
22893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22895 /* Consume the `return' keyword. */
22896 cp_lexer_consume_token (parser->lexer);
22897 /* Look for the identifier that indicates what value is to be
22898 returned. */
22899 cp_parser_identifier (parser);
22900 /* Issue an error message. */
22901 error_at (token->location,
22902 "named return values are no longer supported");
22903 /* Skip tokens until we reach the start of the function body. */
22904 while (true)
22906 cp_token *token = cp_lexer_peek_token (parser->lexer);
22907 if (token->type == CPP_OPEN_BRACE
22908 || token->type == CPP_EOF
22909 || token->type == CPP_PRAGMA_EOL)
22910 break;
22911 cp_lexer_consume_token (parser->lexer);
22914 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22915 anything declared inside `f'. */
22916 saved_in_unbraced_linkage_specification_p
22917 = parser->in_unbraced_linkage_specification_p;
22918 parser->in_unbraced_linkage_specification_p = false;
22919 /* Inside the function, surrounding template-parameter-lists do not
22920 apply. */
22921 saved_num_template_parameter_lists
22922 = parser->num_template_parameter_lists;
22923 parser->num_template_parameter_lists = 0;
22925 start_lambda_scope (current_function_decl);
22927 /* If the next token is `try', `__transaction_atomic', or
22928 `__transaction_relaxed`, then we are looking at either function-try-block
22929 or function-transaction-block. Note that all of these include the
22930 function-body. */
22931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22932 ctor_initializer_p = cp_parser_function_transaction (parser,
22933 RID_TRANSACTION_ATOMIC);
22934 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22935 RID_TRANSACTION_RELAXED))
22936 ctor_initializer_p = cp_parser_function_transaction (parser,
22937 RID_TRANSACTION_RELAXED);
22938 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22939 ctor_initializer_p = cp_parser_function_try_block (parser);
22940 else
22941 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22942 (parser, /*in_function_try_block=*/false);
22944 finish_lambda_scope ();
22946 /* Finish the function. */
22947 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22948 (inline_p ? 2 : 0));
22949 /* Generate code for it, if necessary. */
22950 expand_or_defer_fn (fn);
22951 /* Restore the saved values. */
22952 parser->in_unbraced_linkage_specification_p
22953 = saved_in_unbraced_linkage_specification_p;
22954 parser->num_template_parameter_lists
22955 = saved_num_template_parameter_lists;
22956 parser->in_function_body = saved_in_function_body;
22958 parser->fully_implicit_function_template_p
22959 = fully_implicit_function_template_p;
22960 parser->implicit_template_parms
22961 = implicit_template_parms;
22962 parser->implicit_template_scope
22963 = implicit_template_scope;
22965 if (parser->fully_implicit_function_template_p)
22966 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22968 return fn;
22971 /* Parse a template-declaration, assuming that the `export' (and
22972 `extern') keywords, if present, has already been scanned. MEMBER_P
22973 is as for cp_parser_template_declaration. */
22975 static void
22976 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22978 tree decl = NULL_TREE;
22979 vec<deferred_access_check, va_gc> *checks;
22980 tree parameter_list;
22981 bool friend_p = false;
22982 bool need_lang_pop;
22983 cp_token *token;
22985 /* Look for the `template' keyword. */
22986 token = cp_lexer_peek_token (parser->lexer);
22987 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22988 return;
22990 /* And the `<'. */
22991 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22992 return;
22993 if (at_class_scope_p () && current_function_decl)
22995 /* 14.5.2.2 [temp.mem]
22997 A local class shall not have member templates. */
22998 error_at (token->location,
22999 "invalid declaration of member template in local class");
23000 cp_parser_skip_to_end_of_block_or_statement (parser);
23001 return;
23003 /* [temp]
23005 A template ... shall not have C linkage. */
23006 if (current_lang_name == lang_name_c)
23008 error_at (token->location, "template with C linkage");
23009 /* Give it C++ linkage to avoid confusing other parts of the
23010 front end. */
23011 push_lang_context (lang_name_cplusplus);
23012 need_lang_pop = true;
23014 else
23015 need_lang_pop = false;
23017 /* We cannot perform access checks on the template parameter
23018 declarations until we know what is being declared, just as we
23019 cannot check the decl-specifier list. */
23020 push_deferring_access_checks (dk_deferred);
23022 /* If the next token is `>', then we have an invalid
23023 specialization. Rather than complain about an invalid template
23024 parameter, issue an error message here. */
23025 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23027 cp_parser_error (parser, "invalid explicit specialization");
23028 begin_specialization ();
23029 parameter_list = NULL_TREE;
23031 else
23033 /* Parse the template parameters. */
23034 parameter_list = cp_parser_template_parameter_list (parser);
23037 /* Get the deferred access checks from the parameter list. These
23038 will be checked once we know what is being declared, as for a
23039 member template the checks must be performed in the scope of the
23040 class containing the member. */
23041 checks = get_deferred_access_checks ();
23043 /* Look for the `>'. */
23044 cp_parser_skip_to_end_of_template_parameter_list (parser);
23045 /* We just processed one more parameter list. */
23046 ++parser->num_template_parameter_lists;
23047 /* If the next token is `template', there are more template
23048 parameters. */
23049 if (cp_lexer_next_token_is_keyword (parser->lexer,
23050 RID_TEMPLATE))
23051 cp_parser_template_declaration_after_export (parser, member_p);
23052 else if (cxx_dialect >= cxx11
23053 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23054 decl = cp_parser_alias_declaration (parser);
23055 else
23057 /* There are no access checks when parsing a template, as we do not
23058 know if a specialization will be a friend. */
23059 push_deferring_access_checks (dk_no_check);
23060 token = cp_lexer_peek_token (parser->lexer);
23061 decl = cp_parser_single_declaration (parser,
23062 checks,
23063 member_p,
23064 /*explicit_specialization_p=*/false,
23065 &friend_p);
23066 pop_deferring_access_checks ();
23068 /* If this is a member template declaration, let the front
23069 end know. */
23070 if (member_p && !friend_p && decl)
23072 if (TREE_CODE (decl) == TYPE_DECL)
23073 cp_parser_check_access_in_redeclaration (decl, token->location);
23075 decl = finish_member_template_decl (decl);
23077 else if (friend_p && decl
23078 && DECL_DECLARES_TYPE_P (decl))
23079 make_friend_class (current_class_type, TREE_TYPE (decl),
23080 /*complain=*/true);
23082 /* We are done with the current parameter list. */
23083 --parser->num_template_parameter_lists;
23085 pop_deferring_access_checks ();
23087 /* Finish up. */
23088 finish_template_decl (parameter_list);
23090 /* Check the template arguments for a literal operator template. */
23091 if (decl
23092 && DECL_DECLARES_FUNCTION_P (decl)
23093 && UDLIT_OPER_P (DECL_NAME (decl)))
23095 bool ok = true;
23096 if (parameter_list == NULL_TREE)
23097 ok = false;
23098 else
23100 int num_parms = TREE_VEC_LENGTH (parameter_list);
23101 if (num_parms == 1)
23103 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23104 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23105 if (TREE_TYPE (parm) != char_type_node
23106 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23107 ok = false;
23109 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23111 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23112 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23113 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23114 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23115 if (TREE_TYPE (parm) != TREE_TYPE (type)
23116 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23117 ok = false;
23119 else
23120 ok = false;
23122 if (!ok)
23123 error ("literal operator template %qD has invalid parameter list."
23124 " Expected non-type template argument pack <char...>"
23125 " or <typename CharT, CharT...>",
23126 decl);
23128 /* Register member declarations. */
23129 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23130 finish_member_declaration (decl);
23131 /* For the erroneous case of a template with C linkage, we pushed an
23132 implicit C++ linkage scope; exit that scope now. */
23133 if (need_lang_pop)
23134 pop_lang_context ();
23135 /* If DECL is a function template, we must return to parse it later.
23136 (Even though there is no definition, there might be default
23137 arguments that need handling.) */
23138 if (member_p && decl
23139 && DECL_DECLARES_FUNCTION_P (decl))
23140 vec_safe_push (unparsed_funs_with_definitions, decl);
23143 /* Perform the deferred access checks from a template-parameter-list.
23144 CHECKS is a TREE_LIST of access checks, as returned by
23145 get_deferred_access_checks. */
23147 static void
23148 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23150 ++processing_template_parmlist;
23151 perform_access_checks (checks, tf_warning_or_error);
23152 --processing_template_parmlist;
23155 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23156 `function-definition' sequence that follows a template header.
23157 If MEMBER_P is true, this declaration appears in a class scope.
23159 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23160 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23162 static tree
23163 cp_parser_single_declaration (cp_parser* parser,
23164 vec<deferred_access_check, va_gc> *checks,
23165 bool member_p,
23166 bool explicit_specialization_p,
23167 bool* friend_p)
23169 int declares_class_or_enum;
23170 tree decl = NULL_TREE;
23171 cp_decl_specifier_seq decl_specifiers;
23172 bool function_definition_p = false;
23173 cp_token *decl_spec_token_start;
23175 /* This function is only used when processing a template
23176 declaration. */
23177 gcc_assert (innermost_scope_kind () == sk_template_parms
23178 || innermost_scope_kind () == sk_template_spec);
23180 /* Defer access checks until we know what is being declared. */
23181 push_deferring_access_checks (dk_deferred);
23183 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23184 alternative. */
23185 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23186 cp_parser_decl_specifier_seq (parser,
23187 CP_PARSER_FLAGS_OPTIONAL,
23188 &decl_specifiers,
23189 &declares_class_or_enum);
23190 if (friend_p)
23191 *friend_p = cp_parser_friend_p (&decl_specifiers);
23193 /* There are no template typedefs. */
23194 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23196 error_at (decl_spec_token_start->location,
23197 "template declaration of %<typedef%>");
23198 decl = error_mark_node;
23201 /* Gather up the access checks that occurred the
23202 decl-specifier-seq. */
23203 stop_deferring_access_checks ();
23205 /* Check for the declaration of a template class. */
23206 if (declares_class_or_enum)
23208 if (cp_parser_declares_only_class_p (parser))
23210 decl = shadow_tag (&decl_specifiers);
23212 /* In this case:
23214 struct C {
23215 friend template <typename T> struct A<T>::B;
23218 A<T>::B will be represented by a TYPENAME_TYPE, and
23219 therefore not recognized by shadow_tag. */
23220 if (friend_p && *friend_p
23221 && !decl
23222 && decl_specifiers.type
23223 && TYPE_P (decl_specifiers.type))
23224 decl = decl_specifiers.type;
23226 if (decl && decl != error_mark_node)
23227 decl = TYPE_NAME (decl);
23228 else
23229 decl = error_mark_node;
23231 /* Perform access checks for template parameters. */
23232 cp_parser_perform_template_parameter_access_checks (checks);
23236 /* Complain about missing 'typename' or other invalid type names. */
23237 if (!decl_specifiers.any_type_specifiers_p
23238 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23240 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23241 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23242 the rest of this declaration. */
23243 decl = error_mark_node;
23244 goto out;
23247 /* If it's not a template class, try for a template function. If
23248 the next token is a `;', then this declaration does not declare
23249 anything. But, if there were errors in the decl-specifiers, then
23250 the error might well have come from an attempted class-specifier.
23251 In that case, there's no need to warn about a missing declarator. */
23252 if (!decl
23253 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23254 || decl_specifiers.type != error_mark_node))
23256 decl = cp_parser_init_declarator (parser,
23257 &decl_specifiers,
23258 checks,
23259 /*function_definition_allowed_p=*/true,
23260 member_p,
23261 declares_class_or_enum,
23262 &function_definition_p,
23263 NULL);
23265 /* 7.1.1-1 [dcl.stc]
23267 A storage-class-specifier shall not be specified in an explicit
23268 specialization... */
23269 if (decl
23270 && explicit_specialization_p
23271 && decl_specifiers.storage_class != sc_none)
23273 error_at (decl_spec_token_start->location,
23274 "explicit template specialization cannot have a storage class");
23275 decl = error_mark_node;
23278 if (decl && VAR_P (decl))
23279 check_template_variable (decl);
23282 /* Look for a trailing `;' after the declaration. */
23283 if (!function_definition_p
23284 && (decl == error_mark_node
23285 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23286 cp_parser_skip_to_end_of_block_or_statement (parser);
23288 out:
23289 pop_deferring_access_checks ();
23291 /* Clear any current qualification; whatever comes next is the start
23292 of something new. */
23293 parser->scope = NULL_TREE;
23294 parser->qualifying_scope = NULL_TREE;
23295 parser->object_scope = NULL_TREE;
23297 return decl;
23300 /* Parse a cast-expression that is not the operand of a unary "&". */
23302 static tree
23303 cp_parser_simple_cast_expression (cp_parser *parser)
23305 return cp_parser_cast_expression (parser, /*address_p=*/false,
23306 /*cast_p=*/false, /*decltype*/false, NULL);
23309 /* Parse a functional cast to TYPE. Returns an expression
23310 representing the cast. */
23312 static tree
23313 cp_parser_functional_cast (cp_parser* parser, tree type)
23315 vec<tree, va_gc> *vec;
23316 tree expression_list;
23317 tree cast;
23318 bool nonconst_p;
23320 if (!type)
23321 type = error_mark_node;
23323 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23325 cp_lexer_set_source_position (parser->lexer);
23326 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23327 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23328 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23329 if (TREE_CODE (type) == TYPE_DECL)
23330 type = TREE_TYPE (type);
23331 return finish_compound_literal (type, expression_list,
23332 tf_warning_or_error);
23336 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23337 /*cast_p=*/true,
23338 /*allow_expansion_p=*/true,
23339 /*non_constant_p=*/NULL);
23340 if (vec == NULL)
23341 expression_list = error_mark_node;
23342 else
23344 expression_list = build_tree_list_vec (vec);
23345 release_tree_vector (vec);
23348 cast = build_functional_cast (type, expression_list,
23349 tf_warning_or_error);
23350 /* [expr.const]/1: In an integral constant expression "only type
23351 conversions to integral or enumeration type can be used". */
23352 if (TREE_CODE (type) == TYPE_DECL)
23353 type = TREE_TYPE (type);
23354 if (cast != error_mark_node
23355 && !cast_valid_in_integral_constant_expression_p (type)
23356 && cp_parser_non_integral_constant_expression (parser,
23357 NIC_CONSTRUCTOR))
23358 return error_mark_node;
23359 return cast;
23362 /* Save the tokens that make up the body of a member function defined
23363 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23364 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23365 specifiers applied to the declaration. Returns the FUNCTION_DECL
23366 for the member function. */
23368 static tree
23369 cp_parser_save_member_function_body (cp_parser* parser,
23370 cp_decl_specifier_seq *decl_specifiers,
23371 cp_declarator *declarator,
23372 tree attributes)
23374 cp_token *first;
23375 cp_token *last;
23376 tree fn;
23378 /* Create the FUNCTION_DECL. */
23379 fn = grokmethod (decl_specifiers, declarator, attributes);
23380 cp_finalize_omp_declare_simd (parser, fn);
23381 /* If something went badly wrong, bail out now. */
23382 if (fn == error_mark_node)
23384 /* If there's a function-body, skip it. */
23385 if (cp_parser_token_starts_function_definition_p
23386 (cp_lexer_peek_token (parser->lexer)))
23387 cp_parser_skip_to_end_of_block_or_statement (parser);
23388 return error_mark_node;
23391 /* Remember it, if there default args to post process. */
23392 cp_parser_save_default_args (parser, fn);
23394 /* Save away the tokens that make up the body of the
23395 function. */
23396 first = parser->lexer->next_token;
23397 /* Handle function try blocks. */
23398 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23399 cp_lexer_consume_token (parser->lexer);
23400 /* We can have braced-init-list mem-initializers before the fn body. */
23401 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23403 cp_lexer_consume_token (parser->lexer);
23404 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23406 /* cache_group will stop after an un-nested { } pair, too. */
23407 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23408 break;
23410 /* variadic mem-inits have ... after the ')'. */
23411 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23412 cp_lexer_consume_token (parser->lexer);
23415 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23416 /* Handle function try blocks. */
23417 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23418 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23419 last = parser->lexer->next_token;
23421 /* Save away the inline definition; we will process it when the
23422 class is complete. */
23423 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23424 DECL_PENDING_INLINE_P (fn) = 1;
23426 /* We need to know that this was defined in the class, so that
23427 friend templates are handled correctly. */
23428 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23430 /* Add FN to the queue of functions to be parsed later. */
23431 vec_safe_push (unparsed_funs_with_definitions, fn);
23433 return fn;
23436 /* Save the tokens that make up the in-class initializer for a non-static
23437 data member. Returns a DEFAULT_ARG. */
23439 static tree
23440 cp_parser_save_nsdmi (cp_parser* parser)
23442 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23445 /* Parse a template-argument-list, as well as the trailing ">" (but
23446 not the opening "<"). See cp_parser_template_argument_list for the
23447 return value. */
23449 static tree
23450 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23452 tree arguments;
23453 tree saved_scope;
23454 tree saved_qualifying_scope;
23455 tree saved_object_scope;
23456 bool saved_greater_than_is_operator_p;
23457 int saved_unevaluated_operand;
23458 int saved_inhibit_evaluation_warnings;
23460 /* [temp.names]
23462 When parsing a template-id, the first non-nested `>' is taken as
23463 the end of the template-argument-list rather than a greater-than
23464 operator. */
23465 saved_greater_than_is_operator_p
23466 = parser->greater_than_is_operator_p;
23467 parser->greater_than_is_operator_p = false;
23468 /* Parsing the argument list may modify SCOPE, so we save it
23469 here. */
23470 saved_scope = parser->scope;
23471 saved_qualifying_scope = parser->qualifying_scope;
23472 saved_object_scope = parser->object_scope;
23473 /* We need to evaluate the template arguments, even though this
23474 template-id may be nested within a "sizeof". */
23475 saved_unevaluated_operand = cp_unevaluated_operand;
23476 cp_unevaluated_operand = 0;
23477 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23478 c_inhibit_evaluation_warnings = 0;
23479 /* Parse the template-argument-list itself. */
23480 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23481 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23482 arguments = NULL_TREE;
23483 else
23484 arguments = cp_parser_template_argument_list (parser);
23485 /* Look for the `>' that ends the template-argument-list. If we find
23486 a '>>' instead, it's probably just a typo. */
23487 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23489 if (cxx_dialect != cxx98)
23491 /* In C++0x, a `>>' in a template argument list or cast
23492 expression is considered to be two separate `>'
23493 tokens. So, change the current token to a `>', but don't
23494 consume it: it will be consumed later when the outer
23495 template argument list (or cast expression) is parsed.
23496 Note that this replacement of `>' for `>>' is necessary
23497 even if we are parsing tentatively: in the tentative
23498 case, after calling
23499 cp_parser_enclosed_template_argument_list we will always
23500 throw away all of the template arguments and the first
23501 closing `>', either because the template argument list
23502 was erroneous or because we are replacing those tokens
23503 with a CPP_TEMPLATE_ID token. The second `>' (which will
23504 not have been thrown away) is needed either to close an
23505 outer template argument list or to complete a new-style
23506 cast. */
23507 cp_token *token = cp_lexer_peek_token (parser->lexer);
23508 token->type = CPP_GREATER;
23510 else if (!saved_greater_than_is_operator_p)
23512 /* If we're in a nested template argument list, the '>>' has
23513 to be a typo for '> >'. We emit the error message, but we
23514 continue parsing and we push a '>' as next token, so that
23515 the argument list will be parsed correctly. Note that the
23516 global source location is still on the token before the
23517 '>>', so we need to say explicitly where we want it. */
23518 cp_token *token = cp_lexer_peek_token (parser->lexer);
23519 error_at (token->location, "%<>>%> should be %<> >%> "
23520 "within a nested template argument list");
23522 token->type = CPP_GREATER;
23524 else
23526 /* If this is not a nested template argument list, the '>>'
23527 is a typo for '>'. Emit an error message and continue.
23528 Same deal about the token location, but here we can get it
23529 right by consuming the '>>' before issuing the diagnostic. */
23530 cp_token *token = cp_lexer_consume_token (parser->lexer);
23531 error_at (token->location,
23532 "spurious %<>>%>, use %<>%> to terminate "
23533 "a template argument list");
23536 else
23537 cp_parser_skip_to_end_of_template_parameter_list (parser);
23538 /* The `>' token might be a greater-than operator again now. */
23539 parser->greater_than_is_operator_p
23540 = saved_greater_than_is_operator_p;
23541 /* Restore the SAVED_SCOPE. */
23542 parser->scope = saved_scope;
23543 parser->qualifying_scope = saved_qualifying_scope;
23544 parser->object_scope = saved_object_scope;
23545 cp_unevaluated_operand = saved_unevaluated_operand;
23546 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23548 return arguments;
23551 /* MEMBER_FUNCTION is a member function, or a friend. If default
23552 arguments, or the body of the function have not yet been parsed,
23553 parse them now. */
23555 static void
23556 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23558 timevar_push (TV_PARSE_INMETH);
23559 /* If this member is a template, get the underlying
23560 FUNCTION_DECL. */
23561 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23562 member_function = DECL_TEMPLATE_RESULT (member_function);
23564 /* There should not be any class definitions in progress at this
23565 point; the bodies of members are only parsed outside of all class
23566 definitions. */
23567 gcc_assert (parser->num_classes_being_defined == 0);
23568 /* While we're parsing the member functions we might encounter more
23569 classes. We want to handle them right away, but we don't want
23570 them getting mixed up with functions that are currently in the
23571 queue. */
23572 push_unparsed_function_queues (parser);
23574 /* Make sure that any template parameters are in scope. */
23575 maybe_begin_member_template_processing (member_function);
23577 /* If the body of the function has not yet been parsed, parse it
23578 now. */
23579 if (DECL_PENDING_INLINE_P (member_function))
23581 tree function_scope;
23582 cp_token_cache *tokens;
23584 /* The function is no longer pending; we are processing it. */
23585 tokens = DECL_PENDING_INLINE_INFO (member_function);
23586 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23587 DECL_PENDING_INLINE_P (member_function) = 0;
23589 /* If this is a local class, enter the scope of the containing
23590 function. */
23591 function_scope = current_function_decl;
23592 if (function_scope)
23593 push_function_context ();
23595 /* Push the body of the function onto the lexer stack. */
23596 cp_parser_push_lexer_for_tokens (parser, tokens);
23598 /* Let the front end know that we going to be defining this
23599 function. */
23600 start_preparsed_function (member_function, NULL_TREE,
23601 SF_PRE_PARSED | SF_INCLASS_INLINE);
23603 /* Don't do access checking if it is a templated function. */
23604 if (processing_template_decl)
23605 push_deferring_access_checks (dk_no_check);
23607 /* #pragma omp declare reduction needs special parsing. */
23608 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23610 parser->lexer->in_pragma = true;
23611 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23612 finish_function (/*inline*/2);
23613 cp_check_omp_declare_reduction (member_function);
23615 else
23616 /* Now, parse the body of the function. */
23617 cp_parser_function_definition_after_declarator (parser,
23618 /*inline_p=*/true);
23620 if (processing_template_decl)
23621 pop_deferring_access_checks ();
23623 /* Leave the scope of the containing function. */
23624 if (function_scope)
23625 pop_function_context ();
23626 cp_parser_pop_lexer (parser);
23629 /* Remove any template parameters from the symbol table. */
23630 maybe_end_member_template_processing ();
23632 /* Restore the queue. */
23633 pop_unparsed_function_queues (parser);
23634 timevar_pop (TV_PARSE_INMETH);
23637 /* If DECL contains any default args, remember it on the unparsed
23638 functions queue. */
23640 static void
23641 cp_parser_save_default_args (cp_parser* parser, tree decl)
23643 tree probe;
23645 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23646 probe;
23647 probe = TREE_CHAIN (probe))
23648 if (TREE_PURPOSE (probe))
23650 cp_default_arg_entry entry = {current_class_type, decl};
23651 vec_safe_push (unparsed_funs_with_default_args, entry);
23652 break;
23656 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23657 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23658 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23659 from the parameter-type-list. */
23661 static tree
23662 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23663 tree default_arg, tree parmtype)
23665 cp_token_cache *tokens;
23666 tree parsed_arg;
23667 bool dummy;
23669 if (default_arg == error_mark_node)
23670 return error_mark_node;
23672 /* Push the saved tokens for the default argument onto the parser's
23673 lexer stack. */
23674 tokens = DEFARG_TOKENS (default_arg);
23675 cp_parser_push_lexer_for_tokens (parser, tokens);
23677 start_lambda_scope (decl);
23679 /* Parse the default argument. */
23680 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23681 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23682 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23684 finish_lambda_scope ();
23686 if (parsed_arg == error_mark_node)
23687 cp_parser_skip_to_end_of_statement (parser);
23689 if (!processing_template_decl)
23691 /* In a non-template class, check conversions now. In a template,
23692 we'll wait and instantiate these as needed. */
23693 if (TREE_CODE (decl) == PARM_DECL)
23694 parsed_arg = check_default_argument (parmtype, parsed_arg,
23695 tf_warning_or_error);
23696 else
23697 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
23700 /* If the token stream has not been completely used up, then
23701 there was extra junk after the end of the default
23702 argument. */
23703 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23705 if (TREE_CODE (decl) == PARM_DECL)
23706 cp_parser_error (parser, "expected %<,%>");
23707 else
23708 cp_parser_error (parser, "expected %<;%>");
23711 /* Revert to the main lexer. */
23712 cp_parser_pop_lexer (parser);
23714 return parsed_arg;
23717 /* FIELD is a non-static data member with an initializer which we saved for
23718 later; parse it now. */
23720 static void
23721 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23723 tree def;
23725 maybe_begin_member_template_processing (field);
23727 push_unparsed_function_queues (parser);
23728 def = cp_parser_late_parse_one_default_arg (parser, field,
23729 DECL_INITIAL (field),
23730 NULL_TREE);
23731 pop_unparsed_function_queues (parser);
23733 maybe_end_member_template_processing ();
23735 DECL_INITIAL (field) = def;
23738 /* FN is a FUNCTION_DECL which may contains a parameter with an
23739 unparsed DEFAULT_ARG. Parse the default args now. This function
23740 assumes that the current scope is the scope in which the default
23741 argument should be processed. */
23743 static void
23744 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23746 bool saved_local_variables_forbidden_p;
23747 tree parm, parmdecl;
23749 /* While we're parsing the default args, we might (due to the
23750 statement expression extension) encounter more classes. We want
23751 to handle them right away, but we don't want them getting mixed
23752 up with default args that are currently in the queue. */
23753 push_unparsed_function_queues (parser);
23755 /* Local variable names (and the `this' keyword) may not appear
23756 in a default argument. */
23757 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23758 parser->local_variables_forbidden_p = true;
23760 push_defarg_context (fn);
23762 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23763 parmdecl = DECL_ARGUMENTS (fn);
23764 parm && parm != void_list_node;
23765 parm = TREE_CHAIN (parm),
23766 parmdecl = DECL_CHAIN (parmdecl))
23768 tree default_arg = TREE_PURPOSE (parm);
23769 tree parsed_arg;
23770 vec<tree, va_gc> *insts;
23771 tree copy;
23772 unsigned ix;
23774 if (!default_arg)
23775 continue;
23777 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23778 /* This can happen for a friend declaration for a function
23779 already declared with default arguments. */
23780 continue;
23782 parsed_arg
23783 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23784 default_arg,
23785 TREE_VALUE (parm));
23786 if (parsed_arg == error_mark_node)
23788 continue;
23791 TREE_PURPOSE (parm) = parsed_arg;
23793 /* Update any instantiations we've already created. */
23794 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23795 vec_safe_iterate (insts, ix, &copy); ix++)
23796 TREE_PURPOSE (copy) = parsed_arg;
23799 pop_defarg_context ();
23801 /* Make sure no default arg is missing. */
23802 check_default_args (fn);
23804 /* Restore the state of local_variables_forbidden_p. */
23805 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23807 /* Restore the queue. */
23808 pop_unparsed_function_queues (parser);
23811 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23813 sizeof ... ( identifier )
23815 where the 'sizeof' token has already been consumed. */
23817 static tree
23818 cp_parser_sizeof_pack (cp_parser *parser)
23820 /* Consume the `...'. */
23821 cp_lexer_consume_token (parser->lexer);
23822 maybe_warn_variadic_templates ();
23824 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23825 if (paren)
23826 cp_lexer_consume_token (parser->lexer);
23827 else
23828 permerror (cp_lexer_peek_token (parser->lexer)->location,
23829 "%<sizeof...%> argument must be surrounded by parentheses");
23831 cp_token *token = cp_lexer_peek_token (parser->lexer);
23832 tree name = cp_parser_identifier (parser);
23833 if (name == error_mark_node)
23834 return error_mark_node;
23835 /* The name is not qualified. */
23836 parser->scope = NULL_TREE;
23837 parser->qualifying_scope = NULL_TREE;
23838 parser->object_scope = NULL_TREE;
23839 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23840 if (expr == error_mark_node)
23841 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23842 token->location);
23843 if (TREE_CODE (expr) == TYPE_DECL)
23844 expr = TREE_TYPE (expr);
23845 else if (TREE_CODE (expr) == CONST_DECL)
23846 expr = DECL_INITIAL (expr);
23847 expr = make_pack_expansion (expr);
23849 if (paren)
23850 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23852 return expr;
23855 /* Parse the operand of `sizeof' (or a similar operator). Returns
23856 either a TYPE or an expression, depending on the form of the
23857 input. The KEYWORD indicates which kind of expression we have
23858 encountered. */
23860 static tree
23861 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23863 tree expr = NULL_TREE;
23864 const char *saved_message;
23865 char *tmp;
23866 bool saved_integral_constant_expression_p;
23867 bool saved_non_integral_constant_expression_p;
23869 /* If it's a `...', then we are computing the length of a parameter
23870 pack. */
23871 if (keyword == RID_SIZEOF
23872 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23873 return cp_parser_sizeof_pack (parser);
23875 /* Types cannot be defined in a `sizeof' expression. Save away the
23876 old message. */
23877 saved_message = parser->type_definition_forbidden_message;
23878 /* And create the new one. */
23879 tmp = concat ("types may not be defined in %<",
23880 IDENTIFIER_POINTER (ridpointers[keyword]),
23881 "%> expressions", NULL);
23882 parser->type_definition_forbidden_message = tmp;
23884 /* The restrictions on constant-expressions do not apply inside
23885 sizeof expressions. */
23886 saved_integral_constant_expression_p
23887 = parser->integral_constant_expression_p;
23888 saved_non_integral_constant_expression_p
23889 = parser->non_integral_constant_expression_p;
23890 parser->integral_constant_expression_p = false;
23892 /* Do not actually evaluate the expression. */
23893 ++cp_unevaluated_operand;
23894 ++c_inhibit_evaluation_warnings;
23895 /* If it's a `(', then we might be looking at the type-id
23896 construction. */
23897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23899 tree type = NULL_TREE;
23900 bool compound_literal_p;
23902 /* We can't be sure yet whether we're looking at a type-id or an
23903 expression. */
23904 cp_parser_parse_tentatively (parser);
23905 /* Consume the `('. */
23906 cp_lexer_consume_token (parser->lexer);
23907 /* Note: as a GNU Extension, compound literals are considered
23908 postfix-expressions as they are in C99, so they are valid
23909 arguments to sizeof. See comment in cp_parser_cast_expression
23910 for details. */
23911 cp_lexer_save_tokens (parser->lexer);
23912 /* Skip tokens until the next token is a closing parenthesis.
23913 If we find the closing `)', and the next token is a `{', then
23914 we are looking at a compound-literal. */
23915 compound_literal_p
23916 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23917 /*consume_paren=*/true)
23918 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23919 /* Roll back the tokens we skipped. */
23920 cp_lexer_rollback_tokens (parser->lexer);
23921 /* If we were looking at a compound-literal, simulate an error
23922 so that the call to cp_parser_parse_definitely below will
23923 fail. */
23924 if (compound_literal_p)
23925 cp_parser_simulate_error (parser);
23926 else
23928 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23929 parser->in_type_id_in_expr_p = true;
23930 /* Look for the type-id. */
23931 type = cp_parser_type_id (parser);
23932 /* Look for the closing `)'. */
23933 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23934 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23937 /* If all went well, then we're done. */
23938 if (cp_parser_parse_definitely (parser))
23940 cp_decl_specifier_seq decl_specs;
23942 /* Build a trivial decl-specifier-seq. */
23943 clear_decl_specs (&decl_specs);
23944 decl_specs.type = type;
23946 /* Call grokdeclarator to figure out what type this is. */
23947 expr = grokdeclarator (NULL,
23948 &decl_specs,
23949 TYPENAME,
23950 /*initialized=*/0,
23951 /*attrlist=*/NULL);
23955 /* If the type-id production did not work out, then we must be
23956 looking at the unary-expression production. */
23957 if (!expr)
23958 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23959 /*cast_p=*/false, NULL);
23961 /* Go back to evaluating expressions. */
23962 --cp_unevaluated_operand;
23963 --c_inhibit_evaluation_warnings;
23965 /* Free the message we created. */
23966 free (tmp);
23967 /* And restore the old one. */
23968 parser->type_definition_forbidden_message = saved_message;
23969 parser->integral_constant_expression_p
23970 = saved_integral_constant_expression_p;
23971 parser->non_integral_constant_expression_p
23972 = saved_non_integral_constant_expression_p;
23974 return expr;
23977 /* If the current declaration has no declarator, return true. */
23979 static bool
23980 cp_parser_declares_only_class_p (cp_parser *parser)
23982 /* If the next token is a `;' or a `,' then there is no
23983 declarator. */
23984 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23985 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23988 /* Update the DECL_SPECS to reflect the storage class indicated by
23989 KEYWORD. */
23991 static void
23992 cp_parser_set_storage_class (cp_parser *parser,
23993 cp_decl_specifier_seq *decl_specs,
23994 enum rid keyword,
23995 cp_token *token)
23997 cp_storage_class storage_class;
23999 if (parser->in_unbraced_linkage_specification_p)
24001 error_at (token->location, "invalid use of %qD in linkage specification",
24002 ridpointers[keyword]);
24003 return;
24005 else if (decl_specs->storage_class != sc_none)
24007 decl_specs->conflicting_specifiers_p = true;
24008 return;
24011 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24012 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24013 && decl_specs->gnu_thread_keyword_p)
24015 pedwarn (decl_specs->locations[ds_thread], 0,
24016 "%<__thread%> before %qD", ridpointers[keyword]);
24019 switch (keyword)
24021 case RID_AUTO:
24022 storage_class = sc_auto;
24023 break;
24024 case RID_REGISTER:
24025 storage_class = sc_register;
24026 break;
24027 case RID_STATIC:
24028 storage_class = sc_static;
24029 break;
24030 case RID_EXTERN:
24031 storage_class = sc_extern;
24032 break;
24033 case RID_MUTABLE:
24034 storage_class = sc_mutable;
24035 break;
24036 default:
24037 gcc_unreachable ();
24039 decl_specs->storage_class = storage_class;
24040 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24042 /* A storage class specifier cannot be applied alongside a typedef
24043 specifier. If there is a typedef specifier present then set
24044 conflicting_specifiers_p which will trigger an error later
24045 on in grokdeclarator. */
24046 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24047 decl_specs->conflicting_specifiers_p = true;
24050 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24051 is true, the type is a class or enum definition. */
24053 static void
24054 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24055 tree type_spec,
24056 cp_token *token,
24057 bool type_definition_p)
24059 decl_specs->any_specifiers_p = true;
24061 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24062 (with, for example, in "typedef int wchar_t;") we remember that
24063 this is what happened. In system headers, we ignore these
24064 declarations so that G++ can work with system headers that are not
24065 C++-safe. */
24066 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24067 && !type_definition_p
24068 && (type_spec == boolean_type_node
24069 || type_spec == char16_type_node
24070 || type_spec == char32_type_node
24071 || type_spec == wchar_type_node)
24072 && (decl_specs->type
24073 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24074 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24075 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24076 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24078 decl_specs->redefined_builtin_type = type_spec;
24079 set_and_check_decl_spec_loc (decl_specs,
24080 ds_redefined_builtin_type_spec,
24081 token);
24082 if (!decl_specs->type)
24084 decl_specs->type = type_spec;
24085 decl_specs->type_definition_p = false;
24086 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24089 else if (decl_specs->type)
24090 decl_specs->multiple_types_p = true;
24091 else
24093 decl_specs->type = type_spec;
24094 decl_specs->type_definition_p = type_definition_p;
24095 decl_specs->redefined_builtin_type = NULL_TREE;
24096 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24100 /* True iff TOKEN is the GNU keyword __thread. */
24102 static bool
24103 token_is__thread (cp_token *token)
24105 gcc_assert (token->keyword == RID_THREAD);
24106 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24109 /* Set the location for a declarator specifier and check if it is
24110 duplicated.
24112 DECL_SPECS is the sequence of declarator specifiers onto which to
24113 set the location.
24115 DS is the single declarator specifier to set which location is to
24116 be set onto the existing sequence of declarators.
24118 LOCATION is the location for the declarator specifier to
24119 consider. */
24121 static void
24122 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24123 cp_decl_spec ds, cp_token *token)
24125 gcc_assert (ds < ds_last);
24127 if (decl_specs == NULL)
24128 return;
24130 source_location location = token->location;
24132 if (decl_specs->locations[ds] == 0)
24134 decl_specs->locations[ds] = location;
24135 if (ds == ds_thread)
24136 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24138 else
24140 if (ds == ds_long)
24142 if (decl_specs->locations[ds_long_long] != 0)
24143 error_at (location,
24144 "%<long long long%> is too long for GCC");
24145 else
24147 decl_specs->locations[ds_long_long] = location;
24148 pedwarn_cxx98 (location,
24149 OPT_Wlong_long,
24150 "ISO C++ 1998 does not support %<long long%>");
24153 else if (ds == ds_thread)
24155 bool gnu = token_is__thread (token);
24156 if (gnu != decl_specs->gnu_thread_keyword_p)
24157 error_at (location,
24158 "both %<__thread%> and %<thread_local%> specified");
24159 else
24160 error_at (location, "duplicate %qD", token->u.value);
24162 else
24164 static const char *const decl_spec_names[] = {
24165 "signed",
24166 "unsigned",
24167 "short",
24168 "long",
24169 "const",
24170 "volatile",
24171 "restrict",
24172 "inline",
24173 "virtual",
24174 "explicit",
24175 "friend",
24176 "typedef",
24177 "using",
24178 "constexpr",
24179 "__complex"
24181 error_at (location,
24182 "duplicate %qs", decl_spec_names[ds]);
24187 /* Return true iff the declarator specifier DS is present in the
24188 sequence of declarator specifiers DECL_SPECS. */
24190 bool
24191 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24192 cp_decl_spec ds)
24194 gcc_assert (ds < ds_last);
24196 if (decl_specs == NULL)
24197 return false;
24199 return decl_specs->locations[ds] != 0;
24202 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24203 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24205 static bool
24206 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24208 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24211 /* Issue an error message indicating that TOKEN_DESC was expected.
24212 If KEYWORD is true, it indicated this function is called by
24213 cp_parser_require_keword and the required token can only be
24214 a indicated keyword. */
24216 static void
24217 cp_parser_required_error (cp_parser *parser,
24218 required_token token_desc,
24219 bool keyword)
24221 switch (token_desc)
24223 case RT_NEW:
24224 cp_parser_error (parser, "expected %<new%>");
24225 return;
24226 case RT_DELETE:
24227 cp_parser_error (parser, "expected %<delete%>");
24228 return;
24229 case RT_RETURN:
24230 cp_parser_error (parser, "expected %<return%>");
24231 return;
24232 case RT_WHILE:
24233 cp_parser_error (parser, "expected %<while%>");
24234 return;
24235 case RT_EXTERN:
24236 cp_parser_error (parser, "expected %<extern%>");
24237 return;
24238 case RT_STATIC_ASSERT:
24239 cp_parser_error (parser, "expected %<static_assert%>");
24240 return;
24241 case RT_DECLTYPE:
24242 cp_parser_error (parser, "expected %<decltype%>");
24243 return;
24244 case RT_OPERATOR:
24245 cp_parser_error (parser, "expected %<operator%>");
24246 return;
24247 case RT_CLASS:
24248 cp_parser_error (parser, "expected %<class%>");
24249 return;
24250 case RT_TEMPLATE:
24251 cp_parser_error (parser, "expected %<template%>");
24252 return;
24253 case RT_NAMESPACE:
24254 cp_parser_error (parser, "expected %<namespace%>");
24255 return;
24256 case RT_USING:
24257 cp_parser_error (parser, "expected %<using%>");
24258 return;
24259 case RT_ASM:
24260 cp_parser_error (parser, "expected %<asm%>");
24261 return;
24262 case RT_TRY:
24263 cp_parser_error (parser, "expected %<try%>");
24264 return;
24265 case RT_CATCH:
24266 cp_parser_error (parser, "expected %<catch%>");
24267 return;
24268 case RT_THROW:
24269 cp_parser_error (parser, "expected %<throw%>");
24270 return;
24271 case RT_LABEL:
24272 cp_parser_error (parser, "expected %<__label__%>");
24273 return;
24274 case RT_AT_TRY:
24275 cp_parser_error (parser, "expected %<@try%>");
24276 return;
24277 case RT_AT_SYNCHRONIZED:
24278 cp_parser_error (parser, "expected %<@synchronized%>");
24279 return;
24280 case RT_AT_THROW:
24281 cp_parser_error (parser, "expected %<@throw%>");
24282 return;
24283 case RT_TRANSACTION_ATOMIC:
24284 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24285 return;
24286 case RT_TRANSACTION_RELAXED:
24287 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24288 return;
24289 default:
24290 break;
24292 if (!keyword)
24294 switch (token_desc)
24296 case RT_SEMICOLON:
24297 cp_parser_error (parser, "expected %<;%>");
24298 return;
24299 case RT_OPEN_PAREN:
24300 cp_parser_error (parser, "expected %<(%>");
24301 return;
24302 case RT_CLOSE_BRACE:
24303 cp_parser_error (parser, "expected %<}%>");
24304 return;
24305 case RT_OPEN_BRACE:
24306 cp_parser_error (parser, "expected %<{%>");
24307 return;
24308 case RT_CLOSE_SQUARE:
24309 cp_parser_error (parser, "expected %<]%>");
24310 return;
24311 case RT_OPEN_SQUARE:
24312 cp_parser_error (parser, "expected %<[%>");
24313 return;
24314 case RT_COMMA:
24315 cp_parser_error (parser, "expected %<,%>");
24316 return;
24317 case RT_SCOPE:
24318 cp_parser_error (parser, "expected %<::%>");
24319 return;
24320 case RT_LESS:
24321 cp_parser_error (parser, "expected %<<%>");
24322 return;
24323 case RT_GREATER:
24324 cp_parser_error (parser, "expected %<>%>");
24325 return;
24326 case RT_EQ:
24327 cp_parser_error (parser, "expected %<=%>");
24328 return;
24329 case RT_ELLIPSIS:
24330 cp_parser_error (parser, "expected %<...%>");
24331 return;
24332 case RT_MULT:
24333 cp_parser_error (parser, "expected %<*%>");
24334 return;
24335 case RT_COMPL:
24336 cp_parser_error (parser, "expected %<~%>");
24337 return;
24338 case RT_COLON:
24339 cp_parser_error (parser, "expected %<:%>");
24340 return;
24341 case RT_COLON_SCOPE:
24342 cp_parser_error (parser, "expected %<:%> or %<::%>");
24343 return;
24344 case RT_CLOSE_PAREN:
24345 cp_parser_error (parser, "expected %<)%>");
24346 return;
24347 case RT_COMMA_CLOSE_PAREN:
24348 cp_parser_error (parser, "expected %<,%> or %<)%>");
24349 return;
24350 case RT_PRAGMA_EOL:
24351 cp_parser_error (parser, "expected end of line");
24352 return;
24353 case RT_NAME:
24354 cp_parser_error (parser, "expected identifier");
24355 return;
24356 case RT_SELECT:
24357 cp_parser_error (parser, "expected selection-statement");
24358 return;
24359 case RT_INTERATION:
24360 cp_parser_error (parser, "expected iteration-statement");
24361 return;
24362 case RT_JUMP:
24363 cp_parser_error (parser, "expected jump-statement");
24364 return;
24365 case RT_CLASS_KEY:
24366 cp_parser_error (parser, "expected class-key");
24367 return;
24368 case RT_CLASS_TYPENAME_TEMPLATE:
24369 cp_parser_error (parser,
24370 "expected %<class%>, %<typename%>, or %<template%>");
24371 return;
24372 default:
24373 gcc_unreachable ();
24376 else
24377 gcc_unreachable ();
24382 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24383 issue an error message indicating that TOKEN_DESC was expected.
24385 Returns the token consumed, if the token had the appropriate type.
24386 Otherwise, returns NULL. */
24388 static cp_token *
24389 cp_parser_require (cp_parser* parser,
24390 enum cpp_ttype type,
24391 required_token token_desc)
24393 if (cp_lexer_next_token_is (parser->lexer, type))
24394 return cp_lexer_consume_token (parser->lexer);
24395 else
24397 /* Output the MESSAGE -- unless we're parsing tentatively. */
24398 if (!cp_parser_simulate_error (parser))
24399 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24400 return NULL;
24404 /* An error message is produced if the next token is not '>'.
24405 All further tokens are skipped until the desired token is
24406 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24408 static void
24409 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24411 /* Current level of '< ... >'. */
24412 unsigned level = 0;
24413 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24414 unsigned nesting_depth = 0;
24416 /* Are we ready, yet? If not, issue error message. */
24417 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24418 return;
24420 /* Skip tokens until the desired token is found. */
24421 while (true)
24423 /* Peek at the next token. */
24424 switch (cp_lexer_peek_token (parser->lexer)->type)
24426 case CPP_LESS:
24427 if (!nesting_depth)
24428 ++level;
24429 break;
24431 case CPP_RSHIFT:
24432 if (cxx_dialect == cxx98)
24433 /* C++0x views the `>>' operator as two `>' tokens, but
24434 C++98 does not. */
24435 break;
24436 else if (!nesting_depth && level-- == 0)
24438 /* We've hit a `>>' where the first `>' closes the
24439 template argument list, and the second `>' is
24440 spurious. Just consume the `>>' and stop; we've
24441 already produced at least one error. */
24442 cp_lexer_consume_token (parser->lexer);
24443 return;
24445 /* Fall through for C++0x, so we handle the second `>' in
24446 the `>>'. */
24448 case CPP_GREATER:
24449 if (!nesting_depth && level-- == 0)
24451 /* We've reached the token we want, consume it and stop. */
24452 cp_lexer_consume_token (parser->lexer);
24453 return;
24455 break;
24457 case CPP_OPEN_PAREN:
24458 case CPP_OPEN_SQUARE:
24459 ++nesting_depth;
24460 break;
24462 case CPP_CLOSE_PAREN:
24463 case CPP_CLOSE_SQUARE:
24464 if (nesting_depth-- == 0)
24465 return;
24466 break;
24468 case CPP_EOF:
24469 case CPP_PRAGMA_EOL:
24470 case CPP_SEMICOLON:
24471 case CPP_OPEN_BRACE:
24472 case CPP_CLOSE_BRACE:
24473 /* The '>' was probably forgotten, don't look further. */
24474 return;
24476 default:
24477 break;
24480 /* Consume this token. */
24481 cp_lexer_consume_token (parser->lexer);
24485 /* If the next token is the indicated keyword, consume it. Otherwise,
24486 issue an error message indicating that TOKEN_DESC was expected.
24488 Returns the token consumed, if the token had the appropriate type.
24489 Otherwise, returns NULL. */
24491 static cp_token *
24492 cp_parser_require_keyword (cp_parser* parser,
24493 enum rid keyword,
24494 required_token token_desc)
24496 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24498 if (token && token->keyword != keyword)
24500 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24501 return NULL;
24504 return token;
24507 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24508 function-definition. */
24510 static bool
24511 cp_parser_token_starts_function_definition_p (cp_token* token)
24513 return (/* An ordinary function-body begins with an `{'. */
24514 token->type == CPP_OPEN_BRACE
24515 /* A ctor-initializer begins with a `:'. */
24516 || token->type == CPP_COLON
24517 /* A function-try-block begins with `try'. */
24518 || token->keyword == RID_TRY
24519 /* A function-transaction-block begins with `__transaction_atomic'
24520 or `__transaction_relaxed'. */
24521 || token->keyword == RID_TRANSACTION_ATOMIC
24522 || token->keyword == RID_TRANSACTION_RELAXED
24523 /* The named return value extension begins with `return'. */
24524 || token->keyword == RID_RETURN);
24527 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24528 definition. */
24530 static bool
24531 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24533 cp_token *token;
24535 token = cp_lexer_peek_token (parser->lexer);
24536 return (token->type == CPP_OPEN_BRACE
24537 || (token->type == CPP_COLON
24538 && !parser->colon_doesnt_start_class_def_p));
24541 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24542 C++0x) ending a template-argument. */
24544 static bool
24545 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24547 cp_token *token;
24549 token = cp_lexer_peek_token (parser->lexer);
24550 return (token->type == CPP_COMMA
24551 || token->type == CPP_GREATER
24552 || token->type == CPP_ELLIPSIS
24553 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24556 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24557 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24559 static bool
24560 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24561 size_t n)
24563 cp_token *token;
24565 token = cp_lexer_peek_nth_token (parser->lexer, n);
24566 if (token->type == CPP_LESS)
24567 return true;
24568 /* Check for the sequence `<::' in the original code. It would be lexed as
24569 `[:', where `[' is a digraph, and there is no whitespace before
24570 `:'. */
24571 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24573 cp_token *token2;
24574 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24575 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24576 return true;
24578 return false;
24581 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24582 or none_type otherwise. */
24584 static enum tag_types
24585 cp_parser_token_is_class_key (cp_token* token)
24587 switch (token->keyword)
24589 case RID_CLASS:
24590 return class_type;
24591 case RID_STRUCT:
24592 return record_type;
24593 case RID_UNION:
24594 return union_type;
24596 default:
24597 return none_type;
24601 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24603 static void
24604 cp_parser_check_class_key (enum tag_types class_key, tree type)
24606 if (type == error_mark_node)
24607 return;
24608 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24610 if (permerror (input_location, "%qs tag used in naming %q#T",
24611 class_key == union_type ? "union"
24612 : class_key == record_type ? "struct" : "class",
24613 type))
24614 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24615 "%q#T was previously declared here", type);
24619 /* Issue an error message if DECL is redeclared with different
24620 access than its original declaration [class.access.spec/3].
24621 This applies to nested classes and nested class templates.
24622 [class.mem/1]. */
24624 static void
24625 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24627 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24628 return;
24630 if ((TREE_PRIVATE (decl)
24631 != (current_access_specifier == access_private_node))
24632 || (TREE_PROTECTED (decl)
24633 != (current_access_specifier == access_protected_node)))
24634 error_at (location, "%qD redeclared with different access", decl);
24637 /* Look for the `template' keyword, as a syntactic disambiguator.
24638 Return TRUE iff it is present, in which case it will be
24639 consumed. */
24641 static bool
24642 cp_parser_optional_template_keyword (cp_parser *parser)
24644 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24646 /* In C++98 the `template' keyword can only be used within templates;
24647 outside templates the parser can always figure out what is a
24648 template and what is not. In C++11, per the resolution of DR 468,
24649 `template' is allowed in cases where it is not strictly necessary. */
24650 if (!processing_template_decl
24651 && pedantic && cxx_dialect == cxx98)
24653 cp_token *token = cp_lexer_peek_token (parser->lexer);
24654 pedwarn (token->location, OPT_Wpedantic,
24655 "in C++98 %<template%> (as a disambiguator) is only "
24656 "allowed within templates");
24657 /* If this part of the token stream is rescanned, the same
24658 error message would be generated. So, we purge the token
24659 from the stream. */
24660 cp_lexer_purge_token (parser->lexer);
24661 return false;
24663 else
24665 /* Consume the `template' keyword. */
24666 cp_lexer_consume_token (parser->lexer);
24667 return true;
24670 return false;
24673 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24674 set PARSER->SCOPE, and perform other related actions. */
24676 static void
24677 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24679 int i;
24680 struct tree_check *check_value;
24681 deferred_access_check *chk;
24682 vec<deferred_access_check, va_gc> *checks;
24684 /* Get the stored value. */
24685 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24686 /* Perform any access checks that were deferred. */
24687 checks = check_value->checks;
24688 if (checks)
24690 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24691 perform_or_defer_access_check (chk->binfo,
24692 chk->decl,
24693 chk->diag_decl, tf_warning_or_error);
24695 /* Set the scope from the stored value. */
24696 parser->scope = check_value->value;
24697 parser->qualifying_scope = check_value->qualifying_scope;
24698 parser->object_scope = NULL_TREE;
24701 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24702 encounter the end of a block before what we were looking for. */
24704 static bool
24705 cp_parser_cache_group (cp_parser *parser,
24706 enum cpp_ttype end,
24707 unsigned depth)
24709 while (true)
24711 cp_token *token = cp_lexer_peek_token (parser->lexer);
24713 /* Abort a parenthesized expression if we encounter a semicolon. */
24714 if ((end == CPP_CLOSE_PAREN || depth == 0)
24715 && token->type == CPP_SEMICOLON)
24716 return true;
24717 /* If we've reached the end of the file, stop. */
24718 if (token->type == CPP_EOF
24719 || (end != CPP_PRAGMA_EOL
24720 && token->type == CPP_PRAGMA_EOL))
24721 return true;
24722 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24723 /* We've hit the end of an enclosing block, so there's been some
24724 kind of syntax error. */
24725 return true;
24727 /* Consume the token. */
24728 cp_lexer_consume_token (parser->lexer);
24729 /* See if it starts a new group. */
24730 if (token->type == CPP_OPEN_BRACE)
24732 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24733 /* In theory this should probably check end == '}', but
24734 cp_parser_save_member_function_body needs it to exit
24735 after either '}' or ')' when called with ')'. */
24736 if (depth == 0)
24737 return false;
24739 else if (token->type == CPP_OPEN_PAREN)
24741 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24742 if (depth == 0 && end == CPP_CLOSE_PAREN)
24743 return false;
24745 else if (token->type == CPP_PRAGMA)
24746 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24747 else if (token->type == end)
24748 return false;
24752 /* Like above, for caching a default argument or NSDMI. Both of these are
24753 terminated by a non-nested comma, but it can be unclear whether or not a
24754 comma is nested in a template argument list unless we do more parsing.
24755 In order to handle this ambiguity, when we encounter a ',' after a '<'
24756 we try to parse what follows as a parameter-declaration-list (in the
24757 case of a default argument) or a member-declarator (in the case of an
24758 NSDMI). If that succeeds, then we stop caching. */
24760 static tree
24761 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24763 unsigned depth = 0;
24764 int maybe_template_id = 0;
24765 cp_token *first_token;
24766 cp_token *token;
24767 tree default_argument;
24769 /* Add tokens until we have processed the entire default
24770 argument. We add the range [first_token, token). */
24771 first_token = cp_lexer_peek_token (parser->lexer);
24772 if (first_token->type == CPP_OPEN_BRACE)
24774 /* For list-initialization, this is straightforward. */
24775 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24776 token = cp_lexer_peek_token (parser->lexer);
24778 else while (true)
24780 bool done = false;
24782 /* Peek at the next token. */
24783 token = cp_lexer_peek_token (parser->lexer);
24784 /* What we do depends on what token we have. */
24785 switch (token->type)
24787 /* In valid code, a default argument must be
24788 immediately followed by a `,' `)', or `...'. */
24789 case CPP_COMMA:
24790 if (depth == 0 && maybe_template_id)
24792 /* If we've seen a '<', we might be in a
24793 template-argument-list. Until Core issue 325 is
24794 resolved, we don't know how this situation ought
24795 to be handled, so try to DTRT. We check whether
24796 what comes after the comma is a valid parameter
24797 declaration list. If it is, then the comma ends
24798 the default argument; otherwise the default
24799 argument continues. */
24800 bool error = false;
24802 /* Set ITALP so cp_parser_parameter_declaration_list
24803 doesn't decide to commit to this parse. */
24804 bool saved_italp = parser->in_template_argument_list_p;
24805 parser->in_template_argument_list_p = true;
24807 cp_parser_parse_tentatively (parser);
24808 cp_lexer_consume_token (parser->lexer);
24810 if (nsdmi)
24812 int ctor_dtor_or_conv_p;
24813 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24814 &ctor_dtor_or_conv_p,
24815 /*parenthesized_p=*/NULL,
24816 /*member_p=*/true);
24818 else
24820 begin_scope (sk_function_parms, NULL_TREE);
24821 cp_parser_parameter_declaration_list (parser, &error);
24822 pop_bindings_and_leave_scope ();
24824 if (!cp_parser_error_occurred (parser) && !error)
24825 done = true;
24826 cp_parser_abort_tentative_parse (parser);
24828 parser->in_template_argument_list_p = saved_italp;
24829 break;
24831 case CPP_CLOSE_PAREN:
24832 case CPP_ELLIPSIS:
24833 /* If we run into a non-nested `;', `}', or `]',
24834 then the code is invalid -- but the default
24835 argument is certainly over. */
24836 case CPP_SEMICOLON:
24837 case CPP_CLOSE_BRACE:
24838 case CPP_CLOSE_SQUARE:
24839 if (depth == 0
24840 /* Handle correctly int n = sizeof ... ( p ); */
24841 && token->type != CPP_ELLIPSIS)
24842 done = true;
24843 /* Update DEPTH, if necessary. */
24844 else if (token->type == CPP_CLOSE_PAREN
24845 || token->type == CPP_CLOSE_BRACE
24846 || token->type == CPP_CLOSE_SQUARE)
24847 --depth;
24848 break;
24850 case CPP_OPEN_PAREN:
24851 case CPP_OPEN_SQUARE:
24852 case CPP_OPEN_BRACE:
24853 ++depth;
24854 break;
24856 case CPP_LESS:
24857 if (depth == 0)
24858 /* This might be the comparison operator, or it might
24859 start a template argument list. */
24860 ++maybe_template_id;
24861 break;
24863 case CPP_RSHIFT:
24864 if (cxx_dialect == cxx98)
24865 break;
24866 /* Fall through for C++0x, which treats the `>>'
24867 operator like two `>' tokens in certain
24868 cases. */
24870 case CPP_GREATER:
24871 if (depth == 0)
24873 /* This might be an operator, or it might close a
24874 template argument list. But if a previous '<'
24875 started a template argument list, this will have
24876 closed it, so we can't be in one anymore. */
24877 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24878 if (maybe_template_id < 0)
24879 maybe_template_id = 0;
24881 break;
24883 /* If we run out of tokens, issue an error message. */
24884 case CPP_EOF:
24885 case CPP_PRAGMA_EOL:
24886 error_at (token->location, "file ends in default argument");
24887 done = true;
24888 break;
24890 case CPP_NAME:
24891 case CPP_SCOPE:
24892 /* In these cases, we should look for template-ids.
24893 For example, if the default argument is
24894 `X<int, double>()', we need to do name lookup to
24895 figure out whether or not `X' is a template; if
24896 so, the `,' does not end the default argument.
24898 That is not yet done. */
24899 break;
24901 default:
24902 break;
24905 /* If we've reached the end, stop. */
24906 if (done)
24907 break;
24909 /* Add the token to the token block. */
24910 token = cp_lexer_consume_token (parser->lexer);
24913 /* Create a DEFAULT_ARG to represent the unparsed default
24914 argument. */
24915 default_argument = make_node (DEFAULT_ARG);
24916 DEFARG_TOKENS (default_argument)
24917 = cp_token_cache_new (first_token, token);
24918 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24920 return default_argument;
24923 /* Begin parsing tentatively. We always save tokens while parsing
24924 tentatively so that if the tentative parsing fails we can restore the
24925 tokens. */
24927 static void
24928 cp_parser_parse_tentatively (cp_parser* parser)
24930 /* Enter a new parsing context. */
24931 parser->context = cp_parser_context_new (parser->context);
24932 /* Begin saving tokens. */
24933 cp_lexer_save_tokens (parser->lexer);
24934 /* In order to avoid repetitive access control error messages,
24935 access checks are queued up until we are no longer parsing
24936 tentatively. */
24937 push_deferring_access_checks (dk_deferred);
24940 /* Commit to the currently active tentative parse. */
24942 static void
24943 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24945 cp_parser_context *context;
24946 cp_lexer *lexer;
24948 /* Mark all of the levels as committed. */
24949 lexer = parser->lexer;
24950 for (context = parser->context; context->next; context = context->next)
24952 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24953 break;
24954 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24955 while (!cp_lexer_saving_tokens (lexer))
24956 lexer = lexer->next;
24957 cp_lexer_commit_tokens (lexer);
24961 /* Commit to the topmost currently active tentative parse.
24963 Note that this function shouldn't be called when there are
24964 irreversible side-effects while in a tentative state. For
24965 example, we shouldn't create a permanent entry in the symbol
24966 table, or issue an error message that might not apply if the
24967 tentative parse is aborted. */
24969 static void
24970 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24972 cp_parser_context *context = parser->context;
24973 cp_lexer *lexer = parser->lexer;
24975 if (context)
24977 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24978 return;
24979 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24981 while (!cp_lexer_saving_tokens (lexer))
24982 lexer = lexer->next;
24983 cp_lexer_commit_tokens (lexer);
24987 /* Abort the currently active tentative parse. All consumed tokens
24988 will be rolled back, and no diagnostics will be issued. */
24990 static void
24991 cp_parser_abort_tentative_parse (cp_parser* parser)
24993 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24994 || errorcount > 0);
24995 cp_parser_simulate_error (parser);
24996 /* Now, pretend that we want to see if the construct was
24997 successfully parsed. */
24998 cp_parser_parse_definitely (parser);
25001 /* Stop parsing tentatively. If a parse error has occurred, restore the
25002 token stream. Otherwise, commit to the tokens we have consumed.
25003 Returns true if no error occurred; false otherwise. */
25005 static bool
25006 cp_parser_parse_definitely (cp_parser* parser)
25008 bool error_occurred;
25009 cp_parser_context *context;
25011 /* Remember whether or not an error occurred, since we are about to
25012 destroy that information. */
25013 error_occurred = cp_parser_error_occurred (parser);
25014 /* Remove the topmost context from the stack. */
25015 context = parser->context;
25016 parser->context = context->next;
25017 /* If no parse errors occurred, commit to the tentative parse. */
25018 if (!error_occurred)
25020 /* Commit to the tokens read tentatively, unless that was
25021 already done. */
25022 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25023 cp_lexer_commit_tokens (parser->lexer);
25025 pop_to_parent_deferring_access_checks ();
25027 /* Otherwise, if errors occurred, roll back our state so that things
25028 are just as they were before we began the tentative parse. */
25029 else
25031 cp_lexer_rollback_tokens (parser->lexer);
25032 pop_deferring_access_checks ();
25034 /* Add the context to the front of the free list. */
25035 context->next = cp_parser_context_free_list;
25036 cp_parser_context_free_list = context;
25038 return !error_occurred;
25041 /* Returns true if we are parsing tentatively and are not committed to
25042 this tentative parse. */
25044 static bool
25045 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25047 return (cp_parser_parsing_tentatively (parser)
25048 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25051 /* Returns nonzero iff an error has occurred during the most recent
25052 tentative parse. */
25054 static bool
25055 cp_parser_error_occurred (cp_parser* parser)
25057 return (cp_parser_parsing_tentatively (parser)
25058 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25061 /* Returns nonzero if GNU extensions are allowed. */
25063 static bool
25064 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25066 return parser->allow_gnu_extensions_p;
25069 /* Objective-C++ Productions */
25072 /* Parse an Objective-C expression, which feeds into a primary-expression
25073 above.
25075 objc-expression:
25076 objc-message-expression
25077 objc-string-literal
25078 objc-encode-expression
25079 objc-protocol-expression
25080 objc-selector-expression
25082 Returns a tree representation of the expression. */
25084 static tree
25085 cp_parser_objc_expression (cp_parser* parser)
25087 /* Try to figure out what kind of declaration is present. */
25088 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25090 switch (kwd->type)
25092 case CPP_OPEN_SQUARE:
25093 return cp_parser_objc_message_expression (parser);
25095 case CPP_OBJC_STRING:
25096 kwd = cp_lexer_consume_token (parser->lexer);
25097 return objc_build_string_object (kwd->u.value);
25099 case CPP_KEYWORD:
25100 switch (kwd->keyword)
25102 case RID_AT_ENCODE:
25103 return cp_parser_objc_encode_expression (parser);
25105 case RID_AT_PROTOCOL:
25106 return cp_parser_objc_protocol_expression (parser);
25108 case RID_AT_SELECTOR:
25109 return cp_parser_objc_selector_expression (parser);
25111 default:
25112 break;
25114 default:
25115 error_at (kwd->location,
25116 "misplaced %<@%D%> Objective-C++ construct",
25117 kwd->u.value);
25118 cp_parser_skip_to_end_of_block_or_statement (parser);
25121 return error_mark_node;
25124 /* Parse an Objective-C message expression.
25126 objc-message-expression:
25127 [ objc-message-receiver objc-message-args ]
25129 Returns a representation of an Objective-C message. */
25131 static tree
25132 cp_parser_objc_message_expression (cp_parser* parser)
25134 tree receiver, messageargs;
25136 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25137 receiver = cp_parser_objc_message_receiver (parser);
25138 messageargs = cp_parser_objc_message_args (parser);
25139 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25141 return objc_build_message_expr (receiver, messageargs);
25144 /* Parse an objc-message-receiver.
25146 objc-message-receiver:
25147 expression
25148 simple-type-specifier
25150 Returns a representation of the type or expression. */
25152 static tree
25153 cp_parser_objc_message_receiver (cp_parser* parser)
25155 tree rcv;
25157 /* An Objective-C message receiver may be either (1) a type
25158 or (2) an expression. */
25159 cp_parser_parse_tentatively (parser);
25160 rcv = cp_parser_expression (parser, false, NULL);
25162 if (cp_parser_parse_definitely (parser))
25163 return rcv;
25165 rcv = cp_parser_simple_type_specifier (parser,
25166 /*decl_specs=*/NULL,
25167 CP_PARSER_FLAGS_NONE);
25169 return objc_get_class_reference (rcv);
25172 /* Parse the arguments and selectors comprising an Objective-C message.
25174 objc-message-args:
25175 objc-selector
25176 objc-selector-args
25177 objc-selector-args , objc-comma-args
25179 objc-selector-args:
25180 objc-selector [opt] : assignment-expression
25181 objc-selector-args objc-selector [opt] : assignment-expression
25183 objc-comma-args:
25184 assignment-expression
25185 objc-comma-args , assignment-expression
25187 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25188 selector arguments and TREE_VALUE containing a list of comma
25189 arguments. */
25191 static tree
25192 cp_parser_objc_message_args (cp_parser* parser)
25194 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25195 bool maybe_unary_selector_p = true;
25196 cp_token *token = cp_lexer_peek_token (parser->lexer);
25198 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25200 tree selector = NULL_TREE, arg;
25202 if (token->type != CPP_COLON)
25203 selector = cp_parser_objc_selector (parser);
25205 /* Detect if we have a unary selector. */
25206 if (maybe_unary_selector_p
25207 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25208 return build_tree_list (selector, NULL_TREE);
25210 maybe_unary_selector_p = false;
25211 cp_parser_require (parser, CPP_COLON, RT_COLON);
25212 arg = cp_parser_assignment_expression (parser, false, NULL);
25214 sel_args
25215 = chainon (sel_args,
25216 build_tree_list (selector, arg));
25218 token = cp_lexer_peek_token (parser->lexer);
25221 /* Handle non-selector arguments, if any. */
25222 while (token->type == CPP_COMMA)
25224 tree arg;
25226 cp_lexer_consume_token (parser->lexer);
25227 arg = cp_parser_assignment_expression (parser, false, NULL);
25229 addl_args
25230 = chainon (addl_args,
25231 build_tree_list (NULL_TREE, arg));
25233 token = cp_lexer_peek_token (parser->lexer);
25236 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25238 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25239 return build_tree_list (error_mark_node, error_mark_node);
25242 return build_tree_list (sel_args, addl_args);
25245 /* Parse an Objective-C encode expression.
25247 objc-encode-expression:
25248 @encode objc-typename
25250 Returns an encoded representation of the type argument. */
25252 static tree
25253 cp_parser_objc_encode_expression (cp_parser* parser)
25255 tree type;
25256 cp_token *token;
25258 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25259 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25260 token = cp_lexer_peek_token (parser->lexer);
25261 type = complete_type (cp_parser_type_id (parser));
25262 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25264 if (!type)
25266 error_at (token->location,
25267 "%<@encode%> must specify a type as an argument");
25268 return error_mark_node;
25271 /* This happens if we find @encode(T) (where T is a template
25272 typename or something dependent on a template typename) when
25273 parsing a template. In that case, we can't compile it
25274 immediately, but we rather create an AT_ENCODE_EXPR which will
25275 need to be instantiated when the template is used.
25277 if (dependent_type_p (type))
25279 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25280 TREE_READONLY (value) = 1;
25281 return value;
25284 return objc_build_encode_expr (type);
25287 /* Parse an Objective-C @defs expression. */
25289 static tree
25290 cp_parser_objc_defs_expression (cp_parser *parser)
25292 tree name;
25294 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25295 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25296 name = cp_parser_identifier (parser);
25297 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25299 return objc_get_class_ivars (name);
25302 /* Parse an Objective-C protocol expression.
25304 objc-protocol-expression:
25305 @protocol ( identifier )
25307 Returns a representation of the protocol expression. */
25309 static tree
25310 cp_parser_objc_protocol_expression (cp_parser* parser)
25312 tree proto;
25314 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25315 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25316 proto = cp_parser_identifier (parser);
25317 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25319 return objc_build_protocol_expr (proto);
25322 /* Parse an Objective-C selector expression.
25324 objc-selector-expression:
25325 @selector ( objc-method-signature )
25327 objc-method-signature:
25328 objc-selector
25329 objc-selector-seq
25331 objc-selector-seq:
25332 objc-selector :
25333 objc-selector-seq objc-selector :
25335 Returns a representation of the method selector. */
25337 static tree
25338 cp_parser_objc_selector_expression (cp_parser* parser)
25340 tree sel_seq = NULL_TREE;
25341 bool maybe_unary_selector_p = true;
25342 cp_token *token;
25343 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25345 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25346 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25347 token = cp_lexer_peek_token (parser->lexer);
25349 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25350 || token->type == CPP_SCOPE)
25352 tree selector = NULL_TREE;
25354 if (token->type != CPP_COLON
25355 || token->type == CPP_SCOPE)
25356 selector = cp_parser_objc_selector (parser);
25358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25359 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25361 /* Detect if we have a unary selector. */
25362 if (maybe_unary_selector_p)
25364 sel_seq = selector;
25365 goto finish_selector;
25367 else
25369 cp_parser_error (parser, "expected %<:%>");
25372 maybe_unary_selector_p = false;
25373 token = cp_lexer_consume_token (parser->lexer);
25375 if (token->type == CPP_SCOPE)
25377 sel_seq
25378 = chainon (sel_seq,
25379 build_tree_list (selector, NULL_TREE));
25380 sel_seq
25381 = chainon (sel_seq,
25382 build_tree_list (NULL_TREE, NULL_TREE));
25384 else
25385 sel_seq
25386 = chainon (sel_seq,
25387 build_tree_list (selector, NULL_TREE));
25389 token = cp_lexer_peek_token (parser->lexer);
25392 finish_selector:
25393 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25395 return objc_build_selector_expr (loc, sel_seq);
25398 /* Parse a list of identifiers.
25400 objc-identifier-list:
25401 identifier
25402 objc-identifier-list , identifier
25404 Returns a TREE_LIST of identifier nodes. */
25406 static tree
25407 cp_parser_objc_identifier_list (cp_parser* parser)
25409 tree identifier;
25410 tree list;
25411 cp_token *sep;
25413 identifier = cp_parser_identifier (parser);
25414 if (identifier == error_mark_node)
25415 return error_mark_node;
25417 list = build_tree_list (NULL_TREE, identifier);
25418 sep = cp_lexer_peek_token (parser->lexer);
25420 while (sep->type == CPP_COMMA)
25422 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25423 identifier = cp_parser_identifier (parser);
25424 if (identifier == error_mark_node)
25425 return list;
25427 list = chainon (list, build_tree_list (NULL_TREE,
25428 identifier));
25429 sep = cp_lexer_peek_token (parser->lexer);
25432 return list;
25435 /* Parse an Objective-C alias declaration.
25437 objc-alias-declaration:
25438 @compatibility_alias identifier identifier ;
25440 This function registers the alias mapping with the Objective-C front end.
25441 It returns nothing. */
25443 static void
25444 cp_parser_objc_alias_declaration (cp_parser* parser)
25446 tree alias, orig;
25448 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25449 alias = cp_parser_identifier (parser);
25450 orig = cp_parser_identifier (parser);
25451 objc_declare_alias (alias, orig);
25452 cp_parser_consume_semicolon_at_end_of_statement (parser);
25455 /* Parse an Objective-C class forward-declaration.
25457 objc-class-declaration:
25458 @class objc-identifier-list ;
25460 The function registers the forward declarations with the Objective-C
25461 front end. It returns nothing. */
25463 static void
25464 cp_parser_objc_class_declaration (cp_parser* parser)
25466 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25467 while (true)
25469 tree id;
25471 id = cp_parser_identifier (parser);
25472 if (id == error_mark_node)
25473 break;
25475 objc_declare_class (id);
25477 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25478 cp_lexer_consume_token (parser->lexer);
25479 else
25480 break;
25482 cp_parser_consume_semicolon_at_end_of_statement (parser);
25485 /* Parse a list of Objective-C protocol references.
25487 objc-protocol-refs-opt:
25488 objc-protocol-refs [opt]
25490 objc-protocol-refs:
25491 < objc-identifier-list >
25493 Returns a TREE_LIST of identifiers, if any. */
25495 static tree
25496 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25498 tree protorefs = NULL_TREE;
25500 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25502 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25503 protorefs = cp_parser_objc_identifier_list (parser);
25504 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25507 return protorefs;
25510 /* Parse a Objective-C visibility specification. */
25512 static void
25513 cp_parser_objc_visibility_spec (cp_parser* parser)
25515 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25517 switch (vis->keyword)
25519 case RID_AT_PRIVATE:
25520 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25521 break;
25522 case RID_AT_PROTECTED:
25523 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25524 break;
25525 case RID_AT_PUBLIC:
25526 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25527 break;
25528 case RID_AT_PACKAGE:
25529 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25530 break;
25531 default:
25532 return;
25535 /* Eat '@private'/'@protected'/'@public'. */
25536 cp_lexer_consume_token (parser->lexer);
25539 /* Parse an Objective-C method type. Return 'true' if it is a class
25540 (+) method, and 'false' if it is an instance (-) method. */
25542 static inline bool
25543 cp_parser_objc_method_type (cp_parser* parser)
25545 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25546 return true;
25547 else
25548 return false;
25551 /* Parse an Objective-C protocol qualifier. */
25553 static tree
25554 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25556 tree quals = NULL_TREE, node;
25557 cp_token *token = cp_lexer_peek_token (parser->lexer);
25559 node = token->u.value;
25561 while (node && identifier_p (node)
25562 && (node == ridpointers [(int) RID_IN]
25563 || node == ridpointers [(int) RID_OUT]
25564 || node == ridpointers [(int) RID_INOUT]
25565 || node == ridpointers [(int) RID_BYCOPY]
25566 || node == ridpointers [(int) RID_BYREF]
25567 || node == ridpointers [(int) RID_ONEWAY]))
25569 quals = tree_cons (NULL_TREE, node, quals);
25570 cp_lexer_consume_token (parser->lexer);
25571 token = cp_lexer_peek_token (parser->lexer);
25572 node = token->u.value;
25575 return quals;
25578 /* Parse an Objective-C typename. */
25580 static tree
25581 cp_parser_objc_typename (cp_parser* parser)
25583 tree type_name = NULL_TREE;
25585 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25587 tree proto_quals, cp_type = NULL_TREE;
25589 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25590 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25592 /* An ObjC type name may consist of just protocol qualifiers, in which
25593 case the type shall default to 'id'. */
25594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25596 cp_type = cp_parser_type_id (parser);
25598 /* If the type could not be parsed, an error has already
25599 been produced. For error recovery, behave as if it had
25600 not been specified, which will use the default type
25601 'id'. */
25602 if (cp_type == error_mark_node)
25604 cp_type = NULL_TREE;
25605 /* We need to skip to the closing parenthesis as
25606 cp_parser_type_id() does not seem to do it for
25607 us. */
25608 cp_parser_skip_to_closing_parenthesis (parser,
25609 /*recovering=*/true,
25610 /*or_comma=*/false,
25611 /*consume_paren=*/false);
25615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25616 type_name = build_tree_list (proto_quals, cp_type);
25619 return type_name;
25622 /* Check to see if TYPE refers to an Objective-C selector name. */
25624 static bool
25625 cp_parser_objc_selector_p (enum cpp_ttype type)
25627 return (type == CPP_NAME || type == CPP_KEYWORD
25628 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25629 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25630 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25631 || type == CPP_XOR || type == CPP_XOR_EQ);
25634 /* Parse an Objective-C selector. */
25636 static tree
25637 cp_parser_objc_selector (cp_parser* parser)
25639 cp_token *token = cp_lexer_consume_token (parser->lexer);
25641 if (!cp_parser_objc_selector_p (token->type))
25643 error_at (token->location, "invalid Objective-C++ selector name");
25644 return error_mark_node;
25647 /* C++ operator names are allowed to appear in ObjC selectors. */
25648 switch (token->type)
25650 case CPP_AND_AND: return get_identifier ("and");
25651 case CPP_AND_EQ: return get_identifier ("and_eq");
25652 case CPP_AND: return get_identifier ("bitand");
25653 case CPP_OR: return get_identifier ("bitor");
25654 case CPP_COMPL: return get_identifier ("compl");
25655 case CPP_NOT: return get_identifier ("not");
25656 case CPP_NOT_EQ: return get_identifier ("not_eq");
25657 case CPP_OR_OR: return get_identifier ("or");
25658 case CPP_OR_EQ: return get_identifier ("or_eq");
25659 case CPP_XOR: return get_identifier ("xor");
25660 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25661 default: return token->u.value;
25665 /* Parse an Objective-C params list. */
25667 static tree
25668 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25670 tree params = NULL_TREE;
25671 bool maybe_unary_selector_p = true;
25672 cp_token *token = cp_lexer_peek_token (parser->lexer);
25674 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25676 tree selector = NULL_TREE, type_name, identifier;
25677 tree parm_attr = NULL_TREE;
25679 if (token->keyword == RID_ATTRIBUTE)
25680 break;
25682 if (token->type != CPP_COLON)
25683 selector = cp_parser_objc_selector (parser);
25685 /* Detect if we have a unary selector. */
25686 if (maybe_unary_selector_p
25687 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25689 params = selector; /* Might be followed by attributes. */
25690 break;
25693 maybe_unary_selector_p = false;
25694 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25696 /* Something went quite wrong. There should be a colon
25697 here, but there is not. Stop parsing parameters. */
25698 break;
25700 type_name = cp_parser_objc_typename (parser);
25701 /* New ObjC allows attributes on parameters too. */
25702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25703 parm_attr = cp_parser_attributes_opt (parser);
25704 identifier = cp_parser_identifier (parser);
25706 params
25707 = chainon (params,
25708 objc_build_keyword_decl (selector,
25709 type_name,
25710 identifier,
25711 parm_attr));
25713 token = cp_lexer_peek_token (parser->lexer);
25716 if (params == NULL_TREE)
25718 cp_parser_error (parser, "objective-c++ method declaration is expected");
25719 return error_mark_node;
25722 /* We allow tail attributes for the method. */
25723 if (token->keyword == RID_ATTRIBUTE)
25725 *attributes = cp_parser_attributes_opt (parser);
25726 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25727 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25728 return params;
25729 cp_parser_error (parser,
25730 "method attributes must be specified at the end");
25731 return error_mark_node;
25734 if (params == NULL_TREE)
25736 cp_parser_error (parser, "objective-c++ method declaration is expected");
25737 return error_mark_node;
25739 return params;
25742 /* Parse the non-keyword Objective-C params. */
25744 static tree
25745 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25746 tree* attributes)
25748 tree params = make_node (TREE_LIST);
25749 cp_token *token = cp_lexer_peek_token (parser->lexer);
25750 *ellipsisp = false; /* Initially, assume no ellipsis. */
25752 while (token->type == CPP_COMMA)
25754 cp_parameter_declarator *parmdecl;
25755 tree parm;
25757 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25758 token = cp_lexer_peek_token (parser->lexer);
25760 if (token->type == CPP_ELLIPSIS)
25762 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25763 *ellipsisp = true;
25764 token = cp_lexer_peek_token (parser->lexer);
25765 break;
25768 /* TODO: parse attributes for tail parameters. */
25769 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25770 parm = grokdeclarator (parmdecl->declarator,
25771 &parmdecl->decl_specifiers,
25772 PARM, /*initialized=*/0,
25773 /*attrlist=*/NULL);
25775 chainon (params, build_tree_list (NULL_TREE, parm));
25776 token = cp_lexer_peek_token (parser->lexer);
25779 /* We allow tail attributes for the method. */
25780 if (token->keyword == RID_ATTRIBUTE)
25782 if (*attributes == NULL_TREE)
25784 *attributes = cp_parser_attributes_opt (parser);
25785 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25786 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25787 return params;
25789 else
25790 /* We have an error, but parse the attributes, so that we can
25791 carry on. */
25792 *attributes = cp_parser_attributes_opt (parser);
25794 cp_parser_error (parser,
25795 "method attributes must be specified at the end");
25796 return error_mark_node;
25799 return params;
25802 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25804 static void
25805 cp_parser_objc_interstitial_code (cp_parser* parser)
25807 cp_token *token = cp_lexer_peek_token (parser->lexer);
25809 /* If the next token is `extern' and the following token is a string
25810 literal, then we have a linkage specification. */
25811 if (token->keyword == RID_EXTERN
25812 && cp_parser_is_pure_string_literal
25813 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25814 cp_parser_linkage_specification (parser);
25815 /* Handle #pragma, if any. */
25816 else if (token->type == CPP_PRAGMA)
25817 cp_parser_pragma (parser, pragma_objc_icode);
25818 /* Allow stray semicolons. */
25819 else if (token->type == CPP_SEMICOLON)
25820 cp_lexer_consume_token (parser->lexer);
25821 /* Mark methods as optional or required, when building protocols. */
25822 else if (token->keyword == RID_AT_OPTIONAL)
25824 cp_lexer_consume_token (parser->lexer);
25825 objc_set_method_opt (true);
25827 else if (token->keyword == RID_AT_REQUIRED)
25829 cp_lexer_consume_token (parser->lexer);
25830 objc_set_method_opt (false);
25832 else if (token->keyword == RID_NAMESPACE)
25833 cp_parser_namespace_definition (parser);
25834 /* Other stray characters must generate errors. */
25835 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25837 cp_lexer_consume_token (parser->lexer);
25838 error ("stray %qs between Objective-C++ methods",
25839 token->type == CPP_OPEN_BRACE ? "{" : "}");
25841 /* Finally, try to parse a block-declaration, or a function-definition. */
25842 else
25843 cp_parser_block_declaration (parser, /*statement_p=*/false);
25846 /* Parse a method signature. */
25848 static tree
25849 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25851 tree rettype, kwdparms, optparms;
25852 bool ellipsis = false;
25853 bool is_class_method;
25855 is_class_method = cp_parser_objc_method_type (parser);
25856 rettype = cp_parser_objc_typename (parser);
25857 *attributes = NULL_TREE;
25858 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25859 if (kwdparms == error_mark_node)
25860 return error_mark_node;
25861 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25862 if (optparms == error_mark_node)
25863 return error_mark_node;
25865 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25868 static bool
25869 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25871 tree tattr;
25872 cp_lexer_save_tokens (parser->lexer);
25873 tattr = cp_parser_attributes_opt (parser);
25874 gcc_assert (tattr) ;
25876 /* If the attributes are followed by a method introducer, this is not allowed.
25877 Dump the attributes and flag the situation. */
25878 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25879 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25880 return true;
25882 /* Otherwise, the attributes introduce some interstitial code, possibly so
25883 rewind to allow that check. */
25884 cp_lexer_rollback_tokens (parser->lexer);
25885 return false;
25888 /* Parse an Objective-C method prototype list. */
25890 static void
25891 cp_parser_objc_method_prototype_list (cp_parser* parser)
25893 cp_token *token = cp_lexer_peek_token (parser->lexer);
25895 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25897 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25899 tree attributes, sig;
25900 bool is_class_method;
25901 if (token->type == CPP_PLUS)
25902 is_class_method = true;
25903 else
25904 is_class_method = false;
25905 sig = cp_parser_objc_method_signature (parser, &attributes);
25906 if (sig == error_mark_node)
25908 cp_parser_skip_to_end_of_block_or_statement (parser);
25909 token = cp_lexer_peek_token (parser->lexer);
25910 continue;
25912 objc_add_method_declaration (is_class_method, sig, attributes);
25913 cp_parser_consume_semicolon_at_end_of_statement (parser);
25915 else if (token->keyword == RID_AT_PROPERTY)
25916 cp_parser_objc_at_property_declaration (parser);
25917 else if (token->keyword == RID_ATTRIBUTE
25918 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25919 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25920 OPT_Wattributes,
25921 "prefix attributes are ignored for methods");
25922 else
25923 /* Allow for interspersed non-ObjC++ code. */
25924 cp_parser_objc_interstitial_code (parser);
25926 token = cp_lexer_peek_token (parser->lexer);
25929 if (token->type != CPP_EOF)
25930 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25931 else
25932 cp_parser_error (parser, "expected %<@end%>");
25934 objc_finish_interface ();
25937 /* Parse an Objective-C method definition list. */
25939 static void
25940 cp_parser_objc_method_definition_list (cp_parser* parser)
25942 cp_token *token = cp_lexer_peek_token (parser->lexer);
25944 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25946 tree meth;
25948 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25950 cp_token *ptk;
25951 tree sig, attribute;
25952 bool is_class_method;
25953 if (token->type == CPP_PLUS)
25954 is_class_method = true;
25955 else
25956 is_class_method = false;
25957 push_deferring_access_checks (dk_deferred);
25958 sig = cp_parser_objc_method_signature (parser, &attribute);
25959 if (sig == error_mark_node)
25961 cp_parser_skip_to_end_of_block_or_statement (parser);
25962 token = cp_lexer_peek_token (parser->lexer);
25963 continue;
25965 objc_start_method_definition (is_class_method, sig, attribute,
25966 NULL_TREE);
25968 /* For historical reasons, we accept an optional semicolon. */
25969 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25970 cp_lexer_consume_token (parser->lexer);
25972 ptk = cp_lexer_peek_token (parser->lexer);
25973 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25974 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25976 perform_deferred_access_checks (tf_warning_or_error);
25977 stop_deferring_access_checks ();
25978 meth = cp_parser_function_definition_after_declarator (parser,
25979 false);
25980 pop_deferring_access_checks ();
25981 objc_finish_method_definition (meth);
25984 /* The following case will be removed once @synthesize is
25985 completely implemented. */
25986 else if (token->keyword == RID_AT_PROPERTY)
25987 cp_parser_objc_at_property_declaration (parser);
25988 else if (token->keyword == RID_AT_SYNTHESIZE)
25989 cp_parser_objc_at_synthesize_declaration (parser);
25990 else if (token->keyword == RID_AT_DYNAMIC)
25991 cp_parser_objc_at_dynamic_declaration (parser);
25992 else if (token->keyword == RID_ATTRIBUTE
25993 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25994 warning_at (token->location, OPT_Wattributes,
25995 "prefix attributes are ignored for methods");
25996 else
25997 /* Allow for interspersed non-ObjC++ code. */
25998 cp_parser_objc_interstitial_code (parser);
26000 token = cp_lexer_peek_token (parser->lexer);
26003 if (token->type != CPP_EOF)
26004 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26005 else
26006 cp_parser_error (parser, "expected %<@end%>");
26008 objc_finish_implementation ();
26011 /* Parse Objective-C ivars. */
26013 static void
26014 cp_parser_objc_class_ivars (cp_parser* parser)
26016 cp_token *token = cp_lexer_peek_token (parser->lexer);
26018 if (token->type != CPP_OPEN_BRACE)
26019 return; /* No ivars specified. */
26021 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26022 token = cp_lexer_peek_token (parser->lexer);
26024 while (token->type != CPP_CLOSE_BRACE
26025 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26027 cp_decl_specifier_seq declspecs;
26028 int decl_class_or_enum_p;
26029 tree prefix_attributes;
26031 cp_parser_objc_visibility_spec (parser);
26033 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26034 break;
26036 cp_parser_decl_specifier_seq (parser,
26037 CP_PARSER_FLAGS_OPTIONAL,
26038 &declspecs,
26039 &decl_class_or_enum_p);
26041 /* auto, register, static, extern, mutable. */
26042 if (declspecs.storage_class != sc_none)
26044 cp_parser_error (parser, "invalid type for instance variable");
26045 declspecs.storage_class = sc_none;
26048 /* thread_local. */
26049 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26051 cp_parser_error (parser, "invalid type for instance variable");
26052 declspecs.locations[ds_thread] = 0;
26055 /* typedef. */
26056 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26058 cp_parser_error (parser, "invalid type for instance variable");
26059 declspecs.locations[ds_typedef] = 0;
26062 prefix_attributes = declspecs.attributes;
26063 declspecs.attributes = NULL_TREE;
26065 /* Keep going until we hit the `;' at the end of the
26066 declaration. */
26067 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26069 tree width = NULL_TREE, attributes, first_attribute, decl;
26070 cp_declarator *declarator = NULL;
26071 int ctor_dtor_or_conv_p;
26073 /* Check for a (possibly unnamed) bitfield declaration. */
26074 token = cp_lexer_peek_token (parser->lexer);
26075 if (token->type == CPP_COLON)
26076 goto eat_colon;
26078 if (token->type == CPP_NAME
26079 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26080 == CPP_COLON))
26082 /* Get the name of the bitfield. */
26083 declarator = make_id_declarator (NULL_TREE,
26084 cp_parser_identifier (parser),
26085 sfk_none);
26087 eat_colon:
26088 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26089 /* Get the width of the bitfield. */
26090 width
26091 = cp_parser_constant_expression (parser,
26092 /*allow_non_constant=*/false,
26093 NULL);
26095 else
26097 /* Parse the declarator. */
26098 declarator
26099 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26100 &ctor_dtor_or_conv_p,
26101 /*parenthesized_p=*/NULL,
26102 /*member_p=*/false);
26105 /* Look for attributes that apply to the ivar. */
26106 attributes = cp_parser_attributes_opt (parser);
26107 /* Remember which attributes are prefix attributes and
26108 which are not. */
26109 first_attribute = attributes;
26110 /* Combine the attributes. */
26111 attributes = chainon (prefix_attributes, attributes);
26113 if (width)
26114 /* Create the bitfield declaration. */
26115 decl = grokbitfield (declarator, &declspecs,
26116 width,
26117 attributes);
26118 else
26119 decl = grokfield (declarator, &declspecs,
26120 NULL_TREE, /*init_const_expr_p=*/false,
26121 NULL_TREE, attributes);
26123 /* Add the instance variable. */
26124 if (decl != error_mark_node && decl != NULL_TREE)
26125 objc_add_instance_variable (decl);
26127 /* Reset PREFIX_ATTRIBUTES. */
26128 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26129 attributes = TREE_CHAIN (attributes);
26130 if (attributes)
26131 TREE_CHAIN (attributes) = NULL_TREE;
26133 token = cp_lexer_peek_token (parser->lexer);
26135 if (token->type == CPP_COMMA)
26137 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26138 continue;
26140 break;
26143 cp_parser_consume_semicolon_at_end_of_statement (parser);
26144 token = cp_lexer_peek_token (parser->lexer);
26147 if (token->keyword == RID_AT_END)
26148 cp_parser_error (parser, "expected %<}%>");
26150 /* Do not consume the RID_AT_END, so it will be read again as terminating
26151 the @interface of @implementation. */
26152 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26153 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26155 /* For historical reasons, we accept an optional semicolon. */
26156 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26157 cp_lexer_consume_token (parser->lexer);
26160 /* Parse an Objective-C protocol declaration. */
26162 static void
26163 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26165 tree proto, protorefs;
26166 cp_token *tok;
26168 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26171 tok = cp_lexer_peek_token (parser->lexer);
26172 error_at (tok->location, "identifier expected after %<@protocol%>");
26173 cp_parser_consume_semicolon_at_end_of_statement (parser);
26174 return;
26177 /* See if we have a forward declaration or a definition. */
26178 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26180 /* Try a forward declaration first. */
26181 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26183 while (true)
26185 tree id;
26187 id = cp_parser_identifier (parser);
26188 if (id == error_mark_node)
26189 break;
26191 objc_declare_protocol (id, attributes);
26193 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26194 cp_lexer_consume_token (parser->lexer);
26195 else
26196 break;
26198 cp_parser_consume_semicolon_at_end_of_statement (parser);
26201 /* Ok, we got a full-fledged definition (or at least should). */
26202 else
26204 proto = cp_parser_identifier (parser);
26205 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26206 objc_start_protocol (proto, protorefs, attributes);
26207 cp_parser_objc_method_prototype_list (parser);
26211 /* Parse an Objective-C superclass or category. */
26213 static void
26214 cp_parser_objc_superclass_or_category (cp_parser *parser,
26215 bool iface_p,
26216 tree *super,
26217 tree *categ, bool *is_class_extension)
26219 cp_token *next = cp_lexer_peek_token (parser->lexer);
26221 *super = *categ = NULL_TREE;
26222 *is_class_extension = false;
26223 if (next->type == CPP_COLON)
26225 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26226 *super = cp_parser_identifier (parser);
26228 else if (next->type == CPP_OPEN_PAREN)
26230 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26232 /* If there is no category name, and this is an @interface, we
26233 have a class extension. */
26234 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26236 *categ = NULL_TREE;
26237 *is_class_extension = true;
26239 else
26240 *categ = cp_parser_identifier (parser);
26242 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26246 /* Parse an Objective-C class interface. */
26248 static void
26249 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26251 tree name, super, categ, protos;
26252 bool is_class_extension;
26254 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26255 name = cp_parser_identifier (parser);
26256 if (name == error_mark_node)
26258 /* It's hard to recover because even if valid @interface stuff
26259 is to follow, we can't compile it (or validate it) if we
26260 don't even know which class it refers to. Let's assume this
26261 was a stray '@interface' token in the stream and skip it.
26263 return;
26265 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26266 &is_class_extension);
26267 protos = cp_parser_objc_protocol_refs_opt (parser);
26269 /* We have either a class or a category on our hands. */
26270 if (categ || is_class_extension)
26271 objc_start_category_interface (name, categ, protos, attributes);
26272 else
26274 objc_start_class_interface (name, super, protos, attributes);
26275 /* Handle instance variable declarations, if any. */
26276 cp_parser_objc_class_ivars (parser);
26277 objc_continue_interface ();
26280 cp_parser_objc_method_prototype_list (parser);
26283 /* Parse an Objective-C class implementation. */
26285 static void
26286 cp_parser_objc_class_implementation (cp_parser* parser)
26288 tree name, super, categ;
26289 bool is_class_extension;
26291 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26292 name = cp_parser_identifier (parser);
26293 if (name == error_mark_node)
26295 /* It's hard to recover because even if valid @implementation
26296 stuff is to follow, we can't compile it (or validate it) if
26297 we don't even know which class it refers to. Let's assume
26298 this was a stray '@implementation' token in the stream and
26299 skip it.
26301 return;
26303 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26304 &is_class_extension);
26306 /* We have either a class or a category on our hands. */
26307 if (categ)
26308 objc_start_category_implementation (name, categ);
26309 else
26311 objc_start_class_implementation (name, super);
26312 /* Handle instance variable declarations, if any. */
26313 cp_parser_objc_class_ivars (parser);
26314 objc_continue_implementation ();
26317 cp_parser_objc_method_definition_list (parser);
26320 /* Consume the @end token and finish off the implementation. */
26322 static void
26323 cp_parser_objc_end_implementation (cp_parser* parser)
26325 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26326 objc_finish_implementation ();
26329 /* Parse an Objective-C declaration. */
26331 static void
26332 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26334 /* Try to figure out what kind of declaration is present. */
26335 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26337 if (attributes)
26338 switch (kwd->keyword)
26340 case RID_AT_ALIAS:
26341 case RID_AT_CLASS:
26342 case RID_AT_END:
26343 error_at (kwd->location, "attributes may not be specified before"
26344 " the %<@%D%> Objective-C++ keyword",
26345 kwd->u.value);
26346 attributes = NULL;
26347 break;
26348 case RID_AT_IMPLEMENTATION:
26349 warning_at (kwd->location, OPT_Wattributes,
26350 "prefix attributes are ignored before %<@%D%>",
26351 kwd->u.value);
26352 attributes = NULL;
26353 default:
26354 break;
26357 switch (kwd->keyword)
26359 case RID_AT_ALIAS:
26360 cp_parser_objc_alias_declaration (parser);
26361 break;
26362 case RID_AT_CLASS:
26363 cp_parser_objc_class_declaration (parser);
26364 break;
26365 case RID_AT_PROTOCOL:
26366 cp_parser_objc_protocol_declaration (parser, attributes);
26367 break;
26368 case RID_AT_INTERFACE:
26369 cp_parser_objc_class_interface (parser, attributes);
26370 break;
26371 case RID_AT_IMPLEMENTATION:
26372 cp_parser_objc_class_implementation (parser);
26373 break;
26374 case RID_AT_END:
26375 cp_parser_objc_end_implementation (parser);
26376 break;
26377 default:
26378 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26379 kwd->u.value);
26380 cp_parser_skip_to_end_of_block_or_statement (parser);
26384 /* Parse an Objective-C try-catch-finally statement.
26386 objc-try-catch-finally-stmt:
26387 @try compound-statement objc-catch-clause-seq [opt]
26388 objc-finally-clause [opt]
26390 objc-catch-clause-seq:
26391 objc-catch-clause objc-catch-clause-seq [opt]
26393 objc-catch-clause:
26394 @catch ( objc-exception-declaration ) compound-statement
26396 objc-finally-clause:
26397 @finally compound-statement
26399 objc-exception-declaration:
26400 parameter-declaration
26401 '...'
26403 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26405 Returns NULL_TREE.
26407 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26408 for C. Keep them in sync. */
26410 static tree
26411 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26413 location_t location;
26414 tree stmt;
26416 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26417 location = cp_lexer_peek_token (parser->lexer)->location;
26418 objc_maybe_warn_exceptions (location);
26419 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26420 node, lest it get absorbed into the surrounding block. */
26421 stmt = push_stmt_list ();
26422 cp_parser_compound_statement (parser, NULL, false, false);
26423 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26425 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26427 cp_parameter_declarator *parm;
26428 tree parameter_declaration = error_mark_node;
26429 bool seen_open_paren = false;
26431 cp_lexer_consume_token (parser->lexer);
26432 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26433 seen_open_paren = true;
26434 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26436 /* We have "@catch (...)" (where the '...' are literally
26437 what is in the code). Skip the '...'.
26438 parameter_declaration is set to NULL_TREE, and
26439 objc_being_catch_clauses() knows that that means
26440 '...'. */
26441 cp_lexer_consume_token (parser->lexer);
26442 parameter_declaration = NULL_TREE;
26444 else
26446 /* We have "@catch (NSException *exception)" or something
26447 like that. Parse the parameter declaration. */
26448 parm = cp_parser_parameter_declaration (parser, false, NULL);
26449 if (parm == NULL)
26450 parameter_declaration = error_mark_node;
26451 else
26452 parameter_declaration = grokdeclarator (parm->declarator,
26453 &parm->decl_specifiers,
26454 PARM, /*initialized=*/0,
26455 /*attrlist=*/NULL);
26457 if (seen_open_paren)
26458 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26459 else
26461 /* If there was no open parenthesis, we are recovering from
26462 an error, and we are trying to figure out what mistake
26463 the user has made. */
26465 /* If there is an immediate closing parenthesis, the user
26466 probably forgot the opening one (ie, they typed "@catch
26467 NSException *e)". Parse the closing parenthesis and keep
26468 going. */
26469 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26470 cp_lexer_consume_token (parser->lexer);
26472 /* If these is no immediate closing parenthesis, the user
26473 probably doesn't know that parenthesis are required at
26474 all (ie, they typed "@catch NSException *e"). So, just
26475 forget about the closing parenthesis and keep going. */
26477 objc_begin_catch_clause (parameter_declaration);
26478 cp_parser_compound_statement (parser, NULL, false, false);
26479 objc_finish_catch_clause ();
26481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26483 cp_lexer_consume_token (parser->lexer);
26484 location = cp_lexer_peek_token (parser->lexer)->location;
26485 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26486 node, lest it get absorbed into the surrounding block. */
26487 stmt = push_stmt_list ();
26488 cp_parser_compound_statement (parser, NULL, false, false);
26489 objc_build_finally_clause (location, pop_stmt_list (stmt));
26492 return objc_finish_try_stmt ();
26495 /* Parse an Objective-C synchronized statement.
26497 objc-synchronized-stmt:
26498 @synchronized ( expression ) compound-statement
26500 Returns NULL_TREE. */
26502 static tree
26503 cp_parser_objc_synchronized_statement (cp_parser *parser)
26505 location_t location;
26506 tree lock, stmt;
26508 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26510 location = cp_lexer_peek_token (parser->lexer)->location;
26511 objc_maybe_warn_exceptions (location);
26512 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26513 lock = cp_parser_expression (parser, false, NULL);
26514 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26516 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26517 node, lest it get absorbed into the surrounding block. */
26518 stmt = push_stmt_list ();
26519 cp_parser_compound_statement (parser, NULL, false, false);
26521 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26524 /* Parse an Objective-C throw statement.
26526 objc-throw-stmt:
26527 @throw assignment-expression [opt] ;
26529 Returns a constructed '@throw' statement. */
26531 static tree
26532 cp_parser_objc_throw_statement (cp_parser *parser)
26534 tree expr = NULL_TREE;
26535 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26537 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26540 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26542 cp_parser_consume_semicolon_at_end_of_statement (parser);
26544 return objc_build_throw_stmt (loc, expr);
26547 /* Parse an Objective-C statement. */
26549 static tree
26550 cp_parser_objc_statement (cp_parser * parser)
26552 /* Try to figure out what kind of declaration is present. */
26553 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26555 switch (kwd->keyword)
26557 case RID_AT_TRY:
26558 return cp_parser_objc_try_catch_finally_statement (parser);
26559 case RID_AT_SYNCHRONIZED:
26560 return cp_parser_objc_synchronized_statement (parser);
26561 case RID_AT_THROW:
26562 return cp_parser_objc_throw_statement (parser);
26563 default:
26564 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26565 kwd->u.value);
26566 cp_parser_skip_to_end_of_block_or_statement (parser);
26569 return error_mark_node;
26572 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26573 look ahead to see if an objc keyword follows the attributes. This
26574 is to detect the use of prefix attributes on ObjC @interface and
26575 @protocol. */
26577 static bool
26578 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26580 cp_lexer_save_tokens (parser->lexer);
26581 *attrib = cp_parser_attributes_opt (parser);
26582 gcc_assert (*attrib);
26583 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26585 cp_lexer_commit_tokens (parser->lexer);
26586 return true;
26588 cp_lexer_rollback_tokens (parser->lexer);
26589 return false;
26592 /* This routine is a minimal replacement for
26593 c_parser_struct_declaration () used when parsing the list of
26594 types/names or ObjC++ properties. For example, when parsing the
26595 code
26597 @property (readonly) int a, b, c;
26599 this function is responsible for parsing "int a, int b, int c" and
26600 returning the declarations as CHAIN of DECLs.
26602 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26603 similar parsing. */
26604 static tree
26605 cp_parser_objc_struct_declaration (cp_parser *parser)
26607 tree decls = NULL_TREE;
26608 cp_decl_specifier_seq declspecs;
26609 int decl_class_or_enum_p;
26610 tree prefix_attributes;
26612 cp_parser_decl_specifier_seq (parser,
26613 CP_PARSER_FLAGS_NONE,
26614 &declspecs,
26615 &decl_class_or_enum_p);
26617 if (declspecs.type == error_mark_node)
26618 return error_mark_node;
26620 /* auto, register, static, extern, mutable. */
26621 if (declspecs.storage_class != sc_none)
26623 cp_parser_error (parser, "invalid type for property");
26624 declspecs.storage_class = sc_none;
26627 /* thread_local. */
26628 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26630 cp_parser_error (parser, "invalid type for property");
26631 declspecs.locations[ds_thread] = 0;
26634 /* typedef. */
26635 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26637 cp_parser_error (parser, "invalid type for property");
26638 declspecs.locations[ds_typedef] = 0;
26641 prefix_attributes = declspecs.attributes;
26642 declspecs.attributes = NULL_TREE;
26644 /* Keep going until we hit the `;' at the end of the declaration. */
26645 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26647 tree attributes, first_attribute, decl;
26648 cp_declarator *declarator;
26649 cp_token *token;
26651 /* Parse the declarator. */
26652 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26653 NULL, NULL, false);
26655 /* Look for attributes that apply to the ivar. */
26656 attributes = cp_parser_attributes_opt (parser);
26657 /* Remember which attributes are prefix attributes and
26658 which are not. */
26659 first_attribute = attributes;
26660 /* Combine the attributes. */
26661 attributes = chainon (prefix_attributes, attributes);
26663 decl = grokfield (declarator, &declspecs,
26664 NULL_TREE, /*init_const_expr_p=*/false,
26665 NULL_TREE, attributes);
26667 if (decl == error_mark_node || decl == NULL_TREE)
26668 return error_mark_node;
26670 /* Reset PREFIX_ATTRIBUTES. */
26671 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26672 attributes = TREE_CHAIN (attributes);
26673 if (attributes)
26674 TREE_CHAIN (attributes) = NULL_TREE;
26676 DECL_CHAIN (decl) = decls;
26677 decls = decl;
26679 token = cp_lexer_peek_token (parser->lexer);
26680 if (token->type == CPP_COMMA)
26682 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26683 continue;
26685 else
26686 break;
26688 return decls;
26691 /* Parse an Objective-C @property declaration. The syntax is:
26693 objc-property-declaration:
26694 '@property' objc-property-attributes[opt] struct-declaration ;
26696 objc-property-attributes:
26697 '(' objc-property-attribute-list ')'
26699 objc-property-attribute-list:
26700 objc-property-attribute
26701 objc-property-attribute-list, objc-property-attribute
26703 objc-property-attribute
26704 'getter' = identifier
26705 'setter' = identifier
26706 'readonly'
26707 'readwrite'
26708 'assign'
26709 'retain'
26710 'copy'
26711 'nonatomic'
26713 For example:
26714 @property NSString *name;
26715 @property (readonly) id object;
26716 @property (retain, nonatomic, getter=getTheName) id name;
26717 @property int a, b, c;
26719 PS: This function is identical to
26720 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26721 static void
26722 cp_parser_objc_at_property_declaration (cp_parser *parser)
26724 /* The following variables hold the attributes of the properties as
26725 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26726 seen. When we see an attribute, we set them to 'true' (if they
26727 are boolean properties) or to the identifier (if they have an
26728 argument, ie, for getter and setter). Note that here we only
26729 parse the list of attributes, check the syntax and accumulate the
26730 attributes that we find. objc_add_property_declaration() will
26731 then process the information. */
26732 bool property_assign = false;
26733 bool property_copy = false;
26734 tree property_getter_ident = NULL_TREE;
26735 bool property_nonatomic = false;
26736 bool property_readonly = false;
26737 bool property_readwrite = false;
26738 bool property_retain = false;
26739 tree property_setter_ident = NULL_TREE;
26741 /* 'properties' is the list of properties that we read. Usually a
26742 single one, but maybe more (eg, in "@property int a, b, c;" there
26743 are three). */
26744 tree properties;
26745 location_t loc;
26747 loc = cp_lexer_peek_token (parser->lexer)->location;
26749 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26751 /* Parse the optional attribute list... */
26752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26754 /* Eat the '('. */
26755 cp_lexer_consume_token (parser->lexer);
26757 while (true)
26759 bool syntax_error = false;
26760 cp_token *token = cp_lexer_peek_token (parser->lexer);
26761 enum rid keyword;
26763 if (token->type != CPP_NAME)
26765 cp_parser_error (parser, "expected identifier");
26766 break;
26768 keyword = C_RID_CODE (token->u.value);
26769 cp_lexer_consume_token (parser->lexer);
26770 switch (keyword)
26772 case RID_ASSIGN: property_assign = true; break;
26773 case RID_COPY: property_copy = true; break;
26774 case RID_NONATOMIC: property_nonatomic = true; break;
26775 case RID_READONLY: property_readonly = true; break;
26776 case RID_READWRITE: property_readwrite = true; break;
26777 case RID_RETAIN: property_retain = true; break;
26779 case RID_GETTER:
26780 case RID_SETTER:
26781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26783 if (keyword == RID_GETTER)
26784 cp_parser_error (parser,
26785 "missing %<=%> (after %<getter%> attribute)");
26786 else
26787 cp_parser_error (parser,
26788 "missing %<=%> (after %<setter%> attribute)");
26789 syntax_error = true;
26790 break;
26792 cp_lexer_consume_token (parser->lexer); /* eat the = */
26793 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26795 cp_parser_error (parser, "expected identifier");
26796 syntax_error = true;
26797 break;
26799 if (keyword == RID_SETTER)
26801 if (property_setter_ident != NULL_TREE)
26803 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26804 cp_lexer_consume_token (parser->lexer);
26806 else
26807 property_setter_ident = cp_parser_objc_selector (parser);
26808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26809 cp_parser_error (parser, "setter name must terminate with %<:%>");
26810 else
26811 cp_lexer_consume_token (parser->lexer);
26813 else
26815 if (property_getter_ident != NULL_TREE)
26817 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26818 cp_lexer_consume_token (parser->lexer);
26820 else
26821 property_getter_ident = cp_parser_objc_selector (parser);
26823 break;
26824 default:
26825 cp_parser_error (parser, "unknown property attribute");
26826 syntax_error = true;
26827 break;
26830 if (syntax_error)
26831 break;
26833 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26834 cp_lexer_consume_token (parser->lexer);
26835 else
26836 break;
26839 /* FIXME: "@property (setter, assign);" will generate a spurious
26840 "error: expected ‘)’ before ‘,’ token". This is because
26841 cp_parser_require, unlike the C counterpart, will produce an
26842 error even if we are in error recovery. */
26843 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26845 cp_parser_skip_to_closing_parenthesis (parser,
26846 /*recovering=*/true,
26847 /*or_comma=*/false,
26848 /*consume_paren=*/true);
26852 /* ... and the property declaration(s). */
26853 properties = cp_parser_objc_struct_declaration (parser);
26855 if (properties == error_mark_node)
26857 cp_parser_skip_to_end_of_statement (parser);
26858 /* If the next token is now a `;', consume it. */
26859 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26860 cp_lexer_consume_token (parser->lexer);
26861 return;
26864 if (properties == NULL_TREE)
26865 cp_parser_error (parser, "expected identifier");
26866 else
26868 /* Comma-separated properties are chained together in
26869 reverse order; add them one by one. */
26870 properties = nreverse (properties);
26872 for (; properties; properties = TREE_CHAIN (properties))
26873 objc_add_property_declaration (loc, copy_node (properties),
26874 property_readonly, property_readwrite,
26875 property_assign, property_retain,
26876 property_copy, property_nonatomic,
26877 property_getter_ident, property_setter_ident);
26880 cp_parser_consume_semicolon_at_end_of_statement (parser);
26883 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26885 objc-synthesize-declaration:
26886 @synthesize objc-synthesize-identifier-list ;
26888 objc-synthesize-identifier-list:
26889 objc-synthesize-identifier
26890 objc-synthesize-identifier-list, objc-synthesize-identifier
26892 objc-synthesize-identifier
26893 identifier
26894 identifier = identifier
26896 For example:
26897 @synthesize MyProperty;
26898 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26900 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26901 for C. Keep them in sync.
26903 static void
26904 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26906 tree list = NULL_TREE;
26907 location_t loc;
26908 loc = cp_lexer_peek_token (parser->lexer)->location;
26910 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26911 while (true)
26913 tree property, ivar;
26914 property = cp_parser_identifier (parser);
26915 if (property == error_mark_node)
26917 cp_parser_consume_semicolon_at_end_of_statement (parser);
26918 return;
26920 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26922 cp_lexer_consume_token (parser->lexer);
26923 ivar = cp_parser_identifier (parser);
26924 if (ivar == error_mark_node)
26926 cp_parser_consume_semicolon_at_end_of_statement (parser);
26927 return;
26930 else
26931 ivar = NULL_TREE;
26932 list = chainon (list, build_tree_list (ivar, property));
26933 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26934 cp_lexer_consume_token (parser->lexer);
26935 else
26936 break;
26938 cp_parser_consume_semicolon_at_end_of_statement (parser);
26939 objc_add_synthesize_declaration (loc, list);
26942 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26944 objc-dynamic-declaration:
26945 @dynamic identifier-list ;
26947 For example:
26948 @dynamic MyProperty;
26949 @dynamic MyProperty, AnotherProperty;
26951 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26952 for C. Keep them in sync.
26954 static void
26955 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26957 tree list = NULL_TREE;
26958 location_t loc;
26959 loc = cp_lexer_peek_token (parser->lexer)->location;
26961 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26962 while (true)
26964 tree property;
26965 property = cp_parser_identifier (parser);
26966 if (property == error_mark_node)
26968 cp_parser_consume_semicolon_at_end_of_statement (parser);
26969 return;
26971 list = chainon (list, build_tree_list (NULL, property));
26972 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26973 cp_lexer_consume_token (parser->lexer);
26974 else
26975 break;
26977 cp_parser_consume_semicolon_at_end_of_statement (parser);
26978 objc_add_dynamic_declaration (loc, list);
26982 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26984 /* Returns name of the next clause.
26985 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26986 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26987 returned and the token is consumed. */
26989 static pragma_omp_clause
26990 cp_parser_omp_clause_name (cp_parser *parser)
26992 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26994 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26995 result = PRAGMA_OMP_CLAUSE_IF;
26996 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26997 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26998 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26999 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27000 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27001 result = PRAGMA_OMP_CLAUSE_FOR;
27002 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27004 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27005 const char *p = IDENTIFIER_POINTER (id);
27007 switch (p[0])
27009 case 'a':
27010 if (!strcmp ("aligned", p))
27011 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27012 break;
27013 case 'c':
27014 if (!strcmp ("collapse", p))
27015 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27016 else if (!strcmp ("copyin", p))
27017 result = PRAGMA_OMP_CLAUSE_COPYIN;
27018 else if (!strcmp ("copyprivate", p))
27019 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27020 break;
27021 case 'd':
27022 if (!strcmp ("depend", p))
27023 result = PRAGMA_OMP_CLAUSE_DEPEND;
27024 else if (!strcmp ("device", p))
27025 result = PRAGMA_OMP_CLAUSE_DEVICE;
27026 else if (!strcmp ("dist_schedule", p))
27027 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27028 break;
27029 case 'f':
27030 if (!strcmp ("final", p))
27031 result = PRAGMA_OMP_CLAUSE_FINAL;
27032 else if (!strcmp ("firstprivate", p))
27033 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27034 else if (!strcmp ("from", p))
27035 result = PRAGMA_OMP_CLAUSE_FROM;
27036 break;
27037 case 'i':
27038 if (!strcmp ("inbranch", p))
27039 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27040 break;
27041 case 'l':
27042 if (!strcmp ("lastprivate", p))
27043 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27044 else if (!strcmp ("linear", p))
27045 result = PRAGMA_OMP_CLAUSE_LINEAR;
27046 break;
27047 case 'm':
27048 if (!strcmp ("map", p))
27049 result = PRAGMA_OMP_CLAUSE_MAP;
27050 else if (!strcmp ("mergeable", p))
27051 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27052 else if (flag_cilkplus && !strcmp ("mask", p))
27053 result = PRAGMA_CILK_CLAUSE_MASK;
27054 break;
27055 case 'n':
27056 if (!strcmp ("notinbranch", p))
27057 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27058 else if (!strcmp ("nowait", p))
27059 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27060 else if (flag_cilkplus && !strcmp ("nomask", p))
27061 result = PRAGMA_CILK_CLAUSE_NOMASK;
27062 else if (!strcmp ("num_teams", p))
27063 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27064 else if (!strcmp ("num_threads", p))
27065 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27066 break;
27067 case 'o':
27068 if (!strcmp ("ordered", p))
27069 result = PRAGMA_OMP_CLAUSE_ORDERED;
27070 break;
27071 case 'p':
27072 if (!strcmp ("parallel", p))
27073 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27074 else if (!strcmp ("proc_bind", p))
27075 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27076 break;
27077 case 'r':
27078 if (!strcmp ("reduction", p))
27079 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27080 break;
27081 case 's':
27082 if (!strcmp ("safelen", p))
27083 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27084 else if (!strcmp ("schedule", p))
27085 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27086 else if (!strcmp ("sections", p))
27087 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27088 else if (!strcmp ("shared", p))
27089 result = PRAGMA_OMP_CLAUSE_SHARED;
27090 else if (!strcmp ("simdlen", p))
27091 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27092 break;
27093 case 't':
27094 if (!strcmp ("taskgroup", p))
27095 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27096 else if (!strcmp ("thread_limit", p))
27097 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27098 else if (!strcmp ("to", p))
27099 result = PRAGMA_OMP_CLAUSE_TO;
27100 break;
27101 case 'u':
27102 if (!strcmp ("uniform", p))
27103 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27104 else if (!strcmp ("untied", p))
27105 result = PRAGMA_OMP_CLAUSE_UNTIED;
27106 break;
27107 case 'v':
27108 if (flag_cilkplus && !strcmp ("vectorlength", p))
27109 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27110 break;
27114 if (result != PRAGMA_OMP_CLAUSE_NONE)
27115 cp_lexer_consume_token (parser->lexer);
27117 return result;
27120 /* Validate that a clause of the given type does not already exist. */
27122 static void
27123 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27124 const char *name, location_t location)
27126 tree c;
27128 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27129 if (OMP_CLAUSE_CODE (c) == code)
27131 error_at (location, "too many %qs clauses", name);
27132 break;
27136 /* OpenMP 2.5:
27137 variable-list:
27138 identifier
27139 variable-list , identifier
27141 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27142 colon). An opening parenthesis will have been consumed by the caller.
27144 If KIND is nonzero, create the appropriate node and install the decl
27145 in OMP_CLAUSE_DECL and add the node to the head of the list.
27147 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27148 return the list created.
27150 COLON can be NULL if only closing parenthesis should end the list,
27151 or pointer to bool which will receive false if the list is terminated
27152 by closing parenthesis or true if the list is terminated by colon. */
27154 static tree
27155 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27156 tree list, bool *colon)
27158 cp_token *token;
27159 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27160 if (colon)
27162 parser->colon_corrects_to_scope_p = false;
27163 *colon = false;
27165 while (1)
27167 tree name, decl;
27169 token = cp_lexer_peek_token (parser->lexer);
27170 name = cp_parser_id_expression (parser, /*template_p=*/false,
27171 /*check_dependency_p=*/true,
27172 /*template_p=*/NULL,
27173 /*declarator_p=*/false,
27174 /*optional_p=*/false);
27175 if (name == error_mark_node)
27176 goto skip_comma;
27178 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27179 if (decl == error_mark_node)
27180 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27181 token->location);
27182 else if (kind != 0)
27184 switch (kind)
27186 case OMP_CLAUSE_MAP:
27187 case OMP_CLAUSE_FROM:
27188 case OMP_CLAUSE_TO:
27189 case OMP_CLAUSE_DEPEND:
27190 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27192 tree low_bound = NULL_TREE, length = NULL_TREE;
27194 parser->colon_corrects_to_scope_p = false;
27195 cp_lexer_consume_token (parser->lexer);
27196 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27197 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27198 NULL);
27199 if (!colon)
27200 parser->colon_corrects_to_scope_p
27201 = saved_colon_corrects_to_scope_p;
27202 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27203 length = integer_one_node;
27204 else
27206 /* Look for `:'. */
27207 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27208 goto skip_comma;
27209 if (!cp_lexer_next_token_is (parser->lexer,
27210 CPP_CLOSE_SQUARE))
27211 length = cp_parser_expression (parser,
27212 /*cast_p=*/false,
27213 NULL);
27215 /* Look for the closing `]'. */
27216 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27217 RT_CLOSE_SQUARE))
27218 goto skip_comma;
27219 decl = tree_cons (low_bound, length, decl);
27221 break;
27222 default:
27223 break;
27226 tree u = build_omp_clause (token->location, kind);
27227 OMP_CLAUSE_DECL (u) = decl;
27228 OMP_CLAUSE_CHAIN (u) = list;
27229 list = u;
27231 else
27232 list = tree_cons (decl, NULL_TREE, list);
27234 get_comma:
27235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27236 break;
27237 cp_lexer_consume_token (parser->lexer);
27240 if (colon)
27241 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27243 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27245 *colon = true;
27246 cp_parser_require (parser, CPP_COLON, RT_COLON);
27247 return list;
27250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27252 int ending;
27254 /* Try to resync to an unnested comma. Copied from
27255 cp_parser_parenthesized_expression_list. */
27256 skip_comma:
27257 if (colon)
27258 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27259 ending = cp_parser_skip_to_closing_parenthesis (parser,
27260 /*recovering=*/true,
27261 /*or_comma=*/true,
27262 /*consume_paren=*/true);
27263 if (ending < 0)
27264 goto get_comma;
27267 return list;
27270 /* Similarly, but expect leading and trailing parenthesis. This is a very
27271 common case for omp clauses. */
27273 static tree
27274 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27276 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27277 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27278 return list;
27281 /* OpenMP 3.0:
27282 collapse ( constant-expression ) */
27284 static tree
27285 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27287 tree c, num;
27288 location_t loc;
27289 HOST_WIDE_INT n;
27291 loc = cp_lexer_peek_token (parser->lexer)->location;
27292 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27293 return list;
27295 num = cp_parser_constant_expression (parser, false, NULL);
27297 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27298 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27299 /*or_comma=*/false,
27300 /*consume_paren=*/true);
27302 if (num == error_mark_node)
27303 return list;
27304 num = fold_non_dependent_expr (num);
27305 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27306 || !tree_fits_shwi_p (num)
27307 || (n = tree_to_shwi (num)) <= 0
27308 || (int) n != n)
27310 error_at (loc, "collapse argument needs positive constant integer expression");
27311 return list;
27314 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27315 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27316 OMP_CLAUSE_CHAIN (c) = list;
27317 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27319 return c;
27322 /* OpenMP 2.5:
27323 default ( shared | none ) */
27325 static tree
27326 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27328 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27329 tree c;
27331 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27332 return list;
27333 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27335 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27336 const char *p = IDENTIFIER_POINTER (id);
27338 switch (p[0])
27340 case 'n':
27341 if (strcmp ("none", p) != 0)
27342 goto invalid_kind;
27343 kind = OMP_CLAUSE_DEFAULT_NONE;
27344 break;
27346 case 's':
27347 if (strcmp ("shared", p) != 0)
27348 goto invalid_kind;
27349 kind = OMP_CLAUSE_DEFAULT_SHARED;
27350 break;
27352 default:
27353 goto invalid_kind;
27356 cp_lexer_consume_token (parser->lexer);
27358 else
27360 invalid_kind:
27361 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27364 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27365 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27366 /*or_comma=*/false,
27367 /*consume_paren=*/true);
27369 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27370 return list;
27372 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27373 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27374 OMP_CLAUSE_CHAIN (c) = list;
27375 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27377 return c;
27380 /* OpenMP 3.1:
27381 final ( expression ) */
27383 static tree
27384 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27386 tree t, c;
27388 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27389 return list;
27391 t = cp_parser_condition (parser);
27393 if (t == error_mark_node
27394 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27396 /*or_comma=*/false,
27397 /*consume_paren=*/true);
27399 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27401 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27402 OMP_CLAUSE_FINAL_EXPR (c) = t;
27403 OMP_CLAUSE_CHAIN (c) = list;
27405 return c;
27408 /* OpenMP 2.5:
27409 if ( expression ) */
27411 static tree
27412 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27414 tree t, c;
27416 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27417 return list;
27419 t = cp_parser_condition (parser);
27421 if (t == error_mark_node
27422 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27423 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27424 /*or_comma=*/false,
27425 /*consume_paren=*/true);
27427 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27429 c = build_omp_clause (location, OMP_CLAUSE_IF);
27430 OMP_CLAUSE_IF_EXPR (c) = t;
27431 OMP_CLAUSE_CHAIN (c) = list;
27433 return c;
27436 /* OpenMP 3.1:
27437 mergeable */
27439 static tree
27440 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27441 tree list, location_t location)
27443 tree c;
27445 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27446 location);
27448 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27449 OMP_CLAUSE_CHAIN (c) = list;
27450 return c;
27453 /* OpenMP 2.5:
27454 nowait */
27456 static tree
27457 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27458 tree list, location_t location)
27460 tree c;
27462 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27464 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27465 OMP_CLAUSE_CHAIN (c) = list;
27466 return c;
27469 /* OpenMP 2.5:
27470 num_threads ( expression ) */
27472 static tree
27473 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27474 location_t location)
27476 tree t, c;
27478 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27479 return list;
27481 t = cp_parser_expression (parser, false, NULL);
27483 if (t == error_mark_node
27484 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27486 /*or_comma=*/false,
27487 /*consume_paren=*/true);
27489 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27490 "num_threads", location);
27492 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27493 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27494 OMP_CLAUSE_CHAIN (c) = list;
27496 return c;
27499 /* OpenMP 2.5:
27500 ordered */
27502 static tree
27503 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27504 tree list, location_t location)
27506 tree c;
27508 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27509 "ordered", location);
27511 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27512 OMP_CLAUSE_CHAIN (c) = list;
27513 return c;
27516 /* OpenMP 2.5:
27517 reduction ( reduction-operator : variable-list )
27519 reduction-operator:
27520 One of: + * - & ^ | && ||
27522 OpenMP 3.1:
27524 reduction-operator:
27525 One of: + * - & ^ | && || min max
27527 OpenMP 4.0:
27529 reduction-operator:
27530 One of: + * - & ^ | && ||
27531 id-expression */
27533 static tree
27534 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27536 enum tree_code code = ERROR_MARK;
27537 tree nlist, c, id = NULL_TREE;
27539 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27540 return list;
27542 switch (cp_lexer_peek_token (parser->lexer)->type)
27544 case CPP_PLUS: code = PLUS_EXPR; break;
27545 case CPP_MULT: code = MULT_EXPR; break;
27546 case CPP_MINUS: code = MINUS_EXPR; break;
27547 case CPP_AND: code = BIT_AND_EXPR; break;
27548 case CPP_XOR: code = BIT_XOR_EXPR; break;
27549 case CPP_OR: code = BIT_IOR_EXPR; break;
27550 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27551 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27552 default: break;
27555 if (code != ERROR_MARK)
27556 cp_lexer_consume_token (parser->lexer);
27557 else
27559 bool saved_colon_corrects_to_scope_p;
27560 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27561 parser->colon_corrects_to_scope_p = false;
27562 id = cp_parser_id_expression (parser, /*template_p=*/false,
27563 /*check_dependency_p=*/true,
27564 /*template_p=*/NULL,
27565 /*declarator_p=*/false,
27566 /*optional_p=*/false);
27567 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27568 if (identifier_p (id))
27570 const char *p = IDENTIFIER_POINTER (id);
27572 if (strcmp (p, "min") == 0)
27573 code = MIN_EXPR;
27574 else if (strcmp (p, "max") == 0)
27575 code = MAX_EXPR;
27576 else if (id == ansi_opname (PLUS_EXPR))
27577 code = PLUS_EXPR;
27578 else if (id == ansi_opname (MULT_EXPR))
27579 code = MULT_EXPR;
27580 else if (id == ansi_opname (MINUS_EXPR))
27581 code = MINUS_EXPR;
27582 else if (id == ansi_opname (BIT_AND_EXPR))
27583 code = BIT_AND_EXPR;
27584 else if (id == ansi_opname (BIT_IOR_EXPR))
27585 code = BIT_IOR_EXPR;
27586 else if (id == ansi_opname (BIT_XOR_EXPR))
27587 code = BIT_XOR_EXPR;
27588 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27589 code = TRUTH_ANDIF_EXPR;
27590 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27591 code = TRUTH_ORIF_EXPR;
27592 id = omp_reduction_id (code, id, NULL_TREE);
27593 tree scope = parser->scope;
27594 if (scope)
27595 id = build_qualified_name (NULL_TREE, scope, id, false);
27596 parser->scope = NULL_TREE;
27597 parser->qualifying_scope = NULL_TREE;
27598 parser->object_scope = NULL_TREE;
27600 else
27602 error ("invalid reduction-identifier");
27603 resync_fail:
27604 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27605 /*or_comma=*/false,
27606 /*consume_paren=*/true);
27607 return list;
27611 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27612 goto resync_fail;
27614 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27615 NULL);
27616 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27618 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27619 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27622 return nlist;
27625 /* OpenMP 2.5:
27626 schedule ( schedule-kind )
27627 schedule ( schedule-kind , expression )
27629 schedule-kind:
27630 static | dynamic | guided | runtime | auto */
27632 static tree
27633 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27635 tree c, t;
27637 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27638 return list;
27640 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27642 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27644 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27645 const char *p = IDENTIFIER_POINTER (id);
27647 switch (p[0])
27649 case 'd':
27650 if (strcmp ("dynamic", p) != 0)
27651 goto invalid_kind;
27652 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27653 break;
27655 case 'g':
27656 if (strcmp ("guided", p) != 0)
27657 goto invalid_kind;
27658 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27659 break;
27661 case 'r':
27662 if (strcmp ("runtime", p) != 0)
27663 goto invalid_kind;
27664 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27665 break;
27667 default:
27668 goto invalid_kind;
27671 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27672 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27673 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27674 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27675 else
27676 goto invalid_kind;
27677 cp_lexer_consume_token (parser->lexer);
27679 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27681 cp_token *token;
27682 cp_lexer_consume_token (parser->lexer);
27684 token = cp_lexer_peek_token (parser->lexer);
27685 t = cp_parser_assignment_expression (parser, false, NULL);
27687 if (t == error_mark_node)
27688 goto resync_fail;
27689 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27690 error_at (token->location, "schedule %<runtime%> does not take "
27691 "a %<chunk_size%> parameter");
27692 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27693 error_at (token->location, "schedule %<auto%> does not take "
27694 "a %<chunk_size%> parameter");
27695 else
27696 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27698 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27699 goto resync_fail;
27701 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27702 goto resync_fail;
27704 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27705 OMP_CLAUSE_CHAIN (c) = list;
27706 return c;
27708 invalid_kind:
27709 cp_parser_error (parser, "invalid schedule kind");
27710 resync_fail:
27711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27712 /*or_comma=*/false,
27713 /*consume_paren=*/true);
27714 return list;
27717 /* OpenMP 3.0:
27718 untied */
27720 static tree
27721 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27722 tree list, location_t location)
27724 tree c;
27726 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27728 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27729 OMP_CLAUSE_CHAIN (c) = list;
27730 return c;
27733 /* OpenMP 4.0:
27734 inbranch
27735 notinbranch */
27737 static tree
27738 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27739 tree list, location_t location)
27741 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27742 tree c = build_omp_clause (location, code);
27743 OMP_CLAUSE_CHAIN (c) = list;
27744 return c;
27747 /* OpenMP 4.0:
27748 parallel
27750 sections
27751 taskgroup */
27753 static tree
27754 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27755 enum omp_clause_code code,
27756 tree list, location_t location)
27758 tree c = build_omp_clause (location, code);
27759 OMP_CLAUSE_CHAIN (c) = list;
27760 return c;
27763 /* OpenMP 4.0:
27764 num_teams ( expression ) */
27766 static tree
27767 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27768 location_t location)
27770 tree t, c;
27772 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27773 return list;
27775 t = cp_parser_expression (parser, false, NULL);
27777 if (t == error_mark_node
27778 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27779 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27780 /*or_comma=*/false,
27781 /*consume_paren=*/true);
27783 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27784 "num_teams", location);
27786 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27787 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27788 OMP_CLAUSE_CHAIN (c) = list;
27790 return c;
27793 /* OpenMP 4.0:
27794 thread_limit ( expression ) */
27796 static tree
27797 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27798 location_t location)
27800 tree t, c;
27802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27803 return list;
27805 t = cp_parser_expression (parser, false, NULL);
27807 if (t == error_mark_node
27808 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27809 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27810 /*or_comma=*/false,
27811 /*consume_paren=*/true);
27813 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27814 "thread_limit", location);
27816 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27817 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27818 OMP_CLAUSE_CHAIN (c) = list;
27820 return c;
27823 /* OpenMP 4.0:
27824 aligned ( variable-list )
27825 aligned ( variable-list : constant-expression ) */
27827 static tree
27828 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27830 tree nlist, c, alignment = NULL_TREE;
27831 bool colon;
27833 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27834 return list;
27836 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27837 &colon);
27839 if (colon)
27841 alignment = cp_parser_constant_expression (parser, false, NULL);
27843 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27845 /*or_comma=*/false,
27846 /*consume_paren=*/true);
27848 if (alignment == error_mark_node)
27849 alignment = NULL_TREE;
27852 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27853 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27855 return nlist;
27858 /* OpenMP 4.0:
27859 linear ( variable-list )
27860 linear ( variable-list : expression ) */
27862 static tree
27863 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
27864 bool is_cilk_simd_fn)
27866 tree nlist, c, step = integer_one_node;
27867 bool colon;
27869 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27870 return list;
27872 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27873 &colon);
27875 if (colon)
27877 step = cp_parser_expression (parser, false, NULL);
27879 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
27881 sorry ("using parameters for %<linear%> step is not supported yet");
27882 step = integer_one_node;
27884 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27885 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27886 /*or_comma=*/false,
27887 /*consume_paren=*/true);
27889 if (step == error_mark_node)
27890 return list;
27893 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27894 OMP_CLAUSE_LINEAR_STEP (c) = step;
27896 return nlist;
27899 /* OpenMP 4.0:
27900 safelen ( constant-expression ) */
27902 static tree
27903 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27904 location_t location)
27906 tree t, c;
27908 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27909 return list;
27911 t = cp_parser_constant_expression (parser, false, NULL);
27913 if (t == error_mark_node
27914 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27915 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27916 /*or_comma=*/false,
27917 /*consume_paren=*/true);
27919 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27921 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27922 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27923 OMP_CLAUSE_CHAIN (c) = list;
27925 return c;
27928 /* OpenMP 4.0:
27929 simdlen ( constant-expression ) */
27931 static tree
27932 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27933 location_t location)
27935 tree t, c;
27937 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27938 return list;
27940 t = cp_parser_constant_expression (parser, false, NULL);
27942 if (t == error_mark_node
27943 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27944 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27945 /*or_comma=*/false,
27946 /*consume_paren=*/true);
27948 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27950 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27951 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27952 OMP_CLAUSE_CHAIN (c) = list;
27954 return c;
27957 /* OpenMP 4.0:
27958 depend ( depend-kind : variable-list )
27960 depend-kind:
27961 in | out | inout */
27963 static tree
27964 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27966 tree nlist, c;
27967 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27969 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27970 return list;
27972 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27974 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27975 const char *p = IDENTIFIER_POINTER (id);
27977 if (strcmp ("in", p) == 0)
27978 kind = OMP_CLAUSE_DEPEND_IN;
27979 else if (strcmp ("inout", p) == 0)
27980 kind = OMP_CLAUSE_DEPEND_INOUT;
27981 else if (strcmp ("out", p) == 0)
27982 kind = OMP_CLAUSE_DEPEND_OUT;
27983 else
27984 goto invalid_kind;
27986 else
27987 goto invalid_kind;
27989 cp_lexer_consume_token (parser->lexer);
27990 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27991 goto resync_fail;
27993 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27994 NULL);
27996 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27997 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27999 return nlist;
28001 invalid_kind:
28002 cp_parser_error (parser, "invalid depend kind");
28003 resync_fail:
28004 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28005 /*or_comma=*/false,
28006 /*consume_paren=*/true);
28007 return list;
28010 /* OpenMP 4.0:
28011 map ( map-kind : variable-list )
28012 map ( variable-list )
28014 map-kind:
28015 alloc | to | from | tofrom */
28017 static tree
28018 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28020 tree nlist, c;
28021 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28023 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28024 return list;
28026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28027 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28029 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28030 const char *p = IDENTIFIER_POINTER (id);
28032 if (strcmp ("alloc", p) == 0)
28033 kind = OMP_CLAUSE_MAP_ALLOC;
28034 else if (strcmp ("to", p) == 0)
28035 kind = OMP_CLAUSE_MAP_TO;
28036 else if (strcmp ("from", p) == 0)
28037 kind = OMP_CLAUSE_MAP_FROM;
28038 else if (strcmp ("tofrom", p) == 0)
28039 kind = OMP_CLAUSE_MAP_TOFROM;
28040 else
28042 cp_parser_error (parser, "invalid map kind");
28043 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28044 /*or_comma=*/false,
28045 /*consume_paren=*/true);
28046 return list;
28048 cp_lexer_consume_token (parser->lexer);
28049 cp_lexer_consume_token (parser->lexer);
28052 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28053 NULL);
28055 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28056 OMP_CLAUSE_MAP_KIND (c) = kind;
28058 return nlist;
28061 /* OpenMP 4.0:
28062 device ( expression ) */
28064 static tree
28065 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28066 location_t location)
28068 tree t, c;
28070 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28071 return list;
28073 t = cp_parser_expression (parser, false, NULL);
28075 if (t == error_mark_node
28076 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28077 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28078 /*or_comma=*/false,
28079 /*consume_paren=*/true);
28081 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28082 "device", location);
28084 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28085 OMP_CLAUSE_DEVICE_ID (c) = t;
28086 OMP_CLAUSE_CHAIN (c) = list;
28088 return c;
28091 /* OpenMP 4.0:
28092 dist_schedule ( static )
28093 dist_schedule ( static , expression ) */
28095 static tree
28096 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28097 location_t location)
28099 tree c, t;
28101 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28102 return list;
28104 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28106 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28107 goto invalid_kind;
28108 cp_lexer_consume_token (parser->lexer);
28110 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28112 cp_lexer_consume_token (parser->lexer);
28114 t = cp_parser_assignment_expression (parser, false, NULL);
28116 if (t == error_mark_node)
28117 goto resync_fail;
28118 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28120 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28121 goto resync_fail;
28123 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28124 goto resync_fail;
28126 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28127 location);
28128 OMP_CLAUSE_CHAIN (c) = list;
28129 return c;
28131 invalid_kind:
28132 cp_parser_error (parser, "invalid dist_schedule kind");
28133 resync_fail:
28134 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28135 /*or_comma=*/false,
28136 /*consume_paren=*/true);
28137 return list;
28140 /* OpenMP 4.0:
28141 proc_bind ( proc-bind-kind )
28143 proc-bind-kind:
28144 master | close | spread */
28146 static tree
28147 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28148 location_t location)
28150 tree c;
28151 enum omp_clause_proc_bind_kind kind;
28153 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28154 return list;
28156 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28158 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28159 const char *p = IDENTIFIER_POINTER (id);
28161 if (strcmp ("master", p) == 0)
28162 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28163 else if (strcmp ("close", p) == 0)
28164 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28165 else if (strcmp ("spread", p) == 0)
28166 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28167 else
28168 goto invalid_kind;
28170 else
28171 goto invalid_kind;
28173 cp_lexer_consume_token (parser->lexer);
28174 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28175 goto resync_fail;
28177 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28178 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28179 location);
28180 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28181 OMP_CLAUSE_CHAIN (c) = list;
28182 return c;
28184 invalid_kind:
28185 cp_parser_error (parser, "invalid depend kind");
28186 resync_fail:
28187 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28188 /*or_comma=*/false,
28189 /*consume_paren=*/true);
28190 return list;
28193 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28194 is a bitmask in MASK. Return the list of clauses found; the result
28195 of clause default goes in *pdefault. */
28197 static tree
28198 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28199 const char *where, cp_token *pragma_tok,
28200 bool finish_p = true)
28202 tree clauses = NULL;
28203 bool first = true;
28204 cp_token *token = NULL;
28205 bool cilk_simd_fn = false;
28207 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28209 pragma_omp_clause c_kind;
28210 const char *c_name;
28211 tree prev = clauses;
28213 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28214 cp_lexer_consume_token (parser->lexer);
28216 token = cp_lexer_peek_token (parser->lexer);
28217 c_kind = cp_parser_omp_clause_name (parser);
28219 switch (c_kind)
28221 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28222 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28223 token->location);
28224 c_name = "collapse";
28225 break;
28226 case PRAGMA_OMP_CLAUSE_COPYIN:
28227 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28228 c_name = "copyin";
28229 break;
28230 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28231 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28232 clauses);
28233 c_name = "copyprivate";
28234 break;
28235 case PRAGMA_OMP_CLAUSE_DEFAULT:
28236 clauses = cp_parser_omp_clause_default (parser, clauses,
28237 token->location);
28238 c_name = "default";
28239 break;
28240 case PRAGMA_OMP_CLAUSE_FINAL:
28241 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28242 c_name = "final";
28243 break;
28244 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28245 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28246 clauses);
28247 c_name = "firstprivate";
28248 break;
28249 case PRAGMA_OMP_CLAUSE_IF:
28250 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28251 c_name = "if";
28252 break;
28253 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28254 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28255 clauses);
28256 c_name = "lastprivate";
28257 break;
28258 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28259 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28260 token->location);
28261 c_name = "mergeable";
28262 break;
28263 case PRAGMA_OMP_CLAUSE_NOWAIT:
28264 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28265 c_name = "nowait";
28266 break;
28267 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28268 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28269 token->location);
28270 c_name = "num_threads";
28271 break;
28272 case PRAGMA_OMP_CLAUSE_ORDERED:
28273 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28274 token->location);
28275 c_name = "ordered";
28276 break;
28277 case PRAGMA_OMP_CLAUSE_PRIVATE:
28278 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28279 clauses);
28280 c_name = "private";
28281 break;
28282 case PRAGMA_OMP_CLAUSE_REDUCTION:
28283 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28284 c_name = "reduction";
28285 break;
28286 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28287 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28288 token->location);
28289 c_name = "schedule";
28290 break;
28291 case PRAGMA_OMP_CLAUSE_SHARED:
28292 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28293 clauses);
28294 c_name = "shared";
28295 break;
28296 case PRAGMA_OMP_CLAUSE_UNTIED:
28297 clauses = cp_parser_omp_clause_untied (parser, clauses,
28298 token->location);
28299 c_name = "untied";
28300 break;
28301 case PRAGMA_OMP_CLAUSE_INBRANCH:
28302 case PRAGMA_CILK_CLAUSE_MASK:
28303 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28304 clauses, token->location);
28305 c_name = "inbranch";
28306 break;
28307 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28308 case PRAGMA_CILK_CLAUSE_NOMASK:
28309 clauses = cp_parser_omp_clause_branch (parser,
28310 OMP_CLAUSE_NOTINBRANCH,
28311 clauses, token->location);
28312 c_name = "notinbranch";
28313 break;
28314 case PRAGMA_OMP_CLAUSE_PARALLEL:
28315 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28316 clauses, token->location);
28317 c_name = "parallel";
28318 if (!first)
28320 clause_not_first:
28321 error_at (token->location, "%qs must be the first clause of %qs",
28322 c_name, where);
28323 clauses = prev;
28325 break;
28326 case PRAGMA_OMP_CLAUSE_FOR:
28327 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28328 clauses, token->location);
28329 c_name = "for";
28330 if (!first)
28331 goto clause_not_first;
28332 break;
28333 case PRAGMA_OMP_CLAUSE_SECTIONS:
28334 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28335 clauses, token->location);
28336 c_name = "sections";
28337 if (!first)
28338 goto clause_not_first;
28339 break;
28340 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28341 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28342 clauses, token->location);
28343 c_name = "taskgroup";
28344 if (!first)
28345 goto clause_not_first;
28346 break;
28347 case PRAGMA_OMP_CLAUSE_TO:
28348 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28349 clauses);
28350 c_name = "to";
28351 break;
28352 case PRAGMA_OMP_CLAUSE_FROM:
28353 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28354 clauses);
28355 c_name = "from";
28356 break;
28357 case PRAGMA_OMP_CLAUSE_UNIFORM:
28358 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28359 clauses);
28360 c_name = "uniform";
28361 break;
28362 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28363 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28364 token->location);
28365 c_name = "num_teams";
28366 break;
28367 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28368 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28369 token->location);
28370 c_name = "thread_limit";
28371 break;
28372 case PRAGMA_OMP_CLAUSE_ALIGNED:
28373 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28374 c_name = "aligned";
28375 break;
28376 case PRAGMA_OMP_CLAUSE_LINEAR:
28377 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28378 cilk_simd_fn = true;
28379 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28380 c_name = "linear";
28381 break;
28382 case PRAGMA_OMP_CLAUSE_DEPEND:
28383 clauses = cp_parser_omp_clause_depend (parser, clauses);
28384 c_name = "depend";
28385 break;
28386 case PRAGMA_OMP_CLAUSE_MAP:
28387 clauses = cp_parser_omp_clause_map (parser, clauses);
28388 c_name = "map";
28389 break;
28390 case PRAGMA_OMP_CLAUSE_DEVICE:
28391 clauses = cp_parser_omp_clause_device (parser, clauses,
28392 token->location);
28393 c_name = "device";
28394 break;
28395 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28396 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28397 token->location);
28398 c_name = "dist_schedule";
28399 break;
28400 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28401 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28402 token->location);
28403 c_name = "proc_bind";
28404 break;
28405 case PRAGMA_OMP_CLAUSE_SAFELEN:
28406 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28407 token->location);
28408 c_name = "safelen";
28409 break;
28410 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28411 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28412 token->location);
28413 c_name = "simdlen";
28414 break;
28415 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28416 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28417 c_name = "simdlen";
28418 break;
28419 default:
28420 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28421 goto saw_error;
28424 first = false;
28426 if (((mask >> c_kind) & 1) == 0)
28428 /* Remove the invalid clause(s) from the list to avoid
28429 confusing the rest of the compiler. */
28430 clauses = prev;
28431 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28434 saw_error:
28435 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28436 no reason to skip to the end. */
28437 if (!(flag_cilkplus && pragma_tok == NULL))
28438 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28439 if (finish_p)
28440 return finish_omp_clauses (clauses);
28441 return clauses;
28444 /* OpenMP 2.5:
28445 structured-block:
28446 statement
28448 In practice, we're also interested in adding the statement to an
28449 outer node. So it is convenient if we work around the fact that
28450 cp_parser_statement calls add_stmt. */
28452 static unsigned
28453 cp_parser_begin_omp_structured_block (cp_parser *parser)
28455 unsigned save = parser->in_statement;
28457 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28458 This preserves the "not within loop or switch" style error messages
28459 for nonsense cases like
28460 void foo() {
28461 #pragma omp single
28462 break;
28465 if (parser->in_statement)
28466 parser->in_statement = IN_OMP_BLOCK;
28468 return save;
28471 static void
28472 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28474 parser->in_statement = save;
28477 static tree
28478 cp_parser_omp_structured_block (cp_parser *parser)
28480 tree stmt = begin_omp_structured_block ();
28481 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28483 cp_parser_statement (parser, NULL_TREE, false, NULL);
28485 cp_parser_end_omp_structured_block (parser, save);
28486 return finish_omp_structured_block (stmt);
28489 /* OpenMP 2.5:
28490 # pragma omp atomic new-line
28491 expression-stmt
28493 expression-stmt:
28494 x binop= expr | x++ | ++x | x-- | --x
28495 binop:
28496 +, *, -, /, &, ^, |, <<, >>
28498 where x is an lvalue expression with scalar type.
28500 OpenMP 3.1:
28501 # pragma omp atomic new-line
28502 update-stmt
28504 # pragma omp atomic read new-line
28505 read-stmt
28507 # pragma omp atomic write new-line
28508 write-stmt
28510 # pragma omp atomic update new-line
28511 update-stmt
28513 # pragma omp atomic capture new-line
28514 capture-stmt
28516 # pragma omp atomic capture new-line
28517 capture-block
28519 read-stmt:
28520 v = x
28521 write-stmt:
28522 x = expr
28523 update-stmt:
28524 expression-stmt | x = x binop expr
28525 capture-stmt:
28526 v = expression-stmt
28527 capture-block:
28528 { v = x; update-stmt; } | { update-stmt; v = x; }
28530 OpenMP 4.0:
28531 update-stmt:
28532 expression-stmt | x = x binop expr | x = expr binop x
28533 capture-stmt:
28534 v = update-stmt
28535 capture-block:
28536 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28538 where x and v are lvalue expressions with scalar type. */
28540 static void
28541 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28543 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28544 tree rhs1 = NULL_TREE, orig_lhs;
28545 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28546 bool structured_block = false;
28547 bool seq_cst = false;
28549 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28551 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28552 const char *p = IDENTIFIER_POINTER (id);
28554 if (!strcmp (p, "seq_cst"))
28556 seq_cst = true;
28557 cp_lexer_consume_token (parser->lexer);
28558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28559 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28560 cp_lexer_consume_token (parser->lexer);
28563 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28565 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28566 const char *p = IDENTIFIER_POINTER (id);
28568 if (!strcmp (p, "read"))
28569 code = OMP_ATOMIC_READ;
28570 else if (!strcmp (p, "write"))
28571 code = NOP_EXPR;
28572 else if (!strcmp (p, "update"))
28573 code = OMP_ATOMIC;
28574 else if (!strcmp (p, "capture"))
28575 code = OMP_ATOMIC_CAPTURE_NEW;
28576 else
28577 p = NULL;
28578 if (p)
28579 cp_lexer_consume_token (parser->lexer);
28581 if (!seq_cst)
28583 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28584 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28585 cp_lexer_consume_token (parser->lexer);
28587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28589 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28590 const char *p = IDENTIFIER_POINTER (id);
28592 if (!strcmp (p, "seq_cst"))
28594 seq_cst = true;
28595 cp_lexer_consume_token (parser->lexer);
28599 cp_parser_require_pragma_eol (parser, pragma_tok);
28601 switch (code)
28603 case OMP_ATOMIC_READ:
28604 case NOP_EXPR: /* atomic write */
28605 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28606 /*cast_p=*/false, NULL);
28607 if (v == error_mark_node)
28608 goto saw_error;
28609 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28610 goto saw_error;
28611 if (code == NOP_EXPR)
28612 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28613 else
28614 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28615 /*cast_p=*/false, NULL);
28616 if (lhs == error_mark_node)
28617 goto saw_error;
28618 if (code == NOP_EXPR)
28620 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28621 opcode. */
28622 code = OMP_ATOMIC;
28623 rhs = lhs;
28624 lhs = v;
28625 v = NULL_TREE;
28627 goto done;
28628 case OMP_ATOMIC_CAPTURE_NEW:
28629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28631 cp_lexer_consume_token (parser->lexer);
28632 structured_block = true;
28634 else
28636 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28637 /*cast_p=*/false, NULL);
28638 if (v == error_mark_node)
28639 goto saw_error;
28640 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28641 goto saw_error;
28643 default:
28644 break;
28647 restart:
28648 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28649 /*cast_p=*/false, NULL);
28650 orig_lhs = lhs;
28651 switch (TREE_CODE (lhs))
28653 case ERROR_MARK:
28654 goto saw_error;
28656 case POSTINCREMENT_EXPR:
28657 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28658 code = OMP_ATOMIC_CAPTURE_OLD;
28659 /* FALLTHROUGH */
28660 case PREINCREMENT_EXPR:
28661 lhs = TREE_OPERAND (lhs, 0);
28662 opcode = PLUS_EXPR;
28663 rhs = integer_one_node;
28664 break;
28666 case POSTDECREMENT_EXPR:
28667 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28668 code = OMP_ATOMIC_CAPTURE_OLD;
28669 /* FALLTHROUGH */
28670 case PREDECREMENT_EXPR:
28671 lhs = TREE_OPERAND (lhs, 0);
28672 opcode = MINUS_EXPR;
28673 rhs = integer_one_node;
28674 break;
28676 case COMPOUND_EXPR:
28677 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28678 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28679 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28680 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28681 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28682 (TREE_OPERAND (lhs, 1), 0), 0)))
28683 == BOOLEAN_TYPE)
28684 /* Undo effects of boolean_increment for post {in,de}crement. */
28685 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28686 /* FALLTHRU */
28687 case MODIFY_EXPR:
28688 if (TREE_CODE (lhs) == MODIFY_EXPR
28689 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28691 /* Undo effects of boolean_increment. */
28692 if (integer_onep (TREE_OPERAND (lhs, 1)))
28694 /* This is pre or post increment. */
28695 rhs = TREE_OPERAND (lhs, 1);
28696 lhs = TREE_OPERAND (lhs, 0);
28697 opcode = NOP_EXPR;
28698 if (code == OMP_ATOMIC_CAPTURE_NEW
28699 && !structured_block
28700 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28701 code = OMP_ATOMIC_CAPTURE_OLD;
28702 break;
28705 /* FALLTHRU */
28706 default:
28707 switch (cp_lexer_peek_token (parser->lexer)->type)
28709 case CPP_MULT_EQ:
28710 opcode = MULT_EXPR;
28711 break;
28712 case CPP_DIV_EQ:
28713 opcode = TRUNC_DIV_EXPR;
28714 break;
28715 case CPP_PLUS_EQ:
28716 opcode = PLUS_EXPR;
28717 break;
28718 case CPP_MINUS_EQ:
28719 opcode = MINUS_EXPR;
28720 break;
28721 case CPP_LSHIFT_EQ:
28722 opcode = LSHIFT_EXPR;
28723 break;
28724 case CPP_RSHIFT_EQ:
28725 opcode = RSHIFT_EXPR;
28726 break;
28727 case CPP_AND_EQ:
28728 opcode = BIT_AND_EXPR;
28729 break;
28730 case CPP_OR_EQ:
28731 opcode = BIT_IOR_EXPR;
28732 break;
28733 case CPP_XOR_EQ:
28734 opcode = BIT_XOR_EXPR;
28735 break;
28736 case CPP_EQ:
28737 enum cp_parser_prec oprec;
28738 cp_token *token;
28739 cp_lexer_consume_token (parser->lexer);
28740 cp_parser_parse_tentatively (parser);
28741 rhs1 = cp_parser_simple_cast_expression (parser);
28742 if (rhs1 == error_mark_node)
28744 cp_parser_abort_tentative_parse (parser);
28745 cp_parser_simple_cast_expression (parser);
28746 goto saw_error;
28748 token = cp_lexer_peek_token (parser->lexer);
28749 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28751 cp_parser_abort_tentative_parse (parser);
28752 cp_parser_parse_tentatively (parser);
28753 rhs = cp_parser_binary_expression (parser, false, true,
28754 PREC_NOT_OPERATOR, NULL);
28755 if (rhs == error_mark_node)
28757 cp_parser_abort_tentative_parse (parser);
28758 cp_parser_binary_expression (parser, false, true,
28759 PREC_NOT_OPERATOR, NULL);
28760 goto saw_error;
28762 switch (TREE_CODE (rhs))
28764 case MULT_EXPR:
28765 case TRUNC_DIV_EXPR:
28766 case PLUS_EXPR:
28767 case MINUS_EXPR:
28768 case LSHIFT_EXPR:
28769 case RSHIFT_EXPR:
28770 case BIT_AND_EXPR:
28771 case BIT_IOR_EXPR:
28772 case BIT_XOR_EXPR:
28773 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28775 if (cp_parser_parse_definitely (parser))
28777 opcode = TREE_CODE (rhs);
28778 rhs1 = TREE_OPERAND (rhs, 0);
28779 rhs = TREE_OPERAND (rhs, 1);
28780 goto stmt_done;
28782 else
28783 goto saw_error;
28785 break;
28786 default:
28787 break;
28789 cp_parser_abort_tentative_parse (parser);
28790 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28792 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28793 if (rhs == error_mark_node)
28794 goto saw_error;
28795 opcode = NOP_EXPR;
28796 rhs1 = NULL_TREE;
28797 goto stmt_done;
28799 cp_parser_error (parser,
28800 "invalid form of %<#pragma omp atomic%>");
28801 goto saw_error;
28803 if (!cp_parser_parse_definitely (parser))
28804 goto saw_error;
28805 switch (token->type)
28807 case CPP_SEMICOLON:
28808 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28810 code = OMP_ATOMIC_CAPTURE_OLD;
28811 v = lhs;
28812 lhs = NULL_TREE;
28813 lhs1 = rhs1;
28814 rhs1 = NULL_TREE;
28815 cp_lexer_consume_token (parser->lexer);
28816 goto restart;
28818 else if (structured_block)
28820 opcode = NOP_EXPR;
28821 rhs = rhs1;
28822 rhs1 = NULL_TREE;
28823 goto stmt_done;
28825 cp_parser_error (parser,
28826 "invalid form of %<#pragma omp atomic%>");
28827 goto saw_error;
28828 case CPP_MULT:
28829 opcode = MULT_EXPR;
28830 break;
28831 case CPP_DIV:
28832 opcode = TRUNC_DIV_EXPR;
28833 break;
28834 case CPP_PLUS:
28835 opcode = PLUS_EXPR;
28836 break;
28837 case CPP_MINUS:
28838 opcode = MINUS_EXPR;
28839 break;
28840 case CPP_LSHIFT:
28841 opcode = LSHIFT_EXPR;
28842 break;
28843 case CPP_RSHIFT:
28844 opcode = RSHIFT_EXPR;
28845 break;
28846 case CPP_AND:
28847 opcode = BIT_AND_EXPR;
28848 break;
28849 case CPP_OR:
28850 opcode = BIT_IOR_EXPR;
28851 break;
28852 case CPP_XOR:
28853 opcode = BIT_XOR_EXPR;
28854 break;
28855 default:
28856 cp_parser_error (parser,
28857 "invalid operator for %<#pragma omp atomic%>");
28858 goto saw_error;
28860 oprec = TOKEN_PRECEDENCE (token);
28861 gcc_assert (oprec != PREC_NOT_OPERATOR);
28862 if (commutative_tree_code (opcode))
28863 oprec = (enum cp_parser_prec) (oprec - 1);
28864 cp_lexer_consume_token (parser->lexer);
28865 rhs = cp_parser_binary_expression (parser, false, false,
28866 oprec, NULL);
28867 if (rhs == error_mark_node)
28868 goto saw_error;
28869 goto stmt_done;
28870 /* FALLTHROUGH */
28871 default:
28872 cp_parser_error (parser,
28873 "invalid operator for %<#pragma omp atomic%>");
28874 goto saw_error;
28876 cp_lexer_consume_token (parser->lexer);
28878 rhs = cp_parser_expression (parser, false, NULL);
28879 if (rhs == error_mark_node)
28880 goto saw_error;
28881 break;
28883 stmt_done:
28884 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28886 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28887 goto saw_error;
28888 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28889 /*cast_p=*/false, NULL);
28890 if (v == error_mark_node)
28891 goto saw_error;
28892 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28893 goto saw_error;
28894 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28895 /*cast_p=*/false, NULL);
28896 if (lhs1 == error_mark_node)
28897 goto saw_error;
28899 if (structured_block)
28901 cp_parser_consume_semicolon_at_end_of_statement (parser);
28902 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28904 done:
28905 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28906 if (!structured_block)
28907 cp_parser_consume_semicolon_at_end_of_statement (parser);
28908 return;
28910 saw_error:
28911 cp_parser_skip_to_end_of_block_or_statement (parser);
28912 if (structured_block)
28914 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28915 cp_lexer_consume_token (parser->lexer);
28916 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28918 cp_parser_skip_to_end_of_block_or_statement (parser);
28919 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28920 cp_lexer_consume_token (parser->lexer);
28926 /* OpenMP 2.5:
28927 # pragma omp barrier new-line */
28929 static void
28930 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28932 cp_parser_require_pragma_eol (parser, pragma_tok);
28933 finish_omp_barrier ();
28936 /* OpenMP 2.5:
28937 # pragma omp critical [(name)] new-line
28938 structured-block */
28940 static tree
28941 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28943 tree stmt, name = NULL;
28945 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28947 cp_lexer_consume_token (parser->lexer);
28949 name = cp_parser_identifier (parser);
28951 if (name == error_mark_node
28952 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28953 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28954 /*or_comma=*/false,
28955 /*consume_paren=*/true);
28956 if (name == error_mark_node)
28957 name = NULL;
28959 cp_parser_require_pragma_eol (parser, pragma_tok);
28961 stmt = cp_parser_omp_structured_block (parser);
28962 return c_finish_omp_critical (input_location, stmt, name);
28965 /* OpenMP 2.5:
28966 # pragma omp flush flush-vars[opt] new-line
28968 flush-vars:
28969 ( variable-list ) */
28971 static void
28972 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28975 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28976 cp_parser_require_pragma_eol (parser, pragma_tok);
28978 finish_omp_flush ();
28981 /* Helper function, to parse omp for increment expression. */
28983 static tree
28984 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
28986 tree cond = cp_parser_binary_expression (parser, false, true,
28987 PREC_NOT_OPERATOR, NULL);
28988 if (cond == error_mark_node
28989 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28991 cp_parser_skip_to_end_of_statement (parser);
28992 return error_mark_node;
28995 switch (TREE_CODE (cond))
28997 case GT_EXPR:
28998 case GE_EXPR:
28999 case LT_EXPR:
29000 case LE_EXPR:
29001 break;
29002 case NE_EXPR:
29003 if (code == CILK_SIMD)
29004 break;
29005 /* Fall through: OpenMP disallows NE_EXPR. */
29006 default:
29007 return error_mark_node;
29010 /* If decl is an iterator, preserve LHS and RHS of the relational
29011 expr until finish_omp_for. */
29012 if (decl
29013 && (type_dependent_expression_p (decl)
29014 || CLASS_TYPE_P (TREE_TYPE (decl))))
29015 return cond;
29017 return build_x_binary_op (input_location, TREE_CODE (cond),
29018 TREE_OPERAND (cond, 0), ERROR_MARK,
29019 TREE_OPERAND (cond, 1), ERROR_MARK,
29020 /*overload=*/NULL, tf_warning_or_error);
29023 /* Helper function, to parse omp for increment expression. */
29025 static tree
29026 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29028 cp_token *token = cp_lexer_peek_token (parser->lexer);
29029 enum tree_code op;
29030 tree lhs, rhs;
29031 cp_id_kind idk;
29032 bool decl_first;
29034 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29036 op = (token->type == CPP_PLUS_PLUS
29037 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29038 cp_lexer_consume_token (parser->lexer);
29039 lhs = cp_parser_simple_cast_expression (parser);
29040 if (lhs != decl)
29041 return error_mark_node;
29042 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29045 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29046 if (lhs != decl)
29047 return error_mark_node;
29049 token = cp_lexer_peek_token (parser->lexer);
29050 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29052 op = (token->type == CPP_PLUS_PLUS
29053 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29054 cp_lexer_consume_token (parser->lexer);
29055 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29058 op = cp_parser_assignment_operator_opt (parser);
29059 if (op == ERROR_MARK)
29060 return error_mark_node;
29062 if (op != NOP_EXPR)
29064 rhs = cp_parser_assignment_expression (parser, false, NULL);
29065 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29066 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29069 lhs = cp_parser_binary_expression (parser, false, false,
29070 PREC_ADDITIVE_EXPRESSION, NULL);
29071 token = cp_lexer_peek_token (parser->lexer);
29072 decl_first = lhs == decl;
29073 if (decl_first)
29074 lhs = NULL_TREE;
29075 if (token->type != CPP_PLUS
29076 && token->type != CPP_MINUS)
29077 return error_mark_node;
29081 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29082 cp_lexer_consume_token (parser->lexer);
29083 rhs = cp_parser_binary_expression (parser, false, false,
29084 PREC_ADDITIVE_EXPRESSION, NULL);
29085 token = cp_lexer_peek_token (parser->lexer);
29086 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29088 if (lhs == NULL_TREE)
29090 if (op == PLUS_EXPR)
29091 lhs = rhs;
29092 else
29093 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29094 tf_warning_or_error);
29096 else
29097 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29098 ERROR_MARK, NULL, tf_warning_or_error);
29101 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29103 if (!decl_first)
29105 if (rhs != decl || op == MINUS_EXPR)
29106 return error_mark_node;
29107 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29109 else
29110 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29112 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29115 /* Parse the initialization statement of either an OpenMP for loop or
29116 a Cilk Plus for loop.
29118 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29119 Plus.
29121 Return true if the resulting construct should have an
29122 OMP_CLAUSE_PRIVATE added to it. */
29124 static bool
29125 cp_parser_omp_for_loop_init (cp_parser *parser,
29126 bool parsing_openmp,
29127 tree &this_pre_body,
29128 vec<tree, va_gc> *for_block,
29129 tree &init,
29130 tree &decl,
29131 tree &real_decl)
29133 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29134 return false;
29136 bool add_private_clause = false;
29138 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29140 init-expr:
29141 var = lb
29142 integer-type var = lb
29143 random-access-iterator-type var = lb
29144 pointer-type var = lb
29146 cp_decl_specifier_seq type_specifiers;
29148 /* First, try to parse as an initialized declaration. See
29149 cp_parser_condition, from whence the bulk of this is copied. */
29151 cp_parser_parse_tentatively (parser);
29152 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29153 /*is_trailing_return=*/false,
29154 &type_specifiers);
29155 if (cp_parser_parse_definitely (parser))
29157 /* If parsing a type specifier seq succeeded, then this
29158 MUST be a initialized declaration. */
29159 tree asm_specification, attributes;
29160 cp_declarator *declarator;
29162 declarator = cp_parser_declarator (parser,
29163 CP_PARSER_DECLARATOR_NAMED,
29164 /*ctor_dtor_or_conv_p=*/NULL,
29165 /*parenthesized_p=*/NULL,
29166 /*member_p=*/false);
29167 attributes = cp_parser_attributes_opt (parser);
29168 asm_specification = cp_parser_asm_specification_opt (parser);
29170 if (declarator == cp_error_declarator)
29171 cp_parser_skip_to_end_of_statement (parser);
29173 else
29175 tree pushed_scope, auto_node;
29177 decl = start_decl (declarator, &type_specifiers,
29178 SD_INITIALIZED, attributes,
29179 /*prefix_attributes=*/NULL_TREE,
29180 &pushed_scope);
29182 auto_node = type_uses_auto (TREE_TYPE (decl));
29183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29185 if (cp_lexer_next_token_is (parser->lexer,
29186 CPP_OPEN_PAREN))
29188 if (parsing_openmp)
29189 error ("parenthesized initialization is not allowed in "
29190 "OpenMP %<for%> loop");
29191 else
29192 error ("parenthesized initialization is "
29193 "not allowed in for-loop");
29195 else
29196 /* Trigger an error. */
29197 cp_parser_require (parser, CPP_EQ, RT_EQ);
29199 init = error_mark_node;
29200 cp_parser_skip_to_end_of_statement (parser);
29202 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29203 || type_dependent_expression_p (decl)
29204 || auto_node)
29206 bool is_direct_init, is_non_constant_init;
29208 init = cp_parser_initializer (parser,
29209 &is_direct_init,
29210 &is_non_constant_init);
29212 if (auto_node)
29214 TREE_TYPE (decl)
29215 = do_auto_deduction (TREE_TYPE (decl), init,
29216 auto_node);
29218 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29219 && !type_dependent_expression_p (decl))
29220 goto non_class;
29223 cp_finish_decl (decl, init, !is_non_constant_init,
29224 asm_specification,
29225 LOOKUP_ONLYCONVERTING);
29226 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29228 vec_safe_push (for_block, this_pre_body);
29229 init = NULL_TREE;
29231 else
29232 init = pop_stmt_list (this_pre_body);
29233 this_pre_body = NULL_TREE;
29235 else
29237 /* Consume '='. */
29238 cp_lexer_consume_token (parser->lexer);
29239 init = cp_parser_assignment_expression (parser, false, NULL);
29241 non_class:
29242 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29243 init = error_mark_node;
29244 else
29245 cp_finish_decl (decl, NULL_TREE,
29246 /*init_const_expr_p=*/false,
29247 asm_specification,
29248 LOOKUP_ONLYCONVERTING);
29251 if (pushed_scope)
29252 pop_scope (pushed_scope);
29255 else
29257 cp_id_kind idk;
29258 /* If parsing a type specifier sequence failed, then
29259 this MUST be a simple expression. */
29260 cp_parser_parse_tentatively (parser);
29261 decl = cp_parser_primary_expression (parser, false, false,
29262 false, &idk);
29263 if (!cp_parser_error_occurred (parser)
29264 && decl
29265 && DECL_P (decl)
29266 && CLASS_TYPE_P (TREE_TYPE (decl)))
29268 tree rhs;
29270 cp_parser_parse_definitely (parser);
29271 cp_parser_require (parser, CPP_EQ, RT_EQ);
29272 rhs = cp_parser_assignment_expression (parser, false, NULL);
29273 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29274 decl, NOP_EXPR,
29275 rhs,
29276 tf_warning_or_error));
29277 add_private_clause = true;
29279 else
29281 decl = NULL;
29282 cp_parser_abort_tentative_parse (parser);
29283 init = cp_parser_expression (parser, false, NULL);
29284 if (init)
29286 if (TREE_CODE (init) == MODIFY_EXPR
29287 || TREE_CODE (init) == MODOP_EXPR)
29288 real_decl = TREE_OPERAND (init, 0);
29292 return add_private_clause;
29295 /* Parse the restricted form of the for statement allowed by OpenMP. */
29297 static tree
29298 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29299 tree *cclauses)
29301 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29302 tree real_decl, initv, condv, incrv, declv;
29303 tree this_pre_body, cl;
29304 location_t loc_first;
29305 bool collapse_err = false;
29306 int i, collapse = 1, nbraces = 0;
29307 vec<tree, va_gc> *for_block = make_tree_vector ();
29309 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29310 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29311 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29313 gcc_assert (collapse >= 1);
29315 declv = make_tree_vec (collapse);
29316 initv = make_tree_vec (collapse);
29317 condv = make_tree_vec (collapse);
29318 incrv = make_tree_vec (collapse);
29320 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29322 for (i = 0; i < collapse; i++)
29324 int bracecount = 0;
29325 bool add_private_clause = false;
29326 location_t loc;
29328 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29330 cp_parser_error (parser, "for statement expected");
29331 return NULL;
29333 loc = cp_lexer_consume_token (parser->lexer)->location;
29335 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29336 return NULL;
29338 init = decl = real_decl = NULL;
29339 this_pre_body = push_stmt_list ();
29341 add_private_clause
29342 |= cp_parser_omp_for_loop_init (parser,
29343 /*parsing_openmp=*/code != CILK_SIMD,
29344 this_pre_body, for_block,
29345 init, decl, real_decl);
29347 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29348 if (this_pre_body)
29350 this_pre_body = pop_stmt_list (this_pre_body);
29351 if (pre_body)
29353 tree t = pre_body;
29354 pre_body = push_stmt_list ();
29355 add_stmt (t);
29356 add_stmt (this_pre_body);
29357 pre_body = pop_stmt_list (pre_body);
29359 else
29360 pre_body = this_pre_body;
29363 if (decl)
29364 real_decl = decl;
29365 if (cclauses != NULL
29366 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29367 && real_decl != NULL_TREE)
29369 tree *c;
29370 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29371 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29372 && OMP_CLAUSE_DECL (*c) == real_decl)
29374 error_at (loc, "iteration variable %qD"
29375 " should not be firstprivate", real_decl);
29376 *c = OMP_CLAUSE_CHAIN (*c);
29378 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29379 && OMP_CLAUSE_DECL (*c) == real_decl)
29381 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29382 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29383 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29384 OMP_CLAUSE_DECL (l) = real_decl;
29385 OMP_CLAUSE_CHAIN (l) = clauses;
29386 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29387 clauses = l;
29388 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29389 CP_OMP_CLAUSE_INFO (*c) = NULL;
29390 add_private_clause = false;
29392 else
29394 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29395 && OMP_CLAUSE_DECL (*c) == real_decl)
29396 add_private_clause = false;
29397 c = &OMP_CLAUSE_CHAIN (*c);
29401 if (add_private_clause)
29403 tree c;
29404 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29406 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29407 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29408 && OMP_CLAUSE_DECL (c) == decl)
29409 break;
29410 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29411 && OMP_CLAUSE_DECL (c) == decl)
29412 error_at (loc, "iteration variable %qD "
29413 "should not be firstprivate",
29414 decl);
29415 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29416 && OMP_CLAUSE_DECL (c) == decl)
29417 error_at (loc, "iteration variable %qD should not be reduction",
29418 decl);
29420 if (c == NULL)
29422 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29423 OMP_CLAUSE_DECL (c) = decl;
29424 c = finish_omp_clauses (c);
29425 if (c)
29427 OMP_CLAUSE_CHAIN (c) = clauses;
29428 clauses = c;
29433 cond = NULL;
29434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29435 cond = cp_parser_omp_for_cond (parser, decl, code);
29436 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29438 incr = NULL;
29439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29441 /* If decl is an iterator, preserve the operator on decl
29442 until finish_omp_for. */
29443 if (real_decl
29444 && ((processing_template_decl
29445 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29446 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29447 incr = cp_parser_omp_for_incr (parser, real_decl);
29448 else
29449 incr = cp_parser_expression (parser, false, NULL);
29450 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29451 SET_EXPR_LOCATION (incr, input_location);
29454 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29456 /*or_comma=*/false,
29457 /*consume_paren=*/true);
29459 TREE_VEC_ELT (declv, i) = decl;
29460 TREE_VEC_ELT (initv, i) = init;
29461 TREE_VEC_ELT (condv, i) = cond;
29462 TREE_VEC_ELT (incrv, i) = incr;
29464 if (i == collapse - 1)
29465 break;
29467 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29468 in between the collapsed for loops to be still considered perfectly
29469 nested. Hopefully the final version clarifies this.
29470 For now handle (multiple) {'s and empty statements. */
29471 cp_parser_parse_tentatively (parser);
29474 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29475 break;
29476 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29478 cp_lexer_consume_token (parser->lexer);
29479 bracecount++;
29481 else if (bracecount
29482 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29483 cp_lexer_consume_token (parser->lexer);
29484 else
29486 loc = cp_lexer_peek_token (parser->lexer)->location;
29487 error_at (loc, "not enough collapsed for loops");
29488 collapse_err = true;
29489 cp_parser_abort_tentative_parse (parser);
29490 declv = NULL_TREE;
29491 break;
29494 while (1);
29496 if (declv)
29498 cp_parser_parse_definitely (parser);
29499 nbraces += bracecount;
29503 /* Note that we saved the original contents of this flag when we entered
29504 the structured block, and so we don't need to re-save it here. */
29505 if (code == CILK_SIMD)
29506 parser->in_statement = IN_CILK_SIMD_FOR;
29507 else
29508 parser->in_statement = IN_OMP_FOR;
29510 /* Note that the grammar doesn't call for a structured block here,
29511 though the loop as a whole is a structured block. */
29512 body = push_stmt_list ();
29513 cp_parser_statement (parser, NULL_TREE, false, NULL);
29514 body = pop_stmt_list (body);
29516 if (declv == NULL_TREE)
29517 ret = NULL_TREE;
29518 else
29519 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29520 pre_body, clauses);
29522 while (nbraces)
29524 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29526 cp_lexer_consume_token (parser->lexer);
29527 nbraces--;
29529 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29530 cp_lexer_consume_token (parser->lexer);
29531 else
29533 if (!collapse_err)
29535 error_at (cp_lexer_peek_token (parser->lexer)->location,
29536 "collapsed loops not perfectly nested");
29538 collapse_err = true;
29539 cp_parser_statement_seq_opt (parser, NULL);
29540 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29541 break;
29545 while (!for_block->is_empty ())
29546 add_stmt (pop_stmt_list (for_block->pop ()));
29547 release_tree_vector (for_block);
29549 return ret;
29552 /* Helper function for OpenMP parsing, split clauses and call
29553 finish_omp_clauses on each of the set of clauses afterwards. */
29555 static void
29556 cp_omp_split_clauses (location_t loc, enum tree_code code,
29557 omp_clause_mask mask, tree clauses, tree *cclauses)
29559 int i;
29560 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29561 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29562 if (cclauses[i])
29563 cclauses[i] = finish_omp_clauses (cclauses[i]);
29566 /* OpenMP 4.0:
29567 #pragma omp simd simd-clause[optseq] new-line
29568 for-loop */
29570 #define OMP_SIMD_CLAUSE_MASK \
29571 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29579 static tree
29580 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29581 char *p_name, omp_clause_mask mask, tree *cclauses)
29583 tree clauses, sb, ret;
29584 unsigned int save;
29585 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29587 strcat (p_name, " simd");
29588 mask |= OMP_SIMD_CLAUSE_MASK;
29589 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29591 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29592 cclauses == NULL);
29593 if (cclauses)
29595 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29596 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29599 sb = begin_omp_structured_block ();
29600 save = cp_parser_begin_omp_structured_block (parser);
29602 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29604 cp_parser_end_omp_structured_block (parser, save);
29605 add_stmt (finish_omp_structured_block (sb));
29607 return ret;
29610 /* OpenMP 2.5:
29611 #pragma omp for for-clause[optseq] new-line
29612 for-loop
29614 OpenMP 4.0:
29615 #pragma omp for simd for-simd-clause[optseq] new-line
29616 for-loop */
29618 #define OMP_FOR_CLAUSE_MASK \
29619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29628 static tree
29629 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29630 char *p_name, omp_clause_mask mask, tree *cclauses)
29632 tree clauses, sb, ret;
29633 unsigned int save;
29634 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29636 strcat (p_name, " for");
29637 mask |= OMP_FOR_CLAUSE_MASK;
29638 if (cclauses)
29639 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29644 const char *p = IDENTIFIER_POINTER (id);
29646 if (strcmp (p, "simd") == 0)
29648 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29649 if (cclauses == NULL)
29650 cclauses = cclauses_buf;
29652 cp_lexer_consume_token (parser->lexer);
29653 if (!flag_openmp) /* flag_openmp_simd */
29654 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29655 cclauses);
29656 sb = begin_omp_structured_block ();
29657 save = cp_parser_begin_omp_structured_block (parser);
29658 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29659 cclauses);
29660 cp_parser_end_omp_structured_block (parser, save);
29661 tree body = finish_omp_structured_block (sb);
29662 if (ret == NULL)
29663 return ret;
29664 ret = make_node (OMP_FOR);
29665 TREE_TYPE (ret) = void_type_node;
29666 OMP_FOR_BODY (ret) = body;
29667 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29668 SET_EXPR_LOCATION (ret, loc);
29669 add_stmt (ret);
29670 return ret;
29673 if (!flag_openmp) /* flag_openmp_simd */
29675 cp_parser_require_pragma_eol (parser, pragma_tok);
29676 return NULL_TREE;
29679 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29680 cclauses == NULL);
29681 if (cclauses)
29683 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29684 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29687 sb = begin_omp_structured_block ();
29688 save = cp_parser_begin_omp_structured_block (parser);
29690 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29692 cp_parser_end_omp_structured_block (parser, save);
29693 add_stmt (finish_omp_structured_block (sb));
29695 return ret;
29698 /* OpenMP 2.5:
29699 # pragma omp master new-line
29700 structured-block */
29702 static tree
29703 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29705 cp_parser_require_pragma_eol (parser, pragma_tok);
29706 return c_finish_omp_master (input_location,
29707 cp_parser_omp_structured_block (parser));
29710 /* OpenMP 2.5:
29711 # pragma omp ordered new-line
29712 structured-block */
29714 static tree
29715 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29717 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29718 cp_parser_require_pragma_eol (parser, pragma_tok);
29719 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29722 /* OpenMP 2.5:
29724 section-scope:
29725 { section-sequence }
29727 section-sequence:
29728 section-directive[opt] structured-block
29729 section-sequence section-directive structured-block */
29731 static tree
29732 cp_parser_omp_sections_scope (cp_parser *parser)
29734 tree stmt, substmt;
29735 bool error_suppress = false;
29736 cp_token *tok;
29738 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29739 return NULL_TREE;
29741 stmt = push_stmt_list ();
29743 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29745 substmt = cp_parser_omp_structured_block (parser);
29746 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29747 add_stmt (substmt);
29750 while (1)
29752 tok = cp_lexer_peek_token (parser->lexer);
29753 if (tok->type == CPP_CLOSE_BRACE)
29754 break;
29755 if (tok->type == CPP_EOF)
29756 break;
29758 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29760 cp_lexer_consume_token (parser->lexer);
29761 cp_parser_require_pragma_eol (parser, tok);
29762 error_suppress = false;
29764 else if (!error_suppress)
29766 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29767 error_suppress = true;
29770 substmt = cp_parser_omp_structured_block (parser);
29771 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29772 add_stmt (substmt);
29774 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29776 substmt = pop_stmt_list (stmt);
29778 stmt = make_node (OMP_SECTIONS);
29779 TREE_TYPE (stmt) = void_type_node;
29780 OMP_SECTIONS_BODY (stmt) = substmt;
29782 add_stmt (stmt);
29783 return stmt;
29786 /* OpenMP 2.5:
29787 # pragma omp sections sections-clause[optseq] newline
29788 sections-scope */
29790 #define OMP_SECTIONS_CLAUSE_MASK \
29791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29797 static tree
29798 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29799 char *p_name, omp_clause_mask mask, tree *cclauses)
29801 tree clauses, ret;
29802 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29804 strcat (p_name, " sections");
29805 mask |= OMP_SECTIONS_CLAUSE_MASK;
29806 if (cclauses)
29807 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29809 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29810 cclauses == NULL);
29811 if (cclauses)
29813 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29814 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29817 ret = cp_parser_omp_sections_scope (parser);
29818 if (ret)
29819 OMP_SECTIONS_CLAUSES (ret) = clauses;
29821 return ret;
29824 /* OpenMP 2.5:
29825 # pragma omp parallel parallel-clause[optseq] new-line
29826 structured-block
29827 # pragma omp parallel for parallel-for-clause[optseq] new-line
29828 structured-block
29829 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29830 structured-block
29832 OpenMP 4.0:
29833 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29834 structured-block */
29836 #define OMP_PARALLEL_CLAUSE_MASK \
29837 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29847 static tree
29848 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29849 char *p_name, omp_clause_mask mask, tree *cclauses)
29851 tree stmt, clauses, block;
29852 unsigned int save;
29853 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29855 strcat (p_name, " parallel");
29856 mask |= OMP_PARALLEL_CLAUSE_MASK;
29858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29860 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29861 if (cclauses == NULL)
29862 cclauses = cclauses_buf;
29864 cp_lexer_consume_token (parser->lexer);
29865 if (!flag_openmp) /* flag_openmp_simd */
29866 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29867 block = begin_omp_parallel ();
29868 save = cp_parser_begin_omp_structured_block (parser);
29869 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29870 cp_parser_end_omp_structured_block (parser, save);
29871 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29872 block);
29873 if (ret == NULL_TREE)
29874 return ret;
29875 OMP_PARALLEL_COMBINED (stmt) = 1;
29876 return stmt;
29878 else if (cclauses)
29880 error_at (loc, "expected %<for%> after %qs", p_name);
29881 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29882 return NULL_TREE;
29884 else if (!flag_openmp) /* flag_openmp_simd */
29886 cp_parser_require_pragma_eol (parser, pragma_tok);
29887 return NULL_TREE;
29889 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29891 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29892 const char *p = IDENTIFIER_POINTER (id);
29893 if (strcmp (p, "sections") == 0)
29895 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29896 cclauses = cclauses_buf;
29898 cp_lexer_consume_token (parser->lexer);
29899 block = begin_omp_parallel ();
29900 save = cp_parser_begin_omp_structured_block (parser);
29901 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29902 cp_parser_end_omp_structured_block (parser, save);
29903 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29904 block);
29905 OMP_PARALLEL_COMBINED (stmt) = 1;
29906 return stmt;
29910 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29912 block = begin_omp_parallel ();
29913 save = cp_parser_begin_omp_structured_block (parser);
29914 cp_parser_statement (parser, NULL_TREE, false, NULL);
29915 cp_parser_end_omp_structured_block (parser, save);
29916 stmt = finish_omp_parallel (clauses, block);
29917 return stmt;
29920 /* OpenMP 2.5:
29921 # pragma omp single single-clause[optseq] new-line
29922 structured-block */
29924 #define OMP_SINGLE_CLAUSE_MASK \
29925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29930 static tree
29931 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29933 tree stmt = make_node (OMP_SINGLE);
29934 TREE_TYPE (stmt) = void_type_node;
29936 OMP_SINGLE_CLAUSES (stmt)
29937 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29938 "#pragma omp single", pragma_tok);
29939 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29941 return add_stmt (stmt);
29944 /* OpenMP 3.0:
29945 # pragma omp task task-clause[optseq] new-line
29946 structured-block */
29948 #define OMP_TASK_CLAUSE_MASK \
29949 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29959 static tree
29960 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29962 tree clauses, block;
29963 unsigned int save;
29965 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29966 "#pragma omp task", pragma_tok);
29967 block = begin_omp_task ();
29968 save = cp_parser_begin_omp_structured_block (parser);
29969 cp_parser_statement (parser, NULL_TREE, false, NULL);
29970 cp_parser_end_omp_structured_block (parser, save);
29971 return finish_omp_task (clauses, block);
29974 /* OpenMP 3.0:
29975 # pragma omp taskwait new-line */
29977 static void
29978 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29980 cp_parser_require_pragma_eol (parser, pragma_tok);
29981 finish_omp_taskwait ();
29984 /* OpenMP 3.1:
29985 # pragma omp taskyield new-line */
29987 static void
29988 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29990 cp_parser_require_pragma_eol (parser, pragma_tok);
29991 finish_omp_taskyield ();
29994 /* OpenMP 4.0:
29995 # pragma omp taskgroup new-line
29996 structured-block */
29998 static tree
29999 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30001 cp_parser_require_pragma_eol (parser, pragma_tok);
30002 return c_finish_omp_taskgroup (input_location,
30003 cp_parser_omp_structured_block (parser));
30007 /* OpenMP 2.5:
30008 # pragma omp threadprivate (variable-list) */
30010 static void
30011 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30013 tree vars;
30015 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30016 cp_parser_require_pragma_eol (parser, pragma_tok);
30018 finish_omp_threadprivate (vars);
30021 /* OpenMP 4.0:
30022 # pragma omp cancel cancel-clause[optseq] new-line */
30024 #define OMP_CANCEL_CLAUSE_MASK \
30025 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30031 static void
30032 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30034 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30035 "#pragma omp cancel", pragma_tok);
30036 finish_omp_cancel (clauses);
30039 /* OpenMP 4.0:
30040 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30042 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30048 static void
30049 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30051 tree clauses;
30052 bool point_seen = false;
30054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30056 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30057 const char *p = IDENTIFIER_POINTER (id);
30059 if (strcmp (p, "point") == 0)
30061 cp_lexer_consume_token (parser->lexer);
30062 point_seen = true;
30065 if (!point_seen)
30067 cp_parser_error (parser, "expected %<point%>");
30068 cp_parser_require_pragma_eol (parser, pragma_tok);
30069 return;
30072 clauses = cp_parser_omp_all_clauses (parser,
30073 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30074 "#pragma omp cancellation point",
30075 pragma_tok);
30076 finish_omp_cancellation_point (clauses);
30079 /* OpenMP 4.0:
30080 #pragma omp distribute distribute-clause[optseq] new-line
30081 for-loop */
30083 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30089 static tree
30090 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30091 char *p_name, omp_clause_mask mask, tree *cclauses)
30093 tree clauses, sb, ret;
30094 unsigned int save;
30095 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30097 strcat (p_name, " distribute");
30098 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30100 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30102 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30103 const char *p = IDENTIFIER_POINTER (id);
30104 bool simd = false;
30105 bool parallel = false;
30107 if (strcmp (p, "simd") == 0)
30108 simd = true;
30109 else
30110 parallel = strcmp (p, "parallel") == 0;
30111 if (parallel || simd)
30113 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30114 if (cclauses == NULL)
30115 cclauses = cclauses_buf;
30116 cp_lexer_consume_token (parser->lexer);
30117 if (!flag_openmp) /* flag_openmp_simd */
30119 if (simd)
30120 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30121 cclauses);
30122 else
30123 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30124 cclauses);
30126 sb = begin_omp_structured_block ();
30127 save = cp_parser_begin_omp_structured_block (parser);
30128 if (simd)
30129 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30130 cclauses);
30131 else
30132 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30133 cclauses);
30134 cp_parser_end_omp_structured_block (parser, save);
30135 tree body = finish_omp_structured_block (sb);
30136 if (ret == NULL)
30137 return ret;
30138 ret = make_node (OMP_DISTRIBUTE);
30139 TREE_TYPE (ret) = void_type_node;
30140 OMP_FOR_BODY (ret) = body;
30141 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30142 SET_EXPR_LOCATION (ret, loc);
30143 add_stmt (ret);
30144 return ret;
30147 if (!flag_openmp) /* flag_openmp_simd */
30149 cp_parser_require_pragma_eol (parser, pragma_tok);
30150 return NULL_TREE;
30153 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30154 cclauses == NULL);
30155 if (cclauses)
30157 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30158 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30161 sb = begin_omp_structured_block ();
30162 save = cp_parser_begin_omp_structured_block (parser);
30164 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30166 cp_parser_end_omp_structured_block (parser, save);
30167 add_stmt (finish_omp_structured_block (sb));
30169 return ret;
30172 /* OpenMP 4.0:
30173 # pragma omp teams teams-clause[optseq] new-line
30174 structured-block */
30176 #define OMP_TEAMS_CLAUSE_MASK \
30177 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30185 static tree
30186 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30187 char *p_name, omp_clause_mask mask, tree *cclauses)
30189 tree clauses, sb, ret;
30190 unsigned int save;
30191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30193 strcat (p_name, " teams");
30194 mask |= OMP_TEAMS_CLAUSE_MASK;
30196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30198 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30199 const char *p = IDENTIFIER_POINTER (id);
30200 if (strcmp (p, "distribute") == 0)
30202 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30203 if (cclauses == NULL)
30204 cclauses = cclauses_buf;
30206 cp_lexer_consume_token (parser->lexer);
30207 if (!flag_openmp) /* flag_openmp_simd */
30208 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30209 cclauses);
30210 sb = begin_omp_structured_block ();
30211 save = cp_parser_begin_omp_structured_block (parser);
30212 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30213 cclauses);
30214 cp_parser_end_omp_structured_block (parser, save);
30215 tree body = finish_omp_structured_block (sb);
30216 if (ret == NULL)
30217 return ret;
30218 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30219 ret = make_node (OMP_TEAMS);
30220 TREE_TYPE (ret) = void_type_node;
30221 OMP_TEAMS_CLAUSES (ret) = clauses;
30222 OMP_TEAMS_BODY (ret) = body;
30223 return add_stmt (ret);
30226 if (!flag_openmp) /* flag_openmp_simd */
30228 cp_parser_require_pragma_eol (parser, pragma_tok);
30229 return NULL_TREE;
30232 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30233 cclauses == NULL);
30234 if (cclauses)
30236 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30237 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30240 tree stmt = make_node (OMP_TEAMS);
30241 TREE_TYPE (stmt) = void_type_node;
30242 OMP_TEAMS_CLAUSES (stmt) = clauses;
30243 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30245 return add_stmt (stmt);
30248 /* OpenMP 4.0:
30249 # pragma omp target data target-data-clause[optseq] new-line
30250 structured-block */
30252 #define OMP_TARGET_DATA_CLAUSE_MASK \
30253 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30257 static tree
30258 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30260 tree stmt = make_node (OMP_TARGET_DATA);
30261 TREE_TYPE (stmt) = void_type_node;
30263 OMP_TARGET_DATA_CLAUSES (stmt)
30264 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30265 "#pragma omp target data", pragma_tok);
30266 keep_next_level (true);
30267 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30269 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30270 return add_stmt (stmt);
30273 /* OpenMP 4.0:
30274 # pragma omp target update target-update-clause[optseq] new-line */
30276 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30282 static bool
30283 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30284 enum pragma_context context)
30286 if (context == pragma_stmt)
30288 error_at (pragma_tok->location,
30289 "%<#pragma omp target update%> may only be "
30290 "used in compound statements");
30291 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30292 return false;
30295 tree clauses
30296 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30297 "#pragma omp target update", pragma_tok);
30298 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30299 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30301 error_at (pragma_tok->location,
30302 "%<#pragma omp target update must contain at least one "
30303 "%<from%> or %<to%> clauses");
30304 return false;
30307 tree stmt = make_node (OMP_TARGET_UPDATE);
30308 TREE_TYPE (stmt) = void_type_node;
30309 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30310 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30311 add_stmt (stmt);
30312 return false;
30315 /* OpenMP 4.0:
30316 # pragma omp target target-clause[optseq] new-line
30317 structured-block */
30319 #define OMP_TARGET_CLAUSE_MASK \
30320 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30324 static bool
30325 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30326 enum pragma_context context)
30328 if (context != pragma_stmt && context != pragma_compound)
30330 cp_parser_error (parser, "expected declaration specifiers");
30331 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30332 return false;
30335 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30337 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30338 const char *p = IDENTIFIER_POINTER (id);
30340 if (strcmp (p, "teams") == 0)
30342 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30343 char p_name[sizeof ("#pragma omp target teams distribute "
30344 "parallel for simd")];
30346 cp_lexer_consume_token (parser->lexer);
30347 strcpy (p_name, "#pragma omp target");
30348 if (!flag_openmp) /* flag_openmp_simd */
30350 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30351 OMP_TARGET_CLAUSE_MASK,
30352 cclauses);
30353 return stmt != NULL_TREE;
30355 keep_next_level (true);
30356 tree sb = begin_omp_structured_block ();
30357 unsigned save = cp_parser_begin_omp_structured_block (parser);
30358 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30359 OMP_TARGET_CLAUSE_MASK, cclauses);
30360 cp_parser_end_omp_structured_block (parser, save);
30361 tree body = finish_omp_structured_block (sb);
30362 if (ret == NULL_TREE)
30363 return false;
30364 tree stmt = make_node (OMP_TARGET);
30365 TREE_TYPE (stmt) = void_type_node;
30366 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30367 OMP_TARGET_BODY (stmt) = body;
30368 add_stmt (stmt);
30369 return true;
30371 else if (!flag_openmp) /* flag_openmp_simd */
30373 cp_parser_require_pragma_eol (parser, pragma_tok);
30374 return false;
30376 else if (strcmp (p, "data") == 0)
30378 cp_lexer_consume_token (parser->lexer);
30379 cp_parser_omp_target_data (parser, pragma_tok);
30380 return true;
30382 else if (strcmp (p, "update") == 0)
30384 cp_lexer_consume_token (parser->lexer);
30385 return cp_parser_omp_target_update (parser, pragma_tok, context);
30389 tree stmt = make_node (OMP_TARGET);
30390 TREE_TYPE (stmt) = void_type_node;
30392 OMP_TARGET_CLAUSES (stmt)
30393 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30394 "#pragma omp target", pragma_tok);
30395 keep_next_level (true);
30396 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30398 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30399 add_stmt (stmt);
30400 return true;
30403 /* OpenMP 4.0:
30404 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30406 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30407 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30414 static void
30415 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30416 enum pragma_context context)
30418 bool first_p = parser->omp_declare_simd == NULL;
30419 cp_omp_declare_simd_data data;
30420 if (first_p)
30422 data.error_seen = false;
30423 data.fndecl_seen = false;
30424 data.tokens = vNULL;
30425 parser->omp_declare_simd = &data;
30427 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30428 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30429 cp_lexer_consume_token (parser->lexer);
30430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30431 parser->omp_declare_simd->error_seen = true;
30432 cp_parser_require_pragma_eol (parser, pragma_tok);
30433 struct cp_token_cache *cp
30434 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30435 parser->omp_declare_simd->tokens.safe_push (cp);
30436 if (first_p)
30438 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30439 cp_parser_pragma (parser, context);
30440 switch (context)
30442 case pragma_external:
30443 cp_parser_declaration (parser);
30444 break;
30445 case pragma_member:
30446 cp_parser_member_declaration (parser);
30447 break;
30448 case pragma_objc_icode:
30449 cp_parser_block_declaration (parser, /*statement_p=*/false);
30450 break;
30451 default:
30452 cp_parser_declaration_statement (parser);
30453 break;
30455 if (parser->omp_declare_simd
30456 && !parser->omp_declare_simd->error_seen
30457 && !parser->omp_declare_simd->fndecl_seen)
30458 error_at (pragma_tok->location,
30459 "%<#pragma omp declare simd%> not immediately followed by "
30460 "function declaration or definition");
30461 data.tokens.release ();
30462 parser->omp_declare_simd = NULL;
30466 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30467 This function is modelled similar to the late parsing of omp declare
30468 simd. */
30470 static tree
30471 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30473 struct cp_token_cache *ce;
30474 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30475 int ii = 0;
30477 if (parser->omp_declare_simd != NULL)
30479 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30480 " marked as a Cilk Plus SIMD-enabled function");
30481 XDELETE (parser->cilk_simd_fn_info);
30482 parser->cilk_simd_fn_info = NULL;
30483 return attrs;
30485 if (!info->error_seen && info->fndecl_seen)
30487 error ("vector attribute not immediately followed by a single function"
30488 " declaration or definition");
30489 info->error_seen = true;
30491 if (info->error_seen)
30492 return attrs;
30494 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30496 tree c, cl;
30498 cp_parser_push_lexer_for_tokens (parser, ce);
30499 parser->lexer->in_pragma = true;
30500 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30501 "SIMD-enabled functions attribute",
30502 NULL);
30503 cp_parser_pop_lexer (parser);
30504 if (cl)
30505 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30507 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30508 TREE_CHAIN (c) = attrs;
30509 attrs = c;
30511 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30512 TREE_CHAIN (c) = attrs;
30513 if (processing_template_decl)
30514 ATTR_IS_DEPENDENT (c) = 1;
30515 attrs = c;
30517 info->fndecl_seen = true;
30518 XDELETE (parser->cilk_simd_fn_info);
30519 parser->cilk_simd_fn_info = NULL;
30520 return attrs;
30523 /* Finalize #pragma omp declare simd clauses after direct declarator has
30524 been parsed, and put that into "omp declare simd" attribute. */
30526 static tree
30527 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30529 struct cp_token_cache *ce;
30530 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30531 int i;
30533 if (!data->error_seen && data->fndecl_seen)
30535 error ("%<#pragma omp declare simd%> not immediately followed by "
30536 "a single function declaration or definition");
30537 data->error_seen = true;
30538 return attrs;
30540 if (data->error_seen)
30541 return attrs;
30543 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30545 tree c, cl;
30547 cp_parser_push_lexer_for_tokens (parser, ce);
30548 parser->lexer->in_pragma = true;
30549 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30550 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30551 cp_lexer_consume_token (parser->lexer);
30552 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30553 "#pragma omp declare simd", pragma_tok);
30554 cp_parser_pop_lexer (parser);
30555 if (cl)
30556 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30557 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30558 TREE_CHAIN (c) = attrs;
30559 if (processing_template_decl)
30560 ATTR_IS_DEPENDENT (c) = 1;
30561 attrs = c;
30564 data->fndecl_seen = true;
30565 return attrs;
30569 /* OpenMP 4.0:
30570 # pragma omp declare target new-line
30571 declarations and definitions
30572 # pragma omp end declare target new-line */
30574 static void
30575 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30577 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30578 scope_chain->omp_declare_target_attribute++;
30581 static void
30582 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30584 const char *p = "";
30585 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30587 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30588 p = IDENTIFIER_POINTER (id);
30590 if (strcmp (p, "declare") == 0)
30592 cp_lexer_consume_token (parser->lexer);
30593 p = "";
30594 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30597 p = IDENTIFIER_POINTER (id);
30599 if (strcmp (p, "target") == 0)
30600 cp_lexer_consume_token (parser->lexer);
30601 else
30603 cp_parser_error (parser, "expected %<target%>");
30604 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30605 return;
30608 else
30610 cp_parser_error (parser, "expected %<declare%>");
30611 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30612 return;
30614 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30615 if (!scope_chain->omp_declare_target_attribute)
30616 error_at (pragma_tok->location,
30617 "%<#pragma omp end declare target%> without corresponding "
30618 "%<#pragma omp declare target%>");
30619 else
30620 scope_chain->omp_declare_target_attribute--;
30623 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30624 expression and optional initializer clause of
30625 #pragma omp declare reduction. We store the expression(s) as
30626 either 3, 6 or 7 special statements inside of the artificial function's
30627 body. The first two statements are DECL_EXPRs for the artificial
30628 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30629 expression that uses those variables.
30630 If there was any INITIALIZER clause, this is followed by further statements,
30631 the fourth and fifth statements are DECL_EXPRs for the artificial
30632 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30633 constructor variant (first token after open paren is not omp_priv),
30634 then the sixth statement is a statement with the function call expression
30635 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30636 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30637 to initialize the OMP_PRIV artificial variable and there is seventh
30638 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30640 static bool
30641 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30643 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30644 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30645 type = TREE_TYPE (type);
30646 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30647 DECL_ARTIFICIAL (omp_out) = 1;
30648 pushdecl (omp_out);
30649 add_decl_expr (omp_out);
30650 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30651 DECL_ARTIFICIAL (omp_in) = 1;
30652 pushdecl (omp_in);
30653 add_decl_expr (omp_in);
30654 tree combiner;
30655 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30657 keep_next_level (true);
30658 tree block = begin_omp_structured_block ();
30659 combiner = cp_parser_expression (parser, false, NULL);
30660 finish_expr_stmt (combiner);
30661 block = finish_omp_structured_block (block);
30662 add_stmt (block);
30664 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30665 return false;
30667 const char *p = "";
30668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30671 p = IDENTIFIER_POINTER (id);
30674 if (strcmp (p, "initializer") == 0)
30676 cp_lexer_consume_token (parser->lexer);
30677 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30678 return false;
30680 p = "";
30681 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30683 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30684 p = IDENTIFIER_POINTER (id);
30687 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30688 DECL_ARTIFICIAL (omp_priv) = 1;
30689 pushdecl (omp_priv);
30690 add_decl_expr (omp_priv);
30691 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30692 DECL_ARTIFICIAL (omp_orig) = 1;
30693 pushdecl (omp_orig);
30694 add_decl_expr (omp_orig);
30696 keep_next_level (true);
30697 block = begin_omp_structured_block ();
30699 bool ctor = false;
30700 if (strcmp (p, "omp_priv") == 0)
30702 bool is_direct_init, is_non_constant_init;
30703 ctor = true;
30704 cp_lexer_consume_token (parser->lexer);
30705 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30706 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30707 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30708 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30709 == CPP_CLOSE_PAREN
30710 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30711 == CPP_CLOSE_PAREN))
30713 finish_omp_structured_block (block);
30714 error ("invalid initializer clause");
30715 return false;
30717 initializer = cp_parser_initializer (parser, &is_direct_init,
30718 &is_non_constant_init);
30719 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30720 NULL_TREE, LOOKUP_ONLYCONVERTING);
30722 else
30724 cp_parser_parse_tentatively (parser);
30725 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30726 /*check_dependency_p=*/true,
30727 /*template_p=*/NULL,
30728 /*declarator_p=*/false,
30729 /*optional_p=*/false);
30730 vec<tree, va_gc> *args;
30731 if (fn_name == error_mark_node
30732 || cp_parser_error_occurred (parser)
30733 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30734 || ((args = cp_parser_parenthesized_expression_list
30735 (parser, non_attr, /*cast_p=*/false,
30736 /*allow_expansion_p=*/true,
30737 /*non_constant_p=*/NULL)),
30738 cp_parser_error_occurred (parser)))
30740 finish_omp_structured_block (block);
30741 cp_parser_abort_tentative_parse (parser);
30742 cp_parser_error (parser, "expected id-expression (arguments)");
30743 return false;
30745 unsigned int i;
30746 tree arg;
30747 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30748 if (arg == omp_priv
30749 || (TREE_CODE (arg) == ADDR_EXPR
30750 && TREE_OPERAND (arg, 0) == omp_priv))
30751 break;
30752 cp_parser_abort_tentative_parse (parser);
30753 if (arg == NULL_TREE)
30754 error ("one of the initializer call arguments should be %<omp_priv%>"
30755 " or %<&omp_priv%>");
30756 initializer = cp_parser_postfix_expression (parser, false, false, false,
30757 false, NULL);
30758 finish_expr_stmt (initializer);
30761 block = finish_omp_structured_block (block);
30762 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30763 finish_expr_stmt (block);
30765 if (ctor)
30766 add_decl_expr (omp_orig);
30768 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30769 return false;
30772 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30773 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30775 return true;
30778 /* OpenMP 4.0
30779 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30780 initializer-clause[opt] new-line
30782 initializer-clause:
30783 initializer (omp_priv initializer)
30784 initializer (function-name (argument-list)) */
30786 static void
30787 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30788 enum pragma_context)
30790 auto_vec<tree> types;
30791 enum tree_code reduc_code = ERROR_MARK;
30792 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30793 unsigned int i;
30794 cp_token *first_token;
30795 cp_token_cache *cp;
30796 int errs;
30797 void *p;
30799 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
30800 p = obstack_alloc (&declarator_obstack, 0);
30802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30803 goto fail;
30805 switch (cp_lexer_peek_token (parser->lexer)->type)
30807 case CPP_PLUS:
30808 reduc_code = PLUS_EXPR;
30809 break;
30810 case CPP_MULT:
30811 reduc_code = MULT_EXPR;
30812 break;
30813 case CPP_MINUS:
30814 reduc_code = MINUS_EXPR;
30815 break;
30816 case CPP_AND:
30817 reduc_code = BIT_AND_EXPR;
30818 break;
30819 case CPP_XOR:
30820 reduc_code = BIT_XOR_EXPR;
30821 break;
30822 case CPP_OR:
30823 reduc_code = BIT_IOR_EXPR;
30824 break;
30825 case CPP_AND_AND:
30826 reduc_code = TRUTH_ANDIF_EXPR;
30827 break;
30828 case CPP_OR_OR:
30829 reduc_code = TRUTH_ORIF_EXPR;
30830 break;
30831 case CPP_NAME:
30832 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30833 break;
30834 default:
30835 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30836 "%<|%>, %<&&%>, %<||%> or identifier");
30837 goto fail;
30840 if (reduc_code != ERROR_MARK)
30841 cp_lexer_consume_token (parser->lexer);
30843 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30844 if (reduc_id == error_mark_node)
30845 goto fail;
30847 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30848 goto fail;
30850 /* Types may not be defined in declare reduction type list. */
30851 const char *saved_message;
30852 saved_message = parser->type_definition_forbidden_message;
30853 parser->type_definition_forbidden_message
30854 = G_("types may not be defined in declare reduction type list");
30855 bool saved_colon_corrects_to_scope_p;
30856 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30857 parser->colon_corrects_to_scope_p = false;
30858 bool saved_colon_doesnt_start_class_def_p;
30859 saved_colon_doesnt_start_class_def_p
30860 = parser->colon_doesnt_start_class_def_p;
30861 parser->colon_doesnt_start_class_def_p = true;
30863 while (true)
30865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30866 type = cp_parser_type_id (parser);
30867 if (type == error_mark_node)
30869 else if (ARITHMETIC_TYPE_P (type)
30870 && (orig_reduc_id == NULL_TREE
30871 || (TREE_CODE (type) != COMPLEX_TYPE
30872 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30873 "min") == 0
30874 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30875 "max") == 0))))
30876 error_at (loc, "predeclared arithmetic type %qT in "
30877 "%<#pragma omp declare reduction%>", type);
30878 else if (TREE_CODE (type) == FUNCTION_TYPE
30879 || TREE_CODE (type) == METHOD_TYPE
30880 || TREE_CODE (type) == ARRAY_TYPE)
30881 error_at (loc, "function or array type %qT in "
30882 "%<#pragma omp declare reduction%>", type);
30883 else if (TREE_CODE (type) == REFERENCE_TYPE)
30884 error_at (loc, "reference type %qT in "
30885 "%<#pragma omp declare reduction%>", type);
30886 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30887 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30888 "%<#pragma omp declare reduction%>", type);
30889 else
30890 types.safe_push (type);
30892 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30893 cp_lexer_consume_token (parser->lexer);
30894 else
30895 break;
30898 /* Restore the saved message. */
30899 parser->type_definition_forbidden_message = saved_message;
30900 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30901 parser->colon_doesnt_start_class_def_p
30902 = saved_colon_doesnt_start_class_def_p;
30904 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30905 || types.is_empty ())
30907 fail:
30908 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30909 goto done;
30912 first_token = cp_lexer_peek_token (parser->lexer);
30913 cp = NULL;
30914 errs = errorcount;
30915 FOR_EACH_VEC_ELT (types, i, type)
30917 tree fntype
30918 = build_function_type_list (void_type_node,
30919 cp_build_reference_type (type, false),
30920 NULL_TREE);
30921 tree this_reduc_id = reduc_id;
30922 if (!dependent_type_p (type))
30923 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30924 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30925 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30926 DECL_ARTIFICIAL (fndecl) = 1;
30927 DECL_EXTERNAL (fndecl) = 1;
30928 DECL_DECLARED_INLINE_P (fndecl) = 1;
30929 DECL_IGNORED_P (fndecl) = 1;
30930 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30931 DECL_ATTRIBUTES (fndecl)
30932 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30933 DECL_ATTRIBUTES (fndecl));
30934 if (processing_template_decl)
30935 fndecl = push_template_decl (fndecl);
30936 bool block_scope = false;
30937 tree block = NULL_TREE;
30938 if (current_function_decl)
30940 block_scope = true;
30941 DECL_CONTEXT (fndecl) = global_namespace;
30942 if (!processing_template_decl)
30943 pushdecl (fndecl);
30945 else if (current_class_type)
30947 if (cp == NULL)
30949 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30950 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30951 cp_lexer_consume_token (parser->lexer);
30952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30953 goto fail;
30954 cp = cp_token_cache_new (first_token,
30955 cp_lexer_peek_nth_token (parser->lexer,
30956 2));
30958 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30959 finish_member_declaration (fndecl);
30960 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30961 DECL_PENDING_INLINE_P (fndecl) = 1;
30962 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30963 continue;
30965 else
30967 DECL_CONTEXT (fndecl) = current_namespace;
30968 pushdecl (fndecl);
30970 if (!block_scope)
30971 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30972 else
30973 block = begin_omp_structured_block ();
30974 if (cp)
30976 cp_parser_push_lexer_for_tokens (parser, cp);
30977 parser->lexer->in_pragma = true;
30979 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30981 if (!block_scope)
30982 finish_function (0);
30983 else
30984 DECL_CONTEXT (fndecl) = current_function_decl;
30985 if (cp)
30986 cp_parser_pop_lexer (parser);
30987 goto fail;
30989 if (cp)
30990 cp_parser_pop_lexer (parser);
30991 if (!block_scope)
30992 finish_function (0);
30993 else
30995 DECL_CONTEXT (fndecl) = current_function_decl;
30996 block = finish_omp_structured_block (block);
30997 if (TREE_CODE (block) == BIND_EXPR)
30998 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30999 else if (TREE_CODE (block) == STATEMENT_LIST)
31000 DECL_SAVED_TREE (fndecl) = block;
31001 if (processing_template_decl)
31002 add_decl_expr (fndecl);
31004 cp_check_omp_declare_reduction (fndecl);
31005 if (cp == NULL && types.length () > 1)
31006 cp = cp_token_cache_new (first_token,
31007 cp_lexer_peek_nth_token (parser->lexer, 2));
31008 if (errs != errorcount)
31009 break;
31012 cp_parser_require_pragma_eol (parser, pragma_tok);
31014 done:
31015 /* Free any declarators allocated. */
31016 obstack_free (&declarator_obstack, p);
31019 /* OpenMP 4.0
31020 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31021 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31022 initializer-clause[opt] new-line
31023 #pragma omp declare target new-line */
31025 static void
31026 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31027 enum pragma_context context)
31029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31031 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31032 const char *p = IDENTIFIER_POINTER (id);
31034 if (strcmp (p, "simd") == 0)
31036 cp_lexer_consume_token (parser->lexer);
31037 cp_parser_omp_declare_simd (parser, pragma_tok,
31038 context);
31039 return;
31041 cp_ensure_no_omp_declare_simd (parser);
31042 if (strcmp (p, "reduction") == 0)
31044 cp_lexer_consume_token (parser->lexer);
31045 cp_parser_omp_declare_reduction (parser, pragma_tok,
31046 context);
31047 return;
31049 if (!flag_openmp) /* flag_openmp_simd */
31051 cp_parser_require_pragma_eol (parser, pragma_tok);
31052 return;
31054 if (strcmp (p, "target") == 0)
31056 cp_lexer_consume_token (parser->lexer);
31057 cp_parser_omp_declare_target (parser, pragma_tok);
31058 return;
31061 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31062 "or %<target%>");
31063 cp_parser_require_pragma_eol (parser, pragma_tok);
31066 /* Main entry point to OpenMP statement pragmas. */
31068 static void
31069 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31071 tree stmt;
31072 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31073 omp_clause_mask mask (0);
31075 switch (pragma_tok->pragma_kind)
31077 case PRAGMA_OMP_ATOMIC:
31078 cp_parser_omp_atomic (parser, pragma_tok);
31079 return;
31080 case PRAGMA_OMP_CRITICAL:
31081 stmt = cp_parser_omp_critical (parser, pragma_tok);
31082 break;
31083 case PRAGMA_OMP_DISTRIBUTE:
31084 strcpy (p_name, "#pragma omp");
31085 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31086 break;
31087 case PRAGMA_OMP_FOR:
31088 strcpy (p_name, "#pragma omp");
31089 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31090 break;
31091 case PRAGMA_OMP_MASTER:
31092 stmt = cp_parser_omp_master (parser, pragma_tok);
31093 break;
31094 case PRAGMA_OMP_ORDERED:
31095 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31096 break;
31097 case PRAGMA_OMP_PARALLEL:
31098 strcpy (p_name, "#pragma omp");
31099 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31100 break;
31101 case PRAGMA_OMP_SECTIONS:
31102 strcpy (p_name, "#pragma omp");
31103 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31104 break;
31105 case PRAGMA_OMP_SIMD:
31106 strcpy (p_name, "#pragma omp");
31107 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31108 break;
31109 case PRAGMA_OMP_SINGLE:
31110 stmt = cp_parser_omp_single (parser, pragma_tok);
31111 break;
31112 case PRAGMA_OMP_TASK:
31113 stmt = cp_parser_omp_task (parser, pragma_tok);
31114 break;
31115 case PRAGMA_OMP_TASKGROUP:
31116 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31117 break;
31118 case PRAGMA_OMP_TEAMS:
31119 strcpy (p_name, "#pragma omp");
31120 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31121 break;
31122 default:
31123 gcc_unreachable ();
31126 if (stmt)
31127 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31130 /* Transactional Memory parsing routines. */
31132 /* Parse a transaction attribute.
31134 txn-attribute:
31135 attribute
31136 [ [ identifier ] ]
31138 ??? Simplify this when C++0x bracket attributes are
31139 implemented properly. */
31141 static tree
31142 cp_parser_txn_attribute_opt (cp_parser *parser)
31144 cp_token *token;
31145 tree attr_name, attr = NULL;
31147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31148 return cp_parser_attributes_opt (parser);
31150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31151 return NULL_TREE;
31152 cp_lexer_consume_token (parser->lexer);
31153 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31154 goto error1;
31156 token = cp_lexer_peek_token (parser->lexer);
31157 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31159 token = cp_lexer_consume_token (parser->lexer);
31161 attr_name = (token->type == CPP_KEYWORD
31162 /* For keywords, use the canonical spelling,
31163 not the parsed identifier. */
31164 ? ridpointers[(int) token->keyword]
31165 : token->u.value);
31166 attr = build_tree_list (attr_name, NULL_TREE);
31168 else
31169 cp_parser_error (parser, "expected identifier");
31171 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31172 error1:
31173 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31174 return attr;
31177 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31179 transaction-statement:
31180 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31181 compound-statement
31182 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31185 static tree
31186 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31188 unsigned char old_in = parser->in_transaction;
31189 unsigned char this_in = 1, new_in;
31190 cp_token *token;
31191 tree stmt, attrs, noex;
31193 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31194 || keyword == RID_TRANSACTION_RELAXED);
31195 token = cp_parser_require_keyword (parser, keyword,
31196 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31197 : RT_TRANSACTION_RELAXED));
31198 gcc_assert (token != NULL);
31200 if (keyword == RID_TRANSACTION_RELAXED)
31201 this_in |= TM_STMT_ATTR_RELAXED;
31202 else
31204 attrs = cp_parser_txn_attribute_opt (parser);
31205 if (attrs)
31206 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31209 /* Parse a noexcept specification. */
31210 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31212 /* Keep track if we're in the lexical scope of an outer transaction. */
31213 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31215 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31217 parser->in_transaction = new_in;
31218 cp_parser_compound_statement (parser, NULL, false, false);
31219 parser->in_transaction = old_in;
31221 finish_transaction_stmt (stmt, NULL, this_in, noex);
31223 return stmt;
31226 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31228 transaction-expression:
31229 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31230 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31233 static tree
31234 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31236 unsigned char old_in = parser->in_transaction;
31237 unsigned char this_in = 1;
31238 cp_token *token;
31239 tree expr, noex;
31240 bool noex_expr;
31242 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31243 || keyword == RID_TRANSACTION_RELAXED);
31245 if (!flag_tm)
31246 error (keyword == RID_TRANSACTION_RELAXED
31247 ? G_("%<__transaction_relaxed%> without transactional memory "
31248 "support enabled")
31249 : G_("%<__transaction_atomic%> without transactional memory "
31250 "support enabled"));
31252 token = cp_parser_require_keyword (parser, keyword,
31253 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31254 : RT_TRANSACTION_RELAXED));
31255 gcc_assert (token != NULL);
31257 if (keyword == RID_TRANSACTION_RELAXED)
31258 this_in |= TM_STMT_ATTR_RELAXED;
31260 /* Set this early. This might mean that we allow transaction_cancel in
31261 an expression that we find out later actually has to be a constexpr.
31262 However, we expect that cxx_constant_value will be able to deal with
31263 this; also, if the noexcept has no constexpr, then what we parse next
31264 really is a transaction's body. */
31265 parser->in_transaction = this_in;
31267 /* Parse a noexcept specification. */
31268 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31269 true);
31271 if (!noex || !noex_expr
31272 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31274 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31276 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31277 expr = finish_parenthesized_expr (expr);
31279 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31281 else
31283 /* The only expression that is available got parsed for the noexcept
31284 already. noexcept is true then. */
31285 expr = noex;
31286 noex = boolean_true_node;
31289 expr = build_transaction_expr (token->location, expr, this_in, noex);
31290 parser->in_transaction = old_in;
31292 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31293 return error_mark_node;
31295 return (flag_tm ? expr : error_mark_node);
31298 /* Parse a function-transaction-block.
31300 function-transaction-block:
31301 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31302 function-body
31303 __transaction_atomic txn-attribute[opt] function-try-block
31304 __transaction_relaxed ctor-initializer[opt] function-body
31305 __transaction_relaxed function-try-block
31308 static bool
31309 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31311 unsigned char old_in = parser->in_transaction;
31312 unsigned char new_in = 1;
31313 tree compound_stmt, stmt, attrs;
31314 bool ctor_initializer_p;
31315 cp_token *token;
31317 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31318 || keyword == RID_TRANSACTION_RELAXED);
31319 token = cp_parser_require_keyword (parser, keyword,
31320 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31321 : RT_TRANSACTION_RELAXED));
31322 gcc_assert (token != NULL);
31324 if (keyword == RID_TRANSACTION_RELAXED)
31325 new_in |= TM_STMT_ATTR_RELAXED;
31326 else
31328 attrs = cp_parser_txn_attribute_opt (parser);
31329 if (attrs)
31330 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31333 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31335 parser->in_transaction = new_in;
31337 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31338 ctor_initializer_p = cp_parser_function_try_block (parser);
31339 else
31340 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31341 (parser, /*in_function_try_block=*/false);
31343 parser->in_transaction = old_in;
31345 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31347 return ctor_initializer_p;
31350 /* Parse a __transaction_cancel statement.
31352 cancel-statement:
31353 __transaction_cancel txn-attribute[opt] ;
31354 __transaction_cancel txn-attribute[opt] throw-expression ;
31356 ??? Cancel and throw is not yet implemented. */
31358 static tree
31359 cp_parser_transaction_cancel (cp_parser *parser)
31361 cp_token *token;
31362 bool is_outer = false;
31363 tree stmt, attrs;
31365 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31366 RT_TRANSACTION_CANCEL);
31367 gcc_assert (token != NULL);
31369 attrs = cp_parser_txn_attribute_opt (parser);
31370 if (attrs)
31371 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31373 /* ??? Parse cancel-and-throw here. */
31375 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31377 if (!flag_tm)
31379 error_at (token->location, "%<__transaction_cancel%> without "
31380 "transactional memory support enabled");
31381 return error_mark_node;
31383 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31385 error_at (token->location, "%<__transaction_cancel%> within a "
31386 "%<__transaction_relaxed%>");
31387 return error_mark_node;
31389 else if (is_outer)
31391 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31392 && !is_tm_may_cancel_outer (current_function_decl))
31394 error_at (token->location, "outer %<__transaction_cancel%> not "
31395 "within outer %<__transaction_atomic%>");
31396 error_at (token->location,
31397 " or a %<transaction_may_cancel_outer%> function");
31398 return error_mark_node;
31401 else if (parser->in_transaction == 0)
31403 error_at (token->location, "%<__transaction_cancel%> not within "
31404 "%<__transaction_atomic%>");
31405 return error_mark_node;
31408 stmt = build_tm_abort_call (token->location, is_outer);
31409 add_stmt (stmt);
31411 return stmt;
31414 /* The parser. */
31416 static GTY (()) cp_parser *the_parser;
31419 /* Special handling for the first token or line in the file. The first
31420 thing in the file might be #pragma GCC pch_preprocess, which loads a
31421 PCH file, which is a GC collection point. So we need to handle this
31422 first pragma without benefit of an existing lexer structure.
31424 Always returns one token to the caller in *FIRST_TOKEN. This is
31425 either the true first token of the file, or the first token after
31426 the initial pragma. */
31428 static void
31429 cp_parser_initial_pragma (cp_token *first_token)
31431 tree name = NULL;
31433 cp_lexer_get_preprocessor_token (NULL, first_token);
31434 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31435 return;
31437 cp_lexer_get_preprocessor_token (NULL, first_token);
31438 if (first_token->type == CPP_STRING)
31440 name = first_token->u.value;
31442 cp_lexer_get_preprocessor_token (NULL, first_token);
31443 if (first_token->type != CPP_PRAGMA_EOL)
31444 error_at (first_token->location,
31445 "junk at end of %<#pragma GCC pch_preprocess%>");
31447 else
31448 error_at (first_token->location, "expected string literal");
31450 /* Skip to the end of the pragma. */
31451 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31452 cp_lexer_get_preprocessor_token (NULL, first_token);
31454 /* Now actually load the PCH file. */
31455 if (name)
31456 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31458 /* Read one more token to return to our caller. We have to do this
31459 after reading the PCH file in, since its pointers have to be
31460 live. */
31461 cp_lexer_get_preprocessor_token (NULL, first_token);
31464 /* Normal parsing of a pragma token. Here we can (and must) use the
31465 regular lexer. */
31467 static bool
31468 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31470 cp_token *pragma_tok;
31471 unsigned int id;
31473 pragma_tok = cp_lexer_consume_token (parser->lexer);
31474 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31475 parser->lexer->in_pragma = true;
31477 id = pragma_tok->pragma_kind;
31478 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31479 cp_ensure_no_omp_declare_simd (parser);
31480 switch (id)
31482 case PRAGMA_GCC_PCH_PREPROCESS:
31483 error_at (pragma_tok->location,
31484 "%<#pragma GCC pch_preprocess%> must be first");
31485 break;
31487 case PRAGMA_OMP_BARRIER:
31488 switch (context)
31490 case pragma_compound:
31491 cp_parser_omp_barrier (parser, pragma_tok);
31492 return false;
31493 case pragma_stmt:
31494 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31495 "used in compound statements");
31496 break;
31497 default:
31498 goto bad_stmt;
31500 break;
31502 case PRAGMA_OMP_FLUSH:
31503 switch (context)
31505 case pragma_compound:
31506 cp_parser_omp_flush (parser, pragma_tok);
31507 return false;
31508 case pragma_stmt:
31509 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31510 "used in compound statements");
31511 break;
31512 default:
31513 goto bad_stmt;
31515 break;
31517 case PRAGMA_OMP_TASKWAIT:
31518 switch (context)
31520 case pragma_compound:
31521 cp_parser_omp_taskwait (parser, pragma_tok);
31522 return false;
31523 case pragma_stmt:
31524 error_at (pragma_tok->location,
31525 "%<#pragma omp taskwait%> may only be "
31526 "used in compound statements");
31527 break;
31528 default:
31529 goto bad_stmt;
31531 break;
31533 case PRAGMA_OMP_TASKYIELD:
31534 switch (context)
31536 case pragma_compound:
31537 cp_parser_omp_taskyield (parser, pragma_tok);
31538 return false;
31539 case pragma_stmt:
31540 error_at (pragma_tok->location,
31541 "%<#pragma omp taskyield%> may only be "
31542 "used in compound statements");
31543 break;
31544 default:
31545 goto bad_stmt;
31547 break;
31549 case PRAGMA_OMP_CANCEL:
31550 switch (context)
31552 case pragma_compound:
31553 cp_parser_omp_cancel (parser, pragma_tok);
31554 return false;
31555 case pragma_stmt:
31556 error_at (pragma_tok->location,
31557 "%<#pragma omp cancel%> may only be "
31558 "used in compound statements");
31559 break;
31560 default:
31561 goto bad_stmt;
31563 break;
31565 case PRAGMA_OMP_CANCELLATION_POINT:
31566 switch (context)
31568 case pragma_compound:
31569 cp_parser_omp_cancellation_point (parser, pragma_tok);
31570 return false;
31571 case pragma_stmt:
31572 error_at (pragma_tok->location,
31573 "%<#pragma omp cancellation point%> may only be "
31574 "used in compound statements");
31575 break;
31576 default:
31577 goto bad_stmt;
31579 break;
31581 case PRAGMA_OMP_THREADPRIVATE:
31582 cp_parser_omp_threadprivate (parser, pragma_tok);
31583 return false;
31585 case PRAGMA_OMP_DECLARE_REDUCTION:
31586 cp_parser_omp_declare (parser, pragma_tok, context);
31587 return false;
31589 case PRAGMA_OMP_ATOMIC:
31590 case PRAGMA_OMP_CRITICAL:
31591 case PRAGMA_OMP_DISTRIBUTE:
31592 case PRAGMA_OMP_FOR:
31593 case PRAGMA_OMP_MASTER:
31594 case PRAGMA_OMP_ORDERED:
31595 case PRAGMA_OMP_PARALLEL:
31596 case PRAGMA_OMP_SECTIONS:
31597 case PRAGMA_OMP_SIMD:
31598 case PRAGMA_OMP_SINGLE:
31599 case PRAGMA_OMP_TASK:
31600 case PRAGMA_OMP_TASKGROUP:
31601 case PRAGMA_OMP_TEAMS:
31602 if (context != pragma_stmt && context != pragma_compound)
31603 goto bad_stmt;
31604 cp_parser_omp_construct (parser, pragma_tok);
31605 return true;
31607 case PRAGMA_OMP_TARGET:
31608 return cp_parser_omp_target (parser, pragma_tok, context);
31610 case PRAGMA_OMP_END_DECLARE_TARGET:
31611 cp_parser_omp_end_declare_target (parser, pragma_tok);
31612 return false;
31614 case PRAGMA_OMP_SECTION:
31615 error_at (pragma_tok->location,
31616 "%<#pragma omp section%> may only be used in "
31617 "%<#pragma omp sections%> construct");
31618 break;
31620 case PRAGMA_IVDEP:
31622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31623 cp_token *tok;
31624 tok = cp_lexer_peek_token (the_parser->lexer);
31625 if (tok->type != CPP_KEYWORD
31626 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31627 && tok->keyword != RID_DO))
31629 cp_parser_error (parser, "for, while or do statement expected");
31630 return false;
31632 cp_parser_iteration_statement (parser, true);
31633 return true;
31636 case PRAGMA_CILK_SIMD:
31637 if (context == pragma_external)
31639 error_at (pragma_tok->location,
31640 "%<#pragma simd%> must be inside a function");
31641 break;
31643 cp_parser_cilk_simd (parser, pragma_tok);
31644 return true;
31646 default:
31647 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31648 c_invoke_pragma_handler (id);
31649 break;
31651 bad_stmt:
31652 cp_parser_error (parser, "expected declaration specifiers");
31653 break;
31656 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31657 return false;
31660 /* The interface the pragma parsers have to the lexer. */
31662 enum cpp_ttype
31663 pragma_lex (tree *value)
31665 cp_token *tok;
31666 enum cpp_ttype ret;
31668 tok = cp_lexer_peek_token (the_parser->lexer);
31670 ret = tok->type;
31671 *value = tok->u.value;
31673 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31674 ret = CPP_EOF;
31675 else if (ret == CPP_STRING)
31676 *value = cp_parser_string_literal (the_parser, false, false);
31677 else
31679 cp_lexer_consume_token (the_parser->lexer);
31680 if (ret == CPP_KEYWORD)
31681 ret = CPP_NAME;
31684 return ret;
31688 /* External interface. */
31690 /* Parse one entire translation unit. */
31692 void
31693 c_parse_file (void)
31695 static bool already_called = false;
31697 if (already_called)
31699 sorry ("inter-module optimizations not implemented for C++");
31700 return;
31702 already_called = true;
31704 the_parser = cp_parser_new ();
31705 push_deferring_access_checks (flag_access_control
31706 ? dk_no_deferred : dk_no_check);
31707 cp_parser_translation_unit (the_parser);
31708 the_parser = NULL;
31711 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31712 vectorlength clause:
31713 Syntax:
31714 vectorlength ( constant-expression ) */
31716 static tree
31717 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31718 bool is_simd_fn)
31720 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31721 tree expr;
31722 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31723 safelen clause. Thus, vectorlength is represented as OMP 4.0
31724 safelen. For SIMD-enabled function it is represented by OMP 4.0
31725 simdlen. */
31726 if (!is_simd_fn)
31727 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31728 loc);
31729 else
31730 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31731 loc);
31733 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31734 return error_mark_node;
31736 expr = cp_parser_constant_expression (parser, false, NULL);
31737 expr = maybe_constant_value (expr);
31739 /* If expr == error_mark_node, then don't emit any errors nor
31740 create a clause. if any of the above functions returns
31741 error mark node then they would have emitted an error message. */
31742 if (expr == error_mark_node)
31744 else if (!TREE_TYPE (expr)
31745 || !TREE_CONSTANT (expr)
31746 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31747 error_at (loc, "vectorlength must be an integer constant");
31748 else if (TREE_CONSTANT (expr)
31749 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31750 error_at (loc, "vectorlength must be a power of 2");
31751 else
31753 tree c;
31754 if (!is_simd_fn)
31756 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31757 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31758 OMP_CLAUSE_CHAIN (c) = clauses;
31759 clauses = c;
31761 else
31763 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
31764 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
31765 OMP_CLAUSE_CHAIN (c) = clauses;
31766 clauses = c;
31770 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31771 return error_mark_node;
31772 return clauses;
31775 /* Handles the Cilk Plus #pragma simd linear clause.
31776 Syntax:
31777 linear ( simd-linear-variable-list )
31779 simd-linear-variable-list:
31780 simd-linear-variable
31781 simd-linear-variable-list , simd-linear-variable
31783 simd-linear-variable:
31784 id-expression
31785 id-expression : simd-linear-step
31787 simd-linear-step:
31788 conditional-expression */
31790 static tree
31791 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31793 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31795 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31796 return clauses;
31797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31799 cp_parser_error (parser, "expected identifier");
31800 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31801 return error_mark_node;
31804 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31805 parser->colon_corrects_to_scope_p = false;
31806 while (1)
31808 cp_token *token = cp_lexer_peek_token (parser->lexer);
31809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31811 cp_parser_error (parser, "expected variable-name");
31812 clauses = error_mark_node;
31813 break;
31816 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
31817 false, false);
31818 tree decl = cp_parser_lookup_name_simple (parser, var_name,
31819 token->location);
31820 if (decl == error_mark_node)
31822 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
31823 token->location);
31824 clauses = error_mark_node;
31826 else
31828 tree e = NULL_TREE;
31829 tree step_size = integer_one_node;
31831 /* If present, parse the linear step. Otherwise, assume the default
31832 value of 1. */
31833 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
31835 cp_lexer_consume_token (parser->lexer);
31837 e = cp_parser_assignment_expression (parser, false, NULL);
31838 e = maybe_constant_value (e);
31840 if (e == error_mark_node)
31842 /* If an error has occurred, then the whole pragma is
31843 considered ill-formed. Thus, no reason to keep
31844 parsing. */
31845 clauses = error_mark_node;
31846 break;
31848 else if (type_dependent_expression_p (e)
31849 || value_dependent_expression_p (e)
31850 || (TREE_TYPE (e)
31851 && INTEGRAL_TYPE_P (TREE_TYPE (e))
31852 && (TREE_CONSTANT (e)
31853 || DECL_P (e))))
31854 step_size = e;
31855 else
31856 cp_parser_error (parser,
31857 "step size must be an integer constant "
31858 "expression or an integer variable");
31861 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31862 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31863 OMP_CLAUSE_DECL (l) = decl;
31864 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
31865 OMP_CLAUSE_CHAIN (l) = clauses;
31866 clauses = l;
31868 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31869 cp_lexer_consume_token (parser->lexer);
31870 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31871 break;
31872 else
31874 error_at (cp_lexer_peek_token (parser->lexer)->location,
31875 "expected %<,%> or %<)%> after %qE", decl);
31876 clauses = error_mark_node;
31877 break;
31880 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31881 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31882 return clauses;
31885 /* Returns the name of the next clause. If the clause is not
31886 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31887 token is not consumed. Otherwise, the appropriate enum from the
31888 pragma_simd_clause is returned and the token is consumed. */
31890 static pragma_omp_clause
31891 cp_parser_cilk_simd_clause_name (cp_parser *parser)
31893 pragma_omp_clause clause_type;
31894 cp_token *token = cp_lexer_peek_token (parser->lexer);
31896 if (token->keyword == RID_PRIVATE)
31897 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
31898 else if (!token->u.value || token->type != CPP_NAME)
31899 return PRAGMA_CILK_CLAUSE_NONE;
31900 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
31901 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31902 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
31903 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
31904 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
31905 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
31906 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
31907 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
31908 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
31909 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
31910 else
31911 return PRAGMA_CILK_CLAUSE_NONE;
31913 cp_lexer_consume_token (parser->lexer);
31914 return clause_type;
31917 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31919 static tree
31920 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
31922 tree clauses = NULL_TREE;
31924 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31925 && clauses != error_mark_node)
31927 pragma_omp_clause c_kind;
31928 c_kind = cp_parser_cilk_simd_clause_name (parser);
31929 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
31930 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
31931 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
31932 clauses = cp_parser_cilk_simd_linear (parser, clauses);
31933 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
31934 /* Use the OpenMP 4.0 equivalent function. */
31935 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
31936 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
31937 /* Use the OpenMP 4.0 equivalent function. */
31938 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31939 clauses);
31940 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
31941 /* Use the OMP 4.0 equivalent function. */
31942 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31943 clauses);
31944 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
31945 /* Use the OMP 4.0 equivalent function. */
31946 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31947 else
31949 clauses = error_mark_node;
31950 cp_parser_error (parser, "expected %<#pragma simd%> clause");
31951 break;
31955 cp_parser_skip_to_pragma_eol (parser, pragma_token);
31957 if (clauses == error_mark_node)
31958 return error_mark_node;
31959 else
31960 return c_finish_cilk_clauses (clauses);
31963 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31965 static void
31966 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
31968 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
31970 if (clauses == error_mark_node)
31971 return;
31973 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
31975 error_at (cp_lexer_peek_token (parser->lexer)->location,
31976 "for statement expected");
31977 return;
31980 tree sb = begin_omp_structured_block ();
31981 int save = cp_parser_begin_omp_structured_block (parser);
31982 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
31983 if (ret)
31984 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
31985 cp_parser_end_omp_structured_block (parser, save);
31986 add_stmt (finish_omp_structured_block (sb));
31987 return;
31990 /* Create an identifier for a generic parameter type (a synthesized
31991 template parameter implied by `auto' or a concept identifier). */
31993 static GTY(()) int generic_parm_count;
31994 static tree
31995 make_generic_type_name ()
31997 char buf[32];
31998 sprintf (buf, "auto:%d", ++generic_parm_count);
31999 return get_identifier (buf);
32002 /* Predicate that behaves as is_auto_or_concept but matches the parent
32003 node of the generic type rather than the generic type itself. This
32004 allows for type transformation in add_implicit_template_parms. */
32006 static inline bool
32007 tree_type_is_auto_or_concept (const_tree t)
32009 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32012 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32013 (creating a new template parameter list if necessary). Returns the newly
32014 created template type parm. */
32016 tree
32017 synthesize_implicit_template_parm (cp_parser *parser)
32019 gcc_assert (current_binding_level->kind == sk_function_parms);
32021 /* We are either continuing a function template that already contains implicit
32022 template parameters, creating a new fully-implicit function template, or
32023 extending an existing explicit function template with implicit template
32024 parameters. */
32026 cp_binding_level *const entry_scope = current_binding_level;
32028 bool become_template = false;
32029 cp_binding_level *parent_scope = 0;
32031 if (parser->implicit_template_scope)
32033 gcc_assert (parser->implicit_template_parms);
32035 current_binding_level = parser->implicit_template_scope;
32037 else
32039 /* Roll back to the existing template parameter scope (in the case of
32040 extending an explicit function template) or introduce a new template
32041 parameter scope ahead of the function parameter scope (or class scope
32042 in the case of out-of-line member definitions). The function scope is
32043 added back after template parameter synthesis below. */
32045 cp_binding_level *scope = entry_scope;
32047 while (scope->kind == sk_function_parms)
32049 parent_scope = scope;
32050 scope = scope->level_chain;
32052 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32054 /* If not defining a class, then any class scope is a scope level in
32055 an out-of-line member definition. In this case simply wind back
32056 beyond the first such scope to inject the template parameter list.
32057 Otherwise wind back to the class being defined. The latter can
32058 occur in class member friend declarations such as:
32060 class A {
32061 void foo (auto);
32063 class B {
32064 friend void A::foo (auto);
32067 The template parameter list synthesized for the friend declaration
32068 must be injected in the scope of 'B'. This can also occur in
32069 erroneous cases such as:
32071 struct A {
32072 struct B {
32073 void foo (auto);
32075 void B::foo (auto) {}
32078 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32079 but, nevertheless, the template parameter list synthesized for the
32080 declarator should be injected into the scope of 'A' as if the
32081 ill-formed template was specified explicitly. */
32083 while (scope->kind == sk_class && !scope->defining_class_p)
32085 parent_scope = scope;
32086 scope = scope->level_chain;
32090 current_binding_level = scope;
32092 if (scope->kind != sk_template_parms
32093 || !function_being_declared_is_template_p (parser))
32095 /* Introduce a new template parameter list for implicit template
32096 parameters. */
32098 become_template = true;
32100 parser->implicit_template_scope
32101 = begin_scope (sk_template_parms, NULL);
32103 ++processing_template_decl;
32105 parser->fully_implicit_function_template_p = true;
32106 ++parser->num_template_parameter_lists;
32108 else
32110 /* Synthesize implicit template parameters at the end of the explicit
32111 template parameter list. */
32113 gcc_assert (current_template_parms);
32115 parser->implicit_template_scope = scope;
32117 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32118 parser->implicit_template_parms
32119 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32123 /* Synthesize a new template parameter and track the current template
32124 parameter chain with implicit_template_parms. */
32126 tree synth_id = make_generic_type_name ();
32127 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32128 synth_id);
32129 tree new_parm
32130 = process_template_parm (parser->implicit_template_parms,
32131 input_location,
32132 build_tree_list (NULL_TREE, synth_tmpl_parm),
32133 /*non_type=*/false,
32134 /*param_pack=*/false);
32137 if (parser->implicit_template_parms)
32138 parser->implicit_template_parms
32139 = TREE_CHAIN (parser->implicit_template_parms);
32140 else
32141 parser->implicit_template_parms = new_parm;
32143 tree new_type = TREE_TYPE (getdecls ());
32145 /* If creating a fully implicit function template, start the new implicit
32146 template parameter list with this synthesized type, otherwise grow the
32147 current template parameter list. */
32149 if (become_template)
32151 parent_scope->level_chain = current_binding_level;
32153 tree new_parms = make_tree_vec (1);
32154 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32155 current_template_parms = tree_cons (size_int (processing_template_decl),
32156 new_parms, current_template_parms);
32158 else
32160 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32161 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32162 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32163 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32166 current_binding_level = entry_scope;
32168 return new_type;
32171 /* Finish the declaration of a fully implicit function template. Such a
32172 template has no explicit template parameter list so has not been through the
32173 normal template head and tail processing. synthesize_implicit_template_parm
32174 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32175 provided if the declaration is a class member such that its template
32176 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32177 form is returned. Otherwise NULL_TREE is returned. */
32179 tree
32180 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32182 gcc_assert (parser->fully_implicit_function_template_p);
32184 if (member_decl_opt && member_decl_opt != error_mark_node
32185 && DECL_VIRTUAL_P (member_decl_opt))
32187 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32188 "implicit templates may not be %<virtual%>");
32189 DECL_VIRTUAL_P (member_decl_opt) = false;
32192 if (member_decl_opt)
32193 member_decl_opt = finish_member_template_decl (member_decl_opt);
32194 end_template_decl ();
32196 parser->fully_implicit_function_template_p = false;
32197 --parser->num_template_parameter_lists;
32199 return member_decl_opt;
32202 #include "gt-cp-parser.h"