2014-07-30 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / cp / parser.c
blob32c7a3fe1469531c18536e74ff52c890335bc757
1 /* C++ Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 } non_integral_constant;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 } name_lookup_error;
130 /* The various kinds of required token */
131 typedef enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_INTERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 } required_token;
186 /* Prototypes. */
188 static cp_lexer *cp_lexer_new_main
189 (void);
190 static cp_lexer *cp_lexer_new_from_tokens
191 (cp_token_cache *tokens);
192 static void cp_lexer_destroy
193 (cp_lexer *);
194 static int cp_lexer_saving_tokens
195 (const cp_lexer *);
196 static cp_token *cp_lexer_token_at
197 (cp_lexer *, cp_token_position);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer *, cp_token *);
200 static inline cp_token *cp_lexer_peek_token
201 (cp_lexer *);
202 static cp_token *cp_lexer_peek_nth_token
203 (cp_lexer *, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer *, enum cpp_ttype);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer *, enum cpp_ttype);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer *, enum rid);
210 static cp_token *cp_lexer_consume_token
211 (cp_lexer *);
212 static void cp_lexer_purge_token
213 (cp_lexer *);
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_save_tokens
217 (cp_lexer *);
218 static void cp_lexer_commit_tokens
219 (cp_lexer *);
220 static void cp_lexer_rollback_tokens
221 (cp_lexer *);
222 static void cp_lexer_print_token
223 (FILE *, cp_token *);
224 static inline bool cp_lexer_debugging_p
225 (cp_lexer *);
226 static void cp_lexer_start_debugging
227 (cp_lexer *) ATTRIBUTE_UNUSED;
228 static void cp_lexer_stop_debugging
229 (cp_lexer *) ATTRIBUTE_UNUSED;
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
234 static void cp_parser_initial_pragma
235 (cp_token *);
237 static tree cp_literal_operator_id
238 (const char *);
240 static void cp_parser_cilk_simd
241 (cp_parser *, cp_token *);
242 static bool cp_parser_omp_declare_reduction_exprs
243 (tree, cp_parser *);
244 static tree cp_parser_cilk_simd_vectorlength
245 (cp_parser *, tree, bool);
247 /* Manifest constants. */
248 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
249 #define CP_SAVED_TOKEN_STACK 5
251 /* Variables. */
253 /* The stream to which debugging output should be written. */
254 static FILE *cp_lexer_debug_stream;
256 /* Nonzero if we are parsing an unevaluated operand: an operand to
257 sizeof, typeof, or alignof. */
258 int cp_unevaluated_operand;
260 /* Dump up to NUM tokens in BUFFER to FILE starting with token
261 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
262 first token in BUFFER. If NUM is 0, dump all the tokens. If
263 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
264 highlighted by surrounding it in [[ ]]. */
266 static void
267 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
268 cp_token *start_token, unsigned num,
269 cp_token *curr_token)
271 unsigned i, nprinted;
272 cp_token *token;
273 bool do_print;
275 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
277 if (buffer == NULL)
278 return;
280 if (num == 0)
281 num = buffer->length ();
283 if (start_token == NULL)
284 start_token = buffer->address ();
286 if (start_token > buffer->address ())
288 cp_lexer_print_token (file, &(*buffer)[0]);
289 fprintf (file, " ... ");
292 do_print = false;
293 nprinted = 0;
294 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
296 if (token == start_token)
297 do_print = true;
299 if (!do_print)
300 continue;
302 nprinted++;
303 if (token == curr_token)
304 fprintf (file, "[[");
306 cp_lexer_print_token (file, token);
308 if (token == curr_token)
309 fprintf (file, "]]");
311 switch (token->type)
313 case CPP_SEMICOLON:
314 case CPP_OPEN_BRACE:
315 case CPP_CLOSE_BRACE:
316 case CPP_EOF:
317 fputc ('\n', file);
318 break;
320 default:
321 fputc (' ', file);
325 if (i == num && i < buffer->length ())
327 fprintf (file, " ... ");
328 cp_lexer_print_token (file, &buffer->last ());
331 fprintf (file, "\n");
335 /* Dump all tokens in BUFFER to stderr. */
337 void
338 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
340 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
343 DEBUG_FUNCTION void
344 debug (vec<cp_token, va_gc> &ref)
346 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
349 DEBUG_FUNCTION void
350 debug (vec<cp_token, va_gc> *ptr)
352 if (ptr)
353 debug (*ptr);
354 else
355 fprintf (stderr, "<nil>\n");
359 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
360 description for T. */
362 static void
363 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
365 if (t)
367 fprintf (file, "%s: ", desc);
368 print_node_brief (file, "", t, 0);
373 /* Dump parser context C to FILE. */
375 static void
376 cp_debug_print_context (FILE *file, cp_parser_context *c)
378 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
379 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
380 print_node_brief (file, "", c->object_type, 0);
381 fprintf (file, "}\n");
385 /* Print the stack of parsing contexts to FILE starting with FIRST. */
387 static void
388 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
390 unsigned i;
391 cp_parser_context *c;
393 fprintf (file, "Parsing context stack:\n");
394 for (i = 0, c = first; c; c = c->next, i++)
396 fprintf (file, "\t#%u: ", i);
397 cp_debug_print_context (file, c);
402 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
404 static void
405 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
407 if (flag)
408 fprintf (file, "%s: true\n", desc);
412 /* Print an unparsed function entry UF to FILE. */
414 static void
415 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
417 unsigned i;
418 cp_default_arg_entry *default_arg_fn;
419 tree fn;
421 fprintf (file, "\tFunctions with default args:\n");
422 for (i = 0;
423 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
424 i++)
426 fprintf (file, "\t\tClass type: ");
427 print_node_brief (file, "", default_arg_fn->class_type, 0);
428 fprintf (file, "\t\tDeclaration: ");
429 print_node_brief (file, "", default_arg_fn->decl, 0);
430 fprintf (file, "\n");
433 fprintf (file, "\n\tFunctions with definitions that require "
434 "post-processing\n\t\t");
435 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
437 print_node_brief (file, "", fn, 0);
438 fprintf (file, " ");
440 fprintf (file, "\n");
442 fprintf (file, "\n\tNon-static data members with initializers that require "
443 "post-processing\n\t\t");
444 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
446 print_node_brief (file, "", fn, 0);
447 fprintf (file, " ");
449 fprintf (file, "\n");
453 /* Print the stack of unparsed member functions S to FILE. */
455 static void
456 cp_debug_print_unparsed_queues (FILE *file,
457 vec<cp_unparsed_functions_entry, va_gc> *s)
459 unsigned i;
460 cp_unparsed_functions_entry *uf;
462 fprintf (file, "Unparsed functions\n");
463 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
465 fprintf (file, "#%u:\n", i);
466 cp_debug_print_unparsed_function (file, uf);
471 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
472 the given PARSER. If FILE is NULL, the output is printed on stderr. */
474 static void
475 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
477 cp_token *next_token, *first_token, *start_token;
479 if (file == NULL)
480 file = stderr;
482 next_token = parser->lexer->next_token;
483 first_token = parser->lexer->buffer->address ();
484 start_token = (next_token > first_token + window_size / 2)
485 ? next_token - window_size / 2
486 : first_token;
487 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
488 next_token);
492 /* Dump debugging information for the given PARSER. If FILE is NULL,
493 the output is printed on stderr. */
495 void
496 cp_debug_parser (FILE *file, cp_parser *parser)
498 const size_t window_size = 20;
499 cp_token *token;
500 expanded_location eloc;
502 if (file == NULL)
503 file = stderr;
505 fprintf (file, "Parser state\n\n");
506 fprintf (file, "Number of tokens: %u\n",
507 vec_safe_length (parser->lexer->buffer));
508 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
509 cp_debug_print_tree_if_set (file, "Object scope",
510 parser->object_scope);
511 cp_debug_print_tree_if_set (file, "Qualifying scope",
512 parser->qualifying_scope);
513 cp_debug_print_context_stack (file, parser->context);
514 cp_debug_print_flag (file, "Allow GNU extensions",
515 parser->allow_gnu_extensions_p);
516 cp_debug_print_flag (file, "'>' token is greater-than",
517 parser->greater_than_is_operator_p);
518 cp_debug_print_flag (file, "Default args allowed in current "
519 "parameter list", parser->default_arg_ok_p);
520 cp_debug_print_flag (file, "Parsing integral constant-expression",
521 parser->integral_constant_expression_p);
522 cp_debug_print_flag (file, "Allow non-constant expression in current "
523 "constant-expression",
524 parser->allow_non_integral_constant_expression_p);
525 cp_debug_print_flag (file, "Seen non-constant expression",
526 parser->non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
528 "current context",
529 parser->local_variables_forbidden_p);
530 cp_debug_print_flag (file, "In unbraced linkage specification",
531 parser->in_unbraced_linkage_specification_p);
532 cp_debug_print_flag (file, "Parsing a declarator",
533 parser->in_declarator_p);
534 cp_debug_print_flag (file, "In template argument list",
535 parser->in_template_argument_list_p);
536 cp_debug_print_flag (file, "Parsing an iteration statement",
537 parser->in_statement & IN_ITERATION_STMT);
538 cp_debug_print_flag (file, "Parsing a switch statement",
539 parser->in_statement & IN_SWITCH_STMT);
540 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
541 parser->in_statement & IN_OMP_BLOCK);
542 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
543 parser->in_statement & IN_CILK_SIMD_FOR);
544 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
545 parser->in_statement & IN_OMP_FOR);
546 cp_debug_print_flag (file, "Parsing an if statement",
547 parser->in_statement & IN_IF_STMT);
548 cp_debug_print_flag (file, "Parsing a type-id in an expression "
549 "context", parser->in_type_id_in_expr_p);
550 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
551 parser->implicit_extern_c);
552 cp_debug_print_flag (file, "String expressions should be translated "
553 "to execution character set",
554 parser->translate_strings_p);
555 cp_debug_print_flag (file, "Parsing function body outside of a "
556 "local class", parser->in_function_body);
557 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
558 parser->colon_corrects_to_scope_p);
559 cp_debug_print_flag (file, "Colon doesn't start a class definition",
560 parser->colon_doesnt_start_class_def_p);
561 if (parser->type_definition_forbidden_message)
562 fprintf (file, "Error message for forbidden type definitions: %s\n",
563 parser->type_definition_forbidden_message);
564 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
565 fprintf (file, "Number of class definitions in progress: %u\n",
566 parser->num_classes_being_defined);
567 fprintf (file, "Number of template parameter lists for the current "
568 "declaration: %u\n", parser->num_template_parameter_lists);
569 cp_debug_parser_tokens (file, parser, window_size);
570 token = parser->lexer->next_token;
571 fprintf (file, "Next token to parse:\n");
572 fprintf (file, "\tToken: ");
573 cp_lexer_print_token (file, token);
574 eloc = expand_location (token->location);
575 fprintf (file, "\n\tFile: %s\n", eloc.file);
576 fprintf (file, "\tLine: %d\n", eloc.line);
577 fprintf (file, "\tColumn: %d\n", eloc.column);
580 DEBUG_FUNCTION void
581 debug (cp_parser &ref)
583 cp_debug_parser (stderr, &ref);
586 DEBUG_FUNCTION void
587 debug (cp_parser *ptr)
589 if (ptr)
590 debug (*ptr);
591 else
592 fprintf (stderr, "<nil>\n");
595 /* Allocate memory for a new lexer object and return it. */
597 static cp_lexer *
598 cp_lexer_alloc (void)
600 cp_lexer *lexer;
602 c_common_no_more_pch ();
604 /* Allocate the memory. */
605 lexer = ggc_cleared_alloc<cp_lexer> ();
607 /* Initially we are not debugging. */
608 lexer->debugging_p = false;
610 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
612 /* Create the buffer. */
613 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
615 return lexer;
619 /* Create a new main C++ lexer, the lexer that gets tokens from the
620 preprocessor. */
622 static cp_lexer *
623 cp_lexer_new_main (void)
625 cp_lexer *lexer;
626 cp_token token;
628 /* It's possible that parsing the first pragma will load a PCH file,
629 which is a GC collection point. So we have to do that before
630 allocating any memory. */
631 cp_parser_initial_pragma (&token);
633 lexer = cp_lexer_alloc ();
635 /* Put the first token in the buffer. */
636 lexer->buffer->quick_push (token);
638 /* Get the remaining tokens from the preprocessor. */
639 while (token.type != CPP_EOF)
641 cp_lexer_get_preprocessor_token (lexer, &token);
642 vec_safe_push (lexer->buffer, token);
645 lexer->last_token = lexer->buffer->address ()
646 + lexer->buffer->length ()
647 - 1;
648 lexer->next_token = lexer->buffer->length ()
649 ? lexer->buffer->address ()
650 : &eof_token;
652 /* Subsequent preprocessor diagnostics should use compiler
653 diagnostic functions to get the compiler source location. */
654 done_lexing = true;
656 gcc_assert (!lexer->next_token->purged_p);
657 return lexer;
660 /* Create a new lexer whose token stream is primed with the tokens in
661 CACHE. When these tokens are exhausted, no new tokens will be read. */
663 static cp_lexer *
664 cp_lexer_new_from_tokens (cp_token_cache *cache)
666 cp_token *first = cache->first;
667 cp_token *last = cache->last;
668 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
670 /* We do not own the buffer. */
671 lexer->buffer = NULL;
672 lexer->next_token = first == last ? &eof_token : first;
673 lexer->last_token = last;
675 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
677 /* Initially we are not debugging. */
678 lexer->debugging_p = false;
680 gcc_assert (!lexer->next_token->purged_p);
681 return lexer;
684 /* Frees all resources associated with LEXER. */
686 static void
687 cp_lexer_destroy (cp_lexer *lexer)
689 vec_free (lexer->buffer);
690 lexer->saved_tokens.release ();
691 ggc_free (lexer);
694 /* Returns nonzero if debugging information should be output. */
696 static inline bool
697 cp_lexer_debugging_p (cp_lexer *lexer)
699 return lexer->debugging_p;
703 static inline cp_token_position
704 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
706 gcc_assert (!previous_p || lexer->next_token != &eof_token);
708 return lexer->next_token - previous_p;
711 static inline cp_token *
712 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
714 return pos;
717 static inline void
718 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
720 lexer->next_token = cp_lexer_token_at (lexer, pos);
723 static inline cp_token_position
724 cp_lexer_previous_token_position (cp_lexer *lexer)
726 if (lexer->next_token == &eof_token)
727 return lexer->last_token - 1;
728 else
729 return cp_lexer_token_position (lexer, true);
732 static inline cp_token *
733 cp_lexer_previous_token (cp_lexer *lexer)
735 cp_token_position tp = cp_lexer_previous_token_position (lexer);
737 return cp_lexer_token_at (lexer, tp);
740 /* nonzero if we are presently saving tokens. */
742 static inline int
743 cp_lexer_saving_tokens (const cp_lexer* lexer)
745 return lexer->saved_tokens.length () != 0;
748 /* Store the next token from the preprocessor in *TOKEN. Return true
749 if we reach EOF. If LEXER is NULL, assume we are handling an
750 initial #pragma pch_preprocess, and thus want the lexer to return
751 processed strings. */
753 static void
754 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
756 static int is_extern_c = 0;
758 /* Get a new token from the preprocessor. */
759 token->type
760 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
761 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
762 token->keyword = RID_MAX;
763 token->pragma_kind = PRAGMA_NONE;
764 token->purged_p = false;
765 token->error_reported = false;
767 /* On some systems, some header files are surrounded by an
768 implicit extern "C" block. Set a flag in the token if it
769 comes from such a header. */
770 is_extern_c += pending_lang_change;
771 pending_lang_change = 0;
772 token->implicit_extern_c = is_extern_c > 0;
774 /* Check to see if this token is a keyword. */
775 if (token->type == CPP_NAME)
777 if (C_IS_RESERVED_WORD (token->u.value))
779 /* Mark this token as a keyword. */
780 token->type = CPP_KEYWORD;
781 /* Record which keyword. */
782 token->keyword = C_RID_CODE (token->u.value);
784 else
786 if (warn_cxx0x_compat
787 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
788 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
790 /* Warn about the C++0x keyword (but still treat it as
791 an identifier). */
792 warning (OPT_Wc__0x_compat,
793 "identifier %qE is a keyword in C++11",
794 token->u.value);
796 /* Clear out the C_RID_CODE so we don't warn about this
797 particular identifier-turned-keyword again. */
798 C_SET_RID_CODE (token->u.value, RID_MAX);
801 token->keyword = RID_MAX;
804 else if (token->type == CPP_AT_NAME)
806 /* This only happens in Objective-C++; it must be a keyword. */
807 token->type = CPP_KEYWORD;
808 switch (C_RID_CODE (token->u.value))
810 /* Replace 'class' with '@class', 'private' with '@private',
811 etc. This prevents confusion with the C++ keyword
812 'class', and makes the tokens consistent with other
813 Objective-C 'AT' keywords. For example '@class' is
814 reported as RID_AT_CLASS which is consistent with
815 '@synchronized', which is reported as
816 RID_AT_SYNCHRONIZED.
818 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
819 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
820 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
821 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
822 case RID_THROW: token->keyword = RID_AT_THROW; break;
823 case RID_TRY: token->keyword = RID_AT_TRY; break;
824 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
825 default: token->keyword = C_RID_CODE (token->u.value);
828 else if (token->type == CPP_PRAGMA)
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token->pragma_kind = ((enum pragma_kind)
832 TREE_INT_CST_LOW (token->u.value));
833 token->u.value = NULL_TREE;
837 /* Update the globals input_location and the input file stack from TOKEN. */
838 static inline void
839 cp_lexer_set_source_position_from_token (cp_token *token)
841 if (token->type != CPP_EOF)
843 input_location = token->location;
847 /* Update the globals input_location and the input file stack from LEXER. */
848 static inline void
849 cp_lexer_set_source_position (cp_lexer *lexer)
851 cp_token *token = cp_lexer_peek_token (lexer);
852 cp_lexer_set_source_position_from_token (token);
855 /* Return a pointer to the next token in the token stream, but do not
856 consume it. */
858 static inline cp_token *
859 cp_lexer_peek_token (cp_lexer *lexer)
861 if (cp_lexer_debugging_p (lexer))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865 putc ('\n', cp_lexer_debug_stream);
867 return lexer->next_token;
870 /* Return true if the next token has the indicated TYPE. */
872 static inline bool
873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
875 return cp_lexer_peek_token (lexer)->type == type;
878 /* Return true if the next token does not have the indicated TYPE. */
880 static inline bool
881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
883 return !cp_lexer_next_token_is (lexer, type);
886 /* Return true if the next token is the indicated KEYWORD. */
888 static inline bool
889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
891 return cp_lexer_peek_token (lexer)->keyword == keyword;
894 static inline bool
895 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
897 return cp_lexer_peek_nth_token (lexer, n)->type == type;
900 static inline bool
901 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
903 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
906 /* Return true if the next token is not the indicated KEYWORD. */
908 static inline bool
909 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
911 return cp_lexer_peek_token (lexer)->keyword != keyword;
914 /* Return true if the next token is a keyword for a decl-specifier. */
916 static bool
917 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
919 cp_token *token;
921 token = cp_lexer_peek_token (lexer);
922 switch (token->keyword)
924 /* auto specifier: storage-class-specifier in C++,
925 simple-type-specifier in C++0x. */
926 case RID_AUTO:
927 /* Storage classes. */
928 case RID_REGISTER:
929 case RID_STATIC:
930 case RID_EXTERN:
931 case RID_MUTABLE:
932 case RID_THREAD:
933 /* Elaborated type specifiers. */
934 case RID_ENUM:
935 case RID_CLASS:
936 case RID_STRUCT:
937 case RID_UNION:
938 case RID_TYPENAME:
939 /* Simple type specifiers. */
940 case RID_CHAR:
941 case RID_CHAR16:
942 case RID_CHAR32:
943 case RID_WCHAR:
944 case RID_BOOL:
945 case RID_SHORT:
946 case RID_INT:
947 case RID_LONG:
948 case RID_INT128:
949 case RID_SIGNED:
950 case RID_UNSIGNED:
951 case RID_FLOAT:
952 case RID_DOUBLE:
953 case RID_VOID:
954 /* GNU extensions. */
955 case RID_ATTRIBUTE:
956 case RID_TYPEOF:
957 /* C++0x extensions. */
958 case RID_DECLTYPE:
959 case RID_UNDERLYING_TYPE:
960 return true;
962 default:
963 return false;
967 /* Returns TRUE iff the token T begins a decltype type. */
969 static bool
970 token_is_decltype (cp_token *t)
972 return (t->keyword == RID_DECLTYPE
973 || t->type == CPP_DECLTYPE);
976 /* Returns TRUE iff the next token begins a decltype type. */
978 static bool
979 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
981 cp_token *t = cp_lexer_peek_token (lexer);
982 return token_is_decltype (t);
985 /* Return a pointer to the Nth token in the token stream. If N is 1,
986 then this is precisely equivalent to cp_lexer_peek_token (except
987 that it is not inline). One would like to disallow that case, but
988 there is one case (cp_parser_nth_token_starts_template_id) where
989 the caller passes a variable for N and it might be 1. */
991 static cp_token *
992 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
994 cp_token *token;
996 /* N is 1-based, not zero-based. */
997 gcc_assert (n > 0);
999 if (cp_lexer_debugging_p (lexer))
1000 fprintf (cp_lexer_debug_stream,
1001 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1003 --n;
1004 token = lexer->next_token;
1005 gcc_assert (!n || token != &eof_token);
1006 while (n != 0)
1008 ++token;
1009 if (token == lexer->last_token)
1011 token = &eof_token;
1012 break;
1015 if (!token->purged_p)
1016 --n;
1019 if (cp_lexer_debugging_p (lexer))
1021 cp_lexer_print_token (cp_lexer_debug_stream, token);
1022 putc ('\n', cp_lexer_debug_stream);
1025 return token;
1028 /* Return the next token, and advance the lexer's next_token pointer
1029 to point to the next non-purged token. */
1031 static cp_token *
1032 cp_lexer_consume_token (cp_lexer* lexer)
1034 cp_token *token = lexer->next_token;
1036 gcc_assert (token != &eof_token);
1037 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1041 lexer->next_token++;
1042 if (lexer->next_token == lexer->last_token)
1044 lexer->next_token = &eof_token;
1045 break;
1049 while (lexer->next_token->purged_p);
1051 cp_lexer_set_source_position_from_token (token);
1053 /* Provide debugging output. */
1054 if (cp_lexer_debugging_p (lexer))
1056 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1057 cp_lexer_print_token (cp_lexer_debug_stream, token);
1058 putc ('\n', cp_lexer_debug_stream);
1061 return token;
1064 /* Permanently remove the next token from the token stream, and
1065 advance the next_token pointer to refer to the next non-purged
1066 token. */
1068 static void
1069 cp_lexer_purge_token (cp_lexer *lexer)
1071 cp_token *tok = lexer->next_token;
1073 gcc_assert (tok != &eof_token);
1074 tok->purged_p = true;
1075 tok->location = UNKNOWN_LOCATION;
1076 tok->u.value = NULL_TREE;
1077 tok->keyword = RID_MAX;
1081 tok++;
1082 if (tok == lexer->last_token)
1084 tok = &eof_token;
1085 break;
1088 while (tok->purged_p);
1089 lexer->next_token = tok;
1092 /* Permanently remove all tokens after TOK, up to, but not
1093 including, the token that will be returned next by
1094 cp_lexer_peek_token. */
1096 static void
1097 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1099 cp_token *peek = lexer->next_token;
1101 if (peek == &eof_token)
1102 peek = lexer->last_token;
1104 gcc_assert (tok < peek);
1106 for ( tok += 1; tok != peek; tok += 1)
1108 tok->purged_p = true;
1109 tok->location = UNKNOWN_LOCATION;
1110 tok->u.value = NULL_TREE;
1111 tok->keyword = RID_MAX;
1115 /* Begin saving tokens. All tokens consumed after this point will be
1116 preserved. */
1118 static void
1119 cp_lexer_save_tokens (cp_lexer* lexer)
1121 /* Provide debugging output. */
1122 if (cp_lexer_debugging_p (lexer))
1123 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1125 lexer->saved_tokens.safe_push (lexer->next_token);
1128 /* Commit to the portion of the token stream most recently saved. */
1130 static void
1131 cp_lexer_commit_tokens (cp_lexer* lexer)
1133 /* Provide debugging output. */
1134 if (cp_lexer_debugging_p (lexer))
1135 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1137 lexer->saved_tokens.pop ();
1140 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1141 to the token stream. Stop saving tokens. */
1143 static void
1144 cp_lexer_rollback_tokens (cp_lexer* lexer)
1146 /* Provide debugging output. */
1147 if (cp_lexer_debugging_p (lexer))
1148 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1150 lexer->next_token = lexer->saved_tokens.pop ();
1153 /* Print a representation of the TOKEN on the STREAM. */
1155 static void
1156 cp_lexer_print_token (FILE * stream, cp_token *token)
1158 /* We don't use cpp_type2name here because the parser defines
1159 a few tokens of its own. */
1160 static const char *const token_names[] = {
1161 /* cpplib-defined token types */
1162 #define OP(e, s) #e,
1163 #define TK(e, s) #e,
1164 TTYPE_TABLE
1165 #undef OP
1166 #undef TK
1167 /* C++ parser token types - see "Manifest constants", above. */
1168 "KEYWORD",
1169 "TEMPLATE_ID",
1170 "NESTED_NAME_SPECIFIER",
1173 /* For some tokens, print the associated data. */
1174 switch (token->type)
1176 case CPP_KEYWORD:
1177 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1178 For example, `struct' is mapped to an INTEGER_CST. */
1179 if (!identifier_p (token->u.value))
1180 break;
1181 /* else fall through */
1182 case CPP_NAME:
1183 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1184 break;
1186 case CPP_STRING:
1187 case CPP_STRING16:
1188 case CPP_STRING32:
1189 case CPP_WSTRING:
1190 case CPP_UTF8STRING:
1191 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1192 break;
1194 case CPP_NUMBER:
1195 print_generic_expr (stream, token->u.value, 0);
1196 break;
1198 default:
1199 /* If we have a name for the token, print it out. Otherwise, we
1200 simply give the numeric code. */
1201 if (token->type < ARRAY_SIZE(token_names))
1202 fputs (token_names[token->type], stream);
1203 else
1204 fprintf (stream, "[%d]", token->type);
1205 break;
1209 DEBUG_FUNCTION void
1210 debug (cp_token &ref)
1212 cp_lexer_print_token (stderr, &ref);
1213 fprintf (stderr, "\n");
1216 DEBUG_FUNCTION void
1217 debug (cp_token *ptr)
1219 if (ptr)
1220 debug (*ptr);
1221 else
1222 fprintf (stderr, "<nil>\n");
1226 /* Start emitting debugging information. */
1228 static void
1229 cp_lexer_start_debugging (cp_lexer* lexer)
1231 lexer->debugging_p = true;
1232 cp_lexer_debug_stream = stderr;
1235 /* Stop emitting debugging information. */
1237 static void
1238 cp_lexer_stop_debugging (cp_lexer* lexer)
1240 lexer->debugging_p = false;
1241 cp_lexer_debug_stream = NULL;
1244 /* Create a new cp_token_cache, representing a range of tokens. */
1246 static cp_token_cache *
1247 cp_token_cache_new (cp_token *first, cp_token *last)
1249 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1250 cache->first = first;
1251 cache->last = last;
1252 return cache;
1255 /* Diagnose if #pragma omp declare simd isn't followed immediately
1256 by function declaration or definition. */
1258 static inline void
1259 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1261 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1263 error ("%<#pragma omp declare simd%> not immediately followed by "
1264 "function declaration or definition");
1265 parser->omp_declare_simd = NULL;
1269 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1270 and put that into "omp declare simd" attribute. */
1272 static inline void
1273 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1275 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1277 if (fndecl == error_mark_node)
1279 parser->omp_declare_simd = NULL;
1280 return;
1282 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1284 cp_ensure_no_omp_declare_simd (parser);
1285 return;
1290 /* Decl-specifiers. */
1292 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1294 static void
1295 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1297 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1300 /* Declarators. */
1302 /* Nothing other than the parser should be creating declarators;
1303 declarators are a semi-syntactic representation of C++ entities.
1304 Other parts of the front end that need to create entities (like
1305 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1307 static cp_declarator *make_call_declarator
1308 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1309 static cp_declarator *make_array_declarator
1310 (cp_declarator *, tree);
1311 static cp_declarator *make_pointer_declarator
1312 (cp_cv_quals, cp_declarator *, tree);
1313 static cp_declarator *make_reference_declarator
1314 (cp_cv_quals, cp_declarator *, bool, tree);
1315 static cp_parameter_declarator *make_parameter_declarator
1316 (cp_decl_specifier_seq *, cp_declarator *, tree);
1317 static cp_declarator *make_ptrmem_declarator
1318 (cp_cv_quals, tree, cp_declarator *, tree);
1320 /* An erroneous declarator. */
1321 static cp_declarator *cp_error_declarator;
1323 /* The obstack on which declarators and related data structures are
1324 allocated. */
1325 static struct obstack declarator_obstack;
1327 /* Alloc BYTES from the declarator memory pool. */
1329 static inline void *
1330 alloc_declarator (size_t bytes)
1332 return obstack_alloc (&declarator_obstack, bytes);
1335 /* Allocate a declarator of the indicated KIND. Clear fields that are
1336 common to all declarators. */
1338 static cp_declarator *
1339 make_declarator (cp_declarator_kind kind)
1341 cp_declarator *declarator;
1343 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1344 declarator->kind = kind;
1345 declarator->attributes = NULL_TREE;
1346 declarator->std_attributes = NULL_TREE;
1347 declarator->declarator = NULL;
1348 declarator->parameter_pack_p = false;
1349 declarator->id_loc = UNKNOWN_LOCATION;
1351 return declarator;
1354 /* Make a declarator for a generalized identifier. If
1355 QUALIFYING_SCOPE is non-NULL, the identifier is
1356 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1357 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1358 is, if any. */
1360 static cp_declarator *
1361 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1362 special_function_kind sfk)
1364 cp_declarator *declarator;
1366 /* It is valid to write:
1368 class C { void f(); };
1369 typedef C D;
1370 void D::f();
1372 The standard is not clear about whether `typedef const C D' is
1373 legal; as of 2002-09-15 the committee is considering that
1374 question. EDG 3.0 allows that syntax. Therefore, we do as
1375 well. */
1376 if (qualifying_scope && TYPE_P (qualifying_scope))
1377 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1379 gcc_assert (identifier_p (unqualified_name)
1380 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1381 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1383 declarator = make_declarator (cdk_id);
1384 declarator->u.id.qualifying_scope = qualifying_scope;
1385 declarator->u.id.unqualified_name = unqualified_name;
1386 declarator->u.id.sfk = sfk;
1388 return declarator;
1391 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1392 of modifiers such as const or volatile to apply to the pointer
1393 type, represented as identifiers. ATTRIBUTES represent the attributes that
1394 appertain to the pointer or reference. */
1396 cp_declarator *
1397 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1398 tree attributes)
1400 cp_declarator *declarator;
1402 declarator = make_declarator (cdk_pointer);
1403 declarator->declarator = target;
1404 declarator->u.pointer.qualifiers = cv_qualifiers;
1405 declarator->u.pointer.class_type = NULL_TREE;
1406 if (target)
1408 declarator->id_loc = target->id_loc;
1409 declarator->parameter_pack_p = target->parameter_pack_p;
1410 target->parameter_pack_p = false;
1412 else
1413 declarator->parameter_pack_p = false;
1415 declarator->std_attributes = attributes;
1417 return declarator;
1420 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1421 represent the attributes that appertain to the pointer or
1422 reference. */
1424 cp_declarator *
1425 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1426 bool rvalue_ref, tree attributes)
1428 cp_declarator *declarator;
1430 declarator = make_declarator (cdk_reference);
1431 declarator->declarator = target;
1432 declarator->u.reference.qualifiers = cv_qualifiers;
1433 declarator->u.reference.rvalue_ref = rvalue_ref;
1434 if (target)
1436 declarator->id_loc = target->id_loc;
1437 declarator->parameter_pack_p = target->parameter_pack_p;
1438 target->parameter_pack_p = false;
1440 else
1441 declarator->parameter_pack_p = false;
1443 declarator->std_attributes = attributes;
1445 return declarator;
1448 /* Like make_pointer_declarator -- but for a pointer to a non-static
1449 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1450 appertain to the pointer or reference. */
1452 cp_declarator *
1453 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1454 cp_declarator *pointee,
1455 tree attributes)
1457 cp_declarator *declarator;
1459 declarator = make_declarator (cdk_ptrmem);
1460 declarator->declarator = pointee;
1461 declarator->u.pointer.qualifiers = cv_qualifiers;
1462 declarator->u.pointer.class_type = class_type;
1464 if (pointee)
1466 declarator->parameter_pack_p = pointee->parameter_pack_p;
1467 pointee->parameter_pack_p = false;
1469 else
1470 declarator->parameter_pack_p = false;
1472 declarator->std_attributes = attributes;
1474 return declarator;
1477 /* Make a declarator for the function given by TARGET, with the
1478 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1479 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1480 indicates what exceptions can be thrown. */
1482 cp_declarator *
1483 make_call_declarator (cp_declarator *target,
1484 tree parms,
1485 cp_cv_quals cv_qualifiers,
1486 cp_virt_specifiers virt_specifiers,
1487 cp_ref_qualifier ref_qualifier,
1488 tree exception_specification,
1489 tree late_return_type)
1491 cp_declarator *declarator;
1493 declarator = make_declarator (cdk_function);
1494 declarator->declarator = target;
1495 declarator->u.function.parameters = parms;
1496 declarator->u.function.qualifiers = cv_qualifiers;
1497 declarator->u.function.virt_specifiers = virt_specifiers;
1498 declarator->u.function.ref_qualifier = ref_qualifier;
1499 declarator->u.function.exception_specification = exception_specification;
1500 declarator->u.function.late_return_type = late_return_type;
1501 if (target)
1503 declarator->id_loc = target->id_loc;
1504 declarator->parameter_pack_p = target->parameter_pack_p;
1505 target->parameter_pack_p = false;
1507 else
1508 declarator->parameter_pack_p = false;
1510 return declarator;
1513 /* Make a declarator for an array of BOUNDS elements, each of which is
1514 defined by ELEMENT. */
1516 cp_declarator *
1517 make_array_declarator (cp_declarator *element, tree bounds)
1519 cp_declarator *declarator;
1521 declarator = make_declarator (cdk_array);
1522 declarator->declarator = element;
1523 declarator->u.array.bounds = bounds;
1524 if (element)
1526 declarator->id_loc = element->id_loc;
1527 declarator->parameter_pack_p = element->parameter_pack_p;
1528 element->parameter_pack_p = false;
1530 else
1531 declarator->parameter_pack_p = false;
1533 return declarator;
1536 /* Determine whether the declarator we've seen so far can be a
1537 parameter pack, when followed by an ellipsis. */
1538 static bool
1539 declarator_can_be_parameter_pack (cp_declarator *declarator)
1541 /* Search for a declarator name, or any other declarator that goes
1542 after the point where the ellipsis could appear in a parameter
1543 pack. If we find any of these, then this declarator can not be
1544 made into a parameter pack. */
1545 bool found = false;
1546 while (declarator && !found)
1548 switch ((int)declarator->kind)
1550 case cdk_id:
1551 case cdk_array:
1552 found = true;
1553 break;
1555 case cdk_error:
1556 return true;
1558 default:
1559 declarator = declarator->declarator;
1560 break;
1564 return !found;
1567 cp_parameter_declarator *no_parameters;
1569 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1570 DECLARATOR and DEFAULT_ARGUMENT. */
1572 cp_parameter_declarator *
1573 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1574 cp_declarator *declarator,
1575 tree default_argument)
1577 cp_parameter_declarator *parameter;
1579 parameter = ((cp_parameter_declarator *)
1580 alloc_declarator (sizeof (cp_parameter_declarator)));
1581 parameter->next = NULL;
1582 if (decl_specifiers)
1583 parameter->decl_specifiers = *decl_specifiers;
1584 else
1585 clear_decl_specs (&parameter->decl_specifiers);
1586 parameter->declarator = declarator;
1587 parameter->default_argument = default_argument;
1588 parameter->ellipsis_p = false;
1590 return parameter;
1593 /* Returns true iff DECLARATOR is a declaration for a function. */
1595 static bool
1596 function_declarator_p (const cp_declarator *declarator)
1598 while (declarator)
1600 if (declarator->kind == cdk_function
1601 && declarator->declarator->kind == cdk_id)
1602 return true;
1603 if (declarator->kind == cdk_id
1604 || declarator->kind == cdk_error)
1605 return false;
1606 declarator = declarator->declarator;
1608 return false;
1611 /* The parser. */
1613 /* Overview
1614 --------
1616 A cp_parser parses the token stream as specified by the C++
1617 grammar. Its job is purely parsing, not semantic analysis. For
1618 example, the parser breaks the token stream into declarators,
1619 expressions, statements, and other similar syntactic constructs.
1620 It does not check that the types of the expressions on either side
1621 of an assignment-statement are compatible, or that a function is
1622 not declared with a parameter of type `void'.
1624 The parser invokes routines elsewhere in the compiler to perform
1625 semantic analysis and to build up the abstract syntax tree for the
1626 code processed.
1628 The parser (and the template instantiation code, which is, in a
1629 way, a close relative of parsing) are the only parts of the
1630 compiler that should be calling push_scope and pop_scope, or
1631 related functions. The parser (and template instantiation code)
1632 keeps track of what scope is presently active; everything else
1633 should simply honor that. (The code that generates static
1634 initializers may also need to set the scope, in order to check
1635 access control correctly when emitting the initializers.)
1637 Methodology
1638 -----------
1640 The parser is of the standard recursive-descent variety. Upcoming
1641 tokens in the token stream are examined in order to determine which
1642 production to use when parsing a non-terminal. Some C++ constructs
1643 require arbitrary look ahead to disambiguate. For example, it is
1644 impossible, in the general case, to tell whether a statement is an
1645 expression or declaration without scanning the entire statement.
1646 Therefore, the parser is capable of "parsing tentatively." When the
1647 parser is not sure what construct comes next, it enters this mode.
1648 Then, while we attempt to parse the construct, the parser queues up
1649 error messages, rather than issuing them immediately, and saves the
1650 tokens it consumes. If the construct is parsed successfully, the
1651 parser "commits", i.e., it issues any queued error messages and
1652 the tokens that were being preserved are permanently discarded.
1653 If, however, the construct is not parsed successfully, the parser
1654 rolls back its state completely so that it can resume parsing using
1655 a different alternative.
1657 Future Improvements
1658 -------------------
1660 The performance of the parser could probably be improved substantially.
1661 We could often eliminate the need to parse tentatively by looking ahead
1662 a little bit. In some places, this approach might not entirely eliminate
1663 the need to parse tentatively, but it might still speed up the average
1664 case. */
1666 /* Flags that are passed to some parsing functions. These values can
1667 be bitwise-ored together. */
1669 enum
1671 /* No flags. */
1672 CP_PARSER_FLAGS_NONE = 0x0,
1673 /* The construct is optional. If it is not present, then no error
1674 should be issued. */
1675 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1676 /* When parsing a type-specifier, treat user-defined type-names
1677 as non-type identifiers. */
1678 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1679 /* When parsing a type-specifier, do not try to parse a class-specifier
1680 or enum-specifier. */
1681 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1682 /* When parsing a decl-specifier-seq, only allow type-specifier or
1683 constexpr. */
1684 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1687 /* This type is used for parameters and variables which hold
1688 combinations of the above flags. */
1689 typedef int cp_parser_flags;
1691 /* The different kinds of declarators we want to parse. */
1693 typedef enum cp_parser_declarator_kind
1695 /* We want an abstract declarator. */
1696 CP_PARSER_DECLARATOR_ABSTRACT,
1697 /* We want a named declarator. */
1698 CP_PARSER_DECLARATOR_NAMED,
1699 /* We don't mind, but the name must be an unqualified-id. */
1700 CP_PARSER_DECLARATOR_EITHER
1701 } cp_parser_declarator_kind;
1703 /* The precedence values used to parse binary expressions. The minimum value
1704 of PREC must be 1, because zero is reserved to quickly discriminate
1705 binary operators from other tokens. */
1707 enum cp_parser_prec
1709 PREC_NOT_OPERATOR,
1710 PREC_LOGICAL_OR_EXPRESSION,
1711 PREC_LOGICAL_AND_EXPRESSION,
1712 PREC_INCLUSIVE_OR_EXPRESSION,
1713 PREC_EXCLUSIVE_OR_EXPRESSION,
1714 PREC_AND_EXPRESSION,
1715 PREC_EQUALITY_EXPRESSION,
1716 PREC_RELATIONAL_EXPRESSION,
1717 PREC_SHIFT_EXPRESSION,
1718 PREC_ADDITIVE_EXPRESSION,
1719 PREC_MULTIPLICATIVE_EXPRESSION,
1720 PREC_PM_EXPRESSION,
1721 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1724 /* A mapping from a token type to a corresponding tree node type, with a
1725 precedence value. */
1727 typedef struct cp_parser_binary_operations_map_node
1729 /* The token type. */
1730 enum cpp_ttype token_type;
1731 /* The corresponding tree code. */
1732 enum tree_code tree_type;
1733 /* The precedence of this operator. */
1734 enum cp_parser_prec prec;
1735 } cp_parser_binary_operations_map_node;
1737 typedef struct cp_parser_expression_stack_entry
1739 /* Left hand side of the binary operation we are currently
1740 parsing. */
1741 tree lhs;
1742 /* Original tree code for left hand side, if it was a binary
1743 expression itself (used for -Wparentheses). */
1744 enum tree_code lhs_type;
1745 /* Tree code for the binary operation we are parsing. */
1746 enum tree_code tree_type;
1747 /* Precedence of the binary operation we are parsing. */
1748 enum cp_parser_prec prec;
1749 /* Location of the binary operation we are parsing. */
1750 location_t loc;
1751 } cp_parser_expression_stack_entry;
1753 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1754 entries because precedence levels on the stack are monotonically
1755 increasing. */
1756 typedef struct cp_parser_expression_stack_entry
1757 cp_parser_expression_stack[NUM_PREC_VALUES];
1759 /* Prototypes. */
1761 /* Constructors and destructors. */
1763 static cp_parser_context *cp_parser_context_new
1764 (cp_parser_context *);
1766 /* Class variables. */
1768 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1770 /* The operator-precedence table used by cp_parser_binary_expression.
1771 Transformed into an associative array (binops_by_token) by
1772 cp_parser_new. */
1774 static const cp_parser_binary_operations_map_node binops[] = {
1775 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1776 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1778 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1779 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1780 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1782 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1783 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1785 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1786 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1788 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1789 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1790 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1791 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1793 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1794 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1796 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1798 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1800 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1802 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1804 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1807 /* The same as binops, but initialized by cp_parser_new so that
1808 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1809 for speed. */
1810 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1812 /* Constructors and destructors. */
1814 /* Construct a new context. The context below this one on the stack
1815 is given by NEXT. */
1817 static cp_parser_context *
1818 cp_parser_context_new (cp_parser_context* next)
1820 cp_parser_context *context;
1822 /* Allocate the storage. */
1823 if (cp_parser_context_free_list != NULL)
1825 /* Pull the first entry from the free list. */
1826 context = cp_parser_context_free_list;
1827 cp_parser_context_free_list = context->next;
1828 memset (context, 0, sizeof (*context));
1830 else
1831 context = ggc_cleared_alloc<cp_parser_context> ();
1833 /* No errors have occurred yet in this context. */
1834 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1835 /* If this is not the bottommost context, copy information that we
1836 need from the previous context. */
1837 if (next)
1839 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1840 expression, then we are parsing one in this context, too. */
1841 context->object_type = next->object_type;
1842 /* Thread the stack. */
1843 context->next = next;
1846 return context;
1849 /* Managing the unparsed function queues. */
1851 #define unparsed_funs_with_default_args \
1852 parser->unparsed_queues->last ().funs_with_default_args
1853 #define unparsed_funs_with_definitions \
1854 parser->unparsed_queues->last ().funs_with_definitions
1855 #define unparsed_nsdmis \
1856 parser->unparsed_queues->last ().nsdmis
1857 #define unparsed_classes \
1858 parser->unparsed_queues->last ().classes
1860 static void
1861 push_unparsed_function_queues (cp_parser *parser)
1863 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1864 vec_safe_push (parser->unparsed_queues, e);
1867 static void
1868 pop_unparsed_function_queues (cp_parser *parser)
1870 release_tree_vector (unparsed_funs_with_definitions);
1871 parser->unparsed_queues->pop ();
1874 /* Prototypes. */
1876 /* Constructors and destructors. */
1878 static cp_parser *cp_parser_new
1879 (void);
1881 /* Routines to parse various constructs.
1883 Those that return `tree' will return the error_mark_node (rather
1884 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1885 Sometimes, they will return an ordinary node if error-recovery was
1886 attempted, even though a parse error occurred. So, to check
1887 whether or not a parse error occurred, you should always use
1888 cp_parser_error_occurred. If the construct is optional (indicated
1889 either by an `_opt' in the name of the function that does the
1890 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1891 the construct is not present. */
1893 /* Lexical conventions [gram.lex] */
1895 static tree cp_parser_identifier
1896 (cp_parser *);
1897 static tree cp_parser_string_literal
1898 (cp_parser *, bool, bool, bool);
1899 static tree cp_parser_userdef_char_literal
1900 (cp_parser *);
1901 static tree cp_parser_userdef_string_literal
1902 (tree);
1903 static tree cp_parser_userdef_numeric_literal
1904 (cp_parser *);
1906 /* Basic concepts [gram.basic] */
1908 static bool cp_parser_translation_unit
1909 (cp_parser *);
1911 /* Expressions [gram.expr] */
1913 static tree cp_parser_primary_expression
1914 (cp_parser *, bool, bool, bool, cp_id_kind *);
1915 static tree cp_parser_id_expression
1916 (cp_parser *, bool, bool, bool *, bool, bool);
1917 static tree cp_parser_unqualified_id
1918 (cp_parser *, bool, bool, bool, bool);
1919 static tree cp_parser_nested_name_specifier_opt
1920 (cp_parser *, bool, bool, bool, bool);
1921 static tree cp_parser_nested_name_specifier
1922 (cp_parser *, bool, bool, bool, bool);
1923 static tree cp_parser_qualifying_entity
1924 (cp_parser *, bool, bool, bool, bool, bool);
1925 static tree cp_parser_postfix_expression
1926 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1927 static tree cp_parser_postfix_open_square_expression
1928 (cp_parser *, tree, bool, bool);
1929 static tree cp_parser_postfix_dot_deref_expression
1930 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1931 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1932 (cp_parser *, int, bool, bool, bool *, bool = false);
1933 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1934 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1935 static void cp_parser_pseudo_destructor_name
1936 (cp_parser *, tree, tree *, tree *);
1937 static tree cp_parser_unary_expression
1938 (cp_parser *, bool, bool, cp_id_kind *);
1939 static enum tree_code cp_parser_unary_operator
1940 (cp_token *);
1941 static tree cp_parser_new_expression
1942 (cp_parser *);
1943 static vec<tree, va_gc> *cp_parser_new_placement
1944 (cp_parser *);
1945 static tree cp_parser_new_type_id
1946 (cp_parser *, tree *);
1947 static cp_declarator *cp_parser_new_declarator_opt
1948 (cp_parser *);
1949 static cp_declarator *cp_parser_direct_new_declarator
1950 (cp_parser *);
1951 static vec<tree, va_gc> *cp_parser_new_initializer
1952 (cp_parser *);
1953 static tree cp_parser_delete_expression
1954 (cp_parser *);
1955 static tree cp_parser_cast_expression
1956 (cp_parser *, bool, bool, bool, cp_id_kind *);
1957 static tree cp_parser_binary_expression
1958 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1959 static tree cp_parser_question_colon_clause
1960 (cp_parser *, tree);
1961 static tree cp_parser_assignment_expression
1962 (cp_parser *, bool, cp_id_kind *);
1963 static enum tree_code cp_parser_assignment_operator_opt
1964 (cp_parser *);
1965 static tree cp_parser_expression
1966 (cp_parser *, bool, cp_id_kind *);
1967 static tree cp_parser_expression
1968 (cp_parser *, bool, bool, cp_id_kind *);
1969 static tree cp_parser_constant_expression
1970 (cp_parser *, bool, bool *);
1971 static tree cp_parser_builtin_offsetof
1972 (cp_parser *);
1973 static tree cp_parser_lambda_expression
1974 (cp_parser *);
1975 static void cp_parser_lambda_introducer
1976 (cp_parser *, tree);
1977 static bool cp_parser_lambda_declarator_opt
1978 (cp_parser *, tree);
1979 static void cp_parser_lambda_body
1980 (cp_parser *, tree);
1982 /* Statements [gram.stmt.stmt] */
1984 static void cp_parser_statement
1985 (cp_parser *, tree, bool, bool *);
1986 static void cp_parser_label_for_labeled_statement
1987 (cp_parser *, tree);
1988 static tree cp_parser_expression_statement
1989 (cp_parser *, tree);
1990 static tree cp_parser_compound_statement
1991 (cp_parser *, tree, bool, bool);
1992 static void cp_parser_statement_seq_opt
1993 (cp_parser *, tree);
1994 static tree cp_parser_selection_statement
1995 (cp_parser *, bool *);
1996 static tree cp_parser_condition
1997 (cp_parser *);
1998 static tree cp_parser_iteration_statement
1999 (cp_parser *, bool);
2000 static bool cp_parser_for_init_statement
2001 (cp_parser *, tree *decl);
2002 static tree cp_parser_for
2003 (cp_parser *, bool);
2004 static tree cp_parser_c_for
2005 (cp_parser *, tree, tree, bool);
2006 static tree cp_parser_range_for
2007 (cp_parser *, tree, tree, tree, bool);
2008 static void do_range_for_auto_deduction
2009 (tree, tree);
2010 static tree cp_parser_perform_range_for_lookup
2011 (tree, tree *, tree *);
2012 static tree cp_parser_range_for_member_function
2013 (tree, tree);
2014 static tree cp_parser_jump_statement
2015 (cp_parser *);
2016 static void cp_parser_declaration_statement
2017 (cp_parser *);
2019 static tree cp_parser_implicitly_scoped_statement
2020 (cp_parser *, bool *);
2021 static void cp_parser_already_scoped_statement
2022 (cp_parser *);
2024 /* Declarations [gram.dcl.dcl] */
2026 static void cp_parser_declaration_seq_opt
2027 (cp_parser *);
2028 static void cp_parser_declaration
2029 (cp_parser *);
2030 static void cp_parser_block_declaration
2031 (cp_parser *, bool);
2032 static void cp_parser_simple_declaration
2033 (cp_parser *, bool, tree *);
2034 static void cp_parser_decl_specifier_seq
2035 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2036 static tree cp_parser_storage_class_specifier_opt
2037 (cp_parser *);
2038 static tree cp_parser_function_specifier_opt
2039 (cp_parser *, cp_decl_specifier_seq *);
2040 static tree cp_parser_type_specifier
2041 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2042 int *, bool *);
2043 static tree cp_parser_simple_type_specifier
2044 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2045 static tree cp_parser_type_name
2046 (cp_parser *);
2047 static tree cp_parser_nonclass_name
2048 (cp_parser* parser);
2049 static tree cp_parser_elaborated_type_specifier
2050 (cp_parser *, bool, bool);
2051 static tree cp_parser_enum_specifier
2052 (cp_parser *);
2053 static void cp_parser_enumerator_list
2054 (cp_parser *, tree);
2055 static void cp_parser_enumerator_definition
2056 (cp_parser *, tree);
2057 static tree cp_parser_namespace_name
2058 (cp_parser *);
2059 static void cp_parser_namespace_definition
2060 (cp_parser *);
2061 static void cp_parser_namespace_body
2062 (cp_parser *);
2063 static tree cp_parser_qualified_namespace_specifier
2064 (cp_parser *);
2065 static void cp_parser_namespace_alias_definition
2066 (cp_parser *);
2067 static bool cp_parser_using_declaration
2068 (cp_parser *, bool);
2069 static void cp_parser_using_directive
2070 (cp_parser *);
2071 static tree cp_parser_alias_declaration
2072 (cp_parser *);
2073 static void cp_parser_asm_definition
2074 (cp_parser *);
2075 static void cp_parser_linkage_specification
2076 (cp_parser *);
2077 static void cp_parser_static_assert
2078 (cp_parser *, bool);
2079 static tree cp_parser_decltype
2080 (cp_parser *);
2082 /* Declarators [gram.dcl.decl] */
2084 static tree cp_parser_init_declarator
2085 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2086 static cp_declarator *cp_parser_declarator
2087 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2088 static cp_declarator *cp_parser_direct_declarator
2089 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2090 static enum tree_code cp_parser_ptr_operator
2091 (cp_parser *, tree *, cp_cv_quals *, tree *);
2092 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2093 (cp_parser *);
2094 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2095 (cp_parser *);
2096 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2097 (cp_parser *);
2098 static tree cp_parser_late_return_type_opt
2099 (cp_parser *, cp_declarator *, cp_cv_quals);
2100 static tree cp_parser_declarator_id
2101 (cp_parser *, bool);
2102 static tree cp_parser_type_id
2103 (cp_parser *);
2104 static tree cp_parser_template_type_arg
2105 (cp_parser *);
2106 static tree cp_parser_trailing_type_id (cp_parser *);
2107 static tree cp_parser_type_id_1
2108 (cp_parser *, bool, bool);
2109 static void cp_parser_type_specifier_seq
2110 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2111 static tree cp_parser_parameter_declaration_clause
2112 (cp_parser *);
2113 static tree cp_parser_parameter_declaration_list
2114 (cp_parser *, bool *);
2115 static cp_parameter_declarator *cp_parser_parameter_declaration
2116 (cp_parser *, bool, bool *);
2117 static tree cp_parser_default_argument
2118 (cp_parser *, bool);
2119 static void cp_parser_function_body
2120 (cp_parser *, bool);
2121 static tree cp_parser_initializer
2122 (cp_parser *, bool *, bool *);
2123 static tree cp_parser_initializer_clause
2124 (cp_parser *, bool *);
2125 static tree cp_parser_braced_list
2126 (cp_parser*, bool*);
2127 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2128 (cp_parser *, bool *);
2130 static bool cp_parser_ctor_initializer_opt_and_function_body
2131 (cp_parser *, bool);
2133 static tree cp_parser_late_parsing_omp_declare_simd
2134 (cp_parser *, tree);
2136 static tree cp_parser_late_parsing_cilk_simd_fn_info
2137 (cp_parser *, tree);
2139 static tree synthesize_implicit_template_parm
2140 (cp_parser *);
2141 static tree finish_fully_implicit_template
2142 (cp_parser *, tree);
2144 /* Classes [gram.class] */
2146 static tree cp_parser_class_name
2147 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2148 static tree cp_parser_class_specifier
2149 (cp_parser *);
2150 static tree cp_parser_class_head
2151 (cp_parser *, bool *);
2152 static enum tag_types cp_parser_class_key
2153 (cp_parser *);
2154 static void cp_parser_type_parameter_key
2155 (cp_parser* parser);
2156 static void cp_parser_member_specification_opt
2157 (cp_parser *);
2158 static void cp_parser_member_declaration
2159 (cp_parser *);
2160 static tree cp_parser_pure_specifier
2161 (cp_parser *);
2162 static tree cp_parser_constant_initializer
2163 (cp_parser *);
2165 /* Derived classes [gram.class.derived] */
2167 static tree cp_parser_base_clause
2168 (cp_parser *);
2169 static tree cp_parser_base_specifier
2170 (cp_parser *);
2172 /* Special member functions [gram.special] */
2174 static tree cp_parser_conversion_function_id
2175 (cp_parser *);
2176 static tree cp_parser_conversion_type_id
2177 (cp_parser *);
2178 static cp_declarator *cp_parser_conversion_declarator_opt
2179 (cp_parser *);
2180 static bool cp_parser_ctor_initializer_opt
2181 (cp_parser *);
2182 static void cp_parser_mem_initializer_list
2183 (cp_parser *);
2184 static tree cp_parser_mem_initializer
2185 (cp_parser *);
2186 static tree cp_parser_mem_initializer_id
2187 (cp_parser *);
2189 /* Overloading [gram.over] */
2191 static tree cp_parser_operator_function_id
2192 (cp_parser *);
2193 static tree cp_parser_operator
2194 (cp_parser *);
2196 /* Templates [gram.temp] */
2198 static void cp_parser_template_declaration
2199 (cp_parser *, bool);
2200 static tree cp_parser_template_parameter_list
2201 (cp_parser *);
2202 static tree cp_parser_template_parameter
2203 (cp_parser *, bool *, bool *);
2204 static tree cp_parser_type_parameter
2205 (cp_parser *, bool *);
2206 static tree cp_parser_template_id
2207 (cp_parser *, bool, bool, enum tag_types, bool);
2208 static tree cp_parser_template_name
2209 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2210 static tree cp_parser_template_argument_list
2211 (cp_parser *);
2212 static tree cp_parser_template_argument
2213 (cp_parser *);
2214 static void cp_parser_explicit_instantiation
2215 (cp_parser *);
2216 static void cp_parser_explicit_specialization
2217 (cp_parser *);
2219 /* Exception handling [gram.exception] */
2221 static tree cp_parser_try_block
2222 (cp_parser *);
2223 static bool cp_parser_function_try_block
2224 (cp_parser *);
2225 static void cp_parser_handler_seq
2226 (cp_parser *);
2227 static void cp_parser_handler
2228 (cp_parser *);
2229 static tree cp_parser_exception_declaration
2230 (cp_parser *);
2231 static tree cp_parser_throw_expression
2232 (cp_parser *);
2233 static tree cp_parser_exception_specification_opt
2234 (cp_parser *);
2235 static tree cp_parser_type_id_list
2236 (cp_parser *);
2238 /* GNU Extensions */
2240 static tree cp_parser_asm_specification_opt
2241 (cp_parser *);
2242 static tree cp_parser_asm_operand_list
2243 (cp_parser *);
2244 static tree cp_parser_asm_clobber_list
2245 (cp_parser *);
2246 static tree cp_parser_asm_label_list
2247 (cp_parser *);
2248 static bool cp_next_tokens_can_be_attribute_p
2249 (cp_parser *);
2250 static bool cp_next_tokens_can_be_gnu_attribute_p
2251 (cp_parser *);
2252 static bool cp_next_tokens_can_be_std_attribute_p
2253 (cp_parser *);
2254 static bool cp_nth_tokens_can_be_std_attribute_p
2255 (cp_parser *, size_t);
2256 static bool cp_nth_tokens_can_be_gnu_attribute_p
2257 (cp_parser *, size_t);
2258 static bool cp_nth_tokens_can_be_attribute_p
2259 (cp_parser *, size_t);
2260 static tree cp_parser_attributes_opt
2261 (cp_parser *);
2262 static tree cp_parser_gnu_attributes_opt
2263 (cp_parser *);
2264 static tree cp_parser_gnu_attribute_list
2265 (cp_parser *);
2266 static tree cp_parser_std_attribute
2267 (cp_parser *);
2268 static tree cp_parser_std_attribute_spec
2269 (cp_parser *);
2270 static tree cp_parser_std_attribute_spec_seq
2271 (cp_parser *);
2272 static bool cp_parser_extension_opt
2273 (cp_parser *, int *);
2274 static void cp_parser_label_declaration
2275 (cp_parser *);
2277 /* Transactional Memory Extensions */
2279 static tree cp_parser_transaction
2280 (cp_parser *, enum rid);
2281 static tree cp_parser_transaction_expression
2282 (cp_parser *, enum rid);
2283 static bool cp_parser_function_transaction
2284 (cp_parser *, enum rid);
2285 static tree cp_parser_transaction_cancel
2286 (cp_parser *);
2288 enum pragma_context {
2289 pragma_external,
2290 pragma_member,
2291 pragma_objc_icode,
2292 pragma_stmt,
2293 pragma_compound
2295 static bool cp_parser_pragma
2296 (cp_parser *, enum pragma_context);
2298 /* Objective-C++ Productions */
2300 static tree cp_parser_objc_message_receiver
2301 (cp_parser *);
2302 static tree cp_parser_objc_message_args
2303 (cp_parser *);
2304 static tree cp_parser_objc_message_expression
2305 (cp_parser *);
2306 static tree cp_parser_objc_encode_expression
2307 (cp_parser *);
2308 static tree cp_parser_objc_defs_expression
2309 (cp_parser *);
2310 static tree cp_parser_objc_protocol_expression
2311 (cp_parser *);
2312 static tree cp_parser_objc_selector_expression
2313 (cp_parser *);
2314 static tree cp_parser_objc_expression
2315 (cp_parser *);
2316 static bool cp_parser_objc_selector_p
2317 (enum cpp_ttype);
2318 static tree cp_parser_objc_selector
2319 (cp_parser *);
2320 static tree cp_parser_objc_protocol_refs_opt
2321 (cp_parser *);
2322 static void cp_parser_objc_declaration
2323 (cp_parser *, tree);
2324 static tree cp_parser_objc_statement
2325 (cp_parser *);
2326 static bool cp_parser_objc_valid_prefix_attributes
2327 (cp_parser *, tree *);
2328 static void cp_parser_objc_at_property_declaration
2329 (cp_parser *) ;
2330 static void cp_parser_objc_at_synthesize_declaration
2331 (cp_parser *) ;
2332 static void cp_parser_objc_at_dynamic_declaration
2333 (cp_parser *) ;
2334 static tree cp_parser_objc_struct_declaration
2335 (cp_parser *) ;
2337 /* Utility Routines */
2339 static tree cp_parser_lookup_name
2340 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2341 static tree cp_parser_lookup_name_simple
2342 (cp_parser *, tree, location_t);
2343 static tree cp_parser_maybe_treat_template_as_class
2344 (tree, bool);
2345 static bool cp_parser_check_declarator_template_parameters
2346 (cp_parser *, cp_declarator *, location_t);
2347 static bool cp_parser_check_template_parameters
2348 (cp_parser *, unsigned, location_t, cp_declarator *);
2349 static tree cp_parser_simple_cast_expression
2350 (cp_parser *);
2351 static tree cp_parser_global_scope_opt
2352 (cp_parser *, bool);
2353 static bool cp_parser_constructor_declarator_p
2354 (cp_parser *, bool);
2355 static tree cp_parser_function_definition_from_specifiers_and_declarator
2356 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2357 static tree cp_parser_function_definition_after_declarator
2358 (cp_parser *, bool);
2359 static void cp_parser_template_declaration_after_export
2360 (cp_parser *, bool);
2361 static void cp_parser_perform_template_parameter_access_checks
2362 (vec<deferred_access_check, va_gc> *);
2363 static tree cp_parser_single_declaration
2364 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2365 static tree cp_parser_functional_cast
2366 (cp_parser *, tree);
2367 static tree cp_parser_save_member_function_body
2368 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2369 static tree cp_parser_save_nsdmi
2370 (cp_parser *);
2371 static tree cp_parser_enclosed_template_argument_list
2372 (cp_parser *);
2373 static void cp_parser_save_default_args
2374 (cp_parser *, tree);
2375 static void cp_parser_late_parsing_for_member
2376 (cp_parser *, tree);
2377 static tree cp_parser_late_parse_one_default_arg
2378 (cp_parser *, tree, tree, tree);
2379 static void cp_parser_late_parsing_nsdmi
2380 (cp_parser *, tree);
2381 static void cp_parser_late_parsing_default_args
2382 (cp_parser *, tree);
2383 static tree cp_parser_sizeof_operand
2384 (cp_parser *, enum rid);
2385 static tree cp_parser_trait_expr
2386 (cp_parser *, enum rid);
2387 static bool cp_parser_declares_only_class_p
2388 (cp_parser *);
2389 static void cp_parser_set_storage_class
2390 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2391 static void cp_parser_set_decl_spec_type
2392 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2393 static void set_and_check_decl_spec_loc
2394 (cp_decl_specifier_seq *decl_specs,
2395 cp_decl_spec ds, cp_token *);
2396 static bool cp_parser_friend_p
2397 (const cp_decl_specifier_seq *);
2398 static void cp_parser_required_error
2399 (cp_parser *, required_token, bool);
2400 static cp_token *cp_parser_require
2401 (cp_parser *, enum cpp_ttype, required_token);
2402 static cp_token *cp_parser_require_keyword
2403 (cp_parser *, enum rid, required_token);
2404 static bool cp_parser_token_starts_function_definition_p
2405 (cp_token *);
2406 static bool cp_parser_next_token_starts_class_definition_p
2407 (cp_parser *);
2408 static bool cp_parser_next_token_ends_template_argument_p
2409 (cp_parser *);
2410 static bool cp_parser_nth_token_starts_template_argument_list_p
2411 (cp_parser *, size_t);
2412 static enum tag_types cp_parser_token_is_class_key
2413 (cp_token *);
2414 static enum tag_types cp_parser_token_is_type_parameter_key
2415 (cp_token *);
2416 static void cp_parser_check_class_key
2417 (enum tag_types, tree type);
2418 static void cp_parser_check_access_in_redeclaration
2419 (tree type, location_t location);
2420 static bool cp_parser_optional_template_keyword
2421 (cp_parser *);
2422 static void cp_parser_pre_parsed_nested_name_specifier
2423 (cp_parser *);
2424 static bool cp_parser_cache_group
2425 (cp_parser *, enum cpp_ttype, unsigned);
2426 static tree cp_parser_cache_defarg
2427 (cp_parser *parser, bool nsdmi);
2428 static void cp_parser_parse_tentatively
2429 (cp_parser *);
2430 static void cp_parser_commit_to_tentative_parse
2431 (cp_parser *);
2432 static void cp_parser_commit_to_topmost_tentative_parse
2433 (cp_parser *);
2434 static void cp_parser_abort_tentative_parse
2435 (cp_parser *);
2436 static bool cp_parser_parse_definitely
2437 (cp_parser *);
2438 static inline bool cp_parser_parsing_tentatively
2439 (cp_parser *);
2440 static bool cp_parser_uncommitted_to_tentative_parse_p
2441 (cp_parser *);
2442 static void cp_parser_error
2443 (cp_parser *, const char *);
2444 static void cp_parser_name_lookup_error
2445 (cp_parser *, tree, tree, name_lookup_error, location_t);
2446 static bool cp_parser_simulate_error
2447 (cp_parser *);
2448 static bool cp_parser_check_type_definition
2449 (cp_parser *);
2450 static void cp_parser_check_for_definition_in_return_type
2451 (cp_declarator *, tree, location_t type_location);
2452 static void cp_parser_check_for_invalid_template_id
2453 (cp_parser *, tree, enum tag_types, location_t location);
2454 static bool cp_parser_non_integral_constant_expression
2455 (cp_parser *, non_integral_constant);
2456 static void cp_parser_diagnose_invalid_type_name
2457 (cp_parser *, tree, tree, location_t);
2458 static bool cp_parser_parse_and_diagnose_invalid_type_name
2459 (cp_parser *);
2460 static int cp_parser_skip_to_closing_parenthesis
2461 (cp_parser *, bool, bool, bool);
2462 static void cp_parser_skip_to_end_of_statement
2463 (cp_parser *);
2464 static void cp_parser_consume_semicolon_at_end_of_statement
2465 (cp_parser *);
2466 static void cp_parser_skip_to_end_of_block_or_statement
2467 (cp_parser *);
2468 static bool cp_parser_skip_to_closing_brace
2469 (cp_parser *);
2470 static void cp_parser_skip_to_end_of_template_parameter_list
2471 (cp_parser *);
2472 static void cp_parser_skip_to_pragma_eol
2473 (cp_parser*, cp_token *);
2474 static bool cp_parser_error_occurred
2475 (cp_parser *);
2476 static bool cp_parser_allow_gnu_extensions_p
2477 (cp_parser *);
2478 static bool cp_parser_is_pure_string_literal
2479 (cp_token *);
2480 static bool cp_parser_is_string_literal
2481 (cp_token *);
2482 static bool cp_parser_is_keyword
2483 (cp_token *, enum rid);
2484 static tree cp_parser_make_typename_type
2485 (cp_parser *, tree, tree, location_t location);
2486 static cp_declarator * cp_parser_make_indirect_declarator
2487 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2488 static bool cp_parser_compound_literal_p
2489 (cp_parser *);
2491 /* Returns nonzero if we are parsing tentatively. */
2493 static inline bool
2494 cp_parser_parsing_tentatively (cp_parser* parser)
2496 return parser->context->next != NULL;
2499 /* Returns nonzero if TOKEN is a string literal. */
2501 static bool
2502 cp_parser_is_pure_string_literal (cp_token* token)
2504 return (token->type == CPP_STRING ||
2505 token->type == CPP_STRING16 ||
2506 token->type == CPP_STRING32 ||
2507 token->type == CPP_WSTRING ||
2508 token->type == CPP_UTF8STRING);
2511 /* Returns nonzero if TOKEN is a string literal
2512 of a user-defined string literal. */
2514 static bool
2515 cp_parser_is_string_literal (cp_token* token)
2517 return (cp_parser_is_pure_string_literal (token) ||
2518 token->type == CPP_STRING_USERDEF ||
2519 token->type == CPP_STRING16_USERDEF ||
2520 token->type == CPP_STRING32_USERDEF ||
2521 token->type == CPP_WSTRING_USERDEF ||
2522 token->type == CPP_UTF8STRING_USERDEF);
2525 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2527 static bool
2528 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2530 return token->keyword == keyword;
2533 /* If not parsing tentatively, issue a diagnostic of the form
2534 FILE:LINE: MESSAGE before TOKEN
2535 where TOKEN is the next token in the input stream. MESSAGE
2536 (specified by the caller) is usually of the form "expected
2537 OTHER-TOKEN". */
2539 static void
2540 cp_parser_error (cp_parser* parser, const char* gmsgid)
2542 if (!cp_parser_simulate_error (parser))
2544 cp_token *token = cp_lexer_peek_token (parser->lexer);
2545 /* This diagnostic makes more sense if it is tagged to the line
2546 of the token we just peeked at. */
2547 cp_lexer_set_source_position_from_token (token);
2549 if (token->type == CPP_PRAGMA)
2551 error_at (token->location,
2552 "%<#pragma%> is not allowed here");
2553 cp_parser_skip_to_pragma_eol (parser, token);
2554 return;
2557 c_parse_error (gmsgid,
2558 /* Because c_parser_error does not understand
2559 CPP_KEYWORD, keywords are treated like
2560 identifiers. */
2561 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2562 token->u.value, token->flags);
2566 /* Issue an error about name-lookup failing. NAME is the
2567 IDENTIFIER_NODE DECL is the result of
2568 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2569 the thing that we hoped to find. */
2571 static void
2572 cp_parser_name_lookup_error (cp_parser* parser,
2573 tree name,
2574 tree decl,
2575 name_lookup_error desired,
2576 location_t location)
2578 /* If name lookup completely failed, tell the user that NAME was not
2579 declared. */
2580 if (decl == error_mark_node)
2582 if (parser->scope && parser->scope != global_namespace)
2583 error_at (location, "%<%E::%E%> has not been declared",
2584 parser->scope, name);
2585 else if (parser->scope == global_namespace)
2586 error_at (location, "%<::%E%> has not been declared", name);
2587 else if (parser->object_scope
2588 && !CLASS_TYPE_P (parser->object_scope))
2589 error_at (location, "request for member %qE in non-class type %qT",
2590 name, parser->object_scope);
2591 else if (parser->object_scope)
2592 error_at (location, "%<%T::%E%> has not been declared",
2593 parser->object_scope, name);
2594 else
2595 error_at (location, "%qE has not been declared", name);
2597 else if (parser->scope && parser->scope != global_namespace)
2599 switch (desired)
2601 case NLE_TYPE:
2602 error_at (location, "%<%E::%E%> is not a type",
2603 parser->scope, name);
2604 break;
2605 case NLE_CXX98:
2606 error_at (location, "%<%E::%E%> is not a class or namespace",
2607 parser->scope, name);
2608 break;
2609 case NLE_NOT_CXX98:
2610 error_at (location,
2611 "%<%E::%E%> is not a class, namespace, or enumeration",
2612 parser->scope, name);
2613 break;
2614 default:
2615 gcc_unreachable ();
2619 else if (parser->scope == global_namespace)
2621 switch (desired)
2623 case NLE_TYPE:
2624 error_at (location, "%<::%E%> is not a type", name);
2625 break;
2626 case NLE_CXX98:
2627 error_at (location, "%<::%E%> is not a class or namespace", name);
2628 break;
2629 case NLE_NOT_CXX98:
2630 error_at (location,
2631 "%<::%E%> is not a class, namespace, or enumeration",
2632 name);
2633 break;
2634 default:
2635 gcc_unreachable ();
2638 else
2640 switch (desired)
2642 case NLE_TYPE:
2643 error_at (location, "%qE is not a type", name);
2644 break;
2645 case NLE_CXX98:
2646 error_at (location, "%qE is not a class or namespace", name);
2647 break;
2648 case NLE_NOT_CXX98:
2649 error_at (location,
2650 "%qE is not a class, namespace, or enumeration", name);
2651 break;
2652 default:
2653 gcc_unreachable ();
2658 /* If we are parsing tentatively, remember that an error has occurred
2659 during this tentative parse. Returns true if the error was
2660 simulated; false if a message should be issued by the caller. */
2662 static bool
2663 cp_parser_simulate_error (cp_parser* parser)
2665 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2667 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2668 return true;
2670 return false;
2673 /* This function is called when a type is defined. If type
2674 definitions are forbidden at this point, an error message is
2675 issued. */
2677 static bool
2678 cp_parser_check_type_definition (cp_parser* parser)
2680 /* If types are forbidden here, issue a message. */
2681 if (parser->type_definition_forbidden_message)
2683 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2684 in the message need to be interpreted. */
2685 error (parser->type_definition_forbidden_message);
2686 return false;
2688 return true;
2691 /* This function is called when the DECLARATOR is processed. The TYPE
2692 was a type defined in the decl-specifiers. If it is invalid to
2693 define a type in the decl-specifiers for DECLARATOR, an error is
2694 issued. TYPE_LOCATION is the location of TYPE and is used
2695 for error reporting. */
2697 static void
2698 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2699 tree type, location_t type_location)
2701 /* [dcl.fct] forbids type definitions in return types.
2702 Unfortunately, it's not easy to know whether or not we are
2703 processing a return type until after the fact. */
2704 while (declarator
2705 && (declarator->kind == cdk_pointer
2706 || declarator->kind == cdk_reference
2707 || declarator->kind == cdk_ptrmem))
2708 declarator = declarator->declarator;
2709 if (declarator
2710 && declarator->kind == cdk_function)
2712 error_at (type_location,
2713 "new types may not be defined in a return type");
2714 inform (type_location,
2715 "(perhaps a semicolon is missing after the definition of %qT)",
2716 type);
2720 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2721 "<" in any valid C++ program. If the next token is indeed "<",
2722 issue a message warning the user about what appears to be an
2723 invalid attempt to form a template-id. LOCATION is the location
2724 of the type-specifier (TYPE) */
2726 static void
2727 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2728 tree type,
2729 enum tag_types tag_type,
2730 location_t location)
2732 cp_token_position start = 0;
2734 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2736 if (TYPE_P (type))
2737 error_at (location, "%qT is not a template", type);
2738 else if (identifier_p (type))
2740 if (tag_type != none_type)
2741 error_at (location, "%qE is not a class template", type);
2742 else
2743 error_at (location, "%qE is not a template", type);
2745 else
2746 error_at (location, "invalid template-id");
2747 /* Remember the location of the invalid "<". */
2748 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2749 start = cp_lexer_token_position (parser->lexer, true);
2750 /* Consume the "<". */
2751 cp_lexer_consume_token (parser->lexer);
2752 /* Parse the template arguments. */
2753 cp_parser_enclosed_template_argument_list (parser);
2754 /* Permanently remove the invalid template arguments so that
2755 this error message is not issued again. */
2756 if (start)
2757 cp_lexer_purge_tokens_after (parser->lexer, start);
2761 /* If parsing an integral constant-expression, issue an error message
2762 about the fact that THING appeared and return true. Otherwise,
2763 return false. In either case, set
2764 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2766 static bool
2767 cp_parser_non_integral_constant_expression (cp_parser *parser,
2768 non_integral_constant thing)
2770 parser->non_integral_constant_expression_p = true;
2771 if (parser->integral_constant_expression_p)
2773 if (!parser->allow_non_integral_constant_expression_p)
2775 const char *msg = NULL;
2776 switch (thing)
2778 case NIC_FLOAT:
2779 error ("floating-point literal "
2780 "cannot appear in a constant-expression");
2781 return true;
2782 case NIC_CAST:
2783 error ("a cast to a type other than an integral or "
2784 "enumeration type cannot appear in a "
2785 "constant-expression");
2786 return true;
2787 case NIC_TYPEID:
2788 error ("%<typeid%> operator "
2789 "cannot appear in a constant-expression");
2790 return true;
2791 case NIC_NCC:
2792 error ("non-constant compound literals "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_FUNC_CALL:
2796 error ("a function call "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_INC:
2800 error ("an increment "
2801 "cannot appear in a constant-expression");
2802 return true;
2803 case NIC_DEC:
2804 error ("an decrement "
2805 "cannot appear in a constant-expression");
2806 return true;
2807 case NIC_ARRAY_REF:
2808 error ("an array reference "
2809 "cannot appear in a constant-expression");
2810 return true;
2811 case NIC_ADDR_LABEL:
2812 error ("the address of a label "
2813 "cannot appear in a constant-expression");
2814 return true;
2815 case NIC_OVERLOADED:
2816 error ("calls to overloaded operators "
2817 "cannot appear in a constant-expression");
2818 return true;
2819 case NIC_ASSIGNMENT:
2820 error ("an assignment cannot appear in a constant-expression");
2821 return true;
2822 case NIC_COMMA:
2823 error ("a comma operator "
2824 "cannot appear in a constant-expression");
2825 return true;
2826 case NIC_CONSTRUCTOR:
2827 error ("a call to a constructor "
2828 "cannot appear in a constant-expression");
2829 return true;
2830 case NIC_TRANSACTION:
2831 error ("a transaction expression "
2832 "cannot appear in a constant-expression");
2833 return true;
2834 case NIC_THIS:
2835 msg = "this";
2836 break;
2837 case NIC_FUNC_NAME:
2838 msg = "__FUNCTION__";
2839 break;
2840 case NIC_PRETTY_FUNC:
2841 msg = "__PRETTY_FUNCTION__";
2842 break;
2843 case NIC_C99_FUNC:
2844 msg = "__func__";
2845 break;
2846 case NIC_VA_ARG:
2847 msg = "va_arg";
2848 break;
2849 case NIC_ARROW:
2850 msg = "->";
2851 break;
2852 case NIC_POINT:
2853 msg = ".";
2854 break;
2855 case NIC_STAR:
2856 msg = "*";
2857 break;
2858 case NIC_ADDR:
2859 msg = "&";
2860 break;
2861 case NIC_PREINCREMENT:
2862 msg = "++";
2863 break;
2864 case NIC_PREDECREMENT:
2865 msg = "--";
2866 break;
2867 case NIC_NEW:
2868 msg = "new";
2869 break;
2870 case NIC_DEL:
2871 msg = "delete";
2872 break;
2873 default:
2874 gcc_unreachable ();
2876 if (msg)
2877 error ("%qs cannot appear in a constant-expression", msg);
2878 return true;
2881 return false;
2884 /* Emit a diagnostic for an invalid type name. SCOPE is the
2885 qualifying scope (or NULL, if none) for ID. This function commits
2886 to the current active tentative parse, if any. (Otherwise, the
2887 problematic construct might be encountered again later, resulting
2888 in duplicate error messages.) LOCATION is the location of ID. */
2890 static void
2891 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2892 tree scope, tree id,
2893 location_t location)
2895 tree decl, old_scope, ambiguous_decls;
2896 cp_parser_commit_to_tentative_parse (parser);
2897 /* Try to lookup the identifier. */
2898 old_scope = parser->scope;
2899 parser->scope = scope;
2900 decl = cp_parser_lookup_name (parser, id, none_type,
2901 /*is_template=*/false,
2902 /*is_namespace=*/false,
2903 /*check_dependency=*/true,
2904 &ambiguous_decls, location);
2905 parser->scope = old_scope;
2906 if (ambiguous_decls)
2907 /* If the lookup was ambiguous, an error will already have
2908 been issued. */
2909 return;
2910 /* If the lookup found a template-name, it means that the user forgot
2911 to specify an argument list. Emit a useful error message. */
2912 if (TREE_CODE (decl) == TEMPLATE_DECL)
2913 error_at (location,
2914 "invalid use of template-name %qE without an argument list",
2915 decl);
2916 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2917 error_at (location, "invalid use of destructor %qD as a type", id);
2918 else if (TREE_CODE (decl) == TYPE_DECL)
2919 /* Something like 'unsigned A a;' */
2920 error_at (location, "invalid combination of multiple type-specifiers");
2921 else if (!parser->scope)
2923 /* Issue an error message. */
2924 error_at (location, "%qE does not name a type", id);
2925 /* If we're in a template class, it's possible that the user was
2926 referring to a type from a base class. For example:
2928 template <typename T> struct A { typedef T X; };
2929 template <typename T> struct B : public A<T> { X x; };
2931 The user should have said "typename A<T>::X". */
2932 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2933 inform (location, "C++11 %<constexpr%> only available with "
2934 "-std=c++11 or -std=gnu++11");
2935 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2936 inform (location, "C++11 %<noexcept%> only available with "
2937 "-std=c++11 or -std=gnu++11");
2938 else if (cxx_dialect < cxx11
2939 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2940 inform (location, "C++11 %<thread_local%> only available with "
2941 "-std=c++11 or -std=gnu++11");
2942 else if (processing_template_decl && current_class_type
2943 && TYPE_BINFO (current_class_type))
2945 tree b;
2947 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2949 b = TREE_CHAIN (b))
2951 tree base_type = BINFO_TYPE (b);
2952 if (CLASS_TYPE_P (base_type)
2953 && dependent_type_p (base_type))
2955 tree field;
2956 /* Go from a particular instantiation of the
2957 template (which will have an empty TYPE_FIELDs),
2958 to the main version. */
2959 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2960 for (field = TYPE_FIELDS (base_type);
2961 field;
2962 field = DECL_CHAIN (field))
2963 if (TREE_CODE (field) == TYPE_DECL
2964 && DECL_NAME (field) == id)
2966 inform (location,
2967 "(perhaps %<typename %T::%E%> was intended)",
2968 BINFO_TYPE (b), id);
2969 break;
2971 if (field)
2972 break;
2977 /* Here we diagnose qualified-ids where the scope is actually correct,
2978 but the identifier does not resolve to a valid type name. */
2979 else if (parser->scope != error_mark_node)
2981 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2983 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2984 error_at (location_of (id),
2985 "%qE in namespace %qE does not name a template type",
2986 id, parser->scope);
2987 else
2988 error_at (location_of (id),
2989 "%qE in namespace %qE does not name a type",
2990 id, parser->scope);
2992 else if (CLASS_TYPE_P (parser->scope)
2993 && constructor_name_p (id, parser->scope))
2995 /* A<T>::A<T>() */
2996 error_at (location, "%<%T::%E%> names the constructor, not"
2997 " the type", parser->scope, id);
2998 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2999 error_at (location, "and %qT has no template constructors",
3000 parser->scope);
3002 else if (TYPE_P (parser->scope)
3003 && dependent_scope_p (parser->scope))
3004 error_at (location, "need %<typename%> before %<%T::%E%> because "
3005 "%qT is a dependent scope",
3006 parser->scope, id, parser->scope);
3007 else if (TYPE_P (parser->scope))
3009 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3010 error_at (location_of (id),
3011 "%qE in %q#T does not name a template type",
3012 id, parser->scope);
3013 else
3014 error_at (location_of (id),
3015 "%qE in %q#T does not name a type",
3016 id, parser->scope);
3018 else
3019 gcc_unreachable ();
3023 /* Check for a common situation where a type-name should be present,
3024 but is not, and issue a sensible error message. Returns true if an
3025 invalid type-name was detected.
3027 The situation handled by this function are variable declarations of the
3028 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3029 Usually, `ID' should name a type, but if we got here it means that it
3030 does not. We try to emit the best possible error message depending on
3031 how exactly the id-expression looks like. */
3033 static bool
3034 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3036 tree id;
3037 cp_token *token = cp_lexer_peek_token (parser->lexer);
3039 /* Avoid duplicate error about ambiguous lookup. */
3040 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3042 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3043 if (next->type == CPP_NAME && next->error_reported)
3044 goto out;
3047 cp_parser_parse_tentatively (parser);
3048 id = cp_parser_id_expression (parser,
3049 /*template_keyword_p=*/false,
3050 /*check_dependency_p=*/true,
3051 /*template_p=*/NULL,
3052 /*declarator_p=*/true,
3053 /*optional_p=*/false);
3054 /* If the next token is a (, this is a function with no explicit return
3055 type, i.e. constructor, destructor or conversion op. */
3056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3057 || TREE_CODE (id) == TYPE_DECL)
3059 cp_parser_abort_tentative_parse (parser);
3060 return false;
3062 if (!cp_parser_parse_definitely (parser))
3063 return false;
3065 /* Emit a diagnostic for the invalid type. */
3066 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3067 id, token->location);
3068 out:
3069 /* If we aren't in the middle of a declarator (i.e. in a
3070 parameter-declaration-clause), skip to the end of the declaration;
3071 there's no point in trying to process it. */
3072 if (!parser->in_declarator_p)
3073 cp_parser_skip_to_end_of_block_or_statement (parser);
3074 return true;
3077 /* Consume tokens up to, and including, the next non-nested closing `)'.
3078 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3079 are doing error recovery. Returns -1 if OR_COMMA is true and we
3080 found an unnested comma. */
3082 static int
3083 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3084 bool recovering,
3085 bool or_comma,
3086 bool consume_paren)
3088 unsigned paren_depth = 0;
3089 unsigned brace_depth = 0;
3090 unsigned square_depth = 0;
3092 if (recovering && !or_comma
3093 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3094 return 0;
3096 while (true)
3098 cp_token * token = cp_lexer_peek_token (parser->lexer);
3100 switch (token->type)
3102 case CPP_EOF:
3103 case CPP_PRAGMA_EOL:
3104 /* If we've run out of tokens, then there is no closing `)'. */
3105 return 0;
3107 /* This is good for lambda expression capture-lists. */
3108 case CPP_OPEN_SQUARE:
3109 ++square_depth;
3110 break;
3111 case CPP_CLOSE_SQUARE:
3112 if (!square_depth--)
3113 return 0;
3114 break;
3116 case CPP_SEMICOLON:
3117 /* This matches the processing in skip_to_end_of_statement. */
3118 if (!brace_depth)
3119 return 0;
3120 break;
3122 case CPP_OPEN_BRACE:
3123 ++brace_depth;
3124 break;
3125 case CPP_CLOSE_BRACE:
3126 if (!brace_depth--)
3127 return 0;
3128 break;
3130 case CPP_COMMA:
3131 if (recovering && or_comma && !brace_depth && !paren_depth
3132 && !square_depth)
3133 return -1;
3134 break;
3136 case CPP_OPEN_PAREN:
3137 if (!brace_depth)
3138 ++paren_depth;
3139 break;
3141 case CPP_CLOSE_PAREN:
3142 if (!brace_depth && !paren_depth--)
3144 if (consume_paren)
3145 cp_lexer_consume_token (parser->lexer);
3146 return 1;
3148 break;
3150 default:
3151 break;
3154 /* Consume the token. */
3155 cp_lexer_consume_token (parser->lexer);
3159 /* Consume tokens until we reach the end of the current statement.
3160 Normally, that will be just before consuming a `;'. However, if a
3161 non-nested `}' comes first, then we stop before consuming that. */
3163 static void
3164 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3166 unsigned nesting_depth = 0;
3168 /* Unwind generic function template scope if necessary. */
3169 if (parser->fully_implicit_function_template_p)
3170 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3172 while (true)
3174 cp_token *token = cp_lexer_peek_token (parser->lexer);
3176 switch (token->type)
3178 case CPP_EOF:
3179 case CPP_PRAGMA_EOL:
3180 /* If we've run out of tokens, stop. */
3181 return;
3183 case CPP_SEMICOLON:
3184 /* If the next token is a `;', we have reached the end of the
3185 statement. */
3186 if (!nesting_depth)
3187 return;
3188 break;
3190 case CPP_CLOSE_BRACE:
3191 /* If this is a non-nested '}', stop before consuming it.
3192 That way, when confronted with something like:
3194 { 3 + }
3196 we stop before consuming the closing '}', even though we
3197 have not yet reached a `;'. */
3198 if (nesting_depth == 0)
3199 return;
3201 /* If it is the closing '}' for a block that we have
3202 scanned, stop -- but only after consuming the token.
3203 That way given:
3205 void f g () { ... }
3206 typedef int I;
3208 we will stop after the body of the erroneously declared
3209 function, but before consuming the following `typedef'
3210 declaration. */
3211 if (--nesting_depth == 0)
3213 cp_lexer_consume_token (parser->lexer);
3214 return;
3217 case CPP_OPEN_BRACE:
3218 ++nesting_depth;
3219 break;
3221 default:
3222 break;
3225 /* Consume the token. */
3226 cp_lexer_consume_token (parser->lexer);
3230 /* This function is called at the end of a statement or declaration.
3231 If the next token is a semicolon, it is consumed; otherwise, error
3232 recovery is attempted. */
3234 static void
3235 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3237 /* Look for the trailing `;'. */
3238 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3240 /* If there is additional (erroneous) input, skip to the end of
3241 the statement. */
3242 cp_parser_skip_to_end_of_statement (parser);
3243 /* If the next token is now a `;', consume it. */
3244 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3245 cp_lexer_consume_token (parser->lexer);
3249 /* Skip tokens until we have consumed an entire block, or until we
3250 have consumed a non-nested `;'. */
3252 static void
3253 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3255 int nesting_depth = 0;
3257 /* Unwind generic function template scope if necessary. */
3258 if (parser->fully_implicit_function_template_p)
3259 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3261 while (nesting_depth >= 0)
3263 cp_token *token = cp_lexer_peek_token (parser->lexer);
3265 switch (token->type)
3267 case CPP_EOF:
3268 case CPP_PRAGMA_EOL:
3269 /* If we've run out of tokens, stop. */
3270 return;
3272 case CPP_SEMICOLON:
3273 /* Stop if this is an unnested ';'. */
3274 if (!nesting_depth)
3275 nesting_depth = -1;
3276 break;
3278 case CPP_CLOSE_BRACE:
3279 /* Stop if this is an unnested '}', or closes the outermost
3280 nesting level. */
3281 nesting_depth--;
3282 if (nesting_depth < 0)
3283 return;
3284 if (!nesting_depth)
3285 nesting_depth = -1;
3286 break;
3288 case CPP_OPEN_BRACE:
3289 /* Nest. */
3290 nesting_depth++;
3291 break;
3293 default:
3294 break;
3297 /* Consume the token. */
3298 cp_lexer_consume_token (parser->lexer);
3302 /* Skip tokens until a non-nested closing curly brace is the next
3303 token, or there are no more tokens. Return true in the first case,
3304 false otherwise. */
3306 static bool
3307 cp_parser_skip_to_closing_brace (cp_parser *parser)
3309 unsigned nesting_depth = 0;
3311 while (true)
3313 cp_token *token = cp_lexer_peek_token (parser->lexer);
3315 switch (token->type)
3317 case CPP_EOF:
3318 case CPP_PRAGMA_EOL:
3319 /* If we've run out of tokens, stop. */
3320 return false;
3322 case CPP_CLOSE_BRACE:
3323 /* If the next token is a non-nested `}', then we have reached
3324 the end of the current block. */
3325 if (nesting_depth-- == 0)
3326 return true;
3327 break;
3329 case CPP_OPEN_BRACE:
3330 /* If it the next token is a `{', then we are entering a new
3331 block. Consume the entire block. */
3332 ++nesting_depth;
3333 break;
3335 default:
3336 break;
3339 /* Consume the token. */
3340 cp_lexer_consume_token (parser->lexer);
3344 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3345 parameter is the PRAGMA token, allowing us to purge the entire pragma
3346 sequence. */
3348 static void
3349 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3351 cp_token *token;
3353 parser->lexer->in_pragma = false;
3356 token = cp_lexer_consume_token (parser->lexer);
3357 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3359 /* Ensure that the pragma is not parsed again. */
3360 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3363 /* Require pragma end of line, resyncing with it as necessary. The
3364 arguments are as for cp_parser_skip_to_pragma_eol. */
3366 static void
3367 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3369 parser->lexer->in_pragma = false;
3370 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3371 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3374 /* This is a simple wrapper around make_typename_type. When the id is
3375 an unresolved identifier node, we can provide a superior diagnostic
3376 using cp_parser_diagnose_invalid_type_name. */
3378 static tree
3379 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3380 tree id, location_t id_location)
3382 tree result;
3383 if (identifier_p (id))
3385 result = make_typename_type (scope, id, typename_type,
3386 /*complain=*/tf_none);
3387 if (result == error_mark_node)
3388 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3389 return result;
3391 return make_typename_type (scope, id, typename_type, tf_error);
3394 /* This is a wrapper around the
3395 make_{pointer,ptrmem,reference}_declarator functions that decides
3396 which one to call based on the CODE and CLASS_TYPE arguments. The
3397 CODE argument should be one of the values returned by
3398 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3399 appertain to the pointer or reference. */
3401 static cp_declarator *
3402 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3403 cp_cv_quals cv_qualifiers,
3404 cp_declarator *target,
3405 tree attributes)
3407 if (code == ERROR_MARK)
3408 return cp_error_declarator;
3410 if (code == INDIRECT_REF)
3411 if (class_type == NULL_TREE)
3412 return make_pointer_declarator (cv_qualifiers, target, attributes);
3413 else
3414 return make_ptrmem_declarator (cv_qualifiers, class_type,
3415 target, attributes);
3416 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3417 return make_reference_declarator (cv_qualifiers, target,
3418 false, attributes);
3419 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3420 return make_reference_declarator (cv_qualifiers, target,
3421 true, attributes);
3422 gcc_unreachable ();
3425 /* Create a new C++ parser. */
3427 static cp_parser *
3428 cp_parser_new (void)
3430 cp_parser *parser;
3431 cp_lexer *lexer;
3432 unsigned i;
3434 /* cp_lexer_new_main is called before doing GC allocation because
3435 cp_lexer_new_main might load a PCH file. */
3436 lexer = cp_lexer_new_main ();
3438 /* Initialize the binops_by_token so that we can get the tree
3439 directly from the token. */
3440 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3441 binops_by_token[binops[i].token_type] = binops[i];
3443 parser = ggc_cleared_alloc<cp_parser> ();
3444 parser->lexer = lexer;
3445 parser->context = cp_parser_context_new (NULL);
3447 /* For now, we always accept GNU extensions. */
3448 parser->allow_gnu_extensions_p = 1;
3450 /* The `>' token is a greater-than operator, not the end of a
3451 template-id. */
3452 parser->greater_than_is_operator_p = true;
3454 parser->default_arg_ok_p = true;
3456 /* We are not parsing a constant-expression. */
3457 parser->integral_constant_expression_p = false;
3458 parser->allow_non_integral_constant_expression_p = false;
3459 parser->non_integral_constant_expression_p = false;
3461 /* Local variable names are not forbidden. */
3462 parser->local_variables_forbidden_p = false;
3464 /* We are not processing an `extern "C"' declaration. */
3465 parser->in_unbraced_linkage_specification_p = false;
3467 /* We are not processing a declarator. */
3468 parser->in_declarator_p = false;
3470 /* We are not processing a template-argument-list. */
3471 parser->in_template_argument_list_p = false;
3473 /* We are not in an iteration statement. */
3474 parser->in_statement = 0;
3476 /* We are not in a switch statement. */
3477 parser->in_switch_statement_p = false;
3479 /* We are not parsing a type-id inside an expression. */
3480 parser->in_type_id_in_expr_p = false;
3482 /* Declarations aren't implicitly extern "C". */
3483 parser->implicit_extern_c = false;
3485 /* String literals should be translated to the execution character set. */
3486 parser->translate_strings_p = true;
3488 /* We are not parsing a function body. */
3489 parser->in_function_body = false;
3491 /* We can correct until told otherwise. */
3492 parser->colon_corrects_to_scope_p = true;
3494 /* The unparsed function queue is empty. */
3495 push_unparsed_function_queues (parser);
3497 /* There are no classes being defined. */
3498 parser->num_classes_being_defined = 0;
3500 /* No template parameters apply. */
3501 parser->num_template_parameter_lists = 0;
3503 /* Not declaring an implicit function template. */
3504 parser->auto_is_implicit_function_template_parm_p = false;
3505 parser->fully_implicit_function_template_p = false;
3506 parser->implicit_template_parms = 0;
3507 parser->implicit_template_scope = 0;
3509 return parser;
3512 /* Create a cp_lexer structure which will emit the tokens in CACHE
3513 and push it onto the parser's lexer stack. This is used for delayed
3514 parsing of in-class method bodies and default arguments, and should
3515 not be confused with tentative parsing. */
3516 static void
3517 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3519 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3520 lexer->next = parser->lexer;
3521 parser->lexer = lexer;
3523 /* Move the current source position to that of the first token in the
3524 new lexer. */
3525 cp_lexer_set_source_position_from_token (lexer->next_token);
3528 /* Pop the top lexer off the parser stack. This is never used for the
3529 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3530 static void
3531 cp_parser_pop_lexer (cp_parser *parser)
3533 cp_lexer *lexer = parser->lexer;
3534 parser->lexer = lexer->next;
3535 cp_lexer_destroy (lexer);
3537 /* Put the current source position back where it was before this
3538 lexer was pushed. */
3539 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3542 /* Lexical conventions [gram.lex] */
3544 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3545 identifier. */
3547 static tree
3548 cp_parser_identifier (cp_parser* parser)
3550 cp_token *token;
3552 /* Look for the identifier. */
3553 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3554 /* Return the value. */
3555 return token ? token->u.value : error_mark_node;
3558 /* Parse a sequence of adjacent string constants. Returns a
3559 TREE_STRING representing the combined, nul-terminated string
3560 constant. If TRANSLATE is true, translate the string to the
3561 execution character set. If WIDE_OK is true, a wide string is
3562 invalid here.
3564 C++98 [lex.string] says that if a narrow string literal token is
3565 adjacent to a wide string literal token, the behavior is undefined.
3566 However, C99 6.4.5p4 says that this results in a wide string literal.
3567 We follow C99 here, for consistency with the C front end.
3569 This code is largely lifted from lex_string() in c-lex.c.
3571 FUTURE: ObjC++ will need to handle @-strings here. */
3572 static tree
3573 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3574 bool lookup_udlit = true)
3576 tree value;
3577 size_t count;
3578 struct obstack str_ob;
3579 cpp_string str, istr, *strs;
3580 cp_token *tok;
3581 enum cpp_ttype type, curr_type;
3582 int have_suffix_p = 0;
3583 tree string_tree;
3584 tree suffix_id = NULL_TREE;
3585 bool curr_tok_is_userdef_p = false;
3587 tok = cp_lexer_peek_token (parser->lexer);
3588 if (!cp_parser_is_string_literal (tok))
3590 cp_parser_error (parser, "expected string-literal");
3591 return error_mark_node;
3594 if (cpp_userdef_string_p (tok->type))
3596 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3597 curr_type = cpp_userdef_string_remove_type (tok->type);
3598 curr_tok_is_userdef_p = true;
3600 else
3602 string_tree = tok->u.value;
3603 curr_type = tok->type;
3605 type = curr_type;
3607 /* Try to avoid the overhead of creating and destroying an obstack
3608 for the common case of just one string. */
3609 if (!cp_parser_is_string_literal
3610 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3612 cp_lexer_consume_token (parser->lexer);
3614 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3615 str.len = TREE_STRING_LENGTH (string_tree);
3616 count = 1;
3618 if (curr_tok_is_userdef_p)
3620 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3621 have_suffix_p = 1;
3622 curr_type = cpp_userdef_string_remove_type (tok->type);
3624 else
3625 curr_type = tok->type;
3627 strs = &str;
3629 else
3631 gcc_obstack_init (&str_ob);
3632 count = 0;
3636 cp_lexer_consume_token (parser->lexer);
3637 count++;
3638 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3639 str.len = TREE_STRING_LENGTH (string_tree);
3641 if (curr_tok_is_userdef_p)
3643 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3644 if (have_suffix_p == 0)
3646 suffix_id = curr_suffix_id;
3647 have_suffix_p = 1;
3649 else if (have_suffix_p == 1
3650 && curr_suffix_id != suffix_id)
3652 error ("inconsistent user-defined literal suffixes"
3653 " %qD and %qD in string literal",
3654 suffix_id, curr_suffix_id);
3655 have_suffix_p = -1;
3657 curr_type = cpp_userdef_string_remove_type (tok->type);
3659 else
3660 curr_type = tok->type;
3662 if (type != curr_type)
3664 if (type == CPP_STRING)
3665 type = curr_type;
3666 else if (curr_type != CPP_STRING)
3667 error_at (tok->location,
3668 "unsupported non-standard concatenation "
3669 "of string literals");
3672 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3674 tok = cp_lexer_peek_token (parser->lexer);
3675 if (cpp_userdef_string_p (tok->type))
3677 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3678 curr_type = cpp_userdef_string_remove_type (tok->type);
3679 curr_tok_is_userdef_p = true;
3681 else
3683 string_tree = tok->u.value;
3684 curr_type = tok->type;
3685 curr_tok_is_userdef_p = false;
3688 while (cp_parser_is_string_literal (tok));
3690 strs = (cpp_string *) obstack_finish (&str_ob);
3693 if (type != CPP_STRING && !wide_ok)
3695 cp_parser_error (parser, "a wide string is invalid in this context");
3696 type = CPP_STRING;
3699 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3700 (parse_in, strs, count, &istr, type))
3702 value = build_string (istr.len, (const char *)istr.text);
3703 free (CONST_CAST (unsigned char *, istr.text));
3705 switch (type)
3707 default:
3708 case CPP_STRING:
3709 case CPP_UTF8STRING:
3710 TREE_TYPE (value) = char_array_type_node;
3711 break;
3712 case CPP_STRING16:
3713 TREE_TYPE (value) = char16_array_type_node;
3714 break;
3715 case CPP_STRING32:
3716 TREE_TYPE (value) = char32_array_type_node;
3717 break;
3718 case CPP_WSTRING:
3719 TREE_TYPE (value) = wchar_array_type_node;
3720 break;
3723 value = fix_string_type (value);
3725 if (have_suffix_p)
3727 tree literal = build_userdef_literal (suffix_id, value,
3728 OT_NONE, NULL_TREE);
3729 if (lookup_udlit)
3730 value = cp_parser_userdef_string_literal (literal);
3731 else
3732 value = literal;
3735 else
3736 /* cpp_interpret_string has issued an error. */
3737 value = error_mark_node;
3739 if (count > 1)
3740 obstack_free (&str_ob, 0);
3742 return value;
3745 /* Look up a literal operator with the name and the exact arguments. */
3747 static tree
3748 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3750 tree decl, fns;
3751 decl = lookup_name (name);
3752 if (!decl || !is_overloaded_fn (decl))
3753 return error_mark_node;
3755 for (fns = decl; fns; fns = OVL_NEXT (fns))
3757 unsigned int ix;
3758 bool found = true;
3759 tree fn = OVL_CURRENT (fns);
3760 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3761 if (parmtypes != NULL_TREE)
3763 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3764 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3766 tree tparm = TREE_VALUE (parmtypes);
3767 tree targ = TREE_TYPE ((*args)[ix]);
3768 bool ptr = TYPE_PTR_P (tparm);
3769 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3770 if ((ptr || arr || !same_type_p (tparm, targ))
3771 && (!ptr || !arr
3772 || !same_type_p (TREE_TYPE (tparm),
3773 TREE_TYPE (targ))))
3774 found = false;
3776 if (found
3777 && ix == vec_safe_length (args)
3778 /* May be this should be sufficient_parms_p instead,
3779 depending on how exactly should user-defined literals
3780 work in presence of default arguments on the literal
3781 operator parameters. */
3782 && parmtypes == void_list_node)
3783 return fn;
3787 return error_mark_node;
3790 /* Parse a user-defined char constant. Returns a call to a user-defined
3791 literal operator taking the character as an argument. */
3793 static tree
3794 cp_parser_userdef_char_literal (cp_parser *parser)
3796 cp_token *token = cp_lexer_consume_token (parser->lexer);
3797 tree literal = token->u.value;
3798 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3799 tree value = USERDEF_LITERAL_VALUE (literal);
3800 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3801 tree decl, result;
3803 /* Build up a call to the user-defined operator */
3804 /* Lookup the name we got back from the id-expression. */
3805 vec<tree, va_gc> *args = make_tree_vector ();
3806 vec_safe_push (args, value);
3807 decl = lookup_literal_operator (name, args);
3808 if (!decl || decl == error_mark_node)
3810 error ("unable to find character literal operator %qD with %qT argument",
3811 name, TREE_TYPE (value));
3812 release_tree_vector (args);
3813 return error_mark_node;
3815 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3816 release_tree_vector (args);
3817 if (result != error_mark_node)
3818 return result;
3820 error ("unable to find character literal operator %qD with %qT argument",
3821 name, TREE_TYPE (value));
3822 return error_mark_node;
3825 /* A subroutine of cp_parser_userdef_numeric_literal to
3826 create a char... template parameter pack from a string node. */
3828 static tree
3829 make_char_string_pack (tree value)
3831 tree charvec;
3832 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3833 const char *str = TREE_STRING_POINTER (value);
3834 int i, len = TREE_STRING_LENGTH (value) - 1;
3835 tree argvec = make_tree_vec (1);
3837 /* Fill in CHARVEC with all of the parameters. */
3838 charvec = make_tree_vec (len);
3839 for (i = 0; i < len; ++i)
3840 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3842 /* Build the argument packs. */
3843 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3844 TREE_TYPE (argpack) = char_type_node;
3846 TREE_VEC_ELT (argvec, 0) = argpack;
3848 return argvec;
3851 /* A subroutine of cp_parser_userdef_numeric_literal to
3852 create a char... template parameter pack from a string node. */
3854 static tree
3855 make_string_pack (tree value)
3857 tree charvec;
3858 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3859 const unsigned char *str
3860 = (const unsigned char *) TREE_STRING_POINTER (value);
3861 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3862 int len = TREE_STRING_LENGTH (value) / sz - 1;
3863 tree argvec = make_tree_vec (2);
3865 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3866 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3868 /* First template parm is character type. */
3869 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3871 /* Fill in CHARVEC with all of the parameters. */
3872 charvec = make_tree_vec (len);
3873 for (int i = 0; i < len; ++i)
3874 TREE_VEC_ELT (charvec, i)
3875 = double_int_to_tree (str_char_type_node,
3876 double_int::from_buffer (str + i * sz, sz));
3878 /* Build the argument packs. */
3879 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3880 TREE_TYPE (argpack) = str_char_type_node;
3882 TREE_VEC_ELT (argvec, 1) = argpack;
3884 return argvec;
3887 /* Parse a user-defined numeric constant. returns a call to a user-defined
3888 literal operator. */
3890 static tree
3891 cp_parser_userdef_numeric_literal (cp_parser *parser)
3893 cp_token *token = cp_lexer_consume_token (parser->lexer);
3894 tree literal = token->u.value;
3895 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3896 tree value = USERDEF_LITERAL_VALUE (literal);
3897 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3898 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3899 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3900 tree decl, result;
3901 vec<tree, va_gc> *args;
3903 /* Look for a literal operator taking the exact type of numeric argument
3904 as the literal value. */
3905 args = make_tree_vector ();
3906 vec_safe_push (args, value);
3907 decl = lookup_literal_operator (name, args);
3908 if (decl && decl != error_mark_node)
3910 result = finish_call_expr (decl, &args, false, true, tf_none);
3911 if (result != error_mark_node)
3913 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3914 warning_at (token->location, OPT_Woverflow,
3915 "integer literal exceeds range of %qT type",
3916 long_long_unsigned_type_node);
3917 else
3919 if (overflow > 0)
3920 warning_at (token->location, OPT_Woverflow,
3921 "floating literal exceeds range of %qT type",
3922 long_double_type_node);
3923 else if (overflow < 0)
3924 warning_at (token->location, OPT_Woverflow,
3925 "floating literal truncated to zero");
3927 release_tree_vector (args);
3928 return result;
3931 release_tree_vector (args);
3933 /* If the numeric argument didn't work, look for a raw literal
3934 operator taking a const char* argument consisting of the number
3935 in string format. */
3936 args = make_tree_vector ();
3937 vec_safe_push (args, num_string);
3938 decl = lookup_literal_operator (name, args);
3939 if (decl && decl != error_mark_node)
3941 result = finish_call_expr (decl, &args, false, true, tf_none);
3942 if (result != error_mark_node)
3944 release_tree_vector (args);
3945 return result;
3948 release_tree_vector (args);
3950 /* If the raw literal didn't work, look for a non-type template
3951 function with parameter pack char.... Call the function with
3952 template parameter characters representing the number. */
3953 args = make_tree_vector ();
3954 decl = lookup_literal_operator (name, args);
3955 if (decl && decl != error_mark_node)
3957 tree tmpl_args = make_char_string_pack (num_string);
3958 decl = lookup_template_function (decl, tmpl_args);
3959 result = finish_call_expr (decl, &args, false, true, tf_none);
3960 if (result != error_mark_node)
3962 release_tree_vector (args);
3963 return result;
3966 release_tree_vector (args);
3968 error ("unable to find numeric literal operator %qD", name);
3969 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3970 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3971 "to enable more built-in suffixes");
3972 return error_mark_node;
3975 /* Parse a user-defined string constant. Returns a call to a user-defined
3976 literal operator taking a character pointer and the length of the string
3977 as arguments. */
3979 static tree
3980 cp_parser_userdef_string_literal (tree literal)
3982 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3983 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3984 tree value = USERDEF_LITERAL_VALUE (literal);
3985 int len = TREE_STRING_LENGTH (value)
3986 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3987 tree decl, result;
3988 vec<tree, va_gc> *args;
3990 /* Look for a template function with typename parameter CharT
3991 and parameter pack CharT... Call the function with
3992 template parameter characters representing the string. */
3993 args = make_tree_vector ();
3994 decl = lookup_literal_operator (name, args);
3995 if (decl && decl != error_mark_node)
3997 tree tmpl_args = make_string_pack (value);
3998 decl = lookup_template_function (decl, tmpl_args);
3999 result = finish_call_expr (decl, &args, false, true, tf_none);
4000 if (result != error_mark_node)
4002 release_tree_vector (args);
4003 return result;
4006 release_tree_vector (args);
4008 /* Build up a call to the user-defined operator */
4009 /* Lookup the name we got back from the id-expression. */
4010 args = make_tree_vector ();
4011 vec_safe_push (args, value);
4012 vec_safe_push (args, build_int_cst (size_type_node, len));
4013 decl = lookup_name (name);
4014 if (!decl || decl == error_mark_node)
4016 error ("unable to find string literal operator %qD", name);
4017 release_tree_vector (args);
4018 return error_mark_node;
4020 result = finish_call_expr (decl, &args, false, true, tf_none);
4021 release_tree_vector (args);
4022 if (result != error_mark_node)
4023 return result;
4025 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4026 name, TREE_TYPE (value), size_type_node);
4027 return error_mark_node;
4031 /* Basic concepts [gram.basic] */
4033 /* Parse a translation-unit.
4035 translation-unit:
4036 declaration-seq [opt]
4038 Returns TRUE if all went well. */
4040 static bool
4041 cp_parser_translation_unit (cp_parser* parser)
4043 /* The address of the first non-permanent object on the declarator
4044 obstack. */
4045 static void *declarator_obstack_base;
4047 bool success;
4049 /* Create the declarator obstack, if necessary. */
4050 if (!cp_error_declarator)
4052 gcc_obstack_init (&declarator_obstack);
4053 /* Create the error declarator. */
4054 cp_error_declarator = make_declarator (cdk_error);
4055 /* Create the empty parameter list. */
4056 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4057 /* Remember where the base of the declarator obstack lies. */
4058 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4061 cp_parser_declaration_seq_opt (parser);
4063 /* If there are no tokens left then all went well. */
4064 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4066 /* Get rid of the token array; we don't need it any more. */
4067 cp_lexer_destroy (parser->lexer);
4068 parser->lexer = NULL;
4070 /* This file might have been a context that's implicitly extern
4071 "C". If so, pop the lang context. (Only relevant for PCH.) */
4072 if (parser->implicit_extern_c)
4074 pop_lang_context ();
4075 parser->implicit_extern_c = false;
4078 /* Finish up. */
4079 finish_translation_unit ();
4081 success = true;
4083 else
4085 cp_parser_error (parser, "expected declaration");
4086 success = false;
4089 /* Make sure the declarator obstack was fully cleaned up. */
4090 gcc_assert (obstack_next_free (&declarator_obstack)
4091 == declarator_obstack_base);
4093 /* All went well. */
4094 return success;
4097 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4098 decltype context. */
4100 static inline tsubst_flags_t
4101 complain_flags (bool decltype_p)
4103 tsubst_flags_t complain = tf_warning_or_error;
4104 if (decltype_p)
4105 complain |= tf_decltype;
4106 return complain;
4109 /* Expressions [gram.expr] */
4111 /* Parse a primary-expression.
4113 primary-expression:
4114 literal
4115 this
4116 ( expression )
4117 id-expression
4118 lambda-expression (C++11)
4120 GNU Extensions:
4122 primary-expression:
4123 ( compound-statement )
4124 __builtin_va_arg ( assignment-expression , type-id )
4125 __builtin_offsetof ( type-id , offsetof-expression )
4127 C++ Extensions:
4128 __has_nothrow_assign ( type-id )
4129 __has_nothrow_constructor ( type-id )
4130 __has_nothrow_copy ( type-id )
4131 __has_trivial_assign ( type-id )
4132 __has_trivial_constructor ( type-id )
4133 __has_trivial_copy ( type-id )
4134 __has_trivial_destructor ( type-id )
4135 __has_virtual_destructor ( type-id )
4136 __is_abstract ( type-id )
4137 __is_base_of ( type-id , type-id )
4138 __is_class ( type-id )
4139 __is_convertible_to ( type-id , type-id )
4140 __is_empty ( type-id )
4141 __is_enum ( type-id )
4142 __is_final ( type-id )
4143 __is_literal_type ( type-id )
4144 __is_pod ( type-id )
4145 __is_polymorphic ( type-id )
4146 __is_std_layout ( type-id )
4147 __is_trivial ( type-id )
4148 __is_union ( type-id )
4150 Objective-C++ Extension:
4152 primary-expression:
4153 objc-expression
4155 literal:
4156 __null
4158 ADDRESS_P is true iff this expression was immediately preceded by
4159 "&" and therefore might denote a pointer-to-member. CAST_P is true
4160 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4161 true iff this expression is a template argument.
4163 Returns a representation of the expression. Upon return, *IDK
4164 indicates what kind of id-expression (if any) was present. */
4166 static tree
4167 cp_parser_primary_expression (cp_parser *parser,
4168 bool address_p,
4169 bool cast_p,
4170 bool template_arg_p,
4171 bool decltype_p,
4172 cp_id_kind *idk)
4174 cp_token *token = NULL;
4176 /* Assume the primary expression is not an id-expression. */
4177 *idk = CP_ID_KIND_NONE;
4179 /* Peek at the next token. */
4180 token = cp_lexer_peek_token (parser->lexer);
4181 switch (token->type)
4183 /* literal:
4184 integer-literal
4185 character-literal
4186 floating-literal
4187 string-literal
4188 boolean-literal
4189 pointer-literal
4190 user-defined-literal */
4191 case CPP_CHAR:
4192 case CPP_CHAR16:
4193 case CPP_CHAR32:
4194 case CPP_WCHAR:
4195 case CPP_NUMBER:
4196 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4197 return cp_parser_userdef_numeric_literal (parser);
4198 token = cp_lexer_consume_token (parser->lexer);
4199 if (TREE_CODE (token->u.value) == FIXED_CST)
4201 error_at (token->location,
4202 "fixed-point types not supported in C++");
4203 return error_mark_node;
4205 /* Floating-point literals are only allowed in an integral
4206 constant expression if they are cast to an integral or
4207 enumeration type. */
4208 if (TREE_CODE (token->u.value) == REAL_CST
4209 && parser->integral_constant_expression_p
4210 && pedantic)
4212 /* CAST_P will be set even in invalid code like "int(2.7 +
4213 ...)". Therefore, we have to check that the next token
4214 is sure to end the cast. */
4215 if (cast_p)
4217 cp_token *next_token;
4219 next_token = cp_lexer_peek_token (parser->lexer);
4220 if (/* The comma at the end of an
4221 enumerator-definition. */
4222 next_token->type != CPP_COMMA
4223 /* The curly brace at the end of an enum-specifier. */
4224 && next_token->type != CPP_CLOSE_BRACE
4225 /* The end of a statement. */
4226 && next_token->type != CPP_SEMICOLON
4227 /* The end of the cast-expression. */
4228 && next_token->type != CPP_CLOSE_PAREN
4229 /* The end of an array bound. */
4230 && next_token->type != CPP_CLOSE_SQUARE
4231 /* The closing ">" in a template-argument-list. */
4232 && (next_token->type != CPP_GREATER
4233 || parser->greater_than_is_operator_p)
4234 /* C++0x only: A ">>" treated like two ">" tokens,
4235 in a template-argument-list. */
4236 && (next_token->type != CPP_RSHIFT
4237 || (cxx_dialect == cxx98)
4238 || parser->greater_than_is_operator_p))
4239 cast_p = false;
4242 /* If we are within a cast, then the constraint that the
4243 cast is to an integral or enumeration type will be
4244 checked at that point. If we are not within a cast, then
4245 this code is invalid. */
4246 if (!cast_p)
4247 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4249 return token->u.value;
4251 case CPP_CHAR_USERDEF:
4252 case CPP_CHAR16_USERDEF:
4253 case CPP_CHAR32_USERDEF:
4254 case CPP_WCHAR_USERDEF:
4255 return cp_parser_userdef_char_literal (parser);
4257 case CPP_STRING:
4258 case CPP_STRING16:
4259 case CPP_STRING32:
4260 case CPP_WSTRING:
4261 case CPP_UTF8STRING:
4262 case CPP_STRING_USERDEF:
4263 case CPP_STRING16_USERDEF:
4264 case CPP_STRING32_USERDEF:
4265 case CPP_WSTRING_USERDEF:
4266 case CPP_UTF8STRING_USERDEF:
4267 /* ??? Should wide strings be allowed when parser->translate_strings_p
4268 is false (i.e. in attributes)? If not, we can kill the third
4269 argument to cp_parser_string_literal. */
4270 return cp_parser_string_literal (parser,
4271 parser->translate_strings_p,
4272 true);
4274 case CPP_OPEN_PAREN:
4276 tree expr;
4277 bool saved_greater_than_is_operator_p;
4279 /* Consume the `('. */
4280 cp_lexer_consume_token (parser->lexer);
4281 /* Within a parenthesized expression, a `>' token is always
4282 the greater-than operator. */
4283 saved_greater_than_is_operator_p
4284 = parser->greater_than_is_operator_p;
4285 parser->greater_than_is_operator_p = true;
4286 /* If we see `( { ' then we are looking at the beginning of
4287 a GNU statement-expression. */
4288 if (cp_parser_allow_gnu_extensions_p (parser)
4289 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4291 /* Statement-expressions are not allowed by the standard. */
4292 pedwarn (token->location, OPT_Wpedantic,
4293 "ISO C++ forbids braced-groups within expressions");
4295 /* And they're not allowed outside of a function-body; you
4296 cannot, for example, write:
4298 int i = ({ int j = 3; j + 1; });
4300 at class or namespace scope. */
4301 if (!parser->in_function_body
4302 || parser->in_template_argument_list_p)
4304 error_at (token->location,
4305 "statement-expressions are not allowed outside "
4306 "functions nor in template-argument lists");
4307 cp_parser_skip_to_end_of_block_or_statement (parser);
4308 expr = error_mark_node;
4310 else
4312 /* Start the statement-expression. */
4313 expr = begin_stmt_expr ();
4314 /* Parse the compound-statement. */
4315 cp_parser_compound_statement (parser, expr, false, false);
4316 /* Finish up. */
4317 expr = finish_stmt_expr (expr, false);
4320 else
4322 /* Parse the parenthesized expression. */
4323 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4324 /* Let the front end know that this expression was
4325 enclosed in parentheses. This matters in case, for
4326 example, the expression is of the form `A::B', since
4327 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4328 not. */
4329 expr = finish_parenthesized_expr (expr);
4330 /* DR 705: Wrapping an unqualified name in parentheses
4331 suppresses arg-dependent lookup. We want to pass back
4332 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4333 (c++/37862), but none of the others. */
4334 if (*idk != CP_ID_KIND_QUALIFIED)
4335 *idk = CP_ID_KIND_NONE;
4337 /* The `>' token might be the end of a template-id or
4338 template-parameter-list now. */
4339 parser->greater_than_is_operator_p
4340 = saved_greater_than_is_operator_p;
4341 /* Consume the `)'. */
4342 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4343 cp_parser_skip_to_end_of_statement (parser);
4345 return expr;
4348 case CPP_OPEN_SQUARE:
4349 if (c_dialect_objc ())
4350 /* We have an Objective-C++ message. */
4351 return cp_parser_objc_expression (parser);
4353 tree lam = cp_parser_lambda_expression (parser);
4354 /* Don't warn about a failed tentative parse. */
4355 if (cp_parser_error_occurred (parser))
4356 return error_mark_node;
4357 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4358 return lam;
4361 case CPP_OBJC_STRING:
4362 if (c_dialect_objc ())
4363 /* We have an Objective-C++ string literal. */
4364 return cp_parser_objc_expression (parser);
4365 cp_parser_error (parser, "expected primary-expression");
4366 return error_mark_node;
4368 case CPP_KEYWORD:
4369 switch (token->keyword)
4371 /* These two are the boolean literals. */
4372 case RID_TRUE:
4373 cp_lexer_consume_token (parser->lexer);
4374 return boolean_true_node;
4375 case RID_FALSE:
4376 cp_lexer_consume_token (parser->lexer);
4377 return boolean_false_node;
4379 /* The `__null' literal. */
4380 case RID_NULL:
4381 cp_lexer_consume_token (parser->lexer);
4382 return null_node;
4384 /* The `nullptr' literal. */
4385 case RID_NULLPTR:
4386 cp_lexer_consume_token (parser->lexer);
4387 return nullptr_node;
4389 /* Recognize the `this' keyword. */
4390 case RID_THIS:
4391 cp_lexer_consume_token (parser->lexer);
4392 if (parser->local_variables_forbidden_p)
4394 error_at (token->location,
4395 "%<this%> may not be used in this context");
4396 return error_mark_node;
4398 /* Pointers cannot appear in constant-expressions. */
4399 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4400 return error_mark_node;
4401 return finish_this_expr ();
4403 /* The `operator' keyword can be the beginning of an
4404 id-expression. */
4405 case RID_OPERATOR:
4406 goto id_expression;
4408 case RID_FUNCTION_NAME:
4409 case RID_PRETTY_FUNCTION_NAME:
4410 case RID_C99_FUNCTION_NAME:
4412 non_integral_constant name;
4414 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4415 __func__ are the names of variables -- but they are
4416 treated specially. Therefore, they are handled here,
4417 rather than relying on the generic id-expression logic
4418 below. Grammatically, these names are id-expressions.
4420 Consume the token. */
4421 token = cp_lexer_consume_token (parser->lexer);
4423 switch (token->keyword)
4425 case RID_FUNCTION_NAME:
4426 name = NIC_FUNC_NAME;
4427 break;
4428 case RID_PRETTY_FUNCTION_NAME:
4429 name = NIC_PRETTY_FUNC;
4430 break;
4431 case RID_C99_FUNCTION_NAME:
4432 name = NIC_C99_FUNC;
4433 break;
4434 default:
4435 gcc_unreachable ();
4438 if (cp_parser_non_integral_constant_expression (parser, name))
4439 return error_mark_node;
4441 /* Look up the name. */
4442 return finish_fname (token->u.value);
4445 case RID_VA_ARG:
4447 tree expression;
4448 tree type;
4449 source_location type_location;
4451 /* The `__builtin_va_arg' construct is used to handle
4452 `va_arg'. Consume the `__builtin_va_arg' token. */
4453 cp_lexer_consume_token (parser->lexer);
4454 /* Look for the opening `('. */
4455 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4456 /* Now, parse the assignment-expression. */
4457 expression = cp_parser_assignment_expression (parser,
4458 /*cast_p=*/false, NULL);
4459 /* Look for the `,'. */
4460 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4461 type_location = cp_lexer_peek_token (parser->lexer)->location;
4462 /* Parse the type-id. */
4463 type = cp_parser_type_id (parser);
4464 /* Look for the closing `)'. */
4465 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4466 /* Using `va_arg' in a constant-expression is not
4467 allowed. */
4468 if (cp_parser_non_integral_constant_expression (parser,
4469 NIC_VA_ARG))
4470 return error_mark_node;
4471 return build_x_va_arg (type_location, expression, type);
4474 case RID_OFFSETOF:
4475 return cp_parser_builtin_offsetof (parser);
4477 case RID_HAS_NOTHROW_ASSIGN:
4478 case RID_HAS_NOTHROW_CONSTRUCTOR:
4479 case RID_HAS_NOTHROW_COPY:
4480 case RID_HAS_TRIVIAL_ASSIGN:
4481 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4482 case RID_HAS_TRIVIAL_COPY:
4483 case RID_HAS_TRIVIAL_DESTRUCTOR:
4484 case RID_HAS_VIRTUAL_DESTRUCTOR:
4485 case RID_IS_ABSTRACT:
4486 case RID_IS_BASE_OF:
4487 case RID_IS_CLASS:
4488 case RID_IS_CONVERTIBLE_TO:
4489 case RID_IS_EMPTY:
4490 case RID_IS_ENUM:
4491 case RID_IS_FINAL:
4492 case RID_IS_LITERAL_TYPE:
4493 case RID_IS_POD:
4494 case RID_IS_POLYMORPHIC:
4495 case RID_IS_STD_LAYOUT:
4496 case RID_IS_TRIVIAL:
4497 case RID_IS_UNION:
4498 return cp_parser_trait_expr (parser, token->keyword);
4500 /* Objective-C++ expressions. */
4501 case RID_AT_ENCODE:
4502 case RID_AT_PROTOCOL:
4503 case RID_AT_SELECTOR:
4504 return cp_parser_objc_expression (parser);
4506 case RID_TEMPLATE:
4507 if (parser->in_function_body
4508 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4509 == CPP_LESS))
4511 error_at (token->location,
4512 "a template declaration cannot appear at block scope");
4513 cp_parser_skip_to_end_of_block_or_statement (parser);
4514 return error_mark_node;
4516 default:
4517 cp_parser_error (parser, "expected primary-expression");
4518 return error_mark_node;
4521 /* An id-expression can start with either an identifier, a
4522 `::' as the beginning of a qualified-id, or the "operator"
4523 keyword. */
4524 case CPP_NAME:
4525 case CPP_SCOPE:
4526 case CPP_TEMPLATE_ID:
4527 case CPP_NESTED_NAME_SPECIFIER:
4529 tree id_expression;
4530 tree decl;
4531 const char *error_msg;
4532 bool template_p;
4533 bool done;
4534 cp_token *id_expr_token;
4536 id_expression:
4537 /* Parse the id-expression. */
4538 id_expression
4539 = cp_parser_id_expression (parser,
4540 /*template_keyword_p=*/false,
4541 /*check_dependency_p=*/true,
4542 &template_p,
4543 /*declarator_p=*/false,
4544 /*optional_p=*/false);
4545 if (id_expression == error_mark_node)
4546 return error_mark_node;
4547 id_expr_token = token;
4548 token = cp_lexer_peek_token (parser->lexer);
4549 done = (token->type != CPP_OPEN_SQUARE
4550 && token->type != CPP_OPEN_PAREN
4551 && token->type != CPP_DOT
4552 && token->type != CPP_DEREF
4553 && token->type != CPP_PLUS_PLUS
4554 && token->type != CPP_MINUS_MINUS);
4555 /* If we have a template-id, then no further lookup is
4556 required. If the template-id was for a template-class, we
4557 will sometimes have a TYPE_DECL at this point. */
4558 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4559 || TREE_CODE (id_expression) == TYPE_DECL)
4560 decl = id_expression;
4561 /* Look up the name. */
4562 else
4564 tree ambiguous_decls;
4566 /* If we already know that this lookup is ambiguous, then
4567 we've already issued an error message; there's no reason
4568 to check again. */
4569 if (id_expr_token->type == CPP_NAME
4570 && id_expr_token->error_reported)
4572 cp_parser_simulate_error (parser);
4573 return error_mark_node;
4576 decl = cp_parser_lookup_name (parser, id_expression,
4577 none_type,
4578 template_p,
4579 /*is_namespace=*/false,
4580 /*check_dependency=*/true,
4581 &ambiguous_decls,
4582 id_expr_token->location);
4583 /* If the lookup was ambiguous, an error will already have
4584 been issued. */
4585 if (ambiguous_decls)
4586 return error_mark_node;
4588 /* In Objective-C++, we may have an Objective-C 2.0
4589 dot-syntax for classes here. */
4590 if (c_dialect_objc ()
4591 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4592 && TREE_CODE (decl) == TYPE_DECL
4593 && objc_is_class_name (decl))
4595 tree component;
4596 cp_lexer_consume_token (parser->lexer);
4597 component = cp_parser_identifier (parser);
4598 if (component == error_mark_node)
4599 return error_mark_node;
4601 return objc_build_class_component_ref (id_expression, component);
4604 /* In Objective-C++, an instance variable (ivar) may be preferred
4605 to whatever cp_parser_lookup_name() found. */
4606 decl = objc_lookup_ivar (decl, id_expression);
4608 /* If name lookup gives us a SCOPE_REF, then the
4609 qualifying scope was dependent. */
4610 if (TREE_CODE (decl) == SCOPE_REF)
4612 /* At this point, we do not know if DECL is a valid
4613 integral constant expression. We assume that it is
4614 in fact such an expression, so that code like:
4616 template <int N> struct A {
4617 int a[B<N>::i];
4620 is accepted. At template-instantiation time, we
4621 will check that B<N>::i is actually a constant. */
4622 return decl;
4624 /* Check to see if DECL is a local variable in a context
4625 where that is forbidden. */
4626 if (parser->local_variables_forbidden_p
4627 && local_variable_p (decl))
4629 /* It might be that we only found DECL because we are
4630 trying to be generous with pre-ISO scoping rules.
4631 For example, consider:
4633 int i;
4634 void g() {
4635 for (int i = 0; i < 10; ++i) {}
4636 extern void f(int j = i);
4639 Here, name look up will originally find the out
4640 of scope `i'. We need to issue a warning message,
4641 but then use the global `i'. */
4642 decl = check_for_out_of_scope_variable (decl);
4643 if (local_variable_p (decl))
4645 error_at (id_expr_token->location,
4646 "local variable %qD may not appear in this context",
4647 decl);
4648 return error_mark_node;
4653 decl = (finish_id_expression
4654 (id_expression, decl, parser->scope,
4655 idk,
4656 parser->integral_constant_expression_p,
4657 parser->allow_non_integral_constant_expression_p,
4658 &parser->non_integral_constant_expression_p,
4659 template_p, done, address_p,
4660 template_arg_p,
4661 &error_msg,
4662 id_expr_token->location));
4663 if (error_msg)
4664 cp_parser_error (parser, error_msg);
4665 return decl;
4668 /* Anything else is an error. */
4669 default:
4670 cp_parser_error (parser, "expected primary-expression");
4671 return error_mark_node;
4675 static inline tree
4676 cp_parser_primary_expression (cp_parser *parser,
4677 bool address_p,
4678 bool cast_p,
4679 bool template_arg_p,
4680 cp_id_kind *idk)
4682 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4683 /*decltype*/false, idk);
4686 /* Parse an id-expression.
4688 id-expression:
4689 unqualified-id
4690 qualified-id
4692 qualified-id:
4693 :: [opt] nested-name-specifier template [opt] unqualified-id
4694 :: identifier
4695 :: operator-function-id
4696 :: template-id
4698 Return a representation of the unqualified portion of the
4699 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4700 a `::' or nested-name-specifier.
4702 Often, if the id-expression was a qualified-id, the caller will
4703 want to make a SCOPE_REF to represent the qualified-id. This
4704 function does not do this in order to avoid wastefully creating
4705 SCOPE_REFs when they are not required.
4707 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4708 `template' keyword.
4710 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4711 uninstantiated templates.
4713 If *TEMPLATE_P is non-NULL, it is set to true iff the
4714 `template' keyword is used to explicitly indicate that the entity
4715 named is a template.
4717 If DECLARATOR_P is true, the id-expression is appearing as part of
4718 a declarator, rather than as part of an expression. */
4720 static tree
4721 cp_parser_id_expression (cp_parser *parser,
4722 bool template_keyword_p,
4723 bool check_dependency_p,
4724 bool *template_p,
4725 bool declarator_p,
4726 bool optional_p)
4728 bool global_scope_p;
4729 bool nested_name_specifier_p;
4731 /* Assume the `template' keyword was not used. */
4732 if (template_p)
4733 *template_p = template_keyword_p;
4735 /* Look for the optional `::' operator. */
4736 global_scope_p
4737 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4738 != NULL_TREE);
4739 /* Look for the optional nested-name-specifier. */
4740 nested_name_specifier_p
4741 = (cp_parser_nested_name_specifier_opt (parser,
4742 /*typename_keyword_p=*/false,
4743 check_dependency_p,
4744 /*type_p=*/false,
4745 declarator_p)
4746 != NULL_TREE);
4747 /* If there is a nested-name-specifier, then we are looking at
4748 the first qualified-id production. */
4749 if (nested_name_specifier_p)
4751 tree saved_scope;
4752 tree saved_object_scope;
4753 tree saved_qualifying_scope;
4754 tree unqualified_id;
4755 bool is_template;
4757 /* See if the next token is the `template' keyword. */
4758 if (!template_p)
4759 template_p = &is_template;
4760 *template_p = cp_parser_optional_template_keyword (parser);
4761 /* Name lookup we do during the processing of the
4762 unqualified-id might obliterate SCOPE. */
4763 saved_scope = parser->scope;
4764 saved_object_scope = parser->object_scope;
4765 saved_qualifying_scope = parser->qualifying_scope;
4766 /* Process the final unqualified-id. */
4767 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4768 check_dependency_p,
4769 declarator_p,
4770 /*optional_p=*/false);
4771 /* Restore the SAVED_SCOPE for our caller. */
4772 parser->scope = saved_scope;
4773 parser->object_scope = saved_object_scope;
4774 parser->qualifying_scope = saved_qualifying_scope;
4776 return unqualified_id;
4778 /* Otherwise, if we are in global scope, then we are looking at one
4779 of the other qualified-id productions. */
4780 else if (global_scope_p)
4782 cp_token *token;
4783 tree id;
4785 /* Peek at the next token. */
4786 token = cp_lexer_peek_token (parser->lexer);
4788 /* If it's an identifier, and the next token is not a "<", then
4789 we can avoid the template-id case. This is an optimization
4790 for this common case. */
4791 if (token->type == CPP_NAME
4792 && !cp_parser_nth_token_starts_template_argument_list_p
4793 (parser, 2))
4794 return cp_parser_identifier (parser);
4796 cp_parser_parse_tentatively (parser);
4797 /* Try a template-id. */
4798 id = cp_parser_template_id (parser,
4799 /*template_keyword_p=*/false,
4800 /*check_dependency_p=*/true,
4801 none_type,
4802 declarator_p);
4803 /* If that worked, we're done. */
4804 if (cp_parser_parse_definitely (parser))
4805 return id;
4807 /* Peek at the next token. (Changes in the token buffer may
4808 have invalidated the pointer obtained above.) */
4809 token = cp_lexer_peek_token (parser->lexer);
4811 switch (token->type)
4813 case CPP_NAME:
4814 return cp_parser_identifier (parser);
4816 case CPP_KEYWORD:
4817 if (token->keyword == RID_OPERATOR)
4818 return cp_parser_operator_function_id (parser);
4819 /* Fall through. */
4821 default:
4822 cp_parser_error (parser, "expected id-expression");
4823 return error_mark_node;
4826 else
4827 return cp_parser_unqualified_id (parser, template_keyword_p,
4828 /*check_dependency_p=*/true,
4829 declarator_p,
4830 optional_p);
4833 /* Parse an unqualified-id.
4835 unqualified-id:
4836 identifier
4837 operator-function-id
4838 conversion-function-id
4839 ~ class-name
4840 template-id
4842 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4843 keyword, in a construct like `A::template ...'.
4845 Returns a representation of unqualified-id. For the `identifier'
4846 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4847 production a BIT_NOT_EXPR is returned; the operand of the
4848 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4849 other productions, see the documentation accompanying the
4850 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4851 names are looked up in uninstantiated templates. If DECLARATOR_P
4852 is true, the unqualified-id is appearing as part of a declarator,
4853 rather than as part of an expression. */
4855 static tree
4856 cp_parser_unqualified_id (cp_parser* parser,
4857 bool template_keyword_p,
4858 bool check_dependency_p,
4859 bool declarator_p,
4860 bool optional_p)
4862 cp_token *token;
4864 /* Peek at the next token. */
4865 token = cp_lexer_peek_token (parser->lexer);
4867 switch (token->type)
4869 case CPP_NAME:
4871 tree id;
4873 /* We don't know yet whether or not this will be a
4874 template-id. */
4875 cp_parser_parse_tentatively (parser);
4876 /* Try a template-id. */
4877 id = cp_parser_template_id (parser, template_keyword_p,
4878 check_dependency_p,
4879 none_type,
4880 declarator_p);
4881 /* If it worked, we're done. */
4882 if (cp_parser_parse_definitely (parser))
4883 return id;
4884 /* Otherwise, it's an ordinary identifier. */
4885 return cp_parser_identifier (parser);
4888 case CPP_TEMPLATE_ID:
4889 return cp_parser_template_id (parser, template_keyword_p,
4890 check_dependency_p,
4891 none_type,
4892 declarator_p);
4894 case CPP_COMPL:
4896 tree type_decl;
4897 tree qualifying_scope;
4898 tree object_scope;
4899 tree scope;
4900 bool done;
4902 /* Consume the `~' token. */
4903 cp_lexer_consume_token (parser->lexer);
4904 /* Parse the class-name. The standard, as written, seems to
4905 say that:
4907 template <typename T> struct S { ~S (); };
4908 template <typename T> S<T>::~S() {}
4910 is invalid, since `~' must be followed by a class-name, but
4911 `S<T>' is dependent, and so not known to be a class.
4912 That's not right; we need to look in uninstantiated
4913 templates. A further complication arises from:
4915 template <typename T> void f(T t) {
4916 t.T::~T();
4919 Here, it is not possible to look up `T' in the scope of `T'
4920 itself. We must look in both the current scope, and the
4921 scope of the containing complete expression.
4923 Yet another issue is:
4925 struct S {
4926 int S;
4927 ~S();
4930 S::~S() {}
4932 The standard does not seem to say that the `S' in `~S'
4933 should refer to the type `S' and not the data member
4934 `S::S'. */
4936 /* DR 244 says that we look up the name after the "~" in the
4937 same scope as we looked up the qualifying name. That idea
4938 isn't fully worked out; it's more complicated than that. */
4939 scope = parser->scope;
4940 object_scope = parser->object_scope;
4941 qualifying_scope = parser->qualifying_scope;
4943 /* Check for invalid scopes. */
4944 if (scope == error_mark_node)
4946 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4947 cp_lexer_consume_token (parser->lexer);
4948 return error_mark_node;
4950 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4952 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4953 error_at (token->location,
4954 "scope %qT before %<~%> is not a class-name",
4955 scope);
4956 cp_parser_simulate_error (parser);
4957 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4958 cp_lexer_consume_token (parser->lexer);
4959 return error_mark_node;
4961 gcc_assert (!scope || TYPE_P (scope));
4963 /* If the name is of the form "X::~X" it's OK even if X is a
4964 typedef. */
4965 token = cp_lexer_peek_token (parser->lexer);
4966 if (scope
4967 && token->type == CPP_NAME
4968 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4969 != CPP_LESS)
4970 && (token->u.value == TYPE_IDENTIFIER (scope)
4971 || (CLASS_TYPE_P (scope)
4972 && constructor_name_p (token->u.value, scope))))
4974 cp_lexer_consume_token (parser->lexer);
4975 return build_nt (BIT_NOT_EXPR, scope);
4978 /* ~auto means the destructor of whatever the object is. */
4979 if (cp_parser_is_keyword (token, RID_AUTO))
4981 if (cxx_dialect < cxx1y)
4982 pedwarn (input_location, 0,
4983 "%<~auto%> only available with "
4984 "-std=c++1y or -std=gnu++1y");
4985 cp_lexer_consume_token (parser->lexer);
4986 return build_nt (BIT_NOT_EXPR, make_auto ());
4989 /* If there was an explicit qualification (S::~T), first look
4990 in the scope given by the qualification (i.e., S).
4992 Note: in the calls to cp_parser_class_name below we pass
4993 typename_type so that lookup finds the injected-class-name
4994 rather than the constructor. */
4995 done = false;
4996 type_decl = NULL_TREE;
4997 if (scope)
4999 cp_parser_parse_tentatively (parser);
5000 type_decl = cp_parser_class_name (parser,
5001 /*typename_keyword_p=*/false,
5002 /*template_keyword_p=*/false,
5003 typename_type,
5004 /*check_dependency=*/false,
5005 /*class_head_p=*/false,
5006 declarator_p);
5007 if (cp_parser_parse_definitely (parser))
5008 done = true;
5010 /* In "N::S::~S", look in "N" as well. */
5011 if (!done && scope && qualifying_scope)
5013 cp_parser_parse_tentatively (parser);
5014 parser->scope = qualifying_scope;
5015 parser->object_scope = NULL_TREE;
5016 parser->qualifying_scope = NULL_TREE;
5017 type_decl
5018 = cp_parser_class_name (parser,
5019 /*typename_keyword_p=*/false,
5020 /*template_keyword_p=*/false,
5021 typename_type,
5022 /*check_dependency=*/false,
5023 /*class_head_p=*/false,
5024 declarator_p);
5025 if (cp_parser_parse_definitely (parser))
5026 done = true;
5028 /* In "p->S::~T", look in the scope given by "*p" as well. */
5029 else if (!done && object_scope)
5031 cp_parser_parse_tentatively (parser);
5032 parser->scope = object_scope;
5033 parser->object_scope = NULL_TREE;
5034 parser->qualifying_scope = NULL_TREE;
5035 type_decl
5036 = cp_parser_class_name (parser,
5037 /*typename_keyword_p=*/false,
5038 /*template_keyword_p=*/false,
5039 typename_type,
5040 /*check_dependency=*/false,
5041 /*class_head_p=*/false,
5042 declarator_p);
5043 if (cp_parser_parse_definitely (parser))
5044 done = true;
5046 /* Look in the surrounding context. */
5047 if (!done)
5049 parser->scope = NULL_TREE;
5050 parser->object_scope = NULL_TREE;
5051 parser->qualifying_scope = NULL_TREE;
5052 if (processing_template_decl)
5053 cp_parser_parse_tentatively (parser);
5054 type_decl
5055 = cp_parser_class_name (parser,
5056 /*typename_keyword_p=*/false,
5057 /*template_keyword_p=*/false,
5058 typename_type,
5059 /*check_dependency=*/false,
5060 /*class_head_p=*/false,
5061 declarator_p);
5062 if (processing_template_decl
5063 && ! cp_parser_parse_definitely (parser))
5065 /* We couldn't find a type with this name, so just accept
5066 it and check for a match at instantiation time. */
5067 type_decl = cp_parser_identifier (parser);
5068 if (type_decl != error_mark_node)
5069 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5070 return type_decl;
5073 /* If an error occurred, assume that the name of the
5074 destructor is the same as the name of the qualifying
5075 class. That allows us to keep parsing after running
5076 into ill-formed destructor names. */
5077 if (type_decl == error_mark_node && scope)
5078 return build_nt (BIT_NOT_EXPR, scope);
5079 else if (type_decl == error_mark_node)
5080 return error_mark_node;
5082 /* Check that destructor name and scope match. */
5083 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5085 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5086 error_at (token->location,
5087 "declaration of %<~%T%> as member of %qT",
5088 type_decl, scope);
5089 cp_parser_simulate_error (parser);
5090 return error_mark_node;
5093 /* [class.dtor]
5095 A typedef-name that names a class shall not be used as the
5096 identifier in the declarator for a destructor declaration. */
5097 if (declarator_p
5098 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5099 && !DECL_SELF_REFERENCE_P (type_decl)
5100 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5101 error_at (token->location,
5102 "typedef-name %qD used as destructor declarator",
5103 type_decl);
5105 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5108 case CPP_KEYWORD:
5109 if (token->keyword == RID_OPERATOR)
5111 tree id;
5113 /* This could be a template-id, so we try that first. */
5114 cp_parser_parse_tentatively (parser);
5115 /* Try a template-id. */
5116 id = cp_parser_template_id (parser, template_keyword_p,
5117 /*check_dependency_p=*/true,
5118 none_type,
5119 declarator_p);
5120 /* If that worked, we're done. */
5121 if (cp_parser_parse_definitely (parser))
5122 return id;
5123 /* We still don't know whether we're looking at an
5124 operator-function-id or a conversion-function-id. */
5125 cp_parser_parse_tentatively (parser);
5126 /* Try an operator-function-id. */
5127 id = cp_parser_operator_function_id (parser);
5128 /* If that didn't work, try a conversion-function-id. */
5129 if (!cp_parser_parse_definitely (parser))
5130 id = cp_parser_conversion_function_id (parser);
5131 else if (UDLIT_OPER_P (id))
5133 /* 17.6.3.3.5 */
5134 const char *name = UDLIT_OP_SUFFIX (id);
5135 if (name[0] != '_' && !in_system_header_at (input_location)
5136 && declarator_p)
5137 warning (0, "literal operator suffixes not preceded by %<_%>"
5138 " are reserved for future standardization");
5141 return id;
5143 /* Fall through. */
5145 default:
5146 if (optional_p)
5147 return NULL_TREE;
5148 cp_parser_error (parser, "expected unqualified-id");
5149 return error_mark_node;
5153 /* Parse an (optional) nested-name-specifier.
5155 nested-name-specifier: [C++98]
5156 class-or-namespace-name :: nested-name-specifier [opt]
5157 class-or-namespace-name :: template nested-name-specifier [opt]
5159 nested-name-specifier: [C++0x]
5160 type-name ::
5161 namespace-name ::
5162 nested-name-specifier identifier ::
5163 nested-name-specifier template [opt] simple-template-id ::
5165 PARSER->SCOPE should be set appropriately before this function is
5166 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5167 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5168 in name lookups.
5170 Sets PARSER->SCOPE to the class (TYPE) or namespace
5171 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5172 it unchanged if there is no nested-name-specifier. Returns the new
5173 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5175 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5176 part of a declaration and/or decl-specifier. */
5178 static tree
5179 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5180 bool typename_keyword_p,
5181 bool check_dependency_p,
5182 bool type_p,
5183 bool is_declaration)
5185 bool success = false;
5186 cp_token_position start = 0;
5187 cp_token *token;
5189 /* Remember where the nested-name-specifier starts. */
5190 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5192 start = cp_lexer_token_position (parser->lexer, false);
5193 push_deferring_access_checks (dk_deferred);
5196 while (true)
5198 tree new_scope;
5199 tree old_scope;
5200 tree saved_qualifying_scope;
5201 bool template_keyword_p;
5203 /* Spot cases that cannot be the beginning of a
5204 nested-name-specifier. */
5205 token = cp_lexer_peek_token (parser->lexer);
5207 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5208 the already parsed nested-name-specifier. */
5209 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5211 /* Grab the nested-name-specifier and continue the loop. */
5212 cp_parser_pre_parsed_nested_name_specifier (parser);
5213 /* If we originally encountered this nested-name-specifier
5214 with IS_DECLARATION set to false, we will not have
5215 resolved TYPENAME_TYPEs, so we must do so here. */
5216 if (is_declaration
5217 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5219 new_scope = resolve_typename_type (parser->scope,
5220 /*only_current_p=*/false);
5221 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5222 parser->scope = new_scope;
5224 success = true;
5225 continue;
5228 /* Spot cases that cannot be the beginning of a
5229 nested-name-specifier. On the second and subsequent times
5230 through the loop, we look for the `template' keyword. */
5231 if (success && token->keyword == RID_TEMPLATE)
5233 /* A template-id can start a nested-name-specifier. */
5234 else if (token->type == CPP_TEMPLATE_ID)
5236 /* DR 743: decltype can be used in a nested-name-specifier. */
5237 else if (token_is_decltype (token))
5239 else
5241 /* If the next token is not an identifier, then it is
5242 definitely not a type-name or namespace-name. */
5243 if (token->type != CPP_NAME)
5244 break;
5245 /* If the following token is neither a `<' (to begin a
5246 template-id), nor a `::', then we are not looking at a
5247 nested-name-specifier. */
5248 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5250 if (token->type == CPP_COLON
5251 && parser->colon_corrects_to_scope_p
5252 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5254 error_at (token->location,
5255 "found %<:%> in nested-name-specifier, expected %<::%>");
5256 token->type = CPP_SCOPE;
5259 if (token->type != CPP_SCOPE
5260 && !cp_parser_nth_token_starts_template_argument_list_p
5261 (parser, 2))
5262 break;
5265 /* The nested-name-specifier is optional, so we parse
5266 tentatively. */
5267 cp_parser_parse_tentatively (parser);
5269 /* Look for the optional `template' keyword, if this isn't the
5270 first time through the loop. */
5271 if (success)
5272 template_keyword_p = cp_parser_optional_template_keyword (parser);
5273 else
5274 template_keyword_p = false;
5276 /* Save the old scope since the name lookup we are about to do
5277 might destroy it. */
5278 old_scope = parser->scope;
5279 saved_qualifying_scope = parser->qualifying_scope;
5280 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5281 look up names in "X<T>::I" in order to determine that "Y" is
5282 a template. So, if we have a typename at this point, we make
5283 an effort to look through it. */
5284 if (is_declaration
5285 && !typename_keyword_p
5286 && parser->scope
5287 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5288 parser->scope = resolve_typename_type (parser->scope,
5289 /*only_current_p=*/false);
5290 /* Parse the qualifying entity. */
5291 new_scope
5292 = cp_parser_qualifying_entity (parser,
5293 typename_keyword_p,
5294 template_keyword_p,
5295 check_dependency_p,
5296 type_p,
5297 is_declaration);
5298 /* Look for the `::' token. */
5299 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5301 /* If we found what we wanted, we keep going; otherwise, we're
5302 done. */
5303 if (!cp_parser_parse_definitely (parser))
5305 bool error_p = false;
5307 /* Restore the OLD_SCOPE since it was valid before the
5308 failed attempt at finding the last
5309 class-or-namespace-name. */
5310 parser->scope = old_scope;
5311 parser->qualifying_scope = saved_qualifying_scope;
5313 /* If the next token is a decltype, and the one after that is a
5314 `::', then the decltype has failed to resolve to a class or
5315 enumeration type. Give this error even when parsing
5316 tentatively since it can't possibly be valid--and we're going
5317 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5318 won't get another chance.*/
5319 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5320 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5321 == CPP_SCOPE))
5323 token = cp_lexer_consume_token (parser->lexer);
5324 error_at (token->location, "decltype evaluates to %qT, "
5325 "which is not a class or enumeration type",
5326 token->u.value);
5327 parser->scope = error_mark_node;
5328 error_p = true;
5329 /* As below. */
5330 success = true;
5331 cp_lexer_consume_token (parser->lexer);
5334 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5335 break;
5336 /* If the next token is an identifier, and the one after
5337 that is a `::', then any valid interpretation would have
5338 found a class-or-namespace-name. */
5339 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5340 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5341 == CPP_SCOPE)
5342 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5343 != CPP_COMPL))
5345 token = cp_lexer_consume_token (parser->lexer);
5346 if (!error_p)
5348 if (!token->error_reported)
5350 tree decl;
5351 tree ambiguous_decls;
5353 decl = cp_parser_lookup_name (parser, token->u.value,
5354 none_type,
5355 /*is_template=*/false,
5356 /*is_namespace=*/false,
5357 /*check_dependency=*/true,
5358 &ambiguous_decls,
5359 token->location);
5360 if (TREE_CODE (decl) == TEMPLATE_DECL)
5361 error_at (token->location,
5362 "%qD used without template parameters",
5363 decl);
5364 else if (ambiguous_decls)
5366 // cp_parser_lookup_name has the same diagnostic,
5367 // thus make sure to emit it at most once.
5368 if (cp_parser_uncommitted_to_tentative_parse_p
5369 (parser))
5371 error_at (token->location,
5372 "reference to %qD is ambiguous",
5373 token->u.value);
5374 print_candidates (ambiguous_decls);
5376 decl = error_mark_node;
5378 else
5380 if (cxx_dialect != cxx98)
5381 cp_parser_name_lookup_error
5382 (parser, token->u.value, decl, NLE_NOT_CXX98,
5383 token->location);
5384 else
5385 cp_parser_name_lookup_error
5386 (parser, token->u.value, decl, NLE_CXX98,
5387 token->location);
5390 parser->scope = error_mark_node;
5391 error_p = true;
5392 /* Treat this as a successful nested-name-specifier
5393 due to:
5395 [basic.lookup.qual]
5397 If the name found is not a class-name (clause
5398 _class_) or namespace-name (_namespace.def_), the
5399 program is ill-formed. */
5400 success = true;
5402 cp_lexer_consume_token (parser->lexer);
5404 break;
5406 /* We've found one valid nested-name-specifier. */
5407 success = true;
5408 /* Name lookup always gives us a DECL. */
5409 if (TREE_CODE (new_scope) == TYPE_DECL)
5410 new_scope = TREE_TYPE (new_scope);
5411 /* Uses of "template" must be followed by actual templates. */
5412 if (template_keyword_p
5413 && !(CLASS_TYPE_P (new_scope)
5414 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5415 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5416 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5417 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5418 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5419 == TEMPLATE_ID_EXPR)))
5420 permerror (input_location, TYPE_P (new_scope)
5421 ? G_("%qT is not a template")
5422 : G_("%qD is not a template"),
5423 new_scope);
5424 /* If it is a class scope, try to complete it; we are about to
5425 be looking up names inside the class. */
5426 if (TYPE_P (new_scope)
5427 /* Since checking types for dependency can be expensive,
5428 avoid doing it if the type is already complete. */
5429 && !COMPLETE_TYPE_P (new_scope)
5430 /* Do not try to complete dependent types. */
5431 && !dependent_type_p (new_scope))
5433 new_scope = complete_type (new_scope);
5434 /* If it is a typedef to current class, use the current
5435 class instead, as the typedef won't have any names inside
5436 it yet. */
5437 if (!COMPLETE_TYPE_P (new_scope)
5438 && currently_open_class (new_scope))
5439 new_scope = TYPE_MAIN_VARIANT (new_scope);
5441 /* Make sure we look in the right scope the next time through
5442 the loop. */
5443 parser->scope = new_scope;
5446 /* If parsing tentatively, replace the sequence of tokens that makes
5447 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5448 token. That way, should we re-parse the token stream, we will
5449 not have to repeat the effort required to do the parse, nor will
5450 we issue duplicate error messages. */
5451 if (success && start)
5453 cp_token *token;
5455 token = cp_lexer_token_at (parser->lexer, start);
5456 /* Reset the contents of the START token. */
5457 token->type = CPP_NESTED_NAME_SPECIFIER;
5458 /* Retrieve any deferred checks. Do not pop this access checks yet
5459 so the memory will not be reclaimed during token replacing below. */
5460 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5461 token->u.tree_check_value->value = parser->scope;
5462 token->u.tree_check_value->checks = get_deferred_access_checks ();
5463 token->u.tree_check_value->qualifying_scope =
5464 parser->qualifying_scope;
5465 token->keyword = RID_MAX;
5467 /* Purge all subsequent tokens. */
5468 cp_lexer_purge_tokens_after (parser->lexer, start);
5471 if (start)
5472 pop_to_parent_deferring_access_checks ();
5474 return success ? parser->scope : NULL_TREE;
5477 /* Parse a nested-name-specifier. See
5478 cp_parser_nested_name_specifier_opt for details. This function
5479 behaves identically, except that it will an issue an error if no
5480 nested-name-specifier is present. */
5482 static tree
5483 cp_parser_nested_name_specifier (cp_parser *parser,
5484 bool typename_keyword_p,
5485 bool check_dependency_p,
5486 bool type_p,
5487 bool is_declaration)
5489 tree scope;
5491 /* Look for the nested-name-specifier. */
5492 scope = cp_parser_nested_name_specifier_opt (parser,
5493 typename_keyword_p,
5494 check_dependency_p,
5495 type_p,
5496 is_declaration);
5497 /* If it was not present, issue an error message. */
5498 if (!scope)
5500 cp_parser_error (parser, "expected nested-name-specifier");
5501 parser->scope = NULL_TREE;
5504 return scope;
5507 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5508 this is either a class-name or a namespace-name (which corresponds
5509 to the class-or-namespace-name production in the grammar). For
5510 C++0x, it can also be a type-name that refers to an enumeration
5511 type or a simple-template-id.
5513 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5514 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5515 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5516 TYPE_P is TRUE iff the next name should be taken as a class-name,
5517 even the same name is declared to be another entity in the same
5518 scope.
5520 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5521 specified by the class-or-namespace-name. If neither is found the
5522 ERROR_MARK_NODE is returned. */
5524 static tree
5525 cp_parser_qualifying_entity (cp_parser *parser,
5526 bool typename_keyword_p,
5527 bool template_keyword_p,
5528 bool check_dependency_p,
5529 bool type_p,
5530 bool is_declaration)
5532 tree saved_scope;
5533 tree saved_qualifying_scope;
5534 tree saved_object_scope;
5535 tree scope;
5536 bool only_class_p;
5537 bool successful_parse_p;
5539 /* DR 743: decltype can appear in a nested-name-specifier. */
5540 if (cp_lexer_next_token_is_decltype (parser->lexer))
5542 scope = cp_parser_decltype (parser);
5543 if (TREE_CODE (scope) != ENUMERAL_TYPE
5544 && !MAYBE_CLASS_TYPE_P (scope))
5546 cp_parser_simulate_error (parser);
5547 return error_mark_node;
5549 if (TYPE_NAME (scope))
5550 scope = TYPE_NAME (scope);
5551 return scope;
5554 /* Before we try to parse the class-name, we must save away the
5555 current PARSER->SCOPE since cp_parser_class_name will destroy
5556 it. */
5557 saved_scope = parser->scope;
5558 saved_qualifying_scope = parser->qualifying_scope;
5559 saved_object_scope = parser->object_scope;
5560 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5561 there is no need to look for a namespace-name. */
5562 only_class_p = template_keyword_p
5563 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5564 if (!only_class_p)
5565 cp_parser_parse_tentatively (parser);
5566 scope = cp_parser_class_name (parser,
5567 typename_keyword_p,
5568 template_keyword_p,
5569 type_p ? class_type : none_type,
5570 check_dependency_p,
5571 /*class_head_p=*/false,
5572 is_declaration);
5573 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5574 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5575 if (!only_class_p
5576 && cxx_dialect != cxx98
5577 && !successful_parse_p)
5579 /* Restore the saved scope. */
5580 parser->scope = saved_scope;
5581 parser->qualifying_scope = saved_qualifying_scope;
5582 parser->object_scope = saved_object_scope;
5584 /* Parse tentatively. */
5585 cp_parser_parse_tentatively (parser);
5587 /* Parse a type-name */
5588 scope = cp_parser_type_name (parser);
5590 /* "If the name found does not designate a namespace or a class,
5591 enumeration, or dependent type, the program is ill-formed."
5593 We cover classes and dependent types above and namespaces below,
5594 so this code is only looking for enums. */
5595 if (!scope || TREE_CODE (scope) != TYPE_DECL
5596 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5597 cp_parser_simulate_error (parser);
5599 successful_parse_p = cp_parser_parse_definitely (parser);
5601 /* If that didn't work, try for a namespace-name. */
5602 if (!only_class_p && !successful_parse_p)
5604 /* Restore the saved scope. */
5605 parser->scope = saved_scope;
5606 parser->qualifying_scope = saved_qualifying_scope;
5607 parser->object_scope = saved_object_scope;
5608 /* If we are not looking at an identifier followed by the scope
5609 resolution operator, then this is not part of a
5610 nested-name-specifier. (Note that this function is only used
5611 to parse the components of a nested-name-specifier.) */
5612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5613 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5614 return error_mark_node;
5615 scope = cp_parser_namespace_name (parser);
5618 return scope;
5621 /* Return true if we are looking at a compound-literal, false otherwise. */
5623 static bool
5624 cp_parser_compound_literal_p (cp_parser *parser)
5626 /* Consume the `('. */
5627 cp_lexer_consume_token (parser->lexer);
5629 cp_lexer_save_tokens (parser->lexer);
5631 /* Skip tokens until the next token is a closing parenthesis.
5632 If we find the closing `)', and the next token is a `{', then
5633 we are looking at a compound-literal. */
5634 bool compound_literal_p
5635 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5636 /*consume_paren=*/true)
5637 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5639 /* Roll back the tokens we skipped. */
5640 cp_lexer_rollback_tokens (parser->lexer);
5642 return compound_literal_p;
5645 /* Parse a postfix-expression.
5647 postfix-expression:
5648 primary-expression
5649 postfix-expression [ expression ]
5650 postfix-expression ( expression-list [opt] )
5651 simple-type-specifier ( expression-list [opt] )
5652 typename :: [opt] nested-name-specifier identifier
5653 ( expression-list [opt] )
5654 typename :: [opt] nested-name-specifier template [opt] template-id
5655 ( expression-list [opt] )
5656 postfix-expression . template [opt] id-expression
5657 postfix-expression -> template [opt] id-expression
5658 postfix-expression . pseudo-destructor-name
5659 postfix-expression -> pseudo-destructor-name
5660 postfix-expression ++
5661 postfix-expression --
5662 dynamic_cast < type-id > ( expression )
5663 static_cast < type-id > ( expression )
5664 reinterpret_cast < type-id > ( expression )
5665 const_cast < type-id > ( expression )
5666 typeid ( expression )
5667 typeid ( type-id )
5669 GNU Extension:
5671 postfix-expression:
5672 ( type-id ) { initializer-list , [opt] }
5674 This extension is a GNU version of the C99 compound-literal
5675 construct. (The C99 grammar uses `type-name' instead of `type-id',
5676 but they are essentially the same concept.)
5678 If ADDRESS_P is true, the postfix expression is the operand of the
5679 `&' operator. CAST_P is true if this expression is the target of a
5680 cast.
5682 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5683 class member access expressions [expr.ref].
5685 Returns a representation of the expression. */
5687 static tree
5688 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5689 bool member_access_only_p, bool decltype_p,
5690 cp_id_kind * pidk_return)
5692 cp_token *token;
5693 location_t loc;
5694 enum rid keyword;
5695 cp_id_kind idk = CP_ID_KIND_NONE;
5696 tree postfix_expression = NULL_TREE;
5697 bool is_member_access = false;
5698 int saved_in_statement = -1;
5700 /* Peek at the next token. */
5701 token = cp_lexer_peek_token (parser->lexer);
5702 loc = token->location;
5703 /* Some of the productions are determined by keywords. */
5704 keyword = token->keyword;
5705 switch (keyword)
5707 case RID_DYNCAST:
5708 case RID_STATCAST:
5709 case RID_REINTCAST:
5710 case RID_CONSTCAST:
5712 tree type;
5713 tree expression;
5714 const char *saved_message;
5715 bool saved_in_type_id_in_expr_p;
5717 /* All of these can be handled in the same way from the point
5718 of view of parsing. Begin by consuming the token
5719 identifying the cast. */
5720 cp_lexer_consume_token (parser->lexer);
5722 /* New types cannot be defined in the cast. */
5723 saved_message = parser->type_definition_forbidden_message;
5724 parser->type_definition_forbidden_message
5725 = G_("types may not be defined in casts");
5727 /* Look for the opening `<'. */
5728 cp_parser_require (parser, CPP_LESS, RT_LESS);
5729 /* Parse the type to which we are casting. */
5730 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5731 parser->in_type_id_in_expr_p = true;
5732 type = cp_parser_type_id (parser);
5733 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5734 /* Look for the closing `>'. */
5735 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5736 /* Restore the old message. */
5737 parser->type_definition_forbidden_message = saved_message;
5739 bool saved_greater_than_is_operator_p
5740 = parser->greater_than_is_operator_p;
5741 parser->greater_than_is_operator_p = true;
5743 /* And the expression which is being cast. */
5744 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5745 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5746 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5748 parser->greater_than_is_operator_p
5749 = saved_greater_than_is_operator_p;
5751 /* Only type conversions to integral or enumeration types
5752 can be used in constant-expressions. */
5753 if (!cast_valid_in_integral_constant_expression_p (type)
5754 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5755 return error_mark_node;
5757 switch (keyword)
5759 case RID_DYNCAST:
5760 postfix_expression
5761 = build_dynamic_cast (type, expression, tf_warning_or_error);
5762 break;
5763 case RID_STATCAST:
5764 postfix_expression
5765 = build_static_cast (type, expression, tf_warning_or_error);
5766 break;
5767 case RID_REINTCAST:
5768 postfix_expression
5769 = build_reinterpret_cast (type, expression,
5770 tf_warning_or_error);
5771 break;
5772 case RID_CONSTCAST:
5773 postfix_expression
5774 = build_const_cast (type, expression, tf_warning_or_error);
5775 break;
5776 default:
5777 gcc_unreachable ();
5780 break;
5782 case RID_TYPEID:
5784 tree type;
5785 const char *saved_message;
5786 bool saved_in_type_id_in_expr_p;
5788 /* Consume the `typeid' token. */
5789 cp_lexer_consume_token (parser->lexer);
5790 /* Look for the `(' token. */
5791 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5792 /* Types cannot be defined in a `typeid' expression. */
5793 saved_message = parser->type_definition_forbidden_message;
5794 parser->type_definition_forbidden_message
5795 = G_("types may not be defined in a %<typeid%> expression");
5796 /* We can't be sure yet whether we're looking at a type-id or an
5797 expression. */
5798 cp_parser_parse_tentatively (parser);
5799 /* Try a type-id first. */
5800 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5801 parser->in_type_id_in_expr_p = true;
5802 type = cp_parser_type_id (parser);
5803 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5804 /* Look for the `)' token. Otherwise, we can't be sure that
5805 we're not looking at an expression: consider `typeid (int
5806 (3))', for example. */
5807 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5808 /* If all went well, simply lookup the type-id. */
5809 if (cp_parser_parse_definitely (parser))
5810 postfix_expression = get_typeid (type, tf_warning_or_error);
5811 /* Otherwise, fall back to the expression variant. */
5812 else
5814 tree expression;
5816 /* Look for an expression. */
5817 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5818 /* Compute its typeid. */
5819 postfix_expression = build_typeid (expression, tf_warning_or_error);
5820 /* Look for the `)' token. */
5821 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5823 /* Restore the saved message. */
5824 parser->type_definition_forbidden_message = saved_message;
5825 /* `typeid' may not appear in an integral constant expression. */
5826 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5827 return error_mark_node;
5829 break;
5831 case RID_TYPENAME:
5833 tree type;
5834 /* The syntax permitted here is the same permitted for an
5835 elaborated-type-specifier. */
5836 type = cp_parser_elaborated_type_specifier (parser,
5837 /*is_friend=*/false,
5838 /*is_declaration=*/false);
5839 postfix_expression = cp_parser_functional_cast (parser, type);
5841 break;
5843 case RID_CILK_SPAWN:
5845 cp_lexer_consume_token (parser->lexer);
5846 token = cp_lexer_peek_token (parser->lexer);
5847 if (token->type == CPP_SEMICOLON)
5849 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5850 "an expression");
5851 postfix_expression = error_mark_node;
5852 break;
5854 else if (!current_function_decl)
5856 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5857 "inside a function");
5858 postfix_expression = error_mark_node;
5859 break;
5861 else
5863 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5864 saved_in_statement = parser->in_statement;
5865 parser->in_statement |= IN_CILK_SPAWN;
5867 cfun->calls_cilk_spawn = 1;
5868 postfix_expression =
5869 cp_parser_postfix_expression (parser, false, false,
5870 false, false, &idk);
5871 if (!flag_cilkplus)
5873 error_at (token->location, "-fcilkplus must be enabled to use"
5874 " %<_Cilk_spawn%>");
5875 cfun->calls_cilk_spawn = 0;
5877 else if (saved_in_statement & IN_CILK_SPAWN)
5879 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5880 "are not permitted");
5881 postfix_expression = error_mark_node;
5882 cfun->calls_cilk_spawn = 0;
5884 else
5886 postfix_expression = build_cilk_spawn (token->location,
5887 postfix_expression);
5888 if (postfix_expression != error_mark_node)
5889 SET_EXPR_LOCATION (postfix_expression, input_location);
5890 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5892 break;
5895 case RID_BUILTIN_SHUFFLE:
5897 vec<tree, va_gc> *vec;
5898 unsigned int i;
5899 tree p;
5901 cp_lexer_consume_token (parser->lexer);
5902 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5903 /*cast_p=*/false, /*allow_expansion_p=*/true,
5904 /*non_constant_p=*/NULL);
5905 if (vec == NULL)
5906 return error_mark_node;
5908 FOR_EACH_VEC_ELT (*vec, i, p)
5909 mark_exp_read (p);
5911 if (vec->length () == 2)
5912 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5913 tf_warning_or_error);
5914 else if (vec->length () == 3)
5915 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5916 tf_warning_or_error);
5917 else
5919 error_at (loc, "wrong number of arguments to "
5920 "%<__builtin_shuffle%>");
5921 return error_mark_node;
5923 break;
5926 default:
5928 tree type;
5930 /* If the next thing is a simple-type-specifier, we may be
5931 looking at a functional cast. We could also be looking at
5932 an id-expression. So, we try the functional cast, and if
5933 that doesn't work we fall back to the primary-expression. */
5934 cp_parser_parse_tentatively (parser);
5935 /* Look for the simple-type-specifier. */
5936 type = cp_parser_simple_type_specifier (parser,
5937 /*decl_specs=*/NULL,
5938 CP_PARSER_FLAGS_NONE);
5939 /* Parse the cast itself. */
5940 if (!cp_parser_error_occurred (parser))
5941 postfix_expression
5942 = cp_parser_functional_cast (parser, type);
5943 /* If that worked, we're done. */
5944 if (cp_parser_parse_definitely (parser))
5945 break;
5947 /* If the functional-cast didn't work out, try a
5948 compound-literal. */
5949 if (cp_parser_allow_gnu_extensions_p (parser)
5950 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5952 tree initializer = NULL_TREE;
5954 cp_parser_parse_tentatively (parser);
5956 /* Avoid calling cp_parser_type_id pointlessly, see comment
5957 in cp_parser_cast_expression about c++/29234. */
5958 if (!cp_parser_compound_literal_p (parser))
5959 cp_parser_simulate_error (parser);
5960 else
5962 /* Parse the type. */
5963 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5964 parser->in_type_id_in_expr_p = true;
5965 type = cp_parser_type_id (parser);
5966 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5967 /* Look for the `)'. */
5968 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5971 /* If things aren't going well, there's no need to
5972 keep going. */
5973 if (!cp_parser_error_occurred (parser))
5975 bool non_constant_p;
5976 /* Parse the brace-enclosed initializer list. */
5977 initializer = cp_parser_braced_list (parser,
5978 &non_constant_p);
5980 /* If that worked, we're definitely looking at a
5981 compound-literal expression. */
5982 if (cp_parser_parse_definitely (parser))
5984 /* Warn the user that a compound literal is not
5985 allowed in standard C++. */
5986 pedwarn (input_location, OPT_Wpedantic,
5987 "ISO C++ forbids compound-literals");
5988 /* For simplicity, we disallow compound literals in
5989 constant-expressions. We could
5990 allow compound literals of integer type, whose
5991 initializer was a constant, in constant
5992 expressions. Permitting that usage, as a further
5993 extension, would not change the meaning of any
5994 currently accepted programs. (Of course, as
5995 compound literals are not part of ISO C++, the
5996 standard has nothing to say.) */
5997 if (cp_parser_non_integral_constant_expression (parser,
5998 NIC_NCC))
6000 postfix_expression = error_mark_node;
6001 break;
6003 /* Form the representation of the compound-literal. */
6004 postfix_expression
6005 = finish_compound_literal (type, initializer,
6006 tf_warning_or_error);
6007 break;
6011 /* It must be a primary-expression. */
6012 postfix_expression
6013 = cp_parser_primary_expression (parser, address_p, cast_p,
6014 /*template_arg_p=*/false,
6015 decltype_p,
6016 &idk);
6018 break;
6021 /* Note that we don't need to worry about calling build_cplus_new on a
6022 class-valued CALL_EXPR in decltype when it isn't the end of the
6023 postfix-expression; unary_complex_lvalue will take care of that for
6024 all these cases. */
6026 /* Keep looping until the postfix-expression is complete. */
6027 while (true)
6029 if (idk == CP_ID_KIND_UNQUALIFIED
6030 && identifier_p (postfix_expression)
6031 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6032 /* It is not a Koenig lookup function call. */
6033 postfix_expression
6034 = unqualified_name_lookup_error (postfix_expression);
6036 /* Peek at the next token. */
6037 token = cp_lexer_peek_token (parser->lexer);
6039 switch (token->type)
6041 case CPP_OPEN_SQUARE:
6042 if (cp_next_tokens_can_be_std_attribute_p (parser))
6044 cp_parser_error (parser,
6045 "two consecutive %<[%> shall "
6046 "only introduce an attribute");
6047 return error_mark_node;
6049 postfix_expression
6050 = cp_parser_postfix_open_square_expression (parser,
6051 postfix_expression,
6052 false,
6053 decltype_p);
6054 idk = CP_ID_KIND_NONE;
6055 is_member_access = false;
6056 break;
6058 case CPP_OPEN_PAREN:
6059 /* postfix-expression ( expression-list [opt] ) */
6061 bool koenig_p;
6062 bool is_builtin_constant_p;
6063 bool saved_integral_constant_expression_p = false;
6064 bool saved_non_integral_constant_expression_p = false;
6065 tsubst_flags_t complain = complain_flags (decltype_p);
6066 vec<tree, va_gc> *args;
6068 is_member_access = false;
6070 is_builtin_constant_p
6071 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6072 if (is_builtin_constant_p)
6074 /* The whole point of __builtin_constant_p is to allow
6075 non-constant expressions to appear as arguments. */
6076 saved_integral_constant_expression_p
6077 = parser->integral_constant_expression_p;
6078 saved_non_integral_constant_expression_p
6079 = parser->non_integral_constant_expression_p;
6080 parser->integral_constant_expression_p = false;
6082 args = (cp_parser_parenthesized_expression_list
6083 (parser, non_attr,
6084 /*cast_p=*/false, /*allow_expansion_p=*/true,
6085 /*non_constant_p=*/NULL,
6086 /*want_literal_zero_p=*/warn_memset_transposed_args));
6087 if (is_builtin_constant_p)
6089 parser->integral_constant_expression_p
6090 = saved_integral_constant_expression_p;
6091 parser->non_integral_constant_expression_p
6092 = saved_non_integral_constant_expression_p;
6095 if (args == NULL)
6097 postfix_expression = error_mark_node;
6098 break;
6101 /* Function calls are not permitted in
6102 constant-expressions. */
6103 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6104 && cp_parser_non_integral_constant_expression (parser,
6105 NIC_FUNC_CALL))
6107 postfix_expression = error_mark_node;
6108 release_tree_vector (args);
6109 break;
6112 koenig_p = false;
6113 if (idk == CP_ID_KIND_UNQUALIFIED
6114 || idk == CP_ID_KIND_TEMPLATE_ID)
6116 if (identifier_p (postfix_expression))
6118 if (!args->is_empty ())
6120 koenig_p = true;
6121 if (!any_type_dependent_arguments_p (args))
6122 postfix_expression
6123 = perform_koenig_lookup (postfix_expression, args,
6124 complain);
6126 else
6127 postfix_expression
6128 = unqualified_fn_lookup_error (postfix_expression);
6130 /* We do not perform argument-dependent lookup if
6131 normal lookup finds a non-function, in accordance
6132 with the expected resolution of DR 218. */
6133 else if (!args->is_empty ()
6134 && is_overloaded_fn (postfix_expression))
6136 tree fn = get_first_fn (postfix_expression);
6137 fn = STRIP_TEMPLATE (fn);
6139 /* Do not do argument dependent lookup if regular
6140 lookup finds a member function or a block-scope
6141 function declaration. [basic.lookup.argdep]/3 */
6142 if (!DECL_FUNCTION_MEMBER_P (fn)
6143 && !DECL_LOCAL_FUNCTION_P (fn))
6145 koenig_p = true;
6146 if (!any_type_dependent_arguments_p (args))
6147 postfix_expression
6148 = perform_koenig_lookup (postfix_expression, args,
6149 complain);
6154 if (warn_memset_transposed_args)
6156 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6157 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6158 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6159 && vec_safe_length (args) == 3
6160 && integer_zerop ((*args)[2])
6161 && LITERAL_ZERO_P ((*args)[2])
6162 && !(integer_zerop ((*args)[1])
6163 && LITERAL_ZERO_P ((*args)[1])))
6164 warning (OPT_Wmemset_transposed_args,
6165 "%<memset%> used with constant zero length "
6166 "parameter; this could be due to transposed "
6167 "parameters");
6169 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6170 to avoid leaking those into folder and middle-end. */
6171 unsigned int i;
6172 tree arg;
6173 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6174 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6175 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6178 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6180 tree instance = TREE_OPERAND (postfix_expression, 0);
6181 tree fn = TREE_OPERAND (postfix_expression, 1);
6183 if (processing_template_decl
6184 && (type_dependent_expression_p (instance)
6185 || (!BASELINK_P (fn)
6186 && TREE_CODE (fn) != FIELD_DECL)
6187 || type_dependent_expression_p (fn)
6188 || any_type_dependent_arguments_p (args)))
6190 postfix_expression
6191 = build_nt_call_vec (postfix_expression, args);
6192 release_tree_vector (args);
6193 break;
6196 if (BASELINK_P (fn))
6198 postfix_expression
6199 = (build_new_method_call
6200 (instance, fn, &args, NULL_TREE,
6201 (idk == CP_ID_KIND_QUALIFIED
6202 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6203 : LOOKUP_NORMAL),
6204 /*fn_p=*/NULL,
6205 complain));
6207 else
6208 postfix_expression
6209 = finish_call_expr (postfix_expression, &args,
6210 /*disallow_virtual=*/false,
6211 /*koenig_p=*/false,
6212 complain);
6214 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6215 || TREE_CODE (postfix_expression) == MEMBER_REF
6216 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6217 postfix_expression = (build_offset_ref_call_from_tree
6218 (postfix_expression, &args,
6219 complain));
6220 else if (idk == CP_ID_KIND_QUALIFIED)
6221 /* A call to a static class member, or a namespace-scope
6222 function. */
6223 postfix_expression
6224 = finish_call_expr (postfix_expression, &args,
6225 /*disallow_virtual=*/true,
6226 koenig_p,
6227 complain);
6228 else
6229 /* All other function calls. */
6230 postfix_expression
6231 = finish_call_expr (postfix_expression, &args,
6232 /*disallow_virtual=*/false,
6233 koenig_p,
6234 complain);
6236 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6237 idk = CP_ID_KIND_NONE;
6239 release_tree_vector (args);
6241 break;
6243 case CPP_DOT:
6244 case CPP_DEREF:
6245 /* postfix-expression . template [opt] id-expression
6246 postfix-expression . pseudo-destructor-name
6247 postfix-expression -> template [opt] id-expression
6248 postfix-expression -> pseudo-destructor-name */
6250 /* Consume the `.' or `->' operator. */
6251 cp_lexer_consume_token (parser->lexer);
6253 postfix_expression
6254 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6255 postfix_expression,
6256 false, &idk, loc);
6258 is_member_access = true;
6259 break;
6261 case CPP_PLUS_PLUS:
6262 /* postfix-expression ++ */
6263 /* Consume the `++' token. */
6264 cp_lexer_consume_token (parser->lexer);
6265 /* Generate a representation for the complete expression. */
6266 postfix_expression
6267 = finish_increment_expr (postfix_expression,
6268 POSTINCREMENT_EXPR);
6269 /* Increments may not appear in constant-expressions. */
6270 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6271 postfix_expression = error_mark_node;
6272 idk = CP_ID_KIND_NONE;
6273 is_member_access = false;
6274 break;
6276 case CPP_MINUS_MINUS:
6277 /* postfix-expression -- */
6278 /* Consume the `--' token. */
6279 cp_lexer_consume_token (parser->lexer);
6280 /* Generate a representation for the complete expression. */
6281 postfix_expression
6282 = finish_increment_expr (postfix_expression,
6283 POSTDECREMENT_EXPR);
6284 /* Decrements may not appear in constant-expressions. */
6285 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6286 postfix_expression = error_mark_node;
6287 idk = CP_ID_KIND_NONE;
6288 is_member_access = false;
6289 break;
6291 default:
6292 if (pidk_return != NULL)
6293 * pidk_return = idk;
6294 if (member_access_only_p)
6295 return is_member_access? postfix_expression : error_mark_node;
6296 else
6297 return postfix_expression;
6301 /* We should never get here. */
6302 gcc_unreachable ();
6303 return error_mark_node;
6306 /* This function parses Cilk Plus array notations. If a normal array expr. is
6307 parsed then the array index is passed back to the caller through *INIT_INDEX
6308 and the function returns a NULL_TREE. If array notation expr. is parsed,
6309 then *INIT_INDEX is ignored by the caller and the function returns
6310 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6311 error_mark_node. */
6313 static tree
6314 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6315 tree array_value)
6317 cp_token *token = NULL;
6318 tree length_index, stride = NULL_TREE, value_tree, array_type;
6319 if (!array_value || array_value == error_mark_node)
6321 cp_parser_skip_to_end_of_statement (parser);
6322 return error_mark_node;
6325 array_type = TREE_TYPE (array_value);
6327 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6328 parser->colon_corrects_to_scope_p = false;
6329 token = cp_lexer_peek_token (parser->lexer);
6331 if (!token)
6333 cp_parser_error (parser, "expected %<:%> or numeral");
6334 return error_mark_node;
6336 else if (token->type == CPP_COLON)
6338 /* Consume the ':'. */
6339 cp_lexer_consume_token (parser->lexer);
6341 /* If we are here, then we have a case like this A[:]. */
6342 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6344 cp_parser_error (parser, "expected %<]%>");
6345 cp_parser_skip_to_end_of_statement (parser);
6346 return error_mark_node;
6348 *init_index = NULL_TREE;
6349 stride = NULL_TREE;
6350 length_index = NULL_TREE;
6352 else
6354 /* If we are here, then there are three valid possibilities:
6355 1. ARRAY [ EXP ]
6356 2. ARRAY [ EXP : EXP ]
6357 3. ARRAY [ EXP : EXP : EXP ] */
6359 *init_index = cp_parser_expression (parser, false, NULL);
6360 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6362 /* This indicates that we have a normal array expression. */
6363 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6364 return NULL_TREE;
6367 /* Consume the ':'. */
6368 cp_lexer_consume_token (parser->lexer);
6369 length_index = cp_parser_expression (parser, false, NULL);
6370 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6372 cp_lexer_consume_token (parser->lexer);
6373 stride = cp_parser_expression (parser, false, NULL);
6376 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6378 if (*init_index == error_mark_node || length_index == error_mark_node
6379 || stride == error_mark_node)
6381 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6382 cp_lexer_consume_token (parser->lexer);
6383 return error_mark_node;
6385 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6387 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6388 length_index, stride, array_type);
6389 return value_tree;
6392 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6393 by cp_parser_builtin_offsetof. We're looking for
6395 postfix-expression [ expression ]
6396 postfix-expression [ braced-init-list ] (C++11)
6398 FOR_OFFSETOF is set if we're being called in that context, which
6399 changes how we deal with integer constant expressions. */
6401 static tree
6402 cp_parser_postfix_open_square_expression (cp_parser *parser,
6403 tree postfix_expression,
6404 bool for_offsetof,
6405 bool decltype_p)
6407 tree index = NULL_TREE;
6408 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6409 bool saved_greater_than_is_operator_p;
6411 /* Consume the `[' token. */
6412 cp_lexer_consume_token (parser->lexer);
6414 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6415 parser->greater_than_is_operator_p = true;
6417 /* Parse the index expression. */
6418 /* ??? For offsetof, there is a question of what to allow here. If
6419 offsetof is not being used in an integral constant expression context,
6420 then we *could* get the right answer by computing the value at runtime.
6421 If we are in an integral constant expression context, then we might
6422 could accept any constant expression; hard to say without analysis.
6423 Rather than open the barn door too wide right away, allow only integer
6424 constant expressions here. */
6425 if (for_offsetof)
6426 index = cp_parser_constant_expression (parser, false, NULL);
6427 else
6429 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6431 bool expr_nonconst_p;
6432 cp_lexer_set_source_position (parser->lexer);
6433 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6434 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6435 if (flag_cilkplus
6436 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6438 error_at (cp_lexer_peek_token (parser->lexer)->location,
6439 "braced list index is not allowed with array "
6440 "notation");
6441 cp_parser_skip_to_end_of_statement (parser);
6442 return error_mark_node;
6445 else if (flag_cilkplus)
6447 /* Here are have these two options:
6448 ARRAY[EXP : EXP] - Array notation expr with default
6449 stride of 1.
6450 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6451 stride. */
6452 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6453 postfix_expression);
6454 if (an_exp)
6455 return an_exp;
6457 else
6458 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6461 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6463 /* Look for the closing `]'. */
6464 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6466 /* Build the ARRAY_REF. */
6467 postfix_expression = grok_array_decl (loc, postfix_expression,
6468 index, decltype_p);
6470 /* When not doing offsetof, array references are not permitted in
6471 constant-expressions. */
6472 if (!for_offsetof
6473 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6474 postfix_expression = error_mark_node;
6476 return postfix_expression;
6479 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6480 by cp_parser_builtin_offsetof. We're looking for
6482 postfix-expression . template [opt] id-expression
6483 postfix-expression . pseudo-destructor-name
6484 postfix-expression -> template [opt] id-expression
6485 postfix-expression -> pseudo-destructor-name
6487 FOR_OFFSETOF is set if we're being called in that context. That sorta
6488 limits what of the above we'll actually accept, but nevermind.
6489 TOKEN_TYPE is the "." or "->" token, which will already have been
6490 removed from the stream. */
6492 static tree
6493 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6494 enum cpp_ttype token_type,
6495 tree postfix_expression,
6496 bool for_offsetof, cp_id_kind *idk,
6497 location_t location)
6499 tree name;
6500 bool dependent_p;
6501 bool pseudo_destructor_p;
6502 tree scope = NULL_TREE;
6504 /* If this is a `->' operator, dereference the pointer. */
6505 if (token_type == CPP_DEREF)
6506 postfix_expression = build_x_arrow (location, postfix_expression,
6507 tf_warning_or_error);
6508 /* Check to see whether or not the expression is type-dependent. */
6509 dependent_p = type_dependent_expression_p (postfix_expression);
6510 /* The identifier following the `->' or `.' is not qualified. */
6511 parser->scope = NULL_TREE;
6512 parser->qualifying_scope = NULL_TREE;
6513 parser->object_scope = NULL_TREE;
6514 *idk = CP_ID_KIND_NONE;
6516 /* Enter the scope corresponding to the type of the object
6517 given by the POSTFIX_EXPRESSION. */
6518 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6520 scope = TREE_TYPE (postfix_expression);
6521 /* According to the standard, no expression should ever have
6522 reference type. Unfortunately, we do not currently match
6523 the standard in this respect in that our internal representation
6524 of an expression may have reference type even when the standard
6525 says it does not. Therefore, we have to manually obtain the
6526 underlying type here. */
6527 scope = non_reference (scope);
6528 /* The type of the POSTFIX_EXPRESSION must be complete. */
6529 if (scope == unknown_type_node)
6531 error_at (location, "%qE does not have class type",
6532 postfix_expression);
6533 scope = NULL_TREE;
6535 /* Unlike the object expression in other contexts, *this is not
6536 required to be of complete type for purposes of class member
6537 access (5.2.5) outside the member function body. */
6538 else if (postfix_expression != current_class_ref
6539 && !(processing_template_decl && scope == current_class_type))
6540 scope = complete_type_or_else (scope, NULL_TREE);
6541 /* Let the name lookup machinery know that we are processing a
6542 class member access expression. */
6543 parser->context->object_type = scope;
6544 /* If something went wrong, we want to be able to discern that case,
6545 as opposed to the case where there was no SCOPE due to the type
6546 of expression being dependent. */
6547 if (!scope)
6548 scope = error_mark_node;
6549 /* If the SCOPE was erroneous, make the various semantic analysis
6550 functions exit quickly -- and without issuing additional error
6551 messages. */
6552 if (scope == error_mark_node)
6553 postfix_expression = error_mark_node;
6556 /* Assume this expression is not a pseudo-destructor access. */
6557 pseudo_destructor_p = false;
6559 /* If the SCOPE is a scalar type, then, if this is a valid program,
6560 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6561 is type dependent, it can be pseudo-destructor-name or something else.
6562 Try to parse it as pseudo-destructor-name first. */
6563 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6565 tree s;
6566 tree type;
6568 cp_parser_parse_tentatively (parser);
6569 /* Parse the pseudo-destructor-name. */
6570 s = NULL_TREE;
6571 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6572 &s, &type);
6573 if (dependent_p
6574 && (cp_parser_error_occurred (parser)
6575 || !SCALAR_TYPE_P (type)))
6576 cp_parser_abort_tentative_parse (parser);
6577 else if (cp_parser_parse_definitely (parser))
6579 pseudo_destructor_p = true;
6580 postfix_expression
6581 = finish_pseudo_destructor_expr (postfix_expression,
6582 s, type, location);
6586 if (!pseudo_destructor_p)
6588 /* If the SCOPE is not a scalar type, we are looking at an
6589 ordinary class member access expression, rather than a
6590 pseudo-destructor-name. */
6591 bool template_p;
6592 cp_token *token = cp_lexer_peek_token (parser->lexer);
6593 /* Parse the id-expression. */
6594 name = (cp_parser_id_expression
6595 (parser,
6596 cp_parser_optional_template_keyword (parser),
6597 /*check_dependency_p=*/true,
6598 &template_p,
6599 /*declarator_p=*/false,
6600 /*optional_p=*/false));
6601 /* In general, build a SCOPE_REF if the member name is qualified.
6602 However, if the name was not dependent and has already been
6603 resolved; there is no need to build the SCOPE_REF. For example;
6605 struct X { void f(); };
6606 template <typename T> void f(T* t) { t->X::f(); }
6608 Even though "t" is dependent, "X::f" is not and has been resolved
6609 to a BASELINK; there is no need to include scope information. */
6611 /* But we do need to remember that there was an explicit scope for
6612 virtual function calls. */
6613 if (parser->scope)
6614 *idk = CP_ID_KIND_QUALIFIED;
6616 /* If the name is a template-id that names a type, we will get a
6617 TYPE_DECL here. That is invalid code. */
6618 if (TREE_CODE (name) == TYPE_DECL)
6620 error_at (token->location, "invalid use of %qD", name);
6621 postfix_expression = error_mark_node;
6623 else
6625 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6627 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6629 error_at (token->location, "%<%D::%D%> is not a class member",
6630 parser->scope, name);
6631 postfix_expression = error_mark_node;
6633 else
6634 name = build_qualified_name (/*type=*/NULL_TREE,
6635 parser->scope,
6636 name,
6637 template_p);
6638 parser->scope = NULL_TREE;
6639 parser->qualifying_scope = NULL_TREE;
6640 parser->object_scope = NULL_TREE;
6642 if (parser->scope && name && BASELINK_P (name))
6643 adjust_result_of_qualified_name_lookup
6644 (name, parser->scope, scope);
6645 postfix_expression
6646 = finish_class_member_access_expr (postfix_expression, name,
6647 template_p,
6648 tf_warning_or_error);
6652 /* We no longer need to look up names in the scope of the object on
6653 the left-hand side of the `.' or `->' operator. */
6654 parser->context->object_type = NULL_TREE;
6656 /* Outside of offsetof, these operators may not appear in
6657 constant-expressions. */
6658 if (!for_offsetof
6659 && (cp_parser_non_integral_constant_expression
6660 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6661 postfix_expression = error_mark_node;
6663 return postfix_expression;
6666 /* Cache of LITERAL_ZERO_P constants. */
6668 static GTY(()) tree literal_zeros[itk_none];
6670 /* Parse a parenthesized expression-list.
6672 expression-list:
6673 assignment-expression
6674 expression-list, assignment-expression
6676 attribute-list:
6677 expression-list
6678 identifier
6679 identifier, expression-list
6681 CAST_P is true if this expression is the target of a cast.
6683 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6684 argument pack.
6686 Returns a vector of trees. Each element is a representation of an
6687 assignment-expression. NULL is returned if the ( and or ) are
6688 missing. An empty, but allocated, vector is returned on no
6689 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6690 if we are parsing an attribute list for an attribute that wants a
6691 plain identifier argument, normal_attr for an attribute that wants
6692 an expression, or non_attr if we aren't parsing an attribute list. If
6693 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6694 not all of the expressions in the list were constant.
6695 WANT_LITERAL_ZERO_P is true if the caller is interested in
6696 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6697 immediately, this can be removed. */
6699 static vec<tree, va_gc> *
6700 cp_parser_parenthesized_expression_list (cp_parser* parser,
6701 int is_attribute_list,
6702 bool cast_p,
6703 bool allow_expansion_p,
6704 bool *non_constant_p,
6705 bool want_literal_zero_p)
6707 vec<tree, va_gc> *expression_list;
6708 bool fold_expr_p = is_attribute_list != non_attr;
6709 tree identifier = NULL_TREE;
6710 bool saved_greater_than_is_operator_p;
6712 /* Assume all the expressions will be constant. */
6713 if (non_constant_p)
6714 *non_constant_p = false;
6716 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6717 return NULL;
6719 expression_list = make_tree_vector ();
6721 /* Within a parenthesized expression, a `>' token is always
6722 the greater-than operator. */
6723 saved_greater_than_is_operator_p
6724 = parser->greater_than_is_operator_p;
6725 parser->greater_than_is_operator_p = true;
6727 /* Consume expressions until there are no more. */
6728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6729 while (true)
6731 tree expr;
6733 /* At the beginning of attribute lists, check to see if the
6734 next token is an identifier. */
6735 if (is_attribute_list == id_attr
6736 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6738 cp_token *token;
6740 /* Consume the identifier. */
6741 token = cp_lexer_consume_token (parser->lexer);
6742 /* Save the identifier. */
6743 identifier = token->u.value;
6745 else
6747 bool expr_non_constant_p;
6749 /* Parse the next assignment-expression. */
6750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6752 /* A braced-init-list. */
6753 cp_lexer_set_source_position (parser->lexer);
6754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6755 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6756 if (non_constant_p && expr_non_constant_p)
6757 *non_constant_p = true;
6759 else if (non_constant_p)
6761 expr = (cp_parser_constant_expression
6762 (parser, /*allow_non_constant_p=*/true,
6763 &expr_non_constant_p));
6764 if (expr_non_constant_p)
6765 *non_constant_p = true;
6767 else
6769 expr = NULL_TREE;
6770 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6771 switch (tok->type)
6773 case CPP_NUMBER:
6774 case CPP_CHAR:
6775 case CPP_WCHAR:
6776 case CPP_CHAR16:
6777 case CPP_CHAR32:
6778 /* If a parameter is literal zero alone, remember it
6779 for -Wmemset-transposed-args warning. */
6780 if (integer_zerop (tok->u.value)
6781 && !TREE_OVERFLOW (tok->u.value)
6782 && want_literal_zero_p
6783 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6784 == CPP_COMMA
6785 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6786 == CPP_CLOSE_PAREN))
6788 unsigned int i;
6789 for (i = 0; i < itk_none; ++i)
6790 if (TREE_TYPE (tok->u.value) == integer_types[i])
6791 break;
6792 if (i < itk_none && literal_zeros[i])
6793 expr = literal_zeros[i];
6794 else
6796 expr = copy_node (tok->u.value);
6797 LITERAL_ZERO_P (expr) = 1;
6798 if (i < itk_none)
6799 literal_zeros[i] = expr;
6801 /* Consume the 0 token (or '\0', 0LL etc.). */
6802 cp_lexer_consume_token (parser->lexer);
6804 break;
6805 default:
6806 break;
6808 if (expr == NULL_TREE)
6809 expr = cp_parser_assignment_expression (parser, cast_p,
6810 NULL);
6813 if (fold_expr_p)
6814 expr = fold_non_dependent_expr (expr);
6816 /* If we have an ellipsis, then this is an expression
6817 expansion. */
6818 if (allow_expansion_p
6819 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6821 /* Consume the `...'. */
6822 cp_lexer_consume_token (parser->lexer);
6824 /* Build the argument pack. */
6825 expr = make_pack_expansion (expr);
6828 /* Add it to the list. We add error_mark_node
6829 expressions to the list, so that we can still tell if
6830 the correct form for a parenthesized expression-list
6831 is found. That gives better errors. */
6832 vec_safe_push (expression_list, expr);
6834 if (expr == error_mark_node)
6835 goto skip_comma;
6838 /* After the first item, attribute lists look the same as
6839 expression lists. */
6840 is_attribute_list = non_attr;
6842 get_comma:;
6843 /* If the next token isn't a `,', then we are done. */
6844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6845 break;
6847 /* Otherwise, consume the `,' and keep going. */
6848 cp_lexer_consume_token (parser->lexer);
6851 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6853 int ending;
6855 skip_comma:;
6856 /* We try and resync to an unnested comma, as that will give the
6857 user better diagnostics. */
6858 ending = cp_parser_skip_to_closing_parenthesis (parser,
6859 /*recovering=*/true,
6860 /*or_comma=*/true,
6861 /*consume_paren=*/true);
6862 if (ending < 0)
6863 goto get_comma;
6864 if (!ending)
6866 parser->greater_than_is_operator_p
6867 = saved_greater_than_is_operator_p;
6868 return NULL;
6872 parser->greater_than_is_operator_p
6873 = saved_greater_than_is_operator_p;
6875 if (identifier)
6876 vec_safe_insert (expression_list, 0, identifier);
6878 return expression_list;
6881 /* Parse a pseudo-destructor-name.
6883 pseudo-destructor-name:
6884 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6885 :: [opt] nested-name-specifier template template-id :: ~ type-name
6886 :: [opt] nested-name-specifier [opt] ~ type-name
6888 If either of the first two productions is used, sets *SCOPE to the
6889 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6890 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6891 or ERROR_MARK_NODE if the parse fails. */
6893 static void
6894 cp_parser_pseudo_destructor_name (cp_parser* parser,
6895 tree object,
6896 tree* scope,
6897 tree* type)
6899 bool nested_name_specifier_p;
6901 /* Handle ~auto. */
6902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6903 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6904 && !type_dependent_expression_p (object))
6906 if (cxx_dialect < cxx1y)
6907 pedwarn (input_location, 0,
6908 "%<~auto%> only available with "
6909 "-std=c++1y or -std=gnu++1y");
6910 cp_lexer_consume_token (parser->lexer);
6911 cp_lexer_consume_token (parser->lexer);
6912 *scope = NULL_TREE;
6913 *type = TREE_TYPE (object);
6914 return;
6917 /* Assume that things will not work out. */
6918 *type = error_mark_node;
6920 /* Look for the optional `::' operator. */
6921 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6922 /* Look for the optional nested-name-specifier. */
6923 nested_name_specifier_p
6924 = (cp_parser_nested_name_specifier_opt (parser,
6925 /*typename_keyword_p=*/false,
6926 /*check_dependency_p=*/true,
6927 /*type_p=*/false,
6928 /*is_declaration=*/false)
6929 != NULL_TREE);
6930 /* Now, if we saw a nested-name-specifier, we might be doing the
6931 second production. */
6932 if (nested_name_specifier_p
6933 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6935 /* Consume the `template' keyword. */
6936 cp_lexer_consume_token (parser->lexer);
6937 /* Parse the template-id. */
6938 cp_parser_template_id (parser,
6939 /*template_keyword_p=*/true,
6940 /*check_dependency_p=*/false,
6941 class_type,
6942 /*is_declaration=*/true);
6943 /* Look for the `::' token. */
6944 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6946 /* If the next token is not a `~', then there might be some
6947 additional qualification. */
6948 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6950 /* At this point, we're looking for "type-name :: ~". The type-name
6951 must not be a class-name, since this is a pseudo-destructor. So,
6952 it must be either an enum-name, or a typedef-name -- both of which
6953 are just identifiers. So, we peek ahead to check that the "::"
6954 and "~" tokens are present; if they are not, then we can avoid
6955 calling type_name. */
6956 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6957 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6958 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6960 cp_parser_error (parser, "non-scalar type");
6961 return;
6964 /* Look for the type-name. */
6965 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6966 if (*scope == error_mark_node)
6967 return;
6969 /* Look for the `::' token. */
6970 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6972 else
6973 *scope = NULL_TREE;
6975 /* Look for the `~'. */
6976 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6978 /* Once we see the ~, this has to be a pseudo-destructor. */
6979 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6980 cp_parser_commit_to_topmost_tentative_parse (parser);
6982 /* Look for the type-name again. We are not responsible for
6983 checking that it matches the first type-name. */
6984 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6987 /* Parse a unary-expression.
6989 unary-expression:
6990 postfix-expression
6991 ++ cast-expression
6992 -- cast-expression
6993 unary-operator cast-expression
6994 sizeof unary-expression
6995 sizeof ( type-id )
6996 alignof ( type-id ) [C++0x]
6997 new-expression
6998 delete-expression
7000 GNU Extensions:
7002 unary-expression:
7003 __extension__ cast-expression
7004 __alignof__ unary-expression
7005 __alignof__ ( type-id )
7006 alignof unary-expression [C++0x]
7007 __real__ cast-expression
7008 __imag__ cast-expression
7009 && identifier
7010 sizeof ( type-id ) { initializer-list , [opt] }
7011 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7012 __alignof__ ( type-id ) { initializer-list , [opt] }
7014 ADDRESS_P is true iff the unary-expression is appearing as the
7015 operand of the `&' operator. CAST_P is true if this expression is
7016 the target of a cast.
7018 Returns a representation of the expression. */
7020 static tree
7021 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7022 bool decltype_p, cp_id_kind * pidk)
7024 cp_token *token;
7025 enum tree_code unary_operator;
7027 /* Peek at the next token. */
7028 token = cp_lexer_peek_token (parser->lexer);
7029 /* Some keywords give away the kind of expression. */
7030 if (token->type == CPP_KEYWORD)
7032 enum rid keyword = token->keyword;
7034 switch (keyword)
7036 case RID_ALIGNOF:
7037 case RID_SIZEOF:
7039 tree operand, ret;
7040 enum tree_code op;
7041 location_t first_loc;
7043 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7044 /* Consume the token. */
7045 cp_lexer_consume_token (parser->lexer);
7046 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7047 /* Parse the operand. */
7048 operand = cp_parser_sizeof_operand (parser, keyword);
7050 if (TYPE_P (operand))
7051 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7052 else
7054 /* ISO C++ defines alignof only with types, not with
7055 expressions. So pedwarn if alignof is used with a non-
7056 type expression. However, __alignof__ is ok. */
7057 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7058 pedwarn (token->location, OPT_Wpedantic,
7059 "ISO C++ does not allow %<alignof%> "
7060 "with a non-type");
7062 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7064 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7065 SIZEOF_EXPR with the original operand. */
7066 if (op == SIZEOF_EXPR && ret != error_mark_node)
7068 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7070 if (!processing_template_decl && TYPE_P (operand))
7072 ret = build_min (SIZEOF_EXPR, size_type_node,
7073 build1 (NOP_EXPR, operand,
7074 error_mark_node));
7075 SIZEOF_EXPR_TYPE_P (ret) = 1;
7077 else
7078 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7079 TREE_SIDE_EFFECTS (ret) = 0;
7080 TREE_READONLY (ret) = 1;
7082 SET_EXPR_LOCATION (ret, first_loc);
7084 return ret;
7087 case RID_NEW:
7088 return cp_parser_new_expression (parser);
7090 case RID_DELETE:
7091 return cp_parser_delete_expression (parser);
7093 case RID_EXTENSION:
7095 /* The saved value of the PEDANTIC flag. */
7096 int saved_pedantic;
7097 tree expr;
7099 /* Save away the PEDANTIC flag. */
7100 cp_parser_extension_opt (parser, &saved_pedantic);
7101 /* Parse the cast-expression. */
7102 expr = cp_parser_simple_cast_expression (parser);
7103 /* Restore the PEDANTIC flag. */
7104 pedantic = saved_pedantic;
7106 return expr;
7109 case RID_REALPART:
7110 case RID_IMAGPART:
7112 tree expression;
7114 /* Consume the `__real__' or `__imag__' token. */
7115 cp_lexer_consume_token (parser->lexer);
7116 /* Parse the cast-expression. */
7117 expression = cp_parser_simple_cast_expression (parser);
7118 /* Create the complete representation. */
7119 return build_x_unary_op (token->location,
7120 (keyword == RID_REALPART
7121 ? REALPART_EXPR : IMAGPART_EXPR),
7122 expression,
7123 tf_warning_or_error);
7125 break;
7127 case RID_TRANSACTION_ATOMIC:
7128 case RID_TRANSACTION_RELAXED:
7129 return cp_parser_transaction_expression (parser, keyword);
7131 case RID_NOEXCEPT:
7133 tree expr;
7134 const char *saved_message;
7135 bool saved_integral_constant_expression_p;
7136 bool saved_non_integral_constant_expression_p;
7137 bool saved_greater_than_is_operator_p;
7139 cp_lexer_consume_token (parser->lexer);
7140 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7142 saved_message = parser->type_definition_forbidden_message;
7143 parser->type_definition_forbidden_message
7144 = G_("types may not be defined in %<noexcept%> expressions");
7146 saved_integral_constant_expression_p
7147 = parser->integral_constant_expression_p;
7148 saved_non_integral_constant_expression_p
7149 = parser->non_integral_constant_expression_p;
7150 parser->integral_constant_expression_p = false;
7152 saved_greater_than_is_operator_p
7153 = parser->greater_than_is_operator_p;
7154 parser->greater_than_is_operator_p = true;
7156 ++cp_unevaluated_operand;
7157 ++c_inhibit_evaluation_warnings;
7158 expr = cp_parser_expression (parser, false, NULL);
7159 --c_inhibit_evaluation_warnings;
7160 --cp_unevaluated_operand;
7162 parser->greater_than_is_operator_p
7163 = saved_greater_than_is_operator_p;
7165 parser->integral_constant_expression_p
7166 = saved_integral_constant_expression_p;
7167 parser->non_integral_constant_expression_p
7168 = saved_non_integral_constant_expression_p;
7170 parser->type_definition_forbidden_message = saved_message;
7172 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7173 return finish_noexcept_expr (expr, tf_warning_or_error);
7176 default:
7177 break;
7181 /* Look for the `:: new' and `:: delete', which also signal the
7182 beginning of a new-expression, or delete-expression,
7183 respectively. If the next token is `::', then it might be one of
7184 these. */
7185 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7187 enum rid keyword;
7189 /* See if the token after the `::' is one of the keywords in
7190 which we're interested. */
7191 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7192 /* If it's `new', we have a new-expression. */
7193 if (keyword == RID_NEW)
7194 return cp_parser_new_expression (parser);
7195 /* Similarly, for `delete'. */
7196 else if (keyword == RID_DELETE)
7197 return cp_parser_delete_expression (parser);
7200 /* Look for a unary operator. */
7201 unary_operator = cp_parser_unary_operator (token);
7202 /* The `++' and `--' operators can be handled similarly, even though
7203 they are not technically unary-operators in the grammar. */
7204 if (unary_operator == ERROR_MARK)
7206 if (token->type == CPP_PLUS_PLUS)
7207 unary_operator = PREINCREMENT_EXPR;
7208 else if (token->type == CPP_MINUS_MINUS)
7209 unary_operator = PREDECREMENT_EXPR;
7210 /* Handle the GNU address-of-label extension. */
7211 else if (cp_parser_allow_gnu_extensions_p (parser)
7212 && token->type == CPP_AND_AND)
7214 tree identifier;
7215 tree expression;
7216 location_t loc = token->location;
7218 /* Consume the '&&' token. */
7219 cp_lexer_consume_token (parser->lexer);
7220 /* Look for the identifier. */
7221 identifier = cp_parser_identifier (parser);
7222 /* Create an expression representing the address. */
7223 expression = finish_label_address_expr (identifier, loc);
7224 if (cp_parser_non_integral_constant_expression (parser,
7225 NIC_ADDR_LABEL))
7226 expression = error_mark_node;
7227 return expression;
7230 if (unary_operator != ERROR_MARK)
7232 tree cast_expression;
7233 tree expression = error_mark_node;
7234 non_integral_constant non_constant_p = NIC_NONE;
7235 location_t loc = token->location;
7236 tsubst_flags_t complain = complain_flags (decltype_p);
7238 /* Consume the operator token. */
7239 token = cp_lexer_consume_token (parser->lexer);
7240 /* Parse the cast-expression. */
7241 cast_expression
7242 = cp_parser_cast_expression (parser,
7243 unary_operator == ADDR_EXPR,
7244 /*cast_p=*/false,
7245 /*decltype*/false,
7246 pidk);
7247 /* Now, build an appropriate representation. */
7248 switch (unary_operator)
7250 case INDIRECT_REF:
7251 non_constant_p = NIC_STAR;
7252 expression = build_x_indirect_ref (loc, cast_expression,
7253 RO_UNARY_STAR,
7254 complain);
7255 break;
7257 case ADDR_EXPR:
7258 non_constant_p = NIC_ADDR;
7259 /* Fall through. */
7260 case BIT_NOT_EXPR:
7261 expression = build_x_unary_op (loc, unary_operator,
7262 cast_expression,
7263 complain);
7264 break;
7266 case PREINCREMENT_EXPR:
7267 case PREDECREMENT_EXPR:
7268 non_constant_p = unary_operator == PREINCREMENT_EXPR
7269 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7270 /* Fall through. */
7271 case UNARY_PLUS_EXPR:
7272 case NEGATE_EXPR:
7273 case TRUTH_NOT_EXPR:
7274 expression = finish_unary_op_expr (loc, unary_operator,
7275 cast_expression, complain);
7276 break;
7278 default:
7279 gcc_unreachable ();
7282 if (non_constant_p != NIC_NONE
7283 && cp_parser_non_integral_constant_expression (parser,
7284 non_constant_p))
7285 expression = error_mark_node;
7287 return expression;
7290 return cp_parser_postfix_expression (parser, address_p, cast_p,
7291 /*member_access_only_p=*/false,
7292 decltype_p,
7293 pidk);
7296 static inline tree
7297 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7298 cp_id_kind * pidk)
7300 return cp_parser_unary_expression (parser, address_p, cast_p,
7301 /*decltype*/false, pidk);
7304 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7305 unary-operator, the corresponding tree code is returned. */
7307 static enum tree_code
7308 cp_parser_unary_operator (cp_token* token)
7310 switch (token->type)
7312 case CPP_MULT:
7313 return INDIRECT_REF;
7315 case CPP_AND:
7316 return ADDR_EXPR;
7318 case CPP_PLUS:
7319 return UNARY_PLUS_EXPR;
7321 case CPP_MINUS:
7322 return NEGATE_EXPR;
7324 case CPP_NOT:
7325 return TRUTH_NOT_EXPR;
7327 case CPP_COMPL:
7328 return BIT_NOT_EXPR;
7330 default:
7331 return ERROR_MARK;
7335 /* Parse a new-expression.
7337 new-expression:
7338 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7339 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7341 Returns a representation of the expression. */
7343 static tree
7344 cp_parser_new_expression (cp_parser* parser)
7346 bool global_scope_p;
7347 vec<tree, va_gc> *placement;
7348 tree type;
7349 vec<tree, va_gc> *initializer;
7350 tree nelts = NULL_TREE;
7351 tree ret;
7353 /* Look for the optional `::' operator. */
7354 global_scope_p
7355 = (cp_parser_global_scope_opt (parser,
7356 /*current_scope_valid_p=*/false)
7357 != NULL_TREE);
7358 /* Look for the `new' operator. */
7359 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7360 /* There's no easy way to tell a new-placement from the
7361 `( type-id )' construct. */
7362 cp_parser_parse_tentatively (parser);
7363 /* Look for a new-placement. */
7364 placement = cp_parser_new_placement (parser);
7365 /* If that didn't work out, there's no new-placement. */
7366 if (!cp_parser_parse_definitely (parser))
7368 if (placement != NULL)
7369 release_tree_vector (placement);
7370 placement = NULL;
7373 /* If the next token is a `(', then we have a parenthesized
7374 type-id. */
7375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7377 cp_token *token;
7378 const char *saved_message = parser->type_definition_forbidden_message;
7380 /* Consume the `('. */
7381 cp_lexer_consume_token (parser->lexer);
7383 /* Parse the type-id. */
7384 parser->type_definition_forbidden_message
7385 = G_("types may not be defined in a new-expression");
7386 type = cp_parser_type_id (parser);
7387 parser->type_definition_forbidden_message = saved_message;
7389 /* Look for the closing `)'. */
7390 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7391 token = cp_lexer_peek_token (parser->lexer);
7392 /* There should not be a direct-new-declarator in this production,
7393 but GCC used to allowed this, so we check and emit a sensible error
7394 message for this case. */
7395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7397 error_at (token->location,
7398 "array bound forbidden after parenthesized type-id");
7399 inform (token->location,
7400 "try removing the parentheses around the type-id");
7401 cp_parser_direct_new_declarator (parser);
7404 /* Otherwise, there must be a new-type-id. */
7405 else
7406 type = cp_parser_new_type_id (parser, &nelts);
7408 /* If the next token is a `(' or '{', then we have a new-initializer. */
7409 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7410 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7411 initializer = cp_parser_new_initializer (parser);
7412 else
7413 initializer = NULL;
7415 /* A new-expression may not appear in an integral constant
7416 expression. */
7417 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7418 ret = error_mark_node;
7419 else
7421 /* Create a representation of the new-expression. */
7422 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7423 tf_warning_or_error);
7426 if (placement != NULL)
7427 release_tree_vector (placement);
7428 if (initializer != NULL)
7429 release_tree_vector (initializer);
7431 return ret;
7434 /* Parse a new-placement.
7436 new-placement:
7437 ( expression-list )
7439 Returns the same representation as for an expression-list. */
7441 static vec<tree, va_gc> *
7442 cp_parser_new_placement (cp_parser* parser)
7444 vec<tree, va_gc> *expression_list;
7446 /* Parse the expression-list. */
7447 expression_list = (cp_parser_parenthesized_expression_list
7448 (parser, non_attr, /*cast_p=*/false,
7449 /*allow_expansion_p=*/true,
7450 /*non_constant_p=*/NULL));
7452 return expression_list;
7455 /* Parse a new-type-id.
7457 new-type-id:
7458 type-specifier-seq new-declarator [opt]
7460 Returns the TYPE allocated. If the new-type-id indicates an array
7461 type, *NELTS is set to the number of elements in the last array
7462 bound; the TYPE will not include the last array bound. */
7464 static tree
7465 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7467 cp_decl_specifier_seq type_specifier_seq;
7468 cp_declarator *new_declarator;
7469 cp_declarator *declarator;
7470 cp_declarator *outer_declarator;
7471 const char *saved_message;
7473 /* The type-specifier sequence must not contain type definitions.
7474 (It cannot contain declarations of new types either, but if they
7475 are not definitions we will catch that because they are not
7476 complete.) */
7477 saved_message = parser->type_definition_forbidden_message;
7478 parser->type_definition_forbidden_message
7479 = G_("types may not be defined in a new-type-id");
7480 /* Parse the type-specifier-seq. */
7481 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7482 /*is_trailing_return=*/false,
7483 &type_specifier_seq);
7484 /* Restore the old message. */
7485 parser->type_definition_forbidden_message = saved_message;
7487 if (type_specifier_seq.type == error_mark_node)
7488 return error_mark_node;
7490 /* Parse the new-declarator. */
7491 new_declarator = cp_parser_new_declarator_opt (parser);
7493 /* Determine the number of elements in the last array dimension, if
7494 any. */
7495 *nelts = NULL_TREE;
7496 /* Skip down to the last array dimension. */
7497 declarator = new_declarator;
7498 outer_declarator = NULL;
7499 while (declarator && (declarator->kind == cdk_pointer
7500 || declarator->kind == cdk_ptrmem))
7502 outer_declarator = declarator;
7503 declarator = declarator->declarator;
7505 while (declarator
7506 && declarator->kind == cdk_array
7507 && declarator->declarator
7508 && declarator->declarator->kind == cdk_array)
7510 outer_declarator = declarator;
7511 declarator = declarator->declarator;
7514 if (declarator && declarator->kind == cdk_array)
7516 *nelts = declarator->u.array.bounds;
7517 if (*nelts == error_mark_node)
7518 *nelts = integer_one_node;
7520 if (outer_declarator)
7521 outer_declarator->declarator = declarator->declarator;
7522 else
7523 new_declarator = NULL;
7526 return groktypename (&type_specifier_seq, new_declarator, false);
7529 /* Parse an (optional) new-declarator.
7531 new-declarator:
7532 ptr-operator new-declarator [opt]
7533 direct-new-declarator
7535 Returns the declarator. */
7537 static cp_declarator *
7538 cp_parser_new_declarator_opt (cp_parser* parser)
7540 enum tree_code code;
7541 tree type, std_attributes = NULL_TREE;
7542 cp_cv_quals cv_quals;
7544 /* We don't know if there's a ptr-operator next, or not. */
7545 cp_parser_parse_tentatively (parser);
7546 /* Look for a ptr-operator. */
7547 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7548 /* If that worked, look for more new-declarators. */
7549 if (cp_parser_parse_definitely (parser))
7551 cp_declarator *declarator;
7553 /* Parse another optional declarator. */
7554 declarator = cp_parser_new_declarator_opt (parser);
7556 declarator = cp_parser_make_indirect_declarator
7557 (code, type, cv_quals, declarator, std_attributes);
7559 return declarator;
7562 /* If the next token is a `[', there is a direct-new-declarator. */
7563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7564 return cp_parser_direct_new_declarator (parser);
7566 return NULL;
7569 /* Parse a direct-new-declarator.
7571 direct-new-declarator:
7572 [ expression ]
7573 direct-new-declarator [constant-expression]
7577 static cp_declarator *
7578 cp_parser_direct_new_declarator (cp_parser* parser)
7580 cp_declarator *declarator = NULL;
7582 while (true)
7584 tree expression;
7585 cp_token *token;
7587 /* Look for the opening `['. */
7588 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7590 token = cp_lexer_peek_token (parser->lexer);
7591 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7592 /* The standard requires that the expression have integral
7593 type. DR 74 adds enumeration types. We believe that the
7594 real intent is that these expressions be handled like the
7595 expression in a `switch' condition, which also allows
7596 classes with a single conversion to integral or
7597 enumeration type. */
7598 if (!processing_template_decl)
7600 expression
7601 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7602 expression,
7603 /*complain=*/true);
7604 if (!expression)
7606 error_at (token->location,
7607 "expression in new-declarator must have integral "
7608 "or enumeration type");
7609 expression = error_mark_node;
7613 /* Look for the closing `]'. */
7614 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7616 /* Add this bound to the declarator. */
7617 declarator = make_array_declarator (declarator, expression);
7619 /* If the next token is not a `[', then there are no more
7620 bounds. */
7621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7622 break;
7625 return declarator;
7628 /* Parse a new-initializer.
7630 new-initializer:
7631 ( expression-list [opt] )
7632 braced-init-list
7634 Returns a representation of the expression-list. */
7636 static vec<tree, va_gc> *
7637 cp_parser_new_initializer (cp_parser* parser)
7639 vec<tree, va_gc> *expression_list;
7641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7643 tree t;
7644 bool expr_non_constant_p;
7645 cp_lexer_set_source_position (parser->lexer);
7646 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7647 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7648 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7649 expression_list = make_tree_vector_single (t);
7651 else
7652 expression_list = (cp_parser_parenthesized_expression_list
7653 (parser, non_attr, /*cast_p=*/false,
7654 /*allow_expansion_p=*/true,
7655 /*non_constant_p=*/NULL));
7657 return expression_list;
7660 /* Parse a delete-expression.
7662 delete-expression:
7663 :: [opt] delete cast-expression
7664 :: [opt] delete [ ] cast-expression
7666 Returns a representation of the expression. */
7668 static tree
7669 cp_parser_delete_expression (cp_parser* parser)
7671 bool global_scope_p;
7672 bool array_p;
7673 tree expression;
7675 /* Look for the optional `::' operator. */
7676 global_scope_p
7677 = (cp_parser_global_scope_opt (parser,
7678 /*current_scope_valid_p=*/false)
7679 != NULL_TREE);
7680 /* Look for the `delete' keyword. */
7681 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7682 /* See if the array syntax is in use. */
7683 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7685 /* Consume the `[' token. */
7686 cp_lexer_consume_token (parser->lexer);
7687 /* Look for the `]' token. */
7688 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7689 /* Remember that this is the `[]' construct. */
7690 array_p = true;
7692 else
7693 array_p = false;
7695 /* Parse the cast-expression. */
7696 expression = cp_parser_simple_cast_expression (parser);
7698 /* A delete-expression may not appear in an integral constant
7699 expression. */
7700 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7701 return error_mark_node;
7703 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7704 tf_warning_or_error);
7707 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7708 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7709 0 otherwise. */
7711 static int
7712 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7714 cp_token *token = cp_lexer_peek_token (parser->lexer);
7715 switch (token->type)
7717 case CPP_COMMA:
7718 case CPP_SEMICOLON:
7719 case CPP_QUERY:
7720 case CPP_COLON:
7721 case CPP_CLOSE_SQUARE:
7722 case CPP_CLOSE_PAREN:
7723 case CPP_CLOSE_BRACE:
7724 case CPP_OPEN_BRACE:
7725 case CPP_DOT:
7726 case CPP_DOT_STAR:
7727 case CPP_DEREF:
7728 case CPP_DEREF_STAR:
7729 case CPP_DIV:
7730 case CPP_MOD:
7731 case CPP_LSHIFT:
7732 case CPP_RSHIFT:
7733 case CPP_LESS:
7734 case CPP_GREATER:
7735 case CPP_LESS_EQ:
7736 case CPP_GREATER_EQ:
7737 case CPP_EQ_EQ:
7738 case CPP_NOT_EQ:
7739 case CPP_EQ:
7740 case CPP_MULT_EQ:
7741 case CPP_DIV_EQ:
7742 case CPP_MOD_EQ:
7743 case CPP_PLUS_EQ:
7744 case CPP_MINUS_EQ:
7745 case CPP_RSHIFT_EQ:
7746 case CPP_LSHIFT_EQ:
7747 case CPP_AND_EQ:
7748 case CPP_XOR_EQ:
7749 case CPP_OR_EQ:
7750 case CPP_XOR:
7751 case CPP_OR:
7752 case CPP_OR_OR:
7753 case CPP_EOF:
7754 case CPP_ELLIPSIS:
7755 return 0;
7757 case CPP_OPEN_PAREN:
7758 /* In ((type ()) () the last () isn't a valid cast-expression,
7759 so the whole must be parsed as postfix-expression. */
7760 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7761 != CPP_CLOSE_PAREN;
7763 case CPP_OPEN_SQUARE:
7764 /* '[' may start a primary-expression in obj-c++ and in C++11,
7765 as a lambda-expression, eg, '(void)[]{}'. */
7766 if (cxx_dialect >= cxx11)
7767 return -1;
7768 return c_dialect_objc ();
7770 case CPP_PLUS_PLUS:
7771 case CPP_MINUS_MINUS:
7772 /* '++' and '--' may or may not start a cast-expression:
7774 struct T { void operator++(int); };
7775 void f() { (T())++; }
7779 int a;
7780 (int)++a; */
7781 return -1;
7783 default:
7784 return 1;
7788 /* Parse a cast-expression.
7790 cast-expression:
7791 unary-expression
7792 ( type-id ) cast-expression
7794 ADDRESS_P is true iff the unary-expression is appearing as the
7795 operand of the `&' operator. CAST_P is true if this expression is
7796 the target of a cast.
7798 Returns a representation of the expression. */
7800 static tree
7801 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7802 bool decltype_p, cp_id_kind * pidk)
7804 /* If it's a `(', then we might be looking at a cast. */
7805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7807 tree type = NULL_TREE;
7808 tree expr = NULL_TREE;
7809 int cast_expression = 0;
7810 const char *saved_message;
7812 /* There's no way to know yet whether or not this is a cast.
7813 For example, `(int (3))' is a unary-expression, while `(int)
7814 3' is a cast. So, we resort to parsing tentatively. */
7815 cp_parser_parse_tentatively (parser);
7816 /* Types may not be defined in a cast. */
7817 saved_message = parser->type_definition_forbidden_message;
7818 parser->type_definition_forbidden_message
7819 = G_("types may not be defined in casts");
7820 /* Consume the `('. */
7821 cp_lexer_consume_token (parser->lexer);
7822 /* A very tricky bit is that `(struct S) { 3 }' is a
7823 compound-literal (which we permit in C++ as an extension).
7824 But, that construct is not a cast-expression -- it is a
7825 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7826 is legal; if the compound-literal were a cast-expression,
7827 you'd need an extra set of parentheses.) But, if we parse
7828 the type-id, and it happens to be a class-specifier, then we
7829 will commit to the parse at that point, because we cannot
7830 undo the action that is done when creating a new class. So,
7831 then we cannot back up and do a postfix-expression.
7833 Another tricky case is the following (c++/29234):
7835 struct S { void operator () (); };
7837 void foo ()
7839 ( S()() );
7842 As a type-id we parse the parenthesized S()() as a function
7843 returning a function, groktypename complains and we cannot
7844 back up in this case either.
7846 Therefore, we scan ahead to the closing `)', and check to see
7847 if the tokens after the `)' can start a cast-expression. Otherwise
7848 we are dealing with an unary-expression, a postfix-expression
7849 or something else.
7851 Yet another tricky case, in C++11, is the following (c++/54891):
7853 (void)[]{};
7855 The issue is that usually, besides the case of lambda-expressions,
7856 the parenthesized type-id cannot be followed by '[', and, eg, we
7857 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7858 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7859 we don't commit, we try a cast-expression, then an unary-expression.
7861 Save tokens so that we can put them back. */
7862 cp_lexer_save_tokens (parser->lexer);
7864 /* We may be looking at a cast-expression. */
7865 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7866 /*consume_paren=*/true))
7867 cast_expression
7868 = cp_parser_tokens_start_cast_expression (parser);
7870 /* Roll back the tokens we skipped. */
7871 cp_lexer_rollback_tokens (parser->lexer);
7872 /* If we aren't looking at a cast-expression, simulate an error so
7873 that the call to cp_parser_error_occurred below returns true. */
7874 if (!cast_expression)
7875 cp_parser_simulate_error (parser);
7876 else
7878 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7879 parser->in_type_id_in_expr_p = true;
7880 /* Look for the type-id. */
7881 type = cp_parser_type_id (parser);
7882 /* Look for the closing `)'. */
7883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7884 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7887 /* Restore the saved message. */
7888 parser->type_definition_forbidden_message = saved_message;
7890 /* At this point this can only be either a cast or a
7891 parenthesized ctor such as `(T ())' that looks like a cast to
7892 function returning T. */
7893 if (!cp_parser_error_occurred (parser))
7895 /* Only commit if the cast-expression doesn't start with
7896 '++', '--', or '[' in C++11. */
7897 if (cast_expression > 0)
7898 cp_parser_commit_to_topmost_tentative_parse (parser);
7900 expr = cp_parser_cast_expression (parser,
7901 /*address_p=*/false,
7902 /*cast_p=*/true,
7903 /*decltype_p=*/false,
7904 pidk);
7906 if (cp_parser_parse_definitely (parser))
7908 /* Warn about old-style casts, if so requested. */
7909 if (warn_old_style_cast
7910 && !in_system_header_at (input_location)
7911 && !VOID_TYPE_P (type)
7912 && current_lang_name != lang_name_c)
7913 warning (OPT_Wold_style_cast, "use of old-style cast");
7915 /* Only type conversions to integral or enumeration types
7916 can be used in constant-expressions. */
7917 if (!cast_valid_in_integral_constant_expression_p (type)
7918 && cp_parser_non_integral_constant_expression (parser,
7919 NIC_CAST))
7920 return error_mark_node;
7922 /* Perform the cast. */
7923 expr = build_c_cast (input_location, type, expr);
7924 return expr;
7927 else
7928 cp_parser_abort_tentative_parse (parser);
7931 /* If we get here, then it's not a cast, so it must be a
7932 unary-expression. */
7933 return cp_parser_unary_expression (parser, address_p, cast_p,
7934 decltype_p, pidk);
7937 /* Parse a binary expression of the general form:
7939 pm-expression:
7940 cast-expression
7941 pm-expression .* cast-expression
7942 pm-expression ->* cast-expression
7944 multiplicative-expression:
7945 pm-expression
7946 multiplicative-expression * pm-expression
7947 multiplicative-expression / pm-expression
7948 multiplicative-expression % pm-expression
7950 additive-expression:
7951 multiplicative-expression
7952 additive-expression + multiplicative-expression
7953 additive-expression - multiplicative-expression
7955 shift-expression:
7956 additive-expression
7957 shift-expression << additive-expression
7958 shift-expression >> additive-expression
7960 relational-expression:
7961 shift-expression
7962 relational-expression < shift-expression
7963 relational-expression > shift-expression
7964 relational-expression <= shift-expression
7965 relational-expression >= shift-expression
7967 GNU Extension:
7969 relational-expression:
7970 relational-expression <? shift-expression
7971 relational-expression >? shift-expression
7973 equality-expression:
7974 relational-expression
7975 equality-expression == relational-expression
7976 equality-expression != relational-expression
7978 and-expression:
7979 equality-expression
7980 and-expression & equality-expression
7982 exclusive-or-expression:
7983 and-expression
7984 exclusive-or-expression ^ and-expression
7986 inclusive-or-expression:
7987 exclusive-or-expression
7988 inclusive-or-expression | exclusive-or-expression
7990 logical-and-expression:
7991 inclusive-or-expression
7992 logical-and-expression && inclusive-or-expression
7994 logical-or-expression:
7995 logical-and-expression
7996 logical-or-expression || logical-and-expression
7998 All these are implemented with a single function like:
8000 binary-expression:
8001 simple-cast-expression
8002 binary-expression <token> binary-expression
8004 CAST_P is true if this expression is the target of a cast.
8006 The binops_by_token map is used to get the tree codes for each <token> type.
8007 binary-expressions are associated according to a precedence table. */
8009 #define TOKEN_PRECEDENCE(token) \
8010 (((token->type == CPP_GREATER \
8011 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8012 && !parser->greater_than_is_operator_p) \
8013 ? PREC_NOT_OPERATOR \
8014 : binops_by_token[token->type].prec)
8016 static tree
8017 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8018 bool no_toplevel_fold_p,
8019 bool decltype_p,
8020 enum cp_parser_prec prec,
8021 cp_id_kind * pidk)
8023 cp_parser_expression_stack stack;
8024 cp_parser_expression_stack_entry *sp = &stack[0];
8025 cp_parser_expression_stack_entry current;
8026 tree rhs;
8027 cp_token *token;
8028 enum tree_code rhs_type;
8029 enum cp_parser_prec new_prec, lookahead_prec;
8030 tree overload;
8031 bool parenthesized_not_lhs_warn
8032 = cp_lexer_next_token_is (parser->lexer, CPP_NOT);
8034 /* Parse the first expression. */
8035 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8036 cast_p, decltype_p, pidk);
8037 current.lhs_type = ERROR_MARK;
8038 current.prec = prec;
8040 if (cp_parser_error_occurred (parser))
8041 return error_mark_node;
8043 for (;;)
8045 /* Get an operator token. */
8046 token = cp_lexer_peek_token (parser->lexer);
8048 if (warn_cxx0x_compat
8049 && token->type == CPP_RSHIFT
8050 && !parser->greater_than_is_operator_p)
8052 if (warning_at (token->location, OPT_Wc__0x_compat,
8053 "%<>>%> operator is treated"
8054 " as two right angle brackets in C++11"))
8055 inform (token->location,
8056 "suggest parentheses around %<>>%> expression");
8059 new_prec = TOKEN_PRECEDENCE (token);
8061 /* Popping an entry off the stack means we completed a subexpression:
8062 - either we found a token which is not an operator (`>' where it is not
8063 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8064 will happen repeatedly;
8065 - or, we found an operator which has lower priority. This is the case
8066 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8067 parsing `3 * 4'. */
8068 if (new_prec <= current.prec)
8070 if (sp == stack)
8071 break;
8072 else
8073 goto pop;
8076 get_rhs:
8077 current.tree_type = binops_by_token[token->type].tree_type;
8078 current.loc = token->location;
8080 /* We used the operator token. */
8081 cp_lexer_consume_token (parser->lexer);
8083 /* For "false && x" or "true || x", x will never be executed;
8084 disable warnings while evaluating it. */
8085 if (current.tree_type == TRUTH_ANDIF_EXPR)
8086 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8087 else if (current.tree_type == TRUTH_ORIF_EXPR)
8088 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8090 /* Extract another operand. It may be the RHS of this expression
8091 or the LHS of a new, higher priority expression. */
8092 rhs = cp_parser_simple_cast_expression (parser);
8093 rhs_type = ERROR_MARK;
8095 /* Get another operator token. Look up its precedence to avoid
8096 building a useless (immediately popped) stack entry for common
8097 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8098 token = cp_lexer_peek_token (parser->lexer);
8099 lookahead_prec = TOKEN_PRECEDENCE (token);
8100 if (lookahead_prec > new_prec)
8102 /* ... and prepare to parse the RHS of the new, higher priority
8103 expression. Since precedence levels on the stack are
8104 monotonically increasing, we do not have to care about
8105 stack overflows. */
8106 *sp = current;
8107 ++sp;
8108 current.lhs = rhs;
8109 current.lhs_type = rhs_type;
8110 current.prec = new_prec;
8111 new_prec = lookahead_prec;
8112 goto get_rhs;
8114 pop:
8115 lookahead_prec = new_prec;
8116 /* If the stack is not empty, we have parsed into LHS the right side
8117 (`4' in the example above) of an expression we had suspended.
8118 We can use the information on the stack to recover the LHS (`3')
8119 from the stack together with the tree code (`MULT_EXPR'), and
8120 the precedence of the higher level subexpression
8121 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8122 which will be used to actually build the additive expression. */
8123 rhs = current.lhs;
8124 rhs_type = current.lhs_type;
8125 --sp;
8126 current = *sp;
8129 /* Undo the disabling of warnings done above. */
8130 if (current.tree_type == TRUTH_ANDIF_EXPR)
8131 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8132 else if (current.tree_type == TRUTH_ORIF_EXPR)
8133 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8135 if (warn_logical_not_paren
8136 && parenthesized_not_lhs_warn)
8137 warn_logical_not_parentheses (current.loc, current.tree_type,
8138 TREE_OPERAND (current.lhs, 0), rhs);
8140 overload = NULL;
8141 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8142 ERROR_MARK for everything that is not a binary expression.
8143 This makes warn_about_parentheses miss some warnings that
8144 involve unary operators. For unary expressions we should
8145 pass the correct tree_code unless the unary expression was
8146 surrounded by parentheses.
8148 if (no_toplevel_fold_p
8149 && lookahead_prec <= current.prec
8150 && sp == stack)
8151 current.lhs = build2 (current.tree_type,
8152 TREE_CODE_CLASS (current.tree_type)
8153 == tcc_comparison
8154 ? boolean_type_node : TREE_TYPE (current.lhs),
8155 current.lhs, rhs);
8156 else
8157 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8158 current.lhs, current.lhs_type,
8159 rhs, rhs_type, &overload,
8160 complain_flags (decltype_p));
8161 current.lhs_type = current.tree_type;
8162 if (EXPR_P (current.lhs))
8163 SET_EXPR_LOCATION (current.lhs, current.loc);
8165 /* If the binary operator required the use of an overloaded operator,
8166 then this expression cannot be an integral constant-expression.
8167 An overloaded operator can be used even if both operands are
8168 otherwise permissible in an integral constant-expression if at
8169 least one of the operands is of enumeration type. */
8171 if (overload
8172 && cp_parser_non_integral_constant_expression (parser,
8173 NIC_OVERLOADED))
8174 return error_mark_node;
8177 return current.lhs;
8180 static tree
8181 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8182 bool no_toplevel_fold_p,
8183 enum cp_parser_prec prec,
8184 cp_id_kind * pidk)
8186 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8187 /*decltype*/false, prec, pidk);
8190 /* Parse the `? expression : assignment-expression' part of a
8191 conditional-expression. The LOGICAL_OR_EXPR is the
8192 logical-or-expression that started the conditional-expression.
8193 Returns a representation of the entire conditional-expression.
8195 This routine is used by cp_parser_assignment_expression.
8197 ? expression : assignment-expression
8199 GNU Extensions:
8201 ? : assignment-expression */
8203 static tree
8204 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8206 tree expr;
8207 tree assignment_expr;
8208 struct cp_token *token;
8209 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8211 /* Consume the `?' token. */
8212 cp_lexer_consume_token (parser->lexer);
8213 token = cp_lexer_peek_token (parser->lexer);
8214 if (cp_parser_allow_gnu_extensions_p (parser)
8215 && token->type == CPP_COLON)
8217 pedwarn (token->location, OPT_Wpedantic,
8218 "ISO C++ does not allow ?: with omitted middle operand");
8219 /* Implicit true clause. */
8220 expr = NULL_TREE;
8221 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8222 warn_for_omitted_condop (token->location, logical_or_expr);
8224 else
8226 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8227 parser->colon_corrects_to_scope_p = false;
8228 /* Parse the expression. */
8229 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8230 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8231 c_inhibit_evaluation_warnings +=
8232 ((logical_or_expr == truthvalue_true_node)
8233 - (logical_or_expr == truthvalue_false_node));
8234 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8237 /* The next token should be a `:'. */
8238 cp_parser_require (parser, CPP_COLON, RT_COLON);
8239 /* Parse the assignment-expression. */
8240 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8241 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8243 /* Build the conditional-expression. */
8244 return build_x_conditional_expr (loc, logical_or_expr,
8245 expr,
8246 assignment_expr,
8247 tf_warning_or_error);
8250 /* Parse an assignment-expression.
8252 assignment-expression:
8253 conditional-expression
8254 logical-or-expression assignment-operator assignment_expression
8255 throw-expression
8257 CAST_P is true if this expression is the target of a cast.
8258 DECLTYPE_P is true if this expression is the operand of decltype.
8260 Returns a representation for the expression. */
8262 static tree
8263 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8264 bool decltype_p, cp_id_kind * pidk)
8266 tree expr;
8268 /* If the next token is the `throw' keyword, then we're looking at
8269 a throw-expression. */
8270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8271 expr = cp_parser_throw_expression (parser);
8272 /* Otherwise, it must be that we are looking at a
8273 logical-or-expression. */
8274 else
8276 /* Parse the binary expressions (logical-or-expression). */
8277 expr = cp_parser_binary_expression (parser, cast_p, false,
8278 decltype_p,
8279 PREC_NOT_OPERATOR, pidk);
8280 /* If the next token is a `?' then we're actually looking at a
8281 conditional-expression. */
8282 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8283 return cp_parser_question_colon_clause (parser, expr);
8284 else
8286 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8288 /* If it's an assignment-operator, we're using the second
8289 production. */
8290 enum tree_code assignment_operator
8291 = cp_parser_assignment_operator_opt (parser);
8292 if (assignment_operator != ERROR_MARK)
8294 bool non_constant_p;
8295 location_t saved_input_location;
8297 /* Parse the right-hand side of the assignment. */
8298 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8300 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8301 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8303 /* An assignment may not appear in a
8304 constant-expression. */
8305 if (cp_parser_non_integral_constant_expression (parser,
8306 NIC_ASSIGNMENT))
8307 return error_mark_node;
8308 /* Build the assignment expression. Its default
8309 location is the location of the '=' token. */
8310 saved_input_location = input_location;
8311 input_location = loc;
8312 expr = build_x_modify_expr (loc, expr,
8313 assignment_operator,
8314 rhs,
8315 complain_flags (decltype_p));
8316 input_location = saved_input_location;
8321 return expr;
8324 static tree
8325 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8326 cp_id_kind * pidk)
8328 return cp_parser_assignment_expression (parser, cast_p,
8329 /*decltype*/false, pidk);
8332 /* Parse an (optional) assignment-operator.
8334 assignment-operator: one of
8335 = *= /= %= += -= >>= <<= &= ^= |=
8337 GNU Extension:
8339 assignment-operator: one of
8340 <?= >?=
8342 If the next token is an assignment operator, the corresponding tree
8343 code is returned, and the token is consumed. For example, for
8344 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8345 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8346 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8347 operator, ERROR_MARK is returned. */
8349 static enum tree_code
8350 cp_parser_assignment_operator_opt (cp_parser* parser)
8352 enum tree_code op;
8353 cp_token *token;
8355 /* Peek at the next token. */
8356 token = cp_lexer_peek_token (parser->lexer);
8358 switch (token->type)
8360 case CPP_EQ:
8361 op = NOP_EXPR;
8362 break;
8364 case CPP_MULT_EQ:
8365 op = MULT_EXPR;
8366 break;
8368 case CPP_DIV_EQ:
8369 op = TRUNC_DIV_EXPR;
8370 break;
8372 case CPP_MOD_EQ:
8373 op = TRUNC_MOD_EXPR;
8374 break;
8376 case CPP_PLUS_EQ:
8377 op = PLUS_EXPR;
8378 break;
8380 case CPP_MINUS_EQ:
8381 op = MINUS_EXPR;
8382 break;
8384 case CPP_RSHIFT_EQ:
8385 op = RSHIFT_EXPR;
8386 break;
8388 case CPP_LSHIFT_EQ:
8389 op = LSHIFT_EXPR;
8390 break;
8392 case CPP_AND_EQ:
8393 op = BIT_AND_EXPR;
8394 break;
8396 case CPP_XOR_EQ:
8397 op = BIT_XOR_EXPR;
8398 break;
8400 case CPP_OR_EQ:
8401 op = BIT_IOR_EXPR;
8402 break;
8404 default:
8405 /* Nothing else is an assignment operator. */
8406 op = ERROR_MARK;
8409 /* If it was an assignment operator, consume it. */
8410 if (op != ERROR_MARK)
8411 cp_lexer_consume_token (parser->lexer);
8413 return op;
8416 /* Parse an expression.
8418 expression:
8419 assignment-expression
8420 expression , assignment-expression
8422 CAST_P is true if this expression is the target of a cast.
8423 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8424 except possibly parenthesized or on the RHS of a comma (N3276).
8426 Returns a representation of the expression. */
8428 static tree
8429 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8430 cp_id_kind * pidk)
8432 tree expression = NULL_TREE;
8433 location_t loc = UNKNOWN_LOCATION;
8435 while (true)
8437 tree assignment_expression;
8439 /* Parse the next assignment-expression. */
8440 assignment_expression
8441 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8443 /* We don't create a temporary for a call that is the immediate operand
8444 of decltype or on the RHS of a comma. But when we see a comma, we
8445 need to create a temporary for a call on the LHS. */
8446 if (decltype_p && !processing_template_decl
8447 && TREE_CODE (assignment_expression) == CALL_EXPR
8448 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8449 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8450 assignment_expression
8451 = build_cplus_new (TREE_TYPE (assignment_expression),
8452 assignment_expression, tf_warning_or_error);
8454 /* If this is the first assignment-expression, we can just
8455 save it away. */
8456 if (!expression)
8457 expression = assignment_expression;
8458 else
8459 expression = build_x_compound_expr (loc, expression,
8460 assignment_expression,
8461 complain_flags (decltype_p));
8462 /* If the next token is not a comma, then we are done with the
8463 expression. */
8464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8465 break;
8466 /* Consume the `,'. */
8467 loc = cp_lexer_peek_token (parser->lexer)->location;
8468 cp_lexer_consume_token (parser->lexer);
8469 /* A comma operator cannot appear in a constant-expression. */
8470 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8471 expression = error_mark_node;
8474 return expression;
8477 static inline tree
8478 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8480 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8483 /* Parse a constant-expression.
8485 constant-expression:
8486 conditional-expression
8488 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8489 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8490 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8491 is false, NON_CONSTANT_P should be NULL. */
8493 static tree
8494 cp_parser_constant_expression (cp_parser* parser,
8495 bool allow_non_constant_p,
8496 bool *non_constant_p)
8498 bool saved_integral_constant_expression_p;
8499 bool saved_allow_non_integral_constant_expression_p;
8500 bool saved_non_integral_constant_expression_p;
8501 tree expression;
8503 /* It might seem that we could simply parse the
8504 conditional-expression, and then check to see if it were
8505 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8506 one that the compiler can figure out is constant, possibly after
8507 doing some simplifications or optimizations. The standard has a
8508 precise definition of constant-expression, and we must honor
8509 that, even though it is somewhat more restrictive.
8511 For example:
8513 int i[(2, 3)];
8515 is not a legal declaration, because `(2, 3)' is not a
8516 constant-expression. The `,' operator is forbidden in a
8517 constant-expression. However, GCC's constant-folding machinery
8518 will fold this operation to an INTEGER_CST for `3'. */
8520 /* Save the old settings. */
8521 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8522 saved_allow_non_integral_constant_expression_p
8523 = parser->allow_non_integral_constant_expression_p;
8524 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8525 /* We are now parsing a constant-expression. */
8526 parser->integral_constant_expression_p = true;
8527 parser->allow_non_integral_constant_expression_p
8528 = (allow_non_constant_p || cxx_dialect >= cxx11);
8529 parser->non_integral_constant_expression_p = false;
8530 /* Although the grammar says "conditional-expression", we parse an
8531 "assignment-expression", which also permits "throw-expression"
8532 and the use of assignment operators. In the case that
8533 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8534 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8535 actually essential that we look for an assignment-expression.
8536 For example, cp_parser_initializer_clauses uses this function to
8537 determine whether a particular assignment-expression is in fact
8538 constant. */
8539 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8540 /* Restore the old settings. */
8541 parser->integral_constant_expression_p
8542 = saved_integral_constant_expression_p;
8543 parser->allow_non_integral_constant_expression_p
8544 = saved_allow_non_integral_constant_expression_p;
8545 if (cxx_dialect >= cxx11)
8547 /* Require an rvalue constant expression here; that's what our
8548 callers expect. Reference constant expressions are handled
8549 separately in e.g. cp_parser_template_argument. */
8550 bool is_const = potential_rvalue_constant_expression (expression);
8551 parser->non_integral_constant_expression_p = !is_const;
8552 if (!is_const && !allow_non_constant_p)
8553 require_potential_rvalue_constant_expression (expression);
8555 if (allow_non_constant_p)
8556 *non_constant_p = parser->non_integral_constant_expression_p;
8557 parser->non_integral_constant_expression_p
8558 = saved_non_integral_constant_expression_p;
8560 return expression;
8563 /* Parse __builtin_offsetof.
8565 offsetof-expression:
8566 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8568 offsetof-member-designator:
8569 id-expression
8570 | offsetof-member-designator "." id-expression
8571 | offsetof-member-designator "[" expression "]"
8572 | offsetof-member-designator "->" id-expression */
8574 static tree
8575 cp_parser_builtin_offsetof (cp_parser *parser)
8577 int save_ice_p, save_non_ice_p;
8578 tree type, expr;
8579 cp_id_kind dummy;
8580 cp_token *token;
8582 /* We're about to accept non-integral-constant things, but will
8583 definitely yield an integral constant expression. Save and
8584 restore these values around our local parsing. */
8585 save_ice_p = parser->integral_constant_expression_p;
8586 save_non_ice_p = parser->non_integral_constant_expression_p;
8588 /* Consume the "__builtin_offsetof" token. */
8589 cp_lexer_consume_token (parser->lexer);
8590 /* Consume the opening `('. */
8591 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8592 /* Parse the type-id. */
8593 type = cp_parser_type_id (parser);
8594 /* Look for the `,'. */
8595 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8596 token = cp_lexer_peek_token (parser->lexer);
8598 /* Build the (type *)null that begins the traditional offsetof macro. */
8599 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8600 tf_warning_or_error);
8602 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8603 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8604 true, &dummy, token->location);
8605 while (true)
8607 token = cp_lexer_peek_token (parser->lexer);
8608 switch (token->type)
8610 case CPP_OPEN_SQUARE:
8611 /* offsetof-member-designator "[" expression "]" */
8612 expr = cp_parser_postfix_open_square_expression (parser, expr,
8613 true, false);
8614 break;
8616 case CPP_DEREF:
8617 /* offsetof-member-designator "->" identifier */
8618 expr = grok_array_decl (token->location, expr,
8619 integer_zero_node, false);
8620 /* FALLTHRU */
8622 case CPP_DOT:
8623 /* offsetof-member-designator "." identifier */
8624 cp_lexer_consume_token (parser->lexer);
8625 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8626 expr, true, &dummy,
8627 token->location);
8628 break;
8630 case CPP_CLOSE_PAREN:
8631 /* Consume the ")" token. */
8632 cp_lexer_consume_token (parser->lexer);
8633 goto success;
8635 default:
8636 /* Error. We know the following require will fail, but
8637 that gives the proper error message. */
8638 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8639 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8640 expr = error_mark_node;
8641 goto failure;
8645 success:
8646 /* If we're processing a template, we can't finish the semantics yet.
8647 Otherwise we can fold the entire expression now. */
8648 if (processing_template_decl)
8649 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8650 else
8651 expr = finish_offsetof (expr);
8653 failure:
8654 parser->integral_constant_expression_p = save_ice_p;
8655 parser->non_integral_constant_expression_p = save_non_ice_p;
8657 return expr;
8660 /* Parse a trait expression.
8662 Returns a representation of the expression, the underlying type
8663 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8665 static tree
8666 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8668 cp_trait_kind kind;
8669 tree type1, type2 = NULL_TREE;
8670 bool binary = false;
8671 cp_decl_specifier_seq decl_specs;
8673 switch (keyword)
8675 case RID_HAS_NOTHROW_ASSIGN:
8676 kind = CPTK_HAS_NOTHROW_ASSIGN;
8677 break;
8678 case RID_HAS_NOTHROW_CONSTRUCTOR:
8679 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8680 break;
8681 case RID_HAS_NOTHROW_COPY:
8682 kind = CPTK_HAS_NOTHROW_COPY;
8683 break;
8684 case RID_HAS_TRIVIAL_ASSIGN:
8685 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8686 break;
8687 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8688 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8689 break;
8690 case RID_HAS_TRIVIAL_COPY:
8691 kind = CPTK_HAS_TRIVIAL_COPY;
8692 break;
8693 case RID_HAS_TRIVIAL_DESTRUCTOR:
8694 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8695 break;
8696 case RID_HAS_VIRTUAL_DESTRUCTOR:
8697 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8698 break;
8699 case RID_IS_ABSTRACT:
8700 kind = CPTK_IS_ABSTRACT;
8701 break;
8702 case RID_IS_BASE_OF:
8703 kind = CPTK_IS_BASE_OF;
8704 binary = true;
8705 break;
8706 case RID_IS_CLASS:
8707 kind = CPTK_IS_CLASS;
8708 break;
8709 case RID_IS_CONVERTIBLE_TO:
8710 kind = CPTK_IS_CONVERTIBLE_TO;
8711 binary = true;
8712 break;
8713 case RID_IS_EMPTY:
8714 kind = CPTK_IS_EMPTY;
8715 break;
8716 case RID_IS_ENUM:
8717 kind = CPTK_IS_ENUM;
8718 break;
8719 case RID_IS_FINAL:
8720 kind = CPTK_IS_FINAL;
8721 break;
8722 case RID_IS_LITERAL_TYPE:
8723 kind = CPTK_IS_LITERAL_TYPE;
8724 break;
8725 case RID_IS_POD:
8726 kind = CPTK_IS_POD;
8727 break;
8728 case RID_IS_POLYMORPHIC:
8729 kind = CPTK_IS_POLYMORPHIC;
8730 break;
8731 case RID_IS_STD_LAYOUT:
8732 kind = CPTK_IS_STD_LAYOUT;
8733 break;
8734 case RID_IS_TRIVIAL:
8735 kind = CPTK_IS_TRIVIAL;
8736 break;
8737 case RID_IS_UNION:
8738 kind = CPTK_IS_UNION;
8739 break;
8740 case RID_UNDERLYING_TYPE:
8741 kind = CPTK_UNDERLYING_TYPE;
8742 break;
8743 case RID_BASES:
8744 kind = CPTK_BASES;
8745 break;
8746 case RID_DIRECT_BASES:
8747 kind = CPTK_DIRECT_BASES;
8748 break;
8749 default:
8750 gcc_unreachable ();
8753 /* Consume the token. */
8754 cp_lexer_consume_token (parser->lexer);
8756 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8758 type1 = cp_parser_type_id (parser);
8760 if (type1 == error_mark_node)
8761 return error_mark_node;
8763 /* Build a trivial decl-specifier-seq. */
8764 clear_decl_specs (&decl_specs);
8765 decl_specs.type = type1;
8767 /* Call grokdeclarator to figure out what type this is. */
8768 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8769 /*initialized=*/0, /*attrlist=*/NULL);
8771 if (binary)
8773 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8775 type2 = cp_parser_type_id (parser);
8777 if (type2 == error_mark_node)
8778 return error_mark_node;
8780 /* Build a trivial decl-specifier-seq. */
8781 clear_decl_specs (&decl_specs);
8782 decl_specs.type = type2;
8784 /* Call grokdeclarator to figure out what type this is. */
8785 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8786 /*initialized=*/0, /*attrlist=*/NULL);
8789 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8791 /* Complete the trait expression, which may mean either processing
8792 the trait expr now or saving it for template instantiation. */
8793 switch(kind)
8795 case CPTK_UNDERLYING_TYPE:
8796 return finish_underlying_type (type1);
8797 case CPTK_BASES:
8798 return finish_bases (type1, false);
8799 case CPTK_DIRECT_BASES:
8800 return finish_bases (type1, true);
8801 default:
8802 return finish_trait_expr (kind, type1, type2);
8806 /* Lambdas that appear in variable initializer or default argument scope
8807 get that in their mangling, so we need to record it. We might as well
8808 use the count for function and namespace scopes as well. */
8809 static GTY(()) tree lambda_scope;
8810 static GTY(()) int lambda_count;
8811 typedef struct GTY(()) tree_int
8813 tree t;
8814 int i;
8815 } tree_int;
8816 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8818 static void
8819 start_lambda_scope (tree decl)
8821 tree_int ti;
8822 gcc_assert (decl);
8823 /* Once we're inside a function, we ignore other scopes and just push
8824 the function again so that popping works properly. */
8825 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8826 decl = current_function_decl;
8827 ti.t = lambda_scope;
8828 ti.i = lambda_count;
8829 vec_safe_push (lambda_scope_stack, ti);
8830 if (lambda_scope != decl)
8832 /* Don't reset the count if we're still in the same function. */
8833 lambda_scope = decl;
8834 lambda_count = 0;
8838 static void
8839 record_lambda_scope (tree lambda)
8841 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8842 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8845 static void
8846 finish_lambda_scope (void)
8848 tree_int *p = &lambda_scope_stack->last ();
8849 if (lambda_scope != p->t)
8851 lambda_scope = p->t;
8852 lambda_count = p->i;
8854 lambda_scope_stack->pop ();
8857 /* Parse a lambda expression.
8859 lambda-expression:
8860 lambda-introducer lambda-declarator [opt] compound-statement
8862 Returns a representation of the expression. */
8864 static tree
8865 cp_parser_lambda_expression (cp_parser* parser)
8867 tree lambda_expr = build_lambda_expr ();
8868 tree type;
8869 bool ok = true;
8870 cp_token *token = cp_lexer_peek_token (parser->lexer);
8872 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8874 if (cp_unevaluated_operand)
8876 if (!token->error_reported)
8878 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8879 "lambda-expression in unevaluated context");
8880 token->error_reported = true;
8882 ok = false;
8885 /* We may be in the middle of deferred access check. Disable
8886 it now. */
8887 push_deferring_access_checks (dk_no_deferred);
8889 cp_parser_lambda_introducer (parser, lambda_expr);
8891 type = begin_lambda_type (lambda_expr);
8892 if (type == error_mark_node)
8893 return error_mark_node;
8895 record_lambda_scope (lambda_expr);
8897 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8898 determine_visibility (TYPE_NAME (type));
8900 /* Now that we've started the type, add the capture fields for any
8901 explicit captures. */
8902 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8905 /* Inside the class, surrounding template-parameter-lists do not apply. */
8906 unsigned int saved_num_template_parameter_lists
8907 = parser->num_template_parameter_lists;
8908 unsigned char in_statement = parser->in_statement;
8909 bool in_switch_statement_p = parser->in_switch_statement_p;
8910 bool fully_implicit_function_template_p
8911 = parser->fully_implicit_function_template_p;
8912 tree implicit_template_parms = parser->implicit_template_parms;
8913 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8914 bool auto_is_implicit_function_template_parm_p
8915 = parser->auto_is_implicit_function_template_parm_p;
8917 parser->num_template_parameter_lists = 0;
8918 parser->in_statement = 0;
8919 parser->in_switch_statement_p = false;
8920 parser->fully_implicit_function_template_p = false;
8921 parser->implicit_template_parms = 0;
8922 parser->implicit_template_scope = 0;
8923 parser->auto_is_implicit_function_template_parm_p = false;
8925 /* By virtue of defining a local class, a lambda expression has access to
8926 the private variables of enclosing classes. */
8928 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8930 if (ok)
8931 cp_parser_lambda_body (parser, lambda_expr);
8932 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8934 if (cp_parser_skip_to_closing_brace (parser))
8935 cp_lexer_consume_token (parser->lexer);
8938 /* The capture list was built up in reverse order; fix that now. */
8939 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8940 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8942 if (ok)
8943 maybe_add_lambda_conv_op (type);
8945 type = finish_struct (type, /*attributes=*/NULL_TREE);
8947 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8948 parser->in_statement = in_statement;
8949 parser->in_switch_statement_p = in_switch_statement_p;
8950 parser->fully_implicit_function_template_p
8951 = fully_implicit_function_template_p;
8952 parser->implicit_template_parms = implicit_template_parms;
8953 parser->implicit_template_scope = implicit_template_scope;
8954 parser->auto_is_implicit_function_template_parm_p
8955 = auto_is_implicit_function_template_parm_p;
8958 pop_deferring_access_checks ();
8960 /* This field is only used during parsing of the lambda. */
8961 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8963 /* This lambda shouldn't have any proxies left at this point. */
8964 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8965 /* And now that we're done, push proxies for an enclosing lambda. */
8966 insert_pending_capture_proxies ();
8968 if (ok)
8969 return build_lambda_object (lambda_expr);
8970 else
8971 return error_mark_node;
8974 /* Parse the beginning of a lambda expression.
8976 lambda-introducer:
8977 [ lambda-capture [opt] ]
8979 LAMBDA_EXPR is the current representation of the lambda expression. */
8981 static void
8982 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8984 /* Need commas after the first capture. */
8985 bool first = true;
8987 /* Eat the leading `['. */
8988 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8990 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8991 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8992 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8993 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8994 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8995 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8997 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8999 cp_lexer_consume_token (parser->lexer);
9000 first = false;
9003 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9005 cp_token* capture_token;
9006 tree capture_id;
9007 tree capture_init_expr;
9008 cp_id_kind idk = CP_ID_KIND_NONE;
9009 bool explicit_init_p = false;
9011 enum capture_kind_type
9013 BY_COPY,
9014 BY_REFERENCE
9016 enum capture_kind_type capture_kind = BY_COPY;
9018 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9020 error ("expected end of capture-list");
9021 return;
9024 if (first)
9025 first = false;
9026 else
9027 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9029 /* Possibly capture `this'. */
9030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9032 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9033 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9034 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9035 "with by-copy capture default");
9036 cp_lexer_consume_token (parser->lexer);
9037 add_capture (lambda_expr,
9038 /*id=*/this_identifier,
9039 /*initializer=*/finish_this_expr(),
9040 /*by_reference_p=*/false,
9041 explicit_init_p);
9042 continue;
9045 /* Remember whether we want to capture as a reference or not. */
9046 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9048 capture_kind = BY_REFERENCE;
9049 cp_lexer_consume_token (parser->lexer);
9052 /* Get the identifier. */
9053 capture_token = cp_lexer_peek_token (parser->lexer);
9054 capture_id = cp_parser_identifier (parser);
9056 if (capture_id == error_mark_node)
9057 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9058 delimiters, but I modified this to stop on unnested ']' as well. It
9059 was already changed to stop on unnested '}', so the
9060 "closing_parenthesis" name is no more misleading with my change. */
9062 cp_parser_skip_to_closing_parenthesis (parser,
9063 /*recovering=*/true,
9064 /*or_comma=*/true,
9065 /*consume_paren=*/true);
9066 break;
9069 /* Find the initializer for this capture. */
9070 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9071 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9072 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9074 bool direct, non_constant;
9075 /* An explicit initializer exists. */
9076 if (cxx_dialect < cxx1y)
9077 pedwarn (input_location, 0,
9078 "lambda capture initializers "
9079 "only available with -std=c++1y or -std=gnu++1y");
9080 capture_init_expr = cp_parser_initializer (parser, &direct,
9081 &non_constant);
9082 explicit_init_p = true;
9083 if (capture_init_expr == NULL_TREE)
9085 error ("empty initializer for lambda init-capture");
9086 capture_init_expr = error_mark_node;
9089 else
9091 const char* error_msg;
9093 /* Turn the identifier into an id-expression. */
9094 capture_init_expr
9095 = cp_parser_lookup_name_simple (parser, capture_id,
9096 capture_token->location);
9098 if (capture_init_expr == error_mark_node)
9100 unqualified_name_lookup_error (capture_id);
9101 continue;
9103 else if (DECL_P (capture_init_expr)
9104 && (!VAR_P (capture_init_expr)
9105 && TREE_CODE (capture_init_expr) != PARM_DECL))
9107 error_at (capture_token->location,
9108 "capture of non-variable %qD ",
9109 capture_init_expr);
9110 inform (0, "%q+#D declared here", capture_init_expr);
9111 continue;
9113 if (VAR_P (capture_init_expr)
9114 && decl_storage_duration (capture_init_expr) != dk_auto)
9116 if (pedwarn (capture_token->location, 0, "capture of variable "
9117 "%qD with non-automatic storage duration",
9118 capture_init_expr))
9119 inform (0, "%q+#D declared here", capture_init_expr);
9120 continue;
9123 capture_init_expr
9124 = finish_id_expression
9125 (capture_id,
9126 capture_init_expr,
9127 parser->scope,
9128 &idk,
9129 /*integral_constant_expression_p=*/false,
9130 /*allow_non_integral_constant_expression_p=*/false,
9131 /*non_integral_constant_expression_p=*/NULL,
9132 /*template_p=*/false,
9133 /*done=*/true,
9134 /*address_p=*/false,
9135 /*template_arg_p=*/false,
9136 &error_msg,
9137 capture_token->location);
9139 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9141 cp_lexer_consume_token (parser->lexer);
9142 capture_init_expr = make_pack_expansion (capture_init_expr);
9144 else
9145 check_for_bare_parameter_packs (capture_init_expr);
9148 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9149 && !explicit_init_p)
9151 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9152 && capture_kind == BY_COPY)
9153 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9154 "of %qD redundant with by-copy capture default",
9155 capture_id);
9156 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9157 && capture_kind == BY_REFERENCE)
9158 pedwarn (capture_token->location, 0, "explicit by-reference "
9159 "capture of %qD redundant with by-reference capture "
9160 "default", capture_id);
9163 add_capture (lambda_expr,
9164 capture_id,
9165 capture_init_expr,
9166 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9167 explicit_init_p);
9170 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9173 /* Parse the (optional) middle of a lambda expression.
9175 lambda-declarator:
9176 < template-parameter-list [opt] >
9177 ( parameter-declaration-clause [opt] )
9178 attribute-specifier [opt]
9179 mutable [opt]
9180 exception-specification [opt]
9181 lambda-return-type-clause [opt]
9183 LAMBDA_EXPR is the current representation of the lambda expression. */
9185 static bool
9186 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9188 /* 5.1.1.4 of the standard says:
9189 If a lambda-expression does not include a lambda-declarator, it is as if
9190 the lambda-declarator were ().
9191 This means an empty parameter list, no attributes, and no exception
9192 specification. */
9193 tree param_list = void_list_node;
9194 tree attributes = NULL_TREE;
9195 tree exception_spec = NULL_TREE;
9196 tree template_param_list = NULL_TREE;
9198 /* The template-parameter-list is optional, but must begin with
9199 an opening angle if present. */
9200 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9202 if (cxx_dialect < cxx1y)
9203 pedwarn (parser->lexer->next_token->location, 0,
9204 "lambda templates are only available with "
9205 "-std=c++1y or -std=gnu++1y");
9207 cp_lexer_consume_token (parser->lexer);
9209 template_param_list = cp_parser_template_parameter_list (parser);
9211 cp_parser_skip_to_end_of_template_parameter_list (parser);
9213 /* We just processed one more parameter list. */
9214 ++parser->num_template_parameter_lists;
9217 /* The parameter-declaration-clause is optional (unless
9218 template-parameter-list was given), but must begin with an
9219 opening parenthesis if present. */
9220 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9222 cp_lexer_consume_token (parser->lexer);
9224 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9226 /* Parse parameters. */
9227 param_list = cp_parser_parameter_declaration_clause (parser);
9229 /* Default arguments shall not be specified in the
9230 parameter-declaration-clause of a lambda-declarator. */
9231 for (tree t = param_list; t; t = TREE_CHAIN (t))
9232 if (TREE_PURPOSE (t))
9233 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9234 "default argument specified for lambda parameter");
9236 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9238 attributes = cp_parser_attributes_opt (parser);
9240 /* Parse optional `mutable' keyword. */
9241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9243 cp_lexer_consume_token (parser->lexer);
9244 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9247 /* Parse optional exception specification. */
9248 exception_spec = cp_parser_exception_specification_opt (parser);
9250 /* Parse optional trailing return type. */
9251 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9253 cp_lexer_consume_token (parser->lexer);
9254 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9255 = cp_parser_trailing_type_id (parser);
9258 /* The function parameters must be in scope all the way until after the
9259 trailing-return-type in case of decltype. */
9260 pop_bindings_and_leave_scope ();
9262 else if (template_param_list != NULL_TREE) // generate diagnostic
9263 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9265 /* Create the function call operator.
9267 Messing with declarators like this is no uglier than building up the
9268 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9269 other code. */
9271 cp_decl_specifier_seq return_type_specs;
9272 cp_declarator* declarator;
9273 tree fco;
9274 int quals;
9275 void *p;
9277 clear_decl_specs (&return_type_specs);
9278 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9279 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9280 else
9281 /* Maybe we will deduce the return type later. */
9282 return_type_specs.type = make_auto ();
9284 p = obstack_alloc (&declarator_obstack, 0);
9286 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9287 sfk_none);
9289 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9290 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9291 declarator = make_call_declarator (declarator, param_list, quals,
9292 VIRT_SPEC_UNSPECIFIED,
9293 REF_QUAL_NONE,
9294 exception_spec,
9295 /*late_return_type=*/NULL_TREE);
9296 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9298 fco = grokmethod (&return_type_specs,
9299 declarator,
9300 attributes);
9301 if (fco != error_mark_node)
9303 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9304 DECL_ARTIFICIAL (fco) = 1;
9305 /* Give the object parameter a different name. */
9306 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9307 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9308 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9310 if (template_param_list)
9312 fco = finish_member_template_decl (fco);
9313 finish_template_decl (template_param_list);
9314 --parser->num_template_parameter_lists;
9316 else if (parser->fully_implicit_function_template_p)
9317 fco = finish_fully_implicit_template (parser, fco);
9319 finish_member_declaration (fco);
9321 obstack_free (&declarator_obstack, p);
9323 return (fco != error_mark_node);
9327 /* Parse the body of a lambda expression, which is simply
9329 compound-statement
9331 but which requires special handling.
9332 LAMBDA_EXPR is the current representation of the lambda expression. */
9334 static void
9335 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9337 bool nested = (current_function_decl != NULL_TREE);
9338 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9339 if (nested)
9340 push_function_context ();
9341 else
9342 /* Still increment function_depth so that we don't GC in the
9343 middle of an expression. */
9344 ++function_depth;
9345 /* Clear this in case we're in the middle of a default argument. */
9346 parser->local_variables_forbidden_p = false;
9348 /* Finish the function call operator
9349 - class_specifier
9350 + late_parsing_for_member
9351 + function_definition_after_declarator
9352 + ctor_initializer_opt_and_function_body */
9354 tree fco = lambda_function (lambda_expr);
9355 tree body;
9356 bool done = false;
9357 tree compound_stmt;
9358 tree cap;
9360 /* Let the front end know that we are going to be defining this
9361 function. */
9362 start_preparsed_function (fco,
9363 NULL_TREE,
9364 SF_PRE_PARSED | SF_INCLASS_INLINE);
9366 start_lambda_scope (fco);
9367 body = begin_function_body ();
9369 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9370 goto out;
9372 /* Push the proxies for any explicit captures. */
9373 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9374 cap = TREE_CHAIN (cap))
9375 build_capture_proxy (TREE_PURPOSE (cap));
9377 compound_stmt = begin_compound_stmt (0);
9379 /* 5.1.1.4 of the standard says:
9380 If a lambda-expression does not include a trailing-return-type, it
9381 is as if the trailing-return-type denotes the following type:
9382 * if the compound-statement is of the form
9383 { return attribute-specifier [opt] expression ; }
9384 the type of the returned expression after lvalue-to-rvalue
9385 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9386 (_conv.array_ 4.2), and function-to-pointer conversion
9387 (_conv.func_ 4.3);
9388 * otherwise, void. */
9390 /* In a lambda that has neither a lambda-return-type-clause
9391 nor a deducible form, errors should be reported for return statements
9392 in the body. Since we used void as the placeholder return type, parsing
9393 the body as usual will give such desired behavior. */
9394 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9395 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9396 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9398 tree expr = NULL_TREE;
9399 cp_id_kind idk = CP_ID_KIND_NONE;
9401 /* Parse tentatively in case there's more after the initial return
9402 statement. */
9403 cp_parser_parse_tentatively (parser);
9405 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9407 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9409 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9410 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9412 if (cp_parser_parse_definitely (parser))
9414 if (!processing_template_decl)
9415 apply_deduced_return_type (fco, lambda_return_type (expr));
9417 /* Will get error here if type not deduced yet. */
9418 finish_return_stmt (expr);
9420 done = true;
9424 if (!done)
9426 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9427 cp_parser_label_declaration (parser);
9428 cp_parser_statement_seq_opt (parser, NULL_TREE);
9429 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9432 finish_compound_stmt (compound_stmt);
9434 out:
9435 finish_function_body (body);
9436 finish_lambda_scope ();
9438 /* Finish the function and generate code for it if necessary. */
9439 tree fn = finish_function (/*inline*/2);
9441 /* Only expand if the call op is not a template. */
9442 if (!DECL_TEMPLATE_INFO (fco))
9443 expand_or_defer_fn (fn);
9446 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9447 if (nested)
9448 pop_function_context();
9449 else
9450 --function_depth;
9453 /* Statements [gram.stmt.stmt] */
9455 /* Parse a statement.
9457 statement:
9458 labeled-statement
9459 expression-statement
9460 compound-statement
9461 selection-statement
9462 iteration-statement
9463 jump-statement
9464 declaration-statement
9465 try-block
9467 C++11:
9469 statement:
9470 labeled-statement
9471 attribute-specifier-seq (opt) expression-statement
9472 attribute-specifier-seq (opt) compound-statement
9473 attribute-specifier-seq (opt) selection-statement
9474 attribute-specifier-seq (opt) iteration-statement
9475 attribute-specifier-seq (opt) jump-statement
9476 declaration-statement
9477 attribute-specifier-seq (opt) try-block
9479 TM Extension:
9481 statement:
9482 atomic-statement
9484 IN_COMPOUND is true when the statement is nested inside a
9485 cp_parser_compound_statement; this matters for certain pragmas.
9487 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9488 is a (possibly labeled) if statement which is not enclosed in braces
9489 and has an else clause. This is used to implement -Wparentheses. */
9491 static void
9492 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9493 bool in_compound, bool *if_p)
9495 tree statement, std_attrs = NULL_TREE;
9496 cp_token *token;
9497 location_t statement_location, attrs_location;
9499 restart:
9500 if (if_p != NULL)
9501 *if_p = false;
9502 /* There is no statement yet. */
9503 statement = NULL_TREE;
9505 cp_lexer_save_tokens (parser->lexer);
9506 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9507 if (c_dialect_objc ())
9508 /* In obj-c++, seeing '[[' might be the either the beginning of
9509 c++11 attributes, or a nested objc-message-expression. So
9510 let's parse the c++11 attributes tentatively. */
9511 cp_parser_parse_tentatively (parser);
9512 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9513 if (c_dialect_objc ())
9515 if (!cp_parser_parse_definitely (parser))
9516 std_attrs = NULL_TREE;
9519 /* Peek at the next token. */
9520 token = cp_lexer_peek_token (parser->lexer);
9521 /* Remember the location of the first token in the statement. */
9522 statement_location = token->location;
9523 /* If this is a keyword, then that will often determine what kind of
9524 statement we have. */
9525 if (token->type == CPP_KEYWORD)
9527 enum rid keyword = token->keyword;
9529 switch (keyword)
9531 case RID_CASE:
9532 case RID_DEFAULT:
9533 /* Looks like a labeled-statement with a case label.
9534 Parse the label, and then use tail recursion to parse
9535 the statement. */
9536 cp_parser_label_for_labeled_statement (parser, std_attrs);
9537 goto restart;
9539 case RID_IF:
9540 case RID_SWITCH:
9541 statement = cp_parser_selection_statement (parser, if_p);
9542 break;
9544 case RID_WHILE:
9545 case RID_DO:
9546 case RID_FOR:
9547 statement = cp_parser_iteration_statement (parser, false);
9548 break;
9550 case RID_BREAK:
9551 case RID_CONTINUE:
9552 case RID_RETURN:
9553 case RID_GOTO:
9554 statement = cp_parser_jump_statement (parser);
9555 break;
9557 case RID_CILK_SYNC:
9558 cp_lexer_consume_token (parser->lexer);
9559 if (flag_cilkplus)
9561 tree sync_expr = build_cilk_sync ();
9562 SET_EXPR_LOCATION (sync_expr,
9563 token->location);
9564 statement = finish_expr_stmt (sync_expr);
9566 else
9568 error_at (token->location, "-fcilkplus must be enabled to use"
9569 " %<_Cilk_sync%>");
9570 statement = error_mark_node;
9572 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9573 break;
9575 /* Objective-C++ exception-handling constructs. */
9576 case RID_AT_TRY:
9577 case RID_AT_CATCH:
9578 case RID_AT_FINALLY:
9579 case RID_AT_SYNCHRONIZED:
9580 case RID_AT_THROW:
9581 statement = cp_parser_objc_statement (parser);
9582 break;
9584 case RID_TRY:
9585 statement = cp_parser_try_block (parser);
9586 break;
9588 case RID_NAMESPACE:
9589 /* This must be a namespace alias definition. */
9590 cp_parser_declaration_statement (parser);
9591 return;
9593 case RID_TRANSACTION_ATOMIC:
9594 case RID_TRANSACTION_RELAXED:
9595 statement = cp_parser_transaction (parser, keyword);
9596 break;
9597 case RID_TRANSACTION_CANCEL:
9598 statement = cp_parser_transaction_cancel (parser);
9599 break;
9601 default:
9602 /* It might be a keyword like `int' that can start a
9603 declaration-statement. */
9604 break;
9607 else if (token->type == CPP_NAME)
9609 /* If the next token is a `:', then we are looking at a
9610 labeled-statement. */
9611 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9612 if (token->type == CPP_COLON)
9614 /* Looks like a labeled-statement with an ordinary label.
9615 Parse the label, and then use tail recursion to parse
9616 the statement. */
9618 cp_parser_label_for_labeled_statement (parser, std_attrs);
9619 goto restart;
9622 /* Anything that starts with a `{' must be a compound-statement. */
9623 else if (token->type == CPP_OPEN_BRACE)
9624 statement = cp_parser_compound_statement (parser, NULL, false, false);
9625 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9626 a statement all its own. */
9627 else if (token->type == CPP_PRAGMA)
9629 /* Only certain OpenMP pragmas are attached to statements, and thus
9630 are considered statements themselves. All others are not. In
9631 the context of a compound, accept the pragma as a "statement" and
9632 return so that we can check for a close brace. Otherwise we
9633 require a real statement and must go back and read one. */
9634 if (in_compound)
9635 cp_parser_pragma (parser, pragma_compound);
9636 else if (!cp_parser_pragma (parser, pragma_stmt))
9637 goto restart;
9638 return;
9640 else if (token->type == CPP_EOF)
9642 cp_parser_error (parser, "expected statement");
9643 return;
9646 /* Everything else must be a declaration-statement or an
9647 expression-statement. Try for the declaration-statement
9648 first, unless we are looking at a `;', in which case we know that
9649 we have an expression-statement. */
9650 if (!statement)
9652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9654 if (std_attrs != NULL_TREE)
9656 /* Attributes should be parsed as part of the the
9657 declaration, so let's un-parse them. */
9658 cp_lexer_rollback_tokens (parser->lexer);
9659 std_attrs = NULL_TREE;
9662 cp_parser_parse_tentatively (parser);
9663 /* Try to parse the declaration-statement. */
9664 cp_parser_declaration_statement (parser);
9665 /* If that worked, we're done. */
9666 if (cp_parser_parse_definitely (parser))
9667 return;
9669 /* Look for an expression-statement instead. */
9670 statement = cp_parser_expression_statement (parser, in_statement_expr);
9673 /* Set the line number for the statement. */
9674 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9675 SET_EXPR_LOCATION (statement, statement_location);
9677 /* Note that for now, we don't do anything with c++11 statements
9678 parsed at this level. */
9679 if (std_attrs != NULL_TREE)
9680 warning_at (attrs_location,
9681 OPT_Wattributes,
9682 "attributes at the beginning of statement are ignored");
9685 /* Parse the label for a labeled-statement, i.e.
9687 identifier :
9688 case constant-expression :
9689 default :
9691 GNU Extension:
9692 case constant-expression ... constant-expression : statement
9694 When a label is parsed without errors, the label is added to the
9695 parse tree by the finish_* functions, so this function doesn't
9696 have to return the label. */
9698 static void
9699 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9701 cp_token *token;
9702 tree label = NULL_TREE;
9703 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9705 /* The next token should be an identifier. */
9706 token = cp_lexer_peek_token (parser->lexer);
9707 if (token->type != CPP_NAME
9708 && token->type != CPP_KEYWORD)
9710 cp_parser_error (parser, "expected labeled-statement");
9711 return;
9714 parser->colon_corrects_to_scope_p = false;
9715 switch (token->keyword)
9717 case RID_CASE:
9719 tree expr, expr_hi;
9720 cp_token *ellipsis;
9722 /* Consume the `case' token. */
9723 cp_lexer_consume_token (parser->lexer);
9724 /* Parse the constant-expression. */
9725 expr = cp_parser_constant_expression (parser,
9726 /*allow_non_constant_p=*/false,
9727 NULL);
9729 ellipsis = cp_lexer_peek_token (parser->lexer);
9730 if (ellipsis->type == CPP_ELLIPSIS)
9732 /* Consume the `...' token. */
9733 cp_lexer_consume_token (parser->lexer);
9734 expr_hi =
9735 cp_parser_constant_expression (parser,
9736 /*allow_non_constant_p=*/false,
9737 NULL);
9738 /* We don't need to emit warnings here, as the common code
9739 will do this for us. */
9741 else
9742 expr_hi = NULL_TREE;
9744 if (parser->in_switch_statement_p)
9745 finish_case_label (token->location, expr, expr_hi);
9746 else
9747 error_at (token->location,
9748 "case label %qE not within a switch statement",
9749 expr);
9751 break;
9753 case RID_DEFAULT:
9754 /* Consume the `default' token. */
9755 cp_lexer_consume_token (parser->lexer);
9757 if (parser->in_switch_statement_p)
9758 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9759 else
9760 error_at (token->location, "case label not within a switch statement");
9761 break;
9763 default:
9764 /* Anything else must be an ordinary label. */
9765 label = finish_label_stmt (cp_parser_identifier (parser));
9766 break;
9769 /* Require the `:' token. */
9770 cp_parser_require (parser, CPP_COLON, RT_COLON);
9772 /* An ordinary label may optionally be followed by attributes.
9773 However, this is only permitted if the attributes are then
9774 followed by a semicolon. This is because, for backward
9775 compatibility, when parsing
9776 lab: __attribute__ ((unused)) int i;
9777 we want the attribute to attach to "i", not "lab". */
9778 if (label != NULL_TREE
9779 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9781 tree attrs;
9782 cp_parser_parse_tentatively (parser);
9783 attrs = cp_parser_gnu_attributes_opt (parser);
9784 if (attrs == NULL_TREE
9785 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9786 cp_parser_abort_tentative_parse (parser);
9787 else if (!cp_parser_parse_definitely (parser))
9789 else
9790 attributes = chainon (attributes, attrs);
9793 if (attributes != NULL_TREE)
9794 cplus_decl_attributes (&label, attributes, 0);
9796 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9799 /* Parse an expression-statement.
9801 expression-statement:
9802 expression [opt] ;
9804 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9805 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9806 indicates whether this expression-statement is part of an
9807 expression statement. */
9809 static tree
9810 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9812 tree statement = NULL_TREE;
9813 cp_token *token = cp_lexer_peek_token (parser->lexer);
9815 /* If the next token is a ';', then there is no expression
9816 statement. */
9817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9819 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9820 if (statement == error_mark_node
9821 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9823 cp_parser_skip_to_end_of_block_or_statement (parser);
9824 return error_mark_node;
9828 /* Give a helpful message for "A<T>::type t;" and the like. */
9829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9830 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9832 if (TREE_CODE (statement) == SCOPE_REF)
9833 error_at (token->location, "need %<typename%> before %qE because "
9834 "%qT is a dependent scope",
9835 statement, TREE_OPERAND (statement, 0));
9836 else if (is_overloaded_fn (statement)
9837 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9839 /* A::A a; */
9840 tree fn = get_first_fn (statement);
9841 error_at (token->location,
9842 "%<%T::%D%> names the constructor, not the type",
9843 DECL_CONTEXT (fn), DECL_NAME (fn));
9847 /* Consume the final `;'. */
9848 cp_parser_consume_semicolon_at_end_of_statement (parser);
9850 if (in_statement_expr
9851 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9852 /* This is the final expression statement of a statement
9853 expression. */
9854 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9855 else if (statement)
9856 statement = finish_expr_stmt (statement);
9858 return statement;
9861 /* Parse a compound-statement.
9863 compound-statement:
9864 { statement-seq [opt] }
9866 GNU extension:
9868 compound-statement:
9869 { label-declaration-seq [opt] statement-seq [opt] }
9871 label-declaration-seq:
9872 label-declaration
9873 label-declaration-seq label-declaration
9875 Returns a tree representing the statement. */
9877 static tree
9878 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9879 bool in_try, bool function_body)
9881 tree compound_stmt;
9883 /* Consume the `{'. */
9884 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9885 return error_mark_node;
9886 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9887 && !function_body)
9888 pedwarn (input_location, OPT_Wpedantic,
9889 "compound-statement in constexpr function");
9890 /* Begin the compound-statement. */
9891 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9892 /* If the next keyword is `__label__' we have a label declaration. */
9893 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9894 cp_parser_label_declaration (parser);
9895 /* Parse an (optional) statement-seq. */
9896 cp_parser_statement_seq_opt (parser, in_statement_expr);
9897 /* Finish the compound-statement. */
9898 finish_compound_stmt (compound_stmt);
9899 /* Consume the `}'. */
9900 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9902 return compound_stmt;
9905 /* Parse an (optional) statement-seq.
9907 statement-seq:
9908 statement
9909 statement-seq [opt] statement */
9911 static void
9912 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9914 /* Scan statements until there aren't any more. */
9915 while (true)
9917 cp_token *token = cp_lexer_peek_token (parser->lexer);
9919 /* If we are looking at a `}', then we have run out of
9920 statements; the same is true if we have reached the end
9921 of file, or have stumbled upon a stray '@end'. */
9922 if (token->type == CPP_CLOSE_BRACE
9923 || token->type == CPP_EOF
9924 || token->type == CPP_PRAGMA_EOL
9925 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9926 break;
9928 /* If we are in a compound statement and find 'else' then
9929 something went wrong. */
9930 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9932 if (parser->in_statement & IN_IF_STMT)
9933 break;
9934 else
9936 token = cp_lexer_consume_token (parser->lexer);
9937 error_at (token->location, "%<else%> without a previous %<if%>");
9941 /* Parse the statement. */
9942 cp_parser_statement (parser, in_statement_expr, true, NULL);
9946 /* Parse a selection-statement.
9948 selection-statement:
9949 if ( condition ) statement
9950 if ( condition ) statement else statement
9951 switch ( condition ) statement
9953 Returns the new IF_STMT or SWITCH_STMT.
9955 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9956 is a (possibly labeled) if statement which is not enclosed in
9957 braces and has an else clause. This is used to implement
9958 -Wparentheses. */
9960 static tree
9961 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9963 cp_token *token;
9964 enum rid keyword;
9966 if (if_p != NULL)
9967 *if_p = false;
9969 /* Peek at the next token. */
9970 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9972 /* See what kind of keyword it is. */
9973 keyword = token->keyword;
9974 switch (keyword)
9976 case RID_IF:
9977 case RID_SWITCH:
9979 tree statement;
9980 tree condition;
9982 /* Look for the `('. */
9983 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9985 cp_parser_skip_to_end_of_statement (parser);
9986 return error_mark_node;
9989 /* Begin the selection-statement. */
9990 if (keyword == RID_IF)
9991 statement = begin_if_stmt ();
9992 else
9993 statement = begin_switch_stmt ();
9995 /* Parse the condition. */
9996 condition = cp_parser_condition (parser);
9997 /* Look for the `)'. */
9998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9999 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10000 /*consume_paren=*/true);
10002 if (keyword == RID_IF)
10004 bool nested_if;
10005 unsigned char in_statement;
10007 /* Add the condition. */
10008 finish_if_stmt_cond (condition, statement);
10010 /* Parse the then-clause. */
10011 in_statement = parser->in_statement;
10012 parser->in_statement |= IN_IF_STMT;
10013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10015 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10016 add_stmt (build_empty_stmt (loc));
10017 cp_lexer_consume_token (parser->lexer);
10018 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10019 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10020 "empty body in an %<if%> statement");
10021 nested_if = false;
10023 else
10024 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10025 parser->in_statement = in_statement;
10027 finish_then_clause (statement);
10029 /* If the next token is `else', parse the else-clause. */
10030 if (cp_lexer_next_token_is_keyword (parser->lexer,
10031 RID_ELSE))
10033 /* Consume the `else' keyword. */
10034 cp_lexer_consume_token (parser->lexer);
10035 begin_else_clause (statement);
10036 /* Parse the else-clause. */
10037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10039 location_t loc;
10040 loc = cp_lexer_peek_token (parser->lexer)->location;
10041 warning_at (loc,
10042 OPT_Wempty_body, "suggest braces around "
10043 "empty body in an %<else%> statement");
10044 add_stmt (build_empty_stmt (loc));
10045 cp_lexer_consume_token (parser->lexer);
10047 else
10048 cp_parser_implicitly_scoped_statement (parser, NULL);
10050 finish_else_clause (statement);
10052 /* If we are currently parsing a then-clause, then
10053 IF_P will not be NULL. We set it to true to
10054 indicate that this if statement has an else clause.
10055 This may trigger the Wparentheses warning below
10056 when we get back up to the parent if statement. */
10057 if (if_p != NULL)
10058 *if_p = true;
10060 else
10062 /* This if statement does not have an else clause. If
10063 NESTED_IF is true, then the then-clause is an if
10064 statement which does have an else clause. We warn
10065 about the potential ambiguity. */
10066 if (nested_if)
10067 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10068 "suggest explicit braces to avoid ambiguous"
10069 " %<else%>");
10072 /* Now we're all done with the if-statement. */
10073 finish_if_stmt (statement);
10075 else
10077 bool in_switch_statement_p;
10078 unsigned char in_statement;
10080 /* Add the condition. */
10081 finish_switch_cond (condition, statement);
10083 /* Parse the body of the switch-statement. */
10084 in_switch_statement_p = parser->in_switch_statement_p;
10085 in_statement = parser->in_statement;
10086 parser->in_switch_statement_p = true;
10087 parser->in_statement |= IN_SWITCH_STMT;
10088 cp_parser_implicitly_scoped_statement (parser, NULL);
10089 parser->in_switch_statement_p = in_switch_statement_p;
10090 parser->in_statement = in_statement;
10092 /* Now we're all done with the switch-statement. */
10093 finish_switch_stmt (statement);
10096 return statement;
10098 break;
10100 default:
10101 cp_parser_error (parser, "expected selection-statement");
10102 return error_mark_node;
10106 /* Parse a condition.
10108 condition:
10109 expression
10110 type-specifier-seq declarator = initializer-clause
10111 type-specifier-seq declarator braced-init-list
10113 GNU Extension:
10115 condition:
10116 type-specifier-seq declarator asm-specification [opt]
10117 attributes [opt] = assignment-expression
10119 Returns the expression that should be tested. */
10121 static tree
10122 cp_parser_condition (cp_parser* parser)
10124 cp_decl_specifier_seq type_specifiers;
10125 const char *saved_message;
10126 int declares_class_or_enum;
10128 /* Try the declaration first. */
10129 cp_parser_parse_tentatively (parser);
10130 /* New types are not allowed in the type-specifier-seq for a
10131 condition. */
10132 saved_message = parser->type_definition_forbidden_message;
10133 parser->type_definition_forbidden_message
10134 = G_("types may not be defined in conditions");
10135 /* Parse the type-specifier-seq. */
10136 cp_parser_decl_specifier_seq (parser,
10137 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10138 &type_specifiers,
10139 &declares_class_or_enum);
10140 /* Restore the saved message. */
10141 parser->type_definition_forbidden_message = saved_message;
10142 /* If all is well, we might be looking at a declaration. */
10143 if (!cp_parser_error_occurred (parser))
10145 tree decl;
10146 tree asm_specification;
10147 tree attributes;
10148 cp_declarator *declarator;
10149 tree initializer = NULL_TREE;
10151 /* Parse the declarator. */
10152 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10153 /*ctor_dtor_or_conv_p=*/NULL,
10154 /*parenthesized_p=*/NULL,
10155 /*member_p=*/false,
10156 /*friend_p=*/false);
10157 /* Parse the attributes. */
10158 attributes = cp_parser_attributes_opt (parser);
10159 /* Parse the asm-specification. */
10160 asm_specification = cp_parser_asm_specification_opt (parser);
10161 /* If the next token is not an `=' or '{', then we might still be
10162 looking at an expression. For example:
10164 if (A(a).x)
10166 looks like a decl-specifier-seq and a declarator -- but then
10167 there is no `=', so this is an expression. */
10168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10169 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10170 cp_parser_simulate_error (parser);
10172 /* If we did see an `=' or '{', then we are looking at a declaration
10173 for sure. */
10174 if (cp_parser_parse_definitely (parser))
10176 tree pushed_scope;
10177 bool non_constant_p;
10178 bool flags = LOOKUP_ONLYCONVERTING;
10180 /* Create the declaration. */
10181 decl = start_decl (declarator, &type_specifiers,
10182 /*initialized_p=*/true,
10183 attributes, /*prefix_attributes=*/NULL_TREE,
10184 &pushed_scope);
10186 /* Parse the initializer. */
10187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10189 initializer = cp_parser_braced_list (parser, &non_constant_p);
10190 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10191 flags = 0;
10193 else
10195 /* Consume the `='. */
10196 cp_parser_require (parser, CPP_EQ, RT_EQ);
10197 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10199 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10200 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10202 /* Process the initializer. */
10203 cp_finish_decl (decl,
10204 initializer, !non_constant_p,
10205 asm_specification,
10206 flags);
10208 if (pushed_scope)
10209 pop_scope (pushed_scope);
10211 return convert_from_reference (decl);
10214 /* If we didn't even get past the declarator successfully, we are
10215 definitely not looking at a declaration. */
10216 else
10217 cp_parser_abort_tentative_parse (parser);
10219 /* Otherwise, we are looking at an expression. */
10220 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10223 /* Parses a for-statement or range-for-statement until the closing ')',
10224 not included. */
10226 static tree
10227 cp_parser_for (cp_parser *parser, bool ivdep)
10229 tree init, scope, decl;
10230 bool is_range_for;
10232 /* Begin the for-statement. */
10233 scope = begin_for_scope (&init);
10235 /* Parse the initialization. */
10236 is_range_for = cp_parser_for_init_statement (parser, &decl);
10238 if (is_range_for)
10239 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10240 else
10241 return cp_parser_c_for (parser, scope, init, ivdep);
10244 static tree
10245 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10247 /* Normal for loop */
10248 tree condition = NULL_TREE;
10249 tree expression = NULL_TREE;
10250 tree stmt;
10252 stmt = begin_for_stmt (scope, init);
10253 /* The for-init-statement has already been parsed in
10254 cp_parser_for_init_statement, so no work is needed here. */
10255 finish_for_init_stmt (stmt);
10257 /* If there's a condition, process it. */
10258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10259 condition = cp_parser_condition (parser);
10260 else if (ivdep)
10262 cp_parser_error (parser, "missing loop condition in loop with "
10263 "%<GCC ivdep%> pragma");
10264 condition = error_mark_node;
10266 finish_for_cond (condition, stmt, ivdep);
10267 /* Look for the `;'. */
10268 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10270 /* If there's an expression, process it. */
10271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10272 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10273 finish_for_expr (expression, stmt);
10275 return stmt;
10278 /* Tries to parse a range-based for-statement:
10280 range-based-for:
10281 decl-specifier-seq declarator : expression
10283 The decl-specifier-seq declarator and the `:' are already parsed by
10284 cp_parser_for_init_statement. If processing_template_decl it returns a
10285 newly created RANGE_FOR_STMT; if not, it is converted to a
10286 regular FOR_STMT. */
10288 static tree
10289 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10290 bool ivdep)
10292 tree stmt, range_expr;
10294 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10296 bool expr_non_constant_p;
10297 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10299 else
10300 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10302 /* If in template, STMT is converted to a normal for-statement
10303 at instantiation. If not, it is done just ahead. */
10304 if (processing_template_decl)
10306 if (check_for_bare_parameter_packs (range_expr))
10307 range_expr = error_mark_node;
10308 stmt = begin_range_for_stmt (scope, init);
10309 if (ivdep)
10310 RANGE_FOR_IVDEP (stmt) = 1;
10311 finish_range_for_decl (stmt, range_decl, range_expr);
10312 if (!type_dependent_expression_p (range_expr)
10313 /* do_auto_deduction doesn't mess with template init-lists. */
10314 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10315 do_range_for_auto_deduction (range_decl, range_expr);
10317 else
10319 stmt = begin_for_stmt (scope, init);
10320 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10322 return stmt;
10325 /* Subroutine of cp_convert_range_for: given the initializer expression,
10326 builds up the range temporary. */
10328 static tree
10329 build_range_temp (tree range_expr)
10331 tree range_type, range_temp;
10333 /* Find out the type deduced by the declaration
10334 `auto &&__range = range_expr'. */
10335 range_type = cp_build_reference_type (make_auto (), true);
10336 range_type = do_auto_deduction (range_type, range_expr,
10337 type_uses_auto (range_type));
10339 /* Create the __range variable. */
10340 range_temp = build_decl (input_location, VAR_DECL,
10341 get_identifier ("__for_range"), range_type);
10342 TREE_USED (range_temp) = 1;
10343 DECL_ARTIFICIAL (range_temp) = 1;
10345 return range_temp;
10348 /* Used by cp_parser_range_for in template context: we aren't going to
10349 do a full conversion yet, but we still need to resolve auto in the
10350 type of the for-range-declaration if present. This is basically
10351 a shortcut version of cp_convert_range_for. */
10353 static void
10354 do_range_for_auto_deduction (tree decl, tree range_expr)
10356 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10357 if (auto_node)
10359 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10360 range_temp = convert_from_reference (build_range_temp (range_expr));
10361 iter_type = (cp_parser_perform_range_for_lookup
10362 (range_temp, &begin_dummy, &end_dummy));
10363 if (iter_type)
10365 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10366 iter_type);
10367 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10368 tf_warning_or_error);
10369 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10370 iter_decl, auto_node);
10375 /* Converts a range-based for-statement into a normal
10376 for-statement, as per the definition.
10378 for (RANGE_DECL : RANGE_EXPR)
10379 BLOCK
10381 should be equivalent to:
10384 auto &&__range = RANGE_EXPR;
10385 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10386 __begin != __end;
10387 ++__begin)
10389 RANGE_DECL = *__begin;
10390 BLOCK
10394 If RANGE_EXPR is an array:
10395 BEGIN_EXPR = __range
10396 END_EXPR = __range + ARRAY_SIZE(__range)
10397 Else if RANGE_EXPR has a member 'begin' or 'end':
10398 BEGIN_EXPR = __range.begin()
10399 END_EXPR = __range.end()
10400 Else:
10401 BEGIN_EXPR = begin(__range)
10402 END_EXPR = end(__range);
10404 If __range has a member 'begin' but not 'end', or vice versa, we must
10405 still use the second alternative (it will surely fail, however).
10406 When calling begin()/end() in the third alternative we must use
10407 argument dependent lookup, but always considering 'std' as an associated
10408 namespace. */
10410 tree
10411 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10412 bool ivdep)
10414 tree begin, end;
10415 tree iter_type, begin_expr, end_expr;
10416 tree condition, expression;
10418 if (range_decl == error_mark_node || range_expr == error_mark_node)
10419 /* If an error happened previously do nothing or else a lot of
10420 unhelpful errors would be issued. */
10421 begin_expr = end_expr = iter_type = error_mark_node;
10422 else
10424 tree range_temp;
10426 if (TREE_CODE (range_expr) == VAR_DECL
10427 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10428 /* Can't bind a reference to an array of runtime bound. */
10429 range_temp = range_expr;
10430 else
10432 range_temp = build_range_temp (range_expr);
10433 pushdecl (range_temp);
10434 cp_finish_decl (range_temp, range_expr,
10435 /*is_constant_init*/false, NULL_TREE,
10436 LOOKUP_ONLYCONVERTING);
10437 range_temp = convert_from_reference (range_temp);
10439 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10440 &begin_expr, &end_expr);
10443 /* The new for initialization statement. */
10444 begin = build_decl (input_location, VAR_DECL,
10445 get_identifier ("__for_begin"), iter_type);
10446 TREE_USED (begin) = 1;
10447 DECL_ARTIFICIAL (begin) = 1;
10448 pushdecl (begin);
10449 cp_finish_decl (begin, begin_expr,
10450 /*is_constant_init*/false, NULL_TREE,
10451 LOOKUP_ONLYCONVERTING);
10453 end = build_decl (input_location, VAR_DECL,
10454 get_identifier ("__for_end"), iter_type);
10455 TREE_USED (end) = 1;
10456 DECL_ARTIFICIAL (end) = 1;
10457 pushdecl (end);
10458 cp_finish_decl (end, end_expr,
10459 /*is_constant_init*/false, NULL_TREE,
10460 LOOKUP_ONLYCONVERTING);
10462 finish_for_init_stmt (statement);
10464 /* The new for condition. */
10465 condition = build_x_binary_op (input_location, NE_EXPR,
10466 begin, ERROR_MARK,
10467 end, ERROR_MARK,
10468 NULL, tf_warning_or_error);
10469 finish_for_cond (condition, statement, ivdep);
10471 /* The new increment expression. */
10472 expression = finish_unary_op_expr (input_location,
10473 PREINCREMENT_EXPR, begin,
10474 tf_warning_or_error);
10475 finish_for_expr (expression, statement);
10477 /* The declaration is initialized with *__begin inside the loop body. */
10478 cp_finish_decl (range_decl,
10479 build_x_indirect_ref (input_location, begin, RO_NULL,
10480 tf_warning_or_error),
10481 /*is_constant_init*/false, NULL_TREE,
10482 LOOKUP_ONLYCONVERTING);
10484 return statement;
10487 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10488 We need to solve both at the same time because the method used
10489 depends on the existence of members begin or end.
10490 Returns the type deduced for the iterator expression. */
10492 static tree
10493 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10495 if (error_operand_p (range))
10497 *begin = *end = error_mark_node;
10498 return error_mark_node;
10501 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10503 error ("range-based %<for%> expression of type %qT "
10504 "has incomplete type", TREE_TYPE (range));
10505 *begin = *end = error_mark_node;
10506 return error_mark_node;
10508 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10510 /* If RANGE is an array, we will use pointer arithmetic. */
10511 *begin = range;
10512 *end = build_binary_op (input_location, PLUS_EXPR,
10513 range,
10514 array_type_nelts_top (TREE_TYPE (range)),
10516 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10518 else
10520 /* If it is not an array, we must do a bit of magic. */
10521 tree id_begin, id_end;
10522 tree member_begin, member_end;
10524 *begin = *end = error_mark_node;
10526 id_begin = get_identifier ("begin");
10527 id_end = get_identifier ("end");
10528 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10529 /*protect=*/2, /*want_type=*/false,
10530 tf_warning_or_error);
10531 member_end = lookup_member (TREE_TYPE (range), id_end,
10532 /*protect=*/2, /*want_type=*/false,
10533 tf_warning_or_error);
10535 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10537 /* Use the member functions. */
10538 if (member_begin != NULL_TREE)
10539 *begin = cp_parser_range_for_member_function (range, id_begin);
10540 else
10541 error ("range-based %<for%> expression of type %qT has an "
10542 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10544 if (member_end != NULL_TREE)
10545 *end = cp_parser_range_for_member_function (range, id_end);
10546 else
10547 error ("range-based %<for%> expression of type %qT has a "
10548 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10550 else
10552 /* Use global functions with ADL. */
10553 vec<tree, va_gc> *vec;
10554 vec = make_tree_vector ();
10556 vec_safe_push (vec, range);
10558 member_begin = perform_koenig_lookup (id_begin, vec,
10559 tf_warning_or_error);
10560 *begin = finish_call_expr (member_begin, &vec, false, true,
10561 tf_warning_or_error);
10562 member_end = perform_koenig_lookup (id_end, vec,
10563 tf_warning_or_error);
10564 *end = finish_call_expr (member_end, &vec, false, true,
10565 tf_warning_or_error);
10567 release_tree_vector (vec);
10570 /* Last common checks. */
10571 if (*begin == error_mark_node || *end == error_mark_node)
10573 /* If one of the expressions is an error do no more checks. */
10574 *begin = *end = error_mark_node;
10575 return error_mark_node;
10577 else if (type_dependent_expression_p (*begin)
10578 || type_dependent_expression_p (*end))
10579 /* Can happen, when, eg, in a template context, Koenig lookup
10580 can't resolve begin/end (c++/58503). */
10581 return NULL_TREE;
10582 else
10584 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10585 /* The unqualified type of the __begin and __end temporaries should
10586 be the same, as required by the multiple auto declaration. */
10587 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10588 error ("inconsistent begin/end types in range-based %<for%> "
10589 "statement: %qT and %qT",
10590 TREE_TYPE (*begin), TREE_TYPE (*end));
10591 return iter_type;
10596 /* Helper function for cp_parser_perform_range_for_lookup.
10597 Builds a tree for RANGE.IDENTIFIER(). */
10599 static tree
10600 cp_parser_range_for_member_function (tree range, tree identifier)
10602 tree member, res;
10603 vec<tree, va_gc> *vec;
10605 member = finish_class_member_access_expr (range, identifier,
10606 false, tf_warning_or_error);
10607 if (member == error_mark_node)
10608 return error_mark_node;
10610 vec = make_tree_vector ();
10611 res = finish_call_expr (member, &vec,
10612 /*disallow_virtual=*/false,
10613 /*koenig_p=*/false,
10614 tf_warning_or_error);
10615 release_tree_vector (vec);
10616 return res;
10619 /* Parse an iteration-statement.
10621 iteration-statement:
10622 while ( condition ) statement
10623 do statement while ( expression ) ;
10624 for ( for-init-statement condition [opt] ; expression [opt] )
10625 statement
10627 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10629 static tree
10630 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10632 cp_token *token;
10633 enum rid keyword;
10634 tree statement;
10635 unsigned char in_statement;
10637 /* Peek at the next token. */
10638 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10639 if (!token)
10640 return error_mark_node;
10642 /* Remember whether or not we are already within an iteration
10643 statement. */
10644 in_statement = parser->in_statement;
10646 /* See what kind of keyword it is. */
10647 keyword = token->keyword;
10648 switch (keyword)
10650 case RID_WHILE:
10652 tree condition;
10654 /* Begin the while-statement. */
10655 statement = begin_while_stmt ();
10656 /* Look for the `('. */
10657 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10658 /* Parse the condition. */
10659 condition = cp_parser_condition (parser);
10660 finish_while_stmt_cond (condition, statement, ivdep);
10661 /* Look for the `)'. */
10662 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10663 /* Parse the dependent statement. */
10664 parser->in_statement = IN_ITERATION_STMT;
10665 cp_parser_already_scoped_statement (parser);
10666 parser->in_statement = in_statement;
10667 /* We're done with the while-statement. */
10668 finish_while_stmt (statement);
10670 break;
10672 case RID_DO:
10674 tree expression;
10676 /* Begin the do-statement. */
10677 statement = begin_do_stmt ();
10678 /* Parse the body of the do-statement. */
10679 parser->in_statement = IN_ITERATION_STMT;
10680 cp_parser_implicitly_scoped_statement (parser, NULL);
10681 parser->in_statement = in_statement;
10682 finish_do_body (statement);
10683 /* Look for the `while' keyword. */
10684 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10685 /* Look for the `('. */
10686 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10687 /* Parse the expression. */
10688 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10689 /* We're done with the do-statement. */
10690 finish_do_stmt (expression, statement, ivdep);
10691 /* Look for the `)'. */
10692 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10693 /* Look for the `;'. */
10694 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10696 break;
10698 case RID_FOR:
10700 /* Look for the `('. */
10701 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10703 statement = cp_parser_for (parser, ivdep);
10705 /* Look for the `)'. */
10706 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10708 /* Parse the body of the for-statement. */
10709 parser->in_statement = IN_ITERATION_STMT;
10710 cp_parser_already_scoped_statement (parser);
10711 parser->in_statement = in_statement;
10713 /* We're done with the for-statement. */
10714 finish_for_stmt (statement);
10716 break;
10718 default:
10719 cp_parser_error (parser, "expected iteration-statement");
10720 statement = error_mark_node;
10721 break;
10724 return statement;
10727 /* Parse a for-init-statement or the declarator of a range-based-for.
10728 Returns true if a range-based-for declaration is seen.
10730 for-init-statement:
10731 expression-statement
10732 simple-declaration */
10734 static bool
10735 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10737 /* If the next token is a `;', then we have an empty
10738 expression-statement. Grammatically, this is also a
10739 simple-declaration, but an invalid one, because it does not
10740 declare anything. Therefore, if we did not handle this case
10741 specially, we would issue an error message about an invalid
10742 declaration. */
10743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10745 bool is_range_for = false;
10746 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10748 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10749 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10751 /* N3994 -- for (id : init) ... */
10752 if (cxx_dialect < cxx1z)
10753 pedwarn (input_location, 0, "range-based for loop without a "
10754 "type-specifier only available with "
10755 "-std=c++1z or -std=gnu++1z");
10756 tree name = cp_parser_identifier (parser);
10757 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10758 *decl = build_decl (input_location, VAR_DECL, name, type);
10759 pushdecl (*decl);
10760 cp_lexer_consume_token (parser->lexer);
10761 return true;
10764 /* A colon is used in range-based for. */
10765 parser->colon_corrects_to_scope_p = false;
10767 /* We're going to speculatively look for a declaration, falling back
10768 to an expression, if necessary. */
10769 cp_parser_parse_tentatively (parser);
10770 /* Parse the declaration. */
10771 cp_parser_simple_declaration (parser,
10772 /*function_definition_allowed_p=*/false,
10773 decl);
10774 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10775 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10777 /* It is a range-for, consume the ':' */
10778 cp_lexer_consume_token (parser->lexer);
10779 is_range_for = true;
10780 if (cxx_dialect < cxx11)
10782 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10783 "range-based %<for%> loops only available with "
10784 "-std=c++11 or -std=gnu++11");
10785 *decl = error_mark_node;
10788 else
10789 /* The ';' is not consumed yet because we told
10790 cp_parser_simple_declaration not to. */
10791 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10793 if (cp_parser_parse_definitely (parser))
10794 return is_range_for;
10795 /* If the tentative parse failed, then we shall need to look for an
10796 expression-statement. */
10798 /* If we are here, it is an expression-statement. */
10799 cp_parser_expression_statement (parser, NULL_TREE);
10800 return false;
10803 /* Parse a jump-statement.
10805 jump-statement:
10806 break ;
10807 continue ;
10808 return expression [opt] ;
10809 return braced-init-list ;
10810 goto identifier ;
10812 GNU extension:
10814 jump-statement:
10815 goto * expression ;
10817 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10819 static tree
10820 cp_parser_jump_statement (cp_parser* parser)
10822 tree statement = error_mark_node;
10823 cp_token *token;
10824 enum rid keyword;
10825 unsigned char in_statement;
10827 /* Peek at the next token. */
10828 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10829 if (!token)
10830 return error_mark_node;
10832 /* See what kind of keyword it is. */
10833 keyword = token->keyword;
10834 switch (keyword)
10836 case RID_BREAK:
10837 in_statement = parser->in_statement & ~IN_IF_STMT;
10838 switch (in_statement)
10840 case 0:
10841 error_at (token->location, "break statement not within loop or switch");
10842 break;
10843 default:
10844 gcc_assert ((in_statement & IN_SWITCH_STMT)
10845 || in_statement == IN_ITERATION_STMT);
10846 statement = finish_break_stmt ();
10847 if (in_statement == IN_ITERATION_STMT)
10848 break_maybe_infinite_loop ();
10849 break;
10850 case IN_OMP_BLOCK:
10851 error_at (token->location, "invalid exit from OpenMP structured block");
10852 break;
10853 case IN_OMP_FOR:
10854 error_at (token->location, "break statement used with OpenMP for loop");
10855 break;
10856 case IN_CILK_SIMD_FOR:
10857 error_at (token->location, "break statement used with Cilk Plus for loop");
10858 break;
10860 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10861 break;
10863 case RID_CONTINUE:
10864 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10866 case 0:
10867 error_at (token->location, "continue statement not within a loop");
10868 break;
10869 case IN_CILK_SIMD_FOR:
10870 error_at (token->location,
10871 "continue statement within %<#pragma simd%> loop body");
10872 /* Fall through. */
10873 case IN_ITERATION_STMT:
10874 case IN_OMP_FOR:
10875 statement = finish_continue_stmt ();
10876 break;
10877 case IN_OMP_BLOCK:
10878 error_at (token->location, "invalid exit from OpenMP structured block");
10879 break;
10880 default:
10881 gcc_unreachable ();
10883 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10884 break;
10886 case RID_RETURN:
10888 tree expr;
10889 bool expr_non_constant_p;
10891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10893 cp_lexer_set_source_position (parser->lexer);
10894 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10895 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10897 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10898 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10899 else
10900 /* If the next token is a `;', then there is no
10901 expression. */
10902 expr = NULL_TREE;
10903 /* Build the return-statement. */
10904 statement = finish_return_stmt (expr);
10905 /* Look for the final `;'. */
10906 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10908 break;
10910 case RID_GOTO:
10911 /* Create the goto-statement. */
10912 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10914 /* Issue a warning about this use of a GNU extension. */
10915 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10916 /* Consume the '*' token. */
10917 cp_lexer_consume_token (parser->lexer);
10918 /* Parse the dependent expression. */
10919 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10921 else
10922 finish_goto_stmt (cp_parser_identifier (parser));
10923 /* Look for the final `;'. */
10924 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10925 break;
10927 default:
10928 cp_parser_error (parser, "expected jump-statement");
10929 break;
10932 return statement;
10935 /* Parse a declaration-statement.
10937 declaration-statement:
10938 block-declaration */
10940 static void
10941 cp_parser_declaration_statement (cp_parser* parser)
10943 void *p;
10945 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10946 p = obstack_alloc (&declarator_obstack, 0);
10948 /* Parse the block-declaration. */
10949 cp_parser_block_declaration (parser, /*statement_p=*/true);
10951 /* Free any declarators allocated. */
10952 obstack_free (&declarator_obstack, p);
10955 /* Some dependent statements (like `if (cond) statement'), are
10956 implicitly in their own scope. In other words, if the statement is
10957 a single statement (as opposed to a compound-statement), it is
10958 none-the-less treated as if it were enclosed in braces. Any
10959 declarations appearing in the dependent statement are out of scope
10960 after control passes that point. This function parses a statement,
10961 but ensures that is in its own scope, even if it is not a
10962 compound-statement.
10964 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10965 is a (possibly labeled) if statement which is not enclosed in
10966 braces and has an else clause. This is used to implement
10967 -Wparentheses.
10969 Returns the new statement. */
10971 static tree
10972 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10974 tree statement;
10976 if (if_p != NULL)
10977 *if_p = false;
10979 /* Mark if () ; with a special NOP_EXPR. */
10980 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10982 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10983 cp_lexer_consume_token (parser->lexer);
10984 statement = add_stmt (build_empty_stmt (loc));
10986 /* if a compound is opened, we simply parse the statement directly. */
10987 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10988 statement = cp_parser_compound_statement (parser, NULL, false, false);
10989 /* If the token is not a `{', then we must take special action. */
10990 else
10992 /* Create a compound-statement. */
10993 statement = begin_compound_stmt (0);
10994 /* Parse the dependent-statement. */
10995 cp_parser_statement (parser, NULL_TREE, false, if_p);
10996 /* Finish the dummy compound-statement. */
10997 finish_compound_stmt (statement);
11000 /* Return the statement. */
11001 return statement;
11004 /* For some dependent statements (like `while (cond) statement'), we
11005 have already created a scope. Therefore, even if the dependent
11006 statement is a compound-statement, we do not want to create another
11007 scope. */
11009 static void
11010 cp_parser_already_scoped_statement (cp_parser* parser)
11012 /* If the token is a `{', then we must take special action. */
11013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11014 cp_parser_statement (parser, NULL_TREE, false, NULL);
11015 else
11017 /* Avoid calling cp_parser_compound_statement, so that we
11018 don't create a new scope. Do everything else by hand. */
11019 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11020 /* If the next keyword is `__label__' we have a label declaration. */
11021 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11022 cp_parser_label_declaration (parser);
11023 /* Parse an (optional) statement-seq. */
11024 cp_parser_statement_seq_opt (parser, NULL_TREE);
11025 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11029 /* Declarations [gram.dcl.dcl] */
11031 /* Parse an optional declaration-sequence.
11033 declaration-seq:
11034 declaration
11035 declaration-seq declaration */
11037 static void
11038 cp_parser_declaration_seq_opt (cp_parser* parser)
11040 while (true)
11042 cp_token *token;
11044 token = cp_lexer_peek_token (parser->lexer);
11046 if (token->type == CPP_CLOSE_BRACE
11047 || token->type == CPP_EOF
11048 || token->type == CPP_PRAGMA_EOL)
11049 break;
11051 if (token->type == CPP_SEMICOLON)
11053 /* A declaration consisting of a single semicolon is
11054 invalid. Allow it unless we're being pedantic. */
11055 cp_lexer_consume_token (parser->lexer);
11056 if (!in_system_header_at (input_location))
11057 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11058 continue;
11061 /* If we're entering or exiting a region that's implicitly
11062 extern "C", modify the lang context appropriately. */
11063 if (!parser->implicit_extern_c && token->implicit_extern_c)
11065 push_lang_context (lang_name_c);
11066 parser->implicit_extern_c = true;
11068 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11070 pop_lang_context ();
11071 parser->implicit_extern_c = false;
11074 if (token->type == CPP_PRAGMA)
11076 /* A top-level declaration can consist solely of a #pragma.
11077 A nested declaration cannot, so this is done here and not
11078 in cp_parser_declaration. (A #pragma at block scope is
11079 handled in cp_parser_statement.) */
11080 cp_parser_pragma (parser, pragma_external);
11081 continue;
11084 /* Parse the declaration itself. */
11085 cp_parser_declaration (parser);
11089 /* Parse a declaration.
11091 declaration:
11092 block-declaration
11093 function-definition
11094 template-declaration
11095 explicit-instantiation
11096 explicit-specialization
11097 linkage-specification
11098 namespace-definition
11100 GNU extension:
11102 declaration:
11103 __extension__ declaration */
11105 static void
11106 cp_parser_declaration (cp_parser* parser)
11108 cp_token token1;
11109 cp_token token2;
11110 int saved_pedantic;
11111 void *p;
11112 tree attributes = NULL_TREE;
11114 /* Check for the `__extension__' keyword. */
11115 if (cp_parser_extension_opt (parser, &saved_pedantic))
11117 /* Parse the qualified declaration. */
11118 cp_parser_declaration (parser);
11119 /* Restore the PEDANTIC flag. */
11120 pedantic = saved_pedantic;
11122 return;
11125 /* Try to figure out what kind of declaration is present. */
11126 token1 = *cp_lexer_peek_token (parser->lexer);
11128 if (token1.type != CPP_EOF)
11129 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11130 else
11132 token2.type = CPP_EOF;
11133 token2.keyword = RID_MAX;
11136 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11137 p = obstack_alloc (&declarator_obstack, 0);
11139 /* If the next token is `extern' and the following token is a string
11140 literal, then we have a linkage specification. */
11141 if (token1.keyword == RID_EXTERN
11142 && cp_parser_is_pure_string_literal (&token2))
11143 cp_parser_linkage_specification (parser);
11144 /* If the next token is `template', then we have either a template
11145 declaration, an explicit instantiation, or an explicit
11146 specialization. */
11147 else if (token1.keyword == RID_TEMPLATE)
11149 /* `template <>' indicates a template specialization. */
11150 if (token2.type == CPP_LESS
11151 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11152 cp_parser_explicit_specialization (parser);
11153 /* `template <' indicates a template declaration. */
11154 else if (token2.type == CPP_LESS)
11155 cp_parser_template_declaration (parser, /*member_p=*/false);
11156 /* Anything else must be an explicit instantiation. */
11157 else
11158 cp_parser_explicit_instantiation (parser);
11160 /* If the next token is `export', then we have a template
11161 declaration. */
11162 else if (token1.keyword == RID_EXPORT)
11163 cp_parser_template_declaration (parser, /*member_p=*/false);
11164 /* If the next token is `extern', 'static' or 'inline' and the one
11165 after that is `template', we have a GNU extended explicit
11166 instantiation directive. */
11167 else if (cp_parser_allow_gnu_extensions_p (parser)
11168 && (token1.keyword == RID_EXTERN
11169 || token1.keyword == RID_STATIC
11170 || token1.keyword == RID_INLINE)
11171 && token2.keyword == RID_TEMPLATE)
11172 cp_parser_explicit_instantiation (parser);
11173 /* If the next token is `namespace', check for a named or unnamed
11174 namespace definition. */
11175 else if (token1.keyword == RID_NAMESPACE
11176 && (/* A named namespace definition. */
11177 (token2.type == CPP_NAME
11178 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11179 != CPP_EQ))
11180 /* An unnamed namespace definition. */
11181 || token2.type == CPP_OPEN_BRACE
11182 || token2.keyword == RID_ATTRIBUTE))
11183 cp_parser_namespace_definition (parser);
11184 /* An inline (associated) namespace definition. */
11185 else if (token1.keyword == RID_INLINE
11186 && token2.keyword == RID_NAMESPACE)
11187 cp_parser_namespace_definition (parser);
11188 /* Objective-C++ declaration/definition. */
11189 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11190 cp_parser_objc_declaration (parser, NULL_TREE);
11191 else if (c_dialect_objc ()
11192 && token1.keyword == RID_ATTRIBUTE
11193 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11194 cp_parser_objc_declaration (parser, attributes);
11195 /* We must have either a block declaration or a function
11196 definition. */
11197 else
11198 /* Try to parse a block-declaration, or a function-definition. */
11199 cp_parser_block_declaration (parser, /*statement_p=*/false);
11201 /* Free any declarators allocated. */
11202 obstack_free (&declarator_obstack, p);
11205 /* Parse a block-declaration.
11207 block-declaration:
11208 simple-declaration
11209 asm-definition
11210 namespace-alias-definition
11211 using-declaration
11212 using-directive
11214 GNU Extension:
11216 block-declaration:
11217 __extension__ block-declaration
11219 C++0x Extension:
11221 block-declaration:
11222 static_assert-declaration
11224 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11225 part of a declaration-statement. */
11227 static void
11228 cp_parser_block_declaration (cp_parser *parser,
11229 bool statement_p)
11231 cp_token *token1;
11232 int saved_pedantic;
11234 /* Check for the `__extension__' keyword. */
11235 if (cp_parser_extension_opt (parser, &saved_pedantic))
11237 /* Parse the qualified declaration. */
11238 cp_parser_block_declaration (parser, statement_p);
11239 /* Restore the PEDANTIC flag. */
11240 pedantic = saved_pedantic;
11242 return;
11245 /* Peek at the next token to figure out which kind of declaration is
11246 present. */
11247 token1 = cp_lexer_peek_token (parser->lexer);
11249 /* If the next keyword is `asm', we have an asm-definition. */
11250 if (token1->keyword == RID_ASM)
11252 if (statement_p)
11253 cp_parser_commit_to_tentative_parse (parser);
11254 cp_parser_asm_definition (parser);
11256 /* If the next keyword is `namespace', we have a
11257 namespace-alias-definition. */
11258 else if (token1->keyword == RID_NAMESPACE)
11259 cp_parser_namespace_alias_definition (parser);
11260 /* If the next keyword is `using', we have a
11261 using-declaration, a using-directive, or an alias-declaration. */
11262 else if (token1->keyword == RID_USING)
11264 cp_token *token2;
11266 if (statement_p)
11267 cp_parser_commit_to_tentative_parse (parser);
11268 /* If the token after `using' is `namespace', then we have a
11269 using-directive. */
11270 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11271 if (token2->keyword == RID_NAMESPACE)
11272 cp_parser_using_directive (parser);
11273 /* If the second token after 'using' is '=', then we have an
11274 alias-declaration. */
11275 else if (cxx_dialect >= cxx11
11276 && token2->type == CPP_NAME
11277 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11278 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11279 cp_parser_alias_declaration (parser);
11280 /* Otherwise, it's a using-declaration. */
11281 else
11282 cp_parser_using_declaration (parser,
11283 /*access_declaration_p=*/false);
11285 /* If the next keyword is `__label__' we have a misplaced label
11286 declaration. */
11287 else if (token1->keyword == RID_LABEL)
11289 cp_lexer_consume_token (parser->lexer);
11290 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11291 cp_parser_skip_to_end_of_statement (parser);
11292 /* If the next token is now a `;', consume it. */
11293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11294 cp_lexer_consume_token (parser->lexer);
11296 /* If the next token is `static_assert' we have a static assertion. */
11297 else if (token1->keyword == RID_STATIC_ASSERT)
11298 cp_parser_static_assert (parser, /*member_p=*/false);
11299 /* Anything else must be a simple-declaration. */
11300 else
11301 cp_parser_simple_declaration (parser, !statement_p,
11302 /*maybe_range_for_decl*/NULL);
11305 /* Parse a simple-declaration.
11307 simple-declaration:
11308 decl-specifier-seq [opt] init-declarator-list [opt] ;
11310 init-declarator-list:
11311 init-declarator
11312 init-declarator-list , init-declarator
11314 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11315 function-definition as a simple-declaration.
11317 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11318 parsed declaration if it is an uninitialized single declarator not followed
11319 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11320 if present, will not be consumed. */
11322 static void
11323 cp_parser_simple_declaration (cp_parser* parser,
11324 bool function_definition_allowed_p,
11325 tree *maybe_range_for_decl)
11327 cp_decl_specifier_seq decl_specifiers;
11328 int declares_class_or_enum;
11329 bool saw_declarator;
11331 if (maybe_range_for_decl)
11332 *maybe_range_for_decl = NULL_TREE;
11334 /* Defer access checks until we know what is being declared; the
11335 checks for names appearing in the decl-specifier-seq should be
11336 done as if we were in the scope of the thing being declared. */
11337 push_deferring_access_checks (dk_deferred);
11339 /* Parse the decl-specifier-seq. We have to keep track of whether
11340 or not the decl-specifier-seq declares a named class or
11341 enumeration type, since that is the only case in which the
11342 init-declarator-list is allowed to be empty.
11344 [dcl.dcl]
11346 In a simple-declaration, the optional init-declarator-list can be
11347 omitted only when declaring a class or enumeration, that is when
11348 the decl-specifier-seq contains either a class-specifier, an
11349 elaborated-type-specifier, or an enum-specifier. */
11350 cp_parser_decl_specifier_seq (parser,
11351 CP_PARSER_FLAGS_OPTIONAL,
11352 &decl_specifiers,
11353 &declares_class_or_enum);
11354 /* We no longer need to defer access checks. */
11355 stop_deferring_access_checks ();
11357 /* In a block scope, a valid declaration must always have a
11358 decl-specifier-seq. By not trying to parse declarators, we can
11359 resolve the declaration/expression ambiguity more quickly. */
11360 if (!function_definition_allowed_p
11361 && !decl_specifiers.any_specifiers_p)
11363 cp_parser_error (parser, "expected declaration");
11364 goto done;
11367 /* If the next two tokens are both identifiers, the code is
11368 erroneous. The usual cause of this situation is code like:
11370 T t;
11372 where "T" should name a type -- but does not. */
11373 if (!decl_specifiers.any_type_specifiers_p
11374 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11376 /* If parsing tentatively, we should commit; we really are
11377 looking at a declaration. */
11378 cp_parser_commit_to_tentative_parse (parser);
11379 /* Give up. */
11380 goto done;
11383 /* If we have seen at least one decl-specifier, and the next token
11384 is not a parenthesis, then we must be looking at a declaration.
11385 (After "int (" we might be looking at a functional cast.) */
11386 if (decl_specifiers.any_specifiers_p
11387 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11388 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11389 && !cp_parser_error_occurred (parser))
11390 cp_parser_commit_to_tentative_parse (parser);
11392 /* Keep going until we hit the `;' at the end of the simple
11393 declaration. */
11394 saw_declarator = false;
11395 while (cp_lexer_next_token_is_not (parser->lexer,
11396 CPP_SEMICOLON))
11398 cp_token *token;
11399 bool function_definition_p;
11400 tree decl;
11402 if (saw_declarator)
11404 /* If we are processing next declarator, coma is expected */
11405 token = cp_lexer_peek_token (parser->lexer);
11406 gcc_assert (token->type == CPP_COMMA);
11407 cp_lexer_consume_token (parser->lexer);
11408 if (maybe_range_for_decl)
11409 *maybe_range_for_decl = error_mark_node;
11411 else
11412 saw_declarator = true;
11414 /* Parse the init-declarator. */
11415 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11416 /*checks=*/NULL,
11417 function_definition_allowed_p,
11418 /*member_p=*/false,
11419 declares_class_or_enum,
11420 &function_definition_p,
11421 maybe_range_for_decl);
11422 /* If an error occurred while parsing tentatively, exit quickly.
11423 (That usually happens when in the body of a function; each
11424 statement is treated as a declaration-statement until proven
11425 otherwise.) */
11426 if (cp_parser_error_occurred (parser))
11427 goto done;
11428 /* Handle function definitions specially. */
11429 if (function_definition_p)
11431 /* If the next token is a `,', then we are probably
11432 processing something like:
11434 void f() {}, *p;
11436 which is erroneous. */
11437 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11439 cp_token *token = cp_lexer_peek_token (parser->lexer);
11440 error_at (token->location,
11441 "mixing"
11442 " declarations and function-definitions is forbidden");
11444 /* Otherwise, we're done with the list of declarators. */
11445 else
11447 pop_deferring_access_checks ();
11448 return;
11451 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11452 *maybe_range_for_decl = decl;
11453 /* The next token should be either a `,' or a `;'. */
11454 token = cp_lexer_peek_token (parser->lexer);
11455 /* If it's a `,', there are more declarators to come. */
11456 if (token->type == CPP_COMMA)
11457 /* will be consumed next time around */;
11458 /* If it's a `;', we are done. */
11459 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11460 break;
11461 /* Anything else is an error. */
11462 else
11464 /* If we have already issued an error message we don't need
11465 to issue another one. */
11466 if (decl != error_mark_node
11467 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11468 cp_parser_error (parser, "expected %<,%> or %<;%>");
11469 /* Skip tokens until we reach the end of the statement. */
11470 cp_parser_skip_to_end_of_statement (parser);
11471 /* If the next token is now a `;', consume it. */
11472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11473 cp_lexer_consume_token (parser->lexer);
11474 goto done;
11476 /* After the first time around, a function-definition is not
11477 allowed -- even if it was OK at first. For example:
11479 int i, f() {}
11481 is not valid. */
11482 function_definition_allowed_p = false;
11485 /* Issue an error message if no declarators are present, and the
11486 decl-specifier-seq does not itself declare a class or
11487 enumeration: [dcl.dcl]/3. */
11488 if (!saw_declarator)
11490 if (cp_parser_declares_only_class_p (parser))
11492 if (!declares_class_or_enum
11493 && decl_specifiers.type
11494 && OVERLOAD_TYPE_P (decl_specifiers.type))
11495 /* Ensure an error is issued anyway when finish_decltype_type,
11496 called via cp_parser_decl_specifier_seq, returns a class or
11497 an enumeration (c++/51786). */
11498 decl_specifiers.type = NULL_TREE;
11499 shadow_tag (&decl_specifiers);
11501 /* Perform any deferred access checks. */
11502 perform_deferred_access_checks (tf_warning_or_error);
11505 /* Consume the `;'. */
11506 if (!maybe_range_for_decl)
11507 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11509 done:
11510 pop_deferring_access_checks ();
11513 /* Parse a decl-specifier-seq.
11515 decl-specifier-seq:
11516 decl-specifier-seq [opt] decl-specifier
11517 decl-specifier attribute-specifier-seq [opt] (C++11)
11519 decl-specifier:
11520 storage-class-specifier
11521 type-specifier
11522 function-specifier
11523 friend
11524 typedef
11526 GNU Extension:
11528 decl-specifier:
11529 attributes
11531 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11533 The parser flags FLAGS is used to control type-specifier parsing.
11535 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11536 flags:
11538 1: one of the decl-specifiers is an elaborated-type-specifier
11539 (i.e., a type declaration)
11540 2: one of the decl-specifiers is an enum-specifier or a
11541 class-specifier (i.e., a type definition)
11545 static void
11546 cp_parser_decl_specifier_seq (cp_parser* parser,
11547 cp_parser_flags flags,
11548 cp_decl_specifier_seq *decl_specs,
11549 int* declares_class_or_enum)
11551 bool constructor_possible_p = !parser->in_declarator_p;
11552 bool found_decl_spec = false;
11553 cp_token *start_token = NULL;
11554 cp_decl_spec ds;
11556 /* Clear DECL_SPECS. */
11557 clear_decl_specs (decl_specs);
11559 /* Assume no class or enumeration type is declared. */
11560 *declares_class_or_enum = 0;
11562 /* Keep reading specifiers until there are no more to read. */
11563 while (true)
11565 bool constructor_p;
11566 cp_token *token;
11567 ds = ds_last;
11569 /* Peek at the next token. */
11570 token = cp_lexer_peek_token (parser->lexer);
11572 /* Save the first token of the decl spec list for error
11573 reporting. */
11574 if (!start_token)
11575 start_token = token;
11576 /* Handle attributes. */
11577 if (cp_next_tokens_can_be_attribute_p (parser))
11579 /* Parse the attributes. */
11580 tree attrs = cp_parser_attributes_opt (parser);
11582 /* In a sequence of declaration specifiers, c++11 attributes
11583 appertain to the type that precede them. In that case
11584 [dcl.spec]/1 says:
11586 The attribute-specifier-seq affects the type only for
11587 the declaration it appears in, not other declarations
11588 involving the same type.
11590 But for now let's force the user to position the
11591 attribute either at the beginning of the declaration or
11592 after the declarator-id, which would clearly mean that it
11593 applies to the declarator. */
11594 if (cxx11_attribute_p (attrs))
11596 if (!found_decl_spec)
11597 /* The c++11 attribute is at the beginning of the
11598 declaration. It appertains to the entity being
11599 declared. */;
11600 else
11602 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11604 /* This is an attribute following a
11605 class-specifier. */
11606 if (decl_specs->type_definition_p)
11607 warn_misplaced_attr_for_class_type (token->location,
11608 decl_specs->type);
11609 attrs = NULL_TREE;
11611 else
11613 decl_specs->std_attributes
11614 = chainon (decl_specs->std_attributes,
11615 attrs);
11616 if (decl_specs->locations[ds_std_attribute] == 0)
11617 decl_specs->locations[ds_std_attribute] = token->location;
11619 continue;
11623 decl_specs->attributes
11624 = chainon (decl_specs->attributes,
11625 attrs);
11626 if (decl_specs->locations[ds_attribute] == 0)
11627 decl_specs->locations[ds_attribute] = token->location;
11628 continue;
11630 /* Assume we will find a decl-specifier keyword. */
11631 found_decl_spec = true;
11632 /* If the next token is an appropriate keyword, we can simply
11633 add it to the list. */
11634 switch (token->keyword)
11636 /* decl-specifier:
11637 friend
11638 constexpr */
11639 case RID_FRIEND:
11640 if (!at_class_scope_p ())
11642 error_at (token->location, "%<friend%> used outside of class");
11643 cp_lexer_purge_token (parser->lexer);
11645 else
11647 ds = ds_friend;
11648 /* Consume the token. */
11649 cp_lexer_consume_token (parser->lexer);
11651 break;
11653 case RID_CONSTEXPR:
11654 ds = ds_constexpr;
11655 cp_lexer_consume_token (parser->lexer);
11656 break;
11658 /* function-specifier:
11659 inline
11660 virtual
11661 explicit */
11662 case RID_INLINE:
11663 case RID_VIRTUAL:
11664 case RID_EXPLICIT:
11665 cp_parser_function_specifier_opt (parser, decl_specs);
11666 break;
11668 /* decl-specifier:
11669 typedef */
11670 case RID_TYPEDEF:
11671 ds = ds_typedef;
11672 /* Consume the token. */
11673 cp_lexer_consume_token (parser->lexer);
11674 /* A constructor declarator cannot appear in a typedef. */
11675 constructor_possible_p = false;
11676 /* The "typedef" keyword can only occur in a declaration; we
11677 may as well commit at this point. */
11678 cp_parser_commit_to_tentative_parse (parser);
11680 if (decl_specs->storage_class != sc_none)
11681 decl_specs->conflicting_specifiers_p = true;
11682 break;
11684 /* storage-class-specifier:
11685 auto
11686 register
11687 static
11688 extern
11689 mutable
11691 GNU Extension:
11692 thread */
11693 case RID_AUTO:
11694 if (cxx_dialect == cxx98)
11696 /* Consume the token. */
11697 cp_lexer_consume_token (parser->lexer);
11699 /* Complain about `auto' as a storage specifier, if
11700 we're complaining about C++0x compatibility. */
11701 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11702 " changes meaning in C++11; please remove it");
11704 /* Set the storage class anyway. */
11705 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11706 token);
11708 else
11709 /* C++0x auto type-specifier. */
11710 found_decl_spec = false;
11711 break;
11713 case RID_REGISTER:
11714 case RID_STATIC:
11715 case RID_EXTERN:
11716 case RID_MUTABLE:
11717 /* Consume the token. */
11718 cp_lexer_consume_token (parser->lexer);
11719 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11720 token);
11721 break;
11722 case RID_THREAD:
11723 /* Consume the token. */
11724 ds = ds_thread;
11725 cp_lexer_consume_token (parser->lexer);
11726 break;
11728 default:
11729 /* We did not yet find a decl-specifier yet. */
11730 found_decl_spec = false;
11731 break;
11734 if (found_decl_spec
11735 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11736 && token->keyword != RID_CONSTEXPR)
11737 error ("decl-specifier invalid in condition");
11739 if (ds != ds_last)
11740 set_and_check_decl_spec_loc (decl_specs, ds, token);
11742 /* Constructors are a special case. The `S' in `S()' is not a
11743 decl-specifier; it is the beginning of the declarator. */
11744 constructor_p
11745 = (!found_decl_spec
11746 && constructor_possible_p
11747 && (cp_parser_constructor_declarator_p
11748 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11750 /* If we don't have a DECL_SPEC yet, then we must be looking at
11751 a type-specifier. */
11752 if (!found_decl_spec && !constructor_p)
11754 int decl_spec_declares_class_or_enum;
11755 bool is_cv_qualifier;
11756 tree type_spec;
11758 type_spec
11759 = cp_parser_type_specifier (parser, flags,
11760 decl_specs,
11761 /*is_declaration=*/true,
11762 &decl_spec_declares_class_or_enum,
11763 &is_cv_qualifier);
11764 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11766 /* If this type-specifier referenced a user-defined type
11767 (a typedef, class-name, etc.), then we can't allow any
11768 more such type-specifiers henceforth.
11770 [dcl.spec]
11772 The longest sequence of decl-specifiers that could
11773 possibly be a type name is taken as the
11774 decl-specifier-seq of a declaration. The sequence shall
11775 be self-consistent as described below.
11777 [dcl.type]
11779 As a general rule, at most one type-specifier is allowed
11780 in the complete decl-specifier-seq of a declaration. The
11781 only exceptions are the following:
11783 -- const or volatile can be combined with any other
11784 type-specifier.
11786 -- signed or unsigned can be combined with char, long,
11787 short, or int.
11789 -- ..
11791 Example:
11793 typedef char* Pc;
11794 void g (const int Pc);
11796 Here, Pc is *not* part of the decl-specifier seq; it's
11797 the declarator. Therefore, once we see a type-specifier
11798 (other than a cv-qualifier), we forbid any additional
11799 user-defined types. We *do* still allow things like `int
11800 int' to be considered a decl-specifier-seq, and issue the
11801 error message later. */
11802 if (type_spec && !is_cv_qualifier)
11803 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11804 /* A constructor declarator cannot follow a type-specifier. */
11805 if (type_spec)
11807 constructor_possible_p = false;
11808 found_decl_spec = true;
11809 if (!is_cv_qualifier)
11810 decl_specs->any_type_specifiers_p = true;
11814 /* If we still do not have a DECL_SPEC, then there are no more
11815 decl-specifiers. */
11816 if (!found_decl_spec)
11817 break;
11819 decl_specs->any_specifiers_p = true;
11820 /* After we see one decl-specifier, further decl-specifiers are
11821 always optional. */
11822 flags |= CP_PARSER_FLAGS_OPTIONAL;
11825 /* Don't allow a friend specifier with a class definition. */
11826 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11827 && (*declares_class_or_enum & 2))
11828 error_at (decl_specs->locations[ds_friend],
11829 "class definition may not be declared a friend");
11832 /* Parse an (optional) storage-class-specifier.
11834 storage-class-specifier:
11835 auto
11836 register
11837 static
11838 extern
11839 mutable
11841 GNU Extension:
11843 storage-class-specifier:
11844 thread
11846 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11848 static tree
11849 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11851 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11853 case RID_AUTO:
11854 if (cxx_dialect != cxx98)
11855 return NULL_TREE;
11856 /* Fall through for C++98. */
11858 case RID_REGISTER:
11859 case RID_STATIC:
11860 case RID_EXTERN:
11861 case RID_MUTABLE:
11862 case RID_THREAD:
11863 /* Consume the token. */
11864 return cp_lexer_consume_token (parser->lexer)->u.value;
11866 default:
11867 return NULL_TREE;
11871 /* Parse an (optional) function-specifier.
11873 function-specifier:
11874 inline
11875 virtual
11876 explicit
11878 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11879 Updates DECL_SPECS, if it is non-NULL. */
11881 static tree
11882 cp_parser_function_specifier_opt (cp_parser* parser,
11883 cp_decl_specifier_seq *decl_specs)
11885 cp_token *token = cp_lexer_peek_token (parser->lexer);
11886 switch (token->keyword)
11888 case RID_INLINE:
11889 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11890 break;
11892 case RID_VIRTUAL:
11893 /* 14.5.2.3 [temp.mem]
11895 A member function template shall not be virtual. */
11896 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11897 error_at (token->location, "templates may not be %<virtual%>");
11898 else
11899 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11900 break;
11902 case RID_EXPLICIT:
11903 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11904 break;
11906 default:
11907 return NULL_TREE;
11910 /* Consume the token. */
11911 return cp_lexer_consume_token (parser->lexer)->u.value;
11914 /* Parse a linkage-specification.
11916 linkage-specification:
11917 extern string-literal { declaration-seq [opt] }
11918 extern string-literal declaration */
11920 static void
11921 cp_parser_linkage_specification (cp_parser* parser)
11923 tree linkage;
11925 /* Look for the `extern' keyword. */
11926 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11928 /* Look for the string-literal. */
11929 linkage = cp_parser_string_literal (parser, false, false);
11931 /* Transform the literal into an identifier. If the literal is a
11932 wide-character string, or contains embedded NULs, then we can't
11933 handle it as the user wants. */
11934 if (strlen (TREE_STRING_POINTER (linkage))
11935 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11937 cp_parser_error (parser, "invalid linkage-specification");
11938 /* Assume C++ linkage. */
11939 linkage = lang_name_cplusplus;
11941 else
11942 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11944 /* We're now using the new linkage. */
11945 push_lang_context (linkage);
11947 /* If the next token is a `{', then we're using the first
11948 production. */
11949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11951 cp_ensure_no_omp_declare_simd (parser);
11953 /* Consume the `{' token. */
11954 cp_lexer_consume_token (parser->lexer);
11955 /* Parse the declarations. */
11956 cp_parser_declaration_seq_opt (parser);
11957 /* Look for the closing `}'. */
11958 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11960 /* Otherwise, there's just one declaration. */
11961 else
11963 bool saved_in_unbraced_linkage_specification_p;
11965 saved_in_unbraced_linkage_specification_p
11966 = parser->in_unbraced_linkage_specification_p;
11967 parser->in_unbraced_linkage_specification_p = true;
11968 cp_parser_declaration (parser);
11969 parser->in_unbraced_linkage_specification_p
11970 = saved_in_unbraced_linkage_specification_p;
11973 /* We're done with the linkage-specification. */
11974 pop_lang_context ();
11977 /* Parse a static_assert-declaration.
11979 static_assert-declaration:
11980 static_assert ( constant-expression , string-literal ) ;
11982 If MEMBER_P, this static_assert is a class member. */
11984 static void
11985 cp_parser_static_assert(cp_parser *parser, bool member_p)
11987 tree condition;
11988 tree message;
11989 cp_token *token;
11990 location_t saved_loc;
11991 bool dummy;
11993 /* Peek at the `static_assert' token so we can keep track of exactly
11994 where the static assertion started. */
11995 token = cp_lexer_peek_token (parser->lexer);
11996 saved_loc = token->location;
11998 /* Look for the `static_assert' keyword. */
11999 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12000 RT_STATIC_ASSERT))
12001 return;
12003 /* We know we are in a static assertion; commit to any tentative
12004 parse. */
12005 if (cp_parser_parsing_tentatively (parser))
12006 cp_parser_commit_to_tentative_parse (parser);
12008 /* Parse the `(' starting the static assertion condition. */
12009 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12011 /* Parse the constant-expression. Allow a non-constant expression
12012 here in order to give better diagnostics in finish_static_assert. */
12013 condition =
12014 cp_parser_constant_expression (parser,
12015 /*allow_non_constant_p=*/true,
12016 /*non_constant_p=*/&dummy);
12018 /* Parse the separating `,'. */
12019 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12021 /* Parse the string-literal message. */
12022 message = cp_parser_string_literal (parser,
12023 /*translate=*/false,
12024 /*wide_ok=*/true);
12026 /* A `)' completes the static assertion. */
12027 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12028 cp_parser_skip_to_closing_parenthesis (parser,
12029 /*recovering=*/true,
12030 /*or_comma=*/false,
12031 /*consume_paren=*/true);
12033 /* A semicolon terminates the declaration. */
12034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12036 /* Complete the static assertion, which may mean either processing
12037 the static assert now or saving it for template instantiation. */
12038 finish_static_assert (condition, message, saved_loc, member_p);
12041 /* Parse the expression in decltype ( expression ). */
12043 static tree
12044 cp_parser_decltype_expr (cp_parser *parser,
12045 bool &id_expression_or_member_access_p)
12047 cp_token *id_expr_start_token;
12048 tree expr;
12050 /* First, try parsing an id-expression. */
12051 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12052 cp_parser_parse_tentatively (parser);
12053 expr = cp_parser_id_expression (parser,
12054 /*template_keyword_p=*/false,
12055 /*check_dependency_p=*/true,
12056 /*template_p=*/NULL,
12057 /*declarator_p=*/false,
12058 /*optional_p=*/false);
12060 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12062 bool non_integral_constant_expression_p = false;
12063 tree id_expression = expr;
12064 cp_id_kind idk;
12065 const char *error_msg;
12067 if (identifier_p (expr))
12068 /* Lookup the name we got back from the id-expression. */
12069 expr = cp_parser_lookup_name_simple (parser, expr,
12070 id_expr_start_token->location);
12072 if (expr
12073 && expr != error_mark_node
12074 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12075 && TREE_CODE (expr) != TYPE_DECL
12076 && (TREE_CODE (expr) != BIT_NOT_EXPR
12077 || !TYPE_P (TREE_OPERAND (expr, 0)))
12078 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12080 /* Complete lookup of the id-expression. */
12081 expr = (finish_id_expression
12082 (id_expression, expr, parser->scope, &idk,
12083 /*integral_constant_expression_p=*/false,
12084 /*allow_non_integral_constant_expression_p=*/true,
12085 &non_integral_constant_expression_p,
12086 /*template_p=*/false,
12087 /*done=*/true,
12088 /*address_p=*/false,
12089 /*template_arg_p=*/false,
12090 &error_msg,
12091 id_expr_start_token->location));
12093 if (expr == error_mark_node)
12094 /* We found an id-expression, but it was something that we
12095 should not have found. This is an error, not something
12096 we can recover from, so note that we found an
12097 id-expression and we'll recover as gracefully as
12098 possible. */
12099 id_expression_or_member_access_p = true;
12102 if (expr
12103 && expr != error_mark_node
12104 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12105 /* We have an id-expression. */
12106 id_expression_or_member_access_p = true;
12109 if (!id_expression_or_member_access_p)
12111 /* Abort the id-expression parse. */
12112 cp_parser_abort_tentative_parse (parser);
12114 /* Parsing tentatively, again. */
12115 cp_parser_parse_tentatively (parser);
12117 /* Parse a class member access. */
12118 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12119 /*cast_p=*/false, /*decltype*/true,
12120 /*member_access_only_p=*/true, NULL);
12122 if (expr
12123 && expr != error_mark_node
12124 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12125 /* We have an id-expression. */
12126 id_expression_or_member_access_p = true;
12129 if (id_expression_or_member_access_p)
12130 /* We have parsed the complete id-expression or member access. */
12131 cp_parser_parse_definitely (parser);
12132 else
12134 /* Abort our attempt to parse an id-expression or member access
12135 expression. */
12136 cp_parser_abort_tentative_parse (parser);
12138 /* Parse a full expression. */
12139 expr = cp_parser_expression (parser, /*cast_p=*/false,
12140 /*decltype*/true, NULL);
12143 return expr;
12146 /* Parse a `decltype' type. Returns the type.
12148 simple-type-specifier:
12149 decltype ( expression )
12150 C++14 proposal:
12151 decltype ( auto ) */
12153 static tree
12154 cp_parser_decltype (cp_parser *parser)
12156 tree expr;
12157 bool id_expression_or_member_access_p = false;
12158 const char *saved_message;
12159 bool saved_integral_constant_expression_p;
12160 bool saved_non_integral_constant_expression_p;
12161 bool saved_greater_than_is_operator_p;
12162 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12164 if (start_token->type == CPP_DECLTYPE)
12166 /* Already parsed. */
12167 cp_lexer_consume_token (parser->lexer);
12168 return start_token->u.value;
12171 /* Look for the `decltype' token. */
12172 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12173 return error_mark_node;
12175 /* Parse the opening `('. */
12176 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12177 return error_mark_node;
12179 /* decltype (auto) */
12180 if (cxx_dialect >= cxx1y
12181 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12183 cp_lexer_consume_token (parser->lexer);
12184 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12185 return error_mark_node;
12186 expr = make_decltype_auto ();
12187 AUTO_IS_DECLTYPE (expr) = true;
12188 goto rewrite;
12191 /* Types cannot be defined in a `decltype' expression. Save away the
12192 old message. */
12193 saved_message = parser->type_definition_forbidden_message;
12195 /* And create the new one. */
12196 parser->type_definition_forbidden_message
12197 = G_("types may not be defined in %<decltype%> expressions");
12199 /* The restrictions on constant-expressions do not apply inside
12200 decltype expressions. */
12201 saved_integral_constant_expression_p
12202 = parser->integral_constant_expression_p;
12203 saved_non_integral_constant_expression_p
12204 = parser->non_integral_constant_expression_p;
12205 parser->integral_constant_expression_p = false;
12207 /* Within a parenthesized expression, a `>' token is always
12208 the greater-than operator. */
12209 saved_greater_than_is_operator_p
12210 = parser->greater_than_is_operator_p;
12211 parser->greater_than_is_operator_p = true;
12213 /* Do not actually evaluate the expression. */
12214 ++cp_unevaluated_operand;
12216 /* Do not warn about problems with the expression. */
12217 ++c_inhibit_evaluation_warnings;
12219 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12221 /* Go back to evaluating expressions. */
12222 --cp_unevaluated_operand;
12223 --c_inhibit_evaluation_warnings;
12225 /* The `>' token might be the end of a template-id or
12226 template-parameter-list now. */
12227 parser->greater_than_is_operator_p
12228 = saved_greater_than_is_operator_p;
12230 /* Restore the old message and the integral constant expression
12231 flags. */
12232 parser->type_definition_forbidden_message = saved_message;
12233 parser->integral_constant_expression_p
12234 = saved_integral_constant_expression_p;
12235 parser->non_integral_constant_expression_p
12236 = saved_non_integral_constant_expression_p;
12238 /* Parse to the closing `)'. */
12239 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12241 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12242 /*consume_paren=*/true);
12243 return error_mark_node;
12246 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12247 tf_warning_or_error);
12249 rewrite:
12250 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12251 it again. */
12252 start_token->type = CPP_DECLTYPE;
12253 start_token->u.value = expr;
12254 start_token->keyword = RID_MAX;
12255 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12257 return expr;
12260 /* Special member functions [gram.special] */
12262 /* Parse a conversion-function-id.
12264 conversion-function-id:
12265 operator conversion-type-id
12267 Returns an IDENTIFIER_NODE representing the operator. */
12269 static tree
12270 cp_parser_conversion_function_id (cp_parser* parser)
12272 tree type;
12273 tree saved_scope;
12274 tree saved_qualifying_scope;
12275 tree saved_object_scope;
12276 tree pushed_scope = NULL_TREE;
12278 /* Look for the `operator' token. */
12279 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12280 return error_mark_node;
12281 /* When we parse the conversion-type-id, the current scope will be
12282 reset. However, we need that information in able to look up the
12283 conversion function later, so we save it here. */
12284 saved_scope = parser->scope;
12285 saved_qualifying_scope = parser->qualifying_scope;
12286 saved_object_scope = parser->object_scope;
12287 /* We must enter the scope of the class so that the names of
12288 entities declared within the class are available in the
12289 conversion-type-id. For example, consider:
12291 struct S {
12292 typedef int I;
12293 operator I();
12296 S::operator I() { ... }
12298 In order to see that `I' is a type-name in the definition, we
12299 must be in the scope of `S'. */
12300 if (saved_scope)
12301 pushed_scope = push_scope (saved_scope);
12302 /* Parse the conversion-type-id. */
12303 type = cp_parser_conversion_type_id (parser);
12304 /* Leave the scope of the class, if any. */
12305 if (pushed_scope)
12306 pop_scope (pushed_scope);
12307 /* Restore the saved scope. */
12308 parser->scope = saved_scope;
12309 parser->qualifying_scope = saved_qualifying_scope;
12310 parser->object_scope = saved_object_scope;
12311 /* If the TYPE is invalid, indicate failure. */
12312 if (type == error_mark_node)
12313 return error_mark_node;
12314 return mangle_conv_op_name_for_type (type);
12317 /* Parse a conversion-type-id:
12319 conversion-type-id:
12320 type-specifier-seq conversion-declarator [opt]
12322 Returns the TYPE specified. */
12324 static tree
12325 cp_parser_conversion_type_id (cp_parser* parser)
12327 tree attributes;
12328 cp_decl_specifier_seq type_specifiers;
12329 cp_declarator *declarator;
12330 tree type_specified;
12331 const char *saved_message;
12333 /* Parse the attributes. */
12334 attributes = cp_parser_attributes_opt (parser);
12336 saved_message = parser->type_definition_forbidden_message;
12337 parser->type_definition_forbidden_message
12338 = G_("types may not be defined in a conversion-type-id");
12340 /* Parse the type-specifiers. */
12341 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12342 /*is_trailing_return=*/false,
12343 &type_specifiers);
12345 parser->type_definition_forbidden_message = saved_message;
12347 /* If that didn't work, stop. */
12348 if (type_specifiers.type == error_mark_node)
12349 return error_mark_node;
12350 /* Parse the conversion-declarator. */
12351 declarator = cp_parser_conversion_declarator_opt (parser);
12353 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12354 /*initialized=*/0, &attributes);
12355 if (attributes)
12356 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12358 /* Don't give this error when parsing tentatively. This happens to
12359 work because we always parse this definitively once. */
12360 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12361 && type_uses_auto (type_specified))
12363 if (cxx_dialect < cxx1y)
12365 error ("invalid use of %<auto%> in conversion operator");
12366 return error_mark_node;
12368 else if (template_parm_scope_p ())
12369 warning (0, "use of %<auto%> in member template "
12370 "conversion operator can never be deduced");
12373 return type_specified;
12376 /* Parse an (optional) conversion-declarator.
12378 conversion-declarator:
12379 ptr-operator conversion-declarator [opt]
12383 static cp_declarator *
12384 cp_parser_conversion_declarator_opt (cp_parser* parser)
12386 enum tree_code code;
12387 tree class_type, std_attributes = NULL_TREE;
12388 cp_cv_quals cv_quals;
12390 /* We don't know if there's a ptr-operator next, or not. */
12391 cp_parser_parse_tentatively (parser);
12392 /* Try the ptr-operator. */
12393 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12394 &std_attributes);
12395 /* If it worked, look for more conversion-declarators. */
12396 if (cp_parser_parse_definitely (parser))
12398 cp_declarator *declarator;
12400 /* Parse another optional declarator. */
12401 declarator = cp_parser_conversion_declarator_opt (parser);
12403 declarator = cp_parser_make_indirect_declarator
12404 (code, class_type, cv_quals, declarator, std_attributes);
12406 return declarator;
12409 return NULL;
12412 /* Parse an (optional) ctor-initializer.
12414 ctor-initializer:
12415 : mem-initializer-list
12417 Returns TRUE iff the ctor-initializer was actually present. */
12419 static bool
12420 cp_parser_ctor_initializer_opt (cp_parser* parser)
12422 /* If the next token is not a `:', then there is no
12423 ctor-initializer. */
12424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12426 /* Do default initialization of any bases and members. */
12427 if (DECL_CONSTRUCTOR_P (current_function_decl))
12428 finish_mem_initializers (NULL_TREE);
12430 return false;
12433 /* Consume the `:' token. */
12434 cp_lexer_consume_token (parser->lexer);
12435 /* And the mem-initializer-list. */
12436 cp_parser_mem_initializer_list (parser);
12438 return true;
12441 /* Parse a mem-initializer-list.
12443 mem-initializer-list:
12444 mem-initializer ... [opt]
12445 mem-initializer ... [opt] , mem-initializer-list */
12447 static void
12448 cp_parser_mem_initializer_list (cp_parser* parser)
12450 tree mem_initializer_list = NULL_TREE;
12451 tree target_ctor = error_mark_node;
12452 cp_token *token = cp_lexer_peek_token (parser->lexer);
12454 /* Let the semantic analysis code know that we are starting the
12455 mem-initializer-list. */
12456 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12457 error_at (token->location,
12458 "only constructors take member initializers");
12460 /* Loop through the list. */
12461 while (true)
12463 tree mem_initializer;
12465 token = cp_lexer_peek_token (parser->lexer);
12466 /* Parse the mem-initializer. */
12467 mem_initializer = cp_parser_mem_initializer (parser);
12468 /* If the next token is a `...', we're expanding member initializers. */
12469 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12471 /* Consume the `...'. */
12472 cp_lexer_consume_token (parser->lexer);
12474 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12475 can be expanded but members cannot. */
12476 if (mem_initializer != error_mark_node
12477 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12479 error_at (token->location,
12480 "cannot expand initializer for member %<%D%>",
12481 TREE_PURPOSE (mem_initializer));
12482 mem_initializer = error_mark_node;
12485 /* Construct the pack expansion type. */
12486 if (mem_initializer != error_mark_node)
12487 mem_initializer = make_pack_expansion (mem_initializer);
12489 if (target_ctor != error_mark_node
12490 && mem_initializer != error_mark_node)
12492 error ("mem-initializer for %qD follows constructor delegation",
12493 TREE_PURPOSE (mem_initializer));
12494 mem_initializer = error_mark_node;
12496 /* Look for a target constructor. */
12497 if (mem_initializer != error_mark_node
12498 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12499 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12501 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12502 if (mem_initializer_list)
12504 error ("constructor delegation follows mem-initializer for %qD",
12505 TREE_PURPOSE (mem_initializer_list));
12506 mem_initializer = error_mark_node;
12508 target_ctor = mem_initializer;
12510 /* Add it to the list, unless it was erroneous. */
12511 if (mem_initializer != error_mark_node)
12513 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12514 mem_initializer_list = mem_initializer;
12516 /* If the next token is not a `,', we're done. */
12517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12518 break;
12519 /* Consume the `,' token. */
12520 cp_lexer_consume_token (parser->lexer);
12523 /* Perform semantic analysis. */
12524 if (DECL_CONSTRUCTOR_P (current_function_decl))
12525 finish_mem_initializers (mem_initializer_list);
12528 /* Parse a mem-initializer.
12530 mem-initializer:
12531 mem-initializer-id ( expression-list [opt] )
12532 mem-initializer-id braced-init-list
12534 GNU extension:
12536 mem-initializer:
12537 ( expression-list [opt] )
12539 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12540 class) or FIELD_DECL (for a non-static data member) to initialize;
12541 the TREE_VALUE is the expression-list. An empty initialization
12542 list is represented by void_list_node. */
12544 static tree
12545 cp_parser_mem_initializer (cp_parser* parser)
12547 tree mem_initializer_id;
12548 tree expression_list;
12549 tree member;
12550 cp_token *token = cp_lexer_peek_token (parser->lexer);
12552 /* Find out what is being initialized. */
12553 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12555 permerror (token->location,
12556 "anachronistic old-style base class initializer");
12557 mem_initializer_id = NULL_TREE;
12559 else
12561 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12562 if (mem_initializer_id == error_mark_node)
12563 return mem_initializer_id;
12565 member = expand_member_init (mem_initializer_id);
12566 if (member && !DECL_P (member))
12567 in_base_initializer = 1;
12569 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12571 bool expr_non_constant_p;
12572 cp_lexer_set_source_position (parser->lexer);
12573 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12574 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12575 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12576 expression_list = build_tree_list (NULL_TREE, expression_list);
12578 else
12580 vec<tree, va_gc> *vec;
12581 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12582 /*cast_p=*/false,
12583 /*allow_expansion_p=*/true,
12584 /*non_constant_p=*/NULL);
12585 if (vec == NULL)
12586 return error_mark_node;
12587 expression_list = build_tree_list_vec (vec);
12588 release_tree_vector (vec);
12591 if (expression_list == error_mark_node)
12592 return error_mark_node;
12593 if (!expression_list)
12594 expression_list = void_type_node;
12596 in_base_initializer = 0;
12598 return member ? build_tree_list (member, expression_list) : error_mark_node;
12601 /* Parse a mem-initializer-id.
12603 mem-initializer-id:
12604 :: [opt] nested-name-specifier [opt] class-name
12605 identifier
12607 Returns a TYPE indicating the class to be initializer for the first
12608 production. Returns an IDENTIFIER_NODE indicating the data member
12609 to be initialized for the second production. */
12611 static tree
12612 cp_parser_mem_initializer_id (cp_parser* parser)
12614 bool global_scope_p;
12615 bool nested_name_specifier_p;
12616 bool template_p = false;
12617 tree id;
12619 cp_token *token = cp_lexer_peek_token (parser->lexer);
12621 /* `typename' is not allowed in this context ([temp.res]). */
12622 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12624 error_at (token->location,
12625 "keyword %<typename%> not allowed in this context (a qualified "
12626 "member initializer is implicitly a type)");
12627 cp_lexer_consume_token (parser->lexer);
12629 /* Look for the optional `::' operator. */
12630 global_scope_p
12631 = (cp_parser_global_scope_opt (parser,
12632 /*current_scope_valid_p=*/false)
12633 != NULL_TREE);
12634 /* Look for the optional nested-name-specifier. The simplest way to
12635 implement:
12637 [temp.res]
12639 The keyword `typename' is not permitted in a base-specifier or
12640 mem-initializer; in these contexts a qualified name that
12641 depends on a template-parameter is implicitly assumed to be a
12642 type name.
12644 is to assume that we have seen the `typename' keyword at this
12645 point. */
12646 nested_name_specifier_p
12647 = (cp_parser_nested_name_specifier_opt (parser,
12648 /*typename_keyword_p=*/true,
12649 /*check_dependency_p=*/true,
12650 /*type_p=*/true,
12651 /*is_declaration=*/true)
12652 != NULL_TREE);
12653 if (nested_name_specifier_p)
12654 template_p = cp_parser_optional_template_keyword (parser);
12655 /* If there is a `::' operator or a nested-name-specifier, then we
12656 are definitely looking for a class-name. */
12657 if (global_scope_p || nested_name_specifier_p)
12658 return cp_parser_class_name (parser,
12659 /*typename_keyword_p=*/true,
12660 /*template_keyword_p=*/template_p,
12661 typename_type,
12662 /*check_dependency_p=*/true,
12663 /*class_head_p=*/false,
12664 /*is_declaration=*/true);
12665 /* Otherwise, we could also be looking for an ordinary identifier. */
12666 cp_parser_parse_tentatively (parser);
12667 /* Try a class-name. */
12668 id = cp_parser_class_name (parser,
12669 /*typename_keyword_p=*/true,
12670 /*template_keyword_p=*/false,
12671 none_type,
12672 /*check_dependency_p=*/true,
12673 /*class_head_p=*/false,
12674 /*is_declaration=*/true);
12675 /* If we found one, we're done. */
12676 if (cp_parser_parse_definitely (parser))
12677 return id;
12678 /* Otherwise, look for an ordinary identifier. */
12679 return cp_parser_identifier (parser);
12682 /* Overloading [gram.over] */
12684 /* Parse an operator-function-id.
12686 operator-function-id:
12687 operator operator
12689 Returns an IDENTIFIER_NODE for the operator which is a
12690 human-readable spelling of the identifier, e.g., `operator +'. */
12692 static tree
12693 cp_parser_operator_function_id (cp_parser* parser)
12695 /* Look for the `operator' keyword. */
12696 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12697 return error_mark_node;
12698 /* And then the name of the operator itself. */
12699 return cp_parser_operator (parser);
12702 /* Return an identifier node for a user-defined literal operator.
12703 The suffix identifier is chained to the operator name identifier. */
12705 static tree
12706 cp_literal_operator_id (const char* name)
12708 tree identifier;
12709 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12710 + strlen (name) + 10);
12711 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12712 identifier = get_identifier (buffer);
12714 return identifier;
12717 /* Parse an operator.
12719 operator:
12720 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12721 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12722 || ++ -- , ->* -> () []
12724 GNU Extensions:
12726 operator:
12727 <? >? <?= >?=
12729 Returns an IDENTIFIER_NODE for the operator which is a
12730 human-readable spelling of the identifier, e.g., `operator +'. */
12732 static tree
12733 cp_parser_operator (cp_parser* parser)
12735 tree id = NULL_TREE;
12736 cp_token *token;
12737 bool utf8 = false;
12739 /* Peek at the next token. */
12740 token = cp_lexer_peek_token (parser->lexer);
12741 /* Figure out which operator we have. */
12742 switch (token->type)
12744 case CPP_KEYWORD:
12746 enum tree_code op;
12748 /* The keyword should be either `new' or `delete'. */
12749 if (token->keyword == RID_NEW)
12750 op = NEW_EXPR;
12751 else if (token->keyword == RID_DELETE)
12752 op = DELETE_EXPR;
12753 else
12754 break;
12756 /* Consume the `new' or `delete' token. */
12757 cp_lexer_consume_token (parser->lexer);
12759 /* Peek at the next token. */
12760 token = cp_lexer_peek_token (parser->lexer);
12761 /* If it's a `[' token then this is the array variant of the
12762 operator. */
12763 if (token->type == CPP_OPEN_SQUARE)
12765 /* Consume the `[' token. */
12766 cp_lexer_consume_token (parser->lexer);
12767 /* Look for the `]' token. */
12768 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12769 id = ansi_opname (op == NEW_EXPR
12770 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12772 /* Otherwise, we have the non-array variant. */
12773 else
12774 id = ansi_opname (op);
12776 return id;
12779 case CPP_PLUS:
12780 id = ansi_opname (PLUS_EXPR);
12781 break;
12783 case CPP_MINUS:
12784 id = ansi_opname (MINUS_EXPR);
12785 break;
12787 case CPP_MULT:
12788 id = ansi_opname (MULT_EXPR);
12789 break;
12791 case CPP_DIV:
12792 id = ansi_opname (TRUNC_DIV_EXPR);
12793 break;
12795 case CPP_MOD:
12796 id = ansi_opname (TRUNC_MOD_EXPR);
12797 break;
12799 case CPP_XOR:
12800 id = ansi_opname (BIT_XOR_EXPR);
12801 break;
12803 case CPP_AND:
12804 id = ansi_opname (BIT_AND_EXPR);
12805 break;
12807 case CPP_OR:
12808 id = ansi_opname (BIT_IOR_EXPR);
12809 break;
12811 case CPP_COMPL:
12812 id = ansi_opname (BIT_NOT_EXPR);
12813 break;
12815 case CPP_NOT:
12816 id = ansi_opname (TRUTH_NOT_EXPR);
12817 break;
12819 case CPP_EQ:
12820 id = ansi_assopname (NOP_EXPR);
12821 break;
12823 case CPP_LESS:
12824 id = ansi_opname (LT_EXPR);
12825 break;
12827 case CPP_GREATER:
12828 id = ansi_opname (GT_EXPR);
12829 break;
12831 case CPP_PLUS_EQ:
12832 id = ansi_assopname (PLUS_EXPR);
12833 break;
12835 case CPP_MINUS_EQ:
12836 id = ansi_assopname (MINUS_EXPR);
12837 break;
12839 case CPP_MULT_EQ:
12840 id = ansi_assopname (MULT_EXPR);
12841 break;
12843 case CPP_DIV_EQ:
12844 id = ansi_assopname (TRUNC_DIV_EXPR);
12845 break;
12847 case CPP_MOD_EQ:
12848 id = ansi_assopname (TRUNC_MOD_EXPR);
12849 break;
12851 case CPP_XOR_EQ:
12852 id = ansi_assopname (BIT_XOR_EXPR);
12853 break;
12855 case CPP_AND_EQ:
12856 id = ansi_assopname (BIT_AND_EXPR);
12857 break;
12859 case CPP_OR_EQ:
12860 id = ansi_assopname (BIT_IOR_EXPR);
12861 break;
12863 case CPP_LSHIFT:
12864 id = ansi_opname (LSHIFT_EXPR);
12865 break;
12867 case CPP_RSHIFT:
12868 id = ansi_opname (RSHIFT_EXPR);
12869 break;
12871 case CPP_LSHIFT_EQ:
12872 id = ansi_assopname (LSHIFT_EXPR);
12873 break;
12875 case CPP_RSHIFT_EQ:
12876 id = ansi_assopname (RSHIFT_EXPR);
12877 break;
12879 case CPP_EQ_EQ:
12880 id = ansi_opname (EQ_EXPR);
12881 break;
12883 case CPP_NOT_EQ:
12884 id = ansi_opname (NE_EXPR);
12885 break;
12887 case CPP_LESS_EQ:
12888 id = ansi_opname (LE_EXPR);
12889 break;
12891 case CPP_GREATER_EQ:
12892 id = ansi_opname (GE_EXPR);
12893 break;
12895 case CPP_AND_AND:
12896 id = ansi_opname (TRUTH_ANDIF_EXPR);
12897 break;
12899 case CPP_OR_OR:
12900 id = ansi_opname (TRUTH_ORIF_EXPR);
12901 break;
12903 case CPP_PLUS_PLUS:
12904 id = ansi_opname (POSTINCREMENT_EXPR);
12905 break;
12907 case CPP_MINUS_MINUS:
12908 id = ansi_opname (PREDECREMENT_EXPR);
12909 break;
12911 case CPP_COMMA:
12912 id = ansi_opname (COMPOUND_EXPR);
12913 break;
12915 case CPP_DEREF_STAR:
12916 id = ansi_opname (MEMBER_REF);
12917 break;
12919 case CPP_DEREF:
12920 id = ansi_opname (COMPONENT_REF);
12921 break;
12923 case CPP_OPEN_PAREN:
12924 /* Consume the `('. */
12925 cp_lexer_consume_token (parser->lexer);
12926 /* Look for the matching `)'. */
12927 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12928 return ansi_opname (CALL_EXPR);
12930 case CPP_OPEN_SQUARE:
12931 /* Consume the `['. */
12932 cp_lexer_consume_token (parser->lexer);
12933 /* Look for the matching `]'. */
12934 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12935 return ansi_opname (ARRAY_REF);
12937 case CPP_UTF8STRING:
12938 case CPP_UTF8STRING_USERDEF:
12939 utf8 = true;
12940 case CPP_STRING:
12941 case CPP_WSTRING:
12942 case CPP_STRING16:
12943 case CPP_STRING32:
12944 case CPP_STRING_USERDEF:
12945 case CPP_WSTRING_USERDEF:
12946 case CPP_STRING16_USERDEF:
12947 case CPP_STRING32_USERDEF:
12949 tree str, string_tree;
12950 int sz, len;
12952 if (cxx_dialect == cxx98)
12953 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12955 /* Consume the string. */
12956 str = cp_parser_string_literal (parser, /*translate=*/true,
12957 /*wide_ok=*/true, /*lookup_udlit=*/false);
12958 if (str == error_mark_node)
12959 return error_mark_node;
12960 else if (TREE_CODE (str) == USERDEF_LITERAL)
12962 string_tree = USERDEF_LITERAL_VALUE (str);
12963 id = USERDEF_LITERAL_SUFFIX_ID (str);
12965 else
12967 string_tree = str;
12968 /* Look for the suffix identifier. */
12969 token = cp_lexer_peek_token (parser->lexer);
12970 if (token->type == CPP_NAME)
12971 id = cp_parser_identifier (parser);
12972 else if (token->type == CPP_KEYWORD)
12974 error ("unexpected keyword;"
12975 " remove space between quotes and suffix identifier");
12976 return error_mark_node;
12978 else
12980 error ("expected suffix identifier");
12981 return error_mark_node;
12984 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
12985 (TREE_TYPE (TREE_TYPE (string_tree))));
12986 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
12987 if (len != 0)
12989 error ("expected empty string after %<operator%> keyword");
12990 return error_mark_node;
12992 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
12993 != char_type_node)
12995 error ("invalid encoding prefix in literal operator");
12996 return error_mark_node;
12998 if (id != error_mark_node)
13000 const char *name = IDENTIFIER_POINTER (id);
13001 id = cp_literal_operator_id (name);
13003 return id;
13006 default:
13007 /* Anything else is an error. */
13008 break;
13011 /* If we have selected an identifier, we need to consume the
13012 operator token. */
13013 if (id)
13014 cp_lexer_consume_token (parser->lexer);
13015 /* Otherwise, no valid operator name was present. */
13016 else
13018 cp_parser_error (parser, "expected operator");
13019 id = error_mark_node;
13022 return id;
13025 /* Parse a template-declaration.
13027 template-declaration:
13028 export [opt] template < template-parameter-list > declaration
13030 If MEMBER_P is TRUE, this template-declaration occurs within a
13031 class-specifier.
13033 The grammar rule given by the standard isn't correct. What
13034 is really meant is:
13036 template-declaration:
13037 export [opt] template-parameter-list-seq
13038 decl-specifier-seq [opt] init-declarator [opt] ;
13039 export [opt] template-parameter-list-seq
13040 function-definition
13042 template-parameter-list-seq:
13043 template-parameter-list-seq [opt]
13044 template < template-parameter-list > */
13046 static void
13047 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13049 /* Check for `export'. */
13050 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13052 /* Consume the `export' token. */
13053 cp_lexer_consume_token (parser->lexer);
13054 /* Warn that we do not support `export'. */
13055 warning (0, "keyword %<export%> not implemented, and will be ignored");
13058 cp_parser_template_declaration_after_export (parser, member_p);
13061 /* Parse a template-parameter-list.
13063 template-parameter-list:
13064 template-parameter
13065 template-parameter-list , template-parameter
13067 Returns a TREE_LIST. Each node represents a template parameter.
13068 The nodes are connected via their TREE_CHAINs. */
13070 static tree
13071 cp_parser_template_parameter_list (cp_parser* parser)
13073 tree parameter_list = NULL_TREE;
13075 begin_template_parm_list ();
13077 /* The loop below parses the template parms. We first need to know
13078 the total number of template parms to be able to compute proper
13079 canonical types of each dependent type. So after the loop, when
13080 we know the total number of template parms,
13081 end_template_parm_list computes the proper canonical types and
13082 fixes up the dependent types accordingly. */
13083 while (true)
13085 tree parameter;
13086 bool is_non_type;
13087 bool is_parameter_pack;
13088 location_t parm_loc;
13090 /* Parse the template-parameter. */
13091 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13092 parameter = cp_parser_template_parameter (parser,
13093 &is_non_type,
13094 &is_parameter_pack);
13095 /* Add it to the list. */
13096 if (parameter != error_mark_node)
13097 parameter_list = process_template_parm (parameter_list,
13098 parm_loc,
13099 parameter,
13100 is_non_type,
13101 is_parameter_pack);
13102 else
13104 tree err_parm = build_tree_list (parameter, parameter);
13105 parameter_list = chainon (parameter_list, err_parm);
13108 /* If the next token is not a `,', we're done. */
13109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13110 break;
13111 /* Otherwise, consume the `,' token. */
13112 cp_lexer_consume_token (parser->lexer);
13115 return end_template_parm_list (parameter_list);
13118 /* Parse a template-parameter.
13120 template-parameter:
13121 type-parameter
13122 parameter-declaration
13124 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13125 the parameter. The TREE_PURPOSE is the default value, if any.
13126 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13127 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13128 set to true iff this parameter is a parameter pack. */
13130 static tree
13131 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13132 bool *is_parameter_pack)
13134 cp_token *token;
13135 cp_parameter_declarator *parameter_declarator;
13136 cp_declarator *id_declarator;
13137 tree parm;
13139 /* Assume it is a type parameter or a template parameter. */
13140 *is_non_type = false;
13141 /* Assume it not a parameter pack. */
13142 *is_parameter_pack = false;
13143 /* Peek at the next token. */
13144 token = cp_lexer_peek_token (parser->lexer);
13145 /* If it is `class' or `template', we have a type-parameter. */
13146 if (token->keyword == RID_TEMPLATE)
13147 return cp_parser_type_parameter (parser, is_parameter_pack);
13148 /* If it is `class' or `typename' we do not know yet whether it is a
13149 type parameter or a non-type parameter. Consider:
13151 template <typename T, typename T::X X> ...
13155 template <class C, class D*> ...
13157 Here, the first parameter is a type parameter, and the second is
13158 a non-type parameter. We can tell by looking at the token after
13159 the identifier -- if it is a `,', `=', or `>' then we have a type
13160 parameter. */
13161 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13163 /* Peek at the token after `class' or `typename'. */
13164 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13165 /* If it's an ellipsis, we have a template type parameter
13166 pack. */
13167 if (token->type == CPP_ELLIPSIS)
13168 return cp_parser_type_parameter (parser, is_parameter_pack);
13169 /* If it's an identifier, skip it. */
13170 if (token->type == CPP_NAME)
13171 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13172 /* Now, see if the token looks like the end of a template
13173 parameter. */
13174 if (token->type == CPP_COMMA
13175 || token->type == CPP_EQ
13176 || token->type == CPP_GREATER)
13177 return cp_parser_type_parameter (parser, is_parameter_pack);
13180 /* Otherwise, it is a non-type parameter.
13182 [temp.param]
13184 When parsing a default template-argument for a non-type
13185 template-parameter, the first non-nested `>' is taken as the end
13186 of the template parameter-list rather than a greater-than
13187 operator. */
13188 *is_non_type = true;
13189 parameter_declarator
13190 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13191 /*parenthesized_p=*/NULL);
13193 if (!parameter_declarator)
13194 return error_mark_node;
13196 /* If the parameter declaration is marked as a parameter pack, set
13197 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13198 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13199 grokdeclarator. */
13200 if (parameter_declarator->declarator
13201 && parameter_declarator->declarator->parameter_pack_p)
13203 *is_parameter_pack = true;
13204 parameter_declarator->declarator->parameter_pack_p = false;
13207 if (parameter_declarator->default_argument)
13209 /* Can happen in some cases of erroneous input (c++/34892). */
13210 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13211 /* Consume the `...' for better error recovery. */
13212 cp_lexer_consume_token (parser->lexer);
13214 /* If the next token is an ellipsis, and we don't already have it
13215 marked as a parameter pack, then we have a parameter pack (that
13216 has no declarator). */
13217 else if (!*is_parameter_pack
13218 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13219 && (declarator_can_be_parameter_pack
13220 (parameter_declarator->declarator)))
13222 /* Consume the `...'. */
13223 cp_lexer_consume_token (parser->lexer);
13224 maybe_warn_variadic_templates ();
13226 *is_parameter_pack = true;
13228 /* We might end up with a pack expansion as the type of the non-type
13229 template parameter, in which case this is a non-type template
13230 parameter pack. */
13231 else if (parameter_declarator->decl_specifiers.type
13232 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13234 *is_parameter_pack = true;
13235 parameter_declarator->decl_specifiers.type =
13236 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13239 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13241 /* Parameter packs cannot have default arguments. However, a
13242 user may try to do so, so we'll parse them and give an
13243 appropriate diagnostic here. */
13245 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13247 /* Find the name of the parameter pack. */
13248 id_declarator = parameter_declarator->declarator;
13249 while (id_declarator && id_declarator->kind != cdk_id)
13250 id_declarator = id_declarator->declarator;
13252 if (id_declarator && id_declarator->kind == cdk_id)
13253 error_at (start_token->location,
13254 "template parameter pack %qD cannot have a default argument",
13255 id_declarator->u.id.unqualified_name);
13256 else
13257 error_at (start_token->location,
13258 "template parameter pack cannot have a default argument");
13260 /* Parse the default argument, but throw away the result. */
13261 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13264 parm = grokdeclarator (parameter_declarator->declarator,
13265 &parameter_declarator->decl_specifiers,
13266 TPARM, /*initialized=*/0,
13267 /*attrlist=*/NULL);
13268 if (parm == error_mark_node)
13269 return error_mark_node;
13271 return build_tree_list (parameter_declarator->default_argument, parm);
13274 /* Parse a type-parameter.
13276 type-parameter:
13277 class identifier [opt]
13278 class identifier [opt] = type-id
13279 typename identifier [opt]
13280 typename identifier [opt] = type-id
13281 template < template-parameter-list > class identifier [opt]
13282 template < template-parameter-list > class identifier [opt]
13283 = id-expression
13285 GNU Extension (variadic templates):
13287 type-parameter:
13288 class ... identifier [opt]
13289 typename ... identifier [opt]
13291 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13292 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13293 the declaration of the parameter.
13295 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13297 static tree
13298 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13300 cp_token *token;
13301 tree parameter;
13303 /* Look for a keyword to tell us what kind of parameter this is. */
13304 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13305 if (!token)
13306 return error_mark_node;
13308 switch (token->keyword)
13310 case RID_CLASS:
13311 case RID_TYPENAME:
13313 tree identifier;
13314 tree default_argument;
13316 /* If the next token is an ellipsis, we have a template
13317 argument pack. */
13318 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13320 /* Consume the `...' token. */
13321 cp_lexer_consume_token (parser->lexer);
13322 maybe_warn_variadic_templates ();
13324 *is_parameter_pack = true;
13327 /* If the next token is an identifier, then it names the
13328 parameter. */
13329 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13330 identifier = cp_parser_identifier (parser);
13331 else
13332 identifier = NULL_TREE;
13334 /* Create the parameter. */
13335 parameter = finish_template_type_parm (class_type_node, identifier);
13337 /* If the next token is an `=', we have a default argument. */
13338 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13340 /* Consume the `=' token. */
13341 cp_lexer_consume_token (parser->lexer);
13342 /* Parse the default-argument. */
13343 push_deferring_access_checks (dk_no_deferred);
13344 default_argument = cp_parser_type_id (parser);
13346 /* Template parameter packs cannot have default
13347 arguments. */
13348 if (*is_parameter_pack)
13350 if (identifier)
13351 error_at (token->location,
13352 "template parameter pack %qD cannot have a "
13353 "default argument", identifier);
13354 else
13355 error_at (token->location,
13356 "template parameter packs cannot have "
13357 "default arguments");
13358 default_argument = NULL_TREE;
13360 pop_deferring_access_checks ();
13362 else
13363 default_argument = NULL_TREE;
13365 /* Create the combined representation of the parameter and the
13366 default argument. */
13367 parameter = build_tree_list (default_argument, parameter);
13369 break;
13371 case RID_TEMPLATE:
13373 tree identifier;
13374 tree default_argument;
13376 /* Look for the `<'. */
13377 cp_parser_require (parser, CPP_LESS, RT_LESS);
13378 /* Parse the template-parameter-list. */
13379 cp_parser_template_parameter_list (parser);
13380 /* Look for the `>'. */
13381 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13382 /* Look for the `class' or 'typename' keywords. */
13383 cp_parser_type_parameter_key (parser);
13384 /* If the next token is an ellipsis, we have a template
13385 argument pack. */
13386 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13388 /* Consume the `...' token. */
13389 cp_lexer_consume_token (parser->lexer);
13390 maybe_warn_variadic_templates ();
13392 *is_parameter_pack = true;
13394 /* If the next token is an `=', then there is a
13395 default-argument. If the next token is a `>', we are at
13396 the end of the parameter-list. If the next token is a `,',
13397 then we are at the end of this parameter. */
13398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13399 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13400 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13402 identifier = cp_parser_identifier (parser);
13403 /* Treat invalid names as if the parameter were nameless. */
13404 if (identifier == error_mark_node)
13405 identifier = NULL_TREE;
13407 else
13408 identifier = NULL_TREE;
13410 /* Create the template parameter. */
13411 parameter = finish_template_template_parm (class_type_node,
13412 identifier);
13414 /* If the next token is an `=', then there is a
13415 default-argument. */
13416 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13418 bool is_template;
13420 /* Consume the `='. */
13421 cp_lexer_consume_token (parser->lexer);
13422 /* Parse the id-expression. */
13423 push_deferring_access_checks (dk_no_deferred);
13424 /* save token before parsing the id-expression, for error
13425 reporting */
13426 token = cp_lexer_peek_token (parser->lexer);
13427 default_argument
13428 = cp_parser_id_expression (parser,
13429 /*template_keyword_p=*/false,
13430 /*check_dependency_p=*/true,
13431 /*template_p=*/&is_template,
13432 /*declarator_p=*/false,
13433 /*optional_p=*/false);
13434 if (TREE_CODE (default_argument) == TYPE_DECL)
13435 /* If the id-expression was a template-id that refers to
13436 a template-class, we already have the declaration here,
13437 so no further lookup is needed. */
13439 else
13440 /* Look up the name. */
13441 default_argument
13442 = cp_parser_lookup_name (parser, default_argument,
13443 none_type,
13444 /*is_template=*/is_template,
13445 /*is_namespace=*/false,
13446 /*check_dependency=*/true,
13447 /*ambiguous_decls=*/NULL,
13448 token->location);
13449 /* See if the default argument is valid. */
13450 default_argument
13451 = check_template_template_default_arg (default_argument);
13453 /* Template parameter packs cannot have default
13454 arguments. */
13455 if (*is_parameter_pack)
13457 if (identifier)
13458 error_at (token->location,
13459 "template parameter pack %qD cannot "
13460 "have a default argument",
13461 identifier);
13462 else
13463 error_at (token->location, "template parameter packs cannot "
13464 "have default arguments");
13465 default_argument = NULL_TREE;
13467 pop_deferring_access_checks ();
13469 else
13470 default_argument = NULL_TREE;
13472 /* Create the combined representation of the parameter and the
13473 default argument. */
13474 parameter = build_tree_list (default_argument, parameter);
13476 break;
13478 default:
13479 gcc_unreachable ();
13480 break;
13483 return parameter;
13486 /* Parse a template-id.
13488 template-id:
13489 template-name < template-argument-list [opt] >
13491 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13492 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13493 returned. Otherwise, if the template-name names a function, or set
13494 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13495 names a class, returns a TYPE_DECL for the specialization.
13497 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13498 uninstantiated templates. */
13500 static tree
13501 cp_parser_template_id (cp_parser *parser,
13502 bool template_keyword_p,
13503 bool check_dependency_p,
13504 enum tag_types tag_type,
13505 bool is_declaration)
13507 int i;
13508 tree templ;
13509 tree arguments;
13510 tree template_id;
13511 cp_token_position start_of_id = 0;
13512 deferred_access_check *chk;
13513 vec<deferred_access_check, va_gc> *access_check;
13514 cp_token *next_token = NULL, *next_token_2 = NULL;
13515 bool is_identifier;
13517 /* If the next token corresponds to a template-id, there is no need
13518 to reparse it. */
13519 next_token = cp_lexer_peek_token (parser->lexer);
13520 if (next_token->type == CPP_TEMPLATE_ID)
13522 struct tree_check *check_value;
13524 /* Get the stored value. */
13525 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13526 /* Perform any access checks that were deferred. */
13527 access_check = check_value->checks;
13528 if (access_check)
13530 FOR_EACH_VEC_ELT (*access_check, i, chk)
13531 perform_or_defer_access_check (chk->binfo,
13532 chk->decl,
13533 chk->diag_decl,
13534 tf_warning_or_error);
13536 /* Return the stored value. */
13537 return check_value->value;
13540 /* Avoid performing name lookup if there is no possibility of
13541 finding a template-id. */
13542 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13543 || (next_token->type == CPP_NAME
13544 && !cp_parser_nth_token_starts_template_argument_list_p
13545 (parser, 2)))
13547 cp_parser_error (parser, "expected template-id");
13548 return error_mark_node;
13551 /* Remember where the template-id starts. */
13552 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13553 start_of_id = cp_lexer_token_position (parser->lexer, false);
13555 push_deferring_access_checks (dk_deferred);
13557 /* Parse the template-name. */
13558 is_identifier = false;
13559 templ = cp_parser_template_name (parser, template_keyword_p,
13560 check_dependency_p,
13561 is_declaration,
13562 tag_type,
13563 &is_identifier);
13564 if (templ == error_mark_node || is_identifier)
13566 pop_deferring_access_checks ();
13567 return templ;
13570 /* If we find the sequence `[:' after a template-name, it's probably
13571 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13572 parse correctly the argument list. */
13573 next_token = cp_lexer_peek_token (parser->lexer);
13574 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13575 if (next_token->type == CPP_OPEN_SQUARE
13576 && next_token->flags & DIGRAPH
13577 && next_token_2->type == CPP_COLON
13578 && !(next_token_2->flags & PREV_WHITE))
13580 cp_parser_parse_tentatively (parser);
13581 /* Change `:' into `::'. */
13582 next_token_2->type = CPP_SCOPE;
13583 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13584 CPP_LESS. */
13585 cp_lexer_consume_token (parser->lexer);
13587 /* Parse the arguments. */
13588 arguments = cp_parser_enclosed_template_argument_list (parser);
13589 if (!cp_parser_parse_definitely (parser))
13591 /* If we couldn't parse an argument list, then we revert our changes
13592 and return simply an error. Maybe this is not a template-id
13593 after all. */
13594 next_token_2->type = CPP_COLON;
13595 cp_parser_error (parser, "expected %<<%>");
13596 pop_deferring_access_checks ();
13597 return error_mark_node;
13599 /* Otherwise, emit an error about the invalid digraph, but continue
13600 parsing because we got our argument list. */
13601 if (permerror (next_token->location,
13602 "%<<::%> cannot begin a template-argument list"))
13604 static bool hint = false;
13605 inform (next_token->location,
13606 "%<<:%> is an alternate spelling for %<[%>."
13607 " Insert whitespace between %<<%> and %<::%>");
13608 if (!hint && !flag_permissive)
13610 inform (next_token->location, "(if you use %<-fpermissive%> "
13611 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13612 "accept your code)");
13613 hint = true;
13617 else
13619 /* Look for the `<' that starts the template-argument-list. */
13620 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13622 pop_deferring_access_checks ();
13623 return error_mark_node;
13625 /* Parse the arguments. */
13626 arguments = cp_parser_enclosed_template_argument_list (parser);
13629 /* Build a representation of the specialization. */
13630 if (identifier_p (templ))
13631 template_id = build_min_nt_loc (next_token->location,
13632 TEMPLATE_ID_EXPR,
13633 templ, arguments);
13634 else if (DECL_TYPE_TEMPLATE_P (templ)
13635 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13637 bool entering_scope;
13638 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13639 template (rather than some instantiation thereof) only if
13640 is not nested within some other construct. For example, in
13641 "template <typename T> void f(T) { A<T>::", A<T> is just an
13642 instantiation of A. */
13643 entering_scope = (template_parm_scope_p ()
13644 && cp_lexer_next_token_is (parser->lexer,
13645 CPP_SCOPE));
13646 template_id
13647 = finish_template_type (templ, arguments, entering_scope);
13649 else
13651 /* If it's not a class-template or a template-template, it should be
13652 a function-template. */
13653 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13654 || TREE_CODE (templ) == OVERLOAD
13655 || BASELINK_P (templ)));
13657 template_id = lookup_template_function (templ, arguments);
13660 /* If parsing tentatively, replace the sequence of tokens that makes
13661 up the template-id with a CPP_TEMPLATE_ID token. That way,
13662 should we re-parse the token stream, we will not have to repeat
13663 the effort required to do the parse, nor will we issue duplicate
13664 error messages about problems during instantiation of the
13665 template. */
13666 if (start_of_id
13667 /* Don't do this if we had a parse error in a declarator; re-parsing
13668 might succeed if a name changes meaning (60361). */
13669 && !(cp_parser_error_occurred (parser)
13670 && cp_parser_parsing_tentatively (parser)
13671 && parser->in_declarator_p))
13673 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13675 /* Reset the contents of the START_OF_ID token. */
13676 token->type = CPP_TEMPLATE_ID;
13677 /* Retrieve any deferred checks. Do not pop this access checks yet
13678 so the memory will not be reclaimed during token replacing below. */
13679 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13680 token->u.tree_check_value->value = template_id;
13681 token->u.tree_check_value->checks = get_deferred_access_checks ();
13682 token->keyword = RID_MAX;
13684 /* Purge all subsequent tokens. */
13685 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13687 /* ??? Can we actually assume that, if template_id ==
13688 error_mark_node, we will have issued a diagnostic to the
13689 user, as opposed to simply marking the tentative parse as
13690 failed? */
13691 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13692 error_at (token->location, "parse error in template argument list");
13695 pop_to_parent_deferring_access_checks ();
13696 return template_id;
13699 /* Parse a template-name.
13701 template-name:
13702 identifier
13704 The standard should actually say:
13706 template-name:
13707 identifier
13708 operator-function-id
13710 A defect report has been filed about this issue.
13712 A conversion-function-id cannot be a template name because they cannot
13713 be part of a template-id. In fact, looking at this code:
13715 a.operator K<int>()
13717 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13718 It is impossible to call a templated conversion-function-id with an
13719 explicit argument list, since the only allowed template parameter is
13720 the type to which it is converting.
13722 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13723 `template' keyword, in a construction like:
13725 T::template f<3>()
13727 In that case `f' is taken to be a template-name, even though there
13728 is no way of knowing for sure.
13730 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13731 name refers to a set of overloaded functions, at least one of which
13732 is a template, or an IDENTIFIER_NODE with the name of the template,
13733 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13734 names are looked up inside uninstantiated templates. */
13736 static tree
13737 cp_parser_template_name (cp_parser* parser,
13738 bool template_keyword_p,
13739 bool check_dependency_p,
13740 bool is_declaration,
13741 enum tag_types tag_type,
13742 bool *is_identifier)
13744 tree identifier;
13745 tree decl;
13746 tree fns;
13747 cp_token *token = cp_lexer_peek_token (parser->lexer);
13749 /* If the next token is `operator', then we have either an
13750 operator-function-id or a conversion-function-id. */
13751 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13753 /* We don't know whether we're looking at an
13754 operator-function-id or a conversion-function-id. */
13755 cp_parser_parse_tentatively (parser);
13756 /* Try an operator-function-id. */
13757 identifier = cp_parser_operator_function_id (parser);
13758 /* If that didn't work, try a conversion-function-id. */
13759 if (!cp_parser_parse_definitely (parser))
13761 cp_parser_error (parser, "expected template-name");
13762 return error_mark_node;
13765 /* Look for the identifier. */
13766 else
13767 identifier = cp_parser_identifier (parser);
13769 /* If we didn't find an identifier, we don't have a template-id. */
13770 if (identifier == error_mark_node)
13771 return error_mark_node;
13773 /* If the name immediately followed the `template' keyword, then it
13774 is a template-name. However, if the next token is not `<', then
13775 we do not treat it as a template-name, since it is not being used
13776 as part of a template-id. This enables us to handle constructs
13777 like:
13779 template <typename T> struct S { S(); };
13780 template <typename T> S<T>::S();
13782 correctly. We would treat `S' as a template -- if it were `S<T>'
13783 -- but we do not if there is no `<'. */
13785 if (processing_template_decl
13786 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13788 /* In a declaration, in a dependent context, we pretend that the
13789 "template" keyword was present in order to improve error
13790 recovery. For example, given:
13792 template <typename T> void f(T::X<int>);
13794 we want to treat "X<int>" as a template-id. */
13795 if (is_declaration
13796 && !template_keyword_p
13797 && parser->scope && TYPE_P (parser->scope)
13798 && check_dependency_p
13799 && dependent_scope_p (parser->scope)
13800 /* Do not do this for dtors (or ctors), since they never
13801 need the template keyword before their name. */
13802 && !constructor_name_p (identifier, parser->scope))
13804 cp_token_position start = 0;
13806 /* Explain what went wrong. */
13807 error_at (token->location, "non-template %qD used as template",
13808 identifier);
13809 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13810 parser->scope, identifier);
13811 /* If parsing tentatively, find the location of the "<" token. */
13812 if (cp_parser_simulate_error (parser))
13813 start = cp_lexer_token_position (parser->lexer, true);
13814 /* Parse the template arguments so that we can issue error
13815 messages about them. */
13816 cp_lexer_consume_token (parser->lexer);
13817 cp_parser_enclosed_template_argument_list (parser);
13818 /* Skip tokens until we find a good place from which to
13819 continue parsing. */
13820 cp_parser_skip_to_closing_parenthesis (parser,
13821 /*recovering=*/true,
13822 /*or_comma=*/true,
13823 /*consume_paren=*/false);
13824 /* If parsing tentatively, permanently remove the
13825 template argument list. That will prevent duplicate
13826 error messages from being issued about the missing
13827 "template" keyword. */
13828 if (start)
13829 cp_lexer_purge_tokens_after (parser->lexer, start);
13830 if (is_identifier)
13831 *is_identifier = true;
13832 return identifier;
13835 /* If the "template" keyword is present, then there is generally
13836 no point in doing name-lookup, so we just return IDENTIFIER.
13837 But, if the qualifying scope is non-dependent then we can
13838 (and must) do name-lookup normally. */
13839 if (template_keyword_p
13840 && (!parser->scope
13841 || (TYPE_P (parser->scope)
13842 && dependent_type_p (parser->scope))))
13843 return identifier;
13846 /* Look up the name. */
13847 decl = cp_parser_lookup_name (parser, identifier,
13848 tag_type,
13849 /*is_template=*/true,
13850 /*is_namespace=*/false,
13851 check_dependency_p,
13852 /*ambiguous_decls=*/NULL,
13853 token->location);
13855 /* If DECL is a template, then the name was a template-name. */
13856 if (TREE_CODE (decl) == TEMPLATE_DECL)
13858 else
13860 tree fn = NULL_TREE;
13862 /* The standard does not explicitly indicate whether a name that
13863 names a set of overloaded declarations, some of which are
13864 templates, is a template-name. However, such a name should
13865 be a template-name; otherwise, there is no way to form a
13866 template-id for the overloaded templates. */
13867 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13868 if (TREE_CODE (fns) == OVERLOAD)
13869 for (fn = fns; fn; fn = OVL_NEXT (fn))
13870 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13871 break;
13873 if (!fn)
13875 /* The name does not name a template. */
13876 cp_parser_error (parser, "expected template-name");
13877 return error_mark_node;
13881 /* If DECL is dependent, and refers to a function, then just return
13882 its name; we will look it up again during template instantiation. */
13883 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13885 tree scope = ovl_scope (decl);
13886 if (TYPE_P (scope) && dependent_type_p (scope))
13887 return identifier;
13890 return decl;
13893 /* Parse a template-argument-list.
13895 template-argument-list:
13896 template-argument ... [opt]
13897 template-argument-list , template-argument ... [opt]
13899 Returns a TREE_VEC containing the arguments. */
13901 static tree
13902 cp_parser_template_argument_list (cp_parser* parser)
13904 tree fixed_args[10];
13905 unsigned n_args = 0;
13906 unsigned alloced = 10;
13907 tree *arg_ary = fixed_args;
13908 tree vec;
13909 bool saved_in_template_argument_list_p;
13910 bool saved_ice_p;
13911 bool saved_non_ice_p;
13913 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13914 parser->in_template_argument_list_p = true;
13915 /* Even if the template-id appears in an integral
13916 constant-expression, the contents of the argument list do
13917 not. */
13918 saved_ice_p = parser->integral_constant_expression_p;
13919 parser->integral_constant_expression_p = false;
13920 saved_non_ice_p = parser->non_integral_constant_expression_p;
13921 parser->non_integral_constant_expression_p = false;
13923 /* Parse the arguments. */
13926 tree argument;
13928 if (n_args)
13929 /* Consume the comma. */
13930 cp_lexer_consume_token (parser->lexer);
13932 /* Parse the template-argument. */
13933 argument = cp_parser_template_argument (parser);
13935 /* If the next token is an ellipsis, we're expanding a template
13936 argument pack. */
13937 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13939 if (argument == error_mark_node)
13941 cp_token *token = cp_lexer_peek_token (parser->lexer);
13942 error_at (token->location,
13943 "expected parameter pack before %<...%>");
13945 /* Consume the `...' token. */
13946 cp_lexer_consume_token (parser->lexer);
13948 /* Make the argument into a TYPE_PACK_EXPANSION or
13949 EXPR_PACK_EXPANSION. */
13950 argument = make_pack_expansion (argument);
13953 if (n_args == alloced)
13955 alloced *= 2;
13957 if (arg_ary == fixed_args)
13959 arg_ary = XNEWVEC (tree, alloced);
13960 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13962 else
13963 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13965 arg_ary[n_args++] = argument;
13967 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13969 vec = make_tree_vec (n_args);
13971 while (n_args--)
13972 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13974 if (arg_ary != fixed_args)
13975 free (arg_ary);
13976 parser->non_integral_constant_expression_p = saved_non_ice_p;
13977 parser->integral_constant_expression_p = saved_ice_p;
13978 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13979 #ifdef ENABLE_CHECKING
13980 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13981 #endif
13982 return vec;
13985 /* Parse a template-argument.
13987 template-argument:
13988 assignment-expression
13989 type-id
13990 id-expression
13992 The representation is that of an assignment-expression, type-id, or
13993 id-expression -- except that the qualified id-expression is
13994 evaluated, so that the value returned is either a DECL or an
13995 OVERLOAD.
13997 Although the standard says "assignment-expression", it forbids
13998 throw-expressions or assignments in the template argument.
13999 Therefore, we use "conditional-expression" instead. */
14001 static tree
14002 cp_parser_template_argument (cp_parser* parser)
14004 tree argument;
14005 bool template_p;
14006 bool address_p;
14007 bool maybe_type_id = false;
14008 cp_token *token = NULL, *argument_start_token = NULL;
14009 location_t loc = 0;
14010 cp_id_kind idk;
14012 /* There's really no way to know what we're looking at, so we just
14013 try each alternative in order.
14015 [temp.arg]
14017 In a template-argument, an ambiguity between a type-id and an
14018 expression is resolved to a type-id, regardless of the form of
14019 the corresponding template-parameter.
14021 Therefore, we try a type-id first. */
14022 cp_parser_parse_tentatively (parser);
14023 argument = cp_parser_template_type_arg (parser);
14024 /* If there was no error parsing the type-id but the next token is a
14025 '>>', our behavior depends on which dialect of C++ we're
14026 parsing. In C++98, we probably found a typo for '> >'. But there
14027 are type-id which are also valid expressions. For instance:
14029 struct X { int operator >> (int); };
14030 template <int V> struct Foo {};
14031 Foo<X () >> 5> r;
14033 Here 'X()' is a valid type-id of a function type, but the user just
14034 wanted to write the expression "X() >> 5". Thus, we remember that we
14035 found a valid type-id, but we still try to parse the argument as an
14036 expression to see what happens.
14038 In C++0x, the '>>' will be considered two separate '>'
14039 tokens. */
14040 if (!cp_parser_error_occurred (parser)
14041 && cxx_dialect == cxx98
14042 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14044 maybe_type_id = true;
14045 cp_parser_abort_tentative_parse (parser);
14047 else
14049 /* If the next token isn't a `,' or a `>', then this argument wasn't
14050 really finished. This means that the argument is not a valid
14051 type-id. */
14052 if (!cp_parser_next_token_ends_template_argument_p (parser))
14053 cp_parser_error (parser, "expected template-argument");
14054 /* If that worked, we're done. */
14055 if (cp_parser_parse_definitely (parser))
14056 return argument;
14058 /* We're still not sure what the argument will be. */
14059 cp_parser_parse_tentatively (parser);
14060 /* Try a template. */
14061 argument_start_token = cp_lexer_peek_token (parser->lexer);
14062 argument = cp_parser_id_expression (parser,
14063 /*template_keyword_p=*/false,
14064 /*check_dependency_p=*/true,
14065 &template_p,
14066 /*declarator_p=*/false,
14067 /*optional_p=*/false);
14068 /* If the next token isn't a `,' or a `>', then this argument wasn't
14069 really finished. */
14070 if (!cp_parser_next_token_ends_template_argument_p (parser))
14071 cp_parser_error (parser, "expected template-argument");
14072 if (!cp_parser_error_occurred (parser))
14074 /* Figure out what is being referred to. If the id-expression
14075 was for a class template specialization, then we will have a
14076 TYPE_DECL at this point. There is no need to do name lookup
14077 at this point in that case. */
14078 if (TREE_CODE (argument) != TYPE_DECL)
14079 argument = cp_parser_lookup_name (parser, argument,
14080 none_type,
14081 /*is_template=*/template_p,
14082 /*is_namespace=*/false,
14083 /*check_dependency=*/true,
14084 /*ambiguous_decls=*/NULL,
14085 argument_start_token->location);
14086 if (TREE_CODE (argument) != TEMPLATE_DECL
14087 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14088 cp_parser_error (parser, "expected template-name");
14090 if (cp_parser_parse_definitely (parser))
14091 return argument;
14092 /* It must be a non-type argument. There permitted cases are given
14093 in [temp.arg.nontype]:
14095 -- an integral constant-expression of integral or enumeration
14096 type; or
14098 -- the name of a non-type template-parameter; or
14100 -- the name of an object or function with external linkage...
14102 -- the address of an object or function with external linkage...
14104 -- a pointer to member... */
14105 /* Look for a non-type template parameter. */
14106 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14108 cp_parser_parse_tentatively (parser);
14109 argument = cp_parser_primary_expression (parser,
14110 /*address_p=*/false,
14111 /*cast_p=*/false,
14112 /*template_arg_p=*/true,
14113 &idk);
14114 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14115 || !cp_parser_next_token_ends_template_argument_p (parser))
14116 cp_parser_simulate_error (parser);
14117 if (cp_parser_parse_definitely (parser))
14118 return argument;
14121 /* If the next token is "&", the argument must be the address of an
14122 object or function with external linkage. */
14123 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14124 if (address_p)
14126 loc = cp_lexer_peek_token (parser->lexer)->location;
14127 cp_lexer_consume_token (parser->lexer);
14129 /* See if we might have an id-expression. */
14130 token = cp_lexer_peek_token (parser->lexer);
14131 if (token->type == CPP_NAME
14132 || token->keyword == RID_OPERATOR
14133 || token->type == CPP_SCOPE
14134 || token->type == CPP_TEMPLATE_ID
14135 || token->type == CPP_NESTED_NAME_SPECIFIER)
14137 cp_parser_parse_tentatively (parser);
14138 argument = cp_parser_primary_expression (parser,
14139 address_p,
14140 /*cast_p=*/false,
14141 /*template_arg_p=*/true,
14142 &idk);
14143 if (cp_parser_error_occurred (parser)
14144 || !cp_parser_next_token_ends_template_argument_p (parser))
14145 cp_parser_abort_tentative_parse (parser);
14146 else
14148 tree probe;
14150 if (INDIRECT_REF_P (argument))
14152 /* Strip the dereference temporarily. */
14153 gcc_assert (REFERENCE_REF_P (argument));
14154 argument = TREE_OPERAND (argument, 0);
14157 /* If we're in a template, we represent a qualified-id referring
14158 to a static data member as a SCOPE_REF even if the scope isn't
14159 dependent so that we can check access control later. */
14160 probe = argument;
14161 if (TREE_CODE (probe) == SCOPE_REF)
14162 probe = TREE_OPERAND (probe, 1);
14163 if (VAR_P (probe))
14165 /* A variable without external linkage might still be a
14166 valid constant-expression, so no error is issued here
14167 if the external-linkage check fails. */
14168 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14169 cp_parser_simulate_error (parser);
14171 else if (is_overloaded_fn (argument))
14172 /* All overloaded functions are allowed; if the external
14173 linkage test does not pass, an error will be issued
14174 later. */
14176 else if (address_p
14177 && (TREE_CODE (argument) == OFFSET_REF
14178 || TREE_CODE (argument) == SCOPE_REF))
14179 /* A pointer-to-member. */
14181 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14183 else
14184 cp_parser_simulate_error (parser);
14186 if (cp_parser_parse_definitely (parser))
14188 if (address_p)
14189 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14190 tf_warning_or_error);
14191 else
14192 argument = convert_from_reference (argument);
14193 return argument;
14197 /* If the argument started with "&", there are no other valid
14198 alternatives at this point. */
14199 if (address_p)
14201 cp_parser_error (parser, "invalid non-type template argument");
14202 return error_mark_node;
14205 /* If the argument wasn't successfully parsed as a type-id followed
14206 by '>>', the argument can only be a constant expression now.
14207 Otherwise, we try parsing the constant-expression tentatively,
14208 because the argument could really be a type-id. */
14209 if (maybe_type_id)
14210 cp_parser_parse_tentatively (parser);
14211 argument = cp_parser_constant_expression (parser,
14212 /*allow_non_constant_p=*/false,
14213 /*non_constant_p=*/NULL);
14214 if (!maybe_type_id)
14215 return argument;
14216 if (!cp_parser_next_token_ends_template_argument_p (parser))
14217 cp_parser_error (parser, "expected template-argument");
14218 if (cp_parser_parse_definitely (parser))
14219 return argument;
14220 /* We did our best to parse the argument as a non type-id, but that
14221 was the only alternative that matched (albeit with a '>' after
14222 it). We can assume it's just a typo from the user, and a
14223 diagnostic will then be issued. */
14224 return cp_parser_template_type_arg (parser);
14227 /* Parse an explicit-instantiation.
14229 explicit-instantiation:
14230 template declaration
14232 Although the standard says `declaration', what it really means is:
14234 explicit-instantiation:
14235 template decl-specifier-seq [opt] declarator [opt] ;
14237 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14238 supposed to be allowed. A defect report has been filed about this
14239 issue.
14241 GNU Extension:
14243 explicit-instantiation:
14244 storage-class-specifier template
14245 decl-specifier-seq [opt] declarator [opt] ;
14246 function-specifier template
14247 decl-specifier-seq [opt] declarator [opt] ; */
14249 static void
14250 cp_parser_explicit_instantiation (cp_parser* parser)
14252 int declares_class_or_enum;
14253 cp_decl_specifier_seq decl_specifiers;
14254 tree extension_specifier = NULL_TREE;
14256 timevar_push (TV_TEMPLATE_INST);
14258 /* Look for an (optional) storage-class-specifier or
14259 function-specifier. */
14260 if (cp_parser_allow_gnu_extensions_p (parser))
14262 extension_specifier
14263 = cp_parser_storage_class_specifier_opt (parser);
14264 if (!extension_specifier)
14265 extension_specifier
14266 = cp_parser_function_specifier_opt (parser,
14267 /*decl_specs=*/NULL);
14270 /* Look for the `template' keyword. */
14271 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14272 /* Let the front end know that we are processing an explicit
14273 instantiation. */
14274 begin_explicit_instantiation ();
14275 /* [temp.explicit] says that we are supposed to ignore access
14276 control while processing explicit instantiation directives. */
14277 push_deferring_access_checks (dk_no_check);
14278 /* Parse a decl-specifier-seq. */
14279 cp_parser_decl_specifier_seq (parser,
14280 CP_PARSER_FLAGS_OPTIONAL,
14281 &decl_specifiers,
14282 &declares_class_or_enum);
14283 /* If there was exactly one decl-specifier, and it declared a class,
14284 and there's no declarator, then we have an explicit type
14285 instantiation. */
14286 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14288 tree type;
14290 type = check_tag_decl (&decl_specifiers,
14291 /*explicit_type_instantiation_p=*/true);
14292 /* Turn access control back on for names used during
14293 template instantiation. */
14294 pop_deferring_access_checks ();
14295 if (type)
14296 do_type_instantiation (type, extension_specifier,
14297 /*complain=*/tf_error);
14299 else
14301 cp_declarator *declarator;
14302 tree decl;
14304 /* Parse the declarator. */
14305 declarator
14306 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14307 /*ctor_dtor_or_conv_p=*/NULL,
14308 /*parenthesized_p=*/NULL,
14309 /*member_p=*/false,
14310 /*friend_p=*/false);
14311 if (declares_class_or_enum & 2)
14312 cp_parser_check_for_definition_in_return_type (declarator,
14313 decl_specifiers.type,
14314 decl_specifiers.locations[ds_type_spec]);
14315 if (declarator != cp_error_declarator)
14317 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14318 permerror (decl_specifiers.locations[ds_inline],
14319 "explicit instantiation shall not use"
14320 " %<inline%> specifier");
14321 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14322 permerror (decl_specifiers.locations[ds_constexpr],
14323 "explicit instantiation shall not use"
14324 " %<constexpr%> specifier");
14326 decl = grokdeclarator (declarator, &decl_specifiers,
14327 NORMAL, 0, &decl_specifiers.attributes);
14328 /* Turn access control back on for names used during
14329 template instantiation. */
14330 pop_deferring_access_checks ();
14331 /* Do the explicit instantiation. */
14332 do_decl_instantiation (decl, extension_specifier);
14334 else
14336 pop_deferring_access_checks ();
14337 /* Skip the body of the explicit instantiation. */
14338 cp_parser_skip_to_end_of_statement (parser);
14341 /* We're done with the instantiation. */
14342 end_explicit_instantiation ();
14344 cp_parser_consume_semicolon_at_end_of_statement (parser);
14346 timevar_pop (TV_TEMPLATE_INST);
14349 /* Parse an explicit-specialization.
14351 explicit-specialization:
14352 template < > declaration
14354 Although the standard says `declaration', what it really means is:
14356 explicit-specialization:
14357 template <> decl-specifier [opt] init-declarator [opt] ;
14358 template <> function-definition
14359 template <> explicit-specialization
14360 template <> template-declaration */
14362 static void
14363 cp_parser_explicit_specialization (cp_parser* parser)
14365 bool need_lang_pop;
14366 cp_token *token = cp_lexer_peek_token (parser->lexer);
14368 /* Look for the `template' keyword. */
14369 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14370 /* Look for the `<'. */
14371 cp_parser_require (parser, CPP_LESS, RT_LESS);
14372 /* Look for the `>'. */
14373 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14374 /* We have processed another parameter list. */
14375 ++parser->num_template_parameter_lists;
14376 /* [temp]
14378 A template ... explicit specialization ... shall not have C
14379 linkage. */
14380 if (current_lang_name == lang_name_c)
14382 error_at (token->location, "template specialization with C linkage");
14383 /* Give it C++ linkage to avoid confusing other parts of the
14384 front end. */
14385 push_lang_context (lang_name_cplusplus);
14386 need_lang_pop = true;
14388 else
14389 need_lang_pop = false;
14390 /* Let the front end know that we are beginning a specialization. */
14391 if (!begin_specialization ())
14393 end_specialization ();
14394 return;
14397 /* If the next keyword is `template', we need to figure out whether
14398 or not we're looking a template-declaration. */
14399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14401 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14402 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14403 cp_parser_template_declaration_after_export (parser,
14404 /*member_p=*/false);
14405 else
14406 cp_parser_explicit_specialization (parser);
14408 else
14409 /* Parse the dependent declaration. */
14410 cp_parser_single_declaration (parser,
14411 /*checks=*/NULL,
14412 /*member_p=*/false,
14413 /*explicit_specialization_p=*/true,
14414 /*friend_p=*/NULL);
14415 /* We're done with the specialization. */
14416 end_specialization ();
14417 /* For the erroneous case of a template with C linkage, we pushed an
14418 implicit C++ linkage scope; exit that scope now. */
14419 if (need_lang_pop)
14420 pop_lang_context ();
14421 /* We're done with this parameter list. */
14422 --parser->num_template_parameter_lists;
14425 /* Parse a type-specifier.
14427 type-specifier:
14428 simple-type-specifier
14429 class-specifier
14430 enum-specifier
14431 elaborated-type-specifier
14432 cv-qualifier
14434 GNU Extension:
14436 type-specifier:
14437 __complex__
14439 Returns a representation of the type-specifier. For a
14440 class-specifier, enum-specifier, or elaborated-type-specifier, a
14441 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14443 The parser flags FLAGS is used to control type-specifier parsing.
14445 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14446 in a decl-specifier-seq.
14448 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14449 class-specifier, enum-specifier, or elaborated-type-specifier, then
14450 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14451 if a type is declared; 2 if it is defined. Otherwise, it is set to
14452 zero.
14454 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14455 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14456 is set to FALSE. */
14458 static tree
14459 cp_parser_type_specifier (cp_parser* parser,
14460 cp_parser_flags flags,
14461 cp_decl_specifier_seq *decl_specs,
14462 bool is_declaration,
14463 int* declares_class_or_enum,
14464 bool* is_cv_qualifier)
14466 tree type_spec = NULL_TREE;
14467 cp_token *token;
14468 enum rid keyword;
14469 cp_decl_spec ds = ds_last;
14471 /* Assume this type-specifier does not declare a new type. */
14472 if (declares_class_or_enum)
14473 *declares_class_or_enum = 0;
14474 /* And that it does not specify a cv-qualifier. */
14475 if (is_cv_qualifier)
14476 *is_cv_qualifier = false;
14477 /* Peek at the next token. */
14478 token = cp_lexer_peek_token (parser->lexer);
14480 /* If we're looking at a keyword, we can use that to guide the
14481 production we choose. */
14482 keyword = token->keyword;
14483 switch (keyword)
14485 case RID_ENUM:
14486 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14487 goto elaborated_type_specifier;
14489 /* Look for the enum-specifier. */
14490 type_spec = cp_parser_enum_specifier (parser);
14491 /* If that worked, we're done. */
14492 if (type_spec)
14494 if (declares_class_or_enum)
14495 *declares_class_or_enum = 2;
14496 if (decl_specs)
14497 cp_parser_set_decl_spec_type (decl_specs,
14498 type_spec,
14499 token,
14500 /*type_definition_p=*/true);
14501 return type_spec;
14503 else
14504 goto elaborated_type_specifier;
14506 /* Any of these indicate either a class-specifier, or an
14507 elaborated-type-specifier. */
14508 case RID_CLASS:
14509 case RID_STRUCT:
14510 case RID_UNION:
14511 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14512 goto elaborated_type_specifier;
14514 /* Parse tentatively so that we can back up if we don't find a
14515 class-specifier. */
14516 cp_parser_parse_tentatively (parser);
14517 /* Look for the class-specifier. */
14518 type_spec = cp_parser_class_specifier (parser);
14519 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14520 /* If that worked, we're done. */
14521 if (cp_parser_parse_definitely (parser))
14523 if (declares_class_or_enum)
14524 *declares_class_or_enum = 2;
14525 if (decl_specs)
14526 cp_parser_set_decl_spec_type (decl_specs,
14527 type_spec,
14528 token,
14529 /*type_definition_p=*/true);
14530 return type_spec;
14533 /* Fall through. */
14534 elaborated_type_specifier:
14535 /* We're declaring (not defining) a class or enum. */
14536 if (declares_class_or_enum)
14537 *declares_class_or_enum = 1;
14539 /* Fall through. */
14540 case RID_TYPENAME:
14541 /* Look for an elaborated-type-specifier. */
14542 type_spec
14543 = (cp_parser_elaborated_type_specifier
14544 (parser,
14545 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14546 is_declaration));
14547 if (decl_specs)
14548 cp_parser_set_decl_spec_type (decl_specs,
14549 type_spec,
14550 token,
14551 /*type_definition_p=*/false);
14552 return type_spec;
14554 case RID_CONST:
14555 ds = ds_const;
14556 if (is_cv_qualifier)
14557 *is_cv_qualifier = true;
14558 break;
14560 case RID_VOLATILE:
14561 ds = ds_volatile;
14562 if (is_cv_qualifier)
14563 *is_cv_qualifier = true;
14564 break;
14566 case RID_RESTRICT:
14567 ds = ds_restrict;
14568 if (is_cv_qualifier)
14569 *is_cv_qualifier = true;
14570 break;
14572 case RID_COMPLEX:
14573 /* The `__complex__' keyword is a GNU extension. */
14574 ds = ds_complex;
14575 break;
14577 default:
14578 break;
14581 /* Handle simple keywords. */
14582 if (ds != ds_last)
14584 if (decl_specs)
14586 set_and_check_decl_spec_loc (decl_specs, ds, token);
14587 decl_specs->any_specifiers_p = true;
14589 return cp_lexer_consume_token (parser->lexer)->u.value;
14592 /* If we do not already have a type-specifier, assume we are looking
14593 at a simple-type-specifier. */
14594 type_spec = cp_parser_simple_type_specifier (parser,
14595 decl_specs,
14596 flags);
14598 /* If we didn't find a type-specifier, and a type-specifier was not
14599 optional in this context, issue an error message. */
14600 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14602 cp_parser_error (parser, "expected type specifier");
14603 return error_mark_node;
14606 return type_spec;
14609 /* Parse a simple-type-specifier.
14611 simple-type-specifier:
14612 :: [opt] nested-name-specifier [opt] type-name
14613 :: [opt] nested-name-specifier template template-id
14614 char
14615 wchar_t
14616 bool
14617 short
14619 long
14620 signed
14621 unsigned
14622 float
14623 double
14624 void
14626 C++0x Extension:
14628 simple-type-specifier:
14629 auto
14630 decltype ( expression )
14631 char16_t
14632 char32_t
14633 __underlying_type ( type-id )
14635 GNU Extension:
14637 simple-type-specifier:
14638 __int128
14639 __typeof__ unary-expression
14640 __typeof__ ( type-id )
14641 __typeof__ ( type-id ) { initializer-list , [opt] }
14643 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14644 appropriately updated. */
14646 static tree
14647 cp_parser_simple_type_specifier (cp_parser* parser,
14648 cp_decl_specifier_seq *decl_specs,
14649 cp_parser_flags flags)
14651 tree type = NULL_TREE;
14652 cp_token *token;
14654 /* Peek at the next token. */
14655 token = cp_lexer_peek_token (parser->lexer);
14657 /* If we're looking at a keyword, things are easy. */
14658 switch (token->keyword)
14660 case RID_CHAR:
14661 if (decl_specs)
14662 decl_specs->explicit_char_p = true;
14663 type = char_type_node;
14664 break;
14665 case RID_CHAR16:
14666 type = char16_type_node;
14667 break;
14668 case RID_CHAR32:
14669 type = char32_type_node;
14670 break;
14671 case RID_WCHAR:
14672 type = wchar_type_node;
14673 break;
14674 case RID_BOOL:
14675 type = boolean_type_node;
14676 break;
14677 case RID_SHORT:
14678 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14679 type = short_integer_type_node;
14680 break;
14681 case RID_INT:
14682 if (decl_specs)
14683 decl_specs->explicit_int_p = true;
14684 type = integer_type_node;
14685 break;
14686 case RID_INT128:
14687 if (!int128_integer_type_node)
14688 break;
14689 if (decl_specs)
14690 decl_specs->explicit_int128_p = true;
14691 type = int128_integer_type_node;
14692 break;
14693 case RID_LONG:
14694 if (decl_specs)
14695 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14696 type = long_integer_type_node;
14697 break;
14698 case RID_SIGNED:
14699 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14700 type = integer_type_node;
14701 break;
14702 case RID_UNSIGNED:
14703 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14704 type = unsigned_type_node;
14705 break;
14706 case RID_FLOAT:
14707 type = float_type_node;
14708 break;
14709 case RID_DOUBLE:
14710 type = double_type_node;
14711 break;
14712 case RID_VOID:
14713 type = void_type_node;
14714 break;
14716 case RID_AUTO:
14717 maybe_warn_cpp0x (CPP0X_AUTO);
14718 if (parser->auto_is_implicit_function_template_parm_p)
14720 type = synthesize_implicit_template_parm (parser);
14722 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14724 if (cxx_dialect < cxx1y)
14725 pedwarn (location_of (type), 0,
14726 "use of %<auto%> in lambda parameter declaration "
14727 "only available with "
14728 "-std=c++1y or -std=gnu++1y");
14730 else if (cxx_dialect < cxx1y)
14731 pedwarn (location_of (type), 0,
14732 "use of %<auto%> in parameter declaration "
14733 "only available with "
14734 "-std=c++1y or -std=gnu++1y");
14735 else
14736 pedwarn (location_of (type), OPT_Wpedantic,
14737 "ISO C++ forbids use of %<auto%> in parameter "
14738 "declaration");
14740 else
14741 type = make_auto ();
14742 break;
14744 case RID_DECLTYPE:
14745 /* Since DR 743, decltype can either be a simple-type-specifier by
14746 itself or begin a nested-name-specifier. Parsing it will replace
14747 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14748 handling below decide what to do. */
14749 cp_parser_decltype (parser);
14750 cp_lexer_set_token_position (parser->lexer, token);
14751 break;
14753 case RID_TYPEOF:
14754 /* Consume the `typeof' token. */
14755 cp_lexer_consume_token (parser->lexer);
14756 /* Parse the operand to `typeof'. */
14757 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14758 /* If it is not already a TYPE, take its type. */
14759 if (!TYPE_P (type))
14760 type = finish_typeof (type);
14762 if (decl_specs)
14763 cp_parser_set_decl_spec_type (decl_specs, type,
14764 token,
14765 /*type_definition_p=*/false);
14767 return type;
14769 case RID_UNDERLYING_TYPE:
14770 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14771 if (decl_specs)
14772 cp_parser_set_decl_spec_type (decl_specs, type,
14773 token,
14774 /*type_definition_p=*/false);
14776 return type;
14778 case RID_BASES:
14779 case RID_DIRECT_BASES:
14780 type = cp_parser_trait_expr (parser, token->keyword);
14781 if (decl_specs)
14782 cp_parser_set_decl_spec_type (decl_specs, type,
14783 token,
14784 /*type_definition_p=*/false);
14785 return type;
14786 default:
14787 break;
14790 /* If token is an already-parsed decltype not followed by ::,
14791 it's a simple-type-specifier. */
14792 if (token->type == CPP_DECLTYPE
14793 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14795 type = token->u.value;
14796 if (decl_specs)
14797 cp_parser_set_decl_spec_type (decl_specs, type,
14798 token,
14799 /*type_definition_p=*/false);
14800 cp_lexer_consume_token (parser->lexer);
14801 return type;
14804 /* If the type-specifier was for a built-in type, we're done. */
14805 if (type)
14807 /* Record the type. */
14808 if (decl_specs
14809 && (token->keyword != RID_SIGNED
14810 && token->keyword != RID_UNSIGNED
14811 && token->keyword != RID_SHORT
14812 && token->keyword != RID_LONG))
14813 cp_parser_set_decl_spec_type (decl_specs,
14814 type,
14815 token,
14816 /*type_definition_p=*/false);
14817 if (decl_specs)
14818 decl_specs->any_specifiers_p = true;
14820 /* Consume the token. */
14821 cp_lexer_consume_token (parser->lexer);
14823 /* There is no valid C++ program where a non-template type is
14824 followed by a "<". That usually indicates that the user thought
14825 that the type was a template. */
14826 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14827 token->location);
14829 return TYPE_NAME (type);
14832 /* The type-specifier must be a user-defined type. */
14833 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14835 bool qualified_p;
14836 bool global_p;
14838 /* Don't gobble tokens or issue error messages if this is an
14839 optional type-specifier. */
14840 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14841 cp_parser_parse_tentatively (parser);
14843 /* Look for the optional `::' operator. */
14844 global_p
14845 = (cp_parser_global_scope_opt (parser,
14846 /*current_scope_valid_p=*/false)
14847 != NULL_TREE);
14848 /* Look for the nested-name specifier. */
14849 qualified_p
14850 = (cp_parser_nested_name_specifier_opt (parser,
14851 /*typename_keyword_p=*/false,
14852 /*check_dependency_p=*/true,
14853 /*type_p=*/false,
14854 /*is_declaration=*/false)
14855 != NULL_TREE);
14856 token = cp_lexer_peek_token (parser->lexer);
14857 /* If we have seen a nested-name-specifier, and the next token
14858 is `template', then we are using the template-id production. */
14859 if (parser->scope
14860 && cp_parser_optional_template_keyword (parser))
14862 /* Look for the template-id. */
14863 type = cp_parser_template_id (parser,
14864 /*template_keyword_p=*/true,
14865 /*check_dependency_p=*/true,
14866 none_type,
14867 /*is_declaration=*/false);
14868 /* If the template-id did not name a type, we are out of
14869 luck. */
14870 if (TREE_CODE (type) != TYPE_DECL)
14872 cp_parser_error (parser, "expected template-id for type");
14873 type = NULL_TREE;
14876 /* Otherwise, look for a type-name. */
14877 else
14878 type = cp_parser_type_name (parser);
14879 /* Keep track of all name-lookups performed in class scopes. */
14880 if (type
14881 && !global_p
14882 && !qualified_p
14883 && TREE_CODE (type) == TYPE_DECL
14884 && identifier_p (DECL_NAME (type)))
14885 maybe_note_name_used_in_class (DECL_NAME (type), type);
14886 /* If it didn't work out, we don't have a TYPE. */
14887 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14888 && !cp_parser_parse_definitely (parser))
14889 type = NULL_TREE;
14890 if (type && decl_specs)
14891 cp_parser_set_decl_spec_type (decl_specs, type,
14892 token,
14893 /*type_definition_p=*/false);
14896 /* If we didn't get a type-name, issue an error message. */
14897 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14899 cp_parser_error (parser, "expected type-name");
14900 return error_mark_node;
14903 if (type && type != error_mark_node)
14905 /* See if TYPE is an Objective-C type, and if so, parse and
14906 accept any protocol references following it. Do this before
14907 the cp_parser_check_for_invalid_template_id() call, because
14908 Objective-C types can be followed by '<...>' which would
14909 enclose protocol names rather than template arguments, and so
14910 everything is fine. */
14911 if (c_dialect_objc () && !parser->scope
14912 && (objc_is_id (type) || objc_is_class_name (type)))
14914 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14915 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14917 /* Clobber the "unqualified" type previously entered into
14918 DECL_SPECS with the new, improved protocol-qualified version. */
14919 if (decl_specs)
14920 decl_specs->type = qual_type;
14922 return qual_type;
14925 /* There is no valid C++ program where a non-template type is
14926 followed by a "<". That usually indicates that the user
14927 thought that the type was a template. */
14928 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14929 none_type,
14930 token->location);
14933 return type;
14936 /* Parse a type-name.
14938 type-name:
14939 class-name
14940 enum-name
14941 typedef-name
14942 simple-template-id [in c++0x]
14944 enum-name:
14945 identifier
14947 typedef-name:
14948 identifier
14950 Returns a TYPE_DECL for the type. */
14952 static tree
14953 cp_parser_type_name (cp_parser* parser)
14955 tree type_decl;
14957 /* We can't know yet whether it is a class-name or not. */
14958 cp_parser_parse_tentatively (parser);
14959 /* Try a class-name. */
14960 type_decl = cp_parser_class_name (parser,
14961 /*typename_keyword_p=*/false,
14962 /*template_keyword_p=*/false,
14963 none_type,
14964 /*check_dependency_p=*/true,
14965 /*class_head_p=*/false,
14966 /*is_declaration=*/false);
14967 /* If it's not a class-name, keep looking. */
14968 if (!cp_parser_parse_definitely (parser))
14970 if (cxx_dialect < cxx11)
14971 /* It must be a typedef-name or an enum-name. */
14972 return cp_parser_nonclass_name (parser);
14974 cp_parser_parse_tentatively (parser);
14975 /* It is either a simple-template-id representing an
14976 instantiation of an alias template... */
14977 type_decl = cp_parser_template_id (parser,
14978 /*template_keyword_p=*/false,
14979 /*check_dependency_p=*/true,
14980 none_type,
14981 /*is_declaration=*/false);
14982 /* Note that this must be an instantiation of an alias template
14983 because [temp.names]/6 says:
14985 A template-id that names an alias template specialization
14986 is a type-name.
14988 Whereas [temp.names]/7 says:
14990 A simple-template-id that names a class template
14991 specialization is a class-name. */
14992 if (type_decl != NULL_TREE
14993 && TREE_CODE (type_decl) == TYPE_DECL
14994 && TYPE_DECL_ALIAS_P (type_decl))
14995 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14996 else
14997 cp_parser_simulate_error (parser);
14999 if (!cp_parser_parse_definitely (parser))
15000 /* ... Or a typedef-name or an enum-name. */
15001 return cp_parser_nonclass_name (parser);
15004 return type_decl;
15007 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15009 enum-name:
15010 identifier
15012 typedef-name:
15013 identifier
15015 Returns a TYPE_DECL for the type. */
15017 static tree
15018 cp_parser_nonclass_name (cp_parser* parser)
15020 tree type_decl;
15021 tree identifier;
15023 cp_token *token = cp_lexer_peek_token (parser->lexer);
15024 identifier = cp_parser_identifier (parser);
15025 if (identifier == error_mark_node)
15026 return error_mark_node;
15028 /* Look up the type-name. */
15029 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15031 type_decl = strip_using_decl (type_decl);
15033 if (TREE_CODE (type_decl) != TYPE_DECL
15034 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15036 /* See if this is an Objective-C type. */
15037 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15038 tree type = objc_get_protocol_qualified_type (identifier, protos);
15039 if (type)
15040 type_decl = TYPE_NAME (type);
15043 /* Issue an error if we did not find a type-name. */
15044 if (TREE_CODE (type_decl) != TYPE_DECL
15045 /* In Objective-C, we have the complication that class names are
15046 normally type names and start declarations (eg, the
15047 "NSObject" in "NSObject *object;"), but can be used in an
15048 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15049 is an expression. So, a classname followed by a dot is not a
15050 valid type-name. */
15051 || (objc_is_class_name (TREE_TYPE (type_decl))
15052 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15054 if (!cp_parser_simulate_error (parser))
15055 cp_parser_name_lookup_error (parser, identifier, type_decl,
15056 NLE_TYPE, token->location);
15057 return error_mark_node;
15059 /* Remember that the name was used in the definition of the
15060 current class so that we can check later to see if the
15061 meaning would have been different after the class was
15062 entirely defined. */
15063 else if (type_decl != error_mark_node
15064 && !parser->scope)
15065 maybe_note_name_used_in_class (identifier, type_decl);
15067 return type_decl;
15070 /* Parse an elaborated-type-specifier. Note that the grammar given
15071 here incorporates the resolution to DR68.
15073 elaborated-type-specifier:
15074 class-key :: [opt] nested-name-specifier [opt] identifier
15075 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15076 enum-key :: [opt] nested-name-specifier [opt] identifier
15077 typename :: [opt] nested-name-specifier identifier
15078 typename :: [opt] nested-name-specifier template [opt]
15079 template-id
15081 GNU extension:
15083 elaborated-type-specifier:
15084 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15085 class-key attributes :: [opt] nested-name-specifier [opt]
15086 template [opt] template-id
15087 enum attributes :: [opt] nested-name-specifier [opt] identifier
15089 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15090 declared `friend'. If IS_DECLARATION is TRUE, then this
15091 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15092 something is being declared.
15094 Returns the TYPE specified. */
15096 static tree
15097 cp_parser_elaborated_type_specifier (cp_parser* parser,
15098 bool is_friend,
15099 bool is_declaration)
15101 enum tag_types tag_type;
15102 tree identifier;
15103 tree type = NULL_TREE;
15104 tree attributes = NULL_TREE;
15105 tree globalscope;
15106 cp_token *token = NULL;
15108 /* See if we're looking at the `enum' keyword. */
15109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15111 /* Consume the `enum' token. */
15112 cp_lexer_consume_token (parser->lexer);
15113 /* Remember that it's an enumeration type. */
15114 tag_type = enum_type;
15115 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15116 enums) is used here. */
15117 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15118 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15120 pedwarn (input_location, 0, "elaborated-type-specifier "
15121 "for a scoped enum must not use the %<%D%> keyword",
15122 cp_lexer_peek_token (parser->lexer)->u.value);
15123 /* Consume the `struct' or `class' and parse it anyway. */
15124 cp_lexer_consume_token (parser->lexer);
15126 /* Parse the attributes. */
15127 attributes = cp_parser_attributes_opt (parser);
15129 /* Or, it might be `typename'. */
15130 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15131 RID_TYPENAME))
15133 /* Consume the `typename' token. */
15134 cp_lexer_consume_token (parser->lexer);
15135 /* Remember that it's a `typename' type. */
15136 tag_type = typename_type;
15138 /* Otherwise it must be a class-key. */
15139 else
15141 tag_type = cp_parser_class_key (parser);
15142 if (tag_type == none_type)
15143 return error_mark_node;
15144 /* Parse the attributes. */
15145 attributes = cp_parser_attributes_opt (parser);
15148 /* Look for the `::' operator. */
15149 globalscope = cp_parser_global_scope_opt (parser,
15150 /*current_scope_valid_p=*/false);
15151 /* Look for the nested-name-specifier. */
15152 if (tag_type == typename_type && !globalscope)
15154 if (!cp_parser_nested_name_specifier (parser,
15155 /*typename_keyword_p=*/true,
15156 /*check_dependency_p=*/true,
15157 /*type_p=*/true,
15158 is_declaration))
15159 return error_mark_node;
15161 else
15162 /* Even though `typename' is not present, the proposed resolution
15163 to Core Issue 180 says that in `class A<T>::B', `B' should be
15164 considered a type-name, even if `A<T>' is dependent. */
15165 cp_parser_nested_name_specifier_opt (parser,
15166 /*typename_keyword_p=*/true,
15167 /*check_dependency_p=*/true,
15168 /*type_p=*/true,
15169 is_declaration);
15170 /* For everything but enumeration types, consider a template-id.
15171 For an enumeration type, consider only a plain identifier. */
15172 if (tag_type != enum_type)
15174 bool template_p = false;
15175 tree decl;
15177 /* Allow the `template' keyword. */
15178 template_p = cp_parser_optional_template_keyword (parser);
15179 /* If we didn't see `template', we don't know if there's a
15180 template-id or not. */
15181 if (!template_p)
15182 cp_parser_parse_tentatively (parser);
15183 /* Parse the template-id. */
15184 token = cp_lexer_peek_token (parser->lexer);
15185 decl = cp_parser_template_id (parser, template_p,
15186 /*check_dependency_p=*/true,
15187 tag_type,
15188 is_declaration);
15189 /* If we didn't find a template-id, look for an ordinary
15190 identifier. */
15191 if (!template_p && !cp_parser_parse_definitely (parser))
15193 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15194 in effect, then we must assume that, upon instantiation, the
15195 template will correspond to a class. */
15196 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15197 && tag_type == typename_type)
15198 type = make_typename_type (parser->scope, decl,
15199 typename_type,
15200 /*complain=*/tf_error);
15201 /* If the `typename' keyword is in effect and DECL is not a type
15202 decl, then type is non existent. */
15203 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15205 else if (TREE_CODE (decl) == TYPE_DECL)
15206 type = check_elaborated_type_specifier (tag_type, decl,
15207 /*allow_template_p=*/true);
15208 else if (decl == error_mark_node)
15209 type = error_mark_node;
15212 if (!type)
15214 token = cp_lexer_peek_token (parser->lexer);
15215 identifier = cp_parser_identifier (parser);
15217 if (identifier == error_mark_node)
15219 parser->scope = NULL_TREE;
15220 return error_mark_node;
15223 /* For a `typename', we needn't call xref_tag. */
15224 if (tag_type == typename_type
15225 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15226 return cp_parser_make_typename_type (parser, parser->scope,
15227 identifier,
15228 token->location);
15230 /* Template parameter lists apply only if we are not within a
15231 function parameter list. */
15232 bool template_parm_lists_apply
15233 = parser->num_template_parameter_lists;
15234 if (template_parm_lists_apply)
15235 for (cp_binding_level *s = current_binding_level;
15236 s && s->kind != sk_template_parms;
15237 s = s->level_chain)
15238 if (s->kind == sk_function_parms)
15239 template_parm_lists_apply = false;
15241 /* Look up a qualified name in the usual way. */
15242 if (parser->scope)
15244 tree decl;
15245 tree ambiguous_decls;
15247 decl = cp_parser_lookup_name (parser, identifier,
15248 tag_type,
15249 /*is_template=*/false,
15250 /*is_namespace=*/false,
15251 /*check_dependency=*/true,
15252 &ambiguous_decls,
15253 token->location);
15255 /* If the lookup was ambiguous, an error will already have been
15256 issued. */
15257 if (ambiguous_decls)
15258 return error_mark_node;
15260 /* If we are parsing friend declaration, DECL may be a
15261 TEMPLATE_DECL tree node here. However, we need to check
15262 whether this TEMPLATE_DECL results in valid code. Consider
15263 the following example:
15265 namespace N {
15266 template <class T> class C {};
15268 class X {
15269 template <class T> friend class N::C; // #1, valid code
15271 template <class T> class Y {
15272 friend class N::C; // #2, invalid code
15275 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15276 name lookup of `N::C'. We see that friend declaration must
15277 be template for the code to be valid. Note that
15278 processing_template_decl does not work here since it is
15279 always 1 for the above two cases. */
15281 decl = (cp_parser_maybe_treat_template_as_class
15282 (decl, /*tag_name_p=*/is_friend
15283 && template_parm_lists_apply));
15285 if (TREE_CODE (decl) != TYPE_DECL)
15287 cp_parser_diagnose_invalid_type_name (parser,
15288 parser->scope,
15289 identifier,
15290 token->location);
15291 return error_mark_node;
15294 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15296 bool allow_template = (template_parm_lists_apply
15297 || DECL_SELF_REFERENCE_P (decl));
15298 type = check_elaborated_type_specifier (tag_type, decl,
15299 allow_template);
15301 if (type == error_mark_node)
15302 return error_mark_node;
15305 /* Forward declarations of nested types, such as
15307 class C1::C2;
15308 class C1::C2::C3;
15310 are invalid unless all components preceding the final '::'
15311 are complete. If all enclosing types are complete, these
15312 declarations become merely pointless.
15314 Invalid forward declarations of nested types are errors
15315 caught elsewhere in parsing. Those that are pointless arrive
15316 here. */
15318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15319 && !is_friend && !processing_explicit_instantiation)
15320 warning (0, "declaration %qD does not declare anything", decl);
15322 type = TREE_TYPE (decl);
15324 else
15326 /* An elaborated-type-specifier sometimes introduces a new type and
15327 sometimes names an existing type. Normally, the rule is that it
15328 introduces a new type only if there is not an existing type of
15329 the same name already in scope. For example, given:
15331 struct S {};
15332 void f() { struct S s; }
15334 the `struct S' in the body of `f' is the same `struct S' as in
15335 the global scope; the existing definition is used. However, if
15336 there were no global declaration, this would introduce a new
15337 local class named `S'.
15339 An exception to this rule applies to the following code:
15341 namespace N { struct S; }
15343 Here, the elaborated-type-specifier names a new type
15344 unconditionally; even if there is already an `S' in the
15345 containing scope this declaration names a new type.
15346 This exception only applies if the elaborated-type-specifier
15347 forms the complete declaration:
15349 [class.name]
15351 A declaration consisting solely of `class-key identifier ;' is
15352 either a redeclaration of the name in the current scope or a
15353 forward declaration of the identifier as a class name. It
15354 introduces the name into the current scope.
15356 We are in this situation precisely when the next token is a `;'.
15358 An exception to the exception is that a `friend' declaration does
15359 *not* name a new type; i.e., given:
15361 struct S { friend struct T; };
15363 `T' is not a new type in the scope of `S'.
15365 Also, `new struct S' or `sizeof (struct S)' never results in the
15366 definition of a new type; a new type can only be declared in a
15367 declaration context. */
15369 tag_scope ts;
15370 bool template_p;
15372 if (is_friend)
15373 /* Friends have special name lookup rules. */
15374 ts = ts_within_enclosing_non_class;
15375 else if (is_declaration
15376 && cp_lexer_next_token_is (parser->lexer,
15377 CPP_SEMICOLON))
15378 /* This is a `class-key identifier ;' */
15379 ts = ts_current;
15380 else
15381 ts = ts_global;
15383 template_p =
15384 (template_parm_lists_apply
15385 && (cp_parser_next_token_starts_class_definition_p (parser)
15386 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15387 /* An unqualified name was used to reference this type, so
15388 there were no qualifying templates. */
15389 if (template_parm_lists_apply
15390 && !cp_parser_check_template_parameters (parser,
15391 /*num_templates=*/0,
15392 token->location,
15393 /*declarator=*/NULL))
15394 return error_mark_node;
15395 type = xref_tag (tag_type, identifier, ts, template_p);
15399 if (type == error_mark_node)
15400 return error_mark_node;
15402 /* Allow attributes on forward declarations of classes. */
15403 if (attributes)
15405 if (TREE_CODE (type) == TYPENAME_TYPE)
15406 warning (OPT_Wattributes,
15407 "attributes ignored on uninstantiated type");
15408 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15409 && ! processing_explicit_instantiation)
15410 warning (OPT_Wattributes,
15411 "attributes ignored on template instantiation");
15412 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15413 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15414 else
15415 warning (OPT_Wattributes,
15416 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15419 if (tag_type != enum_type)
15421 /* Indicate whether this class was declared as a `class' or as a
15422 `struct'. */
15423 if (TREE_CODE (type) == RECORD_TYPE)
15424 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15425 cp_parser_check_class_key (tag_type, type);
15428 /* A "<" cannot follow an elaborated type specifier. If that
15429 happens, the user was probably trying to form a template-id. */
15430 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15431 token->location);
15433 return type;
15436 /* Parse an enum-specifier.
15438 enum-specifier:
15439 enum-head { enumerator-list [opt] }
15440 enum-head { enumerator-list , } [C++0x]
15442 enum-head:
15443 enum-key identifier [opt] enum-base [opt]
15444 enum-key nested-name-specifier identifier enum-base [opt]
15446 enum-key:
15447 enum
15448 enum class [C++0x]
15449 enum struct [C++0x]
15451 enum-base: [C++0x]
15452 : type-specifier-seq
15454 opaque-enum-specifier:
15455 enum-key identifier enum-base [opt] ;
15457 GNU Extensions:
15458 enum-key attributes[opt] identifier [opt] enum-base [opt]
15459 { enumerator-list [opt] }attributes[opt]
15460 enum-key attributes[opt] identifier [opt] enum-base [opt]
15461 { enumerator-list, }attributes[opt] [C++0x]
15463 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15464 if the token stream isn't an enum-specifier after all. */
15466 static tree
15467 cp_parser_enum_specifier (cp_parser* parser)
15469 tree identifier;
15470 tree type = NULL_TREE;
15471 tree prev_scope;
15472 tree nested_name_specifier = NULL_TREE;
15473 tree attributes;
15474 bool scoped_enum_p = false;
15475 bool has_underlying_type = false;
15476 bool nested_being_defined = false;
15477 bool new_value_list = false;
15478 bool is_new_type = false;
15479 bool is_anonymous = false;
15480 tree underlying_type = NULL_TREE;
15481 cp_token *type_start_token = NULL;
15482 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15484 parser->colon_corrects_to_scope_p = false;
15486 /* Parse tentatively so that we can back up if we don't find a
15487 enum-specifier. */
15488 cp_parser_parse_tentatively (parser);
15490 /* Caller guarantees that the current token is 'enum', an identifier
15491 possibly follows, and the token after that is an opening brace.
15492 If we don't have an identifier, fabricate an anonymous name for
15493 the enumeration being defined. */
15494 cp_lexer_consume_token (parser->lexer);
15496 /* Parse the "class" or "struct", which indicates a scoped
15497 enumeration type in C++0x. */
15498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15499 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15501 if (cxx_dialect < cxx11)
15502 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15504 /* Consume the `struct' or `class' token. */
15505 cp_lexer_consume_token (parser->lexer);
15507 scoped_enum_p = true;
15510 attributes = cp_parser_attributes_opt (parser);
15512 /* Clear the qualification. */
15513 parser->scope = NULL_TREE;
15514 parser->qualifying_scope = NULL_TREE;
15515 parser->object_scope = NULL_TREE;
15517 /* Figure out in what scope the declaration is being placed. */
15518 prev_scope = current_scope ();
15520 type_start_token = cp_lexer_peek_token (parser->lexer);
15522 push_deferring_access_checks (dk_no_check);
15523 nested_name_specifier
15524 = cp_parser_nested_name_specifier_opt (parser,
15525 /*typename_keyword_p=*/true,
15526 /*check_dependency_p=*/false,
15527 /*type_p=*/false,
15528 /*is_declaration=*/false);
15530 if (nested_name_specifier)
15532 tree name;
15534 identifier = cp_parser_identifier (parser);
15535 name = cp_parser_lookup_name (parser, identifier,
15536 enum_type,
15537 /*is_template=*/false,
15538 /*is_namespace=*/false,
15539 /*check_dependency=*/true,
15540 /*ambiguous_decls=*/NULL,
15541 input_location);
15542 if (name && name != error_mark_node)
15544 type = TREE_TYPE (name);
15545 if (TREE_CODE (type) == TYPENAME_TYPE)
15547 /* Are template enums allowed in ISO? */
15548 if (template_parm_scope_p ())
15549 pedwarn (type_start_token->location, OPT_Wpedantic,
15550 "%qD is an enumeration template", name);
15551 /* ignore a typename reference, for it will be solved by name
15552 in start_enum. */
15553 type = NULL_TREE;
15556 else if (nested_name_specifier == error_mark_node)
15557 /* We already issued an error. */;
15558 else
15559 error_at (type_start_token->location,
15560 "%qD is not an enumerator-name", identifier);
15562 else
15564 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15565 identifier = cp_parser_identifier (parser);
15566 else
15568 identifier = make_anon_name ();
15569 is_anonymous = true;
15570 if (scoped_enum_p)
15571 error_at (type_start_token->location,
15572 "anonymous scoped enum is not allowed");
15575 pop_deferring_access_checks ();
15577 /* Check for the `:' that denotes a specified underlying type in C++0x.
15578 Note that a ':' could also indicate a bitfield width, however. */
15579 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15581 cp_decl_specifier_seq type_specifiers;
15583 /* Consume the `:'. */
15584 cp_lexer_consume_token (parser->lexer);
15586 /* Parse the type-specifier-seq. */
15587 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15588 /*is_trailing_return=*/false,
15589 &type_specifiers);
15591 /* At this point this is surely not elaborated type specifier. */
15592 if (!cp_parser_parse_definitely (parser))
15593 return NULL_TREE;
15595 if (cxx_dialect < cxx11)
15596 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15598 has_underlying_type = true;
15600 /* If that didn't work, stop. */
15601 if (type_specifiers.type != error_mark_node)
15603 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15604 /*initialized=*/0, NULL);
15605 if (underlying_type == error_mark_node
15606 || check_for_bare_parameter_packs (underlying_type))
15607 underlying_type = NULL_TREE;
15611 /* Look for the `{' but don't consume it yet. */
15612 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15614 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15616 cp_parser_error (parser, "expected %<{%>");
15617 if (has_underlying_type)
15619 type = NULL_TREE;
15620 goto out;
15623 /* An opaque-enum-specifier must have a ';' here. */
15624 if ((scoped_enum_p || underlying_type)
15625 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15627 cp_parser_error (parser, "expected %<;%> or %<{%>");
15628 if (has_underlying_type)
15630 type = NULL_TREE;
15631 goto out;
15636 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15637 return NULL_TREE;
15639 if (nested_name_specifier)
15641 if (CLASS_TYPE_P (nested_name_specifier))
15643 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15644 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15645 push_scope (nested_name_specifier);
15647 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15649 push_nested_namespace (nested_name_specifier);
15653 /* Issue an error message if type-definitions are forbidden here. */
15654 if (!cp_parser_check_type_definition (parser))
15655 type = error_mark_node;
15656 else
15657 /* Create the new type. We do this before consuming the opening
15658 brace so the enum will be recorded as being on the line of its
15659 tag (or the 'enum' keyword, if there is no tag). */
15660 type = start_enum (identifier, type, underlying_type,
15661 scoped_enum_p, &is_new_type);
15663 /* If the next token is not '{' it is an opaque-enum-specifier or an
15664 elaborated-type-specifier. */
15665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15667 timevar_push (TV_PARSE_ENUM);
15668 if (nested_name_specifier
15669 && nested_name_specifier != error_mark_node)
15671 /* The following catches invalid code such as:
15672 enum class S<int>::E { A, B, C }; */
15673 if (!processing_specialization
15674 && CLASS_TYPE_P (nested_name_specifier)
15675 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15676 error_at (type_start_token->location, "cannot add an enumerator "
15677 "list to a template instantiation");
15679 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15681 error_at (type_start_token->location,
15682 "%<%T::%E%> has not been declared",
15683 TYPE_CONTEXT (nested_name_specifier),
15684 nested_name_specifier);
15685 type = error_mark_node;
15687 /* If that scope does not contain the scope in which the
15688 class was originally declared, the program is invalid. */
15689 else if (prev_scope && !is_ancestor (prev_scope,
15690 nested_name_specifier))
15692 if (at_namespace_scope_p ())
15693 error_at (type_start_token->location,
15694 "declaration of %qD in namespace %qD which does not "
15695 "enclose %qD",
15696 type, prev_scope, nested_name_specifier);
15697 else
15698 error_at (type_start_token->location,
15699 "declaration of %qD in %qD which does not "
15700 "enclose %qD",
15701 type, prev_scope, nested_name_specifier);
15702 type = error_mark_node;
15706 if (scoped_enum_p)
15707 begin_scope (sk_scoped_enum, type);
15709 /* Consume the opening brace. */
15710 cp_lexer_consume_token (parser->lexer);
15712 if (type == error_mark_node)
15713 ; /* Nothing to add */
15714 else if (OPAQUE_ENUM_P (type)
15715 || (cxx_dialect > cxx98 && processing_specialization))
15717 new_value_list = true;
15718 SET_OPAQUE_ENUM_P (type, false);
15719 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15721 else
15723 error_at (type_start_token->location,
15724 "multiple definition of %q#T", type);
15725 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15726 "previous definition here");
15727 type = error_mark_node;
15730 if (type == error_mark_node)
15731 cp_parser_skip_to_end_of_block_or_statement (parser);
15732 /* If the next token is not '}', then there are some enumerators. */
15733 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15735 if (is_anonymous && !scoped_enum_p)
15736 pedwarn (type_start_token->location, OPT_Wpedantic,
15737 "ISO C++ forbids empty anonymous enum");
15739 else
15740 cp_parser_enumerator_list (parser, type);
15742 /* Consume the final '}'. */
15743 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15745 if (scoped_enum_p)
15746 finish_scope ();
15747 timevar_pop (TV_PARSE_ENUM);
15749 else
15751 /* If a ';' follows, then it is an opaque-enum-specifier
15752 and additional restrictions apply. */
15753 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15755 if (is_anonymous)
15756 error_at (type_start_token->location,
15757 "opaque-enum-specifier without name");
15758 else if (nested_name_specifier)
15759 error_at (type_start_token->location,
15760 "opaque-enum-specifier must use a simple identifier");
15764 /* Look for trailing attributes to apply to this enumeration, and
15765 apply them if appropriate. */
15766 if (cp_parser_allow_gnu_extensions_p (parser))
15768 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15769 trailing_attr = chainon (trailing_attr, attributes);
15770 cplus_decl_attributes (&type,
15771 trailing_attr,
15772 (int) ATTR_FLAG_TYPE_IN_PLACE);
15775 /* Finish up the enumeration. */
15776 if (type != error_mark_node)
15778 if (new_value_list)
15779 finish_enum_value_list (type);
15780 if (is_new_type)
15781 finish_enum (type);
15784 if (nested_name_specifier)
15786 if (CLASS_TYPE_P (nested_name_specifier))
15788 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15789 pop_scope (nested_name_specifier);
15791 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15793 pop_nested_namespace (nested_name_specifier);
15796 out:
15797 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15798 return type;
15801 /* Parse an enumerator-list. The enumerators all have the indicated
15802 TYPE.
15804 enumerator-list:
15805 enumerator-definition
15806 enumerator-list , enumerator-definition */
15808 static void
15809 cp_parser_enumerator_list (cp_parser* parser, tree type)
15811 while (true)
15813 /* Parse an enumerator-definition. */
15814 cp_parser_enumerator_definition (parser, type);
15816 /* If the next token is not a ',', we've reached the end of
15817 the list. */
15818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15819 break;
15820 /* Otherwise, consume the `,' and keep going. */
15821 cp_lexer_consume_token (parser->lexer);
15822 /* If the next token is a `}', there is a trailing comma. */
15823 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15825 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15826 pedwarn (input_location, OPT_Wpedantic,
15827 "comma at end of enumerator list");
15828 break;
15833 /* Parse an enumerator-definition. The enumerator has the indicated
15834 TYPE.
15836 enumerator-definition:
15837 enumerator
15838 enumerator = constant-expression
15840 enumerator:
15841 identifier */
15843 static void
15844 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15846 tree identifier;
15847 tree value;
15848 location_t loc;
15850 /* Save the input location because we are interested in the location
15851 of the identifier and not the location of the explicit value. */
15852 loc = cp_lexer_peek_token (parser->lexer)->location;
15854 /* Look for the identifier. */
15855 identifier = cp_parser_identifier (parser);
15856 if (identifier == error_mark_node)
15857 return;
15859 /* If the next token is an '=', then there is an explicit value. */
15860 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15862 /* Consume the `=' token. */
15863 cp_lexer_consume_token (parser->lexer);
15864 /* Parse the value. */
15865 value = cp_parser_constant_expression (parser,
15866 /*allow_non_constant_p=*/false,
15867 NULL);
15869 else
15870 value = NULL_TREE;
15872 /* If we are processing a template, make sure the initializer of the
15873 enumerator doesn't contain any bare template parameter pack. */
15874 if (check_for_bare_parameter_packs (value))
15875 value = error_mark_node;
15877 /* integral_constant_value will pull out this expression, so make sure
15878 it's folded as appropriate. */
15879 value = fold_non_dependent_expr (value);
15881 /* Create the enumerator. */
15882 build_enumerator (identifier, value, type, loc);
15885 /* Parse a namespace-name.
15887 namespace-name:
15888 original-namespace-name
15889 namespace-alias
15891 Returns the NAMESPACE_DECL for the namespace. */
15893 static tree
15894 cp_parser_namespace_name (cp_parser* parser)
15896 tree identifier;
15897 tree namespace_decl;
15899 cp_token *token = cp_lexer_peek_token (parser->lexer);
15901 /* Get the name of the namespace. */
15902 identifier = cp_parser_identifier (parser);
15903 if (identifier == error_mark_node)
15904 return error_mark_node;
15906 /* Look up the identifier in the currently active scope. Look only
15907 for namespaces, due to:
15909 [basic.lookup.udir]
15911 When looking up a namespace-name in a using-directive or alias
15912 definition, only namespace names are considered.
15914 And:
15916 [basic.lookup.qual]
15918 During the lookup of a name preceding the :: scope resolution
15919 operator, object, function, and enumerator names are ignored.
15921 (Note that cp_parser_qualifying_entity only calls this
15922 function if the token after the name is the scope resolution
15923 operator.) */
15924 namespace_decl = cp_parser_lookup_name (parser, identifier,
15925 none_type,
15926 /*is_template=*/false,
15927 /*is_namespace=*/true,
15928 /*check_dependency=*/true,
15929 /*ambiguous_decls=*/NULL,
15930 token->location);
15931 /* If it's not a namespace, issue an error. */
15932 if (namespace_decl == error_mark_node
15933 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15935 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15936 error_at (token->location, "%qD is not a namespace-name", identifier);
15937 cp_parser_error (parser, "expected namespace-name");
15938 namespace_decl = error_mark_node;
15941 return namespace_decl;
15944 /* Parse a namespace-definition.
15946 namespace-definition:
15947 named-namespace-definition
15948 unnamed-namespace-definition
15950 named-namespace-definition:
15951 original-namespace-definition
15952 extension-namespace-definition
15954 original-namespace-definition:
15955 namespace identifier { namespace-body }
15957 extension-namespace-definition:
15958 namespace original-namespace-name { namespace-body }
15960 unnamed-namespace-definition:
15961 namespace { namespace-body } */
15963 static void
15964 cp_parser_namespace_definition (cp_parser* parser)
15966 tree identifier, attribs;
15967 bool has_visibility;
15968 bool is_inline;
15970 cp_ensure_no_omp_declare_simd (parser);
15971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15973 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15974 is_inline = true;
15975 cp_lexer_consume_token (parser->lexer);
15977 else
15978 is_inline = false;
15980 /* Look for the `namespace' keyword. */
15981 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15983 /* Get the name of the namespace. We do not attempt to distinguish
15984 between an original-namespace-definition and an
15985 extension-namespace-definition at this point. The semantic
15986 analysis routines are responsible for that. */
15987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15988 identifier = cp_parser_identifier (parser);
15989 else
15990 identifier = NULL_TREE;
15992 /* Parse any specified attributes. */
15993 attribs = cp_parser_attributes_opt (parser);
15995 /* Look for the `{' to start the namespace. */
15996 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15997 /* Start the namespace. */
15998 push_namespace (identifier);
16000 /* "inline namespace" is equivalent to a stub namespace definition
16001 followed by a strong using directive. */
16002 if (is_inline)
16004 tree name_space = current_namespace;
16005 /* Set up namespace association. */
16006 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16007 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16008 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16009 /* Import the contents of the inline namespace. */
16010 pop_namespace ();
16011 do_using_directive (name_space);
16012 push_namespace (identifier);
16015 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16017 /* Parse the body of the namespace. */
16018 cp_parser_namespace_body (parser);
16020 if (has_visibility)
16021 pop_visibility (1);
16023 /* Finish the namespace. */
16024 pop_namespace ();
16025 /* Look for the final `}'. */
16026 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16029 /* Parse a namespace-body.
16031 namespace-body:
16032 declaration-seq [opt] */
16034 static void
16035 cp_parser_namespace_body (cp_parser* parser)
16037 cp_parser_declaration_seq_opt (parser);
16040 /* Parse a namespace-alias-definition.
16042 namespace-alias-definition:
16043 namespace identifier = qualified-namespace-specifier ; */
16045 static void
16046 cp_parser_namespace_alias_definition (cp_parser* parser)
16048 tree identifier;
16049 tree namespace_specifier;
16051 cp_token *token = cp_lexer_peek_token (parser->lexer);
16053 /* Look for the `namespace' keyword. */
16054 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16055 /* Look for the identifier. */
16056 identifier = cp_parser_identifier (parser);
16057 if (identifier == error_mark_node)
16058 return;
16059 /* Look for the `=' token. */
16060 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16061 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16063 error_at (token->location, "%<namespace%> definition is not allowed here");
16064 /* Skip the definition. */
16065 cp_lexer_consume_token (parser->lexer);
16066 if (cp_parser_skip_to_closing_brace (parser))
16067 cp_lexer_consume_token (parser->lexer);
16068 return;
16070 cp_parser_require (parser, CPP_EQ, RT_EQ);
16071 /* Look for the qualified-namespace-specifier. */
16072 namespace_specifier
16073 = cp_parser_qualified_namespace_specifier (parser);
16074 /* Look for the `;' token. */
16075 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16077 /* Register the alias in the symbol table. */
16078 do_namespace_alias (identifier, namespace_specifier);
16081 /* Parse a qualified-namespace-specifier.
16083 qualified-namespace-specifier:
16084 :: [opt] nested-name-specifier [opt] namespace-name
16086 Returns a NAMESPACE_DECL corresponding to the specified
16087 namespace. */
16089 static tree
16090 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16092 /* Look for the optional `::'. */
16093 cp_parser_global_scope_opt (parser,
16094 /*current_scope_valid_p=*/false);
16096 /* Look for the optional nested-name-specifier. */
16097 cp_parser_nested_name_specifier_opt (parser,
16098 /*typename_keyword_p=*/false,
16099 /*check_dependency_p=*/true,
16100 /*type_p=*/false,
16101 /*is_declaration=*/true);
16103 return cp_parser_namespace_name (parser);
16106 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16107 access declaration.
16109 using-declaration:
16110 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16111 using :: unqualified-id ;
16113 access-declaration:
16114 qualified-id ;
16118 static bool
16119 cp_parser_using_declaration (cp_parser* parser,
16120 bool access_declaration_p)
16122 cp_token *token;
16123 bool typename_p = false;
16124 bool global_scope_p;
16125 tree decl;
16126 tree identifier;
16127 tree qscope;
16128 int oldcount = errorcount;
16129 cp_token *diag_token = NULL;
16131 if (access_declaration_p)
16133 diag_token = cp_lexer_peek_token (parser->lexer);
16134 cp_parser_parse_tentatively (parser);
16136 else
16138 /* Look for the `using' keyword. */
16139 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16141 /* Peek at the next token. */
16142 token = cp_lexer_peek_token (parser->lexer);
16143 /* See if it's `typename'. */
16144 if (token->keyword == RID_TYPENAME)
16146 /* Remember that we've seen it. */
16147 typename_p = true;
16148 /* Consume the `typename' token. */
16149 cp_lexer_consume_token (parser->lexer);
16153 /* Look for the optional global scope qualification. */
16154 global_scope_p
16155 = (cp_parser_global_scope_opt (parser,
16156 /*current_scope_valid_p=*/false)
16157 != NULL_TREE);
16159 /* If we saw `typename', or didn't see `::', then there must be a
16160 nested-name-specifier present. */
16161 if (typename_p || !global_scope_p)
16163 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16164 /*check_dependency_p=*/true,
16165 /*type_p=*/false,
16166 /*is_declaration=*/true);
16167 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16169 cp_parser_skip_to_end_of_block_or_statement (parser);
16170 return false;
16173 /* Otherwise, we could be in either of the two productions. In that
16174 case, treat the nested-name-specifier as optional. */
16175 else
16176 qscope = cp_parser_nested_name_specifier_opt (parser,
16177 /*typename_keyword_p=*/false,
16178 /*check_dependency_p=*/true,
16179 /*type_p=*/false,
16180 /*is_declaration=*/true);
16181 if (!qscope)
16182 qscope = global_namespace;
16183 else if (UNSCOPED_ENUM_P (qscope))
16184 qscope = CP_TYPE_CONTEXT (qscope);
16186 if (access_declaration_p && cp_parser_error_occurred (parser))
16187 /* Something has already gone wrong; there's no need to parse
16188 further. Since an error has occurred, the return value of
16189 cp_parser_parse_definitely will be false, as required. */
16190 return cp_parser_parse_definitely (parser);
16192 token = cp_lexer_peek_token (parser->lexer);
16193 /* Parse the unqualified-id. */
16194 identifier = cp_parser_unqualified_id (parser,
16195 /*template_keyword_p=*/false,
16196 /*check_dependency_p=*/true,
16197 /*declarator_p=*/true,
16198 /*optional_p=*/false);
16200 if (access_declaration_p)
16202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16203 cp_parser_simulate_error (parser);
16204 if (!cp_parser_parse_definitely (parser))
16205 return false;
16208 /* The function we call to handle a using-declaration is different
16209 depending on what scope we are in. */
16210 if (qscope == error_mark_node || identifier == error_mark_node)
16212 else if (!identifier_p (identifier)
16213 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16214 /* [namespace.udecl]
16216 A using declaration shall not name a template-id. */
16217 error_at (token->location,
16218 "a template-id may not appear in a using-declaration");
16219 else
16221 if (at_class_scope_p ())
16223 /* Create the USING_DECL. */
16224 decl = do_class_using_decl (parser->scope, identifier);
16226 if (decl && typename_p)
16227 USING_DECL_TYPENAME_P (decl) = 1;
16229 if (check_for_bare_parameter_packs (decl))
16231 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16232 return false;
16234 else
16235 /* Add it to the list of members in this class. */
16236 finish_member_declaration (decl);
16238 else
16240 decl = cp_parser_lookup_name_simple (parser,
16241 identifier,
16242 token->location);
16243 if (decl == error_mark_node)
16244 cp_parser_name_lookup_error (parser, identifier,
16245 decl, NLE_NULL,
16246 token->location);
16247 else if (check_for_bare_parameter_packs (decl))
16249 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16250 return false;
16252 else if (!at_namespace_scope_p ())
16253 do_local_using_decl (decl, qscope, identifier);
16254 else
16255 do_toplevel_using_decl (decl, qscope, identifier);
16259 /* Look for the final `;'. */
16260 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16262 if (access_declaration_p && errorcount == oldcount)
16263 warning_at (diag_token->location, OPT_Wdeprecated,
16264 "access declarations are deprecated "
16265 "in favour of using-declarations; "
16266 "suggestion: add the %<using%> keyword");
16268 return true;
16271 /* Parse an alias-declaration.
16273 alias-declaration:
16274 using identifier attribute-specifier-seq [opt] = type-id */
16276 static tree
16277 cp_parser_alias_declaration (cp_parser* parser)
16279 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16280 location_t id_location;
16281 cp_declarator *declarator;
16282 cp_decl_specifier_seq decl_specs;
16283 bool member_p;
16284 const char *saved_message = NULL;
16286 /* Look for the `using' keyword. */
16287 cp_token *using_token
16288 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16289 if (using_token == NULL)
16290 return error_mark_node;
16292 id_location = cp_lexer_peek_token (parser->lexer)->location;
16293 id = cp_parser_identifier (parser);
16294 if (id == error_mark_node)
16295 return error_mark_node;
16297 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16298 attributes = cp_parser_attributes_opt (parser);
16299 if (attributes == error_mark_node)
16300 return error_mark_node;
16302 cp_parser_require (parser, CPP_EQ, RT_EQ);
16304 if (cp_parser_error_occurred (parser))
16305 return error_mark_node;
16307 cp_parser_commit_to_tentative_parse (parser);
16309 /* Now we are going to parse the type-id of the declaration. */
16312 [dcl.type]/3 says:
16314 "A type-specifier-seq shall not define a class or enumeration
16315 unless it appears in the type-id of an alias-declaration (7.1.3) that
16316 is not the declaration of a template-declaration."
16318 In other words, if we currently are in an alias template, the
16319 type-id should not define a type.
16321 So let's set parser->type_definition_forbidden_message in that
16322 case; cp_parser_check_type_definition (called by
16323 cp_parser_class_specifier) will then emit an error if a type is
16324 defined in the type-id. */
16325 if (parser->num_template_parameter_lists)
16327 saved_message = parser->type_definition_forbidden_message;
16328 parser->type_definition_forbidden_message =
16329 G_("types may not be defined in alias template declarations");
16332 type = cp_parser_type_id (parser);
16334 /* Restore the error message if need be. */
16335 if (parser->num_template_parameter_lists)
16336 parser->type_definition_forbidden_message = saved_message;
16338 if (type == error_mark_node
16339 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16341 cp_parser_skip_to_end_of_block_or_statement (parser);
16342 return error_mark_node;
16345 /* A typedef-name can also be introduced by an alias-declaration. The
16346 identifier following the using keyword becomes a typedef-name. It has
16347 the same semantics as if it were introduced by the typedef
16348 specifier. In particular, it does not define a new type and it shall
16349 not appear in the type-id. */
16351 clear_decl_specs (&decl_specs);
16352 decl_specs.type = type;
16353 if (attributes != NULL_TREE)
16355 decl_specs.attributes = attributes;
16356 set_and_check_decl_spec_loc (&decl_specs,
16357 ds_attribute,
16358 attrs_token);
16360 set_and_check_decl_spec_loc (&decl_specs,
16361 ds_typedef,
16362 using_token);
16363 set_and_check_decl_spec_loc (&decl_specs,
16364 ds_alias,
16365 using_token);
16367 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16368 declarator->id_loc = id_location;
16370 member_p = at_class_scope_p ();
16371 if (member_p)
16372 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16373 NULL_TREE, attributes);
16374 else
16375 decl = start_decl (declarator, &decl_specs, 0,
16376 attributes, NULL_TREE, &pushed_scope);
16377 if (decl == error_mark_node)
16378 return decl;
16380 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16382 if (pushed_scope)
16383 pop_scope (pushed_scope);
16385 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16386 added into the symbol table; otherwise, return the TYPE_DECL. */
16387 if (DECL_LANG_SPECIFIC (decl)
16388 && DECL_TEMPLATE_INFO (decl)
16389 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16391 decl = DECL_TI_TEMPLATE (decl);
16392 if (member_p)
16393 check_member_template (decl);
16396 return decl;
16399 /* Parse a using-directive.
16401 using-directive:
16402 using namespace :: [opt] nested-name-specifier [opt]
16403 namespace-name ; */
16405 static void
16406 cp_parser_using_directive (cp_parser* parser)
16408 tree namespace_decl;
16409 tree attribs;
16411 /* Look for the `using' keyword. */
16412 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16413 /* And the `namespace' keyword. */
16414 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16415 /* Look for the optional `::' operator. */
16416 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16417 /* And the optional nested-name-specifier. */
16418 cp_parser_nested_name_specifier_opt (parser,
16419 /*typename_keyword_p=*/false,
16420 /*check_dependency_p=*/true,
16421 /*type_p=*/false,
16422 /*is_declaration=*/true);
16423 /* Get the namespace being used. */
16424 namespace_decl = cp_parser_namespace_name (parser);
16425 /* And any specified attributes. */
16426 attribs = cp_parser_attributes_opt (parser);
16427 /* Update the symbol table. */
16428 parse_using_directive (namespace_decl, attribs);
16429 /* Look for the final `;'. */
16430 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16433 /* Parse an asm-definition.
16435 asm-definition:
16436 asm ( string-literal ) ;
16438 GNU Extension:
16440 asm-definition:
16441 asm volatile [opt] ( string-literal ) ;
16442 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16443 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16444 : asm-operand-list [opt] ) ;
16445 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16446 : asm-operand-list [opt]
16447 : asm-clobber-list [opt] ) ;
16448 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16449 : asm-clobber-list [opt]
16450 : asm-goto-list ) ; */
16452 static void
16453 cp_parser_asm_definition (cp_parser* parser)
16455 tree string;
16456 tree outputs = NULL_TREE;
16457 tree inputs = NULL_TREE;
16458 tree clobbers = NULL_TREE;
16459 tree labels = NULL_TREE;
16460 tree asm_stmt;
16461 bool volatile_p = false;
16462 bool extended_p = false;
16463 bool invalid_inputs_p = false;
16464 bool invalid_outputs_p = false;
16465 bool goto_p = false;
16466 required_token missing = RT_NONE;
16468 /* Look for the `asm' keyword. */
16469 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16470 /* See if the next token is `volatile'. */
16471 if (cp_parser_allow_gnu_extensions_p (parser)
16472 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16474 /* Remember that we saw the `volatile' keyword. */
16475 volatile_p = true;
16476 /* Consume the token. */
16477 cp_lexer_consume_token (parser->lexer);
16479 if (cp_parser_allow_gnu_extensions_p (parser)
16480 && parser->in_function_body
16481 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16483 /* Remember that we saw the `goto' keyword. */
16484 goto_p = true;
16485 /* Consume the token. */
16486 cp_lexer_consume_token (parser->lexer);
16488 /* Look for the opening `('. */
16489 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16490 return;
16491 /* Look for the string. */
16492 string = cp_parser_string_literal (parser, false, false);
16493 if (string == error_mark_node)
16495 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16496 /*consume_paren=*/true);
16497 return;
16500 /* If we're allowing GNU extensions, check for the extended assembly
16501 syntax. Unfortunately, the `:' tokens need not be separated by
16502 a space in C, and so, for compatibility, we tolerate that here
16503 too. Doing that means that we have to treat the `::' operator as
16504 two `:' tokens. */
16505 if (cp_parser_allow_gnu_extensions_p (parser)
16506 && parser->in_function_body
16507 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16508 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16510 bool inputs_p = false;
16511 bool clobbers_p = false;
16512 bool labels_p = false;
16514 /* The extended syntax was used. */
16515 extended_p = true;
16517 /* Look for outputs. */
16518 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16520 /* Consume the `:'. */
16521 cp_lexer_consume_token (parser->lexer);
16522 /* Parse the output-operands. */
16523 if (cp_lexer_next_token_is_not (parser->lexer,
16524 CPP_COLON)
16525 && cp_lexer_next_token_is_not (parser->lexer,
16526 CPP_SCOPE)
16527 && cp_lexer_next_token_is_not (parser->lexer,
16528 CPP_CLOSE_PAREN)
16529 && !goto_p)
16530 outputs = cp_parser_asm_operand_list (parser);
16532 if (outputs == error_mark_node)
16533 invalid_outputs_p = true;
16535 /* If the next token is `::', there are no outputs, and the
16536 next token is the beginning of the inputs. */
16537 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16538 /* The inputs are coming next. */
16539 inputs_p = true;
16541 /* Look for inputs. */
16542 if (inputs_p
16543 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16545 /* Consume the `:' or `::'. */
16546 cp_lexer_consume_token (parser->lexer);
16547 /* Parse the output-operands. */
16548 if (cp_lexer_next_token_is_not (parser->lexer,
16549 CPP_COLON)
16550 && cp_lexer_next_token_is_not (parser->lexer,
16551 CPP_SCOPE)
16552 && cp_lexer_next_token_is_not (parser->lexer,
16553 CPP_CLOSE_PAREN))
16554 inputs = cp_parser_asm_operand_list (parser);
16556 if (inputs == error_mark_node)
16557 invalid_inputs_p = true;
16559 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16560 /* The clobbers are coming next. */
16561 clobbers_p = true;
16563 /* Look for clobbers. */
16564 if (clobbers_p
16565 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16567 clobbers_p = true;
16568 /* Consume the `:' or `::'. */
16569 cp_lexer_consume_token (parser->lexer);
16570 /* Parse the clobbers. */
16571 if (cp_lexer_next_token_is_not (parser->lexer,
16572 CPP_COLON)
16573 && cp_lexer_next_token_is_not (parser->lexer,
16574 CPP_CLOSE_PAREN))
16575 clobbers = cp_parser_asm_clobber_list (parser);
16577 else if (goto_p
16578 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16579 /* The labels are coming next. */
16580 labels_p = true;
16582 /* Look for labels. */
16583 if (labels_p
16584 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16586 labels_p = true;
16587 /* Consume the `:' or `::'. */
16588 cp_lexer_consume_token (parser->lexer);
16589 /* Parse the labels. */
16590 labels = cp_parser_asm_label_list (parser);
16593 if (goto_p && !labels_p)
16594 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16596 else if (goto_p)
16597 missing = RT_COLON_SCOPE;
16599 /* Look for the closing `)'. */
16600 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16601 missing ? missing : RT_CLOSE_PAREN))
16602 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16603 /*consume_paren=*/true);
16604 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16606 if (!invalid_inputs_p && !invalid_outputs_p)
16608 /* Create the ASM_EXPR. */
16609 if (parser->in_function_body)
16611 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16612 inputs, clobbers, labels);
16613 /* If the extended syntax was not used, mark the ASM_EXPR. */
16614 if (!extended_p)
16616 tree temp = asm_stmt;
16617 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16618 temp = TREE_OPERAND (temp, 0);
16620 ASM_INPUT_P (temp) = 1;
16623 else
16624 add_asm_node (string);
16628 /* Declarators [gram.dcl.decl] */
16630 /* Parse an init-declarator.
16632 init-declarator:
16633 declarator initializer [opt]
16635 GNU Extension:
16637 init-declarator:
16638 declarator asm-specification [opt] attributes [opt] initializer [opt]
16640 function-definition:
16641 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16642 function-body
16643 decl-specifier-seq [opt] declarator function-try-block
16645 GNU Extension:
16647 function-definition:
16648 __extension__ function-definition
16650 TM Extension:
16652 function-definition:
16653 decl-specifier-seq [opt] declarator function-transaction-block
16655 The DECL_SPECIFIERS apply to this declarator. Returns a
16656 representation of the entity declared. If MEMBER_P is TRUE, then
16657 this declarator appears in a class scope. The new DECL created by
16658 this declarator is returned.
16660 The CHECKS are access checks that should be performed once we know
16661 what entity is being declared (and, therefore, what classes have
16662 befriended it).
16664 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16665 for a function-definition here as well. If the declarator is a
16666 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16667 be TRUE upon return. By that point, the function-definition will
16668 have been completely parsed.
16670 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16671 is FALSE.
16673 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16674 parsed declaration if it is an uninitialized single declarator not followed
16675 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16676 if present, will not be consumed. If returned, this declarator will be
16677 created with SD_INITIALIZED but will not call cp_finish_decl. */
16679 static tree
16680 cp_parser_init_declarator (cp_parser* parser,
16681 cp_decl_specifier_seq *decl_specifiers,
16682 vec<deferred_access_check, va_gc> *checks,
16683 bool function_definition_allowed_p,
16684 bool member_p,
16685 int declares_class_or_enum,
16686 bool* function_definition_p,
16687 tree* maybe_range_for_decl)
16689 cp_token *token = NULL, *asm_spec_start_token = NULL,
16690 *attributes_start_token = NULL;
16691 cp_declarator *declarator;
16692 tree prefix_attributes;
16693 tree attributes = NULL;
16694 tree asm_specification;
16695 tree initializer;
16696 tree decl = NULL_TREE;
16697 tree scope;
16698 int is_initialized;
16699 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16700 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16701 "(...)". */
16702 enum cpp_ttype initialization_kind;
16703 bool is_direct_init = false;
16704 bool is_non_constant_init;
16705 int ctor_dtor_or_conv_p;
16706 bool friend_p = cp_parser_friend_p (decl_specifiers);
16707 tree pushed_scope = NULL_TREE;
16708 bool range_for_decl_p = false;
16709 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16711 /* Gather the attributes that were provided with the
16712 decl-specifiers. */
16713 prefix_attributes = decl_specifiers->attributes;
16715 /* Assume that this is not the declarator for a function
16716 definition. */
16717 if (function_definition_p)
16718 *function_definition_p = false;
16720 /* Default arguments are only permitted for function parameters. */
16721 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16722 parser->default_arg_ok_p = false;
16724 /* Defer access checks while parsing the declarator; we cannot know
16725 what names are accessible until we know what is being
16726 declared. */
16727 resume_deferring_access_checks ();
16729 /* Parse the declarator. */
16730 token = cp_lexer_peek_token (parser->lexer);
16731 declarator
16732 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16733 &ctor_dtor_or_conv_p,
16734 /*parenthesized_p=*/NULL,
16735 member_p, friend_p);
16736 /* Gather up the deferred checks. */
16737 stop_deferring_access_checks ();
16739 parser->default_arg_ok_p = saved_default_arg_ok_p;
16741 /* If the DECLARATOR was erroneous, there's no need to go
16742 further. */
16743 if (declarator == cp_error_declarator)
16744 return error_mark_node;
16746 /* Check that the number of template-parameter-lists is OK. */
16747 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16748 token->location))
16749 return error_mark_node;
16751 if (declares_class_or_enum & 2)
16752 cp_parser_check_for_definition_in_return_type (declarator,
16753 decl_specifiers->type,
16754 decl_specifiers->locations[ds_type_spec]);
16756 /* Figure out what scope the entity declared by the DECLARATOR is
16757 located in. `grokdeclarator' sometimes changes the scope, so
16758 we compute it now. */
16759 scope = get_scope_of_declarator (declarator);
16761 /* Perform any lookups in the declared type which were thought to be
16762 dependent, but are not in the scope of the declarator. */
16763 decl_specifiers->type
16764 = maybe_update_decl_type (decl_specifiers->type, scope);
16766 /* If we're allowing GNU extensions, look for an
16767 asm-specification. */
16768 if (cp_parser_allow_gnu_extensions_p (parser))
16770 /* Look for an asm-specification. */
16771 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16772 asm_specification = cp_parser_asm_specification_opt (parser);
16774 else
16775 asm_specification = NULL_TREE;
16777 /* Look for attributes. */
16778 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16779 attributes = cp_parser_attributes_opt (parser);
16781 /* Peek at the next token. */
16782 token = cp_lexer_peek_token (parser->lexer);
16784 if (function_declarator_p (declarator))
16786 /* Check to see if the token indicates the start of a
16787 function-definition. */
16788 if (cp_parser_token_starts_function_definition_p (token))
16790 if (!function_definition_allowed_p)
16792 /* If a function-definition should not appear here, issue an
16793 error message. */
16794 cp_parser_error (parser,
16795 "a function-definition is not allowed here");
16796 return error_mark_node;
16799 location_t func_brace_location
16800 = cp_lexer_peek_token (parser->lexer)->location;
16802 /* Neither attributes nor an asm-specification are allowed
16803 on a function-definition. */
16804 if (asm_specification)
16805 error_at (asm_spec_start_token->location,
16806 "an asm-specification is not allowed "
16807 "on a function-definition");
16808 if (attributes)
16809 error_at (attributes_start_token->location,
16810 "attributes are not allowed "
16811 "on a function-definition");
16812 /* This is a function-definition. */
16813 *function_definition_p = true;
16815 /* Parse the function definition. */
16816 if (member_p)
16817 decl = cp_parser_save_member_function_body (parser,
16818 decl_specifiers,
16819 declarator,
16820 prefix_attributes);
16821 else
16822 decl =
16823 (cp_parser_function_definition_from_specifiers_and_declarator
16824 (parser, decl_specifiers, prefix_attributes, declarator));
16826 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16828 /* This is where the prologue starts... */
16829 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16830 = func_brace_location;
16833 return decl;
16837 /* [dcl.dcl]
16839 Only in function declarations for constructors, destructors, and
16840 type conversions can the decl-specifier-seq be omitted.
16842 We explicitly postpone this check past the point where we handle
16843 function-definitions because we tolerate function-definitions
16844 that are missing their return types in some modes. */
16845 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16847 cp_parser_error (parser,
16848 "expected constructor, destructor, or type conversion");
16849 return error_mark_node;
16852 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16853 if (token->type == CPP_EQ
16854 || token->type == CPP_OPEN_PAREN
16855 || token->type == CPP_OPEN_BRACE)
16857 is_initialized = SD_INITIALIZED;
16858 initialization_kind = token->type;
16859 if (maybe_range_for_decl)
16860 *maybe_range_for_decl = error_mark_node;
16862 if (token->type == CPP_EQ
16863 && function_declarator_p (declarator))
16865 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16866 if (t2->keyword == RID_DEFAULT)
16867 is_initialized = SD_DEFAULTED;
16868 else if (t2->keyword == RID_DELETE)
16869 is_initialized = SD_DELETED;
16872 else
16874 /* If the init-declarator isn't initialized and isn't followed by a
16875 `,' or `;', it's not a valid init-declarator. */
16876 if (token->type != CPP_COMMA
16877 && token->type != CPP_SEMICOLON)
16879 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16880 range_for_decl_p = true;
16881 else
16883 cp_parser_error (parser, "expected initializer");
16884 return error_mark_node;
16887 is_initialized = SD_UNINITIALIZED;
16888 initialization_kind = CPP_EOF;
16891 /* Because start_decl has side-effects, we should only call it if we
16892 know we're going ahead. By this point, we know that we cannot
16893 possibly be looking at any other construct. */
16894 cp_parser_commit_to_tentative_parse (parser);
16896 /* If the decl specifiers were bad, issue an error now that we're
16897 sure this was intended to be a declarator. Then continue
16898 declaring the variable(s), as int, to try to cut down on further
16899 errors. */
16900 if (decl_specifiers->any_specifiers_p
16901 && decl_specifiers->type == error_mark_node)
16903 cp_parser_error (parser, "invalid type in declaration");
16904 decl_specifiers->type = integer_type_node;
16907 /* Enter the newly declared entry in the symbol table. If we're
16908 processing a declaration in a class-specifier, we wait until
16909 after processing the initializer. */
16910 if (!member_p)
16912 if (parser->in_unbraced_linkage_specification_p)
16913 decl_specifiers->storage_class = sc_extern;
16914 decl = start_decl (declarator, decl_specifiers,
16915 range_for_decl_p? SD_INITIALIZED : is_initialized,
16916 attributes, prefix_attributes, &pushed_scope);
16917 cp_finalize_omp_declare_simd (parser, decl);
16918 /* Adjust location of decl if declarator->id_loc is more appropriate:
16919 set, and decl wasn't merged with another decl, in which case its
16920 location would be different from input_location, and more accurate. */
16921 if (DECL_P (decl)
16922 && declarator->id_loc != UNKNOWN_LOCATION
16923 && DECL_SOURCE_LOCATION (decl) == input_location)
16924 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16926 else if (scope)
16927 /* Enter the SCOPE. That way unqualified names appearing in the
16928 initializer will be looked up in SCOPE. */
16929 pushed_scope = push_scope (scope);
16931 /* Perform deferred access control checks, now that we know in which
16932 SCOPE the declared entity resides. */
16933 if (!member_p && decl)
16935 tree saved_current_function_decl = NULL_TREE;
16937 /* If the entity being declared is a function, pretend that we
16938 are in its scope. If it is a `friend', it may have access to
16939 things that would not otherwise be accessible. */
16940 if (TREE_CODE (decl) == FUNCTION_DECL)
16942 saved_current_function_decl = current_function_decl;
16943 current_function_decl = decl;
16946 /* Perform access checks for template parameters. */
16947 cp_parser_perform_template_parameter_access_checks (checks);
16949 /* Perform the access control checks for the declarator and the
16950 decl-specifiers. */
16951 perform_deferred_access_checks (tf_warning_or_error);
16953 /* Restore the saved value. */
16954 if (TREE_CODE (decl) == FUNCTION_DECL)
16955 current_function_decl = saved_current_function_decl;
16958 /* Parse the initializer. */
16959 initializer = NULL_TREE;
16960 is_direct_init = false;
16961 is_non_constant_init = true;
16962 if (is_initialized)
16964 if (function_declarator_p (declarator))
16966 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16967 if (initialization_kind == CPP_EQ)
16968 initializer = cp_parser_pure_specifier (parser);
16969 else
16971 /* If the declaration was erroneous, we don't really
16972 know what the user intended, so just silently
16973 consume the initializer. */
16974 if (decl != error_mark_node)
16975 error_at (initializer_start_token->location,
16976 "initializer provided for function");
16977 cp_parser_skip_to_closing_parenthesis (parser,
16978 /*recovering=*/true,
16979 /*or_comma=*/false,
16980 /*consume_paren=*/true);
16983 else
16985 /* We want to record the extra mangling scope for in-class
16986 initializers of class members and initializers of static data
16987 member templates. The former involves deferring
16988 parsing of the initializer until end of class as with default
16989 arguments. So right here we only handle the latter. */
16990 if (!member_p && processing_template_decl)
16991 start_lambda_scope (decl);
16992 initializer = cp_parser_initializer (parser,
16993 &is_direct_init,
16994 &is_non_constant_init);
16995 if (!member_p && processing_template_decl)
16996 finish_lambda_scope ();
16997 if (initializer == error_mark_node)
16998 cp_parser_skip_to_end_of_statement (parser);
17002 /* The old parser allows attributes to appear after a parenthesized
17003 initializer. Mark Mitchell proposed removing this functionality
17004 on the GCC mailing lists on 2002-08-13. This parser accepts the
17005 attributes -- but ignores them. */
17006 if (cp_parser_allow_gnu_extensions_p (parser)
17007 && initialization_kind == CPP_OPEN_PAREN)
17008 if (cp_parser_attributes_opt (parser))
17009 warning (OPT_Wattributes,
17010 "attributes after parenthesized initializer ignored");
17012 /* A non-template declaration involving a function parameter list containing
17013 an implicit template parameter will have been made into a template. If it
17014 turns out that the resulting declaration is not an actual function then
17015 finish the template declaration here. An error message will already have
17016 been issued. */
17017 if (parser->fully_implicit_function_template_p)
17018 if (!function_declarator_p (declarator))
17020 if (pushed_scope)
17022 pop_scope (pushed_scope);
17023 pushed_scope = 0;
17025 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17028 /* For an in-class declaration, use `grokfield' to create the
17029 declaration. */
17030 if (member_p)
17032 if (pushed_scope)
17034 pop_scope (pushed_scope);
17035 pushed_scope = NULL_TREE;
17037 decl = grokfield (declarator, decl_specifiers,
17038 initializer, !is_non_constant_init,
17039 /*asmspec=*/NULL_TREE,
17040 chainon (attributes, prefix_attributes));
17041 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17042 cp_parser_save_default_args (parser, decl);
17043 cp_finalize_omp_declare_simd (parser, decl);
17046 /* Finish processing the declaration. But, skip member
17047 declarations. */
17048 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17050 cp_finish_decl (decl,
17051 initializer, !is_non_constant_init,
17052 asm_specification,
17053 /* If the initializer is in parentheses, then this is
17054 a direct-initialization, which means that an
17055 `explicit' constructor is OK. Otherwise, an
17056 `explicit' constructor cannot be used. */
17057 ((is_direct_init || !is_initialized)
17058 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17060 else if ((cxx_dialect != cxx98) && friend_p
17061 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17062 /* Core issue #226 (C++0x only): A default template-argument
17063 shall not be specified in a friend class template
17064 declaration. */
17065 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17066 /*is_partial=*/false, /*is_friend_decl=*/1);
17068 if (!friend_p && pushed_scope)
17069 pop_scope (pushed_scope);
17071 if (function_declarator_p (declarator)
17072 && parser->fully_implicit_function_template_p)
17074 if (member_p)
17075 decl = finish_fully_implicit_template (parser, decl);
17076 else
17077 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17080 return decl;
17083 /* Parse a declarator.
17085 declarator:
17086 direct-declarator
17087 ptr-operator declarator
17089 abstract-declarator:
17090 ptr-operator abstract-declarator [opt]
17091 direct-abstract-declarator
17093 GNU Extensions:
17095 declarator:
17096 attributes [opt] direct-declarator
17097 attributes [opt] ptr-operator declarator
17099 abstract-declarator:
17100 attributes [opt] ptr-operator abstract-declarator [opt]
17101 attributes [opt] direct-abstract-declarator
17103 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17104 detect constructor, destructor or conversion operators. It is set
17105 to -1 if the declarator is a name, and +1 if it is a
17106 function. Otherwise it is set to zero. Usually you just want to
17107 test for >0, but internally the negative value is used.
17109 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17110 a decl-specifier-seq unless it declares a constructor, destructor,
17111 or conversion. It might seem that we could check this condition in
17112 semantic analysis, rather than parsing, but that makes it difficult
17113 to handle something like `f()'. We want to notice that there are
17114 no decl-specifiers, and therefore realize that this is an
17115 expression, not a declaration.)
17117 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17118 the declarator is a direct-declarator of the form "(...)".
17120 MEMBER_P is true iff this declarator is a member-declarator.
17122 FRIEND_P is true iff this declarator is a friend. */
17124 static cp_declarator *
17125 cp_parser_declarator (cp_parser* parser,
17126 cp_parser_declarator_kind dcl_kind,
17127 int* ctor_dtor_or_conv_p,
17128 bool* parenthesized_p,
17129 bool member_p, bool friend_p)
17131 cp_declarator *declarator;
17132 enum tree_code code;
17133 cp_cv_quals cv_quals;
17134 tree class_type;
17135 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17137 /* Assume this is not a constructor, destructor, or type-conversion
17138 operator. */
17139 if (ctor_dtor_or_conv_p)
17140 *ctor_dtor_or_conv_p = 0;
17142 if (cp_parser_allow_gnu_extensions_p (parser))
17143 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17145 /* Check for the ptr-operator production. */
17146 cp_parser_parse_tentatively (parser);
17147 /* Parse the ptr-operator. */
17148 code = cp_parser_ptr_operator (parser,
17149 &class_type,
17150 &cv_quals,
17151 &std_attributes);
17153 /* If that worked, then we have a ptr-operator. */
17154 if (cp_parser_parse_definitely (parser))
17156 /* If a ptr-operator was found, then this declarator was not
17157 parenthesized. */
17158 if (parenthesized_p)
17159 *parenthesized_p = true;
17160 /* The dependent declarator is optional if we are parsing an
17161 abstract-declarator. */
17162 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17163 cp_parser_parse_tentatively (parser);
17165 /* Parse the dependent declarator. */
17166 declarator = cp_parser_declarator (parser, dcl_kind,
17167 /*ctor_dtor_or_conv_p=*/NULL,
17168 /*parenthesized_p=*/NULL,
17169 /*member_p=*/false,
17170 friend_p);
17172 /* If we are parsing an abstract-declarator, we must handle the
17173 case where the dependent declarator is absent. */
17174 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17175 && !cp_parser_parse_definitely (parser))
17176 declarator = NULL;
17178 declarator = cp_parser_make_indirect_declarator
17179 (code, class_type, cv_quals, declarator, std_attributes);
17181 /* Everything else is a direct-declarator. */
17182 else
17184 if (parenthesized_p)
17185 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17186 CPP_OPEN_PAREN);
17187 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17188 ctor_dtor_or_conv_p,
17189 member_p, friend_p);
17192 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17193 declarator->attributes = gnu_attributes;
17194 return declarator;
17197 /* Parse a direct-declarator or direct-abstract-declarator.
17199 direct-declarator:
17200 declarator-id
17201 direct-declarator ( parameter-declaration-clause )
17202 cv-qualifier-seq [opt]
17203 ref-qualifier [opt]
17204 exception-specification [opt]
17205 direct-declarator [ constant-expression [opt] ]
17206 ( declarator )
17208 direct-abstract-declarator:
17209 direct-abstract-declarator [opt]
17210 ( parameter-declaration-clause )
17211 cv-qualifier-seq [opt]
17212 ref-qualifier [opt]
17213 exception-specification [opt]
17214 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17215 ( abstract-declarator )
17217 Returns a representation of the declarator. DCL_KIND is
17218 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17219 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17220 we are parsing a direct-declarator. It is
17221 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17222 of ambiguity we prefer an abstract declarator, as per
17223 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17224 as for cp_parser_declarator. */
17226 static cp_declarator *
17227 cp_parser_direct_declarator (cp_parser* parser,
17228 cp_parser_declarator_kind dcl_kind,
17229 int* ctor_dtor_or_conv_p,
17230 bool member_p, bool friend_p)
17232 cp_token *token;
17233 cp_declarator *declarator = NULL;
17234 tree scope = NULL_TREE;
17235 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17236 bool saved_in_declarator_p = parser->in_declarator_p;
17237 bool first = true;
17238 tree pushed_scope = NULL_TREE;
17240 while (true)
17242 /* Peek at the next token. */
17243 token = cp_lexer_peek_token (parser->lexer);
17244 if (token->type == CPP_OPEN_PAREN)
17246 /* This is either a parameter-declaration-clause, or a
17247 parenthesized declarator. When we know we are parsing a
17248 named declarator, it must be a parenthesized declarator
17249 if FIRST is true. For instance, `(int)' is a
17250 parameter-declaration-clause, with an omitted
17251 direct-abstract-declarator. But `((*))', is a
17252 parenthesized abstract declarator. Finally, when T is a
17253 template parameter `(T)' is a
17254 parameter-declaration-clause, and not a parenthesized
17255 named declarator.
17257 We first try and parse a parameter-declaration-clause,
17258 and then try a nested declarator (if FIRST is true).
17260 It is not an error for it not to be a
17261 parameter-declaration-clause, even when FIRST is
17262 false. Consider,
17264 int i (int);
17265 int i (3);
17267 The first is the declaration of a function while the
17268 second is the definition of a variable, including its
17269 initializer.
17271 Having seen only the parenthesis, we cannot know which of
17272 these two alternatives should be selected. Even more
17273 complex are examples like:
17275 int i (int (a));
17276 int i (int (3));
17278 The former is a function-declaration; the latter is a
17279 variable initialization.
17281 Thus again, we try a parameter-declaration-clause, and if
17282 that fails, we back out and return. */
17284 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17286 tree params;
17287 bool is_declarator = false;
17289 /* In a member-declarator, the only valid interpretation
17290 of a parenthesis is the start of a
17291 parameter-declaration-clause. (It is invalid to
17292 initialize a static data member with a parenthesized
17293 initializer; only the "=" form of initialization is
17294 permitted.) */
17295 if (!member_p)
17296 cp_parser_parse_tentatively (parser);
17298 /* Consume the `('. */
17299 cp_lexer_consume_token (parser->lexer);
17300 if (first)
17302 /* If this is going to be an abstract declarator, we're
17303 in a declarator and we can't have default args. */
17304 parser->default_arg_ok_p = false;
17305 parser->in_declarator_p = true;
17308 begin_scope (sk_function_parms, NULL_TREE);
17310 /* Parse the parameter-declaration-clause. */
17311 params = cp_parser_parameter_declaration_clause (parser);
17313 /* Consume the `)'. */
17314 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17316 /* If all went well, parse the cv-qualifier-seq,
17317 ref-qualifier and the exception-specification. */
17318 if (member_p || cp_parser_parse_definitely (parser))
17320 cp_cv_quals cv_quals;
17321 cp_virt_specifiers virt_specifiers;
17322 cp_ref_qualifier ref_qual;
17323 tree exception_specification;
17324 tree late_return;
17325 tree attrs;
17326 bool memfn = (member_p || (pushed_scope
17327 && CLASS_TYPE_P (pushed_scope)));
17329 is_declarator = true;
17331 if (ctor_dtor_or_conv_p)
17332 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17333 first = false;
17335 /* Parse the cv-qualifier-seq. */
17336 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17337 /* Parse the ref-qualifier. */
17338 ref_qual = cp_parser_ref_qualifier_opt (parser);
17339 /* And the exception-specification. */
17340 exception_specification
17341 = cp_parser_exception_specification_opt (parser);
17343 attrs = cp_parser_std_attribute_spec_seq (parser);
17345 /* In here, we handle cases where attribute is used after
17346 the function declaration. For example:
17347 void func (int x) __attribute__((vector(..))); */
17348 if (flag_cilkplus
17349 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17351 cp_parser_parse_tentatively (parser);
17352 tree attr = cp_parser_gnu_attributes_opt (parser);
17353 if (cp_lexer_next_token_is_not (parser->lexer,
17354 CPP_SEMICOLON)
17355 && cp_lexer_next_token_is_not (parser->lexer,
17356 CPP_OPEN_BRACE))
17357 cp_parser_abort_tentative_parse (parser);
17358 else if (!cp_parser_parse_definitely (parser))
17360 else
17361 attrs = chainon (attr, attrs);
17363 late_return = (cp_parser_late_return_type_opt
17364 (parser, declarator,
17365 memfn ? cv_quals : -1));
17368 /* Parse the virt-specifier-seq. */
17369 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17371 /* Create the function-declarator. */
17372 declarator = make_call_declarator (declarator,
17373 params,
17374 cv_quals,
17375 virt_specifiers,
17376 ref_qual,
17377 exception_specification,
17378 late_return);
17379 declarator->std_attributes = attrs;
17380 /* Any subsequent parameter lists are to do with
17381 return type, so are not those of the declared
17382 function. */
17383 parser->default_arg_ok_p = false;
17386 /* Remove the function parms from scope. */
17387 pop_bindings_and_leave_scope ();
17389 if (is_declarator)
17390 /* Repeat the main loop. */
17391 continue;
17394 /* If this is the first, we can try a parenthesized
17395 declarator. */
17396 if (first)
17398 bool saved_in_type_id_in_expr_p;
17400 parser->default_arg_ok_p = saved_default_arg_ok_p;
17401 parser->in_declarator_p = saved_in_declarator_p;
17403 /* Consume the `('. */
17404 cp_lexer_consume_token (parser->lexer);
17405 /* Parse the nested declarator. */
17406 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17407 parser->in_type_id_in_expr_p = true;
17408 declarator
17409 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17410 /*parenthesized_p=*/NULL,
17411 member_p, friend_p);
17412 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17413 first = false;
17414 /* Expect a `)'. */
17415 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17416 declarator = cp_error_declarator;
17417 if (declarator == cp_error_declarator)
17418 break;
17420 goto handle_declarator;
17422 /* Otherwise, we must be done. */
17423 else
17424 break;
17426 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17427 && token->type == CPP_OPEN_SQUARE
17428 && !cp_next_tokens_can_be_attribute_p (parser))
17430 /* Parse an array-declarator. */
17431 tree bounds, attrs;
17433 if (ctor_dtor_or_conv_p)
17434 *ctor_dtor_or_conv_p = 0;
17436 first = false;
17437 parser->default_arg_ok_p = false;
17438 parser->in_declarator_p = true;
17439 /* Consume the `['. */
17440 cp_lexer_consume_token (parser->lexer);
17441 /* Peek at the next token. */
17442 token = cp_lexer_peek_token (parser->lexer);
17443 /* If the next token is `]', then there is no
17444 constant-expression. */
17445 if (token->type != CPP_CLOSE_SQUARE)
17447 bool non_constant_p;
17448 bounds
17449 = cp_parser_constant_expression (parser,
17450 /*allow_non_constant=*/true,
17451 &non_constant_p);
17452 if (!non_constant_p)
17453 /* OK */;
17454 else if (error_operand_p (bounds))
17455 /* Already gave an error. */;
17456 else if (!parser->in_function_body
17457 || current_binding_level->kind == sk_function_parms)
17459 /* Normally, the array bound must be an integral constant
17460 expression. However, as an extension, we allow VLAs
17461 in function scopes as long as they aren't part of a
17462 parameter declaration. */
17463 cp_parser_error (parser,
17464 "array bound is not an integer constant");
17465 bounds = error_mark_node;
17467 else if (processing_template_decl
17468 && !type_dependent_expression_p (bounds))
17470 /* Remember this wasn't a constant-expression. */
17471 bounds = build_nop (TREE_TYPE (bounds), bounds);
17472 TREE_SIDE_EFFECTS (bounds) = 1;
17475 else
17476 bounds = NULL_TREE;
17477 /* Look for the closing `]'. */
17478 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17480 declarator = cp_error_declarator;
17481 break;
17484 attrs = cp_parser_std_attribute_spec_seq (parser);
17485 declarator = make_array_declarator (declarator, bounds);
17486 declarator->std_attributes = attrs;
17488 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17491 tree qualifying_scope;
17492 tree unqualified_name;
17493 tree attrs;
17494 special_function_kind sfk;
17495 bool abstract_ok;
17496 bool pack_expansion_p = false;
17497 cp_token *declarator_id_start_token;
17499 /* Parse a declarator-id */
17500 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17501 if (abstract_ok)
17503 cp_parser_parse_tentatively (parser);
17505 /* If we see an ellipsis, we should be looking at a
17506 parameter pack. */
17507 if (token->type == CPP_ELLIPSIS)
17509 /* Consume the `...' */
17510 cp_lexer_consume_token (parser->lexer);
17512 pack_expansion_p = true;
17516 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17517 unqualified_name
17518 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17519 qualifying_scope = parser->scope;
17520 if (abstract_ok)
17522 bool okay = false;
17524 if (!unqualified_name && pack_expansion_p)
17526 /* Check whether an error occurred. */
17527 okay = !cp_parser_error_occurred (parser);
17529 /* We already consumed the ellipsis to mark a
17530 parameter pack, but we have no way to report it,
17531 so abort the tentative parse. We will be exiting
17532 immediately anyway. */
17533 cp_parser_abort_tentative_parse (parser);
17535 else
17536 okay = cp_parser_parse_definitely (parser);
17538 if (!okay)
17539 unqualified_name = error_mark_node;
17540 else if (unqualified_name
17541 && (qualifying_scope
17542 || (!identifier_p (unqualified_name))))
17544 cp_parser_error (parser, "expected unqualified-id");
17545 unqualified_name = error_mark_node;
17549 if (!unqualified_name)
17550 return NULL;
17551 if (unqualified_name == error_mark_node)
17553 declarator = cp_error_declarator;
17554 pack_expansion_p = false;
17555 declarator->parameter_pack_p = false;
17556 break;
17559 attrs = cp_parser_std_attribute_spec_seq (parser);
17561 if (qualifying_scope && at_namespace_scope_p ()
17562 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17564 /* In the declaration of a member of a template class
17565 outside of the class itself, the SCOPE will sometimes
17566 be a TYPENAME_TYPE. For example, given:
17568 template <typename T>
17569 int S<T>::R::i = 3;
17571 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17572 this context, we must resolve S<T>::R to an ordinary
17573 type, rather than a typename type.
17575 The reason we normally avoid resolving TYPENAME_TYPEs
17576 is that a specialization of `S' might render
17577 `S<T>::R' not a type. However, if `S' is
17578 specialized, then this `i' will not be used, so there
17579 is no harm in resolving the types here. */
17580 tree type;
17582 /* Resolve the TYPENAME_TYPE. */
17583 type = resolve_typename_type (qualifying_scope,
17584 /*only_current_p=*/false);
17585 /* If that failed, the declarator is invalid. */
17586 if (TREE_CODE (type) == TYPENAME_TYPE)
17588 if (typedef_variant_p (type))
17589 error_at (declarator_id_start_token->location,
17590 "cannot define member of dependent typedef "
17591 "%qT", type);
17592 else
17593 error_at (declarator_id_start_token->location,
17594 "%<%T::%E%> is not a type",
17595 TYPE_CONTEXT (qualifying_scope),
17596 TYPE_IDENTIFIER (qualifying_scope));
17598 qualifying_scope = type;
17601 sfk = sfk_none;
17603 if (unqualified_name)
17605 tree class_type;
17607 if (qualifying_scope
17608 && CLASS_TYPE_P (qualifying_scope))
17609 class_type = qualifying_scope;
17610 else
17611 class_type = current_class_type;
17613 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17615 tree name_type = TREE_TYPE (unqualified_name);
17616 if (class_type && same_type_p (name_type, class_type))
17618 if (qualifying_scope
17619 && CLASSTYPE_USE_TEMPLATE (name_type))
17621 error_at (declarator_id_start_token->location,
17622 "invalid use of constructor as a template");
17623 inform (declarator_id_start_token->location,
17624 "use %<%T::%D%> instead of %<%T::%D%> to "
17625 "name the constructor in a qualified name",
17626 class_type,
17627 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17628 class_type, name_type);
17629 declarator = cp_error_declarator;
17630 break;
17632 else
17633 unqualified_name = constructor_name (class_type);
17635 else
17637 /* We do not attempt to print the declarator
17638 here because we do not have enough
17639 information about its original syntactic
17640 form. */
17641 cp_parser_error (parser, "invalid declarator");
17642 declarator = cp_error_declarator;
17643 break;
17647 if (class_type)
17649 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17650 sfk = sfk_destructor;
17651 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17652 sfk = sfk_conversion;
17653 else if (/* There's no way to declare a constructor
17654 for an anonymous type, even if the type
17655 got a name for linkage purposes. */
17656 !TYPE_WAS_ANONYMOUS (class_type)
17657 /* Handle correctly (c++/19200):
17659 struct S {
17660 struct T{};
17661 friend void S(T);
17664 and also:
17666 namespace N {
17667 void S();
17670 struct S {
17671 friend void N::S();
17672 }; */
17673 && !(friend_p
17674 && class_type != qualifying_scope)
17675 && constructor_name_p (unqualified_name,
17676 class_type))
17678 unqualified_name = constructor_name (class_type);
17679 sfk = sfk_constructor;
17681 else if (is_overloaded_fn (unqualified_name)
17682 && DECL_CONSTRUCTOR_P (get_first_fn
17683 (unqualified_name)))
17684 sfk = sfk_constructor;
17686 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17687 *ctor_dtor_or_conv_p = -1;
17690 declarator = make_id_declarator (qualifying_scope,
17691 unqualified_name,
17692 sfk);
17693 declarator->std_attributes = attrs;
17694 declarator->id_loc = token->location;
17695 declarator->parameter_pack_p = pack_expansion_p;
17697 if (pack_expansion_p)
17698 maybe_warn_variadic_templates ();
17701 handle_declarator:;
17702 scope = get_scope_of_declarator (declarator);
17703 if (scope)
17705 /* Any names that appear after the declarator-id for a
17706 member are looked up in the containing scope. */
17707 if (at_function_scope_p ())
17709 /* But declarations with qualified-ids can't appear in a
17710 function. */
17711 cp_parser_error (parser, "qualified-id in declaration");
17712 declarator = cp_error_declarator;
17713 break;
17715 pushed_scope = push_scope (scope);
17717 parser->in_declarator_p = true;
17718 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17719 || (declarator && declarator->kind == cdk_id))
17720 /* Default args are only allowed on function
17721 declarations. */
17722 parser->default_arg_ok_p = saved_default_arg_ok_p;
17723 else
17724 parser->default_arg_ok_p = false;
17726 first = false;
17728 /* We're done. */
17729 else
17730 break;
17733 /* For an abstract declarator, we might wind up with nothing at this
17734 point. That's an error; the declarator is not optional. */
17735 if (!declarator)
17736 cp_parser_error (parser, "expected declarator");
17738 /* If we entered a scope, we must exit it now. */
17739 if (pushed_scope)
17740 pop_scope (pushed_scope);
17742 parser->default_arg_ok_p = saved_default_arg_ok_p;
17743 parser->in_declarator_p = saved_in_declarator_p;
17745 return declarator;
17748 /* Parse a ptr-operator.
17750 ptr-operator:
17751 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17752 * cv-qualifier-seq [opt]
17754 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17755 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17757 GNU Extension:
17759 ptr-operator:
17760 & cv-qualifier-seq [opt]
17762 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17763 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17764 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17765 filled in with the TYPE containing the member. *CV_QUALS is
17766 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17767 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17768 Note that the tree codes returned by this function have nothing
17769 to do with the types of trees that will be eventually be created
17770 to represent the pointer or reference type being parsed. They are
17771 just constants with suggestive names. */
17772 static enum tree_code
17773 cp_parser_ptr_operator (cp_parser* parser,
17774 tree* type,
17775 cp_cv_quals *cv_quals,
17776 tree *attributes)
17778 enum tree_code code = ERROR_MARK;
17779 cp_token *token;
17780 tree attrs = NULL_TREE;
17782 /* Assume that it's not a pointer-to-member. */
17783 *type = NULL_TREE;
17784 /* And that there are no cv-qualifiers. */
17785 *cv_quals = TYPE_UNQUALIFIED;
17787 /* Peek at the next token. */
17788 token = cp_lexer_peek_token (parser->lexer);
17790 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17791 if (token->type == CPP_MULT)
17792 code = INDIRECT_REF;
17793 else if (token->type == CPP_AND)
17794 code = ADDR_EXPR;
17795 else if ((cxx_dialect != cxx98) &&
17796 token->type == CPP_AND_AND) /* C++0x only */
17797 code = NON_LVALUE_EXPR;
17799 if (code != ERROR_MARK)
17801 /* Consume the `*', `&' or `&&'. */
17802 cp_lexer_consume_token (parser->lexer);
17804 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17805 `&', if we are allowing GNU extensions. (The only qualifier
17806 that can legally appear after `&' is `restrict', but that is
17807 enforced during semantic analysis. */
17808 if (code == INDIRECT_REF
17809 || cp_parser_allow_gnu_extensions_p (parser))
17810 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17812 attrs = cp_parser_std_attribute_spec_seq (parser);
17813 if (attributes != NULL)
17814 *attributes = attrs;
17816 else
17818 /* Try the pointer-to-member case. */
17819 cp_parser_parse_tentatively (parser);
17820 /* Look for the optional `::' operator. */
17821 cp_parser_global_scope_opt (parser,
17822 /*current_scope_valid_p=*/false);
17823 /* Look for the nested-name specifier. */
17824 token = cp_lexer_peek_token (parser->lexer);
17825 cp_parser_nested_name_specifier (parser,
17826 /*typename_keyword_p=*/false,
17827 /*check_dependency_p=*/true,
17828 /*type_p=*/false,
17829 /*is_declaration=*/false);
17830 /* If we found it, and the next token is a `*', then we are
17831 indeed looking at a pointer-to-member operator. */
17832 if (!cp_parser_error_occurred (parser)
17833 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17835 /* Indicate that the `*' operator was used. */
17836 code = INDIRECT_REF;
17838 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17839 error_at (token->location, "%qD is a namespace", parser->scope);
17840 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17841 error_at (token->location, "cannot form pointer to member of "
17842 "non-class %q#T", parser->scope);
17843 else
17845 /* The type of which the member is a member is given by the
17846 current SCOPE. */
17847 *type = parser->scope;
17848 /* The next name will not be qualified. */
17849 parser->scope = NULL_TREE;
17850 parser->qualifying_scope = NULL_TREE;
17851 parser->object_scope = NULL_TREE;
17852 /* Look for optional c++11 attributes. */
17853 attrs = cp_parser_std_attribute_spec_seq (parser);
17854 if (attributes != NULL)
17855 *attributes = attrs;
17856 /* Look for the optional cv-qualifier-seq. */
17857 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17860 /* If that didn't work we don't have a ptr-operator. */
17861 if (!cp_parser_parse_definitely (parser))
17862 cp_parser_error (parser, "expected ptr-operator");
17865 return code;
17868 /* Parse an (optional) cv-qualifier-seq.
17870 cv-qualifier-seq:
17871 cv-qualifier cv-qualifier-seq [opt]
17873 cv-qualifier:
17874 const
17875 volatile
17877 GNU Extension:
17879 cv-qualifier:
17880 __restrict__
17882 Returns a bitmask representing the cv-qualifiers. */
17884 static cp_cv_quals
17885 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17887 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17889 while (true)
17891 cp_token *token;
17892 cp_cv_quals cv_qualifier;
17894 /* Peek at the next token. */
17895 token = cp_lexer_peek_token (parser->lexer);
17896 /* See if it's a cv-qualifier. */
17897 switch (token->keyword)
17899 case RID_CONST:
17900 cv_qualifier = TYPE_QUAL_CONST;
17901 break;
17903 case RID_VOLATILE:
17904 cv_qualifier = TYPE_QUAL_VOLATILE;
17905 break;
17907 case RID_RESTRICT:
17908 cv_qualifier = TYPE_QUAL_RESTRICT;
17909 break;
17911 default:
17912 cv_qualifier = TYPE_UNQUALIFIED;
17913 break;
17916 if (!cv_qualifier)
17917 break;
17919 if (cv_quals & cv_qualifier)
17921 error_at (token->location, "duplicate cv-qualifier");
17922 cp_lexer_purge_token (parser->lexer);
17924 else
17926 cp_lexer_consume_token (parser->lexer);
17927 cv_quals |= cv_qualifier;
17931 return cv_quals;
17934 /* Parse an (optional) ref-qualifier
17936 ref-qualifier:
17940 Returns cp_ref_qualifier representing ref-qualifier. */
17942 static cp_ref_qualifier
17943 cp_parser_ref_qualifier_opt (cp_parser* parser)
17945 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17947 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17948 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17949 return ref_qual;
17951 while (true)
17953 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17954 cp_token *token = cp_lexer_peek_token (parser->lexer);
17956 switch (token->type)
17958 case CPP_AND:
17959 curr_ref_qual = REF_QUAL_LVALUE;
17960 break;
17962 case CPP_AND_AND:
17963 curr_ref_qual = REF_QUAL_RVALUE;
17964 break;
17966 default:
17967 curr_ref_qual = REF_QUAL_NONE;
17968 break;
17971 if (!curr_ref_qual)
17972 break;
17973 else if (ref_qual)
17975 error_at (token->location, "multiple ref-qualifiers");
17976 cp_lexer_purge_token (parser->lexer);
17978 else
17980 ref_qual = curr_ref_qual;
17981 cp_lexer_consume_token (parser->lexer);
17985 return ref_qual;
17988 /* Parse an (optional) virt-specifier-seq.
17990 virt-specifier-seq:
17991 virt-specifier virt-specifier-seq [opt]
17993 virt-specifier:
17994 override
17995 final
17997 Returns a bitmask representing the virt-specifiers. */
17999 static cp_virt_specifiers
18000 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18002 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18004 while (true)
18006 cp_token *token;
18007 cp_virt_specifiers virt_specifier;
18009 /* Peek at the next token. */
18010 token = cp_lexer_peek_token (parser->lexer);
18011 /* See if it's a virt-specifier-qualifier. */
18012 if (token->type != CPP_NAME)
18013 break;
18014 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18016 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18017 virt_specifier = VIRT_SPEC_OVERRIDE;
18019 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18021 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18022 virt_specifier = VIRT_SPEC_FINAL;
18024 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18026 virt_specifier = VIRT_SPEC_FINAL;
18028 else
18029 break;
18031 if (virt_specifiers & virt_specifier)
18033 error_at (token->location, "duplicate virt-specifier");
18034 cp_lexer_purge_token (parser->lexer);
18036 else
18038 cp_lexer_consume_token (parser->lexer);
18039 virt_specifiers |= virt_specifier;
18042 return virt_specifiers;
18045 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18046 is in scope even though it isn't real. */
18048 void
18049 inject_this_parameter (tree ctype, cp_cv_quals quals)
18051 tree this_parm;
18053 if (current_class_ptr)
18055 /* We don't clear this between NSDMIs. Is it already what we want? */
18056 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18057 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18058 && cp_type_quals (type) == quals)
18059 return;
18062 this_parm = build_this_parm (ctype, quals);
18063 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18064 current_class_ptr = NULL_TREE;
18065 current_class_ref
18066 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18067 current_class_ptr = this_parm;
18070 /* Return true iff our current scope is a non-static data member
18071 initializer. */
18073 bool
18074 parsing_nsdmi (void)
18076 /* We recognize NSDMI context by the context-less 'this' pointer set up
18077 by the function above. */
18078 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18079 return true;
18080 return false;
18083 /* Parse a late-specified return type, if any. This is not a separate
18084 non-terminal, but part of a function declarator, which looks like
18086 -> trailing-type-specifier-seq abstract-declarator(opt)
18088 Returns the type indicated by the type-id.
18090 In addition to this this parses any queued up omp declare simd
18091 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18093 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18094 function. */
18096 static tree
18097 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18098 cp_cv_quals quals)
18100 cp_token *token;
18101 tree type = NULL_TREE;
18102 bool declare_simd_p = (parser->omp_declare_simd
18103 && declarator
18104 && declarator->kind == cdk_id);
18106 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18107 && declarator && declarator->kind == cdk_id);
18109 /* Peek at the next token. */
18110 token = cp_lexer_peek_token (parser->lexer);
18111 /* A late-specified return type is indicated by an initial '->'. */
18112 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18113 return NULL_TREE;
18115 tree save_ccp = current_class_ptr;
18116 tree save_ccr = current_class_ref;
18117 if (quals >= 0)
18119 /* DR 1207: 'this' is in scope in the trailing return type. */
18120 inject_this_parameter (current_class_type, quals);
18123 if (token->type == CPP_DEREF)
18125 /* Consume the ->. */
18126 cp_lexer_consume_token (parser->lexer);
18128 type = cp_parser_trailing_type_id (parser);
18131 if (cilk_simd_fn_vector_p)
18132 declarator->std_attributes
18133 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18134 declarator->std_attributes);
18135 if (declare_simd_p)
18136 declarator->std_attributes
18137 = cp_parser_late_parsing_omp_declare_simd (parser,
18138 declarator->std_attributes);
18140 if (quals >= 0)
18142 current_class_ptr = save_ccp;
18143 current_class_ref = save_ccr;
18146 return type;
18149 /* Parse a declarator-id.
18151 declarator-id:
18152 id-expression
18153 :: [opt] nested-name-specifier [opt] type-name
18155 In the `id-expression' case, the value returned is as for
18156 cp_parser_id_expression if the id-expression was an unqualified-id.
18157 If the id-expression was a qualified-id, then a SCOPE_REF is
18158 returned. The first operand is the scope (either a NAMESPACE_DECL
18159 or TREE_TYPE), but the second is still just a representation of an
18160 unqualified-id. */
18162 static tree
18163 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18165 tree id;
18166 /* The expression must be an id-expression. Assume that qualified
18167 names are the names of types so that:
18169 template <class T>
18170 int S<T>::R::i = 3;
18172 will work; we must treat `S<T>::R' as the name of a type.
18173 Similarly, assume that qualified names are templates, where
18174 required, so that:
18176 template <class T>
18177 int S<T>::R<T>::i = 3;
18179 will work, too. */
18180 id = cp_parser_id_expression (parser,
18181 /*template_keyword_p=*/false,
18182 /*check_dependency_p=*/false,
18183 /*template_p=*/NULL,
18184 /*declarator_p=*/true,
18185 optional_p);
18186 if (id && BASELINK_P (id))
18187 id = BASELINK_FUNCTIONS (id);
18188 return id;
18191 /* Parse a type-id.
18193 type-id:
18194 type-specifier-seq abstract-declarator [opt]
18196 Returns the TYPE specified. */
18198 static tree
18199 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18200 bool is_trailing_return)
18202 cp_decl_specifier_seq type_specifier_seq;
18203 cp_declarator *abstract_declarator;
18205 /* Parse the type-specifier-seq. */
18206 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18207 is_trailing_return,
18208 &type_specifier_seq);
18209 if (type_specifier_seq.type == error_mark_node)
18210 return error_mark_node;
18212 /* There might or might not be an abstract declarator. */
18213 cp_parser_parse_tentatively (parser);
18214 /* Look for the declarator. */
18215 abstract_declarator
18216 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18217 /*parenthesized_p=*/NULL,
18218 /*member_p=*/false,
18219 /*friend_p=*/false);
18220 /* Check to see if there really was a declarator. */
18221 if (!cp_parser_parse_definitely (parser))
18222 abstract_declarator = NULL;
18224 if (type_specifier_seq.type
18225 /* None of the valid uses of 'auto' in C++14 involve the type-id
18226 nonterminal, but it is valid in a trailing-return-type. */
18227 && !(cxx_dialect >= cxx1y && is_trailing_return)
18228 && type_uses_auto (type_specifier_seq.type))
18230 /* A type-id with type 'auto' is only ok if the abstract declarator
18231 is a function declarator with a late-specified return type. */
18232 if (abstract_declarator
18233 && abstract_declarator->kind == cdk_function
18234 && abstract_declarator->u.function.late_return_type)
18235 /* OK */;
18236 else
18238 error ("invalid use of %<auto%>");
18239 return error_mark_node;
18243 return groktypename (&type_specifier_seq, abstract_declarator,
18244 is_template_arg);
18247 static tree cp_parser_type_id (cp_parser *parser)
18249 return cp_parser_type_id_1 (parser, false, false);
18252 static tree cp_parser_template_type_arg (cp_parser *parser)
18254 tree r;
18255 const char *saved_message = parser->type_definition_forbidden_message;
18256 parser->type_definition_forbidden_message
18257 = G_("types may not be defined in template arguments");
18258 r = cp_parser_type_id_1 (parser, true, false);
18259 parser->type_definition_forbidden_message = saved_message;
18260 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18262 error ("invalid use of %<auto%> in template argument");
18263 r = error_mark_node;
18265 return r;
18268 static tree cp_parser_trailing_type_id (cp_parser *parser)
18270 return cp_parser_type_id_1 (parser, false, true);
18273 /* Parse a type-specifier-seq.
18275 type-specifier-seq:
18276 type-specifier type-specifier-seq [opt]
18278 GNU extension:
18280 type-specifier-seq:
18281 attributes type-specifier-seq [opt]
18283 If IS_DECLARATION is true, we are at the start of a "condition" or
18284 exception-declaration, so we might be followed by a declarator-id.
18286 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18287 i.e. we've just seen "->".
18289 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18291 static void
18292 cp_parser_type_specifier_seq (cp_parser* parser,
18293 bool is_declaration,
18294 bool is_trailing_return,
18295 cp_decl_specifier_seq *type_specifier_seq)
18297 bool seen_type_specifier = false;
18298 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18299 cp_token *start_token = NULL;
18301 /* Clear the TYPE_SPECIFIER_SEQ. */
18302 clear_decl_specs (type_specifier_seq);
18304 /* In the context of a trailing return type, enum E { } is an
18305 elaborated-type-specifier followed by a function-body, not an
18306 enum-specifier. */
18307 if (is_trailing_return)
18308 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18310 /* Parse the type-specifiers and attributes. */
18311 while (true)
18313 tree type_specifier;
18314 bool is_cv_qualifier;
18316 /* Check for attributes first. */
18317 if (cp_next_tokens_can_be_attribute_p (parser))
18319 type_specifier_seq->attributes =
18320 chainon (type_specifier_seq->attributes,
18321 cp_parser_attributes_opt (parser));
18322 continue;
18325 /* record the token of the beginning of the type specifier seq,
18326 for error reporting purposes*/
18327 if (!start_token)
18328 start_token = cp_lexer_peek_token (parser->lexer);
18330 /* Look for the type-specifier. */
18331 type_specifier = cp_parser_type_specifier (parser,
18332 flags,
18333 type_specifier_seq,
18334 /*is_declaration=*/false,
18335 NULL,
18336 &is_cv_qualifier);
18337 if (!type_specifier)
18339 /* If the first type-specifier could not be found, this is not a
18340 type-specifier-seq at all. */
18341 if (!seen_type_specifier)
18343 /* Set in_declarator_p to avoid skipping to the semicolon. */
18344 int in_decl = parser->in_declarator_p;
18345 parser->in_declarator_p = true;
18347 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18348 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18349 cp_parser_error (parser, "expected type-specifier");
18351 parser->in_declarator_p = in_decl;
18353 type_specifier_seq->type = error_mark_node;
18354 return;
18356 /* If subsequent type-specifiers could not be found, the
18357 type-specifier-seq is complete. */
18358 break;
18361 seen_type_specifier = true;
18362 /* The standard says that a condition can be:
18364 type-specifier-seq declarator = assignment-expression
18366 However, given:
18368 struct S {};
18369 if (int S = ...)
18371 we should treat the "S" as a declarator, not as a
18372 type-specifier. The standard doesn't say that explicitly for
18373 type-specifier-seq, but it does say that for
18374 decl-specifier-seq in an ordinary declaration. Perhaps it
18375 would be clearer just to allow a decl-specifier-seq here, and
18376 then add a semantic restriction that if any decl-specifiers
18377 that are not type-specifiers appear, the program is invalid. */
18378 if (is_declaration && !is_cv_qualifier)
18379 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18383 /* Return whether the function currently being declared has an associated
18384 template parameter list. */
18386 static bool
18387 function_being_declared_is_template_p (cp_parser* parser)
18389 if (!current_template_parms || processing_template_parmlist)
18390 return false;
18392 if (parser->implicit_template_scope)
18393 return true;
18395 if (at_class_scope_p ()
18396 && TYPE_BEING_DEFINED (current_class_type))
18397 return parser->num_template_parameter_lists != 0;
18399 return ((int) parser->num_template_parameter_lists > template_class_depth
18400 (current_class_type));
18403 /* Parse a parameter-declaration-clause.
18405 parameter-declaration-clause:
18406 parameter-declaration-list [opt] ... [opt]
18407 parameter-declaration-list , ...
18409 Returns a representation for the parameter declarations. A return
18410 value of NULL indicates a parameter-declaration-clause consisting
18411 only of an ellipsis. */
18413 static tree
18414 cp_parser_parameter_declaration_clause (cp_parser* parser)
18416 tree parameters;
18417 cp_token *token;
18418 bool ellipsis_p;
18419 bool is_error;
18421 struct cleanup {
18422 cp_parser* parser;
18423 int auto_is_implicit_function_template_parm_p;
18424 ~cleanup() {
18425 parser->auto_is_implicit_function_template_parm_p
18426 = auto_is_implicit_function_template_parm_p;
18428 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18430 (void) cleanup;
18432 if (!processing_specialization
18433 && !processing_template_parmlist
18434 && !processing_explicit_instantiation)
18435 if (!current_function_decl
18436 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18437 parser->auto_is_implicit_function_template_parm_p = true;
18439 /* Peek at the next token. */
18440 token = cp_lexer_peek_token (parser->lexer);
18441 /* Check for trivial parameter-declaration-clauses. */
18442 if (token->type == CPP_ELLIPSIS)
18444 /* Consume the `...' token. */
18445 cp_lexer_consume_token (parser->lexer);
18446 return NULL_TREE;
18448 else if (token->type == CPP_CLOSE_PAREN)
18449 /* There are no parameters. */
18451 #ifndef NO_IMPLICIT_EXTERN_C
18452 if (in_system_header_at (input_location)
18453 && current_class_type == NULL
18454 && current_lang_name == lang_name_c)
18455 return NULL_TREE;
18456 else
18457 #endif
18458 return void_list_node;
18460 /* Check for `(void)', too, which is a special case. */
18461 else if (token->keyword == RID_VOID
18462 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18463 == CPP_CLOSE_PAREN))
18465 /* Consume the `void' token. */
18466 cp_lexer_consume_token (parser->lexer);
18467 /* There are no parameters. */
18468 return void_list_node;
18471 /* Parse the parameter-declaration-list. */
18472 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18473 /* If a parse error occurred while parsing the
18474 parameter-declaration-list, then the entire
18475 parameter-declaration-clause is erroneous. */
18476 if (is_error)
18477 return NULL;
18479 /* Peek at the next token. */
18480 token = cp_lexer_peek_token (parser->lexer);
18481 /* If it's a `,', the clause should terminate with an ellipsis. */
18482 if (token->type == CPP_COMMA)
18484 /* Consume the `,'. */
18485 cp_lexer_consume_token (parser->lexer);
18486 /* Expect an ellipsis. */
18487 ellipsis_p
18488 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18490 /* It might also be `...' if the optional trailing `,' was
18491 omitted. */
18492 else if (token->type == CPP_ELLIPSIS)
18494 /* Consume the `...' token. */
18495 cp_lexer_consume_token (parser->lexer);
18496 /* And remember that we saw it. */
18497 ellipsis_p = true;
18499 else
18500 ellipsis_p = false;
18502 /* Finish the parameter list. */
18503 if (!ellipsis_p)
18504 parameters = chainon (parameters, void_list_node);
18506 return parameters;
18509 /* Parse a parameter-declaration-list.
18511 parameter-declaration-list:
18512 parameter-declaration
18513 parameter-declaration-list , parameter-declaration
18515 Returns a representation of the parameter-declaration-list, as for
18516 cp_parser_parameter_declaration_clause. However, the
18517 `void_list_node' is never appended to the list. Upon return,
18518 *IS_ERROR will be true iff an error occurred. */
18520 static tree
18521 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18523 tree parameters = NULL_TREE;
18524 tree *tail = &parameters;
18525 bool saved_in_unbraced_linkage_specification_p;
18526 int index = 0;
18528 /* Assume all will go well. */
18529 *is_error = false;
18530 /* The special considerations that apply to a function within an
18531 unbraced linkage specifications do not apply to the parameters
18532 to the function. */
18533 saved_in_unbraced_linkage_specification_p
18534 = parser->in_unbraced_linkage_specification_p;
18535 parser->in_unbraced_linkage_specification_p = false;
18537 /* Look for more parameters. */
18538 while (true)
18540 cp_parameter_declarator *parameter;
18541 tree decl = error_mark_node;
18542 bool parenthesized_p = false;
18543 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18544 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18545 (current_template_parms)) : 0);
18547 /* Parse the parameter. */
18548 parameter
18549 = cp_parser_parameter_declaration (parser,
18550 /*template_parm_p=*/false,
18551 &parenthesized_p);
18553 /* We don't know yet if the enclosing context is deprecated, so wait
18554 and warn in grokparms if appropriate. */
18555 deprecated_state = DEPRECATED_SUPPRESS;
18557 if (parameter)
18559 /* If a function parameter pack was specified and an implicit template
18560 parameter was introduced during cp_parser_parameter_declaration,
18561 change any implicit parameters introduced into packs. */
18562 if (parser->implicit_template_parms
18563 && parameter->declarator
18564 && parameter->declarator->parameter_pack_p)
18566 int latest_template_parm_idx = TREE_VEC_LENGTH
18567 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18569 if (latest_template_parm_idx != template_parm_idx)
18570 parameter->decl_specifiers.type = convert_generic_types_to_packs
18571 (parameter->decl_specifiers.type,
18572 template_parm_idx, latest_template_parm_idx);
18575 decl = grokdeclarator (parameter->declarator,
18576 &parameter->decl_specifiers,
18577 PARM,
18578 parameter->default_argument != NULL_TREE,
18579 &parameter->decl_specifiers.attributes);
18582 deprecated_state = DEPRECATED_NORMAL;
18584 /* If a parse error occurred parsing the parameter declaration,
18585 then the entire parameter-declaration-list is erroneous. */
18586 if (decl == error_mark_node)
18588 *is_error = true;
18589 parameters = error_mark_node;
18590 break;
18593 if (parameter->decl_specifiers.attributes)
18594 cplus_decl_attributes (&decl,
18595 parameter->decl_specifiers.attributes,
18597 if (DECL_NAME (decl))
18598 decl = pushdecl (decl);
18600 if (decl != error_mark_node)
18602 retrofit_lang_decl (decl);
18603 DECL_PARM_INDEX (decl) = ++index;
18604 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18607 /* Add the new parameter to the list. */
18608 *tail = build_tree_list (parameter->default_argument, decl);
18609 tail = &TREE_CHAIN (*tail);
18611 /* Peek at the next token. */
18612 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18613 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18614 /* These are for Objective-C++ */
18615 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18616 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18617 /* The parameter-declaration-list is complete. */
18618 break;
18619 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18621 cp_token *token;
18623 /* Peek at the next token. */
18624 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18625 /* If it's an ellipsis, then the list is complete. */
18626 if (token->type == CPP_ELLIPSIS)
18627 break;
18628 /* Otherwise, there must be more parameters. Consume the
18629 `,'. */
18630 cp_lexer_consume_token (parser->lexer);
18631 /* When parsing something like:
18633 int i(float f, double d)
18635 we can tell after seeing the declaration for "f" that we
18636 are not looking at an initialization of a variable "i",
18637 but rather at the declaration of a function "i".
18639 Due to the fact that the parsing of template arguments
18640 (as specified to a template-id) requires backtracking we
18641 cannot use this technique when inside a template argument
18642 list. */
18643 if (!parser->in_template_argument_list_p
18644 && !parser->in_type_id_in_expr_p
18645 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18646 /* However, a parameter-declaration of the form
18647 "float(f)" (which is a valid declaration of a
18648 parameter "f") can also be interpreted as an
18649 expression (the conversion of "f" to "float"). */
18650 && !parenthesized_p)
18651 cp_parser_commit_to_tentative_parse (parser);
18653 else
18655 cp_parser_error (parser, "expected %<,%> or %<...%>");
18656 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18657 cp_parser_skip_to_closing_parenthesis (parser,
18658 /*recovering=*/true,
18659 /*or_comma=*/false,
18660 /*consume_paren=*/false);
18661 break;
18665 parser->in_unbraced_linkage_specification_p
18666 = saved_in_unbraced_linkage_specification_p;
18668 /* Reset implicit_template_scope if we are about to leave the function
18669 parameter list that introduced it. Note that for out-of-line member
18670 definitions, there will be one or more class scopes before we get to
18671 the template parameter scope. */
18673 if (cp_binding_level *its = parser->implicit_template_scope)
18674 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18676 while (maybe_its->kind == sk_class)
18677 maybe_its = maybe_its->level_chain;
18678 if (maybe_its == its)
18680 parser->implicit_template_parms = 0;
18681 parser->implicit_template_scope = 0;
18685 return parameters;
18688 /* Parse a parameter declaration.
18690 parameter-declaration:
18691 decl-specifier-seq ... [opt] declarator
18692 decl-specifier-seq declarator = assignment-expression
18693 decl-specifier-seq ... [opt] abstract-declarator [opt]
18694 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18696 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18697 declares a template parameter. (In that case, a non-nested `>'
18698 token encountered during the parsing of the assignment-expression
18699 is not interpreted as a greater-than operator.)
18701 Returns a representation of the parameter, or NULL if an error
18702 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18703 true iff the declarator is of the form "(p)". */
18705 static cp_parameter_declarator *
18706 cp_parser_parameter_declaration (cp_parser *parser,
18707 bool template_parm_p,
18708 bool *parenthesized_p)
18710 int declares_class_or_enum;
18711 cp_decl_specifier_seq decl_specifiers;
18712 cp_declarator *declarator;
18713 tree default_argument;
18714 cp_token *token = NULL, *declarator_token_start = NULL;
18715 const char *saved_message;
18717 /* In a template parameter, `>' is not an operator.
18719 [temp.param]
18721 When parsing a default template-argument for a non-type
18722 template-parameter, the first non-nested `>' is taken as the end
18723 of the template parameter-list rather than a greater-than
18724 operator. */
18726 /* Type definitions may not appear in parameter types. */
18727 saved_message = parser->type_definition_forbidden_message;
18728 parser->type_definition_forbidden_message
18729 = G_("types may not be defined in parameter types");
18731 /* Parse the declaration-specifiers. */
18732 cp_parser_decl_specifier_seq (parser,
18733 CP_PARSER_FLAGS_NONE,
18734 &decl_specifiers,
18735 &declares_class_or_enum);
18737 /* Complain about missing 'typename' or other invalid type names. */
18738 if (!decl_specifiers.any_type_specifiers_p
18739 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18740 decl_specifiers.type = error_mark_node;
18742 /* If an error occurred, there's no reason to attempt to parse the
18743 rest of the declaration. */
18744 if (cp_parser_error_occurred (parser))
18746 parser->type_definition_forbidden_message = saved_message;
18747 return NULL;
18750 /* Peek at the next token. */
18751 token = cp_lexer_peek_token (parser->lexer);
18753 /* If the next token is a `)', `,', `=', `>', or `...', then there
18754 is no declarator. However, when variadic templates are enabled,
18755 there may be a declarator following `...'. */
18756 if (token->type == CPP_CLOSE_PAREN
18757 || token->type == CPP_COMMA
18758 || token->type == CPP_EQ
18759 || token->type == CPP_GREATER)
18761 declarator = NULL;
18762 if (parenthesized_p)
18763 *parenthesized_p = false;
18765 /* Otherwise, there should be a declarator. */
18766 else
18768 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18769 parser->default_arg_ok_p = false;
18771 /* After seeing a decl-specifier-seq, if the next token is not a
18772 "(", there is no possibility that the code is a valid
18773 expression. Therefore, if parsing tentatively, we commit at
18774 this point. */
18775 if (!parser->in_template_argument_list_p
18776 /* In an expression context, having seen:
18778 (int((char ...
18780 we cannot be sure whether we are looking at a
18781 function-type (taking a "char" as a parameter) or a cast
18782 of some object of type "char" to "int". */
18783 && !parser->in_type_id_in_expr_p
18784 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18785 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18786 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18787 cp_parser_commit_to_tentative_parse (parser);
18788 /* Parse the declarator. */
18789 declarator_token_start = token;
18790 declarator = cp_parser_declarator (parser,
18791 CP_PARSER_DECLARATOR_EITHER,
18792 /*ctor_dtor_or_conv_p=*/NULL,
18793 parenthesized_p,
18794 /*member_p=*/false,
18795 /*friend_p=*/false);
18796 parser->default_arg_ok_p = saved_default_arg_ok_p;
18797 /* After the declarator, allow more attributes. */
18798 decl_specifiers.attributes
18799 = chainon (decl_specifiers.attributes,
18800 cp_parser_attributes_opt (parser));
18803 /* If the next token is an ellipsis, and we have not seen a
18804 declarator name, and the type of the declarator contains parameter
18805 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18806 a parameter pack expansion expression. Otherwise, leave the
18807 ellipsis for a C-style variadic function. */
18808 token = cp_lexer_peek_token (parser->lexer);
18809 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18811 tree type = decl_specifiers.type;
18813 if (type && DECL_P (type))
18814 type = TREE_TYPE (type);
18816 if (type
18817 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18818 && declarator_can_be_parameter_pack (declarator)
18819 && (!declarator || !declarator->parameter_pack_p)
18820 && uses_parameter_packs (type))
18822 /* Consume the `...'. */
18823 cp_lexer_consume_token (parser->lexer);
18824 maybe_warn_variadic_templates ();
18826 /* Build a pack expansion type */
18827 if (declarator)
18828 declarator->parameter_pack_p = true;
18829 else
18830 decl_specifiers.type = make_pack_expansion (type);
18834 /* The restriction on defining new types applies only to the type
18835 of the parameter, not to the default argument. */
18836 parser->type_definition_forbidden_message = saved_message;
18838 /* If the next token is `=', then process a default argument. */
18839 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18841 token = cp_lexer_peek_token (parser->lexer);
18842 /* If we are defining a class, then the tokens that make up the
18843 default argument must be saved and processed later. */
18844 if (!template_parm_p && at_class_scope_p ()
18845 && TYPE_BEING_DEFINED (current_class_type)
18846 && !LAMBDA_TYPE_P (current_class_type))
18847 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18848 /* Outside of a class definition, we can just parse the
18849 assignment-expression. */
18850 else
18851 default_argument
18852 = cp_parser_default_argument (parser, template_parm_p);
18854 if (!parser->default_arg_ok_p)
18856 if (flag_permissive)
18857 warning (0, "deprecated use of default argument for parameter of non-function");
18858 else
18860 error_at (token->location,
18861 "default arguments are only "
18862 "permitted for function parameters");
18863 default_argument = NULL_TREE;
18866 else if ((declarator && declarator->parameter_pack_p)
18867 || (decl_specifiers.type
18868 && PACK_EXPANSION_P (decl_specifiers.type)))
18870 /* Find the name of the parameter pack. */
18871 cp_declarator *id_declarator = declarator;
18872 while (id_declarator && id_declarator->kind != cdk_id)
18873 id_declarator = id_declarator->declarator;
18875 if (id_declarator && id_declarator->kind == cdk_id)
18876 error_at (declarator_token_start->location,
18877 template_parm_p
18878 ? G_("template parameter pack %qD "
18879 "cannot have a default argument")
18880 : G_("parameter pack %qD cannot have "
18881 "a default argument"),
18882 id_declarator->u.id.unqualified_name);
18883 else
18884 error_at (declarator_token_start->location,
18885 template_parm_p
18886 ? G_("template parameter pack cannot have "
18887 "a default argument")
18888 : G_("parameter pack cannot have a "
18889 "default argument"));
18891 default_argument = NULL_TREE;
18894 else
18895 default_argument = NULL_TREE;
18897 return make_parameter_declarator (&decl_specifiers,
18898 declarator,
18899 default_argument);
18902 /* Parse a default argument and return it.
18904 TEMPLATE_PARM_P is true if this is a default argument for a
18905 non-type template parameter. */
18906 static tree
18907 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18909 tree default_argument = NULL_TREE;
18910 bool saved_greater_than_is_operator_p;
18911 bool saved_local_variables_forbidden_p;
18912 bool non_constant_p, is_direct_init;
18914 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18915 set correctly. */
18916 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18917 parser->greater_than_is_operator_p = !template_parm_p;
18918 /* Local variable names (and the `this' keyword) may not
18919 appear in a default argument. */
18920 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18921 parser->local_variables_forbidden_p = true;
18922 /* Parse the assignment-expression. */
18923 if (template_parm_p)
18924 push_deferring_access_checks (dk_no_deferred);
18925 tree saved_class_ptr = NULL_TREE;
18926 tree saved_class_ref = NULL_TREE;
18927 /* The "this" pointer is not valid in a default argument. */
18928 if (cfun)
18930 saved_class_ptr = current_class_ptr;
18931 cp_function_chain->x_current_class_ptr = NULL_TREE;
18932 saved_class_ref = current_class_ref;
18933 cp_function_chain->x_current_class_ref = NULL_TREE;
18935 default_argument
18936 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18937 /* Restore the "this" pointer. */
18938 if (cfun)
18940 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18941 cp_function_chain->x_current_class_ref = saved_class_ref;
18943 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18944 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18945 if (template_parm_p)
18946 pop_deferring_access_checks ();
18947 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18948 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18950 return default_argument;
18953 /* Parse a function-body.
18955 function-body:
18956 compound_statement */
18958 static void
18959 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18961 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18964 /* Parse a ctor-initializer-opt followed by a function-body. Return
18965 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18966 is true we are parsing a function-try-block. */
18968 static bool
18969 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18970 bool in_function_try_block)
18972 tree body, list;
18973 bool ctor_initializer_p;
18974 const bool check_body_p =
18975 DECL_CONSTRUCTOR_P (current_function_decl)
18976 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18977 tree last = NULL;
18979 /* Begin the function body. */
18980 body = begin_function_body ();
18981 /* Parse the optional ctor-initializer. */
18982 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18984 /* If we're parsing a constexpr constructor definition, we need
18985 to check that the constructor body is indeed empty. However,
18986 before we get to cp_parser_function_body lot of junk has been
18987 generated, so we can't just check that we have an empty block.
18988 Rather we take a snapshot of the outermost block, and check whether
18989 cp_parser_function_body changed its state. */
18990 if (check_body_p)
18992 list = cur_stmt_list;
18993 if (STATEMENT_LIST_TAIL (list))
18994 last = STATEMENT_LIST_TAIL (list)->stmt;
18996 /* Parse the function-body. */
18997 cp_parser_function_body (parser, in_function_try_block);
18998 if (check_body_p)
18999 check_constexpr_ctor_body (last, list);
19000 /* Finish the function body. */
19001 finish_function_body (body);
19003 return ctor_initializer_p;
19006 /* Parse an initializer.
19008 initializer:
19009 = initializer-clause
19010 ( expression-list )
19012 Returns an expression representing the initializer. If no
19013 initializer is present, NULL_TREE is returned.
19015 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19016 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19017 set to TRUE if there is no initializer present. If there is an
19018 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19019 is set to true; otherwise it is set to false. */
19021 static tree
19022 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19023 bool* non_constant_p)
19025 cp_token *token;
19026 tree init;
19028 /* Peek at the next token. */
19029 token = cp_lexer_peek_token (parser->lexer);
19031 /* Let our caller know whether or not this initializer was
19032 parenthesized. */
19033 *is_direct_init = (token->type != CPP_EQ);
19034 /* Assume that the initializer is constant. */
19035 *non_constant_p = false;
19037 if (token->type == CPP_EQ)
19039 /* Consume the `='. */
19040 cp_lexer_consume_token (parser->lexer);
19041 /* Parse the initializer-clause. */
19042 init = cp_parser_initializer_clause (parser, non_constant_p);
19044 else if (token->type == CPP_OPEN_PAREN)
19046 vec<tree, va_gc> *vec;
19047 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19048 /*cast_p=*/false,
19049 /*allow_expansion_p=*/true,
19050 non_constant_p);
19051 if (vec == NULL)
19052 return error_mark_node;
19053 init = build_tree_list_vec (vec);
19054 release_tree_vector (vec);
19056 else if (token->type == CPP_OPEN_BRACE)
19058 cp_lexer_set_source_position (parser->lexer);
19059 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19060 init = cp_parser_braced_list (parser, non_constant_p);
19061 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19063 else
19065 /* Anything else is an error. */
19066 cp_parser_error (parser, "expected initializer");
19067 init = error_mark_node;
19070 return init;
19073 /* Parse an initializer-clause.
19075 initializer-clause:
19076 assignment-expression
19077 braced-init-list
19079 Returns an expression representing the initializer.
19081 If the `assignment-expression' production is used the value
19082 returned is simply a representation for the expression.
19084 Otherwise, calls cp_parser_braced_list. */
19086 static tree
19087 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19089 tree initializer;
19091 /* Assume the expression is constant. */
19092 *non_constant_p = false;
19094 /* If it is not a `{', then we are looking at an
19095 assignment-expression. */
19096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19098 initializer
19099 = cp_parser_constant_expression (parser,
19100 /*allow_non_constant_p=*/true,
19101 non_constant_p);
19103 else
19104 initializer = cp_parser_braced_list (parser, non_constant_p);
19106 return initializer;
19109 /* Parse a brace-enclosed initializer list.
19111 braced-init-list:
19112 { initializer-list , [opt] }
19115 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19116 the elements of the initializer-list (or NULL, if the last
19117 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19118 NULL_TREE. There is no way to detect whether or not the optional
19119 trailing `,' was provided. NON_CONSTANT_P is as for
19120 cp_parser_initializer. */
19122 static tree
19123 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19125 tree initializer;
19127 /* Consume the `{' token. */
19128 cp_lexer_consume_token (parser->lexer);
19129 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19130 initializer = make_node (CONSTRUCTOR);
19131 /* If it's not a `}', then there is a non-trivial initializer. */
19132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19134 /* Parse the initializer list. */
19135 CONSTRUCTOR_ELTS (initializer)
19136 = cp_parser_initializer_list (parser, non_constant_p);
19137 /* A trailing `,' token is allowed. */
19138 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19139 cp_lexer_consume_token (parser->lexer);
19141 else
19142 *non_constant_p = false;
19143 /* Now, there should be a trailing `}'. */
19144 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19145 TREE_TYPE (initializer) = init_list_type_node;
19146 return initializer;
19149 /* Parse an initializer-list.
19151 initializer-list:
19152 initializer-clause ... [opt]
19153 initializer-list , initializer-clause ... [opt]
19155 GNU Extension:
19157 initializer-list:
19158 designation initializer-clause ...[opt]
19159 initializer-list , designation initializer-clause ...[opt]
19161 designation:
19162 . identifier =
19163 identifier :
19164 [ constant-expression ] =
19166 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19167 for the initializer. If the INDEX of the elt is non-NULL, it is the
19168 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19169 as for cp_parser_initializer. */
19171 static vec<constructor_elt, va_gc> *
19172 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19174 vec<constructor_elt, va_gc> *v = NULL;
19176 /* Assume all of the expressions are constant. */
19177 *non_constant_p = false;
19179 /* Parse the rest of the list. */
19180 while (true)
19182 cp_token *token;
19183 tree designator;
19184 tree initializer;
19185 bool clause_non_constant_p;
19187 /* If the next token is an identifier and the following one is a
19188 colon, we are looking at the GNU designated-initializer
19189 syntax. */
19190 if (cp_parser_allow_gnu_extensions_p (parser)
19191 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19192 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19194 /* Warn the user that they are using an extension. */
19195 pedwarn (input_location, OPT_Wpedantic,
19196 "ISO C++ does not allow designated initializers");
19197 /* Consume the identifier. */
19198 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19199 /* Consume the `:'. */
19200 cp_lexer_consume_token (parser->lexer);
19202 /* Also handle the C99 syntax, '. id ='. */
19203 else if (cp_parser_allow_gnu_extensions_p (parser)
19204 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19205 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19206 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19208 /* Warn the user that they are using an extension. */
19209 pedwarn (input_location, OPT_Wpedantic,
19210 "ISO C++ does not allow C99 designated initializers");
19211 /* Consume the `.'. */
19212 cp_lexer_consume_token (parser->lexer);
19213 /* Consume the identifier. */
19214 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19215 /* Consume the `='. */
19216 cp_lexer_consume_token (parser->lexer);
19218 /* Also handle C99 array designators, '[ const ] ='. */
19219 else if (cp_parser_allow_gnu_extensions_p (parser)
19220 && !c_dialect_objc ()
19221 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19223 /* In C++11, [ could start a lambda-introducer. */
19224 bool non_const = false;
19226 cp_parser_parse_tentatively (parser);
19227 cp_lexer_consume_token (parser->lexer);
19228 designator = cp_parser_constant_expression (parser, true, &non_const);
19229 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19230 cp_parser_require (parser, CPP_EQ, RT_EQ);
19231 if (!cp_parser_parse_definitely (parser))
19232 designator = NULL_TREE;
19233 else if (non_const)
19234 require_potential_rvalue_constant_expression (designator);
19236 else
19237 designator = NULL_TREE;
19239 /* Parse the initializer. */
19240 initializer = cp_parser_initializer_clause (parser,
19241 &clause_non_constant_p);
19242 /* If any clause is non-constant, so is the entire initializer. */
19243 if (clause_non_constant_p)
19244 *non_constant_p = true;
19246 /* If we have an ellipsis, this is an initializer pack
19247 expansion. */
19248 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19250 /* Consume the `...'. */
19251 cp_lexer_consume_token (parser->lexer);
19253 /* Turn the initializer into an initializer expansion. */
19254 initializer = make_pack_expansion (initializer);
19257 /* Add it to the vector. */
19258 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19260 /* If the next token is not a comma, we have reached the end of
19261 the list. */
19262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19263 break;
19265 /* Peek at the next token. */
19266 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19267 /* If the next token is a `}', then we're still done. An
19268 initializer-clause can have a trailing `,' after the
19269 initializer-list and before the closing `}'. */
19270 if (token->type == CPP_CLOSE_BRACE)
19271 break;
19273 /* Consume the `,' token. */
19274 cp_lexer_consume_token (parser->lexer);
19277 return v;
19280 /* Classes [gram.class] */
19282 /* Parse a class-name.
19284 class-name:
19285 identifier
19286 template-id
19288 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19289 to indicate that names looked up in dependent types should be
19290 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19291 keyword has been used to indicate that the name that appears next
19292 is a template. TAG_TYPE indicates the explicit tag given before
19293 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19294 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19295 is the class being defined in a class-head.
19297 Returns the TYPE_DECL representing the class. */
19299 static tree
19300 cp_parser_class_name (cp_parser *parser,
19301 bool typename_keyword_p,
19302 bool template_keyword_p,
19303 enum tag_types tag_type,
19304 bool check_dependency_p,
19305 bool class_head_p,
19306 bool is_declaration)
19308 tree decl;
19309 tree scope;
19310 bool typename_p;
19311 cp_token *token;
19312 tree identifier = NULL_TREE;
19314 /* All class-names start with an identifier. */
19315 token = cp_lexer_peek_token (parser->lexer);
19316 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19318 cp_parser_error (parser, "expected class-name");
19319 return error_mark_node;
19322 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19323 to a template-id, so we save it here. */
19324 scope = parser->scope;
19325 if (scope == error_mark_node)
19326 return error_mark_node;
19328 /* Any name names a type if we're following the `typename' keyword
19329 in a qualified name where the enclosing scope is type-dependent. */
19330 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19331 && dependent_type_p (scope));
19332 /* Handle the common case (an identifier, but not a template-id)
19333 efficiently. */
19334 if (token->type == CPP_NAME
19335 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19337 cp_token *identifier_token;
19338 bool ambiguous_p;
19340 /* Look for the identifier. */
19341 identifier_token = cp_lexer_peek_token (parser->lexer);
19342 ambiguous_p = identifier_token->error_reported;
19343 identifier = cp_parser_identifier (parser);
19344 /* If the next token isn't an identifier, we are certainly not
19345 looking at a class-name. */
19346 if (identifier == error_mark_node)
19347 decl = error_mark_node;
19348 /* If we know this is a type-name, there's no need to look it
19349 up. */
19350 else if (typename_p)
19351 decl = identifier;
19352 else
19354 tree ambiguous_decls;
19355 /* If we already know that this lookup is ambiguous, then
19356 we've already issued an error message; there's no reason
19357 to check again. */
19358 if (ambiguous_p)
19360 cp_parser_simulate_error (parser);
19361 return error_mark_node;
19363 /* If the next token is a `::', then the name must be a type
19364 name.
19366 [basic.lookup.qual]
19368 During the lookup for a name preceding the :: scope
19369 resolution operator, object, function, and enumerator
19370 names are ignored. */
19371 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19372 tag_type = typename_type;
19373 /* Look up the name. */
19374 decl = cp_parser_lookup_name (parser, identifier,
19375 tag_type,
19376 /*is_template=*/false,
19377 /*is_namespace=*/false,
19378 check_dependency_p,
19379 &ambiguous_decls,
19380 identifier_token->location);
19381 if (ambiguous_decls)
19383 if (cp_parser_parsing_tentatively (parser))
19384 cp_parser_simulate_error (parser);
19385 return error_mark_node;
19389 else
19391 /* Try a template-id. */
19392 decl = cp_parser_template_id (parser, template_keyword_p,
19393 check_dependency_p,
19394 tag_type,
19395 is_declaration);
19396 if (decl == error_mark_node)
19397 return error_mark_node;
19400 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19402 /* If this is a typename, create a TYPENAME_TYPE. */
19403 if (typename_p && decl != error_mark_node)
19405 decl = make_typename_type (scope, decl, typename_type,
19406 /*complain=*/tf_error);
19407 if (decl != error_mark_node)
19408 decl = TYPE_NAME (decl);
19411 decl = strip_using_decl (decl);
19413 /* Check to see that it is really the name of a class. */
19414 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19415 && identifier_p (TREE_OPERAND (decl, 0))
19416 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19417 /* Situations like this:
19419 template <typename T> struct A {
19420 typename T::template X<int>::I i;
19423 are problematic. Is `T::template X<int>' a class-name? The
19424 standard does not seem to be definitive, but there is no other
19425 valid interpretation of the following `::'. Therefore, those
19426 names are considered class-names. */
19428 decl = make_typename_type (scope, decl, tag_type, tf_error);
19429 if (decl != error_mark_node)
19430 decl = TYPE_NAME (decl);
19432 else if (TREE_CODE (decl) != TYPE_DECL
19433 || TREE_TYPE (decl) == error_mark_node
19434 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19435 /* In Objective-C 2.0, a classname followed by '.' starts a
19436 dot-syntax expression, and it's not a type-name. */
19437 || (c_dialect_objc ()
19438 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19439 && objc_is_class_name (decl)))
19440 decl = error_mark_node;
19442 if (decl == error_mark_node)
19443 cp_parser_error (parser, "expected class-name");
19444 else if (identifier && !parser->scope)
19445 maybe_note_name_used_in_class (identifier, decl);
19447 return decl;
19450 /* Parse a class-specifier.
19452 class-specifier:
19453 class-head { member-specification [opt] }
19455 Returns the TREE_TYPE representing the class. */
19457 static tree
19458 cp_parser_class_specifier_1 (cp_parser* parser)
19460 tree type;
19461 tree attributes = NULL_TREE;
19462 bool nested_name_specifier_p;
19463 unsigned saved_num_template_parameter_lists;
19464 bool saved_in_function_body;
19465 unsigned char in_statement;
19466 bool in_switch_statement_p;
19467 bool saved_in_unbraced_linkage_specification_p;
19468 tree old_scope = NULL_TREE;
19469 tree scope = NULL_TREE;
19470 cp_token *closing_brace;
19472 push_deferring_access_checks (dk_no_deferred);
19474 /* Parse the class-head. */
19475 type = cp_parser_class_head (parser,
19476 &nested_name_specifier_p);
19477 /* If the class-head was a semantic disaster, skip the entire body
19478 of the class. */
19479 if (!type)
19481 cp_parser_skip_to_end_of_block_or_statement (parser);
19482 pop_deferring_access_checks ();
19483 return error_mark_node;
19486 /* Look for the `{'. */
19487 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19489 pop_deferring_access_checks ();
19490 return error_mark_node;
19493 cp_ensure_no_omp_declare_simd (parser);
19495 /* Issue an error message if type-definitions are forbidden here. */
19496 cp_parser_check_type_definition (parser);
19497 /* Remember that we are defining one more class. */
19498 ++parser->num_classes_being_defined;
19499 /* Inside the class, surrounding template-parameter-lists do not
19500 apply. */
19501 saved_num_template_parameter_lists
19502 = parser->num_template_parameter_lists;
19503 parser->num_template_parameter_lists = 0;
19504 /* We are not in a function body. */
19505 saved_in_function_body = parser->in_function_body;
19506 parser->in_function_body = false;
19507 /* Or in a loop. */
19508 in_statement = parser->in_statement;
19509 parser->in_statement = 0;
19510 /* Or in a switch. */
19511 in_switch_statement_p = parser->in_switch_statement_p;
19512 parser->in_switch_statement_p = false;
19513 /* We are not immediately inside an extern "lang" block. */
19514 saved_in_unbraced_linkage_specification_p
19515 = parser->in_unbraced_linkage_specification_p;
19516 parser->in_unbraced_linkage_specification_p = false;
19518 /* Start the class. */
19519 if (nested_name_specifier_p)
19521 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19522 old_scope = push_inner_scope (scope);
19524 type = begin_class_definition (type);
19526 if (type == error_mark_node)
19527 /* If the type is erroneous, skip the entire body of the class. */
19528 cp_parser_skip_to_closing_brace (parser);
19529 else
19530 /* Parse the member-specification. */
19531 cp_parser_member_specification_opt (parser);
19533 /* Look for the trailing `}'. */
19534 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19535 /* Look for trailing attributes to apply to this class. */
19536 if (cp_parser_allow_gnu_extensions_p (parser))
19537 attributes = cp_parser_gnu_attributes_opt (parser);
19538 if (type != error_mark_node)
19539 type = finish_struct (type, attributes);
19540 if (nested_name_specifier_p)
19541 pop_inner_scope (old_scope, scope);
19543 /* We've finished a type definition. Check for the common syntax
19544 error of forgetting a semicolon after the definition. We need to
19545 be careful, as we can't just check for not-a-semicolon and be done
19546 with it; the user might have typed:
19548 class X { } c = ...;
19549 class X { } *p = ...;
19551 and so forth. Instead, enumerate all the possible tokens that
19552 might follow this production; if we don't see one of them, then
19553 complain and silently insert the semicolon. */
19555 cp_token *token = cp_lexer_peek_token (parser->lexer);
19556 bool want_semicolon = true;
19558 if (cp_next_tokens_can_be_std_attribute_p (parser))
19559 /* Don't try to parse c++11 attributes here. As per the
19560 grammar, that should be a task for
19561 cp_parser_decl_specifier_seq. */
19562 want_semicolon = false;
19564 switch (token->type)
19566 case CPP_NAME:
19567 case CPP_SEMICOLON:
19568 case CPP_MULT:
19569 case CPP_AND:
19570 case CPP_OPEN_PAREN:
19571 case CPP_CLOSE_PAREN:
19572 case CPP_COMMA:
19573 want_semicolon = false;
19574 break;
19576 /* While it's legal for type qualifiers and storage class
19577 specifiers to follow type definitions in the grammar, only
19578 compiler testsuites contain code like that. Assume that if
19579 we see such code, then what we're really seeing is a case
19580 like:
19582 class X { }
19583 const <type> var = ...;
19587 class Y { }
19588 static <type> func (...) ...
19590 i.e. the qualifier or specifier applies to the next
19591 declaration. To do so, however, we need to look ahead one
19592 more token to see if *that* token is a type specifier.
19594 This code could be improved to handle:
19596 class Z { }
19597 static const <type> var = ...; */
19598 case CPP_KEYWORD:
19599 if (keyword_is_decl_specifier (token->keyword))
19601 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19603 /* Handling user-defined types here would be nice, but very
19604 tricky. */
19605 want_semicolon
19606 = (lookahead->type == CPP_KEYWORD
19607 && keyword_begins_type_specifier (lookahead->keyword));
19609 break;
19610 default:
19611 break;
19614 /* If we don't have a type, then something is very wrong and we
19615 shouldn't try to do anything clever. Likewise for not seeing the
19616 closing brace. */
19617 if (closing_brace && TYPE_P (type) && want_semicolon)
19619 cp_token_position prev
19620 = cp_lexer_previous_token_position (parser->lexer);
19621 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19622 location_t loc = prev_token->location;
19624 if (CLASSTYPE_DECLARED_CLASS (type))
19625 error_at (loc, "expected %<;%> after class definition");
19626 else if (TREE_CODE (type) == RECORD_TYPE)
19627 error_at (loc, "expected %<;%> after struct definition");
19628 else if (TREE_CODE (type) == UNION_TYPE)
19629 error_at (loc, "expected %<;%> after union definition");
19630 else
19631 gcc_unreachable ();
19633 /* Unget one token and smash it to look as though we encountered
19634 a semicolon in the input stream. */
19635 cp_lexer_set_token_position (parser->lexer, prev);
19636 token = cp_lexer_peek_token (parser->lexer);
19637 token->type = CPP_SEMICOLON;
19638 token->keyword = RID_MAX;
19642 /* If this class is not itself within the scope of another class,
19643 then we need to parse the bodies of all of the queued function
19644 definitions. Note that the queued functions defined in a class
19645 are not always processed immediately following the
19646 class-specifier for that class. Consider:
19648 struct A {
19649 struct B { void f() { sizeof (A); } };
19652 If `f' were processed before the processing of `A' were
19653 completed, there would be no way to compute the size of `A'.
19654 Note that the nesting we are interested in here is lexical --
19655 not the semantic nesting given by TYPE_CONTEXT. In particular,
19656 for:
19658 struct A { struct B; };
19659 struct A::B { void f() { } };
19661 there is no need to delay the parsing of `A::B::f'. */
19662 if (--parser->num_classes_being_defined == 0)
19664 tree decl;
19665 tree class_type = NULL_TREE;
19666 tree pushed_scope = NULL_TREE;
19667 unsigned ix;
19668 cp_default_arg_entry *e;
19669 tree save_ccp, save_ccr;
19671 /* In a first pass, parse default arguments to the functions.
19672 Then, in a second pass, parse the bodies of the functions.
19673 This two-phased approach handles cases like:
19675 struct S {
19676 void f() { g(); }
19677 void g(int i = 3);
19681 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19683 decl = e->decl;
19684 /* If there are default arguments that have not yet been processed,
19685 take care of them now. */
19686 if (class_type != e->class_type)
19688 if (pushed_scope)
19689 pop_scope (pushed_scope);
19690 class_type = e->class_type;
19691 pushed_scope = push_scope (class_type);
19693 /* Make sure that any template parameters are in scope. */
19694 maybe_begin_member_template_processing (decl);
19695 /* Parse the default argument expressions. */
19696 cp_parser_late_parsing_default_args (parser, decl);
19697 /* Remove any template parameters from the symbol table. */
19698 maybe_end_member_template_processing ();
19700 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19701 /* Now parse any NSDMIs. */
19702 save_ccp = current_class_ptr;
19703 save_ccr = current_class_ref;
19704 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19706 if (class_type != DECL_CONTEXT (decl))
19708 if (pushed_scope)
19709 pop_scope (pushed_scope);
19710 class_type = DECL_CONTEXT (decl);
19711 pushed_scope = push_scope (class_type);
19713 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19714 cp_parser_late_parsing_nsdmi (parser, decl);
19716 vec_safe_truncate (unparsed_nsdmis, 0);
19717 current_class_ptr = save_ccp;
19718 current_class_ref = save_ccr;
19719 if (pushed_scope)
19720 pop_scope (pushed_scope);
19722 /* Now do some post-NSDMI bookkeeping. */
19723 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19724 after_nsdmi_defaulted_late_checks (class_type);
19725 vec_safe_truncate (unparsed_classes, 0);
19726 after_nsdmi_defaulted_late_checks (type);
19728 /* Now parse the body of the functions. */
19729 if (flag_openmp)
19731 /* OpenMP UDRs need to be parsed before all other functions. */
19732 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19733 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19734 cp_parser_late_parsing_for_member (parser, decl);
19735 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19736 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19737 cp_parser_late_parsing_for_member (parser, decl);
19739 else
19740 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19741 cp_parser_late_parsing_for_member (parser, decl);
19742 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19744 else
19745 vec_safe_push (unparsed_classes, type);
19747 /* Put back any saved access checks. */
19748 pop_deferring_access_checks ();
19750 /* Restore saved state. */
19751 parser->in_switch_statement_p = in_switch_statement_p;
19752 parser->in_statement = in_statement;
19753 parser->in_function_body = saved_in_function_body;
19754 parser->num_template_parameter_lists
19755 = saved_num_template_parameter_lists;
19756 parser->in_unbraced_linkage_specification_p
19757 = saved_in_unbraced_linkage_specification_p;
19759 return type;
19762 static tree
19763 cp_parser_class_specifier (cp_parser* parser)
19765 tree ret;
19766 timevar_push (TV_PARSE_STRUCT);
19767 ret = cp_parser_class_specifier_1 (parser);
19768 timevar_pop (TV_PARSE_STRUCT);
19769 return ret;
19772 /* Parse a class-head.
19774 class-head:
19775 class-key identifier [opt] base-clause [opt]
19776 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19777 class-key nested-name-specifier [opt] template-id
19778 base-clause [opt]
19780 class-virt-specifier:
19781 final
19783 GNU Extensions:
19784 class-key attributes identifier [opt] base-clause [opt]
19785 class-key attributes nested-name-specifier identifier base-clause [opt]
19786 class-key attributes nested-name-specifier [opt] template-id
19787 base-clause [opt]
19789 Upon return BASES is initialized to the list of base classes (or
19790 NULL, if there are none) in the same form returned by
19791 cp_parser_base_clause.
19793 Returns the TYPE of the indicated class. Sets
19794 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19795 involving a nested-name-specifier was used, and FALSE otherwise.
19797 Returns error_mark_node if this is not a class-head.
19799 Returns NULL_TREE if the class-head is syntactically valid, but
19800 semantically invalid in a way that means we should skip the entire
19801 body of the class. */
19803 static tree
19804 cp_parser_class_head (cp_parser* parser,
19805 bool* nested_name_specifier_p)
19807 tree nested_name_specifier;
19808 enum tag_types class_key;
19809 tree id = NULL_TREE;
19810 tree type = NULL_TREE;
19811 tree attributes;
19812 tree bases;
19813 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19814 bool template_id_p = false;
19815 bool qualified_p = false;
19816 bool invalid_nested_name_p = false;
19817 bool invalid_explicit_specialization_p = false;
19818 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19819 tree pushed_scope = NULL_TREE;
19820 unsigned num_templates;
19821 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19822 /* Assume no nested-name-specifier will be present. */
19823 *nested_name_specifier_p = false;
19824 /* Assume no template parameter lists will be used in defining the
19825 type. */
19826 num_templates = 0;
19827 parser->colon_corrects_to_scope_p = false;
19829 /* Look for the class-key. */
19830 class_key = cp_parser_class_key (parser);
19831 if (class_key == none_type)
19832 return error_mark_node;
19834 /* Parse the attributes. */
19835 attributes = cp_parser_attributes_opt (parser);
19837 /* If the next token is `::', that is invalid -- but sometimes
19838 people do try to write:
19840 struct ::S {};
19842 Handle this gracefully by accepting the extra qualifier, and then
19843 issuing an error about it later if this really is a
19844 class-head. If it turns out just to be an elaborated type
19845 specifier, remain silent. */
19846 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19847 qualified_p = true;
19849 push_deferring_access_checks (dk_no_check);
19851 /* Determine the name of the class. Begin by looking for an
19852 optional nested-name-specifier. */
19853 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19854 nested_name_specifier
19855 = cp_parser_nested_name_specifier_opt (parser,
19856 /*typename_keyword_p=*/false,
19857 /*check_dependency_p=*/false,
19858 /*type_p=*/true,
19859 /*is_declaration=*/false);
19860 /* If there was a nested-name-specifier, then there *must* be an
19861 identifier. */
19862 if (nested_name_specifier)
19864 type_start_token = cp_lexer_peek_token (parser->lexer);
19865 /* Although the grammar says `identifier', it really means
19866 `class-name' or `template-name'. You are only allowed to
19867 define a class that has already been declared with this
19868 syntax.
19870 The proposed resolution for Core Issue 180 says that wherever
19871 you see `class T::X' you should treat `X' as a type-name.
19873 It is OK to define an inaccessible class; for example:
19875 class A { class B; };
19876 class A::B {};
19878 We do not know if we will see a class-name, or a
19879 template-name. We look for a class-name first, in case the
19880 class-name is a template-id; if we looked for the
19881 template-name first we would stop after the template-name. */
19882 cp_parser_parse_tentatively (parser);
19883 type = cp_parser_class_name (parser,
19884 /*typename_keyword_p=*/false,
19885 /*template_keyword_p=*/false,
19886 class_type,
19887 /*check_dependency_p=*/false,
19888 /*class_head_p=*/true,
19889 /*is_declaration=*/false);
19890 /* If that didn't work, ignore the nested-name-specifier. */
19891 if (!cp_parser_parse_definitely (parser))
19893 invalid_nested_name_p = true;
19894 type_start_token = cp_lexer_peek_token (parser->lexer);
19895 id = cp_parser_identifier (parser);
19896 if (id == error_mark_node)
19897 id = NULL_TREE;
19899 /* If we could not find a corresponding TYPE, treat this
19900 declaration like an unqualified declaration. */
19901 if (type == error_mark_node)
19902 nested_name_specifier = NULL_TREE;
19903 /* Otherwise, count the number of templates used in TYPE and its
19904 containing scopes. */
19905 else
19907 tree scope;
19909 for (scope = TREE_TYPE (type);
19910 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19911 scope = get_containing_scope (scope))
19912 if (TYPE_P (scope)
19913 && CLASS_TYPE_P (scope)
19914 && CLASSTYPE_TEMPLATE_INFO (scope)
19915 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19916 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19917 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19918 ++num_templates;
19921 /* Otherwise, the identifier is optional. */
19922 else
19924 /* We don't know whether what comes next is a template-id,
19925 an identifier, or nothing at all. */
19926 cp_parser_parse_tentatively (parser);
19927 /* Check for a template-id. */
19928 type_start_token = cp_lexer_peek_token (parser->lexer);
19929 id = cp_parser_template_id (parser,
19930 /*template_keyword_p=*/false,
19931 /*check_dependency_p=*/true,
19932 class_key,
19933 /*is_declaration=*/true);
19934 /* If that didn't work, it could still be an identifier. */
19935 if (!cp_parser_parse_definitely (parser))
19937 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19939 type_start_token = cp_lexer_peek_token (parser->lexer);
19940 id = cp_parser_identifier (parser);
19942 else
19943 id = NULL_TREE;
19945 else
19947 template_id_p = true;
19948 ++num_templates;
19952 pop_deferring_access_checks ();
19954 if (id)
19956 cp_parser_check_for_invalid_template_id (parser, id,
19957 class_key,
19958 type_start_token->location);
19960 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19962 /* If it's not a `:' or a `{' then we can't really be looking at a
19963 class-head, since a class-head only appears as part of a
19964 class-specifier. We have to detect this situation before calling
19965 xref_tag, since that has irreversible side-effects. */
19966 if (!cp_parser_next_token_starts_class_definition_p (parser))
19968 cp_parser_error (parser, "expected %<{%> or %<:%>");
19969 type = error_mark_node;
19970 goto out;
19973 /* At this point, we're going ahead with the class-specifier, even
19974 if some other problem occurs. */
19975 cp_parser_commit_to_tentative_parse (parser);
19976 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19978 cp_parser_error (parser,
19979 "cannot specify %<override%> for a class");
19980 type = error_mark_node;
19981 goto out;
19983 /* Issue the error about the overly-qualified name now. */
19984 if (qualified_p)
19986 cp_parser_error (parser,
19987 "global qualification of class name is invalid");
19988 type = error_mark_node;
19989 goto out;
19991 else if (invalid_nested_name_p)
19993 cp_parser_error (parser,
19994 "qualified name does not name a class");
19995 type = error_mark_node;
19996 goto out;
19998 else if (nested_name_specifier)
20000 tree scope;
20002 /* Reject typedef-names in class heads. */
20003 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20005 error_at (type_start_token->location,
20006 "invalid class name in declaration of %qD",
20007 type);
20008 type = NULL_TREE;
20009 goto done;
20012 /* Figure out in what scope the declaration is being placed. */
20013 scope = current_scope ();
20014 /* If that scope does not contain the scope in which the
20015 class was originally declared, the program is invalid. */
20016 if (scope && !is_ancestor (scope, nested_name_specifier))
20018 if (at_namespace_scope_p ())
20019 error_at (type_start_token->location,
20020 "declaration of %qD in namespace %qD which does not "
20021 "enclose %qD",
20022 type, scope, nested_name_specifier);
20023 else
20024 error_at (type_start_token->location,
20025 "declaration of %qD in %qD which does not enclose %qD",
20026 type, scope, nested_name_specifier);
20027 type = NULL_TREE;
20028 goto done;
20030 /* [dcl.meaning]
20032 A declarator-id shall not be qualified except for the
20033 definition of a ... nested class outside of its class
20034 ... [or] the definition or explicit instantiation of a
20035 class member of a namespace outside of its namespace. */
20036 if (scope == nested_name_specifier)
20038 permerror (nested_name_specifier_token_start->location,
20039 "extra qualification not allowed");
20040 nested_name_specifier = NULL_TREE;
20041 num_templates = 0;
20044 /* An explicit-specialization must be preceded by "template <>". If
20045 it is not, try to recover gracefully. */
20046 if (at_namespace_scope_p ()
20047 && parser->num_template_parameter_lists == 0
20048 && template_id_p)
20050 error_at (type_start_token->location,
20051 "an explicit specialization must be preceded by %<template <>%>");
20052 invalid_explicit_specialization_p = true;
20053 /* Take the same action that would have been taken by
20054 cp_parser_explicit_specialization. */
20055 ++parser->num_template_parameter_lists;
20056 begin_specialization ();
20058 /* There must be no "return" statements between this point and the
20059 end of this function; set "type "to the correct return value and
20060 use "goto done;" to return. */
20061 /* Make sure that the right number of template parameters were
20062 present. */
20063 if (!cp_parser_check_template_parameters (parser, num_templates,
20064 type_start_token->location,
20065 /*declarator=*/NULL))
20067 /* If something went wrong, there is no point in even trying to
20068 process the class-definition. */
20069 type = NULL_TREE;
20070 goto done;
20073 /* Look up the type. */
20074 if (template_id_p)
20076 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20077 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20078 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20080 error_at (type_start_token->location,
20081 "function template %qD redeclared as a class template", id);
20082 type = error_mark_node;
20084 else
20086 type = TREE_TYPE (id);
20087 type = maybe_process_partial_specialization (type);
20089 if (nested_name_specifier)
20090 pushed_scope = push_scope (nested_name_specifier);
20092 else if (nested_name_specifier)
20094 tree class_type;
20096 /* Given:
20098 template <typename T> struct S { struct T };
20099 template <typename T> struct S<T>::T { };
20101 we will get a TYPENAME_TYPE when processing the definition of
20102 `S::T'. We need to resolve it to the actual type before we
20103 try to define it. */
20104 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20106 class_type = resolve_typename_type (TREE_TYPE (type),
20107 /*only_current_p=*/false);
20108 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20109 type = TYPE_NAME (class_type);
20110 else
20112 cp_parser_error (parser, "could not resolve typename type");
20113 type = error_mark_node;
20117 if (maybe_process_partial_specialization (TREE_TYPE (type))
20118 == error_mark_node)
20120 type = NULL_TREE;
20121 goto done;
20124 class_type = current_class_type;
20125 /* Enter the scope indicated by the nested-name-specifier. */
20126 pushed_scope = push_scope (nested_name_specifier);
20127 /* Get the canonical version of this type. */
20128 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20129 /* Call push_template_decl if it seems like we should be defining a
20130 template either from the template headers or the type we're
20131 defining, so that we diagnose both extra and missing headers. */
20132 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20133 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
20134 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
20135 (TREE_TYPE (type)))))
20136 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20138 type = push_template_decl (type);
20139 if (type == error_mark_node)
20141 type = NULL_TREE;
20142 goto done;
20146 type = TREE_TYPE (type);
20147 *nested_name_specifier_p = true;
20149 else /* The name is not a nested name. */
20151 /* If the class was unnamed, create a dummy name. */
20152 if (!id)
20153 id = make_anon_name ();
20154 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20155 parser->num_template_parameter_lists);
20158 /* Indicate whether this class was declared as a `class' or as a
20159 `struct'. */
20160 if (TREE_CODE (type) == RECORD_TYPE)
20161 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20162 cp_parser_check_class_key (class_key, type);
20164 /* If this type was already complete, and we see another definition,
20165 that's an error. */
20166 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20168 error_at (type_start_token->location, "redefinition of %q#T",
20169 type);
20170 error_at (type_start_token->location, "previous definition of %q+#T",
20171 type);
20172 type = NULL_TREE;
20173 goto done;
20175 else if (type == error_mark_node)
20176 type = NULL_TREE;
20178 if (type)
20180 /* Apply attributes now, before any use of the class as a template
20181 argument in its base list. */
20182 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20183 fixup_attribute_variants (type);
20186 /* We will have entered the scope containing the class; the names of
20187 base classes should be looked up in that context. For example:
20189 struct A { struct B {}; struct C; };
20190 struct A::C : B {};
20192 is valid. */
20194 /* Get the list of base-classes, if there is one. */
20195 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20197 /* PR59482: enter the class scope so that base-specifiers are looked
20198 up correctly. */
20199 if (type)
20200 pushclass (type);
20201 bases = cp_parser_base_clause (parser);
20202 /* PR59482: get out of the previously pushed class scope so that the
20203 subsequent pops pop the right thing. */
20204 if (type)
20205 popclass ();
20207 else
20208 bases = NULL_TREE;
20210 /* If we're really defining a class, process the base classes.
20211 If they're invalid, fail. */
20212 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20213 && !xref_basetypes (type, bases))
20214 type = NULL_TREE;
20216 done:
20217 /* Leave the scope given by the nested-name-specifier. We will
20218 enter the class scope itself while processing the members. */
20219 if (pushed_scope)
20220 pop_scope (pushed_scope);
20222 if (invalid_explicit_specialization_p)
20224 end_specialization ();
20225 --parser->num_template_parameter_lists;
20228 if (type)
20229 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20230 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20231 CLASSTYPE_FINAL (type) = 1;
20232 out:
20233 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20234 return type;
20237 /* Parse a class-key.
20239 class-key:
20240 class
20241 struct
20242 union
20244 Returns the kind of class-key specified, or none_type to indicate
20245 error. */
20247 static enum tag_types
20248 cp_parser_class_key (cp_parser* parser)
20250 cp_token *token;
20251 enum tag_types tag_type;
20253 /* Look for the class-key. */
20254 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20255 if (!token)
20256 return none_type;
20258 /* Check to see if the TOKEN is a class-key. */
20259 tag_type = cp_parser_token_is_class_key (token);
20260 if (!tag_type)
20261 cp_parser_error (parser, "expected class-key");
20262 return tag_type;
20265 /* Parse a type-parameter-key.
20267 type-parameter-key:
20268 class
20269 typename
20272 static void
20273 cp_parser_type_parameter_key (cp_parser* parser)
20275 /* Look for the type-parameter-key. */
20276 enum tag_types tag_type = none_type;
20277 cp_token *token = cp_lexer_peek_token (parser->lexer);
20278 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20280 cp_lexer_consume_token (parser->lexer);
20281 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20282 /* typename is not allowed in a template template parameter
20283 by the standard until C++1Z. */
20284 pedwarn (token->location, OPT_Wpedantic,
20285 "ISO C++ forbids typename key in template template parameter;"
20286 " use -std=c++1z or -std=gnu++1z");
20288 else
20289 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20291 return;
20294 /* Parse an (optional) member-specification.
20296 member-specification:
20297 member-declaration member-specification [opt]
20298 access-specifier : member-specification [opt] */
20300 static void
20301 cp_parser_member_specification_opt (cp_parser* parser)
20303 while (true)
20305 cp_token *token;
20306 enum rid keyword;
20308 /* Peek at the next token. */
20309 token = cp_lexer_peek_token (parser->lexer);
20310 /* If it's a `}', or EOF then we've seen all the members. */
20311 if (token->type == CPP_CLOSE_BRACE
20312 || token->type == CPP_EOF
20313 || token->type == CPP_PRAGMA_EOL)
20314 break;
20316 /* See if this token is a keyword. */
20317 keyword = token->keyword;
20318 switch (keyword)
20320 case RID_PUBLIC:
20321 case RID_PROTECTED:
20322 case RID_PRIVATE:
20323 /* Consume the access-specifier. */
20324 cp_lexer_consume_token (parser->lexer);
20325 /* Remember which access-specifier is active. */
20326 current_access_specifier = token->u.value;
20327 /* Look for the `:'. */
20328 cp_parser_require (parser, CPP_COLON, RT_COLON);
20329 break;
20331 default:
20332 /* Accept #pragmas at class scope. */
20333 if (token->type == CPP_PRAGMA)
20335 cp_parser_pragma (parser, pragma_member);
20336 break;
20339 /* Otherwise, the next construction must be a
20340 member-declaration. */
20341 cp_parser_member_declaration (parser);
20346 /* Parse a member-declaration.
20348 member-declaration:
20349 decl-specifier-seq [opt] member-declarator-list [opt] ;
20350 function-definition ; [opt]
20351 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20352 using-declaration
20353 template-declaration
20354 alias-declaration
20356 member-declarator-list:
20357 member-declarator
20358 member-declarator-list , member-declarator
20360 member-declarator:
20361 declarator pure-specifier [opt]
20362 declarator constant-initializer [opt]
20363 identifier [opt] : constant-expression
20365 GNU Extensions:
20367 member-declaration:
20368 __extension__ member-declaration
20370 member-declarator:
20371 declarator attributes [opt] pure-specifier [opt]
20372 declarator attributes [opt] constant-initializer [opt]
20373 identifier [opt] attributes [opt] : constant-expression
20375 C++0x Extensions:
20377 member-declaration:
20378 static_assert-declaration */
20380 static void
20381 cp_parser_member_declaration (cp_parser* parser)
20383 cp_decl_specifier_seq decl_specifiers;
20384 tree prefix_attributes;
20385 tree decl;
20386 int declares_class_or_enum;
20387 bool friend_p;
20388 cp_token *token = NULL;
20389 cp_token *decl_spec_token_start = NULL;
20390 cp_token *initializer_token_start = NULL;
20391 int saved_pedantic;
20392 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20394 /* Check for the `__extension__' keyword. */
20395 if (cp_parser_extension_opt (parser, &saved_pedantic))
20397 /* Recurse. */
20398 cp_parser_member_declaration (parser);
20399 /* Restore the old value of the PEDANTIC flag. */
20400 pedantic = saved_pedantic;
20402 return;
20405 /* Check for a template-declaration. */
20406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20408 /* An explicit specialization here is an error condition, and we
20409 expect the specialization handler to detect and report this. */
20410 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20411 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20412 cp_parser_explicit_specialization (parser);
20413 else
20414 cp_parser_template_declaration (parser, /*member_p=*/true);
20416 return;
20419 /* Check for a using-declaration. */
20420 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20422 if (cxx_dialect < cxx11)
20424 /* Parse the using-declaration. */
20425 cp_parser_using_declaration (parser,
20426 /*access_declaration_p=*/false);
20427 return;
20429 else
20431 tree decl;
20432 bool alias_decl_expected;
20433 cp_parser_parse_tentatively (parser);
20434 decl = cp_parser_alias_declaration (parser);
20435 /* Note that if we actually see the '=' token after the
20436 identifier, cp_parser_alias_declaration commits the
20437 tentative parse. In that case, we really expects an
20438 alias-declaration. Otherwise, we expect a using
20439 declaration. */
20440 alias_decl_expected =
20441 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20442 cp_parser_parse_definitely (parser);
20444 if (alias_decl_expected)
20445 finish_member_declaration (decl);
20446 else
20447 cp_parser_using_declaration (parser,
20448 /*access_declaration_p=*/false);
20449 return;
20453 /* Check for @defs. */
20454 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20456 tree ivar, member;
20457 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20458 ivar = ivar_chains;
20459 while (ivar)
20461 member = ivar;
20462 ivar = TREE_CHAIN (member);
20463 TREE_CHAIN (member) = NULL_TREE;
20464 finish_member_declaration (member);
20466 return;
20469 /* If the next token is `static_assert' we have a static assertion. */
20470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20472 cp_parser_static_assert (parser, /*member_p=*/true);
20473 return;
20476 parser->colon_corrects_to_scope_p = false;
20478 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20479 goto out;
20481 /* Parse the decl-specifier-seq. */
20482 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20483 cp_parser_decl_specifier_seq (parser,
20484 CP_PARSER_FLAGS_OPTIONAL,
20485 &decl_specifiers,
20486 &declares_class_or_enum);
20487 /* Check for an invalid type-name. */
20488 if (!decl_specifiers.any_type_specifiers_p
20489 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20490 goto out;
20491 /* If there is no declarator, then the decl-specifier-seq should
20492 specify a type. */
20493 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20495 /* If there was no decl-specifier-seq, and the next token is a
20496 `;', then we have something like:
20498 struct S { ; };
20500 [class.mem]
20502 Each member-declaration shall declare at least one member
20503 name of the class. */
20504 if (!decl_specifiers.any_specifiers_p)
20506 cp_token *token = cp_lexer_peek_token (parser->lexer);
20507 if (!in_system_header_at (token->location))
20508 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20510 else
20512 tree type;
20514 /* See if this declaration is a friend. */
20515 friend_p = cp_parser_friend_p (&decl_specifiers);
20516 /* If there were decl-specifiers, check to see if there was
20517 a class-declaration. */
20518 type = check_tag_decl (&decl_specifiers,
20519 /*explicit_type_instantiation_p=*/false);
20520 /* Nested classes have already been added to the class, but
20521 a `friend' needs to be explicitly registered. */
20522 if (friend_p)
20524 /* If the `friend' keyword was present, the friend must
20525 be introduced with a class-key. */
20526 if (!declares_class_or_enum && cxx_dialect < cxx11)
20527 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20528 "in C++03 a class-key must be used "
20529 "when declaring a friend");
20530 /* In this case:
20532 template <typename T> struct A {
20533 friend struct A<T>::B;
20536 A<T>::B will be represented by a TYPENAME_TYPE, and
20537 therefore not recognized by check_tag_decl. */
20538 if (!type)
20540 type = decl_specifiers.type;
20541 if (type && TREE_CODE (type) == TYPE_DECL)
20542 type = TREE_TYPE (type);
20544 if (!type || !TYPE_P (type))
20545 error_at (decl_spec_token_start->location,
20546 "friend declaration does not name a class or "
20547 "function");
20548 else
20549 make_friend_class (current_class_type, type,
20550 /*complain=*/true);
20552 /* If there is no TYPE, an error message will already have
20553 been issued. */
20554 else if (!type || type == error_mark_node)
20556 /* An anonymous aggregate has to be handled specially; such
20557 a declaration really declares a data member (with a
20558 particular type), as opposed to a nested class. */
20559 else if (ANON_AGGR_TYPE_P (type))
20561 /* C++11 9.5/6. */
20562 if (decl_specifiers.storage_class != sc_none)
20563 error_at (decl_spec_token_start->location,
20564 "a storage class on an anonymous aggregate "
20565 "in class scope is not allowed");
20567 /* Remove constructors and such from TYPE, now that we
20568 know it is an anonymous aggregate. */
20569 fixup_anonymous_aggr (type);
20570 /* And make the corresponding data member. */
20571 decl = build_decl (decl_spec_token_start->location,
20572 FIELD_DECL, NULL_TREE, type);
20573 /* Add it to the class. */
20574 finish_member_declaration (decl);
20576 else
20577 cp_parser_check_access_in_redeclaration
20578 (TYPE_NAME (type),
20579 decl_spec_token_start->location);
20582 else
20584 bool assume_semicolon = false;
20586 /* Clear attributes from the decl_specifiers but keep them
20587 around as prefix attributes that apply them to the entity
20588 being declared. */
20589 prefix_attributes = decl_specifiers.attributes;
20590 decl_specifiers.attributes = NULL_TREE;
20592 /* See if these declarations will be friends. */
20593 friend_p = cp_parser_friend_p (&decl_specifiers);
20595 /* Keep going until we hit the `;' at the end of the
20596 declaration. */
20597 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20599 tree attributes = NULL_TREE;
20600 tree first_attribute;
20602 /* Peek at the next token. */
20603 token = cp_lexer_peek_token (parser->lexer);
20605 /* Check for a bitfield declaration. */
20606 if (token->type == CPP_COLON
20607 || (token->type == CPP_NAME
20608 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20609 == CPP_COLON))
20611 tree identifier;
20612 tree width;
20614 /* Get the name of the bitfield. Note that we cannot just
20615 check TOKEN here because it may have been invalidated by
20616 the call to cp_lexer_peek_nth_token above. */
20617 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20618 identifier = cp_parser_identifier (parser);
20619 else
20620 identifier = NULL_TREE;
20622 /* Consume the `:' token. */
20623 cp_lexer_consume_token (parser->lexer);
20624 /* Get the width of the bitfield. */
20625 width
20626 = cp_parser_constant_expression (parser,
20627 /*allow_non_constant=*/false,
20628 NULL);
20630 /* Look for attributes that apply to the bitfield. */
20631 attributes = cp_parser_attributes_opt (parser);
20632 /* Remember which attributes are prefix attributes and
20633 which are not. */
20634 first_attribute = attributes;
20635 /* Combine the attributes. */
20636 attributes = chainon (prefix_attributes, attributes);
20638 /* Create the bitfield declaration. */
20639 decl = grokbitfield (identifier
20640 ? make_id_declarator (NULL_TREE,
20641 identifier,
20642 sfk_none)
20643 : NULL,
20644 &decl_specifiers,
20645 width,
20646 attributes);
20648 else
20650 cp_declarator *declarator;
20651 tree initializer;
20652 tree asm_specification;
20653 int ctor_dtor_or_conv_p;
20655 /* Parse the declarator. */
20656 declarator
20657 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20658 &ctor_dtor_or_conv_p,
20659 /*parenthesized_p=*/NULL,
20660 /*member_p=*/true,
20661 friend_p);
20663 /* If something went wrong parsing the declarator, make sure
20664 that we at least consume some tokens. */
20665 if (declarator == cp_error_declarator)
20667 /* Skip to the end of the statement. */
20668 cp_parser_skip_to_end_of_statement (parser);
20669 /* If the next token is not a semicolon, that is
20670 probably because we just skipped over the body of
20671 a function. So, we consume a semicolon if
20672 present, but do not issue an error message if it
20673 is not present. */
20674 if (cp_lexer_next_token_is (parser->lexer,
20675 CPP_SEMICOLON))
20676 cp_lexer_consume_token (parser->lexer);
20677 goto out;
20680 if (declares_class_or_enum & 2)
20681 cp_parser_check_for_definition_in_return_type
20682 (declarator, decl_specifiers.type,
20683 decl_specifiers.locations[ds_type_spec]);
20685 /* Look for an asm-specification. */
20686 asm_specification = cp_parser_asm_specification_opt (parser);
20687 /* Look for attributes that apply to the declaration. */
20688 attributes = cp_parser_attributes_opt (parser);
20689 /* Remember which attributes are prefix attributes and
20690 which are not. */
20691 first_attribute = attributes;
20692 /* Combine the attributes. */
20693 attributes = chainon (prefix_attributes, attributes);
20695 /* If it's an `=', then we have a constant-initializer or a
20696 pure-specifier. It is not correct to parse the
20697 initializer before registering the member declaration
20698 since the member declaration should be in scope while
20699 its initializer is processed. However, the rest of the
20700 front end does not yet provide an interface that allows
20701 us to handle this correctly. */
20702 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20704 /* In [class.mem]:
20706 A pure-specifier shall be used only in the declaration of
20707 a virtual function.
20709 A member-declarator can contain a constant-initializer
20710 only if it declares a static member of integral or
20711 enumeration type.
20713 Therefore, if the DECLARATOR is for a function, we look
20714 for a pure-specifier; otherwise, we look for a
20715 constant-initializer. When we call `grokfield', it will
20716 perform more stringent semantics checks. */
20717 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20718 if (function_declarator_p (declarator)
20719 || (decl_specifiers.type
20720 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20721 && declarator->kind == cdk_id
20722 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20723 == FUNCTION_TYPE)))
20724 initializer = cp_parser_pure_specifier (parser);
20725 else if (decl_specifiers.storage_class != sc_static)
20726 initializer = cp_parser_save_nsdmi (parser);
20727 else if (cxx_dialect >= cxx11)
20729 bool nonconst;
20730 /* Don't require a constant rvalue in C++11, since we
20731 might want a reference constant. We'll enforce
20732 constancy later. */
20733 cp_lexer_consume_token (parser->lexer);
20734 /* Parse the initializer. */
20735 initializer = cp_parser_initializer_clause (parser,
20736 &nonconst);
20738 else
20739 /* Parse the initializer. */
20740 initializer = cp_parser_constant_initializer (parser);
20742 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20743 && !function_declarator_p (declarator))
20745 bool x;
20746 if (decl_specifiers.storage_class != sc_static)
20747 initializer = cp_parser_save_nsdmi (parser);
20748 else
20749 initializer = cp_parser_initializer (parser, &x, &x);
20751 /* Otherwise, there is no initializer. */
20752 else
20753 initializer = NULL_TREE;
20755 /* See if we are probably looking at a function
20756 definition. We are certainly not looking at a
20757 member-declarator. Calling `grokfield' has
20758 side-effects, so we must not do it unless we are sure
20759 that we are looking at a member-declarator. */
20760 if (cp_parser_token_starts_function_definition_p
20761 (cp_lexer_peek_token (parser->lexer)))
20763 /* The grammar does not allow a pure-specifier to be
20764 used when a member function is defined. (It is
20765 possible that this fact is an oversight in the
20766 standard, since a pure function may be defined
20767 outside of the class-specifier. */
20768 if (initializer && initializer_token_start)
20769 error_at (initializer_token_start->location,
20770 "pure-specifier on function-definition");
20771 decl = cp_parser_save_member_function_body (parser,
20772 &decl_specifiers,
20773 declarator,
20774 attributes);
20775 if (parser->fully_implicit_function_template_p)
20776 decl = finish_fully_implicit_template (parser, decl);
20777 /* If the member was not a friend, declare it here. */
20778 if (!friend_p)
20779 finish_member_declaration (decl);
20780 /* Peek at the next token. */
20781 token = cp_lexer_peek_token (parser->lexer);
20782 /* If the next token is a semicolon, consume it. */
20783 if (token->type == CPP_SEMICOLON)
20784 cp_lexer_consume_token (parser->lexer);
20785 goto out;
20787 else
20788 if (declarator->kind == cdk_function)
20789 declarator->id_loc = token->location;
20790 /* Create the declaration. */
20791 decl = grokfield (declarator, &decl_specifiers,
20792 initializer, /*init_const_expr_p=*/true,
20793 asm_specification, attributes);
20794 if (parser->fully_implicit_function_template_p)
20796 if (friend_p)
20797 finish_fully_implicit_template (parser, 0);
20798 else
20799 decl = finish_fully_implicit_template (parser, decl);
20803 cp_finalize_omp_declare_simd (parser, decl);
20805 /* Reset PREFIX_ATTRIBUTES. */
20806 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20807 attributes = TREE_CHAIN (attributes);
20808 if (attributes)
20809 TREE_CHAIN (attributes) = NULL_TREE;
20811 /* If there is any qualification still in effect, clear it
20812 now; we will be starting fresh with the next declarator. */
20813 parser->scope = NULL_TREE;
20814 parser->qualifying_scope = NULL_TREE;
20815 parser->object_scope = NULL_TREE;
20816 /* If it's a `,', then there are more declarators. */
20817 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20819 cp_lexer_consume_token (parser->lexer);
20820 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20822 cp_token *token = cp_lexer_previous_token (parser->lexer);
20823 error_at (token->location,
20824 "stray %<,%> at end of member declaration");
20827 /* If the next token isn't a `;', then we have a parse error. */
20828 else if (cp_lexer_next_token_is_not (parser->lexer,
20829 CPP_SEMICOLON))
20831 /* The next token might be a ways away from where the
20832 actual semicolon is missing. Find the previous token
20833 and use that for our error position. */
20834 cp_token *token = cp_lexer_previous_token (parser->lexer);
20835 error_at (token->location,
20836 "expected %<;%> at end of member declaration");
20838 /* Assume that the user meant to provide a semicolon. If
20839 we were to cp_parser_skip_to_end_of_statement, we might
20840 skip to a semicolon inside a member function definition
20841 and issue nonsensical error messages. */
20842 assume_semicolon = true;
20845 if (decl)
20847 /* Add DECL to the list of members. */
20848 if (!friend_p)
20849 finish_member_declaration (decl);
20851 if (TREE_CODE (decl) == FUNCTION_DECL)
20852 cp_parser_save_default_args (parser, decl);
20853 else if (TREE_CODE (decl) == FIELD_DECL
20854 && !DECL_C_BIT_FIELD (decl)
20855 && DECL_INITIAL (decl))
20856 /* Add DECL to the queue of NSDMI to be parsed later. */
20857 vec_safe_push (unparsed_nsdmis, decl);
20860 if (assume_semicolon)
20861 goto out;
20865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20866 out:
20867 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20870 /* Parse a pure-specifier.
20872 pure-specifier:
20875 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20876 Otherwise, ERROR_MARK_NODE is returned. */
20878 static tree
20879 cp_parser_pure_specifier (cp_parser* parser)
20881 cp_token *token;
20883 /* Look for the `=' token. */
20884 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20885 return error_mark_node;
20886 /* Look for the `0' token. */
20887 token = cp_lexer_peek_token (parser->lexer);
20889 if (token->type == CPP_EOF
20890 || token->type == CPP_PRAGMA_EOL)
20891 return error_mark_node;
20893 cp_lexer_consume_token (parser->lexer);
20895 /* Accept = default or = delete in c++0x mode. */
20896 if (token->keyword == RID_DEFAULT
20897 || token->keyword == RID_DELETE)
20899 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20900 return token->u.value;
20903 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20904 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20906 cp_parser_error (parser,
20907 "invalid pure specifier (only %<= 0%> is allowed)");
20908 cp_parser_skip_to_end_of_statement (parser);
20909 return error_mark_node;
20911 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20913 error_at (token->location, "templates may not be %<virtual%>");
20914 return error_mark_node;
20917 return integer_zero_node;
20920 /* Parse a constant-initializer.
20922 constant-initializer:
20923 = constant-expression
20925 Returns a representation of the constant-expression. */
20927 static tree
20928 cp_parser_constant_initializer (cp_parser* parser)
20930 /* Look for the `=' token. */
20931 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20932 return error_mark_node;
20934 /* It is invalid to write:
20936 struct S { static const int i = { 7 }; };
20939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20941 cp_parser_error (parser,
20942 "a brace-enclosed initializer is not allowed here");
20943 /* Consume the opening brace. */
20944 cp_lexer_consume_token (parser->lexer);
20945 /* Skip the initializer. */
20946 cp_parser_skip_to_closing_brace (parser);
20947 /* Look for the trailing `}'. */
20948 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20950 return error_mark_node;
20953 return cp_parser_constant_expression (parser,
20954 /*allow_non_constant=*/false,
20955 NULL);
20958 /* Derived classes [gram.class.derived] */
20960 /* Parse a base-clause.
20962 base-clause:
20963 : base-specifier-list
20965 base-specifier-list:
20966 base-specifier ... [opt]
20967 base-specifier-list , base-specifier ... [opt]
20969 Returns a TREE_LIST representing the base-classes, in the order in
20970 which they were declared. The representation of each node is as
20971 described by cp_parser_base_specifier.
20973 In the case that no bases are specified, this function will return
20974 NULL_TREE, not ERROR_MARK_NODE. */
20976 static tree
20977 cp_parser_base_clause (cp_parser* parser)
20979 tree bases = NULL_TREE;
20981 /* Look for the `:' that begins the list. */
20982 cp_parser_require (parser, CPP_COLON, RT_COLON);
20984 /* Scan the base-specifier-list. */
20985 while (true)
20987 cp_token *token;
20988 tree base;
20989 bool pack_expansion_p = false;
20991 /* Look for the base-specifier. */
20992 base = cp_parser_base_specifier (parser);
20993 /* Look for the (optional) ellipsis. */
20994 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20996 /* Consume the `...'. */
20997 cp_lexer_consume_token (parser->lexer);
20999 pack_expansion_p = true;
21002 /* Add BASE to the front of the list. */
21003 if (base && base != error_mark_node)
21005 if (pack_expansion_p)
21006 /* Make this a pack expansion type. */
21007 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21009 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21011 TREE_CHAIN (base) = bases;
21012 bases = base;
21015 /* Peek at the next token. */
21016 token = cp_lexer_peek_token (parser->lexer);
21017 /* If it's not a comma, then the list is complete. */
21018 if (token->type != CPP_COMMA)
21019 break;
21020 /* Consume the `,'. */
21021 cp_lexer_consume_token (parser->lexer);
21024 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21025 base class had a qualified name. However, the next name that
21026 appears is certainly not qualified. */
21027 parser->scope = NULL_TREE;
21028 parser->qualifying_scope = NULL_TREE;
21029 parser->object_scope = NULL_TREE;
21031 return nreverse (bases);
21034 /* Parse a base-specifier.
21036 base-specifier:
21037 :: [opt] nested-name-specifier [opt] class-name
21038 virtual access-specifier [opt] :: [opt] nested-name-specifier
21039 [opt] class-name
21040 access-specifier virtual [opt] :: [opt] nested-name-specifier
21041 [opt] class-name
21043 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21044 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21045 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21046 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21048 static tree
21049 cp_parser_base_specifier (cp_parser* parser)
21051 cp_token *token;
21052 bool done = false;
21053 bool virtual_p = false;
21054 bool duplicate_virtual_error_issued_p = false;
21055 bool duplicate_access_error_issued_p = false;
21056 bool class_scope_p, template_p;
21057 tree access = access_default_node;
21058 tree type;
21060 /* Process the optional `virtual' and `access-specifier'. */
21061 while (!done)
21063 /* Peek at the next token. */
21064 token = cp_lexer_peek_token (parser->lexer);
21065 /* Process `virtual'. */
21066 switch (token->keyword)
21068 case RID_VIRTUAL:
21069 /* If `virtual' appears more than once, issue an error. */
21070 if (virtual_p && !duplicate_virtual_error_issued_p)
21072 cp_parser_error (parser,
21073 "%<virtual%> specified more than once in base-specified");
21074 duplicate_virtual_error_issued_p = true;
21077 virtual_p = true;
21079 /* Consume the `virtual' token. */
21080 cp_lexer_consume_token (parser->lexer);
21082 break;
21084 case RID_PUBLIC:
21085 case RID_PROTECTED:
21086 case RID_PRIVATE:
21087 /* If more than one access specifier appears, issue an
21088 error. */
21089 if (access != access_default_node
21090 && !duplicate_access_error_issued_p)
21092 cp_parser_error (parser,
21093 "more than one access specifier in base-specified");
21094 duplicate_access_error_issued_p = true;
21097 access = ridpointers[(int) token->keyword];
21099 /* Consume the access-specifier. */
21100 cp_lexer_consume_token (parser->lexer);
21102 break;
21104 default:
21105 done = true;
21106 break;
21109 /* It is not uncommon to see programs mechanically, erroneously, use
21110 the 'typename' keyword to denote (dependent) qualified types
21111 as base classes. */
21112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21114 token = cp_lexer_peek_token (parser->lexer);
21115 if (!processing_template_decl)
21116 error_at (token->location,
21117 "keyword %<typename%> not allowed outside of templates");
21118 else
21119 error_at (token->location,
21120 "keyword %<typename%> not allowed in this context "
21121 "(the base class is implicitly a type)");
21122 cp_lexer_consume_token (parser->lexer);
21125 /* Look for the optional `::' operator. */
21126 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21127 /* Look for the nested-name-specifier. The simplest way to
21128 implement:
21130 [temp.res]
21132 The keyword `typename' is not permitted in a base-specifier or
21133 mem-initializer; in these contexts a qualified name that
21134 depends on a template-parameter is implicitly assumed to be a
21135 type name.
21137 is to pretend that we have seen the `typename' keyword at this
21138 point. */
21139 cp_parser_nested_name_specifier_opt (parser,
21140 /*typename_keyword_p=*/true,
21141 /*check_dependency_p=*/true,
21142 typename_type,
21143 /*is_declaration=*/true);
21144 /* If the base class is given by a qualified name, assume that names
21145 we see are type names or templates, as appropriate. */
21146 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21147 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21149 if (!parser->scope
21150 && cp_lexer_next_token_is_decltype (parser->lexer))
21151 /* DR 950 allows decltype as a base-specifier. */
21152 type = cp_parser_decltype (parser);
21153 else
21155 /* Otherwise, look for the class-name. */
21156 type = cp_parser_class_name (parser,
21157 class_scope_p,
21158 template_p,
21159 typename_type,
21160 /*check_dependency_p=*/true,
21161 /*class_head_p=*/false,
21162 /*is_declaration=*/true);
21163 type = TREE_TYPE (type);
21166 if (type == error_mark_node)
21167 return error_mark_node;
21169 return finish_base_specifier (type, access, virtual_p);
21172 /* Exception handling [gram.exception] */
21174 /* Parse an (optional) noexcept-specification.
21176 noexcept-specification:
21177 noexcept ( constant-expression ) [opt]
21179 If no noexcept-specification is present, returns NULL_TREE.
21180 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21181 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21182 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21183 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21184 in which case a boolean condition is returned instead. */
21186 static tree
21187 cp_parser_noexcept_specification_opt (cp_parser* parser,
21188 bool require_constexpr,
21189 bool* consumed_expr,
21190 bool return_cond)
21192 cp_token *token;
21193 const char *saved_message;
21195 /* Peek at the next token. */
21196 token = cp_lexer_peek_token (parser->lexer);
21198 /* Is it a noexcept-specification? */
21199 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21201 tree expr;
21202 cp_lexer_consume_token (parser->lexer);
21204 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21206 cp_lexer_consume_token (parser->lexer);
21208 if (require_constexpr)
21210 /* Types may not be defined in an exception-specification. */
21211 saved_message = parser->type_definition_forbidden_message;
21212 parser->type_definition_forbidden_message
21213 = G_("types may not be defined in an exception-specification");
21215 expr = cp_parser_constant_expression (parser, false, NULL);
21217 /* Restore the saved message. */
21218 parser->type_definition_forbidden_message = saved_message;
21220 else
21222 expr = cp_parser_expression (parser, false, NULL);
21223 *consumed_expr = true;
21226 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21228 else
21230 expr = boolean_true_node;
21231 if (!require_constexpr)
21232 *consumed_expr = false;
21235 /* We cannot build a noexcept-spec right away because this will check
21236 that expr is a constexpr. */
21237 if (!return_cond)
21238 return build_noexcept_spec (expr, tf_warning_or_error);
21239 else
21240 return expr;
21242 else
21243 return NULL_TREE;
21246 /* Parse an (optional) exception-specification.
21248 exception-specification:
21249 throw ( type-id-list [opt] )
21251 Returns a TREE_LIST representing the exception-specification. The
21252 TREE_VALUE of each node is a type. */
21254 static tree
21255 cp_parser_exception_specification_opt (cp_parser* parser)
21257 cp_token *token;
21258 tree type_id_list;
21259 const char *saved_message;
21261 /* Peek at the next token. */
21262 token = cp_lexer_peek_token (parser->lexer);
21264 /* Is it a noexcept-specification? */
21265 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21266 false);
21267 if (type_id_list != NULL_TREE)
21268 return type_id_list;
21270 /* If it's not `throw', then there's no exception-specification. */
21271 if (!cp_parser_is_keyword (token, RID_THROW))
21272 return NULL_TREE;
21274 #if 0
21275 /* Enable this once a lot of code has transitioned to noexcept? */
21276 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21277 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21278 "deprecated in C++0x; use %<noexcept%> instead");
21279 #endif
21281 /* Consume the `throw'. */
21282 cp_lexer_consume_token (parser->lexer);
21284 /* Look for the `('. */
21285 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21287 /* Peek at the next token. */
21288 token = cp_lexer_peek_token (parser->lexer);
21289 /* If it's not a `)', then there is a type-id-list. */
21290 if (token->type != CPP_CLOSE_PAREN)
21292 /* Types may not be defined in an exception-specification. */
21293 saved_message = parser->type_definition_forbidden_message;
21294 parser->type_definition_forbidden_message
21295 = G_("types may not be defined in an exception-specification");
21296 /* Parse the type-id-list. */
21297 type_id_list = cp_parser_type_id_list (parser);
21298 /* Restore the saved message. */
21299 parser->type_definition_forbidden_message = saved_message;
21301 else
21302 type_id_list = empty_except_spec;
21304 /* Look for the `)'. */
21305 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21307 return type_id_list;
21310 /* Parse an (optional) type-id-list.
21312 type-id-list:
21313 type-id ... [opt]
21314 type-id-list , type-id ... [opt]
21316 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21317 in the order that the types were presented. */
21319 static tree
21320 cp_parser_type_id_list (cp_parser* parser)
21322 tree types = NULL_TREE;
21324 while (true)
21326 cp_token *token;
21327 tree type;
21329 /* Get the next type-id. */
21330 type = cp_parser_type_id (parser);
21331 /* Parse the optional ellipsis. */
21332 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21334 /* Consume the `...'. */
21335 cp_lexer_consume_token (parser->lexer);
21337 /* Turn the type into a pack expansion expression. */
21338 type = make_pack_expansion (type);
21340 /* Add it to the list. */
21341 types = add_exception_specifier (types, type, /*complain=*/1);
21342 /* Peek at the next token. */
21343 token = cp_lexer_peek_token (parser->lexer);
21344 /* If it is not a `,', we are done. */
21345 if (token->type != CPP_COMMA)
21346 break;
21347 /* Consume the `,'. */
21348 cp_lexer_consume_token (parser->lexer);
21351 return nreverse (types);
21354 /* Parse a try-block.
21356 try-block:
21357 try compound-statement handler-seq */
21359 static tree
21360 cp_parser_try_block (cp_parser* parser)
21362 tree try_block;
21364 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21365 try_block = begin_try_block ();
21366 cp_parser_compound_statement (parser, NULL, true, false);
21367 finish_try_block (try_block);
21368 cp_parser_handler_seq (parser);
21369 finish_handler_sequence (try_block);
21371 return try_block;
21374 /* Parse a function-try-block.
21376 function-try-block:
21377 try ctor-initializer [opt] function-body handler-seq */
21379 static bool
21380 cp_parser_function_try_block (cp_parser* parser)
21382 tree compound_stmt;
21383 tree try_block;
21384 bool ctor_initializer_p;
21386 /* Look for the `try' keyword. */
21387 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21388 return false;
21389 /* Let the rest of the front end know where we are. */
21390 try_block = begin_function_try_block (&compound_stmt);
21391 /* Parse the function-body. */
21392 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21393 (parser, /*in_function_try_block=*/true);
21394 /* We're done with the `try' part. */
21395 finish_function_try_block (try_block);
21396 /* Parse the handlers. */
21397 cp_parser_handler_seq (parser);
21398 /* We're done with the handlers. */
21399 finish_function_handler_sequence (try_block, compound_stmt);
21401 return ctor_initializer_p;
21404 /* Parse a handler-seq.
21406 handler-seq:
21407 handler handler-seq [opt] */
21409 static void
21410 cp_parser_handler_seq (cp_parser* parser)
21412 while (true)
21414 cp_token *token;
21416 /* Parse the handler. */
21417 cp_parser_handler (parser);
21418 /* Peek at the next token. */
21419 token = cp_lexer_peek_token (parser->lexer);
21420 /* If it's not `catch' then there are no more handlers. */
21421 if (!cp_parser_is_keyword (token, RID_CATCH))
21422 break;
21426 /* Parse a handler.
21428 handler:
21429 catch ( exception-declaration ) compound-statement */
21431 static void
21432 cp_parser_handler (cp_parser* parser)
21434 tree handler;
21435 tree declaration;
21437 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21438 handler = begin_handler ();
21439 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21440 declaration = cp_parser_exception_declaration (parser);
21441 finish_handler_parms (declaration, handler);
21442 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21443 cp_parser_compound_statement (parser, NULL, false, false);
21444 finish_handler (handler);
21447 /* Parse an exception-declaration.
21449 exception-declaration:
21450 type-specifier-seq declarator
21451 type-specifier-seq abstract-declarator
21452 type-specifier-seq
21455 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21456 ellipsis variant is used. */
21458 static tree
21459 cp_parser_exception_declaration (cp_parser* parser)
21461 cp_decl_specifier_seq type_specifiers;
21462 cp_declarator *declarator;
21463 const char *saved_message;
21465 /* If it's an ellipsis, it's easy to handle. */
21466 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21468 /* Consume the `...' token. */
21469 cp_lexer_consume_token (parser->lexer);
21470 return NULL_TREE;
21473 /* Types may not be defined in exception-declarations. */
21474 saved_message = parser->type_definition_forbidden_message;
21475 parser->type_definition_forbidden_message
21476 = G_("types may not be defined in exception-declarations");
21478 /* Parse the type-specifier-seq. */
21479 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21480 /*is_trailing_return=*/false,
21481 &type_specifiers);
21482 /* If it's a `)', then there is no declarator. */
21483 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21484 declarator = NULL;
21485 else
21486 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21487 /*ctor_dtor_or_conv_p=*/NULL,
21488 /*parenthesized_p=*/NULL,
21489 /*member_p=*/false,
21490 /*friend_p=*/false);
21492 /* Restore the saved message. */
21493 parser->type_definition_forbidden_message = saved_message;
21495 if (!type_specifiers.any_specifiers_p)
21496 return error_mark_node;
21498 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21501 /* Parse a throw-expression.
21503 throw-expression:
21504 throw assignment-expression [opt]
21506 Returns a THROW_EXPR representing the throw-expression. */
21508 static tree
21509 cp_parser_throw_expression (cp_parser* parser)
21511 tree expression;
21512 cp_token* token;
21514 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21515 token = cp_lexer_peek_token (parser->lexer);
21516 /* Figure out whether or not there is an assignment-expression
21517 following the "throw" keyword. */
21518 if (token->type == CPP_COMMA
21519 || token->type == CPP_SEMICOLON
21520 || token->type == CPP_CLOSE_PAREN
21521 || token->type == CPP_CLOSE_SQUARE
21522 || token->type == CPP_CLOSE_BRACE
21523 || token->type == CPP_COLON)
21524 expression = NULL_TREE;
21525 else
21526 expression = cp_parser_assignment_expression (parser,
21527 /*cast_p=*/false, NULL);
21529 return build_throw (expression);
21532 /* GNU Extensions */
21534 /* Parse an (optional) asm-specification.
21536 asm-specification:
21537 asm ( string-literal )
21539 If the asm-specification is present, returns a STRING_CST
21540 corresponding to the string-literal. Otherwise, returns
21541 NULL_TREE. */
21543 static tree
21544 cp_parser_asm_specification_opt (cp_parser* parser)
21546 cp_token *token;
21547 tree asm_specification;
21549 /* Peek at the next token. */
21550 token = cp_lexer_peek_token (parser->lexer);
21551 /* If the next token isn't the `asm' keyword, then there's no
21552 asm-specification. */
21553 if (!cp_parser_is_keyword (token, RID_ASM))
21554 return NULL_TREE;
21556 /* Consume the `asm' token. */
21557 cp_lexer_consume_token (parser->lexer);
21558 /* Look for the `('. */
21559 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21561 /* Look for the string-literal. */
21562 asm_specification = cp_parser_string_literal (parser, false, false);
21564 /* Look for the `)'. */
21565 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21567 return asm_specification;
21570 /* Parse an asm-operand-list.
21572 asm-operand-list:
21573 asm-operand
21574 asm-operand-list , asm-operand
21576 asm-operand:
21577 string-literal ( expression )
21578 [ string-literal ] string-literal ( expression )
21580 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21581 each node is the expression. The TREE_PURPOSE is itself a
21582 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21583 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21584 is a STRING_CST for the string literal before the parenthesis. Returns
21585 ERROR_MARK_NODE if any of the operands are invalid. */
21587 static tree
21588 cp_parser_asm_operand_list (cp_parser* parser)
21590 tree asm_operands = NULL_TREE;
21591 bool invalid_operands = false;
21593 while (true)
21595 tree string_literal;
21596 tree expression;
21597 tree name;
21599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21601 /* Consume the `[' token. */
21602 cp_lexer_consume_token (parser->lexer);
21603 /* Read the operand name. */
21604 name = cp_parser_identifier (parser);
21605 if (name != error_mark_node)
21606 name = build_string (IDENTIFIER_LENGTH (name),
21607 IDENTIFIER_POINTER (name));
21608 /* Look for the closing `]'. */
21609 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21611 else
21612 name = NULL_TREE;
21613 /* Look for the string-literal. */
21614 string_literal = cp_parser_string_literal (parser, false, false);
21616 /* Look for the `('. */
21617 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21618 /* Parse the expression. */
21619 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21620 /* Look for the `)'. */
21621 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21623 if (name == error_mark_node
21624 || string_literal == error_mark_node
21625 || expression == error_mark_node)
21626 invalid_operands = true;
21628 /* Add this operand to the list. */
21629 asm_operands = tree_cons (build_tree_list (name, string_literal),
21630 expression,
21631 asm_operands);
21632 /* If the next token is not a `,', there are no more
21633 operands. */
21634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21635 break;
21636 /* Consume the `,'. */
21637 cp_lexer_consume_token (parser->lexer);
21640 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21643 /* Parse an asm-clobber-list.
21645 asm-clobber-list:
21646 string-literal
21647 asm-clobber-list , string-literal
21649 Returns a TREE_LIST, indicating the clobbers in the order that they
21650 appeared. The TREE_VALUE of each node is a STRING_CST. */
21652 static tree
21653 cp_parser_asm_clobber_list (cp_parser* parser)
21655 tree clobbers = NULL_TREE;
21657 while (true)
21659 tree string_literal;
21661 /* Look for the string literal. */
21662 string_literal = cp_parser_string_literal (parser, false, false);
21663 /* Add it to the list. */
21664 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21665 /* If the next token is not a `,', then the list is
21666 complete. */
21667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21668 break;
21669 /* Consume the `,' token. */
21670 cp_lexer_consume_token (parser->lexer);
21673 return clobbers;
21676 /* Parse an asm-label-list.
21678 asm-label-list:
21679 identifier
21680 asm-label-list , identifier
21682 Returns a TREE_LIST, indicating the labels in the order that they
21683 appeared. The TREE_VALUE of each node is a label. */
21685 static tree
21686 cp_parser_asm_label_list (cp_parser* parser)
21688 tree labels = NULL_TREE;
21690 while (true)
21692 tree identifier, label, name;
21694 /* Look for the identifier. */
21695 identifier = cp_parser_identifier (parser);
21696 if (!error_operand_p (identifier))
21698 label = lookup_label (identifier);
21699 if (TREE_CODE (label) == LABEL_DECL)
21701 TREE_USED (label) = 1;
21702 check_goto (label);
21703 name = build_string (IDENTIFIER_LENGTH (identifier),
21704 IDENTIFIER_POINTER (identifier));
21705 labels = tree_cons (name, label, labels);
21708 /* If the next token is not a `,', then the list is
21709 complete. */
21710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21711 break;
21712 /* Consume the `,' token. */
21713 cp_lexer_consume_token (parser->lexer);
21716 return nreverse (labels);
21719 /* Return TRUE iff the next tokens in the stream are possibly the
21720 beginning of a GNU extension attribute. */
21722 static bool
21723 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21725 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21728 /* Return TRUE iff the next tokens in the stream are possibly the
21729 beginning of a standard C++-11 attribute specifier. */
21731 static bool
21732 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21734 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21737 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21738 beginning of a standard C++-11 attribute specifier. */
21740 static bool
21741 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21743 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21745 return (cxx_dialect >= cxx11
21746 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21747 || (token->type == CPP_OPEN_SQUARE
21748 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21749 && token->type == CPP_OPEN_SQUARE)));
21752 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21753 beginning of a GNU extension attribute. */
21755 static bool
21756 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21758 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21760 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21763 /* Return true iff the next tokens can be the beginning of either a
21764 GNU attribute list, or a standard C++11 attribute sequence. */
21766 static bool
21767 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21769 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21770 || cp_next_tokens_can_be_std_attribute_p (parser));
21773 /* Return true iff the next Nth tokens can be the beginning of either
21774 a GNU attribute list, or a standard C++11 attribute sequence. */
21776 static bool
21777 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21779 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21780 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21783 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21784 of GNU attributes, or return NULL. */
21786 static tree
21787 cp_parser_attributes_opt (cp_parser *parser)
21789 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21790 return cp_parser_gnu_attributes_opt (parser);
21791 return cp_parser_std_attribute_spec_seq (parser);
21794 #define CILK_SIMD_FN_CLAUSE_MASK \
21795 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21796 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21797 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21798 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21799 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21801 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21802 vector [(<clauses>)] */
21804 static void
21805 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21807 bool first_p = parser->cilk_simd_fn_info == NULL;
21808 cp_token *token = v_token;
21809 if (first_p)
21811 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21812 parser->cilk_simd_fn_info->error_seen = false;
21813 parser->cilk_simd_fn_info->fndecl_seen = false;
21814 parser->cilk_simd_fn_info->tokens = vNULL;
21816 int paren_scope = 0;
21817 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21819 cp_lexer_consume_token (parser->lexer);
21820 v_token = cp_lexer_peek_token (parser->lexer);
21821 paren_scope++;
21823 while (paren_scope > 0)
21825 token = cp_lexer_peek_token (parser->lexer);
21826 if (token->type == CPP_OPEN_PAREN)
21827 paren_scope++;
21828 else if (token->type == CPP_CLOSE_PAREN)
21829 paren_scope--;
21830 /* Do not push the last ')' */
21831 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21832 cp_lexer_consume_token (parser->lexer);
21835 token->type = CPP_PRAGMA_EOL;
21836 parser->lexer->next_token = token;
21837 cp_lexer_consume_token (parser->lexer);
21839 struct cp_token_cache *cp
21840 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21841 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21844 /* Parse an (optional) series of attributes.
21846 attributes:
21847 attributes attribute
21849 attribute:
21850 __attribute__ (( attribute-list [opt] ))
21852 The return value is as for cp_parser_gnu_attribute_list. */
21854 static tree
21855 cp_parser_gnu_attributes_opt (cp_parser* parser)
21857 tree attributes = NULL_TREE;
21859 while (true)
21861 cp_token *token;
21862 tree attribute_list;
21863 bool ok = true;
21865 /* Peek at the next token. */
21866 token = cp_lexer_peek_token (parser->lexer);
21867 /* If it's not `__attribute__', then we're done. */
21868 if (token->keyword != RID_ATTRIBUTE)
21869 break;
21871 /* Consume the `__attribute__' keyword. */
21872 cp_lexer_consume_token (parser->lexer);
21873 /* Look for the two `(' tokens. */
21874 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21875 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21877 /* Peek at the next token. */
21878 token = cp_lexer_peek_token (parser->lexer);
21879 if (token->type != CPP_CLOSE_PAREN)
21880 /* Parse the attribute-list. */
21881 attribute_list = cp_parser_gnu_attribute_list (parser);
21882 else
21883 /* If the next token is a `)', then there is no attribute
21884 list. */
21885 attribute_list = NULL;
21887 /* Look for the two `)' tokens. */
21888 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21889 ok = false;
21890 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21891 ok = false;
21892 if (!ok)
21893 cp_parser_skip_to_end_of_statement (parser);
21895 /* Add these new attributes to the list. */
21896 attributes = chainon (attributes, attribute_list);
21899 return attributes;
21902 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21903 "__vector" or "__vector__." */
21905 static inline bool
21906 is_cilkplus_vector_p (tree name)
21908 if (flag_cilkplus && is_attribute_p ("vector", name))
21909 return true;
21910 return false;
21913 /* Parse a GNU attribute-list.
21915 attribute-list:
21916 attribute
21917 attribute-list , attribute
21919 attribute:
21920 identifier
21921 identifier ( identifier )
21922 identifier ( identifier , expression-list )
21923 identifier ( expression-list )
21925 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21926 to an attribute. The TREE_PURPOSE of each node is the identifier
21927 indicating which attribute is in use. The TREE_VALUE represents
21928 the arguments, if any. */
21930 static tree
21931 cp_parser_gnu_attribute_list (cp_parser* parser)
21933 tree attribute_list = NULL_TREE;
21934 bool save_translate_strings_p = parser->translate_strings_p;
21936 parser->translate_strings_p = false;
21937 while (true)
21939 cp_token *token;
21940 tree identifier;
21941 tree attribute;
21943 /* Look for the identifier. We also allow keywords here; for
21944 example `__attribute__ ((const))' is legal. */
21945 token = cp_lexer_peek_token (parser->lexer);
21946 if (token->type == CPP_NAME
21947 || token->type == CPP_KEYWORD)
21949 tree arguments = NULL_TREE;
21951 /* Consume the token, but save it since we need it for the
21952 SIMD enabled function parsing. */
21953 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21955 /* Save away the identifier that indicates which attribute
21956 this is. */
21957 identifier = (token->type == CPP_KEYWORD)
21958 /* For keywords, use the canonical spelling, not the
21959 parsed identifier. */
21960 ? ridpointers[(int) token->keyword]
21961 : id_token->u.value;
21963 attribute = build_tree_list (identifier, NULL_TREE);
21965 /* Peek at the next token. */
21966 token = cp_lexer_peek_token (parser->lexer);
21967 /* If it's an `(', then parse the attribute arguments. */
21968 if (token->type == CPP_OPEN_PAREN)
21970 vec<tree, va_gc> *vec;
21971 int attr_flag = (attribute_takes_identifier_p (identifier)
21972 ? id_attr : normal_attr);
21973 if (is_cilkplus_vector_p (identifier))
21975 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21976 continue;
21978 else
21979 vec = cp_parser_parenthesized_expression_list
21980 (parser, attr_flag, /*cast_p=*/false,
21981 /*allow_expansion_p=*/false,
21982 /*non_constant_p=*/NULL);
21983 if (vec == NULL)
21984 arguments = error_mark_node;
21985 else
21987 arguments = build_tree_list_vec (vec);
21988 release_tree_vector (vec);
21990 /* Save the arguments away. */
21991 TREE_VALUE (attribute) = arguments;
21993 else if (is_cilkplus_vector_p (identifier))
21995 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21996 continue;
21999 if (arguments != error_mark_node)
22001 /* Add this attribute to the list. */
22002 TREE_CHAIN (attribute) = attribute_list;
22003 attribute_list = attribute;
22006 token = cp_lexer_peek_token (parser->lexer);
22008 /* Now, look for more attributes. If the next token isn't a
22009 `,', we're done. */
22010 if (token->type != CPP_COMMA)
22011 break;
22013 /* Consume the comma and keep going. */
22014 cp_lexer_consume_token (parser->lexer);
22016 parser->translate_strings_p = save_translate_strings_p;
22018 /* We built up the list in reverse order. */
22019 return nreverse (attribute_list);
22022 /* Parse a standard C++11 attribute.
22024 The returned representation is a TREE_LIST which TREE_PURPOSE is
22025 the scoped name of the attribute, and the TREE_VALUE is its
22026 arguments list.
22028 Note that the scoped name of the attribute is itself a TREE_LIST
22029 which TREE_PURPOSE is the namespace of the attribute, and
22030 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22031 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22032 and which TREE_PURPOSE is directly the attribute name.
22034 Clients of the attribute code should use get_attribute_namespace
22035 and get_attribute_name to get the actual namespace and name of
22036 attributes, regardless of their being GNU or C++11 attributes.
22038 attribute:
22039 attribute-token attribute-argument-clause [opt]
22041 attribute-token:
22042 identifier
22043 attribute-scoped-token
22045 attribute-scoped-token:
22046 attribute-namespace :: identifier
22048 attribute-namespace:
22049 identifier
22051 attribute-argument-clause:
22052 ( balanced-token-seq )
22054 balanced-token-seq:
22055 balanced-token [opt]
22056 balanced-token-seq balanced-token
22058 balanced-token:
22059 ( balanced-token-seq )
22060 [ balanced-token-seq ]
22061 { balanced-token-seq }. */
22063 static tree
22064 cp_parser_std_attribute (cp_parser *parser)
22066 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22067 cp_token *token;
22069 /* First, parse name of the the attribute, a.k.a
22070 attribute-token. */
22072 token = cp_lexer_peek_token (parser->lexer);
22073 if (token->type == CPP_NAME)
22074 attr_id = token->u.value;
22075 else if (token->type == CPP_KEYWORD)
22076 attr_id = ridpointers[(int) token->keyword];
22077 else if (token->flags & NAMED_OP)
22078 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22080 if (attr_id == NULL_TREE)
22081 return NULL_TREE;
22083 cp_lexer_consume_token (parser->lexer);
22085 token = cp_lexer_peek_token (parser->lexer);
22086 if (token->type == CPP_SCOPE)
22088 /* We are seeing a scoped attribute token. */
22090 cp_lexer_consume_token (parser->lexer);
22091 attr_ns = attr_id;
22093 token = cp_lexer_consume_token (parser->lexer);
22094 if (token->type == CPP_NAME)
22095 attr_id = token->u.value;
22096 else if (token->type == CPP_KEYWORD)
22097 attr_id = ridpointers[(int) token->keyword];
22098 else
22100 error_at (token->location,
22101 "expected an identifier for the attribute name");
22102 return error_mark_node;
22104 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22105 NULL_TREE);
22106 token = cp_lexer_peek_token (parser->lexer);
22108 else
22110 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22111 NULL_TREE);
22112 /* C++11 noreturn attribute is equivalent to GNU's. */
22113 if (is_attribute_p ("noreturn", attr_id))
22114 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22115 /* C++14 deprecated attribute is equivalent to GNU's. */
22116 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
22117 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22120 /* Now parse the optional argument clause of the attribute. */
22122 if (token->type != CPP_OPEN_PAREN)
22123 return attribute;
22126 vec<tree, va_gc> *vec;
22127 int attr_flag = normal_attr;
22129 if (attr_ns == get_identifier ("gnu")
22130 && attribute_takes_identifier_p (attr_id))
22131 /* A GNU attribute that takes an identifier in parameter. */
22132 attr_flag = id_attr;
22134 vec = cp_parser_parenthesized_expression_list
22135 (parser, attr_flag, /*cast_p=*/false,
22136 /*allow_expansion_p=*/true,
22137 /*non_constant_p=*/NULL);
22138 if (vec == NULL)
22139 arguments = error_mark_node;
22140 else
22142 arguments = build_tree_list_vec (vec);
22143 release_tree_vector (vec);
22146 if (arguments == error_mark_node)
22147 attribute = error_mark_node;
22148 else
22149 TREE_VALUE (attribute) = arguments;
22152 return attribute;
22155 /* Parse a list of standard C++-11 attributes.
22157 attribute-list:
22158 attribute [opt]
22159 attribute-list , attribute[opt]
22160 attribute ...
22161 attribute-list , attribute ...
22164 static tree
22165 cp_parser_std_attribute_list (cp_parser *parser)
22167 tree attributes = NULL_TREE, attribute = NULL_TREE;
22168 cp_token *token = NULL;
22170 while (true)
22172 attribute = cp_parser_std_attribute (parser);
22173 if (attribute == error_mark_node)
22174 break;
22175 if (attribute != NULL_TREE)
22177 TREE_CHAIN (attribute) = attributes;
22178 attributes = attribute;
22180 token = cp_lexer_peek_token (parser->lexer);
22181 if (token->type != CPP_COMMA)
22182 break;
22183 cp_lexer_consume_token (parser->lexer);
22185 attributes = nreverse (attributes);
22186 return attributes;
22189 /* Parse a standard C++-11 attribute specifier.
22191 attribute-specifier:
22192 [ [ attribute-list ] ]
22193 alignment-specifier
22195 alignment-specifier:
22196 alignas ( type-id ... [opt] )
22197 alignas ( alignment-expression ... [opt] ). */
22199 static tree
22200 cp_parser_std_attribute_spec (cp_parser *parser)
22202 tree attributes = NULL_TREE;
22203 cp_token *token = cp_lexer_peek_token (parser->lexer);
22205 if (token->type == CPP_OPEN_SQUARE
22206 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22208 cp_lexer_consume_token (parser->lexer);
22209 cp_lexer_consume_token (parser->lexer);
22211 attributes = cp_parser_std_attribute_list (parser);
22213 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22214 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22215 cp_parser_skip_to_end_of_statement (parser);
22216 else
22217 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22218 when we are sure that we have actually parsed them. */
22219 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22221 else
22223 tree alignas_expr;
22225 /* Look for an alignment-specifier. */
22227 token = cp_lexer_peek_token (parser->lexer);
22229 if (token->type != CPP_KEYWORD
22230 || token->keyword != RID_ALIGNAS)
22231 return NULL_TREE;
22233 cp_lexer_consume_token (parser->lexer);
22234 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22236 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22238 cp_parser_error (parser, "expected %<(%>");
22239 return error_mark_node;
22242 cp_parser_parse_tentatively (parser);
22243 alignas_expr = cp_parser_type_id (parser);
22245 if (!cp_parser_parse_definitely (parser))
22247 gcc_assert (alignas_expr == error_mark_node
22248 || alignas_expr == NULL_TREE);
22250 alignas_expr =
22251 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22252 /**cp_id_kind=*/NULL);
22253 if (alignas_expr == error_mark_node)
22254 cp_parser_skip_to_end_of_statement (parser);
22255 if (alignas_expr == NULL_TREE
22256 || alignas_expr == error_mark_node)
22257 return alignas_expr;
22260 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22262 cp_parser_error (parser, "expected %<)%>");
22263 return error_mark_node;
22266 alignas_expr = cxx_alignas_expr (alignas_expr);
22268 /* Build the C++-11 representation of an 'aligned'
22269 attribute. */
22270 attributes =
22271 build_tree_list (build_tree_list (get_identifier ("gnu"),
22272 get_identifier ("aligned")),
22273 build_tree_list (NULL_TREE, alignas_expr));
22276 return attributes;
22279 /* Parse a standard C++-11 attribute-specifier-seq.
22281 attribute-specifier-seq:
22282 attribute-specifier-seq [opt] attribute-specifier
22285 static tree
22286 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22288 tree attr_specs = NULL;
22290 while (true)
22292 tree attr_spec = cp_parser_std_attribute_spec (parser);
22293 if (attr_spec == NULL_TREE)
22294 break;
22295 if (attr_spec == error_mark_node)
22296 return error_mark_node;
22298 TREE_CHAIN (attr_spec) = attr_specs;
22299 attr_specs = attr_spec;
22302 attr_specs = nreverse (attr_specs);
22303 return attr_specs;
22306 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22307 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22308 current value of the PEDANTIC flag, regardless of whether or not
22309 the `__extension__' keyword is present. The caller is responsible
22310 for restoring the value of the PEDANTIC flag. */
22312 static bool
22313 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22315 /* Save the old value of the PEDANTIC flag. */
22316 *saved_pedantic = pedantic;
22318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22320 /* Consume the `__extension__' token. */
22321 cp_lexer_consume_token (parser->lexer);
22322 /* We're not being pedantic while the `__extension__' keyword is
22323 in effect. */
22324 pedantic = 0;
22326 return true;
22329 return false;
22332 /* Parse a label declaration.
22334 label-declaration:
22335 __label__ label-declarator-seq ;
22337 label-declarator-seq:
22338 identifier , label-declarator-seq
22339 identifier */
22341 static void
22342 cp_parser_label_declaration (cp_parser* parser)
22344 /* Look for the `__label__' keyword. */
22345 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22347 while (true)
22349 tree identifier;
22351 /* Look for an identifier. */
22352 identifier = cp_parser_identifier (parser);
22353 /* If we failed, stop. */
22354 if (identifier == error_mark_node)
22355 break;
22356 /* Declare it as a label. */
22357 finish_label_decl (identifier);
22358 /* If the next token is a `;', stop. */
22359 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22360 break;
22361 /* Look for the `,' separating the label declarations. */
22362 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22365 /* Look for the final `;'. */
22366 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22369 /* Support Functions */
22371 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22372 NAME should have one of the representations used for an
22373 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22374 is returned. If PARSER->SCOPE is a dependent type, then a
22375 SCOPE_REF is returned.
22377 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22378 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22379 was formed. Abstractly, such entities should not be passed to this
22380 function, because they do not need to be looked up, but it is
22381 simpler to check for this special case here, rather than at the
22382 call-sites.
22384 In cases not explicitly covered above, this function returns a
22385 DECL, OVERLOAD, or baselink representing the result of the lookup.
22386 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22387 is returned.
22389 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22390 (e.g., "struct") that was used. In that case bindings that do not
22391 refer to types are ignored.
22393 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22394 ignored.
22396 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22397 are ignored.
22399 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22400 types.
22402 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22403 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22404 NULL_TREE otherwise. */
22406 static tree
22407 cp_parser_lookup_name (cp_parser *parser, tree name,
22408 enum tag_types tag_type,
22409 bool is_template,
22410 bool is_namespace,
22411 bool check_dependency,
22412 tree *ambiguous_decls,
22413 location_t name_location)
22415 tree decl;
22416 tree object_type = parser->context->object_type;
22418 /* Assume that the lookup will be unambiguous. */
22419 if (ambiguous_decls)
22420 *ambiguous_decls = NULL_TREE;
22422 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22423 no longer valid. Note that if we are parsing tentatively, and
22424 the parse fails, OBJECT_TYPE will be automatically restored. */
22425 parser->context->object_type = NULL_TREE;
22427 if (name == error_mark_node)
22428 return error_mark_node;
22430 /* A template-id has already been resolved; there is no lookup to
22431 do. */
22432 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22433 return name;
22434 if (BASELINK_P (name))
22436 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22437 == TEMPLATE_ID_EXPR);
22438 return name;
22441 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22442 it should already have been checked to make sure that the name
22443 used matches the type being destroyed. */
22444 if (TREE_CODE (name) == BIT_NOT_EXPR)
22446 tree type;
22448 /* Figure out to which type this destructor applies. */
22449 if (parser->scope)
22450 type = parser->scope;
22451 else if (object_type)
22452 type = object_type;
22453 else
22454 type = current_class_type;
22455 /* If that's not a class type, there is no destructor. */
22456 if (!type || !CLASS_TYPE_P (type))
22457 return error_mark_node;
22458 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22459 lazily_declare_fn (sfk_destructor, type);
22460 if (!CLASSTYPE_DESTRUCTORS (type))
22461 return error_mark_node;
22462 /* If it was a class type, return the destructor. */
22463 return CLASSTYPE_DESTRUCTORS (type);
22466 /* By this point, the NAME should be an ordinary identifier. If
22467 the id-expression was a qualified name, the qualifying scope is
22468 stored in PARSER->SCOPE at this point. */
22469 gcc_assert (identifier_p (name));
22471 /* Perform the lookup. */
22472 if (parser->scope)
22474 bool dependent_p;
22476 if (parser->scope == error_mark_node)
22477 return error_mark_node;
22479 /* If the SCOPE is dependent, the lookup must be deferred until
22480 the template is instantiated -- unless we are explicitly
22481 looking up names in uninstantiated templates. Even then, we
22482 cannot look up the name if the scope is not a class type; it
22483 might, for example, be a template type parameter. */
22484 dependent_p = (TYPE_P (parser->scope)
22485 && dependent_scope_p (parser->scope));
22486 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22487 && dependent_p)
22488 /* Defer lookup. */
22489 decl = error_mark_node;
22490 else
22492 tree pushed_scope = NULL_TREE;
22494 /* If PARSER->SCOPE is a dependent type, then it must be a
22495 class type, and we must not be checking dependencies;
22496 otherwise, we would have processed this lookup above. So
22497 that PARSER->SCOPE is not considered a dependent base by
22498 lookup_member, we must enter the scope here. */
22499 if (dependent_p)
22500 pushed_scope = push_scope (parser->scope);
22502 /* If the PARSER->SCOPE is a template specialization, it
22503 may be instantiated during name lookup. In that case,
22504 errors may be issued. Even if we rollback the current
22505 tentative parse, those errors are valid. */
22506 decl = lookup_qualified_name (parser->scope, name,
22507 tag_type != none_type,
22508 /*complain=*/true);
22510 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22511 lookup result and the nested-name-specifier nominates a class C:
22512 * if the name specified after the nested-name-specifier, when
22513 looked up in C, is the injected-class-name of C (Clause 9), or
22514 * if the name specified after the nested-name-specifier is the
22515 same as the identifier or the simple-template-id's template-
22516 name in the last component of the nested-name-specifier,
22517 the name is instead considered to name the constructor of
22518 class C. [ Note: for example, the constructor is not an
22519 acceptable lookup result in an elaborated-type-specifier so
22520 the constructor would not be used in place of the
22521 injected-class-name. --end note ] Such a constructor name
22522 shall be used only in the declarator-id of a declaration that
22523 names a constructor or in a using-declaration. */
22524 if (tag_type == none_type
22525 && DECL_SELF_REFERENCE_P (decl)
22526 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22527 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22528 tag_type != none_type,
22529 /*complain=*/true);
22531 /* If we have a single function from a using decl, pull it out. */
22532 if (TREE_CODE (decl) == OVERLOAD
22533 && !really_overloaded_fn (decl))
22534 decl = OVL_FUNCTION (decl);
22536 if (pushed_scope)
22537 pop_scope (pushed_scope);
22540 /* If the scope is a dependent type and either we deferred lookup or
22541 we did lookup but didn't find the name, rememeber the name. */
22542 if (decl == error_mark_node && TYPE_P (parser->scope)
22543 && dependent_type_p (parser->scope))
22545 if (tag_type)
22547 tree type;
22549 /* The resolution to Core Issue 180 says that `struct
22550 A::B' should be considered a type-name, even if `A'
22551 is dependent. */
22552 type = make_typename_type (parser->scope, name, tag_type,
22553 /*complain=*/tf_error);
22554 if (type != error_mark_node)
22555 decl = TYPE_NAME (type);
22557 else if (is_template
22558 && (cp_parser_next_token_ends_template_argument_p (parser)
22559 || cp_lexer_next_token_is (parser->lexer,
22560 CPP_CLOSE_PAREN)))
22561 decl = make_unbound_class_template (parser->scope,
22562 name, NULL_TREE,
22563 /*complain=*/tf_error);
22564 else
22565 decl = build_qualified_name (/*type=*/NULL_TREE,
22566 parser->scope, name,
22567 is_template);
22569 parser->qualifying_scope = parser->scope;
22570 parser->object_scope = NULL_TREE;
22572 else if (object_type)
22574 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22575 OBJECT_TYPE is not a class. */
22576 if (CLASS_TYPE_P (object_type))
22577 /* If the OBJECT_TYPE is a template specialization, it may
22578 be instantiated during name lookup. In that case, errors
22579 may be issued. Even if we rollback the current tentative
22580 parse, those errors are valid. */
22581 decl = lookup_member (object_type,
22582 name,
22583 /*protect=*/0,
22584 tag_type != none_type,
22585 tf_warning_or_error);
22586 else
22587 decl = NULL_TREE;
22589 if (!decl)
22590 /* Look it up in the enclosing context. */
22591 decl = lookup_name_real (name, tag_type != none_type,
22592 /*nonclass=*/0,
22593 /*block_p=*/true, is_namespace, 0);
22594 parser->object_scope = object_type;
22595 parser->qualifying_scope = NULL_TREE;
22597 else
22599 decl = lookup_name_real (name, tag_type != none_type,
22600 /*nonclass=*/0,
22601 /*block_p=*/true, is_namespace, 0);
22602 parser->qualifying_scope = NULL_TREE;
22603 parser->object_scope = NULL_TREE;
22606 /* If the lookup failed, let our caller know. */
22607 if (!decl || decl == error_mark_node)
22608 return error_mark_node;
22610 /* Pull out the template from an injected-class-name (or multiple). */
22611 if (is_template)
22612 decl = maybe_get_template_decl_from_type_decl (decl);
22614 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22615 if (TREE_CODE (decl) == TREE_LIST)
22617 if (ambiguous_decls)
22618 *ambiguous_decls = decl;
22619 /* The error message we have to print is too complicated for
22620 cp_parser_error, so we incorporate its actions directly. */
22621 if (!cp_parser_simulate_error (parser))
22623 error_at (name_location, "reference to %qD is ambiguous",
22624 name);
22625 print_candidates (decl);
22627 return error_mark_node;
22630 gcc_assert (DECL_P (decl)
22631 || TREE_CODE (decl) == OVERLOAD
22632 || TREE_CODE (decl) == SCOPE_REF
22633 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22634 || BASELINK_P (decl));
22636 /* If we have resolved the name of a member declaration, check to
22637 see if the declaration is accessible. When the name resolves to
22638 set of overloaded functions, accessibility is checked when
22639 overload resolution is done.
22641 During an explicit instantiation, access is not checked at all,
22642 as per [temp.explicit]. */
22643 if (DECL_P (decl))
22644 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22646 maybe_record_typedef_use (decl);
22648 return decl;
22651 /* Like cp_parser_lookup_name, but for use in the typical case where
22652 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22653 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22655 static tree
22656 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22658 return cp_parser_lookup_name (parser, name,
22659 none_type,
22660 /*is_template=*/false,
22661 /*is_namespace=*/false,
22662 /*check_dependency=*/true,
22663 /*ambiguous_decls=*/NULL,
22664 location);
22667 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22668 the current context, return the TYPE_DECL. If TAG_NAME_P is
22669 true, the DECL indicates the class being defined in a class-head,
22670 or declared in an elaborated-type-specifier.
22672 Otherwise, return DECL. */
22674 static tree
22675 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22677 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22678 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22680 struct A {
22681 template <typename T> struct B;
22684 template <typename T> struct A::B {};
22686 Similarly, in an elaborated-type-specifier:
22688 namespace N { struct X{}; }
22690 struct A {
22691 template <typename T> friend struct N::X;
22694 However, if the DECL refers to a class type, and we are in
22695 the scope of the class, then the name lookup automatically
22696 finds the TYPE_DECL created by build_self_reference rather
22697 than a TEMPLATE_DECL. For example, in:
22699 template <class T> struct S {
22700 S s;
22703 there is no need to handle such case. */
22705 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22706 return DECL_TEMPLATE_RESULT (decl);
22708 return decl;
22711 /* If too many, or too few, template-parameter lists apply to the
22712 declarator, issue an error message. Returns TRUE if all went well,
22713 and FALSE otherwise. */
22715 static bool
22716 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22717 cp_declarator *declarator,
22718 location_t declarator_location)
22720 switch (declarator->kind)
22722 case cdk_id:
22724 unsigned num_templates = 0;
22725 tree scope = declarator->u.id.qualifying_scope;
22727 if (scope)
22728 num_templates = num_template_headers_for_class (scope);
22729 else if (TREE_CODE (declarator->u.id.unqualified_name)
22730 == TEMPLATE_ID_EXPR)
22731 /* If the DECLARATOR has the form `X<y>' then it uses one
22732 additional level of template parameters. */
22733 ++num_templates;
22735 return cp_parser_check_template_parameters
22736 (parser, num_templates, declarator_location, declarator);
22739 case cdk_function:
22740 case cdk_array:
22741 case cdk_pointer:
22742 case cdk_reference:
22743 case cdk_ptrmem:
22744 return (cp_parser_check_declarator_template_parameters
22745 (parser, declarator->declarator, declarator_location));
22747 case cdk_error:
22748 return true;
22750 default:
22751 gcc_unreachable ();
22753 return false;
22756 /* NUM_TEMPLATES were used in the current declaration. If that is
22757 invalid, return FALSE and issue an error messages. Otherwise,
22758 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22759 declarator and we can print more accurate diagnostics. */
22761 static bool
22762 cp_parser_check_template_parameters (cp_parser* parser,
22763 unsigned num_templates,
22764 location_t location,
22765 cp_declarator *declarator)
22767 /* If there are the same number of template classes and parameter
22768 lists, that's OK. */
22769 if (parser->num_template_parameter_lists == num_templates)
22770 return true;
22771 /* If there are more, but only one more, then we are referring to a
22772 member template. That's OK too. */
22773 if (parser->num_template_parameter_lists == num_templates + 1)
22774 return true;
22775 /* If there are more template classes than parameter lists, we have
22776 something like:
22778 template <class T> void S<T>::R<T>::f (); */
22779 if (parser->num_template_parameter_lists < num_templates)
22781 if (declarator && !current_function_decl)
22782 error_at (location, "specializing member %<%T::%E%> "
22783 "requires %<template<>%> syntax",
22784 declarator->u.id.qualifying_scope,
22785 declarator->u.id.unqualified_name);
22786 else if (declarator)
22787 error_at (location, "invalid declaration of %<%T::%E%>",
22788 declarator->u.id.qualifying_scope,
22789 declarator->u.id.unqualified_name);
22790 else
22791 error_at (location, "too few template-parameter-lists");
22792 return false;
22794 /* Otherwise, there are too many template parameter lists. We have
22795 something like:
22797 template <class T> template <class U> void S::f(); */
22798 error_at (location, "too many template-parameter-lists");
22799 return false;
22802 /* Parse an optional `::' token indicating that the following name is
22803 from the global namespace. If so, PARSER->SCOPE is set to the
22804 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22805 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22806 Returns the new value of PARSER->SCOPE, if the `::' token is
22807 present, and NULL_TREE otherwise. */
22809 static tree
22810 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22812 cp_token *token;
22814 /* Peek at the next token. */
22815 token = cp_lexer_peek_token (parser->lexer);
22816 /* If we're looking at a `::' token then we're starting from the
22817 global namespace, not our current location. */
22818 if (token->type == CPP_SCOPE)
22820 /* Consume the `::' token. */
22821 cp_lexer_consume_token (parser->lexer);
22822 /* Set the SCOPE so that we know where to start the lookup. */
22823 parser->scope = global_namespace;
22824 parser->qualifying_scope = global_namespace;
22825 parser->object_scope = NULL_TREE;
22827 return parser->scope;
22829 else if (!current_scope_valid_p)
22831 parser->scope = NULL_TREE;
22832 parser->qualifying_scope = NULL_TREE;
22833 parser->object_scope = NULL_TREE;
22836 return NULL_TREE;
22839 /* Returns TRUE if the upcoming token sequence is the start of a
22840 constructor declarator. If FRIEND_P is true, the declarator is
22841 preceded by the `friend' specifier. */
22843 static bool
22844 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22846 bool constructor_p;
22847 bool outside_class_specifier_p;
22848 tree nested_name_specifier;
22849 cp_token *next_token;
22851 /* The common case is that this is not a constructor declarator, so
22852 try to avoid doing lots of work if at all possible. It's not
22853 valid declare a constructor at function scope. */
22854 if (parser->in_function_body)
22855 return false;
22856 /* And only certain tokens can begin a constructor declarator. */
22857 next_token = cp_lexer_peek_token (parser->lexer);
22858 if (next_token->type != CPP_NAME
22859 && next_token->type != CPP_SCOPE
22860 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22861 && next_token->type != CPP_TEMPLATE_ID)
22862 return false;
22864 /* Parse tentatively; we are going to roll back all of the tokens
22865 consumed here. */
22866 cp_parser_parse_tentatively (parser);
22867 /* Assume that we are looking at a constructor declarator. */
22868 constructor_p = true;
22870 /* Look for the optional `::' operator. */
22871 cp_parser_global_scope_opt (parser,
22872 /*current_scope_valid_p=*/false);
22873 /* Look for the nested-name-specifier. */
22874 nested_name_specifier
22875 = (cp_parser_nested_name_specifier_opt (parser,
22876 /*typename_keyword_p=*/false,
22877 /*check_dependency_p=*/false,
22878 /*type_p=*/false,
22879 /*is_declaration=*/false));
22881 outside_class_specifier_p = (!at_class_scope_p ()
22882 || !TYPE_BEING_DEFINED (current_class_type)
22883 || friend_p);
22885 /* Outside of a class-specifier, there must be a
22886 nested-name-specifier. */
22887 if (!nested_name_specifier && outside_class_specifier_p)
22888 constructor_p = false;
22889 else if (nested_name_specifier == error_mark_node)
22890 constructor_p = false;
22892 /* If we have a class scope, this is easy; DR 147 says that S::S always
22893 names the constructor, and no other qualified name could. */
22894 if (constructor_p && nested_name_specifier
22895 && CLASS_TYPE_P (nested_name_specifier))
22897 tree id = cp_parser_unqualified_id (parser,
22898 /*template_keyword_p=*/false,
22899 /*check_dependency_p=*/false,
22900 /*declarator_p=*/true,
22901 /*optional_p=*/false);
22902 if (is_overloaded_fn (id))
22903 id = DECL_NAME (get_first_fn (id));
22904 if (!constructor_name_p (id, nested_name_specifier))
22905 constructor_p = false;
22907 /* If we still think that this might be a constructor-declarator,
22908 look for a class-name. */
22909 else if (constructor_p)
22911 /* If we have:
22913 template <typename T> struct S {
22914 S();
22917 we must recognize that the nested `S' names a class. */
22918 tree type_decl;
22919 type_decl = cp_parser_class_name (parser,
22920 /*typename_keyword_p=*/false,
22921 /*template_keyword_p=*/false,
22922 none_type,
22923 /*check_dependency_p=*/false,
22924 /*class_head_p=*/false,
22925 /*is_declaration=*/false);
22926 /* If there was no class-name, then this is not a constructor.
22927 Otherwise, if we are in a class-specifier and we aren't
22928 handling a friend declaration, check that its type matches
22929 current_class_type (c++/38313). Note: error_mark_node
22930 is left alone for error recovery purposes. */
22931 constructor_p = (!cp_parser_error_occurred (parser)
22932 && (outside_class_specifier_p
22933 || type_decl == error_mark_node
22934 || same_type_p (current_class_type,
22935 TREE_TYPE (type_decl))));
22937 /* If we're still considering a constructor, we have to see a `(',
22938 to begin the parameter-declaration-clause, followed by either a
22939 `)', an `...', or a decl-specifier. We need to check for a
22940 type-specifier to avoid being fooled into thinking that:
22942 S (f) (int);
22944 is a constructor. (It is actually a function named `f' that
22945 takes one parameter (of type `int') and returns a value of type
22946 `S'. */
22947 if (constructor_p
22948 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22949 constructor_p = false;
22951 if (constructor_p
22952 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22953 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22954 /* A parameter declaration begins with a decl-specifier,
22955 which is either the "attribute" keyword, a storage class
22956 specifier, or (usually) a type-specifier. */
22957 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22959 tree type;
22960 tree pushed_scope = NULL_TREE;
22961 unsigned saved_num_template_parameter_lists;
22963 /* Names appearing in the type-specifier should be looked up
22964 in the scope of the class. */
22965 if (current_class_type)
22966 type = NULL_TREE;
22967 else
22969 type = TREE_TYPE (type_decl);
22970 if (TREE_CODE (type) == TYPENAME_TYPE)
22972 type = resolve_typename_type (type,
22973 /*only_current_p=*/false);
22974 if (TREE_CODE (type) == TYPENAME_TYPE)
22976 cp_parser_abort_tentative_parse (parser);
22977 return false;
22980 pushed_scope = push_scope (type);
22983 /* Inside the constructor parameter list, surrounding
22984 template-parameter-lists do not apply. */
22985 saved_num_template_parameter_lists
22986 = parser->num_template_parameter_lists;
22987 parser->num_template_parameter_lists = 0;
22989 /* Look for the type-specifier. */
22990 cp_parser_type_specifier (parser,
22991 CP_PARSER_FLAGS_NONE,
22992 /*decl_specs=*/NULL,
22993 /*is_declarator=*/true,
22994 /*declares_class_or_enum=*/NULL,
22995 /*is_cv_qualifier=*/NULL);
22997 parser->num_template_parameter_lists
22998 = saved_num_template_parameter_lists;
23000 /* Leave the scope of the class. */
23001 if (pushed_scope)
23002 pop_scope (pushed_scope);
23004 constructor_p = !cp_parser_error_occurred (parser);
23008 /* We did not really want to consume any tokens. */
23009 cp_parser_abort_tentative_parse (parser);
23011 return constructor_p;
23014 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23015 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23016 they must be performed once we are in the scope of the function.
23018 Returns the function defined. */
23020 static tree
23021 cp_parser_function_definition_from_specifiers_and_declarator
23022 (cp_parser* parser,
23023 cp_decl_specifier_seq *decl_specifiers,
23024 tree attributes,
23025 const cp_declarator *declarator)
23027 tree fn;
23028 bool success_p;
23030 /* Begin the function-definition. */
23031 success_p = start_function (decl_specifiers, declarator, attributes);
23033 /* The things we're about to see are not directly qualified by any
23034 template headers we've seen thus far. */
23035 reset_specialization ();
23037 /* If there were names looked up in the decl-specifier-seq that we
23038 did not check, check them now. We must wait until we are in the
23039 scope of the function to perform the checks, since the function
23040 might be a friend. */
23041 perform_deferred_access_checks (tf_warning_or_error);
23043 if (success_p)
23045 cp_finalize_omp_declare_simd (parser, current_function_decl);
23046 parser->omp_declare_simd = NULL;
23049 if (!success_p)
23051 /* Skip the entire function. */
23052 cp_parser_skip_to_end_of_block_or_statement (parser);
23053 fn = error_mark_node;
23055 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23057 /* Seen already, skip it. An error message has already been output. */
23058 cp_parser_skip_to_end_of_block_or_statement (parser);
23059 fn = current_function_decl;
23060 current_function_decl = NULL_TREE;
23061 /* If this is a function from a class, pop the nested class. */
23062 if (current_class_name)
23063 pop_nested_class ();
23065 else
23067 timevar_id_t tv;
23068 if (DECL_DECLARED_INLINE_P (current_function_decl))
23069 tv = TV_PARSE_INLINE;
23070 else
23071 tv = TV_PARSE_FUNC;
23072 timevar_push (tv);
23073 fn = cp_parser_function_definition_after_declarator (parser,
23074 /*inline_p=*/false);
23075 timevar_pop (tv);
23078 return fn;
23081 /* Parse the part of a function-definition that follows the
23082 declarator. INLINE_P is TRUE iff this function is an inline
23083 function defined within a class-specifier.
23085 Returns the function defined. */
23087 static tree
23088 cp_parser_function_definition_after_declarator (cp_parser* parser,
23089 bool inline_p)
23091 tree fn;
23092 bool ctor_initializer_p = false;
23093 bool saved_in_unbraced_linkage_specification_p;
23094 bool saved_in_function_body;
23095 unsigned saved_num_template_parameter_lists;
23096 cp_token *token;
23097 bool fully_implicit_function_template_p
23098 = parser->fully_implicit_function_template_p;
23099 parser->fully_implicit_function_template_p = false;
23100 tree implicit_template_parms
23101 = parser->implicit_template_parms;
23102 parser->implicit_template_parms = 0;
23103 cp_binding_level* implicit_template_scope
23104 = parser->implicit_template_scope;
23105 parser->implicit_template_scope = 0;
23107 saved_in_function_body = parser->in_function_body;
23108 parser->in_function_body = true;
23109 /* If the next token is `return', then the code may be trying to
23110 make use of the "named return value" extension that G++ used to
23111 support. */
23112 token = cp_lexer_peek_token (parser->lexer);
23113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23115 /* Consume the `return' keyword. */
23116 cp_lexer_consume_token (parser->lexer);
23117 /* Look for the identifier that indicates what value is to be
23118 returned. */
23119 cp_parser_identifier (parser);
23120 /* Issue an error message. */
23121 error_at (token->location,
23122 "named return values are no longer supported");
23123 /* Skip tokens until we reach the start of the function body. */
23124 while (true)
23126 cp_token *token = cp_lexer_peek_token (parser->lexer);
23127 if (token->type == CPP_OPEN_BRACE
23128 || token->type == CPP_EOF
23129 || token->type == CPP_PRAGMA_EOL)
23130 break;
23131 cp_lexer_consume_token (parser->lexer);
23134 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23135 anything declared inside `f'. */
23136 saved_in_unbraced_linkage_specification_p
23137 = parser->in_unbraced_linkage_specification_p;
23138 parser->in_unbraced_linkage_specification_p = false;
23139 /* Inside the function, surrounding template-parameter-lists do not
23140 apply. */
23141 saved_num_template_parameter_lists
23142 = parser->num_template_parameter_lists;
23143 parser->num_template_parameter_lists = 0;
23145 start_lambda_scope (current_function_decl);
23147 /* If the next token is `try', `__transaction_atomic', or
23148 `__transaction_relaxed`, then we are looking at either function-try-block
23149 or function-transaction-block. Note that all of these include the
23150 function-body. */
23151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23152 ctor_initializer_p = cp_parser_function_transaction (parser,
23153 RID_TRANSACTION_ATOMIC);
23154 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23155 RID_TRANSACTION_RELAXED))
23156 ctor_initializer_p = cp_parser_function_transaction (parser,
23157 RID_TRANSACTION_RELAXED);
23158 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23159 ctor_initializer_p = cp_parser_function_try_block (parser);
23160 else
23161 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23162 (parser, /*in_function_try_block=*/false);
23164 finish_lambda_scope ();
23166 /* Finish the function. */
23167 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23168 (inline_p ? 2 : 0));
23169 /* Generate code for it, if necessary. */
23170 expand_or_defer_fn (fn);
23171 /* Restore the saved values. */
23172 parser->in_unbraced_linkage_specification_p
23173 = saved_in_unbraced_linkage_specification_p;
23174 parser->num_template_parameter_lists
23175 = saved_num_template_parameter_lists;
23176 parser->in_function_body = saved_in_function_body;
23178 parser->fully_implicit_function_template_p
23179 = fully_implicit_function_template_p;
23180 parser->implicit_template_parms
23181 = implicit_template_parms;
23182 parser->implicit_template_scope
23183 = implicit_template_scope;
23185 if (parser->fully_implicit_function_template_p)
23186 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23188 return fn;
23191 /* Parse a template-declaration, assuming that the `export' (and
23192 `extern') keywords, if present, has already been scanned. MEMBER_P
23193 is as for cp_parser_template_declaration. */
23195 static void
23196 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23198 tree decl = NULL_TREE;
23199 vec<deferred_access_check, va_gc> *checks;
23200 tree parameter_list;
23201 bool friend_p = false;
23202 bool need_lang_pop;
23203 cp_token *token;
23205 /* Look for the `template' keyword. */
23206 token = cp_lexer_peek_token (parser->lexer);
23207 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23208 return;
23210 /* And the `<'. */
23211 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23212 return;
23213 if (at_class_scope_p () && current_function_decl)
23215 /* 14.5.2.2 [temp.mem]
23217 A local class shall not have member templates. */
23218 error_at (token->location,
23219 "invalid declaration of member template in local class");
23220 cp_parser_skip_to_end_of_block_or_statement (parser);
23221 return;
23223 /* [temp]
23225 A template ... shall not have C linkage. */
23226 if (current_lang_name == lang_name_c)
23228 error_at (token->location, "template with C linkage");
23229 /* Give it C++ linkage to avoid confusing other parts of the
23230 front end. */
23231 push_lang_context (lang_name_cplusplus);
23232 need_lang_pop = true;
23234 else
23235 need_lang_pop = false;
23237 /* We cannot perform access checks on the template parameter
23238 declarations until we know what is being declared, just as we
23239 cannot check the decl-specifier list. */
23240 push_deferring_access_checks (dk_deferred);
23242 /* If the next token is `>', then we have an invalid
23243 specialization. Rather than complain about an invalid template
23244 parameter, issue an error message here. */
23245 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23247 cp_parser_error (parser, "invalid explicit specialization");
23248 begin_specialization ();
23249 parameter_list = NULL_TREE;
23251 else
23253 /* Parse the template parameters. */
23254 parameter_list = cp_parser_template_parameter_list (parser);
23257 /* Get the deferred access checks from the parameter list. These
23258 will be checked once we know what is being declared, as for a
23259 member template the checks must be performed in the scope of the
23260 class containing the member. */
23261 checks = get_deferred_access_checks ();
23263 /* Look for the `>'. */
23264 cp_parser_skip_to_end_of_template_parameter_list (parser);
23265 /* We just processed one more parameter list. */
23266 ++parser->num_template_parameter_lists;
23267 /* If the next token is `template', there are more template
23268 parameters. */
23269 if (cp_lexer_next_token_is_keyword (parser->lexer,
23270 RID_TEMPLATE))
23271 cp_parser_template_declaration_after_export (parser, member_p);
23272 else if (cxx_dialect >= cxx11
23273 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23274 decl = cp_parser_alias_declaration (parser);
23275 else
23277 /* There are no access checks when parsing a template, as we do not
23278 know if a specialization will be a friend. */
23279 push_deferring_access_checks (dk_no_check);
23280 token = cp_lexer_peek_token (parser->lexer);
23281 decl = cp_parser_single_declaration (parser,
23282 checks,
23283 member_p,
23284 /*explicit_specialization_p=*/false,
23285 &friend_p);
23286 pop_deferring_access_checks ();
23288 /* If this is a member template declaration, let the front
23289 end know. */
23290 if (member_p && !friend_p && decl)
23292 if (TREE_CODE (decl) == TYPE_DECL)
23293 cp_parser_check_access_in_redeclaration (decl, token->location);
23295 decl = finish_member_template_decl (decl);
23297 else if (friend_p && decl
23298 && DECL_DECLARES_TYPE_P (decl))
23299 make_friend_class (current_class_type, TREE_TYPE (decl),
23300 /*complain=*/true);
23302 /* We are done with the current parameter list. */
23303 --parser->num_template_parameter_lists;
23305 pop_deferring_access_checks ();
23307 /* Finish up. */
23308 finish_template_decl (parameter_list);
23310 /* Check the template arguments for a literal operator template. */
23311 if (decl
23312 && DECL_DECLARES_FUNCTION_P (decl)
23313 && UDLIT_OPER_P (DECL_NAME (decl)))
23315 bool ok = true;
23316 if (parameter_list == NULL_TREE)
23317 ok = false;
23318 else
23320 int num_parms = TREE_VEC_LENGTH (parameter_list);
23321 if (num_parms == 1)
23323 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23324 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23325 if (TREE_TYPE (parm) != char_type_node
23326 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23327 ok = false;
23329 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23331 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23332 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23333 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23334 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23335 if (TREE_TYPE (parm) != TREE_TYPE (type)
23336 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23337 ok = false;
23339 else
23340 ok = false;
23342 if (!ok)
23344 if (cxx_dialect >= cxx1y)
23345 error ("literal operator template %qD has invalid parameter list."
23346 " Expected non-type template argument pack <char...>"
23347 " or <typename CharT, CharT...>",
23348 decl);
23349 else
23350 error ("literal operator template %qD has invalid parameter list."
23351 " Expected non-type template argument pack <char...>",
23352 decl);
23355 /* Register member declarations. */
23356 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23357 finish_member_declaration (decl);
23358 /* For the erroneous case of a template with C linkage, we pushed an
23359 implicit C++ linkage scope; exit that scope now. */
23360 if (need_lang_pop)
23361 pop_lang_context ();
23362 /* If DECL is a function template, we must return to parse it later.
23363 (Even though there is no definition, there might be default
23364 arguments that need handling.) */
23365 if (member_p && decl
23366 && DECL_DECLARES_FUNCTION_P (decl))
23367 vec_safe_push (unparsed_funs_with_definitions, decl);
23370 /* Perform the deferred access checks from a template-parameter-list.
23371 CHECKS is a TREE_LIST of access checks, as returned by
23372 get_deferred_access_checks. */
23374 static void
23375 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23377 ++processing_template_parmlist;
23378 perform_access_checks (checks, tf_warning_or_error);
23379 --processing_template_parmlist;
23382 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23383 `function-definition' sequence that follows a template header.
23384 If MEMBER_P is true, this declaration appears in a class scope.
23386 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23387 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23389 static tree
23390 cp_parser_single_declaration (cp_parser* parser,
23391 vec<deferred_access_check, va_gc> *checks,
23392 bool member_p,
23393 bool explicit_specialization_p,
23394 bool* friend_p)
23396 int declares_class_or_enum;
23397 tree decl = NULL_TREE;
23398 cp_decl_specifier_seq decl_specifiers;
23399 bool function_definition_p = false;
23400 cp_token *decl_spec_token_start;
23402 /* This function is only used when processing a template
23403 declaration. */
23404 gcc_assert (innermost_scope_kind () == sk_template_parms
23405 || innermost_scope_kind () == sk_template_spec);
23407 /* Defer access checks until we know what is being declared. */
23408 push_deferring_access_checks (dk_deferred);
23410 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23411 alternative. */
23412 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23413 cp_parser_decl_specifier_seq (parser,
23414 CP_PARSER_FLAGS_OPTIONAL,
23415 &decl_specifiers,
23416 &declares_class_or_enum);
23417 if (friend_p)
23418 *friend_p = cp_parser_friend_p (&decl_specifiers);
23420 /* There are no template typedefs. */
23421 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23423 error_at (decl_spec_token_start->location,
23424 "template declaration of %<typedef%>");
23425 decl = error_mark_node;
23428 /* Gather up the access checks that occurred the
23429 decl-specifier-seq. */
23430 stop_deferring_access_checks ();
23432 /* Check for the declaration of a template class. */
23433 if (declares_class_or_enum)
23435 if (cp_parser_declares_only_class_p (parser))
23437 decl = shadow_tag (&decl_specifiers);
23439 /* In this case:
23441 struct C {
23442 friend template <typename T> struct A<T>::B;
23445 A<T>::B will be represented by a TYPENAME_TYPE, and
23446 therefore not recognized by shadow_tag. */
23447 if (friend_p && *friend_p
23448 && !decl
23449 && decl_specifiers.type
23450 && TYPE_P (decl_specifiers.type))
23451 decl = decl_specifiers.type;
23453 if (decl && decl != error_mark_node)
23454 decl = TYPE_NAME (decl);
23455 else
23456 decl = error_mark_node;
23458 /* Perform access checks for template parameters. */
23459 cp_parser_perform_template_parameter_access_checks (checks);
23463 /* Complain about missing 'typename' or other invalid type names. */
23464 if (!decl_specifiers.any_type_specifiers_p
23465 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23467 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23468 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23469 the rest of this declaration. */
23470 decl = error_mark_node;
23471 goto out;
23474 /* If it's not a template class, try for a template function. If
23475 the next token is a `;', then this declaration does not declare
23476 anything. But, if there were errors in the decl-specifiers, then
23477 the error might well have come from an attempted class-specifier.
23478 In that case, there's no need to warn about a missing declarator. */
23479 if (!decl
23480 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23481 || decl_specifiers.type != error_mark_node))
23483 decl = cp_parser_init_declarator (parser,
23484 &decl_specifiers,
23485 checks,
23486 /*function_definition_allowed_p=*/true,
23487 member_p,
23488 declares_class_or_enum,
23489 &function_definition_p,
23490 NULL);
23492 /* 7.1.1-1 [dcl.stc]
23494 A storage-class-specifier shall not be specified in an explicit
23495 specialization... */
23496 if (decl
23497 && explicit_specialization_p
23498 && decl_specifiers.storage_class != sc_none)
23500 error_at (decl_spec_token_start->location,
23501 "explicit template specialization cannot have a storage class");
23502 decl = error_mark_node;
23505 if (decl && VAR_P (decl))
23506 check_template_variable (decl);
23509 /* Look for a trailing `;' after the declaration. */
23510 if (!function_definition_p
23511 && (decl == error_mark_node
23512 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23513 cp_parser_skip_to_end_of_block_or_statement (parser);
23515 out:
23516 pop_deferring_access_checks ();
23518 /* Clear any current qualification; whatever comes next is the start
23519 of something new. */
23520 parser->scope = NULL_TREE;
23521 parser->qualifying_scope = NULL_TREE;
23522 parser->object_scope = NULL_TREE;
23524 return decl;
23527 /* Parse a cast-expression that is not the operand of a unary "&". */
23529 static tree
23530 cp_parser_simple_cast_expression (cp_parser *parser)
23532 return cp_parser_cast_expression (parser, /*address_p=*/false,
23533 /*cast_p=*/false, /*decltype*/false, NULL);
23536 /* Parse a functional cast to TYPE. Returns an expression
23537 representing the cast. */
23539 static tree
23540 cp_parser_functional_cast (cp_parser* parser, tree type)
23542 vec<tree, va_gc> *vec;
23543 tree expression_list;
23544 tree cast;
23545 bool nonconst_p;
23547 if (!type)
23548 type = error_mark_node;
23550 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23552 cp_lexer_set_source_position (parser->lexer);
23553 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23554 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23555 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23556 if (TREE_CODE (type) == TYPE_DECL)
23557 type = TREE_TYPE (type);
23558 return finish_compound_literal (type, expression_list,
23559 tf_warning_or_error);
23563 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23564 /*cast_p=*/true,
23565 /*allow_expansion_p=*/true,
23566 /*non_constant_p=*/NULL);
23567 if (vec == NULL)
23568 expression_list = error_mark_node;
23569 else
23571 expression_list = build_tree_list_vec (vec);
23572 release_tree_vector (vec);
23575 cast = build_functional_cast (type, expression_list,
23576 tf_warning_or_error);
23577 /* [expr.const]/1: In an integral constant expression "only type
23578 conversions to integral or enumeration type can be used". */
23579 if (TREE_CODE (type) == TYPE_DECL)
23580 type = TREE_TYPE (type);
23581 if (cast != error_mark_node
23582 && !cast_valid_in_integral_constant_expression_p (type)
23583 && cp_parser_non_integral_constant_expression (parser,
23584 NIC_CONSTRUCTOR))
23585 return error_mark_node;
23586 return cast;
23589 /* Save the tokens that make up the body of a member function defined
23590 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23591 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23592 specifiers applied to the declaration. Returns the FUNCTION_DECL
23593 for the member function. */
23595 static tree
23596 cp_parser_save_member_function_body (cp_parser* parser,
23597 cp_decl_specifier_seq *decl_specifiers,
23598 cp_declarator *declarator,
23599 tree attributes)
23601 cp_token *first;
23602 cp_token *last;
23603 tree fn;
23605 /* Create the FUNCTION_DECL. */
23606 fn = grokmethod (decl_specifiers, declarator, attributes);
23607 cp_finalize_omp_declare_simd (parser, fn);
23608 /* If something went badly wrong, bail out now. */
23609 if (fn == error_mark_node)
23611 /* If there's a function-body, skip it. */
23612 if (cp_parser_token_starts_function_definition_p
23613 (cp_lexer_peek_token (parser->lexer)))
23614 cp_parser_skip_to_end_of_block_or_statement (parser);
23615 return error_mark_node;
23618 /* Remember it, if there default args to post process. */
23619 cp_parser_save_default_args (parser, fn);
23621 /* Save away the tokens that make up the body of the
23622 function. */
23623 first = parser->lexer->next_token;
23624 /* Handle function try blocks. */
23625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23626 cp_lexer_consume_token (parser->lexer);
23627 /* We can have braced-init-list mem-initializers before the fn body. */
23628 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23630 cp_lexer_consume_token (parser->lexer);
23631 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23633 /* cache_group will stop after an un-nested { } pair, too. */
23634 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23635 break;
23637 /* variadic mem-inits have ... after the ')'. */
23638 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23639 cp_lexer_consume_token (parser->lexer);
23642 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23643 /* Handle function try blocks. */
23644 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23645 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23646 last = parser->lexer->next_token;
23648 /* Save away the inline definition; we will process it when the
23649 class is complete. */
23650 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23651 DECL_PENDING_INLINE_P (fn) = 1;
23653 /* We need to know that this was defined in the class, so that
23654 friend templates are handled correctly. */
23655 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23657 /* Add FN to the queue of functions to be parsed later. */
23658 vec_safe_push (unparsed_funs_with_definitions, fn);
23660 return fn;
23663 /* Save the tokens that make up the in-class initializer for a non-static
23664 data member. Returns a DEFAULT_ARG. */
23666 static tree
23667 cp_parser_save_nsdmi (cp_parser* parser)
23669 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23672 /* Parse a template-argument-list, as well as the trailing ">" (but
23673 not the opening "<"). See cp_parser_template_argument_list for the
23674 return value. */
23676 static tree
23677 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23679 tree arguments;
23680 tree saved_scope;
23681 tree saved_qualifying_scope;
23682 tree saved_object_scope;
23683 bool saved_greater_than_is_operator_p;
23684 int saved_unevaluated_operand;
23685 int saved_inhibit_evaluation_warnings;
23687 /* [temp.names]
23689 When parsing a template-id, the first non-nested `>' is taken as
23690 the end of the template-argument-list rather than a greater-than
23691 operator. */
23692 saved_greater_than_is_operator_p
23693 = parser->greater_than_is_operator_p;
23694 parser->greater_than_is_operator_p = false;
23695 /* Parsing the argument list may modify SCOPE, so we save it
23696 here. */
23697 saved_scope = parser->scope;
23698 saved_qualifying_scope = parser->qualifying_scope;
23699 saved_object_scope = parser->object_scope;
23700 /* We need to evaluate the template arguments, even though this
23701 template-id may be nested within a "sizeof". */
23702 saved_unevaluated_operand = cp_unevaluated_operand;
23703 cp_unevaluated_operand = 0;
23704 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23705 c_inhibit_evaluation_warnings = 0;
23706 /* Parse the template-argument-list itself. */
23707 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23708 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23709 arguments = NULL_TREE;
23710 else
23711 arguments = cp_parser_template_argument_list (parser);
23712 /* Look for the `>' that ends the template-argument-list. If we find
23713 a '>>' instead, it's probably just a typo. */
23714 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23716 if (cxx_dialect != cxx98)
23718 /* In C++0x, a `>>' in a template argument list or cast
23719 expression is considered to be two separate `>'
23720 tokens. So, change the current token to a `>', but don't
23721 consume it: it will be consumed later when the outer
23722 template argument list (or cast expression) is parsed.
23723 Note that this replacement of `>' for `>>' is necessary
23724 even if we are parsing tentatively: in the tentative
23725 case, after calling
23726 cp_parser_enclosed_template_argument_list we will always
23727 throw away all of the template arguments and the first
23728 closing `>', either because the template argument list
23729 was erroneous or because we are replacing those tokens
23730 with a CPP_TEMPLATE_ID token. The second `>' (which will
23731 not have been thrown away) is needed either to close an
23732 outer template argument list or to complete a new-style
23733 cast. */
23734 cp_token *token = cp_lexer_peek_token (parser->lexer);
23735 token->type = CPP_GREATER;
23737 else if (!saved_greater_than_is_operator_p)
23739 /* If we're in a nested template argument list, the '>>' has
23740 to be a typo for '> >'. We emit the error message, but we
23741 continue parsing and we push a '>' as next token, so that
23742 the argument list will be parsed correctly. Note that the
23743 global source location is still on the token before the
23744 '>>', so we need to say explicitly where we want it. */
23745 cp_token *token = cp_lexer_peek_token (parser->lexer);
23746 error_at (token->location, "%<>>%> should be %<> >%> "
23747 "within a nested template argument list");
23749 token->type = CPP_GREATER;
23751 else
23753 /* If this is not a nested template argument list, the '>>'
23754 is a typo for '>'. Emit an error message and continue.
23755 Same deal about the token location, but here we can get it
23756 right by consuming the '>>' before issuing the diagnostic. */
23757 cp_token *token = cp_lexer_consume_token (parser->lexer);
23758 error_at (token->location,
23759 "spurious %<>>%>, use %<>%> to terminate "
23760 "a template argument list");
23763 else
23764 cp_parser_skip_to_end_of_template_parameter_list (parser);
23765 /* The `>' token might be a greater-than operator again now. */
23766 parser->greater_than_is_operator_p
23767 = saved_greater_than_is_operator_p;
23768 /* Restore the SAVED_SCOPE. */
23769 parser->scope = saved_scope;
23770 parser->qualifying_scope = saved_qualifying_scope;
23771 parser->object_scope = saved_object_scope;
23772 cp_unevaluated_operand = saved_unevaluated_operand;
23773 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23775 return arguments;
23778 /* MEMBER_FUNCTION is a member function, or a friend. If default
23779 arguments, or the body of the function have not yet been parsed,
23780 parse them now. */
23782 static void
23783 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23785 timevar_push (TV_PARSE_INMETH);
23786 /* If this member is a template, get the underlying
23787 FUNCTION_DECL. */
23788 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23789 member_function = DECL_TEMPLATE_RESULT (member_function);
23791 /* There should not be any class definitions in progress at this
23792 point; the bodies of members are only parsed outside of all class
23793 definitions. */
23794 gcc_assert (parser->num_classes_being_defined == 0);
23795 /* While we're parsing the member functions we might encounter more
23796 classes. We want to handle them right away, but we don't want
23797 them getting mixed up with functions that are currently in the
23798 queue. */
23799 push_unparsed_function_queues (parser);
23801 /* Make sure that any template parameters are in scope. */
23802 maybe_begin_member_template_processing (member_function);
23804 /* If the body of the function has not yet been parsed, parse it
23805 now. */
23806 if (DECL_PENDING_INLINE_P (member_function))
23808 tree function_scope;
23809 cp_token_cache *tokens;
23811 /* The function is no longer pending; we are processing it. */
23812 tokens = DECL_PENDING_INLINE_INFO (member_function);
23813 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23814 DECL_PENDING_INLINE_P (member_function) = 0;
23816 /* If this is a local class, enter the scope of the containing
23817 function. */
23818 function_scope = current_function_decl;
23819 if (function_scope)
23820 push_function_context ();
23822 /* Push the body of the function onto the lexer stack. */
23823 cp_parser_push_lexer_for_tokens (parser, tokens);
23825 /* Let the front end know that we going to be defining this
23826 function. */
23827 start_preparsed_function (member_function, NULL_TREE,
23828 SF_PRE_PARSED | SF_INCLASS_INLINE);
23830 /* Don't do access checking if it is a templated function. */
23831 if (processing_template_decl)
23832 push_deferring_access_checks (dk_no_check);
23834 /* #pragma omp declare reduction needs special parsing. */
23835 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23837 parser->lexer->in_pragma = true;
23838 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23839 finish_function (/*inline*/2);
23840 cp_check_omp_declare_reduction (member_function);
23842 else
23843 /* Now, parse the body of the function. */
23844 cp_parser_function_definition_after_declarator (parser,
23845 /*inline_p=*/true);
23847 if (processing_template_decl)
23848 pop_deferring_access_checks ();
23850 /* Leave the scope of the containing function. */
23851 if (function_scope)
23852 pop_function_context ();
23853 cp_parser_pop_lexer (parser);
23856 /* Remove any template parameters from the symbol table. */
23857 maybe_end_member_template_processing ();
23859 /* Restore the queue. */
23860 pop_unparsed_function_queues (parser);
23861 timevar_pop (TV_PARSE_INMETH);
23864 /* If DECL contains any default args, remember it on the unparsed
23865 functions queue. */
23867 static void
23868 cp_parser_save_default_args (cp_parser* parser, tree decl)
23870 tree probe;
23872 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23873 probe;
23874 probe = TREE_CHAIN (probe))
23875 if (TREE_PURPOSE (probe))
23877 cp_default_arg_entry entry = {current_class_type, decl};
23878 vec_safe_push (unparsed_funs_with_default_args, entry);
23879 break;
23883 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23884 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23885 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23886 from the parameter-type-list. */
23888 static tree
23889 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23890 tree default_arg, tree parmtype)
23892 cp_token_cache *tokens;
23893 tree parsed_arg;
23894 bool dummy;
23896 if (default_arg == error_mark_node)
23897 return error_mark_node;
23899 /* Push the saved tokens for the default argument onto the parser's
23900 lexer stack. */
23901 tokens = DEFARG_TOKENS (default_arg);
23902 cp_parser_push_lexer_for_tokens (parser, tokens);
23904 start_lambda_scope (decl);
23906 /* Parse the default argument. */
23907 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23908 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23909 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23911 finish_lambda_scope ();
23913 if (parsed_arg == error_mark_node)
23914 cp_parser_skip_to_end_of_statement (parser);
23916 if (!processing_template_decl)
23918 /* In a non-template class, check conversions now. In a template,
23919 we'll wait and instantiate these as needed. */
23920 if (TREE_CODE (decl) == PARM_DECL)
23921 parsed_arg = check_default_argument (parmtype, parsed_arg,
23922 tf_warning_or_error);
23923 else
23924 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
23927 /* If the token stream has not been completely used up, then
23928 there was extra junk after the end of the default
23929 argument. */
23930 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23932 if (TREE_CODE (decl) == PARM_DECL)
23933 cp_parser_error (parser, "expected %<,%>");
23934 else
23935 cp_parser_error (parser, "expected %<;%>");
23938 /* Revert to the main lexer. */
23939 cp_parser_pop_lexer (parser);
23941 return parsed_arg;
23944 /* FIELD is a non-static data member with an initializer which we saved for
23945 later; parse it now. */
23947 static void
23948 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23950 tree def;
23952 maybe_begin_member_template_processing (field);
23954 push_unparsed_function_queues (parser);
23955 def = cp_parser_late_parse_one_default_arg (parser, field,
23956 DECL_INITIAL (field),
23957 NULL_TREE);
23958 pop_unparsed_function_queues (parser);
23960 maybe_end_member_template_processing ();
23962 DECL_INITIAL (field) = def;
23965 /* FN is a FUNCTION_DECL which may contains a parameter with an
23966 unparsed DEFAULT_ARG. Parse the default args now. This function
23967 assumes that the current scope is the scope in which the default
23968 argument should be processed. */
23970 static void
23971 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23973 bool saved_local_variables_forbidden_p;
23974 tree parm, parmdecl;
23976 /* While we're parsing the default args, we might (due to the
23977 statement expression extension) encounter more classes. We want
23978 to handle them right away, but we don't want them getting mixed
23979 up with default args that are currently in the queue. */
23980 push_unparsed_function_queues (parser);
23982 /* Local variable names (and the `this' keyword) may not appear
23983 in a default argument. */
23984 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23985 parser->local_variables_forbidden_p = true;
23987 push_defarg_context (fn);
23989 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23990 parmdecl = DECL_ARGUMENTS (fn);
23991 parm && parm != void_list_node;
23992 parm = TREE_CHAIN (parm),
23993 parmdecl = DECL_CHAIN (parmdecl))
23995 tree default_arg = TREE_PURPOSE (parm);
23996 tree parsed_arg;
23997 vec<tree, va_gc> *insts;
23998 tree copy;
23999 unsigned ix;
24001 if (!default_arg)
24002 continue;
24004 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24005 /* This can happen for a friend declaration for a function
24006 already declared with default arguments. */
24007 continue;
24009 parsed_arg
24010 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24011 default_arg,
24012 TREE_VALUE (parm));
24013 if (parsed_arg == error_mark_node)
24015 continue;
24018 TREE_PURPOSE (parm) = parsed_arg;
24020 /* Update any instantiations we've already created. */
24021 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24022 vec_safe_iterate (insts, ix, &copy); ix++)
24023 TREE_PURPOSE (copy) = parsed_arg;
24026 pop_defarg_context ();
24028 /* Make sure no default arg is missing. */
24029 check_default_args (fn);
24031 /* Restore the state of local_variables_forbidden_p. */
24032 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24034 /* Restore the queue. */
24035 pop_unparsed_function_queues (parser);
24038 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24040 sizeof ... ( identifier )
24042 where the 'sizeof' token has already been consumed. */
24044 static tree
24045 cp_parser_sizeof_pack (cp_parser *parser)
24047 /* Consume the `...'. */
24048 cp_lexer_consume_token (parser->lexer);
24049 maybe_warn_variadic_templates ();
24051 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24052 if (paren)
24053 cp_lexer_consume_token (parser->lexer);
24054 else
24055 permerror (cp_lexer_peek_token (parser->lexer)->location,
24056 "%<sizeof...%> argument must be surrounded by parentheses");
24058 cp_token *token = cp_lexer_peek_token (parser->lexer);
24059 tree name = cp_parser_identifier (parser);
24060 if (name == error_mark_node)
24061 return error_mark_node;
24062 /* The name is not qualified. */
24063 parser->scope = NULL_TREE;
24064 parser->qualifying_scope = NULL_TREE;
24065 parser->object_scope = NULL_TREE;
24066 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24067 if (expr == error_mark_node)
24068 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24069 token->location);
24070 if (TREE_CODE (expr) == TYPE_DECL)
24071 expr = TREE_TYPE (expr);
24072 else if (TREE_CODE (expr) == CONST_DECL)
24073 expr = DECL_INITIAL (expr);
24074 expr = make_pack_expansion (expr);
24076 if (paren)
24077 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24079 return expr;
24082 /* Parse the operand of `sizeof' (or a similar operator). Returns
24083 either a TYPE or an expression, depending on the form of the
24084 input. The KEYWORD indicates which kind of expression we have
24085 encountered. */
24087 static tree
24088 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24090 tree expr = NULL_TREE;
24091 const char *saved_message;
24092 char *tmp;
24093 bool saved_integral_constant_expression_p;
24094 bool saved_non_integral_constant_expression_p;
24096 /* If it's a `...', then we are computing the length of a parameter
24097 pack. */
24098 if (keyword == RID_SIZEOF
24099 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24100 return cp_parser_sizeof_pack (parser);
24102 /* Types cannot be defined in a `sizeof' expression. Save away the
24103 old message. */
24104 saved_message = parser->type_definition_forbidden_message;
24105 /* And create the new one. */
24106 tmp = concat ("types may not be defined in %<",
24107 IDENTIFIER_POINTER (ridpointers[keyword]),
24108 "%> expressions", NULL);
24109 parser->type_definition_forbidden_message = tmp;
24111 /* The restrictions on constant-expressions do not apply inside
24112 sizeof expressions. */
24113 saved_integral_constant_expression_p
24114 = parser->integral_constant_expression_p;
24115 saved_non_integral_constant_expression_p
24116 = parser->non_integral_constant_expression_p;
24117 parser->integral_constant_expression_p = false;
24119 /* Do not actually evaluate the expression. */
24120 ++cp_unevaluated_operand;
24121 ++c_inhibit_evaluation_warnings;
24122 /* If it's a `(', then we might be looking at the type-id
24123 construction. */
24124 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24126 tree type = NULL_TREE;
24128 /* We can't be sure yet whether we're looking at a type-id or an
24129 expression. */
24130 cp_parser_parse_tentatively (parser);
24131 /* Note: as a GNU Extension, compound literals are considered
24132 postfix-expressions as they are in C99, so they are valid
24133 arguments to sizeof. See comment in cp_parser_cast_expression
24134 for details. */
24135 if (cp_parser_compound_literal_p (parser))
24136 cp_parser_simulate_error (parser);
24137 else
24139 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24140 parser->in_type_id_in_expr_p = true;
24141 /* Look for the type-id. */
24142 type = cp_parser_type_id (parser);
24143 /* Look for the closing `)'. */
24144 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24145 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24148 /* If all went well, then we're done. */
24149 if (cp_parser_parse_definitely (parser))
24151 cp_decl_specifier_seq decl_specs;
24153 /* Build a trivial decl-specifier-seq. */
24154 clear_decl_specs (&decl_specs);
24155 decl_specs.type = type;
24157 /* Call grokdeclarator to figure out what type this is. */
24158 expr = grokdeclarator (NULL,
24159 &decl_specs,
24160 TYPENAME,
24161 /*initialized=*/0,
24162 /*attrlist=*/NULL);
24166 /* If the type-id production did not work out, then we must be
24167 looking at the unary-expression production. */
24168 if (!expr)
24169 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24170 /*cast_p=*/false, NULL);
24172 /* Go back to evaluating expressions. */
24173 --cp_unevaluated_operand;
24174 --c_inhibit_evaluation_warnings;
24176 /* Free the message we created. */
24177 free (tmp);
24178 /* And restore the old one. */
24179 parser->type_definition_forbidden_message = saved_message;
24180 parser->integral_constant_expression_p
24181 = saved_integral_constant_expression_p;
24182 parser->non_integral_constant_expression_p
24183 = saved_non_integral_constant_expression_p;
24185 return expr;
24188 /* If the current declaration has no declarator, return true. */
24190 static bool
24191 cp_parser_declares_only_class_p (cp_parser *parser)
24193 /* If the next token is a `;' or a `,' then there is no
24194 declarator. */
24195 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24196 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24199 /* Update the DECL_SPECS to reflect the storage class indicated by
24200 KEYWORD. */
24202 static void
24203 cp_parser_set_storage_class (cp_parser *parser,
24204 cp_decl_specifier_seq *decl_specs,
24205 enum rid keyword,
24206 cp_token *token)
24208 cp_storage_class storage_class;
24210 if (parser->in_unbraced_linkage_specification_p)
24212 error_at (token->location, "invalid use of %qD in linkage specification",
24213 ridpointers[keyword]);
24214 return;
24216 else if (decl_specs->storage_class != sc_none)
24218 decl_specs->conflicting_specifiers_p = true;
24219 return;
24222 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24223 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24224 && decl_specs->gnu_thread_keyword_p)
24226 pedwarn (decl_specs->locations[ds_thread], 0,
24227 "%<__thread%> before %qD", ridpointers[keyword]);
24230 switch (keyword)
24232 case RID_AUTO:
24233 storage_class = sc_auto;
24234 break;
24235 case RID_REGISTER:
24236 storage_class = sc_register;
24237 break;
24238 case RID_STATIC:
24239 storage_class = sc_static;
24240 break;
24241 case RID_EXTERN:
24242 storage_class = sc_extern;
24243 break;
24244 case RID_MUTABLE:
24245 storage_class = sc_mutable;
24246 break;
24247 default:
24248 gcc_unreachable ();
24250 decl_specs->storage_class = storage_class;
24251 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24253 /* A storage class specifier cannot be applied alongside a typedef
24254 specifier. If there is a typedef specifier present then set
24255 conflicting_specifiers_p which will trigger an error later
24256 on in grokdeclarator. */
24257 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24258 decl_specs->conflicting_specifiers_p = true;
24261 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24262 is true, the type is a class or enum definition. */
24264 static void
24265 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24266 tree type_spec,
24267 cp_token *token,
24268 bool type_definition_p)
24270 decl_specs->any_specifiers_p = true;
24272 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24273 (with, for example, in "typedef int wchar_t;") we remember that
24274 this is what happened. In system headers, we ignore these
24275 declarations so that G++ can work with system headers that are not
24276 C++-safe. */
24277 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24278 && !type_definition_p
24279 && (type_spec == boolean_type_node
24280 || type_spec == char16_type_node
24281 || type_spec == char32_type_node
24282 || type_spec == wchar_type_node)
24283 && (decl_specs->type
24284 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24285 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24286 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24287 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24289 decl_specs->redefined_builtin_type = type_spec;
24290 set_and_check_decl_spec_loc (decl_specs,
24291 ds_redefined_builtin_type_spec,
24292 token);
24293 if (!decl_specs->type)
24295 decl_specs->type = type_spec;
24296 decl_specs->type_definition_p = false;
24297 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24300 else if (decl_specs->type)
24301 decl_specs->multiple_types_p = true;
24302 else
24304 decl_specs->type = type_spec;
24305 decl_specs->type_definition_p = type_definition_p;
24306 decl_specs->redefined_builtin_type = NULL_TREE;
24307 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24311 /* True iff TOKEN is the GNU keyword __thread. */
24313 static bool
24314 token_is__thread (cp_token *token)
24316 gcc_assert (token->keyword == RID_THREAD);
24317 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24320 /* Set the location for a declarator specifier and check if it is
24321 duplicated.
24323 DECL_SPECS is the sequence of declarator specifiers onto which to
24324 set the location.
24326 DS is the single declarator specifier to set which location is to
24327 be set onto the existing sequence of declarators.
24329 LOCATION is the location for the declarator specifier to
24330 consider. */
24332 static void
24333 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24334 cp_decl_spec ds, cp_token *token)
24336 gcc_assert (ds < ds_last);
24338 if (decl_specs == NULL)
24339 return;
24341 source_location location = token->location;
24343 if (decl_specs->locations[ds] == 0)
24345 decl_specs->locations[ds] = location;
24346 if (ds == ds_thread)
24347 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24349 else
24351 if (ds == ds_long)
24353 if (decl_specs->locations[ds_long_long] != 0)
24354 error_at (location,
24355 "%<long long long%> is too long for GCC");
24356 else
24358 decl_specs->locations[ds_long_long] = location;
24359 pedwarn_cxx98 (location,
24360 OPT_Wlong_long,
24361 "ISO C++ 1998 does not support %<long long%>");
24364 else if (ds == ds_thread)
24366 bool gnu = token_is__thread (token);
24367 if (gnu != decl_specs->gnu_thread_keyword_p)
24368 error_at (location,
24369 "both %<__thread%> and %<thread_local%> specified");
24370 else
24371 error_at (location, "duplicate %qD", token->u.value);
24373 else
24375 static const char *const decl_spec_names[] = {
24376 "signed",
24377 "unsigned",
24378 "short",
24379 "long",
24380 "const",
24381 "volatile",
24382 "restrict",
24383 "inline",
24384 "virtual",
24385 "explicit",
24386 "friend",
24387 "typedef",
24388 "using",
24389 "constexpr",
24390 "__complex"
24392 error_at (location,
24393 "duplicate %qs", decl_spec_names[ds]);
24398 /* Return true iff the declarator specifier DS is present in the
24399 sequence of declarator specifiers DECL_SPECS. */
24401 bool
24402 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24403 cp_decl_spec ds)
24405 gcc_assert (ds < ds_last);
24407 if (decl_specs == NULL)
24408 return false;
24410 return decl_specs->locations[ds] != 0;
24413 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24414 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24416 static bool
24417 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24419 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24422 /* Issue an error message indicating that TOKEN_DESC was expected.
24423 If KEYWORD is true, it indicated this function is called by
24424 cp_parser_require_keword and the required token can only be
24425 a indicated keyword. */
24427 static void
24428 cp_parser_required_error (cp_parser *parser,
24429 required_token token_desc,
24430 bool keyword)
24432 switch (token_desc)
24434 case RT_NEW:
24435 cp_parser_error (parser, "expected %<new%>");
24436 return;
24437 case RT_DELETE:
24438 cp_parser_error (parser, "expected %<delete%>");
24439 return;
24440 case RT_RETURN:
24441 cp_parser_error (parser, "expected %<return%>");
24442 return;
24443 case RT_WHILE:
24444 cp_parser_error (parser, "expected %<while%>");
24445 return;
24446 case RT_EXTERN:
24447 cp_parser_error (parser, "expected %<extern%>");
24448 return;
24449 case RT_STATIC_ASSERT:
24450 cp_parser_error (parser, "expected %<static_assert%>");
24451 return;
24452 case RT_DECLTYPE:
24453 cp_parser_error (parser, "expected %<decltype%>");
24454 return;
24455 case RT_OPERATOR:
24456 cp_parser_error (parser, "expected %<operator%>");
24457 return;
24458 case RT_CLASS:
24459 cp_parser_error (parser, "expected %<class%>");
24460 return;
24461 case RT_TEMPLATE:
24462 cp_parser_error (parser, "expected %<template%>");
24463 return;
24464 case RT_NAMESPACE:
24465 cp_parser_error (parser, "expected %<namespace%>");
24466 return;
24467 case RT_USING:
24468 cp_parser_error (parser, "expected %<using%>");
24469 return;
24470 case RT_ASM:
24471 cp_parser_error (parser, "expected %<asm%>");
24472 return;
24473 case RT_TRY:
24474 cp_parser_error (parser, "expected %<try%>");
24475 return;
24476 case RT_CATCH:
24477 cp_parser_error (parser, "expected %<catch%>");
24478 return;
24479 case RT_THROW:
24480 cp_parser_error (parser, "expected %<throw%>");
24481 return;
24482 case RT_LABEL:
24483 cp_parser_error (parser, "expected %<__label__%>");
24484 return;
24485 case RT_AT_TRY:
24486 cp_parser_error (parser, "expected %<@try%>");
24487 return;
24488 case RT_AT_SYNCHRONIZED:
24489 cp_parser_error (parser, "expected %<@synchronized%>");
24490 return;
24491 case RT_AT_THROW:
24492 cp_parser_error (parser, "expected %<@throw%>");
24493 return;
24494 case RT_TRANSACTION_ATOMIC:
24495 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24496 return;
24497 case RT_TRANSACTION_RELAXED:
24498 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24499 return;
24500 default:
24501 break;
24503 if (!keyword)
24505 switch (token_desc)
24507 case RT_SEMICOLON:
24508 cp_parser_error (parser, "expected %<;%>");
24509 return;
24510 case RT_OPEN_PAREN:
24511 cp_parser_error (parser, "expected %<(%>");
24512 return;
24513 case RT_CLOSE_BRACE:
24514 cp_parser_error (parser, "expected %<}%>");
24515 return;
24516 case RT_OPEN_BRACE:
24517 cp_parser_error (parser, "expected %<{%>");
24518 return;
24519 case RT_CLOSE_SQUARE:
24520 cp_parser_error (parser, "expected %<]%>");
24521 return;
24522 case RT_OPEN_SQUARE:
24523 cp_parser_error (parser, "expected %<[%>");
24524 return;
24525 case RT_COMMA:
24526 cp_parser_error (parser, "expected %<,%>");
24527 return;
24528 case RT_SCOPE:
24529 cp_parser_error (parser, "expected %<::%>");
24530 return;
24531 case RT_LESS:
24532 cp_parser_error (parser, "expected %<<%>");
24533 return;
24534 case RT_GREATER:
24535 cp_parser_error (parser, "expected %<>%>");
24536 return;
24537 case RT_EQ:
24538 cp_parser_error (parser, "expected %<=%>");
24539 return;
24540 case RT_ELLIPSIS:
24541 cp_parser_error (parser, "expected %<...%>");
24542 return;
24543 case RT_MULT:
24544 cp_parser_error (parser, "expected %<*%>");
24545 return;
24546 case RT_COMPL:
24547 cp_parser_error (parser, "expected %<~%>");
24548 return;
24549 case RT_COLON:
24550 cp_parser_error (parser, "expected %<:%>");
24551 return;
24552 case RT_COLON_SCOPE:
24553 cp_parser_error (parser, "expected %<:%> or %<::%>");
24554 return;
24555 case RT_CLOSE_PAREN:
24556 cp_parser_error (parser, "expected %<)%>");
24557 return;
24558 case RT_COMMA_CLOSE_PAREN:
24559 cp_parser_error (parser, "expected %<,%> or %<)%>");
24560 return;
24561 case RT_PRAGMA_EOL:
24562 cp_parser_error (parser, "expected end of line");
24563 return;
24564 case RT_NAME:
24565 cp_parser_error (parser, "expected identifier");
24566 return;
24567 case RT_SELECT:
24568 cp_parser_error (parser, "expected selection-statement");
24569 return;
24570 case RT_INTERATION:
24571 cp_parser_error (parser, "expected iteration-statement");
24572 return;
24573 case RT_JUMP:
24574 cp_parser_error (parser, "expected jump-statement");
24575 return;
24576 case RT_CLASS_KEY:
24577 cp_parser_error (parser, "expected class-key");
24578 return;
24579 case RT_CLASS_TYPENAME_TEMPLATE:
24580 cp_parser_error (parser,
24581 "expected %<class%>, %<typename%>, or %<template%>");
24582 return;
24583 default:
24584 gcc_unreachable ();
24587 else
24588 gcc_unreachable ();
24593 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24594 issue an error message indicating that TOKEN_DESC was expected.
24596 Returns the token consumed, if the token had the appropriate type.
24597 Otherwise, returns NULL. */
24599 static cp_token *
24600 cp_parser_require (cp_parser* parser,
24601 enum cpp_ttype type,
24602 required_token token_desc)
24604 if (cp_lexer_next_token_is (parser->lexer, type))
24605 return cp_lexer_consume_token (parser->lexer);
24606 else
24608 /* Output the MESSAGE -- unless we're parsing tentatively. */
24609 if (!cp_parser_simulate_error (parser))
24610 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24611 return NULL;
24615 /* An error message is produced if the next token is not '>'.
24616 All further tokens are skipped until the desired token is
24617 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24619 static void
24620 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24622 /* Current level of '< ... >'. */
24623 unsigned level = 0;
24624 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24625 unsigned nesting_depth = 0;
24627 /* Are we ready, yet? If not, issue error message. */
24628 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24629 return;
24631 /* Skip tokens until the desired token is found. */
24632 while (true)
24634 /* Peek at the next token. */
24635 switch (cp_lexer_peek_token (parser->lexer)->type)
24637 case CPP_LESS:
24638 if (!nesting_depth)
24639 ++level;
24640 break;
24642 case CPP_RSHIFT:
24643 if (cxx_dialect == cxx98)
24644 /* C++0x views the `>>' operator as two `>' tokens, but
24645 C++98 does not. */
24646 break;
24647 else if (!nesting_depth && level-- == 0)
24649 /* We've hit a `>>' where the first `>' closes the
24650 template argument list, and the second `>' is
24651 spurious. Just consume the `>>' and stop; we've
24652 already produced at least one error. */
24653 cp_lexer_consume_token (parser->lexer);
24654 return;
24656 /* Fall through for C++0x, so we handle the second `>' in
24657 the `>>'. */
24659 case CPP_GREATER:
24660 if (!nesting_depth && level-- == 0)
24662 /* We've reached the token we want, consume it and stop. */
24663 cp_lexer_consume_token (parser->lexer);
24664 return;
24666 break;
24668 case CPP_OPEN_PAREN:
24669 case CPP_OPEN_SQUARE:
24670 ++nesting_depth;
24671 break;
24673 case CPP_CLOSE_PAREN:
24674 case CPP_CLOSE_SQUARE:
24675 if (nesting_depth-- == 0)
24676 return;
24677 break;
24679 case CPP_EOF:
24680 case CPP_PRAGMA_EOL:
24681 case CPP_SEMICOLON:
24682 case CPP_OPEN_BRACE:
24683 case CPP_CLOSE_BRACE:
24684 /* The '>' was probably forgotten, don't look further. */
24685 return;
24687 default:
24688 break;
24691 /* Consume this token. */
24692 cp_lexer_consume_token (parser->lexer);
24696 /* If the next token is the indicated keyword, consume it. Otherwise,
24697 issue an error message indicating that TOKEN_DESC was expected.
24699 Returns the token consumed, if the token had the appropriate type.
24700 Otherwise, returns NULL. */
24702 static cp_token *
24703 cp_parser_require_keyword (cp_parser* parser,
24704 enum rid keyword,
24705 required_token token_desc)
24707 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24709 if (token && token->keyword != keyword)
24711 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24712 return NULL;
24715 return token;
24718 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24719 function-definition. */
24721 static bool
24722 cp_parser_token_starts_function_definition_p (cp_token* token)
24724 return (/* An ordinary function-body begins with an `{'. */
24725 token->type == CPP_OPEN_BRACE
24726 /* A ctor-initializer begins with a `:'. */
24727 || token->type == CPP_COLON
24728 /* A function-try-block begins with `try'. */
24729 || token->keyword == RID_TRY
24730 /* A function-transaction-block begins with `__transaction_atomic'
24731 or `__transaction_relaxed'. */
24732 || token->keyword == RID_TRANSACTION_ATOMIC
24733 || token->keyword == RID_TRANSACTION_RELAXED
24734 /* The named return value extension begins with `return'. */
24735 || token->keyword == RID_RETURN);
24738 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24739 definition. */
24741 static bool
24742 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24744 cp_token *token;
24746 token = cp_lexer_peek_token (parser->lexer);
24747 return (token->type == CPP_OPEN_BRACE
24748 || (token->type == CPP_COLON
24749 && !parser->colon_doesnt_start_class_def_p));
24752 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24753 C++0x) ending a template-argument. */
24755 static bool
24756 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24758 cp_token *token;
24760 token = cp_lexer_peek_token (parser->lexer);
24761 return (token->type == CPP_COMMA
24762 || token->type == CPP_GREATER
24763 || token->type == CPP_ELLIPSIS
24764 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24767 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24768 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24770 static bool
24771 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24772 size_t n)
24774 cp_token *token;
24776 token = cp_lexer_peek_nth_token (parser->lexer, n);
24777 if (token->type == CPP_LESS)
24778 return true;
24779 /* Check for the sequence `<::' in the original code. It would be lexed as
24780 `[:', where `[' is a digraph, and there is no whitespace before
24781 `:'. */
24782 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24784 cp_token *token2;
24785 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24786 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24787 return true;
24789 return false;
24792 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24793 or none_type otherwise. */
24795 static enum tag_types
24796 cp_parser_token_is_class_key (cp_token* token)
24798 switch (token->keyword)
24800 case RID_CLASS:
24801 return class_type;
24802 case RID_STRUCT:
24803 return record_type;
24804 case RID_UNION:
24805 return union_type;
24807 default:
24808 return none_type;
24812 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
24813 or none_type otherwise or if the token is null. */
24815 static enum tag_types
24816 cp_parser_token_is_type_parameter_key (cp_token* token)
24818 if (!token)
24819 return none_type;
24821 switch (token->keyword)
24823 case RID_CLASS:
24824 return class_type;
24825 case RID_TYPENAME:
24826 return typename_type;
24828 default:
24829 return none_type;
24833 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24835 static void
24836 cp_parser_check_class_key (enum tag_types class_key, tree type)
24838 if (type == error_mark_node)
24839 return;
24840 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24842 if (permerror (input_location, "%qs tag used in naming %q#T",
24843 class_key == union_type ? "union"
24844 : class_key == record_type ? "struct" : "class",
24845 type))
24846 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24847 "%q#T was previously declared here", type);
24851 /* Issue an error message if DECL is redeclared with different
24852 access than its original declaration [class.access.spec/3].
24853 This applies to nested classes and nested class templates.
24854 [class.mem/1]. */
24856 static void
24857 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24859 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24860 return;
24862 if ((TREE_PRIVATE (decl)
24863 != (current_access_specifier == access_private_node))
24864 || (TREE_PROTECTED (decl)
24865 != (current_access_specifier == access_protected_node)))
24866 error_at (location, "%qD redeclared with different access", decl);
24869 /* Look for the `template' keyword, as a syntactic disambiguator.
24870 Return TRUE iff it is present, in which case it will be
24871 consumed. */
24873 static bool
24874 cp_parser_optional_template_keyword (cp_parser *parser)
24876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24878 /* In C++98 the `template' keyword can only be used within templates;
24879 outside templates the parser can always figure out what is a
24880 template and what is not. In C++11, per the resolution of DR 468,
24881 `template' is allowed in cases where it is not strictly necessary. */
24882 if (!processing_template_decl
24883 && pedantic && cxx_dialect == cxx98)
24885 cp_token *token = cp_lexer_peek_token (parser->lexer);
24886 pedwarn (token->location, OPT_Wpedantic,
24887 "in C++98 %<template%> (as a disambiguator) is only "
24888 "allowed within templates");
24889 /* If this part of the token stream is rescanned, the same
24890 error message would be generated. So, we purge the token
24891 from the stream. */
24892 cp_lexer_purge_token (parser->lexer);
24893 return false;
24895 else
24897 /* Consume the `template' keyword. */
24898 cp_lexer_consume_token (parser->lexer);
24899 return true;
24902 return false;
24905 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24906 set PARSER->SCOPE, and perform other related actions. */
24908 static void
24909 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24911 int i;
24912 struct tree_check *check_value;
24913 deferred_access_check *chk;
24914 vec<deferred_access_check, va_gc> *checks;
24916 /* Get the stored value. */
24917 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24918 /* Perform any access checks that were deferred. */
24919 checks = check_value->checks;
24920 if (checks)
24922 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24923 perform_or_defer_access_check (chk->binfo,
24924 chk->decl,
24925 chk->diag_decl, tf_warning_or_error);
24927 /* Set the scope from the stored value. */
24928 parser->scope = check_value->value;
24929 parser->qualifying_scope = check_value->qualifying_scope;
24930 parser->object_scope = NULL_TREE;
24933 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24934 encounter the end of a block before what we were looking for. */
24936 static bool
24937 cp_parser_cache_group (cp_parser *parser,
24938 enum cpp_ttype end,
24939 unsigned depth)
24941 while (true)
24943 cp_token *token = cp_lexer_peek_token (parser->lexer);
24945 /* Abort a parenthesized expression if we encounter a semicolon. */
24946 if ((end == CPP_CLOSE_PAREN || depth == 0)
24947 && token->type == CPP_SEMICOLON)
24948 return true;
24949 /* If we've reached the end of the file, stop. */
24950 if (token->type == CPP_EOF
24951 || (end != CPP_PRAGMA_EOL
24952 && token->type == CPP_PRAGMA_EOL))
24953 return true;
24954 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24955 /* We've hit the end of an enclosing block, so there's been some
24956 kind of syntax error. */
24957 return true;
24959 /* Consume the token. */
24960 cp_lexer_consume_token (parser->lexer);
24961 /* See if it starts a new group. */
24962 if (token->type == CPP_OPEN_BRACE)
24964 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24965 /* In theory this should probably check end == '}', but
24966 cp_parser_save_member_function_body needs it to exit
24967 after either '}' or ')' when called with ')'. */
24968 if (depth == 0)
24969 return false;
24971 else if (token->type == CPP_OPEN_PAREN)
24973 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24974 if (depth == 0 && end == CPP_CLOSE_PAREN)
24975 return false;
24977 else if (token->type == CPP_PRAGMA)
24978 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24979 else if (token->type == end)
24980 return false;
24984 /* Like above, for caching a default argument or NSDMI. Both of these are
24985 terminated by a non-nested comma, but it can be unclear whether or not a
24986 comma is nested in a template argument list unless we do more parsing.
24987 In order to handle this ambiguity, when we encounter a ',' after a '<'
24988 we try to parse what follows as a parameter-declaration-list (in the
24989 case of a default argument) or a member-declarator (in the case of an
24990 NSDMI). If that succeeds, then we stop caching. */
24992 static tree
24993 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24995 unsigned depth = 0;
24996 int maybe_template_id = 0;
24997 cp_token *first_token;
24998 cp_token *token;
24999 tree default_argument;
25001 /* Add tokens until we have processed the entire default
25002 argument. We add the range [first_token, token). */
25003 first_token = cp_lexer_peek_token (parser->lexer);
25004 if (first_token->type == CPP_OPEN_BRACE)
25006 /* For list-initialization, this is straightforward. */
25007 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25008 token = cp_lexer_peek_token (parser->lexer);
25010 else while (true)
25012 bool done = false;
25014 /* Peek at the next token. */
25015 token = cp_lexer_peek_token (parser->lexer);
25016 /* What we do depends on what token we have. */
25017 switch (token->type)
25019 /* In valid code, a default argument must be
25020 immediately followed by a `,' `)', or `...'. */
25021 case CPP_COMMA:
25022 if (depth == 0 && maybe_template_id)
25024 /* If we've seen a '<', we might be in a
25025 template-argument-list. Until Core issue 325 is
25026 resolved, we don't know how this situation ought
25027 to be handled, so try to DTRT. We check whether
25028 what comes after the comma is a valid parameter
25029 declaration list. If it is, then the comma ends
25030 the default argument; otherwise the default
25031 argument continues. */
25032 bool error = false;
25034 /* Set ITALP so cp_parser_parameter_declaration_list
25035 doesn't decide to commit to this parse. */
25036 bool saved_italp = parser->in_template_argument_list_p;
25037 parser->in_template_argument_list_p = true;
25039 cp_parser_parse_tentatively (parser);
25040 cp_lexer_consume_token (parser->lexer);
25042 if (nsdmi)
25044 int ctor_dtor_or_conv_p;
25045 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25046 &ctor_dtor_or_conv_p,
25047 /*parenthesized_p=*/NULL,
25048 /*member_p=*/true,
25049 /*friend_p=*/false);
25051 else
25053 begin_scope (sk_function_parms, NULL_TREE);
25054 cp_parser_parameter_declaration_list (parser, &error);
25055 pop_bindings_and_leave_scope ();
25057 if (!cp_parser_error_occurred (parser) && !error)
25058 done = true;
25059 cp_parser_abort_tentative_parse (parser);
25061 parser->in_template_argument_list_p = saved_italp;
25062 break;
25064 case CPP_CLOSE_PAREN:
25065 case CPP_ELLIPSIS:
25066 /* If we run into a non-nested `;', `}', or `]',
25067 then the code is invalid -- but the default
25068 argument is certainly over. */
25069 case CPP_SEMICOLON:
25070 case CPP_CLOSE_BRACE:
25071 case CPP_CLOSE_SQUARE:
25072 if (depth == 0
25073 /* Handle correctly int n = sizeof ... ( p ); */
25074 && token->type != CPP_ELLIPSIS)
25075 done = true;
25076 /* Update DEPTH, if necessary. */
25077 else if (token->type == CPP_CLOSE_PAREN
25078 || token->type == CPP_CLOSE_BRACE
25079 || token->type == CPP_CLOSE_SQUARE)
25080 --depth;
25081 break;
25083 case CPP_OPEN_PAREN:
25084 case CPP_OPEN_SQUARE:
25085 case CPP_OPEN_BRACE:
25086 ++depth;
25087 break;
25089 case CPP_LESS:
25090 if (depth == 0)
25091 /* This might be the comparison operator, or it might
25092 start a template argument list. */
25093 ++maybe_template_id;
25094 break;
25096 case CPP_RSHIFT:
25097 if (cxx_dialect == cxx98)
25098 break;
25099 /* Fall through for C++0x, which treats the `>>'
25100 operator like two `>' tokens in certain
25101 cases. */
25103 case CPP_GREATER:
25104 if (depth == 0)
25106 /* This might be an operator, or it might close a
25107 template argument list. But if a previous '<'
25108 started a template argument list, this will have
25109 closed it, so we can't be in one anymore. */
25110 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25111 if (maybe_template_id < 0)
25112 maybe_template_id = 0;
25114 break;
25116 /* If we run out of tokens, issue an error message. */
25117 case CPP_EOF:
25118 case CPP_PRAGMA_EOL:
25119 error_at (token->location, "file ends in default argument");
25120 done = true;
25121 break;
25123 case CPP_NAME:
25124 case CPP_SCOPE:
25125 /* In these cases, we should look for template-ids.
25126 For example, if the default argument is
25127 `X<int, double>()', we need to do name lookup to
25128 figure out whether or not `X' is a template; if
25129 so, the `,' does not end the default argument.
25131 That is not yet done. */
25132 break;
25134 default:
25135 break;
25138 /* If we've reached the end, stop. */
25139 if (done)
25140 break;
25142 /* Add the token to the token block. */
25143 token = cp_lexer_consume_token (parser->lexer);
25146 /* Create a DEFAULT_ARG to represent the unparsed default
25147 argument. */
25148 default_argument = make_node (DEFAULT_ARG);
25149 DEFARG_TOKENS (default_argument)
25150 = cp_token_cache_new (first_token, token);
25151 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25153 return default_argument;
25156 /* Begin parsing tentatively. We always save tokens while parsing
25157 tentatively so that if the tentative parsing fails we can restore the
25158 tokens. */
25160 static void
25161 cp_parser_parse_tentatively (cp_parser* parser)
25163 /* Enter a new parsing context. */
25164 parser->context = cp_parser_context_new (parser->context);
25165 /* Begin saving tokens. */
25166 cp_lexer_save_tokens (parser->lexer);
25167 /* In order to avoid repetitive access control error messages,
25168 access checks are queued up until we are no longer parsing
25169 tentatively. */
25170 push_deferring_access_checks (dk_deferred);
25173 /* Commit to the currently active tentative parse. */
25175 static void
25176 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25178 cp_parser_context *context;
25179 cp_lexer *lexer;
25181 /* Mark all of the levels as committed. */
25182 lexer = parser->lexer;
25183 for (context = parser->context; context->next; context = context->next)
25185 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25186 break;
25187 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25188 while (!cp_lexer_saving_tokens (lexer))
25189 lexer = lexer->next;
25190 cp_lexer_commit_tokens (lexer);
25194 /* Commit to the topmost currently active tentative parse.
25196 Note that this function shouldn't be called when there are
25197 irreversible side-effects while in a tentative state. For
25198 example, we shouldn't create a permanent entry in the symbol
25199 table, or issue an error message that might not apply if the
25200 tentative parse is aborted. */
25202 static void
25203 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25205 cp_parser_context *context = parser->context;
25206 cp_lexer *lexer = parser->lexer;
25208 if (context)
25210 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25211 return;
25212 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25214 while (!cp_lexer_saving_tokens (lexer))
25215 lexer = lexer->next;
25216 cp_lexer_commit_tokens (lexer);
25220 /* Abort the currently active tentative parse. All consumed tokens
25221 will be rolled back, and no diagnostics will be issued. */
25223 static void
25224 cp_parser_abort_tentative_parse (cp_parser* parser)
25226 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25227 || errorcount > 0);
25228 cp_parser_simulate_error (parser);
25229 /* Now, pretend that we want to see if the construct was
25230 successfully parsed. */
25231 cp_parser_parse_definitely (parser);
25234 /* Stop parsing tentatively. If a parse error has occurred, restore the
25235 token stream. Otherwise, commit to the tokens we have consumed.
25236 Returns true if no error occurred; false otherwise. */
25238 static bool
25239 cp_parser_parse_definitely (cp_parser* parser)
25241 bool error_occurred;
25242 cp_parser_context *context;
25244 /* Remember whether or not an error occurred, since we are about to
25245 destroy that information. */
25246 error_occurred = cp_parser_error_occurred (parser);
25247 /* Remove the topmost context from the stack. */
25248 context = parser->context;
25249 parser->context = context->next;
25250 /* If no parse errors occurred, commit to the tentative parse. */
25251 if (!error_occurred)
25253 /* Commit to the tokens read tentatively, unless that was
25254 already done. */
25255 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25256 cp_lexer_commit_tokens (parser->lexer);
25258 pop_to_parent_deferring_access_checks ();
25260 /* Otherwise, if errors occurred, roll back our state so that things
25261 are just as they were before we began the tentative parse. */
25262 else
25264 cp_lexer_rollback_tokens (parser->lexer);
25265 pop_deferring_access_checks ();
25267 /* Add the context to the front of the free list. */
25268 context->next = cp_parser_context_free_list;
25269 cp_parser_context_free_list = context;
25271 return !error_occurred;
25274 /* Returns true if we are parsing tentatively and are not committed to
25275 this tentative parse. */
25277 static bool
25278 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25280 return (cp_parser_parsing_tentatively (parser)
25281 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25284 /* Returns nonzero iff an error has occurred during the most recent
25285 tentative parse. */
25287 static bool
25288 cp_parser_error_occurred (cp_parser* parser)
25290 return (cp_parser_parsing_tentatively (parser)
25291 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25294 /* Returns nonzero if GNU extensions are allowed. */
25296 static bool
25297 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25299 return parser->allow_gnu_extensions_p;
25302 /* Objective-C++ Productions */
25305 /* Parse an Objective-C expression, which feeds into a primary-expression
25306 above.
25308 objc-expression:
25309 objc-message-expression
25310 objc-string-literal
25311 objc-encode-expression
25312 objc-protocol-expression
25313 objc-selector-expression
25315 Returns a tree representation of the expression. */
25317 static tree
25318 cp_parser_objc_expression (cp_parser* parser)
25320 /* Try to figure out what kind of declaration is present. */
25321 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25323 switch (kwd->type)
25325 case CPP_OPEN_SQUARE:
25326 return cp_parser_objc_message_expression (parser);
25328 case CPP_OBJC_STRING:
25329 kwd = cp_lexer_consume_token (parser->lexer);
25330 return objc_build_string_object (kwd->u.value);
25332 case CPP_KEYWORD:
25333 switch (kwd->keyword)
25335 case RID_AT_ENCODE:
25336 return cp_parser_objc_encode_expression (parser);
25338 case RID_AT_PROTOCOL:
25339 return cp_parser_objc_protocol_expression (parser);
25341 case RID_AT_SELECTOR:
25342 return cp_parser_objc_selector_expression (parser);
25344 default:
25345 break;
25347 default:
25348 error_at (kwd->location,
25349 "misplaced %<@%D%> Objective-C++ construct",
25350 kwd->u.value);
25351 cp_parser_skip_to_end_of_block_or_statement (parser);
25354 return error_mark_node;
25357 /* Parse an Objective-C message expression.
25359 objc-message-expression:
25360 [ objc-message-receiver objc-message-args ]
25362 Returns a representation of an Objective-C message. */
25364 static tree
25365 cp_parser_objc_message_expression (cp_parser* parser)
25367 tree receiver, messageargs;
25369 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25370 receiver = cp_parser_objc_message_receiver (parser);
25371 messageargs = cp_parser_objc_message_args (parser);
25372 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25374 return objc_build_message_expr (receiver, messageargs);
25377 /* Parse an objc-message-receiver.
25379 objc-message-receiver:
25380 expression
25381 simple-type-specifier
25383 Returns a representation of the type or expression. */
25385 static tree
25386 cp_parser_objc_message_receiver (cp_parser* parser)
25388 tree rcv;
25390 /* An Objective-C message receiver may be either (1) a type
25391 or (2) an expression. */
25392 cp_parser_parse_tentatively (parser);
25393 rcv = cp_parser_expression (parser, false, NULL);
25395 if (cp_parser_parse_definitely (parser))
25396 return rcv;
25398 rcv = cp_parser_simple_type_specifier (parser,
25399 /*decl_specs=*/NULL,
25400 CP_PARSER_FLAGS_NONE);
25402 return objc_get_class_reference (rcv);
25405 /* Parse the arguments and selectors comprising an Objective-C message.
25407 objc-message-args:
25408 objc-selector
25409 objc-selector-args
25410 objc-selector-args , objc-comma-args
25412 objc-selector-args:
25413 objc-selector [opt] : assignment-expression
25414 objc-selector-args objc-selector [opt] : assignment-expression
25416 objc-comma-args:
25417 assignment-expression
25418 objc-comma-args , assignment-expression
25420 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25421 selector arguments and TREE_VALUE containing a list of comma
25422 arguments. */
25424 static tree
25425 cp_parser_objc_message_args (cp_parser* parser)
25427 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25428 bool maybe_unary_selector_p = true;
25429 cp_token *token = cp_lexer_peek_token (parser->lexer);
25431 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25433 tree selector = NULL_TREE, arg;
25435 if (token->type != CPP_COLON)
25436 selector = cp_parser_objc_selector (parser);
25438 /* Detect if we have a unary selector. */
25439 if (maybe_unary_selector_p
25440 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25441 return build_tree_list (selector, NULL_TREE);
25443 maybe_unary_selector_p = false;
25444 cp_parser_require (parser, CPP_COLON, RT_COLON);
25445 arg = cp_parser_assignment_expression (parser, false, NULL);
25447 sel_args
25448 = chainon (sel_args,
25449 build_tree_list (selector, arg));
25451 token = cp_lexer_peek_token (parser->lexer);
25454 /* Handle non-selector arguments, if any. */
25455 while (token->type == CPP_COMMA)
25457 tree arg;
25459 cp_lexer_consume_token (parser->lexer);
25460 arg = cp_parser_assignment_expression (parser, false, NULL);
25462 addl_args
25463 = chainon (addl_args,
25464 build_tree_list (NULL_TREE, arg));
25466 token = cp_lexer_peek_token (parser->lexer);
25469 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25471 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25472 return build_tree_list (error_mark_node, error_mark_node);
25475 return build_tree_list (sel_args, addl_args);
25478 /* Parse an Objective-C encode expression.
25480 objc-encode-expression:
25481 @encode objc-typename
25483 Returns an encoded representation of the type argument. */
25485 static tree
25486 cp_parser_objc_encode_expression (cp_parser* parser)
25488 tree type;
25489 cp_token *token;
25491 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25492 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25493 token = cp_lexer_peek_token (parser->lexer);
25494 type = complete_type (cp_parser_type_id (parser));
25495 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25497 if (!type)
25499 error_at (token->location,
25500 "%<@encode%> must specify a type as an argument");
25501 return error_mark_node;
25504 /* This happens if we find @encode(T) (where T is a template
25505 typename or something dependent on a template typename) when
25506 parsing a template. In that case, we can't compile it
25507 immediately, but we rather create an AT_ENCODE_EXPR which will
25508 need to be instantiated when the template is used.
25510 if (dependent_type_p (type))
25512 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25513 TREE_READONLY (value) = 1;
25514 return value;
25517 return objc_build_encode_expr (type);
25520 /* Parse an Objective-C @defs expression. */
25522 static tree
25523 cp_parser_objc_defs_expression (cp_parser *parser)
25525 tree name;
25527 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25528 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25529 name = cp_parser_identifier (parser);
25530 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25532 return objc_get_class_ivars (name);
25535 /* Parse an Objective-C protocol expression.
25537 objc-protocol-expression:
25538 @protocol ( identifier )
25540 Returns a representation of the protocol expression. */
25542 static tree
25543 cp_parser_objc_protocol_expression (cp_parser* parser)
25545 tree proto;
25547 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25548 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25549 proto = cp_parser_identifier (parser);
25550 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25552 return objc_build_protocol_expr (proto);
25555 /* Parse an Objective-C selector expression.
25557 objc-selector-expression:
25558 @selector ( objc-method-signature )
25560 objc-method-signature:
25561 objc-selector
25562 objc-selector-seq
25564 objc-selector-seq:
25565 objc-selector :
25566 objc-selector-seq objc-selector :
25568 Returns a representation of the method selector. */
25570 static tree
25571 cp_parser_objc_selector_expression (cp_parser* parser)
25573 tree sel_seq = NULL_TREE;
25574 bool maybe_unary_selector_p = true;
25575 cp_token *token;
25576 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25578 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25579 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25580 token = cp_lexer_peek_token (parser->lexer);
25582 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25583 || token->type == CPP_SCOPE)
25585 tree selector = NULL_TREE;
25587 if (token->type != CPP_COLON
25588 || token->type == CPP_SCOPE)
25589 selector = cp_parser_objc_selector (parser);
25591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25592 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25594 /* Detect if we have a unary selector. */
25595 if (maybe_unary_selector_p)
25597 sel_seq = selector;
25598 goto finish_selector;
25600 else
25602 cp_parser_error (parser, "expected %<:%>");
25605 maybe_unary_selector_p = false;
25606 token = cp_lexer_consume_token (parser->lexer);
25608 if (token->type == CPP_SCOPE)
25610 sel_seq
25611 = chainon (sel_seq,
25612 build_tree_list (selector, NULL_TREE));
25613 sel_seq
25614 = chainon (sel_seq,
25615 build_tree_list (NULL_TREE, NULL_TREE));
25617 else
25618 sel_seq
25619 = chainon (sel_seq,
25620 build_tree_list (selector, NULL_TREE));
25622 token = cp_lexer_peek_token (parser->lexer);
25625 finish_selector:
25626 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25628 return objc_build_selector_expr (loc, sel_seq);
25631 /* Parse a list of identifiers.
25633 objc-identifier-list:
25634 identifier
25635 objc-identifier-list , identifier
25637 Returns a TREE_LIST of identifier nodes. */
25639 static tree
25640 cp_parser_objc_identifier_list (cp_parser* parser)
25642 tree identifier;
25643 tree list;
25644 cp_token *sep;
25646 identifier = cp_parser_identifier (parser);
25647 if (identifier == error_mark_node)
25648 return error_mark_node;
25650 list = build_tree_list (NULL_TREE, identifier);
25651 sep = cp_lexer_peek_token (parser->lexer);
25653 while (sep->type == CPP_COMMA)
25655 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25656 identifier = cp_parser_identifier (parser);
25657 if (identifier == error_mark_node)
25658 return list;
25660 list = chainon (list, build_tree_list (NULL_TREE,
25661 identifier));
25662 sep = cp_lexer_peek_token (parser->lexer);
25665 return list;
25668 /* Parse an Objective-C alias declaration.
25670 objc-alias-declaration:
25671 @compatibility_alias identifier identifier ;
25673 This function registers the alias mapping with the Objective-C front end.
25674 It returns nothing. */
25676 static void
25677 cp_parser_objc_alias_declaration (cp_parser* parser)
25679 tree alias, orig;
25681 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25682 alias = cp_parser_identifier (parser);
25683 orig = cp_parser_identifier (parser);
25684 objc_declare_alias (alias, orig);
25685 cp_parser_consume_semicolon_at_end_of_statement (parser);
25688 /* Parse an Objective-C class forward-declaration.
25690 objc-class-declaration:
25691 @class objc-identifier-list ;
25693 The function registers the forward declarations with the Objective-C
25694 front end. It returns nothing. */
25696 static void
25697 cp_parser_objc_class_declaration (cp_parser* parser)
25699 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25700 while (true)
25702 tree id;
25704 id = cp_parser_identifier (parser);
25705 if (id == error_mark_node)
25706 break;
25708 objc_declare_class (id);
25710 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25711 cp_lexer_consume_token (parser->lexer);
25712 else
25713 break;
25715 cp_parser_consume_semicolon_at_end_of_statement (parser);
25718 /* Parse a list of Objective-C protocol references.
25720 objc-protocol-refs-opt:
25721 objc-protocol-refs [opt]
25723 objc-protocol-refs:
25724 < objc-identifier-list >
25726 Returns a TREE_LIST of identifiers, if any. */
25728 static tree
25729 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25731 tree protorefs = NULL_TREE;
25733 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25735 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25736 protorefs = cp_parser_objc_identifier_list (parser);
25737 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25740 return protorefs;
25743 /* Parse a Objective-C visibility specification. */
25745 static void
25746 cp_parser_objc_visibility_spec (cp_parser* parser)
25748 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25750 switch (vis->keyword)
25752 case RID_AT_PRIVATE:
25753 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25754 break;
25755 case RID_AT_PROTECTED:
25756 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25757 break;
25758 case RID_AT_PUBLIC:
25759 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25760 break;
25761 case RID_AT_PACKAGE:
25762 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25763 break;
25764 default:
25765 return;
25768 /* Eat '@private'/'@protected'/'@public'. */
25769 cp_lexer_consume_token (parser->lexer);
25772 /* Parse an Objective-C method type. Return 'true' if it is a class
25773 (+) method, and 'false' if it is an instance (-) method. */
25775 static inline bool
25776 cp_parser_objc_method_type (cp_parser* parser)
25778 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25779 return true;
25780 else
25781 return false;
25784 /* Parse an Objective-C protocol qualifier. */
25786 static tree
25787 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25789 tree quals = NULL_TREE, node;
25790 cp_token *token = cp_lexer_peek_token (parser->lexer);
25792 node = token->u.value;
25794 while (node && identifier_p (node)
25795 && (node == ridpointers [(int) RID_IN]
25796 || node == ridpointers [(int) RID_OUT]
25797 || node == ridpointers [(int) RID_INOUT]
25798 || node == ridpointers [(int) RID_BYCOPY]
25799 || node == ridpointers [(int) RID_BYREF]
25800 || node == ridpointers [(int) RID_ONEWAY]))
25802 quals = tree_cons (NULL_TREE, node, quals);
25803 cp_lexer_consume_token (parser->lexer);
25804 token = cp_lexer_peek_token (parser->lexer);
25805 node = token->u.value;
25808 return quals;
25811 /* Parse an Objective-C typename. */
25813 static tree
25814 cp_parser_objc_typename (cp_parser* parser)
25816 tree type_name = NULL_TREE;
25818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25820 tree proto_quals, cp_type = NULL_TREE;
25822 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25823 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25825 /* An ObjC type name may consist of just protocol qualifiers, in which
25826 case the type shall default to 'id'. */
25827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25829 cp_type = cp_parser_type_id (parser);
25831 /* If the type could not be parsed, an error has already
25832 been produced. For error recovery, behave as if it had
25833 not been specified, which will use the default type
25834 'id'. */
25835 if (cp_type == error_mark_node)
25837 cp_type = NULL_TREE;
25838 /* We need to skip to the closing parenthesis as
25839 cp_parser_type_id() does not seem to do it for
25840 us. */
25841 cp_parser_skip_to_closing_parenthesis (parser,
25842 /*recovering=*/true,
25843 /*or_comma=*/false,
25844 /*consume_paren=*/false);
25848 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25849 type_name = build_tree_list (proto_quals, cp_type);
25852 return type_name;
25855 /* Check to see if TYPE refers to an Objective-C selector name. */
25857 static bool
25858 cp_parser_objc_selector_p (enum cpp_ttype type)
25860 return (type == CPP_NAME || type == CPP_KEYWORD
25861 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25862 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25863 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25864 || type == CPP_XOR || type == CPP_XOR_EQ);
25867 /* Parse an Objective-C selector. */
25869 static tree
25870 cp_parser_objc_selector (cp_parser* parser)
25872 cp_token *token = cp_lexer_consume_token (parser->lexer);
25874 if (!cp_parser_objc_selector_p (token->type))
25876 error_at (token->location, "invalid Objective-C++ selector name");
25877 return error_mark_node;
25880 /* C++ operator names are allowed to appear in ObjC selectors. */
25881 switch (token->type)
25883 case CPP_AND_AND: return get_identifier ("and");
25884 case CPP_AND_EQ: return get_identifier ("and_eq");
25885 case CPP_AND: return get_identifier ("bitand");
25886 case CPP_OR: return get_identifier ("bitor");
25887 case CPP_COMPL: return get_identifier ("compl");
25888 case CPP_NOT: return get_identifier ("not");
25889 case CPP_NOT_EQ: return get_identifier ("not_eq");
25890 case CPP_OR_OR: return get_identifier ("or");
25891 case CPP_OR_EQ: return get_identifier ("or_eq");
25892 case CPP_XOR: return get_identifier ("xor");
25893 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25894 default: return token->u.value;
25898 /* Parse an Objective-C params list. */
25900 static tree
25901 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25903 tree params = NULL_TREE;
25904 bool maybe_unary_selector_p = true;
25905 cp_token *token = cp_lexer_peek_token (parser->lexer);
25907 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25909 tree selector = NULL_TREE, type_name, identifier;
25910 tree parm_attr = NULL_TREE;
25912 if (token->keyword == RID_ATTRIBUTE)
25913 break;
25915 if (token->type != CPP_COLON)
25916 selector = cp_parser_objc_selector (parser);
25918 /* Detect if we have a unary selector. */
25919 if (maybe_unary_selector_p
25920 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25922 params = selector; /* Might be followed by attributes. */
25923 break;
25926 maybe_unary_selector_p = false;
25927 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25929 /* Something went quite wrong. There should be a colon
25930 here, but there is not. Stop parsing parameters. */
25931 break;
25933 type_name = cp_parser_objc_typename (parser);
25934 /* New ObjC allows attributes on parameters too. */
25935 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25936 parm_attr = cp_parser_attributes_opt (parser);
25937 identifier = cp_parser_identifier (parser);
25939 params
25940 = chainon (params,
25941 objc_build_keyword_decl (selector,
25942 type_name,
25943 identifier,
25944 parm_attr));
25946 token = cp_lexer_peek_token (parser->lexer);
25949 if (params == NULL_TREE)
25951 cp_parser_error (parser, "objective-c++ method declaration is expected");
25952 return error_mark_node;
25955 /* We allow tail attributes for the method. */
25956 if (token->keyword == RID_ATTRIBUTE)
25958 *attributes = cp_parser_attributes_opt (parser);
25959 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25960 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25961 return params;
25962 cp_parser_error (parser,
25963 "method attributes must be specified at the end");
25964 return error_mark_node;
25967 if (params == NULL_TREE)
25969 cp_parser_error (parser, "objective-c++ method declaration is expected");
25970 return error_mark_node;
25972 return params;
25975 /* Parse the non-keyword Objective-C params. */
25977 static tree
25978 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25979 tree* attributes)
25981 tree params = make_node (TREE_LIST);
25982 cp_token *token = cp_lexer_peek_token (parser->lexer);
25983 *ellipsisp = false; /* Initially, assume no ellipsis. */
25985 while (token->type == CPP_COMMA)
25987 cp_parameter_declarator *parmdecl;
25988 tree parm;
25990 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25991 token = cp_lexer_peek_token (parser->lexer);
25993 if (token->type == CPP_ELLIPSIS)
25995 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25996 *ellipsisp = true;
25997 token = cp_lexer_peek_token (parser->lexer);
25998 break;
26001 /* TODO: parse attributes for tail parameters. */
26002 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26003 parm = grokdeclarator (parmdecl->declarator,
26004 &parmdecl->decl_specifiers,
26005 PARM, /*initialized=*/0,
26006 /*attrlist=*/NULL);
26008 chainon (params, build_tree_list (NULL_TREE, parm));
26009 token = cp_lexer_peek_token (parser->lexer);
26012 /* We allow tail attributes for the method. */
26013 if (token->keyword == RID_ATTRIBUTE)
26015 if (*attributes == NULL_TREE)
26017 *attributes = cp_parser_attributes_opt (parser);
26018 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26019 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26020 return params;
26022 else
26023 /* We have an error, but parse the attributes, so that we can
26024 carry on. */
26025 *attributes = cp_parser_attributes_opt (parser);
26027 cp_parser_error (parser,
26028 "method attributes must be specified at the end");
26029 return error_mark_node;
26032 return params;
26035 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26037 static void
26038 cp_parser_objc_interstitial_code (cp_parser* parser)
26040 cp_token *token = cp_lexer_peek_token (parser->lexer);
26042 /* If the next token is `extern' and the following token is a string
26043 literal, then we have a linkage specification. */
26044 if (token->keyword == RID_EXTERN
26045 && cp_parser_is_pure_string_literal
26046 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26047 cp_parser_linkage_specification (parser);
26048 /* Handle #pragma, if any. */
26049 else if (token->type == CPP_PRAGMA)
26050 cp_parser_pragma (parser, pragma_objc_icode);
26051 /* Allow stray semicolons. */
26052 else if (token->type == CPP_SEMICOLON)
26053 cp_lexer_consume_token (parser->lexer);
26054 /* Mark methods as optional or required, when building protocols. */
26055 else if (token->keyword == RID_AT_OPTIONAL)
26057 cp_lexer_consume_token (parser->lexer);
26058 objc_set_method_opt (true);
26060 else if (token->keyword == RID_AT_REQUIRED)
26062 cp_lexer_consume_token (parser->lexer);
26063 objc_set_method_opt (false);
26065 else if (token->keyword == RID_NAMESPACE)
26066 cp_parser_namespace_definition (parser);
26067 /* Other stray characters must generate errors. */
26068 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26070 cp_lexer_consume_token (parser->lexer);
26071 error ("stray %qs between Objective-C++ methods",
26072 token->type == CPP_OPEN_BRACE ? "{" : "}");
26074 /* Finally, try to parse a block-declaration, or a function-definition. */
26075 else
26076 cp_parser_block_declaration (parser, /*statement_p=*/false);
26079 /* Parse a method signature. */
26081 static tree
26082 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26084 tree rettype, kwdparms, optparms;
26085 bool ellipsis = false;
26086 bool is_class_method;
26088 is_class_method = cp_parser_objc_method_type (parser);
26089 rettype = cp_parser_objc_typename (parser);
26090 *attributes = NULL_TREE;
26091 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26092 if (kwdparms == error_mark_node)
26093 return error_mark_node;
26094 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26095 if (optparms == error_mark_node)
26096 return error_mark_node;
26098 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26101 static bool
26102 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26104 tree tattr;
26105 cp_lexer_save_tokens (parser->lexer);
26106 tattr = cp_parser_attributes_opt (parser);
26107 gcc_assert (tattr) ;
26109 /* If the attributes are followed by a method introducer, this is not allowed.
26110 Dump the attributes and flag the situation. */
26111 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26112 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26113 return true;
26115 /* Otherwise, the attributes introduce some interstitial code, possibly so
26116 rewind to allow that check. */
26117 cp_lexer_rollback_tokens (parser->lexer);
26118 return false;
26121 /* Parse an Objective-C method prototype list. */
26123 static void
26124 cp_parser_objc_method_prototype_list (cp_parser* parser)
26126 cp_token *token = cp_lexer_peek_token (parser->lexer);
26128 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26130 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26132 tree attributes, sig;
26133 bool is_class_method;
26134 if (token->type == CPP_PLUS)
26135 is_class_method = true;
26136 else
26137 is_class_method = false;
26138 sig = cp_parser_objc_method_signature (parser, &attributes);
26139 if (sig == error_mark_node)
26141 cp_parser_skip_to_end_of_block_or_statement (parser);
26142 token = cp_lexer_peek_token (parser->lexer);
26143 continue;
26145 objc_add_method_declaration (is_class_method, sig, attributes);
26146 cp_parser_consume_semicolon_at_end_of_statement (parser);
26148 else if (token->keyword == RID_AT_PROPERTY)
26149 cp_parser_objc_at_property_declaration (parser);
26150 else if (token->keyword == RID_ATTRIBUTE
26151 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26152 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26153 OPT_Wattributes,
26154 "prefix attributes are ignored for methods");
26155 else
26156 /* Allow for interspersed non-ObjC++ code. */
26157 cp_parser_objc_interstitial_code (parser);
26159 token = cp_lexer_peek_token (parser->lexer);
26162 if (token->type != CPP_EOF)
26163 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26164 else
26165 cp_parser_error (parser, "expected %<@end%>");
26167 objc_finish_interface ();
26170 /* Parse an Objective-C method definition list. */
26172 static void
26173 cp_parser_objc_method_definition_list (cp_parser* parser)
26175 cp_token *token = cp_lexer_peek_token (parser->lexer);
26177 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26179 tree meth;
26181 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26183 cp_token *ptk;
26184 tree sig, attribute;
26185 bool is_class_method;
26186 if (token->type == CPP_PLUS)
26187 is_class_method = true;
26188 else
26189 is_class_method = false;
26190 push_deferring_access_checks (dk_deferred);
26191 sig = cp_parser_objc_method_signature (parser, &attribute);
26192 if (sig == error_mark_node)
26194 cp_parser_skip_to_end_of_block_or_statement (parser);
26195 token = cp_lexer_peek_token (parser->lexer);
26196 continue;
26198 objc_start_method_definition (is_class_method, sig, attribute,
26199 NULL_TREE);
26201 /* For historical reasons, we accept an optional semicolon. */
26202 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26203 cp_lexer_consume_token (parser->lexer);
26205 ptk = cp_lexer_peek_token (parser->lexer);
26206 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26207 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26209 perform_deferred_access_checks (tf_warning_or_error);
26210 stop_deferring_access_checks ();
26211 meth = cp_parser_function_definition_after_declarator (parser,
26212 false);
26213 pop_deferring_access_checks ();
26214 objc_finish_method_definition (meth);
26217 /* The following case will be removed once @synthesize is
26218 completely implemented. */
26219 else if (token->keyword == RID_AT_PROPERTY)
26220 cp_parser_objc_at_property_declaration (parser);
26221 else if (token->keyword == RID_AT_SYNTHESIZE)
26222 cp_parser_objc_at_synthesize_declaration (parser);
26223 else if (token->keyword == RID_AT_DYNAMIC)
26224 cp_parser_objc_at_dynamic_declaration (parser);
26225 else if (token->keyword == RID_ATTRIBUTE
26226 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26227 warning_at (token->location, OPT_Wattributes,
26228 "prefix attributes are ignored for methods");
26229 else
26230 /* Allow for interspersed non-ObjC++ code. */
26231 cp_parser_objc_interstitial_code (parser);
26233 token = cp_lexer_peek_token (parser->lexer);
26236 if (token->type != CPP_EOF)
26237 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26238 else
26239 cp_parser_error (parser, "expected %<@end%>");
26241 objc_finish_implementation ();
26244 /* Parse Objective-C ivars. */
26246 static void
26247 cp_parser_objc_class_ivars (cp_parser* parser)
26249 cp_token *token = cp_lexer_peek_token (parser->lexer);
26251 if (token->type != CPP_OPEN_BRACE)
26252 return; /* No ivars specified. */
26254 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26255 token = cp_lexer_peek_token (parser->lexer);
26257 while (token->type != CPP_CLOSE_BRACE
26258 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26260 cp_decl_specifier_seq declspecs;
26261 int decl_class_or_enum_p;
26262 tree prefix_attributes;
26264 cp_parser_objc_visibility_spec (parser);
26266 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26267 break;
26269 cp_parser_decl_specifier_seq (parser,
26270 CP_PARSER_FLAGS_OPTIONAL,
26271 &declspecs,
26272 &decl_class_or_enum_p);
26274 /* auto, register, static, extern, mutable. */
26275 if (declspecs.storage_class != sc_none)
26277 cp_parser_error (parser, "invalid type for instance variable");
26278 declspecs.storage_class = sc_none;
26281 /* thread_local. */
26282 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26284 cp_parser_error (parser, "invalid type for instance variable");
26285 declspecs.locations[ds_thread] = 0;
26288 /* typedef. */
26289 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26291 cp_parser_error (parser, "invalid type for instance variable");
26292 declspecs.locations[ds_typedef] = 0;
26295 prefix_attributes = declspecs.attributes;
26296 declspecs.attributes = NULL_TREE;
26298 /* Keep going until we hit the `;' at the end of the
26299 declaration. */
26300 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26302 tree width = NULL_TREE, attributes, first_attribute, decl;
26303 cp_declarator *declarator = NULL;
26304 int ctor_dtor_or_conv_p;
26306 /* Check for a (possibly unnamed) bitfield declaration. */
26307 token = cp_lexer_peek_token (parser->lexer);
26308 if (token->type == CPP_COLON)
26309 goto eat_colon;
26311 if (token->type == CPP_NAME
26312 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26313 == CPP_COLON))
26315 /* Get the name of the bitfield. */
26316 declarator = make_id_declarator (NULL_TREE,
26317 cp_parser_identifier (parser),
26318 sfk_none);
26320 eat_colon:
26321 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26322 /* Get the width of the bitfield. */
26323 width
26324 = cp_parser_constant_expression (parser,
26325 /*allow_non_constant=*/false,
26326 NULL);
26328 else
26330 /* Parse the declarator. */
26331 declarator
26332 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26333 &ctor_dtor_or_conv_p,
26334 /*parenthesized_p=*/NULL,
26335 /*member_p=*/false,
26336 /*friend_p=*/false);
26339 /* Look for attributes that apply to the ivar. */
26340 attributes = cp_parser_attributes_opt (parser);
26341 /* Remember which attributes are prefix attributes and
26342 which are not. */
26343 first_attribute = attributes;
26344 /* Combine the attributes. */
26345 attributes = chainon (prefix_attributes, attributes);
26347 if (width)
26348 /* Create the bitfield declaration. */
26349 decl = grokbitfield (declarator, &declspecs,
26350 width,
26351 attributes);
26352 else
26353 decl = grokfield (declarator, &declspecs,
26354 NULL_TREE, /*init_const_expr_p=*/false,
26355 NULL_TREE, attributes);
26357 /* Add the instance variable. */
26358 if (decl != error_mark_node && decl != NULL_TREE)
26359 objc_add_instance_variable (decl);
26361 /* Reset PREFIX_ATTRIBUTES. */
26362 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26363 attributes = TREE_CHAIN (attributes);
26364 if (attributes)
26365 TREE_CHAIN (attributes) = NULL_TREE;
26367 token = cp_lexer_peek_token (parser->lexer);
26369 if (token->type == CPP_COMMA)
26371 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26372 continue;
26374 break;
26377 cp_parser_consume_semicolon_at_end_of_statement (parser);
26378 token = cp_lexer_peek_token (parser->lexer);
26381 if (token->keyword == RID_AT_END)
26382 cp_parser_error (parser, "expected %<}%>");
26384 /* Do not consume the RID_AT_END, so it will be read again as terminating
26385 the @interface of @implementation. */
26386 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26387 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26389 /* For historical reasons, we accept an optional semicolon. */
26390 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26391 cp_lexer_consume_token (parser->lexer);
26394 /* Parse an Objective-C protocol declaration. */
26396 static void
26397 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26399 tree proto, protorefs;
26400 cp_token *tok;
26402 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26405 tok = cp_lexer_peek_token (parser->lexer);
26406 error_at (tok->location, "identifier expected after %<@protocol%>");
26407 cp_parser_consume_semicolon_at_end_of_statement (parser);
26408 return;
26411 /* See if we have a forward declaration or a definition. */
26412 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26414 /* Try a forward declaration first. */
26415 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26417 while (true)
26419 tree id;
26421 id = cp_parser_identifier (parser);
26422 if (id == error_mark_node)
26423 break;
26425 objc_declare_protocol (id, attributes);
26427 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26428 cp_lexer_consume_token (parser->lexer);
26429 else
26430 break;
26432 cp_parser_consume_semicolon_at_end_of_statement (parser);
26435 /* Ok, we got a full-fledged definition (or at least should). */
26436 else
26438 proto = cp_parser_identifier (parser);
26439 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26440 objc_start_protocol (proto, protorefs, attributes);
26441 cp_parser_objc_method_prototype_list (parser);
26445 /* Parse an Objective-C superclass or category. */
26447 static void
26448 cp_parser_objc_superclass_or_category (cp_parser *parser,
26449 bool iface_p,
26450 tree *super,
26451 tree *categ, bool *is_class_extension)
26453 cp_token *next = cp_lexer_peek_token (parser->lexer);
26455 *super = *categ = NULL_TREE;
26456 *is_class_extension = false;
26457 if (next->type == CPP_COLON)
26459 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26460 *super = cp_parser_identifier (parser);
26462 else if (next->type == CPP_OPEN_PAREN)
26464 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26466 /* If there is no category name, and this is an @interface, we
26467 have a class extension. */
26468 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26470 *categ = NULL_TREE;
26471 *is_class_extension = true;
26473 else
26474 *categ = cp_parser_identifier (parser);
26476 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26480 /* Parse an Objective-C class interface. */
26482 static void
26483 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26485 tree name, super, categ, protos;
26486 bool is_class_extension;
26488 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26489 name = cp_parser_identifier (parser);
26490 if (name == error_mark_node)
26492 /* It's hard to recover because even if valid @interface stuff
26493 is to follow, we can't compile it (or validate it) if we
26494 don't even know which class it refers to. Let's assume this
26495 was a stray '@interface' token in the stream and skip it.
26497 return;
26499 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26500 &is_class_extension);
26501 protos = cp_parser_objc_protocol_refs_opt (parser);
26503 /* We have either a class or a category on our hands. */
26504 if (categ || is_class_extension)
26505 objc_start_category_interface (name, categ, protos, attributes);
26506 else
26508 objc_start_class_interface (name, super, protos, attributes);
26509 /* Handle instance variable declarations, if any. */
26510 cp_parser_objc_class_ivars (parser);
26511 objc_continue_interface ();
26514 cp_parser_objc_method_prototype_list (parser);
26517 /* Parse an Objective-C class implementation. */
26519 static void
26520 cp_parser_objc_class_implementation (cp_parser* parser)
26522 tree name, super, categ;
26523 bool is_class_extension;
26525 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26526 name = cp_parser_identifier (parser);
26527 if (name == error_mark_node)
26529 /* It's hard to recover because even if valid @implementation
26530 stuff is to follow, we can't compile it (or validate it) if
26531 we don't even know which class it refers to. Let's assume
26532 this was a stray '@implementation' token in the stream and
26533 skip it.
26535 return;
26537 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26538 &is_class_extension);
26540 /* We have either a class or a category on our hands. */
26541 if (categ)
26542 objc_start_category_implementation (name, categ);
26543 else
26545 objc_start_class_implementation (name, super);
26546 /* Handle instance variable declarations, if any. */
26547 cp_parser_objc_class_ivars (parser);
26548 objc_continue_implementation ();
26551 cp_parser_objc_method_definition_list (parser);
26554 /* Consume the @end token and finish off the implementation. */
26556 static void
26557 cp_parser_objc_end_implementation (cp_parser* parser)
26559 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26560 objc_finish_implementation ();
26563 /* Parse an Objective-C declaration. */
26565 static void
26566 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26568 /* Try to figure out what kind of declaration is present. */
26569 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26571 if (attributes)
26572 switch (kwd->keyword)
26574 case RID_AT_ALIAS:
26575 case RID_AT_CLASS:
26576 case RID_AT_END:
26577 error_at (kwd->location, "attributes may not be specified before"
26578 " the %<@%D%> Objective-C++ keyword",
26579 kwd->u.value);
26580 attributes = NULL;
26581 break;
26582 case RID_AT_IMPLEMENTATION:
26583 warning_at (kwd->location, OPT_Wattributes,
26584 "prefix attributes are ignored before %<@%D%>",
26585 kwd->u.value);
26586 attributes = NULL;
26587 default:
26588 break;
26591 switch (kwd->keyword)
26593 case RID_AT_ALIAS:
26594 cp_parser_objc_alias_declaration (parser);
26595 break;
26596 case RID_AT_CLASS:
26597 cp_parser_objc_class_declaration (parser);
26598 break;
26599 case RID_AT_PROTOCOL:
26600 cp_parser_objc_protocol_declaration (parser, attributes);
26601 break;
26602 case RID_AT_INTERFACE:
26603 cp_parser_objc_class_interface (parser, attributes);
26604 break;
26605 case RID_AT_IMPLEMENTATION:
26606 cp_parser_objc_class_implementation (parser);
26607 break;
26608 case RID_AT_END:
26609 cp_parser_objc_end_implementation (parser);
26610 break;
26611 default:
26612 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26613 kwd->u.value);
26614 cp_parser_skip_to_end_of_block_or_statement (parser);
26618 /* Parse an Objective-C try-catch-finally statement.
26620 objc-try-catch-finally-stmt:
26621 @try compound-statement objc-catch-clause-seq [opt]
26622 objc-finally-clause [opt]
26624 objc-catch-clause-seq:
26625 objc-catch-clause objc-catch-clause-seq [opt]
26627 objc-catch-clause:
26628 @catch ( objc-exception-declaration ) compound-statement
26630 objc-finally-clause:
26631 @finally compound-statement
26633 objc-exception-declaration:
26634 parameter-declaration
26635 '...'
26637 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26639 Returns NULL_TREE.
26641 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26642 for C. Keep them in sync. */
26644 static tree
26645 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26647 location_t location;
26648 tree stmt;
26650 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26651 location = cp_lexer_peek_token (parser->lexer)->location;
26652 objc_maybe_warn_exceptions (location);
26653 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26654 node, lest it get absorbed into the surrounding block. */
26655 stmt = push_stmt_list ();
26656 cp_parser_compound_statement (parser, NULL, false, false);
26657 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26659 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26661 cp_parameter_declarator *parm;
26662 tree parameter_declaration = error_mark_node;
26663 bool seen_open_paren = false;
26665 cp_lexer_consume_token (parser->lexer);
26666 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26667 seen_open_paren = true;
26668 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26670 /* We have "@catch (...)" (where the '...' are literally
26671 what is in the code). Skip the '...'.
26672 parameter_declaration is set to NULL_TREE, and
26673 objc_being_catch_clauses() knows that that means
26674 '...'. */
26675 cp_lexer_consume_token (parser->lexer);
26676 parameter_declaration = NULL_TREE;
26678 else
26680 /* We have "@catch (NSException *exception)" or something
26681 like that. Parse the parameter declaration. */
26682 parm = cp_parser_parameter_declaration (parser, false, NULL);
26683 if (parm == NULL)
26684 parameter_declaration = error_mark_node;
26685 else
26686 parameter_declaration = grokdeclarator (parm->declarator,
26687 &parm->decl_specifiers,
26688 PARM, /*initialized=*/0,
26689 /*attrlist=*/NULL);
26691 if (seen_open_paren)
26692 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26693 else
26695 /* If there was no open parenthesis, we are recovering from
26696 an error, and we are trying to figure out what mistake
26697 the user has made. */
26699 /* If there is an immediate closing parenthesis, the user
26700 probably forgot the opening one (ie, they typed "@catch
26701 NSException *e)". Parse the closing parenthesis and keep
26702 going. */
26703 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26704 cp_lexer_consume_token (parser->lexer);
26706 /* If these is no immediate closing parenthesis, the user
26707 probably doesn't know that parenthesis are required at
26708 all (ie, they typed "@catch NSException *e"). So, just
26709 forget about the closing parenthesis and keep going. */
26711 objc_begin_catch_clause (parameter_declaration);
26712 cp_parser_compound_statement (parser, NULL, false, false);
26713 objc_finish_catch_clause ();
26715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26717 cp_lexer_consume_token (parser->lexer);
26718 location = cp_lexer_peek_token (parser->lexer)->location;
26719 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26720 node, lest it get absorbed into the surrounding block. */
26721 stmt = push_stmt_list ();
26722 cp_parser_compound_statement (parser, NULL, false, false);
26723 objc_build_finally_clause (location, pop_stmt_list (stmt));
26726 return objc_finish_try_stmt ();
26729 /* Parse an Objective-C synchronized statement.
26731 objc-synchronized-stmt:
26732 @synchronized ( expression ) compound-statement
26734 Returns NULL_TREE. */
26736 static tree
26737 cp_parser_objc_synchronized_statement (cp_parser *parser)
26739 location_t location;
26740 tree lock, stmt;
26742 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26744 location = cp_lexer_peek_token (parser->lexer)->location;
26745 objc_maybe_warn_exceptions (location);
26746 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26747 lock = cp_parser_expression (parser, false, NULL);
26748 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26750 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26751 node, lest it get absorbed into the surrounding block. */
26752 stmt = push_stmt_list ();
26753 cp_parser_compound_statement (parser, NULL, false, false);
26755 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26758 /* Parse an Objective-C throw statement.
26760 objc-throw-stmt:
26761 @throw assignment-expression [opt] ;
26763 Returns a constructed '@throw' statement. */
26765 static tree
26766 cp_parser_objc_throw_statement (cp_parser *parser)
26768 tree expr = NULL_TREE;
26769 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26771 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26774 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26776 cp_parser_consume_semicolon_at_end_of_statement (parser);
26778 return objc_build_throw_stmt (loc, expr);
26781 /* Parse an Objective-C statement. */
26783 static tree
26784 cp_parser_objc_statement (cp_parser * parser)
26786 /* Try to figure out what kind of declaration is present. */
26787 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26789 switch (kwd->keyword)
26791 case RID_AT_TRY:
26792 return cp_parser_objc_try_catch_finally_statement (parser);
26793 case RID_AT_SYNCHRONIZED:
26794 return cp_parser_objc_synchronized_statement (parser);
26795 case RID_AT_THROW:
26796 return cp_parser_objc_throw_statement (parser);
26797 default:
26798 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26799 kwd->u.value);
26800 cp_parser_skip_to_end_of_block_or_statement (parser);
26803 return error_mark_node;
26806 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26807 look ahead to see if an objc keyword follows the attributes. This
26808 is to detect the use of prefix attributes on ObjC @interface and
26809 @protocol. */
26811 static bool
26812 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26814 cp_lexer_save_tokens (parser->lexer);
26815 *attrib = cp_parser_attributes_opt (parser);
26816 gcc_assert (*attrib);
26817 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26819 cp_lexer_commit_tokens (parser->lexer);
26820 return true;
26822 cp_lexer_rollback_tokens (parser->lexer);
26823 return false;
26826 /* This routine is a minimal replacement for
26827 c_parser_struct_declaration () used when parsing the list of
26828 types/names or ObjC++ properties. For example, when parsing the
26829 code
26831 @property (readonly) int a, b, c;
26833 this function is responsible for parsing "int a, int b, int c" and
26834 returning the declarations as CHAIN of DECLs.
26836 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26837 similar parsing. */
26838 static tree
26839 cp_parser_objc_struct_declaration (cp_parser *parser)
26841 tree decls = NULL_TREE;
26842 cp_decl_specifier_seq declspecs;
26843 int decl_class_or_enum_p;
26844 tree prefix_attributes;
26846 cp_parser_decl_specifier_seq (parser,
26847 CP_PARSER_FLAGS_NONE,
26848 &declspecs,
26849 &decl_class_or_enum_p);
26851 if (declspecs.type == error_mark_node)
26852 return error_mark_node;
26854 /* auto, register, static, extern, mutable. */
26855 if (declspecs.storage_class != sc_none)
26857 cp_parser_error (parser, "invalid type for property");
26858 declspecs.storage_class = sc_none;
26861 /* thread_local. */
26862 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26864 cp_parser_error (parser, "invalid type for property");
26865 declspecs.locations[ds_thread] = 0;
26868 /* typedef. */
26869 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26871 cp_parser_error (parser, "invalid type for property");
26872 declspecs.locations[ds_typedef] = 0;
26875 prefix_attributes = declspecs.attributes;
26876 declspecs.attributes = NULL_TREE;
26878 /* Keep going until we hit the `;' at the end of the declaration. */
26879 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26881 tree attributes, first_attribute, decl;
26882 cp_declarator *declarator;
26883 cp_token *token;
26885 /* Parse the declarator. */
26886 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26887 NULL, NULL, false, false);
26889 /* Look for attributes that apply to the ivar. */
26890 attributes = cp_parser_attributes_opt (parser);
26891 /* Remember which attributes are prefix attributes and
26892 which are not. */
26893 first_attribute = attributes;
26894 /* Combine the attributes. */
26895 attributes = chainon (prefix_attributes, attributes);
26897 decl = grokfield (declarator, &declspecs,
26898 NULL_TREE, /*init_const_expr_p=*/false,
26899 NULL_TREE, attributes);
26901 if (decl == error_mark_node || decl == NULL_TREE)
26902 return error_mark_node;
26904 /* Reset PREFIX_ATTRIBUTES. */
26905 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26906 attributes = TREE_CHAIN (attributes);
26907 if (attributes)
26908 TREE_CHAIN (attributes) = NULL_TREE;
26910 DECL_CHAIN (decl) = decls;
26911 decls = decl;
26913 token = cp_lexer_peek_token (parser->lexer);
26914 if (token->type == CPP_COMMA)
26916 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26917 continue;
26919 else
26920 break;
26922 return decls;
26925 /* Parse an Objective-C @property declaration. The syntax is:
26927 objc-property-declaration:
26928 '@property' objc-property-attributes[opt] struct-declaration ;
26930 objc-property-attributes:
26931 '(' objc-property-attribute-list ')'
26933 objc-property-attribute-list:
26934 objc-property-attribute
26935 objc-property-attribute-list, objc-property-attribute
26937 objc-property-attribute
26938 'getter' = identifier
26939 'setter' = identifier
26940 'readonly'
26941 'readwrite'
26942 'assign'
26943 'retain'
26944 'copy'
26945 'nonatomic'
26947 For example:
26948 @property NSString *name;
26949 @property (readonly) id object;
26950 @property (retain, nonatomic, getter=getTheName) id name;
26951 @property int a, b, c;
26953 PS: This function is identical to
26954 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26955 static void
26956 cp_parser_objc_at_property_declaration (cp_parser *parser)
26958 /* The following variables hold the attributes of the properties as
26959 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26960 seen. When we see an attribute, we set them to 'true' (if they
26961 are boolean properties) or to the identifier (if they have an
26962 argument, ie, for getter and setter). Note that here we only
26963 parse the list of attributes, check the syntax and accumulate the
26964 attributes that we find. objc_add_property_declaration() will
26965 then process the information. */
26966 bool property_assign = false;
26967 bool property_copy = false;
26968 tree property_getter_ident = NULL_TREE;
26969 bool property_nonatomic = false;
26970 bool property_readonly = false;
26971 bool property_readwrite = false;
26972 bool property_retain = false;
26973 tree property_setter_ident = NULL_TREE;
26975 /* 'properties' is the list of properties that we read. Usually a
26976 single one, but maybe more (eg, in "@property int a, b, c;" there
26977 are three). */
26978 tree properties;
26979 location_t loc;
26981 loc = cp_lexer_peek_token (parser->lexer)->location;
26983 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26985 /* Parse the optional attribute list... */
26986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26988 /* Eat the '('. */
26989 cp_lexer_consume_token (parser->lexer);
26991 while (true)
26993 bool syntax_error = false;
26994 cp_token *token = cp_lexer_peek_token (parser->lexer);
26995 enum rid keyword;
26997 if (token->type != CPP_NAME)
26999 cp_parser_error (parser, "expected identifier");
27000 break;
27002 keyword = C_RID_CODE (token->u.value);
27003 cp_lexer_consume_token (parser->lexer);
27004 switch (keyword)
27006 case RID_ASSIGN: property_assign = true; break;
27007 case RID_COPY: property_copy = true; break;
27008 case RID_NONATOMIC: property_nonatomic = true; break;
27009 case RID_READONLY: property_readonly = true; break;
27010 case RID_READWRITE: property_readwrite = true; break;
27011 case RID_RETAIN: property_retain = true; break;
27013 case RID_GETTER:
27014 case RID_SETTER:
27015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27017 if (keyword == RID_GETTER)
27018 cp_parser_error (parser,
27019 "missing %<=%> (after %<getter%> attribute)");
27020 else
27021 cp_parser_error (parser,
27022 "missing %<=%> (after %<setter%> attribute)");
27023 syntax_error = true;
27024 break;
27026 cp_lexer_consume_token (parser->lexer); /* eat the = */
27027 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27029 cp_parser_error (parser, "expected identifier");
27030 syntax_error = true;
27031 break;
27033 if (keyword == RID_SETTER)
27035 if (property_setter_ident != NULL_TREE)
27037 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27038 cp_lexer_consume_token (parser->lexer);
27040 else
27041 property_setter_ident = cp_parser_objc_selector (parser);
27042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27043 cp_parser_error (parser, "setter name must terminate with %<:%>");
27044 else
27045 cp_lexer_consume_token (parser->lexer);
27047 else
27049 if (property_getter_ident != NULL_TREE)
27051 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27052 cp_lexer_consume_token (parser->lexer);
27054 else
27055 property_getter_ident = cp_parser_objc_selector (parser);
27057 break;
27058 default:
27059 cp_parser_error (parser, "unknown property attribute");
27060 syntax_error = true;
27061 break;
27064 if (syntax_error)
27065 break;
27067 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27068 cp_lexer_consume_token (parser->lexer);
27069 else
27070 break;
27073 /* FIXME: "@property (setter, assign);" will generate a spurious
27074 "error: expected ‘)’ before ‘,’ token". This is because
27075 cp_parser_require, unlike the C counterpart, will produce an
27076 error even if we are in error recovery. */
27077 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27079 cp_parser_skip_to_closing_parenthesis (parser,
27080 /*recovering=*/true,
27081 /*or_comma=*/false,
27082 /*consume_paren=*/true);
27086 /* ... and the property declaration(s). */
27087 properties = cp_parser_objc_struct_declaration (parser);
27089 if (properties == error_mark_node)
27091 cp_parser_skip_to_end_of_statement (parser);
27092 /* If the next token is now a `;', consume it. */
27093 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27094 cp_lexer_consume_token (parser->lexer);
27095 return;
27098 if (properties == NULL_TREE)
27099 cp_parser_error (parser, "expected identifier");
27100 else
27102 /* Comma-separated properties are chained together in
27103 reverse order; add them one by one. */
27104 properties = nreverse (properties);
27106 for (; properties; properties = TREE_CHAIN (properties))
27107 objc_add_property_declaration (loc, copy_node (properties),
27108 property_readonly, property_readwrite,
27109 property_assign, property_retain,
27110 property_copy, property_nonatomic,
27111 property_getter_ident, property_setter_ident);
27114 cp_parser_consume_semicolon_at_end_of_statement (parser);
27117 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27119 objc-synthesize-declaration:
27120 @synthesize objc-synthesize-identifier-list ;
27122 objc-synthesize-identifier-list:
27123 objc-synthesize-identifier
27124 objc-synthesize-identifier-list, objc-synthesize-identifier
27126 objc-synthesize-identifier
27127 identifier
27128 identifier = identifier
27130 For example:
27131 @synthesize MyProperty;
27132 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27134 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27135 for C. Keep them in sync.
27137 static void
27138 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27140 tree list = NULL_TREE;
27141 location_t loc;
27142 loc = cp_lexer_peek_token (parser->lexer)->location;
27144 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27145 while (true)
27147 tree property, ivar;
27148 property = cp_parser_identifier (parser);
27149 if (property == error_mark_node)
27151 cp_parser_consume_semicolon_at_end_of_statement (parser);
27152 return;
27154 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27156 cp_lexer_consume_token (parser->lexer);
27157 ivar = cp_parser_identifier (parser);
27158 if (ivar == error_mark_node)
27160 cp_parser_consume_semicolon_at_end_of_statement (parser);
27161 return;
27164 else
27165 ivar = NULL_TREE;
27166 list = chainon (list, build_tree_list (ivar, property));
27167 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27168 cp_lexer_consume_token (parser->lexer);
27169 else
27170 break;
27172 cp_parser_consume_semicolon_at_end_of_statement (parser);
27173 objc_add_synthesize_declaration (loc, list);
27176 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27178 objc-dynamic-declaration:
27179 @dynamic identifier-list ;
27181 For example:
27182 @dynamic MyProperty;
27183 @dynamic MyProperty, AnotherProperty;
27185 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27186 for C. Keep them in sync.
27188 static void
27189 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27191 tree list = NULL_TREE;
27192 location_t loc;
27193 loc = cp_lexer_peek_token (parser->lexer)->location;
27195 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27196 while (true)
27198 tree property;
27199 property = cp_parser_identifier (parser);
27200 if (property == error_mark_node)
27202 cp_parser_consume_semicolon_at_end_of_statement (parser);
27203 return;
27205 list = chainon (list, build_tree_list (NULL, property));
27206 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27207 cp_lexer_consume_token (parser->lexer);
27208 else
27209 break;
27211 cp_parser_consume_semicolon_at_end_of_statement (parser);
27212 objc_add_dynamic_declaration (loc, list);
27216 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27218 /* Returns name of the next clause.
27219 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27220 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27221 returned and the token is consumed. */
27223 static pragma_omp_clause
27224 cp_parser_omp_clause_name (cp_parser *parser)
27226 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27228 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27229 result = PRAGMA_OMP_CLAUSE_IF;
27230 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27231 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27232 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27233 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27234 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27235 result = PRAGMA_OMP_CLAUSE_FOR;
27236 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27238 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27239 const char *p = IDENTIFIER_POINTER (id);
27241 switch (p[0])
27243 case 'a':
27244 if (!strcmp ("aligned", p))
27245 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27246 break;
27247 case 'c':
27248 if (!strcmp ("collapse", p))
27249 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27250 else if (!strcmp ("copyin", p))
27251 result = PRAGMA_OMP_CLAUSE_COPYIN;
27252 else if (!strcmp ("copyprivate", p))
27253 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27254 break;
27255 case 'd':
27256 if (!strcmp ("depend", p))
27257 result = PRAGMA_OMP_CLAUSE_DEPEND;
27258 else if (!strcmp ("device", p))
27259 result = PRAGMA_OMP_CLAUSE_DEVICE;
27260 else if (!strcmp ("dist_schedule", p))
27261 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27262 break;
27263 case 'f':
27264 if (!strcmp ("final", p))
27265 result = PRAGMA_OMP_CLAUSE_FINAL;
27266 else if (!strcmp ("firstprivate", p))
27267 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27268 else if (!strcmp ("from", p))
27269 result = PRAGMA_OMP_CLAUSE_FROM;
27270 break;
27271 case 'i':
27272 if (!strcmp ("inbranch", p))
27273 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27274 break;
27275 case 'l':
27276 if (!strcmp ("lastprivate", p))
27277 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27278 else if (!strcmp ("linear", p))
27279 result = PRAGMA_OMP_CLAUSE_LINEAR;
27280 break;
27281 case 'm':
27282 if (!strcmp ("map", p))
27283 result = PRAGMA_OMP_CLAUSE_MAP;
27284 else if (!strcmp ("mergeable", p))
27285 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27286 else if (flag_cilkplus && !strcmp ("mask", p))
27287 result = PRAGMA_CILK_CLAUSE_MASK;
27288 break;
27289 case 'n':
27290 if (!strcmp ("notinbranch", p))
27291 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27292 else if (!strcmp ("nowait", p))
27293 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27294 else if (flag_cilkplus && !strcmp ("nomask", p))
27295 result = PRAGMA_CILK_CLAUSE_NOMASK;
27296 else if (!strcmp ("num_teams", p))
27297 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27298 else if (!strcmp ("num_threads", p))
27299 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27300 break;
27301 case 'o':
27302 if (!strcmp ("ordered", p))
27303 result = PRAGMA_OMP_CLAUSE_ORDERED;
27304 break;
27305 case 'p':
27306 if (!strcmp ("parallel", p))
27307 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27308 else if (!strcmp ("proc_bind", p))
27309 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27310 break;
27311 case 'r':
27312 if (!strcmp ("reduction", p))
27313 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27314 break;
27315 case 's':
27316 if (!strcmp ("safelen", p))
27317 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27318 else if (!strcmp ("schedule", p))
27319 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27320 else if (!strcmp ("sections", p))
27321 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27322 else if (!strcmp ("shared", p))
27323 result = PRAGMA_OMP_CLAUSE_SHARED;
27324 else if (!strcmp ("simdlen", p))
27325 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27326 break;
27327 case 't':
27328 if (!strcmp ("taskgroup", p))
27329 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27330 else if (!strcmp ("thread_limit", p))
27331 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27332 else if (!strcmp ("to", p))
27333 result = PRAGMA_OMP_CLAUSE_TO;
27334 break;
27335 case 'u':
27336 if (!strcmp ("uniform", p))
27337 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27338 else if (!strcmp ("untied", p))
27339 result = PRAGMA_OMP_CLAUSE_UNTIED;
27340 break;
27341 case 'v':
27342 if (flag_cilkplus && !strcmp ("vectorlength", p))
27343 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27344 break;
27348 if (result != PRAGMA_OMP_CLAUSE_NONE)
27349 cp_lexer_consume_token (parser->lexer);
27351 return result;
27354 /* Validate that a clause of the given type does not already exist. */
27356 static void
27357 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27358 const char *name, location_t location)
27360 tree c;
27362 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27363 if (OMP_CLAUSE_CODE (c) == code)
27365 error_at (location, "too many %qs clauses", name);
27366 break;
27370 /* OpenMP 2.5:
27371 variable-list:
27372 identifier
27373 variable-list , identifier
27375 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27376 colon). An opening parenthesis will have been consumed by the caller.
27378 If KIND is nonzero, create the appropriate node and install the decl
27379 in OMP_CLAUSE_DECL and add the node to the head of the list.
27381 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27382 return the list created.
27384 COLON can be NULL if only closing parenthesis should end the list,
27385 or pointer to bool which will receive false if the list is terminated
27386 by closing parenthesis or true if the list is terminated by colon. */
27388 static tree
27389 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27390 tree list, bool *colon)
27392 cp_token *token;
27393 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27394 if (colon)
27396 parser->colon_corrects_to_scope_p = false;
27397 *colon = false;
27399 while (1)
27401 tree name, decl;
27403 token = cp_lexer_peek_token (parser->lexer);
27404 name = cp_parser_id_expression (parser, /*template_p=*/false,
27405 /*check_dependency_p=*/true,
27406 /*template_p=*/NULL,
27407 /*declarator_p=*/false,
27408 /*optional_p=*/false);
27409 if (name == error_mark_node)
27410 goto skip_comma;
27412 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27413 if (decl == error_mark_node)
27414 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27415 token->location);
27416 else if (kind != 0)
27418 switch (kind)
27420 case OMP_CLAUSE_MAP:
27421 case OMP_CLAUSE_FROM:
27422 case OMP_CLAUSE_TO:
27423 case OMP_CLAUSE_DEPEND:
27424 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27426 tree low_bound = NULL_TREE, length = NULL_TREE;
27428 parser->colon_corrects_to_scope_p = false;
27429 cp_lexer_consume_token (parser->lexer);
27430 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27431 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27432 NULL);
27433 if (!colon)
27434 parser->colon_corrects_to_scope_p
27435 = saved_colon_corrects_to_scope_p;
27436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27437 length = integer_one_node;
27438 else
27440 /* Look for `:'. */
27441 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27442 goto skip_comma;
27443 if (!cp_lexer_next_token_is (parser->lexer,
27444 CPP_CLOSE_SQUARE))
27445 length = cp_parser_expression (parser,
27446 /*cast_p=*/false,
27447 NULL);
27449 /* Look for the closing `]'. */
27450 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27451 RT_CLOSE_SQUARE))
27452 goto skip_comma;
27453 decl = tree_cons (low_bound, length, decl);
27455 break;
27456 default:
27457 break;
27460 tree u = build_omp_clause (token->location, kind);
27461 OMP_CLAUSE_DECL (u) = decl;
27462 OMP_CLAUSE_CHAIN (u) = list;
27463 list = u;
27465 else
27466 list = tree_cons (decl, NULL_TREE, list);
27468 get_comma:
27469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27470 break;
27471 cp_lexer_consume_token (parser->lexer);
27474 if (colon)
27475 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27477 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27479 *colon = true;
27480 cp_parser_require (parser, CPP_COLON, RT_COLON);
27481 return list;
27484 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27486 int ending;
27488 /* Try to resync to an unnested comma. Copied from
27489 cp_parser_parenthesized_expression_list. */
27490 skip_comma:
27491 if (colon)
27492 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27493 ending = cp_parser_skip_to_closing_parenthesis (parser,
27494 /*recovering=*/true,
27495 /*or_comma=*/true,
27496 /*consume_paren=*/true);
27497 if (ending < 0)
27498 goto get_comma;
27501 return list;
27504 /* Similarly, but expect leading and trailing parenthesis. This is a very
27505 common case for omp clauses. */
27507 static tree
27508 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27510 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27511 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27512 return list;
27515 /* OpenMP 3.0:
27516 collapse ( constant-expression ) */
27518 static tree
27519 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27521 tree c, num;
27522 location_t loc;
27523 HOST_WIDE_INT n;
27525 loc = cp_lexer_peek_token (parser->lexer)->location;
27526 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27527 return list;
27529 num = cp_parser_constant_expression (parser, false, NULL);
27531 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27532 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27533 /*or_comma=*/false,
27534 /*consume_paren=*/true);
27536 if (num == error_mark_node)
27537 return list;
27538 num = fold_non_dependent_expr (num);
27539 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27540 || !tree_fits_shwi_p (num)
27541 || (n = tree_to_shwi (num)) <= 0
27542 || (int) n != n)
27544 error_at (loc, "collapse argument needs positive constant integer expression");
27545 return list;
27548 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27549 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27550 OMP_CLAUSE_CHAIN (c) = list;
27551 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27553 return c;
27556 /* OpenMP 2.5:
27557 default ( shared | none ) */
27559 static tree
27560 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27562 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27563 tree c;
27565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27566 return list;
27567 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27569 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27570 const char *p = IDENTIFIER_POINTER (id);
27572 switch (p[0])
27574 case 'n':
27575 if (strcmp ("none", p) != 0)
27576 goto invalid_kind;
27577 kind = OMP_CLAUSE_DEFAULT_NONE;
27578 break;
27580 case 's':
27581 if (strcmp ("shared", p) != 0)
27582 goto invalid_kind;
27583 kind = OMP_CLAUSE_DEFAULT_SHARED;
27584 break;
27586 default:
27587 goto invalid_kind;
27590 cp_lexer_consume_token (parser->lexer);
27592 else
27594 invalid_kind:
27595 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27598 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27599 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27600 /*or_comma=*/false,
27601 /*consume_paren=*/true);
27603 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27604 return list;
27606 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27607 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27608 OMP_CLAUSE_CHAIN (c) = list;
27609 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27611 return c;
27614 /* OpenMP 3.1:
27615 final ( expression ) */
27617 static tree
27618 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27620 tree t, c;
27622 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27623 return list;
27625 t = cp_parser_condition (parser);
27627 if (t == error_mark_node
27628 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27629 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27630 /*or_comma=*/false,
27631 /*consume_paren=*/true);
27633 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27635 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27636 OMP_CLAUSE_FINAL_EXPR (c) = t;
27637 OMP_CLAUSE_CHAIN (c) = list;
27639 return c;
27642 /* OpenMP 2.5:
27643 if ( expression ) */
27645 static tree
27646 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27648 tree t, c;
27650 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27651 return list;
27653 t = cp_parser_condition (parser);
27655 if (t == error_mark_node
27656 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27657 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27658 /*or_comma=*/false,
27659 /*consume_paren=*/true);
27661 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27663 c = build_omp_clause (location, OMP_CLAUSE_IF);
27664 OMP_CLAUSE_IF_EXPR (c) = t;
27665 OMP_CLAUSE_CHAIN (c) = list;
27667 return c;
27670 /* OpenMP 3.1:
27671 mergeable */
27673 static tree
27674 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27675 tree list, location_t location)
27677 tree c;
27679 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27680 location);
27682 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27683 OMP_CLAUSE_CHAIN (c) = list;
27684 return c;
27687 /* OpenMP 2.5:
27688 nowait */
27690 static tree
27691 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27692 tree list, location_t location)
27694 tree c;
27696 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27698 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27699 OMP_CLAUSE_CHAIN (c) = list;
27700 return c;
27703 /* OpenMP 2.5:
27704 num_threads ( expression ) */
27706 static tree
27707 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27708 location_t location)
27710 tree t, c;
27712 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27713 return list;
27715 t = cp_parser_expression (parser, false, NULL);
27717 if (t == error_mark_node
27718 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27720 /*or_comma=*/false,
27721 /*consume_paren=*/true);
27723 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27724 "num_threads", location);
27726 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27727 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27728 OMP_CLAUSE_CHAIN (c) = list;
27730 return c;
27733 /* OpenMP 2.5:
27734 ordered */
27736 static tree
27737 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27738 tree list, location_t location)
27740 tree c;
27742 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27743 "ordered", location);
27745 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27746 OMP_CLAUSE_CHAIN (c) = list;
27747 return c;
27750 /* OpenMP 2.5:
27751 reduction ( reduction-operator : variable-list )
27753 reduction-operator:
27754 One of: + * - & ^ | && ||
27756 OpenMP 3.1:
27758 reduction-operator:
27759 One of: + * - & ^ | && || min max
27761 OpenMP 4.0:
27763 reduction-operator:
27764 One of: + * - & ^ | && ||
27765 id-expression */
27767 static tree
27768 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27770 enum tree_code code = ERROR_MARK;
27771 tree nlist, c, id = NULL_TREE;
27773 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27774 return list;
27776 switch (cp_lexer_peek_token (parser->lexer)->type)
27778 case CPP_PLUS: code = PLUS_EXPR; break;
27779 case CPP_MULT: code = MULT_EXPR; break;
27780 case CPP_MINUS: code = MINUS_EXPR; break;
27781 case CPP_AND: code = BIT_AND_EXPR; break;
27782 case CPP_XOR: code = BIT_XOR_EXPR; break;
27783 case CPP_OR: code = BIT_IOR_EXPR; break;
27784 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27785 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27786 default: break;
27789 if (code != ERROR_MARK)
27790 cp_lexer_consume_token (parser->lexer);
27791 else
27793 bool saved_colon_corrects_to_scope_p;
27794 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27795 parser->colon_corrects_to_scope_p = false;
27796 id = cp_parser_id_expression (parser, /*template_p=*/false,
27797 /*check_dependency_p=*/true,
27798 /*template_p=*/NULL,
27799 /*declarator_p=*/false,
27800 /*optional_p=*/false);
27801 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27802 if (identifier_p (id))
27804 const char *p = IDENTIFIER_POINTER (id);
27806 if (strcmp (p, "min") == 0)
27807 code = MIN_EXPR;
27808 else if (strcmp (p, "max") == 0)
27809 code = MAX_EXPR;
27810 else if (id == ansi_opname (PLUS_EXPR))
27811 code = PLUS_EXPR;
27812 else if (id == ansi_opname (MULT_EXPR))
27813 code = MULT_EXPR;
27814 else if (id == ansi_opname (MINUS_EXPR))
27815 code = MINUS_EXPR;
27816 else if (id == ansi_opname (BIT_AND_EXPR))
27817 code = BIT_AND_EXPR;
27818 else if (id == ansi_opname (BIT_IOR_EXPR))
27819 code = BIT_IOR_EXPR;
27820 else if (id == ansi_opname (BIT_XOR_EXPR))
27821 code = BIT_XOR_EXPR;
27822 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27823 code = TRUTH_ANDIF_EXPR;
27824 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27825 code = TRUTH_ORIF_EXPR;
27826 id = omp_reduction_id (code, id, NULL_TREE);
27827 tree scope = parser->scope;
27828 if (scope)
27829 id = build_qualified_name (NULL_TREE, scope, id, false);
27830 parser->scope = NULL_TREE;
27831 parser->qualifying_scope = NULL_TREE;
27832 parser->object_scope = NULL_TREE;
27834 else
27836 error ("invalid reduction-identifier");
27837 resync_fail:
27838 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27839 /*or_comma=*/false,
27840 /*consume_paren=*/true);
27841 return list;
27845 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27846 goto resync_fail;
27848 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27849 NULL);
27850 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27852 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27853 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27856 return nlist;
27859 /* OpenMP 2.5:
27860 schedule ( schedule-kind )
27861 schedule ( schedule-kind , expression )
27863 schedule-kind:
27864 static | dynamic | guided | runtime | auto */
27866 static tree
27867 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27869 tree c, t;
27871 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27872 return list;
27874 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27878 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27879 const char *p = IDENTIFIER_POINTER (id);
27881 switch (p[0])
27883 case 'd':
27884 if (strcmp ("dynamic", p) != 0)
27885 goto invalid_kind;
27886 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27887 break;
27889 case 'g':
27890 if (strcmp ("guided", p) != 0)
27891 goto invalid_kind;
27892 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27893 break;
27895 case 'r':
27896 if (strcmp ("runtime", p) != 0)
27897 goto invalid_kind;
27898 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27899 break;
27901 default:
27902 goto invalid_kind;
27905 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27906 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27907 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27908 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27909 else
27910 goto invalid_kind;
27911 cp_lexer_consume_token (parser->lexer);
27913 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27915 cp_token *token;
27916 cp_lexer_consume_token (parser->lexer);
27918 token = cp_lexer_peek_token (parser->lexer);
27919 t = cp_parser_assignment_expression (parser, false, NULL);
27921 if (t == error_mark_node)
27922 goto resync_fail;
27923 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27924 error_at (token->location, "schedule %<runtime%> does not take "
27925 "a %<chunk_size%> parameter");
27926 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27927 error_at (token->location, "schedule %<auto%> does not take "
27928 "a %<chunk_size%> parameter");
27929 else
27930 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27932 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27933 goto resync_fail;
27935 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27936 goto resync_fail;
27938 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27939 OMP_CLAUSE_CHAIN (c) = list;
27940 return c;
27942 invalid_kind:
27943 cp_parser_error (parser, "invalid schedule kind");
27944 resync_fail:
27945 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27946 /*or_comma=*/false,
27947 /*consume_paren=*/true);
27948 return list;
27951 /* OpenMP 3.0:
27952 untied */
27954 static tree
27955 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27956 tree list, location_t location)
27958 tree c;
27960 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27962 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27963 OMP_CLAUSE_CHAIN (c) = list;
27964 return c;
27967 /* OpenMP 4.0:
27968 inbranch
27969 notinbranch */
27971 static tree
27972 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27973 tree list, location_t location)
27975 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27976 tree c = build_omp_clause (location, code);
27977 OMP_CLAUSE_CHAIN (c) = list;
27978 return c;
27981 /* OpenMP 4.0:
27982 parallel
27984 sections
27985 taskgroup */
27987 static tree
27988 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27989 enum omp_clause_code code,
27990 tree list, location_t location)
27992 tree c = build_omp_clause (location, code);
27993 OMP_CLAUSE_CHAIN (c) = list;
27994 return c;
27997 /* OpenMP 4.0:
27998 num_teams ( expression ) */
28000 static tree
28001 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28002 location_t location)
28004 tree t, c;
28006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28007 return list;
28009 t = cp_parser_expression (parser, false, NULL);
28011 if (t == error_mark_node
28012 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28014 /*or_comma=*/false,
28015 /*consume_paren=*/true);
28017 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28018 "num_teams", location);
28020 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28021 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28022 OMP_CLAUSE_CHAIN (c) = list;
28024 return c;
28027 /* OpenMP 4.0:
28028 thread_limit ( expression ) */
28030 static tree
28031 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28032 location_t location)
28034 tree t, c;
28036 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28037 return list;
28039 t = cp_parser_expression (parser, false, NULL);
28041 if (t == error_mark_node
28042 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28043 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28044 /*or_comma=*/false,
28045 /*consume_paren=*/true);
28047 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28048 "thread_limit", location);
28050 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28051 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28052 OMP_CLAUSE_CHAIN (c) = list;
28054 return c;
28057 /* OpenMP 4.0:
28058 aligned ( variable-list )
28059 aligned ( variable-list : constant-expression ) */
28061 static tree
28062 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28064 tree nlist, c, alignment = NULL_TREE;
28065 bool colon;
28067 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28068 return list;
28070 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28071 &colon);
28073 if (colon)
28075 alignment = cp_parser_constant_expression (parser, false, NULL);
28077 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28079 /*or_comma=*/false,
28080 /*consume_paren=*/true);
28082 if (alignment == error_mark_node)
28083 alignment = NULL_TREE;
28086 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28087 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28089 return nlist;
28092 /* OpenMP 4.0:
28093 linear ( variable-list )
28094 linear ( variable-list : expression ) */
28096 static tree
28097 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28098 bool is_cilk_simd_fn)
28100 tree nlist, c, step = integer_one_node;
28101 bool colon;
28103 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28104 return list;
28106 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28107 &colon);
28109 if (colon)
28111 step = cp_parser_expression (parser, false, NULL);
28113 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28115 sorry ("using parameters for %<linear%> step is not supported yet");
28116 step = integer_one_node;
28118 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28119 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28120 /*or_comma=*/false,
28121 /*consume_paren=*/true);
28123 if (step == error_mark_node)
28124 return list;
28127 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28128 OMP_CLAUSE_LINEAR_STEP (c) = step;
28130 return nlist;
28133 /* OpenMP 4.0:
28134 safelen ( constant-expression ) */
28136 static tree
28137 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28138 location_t location)
28140 tree t, c;
28142 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28143 return list;
28145 t = cp_parser_constant_expression (parser, false, NULL);
28147 if (t == error_mark_node
28148 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28149 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28150 /*or_comma=*/false,
28151 /*consume_paren=*/true);
28153 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28155 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28156 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28157 OMP_CLAUSE_CHAIN (c) = list;
28159 return c;
28162 /* OpenMP 4.0:
28163 simdlen ( constant-expression ) */
28165 static tree
28166 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28167 location_t location)
28169 tree t, c;
28171 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28172 return list;
28174 t = cp_parser_constant_expression (parser, false, NULL);
28176 if (t == error_mark_node
28177 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28178 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28179 /*or_comma=*/false,
28180 /*consume_paren=*/true);
28182 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28184 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28185 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28186 OMP_CLAUSE_CHAIN (c) = list;
28188 return c;
28191 /* OpenMP 4.0:
28192 depend ( depend-kind : variable-list )
28194 depend-kind:
28195 in | out | inout */
28197 static tree
28198 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28200 tree nlist, c;
28201 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28203 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28204 return list;
28206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28208 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28209 const char *p = IDENTIFIER_POINTER (id);
28211 if (strcmp ("in", p) == 0)
28212 kind = OMP_CLAUSE_DEPEND_IN;
28213 else if (strcmp ("inout", p) == 0)
28214 kind = OMP_CLAUSE_DEPEND_INOUT;
28215 else if (strcmp ("out", p) == 0)
28216 kind = OMP_CLAUSE_DEPEND_OUT;
28217 else
28218 goto invalid_kind;
28220 else
28221 goto invalid_kind;
28223 cp_lexer_consume_token (parser->lexer);
28224 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28225 goto resync_fail;
28227 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28228 NULL);
28230 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28231 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28233 return nlist;
28235 invalid_kind:
28236 cp_parser_error (parser, "invalid depend kind");
28237 resync_fail:
28238 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28239 /*or_comma=*/false,
28240 /*consume_paren=*/true);
28241 return list;
28244 /* OpenMP 4.0:
28245 map ( map-kind : variable-list )
28246 map ( variable-list )
28248 map-kind:
28249 alloc | to | from | tofrom */
28251 static tree
28252 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28254 tree nlist, c;
28255 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28257 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28258 return list;
28260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28261 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28263 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28264 const char *p = IDENTIFIER_POINTER (id);
28266 if (strcmp ("alloc", p) == 0)
28267 kind = OMP_CLAUSE_MAP_ALLOC;
28268 else if (strcmp ("to", p) == 0)
28269 kind = OMP_CLAUSE_MAP_TO;
28270 else if (strcmp ("from", p) == 0)
28271 kind = OMP_CLAUSE_MAP_FROM;
28272 else if (strcmp ("tofrom", p) == 0)
28273 kind = OMP_CLAUSE_MAP_TOFROM;
28274 else
28276 cp_parser_error (parser, "invalid map kind");
28277 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28278 /*or_comma=*/false,
28279 /*consume_paren=*/true);
28280 return list;
28282 cp_lexer_consume_token (parser->lexer);
28283 cp_lexer_consume_token (parser->lexer);
28286 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28287 NULL);
28289 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28290 OMP_CLAUSE_MAP_KIND (c) = kind;
28292 return nlist;
28295 /* OpenMP 4.0:
28296 device ( expression ) */
28298 static tree
28299 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28300 location_t location)
28302 tree t, c;
28304 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28305 return list;
28307 t = cp_parser_expression (parser, false, NULL);
28309 if (t == error_mark_node
28310 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28311 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28312 /*or_comma=*/false,
28313 /*consume_paren=*/true);
28315 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28316 "device", location);
28318 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28319 OMP_CLAUSE_DEVICE_ID (c) = t;
28320 OMP_CLAUSE_CHAIN (c) = list;
28322 return c;
28325 /* OpenMP 4.0:
28326 dist_schedule ( static )
28327 dist_schedule ( static , expression ) */
28329 static tree
28330 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28331 location_t location)
28333 tree c, t;
28335 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28336 return list;
28338 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28340 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28341 goto invalid_kind;
28342 cp_lexer_consume_token (parser->lexer);
28344 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28346 cp_lexer_consume_token (parser->lexer);
28348 t = cp_parser_assignment_expression (parser, false, NULL);
28350 if (t == error_mark_node)
28351 goto resync_fail;
28352 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28354 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28355 goto resync_fail;
28357 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28358 goto resync_fail;
28360 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28361 location);
28362 OMP_CLAUSE_CHAIN (c) = list;
28363 return c;
28365 invalid_kind:
28366 cp_parser_error (parser, "invalid dist_schedule kind");
28367 resync_fail:
28368 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28369 /*or_comma=*/false,
28370 /*consume_paren=*/true);
28371 return list;
28374 /* OpenMP 4.0:
28375 proc_bind ( proc-bind-kind )
28377 proc-bind-kind:
28378 master | close | spread */
28380 static tree
28381 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28382 location_t location)
28384 tree c;
28385 enum omp_clause_proc_bind_kind kind;
28387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28388 return list;
28390 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28392 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28393 const char *p = IDENTIFIER_POINTER (id);
28395 if (strcmp ("master", p) == 0)
28396 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28397 else if (strcmp ("close", p) == 0)
28398 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28399 else if (strcmp ("spread", p) == 0)
28400 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28401 else
28402 goto invalid_kind;
28404 else
28405 goto invalid_kind;
28407 cp_lexer_consume_token (parser->lexer);
28408 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28409 goto resync_fail;
28411 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28412 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28413 location);
28414 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28415 OMP_CLAUSE_CHAIN (c) = list;
28416 return c;
28418 invalid_kind:
28419 cp_parser_error (parser, "invalid depend kind");
28420 resync_fail:
28421 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28422 /*or_comma=*/false,
28423 /*consume_paren=*/true);
28424 return list;
28427 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28428 is a bitmask in MASK. Return the list of clauses found; the result
28429 of clause default goes in *pdefault. */
28431 static tree
28432 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28433 const char *where, cp_token *pragma_tok,
28434 bool finish_p = true)
28436 tree clauses = NULL;
28437 bool first = true;
28438 cp_token *token = NULL;
28439 bool cilk_simd_fn = false;
28441 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28443 pragma_omp_clause c_kind;
28444 const char *c_name;
28445 tree prev = clauses;
28447 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28448 cp_lexer_consume_token (parser->lexer);
28450 token = cp_lexer_peek_token (parser->lexer);
28451 c_kind = cp_parser_omp_clause_name (parser);
28453 switch (c_kind)
28455 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28456 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28457 token->location);
28458 c_name = "collapse";
28459 break;
28460 case PRAGMA_OMP_CLAUSE_COPYIN:
28461 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28462 c_name = "copyin";
28463 break;
28464 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28465 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28466 clauses);
28467 c_name = "copyprivate";
28468 break;
28469 case PRAGMA_OMP_CLAUSE_DEFAULT:
28470 clauses = cp_parser_omp_clause_default (parser, clauses,
28471 token->location);
28472 c_name = "default";
28473 break;
28474 case PRAGMA_OMP_CLAUSE_FINAL:
28475 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28476 c_name = "final";
28477 break;
28478 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28479 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28480 clauses);
28481 c_name = "firstprivate";
28482 break;
28483 case PRAGMA_OMP_CLAUSE_IF:
28484 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28485 c_name = "if";
28486 break;
28487 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28488 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28489 clauses);
28490 c_name = "lastprivate";
28491 break;
28492 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28493 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28494 token->location);
28495 c_name = "mergeable";
28496 break;
28497 case PRAGMA_OMP_CLAUSE_NOWAIT:
28498 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28499 c_name = "nowait";
28500 break;
28501 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28502 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28503 token->location);
28504 c_name = "num_threads";
28505 break;
28506 case PRAGMA_OMP_CLAUSE_ORDERED:
28507 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28508 token->location);
28509 c_name = "ordered";
28510 break;
28511 case PRAGMA_OMP_CLAUSE_PRIVATE:
28512 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28513 clauses);
28514 c_name = "private";
28515 break;
28516 case PRAGMA_OMP_CLAUSE_REDUCTION:
28517 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28518 c_name = "reduction";
28519 break;
28520 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28521 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28522 token->location);
28523 c_name = "schedule";
28524 break;
28525 case PRAGMA_OMP_CLAUSE_SHARED:
28526 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28527 clauses);
28528 c_name = "shared";
28529 break;
28530 case PRAGMA_OMP_CLAUSE_UNTIED:
28531 clauses = cp_parser_omp_clause_untied (parser, clauses,
28532 token->location);
28533 c_name = "untied";
28534 break;
28535 case PRAGMA_OMP_CLAUSE_INBRANCH:
28536 case PRAGMA_CILK_CLAUSE_MASK:
28537 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28538 clauses, token->location);
28539 c_name = "inbranch";
28540 break;
28541 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28542 case PRAGMA_CILK_CLAUSE_NOMASK:
28543 clauses = cp_parser_omp_clause_branch (parser,
28544 OMP_CLAUSE_NOTINBRANCH,
28545 clauses, token->location);
28546 c_name = "notinbranch";
28547 break;
28548 case PRAGMA_OMP_CLAUSE_PARALLEL:
28549 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28550 clauses, token->location);
28551 c_name = "parallel";
28552 if (!first)
28554 clause_not_first:
28555 error_at (token->location, "%qs must be the first clause of %qs",
28556 c_name, where);
28557 clauses = prev;
28559 break;
28560 case PRAGMA_OMP_CLAUSE_FOR:
28561 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28562 clauses, token->location);
28563 c_name = "for";
28564 if (!first)
28565 goto clause_not_first;
28566 break;
28567 case PRAGMA_OMP_CLAUSE_SECTIONS:
28568 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28569 clauses, token->location);
28570 c_name = "sections";
28571 if (!first)
28572 goto clause_not_first;
28573 break;
28574 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28575 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28576 clauses, token->location);
28577 c_name = "taskgroup";
28578 if (!first)
28579 goto clause_not_first;
28580 break;
28581 case PRAGMA_OMP_CLAUSE_TO:
28582 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28583 clauses);
28584 c_name = "to";
28585 break;
28586 case PRAGMA_OMP_CLAUSE_FROM:
28587 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28588 clauses);
28589 c_name = "from";
28590 break;
28591 case PRAGMA_OMP_CLAUSE_UNIFORM:
28592 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28593 clauses);
28594 c_name = "uniform";
28595 break;
28596 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28597 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28598 token->location);
28599 c_name = "num_teams";
28600 break;
28601 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28602 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28603 token->location);
28604 c_name = "thread_limit";
28605 break;
28606 case PRAGMA_OMP_CLAUSE_ALIGNED:
28607 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28608 c_name = "aligned";
28609 break;
28610 case PRAGMA_OMP_CLAUSE_LINEAR:
28611 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28612 cilk_simd_fn = true;
28613 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28614 c_name = "linear";
28615 break;
28616 case PRAGMA_OMP_CLAUSE_DEPEND:
28617 clauses = cp_parser_omp_clause_depend (parser, clauses);
28618 c_name = "depend";
28619 break;
28620 case PRAGMA_OMP_CLAUSE_MAP:
28621 clauses = cp_parser_omp_clause_map (parser, clauses);
28622 c_name = "map";
28623 break;
28624 case PRAGMA_OMP_CLAUSE_DEVICE:
28625 clauses = cp_parser_omp_clause_device (parser, clauses,
28626 token->location);
28627 c_name = "device";
28628 break;
28629 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28630 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28631 token->location);
28632 c_name = "dist_schedule";
28633 break;
28634 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28635 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28636 token->location);
28637 c_name = "proc_bind";
28638 break;
28639 case PRAGMA_OMP_CLAUSE_SAFELEN:
28640 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28641 token->location);
28642 c_name = "safelen";
28643 break;
28644 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28645 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28646 token->location);
28647 c_name = "simdlen";
28648 break;
28649 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28650 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28651 c_name = "simdlen";
28652 break;
28653 default:
28654 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28655 goto saw_error;
28658 first = false;
28660 if (((mask >> c_kind) & 1) == 0)
28662 /* Remove the invalid clause(s) from the list to avoid
28663 confusing the rest of the compiler. */
28664 clauses = prev;
28665 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28668 saw_error:
28669 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28670 no reason to skip to the end. */
28671 if (!(flag_cilkplus && pragma_tok == NULL))
28672 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28673 if (finish_p)
28674 return finish_omp_clauses (clauses);
28675 return clauses;
28678 /* OpenMP 2.5:
28679 structured-block:
28680 statement
28682 In practice, we're also interested in adding the statement to an
28683 outer node. So it is convenient if we work around the fact that
28684 cp_parser_statement calls add_stmt. */
28686 static unsigned
28687 cp_parser_begin_omp_structured_block (cp_parser *parser)
28689 unsigned save = parser->in_statement;
28691 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28692 This preserves the "not within loop or switch" style error messages
28693 for nonsense cases like
28694 void foo() {
28695 #pragma omp single
28696 break;
28699 if (parser->in_statement)
28700 parser->in_statement = IN_OMP_BLOCK;
28702 return save;
28705 static void
28706 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28708 parser->in_statement = save;
28711 static tree
28712 cp_parser_omp_structured_block (cp_parser *parser)
28714 tree stmt = begin_omp_structured_block ();
28715 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28717 cp_parser_statement (parser, NULL_TREE, false, NULL);
28719 cp_parser_end_omp_structured_block (parser, save);
28720 return finish_omp_structured_block (stmt);
28723 /* OpenMP 2.5:
28724 # pragma omp atomic new-line
28725 expression-stmt
28727 expression-stmt:
28728 x binop= expr | x++ | ++x | x-- | --x
28729 binop:
28730 +, *, -, /, &, ^, |, <<, >>
28732 where x is an lvalue expression with scalar type.
28734 OpenMP 3.1:
28735 # pragma omp atomic new-line
28736 update-stmt
28738 # pragma omp atomic read new-line
28739 read-stmt
28741 # pragma omp atomic write new-line
28742 write-stmt
28744 # pragma omp atomic update new-line
28745 update-stmt
28747 # pragma omp atomic capture new-line
28748 capture-stmt
28750 # pragma omp atomic capture new-line
28751 capture-block
28753 read-stmt:
28754 v = x
28755 write-stmt:
28756 x = expr
28757 update-stmt:
28758 expression-stmt | x = x binop expr
28759 capture-stmt:
28760 v = expression-stmt
28761 capture-block:
28762 { v = x; update-stmt; } | { update-stmt; v = x; }
28764 OpenMP 4.0:
28765 update-stmt:
28766 expression-stmt | x = x binop expr | x = expr binop x
28767 capture-stmt:
28768 v = update-stmt
28769 capture-block:
28770 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28772 where x and v are lvalue expressions with scalar type. */
28774 static void
28775 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28777 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28778 tree rhs1 = NULL_TREE, orig_lhs;
28779 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28780 bool structured_block = false;
28781 bool seq_cst = false;
28783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28785 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28786 const char *p = IDENTIFIER_POINTER (id);
28788 if (!strcmp (p, "seq_cst"))
28790 seq_cst = true;
28791 cp_lexer_consume_token (parser->lexer);
28792 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28793 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28794 cp_lexer_consume_token (parser->lexer);
28797 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28800 const char *p = IDENTIFIER_POINTER (id);
28802 if (!strcmp (p, "read"))
28803 code = OMP_ATOMIC_READ;
28804 else if (!strcmp (p, "write"))
28805 code = NOP_EXPR;
28806 else if (!strcmp (p, "update"))
28807 code = OMP_ATOMIC;
28808 else if (!strcmp (p, "capture"))
28809 code = OMP_ATOMIC_CAPTURE_NEW;
28810 else
28811 p = NULL;
28812 if (p)
28813 cp_lexer_consume_token (parser->lexer);
28815 if (!seq_cst)
28817 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28818 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28819 cp_lexer_consume_token (parser->lexer);
28821 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28823 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28824 const char *p = IDENTIFIER_POINTER (id);
28826 if (!strcmp (p, "seq_cst"))
28828 seq_cst = true;
28829 cp_lexer_consume_token (parser->lexer);
28833 cp_parser_require_pragma_eol (parser, pragma_tok);
28835 switch (code)
28837 case OMP_ATOMIC_READ:
28838 case NOP_EXPR: /* atomic write */
28839 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28840 /*cast_p=*/false, NULL);
28841 if (v == error_mark_node)
28842 goto saw_error;
28843 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28844 goto saw_error;
28845 if (code == NOP_EXPR)
28846 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28847 else
28848 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28849 /*cast_p=*/false, NULL);
28850 if (lhs == error_mark_node)
28851 goto saw_error;
28852 if (code == NOP_EXPR)
28854 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28855 opcode. */
28856 code = OMP_ATOMIC;
28857 rhs = lhs;
28858 lhs = v;
28859 v = NULL_TREE;
28861 goto done;
28862 case OMP_ATOMIC_CAPTURE_NEW:
28863 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28865 cp_lexer_consume_token (parser->lexer);
28866 structured_block = true;
28868 else
28870 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28871 /*cast_p=*/false, NULL);
28872 if (v == error_mark_node)
28873 goto saw_error;
28874 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28875 goto saw_error;
28877 default:
28878 break;
28881 restart:
28882 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28883 /*cast_p=*/false, NULL);
28884 orig_lhs = lhs;
28885 switch (TREE_CODE (lhs))
28887 case ERROR_MARK:
28888 goto saw_error;
28890 case POSTINCREMENT_EXPR:
28891 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28892 code = OMP_ATOMIC_CAPTURE_OLD;
28893 /* FALLTHROUGH */
28894 case PREINCREMENT_EXPR:
28895 lhs = TREE_OPERAND (lhs, 0);
28896 opcode = PLUS_EXPR;
28897 rhs = integer_one_node;
28898 break;
28900 case POSTDECREMENT_EXPR:
28901 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28902 code = OMP_ATOMIC_CAPTURE_OLD;
28903 /* FALLTHROUGH */
28904 case PREDECREMENT_EXPR:
28905 lhs = TREE_OPERAND (lhs, 0);
28906 opcode = MINUS_EXPR;
28907 rhs = integer_one_node;
28908 break;
28910 case COMPOUND_EXPR:
28911 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28912 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28913 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28914 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28915 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28916 (TREE_OPERAND (lhs, 1), 0), 0)))
28917 == BOOLEAN_TYPE)
28918 /* Undo effects of boolean_increment for post {in,de}crement. */
28919 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28920 /* FALLTHRU */
28921 case MODIFY_EXPR:
28922 if (TREE_CODE (lhs) == MODIFY_EXPR
28923 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28925 /* Undo effects of boolean_increment. */
28926 if (integer_onep (TREE_OPERAND (lhs, 1)))
28928 /* This is pre or post increment. */
28929 rhs = TREE_OPERAND (lhs, 1);
28930 lhs = TREE_OPERAND (lhs, 0);
28931 opcode = NOP_EXPR;
28932 if (code == OMP_ATOMIC_CAPTURE_NEW
28933 && !structured_block
28934 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28935 code = OMP_ATOMIC_CAPTURE_OLD;
28936 break;
28939 /* FALLTHRU */
28940 default:
28941 switch (cp_lexer_peek_token (parser->lexer)->type)
28943 case CPP_MULT_EQ:
28944 opcode = MULT_EXPR;
28945 break;
28946 case CPP_DIV_EQ:
28947 opcode = TRUNC_DIV_EXPR;
28948 break;
28949 case CPP_PLUS_EQ:
28950 opcode = PLUS_EXPR;
28951 break;
28952 case CPP_MINUS_EQ:
28953 opcode = MINUS_EXPR;
28954 break;
28955 case CPP_LSHIFT_EQ:
28956 opcode = LSHIFT_EXPR;
28957 break;
28958 case CPP_RSHIFT_EQ:
28959 opcode = RSHIFT_EXPR;
28960 break;
28961 case CPP_AND_EQ:
28962 opcode = BIT_AND_EXPR;
28963 break;
28964 case CPP_OR_EQ:
28965 opcode = BIT_IOR_EXPR;
28966 break;
28967 case CPP_XOR_EQ:
28968 opcode = BIT_XOR_EXPR;
28969 break;
28970 case CPP_EQ:
28971 enum cp_parser_prec oprec;
28972 cp_token *token;
28973 cp_lexer_consume_token (parser->lexer);
28974 cp_parser_parse_tentatively (parser);
28975 rhs1 = cp_parser_simple_cast_expression (parser);
28976 if (rhs1 == error_mark_node)
28978 cp_parser_abort_tentative_parse (parser);
28979 cp_parser_simple_cast_expression (parser);
28980 goto saw_error;
28982 token = cp_lexer_peek_token (parser->lexer);
28983 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28985 cp_parser_abort_tentative_parse (parser);
28986 cp_parser_parse_tentatively (parser);
28987 rhs = cp_parser_binary_expression (parser, false, true,
28988 PREC_NOT_OPERATOR, NULL);
28989 if (rhs == error_mark_node)
28991 cp_parser_abort_tentative_parse (parser);
28992 cp_parser_binary_expression (parser, false, true,
28993 PREC_NOT_OPERATOR, NULL);
28994 goto saw_error;
28996 switch (TREE_CODE (rhs))
28998 case MULT_EXPR:
28999 case TRUNC_DIV_EXPR:
29000 case PLUS_EXPR:
29001 case MINUS_EXPR:
29002 case LSHIFT_EXPR:
29003 case RSHIFT_EXPR:
29004 case BIT_AND_EXPR:
29005 case BIT_IOR_EXPR:
29006 case BIT_XOR_EXPR:
29007 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29009 if (cp_parser_parse_definitely (parser))
29011 opcode = TREE_CODE (rhs);
29012 rhs1 = TREE_OPERAND (rhs, 0);
29013 rhs = TREE_OPERAND (rhs, 1);
29014 goto stmt_done;
29016 else
29017 goto saw_error;
29019 break;
29020 default:
29021 break;
29023 cp_parser_abort_tentative_parse (parser);
29024 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29026 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
29027 if (rhs == error_mark_node)
29028 goto saw_error;
29029 opcode = NOP_EXPR;
29030 rhs1 = NULL_TREE;
29031 goto stmt_done;
29033 cp_parser_error (parser,
29034 "invalid form of %<#pragma omp atomic%>");
29035 goto saw_error;
29037 if (!cp_parser_parse_definitely (parser))
29038 goto saw_error;
29039 switch (token->type)
29041 case CPP_SEMICOLON:
29042 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29044 code = OMP_ATOMIC_CAPTURE_OLD;
29045 v = lhs;
29046 lhs = NULL_TREE;
29047 lhs1 = rhs1;
29048 rhs1 = NULL_TREE;
29049 cp_lexer_consume_token (parser->lexer);
29050 goto restart;
29052 else if (structured_block)
29054 opcode = NOP_EXPR;
29055 rhs = rhs1;
29056 rhs1 = NULL_TREE;
29057 goto stmt_done;
29059 cp_parser_error (parser,
29060 "invalid form of %<#pragma omp atomic%>");
29061 goto saw_error;
29062 case CPP_MULT:
29063 opcode = MULT_EXPR;
29064 break;
29065 case CPP_DIV:
29066 opcode = TRUNC_DIV_EXPR;
29067 break;
29068 case CPP_PLUS:
29069 opcode = PLUS_EXPR;
29070 break;
29071 case CPP_MINUS:
29072 opcode = MINUS_EXPR;
29073 break;
29074 case CPP_LSHIFT:
29075 opcode = LSHIFT_EXPR;
29076 break;
29077 case CPP_RSHIFT:
29078 opcode = RSHIFT_EXPR;
29079 break;
29080 case CPP_AND:
29081 opcode = BIT_AND_EXPR;
29082 break;
29083 case CPP_OR:
29084 opcode = BIT_IOR_EXPR;
29085 break;
29086 case CPP_XOR:
29087 opcode = BIT_XOR_EXPR;
29088 break;
29089 default:
29090 cp_parser_error (parser,
29091 "invalid operator for %<#pragma omp atomic%>");
29092 goto saw_error;
29094 oprec = TOKEN_PRECEDENCE (token);
29095 gcc_assert (oprec != PREC_NOT_OPERATOR);
29096 if (commutative_tree_code (opcode))
29097 oprec = (enum cp_parser_prec) (oprec - 1);
29098 cp_lexer_consume_token (parser->lexer);
29099 rhs = cp_parser_binary_expression (parser, false, false,
29100 oprec, NULL);
29101 if (rhs == error_mark_node)
29102 goto saw_error;
29103 goto stmt_done;
29104 /* FALLTHROUGH */
29105 default:
29106 cp_parser_error (parser,
29107 "invalid operator for %<#pragma omp atomic%>");
29108 goto saw_error;
29110 cp_lexer_consume_token (parser->lexer);
29112 rhs = cp_parser_expression (parser, false, NULL);
29113 if (rhs == error_mark_node)
29114 goto saw_error;
29115 break;
29117 stmt_done:
29118 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29120 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29121 goto saw_error;
29122 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29123 /*cast_p=*/false, NULL);
29124 if (v == error_mark_node)
29125 goto saw_error;
29126 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29127 goto saw_error;
29128 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29129 /*cast_p=*/false, NULL);
29130 if (lhs1 == error_mark_node)
29131 goto saw_error;
29133 if (structured_block)
29135 cp_parser_consume_semicolon_at_end_of_statement (parser);
29136 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29138 done:
29139 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29140 if (!structured_block)
29141 cp_parser_consume_semicolon_at_end_of_statement (parser);
29142 return;
29144 saw_error:
29145 cp_parser_skip_to_end_of_block_or_statement (parser);
29146 if (structured_block)
29148 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29149 cp_lexer_consume_token (parser->lexer);
29150 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29152 cp_parser_skip_to_end_of_block_or_statement (parser);
29153 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29154 cp_lexer_consume_token (parser->lexer);
29160 /* OpenMP 2.5:
29161 # pragma omp barrier new-line */
29163 static void
29164 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29166 cp_parser_require_pragma_eol (parser, pragma_tok);
29167 finish_omp_barrier ();
29170 /* OpenMP 2.5:
29171 # pragma omp critical [(name)] new-line
29172 structured-block */
29174 static tree
29175 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29177 tree stmt, name = NULL;
29179 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29181 cp_lexer_consume_token (parser->lexer);
29183 name = cp_parser_identifier (parser);
29185 if (name == error_mark_node
29186 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29187 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29188 /*or_comma=*/false,
29189 /*consume_paren=*/true);
29190 if (name == error_mark_node)
29191 name = NULL;
29193 cp_parser_require_pragma_eol (parser, pragma_tok);
29195 stmt = cp_parser_omp_structured_block (parser);
29196 return c_finish_omp_critical (input_location, stmt, name);
29199 /* OpenMP 2.5:
29200 # pragma omp flush flush-vars[opt] new-line
29202 flush-vars:
29203 ( variable-list ) */
29205 static void
29206 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29209 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29210 cp_parser_require_pragma_eol (parser, pragma_tok);
29212 finish_omp_flush ();
29215 /* Helper function, to parse omp for increment expression. */
29217 static tree
29218 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29220 tree cond = cp_parser_binary_expression (parser, false, true,
29221 PREC_NOT_OPERATOR, NULL);
29222 if (cond == error_mark_node
29223 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29225 cp_parser_skip_to_end_of_statement (parser);
29226 return error_mark_node;
29229 switch (TREE_CODE (cond))
29231 case GT_EXPR:
29232 case GE_EXPR:
29233 case LT_EXPR:
29234 case LE_EXPR:
29235 break;
29236 case NE_EXPR:
29237 if (code == CILK_SIMD)
29238 break;
29239 /* Fall through: OpenMP disallows NE_EXPR. */
29240 default:
29241 return error_mark_node;
29244 /* If decl is an iterator, preserve LHS and RHS of the relational
29245 expr until finish_omp_for. */
29246 if (decl
29247 && (type_dependent_expression_p (decl)
29248 || CLASS_TYPE_P (TREE_TYPE (decl))))
29249 return cond;
29251 return build_x_binary_op (input_location, TREE_CODE (cond),
29252 TREE_OPERAND (cond, 0), ERROR_MARK,
29253 TREE_OPERAND (cond, 1), ERROR_MARK,
29254 /*overload=*/NULL, tf_warning_or_error);
29257 /* Helper function, to parse omp for increment expression. */
29259 static tree
29260 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29262 cp_token *token = cp_lexer_peek_token (parser->lexer);
29263 enum tree_code op;
29264 tree lhs, rhs;
29265 cp_id_kind idk;
29266 bool decl_first;
29268 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29270 op = (token->type == CPP_PLUS_PLUS
29271 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29272 cp_lexer_consume_token (parser->lexer);
29273 lhs = cp_parser_simple_cast_expression (parser);
29274 if (lhs != decl)
29275 return error_mark_node;
29276 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29279 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29280 if (lhs != decl)
29281 return error_mark_node;
29283 token = cp_lexer_peek_token (parser->lexer);
29284 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29286 op = (token->type == CPP_PLUS_PLUS
29287 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29288 cp_lexer_consume_token (parser->lexer);
29289 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29292 op = cp_parser_assignment_operator_opt (parser);
29293 if (op == ERROR_MARK)
29294 return error_mark_node;
29296 if (op != NOP_EXPR)
29298 rhs = cp_parser_assignment_expression (parser, false, NULL);
29299 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29300 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29303 lhs = cp_parser_binary_expression (parser, false, false,
29304 PREC_ADDITIVE_EXPRESSION, NULL);
29305 token = cp_lexer_peek_token (parser->lexer);
29306 decl_first = lhs == decl;
29307 if (decl_first)
29308 lhs = NULL_TREE;
29309 if (token->type != CPP_PLUS
29310 && token->type != CPP_MINUS)
29311 return error_mark_node;
29315 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29316 cp_lexer_consume_token (parser->lexer);
29317 rhs = cp_parser_binary_expression (parser, false, false,
29318 PREC_ADDITIVE_EXPRESSION, NULL);
29319 token = cp_lexer_peek_token (parser->lexer);
29320 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29322 if (lhs == NULL_TREE)
29324 if (op == PLUS_EXPR)
29325 lhs = rhs;
29326 else
29327 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29328 tf_warning_or_error);
29330 else
29331 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29332 ERROR_MARK, NULL, tf_warning_or_error);
29335 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29337 if (!decl_first)
29339 if (rhs != decl || op == MINUS_EXPR)
29340 return error_mark_node;
29341 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29343 else
29344 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29346 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29349 /* Parse the initialization statement of either an OpenMP for loop or
29350 a Cilk Plus for loop.
29352 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29353 Plus.
29355 Return true if the resulting construct should have an
29356 OMP_CLAUSE_PRIVATE added to it. */
29358 static bool
29359 cp_parser_omp_for_loop_init (cp_parser *parser,
29360 bool parsing_openmp,
29361 tree &this_pre_body,
29362 vec<tree, va_gc> *for_block,
29363 tree &init,
29364 tree &decl,
29365 tree &real_decl)
29367 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29368 return false;
29370 bool add_private_clause = false;
29372 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29374 init-expr:
29375 var = lb
29376 integer-type var = lb
29377 random-access-iterator-type var = lb
29378 pointer-type var = lb
29380 cp_decl_specifier_seq type_specifiers;
29382 /* First, try to parse as an initialized declaration. See
29383 cp_parser_condition, from whence the bulk of this is copied. */
29385 cp_parser_parse_tentatively (parser);
29386 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29387 /*is_trailing_return=*/false,
29388 &type_specifiers);
29389 if (cp_parser_parse_definitely (parser))
29391 /* If parsing a type specifier seq succeeded, then this
29392 MUST be a initialized declaration. */
29393 tree asm_specification, attributes;
29394 cp_declarator *declarator;
29396 declarator = cp_parser_declarator (parser,
29397 CP_PARSER_DECLARATOR_NAMED,
29398 /*ctor_dtor_or_conv_p=*/NULL,
29399 /*parenthesized_p=*/NULL,
29400 /*member_p=*/false,
29401 /*friend_p=*/false);
29402 attributes = cp_parser_attributes_opt (parser);
29403 asm_specification = cp_parser_asm_specification_opt (parser);
29405 if (declarator == cp_error_declarator)
29406 cp_parser_skip_to_end_of_statement (parser);
29408 else
29410 tree pushed_scope, auto_node;
29412 decl = start_decl (declarator, &type_specifiers,
29413 SD_INITIALIZED, attributes,
29414 /*prefix_attributes=*/NULL_TREE,
29415 &pushed_scope);
29417 auto_node = type_uses_auto (TREE_TYPE (decl));
29418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29420 if (cp_lexer_next_token_is (parser->lexer,
29421 CPP_OPEN_PAREN))
29423 if (parsing_openmp)
29424 error ("parenthesized initialization is not allowed in "
29425 "OpenMP %<for%> loop");
29426 else
29427 error ("parenthesized initialization is "
29428 "not allowed in for-loop");
29430 else
29431 /* Trigger an error. */
29432 cp_parser_require (parser, CPP_EQ, RT_EQ);
29434 init = error_mark_node;
29435 cp_parser_skip_to_end_of_statement (parser);
29437 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29438 || type_dependent_expression_p (decl)
29439 || auto_node)
29441 bool is_direct_init, is_non_constant_init;
29443 init = cp_parser_initializer (parser,
29444 &is_direct_init,
29445 &is_non_constant_init);
29447 if (auto_node)
29449 TREE_TYPE (decl)
29450 = do_auto_deduction (TREE_TYPE (decl), init,
29451 auto_node);
29453 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29454 && !type_dependent_expression_p (decl))
29455 goto non_class;
29458 cp_finish_decl (decl, init, !is_non_constant_init,
29459 asm_specification,
29460 LOOKUP_ONLYCONVERTING);
29461 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29463 vec_safe_push (for_block, this_pre_body);
29464 init = NULL_TREE;
29466 else
29467 init = pop_stmt_list (this_pre_body);
29468 this_pre_body = NULL_TREE;
29470 else
29472 /* Consume '='. */
29473 cp_lexer_consume_token (parser->lexer);
29474 init = cp_parser_assignment_expression (parser, false, NULL);
29476 non_class:
29477 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29478 init = error_mark_node;
29479 else
29480 cp_finish_decl (decl, NULL_TREE,
29481 /*init_const_expr_p=*/false,
29482 asm_specification,
29483 LOOKUP_ONLYCONVERTING);
29486 if (pushed_scope)
29487 pop_scope (pushed_scope);
29490 else
29492 cp_id_kind idk;
29493 /* If parsing a type specifier sequence failed, then
29494 this MUST be a simple expression. */
29495 cp_parser_parse_tentatively (parser);
29496 decl = cp_parser_primary_expression (parser, false, false,
29497 false, &idk);
29498 if (!cp_parser_error_occurred (parser)
29499 && decl
29500 && DECL_P (decl)
29501 && CLASS_TYPE_P (TREE_TYPE (decl)))
29503 tree rhs;
29505 cp_parser_parse_definitely (parser);
29506 cp_parser_require (parser, CPP_EQ, RT_EQ);
29507 rhs = cp_parser_assignment_expression (parser, false, NULL);
29508 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29509 decl, NOP_EXPR,
29510 rhs,
29511 tf_warning_or_error));
29512 add_private_clause = true;
29514 else
29516 decl = NULL;
29517 cp_parser_abort_tentative_parse (parser);
29518 init = cp_parser_expression (parser, false, NULL);
29519 if (init)
29521 if (TREE_CODE (init) == MODIFY_EXPR
29522 || TREE_CODE (init) == MODOP_EXPR)
29523 real_decl = TREE_OPERAND (init, 0);
29527 return add_private_clause;
29530 /* Parse the restricted form of the for statement allowed by OpenMP. */
29532 static tree
29533 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29534 tree *cclauses)
29536 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29537 tree real_decl, initv, condv, incrv, declv;
29538 tree this_pre_body, cl;
29539 location_t loc_first;
29540 bool collapse_err = false;
29541 int i, collapse = 1, nbraces = 0;
29542 vec<tree, va_gc> *for_block = make_tree_vector ();
29544 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29545 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29546 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29548 gcc_assert (collapse >= 1);
29550 declv = make_tree_vec (collapse);
29551 initv = make_tree_vec (collapse);
29552 condv = make_tree_vec (collapse);
29553 incrv = make_tree_vec (collapse);
29555 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29557 for (i = 0; i < collapse; i++)
29559 int bracecount = 0;
29560 bool add_private_clause = false;
29561 location_t loc;
29563 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29565 cp_parser_error (parser, "for statement expected");
29566 return NULL;
29568 loc = cp_lexer_consume_token (parser->lexer)->location;
29570 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29571 return NULL;
29573 init = decl = real_decl = NULL;
29574 this_pre_body = push_stmt_list ();
29576 add_private_clause
29577 |= cp_parser_omp_for_loop_init (parser,
29578 /*parsing_openmp=*/code != CILK_SIMD,
29579 this_pre_body, for_block,
29580 init, decl, real_decl);
29582 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29583 if (this_pre_body)
29585 this_pre_body = pop_stmt_list (this_pre_body);
29586 if (pre_body)
29588 tree t = pre_body;
29589 pre_body = push_stmt_list ();
29590 add_stmt (t);
29591 add_stmt (this_pre_body);
29592 pre_body = pop_stmt_list (pre_body);
29594 else
29595 pre_body = this_pre_body;
29598 if (decl)
29599 real_decl = decl;
29600 if (cclauses != NULL
29601 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29602 && real_decl != NULL_TREE)
29604 tree *c;
29605 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29606 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29607 && OMP_CLAUSE_DECL (*c) == real_decl)
29609 error_at (loc, "iteration variable %qD"
29610 " should not be firstprivate", real_decl);
29611 *c = OMP_CLAUSE_CHAIN (*c);
29613 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29614 && OMP_CLAUSE_DECL (*c) == real_decl)
29616 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29617 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29618 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29619 OMP_CLAUSE_DECL (l) = real_decl;
29620 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29621 if (code == OMP_SIMD)
29623 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29624 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29626 else
29628 OMP_CLAUSE_CHAIN (l) = clauses;
29629 clauses = l;
29631 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29632 CP_OMP_CLAUSE_INFO (*c) = NULL;
29633 add_private_clause = false;
29635 else
29637 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29638 && OMP_CLAUSE_DECL (*c) == real_decl)
29639 add_private_clause = false;
29640 c = &OMP_CLAUSE_CHAIN (*c);
29644 if (add_private_clause)
29646 tree c;
29647 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29649 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29650 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29651 && OMP_CLAUSE_DECL (c) == decl)
29652 break;
29653 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29654 && OMP_CLAUSE_DECL (c) == decl)
29655 error_at (loc, "iteration variable %qD "
29656 "should not be firstprivate",
29657 decl);
29658 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29659 && OMP_CLAUSE_DECL (c) == decl)
29660 error_at (loc, "iteration variable %qD should not be reduction",
29661 decl);
29663 if (c == NULL)
29665 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29666 OMP_CLAUSE_DECL (c) = decl;
29667 c = finish_omp_clauses (c);
29668 if (c)
29670 OMP_CLAUSE_CHAIN (c) = clauses;
29671 clauses = c;
29676 cond = NULL;
29677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29678 cond = cp_parser_omp_for_cond (parser, decl, code);
29679 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29681 incr = NULL;
29682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29684 /* If decl is an iterator, preserve the operator on decl
29685 until finish_omp_for. */
29686 if (real_decl
29687 && ((processing_template_decl
29688 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29689 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29690 incr = cp_parser_omp_for_incr (parser, real_decl);
29691 else
29692 incr = cp_parser_expression (parser, false, NULL);
29693 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29694 SET_EXPR_LOCATION (incr, input_location);
29697 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29698 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29699 /*or_comma=*/false,
29700 /*consume_paren=*/true);
29702 TREE_VEC_ELT (declv, i) = decl;
29703 TREE_VEC_ELT (initv, i) = init;
29704 TREE_VEC_ELT (condv, i) = cond;
29705 TREE_VEC_ELT (incrv, i) = incr;
29707 if (i == collapse - 1)
29708 break;
29710 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29711 in between the collapsed for loops to be still considered perfectly
29712 nested. Hopefully the final version clarifies this.
29713 For now handle (multiple) {'s and empty statements. */
29714 cp_parser_parse_tentatively (parser);
29717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29718 break;
29719 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29721 cp_lexer_consume_token (parser->lexer);
29722 bracecount++;
29724 else if (bracecount
29725 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29726 cp_lexer_consume_token (parser->lexer);
29727 else
29729 loc = cp_lexer_peek_token (parser->lexer)->location;
29730 error_at (loc, "not enough collapsed for loops");
29731 collapse_err = true;
29732 cp_parser_abort_tentative_parse (parser);
29733 declv = NULL_TREE;
29734 break;
29737 while (1);
29739 if (declv)
29741 cp_parser_parse_definitely (parser);
29742 nbraces += bracecount;
29746 /* Note that we saved the original contents of this flag when we entered
29747 the structured block, and so we don't need to re-save it here. */
29748 if (code == CILK_SIMD)
29749 parser->in_statement = IN_CILK_SIMD_FOR;
29750 else
29751 parser->in_statement = IN_OMP_FOR;
29753 /* Note that the grammar doesn't call for a structured block here,
29754 though the loop as a whole is a structured block. */
29755 body = push_stmt_list ();
29756 cp_parser_statement (parser, NULL_TREE, false, NULL);
29757 body = pop_stmt_list (body);
29759 if (declv == NULL_TREE)
29760 ret = NULL_TREE;
29761 else
29762 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29763 pre_body, clauses);
29765 while (nbraces)
29767 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29769 cp_lexer_consume_token (parser->lexer);
29770 nbraces--;
29772 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29773 cp_lexer_consume_token (parser->lexer);
29774 else
29776 if (!collapse_err)
29778 error_at (cp_lexer_peek_token (parser->lexer)->location,
29779 "collapsed loops not perfectly nested");
29781 collapse_err = true;
29782 cp_parser_statement_seq_opt (parser, NULL);
29783 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29784 break;
29788 while (!for_block->is_empty ())
29789 add_stmt (pop_stmt_list (for_block->pop ()));
29790 release_tree_vector (for_block);
29792 return ret;
29795 /* Helper function for OpenMP parsing, split clauses and call
29796 finish_omp_clauses on each of the set of clauses afterwards. */
29798 static void
29799 cp_omp_split_clauses (location_t loc, enum tree_code code,
29800 omp_clause_mask mask, tree clauses, tree *cclauses)
29802 int i;
29803 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29804 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29805 if (cclauses[i])
29806 cclauses[i] = finish_omp_clauses (cclauses[i]);
29809 /* OpenMP 4.0:
29810 #pragma omp simd simd-clause[optseq] new-line
29811 for-loop */
29813 #define OMP_SIMD_CLAUSE_MASK \
29814 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29822 static tree
29823 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29824 char *p_name, omp_clause_mask mask, tree *cclauses)
29826 tree clauses, sb, ret;
29827 unsigned int save;
29828 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29830 strcat (p_name, " simd");
29831 mask |= OMP_SIMD_CLAUSE_MASK;
29832 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29834 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29835 cclauses == NULL);
29836 if (cclauses)
29838 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29839 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29842 sb = begin_omp_structured_block ();
29843 save = cp_parser_begin_omp_structured_block (parser);
29845 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29847 cp_parser_end_omp_structured_block (parser, save);
29848 add_stmt (finish_omp_structured_block (sb));
29850 return ret;
29853 /* OpenMP 2.5:
29854 #pragma omp for for-clause[optseq] new-line
29855 for-loop
29857 OpenMP 4.0:
29858 #pragma omp for simd for-simd-clause[optseq] new-line
29859 for-loop */
29861 #define OMP_FOR_CLAUSE_MASK \
29862 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29871 static tree
29872 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29873 char *p_name, omp_clause_mask mask, tree *cclauses)
29875 tree clauses, sb, ret;
29876 unsigned int save;
29877 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29879 strcat (p_name, " for");
29880 mask |= OMP_FOR_CLAUSE_MASK;
29881 if (cclauses)
29882 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29884 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29886 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29887 const char *p = IDENTIFIER_POINTER (id);
29889 if (strcmp (p, "simd") == 0)
29891 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29892 if (cclauses == NULL)
29893 cclauses = cclauses_buf;
29895 cp_lexer_consume_token (parser->lexer);
29896 if (!flag_openmp) /* flag_openmp_simd */
29897 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29898 cclauses);
29899 sb = begin_omp_structured_block ();
29900 save = cp_parser_begin_omp_structured_block (parser);
29901 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29902 cclauses);
29903 cp_parser_end_omp_structured_block (parser, save);
29904 tree body = finish_omp_structured_block (sb);
29905 if (ret == NULL)
29906 return ret;
29907 ret = make_node (OMP_FOR);
29908 TREE_TYPE (ret) = void_type_node;
29909 OMP_FOR_BODY (ret) = body;
29910 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29911 SET_EXPR_LOCATION (ret, loc);
29912 add_stmt (ret);
29913 return ret;
29916 if (!flag_openmp) /* flag_openmp_simd */
29918 cp_parser_require_pragma_eol (parser, pragma_tok);
29919 return NULL_TREE;
29922 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29923 cclauses == NULL);
29924 if (cclauses)
29926 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29927 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29930 sb = begin_omp_structured_block ();
29931 save = cp_parser_begin_omp_structured_block (parser);
29933 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29935 cp_parser_end_omp_structured_block (parser, save);
29936 add_stmt (finish_omp_structured_block (sb));
29938 return ret;
29941 /* OpenMP 2.5:
29942 # pragma omp master new-line
29943 structured-block */
29945 static tree
29946 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29948 cp_parser_require_pragma_eol (parser, pragma_tok);
29949 return c_finish_omp_master (input_location,
29950 cp_parser_omp_structured_block (parser));
29953 /* OpenMP 2.5:
29954 # pragma omp ordered new-line
29955 structured-block */
29957 static tree
29958 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29960 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29961 cp_parser_require_pragma_eol (parser, pragma_tok);
29962 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29965 /* OpenMP 2.5:
29967 section-scope:
29968 { section-sequence }
29970 section-sequence:
29971 section-directive[opt] structured-block
29972 section-sequence section-directive structured-block */
29974 static tree
29975 cp_parser_omp_sections_scope (cp_parser *parser)
29977 tree stmt, substmt;
29978 bool error_suppress = false;
29979 cp_token *tok;
29981 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29982 return NULL_TREE;
29984 stmt = push_stmt_list ();
29986 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29988 substmt = cp_parser_omp_structured_block (parser);
29989 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29990 add_stmt (substmt);
29993 while (1)
29995 tok = cp_lexer_peek_token (parser->lexer);
29996 if (tok->type == CPP_CLOSE_BRACE)
29997 break;
29998 if (tok->type == CPP_EOF)
29999 break;
30001 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30003 cp_lexer_consume_token (parser->lexer);
30004 cp_parser_require_pragma_eol (parser, tok);
30005 error_suppress = false;
30007 else if (!error_suppress)
30009 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30010 error_suppress = true;
30013 substmt = cp_parser_omp_structured_block (parser);
30014 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30015 add_stmt (substmt);
30017 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30019 substmt = pop_stmt_list (stmt);
30021 stmt = make_node (OMP_SECTIONS);
30022 TREE_TYPE (stmt) = void_type_node;
30023 OMP_SECTIONS_BODY (stmt) = substmt;
30025 add_stmt (stmt);
30026 return stmt;
30029 /* OpenMP 2.5:
30030 # pragma omp sections sections-clause[optseq] newline
30031 sections-scope */
30033 #define OMP_SECTIONS_CLAUSE_MASK \
30034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30040 static tree
30041 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30042 char *p_name, omp_clause_mask mask, tree *cclauses)
30044 tree clauses, ret;
30045 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30047 strcat (p_name, " sections");
30048 mask |= OMP_SECTIONS_CLAUSE_MASK;
30049 if (cclauses)
30050 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30052 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30053 cclauses == NULL);
30054 if (cclauses)
30056 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30057 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30060 ret = cp_parser_omp_sections_scope (parser);
30061 if (ret)
30062 OMP_SECTIONS_CLAUSES (ret) = clauses;
30064 return ret;
30067 /* OpenMP 2.5:
30068 # pragma omp parallel parallel-clause[optseq] new-line
30069 structured-block
30070 # pragma omp parallel for parallel-for-clause[optseq] new-line
30071 structured-block
30072 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30073 structured-block
30075 OpenMP 4.0:
30076 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30077 structured-block */
30079 #define OMP_PARALLEL_CLAUSE_MASK \
30080 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30090 static tree
30091 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30092 char *p_name, omp_clause_mask mask, tree *cclauses)
30094 tree stmt, clauses, block;
30095 unsigned int save;
30096 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30098 strcat (p_name, " parallel");
30099 mask |= OMP_PARALLEL_CLAUSE_MASK;
30101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30103 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30104 if (cclauses == NULL)
30105 cclauses = cclauses_buf;
30107 cp_lexer_consume_token (parser->lexer);
30108 if (!flag_openmp) /* flag_openmp_simd */
30109 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30110 block = begin_omp_parallel ();
30111 save = cp_parser_begin_omp_structured_block (parser);
30112 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30113 cp_parser_end_omp_structured_block (parser, save);
30114 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30115 block);
30116 if (ret == NULL_TREE)
30117 return ret;
30118 OMP_PARALLEL_COMBINED (stmt) = 1;
30119 return stmt;
30121 else if (cclauses)
30123 error_at (loc, "expected %<for%> after %qs", p_name);
30124 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30125 return NULL_TREE;
30127 else if (!flag_openmp) /* flag_openmp_simd */
30129 cp_parser_require_pragma_eol (parser, pragma_tok);
30130 return NULL_TREE;
30132 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30134 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30135 const char *p = IDENTIFIER_POINTER (id);
30136 if (strcmp (p, "sections") == 0)
30138 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30139 cclauses = cclauses_buf;
30141 cp_lexer_consume_token (parser->lexer);
30142 block = begin_omp_parallel ();
30143 save = cp_parser_begin_omp_structured_block (parser);
30144 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30145 cp_parser_end_omp_structured_block (parser, save);
30146 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30147 block);
30148 OMP_PARALLEL_COMBINED (stmt) = 1;
30149 return stmt;
30153 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30155 block = begin_omp_parallel ();
30156 save = cp_parser_begin_omp_structured_block (parser);
30157 cp_parser_statement (parser, NULL_TREE, false, NULL);
30158 cp_parser_end_omp_structured_block (parser, save);
30159 stmt = finish_omp_parallel (clauses, block);
30160 return stmt;
30163 /* OpenMP 2.5:
30164 # pragma omp single single-clause[optseq] new-line
30165 structured-block */
30167 #define OMP_SINGLE_CLAUSE_MASK \
30168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30173 static tree
30174 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30176 tree stmt = make_node (OMP_SINGLE);
30177 TREE_TYPE (stmt) = void_type_node;
30179 OMP_SINGLE_CLAUSES (stmt)
30180 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30181 "#pragma omp single", pragma_tok);
30182 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30184 return add_stmt (stmt);
30187 /* OpenMP 3.0:
30188 # pragma omp task task-clause[optseq] new-line
30189 structured-block */
30191 #define OMP_TASK_CLAUSE_MASK \
30192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30202 static tree
30203 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30205 tree clauses, block;
30206 unsigned int save;
30208 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30209 "#pragma omp task", pragma_tok);
30210 block = begin_omp_task ();
30211 save = cp_parser_begin_omp_structured_block (parser);
30212 cp_parser_statement (parser, NULL_TREE, false, NULL);
30213 cp_parser_end_omp_structured_block (parser, save);
30214 return finish_omp_task (clauses, block);
30217 /* OpenMP 3.0:
30218 # pragma omp taskwait new-line */
30220 static void
30221 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30223 cp_parser_require_pragma_eol (parser, pragma_tok);
30224 finish_omp_taskwait ();
30227 /* OpenMP 3.1:
30228 # pragma omp taskyield new-line */
30230 static void
30231 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30233 cp_parser_require_pragma_eol (parser, pragma_tok);
30234 finish_omp_taskyield ();
30237 /* OpenMP 4.0:
30238 # pragma omp taskgroup new-line
30239 structured-block */
30241 static tree
30242 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30244 cp_parser_require_pragma_eol (parser, pragma_tok);
30245 return c_finish_omp_taskgroup (input_location,
30246 cp_parser_omp_structured_block (parser));
30250 /* OpenMP 2.5:
30251 # pragma omp threadprivate (variable-list) */
30253 static void
30254 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30256 tree vars;
30258 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30259 cp_parser_require_pragma_eol (parser, pragma_tok);
30261 finish_omp_threadprivate (vars);
30264 /* OpenMP 4.0:
30265 # pragma omp cancel cancel-clause[optseq] new-line */
30267 #define OMP_CANCEL_CLAUSE_MASK \
30268 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30274 static void
30275 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30277 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30278 "#pragma omp cancel", pragma_tok);
30279 finish_omp_cancel (clauses);
30282 /* OpenMP 4.0:
30283 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30285 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30286 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30291 static void
30292 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30294 tree clauses;
30295 bool point_seen = false;
30297 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30299 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30300 const char *p = IDENTIFIER_POINTER (id);
30302 if (strcmp (p, "point") == 0)
30304 cp_lexer_consume_token (parser->lexer);
30305 point_seen = true;
30308 if (!point_seen)
30310 cp_parser_error (parser, "expected %<point%>");
30311 cp_parser_require_pragma_eol (parser, pragma_tok);
30312 return;
30315 clauses = cp_parser_omp_all_clauses (parser,
30316 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30317 "#pragma omp cancellation point",
30318 pragma_tok);
30319 finish_omp_cancellation_point (clauses);
30322 /* OpenMP 4.0:
30323 #pragma omp distribute distribute-clause[optseq] new-line
30324 for-loop */
30326 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30327 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30332 static tree
30333 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30334 char *p_name, omp_clause_mask mask, tree *cclauses)
30336 tree clauses, sb, ret;
30337 unsigned int save;
30338 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30340 strcat (p_name, " distribute");
30341 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30343 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30345 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30346 const char *p = IDENTIFIER_POINTER (id);
30347 bool simd = false;
30348 bool parallel = false;
30350 if (strcmp (p, "simd") == 0)
30351 simd = true;
30352 else
30353 parallel = strcmp (p, "parallel") == 0;
30354 if (parallel || simd)
30356 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30357 if (cclauses == NULL)
30358 cclauses = cclauses_buf;
30359 cp_lexer_consume_token (parser->lexer);
30360 if (!flag_openmp) /* flag_openmp_simd */
30362 if (simd)
30363 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30364 cclauses);
30365 else
30366 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30367 cclauses);
30369 sb = begin_omp_structured_block ();
30370 save = cp_parser_begin_omp_structured_block (parser);
30371 if (simd)
30372 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30373 cclauses);
30374 else
30375 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30376 cclauses);
30377 cp_parser_end_omp_structured_block (parser, save);
30378 tree body = finish_omp_structured_block (sb);
30379 if (ret == NULL)
30380 return ret;
30381 ret = make_node (OMP_DISTRIBUTE);
30382 TREE_TYPE (ret) = void_type_node;
30383 OMP_FOR_BODY (ret) = body;
30384 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30385 SET_EXPR_LOCATION (ret, loc);
30386 add_stmt (ret);
30387 return ret;
30390 if (!flag_openmp) /* flag_openmp_simd */
30392 cp_parser_require_pragma_eol (parser, pragma_tok);
30393 return NULL_TREE;
30396 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30397 cclauses == NULL);
30398 if (cclauses)
30400 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30401 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30404 sb = begin_omp_structured_block ();
30405 save = cp_parser_begin_omp_structured_block (parser);
30407 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30409 cp_parser_end_omp_structured_block (parser, save);
30410 add_stmt (finish_omp_structured_block (sb));
30412 return ret;
30415 /* OpenMP 4.0:
30416 # pragma omp teams teams-clause[optseq] new-line
30417 structured-block */
30419 #define OMP_TEAMS_CLAUSE_MASK \
30420 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30428 static tree
30429 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30430 char *p_name, omp_clause_mask mask, tree *cclauses)
30432 tree clauses, sb, ret;
30433 unsigned int save;
30434 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30436 strcat (p_name, " teams");
30437 mask |= OMP_TEAMS_CLAUSE_MASK;
30439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30442 const char *p = IDENTIFIER_POINTER (id);
30443 if (strcmp (p, "distribute") == 0)
30445 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30446 if (cclauses == NULL)
30447 cclauses = cclauses_buf;
30449 cp_lexer_consume_token (parser->lexer);
30450 if (!flag_openmp) /* flag_openmp_simd */
30451 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30452 cclauses);
30453 sb = begin_omp_structured_block ();
30454 save = cp_parser_begin_omp_structured_block (parser);
30455 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30456 cclauses);
30457 cp_parser_end_omp_structured_block (parser, save);
30458 tree body = finish_omp_structured_block (sb);
30459 if (ret == NULL)
30460 return ret;
30461 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30462 ret = make_node (OMP_TEAMS);
30463 TREE_TYPE (ret) = void_type_node;
30464 OMP_TEAMS_CLAUSES (ret) = clauses;
30465 OMP_TEAMS_BODY (ret) = body;
30466 return add_stmt (ret);
30469 if (!flag_openmp) /* flag_openmp_simd */
30471 cp_parser_require_pragma_eol (parser, pragma_tok);
30472 return NULL_TREE;
30475 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30476 cclauses == NULL);
30477 if (cclauses)
30479 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30480 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30483 tree stmt = make_node (OMP_TEAMS);
30484 TREE_TYPE (stmt) = void_type_node;
30485 OMP_TEAMS_CLAUSES (stmt) = clauses;
30486 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30488 return add_stmt (stmt);
30491 /* OpenMP 4.0:
30492 # pragma omp target data target-data-clause[optseq] new-line
30493 structured-block */
30495 #define OMP_TARGET_DATA_CLAUSE_MASK \
30496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30500 static tree
30501 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30503 tree stmt = make_node (OMP_TARGET_DATA);
30504 TREE_TYPE (stmt) = void_type_node;
30506 OMP_TARGET_DATA_CLAUSES (stmt)
30507 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30508 "#pragma omp target data", pragma_tok);
30509 keep_next_level (true);
30510 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30512 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30513 return add_stmt (stmt);
30516 /* OpenMP 4.0:
30517 # pragma omp target update target-update-clause[optseq] new-line */
30519 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30520 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30525 static bool
30526 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30527 enum pragma_context context)
30529 if (context == pragma_stmt)
30531 error_at (pragma_tok->location,
30532 "%<#pragma omp target update%> may only be "
30533 "used in compound statements");
30534 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30535 return false;
30538 tree clauses
30539 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30540 "#pragma omp target update", pragma_tok);
30541 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30542 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30544 error_at (pragma_tok->location,
30545 "%<#pragma omp target update must contain at least one "
30546 "%<from%> or %<to%> clauses");
30547 return false;
30550 tree stmt = make_node (OMP_TARGET_UPDATE);
30551 TREE_TYPE (stmt) = void_type_node;
30552 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30553 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30554 add_stmt (stmt);
30555 return false;
30558 /* OpenMP 4.0:
30559 # pragma omp target target-clause[optseq] new-line
30560 structured-block */
30562 #define OMP_TARGET_CLAUSE_MASK \
30563 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30567 static bool
30568 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30569 enum pragma_context context)
30571 if (context != pragma_stmt && context != pragma_compound)
30573 cp_parser_error (parser, "expected declaration specifiers");
30574 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30575 return false;
30578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30580 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30581 const char *p = IDENTIFIER_POINTER (id);
30583 if (strcmp (p, "teams") == 0)
30585 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30586 char p_name[sizeof ("#pragma omp target teams distribute "
30587 "parallel for simd")];
30589 cp_lexer_consume_token (parser->lexer);
30590 strcpy (p_name, "#pragma omp target");
30591 if (!flag_openmp) /* flag_openmp_simd */
30593 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30594 OMP_TARGET_CLAUSE_MASK,
30595 cclauses);
30596 return stmt != NULL_TREE;
30598 keep_next_level (true);
30599 tree sb = begin_omp_structured_block ();
30600 unsigned save = cp_parser_begin_omp_structured_block (parser);
30601 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30602 OMP_TARGET_CLAUSE_MASK, cclauses);
30603 cp_parser_end_omp_structured_block (parser, save);
30604 tree body = finish_omp_structured_block (sb);
30605 if (ret == NULL_TREE)
30606 return false;
30607 tree stmt = make_node (OMP_TARGET);
30608 TREE_TYPE (stmt) = void_type_node;
30609 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30610 OMP_TARGET_BODY (stmt) = body;
30611 add_stmt (stmt);
30612 return true;
30614 else if (!flag_openmp) /* flag_openmp_simd */
30616 cp_parser_require_pragma_eol (parser, pragma_tok);
30617 return false;
30619 else if (strcmp (p, "data") == 0)
30621 cp_lexer_consume_token (parser->lexer);
30622 cp_parser_omp_target_data (parser, pragma_tok);
30623 return true;
30625 else if (strcmp (p, "update") == 0)
30627 cp_lexer_consume_token (parser->lexer);
30628 return cp_parser_omp_target_update (parser, pragma_tok, context);
30632 tree stmt = make_node (OMP_TARGET);
30633 TREE_TYPE (stmt) = void_type_node;
30635 OMP_TARGET_CLAUSES (stmt)
30636 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30637 "#pragma omp target", pragma_tok);
30638 keep_next_level (true);
30639 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30641 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30642 add_stmt (stmt);
30643 return true;
30646 /* OpenMP 4.0:
30647 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30649 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30657 static void
30658 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30659 enum pragma_context context)
30661 bool first_p = parser->omp_declare_simd == NULL;
30662 cp_omp_declare_simd_data data;
30663 if (first_p)
30665 data.error_seen = false;
30666 data.fndecl_seen = false;
30667 data.tokens = vNULL;
30668 parser->omp_declare_simd = &data;
30670 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30671 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30672 cp_lexer_consume_token (parser->lexer);
30673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30674 parser->omp_declare_simd->error_seen = true;
30675 cp_parser_require_pragma_eol (parser, pragma_tok);
30676 struct cp_token_cache *cp
30677 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30678 parser->omp_declare_simd->tokens.safe_push (cp);
30679 if (first_p)
30681 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30682 cp_parser_pragma (parser, context);
30683 switch (context)
30685 case pragma_external:
30686 cp_parser_declaration (parser);
30687 break;
30688 case pragma_member:
30689 cp_parser_member_declaration (parser);
30690 break;
30691 case pragma_objc_icode:
30692 cp_parser_block_declaration (parser, /*statement_p=*/false);
30693 break;
30694 default:
30695 cp_parser_declaration_statement (parser);
30696 break;
30698 if (parser->omp_declare_simd
30699 && !parser->omp_declare_simd->error_seen
30700 && !parser->omp_declare_simd->fndecl_seen)
30701 error_at (pragma_tok->location,
30702 "%<#pragma omp declare simd%> not immediately followed by "
30703 "function declaration or definition");
30704 data.tokens.release ();
30705 parser->omp_declare_simd = NULL;
30709 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30710 This function is modelled similar to the late parsing of omp declare
30711 simd. */
30713 static tree
30714 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30716 struct cp_token_cache *ce;
30717 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30718 int ii = 0;
30720 if (parser->omp_declare_simd != NULL)
30722 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30723 " marked as a Cilk Plus SIMD-enabled function");
30724 XDELETE (parser->cilk_simd_fn_info);
30725 parser->cilk_simd_fn_info = NULL;
30726 return attrs;
30728 if (!info->error_seen && info->fndecl_seen)
30730 error ("vector attribute not immediately followed by a single function"
30731 " declaration or definition");
30732 info->error_seen = true;
30734 if (info->error_seen)
30735 return attrs;
30737 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30739 tree c, cl;
30741 cp_parser_push_lexer_for_tokens (parser, ce);
30742 parser->lexer->in_pragma = true;
30743 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30744 "SIMD-enabled functions attribute",
30745 NULL);
30746 cp_parser_pop_lexer (parser);
30747 if (cl)
30748 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30750 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30751 TREE_CHAIN (c) = attrs;
30752 attrs = c;
30754 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30755 TREE_CHAIN (c) = attrs;
30756 if (processing_template_decl)
30757 ATTR_IS_DEPENDENT (c) = 1;
30758 attrs = c;
30760 info->fndecl_seen = true;
30761 XDELETE (parser->cilk_simd_fn_info);
30762 parser->cilk_simd_fn_info = NULL;
30763 return attrs;
30766 /* Finalize #pragma omp declare simd clauses after direct declarator has
30767 been parsed, and put that into "omp declare simd" attribute. */
30769 static tree
30770 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30772 struct cp_token_cache *ce;
30773 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30774 int i;
30776 if (!data->error_seen && data->fndecl_seen)
30778 error ("%<#pragma omp declare simd%> not immediately followed by "
30779 "a single function declaration or definition");
30780 data->error_seen = true;
30781 return attrs;
30783 if (data->error_seen)
30784 return attrs;
30786 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30788 tree c, cl;
30790 cp_parser_push_lexer_for_tokens (parser, ce);
30791 parser->lexer->in_pragma = true;
30792 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30793 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30794 cp_lexer_consume_token (parser->lexer);
30795 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30796 "#pragma omp declare simd", pragma_tok);
30797 cp_parser_pop_lexer (parser);
30798 if (cl)
30799 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30800 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30801 TREE_CHAIN (c) = attrs;
30802 if (processing_template_decl)
30803 ATTR_IS_DEPENDENT (c) = 1;
30804 attrs = c;
30807 data->fndecl_seen = true;
30808 return attrs;
30812 /* OpenMP 4.0:
30813 # pragma omp declare target new-line
30814 declarations and definitions
30815 # pragma omp end declare target new-line */
30817 static void
30818 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30820 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30821 scope_chain->omp_declare_target_attribute++;
30824 static void
30825 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30827 const char *p = "";
30828 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30830 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30831 p = IDENTIFIER_POINTER (id);
30833 if (strcmp (p, "declare") == 0)
30835 cp_lexer_consume_token (parser->lexer);
30836 p = "";
30837 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30839 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30840 p = IDENTIFIER_POINTER (id);
30842 if (strcmp (p, "target") == 0)
30843 cp_lexer_consume_token (parser->lexer);
30844 else
30846 cp_parser_error (parser, "expected %<target%>");
30847 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30848 return;
30851 else
30853 cp_parser_error (parser, "expected %<declare%>");
30854 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30855 return;
30857 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30858 if (!scope_chain->omp_declare_target_attribute)
30859 error_at (pragma_tok->location,
30860 "%<#pragma omp end declare target%> without corresponding "
30861 "%<#pragma omp declare target%>");
30862 else
30863 scope_chain->omp_declare_target_attribute--;
30866 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30867 expression and optional initializer clause of
30868 #pragma omp declare reduction. We store the expression(s) as
30869 either 3, 6 or 7 special statements inside of the artificial function's
30870 body. The first two statements are DECL_EXPRs for the artificial
30871 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30872 expression that uses those variables.
30873 If there was any INITIALIZER clause, this is followed by further statements,
30874 the fourth and fifth statements are DECL_EXPRs for the artificial
30875 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30876 constructor variant (first token after open paren is not omp_priv),
30877 then the sixth statement is a statement with the function call expression
30878 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30879 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30880 to initialize the OMP_PRIV artificial variable and there is seventh
30881 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30883 static bool
30884 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30886 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30887 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30888 type = TREE_TYPE (type);
30889 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30890 DECL_ARTIFICIAL (omp_out) = 1;
30891 pushdecl (omp_out);
30892 add_decl_expr (omp_out);
30893 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30894 DECL_ARTIFICIAL (omp_in) = 1;
30895 pushdecl (omp_in);
30896 add_decl_expr (omp_in);
30897 tree combiner;
30898 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30900 keep_next_level (true);
30901 tree block = begin_omp_structured_block ();
30902 combiner = cp_parser_expression (parser, false, NULL);
30903 finish_expr_stmt (combiner);
30904 block = finish_omp_structured_block (block);
30905 add_stmt (block);
30907 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30908 return false;
30910 const char *p = "";
30911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30913 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30914 p = IDENTIFIER_POINTER (id);
30917 if (strcmp (p, "initializer") == 0)
30919 cp_lexer_consume_token (parser->lexer);
30920 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30921 return false;
30923 p = "";
30924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30926 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30927 p = IDENTIFIER_POINTER (id);
30930 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30931 DECL_ARTIFICIAL (omp_priv) = 1;
30932 pushdecl (omp_priv);
30933 add_decl_expr (omp_priv);
30934 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30935 DECL_ARTIFICIAL (omp_orig) = 1;
30936 pushdecl (omp_orig);
30937 add_decl_expr (omp_orig);
30939 keep_next_level (true);
30940 block = begin_omp_structured_block ();
30942 bool ctor = false;
30943 if (strcmp (p, "omp_priv") == 0)
30945 bool is_direct_init, is_non_constant_init;
30946 ctor = true;
30947 cp_lexer_consume_token (parser->lexer);
30948 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30949 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30950 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30951 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30952 == CPP_CLOSE_PAREN
30953 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30954 == CPP_CLOSE_PAREN))
30956 finish_omp_structured_block (block);
30957 error ("invalid initializer clause");
30958 return false;
30960 initializer = cp_parser_initializer (parser, &is_direct_init,
30961 &is_non_constant_init);
30962 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30963 NULL_TREE, LOOKUP_ONLYCONVERTING);
30965 else
30967 cp_parser_parse_tentatively (parser);
30968 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30969 /*check_dependency_p=*/true,
30970 /*template_p=*/NULL,
30971 /*declarator_p=*/false,
30972 /*optional_p=*/false);
30973 vec<tree, va_gc> *args;
30974 if (fn_name == error_mark_node
30975 || cp_parser_error_occurred (parser)
30976 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30977 || ((args = cp_parser_parenthesized_expression_list
30978 (parser, non_attr, /*cast_p=*/false,
30979 /*allow_expansion_p=*/true,
30980 /*non_constant_p=*/NULL)),
30981 cp_parser_error_occurred (parser)))
30983 finish_omp_structured_block (block);
30984 cp_parser_abort_tentative_parse (parser);
30985 cp_parser_error (parser, "expected id-expression (arguments)");
30986 return false;
30988 unsigned int i;
30989 tree arg;
30990 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30991 if (arg == omp_priv
30992 || (TREE_CODE (arg) == ADDR_EXPR
30993 && TREE_OPERAND (arg, 0) == omp_priv))
30994 break;
30995 cp_parser_abort_tentative_parse (parser);
30996 if (arg == NULL_TREE)
30997 error ("one of the initializer call arguments should be %<omp_priv%>"
30998 " or %<&omp_priv%>");
30999 initializer = cp_parser_postfix_expression (parser, false, false, false,
31000 false, NULL);
31001 finish_expr_stmt (initializer);
31004 block = finish_omp_structured_block (block);
31005 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31006 finish_expr_stmt (block);
31008 if (ctor)
31009 add_decl_expr (omp_orig);
31011 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31012 return false;
31015 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31016 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31018 return true;
31021 /* OpenMP 4.0
31022 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31023 initializer-clause[opt] new-line
31025 initializer-clause:
31026 initializer (omp_priv initializer)
31027 initializer (function-name (argument-list)) */
31029 static void
31030 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31031 enum pragma_context)
31033 auto_vec<tree> types;
31034 enum tree_code reduc_code = ERROR_MARK;
31035 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31036 unsigned int i;
31037 cp_token *first_token;
31038 cp_token_cache *cp;
31039 int errs;
31040 void *p;
31042 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31043 p = obstack_alloc (&declarator_obstack, 0);
31045 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31046 goto fail;
31048 switch (cp_lexer_peek_token (parser->lexer)->type)
31050 case CPP_PLUS:
31051 reduc_code = PLUS_EXPR;
31052 break;
31053 case CPP_MULT:
31054 reduc_code = MULT_EXPR;
31055 break;
31056 case CPP_MINUS:
31057 reduc_code = MINUS_EXPR;
31058 break;
31059 case CPP_AND:
31060 reduc_code = BIT_AND_EXPR;
31061 break;
31062 case CPP_XOR:
31063 reduc_code = BIT_XOR_EXPR;
31064 break;
31065 case CPP_OR:
31066 reduc_code = BIT_IOR_EXPR;
31067 break;
31068 case CPP_AND_AND:
31069 reduc_code = TRUTH_ANDIF_EXPR;
31070 break;
31071 case CPP_OR_OR:
31072 reduc_code = TRUTH_ORIF_EXPR;
31073 break;
31074 case CPP_NAME:
31075 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31076 break;
31077 default:
31078 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31079 "%<|%>, %<&&%>, %<||%> or identifier");
31080 goto fail;
31083 if (reduc_code != ERROR_MARK)
31084 cp_lexer_consume_token (parser->lexer);
31086 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31087 if (reduc_id == error_mark_node)
31088 goto fail;
31090 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31091 goto fail;
31093 /* Types may not be defined in declare reduction type list. */
31094 const char *saved_message;
31095 saved_message = parser->type_definition_forbidden_message;
31096 parser->type_definition_forbidden_message
31097 = G_("types may not be defined in declare reduction type list");
31098 bool saved_colon_corrects_to_scope_p;
31099 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31100 parser->colon_corrects_to_scope_p = false;
31101 bool saved_colon_doesnt_start_class_def_p;
31102 saved_colon_doesnt_start_class_def_p
31103 = parser->colon_doesnt_start_class_def_p;
31104 parser->colon_doesnt_start_class_def_p = true;
31106 while (true)
31108 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31109 type = cp_parser_type_id (parser);
31110 if (type == error_mark_node)
31112 else if (ARITHMETIC_TYPE_P (type)
31113 && (orig_reduc_id == NULL_TREE
31114 || (TREE_CODE (type) != COMPLEX_TYPE
31115 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31116 "min") == 0
31117 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31118 "max") == 0))))
31119 error_at (loc, "predeclared arithmetic type %qT in "
31120 "%<#pragma omp declare reduction%>", type);
31121 else if (TREE_CODE (type) == FUNCTION_TYPE
31122 || TREE_CODE (type) == METHOD_TYPE
31123 || TREE_CODE (type) == ARRAY_TYPE)
31124 error_at (loc, "function or array type %qT in "
31125 "%<#pragma omp declare reduction%>", type);
31126 else if (TREE_CODE (type) == REFERENCE_TYPE)
31127 error_at (loc, "reference type %qT in "
31128 "%<#pragma omp declare reduction%>", type);
31129 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31130 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31131 "%<#pragma omp declare reduction%>", type);
31132 else
31133 types.safe_push (type);
31135 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31136 cp_lexer_consume_token (parser->lexer);
31137 else
31138 break;
31141 /* Restore the saved message. */
31142 parser->type_definition_forbidden_message = saved_message;
31143 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31144 parser->colon_doesnt_start_class_def_p
31145 = saved_colon_doesnt_start_class_def_p;
31147 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31148 || types.is_empty ())
31150 fail:
31151 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31152 goto done;
31155 first_token = cp_lexer_peek_token (parser->lexer);
31156 cp = NULL;
31157 errs = errorcount;
31158 FOR_EACH_VEC_ELT (types, i, type)
31160 tree fntype
31161 = build_function_type_list (void_type_node,
31162 cp_build_reference_type (type, false),
31163 NULL_TREE);
31164 tree this_reduc_id = reduc_id;
31165 if (!dependent_type_p (type))
31166 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31167 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31168 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31169 DECL_ARTIFICIAL (fndecl) = 1;
31170 DECL_EXTERNAL (fndecl) = 1;
31171 DECL_DECLARED_INLINE_P (fndecl) = 1;
31172 DECL_IGNORED_P (fndecl) = 1;
31173 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31174 DECL_ATTRIBUTES (fndecl)
31175 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31176 DECL_ATTRIBUTES (fndecl));
31177 if (processing_template_decl)
31178 fndecl = push_template_decl (fndecl);
31179 bool block_scope = false;
31180 tree block = NULL_TREE;
31181 if (current_function_decl)
31183 block_scope = true;
31184 DECL_CONTEXT (fndecl) = global_namespace;
31185 if (!processing_template_decl)
31186 pushdecl (fndecl);
31188 else if (current_class_type)
31190 if (cp == NULL)
31192 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31193 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31194 cp_lexer_consume_token (parser->lexer);
31195 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31196 goto fail;
31197 cp = cp_token_cache_new (first_token,
31198 cp_lexer_peek_nth_token (parser->lexer,
31199 2));
31201 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31202 finish_member_declaration (fndecl);
31203 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31204 DECL_PENDING_INLINE_P (fndecl) = 1;
31205 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31206 continue;
31208 else
31210 DECL_CONTEXT (fndecl) = current_namespace;
31211 pushdecl (fndecl);
31213 if (!block_scope)
31214 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31215 else
31216 block = begin_omp_structured_block ();
31217 if (cp)
31219 cp_parser_push_lexer_for_tokens (parser, cp);
31220 parser->lexer->in_pragma = true;
31222 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31224 if (!block_scope)
31225 finish_function (0);
31226 else
31227 DECL_CONTEXT (fndecl) = current_function_decl;
31228 if (cp)
31229 cp_parser_pop_lexer (parser);
31230 goto fail;
31232 if (cp)
31233 cp_parser_pop_lexer (parser);
31234 if (!block_scope)
31235 finish_function (0);
31236 else
31238 DECL_CONTEXT (fndecl) = current_function_decl;
31239 block = finish_omp_structured_block (block);
31240 if (TREE_CODE (block) == BIND_EXPR)
31241 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31242 else if (TREE_CODE (block) == STATEMENT_LIST)
31243 DECL_SAVED_TREE (fndecl) = block;
31244 if (processing_template_decl)
31245 add_decl_expr (fndecl);
31247 cp_check_omp_declare_reduction (fndecl);
31248 if (cp == NULL && types.length () > 1)
31249 cp = cp_token_cache_new (first_token,
31250 cp_lexer_peek_nth_token (parser->lexer, 2));
31251 if (errs != errorcount)
31252 break;
31255 cp_parser_require_pragma_eol (parser, pragma_tok);
31257 done:
31258 /* Free any declarators allocated. */
31259 obstack_free (&declarator_obstack, p);
31262 /* OpenMP 4.0
31263 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31264 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31265 initializer-clause[opt] new-line
31266 #pragma omp declare target new-line */
31268 static void
31269 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31270 enum pragma_context context)
31272 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31274 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31275 const char *p = IDENTIFIER_POINTER (id);
31277 if (strcmp (p, "simd") == 0)
31279 cp_lexer_consume_token (parser->lexer);
31280 cp_parser_omp_declare_simd (parser, pragma_tok,
31281 context);
31282 return;
31284 cp_ensure_no_omp_declare_simd (parser);
31285 if (strcmp (p, "reduction") == 0)
31287 cp_lexer_consume_token (parser->lexer);
31288 cp_parser_omp_declare_reduction (parser, pragma_tok,
31289 context);
31290 return;
31292 if (!flag_openmp) /* flag_openmp_simd */
31294 cp_parser_require_pragma_eol (parser, pragma_tok);
31295 return;
31297 if (strcmp (p, "target") == 0)
31299 cp_lexer_consume_token (parser->lexer);
31300 cp_parser_omp_declare_target (parser, pragma_tok);
31301 return;
31304 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31305 "or %<target%>");
31306 cp_parser_require_pragma_eol (parser, pragma_tok);
31309 /* Main entry point to OpenMP statement pragmas. */
31311 static void
31312 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31314 tree stmt;
31315 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31316 omp_clause_mask mask (0);
31318 switch (pragma_tok->pragma_kind)
31320 case PRAGMA_OMP_ATOMIC:
31321 cp_parser_omp_atomic (parser, pragma_tok);
31322 return;
31323 case PRAGMA_OMP_CRITICAL:
31324 stmt = cp_parser_omp_critical (parser, pragma_tok);
31325 break;
31326 case PRAGMA_OMP_DISTRIBUTE:
31327 strcpy (p_name, "#pragma omp");
31328 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31329 break;
31330 case PRAGMA_OMP_FOR:
31331 strcpy (p_name, "#pragma omp");
31332 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31333 break;
31334 case PRAGMA_OMP_MASTER:
31335 stmt = cp_parser_omp_master (parser, pragma_tok);
31336 break;
31337 case PRAGMA_OMP_ORDERED:
31338 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31339 break;
31340 case PRAGMA_OMP_PARALLEL:
31341 strcpy (p_name, "#pragma omp");
31342 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31343 break;
31344 case PRAGMA_OMP_SECTIONS:
31345 strcpy (p_name, "#pragma omp");
31346 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31347 break;
31348 case PRAGMA_OMP_SIMD:
31349 strcpy (p_name, "#pragma omp");
31350 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31351 break;
31352 case PRAGMA_OMP_SINGLE:
31353 stmt = cp_parser_omp_single (parser, pragma_tok);
31354 break;
31355 case PRAGMA_OMP_TASK:
31356 stmt = cp_parser_omp_task (parser, pragma_tok);
31357 break;
31358 case PRAGMA_OMP_TASKGROUP:
31359 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31360 break;
31361 case PRAGMA_OMP_TEAMS:
31362 strcpy (p_name, "#pragma omp");
31363 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31364 break;
31365 default:
31366 gcc_unreachable ();
31369 if (stmt)
31370 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31373 /* Transactional Memory parsing routines. */
31375 /* Parse a transaction attribute.
31377 txn-attribute:
31378 attribute
31379 [ [ identifier ] ]
31381 ??? Simplify this when C++0x bracket attributes are
31382 implemented properly. */
31384 static tree
31385 cp_parser_txn_attribute_opt (cp_parser *parser)
31387 cp_token *token;
31388 tree attr_name, attr = NULL;
31390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31391 return cp_parser_attributes_opt (parser);
31393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31394 return NULL_TREE;
31395 cp_lexer_consume_token (parser->lexer);
31396 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31397 goto error1;
31399 token = cp_lexer_peek_token (parser->lexer);
31400 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31402 token = cp_lexer_consume_token (parser->lexer);
31404 attr_name = (token->type == CPP_KEYWORD
31405 /* For keywords, use the canonical spelling,
31406 not the parsed identifier. */
31407 ? ridpointers[(int) token->keyword]
31408 : token->u.value);
31409 attr = build_tree_list (attr_name, NULL_TREE);
31411 else
31412 cp_parser_error (parser, "expected identifier");
31414 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31415 error1:
31416 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31417 return attr;
31420 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31422 transaction-statement:
31423 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31424 compound-statement
31425 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31428 static tree
31429 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31431 unsigned char old_in = parser->in_transaction;
31432 unsigned char this_in = 1, new_in;
31433 cp_token *token;
31434 tree stmt, attrs, noex;
31436 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31437 || keyword == RID_TRANSACTION_RELAXED);
31438 token = cp_parser_require_keyword (parser, keyword,
31439 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31440 : RT_TRANSACTION_RELAXED));
31441 gcc_assert (token != NULL);
31443 if (keyword == RID_TRANSACTION_RELAXED)
31444 this_in |= TM_STMT_ATTR_RELAXED;
31445 else
31447 attrs = cp_parser_txn_attribute_opt (parser);
31448 if (attrs)
31449 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31452 /* Parse a noexcept specification. */
31453 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31455 /* Keep track if we're in the lexical scope of an outer transaction. */
31456 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31458 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31460 parser->in_transaction = new_in;
31461 cp_parser_compound_statement (parser, NULL, false, false);
31462 parser->in_transaction = old_in;
31464 finish_transaction_stmt (stmt, NULL, this_in, noex);
31466 return stmt;
31469 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31471 transaction-expression:
31472 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31473 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31476 static tree
31477 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31479 unsigned char old_in = parser->in_transaction;
31480 unsigned char this_in = 1;
31481 cp_token *token;
31482 tree expr, noex;
31483 bool noex_expr;
31485 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31486 || keyword == RID_TRANSACTION_RELAXED);
31488 if (!flag_tm)
31489 error (keyword == RID_TRANSACTION_RELAXED
31490 ? G_("%<__transaction_relaxed%> without transactional memory "
31491 "support enabled")
31492 : G_("%<__transaction_atomic%> without transactional memory "
31493 "support enabled"));
31495 token = cp_parser_require_keyword (parser, keyword,
31496 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31497 : RT_TRANSACTION_RELAXED));
31498 gcc_assert (token != NULL);
31500 if (keyword == RID_TRANSACTION_RELAXED)
31501 this_in |= TM_STMT_ATTR_RELAXED;
31503 /* Set this early. This might mean that we allow transaction_cancel in
31504 an expression that we find out later actually has to be a constexpr.
31505 However, we expect that cxx_constant_value will be able to deal with
31506 this; also, if the noexcept has no constexpr, then what we parse next
31507 really is a transaction's body. */
31508 parser->in_transaction = this_in;
31510 /* Parse a noexcept specification. */
31511 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31512 true);
31514 if (!noex || !noex_expr
31515 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31517 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31519 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31520 expr = finish_parenthesized_expr (expr);
31522 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31524 else
31526 /* The only expression that is available got parsed for the noexcept
31527 already. noexcept is true then. */
31528 expr = noex;
31529 noex = boolean_true_node;
31532 expr = build_transaction_expr (token->location, expr, this_in, noex);
31533 parser->in_transaction = old_in;
31535 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31536 return error_mark_node;
31538 return (flag_tm ? expr : error_mark_node);
31541 /* Parse a function-transaction-block.
31543 function-transaction-block:
31544 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31545 function-body
31546 __transaction_atomic txn-attribute[opt] function-try-block
31547 __transaction_relaxed ctor-initializer[opt] function-body
31548 __transaction_relaxed function-try-block
31551 static bool
31552 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31554 unsigned char old_in = parser->in_transaction;
31555 unsigned char new_in = 1;
31556 tree compound_stmt, stmt, attrs;
31557 bool ctor_initializer_p;
31558 cp_token *token;
31560 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31561 || keyword == RID_TRANSACTION_RELAXED);
31562 token = cp_parser_require_keyword (parser, keyword,
31563 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31564 : RT_TRANSACTION_RELAXED));
31565 gcc_assert (token != NULL);
31567 if (keyword == RID_TRANSACTION_RELAXED)
31568 new_in |= TM_STMT_ATTR_RELAXED;
31569 else
31571 attrs = cp_parser_txn_attribute_opt (parser);
31572 if (attrs)
31573 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31576 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31578 parser->in_transaction = new_in;
31580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31581 ctor_initializer_p = cp_parser_function_try_block (parser);
31582 else
31583 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31584 (parser, /*in_function_try_block=*/false);
31586 parser->in_transaction = old_in;
31588 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31590 return ctor_initializer_p;
31593 /* Parse a __transaction_cancel statement.
31595 cancel-statement:
31596 __transaction_cancel txn-attribute[opt] ;
31597 __transaction_cancel txn-attribute[opt] throw-expression ;
31599 ??? Cancel and throw is not yet implemented. */
31601 static tree
31602 cp_parser_transaction_cancel (cp_parser *parser)
31604 cp_token *token;
31605 bool is_outer = false;
31606 tree stmt, attrs;
31608 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31609 RT_TRANSACTION_CANCEL);
31610 gcc_assert (token != NULL);
31612 attrs = cp_parser_txn_attribute_opt (parser);
31613 if (attrs)
31614 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31616 /* ??? Parse cancel-and-throw here. */
31618 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31620 if (!flag_tm)
31622 error_at (token->location, "%<__transaction_cancel%> without "
31623 "transactional memory support enabled");
31624 return error_mark_node;
31626 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31628 error_at (token->location, "%<__transaction_cancel%> within a "
31629 "%<__transaction_relaxed%>");
31630 return error_mark_node;
31632 else if (is_outer)
31634 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31635 && !is_tm_may_cancel_outer (current_function_decl))
31637 error_at (token->location, "outer %<__transaction_cancel%> not "
31638 "within outer %<__transaction_atomic%>");
31639 error_at (token->location,
31640 " or a %<transaction_may_cancel_outer%> function");
31641 return error_mark_node;
31644 else if (parser->in_transaction == 0)
31646 error_at (token->location, "%<__transaction_cancel%> not within "
31647 "%<__transaction_atomic%>");
31648 return error_mark_node;
31651 stmt = build_tm_abort_call (token->location, is_outer);
31652 add_stmt (stmt);
31654 return stmt;
31657 /* The parser. */
31659 static GTY (()) cp_parser *the_parser;
31662 /* Special handling for the first token or line in the file. The first
31663 thing in the file might be #pragma GCC pch_preprocess, which loads a
31664 PCH file, which is a GC collection point. So we need to handle this
31665 first pragma without benefit of an existing lexer structure.
31667 Always returns one token to the caller in *FIRST_TOKEN. This is
31668 either the true first token of the file, or the first token after
31669 the initial pragma. */
31671 static void
31672 cp_parser_initial_pragma (cp_token *first_token)
31674 tree name = NULL;
31676 cp_lexer_get_preprocessor_token (NULL, first_token);
31677 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31678 return;
31680 cp_lexer_get_preprocessor_token (NULL, first_token);
31681 if (first_token->type == CPP_STRING)
31683 name = first_token->u.value;
31685 cp_lexer_get_preprocessor_token (NULL, first_token);
31686 if (first_token->type != CPP_PRAGMA_EOL)
31687 error_at (first_token->location,
31688 "junk at end of %<#pragma GCC pch_preprocess%>");
31690 else
31691 error_at (first_token->location, "expected string literal");
31693 /* Skip to the end of the pragma. */
31694 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31695 cp_lexer_get_preprocessor_token (NULL, first_token);
31697 /* Now actually load the PCH file. */
31698 if (name)
31699 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31701 /* Read one more token to return to our caller. We have to do this
31702 after reading the PCH file in, since its pointers have to be
31703 live. */
31704 cp_lexer_get_preprocessor_token (NULL, first_token);
31707 /* Normal parsing of a pragma token. Here we can (and must) use the
31708 regular lexer. */
31710 static bool
31711 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31713 cp_token *pragma_tok;
31714 unsigned int id;
31716 pragma_tok = cp_lexer_consume_token (parser->lexer);
31717 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31718 parser->lexer->in_pragma = true;
31720 id = pragma_tok->pragma_kind;
31721 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31722 cp_ensure_no_omp_declare_simd (parser);
31723 switch (id)
31725 case PRAGMA_GCC_PCH_PREPROCESS:
31726 error_at (pragma_tok->location,
31727 "%<#pragma GCC pch_preprocess%> must be first");
31728 break;
31730 case PRAGMA_OMP_BARRIER:
31731 switch (context)
31733 case pragma_compound:
31734 cp_parser_omp_barrier (parser, pragma_tok);
31735 return false;
31736 case pragma_stmt:
31737 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31738 "used in compound statements");
31739 break;
31740 default:
31741 goto bad_stmt;
31743 break;
31745 case PRAGMA_OMP_FLUSH:
31746 switch (context)
31748 case pragma_compound:
31749 cp_parser_omp_flush (parser, pragma_tok);
31750 return false;
31751 case pragma_stmt:
31752 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31753 "used in compound statements");
31754 break;
31755 default:
31756 goto bad_stmt;
31758 break;
31760 case PRAGMA_OMP_TASKWAIT:
31761 switch (context)
31763 case pragma_compound:
31764 cp_parser_omp_taskwait (parser, pragma_tok);
31765 return false;
31766 case pragma_stmt:
31767 error_at (pragma_tok->location,
31768 "%<#pragma omp taskwait%> may only be "
31769 "used in compound statements");
31770 break;
31771 default:
31772 goto bad_stmt;
31774 break;
31776 case PRAGMA_OMP_TASKYIELD:
31777 switch (context)
31779 case pragma_compound:
31780 cp_parser_omp_taskyield (parser, pragma_tok);
31781 return false;
31782 case pragma_stmt:
31783 error_at (pragma_tok->location,
31784 "%<#pragma omp taskyield%> may only be "
31785 "used in compound statements");
31786 break;
31787 default:
31788 goto bad_stmt;
31790 break;
31792 case PRAGMA_OMP_CANCEL:
31793 switch (context)
31795 case pragma_compound:
31796 cp_parser_omp_cancel (parser, pragma_tok);
31797 return false;
31798 case pragma_stmt:
31799 error_at (pragma_tok->location,
31800 "%<#pragma omp cancel%> may only be "
31801 "used in compound statements");
31802 break;
31803 default:
31804 goto bad_stmt;
31806 break;
31808 case PRAGMA_OMP_CANCELLATION_POINT:
31809 switch (context)
31811 case pragma_compound:
31812 cp_parser_omp_cancellation_point (parser, pragma_tok);
31813 return false;
31814 case pragma_stmt:
31815 error_at (pragma_tok->location,
31816 "%<#pragma omp cancellation point%> may only be "
31817 "used in compound statements");
31818 break;
31819 default:
31820 goto bad_stmt;
31822 break;
31824 case PRAGMA_OMP_THREADPRIVATE:
31825 cp_parser_omp_threadprivate (parser, pragma_tok);
31826 return false;
31828 case PRAGMA_OMP_DECLARE_REDUCTION:
31829 cp_parser_omp_declare (parser, pragma_tok, context);
31830 return false;
31832 case PRAGMA_OMP_ATOMIC:
31833 case PRAGMA_OMP_CRITICAL:
31834 case PRAGMA_OMP_DISTRIBUTE:
31835 case PRAGMA_OMP_FOR:
31836 case PRAGMA_OMP_MASTER:
31837 case PRAGMA_OMP_ORDERED:
31838 case PRAGMA_OMP_PARALLEL:
31839 case PRAGMA_OMP_SECTIONS:
31840 case PRAGMA_OMP_SIMD:
31841 case PRAGMA_OMP_SINGLE:
31842 case PRAGMA_OMP_TASK:
31843 case PRAGMA_OMP_TASKGROUP:
31844 case PRAGMA_OMP_TEAMS:
31845 if (context != pragma_stmt && context != pragma_compound)
31846 goto bad_stmt;
31847 cp_parser_omp_construct (parser, pragma_tok);
31848 return true;
31850 case PRAGMA_OMP_TARGET:
31851 return cp_parser_omp_target (parser, pragma_tok, context);
31853 case PRAGMA_OMP_END_DECLARE_TARGET:
31854 cp_parser_omp_end_declare_target (parser, pragma_tok);
31855 return false;
31857 case PRAGMA_OMP_SECTION:
31858 error_at (pragma_tok->location,
31859 "%<#pragma omp section%> may only be used in "
31860 "%<#pragma omp sections%> construct");
31861 break;
31863 case PRAGMA_IVDEP:
31865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31866 cp_token *tok;
31867 tok = cp_lexer_peek_token (the_parser->lexer);
31868 if (tok->type != CPP_KEYWORD
31869 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31870 && tok->keyword != RID_DO))
31872 cp_parser_error (parser, "for, while or do statement expected");
31873 return false;
31875 cp_parser_iteration_statement (parser, true);
31876 return true;
31879 case PRAGMA_CILK_SIMD:
31880 if (context == pragma_external)
31882 error_at (pragma_tok->location,
31883 "%<#pragma simd%> must be inside a function");
31884 break;
31886 cp_parser_cilk_simd (parser, pragma_tok);
31887 return true;
31889 default:
31890 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31891 c_invoke_pragma_handler (id);
31892 break;
31894 bad_stmt:
31895 cp_parser_error (parser, "expected declaration specifiers");
31896 break;
31899 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31900 return false;
31903 /* The interface the pragma parsers have to the lexer. */
31905 enum cpp_ttype
31906 pragma_lex (tree *value)
31908 cp_token *tok;
31909 enum cpp_ttype ret;
31911 tok = cp_lexer_peek_token (the_parser->lexer);
31913 ret = tok->type;
31914 *value = tok->u.value;
31916 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31917 ret = CPP_EOF;
31918 else if (ret == CPP_STRING)
31919 *value = cp_parser_string_literal (the_parser, false, false);
31920 else
31922 cp_lexer_consume_token (the_parser->lexer);
31923 if (ret == CPP_KEYWORD)
31924 ret = CPP_NAME;
31927 return ret;
31931 /* External interface. */
31933 /* Parse one entire translation unit. */
31935 void
31936 c_parse_file (void)
31938 static bool already_called = false;
31940 if (already_called)
31942 sorry ("inter-module optimizations not implemented for C++");
31943 return;
31945 already_called = true;
31947 the_parser = cp_parser_new ();
31948 push_deferring_access_checks (flag_access_control
31949 ? dk_no_deferred : dk_no_check);
31950 cp_parser_translation_unit (the_parser);
31951 the_parser = NULL;
31954 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31955 vectorlength clause:
31956 Syntax:
31957 vectorlength ( constant-expression ) */
31959 static tree
31960 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31961 bool is_simd_fn)
31963 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31964 tree expr;
31965 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31966 safelen clause. Thus, vectorlength is represented as OMP 4.0
31967 safelen. For SIMD-enabled function it is represented by OMP 4.0
31968 simdlen. */
31969 if (!is_simd_fn)
31970 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31971 loc);
31972 else
31973 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31974 loc);
31976 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31977 return error_mark_node;
31979 expr = cp_parser_constant_expression (parser, false, NULL);
31980 expr = maybe_constant_value (expr);
31982 /* If expr == error_mark_node, then don't emit any errors nor
31983 create a clause. if any of the above functions returns
31984 error mark node then they would have emitted an error message. */
31985 if (expr == error_mark_node)
31987 else if (!TREE_TYPE (expr)
31988 || !TREE_CONSTANT (expr)
31989 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31990 error_at (loc, "vectorlength must be an integer constant");
31991 else if (TREE_CONSTANT (expr)
31992 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31993 error_at (loc, "vectorlength must be a power of 2");
31994 else
31996 tree c;
31997 if (!is_simd_fn)
31999 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32000 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32001 OMP_CLAUSE_CHAIN (c) = clauses;
32002 clauses = c;
32004 else
32006 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32007 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32008 OMP_CLAUSE_CHAIN (c) = clauses;
32009 clauses = c;
32013 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32014 return error_mark_node;
32015 return clauses;
32018 /* Handles the Cilk Plus #pragma simd linear clause.
32019 Syntax:
32020 linear ( simd-linear-variable-list )
32022 simd-linear-variable-list:
32023 simd-linear-variable
32024 simd-linear-variable-list , simd-linear-variable
32026 simd-linear-variable:
32027 id-expression
32028 id-expression : simd-linear-step
32030 simd-linear-step:
32031 conditional-expression */
32033 static tree
32034 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32036 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32038 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32039 return clauses;
32040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32042 cp_parser_error (parser, "expected identifier");
32043 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32044 return error_mark_node;
32047 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32048 parser->colon_corrects_to_scope_p = false;
32049 while (1)
32051 cp_token *token = cp_lexer_peek_token (parser->lexer);
32052 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32054 cp_parser_error (parser, "expected variable-name");
32055 clauses = error_mark_node;
32056 break;
32059 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32060 false, false);
32061 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32062 token->location);
32063 if (decl == error_mark_node)
32065 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32066 token->location);
32067 clauses = error_mark_node;
32069 else
32071 tree e = NULL_TREE;
32072 tree step_size = integer_one_node;
32074 /* If present, parse the linear step. Otherwise, assume the default
32075 value of 1. */
32076 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32078 cp_lexer_consume_token (parser->lexer);
32080 e = cp_parser_assignment_expression (parser, false, NULL);
32081 e = maybe_constant_value (e);
32083 if (e == error_mark_node)
32085 /* If an error has occurred, then the whole pragma is
32086 considered ill-formed. Thus, no reason to keep
32087 parsing. */
32088 clauses = error_mark_node;
32089 break;
32091 else if (type_dependent_expression_p (e)
32092 || value_dependent_expression_p (e)
32093 || (TREE_TYPE (e)
32094 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32095 && (TREE_CONSTANT (e)
32096 || DECL_P (e))))
32097 step_size = e;
32098 else
32099 cp_parser_error (parser,
32100 "step size must be an integer constant "
32101 "expression or an integer variable");
32104 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32105 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32106 OMP_CLAUSE_DECL (l) = decl;
32107 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32108 OMP_CLAUSE_CHAIN (l) = clauses;
32109 clauses = l;
32111 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32112 cp_lexer_consume_token (parser->lexer);
32113 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32114 break;
32115 else
32117 error_at (cp_lexer_peek_token (parser->lexer)->location,
32118 "expected %<,%> or %<)%> after %qE", decl);
32119 clauses = error_mark_node;
32120 break;
32123 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32124 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32125 return clauses;
32128 /* Returns the name of the next clause. If the clause is not
32129 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32130 token is not consumed. Otherwise, the appropriate enum from the
32131 pragma_simd_clause is returned and the token is consumed. */
32133 static pragma_omp_clause
32134 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32136 pragma_omp_clause clause_type;
32137 cp_token *token = cp_lexer_peek_token (parser->lexer);
32139 if (token->keyword == RID_PRIVATE)
32140 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32141 else if (!token->u.value || token->type != CPP_NAME)
32142 return PRAGMA_CILK_CLAUSE_NONE;
32143 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32144 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32145 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32146 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32147 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32148 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32149 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32150 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32151 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32152 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32153 else
32154 return PRAGMA_CILK_CLAUSE_NONE;
32156 cp_lexer_consume_token (parser->lexer);
32157 return clause_type;
32160 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32162 static tree
32163 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32165 tree clauses = NULL_TREE;
32167 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32168 && clauses != error_mark_node)
32170 pragma_omp_clause c_kind;
32171 c_kind = cp_parser_cilk_simd_clause_name (parser);
32172 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32173 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32174 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32175 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32176 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32177 /* Use the OpenMP 4.0 equivalent function. */
32178 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32179 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32180 /* Use the OpenMP 4.0 equivalent function. */
32181 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32182 clauses);
32183 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32184 /* Use the OMP 4.0 equivalent function. */
32185 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32186 clauses);
32187 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32188 /* Use the OMP 4.0 equivalent function. */
32189 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32190 else
32192 clauses = error_mark_node;
32193 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32194 break;
32198 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32200 if (clauses == error_mark_node)
32201 return error_mark_node;
32202 else
32203 return c_finish_cilk_clauses (clauses);
32206 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32208 static void
32209 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32211 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32213 if (clauses == error_mark_node)
32214 return;
32216 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32218 error_at (cp_lexer_peek_token (parser->lexer)->location,
32219 "for statement expected");
32220 return;
32223 tree sb = begin_omp_structured_block ();
32224 int save = cp_parser_begin_omp_structured_block (parser);
32225 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32226 if (ret)
32227 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32228 cp_parser_end_omp_structured_block (parser, save);
32229 add_stmt (finish_omp_structured_block (sb));
32230 return;
32233 /* Create an identifier for a generic parameter type (a synthesized
32234 template parameter implied by `auto' or a concept identifier). */
32236 static GTY(()) int generic_parm_count;
32237 static tree
32238 make_generic_type_name ()
32240 char buf[32];
32241 sprintf (buf, "auto:%d", ++generic_parm_count);
32242 return get_identifier (buf);
32245 /* Predicate that behaves as is_auto_or_concept but matches the parent
32246 node of the generic type rather than the generic type itself. This
32247 allows for type transformation in add_implicit_template_parms. */
32249 static inline bool
32250 tree_type_is_auto_or_concept (const_tree t)
32252 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32255 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32256 (creating a new template parameter list if necessary). Returns the newly
32257 created template type parm. */
32259 tree
32260 synthesize_implicit_template_parm (cp_parser *parser)
32262 gcc_assert (current_binding_level->kind == sk_function_parms);
32264 /* We are either continuing a function template that already contains implicit
32265 template parameters, creating a new fully-implicit function template, or
32266 extending an existing explicit function template with implicit template
32267 parameters. */
32269 cp_binding_level *const entry_scope = current_binding_level;
32271 bool become_template = false;
32272 cp_binding_level *parent_scope = 0;
32274 if (parser->implicit_template_scope)
32276 gcc_assert (parser->implicit_template_parms);
32278 current_binding_level = parser->implicit_template_scope;
32280 else
32282 /* Roll back to the existing template parameter scope (in the case of
32283 extending an explicit function template) or introduce a new template
32284 parameter scope ahead of the function parameter scope (or class scope
32285 in the case of out-of-line member definitions). The function scope is
32286 added back after template parameter synthesis below. */
32288 cp_binding_level *scope = entry_scope;
32290 while (scope->kind == sk_function_parms)
32292 parent_scope = scope;
32293 scope = scope->level_chain;
32295 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32297 /* If not defining a class, then any class scope is a scope level in
32298 an out-of-line member definition. In this case simply wind back
32299 beyond the first such scope to inject the template parameter list.
32300 Otherwise wind back to the class being defined. The latter can
32301 occur in class member friend declarations such as:
32303 class A {
32304 void foo (auto);
32306 class B {
32307 friend void A::foo (auto);
32310 The template parameter list synthesized for the friend declaration
32311 must be injected in the scope of 'B'. This can also occur in
32312 erroneous cases such as:
32314 struct A {
32315 struct B {
32316 void foo (auto);
32318 void B::foo (auto) {}
32321 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32322 but, nevertheless, the template parameter list synthesized for the
32323 declarator should be injected into the scope of 'A' as if the
32324 ill-formed template was specified explicitly. */
32326 while (scope->kind == sk_class && !scope->defining_class_p)
32328 parent_scope = scope;
32329 scope = scope->level_chain;
32333 current_binding_level = scope;
32335 if (scope->kind != sk_template_parms
32336 || !function_being_declared_is_template_p (parser))
32338 /* Introduce a new template parameter list for implicit template
32339 parameters. */
32341 become_template = true;
32343 parser->implicit_template_scope
32344 = begin_scope (sk_template_parms, NULL);
32346 ++processing_template_decl;
32348 parser->fully_implicit_function_template_p = true;
32349 ++parser->num_template_parameter_lists;
32351 else
32353 /* Synthesize implicit template parameters at the end of the explicit
32354 template parameter list. */
32356 gcc_assert (current_template_parms);
32358 parser->implicit_template_scope = scope;
32360 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32361 parser->implicit_template_parms
32362 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32366 /* Synthesize a new template parameter and track the current template
32367 parameter chain with implicit_template_parms. */
32369 tree synth_id = make_generic_type_name ();
32370 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32371 synth_id);
32372 tree new_parm
32373 = process_template_parm (parser->implicit_template_parms,
32374 input_location,
32375 build_tree_list (NULL_TREE, synth_tmpl_parm),
32376 /*non_type=*/false,
32377 /*param_pack=*/false);
32380 if (parser->implicit_template_parms)
32381 parser->implicit_template_parms
32382 = TREE_CHAIN (parser->implicit_template_parms);
32383 else
32384 parser->implicit_template_parms = new_parm;
32386 tree new_type = TREE_TYPE (getdecls ());
32388 /* If creating a fully implicit function template, start the new implicit
32389 template parameter list with this synthesized type, otherwise grow the
32390 current template parameter list. */
32392 if (become_template)
32394 parent_scope->level_chain = current_binding_level;
32396 tree new_parms = make_tree_vec (1);
32397 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32398 current_template_parms = tree_cons (size_int (processing_template_decl),
32399 new_parms, current_template_parms);
32401 else
32403 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32404 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32405 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32406 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32409 current_binding_level = entry_scope;
32411 return new_type;
32414 /* Finish the declaration of a fully implicit function template. Such a
32415 template has no explicit template parameter list so has not been through the
32416 normal template head and tail processing. synthesize_implicit_template_parm
32417 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32418 provided if the declaration is a class member such that its template
32419 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32420 form is returned. Otherwise NULL_TREE is returned. */
32422 tree
32423 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32425 gcc_assert (parser->fully_implicit_function_template_p);
32427 if (member_decl_opt && member_decl_opt != error_mark_node
32428 && DECL_VIRTUAL_P (member_decl_opt))
32430 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32431 "implicit templates may not be %<virtual%>");
32432 DECL_VIRTUAL_P (member_decl_opt) = false;
32435 if (member_decl_opt)
32436 member_decl_opt = finish_member_template_decl (member_decl_opt);
32437 end_template_decl ();
32439 parser->fully_implicit_function_template_p = false;
32440 --parser->num_template_parameter_lists;
32442 return member_decl_opt;
32445 #include "gt-cp-parser.h"