Concretize gimple_cond_set_code
[official-gcc.git] / gcc / cp / parser.c
blobc6218af21e9406a163dbbffd3273b6ca91484032
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 tree cp_parser_cilk_for
243 (cp_parser *, tree);
244 static bool cp_parser_omp_declare_reduction_exprs
245 (tree, cp_parser *);
246 static tree cp_parser_cilk_simd_vectorlength
247 (cp_parser *, tree, bool);
249 /* Manifest constants. */
250 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
251 #define CP_SAVED_TOKEN_STACK 5
253 /* Variables. */
255 /* The stream to which debugging output should be written. */
256 static FILE *cp_lexer_debug_stream;
258 /* Nonzero if we are parsing an unevaluated operand: an operand to
259 sizeof, typeof, or alignof. */
260 int cp_unevaluated_operand;
262 /* Dump up to NUM tokens in BUFFER to FILE starting with token
263 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
264 first token in BUFFER. If NUM is 0, dump all the tokens. If
265 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
266 highlighted by surrounding it in [[ ]]. */
268 static void
269 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
270 cp_token *start_token, unsigned num,
271 cp_token *curr_token)
273 unsigned i, nprinted;
274 cp_token *token;
275 bool do_print;
277 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
279 if (buffer == NULL)
280 return;
282 if (num == 0)
283 num = buffer->length ();
285 if (start_token == NULL)
286 start_token = buffer->address ();
288 if (start_token > buffer->address ())
290 cp_lexer_print_token (file, &(*buffer)[0]);
291 fprintf (file, " ... ");
294 do_print = false;
295 nprinted = 0;
296 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
298 if (token == start_token)
299 do_print = true;
301 if (!do_print)
302 continue;
304 nprinted++;
305 if (token == curr_token)
306 fprintf (file, "[[");
308 cp_lexer_print_token (file, token);
310 if (token == curr_token)
311 fprintf (file, "]]");
313 switch (token->type)
315 case CPP_SEMICOLON:
316 case CPP_OPEN_BRACE:
317 case CPP_CLOSE_BRACE:
318 case CPP_EOF:
319 fputc ('\n', file);
320 break;
322 default:
323 fputc (' ', file);
327 if (i == num && i < buffer->length ())
329 fprintf (file, " ... ");
330 cp_lexer_print_token (file, &buffer->last ());
333 fprintf (file, "\n");
337 /* Dump all tokens in BUFFER to stderr. */
339 void
340 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
342 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
345 DEBUG_FUNCTION void
346 debug (vec<cp_token, va_gc> &ref)
348 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
351 DEBUG_FUNCTION void
352 debug (vec<cp_token, va_gc> *ptr)
354 if (ptr)
355 debug (*ptr);
356 else
357 fprintf (stderr, "<nil>\n");
361 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
362 description for T. */
364 static void
365 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
367 if (t)
369 fprintf (file, "%s: ", desc);
370 print_node_brief (file, "", t, 0);
375 /* Dump parser context C to FILE. */
377 static void
378 cp_debug_print_context (FILE *file, cp_parser_context *c)
380 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
381 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
382 print_node_brief (file, "", c->object_type, 0);
383 fprintf (file, "}\n");
387 /* Print the stack of parsing contexts to FILE starting with FIRST. */
389 static void
390 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
392 unsigned i;
393 cp_parser_context *c;
395 fprintf (file, "Parsing context stack:\n");
396 for (i = 0, c = first; c; c = c->next, i++)
398 fprintf (file, "\t#%u: ", i);
399 cp_debug_print_context (file, c);
404 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
406 static void
407 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
409 if (flag)
410 fprintf (file, "%s: true\n", desc);
414 /* Print an unparsed function entry UF to FILE. */
416 static void
417 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
419 unsigned i;
420 cp_default_arg_entry *default_arg_fn;
421 tree fn;
423 fprintf (file, "\tFunctions with default args:\n");
424 for (i = 0;
425 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
426 i++)
428 fprintf (file, "\t\tClass type: ");
429 print_node_brief (file, "", default_arg_fn->class_type, 0);
430 fprintf (file, "\t\tDeclaration: ");
431 print_node_brief (file, "", default_arg_fn->decl, 0);
432 fprintf (file, "\n");
435 fprintf (file, "\n\tFunctions with definitions that require "
436 "post-processing\n\t\t");
437 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
439 print_node_brief (file, "", fn, 0);
440 fprintf (file, " ");
442 fprintf (file, "\n");
444 fprintf (file, "\n\tNon-static data members with initializers that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
455 /* Print the stack of unparsed member functions S to FILE. */
457 static void
458 cp_debug_print_unparsed_queues (FILE *file,
459 vec<cp_unparsed_functions_entry, va_gc> *s)
461 unsigned i;
462 cp_unparsed_functions_entry *uf;
464 fprintf (file, "Unparsed functions\n");
465 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
467 fprintf (file, "#%u:\n", i);
468 cp_debug_print_unparsed_function (file, uf);
473 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
474 the given PARSER. If FILE is NULL, the output is printed on stderr. */
476 static void
477 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
479 cp_token *next_token, *first_token, *start_token;
481 if (file == NULL)
482 file = stderr;
484 next_token = parser->lexer->next_token;
485 first_token = parser->lexer->buffer->address ();
486 start_token = (next_token > first_token + window_size / 2)
487 ? next_token - window_size / 2
488 : first_token;
489 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
490 next_token);
494 /* Dump debugging information for the given PARSER. If FILE is NULL,
495 the output is printed on stderr. */
497 void
498 cp_debug_parser (FILE *file, cp_parser *parser)
500 const size_t window_size = 20;
501 cp_token *token;
502 expanded_location eloc;
504 if (file == NULL)
505 file = stderr;
507 fprintf (file, "Parser state\n\n");
508 fprintf (file, "Number of tokens: %u\n",
509 vec_safe_length (parser->lexer->buffer));
510 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
511 cp_debug_print_tree_if_set (file, "Object scope",
512 parser->object_scope);
513 cp_debug_print_tree_if_set (file, "Qualifying scope",
514 parser->qualifying_scope);
515 cp_debug_print_context_stack (file, parser->context);
516 cp_debug_print_flag (file, "Allow GNU extensions",
517 parser->allow_gnu_extensions_p);
518 cp_debug_print_flag (file, "'>' token is greater-than",
519 parser->greater_than_is_operator_p);
520 cp_debug_print_flag (file, "Default args allowed in current "
521 "parameter list", parser->default_arg_ok_p);
522 cp_debug_print_flag (file, "Parsing integral constant-expression",
523 parser->integral_constant_expression_p);
524 cp_debug_print_flag (file, "Allow non-constant expression in current "
525 "constant-expression",
526 parser->allow_non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Seen non-constant expression",
528 parser->non_integral_constant_expression_p);
529 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
530 "current context",
531 parser->local_variables_forbidden_p);
532 cp_debug_print_flag (file, "In unbraced linkage specification",
533 parser->in_unbraced_linkage_specification_p);
534 cp_debug_print_flag (file, "Parsing a declarator",
535 parser->in_declarator_p);
536 cp_debug_print_flag (file, "In template argument list",
537 parser->in_template_argument_list_p);
538 cp_debug_print_flag (file, "Parsing an iteration statement",
539 parser->in_statement & IN_ITERATION_STMT);
540 cp_debug_print_flag (file, "Parsing a switch statement",
541 parser->in_statement & IN_SWITCH_STMT);
542 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
543 parser->in_statement & IN_OMP_BLOCK);
544 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
545 parser->in_statement & IN_CILK_SIMD_FOR);
546 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
547 parser->in_statement & IN_OMP_FOR);
548 cp_debug_print_flag (file, "Parsing an if statement",
549 parser->in_statement & IN_IF_STMT);
550 cp_debug_print_flag (file, "Parsing a type-id in an expression "
551 "context", parser->in_type_id_in_expr_p);
552 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
553 parser->implicit_extern_c);
554 cp_debug_print_flag (file, "String expressions should be translated "
555 "to execution character set",
556 parser->translate_strings_p);
557 cp_debug_print_flag (file, "Parsing function body outside of a "
558 "local class", parser->in_function_body);
559 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
560 parser->colon_corrects_to_scope_p);
561 cp_debug_print_flag (file, "Colon doesn't start a class definition",
562 parser->colon_doesnt_start_class_def_p);
563 if (parser->type_definition_forbidden_message)
564 fprintf (file, "Error message for forbidden type definitions: %s\n",
565 parser->type_definition_forbidden_message);
566 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
567 fprintf (file, "Number of class definitions in progress: %u\n",
568 parser->num_classes_being_defined);
569 fprintf (file, "Number of template parameter lists for the current "
570 "declaration: %u\n", parser->num_template_parameter_lists);
571 cp_debug_parser_tokens (file, parser, window_size);
572 token = parser->lexer->next_token;
573 fprintf (file, "Next token to parse:\n");
574 fprintf (file, "\tToken: ");
575 cp_lexer_print_token (file, token);
576 eloc = expand_location (token->location);
577 fprintf (file, "\n\tFile: %s\n", eloc.file);
578 fprintf (file, "\tLine: %d\n", eloc.line);
579 fprintf (file, "\tColumn: %d\n", eloc.column);
582 DEBUG_FUNCTION void
583 debug (cp_parser &ref)
585 cp_debug_parser (stderr, &ref);
588 DEBUG_FUNCTION void
589 debug (cp_parser *ptr)
591 if (ptr)
592 debug (*ptr);
593 else
594 fprintf (stderr, "<nil>\n");
597 /* Allocate memory for a new lexer object and return it. */
599 static cp_lexer *
600 cp_lexer_alloc (void)
602 cp_lexer *lexer;
604 c_common_no_more_pch ();
606 /* Allocate the memory. */
607 lexer = ggc_cleared_alloc<cp_lexer> ();
609 /* Initially we are not debugging. */
610 lexer->debugging_p = false;
612 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
614 /* Create the buffer. */
615 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
617 return lexer;
621 /* Create a new main C++ lexer, the lexer that gets tokens from the
622 preprocessor. */
624 static cp_lexer *
625 cp_lexer_new_main (void)
627 cp_lexer *lexer;
628 cp_token token;
630 /* It's possible that parsing the first pragma will load a PCH file,
631 which is a GC collection point. So we have to do that before
632 allocating any memory. */
633 cp_parser_initial_pragma (&token);
635 lexer = cp_lexer_alloc ();
637 /* Put the first token in the buffer. */
638 lexer->buffer->quick_push (token);
640 /* Get the remaining tokens from the preprocessor. */
641 while (token.type != CPP_EOF)
643 cp_lexer_get_preprocessor_token (lexer, &token);
644 vec_safe_push (lexer->buffer, token);
647 lexer->last_token = lexer->buffer->address ()
648 + lexer->buffer->length ()
649 - 1;
650 lexer->next_token = lexer->buffer->length ()
651 ? lexer->buffer->address ()
652 : &eof_token;
654 /* Subsequent preprocessor diagnostics should use compiler
655 diagnostic functions to get the compiler source location. */
656 done_lexing = true;
658 gcc_assert (!lexer->next_token->purged_p);
659 return lexer;
662 /* Create a new lexer whose token stream is primed with the tokens in
663 CACHE. When these tokens are exhausted, no new tokens will be read. */
665 static cp_lexer *
666 cp_lexer_new_from_tokens (cp_token_cache *cache)
668 cp_token *first = cache->first;
669 cp_token *last = cache->last;
670 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
672 /* We do not own the buffer. */
673 lexer->buffer = NULL;
674 lexer->next_token = first == last ? &eof_token : first;
675 lexer->last_token = last;
677 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
679 /* Initially we are not debugging. */
680 lexer->debugging_p = false;
682 gcc_assert (!lexer->next_token->purged_p);
683 return lexer;
686 /* Frees all resources associated with LEXER. */
688 static void
689 cp_lexer_destroy (cp_lexer *lexer)
691 vec_free (lexer->buffer);
692 lexer->saved_tokens.release ();
693 ggc_free (lexer);
696 /* Returns nonzero if debugging information should be output. */
698 static inline bool
699 cp_lexer_debugging_p (cp_lexer *lexer)
701 return lexer->debugging_p;
705 static inline cp_token_position
706 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
708 gcc_assert (!previous_p || lexer->next_token != &eof_token);
710 return lexer->next_token - previous_p;
713 static inline cp_token *
714 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
716 return pos;
719 static inline void
720 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
722 lexer->next_token = cp_lexer_token_at (lexer, pos);
725 static inline cp_token_position
726 cp_lexer_previous_token_position (cp_lexer *lexer)
728 if (lexer->next_token == &eof_token)
729 return lexer->last_token - 1;
730 else
731 return cp_lexer_token_position (lexer, true);
734 static inline cp_token *
735 cp_lexer_previous_token (cp_lexer *lexer)
737 cp_token_position tp = cp_lexer_previous_token_position (lexer);
739 return cp_lexer_token_at (lexer, tp);
742 /* nonzero if we are presently saving tokens. */
744 static inline int
745 cp_lexer_saving_tokens (const cp_lexer* lexer)
747 return lexer->saved_tokens.length () != 0;
750 /* Store the next token from the preprocessor in *TOKEN. Return true
751 if we reach EOF. If LEXER is NULL, assume we are handling an
752 initial #pragma pch_preprocess, and thus want the lexer to return
753 processed strings. */
755 static void
756 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
758 static int is_extern_c = 0;
760 /* Get a new token from the preprocessor. */
761 token->type
762 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
763 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
764 token->keyword = RID_MAX;
765 token->pragma_kind = PRAGMA_NONE;
766 token->purged_p = false;
767 token->error_reported = false;
769 /* On some systems, some header files are surrounded by an
770 implicit extern "C" block. Set a flag in the token if it
771 comes from such a header. */
772 is_extern_c += pending_lang_change;
773 pending_lang_change = 0;
774 token->implicit_extern_c = is_extern_c > 0;
776 /* Check to see if this token is a keyword. */
777 if (token->type == CPP_NAME)
779 if (C_IS_RESERVED_WORD (token->u.value))
781 /* Mark this token as a keyword. */
782 token->type = CPP_KEYWORD;
783 /* Record which keyword. */
784 token->keyword = C_RID_CODE (token->u.value);
786 else
788 if (warn_cxx0x_compat
789 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
790 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
792 /* Warn about the C++0x keyword (but still treat it as
793 an identifier). */
794 warning (OPT_Wc__0x_compat,
795 "identifier %qE is a keyword in C++11",
796 token->u.value);
798 /* Clear out the C_RID_CODE so we don't warn about this
799 particular identifier-turned-keyword again. */
800 C_SET_RID_CODE (token->u.value, RID_MAX);
803 token->keyword = RID_MAX;
806 else if (token->type == CPP_AT_NAME)
808 /* This only happens in Objective-C++; it must be a keyword. */
809 token->type = CPP_KEYWORD;
810 switch (C_RID_CODE (token->u.value))
812 /* Replace 'class' with '@class', 'private' with '@private',
813 etc. This prevents confusion with the C++ keyword
814 'class', and makes the tokens consistent with other
815 Objective-C 'AT' keywords. For example '@class' is
816 reported as RID_AT_CLASS which is consistent with
817 '@synchronized', which is reported as
818 RID_AT_SYNCHRONIZED.
820 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
821 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
822 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
823 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
824 case RID_THROW: token->keyword = RID_AT_THROW; break;
825 case RID_TRY: token->keyword = RID_AT_TRY; break;
826 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
827 default: token->keyword = C_RID_CODE (token->u.value);
830 else if (token->type == CPP_PRAGMA)
832 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
833 token->pragma_kind = ((enum pragma_kind)
834 TREE_INT_CST_LOW (token->u.value));
835 token->u.value = NULL_TREE;
839 /* Update the globals input_location and the input file stack from TOKEN. */
840 static inline void
841 cp_lexer_set_source_position_from_token (cp_token *token)
843 if (token->type != CPP_EOF)
845 input_location = token->location;
849 /* Update the globals input_location and the input file stack from LEXER. */
850 static inline void
851 cp_lexer_set_source_position (cp_lexer *lexer)
853 cp_token *token = cp_lexer_peek_token (lexer);
854 cp_lexer_set_source_position_from_token (token);
857 /* Return a pointer to the next token in the token stream, but do not
858 consume it. */
860 static inline cp_token *
861 cp_lexer_peek_token (cp_lexer *lexer)
863 if (cp_lexer_debugging_p (lexer))
865 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
866 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
867 putc ('\n', cp_lexer_debug_stream);
869 return lexer->next_token;
872 /* Return true if the next token has the indicated TYPE. */
874 static inline bool
875 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
877 return cp_lexer_peek_token (lexer)->type == type;
880 /* Return true if the next token does not have the indicated TYPE. */
882 static inline bool
883 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
885 return !cp_lexer_next_token_is (lexer, type);
888 /* Return true if the next token is the indicated KEYWORD. */
890 static inline bool
891 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
893 return cp_lexer_peek_token (lexer)->keyword == keyword;
896 static inline bool
897 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
899 return cp_lexer_peek_nth_token (lexer, n)->type == type;
902 static inline bool
903 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
905 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
908 /* Return true if the next token is not the indicated KEYWORD. */
910 static inline bool
911 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
913 return cp_lexer_peek_token (lexer)->keyword != keyword;
916 /* Return true if the next token is a keyword for a decl-specifier. */
918 static bool
919 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
921 cp_token *token;
923 token = cp_lexer_peek_token (lexer);
924 switch (token->keyword)
926 /* auto specifier: storage-class-specifier in C++,
927 simple-type-specifier in C++0x. */
928 case RID_AUTO:
929 /* Storage classes. */
930 case RID_REGISTER:
931 case RID_STATIC:
932 case RID_EXTERN:
933 case RID_MUTABLE:
934 case RID_THREAD:
935 /* Elaborated type specifiers. */
936 case RID_ENUM:
937 case RID_CLASS:
938 case RID_STRUCT:
939 case RID_UNION:
940 case RID_TYPENAME:
941 /* Simple type specifiers. */
942 case RID_CHAR:
943 case RID_CHAR16:
944 case RID_CHAR32:
945 case RID_WCHAR:
946 case RID_BOOL:
947 case RID_SHORT:
948 case RID_INT:
949 case RID_LONG:
950 case RID_INT128:
951 case RID_SIGNED:
952 case RID_UNSIGNED:
953 case RID_FLOAT:
954 case RID_DOUBLE:
955 case RID_VOID:
956 /* GNU extensions. */
957 case RID_ATTRIBUTE:
958 case RID_TYPEOF:
959 /* C++0x extensions. */
960 case RID_DECLTYPE:
961 case RID_UNDERLYING_TYPE:
962 return true;
964 default:
965 return false;
969 /* Returns TRUE iff the token T begins a decltype type. */
971 static bool
972 token_is_decltype (cp_token *t)
974 return (t->keyword == RID_DECLTYPE
975 || t->type == CPP_DECLTYPE);
978 /* Returns TRUE iff the next token begins a decltype type. */
980 static bool
981 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
983 cp_token *t = cp_lexer_peek_token (lexer);
984 return token_is_decltype (t);
987 /* Return a pointer to the Nth token in the token stream. If N is 1,
988 then this is precisely equivalent to cp_lexer_peek_token (except
989 that it is not inline). One would like to disallow that case, but
990 there is one case (cp_parser_nth_token_starts_template_id) where
991 the caller passes a variable for N and it might be 1. */
993 static cp_token *
994 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
996 cp_token *token;
998 /* N is 1-based, not zero-based. */
999 gcc_assert (n > 0);
1001 if (cp_lexer_debugging_p (lexer))
1002 fprintf (cp_lexer_debug_stream,
1003 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1005 --n;
1006 token = lexer->next_token;
1007 gcc_assert (!n || token != &eof_token);
1008 while (n != 0)
1010 ++token;
1011 if (token == lexer->last_token)
1013 token = &eof_token;
1014 break;
1017 if (!token->purged_p)
1018 --n;
1021 if (cp_lexer_debugging_p (lexer))
1023 cp_lexer_print_token (cp_lexer_debug_stream, token);
1024 putc ('\n', cp_lexer_debug_stream);
1027 return token;
1030 /* Return the next token, and advance the lexer's next_token pointer
1031 to point to the next non-purged token. */
1033 static cp_token *
1034 cp_lexer_consume_token (cp_lexer* lexer)
1036 cp_token *token = lexer->next_token;
1038 gcc_assert (token != &eof_token);
1039 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1043 lexer->next_token++;
1044 if (lexer->next_token == lexer->last_token)
1046 lexer->next_token = &eof_token;
1047 break;
1051 while (lexer->next_token->purged_p);
1053 cp_lexer_set_source_position_from_token (token);
1055 /* Provide debugging output. */
1056 if (cp_lexer_debugging_p (lexer))
1058 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1059 cp_lexer_print_token (cp_lexer_debug_stream, token);
1060 putc ('\n', cp_lexer_debug_stream);
1063 return token;
1066 /* Permanently remove the next token from the token stream, and
1067 advance the next_token pointer to refer to the next non-purged
1068 token. */
1070 static void
1071 cp_lexer_purge_token (cp_lexer *lexer)
1073 cp_token *tok = lexer->next_token;
1075 gcc_assert (tok != &eof_token);
1076 tok->purged_p = true;
1077 tok->location = UNKNOWN_LOCATION;
1078 tok->u.value = NULL_TREE;
1079 tok->keyword = RID_MAX;
1083 tok++;
1084 if (tok == lexer->last_token)
1086 tok = &eof_token;
1087 break;
1090 while (tok->purged_p);
1091 lexer->next_token = tok;
1094 /* Permanently remove all tokens after TOK, up to, but not
1095 including, the token that will be returned next by
1096 cp_lexer_peek_token. */
1098 static void
1099 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1101 cp_token *peek = lexer->next_token;
1103 if (peek == &eof_token)
1104 peek = lexer->last_token;
1106 gcc_assert (tok < peek);
1108 for ( tok += 1; tok != peek; tok += 1)
1110 tok->purged_p = true;
1111 tok->location = UNKNOWN_LOCATION;
1112 tok->u.value = NULL_TREE;
1113 tok->keyword = RID_MAX;
1117 /* Begin saving tokens. All tokens consumed after this point will be
1118 preserved. */
1120 static void
1121 cp_lexer_save_tokens (cp_lexer* lexer)
1123 /* Provide debugging output. */
1124 if (cp_lexer_debugging_p (lexer))
1125 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1127 lexer->saved_tokens.safe_push (lexer->next_token);
1130 /* Commit to the portion of the token stream most recently saved. */
1132 static void
1133 cp_lexer_commit_tokens (cp_lexer* lexer)
1135 /* Provide debugging output. */
1136 if (cp_lexer_debugging_p (lexer))
1137 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1139 lexer->saved_tokens.pop ();
1142 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1143 to the token stream. Stop saving tokens. */
1145 static void
1146 cp_lexer_rollback_tokens (cp_lexer* lexer)
1148 /* Provide debugging output. */
1149 if (cp_lexer_debugging_p (lexer))
1150 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1152 lexer->next_token = lexer->saved_tokens.pop ();
1155 /* Print a representation of the TOKEN on the STREAM. */
1157 static void
1158 cp_lexer_print_token (FILE * stream, cp_token *token)
1160 /* We don't use cpp_type2name here because the parser defines
1161 a few tokens of its own. */
1162 static const char *const token_names[] = {
1163 /* cpplib-defined token types */
1164 #define OP(e, s) #e,
1165 #define TK(e, s) #e,
1166 TTYPE_TABLE
1167 #undef OP
1168 #undef TK
1169 /* C++ parser token types - see "Manifest constants", above. */
1170 "KEYWORD",
1171 "TEMPLATE_ID",
1172 "NESTED_NAME_SPECIFIER",
1175 /* For some tokens, print the associated data. */
1176 switch (token->type)
1178 case CPP_KEYWORD:
1179 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1180 For example, `struct' is mapped to an INTEGER_CST. */
1181 if (!identifier_p (token->u.value))
1182 break;
1183 /* else fall through */
1184 case CPP_NAME:
1185 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1186 break;
1188 case CPP_STRING:
1189 case CPP_STRING16:
1190 case CPP_STRING32:
1191 case CPP_WSTRING:
1192 case CPP_UTF8STRING:
1193 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1194 break;
1196 case CPP_NUMBER:
1197 print_generic_expr (stream, token->u.value, 0);
1198 break;
1200 default:
1201 /* If we have a name for the token, print it out. Otherwise, we
1202 simply give the numeric code. */
1203 if (token->type < ARRAY_SIZE(token_names))
1204 fputs (token_names[token->type], stream);
1205 else
1206 fprintf (stream, "[%d]", token->type);
1207 break;
1211 DEBUG_FUNCTION void
1212 debug (cp_token &ref)
1214 cp_lexer_print_token (stderr, &ref);
1215 fprintf (stderr, "\n");
1218 DEBUG_FUNCTION void
1219 debug (cp_token *ptr)
1221 if (ptr)
1222 debug (*ptr);
1223 else
1224 fprintf (stderr, "<nil>\n");
1228 /* Start emitting debugging information. */
1230 static void
1231 cp_lexer_start_debugging (cp_lexer* lexer)
1233 lexer->debugging_p = true;
1234 cp_lexer_debug_stream = stderr;
1237 /* Stop emitting debugging information. */
1239 static void
1240 cp_lexer_stop_debugging (cp_lexer* lexer)
1242 lexer->debugging_p = false;
1243 cp_lexer_debug_stream = NULL;
1246 /* Create a new cp_token_cache, representing a range of tokens. */
1248 static cp_token_cache *
1249 cp_token_cache_new (cp_token *first, cp_token *last)
1251 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1252 cache->first = first;
1253 cache->last = last;
1254 return cache;
1257 /* Diagnose if #pragma omp declare simd isn't followed immediately
1258 by function declaration or definition. */
1260 static inline void
1261 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1263 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1265 error ("%<#pragma omp declare simd%> not immediately followed by "
1266 "function declaration or definition");
1267 parser->omp_declare_simd = NULL;
1271 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1272 and put that into "omp declare simd" attribute. */
1274 static inline void
1275 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1277 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1279 if (fndecl == error_mark_node)
1281 parser->omp_declare_simd = NULL;
1282 return;
1284 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1286 cp_ensure_no_omp_declare_simd (parser);
1287 return;
1292 /* Decl-specifiers. */
1294 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1296 static void
1297 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1299 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1302 /* Declarators. */
1304 /* Nothing other than the parser should be creating declarators;
1305 declarators are a semi-syntactic representation of C++ entities.
1306 Other parts of the front end that need to create entities (like
1307 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1309 static cp_declarator *make_call_declarator
1310 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1311 static cp_declarator *make_array_declarator
1312 (cp_declarator *, tree);
1313 static cp_declarator *make_pointer_declarator
1314 (cp_cv_quals, cp_declarator *, tree);
1315 static cp_declarator *make_reference_declarator
1316 (cp_cv_quals, cp_declarator *, bool, tree);
1317 static cp_parameter_declarator *make_parameter_declarator
1318 (cp_decl_specifier_seq *, cp_declarator *, tree);
1319 static cp_declarator *make_ptrmem_declarator
1320 (cp_cv_quals, tree, cp_declarator *, tree);
1322 /* An erroneous declarator. */
1323 static cp_declarator *cp_error_declarator;
1325 /* The obstack on which declarators and related data structures are
1326 allocated. */
1327 static struct obstack declarator_obstack;
1329 /* Alloc BYTES from the declarator memory pool. */
1331 static inline void *
1332 alloc_declarator (size_t bytes)
1334 return obstack_alloc (&declarator_obstack, bytes);
1337 /* Allocate a declarator of the indicated KIND. Clear fields that are
1338 common to all declarators. */
1340 static cp_declarator *
1341 make_declarator (cp_declarator_kind kind)
1343 cp_declarator *declarator;
1345 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1346 declarator->kind = kind;
1347 declarator->attributes = NULL_TREE;
1348 declarator->std_attributes = NULL_TREE;
1349 declarator->declarator = NULL;
1350 declarator->parameter_pack_p = false;
1351 declarator->id_loc = UNKNOWN_LOCATION;
1353 return declarator;
1356 /* Make a declarator for a generalized identifier. If
1357 QUALIFYING_SCOPE is non-NULL, the identifier is
1358 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1359 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1360 is, if any. */
1362 static cp_declarator *
1363 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1364 special_function_kind sfk)
1366 cp_declarator *declarator;
1368 /* It is valid to write:
1370 class C { void f(); };
1371 typedef C D;
1372 void D::f();
1374 The standard is not clear about whether `typedef const C D' is
1375 legal; as of 2002-09-15 the committee is considering that
1376 question. EDG 3.0 allows that syntax. Therefore, we do as
1377 well. */
1378 if (qualifying_scope && TYPE_P (qualifying_scope))
1379 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1381 gcc_assert (identifier_p (unqualified_name)
1382 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1383 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1385 declarator = make_declarator (cdk_id);
1386 declarator->u.id.qualifying_scope = qualifying_scope;
1387 declarator->u.id.unqualified_name = unqualified_name;
1388 declarator->u.id.sfk = sfk;
1390 return declarator;
1393 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1394 of modifiers such as const or volatile to apply to the pointer
1395 type, represented as identifiers. ATTRIBUTES represent the attributes that
1396 appertain to the pointer or reference. */
1398 cp_declarator *
1399 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1400 tree attributes)
1402 cp_declarator *declarator;
1404 declarator = make_declarator (cdk_pointer);
1405 declarator->declarator = target;
1406 declarator->u.pointer.qualifiers = cv_qualifiers;
1407 declarator->u.pointer.class_type = NULL_TREE;
1408 if (target)
1410 declarator->id_loc = target->id_loc;
1411 declarator->parameter_pack_p = target->parameter_pack_p;
1412 target->parameter_pack_p = false;
1414 else
1415 declarator->parameter_pack_p = false;
1417 declarator->std_attributes = attributes;
1419 return declarator;
1422 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1423 represent the attributes that appertain to the pointer or
1424 reference. */
1426 cp_declarator *
1427 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1428 bool rvalue_ref, tree attributes)
1430 cp_declarator *declarator;
1432 declarator = make_declarator (cdk_reference);
1433 declarator->declarator = target;
1434 declarator->u.reference.qualifiers = cv_qualifiers;
1435 declarator->u.reference.rvalue_ref = rvalue_ref;
1436 if (target)
1438 declarator->id_loc = target->id_loc;
1439 declarator->parameter_pack_p = target->parameter_pack_p;
1440 target->parameter_pack_p = false;
1442 else
1443 declarator->parameter_pack_p = false;
1445 declarator->std_attributes = attributes;
1447 return declarator;
1450 /* Like make_pointer_declarator -- but for a pointer to a non-static
1451 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1452 appertain to the pointer or reference. */
1454 cp_declarator *
1455 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1456 cp_declarator *pointee,
1457 tree attributes)
1459 cp_declarator *declarator;
1461 declarator = make_declarator (cdk_ptrmem);
1462 declarator->declarator = pointee;
1463 declarator->u.pointer.qualifiers = cv_qualifiers;
1464 declarator->u.pointer.class_type = class_type;
1466 if (pointee)
1468 declarator->parameter_pack_p = pointee->parameter_pack_p;
1469 pointee->parameter_pack_p = false;
1471 else
1472 declarator->parameter_pack_p = false;
1474 declarator->std_attributes = attributes;
1476 return declarator;
1479 /* Make a declarator for the function given by TARGET, with the
1480 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1481 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1482 indicates what exceptions can be thrown. */
1484 cp_declarator *
1485 make_call_declarator (cp_declarator *target,
1486 tree parms,
1487 cp_cv_quals cv_qualifiers,
1488 cp_virt_specifiers virt_specifiers,
1489 cp_ref_qualifier ref_qualifier,
1490 tree exception_specification,
1491 tree late_return_type)
1493 cp_declarator *declarator;
1495 declarator = make_declarator (cdk_function);
1496 declarator->declarator = target;
1497 declarator->u.function.parameters = parms;
1498 declarator->u.function.qualifiers = cv_qualifiers;
1499 declarator->u.function.virt_specifiers = virt_specifiers;
1500 declarator->u.function.ref_qualifier = ref_qualifier;
1501 declarator->u.function.exception_specification = exception_specification;
1502 declarator->u.function.late_return_type = late_return_type;
1503 if (target)
1505 declarator->id_loc = target->id_loc;
1506 declarator->parameter_pack_p = target->parameter_pack_p;
1507 target->parameter_pack_p = false;
1509 else
1510 declarator->parameter_pack_p = false;
1512 return declarator;
1515 /* Make a declarator for an array of BOUNDS elements, each of which is
1516 defined by ELEMENT. */
1518 cp_declarator *
1519 make_array_declarator (cp_declarator *element, tree bounds)
1521 cp_declarator *declarator;
1523 declarator = make_declarator (cdk_array);
1524 declarator->declarator = element;
1525 declarator->u.array.bounds = bounds;
1526 if (element)
1528 declarator->id_loc = element->id_loc;
1529 declarator->parameter_pack_p = element->parameter_pack_p;
1530 element->parameter_pack_p = false;
1532 else
1533 declarator->parameter_pack_p = false;
1535 return declarator;
1538 /* Determine whether the declarator we've seen so far can be a
1539 parameter pack, when followed by an ellipsis. */
1540 static bool
1541 declarator_can_be_parameter_pack (cp_declarator *declarator)
1543 /* Search for a declarator name, or any other declarator that goes
1544 after the point where the ellipsis could appear in a parameter
1545 pack. If we find any of these, then this declarator can not be
1546 made into a parameter pack. */
1547 bool found = false;
1548 while (declarator && !found)
1550 switch ((int)declarator->kind)
1552 case cdk_id:
1553 case cdk_array:
1554 found = true;
1555 break;
1557 case cdk_error:
1558 return true;
1560 default:
1561 declarator = declarator->declarator;
1562 break;
1566 return !found;
1569 cp_parameter_declarator *no_parameters;
1571 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1572 DECLARATOR and DEFAULT_ARGUMENT. */
1574 cp_parameter_declarator *
1575 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1576 cp_declarator *declarator,
1577 tree default_argument)
1579 cp_parameter_declarator *parameter;
1581 parameter = ((cp_parameter_declarator *)
1582 alloc_declarator (sizeof (cp_parameter_declarator)));
1583 parameter->next = NULL;
1584 if (decl_specifiers)
1585 parameter->decl_specifiers = *decl_specifiers;
1586 else
1587 clear_decl_specs (&parameter->decl_specifiers);
1588 parameter->declarator = declarator;
1589 parameter->default_argument = default_argument;
1590 parameter->ellipsis_p = false;
1592 return parameter;
1595 /* Returns true iff DECLARATOR is a declaration for a function. */
1597 static bool
1598 function_declarator_p (const cp_declarator *declarator)
1600 while (declarator)
1602 if (declarator->kind == cdk_function
1603 && declarator->declarator->kind == cdk_id)
1604 return true;
1605 if (declarator->kind == cdk_id
1606 || declarator->kind == cdk_error)
1607 return false;
1608 declarator = declarator->declarator;
1610 return false;
1613 /* The parser. */
1615 /* Overview
1616 --------
1618 A cp_parser parses the token stream as specified by the C++
1619 grammar. Its job is purely parsing, not semantic analysis. For
1620 example, the parser breaks the token stream into declarators,
1621 expressions, statements, and other similar syntactic constructs.
1622 It does not check that the types of the expressions on either side
1623 of an assignment-statement are compatible, or that a function is
1624 not declared with a parameter of type `void'.
1626 The parser invokes routines elsewhere in the compiler to perform
1627 semantic analysis and to build up the abstract syntax tree for the
1628 code processed.
1630 The parser (and the template instantiation code, which is, in a
1631 way, a close relative of parsing) are the only parts of the
1632 compiler that should be calling push_scope and pop_scope, or
1633 related functions. The parser (and template instantiation code)
1634 keeps track of what scope is presently active; everything else
1635 should simply honor that. (The code that generates static
1636 initializers may also need to set the scope, in order to check
1637 access control correctly when emitting the initializers.)
1639 Methodology
1640 -----------
1642 The parser is of the standard recursive-descent variety. Upcoming
1643 tokens in the token stream are examined in order to determine which
1644 production to use when parsing a non-terminal. Some C++ constructs
1645 require arbitrary look ahead to disambiguate. For example, it is
1646 impossible, in the general case, to tell whether a statement is an
1647 expression or declaration without scanning the entire statement.
1648 Therefore, the parser is capable of "parsing tentatively." When the
1649 parser is not sure what construct comes next, it enters this mode.
1650 Then, while we attempt to parse the construct, the parser queues up
1651 error messages, rather than issuing them immediately, and saves the
1652 tokens it consumes. If the construct is parsed successfully, the
1653 parser "commits", i.e., it issues any queued error messages and
1654 the tokens that were being preserved are permanently discarded.
1655 If, however, the construct is not parsed successfully, the parser
1656 rolls back its state completely so that it can resume parsing using
1657 a different alternative.
1659 Future Improvements
1660 -------------------
1662 The performance of the parser could probably be improved substantially.
1663 We could often eliminate the need to parse tentatively by looking ahead
1664 a little bit. In some places, this approach might not entirely eliminate
1665 the need to parse tentatively, but it might still speed up the average
1666 case. */
1668 /* Flags that are passed to some parsing functions. These values can
1669 be bitwise-ored together. */
1671 enum
1673 /* No flags. */
1674 CP_PARSER_FLAGS_NONE = 0x0,
1675 /* The construct is optional. If it is not present, then no error
1676 should be issued. */
1677 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1678 /* When parsing a type-specifier, treat user-defined type-names
1679 as non-type identifiers. */
1680 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1681 /* When parsing a type-specifier, do not try to parse a class-specifier
1682 or enum-specifier. */
1683 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1684 /* When parsing a decl-specifier-seq, only allow type-specifier or
1685 constexpr. */
1686 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1689 /* This type is used for parameters and variables which hold
1690 combinations of the above flags. */
1691 typedef int cp_parser_flags;
1693 /* The different kinds of declarators we want to parse. */
1695 typedef enum cp_parser_declarator_kind
1697 /* We want an abstract declarator. */
1698 CP_PARSER_DECLARATOR_ABSTRACT,
1699 /* We want a named declarator. */
1700 CP_PARSER_DECLARATOR_NAMED,
1701 /* We don't mind, but the name must be an unqualified-id. */
1702 CP_PARSER_DECLARATOR_EITHER
1703 } cp_parser_declarator_kind;
1705 /* The precedence values used to parse binary expressions. The minimum value
1706 of PREC must be 1, because zero is reserved to quickly discriminate
1707 binary operators from other tokens. */
1709 enum cp_parser_prec
1711 PREC_NOT_OPERATOR,
1712 PREC_LOGICAL_OR_EXPRESSION,
1713 PREC_LOGICAL_AND_EXPRESSION,
1714 PREC_INCLUSIVE_OR_EXPRESSION,
1715 PREC_EXCLUSIVE_OR_EXPRESSION,
1716 PREC_AND_EXPRESSION,
1717 PREC_EQUALITY_EXPRESSION,
1718 PREC_RELATIONAL_EXPRESSION,
1719 PREC_SHIFT_EXPRESSION,
1720 PREC_ADDITIVE_EXPRESSION,
1721 PREC_MULTIPLICATIVE_EXPRESSION,
1722 PREC_PM_EXPRESSION,
1723 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1726 /* A mapping from a token type to a corresponding tree node type, with a
1727 precedence value. */
1729 typedef struct cp_parser_binary_operations_map_node
1731 /* The token type. */
1732 enum cpp_ttype token_type;
1733 /* The corresponding tree code. */
1734 enum tree_code tree_type;
1735 /* The precedence of this operator. */
1736 enum cp_parser_prec prec;
1737 } cp_parser_binary_operations_map_node;
1739 typedef struct cp_parser_expression_stack_entry
1741 /* Left hand side of the binary operation we are currently
1742 parsing. */
1743 tree lhs;
1744 /* Original tree code for left hand side, if it was a binary
1745 expression itself (used for -Wparentheses). */
1746 enum tree_code lhs_type;
1747 /* Tree code for the binary operation we are parsing. */
1748 enum tree_code tree_type;
1749 /* Precedence of the binary operation we are parsing. */
1750 enum cp_parser_prec prec;
1751 /* Location of the binary operation we are parsing. */
1752 location_t loc;
1753 } cp_parser_expression_stack_entry;
1755 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1756 entries because precedence levels on the stack are monotonically
1757 increasing. */
1758 typedef struct cp_parser_expression_stack_entry
1759 cp_parser_expression_stack[NUM_PREC_VALUES];
1761 /* Prototypes. */
1763 /* Constructors and destructors. */
1765 static cp_parser_context *cp_parser_context_new
1766 (cp_parser_context *);
1768 /* Class variables. */
1770 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1772 /* The operator-precedence table used by cp_parser_binary_expression.
1773 Transformed into an associative array (binops_by_token) by
1774 cp_parser_new. */
1776 static const cp_parser_binary_operations_map_node binops[] = {
1777 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1778 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1780 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1781 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1782 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1784 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1785 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1787 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1788 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1790 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1791 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1792 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1793 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1795 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1796 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1798 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1800 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1802 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1804 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1806 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1809 /* The same as binops, but initialized by cp_parser_new so that
1810 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1811 for speed. */
1812 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1814 /* Constructors and destructors. */
1816 /* Construct a new context. The context below this one on the stack
1817 is given by NEXT. */
1819 static cp_parser_context *
1820 cp_parser_context_new (cp_parser_context* next)
1822 cp_parser_context *context;
1824 /* Allocate the storage. */
1825 if (cp_parser_context_free_list != NULL)
1827 /* Pull the first entry from the free list. */
1828 context = cp_parser_context_free_list;
1829 cp_parser_context_free_list = context->next;
1830 memset (context, 0, sizeof (*context));
1832 else
1833 context = ggc_cleared_alloc<cp_parser_context> ();
1835 /* No errors have occurred yet in this context. */
1836 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1837 /* If this is not the bottommost context, copy information that we
1838 need from the previous context. */
1839 if (next)
1841 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1842 expression, then we are parsing one in this context, too. */
1843 context->object_type = next->object_type;
1844 /* Thread the stack. */
1845 context->next = next;
1848 return context;
1851 /* Managing the unparsed function queues. */
1853 #define unparsed_funs_with_default_args \
1854 parser->unparsed_queues->last ().funs_with_default_args
1855 #define unparsed_funs_with_definitions \
1856 parser->unparsed_queues->last ().funs_with_definitions
1857 #define unparsed_nsdmis \
1858 parser->unparsed_queues->last ().nsdmis
1859 #define unparsed_classes \
1860 parser->unparsed_queues->last ().classes
1862 static void
1863 push_unparsed_function_queues (cp_parser *parser)
1865 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1866 vec_safe_push (parser->unparsed_queues, e);
1869 static void
1870 pop_unparsed_function_queues (cp_parser *parser)
1872 release_tree_vector (unparsed_funs_with_definitions);
1873 parser->unparsed_queues->pop ();
1876 /* Prototypes. */
1878 /* Constructors and destructors. */
1880 static cp_parser *cp_parser_new
1881 (void);
1883 /* Routines to parse various constructs.
1885 Those that return `tree' will return the error_mark_node (rather
1886 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1887 Sometimes, they will return an ordinary node if error-recovery was
1888 attempted, even though a parse error occurred. So, to check
1889 whether or not a parse error occurred, you should always use
1890 cp_parser_error_occurred. If the construct is optional (indicated
1891 either by an `_opt' in the name of the function that does the
1892 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1893 the construct is not present. */
1895 /* Lexical conventions [gram.lex] */
1897 static tree cp_parser_identifier
1898 (cp_parser *);
1899 static tree cp_parser_string_literal
1900 (cp_parser *, bool, bool, bool);
1901 static tree cp_parser_userdef_char_literal
1902 (cp_parser *);
1903 static tree cp_parser_userdef_string_literal
1904 (tree);
1905 static tree cp_parser_userdef_numeric_literal
1906 (cp_parser *);
1908 /* Basic concepts [gram.basic] */
1910 static bool cp_parser_translation_unit
1911 (cp_parser *);
1913 /* Expressions [gram.expr] */
1915 static tree cp_parser_primary_expression
1916 (cp_parser *, bool, bool, bool, cp_id_kind *);
1917 static tree cp_parser_id_expression
1918 (cp_parser *, bool, bool, bool *, bool, bool);
1919 static tree cp_parser_unqualified_id
1920 (cp_parser *, bool, bool, bool, bool);
1921 static tree cp_parser_nested_name_specifier_opt
1922 (cp_parser *, bool, bool, bool, bool);
1923 static tree cp_parser_nested_name_specifier
1924 (cp_parser *, bool, bool, bool, bool);
1925 static tree cp_parser_qualifying_entity
1926 (cp_parser *, bool, bool, bool, bool, bool);
1927 static tree cp_parser_postfix_expression
1928 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1929 static tree cp_parser_postfix_open_square_expression
1930 (cp_parser *, tree, bool, bool);
1931 static tree cp_parser_postfix_dot_deref_expression
1932 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1933 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1934 (cp_parser *, int, bool, bool, bool *, bool = false);
1935 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1936 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1937 static void cp_parser_pseudo_destructor_name
1938 (cp_parser *, tree, tree *, tree *);
1939 static tree cp_parser_unary_expression
1940 (cp_parser *, bool, bool, cp_id_kind *);
1941 static enum tree_code cp_parser_unary_operator
1942 (cp_token *);
1943 static tree cp_parser_new_expression
1944 (cp_parser *);
1945 static vec<tree, va_gc> *cp_parser_new_placement
1946 (cp_parser *);
1947 static tree cp_parser_new_type_id
1948 (cp_parser *, tree *);
1949 static cp_declarator *cp_parser_new_declarator_opt
1950 (cp_parser *);
1951 static cp_declarator *cp_parser_direct_new_declarator
1952 (cp_parser *);
1953 static vec<tree, va_gc> *cp_parser_new_initializer
1954 (cp_parser *);
1955 static tree cp_parser_delete_expression
1956 (cp_parser *);
1957 static tree cp_parser_cast_expression
1958 (cp_parser *, bool, bool, bool, cp_id_kind *);
1959 static tree cp_parser_binary_expression
1960 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1961 static tree cp_parser_question_colon_clause
1962 (cp_parser *, tree);
1963 static tree cp_parser_assignment_expression
1964 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
1965 static enum tree_code cp_parser_assignment_operator_opt
1966 (cp_parser *);
1967 static tree cp_parser_expression
1968 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
1969 static tree cp_parser_constant_expression
1970 (cp_parser *, bool = false, bool * = NULL);
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, 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, 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 *);
2490 static bool cp_parser_array_designator_p
2491 (cp_parser *);
2492 static bool cp_parser_skip_to_closing_square_bracket
2493 (cp_parser *);
2495 /* Returns nonzero if we are parsing tentatively. */
2497 static inline bool
2498 cp_parser_parsing_tentatively (cp_parser* parser)
2500 return parser->context->next != NULL;
2503 /* Returns nonzero if TOKEN is a string literal. */
2505 static bool
2506 cp_parser_is_pure_string_literal (cp_token* token)
2508 return (token->type == CPP_STRING ||
2509 token->type == CPP_STRING16 ||
2510 token->type == CPP_STRING32 ||
2511 token->type == CPP_WSTRING ||
2512 token->type == CPP_UTF8STRING);
2515 /* Returns nonzero if TOKEN is a string literal
2516 of a user-defined string literal. */
2518 static bool
2519 cp_parser_is_string_literal (cp_token* token)
2521 return (cp_parser_is_pure_string_literal (token) ||
2522 token->type == CPP_STRING_USERDEF ||
2523 token->type == CPP_STRING16_USERDEF ||
2524 token->type == CPP_STRING32_USERDEF ||
2525 token->type == CPP_WSTRING_USERDEF ||
2526 token->type == CPP_UTF8STRING_USERDEF);
2529 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2531 static bool
2532 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2534 return token->keyword == keyword;
2537 /* If not parsing tentatively, issue a diagnostic of the form
2538 FILE:LINE: MESSAGE before TOKEN
2539 where TOKEN is the next token in the input stream. MESSAGE
2540 (specified by the caller) is usually of the form "expected
2541 OTHER-TOKEN". */
2543 static void
2544 cp_parser_error (cp_parser* parser, const char* gmsgid)
2546 if (!cp_parser_simulate_error (parser))
2548 cp_token *token = cp_lexer_peek_token (parser->lexer);
2549 /* This diagnostic makes more sense if it is tagged to the line
2550 of the token we just peeked at. */
2551 cp_lexer_set_source_position_from_token (token);
2553 if (token->type == CPP_PRAGMA)
2555 error_at (token->location,
2556 "%<#pragma%> is not allowed here");
2557 cp_parser_skip_to_pragma_eol (parser, token);
2558 return;
2561 c_parse_error (gmsgid,
2562 /* Because c_parser_error does not understand
2563 CPP_KEYWORD, keywords are treated like
2564 identifiers. */
2565 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2566 token->u.value, token->flags);
2570 /* Issue an error about name-lookup failing. NAME is the
2571 IDENTIFIER_NODE DECL is the result of
2572 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2573 the thing that we hoped to find. */
2575 static void
2576 cp_parser_name_lookup_error (cp_parser* parser,
2577 tree name,
2578 tree decl,
2579 name_lookup_error desired,
2580 location_t location)
2582 /* If name lookup completely failed, tell the user that NAME was not
2583 declared. */
2584 if (decl == error_mark_node)
2586 if (parser->scope && parser->scope != global_namespace)
2587 error_at (location, "%<%E::%E%> has not been declared",
2588 parser->scope, name);
2589 else if (parser->scope == global_namespace)
2590 error_at (location, "%<::%E%> has not been declared", name);
2591 else if (parser->object_scope
2592 && !CLASS_TYPE_P (parser->object_scope))
2593 error_at (location, "request for member %qE in non-class type %qT",
2594 name, parser->object_scope);
2595 else if (parser->object_scope)
2596 error_at (location, "%<%T::%E%> has not been declared",
2597 parser->object_scope, name);
2598 else
2599 error_at (location, "%qE has not been declared", name);
2601 else if (parser->scope && parser->scope != global_namespace)
2603 switch (desired)
2605 case NLE_TYPE:
2606 error_at (location, "%<%E::%E%> is not a type",
2607 parser->scope, name);
2608 break;
2609 case NLE_CXX98:
2610 error_at (location, "%<%E::%E%> is not a class or namespace",
2611 parser->scope, name);
2612 break;
2613 case NLE_NOT_CXX98:
2614 error_at (location,
2615 "%<%E::%E%> is not a class, namespace, or enumeration",
2616 parser->scope, name);
2617 break;
2618 default:
2619 gcc_unreachable ();
2623 else if (parser->scope == global_namespace)
2625 switch (desired)
2627 case NLE_TYPE:
2628 error_at (location, "%<::%E%> is not a type", name);
2629 break;
2630 case NLE_CXX98:
2631 error_at (location, "%<::%E%> is not a class or namespace", name);
2632 break;
2633 case NLE_NOT_CXX98:
2634 error_at (location,
2635 "%<::%E%> is not a class, namespace, or enumeration",
2636 name);
2637 break;
2638 default:
2639 gcc_unreachable ();
2642 else
2644 switch (desired)
2646 case NLE_TYPE:
2647 error_at (location, "%qE is not a type", name);
2648 break;
2649 case NLE_CXX98:
2650 error_at (location, "%qE is not a class or namespace", name);
2651 break;
2652 case NLE_NOT_CXX98:
2653 error_at (location,
2654 "%qE is not a class, namespace, or enumeration", name);
2655 break;
2656 default:
2657 gcc_unreachable ();
2662 /* If we are parsing tentatively, remember that an error has occurred
2663 during this tentative parse. Returns true if the error was
2664 simulated; false if a message should be issued by the caller. */
2666 static bool
2667 cp_parser_simulate_error (cp_parser* parser)
2669 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2671 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2672 return true;
2674 return false;
2677 /* This function is called when a type is defined. If type
2678 definitions are forbidden at this point, an error message is
2679 issued. */
2681 static bool
2682 cp_parser_check_type_definition (cp_parser* parser)
2684 /* If types are forbidden here, issue a message. */
2685 if (parser->type_definition_forbidden_message)
2687 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2688 in the message need to be interpreted. */
2689 error (parser->type_definition_forbidden_message);
2690 return false;
2692 return true;
2695 /* This function is called when the DECLARATOR is processed. The TYPE
2696 was a type defined in the decl-specifiers. If it is invalid to
2697 define a type in the decl-specifiers for DECLARATOR, an error is
2698 issued. TYPE_LOCATION is the location of TYPE and is used
2699 for error reporting. */
2701 static void
2702 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2703 tree type, location_t type_location)
2705 /* [dcl.fct] forbids type definitions in return types.
2706 Unfortunately, it's not easy to know whether or not we are
2707 processing a return type until after the fact. */
2708 while (declarator
2709 && (declarator->kind == cdk_pointer
2710 || declarator->kind == cdk_reference
2711 || declarator->kind == cdk_ptrmem))
2712 declarator = declarator->declarator;
2713 if (declarator
2714 && declarator->kind == cdk_function)
2716 error_at (type_location,
2717 "new types may not be defined in a return type");
2718 inform (type_location,
2719 "(perhaps a semicolon is missing after the definition of %qT)",
2720 type);
2724 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2725 "<" in any valid C++ program. If the next token is indeed "<",
2726 issue a message warning the user about what appears to be an
2727 invalid attempt to form a template-id. LOCATION is the location
2728 of the type-specifier (TYPE) */
2730 static void
2731 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2732 tree type,
2733 enum tag_types tag_type,
2734 location_t location)
2736 cp_token_position start = 0;
2738 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2740 if (TYPE_P (type))
2741 error_at (location, "%qT is not a template", type);
2742 else if (identifier_p (type))
2744 if (tag_type != none_type)
2745 error_at (location, "%qE is not a class template", type);
2746 else
2747 error_at (location, "%qE is not a template", type);
2749 else
2750 error_at (location, "invalid template-id");
2751 /* Remember the location of the invalid "<". */
2752 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2753 start = cp_lexer_token_position (parser->lexer, true);
2754 /* Consume the "<". */
2755 cp_lexer_consume_token (parser->lexer);
2756 /* Parse the template arguments. */
2757 cp_parser_enclosed_template_argument_list (parser);
2758 /* Permanently remove the invalid template arguments so that
2759 this error message is not issued again. */
2760 if (start)
2761 cp_lexer_purge_tokens_after (parser->lexer, start);
2765 /* If parsing an integral constant-expression, issue an error message
2766 about the fact that THING appeared and return true. Otherwise,
2767 return false. In either case, set
2768 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2770 static bool
2771 cp_parser_non_integral_constant_expression (cp_parser *parser,
2772 non_integral_constant thing)
2774 parser->non_integral_constant_expression_p = true;
2775 if (parser->integral_constant_expression_p)
2777 if (!parser->allow_non_integral_constant_expression_p)
2779 const char *msg = NULL;
2780 switch (thing)
2782 case NIC_FLOAT:
2783 error ("floating-point literal "
2784 "cannot appear in a constant-expression");
2785 return true;
2786 case NIC_CAST:
2787 error ("a cast to a type other than an integral or "
2788 "enumeration type cannot appear in a "
2789 "constant-expression");
2790 return true;
2791 case NIC_TYPEID:
2792 error ("%<typeid%> operator "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_NCC:
2796 error ("non-constant compound literals "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_FUNC_CALL:
2800 error ("a function call "
2801 "cannot appear in a constant-expression");
2802 return true;
2803 case NIC_INC:
2804 error ("an increment "
2805 "cannot appear in a constant-expression");
2806 return true;
2807 case NIC_DEC:
2808 error ("an decrement "
2809 "cannot appear in a constant-expression");
2810 return true;
2811 case NIC_ARRAY_REF:
2812 error ("an array reference "
2813 "cannot appear in a constant-expression");
2814 return true;
2815 case NIC_ADDR_LABEL:
2816 error ("the address of a label "
2817 "cannot appear in a constant-expression");
2818 return true;
2819 case NIC_OVERLOADED:
2820 error ("calls to overloaded operators "
2821 "cannot appear in a constant-expression");
2822 return true;
2823 case NIC_ASSIGNMENT:
2824 error ("an assignment cannot appear in a constant-expression");
2825 return true;
2826 case NIC_COMMA:
2827 error ("a comma operator "
2828 "cannot appear in a constant-expression");
2829 return true;
2830 case NIC_CONSTRUCTOR:
2831 error ("a call to a constructor "
2832 "cannot appear in a constant-expression");
2833 return true;
2834 case NIC_TRANSACTION:
2835 error ("a transaction expression "
2836 "cannot appear in a constant-expression");
2837 return true;
2838 case NIC_THIS:
2839 msg = "this";
2840 break;
2841 case NIC_FUNC_NAME:
2842 msg = "__FUNCTION__";
2843 break;
2844 case NIC_PRETTY_FUNC:
2845 msg = "__PRETTY_FUNCTION__";
2846 break;
2847 case NIC_C99_FUNC:
2848 msg = "__func__";
2849 break;
2850 case NIC_VA_ARG:
2851 msg = "va_arg";
2852 break;
2853 case NIC_ARROW:
2854 msg = "->";
2855 break;
2856 case NIC_POINT:
2857 msg = ".";
2858 break;
2859 case NIC_STAR:
2860 msg = "*";
2861 break;
2862 case NIC_ADDR:
2863 msg = "&";
2864 break;
2865 case NIC_PREINCREMENT:
2866 msg = "++";
2867 break;
2868 case NIC_PREDECREMENT:
2869 msg = "--";
2870 break;
2871 case NIC_NEW:
2872 msg = "new";
2873 break;
2874 case NIC_DEL:
2875 msg = "delete";
2876 break;
2877 default:
2878 gcc_unreachable ();
2880 if (msg)
2881 error ("%qs cannot appear in a constant-expression", msg);
2882 return true;
2885 return false;
2888 /* Emit a diagnostic for an invalid type name. This function commits
2889 to the current active tentative parse, if any. (Otherwise, the
2890 problematic construct might be encountered again later, resulting
2891 in duplicate error messages.) LOCATION is the location of ID. */
2893 static void
2894 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2895 location_t location)
2897 tree decl, ambiguous_decls;
2898 cp_parser_commit_to_tentative_parse (parser);
2899 /* Try to lookup the identifier. */
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 if (ambiguous_decls)
2906 /* If the lookup was ambiguous, an error will already have
2907 been issued. */
2908 return;
2909 /* If the lookup found a template-name, it means that the user forgot
2910 to specify an argument list. Emit a useful error message. */
2911 if (TREE_CODE (decl) == TEMPLATE_DECL)
2912 error_at (location,
2913 "invalid use of template-name %qE without an argument list",
2914 decl);
2915 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2916 error_at (location, "invalid use of destructor %qD as a type", id);
2917 else if (TREE_CODE (decl) == TYPE_DECL)
2918 /* Something like 'unsigned A a;' */
2919 error_at (location, "invalid combination of multiple type-specifiers");
2920 else if (!parser->scope)
2922 /* Issue an error message. */
2923 error_at (location, "%qE does not name a type", id);
2924 /* If we're in a template class, it's possible that the user was
2925 referring to a type from a base class. For example:
2927 template <typename T> struct A { typedef T X; };
2928 template <typename T> struct B : public A<T> { X x; };
2930 The user should have said "typename A<T>::X". */
2931 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2932 inform (location, "C++11 %<constexpr%> only available with "
2933 "-std=c++11 or -std=gnu++11");
2934 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2935 inform (location, "C++11 %<noexcept%> only available with "
2936 "-std=c++11 or -std=gnu++11");
2937 else if (cxx_dialect < cxx11
2938 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2939 inform (location, "C++11 %<thread_local%> only available with "
2940 "-std=c++11 or -std=gnu++11");
2941 else if (processing_template_decl && current_class_type
2942 && TYPE_BINFO (current_class_type))
2944 tree b;
2946 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2948 b = TREE_CHAIN (b))
2950 tree base_type = BINFO_TYPE (b);
2951 if (CLASS_TYPE_P (base_type)
2952 && dependent_type_p (base_type))
2954 tree field;
2955 /* Go from a particular instantiation of the
2956 template (which will have an empty TYPE_FIELDs),
2957 to the main version. */
2958 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2959 for (field = TYPE_FIELDS (base_type);
2960 field;
2961 field = DECL_CHAIN (field))
2962 if (TREE_CODE (field) == TYPE_DECL
2963 && DECL_NAME (field) == id)
2965 inform (location,
2966 "(perhaps %<typename %T::%E%> was intended)",
2967 BINFO_TYPE (b), id);
2968 break;
2970 if (field)
2971 break;
2976 /* Here we diagnose qualified-ids where the scope is actually correct,
2977 but the identifier does not resolve to a valid type name. */
2978 else if (parser->scope != error_mark_node)
2980 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2982 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2983 error_at (location_of (id),
2984 "%qE in namespace %qE does not name a template type",
2985 id, parser->scope);
2986 else
2987 error_at (location_of (id),
2988 "%qE in namespace %qE does not name a type",
2989 id, parser->scope);
2991 else if (CLASS_TYPE_P (parser->scope)
2992 && constructor_name_p (id, parser->scope))
2994 /* A<T>::A<T>() */
2995 error_at (location, "%<%T::%E%> names the constructor, not"
2996 " the type", parser->scope, id);
2997 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2998 error_at (location, "and %qT has no template constructors",
2999 parser->scope);
3001 else if (TYPE_P (parser->scope)
3002 && dependent_scope_p (parser->scope))
3003 error_at (location, "need %<typename%> before %<%T::%E%> because "
3004 "%qT is a dependent scope",
3005 parser->scope, id, parser->scope);
3006 else if (TYPE_P (parser->scope))
3008 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3009 error_at (location_of (id),
3010 "%qE in %q#T does not name a template type",
3011 id, parser->scope);
3012 else
3013 error_at (location_of (id),
3014 "%qE in %q#T does not name a type",
3015 id, parser->scope);
3017 else
3018 gcc_unreachable ();
3022 /* Check for a common situation where a type-name should be present,
3023 but is not, and issue a sensible error message. Returns true if an
3024 invalid type-name was detected.
3026 The situation handled by this function are variable declarations of the
3027 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3028 Usually, `ID' should name a type, but if we got here it means that it
3029 does not. We try to emit the best possible error message depending on
3030 how exactly the id-expression looks like. */
3032 static bool
3033 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3035 tree id;
3036 cp_token *token = cp_lexer_peek_token (parser->lexer);
3038 /* Avoid duplicate error about ambiguous lookup. */
3039 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3041 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3042 if (next->type == CPP_NAME && next->error_reported)
3043 goto out;
3046 cp_parser_parse_tentatively (parser);
3047 id = cp_parser_id_expression (parser,
3048 /*template_keyword_p=*/false,
3049 /*check_dependency_p=*/true,
3050 /*template_p=*/NULL,
3051 /*declarator_p=*/true,
3052 /*optional_p=*/false);
3053 /* If the next token is a (, this is a function with no explicit return
3054 type, i.e. constructor, destructor or conversion op. */
3055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3056 || TREE_CODE (id) == TYPE_DECL)
3058 cp_parser_abort_tentative_parse (parser);
3059 return false;
3061 if (!cp_parser_parse_definitely (parser))
3062 return false;
3064 /* Emit a diagnostic for the invalid type. */
3065 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3066 out:
3067 /* If we aren't in the middle of a declarator (i.e. in a
3068 parameter-declaration-clause), skip to the end of the declaration;
3069 there's no point in trying to process it. */
3070 if (!parser->in_declarator_p)
3071 cp_parser_skip_to_end_of_block_or_statement (parser);
3072 return true;
3075 /* Consume tokens up to, and including, the next non-nested closing `)'.
3076 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3077 are doing error recovery. Returns -1 if OR_COMMA is true and we
3078 found an unnested comma. */
3080 static int
3081 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3082 bool recovering,
3083 bool or_comma,
3084 bool consume_paren)
3086 unsigned paren_depth = 0;
3087 unsigned brace_depth = 0;
3088 unsigned square_depth = 0;
3090 if (recovering && !or_comma
3091 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3092 return 0;
3094 while (true)
3096 cp_token * token = cp_lexer_peek_token (parser->lexer);
3098 switch (token->type)
3100 case CPP_EOF:
3101 case CPP_PRAGMA_EOL:
3102 /* If we've run out of tokens, then there is no closing `)'. */
3103 return 0;
3105 /* This is good for lambda expression capture-lists. */
3106 case CPP_OPEN_SQUARE:
3107 ++square_depth;
3108 break;
3109 case CPP_CLOSE_SQUARE:
3110 if (!square_depth--)
3111 return 0;
3112 break;
3114 case CPP_SEMICOLON:
3115 /* This matches the processing in skip_to_end_of_statement. */
3116 if (!brace_depth)
3117 return 0;
3118 break;
3120 case CPP_OPEN_BRACE:
3121 ++brace_depth;
3122 break;
3123 case CPP_CLOSE_BRACE:
3124 if (!brace_depth--)
3125 return 0;
3126 break;
3128 case CPP_COMMA:
3129 if (recovering && or_comma && !brace_depth && !paren_depth
3130 && !square_depth)
3131 return -1;
3132 break;
3134 case CPP_OPEN_PAREN:
3135 if (!brace_depth)
3136 ++paren_depth;
3137 break;
3139 case CPP_CLOSE_PAREN:
3140 if (!brace_depth && !paren_depth--)
3142 if (consume_paren)
3143 cp_lexer_consume_token (parser->lexer);
3144 return 1;
3146 break;
3148 default:
3149 break;
3152 /* Consume the token. */
3153 cp_lexer_consume_token (parser->lexer);
3157 /* Consume tokens until we reach the end of the current statement.
3158 Normally, that will be just before consuming a `;'. However, if a
3159 non-nested `}' comes first, then we stop before consuming that. */
3161 static void
3162 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3164 unsigned nesting_depth = 0;
3166 /* Unwind generic function template scope if necessary. */
3167 if (parser->fully_implicit_function_template_p)
3168 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3170 while (true)
3172 cp_token *token = cp_lexer_peek_token (parser->lexer);
3174 switch (token->type)
3176 case CPP_EOF:
3177 case CPP_PRAGMA_EOL:
3178 /* If we've run out of tokens, stop. */
3179 return;
3181 case CPP_SEMICOLON:
3182 /* If the next token is a `;', we have reached the end of the
3183 statement. */
3184 if (!nesting_depth)
3185 return;
3186 break;
3188 case CPP_CLOSE_BRACE:
3189 /* If this is a non-nested '}', stop before consuming it.
3190 That way, when confronted with something like:
3192 { 3 + }
3194 we stop before consuming the closing '}', even though we
3195 have not yet reached a `;'. */
3196 if (nesting_depth == 0)
3197 return;
3199 /* If it is the closing '}' for a block that we have
3200 scanned, stop -- but only after consuming the token.
3201 That way given:
3203 void f g () { ... }
3204 typedef int I;
3206 we will stop after the body of the erroneously declared
3207 function, but before consuming the following `typedef'
3208 declaration. */
3209 if (--nesting_depth == 0)
3211 cp_lexer_consume_token (parser->lexer);
3212 return;
3215 case CPP_OPEN_BRACE:
3216 ++nesting_depth;
3217 break;
3219 default:
3220 break;
3223 /* Consume the token. */
3224 cp_lexer_consume_token (parser->lexer);
3228 /* This function is called at the end of a statement or declaration.
3229 If the next token is a semicolon, it is consumed; otherwise, error
3230 recovery is attempted. */
3232 static void
3233 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3235 /* Look for the trailing `;'. */
3236 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3238 /* If there is additional (erroneous) input, skip to the end of
3239 the statement. */
3240 cp_parser_skip_to_end_of_statement (parser);
3241 /* If the next token is now a `;', consume it. */
3242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3243 cp_lexer_consume_token (parser->lexer);
3247 /* Skip tokens until we have consumed an entire block, or until we
3248 have consumed a non-nested `;'. */
3250 static void
3251 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3253 int nesting_depth = 0;
3255 /* Unwind generic function template scope if necessary. */
3256 if (parser->fully_implicit_function_template_p)
3257 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3259 while (nesting_depth >= 0)
3261 cp_token *token = cp_lexer_peek_token (parser->lexer);
3263 switch (token->type)
3265 case CPP_EOF:
3266 case CPP_PRAGMA_EOL:
3267 /* If we've run out of tokens, stop. */
3268 return;
3270 case CPP_SEMICOLON:
3271 /* Stop if this is an unnested ';'. */
3272 if (!nesting_depth)
3273 nesting_depth = -1;
3274 break;
3276 case CPP_CLOSE_BRACE:
3277 /* Stop if this is an unnested '}', or closes the outermost
3278 nesting level. */
3279 nesting_depth--;
3280 if (nesting_depth < 0)
3281 return;
3282 if (!nesting_depth)
3283 nesting_depth = -1;
3284 break;
3286 case CPP_OPEN_BRACE:
3287 /* Nest. */
3288 nesting_depth++;
3289 break;
3291 default:
3292 break;
3295 /* Consume the token. */
3296 cp_lexer_consume_token (parser->lexer);
3300 /* Skip tokens until a non-nested closing curly brace is the next
3301 token, or there are no more tokens. Return true in the first case,
3302 false otherwise. */
3304 static bool
3305 cp_parser_skip_to_closing_brace (cp_parser *parser)
3307 unsigned nesting_depth = 0;
3309 while (true)
3311 cp_token *token = cp_lexer_peek_token (parser->lexer);
3313 switch (token->type)
3315 case CPP_EOF:
3316 case CPP_PRAGMA_EOL:
3317 /* If we've run out of tokens, stop. */
3318 return false;
3320 case CPP_CLOSE_BRACE:
3321 /* If the next token is a non-nested `}', then we have reached
3322 the end of the current block. */
3323 if (nesting_depth-- == 0)
3324 return true;
3325 break;
3327 case CPP_OPEN_BRACE:
3328 /* If it the next token is a `{', then we are entering a new
3329 block. Consume the entire block. */
3330 ++nesting_depth;
3331 break;
3333 default:
3334 break;
3337 /* Consume the token. */
3338 cp_lexer_consume_token (parser->lexer);
3342 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3343 parameter is the PRAGMA token, allowing us to purge the entire pragma
3344 sequence. */
3346 static void
3347 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3349 cp_token *token;
3351 parser->lexer->in_pragma = false;
3354 token = cp_lexer_consume_token (parser->lexer);
3355 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3357 /* Ensure that the pragma is not parsed again. */
3358 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3361 /* Require pragma end of line, resyncing with it as necessary. The
3362 arguments are as for cp_parser_skip_to_pragma_eol. */
3364 static void
3365 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3367 parser->lexer->in_pragma = false;
3368 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3369 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3372 /* This is a simple wrapper around make_typename_type. When the id is
3373 an unresolved identifier node, we can provide a superior diagnostic
3374 using cp_parser_diagnose_invalid_type_name. */
3376 static tree
3377 cp_parser_make_typename_type (cp_parser *parser, tree id,
3378 location_t id_location)
3380 tree result;
3381 if (identifier_p (id))
3383 result = make_typename_type (parser->scope, id, typename_type,
3384 /*complain=*/tf_none);
3385 if (result == error_mark_node)
3386 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3387 return result;
3389 return make_typename_type (parser->scope, id, typename_type, tf_error);
3392 /* This is a wrapper around the
3393 make_{pointer,ptrmem,reference}_declarator functions that decides
3394 which one to call based on the CODE and CLASS_TYPE arguments. The
3395 CODE argument should be one of the values returned by
3396 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3397 appertain to the pointer or reference. */
3399 static cp_declarator *
3400 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3401 cp_cv_quals cv_qualifiers,
3402 cp_declarator *target,
3403 tree attributes)
3405 if (code == ERROR_MARK)
3406 return cp_error_declarator;
3408 if (code == INDIRECT_REF)
3409 if (class_type == NULL_TREE)
3410 return make_pointer_declarator (cv_qualifiers, target, attributes);
3411 else
3412 return make_ptrmem_declarator (cv_qualifiers, class_type,
3413 target, attributes);
3414 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3415 return make_reference_declarator (cv_qualifiers, target,
3416 false, attributes);
3417 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3418 return make_reference_declarator (cv_qualifiers, target,
3419 true, attributes);
3420 gcc_unreachable ();
3423 /* Create a new C++ parser. */
3425 static cp_parser *
3426 cp_parser_new (void)
3428 cp_parser *parser;
3429 cp_lexer *lexer;
3430 unsigned i;
3432 /* cp_lexer_new_main is called before doing GC allocation because
3433 cp_lexer_new_main might load a PCH file. */
3434 lexer = cp_lexer_new_main ();
3436 /* Initialize the binops_by_token so that we can get the tree
3437 directly from the token. */
3438 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3439 binops_by_token[binops[i].token_type] = binops[i];
3441 parser = ggc_cleared_alloc<cp_parser> ();
3442 parser->lexer = lexer;
3443 parser->context = cp_parser_context_new (NULL);
3445 /* For now, we always accept GNU extensions. */
3446 parser->allow_gnu_extensions_p = 1;
3448 /* The `>' token is a greater-than operator, not the end of a
3449 template-id. */
3450 parser->greater_than_is_operator_p = true;
3452 parser->default_arg_ok_p = true;
3454 /* We are not parsing a constant-expression. */
3455 parser->integral_constant_expression_p = false;
3456 parser->allow_non_integral_constant_expression_p = false;
3457 parser->non_integral_constant_expression_p = false;
3459 /* Local variable names are not forbidden. */
3460 parser->local_variables_forbidden_p = false;
3462 /* We are not processing an `extern "C"' declaration. */
3463 parser->in_unbraced_linkage_specification_p = false;
3465 /* We are not processing a declarator. */
3466 parser->in_declarator_p = false;
3468 /* We are not processing a template-argument-list. */
3469 parser->in_template_argument_list_p = false;
3471 /* We are not in an iteration statement. */
3472 parser->in_statement = 0;
3474 /* We are not in a switch statement. */
3475 parser->in_switch_statement_p = false;
3477 /* We are not parsing a type-id inside an expression. */
3478 parser->in_type_id_in_expr_p = false;
3480 /* Declarations aren't implicitly extern "C". */
3481 parser->implicit_extern_c = false;
3483 /* String literals should be translated to the execution character set. */
3484 parser->translate_strings_p = true;
3486 /* We are not parsing a function body. */
3487 parser->in_function_body = false;
3489 /* We can correct until told otherwise. */
3490 parser->colon_corrects_to_scope_p = true;
3492 /* The unparsed function queue is empty. */
3493 push_unparsed_function_queues (parser);
3495 /* There are no classes being defined. */
3496 parser->num_classes_being_defined = 0;
3498 /* No template parameters apply. */
3499 parser->num_template_parameter_lists = 0;
3501 /* Not declaring an implicit function template. */
3502 parser->auto_is_implicit_function_template_parm_p = false;
3503 parser->fully_implicit_function_template_p = false;
3504 parser->implicit_template_parms = 0;
3505 parser->implicit_template_scope = 0;
3507 return parser;
3510 /* Create a cp_lexer structure which will emit the tokens in CACHE
3511 and push it onto the parser's lexer stack. This is used for delayed
3512 parsing of in-class method bodies and default arguments, and should
3513 not be confused with tentative parsing. */
3514 static void
3515 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3517 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3518 lexer->next = parser->lexer;
3519 parser->lexer = lexer;
3521 /* Move the current source position to that of the first token in the
3522 new lexer. */
3523 cp_lexer_set_source_position_from_token (lexer->next_token);
3526 /* Pop the top lexer off the parser stack. This is never used for the
3527 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3528 static void
3529 cp_parser_pop_lexer (cp_parser *parser)
3531 cp_lexer *lexer = parser->lexer;
3532 parser->lexer = lexer->next;
3533 cp_lexer_destroy (lexer);
3535 /* Put the current source position back where it was before this
3536 lexer was pushed. */
3537 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3540 /* Lexical conventions [gram.lex] */
3542 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3543 identifier. */
3545 static tree
3546 cp_parser_identifier (cp_parser* parser)
3548 cp_token *token;
3550 /* Look for the identifier. */
3551 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3552 /* Return the value. */
3553 return token ? token->u.value : error_mark_node;
3556 /* Parse a sequence of adjacent string constants. Returns a
3557 TREE_STRING representing the combined, nul-terminated string
3558 constant. If TRANSLATE is true, translate the string to the
3559 execution character set. If WIDE_OK is true, a wide string is
3560 invalid here.
3562 C++98 [lex.string] says that if a narrow string literal token is
3563 adjacent to a wide string literal token, the behavior is undefined.
3564 However, C99 6.4.5p4 says that this results in a wide string literal.
3565 We follow C99 here, for consistency with the C front end.
3567 This code is largely lifted from lex_string() in c-lex.c.
3569 FUTURE: ObjC++ will need to handle @-strings here. */
3570 static tree
3571 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3572 bool lookup_udlit = true)
3574 tree value;
3575 size_t count;
3576 struct obstack str_ob;
3577 cpp_string str, istr, *strs;
3578 cp_token *tok;
3579 enum cpp_ttype type, curr_type;
3580 int have_suffix_p = 0;
3581 tree string_tree;
3582 tree suffix_id = NULL_TREE;
3583 bool curr_tok_is_userdef_p = false;
3585 tok = cp_lexer_peek_token (parser->lexer);
3586 if (!cp_parser_is_string_literal (tok))
3588 cp_parser_error (parser, "expected string-literal");
3589 return error_mark_node;
3592 if (cpp_userdef_string_p (tok->type))
3594 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3595 curr_type = cpp_userdef_string_remove_type (tok->type);
3596 curr_tok_is_userdef_p = true;
3598 else
3600 string_tree = tok->u.value;
3601 curr_type = tok->type;
3603 type = curr_type;
3605 /* Try to avoid the overhead of creating and destroying an obstack
3606 for the common case of just one string. */
3607 if (!cp_parser_is_string_literal
3608 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3610 cp_lexer_consume_token (parser->lexer);
3612 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3613 str.len = TREE_STRING_LENGTH (string_tree);
3614 count = 1;
3616 if (curr_tok_is_userdef_p)
3618 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3619 have_suffix_p = 1;
3620 curr_type = cpp_userdef_string_remove_type (tok->type);
3622 else
3623 curr_type = tok->type;
3625 strs = &str;
3627 else
3629 gcc_obstack_init (&str_ob);
3630 count = 0;
3634 cp_lexer_consume_token (parser->lexer);
3635 count++;
3636 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3637 str.len = TREE_STRING_LENGTH (string_tree);
3639 if (curr_tok_is_userdef_p)
3641 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3642 if (have_suffix_p == 0)
3644 suffix_id = curr_suffix_id;
3645 have_suffix_p = 1;
3647 else if (have_suffix_p == 1
3648 && curr_suffix_id != suffix_id)
3650 error ("inconsistent user-defined literal suffixes"
3651 " %qD and %qD in string literal",
3652 suffix_id, curr_suffix_id);
3653 have_suffix_p = -1;
3655 curr_type = cpp_userdef_string_remove_type (tok->type);
3657 else
3658 curr_type = tok->type;
3660 if (type != curr_type)
3662 if (type == CPP_STRING)
3663 type = curr_type;
3664 else if (curr_type != CPP_STRING)
3665 error_at (tok->location,
3666 "unsupported non-standard concatenation "
3667 "of string literals");
3670 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3672 tok = cp_lexer_peek_token (parser->lexer);
3673 if (cpp_userdef_string_p (tok->type))
3675 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3676 curr_type = cpp_userdef_string_remove_type (tok->type);
3677 curr_tok_is_userdef_p = true;
3679 else
3681 string_tree = tok->u.value;
3682 curr_type = tok->type;
3683 curr_tok_is_userdef_p = false;
3686 while (cp_parser_is_string_literal (tok));
3688 strs = (cpp_string *) obstack_finish (&str_ob);
3691 if (type != CPP_STRING && !wide_ok)
3693 cp_parser_error (parser, "a wide string is invalid in this context");
3694 type = CPP_STRING;
3697 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3698 (parse_in, strs, count, &istr, type))
3700 value = build_string (istr.len, (const char *)istr.text);
3701 free (CONST_CAST (unsigned char *, istr.text));
3703 switch (type)
3705 default:
3706 case CPP_STRING:
3707 case CPP_UTF8STRING:
3708 TREE_TYPE (value) = char_array_type_node;
3709 break;
3710 case CPP_STRING16:
3711 TREE_TYPE (value) = char16_array_type_node;
3712 break;
3713 case CPP_STRING32:
3714 TREE_TYPE (value) = char32_array_type_node;
3715 break;
3716 case CPP_WSTRING:
3717 TREE_TYPE (value) = wchar_array_type_node;
3718 break;
3721 value = fix_string_type (value);
3723 if (have_suffix_p)
3725 tree literal = build_userdef_literal (suffix_id, value,
3726 OT_NONE, NULL_TREE);
3727 if (lookup_udlit)
3728 value = cp_parser_userdef_string_literal (literal);
3729 else
3730 value = literal;
3733 else
3734 /* cpp_interpret_string has issued an error. */
3735 value = error_mark_node;
3737 if (count > 1)
3738 obstack_free (&str_ob, 0);
3740 return value;
3743 /* Look up a literal operator with the name and the exact arguments. */
3745 static tree
3746 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3748 tree decl, fns;
3749 decl = lookup_name (name);
3750 if (!decl || !is_overloaded_fn (decl))
3751 return error_mark_node;
3753 for (fns = decl; fns; fns = OVL_NEXT (fns))
3755 unsigned int ix;
3756 bool found = true;
3757 tree fn = OVL_CURRENT (fns);
3758 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3759 if (parmtypes != NULL_TREE)
3761 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3762 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3764 tree tparm = TREE_VALUE (parmtypes);
3765 tree targ = TREE_TYPE ((*args)[ix]);
3766 bool ptr = TYPE_PTR_P (tparm);
3767 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3768 if ((ptr || arr || !same_type_p (tparm, targ))
3769 && (!ptr || !arr
3770 || !same_type_p (TREE_TYPE (tparm),
3771 TREE_TYPE (targ))))
3772 found = false;
3774 if (found
3775 && ix == vec_safe_length (args)
3776 /* May be this should be sufficient_parms_p instead,
3777 depending on how exactly should user-defined literals
3778 work in presence of default arguments on the literal
3779 operator parameters. */
3780 && parmtypes == void_list_node)
3781 return fn;
3785 return error_mark_node;
3788 /* Parse a user-defined char constant. Returns a call to a user-defined
3789 literal operator taking the character as an argument. */
3791 static tree
3792 cp_parser_userdef_char_literal (cp_parser *parser)
3794 cp_token *token = cp_lexer_consume_token (parser->lexer);
3795 tree literal = token->u.value;
3796 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3797 tree value = USERDEF_LITERAL_VALUE (literal);
3798 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3799 tree decl, result;
3801 /* Build up a call to the user-defined operator */
3802 /* Lookup the name we got back from the id-expression. */
3803 vec<tree, va_gc> *args = make_tree_vector ();
3804 vec_safe_push (args, value);
3805 decl = lookup_literal_operator (name, args);
3806 if (!decl || decl == error_mark_node)
3808 error ("unable to find character literal operator %qD with %qT argument",
3809 name, TREE_TYPE (value));
3810 release_tree_vector (args);
3811 return error_mark_node;
3813 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3814 release_tree_vector (args);
3815 if (result != error_mark_node)
3816 return result;
3818 error ("unable to find character literal operator %qD with %qT argument",
3819 name, TREE_TYPE (value));
3820 return error_mark_node;
3823 /* A subroutine of cp_parser_userdef_numeric_literal to
3824 create a char... template parameter pack from a string node. */
3826 static tree
3827 make_char_string_pack (tree value)
3829 tree charvec;
3830 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3831 const char *str = TREE_STRING_POINTER (value);
3832 int i, len = TREE_STRING_LENGTH (value) - 1;
3833 tree argvec = make_tree_vec (1);
3835 /* Fill in CHARVEC with all of the parameters. */
3836 charvec = make_tree_vec (len);
3837 for (i = 0; i < len; ++i)
3838 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3840 /* Build the argument packs. */
3841 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3842 TREE_TYPE (argpack) = char_type_node;
3844 TREE_VEC_ELT (argvec, 0) = argpack;
3846 return argvec;
3849 /* A subroutine of cp_parser_userdef_numeric_literal to
3850 create a char... template parameter pack from a string node. */
3852 static tree
3853 make_string_pack (tree value)
3855 tree charvec;
3856 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3857 const unsigned char *str
3858 = (const unsigned char *) TREE_STRING_POINTER (value);
3859 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3860 int len = TREE_STRING_LENGTH (value) / sz - 1;
3861 tree argvec = make_tree_vec (2);
3863 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3864 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3866 /* First template parm is character type. */
3867 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3869 /* Fill in CHARVEC with all of the parameters. */
3870 charvec = make_tree_vec (len);
3871 for (int i = 0; i < len; ++i)
3872 TREE_VEC_ELT (charvec, i)
3873 = double_int_to_tree (str_char_type_node,
3874 double_int::from_buffer (str + i * sz, sz));
3876 /* Build the argument packs. */
3877 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3878 TREE_TYPE (argpack) = str_char_type_node;
3880 TREE_VEC_ELT (argvec, 1) = argpack;
3882 return argvec;
3885 /* Parse a user-defined numeric constant. returns a call to a user-defined
3886 literal operator. */
3888 static tree
3889 cp_parser_userdef_numeric_literal (cp_parser *parser)
3891 cp_token *token = cp_lexer_consume_token (parser->lexer);
3892 tree literal = token->u.value;
3893 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3894 tree value = USERDEF_LITERAL_VALUE (literal);
3895 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3896 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3897 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3898 tree decl, result;
3899 vec<tree, va_gc> *args;
3901 /* Look for a literal operator taking the exact type of numeric argument
3902 as the literal value. */
3903 args = make_tree_vector ();
3904 vec_safe_push (args, value);
3905 decl = lookup_literal_operator (name, args);
3906 if (decl && decl != error_mark_node)
3908 result = finish_call_expr (decl, &args, false, true, tf_none);
3909 if (result != error_mark_node)
3911 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3912 warning_at (token->location, OPT_Woverflow,
3913 "integer literal exceeds range of %qT type",
3914 long_long_unsigned_type_node);
3915 else
3917 if (overflow > 0)
3918 warning_at (token->location, OPT_Woverflow,
3919 "floating literal exceeds range of %qT type",
3920 long_double_type_node);
3921 else if (overflow < 0)
3922 warning_at (token->location, OPT_Woverflow,
3923 "floating literal truncated to zero");
3925 release_tree_vector (args);
3926 return result;
3929 release_tree_vector (args);
3931 /* If the numeric argument didn't work, look for a raw literal
3932 operator taking a const char* argument consisting of the number
3933 in string format. */
3934 args = make_tree_vector ();
3935 vec_safe_push (args, num_string);
3936 decl = lookup_literal_operator (name, args);
3937 if (decl && decl != error_mark_node)
3939 result = finish_call_expr (decl, &args, false, true, tf_none);
3940 if (result != error_mark_node)
3942 release_tree_vector (args);
3943 return result;
3946 release_tree_vector (args);
3948 /* If the raw literal didn't work, look for a non-type template
3949 function with parameter pack char.... Call the function with
3950 template parameter characters representing the number. */
3951 args = make_tree_vector ();
3952 decl = lookup_literal_operator (name, args);
3953 if (decl && decl != error_mark_node)
3955 tree tmpl_args = make_char_string_pack (num_string);
3956 decl = lookup_template_function (decl, tmpl_args);
3957 result = finish_call_expr (decl, &args, false, true, tf_none);
3958 if (result != error_mark_node)
3960 release_tree_vector (args);
3961 return result;
3964 release_tree_vector (args);
3966 error ("unable to find numeric literal operator %qD", name);
3967 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3968 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3969 "to enable more built-in suffixes");
3970 return error_mark_node;
3973 /* Parse a user-defined string constant. Returns a call to a user-defined
3974 literal operator taking a character pointer and the length of the string
3975 as arguments. */
3977 static tree
3978 cp_parser_userdef_string_literal (tree literal)
3980 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3981 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3982 tree value = USERDEF_LITERAL_VALUE (literal);
3983 int len = TREE_STRING_LENGTH (value)
3984 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3985 tree decl, result;
3986 vec<tree, va_gc> *args;
3988 /* Look for a template function with typename parameter CharT
3989 and parameter pack CharT... Call the function with
3990 template parameter characters representing the string. */
3991 args = make_tree_vector ();
3992 decl = lookup_literal_operator (name, args);
3993 if (decl && decl != error_mark_node)
3995 tree tmpl_args = make_string_pack (value);
3996 decl = lookup_template_function (decl, tmpl_args);
3997 result = finish_call_expr (decl, &args, false, true, tf_none);
3998 if (result != error_mark_node)
4000 release_tree_vector (args);
4001 return result;
4004 release_tree_vector (args);
4006 /* Build up a call to the user-defined operator */
4007 /* Lookup the name we got back from the id-expression. */
4008 args = make_tree_vector ();
4009 vec_safe_push (args, value);
4010 vec_safe_push (args, build_int_cst (size_type_node, len));
4011 decl = lookup_name (name);
4012 if (!decl || decl == error_mark_node)
4014 error ("unable to find string literal operator %qD", name);
4015 release_tree_vector (args);
4016 return error_mark_node;
4018 result = finish_call_expr (decl, &args, false, true, tf_none);
4019 release_tree_vector (args);
4020 if (result != error_mark_node)
4021 return result;
4023 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4024 name, TREE_TYPE (value), size_type_node);
4025 return error_mark_node;
4029 /* Basic concepts [gram.basic] */
4031 /* Parse a translation-unit.
4033 translation-unit:
4034 declaration-seq [opt]
4036 Returns TRUE if all went well. */
4038 static bool
4039 cp_parser_translation_unit (cp_parser* parser)
4041 /* The address of the first non-permanent object on the declarator
4042 obstack. */
4043 static void *declarator_obstack_base;
4045 bool success;
4047 /* Create the declarator obstack, if necessary. */
4048 if (!cp_error_declarator)
4050 gcc_obstack_init (&declarator_obstack);
4051 /* Create the error declarator. */
4052 cp_error_declarator = make_declarator (cdk_error);
4053 /* Create the empty parameter list. */
4054 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4055 /* Remember where the base of the declarator obstack lies. */
4056 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4059 cp_parser_declaration_seq_opt (parser);
4061 /* If there are no tokens left then all went well. */
4062 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4064 /* Get rid of the token array; we don't need it any more. */
4065 cp_lexer_destroy (parser->lexer);
4066 parser->lexer = NULL;
4068 /* This file might have been a context that's implicitly extern
4069 "C". If so, pop the lang context. (Only relevant for PCH.) */
4070 if (parser->implicit_extern_c)
4072 pop_lang_context ();
4073 parser->implicit_extern_c = false;
4076 /* Finish up. */
4077 finish_translation_unit ();
4079 success = true;
4081 else
4083 cp_parser_error (parser, "expected declaration");
4084 success = false;
4087 /* Make sure the declarator obstack was fully cleaned up. */
4088 gcc_assert (obstack_next_free (&declarator_obstack)
4089 == declarator_obstack_base);
4091 /* All went well. */
4092 return success;
4095 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4096 decltype context. */
4098 static inline tsubst_flags_t
4099 complain_flags (bool decltype_p)
4101 tsubst_flags_t complain = tf_warning_or_error;
4102 if (decltype_p)
4103 complain |= tf_decltype;
4104 return complain;
4107 /* Expressions [gram.expr] */
4109 /* Parse a primary-expression.
4111 primary-expression:
4112 literal
4113 this
4114 ( expression )
4115 id-expression
4116 lambda-expression (C++11)
4118 GNU Extensions:
4120 primary-expression:
4121 ( compound-statement )
4122 __builtin_va_arg ( assignment-expression , type-id )
4123 __builtin_offsetof ( type-id , offsetof-expression )
4125 C++ Extensions:
4126 __has_nothrow_assign ( type-id )
4127 __has_nothrow_constructor ( type-id )
4128 __has_nothrow_copy ( type-id )
4129 __has_trivial_assign ( type-id )
4130 __has_trivial_constructor ( type-id )
4131 __has_trivial_copy ( type-id )
4132 __has_trivial_destructor ( type-id )
4133 __has_virtual_destructor ( type-id )
4134 __is_abstract ( type-id )
4135 __is_base_of ( type-id , type-id )
4136 __is_class ( type-id )
4137 __is_empty ( type-id )
4138 __is_enum ( type-id )
4139 __is_final ( type-id )
4140 __is_literal_type ( type-id )
4141 __is_pod ( type-id )
4142 __is_polymorphic ( type-id )
4143 __is_std_layout ( type-id )
4144 __is_trivial ( type-id )
4145 __is_union ( type-id )
4147 Objective-C++ Extension:
4149 primary-expression:
4150 objc-expression
4152 literal:
4153 __null
4155 ADDRESS_P is true iff this expression was immediately preceded by
4156 "&" and therefore might denote a pointer-to-member. CAST_P is true
4157 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4158 true iff this expression is a template argument.
4160 Returns a representation of the expression. Upon return, *IDK
4161 indicates what kind of id-expression (if any) was present. */
4163 static tree
4164 cp_parser_primary_expression (cp_parser *parser,
4165 bool address_p,
4166 bool cast_p,
4167 bool template_arg_p,
4168 bool decltype_p,
4169 cp_id_kind *idk)
4171 cp_token *token = NULL;
4173 /* Assume the primary expression is not an id-expression. */
4174 *idk = CP_ID_KIND_NONE;
4176 /* Peek at the next token. */
4177 token = cp_lexer_peek_token (parser->lexer);
4178 switch ((int) token->type)
4180 /* literal:
4181 integer-literal
4182 character-literal
4183 floating-literal
4184 string-literal
4185 boolean-literal
4186 pointer-literal
4187 user-defined-literal */
4188 case CPP_CHAR:
4189 case CPP_CHAR16:
4190 case CPP_CHAR32:
4191 case CPP_WCHAR:
4192 case CPP_NUMBER:
4193 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4194 return cp_parser_userdef_numeric_literal (parser);
4195 token = cp_lexer_consume_token (parser->lexer);
4196 if (TREE_CODE (token->u.value) == FIXED_CST)
4198 error_at (token->location,
4199 "fixed-point types not supported in C++");
4200 return error_mark_node;
4202 /* Floating-point literals are only allowed in an integral
4203 constant expression if they are cast to an integral or
4204 enumeration type. */
4205 if (TREE_CODE (token->u.value) == REAL_CST
4206 && parser->integral_constant_expression_p
4207 && pedantic)
4209 /* CAST_P will be set even in invalid code like "int(2.7 +
4210 ...)". Therefore, we have to check that the next token
4211 is sure to end the cast. */
4212 if (cast_p)
4214 cp_token *next_token;
4216 next_token = cp_lexer_peek_token (parser->lexer);
4217 if (/* The comma at the end of an
4218 enumerator-definition. */
4219 next_token->type != CPP_COMMA
4220 /* The curly brace at the end of an enum-specifier. */
4221 && next_token->type != CPP_CLOSE_BRACE
4222 /* The end of a statement. */
4223 && next_token->type != CPP_SEMICOLON
4224 /* The end of the cast-expression. */
4225 && next_token->type != CPP_CLOSE_PAREN
4226 /* The end of an array bound. */
4227 && next_token->type != CPP_CLOSE_SQUARE
4228 /* The closing ">" in a template-argument-list. */
4229 && (next_token->type != CPP_GREATER
4230 || parser->greater_than_is_operator_p)
4231 /* C++0x only: A ">>" treated like two ">" tokens,
4232 in a template-argument-list. */
4233 && (next_token->type != CPP_RSHIFT
4234 || (cxx_dialect == cxx98)
4235 || parser->greater_than_is_operator_p))
4236 cast_p = false;
4239 /* If we are within a cast, then the constraint that the
4240 cast is to an integral or enumeration type will be
4241 checked at that point. If we are not within a cast, then
4242 this code is invalid. */
4243 if (!cast_p)
4244 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4246 return token->u.value;
4248 case CPP_CHAR_USERDEF:
4249 case CPP_CHAR16_USERDEF:
4250 case CPP_CHAR32_USERDEF:
4251 case CPP_WCHAR_USERDEF:
4252 return cp_parser_userdef_char_literal (parser);
4254 case CPP_STRING:
4255 case CPP_STRING16:
4256 case CPP_STRING32:
4257 case CPP_WSTRING:
4258 case CPP_UTF8STRING:
4259 case CPP_STRING_USERDEF:
4260 case CPP_STRING16_USERDEF:
4261 case CPP_STRING32_USERDEF:
4262 case CPP_WSTRING_USERDEF:
4263 case CPP_UTF8STRING_USERDEF:
4264 /* ??? Should wide strings be allowed when parser->translate_strings_p
4265 is false (i.e. in attributes)? If not, we can kill the third
4266 argument to cp_parser_string_literal. */
4267 return cp_parser_string_literal (parser,
4268 parser->translate_strings_p,
4269 true);
4271 case CPP_OPEN_PAREN:
4273 tree expr;
4274 bool saved_greater_than_is_operator_p;
4276 /* Consume the `('. */
4277 cp_lexer_consume_token (parser->lexer);
4278 /* Within a parenthesized expression, a `>' token is always
4279 the greater-than operator. */
4280 saved_greater_than_is_operator_p
4281 = parser->greater_than_is_operator_p;
4282 parser->greater_than_is_operator_p = true;
4283 /* If we see `( { ' then we are looking at the beginning of
4284 a GNU statement-expression. */
4285 if (cp_parser_allow_gnu_extensions_p (parser)
4286 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4288 /* Statement-expressions are not allowed by the standard. */
4289 pedwarn (token->location, OPT_Wpedantic,
4290 "ISO C++ forbids braced-groups within expressions");
4292 /* And they're not allowed outside of a function-body; you
4293 cannot, for example, write:
4295 int i = ({ int j = 3; j + 1; });
4297 at class or namespace scope. */
4298 if (!parser->in_function_body
4299 || parser->in_template_argument_list_p)
4301 error_at (token->location,
4302 "statement-expressions are not allowed outside "
4303 "functions nor in template-argument lists");
4304 cp_parser_skip_to_end_of_block_or_statement (parser);
4305 expr = error_mark_node;
4307 else
4309 /* Start the statement-expression. */
4310 expr = begin_stmt_expr ();
4311 /* Parse the compound-statement. */
4312 cp_parser_compound_statement (parser, expr, false, false);
4313 /* Finish up. */
4314 expr = finish_stmt_expr (expr, false);
4317 else
4319 /* Parse the parenthesized expression. */
4320 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4321 /* Let the front end know that this expression was
4322 enclosed in parentheses. This matters in case, for
4323 example, the expression is of the form `A::B', since
4324 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4325 not. */
4326 expr = finish_parenthesized_expr (expr);
4327 /* DR 705: Wrapping an unqualified name in parentheses
4328 suppresses arg-dependent lookup. We want to pass back
4329 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4330 (c++/37862), but none of the others. */
4331 if (*idk != CP_ID_KIND_QUALIFIED)
4332 *idk = CP_ID_KIND_NONE;
4334 /* The `>' token might be the end of a template-id or
4335 template-parameter-list now. */
4336 parser->greater_than_is_operator_p
4337 = saved_greater_than_is_operator_p;
4338 /* Consume the `)'. */
4339 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4340 cp_parser_skip_to_end_of_statement (parser);
4342 return expr;
4345 case CPP_OPEN_SQUARE:
4346 if (c_dialect_objc ())
4347 /* We have an Objective-C++ message. */
4348 return cp_parser_objc_expression (parser);
4350 tree lam = cp_parser_lambda_expression (parser);
4351 /* Don't warn about a failed tentative parse. */
4352 if (cp_parser_error_occurred (parser))
4353 return error_mark_node;
4354 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4355 return lam;
4358 case CPP_OBJC_STRING:
4359 if (c_dialect_objc ())
4360 /* We have an Objective-C++ string literal. */
4361 return cp_parser_objc_expression (parser);
4362 cp_parser_error (parser, "expected primary-expression");
4363 return error_mark_node;
4365 case CPP_KEYWORD:
4366 switch (token->keyword)
4368 /* These two are the boolean literals. */
4369 case RID_TRUE:
4370 cp_lexer_consume_token (parser->lexer);
4371 return boolean_true_node;
4372 case RID_FALSE:
4373 cp_lexer_consume_token (parser->lexer);
4374 return boolean_false_node;
4376 /* The `__null' literal. */
4377 case RID_NULL:
4378 cp_lexer_consume_token (parser->lexer);
4379 return null_node;
4381 /* The `nullptr' literal. */
4382 case RID_NULLPTR:
4383 cp_lexer_consume_token (parser->lexer);
4384 return nullptr_node;
4386 /* Recognize the `this' keyword. */
4387 case RID_THIS:
4388 cp_lexer_consume_token (parser->lexer);
4389 if (parser->local_variables_forbidden_p)
4391 error_at (token->location,
4392 "%<this%> may not be used in this context");
4393 return error_mark_node;
4395 /* Pointers cannot appear in constant-expressions. */
4396 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4397 return error_mark_node;
4398 return finish_this_expr ();
4400 /* The `operator' keyword can be the beginning of an
4401 id-expression. */
4402 case RID_OPERATOR:
4403 goto id_expression;
4405 case RID_FUNCTION_NAME:
4406 case RID_PRETTY_FUNCTION_NAME:
4407 case RID_C99_FUNCTION_NAME:
4409 non_integral_constant name;
4411 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4412 __func__ are the names of variables -- but they are
4413 treated specially. Therefore, they are handled here,
4414 rather than relying on the generic id-expression logic
4415 below. Grammatically, these names are id-expressions.
4417 Consume the token. */
4418 token = cp_lexer_consume_token (parser->lexer);
4420 switch (token->keyword)
4422 case RID_FUNCTION_NAME:
4423 name = NIC_FUNC_NAME;
4424 break;
4425 case RID_PRETTY_FUNCTION_NAME:
4426 name = NIC_PRETTY_FUNC;
4427 break;
4428 case RID_C99_FUNCTION_NAME:
4429 name = NIC_C99_FUNC;
4430 break;
4431 default:
4432 gcc_unreachable ();
4435 if (cp_parser_non_integral_constant_expression (parser, name))
4436 return error_mark_node;
4438 /* Look up the name. */
4439 return finish_fname (token->u.value);
4442 case RID_VA_ARG:
4444 tree expression;
4445 tree type;
4446 source_location type_location;
4448 /* The `__builtin_va_arg' construct is used to handle
4449 `va_arg'. Consume the `__builtin_va_arg' token. */
4450 cp_lexer_consume_token (parser->lexer);
4451 /* Look for the opening `('. */
4452 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4453 /* Now, parse the assignment-expression. */
4454 expression = cp_parser_assignment_expression (parser);
4455 /* Look for the `,'. */
4456 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4457 type_location = cp_lexer_peek_token (parser->lexer)->location;
4458 /* Parse the type-id. */
4459 type = cp_parser_type_id (parser);
4460 /* Look for the closing `)'. */
4461 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4462 /* Using `va_arg' in a constant-expression is not
4463 allowed. */
4464 if (cp_parser_non_integral_constant_expression (parser,
4465 NIC_VA_ARG))
4466 return error_mark_node;
4467 return build_x_va_arg (type_location, expression, type);
4470 case RID_OFFSETOF:
4471 return cp_parser_builtin_offsetof (parser);
4473 case RID_HAS_NOTHROW_ASSIGN:
4474 case RID_HAS_NOTHROW_CONSTRUCTOR:
4475 case RID_HAS_NOTHROW_COPY:
4476 case RID_HAS_TRIVIAL_ASSIGN:
4477 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4478 case RID_HAS_TRIVIAL_COPY:
4479 case RID_HAS_TRIVIAL_DESTRUCTOR:
4480 case RID_HAS_VIRTUAL_DESTRUCTOR:
4481 case RID_IS_ABSTRACT:
4482 case RID_IS_BASE_OF:
4483 case RID_IS_CLASS:
4484 case RID_IS_EMPTY:
4485 case RID_IS_ENUM:
4486 case RID_IS_FINAL:
4487 case RID_IS_LITERAL_TYPE:
4488 case RID_IS_POD:
4489 case RID_IS_POLYMORPHIC:
4490 case RID_IS_STD_LAYOUT:
4491 case RID_IS_TRIVIAL:
4492 case RID_IS_TRIVIALLY_ASSIGNABLE:
4493 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4494 case RID_IS_TRIVIALLY_COPYABLE:
4495 case RID_IS_UNION:
4496 return cp_parser_trait_expr (parser, token->keyword);
4498 /* Objective-C++ expressions. */
4499 case RID_AT_ENCODE:
4500 case RID_AT_PROTOCOL:
4501 case RID_AT_SELECTOR:
4502 return cp_parser_objc_expression (parser);
4504 case RID_TEMPLATE:
4505 if (parser->in_function_body
4506 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4507 == CPP_LESS))
4509 error_at (token->location,
4510 "a template declaration cannot appear at block scope");
4511 cp_parser_skip_to_end_of_block_or_statement (parser);
4512 return error_mark_node;
4514 default:
4515 cp_parser_error (parser, "expected primary-expression");
4516 return error_mark_node;
4519 /* An id-expression can start with either an identifier, a
4520 `::' as the beginning of a qualified-id, or the "operator"
4521 keyword. */
4522 case CPP_NAME:
4523 case CPP_SCOPE:
4524 case CPP_TEMPLATE_ID:
4525 case CPP_NESTED_NAME_SPECIFIER:
4527 tree id_expression;
4528 tree decl;
4529 const char *error_msg;
4530 bool template_p;
4531 bool done;
4532 cp_token *id_expr_token;
4534 id_expression:
4535 /* Parse the id-expression. */
4536 id_expression
4537 = cp_parser_id_expression (parser,
4538 /*template_keyword_p=*/false,
4539 /*check_dependency_p=*/true,
4540 &template_p,
4541 /*declarator_p=*/false,
4542 /*optional_p=*/false);
4543 if (id_expression == error_mark_node)
4544 return error_mark_node;
4545 id_expr_token = token;
4546 token = cp_lexer_peek_token (parser->lexer);
4547 done = (token->type != CPP_OPEN_SQUARE
4548 && token->type != CPP_OPEN_PAREN
4549 && token->type != CPP_DOT
4550 && token->type != CPP_DEREF
4551 && token->type != CPP_PLUS_PLUS
4552 && token->type != CPP_MINUS_MINUS);
4553 /* If we have a template-id, then no further lookup is
4554 required. If the template-id was for a template-class, we
4555 will sometimes have a TYPE_DECL at this point. */
4556 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4557 || TREE_CODE (id_expression) == TYPE_DECL)
4558 decl = id_expression;
4559 /* Look up the name. */
4560 else
4562 tree ambiguous_decls;
4564 /* If we already know that this lookup is ambiguous, then
4565 we've already issued an error message; there's no reason
4566 to check again. */
4567 if (id_expr_token->type == CPP_NAME
4568 && id_expr_token->error_reported)
4570 cp_parser_simulate_error (parser);
4571 return error_mark_node;
4574 decl = cp_parser_lookup_name (parser, id_expression,
4575 none_type,
4576 template_p,
4577 /*is_namespace=*/false,
4578 /*check_dependency=*/true,
4579 &ambiguous_decls,
4580 id_expr_token->location);
4581 /* If the lookup was ambiguous, an error will already have
4582 been issued. */
4583 if (ambiguous_decls)
4584 return error_mark_node;
4586 /* In Objective-C++, we may have an Objective-C 2.0
4587 dot-syntax for classes here. */
4588 if (c_dialect_objc ()
4589 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4590 && TREE_CODE (decl) == TYPE_DECL
4591 && objc_is_class_name (decl))
4593 tree component;
4594 cp_lexer_consume_token (parser->lexer);
4595 component = cp_parser_identifier (parser);
4596 if (component == error_mark_node)
4597 return error_mark_node;
4599 return objc_build_class_component_ref (id_expression, component);
4602 /* In Objective-C++, an instance variable (ivar) may be preferred
4603 to whatever cp_parser_lookup_name() found. */
4604 decl = objc_lookup_ivar (decl, id_expression);
4606 /* If name lookup gives us a SCOPE_REF, then the
4607 qualifying scope was dependent. */
4608 if (TREE_CODE (decl) == SCOPE_REF)
4610 /* At this point, we do not know if DECL is a valid
4611 integral constant expression. We assume that it is
4612 in fact such an expression, so that code like:
4614 template <int N> struct A {
4615 int a[B<N>::i];
4618 is accepted. At template-instantiation time, we
4619 will check that B<N>::i is actually a constant. */
4620 return decl;
4622 /* Check to see if DECL is a local variable in a context
4623 where that is forbidden. */
4624 if (parser->local_variables_forbidden_p
4625 && local_variable_p (decl))
4627 /* It might be that we only found DECL because we are
4628 trying to be generous with pre-ISO scoping rules.
4629 For example, consider:
4631 int i;
4632 void g() {
4633 for (int i = 0; i < 10; ++i) {}
4634 extern void f(int j = i);
4637 Here, name look up will originally find the out
4638 of scope `i'. We need to issue a warning message,
4639 but then use the global `i'. */
4640 decl = check_for_out_of_scope_variable (decl);
4641 if (local_variable_p (decl))
4643 error_at (id_expr_token->location,
4644 "local variable %qD may not appear in this context",
4645 decl);
4646 return error_mark_node;
4651 decl = (finish_id_expression
4652 (id_expression, decl, parser->scope,
4653 idk,
4654 parser->integral_constant_expression_p,
4655 parser->allow_non_integral_constant_expression_p,
4656 &parser->non_integral_constant_expression_p,
4657 template_p, done, address_p,
4658 template_arg_p,
4659 &error_msg,
4660 id_expr_token->location));
4661 if (error_msg)
4662 cp_parser_error (parser, error_msg);
4663 return decl;
4666 /* Anything else is an error. */
4667 default:
4668 cp_parser_error (parser, "expected primary-expression");
4669 return error_mark_node;
4673 static inline tree
4674 cp_parser_primary_expression (cp_parser *parser,
4675 bool address_p,
4676 bool cast_p,
4677 bool template_arg_p,
4678 cp_id_kind *idk)
4680 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4681 /*decltype*/false, idk);
4684 /* Parse an id-expression.
4686 id-expression:
4687 unqualified-id
4688 qualified-id
4690 qualified-id:
4691 :: [opt] nested-name-specifier template [opt] unqualified-id
4692 :: identifier
4693 :: operator-function-id
4694 :: template-id
4696 Return a representation of the unqualified portion of the
4697 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4698 a `::' or nested-name-specifier.
4700 Often, if the id-expression was a qualified-id, the caller will
4701 want to make a SCOPE_REF to represent the qualified-id. This
4702 function does not do this in order to avoid wastefully creating
4703 SCOPE_REFs when they are not required.
4705 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4706 `template' keyword.
4708 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4709 uninstantiated templates.
4711 If *TEMPLATE_P is non-NULL, it is set to true iff the
4712 `template' keyword is used to explicitly indicate that the entity
4713 named is a template.
4715 If DECLARATOR_P is true, the id-expression is appearing as part of
4716 a declarator, rather than as part of an expression. */
4718 static tree
4719 cp_parser_id_expression (cp_parser *parser,
4720 bool template_keyword_p,
4721 bool check_dependency_p,
4722 bool *template_p,
4723 bool declarator_p,
4724 bool optional_p)
4726 bool global_scope_p;
4727 bool nested_name_specifier_p;
4729 /* Assume the `template' keyword was not used. */
4730 if (template_p)
4731 *template_p = template_keyword_p;
4733 /* Look for the optional `::' operator. */
4734 global_scope_p
4735 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4736 != NULL_TREE);
4737 /* Look for the optional nested-name-specifier. */
4738 nested_name_specifier_p
4739 = (cp_parser_nested_name_specifier_opt (parser,
4740 /*typename_keyword_p=*/false,
4741 check_dependency_p,
4742 /*type_p=*/false,
4743 declarator_p)
4744 != NULL_TREE);
4745 /* If there is a nested-name-specifier, then we are looking at
4746 the first qualified-id production. */
4747 if (nested_name_specifier_p)
4749 tree saved_scope;
4750 tree saved_object_scope;
4751 tree saved_qualifying_scope;
4752 tree unqualified_id;
4753 bool is_template;
4755 /* See if the next token is the `template' keyword. */
4756 if (!template_p)
4757 template_p = &is_template;
4758 *template_p = cp_parser_optional_template_keyword (parser);
4759 /* Name lookup we do during the processing of the
4760 unqualified-id might obliterate SCOPE. */
4761 saved_scope = parser->scope;
4762 saved_object_scope = parser->object_scope;
4763 saved_qualifying_scope = parser->qualifying_scope;
4764 /* Process the final unqualified-id. */
4765 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4766 check_dependency_p,
4767 declarator_p,
4768 /*optional_p=*/false);
4769 /* Restore the SAVED_SCOPE for our caller. */
4770 parser->scope = saved_scope;
4771 parser->object_scope = saved_object_scope;
4772 parser->qualifying_scope = saved_qualifying_scope;
4774 return unqualified_id;
4776 /* Otherwise, if we are in global scope, then we are looking at one
4777 of the other qualified-id productions. */
4778 else if (global_scope_p)
4780 cp_token *token;
4781 tree id;
4783 /* Peek at the next token. */
4784 token = cp_lexer_peek_token (parser->lexer);
4786 /* If it's an identifier, and the next token is not a "<", then
4787 we can avoid the template-id case. This is an optimization
4788 for this common case. */
4789 if (token->type == CPP_NAME
4790 && !cp_parser_nth_token_starts_template_argument_list_p
4791 (parser, 2))
4792 return cp_parser_identifier (parser);
4794 cp_parser_parse_tentatively (parser);
4795 /* Try a template-id. */
4796 id = cp_parser_template_id (parser,
4797 /*template_keyword_p=*/false,
4798 /*check_dependency_p=*/true,
4799 none_type,
4800 declarator_p);
4801 /* If that worked, we're done. */
4802 if (cp_parser_parse_definitely (parser))
4803 return id;
4805 /* Peek at the next token. (Changes in the token buffer may
4806 have invalidated the pointer obtained above.) */
4807 token = cp_lexer_peek_token (parser->lexer);
4809 switch (token->type)
4811 case CPP_NAME:
4812 return cp_parser_identifier (parser);
4814 case CPP_KEYWORD:
4815 if (token->keyword == RID_OPERATOR)
4816 return cp_parser_operator_function_id (parser);
4817 /* Fall through. */
4819 default:
4820 cp_parser_error (parser, "expected id-expression");
4821 return error_mark_node;
4824 else
4825 return cp_parser_unqualified_id (parser, template_keyword_p,
4826 /*check_dependency_p=*/true,
4827 declarator_p,
4828 optional_p);
4831 /* Parse an unqualified-id.
4833 unqualified-id:
4834 identifier
4835 operator-function-id
4836 conversion-function-id
4837 ~ class-name
4838 template-id
4840 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4841 keyword, in a construct like `A::template ...'.
4843 Returns a representation of unqualified-id. For the `identifier'
4844 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4845 production a BIT_NOT_EXPR is returned; the operand of the
4846 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4847 other productions, see the documentation accompanying the
4848 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4849 names are looked up in uninstantiated templates. If DECLARATOR_P
4850 is true, the unqualified-id is appearing as part of a declarator,
4851 rather than as part of an expression. */
4853 static tree
4854 cp_parser_unqualified_id (cp_parser* parser,
4855 bool template_keyword_p,
4856 bool check_dependency_p,
4857 bool declarator_p,
4858 bool optional_p)
4860 cp_token *token;
4862 /* Peek at the next token. */
4863 token = cp_lexer_peek_token (parser->lexer);
4865 switch ((int) token->type)
4867 case CPP_NAME:
4869 tree id;
4871 /* We don't know yet whether or not this will be a
4872 template-id. */
4873 cp_parser_parse_tentatively (parser);
4874 /* Try a template-id. */
4875 id = cp_parser_template_id (parser, template_keyword_p,
4876 check_dependency_p,
4877 none_type,
4878 declarator_p);
4879 /* If it worked, we're done. */
4880 if (cp_parser_parse_definitely (parser))
4881 return id;
4882 /* Otherwise, it's an ordinary identifier. */
4883 return cp_parser_identifier (parser);
4886 case CPP_TEMPLATE_ID:
4887 return cp_parser_template_id (parser, template_keyword_p,
4888 check_dependency_p,
4889 none_type,
4890 declarator_p);
4892 case CPP_COMPL:
4894 tree type_decl;
4895 tree qualifying_scope;
4896 tree object_scope;
4897 tree scope;
4898 bool done;
4900 /* Consume the `~' token. */
4901 cp_lexer_consume_token (parser->lexer);
4902 /* Parse the class-name. The standard, as written, seems to
4903 say that:
4905 template <typename T> struct S { ~S (); };
4906 template <typename T> S<T>::~S() {}
4908 is invalid, since `~' must be followed by a class-name, but
4909 `S<T>' is dependent, and so not known to be a class.
4910 That's not right; we need to look in uninstantiated
4911 templates. A further complication arises from:
4913 template <typename T> void f(T t) {
4914 t.T::~T();
4917 Here, it is not possible to look up `T' in the scope of `T'
4918 itself. We must look in both the current scope, and the
4919 scope of the containing complete expression.
4921 Yet another issue is:
4923 struct S {
4924 int S;
4925 ~S();
4928 S::~S() {}
4930 The standard does not seem to say that the `S' in `~S'
4931 should refer to the type `S' and not the data member
4932 `S::S'. */
4934 /* DR 244 says that we look up the name after the "~" in the
4935 same scope as we looked up the qualifying name. That idea
4936 isn't fully worked out; it's more complicated than that. */
4937 scope = parser->scope;
4938 object_scope = parser->object_scope;
4939 qualifying_scope = parser->qualifying_scope;
4941 /* Check for invalid scopes. */
4942 if (scope == error_mark_node)
4944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4945 cp_lexer_consume_token (parser->lexer);
4946 return error_mark_node;
4948 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4950 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4951 error_at (token->location,
4952 "scope %qT before %<~%> is not a class-name",
4953 scope);
4954 cp_parser_simulate_error (parser);
4955 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4956 cp_lexer_consume_token (parser->lexer);
4957 return error_mark_node;
4959 gcc_assert (!scope || TYPE_P (scope));
4961 /* If the name is of the form "X::~X" it's OK even if X is a
4962 typedef. */
4963 token = cp_lexer_peek_token (parser->lexer);
4964 if (scope
4965 && token->type == CPP_NAME
4966 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4967 != CPP_LESS)
4968 && (token->u.value == TYPE_IDENTIFIER (scope)
4969 || (CLASS_TYPE_P (scope)
4970 && constructor_name_p (token->u.value, scope))))
4972 cp_lexer_consume_token (parser->lexer);
4973 return build_nt (BIT_NOT_EXPR, scope);
4976 /* ~auto means the destructor of whatever the object is. */
4977 if (cp_parser_is_keyword (token, RID_AUTO))
4979 if (cxx_dialect < cxx14)
4980 pedwarn (input_location, 0,
4981 "%<~auto%> only available with "
4982 "-std=c++14 or -std=gnu++14");
4983 cp_lexer_consume_token (parser->lexer);
4984 return build_nt (BIT_NOT_EXPR, make_auto ());
4987 /* If there was an explicit qualification (S::~T), first look
4988 in the scope given by the qualification (i.e., S).
4990 Note: in the calls to cp_parser_class_name below we pass
4991 typename_type so that lookup finds the injected-class-name
4992 rather than the constructor. */
4993 done = false;
4994 type_decl = NULL_TREE;
4995 if (scope)
4997 cp_parser_parse_tentatively (parser);
4998 type_decl = cp_parser_class_name (parser,
4999 /*typename_keyword_p=*/false,
5000 /*template_keyword_p=*/false,
5001 typename_type,
5002 /*check_dependency=*/false,
5003 /*class_head_p=*/false,
5004 declarator_p);
5005 if (cp_parser_parse_definitely (parser))
5006 done = true;
5008 /* In "N::S::~S", look in "N" as well. */
5009 if (!done && scope && qualifying_scope)
5011 cp_parser_parse_tentatively (parser);
5012 parser->scope = qualifying_scope;
5013 parser->object_scope = NULL_TREE;
5014 parser->qualifying_scope = NULL_TREE;
5015 type_decl
5016 = cp_parser_class_name (parser,
5017 /*typename_keyword_p=*/false,
5018 /*template_keyword_p=*/false,
5019 typename_type,
5020 /*check_dependency=*/false,
5021 /*class_head_p=*/false,
5022 declarator_p);
5023 if (cp_parser_parse_definitely (parser))
5024 done = true;
5026 /* In "p->S::~T", look in the scope given by "*p" as well. */
5027 else if (!done && object_scope)
5029 cp_parser_parse_tentatively (parser);
5030 parser->scope = object_scope;
5031 parser->object_scope = NULL_TREE;
5032 parser->qualifying_scope = NULL_TREE;
5033 type_decl
5034 = cp_parser_class_name (parser,
5035 /*typename_keyword_p=*/false,
5036 /*template_keyword_p=*/false,
5037 typename_type,
5038 /*check_dependency=*/false,
5039 /*class_head_p=*/false,
5040 declarator_p);
5041 if (cp_parser_parse_definitely (parser))
5042 done = true;
5044 /* Look in the surrounding context. */
5045 if (!done)
5047 parser->scope = NULL_TREE;
5048 parser->object_scope = NULL_TREE;
5049 parser->qualifying_scope = NULL_TREE;
5050 if (processing_template_decl)
5051 cp_parser_parse_tentatively (parser);
5052 type_decl
5053 = cp_parser_class_name (parser,
5054 /*typename_keyword_p=*/false,
5055 /*template_keyword_p=*/false,
5056 typename_type,
5057 /*check_dependency=*/false,
5058 /*class_head_p=*/false,
5059 declarator_p);
5060 if (processing_template_decl
5061 && ! cp_parser_parse_definitely (parser))
5063 /* We couldn't find a type with this name, so just accept
5064 it and check for a match at instantiation time. */
5065 type_decl = cp_parser_identifier (parser);
5066 if (type_decl != error_mark_node)
5067 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5068 return type_decl;
5071 /* If an error occurred, assume that the name of the
5072 destructor is the same as the name of the qualifying
5073 class. That allows us to keep parsing after running
5074 into ill-formed destructor names. */
5075 if (type_decl == error_mark_node && scope)
5076 return build_nt (BIT_NOT_EXPR, scope);
5077 else if (type_decl == error_mark_node)
5078 return error_mark_node;
5080 /* Check that destructor name and scope match. */
5081 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5083 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5084 error_at (token->location,
5085 "declaration of %<~%T%> as member of %qT",
5086 type_decl, scope);
5087 cp_parser_simulate_error (parser);
5088 return error_mark_node;
5091 /* [class.dtor]
5093 A typedef-name that names a class shall not be used as the
5094 identifier in the declarator for a destructor declaration. */
5095 if (declarator_p
5096 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5097 && !DECL_SELF_REFERENCE_P (type_decl)
5098 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5099 error_at (token->location,
5100 "typedef-name %qD used as destructor declarator",
5101 type_decl);
5103 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5106 case CPP_KEYWORD:
5107 if (token->keyword == RID_OPERATOR)
5109 tree id;
5111 /* This could be a template-id, so we try that first. */
5112 cp_parser_parse_tentatively (parser);
5113 /* Try a template-id. */
5114 id = cp_parser_template_id (parser, template_keyword_p,
5115 /*check_dependency_p=*/true,
5116 none_type,
5117 declarator_p);
5118 /* If that worked, we're done. */
5119 if (cp_parser_parse_definitely (parser))
5120 return id;
5121 /* We still don't know whether we're looking at an
5122 operator-function-id or a conversion-function-id. */
5123 cp_parser_parse_tentatively (parser);
5124 /* Try an operator-function-id. */
5125 id = cp_parser_operator_function_id (parser);
5126 /* If that didn't work, try a conversion-function-id. */
5127 if (!cp_parser_parse_definitely (parser))
5128 id = cp_parser_conversion_function_id (parser);
5129 else if (UDLIT_OPER_P (id))
5131 /* 17.6.3.3.5 */
5132 const char *name = UDLIT_OP_SUFFIX (id);
5133 if (name[0] != '_' && !in_system_header_at (input_location)
5134 && declarator_p)
5135 warning (0, "literal operator suffixes not preceded by %<_%>"
5136 " are reserved for future standardization");
5139 return id;
5141 /* Fall through. */
5143 default:
5144 if (optional_p)
5145 return NULL_TREE;
5146 cp_parser_error (parser, "expected unqualified-id");
5147 return error_mark_node;
5151 /* Parse an (optional) nested-name-specifier.
5153 nested-name-specifier: [C++98]
5154 class-or-namespace-name :: nested-name-specifier [opt]
5155 class-or-namespace-name :: template nested-name-specifier [opt]
5157 nested-name-specifier: [C++0x]
5158 type-name ::
5159 namespace-name ::
5160 nested-name-specifier identifier ::
5161 nested-name-specifier template [opt] simple-template-id ::
5163 PARSER->SCOPE should be set appropriately before this function is
5164 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5165 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5166 in name lookups.
5168 Sets PARSER->SCOPE to the class (TYPE) or namespace
5169 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5170 it unchanged if there is no nested-name-specifier. Returns the new
5171 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5173 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5174 part of a declaration and/or decl-specifier. */
5176 static tree
5177 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5178 bool typename_keyword_p,
5179 bool check_dependency_p,
5180 bool type_p,
5181 bool is_declaration)
5183 bool success = false;
5184 cp_token_position start = 0;
5185 cp_token *token;
5187 /* Remember where the nested-name-specifier starts. */
5188 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5190 start = cp_lexer_token_position (parser->lexer, false);
5191 push_deferring_access_checks (dk_deferred);
5194 while (true)
5196 tree new_scope;
5197 tree old_scope;
5198 tree saved_qualifying_scope;
5199 bool template_keyword_p;
5201 /* Spot cases that cannot be the beginning of a
5202 nested-name-specifier. */
5203 token = cp_lexer_peek_token (parser->lexer);
5205 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5206 the already parsed nested-name-specifier. */
5207 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5209 /* Grab the nested-name-specifier and continue the loop. */
5210 cp_parser_pre_parsed_nested_name_specifier (parser);
5211 /* If we originally encountered this nested-name-specifier
5212 with IS_DECLARATION set to false, we will not have
5213 resolved TYPENAME_TYPEs, so we must do so here. */
5214 if (is_declaration
5215 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5217 new_scope = resolve_typename_type (parser->scope,
5218 /*only_current_p=*/false);
5219 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5220 parser->scope = new_scope;
5222 success = true;
5223 continue;
5226 /* Spot cases that cannot be the beginning of a
5227 nested-name-specifier. On the second and subsequent times
5228 through the loop, we look for the `template' keyword. */
5229 if (success && token->keyword == RID_TEMPLATE)
5231 /* A template-id can start a nested-name-specifier. */
5232 else if (token->type == CPP_TEMPLATE_ID)
5234 /* DR 743: decltype can be used in a nested-name-specifier. */
5235 else if (token_is_decltype (token))
5237 else
5239 /* If the next token is not an identifier, then it is
5240 definitely not a type-name or namespace-name. */
5241 if (token->type != CPP_NAME)
5242 break;
5243 /* If the following token is neither a `<' (to begin a
5244 template-id), nor a `::', then we are not looking at a
5245 nested-name-specifier. */
5246 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5248 if (token->type == CPP_COLON
5249 && parser->colon_corrects_to_scope_p
5250 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5252 error_at (token->location,
5253 "found %<:%> in nested-name-specifier, expected %<::%>");
5254 token->type = CPP_SCOPE;
5257 if (token->type != CPP_SCOPE
5258 && !cp_parser_nth_token_starts_template_argument_list_p
5259 (parser, 2))
5260 break;
5263 /* The nested-name-specifier is optional, so we parse
5264 tentatively. */
5265 cp_parser_parse_tentatively (parser);
5267 /* Look for the optional `template' keyword, if this isn't the
5268 first time through the loop. */
5269 if (success)
5270 template_keyword_p = cp_parser_optional_template_keyword (parser);
5271 else
5272 template_keyword_p = false;
5274 /* Save the old scope since the name lookup we are about to do
5275 might destroy it. */
5276 old_scope = parser->scope;
5277 saved_qualifying_scope = parser->qualifying_scope;
5278 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5279 look up names in "X<T>::I" in order to determine that "Y" is
5280 a template. So, if we have a typename at this point, we make
5281 an effort to look through it. */
5282 if (is_declaration
5283 && !typename_keyword_p
5284 && parser->scope
5285 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5286 parser->scope = resolve_typename_type (parser->scope,
5287 /*only_current_p=*/false);
5288 /* Parse the qualifying entity. */
5289 new_scope
5290 = cp_parser_qualifying_entity (parser,
5291 typename_keyword_p,
5292 template_keyword_p,
5293 check_dependency_p,
5294 type_p,
5295 is_declaration);
5296 /* Look for the `::' token. */
5297 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5299 /* If we found what we wanted, we keep going; otherwise, we're
5300 done. */
5301 if (!cp_parser_parse_definitely (parser))
5303 bool error_p = false;
5305 /* Restore the OLD_SCOPE since it was valid before the
5306 failed attempt at finding the last
5307 class-or-namespace-name. */
5308 parser->scope = old_scope;
5309 parser->qualifying_scope = saved_qualifying_scope;
5311 /* If the next token is a decltype, and the one after that is a
5312 `::', then the decltype has failed to resolve to a class or
5313 enumeration type. Give this error even when parsing
5314 tentatively since it can't possibly be valid--and we're going
5315 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5316 won't get another chance.*/
5317 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5318 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5319 == CPP_SCOPE))
5321 token = cp_lexer_consume_token (parser->lexer);
5322 error_at (token->location, "decltype evaluates to %qT, "
5323 "which is not a class or enumeration type",
5324 token->u.value);
5325 parser->scope = error_mark_node;
5326 error_p = true;
5327 /* As below. */
5328 success = true;
5329 cp_lexer_consume_token (parser->lexer);
5332 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5333 break;
5334 /* If the next token is an identifier, and the one after
5335 that is a `::', then any valid interpretation would have
5336 found a class-or-namespace-name. */
5337 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5338 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5339 == CPP_SCOPE)
5340 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5341 != CPP_COMPL))
5343 token = cp_lexer_consume_token (parser->lexer);
5344 if (!error_p)
5346 if (!token->error_reported)
5348 tree decl;
5349 tree ambiguous_decls;
5351 decl = cp_parser_lookup_name (parser, token->u.value,
5352 none_type,
5353 /*is_template=*/false,
5354 /*is_namespace=*/false,
5355 /*check_dependency=*/true,
5356 &ambiguous_decls,
5357 token->location);
5358 if (TREE_CODE (decl) == TEMPLATE_DECL)
5359 error_at (token->location,
5360 "%qD used without template parameters",
5361 decl);
5362 else if (ambiguous_decls)
5364 // cp_parser_lookup_name has the same diagnostic,
5365 // thus make sure to emit it at most once.
5366 if (cp_parser_uncommitted_to_tentative_parse_p
5367 (parser))
5369 error_at (token->location,
5370 "reference to %qD is ambiguous",
5371 token->u.value);
5372 print_candidates (ambiguous_decls);
5374 decl = error_mark_node;
5376 else
5378 if (cxx_dialect != cxx98)
5379 cp_parser_name_lookup_error
5380 (parser, token->u.value, decl, NLE_NOT_CXX98,
5381 token->location);
5382 else
5383 cp_parser_name_lookup_error
5384 (parser, token->u.value, decl, NLE_CXX98,
5385 token->location);
5388 parser->scope = error_mark_node;
5389 error_p = true;
5390 /* Treat this as a successful nested-name-specifier
5391 due to:
5393 [basic.lookup.qual]
5395 If the name found is not a class-name (clause
5396 _class_) or namespace-name (_namespace.def_), the
5397 program is ill-formed. */
5398 success = true;
5400 cp_lexer_consume_token (parser->lexer);
5402 break;
5404 /* We've found one valid nested-name-specifier. */
5405 success = true;
5406 /* Name lookup always gives us a DECL. */
5407 if (TREE_CODE (new_scope) == TYPE_DECL)
5408 new_scope = TREE_TYPE (new_scope);
5409 /* Uses of "template" must be followed by actual templates. */
5410 if (template_keyword_p
5411 && !(CLASS_TYPE_P (new_scope)
5412 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5414 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5415 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5416 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5417 == TEMPLATE_ID_EXPR)))
5418 permerror (input_location, TYPE_P (new_scope)
5419 ? G_("%qT is not a template")
5420 : G_("%qD is not a template"),
5421 new_scope);
5422 /* If it is a class scope, try to complete it; we are about to
5423 be looking up names inside the class. */
5424 if (TYPE_P (new_scope)
5425 /* Since checking types for dependency can be expensive,
5426 avoid doing it if the type is already complete. */
5427 && !COMPLETE_TYPE_P (new_scope)
5428 /* Do not try to complete dependent types. */
5429 && !dependent_type_p (new_scope))
5431 new_scope = complete_type (new_scope);
5432 /* If it is a typedef to current class, use the current
5433 class instead, as the typedef won't have any names inside
5434 it yet. */
5435 if (!COMPLETE_TYPE_P (new_scope)
5436 && currently_open_class (new_scope))
5437 new_scope = TYPE_MAIN_VARIANT (new_scope);
5439 /* Make sure we look in the right scope the next time through
5440 the loop. */
5441 parser->scope = new_scope;
5444 /* If parsing tentatively, replace the sequence of tokens that makes
5445 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5446 token. That way, should we re-parse the token stream, we will
5447 not have to repeat the effort required to do the parse, nor will
5448 we issue duplicate error messages. */
5449 if (success && start)
5451 cp_token *token;
5453 token = cp_lexer_token_at (parser->lexer, start);
5454 /* Reset the contents of the START token. */
5455 token->type = CPP_NESTED_NAME_SPECIFIER;
5456 /* Retrieve any deferred checks. Do not pop this access checks yet
5457 so the memory will not be reclaimed during token replacing below. */
5458 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5459 token->u.tree_check_value->value = parser->scope;
5460 token->u.tree_check_value->checks = get_deferred_access_checks ();
5461 token->u.tree_check_value->qualifying_scope =
5462 parser->qualifying_scope;
5463 token->keyword = RID_MAX;
5465 /* Purge all subsequent tokens. */
5466 cp_lexer_purge_tokens_after (parser->lexer, start);
5469 if (start)
5470 pop_to_parent_deferring_access_checks ();
5472 return success ? parser->scope : NULL_TREE;
5475 /* Parse a nested-name-specifier. See
5476 cp_parser_nested_name_specifier_opt for details. This function
5477 behaves identically, except that it will an issue an error if no
5478 nested-name-specifier is present. */
5480 static tree
5481 cp_parser_nested_name_specifier (cp_parser *parser,
5482 bool typename_keyword_p,
5483 bool check_dependency_p,
5484 bool type_p,
5485 bool is_declaration)
5487 tree scope;
5489 /* Look for the nested-name-specifier. */
5490 scope = cp_parser_nested_name_specifier_opt (parser,
5491 typename_keyword_p,
5492 check_dependency_p,
5493 type_p,
5494 is_declaration);
5495 /* If it was not present, issue an error message. */
5496 if (!scope)
5498 cp_parser_error (parser, "expected nested-name-specifier");
5499 parser->scope = NULL_TREE;
5502 return scope;
5505 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5506 this is either a class-name or a namespace-name (which corresponds
5507 to the class-or-namespace-name production in the grammar). For
5508 C++0x, it can also be a type-name that refers to an enumeration
5509 type or a simple-template-id.
5511 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5512 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5513 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5514 TYPE_P is TRUE iff the next name should be taken as a class-name,
5515 even the same name is declared to be another entity in the same
5516 scope.
5518 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5519 specified by the class-or-namespace-name. If neither is found the
5520 ERROR_MARK_NODE is returned. */
5522 static tree
5523 cp_parser_qualifying_entity (cp_parser *parser,
5524 bool typename_keyword_p,
5525 bool template_keyword_p,
5526 bool check_dependency_p,
5527 bool type_p,
5528 bool is_declaration)
5530 tree saved_scope;
5531 tree saved_qualifying_scope;
5532 tree saved_object_scope;
5533 tree scope;
5534 bool only_class_p;
5535 bool successful_parse_p;
5537 /* DR 743: decltype can appear in a nested-name-specifier. */
5538 if (cp_lexer_next_token_is_decltype (parser->lexer))
5540 scope = cp_parser_decltype (parser);
5541 if (TREE_CODE (scope) != ENUMERAL_TYPE
5542 && !MAYBE_CLASS_TYPE_P (scope))
5544 cp_parser_simulate_error (parser);
5545 return error_mark_node;
5547 if (TYPE_NAME (scope))
5548 scope = TYPE_NAME (scope);
5549 return scope;
5552 /* Before we try to parse the class-name, we must save away the
5553 current PARSER->SCOPE since cp_parser_class_name will destroy
5554 it. */
5555 saved_scope = parser->scope;
5556 saved_qualifying_scope = parser->qualifying_scope;
5557 saved_object_scope = parser->object_scope;
5558 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5559 there is no need to look for a namespace-name. */
5560 only_class_p = template_keyword_p
5561 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5562 if (!only_class_p)
5563 cp_parser_parse_tentatively (parser);
5564 scope = cp_parser_class_name (parser,
5565 typename_keyword_p,
5566 template_keyword_p,
5567 type_p ? class_type : none_type,
5568 check_dependency_p,
5569 /*class_head_p=*/false,
5570 is_declaration);
5571 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5572 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5573 if (!only_class_p
5574 && cxx_dialect != cxx98
5575 && !successful_parse_p)
5577 /* Restore the saved scope. */
5578 parser->scope = saved_scope;
5579 parser->qualifying_scope = saved_qualifying_scope;
5580 parser->object_scope = saved_object_scope;
5582 /* Parse tentatively. */
5583 cp_parser_parse_tentatively (parser);
5585 /* Parse a type-name */
5586 scope = cp_parser_type_name (parser);
5588 /* "If the name found does not designate a namespace or a class,
5589 enumeration, or dependent type, the program is ill-formed."
5591 We cover classes and dependent types above and namespaces below,
5592 so this code is only looking for enums. */
5593 if (!scope || TREE_CODE (scope) != TYPE_DECL
5594 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5595 cp_parser_simulate_error (parser);
5597 successful_parse_p = cp_parser_parse_definitely (parser);
5599 /* If that didn't work, try for a namespace-name. */
5600 if (!only_class_p && !successful_parse_p)
5602 /* Restore the saved scope. */
5603 parser->scope = saved_scope;
5604 parser->qualifying_scope = saved_qualifying_scope;
5605 parser->object_scope = saved_object_scope;
5606 /* If we are not looking at an identifier followed by the scope
5607 resolution operator, then this is not part of a
5608 nested-name-specifier. (Note that this function is only used
5609 to parse the components of a nested-name-specifier.) */
5610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5611 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5612 return error_mark_node;
5613 scope = cp_parser_namespace_name (parser);
5616 return scope;
5619 /* Return true if we are looking at a compound-literal, false otherwise. */
5621 static bool
5622 cp_parser_compound_literal_p (cp_parser *parser)
5624 /* Consume the `('. */
5625 cp_lexer_consume_token (parser->lexer);
5627 cp_lexer_save_tokens (parser->lexer);
5629 /* Skip tokens until the next token is a closing parenthesis.
5630 If we find the closing `)', and the next token is a `{', then
5631 we are looking at a compound-literal. */
5632 bool compound_literal_p
5633 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5634 /*consume_paren=*/true)
5635 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5637 /* Roll back the tokens we skipped. */
5638 cp_lexer_rollback_tokens (parser->lexer);
5640 return compound_literal_p;
5643 /* Parse a postfix-expression.
5645 postfix-expression:
5646 primary-expression
5647 postfix-expression [ expression ]
5648 postfix-expression ( expression-list [opt] )
5649 simple-type-specifier ( expression-list [opt] )
5650 typename :: [opt] nested-name-specifier identifier
5651 ( expression-list [opt] )
5652 typename :: [opt] nested-name-specifier template [opt] template-id
5653 ( expression-list [opt] )
5654 postfix-expression . template [opt] id-expression
5655 postfix-expression -> template [opt] id-expression
5656 postfix-expression . pseudo-destructor-name
5657 postfix-expression -> pseudo-destructor-name
5658 postfix-expression ++
5659 postfix-expression --
5660 dynamic_cast < type-id > ( expression )
5661 static_cast < type-id > ( expression )
5662 reinterpret_cast < type-id > ( expression )
5663 const_cast < type-id > ( expression )
5664 typeid ( expression )
5665 typeid ( type-id )
5667 GNU Extension:
5669 postfix-expression:
5670 ( type-id ) { initializer-list , [opt] }
5672 This extension is a GNU version of the C99 compound-literal
5673 construct. (The C99 grammar uses `type-name' instead of `type-id',
5674 but they are essentially the same concept.)
5676 If ADDRESS_P is true, the postfix expression is the operand of the
5677 `&' operator. CAST_P is true if this expression is the target of a
5678 cast.
5680 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5681 class member access expressions [expr.ref].
5683 Returns a representation of the expression. */
5685 static tree
5686 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5687 bool member_access_only_p, bool decltype_p,
5688 cp_id_kind * pidk_return)
5690 cp_token *token;
5691 location_t loc;
5692 enum rid keyword;
5693 cp_id_kind idk = CP_ID_KIND_NONE;
5694 tree postfix_expression = NULL_TREE;
5695 bool is_member_access = false;
5696 int saved_in_statement = -1;
5698 /* Peek at the next token. */
5699 token = cp_lexer_peek_token (parser->lexer);
5700 loc = token->location;
5701 /* Some of the productions are determined by keywords. */
5702 keyword = token->keyword;
5703 switch (keyword)
5705 case RID_DYNCAST:
5706 case RID_STATCAST:
5707 case RID_REINTCAST:
5708 case RID_CONSTCAST:
5710 tree type;
5711 tree expression;
5712 const char *saved_message;
5713 bool saved_in_type_id_in_expr_p;
5715 /* All of these can be handled in the same way from the point
5716 of view of parsing. Begin by consuming the token
5717 identifying the cast. */
5718 cp_lexer_consume_token (parser->lexer);
5720 /* New types cannot be defined in the cast. */
5721 saved_message = parser->type_definition_forbidden_message;
5722 parser->type_definition_forbidden_message
5723 = G_("types may not be defined in casts");
5725 /* Look for the opening `<'. */
5726 cp_parser_require (parser, CPP_LESS, RT_LESS);
5727 /* Parse the type to which we are casting. */
5728 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5729 parser->in_type_id_in_expr_p = true;
5730 type = cp_parser_type_id (parser);
5731 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5732 /* Look for the closing `>'. */
5733 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5734 /* Restore the old message. */
5735 parser->type_definition_forbidden_message = saved_message;
5737 bool saved_greater_than_is_operator_p
5738 = parser->greater_than_is_operator_p;
5739 parser->greater_than_is_operator_p = true;
5741 /* And the expression which is being cast. */
5742 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5743 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5744 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5746 parser->greater_than_is_operator_p
5747 = saved_greater_than_is_operator_p;
5749 /* Only type conversions to integral or enumeration types
5750 can be used in constant-expressions. */
5751 if (!cast_valid_in_integral_constant_expression_p (type)
5752 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5753 return error_mark_node;
5755 switch (keyword)
5757 case RID_DYNCAST:
5758 postfix_expression
5759 = build_dynamic_cast (type, expression, tf_warning_or_error);
5760 break;
5761 case RID_STATCAST:
5762 postfix_expression
5763 = build_static_cast (type, expression, tf_warning_or_error);
5764 break;
5765 case RID_REINTCAST:
5766 postfix_expression
5767 = build_reinterpret_cast (type, expression,
5768 tf_warning_or_error);
5769 break;
5770 case RID_CONSTCAST:
5771 postfix_expression
5772 = build_const_cast (type, expression, tf_warning_or_error);
5773 break;
5774 default:
5775 gcc_unreachable ();
5778 break;
5780 case RID_TYPEID:
5782 tree type;
5783 const char *saved_message;
5784 bool saved_in_type_id_in_expr_p;
5786 /* Consume the `typeid' token. */
5787 cp_lexer_consume_token (parser->lexer);
5788 /* Look for the `(' token. */
5789 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5790 /* Types cannot be defined in a `typeid' expression. */
5791 saved_message = parser->type_definition_forbidden_message;
5792 parser->type_definition_forbidden_message
5793 = G_("types may not be defined in a %<typeid%> expression");
5794 /* We can't be sure yet whether we're looking at a type-id or an
5795 expression. */
5796 cp_parser_parse_tentatively (parser);
5797 /* Try a type-id first. */
5798 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5799 parser->in_type_id_in_expr_p = true;
5800 type = cp_parser_type_id (parser);
5801 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5802 /* Look for the `)' token. Otherwise, we can't be sure that
5803 we're not looking at an expression: consider `typeid (int
5804 (3))', for example. */
5805 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5806 /* If all went well, simply lookup the type-id. */
5807 if (cp_parser_parse_definitely (parser))
5808 postfix_expression = get_typeid (type, tf_warning_or_error);
5809 /* Otherwise, fall back to the expression variant. */
5810 else
5812 tree expression;
5814 /* Look for an expression. */
5815 expression = cp_parser_expression (parser, & idk);
5816 /* Compute its typeid. */
5817 postfix_expression = build_typeid (expression, tf_warning_or_error);
5818 /* Look for the `)' token. */
5819 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5821 /* Restore the saved message. */
5822 parser->type_definition_forbidden_message = saved_message;
5823 /* `typeid' may not appear in an integral constant expression. */
5824 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5825 return error_mark_node;
5827 break;
5829 case RID_TYPENAME:
5831 tree type;
5832 /* The syntax permitted here is the same permitted for an
5833 elaborated-type-specifier. */
5834 type = cp_parser_elaborated_type_specifier (parser,
5835 /*is_friend=*/false,
5836 /*is_declaration=*/false);
5837 postfix_expression = cp_parser_functional_cast (parser, type);
5839 break;
5841 case RID_CILK_SPAWN:
5843 cp_lexer_consume_token (parser->lexer);
5844 token = cp_lexer_peek_token (parser->lexer);
5845 if (token->type == CPP_SEMICOLON)
5847 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5848 "an expression");
5849 postfix_expression = error_mark_node;
5850 break;
5852 else if (!current_function_decl)
5854 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5855 "inside a function");
5856 postfix_expression = error_mark_node;
5857 break;
5859 else
5861 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5862 saved_in_statement = parser->in_statement;
5863 parser->in_statement |= IN_CILK_SPAWN;
5865 cfun->calls_cilk_spawn = 1;
5866 postfix_expression =
5867 cp_parser_postfix_expression (parser, false, false,
5868 false, false, &idk);
5869 if (!flag_cilkplus)
5871 error_at (token->location, "-fcilkplus must be enabled to use"
5872 " %<_Cilk_spawn%>");
5873 cfun->calls_cilk_spawn = 0;
5875 else if (saved_in_statement & IN_CILK_SPAWN)
5877 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5878 "are not permitted");
5879 postfix_expression = error_mark_node;
5880 cfun->calls_cilk_spawn = 0;
5882 else
5884 postfix_expression = build_cilk_spawn (token->location,
5885 postfix_expression);
5886 if (postfix_expression != error_mark_node)
5887 SET_EXPR_LOCATION (postfix_expression, input_location);
5888 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5890 break;
5893 case RID_BUILTIN_SHUFFLE:
5895 vec<tree, va_gc> *vec;
5896 unsigned int i;
5897 tree p;
5899 cp_lexer_consume_token (parser->lexer);
5900 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5901 /*cast_p=*/false, /*allow_expansion_p=*/true,
5902 /*non_constant_p=*/NULL);
5903 if (vec == NULL)
5904 return error_mark_node;
5906 FOR_EACH_VEC_ELT (*vec, i, p)
5907 mark_exp_read (p);
5909 if (vec->length () == 2)
5910 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5911 tf_warning_or_error);
5912 else if (vec->length () == 3)
5913 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5914 tf_warning_or_error);
5915 else
5917 error_at (loc, "wrong number of arguments to "
5918 "%<__builtin_shuffle%>");
5919 return error_mark_node;
5921 break;
5924 default:
5926 tree type;
5928 /* If the next thing is a simple-type-specifier, we may be
5929 looking at a functional cast. We could also be looking at
5930 an id-expression. So, we try the functional cast, and if
5931 that doesn't work we fall back to the primary-expression. */
5932 cp_parser_parse_tentatively (parser);
5933 /* Look for the simple-type-specifier. */
5934 type = cp_parser_simple_type_specifier (parser,
5935 /*decl_specs=*/NULL,
5936 CP_PARSER_FLAGS_NONE);
5937 /* Parse the cast itself. */
5938 if (!cp_parser_error_occurred (parser))
5939 postfix_expression
5940 = cp_parser_functional_cast (parser, type);
5941 /* If that worked, we're done. */
5942 if (cp_parser_parse_definitely (parser))
5943 break;
5945 /* If the functional-cast didn't work out, try a
5946 compound-literal. */
5947 if (cp_parser_allow_gnu_extensions_p (parser)
5948 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5950 tree initializer = NULL_TREE;
5952 cp_parser_parse_tentatively (parser);
5954 /* Avoid calling cp_parser_type_id pointlessly, see comment
5955 in cp_parser_cast_expression about c++/29234. */
5956 if (!cp_parser_compound_literal_p (parser))
5957 cp_parser_simulate_error (parser);
5958 else
5960 /* Parse the type. */
5961 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5962 parser->in_type_id_in_expr_p = true;
5963 type = cp_parser_type_id (parser);
5964 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5965 /* Look for the `)'. */
5966 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5969 /* If things aren't going well, there's no need to
5970 keep going. */
5971 if (!cp_parser_error_occurred (parser))
5973 bool non_constant_p;
5974 /* Parse the brace-enclosed initializer list. */
5975 initializer = cp_parser_braced_list (parser,
5976 &non_constant_p);
5978 /* If that worked, we're definitely looking at a
5979 compound-literal expression. */
5980 if (cp_parser_parse_definitely (parser))
5982 /* Warn the user that a compound literal is not
5983 allowed in standard C++. */
5984 pedwarn (input_location, OPT_Wpedantic,
5985 "ISO C++ forbids compound-literals");
5986 /* For simplicity, we disallow compound literals in
5987 constant-expressions. We could
5988 allow compound literals of integer type, whose
5989 initializer was a constant, in constant
5990 expressions. Permitting that usage, as a further
5991 extension, would not change the meaning of any
5992 currently accepted programs. (Of course, as
5993 compound literals are not part of ISO C++, the
5994 standard has nothing to say.) */
5995 if (cp_parser_non_integral_constant_expression (parser,
5996 NIC_NCC))
5998 postfix_expression = error_mark_node;
5999 break;
6001 /* Form the representation of the compound-literal. */
6002 postfix_expression
6003 = finish_compound_literal (type, initializer,
6004 tf_warning_or_error);
6005 break;
6009 /* It must be a primary-expression. */
6010 postfix_expression
6011 = cp_parser_primary_expression (parser, address_p, cast_p,
6012 /*template_arg_p=*/false,
6013 decltype_p,
6014 &idk);
6016 break;
6019 /* Note that we don't need to worry about calling build_cplus_new on a
6020 class-valued CALL_EXPR in decltype when it isn't the end of the
6021 postfix-expression; unary_complex_lvalue will take care of that for
6022 all these cases. */
6024 /* Keep looping until the postfix-expression is complete. */
6025 while (true)
6027 if (idk == CP_ID_KIND_UNQUALIFIED
6028 && identifier_p (postfix_expression)
6029 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6030 /* It is not a Koenig lookup function call. */
6031 postfix_expression
6032 = unqualified_name_lookup_error (postfix_expression);
6034 /* Peek at the next token. */
6035 token = cp_lexer_peek_token (parser->lexer);
6037 switch (token->type)
6039 case CPP_OPEN_SQUARE:
6040 if (cp_next_tokens_can_be_std_attribute_p (parser))
6042 cp_parser_error (parser,
6043 "two consecutive %<[%> shall "
6044 "only introduce an attribute");
6045 return error_mark_node;
6047 postfix_expression
6048 = cp_parser_postfix_open_square_expression (parser,
6049 postfix_expression,
6050 false,
6051 decltype_p);
6052 idk = CP_ID_KIND_NONE;
6053 is_member_access = false;
6054 break;
6056 case CPP_OPEN_PAREN:
6057 /* postfix-expression ( expression-list [opt] ) */
6059 bool koenig_p;
6060 bool is_builtin_constant_p;
6061 bool saved_integral_constant_expression_p = false;
6062 bool saved_non_integral_constant_expression_p = false;
6063 tsubst_flags_t complain = complain_flags (decltype_p);
6064 vec<tree, va_gc> *args;
6066 is_member_access = false;
6068 is_builtin_constant_p
6069 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6070 if (is_builtin_constant_p)
6072 /* The whole point of __builtin_constant_p is to allow
6073 non-constant expressions to appear as arguments. */
6074 saved_integral_constant_expression_p
6075 = parser->integral_constant_expression_p;
6076 saved_non_integral_constant_expression_p
6077 = parser->non_integral_constant_expression_p;
6078 parser->integral_constant_expression_p = false;
6080 args = (cp_parser_parenthesized_expression_list
6081 (parser, non_attr,
6082 /*cast_p=*/false, /*allow_expansion_p=*/true,
6083 /*non_constant_p=*/NULL,
6084 /*want_literal_zero_p=*/warn_memset_transposed_args));
6085 if (is_builtin_constant_p)
6087 parser->integral_constant_expression_p
6088 = saved_integral_constant_expression_p;
6089 parser->non_integral_constant_expression_p
6090 = saved_non_integral_constant_expression_p;
6093 if (args == NULL)
6095 postfix_expression = error_mark_node;
6096 break;
6099 /* Function calls are not permitted in
6100 constant-expressions. */
6101 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6102 && cp_parser_non_integral_constant_expression (parser,
6103 NIC_FUNC_CALL))
6105 postfix_expression = error_mark_node;
6106 release_tree_vector (args);
6107 break;
6110 koenig_p = false;
6111 if (idk == CP_ID_KIND_UNQUALIFIED
6112 || idk == CP_ID_KIND_TEMPLATE_ID)
6114 if (identifier_p (postfix_expression))
6116 if (!args->is_empty ())
6118 koenig_p = true;
6119 if (!any_type_dependent_arguments_p (args))
6120 postfix_expression
6121 = perform_koenig_lookup (postfix_expression, args,
6122 complain);
6124 else
6125 postfix_expression
6126 = unqualified_fn_lookup_error (postfix_expression);
6128 /* We do not perform argument-dependent lookup if
6129 normal lookup finds a non-function, in accordance
6130 with the expected resolution of DR 218. */
6131 else if (!args->is_empty ()
6132 && is_overloaded_fn (postfix_expression))
6134 tree fn = get_first_fn (postfix_expression);
6135 fn = STRIP_TEMPLATE (fn);
6137 /* Do not do argument dependent lookup if regular
6138 lookup finds a member function or a block-scope
6139 function declaration. [basic.lookup.argdep]/3 */
6140 if (!DECL_FUNCTION_MEMBER_P (fn)
6141 && !DECL_LOCAL_FUNCTION_P (fn))
6143 koenig_p = true;
6144 if (!any_type_dependent_arguments_p (args))
6145 postfix_expression
6146 = perform_koenig_lookup (postfix_expression, args,
6147 complain);
6152 if (warn_memset_transposed_args)
6154 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6155 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6156 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6157 && vec_safe_length (args) == 3
6158 && integer_zerop ((*args)[2])
6159 && LITERAL_ZERO_P ((*args)[2])
6160 && !(integer_zerop ((*args)[1])
6161 && LITERAL_ZERO_P ((*args)[1])))
6162 warning (OPT_Wmemset_transposed_args,
6163 "%<memset%> used with constant zero length "
6164 "parameter; this could be due to transposed "
6165 "parameters");
6167 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6168 to avoid leaking those into folder and middle-end. */
6169 unsigned int i;
6170 tree arg;
6171 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6172 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6173 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6176 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6178 tree instance = TREE_OPERAND (postfix_expression, 0);
6179 tree fn = TREE_OPERAND (postfix_expression, 1);
6181 if (processing_template_decl
6182 && (type_dependent_expression_p (instance)
6183 || (!BASELINK_P (fn)
6184 && TREE_CODE (fn) != FIELD_DECL)
6185 || type_dependent_expression_p (fn)
6186 || any_type_dependent_arguments_p (args)))
6188 postfix_expression
6189 = build_nt_call_vec (postfix_expression, args);
6190 release_tree_vector (args);
6191 break;
6194 if (BASELINK_P (fn))
6196 postfix_expression
6197 = (build_new_method_call
6198 (instance, fn, &args, NULL_TREE,
6199 (idk == CP_ID_KIND_QUALIFIED
6200 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6201 : LOOKUP_NORMAL),
6202 /*fn_p=*/NULL,
6203 complain));
6205 else
6206 postfix_expression
6207 = finish_call_expr (postfix_expression, &args,
6208 /*disallow_virtual=*/false,
6209 /*koenig_p=*/false,
6210 complain);
6212 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6213 || TREE_CODE (postfix_expression) == MEMBER_REF
6214 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6215 postfix_expression = (build_offset_ref_call_from_tree
6216 (postfix_expression, &args,
6217 complain));
6218 else if (idk == CP_ID_KIND_QUALIFIED)
6219 /* A call to a static class member, or a namespace-scope
6220 function. */
6221 postfix_expression
6222 = finish_call_expr (postfix_expression, &args,
6223 /*disallow_virtual=*/true,
6224 koenig_p,
6225 complain);
6226 else
6227 /* All other function calls. */
6228 postfix_expression
6229 = finish_call_expr (postfix_expression, &args,
6230 /*disallow_virtual=*/false,
6231 koenig_p,
6232 complain);
6234 protected_set_expr_location (postfix_expression, token->location);
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);
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);
6370 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6372 cp_lexer_consume_token (parser->lexer);
6373 stride = cp_parser_expression (parser);
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 || array_type == 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);
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);
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, /*pidk=*/NULL,
6810 cast_p);
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 < cxx14)
6907 pedwarn (input_location, 0,
6908 "%<~auto%> only available with "
6909 "-std=c++14 or -std=gnu++14");
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 ++cp_noexcept_operand;
7159 expr = cp_parser_expression (parser);
7160 --cp_noexcept_operand;
7161 --c_inhibit_evaluation_warnings;
7162 --cp_unevaluated_operand;
7164 parser->greater_than_is_operator_p
7165 = saved_greater_than_is_operator_p;
7167 parser->integral_constant_expression_p
7168 = saved_integral_constant_expression_p;
7169 parser->non_integral_constant_expression_p
7170 = saved_non_integral_constant_expression_p;
7172 parser->type_definition_forbidden_message = saved_message;
7174 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7175 return finish_noexcept_expr (expr, tf_warning_or_error);
7178 default:
7179 break;
7183 /* Look for the `:: new' and `:: delete', which also signal the
7184 beginning of a new-expression, or delete-expression,
7185 respectively. If the next token is `::', then it might be one of
7186 these. */
7187 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7189 enum rid keyword;
7191 /* See if the token after the `::' is one of the keywords in
7192 which we're interested. */
7193 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7194 /* If it's `new', we have a new-expression. */
7195 if (keyword == RID_NEW)
7196 return cp_parser_new_expression (parser);
7197 /* Similarly, for `delete'. */
7198 else if (keyword == RID_DELETE)
7199 return cp_parser_delete_expression (parser);
7202 /* Look for a unary operator. */
7203 unary_operator = cp_parser_unary_operator (token);
7204 /* The `++' and `--' operators can be handled similarly, even though
7205 they are not technically unary-operators in the grammar. */
7206 if (unary_operator == ERROR_MARK)
7208 if (token->type == CPP_PLUS_PLUS)
7209 unary_operator = PREINCREMENT_EXPR;
7210 else if (token->type == CPP_MINUS_MINUS)
7211 unary_operator = PREDECREMENT_EXPR;
7212 /* Handle the GNU address-of-label extension. */
7213 else if (cp_parser_allow_gnu_extensions_p (parser)
7214 && token->type == CPP_AND_AND)
7216 tree identifier;
7217 tree expression;
7218 location_t loc = token->location;
7220 /* Consume the '&&' token. */
7221 cp_lexer_consume_token (parser->lexer);
7222 /* Look for the identifier. */
7223 identifier = cp_parser_identifier (parser);
7224 /* Create an expression representing the address. */
7225 expression = finish_label_address_expr (identifier, loc);
7226 if (cp_parser_non_integral_constant_expression (parser,
7227 NIC_ADDR_LABEL))
7228 expression = error_mark_node;
7229 return expression;
7232 if (unary_operator != ERROR_MARK)
7234 tree cast_expression;
7235 tree expression = error_mark_node;
7236 non_integral_constant non_constant_p = NIC_NONE;
7237 location_t loc = token->location;
7238 tsubst_flags_t complain = complain_flags (decltype_p);
7240 /* Consume the operator token. */
7241 token = cp_lexer_consume_token (parser->lexer);
7242 /* Parse the cast-expression. */
7243 cast_expression
7244 = cp_parser_cast_expression (parser,
7245 unary_operator == ADDR_EXPR,
7246 /*cast_p=*/false,
7247 /*decltype*/false,
7248 pidk);
7249 /* Now, build an appropriate representation. */
7250 switch (unary_operator)
7252 case INDIRECT_REF:
7253 non_constant_p = NIC_STAR;
7254 expression = build_x_indirect_ref (loc, cast_expression,
7255 RO_UNARY_STAR,
7256 complain);
7257 break;
7259 case ADDR_EXPR:
7260 non_constant_p = NIC_ADDR;
7261 /* Fall through. */
7262 case BIT_NOT_EXPR:
7263 expression = build_x_unary_op (loc, unary_operator,
7264 cast_expression,
7265 complain);
7266 break;
7268 case PREINCREMENT_EXPR:
7269 case PREDECREMENT_EXPR:
7270 non_constant_p = unary_operator == PREINCREMENT_EXPR
7271 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7272 /* Fall through. */
7273 case UNARY_PLUS_EXPR:
7274 case NEGATE_EXPR:
7275 case TRUTH_NOT_EXPR:
7276 expression = finish_unary_op_expr (loc, unary_operator,
7277 cast_expression, complain);
7278 break;
7280 default:
7281 gcc_unreachable ();
7284 if (non_constant_p != NIC_NONE
7285 && cp_parser_non_integral_constant_expression (parser,
7286 non_constant_p))
7287 expression = error_mark_node;
7289 return expression;
7292 return cp_parser_postfix_expression (parser, address_p, cast_p,
7293 /*member_access_only_p=*/false,
7294 decltype_p,
7295 pidk);
7298 static inline tree
7299 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7300 cp_id_kind * pidk)
7302 return cp_parser_unary_expression (parser, address_p, cast_p,
7303 /*decltype*/false, pidk);
7306 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7307 unary-operator, the corresponding tree code is returned. */
7309 static enum tree_code
7310 cp_parser_unary_operator (cp_token* token)
7312 switch (token->type)
7314 case CPP_MULT:
7315 return INDIRECT_REF;
7317 case CPP_AND:
7318 return ADDR_EXPR;
7320 case CPP_PLUS:
7321 return UNARY_PLUS_EXPR;
7323 case CPP_MINUS:
7324 return NEGATE_EXPR;
7326 case CPP_NOT:
7327 return TRUTH_NOT_EXPR;
7329 case CPP_COMPL:
7330 return BIT_NOT_EXPR;
7332 default:
7333 return ERROR_MARK;
7337 /* Parse a new-expression.
7339 new-expression:
7340 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7341 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7343 Returns a representation of the expression. */
7345 static tree
7346 cp_parser_new_expression (cp_parser* parser)
7348 bool global_scope_p;
7349 vec<tree, va_gc> *placement;
7350 tree type;
7351 vec<tree, va_gc> *initializer;
7352 tree nelts = NULL_TREE;
7353 tree ret;
7355 /* Look for the optional `::' operator. */
7356 global_scope_p
7357 = (cp_parser_global_scope_opt (parser,
7358 /*current_scope_valid_p=*/false)
7359 != NULL_TREE);
7360 /* Look for the `new' operator. */
7361 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7362 /* There's no easy way to tell a new-placement from the
7363 `( type-id )' construct. */
7364 cp_parser_parse_tentatively (parser);
7365 /* Look for a new-placement. */
7366 placement = cp_parser_new_placement (parser);
7367 /* If that didn't work out, there's no new-placement. */
7368 if (!cp_parser_parse_definitely (parser))
7370 if (placement != NULL)
7371 release_tree_vector (placement);
7372 placement = NULL;
7375 /* If the next token is a `(', then we have a parenthesized
7376 type-id. */
7377 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7379 cp_token *token;
7380 const char *saved_message = parser->type_definition_forbidden_message;
7382 /* Consume the `('. */
7383 cp_lexer_consume_token (parser->lexer);
7385 /* Parse the type-id. */
7386 parser->type_definition_forbidden_message
7387 = G_("types may not be defined in a new-expression");
7388 type = cp_parser_type_id (parser);
7389 parser->type_definition_forbidden_message = saved_message;
7391 /* Look for the closing `)'. */
7392 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7393 token = cp_lexer_peek_token (parser->lexer);
7394 /* There should not be a direct-new-declarator in this production,
7395 but GCC used to allowed this, so we check and emit a sensible error
7396 message for this case. */
7397 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7399 error_at (token->location,
7400 "array bound forbidden after parenthesized type-id");
7401 inform (token->location,
7402 "try removing the parentheses around the type-id");
7403 cp_parser_direct_new_declarator (parser);
7406 /* Otherwise, there must be a new-type-id. */
7407 else
7408 type = cp_parser_new_type_id (parser, &nelts);
7410 /* If the next token is a `(' or '{', then we have a new-initializer. */
7411 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7412 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7413 initializer = cp_parser_new_initializer (parser);
7414 else
7415 initializer = NULL;
7417 /* A new-expression may not appear in an integral constant
7418 expression. */
7419 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7420 ret = error_mark_node;
7421 else
7423 /* Create a representation of the new-expression. */
7424 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7425 tf_warning_or_error);
7428 if (placement != NULL)
7429 release_tree_vector (placement);
7430 if (initializer != NULL)
7431 release_tree_vector (initializer);
7433 return ret;
7436 /* Parse a new-placement.
7438 new-placement:
7439 ( expression-list )
7441 Returns the same representation as for an expression-list. */
7443 static vec<tree, va_gc> *
7444 cp_parser_new_placement (cp_parser* parser)
7446 vec<tree, va_gc> *expression_list;
7448 /* Parse the expression-list. */
7449 expression_list = (cp_parser_parenthesized_expression_list
7450 (parser, non_attr, /*cast_p=*/false,
7451 /*allow_expansion_p=*/true,
7452 /*non_constant_p=*/NULL));
7454 return expression_list;
7457 /* Parse a new-type-id.
7459 new-type-id:
7460 type-specifier-seq new-declarator [opt]
7462 Returns the TYPE allocated. If the new-type-id indicates an array
7463 type, *NELTS is set to the number of elements in the last array
7464 bound; the TYPE will not include the last array bound. */
7466 static tree
7467 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7469 cp_decl_specifier_seq type_specifier_seq;
7470 cp_declarator *new_declarator;
7471 cp_declarator *declarator;
7472 cp_declarator *outer_declarator;
7473 const char *saved_message;
7475 /* The type-specifier sequence must not contain type definitions.
7476 (It cannot contain declarations of new types either, but if they
7477 are not definitions we will catch that because they are not
7478 complete.) */
7479 saved_message = parser->type_definition_forbidden_message;
7480 parser->type_definition_forbidden_message
7481 = G_("types may not be defined in a new-type-id");
7482 /* Parse the type-specifier-seq. */
7483 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7484 /*is_trailing_return=*/false,
7485 &type_specifier_seq);
7486 /* Restore the old message. */
7487 parser->type_definition_forbidden_message = saved_message;
7489 if (type_specifier_seq.type == error_mark_node)
7490 return error_mark_node;
7492 /* Parse the new-declarator. */
7493 new_declarator = cp_parser_new_declarator_opt (parser);
7495 /* Determine the number of elements in the last array dimension, if
7496 any. */
7497 *nelts = NULL_TREE;
7498 /* Skip down to the last array dimension. */
7499 declarator = new_declarator;
7500 outer_declarator = NULL;
7501 while (declarator && (declarator->kind == cdk_pointer
7502 || declarator->kind == cdk_ptrmem))
7504 outer_declarator = declarator;
7505 declarator = declarator->declarator;
7507 while (declarator
7508 && declarator->kind == cdk_array
7509 && declarator->declarator
7510 && declarator->declarator->kind == cdk_array)
7512 outer_declarator = declarator;
7513 declarator = declarator->declarator;
7516 if (declarator && declarator->kind == cdk_array)
7518 *nelts = declarator->u.array.bounds;
7519 if (*nelts == error_mark_node)
7520 *nelts = integer_one_node;
7522 if (outer_declarator)
7523 outer_declarator->declarator = declarator->declarator;
7524 else
7525 new_declarator = NULL;
7528 return groktypename (&type_specifier_seq, new_declarator, false);
7531 /* Parse an (optional) new-declarator.
7533 new-declarator:
7534 ptr-operator new-declarator [opt]
7535 direct-new-declarator
7537 Returns the declarator. */
7539 static cp_declarator *
7540 cp_parser_new_declarator_opt (cp_parser* parser)
7542 enum tree_code code;
7543 tree type, std_attributes = NULL_TREE;
7544 cp_cv_quals cv_quals;
7546 /* We don't know if there's a ptr-operator next, or not. */
7547 cp_parser_parse_tentatively (parser);
7548 /* Look for a ptr-operator. */
7549 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7550 /* If that worked, look for more new-declarators. */
7551 if (cp_parser_parse_definitely (parser))
7553 cp_declarator *declarator;
7555 /* Parse another optional declarator. */
7556 declarator = cp_parser_new_declarator_opt (parser);
7558 declarator = cp_parser_make_indirect_declarator
7559 (code, type, cv_quals, declarator, std_attributes);
7561 return declarator;
7564 /* If the next token is a `[', there is a direct-new-declarator. */
7565 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7566 return cp_parser_direct_new_declarator (parser);
7568 return NULL;
7571 /* Parse a direct-new-declarator.
7573 direct-new-declarator:
7574 [ expression ]
7575 direct-new-declarator [constant-expression]
7579 static cp_declarator *
7580 cp_parser_direct_new_declarator (cp_parser* parser)
7582 cp_declarator *declarator = NULL;
7584 while (true)
7586 tree expression;
7587 cp_token *token;
7589 /* Look for the opening `['. */
7590 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7592 token = cp_lexer_peek_token (parser->lexer);
7593 expression = cp_parser_expression (parser);
7594 /* The standard requires that the expression have integral
7595 type. DR 74 adds enumeration types. We believe that the
7596 real intent is that these expressions be handled like the
7597 expression in a `switch' condition, which also allows
7598 classes with a single conversion to integral or
7599 enumeration type. */
7600 if (!processing_template_decl)
7602 expression
7603 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7604 expression,
7605 /*complain=*/true);
7606 if (!expression)
7608 error_at (token->location,
7609 "expression in new-declarator must have integral "
7610 "or enumeration type");
7611 expression = error_mark_node;
7615 /* Look for the closing `]'. */
7616 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7618 /* Add this bound to the declarator. */
7619 declarator = make_array_declarator (declarator, expression);
7621 /* If the next token is not a `[', then there are no more
7622 bounds. */
7623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7624 break;
7627 return declarator;
7630 /* Parse a new-initializer.
7632 new-initializer:
7633 ( expression-list [opt] )
7634 braced-init-list
7636 Returns a representation of the expression-list. */
7638 static vec<tree, va_gc> *
7639 cp_parser_new_initializer (cp_parser* parser)
7641 vec<tree, va_gc> *expression_list;
7643 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7645 tree t;
7646 bool expr_non_constant_p;
7647 cp_lexer_set_source_position (parser->lexer);
7648 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7649 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7650 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7651 expression_list = make_tree_vector_single (t);
7653 else
7654 expression_list = (cp_parser_parenthesized_expression_list
7655 (parser, non_attr, /*cast_p=*/false,
7656 /*allow_expansion_p=*/true,
7657 /*non_constant_p=*/NULL));
7659 return expression_list;
7662 /* Parse a delete-expression.
7664 delete-expression:
7665 :: [opt] delete cast-expression
7666 :: [opt] delete [ ] cast-expression
7668 Returns a representation of the expression. */
7670 static tree
7671 cp_parser_delete_expression (cp_parser* parser)
7673 bool global_scope_p;
7674 bool array_p;
7675 tree expression;
7677 /* Look for the optional `::' operator. */
7678 global_scope_p
7679 = (cp_parser_global_scope_opt (parser,
7680 /*current_scope_valid_p=*/false)
7681 != NULL_TREE);
7682 /* Look for the `delete' keyword. */
7683 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7684 /* See if the array syntax is in use. */
7685 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7687 /* Consume the `[' token. */
7688 cp_lexer_consume_token (parser->lexer);
7689 /* Look for the `]' token. */
7690 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7691 /* Remember that this is the `[]' construct. */
7692 array_p = true;
7694 else
7695 array_p = false;
7697 /* Parse the cast-expression. */
7698 expression = cp_parser_simple_cast_expression (parser);
7700 /* A delete-expression may not appear in an integral constant
7701 expression. */
7702 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7703 return error_mark_node;
7705 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7706 tf_warning_or_error);
7709 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7710 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7711 0 otherwise. */
7713 static int
7714 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7716 cp_token *token = cp_lexer_peek_token (parser->lexer);
7717 switch (token->type)
7719 case CPP_COMMA:
7720 case CPP_SEMICOLON:
7721 case CPP_QUERY:
7722 case CPP_COLON:
7723 case CPP_CLOSE_SQUARE:
7724 case CPP_CLOSE_PAREN:
7725 case CPP_CLOSE_BRACE:
7726 case CPP_OPEN_BRACE:
7727 case CPP_DOT:
7728 case CPP_DOT_STAR:
7729 case CPP_DEREF:
7730 case CPP_DEREF_STAR:
7731 case CPP_DIV:
7732 case CPP_MOD:
7733 case CPP_LSHIFT:
7734 case CPP_RSHIFT:
7735 case CPP_LESS:
7736 case CPP_GREATER:
7737 case CPP_LESS_EQ:
7738 case CPP_GREATER_EQ:
7739 case CPP_EQ_EQ:
7740 case CPP_NOT_EQ:
7741 case CPP_EQ:
7742 case CPP_MULT_EQ:
7743 case CPP_DIV_EQ:
7744 case CPP_MOD_EQ:
7745 case CPP_PLUS_EQ:
7746 case CPP_MINUS_EQ:
7747 case CPP_RSHIFT_EQ:
7748 case CPP_LSHIFT_EQ:
7749 case CPP_AND_EQ:
7750 case CPP_XOR_EQ:
7751 case CPP_OR_EQ:
7752 case CPP_XOR:
7753 case CPP_OR:
7754 case CPP_OR_OR:
7755 case CPP_EOF:
7756 case CPP_ELLIPSIS:
7757 return 0;
7759 case CPP_OPEN_PAREN:
7760 /* In ((type ()) () the last () isn't a valid cast-expression,
7761 so the whole must be parsed as postfix-expression. */
7762 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7763 != CPP_CLOSE_PAREN;
7765 case CPP_OPEN_SQUARE:
7766 /* '[' may start a primary-expression in obj-c++ and in C++11,
7767 as a lambda-expression, eg, '(void)[]{}'. */
7768 if (cxx_dialect >= cxx11)
7769 return -1;
7770 return c_dialect_objc ();
7772 case CPP_PLUS_PLUS:
7773 case CPP_MINUS_MINUS:
7774 /* '++' and '--' may or may not start a cast-expression:
7776 struct T { void operator++(int); };
7777 void f() { (T())++; }
7781 int a;
7782 (int)++a; */
7783 return -1;
7785 default:
7786 return 1;
7790 /* Parse a cast-expression.
7792 cast-expression:
7793 unary-expression
7794 ( type-id ) cast-expression
7796 ADDRESS_P is true iff the unary-expression is appearing as the
7797 operand of the `&' operator. CAST_P is true if this expression is
7798 the target of a cast.
7800 Returns a representation of the expression. */
7802 static tree
7803 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7804 bool decltype_p, cp_id_kind * pidk)
7806 /* If it's a `(', then we might be looking at a cast. */
7807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7809 tree type = NULL_TREE;
7810 tree expr = NULL_TREE;
7811 int cast_expression = 0;
7812 const char *saved_message;
7814 /* There's no way to know yet whether or not this is a cast.
7815 For example, `(int (3))' is a unary-expression, while `(int)
7816 3' is a cast. So, we resort to parsing tentatively. */
7817 cp_parser_parse_tentatively (parser);
7818 /* Types may not be defined in a cast. */
7819 saved_message = parser->type_definition_forbidden_message;
7820 parser->type_definition_forbidden_message
7821 = G_("types may not be defined in casts");
7822 /* Consume the `('. */
7823 cp_lexer_consume_token (parser->lexer);
7824 /* A very tricky bit is that `(struct S) { 3 }' is a
7825 compound-literal (which we permit in C++ as an extension).
7826 But, that construct is not a cast-expression -- it is a
7827 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7828 is legal; if the compound-literal were a cast-expression,
7829 you'd need an extra set of parentheses.) But, if we parse
7830 the type-id, and it happens to be a class-specifier, then we
7831 will commit to the parse at that point, because we cannot
7832 undo the action that is done when creating a new class. So,
7833 then we cannot back up and do a postfix-expression.
7835 Another tricky case is the following (c++/29234):
7837 struct S { void operator () (); };
7839 void foo ()
7841 ( S()() );
7844 As a type-id we parse the parenthesized S()() as a function
7845 returning a function, groktypename complains and we cannot
7846 back up in this case either.
7848 Therefore, we scan ahead to the closing `)', and check to see
7849 if the tokens after the `)' can start a cast-expression. Otherwise
7850 we are dealing with an unary-expression, a postfix-expression
7851 or something else.
7853 Yet another tricky case, in C++11, is the following (c++/54891):
7855 (void)[]{};
7857 The issue is that usually, besides the case of lambda-expressions,
7858 the parenthesized type-id cannot be followed by '[', and, eg, we
7859 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7860 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7861 we don't commit, we try a cast-expression, then an unary-expression.
7863 Save tokens so that we can put them back. */
7864 cp_lexer_save_tokens (parser->lexer);
7866 /* We may be looking at a cast-expression. */
7867 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7868 /*consume_paren=*/true))
7869 cast_expression
7870 = cp_parser_tokens_start_cast_expression (parser);
7872 /* Roll back the tokens we skipped. */
7873 cp_lexer_rollback_tokens (parser->lexer);
7874 /* If we aren't looking at a cast-expression, simulate an error so
7875 that the call to cp_parser_error_occurred below returns true. */
7876 if (!cast_expression)
7877 cp_parser_simulate_error (parser);
7878 else
7880 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7881 parser->in_type_id_in_expr_p = true;
7882 /* Look for the type-id. */
7883 type = cp_parser_type_id (parser);
7884 /* Look for the closing `)'. */
7885 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7886 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7889 /* Restore the saved message. */
7890 parser->type_definition_forbidden_message = saved_message;
7892 /* At this point this can only be either a cast or a
7893 parenthesized ctor such as `(T ())' that looks like a cast to
7894 function returning T. */
7895 if (!cp_parser_error_occurred (parser))
7897 /* Only commit if the cast-expression doesn't start with
7898 '++', '--', or '[' in C++11. */
7899 if (cast_expression > 0)
7900 cp_parser_commit_to_topmost_tentative_parse (parser);
7902 expr = cp_parser_cast_expression (parser,
7903 /*address_p=*/false,
7904 /*cast_p=*/true,
7905 /*decltype_p=*/false,
7906 pidk);
7908 if (cp_parser_parse_definitely (parser))
7910 /* Warn about old-style casts, if so requested. */
7911 if (warn_old_style_cast
7912 && !in_system_header_at (input_location)
7913 && !VOID_TYPE_P (type)
7914 && current_lang_name != lang_name_c)
7915 warning (OPT_Wold_style_cast, "use of old-style cast");
7917 /* Only type conversions to integral or enumeration types
7918 can be used in constant-expressions. */
7919 if (!cast_valid_in_integral_constant_expression_p (type)
7920 && cp_parser_non_integral_constant_expression (parser,
7921 NIC_CAST))
7922 return error_mark_node;
7924 /* Perform the cast. */
7925 expr = build_c_cast (input_location, type, expr);
7926 return expr;
7929 else
7930 cp_parser_abort_tentative_parse (parser);
7933 /* If we get here, then it's not a cast, so it must be a
7934 unary-expression. */
7935 return cp_parser_unary_expression (parser, address_p, cast_p,
7936 decltype_p, pidk);
7939 /* Parse a binary expression of the general form:
7941 pm-expression:
7942 cast-expression
7943 pm-expression .* cast-expression
7944 pm-expression ->* cast-expression
7946 multiplicative-expression:
7947 pm-expression
7948 multiplicative-expression * pm-expression
7949 multiplicative-expression / pm-expression
7950 multiplicative-expression % pm-expression
7952 additive-expression:
7953 multiplicative-expression
7954 additive-expression + multiplicative-expression
7955 additive-expression - multiplicative-expression
7957 shift-expression:
7958 additive-expression
7959 shift-expression << additive-expression
7960 shift-expression >> additive-expression
7962 relational-expression:
7963 shift-expression
7964 relational-expression < shift-expression
7965 relational-expression > shift-expression
7966 relational-expression <= shift-expression
7967 relational-expression >= shift-expression
7969 GNU Extension:
7971 relational-expression:
7972 relational-expression <? shift-expression
7973 relational-expression >? shift-expression
7975 equality-expression:
7976 relational-expression
7977 equality-expression == relational-expression
7978 equality-expression != relational-expression
7980 and-expression:
7981 equality-expression
7982 and-expression & equality-expression
7984 exclusive-or-expression:
7985 and-expression
7986 exclusive-or-expression ^ and-expression
7988 inclusive-or-expression:
7989 exclusive-or-expression
7990 inclusive-or-expression | exclusive-or-expression
7992 logical-and-expression:
7993 inclusive-or-expression
7994 logical-and-expression && inclusive-or-expression
7996 logical-or-expression:
7997 logical-and-expression
7998 logical-or-expression || logical-and-expression
8000 All these are implemented with a single function like:
8002 binary-expression:
8003 simple-cast-expression
8004 binary-expression <token> binary-expression
8006 CAST_P is true if this expression is the target of a cast.
8008 The binops_by_token map is used to get the tree codes for each <token> type.
8009 binary-expressions are associated according to a precedence table. */
8011 #define TOKEN_PRECEDENCE(token) \
8012 (((token->type == CPP_GREATER \
8013 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8014 && !parser->greater_than_is_operator_p) \
8015 ? PREC_NOT_OPERATOR \
8016 : binops_by_token[token->type].prec)
8018 static tree
8019 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8020 bool no_toplevel_fold_p,
8021 bool decltype_p,
8022 enum cp_parser_prec prec,
8023 cp_id_kind * pidk)
8025 cp_parser_expression_stack stack;
8026 cp_parser_expression_stack_entry *sp = &stack[0];
8027 cp_parser_expression_stack_entry current;
8028 tree rhs;
8029 cp_token *token;
8030 enum tree_code rhs_type;
8031 enum cp_parser_prec new_prec, lookahead_prec;
8032 tree overload;
8034 /* Parse the first expression. */
8035 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8036 ? TRUTH_NOT_EXPR : ERROR_MARK);
8037 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8038 cast_p, decltype_p, pidk);
8039 current.prec = prec;
8041 if (cp_parser_error_occurred (parser))
8042 return error_mark_node;
8044 for (;;)
8046 /* Get an operator token. */
8047 token = cp_lexer_peek_token (parser->lexer);
8049 if (warn_cxx0x_compat
8050 && token->type == CPP_RSHIFT
8051 && !parser->greater_than_is_operator_p)
8053 if (warning_at (token->location, OPT_Wc__0x_compat,
8054 "%<>>%> operator is treated"
8055 " as two right angle brackets in C++11"))
8056 inform (token->location,
8057 "suggest parentheses around %<>>%> expression");
8060 new_prec = TOKEN_PRECEDENCE (token);
8062 /* Popping an entry off the stack means we completed a subexpression:
8063 - either we found a token which is not an operator (`>' where it is not
8064 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8065 will happen repeatedly;
8066 - or, we found an operator which has lower priority. This is the case
8067 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8068 parsing `3 * 4'. */
8069 if (new_prec <= current.prec)
8071 if (sp == stack)
8072 break;
8073 else
8074 goto pop;
8077 get_rhs:
8078 current.tree_type = binops_by_token[token->type].tree_type;
8079 current.loc = token->location;
8081 /* We used the operator token. */
8082 cp_lexer_consume_token (parser->lexer);
8084 /* For "false && x" or "true || x", x will never be executed;
8085 disable warnings while evaluating it. */
8086 if (current.tree_type == TRUTH_ANDIF_EXPR)
8087 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8088 else if (current.tree_type == TRUTH_ORIF_EXPR)
8089 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8091 /* Extract another operand. It may be the RHS of this expression
8092 or the LHS of a new, higher priority expression. */
8093 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8094 ? TRUTH_NOT_EXPR : ERROR_MARK);
8095 rhs = cp_parser_simple_cast_expression (parser);
8097 /* Get another operator token. Look up its precedence to avoid
8098 building a useless (immediately popped) stack entry for common
8099 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8100 token = cp_lexer_peek_token (parser->lexer);
8101 lookahead_prec = TOKEN_PRECEDENCE (token);
8102 if (lookahead_prec > new_prec)
8104 /* ... and prepare to parse the RHS of the new, higher priority
8105 expression. Since precedence levels on the stack are
8106 monotonically increasing, we do not have to care about
8107 stack overflows. */
8108 *sp = current;
8109 ++sp;
8110 current.lhs = rhs;
8111 current.lhs_type = rhs_type;
8112 current.prec = new_prec;
8113 new_prec = lookahead_prec;
8114 goto get_rhs;
8116 pop:
8117 lookahead_prec = new_prec;
8118 /* If the stack is not empty, we have parsed into LHS the right side
8119 (`4' in the example above) of an expression we had suspended.
8120 We can use the information on the stack to recover the LHS (`3')
8121 from the stack together with the tree code (`MULT_EXPR'), and
8122 the precedence of the higher level subexpression
8123 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8124 which will be used to actually build the additive expression. */
8125 rhs = current.lhs;
8126 rhs_type = current.lhs_type;
8127 --sp;
8128 current = *sp;
8131 /* Undo the disabling of warnings done above. */
8132 if (current.tree_type == TRUTH_ANDIF_EXPR)
8133 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8134 else if (current.tree_type == TRUTH_ORIF_EXPR)
8135 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8137 if (warn_logical_not_paren
8138 && current.lhs_type == TRUTH_NOT_EXPR)
8139 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8141 overload = NULL;
8142 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8143 ERROR_MARK for everything that is not a binary expression.
8144 This makes warn_about_parentheses miss some warnings that
8145 involve unary operators. For unary expressions we should
8146 pass the correct tree_code unless the unary expression was
8147 surrounded by parentheses.
8149 if (no_toplevel_fold_p
8150 && lookahead_prec <= current.prec
8151 && sp == stack)
8152 current.lhs = build2 (current.tree_type,
8153 TREE_CODE_CLASS (current.tree_type)
8154 == tcc_comparison
8155 ? boolean_type_node : TREE_TYPE (current.lhs),
8156 current.lhs, rhs);
8157 else
8158 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8159 current.lhs, current.lhs_type,
8160 rhs, rhs_type, &overload,
8161 complain_flags (decltype_p));
8162 current.lhs_type = current.tree_type;
8163 if (EXPR_P (current.lhs))
8164 SET_EXPR_LOCATION (current.lhs, current.loc);
8166 /* If the binary operator required the use of an overloaded operator,
8167 then this expression cannot be an integral constant-expression.
8168 An overloaded operator can be used even if both operands are
8169 otherwise permissible in an integral constant-expression if at
8170 least one of the operands is of enumeration type. */
8172 if (overload
8173 && cp_parser_non_integral_constant_expression (parser,
8174 NIC_OVERLOADED))
8175 return error_mark_node;
8178 return current.lhs;
8181 static tree
8182 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8183 bool no_toplevel_fold_p,
8184 enum cp_parser_prec prec,
8185 cp_id_kind * pidk)
8187 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8188 /*decltype*/false, prec, pidk);
8191 /* Parse the `? expression : assignment-expression' part of a
8192 conditional-expression. The LOGICAL_OR_EXPR is the
8193 logical-or-expression that started the conditional-expression.
8194 Returns a representation of the entire conditional-expression.
8196 This routine is used by cp_parser_assignment_expression.
8198 ? expression : assignment-expression
8200 GNU Extensions:
8202 ? : assignment-expression */
8204 static tree
8205 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8207 tree expr;
8208 tree assignment_expr;
8209 struct cp_token *token;
8210 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8212 /* Consume the `?' token. */
8213 cp_lexer_consume_token (parser->lexer);
8214 token = cp_lexer_peek_token (parser->lexer);
8215 if (cp_parser_allow_gnu_extensions_p (parser)
8216 && token->type == CPP_COLON)
8218 pedwarn (token->location, OPT_Wpedantic,
8219 "ISO C++ does not allow ?: with omitted middle operand");
8220 /* Implicit true clause. */
8221 expr = NULL_TREE;
8222 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8223 warn_for_omitted_condop (token->location, logical_or_expr);
8225 else
8227 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8228 parser->colon_corrects_to_scope_p = false;
8229 /* Parse the expression. */
8230 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8231 expr = cp_parser_expression (parser);
8232 c_inhibit_evaluation_warnings +=
8233 ((logical_or_expr == truthvalue_true_node)
8234 - (logical_or_expr == truthvalue_false_node));
8235 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8238 /* The next token should be a `:'. */
8239 cp_parser_require (parser, CPP_COLON, RT_COLON);
8240 /* Parse the assignment-expression. */
8241 assignment_expr = cp_parser_assignment_expression (parser);
8242 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8244 /* Build the conditional-expression. */
8245 return build_x_conditional_expr (loc, logical_or_expr,
8246 expr,
8247 assignment_expr,
8248 tf_warning_or_error);
8251 /* Parse an assignment-expression.
8253 assignment-expression:
8254 conditional-expression
8255 logical-or-expression assignment-operator assignment_expression
8256 throw-expression
8258 CAST_P is true if this expression is the target of a cast.
8259 DECLTYPE_P is true if this expression is the operand of decltype.
8261 Returns a representation for the expression. */
8263 static tree
8264 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8265 bool cast_p, bool decltype_p)
8267 tree expr;
8269 /* If the next token is the `throw' keyword, then we're looking at
8270 a throw-expression. */
8271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8272 expr = cp_parser_throw_expression (parser);
8273 /* Otherwise, it must be that we are looking at a
8274 logical-or-expression. */
8275 else
8277 /* Parse the binary expressions (logical-or-expression). */
8278 expr = cp_parser_binary_expression (parser, cast_p, false,
8279 decltype_p,
8280 PREC_NOT_OPERATOR, pidk);
8281 /* If the next token is a `?' then we're actually looking at a
8282 conditional-expression. */
8283 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8284 return cp_parser_question_colon_clause (parser, expr);
8285 else
8287 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8289 /* If it's an assignment-operator, we're using the second
8290 production. */
8291 enum tree_code assignment_operator
8292 = cp_parser_assignment_operator_opt (parser);
8293 if (assignment_operator != ERROR_MARK)
8295 bool non_constant_p;
8296 location_t saved_input_location;
8298 /* Parse the right-hand side of the assignment. */
8299 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8301 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8302 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8304 /* An assignment may not appear in a
8305 constant-expression. */
8306 if (cp_parser_non_integral_constant_expression (parser,
8307 NIC_ASSIGNMENT))
8308 return error_mark_node;
8309 /* Build the assignment expression. Its default
8310 location is the location of the '=' token. */
8311 saved_input_location = input_location;
8312 input_location = loc;
8313 expr = build_x_modify_expr (loc, expr,
8314 assignment_operator,
8315 rhs,
8316 complain_flags (decltype_p));
8317 input_location = saved_input_location;
8322 return expr;
8325 /* Parse an (optional) assignment-operator.
8327 assignment-operator: one of
8328 = *= /= %= += -= >>= <<= &= ^= |=
8330 GNU Extension:
8332 assignment-operator: one of
8333 <?= >?=
8335 If the next token is an assignment operator, the corresponding tree
8336 code is returned, and the token is consumed. For example, for
8337 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8338 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8339 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8340 operator, ERROR_MARK is returned. */
8342 static enum tree_code
8343 cp_parser_assignment_operator_opt (cp_parser* parser)
8345 enum tree_code op;
8346 cp_token *token;
8348 /* Peek at the next token. */
8349 token = cp_lexer_peek_token (parser->lexer);
8351 switch (token->type)
8353 case CPP_EQ:
8354 op = NOP_EXPR;
8355 break;
8357 case CPP_MULT_EQ:
8358 op = MULT_EXPR;
8359 break;
8361 case CPP_DIV_EQ:
8362 op = TRUNC_DIV_EXPR;
8363 break;
8365 case CPP_MOD_EQ:
8366 op = TRUNC_MOD_EXPR;
8367 break;
8369 case CPP_PLUS_EQ:
8370 op = PLUS_EXPR;
8371 break;
8373 case CPP_MINUS_EQ:
8374 op = MINUS_EXPR;
8375 break;
8377 case CPP_RSHIFT_EQ:
8378 op = RSHIFT_EXPR;
8379 break;
8381 case CPP_LSHIFT_EQ:
8382 op = LSHIFT_EXPR;
8383 break;
8385 case CPP_AND_EQ:
8386 op = BIT_AND_EXPR;
8387 break;
8389 case CPP_XOR_EQ:
8390 op = BIT_XOR_EXPR;
8391 break;
8393 case CPP_OR_EQ:
8394 op = BIT_IOR_EXPR;
8395 break;
8397 default:
8398 /* Nothing else is an assignment operator. */
8399 op = ERROR_MARK;
8402 /* If it was an assignment operator, consume it. */
8403 if (op != ERROR_MARK)
8404 cp_lexer_consume_token (parser->lexer);
8406 return op;
8409 /* Parse an expression.
8411 expression:
8412 assignment-expression
8413 expression , assignment-expression
8415 CAST_P is true if this expression is the target of a cast.
8416 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8417 except possibly parenthesized or on the RHS of a comma (N3276).
8419 Returns a representation of the expression. */
8421 static tree
8422 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8423 bool cast_p, bool decltype_p)
8425 tree expression = NULL_TREE;
8426 location_t loc = UNKNOWN_LOCATION;
8428 while (true)
8430 tree assignment_expression;
8432 /* Parse the next assignment-expression. */
8433 assignment_expression
8434 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8436 /* We don't create a temporary for a call that is the immediate operand
8437 of decltype or on the RHS of a comma. But when we see a comma, we
8438 need to create a temporary for a call on the LHS. */
8439 if (decltype_p && !processing_template_decl
8440 && TREE_CODE (assignment_expression) == CALL_EXPR
8441 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8442 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8443 assignment_expression
8444 = build_cplus_new (TREE_TYPE (assignment_expression),
8445 assignment_expression, tf_warning_or_error);
8447 /* If this is the first assignment-expression, we can just
8448 save it away. */
8449 if (!expression)
8450 expression = assignment_expression;
8451 else
8452 expression = build_x_compound_expr (loc, expression,
8453 assignment_expression,
8454 complain_flags (decltype_p));
8455 /* If the next token is not a comma, then we are done with the
8456 expression. */
8457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8458 break;
8459 /* Consume the `,'. */
8460 loc = cp_lexer_peek_token (parser->lexer)->location;
8461 cp_lexer_consume_token (parser->lexer);
8462 /* A comma operator cannot appear in a constant-expression. */
8463 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8464 expression = error_mark_node;
8467 return expression;
8470 /* Parse a constant-expression.
8472 constant-expression:
8473 conditional-expression
8475 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8476 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8477 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8478 is false, NON_CONSTANT_P should be NULL. */
8480 static tree
8481 cp_parser_constant_expression (cp_parser* parser,
8482 bool allow_non_constant_p,
8483 bool *non_constant_p)
8485 bool saved_integral_constant_expression_p;
8486 bool saved_allow_non_integral_constant_expression_p;
8487 bool saved_non_integral_constant_expression_p;
8488 tree expression;
8490 /* It might seem that we could simply parse the
8491 conditional-expression, and then check to see if it were
8492 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8493 one that the compiler can figure out is constant, possibly after
8494 doing some simplifications or optimizations. The standard has a
8495 precise definition of constant-expression, and we must honor
8496 that, even though it is somewhat more restrictive.
8498 For example:
8500 int i[(2, 3)];
8502 is not a legal declaration, because `(2, 3)' is not a
8503 constant-expression. The `,' operator is forbidden in a
8504 constant-expression. However, GCC's constant-folding machinery
8505 will fold this operation to an INTEGER_CST for `3'. */
8507 /* Save the old settings. */
8508 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8509 saved_allow_non_integral_constant_expression_p
8510 = parser->allow_non_integral_constant_expression_p;
8511 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8512 /* We are now parsing a constant-expression. */
8513 parser->integral_constant_expression_p = true;
8514 parser->allow_non_integral_constant_expression_p
8515 = (allow_non_constant_p || cxx_dialect >= cxx11);
8516 parser->non_integral_constant_expression_p = false;
8517 /* Although the grammar says "conditional-expression", we parse an
8518 "assignment-expression", which also permits "throw-expression"
8519 and the use of assignment operators. In the case that
8520 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8521 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8522 actually essential that we look for an assignment-expression.
8523 For example, cp_parser_initializer_clauses uses this function to
8524 determine whether a particular assignment-expression is in fact
8525 constant. */
8526 expression = cp_parser_assignment_expression (parser);
8527 /* Restore the old settings. */
8528 parser->integral_constant_expression_p
8529 = saved_integral_constant_expression_p;
8530 parser->allow_non_integral_constant_expression_p
8531 = saved_allow_non_integral_constant_expression_p;
8532 if (cxx_dialect >= cxx11)
8534 /* Require an rvalue constant expression here; that's what our
8535 callers expect. Reference constant expressions are handled
8536 separately in e.g. cp_parser_template_argument. */
8537 bool is_const = potential_rvalue_constant_expression (expression);
8538 parser->non_integral_constant_expression_p = !is_const;
8539 if (!is_const && !allow_non_constant_p)
8540 require_potential_rvalue_constant_expression (expression);
8542 if (allow_non_constant_p)
8543 *non_constant_p = parser->non_integral_constant_expression_p;
8544 parser->non_integral_constant_expression_p
8545 = saved_non_integral_constant_expression_p;
8547 return expression;
8550 /* Parse __builtin_offsetof.
8552 offsetof-expression:
8553 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8555 offsetof-member-designator:
8556 id-expression
8557 | offsetof-member-designator "." id-expression
8558 | offsetof-member-designator "[" expression "]"
8559 | offsetof-member-designator "->" id-expression */
8561 static tree
8562 cp_parser_builtin_offsetof (cp_parser *parser)
8564 int save_ice_p, save_non_ice_p;
8565 tree type, expr;
8566 cp_id_kind dummy;
8567 cp_token *token;
8569 /* We're about to accept non-integral-constant things, but will
8570 definitely yield an integral constant expression. Save and
8571 restore these values around our local parsing. */
8572 save_ice_p = parser->integral_constant_expression_p;
8573 save_non_ice_p = parser->non_integral_constant_expression_p;
8575 /* Consume the "__builtin_offsetof" token. */
8576 cp_lexer_consume_token (parser->lexer);
8577 /* Consume the opening `('. */
8578 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8579 /* Parse the type-id. */
8580 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8581 type = cp_parser_type_id (parser);
8582 /* Look for the `,'. */
8583 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8584 token = cp_lexer_peek_token (parser->lexer);
8586 /* Build the (type *)null that begins the traditional offsetof macro. */
8587 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8588 tf_warning_or_error);
8590 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8591 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8592 true, &dummy, token->location);
8593 while (true)
8595 token = cp_lexer_peek_token (parser->lexer);
8596 switch (token->type)
8598 case CPP_OPEN_SQUARE:
8599 /* offsetof-member-designator "[" expression "]" */
8600 expr = cp_parser_postfix_open_square_expression (parser, expr,
8601 true, false);
8602 break;
8604 case CPP_DEREF:
8605 /* offsetof-member-designator "->" identifier */
8606 expr = grok_array_decl (token->location, expr,
8607 integer_zero_node, false);
8608 /* FALLTHRU */
8610 case CPP_DOT:
8611 /* offsetof-member-designator "." identifier */
8612 cp_lexer_consume_token (parser->lexer);
8613 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8614 expr, true, &dummy,
8615 token->location);
8616 break;
8618 case CPP_CLOSE_PAREN:
8619 /* Consume the ")" token. */
8620 cp_lexer_consume_token (parser->lexer);
8621 goto success;
8623 default:
8624 /* Error. We know the following require will fail, but
8625 that gives the proper error message. */
8626 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8627 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8628 expr = error_mark_node;
8629 goto failure;
8633 success:
8634 /* If we're processing a template, we can't finish the semantics yet.
8635 Otherwise we can fold the entire expression now. */
8636 if (processing_template_decl)
8638 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8639 SET_EXPR_LOCATION (expr, loc);
8641 else
8642 expr = finish_offsetof (expr, loc);
8644 failure:
8645 parser->integral_constant_expression_p = save_ice_p;
8646 parser->non_integral_constant_expression_p = save_non_ice_p;
8648 return expr;
8651 /* Parse a trait expression.
8653 Returns a representation of the expression, the underlying type
8654 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8656 static tree
8657 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8659 cp_trait_kind kind;
8660 tree type1, type2 = NULL_TREE;
8661 bool binary = false;
8662 bool variadic = false;
8664 switch (keyword)
8666 case RID_HAS_NOTHROW_ASSIGN:
8667 kind = CPTK_HAS_NOTHROW_ASSIGN;
8668 break;
8669 case RID_HAS_NOTHROW_CONSTRUCTOR:
8670 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8671 break;
8672 case RID_HAS_NOTHROW_COPY:
8673 kind = CPTK_HAS_NOTHROW_COPY;
8674 break;
8675 case RID_HAS_TRIVIAL_ASSIGN:
8676 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8677 break;
8678 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8679 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8680 break;
8681 case RID_HAS_TRIVIAL_COPY:
8682 kind = CPTK_HAS_TRIVIAL_COPY;
8683 break;
8684 case RID_HAS_TRIVIAL_DESTRUCTOR:
8685 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8686 break;
8687 case RID_HAS_VIRTUAL_DESTRUCTOR:
8688 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8689 break;
8690 case RID_IS_ABSTRACT:
8691 kind = CPTK_IS_ABSTRACT;
8692 break;
8693 case RID_IS_BASE_OF:
8694 kind = CPTK_IS_BASE_OF;
8695 binary = true;
8696 break;
8697 case RID_IS_CLASS:
8698 kind = CPTK_IS_CLASS;
8699 break;
8700 case RID_IS_EMPTY:
8701 kind = CPTK_IS_EMPTY;
8702 break;
8703 case RID_IS_ENUM:
8704 kind = CPTK_IS_ENUM;
8705 break;
8706 case RID_IS_FINAL:
8707 kind = CPTK_IS_FINAL;
8708 break;
8709 case RID_IS_LITERAL_TYPE:
8710 kind = CPTK_IS_LITERAL_TYPE;
8711 break;
8712 case RID_IS_POD:
8713 kind = CPTK_IS_POD;
8714 break;
8715 case RID_IS_POLYMORPHIC:
8716 kind = CPTK_IS_POLYMORPHIC;
8717 break;
8718 case RID_IS_STD_LAYOUT:
8719 kind = CPTK_IS_STD_LAYOUT;
8720 break;
8721 case RID_IS_TRIVIAL:
8722 kind = CPTK_IS_TRIVIAL;
8723 break;
8724 case RID_IS_TRIVIALLY_ASSIGNABLE:
8725 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8726 binary = true;
8727 break;
8728 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8729 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8730 variadic = true;
8731 break;
8732 case RID_IS_TRIVIALLY_COPYABLE:
8733 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8734 break;
8735 case RID_IS_UNION:
8736 kind = CPTK_IS_UNION;
8737 break;
8738 case RID_UNDERLYING_TYPE:
8739 kind = CPTK_UNDERLYING_TYPE;
8740 break;
8741 case RID_BASES:
8742 kind = CPTK_BASES;
8743 break;
8744 case RID_DIRECT_BASES:
8745 kind = CPTK_DIRECT_BASES;
8746 break;
8747 default:
8748 gcc_unreachable ();
8751 /* Consume the token. */
8752 cp_lexer_consume_token (parser->lexer);
8754 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8756 type1 = cp_parser_type_id (parser);
8758 if (type1 == error_mark_node)
8759 return error_mark_node;
8761 if (binary)
8763 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8765 type2 = cp_parser_type_id (parser);
8767 if (type2 == error_mark_node)
8768 return error_mark_node;
8770 else if (variadic)
8772 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8774 cp_lexer_consume_token (parser->lexer);
8775 tree elt = cp_parser_type_id (parser);
8776 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8778 cp_lexer_consume_token (parser->lexer);
8779 elt = make_pack_expansion (elt);
8781 if (elt == error_mark_node)
8782 return error_mark_node;
8783 type2 = tree_cons (NULL_TREE, elt, type2);
8787 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8789 /* Complete the trait expression, which may mean either processing
8790 the trait expr now or saving it for template instantiation. */
8791 switch(kind)
8793 case CPTK_UNDERLYING_TYPE:
8794 return finish_underlying_type (type1);
8795 case CPTK_BASES:
8796 return finish_bases (type1, false);
8797 case CPTK_DIRECT_BASES:
8798 return finish_bases (type1, true);
8799 default:
8800 return finish_trait_expr (kind, type1, type2);
8804 /* Lambdas that appear in variable initializer or default argument scope
8805 get that in their mangling, so we need to record it. We might as well
8806 use the count for function and namespace scopes as well. */
8807 static GTY(()) tree lambda_scope;
8808 static GTY(()) int lambda_count;
8809 typedef struct GTY(()) tree_int
8811 tree t;
8812 int i;
8813 } tree_int;
8814 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8816 static void
8817 start_lambda_scope (tree decl)
8819 tree_int ti;
8820 gcc_assert (decl);
8821 /* Once we're inside a function, we ignore other scopes and just push
8822 the function again so that popping works properly. */
8823 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8824 decl = current_function_decl;
8825 ti.t = lambda_scope;
8826 ti.i = lambda_count;
8827 vec_safe_push (lambda_scope_stack, ti);
8828 if (lambda_scope != decl)
8830 /* Don't reset the count if we're still in the same function. */
8831 lambda_scope = decl;
8832 lambda_count = 0;
8836 static void
8837 record_lambda_scope (tree lambda)
8839 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8840 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8843 static void
8844 finish_lambda_scope (void)
8846 tree_int *p = &lambda_scope_stack->last ();
8847 if (lambda_scope != p->t)
8849 lambda_scope = p->t;
8850 lambda_count = p->i;
8852 lambda_scope_stack->pop ();
8855 /* Parse a lambda expression.
8857 lambda-expression:
8858 lambda-introducer lambda-declarator [opt] compound-statement
8860 Returns a representation of the expression. */
8862 static tree
8863 cp_parser_lambda_expression (cp_parser* parser)
8865 tree lambda_expr = build_lambda_expr ();
8866 tree type;
8867 bool ok = true;
8868 cp_token *token = cp_lexer_peek_token (parser->lexer);
8870 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8872 if (cp_unevaluated_operand)
8874 if (!token->error_reported)
8876 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8877 "lambda-expression in unevaluated context");
8878 token->error_reported = true;
8880 ok = false;
8883 /* We may be in the middle of deferred access check. Disable
8884 it now. */
8885 push_deferring_access_checks (dk_no_deferred);
8887 cp_parser_lambda_introducer (parser, lambda_expr);
8889 type = begin_lambda_type (lambda_expr);
8890 if (type == error_mark_node)
8891 return error_mark_node;
8893 record_lambda_scope (lambda_expr);
8895 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8896 determine_visibility (TYPE_NAME (type));
8898 /* Now that we've started the type, add the capture fields for any
8899 explicit captures. */
8900 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8903 /* Inside the class, surrounding template-parameter-lists do not apply. */
8904 unsigned int saved_num_template_parameter_lists
8905 = parser->num_template_parameter_lists;
8906 unsigned char in_statement = parser->in_statement;
8907 bool in_switch_statement_p = parser->in_switch_statement_p;
8908 bool fully_implicit_function_template_p
8909 = parser->fully_implicit_function_template_p;
8910 tree implicit_template_parms = parser->implicit_template_parms;
8911 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8912 bool auto_is_implicit_function_template_parm_p
8913 = parser->auto_is_implicit_function_template_parm_p;
8915 parser->num_template_parameter_lists = 0;
8916 parser->in_statement = 0;
8917 parser->in_switch_statement_p = false;
8918 parser->fully_implicit_function_template_p = false;
8919 parser->implicit_template_parms = 0;
8920 parser->implicit_template_scope = 0;
8921 parser->auto_is_implicit_function_template_parm_p = false;
8923 /* By virtue of defining a local class, a lambda expression has access to
8924 the private variables of enclosing classes. */
8926 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8928 if (ok)
8929 cp_parser_lambda_body (parser, lambda_expr);
8930 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8932 if (cp_parser_skip_to_closing_brace (parser))
8933 cp_lexer_consume_token (parser->lexer);
8936 /* The capture list was built up in reverse order; fix that now. */
8937 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8938 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8940 if (ok)
8941 maybe_add_lambda_conv_op (type);
8943 type = finish_struct (type, /*attributes=*/NULL_TREE);
8945 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8946 parser->in_statement = in_statement;
8947 parser->in_switch_statement_p = in_switch_statement_p;
8948 parser->fully_implicit_function_template_p
8949 = fully_implicit_function_template_p;
8950 parser->implicit_template_parms = implicit_template_parms;
8951 parser->implicit_template_scope = implicit_template_scope;
8952 parser->auto_is_implicit_function_template_parm_p
8953 = auto_is_implicit_function_template_parm_p;
8956 pop_deferring_access_checks ();
8958 /* This field is only used during parsing of the lambda. */
8959 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8961 /* This lambda shouldn't have any proxies left at this point. */
8962 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8963 /* And now that we're done, push proxies for an enclosing lambda. */
8964 insert_pending_capture_proxies ();
8966 if (ok)
8967 return build_lambda_object (lambda_expr);
8968 else
8969 return error_mark_node;
8972 /* Parse the beginning of a lambda expression.
8974 lambda-introducer:
8975 [ lambda-capture [opt] ]
8977 LAMBDA_EXPR is the current representation of the lambda expression. */
8979 static void
8980 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8982 /* Need commas after the first capture. */
8983 bool first = true;
8985 /* Eat the leading `['. */
8986 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8988 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8989 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8990 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8991 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8992 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8993 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8995 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8997 cp_lexer_consume_token (parser->lexer);
8998 first = false;
9001 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9003 cp_token* capture_token;
9004 tree capture_id;
9005 tree capture_init_expr;
9006 cp_id_kind idk = CP_ID_KIND_NONE;
9007 bool explicit_init_p = false;
9009 enum capture_kind_type
9011 BY_COPY,
9012 BY_REFERENCE
9014 enum capture_kind_type capture_kind = BY_COPY;
9016 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9018 error ("expected end of capture-list");
9019 return;
9022 if (first)
9023 first = false;
9024 else
9025 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9027 /* Possibly capture `this'. */
9028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9030 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9031 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9032 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9033 "with by-copy capture default");
9034 cp_lexer_consume_token (parser->lexer);
9035 add_capture (lambda_expr,
9036 /*id=*/this_identifier,
9037 /*initializer=*/finish_this_expr(),
9038 /*by_reference_p=*/false,
9039 explicit_init_p);
9040 continue;
9043 /* Remember whether we want to capture as a reference or not. */
9044 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9046 capture_kind = BY_REFERENCE;
9047 cp_lexer_consume_token (parser->lexer);
9050 /* Get the identifier. */
9051 capture_token = cp_lexer_peek_token (parser->lexer);
9052 capture_id = cp_parser_identifier (parser);
9054 if (capture_id == error_mark_node)
9055 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9056 delimiters, but I modified this to stop on unnested ']' as well. It
9057 was already changed to stop on unnested '}', so the
9058 "closing_parenthesis" name is no more misleading with my change. */
9060 cp_parser_skip_to_closing_parenthesis (parser,
9061 /*recovering=*/true,
9062 /*or_comma=*/true,
9063 /*consume_paren=*/true);
9064 break;
9067 /* Find the initializer for this capture. */
9068 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9069 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9070 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9072 bool direct, non_constant;
9073 /* An explicit initializer exists. */
9074 if (cxx_dialect < cxx14)
9075 pedwarn (input_location, 0,
9076 "lambda capture initializers "
9077 "only available with -std=c++14 or -std=gnu++14");
9078 capture_init_expr = cp_parser_initializer (parser, &direct,
9079 &non_constant);
9080 explicit_init_p = true;
9081 if (capture_init_expr == NULL_TREE)
9083 error ("empty initializer for lambda init-capture");
9084 capture_init_expr = error_mark_node;
9087 else
9089 const char* error_msg;
9091 /* Turn the identifier into an id-expression. */
9092 capture_init_expr
9093 = cp_parser_lookup_name_simple (parser, capture_id,
9094 capture_token->location);
9096 if (capture_init_expr == error_mark_node)
9098 unqualified_name_lookup_error (capture_id);
9099 continue;
9101 else if (DECL_P (capture_init_expr)
9102 && (!VAR_P (capture_init_expr)
9103 && TREE_CODE (capture_init_expr) != PARM_DECL))
9105 error_at (capture_token->location,
9106 "capture of non-variable %qD ",
9107 capture_init_expr);
9108 inform (0, "%q+#D declared here", capture_init_expr);
9109 continue;
9111 if (VAR_P (capture_init_expr)
9112 && decl_storage_duration (capture_init_expr) != dk_auto)
9114 if (pedwarn (capture_token->location, 0, "capture of variable "
9115 "%qD with non-automatic storage duration",
9116 capture_init_expr))
9117 inform (0, "%q+#D declared here", capture_init_expr);
9118 continue;
9121 capture_init_expr
9122 = finish_id_expression
9123 (capture_id,
9124 capture_init_expr,
9125 parser->scope,
9126 &idk,
9127 /*integral_constant_expression_p=*/false,
9128 /*allow_non_integral_constant_expression_p=*/false,
9129 /*non_integral_constant_expression_p=*/NULL,
9130 /*template_p=*/false,
9131 /*done=*/true,
9132 /*address_p=*/false,
9133 /*template_arg_p=*/false,
9134 &error_msg,
9135 capture_token->location);
9137 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9139 cp_lexer_consume_token (parser->lexer);
9140 capture_init_expr = make_pack_expansion (capture_init_expr);
9142 else
9143 check_for_bare_parameter_packs (capture_init_expr);
9146 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9147 && !explicit_init_p)
9149 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9150 && capture_kind == BY_COPY)
9151 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9152 "of %qD redundant with by-copy capture default",
9153 capture_id);
9154 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9155 && capture_kind == BY_REFERENCE)
9156 pedwarn (capture_token->location, 0, "explicit by-reference "
9157 "capture of %qD redundant with by-reference capture "
9158 "default", capture_id);
9161 add_capture (lambda_expr,
9162 capture_id,
9163 capture_init_expr,
9164 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9165 explicit_init_p);
9168 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9171 /* Parse the (optional) middle of a lambda expression.
9173 lambda-declarator:
9174 < template-parameter-list [opt] >
9175 ( parameter-declaration-clause [opt] )
9176 attribute-specifier [opt]
9177 mutable [opt]
9178 exception-specification [opt]
9179 lambda-return-type-clause [opt]
9181 LAMBDA_EXPR is the current representation of the lambda expression. */
9183 static bool
9184 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9186 /* 5.1.1.4 of the standard says:
9187 If a lambda-expression does not include a lambda-declarator, it is as if
9188 the lambda-declarator were ().
9189 This means an empty parameter list, no attributes, and no exception
9190 specification. */
9191 tree param_list = void_list_node;
9192 tree attributes = NULL_TREE;
9193 tree exception_spec = NULL_TREE;
9194 tree template_param_list = NULL_TREE;
9196 /* The template-parameter-list is optional, but must begin with
9197 an opening angle if present. */
9198 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9200 if (cxx_dialect < cxx14)
9201 pedwarn (parser->lexer->next_token->location, 0,
9202 "lambda templates are only available with "
9203 "-std=c++14 or -std=gnu++14");
9205 cp_lexer_consume_token (parser->lexer);
9207 template_param_list = cp_parser_template_parameter_list (parser);
9209 cp_parser_skip_to_end_of_template_parameter_list (parser);
9211 /* We just processed one more parameter list. */
9212 ++parser->num_template_parameter_lists;
9215 /* The parameter-declaration-clause is optional (unless
9216 template-parameter-list was given), but must begin with an
9217 opening parenthesis if present. */
9218 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9220 cp_lexer_consume_token (parser->lexer);
9222 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9224 /* Parse parameters. */
9225 param_list = cp_parser_parameter_declaration_clause (parser);
9227 /* Default arguments shall not be specified in the
9228 parameter-declaration-clause of a lambda-declarator. */
9229 for (tree t = param_list; t; t = TREE_CHAIN (t))
9230 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9231 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9232 "default argument specified for lambda parameter");
9234 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9236 attributes = cp_parser_attributes_opt (parser);
9238 /* Parse optional `mutable' keyword. */
9239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9241 cp_lexer_consume_token (parser->lexer);
9242 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9245 /* Parse optional exception specification. */
9246 exception_spec = cp_parser_exception_specification_opt (parser);
9248 /* Parse optional trailing return type. */
9249 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9251 cp_lexer_consume_token (parser->lexer);
9252 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9253 = cp_parser_trailing_type_id (parser);
9256 /* The function parameters must be in scope all the way until after the
9257 trailing-return-type in case of decltype. */
9258 pop_bindings_and_leave_scope ();
9260 else if (template_param_list != NULL_TREE) // generate diagnostic
9261 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9263 /* Create the function call operator.
9265 Messing with declarators like this is no uglier than building up the
9266 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9267 other code. */
9269 cp_decl_specifier_seq return_type_specs;
9270 cp_declarator* declarator;
9271 tree fco;
9272 int quals;
9273 void *p;
9275 clear_decl_specs (&return_type_specs);
9276 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9277 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9278 else
9279 /* Maybe we will deduce the return type later. */
9280 return_type_specs.type = make_auto ();
9282 p = obstack_alloc (&declarator_obstack, 0);
9284 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9285 sfk_none);
9287 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9288 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9289 declarator = make_call_declarator (declarator, param_list, quals,
9290 VIRT_SPEC_UNSPECIFIED,
9291 REF_QUAL_NONE,
9292 exception_spec,
9293 /*late_return_type=*/NULL_TREE);
9294 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9296 fco = grokmethod (&return_type_specs,
9297 declarator,
9298 attributes);
9299 if (fco != error_mark_node)
9301 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9302 DECL_ARTIFICIAL (fco) = 1;
9303 /* Give the object parameter a different name. */
9304 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9305 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9306 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9308 if (template_param_list)
9310 fco = finish_member_template_decl (fco);
9311 finish_template_decl (template_param_list);
9312 --parser->num_template_parameter_lists;
9314 else if (parser->fully_implicit_function_template_p)
9315 fco = finish_fully_implicit_template (parser, fco);
9317 finish_member_declaration (fco);
9319 obstack_free (&declarator_obstack, p);
9321 return (fco != error_mark_node);
9325 /* Parse the body of a lambda expression, which is simply
9327 compound-statement
9329 but which requires special handling.
9330 LAMBDA_EXPR is the current representation of the lambda expression. */
9332 static void
9333 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9335 bool nested = (current_function_decl != NULL_TREE);
9336 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9337 if (nested)
9338 push_function_context ();
9339 else
9340 /* Still increment function_depth so that we don't GC in the
9341 middle of an expression. */
9342 ++function_depth;
9343 /* Clear this in case we're in the middle of a default argument. */
9344 parser->local_variables_forbidden_p = false;
9346 /* Finish the function call operator
9347 - class_specifier
9348 + late_parsing_for_member
9349 + function_definition_after_declarator
9350 + ctor_initializer_opt_and_function_body */
9352 tree fco = lambda_function (lambda_expr);
9353 tree body;
9354 bool done = false;
9355 tree compound_stmt;
9356 tree cap;
9358 /* Let the front end know that we are going to be defining this
9359 function. */
9360 start_preparsed_function (fco,
9361 NULL_TREE,
9362 SF_PRE_PARSED | SF_INCLASS_INLINE);
9364 start_lambda_scope (fco);
9365 body = begin_function_body ();
9367 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9368 goto out;
9370 /* Push the proxies for any explicit captures. */
9371 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9372 cap = TREE_CHAIN (cap))
9373 build_capture_proxy (TREE_PURPOSE (cap));
9375 compound_stmt = begin_compound_stmt (0);
9377 /* 5.1.1.4 of the standard says:
9378 If a lambda-expression does not include a trailing-return-type, it
9379 is as if the trailing-return-type denotes the following type:
9380 * if the compound-statement is of the form
9381 { return attribute-specifier [opt] expression ; }
9382 the type of the returned expression after lvalue-to-rvalue
9383 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9384 (_conv.array_ 4.2), and function-to-pointer conversion
9385 (_conv.func_ 4.3);
9386 * otherwise, void. */
9388 /* In a lambda that has neither a lambda-return-type-clause
9389 nor a deducible form, errors should be reported for return statements
9390 in the body. Since we used void as the placeholder return type, parsing
9391 the body as usual will give such desired behavior. */
9392 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9393 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9394 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9396 tree expr = NULL_TREE;
9397 cp_id_kind idk = CP_ID_KIND_NONE;
9399 /* Parse tentatively in case there's more after the initial return
9400 statement. */
9401 cp_parser_parse_tentatively (parser);
9403 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9405 expr = cp_parser_expression (parser, &idk);
9407 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9408 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9410 if (cp_parser_parse_definitely (parser))
9412 if (!processing_template_decl)
9413 apply_deduced_return_type (fco, lambda_return_type (expr));
9415 /* Will get error here if type not deduced yet. */
9416 finish_return_stmt (expr);
9418 done = true;
9422 if (!done)
9424 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9425 cp_parser_label_declaration (parser);
9426 cp_parser_statement_seq_opt (parser, NULL_TREE);
9427 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9430 finish_compound_stmt (compound_stmt);
9432 out:
9433 finish_function_body (body);
9434 finish_lambda_scope ();
9436 /* Finish the function and generate code for it if necessary. */
9437 tree fn = finish_function (/*inline*/2);
9439 /* Only expand if the call op is not a template. */
9440 if (!DECL_TEMPLATE_INFO (fco))
9441 expand_or_defer_fn (fn);
9444 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9445 if (nested)
9446 pop_function_context();
9447 else
9448 --function_depth;
9451 /* Statements [gram.stmt.stmt] */
9453 /* Parse a statement.
9455 statement:
9456 labeled-statement
9457 expression-statement
9458 compound-statement
9459 selection-statement
9460 iteration-statement
9461 jump-statement
9462 declaration-statement
9463 try-block
9465 C++11:
9467 statement:
9468 labeled-statement
9469 attribute-specifier-seq (opt) expression-statement
9470 attribute-specifier-seq (opt) compound-statement
9471 attribute-specifier-seq (opt) selection-statement
9472 attribute-specifier-seq (opt) iteration-statement
9473 attribute-specifier-seq (opt) jump-statement
9474 declaration-statement
9475 attribute-specifier-seq (opt) try-block
9477 TM Extension:
9479 statement:
9480 atomic-statement
9482 IN_COMPOUND is true when the statement is nested inside a
9483 cp_parser_compound_statement; this matters for certain pragmas.
9485 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9486 is a (possibly labeled) if statement which is not enclosed in braces
9487 and has an else clause. This is used to implement -Wparentheses. */
9489 static void
9490 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9491 bool in_compound, bool *if_p)
9493 tree statement, std_attrs = NULL_TREE;
9494 cp_token *token;
9495 location_t statement_location, attrs_location;
9497 restart:
9498 if (if_p != NULL)
9499 *if_p = false;
9500 /* There is no statement yet. */
9501 statement = NULL_TREE;
9503 cp_lexer_save_tokens (parser->lexer);
9504 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9505 if (c_dialect_objc ())
9506 /* In obj-c++, seeing '[[' might be the either the beginning of
9507 c++11 attributes, or a nested objc-message-expression. So
9508 let's parse the c++11 attributes tentatively. */
9509 cp_parser_parse_tentatively (parser);
9510 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9511 if (c_dialect_objc ())
9513 if (!cp_parser_parse_definitely (parser))
9514 std_attrs = NULL_TREE;
9517 /* Peek at the next token. */
9518 token = cp_lexer_peek_token (parser->lexer);
9519 /* Remember the location of the first token in the statement. */
9520 statement_location = token->location;
9521 /* If this is a keyword, then that will often determine what kind of
9522 statement we have. */
9523 if (token->type == CPP_KEYWORD)
9525 enum rid keyword = token->keyword;
9527 switch (keyword)
9529 case RID_CASE:
9530 case RID_DEFAULT:
9531 /* Looks like a labeled-statement with a case label.
9532 Parse the label, and then use tail recursion to parse
9533 the statement. */
9534 cp_parser_label_for_labeled_statement (parser, std_attrs);
9535 goto restart;
9537 case RID_IF:
9538 case RID_SWITCH:
9539 statement = cp_parser_selection_statement (parser, if_p);
9540 break;
9542 case RID_WHILE:
9543 case RID_DO:
9544 case RID_FOR:
9545 statement = cp_parser_iteration_statement (parser, false);
9546 break;
9548 case RID_CILK_FOR:
9549 if (!flag_cilkplus)
9551 error_at (cp_lexer_peek_token (parser->lexer)->location,
9552 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9553 cp_lexer_consume_token (parser->lexer);
9554 statement = error_mark_node;
9556 else
9557 statement = cp_parser_cilk_for (parser, integer_zero_node);
9558 break;
9560 case RID_BREAK:
9561 case RID_CONTINUE:
9562 case RID_RETURN:
9563 case RID_GOTO:
9564 statement = cp_parser_jump_statement (parser);
9565 break;
9567 case RID_CILK_SYNC:
9568 cp_lexer_consume_token (parser->lexer);
9569 if (flag_cilkplus)
9571 tree sync_expr = build_cilk_sync ();
9572 SET_EXPR_LOCATION (sync_expr,
9573 token->location);
9574 statement = finish_expr_stmt (sync_expr);
9576 else
9578 error_at (token->location, "-fcilkplus must be enabled to use"
9579 " %<_Cilk_sync%>");
9580 statement = error_mark_node;
9582 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9583 break;
9585 /* Objective-C++ exception-handling constructs. */
9586 case RID_AT_TRY:
9587 case RID_AT_CATCH:
9588 case RID_AT_FINALLY:
9589 case RID_AT_SYNCHRONIZED:
9590 case RID_AT_THROW:
9591 statement = cp_parser_objc_statement (parser);
9592 break;
9594 case RID_TRY:
9595 statement = cp_parser_try_block (parser);
9596 break;
9598 case RID_NAMESPACE:
9599 /* This must be a namespace alias definition. */
9600 cp_parser_declaration_statement (parser);
9601 return;
9603 case RID_TRANSACTION_ATOMIC:
9604 case RID_TRANSACTION_RELAXED:
9605 statement = cp_parser_transaction (parser, keyword);
9606 break;
9607 case RID_TRANSACTION_CANCEL:
9608 statement = cp_parser_transaction_cancel (parser);
9609 break;
9611 default:
9612 /* It might be a keyword like `int' that can start a
9613 declaration-statement. */
9614 break;
9617 else if (token->type == CPP_NAME)
9619 /* If the next token is a `:', then we are looking at a
9620 labeled-statement. */
9621 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9622 if (token->type == CPP_COLON)
9624 /* Looks like a labeled-statement with an ordinary label.
9625 Parse the label, and then use tail recursion to parse
9626 the statement. */
9628 cp_parser_label_for_labeled_statement (parser, std_attrs);
9629 goto restart;
9632 /* Anything that starts with a `{' must be a compound-statement. */
9633 else if (token->type == CPP_OPEN_BRACE)
9634 statement = cp_parser_compound_statement (parser, NULL, false, false);
9635 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9636 a statement all its own. */
9637 else if (token->type == CPP_PRAGMA)
9639 /* Only certain OpenMP pragmas are attached to statements, and thus
9640 are considered statements themselves. All others are not. In
9641 the context of a compound, accept the pragma as a "statement" and
9642 return so that we can check for a close brace. Otherwise we
9643 require a real statement and must go back and read one. */
9644 if (in_compound)
9645 cp_parser_pragma (parser, pragma_compound);
9646 else if (!cp_parser_pragma (parser, pragma_stmt))
9647 goto restart;
9648 return;
9650 else if (token->type == CPP_EOF)
9652 cp_parser_error (parser, "expected statement");
9653 return;
9656 /* Everything else must be a declaration-statement or an
9657 expression-statement. Try for the declaration-statement
9658 first, unless we are looking at a `;', in which case we know that
9659 we have an expression-statement. */
9660 if (!statement)
9662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9664 if (std_attrs != NULL_TREE)
9666 /* Attributes should be parsed as part of the the
9667 declaration, so let's un-parse them. */
9668 cp_lexer_rollback_tokens (parser->lexer);
9669 std_attrs = NULL_TREE;
9672 cp_parser_parse_tentatively (parser);
9673 /* Try to parse the declaration-statement. */
9674 cp_parser_declaration_statement (parser);
9675 /* If that worked, we're done. */
9676 if (cp_parser_parse_definitely (parser))
9677 return;
9679 /* Look for an expression-statement instead. */
9680 statement = cp_parser_expression_statement (parser, in_statement_expr);
9683 /* Set the line number for the statement. */
9684 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9685 SET_EXPR_LOCATION (statement, statement_location);
9687 /* Note that for now, we don't do anything with c++11 statements
9688 parsed at this level. */
9689 if (std_attrs != NULL_TREE)
9690 warning_at (attrs_location,
9691 OPT_Wattributes,
9692 "attributes at the beginning of statement are ignored");
9695 /* Parse the label for a labeled-statement, i.e.
9697 identifier :
9698 case constant-expression :
9699 default :
9701 GNU Extension:
9702 case constant-expression ... constant-expression : statement
9704 When a label is parsed without errors, the label is added to the
9705 parse tree by the finish_* functions, so this function doesn't
9706 have to return the label. */
9708 static void
9709 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9711 cp_token *token;
9712 tree label = NULL_TREE;
9713 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9715 /* The next token should be an identifier. */
9716 token = cp_lexer_peek_token (parser->lexer);
9717 if (token->type != CPP_NAME
9718 && token->type != CPP_KEYWORD)
9720 cp_parser_error (parser, "expected labeled-statement");
9721 return;
9724 parser->colon_corrects_to_scope_p = false;
9725 switch (token->keyword)
9727 case RID_CASE:
9729 tree expr, expr_hi;
9730 cp_token *ellipsis;
9732 /* Consume the `case' token. */
9733 cp_lexer_consume_token (parser->lexer);
9734 /* Parse the constant-expression. */
9735 expr = cp_parser_constant_expression (parser);
9737 ellipsis = cp_lexer_peek_token (parser->lexer);
9738 if (ellipsis->type == CPP_ELLIPSIS)
9740 /* Consume the `...' token. */
9741 cp_lexer_consume_token (parser->lexer);
9742 expr_hi =
9743 cp_parser_constant_expression (parser);
9745 /* We don't need to emit warnings here, as the common code
9746 will do this for us. */
9748 else
9749 expr_hi = NULL_TREE;
9751 if (parser->in_switch_statement_p)
9752 finish_case_label (token->location, expr, expr_hi);
9753 else
9754 error_at (token->location,
9755 "case label %qE not within a switch statement",
9756 expr);
9758 break;
9760 case RID_DEFAULT:
9761 /* Consume the `default' token. */
9762 cp_lexer_consume_token (parser->lexer);
9764 if (parser->in_switch_statement_p)
9765 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9766 else
9767 error_at (token->location, "case label not within a switch statement");
9768 break;
9770 default:
9771 /* Anything else must be an ordinary label. */
9772 label = finish_label_stmt (cp_parser_identifier (parser));
9773 break;
9776 /* Require the `:' token. */
9777 cp_parser_require (parser, CPP_COLON, RT_COLON);
9779 /* An ordinary label may optionally be followed by attributes.
9780 However, this is only permitted if the attributes are then
9781 followed by a semicolon. This is because, for backward
9782 compatibility, when parsing
9783 lab: __attribute__ ((unused)) int i;
9784 we want the attribute to attach to "i", not "lab". */
9785 if (label != NULL_TREE
9786 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9788 tree attrs;
9789 cp_parser_parse_tentatively (parser);
9790 attrs = cp_parser_gnu_attributes_opt (parser);
9791 if (attrs == NULL_TREE
9792 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9793 cp_parser_abort_tentative_parse (parser);
9794 else if (!cp_parser_parse_definitely (parser))
9796 else
9797 attributes = chainon (attributes, attrs);
9800 if (attributes != NULL_TREE)
9801 cplus_decl_attributes (&label, attributes, 0);
9803 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9806 /* Parse an expression-statement.
9808 expression-statement:
9809 expression [opt] ;
9811 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9812 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9813 indicates whether this expression-statement is part of an
9814 expression statement. */
9816 static tree
9817 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9819 tree statement = NULL_TREE;
9820 cp_token *token = cp_lexer_peek_token (parser->lexer);
9822 /* If the next token is a ';', then there is no expression
9823 statement. */
9824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9826 statement = cp_parser_expression (parser);
9827 if (statement == error_mark_node
9828 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9830 cp_parser_skip_to_end_of_block_or_statement (parser);
9831 return error_mark_node;
9835 /* Give a helpful message for "A<T>::type t;" and the like. */
9836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9837 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9839 if (TREE_CODE (statement) == SCOPE_REF)
9840 error_at (token->location, "need %<typename%> before %qE because "
9841 "%qT is a dependent scope",
9842 statement, TREE_OPERAND (statement, 0));
9843 else if (is_overloaded_fn (statement)
9844 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9846 /* A::A a; */
9847 tree fn = get_first_fn (statement);
9848 error_at (token->location,
9849 "%<%T::%D%> names the constructor, not the type",
9850 DECL_CONTEXT (fn), DECL_NAME (fn));
9854 /* Consume the final `;'. */
9855 cp_parser_consume_semicolon_at_end_of_statement (parser);
9857 if (in_statement_expr
9858 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9859 /* This is the final expression statement of a statement
9860 expression. */
9861 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9862 else if (statement)
9863 statement = finish_expr_stmt (statement);
9865 return statement;
9868 /* Parse a compound-statement.
9870 compound-statement:
9871 { statement-seq [opt] }
9873 GNU extension:
9875 compound-statement:
9876 { label-declaration-seq [opt] statement-seq [opt] }
9878 label-declaration-seq:
9879 label-declaration
9880 label-declaration-seq label-declaration
9882 Returns a tree representing the statement. */
9884 static tree
9885 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9886 bool in_try, bool function_body)
9888 tree compound_stmt;
9890 /* Consume the `{'. */
9891 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9892 return error_mark_node;
9893 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9894 && !function_body && cxx_dialect < cxx14)
9895 pedwarn (input_location, OPT_Wpedantic,
9896 "compound-statement in constexpr function");
9897 /* Begin the compound-statement. */
9898 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9899 /* If the next keyword is `__label__' we have a label declaration. */
9900 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9901 cp_parser_label_declaration (parser);
9902 /* Parse an (optional) statement-seq. */
9903 cp_parser_statement_seq_opt (parser, in_statement_expr);
9904 /* Finish the compound-statement. */
9905 finish_compound_stmt (compound_stmt);
9906 /* Consume the `}'. */
9907 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9909 return compound_stmt;
9912 /* Parse an (optional) statement-seq.
9914 statement-seq:
9915 statement
9916 statement-seq [opt] statement */
9918 static void
9919 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9921 /* Scan statements until there aren't any more. */
9922 while (true)
9924 cp_token *token = cp_lexer_peek_token (parser->lexer);
9926 /* If we are looking at a `}', then we have run out of
9927 statements; the same is true if we have reached the end
9928 of file, or have stumbled upon a stray '@end'. */
9929 if (token->type == CPP_CLOSE_BRACE
9930 || token->type == CPP_EOF
9931 || token->type == CPP_PRAGMA_EOL
9932 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9933 break;
9935 /* If we are in a compound statement and find 'else' then
9936 something went wrong. */
9937 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9939 if (parser->in_statement & IN_IF_STMT)
9940 break;
9941 else
9943 token = cp_lexer_consume_token (parser->lexer);
9944 error_at (token->location, "%<else%> without a previous %<if%>");
9948 /* Parse the statement. */
9949 cp_parser_statement (parser, in_statement_expr, true, NULL);
9953 /* Parse a selection-statement.
9955 selection-statement:
9956 if ( condition ) statement
9957 if ( condition ) statement else statement
9958 switch ( condition ) statement
9960 Returns the new IF_STMT or SWITCH_STMT.
9962 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9963 is a (possibly labeled) if statement which is not enclosed in
9964 braces and has an else clause. This is used to implement
9965 -Wparentheses. */
9967 static tree
9968 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9970 cp_token *token;
9971 enum rid keyword;
9973 if (if_p != NULL)
9974 *if_p = false;
9976 /* Peek at the next token. */
9977 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9979 /* See what kind of keyword it is. */
9980 keyword = token->keyword;
9981 switch (keyword)
9983 case RID_IF:
9984 case RID_SWITCH:
9986 tree statement;
9987 tree condition;
9989 /* Look for the `('. */
9990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9992 cp_parser_skip_to_end_of_statement (parser);
9993 return error_mark_node;
9996 /* Begin the selection-statement. */
9997 if (keyword == RID_IF)
9998 statement = begin_if_stmt ();
9999 else
10000 statement = begin_switch_stmt ();
10002 /* Parse the condition. */
10003 condition = cp_parser_condition (parser);
10004 /* Look for the `)'. */
10005 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10006 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10007 /*consume_paren=*/true);
10009 if (keyword == RID_IF)
10011 bool nested_if;
10012 unsigned char in_statement;
10014 /* Add the condition. */
10015 finish_if_stmt_cond (condition, statement);
10017 /* Parse the then-clause. */
10018 in_statement = parser->in_statement;
10019 parser->in_statement |= IN_IF_STMT;
10020 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10022 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10023 add_stmt (build_empty_stmt (loc));
10024 cp_lexer_consume_token (parser->lexer);
10025 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10026 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10027 "empty body in an %<if%> statement");
10028 nested_if = false;
10030 else
10031 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10032 parser->in_statement = in_statement;
10034 finish_then_clause (statement);
10036 /* If the next token is `else', parse the else-clause. */
10037 if (cp_lexer_next_token_is_keyword (parser->lexer,
10038 RID_ELSE))
10040 /* Consume the `else' keyword. */
10041 cp_lexer_consume_token (parser->lexer);
10042 begin_else_clause (statement);
10043 /* Parse the else-clause. */
10044 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10046 location_t loc;
10047 loc = cp_lexer_peek_token (parser->lexer)->location;
10048 warning_at (loc,
10049 OPT_Wempty_body, "suggest braces around "
10050 "empty body in an %<else%> statement");
10051 add_stmt (build_empty_stmt (loc));
10052 cp_lexer_consume_token (parser->lexer);
10054 else
10055 cp_parser_implicitly_scoped_statement (parser, NULL);
10057 finish_else_clause (statement);
10059 /* If we are currently parsing a then-clause, then
10060 IF_P will not be NULL. We set it to true to
10061 indicate that this if statement has an else clause.
10062 This may trigger the Wparentheses warning below
10063 when we get back up to the parent if statement. */
10064 if (if_p != NULL)
10065 *if_p = true;
10067 else
10069 /* This if statement does not have an else clause. If
10070 NESTED_IF is true, then the then-clause is an if
10071 statement which does have an else clause. We warn
10072 about the potential ambiguity. */
10073 if (nested_if)
10074 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10075 "suggest explicit braces to avoid ambiguous"
10076 " %<else%>");
10079 /* Now we're all done with the if-statement. */
10080 finish_if_stmt (statement);
10082 else
10084 bool in_switch_statement_p;
10085 unsigned char in_statement;
10087 /* Add the condition. */
10088 finish_switch_cond (condition, statement);
10090 /* Parse the body of the switch-statement. */
10091 in_switch_statement_p = parser->in_switch_statement_p;
10092 in_statement = parser->in_statement;
10093 parser->in_switch_statement_p = true;
10094 parser->in_statement |= IN_SWITCH_STMT;
10095 cp_parser_implicitly_scoped_statement (parser, NULL);
10096 parser->in_switch_statement_p = in_switch_statement_p;
10097 parser->in_statement = in_statement;
10099 /* Now we're all done with the switch-statement. */
10100 finish_switch_stmt (statement);
10103 return statement;
10105 break;
10107 default:
10108 cp_parser_error (parser, "expected selection-statement");
10109 return error_mark_node;
10113 /* Parse a condition.
10115 condition:
10116 expression
10117 type-specifier-seq declarator = initializer-clause
10118 type-specifier-seq declarator braced-init-list
10120 GNU Extension:
10122 condition:
10123 type-specifier-seq declarator asm-specification [opt]
10124 attributes [opt] = assignment-expression
10126 Returns the expression that should be tested. */
10128 static tree
10129 cp_parser_condition (cp_parser* parser)
10131 cp_decl_specifier_seq type_specifiers;
10132 const char *saved_message;
10133 int declares_class_or_enum;
10135 /* Try the declaration first. */
10136 cp_parser_parse_tentatively (parser);
10137 /* New types are not allowed in the type-specifier-seq for a
10138 condition. */
10139 saved_message = parser->type_definition_forbidden_message;
10140 parser->type_definition_forbidden_message
10141 = G_("types may not be defined in conditions");
10142 /* Parse the type-specifier-seq. */
10143 cp_parser_decl_specifier_seq (parser,
10144 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10145 &type_specifiers,
10146 &declares_class_or_enum);
10147 /* Restore the saved message. */
10148 parser->type_definition_forbidden_message = saved_message;
10149 /* If all is well, we might be looking at a declaration. */
10150 if (!cp_parser_error_occurred (parser))
10152 tree decl;
10153 tree asm_specification;
10154 tree attributes;
10155 cp_declarator *declarator;
10156 tree initializer = NULL_TREE;
10158 /* Parse the declarator. */
10159 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10160 /*ctor_dtor_or_conv_p=*/NULL,
10161 /*parenthesized_p=*/NULL,
10162 /*member_p=*/false,
10163 /*friend_p=*/false);
10164 /* Parse the attributes. */
10165 attributes = cp_parser_attributes_opt (parser);
10166 /* Parse the asm-specification. */
10167 asm_specification = cp_parser_asm_specification_opt (parser);
10168 /* If the next token is not an `=' or '{', then we might still be
10169 looking at an expression. For example:
10171 if (A(a).x)
10173 looks like a decl-specifier-seq and a declarator -- but then
10174 there is no `=', so this is an expression. */
10175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10176 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10177 cp_parser_simulate_error (parser);
10179 /* If we did see an `=' or '{', then we are looking at a declaration
10180 for sure. */
10181 if (cp_parser_parse_definitely (parser))
10183 tree pushed_scope;
10184 bool non_constant_p;
10185 bool flags = LOOKUP_ONLYCONVERTING;
10187 /* Create the declaration. */
10188 decl = start_decl (declarator, &type_specifiers,
10189 /*initialized_p=*/true,
10190 attributes, /*prefix_attributes=*/NULL_TREE,
10191 &pushed_scope);
10193 /* Parse the initializer. */
10194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10196 initializer = cp_parser_braced_list (parser, &non_constant_p);
10197 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10198 flags = 0;
10200 else
10202 /* Consume the `='. */
10203 cp_parser_require (parser, CPP_EQ, RT_EQ);
10204 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10206 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10207 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10209 /* Process the initializer. */
10210 cp_finish_decl (decl,
10211 initializer, !non_constant_p,
10212 asm_specification,
10213 flags);
10215 if (pushed_scope)
10216 pop_scope (pushed_scope);
10218 return convert_from_reference (decl);
10221 /* If we didn't even get past the declarator successfully, we are
10222 definitely not looking at a declaration. */
10223 else
10224 cp_parser_abort_tentative_parse (parser);
10226 /* Otherwise, we are looking at an expression. */
10227 return cp_parser_expression (parser);
10230 /* Parses a for-statement or range-for-statement until the closing ')',
10231 not included. */
10233 static tree
10234 cp_parser_for (cp_parser *parser, bool ivdep)
10236 tree init, scope, decl;
10237 bool is_range_for;
10239 /* Begin the for-statement. */
10240 scope = begin_for_scope (&init);
10242 /* Parse the initialization. */
10243 is_range_for = cp_parser_for_init_statement (parser, &decl);
10245 if (is_range_for)
10246 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10247 else
10248 return cp_parser_c_for (parser, scope, init, ivdep);
10251 static tree
10252 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10254 /* Normal for loop */
10255 tree condition = NULL_TREE;
10256 tree expression = NULL_TREE;
10257 tree stmt;
10259 stmt = begin_for_stmt (scope, init);
10260 /* The for-init-statement has already been parsed in
10261 cp_parser_for_init_statement, so no work is needed here. */
10262 finish_for_init_stmt (stmt);
10264 /* If there's a condition, process it. */
10265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10266 condition = cp_parser_condition (parser);
10267 else if (ivdep)
10269 cp_parser_error (parser, "missing loop condition in loop with "
10270 "%<GCC ivdep%> pragma");
10271 condition = error_mark_node;
10273 finish_for_cond (condition, stmt, ivdep);
10274 /* Look for the `;'. */
10275 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10277 /* If there's an expression, process it. */
10278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10279 expression = cp_parser_expression (parser);
10280 finish_for_expr (expression, stmt);
10282 return stmt;
10285 /* Tries to parse a range-based for-statement:
10287 range-based-for:
10288 decl-specifier-seq declarator : expression
10290 The decl-specifier-seq declarator and the `:' are already parsed by
10291 cp_parser_for_init_statement. If processing_template_decl it returns a
10292 newly created RANGE_FOR_STMT; if not, it is converted to a
10293 regular FOR_STMT. */
10295 static tree
10296 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10297 bool ivdep)
10299 tree stmt, range_expr;
10301 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10303 bool expr_non_constant_p;
10304 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10306 else
10307 range_expr = cp_parser_expression (parser);
10309 /* If in template, STMT is converted to a normal for-statement
10310 at instantiation. If not, it is done just ahead. */
10311 if (processing_template_decl)
10313 if (check_for_bare_parameter_packs (range_expr))
10314 range_expr = error_mark_node;
10315 stmt = begin_range_for_stmt (scope, init);
10316 if (ivdep)
10317 RANGE_FOR_IVDEP (stmt) = 1;
10318 finish_range_for_decl (stmt, range_decl, range_expr);
10319 if (!type_dependent_expression_p (range_expr)
10320 /* do_auto_deduction doesn't mess with template init-lists. */
10321 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10322 do_range_for_auto_deduction (range_decl, range_expr);
10324 else
10326 stmt = begin_for_stmt (scope, init);
10327 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10329 return stmt;
10332 /* Subroutine of cp_convert_range_for: given the initializer expression,
10333 builds up the range temporary. */
10335 static tree
10336 build_range_temp (tree range_expr)
10338 tree range_type, range_temp;
10340 /* Find out the type deduced by the declaration
10341 `auto &&__range = range_expr'. */
10342 range_type = cp_build_reference_type (make_auto (), true);
10343 range_type = do_auto_deduction (range_type, range_expr,
10344 type_uses_auto (range_type));
10346 /* Create the __range variable. */
10347 range_temp = build_decl (input_location, VAR_DECL,
10348 get_identifier ("__for_range"), range_type);
10349 TREE_USED (range_temp) = 1;
10350 DECL_ARTIFICIAL (range_temp) = 1;
10352 return range_temp;
10355 /* Used by cp_parser_range_for in template context: we aren't going to
10356 do a full conversion yet, but we still need to resolve auto in the
10357 type of the for-range-declaration if present. This is basically
10358 a shortcut version of cp_convert_range_for. */
10360 static void
10361 do_range_for_auto_deduction (tree decl, tree range_expr)
10363 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10364 if (auto_node)
10366 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10367 range_temp = convert_from_reference (build_range_temp (range_expr));
10368 iter_type = (cp_parser_perform_range_for_lookup
10369 (range_temp, &begin_dummy, &end_dummy));
10370 if (iter_type)
10372 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10373 iter_type);
10374 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10375 tf_warning_or_error);
10376 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10377 iter_decl, auto_node);
10382 /* Converts a range-based for-statement into a normal
10383 for-statement, as per the definition.
10385 for (RANGE_DECL : RANGE_EXPR)
10386 BLOCK
10388 should be equivalent to:
10391 auto &&__range = RANGE_EXPR;
10392 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10393 __begin != __end;
10394 ++__begin)
10396 RANGE_DECL = *__begin;
10397 BLOCK
10401 If RANGE_EXPR is an array:
10402 BEGIN_EXPR = __range
10403 END_EXPR = __range + ARRAY_SIZE(__range)
10404 Else if RANGE_EXPR has a member 'begin' or 'end':
10405 BEGIN_EXPR = __range.begin()
10406 END_EXPR = __range.end()
10407 Else:
10408 BEGIN_EXPR = begin(__range)
10409 END_EXPR = end(__range);
10411 If __range has a member 'begin' but not 'end', or vice versa, we must
10412 still use the second alternative (it will surely fail, however).
10413 When calling begin()/end() in the third alternative we must use
10414 argument dependent lookup, but always considering 'std' as an associated
10415 namespace. */
10417 tree
10418 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10419 bool ivdep)
10421 tree begin, end;
10422 tree iter_type, begin_expr, end_expr;
10423 tree condition, expression;
10425 if (range_decl == error_mark_node || range_expr == error_mark_node)
10426 /* If an error happened previously do nothing or else a lot of
10427 unhelpful errors would be issued. */
10428 begin_expr = end_expr = iter_type = error_mark_node;
10429 else
10431 tree range_temp;
10433 if (TREE_CODE (range_expr) == VAR_DECL
10434 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10435 /* Can't bind a reference to an array of runtime bound. */
10436 range_temp = range_expr;
10437 else
10439 range_temp = build_range_temp (range_expr);
10440 pushdecl (range_temp);
10441 cp_finish_decl (range_temp, range_expr,
10442 /*is_constant_init*/false, NULL_TREE,
10443 LOOKUP_ONLYCONVERTING);
10444 range_temp = convert_from_reference (range_temp);
10446 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10447 &begin_expr, &end_expr);
10450 /* The new for initialization statement. */
10451 begin = build_decl (input_location, VAR_DECL,
10452 get_identifier ("__for_begin"), iter_type);
10453 TREE_USED (begin) = 1;
10454 DECL_ARTIFICIAL (begin) = 1;
10455 pushdecl (begin);
10456 cp_finish_decl (begin, begin_expr,
10457 /*is_constant_init*/false, NULL_TREE,
10458 LOOKUP_ONLYCONVERTING);
10460 end = build_decl (input_location, VAR_DECL,
10461 get_identifier ("__for_end"), iter_type);
10462 TREE_USED (end) = 1;
10463 DECL_ARTIFICIAL (end) = 1;
10464 pushdecl (end);
10465 cp_finish_decl (end, end_expr,
10466 /*is_constant_init*/false, NULL_TREE,
10467 LOOKUP_ONLYCONVERTING);
10469 finish_for_init_stmt (statement);
10471 /* The new for condition. */
10472 condition = build_x_binary_op (input_location, NE_EXPR,
10473 begin, ERROR_MARK,
10474 end, ERROR_MARK,
10475 NULL, tf_warning_or_error);
10476 finish_for_cond (condition, statement, ivdep);
10478 /* The new increment expression. */
10479 expression = finish_unary_op_expr (input_location,
10480 PREINCREMENT_EXPR, begin,
10481 tf_warning_or_error);
10482 finish_for_expr (expression, statement);
10484 /* The declaration is initialized with *__begin inside the loop body. */
10485 cp_finish_decl (range_decl,
10486 build_x_indirect_ref (input_location, begin, RO_NULL,
10487 tf_warning_or_error),
10488 /*is_constant_init*/false, NULL_TREE,
10489 LOOKUP_ONLYCONVERTING);
10491 return statement;
10494 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10495 We need to solve both at the same time because the method used
10496 depends on the existence of members begin or end.
10497 Returns the type deduced for the iterator expression. */
10499 static tree
10500 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10502 if (error_operand_p (range))
10504 *begin = *end = error_mark_node;
10505 return error_mark_node;
10508 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10510 error ("range-based %<for%> expression of type %qT "
10511 "has incomplete type", TREE_TYPE (range));
10512 *begin = *end = error_mark_node;
10513 return error_mark_node;
10515 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10517 /* If RANGE is an array, we will use pointer arithmetic. */
10518 *begin = range;
10519 *end = build_binary_op (input_location, PLUS_EXPR,
10520 range,
10521 array_type_nelts_top (TREE_TYPE (range)),
10523 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10525 else
10527 /* If it is not an array, we must do a bit of magic. */
10528 tree id_begin, id_end;
10529 tree member_begin, member_end;
10531 *begin = *end = error_mark_node;
10533 id_begin = get_identifier ("begin");
10534 id_end = get_identifier ("end");
10535 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10536 /*protect=*/2, /*want_type=*/false,
10537 tf_warning_or_error);
10538 member_end = lookup_member (TREE_TYPE (range), id_end,
10539 /*protect=*/2, /*want_type=*/false,
10540 tf_warning_or_error);
10542 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10544 /* Use the member functions. */
10545 if (member_begin != NULL_TREE)
10546 *begin = cp_parser_range_for_member_function (range, id_begin);
10547 else
10548 error ("range-based %<for%> expression of type %qT has an "
10549 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10551 if (member_end != NULL_TREE)
10552 *end = cp_parser_range_for_member_function (range, id_end);
10553 else
10554 error ("range-based %<for%> expression of type %qT has a "
10555 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10557 else
10559 /* Use global functions with ADL. */
10560 vec<tree, va_gc> *vec;
10561 vec = make_tree_vector ();
10563 vec_safe_push (vec, range);
10565 member_begin = perform_koenig_lookup (id_begin, vec,
10566 tf_warning_or_error);
10567 *begin = finish_call_expr (member_begin, &vec, false, true,
10568 tf_warning_or_error);
10569 member_end = perform_koenig_lookup (id_end, vec,
10570 tf_warning_or_error);
10571 *end = finish_call_expr (member_end, &vec, false, true,
10572 tf_warning_or_error);
10574 release_tree_vector (vec);
10577 /* Last common checks. */
10578 if (*begin == error_mark_node || *end == error_mark_node)
10580 /* If one of the expressions is an error do no more checks. */
10581 *begin = *end = error_mark_node;
10582 return error_mark_node;
10584 else if (type_dependent_expression_p (*begin)
10585 || type_dependent_expression_p (*end))
10586 /* Can happen, when, eg, in a template context, Koenig lookup
10587 can't resolve begin/end (c++/58503). */
10588 return NULL_TREE;
10589 else
10591 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10592 /* The unqualified type of the __begin and __end temporaries should
10593 be the same, as required by the multiple auto declaration. */
10594 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10595 error ("inconsistent begin/end types in range-based %<for%> "
10596 "statement: %qT and %qT",
10597 TREE_TYPE (*begin), TREE_TYPE (*end));
10598 return iter_type;
10603 /* Helper function for cp_parser_perform_range_for_lookup.
10604 Builds a tree for RANGE.IDENTIFIER(). */
10606 static tree
10607 cp_parser_range_for_member_function (tree range, tree identifier)
10609 tree member, res;
10610 vec<tree, va_gc> *vec;
10612 member = finish_class_member_access_expr (range, identifier,
10613 false, tf_warning_or_error);
10614 if (member == error_mark_node)
10615 return error_mark_node;
10617 vec = make_tree_vector ();
10618 res = finish_call_expr (member, &vec,
10619 /*disallow_virtual=*/false,
10620 /*koenig_p=*/false,
10621 tf_warning_or_error);
10622 release_tree_vector (vec);
10623 return res;
10626 /* Parse an iteration-statement.
10628 iteration-statement:
10629 while ( condition ) statement
10630 do statement while ( expression ) ;
10631 for ( for-init-statement condition [opt] ; expression [opt] )
10632 statement
10634 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10636 static tree
10637 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10639 cp_token *token;
10640 enum rid keyword;
10641 tree statement;
10642 unsigned char in_statement;
10644 /* Peek at the next token. */
10645 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10646 if (!token)
10647 return error_mark_node;
10649 /* Remember whether or not we are already within an iteration
10650 statement. */
10651 in_statement = parser->in_statement;
10653 /* See what kind of keyword it is. */
10654 keyword = token->keyword;
10655 switch (keyword)
10657 case RID_WHILE:
10659 tree condition;
10661 /* Begin the while-statement. */
10662 statement = begin_while_stmt ();
10663 /* Look for the `('. */
10664 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10665 /* Parse the condition. */
10666 condition = cp_parser_condition (parser);
10667 finish_while_stmt_cond (condition, statement, ivdep);
10668 /* Look for the `)'. */
10669 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10670 /* Parse the dependent statement. */
10671 parser->in_statement = IN_ITERATION_STMT;
10672 cp_parser_already_scoped_statement (parser);
10673 parser->in_statement = in_statement;
10674 /* We're done with the while-statement. */
10675 finish_while_stmt (statement);
10677 break;
10679 case RID_DO:
10681 tree expression;
10683 /* Begin the do-statement. */
10684 statement = begin_do_stmt ();
10685 /* Parse the body of the do-statement. */
10686 parser->in_statement = IN_ITERATION_STMT;
10687 cp_parser_implicitly_scoped_statement (parser, NULL);
10688 parser->in_statement = in_statement;
10689 finish_do_body (statement);
10690 /* Look for the `while' keyword. */
10691 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10692 /* Look for the `('. */
10693 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10694 /* Parse the expression. */
10695 expression = cp_parser_expression (parser);
10696 /* We're done with the do-statement. */
10697 finish_do_stmt (expression, statement, ivdep);
10698 /* Look for the `)'. */
10699 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10700 /* Look for the `;'. */
10701 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10703 break;
10705 case RID_FOR:
10707 /* Look for the `('. */
10708 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10710 statement = cp_parser_for (parser, ivdep);
10712 /* Look for the `)'. */
10713 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10715 /* Parse the body of the for-statement. */
10716 parser->in_statement = IN_ITERATION_STMT;
10717 cp_parser_already_scoped_statement (parser);
10718 parser->in_statement = in_statement;
10720 /* We're done with the for-statement. */
10721 finish_for_stmt (statement);
10723 break;
10725 default:
10726 cp_parser_error (parser, "expected iteration-statement");
10727 statement = error_mark_node;
10728 break;
10731 return statement;
10734 /* Parse a for-init-statement or the declarator of a range-based-for.
10735 Returns true if a range-based-for declaration is seen.
10737 for-init-statement:
10738 expression-statement
10739 simple-declaration */
10741 static bool
10742 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10744 /* If the next token is a `;', then we have an empty
10745 expression-statement. Grammatically, this is also a
10746 simple-declaration, but an invalid one, because it does not
10747 declare anything. Therefore, if we did not handle this case
10748 specially, we would issue an error message about an invalid
10749 declaration. */
10750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10752 bool is_range_for = false;
10753 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10755 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10756 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10758 /* N3994 -- for (id : init) ... */
10759 if (cxx_dialect < cxx1z)
10760 pedwarn (input_location, 0, "range-based for loop without a "
10761 "type-specifier only available with "
10762 "-std=c++1z or -std=gnu++1z");
10763 tree name = cp_parser_identifier (parser);
10764 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10765 *decl = build_decl (input_location, VAR_DECL, name, type);
10766 pushdecl (*decl);
10767 cp_lexer_consume_token (parser->lexer);
10768 return true;
10771 /* A colon is used in range-based for. */
10772 parser->colon_corrects_to_scope_p = false;
10774 /* We're going to speculatively look for a declaration, falling back
10775 to an expression, if necessary. */
10776 cp_parser_parse_tentatively (parser);
10777 /* Parse the declaration. */
10778 cp_parser_simple_declaration (parser,
10779 /*function_definition_allowed_p=*/false,
10780 decl);
10781 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10782 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10784 /* It is a range-for, consume the ':' */
10785 cp_lexer_consume_token (parser->lexer);
10786 is_range_for = true;
10787 if (cxx_dialect < cxx11)
10789 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10790 "range-based %<for%> loops only available with "
10791 "-std=c++11 or -std=gnu++11");
10792 *decl = error_mark_node;
10795 else
10796 /* The ';' is not consumed yet because we told
10797 cp_parser_simple_declaration not to. */
10798 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10800 if (cp_parser_parse_definitely (parser))
10801 return is_range_for;
10802 /* If the tentative parse failed, then we shall need to look for an
10803 expression-statement. */
10805 /* If we are here, it is an expression-statement. */
10806 cp_parser_expression_statement (parser, NULL_TREE);
10807 return false;
10810 /* Parse a jump-statement.
10812 jump-statement:
10813 break ;
10814 continue ;
10815 return expression [opt] ;
10816 return braced-init-list ;
10817 goto identifier ;
10819 GNU extension:
10821 jump-statement:
10822 goto * expression ;
10824 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10826 static tree
10827 cp_parser_jump_statement (cp_parser* parser)
10829 tree statement = error_mark_node;
10830 cp_token *token;
10831 enum rid keyword;
10832 unsigned char in_statement;
10834 /* Peek at the next token. */
10835 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10836 if (!token)
10837 return error_mark_node;
10839 /* See what kind of keyword it is. */
10840 keyword = token->keyword;
10841 switch (keyword)
10843 case RID_BREAK:
10844 in_statement = parser->in_statement & ~IN_IF_STMT;
10845 switch (in_statement)
10847 case 0:
10848 error_at (token->location, "break statement not within loop or switch");
10849 break;
10850 default:
10851 gcc_assert ((in_statement & IN_SWITCH_STMT)
10852 || in_statement == IN_ITERATION_STMT);
10853 statement = finish_break_stmt ();
10854 if (in_statement == IN_ITERATION_STMT)
10855 break_maybe_infinite_loop ();
10856 break;
10857 case IN_OMP_BLOCK:
10858 error_at (token->location, "invalid exit from OpenMP structured block");
10859 break;
10860 case IN_OMP_FOR:
10861 error_at (token->location, "break statement used with OpenMP for loop");
10862 break;
10863 case IN_CILK_SIMD_FOR:
10864 error_at (token->location, "break statement used with Cilk Plus for loop");
10865 break;
10867 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10868 break;
10870 case RID_CONTINUE:
10871 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10873 case 0:
10874 error_at (token->location, "continue statement not within a loop");
10875 break;
10876 case IN_CILK_SIMD_FOR:
10877 error_at (token->location,
10878 "continue statement within %<#pragma simd%> loop body");
10879 /* Fall through. */
10880 case IN_ITERATION_STMT:
10881 case IN_OMP_FOR:
10882 statement = finish_continue_stmt ();
10883 break;
10884 case IN_OMP_BLOCK:
10885 error_at (token->location, "invalid exit from OpenMP structured block");
10886 break;
10887 default:
10888 gcc_unreachable ();
10890 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10891 break;
10893 case RID_RETURN:
10895 tree expr;
10896 bool expr_non_constant_p;
10898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10900 cp_lexer_set_source_position (parser->lexer);
10901 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10902 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10904 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10905 expr = cp_parser_expression (parser);
10906 else
10907 /* If the next token is a `;', then there is no
10908 expression. */
10909 expr = NULL_TREE;
10910 /* Build the return-statement. */
10911 statement = finish_return_stmt (expr);
10912 /* Look for the final `;'. */
10913 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10915 break;
10917 case RID_GOTO:
10918 if (parser->in_function_body
10919 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
10920 error ("%<goto%> in %<constexpr%> function");
10922 /* Create the goto-statement. */
10923 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10925 /* Issue a warning about this use of a GNU extension. */
10926 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10927 /* Consume the '*' token. */
10928 cp_lexer_consume_token (parser->lexer);
10929 /* Parse the dependent expression. */
10930 finish_goto_stmt (cp_parser_expression (parser));
10932 else
10933 finish_goto_stmt (cp_parser_identifier (parser));
10934 /* Look for the final `;'. */
10935 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10936 break;
10938 default:
10939 cp_parser_error (parser, "expected jump-statement");
10940 break;
10943 return statement;
10946 /* Parse a declaration-statement.
10948 declaration-statement:
10949 block-declaration */
10951 static void
10952 cp_parser_declaration_statement (cp_parser* parser)
10954 void *p;
10956 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10957 p = obstack_alloc (&declarator_obstack, 0);
10959 /* Parse the block-declaration. */
10960 cp_parser_block_declaration (parser, /*statement_p=*/true);
10962 /* Free any declarators allocated. */
10963 obstack_free (&declarator_obstack, p);
10966 /* Some dependent statements (like `if (cond) statement'), are
10967 implicitly in their own scope. In other words, if the statement is
10968 a single statement (as opposed to a compound-statement), it is
10969 none-the-less treated as if it were enclosed in braces. Any
10970 declarations appearing in the dependent statement are out of scope
10971 after control passes that point. This function parses a statement,
10972 but ensures that is in its own scope, even if it is not a
10973 compound-statement.
10975 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10976 is a (possibly labeled) if statement which is not enclosed in
10977 braces and has an else clause. This is used to implement
10978 -Wparentheses.
10980 Returns the new statement. */
10982 static tree
10983 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10985 tree statement;
10987 if (if_p != NULL)
10988 *if_p = false;
10990 /* Mark if () ; with a special NOP_EXPR. */
10991 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10993 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10994 cp_lexer_consume_token (parser->lexer);
10995 statement = add_stmt (build_empty_stmt (loc));
10997 /* if a compound is opened, we simply parse the statement directly. */
10998 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10999 statement = cp_parser_compound_statement (parser, NULL, false, false);
11000 /* If the token is not a `{', then we must take special action. */
11001 else
11003 /* Create a compound-statement. */
11004 statement = begin_compound_stmt (0);
11005 /* Parse the dependent-statement. */
11006 cp_parser_statement (parser, NULL_TREE, false, if_p);
11007 /* Finish the dummy compound-statement. */
11008 finish_compound_stmt (statement);
11011 /* Return the statement. */
11012 return statement;
11015 /* For some dependent statements (like `while (cond) statement'), we
11016 have already created a scope. Therefore, even if the dependent
11017 statement is a compound-statement, we do not want to create another
11018 scope. */
11020 static void
11021 cp_parser_already_scoped_statement (cp_parser* parser)
11023 /* If the token is a `{', then we must take special action. */
11024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11025 cp_parser_statement (parser, NULL_TREE, false, NULL);
11026 else
11028 /* Avoid calling cp_parser_compound_statement, so that we
11029 don't create a new scope. Do everything else by hand. */
11030 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11031 /* If the next keyword is `__label__' we have a label declaration. */
11032 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11033 cp_parser_label_declaration (parser);
11034 /* Parse an (optional) statement-seq. */
11035 cp_parser_statement_seq_opt (parser, NULL_TREE);
11036 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11040 /* Declarations [gram.dcl.dcl] */
11042 /* Parse an optional declaration-sequence.
11044 declaration-seq:
11045 declaration
11046 declaration-seq declaration */
11048 static void
11049 cp_parser_declaration_seq_opt (cp_parser* parser)
11051 while (true)
11053 cp_token *token;
11055 token = cp_lexer_peek_token (parser->lexer);
11057 if (token->type == CPP_CLOSE_BRACE
11058 || token->type == CPP_EOF
11059 || token->type == CPP_PRAGMA_EOL)
11060 break;
11062 if (token->type == CPP_SEMICOLON)
11064 /* A declaration consisting of a single semicolon is
11065 invalid. Allow it unless we're being pedantic. */
11066 cp_lexer_consume_token (parser->lexer);
11067 if (!in_system_header_at (input_location))
11068 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11069 continue;
11072 /* If we're entering or exiting a region that's implicitly
11073 extern "C", modify the lang context appropriately. */
11074 if (!parser->implicit_extern_c && token->implicit_extern_c)
11076 push_lang_context (lang_name_c);
11077 parser->implicit_extern_c = true;
11079 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11081 pop_lang_context ();
11082 parser->implicit_extern_c = false;
11085 if (token->type == CPP_PRAGMA)
11087 /* A top-level declaration can consist solely of a #pragma.
11088 A nested declaration cannot, so this is done here and not
11089 in cp_parser_declaration. (A #pragma at block scope is
11090 handled in cp_parser_statement.) */
11091 cp_parser_pragma (parser, pragma_external);
11092 continue;
11095 /* Parse the declaration itself. */
11096 cp_parser_declaration (parser);
11100 /* Parse a declaration.
11102 declaration:
11103 block-declaration
11104 function-definition
11105 template-declaration
11106 explicit-instantiation
11107 explicit-specialization
11108 linkage-specification
11109 namespace-definition
11111 GNU extension:
11113 declaration:
11114 __extension__ declaration */
11116 static void
11117 cp_parser_declaration (cp_parser* parser)
11119 cp_token token1;
11120 cp_token token2;
11121 int saved_pedantic;
11122 void *p;
11123 tree attributes = NULL_TREE;
11125 /* Check for the `__extension__' keyword. */
11126 if (cp_parser_extension_opt (parser, &saved_pedantic))
11128 /* Parse the qualified declaration. */
11129 cp_parser_declaration (parser);
11130 /* Restore the PEDANTIC flag. */
11131 pedantic = saved_pedantic;
11133 return;
11136 /* Try to figure out what kind of declaration is present. */
11137 token1 = *cp_lexer_peek_token (parser->lexer);
11139 if (token1.type != CPP_EOF)
11140 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11141 else
11143 token2.type = CPP_EOF;
11144 token2.keyword = RID_MAX;
11147 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11148 p = obstack_alloc (&declarator_obstack, 0);
11150 /* If the next token is `extern' and the following token is a string
11151 literal, then we have a linkage specification. */
11152 if (token1.keyword == RID_EXTERN
11153 && cp_parser_is_pure_string_literal (&token2))
11154 cp_parser_linkage_specification (parser);
11155 /* If the next token is `template', then we have either a template
11156 declaration, an explicit instantiation, or an explicit
11157 specialization. */
11158 else if (token1.keyword == RID_TEMPLATE)
11160 /* `template <>' indicates a template specialization. */
11161 if (token2.type == CPP_LESS
11162 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11163 cp_parser_explicit_specialization (parser);
11164 /* `template <' indicates a template declaration. */
11165 else if (token2.type == CPP_LESS)
11166 cp_parser_template_declaration (parser, /*member_p=*/false);
11167 /* Anything else must be an explicit instantiation. */
11168 else
11169 cp_parser_explicit_instantiation (parser);
11171 /* If the next token is `export', then we have a template
11172 declaration. */
11173 else if (token1.keyword == RID_EXPORT)
11174 cp_parser_template_declaration (parser, /*member_p=*/false);
11175 /* If the next token is `extern', 'static' or 'inline' and the one
11176 after that is `template', we have a GNU extended explicit
11177 instantiation directive. */
11178 else if (cp_parser_allow_gnu_extensions_p (parser)
11179 && (token1.keyword == RID_EXTERN
11180 || token1.keyword == RID_STATIC
11181 || token1.keyword == RID_INLINE)
11182 && token2.keyword == RID_TEMPLATE)
11183 cp_parser_explicit_instantiation (parser);
11184 /* If the next token is `namespace', check for a named or unnamed
11185 namespace definition. */
11186 else if (token1.keyword == RID_NAMESPACE
11187 && (/* A named namespace definition. */
11188 (token2.type == CPP_NAME
11189 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11190 != CPP_EQ))
11191 /* An unnamed namespace definition. */
11192 || token2.type == CPP_OPEN_BRACE
11193 || token2.keyword == RID_ATTRIBUTE))
11194 cp_parser_namespace_definition (parser);
11195 /* An inline (associated) namespace definition. */
11196 else if (token1.keyword == RID_INLINE
11197 && token2.keyword == RID_NAMESPACE)
11198 cp_parser_namespace_definition (parser);
11199 /* Objective-C++ declaration/definition. */
11200 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11201 cp_parser_objc_declaration (parser, NULL_TREE);
11202 else if (c_dialect_objc ()
11203 && token1.keyword == RID_ATTRIBUTE
11204 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11205 cp_parser_objc_declaration (parser, attributes);
11206 /* We must have either a block declaration or a function
11207 definition. */
11208 else
11209 /* Try to parse a block-declaration, or a function-definition. */
11210 cp_parser_block_declaration (parser, /*statement_p=*/false);
11212 /* Free any declarators allocated. */
11213 obstack_free (&declarator_obstack, p);
11216 /* Parse a block-declaration.
11218 block-declaration:
11219 simple-declaration
11220 asm-definition
11221 namespace-alias-definition
11222 using-declaration
11223 using-directive
11225 GNU Extension:
11227 block-declaration:
11228 __extension__ block-declaration
11230 C++0x Extension:
11232 block-declaration:
11233 static_assert-declaration
11235 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11236 part of a declaration-statement. */
11238 static void
11239 cp_parser_block_declaration (cp_parser *parser,
11240 bool statement_p)
11242 cp_token *token1;
11243 int saved_pedantic;
11245 /* Check for the `__extension__' keyword. */
11246 if (cp_parser_extension_opt (parser, &saved_pedantic))
11248 /* Parse the qualified declaration. */
11249 cp_parser_block_declaration (parser, statement_p);
11250 /* Restore the PEDANTIC flag. */
11251 pedantic = saved_pedantic;
11253 return;
11256 /* Peek at the next token to figure out which kind of declaration is
11257 present. */
11258 token1 = cp_lexer_peek_token (parser->lexer);
11260 /* If the next keyword is `asm', we have an asm-definition. */
11261 if (token1->keyword == RID_ASM)
11263 if (statement_p)
11264 cp_parser_commit_to_tentative_parse (parser);
11265 cp_parser_asm_definition (parser);
11267 /* If the next keyword is `namespace', we have a
11268 namespace-alias-definition. */
11269 else if (token1->keyword == RID_NAMESPACE)
11270 cp_parser_namespace_alias_definition (parser);
11271 /* If the next keyword is `using', we have a
11272 using-declaration, a using-directive, or an alias-declaration. */
11273 else if (token1->keyword == RID_USING)
11275 cp_token *token2;
11277 if (statement_p)
11278 cp_parser_commit_to_tentative_parse (parser);
11279 /* If the token after `using' is `namespace', then we have a
11280 using-directive. */
11281 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11282 if (token2->keyword == RID_NAMESPACE)
11283 cp_parser_using_directive (parser);
11284 /* If the second token after 'using' is '=', then we have an
11285 alias-declaration. */
11286 else if (cxx_dialect >= cxx11
11287 && token2->type == CPP_NAME
11288 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11289 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11290 cp_parser_alias_declaration (parser);
11291 /* Otherwise, it's a using-declaration. */
11292 else
11293 cp_parser_using_declaration (parser,
11294 /*access_declaration_p=*/false);
11296 /* If the next keyword is `__label__' we have a misplaced label
11297 declaration. */
11298 else if (token1->keyword == RID_LABEL)
11300 cp_lexer_consume_token (parser->lexer);
11301 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11302 cp_parser_skip_to_end_of_statement (parser);
11303 /* If the next token is now a `;', consume it. */
11304 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11305 cp_lexer_consume_token (parser->lexer);
11307 /* If the next token is `static_assert' we have a static assertion. */
11308 else if (token1->keyword == RID_STATIC_ASSERT)
11309 cp_parser_static_assert (parser, /*member_p=*/false);
11310 /* Anything else must be a simple-declaration. */
11311 else
11312 cp_parser_simple_declaration (parser, !statement_p,
11313 /*maybe_range_for_decl*/NULL);
11316 /* Parse a simple-declaration.
11318 simple-declaration:
11319 decl-specifier-seq [opt] init-declarator-list [opt] ;
11321 init-declarator-list:
11322 init-declarator
11323 init-declarator-list , init-declarator
11325 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11326 function-definition as a simple-declaration.
11328 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11329 parsed declaration if it is an uninitialized single declarator not followed
11330 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11331 if present, will not be consumed. */
11333 static void
11334 cp_parser_simple_declaration (cp_parser* parser,
11335 bool function_definition_allowed_p,
11336 tree *maybe_range_for_decl)
11338 cp_decl_specifier_seq decl_specifiers;
11339 int declares_class_or_enum;
11340 bool saw_declarator;
11342 if (maybe_range_for_decl)
11343 *maybe_range_for_decl = NULL_TREE;
11345 /* Defer access checks until we know what is being declared; the
11346 checks for names appearing in the decl-specifier-seq should be
11347 done as if we were in the scope of the thing being declared. */
11348 push_deferring_access_checks (dk_deferred);
11350 /* Parse the decl-specifier-seq. We have to keep track of whether
11351 or not the decl-specifier-seq declares a named class or
11352 enumeration type, since that is the only case in which the
11353 init-declarator-list is allowed to be empty.
11355 [dcl.dcl]
11357 In a simple-declaration, the optional init-declarator-list can be
11358 omitted only when declaring a class or enumeration, that is when
11359 the decl-specifier-seq contains either a class-specifier, an
11360 elaborated-type-specifier, or an enum-specifier. */
11361 cp_parser_decl_specifier_seq (parser,
11362 CP_PARSER_FLAGS_OPTIONAL,
11363 &decl_specifiers,
11364 &declares_class_or_enum);
11365 /* We no longer need to defer access checks. */
11366 stop_deferring_access_checks ();
11368 /* In a block scope, a valid declaration must always have a
11369 decl-specifier-seq. By not trying to parse declarators, we can
11370 resolve the declaration/expression ambiguity more quickly. */
11371 if (!function_definition_allowed_p
11372 && !decl_specifiers.any_specifiers_p)
11374 cp_parser_error (parser, "expected declaration");
11375 goto done;
11378 /* If the next two tokens are both identifiers, the code is
11379 erroneous. The usual cause of this situation is code like:
11381 T t;
11383 where "T" should name a type -- but does not. */
11384 if (!decl_specifiers.any_type_specifiers_p
11385 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11387 /* If parsing tentatively, we should commit; we really are
11388 looking at a declaration. */
11389 cp_parser_commit_to_tentative_parse (parser);
11390 /* Give up. */
11391 goto done;
11394 /* If we have seen at least one decl-specifier, and the next token
11395 is not a parenthesis, then we must be looking at a declaration.
11396 (After "int (" we might be looking at a functional cast.) */
11397 if (decl_specifiers.any_specifiers_p
11398 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11399 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11400 && !cp_parser_error_occurred (parser))
11401 cp_parser_commit_to_tentative_parse (parser);
11403 /* Keep going until we hit the `;' at the end of the simple
11404 declaration. */
11405 saw_declarator = false;
11406 while (cp_lexer_next_token_is_not (parser->lexer,
11407 CPP_SEMICOLON))
11409 cp_token *token;
11410 bool function_definition_p;
11411 tree decl;
11413 if (saw_declarator)
11415 /* If we are processing next declarator, coma is expected */
11416 token = cp_lexer_peek_token (parser->lexer);
11417 gcc_assert (token->type == CPP_COMMA);
11418 cp_lexer_consume_token (parser->lexer);
11419 if (maybe_range_for_decl)
11420 *maybe_range_for_decl = error_mark_node;
11422 else
11423 saw_declarator = true;
11425 /* Parse the init-declarator. */
11426 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11427 /*checks=*/NULL,
11428 function_definition_allowed_p,
11429 /*member_p=*/false,
11430 declares_class_or_enum,
11431 &function_definition_p,
11432 maybe_range_for_decl);
11433 /* If an error occurred while parsing tentatively, exit quickly.
11434 (That usually happens when in the body of a function; each
11435 statement is treated as a declaration-statement until proven
11436 otherwise.) */
11437 if (cp_parser_error_occurred (parser))
11438 goto done;
11439 /* Handle function definitions specially. */
11440 if (function_definition_p)
11442 /* If the next token is a `,', then we are probably
11443 processing something like:
11445 void f() {}, *p;
11447 which is erroneous. */
11448 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11450 cp_token *token = cp_lexer_peek_token (parser->lexer);
11451 error_at (token->location,
11452 "mixing"
11453 " declarations and function-definitions is forbidden");
11455 /* Otherwise, we're done with the list of declarators. */
11456 else
11458 pop_deferring_access_checks ();
11459 return;
11462 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11463 *maybe_range_for_decl = decl;
11464 /* The next token should be either a `,' or a `;'. */
11465 token = cp_lexer_peek_token (parser->lexer);
11466 /* If it's a `,', there are more declarators to come. */
11467 if (token->type == CPP_COMMA)
11468 /* will be consumed next time around */;
11469 /* If it's a `;', we are done. */
11470 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11471 break;
11472 /* Anything else is an error. */
11473 else
11475 /* If we have already issued an error message we don't need
11476 to issue another one. */
11477 if (decl != error_mark_node
11478 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11479 cp_parser_error (parser, "expected %<,%> or %<;%>");
11480 /* Skip tokens until we reach the end of the statement. */
11481 cp_parser_skip_to_end_of_statement (parser);
11482 /* If the next token is now a `;', consume it. */
11483 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11484 cp_lexer_consume_token (parser->lexer);
11485 goto done;
11487 /* After the first time around, a function-definition is not
11488 allowed -- even if it was OK at first. For example:
11490 int i, f() {}
11492 is not valid. */
11493 function_definition_allowed_p = false;
11496 /* Issue an error message if no declarators are present, and the
11497 decl-specifier-seq does not itself declare a class or
11498 enumeration: [dcl.dcl]/3. */
11499 if (!saw_declarator)
11501 if (cp_parser_declares_only_class_p (parser))
11503 if (!declares_class_or_enum
11504 && decl_specifiers.type
11505 && OVERLOAD_TYPE_P (decl_specifiers.type))
11506 /* Ensure an error is issued anyway when finish_decltype_type,
11507 called via cp_parser_decl_specifier_seq, returns a class or
11508 an enumeration (c++/51786). */
11509 decl_specifiers.type = NULL_TREE;
11510 shadow_tag (&decl_specifiers);
11512 /* Perform any deferred access checks. */
11513 perform_deferred_access_checks (tf_warning_or_error);
11516 /* Consume the `;'. */
11517 if (!maybe_range_for_decl)
11518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11520 done:
11521 pop_deferring_access_checks ();
11524 /* Parse a decl-specifier-seq.
11526 decl-specifier-seq:
11527 decl-specifier-seq [opt] decl-specifier
11528 decl-specifier attribute-specifier-seq [opt] (C++11)
11530 decl-specifier:
11531 storage-class-specifier
11532 type-specifier
11533 function-specifier
11534 friend
11535 typedef
11537 GNU Extension:
11539 decl-specifier:
11540 attributes
11542 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11544 The parser flags FLAGS is used to control type-specifier parsing.
11546 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11547 flags:
11549 1: one of the decl-specifiers is an elaborated-type-specifier
11550 (i.e., a type declaration)
11551 2: one of the decl-specifiers is an enum-specifier or a
11552 class-specifier (i.e., a type definition)
11556 static void
11557 cp_parser_decl_specifier_seq (cp_parser* parser,
11558 cp_parser_flags flags,
11559 cp_decl_specifier_seq *decl_specs,
11560 int* declares_class_or_enum)
11562 bool constructor_possible_p = !parser->in_declarator_p;
11563 bool found_decl_spec = false;
11564 cp_token *start_token = NULL;
11565 cp_decl_spec ds;
11567 /* Clear DECL_SPECS. */
11568 clear_decl_specs (decl_specs);
11570 /* Assume no class or enumeration type is declared. */
11571 *declares_class_or_enum = 0;
11573 /* Keep reading specifiers until there are no more to read. */
11574 while (true)
11576 bool constructor_p;
11577 cp_token *token;
11578 ds = ds_last;
11580 /* Peek at the next token. */
11581 token = cp_lexer_peek_token (parser->lexer);
11583 /* Save the first token of the decl spec list for error
11584 reporting. */
11585 if (!start_token)
11586 start_token = token;
11587 /* Handle attributes. */
11588 if (cp_next_tokens_can_be_attribute_p (parser))
11590 /* Parse the attributes. */
11591 tree attrs = cp_parser_attributes_opt (parser);
11593 /* In a sequence of declaration specifiers, c++11 attributes
11594 appertain to the type that precede them. In that case
11595 [dcl.spec]/1 says:
11597 The attribute-specifier-seq affects the type only for
11598 the declaration it appears in, not other declarations
11599 involving the same type.
11601 But for now let's force the user to position the
11602 attribute either at the beginning of the declaration or
11603 after the declarator-id, which would clearly mean that it
11604 applies to the declarator. */
11605 if (cxx11_attribute_p (attrs))
11607 if (!found_decl_spec)
11608 /* The c++11 attribute is at the beginning of the
11609 declaration. It appertains to the entity being
11610 declared. */;
11611 else
11613 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11615 /* This is an attribute following a
11616 class-specifier. */
11617 if (decl_specs->type_definition_p)
11618 warn_misplaced_attr_for_class_type (token->location,
11619 decl_specs->type);
11620 attrs = NULL_TREE;
11622 else
11624 decl_specs->std_attributes
11625 = chainon (decl_specs->std_attributes,
11626 attrs);
11627 if (decl_specs->locations[ds_std_attribute] == 0)
11628 decl_specs->locations[ds_std_attribute] = token->location;
11630 continue;
11634 decl_specs->attributes
11635 = chainon (decl_specs->attributes,
11636 attrs);
11637 if (decl_specs->locations[ds_attribute] == 0)
11638 decl_specs->locations[ds_attribute] = token->location;
11639 continue;
11641 /* Assume we will find a decl-specifier keyword. */
11642 found_decl_spec = true;
11643 /* If the next token is an appropriate keyword, we can simply
11644 add it to the list. */
11645 switch (token->keyword)
11647 /* decl-specifier:
11648 friend
11649 constexpr */
11650 case RID_FRIEND:
11651 if (!at_class_scope_p ())
11653 error_at (token->location, "%<friend%> used outside of class");
11654 cp_lexer_purge_token (parser->lexer);
11656 else
11658 ds = ds_friend;
11659 /* Consume the token. */
11660 cp_lexer_consume_token (parser->lexer);
11662 break;
11664 case RID_CONSTEXPR:
11665 ds = ds_constexpr;
11666 cp_lexer_consume_token (parser->lexer);
11667 break;
11669 /* function-specifier:
11670 inline
11671 virtual
11672 explicit */
11673 case RID_INLINE:
11674 case RID_VIRTUAL:
11675 case RID_EXPLICIT:
11676 cp_parser_function_specifier_opt (parser, decl_specs);
11677 break;
11679 /* decl-specifier:
11680 typedef */
11681 case RID_TYPEDEF:
11682 ds = ds_typedef;
11683 /* Consume the token. */
11684 cp_lexer_consume_token (parser->lexer);
11685 /* A constructor declarator cannot appear in a typedef. */
11686 constructor_possible_p = false;
11687 /* The "typedef" keyword can only occur in a declaration; we
11688 may as well commit at this point. */
11689 cp_parser_commit_to_tentative_parse (parser);
11691 if (decl_specs->storage_class != sc_none)
11692 decl_specs->conflicting_specifiers_p = true;
11693 break;
11695 /* storage-class-specifier:
11696 auto
11697 register
11698 static
11699 extern
11700 mutable
11702 GNU Extension:
11703 thread */
11704 case RID_AUTO:
11705 if (cxx_dialect == cxx98)
11707 /* Consume the token. */
11708 cp_lexer_consume_token (parser->lexer);
11710 /* Complain about `auto' as a storage specifier, if
11711 we're complaining about C++0x compatibility. */
11712 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11713 " changes meaning in C++11; please remove it");
11715 /* Set the storage class anyway. */
11716 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11717 token);
11719 else
11720 /* C++0x auto type-specifier. */
11721 found_decl_spec = false;
11722 break;
11724 case RID_REGISTER:
11725 case RID_STATIC:
11726 case RID_EXTERN:
11727 case RID_MUTABLE:
11728 /* Consume the token. */
11729 cp_lexer_consume_token (parser->lexer);
11730 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11731 token);
11732 break;
11733 case RID_THREAD:
11734 /* Consume the token. */
11735 ds = ds_thread;
11736 cp_lexer_consume_token (parser->lexer);
11737 break;
11739 default:
11740 /* We did not yet find a decl-specifier yet. */
11741 found_decl_spec = false;
11742 break;
11745 if (found_decl_spec
11746 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11747 && token->keyword != RID_CONSTEXPR)
11748 error ("decl-specifier invalid in condition");
11750 if (ds != ds_last)
11751 set_and_check_decl_spec_loc (decl_specs, ds, token);
11753 /* Constructors are a special case. The `S' in `S()' is not a
11754 decl-specifier; it is the beginning of the declarator. */
11755 constructor_p
11756 = (!found_decl_spec
11757 && constructor_possible_p
11758 && (cp_parser_constructor_declarator_p
11759 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11761 /* If we don't have a DECL_SPEC yet, then we must be looking at
11762 a type-specifier. */
11763 if (!found_decl_spec && !constructor_p)
11765 int decl_spec_declares_class_or_enum;
11766 bool is_cv_qualifier;
11767 tree type_spec;
11769 type_spec
11770 = cp_parser_type_specifier (parser, flags,
11771 decl_specs,
11772 /*is_declaration=*/true,
11773 &decl_spec_declares_class_or_enum,
11774 &is_cv_qualifier);
11775 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11777 /* If this type-specifier referenced a user-defined type
11778 (a typedef, class-name, etc.), then we can't allow any
11779 more such type-specifiers henceforth.
11781 [dcl.spec]
11783 The longest sequence of decl-specifiers that could
11784 possibly be a type name is taken as the
11785 decl-specifier-seq of a declaration. The sequence shall
11786 be self-consistent as described below.
11788 [dcl.type]
11790 As a general rule, at most one type-specifier is allowed
11791 in the complete decl-specifier-seq of a declaration. The
11792 only exceptions are the following:
11794 -- const or volatile can be combined with any other
11795 type-specifier.
11797 -- signed or unsigned can be combined with char, long,
11798 short, or int.
11800 -- ..
11802 Example:
11804 typedef char* Pc;
11805 void g (const int Pc);
11807 Here, Pc is *not* part of the decl-specifier seq; it's
11808 the declarator. Therefore, once we see a type-specifier
11809 (other than a cv-qualifier), we forbid any additional
11810 user-defined types. We *do* still allow things like `int
11811 int' to be considered a decl-specifier-seq, and issue the
11812 error message later. */
11813 if (type_spec && !is_cv_qualifier)
11814 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11815 /* A constructor declarator cannot follow a type-specifier. */
11816 if (type_spec)
11818 constructor_possible_p = false;
11819 found_decl_spec = true;
11820 if (!is_cv_qualifier)
11821 decl_specs->any_type_specifiers_p = true;
11825 /* If we still do not have a DECL_SPEC, then there are no more
11826 decl-specifiers. */
11827 if (!found_decl_spec)
11828 break;
11830 decl_specs->any_specifiers_p = true;
11831 /* After we see one decl-specifier, further decl-specifiers are
11832 always optional. */
11833 flags |= CP_PARSER_FLAGS_OPTIONAL;
11836 /* Don't allow a friend specifier with a class definition. */
11837 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11838 && (*declares_class_or_enum & 2))
11839 error_at (decl_specs->locations[ds_friend],
11840 "class definition may not be declared a friend");
11843 /* Parse an (optional) storage-class-specifier.
11845 storage-class-specifier:
11846 auto
11847 register
11848 static
11849 extern
11850 mutable
11852 GNU Extension:
11854 storage-class-specifier:
11855 thread
11857 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11859 static tree
11860 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11862 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11864 case RID_AUTO:
11865 if (cxx_dialect != cxx98)
11866 return NULL_TREE;
11867 /* Fall through for C++98. */
11869 case RID_REGISTER:
11870 case RID_STATIC:
11871 case RID_EXTERN:
11872 case RID_MUTABLE:
11873 case RID_THREAD:
11874 /* Consume the token. */
11875 return cp_lexer_consume_token (parser->lexer)->u.value;
11877 default:
11878 return NULL_TREE;
11882 /* Parse an (optional) function-specifier.
11884 function-specifier:
11885 inline
11886 virtual
11887 explicit
11889 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11890 Updates DECL_SPECS, if it is non-NULL. */
11892 static tree
11893 cp_parser_function_specifier_opt (cp_parser* parser,
11894 cp_decl_specifier_seq *decl_specs)
11896 cp_token *token = cp_lexer_peek_token (parser->lexer);
11897 switch (token->keyword)
11899 case RID_INLINE:
11900 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11901 break;
11903 case RID_VIRTUAL:
11904 /* 14.5.2.3 [temp.mem]
11906 A member function template shall not be virtual. */
11907 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11908 error_at (token->location, "templates may not be %<virtual%>");
11909 else
11910 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11911 break;
11913 case RID_EXPLICIT:
11914 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11915 break;
11917 default:
11918 return NULL_TREE;
11921 /* Consume the token. */
11922 return cp_lexer_consume_token (parser->lexer)->u.value;
11925 /* Parse a linkage-specification.
11927 linkage-specification:
11928 extern string-literal { declaration-seq [opt] }
11929 extern string-literal declaration */
11931 static void
11932 cp_parser_linkage_specification (cp_parser* parser)
11934 tree linkage;
11936 /* Look for the `extern' keyword. */
11937 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11939 /* Look for the string-literal. */
11940 linkage = cp_parser_string_literal (parser, false, false);
11942 /* Transform the literal into an identifier. If the literal is a
11943 wide-character string, or contains embedded NULs, then we can't
11944 handle it as the user wants. */
11945 if (strlen (TREE_STRING_POINTER (linkage))
11946 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11948 cp_parser_error (parser, "invalid linkage-specification");
11949 /* Assume C++ linkage. */
11950 linkage = lang_name_cplusplus;
11952 else
11953 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11955 /* We're now using the new linkage. */
11956 push_lang_context (linkage);
11958 /* If the next token is a `{', then we're using the first
11959 production. */
11960 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11962 cp_ensure_no_omp_declare_simd (parser);
11964 /* Consume the `{' token. */
11965 cp_lexer_consume_token (parser->lexer);
11966 /* Parse the declarations. */
11967 cp_parser_declaration_seq_opt (parser);
11968 /* Look for the closing `}'. */
11969 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11971 /* Otherwise, there's just one declaration. */
11972 else
11974 bool saved_in_unbraced_linkage_specification_p;
11976 saved_in_unbraced_linkage_specification_p
11977 = parser->in_unbraced_linkage_specification_p;
11978 parser->in_unbraced_linkage_specification_p = true;
11979 cp_parser_declaration (parser);
11980 parser->in_unbraced_linkage_specification_p
11981 = saved_in_unbraced_linkage_specification_p;
11984 /* We're done with the linkage-specification. */
11985 pop_lang_context ();
11988 /* Parse a static_assert-declaration.
11990 static_assert-declaration:
11991 static_assert ( constant-expression , string-literal ) ;
11993 If MEMBER_P, this static_assert is a class member. */
11995 static void
11996 cp_parser_static_assert(cp_parser *parser, bool member_p)
11998 tree condition;
11999 tree message;
12000 cp_token *token;
12001 location_t saved_loc;
12002 bool dummy;
12004 /* Peek at the `static_assert' token so we can keep track of exactly
12005 where the static assertion started. */
12006 token = cp_lexer_peek_token (parser->lexer);
12007 saved_loc = token->location;
12009 /* Look for the `static_assert' keyword. */
12010 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12011 RT_STATIC_ASSERT))
12012 return;
12014 /* We know we are in a static assertion; commit to any tentative
12015 parse. */
12016 if (cp_parser_parsing_tentatively (parser))
12017 cp_parser_commit_to_tentative_parse (parser);
12019 /* Parse the `(' starting the static assertion condition. */
12020 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12022 /* Parse the constant-expression. Allow a non-constant expression
12023 here in order to give better diagnostics in finish_static_assert. */
12024 condition =
12025 cp_parser_constant_expression (parser,
12026 /*allow_non_constant_p=*/true,
12027 /*non_constant_p=*/&dummy);
12029 /* Parse the separating `,'. */
12030 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12032 /* Parse the string-literal message. */
12033 message = cp_parser_string_literal (parser,
12034 /*translate=*/false,
12035 /*wide_ok=*/true);
12037 /* A `)' completes the static assertion. */
12038 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12039 cp_parser_skip_to_closing_parenthesis (parser,
12040 /*recovering=*/true,
12041 /*or_comma=*/false,
12042 /*consume_paren=*/true);
12044 /* A semicolon terminates the declaration. */
12045 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12047 /* Complete the static assertion, which may mean either processing
12048 the static assert now or saving it for template instantiation. */
12049 finish_static_assert (condition, message, saved_loc, member_p);
12052 /* Parse the expression in decltype ( expression ). */
12054 static tree
12055 cp_parser_decltype_expr (cp_parser *parser,
12056 bool &id_expression_or_member_access_p)
12058 cp_token *id_expr_start_token;
12059 tree expr;
12061 /* First, try parsing an id-expression. */
12062 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12063 cp_parser_parse_tentatively (parser);
12064 expr = cp_parser_id_expression (parser,
12065 /*template_keyword_p=*/false,
12066 /*check_dependency_p=*/true,
12067 /*template_p=*/NULL,
12068 /*declarator_p=*/false,
12069 /*optional_p=*/false);
12071 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12073 bool non_integral_constant_expression_p = false;
12074 tree id_expression = expr;
12075 cp_id_kind idk;
12076 const char *error_msg;
12078 if (identifier_p (expr))
12079 /* Lookup the name we got back from the id-expression. */
12080 expr = cp_parser_lookup_name_simple (parser, expr,
12081 id_expr_start_token->location);
12083 if (expr
12084 && expr != error_mark_node
12085 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12086 && TREE_CODE (expr) != TYPE_DECL
12087 && (TREE_CODE (expr) != BIT_NOT_EXPR
12088 || !TYPE_P (TREE_OPERAND (expr, 0)))
12089 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12091 /* Complete lookup of the id-expression. */
12092 expr = (finish_id_expression
12093 (id_expression, expr, parser->scope, &idk,
12094 /*integral_constant_expression_p=*/false,
12095 /*allow_non_integral_constant_expression_p=*/true,
12096 &non_integral_constant_expression_p,
12097 /*template_p=*/false,
12098 /*done=*/true,
12099 /*address_p=*/false,
12100 /*template_arg_p=*/false,
12101 &error_msg,
12102 id_expr_start_token->location));
12104 if (expr == error_mark_node)
12105 /* We found an id-expression, but it was something that we
12106 should not have found. This is an error, not something
12107 we can recover from, so note that we found an
12108 id-expression and we'll recover as gracefully as
12109 possible. */
12110 id_expression_or_member_access_p = true;
12113 if (expr
12114 && expr != error_mark_node
12115 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12116 /* We have an id-expression. */
12117 id_expression_or_member_access_p = true;
12120 if (!id_expression_or_member_access_p)
12122 /* Abort the id-expression parse. */
12123 cp_parser_abort_tentative_parse (parser);
12125 /* Parsing tentatively, again. */
12126 cp_parser_parse_tentatively (parser);
12128 /* Parse a class member access. */
12129 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12130 /*cast_p=*/false, /*decltype*/true,
12131 /*member_access_only_p=*/true, NULL);
12133 if (expr
12134 && expr != error_mark_node
12135 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12136 /* We have an id-expression. */
12137 id_expression_or_member_access_p = true;
12140 if (id_expression_or_member_access_p)
12141 /* We have parsed the complete id-expression or member access. */
12142 cp_parser_parse_definitely (parser);
12143 else
12145 /* Abort our attempt to parse an id-expression or member access
12146 expression. */
12147 cp_parser_abort_tentative_parse (parser);
12149 /* Parse a full expression. */
12150 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12151 /*decltype_p=*/true);
12154 return expr;
12157 /* Parse a `decltype' type. Returns the type.
12159 simple-type-specifier:
12160 decltype ( expression )
12161 C++14 proposal:
12162 decltype ( auto ) */
12164 static tree
12165 cp_parser_decltype (cp_parser *parser)
12167 tree expr;
12168 bool id_expression_or_member_access_p = false;
12169 const char *saved_message;
12170 bool saved_integral_constant_expression_p;
12171 bool saved_non_integral_constant_expression_p;
12172 bool saved_greater_than_is_operator_p;
12173 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12175 if (start_token->type == CPP_DECLTYPE)
12177 /* Already parsed. */
12178 cp_lexer_consume_token (parser->lexer);
12179 return start_token->u.value;
12182 /* Look for the `decltype' token. */
12183 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12184 return error_mark_node;
12186 /* Parse the opening `('. */
12187 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12188 return error_mark_node;
12190 /* decltype (auto) */
12191 if (cxx_dialect >= cxx14
12192 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12194 cp_lexer_consume_token (parser->lexer);
12195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12196 return error_mark_node;
12197 expr = make_decltype_auto ();
12198 AUTO_IS_DECLTYPE (expr) = true;
12199 goto rewrite;
12202 /* Types cannot be defined in a `decltype' expression. Save away the
12203 old message. */
12204 saved_message = parser->type_definition_forbidden_message;
12206 /* And create the new one. */
12207 parser->type_definition_forbidden_message
12208 = G_("types may not be defined in %<decltype%> expressions");
12210 /* The restrictions on constant-expressions do not apply inside
12211 decltype expressions. */
12212 saved_integral_constant_expression_p
12213 = parser->integral_constant_expression_p;
12214 saved_non_integral_constant_expression_p
12215 = parser->non_integral_constant_expression_p;
12216 parser->integral_constant_expression_p = false;
12218 /* Within a parenthesized expression, a `>' token is always
12219 the greater-than operator. */
12220 saved_greater_than_is_operator_p
12221 = parser->greater_than_is_operator_p;
12222 parser->greater_than_is_operator_p = true;
12224 /* Do not actually evaluate the expression. */
12225 ++cp_unevaluated_operand;
12227 /* Do not warn about problems with the expression. */
12228 ++c_inhibit_evaluation_warnings;
12230 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12232 /* Go back to evaluating expressions. */
12233 --cp_unevaluated_operand;
12234 --c_inhibit_evaluation_warnings;
12236 /* The `>' token might be the end of a template-id or
12237 template-parameter-list now. */
12238 parser->greater_than_is_operator_p
12239 = saved_greater_than_is_operator_p;
12241 /* Restore the old message and the integral constant expression
12242 flags. */
12243 parser->type_definition_forbidden_message = saved_message;
12244 parser->integral_constant_expression_p
12245 = saved_integral_constant_expression_p;
12246 parser->non_integral_constant_expression_p
12247 = saved_non_integral_constant_expression_p;
12249 /* Parse to the closing `)'. */
12250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12252 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12253 /*consume_paren=*/true);
12254 return error_mark_node;
12257 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12258 tf_warning_or_error);
12260 rewrite:
12261 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12262 it again. */
12263 start_token->type = CPP_DECLTYPE;
12264 start_token->u.value = expr;
12265 start_token->keyword = RID_MAX;
12266 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12268 return expr;
12271 /* Special member functions [gram.special] */
12273 /* Parse a conversion-function-id.
12275 conversion-function-id:
12276 operator conversion-type-id
12278 Returns an IDENTIFIER_NODE representing the operator. */
12280 static tree
12281 cp_parser_conversion_function_id (cp_parser* parser)
12283 tree type;
12284 tree saved_scope;
12285 tree saved_qualifying_scope;
12286 tree saved_object_scope;
12287 tree pushed_scope = NULL_TREE;
12289 /* Look for the `operator' token. */
12290 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12291 return error_mark_node;
12292 /* When we parse the conversion-type-id, the current scope will be
12293 reset. However, we need that information in able to look up the
12294 conversion function later, so we save it here. */
12295 saved_scope = parser->scope;
12296 saved_qualifying_scope = parser->qualifying_scope;
12297 saved_object_scope = parser->object_scope;
12298 /* We must enter the scope of the class so that the names of
12299 entities declared within the class are available in the
12300 conversion-type-id. For example, consider:
12302 struct S {
12303 typedef int I;
12304 operator I();
12307 S::operator I() { ... }
12309 In order to see that `I' is a type-name in the definition, we
12310 must be in the scope of `S'. */
12311 if (saved_scope)
12312 pushed_scope = push_scope (saved_scope);
12313 /* Parse the conversion-type-id. */
12314 type = cp_parser_conversion_type_id (parser);
12315 /* Leave the scope of the class, if any. */
12316 if (pushed_scope)
12317 pop_scope (pushed_scope);
12318 /* Restore the saved scope. */
12319 parser->scope = saved_scope;
12320 parser->qualifying_scope = saved_qualifying_scope;
12321 parser->object_scope = saved_object_scope;
12322 /* If the TYPE is invalid, indicate failure. */
12323 if (type == error_mark_node)
12324 return error_mark_node;
12325 return mangle_conv_op_name_for_type (type);
12328 /* Parse a conversion-type-id:
12330 conversion-type-id:
12331 type-specifier-seq conversion-declarator [opt]
12333 Returns the TYPE specified. */
12335 static tree
12336 cp_parser_conversion_type_id (cp_parser* parser)
12338 tree attributes;
12339 cp_decl_specifier_seq type_specifiers;
12340 cp_declarator *declarator;
12341 tree type_specified;
12342 const char *saved_message;
12344 /* Parse the attributes. */
12345 attributes = cp_parser_attributes_opt (parser);
12347 saved_message = parser->type_definition_forbidden_message;
12348 parser->type_definition_forbidden_message
12349 = G_("types may not be defined in a conversion-type-id");
12351 /* Parse the type-specifiers. */
12352 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12353 /*is_trailing_return=*/false,
12354 &type_specifiers);
12356 parser->type_definition_forbidden_message = saved_message;
12358 /* If that didn't work, stop. */
12359 if (type_specifiers.type == error_mark_node)
12360 return error_mark_node;
12361 /* Parse the conversion-declarator. */
12362 declarator = cp_parser_conversion_declarator_opt (parser);
12364 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12365 /*initialized=*/0, &attributes);
12366 if (attributes)
12367 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12369 /* Don't give this error when parsing tentatively. This happens to
12370 work because we always parse this definitively once. */
12371 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12372 && type_uses_auto (type_specified))
12374 if (cxx_dialect < cxx14)
12376 error ("invalid use of %<auto%> in conversion operator");
12377 return error_mark_node;
12379 else if (template_parm_scope_p ())
12380 warning (0, "use of %<auto%> in member template "
12381 "conversion operator can never be deduced");
12384 return type_specified;
12387 /* Parse an (optional) conversion-declarator.
12389 conversion-declarator:
12390 ptr-operator conversion-declarator [opt]
12394 static cp_declarator *
12395 cp_parser_conversion_declarator_opt (cp_parser* parser)
12397 enum tree_code code;
12398 tree class_type, std_attributes = NULL_TREE;
12399 cp_cv_quals cv_quals;
12401 /* We don't know if there's a ptr-operator next, or not. */
12402 cp_parser_parse_tentatively (parser);
12403 /* Try the ptr-operator. */
12404 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12405 &std_attributes);
12406 /* If it worked, look for more conversion-declarators. */
12407 if (cp_parser_parse_definitely (parser))
12409 cp_declarator *declarator;
12411 /* Parse another optional declarator. */
12412 declarator = cp_parser_conversion_declarator_opt (parser);
12414 declarator = cp_parser_make_indirect_declarator
12415 (code, class_type, cv_quals, declarator, std_attributes);
12417 return declarator;
12420 return NULL;
12423 /* Parse an (optional) ctor-initializer.
12425 ctor-initializer:
12426 : mem-initializer-list
12428 Returns TRUE iff the ctor-initializer was actually present. */
12430 static bool
12431 cp_parser_ctor_initializer_opt (cp_parser* parser)
12433 /* If the next token is not a `:', then there is no
12434 ctor-initializer. */
12435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12437 /* Do default initialization of any bases and members. */
12438 if (DECL_CONSTRUCTOR_P (current_function_decl))
12439 finish_mem_initializers (NULL_TREE);
12441 return false;
12444 /* Consume the `:' token. */
12445 cp_lexer_consume_token (parser->lexer);
12446 /* And the mem-initializer-list. */
12447 cp_parser_mem_initializer_list (parser);
12449 return true;
12452 /* Parse a mem-initializer-list.
12454 mem-initializer-list:
12455 mem-initializer ... [opt]
12456 mem-initializer ... [opt] , mem-initializer-list */
12458 static void
12459 cp_parser_mem_initializer_list (cp_parser* parser)
12461 tree mem_initializer_list = NULL_TREE;
12462 tree target_ctor = error_mark_node;
12463 cp_token *token = cp_lexer_peek_token (parser->lexer);
12465 /* Let the semantic analysis code know that we are starting the
12466 mem-initializer-list. */
12467 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12468 error_at (token->location,
12469 "only constructors take member initializers");
12471 /* Loop through the list. */
12472 while (true)
12474 tree mem_initializer;
12476 token = cp_lexer_peek_token (parser->lexer);
12477 /* Parse the mem-initializer. */
12478 mem_initializer = cp_parser_mem_initializer (parser);
12479 /* If the next token is a `...', we're expanding member initializers. */
12480 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12482 /* Consume the `...'. */
12483 cp_lexer_consume_token (parser->lexer);
12485 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12486 can be expanded but members cannot. */
12487 if (mem_initializer != error_mark_node
12488 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12490 error_at (token->location,
12491 "cannot expand initializer for member %<%D%>",
12492 TREE_PURPOSE (mem_initializer));
12493 mem_initializer = error_mark_node;
12496 /* Construct the pack expansion type. */
12497 if (mem_initializer != error_mark_node)
12498 mem_initializer = make_pack_expansion (mem_initializer);
12500 if (target_ctor != error_mark_node
12501 && mem_initializer != error_mark_node)
12503 error ("mem-initializer for %qD follows constructor delegation",
12504 TREE_PURPOSE (mem_initializer));
12505 mem_initializer = error_mark_node;
12507 /* Look for a target constructor. */
12508 if (mem_initializer != error_mark_node
12509 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12510 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12512 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12513 if (mem_initializer_list)
12515 error ("constructor delegation follows mem-initializer for %qD",
12516 TREE_PURPOSE (mem_initializer_list));
12517 mem_initializer = error_mark_node;
12519 target_ctor = mem_initializer;
12521 /* Add it to the list, unless it was erroneous. */
12522 if (mem_initializer != error_mark_node)
12524 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12525 mem_initializer_list = mem_initializer;
12527 /* If the next token is not a `,', we're done. */
12528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12529 break;
12530 /* Consume the `,' token. */
12531 cp_lexer_consume_token (parser->lexer);
12534 /* Perform semantic analysis. */
12535 if (DECL_CONSTRUCTOR_P (current_function_decl))
12536 finish_mem_initializers (mem_initializer_list);
12539 /* Parse a mem-initializer.
12541 mem-initializer:
12542 mem-initializer-id ( expression-list [opt] )
12543 mem-initializer-id braced-init-list
12545 GNU extension:
12547 mem-initializer:
12548 ( expression-list [opt] )
12550 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12551 class) or FIELD_DECL (for a non-static data member) to initialize;
12552 the TREE_VALUE is the expression-list. An empty initialization
12553 list is represented by void_list_node. */
12555 static tree
12556 cp_parser_mem_initializer (cp_parser* parser)
12558 tree mem_initializer_id;
12559 tree expression_list;
12560 tree member;
12561 cp_token *token = cp_lexer_peek_token (parser->lexer);
12563 /* Find out what is being initialized. */
12564 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12566 permerror (token->location,
12567 "anachronistic old-style base class initializer");
12568 mem_initializer_id = NULL_TREE;
12570 else
12572 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12573 if (mem_initializer_id == error_mark_node)
12574 return mem_initializer_id;
12576 member = expand_member_init (mem_initializer_id);
12577 if (member && !DECL_P (member))
12578 in_base_initializer = 1;
12580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12582 bool expr_non_constant_p;
12583 cp_lexer_set_source_position (parser->lexer);
12584 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12585 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12586 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12587 expression_list = build_tree_list (NULL_TREE, expression_list);
12589 else
12591 vec<tree, va_gc> *vec;
12592 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12593 /*cast_p=*/false,
12594 /*allow_expansion_p=*/true,
12595 /*non_constant_p=*/NULL);
12596 if (vec == NULL)
12597 return error_mark_node;
12598 expression_list = build_tree_list_vec (vec);
12599 release_tree_vector (vec);
12602 if (expression_list == error_mark_node)
12603 return error_mark_node;
12604 if (!expression_list)
12605 expression_list = void_type_node;
12607 in_base_initializer = 0;
12609 return member ? build_tree_list (member, expression_list) : error_mark_node;
12612 /* Parse a mem-initializer-id.
12614 mem-initializer-id:
12615 :: [opt] nested-name-specifier [opt] class-name
12616 identifier
12618 Returns a TYPE indicating the class to be initializer for the first
12619 production. Returns an IDENTIFIER_NODE indicating the data member
12620 to be initialized for the second production. */
12622 static tree
12623 cp_parser_mem_initializer_id (cp_parser* parser)
12625 bool global_scope_p;
12626 bool nested_name_specifier_p;
12627 bool template_p = false;
12628 tree id;
12630 cp_token *token = cp_lexer_peek_token (parser->lexer);
12632 /* `typename' is not allowed in this context ([temp.res]). */
12633 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12635 error_at (token->location,
12636 "keyword %<typename%> not allowed in this context (a qualified "
12637 "member initializer is implicitly a type)");
12638 cp_lexer_consume_token (parser->lexer);
12640 /* Look for the optional `::' operator. */
12641 global_scope_p
12642 = (cp_parser_global_scope_opt (parser,
12643 /*current_scope_valid_p=*/false)
12644 != NULL_TREE);
12645 /* Look for the optional nested-name-specifier. The simplest way to
12646 implement:
12648 [temp.res]
12650 The keyword `typename' is not permitted in a base-specifier or
12651 mem-initializer; in these contexts a qualified name that
12652 depends on a template-parameter is implicitly assumed to be a
12653 type name.
12655 is to assume that we have seen the `typename' keyword at this
12656 point. */
12657 nested_name_specifier_p
12658 = (cp_parser_nested_name_specifier_opt (parser,
12659 /*typename_keyword_p=*/true,
12660 /*check_dependency_p=*/true,
12661 /*type_p=*/true,
12662 /*is_declaration=*/true)
12663 != NULL_TREE);
12664 if (nested_name_specifier_p)
12665 template_p = cp_parser_optional_template_keyword (parser);
12666 /* If there is a `::' operator or a nested-name-specifier, then we
12667 are definitely looking for a class-name. */
12668 if (global_scope_p || nested_name_specifier_p)
12669 return cp_parser_class_name (parser,
12670 /*typename_keyword_p=*/true,
12671 /*template_keyword_p=*/template_p,
12672 typename_type,
12673 /*check_dependency_p=*/true,
12674 /*class_head_p=*/false,
12675 /*is_declaration=*/true);
12676 /* Otherwise, we could also be looking for an ordinary identifier. */
12677 cp_parser_parse_tentatively (parser);
12678 /* Try a class-name. */
12679 id = cp_parser_class_name (parser,
12680 /*typename_keyword_p=*/true,
12681 /*template_keyword_p=*/false,
12682 none_type,
12683 /*check_dependency_p=*/true,
12684 /*class_head_p=*/false,
12685 /*is_declaration=*/true);
12686 /* If we found one, we're done. */
12687 if (cp_parser_parse_definitely (parser))
12688 return id;
12689 /* Otherwise, look for an ordinary identifier. */
12690 return cp_parser_identifier (parser);
12693 /* Overloading [gram.over] */
12695 /* Parse an operator-function-id.
12697 operator-function-id:
12698 operator operator
12700 Returns an IDENTIFIER_NODE for the operator which is a
12701 human-readable spelling of the identifier, e.g., `operator +'. */
12703 static tree
12704 cp_parser_operator_function_id (cp_parser* parser)
12706 /* Look for the `operator' keyword. */
12707 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12708 return error_mark_node;
12709 /* And then the name of the operator itself. */
12710 return cp_parser_operator (parser);
12713 /* Return an identifier node for a user-defined literal operator.
12714 The suffix identifier is chained to the operator name identifier. */
12716 static tree
12717 cp_literal_operator_id (const char* name)
12719 tree identifier;
12720 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12721 + strlen (name) + 10);
12722 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12723 identifier = get_identifier (buffer);
12725 return identifier;
12728 /* Parse an operator.
12730 operator:
12731 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12732 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12733 || ++ -- , ->* -> () []
12735 GNU Extensions:
12737 operator:
12738 <? >? <?= >?=
12740 Returns an IDENTIFIER_NODE for the operator which is a
12741 human-readable spelling of the identifier, e.g., `operator +'. */
12743 static tree
12744 cp_parser_operator (cp_parser* parser)
12746 tree id = NULL_TREE;
12747 cp_token *token;
12748 bool utf8 = false;
12750 /* Peek at the next token. */
12751 token = cp_lexer_peek_token (parser->lexer);
12752 /* Figure out which operator we have. */
12753 switch (token->type)
12755 case CPP_KEYWORD:
12757 enum tree_code op;
12759 /* The keyword should be either `new' or `delete'. */
12760 if (token->keyword == RID_NEW)
12761 op = NEW_EXPR;
12762 else if (token->keyword == RID_DELETE)
12763 op = DELETE_EXPR;
12764 else
12765 break;
12767 /* Consume the `new' or `delete' token. */
12768 cp_lexer_consume_token (parser->lexer);
12770 /* Peek at the next token. */
12771 token = cp_lexer_peek_token (parser->lexer);
12772 /* If it's a `[' token then this is the array variant of the
12773 operator. */
12774 if (token->type == CPP_OPEN_SQUARE)
12776 /* Consume the `[' token. */
12777 cp_lexer_consume_token (parser->lexer);
12778 /* Look for the `]' token. */
12779 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12780 id = ansi_opname (op == NEW_EXPR
12781 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12783 /* Otherwise, we have the non-array variant. */
12784 else
12785 id = ansi_opname (op);
12787 return id;
12790 case CPP_PLUS:
12791 id = ansi_opname (PLUS_EXPR);
12792 break;
12794 case CPP_MINUS:
12795 id = ansi_opname (MINUS_EXPR);
12796 break;
12798 case CPP_MULT:
12799 id = ansi_opname (MULT_EXPR);
12800 break;
12802 case CPP_DIV:
12803 id = ansi_opname (TRUNC_DIV_EXPR);
12804 break;
12806 case CPP_MOD:
12807 id = ansi_opname (TRUNC_MOD_EXPR);
12808 break;
12810 case CPP_XOR:
12811 id = ansi_opname (BIT_XOR_EXPR);
12812 break;
12814 case CPP_AND:
12815 id = ansi_opname (BIT_AND_EXPR);
12816 break;
12818 case CPP_OR:
12819 id = ansi_opname (BIT_IOR_EXPR);
12820 break;
12822 case CPP_COMPL:
12823 id = ansi_opname (BIT_NOT_EXPR);
12824 break;
12826 case CPP_NOT:
12827 id = ansi_opname (TRUTH_NOT_EXPR);
12828 break;
12830 case CPP_EQ:
12831 id = ansi_assopname (NOP_EXPR);
12832 break;
12834 case CPP_LESS:
12835 id = ansi_opname (LT_EXPR);
12836 break;
12838 case CPP_GREATER:
12839 id = ansi_opname (GT_EXPR);
12840 break;
12842 case CPP_PLUS_EQ:
12843 id = ansi_assopname (PLUS_EXPR);
12844 break;
12846 case CPP_MINUS_EQ:
12847 id = ansi_assopname (MINUS_EXPR);
12848 break;
12850 case CPP_MULT_EQ:
12851 id = ansi_assopname (MULT_EXPR);
12852 break;
12854 case CPP_DIV_EQ:
12855 id = ansi_assopname (TRUNC_DIV_EXPR);
12856 break;
12858 case CPP_MOD_EQ:
12859 id = ansi_assopname (TRUNC_MOD_EXPR);
12860 break;
12862 case CPP_XOR_EQ:
12863 id = ansi_assopname (BIT_XOR_EXPR);
12864 break;
12866 case CPP_AND_EQ:
12867 id = ansi_assopname (BIT_AND_EXPR);
12868 break;
12870 case CPP_OR_EQ:
12871 id = ansi_assopname (BIT_IOR_EXPR);
12872 break;
12874 case CPP_LSHIFT:
12875 id = ansi_opname (LSHIFT_EXPR);
12876 break;
12878 case CPP_RSHIFT:
12879 id = ansi_opname (RSHIFT_EXPR);
12880 break;
12882 case CPP_LSHIFT_EQ:
12883 id = ansi_assopname (LSHIFT_EXPR);
12884 break;
12886 case CPP_RSHIFT_EQ:
12887 id = ansi_assopname (RSHIFT_EXPR);
12888 break;
12890 case CPP_EQ_EQ:
12891 id = ansi_opname (EQ_EXPR);
12892 break;
12894 case CPP_NOT_EQ:
12895 id = ansi_opname (NE_EXPR);
12896 break;
12898 case CPP_LESS_EQ:
12899 id = ansi_opname (LE_EXPR);
12900 break;
12902 case CPP_GREATER_EQ:
12903 id = ansi_opname (GE_EXPR);
12904 break;
12906 case CPP_AND_AND:
12907 id = ansi_opname (TRUTH_ANDIF_EXPR);
12908 break;
12910 case CPP_OR_OR:
12911 id = ansi_opname (TRUTH_ORIF_EXPR);
12912 break;
12914 case CPP_PLUS_PLUS:
12915 id = ansi_opname (POSTINCREMENT_EXPR);
12916 break;
12918 case CPP_MINUS_MINUS:
12919 id = ansi_opname (PREDECREMENT_EXPR);
12920 break;
12922 case CPP_COMMA:
12923 id = ansi_opname (COMPOUND_EXPR);
12924 break;
12926 case CPP_DEREF_STAR:
12927 id = ansi_opname (MEMBER_REF);
12928 break;
12930 case CPP_DEREF:
12931 id = ansi_opname (COMPONENT_REF);
12932 break;
12934 case CPP_OPEN_PAREN:
12935 /* Consume the `('. */
12936 cp_lexer_consume_token (parser->lexer);
12937 /* Look for the matching `)'. */
12938 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12939 return ansi_opname (CALL_EXPR);
12941 case CPP_OPEN_SQUARE:
12942 /* Consume the `['. */
12943 cp_lexer_consume_token (parser->lexer);
12944 /* Look for the matching `]'. */
12945 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12946 return ansi_opname (ARRAY_REF);
12948 case CPP_UTF8STRING:
12949 case CPP_UTF8STRING_USERDEF:
12950 utf8 = true;
12951 case CPP_STRING:
12952 case CPP_WSTRING:
12953 case CPP_STRING16:
12954 case CPP_STRING32:
12955 case CPP_STRING_USERDEF:
12956 case CPP_WSTRING_USERDEF:
12957 case CPP_STRING16_USERDEF:
12958 case CPP_STRING32_USERDEF:
12960 tree str, string_tree;
12961 int sz, len;
12963 if (cxx_dialect == cxx98)
12964 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12966 /* Consume the string. */
12967 str = cp_parser_string_literal (parser, /*translate=*/true,
12968 /*wide_ok=*/true, /*lookup_udlit=*/false);
12969 if (str == error_mark_node)
12970 return error_mark_node;
12971 else if (TREE_CODE (str) == USERDEF_LITERAL)
12973 string_tree = USERDEF_LITERAL_VALUE (str);
12974 id = USERDEF_LITERAL_SUFFIX_ID (str);
12976 else
12978 string_tree = str;
12979 /* Look for the suffix identifier. */
12980 token = cp_lexer_peek_token (parser->lexer);
12981 if (token->type == CPP_NAME)
12982 id = cp_parser_identifier (parser);
12983 else if (token->type == CPP_KEYWORD)
12985 error ("unexpected keyword;"
12986 " remove space between quotes and suffix identifier");
12987 return error_mark_node;
12989 else
12991 error ("expected suffix identifier");
12992 return error_mark_node;
12995 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
12996 (TREE_TYPE (TREE_TYPE (string_tree))));
12997 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
12998 if (len != 0)
13000 error ("expected empty string after %<operator%> keyword");
13001 return error_mark_node;
13003 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13004 != char_type_node)
13006 error ("invalid encoding prefix in literal operator");
13007 return error_mark_node;
13009 if (id != error_mark_node)
13011 const char *name = IDENTIFIER_POINTER (id);
13012 id = cp_literal_operator_id (name);
13014 return id;
13017 default:
13018 /* Anything else is an error. */
13019 break;
13022 /* If we have selected an identifier, we need to consume the
13023 operator token. */
13024 if (id)
13025 cp_lexer_consume_token (parser->lexer);
13026 /* Otherwise, no valid operator name was present. */
13027 else
13029 cp_parser_error (parser, "expected operator");
13030 id = error_mark_node;
13033 return id;
13036 /* Parse a template-declaration.
13038 template-declaration:
13039 export [opt] template < template-parameter-list > declaration
13041 If MEMBER_P is TRUE, this template-declaration occurs within a
13042 class-specifier.
13044 The grammar rule given by the standard isn't correct. What
13045 is really meant is:
13047 template-declaration:
13048 export [opt] template-parameter-list-seq
13049 decl-specifier-seq [opt] init-declarator [opt] ;
13050 export [opt] template-parameter-list-seq
13051 function-definition
13053 template-parameter-list-seq:
13054 template-parameter-list-seq [opt]
13055 template < template-parameter-list > */
13057 static void
13058 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13060 /* Check for `export'. */
13061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13063 /* Consume the `export' token. */
13064 cp_lexer_consume_token (parser->lexer);
13065 /* Warn that we do not support `export'. */
13066 warning (0, "keyword %<export%> not implemented, and will be ignored");
13069 cp_parser_template_declaration_after_export (parser, member_p);
13072 /* Parse a template-parameter-list.
13074 template-parameter-list:
13075 template-parameter
13076 template-parameter-list , template-parameter
13078 Returns a TREE_LIST. Each node represents a template parameter.
13079 The nodes are connected via their TREE_CHAINs. */
13081 static tree
13082 cp_parser_template_parameter_list (cp_parser* parser)
13084 tree parameter_list = NULL_TREE;
13086 begin_template_parm_list ();
13088 /* The loop below parses the template parms. We first need to know
13089 the total number of template parms to be able to compute proper
13090 canonical types of each dependent type. So after the loop, when
13091 we know the total number of template parms,
13092 end_template_parm_list computes the proper canonical types and
13093 fixes up the dependent types accordingly. */
13094 while (true)
13096 tree parameter;
13097 bool is_non_type;
13098 bool is_parameter_pack;
13099 location_t parm_loc;
13101 /* Parse the template-parameter. */
13102 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13103 parameter = cp_parser_template_parameter (parser,
13104 &is_non_type,
13105 &is_parameter_pack);
13106 /* Add it to the list. */
13107 if (parameter != error_mark_node)
13108 parameter_list = process_template_parm (parameter_list,
13109 parm_loc,
13110 parameter,
13111 is_non_type,
13112 is_parameter_pack);
13113 else
13115 tree err_parm = build_tree_list (parameter, parameter);
13116 parameter_list = chainon (parameter_list, err_parm);
13119 /* If the next token is not a `,', we're done. */
13120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13121 break;
13122 /* Otherwise, consume the `,' token. */
13123 cp_lexer_consume_token (parser->lexer);
13126 return end_template_parm_list (parameter_list);
13129 /* Parse a template-parameter.
13131 template-parameter:
13132 type-parameter
13133 parameter-declaration
13135 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13136 the parameter. The TREE_PURPOSE is the default value, if any.
13137 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13138 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13139 set to true iff this parameter is a parameter pack. */
13141 static tree
13142 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13143 bool *is_parameter_pack)
13145 cp_token *token;
13146 cp_parameter_declarator *parameter_declarator;
13147 cp_declarator *id_declarator;
13148 tree parm;
13150 /* Assume it is a type parameter or a template parameter. */
13151 *is_non_type = false;
13152 /* Assume it not a parameter pack. */
13153 *is_parameter_pack = false;
13154 /* Peek at the next token. */
13155 token = cp_lexer_peek_token (parser->lexer);
13156 /* If it is `class' or `template', we have a type-parameter. */
13157 if (token->keyword == RID_TEMPLATE)
13158 return cp_parser_type_parameter (parser, is_parameter_pack);
13159 /* If it is `class' or `typename' we do not know yet whether it is a
13160 type parameter or a non-type parameter. Consider:
13162 template <typename T, typename T::X X> ...
13166 template <class C, class D*> ...
13168 Here, the first parameter is a type parameter, and the second is
13169 a non-type parameter. We can tell by looking at the token after
13170 the identifier -- if it is a `,', `=', or `>' then we have a type
13171 parameter. */
13172 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13174 /* Peek at the token after `class' or `typename'. */
13175 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13176 /* If it's an ellipsis, we have a template type parameter
13177 pack. */
13178 if (token->type == CPP_ELLIPSIS)
13179 return cp_parser_type_parameter (parser, is_parameter_pack);
13180 /* If it's an identifier, skip it. */
13181 if (token->type == CPP_NAME)
13182 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13183 /* Now, see if the token looks like the end of a template
13184 parameter. */
13185 if (token->type == CPP_COMMA
13186 || token->type == CPP_EQ
13187 || token->type == CPP_GREATER)
13188 return cp_parser_type_parameter (parser, is_parameter_pack);
13191 /* Otherwise, it is a non-type parameter.
13193 [temp.param]
13195 When parsing a default template-argument for a non-type
13196 template-parameter, the first non-nested `>' is taken as the end
13197 of the template parameter-list rather than a greater-than
13198 operator. */
13199 *is_non_type = true;
13200 parameter_declarator
13201 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13202 /*parenthesized_p=*/NULL);
13204 if (!parameter_declarator)
13205 return error_mark_node;
13207 /* If the parameter declaration is marked as a parameter pack, set
13208 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13209 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13210 grokdeclarator. */
13211 if (parameter_declarator->declarator
13212 && parameter_declarator->declarator->parameter_pack_p)
13214 *is_parameter_pack = true;
13215 parameter_declarator->declarator->parameter_pack_p = false;
13218 if (parameter_declarator->default_argument)
13220 /* Can happen in some cases of erroneous input (c++/34892). */
13221 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13222 /* Consume the `...' for better error recovery. */
13223 cp_lexer_consume_token (parser->lexer);
13225 /* If the next token is an ellipsis, and we don't already have it
13226 marked as a parameter pack, then we have a parameter pack (that
13227 has no declarator). */
13228 else if (!*is_parameter_pack
13229 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13230 && (declarator_can_be_parameter_pack
13231 (parameter_declarator->declarator)))
13233 /* Consume the `...'. */
13234 cp_lexer_consume_token (parser->lexer);
13235 maybe_warn_variadic_templates ();
13237 *is_parameter_pack = true;
13239 /* We might end up with a pack expansion as the type of the non-type
13240 template parameter, in which case this is a non-type template
13241 parameter pack. */
13242 else if (parameter_declarator->decl_specifiers.type
13243 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13245 *is_parameter_pack = true;
13246 parameter_declarator->decl_specifiers.type =
13247 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13250 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13252 /* Parameter packs cannot have default arguments. However, a
13253 user may try to do so, so we'll parse them and give an
13254 appropriate diagnostic here. */
13256 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13258 /* Find the name of the parameter pack. */
13259 id_declarator = parameter_declarator->declarator;
13260 while (id_declarator && id_declarator->kind != cdk_id)
13261 id_declarator = id_declarator->declarator;
13263 if (id_declarator && id_declarator->kind == cdk_id)
13264 error_at (start_token->location,
13265 "template parameter pack %qD cannot have a default argument",
13266 id_declarator->u.id.unqualified_name);
13267 else
13268 error_at (start_token->location,
13269 "template parameter pack cannot have a default argument");
13271 /* Parse the default argument, but throw away the result. */
13272 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13275 parm = grokdeclarator (parameter_declarator->declarator,
13276 &parameter_declarator->decl_specifiers,
13277 TPARM, /*initialized=*/0,
13278 /*attrlist=*/NULL);
13279 if (parm == error_mark_node)
13280 return error_mark_node;
13282 return build_tree_list (parameter_declarator->default_argument, parm);
13285 /* Parse a type-parameter.
13287 type-parameter:
13288 class identifier [opt]
13289 class identifier [opt] = type-id
13290 typename identifier [opt]
13291 typename identifier [opt] = type-id
13292 template < template-parameter-list > class identifier [opt]
13293 template < template-parameter-list > class identifier [opt]
13294 = id-expression
13296 GNU Extension (variadic templates):
13298 type-parameter:
13299 class ... identifier [opt]
13300 typename ... identifier [opt]
13302 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13303 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13304 the declaration of the parameter.
13306 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13308 static tree
13309 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13311 cp_token *token;
13312 tree parameter;
13314 /* Look for a keyword to tell us what kind of parameter this is. */
13315 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13316 if (!token)
13317 return error_mark_node;
13319 switch (token->keyword)
13321 case RID_CLASS:
13322 case RID_TYPENAME:
13324 tree identifier;
13325 tree default_argument;
13327 /* If the next token is an ellipsis, we have a template
13328 argument pack. */
13329 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13331 /* Consume the `...' token. */
13332 cp_lexer_consume_token (parser->lexer);
13333 maybe_warn_variadic_templates ();
13335 *is_parameter_pack = true;
13338 /* If the next token is an identifier, then it names the
13339 parameter. */
13340 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13341 identifier = cp_parser_identifier (parser);
13342 else
13343 identifier = NULL_TREE;
13345 /* Create the parameter. */
13346 parameter = finish_template_type_parm (class_type_node, identifier);
13348 /* If the next token is an `=', we have a default argument. */
13349 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13351 /* Consume the `=' token. */
13352 cp_lexer_consume_token (parser->lexer);
13353 /* Parse the default-argument. */
13354 push_deferring_access_checks (dk_no_deferred);
13355 default_argument = cp_parser_type_id (parser);
13357 /* Template parameter packs cannot have default
13358 arguments. */
13359 if (*is_parameter_pack)
13361 if (identifier)
13362 error_at (token->location,
13363 "template parameter pack %qD cannot have a "
13364 "default argument", identifier);
13365 else
13366 error_at (token->location,
13367 "template parameter packs cannot have "
13368 "default arguments");
13369 default_argument = NULL_TREE;
13371 pop_deferring_access_checks ();
13373 else
13374 default_argument = NULL_TREE;
13376 /* Create the combined representation of the parameter and the
13377 default argument. */
13378 parameter = build_tree_list (default_argument, parameter);
13380 break;
13382 case RID_TEMPLATE:
13384 tree identifier;
13385 tree default_argument;
13387 /* Look for the `<'. */
13388 cp_parser_require (parser, CPP_LESS, RT_LESS);
13389 /* Parse the template-parameter-list. */
13390 cp_parser_template_parameter_list (parser);
13391 /* Look for the `>'. */
13392 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13393 /* Look for the `class' or 'typename' keywords. */
13394 cp_parser_type_parameter_key (parser);
13395 /* If the next token is an ellipsis, we have a template
13396 argument pack. */
13397 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13399 /* Consume the `...' token. */
13400 cp_lexer_consume_token (parser->lexer);
13401 maybe_warn_variadic_templates ();
13403 *is_parameter_pack = true;
13405 /* If the next token is an `=', then there is a
13406 default-argument. If the next token is a `>', we are at
13407 the end of the parameter-list. If the next token is a `,',
13408 then we are at the end of this parameter. */
13409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13410 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13411 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13413 identifier = cp_parser_identifier (parser);
13414 /* Treat invalid names as if the parameter were nameless. */
13415 if (identifier == error_mark_node)
13416 identifier = NULL_TREE;
13418 else
13419 identifier = NULL_TREE;
13421 /* Create the template parameter. */
13422 parameter = finish_template_template_parm (class_type_node,
13423 identifier);
13425 /* If the next token is an `=', then there is a
13426 default-argument. */
13427 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13429 bool is_template;
13431 /* Consume the `='. */
13432 cp_lexer_consume_token (parser->lexer);
13433 /* Parse the id-expression. */
13434 push_deferring_access_checks (dk_no_deferred);
13435 /* save token before parsing the id-expression, for error
13436 reporting */
13437 token = cp_lexer_peek_token (parser->lexer);
13438 default_argument
13439 = cp_parser_id_expression (parser,
13440 /*template_keyword_p=*/false,
13441 /*check_dependency_p=*/true,
13442 /*template_p=*/&is_template,
13443 /*declarator_p=*/false,
13444 /*optional_p=*/false);
13445 if (TREE_CODE (default_argument) == TYPE_DECL)
13446 /* If the id-expression was a template-id that refers to
13447 a template-class, we already have the declaration here,
13448 so no further lookup is needed. */
13450 else
13451 /* Look up the name. */
13452 default_argument
13453 = cp_parser_lookup_name (parser, default_argument,
13454 none_type,
13455 /*is_template=*/is_template,
13456 /*is_namespace=*/false,
13457 /*check_dependency=*/true,
13458 /*ambiguous_decls=*/NULL,
13459 token->location);
13460 /* See if the default argument is valid. */
13461 default_argument
13462 = check_template_template_default_arg (default_argument);
13464 /* Template parameter packs cannot have default
13465 arguments. */
13466 if (*is_parameter_pack)
13468 if (identifier)
13469 error_at (token->location,
13470 "template parameter pack %qD cannot "
13471 "have a default argument",
13472 identifier);
13473 else
13474 error_at (token->location, "template parameter packs cannot "
13475 "have default arguments");
13476 default_argument = NULL_TREE;
13478 pop_deferring_access_checks ();
13480 else
13481 default_argument = NULL_TREE;
13483 /* Create the combined representation of the parameter and the
13484 default argument. */
13485 parameter = build_tree_list (default_argument, parameter);
13487 break;
13489 default:
13490 gcc_unreachable ();
13491 break;
13494 return parameter;
13497 /* Parse a template-id.
13499 template-id:
13500 template-name < template-argument-list [opt] >
13502 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13503 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13504 returned. Otherwise, if the template-name names a function, or set
13505 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13506 names a class, returns a TYPE_DECL for the specialization.
13508 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13509 uninstantiated templates. */
13511 static tree
13512 cp_parser_template_id (cp_parser *parser,
13513 bool template_keyword_p,
13514 bool check_dependency_p,
13515 enum tag_types tag_type,
13516 bool is_declaration)
13518 int i;
13519 tree templ;
13520 tree arguments;
13521 tree template_id;
13522 cp_token_position start_of_id = 0;
13523 deferred_access_check *chk;
13524 vec<deferred_access_check, va_gc> *access_check;
13525 cp_token *next_token = NULL, *next_token_2 = NULL;
13526 bool is_identifier;
13528 /* If the next token corresponds to a template-id, there is no need
13529 to reparse it. */
13530 next_token = cp_lexer_peek_token (parser->lexer);
13531 if (next_token->type == CPP_TEMPLATE_ID)
13533 struct tree_check *check_value;
13535 /* Get the stored value. */
13536 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13537 /* Perform any access checks that were deferred. */
13538 access_check = check_value->checks;
13539 if (access_check)
13541 FOR_EACH_VEC_ELT (*access_check, i, chk)
13542 perform_or_defer_access_check (chk->binfo,
13543 chk->decl,
13544 chk->diag_decl,
13545 tf_warning_or_error);
13547 /* Return the stored value. */
13548 return check_value->value;
13551 /* Avoid performing name lookup if there is no possibility of
13552 finding a template-id. */
13553 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13554 || (next_token->type == CPP_NAME
13555 && !cp_parser_nth_token_starts_template_argument_list_p
13556 (parser, 2)))
13558 cp_parser_error (parser, "expected template-id");
13559 return error_mark_node;
13562 /* Remember where the template-id starts. */
13563 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13564 start_of_id = cp_lexer_token_position (parser->lexer, false);
13566 push_deferring_access_checks (dk_deferred);
13568 /* Parse the template-name. */
13569 is_identifier = false;
13570 templ = cp_parser_template_name (parser, template_keyword_p,
13571 check_dependency_p,
13572 is_declaration,
13573 tag_type,
13574 &is_identifier);
13575 if (templ == error_mark_node || is_identifier)
13577 pop_deferring_access_checks ();
13578 return templ;
13581 /* If we find the sequence `[:' after a template-name, it's probably
13582 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13583 parse correctly the argument list. */
13584 next_token = cp_lexer_peek_token (parser->lexer);
13585 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13586 if (next_token->type == CPP_OPEN_SQUARE
13587 && next_token->flags & DIGRAPH
13588 && next_token_2->type == CPP_COLON
13589 && !(next_token_2->flags & PREV_WHITE))
13591 cp_parser_parse_tentatively (parser);
13592 /* Change `:' into `::'. */
13593 next_token_2->type = CPP_SCOPE;
13594 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13595 CPP_LESS. */
13596 cp_lexer_consume_token (parser->lexer);
13598 /* Parse the arguments. */
13599 arguments = cp_parser_enclosed_template_argument_list (parser);
13600 if (!cp_parser_parse_definitely (parser))
13602 /* If we couldn't parse an argument list, then we revert our changes
13603 and return simply an error. Maybe this is not a template-id
13604 after all. */
13605 next_token_2->type = CPP_COLON;
13606 cp_parser_error (parser, "expected %<<%>");
13607 pop_deferring_access_checks ();
13608 return error_mark_node;
13610 /* Otherwise, emit an error about the invalid digraph, but continue
13611 parsing because we got our argument list. */
13612 if (permerror (next_token->location,
13613 "%<<::%> cannot begin a template-argument list"))
13615 static bool hint = false;
13616 inform (next_token->location,
13617 "%<<:%> is an alternate spelling for %<[%>."
13618 " Insert whitespace between %<<%> and %<::%>");
13619 if (!hint && !flag_permissive)
13621 inform (next_token->location, "(if you use %<-fpermissive%> "
13622 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13623 "accept your code)");
13624 hint = true;
13628 else
13630 /* Look for the `<' that starts the template-argument-list. */
13631 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13633 pop_deferring_access_checks ();
13634 return error_mark_node;
13636 /* Parse the arguments. */
13637 arguments = cp_parser_enclosed_template_argument_list (parser);
13640 /* Build a representation of the specialization. */
13641 if (identifier_p (templ))
13642 template_id = build_min_nt_loc (next_token->location,
13643 TEMPLATE_ID_EXPR,
13644 templ, arguments);
13645 else if (DECL_TYPE_TEMPLATE_P (templ)
13646 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13648 bool entering_scope;
13649 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13650 template (rather than some instantiation thereof) only if
13651 is not nested within some other construct. For example, in
13652 "template <typename T> void f(T) { A<T>::", A<T> is just an
13653 instantiation of A. */
13654 entering_scope = (template_parm_scope_p ()
13655 && cp_lexer_next_token_is (parser->lexer,
13656 CPP_SCOPE));
13657 template_id
13658 = finish_template_type (templ, arguments, entering_scope);
13660 else if (variable_template_p (templ))
13662 template_id = lookup_template_variable (templ, arguments);
13664 else
13666 /* If it's not a class-template or a template-template, it should be
13667 a function-template. */
13668 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13669 || TREE_CODE (templ) == OVERLOAD
13670 || BASELINK_P (templ)));
13672 template_id = lookup_template_function (templ, arguments);
13675 /* If parsing tentatively, replace the sequence of tokens that makes
13676 up the template-id with a CPP_TEMPLATE_ID token. That way,
13677 should we re-parse the token stream, we will not have to repeat
13678 the effort required to do the parse, nor will we issue duplicate
13679 error messages about problems during instantiation of the
13680 template. */
13681 if (start_of_id
13682 /* Don't do this if we had a parse error in a declarator; re-parsing
13683 might succeed if a name changes meaning (60361). */
13684 && !(cp_parser_error_occurred (parser)
13685 && cp_parser_parsing_tentatively (parser)
13686 && parser->in_declarator_p))
13688 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13690 /* Reset the contents of the START_OF_ID token. */
13691 token->type = CPP_TEMPLATE_ID;
13692 /* Retrieve any deferred checks. Do not pop this access checks yet
13693 so the memory will not be reclaimed during token replacing below. */
13694 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13695 token->u.tree_check_value->value = template_id;
13696 token->u.tree_check_value->checks = get_deferred_access_checks ();
13697 token->keyword = RID_MAX;
13699 /* Purge all subsequent tokens. */
13700 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13702 /* ??? Can we actually assume that, if template_id ==
13703 error_mark_node, we will have issued a diagnostic to the
13704 user, as opposed to simply marking the tentative parse as
13705 failed? */
13706 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13707 error_at (token->location, "parse error in template argument list");
13710 pop_to_parent_deferring_access_checks ();
13711 return template_id;
13714 /* Parse a template-name.
13716 template-name:
13717 identifier
13719 The standard should actually say:
13721 template-name:
13722 identifier
13723 operator-function-id
13725 A defect report has been filed about this issue.
13727 A conversion-function-id cannot be a template name because they cannot
13728 be part of a template-id. In fact, looking at this code:
13730 a.operator K<int>()
13732 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13733 It is impossible to call a templated conversion-function-id with an
13734 explicit argument list, since the only allowed template parameter is
13735 the type to which it is converting.
13737 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13738 `template' keyword, in a construction like:
13740 T::template f<3>()
13742 In that case `f' is taken to be a template-name, even though there
13743 is no way of knowing for sure.
13745 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13746 name refers to a set of overloaded functions, at least one of which
13747 is a template, or an IDENTIFIER_NODE with the name of the template,
13748 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13749 names are looked up inside uninstantiated templates. */
13751 static tree
13752 cp_parser_template_name (cp_parser* parser,
13753 bool template_keyword_p,
13754 bool check_dependency_p,
13755 bool is_declaration,
13756 enum tag_types tag_type,
13757 bool *is_identifier)
13759 tree identifier;
13760 tree decl;
13761 tree fns;
13762 cp_token *token = cp_lexer_peek_token (parser->lexer);
13764 /* If the next token is `operator', then we have either an
13765 operator-function-id or a conversion-function-id. */
13766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13768 /* We don't know whether we're looking at an
13769 operator-function-id or a conversion-function-id. */
13770 cp_parser_parse_tentatively (parser);
13771 /* Try an operator-function-id. */
13772 identifier = cp_parser_operator_function_id (parser);
13773 /* If that didn't work, try a conversion-function-id. */
13774 if (!cp_parser_parse_definitely (parser))
13776 cp_parser_error (parser, "expected template-name");
13777 return error_mark_node;
13780 /* Look for the identifier. */
13781 else
13782 identifier = cp_parser_identifier (parser);
13784 /* If we didn't find an identifier, we don't have a template-id. */
13785 if (identifier == error_mark_node)
13786 return error_mark_node;
13788 /* If the name immediately followed the `template' keyword, then it
13789 is a template-name. However, if the next token is not `<', then
13790 we do not treat it as a template-name, since it is not being used
13791 as part of a template-id. This enables us to handle constructs
13792 like:
13794 template <typename T> struct S { S(); };
13795 template <typename T> S<T>::S();
13797 correctly. We would treat `S' as a template -- if it were `S<T>'
13798 -- but we do not if there is no `<'. */
13800 if (processing_template_decl
13801 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13803 /* In a declaration, in a dependent context, we pretend that the
13804 "template" keyword was present in order to improve error
13805 recovery. For example, given:
13807 template <typename T> void f(T::X<int>);
13809 we want to treat "X<int>" as a template-id. */
13810 if (is_declaration
13811 && !template_keyword_p
13812 && parser->scope && TYPE_P (parser->scope)
13813 && check_dependency_p
13814 && dependent_scope_p (parser->scope)
13815 /* Do not do this for dtors (or ctors), since they never
13816 need the template keyword before their name. */
13817 && !constructor_name_p (identifier, parser->scope))
13819 cp_token_position start = 0;
13821 /* Explain what went wrong. */
13822 error_at (token->location, "non-template %qD used as template",
13823 identifier);
13824 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13825 parser->scope, identifier);
13826 /* If parsing tentatively, find the location of the "<" token. */
13827 if (cp_parser_simulate_error (parser))
13828 start = cp_lexer_token_position (parser->lexer, true);
13829 /* Parse the template arguments so that we can issue error
13830 messages about them. */
13831 cp_lexer_consume_token (parser->lexer);
13832 cp_parser_enclosed_template_argument_list (parser);
13833 /* Skip tokens until we find a good place from which to
13834 continue parsing. */
13835 cp_parser_skip_to_closing_parenthesis (parser,
13836 /*recovering=*/true,
13837 /*or_comma=*/true,
13838 /*consume_paren=*/false);
13839 /* If parsing tentatively, permanently remove the
13840 template argument list. That will prevent duplicate
13841 error messages from being issued about the missing
13842 "template" keyword. */
13843 if (start)
13844 cp_lexer_purge_tokens_after (parser->lexer, start);
13845 if (is_identifier)
13846 *is_identifier = true;
13847 return identifier;
13850 /* If the "template" keyword is present, then there is generally
13851 no point in doing name-lookup, so we just return IDENTIFIER.
13852 But, if the qualifying scope is non-dependent then we can
13853 (and must) do name-lookup normally. */
13854 if (template_keyword_p
13855 && (!parser->scope
13856 || (TYPE_P (parser->scope)
13857 && dependent_type_p (parser->scope))))
13858 return identifier;
13861 /* Look up the name. */
13862 decl = cp_parser_lookup_name (parser, identifier,
13863 tag_type,
13864 /*is_template=*/true,
13865 /*is_namespace=*/false,
13866 check_dependency_p,
13867 /*ambiguous_decls=*/NULL,
13868 token->location);
13870 /* If DECL is a template, then the name was a template-name. */
13871 if (TREE_CODE (decl) == TEMPLATE_DECL)
13873 else
13875 tree fn = NULL_TREE;
13877 /* The standard does not explicitly indicate whether a name that
13878 names a set of overloaded declarations, some of which are
13879 templates, is a template-name. However, such a name should
13880 be a template-name; otherwise, there is no way to form a
13881 template-id for the overloaded templates. */
13882 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13883 if (TREE_CODE (fns) == OVERLOAD)
13884 for (fn = fns; fn; fn = OVL_NEXT (fn))
13885 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13886 break;
13888 if (!fn)
13890 /* The name does not name a template. */
13891 cp_parser_error (parser, "expected template-name");
13892 return error_mark_node;
13896 /* If DECL is dependent, and refers to a function, then just return
13897 its name; we will look it up again during template instantiation. */
13898 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13900 tree scope = ovl_scope (decl);
13901 if (TYPE_P (scope) && dependent_type_p (scope))
13902 return identifier;
13905 return decl;
13908 /* Parse a template-argument-list.
13910 template-argument-list:
13911 template-argument ... [opt]
13912 template-argument-list , template-argument ... [opt]
13914 Returns a TREE_VEC containing the arguments. */
13916 static tree
13917 cp_parser_template_argument_list (cp_parser* parser)
13919 tree fixed_args[10];
13920 unsigned n_args = 0;
13921 unsigned alloced = 10;
13922 tree *arg_ary = fixed_args;
13923 tree vec;
13924 bool saved_in_template_argument_list_p;
13925 bool saved_ice_p;
13926 bool saved_non_ice_p;
13928 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13929 parser->in_template_argument_list_p = true;
13930 /* Even if the template-id appears in an integral
13931 constant-expression, the contents of the argument list do
13932 not. */
13933 saved_ice_p = parser->integral_constant_expression_p;
13934 parser->integral_constant_expression_p = false;
13935 saved_non_ice_p = parser->non_integral_constant_expression_p;
13936 parser->non_integral_constant_expression_p = false;
13938 /* Parse the arguments. */
13941 tree argument;
13943 if (n_args)
13944 /* Consume the comma. */
13945 cp_lexer_consume_token (parser->lexer);
13947 /* Parse the template-argument. */
13948 argument = cp_parser_template_argument (parser);
13950 /* If the next token is an ellipsis, we're expanding a template
13951 argument pack. */
13952 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13954 if (argument == error_mark_node)
13956 cp_token *token = cp_lexer_peek_token (parser->lexer);
13957 error_at (token->location,
13958 "expected parameter pack before %<...%>");
13960 /* Consume the `...' token. */
13961 cp_lexer_consume_token (parser->lexer);
13963 /* Make the argument into a TYPE_PACK_EXPANSION or
13964 EXPR_PACK_EXPANSION. */
13965 argument = make_pack_expansion (argument);
13968 if (n_args == alloced)
13970 alloced *= 2;
13972 if (arg_ary == fixed_args)
13974 arg_ary = XNEWVEC (tree, alloced);
13975 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13977 else
13978 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13980 arg_ary[n_args++] = argument;
13982 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13984 vec = make_tree_vec (n_args);
13986 while (n_args--)
13987 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13989 if (arg_ary != fixed_args)
13990 free (arg_ary);
13991 parser->non_integral_constant_expression_p = saved_non_ice_p;
13992 parser->integral_constant_expression_p = saved_ice_p;
13993 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13994 #ifdef ENABLE_CHECKING
13995 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13996 #endif
13997 return vec;
14000 /* Parse a template-argument.
14002 template-argument:
14003 assignment-expression
14004 type-id
14005 id-expression
14007 The representation is that of an assignment-expression, type-id, or
14008 id-expression -- except that the qualified id-expression is
14009 evaluated, so that the value returned is either a DECL or an
14010 OVERLOAD.
14012 Although the standard says "assignment-expression", it forbids
14013 throw-expressions or assignments in the template argument.
14014 Therefore, we use "conditional-expression" instead. */
14016 static tree
14017 cp_parser_template_argument (cp_parser* parser)
14019 tree argument;
14020 bool template_p;
14021 bool address_p;
14022 bool maybe_type_id = false;
14023 cp_token *token = NULL, *argument_start_token = NULL;
14024 location_t loc = 0;
14025 cp_id_kind idk;
14027 /* There's really no way to know what we're looking at, so we just
14028 try each alternative in order.
14030 [temp.arg]
14032 In a template-argument, an ambiguity between a type-id and an
14033 expression is resolved to a type-id, regardless of the form of
14034 the corresponding template-parameter.
14036 Therefore, we try a type-id first. */
14037 cp_parser_parse_tentatively (parser);
14038 argument = cp_parser_template_type_arg (parser);
14039 /* If there was no error parsing the type-id but the next token is a
14040 '>>', our behavior depends on which dialect of C++ we're
14041 parsing. In C++98, we probably found a typo for '> >'. But there
14042 are type-id which are also valid expressions. For instance:
14044 struct X { int operator >> (int); };
14045 template <int V> struct Foo {};
14046 Foo<X () >> 5> r;
14048 Here 'X()' is a valid type-id of a function type, but the user just
14049 wanted to write the expression "X() >> 5". Thus, we remember that we
14050 found a valid type-id, but we still try to parse the argument as an
14051 expression to see what happens.
14053 In C++0x, the '>>' will be considered two separate '>'
14054 tokens. */
14055 if (!cp_parser_error_occurred (parser)
14056 && cxx_dialect == cxx98
14057 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14059 maybe_type_id = true;
14060 cp_parser_abort_tentative_parse (parser);
14062 else
14064 /* If the next token isn't a `,' or a `>', then this argument wasn't
14065 really finished. This means that the argument is not a valid
14066 type-id. */
14067 if (!cp_parser_next_token_ends_template_argument_p (parser))
14068 cp_parser_error (parser, "expected template-argument");
14069 /* If that worked, we're done. */
14070 if (cp_parser_parse_definitely (parser))
14071 return argument;
14073 /* We're still not sure what the argument will be. */
14074 cp_parser_parse_tentatively (parser);
14075 /* Try a template. */
14076 argument_start_token = cp_lexer_peek_token (parser->lexer);
14077 argument = cp_parser_id_expression (parser,
14078 /*template_keyword_p=*/false,
14079 /*check_dependency_p=*/true,
14080 &template_p,
14081 /*declarator_p=*/false,
14082 /*optional_p=*/false);
14083 /* If the next token isn't a `,' or a `>', then this argument wasn't
14084 really finished. */
14085 if (!cp_parser_next_token_ends_template_argument_p (parser))
14086 cp_parser_error (parser, "expected template-argument");
14087 if (!cp_parser_error_occurred (parser))
14089 /* Figure out what is being referred to. If the id-expression
14090 was for a class template specialization, then we will have a
14091 TYPE_DECL at this point. There is no need to do name lookup
14092 at this point in that case. */
14093 if (TREE_CODE (argument) != TYPE_DECL)
14094 argument = cp_parser_lookup_name (parser, argument,
14095 none_type,
14096 /*is_template=*/template_p,
14097 /*is_namespace=*/false,
14098 /*check_dependency=*/true,
14099 /*ambiguous_decls=*/NULL,
14100 argument_start_token->location);
14101 if (TREE_CODE (argument) != TEMPLATE_DECL
14102 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14103 cp_parser_error (parser, "expected template-name");
14105 if (cp_parser_parse_definitely (parser))
14106 return argument;
14107 /* It must be a non-type argument. There permitted cases are given
14108 in [temp.arg.nontype]:
14110 -- an integral constant-expression of integral or enumeration
14111 type; or
14113 -- the name of a non-type template-parameter; or
14115 -- the name of an object or function with external linkage...
14117 -- the address of an object or function with external linkage...
14119 -- a pointer to member... */
14120 /* Look for a non-type template parameter. */
14121 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14123 cp_parser_parse_tentatively (parser);
14124 argument = cp_parser_primary_expression (parser,
14125 /*address_p=*/false,
14126 /*cast_p=*/false,
14127 /*template_arg_p=*/true,
14128 &idk);
14129 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14130 || !cp_parser_next_token_ends_template_argument_p (parser))
14131 cp_parser_simulate_error (parser);
14132 if (cp_parser_parse_definitely (parser))
14133 return argument;
14136 /* If the next token is "&", the argument must be the address of an
14137 object or function with external linkage. */
14138 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14139 if (address_p)
14141 loc = cp_lexer_peek_token (parser->lexer)->location;
14142 cp_lexer_consume_token (parser->lexer);
14144 /* See if we might have an id-expression. */
14145 token = cp_lexer_peek_token (parser->lexer);
14146 if (token->type == CPP_NAME
14147 || token->keyword == RID_OPERATOR
14148 || token->type == CPP_SCOPE
14149 || token->type == CPP_TEMPLATE_ID
14150 || token->type == CPP_NESTED_NAME_SPECIFIER)
14152 cp_parser_parse_tentatively (parser);
14153 argument = cp_parser_primary_expression (parser,
14154 address_p,
14155 /*cast_p=*/false,
14156 /*template_arg_p=*/true,
14157 &idk);
14158 if (cp_parser_error_occurred (parser)
14159 || !cp_parser_next_token_ends_template_argument_p (parser))
14160 cp_parser_abort_tentative_parse (parser);
14161 else
14163 tree probe;
14165 if (INDIRECT_REF_P (argument))
14167 /* Strip the dereference temporarily. */
14168 gcc_assert (REFERENCE_REF_P (argument));
14169 argument = TREE_OPERAND (argument, 0);
14172 /* If we're in a template, we represent a qualified-id referring
14173 to a static data member as a SCOPE_REF even if the scope isn't
14174 dependent so that we can check access control later. */
14175 probe = argument;
14176 if (TREE_CODE (probe) == SCOPE_REF)
14177 probe = TREE_OPERAND (probe, 1);
14178 if (VAR_P (probe))
14180 /* A variable without external linkage might still be a
14181 valid constant-expression, so no error is issued here
14182 if the external-linkage check fails. */
14183 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14184 cp_parser_simulate_error (parser);
14186 else if (is_overloaded_fn (argument))
14187 /* All overloaded functions are allowed; if the external
14188 linkage test does not pass, an error will be issued
14189 later. */
14191 else if (address_p
14192 && (TREE_CODE (argument) == OFFSET_REF
14193 || TREE_CODE (argument) == SCOPE_REF))
14194 /* A pointer-to-member. */
14196 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14198 else
14199 cp_parser_simulate_error (parser);
14201 if (cp_parser_parse_definitely (parser))
14203 if (address_p)
14204 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14205 tf_warning_or_error);
14206 else
14207 argument = convert_from_reference (argument);
14208 return argument;
14212 /* If the argument started with "&", there are no other valid
14213 alternatives at this point. */
14214 if (address_p)
14216 cp_parser_error (parser, "invalid non-type template argument");
14217 return error_mark_node;
14220 /* If the argument wasn't successfully parsed as a type-id followed
14221 by '>>', the argument can only be a constant expression now.
14222 Otherwise, we try parsing the constant-expression tentatively,
14223 because the argument could really be a type-id. */
14224 if (maybe_type_id)
14225 cp_parser_parse_tentatively (parser);
14226 argument = cp_parser_constant_expression (parser);
14228 if (!maybe_type_id)
14229 return argument;
14230 if (!cp_parser_next_token_ends_template_argument_p (parser))
14231 cp_parser_error (parser, "expected template-argument");
14232 if (cp_parser_parse_definitely (parser))
14233 return argument;
14234 /* We did our best to parse the argument as a non type-id, but that
14235 was the only alternative that matched (albeit with a '>' after
14236 it). We can assume it's just a typo from the user, and a
14237 diagnostic will then be issued. */
14238 return cp_parser_template_type_arg (parser);
14241 /* Parse an explicit-instantiation.
14243 explicit-instantiation:
14244 template declaration
14246 Although the standard says `declaration', what it really means is:
14248 explicit-instantiation:
14249 template decl-specifier-seq [opt] declarator [opt] ;
14251 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14252 supposed to be allowed. A defect report has been filed about this
14253 issue.
14255 GNU Extension:
14257 explicit-instantiation:
14258 storage-class-specifier template
14259 decl-specifier-seq [opt] declarator [opt] ;
14260 function-specifier template
14261 decl-specifier-seq [opt] declarator [opt] ; */
14263 static void
14264 cp_parser_explicit_instantiation (cp_parser* parser)
14266 int declares_class_or_enum;
14267 cp_decl_specifier_seq decl_specifiers;
14268 tree extension_specifier = NULL_TREE;
14270 timevar_push (TV_TEMPLATE_INST);
14272 /* Look for an (optional) storage-class-specifier or
14273 function-specifier. */
14274 if (cp_parser_allow_gnu_extensions_p (parser))
14276 extension_specifier
14277 = cp_parser_storage_class_specifier_opt (parser);
14278 if (!extension_specifier)
14279 extension_specifier
14280 = cp_parser_function_specifier_opt (parser,
14281 /*decl_specs=*/NULL);
14284 /* Look for the `template' keyword. */
14285 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14286 /* Let the front end know that we are processing an explicit
14287 instantiation. */
14288 begin_explicit_instantiation ();
14289 /* [temp.explicit] says that we are supposed to ignore access
14290 control while processing explicit instantiation directives. */
14291 push_deferring_access_checks (dk_no_check);
14292 /* Parse a decl-specifier-seq. */
14293 cp_parser_decl_specifier_seq (parser,
14294 CP_PARSER_FLAGS_OPTIONAL,
14295 &decl_specifiers,
14296 &declares_class_or_enum);
14297 /* If there was exactly one decl-specifier, and it declared a class,
14298 and there's no declarator, then we have an explicit type
14299 instantiation. */
14300 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14302 tree type;
14304 type = check_tag_decl (&decl_specifiers,
14305 /*explicit_type_instantiation_p=*/true);
14306 /* Turn access control back on for names used during
14307 template instantiation. */
14308 pop_deferring_access_checks ();
14309 if (type)
14310 do_type_instantiation (type, extension_specifier,
14311 /*complain=*/tf_error);
14313 else
14315 cp_declarator *declarator;
14316 tree decl;
14318 /* Parse the declarator. */
14319 declarator
14320 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14321 /*ctor_dtor_or_conv_p=*/NULL,
14322 /*parenthesized_p=*/NULL,
14323 /*member_p=*/false,
14324 /*friend_p=*/false);
14325 if (declares_class_or_enum & 2)
14326 cp_parser_check_for_definition_in_return_type (declarator,
14327 decl_specifiers.type,
14328 decl_specifiers.locations[ds_type_spec]);
14329 if (declarator != cp_error_declarator)
14331 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14332 permerror (decl_specifiers.locations[ds_inline],
14333 "explicit instantiation shall not use"
14334 " %<inline%> specifier");
14335 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14336 permerror (decl_specifiers.locations[ds_constexpr],
14337 "explicit instantiation shall not use"
14338 " %<constexpr%> specifier");
14340 decl = grokdeclarator (declarator, &decl_specifiers,
14341 NORMAL, 0, &decl_specifiers.attributes);
14342 /* Turn access control back on for names used during
14343 template instantiation. */
14344 pop_deferring_access_checks ();
14345 /* Do the explicit instantiation. */
14346 do_decl_instantiation (decl, extension_specifier);
14348 else
14350 pop_deferring_access_checks ();
14351 /* Skip the body of the explicit instantiation. */
14352 cp_parser_skip_to_end_of_statement (parser);
14355 /* We're done with the instantiation. */
14356 end_explicit_instantiation ();
14358 cp_parser_consume_semicolon_at_end_of_statement (parser);
14360 timevar_pop (TV_TEMPLATE_INST);
14363 /* Parse an explicit-specialization.
14365 explicit-specialization:
14366 template < > declaration
14368 Although the standard says `declaration', what it really means is:
14370 explicit-specialization:
14371 template <> decl-specifier [opt] init-declarator [opt] ;
14372 template <> function-definition
14373 template <> explicit-specialization
14374 template <> template-declaration */
14376 static void
14377 cp_parser_explicit_specialization (cp_parser* parser)
14379 bool need_lang_pop;
14380 cp_token *token = cp_lexer_peek_token (parser->lexer);
14382 /* Look for the `template' keyword. */
14383 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14384 /* Look for the `<'. */
14385 cp_parser_require (parser, CPP_LESS, RT_LESS);
14386 /* Look for the `>'. */
14387 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14388 /* We have processed another parameter list. */
14389 ++parser->num_template_parameter_lists;
14390 /* [temp]
14392 A template ... explicit specialization ... shall not have C
14393 linkage. */
14394 if (current_lang_name == lang_name_c)
14396 error_at (token->location, "template specialization with C linkage");
14397 /* Give it C++ linkage to avoid confusing other parts of the
14398 front end. */
14399 push_lang_context (lang_name_cplusplus);
14400 need_lang_pop = true;
14402 else
14403 need_lang_pop = false;
14404 /* Let the front end know that we are beginning a specialization. */
14405 if (!begin_specialization ())
14407 end_specialization ();
14408 return;
14411 /* If the next keyword is `template', we need to figure out whether
14412 or not we're looking a template-declaration. */
14413 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14415 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14416 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14417 cp_parser_template_declaration_after_export (parser,
14418 /*member_p=*/false);
14419 else
14420 cp_parser_explicit_specialization (parser);
14422 else
14423 /* Parse the dependent declaration. */
14424 cp_parser_single_declaration (parser,
14425 /*checks=*/NULL,
14426 /*member_p=*/false,
14427 /*explicit_specialization_p=*/true,
14428 /*friend_p=*/NULL);
14429 /* We're done with the specialization. */
14430 end_specialization ();
14431 /* For the erroneous case of a template with C linkage, we pushed an
14432 implicit C++ linkage scope; exit that scope now. */
14433 if (need_lang_pop)
14434 pop_lang_context ();
14435 /* We're done with this parameter list. */
14436 --parser->num_template_parameter_lists;
14439 /* Parse a type-specifier.
14441 type-specifier:
14442 simple-type-specifier
14443 class-specifier
14444 enum-specifier
14445 elaborated-type-specifier
14446 cv-qualifier
14448 GNU Extension:
14450 type-specifier:
14451 __complex__
14453 Returns a representation of the type-specifier. For a
14454 class-specifier, enum-specifier, or elaborated-type-specifier, a
14455 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14457 The parser flags FLAGS is used to control type-specifier parsing.
14459 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14460 in a decl-specifier-seq.
14462 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14463 class-specifier, enum-specifier, or elaborated-type-specifier, then
14464 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14465 if a type is declared; 2 if it is defined. Otherwise, it is set to
14466 zero.
14468 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14469 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14470 is set to FALSE. */
14472 static tree
14473 cp_parser_type_specifier (cp_parser* parser,
14474 cp_parser_flags flags,
14475 cp_decl_specifier_seq *decl_specs,
14476 bool is_declaration,
14477 int* declares_class_or_enum,
14478 bool* is_cv_qualifier)
14480 tree type_spec = NULL_TREE;
14481 cp_token *token;
14482 enum rid keyword;
14483 cp_decl_spec ds = ds_last;
14485 /* Assume this type-specifier does not declare a new type. */
14486 if (declares_class_or_enum)
14487 *declares_class_or_enum = 0;
14488 /* And that it does not specify a cv-qualifier. */
14489 if (is_cv_qualifier)
14490 *is_cv_qualifier = false;
14491 /* Peek at the next token. */
14492 token = cp_lexer_peek_token (parser->lexer);
14494 /* If we're looking at a keyword, we can use that to guide the
14495 production we choose. */
14496 keyword = token->keyword;
14497 switch (keyword)
14499 case RID_ENUM:
14500 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14501 goto elaborated_type_specifier;
14503 /* Look for the enum-specifier. */
14504 type_spec = cp_parser_enum_specifier (parser);
14505 /* If that worked, we're done. */
14506 if (type_spec)
14508 if (declares_class_or_enum)
14509 *declares_class_or_enum = 2;
14510 if (decl_specs)
14511 cp_parser_set_decl_spec_type (decl_specs,
14512 type_spec,
14513 token,
14514 /*type_definition_p=*/true);
14515 return type_spec;
14517 else
14518 goto elaborated_type_specifier;
14520 /* Any of these indicate either a class-specifier, or an
14521 elaborated-type-specifier. */
14522 case RID_CLASS:
14523 case RID_STRUCT:
14524 case RID_UNION:
14525 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14526 goto elaborated_type_specifier;
14528 /* Parse tentatively so that we can back up if we don't find a
14529 class-specifier. */
14530 cp_parser_parse_tentatively (parser);
14531 /* Look for the class-specifier. */
14532 type_spec = cp_parser_class_specifier (parser);
14533 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14534 /* If that worked, we're done. */
14535 if (cp_parser_parse_definitely (parser))
14537 if (declares_class_or_enum)
14538 *declares_class_or_enum = 2;
14539 if (decl_specs)
14540 cp_parser_set_decl_spec_type (decl_specs,
14541 type_spec,
14542 token,
14543 /*type_definition_p=*/true);
14544 return type_spec;
14547 /* Fall through. */
14548 elaborated_type_specifier:
14549 /* We're declaring (not defining) a class or enum. */
14550 if (declares_class_or_enum)
14551 *declares_class_or_enum = 1;
14553 /* Fall through. */
14554 case RID_TYPENAME:
14555 /* Look for an elaborated-type-specifier. */
14556 type_spec
14557 = (cp_parser_elaborated_type_specifier
14558 (parser,
14559 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14560 is_declaration));
14561 if (decl_specs)
14562 cp_parser_set_decl_spec_type (decl_specs,
14563 type_spec,
14564 token,
14565 /*type_definition_p=*/false);
14566 return type_spec;
14568 case RID_CONST:
14569 ds = ds_const;
14570 if (is_cv_qualifier)
14571 *is_cv_qualifier = true;
14572 break;
14574 case RID_VOLATILE:
14575 ds = ds_volatile;
14576 if (is_cv_qualifier)
14577 *is_cv_qualifier = true;
14578 break;
14580 case RID_RESTRICT:
14581 ds = ds_restrict;
14582 if (is_cv_qualifier)
14583 *is_cv_qualifier = true;
14584 break;
14586 case RID_COMPLEX:
14587 /* The `__complex__' keyword is a GNU extension. */
14588 ds = ds_complex;
14589 break;
14591 default:
14592 break;
14595 /* Handle simple keywords. */
14596 if (ds != ds_last)
14598 if (decl_specs)
14600 set_and_check_decl_spec_loc (decl_specs, ds, token);
14601 decl_specs->any_specifiers_p = true;
14603 return cp_lexer_consume_token (parser->lexer)->u.value;
14606 /* If we do not already have a type-specifier, assume we are looking
14607 at a simple-type-specifier. */
14608 type_spec = cp_parser_simple_type_specifier (parser,
14609 decl_specs,
14610 flags);
14612 /* If we didn't find a type-specifier, and a type-specifier was not
14613 optional in this context, issue an error message. */
14614 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14616 cp_parser_error (parser, "expected type specifier");
14617 return error_mark_node;
14620 return type_spec;
14623 /* Parse a simple-type-specifier.
14625 simple-type-specifier:
14626 :: [opt] nested-name-specifier [opt] type-name
14627 :: [opt] nested-name-specifier template template-id
14628 char
14629 wchar_t
14630 bool
14631 short
14633 long
14634 signed
14635 unsigned
14636 float
14637 double
14638 void
14640 C++0x Extension:
14642 simple-type-specifier:
14643 auto
14644 decltype ( expression )
14645 char16_t
14646 char32_t
14647 __underlying_type ( type-id )
14649 GNU Extension:
14651 simple-type-specifier:
14652 __int128
14653 __typeof__ unary-expression
14654 __typeof__ ( type-id )
14655 __typeof__ ( type-id ) { initializer-list , [opt] }
14657 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14658 appropriately updated. */
14660 static tree
14661 cp_parser_simple_type_specifier (cp_parser* parser,
14662 cp_decl_specifier_seq *decl_specs,
14663 cp_parser_flags flags)
14665 tree type = NULL_TREE;
14666 cp_token *token;
14668 /* Peek at the next token. */
14669 token = cp_lexer_peek_token (parser->lexer);
14671 /* If we're looking at a keyword, things are easy. */
14672 switch (token->keyword)
14674 case RID_CHAR:
14675 if (decl_specs)
14676 decl_specs->explicit_char_p = true;
14677 type = char_type_node;
14678 break;
14679 case RID_CHAR16:
14680 type = char16_type_node;
14681 break;
14682 case RID_CHAR32:
14683 type = char32_type_node;
14684 break;
14685 case RID_WCHAR:
14686 type = wchar_type_node;
14687 break;
14688 case RID_BOOL:
14689 type = boolean_type_node;
14690 break;
14691 case RID_SHORT:
14692 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14693 type = short_integer_type_node;
14694 break;
14695 case RID_INT:
14696 if (decl_specs)
14697 decl_specs->explicit_int_p = true;
14698 type = integer_type_node;
14699 break;
14700 case RID_INT128:
14701 if (!int128_integer_type_node)
14702 break;
14703 if (decl_specs)
14704 decl_specs->explicit_int128_p = true;
14705 type = int128_integer_type_node;
14706 break;
14707 case RID_LONG:
14708 if (decl_specs)
14709 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14710 type = long_integer_type_node;
14711 break;
14712 case RID_SIGNED:
14713 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14714 type = integer_type_node;
14715 break;
14716 case RID_UNSIGNED:
14717 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14718 type = unsigned_type_node;
14719 break;
14720 case RID_FLOAT:
14721 type = float_type_node;
14722 break;
14723 case RID_DOUBLE:
14724 type = double_type_node;
14725 break;
14726 case RID_VOID:
14727 type = void_type_node;
14728 break;
14730 case RID_AUTO:
14731 maybe_warn_cpp0x (CPP0X_AUTO);
14732 if (parser->auto_is_implicit_function_template_parm_p)
14734 type = synthesize_implicit_template_parm (parser);
14736 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14738 if (cxx_dialect < cxx14)
14739 pedwarn (location_of (type), 0,
14740 "use of %<auto%> in lambda parameter declaration "
14741 "only available with "
14742 "-std=c++14 or -std=gnu++14");
14744 else if (cxx_dialect < cxx14)
14745 pedwarn (location_of (type), 0,
14746 "use of %<auto%> in parameter declaration "
14747 "only available with "
14748 "-std=c++14 or -std=gnu++14");
14749 else
14750 pedwarn (location_of (type), OPT_Wpedantic,
14751 "ISO C++ forbids use of %<auto%> in parameter "
14752 "declaration");
14754 else
14755 type = make_auto ();
14756 break;
14758 case RID_DECLTYPE:
14759 /* Since DR 743, decltype can either be a simple-type-specifier by
14760 itself or begin a nested-name-specifier. Parsing it will replace
14761 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14762 handling below decide what to do. */
14763 cp_parser_decltype (parser);
14764 cp_lexer_set_token_position (parser->lexer, token);
14765 break;
14767 case RID_TYPEOF:
14768 /* Consume the `typeof' token. */
14769 cp_lexer_consume_token (parser->lexer);
14770 /* Parse the operand to `typeof'. */
14771 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14772 /* If it is not already a TYPE, take its type. */
14773 if (!TYPE_P (type))
14774 type = finish_typeof (type);
14776 if (decl_specs)
14777 cp_parser_set_decl_spec_type (decl_specs, type,
14778 token,
14779 /*type_definition_p=*/false);
14781 return type;
14783 case RID_UNDERLYING_TYPE:
14784 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14785 if (decl_specs)
14786 cp_parser_set_decl_spec_type (decl_specs, type,
14787 token,
14788 /*type_definition_p=*/false);
14790 return type;
14792 case RID_BASES:
14793 case RID_DIRECT_BASES:
14794 type = cp_parser_trait_expr (parser, token->keyword);
14795 if (decl_specs)
14796 cp_parser_set_decl_spec_type (decl_specs, type,
14797 token,
14798 /*type_definition_p=*/false);
14799 return type;
14800 default:
14801 break;
14804 /* If token is an already-parsed decltype not followed by ::,
14805 it's a simple-type-specifier. */
14806 if (token->type == CPP_DECLTYPE
14807 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14809 type = token->u.value;
14810 if (decl_specs)
14811 cp_parser_set_decl_spec_type (decl_specs, type,
14812 token,
14813 /*type_definition_p=*/false);
14814 cp_lexer_consume_token (parser->lexer);
14815 return type;
14818 /* If the type-specifier was for a built-in type, we're done. */
14819 if (type)
14821 /* Record the type. */
14822 if (decl_specs
14823 && (token->keyword != RID_SIGNED
14824 && token->keyword != RID_UNSIGNED
14825 && token->keyword != RID_SHORT
14826 && token->keyword != RID_LONG))
14827 cp_parser_set_decl_spec_type (decl_specs,
14828 type,
14829 token,
14830 /*type_definition_p=*/false);
14831 if (decl_specs)
14832 decl_specs->any_specifiers_p = true;
14834 /* Consume the token. */
14835 cp_lexer_consume_token (parser->lexer);
14837 /* There is no valid C++ program where a non-template type is
14838 followed by a "<". That usually indicates that the user thought
14839 that the type was a template. */
14840 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14841 token->location);
14843 return TYPE_NAME (type);
14846 /* The type-specifier must be a user-defined type. */
14847 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14849 bool qualified_p;
14850 bool global_p;
14852 /* Don't gobble tokens or issue error messages if this is an
14853 optional type-specifier. */
14854 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14855 cp_parser_parse_tentatively (parser);
14857 /* Look for the optional `::' operator. */
14858 global_p
14859 = (cp_parser_global_scope_opt (parser,
14860 /*current_scope_valid_p=*/false)
14861 != NULL_TREE);
14862 /* Look for the nested-name specifier. */
14863 qualified_p
14864 = (cp_parser_nested_name_specifier_opt (parser,
14865 /*typename_keyword_p=*/false,
14866 /*check_dependency_p=*/true,
14867 /*type_p=*/false,
14868 /*is_declaration=*/false)
14869 != NULL_TREE);
14870 token = cp_lexer_peek_token (parser->lexer);
14871 /* If we have seen a nested-name-specifier, and the next token
14872 is `template', then we are using the template-id production. */
14873 if (parser->scope
14874 && cp_parser_optional_template_keyword (parser))
14876 /* Look for the template-id. */
14877 type = cp_parser_template_id (parser,
14878 /*template_keyword_p=*/true,
14879 /*check_dependency_p=*/true,
14880 none_type,
14881 /*is_declaration=*/false);
14882 /* If the template-id did not name a type, we are out of
14883 luck. */
14884 if (TREE_CODE (type) != TYPE_DECL)
14886 cp_parser_error (parser, "expected template-id for type");
14887 type = NULL_TREE;
14890 /* Otherwise, look for a type-name. */
14891 else
14892 type = cp_parser_type_name (parser);
14893 /* Keep track of all name-lookups performed in class scopes. */
14894 if (type
14895 && !global_p
14896 && !qualified_p
14897 && TREE_CODE (type) == TYPE_DECL
14898 && identifier_p (DECL_NAME (type)))
14899 maybe_note_name_used_in_class (DECL_NAME (type), type);
14900 /* If it didn't work out, we don't have a TYPE. */
14901 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14902 && !cp_parser_parse_definitely (parser))
14903 type = NULL_TREE;
14904 if (type && decl_specs)
14905 cp_parser_set_decl_spec_type (decl_specs, type,
14906 token,
14907 /*type_definition_p=*/false);
14910 /* If we didn't get a type-name, issue an error message. */
14911 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14913 cp_parser_error (parser, "expected type-name");
14914 return error_mark_node;
14917 if (type && type != error_mark_node)
14919 /* See if TYPE is an Objective-C type, and if so, parse and
14920 accept any protocol references following it. Do this before
14921 the cp_parser_check_for_invalid_template_id() call, because
14922 Objective-C types can be followed by '<...>' which would
14923 enclose protocol names rather than template arguments, and so
14924 everything is fine. */
14925 if (c_dialect_objc () && !parser->scope
14926 && (objc_is_id (type) || objc_is_class_name (type)))
14928 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14929 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14931 /* Clobber the "unqualified" type previously entered into
14932 DECL_SPECS with the new, improved protocol-qualified version. */
14933 if (decl_specs)
14934 decl_specs->type = qual_type;
14936 return qual_type;
14939 /* There is no valid C++ program where a non-template type is
14940 followed by a "<". That usually indicates that the user
14941 thought that the type was a template. */
14942 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14943 none_type,
14944 token->location);
14947 return type;
14950 /* Parse a type-name.
14952 type-name:
14953 class-name
14954 enum-name
14955 typedef-name
14956 simple-template-id [in c++0x]
14958 enum-name:
14959 identifier
14961 typedef-name:
14962 identifier
14964 Returns a TYPE_DECL for the type. */
14966 static tree
14967 cp_parser_type_name (cp_parser* parser)
14969 tree type_decl;
14971 /* We can't know yet whether it is a class-name or not. */
14972 cp_parser_parse_tentatively (parser);
14973 /* Try a class-name. */
14974 type_decl = cp_parser_class_name (parser,
14975 /*typename_keyword_p=*/false,
14976 /*template_keyword_p=*/false,
14977 none_type,
14978 /*check_dependency_p=*/true,
14979 /*class_head_p=*/false,
14980 /*is_declaration=*/false);
14981 /* If it's not a class-name, keep looking. */
14982 if (!cp_parser_parse_definitely (parser))
14984 if (cxx_dialect < cxx11)
14985 /* It must be a typedef-name or an enum-name. */
14986 return cp_parser_nonclass_name (parser);
14988 cp_parser_parse_tentatively (parser);
14989 /* It is either a simple-template-id representing an
14990 instantiation of an alias template... */
14991 type_decl = cp_parser_template_id (parser,
14992 /*template_keyword_p=*/false,
14993 /*check_dependency_p=*/true,
14994 none_type,
14995 /*is_declaration=*/false);
14996 /* Note that this must be an instantiation of an alias template
14997 because [temp.names]/6 says:
14999 A template-id that names an alias template specialization
15000 is a type-name.
15002 Whereas [temp.names]/7 says:
15004 A simple-template-id that names a class template
15005 specialization is a class-name. */
15006 if (type_decl != NULL_TREE
15007 && TREE_CODE (type_decl) == TYPE_DECL
15008 && TYPE_DECL_ALIAS_P (type_decl))
15009 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15010 else
15011 cp_parser_simulate_error (parser);
15013 if (!cp_parser_parse_definitely (parser))
15014 /* ... Or a typedef-name or an enum-name. */
15015 return cp_parser_nonclass_name (parser);
15018 return type_decl;
15021 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15023 enum-name:
15024 identifier
15026 typedef-name:
15027 identifier
15029 Returns a TYPE_DECL for the type. */
15031 static tree
15032 cp_parser_nonclass_name (cp_parser* parser)
15034 tree type_decl;
15035 tree identifier;
15037 cp_token *token = cp_lexer_peek_token (parser->lexer);
15038 identifier = cp_parser_identifier (parser);
15039 if (identifier == error_mark_node)
15040 return error_mark_node;
15042 /* Look up the type-name. */
15043 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15045 type_decl = strip_using_decl (type_decl);
15047 if (TREE_CODE (type_decl) != TYPE_DECL
15048 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15050 /* See if this is an Objective-C type. */
15051 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15052 tree type = objc_get_protocol_qualified_type (identifier, protos);
15053 if (type)
15054 type_decl = TYPE_NAME (type);
15057 /* Issue an error if we did not find a type-name. */
15058 if (TREE_CODE (type_decl) != TYPE_DECL
15059 /* In Objective-C, we have the complication that class names are
15060 normally type names and start declarations (eg, the
15061 "NSObject" in "NSObject *object;"), but can be used in an
15062 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15063 is an expression. So, a classname followed by a dot is not a
15064 valid type-name. */
15065 || (objc_is_class_name (TREE_TYPE (type_decl))
15066 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15068 if (!cp_parser_simulate_error (parser))
15069 cp_parser_name_lookup_error (parser, identifier, type_decl,
15070 NLE_TYPE, token->location);
15071 return error_mark_node;
15073 /* Remember that the name was used in the definition of the
15074 current class so that we can check later to see if the
15075 meaning would have been different after the class was
15076 entirely defined. */
15077 else if (type_decl != error_mark_node
15078 && !parser->scope)
15079 maybe_note_name_used_in_class (identifier, type_decl);
15081 return type_decl;
15084 /* Parse an elaborated-type-specifier. Note that the grammar given
15085 here incorporates the resolution to DR68.
15087 elaborated-type-specifier:
15088 class-key :: [opt] nested-name-specifier [opt] identifier
15089 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15090 enum-key :: [opt] nested-name-specifier [opt] identifier
15091 typename :: [opt] nested-name-specifier identifier
15092 typename :: [opt] nested-name-specifier template [opt]
15093 template-id
15095 GNU extension:
15097 elaborated-type-specifier:
15098 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15099 class-key attributes :: [opt] nested-name-specifier [opt]
15100 template [opt] template-id
15101 enum attributes :: [opt] nested-name-specifier [opt] identifier
15103 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15104 declared `friend'. If IS_DECLARATION is TRUE, then this
15105 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15106 something is being declared.
15108 Returns the TYPE specified. */
15110 static tree
15111 cp_parser_elaborated_type_specifier (cp_parser* parser,
15112 bool is_friend,
15113 bool is_declaration)
15115 enum tag_types tag_type;
15116 tree identifier;
15117 tree type = NULL_TREE;
15118 tree attributes = NULL_TREE;
15119 tree globalscope;
15120 cp_token *token = NULL;
15122 /* See if we're looking at the `enum' keyword. */
15123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15125 /* Consume the `enum' token. */
15126 cp_lexer_consume_token (parser->lexer);
15127 /* Remember that it's an enumeration type. */
15128 tag_type = enum_type;
15129 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15130 enums) is used here. */
15131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15132 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15134 pedwarn (input_location, 0, "elaborated-type-specifier "
15135 "for a scoped enum must not use the %<%D%> keyword",
15136 cp_lexer_peek_token (parser->lexer)->u.value);
15137 /* Consume the `struct' or `class' and parse it anyway. */
15138 cp_lexer_consume_token (parser->lexer);
15140 /* Parse the attributes. */
15141 attributes = cp_parser_attributes_opt (parser);
15143 /* Or, it might be `typename'. */
15144 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15145 RID_TYPENAME))
15147 /* Consume the `typename' token. */
15148 cp_lexer_consume_token (parser->lexer);
15149 /* Remember that it's a `typename' type. */
15150 tag_type = typename_type;
15152 /* Otherwise it must be a class-key. */
15153 else
15155 tag_type = cp_parser_class_key (parser);
15156 if (tag_type == none_type)
15157 return error_mark_node;
15158 /* Parse the attributes. */
15159 attributes = cp_parser_attributes_opt (parser);
15162 /* Look for the `::' operator. */
15163 globalscope = cp_parser_global_scope_opt (parser,
15164 /*current_scope_valid_p=*/false);
15165 /* Look for the nested-name-specifier. */
15166 if (tag_type == typename_type && !globalscope)
15168 if (!cp_parser_nested_name_specifier (parser,
15169 /*typename_keyword_p=*/true,
15170 /*check_dependency_p=*/true,
15171 /*type_p=*/true,
15172 is_declaration))
15173 return error_mark_node;
15175 else
15176 /* Even though `typename' is not present, the proposed resolution
15177 to Core Issue 180 says that in `class A<T>::B', `B' should be
15178 considered a type-name, even if `A<T>' is dependent. */
15179 cp_parser_nested_name_specifier_opt (parser,
15180 /*typename_keyword_p=*/true,
15181 /*check_dependency_p=*/true,
15182 /*type_p=*/true,
15183 is_declaration);
15184 /* For everything but enumeration types, consider a template-id.
15185 For an enumeration type, consider only a plain identifier. */
15186 if (tag_type != enum_type)
15188 bool template_p = false;
15189 tree decl;
15191 /* Allow the `template' keyword. */
15192 template_p = cp_parser_optional_template_keyword (parser);
15193 /* If we didn't see `template', we don't know if there's a
15194 template-id or not. */
15195 if (!template_p)
15196 cp_parser_parse_tentatively (parser);
15197 /* Parse the template-id. */
15198 token = cp_lexer_peek_token (parser->lexer);
15199 decl = cp_parser_template_id (parser, template_p,
15200 /*check_dependency_p=*/true,
15201 tag_type,
15202 is_declaration);
15203 /* If we didn't find a template-id, look for an ordinary
15204 identifier. */
15205 if (!template_p && !cp_parser_parse_definitely (parser))
15207 /* We can get here when cp_parser_template_id, called by
15208 cp_parser_class_name with tag_type == none_type, succeeds
15209 and caches a BASELINK. Then, when called again here,
15210 instead of failing and returning an error_mark_node
15211 returns it (see template/typename17.C in C++11).
15212 ??? Could we diagnose this earlier? */
15213 else if (tag_type == typename_type && BASELINK_P (decl))
15215 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15216 type = error_mark_node;
15218 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15219 in effect, then we must assume that, upon instantiation, the
15220 template will correspond to a class. */
15221 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15222 && tag_type == typename_type)
15223 type = make_typename_type (parser->scope, decl,
15224 typename_type,
15225 /*complain=*/tf_error);
15226 /* If the `typename' keyword is in effect and DECL is not a type
15227 decl, then type is non existent. */
15228 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15230 else if (TREE_CODE (decl) == TYPE_DECL)
15231 type = check_elaborated_type_specifier (tag_type, decl,
15232 /*allow_template_p=*/true);
15233 else if (decl == error_mark_node)
15234 type = error_mark_node;
15237 if (!type)
15239 token = cp_lexer_peek_token (parser->lexer);
15240 identifier = cp_parser_identifier (parser);
15242 if (identifier == error_mark_node)
15244 parser->scope = NULL_TREE;
15245 return error_mark_node;
15248 /* For a `typename', we needn't call xref_tag. */
15249 if (tag_type == typename_type
15250 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15251 return cp_parser_make_typename_type (parser, identifier,
15252 token->location);
15254 /* Template parameter lists apply only if we are not within a
15255 function parameter list. */
15256 bool template_parm_lists_apply
15257 = parser->num_template_parameter_lists;
15258 if (template_parm_lists_apply)
15259 for (cp_binding_level *s = current_binding_level;
15260 s && s->kind != sk_template_parms;
15261 s = s->level_chain)
15262 if (s->kind == sk_function_parms)
15263 template_parm_lists_apply = false;
15265 /* Look up a qualified name in the usual way. */
15266 if (parser->scope)
15268 tree decl;
15269 tree ambiguous_decls;
15271 decl = cp_parser_lookup_name (parser, identifier,
15272 tag_type,
15273 /*is_template=*/false,
15274 /*is_namespace=*/false,
15275 /*check_dependency=*/true,
15276 &ambiguous_decls,
15277 token->location);
15279 /* If the lookup was ambiguous, an error will already have been
15280 issued. */
15281 if (ambiguous_decls)
15282 return error_mark_node;
15284 /* If we are parsing friend declaration, DECL may be a
15285 TEMPLATE_DECL tree node here. However, we need to check
15286 whether this TEMPLATE_DECL results in valid code. Consider
15287 the following example:
15289 namespace N {
15290 template <class T> class C {};
15292 class X {
15293 template <class T> friend class N::C; // #1, valid code
15295 template <class T> class Y {
15296 friend class N::C; // #2, invalid code
15299 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15300 name lookup of `N::C'. We see that friend declaration must
15301 be template for the code to be valid. Note that
15302 processing_template_decl does not work here since it is
15303 always 1 for the above two cases. */
15305 decl = (cp_parser_maybe_treat_template_as_class
15306 (decl, /*tag_name_p=*/is_friend
15307 && template_parm_lists_apply));
15309 if (TREE_CODE (decl) != TYPE_DECL)
15311 cp_parser_diagnose_invalid_type_name (parser,
15312 identifier,
15313 token->location);
15314 return error_mark_node;
15317 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15319 bool allow_template = (template_parm_lists_apply
15320 || DECL_SELF_REFERENCE_P (decl));
15321 type = check_elaborated_type_specifier (tag_type, decl,
15322 allow_template);
15324 if (type == error_mark_node)
15325 return error_mark_node;
15328 /* Forward declarations of nested types, such as
15330 class C1::C2;
15331 class C1::C2::C3;
15333 are invalid unless all components preceding the final '::'
15334 are complete. If all enclosing types are complete, these
15335 declarations become merely pointless.
15337 Invalid forward declarations of nested types are errors
15338 caught elsewhere in parsing. Those that are pointless arrive
15339 here. */
15341 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15342 && !is_friend && !processing_explicit_instantiation)
15343 warning (0, "declaration %qD does not declare anything", decl);
15345 type = TREE_TYPE (decl);
15347 else
15349 /* An elaborated-type-specifier sometimes introduces a new type and
15350 sometimes names an existing type. Normally, the rule is that it
15351 introduces a new type only if there is not an existing type of
15352 the same name already in scope. For example, given:
15354 struct S {};
15355 void f() { struct S s; }
15357 the `struct S' in the body of `f' is the same `struct S' as in
15358 the global scope; the existing definition is used. However, if
15359 there were no global declaration, this would introduce a new
15360 local class named `S'.
15362 An exception to this rule applies to the following code:
15364 namespace N { struct S; }
15366 Here, the elaborated-type-specifier names a new type
15367 unconditionally; even if there is already an `S' in the
15368 containing scope this declaration names a new type.
15369 This exception only applies if the elaborated-type-specifier
15370 forms the complete declaration:
15372 [class.name]
15374 A declaration consisting solely of `class-key identifier ;' is
15375 either a redeclaration of the name in the current scope or a
15376 forward declaration of the identifier as a class name. It
15377 introduces the name into the current scope.
15379 We are in this situation precisely when the next token is a `;'.
15381 An exception to the exception is that a `friend' declaration does
15382 *not* name a new type; i.e., given:
15384 struct S { friend struct T; };
15386 `T' is not a new type in the scope of `S'.
15388 Also, `new struct S' or `sizeof (struct S)' never results in the
15389 definition of a new type; a new type can only be declared in a
15390 declaration context. */
15392 tag_scope ts;
15393 bool template_p;
15395 if (is_friend)
15396 /* Friends have special name lookup rules. */
15397 ts = ts_within_enclosing_non_class;
15398 else if (is_declaration
15399 && cp_lexer_next_token_is (parser->lexer,
15400 CPP_SEMICOLON))
15401 /* This is a `class-key identifier ;' */
15402 ts = ts_current;
15403 else
15404 ts = ts_global;
15406 template_p =
15407 (template_parm_lists_apply
15408 && (cp_parser_next_token_starts_class_definition_p (parser)
15409 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15410 /* An unqualified name was used to reference this type, so
15411 there were no qualifying templates. */
15412 if (template_parm_lists_apply
15413 && !cp_parser_check_template_parameters (parser,
15414 /*num_templates=*/0,
15415 token->location,
15416 /*declarator=*/NULL))
15417 return error_mark_node;
15418 type = xref_tag (tag_type, identifier, ts, template_p);
15422 if (type == error_mark_node)
15423 return error_mark_node;
15425 /* Allow attributes on forward declarations of classes. */
15426 if (attributes)
15428 if (TREE_CODE (type) == TYPENAME_TYPE)
15429 warning (OPT_Wattributes,
15430 "attributes ignored on uninstantiated type");
15431 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15432 && ! processing_explicit_instantiation)
15433 warning (OPT_Wattributes,
15434 "attributes ignored on template instantiation");
15435 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15436 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15437 else
15438 warning (OPT_Wattributes,
15439 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15442 if (tag_type != enum_type)
15444 /* Indicate whether this class was declared as a `class' or as a
15445 `struct'. */
15446 if (TREE_CODE (type) == RECORD_TYPE)
15447 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15448 cp_parser_check_class_key (tag_type, type);
15451 /* A "<" cannot follow an elaborated type specifier. If that
15452 happens, the user was probably trying to form a template-id. */
15453 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15454 token->location);
15456 return type;
15459 /* Parse an enum-specifier.
15461 enum-specifier:
15462 enum-head { enumerator-list [opt] }
15463 enum-head { enumerator-list , } [C++0x]
15465 enum-head:
15466 enum-key identifier [opt] enum-base [opt]
15467 enum-key nested-name-specifier identifier enum-base [opt]
15469 enum-key:
15470 enum
15471 enum class [C++0x]
15472 enum struct [C++0x]
15474 enum-base: [C++0x]
15475 : type-specifier-seq
15477 opaque-enum-specifier:
15478 enum-key identifier enum-base [opt] ;
15480 GNU Extensions:
15481 enum-key attributes[opt] identifier [opt] enum-base [opt]
15482 { enumerator-list [opt] }attributes[opt]
15483 enum-key attributes[opt] identifier [opt] enum-base [opt]
15484 { enumerator-list, }attributes[opt] [C++0x]
15486 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15487 if the token stream isn't an enum-specifier after all. */
15489 static tree
15490 cp_parser_enum_specifier (cp_parser* parser)
15492 tree identifier;
15493 tree type = NULL_TREE;
15494 tree prev_scope;
15495 tree nested_name_specifier = NULL_TREE;
15496 tree attributes;
15497 bool scoped_enum_p = false;
15498 bool has_underlying_type = false;
15499 bool nested_being_defined = false;
15500 bool new_value_list = false;
15501 bool is_new_type = false;
15502 bool is_anonymous = false;
15503 tree underlying_type = NULL_TREE;
15504 cp_token *type_start_token = NULL;
15505 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15507 parser->colon_corrects_to_scope_p = false;
15509 /* Parse tentatively so that we can back up if we don't find a
15510 enum-specifier. */
15511 cp_parser_parse_tentatively (parser);
15513 /* Caller guarantees that the current token is 'enum', an identifier
15514 possibly follows, and the token after that is an opening brace.
15515 If we don't have an identifier, fabricate an anonymous name for
15516 the enumeration being defined. */
15517 cp_lexer_consume_token (parser->lexer);
15519 /* Parse the "class" or "struct", which indicates a scoped
15520 enumeration type in C++0x. */
15521 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15522 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15524 if (cxx_dialect < cxx11)
15525 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15527 /* Consume the `struct' or `class' token. */
15528 cp_lexer_consume_token (parser->lexer);
15530 scoped_enum_p = true;
15533 attributes = cp_parser_attributes_opt (parser);
15535 /* Clear the qualification. */
15536 parser->scope = NULL_TREE;
15537 parser->qualifying_scope = NULL_TREE;
15538 parser->object_scope = NULL_TREE;
15540 /* Figure out in what scope the declaration is being placed. */
15541 prev_scope = current_scope ();
15543 type_start_token = cp_lexer_peek_token (parser->lexer);
15545 push_deferring_access_checks (dk_no_check);
15546 nested_name_specifier
15547 = cp_parser_nested_name_specifier_opt (parser,
15548 /*typename_keyword_p=*/true,
15549 /*check_dependency_p=*/false,
15550 /*type_p=*/false,
15551 /*is_declaration=*/false);
15553 if (nested_name_specifier)
15555 tree name;
15557 identifier = cp_parser_identifier (parser);
15558 name = cp_parser_lookup_name (parser, identifier,
15559 enum_type,
15560 /*is_template=*/false,
15561 /*is_namespace=*/false,
15562 /*check_dependency=*/true,
15563 /*ambiguous_decls=*/NULL,
15564 input_location);
15565 if (name && name != error_mark_node)
15567 type = TREE_TYPE (name);
15568 if (TREE_CODE (type) == TYPENAME_TYPE)
15570 /* Are template enums allowed in ISO? */
15571 if (template_parm_scope_p ())
15572 pedwarn (type_start_token->location, OPT_Wpedantic,
15573 "%qD is an enumeration template", name);
15574 /* ignore a typename reference, for it will be solved by name
15575 in start_enum. */
15576 type = NULL_TREE;
15579 else if (nested_name_specifier == error_mark_node)
15580 /* We already issued an error. */;
15581 else
15582 error_at (type_start_token->location,
15583 "%qD is not an enumerator-name", identifier);
15585 else
15587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15588 identifier = cp_parser_identifier (parser);
15589 else
15591 identifier = make_anon_name ();
15592 is_anonymous = true;
15593 if (scoped_enum_p)
15594 error_at (type_start_token->location,
15595 "anonymous scoped enum is not allowed");
15598 pop_deferring_access_checks ();
15600 /* Check for the `:' that denotes a specified underlying type in C++0x.
15601 Note that a ':' could also indicate a bitfield width, however. */
15602 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15604 cp_decl_specifier_seq type_specifiers;
15606 /* Consume the `:'. */
15607 cp_lexer_consume_token (parser->lexer);
15609 /* Parse the type-specifier-seq. */
15610 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15611 /*is_trailing_return=*/false,
15612 &type_specifiers);
15614 /* At this point this is surely not elaborated type specifier. */
15615 if (!cp_parser_parse_definitely (parser))
15616 return NULL_TREE;
15618 if (cxx_dialect < cxx11)
15619 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15621 has_underlying_type = true;
15623 /* If that didn't work, stop. */
15624 if (type_specifiers.type != error_mark_node)
15626 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15627 /*initialized=*/0, NULL);
15628 if (underlying_type == error_mark_node
15629 || check_for_bare_parameter_packs (underlying_type))
15630 underlying_type = NULL_TREE;
15634 /* Look for the `{' but don't consume it yet. */
15635 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15637 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15639 cp_parser_error (parser, "expected %<{%>");
15640 if (has_underlying_type)
15642 type = NULL_TREE;
15643 goto out;
15646 /* An opaque-enum-specifier must have a ';' here. */
15647 if ((scoped_enum_p || underlying_type)
15648 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15650 cp_parser_error (parser, "expected %<;%> or %<{%>");
15651 if (has_underlying_type)
15653 type = NULL_TREE;
15654 goto out;
15659 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15660 return NULL_TREE;
15662 if (nested_name_specifier)
15664 if (CLASS_TYPE_P (nested_name_specifier))
15666 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15667 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15668 push_scope (nested_name_specifier);
15670 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15672 push_nested_namespace (nested_name_specifier);
15676 /* Issue an error message if type-definitions are forbidden here. */
15677 if (!cp_parser_check_type_definition (parser))
15678 type = error_mark_node;
15679 else
15680 /* Create the new type. We do this before consuming the opening
15681 brace so the enum will be recorded as being on the line of its
15682 tag (or the 'enum' keyword, if there is no tag). */
15683 type = start_enum (identifier, type, underlying_type,
15684 scoped_enum_p, &is_new_type);
15686 /* If the next token is not '{' it is an opaque-enum-specifier or an
15687 elaborated-type-specifier. */
15688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15690 timevar_push (TV_PARSE_ENUM);
15691 if (nested_name_specifier
15692 && nested_name_specifier != error_mark_node)
15694 /* The following catches invalid code such as:
15695 enum class S<int>::E { A, B, C }; */
15696 if (!processing_specialization
15697 && CLASS_TYPE_P (nested_name_specifier)
15698 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15699 error_at (type_start_token->location, "cannot add an enumerator "
15700 "list to a template instantiation");
15702 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15704 error_at (type_start_token->location,
15705 "%<%T::%E%> has not been declared",
15706 TYPE_CONTEXT (nested_name_specifier),
15707 nested_name_specifier);
15708 type = error_mark_node;
15710 /* If that scope does not contain the scope in which the
15711 class was originally declared, the program is invalid. */
15712 else if (prev_scope && !is_ancestor (prev_scope,
15713 nested_name_specifier))
15715 if (at_namespace_scope_p ())
15716 error_at (type_start_token->location,
15717 "declaration of %qD in namespace %qD which does not "
15718 "enclose %qD",
15719 type, prev_scope, nested_name_specifier);
15720 else
15721 error_at (type_start_token->location,
15722 "declaration of %qD in %qD which does not "
15723 "enclose %qD",
15724 type, prev_scope, nested_name_specifier);
15725 type = error_mark_node;
15729 if (scoped_enum_p)
15730 begin_scope (sk_scoped_enum, type);
15732 /* Consume the opening brace. */
15733 cp_lexer_consume_token (parser->lexer);
15735 if (type == error_mark_node)
15736 ; /* Nothing to add */
15737 else if (OPAQUE_ENUM_P (type)
15738 || (cxx_dialect > cxx98 && processing_specialization))
15740 new_value_list = true;
15741 SET_OPAQUE_ENUM_P (type, false);
15742 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15744 else
15746 error_at (type_start_token->location,
15747 "multiple definition of %q#T", type);
15748 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15749 "previous definition here");
15750 type = error_mark_node;
15753 if (type == error_mark_node)
15754 cp_parser_skip_to_end_of_block_or_statement (parser);
15755 /* If the next token is not '}', then there are some enumerators. */
15756 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15758 if (is_anonymous && !scoped_enum_p)
15759 pedwarn (type_start_token->location, OPT_Wpedantic,
15760 "ISO C++ forbids empty anonymous enum");
15762 else
15763 cp_parser_enumerator_list (parser, type);
15765 /* Consume the final '}'. */
15766 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15768 if (scoped_enum_p)
15769 finish_scope ();
15770 timevar_pop (TV_PARSE_ENUM);
15772 else
15774 /* If a ';' follows, then it is an opaque-enum-specifier
15775 and additional restrictions apply. */
15776 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15778 if (is_anonymous)
15779 error_at (type_start_token->location,
15780 "opaque-enum-specifier without name");
15781 else if (nested_name_specifier)
15782 error_at (type_start_token->location,
15783 "opaque-enum-specifier must use a simple identifier");
15787 /* Look for trailing attributes to apply to this enumeration, and
15788 apply them if appropriate. */
15789 if (cp_parser_allow_gnu_extensions_p (parser))
15791 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15792 trailing_attr = chainon (trailing_attr, attributes);
15793 cplus_decl_attributes (&type,
15794 trailing_attr,
15795 (int) ATTR_FLAG_TYPE_IN_PLACE);
15798 /* Finish up the enumeration. */
15799 if (type != error_mark_node)
15801 if (new_value_list)
15802 finish_enum_value_list (type);
15803 if (is_new_type)
15804 finish_enum (type);
15807 if (nested_name_specifier)
15809 if (CLASS_TYPE_P (nested_name_specifier))
15811 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15812 pop_scope (nested_name_specifier);
15814 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15816 pop_nested_namespace (nested_name_specifier);
15819 out:
15820 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15821 return type;
15824 /* Parse an enumerator-list. The enumerators all have the indicated
15825 TYPE.
15827 enumerator-list:
15828 enumerator-definition
15829 enumerator-list , enumerator-definition */
15831 static void
15832 cp_parser_enumerator_list (cp_parser* parser, tree type)
15834 while (true)
15836 /* Parse an enumerator-definition. */
15837 cp_parser_enumerator_definition (parser, type);
15839 /* If the next token is not a ',', we've reached the end of
15840 the list. */
15841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15842 break;
15843 /* Otherwise, consume the `,' and keep going. */
15844 cp_lexer_consume_token (parser->lexer);
15845 /* If the next token is a `}', there is a trailing comma. */
15846 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15848 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15849 pedwarn (input_location, OPT_Wpedantic,
15850 "comma at end of enumerator list");
15851 break;
15856 /* Parse an enumerator-definition. The enumerator has the indicated
15857 TYPE.
15859 enumerator-definition:
15860 enumerator
15861 enumerator = constant-expression
15863 enumerator:
15864 identifier */
15866 static void
15867 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15869 tree identifier;
15870 tree value;
15871 location_t loc;
15873 /* Save the input location because we are interested in the location
15874 of the identifier and not the location of the explicit value. */
15875 loc = cp_lexer_peek_token (parser->lexer)->location;
15877 /* Look for the identifier. */
15878 identifier = cp_parser_identifier (parser);
15879 if (identifier == error_mark_node)
15880 return;
15882 /* If the next token is an '=', then there is an explicit value. */
15883 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15885 /* Consume the `=' token. */
15886 cp_lexer_consume_token (parser->lexer);
15887 /* Parse the value. */
15888 value = cp_parser_constant_expression (parser);
15890 else
15891 value = NULL_TREE;
15893 /* If we are processing a template, make sure the initializer of the
15894 enumerator doesn't contain any bare template parameter pack. */
15895 if (check_for_bare_parameter_packs (value))
15896 value = error_mark_node;
15898 /* integral_constant_value will pull out this expression, so make sure
15899 it's folded as appropriate. */
15900 value = fold_non_dependent_expr (value);
15902 /* Create the enumerator. */
15903 build_enumerator (identifier, value, type, loc);
15906 /* Parse a namespace-name.
15908 namespace-name:
15909 original-namespace-name
15910 namespace-alias
15912 Returns the NAMESPACE_DECL for the namespace. */
15914 static tree
15915 cp_parser_namespace_name (cp_parser* parser)
15917 tree identifier;
15918 tree namespace_decl;
15920 cp_token *token = cp_lexer_peek_token (parser->lexer);
15922 /* Get the name of the namespace. */
15923 identifier = cp_parser_identifier (parser);
15924 if (identifier == error_mark_node)
15925 return error_mark_node;
15927 /* Look up the identifier in the currently active scope. Look only
15928 for namespaces, due to:
15930 [basic.lookup.udir]
15932 When looking up a namespace-name in a using-directive or alias
15933 definition, only namespace names are considered.
15935 And:
15937 [basic.lookup.qual]
15939 During the lookup of a name preceding the :: scope resolution
15940 operator, object, function, and enumerator names are ignored.
15942 (Note that cp_parser_qualifying_entity only calls this
15943 function if the token after the name is the scope resolution
15944 operator.) */
15945 namespace_decl = cp_parser_lookup_name (parser, identifier,
15946 none_type,
15947 /*is_template=*/false,
15948 /*is_namespace=*/true,
15949 /*check_dependency=*/true,
15950 /*ambiguous_decls=*/NULL,
15951 token->location);
15952 /* If it's not a namespace, issue an error. */
15953 if (namespace_decl == error_mark_node
15954 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15956 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15957 error_at (token->location, "%qD is not a namespace-name", identifier);
15958 cp_parser_error (parser, "expected namespace-name");
15959 namespace_decl = error_mark_node;
15962 return namespace_decl;
15965 /* Parse a namespace-definition.
15967 namespace-definition:
15968 named-namespace-definition
15969 unnamed-namespace-definition
15971 named-namespace-definition:
15972 original-namespace-definition
15973 extension-namespace-definition
15975 original-namespace-definition:
15976 namespace identifier { namespace-body }
15978 extension-namespace-definition:
15979 namespace original-namespace-name { namespace-body }
15981 unnamed-namespace-definition:
15982 namespace { namespace-body } */
15984 static void
15985 cp_parser_namespace_definition (cp_parser* parser)
15987 tree identifier, attribs;
15988 bool has_visibility;
15989 bool is_inline;
15991 cp_ensure_no_omp_declare_simd (parser);
15992 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15994 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15995 is_inline = true;
15996 cp_lexer_consume_token (parser->lexer);
15998 else
15999 is_inline = false;
16001 /* Look for the `namespace' keyword. */
16002 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16004 /* Get the name of the namespace. We do not attempt to distinguish
16005 between an original-namespace-definition and an
16006 extension-namespace-definition at this point. The semantic
16007 analysis routines are responsible for that. */
16008 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16009 identifier = cp_parser_identifier (parser);
16010 else
16011 identifier = NULL_TREE;
16013 /* Parse any specified attributes. */
16014 attribs = cp_parser_attributes_opt (parser);
16016 /* Look for the `{' to start the namespace. */
16017 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16018 /* Start the namespace. */
16019 push_namespace (identifier);
16021 /* "inline namespace" is equivalent to a stub namespace definition
16022 followed by a strong using directive. */
16023 if (is_inline)
16025 tree name_space = current_namespace;
16026 /* Set up namespace association. */
16027 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16028 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16029 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16030 /* Import the contents of the inline namespace. */
16031 pop_namespace ();
16032 do_using_directive (name_space);
16033 push_namespace (identifier);
16036 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16038 /* Parse the body of the namespace. */
16039 cp_parser_namespace_body (parser);
16041 if (has_visibility)
16042 pop_visibility (1);
16044 /* Finish the namespace. */
16045 pop_namespace ();
16046 /* Look for the final `}'. */
16047 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16050 /* Parse a namespace-body.
16052 namespace-body:
16053 declaration-seq [opt] */
16055 static void
16056 cp_parser_namespace_body (cp_parser* parser)
16058 cp_parser_declaration_seq_opt (parser);
16061 /* Parse a namespace-alias-definition.
16063 namespace-alias-definition:
16064 namespace identifier = qualified-namespace-specifier ; */
16066 static void
16067 cp_parser_namespace_alias_definition (cp_parser* parser)
16069 tree identifier;
16070 tree namespace_specifier;
16072 cp_token *token = cp_lexer_peek_token (parser->lexer);
16074 /* Look for the `namespace' keyword. */
16075 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16076 /* Look for the identifier. */
16077 identifier = cp_parser_identifier (parser);
16078 if (identifier == error_mark_node)
16079 return;
16080 /* Look for the `=' token. */
16081 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16082 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16084 error_at (token->location, "%<namespace%> definition is not allowed here");
16085 /* Skip the definition. */
16086 cp_lexer_consume_token (parser->lexer);
16087 if (cp_parser_skip_to_closing_brace (parser))
16088 cp_lexer_consume_token (parser->lexer);
16089 return;
16091 cp_parser_require (parser, CPP_EQ, RT_EQ);
16092 /* Look for the qualified-namespace-specifier. */
16093 namespace_specifier
16094 = cp_parser_qualified_namespace_specifier (parser);
16095 /* Look for the `;' token. */
16096 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16098 /* Register the alias in the symbol table. */
16099 do_namespace_alias (identifier, namespace_specifier);
16102 /* Parse a qualified-namespace-specifier.
16104 qualified-namespace-specifier:
16105 :: [opt] nested-name-specifier [opt] namespace-name
16107 Returns a NAMESPACE_DECL corresponding to the specified
16108 namespace. */
16110 static tree
16111 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16113 /* Look for the optional `::'. */
16114 cp_parser_global_scope_opt (parser,
16115 /*current_scope_valid_p=*/false);
16117 /* Look for the optional nested-name-specifier. */
16118 cp_parser_nested_name_specifier_opt (parser,
16119 /*typename_keyword_p=*/false,
16120 /*check_dependency_p=*/true,
16121 /*type_p=*/false,
16122 /*is_declaration=*/true);
16124 return cp_parser_namespace_name (parser);
16127 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16128 access declaration.
16130 using-declaration:
16131 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16132 using :: unqualified-id ;
16134 access-declaration:
16135 qualified-id ;
16139 static bool
16140 cp_parser_using_declaration (cp_parser* parser,
16141 bool access_declaration_p)
16143 cp_token *token;
16144 bool typename_p = false;
16145 bool global_scope_p;
16146 tree decl;
16147 tree identifier;
16148 tree qscope;
16149 int oldcount = errorcount;
16150 cp_token *diag_token = NULL;
16152 if (access_declaration_p)
16154 diag_token = cp_lexer_peek_token (parser->lexer);
16155 cp_parser_parse_tentatively (parser);
16157 else
16159 /* Look for the `using' keyword. */
16160 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16162 /* Peek at the next token. */
16163 token = cp_lexer_peek_token (parser->lexer);
16164 /* See if it's `typename'. */
16165 if (token->keyword == RID_TYPENAME)
16167 /* Remember that we've seen it. */
16168 typename_p = true;
16169 /* Consume the `typename' token. */
16170 cp_lexer_consume_token (parser->lexer);
16174 /* Look for the optional global scope qualification. */
16175 global_scope_p
16176 = (cp_parser_global_scope_opt (parser,
16177 /*current_scope_valid_p=*/false)
16178 != NULL_TREE);
16180 /* If we saw `typename', or didn't see `::', then there must be a
16181 nested-name-specifier present. */
16182 if (typename_p || !global_scope_p)
16184 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16185 /*check_dependency_p=*/true,
16186 /*type_p=*/false,
16187 /*is_declaration=*/true);
16188 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16190 cp_parser_skip_to_end_of_block_or_statement (parser);
16191 return false;
16194 /* Otherwise, we could be in either of the two productions. In that
16195 case, treat the nested-name-specifier as optional. */
16196 else
16197 qscope = cp_parser_nested_name_specifier_opt (parser,
16198 /*typename_keyword_p=*/false,
16199 /*check_dependency_p=*/true,
16200 /*type_p=*/false,
16201 /*is_declaration=*/true);
16202 if (!qscope)
16203 qscope = global_namespace;
16204 else if (UNSCOPED_ENUM_P (qscope))
16205 qscope = CP_TYPE_CONTEXT (qscope);
16207 if (access_declaration_p && cp_parser_error_occurred (parser))
16208 /* Something has already gone wrong; there's no need to parse
16209 further. Since an error has occurred, the return value of
16210 cp_parser_parse_definitely will be false, as required. */
16211 return cp_parser_parse_definitely (parser);
16213 token = cp_lexer_peek_token (parser->lexer);
16214 /* Parse the unqualified-id. */
16215 identifier = cp_parser_unqualified_id (parser,
16216 /*template_keyword_p=*/false,
16217 /*check_dependency_p=*/true,
16218 /*declarator_p=*/true,
16219 /*optional_p=*/false);
16221 if (access_declaration_p)
16223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16224 cp_parser_simulate_error (parser);
16225 if (!cp_parser_parse_definitely (parser))
16226 return false;
16229 /* The function we call to handle a using-declaration is different
16230 depending on what scope we are in. */
16231 if (qscope == error_mark_node || identifier == error_mark_node)
16233 else if (!identifier_p (identifier)
16234 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16235 /* [namespace.udecl]
16237 A using declaration shall not name a template-id. */
16238 error_at (token->location,
16239 "a template-id may not appear in a using-declaration");
16240 else
16242 if (at_class_scope_p ())
16244 /* Create the USING_DECL. */
16245 decl = do_class_using_decl (parser->scope, identifier);
16247 if (decl && typename_p)
16248 USING_DECL_TYPENAME_P (decl) = 1;
16250 if (check_for_bare_parameter_packs (decl))
16252 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16253 return false;
16255 else
16256 /* Add it to the list of members in this class. */
16257 finish_member_declaration (decl);
16259 else
16261 decl = cp_parser_lookup_name_simple (parser,
16262 identifier,
16263 token->location);
16264 if (decl == error_mark_node)
16265 cp_parser_name_lookup_error (parser, identifier,
16266 decl, NLE_NULL,
16267 token->location);
16268 else if (check_for_bare_parameter_packs (decl))
16270 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16271 return false;
16273 else if (!at_namespace_scope_p ())
16274 do_local_using_decl (decl, qscope, identifier);
16275 else
16276 do_toplevel_using_decl (decl, qscope, identifier);
16280 /* Look for the final `;'. */
16281 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16283 if (access_declaration_p && errorcount == oldcount)
16284 warning_at (diag_token->location, OPT_Wdeprecated,
16285 "access declarations are deprecated "
16286 "in favour of using-declarations; "
16287 "suggestion: add the %<using%> keyword");
16289 return true;
16292 /* Parse an alias-declaration.
16294 alias-declaration:
16295 using identifier attribute-specifier-seq [opt] = type-id */
16297 static tree
16298 cp_parser_alias_declaration (cp_parser* parser)
16300 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16301 location_t id_location;
16302 cp_declarator *declarator;
16303 cp_decl_specifier_seq decl_specs;
16304 bool member_p;
16305 const char *saved_message = NULL;
16307 /* Look for the `using' keyword. */
16308 cp_token *using_token
16309 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16310 if (using_token == NULL)
16311 return error_mark_node;
16313 id_location = cp_lexer_peek_token (parser->lexer)->location;
16314 id = cp_parser_identifier (parser);
16315 if (id == error_mark_node)
16316 return error_mark_node;
16318 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16319 attributes = cp_parser_attributes_opt (parser);
16320 if (attributes == error_mark_node)
16321 return error_mark_node;
16323 cp_parser_require (parser, CPP_EQ, RT_EQ);
16325 if (cp_parser_error_occurred (parser))
16326 return error_mark_node;
16328 cp_parser_commit_to_tentative_parse (parser);
16330 /* Now we are going to parse the type-id of the declaration. */
16333 [dcl.type]/3 says:
16335 "A type-specifier-seq shall not define a class or enumeration
16336 unless it appears in the type-id of an alias-declaration (7.1.3) that
16337 is not the declaration of a template-declaration."
16339 In other words, if we currently are in an alias template, the
16340 type-id should not define a type.
16342 So let's set parser->type_definition_forbidden_message in that
16343 case; cp_parser_check_type_definition (called by
16344 cp_parser_class_specifier) will then emit an error if a type is
16345 defined in the type-id. */
16346 if (parser->num_template_parameter_lists)
16348 saved_message = parser->type_definition_forbidden_message;
16349 parser->type_definition_forbidden_message =
16350 G_("types may not be defined in alias template declarations");
16353 type = cp_parser_type_id (parser);
16355 /* Restore the error message if need be. */
16356 if (parser->num_template_parameter_lists)
16357 parser->type_definition_forbidden_message = saved_message;
16359 if (type == error_mark_node
16360 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16362 cp_parser_skip_to_end_of_block_or_statement (parser);
16363 return error_mark_node;
16366 /* A typedef-name can also be introduced by an alias-declaration. The
16367 identifier following the using keyword becomes a typedef-name. It has
16368 the same semantics as if it were introduced by the typedef
16369 specifier. In particular, it does not define a new type and it shall
16370 not appear in the type-id. */
16372 clear_decl_specs (&decl_specs);
16373 decl_specs.type = type;
16374 if (attributes != NULL_TREE)
16376 decl_specs.attributes = attributes;
16377 set_and_check_decl_spec_loc (&decl_specs,
16378 ds_attribute,
16379 attrs_token);
16381 set_and_check_decl_spec_loc (&decl_specs,
16382 ds_typedef,
16383 using_token);
16384 set_and_check_decl_spec_loc (&decl_specs,
16385 ds_alias,
16386 using_token);
16388 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16389 declarator->id_loc = id_location;
16391 member_p = at_class_scope_p ();
16392 if (member_p)
16393 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16394 NULL_TREE, attributes);
16395 else
16396 decl = start_decl (declarator, &decl_specs, 0,
16397 attributes, NULL_TREE, &pushed_scope);
16398 if (decl == error_mark_node)
16399 return decl;
16401 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16403 if (pushed_scope)
16404 pop_scope (pushed_scope);
16406 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16407 added into the symbol table; otherwise, return the TYPE_DECL. */
16408 if (DECL_LANG_SPECIFIC (decl)
16409 && DECL_TEMPLATE_INFO (decl)
16410 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16412 decl = DECL_TI_TEMPLATE (decl);
16413 if (member_p)
16414 check_member_template (decl);
16417 return decl;
16420 /* Parse a using-directive.
16422 using-directive:
16423 using namespace :: [opt] nested-name-specifier [opt]
16424 namespace-name ; */
16426 static void
16427 cp_parser_using_directive (cp_parser* parser)
16429 tree namespace_decl;
16430 tree attribs;
16432 /* Look for the `using' keyword. */
16433 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16434 /* And the `namespace' keyword. */
16435 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16436 /* Look for the optional `::' operator. */
16437 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16438 /* And the optional nested-name-specifier. */
16439 cp_parser_nested_name_specifier_opt (parser,
16440 /*typename_keyword_p=*/false,
16441 /*check_dependency_p=*/true,
16442 /*type_p=*/false,
16443 /*is_declaration=*/true);
16444 /* Get the namespace being used. */
16445 namespace_decl = cp_parser_namespace_name (parser);
16446 /* And any specified attributes. */
16447 attribs = cp_parser_attributes_opt (parser);
16448 /* Update the symbol table. */
16449 parse_using_directive (namespace_decl, attribs);
16450 /* Look for the final `;'. */
16451 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16454 /* Parse an asm-definition.
16456 asm-definition:
16457 asm ( string-literal ) ;
16459 GNU Extension:
16461 asm-definition:
16462 asm volatile [opt] ( string-literal ) ;
16463 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16464 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16465 : asm-operand-list [opt] ) ;
16466 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16467 : asm-operand-list [opt]
16468 : asm-clobber-list [opt] ) ;
16469 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16470 : asm-clobber-list [opt]
16471 : asm-goto-list ) ; */
16473 static void
16474 cp_parser_asm_definition (cp_parser* parser)
16476 tree string;
16477 tree outputs = NULL_TREE;
16478 tree inputs = NULL_TREE;
16479 tree clobbers = NULL_TREE;
16480 tree labels = NULL_TREE;
16481 tree asm_stmt;
16482 bool volatile_p = false;
16483 bool extended_p = false;
16484 bool invalid_inputs_p = false;
16485 bool invalid_outputs_p = false;
16486 bool goto_p = false;
16487 required_token missing = RT_NONE;
16489 /* Look for the `asm' keyword. */
16490 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16492 if (parser->in_function_body
16493 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16494 error ("%<asm%> in %<constexpr%> function");
16496 /* See if the next token is `volatile'. */
16497 if (cp_parser_allow_gnu_extensions_p (parser)
16498 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16500 /* Remember that we saw the `volatile' keyword. */
16501 volatile_p = true;
16502 /* Consume the token. */
16503 cp_lexer_consume_token (parser->lexer);
16505 if (cp_parser_allow_gnu_extensions_p (parser)
16506 && parser->in_function_body
16507 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16509 /* Remember that we saw the `goto' keyword. */
16510 goto_p = true;
16511 /* Consume the token. */
16512 cp_lexer_consume_token (parser->lexer);
16514 /* Look for the opening `('. */
16515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16516 return;
16517 /* Look for the string. */
16518 string = cp_parser_string_literal (parser, false, false);
16519 if (string == error_mark_node)
16521 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16522 /*consume_paren=*/true);
16523 return;
16526 /* If we're allowing GNU extensions, check for the extended assembly
16527 syntax. Unfortunately, the `:' tokens need not be separated by
16528 a space in C, and so, for compatibility, we tolerate that here
16529 too. Doing that means that we have to treat the `::' operator as
16530 two `:' tokens. */
16531 if (cp_parser_allow_gnu_extensions_p (parser)
16532 && parser->in_function_body
16533 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16534 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16536 bool inputs_p = false;
16537 bool clobbers_p = false;
16538 bool labels_p = false;
16540 /* The extended syntax was used. */
16541 extended_p = true;
16543 /* Look for outputs. */
16544 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16546 /* Consume the `:'. */
16547 cp_lexer_consume_token (parser->lexer);
16548 /* Parse the output-operands. */
16549 if (cp_lexer_next_token_is_not (parser->lexer,
16550 CPP_COLON)
16551 && cp_lexer_next_token_is_not (parser->lexer,
16552 CPP_SCOPE)
16553 && cp_lexer_next_token_is_not (parser->lexer,
16554 CPP_CLOSE_PAREN)
16555 && !goto_p)
16556 outputs = cp_parser_asm_operand_list (parser);
16558 if (outputs == error_mark_node)
16559 invalid_outputs_p = true;
16561 /* If the next token is `::', there are no outputs, and the
16562 next token is the beginning of the inputs. */
16563 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16564 /* The inputs are coming next. */
16565 inputs_p = true;
16567 /* Look for inputs. */
16568 if (inputs_p
16569 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16571 /* Consume the `:' or `::'. */
16572 cp_lexer_consume_token (parser->lexer);
16573 /* Parse the output-operands. */
16574 if (cp_lexer_next_token_is_not (parser->lexer,
16575 CPP_COLON)
16576 && cp_lexer_next_token_is_not (parser->lexer,
16577 CPP_SCOPE)
16578 && cp_lexer_next_token_is_not (parser->lexer,
16579 CPP_CLOSE_PAREN))
16580 inputs = cp_parser_asm_operand_list (parser);
16582 if (inputs == error_mark_node)
16583 invalid_inputs_p = true;
16585 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16586 /* The clobbers are coming next. */
16587 clobbers_p = true;
16589 /* Look for clobbers. */
16590 if (clobbers_p
16591 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16593 clobbers_p = true;
16594 /* Consume the `:' or `::'. */
16595 cp_lexer_consume_token (parser->lexer);
16596 /* Parse the clobbers. */
16597 if (cp_lexer_next_token_is_not (parser->lexer,
16598 CPP_COLON)
16599 && cp_lexer_next_token_is_not (parser->lexer,
16600 CPP_CLOSE_PAREN))
16601 clobbers = cp_parser_asm_clobber_list (parser);
16603 else if (goto_p
16604 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16605 /* The labels are coming next. */
16606 labels_p = true;
16608 /* Look for labels. */
16609 if (labels_p
16610 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16612 labels_p = true;
16613 /* Consume the `:' or `::'. */
16614 cp_lexer_consume_token (parser->lexer);
16615 /* Parse the labels. */
16616 labels = cp_parser_asm_label_list (parser);
16619 if (goto_p && !labels_p)
16620 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16622 else if (goto_p)
16623 missing = RT_COLON_SCOPE;
16625 /* Look for the closing `)'. */
16626 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16627 missing ? missing : RT_CLOSE_PAREN))
16628 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16629 /*consume_paren=*/true);
16630 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16632 if (!invalid_inputs_p && !invalid_outputs_p)
16634 /* Create the ASM_EXPR. */
16635 if (parser->in_function_body)
16637 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16638 inputs, clobbers, labels);
16639 /* If the extended syntax was not used, mark the ASM_EXPR. */
16640 if (!extended_p)
16642 tree temp = asm_stmt;
16643 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16644 temp = TREE_OPERAND (temp, 0);
16646 ASM_INPUT_P (temp) = 1;
16649 else
16650 symtab->finalize_toplevel_asm (string);
16654 /* Declarators [gram.dcl.decl] */
16656 /* Parse an init-declarator.
16658 init-declarator:
16659 declarator initializer [opt]
16661 GNU Extension:
16663 init-declarator:
16664 declarator asm-specification [opt] attributes [opt] initializer [opt]
16666 function-definition:
16667 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16668 function-body
16669 decl-specifier-seq [opt] declarator function-try-block
16671 GNU Extension:
16673 function-definition:
16674 __extension__ function-definition
16676 TM Extension:
16678 function-definition:
16679 decl-specifier-seq [opt] declarator function-transaction-block
16681 The DECL_SPECIFIERS apply to this declarator. Returns a
16682 representation of the entity declared. If MEMBER_P is TRUE, then
16683 this declarator appears in a class scope. The new DECL created by
16684 this declarator is returned.
16686 The CHECKS are access checks that should be performed once we know
16687 what entity is being declared (and, therefore, what classes have
16688 befriended it).
16690 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16691 for a function-definition here as well. If the declarator is a
16692 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16693 be TRUE upon return. By that point, the function-definition will
16694 have been completely parsed.
16696 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16697 is FALSE.
16699 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16700 parsed declaration if it is an uninitialized single declarator not followed
16701 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16702 if present, will not be consumed. If returned, this declarator will be
16703 created with SD_INITIALIZED but will not call cp_finish_decl. */
16705 static tree
16706 cp_parser_init_declarator (cp_parser* parser,
16707 cp_decl_specifier_seq *decl_specifiers,
16708 vec<deferred_access_check, va_gc> *checks,
16709 bool function_definition_allowed_p,
16710 bool member_p,
16711 int declares_class_or_enum,
16712 bool* function_definition_p,
16713 tree* maybe_range_for_decl)
16715 cp_token *token = NULL, *asm_spec_start_token = NULL,
16716 *attributes_start_token = NULL;
16717 cp_declarator *declarator;
16718 tree prefix_attributes;
16719 tree attributes = NULL;
16720 tree asm_specification;
16721 tree initializer;
16722 tree decl = NULL_TREE;
16723 tree scope;
16724 int is_initialized;
16725 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16726 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16727 "(...)". */
16728 enum cpp_ttype initialization_kind;
16729 bool is_direct_init = false;
16730 bool is_non_constant_init;
16731 int ctor_dtor_or_conv_p;
16732 bool friend_p = cp_parser_friend_p (decl_specifiers);
16733 tree pushed_scope = NULL_TREE;
16734 bool range_for_decl_p = false;
16735 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16737 /* Gather the attributes that were provided with the
16738 decl-specifiers. */
16739 prefix_attributes = decl_specifiers->attributes;
16741 /* Assume that this is not the declarator for a function
16742 definition. */
16743 if (function_definition_p)
16744 *function_definition_p = false;
16746 /* Default arguments are only permitted for function parameters. */
16747 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16748 parser->default_arg_ok_p = false;
16750 /* Defer access checks while parsing the declarator; we cannot know
16751 what names are accessible until we know what is being
16752 declared. */
16753 resume_deferring_access_checks ();
16755 /* Parse the declarator. */
16756 token = cp_lexer_peek_token (parser->lexer);
16757 declarator
16758 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16759 &ctor_dtor_or_conv_p,
16760 /*parenthesized_p=*/NULL,
16761 member_p, friend_p);
16762 /* Gather up the deferred checks. */
16763 stop_deferring_access_checks ();
16765 parser->default_arg_ok_p = saved_default_arg_ok_p;
16767 /* If the DECLARATOR was erroneous, there's no need to go
16768 further. */
16769 if (declarator == cp_error_declarator)
16770 return error_mark_node;
16772 /* Check that the number of template-parameter-lists is OK. */
16773 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16774 token->location))
16775 return error_mark_node;
16777 if (declares_class_or_enum & 2)
16778 cp_parser_check_for_definition_in_return_type (declarator,
16779 decl_specifiers->type,
16780 decl_specifiers->locations[ds_type_spec]);
16782 /* Figure out what scope the entity declared by the DECLARATOR is
16783 located in. `grokdeclarator' sometimes changes the scope, so
16784 we compute it now. */
16785 scope = get_scope_of_declarator (declarator);
16787 /* Perform any lookups in the declared type which were thought to be
16788 dependent, but are not in the scope of the declarator. */
16789 decl_specifiers->type
16790 = maybe_update_decl_type (decl_specifiers->type, scope);
16792 /* If we're allowing GNU extensions, look for an
16793 asm-specification. */
16794 if (cp_parser_allow_gnu_extensions_p (parser))
16796 /* Look for an asm-specification. */
16797 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16798 asm_specification = cp_parser_asm_specification_opt (parser);
16800 else
16801 asm_specification = NULL_TREE;
16803 /* Look for attributes. */
16804 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16805 attributes = cp_parser_attributes_opt (parser);
16807 /* Peek at the next token. */
16808 token = cp_lexer_peek_token (parser->lexer);
16810 bool bogus_implicit_tmpl = false;
16812 if (function_declarator_p (declarator))
16814 /* Check to see if the token indicates the start of a
16815 function-definition. */
16816 if (cp_parser_token_starts_function_definition_p (token))
16818 if (!function_definition_allowed_p)
16820 /* If a function-definition should not appear here, issue an
16821 error message. */
16822 cp_parser_error (parser,
16823 "a function-definition is not allowed here");
16824 return error_mark_node;
16827 location_t func_brace_location
16828 = cp_lexer_peek_token (parser->lexer)->location;
16830 /* Neither attributes nor an asm-specification are allowed
16831 on a function-definition. */
16832 if (asm_specification)
16833 error_at (asm_spec_start_token->location,
16834 "an asm-specification is not allowed "
16835 "on a function-definition");
16836 if (attributes)
16837 error_at (attributes_start_token->location,
16838 "attributes are not allowed "
16839 "on a function-definition");
16840 /* This is a function-definition. */
16841 *function_definition_p = true;
16843 /* Parse the function definition. */
16844 if (member_p)
16845 decl = cp_parser_save_member_function_body (parser,
16846 decl_specifiers,
16847 declarator,
16848 prefix_attributes);
16849 else
16850 decl =
16851 (cp_parser_function_definition_from_specifiers_and_declarator
16852 (parser, decl_specifiers, prefix_attributes, declarator));
16854 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16856 /* This is where the prologue starts... */
16857 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16858 = func_brace_location;
16861 return decl;
16864 else if (parser->fully_implicit_function_template_p)
16866 /* A non-template declaration involving a function parameter list
16867 containing an implicit template parameter will be made into a
16868 template. If the resulting declaration is not going to be an
16869 actual function then finish the template scope here to prevent it.
16870 An error message will be issued once we have a decl to talk about.
16872 FIXME probably we should do type deduction rather than create an
16873 implicit template, but the standard currently doesn't allow it. */
16874 bogus_implicit_tmpl = true;
16875 finish_fully_implicit_template (parser, NULL_TREE);
16878 /* [dcl.dcl]
16880 Only in function declarations for constructors, destructors, and
16881 type conversions can the decl-specifier-seq be omitted.
16883 We explicitly postpone this check past the point where we handle
16884 function-definitions because we tolerate function-definitions
16885 that are missing their return types in some modes. */
16886 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16888 cp_parser_error (parser,
16889 "expected constructor, destructor, or type conversion");
16890 return error_mark_node;
16893 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16894 if (token->type == CPP_EQ
16895 || token->type == CPP_OPEN_PAREN
16896 || token->type == CPP_OPEN_BRACE)
16898 is_initialized = SD_INITIALIZED;
16899 initialization_kind = token->type;
16900 if (maybe_range_for_decl)
16901 *maybe_range_for_decl = error_mark_node;
16903 if (token->type == CPP_EQ
16904 && function_declarator_p (declarator))
16906 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16907 if (t2->keyword == RID_DEFAULT)
16908 is_initialized = SD_DEFAULTED;
16909 else if (t2->keyword == RID_DELETE)
16910 is_initialized = SD_DELETED;
16913 else
16915 /* If the init-declarator isn't initialized and isn't followed by a
16916 `,' or `;', it's not a valid init-declarator. */
16917 if (token->type != CPP_COMMA
16918 && token->type != CPP_SEMICOLON)
16920 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16921 range_for_decl_p = true;
16922 else
16924 cp_parser_error (parser, "expected initializer");
16925 return error_mark_node;
16928 is_initialized = SD_UNINITIALIZED;
16929 initialization_kind = CPP_EOF;
16932 /* Because start_decl has side-effects, we should only call it if we
16933 know we're going ahead. By this point, we know that we cannot
16934 possibly be looking at any other construct. */
16935 cp_parser_commit_to_tentative_parse (parser);
16937 /* Enter the newly declared entry in the symbol table. If we're
16938 processing a declaration in a class-specifier, we wait until
16939 after processing the initializer. */
16940 if (!member_p)
16942 if (parser->in_unbraced_linkage_specification_p)
16943 decl_specifiers->storage_class = sc_extern;
16944 decl = start_decl (declarator, decl_specifiers,
16945 range_for_decl_p? SD_INITIALIZED : is_initialized,
16946 attributes, prefix_attributes, &pushed_scope);
16947 cp_finalize_omp_declare_simd (parser, decl);
16948 /* Adjust location of decl if declarator->id_loc is more appropriate:
16949 set, and decl wasn't merged with another decl, in which case its
16950 location would be different from input_location, and more accurate. */
16951 if (DECL_P (decl)
16952 && declarator->id_loc != UNKNOWN_LOCATION
16953 && DECL_SOURCE_LOCATION (decl) == input_location)
16954 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16956 else if (scope)
16957 /* Enter the SCOPE. That way unqualified names appearing in the
16958 initializer will be looked up in SCOPE. */
16959 pushed_scope = push_scope (scope);
16961 /* Perform deferred access control checks, now that we know in which
16962 SCOPE the declared entity resides. */
16963 if (!member_p && decl)
16965 tree saved_current_function_decl = NULL_TREE;
16967 /* If the entity being declared is a function, pretend that we
16968 are in its scope. If it is a `friend', it may have access to
16969 things that would not otherwise be accessible. */
16970 if (TREE_CODE (decl) == FUNCTION_DECL)
16972 saved_current_function_decl = current_function_decl;
16973 current_function_decl = decl;
16976 /* Perform access checks for template parameters. */
16977 cp_parser_perform_template_parameter_access_checks (checks);
16979 /* Perform the access control checks for the declarator and the
16980 decl-specifiers. */
16981 perform_deferred_access_checks (tf_warning_or_error);
16983 /* Restore the saved value. */
16984 if (TREE_CODE (decl) == FUNCTION_DECL)
16985 current_function_decl = saved_current_function_decl;
16988 /* Parse the initializer. */
16989 initializer = NULL_TREE;
16990 is_direct_init = false;
16991 is_non_constant_init = true;
16992 if (is_initialized)
16994 if (function_declarator_p (declarator))
16996 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16997 if (initialization_kind == CPP_EQ)
16998 initializer = cp_parser_pure_specifier (parser);
16999 else
17001 /* If the declaration was erroneous, we don't really
17002 know what the user intended, so just silently
17003 consume the initializer. */
17004 if (decl != error_mark_node)
17005 error_at (initializer_start_token->location,
17006 "initializer provided for function");
17007 cp_parser_skip_to_closing_parenthesis (parser,
17008 /*recovering=*/true,
17009 /*or_comma=*/false,
17010 /*consume_paren=*/true);
17013 else
17015 /* We want to record the extra mangling scope for in-class
17016 initializers of class members and initializers of static data
17017 member templates. The former involves deferring
17018 parsing of the initializer until end of class as with default
17019 arguments. So right here we only handle the latter. */
17020 if (!member_p && processing_template_decl)
17021 start_lambda_scope (decl);
17022 initializer = cp_parser_initializer (parser,
17023 &is_direct_init,
17024 &is_non_constant_init);
17025 if (!member_p && processing_template_decl)
17026 finish_lambda_scope ();
17027 if (initializer == error_mark_node)
17028 cp_parser_skip_to_end_of_statement (parser);
17032 /* The old parser allows attributes to appear after a parenthesized
17033 initializer. Mark Mitchell proposed removing this functionality
17034 on the GCC mailing lists on 2002-08-13. This parser accepts the
17035 attributes -- but ignores them. */
17036 if (cp_parser_allow_gnu_extensions_p (parser)
17037 && initialization_kind == CPP_OPEN_PAREN)
17038 if (cp_parser_attributes_opt (parser))
17039 warning (OPT_Wattributes,
17040 "attributes after parenthesized initializer ignored");
17042 /* And now complain about a non-function implicit template. */
17043 if (bogus_implicit_tmpl)
17044 error_at (DECL_SOURCE_LOCATION (decl),
17045 "non-function %qD declared as implicit template", decl);
17047 /* For an in-class declaration, use `grokfield' to create the
17048 declaration. */
17049 if (member_p)
17051 if (pushed_scope)
17053 pop_scope (pushed_scope);
17054 pushed_scope = NULL_TREE;
17056 decl = grokfield (declarator, decl_specifiers,
17057 initializer, !is_non_constant_init,
17058 /*asmspec=*/NULL_TREE,
17059 chainon (attributes, prefix_attributes));
17060 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17061 cp_parser_save_default_args (parser, decl);
17062 cp_finalize_omp_declare_simd (parser, decl);
17065 /* Finish processing the declaration. But, skip member
17066 declarations. */
17067 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17069 cp_finish_decl (decl,
17070 initializer, !is_non_constant_init,
17071 asm_specification,
17072 /* If the initializer is in parentheses, then this is
17073 a direct-initialization, which means that an
17074 `explicit' constructor is OK. Otherwise, an
17075 `explicit' constructor cannot be used. */
17076 ((is_direct_init || !is_initialized)
17077 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17079 else if ((cxx_dialect != cxx98) && friend_p
17080 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17081 /* Core issue #226 (C++0x only): A default template-argument
17082 shall not be specified in a friend class template
17083 declaration. */
17084 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17085 /*is_partial=*/false, /*is_friend_decl=*/1);
17087 if (!friend_p && pushed_scope)
17088 pop_scope (pushed_scope);
17090 if (function_declarator_p (declarator)
17091 && parser->fully_implicit_function_template_p)
17093 if (member_p)
17094 decl = finish_fully_implicit_template (parser, decl);
17095 else
17096 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17099 return decl;
17102 /* Parse a declarator.
17104 declarator:
17105 direct-declarator
17106 ptr-operator declarator
17108 abstract-declarator:
17109 ptr-operator abstract-declarator [opt]
17110 direct-abstract-declarator
17112 GNU Extensions:
17114 declarator:
17115 attributes [opt] direct-declarator
17116 attributes [opt] ptr-operator declarator
17118 abstract-declarator:
17119 attributes [opt] ptr-operator abstract-declarator [opt]
17120 attributes [opt] direct-abstract-declarator
17122 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17123 detect constructor, destructor or conversion operators. It is set
17124 to -1 if the declarator is a name, and +1 if it is a
17125 function. Otherwise it is set to zero. Usually you just want to
17126 test for >0, but internally the negative value is used.
17128 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17129 a decl-specifier-seq unless it declares a constructor, destructor,
17130 or conversion. It might seem that we could check this condition in
17131 semantic analysis, rather than parsing, but that makes it difficult
17132 to handle something like `f()'. We want to notice that there are
17133 no decl-specifiers, and therefore realize that this is an
17134 expression, not a declaration.)
17136 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17137 the declarator is a direct-declarator of the form "(...)".
17139 MEMBER_P is true iff this declarator is a member-declarator.
17141 FRIEND_P is true iff this declarator is a friend. */
17143 static cp_declarator *
17144 cp_parser_declarator (cp_parser* parser,
17145 cp_parser_declarator_kind dcl_kind,
17146 int* ctor_dtor_or_conv_p,
17147 bool* parenthesized_p,
17148 bool member_p, bool friend_p)
17150 cp_declarator *declarator;
17151 enum tree_code code;
17152 cp_cv_quals cv_quals;
17153 tree class_type;
17154 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17156 /* Assume this is not a constructor, destructor, or type-conversion
17157 operator. */
17158 if (ctor_dtor_or_conv_p)
17159 *ctor_dtor_or_conv_p = 0;
17161 if (cp_parser_allow_gnu_extensions_p (parser))
17162 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17164 /* Check for the ptr-operator production. */
17165 cp_parser_parse_tentatively (parser);
17166 /* Parse the ptr-operator. */
17167 code = cp_parser_ptr_operator (parser,
17168 &class_type,
17169 &cv_quals,
17170 &std_attributes);
17172 /* If that worked, then we have a ptr-operator. */
17173 if (cp_parser_parse_definitely (parser))
17175 /* If a ptr-operator was found, then this declarator was not
17176 parenthesized. */
17177 if (parenthesized_p)
17178 *parenthesized_p = true;
17179 /* The dependent declarator is optional if we are parsing an
17180 abstract-declarator. */
17181 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17182 cp_parser_parse_tentatively (parser);
17184 /* Parse the dependent declarator. */
17185 declarator = cp_parser_declarator (parser, dcl_kind,
17186 /*ctor_dtor_or_conv_p=*/NULL,
17187 /*parenthesized_p=*/NULL,
17188 /*member_p=*/false,
17189 friend_p);
17191 /* If we are parsing an abstract-declarator, we must handle the
17192 case where the dependent declarator is absent. */
17193 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17194 && !cp_parser_parse_definitely (parser))
17195 declarator = NULL;
17197 declarator = cp_parser_make_indirect_declarator
17198 (code, class_type, cv_quals, declarator, std_attributes);
17200 /* Everything else is a direct-declarator. */
17201 else
17203 if (parenthesized_p)
17204 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17205 CPP_OPEN_PAREN);
17206 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17207 ctor_dtor_or_conv_p,
17208 member_p, friend_p);
17211 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17212 declarator->attributes = gnu_attributes;
17213 return declarator;
17216 /* Parse a direct-declarator or direct-abstract-declarator.
17218 direct-declarator:
17219 declarator-id
17220 direct-declarator ( parameter-declaration-clause )
17221 cv-qualifier-seq [opt]
17222 ref-qualifier [opt]
17223 exception-specification [opt]
17224 direct-declarator [ constant-expression [opt] ]
17225 ( declarator )
17227 direct-abstract-declarator:
17228 direct-abstract-declarator [opt]
17229 ( parameter-declaration-clause )
17230 cv-qualifier-seq [opt]
17231 ref-qualifier [opt]
17232 exception-specification [opt]
17233 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17234 ( abstract-declarator )
17236 Returns a representation of the declarator. DCL_KIND is
17237 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17238 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17239 we are parsing a direct-declarator. It is
17240 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17241 of ambiguity we prefer an abstract declarator, as per
17242 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17243 as for cp_parser_declarator. */
17245 static cp_declarator *
17246 cp_parser_direct_declarator (cp_parser* parser,
17247 cp_parser_declarator_kind dcl_kind,
17248 int* ctor_dtor_or_conv_p,
17249 bool member_p, bool friend_p)
17251 cp_token *token;
17252 cp_declarator *declarator = NULL;
17253 tree scope = NULL_TREE;
17254 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17255 bool saved_in_declarator_p = parser->in_declarator_p;
17256 bool first = true;
17257 tree pushed_scope = NULL_TREE;
17259 while (true)
17261 /* Peek at the next token. */
17262 token = cp_lexer_peek_token (parser->lexer);
17263 if (token->type == CPP_OPEN_PAREN)
17265 /* This is either a parameter-declaration-clause, or a
17266 parenthesized declarator. When we know we are parsing a
17267 named declarator, it must be a parenthesized declarator
17268 if FIRST is true. For instance, `(int)' is a
17269 parameter-declaration-clause, with an omitted
17270 direct-abstract-declarator. But `((*))', is a
17271 parenthesized abstract declarator. Finally, when T is a
17272 template parameter `(T)' is a
17273 parameter-declaration-clause, and not a parenthesized
17274 named declarator.
17276 We first try and parse a parameter-declaration-clause,
17277 and then try a nested declarator (if FIRST is true).
17279 It is not an error for it not to be a
17280 parameter-declaration-clause, even when FIRST is
17281 false. Consider,
17283 int i (int);
17284 int i (3);
17286 The first is the declaration of a function while the
17287 second is the definition of a variable, including its
17288 initializer.
17290 Having seen only the parenthesis, we cannot know which of
17291 these two alternatives should be selected. Even more
17292 complex are examples like:
17294 int i (int (a));
17295 int i (int (3));
17297 The former is a function-declaration; the latter is a
17298 variable initialization.
17300 Thus again, we try a parameter-declaration-clause, and if
17301 that fails, we back out and return. */
17303 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17305 tree params;
17306 bool is_declarator = false;
17308 /* In a member-declarator, the only valid interpretation
17309 of a parenthesis is the start of a
17310 parameter-declaration-clause. (It is invalid to
17311 initialize a static data member with a parenthesized
17312 initializer; only the "=" form of initialization is
17313 permitted.) */
17314 if (!member_p)
17315 cp_parser_parse_tentatively (parser);
17317 /* Consume the `('. */
17318 cp_lexer_consume_token (parser->lexer);
17319 if (first)
17321 /* If this is going to be an abstract declarator, we're
17322 in a declarator and we can't have default args. */
17323 parser->default_arg_ok_p = false;
17324 parser->in_declarator_p = true;
17327 begin_scope (sk_function_parms, NULL_TREE);
17329 /* Parse the parameter-declaration-clause. */
17330 params = cp_parser_parameter_declaration_clause (parser);
17332 /* Consume the `)'. */
17333 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17335 /* If all went well, parse the cv-qualifier-seq,
17336 ref-qualifier and the exception-specification. */
17337 if (member_p || cp_parser_parse_definitely (parser))
17339 cp_cv_quals cv_quals;
17340 cp_virt_specifiers virt_specifiers;
17341 cp_ref_qualifier ref_qual;
17342 tree exception_specification;
17343 tree late_return;
17344 tree attrs;
17345 bool memfn = (member_p || (pushed_scope
17346 && CLASS_TYPE_P (pushed_scope)));
17348 is_declarator = true;
17350 if (ctor_dtor_or_conv_p)
17351 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17352 first = false;
17354 /* Parse the cv-qualifier-seq. */
17355 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17356 /* Parse the ref-qualifier. */
17357 ref_qual = cp_parser_ref_qualifier_opt (parser);
17358 /* And the exception-specification. */
17359 exception_specification
17360 = cp_parser_exception_specification_opt (parser);
17362 attrs = cp_parser_std_attribute_spec_seq (parser);
17364 /* In here, we handle cases where attribute is used after
17365 the function declaration. For example:
17366 void func (int x) __attribute__((vector(..))); */
17367 if (flag_cilkplus
17368 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17370 cp_parser_parse_tentatively (parser);
17371 tree attr = cp_parser_gnu_attributes_opt (parser);
17372 if (cp_lexer_next_token_is_not (parser->lexer,
17373 CPP_SEMICOLON)
17374 && cp_lexer_next_token_is_not (parser->lexer,
17375 CPP_OPEN_BRACE))
17376 cp_parser_abort_tentative_parse (parser);
17377 else if (!cp_parser_parse_definitely (parser))
17379 else
17380 attrs = chainon (attr, attrs);
17382 late_return = (cp_parser_late_return_type_opt
17383 (parser, declarator,
17384 memfn ? cv_quals : -1));
17387 /* Parse the virt-specifier-seq. */
17388 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17390 /* Create the function-declarator. */
17391 declarator = make_call_declarator (declarator,
17392 params,
17393 cv_quals,
17394 virt_specifiers,
17395 ref_qual,
17396 exception_specification,
17397 late_return);
17398 declarator->std_attributes = attrs;
17399 /* Any subsequent parameter lists are to do with
17400 return type, so are not those of the declared
17401 function. */
17402 parser->default_arg_ok_p = false;
17405 /* Remove the function parms from scope. */
17406 pop_bindings_and_leave_scope ();
17408 if (is_declarator)
17409 /* Repeat the main loop. */
17410 continue;
17413 /* If this is the first, we can try a parenthesized
17414 declarator. */
17415 if (first)
17417 bool saved_in_type_id_in_expr_p;
17419 parser->default_arg_ok_p = saved_default_arg_ok_p;
17420 parser->in_declarator_p = saved_in_declarator_p;
17422 /* Consume the `('. */
17423 cp_lexer_consume_token (parser->lexer);
17424 /* Parse the nested declarator. */
17425 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17426 parser->in_type_id_in_expr_p = true;
17427 declarator
17428 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17429 /*parenthesized_p=*/NULL,
17430 member_p, friend_p);
17431 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17432 first = false;
17433 /* Expect a `)'. */
17434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17435 declarator = cp_error_declarator;
17436 if (declarator == cp_error_declarator)
17437 break;
17439 goto handle_declarator;
17441 /* Otherwise, we must be done. */
17442 else
17443 break;
17445 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17446 && token->type == CPP_OPEN_SQUARE
17447 && !cp_next_tokens_can_be_attribute_p (parser))
17449 /* Parse an array-declarator. */
17450 tree bounds, attrs;
17452 if (ctor_dtor_or_conv_p)
17453 *ctor_dtor_or_conv_p = 0;
17455 first = false;
17456 parser->default_arg_ok_p = false;
17457 parser->in_declarator_p = true;
17458 /* Consume the `['. */
17459 cp_lexer_consume_token (parser->lexer);
17460 /* Peek at the next token. */
17461 token = cp_lexer_peek_token (parser->lexer);
17462 /* If the next token is `]', then there is no
17463 constant-expression. */
17464 if (token->type != CPP_CLOSE_SQUARE)
17466 bool non_constant_p;
17467 bounds
17468 = cp_parser_constant_expression (parser,
17469 /*allow_non_constant=*/true,
17470 &non_constant_p);
17471 if (!non_constant_p)
17472 /* OK */;
17473 else if (error_operand_p (bounds))
17474 /* Already gave an error. */;
17475 else if (!parser->in_function_body
17476 || current_binding_level->kind == sk_function_parms)
17478 /* Normally, the array bound must be an integral constant
17479 expression. However, as an extension, we allow VLAs
17480 in function scopes as long as they aren't part of a
17481 parameter declaration. */
17482 cp_parser_error (parser,
17483 "array bound is not an integer constant");
17484 bounds = error_mark_node;
17486 else if (processing_template_decl
17487 && !type_dependent_expression_p (bounds))
17489 /* Remember this wasn't a constant-expression. */
17490 bounds = build_nop (TREE_TYPE (bounds), bounds);
17491 TREE_SIDE_EFFECTS (bounds) = 1;
17494 else
17495 bounds = NULL_TREE;
17496 /* Look for the closing `]'. */
17497 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17499 declarator = cp_error_declarator;
17500 break;
17503 attrs = cp_parser_std_attribute_spec_seq (parser);
17504 declarator = make_array_declarator (declarator, bounds);
17505 declarator->std_attributes = attrs;
17507 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17510 tree qualifying_scope;
17511 tree unqualified_name;
17512 tree attrs;
17513 special_function_kind sfk;
17514 bool abstract_ok;
17515 bool pack_expansion_p = false;
17516 cp_token *declarator_id_start_token;
17518 /* Parse a declarator-id */
17519 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17520 if (abstract_ok)
17522 cp_parser_parse_tentatively (parser);
17524 /* If we see an ellipsis, we should be looking at a
17525 parameter pack. */
17526 if (token->type == CPP_ELLIPSIS)
17528 /* Consume the `...' */
17529 cp_lexer_consume_token (parser->lexer);
17531 pack_expansion_p = true;
17535 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17536 unqualified_name
17537 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17538 qualifying_scope = parser->scope;
17539 if (abstract_ok)
17541 bool okay = false;
17543 if (!unqualified_name && pack_expansion_p)
17545 /* Check whether an error occurred. */
17546 okay = !cp_parser_error_occurred (parser);
17548 /* We already consumed the ellipsis to mark a
17549 parameter pack, but we have no way to report it,
17550 so abort the tentative parse. We will be exiting
17551 immediately anyway. */
17552 cp_parser_abort_tentative_parse (parser);
17554 else
17555 okay = cp_parser_parse_definitely (parser);
17557 if (!okay)
17558 unqualified_name = error_mark_node;
17559 else if (unqualified_name
17560 && (qualifying_scope
17561 || (!identifier_p (unqualified_name))))
17563 cp_parser_error (parser, "expected unqualified-id");
17564 unqualified_name = error_mark_node;
17568 if (!unqualified_name)
17569 return NULL;
17570 if (unqualified_name == error_mark_node)
17572 declarator = cp_error_declarator;
17573 pack_expansion_p = false;
17574 declarator->parameter_pack_p = false;
17575 break;
17578 attrs = cp_parser_std_attribute_spec_seq (parser);
17580 if (qualifying_scope && at_namespace_scope_p ()
17581 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17583 /* In the declaration of a member of a template class
17584 outside of the class itself, the SCOPE will sometimes
17585 be a TYPENAME_TYPE. For example, given:
17587 template <typename T>
17588 int S<T>::R::i = 3;
17590 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17591 this context, we must resolve S<T>::R to an ordinary
17592 type, rather than a typename type.
17594 The reason we normally avoid resolving TYPENAME_TYPEs
17595 is that a specialization of `S' might render
17596 `S<T>::R' not a type. However, if `S' is
17597 specialized, then this `i' will not be used, so there
17598 is no harm in resolving the types here. */
17599 tree type;
17601 /* Resolve the TYPENAME_TYPE. */
17602 type = resolve_typename_type (qualifying_scope,
17603 /*only_current_p=*/false);
17604 /* If that failed, the declarator is invalid. */
17605 if (TREE_CODE (type) == TYPENAME_TYPE)
17607 if (typedef_variant_p (type))
17608 error_at (declarator_id_start_token->location,
17609 "cannot define member of dependent typedef "
17610 "%qT", type);
17611 else
17612 error_at (declarator_id_start_token->location,
17613 "%<%T::%E%> is not a type",
17614 TYPE_CONTEXT (qualifying_scope),
17615 TYPE_IDENTIFIER (qualifying_scope));
17617 qualifying_scope = type;
17620 sfk = sfk_none;
17622 if (unqualified_name)
17624 tree class_type;
17626 if (qualifying_scope
17627 && CLASS_TYPE_P (qualifying_scope))
17628 class_type = qualifying_scope;
17629 else
17630 class_type = current_class_type;
17632 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17634 tree name_type = TREE_TYPE (unqualified_name);
17635 if (class_type && same_type_p (name_type, class_type))
17637 if (qualifying_scope
17638 && CLASSTYPE_USE_TEMPLATE (name_type))
17640 error_at (declarator_id_start_token->location,
17641 "invalid use of constructor as a template");
17642 inform (declarator_id_start_token->location,
17643 "use %<%T::%D%> instead of %<%T::%D%> to "
17644 "name the constructor in a qualified name",
17645 class_type,
17646 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17647 class_type, name_type);
17648 declarator = cp_error_declarator;
17649 break;
17651 else
17652 unqualified_name = constructor_name (class_type);
17654 else
17656 /* We do not attempt to print the declarator
17657 here because we do not have enough
17658 information about its original syntactic
17659 form. */
17660 cp_parser_error (parser, "invalid declarator");
17661 declarator = cp_error_declarator;
17662 break;
17666 if (class_type)
17668 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17669 sfk = sfk_destructor;
17670 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17671 sfk = sfk_conversion;
17672 else if (/* There's no way to declare a constructor
17673 for an anonymous type, even if the type
17674 got a name for linkage purposes. */
17675 !TYPE_WAS_ANONYMOUS (class_type)
17676 /* Handle correctly (c++/19200):
17678 struct S {
17679 struct T{};
17680 friend void S(T);
17683 and also:
17685 namespace N {
17686 void S();
17689 struct S {
17690 friend void N::S();
17691 }; */
17692 && !(friend_p
17693 && class_type != qualifying_scope)
17694 && constructor_name_p (unqualified_name,
17695 class_type))
17697 unqualified_name = constructor_name (class_type);
17698 sfk = sfk_constructor;
17700 else if (is_overloaded_fn (unqualified_name)
17701 && DECL_CONSTRUCTOR_P (get_first_fn
17702 (unqualified_name)))
17703 sfk = sfk_constructor;
17705 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17706 *ctor_dtor_or_conv_p = -1;
17709 declarator = make_id_declarator (qualifying_scope,
17710 unqualified_name,
17711 sfk);
17712 declarator->std_attributes = attrs;
17713 declarator->id_loc = token->location;
17714 declarator->parameter_pack_p = pack_expansion_p;
17716 if (pack_expansion_p)
17717 maybe_warn_variadic_templates ();
17720 handle_declarator:;
17721 scope = get_scope_of_declarator (declarator);
17722 if (scope)
17724 /* Any names that appear after the declarator-id for a
17725 member are looked up in the containing scope. */
17726 if (at_function_scope_p ())
17728 /* But declarations with qualified-ids can't appear in a
17729 function. */
17730 cp_parser_error (parser, "qualified-id in declaration");
17731 declarator = cp_error_declarator;
17732 break;
17734 pushed_scope = push_scope (scope);
17736 parser->in_declarator_p = true;
17737 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17738 || (declarator && declarator->kind == cdk_id))
17739 /* Default args are only allowed on function
17740 declarations. */
17741 parser->default_arg_ok_p = saved_default_arg_ok_p;
17742 else
17743 parser->default_arg_ok_p = false;
17745 first = false;
17747 /* We're done. */
17748 else
17749 break;
17752 /* For an abstract declarator, we might wind up with nothing at this
17753 point. That's an error; the declarator is not optional. */
17754 if (!declarator)
17755 cp_parser_error (parser, "expected declarator");
17757 /* If we entered a scope, we must exit it now. */
17758 if (pushed_scope)
17759 pop_scope (pushed_scope);
17761 parser->default_arg_ok_p = saved_default_arg_ok_p;
17762 parser->in_declarator_p = saved_in_declarator_p;
17764 return declarator;
17767 /* Parse a ptr-operator.
17769 ptr-operator:
17770 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17771 * cv-qualifier-seq [opt]
17773 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17774 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17776 GNU Extension:
17778 ptr-operator:
17779 & cv-qualifier-seq [opt]
17781 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17782 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17783 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17784 filled in with the TYPE containing the member. *CV_QUALS is
17785 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17786 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17787 Note that the tree codes returned by this function have nothing
17788 to do with the types of trees that will be eventually be created
17789 to represent the pointer or reference type being parsed. They are
17790 just constants with suggestive names. */
17791 static enum tree_code
17792 cp_parser_ptr_operator (cp_parser* parser,
17793 tree* type,
17794 cp_cv_quals *cv_quals,
17795 tree *attributes)
17797 enum tree_code code = ERROR_MARK;
17798 cp_token *token;
17799 tree attrs = NULL_TREE;
17801 /* Assume that it's not a pointer-to-member. */
17802 *type = NULL_TREE;
17803 /* And that there are no cv-qualifiers. */
17804 *cv_quals = TYPE_UNQUALIFIED;
17806 /* Peek at the next token. */
17807 token = cp_lexer_peek_token (parser->lexer);
17809 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17810 if (token->type == CPP_MULT)
17811 code = INDIRECT_REF;
17812 else if (token->type == CPP_AND)
17813 code = ADDR_EXPR;
17814 else if ((cxx_dialect != cxx98) &&
17815 token->type == CPP_AND_AND) /* C++0x only */
17816 code = NON_LVALUE_EXPR;
17818 if (code != ERROR_MARK)
17820 /* Consume the `*', `&' or `&&'. */
17821 cp_lexer_consume_token (parser->lexer);
17823 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17824 `&', if we are allowing GNU extensions. (The only qualifier
17825 that can legally appear after `&' is `restrict', but that is
17826 enforced during semantic analysis. */
17827 if (code == INDIRECT_REF
17828 || cp_parser_allow_gnu_extensions_p (parser))
17829 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17831 attrs = cp_parser_std_attribute_spec_seq (parser);
17832 if (attributes != NULL)
17833 *attributes = attrs;
17835 else
17837 /* Try the pointer-to-member case. */
17838 cp_parser_parse_tentatively (parser);
17839 /* Look for the optional `::' operator. */
17840 cp_parser_global_scope_opt (parser,
17841 /*current_scope_valid_p=*/false);
17842 /* Look for the nested-name specifier. */
17843 token = cp_lexer_peek_token (parser->lexer);
17844 cp_parser_nested_name_specifier (parser,
17845 /*typename_keyword_p=*/false,
17846 /*check_dependency_p=*/true,
17847 /*type_p=*/false,
17848 /*is_declaration=*/false);
17849 /* If we found it, and the next token is a `*', then we are
17850 indeed looking at a pointer-to-member operator. */
17851 if (!cp_parser_error_occurred (parser)
17852 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17854 /* Indicate that the `*' operator was used. */
17855 code = INDIRECT_REF;
17857 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17858 error_at (token->location, "%qD is a namespace", parser->scope);
17859 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17860 error_at (token->location, "cannot form pointer to member of "
17861 "non-class %q#T", parser->scope);
17862 else
17864 /* The type of which the member is a member is given by the
17865 current SCOPE. */
17866 *type = parser->scope;
17867 /* The next name will not be qualified. */
17868 parser->scope = NULL_TREE;
17869 parser->qualifying_scope = NULL_TREE;
17870 parser->object_scope = NULL_TREE;
17871 /* Look for optional c++11 attributes. */
17872 attrs = cp_parser_std_attribute_spec_seq (parser);
17873 if (attributes != NULL)
17874 *attributes = attrs;
17875 /* Look for the optional cv-qualifier-seq. */
17876 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17879 /* If that didn't work we don't have a ptr-operator. */
17880 if (!cp_parser_parse_definitely (parser))
17881 cp_parser_error (parser, "expected ptr-operator");
17884 return code;
17887 /* Parse an (optional) cv-qualifier-seq.
17889 cv-qualifier-seq:
17890 cv-qualifier cv-qualifier-seq [opt]
17892 cv-qualifier:
17893 const
17894 volatile
17896 GNU Extension:
17898 cv-qualifier:
17899 __restrict__
17901 Returns a bitmask representing the cv-qualifiers. */
17903 static cp_cv_quals
17904 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17906 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17908 while (true)
17910 cp_token *token;
17911 cp_cv_quals cv_qualifier;
17913 /* Peek at the next token. */
17914 token = cp_lexer_peek_token (parser->lexer);
17915 /* See if it's a cv-qualifier. */
17916 switch (token->keyword)
17918 case RID_CONST:
17919 cv_qualifier = TYPE_QUAL_CONST;
17920 break;
17922 case RID_VOLATILE:
17923 cv_qualifier = TYPE_QUAL_VOLATILE;
17924 break;
17926 case RID_RESTRICT:
17927 cv_qualifier = TYPE_QUAL_RESTRICT;
17928 break;
17930 default:
17931 cv_qualifier = TYPE_UNQUALIFIED;
17932 break;
17935 if (!cv_qualifier)
17936 break;
17938 if (cv_quals & cv_qualifier)
17940 error_at (token->location, "duplicate cv-qualifier");
17941 cp_lexer_purge_token (parser->lexer);
17943 else
17945 cp_lexer_consume_token (parser->lexer);
17946 cv_quals |= cv_qualifier;
17950 return cv_quals;
17953 /* Parse an (optional) ref-qualifier
17955 ref-qualifier:
17959 Returns cp_ref_qualifier representing ref-qualifier. */
17961 static cp_ref_qualifier
17962 cp_parser_ref_qualifier_opt (cp_parser* parser)
17964 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17966 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17967 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17968 return ref_qual;
17970 while (true)
17972 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17973 cp_token *token = cp_lexer_peek_token (parser->lexer);
17975 switch (token->type)
17977 case CPP_AND:
17978 curr_ref_qual = REF_QUAL_LVALUE;
17979 break;
17981 case CPP_AND_AND:
17982 curr_ref_qual = REF_QUAL_RVALUE;
17983 break;
17985 default:
17986 curr_ref_qual = REF_QUAL_NONE;
17987 break;
17990 if (!curr_ref_qual)
17991 break;
17992 else if (ref_qual)
17994 error_at (token->location, "multiple ref-qualifiers");
17995 cp_lexer_purge_token (parser->lexer);
17997 else
17999 ref_qual = curr_ref_qual;
18000 cp_lexer_consume_token (parser->lexer);
18004 return ref_qual;
18007 /* Parse an (optional) virt-specifier-seq.
18009 virt-specifier-seq:
18010 virt-specifier virt-specifier-seq [opt]
18012 virt-specifier:
18013 override
18014 final
18016 Returns a bitmask representing the virt-specifiers. */
18018 static cp_virt_specifiers
18019 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18021 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18023 while (true)
18025 cp_token *token;
18026 cp_virt_specifiers virt_specifier;
18028 /* Peek at the next token. */
18029 token = cp_lexer_peek_token (parser->lexer);
18030 /* See if it's a virt-specifier-qualifier. */
18031 if (token->type != CPP_NAME)
18032 break;
18033 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18035 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18036 virt_specifier = VIRT_SPEC_OVERRIDE;
18038 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18040 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18041 virt_specifier = VIRT_SPEC_FINAL;
18043 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18045 virt_specifier = VIRT_SPEC_FINAL;
18047 else
18048 break;
18050 if (virt_specifiers & virt_specifier)
18052 error_at (token->location, "duplicate virt-specifier");
18053 cp_lexer_purge_token (parser->lexer);
18055 else
18057 cp_lexer_consume_token (parser->lexer);
18058 virt_specifiers |= virt_specifier;
18061 return virt_specifiers;
18064 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18065 is in scope even though it isn't real. */
18067 void
18068 inject_this_parameter (tree ctype, cp_cv_quals quals)
18070 tree this_parm;
18072 if (current_class_ptr)
18074 /* We don't clear this between NSDMIs. Is it already what we want? */
18075 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18076 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18077 && cp_type_quals (type) == quals)
18078 return;
18081 this_parm = build_this_parm (ctype, quals);
18082 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18083 current_class_ptr = NULL_TREE;
18084 current_class_ref
18085 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18086 current_class_ptr = this_parm;
18089 /* Return true iff our current scope is a non-static data member
18090 initializer. */
18092 bool
18093 parsing_nsdmi (void)
18095 /* We recognize NSDMI context by the context-less 'this' pointer set up
18096 by the function above. */
18097 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18098 return true;
18099 return false;
18102 /* Parse a late-specified return type, if any. This is not a separate
18103 non-terminal, but part of a function declarator, which looks like
18105 -> trailing-type-specifier-seq abstract-declarator(opt)
18107 Returns the type indicated by the type-id.
18109 In addition to this this parses any queued up omp declare simd
18110 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18112 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18113 function. */
18115 static tree
18116 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18117 cp_cv_quals quals)
18119 cp_token *token;
18120 tree type = NULL_TREE;
18121 bool declare_simd_p = (parser->omp_declare_simd
18122 && declarator
18123 && declarator->kind == cdk_id);
18125 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18126 && declarator && declarator->kind == cdk_id);
18128 /* Peek at the next token. */
18129 token = cp_lexer_peek_token (parser->lexer);
18130 /* A late-specified return type is indicated by an initial '->'. */
18131 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18132 return NULL_TREE;
18134 tree save_ccp = current_class_ptr;
18135 tree save_ccr = current_class_ref;
18136 if (quals >= 0)
18138 /* DR 1207: 'this' is in scope in the trailing return type. */
18139 inject_this_parameter (current_class_type, quals);
18142 if (token->type == CPP_DEREF)
18144 /* Consume the ->. */
18145 cp_lexer_consume_token (parser->lexer);
18147 type = cp_parser_trailing_type_id (parser);
18150 if (cilk_simd_fn_vector_p)
18151 declarator->std_attributes
18152 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18153 declarator->std_attributes);
18154 if (declare_simd_p)
18155 declarator->std_attributes
18156 = cp_parser_late_parsing_omp_declare_simd (parser,
18157 declarator->std_attributes);
18159 if (quals >= 0)
18161 current_class_ptr = save_ccp;
18162 current_class_ref = save_ccr;
18165 return type;
18168 /* Parse a declarator-id.
18170 declarator-id:
18171 id-expression
18172 :: [opt] nested-name-specifier [opt] type-name
18174 In the `id-expression' case, the value returned is as for
18175 cp_parser_id_expression if the id-expression was an unqualified-id.
18176 If the id-expression was a qualified-id, then a SCOPE_REF is
18177 returned. The first operand is the scope (either a NAMESPACE_DECL
18178 or TREE_TYPE), but the second is still just a representation of an
18179 unqualified-id. */
18181 static tree
18182 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18184 tree id;
18185 /* The expression must be an id-expression. Assume that qualified
18186 names are the names of types so that:
18188 template <class T>
18189 int S<T>::R::i = 3;
18191 will work; we must treat `S<T>::R' as the name of a type.
18192 Similarly, assume that qualified names are templates, where
18193 required, so that:
18195 template <class T>
18196 int S<T>::R<T>::i = 3;
18198 will work, too. */
18199 id = cp_parser_id_expression (parser,
18200 /*template_keyword_p=*/false,
18201 /*check_dependency_p=*/false,
18202 /*template_p=*/NULL,
18203 /*declarator_p=*/true,
18204 optional_p);
18205 if (id && BASELINK_P (id))
18206 id = BASELINK_FUNCTIONS (id);
18207 return id;
18210 /* Parse a type-id.
18212 type-id:
18213 type-specifier-seq abstract-declarator [opt]
18215 Returns the TYPE specified. */
18217 static tree
18218 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18219 bool is_trailing_return)
18221 cp_decl_specifier_seq type_specifier_seq;
18222 cp_declarator *abstract_declarator;
18224 /* Parse the type-specifier-seq. */
18225 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18226 is_trailing_return,
18227 &type_specifier_seq);
18228 if (type_specifier_seq.type == error_mark_node)
18229 return error_mark_node;
18231 /* There might or might not be an abstract declarator. */
18232 cp_parser_parse_tentatively (parser);
18233 /* Look for the declarator. */
18234 abstract_declarator
18235 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18236 /*parenthesized_p=*/NULL,
18237 /*member_p=*/false,
18238 /*friend_p=*/false);
18239 /* Check to see if there really was a declarator. */
18240 if (!cp_parser_parse_definitely (parser))
18241 abstract_declarator = NULL;
18243 if (type_specifier_seq.type
18244 /* None of the valid uses of 'auto' in C++14 involve the type-id
18245 nonterminal, but it is valid in a trailing-return-type. */
18246 && !(cxx_dialect >= cxx14 && is_trailing_return)
18247 && type_uses_auto (type_specifier_seq.type))
18249 /* A type-id with type 'auto' is only ok if the abstract declarator
18250 is a function declarator with a late-specified return type. */
18251 if (abstract_declarator
18252 && abstract_declarator->kind == cdk_function
18253 && abstract_declarator->u.function.late_return_type)
18254 /* OK */;
18255 else
18257 error ("invalid use of %<auto%>");
18258 return error_mark_node;
18262 return groktypename (&type_specifier_seq, abstract_declarator,
18263 is_template_arg);
18266 static tree cp_parser_type_id (cp_parser *parser)
18268 return cp_parser_type_id_1 (parser, false, false);
18271 static tree cp_parser_template_type_arg (cp_parser *parser)
18273 tree r;
18274 const char *saved_message = parser->type_definition_forbidden_message;
18275 parser->type_definition_forbidden_message
18276 = G_("types may not be defined in template arguments");
18277 r = cp_parser_type_id_1 (parser, true, false);
18278 parser->type_definition_forbidden_message = saved_message;
18279 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18281 error ("invalid use of %<auto%> in template argument");
18282 r = error_mark_node;
18284 return r;
18287 static tree cp_parser_trailing_type_id (cp_parser *parser)
18289 return cp_parser_type_id_1 (parser, false, true);
18292 /* Parse a type-specifier-seq.
18294 type-specifier-seq:
18295 type-specifier type-specifier-seq [opt]
18297 GNU extension:
18299 type-specifier-seq:
18300 attributes type-specifier-seq [opt]
18302 If IS_DECLARATION is true, we are at the start of a "condition" or
18303 exception-declaration, so we might be followed by a declarator-id.
18305 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18306 i.e. we've just seen "->".
18308 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18310 static void
18311 cp_parser_type_specifier_seq (cp_parser* parser,
18312 bool is_declaration,
18313 bool is_trailing_return,
18314 cp_decl_specifier_seq *type_specifier_seq)
18316 bool seen_type_specifier = false;
18317 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18318 cp_token *start_token = NULL;
18320 /* Clear the TYPE_SPECIFIER_SEQ. */
18321 clear_decl_specs (type_specifier_seq);
18323 /* In the context of a trailing return type, enum E { } is an
18324 elaborated-type-specifier followed by a function-body, not an
18325 enum-specifier. */
18326 if (is_trailing_return)
18327 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18329 /* Parse the type-specifiers and attributes. */
18330 while (true)
18332 tree type_specifier;
18333 bool is_cv_qualifier;
18335 /* Check for attributes first. */
18336 if (cp_next_tokens_can_be_attribute_p (parser))
18338 type_specifier_seq->attributes =
18339 chainon (type_specifier_seq->attributes,
18340 cp_parser_attributes_opt (parser));
18341 continue;
18344 /* record the token of the beginning of the type specifier seq,
18345 for error reporting purposes*/
18346 if (!start_token)
18347 start_token = cp_lexer_peek_token (parser->lexer);
18349 /* Look for the type-specifier. */
18350 type_specifier = cp_parser_type_specifier (parser,
18351 flags,
18352 type_specifier_seq,
18353 /*is_declaration=*/false,
18354 NULL,
18355 &is_cv_qualifier);
18356 if (!type_specifier)
18358 /* If the first type-specifier could not be found, this is not a
18359 type-specifier-seq at all. */
18360 if (!seen_type_specifier)
18362 /* Set in_declarator_p to avoid skipping to the semicolon. */
18363 int in_decl = parser->in_declarator_p;
18364 parser->in_declarator_p = true;
18366 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18367 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18368 cp_parser_error (parser, "expected type-specifier");
18370 parser->in_declarator_p = in_decl;
18372 type_specifier_seq->type = error_mark_node;
18373 return;
18375 /* If subsequent type-specifiers could not be found, the
18376 type-specifier-seq is complete. */
18377 break;
18380 seen_type_specifier = true;
18381 /* The standard says that a condition can be:
18383 type-specifier-seq declarator = assignment-expression
18385 However, given:
18387 struct S {};
18388 if (int S = ...)
18390 we should treat the "S" as a declarator, not as a
18391 type-specifier. The standard doesn't say that explicitly for
18392 type-specifier-seq, but it does say that for
18393 decl-specifier-seq in an ordinary declaration. Perhaps it
18394 would be clearer just to allow a decl-specifier-seq here, and
18395 then add a semantic restriction that if any decl-specifiers
18396 that are not type-specifiers appear, the program is invalid. */
18397 if (is_declaration && !is_cv_qualifier)
18398 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18402 /* Return whether the function currently being declared has an associated
18403 template parameter list. */
18405 static bool
18406 function_being_declared_is_template_p (cp_parser* parser)
18408 if (!current_template_parms || processing_template_parmlist)
18409 return false;
18411 if (parser->implicit_template_scope)
18412 return true;
18414 if (at_class_scope_p ()
18415 && TYPE_BEING_DEFINED (current_class_type))
18416 return parser->num_template_parameter_lists != 0;
18418 return ((int) parser->num_template_parameter_lists > template_class_depth
18419 (current_class_type));
18422 /* Parse a parameter-declaration-clause.
18424 parameter-declaration-clause:
18425 parameter-declaration-list [opt] ... [opt]
18426 parameter-declaration-list , ...
18428 Returns a representation for the parameter declarations. A return
18429 value of NULL indicates a parameter-declaration-clause consisting
18430 only of an ellipsis. */
18432 static tree
18433 cp_parser_parameter_declaration_clause (cp_parser* parser)
18435 tree parameters;
18436 cp_token *token;
18437 bool ellipsis_p;
18438 bool is_error;
18440 struct cleanup {
18441 cp_parser* parser;
18442 int auto_is_implicit_function_template_parm_p;
18443 ~cleanup() {
18444 parser->auto_is_implicit_function_template_parm_p
18445 = auto_is_implicit_function_template_parm_p;
18447 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18449 (void) cleanup;
18451 if (!processing_specialization
18452 && !processing_template_parmlist
18453 && !processing_explicit_instantiation)
18454 if (!current_function_decl
18455 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18456 parser->auto_is_implicit_function_template_parm_p = true;
18458 /* Peek at the next token. */
18459 token = cp_lexer_peek_token (parser->lexer);
18460 /* Check for trivial parameter-declaration-clauses. */
18461 if (token->type == CPP_ELLIPSIS)
18463 /* Consume the `...' token. */
18464 cp_lexer_consume_token (parser->lexer);
18465 return NULL_TREE;
18467 else if (token->type == CPP_CLOSE_PAREN)
18468 /* There are no parameters. */
18470 #ifndef NO_IMPLICIT_EXTERN_C
18471 if (in_system_header_at (input_location)
18472 && current_class_type == NULL
18473 && current_lang_name == lang_name_c)
18474 return NULL_TREE;
18475 else
18476 #endif
18477 return void_list_node;
18479 /* Check for `(void)', too, which is a special case. */
18480 else if (token->keyword == RID_VOID
18481 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18482 == CPP_CLOSE_PAREN))
18484 /* Consume the `void' token. */
18485 cp_lexer_consume_token (parser->lexer);
18486 /* There are no parameters. */
18487 return void_list_node;
18490 /* Parse the parameter-declaration-list. */
18491 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18492 /* If a parse error occurred while parsing the
18493 parameter-declaration-list, then the entire
18494 parameter-declaration-clause is erroneous. */
18495 if (is_error)
18496 return NULL;
18498 /* Peek at the next token. */
18499 token = cp_lexer_peek_token (parser->lexer);
18500 /* If it's a `,', the clause should terminate with an ellipsis. */
18501 if (token->type == CPP_COMMA)
18503 /* Consume the `,'. */
18504 cp_lexer_consume_token (parser->lexer);
18505 /* Expect an ellipsis. */
18506 ellipsis_p
18507 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18509 /* It might also be `...' if the optional trailing `,' was
18510 omitted. */
18511 else if (token->type == CPP_ELLIPSIS)
18513 /* Consume the `...' token. */
18514 cp_lexer_consume_token (parser->lexer);
18515 /* And remember that we saw it. */
18516 ellipsis_p = true;
18518 else
18519 ellipsis_p = false;
18521 /* Finish the parameter list. */
18522 if (!ellipsis_p)
18523 parameters = chainon (parameters, void_list_node);
18525 return parameters;
18528 /* Parse a parameter-declaration-list.
18530 parameter-declaration-list:
18531 parameter-declaration
18532 parameter-declaration-list , parameter-declaration
18534 Returns a representation of the parameter-declaration-list, as for
18535 cp_parser_parameter_declaration_clause. However, the
18536 `void_list_node' is never appended to the list. Upon return,
18537 *IS_ERROR will be true iff an error occurred. */
18539 static tree
18540 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18542 tree parameters = NULL_TREE;
18543 tree *tail = &parameters;
18544 bool saved_in_unbraced_linkage_specification_p;
18545 int index = 0;
18547 /* Assume all will go well. */
18548 *is_error = false;
18549 /* The special considerations that apply to a function within an
18550 unbraced linkage specifications do not apply to the parameters
18551 to the function. */
18552 saved_in_unbraced_linkage_specification_p
18553 = parser->in_unbraced_linkage_specification_p;
18554 parser->in_unbraced_linkage_specification_p = false;
18556 /* Look for more parameters. */
18557 while (true)
18559 cp_parameter_declarator *parameter;
18560 tree decl = error_mark_node;
18561 bool parenthesized_p = false;
18562 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18563 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18564 (current_template_parms)) : 0);
18566 /* Parse the parameter. */
18567 parameter
18568 = cp_parser_parameter_declaration (parser,
18569 /*template_parm_p=*/false,
18570 &parenthesized_p);
18572 /* We don't know yet if the enclosing context is deprecated, so wait
18573 and warn in grokparms if appropriate. */
18574 deprecated_state = DEPRECATED_SUPPRESS;
18576 if (parameter)
18578 /* If a function parameter pack was specified and an implicit template
18579 parameter was introduced during cp_parser_parameter_declaration,
18580 change any implicit parameters introduced into packs. */
18581 if (parser->implicit_template_parms
18582 && parameter->declarator
18583 && parameter->declarator->parameter_pack_p)
18585 int latest_template_parm_idx = TREE_VEC_LENGTH
18586 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18588 if (latest_template_parm_idx != template_parm_idx)
18589 parameter->decl_specifiers.type = convert_generic_types_to_packs
18590 (parameter->decl_specifiers.type,
18591 template_parm_idx, latest_template_parm_idx);
18594 decl = grokdeclarator (parameter->declarator,
18595 &parameter->decl_specifiers,
18596 PARM,
18597 parameter->default_argument != NULL_TREE,
18598 &parameter->decl_specifiers.attributes);
18601 deprecated_state = DEPRECATED_NORMAL;
18603 /* If a parse error occurred parsing the parameter declaration,
18604 then the entire parameter-declaration-list is erroneous. */
18605 if (decl == error_mark_node)
18607 *is_error = true;
18608 parameters = error_mark_node;
18609 break;
18612 if (parameter->decl_specifiers.attributes)
18613 cplus_decl_attributes (&decl,
18614 parameter->decl_specifiers.attributes,
18616 if (DECL_NAME (decl))
18617 decl = pushdecl (decl);
18619 if (decl != error_mark_node)
18621 retrofit_lang_decl (decl);
18622 DECL_PARM_INDEX (decl) = ++index;
18623 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18626 /* Add the new parameter to the list. */
18627 *tail = build_tree_list (parameter->default_argument, decl);
18628 tail = &TREE_CHAIN (*tail);
18630 /* Peek at the next token. */
18631 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18632 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18633 /* These are for Objective-C++ */
18634 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18635 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18636 /* The parameter-declaration-list is complete. */
18637 break;
18638 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18640 cp_token *token;
18642 /* Peek at the next token. */
18643 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18644 /* If it's an ellipsis, then the list is complete. */
18645 if (token->type == CPP_ELLIPSIS)
18646 break;
18647 /* Otherwise, there must be more parameters. Consume the
18648 `,'. */
18649 cp_lexer_consume_token (parser->lexer);
18650 /* When parsing something like:
18652 int i(float f, double d)
18654 we can tell after seeing the declaration for "f" that we
18655 are not looking at an initialization of a variable "i",
18656 but rather at the declaration of a function "i".
18658 Due to the fact that the parsing of template arguments
18659 (as specified to a template-id) requires backtracking we
18660 cannot use this technique when inside a template argument
18661 list. */
18662 if (!parser->in_template_argument_list_p
18663 && !parser->in_type_id_in_expr_p
18664 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18665 /* However, a parameter-declaration of the form
18666 "float(f)" (which is a valid declaration of a
18667 parameter "f") can also be interpreted as an
18668 expression (the conversion of "f" to "float"). */
18669 && !parenthesized_p)
18670 cp_parser_commit_to_tentative_parse (parser);
18672 else
18674 cp_parser_error (parser, "expected %<,%> or %<...%>");
18675 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18676 cp_parser_skip_to_closing_parenthesis (parser,
18677 /*recovering=*/true,
18678 /*or_comma=*/false,
18679 /*consume_paren=*/false);
18680 break;
18684 parser->in_unbraced_linkage_specification_p
18685 = saved_in_unbraced_linkage_specification_p;
18687 /* Reset implicit_template_scope if we are about to leave the function
18688 parameter list that introduced it. Note that for out-of-line member
18689 definitions, there will be one or more class scopes before we get to
18690 the template parameter scope. */
18692 if (cp_binding_level *its = parser->implicit_template_scope)
18693 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18695 while (maybe_its->kind == sk_class)
18696 maybe_its = maybe_its->level_chain;
18697 if (maybe_its == its)
18699 parser->implicit_template_parms = 0;
18700 parser->implicit_template_scope = 0;
18704 return parameters;
18707 /* Parse a parameter declaration.
18709 parameter-declaration:
18710 decl-specifier-seq ... [opt] declarator
18711 decl-specifier-seq declarator = assignment-expression
18712 decl-specifier-seq ... [opt] abstract-declarator [opt]
18713 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18715 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18716 declares a template parameter. (In that case, a non-nested `>'
18717 token encountered during the parsing of the assignment-expression
18718 is not interpreted as a greater-than operator.)
18720 Returns a representation of the parameter, or NULL if an error
18721 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18722 true iff the declarator is of the form "(p)". */
18724 static cp_parameter_declarator *
18725 cp_parser_parameter_declaration (cp_parser *parser,
18726 bool template_parm_p,
18727 bool *parenthesized_p)
18729 int declares_class_or_enum;
18730 cp_decl_specifier_seq decl_specifiers;
18731 cp_declarator *declarator;
18732 tree default_argument;
18733 cp_token *token = NULL, *declarator_token_start = NULL;
18734 const char *saved_message;
18736 /* In a template parameter, `>' is not an operator.
18738 [temp.param]
18740 When parsing a default template-argument for a non-type
18741 template-parameter, the first non-nested `>' is taken as the end
18742 of the template parameter-list rather than a greater-than
18743 operator. */
18745 /* Type definitions may not appear in parameter types. */
18746 saved_message = parser->type_definition_forbidden_message;
18747 parser->type_definition_forbidden_message
18748 = G_("types may not be defined in parameter types");
18750 /* Parse the declaration-specifiers. */
18751 cp_parser_decl_specifier_seq (parser,
18752 CP_PARSER_FLAGS_NONE,
18753 &decl_specifiers,
18754 &declares_class_or_enum);
18756 /* Complain about missing 'typename' or other invalid type names. */
18757 if (!decl_specifiers.any_type_specifiers_p
18758 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18759 decl_specifiers.type = error_mark_node;
18761 /* If an error occurred, there's no reason to attempt to parse the
18762 rest of the declaration. */
18763 if (cp_parser_error_occurred (parser))
18765 parser->type_definition_forbidden_message = saved_message;
18766 return NULL;
18769 /* Peek at the next token. */
18770 token = cp_lexer_peek_token (parser->lexer);
18772 /* If the next token is a `)', `,', `=', `>', or `...', then there
18773 is no declarator. However, when variadic templates are enabled,
18774 there may be a declarator following `...'. */
18775 if (token->type == CPP_CLOSE_PAREN
18776 || token->type == CPP_COMMA
18777 || token->type == CPP_EQ
18778 || token->type == CPP_GREATER)
18780 declarator = NULL;
18781 if (parenthesized_p)
18782 *parenthesized_p = false;
18784 /* Otherwise, there should be a declarator. */
18785 else
18787 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18788 parser->default_arg_ok_p = false;
18790 /* After seeing a decl-specifier-seq, if the next token is not a
18791 "(", there is no possibility that the code is a valid
18792 expression. Therefore, if parsing tentatively, we commit at
18793 this point. */
18794 if (!parser->in_template_argument_list_p
18795 /* In an expression context, having seen:
18797 (int((char ...
18799 we cannot be sure whether we are looking at a
18800 function-type (taking a "char" as a parameter) or a cast
18801 of some object of type "char" to "int". */
18802 && !parser->in_type_id_in_expr_p
18803 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18804 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18805 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18806 cp_parser_commit_to_tentative_parse (parser);
18807 /* Parse the declarator. */
18808 declarator_token_start = token;
18809 declarator = cp_parser_declarator (parser,
18810 CP_PARSER_DECLARATOR_EITHER,
18811 /*ctor_dtor_or_conv_p=*/NULL,
18812 parenthesized_p,
18813 /*member_p=*/false,
18814 /*friend_p=*/false);
18815 parser->default_arg_ok_p = saved_default_arg_ok_p;
18816 /* After the declarator, allow more attributes. */
18817 decl_specifiers.attributes
18818 = chainon (decl_specifiers.attributes,
18819 cp_parser_attributes_opt (parser));
18822 /* If the next token is an ellipsis, and we have not seen a
18823 declarator name, and the type of the declarator contains parameter
18824 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18825 a parameter pack expansion expression. Otherwise, leave the
18826 ellipsis for a C-style variadic function. */
18827 token = cp_lexer_peek_token (parser->lexer);
18828 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18830 tree type = decl_specifiers.type;
18832 if (type && DECL_P (type))
18833 type = TREE_TYPE (type);
18835 if (type
18836 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18837 && declarator_can_be_parameter_pack (declarator)
18838 && (!declarator || !declarator->parameter_pack_p)
18839 && uses_parameter_packs (type))
18841 /* Consume the `...'. */
18842 cp_lexer_consume_token (parser->lexer);
18843 maybe_warn_variadic_templates ();
18845 /* Build a pack expansion type */
18846 if (declarator)
18847 declarator->parameter_pack_p = true;
18848 else
18849 decl_specifiers.type = make_pack_expansion (type);
18853 /* The restriction on defining new types applies only to the type
18854 of the parameter, not to the default argument. */
18855 parser->type_definition_forbidden_message = saved_message;
18857 /* If the next token is `=', then process a default argument. */
18858 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18860 token = cp_lexer_peek_token (parser->lexer);
18861 /* If we are defining a class, then the tokens that make up the
18862 default argument must be saved and processed later. */
18863 if (!template_parm_p && at_class_scope_p ()
18864 && TYPE_BEING_DEFINED (current_class_type)
18865 && !LAMBDA_TYPE_P (current_class_type))
18866 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18867 /* Outside of a class definition, we can just parse the
18868 assignment-expression. */
18869 else
18870 default_argument
18871 = cp_parser_default_argument (parser, template_parm_p);
18873 if (!parser->default_arg_ok_p)
18875 if (flag_permissive)
18876 warning (0, "deprecated use of default argument for parameter of non-function");
18877 else
18879 error_at (token->location,
18880 "default arguments are only "
18881 "permitted for function parameters");
18882 default_argument = NULL_TREE;
18885 else if ((declarator && declarator->parameter_pack_p)
18886 || (decl_specifiers.type
18887 && PACK_EXPANSION_P (decl_specifiers.type)))
18889 /* Find the name of the parameter pack. */
18890 cp_declarator *id_declarator = declarator;
18891 while (id_declarator && id_declarator->kind != cdk_id)
18892 id_declarator = id_declarator->declarator;
18894 if (id_declarator && id_declarator->kind == cdk_id)
18895 error_at (declarator_token_start->location,
18896 template_parm_p
18897 ? G_("template parameter pack %qD "
18898 "cannot have a default argument")
18899 : G_("parameter pack %qD cannot have "
18900 "a default argument"),
18901 id_declarator->u.id.unqualified_name);
18902 else
18903 error_at (declarator_token_start->location,
18904 template_parm_p
18905 ? G_("template parameter pack cannot have "
18906 "a default argument")
18907 : G_("parameter pack cannot have a "
18908 "default argument"));
18910 default_argument = NULL_TREE;
18913 else
18914 default_argument = NULL_TREE;
18916 return make_parameter_declarator (&decl_specifiers,
18917 declarator,
18918 default_argument);
18921 /* Parse a default argument and return it.
18923 TEMPLATE_PARM_P is true if this is a default argument for a
18924 non-type template parameter. */
18925 static tree
18926 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18928 tree default_argument = NULL_TREE;
18929 bool saved_greater_than_is_operator_p;
18930 bool saved_local_variables_forbidden_p;
18931 bool non_constant_p, is_direct_init;
18933 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18934 set correctly. */
18935 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18936 parser->greater_than_is_operator_p = !template_parm_p;
18937 /* Local variable names (and the `this' keyword) may not
18938 appear in a default argument. */
18939 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18940 parser->local_variables_forbidden_p = true;
18941 /* Parse the assignment-expression. */
18942 if (template_parm_p)
18943 push_deferring_access_checks (dk_no_deferred);
18944 tree saved_class_ptr = NULL_TREE;
18945 tree saved_class_ref = NULL_TREE;
18946 /* The "this" pointer is not valid in a default argument. */
18947 if (cfun)
18949 saved_class_ptr = current_class_ptr;
18950 cp_function_chain->x_current_class_ptr = NULL_TREE;
18951 saved_class_ref = current_class_ref;
18952 cp_function_chain->x_current_class_ref = NULL_TREE;
18954 default_argument
18955 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18956 /* Restore the "this" pointer. */
18957 if (cfun)
18959 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18960 cp_function_chain->x_current_class_ref = saved_class_ref;
18962 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18963 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18964 if (template_parm_p)
18965 pop_deferring_access_checks ();
18966 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18967 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18969 return default_argument;
18972 /* Parse a function-body.
18974 function-body:
18975 compound_statement */
18977 static void
18978 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18980 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18983 /* Parse a ctor-initializer-opt followed by a function-body. Return
18984 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18985 is true we are parsing a function-try-block. */
18987 static bool
18988 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18989 bool in_function_try_block)
18991 tree body, list;
18992 bool ctor_initializer_p;
18993 const bool check_body_p =
18994 DECL_CONSTRUCTOR_P (current_function_decl)
18995 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18996 tree last = NULL;
18998 /* Begin the function body. */
18999 body = begin_function_body ();
19000 /* Parse the optional ctor-initializer. */
19001 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19003 /* If we're parsing a constexpr constructor definition, we need
19004 to check that the constructor body is indeed empty. However,
19005 before we get to cp_parser_function_body lot of junk has been
19006 generated, so we can't just check that we have an empty block.
19007 Rather we take a snapshot of the outermost block, and check whether
19008 cp_parser_function_body changed its state. */
19009 if (check_body_p)
19011 list = cur_stmt_list;
19012 if (STATEMENT_LIST_TAIL (list))
19013 last = STATEMENT_LIST_TAIL (list)->stmt;
19015 /* Parse the function-body. */
19016 cp_parser_function_body (parser, in_function_try_block);
19017 if (check_body_p)
19018 check_constexpr_ctor_body (last, list, /*complain=*/true);
19019 /* Finish the function body. */
19020 finish_function_body (body);
19022 return ctor_initializer_p;
19025 /* Parse an initializer.
19027 initializer:
19028 = initializer-clause
19029 ( expression-list )
19031 Returns an expression representing the initializer. If no
19032 initializer is present, NULL_TREE is returned.
19034 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19035 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19036 set to TRUE if there is no initializer present. If there is an
19037 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19038 is set to true; otherwise it is set to false. */
19040 static tree
19041 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19042 bool* non_constant_p)
19044 cp_token *token;
19045 tree init;
19047 /* Peek at the next token. */
19048 token = cp_lexer_peek_token (parser->lexer);
19050 /* Let our caller know whether or not this initializer was
19051 parenthesized. */
19052 *is_direct_init = (token->type != CPP_EQ);
19053 /* Assume that the initializer is constant. */
19054 *non_constant_p = false;
19056 if (token->type == CPP_EQ)
19058 /* Consume the `='. */
19059 cp_lexer_consume_token (parser->lexer);
19060 /* Parse the initializer-clause. */
19061 init = cp_parser_initializer_clause (parser, non_constant_p);
19063 else if (token->type == CPP_OPEN_PAREN)
19065 vec<tree, va_gc> *vec;
19066 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19067 /*cast_p=*/false,
19068 /*allow_expansion_p=*/true,
19069 non_constant_p);
19070 if (vec == NULL)
19071 return error_mark_node;
19072 init = build_tree_list_vec (vec);
19073 release_tree_vector (vec);
19075 else if (token->type == CPP_OPEN_BRACE)
19077 cp_lexer_set_source_position (parser->lexer);
19078 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19079 init = cp_parser_braced_list (parser, non_constant_p);
19080 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19082 else
19084 /* Anything else is an error. */
19085 cp_parser_error (parser, "expected initializer");
19086 init = error_mark_node;
19089 return init;
19092 /* Parse an initializer-clause.
19094 initializer-clause:
19095 assignment-expression
19096 braced-init-list
19098 Returns an expression representing the initializer.
19100 If the `assignment-expression' production is used the value
19101 returned is simply a representation for the expression.
19103 Otherwise, calls cp_parser_braced_list. */
19105 static tree
19106 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19108 tree initializer;
19110 /* Assume the expression is constant. */
19111 *non_constant_p = false;
19113 /* If it is not a `{', then we are looking at an
19114 assignment-expression. */
19115 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19117 initializer
19118 = cp_parser_constant_expression (parser,
19119 /*allow_non_constant_p=*/true,
19120 non_constant_p);
19122 else
19123 initializer = cp_parser_braced_list (parser, non_constant_p);
19125 return initializer;
19128 /* Parse a brace-enclosed initializer list.
19130 braced-init-list:
19131 { initializer-list , [opt] }
19134 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19135 the elements of the initializer-list (or NULL, if the last
19136 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19137 NULL_TREE. There is no way to detect whether or not the optional
19138 trailing `,' was provided. NON_CONSTANT_P is as for
19139 cp_parser_initializer. */
19141 static tree
19142 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19144 tree initializer;
19146 /* Consume the `{' token. */
19147 cp_lexer_consume_token (parser->lexer);
19148 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19149 initializer = make_node (CONSTRUCTOR);
19150 /* If it's not a `}', then there is a non-trivial initializer. */
19151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19153 /* Parse the initializer list. */
19154 CONSTRUCTOR_ELTS (initializer)
19155 = cp_parser_initializer_list (parser, non_constant_p);
19156 /* A trailing `,' token is allowed. */
19157 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19158 cp_lexer_consume_token (parser->lexer);
19160 else
19161 *non_constant_p = false;
19162 /* Now, there should be a trailing `}'. */
19163 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19164 TREE_TYPE (initializer) = init_list_type_node;
19165 return initializer;
19168 /* Consume tokens up to, and including, the next non-nested closing `]'.
19169 Returns true iff we found a closing `]'. */
19171 static bool
19172 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19174 unsigned square_depth = 0;
19176 while (true)
19178 cp_token * token = cp_lexer_peek_token (parser->lexer);
19180 switch (token->type)
19182 case CPP_EOF:
19183 case CPP_PRAGMA_EOL:
19184 /* If we've run out of tokens, then there is no closing `]'. */
19185 return false;
19187 case CPP_OPEN_SQUARE:
19188 ++square_depth;
19189 break;
19191 case CPP_CLOSE_SQUARE:
19192 if (!square_depth--)
19194 cp_lexer_consume_token (parser->lexer);
19195 return true;
19197 break;
19199 default:
19200 break;
19203 /* Consume the token. */
19204 cp_lexer_consume_token (parser->lexer);
19208 /* Return true if we are looking at an array-designator, false otherwise. */
19210 static bool
19211 cp_parser_array_designator_p (cp_parser *parser)
19213 /* Consume the `['. */
19214 cp_lexer_consume_token (parser->lexer);
19216 cp_lexer_save_tokens (parser->lexer);
19218 /* Skip tokens until the next token is a closing square bracket.
19219 If we find the closing `]', and the next token is a `=', then
19220 we are looking at an array designator. */
19221 bool array_designator_p
19222 = (cp_parser_skip_to_closing_square_bracket (parser)
19223 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19225 /* Roll back the tokens we skipped. */
19226 cp_lexer_rollback_tokens (parser->lexer);
19228 return array_designator_p;
19231 /* Parse an initializer-list.
19233 initializer-list:
19234 initializer-clause ... [opt]
19235 initializer-list , initializer-clause ... [opt]
19237 GNU Extension:
19239 initializer-list:
19240 designation initializer-clause ...[opt]
19241 initializer-list , designation initializer-clause ...[opt]
19243 designation:
19244 . identifier =
19245 identifier :
19246 [ constant-expression ] =
19248 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19249 for the initializer. If the INDEX of the elt is non-NULL, it is the
19250 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19251 as for cp_parser_initializer. */
19253 static vec<constructor_elt, va_gc> *
19254 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19256 vec<constructor_elt, va_gc> *v = NULL;
19258 /* Assume all of the expressions are constant. */
19259 *non_constant_p = false;
19261 /* Parse the rest of the list. */
19262 while (true)
19264 cp_token *token;
19265 tree designator;
19266 tree initializer;
19267 bool clause_non_constant_p;
19269 /* If the next token is an identifier and the following one is a
19270 colon, we are looking at the GNU designated-initializer
19271 syntax. */
19272 if (cp_parser_allow_gnu_extensions_p (parser)
19273 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19274 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19276 /* Warn the user that they are using an extension. */
19277 pedwarn (input_location, OPT_Wpedantic,
19278 "ISO C++ does not allow designated initializers");
19279 /* Consume the identifier. */
19280 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19281 /* Consume the `:'. */
19282 cp_lexer_consume_token (parser->lexer);
19284 /* Also handle the C99 syntax, '. id ='. */
19285 else if (cp_parser_allow_gnu_extensions_p (parser)
19286 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19287 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19288 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19290 /* Warn the user that they are using an extension. */
19291 pedwarn (input_location, OPT_Wpedantic,
19292 "ISO C++ does not allow C99 designated initializers");
19293 /* Consume the `.'. */
19294 cp_lexer_consume_token (parser->lexer);
19295 /* Consume the identifier. */
19296 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19297 /* Consume the `='. */
19298 cp_lexer_consume_token (parser->lexer);
19300 /* Also handle C99 array designators, '[ const ] ='. */
19301 else if (cp_parser_allow_gnu_extensions_p (parser)
19302 && !c_dialect_objc ()
19303 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19305 /* In C++11, [ could start a lambda-introducer. */
19306 bool non_const = false;
19308 cp_parser_parse_tentatively (parser);
19310 if (!cp_parser_array_designator_p (parser))
19312 cp_parser_simulate_error (parser);
19313 designator = NULL_TREE;
19315 else
19317 designator = cp_parser_constant_expression (parser, true,
19318 &non_const);
19319 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19320 cp_parser_require (parser, CPP_EQ, RT_EQ);
19323 if (!cp_parser_parse_definitely (parser))
19324 designator = NULL_TREE;
19325 else if (non_const)
19326 require_potential_rvalue_constant_expression (designator);
19328 else
19329 designator = NULL_TREE;
19331 /* Parse the initializer. */
19332 initializer = cp_parser_initializer_clause (parser,
19333 &clause_non_constant_p);
19334 /* If any clause is non-constant, so is the entire initializer. */
19335 if (clause_non_constant_p)
19336 *non_constant_p = true;
19338 /* If we have an ellipsis, this is an initializer pack
19339 expansion. */
19340 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19342 /* Consume the `...'. */
19343 cp_lexer_consume_token (parser->lexer);
19345 /* Turn the initializer into an initializer expansion. */
19346 initializer = make_pack_expansion (initializer);
19349 /* Add it to the vector. */
19350 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19352 /* If the next token is not a comma, we have reached the end of
19353 the list. */
19354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19355 break;
19357 /* Peek at the next token. */
19358 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19359 /* If the next token is a `}', then we're still done. An
19360 initializer-clause can have a trailing `,' after the
19361 initializer-list and before the closing `}'. */
19362 if (token->type == CPP_CLOSE_BRACE)
19363 break;
19365 /* Consume the `,' token. */
19366 cp_lexer_consume_token (parser->lexer);
19369 return v;
19372 /* Classes [gram.class] */
19374 /* Parse a class-name.
19376 class-name:
19377 identifier
19378 template-id
19380 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19381 to indicate that names looked up in dependent types should be
19382 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19383 keyword has been used to indicate that the name that appears next
19384 is a template. TAG_TYPE indicates the explicit tag given before
19385 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19386 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19387 is the class being defined in a class-head.
19389 Returns the TYPE_DECL representing the class. */
19391 static tree
19392 cp_parser_class_name (cp_parser *parser,
19393 bool typename_keyword_p,
19394 bool template_keyword_p,
19395 enum tag_types tag_type,
19396 bool check_dependency_p,
19397 bool class_head_p,
19398 bool is_declaration)
19400 tree decl;
19401 tree scope;
19402 bool typename_p;
19403 cp_token *token;
19404 tree identifier = NULL_TREE;
19406 /* All class-names start with an identifier. */
19407 token = cp_lexer_peek_token (parser->lexer);
19408 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19410 cp_parser_error (parser, "expected class-name");
19411 return error_mark_node;
19414 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19415 to a template-id, so we save it here. */
19416 scope = parser->scope;
19417 if (scope == error_mark_node)
19418 return error_mark_node;
19420 /* Any name names a type if we're following the `typename' keyword
19421 in a qualified name where the enclosing scope is type-dependent. */
19422 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19423 && dependent_type_p (scope));
19424 /* Handle the common case (an identifier, but not a template-id)
19425 efficiently. */
19426 if (token->type == CPP_NAME
19427 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19429 cp_token *identifier_token;
19430 bool ambiguous_p;
19432 /* Look for the identifier. */
19433 identifier_token = cp_lexer_peek_token (parser->lexer);
19434 ambiguous_p = identifier_token->error_reported;
19435 identifier = cp_parser_identifier (parser);
19436 /* If the next token isn't an identifier, we are certainly not
19437 looking at a class-name. */
19438 if (identifier == error_mark_node)
19439 decl = error_mark_node;
19440 /* If we know this is a type-name, there's no need to look it
19441 up. */
19442 else if (typename_p)
19443 decl = identifier;
19444 else
19446 tree ambiguous_decls;
19447 /* If we already know that this lookup is ambiguous, then
19448 we've already issued an error message; there's no reason
19449 to check again. */
19450 if (ambiguous_p)
19452 cp_parser_simulate_error (parser);
19453 return error_mark_node;
19455 /* If the next token is a `::', then the name must be a type
19456 name.
19458 [basic.lookup.qual]
19460 During the lookup for a name preceding the :: scope
19461 resolution operator, object, function, and enumerator
19462 names are ignored. */
19463 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19464 tag_type = typename_type;
19465 /* Look up the name. */
19466 decl = cp_parser_lookup_name (parser, identifier,
19467 tag_type,
19468 /*is_template=*/false,
19469 /*is_namespace=*/false,
19470 check_dependency_p,
19471 &ambiguous_decls,
19472 identifier_token->location);
19473 if (ambiguous_decls)
19475 if (cp_parser_parsing_tentatively (parser))
19476 cp_parser_simulate_error (parser);
19477 return error_mark_node;
19481 else
19483 /* Try a template-id. */
19484 decl = cp_parser_template_id (parser, template_keyword_p,
19485 check_dependency_p,
19486 tag_type,
19487 is_declaration);
19488 if (decl == error_mark_node)
19489 return error_mark_node;
19492 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19494 /* If this is a typename, create a TYPENAME_TYPE. */
19495 if (typename_p && decl != error_mark_node)
19497 decl = make_typename_type (scope, decl, typename_type,
19498 /*complain=*/tf_error);
19499 if (decl != error_mark_node)
19500 decl = TYPE_NAME (decl);
19503 decl = strip_using_decl (decl);
19505 /* Check to see that it is really the name of a class. */
19506 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19507 && identifier_p (TREE_OPERAND (decl, 0))
19508 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19509 /* Situations like this:
19511 template <typename T> struct A {
19512 typename T::template X<int>::I i;
19515 are problematic. Is `T::template X<int>' a class-name? The
19516 standard does not seem to be definitive, but there is no other
19517 valid interpretation of the following `::'. Therefore, those
19518 names are considered class-names. */
19520 decl = make_typename_type (scope, decl, tag_type, tf_error);
19521 if (decl != error_mark_node)
19522 decl = TYPE_NAME (decl);
19524 else if (TREE_CODE (decl) != TYPE_DECL
19525 || TREE_TYPE (decl) == error_mark_node
19526 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19527 /* In Objective-C 2.0, a classname followed by '.' starts a
19528 dot-syntax expression, and it's not a type-name. */
19529 || (c_dialect_objc ()
19530 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19531 && objc_is_class_name (decl)))
19532 decl = error_mark_node;
19534 if (decl == error_mark_node)
19535 cp_parser_error (parser, "expected class-name");
19536 else if (identifier && !parser->scope)
19537 maybe_note_name_used_in_class (identifier, decl);
19539 return decl;
19542 /* Parse a class-specifier.
19544 class-specifier:
19545 class-head { member-specification [opt] }
19547 Returns the TREE_TYPE representing the class. */
19549 static tree
19550 cp_parser_class_specifier_1 (cp_parser* parser)
19552 tree type;
19553 tree attributes = NULL_TREE;
19554 bool nested_name_specifier_p;
19555 unsigned saved_num_template_parameter_lists;
19556 bool saved_in_function_body;
19557 unsigned char in_statement;
19558 bool in_switch_statement_p;
19559 bool saved_in_unbraced_linkage_specification_p;
19560 tree old_scope = NULL_TREE;
19561 tree scope = NULL_TREE;
19562 cp_token *closing_brace;
19564 push_deferring_access_checks (dk_no_deferred);
19566 /* Parse the class-head. */
19567 type = cp_parser_class_head (parser,
19568 &nested_name_specifier_p);
19569 /* If the class-head was a semantic disaster, skip the entire body
19570 of the class. */
19571 if (!type)
19573 cp_parser_skip_to_end_of_block_or_statement (parser);
19574 pop_deferring_access_checks ();
19575 return error_mark_node;
19578 /* Look for the `{'. */
19579 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19581 pop_deferring_access_checks ();
19582 return error_mark_node;
19585 cp_ensure_no_omp_declare_simd (parser);
19587 /* Issue an error message if type-definitions are forbidden here. */
19588 cp_parser_check_type_definition (parser);
19589 /* Remember that we are defining one more class. */
19590 ++parser->num_classes_being_defined;
19591 /* Inside the class, surrounding template-parameter-lists do not
19592 apply. */
19593 saved_num_template_parameter_lists
19594 = parser->num_template_parameter_lists;
19595 parser->num_template_parameter_lists = 0;
19596 /* We are not in a function body. */
19597 saved_in_function_body = parser->in_function_body;
19598 parser->in_function_body = false;
19599 /* Or in a loop. */
19600 in_statement = parser->in_statement;
19601 parser->in_statement = 0;
19602 /* Or in a switch. */
19603 in_switch_statement_p = parser->in_switch_statement_p;
19604 parser->in_switch_statement_p = false;
19605 /* We are not immediately inside an extern "lang" block. */
19606 saved_in_unbraced_linkage_specification_p
19607 = parser->in_unbraced_linkage_specification_p;
19608 parser->in_unbraced_linkage_specification_p = false;
19610 /* Start the class. */
19611 if (nested_name_specifier_p)
19613 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19614 old_scope = push_inner_scope (scope);
19616 type = begin_class_definition (type);
19618 if (type == error_mark_node)
19619 /* If the type is erroneous, skip the entire body of the class. */
19620 cp_parser_skip_to_closing_brace (parser);
19621 else
19622 /* Parse the member-specification. */
19623 cp_parser_member_specification_opt (parser);
19625 /* Look for the trailing `}'. */
19626 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19627 /* Look for trailing attributes to apply to this class. */
19628 if (cp_parser_allow_gnu_extensions_p (parser))
19629 attributes = cp_parser_gnu_attributes_opt (parser);
19630 if (type != error_mark_node)
19631 type = finish_struct (type, attributes);
19632 if (nested_name_specifier_p)
19633 pop_inner_scope (old_scope, scope);
19635 /* We've finished a type definition. Check for the common syntax
19636 error of forgetting a semicolon after the definition. We need to
19637 be careful, as we can't just check for not-a-semicolon and be done
19638 with it; the user might have typed:
19640 class X { } c = ...;
19641 class X { } *p = ...;
19643 and so forth. Instead, enumerate all the possible tokens that
19644 might follow this production; if we don't see one of them, then
19645 complain and silently insert the semicolon. */
19647 cp_token *token = cp_lexer_peek_token (parser->lexer);
19648 bool want_semicolon = true;
19650 if (cp_next_tokens_can_be_std_attribute_p (parser))
19651 /* Don't try to parse c++11 attributes here. As per the
19652 grammar, that should be a task for
19653 cp_parser_decl_specifier_seq. */
19654 want_semicolon = false;
19656 switch (token->type)
19658 case CPP_NAME:
19659 case CPP_SEMICOLON:
19660 case CPP_MULT:
19661 case CPP_AND:
19662 case CPP_OPEN_PAREN:
19663 case CPP_CLOSE_PAREN:
19664 case CPP_COMMA:
19665 want_semicolon = false;
19666 break;
19668 /* While it's legal for type qualifiers and storage class
19669 specifiers to follow type definitions in the grammar, only
19670 compiler testsuites contain code like that. Assume that if
19671 we see such code, then what we're really seeing is a case
19672 like:
19674 class X { }
19675 const <type> var = ...;
19679 class Y { }
19680 static <type> func (...) ...
19682 i.e. the qualifier or specifier applies to the next
19683 declaration. To do so, however, we need to look ahead one
19684 more token to see if *that* token is a type specifier.
19686 This code could be improved to handle:
19688 class Z { }
19689 static const <type> var = ...; */
19690 case CPP_KEYWORD:
19691 if (keyword_is_decl_specifier (token->keyword))
19693 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19695 /* Handling user-defined types here would be nice, but very
19696 tricky. */
19697 want_semicolon
19698 = (lookahead->type == CPP_KEYWORD
19699 && keyword_begins_type_specifier (lookahead->keyword));
19701 break;
19702 default:
19703 break;
19706 /* If we don't have a type, then something is very wrong and we
19707 shouldn't try to do anything clever. Likewise for not seeing the
19708 closing brace. */
19709 if (closing_brace && TYPE_P (type) && want_semicolon)
19711 cp_token_position prev
19712 = cp_lexer_previous_token_position (parser->lexer);
19713 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19714 location_t loc = prev_token->location;
19716 if (CLASSTYPE_DECLARED_CLASS (type))
19717 error_at (loc, "expected %<;%> after class definition");
19718 else if (TREE_CODE (type) == RECORD_TYPE)
19719 error_at (loc, "expected %<;%> after struct definition");
19720 else if (TREE_CODE (type) == UNION_TYPE)
19721 error_at (loc, "expected %<;%> after union definition");
19722 else
19723 gcc_unreachable ();
19725 /* Unget one token and smash it to look as though we encountered
19726 a semicolon in the input stream. */
19727 cp_lexer_set_token_position (parser->lexer, prev);
19728 token = cp_lexer_peek_token (parser->lexer);
19729 token->type = CPP_SEMICOLON;
19730 token->keyword = RID_MAX;
19734 /* If this class is not itself within the scope of another class,
19735 then we need to parse the bodies of all of the queued function
19736 definitions. Note that the queued functions defined in a class
19737 are not always processed immediately following the
19738 class-specifier for that class. Consider:
19740 struct A {
19741 struct B { void f() { sizeof (A); } };
19744 If `f' were processed before the processing of `A' were
19745 completed, there would be no way to compute the size of `A'.
19746 Note that the nesting we are interested in here is lexical --
19747 not the semantic nesting given by TYPE_CONTEXT. In particular,
19748 for:
19750 struct A { struct B; };
19751 struct A::B { void f() { } };
19753 there is no need to delay the parsing of `A::B::f'. */
19754 if (--parser->num_classes_being_defined == 0)
19756 tree decl;
19757 tree class_type = NULL_TREE;
19758 tree pushed_scope = NULL_TREE;
19759 unsigned ix;
19760 cp_default_arg_entry *e;
19761 tree save_ccp, save_ccr;
19763 /* In a first pass, parse default arguments to the functions.
19764 Then, in a second pass, parse the bodies of the functions.
19765 This two-phased approach handles cases like:
19767 struct S {
19768 void f() { g(); }
19769 void g(int i = 3);
19773 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19775 decl = e->decl;
19776 /* If there are default arguments that have not yet been processed,
19777 take care of them now. */
19778 if (class_type != e->class_type)
19780 if (pushed_scope)
19781 pop_scope (pushed_scope);
19782 class_type = e->class_type;
19783 pushed_scope = push_scope (class_type);
19785 /* Make sure that any template parameters are in scope. */
19786 maybe_begin_member_template_processing (decl);
19787 /* Parse the default argument expressions. */
19788 cp_parser_late_parsing_default_args (parser, decl);
19789 /* Remove any template parameters from the symbol table. */
19790 maybe_end_member_template_processing ();
19792 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19793 /* Now parse any NSDMIs. */
19794 save_ccp = current_class_ptr;
19795 save_ccr = current_class_ref;
19796 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19798 if (class_type != DECL_CONTEXT (decl))
19800 if (pushed_scope)
19801 pop_scope (pushed_scope);
19802 class_type = DECL_CONTEXT (decl);
19803 pushed_scope = push_scope (class_type);
19805 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19806 cp_parser_late_parsing_nsdmi (parser, decl);
19808 vec_safe_truncate (unparsed_nsdmis, 0);
19809 current_class_ptr = save_ccp;
19810 current_class_ref = save_ccr;
19811 if (pushed_scope)
19812 pop_scope (pushed_scope);
19814 /* Now do some post-NSDMI bookkeeping. */
19815 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19816 after_nsdmi_defaulted_late_checks (class_type);
19817 vec_safe_truncate (unparsed_classes, 0);
19818 after_nsdmi_defaulted_late_checks (type);
19820 /* Now parse the body of the functions. */
19821 if (flag_openmp)
19823 /* OpenMP UDRs need to be parsed before all other functions. */
19824 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19825 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19826 cp_parser_late_parsing_for_member (parser, decl);
19827 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19828 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19829 cp_parser_late_parsing_for_member (parser, decl);
19831 else
19832 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19833 cp_parser_late_parsing_for_member (parser, decl);
19834 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19836 else
19837 vec_safe_push (unparsed_classes, type);
19839 /* Put back any saved access checks. */
19840 pop_deferring_access_checks ();
19842 /* Restore saved state. */
19843 parser->in_switch_statement_p = in_switch_statement_p;
19844 parser->in_statement = in_statement;
19845 parser->in_function_body = saved_in_function_body;
19846 parser->num_template_parameter_lists
19847 = saved_num_template_parameter_lists;
19848 parser->in_unbraced_linkage_specification_p
19849 = saved_in_unbraced_linkage_specification_p;
19851 return type;
19854 static tree
19855 cp_parser_class_specifier (cp_parser* parser)
19857 tree ret;
19858 timevar_push (TV_PARSE_STRUCT);
19859 ret = cp_parser_class_specifier_1 (parser);
19860 timevar_pop (TV_PARSE_STRUCT);
19861 return ret;
19864 /* Parse a class-head.
19866 class-head:
19867 class-key identifier [opt] base-clause [opt]
19868 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19869 class-key nested-name-specifier [opt] template-id
19870 base-clause [opt]
19872 class-virt-specifier:
19873 final
19875 GNU Extensions:
19876 class-key attributes identifier [opt] base-clause [opt]
19877 class-key attributes nested-name-specifier identifier base-clause [opt]
19878 class-key attributes nested-name-specifier [opt] template-id
19879 base-clause [opt]
19881 Upon return BASES is initialized to the list of base classes (or
19882 NULL, if there are none) in the same form returned by
19883 cp_parser_base_clause.
19885 Returns the TYPE of the indicated class. Sets
19886 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19887 involving a nested-name-specifier was used, and FALSE otherwise.
19889 Returns error_mark_node if this is not a class-head.
19891 Returns NULL_TREE if the class-head is syntactically valid, but
19892 semantically invalid in a way that means we should skip the entire
19893 body of the class. */
19895 static tree
19896 cp_parser_class_head (cp_parser* parser,
19897 bool* nested_name_specifier_p)
19899 tree nested_name_specifier;
19900 enum tag_types class_key;
19901 tree id = NULL_TREE;
19902 tree type = NULL_TREE;
19903 tree attributes;
19904 tree bases;
19905 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19906 bool template_id_p = false;
19907 bool qualified_p = false;
19908 bool invalid_nested_name_p = false;
19909 bool invalid_explicit_specialization_p = false;
19910 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19911 tree pushed_scope = NULL_TREE;
19912 unsigned num_templates;
19913 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19914 /* Assume no nested-name-specifier will be present. */
19915 *nested_name_specifier_p = false;
19916 /* Assume no template parameter lists will be used in defining the
19917 type. */
19918 num_templates = 0;
19919 parser->colon_corrects_to_scope_p = false;
19921 /* Look for the class-key. */
19922 class_key = cp_parser_class_key (parser);
19923 if (class_key == none_type)
19924 return error_mark_node;
19926 /* Parse the attributes. */
19927 attributes = cp_parser_attributes_opt (parser);
19929 /* If the next token is `::', that is invalid -- but sometimes
19930 people do try to write:
19932 struct ::S {};
19934 Handle this gracefully by accepting the extra qualifier, and then
19935 issuing an error about it later if this really is a
19936 class-head. If it turns out just to be an elaborated type
19937 specifier, remain silent. */
19938 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19939 qualified_p = true;
19941 push_deferring_access_checks (dk_no_check);
19943 /* Determine the name of the class. Begin by looking for an
19944 optional nested-name-specifier. */
19945 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19946 nested_name_specifier
19947 = cp_parser_nested_name_specifier_opt (parser,
19948 /*typename_keyword_p=*/false,
19949 /*check_dependency_p=*/false,
19950 /*type_p=*/true,
19951 /*is_declaration=*/false);
19952 /* If there was a nested-name-specifier, then there *must* be an
19953 identifier. */
19954 if (nested_name_specifier)
19956 type_start_token = cp_lexer_peek_token (parser->lexer);
19957 /* Although the grammar says `identifier', it really means
19958 `class-name' or `template-name'. You are only allowed to
19959 define a class that has already been declared with this
19960 syntax.
19962 The proposed resolution for Core Issue 180 says that wherever
19963 you see `class T::X' you should treat `X' as a type-name.
19965 It is OK to define an inaccessible class; for example:
19967 class A { class B; };
19968 class A::B {};
19970 We do not know if we will see a class-name, or a
19971 template-name. We look for a class-name first, in case the
19972 class-name is a template-id; if we looked for the
19973 template-name first we would stop after the template-name. */
19974 cp_parser_parse_tentatively (parser);
19975 type = cp_parser_class_name (parser,
19976 /*typename_keyword_p=*/false,
19977 /*template_keyword_p=*/false,
19978 class_type,
19979 /*check_dependency_p=*/false,
19980 /*class_head_p=*/true,
19981 /*is_declaration=*/false);
19982 /* If that didn't work, ignore the nested-name-specifier. */
19983 if (!cp_parser_parse_definitely (parser))
19985 invalid_nested_name_p = true;
19986 type_start_token = cp_lexer_peek_token (parser->lexer);
19987 id = cp_parser_identifier (parser);
19988 if (id == error_mark_node)
19989 id = NULL_TREE;
19991 /* If we could not find a corresponding TYPE, treat this
19992 declaration like an unqualified declaration. */
19993 if (type == error_mark_node)
19994 nested_name_specifier = NULL_TREE;
19995 /* Otherwise, count the number of templates used in TYPE and its
19996 containing scopes. */
19997 else
19999 tree scope;
20001 for (scope = TREE_TYPE (type);
20002 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20003 scope = get_containing_scope (scope))
20004 if (TYPE_P (scope)
20005 && CLASS_TYPE_P (scope)
20006 && CLASSTYPE_TEMPLATE_INFO (scope)
20007 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20008 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20009 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20010 ++num_templates;
20013 /* Otherwise, the identifier is optional. */
20014 else
20016 /* We don't know whether what comes next is a template-id,
20017 an identifier, or nothing at all. */
20018 cp_parser_parse_tentatively (parser);
20019 /* Check for a template-id. */
20020 type_start_token = cp_lexer_peek_token (parser->lexer);
20021 id = cp_parser_template_id (parser,
20022 /*template_keyword_p=*/false,
20023 /*check_dependency_p=*/true,
20024 class_key,
20025 /*is_declaration=*/true);
20026 /* If that didn't work, it could still be an identifier. */
20027 if (!cp_parser_parse_definitely (parser))
20029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20031 type_start_token = cp_lexer_peek_token (parser->lexer);
20032 id = cp_parser_identifier (parser);
20034 else
20035 id = NULL_TREE;
20037 else
20039 template_id_p = true;
20040 ++num_templates;
20044 pop_deferring_access_checks ();
20046 if (id)
20048 cp_parser_check_for_invalid_template_id (parser, id,
20049 class_key,
20050 type_start_token->location);
20052 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20054 /* If it's not a `:' or a `{' then we can't really be looking at a
20055 class-head, since a class-head only appears as part of a
20056 class-specifier. We have to detect this situation before calling
20057 xref_tag, since that has irreversible side-effects. */
20058 if (!cp_parser_next_token_starts_class_definition_p (parser))
20060 cp_parser_error (parser, "expected %<{%> or %<:%>");
20061 type = error_mark_node;
20062 goto out;
20065 /* At this point, we're going ahead with the class-specifier, even
20066 if some other problem occurs. */
20067 cp_parser_commit_to_tentative_parse (parser);
20068 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20070 cp_parser_error (parser,
20071 "cannot specify %<override%> for a class");
20072 type = error_mark_node;
20073 goto out;
20075 /* Issue the error about the overly-qualified name now. */
20076 if (qualified_p)
20078 cp_parser_error (parser,
20079 "global qualification of class name is invalid");
20080 type = error_mark_node;
20081 goto out;
20083 else if (invalid_nested_name_p)
20085 cp_parser_error (parser,
20086 "qualified name does not name a class");
20087 type = error_mark_node;
20088 goto out;
20090 else if (nested_name_specifier)
20092 tree scope;
20094 /* Reject typedef-names in class heads. */
20095 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20097 error_at (type_start_token->location,
20098 "invalid class name in declaration of %qD",
20099 type);
20100 type = NULL_TREE;
20101 goto done;
20104 /* Figure out in what scope the declaration is being placed. */
20105 scope = current_scope ();
20106 /* If that scope does not contain the scope in which the
20107 class was originally declared, the program is invalid. */
20108 if (scope && !is_ancestor (scope, nested_name_specifier))
20110 if (at_namespace_scope_p ())
20111 error_at (type_start_token->location,
20112 "declaration of %qD in namespace %qD which does not "
20113 "enclose %qD",
20114 type, scope, nested_name_specifier);
20115 else
20116 error_at (type_start_token->location,
20117 "declaration of %qD in %qD which does not enclose %qD",
20118 type, scope, nested_name_specifier);
20119 type = NULL_TREE;
20120 goto done;
20122 /* [dcl.meaning]
20124 A declarator-id shall not be qualified except for the
20125 definition of a ... nested class outside of its class
20126 ... [or] the definition or explicit instantiation of a
20127 class member of a namespace outside of its namespace. */
20128 if (scope == nested_name_specifier)
20130 permerror (nested_name_specifier_token_start->location,
20131 "extra qualification not allowed");
20132 nested_name_specifier = NULL_TREE;
20133 num_templates = 0;
20136 /* An explicit-specialization must be preceded by "template <>". If
20137 it is not, try to recover gracefully. */
20138 if (at_namespace_scope_p ()
20139 && parser->num_template_parameter_lists == 0
20140 && template_id_p)
20142 error_at (type_start_token->location,
20143 "an explicit specialization must be preceded by %<template <>%>");
20144 invalid_explicit_specialization_p = true;
20145 /* Take the same action that would have been taken by
20146 cp_parser_explicit_specialization. */
20147 ++parser->num_template_parameter_lists;
20148 begin_specialization ();
20150 /* There must be no "return" statements between this point and the
20151 end of this function; set "type "to the correct return value and
20152 use "goto done;" to return. */
20153 /* Make sure that the right number of template parameters were
20154 present. */
20155 if (!cp_parser_check_template_parameters (parser, num_templates,
20156 type_start_token->location,
20157 /*declarator=*/NULL))
20159 /* If something went wrong, there is no point in even trying to
20160 process the class-definition. */
20161 type = NULL_TREE;
20162 goto done;
20165 /* Look up the type. */
20166 if (template_id_p)
20168 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20169 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20170 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20172 error_at (type_start_token->location,
20173 "function template %qD redeclared as a class template", id);
20174 type = error_mark_node;
20176 else
20178 type = TREE_TYPE (id);
20179 type = maybe_process_partial_specialization (type);
20181 if (nested_name_specifier)
20182 pushed_scope = push_scope (nested_name_specifier);
20184 else if (nested_name_specifier)
20186 tree class_type;
20188 /* Given:
20190 template <typename T> struct S { struct T };
20191 template <typename T> struct S<T>::T { };
20193 we will get a TYPENAME_TYPE when processing the definition of
20194 `S::T'. We need to resolve it to the actual type before we
20195 try to define it. */
20196 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20198 class_type = resolve_typename_type (TREE_TYPE (type),
20199 /*only_current_p=*/false);
20200 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20201 type = TYPE_NAME (class_type);
20202 else
20204 cp_parser_error (parser, "could not resolve typename type");
20205 type = error_mark_node;
20209 if (maybe_process_partial_specialization (TREE_TYPE (type))
20210 == error_mark_node)
20212 type = NULL_TREE;
20213 goto done;
20216 class_type = current_class_type;
20217 /* Enter the scope indicated by the nested-name-specifier. */
20218 pushed_scope = push_scope (nested_name_specifier);
20219 /* Get the canonical version of this type. */
20220 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20221 /* Call push_template_decl if it seems like we should be defining a
20222 template either from the template headers or the type we're
20223 defining, so that we diagnose both extra and missing headers. */
20224 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20225 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20226 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20228 type = push_template_decl (type);
20229 if (type == error_mark_node)
20231 type = NULL_TREE;
20232 goto done;
20236 type = TREE_TYPE (type);
20237 *nested_name_specifier_p = true;
20239 else /* The name is not a nested name. */
20241 /* If the class was unnamed, create a dummy name. */
20242 if (!id)
20243 id = make_anon_name ();
20244 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20245 parser->num_template_parameter_lists);
20248 /* Indicate whether this class was declared as a `class' or as a
20249 `struct'. */
20250 if (TREE_CODE (type) == RECORD_TYPE)
20251 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20252 cp_parser_check_class_key (class_key, type);
20254 /* If this type was already complete, and we see another definition,
20255 that's an error. */
20256 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20258 error_at (type_start_token->location, "redefinition of %q#T",
20259 type);
20260 error_at (type_start_token->location, "previous definition of %q+#T",
20261 type);
20262 type = NULL_TREE;
20263 goto done;
20265 else if (type == error_mark_node)
20266 type = NULL_TREE;
20268 if (type)
20270 /* Apply attributes now, before any use of the class as a template
20271 argument in its base list. */
20272 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20273 fixup_attribute_variants (type);
20276 /* We will have entered the scope containing the class; the names of
20277 base classes should be looked up in that context. For example:
20279 struct A { struct B {}; struct C; };
20280 struct A::C : B {};
20282 is valid. */
20284 /* Get the list of base-classes, if there is one. */
20285 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20287 /* PR59482: enter the class scope so that base-specifiers are looked
20288 up correctly. */
20289 if (type)
20290 pushclass (type);
20291 bases = cp_parser_base_clause (parser);
20292 /* PR59482: get out of the previously pushed class scope so that the
20293 subsequent pops pop the right thing. */
20294 if (type)
20295 popclass ();
20297 else
20298 bases = NULL_TREE;
20300 /* If we're really defining a class, process the base classes.
20301 If they're invalid, fail. */
20302 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20303 && !xref_basetypes (type, bases))
20304 type = NULL_TREE;
20306 done:
20307 /* Leave the scope given by the nested-name-specifier. We will
20308 enter the class scope itself while processing the members. */
20309 if (pushed_scope)
20310 pop_scope (pushed_scope);
20312 if (invalid_explicit_specialization_p)
20314 end_specialization ();
20315 --parser->num_template_parameter_lists;
20318 if (type)
20319 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20320 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20321 CLASSTYPE_FINAL (type) = 1;
20322 out:
20323 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20324 return type;
20327 /* Parse a class-key.
20329 class-key:
20330 class
20331 struct
20332 union
20334 Returns the kind of class-key specified, or none_type to indicate
20335 error. */
20337 static enum tag_types
20338 cp_parser_class_key (cp_parser* parser)
20340 cp_token *token;
20341 enum tag_types tag_type;
20343 /* Look for the class-key. */
20344 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20345 if (!token)
20346 return none_type;
20348 /* Check to see if the TOKEN is a class-key. */
20349 tag_type = cp_parser_token_is_class_key (token);
20350 if (!tag_type)
20351 cp_parser_error (parser, "expected class-key");
20352 return tag_type;
20355 /* Parse a type-parameter-key.
20357 type-parameter-key:
20358 class
20359 typename
20362 static void
20363 cp_parser_type_parameter_key (cp_parser* parser)
20365 /* Look for the type-parameter-key. */
20366 enum tag_types tag_type = none_type;
20367 cp_token *token = cp_lexer_peek_token (parser->lexer);
20368 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20370 cp_lexer_consume_token (parser->lexer);
20371 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20372 /* typename is not allowed in a template template parameter
20373 by the standard until C++1Z. */
20374 pedwarn (token->location, OPT_Wpedantic,
20375 "ISO C++ forbids typename key in template template parameter;"
20376 " use -std=c++1z or -std=gnu++1z");
20378 else
20379 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20381 return;
20384 /* Parse an (optional) member-specification.
20386 member-specification:
20387 member-declaration member-specification [opt]
20388 access-specifier : member-specification [opt] */
20390 static void
20391 cp_parser_member_specification_opt (cp_parser* parser)
20393 while (true)
20395 cp_token *token;
20396 enum rid keyword;
20398 /* Peek at the next token. */
20399 token = cp_lexer_peek_token (parser->lexer);
20400 /* If it's a `}', or EOF then we've seen all the members. */
20401 if (token->type == CPP_CLOSE_BRACE
20402 || token->type == CPP_EOF
20403 || token->type == CPP_PRAGMA_EOL)
20404 break;
20406 /* See if this token is a keyword. */
20407 keyword = token->keyword;
20408 switch (keyword)
20410 case RID_PUBLIC:
20411 case RID_PROTECTED:
20412 case RID_PRIVATE:
20413 /* Consume the access-specifier. */
20414 cp_lexer_consume_token (parser->lexer);
20415 /* Remember which access-specifier is active. */
20416 current_access_specifier = token->u.value;
20417 /* Look for the `:'. */
20418 cp_parser_require (parser, CPP_COLON, RT_COLON);
20419 break;
20421 default:
20422 /* Accept #pragmas at class scope. */
20423 if (token->type == CPP_PRAGMA)
20425 cp_parser_pragma (parser, pragma_member);
20426 break;
20429 /* Otherwise, the next construction must be a
20430 member-declaration. */
20431 cp_parser_member_declaration (parser);
20436 /* Parse a member-declaration.
20438 member-declaration:
20439 decl-specifier-seq [opt] member-declarator-list [opt] ;
20440 function-definition ; [opt]
20441 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20442 using-declaration
20443 template-declaration
20444 alias-declaration
20446 member-declarator-list:
20447 member-declarator
20448 member-declarator-list , member-declarator
20450 member-declarator:
20451 declarator pure-specifier [opt]
20452 declarator constant-initializer [opt]
20453 identifier [opt] : constant-expression
20455 GNU Extensions:
20457 member-declaration:
20458 __extension__ member-declaration
20460 member-declarator:
20461 declarator attributes [opt] pure-specifier [opt]
20462 declarator attributes [opt] constant-initializer [opt]
20463 identifier [opt] attributes [opt] : constant-expression
20465 C++0x Extensions:
20467 member-declaration:
20468 static_assert-declaration */
20470 static void
20471 cp_parser_member_declaration (cp_parser* parser)
20473 cp_decl_specifier_seq decl_specifiers;
20474 tree prefix_attributes;
20475 tree decl;
20476 int declares_class_or_enum;
20477 bool friend_p;
20478 cp_token *token = NULL;
20479 cp_token *decl_spec_token_start = NULL;
20480 cp_token *initializer_token_start = NULL;
20481 int saved_pedantic;
20482 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20484 /* Check for the `__extension__' keyword. */
20485 if (cp_parser_extension_opt (parser, &saved_pedantic))
20487 /* Recurse. */
20488 cp_parser_member_declaration (parser);
20489 /* Restore the old value of the PEDANTIC flag. */
20490 pedantic = saved_pedantic;
20492 return;
20495 /* Check for a template-declaration. */
20496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20498 /* An explicit specialization here is an error condition, and we
20499 expect the specialization handler to detect and report this. */
20500 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20501 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20502 cp_parser_explicit_specialization (parser);
20503 else
20504 cp_parser_template_declaration (parser, /*member_p=*/true);
20506 return;
20509 /* Check for a using-declaration. */
20510 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20512 if (cxx_dialect < cxx11)
20514 /* Parse the using-declaration. */
20515 cp_parser_using_declaration (parser,
20516 /*access_declaration_p=*/false);
20517 return;
20519 else
20521 tree decl;
20522 bool alias_decl_expected;
20523 cp_parser_parse_tentatively (parser);
20524 decl = cp_parser_alias_declaration (parser);
20525 /* Note that if we actually see the '=' token after the
20526 identifier, cp_parser_alias_declaration commits the
20527 tentative parse. In that case, we really expects an
20528 alias-declaration. Otherwise, we expect a using
20529 declaration. */
20530 alias_decl_expected =
20531 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20532 cp_parser_parse_definitely (parser);
20534 if (alias_decl_expected)
20535 finish_member_declaration (decl);
20536 else
20537 cp_parser_using_declaration (parser,
20538 /*access_declaration_p=*/false);
20539 return;
20543 /* Check for @defs. */
20544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20546 tree ivar, member;
20547 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20548 ivar = ivar_chains;
20549 while (ivar)
20551 member = ivar;
20552 ivar = TREE_CHAIN (member);
20553 TREE_CHAIN (member) = NULL_TREE;
20554 finish_member_declaration (member);
20556 return;
20559 /* If the next token is `static_assert' we have a static assertion. */
20560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20562 cp_parser_static_assert (parser, /*member_p=*/true);
20563 return;
20566 parser->colon_corrects_to_scope_p = false;
20568 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20569 goto out;
20571 /* Parse the decl-specifier-seq. */
20572 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20573 cp_parser_decl_specifier_seq (parser,
20574 CP_PARSER_FLAGS_OPTIONAL,
20575 &decl_specifiers,
20576 &declares_class_or_enum);
20577 /* Check for an invalid type-name. */
20578 if (!decl_specifiers.any_type_specifiers_p
20579 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20580 goto out;
20581 /* If there is no declarator, then the decl-specifier-seq should
20582 specify a type. */
20583 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20585 /* If there was no decl-specifier-seq, and the next token is a
20586 `;', then we have something like:
20588 struct S { ; };
20590 [class.mem]
20592 Each member-declaration shall declare at least one member
20593 name of the class. */
20594 if (!decl_specifiers.any_specifiers_p)
20596 cp_token *token = cp_lexer_peek_token (parser->lexer);
20597 if (!in_system_header_at (token->location))
20598 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20600 else
20602 tree type;
20604 /* See if this declaration is a friend. */
20605 friend_p = cp_parser_friend_p (&decl_specifiers);
20606 /* If there were decl-specifiers, check to see if there was
20607 a class-declaration. */
20608 type = check_tag_decl (&decl_specifiers,
20609 /*explicit_type_instantiation_p=*/false);
20610 /* Nested classes have already been added to the class, but
20611 a `friend' needs to be explicitly registered. */
20612 if (friend_p)
20614 /* If the `friend' keyword was present, the friend must
20615 be introduced with a class-key. */
20616 if (!declares_class_or_enum && cxx_dialect < cxx11)
20617 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20618 "in C++03 a class-key must be used "
20619 "when declaring a friend");
20620 /* In this case:
20622 template <typename T> struct A {
20623 friend struct A<T>::B;
20626 A<T>::B will be represented by a TYPENAME_TYPE, and
20627 therefore not recognized by check_tag_decl. */
20628 if (!type)
20630 type = decl_specifiers.type;
20631 if (type && TREE_CODE (type) == TYPE_DECL)
20632 type = TREE_TYPE (type);
20634 if (!type || !TYPE_P (type))
20635 error_at (decl_spec_token_start->location,
20636 "friend declaration does not name a class or "
20637 "function");
20638 else
20639 make_friend_class (current_class_type, type,
20640 /*complain=*/true);
20642 /* If there is no TYPE, an error message will already have
20643 been issued. */
20644 else if (!type || type == error_mark_node)
20646 /* An anonymous aggregate has to be handled specially; such
20647 a declaration really declares a data member (with a
20648 particular type), as opposed to a nested class. */
20649 else if (ANON_AGGR_TYPE_P (type))
20651 /* C++11 9.5/6. */
20652 if (decl_specifiers.storage_class != sc_none)
20653 error_at (decl_spec_token_start->location,
20654 "a storage class on an anonymous aggregate "
20655 "in class scope is not allowed");
20657 /* Remove constructors and such from TYPE, now that we
20658 know it is an anonymous aggregate. */
20659 fixup_anonymous_aggr (type);
20660 /* And make the corresponding data member. */
20661 decl = build_decl (decl_spec_token_start->location,
20662 FIELD_DECL, NULL_TREE, type);
20663 /* Add it to the class. */
20664 finish_member_declaration (decl);
20666 else
20667 cp_parser_check_access_in_redeclaration
20668 (TYPE_NAME (type),
20669 decl_spec_token_start->location);
20672 else
20674 bool assume_semicolon = false;
20676 /* Clear attributes from the decl_specifiers but keep them
20677 around as prefix attributes that apply them to the entity
20678 being declared. */
20679 prefix_attributes = decl_specifiers.attributes;
20680 decl_specifiers.attributes = NULL_TREE;
20682 /* See if these declarations will be friends. */
20683 friend_p = cp_parser_friend_p (&decl_specifiers);
20685 /* Keep going until we hit the `;' at the end of the
20686 declaration. */
20687 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20689 tree attributes = NULL_TREE;
20690 tree first_attribute;
20692 /* Peek at the next token. */
20693 token = cp_lexer_peek_token (parser->lexer);
20695 /* Check for a bitfield declaration. */
20696 if (token->type == CPP_COLON
20697 || (token->type == CPP_NAME
20698 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20699 == CPP_COLON))
20701 tree identifier;
20702 tree width;
20704 /* Get the name of the bitfield. Note that we cannot just
20705 check TOKEN here because it may have been invalidated by
20706 the call to cp_lexer_peek_nth_token above. */
20707 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20708 identifier = cp_parser_identifier (parser);
20709 else
20710 identifier = NULL_TREE;
20712 /* Consume the `:' token. */
20713 cp_lexer_consume_token (parser->lexer);
20714 /* Get the width of the bitfield. */
20715 width
20716 = cp_parser_constant_expression (parser);
20718 /* Look for attributes that apply to the bitfield. */
20719 attributes = cp_parser_attributes_opt (parser);
20720 /* Remember which attributes are prefix attributes and
20721 which are not. */
20722 first_attribute = attributes;
20723 /* Combine the attributes. */
20724 attributes = chainon (prefix_attributes, attributes);
20726 /* Create the bitfield declaration. */
20727 decl = grokbitfield (identifier
20728 ? make_id_declarator (NULL_TREE,
20729 identifier,
20730 sfk_none)
20731 : NULL,
20732 &decl_specifiers,
20733 width,
20734 attributes);
20736 else
20738 cp_declarator *declarator;
20739 tree initializer;
20740 tree asm_specification;
20741 int ctor_dtor_or_conv_p;
20743 /* Parse the declarator. */
20744 declarator
20745 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20746 &ctor_dtor_or_conv_p,
20747 /*parenthesized_p=*/NULL,
20748 /*member_p=*/true,
20749 friend_p);
20751 /* If something went wrong parsing the declarator, make sure
20752 that we at least consume some tokens. */
20753 if (declarator == cp_error_declarator)
20755 /* Skip to the end of the statement. */
20756 cp_parser_skip_to_end_of_statement (parser);
20757 /* If the next token is not a semicolon, that is
20758 probably because we just skipped over the body of
20759 a function. So, we consume a semicolon if
20760 present, but do not issue an error message if it
20761 is not present. */
20762 if (cp_lexer_next_token_is (parser->lexer,
20763 CPP_SEMICOLON))
20764 cp_lexer_consume_token (parser->lexer);
20765 goto out;
20768 if (declares_class_or_enum & 2)
20769 cp_parser_check_for_definition_in_return_type
20770 (declarator, decl_specifiers.type,
20771 decl_specifiers.locations[ds_type_spec]);
20773 /* Look for an asm-specification. */
20774 asm_specification = cp_parser_asm_specification_opt (parser);
20775 /* Look for attributes that apply to the declaration. */
20776 attributes = cp_parser_attributes_opt (parser);
20777 /* Remember which attributes are prefix attributes and
20778 which are not. */
20779 first_attribute = attributes;
20780 /* Combine the attributes. */
20781 attributes = chainon (prefix_attributes, attributes);
20783 /* If it's an `=', then we have a constant-initializer or a
20784 pure-specifier. It is not correct to parse the
20785 initializer before registering the member declaration
20786 since the member declaration should be in scope while
20787 its initializer is processed. However, the rest of the
20788 front end does not yet provide an interface that allows
20789 us to handle this correctly. */
20790 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20792 /* In [class.mem]:
20794 A pure-specifier shall be used only in the declaration of
20795 a virtual function.
20797 A member-declarator can contain a constant-initializer
20798 only if it declares a static member of integral or
20799 enumeration type.
20801 Therefore, if the DECLARATOR is for a function, we look
20802 for a pure-specifier; otherwise, we look for a
20803 constant-initializer. When we call `grokfield', it will
20804 perform more stringent semantics checks. */
20805 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20806 if (function_declarator_p (declarator)
20807 || (decl_specifiers.type
20808 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20809 && declarator->kind == cdk_id
20810 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20811 == FUNCTION_TYPE)))
20812 initializer = cp_parser_pure_specifier (parser);
20813 else if (decl_specifiers.storage_class != sc_static)
20814 initializer = cp_parser_save_nsdmi (parser);
20815 else if (cxx_dialect >= cxx11)
20817 bool nonconst;
20818 /* Don't require a constant rvalue in C++11, since we
20819 might want a reference constant. We'll enforce
20820 constancy later. */
20821 cp_lexer_consume_token (parser->lexer);
20822 /* Parse the initializer. */
20823 initializer = cp_parser_initializer_clause (parser,
20824 &nonconst);
20826 else
20827 /* Parse the initializer. */
20828 initializer = cp_parser_constant_initializer (parser);
20830 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20831 && !function_declarator_p (declarator))
20833 bool x;
20834 if (decl_specifiers.storage_class != sc_static)
20835 initializer = cp_parser_save_nsdmi (parser);
20836 else
20837 initializer = cp_parser_initializer (parser, &x, &x);
20839 /* Otherwise, there is no initializer. */
20840 else
20841 initializer = NULL_TREE;
20843 /* See if we are probably looking at a function
20844 definition. We are certainly not looking at a
20845 member-declarator. Calling `grokfield' has
20846 side-effects, so we must not do it unless we are sure
20847 that we are looking at a member-declarator. */
20848 if (cp_parser_token_starts_function_definition_p
20849 (cp_lexer_peek_token (parser->lexer)))
20851 /* The grammar does not allow a pure-specifier to be
20852 used when a member function is defined. (It is
20853 possible that this fact is an oversight in the
20854 standard, since a pure function may be defined
20855 outside of the class-specifier. */
20856 if (initializer && initializer_token_start)
20857 error_at (initializer_token_start->location,
20858 "pure-specifier on function-definition");
20859 decl = cp_parser_save_member_function_body (parser,
20860 &decl_specifiers,
20861 declarator,
20862 attributes);
20863 if (parser->fully_implicit_function_template_p)
20864 decl = finish_fully_implicit_template (parser, decl);
20865 /* If the member was not a friend, declare it here. */
20866 if (!friend_p)
20867 finish_member_declaration (decl);
20868 /* Peek at the next token. */
20869 token = cp_lexer_peek_token (parser->lexer);
20870 /* If the next token is a semicolon, consume it. */
20871 if (token->type == CPP_SEMICOLON)
20872 cp_lexer_consume_token (parser->lexer);
20873 goto out;
20875 else
20876 if (declarator->kind == cdk_function)
20877 declarator->id_loc = token->location;
20878 /* Create the declaration. */
20879 decl = grokfield (declarator, &decl_specifiers,
20880 initializer, /*init_const_expr_p=*/true,
20881 asm_specification, attributes);
20882 if (parser->fully_implicit_function_template_p)
20884 if (friend_p)
20885 finish_fully_implicit_template (parser, 0);
20886 else
20887 decl = finish_fully_implicit_template (parser, decl);
20891 cp_finalize_omp_declare_simd (parser, decl);
20893 /* Reset PREFIX_ATTRIBUTES. */
20894 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20895 attributes = TREE_CHAIN (attributes);
20896 if (attributes)
20897 TREE_CHAIN (attributes) = NULL_TREE;
20899 /* If there is any qualification still in effect, clear it
20900 now; we will be starting fresh with the next declarator. */
20901 parser->scope = NULL_TREE;
20902 parser->qualifying_scope = NULL_TREE;
20903 parser->object_scope = NULL_TREE;
20904 /* If it's a `,', then there are more declarators. */
20905 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20907 cp_lexer_consume_token (parser->lexer);
20908 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20910 cp_token *token = cp_lexer_previous_token (parser->lexer);
20911 error_at (token->location,
20912 "stray %<,%> at end of member declaration");
20915 /* If the next token isn't a `;', then we have a parse error. */
20916 else if (cp_lexer_next_token_is_not (parser->lexer,
20917 CPP_SEMICOLON))
20919 /* The next token might be a ways away from where the
20920 actual semicolon is missing. Find the previous token
20921 and use that for our error position. */
20922 cp_token *token = cp_lexer_previous_token (parser->lexer);
20923 error_at (token->location,
20924 "expected %<;%> at end of member declaration");
20926 /* Assume that the user meant to provide a semicolon. If
20927 we were to cp_parser_skip_to_end_of_statement, we might
20928 skip to a semicolon inside a member function definition
20929 and issue nonsensical error messages. */
20930 assume_semicolon = true;
20933 if (decl)
20935 /* Add DECL to the list of members. */
20936 if (!friend_p)
20937 finish_member_declaration (decl);
20939 if (TREE_CODE (decl) == FUNCTION_DECL)
20940 cp_parser_save_default_args (parser, decl);
20941 else if (TREE_CODE (decl) == FIELD_DECL
20942 && !DECL_C_BIT_FIELD (decl)
20943 && DECL_INITIAL (decl))
20944 /* Add DECL to the queue of NSDMI to be parsed later. */
20945 vec_safe_push (unparsed_nsdmis, decl);
20948 if (assume_semicolon)
20949 goto out;
20953 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20954 out:
20955 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20958 /* Parse a pure-specifier.
20960 pure-specifier:
20963 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20964 Otherwise, ERROR_MARK_NODE is returned. */
20966 static tree
20967 cp_parser_pure_specifier (cp_parser* parser)
20969 cp_token *token;
20971 /* Look for the `=' token. */
20972 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20973 return error_mark_node;
20974 /* Look for the `0' token. */
20975 token = cp_lexer_peek_token (parser->lexer);
20977 if (token->type == CPP_EOF
20978 || token->type == CPP_PRAGMA_EOL)
20979 return error_mark_node;
20981 cp_lexer_consume_token (parser->lexer);
20983 /* Accept = default or = delete in c++0x mode. */
20984 if (token->keyword == RID_DEFAULT
20985 || token->keyword == RID_DELETE)
20987 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20988 return token->u.value;
20991 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20992 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20994 cp_parser_error (parser,
20995 "invalid pure specifier (only %<= 0%> is allowed)");
20996 cp_parser_skip_to_end_of_statement (parser);
20997 return error_mark_node;
20999 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21001 error_at (token->location, "templates may not be %<virtual%>");
21002 return error_mark_node;
21005 return integer_zero_node;
21008 /* Parse a constant-initializer.
21010 constant-initializer:
21011 = constant-expression
21013 Returns a representation of the constant-expression. */
21015 static tree
21016 cp_parser_constant_initializer (cp_parser* parser)
21018 /* Look for the `=' token. */
21019 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21020 return error_mark_node;
21022 /* It is invalid to write:
21024 struct S { static const int i = { 7 }; };
21027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21029 cp_parser_error (parser,
21030 "a brace-enclosed initializer is not allowed here");
21031 /* Consume the opening brace. */
21032 cp_lexer_consume_token (parser->lexer);
21033 /* Skip the initializer. */
21034 cp_parser_skip_to_closing_brace (parser);
21035 /* Look for the trailing `}'. */
21036 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21038 return error_mark_node;
21041 return cp_parser_constant_expression (parser);
21044 /* Derived classes [gram.class.derived] */
21046 /* Parse a base-clause.
21048 base-clause:
21049 : base-specifier-list
21051 base-specifier-list:
21052 base-specifier ... [opt]
21053 base-specifier-list , base-specifier ... [opt]
21055 Returns a TREE_LIST representing the base-classes, in the order in
21056 which they were declared. The representation of each node is as
21057 described by cp_parser_base_specifier.
21059 In the case that no bases are specified, this function will return
21060 NULL_TREE, not ERROR_MARK_NODE. */
21062 static tree
21063 cp_parser_base_clause (cp_parser* parser)
21065 tree bases = NULL_TREE;
21067 /* Look for the `:' that begins the list. */
21068 cp_parser_require (parser, CPP_COLON, RT_COLON);
21070 /* Scan the base-specifier-list. */
21071 while (true)
21073 cp_token *token;
21074 tree base;
21075 bool pack_expansion_p = false;
21077 /* Look for the base-specifier. */
21078 base = cp_parser_base_specifier (parser);
21079 /* Look for the (optional) ellipsis. */
21080 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21082 /* Consume the `...'. */
21083 cp_lexer_consume_token (parser->lexer);
21085 pack_expansion_p = true;
21088 /* Add BASE to the front of the list. */
21089 if (base && base != error_mark_node)
21091 if (pack_expansion_p)
21092 /* Make this a pack expansion type. */
21093 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21095 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21097 TREE_CHAIN (base) = bases;
21098 bases = base;
21101 /* Peek at the next token. */
21102 token = cp_lexer_peek_token (parser->lexer);
21103 /* If it's not a comma, then the list is complete. */
21104 if (token->type != CPP_COMMA)
21105 break;
21106 /* Consume the `,'. */
21107 cp_lexer_consume_token (parser->lexer);
21110 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21111 base class had a qualified name. However, the next name that
21112 appears is certainly not qualified. */
21113 parser->scope = NULL_TREE;
21114 parser->qualifying_scope = NULL_TREE;
21115 parser->object_scope = NULL_TREE;
21117 return nreverse (bases);
21120 /* Parse a base-specifier.
21122 base-specifier:
21123 :: [opt] nested-name-specifier [opt] class-name
21124 virtual access-specifier [opt] :: [opt] nested-name-specifier
21125 [opt] class-name
21126 access-specifier virtual [opt] :: [opt] nested-name-specifier
21127 [opt] class-name
21129 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21130 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21131 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21132 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21134 static tree
21135 cp_parser_base_specifier (cp_parser* parser)
21137 cp_token *token;
21138 bool done = false;
21139 bool virtual_p = false;
21140 bool duplicate_virtual_error_issued_p = false;
21141 bool duplicate_access_error_issued_p = false;
21142 bool class_scope_p, template_p;
21143 tree access = access_default_node;
21144 tree type;
21146 /* Process the optional `virtual' and `access-specifier'. */
21147 while (!done)
21149 /* Peek at the next token. */
21150 token = cp_lexer_peek_token (parser->lexer);
21151 /* Process `virtual'. */
21152 switch (token->keyword)
21154 case RID_VIRTUAL:
21155 /* If `virtual' appears more than once, issue an error. */
21156 if (virtual_p && !duplicate_virtual_error_issued_p)
21158 cp_parser_error (parser,
21159 "%<virtual%> specified more than once in base-specified");
21160 duplicate_virtual_error_issued_p = true;
21163 virtual_p = true;
21165 /* Consume the `virtual' token. */
21166 cp_lexer_consume_token (parser->lexer);
21168 break;
21170 case RID_PUBLIC:
21171 case RID_PROTECTED:
21172 case RID_PRIVATE:
21173 /* If more than one access specifier appears, issue an
21174 error. */
21175 if (access != access_default_node
21176 && !duplicate_access_error_issued_p)
21178 cp_parser_error (parser,
21179 "more than one access specifier in base-specified");
21180 duplicate_access_error_issued_p = true;
21183 access = ridpointers[(int) token->keyword];
21185 /* Consume the access-specifier. */
21186 cp_lexer_consume_token (parser->lexer);
21188 break;
21190 default:
21191 done = true;
21192 break;
21195 /* It is not uncommon to see programs mechanically, erroneously, use
21196 the 'typename' keyword to denote (dependent) qualified types
21197 as base classes. */
21198 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21200 token = cp_lexer_peek_token (parser->lexer);
21201 if (!processing_template_decl)
21202 error_at (token->location,
21203 "keyword %<typename%> not allowed outside of templates");
21204 else
21205 error_at (token->location,
21206 "keyword %<typename%> not allowed in this context "
21207 "(the base class is implicitly a type)");
21208 cp_lexer_consume_token (parser->lexer);
21211 /* Look for the optional `::' operator. */
21212 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21213 /* Look for the nested-name-specifier. The simplest way to
21214 implement:
21216 [temp.res]
21218 The keyword `typename' is not permitted in a base-specifier or
21219 mem-initializer; in these contexts a qualified name that
21220 depends on a template-parameter is implicitly assumed to be a
21221 type name.
21223 is to pretend that we have seen the `typename' keyword at this
21224 point. */
21225 cp_parser_nested_name_specifier_opt (parser,
21226 /*typename_keyword_p=*/true,
21227 /*check_dependency_p=*/true,
21228 typename_type,
21229 /*is_declaration=*/true);
21230 /* If the base class is given by a qualified name, assume that names
21231 we see are type names or templates, as appropriate. */
21232 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21233 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21235 if (!parser->scope
21236 && cp_lexer_next_token_is_decltype (parser->lexer))
21237 /* DR 950 allows decltype as a base-specifier. */
21238 type = cp_parser_decltype (parser);
21239 else
21241 /* Otherwise, look for the class-name. */
21242 type = cp_parser_class_name (parser,
21243 class_scope_p,
21244 template_p,
21245 typename_type,
21246 /*check_dependency_p=*/true,
21247 /*class_head_p=*/false,
21248 /*is_declaration=*/true);
21249 type = TREE_TYPE (type);
21252 if (type == error_mark_node)
21253 return error_mark_node;
21255 return finish_base_specifier (type, access, virtual_p);
21258 /* Exception handling [gram.exception] */
21260 /* Parse an (optional) noexcept-specification.
21262 noexcept-specification:
21263 noexcept ( constant-expression ) [opt]
21265 If no noexcept-specification is present, returns NULL_TREE.
21266 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21267 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21268 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21269 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21270 in which case a boolean condition is returned instead. */
21272 static tree
21273 cp_parser_noexcept_specification_opt (cp_parser* parser,
21274 bool require_constexpr,
21275 bool* consumed_expr,
21276 bool return_cond)
21278 cp_token *token;
21279 const char *saved_message;
21281 /* Peek at the next token. */
21282 token = cp_lexer_peek_token (parser->lexer);
21284 /* Is it a noexcept-specification? */
21285 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21287 tree expr;
21288 cp_lexer_consume_token (parser->lexer);
21290 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21292 cp_lexer_consume_token (parser->lexer);
21294 if (require_constexpr)
21296 /* Types may not be defined in an exception-specification. */
21297 saved_message = parser->type_definition_forbidden_message;
21298 parser->type_definition_forbidden_message
21299 = G_("types may not be defined in an exception-specification");
21301 expr = cp_parser_constant_expression (parser);
21303 /* Restore the saved message. */
21304 parser->type_definition_forbidden_message = saved_message;
21306 else
21308 expr = cp_parser_expression (parser);
21309 *consumed_expr = true;
21312 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21314 else
21316 expr = boolean_true_node;
21317 if (!require_constexpr)
21318 *consumed_expr = false;
21321 /* We cannot build a noexcept-spec right away because this will check
21322 that expr is a constexpr. */
21323 if (!return_cond)
21324 return build_noexcept_spec (expr, tf_warning_or_error);
21325 else
21326 return expr;
21328 else
21329 return NULL_TREE;
21332 /* Parse an (optional) exception-specification.
21334 exception-specification:
21335 throw ( type-id-list [opt] )
21337 Returns a TREE_LIST representing the exception-specification. The
21338 TREE_VALUE of each node is a type. */
21340 static tree
21341 cp_parser_exception_specification_opt (cp_parser* parser)
21343 cp_token *token;
21344 tree type_id_list;
21345 const char *saved_message;
21347 /* Peek at the next token. */
21348 token = cp_lexer_peek_token (parser->lexer);
21350 /* Is it a noexcept-specification? */
21351 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21352 false);
21353 if (type_id_list != NULL_TREE)
21354 return type_id_list;
21356 /* If it's not `throw', then there's no exception-specification. */
21357 if (!cp_parser_is_keyword (token, RID_THROW))
21358 return NULL_TREE;
21360 #if 0
21361 /* Enable this once a lot of code has transitioned to noexcept? */
21362 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21363 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21364 "deprecated in C++0x; use %<noexcept%> instead");
21365 #endif
21367 /* Consume the `throw'. */
21368 cp_lexer_consume_token (parser->lexer);
21370 /* Look for the `('. */
21371 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21373 /* Peek at the next token. */
21374 token = cp_lexer_peek_token (parser->lexer);
21375 /* If it's not a `)', then there is a type-id-list. */
21376 if (token->type != CPP_CLOSE_PAREN)
21378 /* Types may not be defined in an exception-specification. */
21379 saved_message = parser->type_definition_forbidden_message;
21380 parser->type_definition_forbidden_message
21381 = G_("types may not be defined in an exception-specification");
21382 /* Parse the type-id-list. */
21383 type_id_list = cp_parser_type_id_list (parser);
21384 /* Restore the saved message. */
21385 parser->type_definition_forbidden_message = saved_message;
21387 else
21388 type_id_list = empty_except_spec;
21390 /* Look for the `)'. */
21391 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21393 return type_id_list;
21396 /* Parse an (optional) type-id-list.
21398 type-id-list:
21399 type-id ... [opt]
21400 type-id-list , type-id ... [opt]
21402 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21403 in the order that the types were presented. */
21405 static tree
21406 cp_parser_type_id_list (cp_parser* parser)
21408 tree types = NULL_TREE;
21410 while (true)
21412 cp_token *token;
21413 tree type;
21415 /* Get the next type-id. */
21416 type = cp_parser_type_id (parser);
21417 /* Parse the optional ellipsis. */
21418 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21420 /* Consume the `...'. */
21421 cp_lexer_consume_token (parser->lexer);
21423 /* Turn the type into a pack expansion expression. */
21424 type = make_pack_expansion (type);
21426 /* Add it to the list. */
21427 types = add_exception_specifier (types, type, /*complain=*/1);
21428 /* Peek at the next token. */
21429 token = cp_lexer_peek_token (parser->lexer);
21430 /* If it is not a `,', we are done. */
21431 if (token->type != CPP_COMMA)
21432 break;
21433 /* Consume the `,'. */
21434 cp_lexer_consume_token (parser->lexer);
21437 return nreverse (types);
21440 /* Parse a try-block.
21442 try-block:
21443 try compound-statement handler-seq */
21445 static tree
21446 cp_parser_try_block (cp_parser* parser)
21448 tree try_block;
21450 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21451 if (parser->in_function_body
21452 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21453 error ("%<try%> in %<constexpr%> function");
21455 try_block = begin_try_block ();
21456 cp_parser_compound_statement (parser, NULL, true, false);
21457 finish_try_block (try_block);
21458 cp_parser_handler_seq (parser);
21459 finish_handler_sequence (try_block);
21461 return try_block;
21464 /* Parse a function-try-block.
21466 function-try-block:
21467 try ctor-initializer [opt] function-body handler-seq */
21469 static bool
21470 cp_parser_function_try_block (cp_parser* parser)
21472 tree compound_stmt;
21473 tree try_block;
21474 bool ctor_initializer_p;
21476 /* Look for the `try' keyword. */
21477 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21478 return false;
21479 /* Let the rest of the front end know where we are. */
21480 try_block = begin_function_try_block (&compound_stmt);
21481 /* Parse the function-body. */
21482 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21483 (parser, /*in_function_try_block=*/true);
21484 /* We're done with the `try' part. */
21485 finish_function_try_block (try_block);
21486 /* Parse the handlers. */
21487 cp_parser_handler_seq (parser);
21488 /* We're done with the handlers. */
21489 finish_function_handler_sequence (try_block, compound_stmt);
21491 return ctor_initializer_p;
21494 /* Parse a handler-seq.
21496 handler-seq:
21497 handler handler-seq [opt] */
21499 static void
21500 cp_parser_handler_seq (cp_parser* parser)
21502 while (true)
21504 cp_token *token;
21506 /* Parse the handler. */
21507 cp_parser_handler (parser);
21508 /* Peek at the next token. */
21509 token = cp_lexer_peek_token (parser->lexer);
21510 /* If it's not `catch' then there are no more handlers. */
21511 if (!cp_parser_is_keyword (token, RID_CATCH))
21512 break;
21516 /* Parse a handler.
21518 handler:
21519 catch ( exception-declaration ) compound-statement */
21521 static void
21522 cp_parser_handler (cp_parser* parser)
21524 tree handler;
21525 tree declaration;
21527 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21528 handler = begin_handler ();
21529 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21530 declaration = cp_parser_exception_declaration (parser);
21531 finish_handler_parms (declaration, handler);
21532 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21533 cp_parser_compound_statement (parser, NULL, false, false);
21534 finish_handler (handler);
21537 /* Parse an exception-declaration.
21539 exception-declaration:
21540 type-specifier-seq declarator
21541 type-specifier-seq abstract-declarator
21542 type-specifier-seq
21545 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21546 ellipsis variant is used. */
21548 static tree
21549 cp_parser_exception_declaration (cp_parser* parser)
21551 cp_decl_specifier_seq type_specifiers;
21552 cp_declarator *declarator;
21553 const char *saved_message;
21555 /* If it's an ellipsis, it's easy to handle. */
21556 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21558 /* Consume the `...' token. */
21559 cp_lexer_consume_token (parser->lexer);
21560 return NULL_TREE;
21563 /* Types may not be defined in exception-declarations. */
21564 saved_message = parser->type_definition_forbidden_message;
21565 parser->type_definition_forbidden_message
21566 = G_("types may not be defined in exception-declarations");
21568 /* Parse the type-specifier-seq. */
21569 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21570 /*is_trailing_return=*/false,
21571 &type_specifiers);
21572 /* If it's a `)', then there is no declarator. */
21573 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21574 declarator = NULL;
21575 else
21576 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21577 /*ctor_dtor_or_conv_p=*/NULL,
21578 /*parenthesized_p=*/NULL,
21579 /*member_p=*/false,
21580 /*friend_p=*/false);
21582 /* Restore the saved message. */
21583 parser->type_definition_forbidden_message = saved_message;
21585 if (!type_specifiers.any_specifiers_p)
21586 return error_mark_node;
21588 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21591 /* Parse a throw-expression.
21593 throw-expression:
21594 throw assignment-expression [opt]
21596 Returns a THROW_EXPR representing the throw-expression. */
21598 static tree
21599 cp_parser_throw_expression (cp_parser* parser)
21601 tree expression;
21602 cp_token* token;
21604 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21605 token = cp_lexer_peek_token (parser->lexer);
21606 /* Figure out whether or not there is an assignment-expression
21607 following the "throw" keyword. */
21608 if (token->type == CPP_COMMA
21609 || token->type == CPP_SEMICOLON
21610 || token->type == CPP_CLOSE_PAREN
21611 || token->type == CPP_CLOSE_SQUARE
21612 || token->type == CPP_CLOSE_BRACE
21613 || token->type == CPP_COLON)
21614 expression = NULL_TREE;
21615 else
21616 expression = cp_parser_assignment_expression (parser);
21618 return build_throw (expression);
21621 /* GNU Extensions */
21623 /* Parse an (optional) asm-specification.
21625 asm-specification:
21626 asm ( string-literal )
21628 If the asm-specification is present, returns a STRING_CST
21629 corresponding to the string-literal. Otherwise, returns
21630 NULL_TREE. */
21632 static tree
21633 cp_parser_asm_specification_opt (cp_parser* parser)
21635 cp_token *token;
21636 tree asm_specification;
21638 /* Peek at the next token. */
21639 token = cp_lexer_peek_token (parser->lexer);
21640 /* If the next token isn't the `asm' keyword, then there's no
21641 asm-specification. */
21642 if (!cp_parser_is_keyword (token, RID_ASM))
21643 return NULL_TREE;
21645 /* Consume the `asm' token. */
21646 cp_lexer_consume_token (parser->lexer);
21647 /* Look for the `('. */
21648 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21650 /* Look for the string-literal. */
21651 asm_specification = cp_parser_string_literal (parser, false, false);
21653 /* Look for the `)'. */
21654 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21656 return asm_specification;
21659 /* Parse an asm-operand-list.
21661 asm-operand-list:
21662 asm-operand
21663 asm-operand-list , asm-operand
21665 asm-operand:
21666 string-literal ( expression )
21667 [ string-literal ] string-literal ( expression )
21669 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21670 each node is the expression. The TREE_PURPOSE is itself a
21671 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21672 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21673 is a STRING_CST for the string literal before the parenthesis. Returns
21674 ERROR_MARK_NODE if any of the operands are invalid. */
21676 static tree
21677 cp_parser_asm_operand_list (cp_parser* parser)
21679 tree asm_operands = NULL_TREE;
21680 bool invalid_operands = false;
21682 while (true)
21684 tree string_literal;
21685 tree expression;
21686 tree name;
21688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21690 /* Consume the `[' token. */
21691 cp_lexer_consume_token (parser->lexer);
21692 /* Read the operand name. */
21693 name = cp_parser_identifier (parser);
21694 if (name != error_mark_node)
21695 name = build_string (IDENTIFIER_LENGTH (name),
21696 IDENTIFIER_POINTER (name));
21697 /* Look for the closing `]'. */
21698 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21700 else
21701 name = NULL_TREE;
21702 /* Look for the string-literal. */
21703 string_literal = cp_parser_string_literal (parser, false, false);
21705 /* Look for the `('. */
21706 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21707 /* Parse the expression. */
21708 expression = cp_parser_expression (parser);
21709 /* Look for the `)'. */
21710 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21712 if (name == error_mark_node
21713 || string_literal == error_mark_node
21714 || expression == error_mark_node)
21715 invalid_operands = true;
21717 /* Add this operand to the list. */
21718 asm_operands = tree_cons (build_tree_list (name, string_literal),
21719 expression,
21720 asm_operands);
21721 /* If the next token is not a `,', there are no more
21722 operands. */
21723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21724 break;
21725 /* Consume the `,'. */
21726 cp_lexer_consume_token (parser->lexer);
21729 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21732 /* Parse an asm-clobber-list.
21734 asm-clobber-list:
21735 string-literal
21736 asm-clobber-list , string-literal
21738 Returns a TREE_LIST, indicating the clobbers in the order that they
21739 appeared. The TREE_VALUE of each node is a STRING_CST. */
21741 static tree
21742 cp_parser_asm_clobber_list (cp_parser* parser)
21744 tree clobbers = NULL_TREE;
21746 while (true)
21748 tree string_literal;
21750 /* Look for the string literal. */
21751 string_literal = cp_parser_string_literal (parser, false, false);
21752 /* Add it to the list. */
21753 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21754 /* If the next token is not a `,', then the list is
21755 complete. */
21756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21757 break;
21758 /* Consume the `,' token. */
21759 cp_lexer_consume_token (parser->lexer);
21762 return clobbers;
21765 /* Parse an asm-label-list.
21767 asm-label-list:
21768 identifier
21769 asm-label-list , identifier
21771 Returns a TREE_LIST, indicating the labels in the order that they
21772 appeared. The TREE_VALUE of each node is a label. */
21774 static tree
21775 cp_parser_asm_label_list (cp_parser* parser)
21777 tree labels = NULL_TREE;
21779 while (true)
21781 tree identifier, label, name;
21783 /* Look for the identifier. */
21784 identifier = cp_parser_identifier (parser);
21785 if (!error_operand_p (identifier))
21787 label = lookup_label (identifier);
21788 if (TREE_CODE (label) == LABEL_DECL)
21790 TREE_USED (label) = 1;
21791 check_goto (label);
21792 name = build_string (IDENTIFIER_LENGTH (identifier),
21793 IDENTIFIER_POINTER (identifier));
21794 labels = tree_cons (name, label, labels);
21797 /* If the next token is not a `,', then the list is
21798 complete. */
21799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21800 break;
21801 /* Consume the `,' token. */
21802 cp_lexer_consume_token (parser->lexer);
21805 return nreverse (labels);
21808 /* Return TRUE iff the next tokens in the stream are possibly the
21809 beginning of a GNU extension attribute. */
21811 static bool
21812 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21814 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21817 /* Return TRUE iff the next tokens in the stream are possibly the
21818 beginning of a standard C++-11 attribute specifier. */
21820 static bool
21821 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21823 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21826 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21827 beginning of a standard C++-11 attribute specifier. */
21829 static bool
21830 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21832 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21834 return (cxx_dialect >= cxx11
21835 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21836 || (token->type == CPP_OPEN_SQUARE
21837 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21838 && token->type == CPP_OPEN_SQUARE)));
21841 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21842 beginning of a GNU extension attribute. */
21844 static bool
21845 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21847 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21849 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21852 /* Return true iff the next tokens can be the beginning of either a
21853 GNU attribute list, or a standard C++11 attribute sequence. */
21855 static bool
21856 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21858 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21859 || cp_next_tokens_can_be_std_attribute_p (parser));
21862 /* Return true iff the next Nth tokens can be the beginning of either
21863 a GNU attribute list, or a standard C++11 attribute sequence. */
21865 static bool
21866 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21868 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21869 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21872 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21873 of GNU attributes, or return NULL. */
21875 static tree
21876 cp_parser_attributes_opt (cp_parser *parser)
21878 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21879 return cp_parser_gnu_attributes_opt (parser);
21880 return cp_parser_std_attribute_spec_seq (parser);
21883 #define CILK_SIMD_FN_CLAUSE_MASK \
21884 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21885 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21886 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21887 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21888 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21890 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21891 vector [(<clauses>)] */
21893 static void
21894 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21896 bool first_p = parser->cilk_simd_fn_info == NULL;
21897 cp_token *token = v_token;
21898 if (first_p)
21900 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21901 parser->cilk_simd_fn_info->error_seen = false;
21902 parser->cilk_simd_fn_info->fndecl_seen = false;
21903 parser->cilk_simd_fn_info->tokens = vNULL;
21905 int paren_scope = 0;
21906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21908 cp_lexer_consume_token (parser->lexer);
21909 v_token = cp_lexer_peek_token (parser->lexer);
21910 paren_scope++;
21912 while (paren_scope > 0)
21914 token = cp_lexer_peek_token (parser->lexer);
21915 if (token->type == CPP_OPEN_PAREN)
21916 paren_scope++;
21917 else if (token->type == CPP_CLOSE_PAREN)
21918 paren_scope--;
21919 /* Do not push the last ')' */
21920 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21921 cp_lexer_consume_token (parser->lexer);
21924 token->type = CPP_PRAGMA_EOL;
21925 parser->lexer->next_token = token;
21926 cp_lexer_consume_token (parser->lexer);
21928 struct cp_token_cache *cp
21929 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21930 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21933 /* Parse an (optional) series of attributes.
21935 attributes:
21936 attributes attribute
21938 attribute:
21939 __attribute__ (( attribute-list [opt] ))
21941 The return value is as for cp_parser_gnu_attribute_list. */
21943 static tree
21944 cp_parser_gnu_attributes_opt (cp_parser* parser)
21946 tree attributes = NULL_TREE;
21948 while (true)
21950 cp_token *token;
21951 tree attribute_list;
21952 bool ok = true;
21954 /* Peek at the next token. */
21955 token = cp_lexer_peek_token (parser->lexer);
21956 /* If it's not `__attribute__', then we're done. */
21957 if (token->keyword != RID_ATTRIBUTE)
21958 break;
21960 /* Consume the `__attribute__' keyword. */
21961 cp_lexer_consume_token (parser->lexer);
21962 /* Look for the two `(' tokens. */
21963 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21964 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21966 /* Peek at the next token. */
21967 token = cp_lexer_peek_token (parser->lexer);
21968 if (token->type != CPP_CLOSE_PAREN)
21969 /* Parse the attribute-list. */
21970 attribute_list = cp_parser_gnu_attribute_list (parser);
21971 else
21972 /* If the next token is a `)', then there is no attribute
21973 list. */
21974 attribute_list = NULL;
21976 /* Look for the two `)' tokens. */
21977 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21978 ok = false;
21979 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21980 ok = false;
21981 if (!ok)
21982 cp_parser_skip_to_end_of_statement (parser);
21984 /* Add these new attributes to the list. */
21985 attributes = chainon (attributes, attribute_list);
21988 return attributes;
21991 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21992 "__vector" or "__vector__." */
21994 static inline bool
21995 is_cilkplus_vector_p (tree name)
21997 if (flag_cilkplus && is_attribute_p ("vector", name))
21998 return true;
21999 return false;
22002 /* Parse a GNU attribute-list.
22004 attribute-list:
22005 attribute
22006 attribute-list , attribute
22008 attribute:
22009 identifier
22010 identifier ( identifier )
22011 identifier ( identifier , expression-list )
22012 identifier ( expression-list )
22014 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22015 to an attribute. The TREE_PURPOSE of each node is the identifier
22016 indicating which attribute is in use. The TREE_VALUE represents
22017 the arguments, if any. */
22019 static tree
22020 cp_parser_gnu_attribute_list (cp_parser* parser)
22022 tree attribute_list = NULL_TREE;
22023 bool save_translate_strings_p = parser->translate_strings_p;
22025 parser->translate_strings_p = false;
22026 while (true)
22028 cp_token *token;
22029 tree identifier;
22030 tree attribute;
22032 /* Look for the identifier. We also allow keywords here; for
22033 example `__attribute__ ((const))' is legal. */
22034 token = cp_lexer_peek_token (parser->lexer);
22035 if (token->type == CPP_NAME
22036 || token->type == CPP_KEYWORD)
22038 tree arguments = NULL_TREE;
22040 /* Consume the token, but save it since we need it for the
22041 SIMD enabled function parsing. */
22042 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22044 /* Save away the identifier that indicates which attribute
22045 this is. */
22046 identifier = (token->type == CPP_KEYWORD)
22047 /* For keywords, use the canonical spelling, not the
22048 parsed identifier. */
22049 ? ridpointers[(int) token->keyword]
22050 : id_token->u.value;
22052 attribute = build_tree_list (identifier, NULL_TREE);
22054 /* Peek at the next token. */
22055 token = cp_lexer_peek_token (parser->lexer);
22056 /* If it's an `(', then parse the attribute arguments. */
22057 if (token->type == CPP_OPEN_PAREN)
22059 vec<tree, va_gc> *vec;
22060 int attr_flag = (attribute_takes_identifier_p (identifier)
22061 ? id_attr : normal_attr);
22062 if (is_cilkplus_vector_p (identifier))
22064 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22065 continue;
22067 else
22068 vec = cp_parser_parenthesized_expression_list
22069 (parser, attr_flag, /*cast_p=*/false,
22070 /*allow_expansion_p=*/false,
22071 /*non_constant_p=*/NULL);
22072 if (vec == NULL)
22073 arguments = error_mark_node;
22074 else
22076 arguments = build_tree_list_vec (vec);
22077 release_tree_vector (vec);
22079 /* Save the arguments away. */
22080 TREE_VALUE (attribute) = arguments;
22082 else if (is_cilkplus_vector_p (identifier))
22084 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22085 continue;
22088 if (arguments != error_mark_node)
22090 /* Add this attribute to the list. */
22091 TREE_CHAIN (attribute) = attribute_list;
22092 attribute_list = attribute;
22095 token = cp_lexer_peek_token (parser->lexer);
22097 /* Now, look for more attributes. If the next token isn't a
22098 `,', we're done. */
22099 if (token->type != CPP_COMMA)
22100 break;
22102 /* Consume the comma and keep going. */
22103 cp_lexer_consume_token (parser->lexer);
22105 parser->translate_strings_p = save_translate_strings_p;
22107 /* We built up the list in reverse order. */
22108 return nreverse (attribute_list);
22111 /* Parse a standard C++11 attribute.
22113 The returned representation is a TREE_LIST which TREE_PURPOSE is
22114 the scoped name of the attribute, and the TREE_VALUE is its
22115 arguments list.
22117 Note that the scoped name of the attribute is itself a TREE_LIST
22118 which TREE_PURPOSE is the namespace of the attribute, and
22119 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22120 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22121 and which TREE_PURPOSE is directly the attribute name.
22123 Clients of the attribute code should use get_attribute_namespace
22124 and get_attribute_name to get the actual namespace and name of
22125 attributes, regardless of their being GNU or C++11 attributes.
22127 attribute:
22128 attribute-token attribute-argument-clause [opt]
22130 attribute-token:
22131 identifier
22132 attribute-scoped-token
22134 attribute-scoped-token:
22135 attribute-namespace :: identifier
22137 attribute-namespace:
22138 identifier
22140 attribute-argument-clause:
22141 ( balanced-token-seq )
22143 balanced-token-seq:
22144 balanced-token [opt]
22145 balanced-token-seq balanced-token
22147 balanced-token:
22148 ( balanced-token-seq )
22149 [ balanced-token-seq ]
22150 { balanced-token-seq }. */
22152 static tree
22153 cp_parser_std_attribute (cp_parser *parser)
22155 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22156 cp_token *token;
22158 /* First, parse name of the the attribute, a.k.a
22159 attribute-token. */
22161 token = cp_lexer_peek_token (parser->lexer);
22162 if (token->type == CPP_NAME)
22163 attr_id = token->u.value;
22164 else if (token->type == CPP_KEYWORD)
22165 attr_id = ridpointers[(int) token->keyword];
22166 else if (token->flags & NAMED_OP)
22167 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22169 if (attr_id == NULL_TREE)
22170 return NULL_TREE;
22172 cp_lexer_consume_token (parser->lexer);
22174 token = cp_lexer_peek_token (parser->lexer);
22175 if (token->type == CPP_SCOPE)
22177 /* We are seeing a scoped attribute token. */
22179 cp_lexer_consume_token (parser->lexer);
22180 attr_ns = attr_id;
22182 token = cp_lexer_consume_token (parser->lexer);
22183 if (token->type == CPP_NAME)
22184 attr_id = token->u.value;
22185 else if (token->type == CPP_KEYWORD)
22186 attr_id = ridpointers[(int) token->keyword];
22187 else
22189 error_at (token->location,
22190 "expected an identifier for the attribute name");
22191 return error_mark_node;
22193 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22194 NULL_TREE);
22195 token = cp_lexer_peek_token (parser->lexer);
22197 else
22199 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22200 NULL_TREE);
22201 /* C++11 noreturn attribute is equivalent to GNU's. */
22202 if (is_attribute_p ("noreturn", attr_id))
22203 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22204 /* C++14 deprecated attribute is equivalent to GNU's. */
22205 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22207 if (cxx_dialect == cxx11)
22208 pedwarn (token->location, OPT_Wpedantic,
22209 "%<deprecated%> is a C++14 feature;"
22210 " use %<gnu::deprecated%>");
22211 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22215 /* Now parse the optional argument clause of the attribute. */
22217 if (token->type != CPP_OPEN_PAREN)
22218 return attribute;
22221 vec<tree, va_gc> *vec;
22222 int attr_flag = normal_attr;
22224 if (attr_ns == get_identifier ("gnu")
22225 && attribute_takes_identifier_p (attr_id))
22226 /* A GNU attribute that takes an identifier in parameter. */
22227 attr_flag = id_attr;
22229 vec = cp_parser_parenthesized_expression_list
22230 (parser, attr_flag, /*cast_p=*/false,
22231 /*allow_expansion_p=*/true,
22232 /*non_constant_p=*/NULL);
22233 if (vec == NULL)
22234 arguments = error_mark_node;
22235 else
22237 arguments = build_tree_list_vec (vec);
22238 release_tree_vector (vec);
22241 if (arguments == error_mark_node)
22242 attribute = error_mark_node;
22243 else
22244 TREE_VALUE (attribute) = arguments;
22247 return attribute;
22250 /* Parse a list of standard C++-11 attributes.
22252 attribute-list:
22253 attribute [opt]
22254 attribute-list , attribute[opt]
22255 attribute ...
22256 attribute-list , attribute ...
22259 static tree
22260 cp_parser_std_attribute_list (cp_parser *parser)
22262 tree attributes = NULL_TREE, attribute = NULL_TREE;
22263 cp_token *token = NULL;
22265 while (true)
22267 attribute = cp_parser_std_attribute (parser);
22268 if (attribute == error_mark_node)
22269 break;
22270 if (attribute != NULL_TREE)
22272 TREE_CHAIN (attribute) = attributes;
22273 attributes = attribute;
22275 token = cp_lexer_peek_token (parser->lexer);
22276 if (token->type != CPP_COMMA)
22277 break;
22278 cp_lexer_consume_token (parser->lexer);
22280 attributes = nreverse (attributes);
22281 return attributes;
22284 /* Parse a standard C++-11 attribute specifier.
22286 attribute-specifier:
22287 [ [ attribute-list ] ]
22288 alignment-specifier
22290 alignment-specifier:
22291 alignas ( type-id ... [opt] )
22292 alignas ( alignment-expression ... [opt] ). */
22294 static tree
22295 cp_parser_std_attribute_spec (cp_parser *parser)
22297 tree attributes = NULL_TREE;
22298 cp_token *token = cp_lexer_peek_token (parser->lexer);
22300 if (token->type == CPP_OPEN_SQUARE
22301 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22303 cp_lexer_consume_token (parser->lexer);
22304 cp_lexer_consume_token (parser->lexer);
22306 attributes = cp_parser_std_attribute_list (parser);
22308 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22309 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22310 cp_parser_skip_to_end_of_statement (parser);
22311 else
22312 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22313 when we are sure that we have actually parsed them. */
22314 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22316 else
22318 tree alignas_expr;
22320 /* Look for an alignment-specifier. */
22322 token = cp_lexer_peek_token (parser->lexer);
22324 if (token->type != CPP_KEYWORD
22325 || token->keyword != RID_ALIGNAS)
22326 return NULL_TREE;
22328 cp_lexer_consume_token (parser->lexer);
22329 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22331 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22333 cp_parser_error (parser, "expected %<(%>");
22334 return error_mark_node;
22337 cp_parser_parse_tentatively (parser);
22338 alignas_expr = cp_parser_type_id (parser);
22340 if (!cp_parser_parse_definitely (parser))
22342 gcc_assert (alignas_expr == error_mark_node
22343 || alignas_expr == NULL_TREE);
22345 alignas_expr =
22346 cp_parser_assignment_expression (parser);
22347 if (alignas_expr == error_mark_node)
22348 cp_parser_skip_to_end_of_statement (parser);
22349 if (alignas_expr == NULL_TREE
22350 || alignas_expr == error_mark_node)
22351 return alignas_expr;
22354 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22356 cp_parser_error (parser, "expected %<)%>");
22357 return error_mark_node;
22360 alignas_expr = cxx_alignas_expr (alignas_expr);
22362 /* Build the C++-11 representation of an 'aligned'
22363 attribute. */
22364 attributes =
22365 build_tree_list (build_tree_list (get_identifier ("gnu"),
22366 get_identifier ("aligned")),
22367 build_tree_list (NULL_TREE, alignas_expr));
22370 return attributes;
22373 /* Parse a standard C++-11 attribute-specifier-seq.
22375 attribute-specifier-seq:
22376 attribute-specifier-seq [opt] attribute-specifier
22379 static tree
22380 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22382 tree attr_specs = NULL;
22384 while (true)
22386 tree attr_spec = cp_parser_std_attribute_spec (parser);
22387 if (attr_spec == NULL_TREE)
22388 break;
22389 if (attr_spec == error_mark_node)
22390 return error_mark_node;
22392 TREE_CHAIN (attr_spec) = attr_specs;
22393 attr_specs = attr_spec;
22396 attr_specs = nreverse (attr_specs);
22397 return attr_specs;
22400 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22401 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22402 current value of the PEDANTIC flag, regardless of whether or not
22403 the `__extension__' keyword is present. The caller is responsible
22404 for restoring the value of the PEDANTIC flag. */
22406 static bool
22407 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22409 /* Save the old value of the PEDANTIC flag. */
22410 *saved_pedantic = pedantic;
22412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22414 /* Consume the `__extension__' token. */
22415 cp_lexer_consume_token (parser->lexer);
22416 /* We're not being pedantic while the `__extension__' keyword is
22417 in effect. */
22418 pedantic = 0;
22420 return true;
22423 return false;
22426 /* Parse a label declaration.
22428 label-declaration:
22429 __label__ label-declarator-seq ;
22431 label-declarator-seq:
22432 identifier , label-declarator-seq
22433 identifier */
22435 static void
22436 cp_parser_label_declaration (cp_parser* parser)
22438 /* Look for the `__label__' keyword. */
22439 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22441 while (true)
22443 tree identifier;
22445 /* Look for an identifier. */
22446 identifier = cp_parser_identifier (parser);
22447 /* If we failed, stop. */
22448 if (identifier == error_mark_node)
22449 break;
22450 /* Declare it as a label. */
22451 finish_label_decl (identifier);
22452 /* If the next token is a `;', stop. */
22453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22454 break;
22455 /* Look for the `,' separating the label declarations. */
22456 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22459 /* Look for the final `;'. */
22460 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22463 /* Support Functions */
22465 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22466 NAME should have one of the representations used for an
22467 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22468 is returned. If PARSER->SCOPE is a dependent type, then a
22469 SCOPE_REF is returned.
22471 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22472 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22473 was formed. Abstractly, such entities should not be passed to this
22474 function, because they do not need to be looked up, but it is
22475 simpler to check for this special case here, rather than at the
22476 call-sites.
22478 In cases not explicitly covered above, this function returns a
22479 DECL, OVERLOAD, or baselink representing the result of the lookup.
22480 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22481 is returned.
22483 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22484 (e.g., "struct") that was used. In that case bindings that do not
22485 refer to types are ignored.
22487 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22488 ignored.
22490 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22491 are ignored.
22493 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22494 types.
22496 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22497 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22498 NULL_TREE otherwise. */
22500 static tree
22501 cp_parser_lookup_name (cp_parser *parser, tree name,
22502 enum tag_types tag_type,
22503 bool is_template,
22504 bool is_namespace,
22505 bool check_dependency,
22506 tree *ambiguous_decls,
22507 location_t name_location)
22509 tree decl;
22510 tree object_type = parser->context->object_type;
22512 /* Assume that the lookup will be unambiguous. */
22513 if (ambiguous_decls)
22514 *ambiguous_decls = NULL_TREE;
22516 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22517 no longer valid. Note that if we are parsing tentatively, and
22518 the parse fails, OBJECT_TYPE will be automatically restored. */
22519 parser->context->object_type = NULL_TREE;
22521 if (name == error_mark_node)
22522 return error_mark_node;
22524 /* A template-id has already been resolved; there is no lookup to
22525 do. */
22526 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22527 return name;
22528 if (BASELINK_P (name))
22530 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22531 == TEMPLATE_ID_EXPR);
22532 return name;
22535 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22536 it should already have been checked to make sure that the name
22537 used matches the type being destroyed. */
22538 if (TREE_CODE (name) == BIT_NOT_EXPR)
22540 tree type;
22542 /* Figure out to which type this destructor applies. */
22543 if (parser->scope)
22544 type = parser->scope;
22545 else if (object_type)
22546 type = object_type;
22547 else
22548 type = current_class_type;
22549 /* If that's not a class type, there is no destructor. */
22550 if (!type || !CLASS_TYPE_P (type))
22551 return error_mark_node;
22552 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22553 lazily_declare_fn (sfk_destructor, type);
22554 if (!CLASSTYPE_DESTRUCTORS (type))
22555 return error_mark_node;
22556 /* If it was a class type, return the destructor. */
22557 return CLASSTYPE_DESTRUCTORS (type);
22560 /* By this point, the NAME should be an ordinary identifier. If
22561 the id-expression was a qualified name, the qualifying scope is
22562 stored in PARSER->SCOPE at this point. */
22563 gcc_assert (identifier_p (name));
22565 /* Perform the lookup. */
22566 if (parser->scope)
22568 bool dependent_p;
22570 if (parser->scope == error_mark_node)
22571 return error_mark_node;
22573 /* If the SCOPE is dependent, the lookup must be deferred until
22574 the template is instantiated -- unless we are explicitly
22575 looking up names in uninstantiated templates. Even then, we
22576 cannot look up the name if the scope is not a class type; it
22577 might, for example, be a template type parameter. */
22578 dependent_p = (TYPE_P (parser->scope)
22579 && dependent_scope_p (parser->scope));
22580 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22581 && dependent_p)
22582 /* Defer lookup. */
22583 decl = error_mark_node;
22584 else
22586 tree pushed_scope = NULL_TREE;
22588 /* If PARSER->SCOPE is a dependent type, then it must be a
22589 class type, and we must not be checking dependencies;
22590 otherwise, we would have processed this lookup above. So
22591 that PARSER->SCOPE is not considered a dependent base by
22592 lookup_member, we must enter the scope here. */
22593 if (dependent_p)
22594 pushed_scope = push_scope (parser->scope);
22596 /* If the PARSER->SCOPE is a template specialization, it
22597 may be instantiated during name lookup. In that case,
22598 errors may be issued. Even if we rollback the current
22599 tentative parse, those errors are valid. */
22600 decl = lookup_qualified_name (parser->scope, name,
22601 tag_type != none_type,
22602 /*complain=*/true);
22604 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22605 lookup result and the nested-name-specifier nominates a class C:
22606 * if the name specified after the nested-name-specifier, when
22607 looked up in C, is the injected-class-name of C (Clause 9), or
22608 * if the name specified after the nested-name-specifier is the
22609 same as the identifier or the simple-template-id's template-
22610 name in the last component of the nested-name-specifier,
22611 the name is instead considered to name the constructor of
22612 class C. [ Note: for example, the constructor is not an
22613 acceptable lookup result in an elaborated-type-specifier so
22614 the constructor would not be used in place of the
22615 injected-class-name. --end note ] Such a constructor name
22616 shall be used only in the declarator-id of a declaration that
22617 names a constructor or in a using-declaration. */
22618 if (tag_type == none_type
22619 && DECL_SELF_REFERENCE_P (decl)
22620 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22621 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22622 tag_type != none_type,
22623 /*complain=*/true);
22625 /* If we have a single function from a using decl, pull it out. */
22626 if (TREE_CODE (decl) == OVERLOAD
22627 && !really_overloaded_fn (decl))
22628 decl = OVL_FUNCTION (decl);
22630 if (pushed_scope)
22631 pop_scope (pushed_scope);
22634 /* If the scope is a dependent type and either we deferred lookup or
22635 we did lookup but didn't find the name, rememeber the name. */
22636 if (decl == error_mark_node && TYPE_P (parser->scope)
22637 && dependent_type_p (parser->scope))
22639 if (tag_type)
22641 tree type;
22643 /* The resolution to Core Issue 180 says that `struct
22644 A::B' should be considered a type-name, even if `A'
22645 is dependent. */
22646 type = make_typename_type (parser->scope, name, tag_type,
22647 /*complain=*/tf_error);
22648 if (type != error_mark_node)
22649 decl = TYPE_NAME (type);
22651 else if (is_template
22652 && (cp_parser_next_token_ends_template_argument_p (parser)
22653 || cp_lexer_next_token_is (parser->lexer,
22654 CPP_CLOSE_PAREN)))
22655 decl = make_unbound_class_template (parser->scope,
22656 name, NULL_TREE,
22657 /*complain=*/tf_error);
22658 else
22659 decl = build_qualified_name (/*type=*/NULL_TREE,
22660 parser->scope, name,
22661 is_template);
22663 parser->qualifying_scope = parser->scope;
22664 parser->object_scope = NULL_TREE;
22666 else if (object_type)
22668 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22669 OBJECT_TYPE is not a class. */
22670 if (CLASS_TYPE_P (object_type))
22671 /* If the OBJECT_TYPE is a template specialization, it may
22672 be instantiated during name lookup. In that case, errors
22673 may be issued. Even if we rollback the current tentative
22674 parse, those errors are valid. */
22675 decl = lookup_member (object_type,
22676 name,
22677 /*protect=*/0,
22678 tag_type != none_type,
22679 tf_warning_or_error);
22680 else
22681 decl = NULL_TREE;
22683 if (!decl)
22684 /* Look it up in the enclosing context. */
22685 decl = lookup_name_real (name, tag_type != none_type,
22686 /*nonclass=*/0,
22687 /*block_p=*/true, is_namespace, 0);
22688 parser->object_scope = object_type;
22689 parser->qualifying_scope = NULL_TREE;
22691 else
22693 decl = lookup_name_real (name, tag_type != none_type,
22694 /*nonclass=*/0,
22695 /*block_p=*/true, is_namespace, 0);
22696 parser->qualifying_scope = NULL_TREE;
22697 parser->object_scope = NULL_TREE;
22700 /* If the lookup failed, let our caller know. */
22701 if (!decl || decl == error_mark_node)
22702 return error_mark_node;
22704 /* Pull out the template from an injected-class-name (or multiple). */
22705 if (is_template)
22706 decl = maybe_get_template_decl_from_type_decl (decl);
22708 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22709 if (TREE_CODE (decl) == TREE_LIST)
22711 if (ambiguous_decls)
22712 *ambiguous_decls = decl;
22713 /* The error message we have to print is too complicated for
22714 cp_parser_error, so we incorporate its actions directly. */
22715 if (!cp_parser_simulate_error (parser))
22717 error_at (name_location, "reference to %qD is ambiguous",
22718 name);
22719 print_candidates (decl);
22721 return error_mark_node;
22724 gcc_assert (DECL_P (decl)
22725 || TREE_CODE (decl) == OVERLOAD
22726 || TREE_CODE (decl) == SCOPE_REF
22727 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22728 || BASELINK_P (decl));
22730 /* If we have resolved the name of a member declaration, check to
22731 see if the declaration is accessible. When the name resolves to
22732 set of overloaded functions, accessibility is checked when
22733 overload resolution is done.
22735 During an explicit instantiation, access is not checked at all,
22736 as per [temp.explicit]. */
22737 if (DECL_P (decl))
22738 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22740 maybe_record_typedef_use (decl);
22742 return decl;
22745 /* Like cp_parser_lookup_name, but for use in the typical case where
22746 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22747 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22749 static tree
22750 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22752 return cp_parser_lookup_name (parser, name,
22753 none_type,
22754 /*is_template=*/false,
22755 /*is_namespace=*/false,
22756 /*check_dependency=*/true,
22757 /*ambiguous_decls=*/NULL,
22758 location);
22761 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22762 the current context, return the TYPE_DECL. If TAG_NAME_P is
22763 true, the DECL indicates the class being defined in a class-head,
22764 or declared in an elaborated-type-specifier.
22766 Otherwise, return DECL. */
22768 static tree
22769 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22771 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22772 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22774 struct A {
22775 template <typename T> struct B;
22778 template <typename T> struct A::B {};
22780 Similarly, in an elaborated-type-specifier:
22782 namespace N { struct X{}; }
22784 struct A {
22785 template <typename T> friend struct N::X;
22788 However, if the DECL refers to a class type, and we are in
22789 the scope of the class, then the name lookup automatically
22790 finds the TYPE_DECL created by build_self_reference rather
22791 than a TEMPLATE_DECL. For example, in:
22793 template <class T> struct S {
22794 S s;
22797 there is no need to handle such case. */
22799 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22800 return DECL_TEMPLATE_RESULT (decl);
22802 return decl;
22805 /* If too many, or too few, template-parameter lists apply to the
22806 declarator, issue an error message. Returns TRUE if all went well,
22807 and FALSE otherwise. */
22809 static bool
22810 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22811 cp_declarator *declarator,
22812 location_t declarator_location)
22814 switch (declarator->kind)
22816 case cdk_id:
22818 unsigned num_templates = 0;
22819 tree scope = declarator->u.id.qualifying_scope;
22821 if (scope)
22822 num_templates = num_template_headers_for_class (scope);
22823 else if (TREE_CODE (declarator->u.id.unqualified_name)
22824 == TEMPLATE_ID_EXPR)
22825 /* If the DECLARATOR has the form `X<y>' then it uses one
22826 additional level of template parameters. */
22827 ++num_templates;
22829 return cp_parser_check_template_parameters
22830 (parser, num_templates, declarator_location, declarator);
22833 case cdk_function:
22834 case cdk_array:
22835 case cdk_pointer:
22836 case cdk_reference:
22837 case cdk_ptrmem:
22838 return (cp_parser_check_declarator_template_parameters
22839 (parser, declarator->declarator, declarator_location));
22841 case cdk_error:
22842 return true;
22844 default:
22845 gcc_unreachable ();
22847 return false;
22850 /* NUM_TEMPLATES were used in the current declaration. If that is
22851 invalid, return FALSE and issue an error messages. Otherwise,
22852 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22853 declarator and we can print more accurate diagnostics. */
22855 static bool
22856 cp_parser_check_template_parameters (cp_parser* parser,
22857 unsigned num_templates,
22858 location_t location,
22859 cp_declarator *declarator)
22861 /* If there are the same number of template classes and parameter
22862 lists, that's OK. */
22863 if (parser->num_template_parameter_lists == num_templates)
22864 return true;
22865 /* If there are more, but only one more, then we are referring to a
22866 member template. That's OK too. */
22867 if (parser->num_template_parameter_lists == num_templates + 1)
22868 return true;
22869 /* If there are more template classes than parameter lists, we have
22870 something like:
22872 template <class T> void S<T>::R<T>::f (); */
22873 if (parser->num_template_parameter_lists < num_templates)
22875 if (declarator && !current_function_decl)
22876 error_at (location, "specializing member %<%T::%E%> "
22877 "requires %<template<>%> syntax",
22878 declarator->u.id.qualifying_scope,
22879 declarator->u.id.unqualified_name);
22880 else if (declarator)
22881 error_at (location, "invalid declaration of %<%T::%E%>",
22882 declarator->u.id.qualifying_scope,
22883 declarator->u.id.unqualified_name);
22884 else
22885 error_at (location, "too few template-parameter-lists");
22886 return false;
22888 /* Otherwise, there are too many template parameter lists. We have
22889 something like:
22891 template <class T> template <class U> void S::f(); */
22892 error_at (location, "too many template-parameter-lists");
22893 return false;
22896 /* Parse an optional `::' token indicating that the following name is
22897 from the global namespace. If so, PARSER->SCOPE is set to the
22898 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22899 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22900 Returns the new value of PARSER->SCOPE, if the `::' token is
22901 present, and NULL_TREE otherwise. */
22903 static tree
22904 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22906 cp_token *token;
22908 /* Peek at the next token. */
22909 token = cp_lexer_peek_token (parser->lexer);
22910 /* If we're looking at a `::' token then we're starting from the
22911 global namespace, not our current location. */
22912 if (token->type == CPP_SCOPE)
22914 /* Consume the `::' token. */
22915 cp_lexer_consume_token (parser->lexer);
22916 /* Set the SCOPE so that we know where to start the lookup. */
22917 parser->scope = global_namespace;
22918 parser->qualifying_scope = global_namespace;
22919 parser->object_scope = NULL_TREE;
22921 return parser->scope;
22923 else if (!current_scope_valid_p)
22925 parser->scope = NULL_TREE;
22926 parser->qualifying_scope = NULL_TREE;
22927 parser->object_scope = NULL_TREE;
22930 return NULL_TREE;
22933 /* Returns TRUE if the upcoming token sequence is the start of a
22934 constructor declarator. If FRIEND_P is true, the declarator is
22935 preceded by the `friend' specifier. */
22937 static bool
22938 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22940 bool constructor_p;
22941 bool outside_class_specifier_p;
22942 tree nested_name_specifier;
22943 cp_token *next_token;
22945 /* The common case is that this is not a constructor declarator, so
22946 try to avoid doing lots of work if at all possible. It's not
22947 valid declare a constructor at function scope. */
22948 if (parser->in_function_body)
22949 return false;
22950 /* And only certain tokens can begin a constructor declarator. */
22951 next_token = cp_lexer_peek_token (parser->lexer);
22952 if (next_token->type != CPP_NAME
22953 && next_token->type != CPP_SCOPE
22954 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22955 && next_token->type != CPP_TEMPLATE_ID)
22956 return false;
22958 /* Parse tentatively; we are going to roll back all of the tokens
22959 consumed here. */
22960 cp_parser_parse_tentatively (parser);
22961 /* Assume that we are looking at a constructor declarator. */
22962 constructor_p = true;
22964 /* Look for the optional `::' operator. */
22965 cp_parser_global_scope_opt (parser,
22966 /*current_scope_valid_p=*/false);
22967 /* Look for the nested-name-specifier. */
22968 nested_name_specifier
22969 = (cp_parser_nested_name_specifier_opt (parser,
22970 /*typename_keyword_p=*/false,
22971 /*check_dependency_p=*/false,
22972 /*type_p=*/false,
22973 /*is_declaration=*/false));
22975 outside_class_specifier_p = (!at_class_scope_p ()
22976 || !TYPE_BEING_DEFINED (current_class_type)
22977 || friend_p);
22979 /* Outside of a class-specifier, there must be a
22980 nested-name-specifier. */
22981 if (!nested_name_specifier && outside_class_specifier_p)
22982 constructor_p = false;
22983 else if (nested_name_specifier == error_mark_node)
22984 constructor_p = false;
22986 /* If we have a class scope, this is easy; DR 147 says that S::S always
22987 names the constructor, and no other qualified name could. */
22988 if (constructor_p && nested_name_specifier
22989 && CLASS_TYPE_P (nested_name_specifier))
22991 tree id = cp_parser_unqualified_id (parser,
22992 /*template_keyword_p=*/false,
22993 /*check_dependency_p=*/false,
22994 /*declarator_p=*/true,
22995 /*optional_p=*/false);
22996 if (is_overloaded_fn (id))
22997 id = DECL_NAME (get_first_fn (id));
22998 if (!constructor_name_p (id, nested_name_specifier))
22999 constructor_p = false;
23001 /* If we still think that this might be a constructor-declarator,
23002 look for a class-name. */
23003 else if (constructor_p)
23005 /* If we have:
23007 template <typename T> struct S {
23008 S();
23011 we must recognize that the nested `S' names a class. */
23012 tree type_decl;
23013 type_decl = cp_parser_class_name (parser,
23014 /*typename_keyword_p=*/false,
23015 /*template_keyword_p=*/false,
23016 none_type,
23017 /*check_dependency_p=*/false,
23018 /*class_head_p=*/false,
23019 /*is_declaration=*/false);
23020 /* If there was no class-name, then this is not a constructor.
23021 Otherwise, if we are in a class-specifier and we aren't
23022 handling a friend declaration, check that its type matches
23023 current_class_type (c++/38313). Note: error_mark_node
23024 is left alone for error recovery purposes. */
23025 constructor_p = (!cp_parser_error_occurred (parser)
23026 && (outside_class_specifier_p
23027 || type_decl == error_mark_node
23028 || same_type_p (current_class_type,
23029 TREE_TYPE (type_decl))));
23031 /* If we're still considering a constructor, we have to see a `(',
23032 to begin the parameter-declaration-clause, followed by either a
23033 `)', an `...', or a decl-specifier. We need to check for a
23034 type-specifier to avoid being fooled into thinking that:
23036 S (f) (int);
23038 is a constructor. (It is actually a function named `f' that
23039 takes one parameter (of type `int') and returns a value of type
23040 `S'. */
23041 if (constructor_p
23042 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23043 constructor_p = false;
23045 if (constructor_p
23046 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23047 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23048 /* A parameter declaration begins with a decl-specifier,
23049 which is either the "attribute" keyword, a storage class
23050 specifier, or (usually) a type-specifier. */
23051 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23053 tree type;
23054 tree pushed_scope = NULL_TREE;
23055 unsigned saved_num_template_parameter_lists;
23057 /* Names appearing in the type-specifier should be looked up
23058 in the scope of the class. */
23059 if (current_class_type)
23060 type = NULL_TREE;
23061 else
23063 type = TREE_TYPE (type_decl);
23064 if (TREE_CODE (type) == TYPENAME_TYPE)
23066 type = resolve_typename_type (type,
23067 /*only_current_p=*/false);
23068 if (TREE_CODE (type) == TYPENAME_TYPE)
23070 cp_parser_abort_tentative_parse (parser);
23071 return false;
23074 pushed_scope = push_scope (type);
23077 /* Inside the constructor parameter list, surrounding
23078 template-parameter-lists do not apply. */
23079 saved_num_template_parameter_lists
23080 = parser->num_template_parameter_lists;
23081 parser->num_template_parameter_lists = 0;
23083 /* Look for the type-specifier. */
23084 cp_parser_type_specifier (parser,
23085 CP_PARSER_FLAGS_NONE,
23086 /*decl_specs=*/NULL,
23087 /*is_declarator=*/true,
23088 /*declares_class_or_enum=*/NULL,
23089 /*is_cv_qualifier=*/NULL);
23091 parser->num_template_parameter_lists
23092 = saved_num_template_parameter_lists;
23094 /* Leave the scope of the class. */
23095 if (pushed_scope)
23096 pop_scope (pushed_scope);
23098 constructor_p = !cp_parser_error_occurred (parser);
23102 /* We did not really want to consume any tokens. */
23103 cp_parser_abort_tentative_parse (parser);
23105 return constructor_p;
23108 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23109 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23110 they must be performed once we are in the scope of the function.
23112 Returns the function defined. */
23114 static tree
23115 cp_parser_function_definition_from_specifiers_and_declarator
23116 (cp_parser* parser,
23117 cp_decl_specifier_seq *decl_specifiers,
23118 tree attributes,
23119 const cp_declarator *declarator)
23121 tree fn;
23122 bool success_p;
23124 /* Begin the function-definition. */
23125 success_p = start_function (decl_specifiers, declarator, attributes);
23127 /* The things we're about to see are not directly qualified by any
23128 template headers we've seen thus far. */
23129 reset_specialization ();
23131 /* If there were names looked up in the decl-specifier-seq that we
23132 did not check, check them now. We must wait until we are in the
23133 scope of the function to perform the checks, since the function
23134 might be a friend. */
23135 perform_deferred_access_checks (tf_warning_or_error);
23137 if (success_p)
23139 cp_finalize_omp_declare_simd (parser, current_function_decl);
23140 parser->omp_declare_simd = NULL;
23143 if (!success_p)
23145 /* Skip the entire function. */
23146 cp_parser_skip_to_end_of_block_or_statement (parser);
23147 fn = error_mark_node;
23149 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23151 /* Seen already, skip it. An error message has already been output. */
23152 cp_parser_skip_to_end_of_block_or_statement (parser);
23153 fn = current_function_decl;
23154 current_function_decl = NULL_TREE;
23155 /* If this is a function from a class, pop the nested class. */
23156 if (current_class_name)
23157 pop_nested_class ();
23159 else
23161 timevar_id_t tv;
23162 if (DECL_DECLARED_INLINE_P (current_function_decl))
23163 tv = TV_PARSE_INLINE;
23164 else
23165 tv = TV_PARSE_FUNC;
23166 timevar_push (tv);
23167 fn = cp_parser_function_definition_after_declarator (parser,
23168 /*inline_p=*/false);
23169 timevar_pop (tv);
23172 return fn;
23175 /* Parse the part of a function-definition that follows the
23176 declarator. INLINE_P is TRUE iff this function is an inline
23177 function defined within a class-specifier.
23179 Returns the function defined. */
23181 static tree
23182 cp_parser_function_definition_after_declarator (cp_parser* parser,
23183 bool inline_p)
23185 tree fn;
23186 bool ctor_initializer_p = false;
23187 bool saved_in_unbraced_linkage_specification_p;
23188 bool saved_in_function_body;
23189 unsigned saved_num_template_parameter_lists;
23190 cp_token *token;
23191 bool fully_implicit_function_template_p
23192 = parser->fully_implicit_function_template_p;
23193 parser->fully_implicit_function_template_p = false;
23194 tree implicit_template_parms
23195 = parser->implicit_template_parms;
23196 parser->implicit_template_parms = 0;
23197 cp_binding_level* implicit_template_scope
23198 = parser->implicit_template_scope;
23199 parser->implicit_template_scope = 0;
23201 saved_in_function_body = parser->in_function_body;
23202 parser->in_function_body = true;
23203 /* If the next token is `return', then the code may be trying to
23204 make use of the "named return value" extension that G++ used to
23205 support. */
23206 token = cp_lexer_peek_token (parser->lexer);
23207 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23209 /* Consume the `return' keyword. */
23210 cp_lexer_consume_token (parser->lexer);
23211 /* Look for the identifier that indicates what value is to be
23212 returned. */
23213 cp_parser_identifier (parser);
23214 /* Issue an error message. */
23215 error_at (token->location,
23216 "named return values are no longer supported");
23217 /* Skip tokens until we reach the start of the function body. */
23218 while (true)
23220 cp_token *token = cp_lexer_peek_token (parser->lexer);
23221 if (token->type == CPP_OPEN_BRACE
23222 || token->type == CPP_EOF
23223 || token->type == CPP_PRAGMA_EOL)
23224 break;
23225 cp_lexer_consume_token (parser->lexer);
23228 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23229 anything declared inside `f'. */
23230 saved_in_unbraced_linkage_specification_p
23231 = parser->in_unbraced_linkage_specification_p;
23232 parser->in_unbraced_linkage_specification_p = false;
23233 /* Inside the function, surrounding template-parameter-lists do not
23234 apply. */
23235 saved_num_template_parameter_lists
23236 = parser->num_template_parameter_lists;
23237 parser->num_template_parameter_lists = 0;
23239 start_lambda_scope (current_function_decl);
23241 /* If the next token is `try', `__transaction_atomic', or
23242 `__transaction_relaxed`, then we are looking at either function-try-block
23243 or function-transaction-block. Note that all of these include the
23244 function-body. */
23245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23246 ctor_initializer_p = cp_parser_function_transaction (parser,
23247 RID_TRANSACTION_ATOMIC);
23248 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23249 RID_TRANSACTION_RELAXED))
23250 ctor_initializer_p = cp_parser_function_transaction (parser,
23251 RID_TRANSACTION_RELAXED);
23252 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23253 ctor_initializer_p = cp_parser_function_try_block (parser);
23254 else
23255 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23256 (parser, /*in_function_try_block=*/false);
23258 finish_lambda_scope ();
23260 /* Finish the function. */
23261 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23262 (inline_p ? 2 : 0));
23263 /* Generate code for it, if necessary. */
23264 expand_or_defer_fn (fn);
23265 /* Restore the saved values. */
23266 parser->in_unbraced_linkage_specification_p
23267 = saved_in_unbraced_linkage_specification_p;
23268 parser->num_template_parameter_lists
23269 = saved_num_template_parameter_lists;
23270 parser->in_function_body = saved_in_function_body;
23272 parser->fully_implicit_function_template_p
23273 = fully_implicit_function_template_p;
23274 parser->implicit_template_parms
23275 = implicit_template_parms;
23276 parser->implicit_template_scope
23277 = implicit_template_scope;
23279 if (parser->fully_implicit_function_template_p)
23280 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23282 return fn;
23285 /* Parse a template-declaration, assuming that the `export' (and
23286 `extern') keywords, if present, has already been scanned. MEMBER_P
23287 is as for cp_parser_template_declaration. */
23289 static void
23290 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23292 tree decl = NULL_TREE;
23293 vec<deferred_access_check, va_gc> *checks;
23294 tree parameter_list;
23295 bool friend_p = false;
23296 bool need_lang_pop;
23297 cp_token *token;
23299 /* Look for the `template' keyword. */
23300 token = cp_lexer_peek_token (parser->lexer);
23301 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23302 return;
23304 /* And the `<'. */
23305 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23306 return;
23307 if (at_class_scope_p () && current_function_decl)
23309 /* 14.5.2.2 [temp.mem]
23311 A local class shall not have member templates. */
23312 error_at (token->location,
23313 "invalid declaration of member template in local class");
23314 cp_parser_skip_to_end_of_block_or_statement (parser);
23315 return;
23317 /* [temp]
23319 A template ... shall not have C linkage. */
23320 if (current_lang_name == lang_name_c)
23322 error_at (token->location, "template with C linkage");
23323 /* Give it C++ linkage to avoid confusing other parts of the
23324 front end. */
23325 push_lang_context (lang_name_cplusplus);
23326 need_lang_pop = true;
23328 else
23329 need_lang_pop = false;
23331 /* We cannot perform access checks on the template parameter
23332 declarations until we know what is being declared, just as we
23333 cannot check the decl-specifier list. */
23334 push_deferring_access_checks (dk_deferred);
23336 /* If the next token is `>', then we have an invalid
23337 specialization. Rather than complain about an invalid template
23338 parameter, issue an error message here. */
23339 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23341 cp_parser_error (parser, "invalid explicit specialization");
23342 begin_specialization ();
23343 parameter_list = NULL_TREE;
23345 else
23347 /* Parse the template parameters. */
23348 parameter_list = cp_parser_template_parameter_list (parser);
23351 /* Get the deferred access checks from the parameter list. These
23352 will be checked once we know what is being declared, as for a
23353 member template the checks must be performed in the scope of the
23354 class containing the member. */
23355 checks = get_deferred_access_checks ();
23357 /* Look for the `>'. */
23358 cp_parser_skip_to_end_of_template_parameter_list (parser);
23359 /* We just processed one more parameter list. */
23360 ++parser->num_template_parameter_lists;
23361 /* If the next token is `template', there are more template
23362 parameters. */
23363 if (cp_lexer_next_token_is_keyword (parser->lexer,
23364 RID_TEMPLATE))
23365 cp_parser_template_declaration_after_export (parser, member_p);
23366 else if (cxx_dialect >= cxx11
23367 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23368 decl = cp_parser_alias_declaration (parser);
23369 else
23371 /* There are no access checks when parsing a template, as we do not
23372 know if a specialization will be a friend. */
23373 push_deferring_access_checks (dk_no_check);
23374 token = cp_lexer_peek_token (parser->lexer);
23375 decl = cp_parser_single_declaration (parser,
23376 checks,
23377 member_p,
23378 /*explicit_specialization_p=*/false,
23379 &friend_p);
23380 pop_deferring_access_checks ();
23382 /* If this is a member template declaration, let the front
23383 end know. */
23384 if (member_p && !friend_p && decl)
23386 if (TREE_CODE (decl) == TYPE_DECL)
23387 cp_parser_check_access_in_redeclaration (decl, token->location);
23389 decl = finish_member_template_decl (decl);
23391 else if (friend_p && decl
23392 && DECL_DECLARES_TYPE_P (decl))
23393 make_friend_class (current_class_type, TREE_TYPE (decl),
23394 /*complain=*/true);
23396 /* We are done with the current parameter list. */
23397 --parser->num_template_parameter_lists;
23399 pop_deferring_access_checks ();
23401 /* Finish up. */
23402 finish_template_decl (parameter_list);
23404 /* Check the template arguments for a literal operator template. */
23405 if (decl
23406 && DECL_DECLARES_FUNCTION_P (decl)
23407 && UDLIT_OPER_P (DECL_NAME (decl)))
23409 bool ok = true;
23410 if (parameter_list == NULL_TREE)
23411 ok = false;
23412 else
23414 int num_parms = TREE_VEC_LENGTH (parameter_list);
23415 if (num_parms == 1)
23417 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23418 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23419 if (TREE_TYPE (parm) != char_type_node
23420 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23421 ok = false;
23423 else if (num_parms == 2 && cxx_dialect >= cxx14)
23425 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23426 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23427 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23428 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23429 if (TREE_TYPE (parm) != TREE_TYPE (type)
23430 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23431 ok = false;
23433 else
23434 ok = false;
23436 if (!ok)
23438 if (cxx_dialect >= cxx14)
23439 error ("literal operator template %qD has invalid parameter list."
23440 " Expected non-type template argument pack <char...>"
23441 " or <typename CharT, CharT...>",
23442 decl);
23443 else
23444 error ("literal operator template %qD has invalid parameter list."
23445 " Expected non-type template argument pack <char...>",
23446 decl);
23449 /* Register member declarations. */
23450 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23451 finish_member_declaration (decl);
23452 /* For the erroneous case of a template with C linkage, we pushed an
23453 implicit C++ linkage scope; exit that scope now. */
23454 if (need_lang_pop)
23455 pop_lang_context ();
23456 /* If DECL is a function template, we must return to parse it later.
23457 (Even though there is no definition, there might be default
23458 arguments that need handling.) */
23459 if (member_p && decl
23460 && DECL_DECLARES_FUNCTION_P (decl))
23461 vec_safe_push (unparsed_funs_with_definitions, decl);
23464 /* Perform the deferred access checks from a template-parameter-list.
23465 CHECKS is a TREE_LIST of access checks, as returned by
23466 get_deferred_access_checks. */
23468 static void
23469 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23471 ++processing_template_parmlist;
23472 perform_access_checks (checks, tf_warning_or_error);
23473 --processing_template_parmlist;
23476 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23477 `function-definition' sequence that follows a template header.
23478 If MEMBER_P is true, this declaration appears in a class scope.
23480 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23481 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23483 static tree
23484 cp_parser_single_declaration (cp_parser* parser,
23485 vec<deferred_access_check, va_gc> *checks,
23486 bool member_p,
23487 bool explicit_specialization_p,
23488 bool* friend_p)
23490 int declares_class_or_enum;
23491 tree decl = NULL_TREE;
23492 cp_decl_specifier_seq decl_specifiers;
23493 bool function_definition_p = false;
23494 cp_token *decl_spec_token_start;
23496 /* This function is only used when processing a template
23497 declaration. */
23498 gcc_assert (innermost_scope_kind () == sk_template_parms
23499 || innermost_scope_kind () == sk_template_spec);
23501 /* Defer access checks until we know what is being declared. */
23502 push_deferring_access_checks (dk_deferred);
23504 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23505 alternative. */
23506 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23507 cp_parser_decl_specifier_seq (parser,
23508 CP_PARSER_FLAGS_OPTIONAL,
23509 &decl_specifiers,
23510 &declares_class_or_enum);
23511 if (friend_p)
23512 *friend_p = cp_parser_friend_p (&decl_specifiers);
23514 /* There are no template typedefs. */
23515 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23517 error_at (decl_spec_token_start->location,
23518 "template declaration of %<typedef%>");
23519 decl = error_mark_node;
23522 /* Gather up the access checks that occurred the
23523 decl-specifier-seq. */
23524 stop_deferring_access_checks ();
23526 /* Check for the declaration of a template class. */
23527 if (declares_class_or_enum)
23529 if (cp_parser_declares_only_class_p (parser))
23531 decl = shadow_tag (&decl_specifiers);
23533 /* In this case:
23535 struct C {
23536 friend template <typename T> struct A<T>::B;
23539 A<T>::B will be represented by a TYPENAME_TYPE, and
23540 therefore not recognized by shadow_tag. */
23541 if (friend_p && *friend_p
23542 && !decl
23543 && decl_specifiers.type
23544 && TYPE_P (decl_specifiers.type))
23545 decl = decl_specifiers.type;
23547 if (decl && decl != error_mark_node)
23548 decl = TYPE_NAME (decl);
23549 else
23550 decl = error_mark_node;
23552 /* Perform access checks for template parameters. */
23553 cp_parser_perform_template_parameter_access_checks (checks);
23557 /* Complain about missing 'typename' or other invalid type names. */
23558 if (!decl_specifiers.any_type_specifiers_p
23559 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23561 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23562 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23563 the rest of this declaration. */
23564 decl = error_mark_node;
23565 goto out;
23568 /* If it's not a template class, try for a template function. If
23569 the next token is a `;', then this declaration does not declare
23570 anything. But, if there were errors in the decl-specifiers, then
23571 the error might well have come from an attempted class-specifier.
23572 In that case, there's no need to warn about a missing declarator. */
23573 if (!decl
23574 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23575 || decl_specifiers.type != error_mark_node))
23577 decl = cp_parser_init_declarator (parser,
23578 &decl_specifiers,
23579 checks,
23580 /*function_definition_allowed_p=*/true,
23581 member_p,
23582 declares_class_or_enum,
23583 &function_definition_p,
23584 NULL);
23586 /* 7.1.1-1 [dcl.stc]
23588 A storage-class-specifier shall not be specified in an explicit
23589 specialization... */
23590 if (decl
23591 && explicit_specialization_p
23592 && decl_specifiers.storage_class != sc_none)
23594 error_at (decl_spec_token_start->location,
23595 "explicit template specialization cannot have a storage class");
23596 decl = error_mark_node;
23599 if (decl && VAR_P (decl))
23600 check_template_variable (decl);
23603 /* Look for a trailing `;' after the declaration. */
23604 if (!function_definition_p
23605 && (decl == error_mark_node
23606 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23607 cp_parser_skip_to_end_of_block_or_statement (parser);
23609 out:
23610 pop_deferring_access_checks ();
23612 /* Clear any current qualification; whatever comes next is the start
23613 of something new. */
23614 parser->scope = NULL_TREE;
23615 parser->qualifying_scope = NULL_TREE;
23616 parser->object_scope = NULL_TREE;
23618 return decl;
23621 /* Parse a cast-expression that is not the operand of a unary "&". */
23623 static tree
23624 cp_parser_simple_cast_expression (cp_parser *parser)
23626 return cp_parser_cast_expression (parser, /*address_p=*/false,
23627 /*cast_p=*/false, /*decltype*/false, NULL);
23630 /* Parse a functional cast to TYPE. Returns an expression
23631 representing the cast. */
23633 static tree
23634 cp_parser_functional_cast (cp_parser* parser, tree type)
23636 vec<tree, va_gc> *vec;
23637 tree expression_list;
23638 tree cast;
23639 bool nonconst_p;
23641 if (!type)
23642 type = error_mark_node;
23644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23646 cp_lexer_set_source_position (parser->lexer);
23647 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23648 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23649 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23650 if (TREE_CODE (type) == TYPE_DECL)
23651 type = TREE_TYPE (type);
23652 return finish_compound_literal (type, expression_list,
23653 tf_warning_or_error);
23657 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23658 /*cast_p=*/true,
23659 /*allow_expansion_p=*/true,
23660 /*non_constant_p=*/NULL);
23661 if (vec == NULL)
23662 expression_list = error_mark_node;
23663 else
23665 expression_list = build_tree_list_vec (vec);
23666 release_tree_vector (vec);
23669 cast = build_functional_cast (type, expression_list,
23670 tf_warning_or_error);
23671 /* [expr.const]/1: In an integral constant expression "only type
23672 conversions to integral or enumeration type can be used". */
23673 if (TREE_CODE (type) == TYPE_DECL)
23674 type = TREE_TYPE (type);
23675 if (cast != error_mark_node
23676 && !cast_valid_in_integral_constant_expression_p (type)
23677 && cp_parser_non_integral_constant_expression (parser,
23678 NIC_CONSTRUCTOR))
23679 return error_mark_node;
23680 return cast;
23683 /* Save the tokens that make up the body of a member function defined
23684 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23685 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23686 specifiers applied to the declaration. Returns the FUNCTION_DECL
23687 for the member function. */
23689 static tree
23690 cp_parser_save_member_function_body (cp_parser* parser,
23691 cp_decl_specifier_seq *decl_specifiers,
23692 cp_declarator *declarator,
23693 tree attributes)
23695 cp_token *first;
23696 cp_token *last;
23697 tree fn;
23699 /* Create the FUNCTION_DECL. */
23700 fn = grokmethod (decl_specifiers, declarator, attributes);
23701 cp_finalize_omp_declare_simd (parser, fn);
23702 /* If something went badly wrong, bail out now. */
23703 if (fn == error_mark_node)
23705 /* If there's a function-body, skip it. */
23706 if (cp_parser_token_starts_function_definition_p
23707 (cp_lexer_peek_token (parser->lexer)))
23708 cp_parser_skip_to_end_of_block_or_statement (parser);
23709 return error_mark_node;
23712 /* Remember it, if there default args to post process. */
23713 cp_parser_save_default_args (parser, fn);
23715 /* Save away the tokens that make up the body of the
23716 function. */
23717 first = parser->lexer->next_token;
23718 /* Handle function try blocks. */
23719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23720 cp_lexer_consume_token (parser->lexer);
23721 /* We can have braced-init-list mem-initializers before the fn body. */
23722 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23724 cp_lexer_consume_token (parser->lexer);
23725 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23727 /* cache_group will stop after an un-nested { } pair, too. */
23728 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23729 break;
23731 /* variadic mem-inits have ... after the ')'. */
23732 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23733 cp_lexer_consume_token (parser->lexer);
23736 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23737 /* Handle function try blocks. */
23738 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23739 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23740 last = parser->lexer->next_token;
23742 /* Save away the inline definition; we will process it when the
23743 class is complete. */
23744 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23745 DECL_PENDING_INLINE_P (fn) = 1;
23747 /* We need to know that this was defined in the class, so that
23748 friend templates are handled correctly. */
23749 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23751 /* Add FN to the queue of functions to be parsed later. */
23752 vec_safe_push (unparsed_funs_with_definitions, fn);
23754 return fn;
23757 /* Save the tokens that make up the in-class initializer for a non-static
23758 data member. Returns a DEFAULT_ARG. */
23760 static tree
23761 cp_parser_save_nsdmi (cp_parser* parser)
23763 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23766 /* Parse a template-argument-list, as well as the trailing ">" (but
23767 not the opening "<"). See cp_parser_template_argument_list for the
23768 return value. */
23770 static tree
23771 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23773 tree arguments;
23774 tree saved_scope;
23775 tree saved_qualifying_scope;
23776 tree saved_object_scope;
23777 bool saved_greater_than_is_operator_p;
23778 int saved_unevaluated_operand;
23779 int saved_inhibit_evaluation_warnings;
23781 /* [temp.names]
23783 When parsing a template-id, the first non-nested `>' is taken as
23784 the end of the template-argument-list rather than a greater-than
23785 operator. */
23786 saved_greater_than_is_operator_p
23787 = parser->greater_than_is_operator_p;
23788 parser->greater_than_is_operator_p = false;
23789 /* Parsing the argument list may modify SCOPE, so we save it
23790 here. */
23791 saved_scope = parser->scope;
23792 saved_qualifying_scope = parser->qualifying_scope;
23793 saved_object_scope = parser->object_scope;
23794 /* We need to evaluate the template arguments, even though this
23795 template-id may be nested within a "sizeof". */
23796 saved_unevaluated_operand = cp_unevaluated_operand;
23797 cp_unevaluated_operand = 0;
23798 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23799 c_inhibit_evaluation_warnings = 0;
23800 /* Parse the template-argument-list itself. */
23801 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23802 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23803 arguments = NULL_TREE;
23804 else
23805 arguments = cp_parser_template_argument_list (parser);
23806 /* Look for the `>' that ends the template-argument-list. If we find
23807 a '>>' instead, it's probably just a typo. */
23808 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23810 if (cxx_dialect != cxx98)
23812 /* In C++0x, a `>>' in a template argument list or cast
23813 expression is considered to be two separate `>'
23814 tokens. So, change the current token to a `>', but don't
23815 consume it: it will be consumed later when the outer
23816 template argument list (or cast expression) is parsed.
23817 Note that this replacement of `>' for `>>' is necessary
23818 even if we are parsing tentatively: in the tentative
23819 case, after calling
23820 cp_parser_enclosed_template_argument_list we will always
23821 throw away all of the template arguments and the first
23822 closing `>', either because the template argument list
23823 was erroneous or because we are replacing those tokens
23824 with a CPP_TEMPLATE_ID token. The second `>' (which will
23825 not have been thrown away) is needed either to close an
23826 outer template argument list or to complete a new-style
23827 cast. */
23828 cp_token *token = cp_lexer_peek_token (parser->lexer);
23829 token->type = CPP_GREATER;
23831 else if (!saved_greater_than_is_operator_p)
23833 /* If we're in a nested template argument list, the '>>' has
23834 to be a typo for '> >'. We emit the error message, but we
23835 continue parsing and we push a '>' as next token, so that
23836 the argument list will be parsed correctly. Note that the
23837 global source location is still on the token before the
23838 '>>', so we need to say explicitly where we want it. */
23839 cp_token *token = cp_lexer_peek_token (parser->lexer);
23840 error_at (token->location, "%<>>%> should be %<> >%> "
23841 "within a nested template argument list");
23843 token->type = CPP_GREATER;
23845 else
23847 /* If this is not a nested template argument list, the '>>'
23848 is a typo for '>'. Emit an error message and continue.
23849 Same deal about the token location, but here we can get it
23850 right by consuming the '>>' before issuing the diagnostic. */
23851 cp_token *token = cp_lexer_consume_token (parser->lexer);
23852 error_at (token->location,
23853 "spurious %<>>%>, use %<>%> to terminate "
23854 "a template argument list");
23857 else
23858 cp_parser_skip_to_end_of_template_parameter_list (parser);
23859 /* The `>' token might be a greater-than operator again now. */
23860 parser->greater_than_is_operator_p
23861 = saved_greater_than_is_operator_p;
23862 /* Restore the SAVED_SCOPE. */
23863 parser->scope = saved_scope;
23864 parser->qualifying_scope = saved_qualifying_scope;
23865 parser->object_scope = saved_object_scope;
23866 cp_unevaluated_operand = saved_unevaluated_operand;
23867 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23869 return arguments;
23872 /* MEMBER_FUNCTION is a member function, or a friend. If default
23873 arguments, or the body of the function have not yet been parsed,
23874 parse them now. */
23876 static void
23877 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23879 timevar_push (TV_PARSE_INMETH);
23880 /* If this member is a template, get the underlying
23881 FUNCTION_DECL. */
23882 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23883 member_function = DECL_TEMPLATE_RESULT (member_function);
23885 /* There should not be any class definitions in progress at this
23886 point; the bodies of members are only parsed outside of all class
23887 definitions. */
23888 gcc_assert (parser->num_classes_being_defined == 0);
23889 /* While we're parsing the member functions we might encounter more
23890 classes. We want to handle them right away, but we don't want
23891 them getting mixed up with functions that are currently in the
23892 queue. */
23893 push_unparsed_function_queues (parser);
23895 /* Make sure that any template parameters are in scope. */
23896 maybe_begin_member_template_processing (member_function);
23898 /* If the body of the function has not yet been parsed, parse it
23899 now. */
23900 if (DECL_PENDING_INLINE_P (member_function))
23902 tree function_scope;
23903 cp_token_cache *tokens;
23905 /* The function is no longer pending; we are processing it. */
23906 tokens = DECL_PENDING_INLINE_INFO (member_function);
23907 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23908 DECL_PENDING_INLINE_P (member_function) = 0;
23910 /* If this is a local class, enter the scope of the containing
23911 function. */
23912 function_scope = current_function_decl;
23913 if (function_scope)
23914 push_function_context ();
23916 /* Push the body of the function onto the lexer stack. */
23917 cp_parser_push_lexer_for_tokens (parser, tokens);
23919 /* Let the front end know that we going to be defining this
23920 function. */
23921 start_preparsed_function (member_function, NULL_TREE,
23922 SF_PRE_PARSED | SF_INCLASS_INLINE);
23924 /* Don't do access checking if it is a templated function. */
23925 if (processing_template_decl)
23926 push_deferring_access_checks (dk_no_check);
23928 /* #pragma omp declare reduction needs special parsing. */
23929 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23931 parser->lexer->in_pragma = true;
23932 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23933 finish_function (/*inline*/2);
23934 cp_check_omp_declare_reduction (member_function);
23936 else
23937 /* Now, parse the body of the function. */
23938 cp_parser_function_definition_after_declarator (parser,
23939 /*inline_p=*/true);
23941 if (processing_template_decl)
23942 pop_deferring_access_checks ();
23944 /* Leave the scope of the containing function. */
23945 if (function_scope)
23946 pop_function_context ();
23947 cp_parser_pop_lexer (parser);
23950 /* Remove any template parameters from the symbol table. */
23951 maybe_end_member_template_processing ();
23953 /* Restore the queue. */
23954 pop_unparsed_function_queues (parser);
23955 timevar_pop (TV_PARSE_INMETH);
23958 /* If DECL contains any default args, remember it on the unparsed
23959 functions queue. */
23961 static void
23962 cp_parser_save_default_args (cp_parser* parser, tree decl)
23964 tree probe;
23966 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23967 probe;
23968 probe = TREE_CHAIN (probe))
23969 if (TREE_PURPOSE (probe))
23971 cp_default_arg_entry entry = {current_class_type, decl};
23972 vec_safe_push (unparsed_funs_with_default_args, entry);
23973 break;
23977 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23978 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23979 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23980 from the parameter-type-list. */
23982 static tree
23983 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23984 tree default_arg, tree parmtype)
23986 cp_token_cache *tokens;
23987 tree parsed_arg;
23988 bool dummy;
23990 if (default_arg == error_mark_node)
23991 return error_mark_node;
23993 /* Push the saved tokens for the default argument onto the parser's
23994 lexer stack. */
23995 tokens = DEFARG_TOKENS (default_arg);
23996 cp_parser_push_lexer_for_tokens (parser, tokens);
23998 start_lambda_scope (decl);
24000 /* Parse the default argument. */
24001 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24002 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24003 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24005 finish_lambda_scope ();
24007 if (parsed_arg == error_mark_node)
24008 cp_parser_skip_to_end_of_statement (parser);
24010 if (!processing_template_decl)
24012 /* In a non-template class, check conversions now. In a template,
24013 we'll wait and instantiate these as needed. */
24014 if (TREE_CODE (decl) == PARM_DECL)
24015 parsed_arg = check_default_argument (parmtype, parsed_arg,
24016 tf_warning_or_error);
24017 else
24018 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24021 /* If the token stream has not been completely used up, then
24022 there was extra junk after the end of the default
24023 argument. */
24024 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24026 if (TREE_CODE (decl) == PARM_DECL)
24027 cp_parser_error (parser, "expected %<,%>");
24028 else
24029 cp_parser_error (parser, "expected %<;%>");
24032 /* Revert to the main lexer. */
24033 cp_parser_pop_lexer (parser);
24035 return parsed_arg;
24038 /* FIELD is a non-static data member with an initializer which we saved for
24039 later; parse it now. */
24041 static void
24042 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24044 tree def;
24046 maybe_begin_member_template_processing (field);
24048 push_unparsed_function_queues (parser);
24049 def = cp_parser_late_parse_one_default_arg (parser, field,
24050 DECL_INITIAL (field),
24051 NULL_TREE);
24052 pop_unparsed_function_queues (parser);
24054 maybe_end_member_template_processing ();
24056 DECL_INITIAL (field) = def;
24059 /* FN is a FUNCTION_DECL which may contains a parameter with an
24060 unparsed DEFAULT_ARG. Parse the default args now. This function
24061 assumes that the current scope is the scope in which the default
24062 argument should be processed. */
24064 static void
24065 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24067 bool saved_local_variables_forbidden_p;
24068 tree parm, parmdecl;
24070 /* While we're parsing the default args, we might (due to the
24071 statement expression extension) encounter more classes. We want
24072 to handle them right away, but we don't want them getting mixed
24073 up with default args that are currently in the queue. */
24074 push_unparsed_function_queues (parser);
24076 /* Local variable names (and the `this' keyword) may not appear
24077 in a default argument. */
24078 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24079 parser->local_variables_forbidden_p = true;
24081 push_defarg_context (fn);
24083 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24084 parmdecl = DECL_ARGUMENTS (fn);
24085 parm && parm != void_list_node;
24086 parm = TREE_CHAIN (parm),
24087 parmdecl = DECL_CHAIN (parmdecl))
24089 tree default_arg = TREE_PURPOSE (parm);
24090 tree parsed_arg;
24091 vec<tree, va_gc> *insts;
24092 tree copy;
24093 unsigned ix;
24095 if (!default_arg)
24096 continue;
24098 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24099 /* This can happen for a friend declaration for a function
24100 already declared with default arguments. */
24101 continue;
24103 parsed_arg
24104 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24105 default_arg,
24106 TREE_VALUE (parm));
24107 if (parsed_arg == error_mark_node)
24109 continue;
24112 TREE_PURPOSE (parm) = parsed_arg;
24114 /* Update any instantiations we've already created. */
24115 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24116 vec_safe_iterate (insts, ix, &copy); ix++)
24117 TREE_PURPOSE (copy) = parsed_arg;
24120 pop_defarg_context ();
24122 /* Make sure no default arg is missing. */
24123 check_default_args (fn);
24125 /* Restore the state of local_variables_forbidden_p. */
24126 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24128 /* Restore the queue. */
24129 pop_unparsed_function_queues (parser);
24132 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24134 sizeof ... ( identifier )
24136 where the 'sizeof' token has already been consumed. */
24138 static tree
24139 cp_parser_sizeof_pack (cp_parser *parser)
24141 /* Consume the `...'. */
24142 cp_lexer_consume_token (parser->lexer);
24143 maybe_warn_variadic_templates ();
24145 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24146 if (paren)
24147 cp_lexer_consume_token (parser->lexer);
24148 else
24149 permerror (cp_lexer_peek_token (parser->lexer)->location,
24150 "%<sizeof...%> argument must be surrounded by parentheses");
24152 cp_token *token = cp_lexer_peek_token (parser->lexer);
24153 tree name = cp_parser_identifier (parser);
24154 if (name == error_mark_node)
24155 return error_mark_node;
24156 /* The name is not qualified. */
24157 parser->scope = NULL_TREE;
24158 parser->qualifying_scope = NULL_TREE;
24159 parser->object_scope = NULL_TREE;
24160 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24161 if (expr == error_mark_node)
24162 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24163 token->location);
24164 if (TREE_CODE (expr) == TYPE_DECL)
24165 expr = TREE_TYPE (expr);
24166 else if (TREE_CODE (expr) == CONST_DECL)
24167 expr = DECL_INITIAL (expr);
24168 expr = make_pack_expansion (expr);
24170 if (paren)
24171 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24173 return expr;
24176 /* Parse the operand of `sizeof' (or a similar operator). Returns
24177 either a TYPE or an expression, depending on the form of the
24178 input. The KEYWORD indicates which kind of expression we have
24179 encountered. */
24181 static tree
24182 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24184 tree expr = NULL_TREE;
24185 const char *saved_message;
24186 char *tmp;
24187 bool saved_integral_constant_expression_p;
24188 bool saved_non_integral_constant_expression_p;
24190 /* If it's a `...', then we are computing the length of a parameter
24191 pack. */
24192 if (keyword == RID_SIZEOF
24193 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24194 return cp_parser_sizeof_pack (parser);
24196 /* Types cannot be defined in a `sizeof' expression. Save away the
24197 old message. */
24198 saved_message = parser->type_definition_forbidden_message;
24199 /* And create the new one. */
24200 tmp = concat ("types may not be defined in %<",
24201 IDENTIFIER_POINTER (ridpointers[keyword]),
24202 "%> expressions", NULL);
24203 parser->type_definition_forbidden_message = tmp;
24205 /* The restrictions on constant-expressions do not apply inside
24206 sizeof expressions. */
24207 saved_integral_constant_expression_p
24208 = parser->integral_constant_expression_p;
24209 saved_non_integral_constant_expression_p
24210 = parser->non_integral_constant_expression_p;
24211 parser->integral_constant_expression_p = false;
24213 /* Do not actually evaluate the expression. */
24214 ++cp_unevaluated_operand;
24215 ++c_inhibit_evaluation_warnings;
24216 /* If it's a `(', then we might be looking at the type-id
24217 construction. */
24218 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24220 tree type = NULL_TREE;
24222 /* We can't be sure yet whether we're looking at a type-id or an
24223 expression. */
24224 cp_parser_parse_tentatively (parser);
24225 /* Note: as a GNU Extension, compound literals are considered
24226 postfix-expressions as they are in C99, so they are valid
24227 arguments to sizeof. See comment in cp_parser_cast_expression
24228 for details. */
24229 if (cp_parser_compound_literal_p (parser))
24230 cp_parser_simulate_error (parser);
24231 else
24233 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24234 parser->in_type_id_in_expr_p = true;
24235 /* Look for the type-id. */
24236 type = cp_parser_type_id (parser);
24237 /* Look for the closing `)'. */
24238 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24239 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24242 /* If all went well, then we're done. */
24243 if (cp_parser_parse_definitely (parser))
24245 cp_decl_specifier_seq decl_specs;
24247 /* Build a trivial decl-specifier-seq. */
24248 clear_decl_specs (&decl_specs);
24249 decl_specs.type = type;
24251 /* Call grokdeclarator to figure out what type this is. */
24252 expr = grokdeclarator (NULL,
24253 &decl_specs,
24254 TYPENAME,
24255 /*initialized=*/0,
24256 /*attrlist=*/NULL);
24260 /* If the type-id production did not work out, then we must be
24261 looking at the unary-expression production. */
24262 if (!expr)
24263 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24264 /*cast_p=*/false, NULL);
24266 /* Go back to evaluating expressions. */
24267 --cp_unevaluated_operand;
24268 --c_inhibit_evaluation_warnings;
24270 /* Free the message we created. */
24271 free (tmp);
24272 /* And restore the old one. */
24273 parser->type_definition_forbidden_message = saved_message;
24274 parser->integral_constant_expression_p
24275 = saved_integral_constant_expression_p;
24276 parser->non_integral_constant_expression_p
24277 = saved_non_integral_constant_expression_p;
24279 return expr;
24282 /* If the current declaration has no declarator, return true. */
24284 static bool
24285 cp_parser_declares_only_class_p (cp_parser *parser)
24287 /* If the next token is a `;' or a `,' then there is no
24288 declarator. */
24289 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24290 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24293 /* Update the DECL_SPECS to reflect the storage class indicated by
24294 KEYWORD. */
24296 static void
24297 cp_parser_set_storage_class (cp_parser *parser,
24298 cp_decl_specifier_seq *decl_specs,
24299 enum rid keyword,
24300 cp_token *token)
24302 cp_storage_class storage_class;
24304 if (parser->in_unbraced_linkage_specification_p)
24306 error_at (token->location, "invalid use of %qD in linkage specification",
24307 ridpointers[keyword]);
24308 return;
24310 else if (decl_specs->storage_class != sc_none)
24312 decl_specs->conflicting_specifiers_p = true;
24313 return;
24316 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24317 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24318 && decl_specs->gnu_thread_keyword_p)
24320 pedwarn (decl_specs->locations[ds_thread], 0,
24321 "%<__thread%> before %qD", ridpointers[keyword]);
24324 switch (keyword)
24326 case RID_AUTO:
24327 storage_class = sc_auto;
24328 break;
24329 case RID_REGISTER:
24330 storage_class = sc_register;
24331 break;
24332 case RID_STATIC:
24333 storage_class = sc_static;
24334 break;
24335 case RID_EXTERN:
24336 storage_class = sc_extern;
24337 break;
24338 case RID_MUTABLE:
24339 storage_class = sc_mutable;
24340 break;
24341 default:
24342 gcc_unreachable ();
24344 decl_specs->storage_class = storage_class;
24345 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24347 /* A storage class specifier cannot be applied alongside a typedef
24348 specifier. If there is a typedef specifier present then set
24349 conflicting_specifiers_p which will trigger an error later
24350 on in grokdeclarator. */
24351 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24352 decl_specs->conflicting_specifiers_p = true;
24355 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24356 is true, the type is a class or enum definition. */
24358 static void
24359 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24360 tree type_spec,
24361 cp_token *token,
24362 bool type_definition_p)
24364 decl_specs->any_specifiers_p = true;
24366 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24367 (with, for example, in "typedef int wchar_t;") we remember that
24368 this is what happened. In system headers, we ignore these
24369 declarations so that G++ can work with system headers that are not
24370 C++-safe. */
24371 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24372 && !type_definition_p
24373 && (type_spec == boolean_type_node
24374 || type_spec == char16_type_node
24375 || type_spec == char32_type_node
24376 || type_spec == wchar_type_node)
24377 && (decl_specs->type
24378 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24379 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24380 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24381 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24383 decl_specs->redefined_builtin_type = type_spec;
24384 set_and_check_decl_spec_loc (decl_specs,
24385 ds_redefined_builtin_type_spec,
24386 token);
24387 if (!decl_specs->type)
24389 decl_specs->type = type_spec;
24390 decl_specs->type_definition_p = false;
24391 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24394 else if (decl_specs->type)
24395 decl_specs->multiple_types_p = true;
24396 else
24398 decl_specs->type = type_spec;
24399 decl_specs->type_definition_p = type_definition_p;
24400 decl_specs->redefined_builtin_type = NULL_TREE;
24401 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24405 /* True iff TOKEN is the GNU keyword __thread. */
24407 static bool
24408 token_is__thread (cp_token *token)
24410 gcc_assert (token->keyword == RID_THREAD);
24411 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24414 /* Set the location for a declarator specifier and check if it is
24415 duplicated.
24417 DECL_SPECS is the sequence of declarator specifiers onto which to
24418 set the location.
24420 DS is the single declarator specifier to set which location is to
24421 be set onto the existing sequence of declarators.
24423 LOCATION is the location for the declarator specifier to
24424 consider. */
24426 static void
24427 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24428 cp_decl_spec ds, cp_token *token)
24430 gcc_assert (ds < ds_last);
24432 if (decl_specs == NULL)
24433 return;
24435 source_location location = token->location;
24437 if (decl_specs->locations[ds] == 0)
24439 decl_specs->locations[ds] = location;
24440 if (ds == ds_thread)
24441 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24443 else
24445 if (ds == ds_long)
24447 if (decl_specs->locations[ds_long_long] != 0)
24448 error_at (location,
24449 "%<long long long%> is too long for GCC");
24450 else
24452 decl_specs->locations[ds_long_long] = location;
24453 pedwarn_cxx98 (location,
24454 OPT_Wlong_long,
24455 "ISO C++ 1998 does not support %<long long%>");
24458 else if (ds == ds_thread)
24460 bool gnu = token_is__thread (token);
24461 if (gnu != decl_specs->gnu_thread_keyword_p)
24462 error_at (location,
24463 "both %<__thread%> and %<thread_local%> specified");
24464 else
24465 error_at (location, "duplicate %qD", token->u.value);
24467 else
24469 static const char *const decl_spec_names[] = {
24470 "signed",
24471 "unsigned",
24472 "short",
24473 "long",
24474 "const",
24475 "volatile",
24476 "restrict",
24477 "inline",
24478 "virtual",
24479 "explicit",
24480 "friend",
24481 "typedef",
24482 "using",
24483 "constexpr",
24484 "__complex"
24486 error_at (location,
24487 "duplicate %qs", decl_spec_names[ds]);
24492 /* Return true iff the declarator specifier DS is present in the
24493 sequence of declarator specifiers DECL_SPECS. */
24495 bool
24496 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24497 cp_decl_spec ds)
24499 gcc_assert (ds < ds_last);
24501 if (decl_specs == NULL)
24502 return false;
24504 return decl_specs->locations[ds] != 0;
24507 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24508 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24510 static bool
24511 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24513 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24516 /* Issue an error message indicating that TOKEN_DESC was expected.
24517 If KEYWORD is true, it indicated this function is called by
24518 cp_parser_require_keword and the required token can only be
24519 a indicated keyword. */
24521 static void
24522 cp_parser_required_error (cp_parser *parser,
24523 required_token token_desc,
24524 bool keyword)
24526 switch (token_desc)
24528 case RT_NEW:
24529 cp_parser_error (parser, "expected %<new%>");
24530 return;
24531 case RT_DELETE:
24532 cp_parser_error (parser, "expected %<delete%>");
24533 return;
24534 case RT_RETURN:
24535 cp_parser_error (parser, "expected %<return%>");
24536 return;
24537 case RT_WHILE:
24538 cp_parser_error (parser, "expected %<while%>");
24539 return;
24540 case RT_EXTERN:
24541 cp_parser_error (parser, "expected %<extern%>");
24542 return;
24543 case RT_STATIC_ASSERT:
24544 cp_parser_error (parser, "expected %<static_assert%>");
24545 return;
24546 case RT_DECLTYPE:
24547 cp_parser_error (parser, "expected %<decltype%>");
24548 return;
24549 case RT_OPERATOR:
24550 cp_parser_error (parser, "expected %<operator%>");
24551 return;
24552 case RT_CLASS:
24553 cp_parser_error (parser, "expected %<class%>");
24554 return;
24555 case RT_TEMPLATE:
24556 cp_parser_error (parser, "expected %<template%>");
24557 return;
24558 case RT_NAMESPACE:
24559 cp_parser_error (parser, "expected %<namespace%>");
24560 return;
24561 case RT_USING:
24562 cp_parser_error (parser, "expected %<using%>");
24563 return;
24564 case RT_ASM:
24565 cp_parser_error (parser, "expected %<asm%>");
24566 return;
24567 case RT_TRY:
24568 cp_parser_error (parser, "expected %<try%>");
24569 return;
24570 case RT_CATCH:
24571 cp_parser_error (parser, "expected %<catch%>");
24572 return;
24573 case RT_THROW:
24574 cp_parser_error (parser, "expected %<throw%>");
24575 return;
24576 case RT_LABEL:
24577 cp_parser_error (parser, "expected %<__label__%>");
24578 return;
24579 case RT_AT_TRY:
24580 cp_parser_error (parser, "expected %<@try%>");
24581 return;
24582 case RT_AT_SYNCHRONIZED:
24583 cp_parser_error (parser, "expected %<@synchronized%>");
24584 return;
24585 case RT_AT_THROW:
24586 cp_parser_error (parser, "expected %<@throw%>");
24587 return;
24588 case RT_TRANSACTION_ATOMIC:
24589 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24590 return;
24591 case RT_TRANSACTION_RELAXED:
24592 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24593 return;
24594 default:
24595 break;
24597 if (!keyword)
24599 switch (token_desc)
24601 case RT_SEMICOLON:
24602 cp_parser_error (parser, "expected %<;%>");
24603 return;
24604 case RT_OPEN_PAREN:
24605 cp_parser_error (parser, "expected %<(%>");
24606 return;
24607 case RT_CLOSE_BRACE:
24608 cp_parser_error (parser, "expected %<}%>");
24609 return;
24610 case RT_OPEN_BRACE:
24611 cp_parser_error (parser, "expected %<{%>");
24612 return;
24613 case RT_CLOSE_SQUARE:
24614 cp_parser_error (parser, "expected %<]%>");
24615 return;
24616 case RT_OPEN_SQUARE:
24617 cp_parser_error (parser, "expected %<[%>");
24618 return;
24619 case RT_COMMA:
24620 cp_parser_error (parser, "expected %<,%>");
24621 return;
24622 case RT_SCOPE:
24623 cp_parser_error (parser, "expected %<::%>");
24624 return;
24625 case RT_LESS:
24626 cp_parser_error (parser, "expected %<<%>");
24627 return;
24628 case RT_GREATER:
24629 cp_parser_error (parser, "expected %<>%>");
24630 return;
24631 case RT_EQ:
24632 cp_parser_error (parser, "expected %<=%>");
24633 return;
24634 case RT_ELLIPSIS:
24635 cp_parser_error (parser, "expected %<...%>");
24636 return;
24637 case RT_MULT:
24638 cp_parser_error (parser, "expected %<*%>");
24639 return;
24640 case RT_COMPL:
24641 cp_parser_error (parser, "expected %<~%>");
24642 return;
24643 case RT_COLON:
24644 cp_parser_error (parser, "expected %<:%>");
24645 return;
24646 case RT_COLON_SCOPE:
24647 cp_parser_error (parser, "expected %<:%> or %<::%>");
24648 return;
24649 case RT_CLOSE_PAREN:
24650 cp_parser_error (parser, "expected %<)%>");
24651 return;
24652 case RT_COMMA_CLOSE_PAREN:
24653 cp_parser_error (parser, "expected %<,%> or %<)%>");
24654 return;
24655 case RT_PRAGMA_EOL:
24656 cp_parser_error (parser, "expected end of line");
24657 return;
24658 case RT_NAME:
24659 cp_parser_error (parser, "expected identifier");
24660 return;
24661 case RT_SELECT:
24662 cp_parser_error (parser, "expected selection-statement");
24663 return;
24664 case RT_INTERATION:
24665 cp_parser_error (parser, "expected iteration-statement");
24666 return;
24667 case RT_JUMP:
24668 cp_parser_error (parser, "expected jump-statement");
24669 return;
24670 case RT_CLASS_KEY:
24671 cp_parser_error (parser, "expected class-key");
24672 return;
24673 case RT_CLASS_TYPENAME_TEMPLATE:
24674 cp_parser_error (parser,
24675 "expected %<class%>, %<typename%>, or %<template%>");
24676 return;
24677 default:
24678 gcc_unreachable ();
24681 else
24682 gcc_unreachable ();
24687 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24688 issue an error message indicating that TOKEN_DESC was expected.
24690 Returns the token consumed, if the token had the appropriate type.
24691 Otherwise, returns NULL. */
24693 static cp_token *
24694 cp_parser_require (cp_parser* parser,
24695 enum cpp_ttype type,
24696 required_token token_desc)
24698 if (cp_lexer_next_token_is (parser->lexer, type))
24699 return cp_lexer_consume_token (parser->lexer);
24700 else
24702 /* Output the MESSAGE -- unless we're parsing tentatively. */
24703 if (!cp_parser_simulate_error (parser))
24704 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24705 return NULL;
24709 /* An error message is produced if the next token is not '>'.
24710 All further tokens are skipped until the desired token is
24711 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24713 static void
24714 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24716 /* Current level of '< ... >'. */
24717 unsigned level = 0;
24718 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24719 unsigned nesting_depth = 0;
24721 /* Are we ready, yet? If not, issue error message. */
24722 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24723 return;
24725 /* Skip tokens until the desired token is found. */
24726 while (true)
24728 /* Peek at the next token. */
24729 switch (cp_lexer_peek_token (parser->lexer)->type)
24731 case CPP_LESS:
24732 if (!nesting_depth)
24733 ++level;
24734 break;
24736 case CPP_RSHIFT:
24737 if (cxx_dialect == cxx98)
24738 /* C++0x views the `>>' operator as two `>' tokens, but
24739 C++98 does not. */
24740 break;
24741 else if (!nesting_depth && level-- == 0)
24743 /* We've hit a `>>' where the first `>' closes the
24744 template argument list, and the second `>' is
24745 spurious. Just consume the `>>' and stop; we've
24746 already produced at least one error. */
24747 cp_lexer_consume_token (parser->lexer);
24748 return;
24750 /* Fall through for C++0x, so we handle the second `>' in
24751 the `>>'. */
24753 case CPP_GREATER:
24754 if (!nesting_depth && level-- == 0)
24756 /* We've reached the token we want, consume it and stop. */
24757 cp_lexer_consume_token (parser->lexer);
24758 return;
24760 break;
24762 case CPP_OPEN_PAREN:
24763 case CPP_OPEN_SQUARE:
24764 ++nesting_depth;
24765 break;
24767 case CPP_CLOSE_PAREN:
24768 case CPP_CLOSE_SQUARE:
24769 if (nesting_depth-- == 0)
24770 return;
24771 break;
24773 case CPP_EOF:
24774 case CPP_PRAGMA_EOL:
24775 case CPP_SEMICOLON:
24776 case CPP_OPEN_BRACE:
24777 case CPP_CLOSE_BRACE:
24778 /* The '>' was probably forgotten, don't look further. */
24779 return;
24781 default:
24782 break;
24785 /* Consume this token. */
24786 cp_lexer_consume_token (parser->lexer);
24790 /* If the next token is the indicated keyword, consume it. Otherwise,
24791 issue an error message indicating that TOKEN_DESC was expected.
24793 Returns the token consumed, if the token had the appropriate type.
24794 Otherwise, returns NULL. */
24796 static cp_token *
24797 cp_parser_require_keyword (cp_parser* parser,
24798 enum rid keyword,
24799 required_token token_desc)
24801 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24803 if (token && token->keyword != keyword)
24805 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24806 return NULL;
24809 return token;
24812 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24813 function-definition. */
24815 static bool
24816 cp_parser_token_starts_function_definition_p (cp_token* token)
24818 return (/* An ordinary function-body begins with an `{'. */
24819 token->type == CPP_OPEN_BRACE
24820 /* A ctor-initializer begins with a `:'. */
24821 || token->type == CPP_COLON
24822 /* A function-try-block begins with `try'. */
24823 || token->keyword == RID_TRY
24824 /* A function-transaction-block begins with `__transaction_atomic'
24825 or `__transaction_relaxed'. */
24826 || token->keyword == RID_TRANSACTION_ATOMIC
24827 || token->keyword == RID_TRANSACTION_RELAXED
24828 /* The named return value extension begins with `return'. */
24829 || token->keyword == RID_RETURN);
24832 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24833 definition. */
24835 static bool
24836 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24838 cp_token *token;
24840 token = cp_lexer_peek_token (parser->lexer);
24841 return (token->type == CPP_OPEN_BRACE
24842 || (token->type == CPP_COLON
24843 && !parser->colon_doesnt_start_class_def_p));
24846 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24847 C++0x) ending a template-argument. */
24849 static bool
24850 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24852 cp_token *token;
24854 token = cp_lexer_peek_token (parser->lexer);
24855 return (token->type == CPP_COMMA
24856 || token->type == CPP_GREATER
24857 || token->type == CPP_ELLIPSIS
24858 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24861 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24862 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24864 static bool
24865 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24866 size_t n)
24868 cp_token *token;
24870 token = cp_lexer_peek_nth_token (parser->lexer, n);
24871 if (token->type == CPP_LESS)
24872 return true;
24873 /* Check for the sequence `<::' in the original code. It would be lexed as
24874 `[:', where `[' is a digraph, and there is no whitespace before
24875 `:'. */
24876 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24878 cp_token *token2;
24879 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24880 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24881 return true;
24883 return false;
24886 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24887 or none_type otherwise. */
24889 static enum tag_types
24890 cp_parser_token_is_class_key (cp_token* token)
24892 switch (token->keyword)
24894 case RID_CLASS:
24895 return class_type;
24896 case RID_STRUCT:
24897 return record_type;
24898 case RID_UNION:
24899 return union_type;
24901 default:
24902 return none_type;
24906 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
24907 or none_type otherwise or if the token is null. */
24909 static enum tag_types
24910 cp_parser_token_is_type_parameter_key (cp_token* token)
24912 if (!token)
24913 return none_type;
24915 switch (token->keyword)
24917 case RID_CLASS:
24918 return class_type;
24919 case RID_TYPENAME:
24920 return typename_type;
24922 default:
24923 return none_type;
24927 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24929 static void
24930 cp_parser_check_class_key (enum tag_types class_key, tree type)
24932 if (type == error_mark_node)
24933 return;
24934 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24936 if (permerror (input_location, "%qs tag used in naming %q#T",
24937 class_key == union_type ? "union"
24938 : class_key == record_type ? "struct" : "class",
24939 type))
24940 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24941 "%q#T was previously declared here", type);
24945 /* Issue an error message if DECL is redeclared with different
24946 access than its original declaration [class.access.spec/3].
24947 This applies to nested classes and nested class templates.
24948 [class.mem/1]. */
24950 static void
24951 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24953 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24954 return;
24956 if ((TREE_PRIVATE (decl)
24957 != (current_access_specifier == access_private_node))
24958 || (TREE_PROTECTED (decl)
24959 != (current_access_specifier == access_protected_node)))
24960 error_at (location, "%qD redeclared with different access", decl);
24963 /* Look for the `template' keyword, as a syntactic disambiguator.
24964 Return TRUE iff it is present, in which case it will be
24965 consumed. */
24967 static bool
24968 cp_parser_optional_template_keyword (cp_parser *parser)
24970 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24972 /* In C++98 the `template' keyword can only be used within templates;
24973 outside templates the parser can always figure out what is a
24974 template and what is not. In C++11, per the resolution of DR 468,
24975 `template' is allowed in cases where it is not strictly necessary. */
24976 if (!processing_template_decl
24977 && pedantic && cxx_dialect == cxx98)
24979 cp_token *token = cp_lexer_peek_token (parser->lexer);
24980 pedwarn (token->location, OPT_Wpedantic,
24981 "in C++98 %<template%> (as a disambiguator) is only "
24982 "allowed within templates");
24983 /* If this part of the token stream is rescanned, the same
24984 error message would be generated. So, we purge the token
24985 from the stream. */
24986 cp_lexer_purge_token (parser->lexer);
24987 return false;
24989 else
24991 /* Consume the `template' keyword. */
24992 cp_lexer_consume_token (parser->lexer);
24993 return true;
24996 return false;
24999 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25000 set PARSER->SCOPE, and perform other related actions. */
25002 static void
25003 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25005 int i;
25006 struct tree_check *check_value;
25007 deferred_access_check *chk;
25008 vec<deferred_access_check, va_gc> *checks;
25010 /* Get the stored value. */
25011 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25012 /* Perform any access checks that were deferred. */
25013 checks = check_value->checks;
25014 if (checks)
25016 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25017 perform_or_defer_access_check (chk->binfo,
25018 chk->decl,
25019 chk->diag_decl, tf_warning_or_error);
25021 /* Set the scope from the stored value. */
25022 parser->scope = check_value->value;
25023 parser->qualifying_scope = check_value->qualifying_scope;
25024 parser->object_scope = NULL_TREE;
25027 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25028 encounter the end of a block before what we were looking for. */
25030 static bool
25031 cp_parser_cache_group (cp_parser *parser,
25032 enum cpp_ttype end,
25033 unsigned depth)
25035 while (true)
25037 cp_token *token = cp_lexer_peek_token (parser->lexer);
25039 /* Abort a parenthesized expression if we encounter a semicolon. */
25040 if ((end == CPP_CLOSE_PAREN || depth == 0)
25041 && token->type == CPP_SEMICOLON)
25042 return true;
25043 /* If we've reached the end of the file, stop. */
25044 if (token->type == CPP_EOF
25045 || (end != CPP_PRAGMA_EOL
25046 && token->type == CPP_PRAGMA_EOL))
25047 return true;
25048 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25049 /* We've hit the end of an enclosing block, so there's been some
25050 kind of syntax error. */
25051 return true;
25053 /* Consume the token. */
25054 cp_lexer_consume_token (parser->lexer);
25055 /* See if it starts a new group. */
25056 if (token->type == CPP_OPEN_BRACE)
25058 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25059 /* In theory this should probably check end == '}', but
25060 cp_parser_save_member_function_body needs it to exit
25061 after either '}' or ')' when called with ')'. */
25062 if (depth == 0)
25063 return false;
25065 else if (token->type == CPP_OPEN_PAREN)
25067 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25068 if (depth == 0 && end == CPP_CLOSE_PAREN)
25069 return false;
25071 else if (token->type == CPP_PRAGMA)
25072 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25073 else if (token->type == end)
25074 return false;
25078 /* Like above, for caching a default argument or NSDMI. Both of these are
25079 terminated by a non-nested comma, but it can be unclear whether or not a
25080 comma is nested in a template argument list unless we do more parsing.
25081 In order to handle this ambiguity, when we encounter a ',' after a '<'
25082 we try to parse what follows as a parameter-declaration-list (in the
25083 case of a default argument) or a member-declarator (in the case of an
25084 NSDMI). If that succeeds, then we stop caching. */
25086 static tree
25087 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25089 unsigned depth = 0;
25090 int maybe_template_id = 0;
25091 cp_token *first_token;
25092 cp_token *token;
25093 tree default_argument;
25095 /* Add tokens until we have processed the entire default
25096 argument. We add the range [first_token, token). */
25097 first_token = cp_lexer_peek_token (parser->lexer);
25098 if (first_token->type == CPP_OPEN_BRACE)
25100 /* For list-initialization, this is straightforward. */
25101 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25102 token = cp_lexer_peek_token (parser->lexer);
25104 else while (true)
25106 bool done = false;
25108 /* Peek at the next token. */
25109 token = cp_lexer_peek_token (parser->lexer);
25110 /* What we do depends on what token we have. */
25111 switch (token->type)
25113 /* In valid code, a default argument must be
25114 immediately followed by a `,' `)', or `...'. */
25115 case CPP_COMMA:
25116 if (depth == 0 && maybe_template_id)
25118 /* If we've seen a '<', we might be in a
25119 template-argument-list. Until Core issue 325 is
25120 resolved, we don't know how this situation ought
25121 to be handled, so try to DTRT. We check whether
25122 what comes after the comma is a valid parameter
25123 declaration list. If it is, then the comma ends
25124 the default argument; otherwise the default
25125 argument continues. */
25126 bool error = false;
25128 /* Set ITALP so cp_parser_parameter_declaration_list
25129 doesn't decide to commit to this parse. */
25130 bool saved_italp = parser->in_template_argument_list_p;
25131 parser->in_template_argument_list_p = true;
25133 cp_parser_parse_tentatively (parser);
25134 cp_lexer_consume_token (parser->lexer);
25136 if (nsdmi)
25138 int ctor_dtor_or_conv_p;
25139 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25140 &ctor_dtor_or_conv_p,
25141 /*parenthesized_p=*/NULL,
25142 /*member_p=*/true,
25143 /*friend_p=*/false);
25145 else
25147 begin_scope (sk_function_parms, NULL_TREE);
25148 cp_parser_parameter_declaration_list (parser, &error);
25149 pop_bindings_and_leave_scope ();
25151 if (!cp_parser_error_occurred (parser) && !error)
25152 done = true;
25153 cp_parser_abort_tentative_parse (parser);
25155 parser->in_template_argument_list_p = saved_italp;
25156 break;
25158 case CPP_CLOSE_PAREN:
25159 case CPP_ELLIPSIS:
25160 /* If we run into a non-nested `;', `}', or `]',
25161 then the code is invalid -- but the default
25162 argument is certainly over. */
25163 case CPP_SEMICOLON:
25164 case CPP_CLOSE_BRACE:
25165 case CPP_CLOSE_SQUARE:
25166 if (depth == 0
25167 /* Handle correctly int n = sizeof ... ( p ); */
25168 && token->type != CPP_ELLIPSIS)
25169 done = true;
25170 /* Update DEPTH, if necessary. */
25171 else if (token->type == CPP_CLOSE_PAREN
25172 || token->type == CPP_CLOSE_BRACE
25173 || token->type == CPP_CLOSE_SQUARE)
25174 --depth;
25175 break;
25177 case CPP_OPEN_PAREN:
25178 case CPP_OPEN_SQUARE:
25179 case CPP_OPEN_BRACE:
25180 ++depth;
25181 break;
25183 case CPP_LESS:
25184 if (depth == 0)
25185 /* This might be the comparison operator, or it might
25186 start a template argument list. */
25187 ++maybe_template_id;
25188 break;
25190 case CPP_RSHIFT:
25191 if (cxx_dialect == cxx98)
25192 break;
25193 /* Fall through for C++0x, which treats the `>>'
25194 operator like two `>' tokens in certain
25195 cases. */
25197 case CPP_GREATER:
25198 if (depth == 0)
25200 /* This might be an operator, or it might close a
25201 template argument list. But if a previous '<'
25202 started a template argument list, this will have
25203 closed it, so we can't be in one anymore. */
25204 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25205 if (maybe_template_id < 0)
25206 maybe_template_id = 0;
25208 break;
25210 /* If we run out of tokens, issue an error message. */
25211 case CPP_EOF:
25212 case CPP_PRAGMA_EOL:
25213 error_at (token->location, "file ends in default argument");
25214 done = true;
25215 break;
25217 case CPP_NAME:
25218 case CPP_SCOPE:
25219 /* In these cases, we should look for template-ids.
25220 For example, if the default argument is
25221 `X<int, double>()', we need to do name lookup to
25222 figure out whether or not `X' is a template; if
25223 so, the `,' does not end the default argument.
25225 That is not yet done. */
25226 break;
25228 default:
25229 break;
25232 /* If we've reached the end, stop. */
25233 if (done)
25234 break;
25236 /* Add the token to the token block. */
25237 token = cp_lexer_consume_token (parser->lexer);
25240 /* Create a DEFAULT_ARG to represent the unparsed default
25241 argument. */
25242 default_argument = make_node (DEFAULT_ARG);
25243 DEFARG_TOKENS (default_argument)
25244 = cp_token_cache_new (first_token, token);
25245 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25247 return default_argument;
25250 /* Begin parsing tentatively. We always save tokens while parsing
25251 tentatively so that if the tentative parsing fails we can restore the
25252 tokens. */
25254 static void
25255 cp_parser_parse_tentatively (cp_parser* parser)
25257 /* Enter a new parsing context. */
25258 parser->context = cp_parser_context_new (parser->context);
25259 /* Begin saving tokens. */
25260 cp_lexer_save_tokens (parser->lexer);
25261 /* In order to avoid repetitive access control error messages,
25262 access checks are queued up until we are no longer parsing
25263 tentatively. */
25264 push_deferring_access_checks (dk_deferred);
25267 /* Commit to the currently active tentative parse. */
25269 static void
25270 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25272 cp_parser_context *context;
25273 cp_lexer *lexer;
25275 /* Mark all of the levels as committed. */
25276 lexer = parser->lexer;
25277 for (context = parser->context; context->next; context = context->next)
25279 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25280 break;
25281 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25282 while (!cp_lexer_saving_tokens (lexer))
25283 lexer = lexer->next;
25284 cp_lexer_commit_tokens (lexer);
25288 /* Commit to the topmost currently active tentative parse.
25290 Note that this function shouldn't be called when there are
25291 irreversible side-effects while in a tentative state. For
25292 example, we shouldn't create a permanent entry in the symbol
25293 table, or issue an error message that might not apply if the
25294 tentative parse is aborted. */
25296 static void
25297 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25299 cp_parser_context *context = parser->context;
25300 cp_lexer *lexer = parser->lexer;
25302 if (context)
25304 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25305 return;
25306 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25308 while (!cp_lexer_saving_tokens (lexer))
25309 lexer = lexer->next;
25310 cp_lexer_commit_tokens (lexer);
25314 /* Abort the currently active tentative parse. All consumed tokens
25315 will be rolled back, and no diagnostics will be issued. */
25317 static void
25318 cp_parser_abort_tentative_parse (cp_parser* parser)
25320 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25321 || errorcount > 0);
25322 cp_parser_simulate_error (parser);
25323 /* Now, pretend that we want to see if the construct was
25324 successfully parsed. */
25325 cp_parser_parse_definitely (parser);
25328 /* Stop parsing tentatively. If a parse error has occurred, restore the
25329 token stream. Otherwise, commit to the tokens we have consumed.
25330 Returns true if no error occurred; false otherwise. */
25332 static bool
25333 cp_parser_parse_definitely (cp_parser* parser)
25335 bool error_occurred;
25336 cp_parser_context *context;
25338 /* Remember whether or not an error occurred, since we are about to
25339 destroy that information. */
25340 error_occurred = cp_parser_error_occurred (parser);
25341 /* Remove the topmost context from the stack. */
25342 context = parser->context;
25343 parser->context = context->next;
25344 /* If no parse errors occurred, commit to the tentative parse. */
25345 if (!error_occurred)
25347 /* Commit to the tokens read tentatively, unless that was
25348 already done. */
25349 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25350 cp_lexer_commit_tokens (parser->lexer);
25352 pop_to_parent_deferring_access_checks ();
25354 /* Otherwise, if errors occurred, roll back our state so that things
25355 are just as they were before we began the tentative parse. */
25356 else
25358 cp_lexer_rollback_tokens (parser->lexer);
25359 pop_deferring_access_checks ();
25361 /* Add the context to the front of the free list. */
25362 context->next = cp_parser_context_free_list;
25363 cp_parser_context_free_list = context;
25365 return !error_occurred;
25368 /* Returns true if we are parsing tentatively and are not committed to
25369 this tentative parse. */
25371 static bool
25372 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25374 return (cp_parser_parsing_tentatively (parser)
25375 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25378 /* Returns nonzero iff an error has occurred during the most recent
25379 tentative parse. */
25381 static bool
25382 cp_parser_error_occurred (cp_parser* parser)
25384 return (cp_parser_parsing_tentatively (parser)
25385 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25388 /* Returns nonzero if GNU extensions are allowed. */
25390 static bool
25391 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25393 return parser->allow_gnu_extensions_p;
25396 /* Objective-C++ Productions */
25399 /* Parse an Objective-C expression, which feeds into a primary-expression
25400 above.
25402 objc-expression:
25403 objc-message-expression
25404 objc-string-literal
25405 objc-encode-expression
25406 objc-protocol-expression
25407 objc-selector-expression
25409 Returns a tree representation of the expression. */
25411 static tree
25412 cp_parser_objc_expression (cp_parser* parser)
25414 /* Try to figure out what kind of declaration is present. */
25415 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25417 switch (kwd->type)
25419 case CPP_OPEN_SQUARE:
25420 return cp_parser_objc_message_expression (parser);
25422 case CPP_OBJC_STRING:
25423 kwd = cp_lexer_consume_token (parser->lexer);
25424 return objc_build_string_object (kwd->u.value);
25426 case CPP_KEYWORD:
25427 switch (kwd->keyword)
25429 case RID_AT_ENCODE:
25430 return cp_parser_objc_encode_expression (parser);
25432 case RID_AT_PROTOCOL:
25433 return cp_parser_objc_protocol_expression (parser);
25435 case RID_AT_SELECTOR:
25436 return cp_parser_objc_selector_expression (parser);
25438 default:
25439 break;
25441 default:
25442 error_at (kwd->location,
25443 "misplaced %<@%D%> Objective-C++ construct",
25444 kwd->u.value);
25445 cp_parser_skip_to_end_of_block_or_statement (parser);
25448 return error_mark_node;
25451 /* Parse an Objective-C message expression.
25453 objc-message-expression:
25454 [ objc-message-receiver objc-message-args ]
25456 Returns a representation of an Objective-C message. */
25458 static tree
25459 cp_parser_objc_message_expression (cp_parser* parser)
25461 tree receiver, messageargs;
25463 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25464 receiver = cp_parser_objc_message_receiver (parser);
25465 messageargs = cp_parser_objc_message_args (parser);
25466 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25468 return objc_build_message_expr (receiver, messageargs);
25471 /* Parse an objc-message-receiver.
25473 objc-message-receiver:
25474 expression
25475 simple-type-specifier
25477 Returns a representation of the type or expression. */
25479 static tree
25480 cp_parser_objc_message_receiver (cp_parser* parser)
25482 tree rcv;
25484 /* An Objective-C message receiver may be either (1) a type
25485 or (2) an expression. */
25486 cp_parser_parse_tentatively (parser);
25487 rcv = cp_parser_expression (parser);
25489 if (cp_parser_parse_definitely (parser))
25490 return rcv;
25492 rcv = cp_parser_simple_type_specifier (parser,
25493 /*decl_specs=*/NULL,
25494 CP_PARSER_FLAGS_NONE);
25496 return objc_get_class_reference (rcv);
25499 /* Parse the arguments and selectors comprising an Objective-C message.
25501 objc-message-args:
25502 objc-selector
25503 objc-selector-args
25504 objc-selector-args , objc-comma-args
25506 objc-selector-args:
25507 objc-selector [opt] : assignment-expression
25508 objc-selector-args objc-selector [opt] : assignment-expression
25510 objc-comma-args:
25511 assignment-expression
25512 objc-comma-args , assignment-expression
25514 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25515 selector arguments and TREE_VALUE containing a list of comma
25516 arguments. */
25518 static tree
25519 cp_parser_objc_message_args (cp_parser* parser)
25521 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25522 bool maybe_unary_selector_p = true;
25523 cp_token *token = cp_lexer_peek_token (parser->lexer);
25525 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25527 tree selector = NULL_TREE, arg;
25529 if (token->type != CPP_COLON)
25530 selector = cp_parser_objc_selector (parser);
25532 /* Detect if we have a unary selector. */
25533 if (maybe_unary_selector_p
25534 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25535 return build_tree_list (selector, NULL_TREE);
25537 maybe_unary_selector_p = false;
25538 cp_parser_require (parser, CPP_COLON, RT_COLON);
25539 arg = cp_parser_assignment_expression (parser);
25541 sel_args
25542 = chainon (sel_args,
25543 build_tree_list (selector, arg));
25545 token = cp_lexer_peek_token (parser->lexer);
25548 /* Handle non-selector arguments, if any. */
25549 while (token->type == CPP_COMMA)
25551 tree arg;
25553 cp_lexer_consume_token (parser->lexer);
25554 arg = cp_parser_assignment_expression (parser);
25556 addl_args
25557 = chainon (addl_args,
25558 build_tree_list (NULL_TREE, arg));
25560 token = cp_lexer_peek_token (parser->lexer);
25563 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25565 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25566 return build_tree_list (error_mark_node, error_mark_node);
25569 return build_tree_list (sel_args, addl_args);
25572 /* Parse an Objective-C encode expression.
25574 objc-encode-expression:
25575 @encode objc-typename
25577 Returns an encoded representation of the type argument. */
25579 static tree
25580 cp_parser_objc_encode_expression (cp_parser* parser)
25582 tree type;
25583 cp_token *token;
25585 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25586 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25587 token = cp_lexer_peek_token (parser->lexer);
25588 type = complete_type (cp_parser_type_id (parser));
25589 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25591 if (!type)
25593 error_at (token->location,
25594 "%<@encode%> must specify a type as an argument");
25595 return error_mark_node;
25598 /* This happens if we find @encode(T) (where T is a template
25599 typename or something dependent on a template typename) when
25600 parsing a template. In that case, we can't compile it
25601 immediately, but we rather create an AT_ENCODE_EXPR which will
25602 need to be instantiated when the template is used.
25604 if (dependent_type_p (type))
25606 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25607 TREE_READONLY (value) = 1;
25608 return value;
25611 return objc_build_encode_expr (type);
25614 /* Parse an Objective-C @defs expression. */
25616 static tree
25617 cp_parser_objc_defs_expression (cp_parser *parser)
25619 tree name;
25621 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25622 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25623 name = cp_parser_identifier (parser);
25624 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25626 return objc_get_class_ivars (name);
25629 /* Parse an Objective-C protocol expression.
25631 objc-protocol-expression:
25632 @protocol ( identifier )
25634 Returns a representation of the protocol expression. */
25636 static tree
25637 cp_parser_objc_protocol_expression (cp_parser* parser)
25639 tree proto;
25641 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25642 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25643 proto = cp_parser_identifier (parser);
25644 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25646 return objc_build_protocol_expr (proto);
25649 /* Parse an Objective-C selector expression.
25651 objc-selector-expression:
25652 @selector ( objc-method-signature )
25654 objc-method-signature:
25655 objc-selector
25656 objc-selector-seq
25658 objc-selector-seq:
25659 objc-selector :
25660 objc-selector-seq objc-selector :
25662 Returns a representation of the method selector. */
25664 static tree
25665 cp_parser_objc_selector_expression (cp_parser* parser)
25667 tree sel_seq = NULL_TREE;
25668 bool maybe_unary_selector_p = true;
25669 cp_token *token;
25670 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25672 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25673 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25674 token = cp_lexer_peek_token (parser->lexer);
25676 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25677 || token->type == CPP_SCOPE)
25679 tree selector = NULL_TREE;
25681 if (token->type != CPP_COLON
25682 || token->type == CPP_SCOPE)
25683 selector = cp_parser_objc_selector (parser);
25685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25686 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25688 /* Detect if we have a unary selector. */
25689 if (maybe_unary_selector_p)
25691 sel_seq = selector;
25692 goto finish_selector;
25694 else
25696 cp_parser_error (parser, "expected %<:%>");
25699 maybe_unary_selector_p = false;
25700 token = cp_lexer_consume_token (parser->lexer);
25702 if (token->type == CPP_SCOPE)
25704 sel_seq
25705 = chainon (sel_seq,
25706 build_tree_list (selector, NULL_TREE));
25707 sel_seq
25708 = chainon (sel_seq,
25709 build_tree_list (NULL_TREE, NULL_TREE));
25711 else
25712 sel_seq
25713 = chainon (sel_seq,
25714 build_tree_list (selector, NULL_TREE));
25716 token = cp_lexer_peek_token (parser->lexer);
25719 finish_selector:
25720 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25722 return objc_build_selector_expr (loc, sel_seq);
25725 /* Parse a list of identifiers.
25727 objc-identifier-list:
25728 identifier
25729 objc-identifier-list , identifier
25731 Returns a TREE_LIST of identifier nodes. */
25733 static tree
25734 cp_parser_objc_identifier_list (cp_parser* parser)
25736 tree identifier;
25737 tree list;
25738 cp_token *sep;
25740 identifier = cp_parser_identifier (parser);
25741 if (identifier == error_mark_node)
25742 return error_mark_node;
25744 list = build_tree_list (NULL_TREE, identifier);
25745 sep = cp_lexer_peek_token (parser->lexer);
25747 while (sep->type == CPP_COMMA)
25749 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25750 identifier = cp_parser_identifier (parser);
25751 if (identifier == error_mark_node)
25752 return list;
25754 list = chainon (list, build_tree_list (NULL_TREE,
25755 identifier));
25756 sep = cp_lexer_peek_token (parser->lexer);
25759 return list;
25762 /* Parse an Objective-C alias declaration.
25764 objc-alias-declaration:
25765 @compatibility_alias identifier identifier ;
25767 This function registers the alias mapping with the Objective-C front end.
25768 It returns nothing. */
25770 static void
25771 cp_parser_objc_alias_declaration (cp_parser* parser)
25773 tree alias, orig;
25775 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25776 alias = cp_parser_identifier (parser);
25777 orig = cp_parser_identifier (parser);
25778 objc_declare_alias (alias, orig);
25779 cp_parser_consume_semicolon_at_end_of_statement (parser);
25782 /* Parse an Objective-C class forward-declaration.
25784 objc-class-declaration:
25785 @class objc-identifier-list ;
25787 The function registers the forward declarations with the Objective-C
25788 front end. It returns nothing. */
25790 static void
25791 cp_parser_objc_class_declaration (cp_parser* parser)
25793 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25794 while (true)
25796 tree id;
25798 id = cp_parser_identifier (parser);
25799 if (id == error_mark_node)
25800 break;
25802 objc_declare_class (id);
25804 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25805 cp_lexer_consume_token (parser->lexer);
25806 else
25807 break;
25809 cp_parser_consume_semicolon_at_end_of_statement (parser);
25812 /* Parse a list of Objective-C protocol references.
25814 objc-protocol-refs-opt:
25815 objc-protocol-refs [opt]
25817 objc-protocol-refs:
25818 < objc-identifier-list >
25820 Returns a TREE_LIST of identifiers, if any. */
25822 static tree
25823 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25825 tree protorefs = NULL_TREE;
25827 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25829 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25830 protorefs = cp_parser_objc_identifier_list (parser);
25831 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25834 return protorefs;
25837 /* Parse a Objective-C visibility specification. */
25839 static void
25840 cp_parser_objc_visibility_spec (cp_parser* parser)
25842 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25844 switch (vis->keyword)
25846 case RID_AT_PRIVATE:
25847 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25848 break;
25849 case RID_AT_PROTECTED:
25850 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25851 break;
25852 case RID_AT_PUBLIC:
25853 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25854 break;
25855 case RID_AT_PACKAGE:
25856 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25857 break;
25858 default:
25859 return;
25862 /* Eat '@private'/'@protected'/'@public'. */
25863 cp_lexer_consume_token (parser->lexer);
25866 /* Parse an Objective-C method type. Return 'true' if it is a class
25867 (+) method, and 'false' if it is an instance (-) method. */
25869 static inline bool
25870 cp_parser_objc_method_type (cp_parser* parser)
25872 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25873 return true;
25874 else
25875 return false;
25878 /* Parse an Objective-C protocol qualifier. */
25880 static tree
25881 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25883 tree quals = NULL_TREE, node;
25884 cp_token *token = cp_lexer_peek_token (parser->lexer);
25886 node = token->u.value;
25888 while (node && identifier_p (node)
25889 && (node == ridpointers [(int) RID_IN]
25890 || node == ridpointers [(int) RID_OUT]
25891 || node == ridpointers [(int) RID_INOUT]
25892 || node == ridpointers [(int) RID_BYCOPY]
25893 || node == ridpointers [(int) RID_BYREF]
25894 || node == ridpointers [(int) RID_ONEWAY]))
25896 quals = tree_cons (NULL_TREE, node, quals);
25897 cp_lexer_consume_token (parser->lexer);
25898 token = cp_lexer_peek_token (parser->lexer);
25899 node = token->u.value;
25902 return quals;
25905 /* Parse an Objective-C typename. */
25907 static tree
25908 cp_parser_objc_typename (cp_parser* parser)
25910 tree type_name = NULL_TREE;
25912 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25914 tree proto_quals, cp_type = NULL_TREE;
25916 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25917 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25919 /* An ObjC type name may consist of just protocol qualifiers, in which
25920 case the type shall default to 'id'. */
25921 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25923 cp_type = cp_parser_type_id (parser);
25925 /* If the type could not be parsed, an error has already
25926 been produced. For error recovery, behave as if it had
25927 not been specified, which will use the default type
25928 'id'. */
25929 if (cp_type == error_mark_node)
25931 cp_type = NULL_TREE;
25932 /* We need to skip to the closing parenthesis as
25933 cp_parser_type_id() does not seem to do it for
25934 us. */
25935 cp_parser_skip_to_closing_parenthesis (parser,
25936 /*recovering=*/true,
25937 /*or_comma=*/false,
25938 /*consume_paren=*/false);
25942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25943 type_name = build_tree_list (proto_quals, cp_type);
25946 return type_name;
25949 /* Check to see if TYPE refers to an Objective-C selector name. */
25951 static bool
25952 cp_parser_objc_selector_p (enum cpp_ttype type)
25954 return (type == CPP_NAME || type == CPP_KEYWORD
25955 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25956 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25957 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25958 || type == CPP_XOR || type == CPP_XOR_EQ);
25961 /* Parse an Objective-C selector. */
25963 static tree
25964 cp_parser_objc_selector (cp_parser* parser)
25966 cp_token *token = cp_lexer_consume_token (parser->lexer);
25968 if (!cp_parser_objc_selector_p (token->type))
25970 error_at (token->location, "invalid Objective-C++ selector name");
25971 return error_mark_node;
25974 /* C++ operator names are allowed to appear in ObjC selectors. */
25975 switch (token->type)
25977 case CPP_AND_AND: return get_identifier ("and");
25978 case CPP_AND_EQ: return get_identifier ("and_eq");
25979 case CPP_AND: return get_identifier ("bitand");
25980 case CPP_OR: return get_identifier ("bitor");
25981 case CPP_COMPL: return get_identifier ("compl");
25982 case CPP_NOT: return get_identifier ("not");
25983 case CPP_NOT_EQ: return get_identifier ("not_eq");
25984 case CPP_OR_OR: return get_identifier ("or");
25985 case CPP_OR_EQ: return get_identifier ("or_eq");
25986 case CPP_XOR: return get_identifier ("xor");
25987 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25988 default: return token->u.value;
25992 /* Parse an Objective-C params list. */
25994 static tree
25995 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25997 tree params = NULL_TREE;
25998 bool maybe_unary_selector_p = true;
25999 cp_token *token = cp_lexer_peek_token (parser->lexer);
26001 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26003 tree selector = NULL_TREE, type_name, identifier;
26004 tree parm_attr = NULL_TREE;
26006 if (token->keyword == RID_ATTRIBUTE)
26007 break;
26009 if (token->type != CPP_COLON)
26010 selector = cp_parser_objc_selector (parser);
26012 /* Detect if we have a unary selector. */
26013 if (maybe_unary_selector_p
26014 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26016 params = selector; /* Might be followed by attributes. */
26017 break;
26020 maybe_unary_selector_p = false;
26021 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26023 /* Something went quite wrong. There should be a colon
26024 here, but there is not. Stop parsing parameters. */
26025 break;
26027 type_name = cp_parser_objc_typename (parser);
26028 /* New ObjC allows attributes on parameters too. */
26029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26030 parm_attr = cp_parser_attributes_opt (parser);
26031 identifier = cp_parser_identifier (parser);
26033 params
26034 = chainon (params,
26035 objc_build_keyword_decl (selector,
26036 type_name,
26037 identifier,
26038 parm_attr));
26040 token = cp_lexer_peek_token (parser->lexer);
26043 if (params == NULL_TREE)
26045 cp_parser_error (parser, "objective-c++ method declaration is expected");
26046 return error_mark_node;
26049 /* We allow tail attributes for the method. */
26050 if (token->keyword == RID_ATTRIBUTE)
26052 *attributes = cp_parser_attributes_opt (parser);
26053 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26054 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26055 return params;
26056 cp_parser_error (parser,
26057 "method attributes must be specified at the end");
26058 return error_mark_node;
26061 if (params == NULL_TREE)
26063 cp_parser_error (parser, "objective-c++ method declaration is expected");
26064 return error_mark_node;
26066 return params;
26069 /* Parse the non-keyword Objective-C params. */
26071 static tree
26072 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26073 tree* attributes)
26075 tree params = make_node (TREE_LIST);
26076 cp_token *token = cp_lexer_peek_token (parser->lexer);
26077 *ellipsisp = false; /* Initially, assume no ellipsis. */
26079 while (token->type == CPP_COMMA)
26081 cp_parameter_declarator *parmdecl;
26082 tree parm;
26084 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26085 token = cp_lexer_peek_token (parser->lexer);
26087 if (token->type == CPP_ELLIPSIS)
26089 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26090 *ellipsisp = true;
26091 token = cp_lexer_peek_token (parser->lexer);
26092 break;
26095 /* TODO: parse attributes for tail parameters. */
26096 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26097 parm = grokdeclarator (parmdecl->declarator,
26098 &parmdecl->decl_specifiers,
26099 PARM, /*initialized=*/0,
26100 /*attrlist=*/NULL);
26102 chainon (params, build_tree_list (NULL_TREE, parm));
26103 token = cp_lexer_peek_token (parser->lexer);
26106 /* We allow tail attributes for the method. */
26107 if (token->keyword == RID_ATTRIBUTE)
26109 if (*attributes == NULL_TREE)
26111 *attributes = cp_parser_attributes_opt (parser);
26112 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26113 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26114 return params;
26116 else
26117 /* We have an error, but parse the attributes, so that we can
26118 carry on. */
26119 *attributes = cp_parser_attributes_opt (parser);
26121 cp_parser_error (parser,
26122 "method attributes must be specified at the end");
26123 return error_mark_node;
26126 return params;
26129 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26131 static void
26132 cp_parser_objc_interstitial_code (cp_parser* parser)
26134 cp_token *token = cp_lexer_peek_token (parser->lexer);
26136 /* If the next token is `extern' and the following token is a string
26137 literal, then we have a linkage specification. */
26138 if (token->keyword == RID_EXTERN
26139 && cp_parser_is_pure_string_literal
26140 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26141 cp_parser_linkage_specification (parser);
26142 /* Handle #pragma, if any. */
26143 else if (token->type == CPP_PRAGMA)
26144 cp_parser_pragma (parser, pragma_objc_icode);
26145 /* Allow stray semicolons. */
26146 else if (token->type == CPP_SEMICOLON)
26147 cp_lexer_consume_token (parser->lexer);
26148 /* Mark methods as optional or required, when building protocols. */
26149 else if (token->keyword == RID_AT_OPTIONAL)
26151 cp_lexer_consume_token (parser->lexer);
26152 objc_set_method_opt (true);
26154 else if (token->keyword == RID_AT_REQUIRED)
26156 cp_lexer_consume_token (parser->lexer);
26157 objc_set_method_opt (false);
26159 else if (token->keyword == RID_NAMESPACE)
26160 cp_parser_namespace_definition (parser);
26161 /* Other stray characters must generate errors. */
26162 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26164 cp_lexer_consume_token (parser->lexer);
26165 error ("stray %qs between Objective-C++ methods",
26166 token->type == CPP_OPEN_BRACE ? "{" : "}");
26168 /* Finally, try to parse a block-declaration, or a function-definition. */
26169 else
26170 cp_parser_block_declaration (parser, /*statement_p=*/false);
26173 /* Parse a method signature. */
26175 static tree
26176 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26178 tree rettype, kwdparms, optparms;
26179 bool ellipsis = false;
26180 bool is_class_method;
26182 is_class_method = cp_parser_objc_method_type (parser);
26183 rettype = cp_parser_objc_typename (parser);
26184 *attributes = NULL_TREE;
26185 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26186 if (kwdparms == error_mark_node)
26187 return error_mark_node;
26188 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26189 if (optparms == error_mark_node)
26190 return error_mark_node;
26192 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26195 static bool
26196 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26198 tree tattr;
26199 cp_lexer_save_tokens (parser->lexer);
26200 tattr = cp_parser_attributes_opt (parser);
26201 gcc_assert (tattr) ;
26203 /* If the attributes are followed by a method introducer, this is not allowed.
26204 Dump the attributes and flag the situation. */
26205 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26206 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26207 return true;
26209 /* Otherwise, the attributes introduce some interstitial code, possibly so
26210 rewind to allow that check. */
26211 cp_lexer_rollback_tokens (parser->lexer);
26212 return false;
26215 /* Parse an Objective-C method prototype list. */
26217 static void
26218 cp_parser_objc_method_prototype_list (cp_parser* parser)
26220 cp_token *token = cp_lexer_peek_token (parser->lexer);
26222 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26224 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26226 tree attributes, sig;
26227 bool is_class_method;
26228 if (token->type == CPP_PLUS)
26229 is_class_method = true;
26230 else
26231 is_class_method = false;
26232 sig = cp_parser_objc_method_signature (parser, &attributes);
26233 if (sig == error_mark_node)
26235 cp_parser_skip_to_end_of_block_or_statement (parser);
26236 token = cp_lexer_peek_token (parser->lexer);
26237 continue;
26239 objc_add_method_declaration (is_class_method, sig, attributes);
26240 cp_parser_consume_semicolon_at_end_of_statement (parser);
26242 else if (token->keyword == RID_AT_PROPERTY)
26243 cp_parser_objc_at_property_declaration (parser);
26244 else if (token->keyword == RID_ATTRIBUTE
26245 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26246 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26247 OPT_Wattributes,
26248 "prefix attributes are ignored for methods");
26249 else
26250 /* Allow for interspersed non-ObjC++ code. */
26251 cp_parser_objc_interstitial_code (parser);
26253 token = cp_lexer_peek_token (parser->lexer);
26256 if (token->type != CPP_EOF)
26257 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26258 else
26259 cp_parser_error (parser, "expected %<@end%>");
26261 objc_finish_interface ();
26264 /* Parse an Objective-C method definition list. */
26266 static void
26267 cp_parser_objc_method_definition_list (cp_parser* parser)
26269 cp_token *token = cp_lexer_peek_token (parser->lexer);
26271 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26273 tree meth;
26275 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26277 cp_token *ptk;
26278 tree sig, attribute;
26279 bool is_class_method;
26280 if (token->type == CPP_PLUS)
26281 is_class_method = true;
26282 else
26283 is_class_method = false;
26284 push_deferring_access_checks (dk_deferred);
26285 sig = cp_parser_objc_method_signature (parser, &attribute);
26286 if (sig == error_mark_node)
26288 cp_parser_skip_to_end_of_block_or_statement (parser);
26289 token = cp_lexer_peek_token (parser->lexer);
26290 continue;
26292 objc_start_method_definition (is_class_method, sig, attribute,
26293 NULL_TREE);
26295 /* For historical reasons, we accept an optional semicolon. */
26296 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26297 cp_lexer_consume_token (parser->lexer);
26299 ptk = cp_lexer_peek_token (parser->lexer);
26300 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26301 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26303 perform_deferred_access_checks (tf_warning_or_error);
26304 stop_deferring_access_checks ();
26305 meth = cp_parser_function_definition_after_declarator (parser,
26306 false);
26307 pop_deferring_access_checks ();
26308 objc_finish_method_definition (meth);
26311 /* The following case will be removed once @synthesize is
26312 completely implemented. */
26313 else if (token->keyword == RID_AT_PROPERTY)
26314 cp_parser_objc_at_property_declaration (parser);
26315 else if (token->keyword == RID_AT_SYNTHESIZE)
26316 cp_parser_objc_at_synthesize_declaration (parser);
26317 else if (token->keyword == RID_AT_DYNAMIC)
26318 cp_parser_objc_at_dynamic_declaration (parser);
26319 else if (token->keyword == RID_ATTRIBUTE
26320 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26321 warning_at (token->location, OPT_Wattributes,
26322 "prefix attributes are ignored for methods");
26323 else
26324 /* Allow for interspersed non-ObjC++ code. */
26325 cp_parser_objc_interstitial_code (parser);
26327 token = cp_lexer_peek_token (parser->lexer);
26330 if (token->type != CPP_EOF)
26331 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26332 else
26333 cp_parser_error (parser, "expected %<@end%>");
26335 objc_finish_implementation ();
26338 /* Parse Objective-C ivars. */
26340 static void
26341 cp_parser_objc_class_ivars (cp_parser* parser)
26343 cp_token *token = cp_lexer_peek_token (parser->lexer);
26345 if (token->type != CPP_OPEN_BRACE)
26346 return; /* No ivars specified. */
26348 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26349 token = cp_lexer_peek_token (parser->lexer);
26351 while (token->type != CPP_CLOSE_BRACE
26352 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26354 cp_decl_specifier_seq declspecs;
26355 int decl_class_or_enum_p;
26356 tree prefix_attributes;
26358 cp_parser_objc_visibility_spec (parser);
26360 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26361 break;
26363 cp_parser_decl_specifier_seq (parser,
26364 CP_PARSER_FLAGS_OPTIONAL,
26365 &declspecs,
26366 &decl_class_or_enum_p);
26368 /* auto, register, static, extern, mutable. */
26369 if (declspecs.storage_class != sc_none)
26371 cp_parser_error (parser, "invalid type for instance variable");
26372 declspecs.storage_class = sc_none;
26375 /* thread_local. */
26376 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26378 cp_parser_error (parser, "invalid type for instance variable");
26379 declspecs.locations[ds_thread] = 0;
26382 /* typedef. */
26383 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26385 cp_parser_error (parser, "invalid type for instance variable");
26386 declspecs.locations[ds_typedef] = 0;
26389 prefix_attributes = declspecs.attributes;
26390 declspecs.attributes = NULL_TREE;
26392 /* Keep going until we hit the `;' at the end of the
26393 declaration. */
26394 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26396 tree width = NULL_TREE, attributes, first_attribute, decl;
26397 cp_declarator *declarator = NULL;
26398 int ctor_dtor_or_conv_p;
26400 /* Check for a (possibly unnamed) bitfield declaration. */
26401 token = cp_lexer_peek_token (parser->lexer);
26402 if (token->type == CPP_COLON)
26403 goto eat_colon;
26405 if (token->type == CPP_NAME
26406 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26407 == CPP_COLON))
26409 /* Get the name of the bitfield. */
26410 declarator = make_id_declarator (NULL_TREE,
26411 cp_parser_identifier (parser),
26412 sfk_none);
26414 eat_colon:
26415 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26416 /* Get the width of the bitfield. */
26417 width
26418 = cp_parser_constant_expression (parser);
26420 else
26422 /* Parse the declarator. */
26423 declarator
26424 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26425 &ctor_dtor_or_conv_p,
26426 /*parenthesized_p=*/NULL,
26427 /*member_p=*/false,
26428 /*friend_p=*/false);
26431 /* Look for attributes that apply to the ivar. */
26432 attributes = cp_parser_attributes_opt (parser);
26433 /* Remember which attributes are prefix attributes and
26434 which are not. */
26435 first_attribute = attributes;
26436 /* Combine the attributes. */
26437 attributes = chainon (prefix_attributes, attributes);
26439 if (width)
26440 /* Create the bitfield declaration. */
26441 decl = grokbitfield (declarator, &declspecs,
26442 width,
26443 attributes);
26444 else
26445 decl = grokfield (declarator, &declspecs,
26446 NULL_TREE, /*init_const_expr_p=*/false,
26447 NULL_TREE, attributes);
26449 /* Add the instance variable. */
26450 if (decl != error_mark_node && decl != NULL_TREE)
26451 objc_add_instance_variable (decl);
26453 /* Reset PREFIX_ATTRIBUTES. */
26454 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26455 attributes = TREE_CHAIN (attributes);
26456 if (attributes)
26457 TREE_CHAIN (attributes) = NULL_TREE;
26459 token = cp_lexer_peek_token (parser->lexer);
26461 if (token->type == CPP_COMMA)
26463 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26464 continue;
26466 break;
26469 cp_parser_consume_semicolon_at_end_of_statement (parser);
26470 token = cp_lexer_peek_token (parser->lexer);
26473 if (token->keyword == RID_AT_END)
26474 cp_parser_error (parser, "expected %<}%>");
26476 /* Do not consume the RID_AT_END, so it will be read again as terminating
26477 the @interface of @implementation. */
26478 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26479 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26481 /* For historical reasons, we accept an optional semicolon. */
26482 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26483 cp_lexer_consume_token (parser->lexer);
26486 /* Parse an Objective-C protocol declaration. */
26488 static void
26489 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26491 tree proto, protorefs;
26492 cp_token *tok;
26494 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26495 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26497 tok = cp_lexer_peek_token (parser->lexer);
26498 error_at (tok->location, "identifier expected after %<@protocol%>");
26499 cp_parser_consume_semicolon_at_end_of_statement (parser);
26500 return;
26503 /* See if we have a forward declaration or a definition. */
26504 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26506 /* Try a forward declaration first. */
26507 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26509 while (true)
26511 tree id;
26513 id = cp_parser_identifier (parser);
26514 if (id == error_mark_node)
26515 break;
26517 objc_declare_protocol (id, attributes);
26519 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26520 cp_lexer_consume_token (parser->lexer);
26521 else
26522 break;
26524 cp_parser_consume_semicolon_at_end_of_statement (parser);
26527 /* Ok, we got a full-fledged definition (or at least should). */
26528 else
26530 proto = cp_parser_identifier (parser);
26531 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26532 objc_start_protocol (proto, protorefs, attributes);
26533 cp_parser_objc_method_prototype_list (parser);
26537 /* Parse an Objective-C superclass or category. */
26539 static void
26540 cp_parser_objc_superclass_or_category (cp_parser *parser,
26541 bool iface_p,
26542 tree *super,
26543 tree *categ, bool *is_class_extension)
26545 cp_token *next = cp_lexer_peek_token (parser->lexer);
26547 *super = *categ = NULL_TREE;
26548 *is_class_extension = false;
26549 if (next->type == CPP_COLON)
26551 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26552 *super = cp_parser_identifier (parser);
26554 else if (next->type == CPP_OPEN_PAREN)
26556 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26558 /* If there is no category name, and this is an @interface, we
26559 have a class extension. */
26560 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26562 *categ = NULL_TREE;
26563 *is_class_extension = true;
26565 else
26566 *categ = cp_parser_identifier (parser);
26568 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26572 /* Parse an Objective-C class interface. */
26574 static void
26575 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26577 tree name, super, categ, protos;
26578 bool is_class_extension;
26580 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26581 name = cp_parser_identifier (parser);
26582 if (name == error_mark_node)
26584 /* It's hard to recover because even if valid @interface stuff
26585 is to follow, we can't compile it (or validate it) if we
26586 don't even know which class it refers to. Let's assume this
26587 was a stray '@interface' token in the stream and skip it.
26589 return;
26591 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26592 &is_class_extension);
26593 protos = cp_parser_objc_protocol_refs_opt (parser);
26595 /* We have either a class or a category on our hands. */
26596 if (categ || is_class_extension)
26597 objc_start_category_interface (name, categ, protos, attributes);
26598 else
26600 objc_start_class_interface (name, super, protos, attributes);
26601 /* Handle instance variable declarations, if any. */
26602 cp_parser_objc_class_ivars (parser);
26603 objc_continue_interface ();
26606 cp_parser_objc_method_prototype_list (parser);
26609 /* Parse an Objective-C class implementation. */
26611 static void
26612 cp_parser_objc_class_implementation (cp_parser* parser)
26614 tree name, super, categ;
26615 bool is_class_extension;
26617 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26618 name = cp_parser_identifier (parser);
26619 if (name == error_mark_node)
26621 /* It's hard to recover because even if valid @implementation
26622 stuff is to follow, we can't compile it (or validate it) if
26623 we don't even know which class it refers to. Let's assume
26624 this was a stray '@implementation' token in the stream and
26625 skip it.
26627 return;
26629 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26630 &is_class_extension);
26632 /* We have either a class or a category on our hands. */
26633 if (categ)
26634 objc_start_category_implementation (name, categ);
26635 else
26637 objc_start_class_implementation (name, super);
26638 /* Handle instance variable declarations, if any. */
26639 cp_parser_objc_class_ivars (parser);
26640 objc_continue_implementation ();
26643 cp_parser_objc_method_definition_list (parser);
26646 /* Consume the @end token and finish off the implementation. */
26648 static void
26649 cp_parser_objc_end_implementation (cp_parser* parser)
26651 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26652 objc_finish_implementation ();
26655 /* Parse an Objective-C declaration. */
26657 static void
26658 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26660 /* Try to figure out what kind of declaration is present. */
26661 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26663 if (attributes)
26664 switch (kwd->keyword)
26666 case RID_AT_ALIAS:
26667 case RID_AT_CLASS:
26668 case RID_AT_END:
26669 error_at (kwd->location, "attributes may not be specified before"
26670 " the %<@%D%> Objective-C++ keyword",
26671 kwd->u.value);
26672 attributes = NULL;
26673 break;
26674 case RID_AT_IMPLEMENTATION:
26675 warning_at (kwd->location, OPT_Wattributes,
26676 "prefix attributes are ignored before %<@%D%>",
26677 kwd->u.value);
26678 attributes = NULL;
26679 default:
26680 break;
26683 switch (kwd->keyword)
26685 case RID_AT_ALIAS:
26686 cp_parser_objc_alias_declaration (parser);
26687 break;
26688 case RID_AT_CLASS:
26689 cp_parser_objc_class_declaration (parser);
26690 break;
26691 case RID_AT_PROTOCOL:
26692 cp_parser_objc_protocol_declaration (parser, attributes);
26693 break;
26694 case RID_AT_INTERFACE:
26695 cp_parser_objc_class_interface (parser, attributes);
26696 break;
26697 case RID_AT_IMPLEMENTATION:
26698 cp_parser_objc_class_implementation (parser);
26699 break;
26700 case RID_AT_END:
26701 cp_parser_objc_end_implementation (parser);
26702 break;
26703 default:
26704 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26705 kwd->u.value);
26706 cp_parser_skip_to_end_of_block_or_statement (parser);
26710 /* Parse an Objective-C try-catch-finally statement.
26712 objc-try-catch-finally-stmt:
26713 @try compound-statement objc-catch-clause-seq [opt]
26714 objc-finally-clause [opt]
26716 objc-catch-clause-seq:
26717 objc-catch-clause objc-catch-clause-seq [opt]
26719 objc-catch-clause:
26720 @catch ( objc-exception-declaration ) compound-statement
26722 objc-finally-clause:
26723 @finally compound-statement
26725 objc-exception-declaration:
26726 parameter-declaration
26727 '...'
26729 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26731 Returns NULL_TREE.
26733 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26734 for C. Keep them in sync. */
26736 static tree
26737 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26739 location_t location;
26740 tree stmt;
26742 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26743 location = cp_lexer_peek_token (parser->lexer)->location;
26744 objc_maybe_warn_exceptions (location);
26745 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26746 node, lest it get absorbed into the surrounding block. */
26747 stmt = push_stmt_list ();
26748 cp_parser_compound_statement (parser, NULL, false, false);
26749 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26751 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26753 cp_parameter_declarator *parm;
26754 tree parameter_declaration = error_mark_node;
26755 bool seen_open_paren = false;
26757 cp_lexer_consume_token (parser->lexer);
26758 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26759 seen_open_paren = true;
26760 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26762 /* We have "@catch (...)" (where the '...' are literally
26763 what is in the code). Skip the '...'.
26764 parameter_declaration is set to NULL_TREE, and
26765 objc_being_catch_clauses() knows that that means
26766 '...'. */
26767 cp_lexer_consume_token (parser->lexer);
26768 parameter_declaration = NULL_TREE;
26770 else
26772 /* We have "@catch (NSException *exception)" or something
26773 like that. Parse the parameter declaration. */
26774 parm = cp_parser_parameter_declaration (parser, false, NULL);
26775 if (parm == NULL)
26776 parameter_declaration = error_mark_node;
26777 else
26778 parameter_declaration = grokdeclarator (parm->declarator,
26779 &parm->decl_specifiers,
26780 PARM, /*initialized=*/0,
26781 /*attrlist=*/NULL);
26783 if (seen_open_paren)
26784 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26785 else
26787 /* If there was no open parenthesis, we are recovering from
26788 an error, and we are trying to figure out what mistake
26789 the user has made. */
26791 /* If there is an immediate closing parenthesis, the user
26792 probably forgot the opening one (ie, they typed "@catch
26793 NSException *e)". Parse the closing parenthesis and keep
26794 going. */
26795 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26796 cp_lexer_consume_token (parser->lexer);
26798 /* If these is no immediate closing parenthesis, the user
26799 probably doesn't know that parenthesis are required at
26800 all (ie, they typed "@catch NSException *e"). So, just
26801 forget about the closing parenthesis and keep going. */
26803 objc_begin_catch_clause (parameter_declaration);
26804 cp_parser_compound_statement (parser, NULL, false, false);
26805 objc_finish_catch_clause ();
26807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26809 cp_lexer_consume_token (parser->lexer);
26810 location = cp_lexer_peek_token (parser->lexer)->location;
26811 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26812 node, lest it get absorbed into the surrounding block. */
26813 stmt = push_stmt_list ();
26814 cp_parser_compound_statement (parser, NULL, false, false);
26815 objc_build_finally_clause (location, pop_stmt_list (stmt));
26818 return objc_finish_try_stmt ();
26821 /* Parse an Objective-C synchronized statement.
26823 objc-synchronized-stmt:
26824 @synchronized ( expression ) compound-statement
26826 Returns NULL_TREE. */
26828 static tree
26829 cp_parser_objc_synchronized_statement (cp_parser *parser)
26831 location_t location;
26832 tree lock, stmt;
26834 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26836 location = cp_lexer_peek_token (parser->lexer)->location;
26837 objc_maybe_warn_exceptions (location);
26838 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26839 lock = cp_parser_expression (parser);
26840 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26842 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26843 node, lest it get absorbed into the surrounding block. */
26844 stmt = push_stmt_list ();
26845 cp_parser_compound_statement (parser, NULL, false, false);
26847 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26850 /* Parse an Objective-C throw statement.
26852 objc-throw-stmt:
26853 @throw assignment-expression [opt] ;
26855 Returns a constructed '@throw' statement. */
26857 static tree
26858 cp_parser_objc_throw_statement (cp_parser *parser)
26860 tree expr = NULL_TREE;
26861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26863 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26866 expr = cp_parser_expression (parser);
26868 cp_parser_consume_semicolon_at_end_of_statement (parser);
26870 return objc_build_throw_stmt (loc, expr);
26873 /* Parse an Objective-C statement. */
26875 static tree
26876 cp_parser_objc_statement (cp_parser * parser)
26878 /* Try to figure out what kind of declaration is present. */
26879 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26881 switch (kwd->keyword)
26883 case RID_AT_TRY:
26884 return cp_parser_objc_try_catch_finally_statement (parser);
26885 case RID_AT_SYNCHRONIZED:
26886 return cp_parser_objc_synchronized_statement (parser);
26887 case RID_AT_THROW:
26888 return cp_parser_objc_throw_statement (parser);
26889 default:
26890 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26891 kwd->u.value);
26892 cp_parser_skip_to_end_of_block_or_statement (parser);
26895 return error_mark_node;
26898 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26899 look ahead to see if an objc keyword follows the attributes. This
26900 is to detect the use of prefix attributes on ObjC @interface and
26901 @protocol. */
26903 static bool
26904 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26906 cp_lexer_save_tokens (parser->lexer);
26907 *attrib = cp_parser_attributes_opt (parser);
26908 gcc_assert (*attrib);
26909 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26911 cp_lexer_commit_tokens (parser->lexer);
26912 return true;
26914 cp_lexer_rollback_tokens (parser->lexer);
26915 return false;
26918 /* This routine is a minimal replacement for
26919 c_parser_struct_declaration () used when parsing the list of
26920 types/names or ObjC++ properties. For example, when parsing the
26921 code
26923 @property (readonly) int a, b, c;
26925 this function is responsible for parsing "int a, int b, int c" and
26926 returning the declarations as CHAIN of DECLs.
26928 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26929 similar parsing. */
26930 static tree
26931 cp_parser_objc_struct_declaration (cp_parser *parser)
26933 tree decls = NULL_TREE;
26934 cp_decl_specifier_seq declspecs;
26935 int decl_class_or_enum_p;
26936 tree prefix_attributes;
26938 cp_parser_decl_specifier_seq (parser,
26939 CP_PARSER_FLAGS_NONE,
26940 &declspecs,
26941 &decl_class_or_enum_p);
26943 if (declspecs.type == error_mark_node)
26944 return error_mark_node;
26946 /* auto, register, static, extern, mutable. */
26947 if (declspecs.storage_class != sc_none)
26949 cp_parser_error (parser, "invalid type for property");
26950 declspecs.storage_class = sc_none;
26953 /* thread_local. */
26954 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26956 cp_parser_error (parser, "invalid type for property");
26957 declspecs.locations[ds_thread] = 0;
26960 /* typedef. */
26961 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26963 cp_parser_error (parser, "invalid type for property");
26964 declspecs.locations[ds_typedef] = 0;
26967 prefix_attributes = declspecs.attributes;
26968 declspecs.attributes = NULL_TREE;
26970 /* Keep going until we hit the `;' at the end of the declaration. */
26971 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26973 tree attributes, first_attribute, decl;
26974 cp_declarator *declarator;
26975 cp_token *token;
26977 /* Parse the declarator. */
26978 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26979 NULL, NULL, false, false);
26981 /* Look for attributes that apply to the ivar. */
26982 attributes = cp_parser_attributes_opt (parser);
26983 /* Remember which attributes are prefix attributes and
26984 which are not. */
26985 first_attribute = attributes;
26986 /* Combine the attributes. */
26987 attributes = chainon (prefix_attributes, attributes);
26989 decl = grokfield (declarator, &declspecs,
26990 NULL_TREE, /*init_const_expr_p=*/false,
26991 NULL_TREE, attributes);
26993 if (decl == error_mark_node || decl == NULL_TREE)
26994 return error_mark_node;
26996 /* Reset PREFIX_ATTRIBUTES. */
26997 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26998 attributes = TREE_CHAIN (attributes);
26999 if (attributes)
27000 TREE_CHAIN (attributes) = NULL_TREE;
27002 DECL_CHAIN (decl) = decls;
27003 decls = decl;
27005 token = cp_lexer_peek_token (parser->lexer);
27006 if (token->type == CPP_COMMA)
27008 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27009 continue;
27011 else
27012 break;
27014 return decls;
27017 /* Parse an Objective-C @property declaration. The syntax is:
27019 objc-property-declaration:
27020 '@property' objc-property-attributes[opt] struct-declaration ;
27022 objc-property-attributes:
27023 '(' objc-property-attribute-list ')'
27025 objc-property-attribute-list:
27026 objc-property-attribute
27027 objc-property-attribute-list, objc-property-attribute
27029 objc-property-attribute
27030 'getter' = identifier
27031 'setter' = identifier
27032 'readonly'
27033 'readwrite'
27034 'assign'
27035 'retain'
27036 'copy'
27037 'nonatomic'
27039 For example:
27040 @property NSString *name;
27041 @property (readonly) id object;
27042 @property (retain, nonatomic, getter=getTheName) id name;
27043 @property int a, b, c;
27045 PS: This function is identical to
27046 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27047 static void
27048 cp_parser_objc_at_property_declaration (cp_parser *parser)
27050 /* The following variables hold the attributes of the properties as
27051 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27052 seen. When we see an attribute, we set them to 'true' (if they
27053 are boolean properties) or to the identifier (if they have an
27054 argument, ie, for getter and setter). Note that here we only
27055 parse the list of attributes, check the syntax and accumulate the
27056 attributes that we find. objc_add_property_declaration() will
27057 then process the information. */
27058 bool property_assign = false;
27059 bool property_copy = false;
27060 tree property_getter_ident = NULL_TREE;
27061 bool property_nonatomic = false;
27062 bool property_readonly = false;
27063 bool property_readwrite = false;
27064 bool property_retain = false;
27065 tree property_setter_ident = NULL_TREE;
27067 /* 'properties' is the list of properties that we read. Usually a
27068 single one, but maybe more (eg, in "@property int a, b, c;" there
27069 are three). */
27070 tree properties;
27071 location_t loc;
27073 loc = cp_lexer_peek_token (parser->lexer)->location;
27075 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27077 /* Parse the optional attribute list... */
27078 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27080 /* Eat the '('. */
27081 cp_lexer_consume_token (parser->lexer);
27083 while (true)
27085 bool syntax_error = false;
27086 cp_token *token = cp_lexer_peek_token (parser->lexer);
27087 enum rid keyword;
27089 if (token->type != CPP_NAME)
27091 cp_parser_error (parser, "expected identifier");
27092 break;
27094 keyword = C_RID_CODE (token->u.value);
27095 cp_lexer_consume_token (parser->lexer);
27096 switch (keyword)
27098 case RID_ASSIGN: property_assign = true; break;
27099 case RID_COPY: property_copy = true; break;
27100 case RID_NONATOMIC: property_nonatomic = true; break;
27101 case RID_READONLY: property_readonly = true; break;
27102 case RID_READWRITE: property_readwrite = true; break;
27103 case RID_RETAIN: property_retain = true; break;
27105 case RID_GETTER:
27106 case RID_SETTER:
27107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27109 if (keyword == RID_GETTER)
27110 cp_parser_error (parser,
27111 "missing %<=%> (after %<getter%> attribute)");
27112 else
27113 cp_parser_error (parser,
27114 "missing %<=%> (after %<setter%> attribute)");
27115 syntax_error = true;
27116 break;
27118 cp_lexer_consume_token (parser->lexer); /* eat the = */
27119 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27121 cp_parser_error (parser, "expected identifier");
27122 syntax_error = true;
27123 break;
27125 if (keyword == RID_SETTER)
27127 if (property_setter_ident != NULL_TREE)
27129 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27130 cp_lexer_consume_token (parser->lexer);
27132 else
27133 property_setter_ident = cp_parser_objc_selector (parser);
27134 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27135 cp_parser_error (parser, "setter name must terminate with %<:%>");
27136 else
27137 cp_lexer_consume_token (parser->lexer);
27139 else
27141 if (property_getter_ident != NULL_TREE)
27143 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27144 cp_lexer_consume_token (parser->lexer);
27146 else
27147 property_getter_ident = cp_parser_objc_selector (parser);
27149 break;
27150 default:
27151 cp_parser_error (parser, "unknown property attribute");
27152 syntax_error = true;
27153 break;
27156 if (syntax_error)
27157 break;
27159 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27160 cp_lexer_consume_token (parser->lexer);
27161 else
27162 break;
27165 /* FIXME: "@property (setter, assign);" will generate a spurious
27166 "error: expected ‘)’ before ‘,’ token". This is because
27167 cp_parser_require, unlike the C counterpart, will produce an
27168 error even if we are in error recovery. */
27169 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27171 cp_parser_skip_to_closing_parenthesis (parser,
27172 /*recovering=*/true,
27173 /*or_comma=*/false,
27174 /*consume_paren=*/true);
27178 /* ... and the property declaration(s). */
27179 properties = cp_parser_objc_struct_declaration (parser);
27181 if (properties == error_mark_node)
27183 cp_parser_skip_to_end_of_statement (parser);
27184 /* If the next token is now a `;', consume it. */
27185 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27186 cp_lexer_consume_token (parser->lexer);
27187 return;
27190 if (properties == NULL_TREE)
27191 cp_parser_error (parser, "expected identifier");
27192 else
27194 /* Comma-separated properties are chained together in
27195 reverse order; add them one by one. */
27196 properties = nreverse (properties);
27198 for (; properties; properties = TREE_CHAIN (properties))
27199 objc_add_property_declaration (loc, copy_node (properties),
27200 property_readonly, property_readwrite,
27201 property_assign, property_retain,
27202 property_copy, property_nonatomic,
27203 property_getter_ident, property_setter_ident);
27206 cp_parser_consume_semicolon_at_end_of_statement (parser);
27209 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27211 objc-synthesize-declaration:
27212 @synthesize objc-synthesize-identifier-list ;
27214 objc-synthesize-identifier-list:
27215 objc-synthesize-identifier
27216 objc-synthesize-identifier-list, objc-synthesize-identifier
27218 objc-synthesize-identifier
27219 identifier
27220 identifier = identifier
27222 For example:
27223 @synthesize MyProperty;
27224 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27226 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27227 for C. Keep them in sync.
27229 static void
27230 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27232 tree list = NULL_TREE;
27233 location_t loc;
27234 loc = cp_lexer_peek_token (parser->lexer)->location;
27236 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27237 while (true)
27239 tree property, ivar;
27240 property = cp_parser_identifier (parser);
27241 if (property == error_mark_node)
27243 cp_parser_consume_semicolon_at_end_of_statement (parser);
27244 return;
27246 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27248 cp_lexer_consume_token (parser->lexer);
27249 ivar = cp_parser_identifier (parser);
27250 if (ivar == error_mark_node)
27252 cp_parser_consume_semicolon_at_end_of_statement (parser);
27253 return;
27256 else
27257 ivar = NULL_TREE;
27258 list = chainon (list, build_tree_list (ivar, property));
27259 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27260 cp_lexer_consume_token (parser->lexer);
27261 else
27262 break;
27264 cp_parser_consume_semicolon_at_end_of_statement (parser);
27265 objc_add_synthesize_declaration (loc, list);
27268 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27270 objc-dynamic-declaration:
27271 @dynamic identifier-list ;
27273 For example:
27274 @dynamic MyProperty;
27275 @dynamic MyProperty, AnotherProperty;
27277 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27278 for C. Keep them in sync.
27280 static void
27281 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27283 tree list = NULL_TREE;
27284 location_t loc;
27285 loc = cp_lexer_peek_token (parser->lexer)->location;
27287 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27288 while (true)
27290 tree property;
27291 property = cp_parser_identifier (parser);
27292 if (property == error_mark_node)
27294 cp_parser_consume_semicolon_at_end_of_statement (parser);
27295 return;
27297 list = chainon (list, build_tree_list (NULL, property));
27298 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27299 cp_lexer_consume_token (parser->lexer);
27300 else
27301 break;
27303 cp_parser_consume_semicolon_at_end_of_statement (parser);
27304 objc_add_dynamic_declaration (loc, list);
27308 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27310 /* Returns name of the next clause.
27311 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27312 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27313 returned and the token is consumed. */
27315 static pragma_omp_clause
27316 cp_parser_omp_clause_name (cp_parser *parser)
27318 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27320 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27321 result = PRAGMA_OMP_CLAUSE_IF;
27322 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27323 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27324 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27325 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27326 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27327 result = PRAGMA_OMP_CLAUSE_FOR;
27328 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27330 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27331 const char *p = IDENTIFIER_POINTER (id);
27333 switch (p[0])
27335 case 'a':
27336 if (!strcmp ("aligned", p))
27337 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27338 break;
27339 case 'c':
27340 if (!strcmp ("collapse", p))
27341 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27342 else if (!strcmp ("copyin", p))
27343 result = PRAGMA_OMP_CLAUSE_COPYIN;
27344 else if (!strcmp ("copyprivate", p))
27345 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27346 break;
27347 case 'd':
27348 if (!strcmp ("depend", p))
27349 result = PRAGMA_OMP_CLAUSE_DEPEND;
27350 else if (!strcmp ("device", p))
27351 result = PRAGMA_OMP_CLAUSE_DEVICE;
27352 else if (!strcmp ("dist_schedule", p))
27353 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27354 break;
27355 case 'f':
27356 if (!strcmp ("final", p))
27357 result = PRAGMA_OMP_CLAUSE_FINAL;
27358 else if (!strcmp ("firstprivate", p))
27359 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27360 else if (!strcmp ("from", p))
27361 result = PRAGMA_OMP_CLAUSE_FROM;
27362 break;
27363 case 'i':
27364 if (!strcmp ("inbranch", p))
27365 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27366 break;
27367 case 'l':
27368 if (!strcmp ("lastprivate", p))
27369 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27370 else if (!strcmp ("linear", p))
27371 result = PRAGMA_OMP_CLAUSE_LINEAR;
27372 break;
27373 case 'm':
27374 if (!strcmp ("map", p))
27375 result = PRAGMA_OMP_CLAUSE_MAP;
27376 else if (!strcmp ("mergeable", p))
27377 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27378 else if (flag_cilkplus && !strcmp ("mask", p))
27379 result = PRAGMA_CILK_CLAUSE_MASK;
27380 break;
27381 case 'n':
27382 if (!strcmp ("notinbranch", p))
27383 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27384 else if (!strcmp ("nowait", p))
27385 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27386 else if (flag_cilkplus && !strcmp ("nomask", p))
27387 result = PRAGMA_CILK_CLAUSE_NOMASK;
27388 else if (!strcmp ("num_teams", p))
27389 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27390 else if (!strcmp ("num_threads", p))
27391 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27392 break;
27393 case 'o':
27394 if (!strcmp ("ordered", p))
27395 result = PRAGMA_OMP_CLAUSE_ORDERED;
27396 break;
27397 case 'p':
27398 if (!strcmp ("parallel", p))
27399 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27400 else if (!strcmp ("proc_bind", p))
27401 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27402 break;
27403 case 'r':
27404 if (!strcmp ("reduction", p))
27405 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27406 break;
27407 case 's':
27408 if (!strcmp ("safelen", p))
27409 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27410 else if (!strcmp ("schedule", p))
27411 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27412 else if (!strcmp ("sections", p))
27413 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27414 else if (!strcmp ("shared", p))
27415 result = PRAGMA_OMP_CLAUSE_SHARED;
27416 else if (!strcmp ("simdlen", p))
27417 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27418 break;
27419 case 't':
27420 if (!strcmp ("taskgroup", p))
27421 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27422 else if (!strcmp ("thread_limit", p))
27423 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27424 else if (!strcmp ("to", p))
27425 result = PRAGMA_OMP_CLAUSE_TO;
27426 break;
27427 case 'u':
27428 if (!strcmp ("uniform", p))
27429 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27430 else if (!strcmp ("untied", p))
27431 result = PRAGMA_OMP_CLAUSE_UNTIED;
27432 break;
27433 case 'v':
27434 if (flag_cilkplus && !strcmp ("vectorlength", p))
27435 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27436 break;
27440 if (result != PRAGMA_OMP_CLAUSE_NONE)
27441 cp_lexer_consume_token (parser->lexer);
27443 return result;
27446 /* Validate that a clause of the given type does not already exist. */
27448 static void
27449 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27450 const char *name, location_t location)
27452 tree c;
27454 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27455 if (OMP_CLAUSE_CODE (c) == code)
27457 error_at (location, "too many %qs clauses", name);
27458 break;
27462 /* OpenMP 2.5:
27463 variable-list:
27464 identifier
27465 variable-list , identifier
27467 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27468 colon). An opening parenthesis will have been consumed by the caller.
27470 If KIND is nonzero, create the appropriate node and install the decl
27471 in OMP_CLAUSE_DECL and add the node to the head of the list.
27473 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27474 return the list created.
27476 COLON can be NULL if only closing parenthesis should end the list,
27477 or pointer to bool which will receive false if the list is terminated
27478 by closing parenthesis or true if the list is terminated by colon. */
27480 static tree
27481 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27482 tree list, bool *colon)
27484 cp_token *token;
27485 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27486 if (colon)
27488 parser->colon_corrects_to_scope_p = false;
27489 *colon = false;
27491 while (1)
27493 tree name, decl;
27495 token = cp_lexer_peek_token (parser->lexer);
27496 name = cp_parser_id_expression (parser, /*template_p=*/false,
27497 /*check_dependency_p=*/true,
27498 /*template_p=*/NULL,
27499 /*declarator_p=*/false,
27500 /*optional_p=*/false);
27501 if (name == error_mark_node)
27502 goto skip_comma;
27504 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27505 if (decl == error_mark_node)
27506 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27507 token->location);
27508 else if (kind != 0)
27510 switch (kind)
27512 case OMP_CLAUSE_MAP:
27513 case OMP_CLAUSE_FROM:
27514 case OMP_CLAUSE_TO:
27515 case OMP_CLAUSE_DEPEND:
27516 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27518 tree low_bound = NULL_TREE, length = NULL_TREE;
27520 parser->colon_corrects_to_scope_p = false;
27521 cp_lexer_consume_token (parser->lexer);
27522 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27523 low_bound = cp_parser_expression (parser);
27524 if (!colon)
27525 parser->colon_corrects_to_scope_p
27526 = saved_colon_corrects_to_scope_p;
27527 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27528 length = integer_one_node;
27529 else
27531 /* Look for `:'. */
27532 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27533 goto skip_comma;
27534 if (!cp_lexer_next_token_is (parser->lexer,
27535 CPP_CLOSE_SQUARE))
27536 length = cp_parser_expression (parser);
27538 /* Look for the closing `]'. */
27539 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27540 RT_CLOSE_SQUARE))
27541 goto skip_comma;
27542 decl = tree_cons (low_bound, length, decl);
27544 break;
27545 default:
27546 break;
27549 tree u = build_omp_clause (token->location, kind);
27550 OMP_CLAUSE_DECL (u) = decl;
27551 OMP_CLAUSE_CHAIN (u) = list;
27552 list = u;
27554 else
27555 list = tree_cons (decl, NULL_TREE, list);
27557 get_comma:
27558 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27559 break;
27560 cp_lexer_consume_token (parser->lexer);
27563 if (colon)
27564 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27566 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27568 *colon = true;
27569 cp_parser_require (parser, CPP_COLON, RT_COLON);
27570 return list;
27573 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27575 int ending;
27577 /* Try to resync to an unnested comma. Copied from
27578 cp_parser_parenthesized_expression_list. */
27579 skip_comma:
27580 if (colon)
27581 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27582 ending = cp_parser_skip_to_closing_parenthesis (parser,
27583 /*recovering=*/true,
27584 /*or_comma=*/true,
27585 /*consume_paren=*/true);
27586 if (ending < 0)
27587 goto get_comma;
27590 return list;
27593 /* Similarly, but expect leading and trailing parenthesis. This is a very
27594 common case for omp clauses. */
27596 static tree
27597 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27599 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27600 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27601 return list;
27604 /* OpenMP 3.0:
27605 collapse ( constant-expression ) */
27607 static tree
27608 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27610 tree c, num;
27611 location_t loc;
27612 HOST_WIDE_INT n;
27614 loc = cp_lexer_peek_token (parser->lexer)->location;
27615 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27616 return list;
27618 num = cp_parser_constant_expression (parser);
27620 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27621 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27622 /*or_comma=*/false,
27623 /*consume_paren=*/true);
27625 if (num == error_mark_node)
27626 return list;
27627 num = fold_non_dependent_expr (num);
27628 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27629 || !tree_fits_shwi_p (num)
27630 || (n = tree_to_shwi (num)) <= 0
27631 || (int) n != n)
27633 error_at (loc, "collapse argument needs positive constant integer expression");
27634 return list;
27637 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27638 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27639 OMP_CLAUSE_CHAIN (c) = list;
27640 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27642 return c;
27645 /* OpenMP 2.5:
27646 default ( shared | none ) */
27648 static tree
27649 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27651 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27652 tree c;
27654 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27655 return list;
27656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27658 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27659 const char *p = IDENTIFIER_POINTER (id);
27661 switch (p[0])
27663 case 'n':
27664 if (strcmp ("none", p) != 0)
27665 goto invalid_kind;
27666 kind = OMP_CLAUSE_DEFAULT_NONE;
27667 break;
27669 case 's':
27670 if (strcmp ("shared", p) != 0)
27671 goto invalid_kind;
27672 kind = OMP_CLAUSE_DEFAULT_SHARED;
27673 break;
27675 default:
27676 goto invalid_kind;
27679 cp_lexer_consume_token (parser->lexer);
27681 else
27683 invalid_kind:
27684 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27687 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27688 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27689 /*or_comma=*/false,
27690 /*consume_paren=*/true);
27692 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27693 return list;
27695 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27696 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27697 OMP_CLAUSE_CHAIN (c) = list;
27698 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27700 return c;
27703 /* OpenMP 3.1:
27704 final ( expression ) */
27706 static tree
27707 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27709 tree t, c;
27711 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27712 return list;
27714 t = cp_parser_condition (parser);
27716 if (t == error_mark_node
27717 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27718 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27719 /*or_comma=*/false,
27720 /*consume_paren=*/true);
27722 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27724 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27725 OMP_CLAUSE_FINAL_EXPR (c) = t;
27726 OMP_CLAUSE_CHAIN (c) = list;
27728 return c;
27731 /* OpenMP 2.5:
27732 if ( expression ) */
27734 static tree
27735 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27737 tree t, c;
27739 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27740 return list;
27742 t = cp_parser_condition (parser);
27744 if (t == error_mark_node
27745 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27746 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27747 /*or_comma=*/false,
27748 /*consume_paren=*/true);
27750 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27752 c = build_omp_clause (location, OMP_CLAUSE_IF);
27753 OMP_CLAUSE_IF_EXPR (c) = t;
27754 OMP_CLAUSE_CHAIN (c) = list;
27756 return c;
27759 /* OpenMP 3.1:
27760 mergeable */
27762 static tree
27763 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27764 tree list, location_t location)
27766 tree c;
27768 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27769 location);
27771 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27772 OMP_CLAUSE_CHAIN (c) = list;
27773 return c;
27776 /* OpenMP 2.5:
27777 nowait */
27779 static tree
27780 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27781 tree list, location_t location)
27783 tree c;
27785 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27787 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27788 OMP_CLAUSE_CHAIN (c) = list;
27789 return c;
27792 /* OpenMP 2.5:
27793 num_threads ( expression ) */
27795 static tree
27796 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27797 location_t location)
27799 tree t, c;
27801 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27802 return list;
27804 t = cp_parser_expression (parser);
27806 if (t == error_mark_node
27807 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27808 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27809 /*or_comma=*/false,
27810 /*consume_paren=*/true);
27812 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27813 "num_threads", location);
27815 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27816 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27817 OMP_CLAUSE_CHAIN (c) = list;
27819 return c;
27822 /* OpenMP 2.5:
27823 ordered */
27825 static tree
27826 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27827 tree list, location_t location)
27829 tree c;
27831 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27832 "ordered", location);
27834 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27835 OMP_CLAUSE_CHAIN (c) = list;
27836 return c;
27839 /* OpenMP 2.5:
27840 reduction ( reduction-operator : variable-list )
27842 reduction-operator:
27843 One of: + * - & ^ | && ||
27845 OpenMP 3.1:
27847 reduction-operator:
27848 One of: + * - & ^ | && || min max
27850 OpenMP 4.0:
27852 reduction-operator:
27853 One of: + * - & ^ | && ||
27854 id-expression */
27856 static tree
27857 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27859 enum tree_code code = ERROR_MARK;
27860 tree nlist, c, id = NULL_TREE;
27862 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27863 return list;
27865 switch (cp_lexer_peek_token (parser->lexer)->type)
27867 case CPP_PLUS: code = PLUS_EXPR; break;
27868 case CPP_MULT: code = MULT_EXPR; break;
27869 case CPP_MINUS: code = MINUS_EXPR; break;
27870 case CPP_AND: code = BIT_AND_EXPR; break;
27871 case CPP_XOR: code = BIT_XOR_EXPR; break;
27872 case CPP_OR: code = BIT_IOR_EXPR; break;
27873 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27874 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27875 default: break;
27878 if (code != ERROR_MARK)
27879 cp_lexer_consume_token (parser->lexer);
27880 else
27882 bool saved_colon_corrects_to_scope_p;
27883 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27884 parser->colon_corrects_to_scope_p = false;
27885 id = cp_parser_id_expression (parser, /*template_p=*/false,
27886 /*check_dependency_p=*/true,
27887 /*template_p=*/NULL,
27888 /*declarator_p=*/false,
27889 /*optional_p=*/false);
27890 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27891 if (identifier_p (id))
27893 const char *p = IDENTIFIER_POINTER (id);
27895 if (strcmp (p, "min") == 0)
27896 code = MIN_EXPR;
27897 else if (strcmp (p, "max") == 0)
27898 code = MAX_EXPR;
27899 else if (id == ansi_opname (PLUS_EXPR))
27900 code = PLUS_EXPR;
27901 else if (id == ansi_opname (MULT_EXPR))
27902 code = MULT_EXPR;
27903 else if (id == ansi_opname (MINUS_EXPR))
27904 code = MINUS_EXPR;
27905 else if (id == ansi_opname (BIT_AND_EXPR))
27906 code = BIT_AND_EXPR;
27907 else if (id == ansi_opname (BIT_IOR_EXPR))
27908 code = BIT_IOR_EXPR;
27909 else if (id == ansi_opname (BIT_XOR_EXPR))
27910 code = BIT_XOR_EXPR;
27911 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27912 code = TRUTH_ANDIF_EXPR;
27913 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27914 code = TRUTH_ORIF_EXPR;
27915 id = omp_reduction_id (code, id, NULL_TREE);
27916 tree scope = parser->scope;
27917 if (scope)
27918 id = build_qualified_name (NULL_TREE, scope, id, false);
27919 parser->scope = NULL_TREE;
27920 parser->qualifying_scope = NULL_TREE;
27921 parser->object_scope = NULL_TREE;
27923 else
27925 error ("invalid reduction-identifier");
27926 resync_fail:
27927 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27928 /*or_comma=*/false,
27929 /*consume_paren=*/true);
27930 return list;
27934 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27935 goto resync_fail;
27937 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27938 NULL);
27939 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27941 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27942 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27945 return nlist;
27948 /* OpenMP 2.5:
27949 schedule ( schedule-kind )
27950 schedule ( schedule-kind , expression )
27952 schedule-kind:
27953 static | dynamic | guided | runtime | auto */
27955 static tree
27956 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27958 tree c, t;
27960 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27961 return list;
27963 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27965 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27967 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27968 const char *p = IDENTIFIER_POINTER (id);
27970 switch (p[0])
27972 case 'd':
27973 if (strcmp ("dynamic", p) != 0)
27974 goto invalid_kind;
27975 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27976 break;
27978 case 'g':
27979 if (strcmp ("guided", p) != 0)
27980 goto invalid_kind;
27981 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27982 break;
27984 case 'r':
27985 if (strcmp ("runtime", p) != 0)
27986 goto invalid_kind;
27987 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27988 break;
27990 default:
27991 goto invalid_kind;
27994 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27995 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27996 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27997 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27998 else
27999 goto invalid_kind;
28000 cp_lexer_consume_token (parser->lexer);
28002 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28004 cp_token *token;
28005 cp_lexer_consume_token (parser->lexer);
28007 token = cp_lexer_peek_token (parser->lexer);
28008 t = cp_parser_assignment_expression (parser);
28010 if (t == error_mark_node)
28011 goto resync_fail;
28012 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28013 error_at (token->location, "schedule %<runtime%> does not take "
28014 "a %<chunk_size%> parameter");
28015 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28016 error_at (token->location, "schedule %<auto%> does not take "
28017 "a %<chunk_size%> parameter");
28018 else
28019 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28021 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28022 goto resync_fail;
28024 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28025 goto resync_fail;
28027 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28028 OMP_CLAUSE_CHAIN (c) = list;
28029 return c;
28031 invalid_kind:
28032 cp_parser_error (parser, "invalid schedule kind");
28033 resync_fail:
28034 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28035 /*or_comma=*/false,
28036 /*consume_paren=*/true);
28037 return list;
28040 /* OpenMP 3.0:
28041 untied */
28043 static tree
28044 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28045 tree list, location_t location)
28047 tree c;
28049 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28051 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28052 OMP_CLAUSE_CHAIN (c) = list;
28053 return c;
28056 /* OpenMP 4.0:
28057 inbranch
28058 notinbranch */
28060 static tree
28061 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28062 tree list, location_t location)
28064 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28065 tree c = build_omp_clause (location, code);
28066 OMP_CLAUSE_CHAIN (c) = list;
28067 return c;
28070 /* OpenMP 4.0:
28071 parallel
28073 sections
28074 taskgroup */
28076 static tree
28077 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28078 enum omp_clause_code code,
28079 tree list, location_t location)
28081 tree c = build_omp_clause (location, code);
28082 OMP_CLAUSE_CHAIN (c) = list;
28083 return c;
28086 /* OpenMP 4.0:
28087 num_teams ( expression ) */
28089 static tree
28090 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28091 location_t location)
28093 tree t, c;
28095 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28096 return list;
28098 t = cp_parser_expression (parser);
28100 if (t == error_mark_node
28101 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28102 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28103 /*or_comma=*/false,
28104 /*consume_paren=*/true);
28106 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28107 "num_teams", location);
28109 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28110 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28111 OMP_CLAUSE_CHAIN (c) = list;
28113 return c;
28116 /* OpenMP 4.0:
28117 thread_limit ( expression ) */
28119 static tree
28120 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28121 location_t location)
28123 tree t, c;
28125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28126 return list;
28128 t = cp_parser_expression (parser);
28130 if (t == error_mark_node
28131 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28132 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28133 /*or_comma=*/false,
28134 /*consume_paren=*/true);
28136 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28137 "thread_limit", location);
28139 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28140 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28141 OMP_CLAUSE_CHAIN (c) = list;
28143 return c;
28146 /* OpenMP 4.0:
28147 aligned ( variable-list )
28148 aligned ( variable-list : constant-expression ) */
28150 static tree
28151 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28153 tree nlist, c, alignment = NULL_TREE;
28154 bool colon;
28156 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28157 return list;
28159 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28160 &colon);
28162 if (colon)
28164 alignment = cp_parser_constant_expression (parser);
28166 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28167 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28168 /*or_comma=*/false,
28169 /*consume_paren=*/true);
28171 if (alignment == error_mark_node)
28172 alignment = NULL_TREE;
28175 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28176 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28178 return nlist;
28181 /* OpenMP 4.0:
28182 linear ( variable-list )
28183 linear ( variable-list : expression ) */
28185 static tree
28186 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28187 bool is_cilk_simd_fn)
28189 tree nlist, c, step = integer_one_node;
28190 bool colon;
28192 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28193 return list;
28195 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28196 &colon);
28198 if (colon)
28200 step = cp_parser_expression (parser);
28202 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28204 sorry ("using parameters for %<linear%> step is not supported yet");
28205 step = integer_one_node;
28207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28208 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28209 /*or_comma=*/false,
28210 /*consume_paren=*/true);
28212 if (step == error_mark_node)
28213 return list;
28216 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28217 OMP_CLAUSE_LINEAR_STEP (c) = step;
28219 return nlist;
28222 /* OpenMP 4.0:
28223 safelen ( constant-expression ) */
28225 static tree
28226 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28227 location_t location)
28229 tree t, c;
28231 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28232 return list;
28234 t = cp_parser_constant_expression (parser);
28236 if (t == error_mark_node
28237 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28238 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28239 /*or_comma=*/false,
28240 /*consume_paren=*/true);
28242 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28244 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28245 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28246 OMP_CLAUSE_CHAIN (c) = list;
28248 return c;
28251 /* OpenMP 4.0:
28252 simdlen ( constant-expression ) */
28254 static tree
28255 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28256 location_t location)
28258 tree t, c;
28260 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28261 return list;
28263 t = cp_parser_constant_expression (parser);
28265 if (t == error_mark_node
28266 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28267 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28268 /*or_comma=*/false,
28269 /*consume_paren=*/true);
28271 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28273 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28274 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28275 OMP_CLAUSE_CHAIN (c) = list;
28277 return c;
28280 /* OpenMP 4.0:
28281 depend ( depend-kind : variable-list )
28283 depend-kind:
28284 in | out | inout */
28286 static tree
28287 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28289 tree nlist, c;
28290 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28292 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28293 return list;
28295 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28297 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28298 const char *p = IDENTIFIER_POINTER (id);
28300 if (strcmp ("in", p) == 0)
28301 kind = OMP_CLAUSE_DEPEND_IN;
28302 else if (strcmp ("inout", p) == 0)
28303 kind = OMP_CLAUSE_DEPEND_INOUT;
28304 else if (strcmp ("out", p) == 0)
28305 kind = OMP_CLAUSE_DEPEND_OUT;
28306 else
28307 goto invalid_kind;
28309 else
28310 goto invalid_kind;
28312 cp_lexer_consume_token (parser->lexer);
28313 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28314 goto resync_fail;
28316 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28317 NULL);
28319 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28320 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28322 return nlist;
28324 invalid_kind:
28325 cp_parser_error (parser, "invalid depend kind");
28326 resync_fail:
28327 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28328 /*or_comma=*/false,
28329 /*consume_paren=*/true);
28330 return list;
28333 /* OpenMP 4.0:
28334 map ( map-kind : variable-list )
28335 map ( variable-list )
28337 map-kind:
28338 alloc | to | from | tofrom */
28340 static tree
28341 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28343 tree nlist, c;
28344 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28346 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28347 return list;
28349 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28350 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28353 const char *p = IDENTIFIER_POINTER (id);
28355 if (strcmp ("alloc", p) == 0)
28356 kind = OMP_CLAUSE_MAP_ALLOC;
28357 else if (strcmp ("to", p) == 0)
28358 kind = OMP_CLAUSE_MAP_TO;
28359 else if (strcmp ("from", p) == 0)
28360 kind = OMP_CLAUSE_MAP_FROM;
28361 else if (strcmp ("tofrom", p) == 0)
28362 kind = OMP_CLAUSE_MAP_TOFROM;
28363 else
28365 cp_parser_error (parser, "invalid map kind");
28366 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28367 /*or_comma=*/false,
28368 /*consume_paren=*/true);
28369 return list;
28371 cp_lexer_consume_token (parser->lexer);
28372 cp_lexer_consume_token (parser->lexer);
28375 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28376 NULL);
28378 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28379 OMP_CLAUSE_MAP_KIND (c) = kind;
28381 return nlist;
28384 /* OpenMP 4.0:
28385 device ( expression ) */
28387 static tree
28388 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28389 location_t location)
28391 tree t, c;
28393 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28394 return list;
28396 t = cp_parser_expression (parser);
28398 if (t == error_mark_node
28399 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28400 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28401 /*or_comma=*/false,
28402 /*consume_paren=*/true);
28404 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28405 "device", location);
28407 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28408 OMP_CLAUSE_DEVICE_ID (c) = t;
28409 OMP_CLAUSE_CHAIN (c) = list;
28411 return c;
28414 /* OpenMP 4.0:
28415 dist_schedule ( static )
28416 dist_schedule ( static , expression ) */
28418 static tree
28419 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28420 location_t location)
28422 tree c, t;
28424 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28425 return list;
28427 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28429 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28430 goto invalid_kind;
28431 cp_lexer_consume_token (parser->lexer);
28433 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28435 cp_lexer_consume_token (parser->lexer);
28437 t = cp_parser_assignment_expression (parser);
28439 if (t == error_mark_node)
28440 goto resync_fail;
28441 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28443 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28444 goto resync_fail;
28446 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28447 goto resync_fail;
28449 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28450 location);
28451 OMP_CLAUSE_CHAIN (c) = list;
28452 return c;
28454 invalid_kind:
28455 cp_parser_error (parser, "invalid dist_schedule kind");
28456 resync_fail:
28457 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28458 /*or_comma=*/false,
28459 /*consume_paren=*/true);
28460 return list;
28463 /* OpenMP 4.0:
28464 proc_bind ( proc-bind-kind )
28466 proc-bind-kind:
28467 master | close | spread */
28469 static tree
28470 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28471 location_t location)
28473 tree c;
28474 enum omp_clause_proc_bind_kind kind;
28476 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28477 return list;
28479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28481 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28482 const char *p = IDENTIFIER_POINTER (id);
28484 if (strcmp ("master", p) == 0)
28485 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28486 else if (strcmp ("close", p) == 0)
28487 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28488 else if (strcmp ("spread", p) == 0)
28489 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28490 else
28491 goto invalid_kind;
28493 else
28494 goto invalid_kind;
28496 cp_lexer_consume_token (parser->lexer);
28497 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28498 goto resync_fail;
28500 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28501 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28502 location);
28503 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28504 OMP_CLAUSE_CHAIN (c) = list;
28505 return c;
28507 invalid_kind:
28508 cp_parser_error (parser, "invalid depend kind");
28509 resync_fail:
28510 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28511 /*or_comma=*/false,
28512 /*consume_paren=*/true);
28513 return list;
28516 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28517 is a bitmask in MASK. Return the list of clauses found; the result
28518 of clause default goes in *pdefault. */
28520 static tree
28521 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28522 const char *where, cp_token *pragma_tok,
28523 bool finish_p = true)
28525 tree clauses = NULL;
28526 bool first = true;
28527 cp_token *token = NULL;
28528 bool cilk_simd_fn = false;
28530 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28532 pragma_omp_clause c_kind;
28533 const char *c_name;
28534 tree prev = clauses;
28536 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28537 cp_lexer_consume_token (parser->lexer);
28539 token = cp_lexer_peek_token (parser->lexer);
28540 c_kind = cp_parser_omp_clause_name (parser);
28542 switch (c_kind)
28544 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28545 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28546 token->location);
28547 c_name = "collapse";
28548 break;
28549 case PRAGMA_OMP_CLAUSE_COPYIN:
28550 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28551 c_name = "copyin";
28552 break;
28553 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28554 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28555 clauses);
28556 c_name = "copyprivate";
28557 break;
28558 case PRAGMA_OMP_CLAUSE_DEFAULT:
28559 clauses = cp_parser_omp_clause_default (parser, clauses,
28560 token->location);
28561 c_name = "default";
28562 break;
28563 case PRAGMA_OMP_CLAUSE_FINAL:
28564 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28565 c_name = "final";
28566 break;
28567 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28568 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28569 clauses);
28570 c_name = "firstprivate";
28571 break;
28572 case PRAGMA_OMP_CLAUSE_IF:
28573 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28574 c_name = "if";
28575 break;
28576 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28577 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28578 clauses);
28579 c_name = "lastprivate";
28580 break;
28581 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28582 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28583 token->location);
28584 c_name = "mergeable";
28585 break;
28586 case PRAGMA_OMP_CLAUSE_NOWAIT:
28587 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28588 c_name = "nowait";
28589 break;
28590 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28591 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28592 token->location);
28593 c_name = "num_threads";
28594 break;
28595 case PRAGMA_OMP_CLAUSE_ORDERED:
28596 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28597 token->location);
28598 c_name = "ordered";
28599 break;
28600 case PRAGMA_OMP_CLAUSE_PRIVATE:
28601 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28602 clauses);
28603 c_name = "private";
28604 break;
28605 case PRAGMA_OMP_CLAUSE_REDUCTION:
28606 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28607 c_name = "reduction";
28608 break;
28609 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28610 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28611 token->location);
28612 c_name = "schedule";
28613 break;
28614 case PRAGMA_OMP_CLAUSE_SHARED:
28615 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28616 clauses);
28617 c_name = "shared";
28618 break;
28619 case PRAGMA_OMP_CLAUSE_UNTIED:
28620 clauses = cp_parser_omp_clause_untied (parser, clauses,
28621 token->location);
28622 c_name = "untied";
28623 break;
28624 case PRAGMA_OMP_CLAUSE_INBRANCH:
28625 case PRAGMA_CILK_CLAUSE_MASK:
28626 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28627 clauses, token->location);
28628 c_name = "inbranch";
28629 break;
28630 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28631 case PRAGMA_CILK_CLAUSE_NOMASK:
28632 clauses = cp_parser_omp_clause_branch (parser,
28633 OMP_CLAUSE_NOTINBRANCH,
28634 clauses, token->location);
28635 c_name = "notinbranch";
28636 break;
28637 case PRAGMA_OMP_CLAUSE_PARALLEL:
28638 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28639 clauses, token->location);
28640 c_name = "parallel";
28641 if (!first)
28643 clause_not_first:
28644 error_at (token->location, "%qs must be the first clause of %qs",
28645 c_name, where);
28646 clauses = prev;
28648 break;
28649 case PRAGMA_OMP_CLAUSE_FOR:
28650 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28651 clauses, token->location);
28652 c_name = "for";
28653 if (!first)
28654 goto clause_not_first;
28655 break;
28656 case PRAGMA_OMP_CLAUSE_SECTIONS:
28657 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28658 clauses, token->location);
28659 c_name = "sections";
28660 if (!first)
28661 goto clause_not_first;
28662 break;
28663 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28664 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28665 clauses, token->location);
28666 c_name = "taskgroup";
28667 if (!first)
28668 goto clause_not_first;
28669 break;
28670 case PRAGMA_OMP_CLAUSE_TO:
28671 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28672 clauses);
28673 c_name = "to";
28674 break;
28675 case PRAGMA_OMP_CLAUSE_FROM:
28676 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28677 clauses);
28678 c_name = "from";
28679 break;
28680 case PRAGMA_OMP_CLAUSE_UNIFORM:
28681 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28682 clauses);
28683 c_name = "uniform";
28684 break;
28685 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28686 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28687 token->location);
28688 c_name = "num_teams";
28689 break;
28690 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28691 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28692 token->location);
28693 c_name = "thread_limit";
28694 break;
28695 case PRAGMA_OMP_CLAUSE_ALIGNED:
28696 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28697 c_name = "aligned";
28698 break;
28699 case PRAGMA_OMP_CLAUSE_LINEAR:
28700 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28701 cilk_simd_fn = true;
28702 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28703 c_name = "linear";
28704 break;
28705 case PRAGMA_OMP_CLAUSE_DEPEND:
28706 clauses = cp_parser_omp_clause_depend (parser, clauses);
28707 c_name = "depend";
28708 break;
28709 case PRAGMA_OMP_CLAUSE_MAP:
28710 clauses = cp_parser_omp_clause_map (parser, clauses);
28711 c_name = "map";
28712 break;
28713 case PRAGMA_OMP_CLAUSE_DEVICE:
28714 clauses = cp_parser_omp_clause_device (parser, clauses,
28715 token->location);
28716 c_name = "device";
28717 break;
28718 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28719 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28720 token->location);
28721 c_name = "dist_schedule";
28722 break;
28723 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28724 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28725 token->location);
28726 c_name = "proc_bind";
28727 break;
28728 case PRAGMA_OMP_CLAUSE_SAFELEN:
28729 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28730 token->location);
28731 c_name = "safelen";
28732 break;
28733 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28734 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28735 token->location);
28736 c_name = "simdlen";
28737 break;
28738 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28739 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28740 c_name = "simdlen";
28741 break;
28742 default:
28743 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28744 goto saw_error;
28747 first = false;
28749 if (((mask >> c_kind) & 1) == 0)
28751 /* Remove the invalid clause(s) from the list to avoid
28752 confusing the rest of the compiler. */
28753 clauses = prev;
28754 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28757 saw_error:
28758 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28759 no reason to skip to the end. */
28760 if (!(flag_cilkplus && pragma_tok == NULL))
28761 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28762 if (finish_p)
28763 return finish_omp_clauses (clauses);
28764 return clauses;
28767 /* OpenMP 2.5:
28768 structured-block:
28769 statement
28771 In practice, we're also interested in adding the statement to an
28772 outer node. So it is convenient if we work around the fact that
28773 cp_parser_statement calls add_stmt. */
28775 static unsigned
28776 cp_parser_begin_omp_structured_block (cp_parser *parser)
28778 unsigned save = parser->in_statement;
28780 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28781 This preserves the "not within loop or switch" style error messages
28782 for nonsense cases like
28783 void foo() {
28784 #pragma omp single
28785 break;
28788 if (parser->in_statement)
28789 parser->in_statement = IN_OMP_BLOCK;
28791 return save;
28794 static void
28795 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28797 parser->in_statement = save;
28800 static tree
28801 cp_parser_omp_structured_block (cp_parser *parser)
28803 tree stmt = begin_omp_structured_block ();
28804 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28806 cp_parser_statement (parser, NULL_TREE, false, NULL);
28808 cp_parser_end_omp_structured_block (parser, save);
28809 return finish_omp_structured_block (stmt);
28812 /* OpenMP 2.5:
28813 # pragma omp atomic new-line
28814 expression-stmt
28816 expression-stmt:
28817 x binop= expr | x++ | ++x | x-- | --x
28818 binop:
28819 +, *, -, /, &, ^, |, <<, >>
28821 where x is an lvalue expression with scalar type.
28823 OpenMP 3.1:
28824 # pragma omp atomic new-line
28825 update-stmt
28827 # pragma omp atomic read new-line
28828 read-stmt
28830 # pragma omp atomic write new-line
28831 write-stmt
28833 # pragma omp atomic update new-line
28834 update-stmt
28836 # pragma omp atomic capture new-line
28837 capture-stmt
28839 # pragma omp atomic capture new-line
28840 capture-block
28842 read-stmt:
28843 v = x
28844 write-stmt:
28845 x = expr
28846 update-stmt:
28847 expression-stmt | x = x binop expr
28848 capture-stmt:
28849 v = expression-stmt
28850 capture-block:
28851 { v = x; update-stmt; } | { update-stmt; v = x; }
28853 OpenMP 4.0:
28854 update-stmt:
28855 expression-stmt | x = x binop expr | x = expr binop x
28856 capture-stmt:
28857 v = update-stmt
28858 capture-block:
28859 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28861 where x and v are lvalue expressions with scalar type. */
28863 static void
28864 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28866 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28867 tree rhs1 = NULL_TREE, orig_lhs;
28868 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28869 bool structured_block = false;
28870 bool seq_cst = false;
28872 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28874 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28875 const char *p = IDENTIFIER_POINTER (id);
28877 if (!strcmp (p, "seq_cst"))
28879 seq_cst = true;
28880 cp_lexer_consume_token (parser->lexer);
28881 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28882 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28883 cp_lexer_consume_token (parser->lexer);
28886 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28888 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28889 const char *p = IDENTIFIER_POINTER (id);
28891 if (!strcmp (p, "read"))
28892 code = OMP_ATOMIC_READ;
28893 else if (!strcmp (p, "write"))
28894 code = NOP_EXPR;
28895 else if (!strcmp (p, "update"))
28896 code = OMP_ATOMIC;
28897 else if (!strcmp (p, "capture"))
28898 code = OMP_ATOMIC_CAPTURE_NEW;
28899 else
28900 p = NULL;
28901 if (p)
28902 cp_lexer_consume_token (parser->lexer);
28904 if (!seq_cst)
28906 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28907 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28908 cp_lexer_consume_token (parser->lexer);
28910 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28912 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28913 const char *p = IDENTIFIER_POINTER (id);
28915 if (!strcmp (p, "seq_cst"))
28917 seq_cst = true;
28918 cp_lexer_consume_token (parser->lexer);
28922 cp_parser_require_pragma_eol (parser, pragma_tok);
28924 switch (code)
28926 case OMP_ATOMIC_READ:
28927 case NOP_EXPR: /* atomic write */
28928 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28929 /*cast_p=*/false, NULL);
28930 if (v == error_mark_node)
28931 goto saw_error;
28932 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28933 goto saw_error;
28934 if (code == NOP_EXPR)
28935 lhs = cp_parser_expression (parser);
28936 else
28937 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28938 /*cast_p=*/false, NULL);
28939 if (lhs == error_mark_node)
28940 goto saw_error;
28941 if (code == NOP_EXPR)
28943 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28944 opcode. */
28945 code = OMP_ATOMIC;
28946 rhs = lhs;
28947 lhs = v;
28948 v = NULL_TREE;
28950 goto done;
28951 case OMP_ATOMIC_CAPTURE_NEW:
28952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28954 cp_lexer_consume_token (parser->lexer);
28955 structured_block = true;
28957 else
28959 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28960 /*cast_p=*/false, NULL);
28961 if (v == error_mark_node)
28962 goto saw_error;
28963 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28964 goto saw_error;
28966 default:
28967 break;
28970 restart:
28971 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28972 /*cast_p=*/false, NULL);
28973 orig_lhs = lhs;
28974 switch (TREE_CODE (lhs))
28976 case ERROR_MARK:
28977 goto saw_error;
28979 case POSTINCREMENT_EXPR:
28980 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28981 code = OMP_ATOMIC_CAPTURE_OLD;
28982 /* FALLTHROUGH */
28983 case PREINCREMENT_EXPR:
28984 lhs = TREE_OPERAND (lhs, 0);
28985 opcode = PLUS_EXPR;
28986 rhs = integer_one_node;
28987 break;
28989 case POSTDECREMENT_EXPR:
28990 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28991 code = OMP_ATOMIC_CAPTURE_OLD;
28992 /* FALLTHROUGH */
28993 case PREDECREMENT_EXPR:
28994 lhs = TREE_OPERAND (lhs, 0);
28995 opcode = MINUS_EXPR;
28996 rhs = integer_one_node;
28997 break;
28999 case COMPOUND_EXPR:
29000 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29001 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29002 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29003 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29004 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29005 (TREE_OPERAND (lhs, 1), 0), 0)))
29006 == BOOLEAN_TYPE)
29007 /* Undo effects of boolean_increment for post {in,de}crement. */
29008 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29009 /* FALLTHRU */
29010 case MODIFY_EXPR:
29011 if (TREE_CODE (lhs) == MODIFY_EXPR
29012 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29014 /* Undo effects of boolean_increment. */
29015 if (integer_onep (TREE_OPERAND (lhs, 1)))
29017 /* This is pre or post increment. */
29018 rhs = TREE_OPERAND (lhs, 1);
29019 lhs = TREE_OPERAND (lhs, 0);
29020 opcode = NOP_EXPR;
29021 if (code == OMP_ATOMIC_CAPTURE_NEW
29022 && !structured_block
29023 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29024 code = OMP_ATOMIC_CAPTURE_OLD;
29025 break;
29028 /* FALLTHRU */
29029 default:
29030 switch (cp_lexer_peek_token (parser->lexer)->type)
29032 case CPP_MULT_EQ:
29033 opcode = MULT_EXPR;
29034 break;
29035 case CPP_DIV_EQ:
29036 opcode = TRUNC_DIV_EXPR;
29037 break;
29038 case CPP_PLUS_EQ:
29039 opcode = PLUS_EXPR;
29040 break;
29041 case CPP_MINUS_EQ:
29042 opcode = MINUS_EXPR;
29043 break;
29044 case CPP_LSHIFT_EQ:
29045 opcode = LSHIFT_EXPR;
29046 break;
29047 case CPP_RSHIFT_EQ:
29048 opcode = RSHIFT_EXPR;
29049 break;
29050 case CPP_AND_EQ:
29051 opcode = BIT_AND_EXPR;
29052 break;
29053 case CPP_OR_EQ:
29054 opcode = BIT_IOR_EXPR;
29055 break;
29056 case CPP_XOR_EQ:
29057 opcode = BIT_XOR_EXPR;
29058 break;
29059 case CPP_EQ:
29060 enum cp_parser_prec oprec;
29061 cp_token *token;
29062 cp_lexer_consume_token (parser->lexer);
29063 cp_parser_parse_tentatively (parser);
29064 rhs1 = cp_parser_simple_cast_expression (parser);
29065 if (rhs1 == error_mark_node)
29067 cp_parser_abort_tentative_parse (parser);
29068 cp_parser_simple_cast_expression (parser);
29069 goto saw_error;
29071 token = cp_lexer_peek_token (parser->lexer);
29072 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29074 cp_parser_abort_tentative_parse (parser);
29075 cp_parser_parse_tentatively (parser);
29076 rhs = cp_parser_binary_expression (parser, false, true,
29077 PREC_NOT_OPERATOR, NULL);
29078 if (rhs == error_mark_node)
29080 cp_parser_abort_tentative_parse (parser);
29081 cp_parser_binary_expression (parser, false, true,
29082 PREC_NOT_OPERATOR, NULL);
29083 goto saw_error;
29085 switch (TREE_CODE (rhs))
29087 case MULT_EXPR:
29088 case TRUNC_DIV_EXPR:
29089 case PLUS_EXPR:
29090 case MINUS_EXPR:
29091 case LSHIFT_EXPR:
29092 case RSHIFT_EXPR:
29093 case BIT_AND_EXPR:
29094 case BIT_IOR_EXPR:
29095 case BIT_XOR_EXPR:
29096 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29098 if (cp_parser_parse_definitely (parser))
29100 opcode = TREE_CODE (rhs);
29101 rhs1 = TREE_OPERAND (rhs, 0);
29102 rhs = TREE_OPERAND (rhs, 1);
29103 goto stmt_done;
29105 else
29106 goto saw_error;
29108 break;
29109 default:
29110 break;
29112 cp_parser_abort_tentative_parse (parser);
29113 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29115 rhs = cp_parser_expression (parser);
29116 if (rhs == error_mark_node)
29117 goto saw_error;
29118 opcode = NOP_EXPR;
29119 rhs1 = NULL_TREE;
29120 goto stmt_done;
29122 cp_parser_error (parser,
29123 "invalid form of %<#pragma omp atomic%>");
29124 goto saw_error;
29126 if (!cp_parser_parse_definitely (parser))
29127 goto saw_error;
29128 switch (token->type)
29130 case CPP_SEMICOLON:
29131 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29133 code = OMP_ATOMIC_CAPTURE_OLD;
29134 v = lhs;
29135 lhs = NULL_TREE;
29136 lhs1 = rhs1;
29137 rhs1 = NULL_TREE;
29138 cp_lexer_consume_token (parser->lexer);
29139 goto restart;
29141 else if (structured_block)
29143 opcode = NOP_EXPR;
29144 rhs = rhs1;
29145 rhs1 = NULL_TREE;
29146 goto stmt_done;
29148 cp_parser_error (parser,
29149 "invalid form of %<#pragma omp atomic%>");
29150 goto saw_error;
29151 case CPP_MULT:
29152 opcode = MULT_EXPR;
29153 break;
29154 case CPP_DIV:
29155 opcode = TRUNC_DIV_EXPR;
29156 break;
29157 case CPP_PLUS:
29158 opcode = PLUS_EXPR;
29159 break;
29160 case CPP_MINUS:
29161 opcode = MINUS_EXPR;
29162 break;
29163 case CPP_LSHIFT:
29164 opcode = LSHIFT_EXPR;
29165 break;
29166 case CPP_RSHIFT:
29167 opcode = RSHIFT_EXPR;
29168 break;
29169 case CPP_AND:
29170 opcode = BIT_AND_EXPR;
29171 break;
29172 case CPP_OR:
29173 opcode = BIT_IOR_EXPR;
29174 break;
29175 case CPP_XOR:
29176 opcode = BIT_XOR_EXPR;
29177 break;
29178 default:
29179 cp_parser_error (parser,
29180 "invalid operator for %<#pragma omp atomic%>");
29181 goto saw_error;
29183 oprec = TOKEN_PRECEDENCE (token);
29184 gcc_assert (oprec != PREC_NOT_OPERATOR);
29185 if (commutative_tree_code (opcode))
29186 oprec = (enum cp_parser_prec) (oprec - 1);
29187 cp_lexer_consume_token (parser->lexer);
29188 rhs = cp_parser_binary_expression (parser, false, false,
29189 oprec, NULL);
29190 if (rhs == error_mark_node)
29191 goto saw_error;
29192 goto stmt_done;
29193 /* FALLTHROUGH */
29194 default:
29195 cp_parser_error (parser,
29196 "invalid operator for %<#pragma omp atomic%>");
29197 goto saw_error;
29199 cp_lexer_consume_token (parser->lexer);
29201 rhs = cp_parser_expression (parser);
29202 if (rhs == error_mark_node)
29203 goto saw_error;
29204 break;
29206 stmt_done:
29207 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29209 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29210 goto saw_error;
29211 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29212 /*cast_p=*/false, NULL);
29213 if (v == error_mark_node)
29214 goto saw_error;
29215 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29216 goto saw_error;
29217 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29218 /*cast_p=*/false, NULL);
29219 if (lhs1 == error_mark_node)
29220 goto saw_error;
29222 if (structured_block)
29224 cp_parser_consume_semicolon_at_end_of_statement (parser);
29225 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29227 done:
29228 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29229 if (!structured_block)
29230 cp_parser_consume_semicolon_at_end_of_statement (parser);
29231 return;
29233 saw_error:
29234 cp_parser_skip_to_end_of_block_or_statement (parser);
29235 if (structured_block)
29237 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29238 cp_lexer_consume_token (parser->lexer);
29239 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29241 cp_parser_skip_to_end_of_block_or_statement (parser);
29242 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29243 cp_lexer_consume_token (parser->lexer);
29249 /* OpenMP 2.5:
29250 # pragma omp barrier new-line */
29252 static void
29253 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29255 cp_parser_require_pragma_eol (parser, pragma_tok);
29256 finish_omp_barrier ();
29259 /* OpenMP 2.5:
29260 # pragma omp critical [(name)] new-line
29261 structured-block */
29263 static tree
29264 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29266 tree stmt, name = NULL;
29268 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29270 cp_lexer_consume_token (parser->lexer);
29272 name = cp_parser_identifier (parser);
29274 if (name == error_mark_node
29275 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29277 /*or_comma=*/false,
29278 /*consume_paren=*/true);
29279 if (name == error_mark_node)
29280 name = NULL;
29282 cp_parser_require_pragma_eol (parser, pragma_tok);
29284 stmt = cp_parser_omp_structured_block (parser);
29285 return c_finish_omp_critical (input_location, stmt, name);
29288 /* OpenMP 2.5:
29289 # pragma omp flush flush-vars[opt] new-line
29291 flush-vars:
29292 ( variable-list ) */
29294 static void
29295 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29297 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29298 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29299 cp_parser_require_pragma_eol (parser, pragma_tok);
29301 finish_omp_flush ();
29304 /* Helper function, to parse omp for increment expression. */
29306 static tree
29307 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29309 tree cond = cp_parser_binary_expression (parser, false, true,
29310 PREC_NOT_OPERATOR, NULL);
29311 if (cond == error_mark_node
29312 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29314 cp_parser_skip_to_end_of_statement (parser);
29315 return error_mark_node;
29318 switch (TREE_CODE (cond))
29320 case GT_EXPR:
29321 case GE_EXPR:
29322 case LT_EXPR:
29323 case LE_EXPR:
29324 break;
29325 case NE_EXPR:
29326 if (code == CILK_SIMD || code == CILK_FOR)
29327 break;
29328 /* Fall through: OpenMP disallows NE_EXPR. */
29329 default:
29330 return error_mark_node;
29333 /* If decl is an iterator, preserve LHS and RHS of the relational
29334 expr until finish_omp_for. */
29335 if (decl
29336 && (type_dependent_expression_p (decl)
29337 || CLASS_TYPE_P (TREE_TYPE (decl))))
29338 return cond;
29340 return build_x_binary_op (input_location, TREE_CODE (cond),
29341 TREE_OPERAND (cond, 0), ERROR_MARK,
29342 TREE_OPERAND (cond, 1), ERROR_MARK,
29343 /*overload=*/NULL, tf_warning_or_error);
29346 /* Helper function, to parse omp for increment expression. */
29348 static tree
29349 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29351 cp_token *token = cp_lexer_peek_token (parser->lexer);
29352 enum tree_code op;
29353 tree lhs, rhs;
29354 cp_id_kind idk;
29355 bool decl_first;
29357 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29359 op = (token->type == CPP_PLUS_PLUS
29360 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29361 cp_lexer_consume_token (parser->lexer);
29362 lhs = cp_parser_simple_cast_expression (parser);
29363 if (lhs != decl)
29364 return error_mark_node;
29365 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29368 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29369 if (lhs != decl)
29370 return error_mark_node;
29372 token = cp_lexer_peek_token (parser->lexer);
29373 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29375 op = (token->type == CPP_PLUS_PLUS
29376 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29377 cp_lexer_consume_token (parser->lexer);
29378 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29381 op = cp_parser_assignment_operator_opt (parser);
29382 if (op == ERROR_MARK)
29383 return error_mark_node;
29385 if (op != NOP_EXPR)
29387 rhs = cp_parser_assignment_expression (parser);
29388 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29389 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29392 lhs = cp_parser_binary_expression (parser, false, false,
29393 PREC_ADDITIVE_EXPRESSION, NULL);
29394 token = cp_lexer_peek_token (parser->lexer);
29395 decl_first = lhs == decl;
29396 if (decl_first)
29397 lhs = NULL_TREE;
29398 if (token->type != CPP_PLUS
29399 && token->type != CPP_MINUS)
29400 return error_mark_node;
29404 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29405 cp_lexer_consume_token (parser->lexer);
29406 rhs = cp_parser_binary_expression (parser, false, false,
29407 PREC_ADDITIVE_EXPRESSION, NULL);
29408 token = cp_lexer_peek_token (parser->lexer);
29409 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29411 if (lhs == NULL_TREE)
29413 if (op == PLUS_EXPR)
29414 lhs = rhs;
29415 else
29416 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29417 tf_warning_or_error);
29419 else
29420 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29421 ERROR_MARK, NULL, tf_warning_or_error);
29424 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29426 if (!decl_first)
29428 if (rhs != decl || op == MINUS_EXPR)
29429 return error_mark_node;
29430 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29432 else
29433 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29435 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29438 /* Parse the initialization statement of either an OpenMP for loop or
29439 a Cilk Plus for loop.
29441 Return true if the resulting construct should have an
29442 OMP_CLAUSE_PRIVATE added to it. */
29444 static bool
29445 cp_parser_omp_for_loop_init (cp_parser *parser,
29446 enum tree_code code,
29447 tree &this_pre_body,
29448 vec<tree, va_gc> *for_block,
29449 tree &init,
29450 tree &decl,
29451 tree &real_decl)
29453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29454 return false;
29456 bool add_private_clause = false;
29458 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29460 init-expr:
29461 var = lb
29462 integer-type var = lb
29463 random-access-iterator-type var = lb
29464 pointer-type var = lb
29466 cp_decl_specifier_seq type_specifiers;
29468 /* First, try to parse as an initialized declaration. See
29469 cp_parser_condition, from whence the bulk of this is copied. */
29471 cp_parser_parse_tentatively (parser);
29472 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29473 /*is_trailing_return=*/false,
29474 &type_specifiers);
29475 if (cp_parser_parse_definitely (parser))
29477 /* If parsing a type specifier seq succeeded, then this
29478 MUST be a initialized declaration. */
29479 tree asm_specification, attributes;
29480 cp_declarator *declarator;
29482 declarator = cp_parser_declarator (parser,
29483 CP_PARSER_DECLARATOR_NAMED,
29484 /*ctor_dtor_or_conv_p=*/NULL,
29485 /*parenthesized_p=*/NULL,
29486 /*member_p=*/false,
29487 /*friend_p=*/false);
29488 attributes = cp_parser_attributes_opt (parser);
29489 asm_specification = cp_parser_asm_specification_opt (parser);
29491 if (declarator == cp_error_declarator)
29492 cp_parser_skip_to_end_of_statement (parser);
29494 else
29496 tree pushed_scope, auto_node;
29498 decl = start_decl (declarator, &type_specifiers,
29499 SD_INITIALIZED, attributes,
29500 /*prefix_attributes=*/NULL_TREE,
29501 &pushed_scope);
29503 auto_node = type_uses_auto (TREE_TYPE (decl));
29504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29506 if (cp_lexer_next_token_is (parser->lexer,
29507 CPP_OPEN_PAREN))
29509 if (code != CILK_SIMD && code != CILK_FOR)
29510 error ("parenthesized initialization is not allowed in "
29511 "OpenMP %<for%> loop");
29512 else
29513 error ("parenthesized initialization is "
29514 "not allowed in for-loop");
29516 else
29517 /* Trigger an error. */
29518 cp_parser_require (parser, CPP_EQ, RT_EQ);
29520 init = error_mark_node;
29521 cp_parser_skip_to_end_of_statement (parser);
29523 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29524 || type_dependent_expression_p (decl)
29525 || auto_node)
29527 bool is_direct_init, is_non_constant_init;
29529 init = cp_parser_initializer (parser,
29530 &is_direct_init,
29531 &is_non_constant_init);
29533 if (auto_node)
29535 TREE_TYPE (decl)
29536 = do_auto_deduction (TREE_TYPE (decl), init,
29537 auto_node);
29539 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29540 && !type_dependent_expression_p (decl))
29541 goto non_class;
29544 cp_finish_decl (decl, init, !is_non_constant_init,
29545 asm_specification,
29546 LOOKUP_ONLYCONVERTING);
29547 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29549 vec_safe_push (for_block, this_pre_body);
29550 init = NULL_TREE;
29552 else
29553 init = pop_stmt_list (this_pre_body);
29554 this_pre_body = NULL_TREE;
29556 else
29558 /* Consume '='. */
29559 cp_lexer_consume_token (parser->lexer);
29560 init = cp_parser_assignment_expression (parser);
29562 non_class:
29563 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29564 init = error_mark_node;
29565 else
29566 cp_finish_decl (decl, NULL_TREE,
29567 /*init_const_expr_p=*/false,
29568 asm_specification,
29569 LOOKUP_ONLYCONVERTING);
29572 if (pushed_scope)
29573 pop_scope (pushed_scope);
29576 else
29578 cp_id_kind idk;
29579 /* If parsing a type specifier sequence failed, then
29580 this MUST be a simple expression. */
29581 if (code == CILK_FOR)
29582 error ("%<_Cilk_for%> allows expression instead of declaration only "
29583 "in C, not in C++");
29584 cp_parser_parse_tentatively (parser);
29585 decl = cp_parser_primary_expression (parser, false, false,
29586 false, &idk);
29587 if (!cp_parser_error_occurred (parser)
29588 && decl
29589 && DECL_P (decl)
29590 && CLASS_TYPE_P (TREE_TYPE (decl)))
29592 tree rhs;
29594 cp_parser_parse_definitely (parser);
29595 cp_parser_require (parser, CPP_EQ, RT_EQ);
29596 rhs = cp_parser_assignment_expression (parser);
29597 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29598 decl, NOP_EXPR,
29599 rhs,
29600 tf_warning_or_error));
29601 add_private_clause = true;
29603 else
29605 decl = NULL;
29606 cp_parser_abort_tentative_parse (parser);
29607 init = cp_parser_expression (parser);
29608 if (init)
29610 if (TREE_CODE (init) == MODIFY_EXPR
29611 || TREE_CODE (init) == MODOP_EXPR)
29612 real_decl = TREE_OPERAND (init, 0);
29616 return add_private_clause;
29619 /* Parse the restricted form of the for statement allowed by OpenMP. */
29621 static tree
29622 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29623 tree *cclauses)
29625 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29626 tree real_decl, initv, condv, incrv, declv;
29627 tree this_pre_body, cl;
29628 location_t loc_first;
29629 bool collapse_err = false;
29630 int i, collapse = 1, nbraces = 0;
29631 vec<tree, va_gc> *for_block = make_tree_vector ();
29633 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29634 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29635 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29637 gcc_assert (collapse >= 1);
29639 declv = make_tree_vec (collapse);
29640 initv = make_tree_vec (collapse);
29641 condv = make_tree_vec (collapse);
29642 incrv = make_tree_vec (collapse);
29644 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29646 for (i = 0; i < collapse; i++)
29648 int bracecount = 0;
29649 bool add_private_clause = false;
29650 location_t loc;
29652 if (code != CILK_FOR
29653 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29655 cp_parser_error (parser, "for statement expected");
29656 return NULL;
29658 if (code == CILK_FOR
29659 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29661 cp_parser_error (parser, "_Cilk_for statement expected");
29662 return NULL;
29664 loc = cp_lexer_consume_token (parser->lexer)->location;
29666 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29667 return NULL;
29669 init = decl = real_decl = NULL;
29670 this_pre_body = push_stmt_list ();
29672 add_private_clause
29673 |= cp_parser_omp_for_loop_init (parser, code,
29674 this_pre_body, for_block,
29675 init, decl, real_decl);
29677 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29678 if (this_pre_body)
29680 this_pre_body = pop_stmt_list (this_pre_body);
29681 if (pre_body)
29683 tree t = pre_body;
29684 pre_body = push_stmt_list ();
29685 add_stmt (t);
29686 add_stmt (this_pre_body);
29687 pre_body = pop_stmt_list (pre_body);
29689 else
29690 pre_body = this_pre_body;
29693 if (decl)
29694 real_decl = decl;
29695 if (cclauses != NULL
29696 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29697 && real_decl != NULL_TREE)
29699 tree *c;
29700 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29701 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29702 && OMP_CLAUSE_DECL (*c) == real_decl)
29704 error_at (loc, "iteration variable %qD"
29705 " should not be firstprivate", real_decl);
29706 *c = OMP_CLAUSE_CHAIN (*c);
29708 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29709 && OMP_CLAUSE_DECL (*c) == real_decl)
29711 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29712 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29713 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29714 OMP_CLAUSE_DECL (l) = real_decl;
29715 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29716 if (code == OMP_SIMD)
29718 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29719 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29721 else
29723 OMP_CLAUSE_CHAIN (l) = clauses;
29724 clauses = l;
29726 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29727 CP_OMP_CLAUSE_INFO (*c) = NULL;
29728 add_private_clause = false;
29730 else
29732 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29733 && OMP_CLAUSE_DECL (*c) == real_decl)
29734 add_private_clause = false;
29735 c = &OMP_CLAUSE_CHAIN (*c);
29739 if (add_private_clause)
29741 tree c;
29742 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29744 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29745 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29746 && OMP_CLAUSE_DECL (c) == decl)
29747 break;
29748 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29749 && OMP_CLAUSE_DECL (c) == decl)
29750 error_at (loc, "iteration variable %qD "
29751 "should not be firstprivate",
29752 decl);
29753 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29754 && OMP_CLAUSE_DECL (c) == decl)
29755 error_at (loc, "iteration variable %qD should not be reduction",
29756 decl);
29758 if (c == NULL)
29760 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29761 OMP_CLAUSE_DECL (c) = decl;
29762 c = finish_omp_clauses (c);
29763 if (c)
29765 OMP_CLAUSE_CHAIN (c) = clauses;
29766 clauses = c;
29771 cond = NULL;
29772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29773 cond = cp_parser_omp_for_cond (parser, decl, code);
29774 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29776 incr = NULL;
29777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29779 /* If decl is an iterator, preserve the operator on decl
29780 until finish_omp_for. */
29781 if (real_decl
29782 && ((processing_template_decl
29783 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29784 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29785 incr = cp_parser_omp_for_incr (parser, real_decl);
29786 else
29787 incr = cp_parser_expression (parser);
29788 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29789 SET_EXPR_LOCATION (incr, input_location);
29792 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29793 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29794 /*or_comma=*/false,
29795 /*consume_paren=*/true);
29797 TREE_VEC_ELT (declv, i) = decl;
29798 TREE_VEC_ELT (initv, i) = init;
29799 TREE_VEC_ELT (condv, i) = cond;
29800 TREE_VEC_ELT (incrv, i) = incr;
29802 if (i == collapse - 1)
29803 break;
29805 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29806 in between the collapsed for loops to be still considered perfectly
29807 nested. Hopefully the final version clarifies this.
29808 For now handle (multiple) {'s and empty statements. */
29809 cp_parser_parse_tentatively (parser);
29812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29813 break;
29814 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29816 cp_lexer_consume_token (parser->lexer);
29817 bracecount++;
29819 else if (bracecount
29820 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29821 cp_lexer_consume_token (parser->lexer);
29822 else
29824 loc = cp_lexer_peek_token (parser->lexer)->location;
29825 error_at (loc, "not enough collapsed for loops");
29826 collapse_err = true;
29827 cp_parser_abort_tentative_parse (parser);
29828 declv = NULL_TREE;
29829 break;
29832 while (1);
29834 if (declv)
29836 cp_parser_parse_definitely (parser);
29837 nbraces += bracecount;
29841 /* Note that we saved the original contents of this flag when we entered
29842 the structured block, and so we don't need to re-save it here. */
29843 if (code == CILK_SIMD || code == CILK_FOR)
29844 parser->in_statement = IN_CILK_SIMD_FOR;
29845 else
29846 parser->in_statement = IN_OMP_FOR;
29848 /* Note that the grammar doesn't call for a structured block here,
29849 though the loop as a whole is a structured block. */
29850 body = push_stmt_list ();
29851 cp_parser_statement (parser, NULL_TREE, false, NULL);
29852 body = pop_stmt_list (body);
29854 if (declv == NULL_TREE)
29855 ret = NULL_TREE;
29856 else
29857 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29858 pre_body, clauses);
29860 while (nbraces)
29862 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29864 cp_lexer_consume_token (parser->lexer);
29865 nbraces--;
29867 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29868 cp_lexer_consume_token (parser->lexer);
29869 else
29871 if (!collapse_err)
29873 error_at (cp_lexer_peek_token (parser->lexer)->location,
29874 "collapsed loops not perfectly nested");
29876 collapse_err = true;
29877 cp_parser_statement_seq_opt (parser, NULL);
29878 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29879 break;
29883 while (!for_block->is_empty ())
29884 add_stmt (pop_stmt_list (for_block->pop ()));
29885 release_tree_vector (for_block);
29887 return ret;
29890 /* Helper function for OpenMP parsing, split clauses and call
29891 finish_omp_clauses on each of the set of clauses afterwards. */
29893 static void
29894 cp_omp_split_clauses (location_t loc, enum tree_code code,
29895 omp_clause_mask mask, tree clauses, tree *cclauses)
29897 int i;
29898 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29899 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29900 if (cclauses[i])
29901 cclauses[i] = finish_omp_clauses (cclauses[i]);
29904 /* OpenMP 4.0:
29905 #pragma omp simd simd-clause[optseq] new-line
29906 for-loop */
29908 #define OMP_SIMD_CLAUSE_MASK \
29909 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29917 static tree
29918 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29919 char *p_name, omp_clause_mask mask, tree *cclauses)
29921 tree clauses, sb, ret;
29922 unsigned int save;
29923 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29925 strcat (p_name, " simd");
29926 mask |= OMP_SIMD_CLAUSE_MASK;
29927 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29929 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29930 cclauses == NULL);
29931 if (cclauses)
29933 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29934 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29937 sb = begin_omp_structured_block ();
29938 save = cp_parser_begin_omp_structured_block (parser);
29940 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29942 cp_parser_end_omp_structured_block (parser, save);
29943 add_stmt (finish_omp_structured_block (sb));
29945 return ret;
29948 /* OpenMP 2.5:
29949 #pragma omp for for-clause[optseq] new-line
29950 for-loop
29952 OpenMP 4.0:
29953 #pragma omp for simd for-simd-clause[optseq] new-line
29954 for-loop */
29956 #define OMP_FOR_CLAUSE_MASK \
29957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29966 static tree
29967 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29968 char *p_name, omp_clause_mask mask, tree *cclauses)
29970 tree clauses, sb, ret;
29971 unsigned int save;
29972 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29974 strcat (p_name, " for");
29975 mask |= OMP_FOR_CLAUSE_MASK;
29976 if (cclauses)
29977 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29981 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29982 const char *p = IDENTIFIER_POINTER (id);
29984 if (strcmp (p, "simd") == 0)
29986 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29987 if (cclauses == NULL)
29988 cclauses = cclauses_buf;
29990 cp_lexer_consume_token (parser->lexer);
29991 if (!flag_openmp) /* flag_openmp_simd */
29992 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29993 cclauses);
29994 sb = begin_omp_structured_block ();
29995 save = cp_parser_begin_omp_structured_block (parser);
29996 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29997 cclauses);
29998 cp_parser_end_omp_structured_block (parser, save);
29999 tree body = finish_omp_structured_block (sb);
30000 if (ret == NULL)
30001 return ret;
30002 ret = make_node (OMP_FOR);
30003 TREE_TYPE (ret) = void_type_node;
30004 OMP_FOR_BODY (ret) = body;
30005 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30006 SET_EXPR_LOCATION (ret, loc);
30007 add_stmt (ret);
30008 return ret;
30011 if (!flag_openmp) /* flag_openmp_simd */
30013 cp_parser_require_pragma_eol (parser, pragma_tok);
30014 return NULL_TREE;
30017 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30018 cclauses == NULL);
30019 if (cclauses)
30021 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30022 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30025 sb = begin_omp_structured_block ();
30026 save = cp_parser_begin_omp_structured_block (parser);
30028 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30030 cp_parser_end_omp_structured_block (parser, save);
30031 add_stmt (finish_omp_structured_block (sb));
30033 return ret;
30036 /* OpenMP 2.5:
30037 # pragma omp master new-line
30038 structured-block */
30040 static tree
30041 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30043 cp_parser_require_pragma_eol (parser, pragma_tok);
30044 return c_finish_omp_master (input_location,
30045 cp_parser_omp_structured_block (parser));
30048 /* OpenMP 2.5:
30049 # pragma omp ordered new-line
30050 structured-block */
30052 static tree
30053 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30055 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30056 cp_parser_require_pragma_eol (parser, pragma_tok);
30057 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30060 /* OpenMP 2.5:
30062 section-scope:
30063 { section-sequence }
30065 section-sequence:
30066 section-directive[opt] structured-block
30067 section-sequence section-directive structured-block */
30069 static tree
30070 cp_parser_omp_sections_scope (cp_parser *parser)
30072 tree stmt, substmt;
30073 bool error_suppress = false;
30074 cp_token *tok;
30076 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30077 return NULL_TREE;
30079 stmt = push_stmt_list ();
30081 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30083 substmt = cp_parser_omp_structured_block (parser);
30084 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30085 add_stmt (substmt);
30088 while (1)
30090 tok = cp_lexer_peek_token (parser->lexer);
30091 if (tok->type == CPP_CLOSE_BRACE)
30092 break;
30093 if (tok->type == CPP_EOF)
30094 break;
30096 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30098 cp_lexer_consume_token (parser->lexer);
30099 cp_parser_require_pragma_eol (parser, tok);
30100 error_suppress = false;
30102 else if (!error_suppress)
30104 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30105 error_suppress = true;
30108 substmt = cp_parser_omp_structured_block (parser);
30109 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30110 add_stmt (substmt);
30112 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30114 substmt = pop_stmt_list (stmt);
30116 stmt = make_node (OMP_SECTIONS);
30117 TREE_TYPE (stmt) = void_type_node;
30118 OMP_SECTIONS_BODY (stmt) = substmt;
30120 add_stmt (stmt);
30121 return stmt;
30124 /* OpenMP 2.5:
30125 # pragma omp sections sections-clause[optseq] newline
30126 sections-scope */
30128 #define OMP_SECTIONS_CLAUSE_MASK \
30129 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30135 static tree
30136 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30137 char *p_name, omp_clause_mask mask, tree *cclauses)
30139 tree clauses, ret;
30140 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30142 strcat (p_name, " sections");
30143 mask |= OMP_SECTIONS_CLAUSE_MASK;
30144 if (cclauses)
30145 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30147 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30148 cclauses == NULL);
30149 if (cclauses)
30151 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30152 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30155 ret = cp_parser_omp_sections_scope (parser);
30156 if (ret)
30157 OMP_SECTIONS_CLAUSES (ret) = clauses;
30159 return ret;
30162 /* OpenMP 2.5:
30163 # pragma omp parallel parallel-clause[optseq] new-line
30164 structured-block
30165 # pragma omp parallel for parallel-for-clause[optseq] new-line
30166 structured-block
30167 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30168 structured-block
30170 OpenMP 4.0:
30171 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30172 structured-block */
30174 #define OMP_PARALLEL_CLAUSE_MASK \
30175 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30185 static tree
30186 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30187 char *p_name, omp_clause_mask mask, tree *cclauses)
30189 tree stmt, clauses, block;
30190 unsigned int save;
30191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30193 strcat (p_name, " parallel");
30194 mask |= OMP_PARALLEL_CLAUSE_MASK;
30196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30198 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30199 if (cclauses == NULL)
30200 cclauses = cclauses_buf;
30202 cp_lexer_consume_token (parser->lexer);
30203 if (!flag_openmp) /* flag_openmp_simd */
30204 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30205 block = begin_omp_parallel ();
30206 save = cp_parser_begin_omp_structured_block (parser);
30207 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30208 cp_parser_end_omp_structured_block (parser, save);
30209 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30210 block);
30211 if (ret == NULL_TREE)
30212 return ret;
30213 OMP_PARALLEL_COMBINED (stmt) = 1;
30214 return stmt;
30216 else if (cclauses)
30218 error_at (loc, "expected %<for%> after %qs", p_name);
30219 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30220 return NULL_TREE;
30222 else if (!flag_openmp) /* flag_openmp_simd */
30224 cp_parser_require_pragma_eol (parser, pragma_tok);
30225 return NULL_TREE;
30227 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30230 const char *p = IDENTIFIER_POINTER (id);
30231 if (strcmp (p, "sections") == 0)
30233 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30234 cclauses = cclauses_buf;
30236 cp_lexer_consume_token (parser->lexer);
30237 block = begin_omp_parallel ();
30238 save = cp_parser_begin_omp_structured_block (parser);
30239 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30240 cp_parser_end_omp_structured_block (parser, save);
30241 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30242 block);
30243 OMP_PARALLEL_COMBINED (stmt) = 1;
30244 return stmt;
30248 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30250 block = begin_omp_parallel ();
30251 save = cp_parser_begin_omp_structured_block (parser);
30252 cp_parser_statement (parser, NULL_TREE, false, NULL);
30253 cp_parser_end_omp_structured_block (parser, save);
30254 stmt = finish_omp_parallel (clauses, block);
30255 return stmt;
30258 /* OpenMP 2.5:
30259 # pragma omp single single-clause[optseq] new-line
30260 structured-block */
30262 #define OMP_SINGLE_CLAUSE_MASK \
30263 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30268 static tree
30269 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30271 tree stmt = make_node (OMP_SINGLE);
30272 TREE_TYPE (stmt) = void_type_node;
30274 OMP_SINGLE_CLAUSES (stmt)
30275 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30276 "#pragma omp single", pragma_tok);
30277 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30279 return add_stmt (stmt);
30282 /* OpenMP 3.0:
30283 # pragma omp task task-clause[optseq] new-line
30284 structured-block */
30286 #define OMP_TASK_CLAUSE_MASK \
30287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30297 static tree
30298 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30300 tree clauses, block;
30301 unsigned int save;
30303 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30304 "#pragma omp task", pragma_tok);
30305 block = begin_omp_task ();
30306 save = cp_parser_begin_omp_structured_block (parser);
30307 cp_parser_statement (parser, NULL_TREE, false, NULL);
30308 cp_parser_end_omp_structured_block (parser, save);
30309 return finish_omp_task (clauses, block);
30312 /* OpenMP 3.0:
30313 # pragma omp taskwait new-line */
30315 static void
30316 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30318 cp_parser_require_pragma_eol (parser, pragma_tok);
30319 finish_omp_taskwait ();
30322 /* OpenMP 3.1:
30323 # pragma omp taskyield new-line */
30325 static void
30326 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30328 cp_parser_require_pragma_eol (parser, pragma_tok);
30329 finish_omp_taskyield ();
30332 /* OpenMP 4.0:
30333 # pragma omp taskgroup new-line
30334 structured-block */
30336 static tree
30337 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30339 cp_parser_require_pragma_eol (parser, pragma_tok);
30340 return c_finish_omp_taskgroup (input_location,
30341 cp_parser_omp_structured_block (parser));
30345 /* OpenMP 2.5:
30346 # pragma omp threadprivate (variable-list) */
30348 static void
30349 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30351 tree vars;
30353 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30354 cp_parser_require_pragma_eol (parser, pragma_tok);
30356 finish_omp_threadprivate (vars);
30359 /* OpenMP 4.0:
30360 # pragma omp cancel cancel-clause[optseq] new-line */
30362 #define OMP_CANCEL_CLAUSE_MASK \
30363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30369 static void
30370 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30372 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30373 "#pragma omp cancel", pragma_tok);
30374 finish_omp_cancel (clauses);
30377 /* OpenMP 4.0:
30378 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30380 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30381 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30386 static void
30387 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30389 tree clauses;
30390 bool point_seen = false;
30392 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30394 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30395 const char *p = IDENTIFIER_POINTER (id);
30397 if (strcmp (p, "point") == 0)
30399 cp_lexer_consume_token (parser->lexer);
30400 point_seen = true;
30403 if (!point_seen)
30405 cp_parser_error (parser, "expected %<point%>");
30406 cp_parser_require_pragma_eol (parser, pragma_tok);
30407 return;
30410 clauses = cp_parser_omp_all_clauses (parser,
30411 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30412 "#pragma omp cancellation point",
30413 pragma_tok);
30414 finish_omp_cancellation_point (clauses);
30417 /* OpenMP 4.0:
30418 #pragma omp distribute distribute-clause[optseq] new-line
30419 for-loop */
30421 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30422 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30427 static tree
30428 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30429 char *p_name, omp_clause_mask mask, tree *cclauses)
30431 tree clauses, sb, ret;
30432 unsigned int save;
30433 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30435 strcat (p_name, " distribute");
30436 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30438 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30440 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30441 const char *p = IDENTIFIER_POINTER (id);
30442 bool simd = false;
30443 bool parallel = false;
30445 if (strcmp (p, "simd") == 0)
30446 simd = true;
30447 else
30448 parallel = strcmp (p, "parallel") == 0;
30449 if (parallel || simd)
30451 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30452 if (cclauses == NULL)
30453 cclauses = cclauses_buf;
30454 cp_lexer_consume_token (parser->lexer);
30455 if (!flag_openmp) /* flag_openmp_simd */
30457 if (simd)
30458 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30459 cclauses);
30460 else
30461 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30462 cclauses);
30464 sb = begin_omp_structured_block ();
30465 save = cp_parser_begin_omp_structured_block (parser);
30466 if (simd)
30467 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30468 cclauses);
30469 else
30470 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30471 cclauses);
30472 cp_parser_end_omp_structured_block (parser, save);
30473 tree body = finish_omp_structured_block (sb);
30474 if (ret == NULL)
30475 return ret;
30476 ret = make_node (OMP_DISTRIBUTE);
30477 TREE_TYPE (ret) = void_type_node;
30478 OMP_FOR_BODY (ret) = body;
30479 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30480 SET_EXPR_LOCATION (ret, loc);
30481 add_stmt (ret);
30482 return ret;
30485 if (!flag_openmp) /* flag_openmp_simd */
30487 cp_parser_require_pragma_eol (parser, pragma_tok);
30488 return NULL_TREE;
30491 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30492 cclauses == NULL);
30493 if (cclauses)
30495 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30496 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30499 sb = begin_omp_structured_block ();
30500 save = cp_parser_begin_omp_structured_block (parser);
30502 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30504 cp_parser_end_omp_structured_block (parser, save);
30505 add_stmt (finish_omp_structured_block (sb));
30507 return ret;
30510 /* OpenMP 4.0:
30511 # pragma omp teams teams-clause[optseq] new-line
30512 structured-block */
30514 #define OMP_TEAMS_CLAUSE_MASK \
30515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30523 static tree
30524 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30525 char *p_name, omp_clause_mask mask, tree *cclauses)
30527 tree clauses, sb, ret;
30528 unsigned int save;
30529 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30531 strcat (p_name, " teams");
30532 mask |= OMP_TEAMS_CLAUSE_MASK;
30534 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30536 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30537 const char *p = IDENTIFIER_POINTER (id);
30538 if (strcmp (p, "distribute") == 0)
30540 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30541 if (cclauses == NULL)
30542 cclauses = cclauses_buf;
30544 cp_lexer_consume_token (parser->lexer);
30545 if (!flag_openmp) /* flag_openmp_simd */
30546 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30547 cclauses);
30548 sb = begin_omp_structured_block ();
30549 save = cp_parser_begin_omp_structured_block (parser);
30550 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30551 cclauses);
30552 cp_parser_end_omp_structured_block (parser, save);
30553 tree body = finish_omp_structured_block (sb);
30554 if (ret == NULL)
30555 return ret;
30556 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30557 ret = make_node (OMP_TEAMS);
30558 TREE_TYPE (ret) = void_type_node;
30559 OMP_TEAMS_CLAUSES (ret) = clauses;
30560 OMP_TEAMS_BODY (ret) = body;
30561 return add_stmt (ret);
30564 if (!flag_openmp) /* flag_openmp_simd */
30566 cp_parser_require_pragma_eol (parser, pragma_tok);
30567 return NULL_TREE;
30570 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30571 cclauses == NULL);
30572 if (cclauses)
30574 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30575 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30578 tree stmt = make_node (OMP_TEAMS);
30579 TREE_TYPE (stmt) = void_type_node;
30580 OMP_TEAMS_CLAUSES (stmt) = clauses;
30581 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30583 return add_stmt (stmt);
30586 /* OpenMP 4.0:
30587 # pragma omp target data target-data-clause[optseq] new-line
30588 structured-block */
30590 #define OMP_TARGET_DATA_CLAUSE_MASK \
30591 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30595 static tree
30596 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30598 tree stmt = make_node (OMP_TARGET_DATA);
30599 TREE_TYPE (stmt) = void_type_node;
30601 OMP_TARGET_DATA_CLAUSES (stmt)
30602 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30603 "#pragma omp target data", pragma_tok);
30604 keep_next_level (true);
30605 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30607 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30608 return add_stmt (stmt);
30611 /* OpenMP 4.0:
30612 # pragma omp target update target-update-clause[optseq] new-line */
30614 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30615 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30620 static bool
30621 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30622 enum pragma_context context)
30624 if (context == pragma_stmt)
30626 error_at (pragma_tok->location,
30627 "%<#pragma omp target update%> may only be "
30628 "used in compound statements");
30629 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30630 return false;
30633 tree clauses
30634 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30635 "#pragma omp target update", pragma_tok);
30636 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30637 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30639 error_at (pragma_tok->location,
30640 "%<#pragma omp target update must contain at least one "
30641 "%<from%> or %<to%> clauses");
30642 return false;
30645 tree stmt = make_node (OMP_TARGET_UPDATE);
30646 TREE_TYPE (stmt) = void_type_node;
30647 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30648 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30649 add_stmt (stmt);
30650 return false;
30653 /* OpenMP 4.0:
30654 # pragma omp target target-clause[optseq] new-line
30655 structured-block */
30657 #define OMP_TARGET_CLAUSE_MASK \
30658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30662 static bool
30663 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30664 enum pragma_context context)
30666 if (context != pragma_stmt && context != pragma_compound)
30668 cp_parser_error (parser, "expected declaration specifiers");
30669 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30670 return false;
30673 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30675 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30676 const char *p = IDENTIFIER_POINTER (id);
30678 if (strcmp (p, "teams") == 0)
30680 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30681 char p_name[sizeof ("#pragma omp target teams distribute "
30682 "parallel for simd")];
30684 cp_lexer_consume_token (parser->lexer);
30685 strcpy (p_name, "#pragma omp target");
30686 if (!flag_openmp) /* flag_openmp_simd */
30688 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30689 OMP_TARGET_CLAUSE_MASK,
30690 cclauses);
30691 return stmt != NULL_TREE;
30693 keep_next_level (true);
30694 tree sb = begin_omp_structured_block ();
30695 unsigned save = cp_parser_begin_omp_structured_block (parser);
30696 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30697 OMP_TARGET_CLAUSE_MASK, cclauses);
30698 cp_parser_end_omp_structured_block (parser, save);
30699 tree body = finish_omp_structured_block (sb);
30700 if (ret == NULL_TREE)
30701 return false;
30702 tree stmt = make_node (OMP_TARGET);
30703 TREE_TYPE (stmt) = void_type_node;
30704 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30705 OMP_TARGET_BODY (stmt) = body;
30706 add_stmt (stmt);
30707 return true;
30709 else if (!flag_openmp) /* flag_openmp_simd */
30711 cp_parser_require_pragma_eol (parser, pragma_tok);
30712 return false;
30714 else if (strcmp (p, "data") == 0)
30716 cp_lexer_consume_token (parser->lexer);
30717 cp_parser_omp_target_data (parser, pragma_tok);
30718 return true;
30720 else if (strcmp (p, "update") == 0)
30722 cp_lexer_consume_token (parser->lexer);
30723 return cp_parser_omp_target_update (parser, pragma_tok, context);
30727 tree stmt = make_node (OMP_TARGET);
30728 TREE_TYPE (stmt) = void_type_node;
30730 OMP_TARGET_CLAUSES (stmt)
30731 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30732 "#pragma omp target", pragma_tok);
30733 keep_next_level (true);
30734 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30736 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30737 add_stmt (stmt);
30738 return true;
30741 /* OpenMP 4.0:
30742 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30744 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30745 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30752 static void
30753 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30754 enum pragma_context context)
30756 bool first_p = parser->omp_declare_simd == NULL;
30757 cp_omp_declare_simd_data data;
30758 if (first_p)
30760 data.error_seen = false;
30761 data.fndecl_seen = false;
30762 data.tokens = vNULL;
30763 parser->omp_declare_simd = &data;
30765 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30766 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30767 cp_lexer_consume_token (parser->lexer);
30768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30769 parser->omp_declare_simd->error_seen = true;
30770 cp_parser_require_pragma_eol (parser, pragma_tok);
30771 struct cp_token_cache *cp
30772 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30773 parser->omp_declare_simd->tokens.safe_push (cp);
30774 if (first_p)
30776 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30777 cp_parser_pragma (parser, context);
30778 switch (context)
30780 case pragma_external:
30781 cp_parser_declaration (parser);
30782 break;
30783 case pragma_member:
30784 cp_parser_member_declaration (parser);
30785 break;
30786 case pragma_objc_icode:
30787 cp_parser_block_declaration (parser, /*statement_p=*/false);
30788 break;
30789 default:
30790 cp_parser_declaration_statement (parser);
30791 break;
30793 if (parser->omp_declare_simd
30794 && !parser->omp_declare_simd->error_seen
30795 && !parser->omp_declare_simd->fndecl_seen)
30796 error_at (pragma_tok->location,
30797 "%<#pragma omp declare simd%> not immediately followed by "
30798 "function declaration or definition");
30799 data.tokens.release ();
30800 parser->omp_declare_simd = NULL;
30804 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30805 This function is modelled similar to the late parsing of omp declare
30806 simd. */
30808 static tree
30809 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30811 struct cp_token_cache *ce;
30812 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30813 int ii = 0;
30815 if (parser->omp_declare_simd != NULL)
30817 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30818 " marked as a Cilk Plus SIMD-enabled function");
30819 XDELETE (parser->cilk_simd_fn_info);
30820 parser->cilk_simd_fn_info = NULL;
30821 return attrs;
30823 if (!info->error_seen && info->fndecl_seen)
30825 error ("vector attribute not immediately followed by a single function"
30826 " declaration or definition");
30827 info->error_seen = true;
30829 if (info->error_seen)
30830 return attrs;
30832 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30834 tree c, cl;
30836 cp_parser_push_lexer_for_tokens (parser, ce);
30837 parser->lexer->in_pragma = true;
30838 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30839 "SIMD-enabled functions attribute",
30840 NULL);
30841 cp_parser_pop_lexer (parser);
30842 if (cl)
30843 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30845 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30846 TREE_CHAIN (c) = attrs;
30847 attrs = c;
30849 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30850 TREE_CHAIN (c) = attrs;
30851 if (processing_template_decl)
30852 ATTR_IS_DEPENDENT (c) = 1;
30853 attrs = c;
30855 info->fndecl_seen = true;
30856 XDELETE (parser->cilk_simd_fn_info);
30857 parser->cilk_simd_fn_info = NULL;
30858 return attrs;
30861 /* Finalize #pragma omp declare simd clauses after direct declarator has
30862 been parsed, and put that into "omp declare simd" attribute. */
30864 static tree
30865 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30867 struct cp_token_cache *ce;
30868 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30869 int i;
30871 if (!data->error_seen && data->fndecl_seen)
30873 error ("%<#pragma omp declare simd%> not immediately followed by "
30874 "a single function declaration or definition");
30875 data->error_seen = true;
30876 return attrs;
30878 if (data->error_seen)
30879 return attrs;
30881 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30883 tree c, cl;
30885 cp_parser_push_lexer_for_tokens (parser, ce);
30886 parser->lexer->in_pragma = true;
30887 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30888 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30889 cp_lexer_consume_token (parser->lexer);
30890 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30891 "#pragma omp declare simd", pragma_tok);
30892 cp_parser_pop_lexer (parser);
30893 if (cl)
30894 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30895 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30896 TREE_CHAIN (c) = attrs;
30897 if (processing_template_decl)
30898 ATTR_IS_DEPENDENT (c) = 1;
30899 attrs = c;
30902 data->fndecl_seen = true;
30903 return attrs;
30907 /* OpenMP 4.0:
30908 # pragma omp declare target new-line
30909 declarations and definitions
30910 # pragma omp end declare target new-line */
30912 static void
30913 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30915 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30916 scope_chain->omp_declare_target_attribute++;
30919 static void
30920 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30922 const char *p = "";
30923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30925 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30926 p = IDENTIFIER_POINTER (id);
30928 if (strcmp (p, "declare") == 0)
30930 cp_lexer_consume_token (parser->lexer);
30931 p = "";
30932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30934 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30935 p = IDENTIFIER_POINTER (id);
30937 if (strcmp (p, "target") == 0)
30938 cp_lexer_consume_token (parser->lexer);
30939 else
30941 cp_parser_error (parser, "expected %<target%>");
30942 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30943 return;
30946 else
30948 cp_parser_error (parser, "expected %<declare%>");
30949 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30950 return;
30952 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30953 if (!scope_chain->omp_declare_target_attribute)
30954 error_at (pragma_tok->location,
30955 "%<#pragma omp end declare target%> without corresponding "
30956 "%<#pragma omp declare target%>");
30957 else
30958 scope_chain->omp_declare_target_attribute--;
30961 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30962 expression and optional initializer clause of
30963 #pragma omp declare reduction. We store the expression(s) as
30964 either 3, 6 or 7 special statements inside of the artificial function's
30965 body. The first two statements are DECL_EXPRs for the artificial
30966 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30967 expression that uses those variables.
30968 If there was any INITIALIZER clause, this is followed by further statements,
30969 the fourth and fifth statements are DECL_EXPRs for the artificial
30970 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30971 constructor variant (first token after open paren is not omp_priv),
30972 then the sixth statement is a statement with the function call expression
30973 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30974 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30975 to initialize the OMP_PRIV artificial variable and there is seventh
30976 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30978 static bool
30979 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30981 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30982 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30983 type = TREE_TYPE (type);
30984 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30985 DECL_ARTIFICIAL (omp_out) = 1;
30986 pushdecl (omp_out);
30987 add_decl_expr (omp_out);
30988 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30989 DECL_ARTIFICIAL (omp_in) = 1;
30990 pushdecl (omp_in);
30991 add_decl_expr (omp_in);
30992 tree combiner;
30993 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30995 keep_next_level (true);
30996 tree block = begin_omp_structured_block ();
30997 combiner = cp_parser_expression (parser);
30998 finish_expr_stmt (combiner);
30999 block = finish_omp_structured_block (block);
31000 add_stmt (block);
31002 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31003 return false;
31005 const char *p = "";
31006 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31008 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31009 p = IDENTIFIER_POINTER (id);
31012 if (strcmp (p, "initializer") == 0)
31014 cp_lexer_consume_token (parser->lexer);
31015 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31016 return false;
31018 p = "";
31019 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31021 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31022 p = IDENTIFIER_POINTER (id);
31025 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31026 DECL_ARTIFICIAL (omp_priv) = 1;
31027 pushdecl (omp_priv);
31028 add_decl_expr (omp_priv);
31029 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31030 DECL_ARTIFICIAL (omp_orig) = 1;
31031 pushdecl (omp_orig);
31032 add_decl_expr (omp_orig);
31034 keep_next_level (true);
31035 block = begin_omp_structured_block ();
31037 bool ctor = false;
31038 if (strcmp (p, "omp_priv") == 0)
31040 bool is_direct_init, is_non_constant_init;
31041 ctor = true;
31042 cp_lexer_consume_token (parser->lexer);
31043 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31044 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31045 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31046 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31047 == CPP_CLOSE_PAREN
31048 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31049 == CPP_CLOSE_PAREN))
31051 finish_omp_structured_block (block);
31052 error ("invalid initializer clause");
31053 return false;
31055 initializer = cp_parser_initializer (parser, &is_direct_init,
31056 &is_non_constant_init);
31057 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31058 NULL_TREE, LOOKUP_ONLYCONVERTING);
31060 else
31062 cp_parser_parse_tentatively (parser);
31063 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31064 /*check_dependency_p=*/true,
31065 /*template_p=*/NULL,
31066 /*declarator_p=*/false,
31067 /*optional_p=*/false);
31068 vec<tree, va_gc> *args;
31069 if (fn_name == error_mark_node
31070 || cp_parser_error_occurred (parser)
31071 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31072 || ((args = cp_parser_parenthesized_expression_list
31073 (parser, non_attr, /*cast_p=*/false,
31074 /*allow_expansion_p=*/true,
31075 /*non_constant_p=*/NULL)),
31076 cp_parser_error_occurred (parser)))
31078 finish_omp_structured_block (block);
31079 cp_parser_abort_tentative_parse (parser);
31080 cp_parser_error (parser, "expected id-expression (arguments)");
31081 return false;
31083 unsigned int i;
31084 tree arg;
31085 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31086 if (arg == omp_priv
31087 || (TREE_CODE (arg) == ADDR_EXPR
31088 && TREE_OPERAND (arg, 0) == omp_priv))
31089 break;
31090 cp_parser_abort_tentative_parse (parser);
31091 if (arg == NULL_TREE)
31092 error ("one of the initializer call arguments should be %<omp_priv%>"
31093 " or %<&omp_priv%>");
31094 initializer = cp_parser_postfix_expression (parser, false, false, false,
31095 false, NULL);
31096 finish_expr_stmt (initializer);
31099 block = finish_omp_structured_block (block);
31100 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31101 finish_expr_stmt (block);
31103 if (ctor)
31104 add_decl_expr (omp_orig);
31106 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31107 return false;
31110 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31111 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31113 return true;
31116 /* OpenMP 4.0
31117 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31118 initializer-clause[opt] new-line
31120 initializer-clause:
31121 initializer (omp_priv initializer)
31122 initializer (function-name (argument-list)) */
31124 static void
31125 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31126 enum pragma_context)
31128 auto_vec<tree> types;
31129 enum tree_code reduc_code = ERROR_MARK;
31130 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31131 unsigned int i;
31132 cp_token *first_token;
31133 cp_token_cache *cp;
31134 int errs;
31135 void *p;
31137 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31138 p = obstack_alloc (&declarator_obstack, 0);
31140 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31141 goto fail;
31143 switch (cp_lexer_peek_token (parser->lexer)->type)
31145 case CPP_PLUS:
31146 reduc_code = PLUS_EXPR;
31147 break;
31148 case CPP_MULT:
31149 reduc_code = MULT_EXPR;
31150 break;
31151 case CPP_MINUS:
31152 reduc_code = MINUS_EXPR;
31153 break;
31154 case CPP_AND:
31155 reduc_code = BIT_AND_EXPR;
31156 break;
31157 case CPP_XOR:
31158 reduc_code = BIT_XOR_EXPR;
31159 break;
31160 case CPP_OR:
31161 reduc_code = BIT_IOR_EXPR;
31162 break;
31163 case CPP_AND_AND:
31164 reduc_code = TRUTH_ANDIF_EXPR;
31165 break;
31166 case CPP_OR_OR:
31167 reduc_code = TRUTH_ORIF_EXPR;
31168 break;
31169 case CPP_NAME:
31170 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31171 break;
31172 default:
31173 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31174 "%<|%>, %<&&%>, %<||%> or identifier");
31175 goto fail;
31178 if (reduc_code != ERROR_MARK)
31179 cp_lexer_consume_token (parser->lexer);
31181 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31182 if (reduc_id == error_mark_node)
31183 goto fail;
31185 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31186 goto fail;
31188 /* Types may not be defined in declare reduction type list. */
31189 const char *saved_message;
31190 saved_message = parser->type_definition_forbidden_message;
31191 parser->type_definition_forbidden_message
31192 = G_("types may not be defined in declare reduction type list");
31193 bool saved_colon_corrects_to_scope_p;
31194 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31195 parser->colon_corrects_to_scope_p = false;
31196 bool saved_colon_doesnt_start_class_def_p;
31197 saved_colon_doesnt_start_class_def_p
31198 = parser->colon_doesnt_start_class_def_p;
31199 parser->colon_doesnt_start_class_def_p = true;
31201 while (true)
31203 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31204 type = cp_parser_type_id (parser);
31205 if (type == error_mark_node)
31207 else if (ARITHMETIC_TYPE_P (type)
31208 && (orig_reduc_id == NULL_TREE
31209 || (TREE_CODE (type) != COMPLEX_TYPE
31210 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31211 "min") == 0
31212 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31213 "max") == 0))))
31214 error_at (loc, "predeclared arithmetic type %qT in "
31215 "%<#pragma omp declare reduction%>", type);
31216 else if (TREE_CODE (type) == FUNCTION_TYPE
31217 || TREE_CODE (type) == METHOD_TYPE
31218 || TREE_CODE (type) == ARRAY_TYPE)
31219 error_at (loc, "function or array type %qT in "
31220 "%<#pragma omp declare reduction%>", type);
31221 else if (TREE_CODE (type) == REFERENCE_TYPE)
31222 error_at (loc, "reference type %qT in "
31223 "%<#pragma omp declare reduction%>", type);
31224 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31225 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31226 "%<#pragma omp declare reduction%>", type);
31227 else
31228 types.safe_push (type);
31230 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31231 cp_lexer_consume_token (parser->lexer);
31232 else
31233 break;
31236 /* Restore the saved message. */
31237 parser->type_definition_forbidden_message = saved_message;
31238 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31239 parser->colon_doesnt_start_class_def_p
31240 = saved_colon_doesnt_start_class_def_p;
31242 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31243 || types.is_empty ())
31245 fail:
31246 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31247 goto done;
31250 first_token = cp_lexer_peek_token (parser->lexer);
31251 cp = NULL;
31252 errs = errorcount;
31253 FOR_EACH_VEC_ELT (types, i, type)
31255 tree fntype
31256 = build_function_type_list (void_type_node,
31257 cp_build_reference_type (type, false),
31258 NULL_TREE);
31259 tree this_reduc_id = reduc_id;
31260 if (!dependent_type_p (type))
31261 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31262 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31263 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31264 DECL_ARTIFICIAL (fndecl) = 1;
31265 DECL_EXTERNAL (fndecl) = 1;
31266 DECL_DECLARED_INLINE_P (fndecl) = 1;
31267 DECL_IGNORED_P (fndecl) = 1;
31268 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31269 DECL_ATTRIBUTES (fndecl)
31270 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31271 DECL_ATTRIBUTES (fndecl));
31272 if (processing_template_decl)
31273 fndecl = push_template_decl (fndecl);
31274 bool block_scope = false;
31275 tree block = NULL_TREE;
31276 if (current_function_decl)
31278 block_scope = true;
31279 DECL_CONTEXT (fndecl) = global_namespace;
31280 if (!processing_template_decl)
31281 pushdecl (fndecl);
31283 else if (current_class_type)
31285 if (cp == NULL)
31287 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31288 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31289 cp_lexer_consume_token (parser->lexer);
31290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31291 goto fail;
31292 cp = cp_token_cache_new (first_token,
31293 cp_lexer_peek_nth_token (parser->lexer,
31294 2));
31296 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31297 finish_member_declaration (fndecl);
31298 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31299 DECL_PENDING_INLINE_P (fndecl) = 1;
31300 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31301 continue;
31303 else
31305 DECL_CONTEXT (fndecl) = current_namespace;
31306 pushdecl (fndecl);
31308 if (!block_scope)
31309 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31310 else
31311 block = begin_omp_structured_block ();
31312 if (cp)
31314 cp_parser_push_lexer_for_tokens (parser, cp);
31315 parser->lexer->in_pragma = true;
31317 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31319 if (!block_scope)
31320 finish_function (0);
31321 else
31322 DECL_CONTEXT (fndecl) = current_function_decl;
31323 if (cp)
31324 cp_parser_pop_lexer (parser);
31325 goto fail;
31327 if (cp)
31328 cp_parser_pop_lexer (parser);
31329 if (!block_scope)
31330 finish_function (0);
31331 else
31333 DECL_CONTEXT (fndecl) = current_function_decl;
31334 block = finish_omp_structured_block (block);
31335 if (TREE_CODE (block) == BIND_EXPR)
31336 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31337 else if (TREE_CODE (block) == STATEMENT_LIST)
31338 DECL_SAVED_TREE (fndecl) = block;
31339 if (processing_template_decl)
31340 add_decl_expr (fndecl);
31342 cp_check_omp_declare_reduction (fndecl);
31343 if (cp == NULL && types.length () > 1)
31344 cp = cp_token_cache_new (first_token,
31345 cp_lexer_peek_nth_token (parser->lexer, 2));
31346 if (errs != errorcount)
31347 break;
31350 cp_parser_require_pragma_eol (parser, pragma_tok);
31352 done:
31353 /* Free any declarators allocated. */
31354 obstack_free (&declarator_obstack, p);
31357 /* OpenMP 4.0
31358 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31359 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31360 initializer-clause[opt] new-line
31361 #pragma omp declare target new-line */
31363 static void
31364 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31365 enum pragma_context context)
31367 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31369 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31370 const char *p = IDENTIFIER_POINTER (id);
31372 if (strcmp (p, "simd") == 0)
31374 cp_lexer_consume_token (parser->lexer);
31375 cp_parser_omp_declare_simd (parser, pragma_tok,
31376 context);
31377 return;
31379 cp_ensure_no_omp_declare_simd (parser);
31380 if (strcmp (p, "reduction") == 0)
31382 cp_lexer_consume_token (parser->lexer);
31383 cp_parser_omp_declare_reduction (parser, pragma_tok,
31384 context);
31385 return;
31387 if (!flag_openmp) /* flag_openmp_simd */
31389 cp_parser_require_pragma_eol (parser, pragma_tok);
31390 return;
31392 if (strcmp (p, "target") == 0)
31394 cp_lexer_consume_token (parser->lexer);
31395 cp_parser_omp_declare_target (parser, pragma_tok);
31396 return;
31399 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31400 "or %<target%>");
31401 cp_parser_require_pragma_eol (parser, pragma_tok);
31404 /* Main entry point to OpenMP statement pragmas. */
31406 static void
31407 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31409 tree stmt;
31410 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31411 omp_clause_mask mask (0);
31413 switch (pragma_tok->pragma_kind)
31415 case PRAGMA_OMP_ATOMIC:
31416 cp_parser_omp_atomic (parser, pragma_tok);
31417 return;
31418 case PRAGMA_OMP_CRITICAL:
31419 stmt = cp_parser_omp_critical (parser, pragma_tok);
31420 break;
31421 case PRAGMA_OMP_DISTRIBUTE:
31422 strcpy (p_name, "#pragma omp");
31423 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31424 break;
31425 case PRAGMA_OMP_FOR:
31426 strcpy (p_name, "#pragma omp");
31427 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31428 break;
31429 case PRAGMA_OMP_MASTER:
31430 stmt = cp_parser_omp_master (parser, pragma_tok);
31431 break;
31432 case PRAGMA_OMP_ORDERED:
31433 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31434 break;
31435 case PRAGMA_OMP_PARALLEL:
31436 strcpy (p_name, "#pragma omp");
31437 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31438 break;
31439 case PRAGMA_OMP_SECTIONS:
31440 strcpy (p_name, "#pragma omp");
31441 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31442 break;
31443 case PRAGMA_OMP_SIMD:
31444 strcpy (p_name, "#pragma omp");
31445 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31446 break;
31447 case PRAGMA_OMP_SINGLE:
31448 stmt = cp_parser_omp_single (parser, pragma_tok);
31449 break;
31450 case PRAGMA_OMP_TASK:
31451 stmt = cp_parser_omp_task (parser, pragma_tok);
31452 break;
31453 case PRAGMA_OMP_TASKGROUP:
31454 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31455 break;
31456 case PRAGMA_OMP_TEAMS:
31457 strcpy (p_name, "#pragma omp");
31458 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31459 break;
31460 default:
31461 gcc_unreachable ();
31464 if (stmt)
31465 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31468 /* Transactional Memory parsing routines. */
31470 /* Parse a transaction attribute.
31472 txn-attribute:
31473 attribute
31474 [ [ identifier ] ]
31476 ??? Simplify this when C++0x bracket attributes are
31477 implemented properly. */
31479 static tree
31480 cp_parser_txn_attribute_opt (cp_parser *parser)
31482 cp_token *token;
31483 tree attr_name, attr = NULL;
31485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31486 return cp_parser_attributes_opt (parser);
31488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31489 return NULL_TREE;
31490 cp_lexer_consume_token (parser->lexer);
31491 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31492 goto error1;
31494 token = cp_lexer_peek_token (parser->lexer);
31495 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31497 token = cp_lexer_consume_token (parser->lexer);
31499 attr_name = (token->type == CPP_KEYWORD
31500 /* For keywords, use the canonical spelling,
31501 not the parsed identifier. */
31502 ? ridpointers[(int) token->keyword]
31503 : token->u.value);
31504 attr = build_tree_list (attr_name, NULL_TREE);
31506 else
31507 cp_parser_error (parser, "expected identifier");
31509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31510 error1:
31511 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31512 return attr;
31515 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31517 transaction-statement:
31518 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31519 compound-statement
31520 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31523 static tree
31524 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31526 unsigned char old_in = parser->in_transaction;
31527 unsigned char this_in = 1, new_in;
31528 cp_token *token;
31529 tree stmt, attrs, noex;
31531 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31532 || keyword == RID_TRANSACTION_RELAXED);
31533 token = cp_parser_require_keyword (parser, keyword,
31534 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31535 : RT_TRANSACTION_RELAXED));
31536 gcc_assert (token != NULL);
31538 if (keyword == RID_TRANSACTION_RELAXED)
31539 this_in |= TM_STMT_ATTR_RELAXED;
31540 else
31542 attrs = cp_parser_txn_attribute_opt (parser);
31543 if (attrs)
31544 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31547 /* Parse a noexcept specification. */
31548 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31550 /* Keep track if we're in the lexical scope of an outer transaction. */
31551 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31553 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31555 parser->in_transaction = new_in;
31556 cp_parser_compound_statement (parser, NULL, false, false);
31557 parser->in_transaction = old_in;
31559 finish_transaction_stmt (stmt, NULL, this_in, noex);
31561 return stmt;
31564 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31566 transaction-expression:
31567 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31568 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31571 static tree
31572 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31574 unsigned char old_in = parser->in_transaction;
31575 unsigned char this_in = 1;
31576 cp_token *token;
31577 tree expr, noex;
31578 bool noex_expr;
31580 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31581 || keyword == RID_TRANSACTION_RELAXED);
31583 if (!flag_tm)
31584 error (keyword == RID_TRANSACTION_RELAXED
31585 ? G_("%<__transaction_relaxed%> without transactional memory "
31586 "support enabled")
31587 : G_("%<__transaction_atomic%> without transactional memory "
31588 "support enabled"));
31590 token = cp_parser_require_keyword (parser, keyword,
31591 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31592 : RT_TRANSACTION_RELAXED));
31593 gcc_assert (token != NULL);
31595 if (keyword == RID_TRANSACTION_RELAXED)
31596 this_in |= TM_STMT_ATTR_RELAXED;
31598 /* Set this early. This might mean that we allow transaction_cancel in
31599 an expression that we find out later actually has to be a constexpr.
31600 However, we expect that cxx_constant_value will be able to deal with
31601 this; also, if the noexcept has no constexpr, then what we parse next
31602 really is a transaction's body. */
31603 parser->in_transaction = this_in;
31605 /* Parse a noexcept specification. */
31606 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31607 true);
31609 if (!noex || !noex_expr
31610 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31612 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31614 expr = cp_parser_expression (parser);
31615 expr = finish_parenthesized_expr (expr);
31617 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31619 else
31621 /* The only expression that is available got parsed for the noexcept
31622 already. noexcept is true then. */
31623 expr = noex;
31624 noex = boolean_true_node;
31627 expr = build_transaction_expr (token->location, expr, this_in, noex);
31628 parser->in_transaction = old_in;
31630 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31631 return error_mark_node;
31633 return (flag_tm ? expr : error_mark_node);
31636 /* Parse a function-transaction-block.
31638 function-transaction-block:
31639 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31640 function-body
31641 __transaction_atomic txn-attribute[opt] function-try-block
31642 __transaction_relaxed ctor-initializer[opt] function-body
31643 __transaction_relaxed function-try-block
31646 static bool
31647 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31649 unsigned char old_in = parser->in_transaction;
31650 unsigned char new_in = 1;
31651 tree compound_stmt, stmt, attrs;
31652 bool ctor_initializer_p;
31653 cp_token *token;
31655 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31656 || keyword == RID_TRANSACTION_RELAXED);
31657 token = cp_parser_require_keyword (parser, keyword,
31658 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31659 : RT_TRANSACTION_RELAXED));
31660 gcc_assert (token != NULL);
31662 if (keyword == RID_TRANSACTION_RELAXED)
31663 new_in |= TM_STMT_ATTR_RELAXED;
31664 else
31666 attrs = cp_parser_txn_attribute_opt (parser);
31667 if (attrs)
31668 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31671 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31673 parser->in_transaction = new_in;
31675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31676 ctor_initializer_p = cp_parser_function_try_block (parser);
31677 else
31678 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31679 (parser, /*in_function_try_block=*/false);
31681 parser->in_transaction = old_in;
31683 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31685 return ctor_initializer_p;
31688 /* Parse a __transaction_cancel statement.
31690 cancel-statement:
31691 __transaction_cancel txn-attribute[opt] ;
31692 __transaction_cancel txn-attribute[opt] throw-expression ;
31694 ??? Cancel and throw is not yet implemented. */
31696 static tree
31697 cp_parser_transaction_cancel (cp_parser *parser)
31699 cp_token *token;
31700 bool is_outer = false;
31701 tree stmt, attrs;
31703 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31704 RT_TRANSACTION_CANCEL);
31705 gcc_assert (token != NULL);
31707 attrs = cp_parser_txn_attribute_opt (parser);
31708 if (attrs)
31709 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31711 /* ??? Parse cancel-and-throw here. */
31713 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31715 if (!flag_tm)
31717 error_at (token->location, "%<__transaction_cancel%> without "
31718 "transactional memory support enabled");
31719 return error_mark_node;
31721 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31723 error_at (token->location, "%<__transaction_cancel%> within a "
31724 "%<__transaction_relaxed%>");
31725 return error_mark_node;
31727 else if (is_outer)
31729 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31730 && !is_tm_may_cancel_outer (current_function_decl))
31732 error_at (token->location, "outer %<__transaction_cancel%> not "
31733 "within outer %<__transaction_atomic%>");
31734 error_at (token->location,
31735 " or a %<transaction_may_cancel_outer%> function");
31736 return error_mark_node;
31739 else if (parser->in_transaction == 0)
31741 error_at (token->location, "%<__transaction_cancel%> not within "
31742 "%<__transaction_atomic%>");
31743 return error_mark_node;
31746 stmt = build_tm_abort_call (token->location, is_outer);
31747 add_stmt (stmt);
31749 return stmt;
31752 /* The parser. */
31754 static GTY (()) cp_parser *the_parser;
31757 /* Special handling for the first token or line in the file. The first
31758 thing in the file might be #pragma GCC pch_preprocess, which loads a
31759 PCH file, which is a GC collection point. So we need to handle this
31760 first pragma without benefit of an existing lexer structure.
31762 Always returns one token to the caller in *FIRST_TOKEN. This is
31763 either the true first token of the file, or the first token after
31764 the initial pragma. */
31766 static void
31767 cp_parser_initial_pragma (cp_token *first_token)
31769 tree name = NULL;
31771 cp_lexer_get_preprocessor_token (NULL, first_token);
31772 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31773 return;
31775 cp_lexer_get_preprocessor_token (NULL, first_token);
31776 if (first_token->type == CPP_STRING)
31778 name = first_token->u.value;
31780 cp_lexer_get_preprocessor_token (NULL, first_token);
31781 if (first_token->type != CPP_PRAGMA_EOL)
31782 error_at (first_token->location,
31783 "junk at end of %<#pragma GCC pch_preprocess%>");
31785 else
31786 error_at (first_token->location, "expected string literal");
31788 /* Skip to the end of the pragma. */
31789 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31790 cp_lexer_get_preprocessor_token (NULL, first_token);
31792 /* Now actually load the PCH file. */
31793 if (name)
31794 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31796 /* Read one more token to return to our caller. We have to do this
31797 after reading the PCH file in, since its pointers have to be
31798 live. */
31799 cp_lexer_get_preprocessor_token (NULL, first_token);
31802 /* Parses the grainsize pragma for the _Cilk_for statement.
31803 Syntax:
31804 #pragma cilk grainsize = <VALUE>. */
31806 static void
31807 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31809 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31811 tree exp = cp_parser_binary_expression (parser, false, false,
31812 PREC_NOT_OPERATOR, NULL);
31813 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31814 if (!exp || exp == error_mark_node)
31816 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31817 return;
31820 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31822 cp_parser_cilk_for (parser, exp);
31823 else
31824 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31825 "%<#pragma cilk grainsize%> is not followed by "
31826 "%<_Cilk_for%>");
31827 return;
31829 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31832 /* Normal parsing of a pragma token. Here we can (and must) use the
31833 regular lexer. */
31835 static bool
31836 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31838 cp_token *pragma_tok;
31839 unsigned int id;
31841 pragma_tok = cp_lexer_consume_token (parser->lexer);
31842 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31843 parser->lexer->in_pragma = true;
31845 id = pragma_tok->pragma_kind;
31846 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31847 cp_ensure_no_omp_declare_simd (parser);
31848 switch (id)
31850 case PRAGMA_GCC_PCH_PREPROCESS:
31851 error_at (pragma_tok->location,
31852 "%<#pragma GCC pch_preprocess%> must be first");
31853 break;
31855 case PRAGMA_OMP_BARRIER:
31856 switch (context)
31858 case pragma_compound:
31859 cp_parser_omp_barrier (parser, pragma_tok);
31860 return false;
31861 case pragma_stmt:
31862 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31863 "used in compound statements");
31864 break;
31865 default:
31866 goto bad_stmt;
31868 break;
31870 case PRAGMA_OMP_FLUSH:
31871 switch (context)
31873 case pragma_compound:
31874 cp_parser_omp_flush (parser, pragma_tok);
31875 return false;
31876 case pragma_stmt:
31877 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31878 "used in compound statements");
31879 break;
31880 default:
31881 goto bad_stmt;
31883 break;
31885 case PRAGMA_OMP_TASKWAIT:
31886 switch (context)
31888 case pragma_compound:
31889 cp_parser_omp_taskwait (parser, pragma_tok);
31890 return false;
31891 case pragma_stmt:
31892 error_at (pragma_tok->location,
31893 "%<#pragma omp taskwait%> may only be "
31894 "used in compound statements");
31895 break;
31896 default:
31897 goto bad_stmt;
31899 break;
31901 case PRAGMA_OMP_TASKYIELD:
31902 switch (context)
31904 case pragma_compound:
31905 cp_parser_omp_taskyield (parser, pragma_tok);
31906 return false;
31907 case pragma_stmt:
31908 error_at (pragma_tok->location,
31909 "%<#pragma omp taskyield%> may only be "
31910 "used in compound statements");
31911 break;
31912 default:
31913 goto bad_stmt;
31915 break;
31917 case PRAGMA_OMP_CANCEL:
31918 switch (context)
31920 case pragma_compound:
31921 cp_parser_omp_cancel (parser, pragma_tok);
31922 return false;
31923 case pragma_stmt:
31924 error_at (pragma_tok->location,
31925 "%<#pragma omp cancel%> may only be "
31926 "used in compound statements");
31927 break;
31928 default:
31929 goto bad_stmt;
31931 break;
31933 case PRAGMA_OMP_CANCELLATION_POINT:
31934 switch (context)
31936 case pragma_compound:
31937 cp_parser_omp_cancellation_point (parser, pragma_tok);
31938 return false;
31939 case pragma_stmt:
31940 error_at (pragma_tok->location,
31941 "%<#pragma omp cancellation point%> may only be "
31942 "used in compound statements");
31943 break;
31944 default:
31945 goto bad_stmt;
31947 break;
31949 case PRAGMA_OMP_THREADPRIVATE:
31950 cp_parser_omp_threadprivate (parser, pragma_tok);
31951 return false;
31953 case PRAGMA_OMP_DECLARE_REDUCTION:
31954 cp_parser_omp_declare (parser, pragma_tok, context);
31955 return false;
31957 case PRAGMA_OMP_ATOMIC:
31958 case PRAGMA_OMP_CRITICAL:
31959 case PRAGMA_OMP_DISTRIBUTE:
31960 case PRAGMA_OMP_FOR:
31961 case PRAGMA_OMP_MASTER:
31962 case PRAGMA_OMP_ORDERED:
31963 case PRAGMA_OMP_PARALLEL:
31964 case PRAGMA_OMP_SECTIONS:
31965 case PRAGMA_OMP_SIMD:
31966 case PRAGMA_OMP_SINGLE:
31967 case PRAGMA_OMP_TASK:
31968 case PRAGMA_OMP_TASKGROUP:
31969 case PRAGMA_OMP_TEAMS:
31970 if (context != pragma_stmt && context != pragma_compound)
31971 goto bad_stmt;
31972 cp_parser_omp_construct (parser, pragma_tok);
31973 return true;
31975 case PRAGMA_OMP_TARGET:
31976 return cp_parser_omp_target (parser, pragma_tok, context);
31978 case PRAGMA_OMP_END_DECLARE_TARGET:
31979 cp_parser_omp_end_declare_target (parser, pragma_tok);
31980 return false;
31982 case PRAGMA_OMP_SECTION:
31983 error_at (pragma_tok->location,
31984 "%<#pragma omp section%> may only be used in "
31985 "%<#pragma omp sections%> construct");
31986 break;
31988 case PRAGMA_IVDEP:
31990 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31991 cp_token *tok;
31992 tok = cp_lexer_peek_token (the_parser->lexer);
31993 if (tok->type != CPP_KEYWORD
31994 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31995 && tok->keyword != RID_DO))
31997 cp_parser_error (parser, "for, while or do statement expected");
31998 return false;
32000 cp_parser_iteration_statement (parser, true);
32001 return true;
32004 case PRAGMA_CILK_SIMD:
32005 if (context == pragma_external)
32007 error_at (pragma_tok->location,
32008 "%<#pragma simd%> must be inside a function");
32009 break;
32011 cp_parser_cilk_simd (parser, pragma_tok);
32012 return true;
32014 case PRAGMA_CILK_GRAINSIZE:
32015 if (context == pragma_external)
32017 error_at (pragma_tok->location,
32018 "%<#pragma cilk grainsize%> must be inside a function");
32019 break;
32022 /* Ignore the pragma if Cilk Plus is not enabled. */
32023 if (flag_cilkplus)
32025 cp_parser_cilk_grainsize (parser, pragma_tok);
32026 return true;
32028 else
32030 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32031 "%<#pragma cilk grainsize%>");
32032 break;
32035 default:
32036 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32037 c_invoke_pragma_handler (id);
32038 break;
32040 bad_stmt:
32041 cp_parser_error (parser, "expected declaration specifiers");
32042 break;
32045 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32046 return false;
32049 /* The interface the pragma parsers have to the lexer. */
32051 enum cpp_ttype
32052 pragma_lex (tree *value)
32054 cp_token *tok;
32055 enum cpp_ttype ret;
32057 tok = cp_lexer_peek_token (the_parser->lexer);
32059 ret = tok->type;
32060 *value = tok->u.value;
32062 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32063 ret = CPP_EOF;
32064 else if (ret == CPP_STRING)
32065 *value = cp_parser_string_literal (the_parser, false, false);
32066 else
32068 cp_lexer_consume_token (the_parser->lexer);
32069 if (ret == CPP_KEYWORD)
32070 ret = CPP_NAME;
32073 return ret;
32077 /* External interface. */
32079 /* Parse one entire translation unit. */
32081 void
32082 c_parse_file (void)
32084 static bool already_called = false;
32086 if (already_called)
32087 fatal_error ("inter-module optimizations not implemented for C++");
32088 already_called = true;
32090 the_parser = cp_parser_new ();
32091 push_deferring_access_checks (flag_access_control
32092 ? dk_no_deferred : dk_no_check);
32093 cp_parser_translation_unit (the_parser);
32094 the_parser = NULL;
32097 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32098 vectorlength clause:
32099 Syntax:
32100 vectorlength ( constant-expression ) */
32102 static tree
32103 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32104 bool is_simd_fn)
32106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32107 tree expr;
32108 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32109 safelen clause. Thus, vectorlength is represented as OMP 4.0
32110 safelen. For SIMD-enabled function it is represented by OMP 4.0
32111 simdlen. */
32112 if (!is_simd_fn)
32113 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32114 loc);
32115 else
32116 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32117 loc);
32119 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32120 return error_mark_node;
32122 expr = cp_parser_constant_expression (parser);
32123 expr = maybe_constant_value (expr);
32125 /* If expr == error_mark_node, then don't emit any errors nor
32126 create a clause. if any of the above functions returns
32127 error mark node then they would have emitted an error message. */
32128 if (expr == error_mark_node)
32130 else if (!TREE_TYPE (expr)
32131 || !TREE_CONSTANT (expr)
32132 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32133 error_at (loc, "vectorlength must be an integer constant");
32134 else if (TREE_CONSTANT (expr)
32135 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32136 error_at (loc, "vectorlength must be a power of 2");
32137 else
32139 tree c;
32140 if (!is_simd_fn)
32142 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32143 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32144 OMP_CLAUSE_CHAIN (c) = clauses;
32145 clauses = c;
32147 else
32149 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32150 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32151 OMP_CLAUSE_CHAIN (c) = clauses;
32152 clauses = c;
32156 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32157 return error_mark_node;
32158 return clauses;
32161 /* Handles the Cilk Plus #pragma simd linear clause.
32162 Syntax:
32163 linear ( simd-linear-variable-list )
32165 simd-linear-variable-list:
32166 simd-linear-variable
32167 simd-linear-variable-list , simd-linear-variable
32169 simd-linear-variable:
32170 id-expression
32171 id-expression : simd-linear-step
32173 simd-linear-step:
32174 conditional-expression */
32176 static tree
32177 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32179 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32181 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32182 return clauses;
32183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32185 cp_parser_error (parser, "expected identifier");
32186 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32187 return error_mark_node;
32190 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32191 parser->colon_corrects_to_scope_p = false;
32192 while (1)
32194 cp_token *token = cp_lexer_peek_token (parser->lexer);
32195 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32197 cp_parser_error (parser, "expected variable-name");
32198 clauses = error_mark_node;
32199 break;
32202 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32203 false, false);
32204 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32205 token->location);
32206 if (decl == error_mark_node)
32208 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32209 token->location);
32210 clauses = error_mark_node;
32212 else
32214 tree e = NULL_TREE;
32215 tree step_size = integer_one_node;
32217 /* If present, parse the linear step. Otherwise, assume the default
32218 value of 1. */
32219 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32221 cp_lexer_consume_token (parser->lexer);
32223 e = cp_parser_assignment_expression (parser);
32224 e = maybe_constant_value (e);
32226 if (e == error_mark_node)
32228 /* If an error has occurred, then the whole pragma is
32229 considered ill-formed. Thus, no reason to keep
32230 parsing. */
32231 clauses = error_mark_node;
32232 break;
32234 else if (type_dependent_expression_p (e)
32235 || value_dependent_expression_p (e)
32236 || (TREE_TYPE (e)
32237 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32238 && (TREE_CONSTANT (e)
32239 || DECL_P (e))))
32240 step_size = e;
32241 else
32242 cp_parser_error (parser,
32243 "step size must be an integer constant "
32244 "expression or an integer variable");
32247 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32248 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32249 OMP_CLAUSE_DECL (l) = decl;
32250 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32251 OMP_CLAUSE_CHAIN (l) = clauses;
32252 clauses = l;
32254 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32255 cp_lexer_consume_token (parser->lexer);
32256 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32257 break;
32258 else
32260 error_at (cp_lexer_peek_token (parser->lexer)->location,
32261 "expected %<,%> or %<)%> after %qE", decl);
32262 clauses = error_mark_node;
32263 break;
32266 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32267 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32268 return clauses;
32271 /* Returns the name of the next clause. If the clause is not
32272 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32273 token is not consumed. Otherwise, the appropriate enum from the
32274 pragma_simd_clause is returned and the token is consumed. */
32276 static pragma_omp_clause
32277 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32279 pragma_omp_clause clause_type;
32280 cp_token *token = cp_lexer_peek_token (parser->lexer);
32282 if (token->keyword == RID_PRIVATE)
32283 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32284 else if (!token->u.value || token->type != CPP_NAME)
32285 return PRAGMA_CILK_CLAUSE_NONE;
32286 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32287 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32288 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32289 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32290 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32291 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32292 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32293 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32294 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32295 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32296 else
32297 return PRAGMA_CILK_CLAUSE_NONE;
32299 cp_lexer_consume_token (parser->lexer);
32300 return clause_type;
32303 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32305 static tree
32306 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32308 tree clauses = NULL_TREE;
32310 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32311 && clauses != error_mark_node)
32313 pragma_omp_clause c_kind;
32314 c_kind = cp_parser_cilk_simd_clause_name (parser);
32315 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32316 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32317 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32318 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32319 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32320 /* Use the OpenMP 4.0 equivalent function. */
32321 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32322 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32323 /* Use the OpenMP 4.0 equivalent function. */
32324 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32325 clauses);
32326 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32327 /* Use the OMP 4.0 equivalent function. */
32328 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32329 clauses);
32330 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32331 /* Use the OMP 4.0 equivalent function. */
32332 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32333 else
32335 clauses = error_mark_node;
32336 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32337 break;
32341 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32343 if (clauses == error_mark_node)
32344 return error_mark_node;
32345 else
32346 return c_finish_cilk_clauses (clauses);
32349 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32351 static void
32352 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32354 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32356 if (clauses == error_mark_node)
32357 return;
32359 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32361 error_at (cp_lexer_peek_token (parser->lexer)->location,
32362 "for statement expected");
32363 return;
32366 tree sb = begin_omp_structured_block ();
32367 int save = cp_parser_begin_omp_structured_block (parser);
32368 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32369 if (ret)
32370 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32371 cp_parser_end_omp_structured_block (parser, save);
32372 add_stmt (finish_omp_structured_block (sb));
32375 /* Main entry-point for parsing Cilk Plus _Cilk_for
32376 loops. The return value is error_mark_node
32377 when errors happen and CILK_FOR tree on success. */
32379 static tree
32380 cp_parser_cilk_for (cp_parser *parser, tree grain)
32382 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32383 gcc_unreachable ();
32385 tree sb = begin_omp_structured_block ();
32386 int save = cp_parser_begin_omp_structured_block (parser);
32388 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32389 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32390 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32391 clauses = finish_omp_clauses (clauses);
32393 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32394 if (ret)
32395 cpp_validate_cilk_plus_loop (ret);
32396 else
32397 ret = error_mark_node;
32399 cp_parser_end_omp_structured_block (parser, save);
32400 add_stmt (finish_omp_structured_block (sb));
32401 return ret;
32404 /* Create an identifier for a generic parameter type (a synthesized
32405 template parameter implied by `auto' or a concept identifier). */
32407 static GTY(()) int generic_parm_count;
32408 static tree
32409 make_generic_type_name ()
32411 char buf[32];
32412 sprintf (buf, "auto:%d", ++generic_parm_count);
32413 return get_identifier (buf);
32416 /* Predicate that behaves as is_auto_or_concept but matches the parent
32417 node of the generic type rather than the generic type itself. This
32418 allows for type transformation in add_implicit_template_parms. */
32420 static inline bool
32421 tree_type_is_auto_or_concept (const_tree t)
32423 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32426 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32427 (creating a new template parameter list if necessary). Returns the newly
32428 created template type parm. */
32430 tree
32431 synthesize_implicit_template_parm (cp_parser *parser)
32433 gcc_assert (current_binding_level->kind == sk_function_parms);
32435 /* We are either continuing a function template that already contains implicit
32436 template parameters, creating a new fully-implicit function template, or
32437 extending an existing explicit function template with implicit template
32438 parameters. */
32440 cp_binding_level *const entry_scope = current_binding_level;
32442 bool become_template = false;
32443 cp_binding_level *parent_scope = 0;
32445 if (parser->implicit_template_scope)
32447 gcc_assert (parser->implicit_template_parms);
32449 current_binding_level = parser->implicit_template_scope;
32451 else
32453 /* Roll back to the existing template parameter scope (in the case of
32454 extending an explicit function template) or introduce a new template
32455 parameter scope ahead of the function parameter scope (or class scope
32456 in the case of out-of-line member definitions). The function scope is
32457 added back after template parameter synthesis below. */
32459 cp_binding_level *scope = entry_scope;
32461 while (scope->kind == sk_function_parms)
32463 parent_scope = scope;
32464 scope = scope->level_chain;
32466 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32468 /* If not defining a class, then any class scope is a scope level in
32469 an out-of-line member definition. In this case simply wind back
32470 beyond the first such scope to inject the template parameter list.
32471 Otherwise wind back to the class being defined. The latter can
32472 occur in class member friend declarations such as:
32474 class A {
32475 void foo (auto);
32477 class B {
32478 friend void A::foo (auto);
32481 The template parameter list synthesized for the friend declaration
32482 must be injected in the scope of 'B'. This can also occur in
32483 erroneous cases such as:
32485 struct A {
32486 struct B {
32487 void foo (auto);
32489 void B::foo (auto) {}
32492 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32493 but, nevertheless, the template parameter list synthesized for the
32494 declarator should be injected into the scope of 'A' as if the
32495 ill-formed template was specified explicitly. */
32497 while (scope->kind == sk_class && !scope->defining_class_p)
32499 parent_scope = scope;
32500 scope = scope->level_chain;
32504 current_binding_level = scope;
32506 if (scope->kind != sk_template_parms
32507 || !function_being_declared_is_template_p (parser))
32509 /* Introduce a new template parameter list for implicit template
32510 parameters. */
32512 become_template = true;
32514 parser->implicit_template_scope
32515 = begin_scope (sk_template_parms, NULL);
32517 ++processing_template_decl;
32519 parser->fully_implicit_function_template_p = true;
32520 ++parser->num_template_parameter_lists;
32522 else
32524 /* Synthesize implicit template parameters at the end of the explicit
32525 template parameter list. */
32527 gcc_assert (current_template_parms);
32529 parser->implicit_template_scope = scope;
32531 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32532 parser->implicit_template_parms
32533 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32537 /* Synthesize a new template parameter and track the current template
32538 parameter chain with implicit_template_parms. */
32540 tree synth_id = make_generic_type_name ();
32541 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32542 synth_id);
32543 tree new_parm
32544 = process_template_parm (parser->implicit_template_parms,
32545 input_location,
32546 build_tree_list (NULL_TREE, synth_tmpl_parm),
32547 /*non_type=*/false,
32548 /*param_pack=*/false);
32551 if (parser->implicit_template_parms)
32552 parser->implicit_template_parms
32553 = TREE_CHAIN (parser->implicit_template_parms);
32554 else
32555 parser->implicit_template_parms = new_parm;
32557 tree new_type = TREE_TYPE (getdecls ());
32559 /* If creating a fully implicit function template, start the new implicit
32560 template parameter list with this synthesized type, otherwise grow the
32561 current template parameter list. */
32563 if (become_template)
32565 parent_scope->level_chain = current_binding_level;
32567 tree new_parms = make_tree_vec (1);
32568 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32569 current_template_parms = tree_cons (size_int (processing_template_decl),
32570 new_parms, current_template_parms);
32572 else
32574 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32575 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32576 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32577 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32580 current_binding_level = entry_scope;
32582 return new_type;
32585 /* Finish the declaration of a fully implicit function template. Such a
32586 template has no explicit template parameter list so has not been through the
32587 normal template head and tail processing. synthesize_implicit_template_parm
32588 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32589 provided if the declaration is a class member such that its template
32590 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32591 form is returned. Otherwise NULL_TREE is returned. */
32593 tree
32594 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32596 gcc_assert (parser->fully_implicit_function_template_p);
32598 if (member_decl_opt && member_decl_opt != error_mark_node
32599 && DECL_VIRTUAL_P (member_decl_opt))
32601 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32602 "implicit templates may not be %<virtual%>");
32603 DECL_VIRTUAL_P (member_decl_opt) = false;
32606 if (member_decl_opt)
32607 member_decl_opt = finish_member_template_decl (member_decl_opt);
32608 end_template_decl ();
32610 parser->fully_implicit_function_template_p = false;
32611 --parser->num_template_parameter_lists;
32613 return member_decl_opt;
32616 #include "gt-cp-parser.h"