Merge from trunk
[official-gcc.git] / gcc / cp / parser.c
blob20d758e888bf8f5efd061c0ad145fde96b5c78ca
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 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 "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "print-tree.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "trans-mem.h"
41 #include "cp-tree.h"
42 #include "intl.h"
43 #include "c-family/c-pragma.h"
44 #include "decl.h"
45 #include "flags.h"
46 #include "diagnostic-core.h"
47 #include "target.h"
48 #include "hash-map.h"
49 #include "is-a.h"
50 #include "plugin-api.h"
51 #include "hard-reg-set.h"
52 #include "input.h"
53 #include "function.h"
54 #include "ipa-ref.h"
55 #include "cgraph.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-objc.h"
58 #include "plugin.h"
59 #include "tree-pretty-print.h"
60 #include "parser.h"
61 #include "type-utils.h"
62 #include "omp-low.h"
63 #include "gomp-constants.h"
67 namespace {
68 // A helper function. Returns the object pointed to by P
69 // and sets P to NULL.
70 template<typename T>
71 inline T* release(T*& p)
73 T* q = p;
74 p = NULL;
75 return q;
77 } // namespace
80 /* The lexer. */
82 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
83 and c-lex.c) and the C++ parser. */
85 static cp_token eof_token =
87 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
90 /* The various kinds of non integral constant we encounter. */
91 typedef enum non_integral_constant {
92 NIC_NONE,
93 /* floating-point literal */
94 NIC_FLOAT,
95 /* %<this%> */
96 NIC_THIS,
97 /* %<__FUNCTION__%> */
98 NIC_FUNC_NAME,
99 /* %<__PRETTY_FUNCTION__%> */
100 NIC_PRETTY_FUNC,
101 /* %<__func__%> */
102 NIC_C99_FUNC,
103 /* "%<va_arg%> */
104 NIC_VA_ARG,
105 /* a cast */
106 NIC_CAST,
107 /* %<typeid%> operator */
108 NIC_TYPEID,
109 /* non-constant compound literals */
110 NIC_NCC,
111 /* a function call */
112 NIC_FUNC_CALL,
113 /* an increment */
114 NIC_INC,
115 /* an decrement */
116 NIC_DEC,
117 /* an array reference */
118 NIC_ARRAY_REF,
119 /* %<->%> */
120 NIC_ARROW,
121 /* %<.%> */
122 NIC_POINT,
123 /* the address of a label */
124 NIC_ADDR_LABEL,
125 /* %<*%> */
126 NIC_STAR,
127 /* %<&%> */
128 NIC_ADDR,
129 /* %<++%> */
130 NIC_PREINCREMENT,
131 /* %<--%> */
132 NIC_PREDECREMENT,
133 /* %<new%> */
134 NIC_NEW,
135 /* %<delete%> */
136 NIC_DEL,
137 /* calls to overloaded operators */
138 NIC_OVERLOADED,
139 /* an assignment */
140 NIC_ASSIGNMENT,
141 /* a comma operator */
142 NIC_COMMA,
143 /* a call to a constructor */
144 NIC_CONSTRUCTOR,
145 /* a transaction expression */
146 NIC_TRANSACTION
147 } non_integral_constant;
149 /* The various kinds of errors about name-lookup failing. */
150 typedef enum name_lookup_error {
151 /* NULL */
152 NLE_NULL,
153 /* is not a type */
154 NLE_TYPE,
155 /* is not a class or namespace */
156 NLE_CXX98,
157 /* is not a class, namespace, or enumeration */
158 NLE_NOT_CXX98
159 } name_lookup_error;
161 /* The various kinds of required token */
162 typedef enum required_token {
163 RT_NONE,
164 RT_SEMICOLON, /* ';' */
165 RT_OPEN_PAREN, /* '(' */
166 RT_CLOSE_BRACE, /* '}' */
167 RT_OPEN_BRACE, /* '{' */
168 RT_CLOSE_SQUARE, /* ']' */
169 RT_OPEN_SQUARE, /* '[' */
170 RT_COMMA, /* ',' */
171 RT_SCOPE, /* '::' */
172 RT_LESS, /* '<' */
173 RT_GREATER, /* '>' */
174 RT_EQ, /* '=' */
175 RT_ELLIPSIS, /* '...' */
176 RT_MULT, /* '*' */
177 RT_COMPL, /* '~' */
178 RT_COLON, /* ':' */
179 RT_COLON_SCOPE, /* ':' or '::' */
180 RT_CLOSE_PAREN, /* ')' */
181 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
182 RT_PRAGMA_EOL, /* end of line */
183 RT_NAME, /* identifier */
185 /* The type is CPP_KEYWORD */
186 RT_NEW, /* new */
187 RT_DELETE, /* delete */
188 RT_RETURN, /* return */
189 RT_WHILE, /* while */
190 RT_EXTERN, /* extern */
191 RT_STATIC_ASSERT, /* static_assert */
192 RT_DECLTYPE, /* decltype */
193 RT_OPERATOR, /* operator */
194 RT_CLASS, /* class */
195 RT_TEMPLATE, /* template */
196 RT_NAMESPACE, /* namespace */
197 RT_USING, /* using */
198 RT_ASM, /* asm */
199 RT_TRY, /* try */
200 RT_CATCH, /* catch */
201 RT_THROW, /* throw */
202 RT_LABEL, /* __label__ */
203 RT_AT_TRY, /* @try */
204 RT_AT_SYNCHRONIZED, /* @synchronized */
205 RT_AT_THROW, /* @throw */
207 RT_SELECT, /* selection-statement */
208 RT_INTERATION, /* iteration-statement */
209 RT_JUMP, /* jump-statement */
210 RT_CLASS_KEY, /* class-key */
211 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
212 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
213 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
214 RT_TRANSACTION_CANCEL /* __transaction_cancel */
215 } required_token;
217 /* Prototypes. */
219 static cp_lexer *cp_lexer_new_main
220 (void);
221 static cp_lexer *cp_lexer_new_from_tokens
222 (cp_token_cache *tokens);
223 static void cp_lexer_destroy
224 (cp_lexer *);
225 static int cp_lexer_saving_tokens
226 (const cp_lexer *);
227 static cp_token *cp_lexer_token_at
228 (cp_lexer *, cp_token_position);
229 static void cp_lexer_get_preprocessor_token
230 (cp_lexer *, cp_token *);
231 static inline cp_token *cp_lexer_peek_token
232 (cp_lexer *);
233 static cp_token *cp_lexer_peek_nth_token
234 (cp_lexer *, size_t);
235 static inline bool cp_lexer_next_token_is
236 (cp_lexer *, enum cpp_ttype);
237 static bool cp_lexer_next_token_is_not
238 (cp_lexer *, enum cpp_ttype);
239 static bool cp_lexer_next_token_is_keyword
240 (cp_lexer *, enum rid);
241 static cp_token *cp_lexer_consume_token
242 (cp_lexer *);
243 static void cp_lexer_purge_token
244 (cp_lexer *);
245 static void cp_lexer_purge_tokens_after
246 (cp_lexer *, cp_token_position);
247 static void cp_lexer_save_tokens
248 (cp_lexer *);
249 static void cp_lexer_commit_tokens
250 (cp_lexer *);
251 static void cp_lexer_rollback_tokens
252 (cp_lexer *);
253 static void cp_lexer_print_token
254 (FILE *, cp_token *);
255 static inline bool cp_lexer_debugging_p
256 (cp_lexer *);
257 static void cp_lexer_start_debugging
258 (cp_lexer *) ATTRIBUTE_UNUSED;
259 static void cp_lexer_stop_debugging
260 (cp_lexer *) ATTRIBUTE_UNUSED;
262 static cp_token_cache *cp_token_cache_new
263 (cp_token *, cp_token *);
265 static void cp_parser_initial_pragma
266 (cp_token *);
268 static tree cp_literal_operator_id
269 (const char *);
271 static void cp_parser_cilk_simd
272 (cp_parser *, cp_token *);
273 static tree cp_parser_cilk_for
274 (cp_parser *, tree);
275 static bool cp_parser_omp_declare_reduction_exprs
276 (tree, cp_parser *);
277 static tree cp_parser_cilk_simd_vectorlength
278 (cp_parser *, tree, bool);
280 /* Manifest constants. */
281 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
282 #define CP_SAVED_TOKEN_STACK 5
284 /* Variables. */
286 /* The stream to which debugging output should be written. */
287 static FILE *cp_lexer_debug_stream;
289 /* Nonzero if we are parsing an unevaluated operand: an operand to
290 sizeof, typeof, or alignof. */
291 int cp_unevaluated_operand;
293 /* Dump up to NUM tokens in BUFFER to FILE starting with token
294 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
295 first token in BUFFER. If NUM is 0, dump all the tokens. If
296 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
297 highlighted by surrounding it in [[ ]]. */
299 static void
300 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
301 cp_token *start_token, unsigned num,
302 cp_token *curr_token)
304 unsigned i, nprinted;
305 cp_token *token;
306 bool do_print;
308 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
310 if (buffer == NULL)
311 return;
313 if (num == 0)
314 num = buffer->length ();
316 if (start_token == NULL)
317 start_token = buffer->address ();
319 if (start_token > buffer->address ())
321 cp_lexer_print_token (file, &(*buffer)[0]);
322 fprintf (file, " ... ");
325 do_print = false;
326 nprinted = 0;
327 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
329 if (token == start_token)
330 do_print = true;
332 if (!do_print)
333 continue;
335 nprinted++;
336 if (token == curr_token)
337 fprintf (file, "[[");
339 cp_lexer_print_token (file, token);
341 if (token == curr_token)
342 fprintf (file, "]]");
344 switch (token->type)
346 case CPP_SEMICOLON:
347 case CPP_OPEN_BRACE:
348 case CPP_CLOSE_BRACE:
349 case CPP_EOF:
350 fputc ('\n', file);
351 break;
353 default:
354 fputc (' ', file);
358 if (i == num && i < buffer->length ())
360 fprintf (file, " ... ");
361 cp_lexer_print_token (file, &buffer->last ());
364 fprintf (file, "\n");
368 /* Dump all tokens in BUFFER to stderr. */
370 void
371 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
373 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
376 DEBUG_FUNCTION void
377 debug (vec<cp_token, va_gc> &ref)
379 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
382 DEBUG_FUNCTION void
383 debug (vec<cp_token, va_gc> *ptr)
385 if (ptr)
386 debug (*ptr);
387 else
388 fprintf (stderr, "<nil>\n");
392 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
393 description for T. */
395 static void
396 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
398 if (t)
400 fprintf (file, "%s: ", desc);
401 print_node_brief (file, "", t, 0);
406 /* Dump parser context C to FILE. */
408 static void
409 cp_debug_print_context (FILE *file, cp_parser_context *c)
411 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
412 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
413 print_node_brief (file, "", c->object_type, 0);
414 fprintf (file, "}\n");
418 /* Print the stack of parsing contexts to FILE starting with FIRST. */
420 static void
421 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
423 unsigned i;
424 cp_parser_context *c;
426 fprintf (file, "Parsing context stack:\n");
427 for (i = 0, c = first; c; c = c->next, i++)
429 fprintf (file, "\t#%u: ", i);
430 cp_debug_print_context (file, c);
435 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
437 static void
438 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
440 if (flag)
441 fprintf (file, "%s: true\n", desc);
445 /* Print an unparsed function entry UF to FILE. */
447 static void
448 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
450 unsigned i;
451 cp_default_arg_entry *default_arg_fn;
452 tree fn;
454 fprintf (file, "\tFunctions with default args:\n");
455 for (i = 0;
456 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
457 i++)
459 fprintf (file, "\t\tClass type: ");
460 print_node_brief (file, "", default_arg_fn->class_type, 0);
461 fprintf (file, "\t\tDeclaration: ");
462 print_node_brief (file, "", default_arg_fn->decl, 0);
463 fprintf (file, "\n");
466 fprintf (file, "\n\tFunctions with definitions that require "
467 "post-processing\n\t\t");
468 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
470 print_node_brief (file, "", fn, 0);
471 fprintf (file, " ");
473 fprintf (file, "\n");
475 fprintf (file, "\n\tNon-static data members with initializers that require "
476 "post-processing\n\t\t");
477 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
479 print_node_brief (file, "", fn, 0);
480 fprintf (file, " ");
482 fprintf (file, "\n");
486 /* Print the stack of unparsed member functions S to FILE. */
488 static void
489 cp_debug_print_unparsed_queues (FILE *file,
490 vec<cp_unparsed_functions_entry, va_gc> *s)
492 unsigned i;
493 cp_unparsed_functions_entry *uf;
495 fprintf (file, "Unparsed functions\n");
496 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
498 fprintf (file, "#%u:\n", i);
499 cp_debug_print_unparsed_function (file, uf);
504 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
505 the given PARSER. If FILE is NULL, the output is printed on stderr. */
507 static void
508 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
510 cp_token *next_token, *first_token, *start_token;
512 if (file == NULL)
513 file = stderr;
515 next_token = parser->lexer->next_token;
516 first_token = parser->lexer->buffer->address ();
517 start_token = (next_token > first_token + window_size / 2)
518 ? next_token - window_size / 2
519 : first_token;
520 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
521 next_token);
525 /* Dump debugging information for the given PARSER. If FILE is NULL,
526 the output is printed on stderr. */
528 void
529 cp_debug_parser (FILE *file, cp_parser *parser)
531 const size_t window_size = 20;
532 cp_token *token;
533 expanded_location eloc;
535 if (file == NULL)
536 file = stderr;
538 fprintf (file, "Parser state\n\n");
539 fprintf (file, "Number of tokens: %u\n",
540 vec_safe_length (parser->lexer->buffer));
541 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
542 cp_debug_print_tree_if_set (file, "Object scope",
543 parser->object_scope);
544 cp_debug_print_tree_if_set (file, "Qualifying scope",
545 parser->qualifying_scope);
546 cp_debug_print_context_stack (file, parser->context);
547 cp_debug_print_flag (file, "Allow GNU extensions",
548 parser->allow_gnu_extensions_p);
549 cp_debug_print_flag (file, "'>' token is greater-than",
550 parser->greater_than_is_operator_p);
551 cp_debug_print_flag (file, "Default args allowed in current "
552 "parameter list", parser->default_arg_ok_p);
553 cp_debug_print_flag (file, "Parsing integral constant-expression",
554 parser->integral_constant_expression_p);
555 cp_debug_print_flag (file, "Allow non-constant expression in current "
556 "constant-expression",
557 parser->allow_non_integral_constant_expression_p);
558 cp_debug_print_flag (file, "Seen non-constant expression",
559 parser->non_integral_constant_expression_p);
560 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
561 "current context",
562 parser->local_variables_forbidden_p);
563 cp_debug_print_flag (file, "In unbraced linkage specification",
564 parser->in_unbraced_linkage_specification_p);
565 cp_debug_print_flag (file, "Parsing a declarator",
566 parser->in_declarator_p);
567 cp_debug_print_flag (file, "In template argument list",
568 parser->in_template_argument_list_p);
569 cp_debug_print_flag (file, "Parsing an iteration statement",
570 parser->in_statement & IN_ITERATION_STMT);
571 cp_debug_print_flag (file, "Parsing a switch statement",
572 parser->in_statement & IN_SWITCH_STMT);
573 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
574 parser->in_statement & IN_OMP_BLOCK);
575 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
576 parser->in_statement & IN_CILK_SIMD_FOR);
577 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
578 parser->in_statement & IN_OMP_FOR);
579 cp_debug_print_flag (file, "Parsing an if statement",
580 parser->in_statement & IN_IF_STMT);
581 cp_debug_print_flag (file, "Parsing a type-id in an expression "
582 "context", parser->in_type_id_in_expr_p);
583 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
584 parser->implicit_extern_c);
585 cp_debug_print_flag (file, "String expressions should be translated "
586 "to execution character set",
587 parser->translate_strings_p);
588 cp_debug_print_flag (file, "Parsing function body outside of a "
589 "local class", parser->in_function_body);
590 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
591 parser->colon_corrects_to_scope_p);
592 cp_debug_print_flag (file, "Colon doesn't start a class definition",
593 parser->colon_doesnt_start_class_def_p);
594 if (parser->type_definition_forbidden_message)
595 fprintf (file, "Error message for forbidden type definitions: %s\n",
596 parser->type_definition_forbidden_message);
597 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
598 fprintf (file, "Number of class definitions in progress: %u\n",
599 parser->num_classes_being_defined);
600 fprintf (file, "Number of template parameter lists for the current "
601 "declaration: %u\n", parser->num_template_parameter_lists);
602 cp_debug_parser_tokens (file, parser, window_size);
603 token = parser->lexer->next_token;
604 fprintf (file, "Next token to parse:\n");
605 fprintf (file, "\tToken: ");
606 cp_lexer_print_token (file, token);
607 eloc = expand_location (token->location);
608 fprintf (file, "\n\tFile: %s\n", eloc.file);
609 fprintf (file, "\tLine: %d\n", eloc.line);
610 fprintf (file, "\tColumn: %d\n", eloc.column);
613 DEBUG_FUNCTION void
614 debug (cp_parser &ref)
616 cp_debug_parser (stderr, &ref);
619 DEBUG_FUNCTION void
620 debug (cp_parser *ptr)
622 if (ptr)
623 debug (*ptr);
624 else
625 fprintf (stderr, "<nil>\n");
628 /* Allocate memory for a new lexer object and return it. */
630 static cp_lexer *
631 cp_lexer_alloc (void)
633 cp_lexer *lexer;
635 c_common_no_more_pch ();
637 /* Allocate the memory. */
638 lexer = ggc_cleared_alloc<cp_lexer> ();
640 /* Initially we are not debugging. */
641 lexer->debugging_p = false;
643 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
645 /* Create the buffer. */
646 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
648 return lexer;
652 /* Create a new main C++ lexer, the lexer that gets tokens from the
653 preprocessor. */
655 static cp_lexer *
656 cp_lexer_new_main (void)
658 cp_lexer *lexer;
659 cp_token token;
661 /* It's possible that parsing the first pragma will load a PCH file,
662 which is a GC collection point. So we have to do that before
663 allocating any memory. */
664 cp_parser_initial_pragma (&token);
666 lexer = cp_lexer_alloc ();
668 /* Put the first token in the buffer. */
669 lexer->buffer->quick_push (token);
671 /* Get the remaining tokens from the preprocessor. */
672 while (token.type != CPP_EOF)
674 cp_lexer_get_preprocessor_token (lexer, &token);
675 vec_safe_push (lexer->buffer, token);
678 lexer->last_token = lexer->buffer->address ()
679 + lexer->buffer->length ()
680 - 1;
681 lexer->next_token = lexer->buffer->length ()
682 ? lexer->buffer->address ()
683 : &eof_token;
685 /* Subsequent preprocessor diagnostics should use compiler
686 diagnostic functions to get the compiler source location. */
687 done_lexing = true;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Create a new lexer whose token stream is primed with the tokens in
694 CACHE. When these tokens are exhausted, no new tokens will be read. */
696 static cp_lexer *
697 cp_lexer_new_from_tokens (cp_token_cache *cache)
699 cp_token *first = cache->first;
700 cp_token *last = cache->last;
701 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
703 /* We do not own the buffer. */
704 lexer->buffer = NULL;
705 lexer->next_token = first == last ? &eof_token : first;
706 lexer->last_token = last;
708 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
710 /* Initially we are not debugging. */
711 lexer->debugging_p = false;
713 gcc_assert (!lexer->next_token->purged_p);
714 return lexer;
717 /* Frees all resources associated with LEXER. */
719 static void
720 cp_lexer_destroy (cp_lexer *lexer)
722 vec_free (lexer->buffer);
723 lexer->saved_tokens.release ();
724 ggc_free (lexer);
727 /* Returns nonzero if debugging information should be output. */
729 static inline bool
730 cp_lexer_debugging_p (cp_lexer *lexer)
732 return lexer->debugging_p;
736 static inline cp_token_position
737 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
739 gcc_assert (!previous_p || lexer->next_token != &eof_token);
741 return lexer->next_token - previous_p;
744 static inline cp_token *
745 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
747 return pos;
750 static inline void
751 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
753 lexer->next_token = cp_lexer_token_at (lexer, pos);
756 static inline cp_token_position
757 cp_lexer_previous_token_position (cp_lexer *lexer)
759 if (lexer->next_token == &eof_token)
760 return lexer->last_token - 1;
761 else
762 return cp_lexer_token_position (lexer, true);
765 static inline cp_token *
766 cp_lexer_previous_token (cp_lexer *lexer)
768 cp_token_position tp = cp_lexer_previous_token_position (lexer);
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->pragma_kind = PRAGMA_NONE;
797 token->purged_p = false;
798 token->error_reported = false;
800 /* On some systems, some header files are surrounded by an
801 implicit extern "C" block. Set a flag in the token if it
802 comes from such a header. */
803 is_extern_c += pending_lang_change;
804 pending_lang_change = 0;
805 token->implicit_extern_c = is_extern_c > 0;
807 /* Check to see if this token is a keyword. */
808 if (token->type == CPP_NAME)
810 if (C_IS_RESERVED_WORD (token->u.value))
812 /* Mark this token as a keyword. */
813 token->type = CPP_KEYWORD;
814 /* Record which keyword. */
815 token->keyword = C_RID_CODE (token->u.value);
817 else
819 if (warn_cxx0x_compat
820 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
821 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
823 /* Warn about the C++0x keyword (but still treat it as
824 an identifier). */
825 warning (OPT_Wc__0x_compat,
826 "identifier %qE is a keyword in C++11",
827 token->u.value);
829 /* Clear out the C_RID_CODE so we don't warn about this
830 particular identifier-turned-keyword again. */
831 C_SET_RID_CODE (token->u.value, RID_MAX);
834 token->keyword = RID_MAX;
837 else if (token->type == CPP_AT_NAME)
839 /* This only happens in Objective-C++; it must be a keyword. */
840 token->type = CPP_KEYWORD;
841 switch (C_RID_CODE (token->u.value))
843 /* Replace 'class' with '@class', 'private' with '@private',
844 etc. This prevents confusion with the C++ keyword
845 'class', and makes the tokens consistent with other
846 Objective-C 'AT' keywords. For example '@class' is
847 reported as RID_AT_CLASS which is consistent with
848 '@synchronized', which is reported as
849 RID_AT_SYNCHRONIZED.
851 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
852 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
853 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
854 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
855 case RID_THROW: token->keyword = RID_AT_THROW; break;
856 case RID_TRY: token->keyword = RID_AT_TRY; break;
857 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
861 else if (token->type == CPP_PRAGMA)
863 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
864 token->pragma_kind = ((enum pragma_kind)
865 TREE_INT_CST_LOW (token->u.value));
866 token->u.value = NULL_TREE;
870 /* Update the globals input_location and the input file stack from TOKEN. */
871 static inline void
872 cp_lexer_set_source_position_from_token (cp_token *token)
874 if (token->type != CPP_EOF)
876 input_location = token->location;
880 /* Update the globals input_location and the input file stack from LEXER. */
881 static inline void
882 cp_lexer_set_source_position (cp_lexer *lexer)
884 cp_token *token = cp_lexer_peek_token (lexer);
885 cp_lexer_set_source_position_from_token (token);
888 /* Return a pointer to the next token in the token stream, but do not
889 consume it. */
891 static inline cp_token *
892 cp_lexer_peek_token (cp_lexer *lexer)
894 if (cp_lexer_debugging_p (lexer))
896 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
897 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
898 putc ('\n', cp_lexer_debug_stream);
900 return lexer->next_token;
903 /* Return true if the next token has the indicated TYPE. */
905 static inline bool
906 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
908 return cp_lexer_peek_token (lexer)->type == type;
911 /* Return true if the next token does not have the indicated TYPE. */
913 static inline bool
914 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
916 return !cp_lexer_next_token_is (lexer, type);
919 /* Return true if the next token is the indicated KEYWORD. */
921 static inline bool
922 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
924 return cp_lexer_peek_token (lexer)->keyword == keyword;
927 static inline bool
928 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
930 return cp_lexer_peek_nth_token (lexer, n)->type == type;
933 static inline bool
934 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
936 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
939 /* Return true if the next token is not the indicated KEYWORD. */
941 static inline bool
942 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
944 return cp_lexer_peek_token (lexer)->keyword != keyword;
947 /* Return true if the next token is a keyword for a decl-specifier. */
949 static bool
950 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
952 cp_token *token;
954 token = cp_lexer_peek_token (lexer);
955 switch (token->keyword)
957 /* auto specifier: storage-class-specifier in C++,
958 simple-type-specifier in C++0x. */
959 case RID_AUTO:
960 /* Storage classes. */
961 case RID_REGISTER:
962 case RID_STATIC:
963 case RID_EXTERN:
964 case RID_MUTABLE:
965 case RID_THREAD:
966 /* Elaborated type specifiers. */
967 case RID_ENUM:
968 case RID_CLASS:
969 case RID_STRUCT:
970 case RID_UNION:
971 case RID_TYPENAME:
972 /* Simple type specifiers. */
973 case RID_CHAR:
974 case RID_CHAR16:
975 case RID_CHAR32:
976 case RID_WCHAR:
977 case RID_BOOL:
978 case RID_SHORT:
979 case RID_INT:
980 case RID_LONG:
981 case RID_SIGNED:
982 case RID_UNSIGNED:
983 case RID_FLOAT:
984 case RID_DOUBLE:
985 case RID_VOID:
986 /* GNU extensions. */
987 case RID_ATTRIBUTE:
988 case RID_TYPEOF:
989 /* C++0x extensions. */
990 case RID_DECLTYPE:
991 case RID_UNDERLYING_TYPE:
992 return true;
994 default:
995 if (token->keyword >= RID_FIRST_INT_N
996 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
997 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
998 return true;
999 return false;
1003 /* Returns TRUE iff the token T begins a decltype type. */
1005 static bool
1006 token_is_decltype (cp_token *t)
1008 return (t->keyword == RID_DECLTYPE
1009 || t->type == CPP_DECLTYPE);
1012 /* Returns TRUE iff the next token begins a decltype type. */
1014 static bool
1015 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1017 cp_token *t = cp_lexer_peek_token (lexer);
1018 return token_is_decltype (t);
1021 /* Return a pointer to the Nth token in the token stream. If N is 1,
1022 then this is precisely equivalent to cp_lexer_peek_token (except
1023 that it is not inline). One would like to disallow that case, but
1024 there is one case (cp_parser_nth_token_starts_template_id) where
1025 the caller passes a variable for N and it might be 1. */
1027 static cp_token *
1028 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1030 cp_token *token;
1032 /* N is 1-based, not zero-based. */
1033 gcc_assert (n > 0);
1035 if (cp_lexer_debugging_p (lexer))
1036 fprintf (cp_lexer_debug_stream,
1037 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1039 --n;
1040 token = lexer->next_token;
1041 gcc_assert (!n || token != &eof_token);
1042 while (n != 0)
1044 ++token;
1045 if (token == lexer->last_token)
1047 token = &eof_token;
1048 break;
1051 if (!token->purged_p)
1052 --n;
1055 if (cp_lexer_debugging_p (lexer))
1057 cp_lexer_print_token (cp_lexer_debug_stream, token);
1058 putc ('\n', cp_lexer_debug_stream);
1061 return token;
1064 /* Return the next token, and advance the lexer's next_token pointer
1065 to point to the next non-purged token. */
1067 static cp_token *
1068 cp_lexer_consume_token (cp_lexer* lexer)
1070 cp_token *token = lexer->next_token;
1072 gcc_assert (token != &eof_token);
1073 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1077 lexer->next_token++;
1078 if (lexer->next_token == lexer->last_token)
1080 lexer->next_token = &eof_token;
1081 break;
1085 while (lexer->next_token->purged_p);
1087 cp_lexer_set_source_position_from_token (token);
1089 /* Provide debugging output. */
1090 if (cp_lexer_debugging_p (lexer))
1092 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1093 cp_lexer_print_token (cp_lexer_debug_stream, token);
1094 putc ('\n', cp_lexer_debug_stream);
1097 return token;
1100 /* Permanently remove the next token from the token stream, and
1101 advance the next_token pointer to refer to the next non-purged
1102 token. */
1104 static void
1105 cp_lexer_purge_token (cp_lexer *lexer)
1107 cp_token *tok = lexer->next_token;
1109 gcc_assert (tok != &eof_token);
1110 tok->purged_p = true;
1111 tok->location = UNKNOWN_LOCATION;
1112 tok->u.value = NULL_TREE;
1113 tok->keyword = RID_MAX;
1117 tok++;
1118 if (tok == lexer->last_token)
1120 tok = &eof_token;
1121 break;
1124 while (tok->purged_p);
1125 lexer->next_token = tok;
1128 /* Permanently remove all tokens after TOK, up to, but not
1129 including, the token that will be returned next by
1130 cp_lexer_peek_token. */
1132 static void
1133 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1135 cp_token *peek = lexer->next_token;
1137 if (peek == &eof_token)
1138 peek = lexer->last_token;
1140 gcc_assert (tok < peek);
1142 for ( tok += 1; tok != peek; tok += 1)
1144 tok->purged_p = true;
1145 tok->location = UNKNOWN_LOCATION;
1146 tok->u.value = NULL_TREE;
1147 tok->keyword = RID_MAX;
1151 /* Begin saving tokens. All tokens consumed after this point will be
1152 preserved. */
1154 static void
1155 cp_lexer_save_tokens (cp_lexer* lexer)
1157 /* Provide debugging output. */
1158 if (cp_lexer_debugging_p (lexer))
1159 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1161 lexer->saved_tokens.safe_push (lexer->next_token);
1164 /* Commit to the portion of the token stream most recently saved. */
1166 static void
1167 cp_lexer_commit_tokens (cp_lexer* lexer)
1169 /* Provide debugging output. */
1170 if (cp_lexer_debugging_p (lexer))
1171 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1173 lexer->saved_tokens.pop ();
1176 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1177 to the token stream. Stop saving tokens. */
1179 static void
1180 cp_lexer_rollback_tokens (cp_lexer* lexer)
1182 /* Provide debugging output. */
1183 if (cp_lexer_debugging_p (lexer))
1184 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1186 lexer->next_token = lexer->saved_tokens.pop ();
1189 /* RAII wrapper around the above functions, with sanity checking. Creating
1190 a variable saves tokens, which are committed when the variable is
1191 destroyed unless they are explicitly rolled back by calling the rollback
1192 member function. */
1194 struct saved_token_sentinel
1196 cp_lexer *lexer;
1197 unsigned len;
1198 bool commit;
1199 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1201 len = lexer->saved_tokens.length ();
1202 cp_lexer_save_tokens (lexer);
1204 void rollback ()
1206 cp_lexer_rollback_tokens (lexer);
1207 commit = false;
1209 ~saved_token_sentinel()
1211 if (commit)
1212 cp_lexer_commit_tokens (lexer);
1213 gcc_assert (lexer->saved_tokens.length () == len);
1217 /* Print a representation of the TOKEN on the STREAM. */
1219 static void
1220 cp_lexer_print_token (FILE * stream, cp_token *token)
1222 /* We don't use cpp_type2name here because the parser defines
1223 a few tokens of its own. */
1224 static const char *const token_names[] = {
1225 /* cpplib-defined token types */
1226 #define OP(e, s) #e,
1227 #define TK(e, s) #e,
1228 TTYPE_TABLE
1229 #undef OP
1230 #undef TK
1231 /* C++ parser token types - see "Manifest constants", above. */
1232 "KEYWORD",
1233 "TEMPLATE_ID",
1234 "NESTED_NAME_SPECIFIER",
1237 /* For some tokens, print the associated data. */
1238 switch (token->type)
1240 case CPP_KEYWORD:
1241 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1242 For example, `struct' is mapped to an INTEGER_CST. */
1243 if (!identifier_p (token->u.value))
1244 break;
1245 /* else fall through */
1246 case CPP_NAME:
1247 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1248 break;
1250 case CPP_STRING:
1251 case CPP_STRING16:
1252 case CPP_STRING32:
1253 case CPP_WSTRING:
1254 case CPP_UTF8STRING:
1255 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1256 break;
1258 case CPP_NUMBER:
1259 print_generic_expr (stream, token->u.value, 0);
1260 break;
1262 default:
1263 /* If we have a name for the token, print it out. Otherwise, we
1264 simply give the numeric code. */
1265 if (token->type < ARRAY_SIZE(token_names))
1266 fputs (token_names[token->type], stream);
1267 else
1268 fprintf (stream, "[%d]", token->type);
1269 break;
1273 DEBUG_FUNCTION void
1274 debug (cp_token &ref)
1276 cp_lexer_print_token (stderr, &ref);
1277 fprintf (stderr, "\n");
1280 DEBUG_FUNCTION void
1281 debug (cp_token *ptr)
1283 if (ptr)
1284 debug (*ptr);
1285 else
1286 fprintf (stderr, "<nil>\n");
1290 /* Start emitting debugging information. */
1292 static void
1293 cp_lexer_start_debugging (cp_lexer* lexer)
1295 lexer->debugging_p = true;
1296 cp_lexer_debug_stream = stderr;
1299 /* Stop emitting debugging information. */
1301 static void
1302 cp_lexer_stop_debugging (cp_lexer* lexer)
1304 lexer->debugging_p = false;
1305 cp_lexer_debug_stream = NULL;
1308 /* Create a new cp_token_cache, representing a range of tokens. */
1310 static cp_token_cache *
1311 cp_token_cache_new (cp_token *first, cp_token *last)
1313 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1314 cache->first = first;
1315 cache->last = last;
1316 return cache;
1319 /* Diagnose if #pragma omp declare simd isn't followed immediately
1320 by function declaration or definition. */
1322 static inline void
1323 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1325 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1327 error ("%<#pragma omp declare simd%> not immediately followed by "
1328 "function declaration or definition");
1329 parser->omp_declare_simd = NULL;
1333 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1334 and put that into "omp declare simd" attribute. */
1336 static inline void
1337 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1339 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1341 if (fndecl == error_mark_node)
1343 parser->omp_declare_simd = NULL;
1344 return;
1346 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1348 cp_ensure_no_omp_declare_simd (parser);
1349 return;
1354 /* Decl-specifiers. */
1356 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1358 static void
1359 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1361 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1364 /* Declarators. */
1366 /* Nothing other than the parser should be creating declarators;
1367 declarators are a semi-syntactic representation of C++ entities.
1368 Other parts of the front end that need to create entities (like
1369 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1371 static cp_declarator *make_call_declarator
1372 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree);
1373 static cp_declarator *make_array_declarator
1374 (cp_declarator *, tree);
1375 static cp_declarator *make_pointer_declarator
1376 (cp_cv_quals, cp_declarator *, tree);
1377 static cp_declarator *make_reference_declarator
1378 (cp_cv_quals, cp_declarator *, bool, tree);
1379 static cp_parameter_declarator *make_parameter_declarator
1380 (cp_decl_specifier_seq *, cp_declarator *, tree);
1381 static cp_declarator *make_ptrmem_declarator
1382 (cp_cv_quals, tree, cp_declarator *, tree);
1384 /* An erroneous declarator. */
1385 static cp_declarator *cp_error_declarator;
1387 /* The obstack on which declarators and related data structures are
1388 allocated. */
1389 static struct obstack declarator_obstack;
1391 /* Alloc BYTES from the declarator memory pool. */
1393 static inline void *
1394 alloc_declarator (size_t bytes)
1396 return obstack_alloc (&declarator_obstack, bytes);
1399 /* Allocate a declarator of the indicated KIND. Clear fields that are
1400 common to all declarators. */
1402 static cp_declarator *
1403 make_declarator (cp_declarator_kind kind)
1405 cp_declarator *declarator;
1407 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1408 declarator->kind = kind;
1409 declarator->attributes = NULL_TREE;
1410 declarator->std_attributes = NULL_TREE;
1411 declarator->declarator = NULL;
1412 declarator->parameter_pack_p = false;
1413 declarator->id_loc = UNKNOWN_LOCATION;
1415 return declarator;
1418 /* Make a declarator for a generalized identifier. If
1419 QUALIFYING_SCOPE is non-NULL, the identifier is
1420 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1421 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1422 is, if any. */
1424 static cp_declarator *
1425 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1426 special_function_kind sfk)
1428 cp_declarator *declarator;
1430 /* It is valid to write:
1432 class C { void f(); };
1433 typedef C D;
1434 void D::f();
1436 The standard is not clear about whether `typedef const C D' is
1437 legal; as of 2002-09-15 the committee is considering that
1438 question. EDG 3.0 allows that syntax. Therefore, we do as
1439 well. */
1440 if (qualifying_scope && TYPE_P (qualifying_scope))
1441 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1443 gcc_assert (identifier_p (unqualified_name)
1444 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1445 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1447 declarator = make_declarator (cdk_id);
1448 declarator->u.id.qualifying_scope = qualifying_scope;
1449 declarator->u.id.unqualified_name = unqualified_name;
1450 declarator->u.id.sfk = sfk;
1452 return declarator;
1455 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1456 of modifiers such as const or volatile to apply to the pointer
1457 type, represented as identifiers. ATTRIBUTES represent the attributes that
1458 appertain to the pointer or reference. */
1460 cp_declarator *
1461 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1462 tree attributes)
1464 cp_declarator *declarator;
1466 declarator = make_declarator (cdk_pointer);
1467 declarator->declarator = target;
1468 declarator->u.pointer.qualifiers = cv_qualifiers;
1469 declarator->u.pointer.class_type = NULL_TREE;
1470 if (target)
1472 declarator->id_loc = target->id_loc;
1473 declarator->parameter_pack_p = target->parameter_pack_p;
1474 target->parameter_pack_p = false;
1476 else
1477 declarator->parameter_pack_p = false;
1479 declarator->std_attributes = attributes;
1481 return declarator;
1484 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1485 represent the attributes that appertain to the pointer or
1486 reference. */
1488 cp_declarator *
1489 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1490 bool rvalue_ref, tree attributes)
1492 cp_declarator *declarator;
1494 declarator = make_declarator (cdk_reference);
1495 declarator->declarator = target;
1496 declarator->u.reference.qualifiers = cv_qualifiers;
1497 declarator->u.reference.rvalue_ref = rvalue_ref;
1498 if (target)
1500 declarator->id_loc = target->id_loc;
1501 declarator->parameter_pack_p = target->parameter_pack_p;
1502 target->parameter_pack_p = false;
1504 else
1505 declarator->parameter_pack_p = false;
1507 declarator->std_attributes = attributes;
1509 return declarator;
1512 /* Like make_pointer_declarator -- but for a pointer to a non-static
1513 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1514 appertain to the pointer or reference. */
1516 cp_declarator *
1517 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1518 cp_declarator *pointee,
1519 tree attributes)
1521 cp_declarator *declarator;
1523 declarator = make_declarator (cdk_ptrmem);
1524 declarator->declarator = pointee;
1525 declarator->u.pointer.qualifiers = cv_qualifiers;
1526 declarator->u.pointer.class_type = class_type;
1528 if (pointee)
1530 declarator->parameter_pack_p = pointee->parameter_pack_p;
1531 pointee->parameter_pack_p = false;
1533 else
1534 declarator->parameter_pack_p = false;
1536 declarator->std_attributes = attributes;
1538 return declarator;
1541 /* Make a declarator for the function given by TARGET, with the
1542 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1543 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1544 indicates what exceptions can be thrown. */
1546 cp_declarator *
1547 make_call_declarator (cp_declarator *target,
1548 tree parms,
1549 cp_cv_quals cv_qualifiers,
1550 cp_virt_specifiers virt_specifiers,
1551 cp_ref_qualifier ref_qualifier,
1552 tree exception_specification,
1553 tree late_return_type,
1554 tree requires_clause)
1556 cp_declarator *declarator;
1558 declarator = make_declarator (cdk_function);
1559 declarator->declarator = target;
1560 declarator->u.function.parameters = parms;
1561 declarator->u.function.qualifiers = cv_qualifiers;
1562 declarator->u.function.virt_specifiers = virt_specifiers;
1563 declarator->u.function.ref_qualifier = ref_qualifier;
1564 declarator->u.function.exception_specification = exception_specification;
1565 declarator->u.function.late_return_type = late_return_type;
1566 declarator->u.function.requires_clause = requires_clause;
1567 if (target)
1569 declarator->id_loc = target->id_loc;
1570 declarator->parameter_pack_p = target->parameter_pack_p;
1571 target->parameter_pack_p = false;
1573 else
1574 declarator->parameter_pack_p = false;
1576 return declarator;
1579 /* Make a declarator for an array of BOUNDS elements, each of which is
1580 defined by ELEMENT. */
1582 cp_declarator *
1583 make_array_declarator (cp_declarator *element, tree bounds)
1585 cp_declarator *declarator;
1587 declarator = make_declarator (cdk_array);
1588 declarator->declarator = element;
1589 declarator->u.array.bounds = bounds;
1590 if (element)
1592 declarator->id_loc = element->id_loc;
1593 declarator->parameter_pack_p = element->parameter_pack_p;
1594 element->parameter_pack_p = false;
1596 else
1597 declarator->parameter_pack_p = false;
1599 return declarator;
1602 /* Determine whether the declarator we've seen so far can be a
1603 parameter pack, when followed by an ellipsis. */
1604 static bool
1605 declarator_can_be_parameter_pack (cp_declarator *declarator)
1607 /* Search for a declarator name, or any other declarator that goes
1608 after the point where the ellipsis could appear in a parameter
1609 pack. If we find any of these, then this declarator can not be
1610 made into a parameter pack. */
1611 bool found = false;
1612 while (declarator && !found)
1614 switch ((int)declarator->kind)
1616 case cdk_id:
1617 case cdk_array:
1618 found = true;
1619 break;
1621 case cdk_error:
1622 return true;
1624 default:
1625 declarator = declarator->declarator;
1626 break;
1630 return !found;
1633 cp_parameter_declarator *no_parameters;
1635 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1636 DECLARATOR and DEFAULT_ARGUMENT. */
1638 cp_parameter_declarator *
1639 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1640 cp_declarator *declarator,
1641 tree default_argument)
1643 cp_parameter_declarator *parameter;
1645 parameter = ((cp_parameter_declarator *)
1646 alloc_declarator (sizeof (cp_parameter_declarator)));
1647 parameter->next = NULL;
1648 if (decl_specifiers)
1649 parameter->decl_specifiers = *decl_specifiers;
1650 else
1651 clear_decl_specs (&parameter->decl_specifiers);
1652 parameter->declarator = declarator;
1653 parameter->default_argument = default_argument;
1654 parameter->ellipsis_p = false;
1656 return parameter;
1659 /* Returns true iff DECLARATOR is a declaration for a function. */
1661 static bool
1662 function_declarator_p (const cp_declarator *declarator)
1664 while (declarator)
1666 if (declarator->kind == cdk_function
1667 && declarator->declarator->kind == cdk_id)
1668 return true;
1669 if (declarator->kind == cdk_id
1670 || declarator->kind == cdk_error)
1671 return false;
1672 declarator = declarator->declarator;
1674 return false;
1677 /* The parser. */
1679 /* Overview
1680 --------
1682 A cp_parser parses the token stream as specified by the C++
1683 grammar. Its job is purely parsing, not semantic analysis. For
1684 example, the parser breaks the token stream into declarators,
1685 expressions, statements, and other similar syntactic constructs.
1686 It does not check that the types of the expressions on either side
1687 of an assignment-statement are compatible, or that a function is
1688 not declared with a parameter of type `void'.
1690 The parser invokes routines elsewhere in the compiler to perform
1691 semantic analysis and to build up the abstract syntax tree for the
1692 code processed.
1694 The parser (and the template instantiation code, which is, in a
1695 way, a close relative of parsing) are the only parts of the
1696 compiler that should be calling push_scope and pop_scope, or
1697 related functions. The parser (and template instantiation code)
1698 keeps track of what scope is presently active; everything else
1699 should simply honor that. (The code that generates static
1700 initializers may also need to set the scope, in order to check
1701 access control correctly when emitting the initializers.)
1703 Methodology
1704 -----------
1706 The parser is of the standard recursive-descent variety. Upcoming
1707 tokens in the token stream are examined in order to determine which
1708 production to use when parsing a non-terminal. Some C++ constructs
1709 require arbitrary look ahead to disambiguate. For example, it is
1710 impossible, in the general case, to tell whether a statement is an
1711 expression or declaration without scanning the entire statement.
1712 Therefore, the parser is capable of "parsing tentatively." When the
1713 parser is not sure what construct comes next, it enters this mode.
1714 Then, while we attempt to parse the construct, the parser queues up
1715 error messages, rather than issuing them immediately, and saves the
1716 tokens it consumes. If the construct is parsed successfully, the
1717 parser "commits", i.e., it issues any queued error messages and
1718 the tokens that were being preserved are permanently discarded.
1719 If, however, the construct is not parsed successfully, the parser
1720 rolls back its state completely so that it can resume parsing using
1721 a different alternative.
1723 Future Improvements
1724 -------------------
1726 The performance of the parser could probably be improved substantially.
1727 We could often eliminate the need to parse tentatively by looking ahead
1728 a little bit. In some places, this approach might not entirely eliminate
1729 the need to parse tentatively, but it might still speed up the average
1730 case. */
1732 /* Flags that are passed to some parsing functions. These values can
1733 be bitwise-ored together. */
1735 enum
1737 /* No flags. */
1738 CP_PARSER_FLAGS_NONE = 0x0,
1739 /* The construct is optional. If it is not present, then no error
1740 should be issued. */
1741 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1742 /* When parsing a type-specifier, treat user-defined type-names
1743 as non-type identifiers. */
1744 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1745 /* When parsing a type-specifier, do not try to parse a class-specifier
1746 or enum-specifier. */
1747 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1748 /* When parsing a decl-specifier-seq, only allow type-specifier or
1749 constexpr. */
1750 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1753 /* This type is used for parameters and variables which hold
1754 combinations of the above flags. */
1755 typedef int cp_parser_flags;
1757 /* The different kinds of declarators we want to parse. */
1759 typedef enum cp_parser_declarator_kind
1761 /* We want an abstract declarator. */
1762 CP_PARSER_DECLARATOR_ABSTRACT,
1763 /* We want a named declarator. */
1764 CP_PARSER_DECLARATOR_NAMED,
1765 /* We don't mind, but the name must be an unqualified-id. */
1766 CP_PARSER_DECLARATOR_EITHER
1767 } cp_parser_declarator_kind;
1769 /* The precedence values used to parse binary expressions. The minimum value
1770 of PREC must be 1, because zero is reserved to quickly discriminate
1771 binary operators from other tokens. */
1773 enum cp_parser_prec
1775 PREC_NOT_OPERATOR,
1776 PREC_LOGICAL_OR_EXPRESSION,
1777 PREC_LOGICAL_AND_EXPRESSION,
1778 PREC_INCLUSIVE_OR_EXPRESSION,
1779 PREC_EXCLUSIVE_OR_EXPRESSION,
1780 PREC_AND_EXPRESSION,
1781 PREC_EQUALITY_EXPRESSION,
1782 PREC_RELATIONAL_EXPRESSION,
1783 PREC_SHIFT_EXPRESSION,
1784 PREC_ADDITIVE_EXPRESSION,
1785 PREC_MULTIPLICATIVE_EXPRESSION,
1786 PREC_PM_EXPRESSION,
1787 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1790 /* A mapping from a token type to a corresponding tree node type, with a
1791 precedence value. */
1793 typedef struct cp_parser_binary_operations_map_node
1795 /* The token type. */
1796 enum cpp_ttype token_type;
1797 /* The corresponding tree code. */
1798 enum tree_code tree_type;
1799 /* The precedence of this operator. */
1800 enum cp_parser_prec prec;
1801 } cp_parser_binary_operations_map_node;
1803 typedef struct cp_parser_expression_stack_entry
1805 /* Left hand side of the binary operation we are currently
1806 parsing. */
1807 tree lhs;
1808 /* Original tree code for left hand side, if it was a binary
1809 expression itself (used for -Wparentheses). */
1810 enum tree_code lhs_type;
1811 /* Tree code for the binary operation we are parsing. */
1812 enum tree_code tree_type;
1813 /* Precedence of the binary operation we are parsing. */
1814 enum cp_parser_prec prec;
1815 /* Location of the binary operation we are parsing. */
1816 location_t loc;
1817 } cp_parser_expression_stack_entry;
1819 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1820 entries because precedence levels on the stack are monotonically
1821 increasing. */
1822 typedef struct cp_parser_expression_stack_entry
1823 cp_parser_expression_stack[NUM_PREC_VALUES];
1825 /* Prototypes. */
1827 /* Constructors and destructors. */
1829 static cp_parser_context *cp_parser_context_new
1830 (cp_parser_context *);
1832 /* Class variables. */
1834 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1836 /* The operator-precedence table used by cp_parser_binary_expression.
1837 Transformed into an associative array (binops_by_token) by
1838 cp_parser_new. */
1840 static const cp_parser_binary_operations_map_node binops[] = {
1841 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1842 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1844 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1845 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1846 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1848 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1849 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1851 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1852 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1854 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1855 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1856 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1857 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1859 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1860 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1862 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1864 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1866 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1868 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1870 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1873 /* The same as binops, but initialized by cp_parser_new so that
1874 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1875 for speed. */
1876 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1878 /* Constructors and destructors. */
1880 /* Construct a new context. The context below this one on the stack
1881 is given by NEXT. */
1883 static cp_parser_context *
1884 cp_parser_context_new (cp_parser_context* next)
1886 cp_parser_context *context;
1888 /* Allocate the storage. */
1889 if (cp_parser_context_free_list != NULL)
1891 /* Pull the first entry from the free list. */
1892 context = cp_parser_context_free_list;
1893 cp_parser_context_free_list = context->next;
1894 memset (context, 0, sizeof (*context));
1896 else
1897 context = ggc_cleared_alloc<cp_parser_context> ();
1899 /* No errors have occurred yet in this context. */
1900 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1901 /* If this is not the bottommost context, copy information that we
1902 need from the previous context. */
1903 if (next)
1905 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1906 expression, then we are parsing one in this context, too. */
1907 context->object_type = next->object_type;
1908 /* Thread the stack. */
1909 context->next = next;
1912 return context;
1915 /* Managing the unparsed function queues. */
1917 #define unparsed_funs_with_default_args \
1918 parser->unparsed_queues->last ().funs_with_default_args
1919 #define unparsed_funs_with_definitions \
1920 parser->unparsed_queues->last ().funs_with_definitions
1921 #define unparsed_nsdmis \
1922 parser->unparsed_queues->last ().nsdmis
1923 #define unparsed_classes \
1924 parser->unparsed_queues->last ().classes
1926 static void
1927 push_unparsed_function_queues (cp_parser *parser)
1929 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1930 vec_safe_push (parser->unparsed_queues, e);
1933 static void
1934 pop_unparsed_function_queues (cp_parser *parser)
1936 release_tree_vector (unparsed_funs_with_definitions);
1937 parser->unparsed_queues->pop ();
1940 /* Prototypes. */
1942 /* Constructors and destructors. */
1944 static cp_parser *cp_parser_new
1945 (void);
1947 /* Routines to parse various constructs.
1949 Those that return `tree' will return the error_mark_node (rather
1950 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1951 Sometimes, they will return an ordinary node if error-recovery was
1952 attempted, even though a parse error occurred. So, to check
1953 whether or not a parse error occurred, you should always use
1954 cp_parser_error_occurred. If the construct is optional (indicated
1955 either by an `_opt' in the name of the function that does the
1956 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1957 the construct is not present. */
1959 /* Lexical conventions [gram.lex] */
1961 static tree cp_parser_identifier
1962 (cp_parser *);
1963 static tree cp_parser_string_literal
1964 (cp_parser *, bool, bool, bool);
1965 static tree cp_parser_userdef_char_literal
1966 (cp_parser *);
1967 static tree cp_parser_userdef_string_literal
1968 (tree);
1969 static tree cp_parser_userdef_numeric_literal
1970 (cp_parser *);
1972 /* Basic concepts [gram.basic] */
1974 static bool cp_parser_translation_unit
1975 (cp_parser *);
1977 /* Expressions [gram.expr] */
1979 static tree cp_parser_primary_expression
1980 (cp_parser *, bool, bool, bool, cp_id_kind *);
1981 static tree cp_parser_id_expression
1982 (cp_parser *, bool, bool, bool *, bool, bool);
1983 static tree cp_parser_unqualified_id
1984 (cp_parser *, bool, bool, bool, bool);
1985 static tree cp_parser_nested_name_specifier_opt
1986 (cp_parser *, bool, bool, bool, bool);
1987 static tree cp_parser_nested_name_specifier
1988 (cp_parser *, bool, bool, bool, bool);
1989 static tree cp_parser_qualifying_entity
1990 (cp_parser *, bool, bool, bool, bool, bool);
1991 static tree cp_parser_postfix_expression
1992 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1993 static tree cp_parser_postfix_open_square_expression
1994 (cp_parser *, tree, bool, bool);
1995 static tree cp_parser_postfix_dot_deref_expression
1996 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1997 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1998 (cp_parser *, int, bool, bool, bool *, bool = false);
1999 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2000 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2001 static void cp_parser_pseudo_destructor_name
2002 (cp_parser *, tree, tree *, tree *);
2003 static tree cp_parser_unary_expression
2004 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2005 static enum tree_code cp_parser_unary_operator
2006 (cp_token *);
2007 static tree cp_parser_new_expression
2008 (cp_parser *);
2009 static vec<tree, va_gc> *cp_parser_new_placement
2010 (cp_parser *);
2011 static tree cp_parser_new_type_id
2012 (cp_parser *, tree *);
2013 static cp_declarator *cp_parser_new_declarator_opt
2014 (cp_parser *);
2015 static cp_declarator *cp_parser_direct_new_declarator
2016 (cp_parser *);
2017 static vec<tree, va_gc> *cp_parser_new_initializer
2018 (cp_parser *);
2019 static tree cp_parser_delete_expression
2020 (cp_parser *);
2021 static tree cp_parser_cast_expression
2022 (cp_parser *, bool, bool, bool, cp_id_kind *);
2023 static tree cp_parser_binary_expression
2024 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2025 static tree cp_parser_question_colon_clause
2026 (cp_parser *, tree);
2027 static tree cp_parser_assignment_expression
2028 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2029 static enum tree_code cp_parser_assignment_operator_opt
2030 (cp_parser *);
2031 static tree cp_parser_expression
2032 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2033 static tree cp_parser_constant_expression
2034 (cp_parser *, bool = false, bool * = NULL);
2035 static tree cp_parser_builtin_offsetof
2036 (cp_parser *);
2037 static tree cp_parser_lambda_expression
2038 (cp_parser *);
2039 static void cp_parser_lambda_introducer
2040 (cp_parser *, tree);
2041 static bool cp_parser_lambda_declarator_opt
2042 (cp_parser *, tree);
2043 static void cp_parser_lambda_body
2044 (cp_parser *, tree);
2046 /* Statements [gram.stmt.stmt] */
2048 static void cp_parser_statement
2049 (cp_parser *, tree, bool, bool *);
2050 static void cp_parser_label_for_labeled_statement
2051 (cp_parser *, tree);
2052 static tree cp_parser_expression_statement
2053 (cp_parser *, tree);
2054 static tree cp_parser_compound_statement
2055 (cp_parser *, tree, bool, bool);
2056 static void cp_parser_statement_seq_opt
2057 (cp_parser *, tree);
2058 static tree cp_parser_selection_statement
2059 (cp_parser *, bool *);
2060 static tree cp_parser_condition
2061 (cp_parser *);
2062 static tree cp_parser_iteration_statement
2063 (cp_parser *, bool);
2064 static bool cp_parser_for_init_statement
2065 (cp_parser *, tree *decl);
2066 static tree cp_parser_for
2067 (cp_parser *, bool);
2068 static tree cp_parser_c_for
2069 (cp_parser *, tree, tree, bool);
2070 static tree cp_parser_range_for
2071 (cp_parser *, tree, tree, tree, bool);
2072 static void do_range_for_auto_deduction
2073 (tree, tree);
2074 static tree cp_parser_perform_range_for_lookup
2075 (tree, tree *, tree *);
2076 static tree cp_parser_range_for_member_function
2077 (tree, tree);
2078 static tree cp_parser_jump_statement
2079 (cp_parser *);
2080 static void cp_parser_declaration_statement
2081 (cp_parser *);
2083 static tree cp_parser_implicitly_scoped_statement
2084 (cp_parser *, bool *);
2085 static void cp_parser_already_scoped_statement
2086 (cp_parser *);
2088 /* Declarations [gram.dcl.dcl] */
2090 static void cp_parser_declaration_seq_opt
2091 (cp_parser *);
2092 static void cp_parser_declaration
2093 (cp_parser *);
2094 static void cp_parser_block_declaration
2095 (cp_parser *, bool);
2096 static void cp_parser_simple_declaration
2097 (cp_parser *, bool, tree *);
2098 static void cp_parser_decl_specifier_seq
2099 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2100 static tree cp_parser_storage_class_specifier_opt
2101 (cp_parser *);
2102 static tree cp_parser_function_specifier_opt
2103 (cp_parser *, cp_decl_specifier_seq *);
2104 static tree cp_parser_type_specifier
2105 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2106 int *, bool *);
2107 static tree cp_parser_simple_type_specifier
2108 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2109 static tree cp_parser_type_name
2110 (cp_parser *, bool);
2111 static tree cp_parser_type_name
2112 (cp_parser *);
2113 static tree cp_parser_nonclass_name
2114 (cp_parser* parser);
2115 static tree cp_parser_elaborated_type_specifier
2116 (cp_parser *, bool, bool);
2117 static tree cp_parser_enum_specifier
2118 (cp_parser *);
2119 static void cp_parser_enumerator_list
2120 (cp_parser *, tree);
2121 static void cp_parser_enumerator_definition
2122 (cp_parser *, tree);
2123 static tree cp_parser_namespace_name
2124 (cp_parser *);
2125 static void cp_parser_namespace_definition
2126 (cp_parser *);
2127 static void cp_parser_namespace_body
2128 (cp_parser *);
2129 static tree cp_parser_qualified_namespace_specifier
2130 (cp_parser *);
2131 static void cp_parser_namespace_alias_definition
2132 (cp_parser *);
2133 static bool cp_parser_using_declaration
2134 (cp_parser *, bool);
2135 static void cp_parser_using_directive
2136 (cp_parser *);
2137 static tree cp_parser_alias_declaration
2138 (cp_parser *);
2139 static void cp_parser_asm_definition
2140 (cp_parser *);
2141 static void cp_parser_linkage_specification
2142 (cp_parser *);
2143 static void cp_parser_static_assert
2144 (cp_parser *, bool);
2145 static tree cp_parser_decltype
2146 (cp_parser *);
2148 /* Declarators [gram.dcl.decl] */
2150 static tree cp_parser_init_declarator
2151 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2152 bool, bool, int, bool *, tree *, location_t *);
2153 static cp_declarator *cp_parser_declarator
2154 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2155 static cp_declarator *cp_parser_direct_declarator
2156 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2157 static enum tree_code cp_parser_ptr_operator
2158 (cp_parser *, tree *, cp_cv_quals *, tree *);
2159 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2160 (cp_parser *);
2161 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2162 (cp_parser *);
2163 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2164 (cp_parser *);
2165 static tree cp_parser_late_return_type_opt
2166 (cp_parser *, cp_declarator *, cp_cv_quals);
2167 static tree cp_parser_declarator_id
2168 (cp_parser *, bool);
2169 static tree cp_parser_type_id
2170 (cp_parser *);
2171 static tree cp_parser_template_type_arg
2172 (cp_parser *);
2173 static tree cp_parser_trailing_type_id (cp_parser *);
2174 static tree cp_parser_type_id_1
2175 (cp_parser *, bool, bool);
2176 static void cp_parser_type_specifier_seq
2177 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2178 static tree cp_parser_parameter_declaration_clause
2179 (cp_parser *);
2180 static tree cp_parser_parameter_declaration_list
2181 (cp_parser *, bool *);
2182 static cp_parameter_declarator *cp_parser_parameter_declaration
2183 (cp_parser *, bool, bool *);
2184 static tree cp_parser_default_argument
2185 (cp_parser *, bool);
2186 static void cp_parser_function_body
2187 (cp_parser *, bool);
2188 static tree cp_parser_initializer
2189 (cp_parser *, bool *, bool *);
2190 static tree cp_parser_initializer_clause
2191 (cp_parser *, bool *);
2192 static tree cp_parser_braced_list
2193 (cp_parser*, bool*);
2194 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2195 (cp_parser *, bool *);
2197 static bool cp_parser_ctor_initializer_opt_and_function_body
2198 (cp_parser *, bool);
2200 static tree cp_parser_late_parsing_omp_declare_simd
2201 (cp_parser *, tree);
2203 static tree cp_parser_late_parsing_cilk_simd_fn_info
2204 (cp_parser *, tree);
2206 static tree synthesize_implicit_template_parm
2207 (cp_parser *, tree);
2208 static tree finish_fully_implicit_template
2209 (cp_parser *, tree);
2211 /* Classes [gram.class] */
2213 static tree cp_parser_class_name
2214 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2215 static tree cp_parser_class_specifier
2216 (cp_parser *);
2217 static tree cp_parser_class_head
2218 (cp_parser *, bool *);
2219 static enum tag_types cp_parser_class_key
2220 (cp_parser *);
2221 static void cp_parser_type_parameter_key
2222 (cp_parser* parser);
2223 static void cp_parser_member_specification_opt
2224 (cp_parser *);
2225 static void cp_parser_member_declaration
2226 (cp_parser *);
2227 static tree cp_parser_pure_specifier
2228 (cp_parser *);
2229 static tree cp_parser_constant_initializer
2230 (cp_parser *);
2232 /* Derived classes [gram.class.derived] */
2234 static tree cp_parser_base_clause
2235 (cp_parser *);
2236 static tree cp_parser_base_specifier
2237 (cp_parser *);
2239 /* Special member functions [gram.special] */
2241 static tree cp_parser_conversion_function_id
2242 (cp_parser *);
2243 static tree cp_parser_conversion_type_id
2244 (cp_parser *);
2245 static cp_declarator *cp_parser_conversion_declarator_opt
2246 (cp_parser *);
2247 static bool cp_parser_ctor_initializer_opt
2248 (cp_parser *);
2249 static void cp_parser_mem_initializer_list
2250 (cp_parser *);
2251 static tree cp_parser_mem_initializer
2252 (cp_parser *);
2253 static tree cp_parser_mem_initializer_id
2254 (cp_parser *);
2256 /* Overloading [gram.over] */
2258 static tree cp_parser_operator_function_id
2259 (cp_parser *);
2260 static tree cp_parser_operator
2261 (cp_parser *);
2263 /* Templates [gram.temp] */
2265 static void cp_parser_template_declaration
2266 (cp_parser *, bool);
2267 static tree cp_parser_template_parameter_list
2268 (cp_parser *);
2269 static tree cp_parser_template_parameter
2270 (cp_parser *, bool *, bool *);
2271 static tree cp_parser_type_parameter
2272 (cp_parser *, bool *);
2273 static tree cp_parser_template_id
2274 (cp_parser *, bool, bool, enum tag_types, bool);
2275 static tree cp_parser_template_name
2276 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2277 static tree cp_parser_template_argument_list
2278 (cp_parser *);
2279 static tree cp_parser_template_argument
2280 (cp_parser *);
2281 static void cp_parser_explicit_instantiation
2282 (cp_parser *);
2283 static void cp_parser_explicit_specialization
2284 (cp_parser *);
2286 /* Exception handling [gram.exception] */
2288 static tree cp_parser_try_block
2289 (cp_parser *);
2290 static bool cp_parser_function_try_block
2291 (cp_parser *);
2292 static void cp_parser_handler_seq
2293 (cp_parser *);
2294 static void cp_parser_handler
2295 (cp_parser *);
2296 static tree cp_parser_exception_declaration
2297 (cp_parser *);
2298 static tree cp_parser_throw_expression
2299 (cp_parser *);
2300 static tree cp_parser_exception_specification_opt
2301 (cp_parser *);
2302 static tree cp_parser_type_id_list
2303 (cp_parser *);
2305 /* GNU Extensions */
2307 static tree cp_parser_asm_specification_opt
2308 (cp_parser *);
2309 static tree cp_parser_asm_operand_list
2310 (cp_parser *);
2311 static tree cp_parser_asm_clobber_list
2312 (cp_parser *);
2313 static tree cp_parser_asm_label_list
2314 (cp_parser *);
2315 static bool cp_next_tokens_can_be_attribute_p
2316 (cp_parser *);
2317 static bool cp_next_tokens_can_be_gnu_attribute_p
2318 (cp_parser *);
2319 static bool cp_next_tokens_can_be_std_attribute_p
2320 (cp_parser *);
2321 static bool cp_nth_tokens_can_be_std_attribute_p
2322 (cp_parser *, size_t);
2323 static bool cp_nth_tokens_can_be_gnu_attribute_p
2324 (cp_parser *, size_t);
2325 static bool cp_nth_tokens_can_be_attribute_p
2326 (cp_parser *, size_t);
2327 static tree cp_parser_attributes_opt
2328 (cp_parser *);
2329 static tree cp_parser_gnu_attributes_opt
2330 (cp_parser *);
2331 static tree cp_parser_gnu_attribute_list
2332 (cp_parser *);
2333 static tree cp_parser_std_attribute
2334 (cp_parser *);
2335 static tree cp_parser_std_attribute_spec
2336 (cp_parser *);
2337 static tree cp_parser_std_attribute_spec_seq
2338 (cp_parser *);
2339 static bool cp_parser_extension_opt
2340 (cp_parser *, int *);
2341 static void cp_parser_label_declaration
2342 (cp_parser *);
2344 /* Concept Extensions */
2346 static tree cp_parser_requires_clause
2347 (cp_parser *);
2348 static tree cp_parser_requires_clause_opt
2349 (cp_parser *);
2350 static tree cp_parser_requires_expression
2351 (cp_parser *);
2352 static tree cp_parser_requirement_parameter_list
2353 (cp_parser *);
2354 static tree cp_parser_requirement_body
2355 (cp_parser *);
2356 static tree cp_parser_requirement_list
2357 (cp_parser *);
2358 static tree cp_parser_requirement
2359 (cp_parser *);
2360 static tree cp_parser_simple_requirement
2361 (cp_parser *);
2362 static tree cp_parser_compound_requirement
2363 (cp_parser *);
2364 static tree cp_parser_type_requirement
2365 (cp_parser *);
2366 static tree cp_parser_nested_requirement
2367 (cp_parser *);
2369 /* Transactional Memory Extensions */
2371 static tree cp_parser_transaction
2372 (cp_parser *, enum rid);
2373 static tree cp_parser_transaction_expression
2374 (cp_parser *, enum rid);
2375 static bool cp_parser_function_transaction
2376 (cp_parser *, enum rid);
2377 static tree cp_parser_transaction_cancel
2378 (cp_parser *);
2380 enum pragma_context {
2381 pragma_external,
2382 pragma_member,
2383 pragma_objc_icode,
2384 pragma_stmt,
2385 pragma_compound
2387 static bool cp_parser_pragma
2388 (cp_parser *, enum pragma_context);
2390 /* Objective-C++ Productions */
2392 static tree cp_parser_objc_message_receiver
2393 (cp_parser *);
2394 static tree cp_parser_objc_message_args
2395 (cp_parser *);
2396 static tree cp_parser_objc_message_expression
2397 (cp_parser *);
2398 static tree cp_parser_objc_encode_expression
2399 (cp_parser *);
2400 static tree cp_parser_objc_defs_expression
2401 (cp_parser *);
2402 static tree cp_parser_objc_protocol_expression
2403 (cp_parser *);
2404 static tree cp_parser_objc_selector_expression
2405 (cp_parser *);
2406 static tree cp_parser_objc_expression
2407 (cp_parser *);
2408 static bool cp_parser_objc_selector_p
2409 (enum cpp_ttype);
2410 static tree cp_parser_objc_selector
2411 (cp_parser *);
2412 static tree cp_parser_objc_protocol_refs_opt
2413 (cp_parser *);
2414 static void cp_parser_objc_declaration
2415 (cp_parser *, tree);
2416 static tree cp_parser_objc_statement
2417 (cp_parser *);
2418 static bool cp_parser_objc_valid_prefix_attributes
2419 (cp_parser *, tree *);
2420 static void cp_parser_objc_at_property_declaration
2421 (cp_parser *) ;
2422 static void cp_parser_objc_at_synthesize_declaration
2423 (cp_parser *) ;
2424 static void cp_parser_objc_at_dynamic_declaration
2425 (cp_parser *) ;
2426 static tree cp_parser_objc_struct_declaration
2427 (cp_parser *) ;
2429 /* Utility Routines */
2431 static tree cp_parser_lookup_name
2432 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2433 static tree cp_parser_lookup_name_simple
2434 (cp_parser *, tree, location_t);
2435 static tree cp_parser_maybe_treat_template_as_class
2436 (tree, bool);
2437 static bool cp_parser_check_declarator_template_parameters
2438 (cp_parser *, cp_declarator *, location_t);
2439 static bool cp_parser_check_template_parameters
2440 (cp_parser *, unsigned, location_t, cp_declarator *);
2441 static tree cp_parser_simple_cast_expression
2442 (cp_parser *);
2443 static tree cp_parser_global_scope_opt
2444 (cp_parser *, bool);
2445 static bool cp_parser_constructor_declarator_p
2446 (cp_parser *, bool);
2447 static tree cp_parser_function_definition_from_specifiers_and_declarator
2448 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2449 static tree cp_parser_function_definition_after_declarator
2450 (cp_parser *, bool);
2451 static void cp_parser_template_declaration_after_export
2452 (cp_parser *, bool);
2453 static void cp_parser_perform_template_parameter_access_checks
2454 (vec<deferred_access_check, va_gc> *);
2455 static tree cp_parser_single_declaration
2456 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2457 static tree cp_parser_functional_cast
2458 (cp_parser *, tree);
2459 static tree cp_parser_save_member_function_body
2460 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2461 static tree cp_parser_save_nsdmi
2462 (cp_parser *);
2463 static tree cp_parser_enclosed_template_argument_list
2464 (cp_parser *);
2465 static void cp_parser_save_default_args
2466 (cp_parser *, tree);
2467 static void cp_parser_late_parsing_for_member
2468 (cp_parser *, tree);
2469 static tree cp_parser_late_parse_one_default_arg
2470 (cp_parser *, tree, tree, tree);
2471 static void cp_parser_late_parsing_nsdmi
2472 (cp_parser *, tree);
2473 static void cp_parser_late_parsing_default_args
2474 (cp_parser *, tree);
2475 static tree cp_parser_sizeof_operand
2476 (cp_parser *, enum rid);
2477 static tree cp_parser_trait_expr
2478 (cp_parser *, enum rid);
2479 static bool cp_parser_declares_only_class_p
2480 (cp_parser *);
2481 static void cp_parser_set_storage_class
2482 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2483 static void cp_parser_set_decl_spec_type
2484 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2485 static void set_and_check_decl_spec_loc
2486 (cp_decl_specifier_seq *decl_specs,
2487 cp_decl_spec ds, cp_token *);
2488 static bool cp_parser_friend_p
2489 (const cp_decl_specifier_seq *);
2490 static void cp_parser_required_error
2491 (cp_parser *, required_token, bool);
2492 static cp_token *cp_parser_require
2493 (cp_parser *, enum cpp_ttype, required_token);
2494 static cp_token *cp_parser_require_keyword
2495 (cp_parser *, enum rid, required_token);
2496 static bool cp_parser_token_starts_function_definition_p
2497 (cp_token *);
2498 static bool cp_parser_next_token_starts_class_definition_p
2499 (cp_parser *);
2500 static bool cp_parser_next_token_ends_template_argument_p
2501 (cp_parser *);
2502 static bool cp_parser_nth_token_starts_template_argument_list_p
2503 (cp_parser *, size_t);
2504 static enum tag_types cp_parser_token_is_class_key
2505 (cp_token *);
2506 static enum tag_types cp_parser_token_is_type_parameter_key
2507 (cp_token *);
2508 static void cp_parser_check_class_key
2509 (enum tag_types, tree type);
2510 static void cp_parser_check_access_in_redeclaration
2511 (tree type, location_t location);
2512 static bool cp_parser_optional_template_keyword
2513 (cp_parser *);
2514 static void cp_parser_pre_parsed_nested_name_specifier
2515 (cp_parser *);
2516 static bool cp_parser_cache_group
2517 (cp_parser *, enum cpp_ttype, unsigned);
2518 static tree cp_parser_cache_defarg
2519 (cp_parser *parser, bool nsdmi);
2520 static void cp_parser_parse_tentatively
2521 (cp_parser *);
2522 static void cp_parser_commit_to_tentative_parse
2523 (cp_parser *);
2524 static void cp_parser_commit_to_topmost_tentative_parse
2525 (cp_parser *);
2526 static void cp_parser_abort_tentative_parse
2527 (cp_parser *);
2528 static bool cp_parser_parse_definitely
2529 (cp_parser *);
2530 static inline bool cp_parser_parsing_tentatively
2531 (cp_parser *);
2532 static bool cp_parser_uncommitted_to_tentative_parse_p
2533 (cp_parser *);
2534 static void cp_parser_error
2535 (cp_parser *, const char *);
2536 static void cp_parser_name_lookup_error
2537 (cp_parser *, tree, tree, name_lookup_error, location_t);
2538 static bool cp_parser_simulate_error
2539 (cp_parser *);
2540 static bool cp_parser_check_type_definition
2541 (cp_parser *);
2542 static void cp_parser_check_for_definition_in_return_type
2543 (cp_declarator *, tree, location_t type_location);
2544 static void cp_parser_check_for_invalid_template_id
2545 (cp_parser *, tree, enum tag_types, location_t location);
2546 static bool cp_parser_non_integral_constant_expression
2547 (cp_parser *, non_integral_constant);
2548 static void cp_parser_diagnose_invalid_type_name
2549 (cp_parser *, tree, location_t);
2550 static bool cp_parser_parse_and_diagnose_invalid_type_name
2551 (cp_parser *);
2552 static int cp_parser_skip_to_closing_parenthesis
2553 (cp_parser *, bool, bool, bool);
2554 static void cp_parser_skip_to_end_of_statement
2555 (cp_parser *);
2556 static void cp_parser_consume_semicolon_at_end_of_statement
2557 (cp_parser *);
2558 static void cp_parser_skip_to_end_of_block_or_statement
2559 (cp_parser *);
2560 static bool cp_parser_skip_to_closing_brace
2561 (cp_parser *);
2562 static void cp_parser_skip_to_end_of_template_parameter_list
2563 (cp_parser *);
2564 static void cp_parser_skip_to_pragma_eol
2565 (cp_parser*, cp_token *);
2566 static bool cp_parser_error_occurred
2567 (cp_parser *);
2568 static bool cp_parser_allow_gnu_extensions_p
2569 (cp_parser *);
2570 static bool cp_parser_is_pure_string_literal
2571 (cp_token *);
2572 static bool cp_parser_is_string_literal
2573 (cp_token *);
2574 static bool cp_parser_is_keyword
2575 (cp_token *, enum rid);
2576 static tree cp_parser_make_typename_type
2577 (cp_parser *, tree, location_t location);
2578 static cp_declarator * cp_parser_make_indirect_declarator
2579 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2580 static bool cp_parser_compound_literal_p
2581 (cp_parser *);
2582 static bool cp_parser_array_designator_p
2583 (cp_parser *);
2584 static bool cp_parser_skip_to_closing_square_bracket
2585 (cp_parser *);
2587 /* Concept-related syntactic transformations */
2589 static tree cp_maybe_concept_name (cp_parser *, tree);
2590 static tree cp_maybe_partial_concept_id (cp_parser *, tree, tree);
2592 // -------------------------------------------------------------------------- //
2593 // Unevaluated Operand Guard
2595 // Implementation of an RAII helper for unevaluated operand parsing.
2596 cp_unevaluated::cp_unevaluated ()
2598 ++cp_unevaluated_operand;
2599 ++c_inhibit_evaluation_warnings;
2602 cp_unevaluated::~cp_unevaluated ()
2604 --c_inhibit_evaluation_warnings;
2605 --cp_unevaluated_operand;
2608 // -------------------------------------------------------------------------- //
2609 // Tentative Parsing
2611 /* Returns nonzero if we are parsing tentatively. */
2613 static inline bool
2614 cp_parser_parsing_tentatively (cp_parser* parser)
2616 return parser->context->next != NULL;
2619 /* Returns nonzero if TOKEN is a string literal. */
2621 static bool
2622 cp_parser_is_pure_string_literal (cp_token* token)
2624 return (token->type == CPP_STRING ||
2625 token->type == CPP_STRING16 ||
2626 token->type == CPP_STRING32 ||
2627 token->type == CPP_WSTRING ||
2628 token->type == CPP_UTF8STRING);
2631 /* Returns nonzero if TOKEN is a string literal
2632 of a user-defined string literal. */
2634 static bool
2635 cp_parser_is_string_literal (cp_token* token)
2637 return (cp_parser_is_pure_string_literal (token) ||
2638 token->type == CPP_STRING_USERDEF ||
2639 token->type == CPP_STRING16_USERDEF ||
2640 token->type == CPP_STRING32_USERDEF ||
2641 token->type == CPP_WSTRING_USERDEF ||
2642 token->type == CPP_UTF8STRING_USERDEF);
2645 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2647 static bool
2648 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2650 return token->keyword == keyword;
2653 /* If not parsing tentatively, issue a diagnostic of the form
2654 FILE:LINE: MESSAGE before TOKEN
2655 where TOKEN is the next token in the input stream. MESSAGE
2656 (specified by the caller) is usually of the form "expected
2657 OTHER-TOKEN". */
2659 static void
2660 cp_parser_error (cp_parser* parser, const char* gmsgid)
2662 if (!cp_parser_simulate_error (parser))
2664 cp_token *token = cp_lexer_peek_token (parser->lexer);
2665 /* This diagnostic makes more sense if it is tagged to the line
2666 of the token we just peeked at. */
2667 cp_lexer_set_source_position_from_token (token);
2669 if (token->type == CPP_PRAGMA)
2671 error_at (token->location,
2672 "%<#pragma%> is not allowed here");
2673 cp_parser_skip_to_pragma_eol (parser, token);
2674 return;
2677 c_parse_error (gmsgid,
2678 /* Because c_parser_error does not understand
2679 CPP_KEYWORD, keywords are treated like
2680 identifiers. */
2681 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2682 token->u.value, token->flags);
2686 /* Issue an error about name-lookup failing. NAME is the
2687 IDENTIFIER_NODE DECL is the result of
2688 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2689 the thing that we hoped to find. */
2691 static void
2692 cp_parser_name_lookup_error (cp_parser* parser,
2693 tree name,
2694 tree decl,
2695 name_lookup_error desired,
2696 location_t location)
2698 /* If name lookup completely failed, tell the user that NAME was not
2699 declared. */
2700 if (decl == error_mark_node)
2702 if (parser->scope && parser->scope != global_namespace)
2703 error_at (location, "%<%E::%E%> has not been declared",
2704 parser->scope, name);
2705 else if (parser->scope == global_namespace)
2706 error_at (location, "%<::%E%> has not been declared", name);
2707 else if (parser->object_scope
2708 && !CLASS_TYPE_P (parser->object_scope))
2709 error_at (location, "request for member %qE in non-class type %qT",
2710 name, parser->object_scope);
2711 else if (parser->object_scope)
2712 error_at (location, "%<%T::%E%> has not been declared",
2713 parser->object_scope, name);
2714 else
2715 error_at (location, "%qE has not been declared", name);
2717 else if (parser->scope && parser->scope != global_namespace)
2719 switch (desired)
2721 case NLE_TYPE:
2722 error_at (location, "%<%E::%E%> is not a type",
2723 parser->scope, name);
2724 break;
2725 case NLE_CXX98:
2726 error_at (location, "%<%E::%E%> is not a class or namespace",
2727 parser->scope, name);
2728 break;
2729 case NLE_NOT_CXX98:
2730 error_at (location,
2731 "%<%E::%E%> is not a class, namespace, or enumeration",
2732 parser->scope, name);
2733 break;
2734 default:
2735 gcc_unreachable ();
2739 else if (parser->scope == global_namespace)
2741 switch (desired)
2743 case NLE_TYPE:
2744 error_at (location, "%<::%E%> is not a type", name);
2745 break;
2746 case NLE_CXX98:
2747 error_at (location, "%<::%E%> is not a class or namespace", name);
2748 break;
2749 case NLE_NOT_CXX98:
2750 error_at (location,
2751 "%<::%E%> is not a class, namespace, or enumeration",
2752 name);
2753 break;
2754 default:
2755 gcc_unreachable ();
2758 else
2760 switch (desired)
2762 case NLE_TYPE:
2763 error_at (location, "%qE is not a type", name);
2764 break;
2765 case NLE_CXX98:
2766 error_at (location, "%qE is not a class or namespace", name);
2767 break;
2768 case NLE_NOT_CXX98:
2769 error_at (location,
2770 "%qE is not a class, namespace, or enumeration", name);
2771 break;
2772 default:
2773 gcc_unreachable ();
2778 /* If we are parsing tentatively, remember that an error has occurred
2779 during this tentative parse. Returns true if the error was
2780 simulated; false if a message should be issued by the caller. */
2782 static bool
2783 cp_parser_simulate_error (cp_parser* parser)
2785 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2787 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2788 return true;
2790 return false;
2793 /* This function is called when a type is defined. If type
2794 definitions are forbidden at this point, an error message is
2795 issued. */
2797 static bool
2798 cp_parser_check_type_definition (cp_parser* parser)
2800 /* If types are forbidden here, issue a message. */
2801 if (parser->type_definition_forbidden_message)
2803 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2804 in the message need to be interpreted. */
2805 error (parser->type_definition_forbidden_message);
2806 return false;
2808 return true;
2811 /* This function is called when the DECLARATOR is processed. The TYPE
2812 was a type defined in the decl-specifiers. If it is invalid to
2813 define a type in the decl-specifiers for DECLARATOR, an error is
2814 issued. TYPE_LOCATION is the location of TYPE and is used
2815 for error reporting. */
2817 static void
2818 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2819 tree type, location_t type_location)
2821 /* [dcl.fct] forbids type definitions in return types.
2822 Unfortunately, it's not easy to know whether or not we are
2823 processing a return type until after the fact. */
2824 while (declarator
2825 && (declarator->kind == cdk_pointer
2826 || declarator->kind == cdk_reference
2827 || declarator->kind == cdk_ptrmem))
2828 declarator = declarator->declarator;
2829 if (declarator
2830 && declarator->kind == cdk_function)
2832 error_at (type_location,
2833 "new types may not be defined in a return type");
2834 inform (type_location,
2835 "(perhaps a semicolon is missing after the definition of %qT)",
2836 type);
2840 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2841 "<" in any valid C++ program. If the next token is indeed "<",
2842 issue a message warning the user about what appears to be an
2843 invalid attempt to form a template-id. LOCATION is the location
2844 of the type-specifier (TYPE) */
2846 static void
2847 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2848 tree type,
2849 enum tag_types tag_type,
2850 location_t location)
2852 cp_token_position start = 0;
2854 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2856 if (TYPE_P (type))
2857 error_at (location, "%qT is not a template", type);
2858 else if (identifier_p (type))
2860 if (tag_type != none_type)
2861 error_at (location, "%qE is not a class template", type);
2862 else
2863 error_at (location, "%qE is not a template", type);
2865 else
2866 error_at (location, "invalid template-id");
2867 /* Remember the location of the invalid "<". */
2868 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2869 start = cp_lexer_token_position (parser->lexer, true);
2870 /* Consume the "<". */
2871 cp_lexer_consume_token (parser->lexer);
2872 /* Parse the template arguments. */
2873 cp_parser_enclosed_template_argument_list (parser);
2874 /* Permanently remove the invalid template arguments so that
2875 this error message is not issued again. */
2876 if (start)
2877 cp_lexer_purge_tokens_after (parser->lexer, start);
2881 /* If parsing an integral constant-expression, issue an error message
2882 about the fact that THING appeared and return true. Otherwise,
2883 return false. In either case, set
2884 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2886 static bool
2887 cp_parser_non_integral_constant_expression (cp_parser *parser,
2888 non_integral_constant thing)
2890 parser->non_integral_constant_expression_p = true;
2891 if (parser->integral_constant_expression_p)
2893 if (!parser->allow_non_integral_constant_expression_p)
2895 const char *msg = NULL;
2896 switch (thing)
2898 case NIC_FLOAT:
2899 error ("floating-point literal "
2900 "cannot appear in a constant-expression");
2901 return true;
2902 case NIC_CAST:
2903 error ("a cast to a type other than an integral or "
2904 "enumeration type cannot appear in a "
2905 "constant-expression");
2906 return true;
2907 case NIC_TYPEID:
2908 error ("%<typeid%> operator "
2909 "cannot appear in a constant-expression");
2910 return true;
2911 case NIC_NCC:
2912 error ("non-constant compound literals "
2913 "cannot appear in a constant-expression");
2914 return true;
2915 case NIC_FUNC_CALL:
2916 error ("a function call "
2917 "cannot appear in a constant-expression");
2918 return true;
2919 case NIC_INC:
2920 error ("an increment "
2921 "cannot appear in a constant-expression");
2922 return true;
2923 case NIC_DEC:
2924 error ("an decrement "
2925 "cannot appear in a constant-expression");
2926 return true;
2927 case NIC_ARRAY_REF:
2928 error ("an array reference "
2929 "cannot appear in a constant-expression");
2930 return true;
2931 case NIC_ADDR_LABEL:
2932 error ("the address of a label "
2933 "cannot appear in a constant-expression");
2934 return true;
2935 case NIC_OVERLOADED:
2936 error ("calls to overloaded operators "
2937 "cannot appear in a constant-expression");
2938 return true;
2939 case NIC_ASSIGNMENT:
2940 error ("an assignment cannot appear in a constant-expression");
2941 return true;
2942 case NIC_COMMA:
2943 error ("a comma operator "
2944 "cannot appear in a constant-expression");
2945 return true;
2946 case NIC_CONSTRUCTOR:
2947 error ("a call to a constructor "
2948 "cannot appear in a constant-expression");
2949 return true;
2950 case NIC_TRANSACTION:
2951 error ("a transaction expression "
2952 "cannot appear in a constant-expression");
2953 return true;
2954 case NIC_THIS:
2955 msg = "this";
2956 break;
2957 case NIC_FUNC_NAME:
2958 msg = "__FUNCTION__";
2959 break;
2960 case NIC_PRETTY_FUNC:
2961 msg = "__PRETTY_FUNCTION__";
2962 break;
2963 case NIC_C99_FUNC:
2964 msg = "__func__";
2965 break;
2966 case NIC_VA_ARG:
2967 msg = "va_arg";
2968 break;
2969 case NIC_ARROW:
2970 msg = "->";
2971 break;
2972 case NIC_POINT:
2973 msg = ".";
2974 break;
2975 case NIC_STAR:
2976 msg = "*";
2977 break;
2978 case NIC_ADDR:
2979 msg = "&";
2980 break;
2981 case NIC_PREINCREMENT:
2982 msg = "++";
2983 break;
2984 case NIC_PREDECREMENT:
2985 msg = "--";
2986 break;
2987 case NIC_NEW:
2988 msg = "new";
2989 break;
2990 case NIC_DEL:
2991 msg = "delete";
2992 break;
2993 default:
2994 gcc_unreachable ();
2996 if (msg)
2997 error ("%qs cannot appear in a constant-expression", msg);
2998 return true;
3001 return false;
3004 /* Emit a diagnostic for an invalid type name. This function commits
3005 to the current active tentative parse, if any. (Otherwise, the
3006 problematic construct might be encountered again later, resulting
3007 in duplicate error messages.) LOCATION is the location of ID. */
3009 static void
3010 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3011 location_t location)
3013 tree decl, ambiguous_decls;
3014 cp_parser_commit_to_tentative_parse (parser);
3015 /* Try to lookup the identifier. */
3016 decl = cp_parser_lookup_name (parser, id, none_type,
3017 /*is_template=*/false,
3018 /*is_namespace=*/false,
3019 /*check_dependency=*/true,
3020 &ambiguous_decls, location);
3021 if (ambiguous_decls)
3022 /* If the lookup was ambiguous, an error will already have
3023 been issued. */
3024 return;
3025 /* If the lookup found a template-name, it means that the user forgot
3026 to specify an argument list. Emit a useful error message. */
3027 if (TREE_CODE (decl) == TEMPLATE_DECL)
3028 error_at (location,
3029 "invalid use of template-name %qE without an argument list",
3030 decl);
3031 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3032 error_at (location, "invalid use of destructor %qD as a type", id);
3033 else if (TREE_CODE (decl) == TYPE_DECL)
3034 /* Something like 'unsigned A a;' */
3035 error_at (location, "invalid combination of multiple type-specifiers");
3036 else if (!parser->scope)
3038 /* Issue an error message. */
3039 error_at (location, "%qE does not name a type", id);
3040 /* If we're in a template class, it's possible that the user was
3041 referring to a type from a base class. For example:
3043 template <typename T> struct A { typedef T X; };
3044 template <typename T> struct B : public A<T> { X x; };
3046 The user should have said "typename A<T>::X". */
3047 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3048 inform (location, "C++11 %<constexpr%> only available with "
3049 "-std=c++11 or -std=gnu++11");
3050 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3051 inform (location, "C++11 %<noexcept%> only available with "
3052 "-std=c++11 or -std=gnu++11");
3053 else if (cxx_dialect < cxx11
3054 && TREE_CODE (id) == IDENTIFIER_NODE
3055 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3056 inform (location, "C++11 %<thread_local%> only available with "
3057 "-std=c++11 or -std=gnu++11");
3058 else if (processing_template_decl && current_class_type
3059 && TYPE_BINFO (current_class_type))
3061 tree b;
3063 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3065 b = TREE_CHAIN (b))
3067 tree base_type = BINFO_TYPE (b);
3068 if (CLASS_TYPE_P (base_type)
3069 && dependent_type_p (base_type))
3071 tree field;
3072 /* Go from a particular instantiation of the
3073 template (which will have an empty TYPE_FIELDs),
3074 to the main version. */
3075 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3076 for (field = TYPE_FIELDS (base_type);
3077 field;
3078 field = DECL_CHAIN (field))
3079 if (TREE_CODE (field) == TYPE_DECL
3080 && DECL_NAME (field) == id)
3082 inform (location,
3083 "(perhaps %<typename %T::%E%> was intended)",
3084 BINFO_TYPE (b), id);
3085 break;
3087 if (field)
3088 break;
3093 /* Here we diagnose qualified-ids where the scope is actually correct,
3094 but the identifier does not resolve to a valid type name. */
3095 else if (parser->scope != error_mark_node)
3097 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3099 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3100 error_at (location_of (id),
3101 "%qE in namespace %qE does not name a template type",
3102 id, parser->scope);
3103 else
3104 error_at (location_of (id),
3105 "%qE in namespace %qE does not name a type",
3106 id, parser->scope);
3108 else if (CLASS_TYPE_P (parser->scope)
3109 && constructor_name_p (id, parser->scope))
3111 /* A<T>::A<T>() */
3112 error_at (location, "%<%T::%E%> names the constructor, not"
3113 " the type", parser->scope, id);
3114 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3115 error_at (location, "and %qT has no template constructors",
3116 parser->scope);
3118 else if (TYPE_P (parser->scope)
3119 && dependent_scope_p (parser->scope))
3120 error_at (location, "need %<typename%> before %<%T::%E%> because "
3121 "%qT is a dependent scope",
3122 parser->scope, id, parser->scope);
3123 else if (TYPE_P (parser->scope))
3125 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3126 error_at (location_of (id),
3127 "%qE in %q#T does not name a template type",
3128 id, parser->scope);
3129 else
3130 error_at (location_of (id),
3131 "%qE in %q#T does not name a type",
3132 id, parser->scope);
3134 else
3135 gcc_unreachable ();
3139 /* Check for a common situation where a type-name should be present,
3140 but is not, and issue a sensible error message. Returns true if an
3141 invalid type-name was detected.
3143 The situation handled by this function are variable declarations of the
3144 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3145 Usually, `ID' should name a type, but if we got here it means that it
3146 does not. We try to emit the best possible error message depending on
3147 how exactly the id-expression looks like. */
3149 static bool
3150 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3152 tree id;
3153 cp_token *token = cp_lexer_peek_token (parser->lexer);
3155 /* Avoid duplicate error about ambiguous lookup. */
3156 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3158 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3159 if (next->type == CPP_NAME && next->error_reported)
3160 goto out;
3163 cp_parser_parse_tentatively (parser);
3164 id = cp_parser_id_expression (parser,
3165 /*template_keyword_p=*/false,
3166 /*check_dependency_p=*/true,
3167 /*template_p=*/NULL,
3168 /*declarator_p=*/true,
3169 /*optional_p=*/false);
3170 /* If the next token is a (, this is a function with no explicit return
3171 type, i.e. constructor, destructor or conversion op. */
3172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3173 || TREE_CODE (id) == TYPE_DECL)
3175 cp_parser_abort_tentative_parse (parser);
3176 return false;
3178 if (!cp_parser_parse_definitely (parser))
3179 return false;
3181 /* Emit a diagnostic for the invalid type. */
3182 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3183 out:
3184 /* If we aren't in the middle of a declarator (i.e. in a
3185 parameter-declaration-clause), skip to the end of the declaration;
3186 there's no point in trying to process it. */
3187 if (!parser->in_declarator_p)
3188 cp_parser_skip_to_end_of_block_or_statement (parser);
3189 return true;
3192 /* Consume tokens up to, and including, the next non-nested closing `)'.
3193 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3194 are doing error recovery. Returns -1 if OR_COMMA is true and we
3195 found an unnested comma. */
3197 static int
3198 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3199 bool recovering,
3200 bool or_comma,
3201 bool consume_paren)
3203 unsigned paren_depth = 0;
3204 unsigned brace_depth = 0;
3205 unsigned square_depth = 0;
3207 if (recovering && !or_comma
3208 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3209 return 0;
3211 while (true)
3213 cp_token * token = cp_lexer_peek_token (parser->lexer);
3215 switch (token->type)
3217 case CPP_EOF:
3218 case CPP_PRAGMA_EOL:
3219 /* If we've run out of tokens, then there is no closing `)'. */
3220 return 0;
3222 /* This is good for lambda expression capture-lists. */
3223 case CPP_OPEN_SQUARE:
3224 ++square_depth;
3225 break;
3226 case CPP_CLOSE_SQUARE:
3227 if (!square_depth--)
3228 return 0;
3229 break;
3231 case CPP_SEMICOLON:
3232 /* This matches the processing in skip_to_end_of_statement. */
3233 if (!brace_depth)
3234 return 0;
3235 break;
3237 case CPP_OPEN_BRACE:
3238 ++brace_depth;
3239 break;
3240 case CPP_CLOSE_BRACE:
3241 if (!brace_depth--)
3242 return 0;
3243 break;
3245 case CPP_COMMA:
3246 if (recovering && or_comma && !brace_depth && !paren_depth
3247 && !square_depth)
3248 return -1;
3249 break;
3251 case CPP_OPEN_PAREN:
3252 if (!brace_depth)
3253 ++paren_depth;
3254 break;
3256 case CPP_CLOSE_PAREN:
3257 if (!brace_depth && !paren_depth--)
3259 if (consume_paren)
3260 cp_lexer_consume_token (parser->lexer);
3261 return 1;
3263 break;
3265 default:
3266 break;
3269 /* Consume the token. */
3270 cp_lexer_consume_token (parser->lexer);
3274 /* Consume tokens until we reach the end of the current statement.
3275 Normally, that will be just before consuming a `;'. However, if a
3276 non-nested `}' comes first, then we stop before consuming that. */
3278 static void
3279 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3281 unsigned nesting_depth = 0;
3283 /* Unwind generic function template scope if necessary. */
3284 if (parser->fully_implicit_function_template_p)
3285 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3287 while (true)
3289 cp_token *token = cp_lexer_peek_token (parser->lexer);
3291 switch (token->type)
3293 case CPP_EOF:
3294 case CPP_PRAGMA_EOL:
3295 /* If we've run out of tokens, stop. */
3296 return;
3298 case CPP_SEMICOLON:
3299 /* If the next token is a `;', we have reached the end of the
3300 statement. */
3301 if (!nesting_depth)
3302 return;
3303 break;
3305 case CPP_CLOSE_BRACE:
3306 /* If this is a non-nested '}', stop before consuming it.
3307 That way, when confronted with something like:
3309 { 3 + }
3311 we stop before consuming the closing '}', even though we
3312 have not yet reached a `;'. */
3313 if (nesting_depth == 0)
3314 return;
3316 /* If it is the closing '}' for a block that we have
3317 scanned, stop -- but only after consuming the token.
3318 That way given:
3320 void f g () { ... }
3321 typedef int I;
3323 we will stop after the body of the erroneously declared
3324 function, but before consuming the following `typedef'
3325 declaration. */
3326 if (--nesting_depth == 0)
3328 cp_lexer_consume_token (parser->lexer);
3329 return;
3332 case CPP_OPEN_BRACE:
3333 ++nesting_depth;
3334 break;
3336 default:
3337 break;
3340 /* Consume the token. */
3341 cp_lexer_consume_token (parser->lexer);
3345 /* This function is called at the end of a statement or declaration.
3346 If the next token is a semicolon, it is consumed; otherwise, error
3347 recovery is attempted. */
3349 static void
3350 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3352 /* Look for the trailing `;'. */
3353 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3355 /* If there is additional (erroneous) input, skip to the end of
3356 the statement. */
3357 cp_parser_skip_to_end_of_statement (parser);
3358 /* If the next token is now a `;', consume it. */
3359 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3360 cp_lexer_consume_token (parser->lexer);
3364 /* Skip tokens until we have consumed an entire block, or until we
3365 have consumed a non-nested `;'. */
3367 static void
3368 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3370 int nesting_depth = 0;
3372 /* Unwind generic function template scope if necessary. */
3373 if (parser->fully_implicit_function_template_p)
3374 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3376 while (nesting_depth >= 0)
3378 cp_token *token = cp_lexer_peek_token (parser->lexer);
3380 switch (token->type)
3382 case CPP_EOF:
3383 case CPP_PRAGMA_EOL:
3384 /* If we've run out of tokens, stop. */
3385 return;
3387 case CPP_SEMICOLON:
3388 /* Stop if this is an unnested ';'. */
3389 if (!nesting_depth)
3390 nesting_depth = -1;
3391 break;
3393 case CPP_CLOSE_BRACE:
3394 /* Stop if this is an unnested '}', or closes the outermost
3395 nesting level. */
3396 nesting_depth--;
3397 if (nesting_depth < 0)
3398 return;
3399 if (!nesting_depth)
3400 nesting_depth = -1;
3401 break;
3403 case CPP_OPEN_BRACE:
3404 /* Nest. */
3405 nesting_depth++;
3406 break;
3408 default:
3409 break;
3412 /* Consume the token. */
3413 cp_lexer_consume_token (parser->lexer);
3417 /* Skip tokens until a non-nested closing curly brace is the next
3418 token, or there are no more tokens. Return true in the first case,
3419 false otherwise. */
3421 static bool
3422 cp_parser_skip_to_closing_brace (cp_parser *parser)
3424 unsigned nesting_depth = 0;
3426 while (true)
3428 cp_token *token = cp_lexer_peek_token (parser->lexer);
3430 switch (token->type)
3432 case CPP_EOF:
3433 case CPP_PRAGMA_EOL:
3434 /* If we've run out of tokens, stop. */
3435 return false;
3437 case CPP_CLOSE_BRACE:
3438 /* If the next token is a non-nested `}', then we have reached
3439 the end of the current block. */
3440 if (nesting_depth-- == 0)
3441 return true;
3442 break;
3444 case CPP_OPEN_BRACE:
3445 /* If it the next token is a `{', then we are entering a new
3446 block. Consume the entire block. */
3447 ++nesting_depth;
3448 break;
3450 default:
3451 break;
3454 /* Consume the token. */
3455 cp_lexer_consume_token (parser->lexer);
3459 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3460 parameter is the PRAGMA token, allowing us to purge the entire pragma
3461 sequence. */
3463 static void
3464 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3466 cp_token *token;
3468 parser->lexer->in_pragma = false;
3471 token = cp_lexer_consume_token (parser->lexer);
3472 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3474 /* Ensure that the pragma is not parsed again. */
3475 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3478 /* Require pragma end of line, resyncing with it as necessary. The
3479 arguments are as for cp_parser_skip_to_pragma_eol. */
3481 static void
3482 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3484 parser->lexer->in_pragma = false;
3485 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3486 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3489 /* This is a simple wrapper around make_typename_type. When the id is
3490 an unresolved identifier node, we can provide a superior diagnostic
3491 using cp_parser_diagnose_invalid_type_name. */
3493 static tree
3494 cp_parser_make_typename_type (cp_parser *parser, tree id,
3495 location_t id_location)
3497 tree result;
3498 if (identifier_p (id))
3500 result = make_typename_type (parser->scope, id, typename_type,
3501 /*complain=*/tf_none);
3502 if (result == error_mark_node)
3503 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3504 return result;
3506 return make_typename_type (parser->scope, id, typename_type, tf_error);
3509 /* This is a wrapper around the
3510 make_{pointer,ptrmem,reference}_declarator functions that decides
3511 which one to call based on the CODE and CLASS_TYPE arguments. The
3512 CODE argument should be one of the values returned by
3513 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3514 appertain to the pointer or reference. */
3516 static cp_declarator *
3517 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3518 cp_cv_quals cv_qualifiers,
3519 cp_declarator *target,
3520 tree attributes)
3522 if (code == ERROR_MARK)
3523 return cp_error_declarator;
3525 if (code == INDIRECT_REF)
3526 if (class_type == NULL_TREE)
3527 return make_pointer_declarator (cv_qualifiers, target, attributes);
3528 else
3529 return make_ptrmem_declarator (cv_qualifiers, class_type,
3530 target, attributes);
3531 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3532 return make_reference_declarator (cv_qualifiers, target,
3533 false, attributes);
3534 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3535 return make_reference_declarator (cv_qualifiers, target,
3536 true, attributes);
3537 gcc_unreachable ();
3540 /* Create a new C++ parser. */
3542 static cp_parser *
3543 cp_parser_new (void)
3545 cp_parser *parser;
3546 cp_lexer *lexer;
3547 unsigned i;
3549 /* cp_lexer_new_main is called before doing GC allocation because
3550 cp_lexer_new_main might load a PCH file. */
3551 lexer = cp_lexer_new_main ();
3553 /* Initialize the binops_by_token so that we can get the tree
3554 directly from the token. */
3555 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3556 binops_by_token[binops[i].token_type] = binops[i];
3558 parser = ggc_cleared_alloc<cp_parser> ();
3559 parser->lexer = lexer;
3560 parser->context = cp_parser_context_new (NULL);
3562 /* For now, we always accept GNU extensions. */
3563 parser->allow_gnu_extensions_p = 1;
3565 /* The `>' token is a greater-than operator, not the end of a
3566 template-id. */
3567 parser->greater_than_is_operator_p = true;
3569 parser->default_arg_ok_p = true;
3571 /* We are not parsing a constant-expression. */
3572 parser->integral_constant_expression_p = false;
3573 parser->allow_non_integral_constant_expression_p = false;
3574 parser->non_integral_constant_expression_p = false;
3576 /* Local variable names are not forbidden. */
3577 parser->local_variables_forbidden_p = false;
3579 /* We are not processing an `extern "C"' declaration. */
3580 parser->in_unbraced_linkage_specification_p = false;
3582 /* We are not processing a declarator. */
3583 parser->in_declarator_p = false;
3585 /* We are not processing a template-argument-list. */
3586 parser->in_template_argument_list_p = false;
3588 /* We are not in an iteration statement. */
3589 parser->in_statement = 0;
3591 /* We are not in a switch statement. */
3592 parser->in_switch_statement_p = false;
3594 /* We are not parsing a type-id inside an expression. */
3595 parser->in_type_id_in_expr_p = false;
3597 /* Declarations aren't implicitly extern "C". */
3598 parser->implicit_extern_c = false;
3600 /* String literals should be translated to the execution character set. */
3601 parser->translate_strings_p = true;
3603 /* We are not parsing a function body. */
3604 parser->in_function_body = false;
3606 /* We can correct until told otherwise. */
3607 parser->colon_corrects_to_scope_p = true;
3609 /* The unparsed function queue is empty. */
3610 push_unparsed_function_queues (parser);
3612 /* There are no classes being defined. */
3613 parser->num_classes_being_defined = 0;
3615 /* No template parameters apply. */
3616 parser->num_template_parameter_lists = 0;
3618 /* Not declaring an implicit function template. */
3619 parser->auto_is_implicit_function_template_parm_p = false;
3620 parser->fully_implicit_function_template_p = false;
3621 parser->implicit_template_parms = 0;
3622 parser->implicit_template_scope = 0;
3624 return parser;
3627 /* Create a cp_lexer structure which will emit the tokens in CACHE
3628 and push it onto the parser's lexer stack. This is used for delayed
3629 parsing of in-class method bodies and default arguments, and should
3630 not be confused with tentative parsing. */
3631 static void
3632 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3634 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3635 lexer->next = parser->lexer;
3636 parser->lexer = lexer;
3638 /* Move the current source position to that of the first token in the
3639 new lexer. */
3640 cp_lexer_set_source_position_from_token (lexer->next_token);
3643 /* Pop the top lexer off the parser stack. This is never used for the
3644 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3645 static void
3646 cp_parser_pop_lexer (cp_parser *parser)
3648 cp_lexer *lexer = parser->lexer;
3649 parser->lexer = lexer->next;
3650 cp_lexer_destroy (lexer);
3652 /* Put the current source position back where it was before this
3653 lexer was pushed. */
3654 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3657 /* Lexical conventions [gram.lex] */
3659 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3660 identifier. */
3662 static tree
3663 cp_parser_identifier (cp_parser* parser)
3665 cp_token *token;
3667 /* Look for the identifier. */
3668 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3669 /* Return the value. */
3670 return token ? token->u.value : error_mark_node;
3673 /* Parse a sequence of adjacent string constants. Returns a
3674 TREE_STRING representing the combined, nul-terminated string
3675 constant. If TRANSLATE is true, translate the string to the
3676 execution character set. If WIDE_OK is true, a wide string is
3677 invalid here.
3679 C++98 [lex.string] says that if a narrow string literal token is
3680 adjacent to a wide string literal token, the behavior is undefined.
3681 However, C99 6.4.5p4 says that this results in a wide string literal.
3682 We follow C99 here, for consistency with the C front end.
3684 This code is largely lifted from lex_string() in c-lex.c.
3686 FUTURE: ObjC++ will need to handle @-strings here. */
3687 static tree
3688 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3689 bool lookup_udlit = true)
3691 tree value;
3692 size_t count;
3693 struct obstack str_ob;
3694 cpp_string str, istr, *strs;
3695 cp_token *tok;
3696 enum cpp_ttype type, curr_type;
3697 int have_suffix_p = 0;
3698 tree string_tree;
3699 tree suffix_id = NULL_TREE;
3700 bool curr_tok_is_userdef_p = false;
3702 tok = cp_lexer_peek_token (parser->lexer);
3703 if (!cp_parser_is_string_literal (tok))
3705 cp_parser_error (parser, "expected string-literal");
3706 return error_mark_node;
3709 if (cpp_userdef_string_p (tok->type))
3711 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3712 curr_type = cpp_userdef_string_remove_type (tok->type);
3713 curr_tok_is_userdef_p = true;
3715 else
3717 string_tree = tok->u.value;
3718 curr_type = tok->type;
3720 type = curr_type;
3722 /* Try to avoid the overhead of creating and destroying an obstack
3723 for the common case of just one string. */
3724 if (!cp_parser_is_string_literal
3725 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3727 cp_lexer_consume_token (parser->lexer);
3729 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3730 str.len = TREE_STRING_LENGTH (string_tree);
3731 count = 1;
3733 if (curr_tok_is_userdef_p)
3735 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3736 have_suffix_p = 1;
3737 curr_type = cpp_userdef_string_remove_type (tok->type);
3739 else
3740 curr_type = tok->type;
3742 strs = &str;
3744 else
3746 gcc_obstack_init (&str_ob);
3747 count = 0;
3751 cp_lexer_consume_token (parser->lexer);
3752 count++;
3753 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3754 str.len = TREE_STRING_LENGTH (string_tree);
3756 if (curr_tok_is_userdef_p)
3758 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3759 if (have_suffix_p == 0)
3761 suffix_id = curr_suffix_id;
3762 have_suffix_p = 1;
3764 else if (have_suffix_p == 1
3765 && curr_suffix_id != suffix_id)
3767 error ("inconsistent user-defined literal suffixes"
3768 " %qD and %qD in string literal",
3769 suffix_id, curr_suffix_id);
3770 have_suffix_p = -1;
3772 curr_type = cpp_userdef_string_remove_type (tok->type);
3774 else
3775 curr_type = tok->type;
3777 if (type != curr_type)
3779 if (type == CPP_STRING)
3780 type = curr_type;
3781 else if (curr_type != CPP_STRING)
3782 error_at (tok->location,
3783 "unsupported non-standard concatenation "
3784 "of string literals");
3787 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3789 tok = cp_lexer_peek_token (parser->lexer);
3790 if (cpp_userdef_string_p (tok->type))
3792 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3793 curr_type = cpp_userdef_string_remove_type (tok->type);
3794 curr_tok_is_userdef_p = true;
3796 else
3798 string_tree = tok->u.value;
3799 curr_type = tok->type;
3800 curr_tok_is_userdef_p = false;
3803 while (cp_parser_is_string_literal (tok));
3805 strs = (cpp_string *) obstack_finish (&str_ob);
3808 if (type != CPP_STRING && !wide_ok)
3810 cp_parser_error (parser, "a wide string is invalid in this context");
3811 type = CPP_STRING;
3814 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3815 (parse_in, strs, count, &istr, type))
3817 value = build_string (istr.len, (const char *)istr.text);
3818 free (CONST_CAST (unsigned char *, istr.text));
3820 switch (type)
3822 default:
3823 case CPP_STRING:
3824 case CPP_UTF8STRING:
3825 TREE_TYPE (value) = char_array_type_node;
3826 break;
3827 case CPP_STRING16:
3828 TREE_TYPE (value) = char16_array_type_node;
3829 break;
3830 case CPP_STRING32:
3831 TREE_TYPE (value) = char32_array_type_node;
3832 break;
3833 case CPP_WSTRING:
3834 TREE_TYPE (value) = wchar_array_type_node;
3835 break;
3838 value = fix_string_type (value);
3840 if (have_suffix_p)
3842 tree literal = build_userdef_literal (suffix_id, value,
3843 OT_NONE, NULL_TREE);
3844 if (lookup_udlit)
3845 value = cp_parser_userdef_string_literal (literal);
3846 else
3847 value = literal;
3850 else
3851 /* cpp_interpret_string has issued an error. */
3852 value = error_mark_node;
3854 if (count > 1)
3855 obstack_free (&str_ob, 0);
3857 return value;
3860 /* Look up a literal operator with the name and the exact arguments. */
3862 static tree
3863 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3865 tree decl, fns;
3866 decl = lookup_name (name);
3867 if (!decl || !is_overloaded_fn (decl))
3868 return error_mark_node;
3870 for (fns = decl; fns; fns = OVL_NEXT (fns))
3872 unsigned int ix;
3873 bool found = true;
3874 tree fn = OVL_CURRENT (fns);
3875 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3876 if (parmtypes != NULL_TREE)
3878 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3879 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3881 tree tparm = TREE_VALUE (parmtypes);
3882 tree targ = TREE_TYPE ((*args)[ix]);
3883 bool ptr = TYPE_PTR_P (tparm);
3884 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3885 if ((ptr || arr || !same_type_p (tparm, targ))
3886 && (!ptr || !arr
3887 || !same_type_p (TREE_TYPE (tparm),
3888 TREE_TYPE (targ))))
3889 found = false;
3891 if (found
3892 && ix == vec_safe_length (args)
3893 /* May be this should be sufficient_parms_p instead,
3894 depending on how exactly should user-defined literals
3895 work in presence of default arguments on the literal
3896 operator parameters. */
3897 && parmtypes == void_list_node)
3898 return decl;
3902 return error_mark_node;
3905 /* Parse a user-defined char constant. Returns a call to a user-defined
3906 literal operator taking the character as an argument. */
3908 static tree
3909 cp_parser_userdef_char_literal (cp_parser *parser)
3911 cp_token *token = cp_lexer_consume_token (parser->lexer);
3912 tree literal = token->u.value;
3913 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3914 tree value = USERDEF_LITERAL_VALUE (literal);
3915 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3916 tree decl, result;
3918 /* Build up a call to the user-defined operator */
3919 /* Lookup the name we got back from the id-expression. */
3920 vec<tree, va_gc> *args = make_tree_vector ();
3921 vec_safe_push (args, value);
3922 decl = lookup_literal_operator (name, args);
3923 if (!decl || decl == error_mark_node)
3925 error ("unable to find character literal operator %qD with %qT argument",
3926 name, TREE_TYPE (value));
3927 release_tree_vector (args);
3928 return error_mark_node;
3930 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3931 release_tree_vector (args);
3932 return result;
3935 /* A subroutine of cp_parser_userdef_numeric_literal to
3936 create a char... template parameter pack from a string node. */
3938 static tree
3939 make_char_string_pack (tree value)
3941 tree charvec;
3942 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3943 const char *str = TREE_STRING_POINTER (value);
3944 int i, len = TREE_STRING_LENGTH (value) - 1;
3945 tree argvec = make_tree_vec (1);
3947 /* Fill in CHARVEC with all of the parameters. */
3948 charvec = make_tree_vec (len);
3949 for (i = 0; i < len; ++i)
3950 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3952 /* Build the argument packs. */
3953 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3954 TREE_TYPE (argpack) = char_type_node;
3956 TREE_VEC_ELT (argvec, 0) = argpack;
3958 return argvec;
3961 /* A subroutine of cp_parser_userdef_numeric_literal to
3962 create a char... template parameter pack from a string node. */
3964 static tree
3965 make_string_pack (tree value)
3967 tree charvec;
3968 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3969 const unsigned char *str
3970 = (const unsigned char *) TREE_STRING_POINTER (value);
3971 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3972 int len = TREE_STRING_LENGTH (value) / sz - 1;
3973 tree argvec = make_tree_vec (2);
3975 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3976 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3978 /* First template parm is character type. */
3979 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3981 /* Fill in CHARVEC with all of the parameters. */
3982 charvec = make_tree_vec (len);
3983 for (int i = 0; i < len; ++i)
3984 TREE_VEC_ELT (charvec, i)
3985 = double_int_to_tree (str_char_type_node,
3986 double_int::from_buffer (str + i * sz, sz));
3988 /* Build the argument packs. */
3989 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3990 TREE_TYPE (argpack) = str_char_type_node;
3992 TREE_VEC_ELT (argvec, 1) = argpack;
3994 return argvec;
3997 /* Parse a user-defined numeric constant. returns a call to a user-defined
3998 literal operator. */
4000 static tree
4001 cp_parser_userdef_numeric_literal (cp_parser *parser)
4003 cp_token *token = cp_lexer_consume_token (parser->lexer);
4004 tree literal = token->u.value;
4005 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4006 tree value = USERDEF_LITERAL_VALUE (literal);
4007 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4008 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4009 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4010 tree decl, result;
4011 vec<tree, va_gc> *args;
4013 /* Look for a literal operator taking the exact type of numeric argument
4014 as the literal value. */
4015 args = make_tree_vector ();
4016 vec_safe_push (args, value);
4017 decl = lookup_literal_operator (name, args);
4018 if (decl && decl != error_mark_node)
4020 result = finish_call_expr (decl, &args, false, true,
4021 tf_warning_or_error);
4023 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4025 warning_at (token->location, OPT_Woverflow,
4026 "integer literal exceeds range of %qT type",
4027 long_long_unsigned_type_node);
4029 else
4031 if (overflow > 0)
4032 warning_at (token->location, OPT_Woverflow,
4033 "floating literal exceeds range of %qT type",
4034 long_double_type_node);
4035 else if (overflow < 0)
4036 warning_at (token->location, OPT_Woverflow,
4037 "floating literal truncated to zero");
4040 release_tree_vector (args);
4041 return result;
4043 release_tree_vector (args);
4045 /* If the numeric argument didn't work, look for a raw literal
4046 operator taking a const char* argument consisting of the number
4047 in string format. */
4048 args = make_tree_vector ();
4049 vec_safe_push (args, num_string);
4050 decl = lookup_literal_operator (name, args);
4051 if (decl && decl != error_mark_node)
4053 result = finish_call_expr (decl, &args, false, true,
4054 tf_warning_or_error);
4055 release_tree_vector (args);
4056 return result;
4058 release_tree_vector (args);
4060 /* If the raw literal didn't work, look for a non-type template
4061 function with parameter pack char.... Call the function with
4062 template parameter characters representing the number. */
4063 args = make_tree_vector ();
4064 decl = lookup_literal_operator (name, args);
4065 if (decl && decl != error_mark_node)
4067 tree tmpl_args = make_char_string_pack (num_string);
4068 decl = lookup_template_function (decl, tmpl_args);
4069 result = finish_call_expr (decl, &args, false, true,
4070 tf_warning_or_error);
4071 release_tree_vector (args);
4072 return result;
4075 release_tree_vector (args);
4077 error ("unable to find numeric literal operator %qD", name);
4078 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4079 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4080 "to enable more built-in suffixes");
4081 return error_mark_node;
4084 /* Parse a user-defined string constant. Returns a call to a user-defined
4085 literal operator taking a character pointer and the length of the string
4086 as arguments. */
4088 static tree
4089 cp_parser_userdef_string_literal (tree literal)
4091 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4092 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4093 tree value = USERDEF_LITERAL_VALUE (literal);
4094 int len = TREE_STRING_LENGTH (value)
4095 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4096 tree decl, result;
4097 vec<tree, va_gc> *args;
4099 /* Build up a call to the user-defined operator. */
4100 /* Lookup the name we got back from the id-expression. */
4101 args = make_tree_vector ();
4102 vec_safe_push (args, value);
4103 vec_safe_push (args, build_int_cst (size_type_node, len));
4104 decl = lookup_literal_operator (name, args);
4106 if (decl && decl != error_mark_node)
4108 result = finish_call_expr (decl, &args, false, true,
4109 tf_warning_or_error);
4110 release_tree_vector (args);
4111 return result;
4113 release_tree_vector (args);
4115 /* Look for a template function with typename parameter CharT
4116 and parameter pack CharT... Call the function with
4117 template parameter characters representing the string. */
4118 args = make_tree_vector ();
4119 decl = lookup_literal_operator (name, args);
4120 if (decl && decl != error_mark_node)
4122 tree tmpl_args = make_string_pack (value);
4123 decl = lookup_template_function (decl, tmpl_args);
4124 result = finish_call_expr (decl, &args, false, true,
4125 tf_warning_or_error);
4126 release_tree_vector (args);
4127 return result;
4129 release_tree_vector (args);
4131 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4132 name, TREE_TYPE (value), size_type_node);
4133 return error_mark_node;
4137 /* Basic concepts [gram.basic] */
4139 /* Parse a translation-unit.
4141 translation-unit:
4142 declaration-seq [opt]
4144 Returns TRUE if all went well. */
4146 static bool
4147 cp_parser_translation_unit (cp_parser* parser)
4149 /* The address of the first non-permanent object on the declarator
4150 obstack. */
4151 static void *declarator_obstack_base;
4153 bool success;
4155 /* Create the declarator obstack, if necessary. */
4156 if (!cp_error_declarator)
4158 gcc_obstack_init (&declarator_obstack);
4159 /* Create the error declarator. */
4160 cp_error_declarator = make_declarator (cdk_error);
4161 /* Create the empty parameter list. */
4162 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4163 /* Remember where the base of the declarator obstack lies. */
4164 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4167 cp_parser_declaration_seq_opt (parser);
4169 /* If there are no tokens left then all went well. */
4170 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4172 /* Get rid of the token array; we don't need it any more. */
4173 cp_lexer_destroy (parser->lexer);
4174 parser->lexer = NULL;
4176 /* This file might have been a context that's implicitly extern
4177 "C". If so, pop the lang context. (Only relevant for PCH.) */
4178 if (parser->implicit_extern_c)
4180 pop_lang_context ();
4181 parser->implicit_extern_c = false;
4184 /* Finish up. */
4185 finish_translation_unit ();
4187 success = true;
4189 else
4191 cp_parser_error (parser, "expected declaration");
4192 success = false;
4195 /* Make sure the declarator obstack was fully cleaned up. */
4196 gcc_assert (obstack_next_free (&declarator_obstack)
4197 == declarator_obstack_base);
4199 /* All went well. */
4200 return success;
4203 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4204 decltype context. */
4206 static inline tsubst_flags_t
4207 complain_flags (bool decltype_p)
4209 tsubst_flags_t complain = tf_warning_or_error;
4210 if (decltype_p)
4211 complain |= tf_decltype;
4212 return complain;
4215 /* We're about to parse a collection of statements. If we're currently
4216 parsing tentatively, set up a firewall so that any nested
4217 cp_parser_commit_to_tentative_parse won't affect the current context. */
4219 static cp_token_position
4220 cp_parser_start_tentative_firewall (cp_parser *parser)
4222 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4223 return 0;
4225 cp_parser_parse_tentatively (parser);
4226 cp_parser_commit_to_topmost_tentative_parse (parser);
4227 return cp_lexer_token_position (parser->lexer, false);
4230 /* We've finished parsing the collection of statements. Wrap up the
4231 firewall and replace the relevant tokens with the parsed form. */
4233 static void
4234 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4235 tree expr)
4237 if (!start)
4238 return;
4240 /* Finish the firewall level. */
4241 cp_parser_parse_definitely (parser);
4242 /* And remember the result of the parse for when we try again. */
4243 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4244 token->type = CPP_PREPARSED_EXPR;
4245 token->u.value = expr;
4246 token->keyword = RID_MAX;
4247 cp_lexer_purge_tokens_after (parser->lexer, start);
4250 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4251 enclosing parentheses. */
4253 static tree
4254 cp_parser_statement_expr (cp_parser *parser)
4256 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4258 /* Consume the '('. */
4259 cp_lexer_consume_token (parser->lexer);
4260 /* Start the statement-expression. */
4261 tree expr = begin_stmt_expr ();
4262 /* Parse the compound-statement. */
4263 cp_parser_compound_statement (parser, expr, false, false);
4264 /* Finish up. */
4265 expr = finish_stmt_expr (expr, false);
4266 /* Consume the ')'. */
4267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4268 cp_parser_skip_to_end_of_statement (parser);
4270 cp_parser_end_tentative_firewall (parser, start, expr);
4271 return expr;
4274 /* Expressions [gram.expr] */
4276 /* Parse a primary-expression.
4278 primary-expression:
4279 literal
4280 this
4281 ( expression )
4282 id-expression
4283 lambda-expression (C++11)
4285 GNU Extensions:
4287 primary-expression:
4288 ( compound-statement )
4289 __builtin_va_arg ( assignment-expression , type-id )
4290 __builtin_offsetof ( type-id , offsetof-expression )
4292 C++ Extensions:
4293 __has_nothrow_assign ( type-id )
4294 __has_nothrow_constructor ( type-id )
4295 __has_nothrow_copy ( type-id )
4296 __has_trivial_assign ( type-id )
4297 __has_trivial_constructor ( type-id )
4298 __has_trivial_copy ( type-id )
4299 __has_trivial_destructor ( type-id )
4300 __has_virtual_destructor ( type-id )
4301 __is_abstract ( type-id )
4302 __is_base_of ( type-id , type-id )
4303 __is_class ( type-id )
4304 __is_empty ( type-id )
4305 __is_enum ( type-id )
4306 __is_final ( type-id )
4307 __is_literal_type ( type-id )
4308 __is_pod ( type-id )
4309 __is_polymorphic ( type-id )
4310 __is_std_layout ( type-id )
4311 __is_trivial ( type-id )
4312 __is_union ( type-id )
4314 Objective-C++ Extension:
4316 primary-expression:
4317 objc-expression
4319 literal:
4320 __null
4322 ADDRESS_P is true iff this expression was immediately preceded by
4323 "&" and therefore might denote a pointer-to-member. CAST_P is true
4324 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4325 true iff this expression is a template argument.
4327 Returns a representation of the expression. Upon return, *IDK
4328 indicates what kind of id-expression (if any) was present. */
4330 static tree
4331 cp_parser_primary_expression (cp_parser *parser,
4332 bool address_p,
4333 bool cast_p,
4334 bool template_arg_p,
4335 bool decltype_p,
4336 cp_id_kind *idk)
4338 cp_token *token = NULL;
4340 /* Assume the primary expression is not an id-expression. */
4341 *idk = CP_ID_KIND_NONE;
4343 /* Peek at the next token. */
4344 token = cp_lexer_peek_token (parser->lexer);
4345 switch ((int) token->type)
4347 /* literal:
4348 integer-literal
4349 character-literal
4350 floating-literal
4351 string-literal
4352 boolean-literal
4353 pointer-literal
4354 user-defined-literal */
4355 case CPP_CHAR:
4356 case CPP_CHAR16:
4357 case CPP_CHAR32:
4358 case CPP_WCHAR:
4359 case CPP_NUMBER:
4360 case CPP_PREPARSED_EXPR:
4361 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4362 return cp_parser_userdef_numeric_literal (parser);
4363 token = cp_lexer_consume_token (parser->lexer);
4364 if (TREE_CODE (token->u.value) == FIXED_CST)
4366 error_at (token->location,
4367 "fixed-point types not supported in C++");
4368 return error_mark_node;
4370 /* Floating-point literals are only allowed in an integral
4371 constant expression if they are cast to an integral or
4372 enumeration type. */
4373 if (TREE_CODE (token->u.value) == REAL_CST
4374 && parser->integral_constant_expression_p
4375 && pedantic)
4377 /* CAST_P will be set even in invalid code like "int(2.7 +
4378 ...)". Therefore, we have to check that the next token
4379 is sure to end the cast. */
4380 if (cast_p)
4382 cp_token *next_token;
4384 next_token = cp_lexer_peek_token (parser->lexer);
4385 if (/* The comma at the end of an
4386 enumerator-definition. */
4387 next_token->type != CPP_COMMA
4388 /* The curly brace at the end of an enum-specifier. */
4389 && next_token->type != CPP_CLOSE_BRACE
4390 /* The end of a statement. */
4391 && next_token->type != CPP_SEMICOLON
4392 /* The end of the cast-expression. */
4393 && next_token->type != CPP_CLOSE_PAREN
4394 /* The end of an array bound. */
4395 && next_token->type != CPP_CLOSE_SQUARE
4396 /* The closing ">" in a template-argument-list. */
4397 && (next_token->type != CPP_GREATER
4398 || parser->greater_than_is_operator_p)
4399 /* C++0x only: A ">>" treated like two ">" tokens,
4400 in a template-argument-list. */
4401 && (next_token->type != CPP_RSHIFT
4402 || (cxx_dialect == cxx98)
4403 || parser->greater_than_is_operator_p))
4404 cast_p = false;
4407 /* If we are within a cast, then the constraint that the
4408 cast is to an integral or enumeration type will be
4409 checked at that point. If we are not within a cast, then
4410 this code is invalid. */
4411 if (!cast_p)
4412 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4414 return token->u.value;
4416 case CPP_CHAR_USERDEF:
4417 case CPP_CHAR16_USERDEF:
4418 case CPP_CHAR32_USERDEF:
4419 case CPP_WCHAR_USERDEF:
4420 return cp_parser_userdef_char_literal (parser);
4422 case CPP_STRING:
4423 case CPP_STRING16:
4424 case CPP_STRING32:
4425 case CPP_WSTRING:
4426 case CPP_UTF8STRING:
4427 case CPP_STRING_USERDEF:
4428 case CPP_STRING16_USERDEF:
4429 case CPP_STRING32_USERDEF:
4430 case CPP_WSTRING_USERDEF:
4431 case CPP_UTF8STRING_USERDEF:
4432 /* ??? Should wide strings be allowed when parser->translate_strings_p
4433 is false (i.e. in attributes)? If not, we can kill the third
4434 argument to cp_parser_string_literal. */
4435 return cp_parser_string_literal (parser,
4436 parser->translate_strings_p,
4437 true);
4439 case CPP_OPEN_PAREN:
4440 /* If we see `( { ' then we are looking at the beginning of
4441 a GNU statement-expression. */
4442 if (cp_parser_allow_gnu_extensions_p (parser)
4443 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4445 /* Statement-expressions are not allowed by the standard. */
4446 pedwarn (token->location, OPT_Wpedantic,
4447 "ISO C++ forbids braced-groups within expressions");
4449 /* And they're not allowed outside of a function-body; you
4450 cannot, for example, write:
4452 int i = ({ int j = 3; j + 1; });
4454 at class or namespace scope. */
4455 if (!parser->in_function_body
4456 || parser->in_template_argument_list_p)
4458 error_at (token->location,
4459 "statement-expressions are not allowed outside "
4460 "functions nor in template-argument lists");
4461 cp_parser_skip_to_end_of_block_or_statement (parser);
4462 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4463 cp_lexer_consume_token (parser->lexer);
4464 return error_mark_node;
4466 else
4467 return cp_parser_statement_expr (parser);
4469 /* Otherwise it's a normal parenthesized expression. */
4471 tree expr;
4472 bool saved_greater_than_is_operator_p;
4474 /* Consume the `('. */
4475 cp_lexer_consume_token (parser->lexer);
4476 /* Within a parenthesized expression, a `>' token is always
4477 the greater-than operator. */
4478 saved_greater_than_is_operator_p
4479 = parser->greater_than_is_operator_p;
4480 parser->greater_than_is_operator_p = true;
4482 /* Parse the parenthesized expression. */
4483 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4484 /* Let the front end know that this expression was
4485 enclosed in parentheses. This matters in case, for
4486 example, the expression is of the form `A::B', since
4487 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4488 not. */
4489 expr = finish_parenthesized_expr (expr);
4490 /* DR 705: Wrapping an unqualified name in parentheses
4491 suppresses arg-dependent lookup. We want to pass back
4492 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4493 (c++/37862), but none of the others. */
4494 if (*idk != CP_ID_KIND_QUALIFIED)
4495 *idk = CP_ID_KIND_NONE;
4497 /* The `>' token might be the end of a template-id or
4498 template-parameter-list now. */
4499 parser->greater_than_is_operator_p
4500 = saved_greater_than_is_operator_p;
4501 /* Consume the `)'. */
4502 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4503 cp_parser_skip_to_end_of_statement (parser);
4505 return expr;
4508 case CPP_OPEN_SQUARE:
4510 if (c_dialect_objc ())
4512 /* We might have an Objective-C++ message. */
4513 cp_parser_parse_tentatively (parser);
4514 tree msg = cp_parser_objc_message_expression (parser);
4515 /* If that works out, we're done ... */
4516 if (cp_parser_parse_definitely (parser))
4517 return msg;
4518 /* ... else, fall though to see if it's a lambda. */
4520 tree lam = cp_parser_lambda_expression (parser);
4521 /* Don't warn about a failed tentative parse. */
4522 if (cp_parser_error_occurred (parser))
4523 return error_mark_node;
4524 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4525 return lam;
4528 case CPP_OBJC_STRING:
4529 if (c_dialect_objc ())
4530 /* We have an Objective-C++ string literal. */
4531 return cp_parser_objc_expression (parser);
4532 cp_parser_error (parser, "expected primary-expression");
4533 return error_mark_node;
4535 case CPP_KEYWORD:
4536 switch (token->keyword)
4538 /* These two are the boolean literals. */
4539 case RID_TRUE:
4540 cp_lexer_consume_token (parser->lexer);
4541 return boolean_true_node;
4542 case RID_FALSE:
4543 cp_lexer_consume_token (parser->lexer);
4544 return boolean_false_node;
4546 /* The `__null' literal. */
4547 case RID_NULL:
4548 cp_lexer_consume_token (parser->lexer);
4549 return null_node;
4551 /* The `nullptr' literal. */
4552 case RID_NULLPTR:
4553 cp_lexer_consume_token (parser->lexer);
4554 return nullptr_node;
4556 /* Recognize the `this' keyword. */
4557 case RID_THIS:
4558 cp_lexer_consume_token (parser->lexer);
4559 if (parser->local_variables_forbidden_p)
4561 error_at (token->location,
4562 "%<this%> may not be used in this context");
4563 return error_mark_node;
4565 /* Pointers cannot appear in constant-expressions. */
4566 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4567 return error_mark_node;
4568 return finish_this_expr ();
4570 /* The `operator' keyword can be the beginning of an
4571 id-expression. */
4572 case RID_OPERATOR:
4573 goto id_expression;
4575 case RID_FUNCTION_NAME:
4576 case RID_PRETTY_FUNCTION_NAME:
4577 case RID_C99_FUNCTION_NAME:
4579 non_integral_constant name;
4581 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4582 __func__ are the names of variables -- but they are
4583 treated specially. Therefore, they are handled here,
4584 rather than relying on the generic id-expression logic
4585 below. Grammatically, these names are id-expressions.
4587 Consume the token. */
4588 token = cp_lexer_consume_token (parser->lexer);
4590 switch (token->keyword)
4592 case RID_FUNCTION_NAME:
4593 name = NIC_FUNC_NAME;
4594 break;
4595 case RID_PRETTY_FUNCTION_NAME:
4596 name = NIC_PRETTY_FUNC;
4597 break;
4598 case RID_C99_FUNCTION_NAME:
4599 name = NIC_C99_FUNC;
4600 break;
4601 default:
4602 gcc_unreachable ();
4605 if (cp_parser_non_integral_constant_expression (parser, name))
4606 return error_mark_node;
4608 /* Look up the name. */
4609 return finish_fname (token->u.value);
4612 case RID_VA_ARG:
4614 tree expression;
4615 tree type;
4616 source_location type_location;
4618 /* The `__builtin_va_arg' construct is used to handle
4619 `va_arg'. Consume the `__builtin_va_arg' token. */
4620 cp_lexer_consume_token (parser->lexer);
4621 /* Look for the opening `('. */
4622 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4623 /* Now, parse the assignment-expression. */
4624 expression = cp_parser_assignment_expression (parser);
4625 /* Look for the `,'. */
4626 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4627 type_location = cp_lexer_peek_token (parser->lexer)->location;
4628 /* Parse the type-id. */
4629 type = cp_parser_type_id (parser);
4630 /* Look for the closing `)'. */
4631 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4632 /* Using `va_arg' in a constant-expression is not
4633 allowed. */
4634 if (cp_parser_non_integral_constant_expression (parser,
4635 NIC_VA_ARG))
4636 return error_mark_node;
4637 return build_x_va_arg (type_location, expression, type);
4640 case RID_OFFSETOF:
4641 return cp_parser_builtin_offsetof (parser);
4643 case RID_HAS_NOTHROW_ASSIGN:
4644 case RID_HAS_NOTHROW_CONSTRUCTOR:
4645 case RID_HAS_NOTHROW_COPY:
4646 case RID_HAS_TRIVIAL_ASSIGN:
4647 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4648 case RID_HAS_TRIVIAL_COPY:
4649 case RID_HAS_TRIVIAL_DESTRUCTOR:
4650 case RID_HAS_VIRTUAL_DESTRUCTOR:
4651 case RID_IS_ABSTRACT:
4652 case RID_IS_BASE_OF:
4653 case RID_IS_CLASS:
4654 case RID_IS_EMPTY:
4655 case RID_IS_ENUM:
4656 case RID_IS_FINAL:
4657 case RID_IS_LITERAL_TYPE:
4658 case RID_IS_POD:
4659 case RID_IS_POLYMORPHIC:
4660 case RID_IS_SAME_AS:
4661 case RID_IS_STD_LAYOUT:
4662 case RID_IS_TRIVIAL:
4663 case RID_IS_TRIVIALLY_ASSIGNABLE:
4664 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4665 case RID_IS_TRIVIALLY_COPYABLE:
4666 case RID_IS_UNION:
4667 return cp_parser_trait_expr (parser, token->keyword);
4669 // C++ concepts
4670 case RID_REQUIRES:
4671 return cp_parser_requires_expression (parser);
4673 /* Objective-C++ expressions. */
4674 case RID_AT_ENCODE:
4675 case RID_AT_PROTOCOL:
4676 case RID_AT_SELECTOR:
4677 return cp_parser_objc_expression (parser);
4679 case RID_TEMPLATE:
4680 if (parser->in_function_body
4681 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4682 == CPP_LESS))
4684 error_at (token->location,
4685 "a template declaration cannot appear at block scope");
4686 cp_parser_skip_to_end_of_block_or_statement (parser);
4687 return error_mark_node;
4689 default:
4690 cp_parser_error (parser, "expected primary-expression");
4691 return error_mark_node;
4694 /* An id-expression can start with either an identifier, a
4695 `::' as the beginning of a qualified-id, or the "operator"
4696 keyword. */
4697 case CPP_NAME:
4698 case CPP_SCOPE:
4699 case CPP_TEMPLATE_ID:
4700 case CPP_NESTED_NAME_SPECIFIER:
4702 tree id_expression;
4703 tree decl;
4704 const char *error_msg;
4705 bool template_p;
4706 bool done;
4707 cp_token *id_expr_token;
4709 id_expression:
4710 /* Parse the id-expression. */
4711 id_expression
4712 = cp_parser_id_expression (parser,
4713 /*template_keyword_p=*/false,
4714 /*check_dependency_p=*/true,
4715 &template_p,
4716 /*declarator_p=*/false,
4717 /*optional_p=*/false);
4718 if (id_expression == error_mark_node)
4719 return error_mark_node;
4720 id_expr_token = token;
4721 token = cp_lexer_peek_token (parser->lexer);
4722 done = (token->type != CPP_OPEN_SQUARE
4723 && token->type != CPP_OPEN_PAREN
4724 && token->type != CPP_DOT
4725 && token->type != CPP_DEREF
4726 && token->type != CPP_PLUS_PLUS
4727 && token->type != CPP_MINUS_MINUS);
4728 /* If we have a template-id, then no further lookup is
4729 required. If the template-id was for a template-class, we
4730 will sometimes have a TYPE_DECL at this point. */
4731 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4732 || TREE_CODE (id_expression) == TYPE_DECL)
4733 decl = id_expression;
4734 /* Look up the name. */
4735 else
4737 tree ambiguous_decls;
4739 /* If we already know that this lookup is ambiguous, then
4740 we've already issued an error message; there's no reason
4741 to check again. */
4742 if (id_expr_token->type == CPP_NAME
4743 && id_expr_token->error_reported)
4745 cp_parser_simulate_error (parser);
4746 return error_mark_node;
4749 decl = cp_parser_lookup_name (parser, id_expression,
4750 none_type,
4751 template_p,
4752 /*is_namespace=*/false,
4753 /*check_dependency=*/true,
4754 &ambiguous_decls,
4755 id_expr_token->location);
4756 /* If the lookup was ambiguous, an error will already have
4757 been issued. */
4758 if (ambiguous_decls)
4759 return error_mark_node;
4761 /* In Objective-C++, we may have an Objective-C 2.0
4762 dot-syntax for classes here. */
4763 if (c_dialect_objc ()
4764 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4765 && TREE_CODE (decl) == TYPE_DECL
4766 && objc_is_class_name (decl))
4768 tree component;
4769 cp_lexer_consume_token (parser->lexer);
4770 component = cp_parser_identifier (parser);
4771 if (component == error_mark_node)
4772 return error_mark_node;
4774 return objc_build_class_component_ref (id_expression, component);
4777 /* In Objective-C++, an instance variable (ivar) may be preferred
4778 to whatever cp_parser_lookup_name() found. */
4779 decl = objc_lookup_ivar (decl, id_expression);
4781 /* If name lookup gives us a SCOPE_REF, then the
4782 qualifying scope was dependent. */
4783 if (TREE_CODE (decl) == SCOPE_REF)
4785 /* At this point, we do not know if DECL is a valid
4786 integral constant expression. We assume that it is
4787 in fact such an expression, so that code like:
4789 template <int N> struct A {
4790 int a[B<N>::i];
4793 is accepted. At template-instantiation time, we
4794 will check that B<N>::i is actually a constant. */
4795 return decl;
4797 /* Check to see if DECL is a local variable in a context
4798 where that is forbidden. */
4799 if (parser->local_variables_forbidden_p
4800 && local_variable_p (decl))
4802 /* It might be that we only found DECL because we are
4803 trying to be generous with pre-ISO scoping rules.
4804 For example, consider:
4806 int i;
4807 void g() {
4808 for (int i = 0; i < 10; ++i) {}
4809 extern void f(int j = i);
4812 Here, name look up will originally find the out
4813 of scope `i'. We need to issue a warning message,
4814 but then use the global `i'. */
4815 decl = check_for_out_of_scope_variable (decl);
4816 if (local_variable_p (decl))
4818 error_at (id_expr_token->location,
4819 "local variable %qD may not appear in this context",
4820 decl);
4821 return error_mark_node;
4826 decl = (finish_id_expression
4827 (id_expression, decl, parser->scope,
4828 idk,
4829 parser->integral_constant_expression_p,
4830 parser->allow_non_integral_constant_expression_p,
4831 &parser->non_integral_constant_expression_p,
4832 template_p, done, address_p,
4833 template_arg_p,
4834 &error_msg,
4835 id_expr_token->location));
4836 if (error_msg)
4837 cp_parser_error (parser, error_msg);
4838 return decl;
4841 /* Anything else is an error. */
4842 default:
4843 cp_parser_error (parser, "expected primary-expression");
4844 return error_mark_node;
4848 static inline tree
4849 cp_parser_primary_expression (cp_parser *parser,
4850 bool address_p,
4851 bool cast_p,
4852 bool template_arg_p,
4853 cp_id_kind *idk)
4855 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4856 /*decltype*/false, idk);
4859 /* Parse an id-expression.
4861 id-expression:
4862 unqualified-id
4863 qualified-id
4865 qualified-id:
4866 :: [opt] nested-name-specifier template [opt] unqualified-id
4867 :: identifier
4868 :: operator-function-id
4869 :: template-id
4871 Return a representation of the unqualified portion of the
4872 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4873 a `::' or nested-name-specifier.
4875 Often, if the id-expression was a qualified-id, the caller will
4876 want to make a SCOPE_REF to represent the qualified-id. This
4877 function does not do this in order to avoid wastefully creating
4878 SCOPE_REFs when they are not required.
4880 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4881 `template' keyword.
4883 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4884 uninstantiated templates.
4886 If *TEMPLATE_P is non-NULL, it is set to true iff the
4887 `template' keyword is used to explicitly indicate that the entity
4888 named is a template.
4890 If DECLARATOR_P is true, the id-expression is appearing as part of
4891 a declarator, rather than as part of an expression. */
4893 static tree
4894 cp_parser_id_expression (cp_parser *parser,
4895 bool template_keyword_p,
4896 bool check_dependency_p,
4897 bool *template_p,
4898 bool declarator_p,
4899 bool optional_p)
4901 bool global_scope_p;
4902 bool nested_name_specifier_p;
4904 /* Assume the `template' keyword was not used. */
4905 if (template_p)
4906 *template_p = template_keyword_p;
4908 /* Look for the optional `::' operator. */
4909 global_scope_p
4910 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4911 != NULL_TREE);
4912 /* Look for the optional nested-name-specifier. */
4913 nested_name_specifier_p
4914 = (cp_parser_nested_name_specifier_opt (parser,
4915 /*typename_keyword_p=*/false,
4916 check_dependency_p,
4917 /*type_p=*/false,
4918 declarator_p)
4919 != NULL_TREE);
4920 /* If there is a nested-name-specifier, then we are looking at
4921 the first qualified-id production. */
4922 if (nested_name_specifier_p)
4924 tree saved_scope;
4925 tree saved_object_scope;
4926 tree saved_qualifying_scope;
4927 tree unqualified_id;
4928 bool is_template;
4930 /* See if the next token is the `template' keyword. */
4931 if (!template_p)
4932 template_p = &is_template;
4933 *template_p = cp_parser_optional_template_keyword (parser);
4934 /* Name lookup we do during the processing of the
4935 unqualified-id might obliterate SCOPE. */
4936 saved_scope = parser->scope;
4937 saved_object_scope = parser->object_scope;
4938 saved_qualifying_scope = parser->qualifying_scope;
4939 /* Process the final unqualified-id. */
4940 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4941 check_dependency_p,
4942 declarator_p,
4943 /*optional_p=*/false);
4944 /* Restore the SAVED_SCOPE for our caller. */
4945 parser->scope = saved_scope;
4946 parser->object_scope = saved_object_scope;
4947 parser->qualifying_scope = saved_qualifying_scope;
4949 return unqualified_id;
4951 /* Otherwise, if we are in global scope, then we are looking at one
4952 of the other qualified-id productions. */
4953 else if (global_scope_p)
4955 cp_token *token;
4956 tree id;
4958 /* Peek at the next token. */
4959 token = cp_lexer_peek_token (parser->lexer);
4961 /* If it's an identifier, and the next token is not a "<", then
4962 we can avoid the template-id case. This is an optimization
4963 for this common case. */
4964 if (token->type == CPP_NAME
4965 && !cp_parser_nth_token_starts_template_argument_list_p
4966 (parser, 2))
4967 return cp_parser_identifier (parser);
4969 cp_parser_parse_tentatively (parser);
4970 /* Try a template-id. */
4971 id = cp_parser_template_id (parser,
4972 /*template_keyword_p=*/false,
4973 /*check_dependency_p=*/true,
4974 none_type,
4975 declarator_p);
4976 /* If that worked, we're done. */
4977 if (cp_parser_parse_definitely (parser))
4978 return id;
4980 /* Peek at the next token. (Changes in the token buffer may
4981 have invalidated the pointer obtained above.) */
4982 token = cp_lexer_peek_token (parser->lexer);
4984 switch (token->type)
4986 case CPP_NAME:
4987 return cp_parser_identifier (parser);
4989 case CPP_KEYWORD:
4990 if (token->keyword == RID_OPERATOR)
4991 return cp_parser_operator_function_id (parser);
4992 /* Fall through. */
4994 default:
4995 cp_parser_error (parser, "expected id-expression");
4996 return error_mark_node;
4999 else
5000 return cp_parser_unqualified_id (parser, template_keyword_p,
5001 /*check_dependency_p=*/true,
5002 declarator_p,
5003 optional_p);
5006 /* Parse an unqualified-id.
5008 unqualified-id:
5009 identifier
5010 operator-function-id
5011 conversion-function-id
5012 ~ class-name
5013 template-id
5015 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5016 keyword, in a construct like `A::template ...'.
5018 Returns a representation of unqualified-id. For the `identifier'
5019 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5020 production a BIT_NOT_EXPR is returned; the operand of the
5021 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5022 other productions, see the documentation accompanying the
5023 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5024 names are looked up in uninstantiated templates. If DECLARATOR_P
5025 is true, the unqualified-id is appearing as part of a declarator,
5026 rather than as part of an expression. */
5028 static tree
5029 cp_parser_unqualified_id (cp_parser* parser,
5030 bool template_keyword_p,
5031 bool check_dependency_p,
5032 bool declarator_p,
5033 bool optional_p)
5035 cp_token *token;
5037 /* Peek at the next token. */
5038 token = cp_lexer_peek_token (parser->lexer);
5040 switch ((int) token->type)
5042 case CPP_NAME:
5044 tree id;
5046 /* We don't know yet whether or not this will be a
5047 template-id. */
5048 cp_parser_parse_tentatively (parser);
5049 /* Try a template-id. */
5050 id = cp_parser_template_id (parser, template_keyword_p,
5051 check_dependency_p,
5052 none_type,
5053 declarator_p);
5054 /* If it worked, we're done. */
5055 if (cp_parser_parse_definitely (parser))
5056 return id;
5057 /* Otherwise, it's an ordinary identifier. */
5058 return cp_parser_identifier (parser);
5061 case CPP_TEMPLATE_ID:
5062 return cp_parser_template_id (parser, template_keyword_p,
5063 check_dependency_p,
5064 none_type,
5065 declarator_p);
5067 case CPP_COMPL:
5069 tree type_decl;
5070 tree qualifying_scope;
5071 tree object_scope;
5072 tree scope;
5073 bool done;
5075 /* Consume the `~' token. */
5076 cp_lexer_consume_token (parser->lexer);
5077 /* Parse the class-name. The standard, as written, seems to
5078 say that:
5080 template <typename T> struct S { ~S (); };
5081 template <typename T> S<T>::~S() {}
5083 is invalid, since `~' must be followed by a class-name, but
5084 `S<T>' is dependent, and so not known to be a class.
5085 That's not right; we need to look in uninstantiated
5086 templates. A further complication arises from:
5088 template <typename T> void f(T t) {
5089 t.T::~T();
5092 Here, it is not possible to look up `T' in the scope of `T'
5093 itself. We must look in both the current scope, and the
5094 scope of the containing complete expression.
5096 Yet another issue is:
5098 struct S {
5099 int S;
5100 ~S();
5103 S::~S() {}
5105 The standard does not seem to say that the `S' in `~S'
5106 should refer to the type `S' and not the data member
5107 `S::S'. */
5109 /* DR 244 says that we look up the name after the "~" in the
5110 same scope as we looked up the qualifying name. That idea
5111 isn't fully worked out; it's more complicated than that. */
5112 scope = parser->scope;
5113 object_scope = parser->object_scope;
5114 qualifying_scope = parser->qualifying_scope;
5116 /* Check for invalid scopes. */
5117 if (scope == error_mark_node)
5119 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5120 cp_lexer_consume_token (parser->lexer);
5121 return error_mark_node;
5123 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5125 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5126 error_at (token->location,
5127 "scope %qT before %<~%> is not a class-name",
5128 scope);
5129 cp_parser_simulate_error (parser);
5130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5131 cp_lexer_consume_token (parser->lexer);
5132 return error_mark_node;
5134 gcc_assert (!scope || TYPE_P (scope));
5136 /* If the name is of the form "X::~X" it's OK even if X is a
5137 typedef. */
5138 token = cp_lexer_peek_token (parser->lexer);
5139 if (scope
5140 && token->type == CPP_NAME
5141 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5142 != CPP_LESS)
5143 && (token->u.value == TYPE_IDENTIFIER (scope)
5144 || (CLASS_TYPE_P (scope)
5145 && constructor_name_p (token->u.value, scope))))
5147 cp_lexer_consume_token (parser->lexer);
5148 return build_nt (BIT_NOT_EXPR, scope);
5151 /* ~auto means the destructor of whatever the object is. */
5152 if (cp_parser_is_keyword (token, RID_AUTO))
5154 if (cxx_dialect < cxx14)
5155 pedwarn (input_location, 0,
5156 "%<~auto%> only available with "
5157 "-std=c++14 or -std=gnu++14");
5158 cp_lexer_consume_token (parser->lexer);
5159 return build_nt (BIT_NOT_EXPR, make_auto ());
5162 /* If there was an explicit qualification (S::~T), first look
5163 in the scope given by the qualification (i.e., S).
5165 Note: in the calls to cp_parser_class_name below we pass
5166 typename_type so that lookup finds the injected-class-name
5167 rather than the constructor. */
5168 done = false;
5169 type_decl = NULL_TREE;
5170 if (scope)
5172 cp_parser_parse_tentatively (parser);
5173 type_decl = cp_parser_class_name (parser,
5174 /*typename_keyword_p=*/false,
5175 /*template_keyword_p=*/false,
5176 typename_type,
5177 /*check_dependency=*/false,
5178 /*class_head_p=*/false,
5179 declarator_p);
5180 if (cp_parser_parse_definitely (parser))
5181 done = true;
5183 /* In "N::S::~S", look in "N" as well. */
5184 if (!done && scope && qualifying_scope)
5186 cp_parser_parse_tentatively (parser);
5187 parser->scope = qualifying_scope;
5188 parser->object_scope = NULL_TREE;
5189 parser->qualifying_scope = NULL_TREE;
5190 type_decl
5191 = cp_parser_class_name (parser,
5192 /*typename_keyword_p=*/false,
5193 /*template_keyword_p=*/false,
5194 typename_type,
5195 /*check_dependency=*/false,
5196 /*class_head_p=*/false,
5197 declarator_p);
5198 if (cp_parser_parse_definitely (parser))
5199 done = true;
5201 /* In "p->S::~T", look in the scope given by "*p" as well. */
5202 else if (!done && object_scope)
5204 cp_parser_parse_tentatively (parser);
5205 parser->scope = object_scope;
5206 parser->object_scope = NULL_TREE;
5207 parser->qualifying_scope = NULL_TREE;
5208 type_decl
5209 = cp_parser_class_name (parser,
5210 /*typename_keyword_p=*/false,
5211 /*template_keyword_p=*/false,
5212 typename_type,
5213 /*check_dependency=*/false,
5214 /*class_head_p=*/false,
5215 declarator_p);
5216 if (cp_parser_parse_definitely (parser))
5217 done = true;
5219 /* Look in the surrounding context. */
5220 if (!done)
5222 parser->scope = NULL_TREE;
5223 parser->object_scope = NULL_TREE;
5224 parser->qualifying_scope = NULL_TREE;
5225 if (processing_template_decl)
5226 cp_parser_parse_tentatively (parser);
5227 type_decl
5228 = cp_parser_class_name (parser,
5229 /*typename_keyword_p=*/false,
5230 /*template_keyword_p=*/false,
5231 typename_type,
5232 /*check_dependency=*/false,
5233 /*class_head_p=*/false,
5234 declarator_p);
5235 if (processing_template_decl
5236 && ! cp_parser_parse_definitely (parser))
5238 /* We couldn't find a type with this name, so just accept
5239 it and check for a match at instantiation time. */
5240 type_decl = cp_parser_identifier (parser);
5241 if (type_decl != error_mark_node)
5242 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5243 return type_decl;
5246 /* If an error occurred, assume that the name of the
5247 destructor is the same as the name of the qualifying
5248 class. That allows us to keep parsing after running
5249 into ill-formed destructor names. */
5250 if (type_decl == error_mark_node && scope)
5251 return build_nt (BIT_NOT_EXPR, scope);
5252 else if (type_decl == error_mark_node)
5253 return error_mark_node;
5255 /* Check that destructor name and scope match. */
5256 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5258 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5259 error_at (token->location,
5260 "declaration of %<~%T%> as member of %qT",
5261 type_decl, scope);
5262 cp_parser_simulate_error (parser);
5263 return error_mark_node;
5266 /* [class.dtor]
5268 A typedef-name that names a class shall not be used as the
5269 identifier in the declarator for a destructor declaration. */
5270 if (declarator_p
5271 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5272 && !DECL_SELF_REFERENCE_P (type_decl)
5273 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5274 error_at (token->location,
5275 "typedef-name %qD used as destructor declarator",
5276 type_decl);
5278 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5281 case CPP_KEYWORD:
5282 if (token->keyword == RID_OPERATOR)
5284 tree id;
5286 /* This could be a template-id, so we try that first. */
5287 cp_parser_parse_tentatively (parser);
5288 /* Try a template-id. */
5289 id = cp_parser_template_id (parser, template_keyword_p,
5290 /*check_dependency_p=*/true,
5291 none_type,
5292 declarator_p);
5293 /* If that worked, we're done. */
5294 if (cp_parser_parse_definitely (parser))
5295 return id;
5296 /* We still don't know whether we're looking at an
5297 operator-function-id or a conversion-function-id. */
5298 cp_parser_parse_tentatively (parser);
5299 /* Try an operator-function-id. */
5300 id = cp_parser_operator_function_id (parser);
5301 /* If that didn't work, try a conversion-function-id. */
5302 if (!cp_parser_parse_definitely (parser))
5303 id = cp_parser_conversion_function_id (parser);
5304 else if (UDLIT_OPER_P (id))
5306 /* 17.6.3.3.5 */
5307 const char *name = UDLIT_OP_SUFFIX (id);
5308 if (name[0] != '_' && !in_system_header_at (input_location)
5309 && declarator_p)
5310 warning (0, "literal operator suffixes not preceded by %<_%>"
5311 " are reserved for future standardization");
5314 return id;
5316 /* Fall through. */
5318 default:
5319 if (optional_p)
5320 return NULL_TREE;
5321 cp_parser_error (parser, "expected unqualified-id");
5322 return error_mark_node;
5326 /* Parse an (optional) nested-name-specifier.
5328 nested-name-specifier: [C++98]
5329 class-or-namespace-name :: nested-name-specifier [opt]
5330 class-or-namespace-name :: template nested-name-specifier [opt]
5332 nested-name-specifier: [C++0x]
5333 type-name ::
5334 namespace-name ::
5335 nested-name-specifier identifier ::
5336 nested-name-specifier template [opt] simple-template-id ::
5338 PARSER->SCOPE should be set appropriately before this function is
5339 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5340 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5341 in name lookups.
5343 Sets PARSER->SCOPE to the class (TYPE) or namespace
5344 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5345 it unchanged if there is no nested-name-specifier. Returns the new
5346 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5348 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5349 part of a declaration and/or decl-specifier. */
5351 static tree
5352 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5353 bool typename_keyword_p,
5354 bool check_dependency_p,
5355 bool type_p,
5356 bool is_declaration)
5358 bool success = false;
5359 cp_token_position start = 0;
5360 cp_token *token;
5362 /* Remember where the nested-name-specifier starts. */
5363 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5365 start = cp_lexer_token_position (parser->lexer, false);
5366 push_deferring_access_checks (dk_deferred);
5369 while (true)
5371 tree new_scope;
5372 tree old_scope;
5373 tree saved_qualifying_scope;
5374 bool template_keyword_p;
5376 /* Spot cases that cannot be the beginning of a
5377 nested-name-specifier. */
5378 token = cp_lexer_peek_token (parser->lexer);
5380 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5381 the already parsed nested-name-specifier. */
5382 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5384 /* Grab the nested-name-specifier and continue the loop. */
5385 cp_parser_pre_parsed_nested_name_specifier (parser);
5386 /* If we originally encountered this nested-name-specifier
5387 with IS_DECLARATION set to false, we will not have
5388 resolved TYPENAME_TYPEs, so we must do so here. */
5389 if (is_declaration
5390 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5392 new_scope = resolve_typename_type (parser->scope,
5393 /*only_current_p=*/false);
5394 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5395 parser->scope = new_scope;
5397 success = true;
5398 continue;
5401 /* Spot cases that cannot be the beginning of a
5402 nested-name-specifier. On the second and subsequent times
5403 through the loop, we look for the `template' keyword. */
5404 if (success && token->keyword == RID_TEMPLATE)
5406 /* A template-id can start a nested-name-specifier. */
5407 else if (token->type == CPP_TEMPLATE_ID)
5409 /* DR 743: decltype can be used in a nested-name-specifier. */
5410 else if (token_is_decltype (token))
5412 else
5414 /* If the next token is not an identifier, then it is
5415 definitely not a type-name or namespace-name. */
5416 if (token->type != CPP_NAME)
5417 break;
5418 /* If the following token is neither a `<' (to begin a
5419 template-id), nor a `::', then we are not looking at a
5420 nested-name-specifier. */
5421 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5423 if (token->type == CPP_COLON
5424 && parser->colon_corrects_to_scope_p
5425 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5427 error_at (token->location,
5428 "found %<:%> in nested-name-specifier, expected %<::%>");
5429 token->type = CPP_SCOPE;
5432 if (token->type != CPP_SCOPE
5433 && !cp_parser_nth_token_starts_template_argument_list_p
5434 (parser, 2))
5435 break;
5438 /* The nested-name-specifier is optional, so we parse
5439 tentatively. */
5440 cp_parser_parse_tentatively (parser);
5442 /* Look for the optional `template' keyword, if this isn't the
5443 first time through the loop. */
5444 if (success)
5445 template_keyword_p = cp_parser_optional_template_keyword (parser);
5446 else
5447 template_keyword_p = false;
5449 /* Save the old scope since the name lookup we are about to do
5450 might destroy it. */
5451 old_scope = parser->scope;
5452 saved_qualifying_scope = parser->qualifying_scope;
5453 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5454 look up names in "X<T>::I" in order to determine that "Y" is
5455 a template. So, if we have a typename at this point, we make
5456 an effort to look through it. */
5457 if (is_declaration
5458 && !typename_keyword_p
5459 && parser->scope
5460 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5461 parser->scope = resolve_typename_type (parser->scope,
5462 /*only_current_p=*/false);
5463 /* Parse the qualifying entity. */
5464 new_scope
5465 = cp_parser_qualifying_entity (parser,
5466 typename_keyword_p,
5467 template_keyword_p,
5468 check_dependency_p,
5469 type_p,
5470 is_declaration);
5471 /* Look for the `::' token. */
5472 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5474 /* If we found what we wanted, we keep going; otherwise, we're
5475 done. */
5476 if (!cp_parser_parse_definitely (parser))
5478 bool error_p = false;
5480 /* Restore the OLD_SCOPE since it was valid before the
5481 failed attempt at finding the last
5482 class-or-namespace-name. */
5483 parser->scope = old_scope;
5484 parser->qualifying_scope = saved_qualifying_scope;
5486 /* If the next token is a decltype, and the one after that is a
5487 `::', then the decltype has failed to resolve to a class or
5488 enumeration type. Give this error even when parsing
5489 tentatively since it can't possibly be valid--and we're going
5490 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5491 won't get another chance.*/
5492 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5493 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5494 == CPP_SCOPE))
5496 token = cp_lexer_consume_token (parser->lexer);
5497 error_at (token->location, "decltype evaluates to %qT, "
5498 "which is not a class or enumeration type",
5499 token->u.value);
5500 parser->scope = error_mark_node;
5501 error_p = true;
5502 /* As below. */
5503 success = true;
5504 cp_lexer_consume_token (parser->lexer);
5507 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5508 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5510 /* If we have a non-type template-id followed by ::, it can't
5511 possibly be valid. */
5512 token = cp_lexer_peek_token (parser->lexer);
5513 tree tid = token->u.tree_check_value->value;
5514 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5515 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5517 tree tmpl = NULL_TREE;
5518 if (is_overloaded_fn (tid))
5520 tree fns = get_fns (tid);
5521 if (!OVL_CHAIN (fns))
5522 tmpl = OVL_CURRENT (fns);
5523 error_at (token->location, "function template-id %qD "
5524 "in nested-name-specifier", tid);
5526 else
5528 /* Variable template. */
5529 tmpl = TREE_OPERAND (tid, 0);
5530 gcc_assert (variable_template_p (tmpl));
5531 error_at (token->location, "variable template-id %qD "
5532 "in nested-name-specifier", tid);
5534 if (tmpl)
5535 inform (DECL_SOURCE_LOCATION (tmpl),
5536 "%qD declared here", tmpl);
5538 parser->scope = error_mark_node;
5539 error_p = true;
5540 /* As below. */
5541 success = true;
5542 cp_lexer_consume_token (parser->lexer);
5543 cp_lexer_consume_token (parser->lexer);
5547 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5548 break;
5549 /* If the next token is an identifier, and the one after
5550 that is a `::', then any valid interpretation would have
5551 found a class-or-namespace-name. */
5552 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5553 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5554 == CPP_SCOPE)
5555 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5556 != CPP_COMPL))
5558 token = cp_lexer_consume_token (parser->lexer);
5559 if (!error_p)
5561 if (!token->error_reported)
5563 tree decl;
5564 tree ambiguous_decls;
5566 decl = cp_parser_lookup_name (parser, token->u.value,
5567 none_type,
5568 /*is_template=*/false,
5569 /*is_namespace=*/false,
5570 /*check_dependency=*/true,
5571 &ambiguous_decls,
5572 token->location);
5573 if (TREE_CODE (decl) == TEMPLATE_DECL)
5574 error_at (token->location,
5575 "%qD used without template parameters",
5576 decl);
5577 else if (ambiguous_decls)
5579 // cp_parser_lookup_name has the same diagnostic,
5580 // thus make sure to emit it at most once.
5581 if (cp_parser_uncommitted_to_tentative_parse_p
5582 (parser))
5584 error_at (token->location,
5585 "reference to %qD is ambiguous",
5586 token->u.value);
5587 print_candidates (ambiguous_decls);
5589 decl = error_mark_node;
5591 else
5593 if (cxx_dialect != cxx98)
5594 cp_parser_name_lookup_error
5595 (parser, token->u.value, decl, NLE_NOT_CXX98,
5596 token->location);
5597 else
5598 cp_parser_name_lookup_error
5599 (parser, token->u.value, decl, NLE_CXX98,
5600 token->location);
5603 parser->scope = error_mark_node;
5604 error_p = true;
5605 /* Treat this as a successful nested-name-specifier
5606 due to:
5608 [basic.lookup.qual]
5610 If the name found is not a class-name (clause
5611 _class_) or namespace-name (_namespace.def_), the
5612 program is ill-formed. */
5613 success = true;
5615 cp_lexer_consume_token (parser->lexer);
5617 break;
5619 /* We've found one valid nested-name-specifier. */
5620 success = true;
5621 /* Name lookup always gives us a DECL. */
5622 if (TREE_CODE (new_scope) == TYPE_DECL)
5623 new_scope = TREE_TYPE (new_scope);
5624 /* Uses of "template" must be followed by actual templates. */
5625 if (template_keyword_p
5626 && !(CLASS_TYPE_P (new_scope)
5627 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5628 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5629 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5630 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5631 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5632 == TEMPLATE_ID_EXPR)))
5633 permerror (input_location, TYPE_P (new_scope)
5634 ? G_("%qT is not a template")
5635 : G_("%qD is not a template"),
5636 new_scope);
5637 /* If it is a class scope, try to complete it; we are about to
5638 be looking up names inside the class. */
5639 if (TYPE_P (new_scope)
5640 /* Since checking types for dependency can be expensive,
5641 avoid doing it if the type is already complete. */
5642 && !COMPLETE_TYPE_P (new_scope)
5643 /* Do not try to complete dependent types. */
5644 && !dependent_type_p (new_scope))
5646 new_scope = complete_type (new_scope);
5647 /* If it is a typedef to current class, use the current
5648 class instead, as the typedef won't have any names inside
5649 it yet. */
5650 if (!COMPLETE_TYPE_P (new_scope)
5651 && currently_open_class (new_scope))
5652 new_scope = TYPE_MAIN_VARIANT (new_scope);
5654 /* Make sure we look in the right scope the next time through
5655 the loop. */
5656 parser->scope = new_scope;
5659 /* If parsing tentatively, replace the sequence of tokens that makes
5660 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5661 token. That way, should we re-parse the token stream, we will
5662 not have to repeat the effort required to do the parse, nor will
5663 we issue duplicate error messages. */
5664 if (success && start)
5666 cp_token *token;
5668 token = cp_lexer_token_at (parser->lexer, start);
5669 /* Reset the contents of the START token. */
5670 token->type = CPP_NESTED_NAME_SPECIFIER;
5671 /* Retrieve any deferred checks. Do not pop this access checks yet
5672 so the memory will not be reclaimed during token replacing below. */
5673 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5674 token->u.tree_check_value->value = parser->scope;
5675 token->u.tree_check_value->checks = get_deferred_access_checks ();
5676 token->u.tree_check_value->qualifying_scope =
5677 parser->qualifying_scope;
5678 token->keyword = RID_MAX;
5680 /* Purge all subsequent tokens. */
5681 cp_lexer_purge_tokens_after (parser->lexer, start);
5684 if (start)
5685 pop_to_parent_deferring_access_checks ();
5687 return success ? parser->scope : NULL_TREE;
5690 /* Parse a nested-name-specifier. See
5691 cp_parser_nested_name_specifier_opt for details. This function
5692 behaves identically, except that it will an issue an error if no
5693 nested-name-specifier is present. */
5695 static tree
5696 cp_parser_nested_name_specifier (cp_parser *parser,
5697 bool typename_keyword_p,
5698 bool check_dependency_p,
5699 bool type_p,
5700 bool is_declaration)
5702 tree scope;
5704 /* Look for the nested-name-specifier. */
5705 scope = cp_parser_nested_name_specifier_opt (parser,
5706 typename_keyword_p,
5707 check_dependency_p,
5708 type_p,
5709 is_declaration);
5710 /* If it was not present, issue an error message. */
5711 if (!scope)
5713 cp_parser_error (parser, "expected nested-name-specifier");
5714 parser->scope = NULL_TREE;
5717 return scope;
5720 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5721 this is either a class-name or a namespace-name (which corresponds
5722 to the class-or-namespace-name production in the grammar). For
5723 C++0x, it can also be a type-name that refers to an enumeration
5724 type or a simple-template-id.
5726 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5727 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5728 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5729 TYPE_P is TRUE iff the next name should be taken as a class-name,
5730 even the same name is declared to be another entity in the same
5731 scope.
5733 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5734 specified by the class-or-namespace-name. If neither is found the
5735 ERROR_MARK_NODE is returned. */
5737 static tree
5738 cp_parser_qualifying_entity (cp_parser *parser,
5739 bool typename_keyword_p,
5740 bool template_keyword_p,
5741 bool check_dependency_p,
5742 bool type_p,
5743 bool is_declaration)
5745 tree saved_scope;
5746 tree saved_qualifying_scope;
5747 tree saved_object_scope;
5748 tree scope;
5749 bool only_class_p;
5750 bool successful_parse_p;
5752 /* DR 743: decltype can appear in a nested-name-specifier. */
5753 if (cp_lexer_next_token_is_decltype (parser->lexer))
5755 scope = cp_parser_decltype (parser);
5756 if (TREE_CODE (scope) != ENUMERAL_TYPE
5757 && !MAYBE_CLASS_TYPE_P (scope))
5759 cp_parser_simulate_error (parser);
5760 return error_mark_node;
5762 if (TYPE_NAME (scope))
5763 scope = TYPE_NAME (scope);
5764 return scope;
5767 /* Before we try to parse the class-name, we must save away the
5768 current PARSER->SCOPE since cp_parser_class_name will destroy
5769 it. */
5770 saved_scope = parser->scope;
5771 saved_qualifying_scope = parser->qualifying_scope;
5772 saved_object_scope = parser->object_scope;
5773 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5774 there is no need to look for a namespace-name. */
5775 only_class_p = template_keyword_p
5776 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5777 if (!only_class_p)
5778 cp_parser_parse_tentatively (parser);
5779 scope = cp_parser_class_name (parser,
5780 typename_keyword_p,
5781 template_keyword_p,
5782 type_p ? class_type : none_type,
5783 check_dependency_p,
5784 /*class_head_p=*/false,
5785 is_declaration);
5786 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5787 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5788 if (!only_class_p
5789 && cxx_dialect != cxx98
5790 && !successful_parse_p)
5792 /* Restore the saved scope. */
5793 parser->scope = saved_scope;
5794 parser->qualifying_scope = saved_qualifying_scope;
5795 parser->object_scope = saved_object_scope;
5797 /* Parse tentatively. */
5798 cp_parser_parse_tentatively (parser);
5800 /* Parse a type-name */
5801 scope = cp_parser_type_name (parser);
5803 /* "If the name found does not designate a namespace or a class,
5804 enumeration, or dependent type, the program is ill-formed."
5806 We cover classes and dependent types above and namespaces below,
5807 so this code is only looking for enums. */
5808 if (!scope || TREE_CODE (scope) != TYPE_DECL
5809 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5810 cp_parser_simulate_error (parser);
5812 successful_parse_p = cp_parser_parse_definitely (parser);
5814 /* If that didn't work, try for a namespace-name. */
5815 if (!only_class_p && !successful_parse_p)
5817 /* Restore the saved scope. */
5818 parser->scope = saved_scope;
5819 parser->qualifying_scope = saved_qualifying_scope;
5820 parser->object_scope = saved_object_scope;
5821 /* If we are not looking at an identifier followed by the scope
5822 resolution operator, then this is not part of a
5823 nested-name-specifier. (Note that this function is only used
5824 to parse the components of a nested-name-specifier.) */
5825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5826 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5827 return error_mark_node;
5828 scope = cp_parser_namespace_name (parser);
5831 return scope;
5834 /* Return true if we are looking at a compound-literal, false otherwise. */
5836 static bool
5837 cp_parser_compound_literal_p (cp_parser *parser)
5839 /* Consume the `('. */
5840 cp_lexer_consume_token (parser->lexer);
5842 cp_lexer_save_tokens (parser->lexer);
5844 /* Skip tokens until the next token is a closing parenthesis.
5845 If we find the closing `)', and the next token is a `{', then
5846 we are looking at a compound-literal. */
5847 bool compound_literal_p
5848 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5849 /*consume_paren=*/true)
5850 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5852 /* Roll back the tokens we skipped. */
5853 cp_lexer_rollback_tokens (parser->lexer);
5855 return compound_literal_p;
5858 /* Parse a postfix-expression.
5860 postfix-expression:
5861 primary-expression
5862 postfix-expression [ expression ]
5863 postfix-expression ( expression-list [opt] )
5864 simple-type-specifier ( expression-list [opt] )
5865 typename :: [opt] nested-name-specifier identifier
5866 ( expression-list [opt] )
5867 typename :: [opt] nested-name-specifier template [opt] template-id
5868 ( expression-list [opt] )
5869 postfix-expression . template [opt] id-expression
5870 postfix-expression -> template [opt] id-expression
5871 postfix-expression . pseudo-destructor-name
5872 postfix-expression -> pseudo-destructor-name
5873 postfix-expression ++
5874 postfix-expression --
5875 dynamic_cast < type-id > ( expression )
5876 static_cast < type-id > ( expression )
5877 reinterpret_cast < type-id > ( expression )
5878 const_cast < type-id > ( expression )
5879 typeid ( expression )
5880 typeid ( type-id )
5882 GNU Extension:
5884 postfix-expression:
5885 ( type-id ) { initializer-list , [opt] }
5887 This extension is a GNU version of the C99 compound-literal
5888 construct. (The C99 grammar uses `type-name' instead of `type-id',
5889 but they are essentially the same concept.)
5891 If ADDRESS_P is true, the postfix expression is the operand of the
5892 `&' operator. CAST_P is true if this expression is the target of a
5893 cast.
5895 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5896 class member access expressions [expr.ref].
5898 Returns a representation of the expression. */
5900 static tree
5901 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5902 bool member_access_only_p, bool decltype_p,
5903 cp_id_kind * pidk_return)
5905 cp_token *token;
5906 location_t loc;
5907 enum rid keyword;
5908 cp_id_kind idk = CP_ID_KIND_NONE;
5909 tree postfix_expression = NULL_TREE;
5910 bool is_member_access = false;
5911 int saved_in_statement = -1;
5913 /* Peek at the next token. */
5914 token = cp_lexer_peek_token (parser->lexer);
5915 loc = token->location;
5916 /* Some of the productions are determined by keywords. */
5917 keyword = token->keyword;
5918 switch (keyword)
5920 case RID_DYNCAST:
5921 case RID_STATCAST:
5922 case RID_REINTCAST:
5923 case RID_CONSTCAST:
5925 tree type;
5926 tree expression;
5927 const char *saved_message;
5928 bool saved_in_type_id_in_expr_p;
5930 /* All of these can be handled in the same way from the point
5931 of view of parsing. Begin by consuming the token
5932 identifying the cast. */
5933 cp_lexer_consume_token (parser->lexer);
5935 /* New types cannot be defined in the cast. */
5936 saved_message = parser->type_definition_forbidden_message;
5937 parser->type_definition_forbidden_message
5938 = G_("types may not be defined in casts");
5940 /* Look for the opening `<'. */
5941 cp_parser_require (parser, CPP_LESS, RT_LESS);
5942 /* Parse the type to which we are casting. */
5943 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5944 parser->in_type_id_in_expr_p = true;
5945 type = cp_parser_type_id (parser);
5946 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5947 /* Look for the closing `>'. */
5948 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5949 /* Restore the old message. */
5950 parser->type_definition_forbidden_message = saved_message;
5952 bool saved_greater_than_is_operator_p
5953 = parser->greater_than_is_operator_p;
5954 parser->greater_than_is_operator_p = true;
5956 /* And the expression which is being cast. */
5957 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5958 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5959 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5961 parser->greater_than_is_operator_p
5962 = saved_greater_than_is_operator_p;
5964 /* Only type conversions to integral or enumeration types
5965 can be used in constant-expressions. */
5966 if (!cast_valid_in_integral_constant_expression_p (type)
5967 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5968 return error_mark_node;
5970 switch (keyword)
5972 case RID_DYNCAST:
5973 postfix_expression
5974 = build_dynamic_cast (type, expression, tf_warning_or_error);
5975 break;
5976 case RID_STATCAST:
5977 postfix_expression
5978 = build_static_cast (type, expression, tf_warning_or_error);
5979 break;
5980 case RID_REINTCAST:
5981 postfix_expression
5982 = build_reinterpret_cast (type, expression,
5983 tf_warning_or_error);
5984 break;
5985 case RID_CONSTCAST:
5986 postfix_expression
5987 = build_const_cast (type, expression, tf_warning_or_error);
5988 break;
5989 default:
5990 gcc_unreachable ();
5993 break;
5995 case RID_TYPEID:
5997 tree type;
5998 const char *saved_message;
5999 bool saved_in_type_id_in_expr_p;
6001 /* Consume the `typeid' token. */
6002 cp_lexer_consume_token (parser->lexer);
6003 /* Look for the `(' token. */
6004 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6005 /* Types cannot be defined in a `typeid' expression. */
6006 saved_message = parser->type_definition_forbidden_message;
6007 parser->type_definition_forbidden_message
6008 = G_("types may not be defined in a %<typeid%> expression");
6009 /* We can't be sure yet whether we're looking at a type-id or an
6010 expression. */
6011 cp_parser_parse_tentatively (parser);
6012 /* Try a type-id first. */
6013 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6014 parser->in_type_id_in_expr_p = true;
6015 type = cp_parser_type_id (parser);
6016 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6017 /* Look for the `)' token. Otherwise, we can't be sure that
6018 we're not looking at an expression: consider `typeid (int
6019 (3))', for example. */
6020 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6021 /* If all went well, simply lookup the type-id. */
6022 if (cp_parser_parse_definitely (parser))
6023 postfix_expression = get_typeid (type, tf_warning_or_error);
6024 /* Otherwise, fall back to the expression variant. */
6025 else
6027 tree expression;
6029 /* Look for an expression. */
6030 expression = cp_parser_expression (parser, & idk);
6031 /* Compute its typeid. */
6032 postfix_expression = build_typeid (expression, tf_warning_or_error);
6033 /* Look for the `)' token. */
6034 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6036 /* Restore the saved message. */
6037 parser->type_definition_forbidden_message = saved_message;
6038 /* `typeid' may not appear in an integral constant expression. */
6039 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6040 return error_mark_node;
6042 break;
6044 case RID_TYPENAME:
6046 tree type;
6047 /* The syntax permitted here is the same permitted for an
6048 elaborated-type-specifier. */
6049 type = cp_parser_elaborated_type_specifier (parser,
6050 /*is_friend=*/false,
6051 /*is_declaration=*/false);
6052 postfix_expression = cp_parser_functional_cast (parser, type);
6054 break;
6056 case RID_CILK_SPAWN:
6058 cp_lexer_consume_token (parser->lexer);
6059 token = cp_lexer_peek_token (parser->lexer);
6060 if (token->type == CPP_SEMICOLON)
6062 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6063 "an expression");
6064 postfix_expression = error_mark_node;
6065 break;
6067 else if (!current_function_decl)
6069 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6070 "inside a function");
6071 postfix_expression = error_mark_node;
6072 break;
6074 else
6076 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6077 saved_in_statement = parser->in_statement;
6078 parser->in_statement |= IN_CILK_SPAWN;
6080 cfun->calls_cilk_spawn = 1;
6081 postfix_expression =
6082 cp_parser_postfix_expression (parser, false, false,
6083 false, false, &idk);
6084 if (!flag_cilkplus)
6086 error_at (token->location, "-fcilkplus must be enabled to use"
6087 " %<_Cilk_spawn%>");
6088 cfun->calls_cilk_spawn = 0;
6090 else if (saved_in_statement & IN_CILK_SPAWN)
6092 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6093 "are not permitted");
6094 postfix_expression = error_mark_node;
6095 cfun->calls_cilk_spawn = 0;
6097 else
6099 postfix_expression = build_cilk_spawn (token->location,
6100 postfix_expression);
6101 if (postfix_expression != error_mark_node)
6102 SET_EXPR_LOCATION (postfix_expression, input_location);
6103 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6105 break;
6108 case RID_BUILTIN_SHUFFLE:
6110 vec<tree, va_gc> *vec;
6111 unsigned int i;
6112 tree p;
6114 cp_lexer_consume_token (parser->lexer);
6115 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6116 /*cast_p=*/false, /*allow_expansion_p=*/true,
6117 /*non_constant_p=*/NULL);
6118 if (vec == NULL)
6119 return error_mark_node;
6121 FOR_EACH_VEC_ELT (*vec, i, p)
6122 mark_exp_read (p);
6124 if (vec->length () == 2)
6125 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6126 tf_warning_or_error);
6127 else if (vec->length () == 3)
6128 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6129 tf_warning_or_error);
6130 else
6132 error_at (loc, "wrong number of arguments to "
6133 "%<__builtin_shuffle%>");
6134 return error_mark_node;
6136 break;
6139 default:
6141 tree type;
6143 /* If the next thing is a simple-type-specifier, we may be
6144 looking at a functional cast. We could also be looking at
6145 an id-expression. So, we try the functional cast, and if
6146 that doesn't work we fall back to the primary-expression. */
6147 cp_parser_parse_tentatively (parser);
6148 /* Look for the simple-type-specifier. */
6149 type = cp_parser_simple_type_specifier (parser,
6150 /*decl_specs=*/NULL,
6151 CP_PARSER_FLAGS_NONE);
6152 /* Parse the cast itself. */
6153 if (!cp_parser_error_occurred (parser))
6154 postfix_expression
6155 = cp_parser_functional_cast (parser, type);
6156 /* If that worked, we're done. */
6157 if (cp_parser_parse_definitely (parser))
6158 break;
6160 /* If the functional-cast didn't work out, try a
6161 compound-literal. */
6162 if (cp_parser_allow_gnu_extensions_p (parser)
6163 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6165 tree initializer = NULL_TREE;
6167 cp_parser_parse_tentatively (parser);
6169 /* Avoid calling cp_parser_type_id pointlessly, see comment
6170 in cp_parser_cast_expression about c++/29234. */
6171 if (!cp_parser_compound_literal_p (parser))
6172 cp_parser_simulate_error (parser);
6173 else
6175 /* Parse the type. */
6176 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6177 parser->in_type_id_in_expr_p = true;
6178 type = cp_parser_type_id (parser);
6179 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6180 /* Look for the `)'. */
6181 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6184 /* If things aren't going well, there's no need to
6185 keep going. */
6186 if (!cp_parser_error_occurred (parser))
6188 bool non_constant_p;
6189 /* Parse the brace-enclosed initializer list. */
6190 initializer = cp_parser_braced_list (parser,
6191 &non_constant_p);
6193 /* If that worked, we're definitely looking at a
6194 compound-literal expression. */
6195 if (cp_parser_parse_definitely (parser))
6197 /* Warn the user that a compound literal is not
6198 allowed in standard C++. */
6199 pedwarn (input_location, OPT_Wpedantic,
6200 "ISO C++ forbids compound-literals");
6201 /* For simplicity, we disallow compound literals in
6202 constant-expressions. We could
6203 allow compound literals of integer type, whose
6204 initializer was a constant, in constant
6205 expressions. Permitting that usage, as a further
6206 extension, would not change the meaning of any
6207 currently accepted programs. (Of course, as
6208 compound literals are not part of ISO C++, the
6209 standard has nothing to say.) */
6210 if (cp_parser_non_integral_constant_expression (parser,
6211 NIC_NCC))
6213 postfix_expression = error_mark_node;
6214 break;
6216 /* Form the representation of the compound-literal. */
6217 postfix_expression
6218 = finish_compound_literal (type, initializer,
6219 tf_warning_or_error);
6220 break;
6224 /* It must be a primary-expression. */
6225 postfix_expression
6226 = cp_parser_primary_expression (parser, address_p, cast_p,
6227 /*template_arg_p=*/false,
6228 decltype_p,
6229 &idk);
6231 break;
6234 /* Note that we don't need to worry about calling build_cplus_new on a
6235 class-valued CALL_EXPR in decltype when it isn't the end of the
6236 postfix-expression; unary_complex_lvalue will take care of that for
6237 all these cases. */
6239 /* Keep looping until the postfix-expression is complete. */
6240 while (true)
6242 if (idk == CP_ID_KIND_UNQUALIFIED
6243 && identifier_p (postfix_expression)
6244 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6245 /* It is not a Koenig lookup function call. */
6246 postfix_expression
6247 = unqualified_name_lookup_error (postfix_expression);
6249 /* Peek at the next token. */
6250 token = cp_lexer_peek_token (parser->lexer);
6252 switch (token->type)
6254 case CPP_OPEN_SQUARE:
6255 if (cp_next_tokens_can_be_std_attribute_p (parser))
6257 cp_parser_error (parser,
6258 "two consecutive %<[%> shall "
6259 "only introduce an attribute");
6260 return error_mark_node;
6262 postfix_expression
6263 = cp_parser_postfix_open_square_expression (parser,
6264 postfix_expression,
6265 false,
6266 decltype_p);
6267 idk = CP_ID_KIND_NONE;
6268 is_member_access = false;
6269 break;
6271 case CPP_OPEN_PAREN:
6272 /* postfix-expression ( expression-list [opt] ) */
6274 bool koenig_p;
6275 bool is_builtin_constant_p;
6276 bool saved_integral_constant_expression_p = false;
6277 bool saved_non_integral_constant_expression_p = false;
6278 tsubst_flags_t complain = complain_flags (decltype_p);
6279 vec<tree, va_gc> *args;
6281 is_member_access = false;
6283 is_builtin_constant_p
6284 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6285 if (is_builtin_constant_p)
6287 /* The whole point of __builtin_constant_p is to allow
6288 non-constant expressions to appear as arguments. */
6289 saved_integral_constant_expression_p
6290 = parser->integral_constant_expression_p;
6291 saved_non_integral_constant_expression_p
6292 = parser->non_integral_constant_expression_p;
6293 parser->integral_constant_expression_p = false;
6295 args = (cp_parser_parenthesized_expression_list
6296 (parser, non_attr,
6297 /*cast_p=*/false, /*allow_expansion_p=*/true,
6298 /*non_constant_p=*/NULL,
6299 /*want_literal_zero_p=*/warn_memset_transposed_args));
6300 if (is_builtin_constant_p)
6302 parser->integral_constant_expression_p
6303 = saved_integral_constant_expression_p;
6304 parser->non_integral_constant_expression_p
6305 = saved_non_integral_constant_expression_p;
6308 if (args == NULL)
6310 postfix_expression = error_mark_node;
6311 break;
6314 /* Function calls are not permitted in
6315 constant-expressions. */
6316 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6317 && cp_parser_non_integral_constant_expression (parser,
6318 NIC_FUNC_CALL))
6320 postfix_expression = error_mark_node;
6321 release_tree_vector (args);
6322 break;
6325 koenig_p = false;
6326 if (idk == CP_ID_KIND_UNQUALIFIED
6327 || idk == CP_ID_KIND_TEMPLATE_ID)
6329 if (identifier_p (postfix_expression))
6331 if (!args->is_empty ())
6333 koenig_p = true;
6334 if (!any_type_dependent_arguments_p (args))
6335 postfix_expression
6336 = perform_koenig_lookup (postfix_expression, args,
6337 complain);
6339 else
6340 postfix_expression
6341 = unqualified_fn_lookup_error (postfix_expression);
6343 /* We do not perform argument-dependent lookup if
6344 normal lookup finds a non-function, in accordance
6345 with the expected resolution of DR 218. */
6346 else if (!args->is_empty ()
6347 && is_overloaded_fn (postfix_expression))
6349 tree fn = get_first_fn (postfix_expression);
6350 fn = STRIP_TEMPLATE (fn);
6352 /* Do not do argument dependent lookup if regular
6353 lookup finds a member function or a block-scope
6354 function declaration. [basic.lookup.argdep]/3 */
6355 if (!DECL_FUNCTION_MEMBER_P (fn)
6356 && !DECL_LOCAL_FUNCTION_P (fn))
6358 koenig_p = true;
6359 if (!any_type_dependent_arguments_p (args))
6360 postfix_expression
6361 = perform_koenig_lookup (postfix_expression, args,
6362 complain);
6367 if (warn_memset_transposed_args)
6369 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6370 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6371 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6372 && vec_safe_length (args) == 3
6373 && integer_zerop ((*args)[2])
6374 && LITERAL_ZERO_P ((*args)[2])
6375 && !(integer_zerop ((*args)[1])
6376 && LITERAL_ZERO_P ((*args)[1])))
6377 warning (OPT_Wmemset_transposed_args,
6378 "%<memset%> used with constant zero length "
6379 "parameter; this could be due to transposed "
6380 "parameters");
6382 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6383 to avoid leaking those into folder and middle-end. */
6384 unsigned int i;
6385 tree arg;
6386 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6387 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6388 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6391 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6393 tree instance = TREE_OPERAND (postfix_expression, 0);
6394 tree fn = TREE_OPERAND (postfix_expression, 1);
6396 if (processing_template_decl
6397 && (type_dependent_expression_p (instance)
6398 || (!BASELINK_P (fn)
6399 && TREE_CODE (fn) != FIELD_DECL)
6400 || type_dependent_expression_p (fn)
6401 || any_type_dependent_arguments_p (args)))
6403 postfix_expression
6404 = build_nt_call_vec (postfix_expression, args);
6405 release_tree_vector (args);
6406 break;
6409 if (BASELINK_P (fn))
6411 postfix_expression
6412 = (build_new_method_call
6413 (instance, fn, &args, NULL_TREE,
6414 (idk == CP_ID_KIND_QUALIFIED
6415 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6416 : LOOKUP_NORMAL),
6417 /*fn_p=*/NULL,
6418 complain));
6420 else
6421 postfix_expression
6422 = finish_call_expr (postfix_expression, &args,
6423 /*disallow_virtual=*/false,
6424 /*koenig_p=*/false,
6425 complain);
6427 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6428 || TREE_CODE (postfix_expression) == MEMBER_REF
6429 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6430 postfix_expression = (build_offset_ref_call_from_tree
6431 (postfix_expression, &args,
6432 complain));
6433 else if (idk == CP_ID_KIND_QUALIFIED)
6434 /* A call to a static class member, or a namespace-scope
6435 function. */
6436 postfix_expression
6437 = finish_call_expr (postfix_expression, &args,
6438 /*disallow_virtual=*/true,
6439 koenig_p,
6440 complain);
6441 else
6442 /* All other function calls. */
6443 postfix_expression
6444 = finish_call_expr (postfix_expression, &args,
6445 /*disallow_virtual=*/false,
6446 koenig_p,
6447 complain);
6449 protected_set_expr_location (postfix_expression, token->location);
6451 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6452 idk = CP_ID_KIND_NONE;
6454 release_tree_vector (args);
6456 break;
6458 case CPP_DOT:
6459 case CPP_DEREF:
6460 /* postfix-expression . template [opt] id-expression
6461 postfix-expression . pseudo-destructor-name
6462 postfix-expression -> template [opt] id-expression
6463 postfix-expression -> pseudo-destructor-name */
6465 /* Consume the `.' or `->' operator. */
6466 cp_lexer_consume_token (parser->lexer);
6468 postfix_expression
6469 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6470 postfix_expression,
6471 false, &idk, loc);
6473 is_member_access = true;
6474 break;
6476 case CPP_PLUS_PLUS:
6477 /* postfix-expression ++ */
6478 /* Consume the `++' token. */
6479 cp_lexer_consume_token (parser->lexer);
6480 /* Generate a representation for the complete expression. */
6481 postfix_expression
6482 = finish_increment_expr (postfix_expression,
6483 POSTINCREMENT_EXPR);
6484 /* Increments may not appear in constant-expressions. */
6485 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6486 postfix_expression = error_mark_node;
6487 idk = CP_ID_KIND_NONE;
6488 is_member_access = false;
6489 break;
6491 case CPP_MINUS_MINUS:
6492 /* postfix-expression -- */
6493 /* Consume the `--' token. */
6494 cp_lexer_consume_token (parser->lexer);
6495 /* Generate a representation for the complete expression. */
6496 postfix_expression
6497 = finish_increment_expr (postfix_expression,
6498 POSTDECREMENT_EXPR);
6499 /* Decrements may not appear in constant-expressions. */
6500 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6501 postfix_expression = error_mark_node;
6502 idk = CP_ID_KIND_NONE;
6503 is_member_access = false;
6504 break;
6506 default:
6507 if (pidk_return != NULL)
6508 * pidk_return = idk;
6509 if (member_access_only_p)
6510 return is_member_access? postfix_expression : error_mark_node;
6511 else
6512 return postfix_expression;
6516 /* We should never get here. */
6517 gcc_unreachable ();
6518 return error_mark_node;
6521 /* This function parses Cilk Plus array notations. If a normal array expr. is
6522 parsed then the array index is passed back to the caller through *INIT_INDEX
6523 and the function returns a NULL_TREE. If array notation expr. is parsed,
6524 then *INIT_INDEX is ignored by the caller and the function returns
6525 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6526 error_mark_node. */
6528 static tree
6529 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6530 tree array_value)
6532 cp_token *token = NULL;
6533 tree length_index, stride = NULL_TREE, value_tree, array_type;
6534 if (!array_value || array_value == error_mark_node)
6536 cp_parser_skip_to_end_of_statement (parser);
6537 return error_mark_node;
6540 array_type = TREE_TYPE (array_value);
6542 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6543 parser->colon_corrects_to_scope_p = false;
6544 token = cp_lexer_peek_token (parser->lexer);
6546 if (!token)
6548 cp_parser_error (parser, "expected %<:%> or numeral");
6549 return error_mark_node;
6551 else if (token->type == CPP_COLON)
6553 /* Consume the ':'. */
6554 cp_lexer_consume_token (parser->lexer);
6556 /* If we are here, then we have a case like this A[:]. */
6557 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6559 cp_parser_error (parser, "expected %<]%>");
6560 cp_parser_skip_to_end_of_statement (parser);
6561 return error_mark_node;
6563 *init_index = NULL_TREE;
6564 stride = NULL_TREE;
6565 length_index = NULL_TREE;
6567 else
6569 /* If we are here, then there are three valid possibilities:
6570 1. ARRAY [ EXP ]
6571 2. ARRAY [ EXP : EXP ]
6572 3. ARRAY [ EXP : EXP : EXP ] */
6574 *init_index = cp_parser_expression (parser);
6575 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6577 /* This indicates that we have a normal array expression. */
6578 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6579 return NULL_TREE;
6582 /* Consume the ':'. */
6583 cp_lexer_consume_token (parser->lexer);
6584 length_index = cp_parser_expression (parser);
6585 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6587 cp_lexer_consume_token (parser->lexer);
6588 stride = cp_parser_expression (parser);
6591 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6593 if (*init_index == error_mark_node || length_index == error_mark_node
6594 || stride == error_mark_node || array_type == error_mark_node)
6596 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6597 cp_lexer_consume_token (parser->lexer);
6598 return error_mark_node;
6600 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6602 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6603 length_index, stride, array_type);
6604 return value_tree;
6607 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6608 by cp_parser_builtin_offsetof. We're looking for
6610 postfix-expression [ expression ]
6611 postfix-expression [ braced-init-list ] (C++11)
6613 FOR_OFFSETOF is set if we're being called in that context, which
6614 changes how we deal with integer constant expressions. */
6616 static tree
6617 cp_parser_postfix_open_square_expression (cp_parser *parser,
6618 tree postfix_expression,
6619 bool for_offsetof,
6620 bool decltype_p)
6622 tree index = NULL_TREE;
6623 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6624 bool saved_greater_than_is_operator_p;
6626 /* Consume the `[' token. */
6627 cp_lexer_consume_token (parser->lexer);
6629 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6630 parser->greater_than_is_operator_p = true;
6632 /* Parse the index expression. */
6633 /* ??? For offsetof, there is a question of what to allow here. If
6634 offsetof is not being used in an integral constant expression context,
6635 then we *could* get the right answer by computing the value at runtime.
6636 If we are in an integral constant expression context, then we might
6637 could accept any constant expression; hard to say without analysis.
6638 Rather than open the barn door too wide right away, allow only integer
6639 constant expressions here. */
6640 if (for_offsetof)
6641 index = cp_parser_constant_expression (parser);
6642 else
6644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6646 bool expr_nonconst_p;
6647 cp_lexer_set_source_position (parser->lexer);
6648 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6649 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6650 if (flag_cilkplus
6651 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6653 error_at (cp_lexer_peek_token (parser->lexer)->location,
6654 "braced list index is not allowed with array "
6655 "notation");
6656 cp_parser_skip_to_end_of_statement (parser);
6657 return error_mark_node;
6660 else if (flag_cilkplus)
6662 /* Here are have these two options:
6663 ARRAY[EXP : EXP] - Array notation expr with default
6664 stride of 1.
6665 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6666 stride. */
6667 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6668 postfix_expression);
6669 if (an_exp)
6670 return an_exp;
6672 else
6673 index = cp_parser_expression (parser);
6676 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6678 /* Look for the closing `]'. */
6679 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6681 /* Build the ARRAY_REF. */
6682 postfix_expression = grok_array_decl (loc, postfix_expression,
6683 index, decltype_p);
6685 /* When not doing offsetof, array references are not permitted in
6686 constant-expressions. */
6687 if (!for_offsetof
6688 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6689 postfix_expression = error_mark_node;
6691 return postfix_expression;
6694 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6695 by cp_parser_builtin_offsetof. We're looking for
6697 postfix-expression . template [opt] id-expression
6698 postfix-expression . pseudo-destructor-name
6699 postfix-expression -> template [opt] id-expression
6700 postfix-expression -> pseudo-destructor-name
6702 FOR_OFFSETOF is set if we're being called in that context. That sorta
6703 limits what of the above we'll actually accept, but nevermind.
6704 TOKEN_TYPE is the "." or "->" token, which will already have been
6705 removed from the stream. */
6707 static tree
6708 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6709 enum cpp_ttype token_type,
6710 tree postfix_expression,
6711 bool for_offsetof, cp_id_kind *idk,
6712 location_t location)
6714 tree name;
6715 bool dependent_p;
6716 bool pseudo_destructor_p;
6717 tree scope = NULL_TREE;
6719 /* If this is a `->' operator, dereference the pointer. */
6720 if (token_type == CPP_DEREF)
6721 postfix_expression = build_x_arrow (location, postfix_expression,
6722 tf_warning_or_error);
6723 /* Check to see whether or not the expression is type-dependent. */
6724 dependent_p = type_dependent_expression_p (postfix_expression);
6725 /* The identifier following the `->' or `.' is not qualified. */
6726 parser->scope = NULL_TREE;
6727 parser->qualifying_scope = NULL_TREE;
6728 parser->object_scope = NULL_TREE;
6729 *idk = CP_ID_KIND_NONE;
6731 /* Enter the scope corresponding to the type of the object
6732 given by the POSTFIX_EXPRESSION. */
6733 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6735 scope = TREE_TYPE (postfix_expression);
6736 /* According to the standard, no expression should ever have
6737 reference type. Unfortunately, we do not currently match
6738 the standard in this respect in that our internal representation
6739 of an expression may have reference type even when the standard
6740 says it does not. Therefore, we have to manually obtain the
6741 underlying type here. */
6742 scope = non_reference (scope);
6743 /* The type of the POSTFIX_EXPRESSION must be complete. */
6744 if (scope == unknown_type_node)
6746 error_at (location, "%qE does not have class type",
6747 postfix_expression);
6748 scope = NULL_TREE;
6750 /* Unlike the object expression in other contexts, *this is not
6751 required to be of complete type for purposes of class member
6752 access (5.2.5) outside the member function body. */
6753 else if (postfix_expression != current_class_ref
6754 && !(processing_template_decl && scope == current_class_type))
6755 scope = complete_type_or_else (scope, NULL_TREE);
6756 /* Let the name lookup machinery know that we are processing a
6757 class member access expression. */
6758 parser->context->object_type = scope;
6759 /* If something went wrong, we want to be able to discern that case,
6760 as opposed to the case where there was no SCOPE due to the type
6761 of expression being dependent. */
6762 if (!scope)
6763 scope = error_mark_node;
6764 /* If the SCOPE was erroneous, make the various semantic analysis
6765 functions exit quickly -- and without issuing additional error
6766 messages. */
6767 if (scope == error_mark_node)
6768 postfix_expression = error_mark_node;
6771 /* Assume this expression is not a pseudo-destructor access. */
6772 pseudo_destructor_p = false;
6774 /* If the SCOPE is a scalar type, then, if this is a valid program,
6775 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6776 is type dependent, it can be pseudo-destructor-name or something else.
6777 Try to parse it as pseudo-destructor-name first. */
6778 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6780 tree s;
6781 tree type;
6783 cp_parser_parse_tentatively (parser);
6784 /* Parse the pseudo-destructor-name. */
6785 s = NULL_TREE;
6786 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6787 &s, &type);
6788 if (dependent_p
6789 && (cp_parser_error_occurred (parser)
6790 || !SCALAR_TYPE_P (type)))
6791 cp_parser_abort_tentative_parse (parser);
6792 else if (cp_parser_parse_definitely (parser))
6794 pseudo_destructor_p = true;
6795 postfix_expression
6796 = finish_pseudo_destructor_expr (postfix_expression,
6797 s, type, location);
6801 if (!pseudo_destructor_p)
6803 /* If the SCOPE is not a scalar type, we are looking at an
6804 ordinary class member access expression, rather than a
6805 pseudo-destructor-name. */
6806 bool template_p;
6807 cp_token *token = cp_lexer_peek_token (parser->lexer);
6808 /* Parse the id-expression. */
6809 name = (cp_parser_id_expression
6810 (parser,
6811 cp_parser_optional_template_keyword (parser),
6812 /*check_dependency_p=*/true,
6813 &template_p,
6814 /*declarator_p=*/false,
6815 /*optional_p=*/false));
6816 /* In general, build a SCOPE_REF if the member name is qualified.
6817 However, if the name was not dependent and has already been
6818 resolved; there is no need to build the SCOPE_REF. For example;
6820 struct X { void f(); };
6821 template <typename T> void f(T* t) { t->X::f(); }
6823 Even though "t" is dependent, "X::f" is not and has been resolved
6824 to a BASELINK; there is no need to include scope information. */
6826 /* But we do need to remember that there was an explicit scope for
6827 virtual function calls. */
6828 if (parser->scope)
6829 *idk = CP_ID_KIND_QUALIFIED;
6831 /* If the name is a template-id that names a type, we will get a
6832 TYPE_DECL here. That is invalid code. */
6833 if (TREE_CODE (name) == TYPE_DECL)
6835 error_at (token->location, "invalid use of %qD", name);
6836 postfix_expression = error_mark_node;
6838 else
6840 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6842 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6844 error_at (token->location, "%<%D::%D%> is not a class member",
6845 parser->scope, name);
6846 postfix_expression = error_mark_node;
6848 else
6849 name = build_qualified_name (/*type=*/NULL_TREE,
6850 parser->scope,
6851 name,
6852 template_p);
6853 parser->scope = NULL_TREE;
6854 parser->qualifying_scope = NULL_TREE;
6855 parser->object_scope = NULL_TREE;
6857 if (parser->scope && name && BASELINK_P (name))
6858 adjust_result_of_qualified_name_lookup
6859 (name, parser->scope, scope);
6860 postfix_expression
6861 = finish_class_member_access_expr (postfix_expression, name,
6862 template_p,
6863 tf_warning_or_error);
6867 /* We no longer need to look up names in the scope of the object on
6868 the left-hand side of the `.' or `->' operator. */
6869 parser->context->object_type = NULL_TREE;
6871 /* Outside of offsetof, these operators may not appear in
6872 constant-expressions. */
6873 if (!for_offsetof
6874 && (cp_parser_non_integral_constant_expression
6875 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6876 postfix_expression = error_mark_node;
6878 return postfix_expression;
6881 /* Cache of LITERAL_ZERO_P constants. */
6883 static GTY(()) tree literal_zeros[itk_none];
6885 /* Parse a parenthesized expression-list.
6887 expression-list:
6888 assignment-expression
6889 expression-list, assignment-expression
6891 attribute-list:
6892 expression-list
6893 identifier
6894 identifier, expression-list
6896 CAST_P is true if this expression is the target of a cast.
6898 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6899 argument pack.
6901 Returns a vector of trees. Each element is a representation of an
6902 assignment-expression. NULL is returned if the ( and or ) are
6903 missing. An empty, but allocated, vector is returned on no
6904 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6905 if we are parsing an attribute list for an attribute that wants a
6906 plain identifier argument, normal_attr for an attribute that wants
6907 an expression, or non_attr if we aren't parsing an attribute list. If
6908 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6909 not all of the expressions in the list were constant.
6910 WANT_LITERAL_ZERO_P is true if the caller is interested in
6911 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6912 immediately, this can be removed. */
6914 static vec<tree, va_gc> *
6915 cp_parser_parenthesized_expression_list (cp_parser* parser,
6916 int is_attribute_list,
6917 bool cast_p,
6918 bool allow_expansion_p,
6919 bool *non_constant_p,
6920 bool want_literal_zero_p)
6922 vec<tree, va_gc> *expression_list;
6923 bool fold_expr_p = is_attribute_list != non_attr;
6924 tree identifier = NULL_TREE;
6925 bool saved_greater_than_is_operator_p;
6927 /* Assume all the expressions will be constant. */
6928 if (non_constant_p)
6929 *non_constant_p = false;
6931 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6932 return NULL;
6934 expression_list = make_tree_vector ();
6936 /* Within a parenthesized expression, a `>' token is always
6937 the greater-than operator. */
6938 saved_greater_than_is_operator_p
6939 = parser->greater_than_is_operator_p;
6940 parser->greater_than_is_operator_p = true;
6942 /* Consume expressions until there are no more. */
6943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6944 while (true)
6946 tree expr;
6948 /* At the beginning of attribute lists, check to see if the
6949 next token is an identifier. */
6950 if (is_attribute_list == id_attr
6951 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6953 cp_token *token;
6955 /* Consume the identifier. */
6956 token = cp_lexer_consume_token (parser->lexer);
6957 /* Save the identifier. */
6958 identifier = token->u.value;
6960 else
6962 bool expr_non_constant_p;
6964 /* Parse the next assignment-expression. */
6965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6967 /* A braced-init-list. */
6968 cp_lexer_set_source_position (parser->lexer);
6969 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6970 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6971 if (non_constant_p && expr_non_constant_p)
6972 *non_constant_p = true;
6974 else if (non_constant_p)
6976 expr = (cp_parser_constant_expression
6977 (parser, /*allow_non_constant_p=*/true,
6978 &expr_non_constant_p));
6979 if (expr_non_constant_p)
6980 *non_constant_p = true;
6982 else
6984 expr = NULL_TREE;
6985 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6986 switch (tok->type)
6988 case CPP_NUMBER:
6989 case CPP_CHAR:
6990 case CPP_WCHAR:
6991 case CPP_CHAR16:
6992 case CPP_CHAR32:
6993 /* If a parameter is literal zero alone, remember it
6994 for -Wmemset-transposed-args warning. */
6995 if (integer_zerop (tok->u.value)
6996 && !TREE_OVERFLOW (tok->u.value)
6997 && want_literal_zero_p
6998 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6999 == CPP_COMMA
7000 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
7001 == CPP_CLOSE_PAREN))
7003 unsigned int i;
7004 for (i = 0; i < itk_none; ++i)
7005 if (TREE_TYPE (tok->u.value) == integer_types[i])
7006 break;
7007 if (i < itk_none && literal_zeros[i])
7008 expr = literal_zeros[i];
7009 else
7011 expr = copy_node (tok->u.value);
7012 LITERAL_ZERO_P (expr) = 1;
7013 if (i < itk_none)
7014 literal_zeros[i] = expr;
7016 /* Consume the 0 token (or '\0', 0LL etc.). */
7017 cp_lexer_consume_token (parser->lexer);
7019 break;
7020 default:
7021 break;
7023 if (expr == NULL_TREE)
7024 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7025 cast_p);
7028 if (fold_expr_p)
7029 expr = instantiate_non_dependent_expr (expr);
7031 /* If we have an ellipsis, then this is an expression
7032 expansion. */
7033 if (allow_expansion_p
7034 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7036 /* Consume the `...'. */
7037 cp_lexer_consume_token (parser->lexer);
7039 /* Build the argument pack. */
7040 expr = make_pack_expansion (expr);
7043 /* Add it to the list. We add error_mark_node
7044 expressions to the list, so that we can still tell if
7045 the correct form for a parenthesized expression-list
7046 is found. That gives better errors. */
7047 vec_safe_push (expression_list, expr);
7049 if (expr == error_mark_node)
7050 goto skip_comma;
7053 /* After the first item, attribute lists look the same as
7054 expression lists. */
7055 is_attribute_list = non_attr;
7057 get_comma:;
7058 /* If the next token isn't a `,', then we are done. */
7059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7060 break;
7062 /* Otherwise, consume the `,' and keep going. */
7063 cp_lexer_consume_token (parser->lexer);
7066 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7068 int ending;
7070 skip_comma:;
7071 /* We try and resync to an unnested comma, as that will give the
7072 user better diagnostics. */
7073 ending = cp_parser_skip_to_closing_parenthesis (parser,
7074 /*recovering=*/true,
7075 /*or_comma=*/true,
7076 /*consume_paren=*/true);
7077 if (ending < 0)
7078 goto get_comma;
7079 if (!ending)
7081 parser->greater_than_is_operator_p
7082 = saved_greater_than_is_operator_p;
7083 return NULL;
7087 parser->greater_than_is_operator_p
7088 = saved_greater_than_is_operator_p;
7090 if (identifier)
7091 vec_safe_insert (expression_list, 0, identifier);
7093 return expression_list;
7096 /* Parse a pseudo-destructor-name.
7098 pseudo-destructor-name:
7099 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7100 :: [opt] nested-name-specifier template template-id :: ~ type-name
7101 :: [opt] nested-name-specifier [opt] ~ type-name
7103 If either of the first two productions is used, sets *SCOPE to the
7104 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7105 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7106 or ERROR_MARK_NODE if the parse fails. */
7108 static void
7109 cp_parser_pseudo_destructor_name (cp_parser* parser,
7110 tree object,
7111 tree* scope,
7112 tree* type)
7114 bool nested_name_specifier_p;
7116 /* Handle ~auto. */
7117 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7118 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7119 && !type_dependent_expression_p (object))
7121 if (cxx_dialect < cxx14)
7122 pedwarn (input_location, 0,
7123 "%<~auto%> only available with "
7124 "-std=c++14 or -std=gnu++14");
7125 cp_lexer_consume_token (parser->lexer);
7126 cp_lexer_consume_token (parser->lexer);
7127 *scope = NULL_TREE;
7128 *type = TREE_TYPE (object);
7129 return;
7132 /* Assume that things will not work out. */
7133 *type = error_mark_node;
7135 /* Look for the optional `::' operator. */
7136 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7137 /* Look for the optional nested-name-specifier. */
7138 nested_name_specifier_p
7139 = (cp_parser_nested_name_specifier_opt (parser,
7140 /*typename_keyword_p=*/false,
7141 /*check_dependency_p=*/true,
7142 /*type_p=*/false,
7143 /*is_declaration=*/false)
7144 != NULL_TREE);
7145 /* Now, if we saw a nested-name-specifier, we might be doing the
7146 second production. */
7147 if (nested_name_specifier_p
7148 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7150 /* Consume the `template' keyword. */
7151 cp_lexer_consume_token (parser->lexer);
7152 /* Parse the template-id. */
7153 cp_parser_template_id (parser,
7154 /*template_keyword_p=*/true,
7155 /*check_dependency_p=*/false,
7156 class_type,
7157 /*is_declaration=*/true);
7158 /* Look for the `::' token. */
7159 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7161 /* If the next token is not a `~', then there might be some
7162 additional qualification. */
7163 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7165 /* At this point, we're looking for "type-name :: ~". The type-name
7166 must not be a class-name, since this is a pseudo-destructor. So,
7167 it must be either an enum-name, or a typedef-name -- both of which
7168 are just identifiers. So, we peek ahead to check that the "::"
7169 and "~" tokens are present; if they are not, then we can avoid
7170 calling type_name. */
7171 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7172 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7173 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7175 cp_parser_error (parser, "non-scalar type");
7176 return;
7179 /* Look for the type-name. */
7180 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7181 if (*scope == error_mark_node)
7182 return;
7184 /* Look for the `::' token. */
7185 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7187 else
7188 *scope = NULL_TREE;
7190 /* Look for the `~'. */
7191 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7193 /* Once we see the ~, this has to be a pseudo-destructor. */
7194 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7195 cp_parser_commit_to_topmost_tentative_parse (parser);
7197 /* Look for the type-name again. We are not responsible for
7198 checking that it matches the first type-name. */
7199 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7202 /* Parse a unary-expression.
7204 unary-expression:
7205 postfix-expression
7206 ++ cast-expression
7207 -- cast-expression
7208 unary-operator cast-expression
7209 sizeof unary-expression
7210 sizeof ( type-id )
7211 alignof ( type-id ) [C++0x]
7212 new-expression
7213 delete-expression
7215 GNU Extensions:
7217 unary-expression:
7218 __extension__ cast-expression
7219 __alignof__ unary-expression
7220 __alignof__ ( type-id )
7221 alignof unary-expression [C++0x]
7222 __real__ cast-expression
7223 __imag__ cast-expression
7224 && identifier
7225 sizeof ( type-id ) { initializer-list , [opt] }
7226 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7227 __alignof__ ( type-id ) { initializer-list , [opt] }
7229 ADDRESS_P is true iff the unary-expression is appearing as the
7230 operand of the `&' operator. CAST_P is true if this expression is
7231 the target of a cast.
7233 Returns a representation of the expression. */
7235 static tree
7236 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7237 bool address_p, bool cast_p, bool decltype_p)
7239 cp_token *token;
7240 enum tree_code unary_operator;
7242 /* Peek at the next token. */
7243 token = cp_lexer_peek_token (parser->lexer);
7244 /* Some keywords give away the kind of expression. */
7245 if (token->type == CPP_KEYWORD)
7247 enum rid keyword = token->keyword;
7249 switch (keyword)
7251 case RID_ALIGNOF:
7252 case RID_SIZEOF:
7254 tree operand, ret;
7255 enum tree_code op;
7256 location_t first_loc;
7258 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7259 /* Consume the token. */
7260 cp_lexer_consume_token (parser->lexer);
7261 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7262 /* Parse the operand. */
7263 operand = cp_parser_sizeof_operand (parser, keyword);
7265 if (TYPE_P (operand))
7266 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7267 else
7269 /* ISO C++ defines alignof only with types, not with
7270 expressions. So pedwarn if alignof is used with a non-
7271 type expression. However, __alignof__ is ok. */
7272 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7273 pedwarn (token->location, OPT_Wpedantic,
7274 "ISO C++ does not allow %<alignof%> "
7275 "with a non-type");
7277 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7279 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7280 SIZEOF_EXPR with the original operand. */
7281 if (op == SIZEOF_EXPR && ret != error_mark_node)
7283 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7285 if (!processing_template_decl && TYPE_P (operand))
7287 ret = build_min (SIZEOF_EXPR, size_type_node,
7288 build1 (NOP_EXPR, operand,
7289 error_mark_node));
7290 SIZEOF_EXPR_TYPE_P (ret) = 1;
7292 else
7293 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7294 TREE_SIDE_EFFECTS (ret) = 0;
7295 TREE_READONLY (ret) = 1;
7297 SET_EXPR_LOCATION (ret, first_loc);
7299 return ret;
7302 case RID_NEW:
7303 return cp_parser_new_expression (parser);
7305 case RID_DELETE:
7306 return cp_parser_delete_expression (parser);
7308 case RID_EXTENSION:
7310 /* The saved value of the PEDANTIC flag. */
7311 int saved_pedantic;
7312 tree expr;
7314 /* Save away the PEDANTIC flag. */
7315 cp_parser_extension_opt (parser, &saved_pedantic);
7316 /* Parse the cast-expression. */
7317 expr = cp_parser_simple_cast_expression (parser);
7318 /* Restore the PEDANTIC flag. */
7319 pedantic = saved_pedantic;
7321 return expr;
7324 case RID_REALPART:
7325 case RID_IMAGPART:
7327 tree expression;
7329 /* Consume the `__real__' or `__imag__' token. */
7330 cp_lexer_consume_token (parser->lexer);
7331 /* Parse the cast-expression. */
7332 expression = cp_parser_simple_cast_expression (parser);
7333 /* Create the complete representation. */
7334 return build_x_unary_op (token->location,
7335 (keyword == RID_REALPART
7336 ? REALPART_EXPR : IMAGPART_EXPR),
7337 expression,
7338 tf_warning_or_error);
7340 break;
7342 case RID_TRANSACTION_ATOMIC:
7343 case RID_TRANSACTION_RELAXED:
7344 return cp_parser_transaction_expression (parser, keyword);
7346 case RID_NOEXCEPT:
7348 tree expr;
7349 const char *saved_message;
7350 bool saved_integral_constant_expression_p;
7351 bool saved_non_integral_constant_expression_p;
7352 bool saved_greater_than_is_operator_p;
7354 cp_lexer_consume_token (parser->lexer);
7355 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7357 saved_message = parser->type_definition_forbidden_message;
7358 parser->type_definition_forbidden_message
7359 = G_("types may not be defined in %<noexcept%> expressions");
7361 saved_integral_constant_expression_p
7362 = parser->integral_constant_expression_p;
7363 saved_non_integral_constant_expression_p
7364 = parser->non_integral_constant_expression_p;
7365 parser->integral_constant_expression_p = false;
7367 saved_greater_than_is_operator_p
7368 = parser->greater_than_is_operator_p;
7369 parser->greater_than_is_operator_p = true;
7371 ++cp_unevaluated_operand;
7372 ++c_inhibit_evaluation_warnings;
7373 ++cp_noexcept_operand;
7374 expr = cp_parser_expression (parser);
7375 --cp_noexcept_operand;
7376 --c_inhibit_evaluation_warnings;
7377 --cp_unevaluated_operand;
7379 parser->greater_than_is_operator_p
7380 = saved_greater_than_is_operator_p;
7382 parser->integral_constant_expression_p
7383 = saved_integral_constant_expression_p;
7384 parser->non_integral_constant_expression_p
7385 = saved_non_integral_constant_expression_p;
7387 parser->type_definition_forbidden_message = saved_message;
7389 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7390 return finish_noexcept_expr (expr, tf_warning_or_error);
7393 default:
7394 break;
7398 /* Look for the `:: new' and `:: delete', which also signal the
7399 beginning of a new-expression, or delete-expression,
7400 respectively. If the next token is `::', then it might be one of
7401 these. */
7402 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7404 enum rid keyword;
7406 /* See if the token after the `::' is one of the keywords in
7407 which we're interested. */
7408 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7409 /* If it's `new', we have a new-expression. */
7410 if (keyword == RID_NEW)
7411 return cp_parser_new_expression (parser);
7412 /* Similarly, for `delete'. */
7413 else if (keyword == RID_DELETE)
7414 return cp_parser_delete_expression (parser);
7417 /* Look for a unary operator. */
7418 unary_operator = cp_parser_unary_operator (token);
7419 /* The `++' and `--' operators can be handled similarly, even though
7420 they are not technically unary-operators in the grammar. */
7421 if (unary_operator == ERROR_MARK)
7423 if (token->type == CPP_PLUS_PLUS)
7424 unary_operator = PREINCREMENT_EXPR;
7425 else if (token->type == CPP_MINUS_MINUS)
7426 unary_operator = PREDECREMENT_EXPR;
7427 /* Handle the GNU address-of-label extension. */
7428 else if (cp_parser_allow_gnu_extensions_p (parser)
7429 && token->type == CPP_AND_AND)
7431 tree identifier;
7432 tree expression;
7433 location_t loc = token->location;
7435 /* Consume the '&&' token. */
7436 cp_lexer_consume_token (parser->lexer);
7437 /* Look for the identifier. */
7438 identifier = cp_parser_identifier (parser);
7439 /* Create an expression representing the address. */
7440 expression = finish_label_address_expr (identifier, loc);
7441 if (cp_parser_non_integral_constant_expression (parser,
7442 NIC_ADDR_LABEL))
7443 expression = error_mark_node;
7444 return expression;
7447 if (unary_operator != ERROR_MARK)
7449 tree cast_expression;
7450 tree expression = error_mark_node;
7451 non_integral_constant non_constant_p = NIC_NONE;
7452 location_t loc = token->location;
7453 tsubst_flags_t complain = complain_flags (decltype_p);
7455 /* Consume the operator token. */
7456 token = cp_lexer_consume_token (parser->lexer);
7457 /* Parse the cast-expression. */
7458 cast_expression
7459 = cp_parser_cast_expression (parser,
7460 unary_operator == ADDR_EXPR,
7461 /*cast_p=*/false,
7462 /*decltype*/false,
7463 pidk);
7464 /* Now, build an appropriate representation. */
7465 switch (unary_operator)
7467 case INDIRECT_REF:
7468 non_constant_p = NIC_STAR;
7469 expression = build_x_indirect_ref (loc, cast_expression,
7470 RO_UNARY_STAR,
7471 complain);
7472 break;
7474 case ADDR_EXPR:
7475 non_constant_p = NIC_ADDR;
7476 /* Fall through. */
7477 case BIT_NOT_EXPR:
7478 expression = build_x_unary_op (loc, unary_operator,
7479 cast_expression,
7480 complain);
7481 break;
7483 case PREINCREMENT_EXPR:
7484 case PREDECREMENT_EXPR:
7485 non_constant_p = unary_operator == PREINCREMENT_EXPR
7486 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7487 /* Fall through. */
7488 case UNARY_PLUS_EXPR:
7489 case NEGATE_EXPR:
7490 case TRUTH_NOT_EXPR:
7491 expression = finish_unary_op_expr (loc, unary_operator,
7492 cast_expression, complain);
7493 break;
7495 default:
7496 gcc_unreachable ();
7499 if (non_constant_p != NIC_NONE
7500 && cp_parser_non_integral_constant_expression (parser,
7501 non_constant_p))
7502 expression = error_mark_node;
7504 return expression;
7507 return cp_parser_postfix_expression (parser, address_p, cast_p,
7508 /*member_access_only_p=*/false,
7509 decltype_p,
7510 pidk);
7513 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7514 unary-operator, the corresponding tree code is returned. */
7516 static enum tree_code
7517 cp_parser_unary_operator (cp_token* token)
7519 switch (token->type)
7521 case CPP_MULT:
7522 return INDIRECT_REF;
7524 case CPP_AND:
7525 return ADDR_EXPR;
7527 case CPP_PLUS:
7528 return UNARY_PLUS_EXPR;
7530 case CPP_MINUS:
7531 return NEGATE_EXPR;
7533 case CPP_NOT:
7534 return TRUTH_NOT_EXPR;
7536 case CPP_COMPL:
7537 return BIT_NOT_EXPR;
7539 default:
7540 return ERROR_MARK;
7544 /* Parse a new-expression.
7546 new-expression:
7547 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7548 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7550 Returns a representation of the expression. */
7552 static tree
7553 cp_parser_new_expression (cp_parser* parser)
7555 bool global_scope_p;
7556 vec<tree, va_gc> *placement;
7557 tree type;
7558 vec<tree, va_gc> *initializer;
7559 tree nelts = NULL_TREE;
7560 tree ret;
7562 /* Look for the optional `::' operator. */
7563 global_scope_p
7564 = (cp_parser_global_scope_opt (parser,
7565 /*current_scope_valid_p=*/false)
7566 != NULL_TREE);
7567 /* Look for the `new' operator. */
7568 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7569 /* There's no easy way to tell a new-placement from the
7570 `( type-id )' construct. */
7571 cp_parser_parse_tentatively (parser);
7572 /* Look for a new-placement. */
7573 placement = cp_parser_new_placement (parser);
7574 /* If that didn't work out, there's no new-placement. */
7575 if (!cp_parser_parse_definitely (parser))
7577 if (placement != NULL)
7578 release_tree_vector (placement);
7579 placement = NULL;
7582 /* If the next token is a `(', then we have a parenthesized
7583 type-id. */
7584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7586 cp_token *token;
7587 const char *saved_message = parser->type_definition_forbidden_message;
7589 /* Consume the `('. */
7590 cp_lexer_consume_token (parser->lexer);
7592 /* Parse the type-id. */
7593 parser->type_definition_forbidden_message
7594 = G_("types may not be defined in a new-expression");
7595 type = cp_parser_type_id (parser);
7596 parser->type_definition_forbidden_message = saved_message;
7598 /* Look for the closing `)'. */
7599 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7600 token = cp_lexer_peek_token (parser->lexer);
7601 /* There should not be a direct-new-declarator in this production,
7602 but GCC used to allowed this, so we check and emit a sensible error
7603 message for this case. */
7604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7606 error_at (token->location,
7607 "array bound forbidden after parenthesized type-id");
7608 inform (token->location,
7609 "try removing the parentheses around the type-id");
7610 cp_parser_direct_new_declarator (parser);
7613 /* Otherwise, there must be a new-type-id. */
7614 else
7615 type = cp_parser_new_type_id (parser, &nelts);
7617 /* If the next token is a `(' or '{', then we have a new-initializer. */
7618 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7619 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7620 initializer = cp_parser_new_initializer (parser);
7621 else
7622 initializer = NULL;
7624 /* A new-expression may not appear in an integral constant
7625 expression. */
7626 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7627 ret = error_mark_node;
7628 else
7630 /* Create a representation of the new-expression. */
7631 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7632 tf_warning_or_error);
7635 if (placement != NULL)
7636 release_tree_vector (placement);
7637 if (initializer != NULL)
7638 release_tree_vector (initializer);
7640 return ret;
7643 /* Parse a new-placement.
7645 new-placement:
7646 ( expression-list )
7648 Returns the same representation as for an expression-list. */
7650 static vec<tree, va_gc> *
7651 cp_parser_new_placement (cp_parser* parser)
7653 vec<tree, va_gc> *expression_list;
7655 /* Parse the expression-list. */
7656 expression_list = (cp_parser_parenthesized_expression_list
7657 (parser, non_attr, /*cast_p=*/false,
7658 /*allow_expansion_p=*/true,
7659 /*non_constant_p=*/NULL));
7661 return expression_list;
7664 /* Parse a new-type-id.
7666 new-type-id:
7667 type-specifier-seq new-declarator [opt]
7669 Returns the TYPE allocated. If the new-type-id indicates an array
7670 type, *NELTS is set to the number of elements in the last array
7671 bound; the TYPE will not include the last array bound. */
7673 static tree
7674 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7676 cp_decl_specifier_seq type_specifier_seq;
7677 cp_declarator *new_declarator;
7678 cp_declarator *declarator;
7679 cp_declarator *outer_declarator;
7680 const char *saved_message;
7682 /* The type-specifier sequence must not contain type definitions.
7683 (It cannot contain declarations of new types either, but if they
7684 are not definitions we will catch that because they are not
7685 complete.) */
7686 saved_message = parser->type_definition_forbidden_message;
7687 parser->type_definition_forbidden_message
7688 = G_("types may not be defined in a new-type-id");
7689 /* Parse the type-specifier-seq. */
7690 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7691 /*is_trailing_return=*/false,
7692 &type_specifier_seq);
7693 /* Restore the old message. */
7694 parser->type_definition_forbidden_message = saved_message;
7696 if (type_specifier_seq.type == error_mark_node)
7697 return error_mark_node;
7699 /* Parse the new-declarator. */
7700 new_declarator = cp_parser_new_declarator_opt (parser);
7702 /* Determine the number of elements in the last array dimension, if
7703 any. */
7704 *nelts = NULL_TREE;
7705 /* Skip down to the last array dimension. */
7706 declarator = new_declarator;
7707 outer_declarator = NULL;
7708 while (declarator && (declarator->kind == cdk_pointer
7709 || declarator->kind == cdk_ptrmem))
7711 outer_declarator = declarator;
7712 declarator = declarator->declarator;
7714 while (declarator
7715 && declarator->kind == cdk_array
7716 && declarator->declarator
7717 && declarator->declarator->kind == cdk_array)
7719 outer_declarator = declarator;
7720 declarator = declarator->declarator;
7723 if (declarator && declarator->kind == cdk_array)
7725 *nelts = declarator->u.array.bounds;
7726 if (*nelts == error_mark_node)
7727 *nelts = integer_one_node;
7729 if (outer_declarator)
7730 outer_declarator->declarator = declarator->declarator;
7731 else
7732 new_declarator = NULL;
7735 return groktypename (&type_specifier_seq, new_declarator, false);
7738 /* Parse an (optional) new-declarator.
7740 new-declarator:
7741 ptr-operator new-declarator [opt]
7742 direct-new-declarator
7744 Returns the declarator. */
7746 static cp_declarator *
7747 cp_parser_new_declarator_opt (cp_parser* parser)
7749 enum tree_code code;
7750 tree type, std_attributes = NULL_TREE;
7751 cp_cv_quals cv_quals;
7753 /* We don't know if there's a ptr-operator next, or not. */
7754 cp_parser_parse_tentatively (parser);
7755 /* Look for a ptr-operator. */
7756 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7757 /* If that worked, look for more new-declarators. */
7758 if (cp_parser_parse_definitely (parser))
7760 cp_declarator *declarator;
7762 /* Parse another optional declarator. */
7763 declarator = cp_parser_new_declarator_opt (parser);
7765 declarator = cp_parser_make_indirect_declarator
7766 (code, type, cv_quals, declarator, std_attributes);
7768 return declarator;
7771 /* If the next token is a `[', there is a direct-new-declarator. */
7772 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7773 return cp_parser_direct_new_declarator (parser);
7775 return NULL;
7778 /* Parse a direct-new-declarator.
7780 direct-new-declarator:
7781 [ expression ]
7782 direct-new-declarator [constant-expression]
7786 static cp_declarator *
7787 cp_parser_direct_new_declarator (cp_parser* parser)
7789 cp_declarator *declarator = NULL;
7791 while (true)
7793 tree expression;
7794 cp_token *token;
7796 /* Look for the opening `['. */
7797 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7799 token = cp_lexer_peek_token (parser->lexer);
7800 expression = cp_parser_expression (parser);
7801 /* The standard requires that the expression have integral
7802 type. DR 74 adds enumeration types. We believe that the
7803 real intent is that these expressions be handled like the
7804 expression in a `switch' condition, which also allows
7805 classes with a single conversion to integral or
7806 enumeration type. */
7807 if (!processing_template_decl)
7809 expression
7810 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7811 expression,
7812 /*complain=*/true);
7813 if (!expression)
7815 error_at (token->location,
7816 "expression in new-declarator must have integral "
7817 "or enumeration type");
7818 expression = error_mark_node;
7822 /* Look for the closing `]'. */
7823 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7825 /* Add this bound to the declarator. */
7826 declarator = make_array_declarator (declarator, expression);
7828 /* If the next token is not a `[', then there are no more
7829 bounds. */
7830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7831 break;
7834 return declarator;
7837 /* Parse a new-initializer.
7839 new-initializer:
7840 ( expression-list [opt] )
7841 braced-init-list
7843 Returns a representation of the expression-list. */
7845 static vec<tree, va_gc> *
7846 cp_parser_new_initializer (cp_parser* parser)
7848 vec<tree, va_gc> *expression_list;
7850 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7852 tree t;
7853 bool expr_non_constant_p;
7854 cp_lexer_set_source_position (parser->lexer);
7855 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7856 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7857 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7858 expression_list = make_tree_vector_single (t);
7860 else
7861 expression_list = (cp_parser_parenthesized_expression_list
7862 (parser, non_attr, /*cast_p=*/false,
7863 /*allow_expansion_p=*/true,
7864 /*non_constant_p=*/NULL));
7866 return expression_list;
7869 /* Parse a delete-expression.
7871 delete-expression:
7872 :: [opt] delete cast-expression
7873 :: [opt] delete [ ] cast-expression
7875 Returns a representation of the expression. */
7877 static tree
7878 cp_parser_delete_expression (cp_parser* parser)
7880 bool global_scope_p;
7881 bool array_p;
7882 tree expression;
7884 /* Look for the optional `::' operator. */
7885 global_scope_p
7886 = (cp_parser_global_scope_opt (parser,
7887 /*current_scope_valid_p=*/false)
7888 != NULL_TREE);
7889 /* Look for the `delete' keyword. */
7890 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7891 /* See if the array syntax is in use. */
7892 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7894 /* Consume the `[' token. */
7895 cp_lexer_consume_token (parser->lexer);
7896 /* Look for the `]' token. */
7897 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7898 /* Remember that this is the `[]' construct. */
7899 array_p = true;
7901 else
7902 array_p = false;
7904 /* Parse the cast-expression. */
7905 expression = cp_parser_simple_cast_expression (parser);
7907 /* A delete-expression may not appear in an integral constant
7908 expression. */
7909 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7910 return error_mark_node;
7912 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7913 tf_warning_or_error);
7916 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7917 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7918 0 otherwise. */
7920 static int
7921 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7923 cp_token *token = cp_lexer_peek_token (parser->lexer);
7924 switch (token->type)
7926 case CPP_COMMA:
7927 case CPP_SEMICOLON:
7928 case CPP_QUERY:
7929 case CPP_COLON:
7930 case CPP_CLOSE_SQUARE:
7931 case CPP_CLOSE_PAREN:
7932 case CPP_CLOSE_BRACE:
7933 case CPP_OPEN_BRACE:
7934 case CPP_DOT:
7935 case CPP_DOT_STAR:
7936 case CPP_DEREF:
7937 case CPP_DEREF_STAR:
7938 case CPP_DIV:
7939 case CPP_MOD:
7940 case CPP_LSHIFT:
7941 case CPP_RSHIFT:
7942 case CPP_LESS:
7943 case CPP_GREATER:
7944 case CPP_LESS_EQ:
7945 case CPP_GREATER_EQ:
7946 case CPP_EQ_EQ:
7947 case CPP_NOT_EQ:
7948 case CPP_EQ:
7949 case CPP_MULT_EQ:
7950 case CPP_DIV_EQ:
7951 case CPP_MOD_EQ:
7952 case CPP_PLUS_EQ:
7953 case CPP_MINUS_EQ:
7954 case CPP_RSHIFT_EQ:
7955 case CPP_LSHIFT_EQ:
7956 case CPP_AND_EQ:
7957 case CPP_XOR_EQ:
7958 case CPP_OR_EQ:
7959 case CPP_XOR:
7960 case CPP_OR:
7961 case CPP_OR_OR:
7962 case CPP_EOF:
7963 case CPP_ELLIPSIS:
7964 return 0;
7966 case CPP_OPEN_PAREN:
7967 /* In ((type ()) () the last () isn't a valid cast-expression,
7968 so the whole must be parsed as postfix-expression. */
7969 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7970 != CPP_CLOSE_PAREN;
7972 case CPP_OPEN_SQUARE:
7973 /* '[' may start a primary-expression in obj-c++ and in C++11,
7974 as a lambda-expression, eg, '(void)[]{}'. */
7975 if (cxx_dialect >= cxx11)
7976 return -1;
7977 return c_dialect_objc ();
7979 case CPP_PLUS_PLUS:
7980 case CPP_MINUS_MINUS:
7981 /* '++' and '--' may or may not start a cast-expression:
7983 struct T { void operator++(int); };
7984 void f() { (T())++; }
7988 int a;
7989 (int)++a; */
7990 return -1;
7992 default:
7993 return 1;
7997 /* Parse a cast-expression.
7999 cast-expression:
8000 unary-expression
8001 ( type-id ) cast-expression
8003 ADDRESS_P is true iff the unary-expression is appearing as the
8004 operand of the `&' operator. CAST_P is true if this expression is
8005 the target of a cast.
8007 Returns a representation of the expression. */
8009 static tree
8010 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8011 bool decltype_p, cp_id_kind * pidk)
8013 /* If it's a `(', then we might be looking at a cast. */
8014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8016 tree type = NULL_TREE;
8017 tree expr = NULL_TREE;
8018 int cast_expression = 0;
8019 const char *saved_message;
8021 /* There's no way to know yet whether or not this is a cast.
8022 For example, `(int (3))' is a unary-expression, while `(int)
8023 3' is a cast. So, we resort to parsing tentatively. */
8024 cp_parser_parse_tentatively (parser);
8025 /* Types may not be defined in a cast. */
8026 saved_message = parser->type_definition_forbidden_message;
8027 parser->type_definition_forbidden_message
8028 = G_("types may not be defined in casts");
8029 /* Consume the `('. */
8030 cp_lexer_consume_token (parser->lexer);
8031 /* A very tricky bit is that `(struct S) { 3 }' is a
8032 compound-literal (which we permit in C++ as an extension).
8033 But, that construct is not a cast-expression -- it is a
8034 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8035 is legal; if the compound-literal were a cast-expression,
8036 you'd need an extra set of parentheses.) But, if we parse
8037 the type-id, and it happens to be a class-specifier, then we
8038 will commit to the parse at that point, because we cannot
8039 undo the action that is done when creating a new class. So,
8040 then we cannot back up and do a postfix-expression.
8042 Another tricky case is the following (c++/29234):
8044 struct S { void operator () (); };
8046 void foo ()
8048 ( S()() );
8051 As a type-id we parse the parenthesized S()() as a function
8052 returning a function, groktypename complains and we cannot
8053 back up in this case either.
8055 Therefore, we scan ahead to the closing `)', and check to see
8056 if the tokens after the `)' can start a cast-expression. Otherwise
8057 we are dealing with an unary-expression, a postfix-expression
8058 or something else.
8060 Yet another tricky case, in C++11, is the following (c++/54891):
8062 (void)[]{};
8064 The issue is that usually, besides the case of lambda-expressions,
8065 the parenthesized type-id cannot be followed by '[', and, eg, we
8066 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8067 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8068 we don't commit, we try a cast-expression, then an unary-expression.
8070 Save tokens so that we can put them back. */
8071 cp_lexer_save_tokens (parser->lexer);
8073 /* We may be looking at a cast-expression. */
8074 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8075 /*consume_paren=*/true))
8076 cast_expression
8077 = cp_parser_tokens_start_cast_expression (parser);
8079 /* Roll back the tokens we skipped. */
8080 cp_lexer_rollback_tokens (parser->lexer);
8081 /* If we aren't looking at a cast-expression, simulate an error so
8082 that the call to cp_parser_error_occurred below returns true. */
8083 if (!cast_expression)
8084 cp_parser_simulate_error (parser);
8085 else
8087 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8088 parser->in_type_id_in_expr_p = true;
8089 /* Look for the type-id. */
8090 type = cp_parser_type_id (parser);
8091 /* Look for the closing `)'. */
8092 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8093 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8096 /* Restore the saved message. */
8097 parser->type_definition_forbidden_message = saved_message;
8099 /* At this point this can only be either a cast or a
8100 parenthesized ctor such as `(T ())' that looks like a cast to
8101 function returning T. */
8102 if (!cp_parser_error_occurred (parser))
8104 /* Only commit if the cast-expression doesn't start with
8105 '++', '--', or '[' in C++11. */
8106 if (cast_expression > 0)
8107 cp_parser_commit_to_topmost_tentative_parse (parser);
8109 expr = cp_parser_cast_expression (parser,
8110 /*address_p=*/false,
8111 /*cast_p=*/true,
8112 /*decltype_p=*/false,
8113 pidk);
8115 if (cp_parser_parse_definitely (parser))
8117 /* Warn about old-style casts, if so requested. */
8118 if (warn_old_style_cast
8119 && !in_system_header_at (input_location)
8120 && !VOID_TYPE_P (type)
8121 && current_lang_name != lang_name_c)
8122 warning (OPT_Wold_style_cast, "use of old-style cast");
8124 /* Only type conversions to integral or enumeration types
8125 can be used in constant-expressions. */
8126 if (!cast_valid_in_integral_constant_expression_p (type)
8127 && cp_parser_non_integral_constant_expression (parser,
8128 NIC_CAST))
8129 return error_mark_node;
8131 /* Perform the cast. */
8132 expr = build_c_cast (input_location, type, expr);
8133 return expr;
8136 else
8137 cp_parser_abort_tentative_parse (parser);
8140 /* If we get here, then it's not a cast, so it must be a
8141 unary-expression. */
8142 return cp_parser_unary_expression (parser, pidk, address_p,
8143 cast_p, decltype_p);
8146 /* Parse a binary expression of the general form:
8148 pm-expression:
8149 cast-expression
8150 pm-expression .* cast-expression
8151 pm-expression ->* cast-expression
8153 multiplicative-expression:
8154 pm-expression
8155 multiplicative-expression * pm-expression
8156 multiplicative-expression / pm-expression
8157 multiplicative-expression % pm-expression
8159 additive-expression:
8160 multiplicative-expression
8161 additive-expression + multiplicative-expression
8162 additive-expression - multiplicative-expression
8164 shift-expression:
8165 additive-expression
8166 shift-expression << additive-expression
8167 shift-expression >> additive-expression
8169 relational-expression:
8170 shift-expression
8171 relational-expression < shift-expression
8172 relational-expression > shift-expression
8173 relational-expression <= shift-expression
8174 relational-expression >= shift-expression
8176 GNU Extension:
8178 relational-expression:
8179 relational-expression <? shift-expression
8180 relational-expression >? shift-expression
8182 equality-expression:
8183 relational-expression
8184 equality-expression == relational-expression
8185 equality-expression != relational-expression
8187 and-expression:
8188 equality-expression
8189 and-expression & equality-expression
8191 exclusive-or-expression:
8192 and-expression
8193 exclusive-or-expression ^ and-expression
8195 inclusive-or-expression:
8196 exclusive-or-expression
8197 inclusive-or-expression | exclusive-or-expression
8199 logical-and-expression:
8200 inclusive-or-expression
8201 logical-and-expression && inclusive-or-expression
8203 logical-or-expression:
8204 logical-and-expression
8205 logical-or-expression || logical-and-expression
8207 All these are implemented with a single function like:
8209 binary-expression:
8210 simple-cast-expression
8211 binary-expression <token> binary-expression
8213 CAST_P is true if this expression is the target of a cast.
8215 The binops_by_token map is used to get the tree codes for each <token> type.
8216 binary-expressions are associated according to a precedence table. */
8218 #define TOKEN_PRECEDENCE(token) \
8219 (((token->type == CPP_GREATER \
8220 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8221 && !parser->greater_than_is_operator_p) \
8222 ? PREC_NOT_OPERATOR \
8223 : binops_by_token[token->type].prec)
8225 static tree
8226 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8227 bool no_toplevel_fold_p,
8228 bool decltype_p,
8229 enum cp_parser_prec prec,
8230 cp_id_kind * pidk)
8232 cp_parser_expression_stack stack;
8233 cp_parser_expression_stack_entry *sp = &stack[0];
8234 cp_parser_expression_stack_entry current;
8235 tree rhs;
8236 cp_token *token;
8237 enum tree_code rhs_type;
8238 enum cp_parser_prec new_prec, lookahead_prec;
8239 tree overload;
8241 /* Parse the first expression. */
8242 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8243 ? TRUTH_NOT_EXPR : ERROR_MARK);
8244 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8245 cast_p, decltype_p, pidk);
8246 current.prec = prec;
8248 if (cp_parser_error_occurred (parser))
8249 return error_mark_node;
8251 for (;;)
8253 /* Get an operator token. */
8254 token = cp_lexer_peek_token (parser->lexer);
8256 if (warn_cxx0x_compat
8257 && token->type == CPP_RSHIFT
8258 && !parser->greater_than_is_operator_p)
8260 if (warning_at (token->location, OPT_Wc__0x_compat,
8261 "%<>>%> operator is treated"
8262 " as two right angle brackets in C++11"))
8263 inform (token->location,
8264 "suggest parentheses around %<>>%> expression");
8267 new_prec = TOKEN_PRECEDENCE (token);
8269 /* Popping an entry off the stack means we completed a subexpression:
8270 - either we found a token which is not an operator (`>' where it is not
8271 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8272 will happen repeatedly;
8273 - or, we found an operator which has lower priority. This is the case
8274 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8275 parsing `3 * 4'. */
8276 if (new_prec <= current.prec)
8278 if (sp == stack)
8279 break;
8280 else
8281 goto pop;
8284 get_rhs:
8285 current.tree_type = binops_by_token[token->type].tree_type;
8286 current.loc = token->location;
8288 /* We used the operator token. */
8289 cp_lexer_consume_token (parser->lexer);
8291 /* For "false && x" or "true || x", x will never be executed;
8292 disable warnings while evaluating it. */
8293 if (current.tree_type == TRUTH_ANDIF_EXPR)
8294 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8295 else if (current.tree_type == TRUTH_ORIF_EXPR)
8296 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8298 /* Extract another operand. It may be the RHS of this expression
8299 or the LHS of a new, higher priority expression. */
8300 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8301 ? TRUTH_NOT_EXPR : ERROR_MARK);
8302 rhs = cp_parser_simple_cast_expression (parser);
8304 /* Get another operator token. Look up its precedence to avoid
8305 building a useless (immediately popped) stack entry for common
8306 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8307 token = cp_lexer_peek_token (parser->lexer);
8308 lookahead_prec = TOKEN_PRECEDENCE (token);
8309 if (lookahead_prec > new_prec)
8311 /* ... and prepare to parse the RHS of the new, higher priority
8312 expression. Since precedence levels on the stack are
8313 monotonically increasing, we do not have to care about
8314 stack overflows. */
8315 *sp = current;
8316 ++sp;
8317 current.lhs = rhs;
8318 current.lhs_type = rhs_type;
8319 current.prec = new_prec;
8320 new_prec = lookahead_prec;
8321 goto get_rhs;
8323 pop:
8324 lookahead_prec = new_prec;
8325 /* If the stack is not empty, we have parsed into LHS the right side
8326 (`4' in the example above) of an expression we had suspended.
8327 We can use the information on the stack to recover the LHS (`3')
8328 from the stack together with the tree code (`MULT_EXPR'), and
8329 the precedence of the higher level subexpression
8330 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8331 which will be used to actually build the additive expression. */
8332 rhs = current.lhs;
8333 rhs_type = current.lhs_type;
8334 --sp;
8335 current = *sp;
8338 /* Undo the disabling of warnings done above. */
8339 if (current.tree_type == TRUTH_ANDIF_EXPR)
8340 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8341 else if (current.tree_type == TRUTH_ORIF_EXPR)
8342 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8344 if (warn_logical_not_paren
8345 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8346 && current.lhs_type == TRUTH_NOT_EXPR
8347 /* Avoid warning for !!x == y. */
8348 && (TREE_CODE (current.lhs) != NE_EXPR
8349 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8350 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8351 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8352 /* Avoid warning for !b == y where b is boolean. */
8353 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8354 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8355 != BOOLEAN_TYPE))))
8356 /* Avoid warning for !!b == y where b is boolean. */
8357 && (!DECL_P (current.lhs)
8358 || TREE_TYPE (current.lhs) == NULL_TREE
8359 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8360 warn_logical_not_parentheses (current.loc, current.tree_type,
8361 maybe_constant_value (rhs));
8363 overload = NULL;
8364 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8365 ERROR_MARK for everything that is not a binary expression.
8366 This makes warn_about_parentheses miss some warnings that
8367 involve unary operators. For unary expressions we should
8368 pass the correct tree_code unless the unary expression was
8369 surrounded by parentheses.
8371 if (no_toplevel_fold_p
8372 && lookahead_prec <= current.prec
8373 && sp == stack)
8374 current.lhs = build2 (current.tree_type,
8375 TREE_CODE_CLASS (current.tree_type)
8376 == tcc_comparison
8377 ? boolean_type_node : TREE_TYPE (current.lhs),
8378 current.lhs, rhs);
8379 else
8380 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8381 current.lhs, current.lhs_type,
8382 rhs, rhs_type, &overload,
8383 complain_flags (decltype_p));
8384 current.lhs_type = current.tree_type;
8385 if (EXPR_P (current.lhs))
8386 SET_EXPR_LOCATION (current.lhs, current.loc);
8388 /* If the binary operator required the use of an overloaded operator,
8389 then this expression cannot be an integral constant-expression.
8390 An overloaded operator can be used even if both operands are
8391 otherwise permissible in an integral constant-expression if at
8392 least one of the operands is of enumeration type. */
8394 if (overload
8395 && cp_parser_non_integral_constant_expression (parser,
8396 NIC_OVERLOADED))
8397 return error_mark_node;
8400 return current.lhs;
8403 static tree
8404 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8405 bool no_toplevel_fold_p,
8406 enum cp_parser_prec prec,
8407 cp_id_kind * pidk)
8409 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8410 /*decltype*/false, prec, pidk);
8413 /* Parse the `? expression : assignment-expression' part of a
8414 conditional-expression. The LOGICAL_OR_EXPR is the
8415 logical-or-expression that started the conditional-expression.
8416 Returns a representation of the entire conditional-expression.
8418 This routine is used by cp_parser_assignment_expression.
8420 ? expression : assignment-expression
8422 GNU Extensions:
8424 ? : assignment-expression */
8426 static tree
8427 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8429 tree expr;
8430 tree assignment_expr;
8431 struct cp_token *token;
8432 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8434 /* Consume the `?' token. */
8435 cp_lexer_consume_token (parser->lexer);
8436 token = cp_lexer_peek_token (parser->lexer);
8437 if (cp_parser_allow_gnu_extensions_p (parser)
8438 && token->type == CPP_COLON)
8440 pedwarn (token->location, OPT_Wpedantic,
8441 "ISO C++ does not allow ?: with omitted middle operand");
8442 /* Implicit true clause. */
8443 expr = NULL_TREE;
8444 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8445 warn_for_omitted_condop (token->location, logical_or_expr);
8447 else
8449 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8450 parser->colon_corrects_to_scope_p = false;
8451 /* Parse the expression. */
8452 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8453 expr = cp_parser_expression (parser);
8454 c_inhibit_evaluation_warnings +=
8455 ((logical_or_expr == truthvalue_true_node)
8456 - (logical_or_expr == truthvalue_false_node));
8457 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8460 /* The next token should be a `:'. */
8461 cp_parser_require (parser, CPP_COLON, RT_COLON);
8462 /* Parse the assignment-expression. */
8463 assignment_expr = cp_parser_assignment_expression (parser);
8464 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8466 /* Build the conditional-expression. */
8467 return build_x_conditional_expr (loc, logical_or_expr,
8468 expr,
8469 assignment_expr,
8470 tf_warning_or_error);
8473 /* Parse an assignment-expression.
8475 assignment-expression:
8476 conditional-expression
8477 logical-or-expression assignment-operator assignment_expression
8478 throw-expression
8480 CAST_P is true if this expression is the target of a cast.
8481 DECLTYPE_P is true if this expression is the operand of decltype.
8483 Returns a representation for the expression. */
8485 static tree
8486 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8487 bool cast_p, bool decltype_p)
8489 tree expr;
8491 /* If the next token is the `throw' keyword, then we're looking at
8492 a throw-expression. */
8493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8494 expr = cp_parser_throw_expression (parser);
8495 /* Otherwise, it must be that we are looking at a
8496 logical-or-expression. */
8497 else
8499 /* Parse the binary expressions (logical-or-expression). */
8500 expr = cp_parser_binary_expression (parser, cast_p, false,
8501 decltype_p,
8502 PREC_NOT_OPERATOR, pidk);
8503 /* If the next token is a `?' then we're actually looking at a
8504 conditional-expression. */
8505 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8506 return cp_parser_question_colon_clause (parser, expr);
8507 else
8509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8511 /* If it's an assignment-operator, we're using the second
8512 production. */
8513 enum tree_code assignment_operator
8514 = cp_parser_assignment_operator_opt (parser);
8515 if (assignment_operator != ERROR_MARK)
8517 bool non_constant_p;
8518 location_t saved_input_location;
8520 /* Parse the right-hand side of the assignment. */
8521 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8523 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8524 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8526 /* An assignment may not appear in a
8527 constant-expression. */
8528 if (cp_parser_non_integral_constant_expression (parser,
8529 NIC_ASSIGNMENT))
8530 return error_mark_node;
8531 /* Build the assignment expression. Its default
8532 location is the location of the '=' token. */
8533 saved_input_location = input_location;
8534 input_location = loc;
8535 expr = build_x_modify_expr (loc, expr,
8536 assignment_operator,
8537 rhs,
8538 complain_flags (decltype_p));
8539 input_location = saved_input_location;
8544 return expr;
8547 /* Parse an (optional) assignment-operator.
8549 assignment-operator: one of
8550 = *= /= %= += -= >>= <<= &= ^= |=
8552 GNU Extension:
8554 assignment-operator: one of
8555 <?= >?=
8557 If the next token is an assignment operator, the corresponding tree
8558 code is returned, and the token is consumed. For example, for
8559 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8560 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8561 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8562 operator, ERROR_MARK is returned. */
8564 static enum tree_code
8565 cp_parser_assignment_operator_opt (cp_parser* parser)
8567 enum tree_code op;
8568 cp_token *token;
8570 /* Peek at the next token. */
8571 token = cp_lexer_peek_token (parser->lexer);
8573 switch (token->type)
8575 case CPP_EQ:
8576 op = NOP_EXPR;
8577 break;
8579 case CPP_MULT_EQ:
8580 op = MULT_EXPR;
8581 break;
8583 case CPP_DIV_EQ:
8584 op = TRUNC_DIV_EXPR;
8585 break;
8587 case CPP_MOD_EQ:
8588 op = TRUNC_MOD_EXPR;
8589 break;
8591 case CPP_PLUS_EQ:
8592 op = PLUS_EXPR;
8593 break;
8595 case CPP_MINUS_EQ:
8596 op = MINUS_EXPR;
8597 break;
8599 case CPP_RSHIFT_EQ:
8600 op = RSHIFT_EXPR;
8601 break;
8603 case CPP_LSHIFT_EQ:
8604 op = LSHIFT_EXPR;
8605 break;
8607 case CPP_AND_EQ:
8608 op = BIT_AND_EXPR;
8609 break;
8611 case CPP_XOR_EQ:
8612 op = BIT_XOR_EXPR;
8613 break;
8615 case CPP_OR_EQ:
8616 op = BIT_IOR_EXPR;
8617 break;
8619 default:
8620 /* Nothing else is an assignment operator. */
8621 op = ERROR_MARK;
8624 /* If it was an assignment operator, consume it. */
8625 if (op != ERROR_MARK)
8626 cp_lexer_consume_token (parser->lexer);
8628 return op;
8631 /* Parse an expression.
8633 expression:
8634 assignment-expression
8635 expression , assignment-expression
8637 CAST_P is true if this expression is the target of a cast.
8638 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8639 except possibly parenthesized or on the RHS of a comma (N3276).
8641 Returns a representation of the expression. */
8643 static tree
8644 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8645 bool cast_p, bool decltype_p)
8647 tree expression = NULL_TREE;
8648 location_t loc = UNKNOWN_LOCATION;
8650 while (true)
8652 tree assignment_expression;
8654 /* Parse the next assignment-expression. */
8655 assignment_expression
8656 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8658 /* We don't create a temporary for a call that is the immediate operand
8659 of decltype or on the RHS of a comma. But when we see a comma, we
8660 need to create a temporary for a call on the LHS. */
8661 if (decltype_p && !processing_template_decl
8662 && TREE_CODE (assignment_expression) == CALL_EXPR
8663 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8664 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8665 assignment_expression
8666 = build_cplus_new (TREE_TYPE (assignment_expression),
8667 assignment_expression, tf_warning_or_error);
8669 /* If this is the first assignment-expression, we can just
8670 save it away. */
8671 if (!expression)
8672 expression = assignment_expression;
8673 else
8674 expression = build_x_compound_expr (loc, expression,
8675 assignment_expression,
8676 complain_flags (decltype_p));
8677 /* If the next token is not a comma, then we are done with the
8678 expression. */
8679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8680 break;
8681 /* Consume the `,'. */
8682 loc = cp_lexer_peek_token (parser->lexer)->location;
8683 cp_lexer_consume_token (parser->lexer);
8684 /* A comma operator cannot appear in a constant-expression. */
8685 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8686 expression = error_mark_node;
8689 return expression;
8692 /* Parse a constant-expression.
8694 constant-expression:
8695 conditional-expression
8697 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8698 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8699 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8700 is false, NON_CONSTANT_P should be NULL. */
8702 static tree
8703 cp_parser_constant_expression (cp_parser* parser,
8704 bool allow_non_constant_p,
8705 bool *non_constant_p)
8707 bool saved_integral_constant_expression_p;
8708 bool saved_allow_non_integral_constant_expression_p;
8709 bool saved_non_integral_constant_expression_p;
8710 tree expression;
8712 /* It might seem that we could simply parse the
8713 conditional-expression, and then check to see if it were
8714 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8715 one that the compiler can figure out is constant, possibly after
8716 doing some simplifications or optimizations. The standard has a
8717 precise definition of constant-expression, and we must honor
8718 that, even though it is somewhat more restrictive.
8720 For example:
8722 int i[(2, 3)];
8724 is not a legal declaration, because `(2, 3)' is not a
8725 constant-expression. The `,' operator is forbidden in a
8726 constant-expression. However, GCC's constant-folding machinery
8727 will fold this operation to an INTEGER_CST for `3'. */
8729 /* Save the old settings. */
8730 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8731 saved_allow_non_integral_constant_expression_p
8732 = parser->allow_non_integral_constant_expression_p;
8733 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8734 /* We are now parsing a constant-expression. */
8735 parser->integral_constant_expression_p = true;
8736 parser->allow_non_integral_constant_expression_p
8737 = (allow_non_constant_p || cxx_dialect >= cxx11);
8738 parser->non_integral_constant_expression_p = false;
8739 /* Although the grammar says "conditional-expression", we parse an
8740 "assignment-expression", which also permits "throw-expression"
8741 and the use of assignment operators. In the case that
8742 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8743 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8744 actually essential that we look for an assignment-expression.
8745 For example, cp_parser_initializer_clauses uses this function to
8746 determine whether a particular assignment-expression is in fact
8747 constant. */
8748 expression = cp_parser_assignment_expression (parser);
8749 /* Restore the old settings. */
8750 parser->integral_constant_expression_p
8751 = saved_integral_constant_expression_p;
8752 parser->allow_non_integral_constant_expression_p
8753 = saved_allow_non_integral_constant_expression_p;
8754 if (cxx_dialect >= cxx11)
8756 /* Require an rvalue constant expression here; that's what our
8757 callers expect. Reference constant expressions are handled
8758 separately in e.g. cp_parser_template_argument. */
8759 bool is_const = potential_rvalue_constant_expression (expression);
8760 parser->non_integral_constant_expression_p = !is_const;
8761 if (!is_const && !allow_non_constant_p)
8762 require_potential_rvalue_constant_expression (expression);
8764 if (allow_non_constant_p)
8765 *non_constant_p = parser->non_integral_constant_expression_p;
8766 parser->non_integral_constant_expression_p
8767 = saved_non_integral_constant_expression_p;
8769 return expression;
8772 /* Parse __builtin_offsetof.
8774 offsetof-expression:
8775 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8777 offsetof-member-designator:
8778 id-expression
8779 | offsetof-member-designator "." id-expression
8780 | offsetof-member-designator "[" expression "]"
8781 | offsetof-member-designator "->" id-expression */
8783 static tree
8784 cp_parser_builtin_offsetof (cp_parser *parser)
8786 int save_ice_p, save_non_ice_p;
8787 tree type, expr;
8788 cp_id_kind dummy;
8789 cp_token *token;
8791 /* We're about to accept non-integral-constant things, but will
8792 definitely yield an integral constant expression. Save and
8793 restore these values around our local parsing. */
8794 save_ice_p = parser->integral_constant_expression_p;
8795 save_non_ice_p = parser->non_integral_constant_expression_p;
8797 /* Consume the "__builtin_offsetof" token. */
8798 cp_lexer_consume_token (parser->lexer);
8799 /* Consume the opening `('. */
8800 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8801 /* Parse the type-id. */
8802 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8803 type = cp_parser_type_id (parser);
8804 /* Look for the `,'. */
8805 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8806 token = cp_lexer_peek_token (parser->lexer);
8808 /* Build the (type *)null that begins the traditional offsetof macro. */
8809 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8810 tf_warning_or_error);
8812 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8813 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8814 true, &dummy, token->location);
8815 while (true)
8817 token = cp_lexer_peek_token (parser->lexer);
8818 switch (token->type)
8820 case CPP_OPEN_SQUARE:
8821 /* offsetof-member-designator "[" expression "]" */
8822 expr = cp_parser_postfix_open_square_expression (parser, expr,
8823 true, false);
8824 break;
8826 case CPP_DEREF:
8827 /* offsetof-member-designator "->" identifier */
8828 expr = grok_array_decl (token->location, expr,
8829 integer_zero_node, false);
8830 /* FALLTHRU */
8832 case CPP_DOT:
8833 /* offsetof-member-designator "." identifier */
8834 cp_lexer_consume_token (parser->lexer);
8835 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8836 expr, true, &dummy,
8837 token->location);
8838 break;
8840 case CPP_CLOSE_PAREN:
8841 /* Consume the ")" token. */
8842 cp_lexer_consume_token (parser->lexer);
8843 goto success;
8845 default:
8846 /* Error. We know the following require will fail, but
8847 that gives the proper error message. */
8848 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8849 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8850 expr = error_mark_node;
8851 goto failure;
8855 success:
8856 expr = finish_offsetof (expr, loc);
8858 failure:
8859 parser->integral_constant_expression_p = save_ice_p;
8860 parser->non_integral_constant_expression_p = save_non_ice_p;
8862 return expr;
8865 /* Parse a trait expression.
8867 Returns a representation of the expression, the underlying type
8868 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8870 static tree
8871 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8873 cp_trait_kind kind;
8874 tree type1, type2 = NULL_TREE;
8875 bool binary = false;
8876 bool variadic = false;
8878 switch (keyword)
8880 case RID_HAS_NOTHROW_ASSIGN:
8881 kind = CPTK_HAS_NOTHROW_ASSIGN;
8882 break;
8883 case RID_HAS_NOTHROW_CONSTRUCTOR:
8884 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8885 break;
8886 case RID_HAS_NOTHROW_COPY:
8887 kind = CPTK_HAS_NOTHROW_COPY;
8888 break;
8889 case RID_HAS_TRIVIAL_ASSIGN:
8890 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8891 break;
8892 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8893 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8894 break;
8895 case RID_HAS_TRIVIAL_COPY:
8896 kind = CPTK_HAS_TRIVIAL_COPY;
8897 break;
8898 case RID_HAS_TRIVIAL_DESTRUCTOR:
8899 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8900 break;
8901 case RID_HAS_VIRTUAL_DESTRUCTOR:
8902 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8903 break;
8904 case RID_IS_ABSTRACT:
8905 kind = CPTK_IS_ABSTRACT;
8906 break;
8907 case RID_IS_BASE_OF:
8908 kind = CPTK_IS_BASE_OF;
8909 binary = true;
8910 break;
8911 case RID_IS_CLASS:
8912 kind = CPTK_IS_CLASS;
8913 break;
8914 case RID_IS_EMPTY:
8915 kind = CPTK_IS_EMPTY;
8916 break;
8917 case RID_IS_ENUM:
8918 kind = CPTK_IS_ENUM;
8919 break;
8920 case RID_IS_FINAL:
8921 kind = CPTK_IS_FINAL;
8922 break;
8923 case RID_IS_LITERAL_TYPE:
8924 kind = CPTK_IS_LITERAL_TYPE;
8925 break;
8926 case RID_IS_POD:
8927 kind = CPTK_IS_POD;
8928 break;
8929 case RID_IS_POLYMORPHIC:
8930 kind = CPTK_IS_POLYMORPHIC;
8931 break;
8932 case RID_IS_SAME_AS:
8933 kind = CPTK_IS_SAME_AS;
8934 binary = true;
8935 break;
8936 case RID_IS_STD_LAYOUT:
8937 kind = CPTK_IS_STD_LAYOUT;
8938 break;
8939 case RID_IS_TRIVIAL:
8940 kind = CPTK_IS_TRIVIAL;
8941 break;
8942 case RID_IS_TRIVIALLY_ASSIGNABLE:
8943 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8944 binary = true;
8945 break;
8946 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8947 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8948 variadic = true;
8949 break;
8950 case RID_IS_TRIVIALLY_COPYABLE:
8951 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8952 break;
8953 case RID_IS_UNION:
8954 kind = CPTK_IS_UNION;
8955 break;
8956 case RID_UNDERLYING_TYPE:
8957 kind = CPTK_UNDERLYING_TYPE;
8958 break;
8959 case RID_BASES:
8960 kind = CPTK_BASES;
8961 break;
8962 case RID_DIRECT_BASES:
8963 kind = CPTK_DIRECT_BASES;
8964 break;
8965 default:
8966 gcc_unreachable ();
8969 /* Consume the token. */
8970 cp_lexer_consume_token (parser->lexer);
8972 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8974 type1 = cp_parser_type_id (parser);
8976 if (type1 == error_mark_node)
8977 return error_mark_node;
8979 if (binary)
8981 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8983 type2 = cp_parser_type_id (parser);
8985 if (type2 == error_mark_node)
8986 return error_mark_node;
8988 else if (variadic)
8990 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8992 cp_lexer_consume_token (parser->lexer);
8993 tree elt = cp_parser_type_id (parser);
8994 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8996 cp_lexer_consume_token (parser->lexer);
8997 elt = make_pack_expansion (elt);
8999 if (elt == error_mark_node)
9000 return error_mark_node;
9001 type2 = tree_cons (NULL_TREE, elt, type2);
9005 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9007 /* Complete the trait expression, which may mean either processing
9008 the trait expr now or saving it for template instantiation. */
9009 switch(kind)
9011 case CPTK_UNDERLYING_TYPE:
9012 return finish_underlying_type (type1);
9013 case CPTK_BASES:
9014 return finish_bases (type1, false);
9015 case CPTK_DIRECT_BASES:
9016 return finish_bases (type1, true);
9017 default:
9018 return finish_trait_expr (kind, type1, type2);
9022 /* Lambdas that appear in variable initializer or default argument scope
9023 get that in their mangling, so we need to record it. We might as well
9024 use the count for function and namespace scopes as well. */
9025 static GTY(()) tree lambda_scope;
9026 static GTY(()) int lambda_count;
9027 typedef struct GTY(()) tree_int
9029 tree t;
9030 int i;
9031 } tree_int;
9032 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9034 static void
9035 start_lambda_scope (tree decl)
9037 tree_int ti;
9038 gcc_assert (decl);
9039 /* Once we're inside a function, we ignore other scopes and just push
9040 the function again so that popping works properly. */
9041 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9042 decl = current_function_decl;
9043 ti.t = lambda_scope;
9044 ti.i = lambda_count;
9045 vec_safe_push (lambda_scope_stack, ti);
9046 if (lambda_scope != decl)
9048 /* Don't reset the count if we're still in the same function. */
9049 lambda_scope = decl;
9050 lambda_count = 0;
9054 static void
9055 record_lambda_scope (tree lambda)
9057 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9058 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9061 static void
9062 finish_lambda_scope (void)
9064 tree_int *p = &lambda_scope_stack->last ();
9065 if (lambda_scope != p->t)
9067 lambda_scope = p->t;
9068 lambda_count = p->i;
9070 lambda_scope_stack->pop ();
9073 /* Parse a lambda expression.
9075 lambda-expression:
9076 lambda-introducer lambda-declarator [opt] compound-statement
9078 Returns a representation of the expression. */
9080 static tree
9081 cp_parser_lambda_expression (cp_parser* parser)
9083 tree lambda_expr = build_lambda_expr ();
9084 tree type;
9085 bool ok = true;
9086 cp_token *token = cp_lexer_peek_token (parser->lexer);
9087 cp_token_position start = 0;
9089 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9091 if (cp_unevaluated_operand)
9093 if (!token->error_reported)
9095 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9096 "lambda-expression in unevaluated context");
9097 token->error_reported = true;
9099 ok = false;
9101 else if (parser->in_template_argument_list_p)
9103 if (!token->error_reported)
9105 error_at (token->location, "lambda-expression in template-argument");
9106 token->error_reported = true;
9108 ok = false;
9111 /* We may be in the middle of deferred access check. Disable
9112 it now. */
9113 push_deferring_access_checks (dk_no_deferred);
9115 cp_parser_lambda_introducer (parser, lambda_expr);
9117 type = begin_lambda_type (lambda_expr);
9118 if (type == error_mark_node)
9119 return error_mark_node;
9121 record_lambda_scope (lambda_expr);
9123 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9124 determine_visibility (TYPE_NAME (type));
9126 /* Now that we've started the type, add the capture fields for any
9127 explicit captures. */
9128 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9131 /* Inside the class, surrounding template-parameter-lists do not apply. */
9132 unsigned int saved_num_template_parameter_lists
9133 = parser->num_template_parameter_lists;
9134 unsigned char in_statement = parser->in_statement;
9135 bool in_switch_statement_p = parser->in_switch_statement_p;
9136 bool fully_implicit_function_template_p
9137 = parser->fully_implicit_function_template_p;
9138 tree implicit_template_parms = parser->implicit_template_parms;
9139 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9140 bool auto_is_implicit_function_template_parm_p
9141 = parser->auto_is_implicit_function_template_parm_p;
9143 parser->num_template_parameter_lists = 0;
9144 parser->in_statement = 0;
9145 parser->in_switch_statement_p = false;
9146 parser->fully_implicit_function_template_p = false;
9147 parser->implicit_template_parms = 0;
9148 parser->implicit_template_scope = 0;
9149 parser->auto_is_implicit_function_template_parm_p = false;
9151 /* By virtue of defining a local class, a lambda expression has access to
9152 the private variables of enclosing classes. */
9154 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9156 if (ok)
9158 if (!cp_parser_error_occurred (parser)
9159 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9160 && cp_parser_start_tentative_firewall (parser))
9161 start = token;
9162 cp_parser_lambda_body (parser, lambda_expr);
9164 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9166 if (cp_parser_skip_to_closing_brace (parser))
9167 cp_lexer_consume_token (parser->lexer);
9170 /* The capture list was built up in reverse order; fix that now. */
9171 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9172 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9174 if (ok)
9175 maybe_add_lambda_conv_op (type);
9177 type = finish_struct (type, /*attributes=*/NULL_TREE);
9179 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9180 parser->in_statement = in_statement;
9181 parser->in_switch_statement_p = in_switch_statement_p;
9182 parser->fully_implicit_function_template_p
9183 = fully_implicit_function_template_p;
9184 parser->implicit_template_parms = implicit_template_parms;
9185 parser->implicit_template_scope = implicit_template_scope;
9186 parser->auto_is_implicit_function_template_parm_p
9187 = auto_is_implicit_function_template_parm_p;
9190 pop_deferring_access_checks ();
9192 /* This field is only used during parsing of the lambda. */
9193 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9195 /* This lambda shouldn't have any proxies left at this point. */
9196 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9197 /* And now that we're done, push proxies for an enclosing lambda. */
9198 insert_pending_capture_proxies ();
9200 if (ok)
9201 lambda_expr = build_lambda_object (lambda_expr);
9202 else
9203 lambda_expr = error_mark_node;
9205 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9207 return lambda_expr;
9210 /* Parse the beginning of a lambda expression.
9212 lambda-introducer:
9213 [ lambda-capture [opt] ]
9215 LAMBDA_EXPR is the current representation of the lambda expression. */
9217 static void
9218 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9220 /* Need commas after the first capture. */
9221 bool first = true;
9223 /* Eat the leading `['. */
9224 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9226 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9227 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9228 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9229 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9230 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9231 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9233 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9235 cp_lexer_consume_token (parser->lexer);
9236 first = false;
9239 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9241 cp_token* capture_token;
9242 tree capture_id;
9243 tree capture_init_expr;
9244 cp_id_kind idk = CP_ID_KIND_NONE;
9245 bool explicit_init_p = false;
9247 enum capture_kind_type
9249 BY_COPY,
9250 BY_REFERENCE
9252 enum capture_kind_type capture_kind = BY_COPY;
9254 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9256 error ("expected end of capture-list");
9257 return;
9260 if (first)
9261 first = false;
9262 else
9263 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9265 /* Possibly capture `this'. */
9266 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9268 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9269 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9270 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9271 "with by-copy capture default");
9272 cp_lexer_consume_token (parser->lexer);
9273 add_capture (lambda_expr,
9274 /*id=*/this_identifier,
9275 /*initializer=*/finish_this_expr(),
9276 /*by_reference_p=*/false,
9277 explicit_init_p);
9278 continue;
9281 /* Remember whether we want to capture as a reference or not. */
9282 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9284 capture_kind = BY_REFERENCE;
9285 cp_lexer_consume_token (parser->lexer);
9288 /* Get the identifier. */
9289 capture_token = cp_lexer_peek_token (parser->lexer);
9290 capture_id = cp_parser_identifier (parser);
9292 if (capture_id == error_mark_node)
9293 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9294 delimiters, but I modified this to stop on unnested ']' as well. It
9295 was already changed to stop on unnested '}', so the
9296 "closing_parenthesis" name is no more misleading with my change. */
9298 cp_parser_skip_to_closing_parenthesis (parser,
9299 /*recovering=*/true,
9300 /*or_comma=*/true,
9301 /*consume_paren=*/true);
9302 break;
9305 /* Find the initializer for this capture. */
9306 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9307 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9308 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9310 bool direct, non_constant;
9311 /* An explicit initializer exists. */
9312 if (cxx_dialect < cxx14)
9313 pedwarn (input_location, 0,
9314 "lambda capture initializers "
9315 "only available with -std=c++14 or -std=gnu++14");
9316 capture_init_expr = cp_parser_initializer (parser, &direct,
9317 &non_constant);
9318 explicit_init_p = true;
9319 if (capture_init_expr == NULL_TREE)
9321 error ("empty initializer for lambda init-capture");
9322 capture_init_expr = error_mark_node;
9325 else
9327 const char* error_msg;
9329 /* Turn the identifier into an id-expression. */
9330 capture_init_expr
9331 = cp_parser_lookup_name_simple (parser, capture_id,
9332 capture_token->location);
9334 if (capture_init_expr == error_mark_node)
9336 unqualified_name_lookup_error (capture_id);
9337 continue;
9339 else if (DECL_P (capture_init_expr)
9340 && (!VAR_P (capture_init_expr)
9341 && TREE_CODE (capture_init_expr) != PARM_DECL))
9343 error_at (capture_token->location,
9344 "capture of non-variable %qD ",
9345 capture_init_expr);
9346 inform (0, "%q+#D declared here", capture_init_expr);
9347 continue;
9349 if (VAR_P (capture_init_expr)
9350 && decl_storage_duration (capture_init_expr) != dk_auto)
9352 if (pedwarn (capture_token->location, 0, "capture of variable "
9353 "%qD with non-automatic storage duration",
9354 capture_init_expr))
9355 inform (0, "%q+#D declared here", capture_init_expr);
9356 continue;
9359 capture_init_expr
9360 = finish_id_expression
9361 (capture_id,
9362 capture_init_expr,
9363 parser->scope,
9364 &idk,
9365 /*integral_constant_expression_p=*/false,
9366 /*allow_non_integral_constant_expression_p=*/false,
9367 /*non_integral_constant_expression_p=*/NULL,
9368 /*template_p=*/false,
9369 /*done=*/true,
9370 /*address_p=*/false,
9371 /*template_arg_p=*/false,
9372 &error_msg,
9373 capture_token->location);
9375 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9377 cp_lexer_consume_token (parser->lexer);
9378 capture_init_expr = make_pack_expansion (capture_init_expr);
9380 else
9381 check_for_bare_parameter_packs (capture_init_expr);
9384 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9385 && !explicit_init_p)
9387 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9388 && capture_kind == BY_COPY)
9389 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9390 "of %qD redundant with by-copy capture default",
9391 capture_id);
9392 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9393 && capture_kind == BY_REFERENCE)
9394 pedwarn (capture_token->location, 0, "explicit by-reference "
9395 "capture of %qD redundant with by-reference capture "
9396 "default", capture_id);
9399 add_capture (lambda_expr,
9400 capture_id,
9401 capture_init_expr,
9402 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9403 explicit_init_p);
9406 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9409 /* Parse the (optional) middle of a lambda expression.
9411 lambda-declarator:
9412 < template-parameter-list [opt] >
9413 ( parameter-declaration-clause [opt] )
9414 attribute-specifier [opt]
9415 mutable [opt]
9416 exception-specification [opt]
9417 lambda-return-type-clause [opt]
9419 LAMBDA_EXPR is the current representation of the lambda expression. */
9421 static bool
9422 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9424 /* 5.1.1.4 of the standard says:
9425 If a lambda-expression does not include a lambda-declarator, it is as if
9426 the lambda-declarator were ().
9427 This means an empty parameter list, no attributes, and no exception
9428 specification. */
9429 tree param_list = void_list_node;
9430 tree attributes = NULL_TREE;
9431 tree exception_spec = NULL_TREE;
9432 tree template_param_list = NULL_TREE;
9434 /* The template-parameter-list is optional, but must begin with
9435 an opening angle if present. */
9436 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9438 if (cxx_dialect < cxx14)
9439 pedwarn (parser->lexer->next_token->location, 0,
9440 "lambda templates are only available with "
9441 "-std=c++14 or -std=gnu++14");
9443 cp_lexer_consume_token (parser->lexer);
9445 template_param_list = cp_parser_template_parameter_list (parser);
9447 cp_parser_skip_to_end_of_template_parameter_list (parser);
9449 /* We just processed one more parameter list. */
9450 ++parser->num_template_parameter_lists;
9453 /* The parameter-declaration-clause is optional (unless
9454 template-parameter-list was given), but must begin with an
9455 opening parenthesis if present. */
9456 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9458 cp_lexer_consume_token (parser->lexer);
9460 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9462 /* Parse parameters. */
9463 param_list = cp_parser_parameter_declaration_clause (parser);
9465 /* Default arguments shall not be specified in the
9466 parameter-declaration-clause of a lambda-declarator. */
9467 for (tree t = param_list; t; t = TREE_CHAIN (t))
9468 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9469 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9470 "default argument specified for lambda parameter");
9472 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9474 attributes = cp_parser_attributes_opt (parser);
9476 /* Parse optional `mutable' keyword. */
9477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9479 cp_lexer_consume_token (parser->lexer);
9480 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9483 /* Parse optional exception specification. */
9484 exception_spec = cp_parser_exception_specification_opt (parser);
9486 /* Parse optional trailing return type. */
9487 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9489 cp_lexer_consume_token (parser->lexer);
9490 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9491 = cp_parser_trailing_type_id (parser);
9494 /* The function parameters must be in scope all the way until after the
9495 trailing-return-type in case of decltype. */
9496 pop_bindings_and_leave_scope ();
9498 else if (template_param_list != NULL_TREE) // generate diagnostic
9499 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9501 /* Create the function call operator.
9503 Messing with declarators like this is no uglier than building up the
9504 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9505 other code. */
9507 cp_decl_specifier_seq return_type_specs;
9508 cp_declarator* declarator;
9509 tree fco;
9510 int quals;
9511 void *p;
9513 clear_decl_specs (&return_type_specs);
9514 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9515 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9516 else
9517 /* Maybe we will deduce the return type later. */
9518 return_type_specs.type = make_auto ();
9520 p = obstack_alloc (&declarator_obstack, 0);
9522 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9523 sfk_none);
9525 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9526 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9527 declarator = make_call_declarator (declarator, param_list, quals,
9528 VIRT_SPEC_UNSPECIFIED,
9529 REF_QUAL_NONE,
9530 exception_spec,
9531 /*late_return_type=*/NULL_TREE,
9532 /*requires_clause*/NULL_TREE);
9533 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9536 fco = grokmethod (&return_type_specs,
9537 declarator,
9538 attributes);
9539 if (fco != error_mark_node)
9541 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9542 DECL_ARTIFICIAL (fco) = 1;
9543 /* Give the object parameter a different name. */
9544 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9545 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9546 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9548 if (template_param_list)
9550 fco = finish_member_template_decl (fco);
9551 finish_template_decl (template_param_list);
9552 --parser->num_template_parameter_lists;
9554 else if (parser->fully_implicit_function_template_p)
9555 fco = finish_fully_implicit_template (parser, fco);
9557 finish_member_declaration (fco);
9559 obstack_free (&declarator_obstack, p);
9561 return (fco != error_mark_node);
9565 /* Parse the body of a lambda expression, which is simply
9567 compound-statement
9569 but which requires special handling.
9570 LAMBDA_EXPR is the current representation of the lambda expression. */
9572 static void
9573 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9575 bool nested = (current_function_decl != NULL_TREE);
9576 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9577 if (nested)
9578 push_function_context ();
9579 else
9580 /* Still increment function_depth so that we don't GC in the
9581 middle of an expression. */
9582 ++function_depth;
9583 /* Clear this in case we're in the middle of a default argument. */
9584 parser->local_variables_forbidden_p = false;
9586 /* Finish the function call operator
9587 - class_specifier
9588 + late_parsing_for_member
9589 + function_definition_after_declarator
9590 + ctor_initializer_opt_and_function_body */
9592 tree fco = lambda_function (lambda_expr);
9593 tree body;
9594 bool done = false;
9595 tree compound_stmt;
9596 tree cap;
9598 /* Let the front end know that we are going to be defining this
9599 function. */
9600 start_preparsed_function (fco,
9601 NULL_TREE,
9602 SF_PRE_PARSED | SF_INCLASS_INLINE);
9604 start_lambda_scope (fco);
9605 body = begin_function_body ();
9607 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9608 goto out;
9610 /* Push the proxies for any explicit captures. */
9611 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9612 cap = TREE_CHAIN (cap))
9613 build_capture_proxy (TREE_PURPOSE (cap));
9615 compound_stmt = begin_compound_stmt (0);
9617 /* 5.1.1.4 of the standard says:
9618 If a lambda-expression does not include a trailing-return-type, it
9619 is as if the trailing-return-type denotes the following type:
9620 * if the compound-statement is of the form
9621 { return attribute-specifier [opt] expression ; }
9622 the type of the returned expression after lvalue-to-rvalue
9623 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9624 (_conv.array_ 4.2), and function-to-pointer conversion
9625 (_conv.func_ 4.3);
9626 * otherwise, void. */
9628 /* In a lambda that has neither a lambda-return-type-clause
9629 nor a deducible form, errors should be reported for return statements
9630 in the body. Since we used void as the placeholder return type, parsing
9631 the body as usual will give such desired behavior. */
9632 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9633 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9634 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9636 tree expr = NULL_TREE;
9637 cp_id_kind idk = CP_ID_KIND_NONE;
9639 /* Parse tentatively in case there's more after the initial return
9640 statement. */
9641 cp_parser_parse_tentatively (parser);
9643 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9645 expr = cp_parser_expression (parser, &idk);
9647 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9648 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9650 if (cp_parser_parse_definitely (parser))
9652 if (!processing_template_decl)
9653 apply_deduced_return_type (fco, lambda_return_type (expr));
9655 /* Will get error here if type not deduced yet. */
9656 finish_return_stmt (expr);
9658 done = true;
9662 if (!done)
9664 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9665 cp_parser_label_declaration (parser);
9666 cp_parser_statement_seq_opt (parser, NULL_TREE);
9667 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9670 finish_compound_stmt (compound_stmt);
9672 out:
9673 finish_function_body (body);
9674 finish_lambda_scope ();
9676 /* Finish the function and generate code for it if necessary. */
9677 tree fn = finish_function (/*inline*/2);
9679 /* Only expand if the call op is not a template. */
9680 if (!DECL_TEMPLATE_INFO (fco))
9681 expand_or_defer_fn (fn);
9684 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9685 if (nested)
9686 pop_function_context();
9687 else
9688 --function_depth;
9691 /* Statements [gram.stmt.stmt] */
9693 /* Parse a statement.
9695 statement:
9696 labeled-statement
9697 expression-statement
9698 compound-statement
9699 selection-statement
9700 iteration-statement
9701 jump-statement
9702 declaration-statement
9703 try-block
9705 C++11:
9707 statement:
9708 labeled-statement
9709 attribute-specifier-seq (opt) expression-statement
9710 attribute-specifier-seq (opt) compound-statement
9711 attribute-specifier-seq (opt) selection-statement
9712 attribute-specifier-seq (opt) iteration-statement
9713 attribute-specifier-seq (opt) jump-statement
9714 declaration-statement
9715 attribute-specifier-seq (opt) try-block
9717 TM Extension:
9719 statement:
9720 atomic-statement
9722 IN_COMPOUND is true when the statement is nested inside a
9723 cp_parser_compound_statement; this matters for certain pragmas.
9725 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9726 is a (possibly labeled) if statement which is not enclosed in braces
9727 and has an else clause. This is used to implement -Wparentheses. */
9729 static void
9730 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9731 bool in_compound, bool *if_p)
9733 tree statement, std_attrs = NULL_TREE;
9734 cp_token *token;
9735 location_t statement_location, attrs_location;
9737 restart:
9738 if (if_p != NULL)
9739 *if_p = false;
9740 /* There is no statement yet. */
9741 statement = NULL_TREE;
9743 saved_token_sentinel saved_tokens (parser->lexer);
9744 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9745 if (c_dialect_objc ())
9746 /* In obj-c++, seeing '[[' might be the either the beginning of
9747 c++11 attributes, or a nested objc-message-expression. So
9748 let's parse the c++11 attributes tentatively. */
9749 cp_parser_parse_tentatively (parser);
9750 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9751 if (c_dialect_objc ())
9753 if (!cp_parser_parse_definitely (parser))
9754 std_attrs = NULL_TREE;
9757 /* Peek at the next token. */
9758 token = cp_lexer_peek_token (parser->lexer);
9759 /* Remember the location of the first token in the statement. */
9760 statement_location = token->location;
9761 /* If this is a keyword, then that will often determine what kind of
9762 statement we have. */
9763 if (token->type == CPP_KEYWORD)
9765 enum rid keyword = token->keyword;
9767 switch (keyword)
9769 case RID_CASE:
9770 case RID_DEFAULT:
9771 /* Looks like a labeled-statement with a case label.
9772 Parse the label, and then use tail recursion to parse
9773 the statement. */
9774 cp_parser_label_for_labeled_statement (parser, std_attrs);
9775 goto restart;
9777 case RID_IF:
9778 case RID_SWITCH:
9779 statement = cp_parser_selection_statement (parser, if_p);
9780 break;
9782 case RID_WHILE:
9783 case RID_DO:
9784 case RID_FOR:
9785 statement = cp_parser_iteration_statement (parser, false);
9786 break;
9788 case RID_CILK_FOR:
9789 if (!flag_cilkplus)
9791 error_at (cp_lexer_peek_token (parser->lexer)->location,
9792 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9793 cp_lexer_consume_token (parser->lexer);
9794 statement = error_mark_node;
9796 else
9797 statement = cp_parser_cilk_for (parser, integer_zero_node);
9798 break;
9800 case RID_BREAK:
9801 case RID_CONTINUE:
9802 case RID_RETURN:
9803 case RID_GOTO:
9804 statement = cp_parser_jump_statement (parser);
9805 break;
9807 case RID_CILK_SYNC:
9808 cp_lexer_consume_token (parser->lexer);
9809 if (flag_cilkplus)
9811 tree sync_expr = build_cilk_sync ();
9812 SET_EXPR_LOCATION (sync_expr,
9813 token->location);
9814 statement = finish_expr_stmt (sync_expr);
9816 else
9818 error_at (token->location, "-fcilkplus must be enabled to use"
9819 " %<_Cilk_sync%>");
9820 statement = error_mark_node;
9822 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9823 break;
9825 /* Objective-C++ exception-handling constructs. */
9826 case RID_AT_TRY:
9827 case RID_AT_CATCH:
9828 case RID_AT_FINALLY:
9829 case RID_AT_SYNCHRONIZED:
9830 case RID_AT_THROW:
9831 statement = cp_parser_objc_statement (parser);
9832 break;
9834 case RID_TRY:
9835 statement = cp_parser_try_block (parser);
9836 break;
9838 case RID_NAMESPACE:
9839 /* This must be a namespace alias definition. */
9840 cp_parser_declaration_statement (parser);
9841 return;
9843 case RID_TRANSACTION_ATOMIC:
9844 case RID_TRANSACTION_RELAXED:
9845 statement = cp_parser_transaction (parser, keyword);
9846 break;
9847 case RID_TRANSACTION_CANCEL:
9848 statement = cp_parser_transaction_cancel (parser);
9849 break;
9851 default:
9852 /* It might be a keyword like `int' that can start a
9853 declaration-statement. */
9854 break;
9857 else if (token->type == CPP_NAME)
9859 /* If the next token is a `:', then we are looking at a
9860 labeled-statement. */
9861 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9862 if (token->type == CPP_COLON)
9864 /* Looks like a labeled-statement with an ordinary label.
9865 Parse the label, and then use tail recursion to parse
9866 the statement. */
9868 cp_parser_label_for_labeled_statement (parser, std_attrs);
9869 goto restart;
9872 /* Anything that starts with a `{' must be a compound-statement. */
9873 else if (token->type == CPP_OPEN_BRACE)
9874 statement = cp_parser_compound_statement (parser, NULL, false, false);
9875 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9876 a statement all its own. */
9877 else if (token->type == CPP_PRAGMA)
9879 /* Only certain OpenMP pragmas are attached to statements, and thus
9880 are considered statements themselves. All others are not. In
9881 the context of a compound, accept the pragma as a "statement" and
9882 return so that we can check for a close brace. Otherwise we
9883 require a real statement and must go back and read one. */
9884 if (in_compound)
9885 cp_parser_pragma (parser, pragma_compound);
9886 else if (!cp_parser_pragma (parser, pragma_stmt))
9887 goto restart;
9888 return;
9890 else if (token->type == CPP_EOF)
9892 cp_parser_error (parser, "expected statement");
9893 return;
9896 /* Everything else must be a declaration-statement or an
9897 expression-statement. Try for the declaration-statement
9898 first, unless we are looking at a `;', in which case we know that
9899 we have an expression-statement. */
9900 if (!statement)
9902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9904 if (std_attrs != NULL_TREE)
9906 /* Attributes should be parsed as part of the the
9907 declaration, so let's un-parse them. */
9908 saved_tokens.rollback();
9909 std_attrs = NULL_TREE;
9912 cp_parser_parse_tentatively (parser);
9913 /* Try to parse the declaration-statement. */
9914 cp_parser_declaration_statement (parser);
9915 /* If that worked, we're done. */
9916 if (cp_parser_parse_definitely (parser))
9917 return;
9919 /* Look for an expression-statement instead. */
9920 statement = cp_parser_expression_statement (parser, in_statement_expr);
9923 /* Set the line number for the statement. */
9924 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9925 SET_EXPR_LOCATION (statement, statement_location);
9927 /* Note that for now, we don't do anything with c++11 statements
9928 parsed at this level. */
9929 if (std_attrs != NULL_TREE)
9930 warning_at (attrs_location,
9931 OPT_Wattributes,
9932 "attributes at the beginning of statement are ignored");
9935 /* Parse the label for a labeled-statement, i.e.
9937 identifier :
9938 case constant-expression :
9939 default :
9941 GNU Extension:
9942 case constant-expression ... constant-expression : statement
9944 When a label is parsed without errors, the label is added to the
9945 parse tree by the finish_* functions, so this function doesn't
9946 have to return the label. */
9948 static void
9949 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9951 cp_token *token;
9952 tree label = NULL_TREE;
9953 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9955 /* The next token should be an identifier. */
9956 token = cp_lexer_peek_token (parser->lexer);
9957 if (token->type != CPP_NAME
9958 && token->type != CPP_KEYWORD)
9960 cp_parser_error (parser, "expected labeled-statement");
9961 return;
9964 parser->colon_corrects_to_scope_p = false;
9965 switch (token->keyword)
9967 case RID_CASE:
9969 tree expr, expr_hi;
9970 cp_token *ellipsis;
9972 /* Consume the `case' token. */
9973 cp_lexer_consume_token (parser->lexer);
9974 /* Parse the constant-expression. */
9975 expr = cp_parser_constant_expression (parser);
9976 if (check_for_bare_parameter_packs (expr))
9977 expr = error_mark_node;
9979 ellipsis = cp_lexer_peek_token (parser->lexer);
9980 if (ellipsis->type == CPP_ELLIPSIS)
9982 /* Consume the `...' token. */
9983 cp_lexer_consume_token (parser->lexer);
9984 expr_hi = cp_parser_constant_expression (parser);
9985 if (check_for_bare_parameter_packs (expr_hi))
9986 expr_hi = error_mark_node;
9988 /* We don't need to emit warnings here, as the common code
9989 will do this for us. */
9991 else
9992 expr_hi = NULL_TREE;
9994 if (parser->in_switch_statement_p)
9995 finish_case_label (token->location, expr, expr_hi);
9996 else
9997 error_at (token->location,
9998 "case label %qE not within a switch statement",
9999 expr);
10001 break;
10003 case RID_DEFAULT:
10004 /* Consume the `default' token. */
10005 cp_lexer_consume_token (parser->lexer);
10007 if (parser->in_switch_statement_p)
10008 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10009 else
10010 error_at (token->location, "case label not within a switch statement");
10011 break;
10013 default:
10014 /* Anything else must be an ordinary label. */
10015 label = finish_label_stmt (cp_parser_identifier (parser));
10016 break;
10019 /* Require the `:' token. */
10020 cp_parser_require (parser, CPP_COLON, RT_COLON);
10022 /* An ordinary label may optionally be followed by attributes.
10023 However, this is only permitted if the attributes are then
10024 followed by a semicolon. This is because, for backward
10025 compatibility, when parsing
10026 lab: __attribute__ ((unused)) int i;
10027 we want the attribute to attach to "i", not "lab". */
10028 if (label != NULL_TREE
10029 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10031 tree attrs;
10032 cp_parser_parse_tentatively (parser);
10033 attrs = cp_parser_gnu_attributes_opt (parser);
10034 if (attrs == NULL_TREE
10035 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10036 cp_parser_abort_tentative_parse (parser);
10037 else if (!cp_parser_parse_definitely (parser))
10039 else
10040 attributes = chainon (attributes, attrs);
10043 if (attributes != NULL_TREE)
10044 cplus_decl_attributes (&label, attributes, 0);
10046 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10049 /* Parse an expression-statement.
10051 expression-statement:
10052 expression [opt] ;
10054 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10055 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10056 indicates whether this expression-statement is part of an
10057 expression statement. */
10059 static tree
10060 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10062 tree statement = NULL_TREE;
10063 cp_token *token = cp_lexer_peek_token (parser->lexer);
10065 /* If the next token is a ';', then there is no expression
10066 statement. */
10067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10069 statement = cp_parser_expression (parser);
10070 if (statement == error_mark_node
10071 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10073 cp_parser_skip_to_end_of_block_or_statement (parser);
10074 return error_mark_node;
10078 /* Give a helpful message for "A<T>::type t;" and the like. */
10079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10080 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10082 if (TREE_CODE (statement) == SCOPE_REF)
10083 error_at (token->location, "need %<typename%> before %qE because "
10084 "%qT is a dependent scope",
10085 statement, TREE_OPERAND (statement, 0));
10086 else if (is_overloaded_fn (statement)
10087 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10089 /* A::A a; */
10090 tree fn = get_first_fn (statement);
10091 error_at (token->location,
10092 "%<%T::%D%> names the constructor, not the type",
10093 DECL_CONTEXT (fn), DECL_NAME (fn));
10097 /* Consume the final `;'. */
10098 cp_parser_consume_semicolon_at_end_of_statement (parser);
10100 if (in_statement_expr
10101 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10102 /* This is the final expression statement of a statement
10103 expression. */
10104 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10105 else if (statement)
10106 statement = finish_expr_stmt (statement);
10108 return statement;
10111 /* Parse a compound-statement.
10113 compound-statement:
10114 { statement-seq [opt] }
10116 GNU extension:
10118 compound-statement:
10119 { label-declaration-seq [opt] statement-seq [opt] }
10121 label-declaration-seq:
10122 label-declaration
10123 label-declaration-seq label-declaration
10125 Returns a tree representing the statement. */
10127 static tree
10128 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10129 bool in_try, bool function_body)
10131 tree compound_stmt;
10133 /* Consume the `{'. */
10134 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10135 return error_mark_node;
10136 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10137 && !function_body && cxx_dialect < cxx14)
10138 pedwarn (input_location, OPT_Wpedantic,
10139 "compound-statement in constexpr function");
10140 /* Begin the compound-statement. */
10141 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10142 /* If the next keyword is `__label__' we have a label declaration. */
10143 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10144 cp_parser_label_declaration (parser);
10145 /* Parse an (optional) statement-seq. */
10146 cp_parser_statement_seq_opt (parser, in_statement_expr);
10147 /* Finish the compound-statement. */
10148 finish_compound_stmt (compound_stmt);
10149 /* Consume the `}'. */
10150 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10152 return compound_stmt;
10155 /* Parse an (optional) statement-seq.
10157 statement-seq:
10158 statement
10159 statement-seq [opt] statement */
10161 static void
10162 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10164 /* Scan statements until there aren't any more. */
10165 while (true)
10167 cp_token *token = cp_lexer_peek_token (parser->lexer);
10169 /* If we are looking at a `}', then we have run out of
10170 statements; the same is true if we have reached the end
10171 of file, or have stumbled upon a stray '@end'. */
10172 if (token->type == CPP_CLOSE_BRACE
10173 || token->type == CPP_EOF
10174 || token->type == CPP_PRAGMA_EOL
10175 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10176 break;
10178 /* If we are in a compound statement and find 'else' then
10179 something went wrong. */
10180 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10182 if (parser->in_statement & IN_IF_STMT)
10183 break;
10184 else
10186 token = cp_lexer_consume_token (parser->lexer);
10187 error_at (token->location, "%<else%> without a previous %<if%>");
10191 /* Parse the statement. */
10192 cp_parser_statement (parser, in_statement_expr, true, NULL);
10196 /* Parse a selection-statement.
10198 selection-statement:
10199 if ( condition ) statement
10200 if ( condition ) statement else statement
10201 switch ( condition ) statement
10203 Returns the new IF_STMT or SWITCH_STMT.
10205 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10206 is a (possibly labeled) if statement which is not enclosed in
10207 braces and has an else clause. This is used to implement
10208 -Wparentheses. */
10210 static tree
10211 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10213 cp_token *token;
10214 enum rid keyword;
10216 if (if_p != NULL)
10217 *if_p = false;
10219 /* Peek at the next token. */
10220 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10222 /* See what kind of keyword it is. */
10223 keyword = token->keyword;
10224 switch (keyword)
10226 case RID_IF:
10227 case RID_SWITCH:
10229 tree statement;
10230 tree condition;
10232 /* Look for the `('. */
10233 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10235 cp_parser_skip_to_end_of_statement (parser);
10236 return error_mark_node;
10239 /* Begin the selection-statement. */
10240 if (keyword == RID_IF)
10241 statement = begin_if_stmt ();
10242 else
10243 statement = begin_switch_stmt ();
10245 /* Parse the condition. */
10246 condition = cp_parser_condition (parser);
10247 /* Look for the `)'. */
10248 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10249 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10250 /*consume_paren=*/true);
10252 if (keyword == RID_IF)
10254 bool nested_if;
10255 unsigned char in_statement;
10257 /* Add the condition. */
10258 finish_if_stmt_cond (condition, statement);
10260 /* Parse the then-clause. */
10261 in_statement = parser->in_statement;
10262 parser->in_statement |= IN_IF_STMT;
10263 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10265 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10266 add_stmt (build_empty_stmt (loc));
10267 cp_lexer_consume_token (parser->lexer);
10268 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10269 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10270 "empty body in an %<if%> statement");
10271 nested_if = false;
10273 else
10274 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10275 parser->in_statement = in_statement;
10277 finish_then_clause (statement);
10279 /* If the next token is `else', parse the else-clause. */
10280 if (cp_lexer_next_token_is_keyword (parser->lexer,
10281 RID_ELSE))
10283 /* Consume the `else' keyword. */
10284 cp_lexer_consume_token (parser->lexer);
10285 begin_else_clause (statement);
10286 /* Parse the else-clause. */
10287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10289 location_t loc;
10290 loc = cp_lexer_peek_token (parser->lexer)->location;
10291 warning_at (loc,
10292 OPT_Wempty_body, "suggest braces around "
10293 "empty body in an %<else%> statement");
10294 add_stmt (build_empty_stmt (loc));
10295 cp_lexer_consume_token (parser->lexer);
10297 else
10298 cp_parser_implicitly_scoped_statement (parser, NULL);
10300 finish_else_clause (statement);
10302 /* If we are currently parsing a then-clause, then
10303 IF_P will not be NULL. We set it to true to
10304 indicate that this if statement has an else clause.
10305 This may trigger the Wparentheses warning below
10306 when we get back up to the parent if statement. */
10307 if (if_p != NULL)
10308 *if_p = true;
10310 else
10312 /* This if statement does not have an else clause. If
10313 NESTED_IF is true, then the then-clause is an if
10314 statement which does have an else clause. We warn
10315 about the potential ambiguity. */
10316 if (nested_if)
10317 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10318 "suggest explicit braces to avoid ambiguous"
10319 " %<else%>");
10322 /* Now we're all done with the if-statement. */
10323 finish_if_stmt (statement);
10325 else
10327 bool in_switch_statement_p;
10328 unsigned char in_statement;
10330 /* Add the condition. */
10331 finish_switch_cond (condition, statement);
10333 /* Parse the body of the switch-statement. */
10334 in_switch_statement_p = parser->in_switch_statement_p;
10335 in_statement = parser->in_statement;
10336 parser->in_switch_statement_p = true;
10337 parser->in_statement |= IN_SWITCH_STMT;
10338 cp_parser_implicitly_scoped_statement (parser, NULL);
10339 parser->in_switch_statement_p = in_switch_statement_p;
10340 parser->in_statement = in_statement;
10342 /* Now we're all done with the switch-statement. */
10343 finish_switch_stmt (statement);
10346 return statement;
10348 break;
10350 default:
10351 cp_parser_error (parser, "expected selection-statement");
10352 return error_mark_node;
10356 /* Parse a condition.
10358 condition:
10359 expression
10360 type-specifier-seq declarator = initializer-clause
10361 type-specifier-seq declarator braced-init-list
10363 GNU Extension:
10365 condition:
10366 type-specifier-seq declarator asm-specification [opt]
10367 attributes [opt] = assignment-expression
10369 Returns the expression that should be tested. */
10371 static tree
10372 cp_parser_condition (cp_parser* parser)
10374 cp_decl_specifier_seq type_specifiers;
10375 const char *saved_message;
10376 int declares_class_or_enum;
10378 /* Try the declaration first. */
10379 cp_parser_parse_tentatively (parser);
10380 /* New types are not allowed in the type-specifier-seq for a
10381 condition. */
10382 saved_message = parser->type_definition_forbidden_message;
10383 parser->type_definition_forbidden_message
10384 = G_("types may not be defined in conditions");
10385 /* Parse the type-specifier-seq. */
10386 cp_parser_decl_specifier_seq (parser,
10387 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10388 &type_specifiers,
10389 &declares_class_or_enum);
10390 /* Restore the saved message. */
10391 parser->type_definition_forbidden_message = saved_message;
10392 /* If all is well, we might be looking at a declaration. */
10393 if (!cp_parser_error_occurred (parser))
10395 tree decl;
10396 tree asm_specification;
10397 tree attributes;
10398 cp_declarator *declarator;
10399 tree initializer = NULL_TREE;
10401 /* Parse the declarator. */
10402 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10403 /*ctor_dtor_or_conv_p=*/NULL,
10404 /*parenthesized_p=*/NULL,
10405 /*member_p=*/false,
10406 /*friend_p=*/false);
10407 /* Parse the attributes. */
10408 attributes = cp_parser_attributes_opt (parser);
10409 /* Parse the asm-specification. */
10410 asm_specification = cp_parser_asm_specification_opt (parser);
10411 /* If the next token is not an `=' or '{', then we might still be
10412 looking at an expression. For example:
10414 if (A(a).x)
10416 looks like a decl-specifier-seq and a declarator -- but then
10417 there is no `=', so this is an expression. */
10418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10419 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10420 cp_parser_simulate_error (parser);
10422 /* If we did see an `=' or '{', then we are looking at a declaration
10423 for sure. */
10424 if (cp_parser_parse_definitely (parser))
10426 tree pushed_scope;
10427 bool non_constant_p;
10428 bool flags = LOOKUP_ONLYCONVERTING;
10430 /* Create the declaration. */
10431 decl = start_decl (declarator, &type_specifiers,
10432 /*initialized_p=*/true,
10433 attributes, /*prefix_attributes=*/NULL_TREE,
10434 &pushed_scope);
10436 /* Parse the initializer. */
10437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10439 initializer = cp_parser_braced_list (parser, &non_constant_p);
10440 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10441 flags = 0;
10443 else
10445 /* Consume the `='. */
10446 cp_parser_require (parser, CPP_EQ, RT_EQ);
10447 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10449 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10452 /* Process the initializer. */
10453 cp_finish_decl (decl,
10454 initializer, !non_constant_p,
10455 asm_specification,
10456 flags);
10458 if (pushed_scope)
10459 pop_scope (pushed_scope);
10461 return convert_from_reference (decl);
10464 /* If we didn't even get past the declarator successfully, we are
10465 definitely not looking at a declaration. */
10466 else
10467 cp_parser_abort_tentative_parse (parser);
10469 /* Otherwise, we are looking at an expression. */
10470 return cp_parser_expression (parser);
10473 /* Parses a for-statement or range-for-statement until the closing ')',
10474 not included. */
10476 static tree
10477 cp_parser_for (cp_parser *parser, bool ivdep)
10479 tree init, scope, decl;
10480 bool is_range_for;
10482 /* Begin the for-statement. */
10483 scope = begin_for_scope (&init);
10485 /* Parse the initialization. */
10486 is_range_for = cp_parser_for_init_statement (parser, &decl);
10488 if (is_range_for)
10489 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10490 else
10491 return cp_parser_c_for (parser, scope, init, ivdep);
10494 static tree
10495 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10497 /* Normal for loop */
10498 tree condition = NULL_TREE;
10499 tree expression = NULL_TREE;
10500 tree stmt;
10502 stmt = begin_for_stmt (scope, init);
10503 /* The for-init-statement has already been parsed in
10504 cp_parser_for_init_statement, so no work is needed here. */
10505 finish_for_init_stmt (stmt);
10507 /* If there's a condition, process it. */
10508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10509 condition = cp_parser_condition (parser);
10510 else if (ivdep)
10512 cp_parser_error (parser, "missing loop condition in loop with "
10513 "%<GCC ivdep%> pragma");
10514 condition = error_mark_node;
10516 finish_for_cond (condition, stmt, ivdep);
10517 /* Look for the `;'. */
10518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10520 /* If there's an expression, process it. */
10521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10522 expression = cp_parser_expression (parser);
10523 finish_for_expr (expression, stmt);
10525 return stmt;
10528 /* Tries to parse a range-based for-statement:
10530 range-based-for:
10531 decl-specifier-seq declarator : expression
10533 The decl-specifier-seq declarator and the `:' are already parsed by
10534 cp_parser_for_init_statement. If processing_template_decl it returns a
10535 newly created RANGE_FOR_STMT; if not, it is converted to a
10536 regular FOR_STMT. */
10538 static tree
10539 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10540 bool ivdep)
10542 tree stmt, range_expr;
10544 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10546 bool expr_non_constant_p;
10547 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10549 else
10550 range_expr = cp_parser_expression (parser);
10552 /* If in template, STMT is converted to a normal for-statement
10553 at instantiation. If not, it is done just ahead. */
10554 if (processing_template_decl)
10556 if (check_for_bare_parameter_packs (range_expr))
10557 range_expr = error_mark_node;
10558 stmt = begin_range_for_stmt (scope, init);
10559 if (ivdep)
10560 RANGE_FOR_IVDEP (stmt) = 1;
10561 finish_range_for_decl (stmt, range_decl, range_expr);
10562 if (!type_dependent_expression_p (range_expr)
10563 /* do_auto_deduction doesn't mess with template init-lists. */
10564 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10565 do_range_for_auto_deduction (range_decl, range_expr);
10567 else
10569 stmt = begin_for_stmt (scope, init);
10570 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10572 return stmt;
10575 /* Subroutine of cp_convert_range_for: given the initializer expression,
10576 builds up the range temporary. */
10578 static tree
10579 build_range_temp (tree range_expr)
10581 tree range_type, range_temp;
10583 /* Find out the type deduced by the declaration
10584 `auto &&__range = range_expr'. */
10585 range_type = cp_build_reference_type (make_auto (), true);
10586 range_type = do_auto_deduction (range_type, range_expr,
10587 type_uses_auto (range_type));
10589 /* Create the __range variable. */
10590 range_temp = build_decl (input_location, VAR_DECL,
10591 get_identifier ("__for_range"), range_type);
10592 TREE_USED (range_temp) = 1;
10593 DECL_ARTIFICIAL (range_temp) = 1;
10595 return range_temp;
10598 /* Used by cp_parser_range_for in template context: we aren't going to
10599 do a full conversion yet, but we still need to resolve auto in the
10600 type of the for-range-declaration if present. This is basically
10601 a shortcut version of cp_convert_range_for. */
10603 static void
10604 do_range_for_auto_deduction (tree decl, tree range_expr)
10606 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10607 if (auto_node)
10609 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10610 range_temp = convert_from_reference (build_range_temp (range_expr));
10611 iter_type = (cp_parser_perform_range_for_lookup
10612 (range_temp, &begin_dummy, &end_dummy));
10613 if (iter_type)
10615 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10616 iter_type);
10617 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10618 tf_warning_or_error);
10619 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10620 iter_decl, auto_node);
10625 /* Converts a range-based for-statement into a normal
10626 for-statement, as per the definition.
10628 for (RANGE_DECL : RANGE_EXPR)
10629 BLOCK
10631 should be equivalent to:
10634 auto &&__range = RANGE_EXPR;
10635 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10636 __begin != __end;
10637 ++__begin)
10639 RANGE_DECL = *__begin;
10640 BLOCK
10644 If RANGE_EXPR is an array:
10645 BEGIN_EXPR = __range
10646 END_EXPR = __range + ARRAY_SIZE(__range)
10647 Else if RANGE_EXPR has a member 'begin' or 'end':
10648 BEGIN_EXPR = __range.begin()
10649 END_EXPR = __range.end()
10650 Else:
10651 BEGIN_EXPR = begin(__range)
10652 END_EXPR = end(__range);
10654 If __range has a member 'begin' but not 'end', or vice versa, we must
10655 still use the second alternative (it will surely fail, however).
10656 When calling begin()/end() in the third alternative we must use
10657 argument dependent lookup, but always considering 'std' as an associated
10658 namespace. */
10660 tree
10661 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10662 bool ivdep)
10664 tree begin, end;
10665 tree iter_type, begin_expr, end_expr;
10666 tree condition, expression;
10668 if (range_decl == error_mark_node || range_expr == error_mark_node)
10669 /* If an error happened previously do nothing or else a lot of
10670 unhelpful errors would be issued. */
10671 begin_expr = end_expr = iter_type = error_mark_node;
10672 else
10674 tree range_temp;
10676 if (TREE_CODE (range_expr) == VAR_DECL
10677 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10678 /* Can't bind a reference to an array of runtime bound. */
10679 range_temp = range_expr;
10680 else
10682 range_temp = build_range_temp (range_expr);
10683 pushdecl (range_temp);
10684 cp_finish_decl (range_temp, range_expr,
10685 /*is_constant_init*/false, NULL_TREE,
10686 LOOKUP_ONLYCONVERTING);
10687 range_temp = convert_from_reference (range_temp);
10689 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10690 &begin_expr, &end_expr);
10693 /* The new for initialization statement. */
10694 begin = build_decl (input_location, VAR_DECL,
10695 get_identifier ("__for_begin"), iter_type);
10696 TREE_USED (begin) = 1;
10697 DECL_ARTIFICIAL (begin) = 1;
10698 pushdecl (begin);
10699 cp_finish_decl (begin, begin_expr,
10700 /*is_constant_init*/false, NULL_TREE,
10701 LOOKUP_ONLYCONVERTING);
10703 end = build_decl (input_location, VAR_DECL,
10704 get_identifier ("__for_end"), iter_type);
10705 TREE_USED (end) = 1;
10706 DECL_ARTIFICIAL (end) = 1;
10707 pushdecl (end);
10708 cp_finish_decl (end, end_expr,
10709 /*is_constant_init*/false, NULL_TREE,
10710 LOOKUP_ONLYCONVERTING);
10712 finish_for_init_stmt (statement);
10714 /* The new for condition. */
10715 condition = build_x_binary_op (input_location, NE_EXPR,
10716 begin, ERROR_MARK,
10717 end, ERROR_MARK,
10718 NULL, tf_warning_or_error);
10719 finish_for_cond (condition, statement, ivdep);
10721 /* The new increment expression. */
10722 expression = finish_unary_op_expr (input_location,
10723 PREINCREMENT_EXPR, begin,
10724 tf_warning_or_error);
10725 finish_for_expr (expression, statement);
10727 /* The declaration is initialized with *__begin inside the loop body. */
10728 cp_finish_decl (range_decl,
10729 build_x_indirect_ref (input_location, begin, RO_NULL,
10730 tf_warning_or_error),
10731 /*is_constant_init*/false, NULL_TREE,
10732 LOOKUP_ONLYCONVERTING);
10734 return statement;
10737 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10738 We need to solve both at the same time because the method used
10739 depends on the existence of members begin or end.
10740 Returns the type deduced for the iterator expression. */
10742 static tree
10743 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10745 if (error_operand_p (range))
10747 *begin = *end = error_mark_node;
10748 return error_mark_node;
10751 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10753 error ("range-based %<for%> expression of type %qT "
10754 "has incomplete type", TREE_TYPE (range));
10755 *begin = *end = error_mark_node;
10756 return error_mark_node;
10758 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10760 /* If RANGE is an array, we will use pointer arithmetic. */
10761 *begin = range;
10762 *end = build_binary_op (input_location, PLUS_EXPR,
10763 range,
10764 array_type_nelts_top (TREE_TYPE (range)),
10766 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10768 else
10770 /* If it is not an array, we must do a bit of magic. */
10771 tree id_begin, id_end;
10772 tree member_begin, member_end;
10774 *begin = *end = error_mark_node;
10776 id_begin = get_identifier ("begin");
10777 id_end = get_identifier ("end");
10778 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10779 /*protect=*/2, /*want_type=*/false,
10780 tf_warning_or_error);
10781 member_end = lookup_member (TREE_TYPE (range), id_end,
10782 /*protect=*/2, /*want_type=*/false,
10783 tf_warning_or_error);
10785 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10787 /* Use the member functions. */
10788 if (member_begin != NULL_TREE)
10789 *begin = cp_parser_range_for_member_function (range, id_begin);
10790 else
10791 error ("range-based %<for%> expression of type %qT has an "
10792 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10794 if (member_end != NULL_TREE)
10795 *end = cp_parser_range_for_member_function (range, id_end);
10796 else
10797 error ("range-based %<for%> expression of type %qT has a "
10798 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10800 else
10802 /* Use global functions with ADL. */
10803 vec<tree, va_gc> *vec;
10804 vec = make_tree_vector ();
10806 vec_safe_push (vec, range);
10808 member_begin = perform_koenig_lookup (id_begin, vec,
10809 tf_warning_or_error);
10810 *begin = finish_call_expr (member_begin, &vec, false, true,
10811 tf_warning_or_error);
10812 member_end = perform_koenig_lookup (id_end, vec,
10813 tf_warning_or_error);
10814 *end = finish_call_expr (member_end, &vec, false, true,
10815 tf_warning_or_error);
10817 release_tree_vector (vec);
10820 /* Last common checks. */
10821 if (*begin == error_mark_node || *end == error_mark_node)
10823 /* If one of the expressions is an error do no more checks. */
10824 *begin = *end = error_mark_node;
10825 return error_mark_node;
10827 else if (type_dependent_expression_p (*begin)
10828 || type_dependent_expression_p (*end))
10829 /* Can happen, when, eg, in a template context, Koenig lookup
10830 can't resolve begin/end (c++/58503). */
10831 return NULL_TREE;
10832 else
10834 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10835 /* The unqualified type of the __begin and __end temporaries should
10836 be the same, as required by the multiple auto declaration. */
10837 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10838 error ("inconsistent begin/end types in range-based %<for%> "
10839 "statement: %qT and %qT",
10840 TREE_TYPE (*begin), TREE_TYPE (*end));
10841 return iter_type;
10846 /* Helper function for cp_parser_perform_range_for_lookup.
10847 Builds a tree for RANGE.IDENTIFIER(). */
10849 static tree
10850 cp_parser_range_for_member_function (tree range, tree identifier)
10852 tree member, res;
10853 vec<tree, va_gc> *vec;
10855 member = finish_class_member_access_expr (range, identifier,
10856 false, tf_warning_or_error);
10857 if (member == error_mark_node)
10858 return error_mark_node;
10860 vec = make_tree_vector ();
10861 res = finish_call_expr (member, &vec,
10862 /*disallow_virtual=*/false,
10863 /*koenig_p=*/false,
10864 tf_warning_or_error);
10865 release_tree_vector (vec);
10866 return res;
10869 /* Parse an iteration-statement.
10871 iteration-statement:
10872 while ( condition ) statement
10873 do statement while ( expression ) ;
10874 for ( for-init-statement condition [opt] ; expression [opt] )
10875 statement
10877 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10879 static tree
10880 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10882 cp_token *token;
10883 enum rid keyword;
10884 tree statement;
10885 unsigned char in_statement;
10887 /* Peek at the next token. */
10888 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10889 if (!token)
10890 return error_mark_node;
10892 /* Remember whether or not we are already within an iteration
10893 statement. */
10894 in_statement = parser->in_statement;
10896 /* See what kind of keyword it is. */
10897 keyword = token->keyword;
10898 switch (keyword)
10900 case RID_WHILE:
10902 tree condition;
10904 /* Begin the while-statement. */
10905 statement = begin_while_stmt ();
10906 /* Look for the `('. */
10907 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10908 /* Parse the condition. */
10909 condition = cp_parser_condition (parser);
10910 finish_while_stmt_cond (condition, statement, ivdep);
10911 /* Look for the `)'. */
10912 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10913 /* Parse the dependent statement. */
10914 parser->in_statement = IN_ITERATION_STMT;
10915 cp_parser_already_scoped_statement (parser);
10916 parser->in_statement = in_statement;
10917 /* We're done with the while-statement. */
10918 finish_while_stmt (statement);
10920 break;
10922 case RID_DO:
10924 tree expression;
10926 /* Begin the do-statement. */
10927 statement = begin_do_stmt ();
10928 /* Parse the body of the do-statement. */
10929 parser->in_statement = IN_ITERATION_STMT;
10930 cp_parser_implicitly_scoped_statement (parser, NULL);
10931 parser->in_statement = in_statement;
10932 finish_do_body (statement);
10933 /* Look for the `while' keyword. */
10934 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10935 /* Look for the `('. */
10936 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10937 /* Parse the expression. */
10938 expression = cp_parser_expression (parser);
10939 /* We're done with the do-statement. */
10940 finish_do_stmt (expression, statement, ivdep);
10941 /* Look for the `)'. */
10942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10943 /* Look for the `;'. */
10944 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10946 break;
10948 case RID_FOR:
10950 /* Look for the `('. */
10951 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10953 statement = cp_parser_for (parser, ivdep);
10955 /* Look for the `)'. */
10956 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10958 /* Parse the body of the for-statement. */
10959 parser->in_statement = IN_ITERATION_STMT;
10960 cp_parser_already_scoped_statement (parser);
10961 parser->in_statement = in_statement;
10963 /* We're done with the for-statement. */
10964 finish_for_stmt (statement);
10966 break;
10968 default:
10969 cp_parser_error (parser, "expected iteration-statement");
10970 statement = error_mark_node;
10971 break;
10974 return statement;
10977 /* Parse a for-init-statement or the declarator of a range-based-for.
10978 Returns true if a range-based-for declaration is seen.
10980 for-init-statement:
10981 expression-statement
10982 simple-declaration */
10984 static bool
10985 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10987 /* If the next token is a `;', then we have an empty
10988 expression-statement. Grammatically, this is also a
10989 simple-declaration, but an invalid one, because it does not
10990 declare anything. Therefore, if we did not handle this case
10991 specially, we would issue an error message about an invalid
10992 declaration. */
10993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10995 bool is_range_for = false;
10996 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10999 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
11001 /* N3994 -- for (id : init) ... */
11002 if (cxx_dialect < cxx1z)
11003 pedwarn (input_location, 0, "range-based for loop without a "
11004 "type-specifier only available with "
11005 "-std=c++1z or -std=gnu++1z");
11006 tree name = cp_parser_identifier (parser);
11007 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
11008 *decl = build_decl (input_location, VAR_DECL, name, type);
11009 pushdecl (*decl);
11010 cp_lexer_consume_token (parser->lexer);
11011 return true;
11014 /* A colon is used in range-based for. */
11015 parser->colon_corrects_to_scope_p = false;
11017 /* We're going to speculatively look for a declaration, falling back
11018 to an expression, if necessary. */
11019 cp_parser_parse_tentatively (parser);
11020 /* Parse the declaration. */
11021 cp_parser_simple_declaration (parser,
11022 /*function_definition_allowed_p=*/false,
11023 decl);
11024 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11025 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11027 /* It is a range-for, consume the ':' */
11028 cp_lexer_consume_token (parser->lexer);
11029 is_range_for = true;
11030 if (cxx_dialect < cxx11)
11032 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11033 "range-based %<for%> loops only available with "
11034 "-std=c++11 or -std=gnu++11");
11035 *decl = error_mark_node;
11038 else
11039 /* The ';' is not consumed yet because we told
11040 cp_parser_simple_declaration not to. */
11041 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11043 if (cp_parser_parse_definitely (parser))
11044 return is_range_for;
11045 /* If the tentative parse failed, then we shall need to look for an
11046 expression-statement. */
11048 /* If we are here, it is an expression-statement. */
11049 cp_parser_expression_statement (parser, NULL_TREE);
11050 return false;
11053 /* Parse a jump-statement.
11055 jump-statement:
11056 break ;
11057 continue ;
11058 return expression [opt] ;
11059 return braced-init-list ;
11060 goto identifier ;
11062 GNU extension:
11064 jump-statement:
11065 goto * expression ;
11067 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11069 static tree
11070 cp_parser_jump_statement (cp_parser* parser)
11072 tree statement = error_mark_node;
11073 cp_token *token;
11074 enum rid keyword;
11075 unsigned char in_statement;
11077 /* Peek at the next token. */
11078 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11079 if (!token)
11080 return error_mark_node;
11082 /* See what kind of keyword it is. */
11083 keyword = token->keyword;
11084 switch (keyword)
11086 case RID_BREAK:
11087 in_statement = parser->in_statement & ~IN_IF_STMT;
11088 switch (in_statement)
11090 case 0:
11091 error_at (token->location, "break statement not within loop or switch");
11092 break;
11093 default:
11094 gcc_assert ((in_statement & IN_SWITCH_STMT)
11095 || in_statement == IN_ITERATION_STMT);
11096 statement = finish_break_stmt ();
11097 if (in_statement == IN_ITERATION_STMT)
11098 break_maybe_infinite_loop ();
11099 break;
11100 case IN_OMP_BLOCK:
11101 error_at (token->location, "invalid exit from OpenMP structured block");
11102 break;
11103 case IN_OMP_FOR:
11104 error_at (token->location, "break statement used with OpenMP for loop");
11105 break;
11106 case IN_CILK_SIMD_FOR:
11107 error_at (token->location, "break statement used with Cilk Plus for loop");
11108 break;
11110 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11111 break;
11113 case RID_CONTINUE:
11114 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11116 case 0:
11117 error_at (token->location, "continue statement not within a loop");
11118 break;
11119 case IN_CILK_SIMD_FOR:
11120 error_at (token->location,
11121 "continue statement within %<#pragma simd%> loop body");
11122 /* Fall through. */
11123 case IN_ITERATION_STMT:
11124 case IN_OMP_FOR:
11125 statement = finish_continue_stmt ();
11126 break;
11127 case IN_OMP_BLOCK:
11128 error_at (token->location, "invalid exit from OpenMP structured block");
11129 break;
11130 default:
11131 gcc_unreachable ();
11133 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11134 break;
11136 case RID_RETURN:
11138 tree expr;
11139 bool expr_non_constant_p;
11141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11143 cp_lexer_set_source_position (parser->lexer);
11144 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11145 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11147 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11148 expr = cp_parser_expression (parser);
11149 else
11150 /* If the next token is a `;', then there is no
11151 expression. */
11152 expr = NULL_TREE;
11153 /* Build the return-statement. */
11154 statement = finish_return_stmt (expr);
11155 /* Look for the final `;'. */
11156 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11158 break;
11160 case RID_GOTO:
11161 if (parser->in_function_body
11162 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11164 error ("%<goto%> in %<constexpr%> function");
11165 cp_function_chain->invalid_constexpr = true;
11168 /* Create the goto-statement. */
11169 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11171 /* Issue a warning about this use of a GNU extension. */
11172 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11173 /* Consume the '*' token. */
11174 cp_lexer_consume_token (parser->lexer);
11175 /* Parse the dependent expression. */
11176 finish_goto_stmt (cp_parser_expression (parser));
11178 else
11179 finish_goto_stmt (cp_parser_identifier (parser));
11180 /* Look for the final `;'. */
11181 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11182 break;
11184 default:
11185 cp_parser_error (parser, "expected jump-statement");
11186 break;
11189 return statement;
11192 /* Parse a declaration-statement.
11194 declaration-statement:
11195 block-declaration */
11197 static void
11198 cp_parser_declaration_statement (cp_parser* parser)
11200 void *p;
11202 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11203 p = obstack_alloc (&declarator_obstack, 0);
11205 /* Parse the block-declaration. */
11206 cp_parser_block_declaration (parser, /*statement_p=*/true);
11208 /* Free any declarators allocated. */
11209 obstack_free (&declarator_obstack, p);
11212 /* Some dependent statements (like `if (cond) statement'), are
11213 implicitly in their own scope. In other words, if the statement is
11214 a single statement (as opposed to a compound-statement), it is
11215 none-the-less treated as if it were enclosed in braces. Any
11216 declarations appearing in the dependent statement are out of scope
11217 after control passes that point. This function parses a statement,
11218 but ensures that is in its own scope, even if it is not a
11219 compound-statement.
11221 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11222 is a (possibly labeled) if statement which is not enclosed in
11223 braces and has an else clause. This is used to implement
11224 -Wparentheses.
11226 Returns the new statement. */
11228 static tree
11229 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11231 tree statement;
11233 if (if_p != NULL)
11234 *if_p = false;
11236 /* Mark if () ; with a special NOP_EXPR. */
11237 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11239 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11240 cp_lexer_consume_token (parser->lexer);
11241 statement = add_stmt (build_empty_stmt (loc));
11243 /* if a compound is opened, we simply parse the statement directly. */
11244 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11245 statement = cp_parser_compound_statement (parser, NULL, false, false);
11246 /* If the token is not a `{', then we must take special action. */
11247 else
11249 /* Create a compound-statement. */
11250 statement = begin_compound_stmt (0);
11251 /* Parse the dependent-statement. */
11252 cp_parser_statement (parser, NULL_TREE, false, if_p);
11253 /* Finish the dummy compound-statement. */
11254 finish_compound_stmt (statement);
11257 /* Return the statement. */
11258 return statement;
11261 /* For some dependent statements (like `while (cond) statement'), we
11262 have already created a scope. Therefore, even if the dependent
11263 statement is a compound-statement, we do not want to create another
11264 scope. */
11266 static void
11267 cp_parser_already_scoped_statement (cp_parser* parser)
11269 /* If the token is a `{', then we must take special action. */
11270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11271 cp_parser_statement (parser, NULL_TREE, false, NULL);
11272 else
11274 /* Avoid calling cp_parser_compound_statement, so that we
11275 don't create a new scope. Do everything else by hand. */
11276 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11277 /* If the next keyword is `__label__' we have a label declaration. */
11278 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11279 cp_parser_label_declaration (parser);
11280 /* Parse an (optional) statement-seq. */
11281 cp_parser_statement_seq_opt (parser, NULL_TREE);
11282 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11286 /* Declarations [gram.dcl.dcl] */
11288 /* Parse an optional declaration-sequence.
11290 declaration-seq:
11291 declaration
11292 declaration-seq declaration */
11294 static void
11295 cp_parser_declaration_seq_opt (cp_parser* parser)
11297 while (true)
11299 cp_token *token;
11301 token = cp_lexer_peek_token (parser->lexer);
11303 if (token->type == CPP_CLOSE_BRACE
11304 || token->type == CPP_EOF
11305 || token->type == CPP_PRAGMA_EOL)
11306 break;
11308 if (token->type == CPP_SEMICOLON)
11310 /* A declaration consisting of a single semicolon is
11311 invalid. Allow it unless we're being pedantic. */
11312 cp_lexer_consume_token (parser->lexer);
11313 if (!in_system_header_at (input_location))
11314 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11315 continue;
11318 /* If we're entering or exiting a region that's implicitly
11319 extern "C", modify the lang context appropriately. */
11320 if (!parser->implicit_extern_c && token->implicit_extern_c)
11322 push_lang_context (lang_name_c);
11323 parser->implicit_extern_c = true;
11325 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11327 pop_lang_context ();
11328 parser->implicit_extern_c = false;
11331 if (token->type == CPP_PRAGMA)
11333 /* A top-level declaration can consist solely of a #pragma.
11334 A nested declaration cannot, so this is done here and not
11335 in cp_parser_declaration. (A #pragma at block scope is
11336 handled in cp_parser_statement.) */
11337 cp_parser_pragma (parser, pragma_external);
11338 continue;
11341 /* Parse the declaration itself. */
11342 cp_parser_declaration (parser);
11346 /* Parse a declaration.
11348 declaration:
11349 block-declaration
11350 function-definition
11351 template-declaration
11352 explicit-instantiation
11353 explicit-specialization
11354 linkage-specification
11355 namespace-definition
11357 GNU extension:
11359 declaration:
11360 __extension__ declaration */
11362 static void
11363 cp_parser_declaration (cp_parser* parser)
11365 cp_token token1;
11366 cp_token token2;
11367 int saved_pedantic;
11368 void *p;
11369 tree attributes = NULL_TREE;
11371 /* Check for the `__extension__' keyword. */
11372 if (cp_parser_extension_opt (parser, &saved_pedantic))
11374 /* Parse the qualified declaration. */
11375 cp_parser_declaration (parser);
11376 /* Restore the PEDANTIC flag. */
11377 pedantic = saved_pedantic;
11379 return;
11382 /* Try to figure out what kind of declaration is present. */
11383 token1 = *cp_lexer_peek_token (parser->lexer);
11385 if (token1.type != CPP_EOF)
11386 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11387 else
11389 token2.type = CPP_EOF;
11390 token2.keyword = RID_MAX;
11393 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11394 p = obstack_alloc (&declarator_obstack, 0);
11396 /* If the next token is `extern' and the following token is a string
11397 literal, then we have a linkage specification. */
11398 if (token1.keyword == RID_EXTERN
11399 && cp_parser_is_pure_string_literal (&token2))
11400 cp_parser_linkage_specification (parser);
11401 /* If the next token is `template', then we have either a template
11402 declaration, an explicit instantiation, or an explicit
11403 specialization. */
11404 else if (token1.keyword == RID_TEMPLATE)
11406 /* `template <>' indicates a template specialization. */
11407 if (token2.type == CPP_LESS
11408 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11409 cp_parser_explicit_specialization (parser);
11410 /* `template <' indicates a template declaration. */
11411 else if (token2.type == CPP_LESS)
11412 cp_parser_template_declaration (parser, /*member_p=*/false);
11413 /* Anything else must be an explicit instantiation. */
11414 else
11415 cp_parser_explicit_instantiation (parser);
11417 /* If the next token is `export', then we have a template
11418 declaration. */
11419 else if (token1.keyword == RID_EXPORT)
11420 cp_parser_template_declaration (parser, /*member_p=*/false);
11421 /* If the next token is `extern', 'static' or 'inline' and the one
11422 after that is `template', we have a GNU extended explicit
11423 instantiation directive. */
11424 else if (cp_parser_allow_gnu_extensions_p (parser)
11425 && (token1.keyword == RID_EXTERN
11426 || token1.keyword == RID_STATIC
11427 || token1.keyword == RID_INLINE)
11428 && token2.keyword == RID_TEMPLATE)
11429 cp_parser_explicit_instantiation (parser);
11430 /* If the next token is `namespace', check for a named or unnamed
11431 namespace definition. */
11432 else if (token1.keyword == RID_NAMESPACE
11433 && (/* A named namespace definition. */
11434 (token2.type == CPP_NAME
11435 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11436 != CPP_EQ))
11437 /* An unnamed namespace definition. */
11438 || token2.type == CPP_OPEN_BRACE
11439 || token2.keyword == RID_ATTRIBUTE))
11440 cp_parser_namespace_definition (parser);
11441 /* An inline (associated) namespace definition. */
11442 else if (token1.keyword == RID_INLINE
11443 && token2.keyword == RID_NAMESPACE)
11444 cp_parser_namespace_definition (parser);
11445 /* Objective-C++ declaration/definition. */
11446 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11447 cp_parser_objc_declaration (parser, NULL_TREE);
11448 else if (c_dialect_objc ()
11449 && token1.keyword == RID_ATTRIBUTE
11450 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11451 cp_parser_objc_declaration (parser, attributes);
11452 /* We must have either a block declaration or a function
11453 definition. */
11454 else
11456 /* At this point we may have a template declared by a concept
11457 introduction. */
11458 cp_parser_parse_tentatively (parser);
11459 cp_parser_template_declaration (parser, /*member_p=*/false);
11461 if (!cp_parser_parse_definitely (parser))
11462 /* Try to parse a block-declaration, or a function-definition. */
11463 cp_parser_block_declaration (parser, /*statement_p=*/false);
11466 /* Free any declarators allocated. */
11467 obstack_free (&declarator_obstack, p);
11470 /* Parse a block-declaration.
11472 block-declaration:
11473 simple-declaration
11474 asm-definition
11475 namespace-alias-definition
11476 using-declaration
11477 using-directive
11479 GNU Extension:
11481 block-declaration:
11482 __extension__ block-declaration
11484 C++0x Extension:
11486 block-declaration:
11487 static_assert-declaration
11489 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11490 part of a declaration-statement. */
11492 static void
11493 cp_parser_block_declaration (cp_parser *parser,
11494 bool statement_p)
11496 cp_token *token1;
11497 int saved_pedantic;
11499 /* Check for the `__extension__' keyword. */
11500 if (cp_parser_extension_opt (parser, &saved_pedantic))
11502 /* Parse the qualified declaration. */
11503 cp_parser_block_declaration (parser, statement_p);
11504 /* Restore the PEDANTIC flag. */
11505 pedantic = saved_pedantic;
11507 return;
11510 /* Peek at the next token to figure out which kind of declaration is
11511 present. */
11512 token1 = cp_lexer_peek_token (parser->lexer);
11514 /* If the next keyword is `asm', we have an asm-definition. */
11515 if (token1->keyword == RID_ASM)
11517 if (statement_p)
11518 cp_parser_commit_to_tentative_parse (parser);
11519 cp_parser_asm_definition (parser);
11521 /* If the next keyword is `namespace', we have a
11522 namespace-alias-definition. */
11523 else if (token1->keyword == RID_NAMESPACE)
11524 cp_parser_namespace_alias_definition (parser);
11525 /* If the next keyword is `using', we have a
11526 using-declaration, a using-directive, or an alias-declaration. */
11527 else if (token1->keyword == RID_USING)
11529 cp_token *token2;
11531 if (statement_p)
11532 cp_parser_commit_to_tentative_parse (parser);
11533 /* If the token after `using' is `namespace', then we have a
11534 using-directive. */
11535 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11536 if (token2->keyword == RID_NAMESPACE)
11537 cp_parser_using_directive (parser);
11538 /* If the second token after 'using' is '=', then we have an
11539 alias-declaration. */
11540 else if (cxx_dialect >= cxx11
11541 && token2->type == CPP_NAME
11542 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11543 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11544 cp_parser_alias_declaration (parser);
11545 /* Otherwise, it's a using-declaration. */
11546 else
11547 cp_parser_using_declaration (parser,
11548 /*access_declaration_p=*/false);
11550 /* If the next keyword is `__label__' we have a misplaced label
11551 declaration. */
11552 else if (token1->keyword == RID_LABEL)
11554 cp_lexer_consume_token (parser->lexer);
11555 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11556 cp_parser_skip_to_end_of_statement (parser);
11557 /* If the next token is now a `;', consume it. */
11558 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11559 cp_lexer_consume_token (parser->lexer);
11561 /* If the next token is `static_assert' we have a static assertion. */
11562 else if (token1->keyword == RID_STATIC_ASSERT)
11563 cp_parser_static_assert (parser, /*member_p=*/false);
11564 /* Anything else must be a simple-declaration. */
11565 else
11566 cp_parser_simple_declaration (parser, !statement_p,
11567 /*maybe_range_for_decl*/NULL);
11570 /* Parse a simple-declaration.
11572 simple-declaration:
11573 decl-specifier-seq [opt] init-declarator-list [opt] ;
11575 init-declarator-list:
11576 init-declarator
11577 init-declarator-list , init-declarator
11579 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11580 function-definition as a simple-declaration.
11582 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11583 parsed declaration if it is an uninitialized single declarator not followed
11584 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11585 if present, will not be consumed. */
11587 static void
11588 cp_parser_simple_declaration (cp_parser* parser,
11589 bool function_definition_allowed_p,
11590 tree *maybe_range_for_decl)
11592 cp_decl_specifier_seq decl_specifiers;
11593 int declares_class_or_enum;
11594 bool saw_declarator;
11595 location_t comma_loc = UNKNOWN_LOCATION;
11596 location_t init_loc = UNKNOWN_LOCATION;
11598 if (maybe_range_for_decl)
11599 *maybe_range_for_decl = NULL_TREE;
11601 /* Defer access checks until we know what is being declared; the
11602 checks for names appearing in the decl-specifier-seq should be
11603 done as if we were in the scope of the thing being declared. */
11604 push_deferring_access_checks (dk_deferred);
11606 /* Parse the decl-specifier-seq. We have to keep track of whether
11607 or not the decl-specifier-seq declares a named class or
11608 enumeration type, since that is the only case in which the
11609 init-declarator-list is allowed to be empty.
11611 [dcl.dcl]
11613 In a simple-declaration, the optional init-declarator-list can be
11614 omitted only when declaring a class or enumeration, that is when
11615 the decl-specifier-seq contains either a class-specifier, an
11616 elaborated-type-specifier, or an enum-specifier. */
11617 cp_parser_decl_specifier_seq (parser,
11618 CP_PARSER_FLAGS_OPTIONAL,
11619 &decl_specifiers,
11620 &declares_class_or_enum);
11621 /* We no longer need to defer access checks. */
11622 stop_deferring_access_checks ();
11624 /* In a block scope, a valid declaration must always have a
11625 decl-specifier-seq. By not trying to parse declarators, we can
11626 resolve the declaration/expression ambiguity more quickly. */
11627 if (!function_definition_allowed_p
11628 && !decl_specifiers.any_specifiers_p)
11630 cp_parser_error (parser, "expected declaration");
11631 goto done;
11634 /* If the next two tokens are both identifiers, the code is
11635 erroneous. The usual cause of this situation is code like:
11637 T t;
11639 where "T" should name a type -- but does not. */
11640 if (!decl_specifiers.any_type_specifiers_p
11641 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11643 /* If parsing tentatively, we should commit; we really are
11644 looking at a declaration. */
11645 cp_parser_commit_to_tentative_parse (parser);
11646 /* Give up. */
11647 goto done;
11650 /* If we have seen at least one decl-specifier, and the next token
11651 is not a parenthesis, then we must be looking at a declaration.
11652 (After "int (" we might be looking at a functional cast.) */
11653 if (decl_specifiers.any_specifiers_p
11654 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11655 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11656 && !cp_parser_error_occurred (parser))
11657 cp_parser_commit_to_tentative_parse (parser);
11659 /* Keep going until we hit the `;' at the end of the simple
11660 declaration. */
11661 saw_declarator = false;
11662 while (cp_lexer_next_token_is_not (parser->lexer,
11663 CPP_SEMICOLON))
11665 cp_token *token;
11666 bool function_definition_p;
11667 tree decl;
11669 if (saw_declarator)
11671 /* If we are processing next declarator, comma is expected */
11672 token = cp_lexer_peek_token (parser->lexer);
11673 gcc_assert (token->type == CPP_COMMA);
11674 cp_lexer_consume_token (parser->lexer);
11675 if (maybe_range_for_decl)
11677 *maybe_range_for_decl = error_mark_node;
11678 if (comma_loc == UNKNOWN_LOCATION)
11679 comma_loc = token->location;
11682 else
11683 saw_declarator = true;
11685 /* Parse the init-declarator. */
11686 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11687 /*checks=*/NULL,
11688 function_definition_allowed_p,
11689 /*member_p=*/false,
11690 declares_class_or_enum,
11691 &function_definition_p,
11692 maybe_range_for_decl,
11693 &init_loc);
11694 /* If an error occurred while parsing tentatively, exit quickly.
11695 (That usually happens when in the body of a function; each
11696 statement is treated as a declaration-statement until proven
11697 otherwise.) */
11698 if (cp_parser_error_occurred (parser))
11699 goto done;
11700 /* Handle function definitions specially. */
11701 if (function_definition_p)
11703 /* If the next token is a `,', then we are probably
11704 processing something like:
11706 void f() {}, *p;
11708 which is erroneous. */
11709 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11711 cp_token *token = cp_lexer_peek_token (parser->lexer);
11712 error_at (token->location,
11713 "mixing"
11714 " declarations and function-definitions is forbidden");
11716 /* Otherwise, we're done with the list of declarators. */
11717 else
11719 pop_deferring_access_checks ();
11720 return;
11723 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11724 *maybe_range_for_decl = decl;
11725 /* The next token should be either a `,' or a `;'. */
11726 token = cp_lexer_peek_token (parser->lexer);
11727 /* If it's a `,', there are more declarators to come. */
11728 if (token->type == CPP_COMMA)
11729 /* will be consumed next time around */;
11730 /* If it's a `;', we are done. */
11731 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11732 break;
11733 /* Anything else is an error. */
11734 else
11736 /* If we have already issued an error message we don't need
11737 to issue another one. */
11738 if (decl != error_mark_node
11739 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11740 cp_parser_error (parser, "expected %<,%> or %<;%>");
11741 /* Skip tokens until we reach the end of the statement. */
11742 cp_parser_skip_to_end_of_statement (parser);
11743 /* If the next token is now a `;', consume it. */
11744 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11745 cp_lexer_consume_token (parser->lexer);
11746 goto done;
11748 /* After the first time around, a function-definition is not
11749 allowed -- even if it was OK at first. For example:
11751 int i, f() {}
11753 is not valid. */
11754 function_definition_allowed_p = false;
11757 /* Issue an error message if no declarators are present, and the
11758 decl-specifier-seq does not itself declare a class or
11759 enumeration: [dcl.dcl]/3. */
11760 if (!saw_declarator)
11762 if (cp_parser_declares_only_class_p (parser))
11764 if (!declares_class_or_enum
11765 && decl_specifiers.type
11766 && OVERLOAD_TYPE_P (decl_specifiers.type))
11767 /* Ensure an error is issued anyway when finish_decltype_type,
11768 called via cp_parser_decl_specifier_seq, returns a class or
11769 an enumeration (c++/51786). */
11770 decl_specifiers.type = NULL_TREE;
11771 shadow_tag (&decl_specifiers);
11773 /* Perform any deferred access checks. */
11774 perform_deferred_access_checks (tf_warning_or_error);
11777 /* Consume the `;'. */
11778 if (!maybe_range_for_decl)
11779 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11780 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11782 if (init_loc != UNKNOWN_LOCATION)
11783 error_at (init_loc, "initializer in range-based %<for%> loop");
11784 if (comma_loc != UNKNOWN_LOCATION)
11785 error_at (comma_loc,
11786 "multiple declarations in range-based %<for%> loop");
11789 done:
11790 pop_deferring_access_checks ();
11793 /* Parse a decl-specifier-seq.
11795 decl-specifier-seq:
11796 decl-specifier-seq [opt] decl-specifier
11797 decl-specifier attribute-specifier-seq [opt] (C++11)
11799 decl-specifier:
11800 storage-class-specifier
11801 type-specifier
11802 function-specifier
11803 friend
11804 typedef
11806 GNU Extension:
11808 decl-specifier:
11809 attributes
11811 Concepts Extension:
11813 decl-specifier:
11814 concept
11817 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11819 The parser flags FLAGS is used to control type-specifier parsing.
11821 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11822 flags:
11824 1: one of the decl-specifiers is an elaborated-type-specifier
11825 (i.e., a type declaration)
11826 2: one of the decl-specifiers is an enum-specifier or a
11827 class-specifier (i.e., a type definition)
11831 static void
11832 cp_parser_decl_specifier_seq (cp_parser* parser,
11833 cp_parser_flags flags,
11834 cp_decl_specifier_seq *decl_specs,
11835 int* declares_class_or_enum)
11837 bool constructor_possible_p = !parser->in_declarator_p;
11838 bool found_decl_spec = false;
11839 cp_token *start_token = NULL;
11840 cp_decl_spec ds;
11842 /* Clear DECL_SPECS. */
11843 clear_decl_specs (decl_specs);
11845 /* Assume no class or enumeration type is declared. */
11846 *declares_class_or_enum = 0;
11848 /* Keep reading specifiers until there are no more to read. */
11849 while (true)
11851 bool constructor_p;
11852 cp_token *token;
11853 ds = ds_last;
11855 /* Peek at the next token. */
11856 token = cp_lexer_peek_token (parser->lexer);
11858 /* Save the first token of the decl spec list for error
11859 reporting. */
11860 if (!start_token)
11861 start_token = token;
11862 /* Handle attributes. */
11863 if (cp_next_tokens_can_be_attribute_p (parser))
11865 /* Parse the attributes. */
11866 tree attrs = cp_parser_attributes_opt (parser);
11868 /* In a sequence of declaration specifiers, c++11 attributes
11869 appertain to the type that precede them. In that case
11870 [dcl.spec]/1 says:
11872 The attribute-specifier-seq affects the type only for
11873 the declaration it appears in, not other declarations
11874 involving the same type.
11876 But for now let's force the user to position the
11877 attribute either at the beginning of the declaration or
11878 after the declarator-id, which would clearly mean that it
11879 applies to the declarator. */
11880 if (cxx11_attribute_p (attrs))
11882 if (!found_decl_spec)
11883 /* The c++11 attribute is at the beginning of the
11884 declaration. It appertains to the entity being
11885 declared. */;
11886 else
11888 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11890 /* This is an attribute following a
11891 class-specifier. */
11892 if (decl_specs->type_definition_p)
11893 warn_misplaced_attr_for_class_type (token->location,
11894 decl_specs->type);
11895 attrs = NULL_TREE;
11897 else
11899 decl_specs->std_attributes
11900 = chainon (decl_specs->std_attributes,
11901 attrs);
11902 if (decl_specs->locations[ds_std_attribute] == 0)
11903 decl_specs->locations[ds_std_attribute] = token->location;
11905 continue;
11909 decl_specs->attributes
11910 = chainon (decl_specs->attributes,
11911 attrs);
11912 if (decl_specs->locations[ds_attribute] == 0)
11913 decl_specs->locations[ds_attribute] = token->location;
11914 continue;
11916 /* Assume we will find a decl-specifier keyword. */
11917 found_decl_spec = true;
11918 /* If the next token is an appropriate keyword, we can simply
11919 add it to the list. */
11920 switch (token->keyword)
11922 /* decl-specifier:
11923 friend
11924 constexpr */
11925 case RID_FRIEND:
11926 if (!at_class_scope_p ())
11928 error_at (token->location, "%<friend%> used outside of class");
11929 cp_lexer_purge_token (parser->lexer);
11931 else
11933 ds = ds_friend;
11934 /* Consume the token. */
11935 cp_lexer_consume_token (parser->lexer);
11937 break;
11939 case RID_CONSTEXPR:
11940 ds = ds_constexpr;
11941 cp_lexer_consume_token (parser->lexer);
11942 break;
11944 case RID_CONCEPT:
11945 ds = ds_concept;
11946 cp_lexer_consume_token (parser->lexer);
11947 break;
11949 /* function-specifier:
11950 inline
11951 virtual
11952 explicit */
11953 case RID_INLINE:
11954 case RID_VIRTUAL:
11955 case RID_EXPLICIT:
11956 cp_parser_function_specifier_opt (parser, decl_specs);
11957 break;
11959 /* decl-specifier:
11960 typedef */
11961 case RID_TYPEDEF:
11962 ds = ds_typedef;
11963 /* Consume the token. */
11964 cp_lexer_consume_token (parser->lexer);
11965 /* A constructor declarator cannot appear in a typedef. */
11966 constructor_possible_p = false;
11967 /* The "typedef" keyword can only occur in a declaration; we
11968 may as well commit at this point. */
11969 cp_parser_commit_to_tentative_parse (parser);
11971 if (decl_specs->storage_class != sc_none)
11972 decl_specs->conflicting_specifiers_p = true;
11973 break;
11975 /* storage-class-specifier:
11976 auto
11977 register
11978 static
11979 extern
11980 mutable
11982 GNU Extension:
11983 thread */
11984 case RID_AUTO:
11985 if (cxx_dialect == cxx98)
11987 /* Consume the token. */
11988 cp_lexer_consume_token (parser->lexer);
11990 /* Complain about `auto' as a storage specifier, if
11991 we're complaining about C++0x compatibility. */
11992 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11993 " changes meaning in C++11; please remove it");
11995 /* Set the storage class anyway. */
11996 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11997 token);
11999 else
12000 /* C++0x auto type-specifier. */
12001 found_decl_spec = false;
12002 break;
12004 case RID_REGISTER:
12005 case RID_STATIC:
12006 case RID_EXTERN:
12007 case RID_MUTABLE:
12008 /* Consume the token. */
12009 cp_lexer_consume_token (parser->lexer);
12010 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12011 token);
12012 break;
12013 case RID_THREAD:
12014 /* Consume the token. */
12015 ds = ds_thread;
12016 cp_lexer_consume_token (parser->lexer);
12017 break;
12019 default:
12020 /* We did not yet find a decl-specifier yet. */
12021 found_decl_spec = false;
12022 break;
12025 if (found_decl_spec
12026 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12027 && token->keyword != RID_CONSTEXPR)
12028 error ("decl-specifier invalid in condition");
12030 if (ds != ds_last)
12031 set_and_check_decl_spec_loc (decl_specs, ds, token);
12033 /* Constructors are a special case. The `S' in `S()' is not a
12034 decl-specifier; it is the beginning of the declarator. */
12035 constructor_p
12036 = (!found_decl_spec
12037 && constructor_possible_p
12038 && (cp_parser_constructor_declarator_p
12039 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12041 /* If we don't have a DECL_SPEC yet, then we must be looking at
12042 a type-specifier. */
12043 if (!found_decl_spec && !constructor_p)
12045 int decl_spec_declares_class_or_enum;
12046 bool is_cv_qualifier;
12047 tree type_spec;
12049 type_spec
12050 = cp_parser_type_specifier (parser, flags,
12051 decl_specs,
12052 /*is_declaration=*/true,
12053 &decl_spec_declares_class_or_enum,
12054 &is_cv_qualifier);
12055 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12057 /* If this type-specifier referenced a user-defined type
12058 (a typedef, class-name, etc.), then we can't allow any
12059 more such type-specifiers henceforth.
12061 [dcl.spec]
12063 The longest sequence of decl-specifiers that could
12064 possibly be a type name is taken as the
12065 decl-specifier-seq of a declaration. The sequence shall
12066 be self-consistent as described below.
12068 [dcl.type]
12070 As a general rule, at most one type-specifier is allowed
12071 in the complete decl-specifier-seq of a declaration. The
12072 only exceptions are the following:
12074 -- const or volatile can be combined with any other
12075 type-specifier.
12077 -- signed or unsigned can be combined with char, long,
12078 short, or int.
12080 -- ..
12082 Example:
12084 typedef char* Pc;
12085 void g (const int Pc);
12087 Here, Pc is *not* part of the decl-specifier seq; it's
12088 the declarator. Therefore, once we see a type-specifier
12089 (other than a cv-qualifier), we forbid any additional
12090 user-defined types. We *do* still allow things like `int
12091 int' to be considered a decl-specifier-seq, and issue the
12092 error message later. */
12093 if (type_spec && !is_cv_qualifier)
12094 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12095 /* A constructor declarator cannot follow a type-specifier. */
12096 if (type_spec)
12098 constructor_possible_p = false;
12099 found_decl_spec = true;
12100 if (!is_cv_qualifier)
12101 decl_specs->any_type_specifiers_p = true;
12105 /* If we still do not have a DECL_SPEC, then there are no more
12106 decl-specifiers. */
12107 if (!found_decl_spec)
12108 break;
12110 decl_specs->any_specifiers_p = true;
12111 /* After we see one decl-specifier, further decl-specifiers are
12112 always optional. */
12113 flags |= CP_PARSER_FLAGS_OPTIONAL;
12116 /* Don't allow a friend specifier with a class definition. */
12117 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12118 && (*declares_class_or_enum & 2))
12119 error_at (decl_specs->locations[ds_friend],
12120 "class definition may not be declared a friend");
12123 /* Parse an (optional) storage-class-specifier.
12125 storage-class-specifier:
12126 auto
12127 register
12128 static
12129 extern
12130 mutable
12132 GNU Extension:
12134 storage-class-specifier:
12135 thread
12137 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12139 static tree
12140 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12142 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12144 case RID_AUTO:
12145 if (cxx_dialect != cxx98)
12146 return NULL_TREE;
12147 /* Fall through for C++98. */
12149 case RID_REGISTER:
12150 case RID_STATIC:
12151 case RID_EXTERN:
12152 case RID_MUTABLE:
12153 case RID_THREAD:
12154 /* Consume the token. */
12155 return cp_lexer_consume_token (parser->lexer)->u.value;
12157 default:
12158 return NULL_TREE;
12162 /* Parse an (optional) function-specifier.
12164 function-specifier:
12165 inline
12166 virtual
12167 explicit
12169 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12170 Updates DECL_SPECS, if it is non-NULL. */
12172 static tree
12173 cp_parser_function_specifier_opt (cp_parser* parser,
12174 cp_decl_specifier_seq *decl_specs)
12176 cp_token *token = cp_lexer_peek_token (parser->lexer);
12177 switch (token->keyword)
12179 case RID_INLINE:
12180 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12181 break;
12183 case RID_VIRTUAL:
12184 /* 14.5.2.3 [temp.mem]
12186 A member function template shall not be virtual. */
12187 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12188 error_at (token->location, "templates may not be %<virtual%>");
12189 else
12190 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12191 break;
12193 case RID_EXPLICIT:
12194 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12195 break;
12197 default:
12198 return NULL_TREE;
12201 /* Consume the token. */
12202 return cp_lexer_consume_token (parser->lexer)->u.value;
12205 /* Parse a linkage-specification.
12207 linkage-specification:
12208 extern string-literal { declaration-seq [opt] }
12209 extern string-literal declaration */
12211 static void
12212 cp_parser_linkage_specification (cp_parser* parser)
12214 tree linkage;
12216 /* Look for the `extern' keyword. */
12217 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12219 /* Look for the string-literal. */
12220 linkage = cp_parser_string_literal (parser, false, false);
12222 /* Transform the literal into an identifier. If the literal is a
12223 wide-character string, or contains embedded NULs, then we can't
12224 handle it as the user wants. */
12225 if (strlen (TREE_STRING_POINTER (linkage))
12226 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12228 cp_parser_error (parser, "invalid linkage-specification");
12229 /* Assume C++ linkage. */
12230 linkage = lang_name_cplusplus;
12232 else
12233 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12235 /* We're now using the new linkage. */
12236 push_lang_context (linkage);
12238 /* If the next token is a `{', then we're using the first
12239 production. */
12240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12242 cp_ensure_no_omp_declare_simd (parser);
12244 /* Consume the `{' token. */
12245 cp_lexer_consume_token (parser->lexer);
12246 /* Parse the declarations. */
12247 cp_parser_declaration_seq_opt (parser);
12248 /* Look for the closing `}'. */
12249 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12251 /* Otherwise, there's just one declaration. */
12252 else
12254 bool saved_in_unbraced_linkage_specification_p;
12256 saved_in_unbraced_linkage_specification_p
12257 = parser->in_unbraced_linkage_specification_p;
12258 parser->in_unbraced_linkage_specification_p = true;
12259 cp_parser_declaration (parser);
12260 parser->in_unbraced_linkage_specification_p
12261 = saved_in_unbraced_linkage_specification_p;
12264 /* We're done with the linkage-specification. */
12265 pop_lang_context ();
12268 /* Parse a static_assert-declaration.
12270 static_assert-declaration:
12271 static_assert ( constant-expression , string-literal ) ;
12273 If MEMBER_P, this static_assert is a class member. */
12275 static void
12276 cp_parser_static_assert(cp_parser *parser, bool member_p)
12278 tree condition;
12279 tree message;
12280 cp_token *token;
12281 location_t saved_loc;
12282 bool dummy;
12284 /* Peek at the `static_assert' token so we can keep track of exactly
12285 where the static assertion started. */
12286 token = cp_lexer_peek_token (parser->lexer);
12287 saved_loc = token->location;
12289 /* Look for the `static_assert' keyword. */
12290 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12291 RT_STATIC_ASSERT))
12292 return;
12294 /* We know we are in a static assertion; commit to any tentative
12295 parse. */
12296 if (cp_parser_parsing_tentatively (parser))
12297 cp_parser_commit_to_tentative_parse (parser);
12299 /* Parse the `(' starting the static assertion condition. */
12300 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12302 /* Parse the constant-expression. Allow a non-constant expression
12303 here in order to give better diagnostics in finish_static_assert. */
12304 condition =
12305 cp_parser_constant_expression (parser,
12306 /*allow_non_constant_p=*/true,
12307 /*non_constant_p=*/&dummy);
12309 /* Parse the separating `,'. */
12310 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12312 /* Parse the string-literal message. */
12313 message = cp_parser_string_literal (parser,
12314 /*translate=*/false,
12315 /*wide_ok=*/true);
12317 /* A `)' completes the static assertion. */
12318 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12319 cp_parser_skip_to_closing_parenthesis (parser,
12320 /*recovering=*/true,
12321 /*or_comma=*/false,
12322 /*consume_paren=*/true);
12324 /* A semicolon terminates the declaration. */
12325 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12327 /* Complete the static assertion, which may mean either processing
12328 the static assert now or saving it for template instantiation. */
12329 finish_static_assert (condition, message, saved_loc, member_p);
12332 /* Parse the expression in decltype ( expression ). */
12334 static tree
12335 cp_parser_decltype_expr (cp_parser *parser,
12336 bool &id_expression_or_member_access_p)
12338 cp_token *id_expr_start_token;
12339 tree expr;
12341 /* First, try parsing an id-expression. */
12342 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12343 cp_parser_parse_tentatively (parser);
12344 expr = cp_parser_id_expression (parser,
12345 /*template_keyword_p=*/false,
12346 /*check_dependency_p=*/true,
12347 /*template_p=*/NULL,
12348 /*declarator_p=*/false,
12349 /*optional_p=*/false);
12351 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12353 bool non_integral_constant_expression_p = false;
12354 tree id_expression = expr;
12355 cp_id_kind idk;
12356 const char *error_msg;
12358 if (identifier_p (expr))
12359 /* Lookup the name we got back from the id-expression. */
12360 expr = cp_parser_lookup_name_simple (parser, expr,
12361 id_expr_start_token->location);
12363 if (expr
12364 && expr != error_mark_node
12365 && TREE_CODE (expr) != TYPE_DECL
12366 && (TREE_CODE (expr) != BIT_NOT_EXPR
12367 || !TYPE_P (TREE_OPERAND (expr, 0)))
12368 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12370 /* Complete lookup of the id-expression. */
12371 expr = (finish_id_expression
12372 (id_expression, expr, parser->scope, &idk,
12373 /*integral_constant_expression_p=*/false,
12374 /*allow_non_integral_constant_expression_p=*/true,
12375 &non_integral_constant_expression_p,
12376 /*template_p=*/false,
12377 /*done=*/true,
12378 /*address_p=*/false,
12379 /*template_arg_p=*/false,
12380 &error_msg,
12381 id_expr_start_token->location));
12383 if (expr == error_mark_node)
12384 /* We found an id-expression, but it was something that we
12385 should not have found. This is an error, not something
12386 we can recover from, so note that we found an
12387 id-expression and we'll recover as gracefully as
12388 possible. */
12389 id_expression_or_member_access_p = true;
12392 if (expr
12393 && expr != error_mark_node
12394 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12395 /* We have an id-expression. */
12396 id_expression_or_member_access_p = true;
12399 if (!id_expression_or_member_access_p)
12401 /* Abort the id-expression parse. */
12402 cp_parser_abort_tentative_parse (parser);
12404 /* Parsing tentatively, again. */
12405 cp_parser_parse_tentatively (parser);
12407 /* Parse a class member access. */
12408 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12409 /*cast_p=*/false, /*decltype*/true,
12410 /*member_access_only_p=*/true, NULL);
12412 if (expr
12413 && expr != error_mark_node
12414 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12415 /* We have an id-expression. */
12416 id_expression_or_member_access_p = true;
12419 if (id_expression_or_member_access_p)
12420 /* We have parsed the complete id-expression or member access. */
12421 cp_parser_parse_definitely (parser);
12422 else
12424 /* Abort our attempt to parse an id-expression or member access
12425 expression. */
12426 cp_parser_abort_tentative_parse (parser);
12428 /* Parse a full expression. */
12429 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12430 /*decltype_p=*/true);
12433 return expr;
12436 /* Parse a `decltype' type. Returns the type.
12438 simple-type-specifier:
12439 decltype ( expression )
12440 C++14 proposal:
12441 decltype ( auto ) */
12443 static tree
12444 cp_parser_decltype (cp_parser *parser)
12446 tree expr;
12447 bool id_expression_or_member_access_p = false;
12448 const char *saved_message;
12449 bool saved_integral_constant_expression_p;
12450 bool saved_non_integral_constant_expression_p;
12451 bool saved_greater_than_is_operator_p;
12452 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12454 if (start_token->type == CPP_DECLTYPE)
12456 /* Already parsed. */
12457 cp_lexer_consume_token (parser->lexer);
12458 return start_token->u.value;
12461 /* Look for the `decltype' token. */
12462 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12463 return error_mark_node;
12465 /* Parse the opening `('. */
12466 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12467 return error_mark_node;
12469 /* decltype (auto) */
12470 if (cxx_dialect >= cxx14
12471 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12473 cp_lexer_consume_token (parser->lexer);
12474 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12475 return error_mark_node;
12476 expr = make_decltype_auto ();
12477 AUTO_IS_DECLTYPE (expr) = true;
12478 goto rewrite;
12481 /* Types cannot be defined in a `decltype' expression. Save away the
12482 old message. */
12483 saved_message = parser->type_definition_forbidden_message;
12485 /* And create the new one. */
12486 parser->type_definition_forbidden_message
12487 = G_("types may not be defined in %<decltype%> expressions");
12489 /* The restrictions on constant-expressions do not apply inside
12490 decltype expressions. */
12491 saved_integral_constant_expression_p
12492 = parser->integral_constant_expression_p;
12493 saved_non_integral_constant_expression_p
12494 = parser->non_integral_constant_expression_p;
12495 parser->integral_constant_expression_p = false;
12497 /* Within a parenthesized expression, a `>' token is always
12498 the greater-than operator. */
12499 saved_greater_than_is_operator_p
12500 = parser->greater_than_is_operator_p;
12501 parser->greater_than_is_operator_p = true;
12503 /* Do not actually evaluate the expression. */
12504 ++cp_unevaluated_operand;
12506 /* Do not warn about problems with the expression. */
12507 ++c_inhibit_evaluation_warnings;
12509 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12511 /* Go back to evaluating expressions. */
12512 --cp_unevaluated_operand;
12513 --c_inhibit_evaluation_warnings;
12515 /* The `>' token might be the end of a template-id or
12516 template-parameter-list now. */
12517 parser->greater_than_is_operator_p
12518 = saved_greater_than_is_operator_p;
12520 /* Restore the old message and the integral constant expression
12521 flags. */
12522 parser->type_definition_forbidden_message = saved_message;
12523 parser->integral_constant_expression_p
12524 = saved_integral_constant_expression_p;
12525 parser->non_integral_constant_expression_p
12526 = saved_non_integral_constant_expression_p;
12528 /* Parse to the closing `)'. */
12529 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12531 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12532 /*consume_paren=*/true);
12533 return error_mark_node;
12536 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12537 tf_warning_or_error);
12539 rewrite:
12540 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12541 it again. */
12542 start_token->type = CPP_DECLTYPE;
12543 start_token->u.value = expr;
12544 start_token->keyword = RID_MAX;
12545 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12547 return expr;
12550 /* Special member functions [gram.special] */
12552 /* Parse a conversion-function-id.
12554 conversion-function-id:
12555 operator conversion-type-id
12557 Returns an IDENTIFIER_NODE representing the operator. */
12559 static tree
12560 cp_parser_conversion_function_id (cp_parser* parser)
12562 tree type;
12563 tree saved_scope;
12564 tree saved_qualifying_scope;
12565 tree saved_object_scope;
12566 tree pushed_scope = NULL_TREE;
12568 /* Look for the `operator' token. */
12569 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12570 return error_mark_node;
12571 /* When we parse the conversion-type-id, the current scope will be
12572 reset. However, we need that information in able to look up the
12573 conversion function later, so we save it here. */
12574 saved_scope = parser->scope;
12575 saved_qualifying_scope = parser->qualifying_scope;
12576 saved_object_scope = parser->object_scope;
12577 /* We must enter the scope of the class so that the names of
12578 entities declared within the class are available in the
12579 conversion-type-id. For example, consider:
12581 struct S {
12582 typedef int I;
12583 operator I();
12586 S::operator I() { ... }
12588 In order to see that `I' is a type-name in the definition, we
12589 must be in the scope of `S'. */
12590 if (saved_scope)
12591 pushed_scope = push_scope (saved_scope);
12592 /* Parse the conversion-type-id. */
12593 type = cp_parser_conversion_type_id (parser);
12594 /* Leave the scope of the class, if any. */
12595 if (pushed_scope)
12596 pop_scope (pushed_scope);
12597 /* Restore the saved scope. */
12598 parser->scope = saved_scope;
12599 parser->qualifying_scope = saved_qualifying_scope;
12600 parser->object_scope = saved_object_scope;
12601 /* If the TYPE is invalid, indicate failure. */
12602 if (type == error_mark_node)
12603 return error_mark_node;
12604 return mangle_conv_op_name_for_type (type);
12607 /* Parse a conversion-type-id:
12609 conversion-type-id:
12610 type-specifier-seq conversion-declarator [opt]
12612 Returns the TYPE specified. */
12614 static tree
12615 cp_parser_conversion_type_id (cp_parser* parser)
12617 tree attributes;
12618 cp_decl_specifier_seq type_specifiers;
12619 cp_declarator *declarator;
12620 tree type_specified;
12621 const char *saved_message;
12623 /* Parse the attributes. */
12624 attributes = cp_parser_attributes_opt (parser);
12626 saved_message = parser->type_definition_forbidden_message;
12627 parser->type_definition_forbidden_message
12628 = G_("types may not be defined in a conversion-type-id");
12630 /* Parse the type-specifiers. */
12631 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12632 /*is_trailing_return=*/false,
12633 &type_specifiers);
12635 parser->type_definition_forbidden_message = saved_message;
12637 /* If that didn't work, stop. */
12638 if (type_specifiers.type == error_mark_node)
12639 return error_mark_node;
12640 /* Parse the conversion-declarator. */
12641 declarator = cp_parser_conversion_declarator_opt (parser);
12643 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12644 /*initialized=*/0, &attributes);
12645 if (attributes)
12646 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12648 /* Don't give this error when parsing tentatively. This happens to
12649 work because we always parse this definitively once. */
12650 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12651 && type_uses_auto (type_specified))
12653 if (cxx_dialect < cxx14)
12655 error ("invalid use of %<auto%> in conversion operator");
12656 return error_mark_node;
12658 else if (template_parm_scope_p ())
12659 warning (0, "use of %<auto%> in member template "
12660 "conversion operator can never be deduced");
12663 return type_specified;
12666 /* Parse an (optional) conversion-declarator.
12668 conversion-declarator:
12669 ptr-operator conversion-declarator [opt]
12673 static cp_declarator *
12674 cp_parser_conversion_declarator_opt (cp_parser* parser)
12676 enum tree_code code;
12677 tree class_type, std_attributes = NULL_TREE;
12678 cp_cv_quals cv_quals;
12680 /* We don't know if there's a ptr-operator next, or not. */
12681 cp_parser_parse_tentatively (parser);
12682 /* Try the ptr-operator. */
12683 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12684 &std_attributes);
12685 /* If it worked, look for more conversion-declarators. */
12686 if (cp_parser_parse_definitely (parser))
12688 cp_declarator *declarator;
12690 /* Parse another optional declarator. */
12691 declarator = cp_parser_conversion_declarator_opt (parser);
12693 declarator = cp_parser_make_indirect_declarator
12694 (code, class_type, cv_quals, declarator, std_attributes);
12696 return declarator;
12699 return NULL;
12702 /* Parse an (optional) ctor-initializer.
12704 ctor-initializer:
12705 : mem-initializer-list
12707 Returns TRUE iff the ctor-initializer was actually present. */
12709 static bool
12710 cp_parser_ctor_initializer_opt (cp_parser* parser)
12712 /* If the next token is not a `:', then there is no
12713 ctor-initializer. */
12714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12716 /* Do default initialization of any bases and members. */
12717 if (DECL_CONSTRUCTOR_P (current_function_decl))
12718 finish_mem_initializers (NULL_TREE);
12720 return false;
12723 /* Consume the `:' token. */
12724 cp_lexer_consume_token (parser->lexer);
12725 /* And the mem-initializer-list. */
12726 cp_parser_mem_initializer_list (parser);
12728 return true;
12731 /* Parse a mem-initializer-list.
12733 mem-initializer-list:
12734 mem-initializer ... [opt]
12735 mem-initializer ... [opt] , mem-initializer-list */
12737 static void
12738 cp_parser_mem_initializer_list (cp_parser* parser)
12740 tree mem_initializer_list = NULL_TREE;
12741 tree target_ctor = error_mark_node;
12742 cp_token *token = cp_lexer_peek_token (parser->lexer);
12744 /* Let the semantic analysis code know that we are starting the
12745 mem-initializer-list. */
12746 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12747 error_at (token->location,
12748 "only constructors take member initializers");
12750 /* Loop through the list. */
12751 while (true)
12753 tree mem_initializer;
12755 token = cp_lexer_peek_token (parser->lexer);
12756 /* Parse the mem-initializer. */
12757 mem_initializer = cp_parser_mem_initializer (parser);
12758 /* If the next token is a `...', we're expanding member initializers. */
12759 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12761 /* Consume the `...'. */
12762 cp_lexer_consume_token (parser->lexer);
12764 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12765 can be expanded but members cannot. */
12766 if (mem_initializer != error_mark_node
12767 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12769 error_at (token->location,
12770 "cannot expand initializer for member %<%D%>",
12771 TREE_PURPOSE (mem_initializer));
12772 mem_initializer = error_mark_node;
12775 /* Construct the pack expansion type. */
12776 if (mem_initializer != error_mark_node)
12777 mem_initializer = make_pack_expansion (mem_initializer);
12779 if (target_ctor != error_mark_node
12780 && mem_initializer != error_mark_node)
12782 error ("mem-initializer for %qD follows constructor delegation",
12783 TREE_PURPOSE (mem_initializer));
12784 mem_initializer = error_mark_node;
12786 /* Look for a target constructor. */
12787 if (mem_initializer != error_mark_node
12788 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12789 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12791 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12792 if (mem_initializer_list)
12794 error ("constructor delegation follows mem-initializer for %qD",
12795 TREE_PURPOSE (mem_initializer_list));
12796 mem_initializer = error_mark_node;
12798 target_ctor = mem_initializer;
12800 /* Add it to the list, unless it was erroneous. */
12801 if (mem_initializer != error_mark_node)
12803 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12804 mem_initializer_list = mem_initializer;
12806 /* If the next token is not a `,', we're done. */
12807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12808 break;
12809 /* Consume the `,' token. */
12810 cp_lexer_consume_token (parser->lexer);
12813 /* Perform semantic analysis. */
12814 if (DECL_CONSTRUCTOR_P (current_function_decl))
12815 finish_mem_initializers (mem_initializer_list);
12818 /* Parse a mem-initializer.
12820 mem-initializer:
12821 mem-initializer-id ( expression-list [opt] )
12822 mem-initializer-id braced-init-list
12824 GNU extension:
12826 mem-initializer:
12827 ( expression-list [opt] )
12829 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12830 class) or FIELD_DECL (for a non-static data member) to initialize;
12831 the TREE_VALUE is the expression-list. An empty initialization
12832 list is represented by void_list_node. */
12834 static tree
12835 cp_parser_mem_initializer (cp_parser* parser)
12837 tree mem_initializer_id;
12838 tree expression_list;
12839 tree member;
12840 cp_token *token = cp_lexer_peek_token (parser->lexer);
12842 /* Find out what is being initialized. */
12843 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12845 permerror (token->location,
12846 "anachronistic old-style base class initializer");
12847 mem_initializer_id = NULL_TREE;
12849 else
12851 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12852 if (mem_initializer_id == error_mark_node)
12853 return mem_initializer_id;
12855 member = expand_member_init (mem_initializer_id);
12856 if (member && !DECL_P (member))
12857 in_base_initializer = 1;
12859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12861 bool expr_non_constant_p;
12862 cp_lexer_set_source_position (parser->lexer);
12863 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12864 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12865 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12866 expression_list = build_tree_list (NULL_TREE, expression_list);
12868 else
12870 vec<tree, va_gc> *vec;
12871 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12872 /*cast_p=*/false,
12873 /*allow_expansion_p=*/true,
12874 /*non_constant_p=*/NULL);
12875 if (vec == NULL)
12876 return error_mark_node;
12877 expression_list = build_tree_list_vec (vec);
12878 release_tree_vector (vec);
12881 if (expression_list == error_mark_node)
12882 return error_mark_node;
12883 if (!expression_list)
12884 expression_list = void_type_node;
12886 in_base_initializer = 0;
12888 return member ? build_tree_list (member, expression_list) : error_mark_node;
12891 /* Parse a mem-initializer-id.
12893 mem-initializer-id:
12894 :: [opt] nested-name-specifier [opt] class-name
12895 identifier
12897 Returns a TYPE indicating the class to be initializer for the first
12898 production. Returns an IDENTIFIER_NODE indicating the data member
12899 to be initialized for the second production. */
12901 static tree
12902 cp_parser_mem_initializer_id (cp_parser* parser)
12904 bool global_scope_p;
12905 bool nested_name_specifier_p;
12906 bool template_p = false;
12907 tree id;
12909 cp_token *token = cp_lexer_peek_token (parser->lexer);
12911 /* `typename' is not allowed in this context ([temp.res]). */
12912 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12914 error_at (token->location,
12915 "keyword %<typename%> not allowed in this context (a qualified "
12916 "member initializer is implicitly a type)");
12917 cp_lexer_consume_token (parser->lexer);
12919 /* Look for the optional `::' operator. */
12920 global_scope_p
12921 = (cp_parser_global_scope_opt (parser,
12922 /*current_scope_valid_p=*/false)
12923 != NULL_TREE);
12924 /* Look for the optional nested-name-specifier. The simplest way to
12925 implement:
12927 [temp.res]
12929 The keyword `typename' is not permitted in a base-specifier or
12930 mem-initializer; in these contexts a qualified name that
12931 depends on a template-parameter is implicitly assumed to be a
12932 type name.
12934 is to assume that we have seen the `typename' keyword at this
12935 point. */
12936 nested_name_specifier_p
12937 = (cp_parser_nested_name_specifier_opt (parser,
12938 /*typename_keyword_p=*/true,
12939 /*check_dependency_p=*/true,
12940 /*type_p=*/true,
12941 /*is_declaration=*/true)
12942 != NULL_TREE);
12943 if (nested_name_specifier_p)
12944 template_p = cp_parser_optional_template_keyword (parser);
12945 /* If there is a `::' operator or a nested-name-specifier, then we
12946 are definitely looking for a class-name. */
12947 if (global_scope_p || nested_name_specifier_p)
12948 return cp_parser_class_name (parser,
12949 /*typename_keyword_p=*/true,
12950 /*template_keyword_p=*/template_p,
12951 typename_type,
12952 /*check_dependency_p=*/true,
12953 /*class_head_p=*/false,
12954 /*is_declaration=*/true);
12955 /* Otherwise, we could also be looking for an ordinary identifier. */
12956 cp_parser_parse_tentatively (parser);
12957 /* Try a class-name. */
12958 id = cp_parser_class_name (parser,
12959 /*typename_keyword_p=*/true,
12960 /*template_keyword_p=*/false,
12961 none_type,
12962 /*check_dependency_p=*/true,
12963 /*class_head_p=*/false,
12964 /*is_declaration=*/true);
12965 /* If we found one, we're done. */
12966 if (cp_parser_parse_definitely (parser))
12967 return id;
12968 /* Otherwise, look for an ordinary identifier. */
12969 return cp_parser_identifier (parser);
12972 /* Overloading [gram.over] */
12974 /* Parse an operator-function-id.
12976 operator-function-id:
12977 operator operator
12979 Returns an IDENTIFIER_NODE for the operator which is a
12980 human-readable spelling of the identifier, e.g., `operator +'. */
12982 static tree
12983 cp_parser_operator_function_id (cp_parser* parser)
12985 /* Look for the `operator' keyword. */
12986 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12987 return error_mark_node;
12988 /* And then the name of the operator itself. */
12989 return cp_parser_operator (parser);
12992 /* Return an identifier node for a user-defined literal operator.
12993 The suffix identifier is chained to the operator name identifier. */
12995 static tree
12996 cp_literal_operator_id (const char* name)
12998 tree identifier;
12999 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13000 + strlen (name) + 10);
13001 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13002 identifier = get_identifier (buffer);
13004 return identifier;
13007 /* Parse an operator.
13009 operator:
13010 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13011 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13012 || ++ -- , ->* -> () []
13014 GNU Extensions:
13016 operator:
13017 <? >? <?= >?=
13019 Returns an IDENTIFIER_NODE for the operator which is a
13020 human-readable spelling of the identifier, e.g., `operator +'. */
13022 static tree
13023 cp_parser_operator (cp_parser* parser)
13025 tree id = NULL_TREE;
13026 cp_token *token;
13027 bool utf8 = false;
13029 /* Peek at the next token. */
13030 token = cp_lexer_peek_token (parser->lexer);
13031 /* Figure out which operator we have. */
13032 switch (token->type)
13034 case CPP_KEYWORD:
13036 enum tree_code op;
13038 /* The keyword should be either `new' or `delete'. */
13039 if (token->keyword == RID_NEW)
13040 op = NEW_EXPR;
13041 else if (token->keyword == RID_DELETE)
13042 op = DELETE_EXPR;
13043 else
13044 break;
13046 /* Consume the `new' or `delete' token. */
13047 cp_lexer_consume_token (parser->lexer);
13049 /* Peek at the next token. */
13050 token = cp_lexer_peek_token (parser->lexer);
13051 /* If it's a `[' token then this is the array variant of the
13052 operator. */
13053 if (token->type == CPP_OPEN_SQUARE)
13055 /* Consume the `[' token. */
13056 cp_lexer_consume_token (parser->lexer);
13057 /* Look for the `]' token. */
13058 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13059 id = ansi_opname (op == NEW_EXPR
13060 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13062 /* Otherwise, we have the non-array variant. */
13063 else
13064 id = ansi_opname (op);
13066 return id;
13069 case CPP_PLUS:
13070 id = ansi_opname (PLUS_EXPR);
13071 break;
13073 case CPP_MINUS:
13074 id = ansi_opname (MINUS_EXPR);
13075 break;
13077 case CPP_MULT:
13078 id = ansi_opname (MULT_EXPR);
13079 break;
13081 case CPP_DIV:
13082 id = ansi_opname (TRUNC_DIV_EXPR);
13083 break;
13085 case CPP_MOD:
13086 id = ansi_opname (TRUNC_MOD_EXPR);
13087 break;
13089 case CPP_XOR:
13090 id = ansi_opname (BIT_XOR_EXPR);
13091 break;
13093 case CPP_AND:
13094 id = ansi_opname (BIT_AND_EXPR);
13095 break;
13097 case CPP_OR:
13098 id = ansi_opname (BIT_IOR_EXPR);
13099 break;
13101 case CPP_COMPL:
13102 id = ansi_opname (BIT_NOT_EXPR);
13103 break;
13105 case CPP_NOT:
13106 id = ansi_opname (TRUTH_NOT_EXPR);
13107 break;
13109 case CPP_EQ:
13110 id = ansi_assopname (NOP_EXPR);
13111 break;
13113 case CPP_LESS:
13114 id = ansi_opname (LT_EXPR);
13115 break;
13117 case CPP_GREATER:
13118 id = ansi_opname (GT_EXPR);
13119 break;
13121 case CPP_PLUS_EQ:
13122 id = ansi_assopname (PLUS_EXPR);
13123 break;
13125 case CPP_MINUS_EQ:
13126 id = ansi_assopname (MINUS_EXPR);
13127 break;
13129 case CPP_MULT_EQ:
13130 id = ansi_assopname (MULT_EXPR);
13131 break;
13133 case CPP_DIV_EQ:
13134 id = ansi_assopname (TRUNC_DIV_EXPR);
13135 break;
13137 case CPP_MOD_EQ:
13138 id = ansi_assopname (TRUNC_MOD_EXPR);
13139 break;
13141 case CPP_XOR_EQ:
13142 id = ansi_assopname (BIT_XOR_EXPR);
13143 break;
13145 case CPP_AND_EQ:
13146 id = ansi_assopname (BIT_AND_EXPR);
13147 break;
13149 case CPP_OR_EQ:
13150 id = ansi_assopname (BIT_IOR_EXPR);
13151 break;
13153 case CPP_LSHIFT:
13154 id = ansi_opname (LSHIFT_EXPR);
13155 break;
13157 case CPP_RSHIFT:
13158 id = ansi_opname (RSHIFT_EXPR);
13159 break;
13161 case CPP_LSHIFT_EQ:
13162 id = ansi_assopname (LSHIFT_EXPR);
13163 break;
13165 case CPP_RSHIFT_EQ:
13166 id = ansi_assopname (RSHIFT_EXPR);
13167 break;
13169 case CPP_EQ_EQ:
13170 id = ansi_opname (EQ_EXPR);
13171 break;
13173 case CPP_NOT_EQ:
13174 id = ansi_opname (NE_EXPR);
13175 break;
13177 case CPP_LESS_EQ:
13178 id = ansi_opname (LE_EXPR);
13179 break;
13181 case CPP_GREATER_EQ:
13182 id = ansi_opname (GE_EXPR);
13183 break;
13185 case CPP_AND_AND:
13186 id = ansi_opname (TRUTH_ANDIF_EXPR);
13187 break;
13189 case CPP_OR_OR:
13190 id = ansi_opname (TRUTH_ORIF_EXPR);
13191 break;
13193 case CPP_PLUS_PLUS:
13194 id = ansi_opname (POSTINCREMENT_EXPR);
13195 break;
13197 case CPP_MINUS_MINUS:
13198 id = ansi_opname (PREDECREMENT_EXPR);
13199 break;
13201 case CPP_COMMA:
13202 id = ansi_opname (COMPOUND_EXPR);
13203 break;
13205 case CPP_DEREF_STAR:
13206 id = ansi_opname (MEMBER_REF);
13207 break;
13209 case CPP_DEREF:
13210 id = ansi_opname (COMPONENT_REF);
13211 break;
13213 case CPP_OPEN_PAREN:
13214 /* Consume the `('. */
13215 cp_lexer_consume_token (parser->lexer);
13216 /* Look for the matching `)'. */
13217 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13218 return ansi_opname (CALL_EXPR);
13220 case CPP_OPEN_SQUARE:
13221 /* Consume the `['. */
13222 cp_lexer_consume_token (parser->lexer);
13223 /* Look for the matching `]'. */
13224 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13225 return ansi_opname (ARRAY_REF);
13227 case CPP_UTF8STRING:
13228 case CPP_UTF8STRING_USERDEF:
13229 utf8 = true;
13230 case CPP_STRING:
13231 case CPP_WSTRING:
13232 case CPP_STRING16:
13233 case CPP_STRING32:
13234 case CPP_STRING_USERDEF:
13235 case CPP_WSTRING_USERDEF:
13236 case CPP_STRING16_USERDEF:
13237 case CPP_STRING32_USERDEF:
13239 tree str, string_tree;
13240 int sz, len;
13242 if (cxx_dialect == cxx98)
13243 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13245 /* Consume the string. */
13246 str = cp_parser_string_literal (parser, /*translate=*/true,
13247 /*wide_ok=*/true, /*lookup_udlit=*/false);
13248 if (str == error_mark_node)
13249 return error_mark_node;
13250 else if (TREE_CODE (str) == USERDEF_LITERAL)
13252 string_tree = USERDEF_LITERAL_VALUE (str);
13253 id = USERDEF_LITERAL_SUFFIX_ID (str);
13255 else
13257 string_tree = str;
13258 /* Look for the suffix identifier. */
13259 token = cp_lexer_peek_token (parser->lexer);
13260 if (token->type == CPP_NAME)
13261 id = cp_parser_identifier (parser);
13262 else if (token->type == CPP_KEYWORD)
13264 error ("unexpected keyword;"
13265 " remove space between quotes and suffix identifier");
13266 return error_mark_node;
13268 else
13270 error ("expected suffix identifier");
13271 return error_mark_node;
13274 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13275 (TREE_TYPE (TREE_TYPE (string_tree))));
13276 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13277 if (len != 0)
13279 error ("expected empty string after %<operator%> keyword");
13280 return error_mark_node;
13282 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13283 != char_type_node)
13285 error ("invalid encoding prefix in literal operator");
13286 return error_mark_node;
13288 if (id != error_mark_node)
13290 const char *name = IDENTIFIER_POINTER (id);
13291 id = cp_literal_operator_id (name);
13293 return id;
13296 default:
13297 /* Anything else is an error. */
13298 break;
13301 /* If we have selected an identifier, we need to consume the
13302 operator token. */
13303 if (id)
13304 cp_lexer_consume_token (parser->lexer);
13305 /* Otherwise, no valid operator name was present. */
13306 else
13308 cp_parser_error (parser, "expected operator");
13309 id = error_mark_node;
13312 return id;
13315 /* Parse a template-declaration.
13317 template-declaration:
13318 export [opt] template < template-parameter-list > declaration
13320 If MEMBER_P is TRUE, this template-declaration occurs within a
13321 class-specifier.
13323 The grammar rule given by the standard isn't correct. What
13324 is really meant is:
13326 template-declaration:
13327 export [opt] template-parameter-list-seq
13328 decl-specifier-seq [opt] init-declarator [opt] ;
13329 export [opt] template-parameter-list-seq
13330 function-definition
13332 template-parameter-list-seq:
13333 template-parameter-list-seq [opt]
13334 template < template-parameter-list >
13336 Concept Extensions:
13338 template-parameter-list-seq:
13339 template < template-parameter-list > template-requirement [opt]
13341 template-requirement:
13342 requires logical-or-expression
13345 static void
13346 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13348 /* Check for `export'. */
13349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13351 /* Consume the `export' token. */
13352 cp_lexer_consume_token (parser->lexer);
13353 /* Warn that we do not support `export'. */
13354 warning (0, "keyword %<export%> not implemented, and will be ignored");
13357 cp_parser_template_declaration_after_export (parser, member_p);
13360 /* Parse a template-parameter-list.
13362 template-parameter-list:
13363 template-parameter
13364 template-parameter-list , template-parameter
13366 Returns a TREE_LIST. Each node represents a template parameter.
13367 The nodes are connected via their TREE_CHAINs. */
13369 static tree
13370 cp_parser_template_parameter_list (cp_parser* parser)
13372 tree parameter_list = NULL_TREE;
13374 begin_template_parm_list ();
13376 /* The loop below parses the template parms. We first need to know
13377 the total number of template parms to be able to compute proper
13378 canonical types of each dependent type. So after the loop, when
13379 we know the total number of template parms,
13380 end_template_parm_list computes the proper canonical types and
13381 fixes up the dependent types accordingly. */
13382 while (true)
13384 tree parameter;
13385 bool is_non_type;
13386 bool is_parameter_pack;
13387 location_t parm_loc;
13389 /* Parse the template-parameter. */
13390 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13391 parameter = cp_parser_template_parameter (parser,
13392 &is_non_type,
13393 &is_parameter_pack);
13394 /* Add it to the list. */
13395 if (parameter != error_mark_node)
13396 parameter_list = process_template_parm (parameter_list,
13397 parm_loc,
13398 parameter,
13399 is_non_type,
13400 is_parameter_pack);
13401 else
13403 tree err_parm = build_tree_list (parameter, parameter);
13404 parameter_list = chainon (parameter_list, err_parm);
13407 /* If the next token is not a `,', we're done. */
13408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13409 break;
13410 /* Otherwise, consume the `,' token. */
13411 cp_lexer_consume_token (parser->lexer);
13414 return end_template_parm_list (parameter_list);
13417 /* Parse a introduction-list.
13419 introduction-list:
13420 introduced-parameter
13421 introduction-list , introduced-parameter
13423 introduced-parameter:
13424 ...[opt] identifier
13426 Returns a TREE_VEC of INTRODUCED_PARM_DECLs. If the parameter is a pack
13427 then the introduced parm will have INTORDUCED_PACK_P set. In addition, the
13428 INTRODUCED_PARM_DECL will also have DECL_NAME set and token location in
13429 DECL_SOURCE_LOCATION. */
13431 static tree
13432 cp_parser_introduction_list (cp_parser *parser)
13434 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
13436 while (true)
13438 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
13439 if (is_pack)
13440 cp_lexer_consume_token (parser->lexer);
13442 // Build placeholder.
13443 tree parm = build_nt (INTRODUCED_PARM_DECL);
13444 DECL_SOURCE_LOCATION (parm)
13445 = cp_lexer_peek_token (parser->lexer)->location;
13446 DECL_NAME (parm) = cp_parser_identifier (parser);
13447 INTRODUCED_PACK_P (parm) = is_pack;
13448 vec_safe_push (introduction_vec, parm);
13450 // If the next token is not a `,', we're done.
13451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13452 break;
13453 // Otherwise, consume the `,' token.
13454 cp_lexer_consume_token (parser->lexer);
13457 // Convert the vec into a TREE_VEC.
13458 tree introduction_list = make_tree_vec (introduction_vec->length ());
13459 unsigned int n;
13460 tree parm;
13461 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
13463 TREE_VEC_ELT (introduction_list, n) = parm;
13466 release_tree_vector (introduction_vec);
13467 return introduction_list;
13470 // Given a declarator, get the declarator-id part, or NULL_TREE if this
13471 // is an abstract declarator.
13472 static inline cp_declarator*
13473 get_id_declarator (cp_declarator *declarator)
13475 cp_declarator *d = declarator;
13476 while (d && d->kind != cdk_id)
13477 d = d->declarator;
13478 return d;
13481 // Get the unqualified-id from the DECLARATOR or NULL_TREE if this
13482 // is an abstract declarator.
13483 static inline tree
13484 get_unqualified_id (cp_declarator *declarator)
13486 declarator = get_id_declarator (declarator);
13487 if (declarator)
13488 return declarator->u.id.unqualified_name;
13489 else
13490 return NULL_TREE;
13493 // Returns true if PARM declares a constrained-parameter.
13494 static inline bool
13495 is_constrained_parameter (cp_parameter_declarator *parm)
13497 gcc_assert (parm);
13498 tree decl = parm->decl_specifiers.type;
13499 return (decl
13500 && TREE_CODE (decl) == TYPE_DECL
13501 && DECL_INITIAL (decl)
13502 && DECL_SIZE_UNIT (decl)
13503 && (TREE_CODE (DECL_SIZE_UNIT (decl)) == FUNCTION_DECL
13504 || TREE_CODE (DECL_SIZE_UNIT (decl)) == VAR_DECL));
13508 // Check that the type parameter is only a declarator-id, and that its
13509 // type is not cv-qualified.
13510 bool
13511 cp_parser_check_constrained_type_parm (cp_parser *parser,
13512 cp_parameter_declarator *parm)
13514 if (!parm->declarator)
13515 return true;
13517 if (parm->declarator->kind != cdk_id)
13519 cp_parser_error (parser, "invalid constrained type parameter");
13520 return false;
13523 // Don't allow cv-qualified type parameters.
13524 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
13525 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
13527 cp_parser_error (parser, "cv-qualified type parameter");
13528 return false;
13531 return true;
13534 // Finish parsing/processing a template type parameter and checking
13535 // various restrictions.
13536 static inline tree
13537 cp_parser_constrained_type_template_parm (cp_parser *parser,
13538 tree id,
13539 cp_parameter_declarator* parmdecl)
13541 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
13542 return finish_template_type_parm (class_type_node, id);
13543 else
13544 return error_mark_node;
13547 // Finish parsing/processing a template template parameter by borrowing
13548 // the template parameter list from the prototype parameter.
13549 static tree
13550 cp_parser_constrained_template_template_parm (cp_parser *parser,
13551 tree proto,
13552 tree id,
13553 cp_parameter_declarator *parmdecl)
13555 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
13556 return error_mark_node;
13558 // FIXME: This should probably be copied, and we may need to adjust
13559 // the template parameter depths.
13560 tree saved_parms = current_template_parms;
13561 begin_template_parm_list ();
13562 current_template_parms = DECL_TEMPLATE_PARMS (proto);
13563 end_template_parm_list ();
13565 tree parm = finish_template_template_parm (class_type_node, id);
13566 current_template_parms = saved_parms;
13568 return parm;
13571 // Create a new non-type template parameter from the given PARM declarator.
13572 static tree
13573 constrained_non_type_template_parm (bool *is_non_type,
13574 cp_parameter_declarator *parm)
13576 *is_non_type = true;
13577 cp_declarator *decl = parm->declarator;
13578 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
13579 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
13580 return grokdeclarator (decl, specs, TPARM, 0, NULL);
13583 // Build a constrained template parameter based on the PARMDECL
13584 // declarator. The type of PARMDECL is the constrained type, which
13585 // refers to the prototype template parameter that ultimately
13586 // specifies the type of the declared parameter.
13587 static tree
13588 finish_constrained_parameter (cp_parser *parser,
13589 cp_parameter_declarator *parmdecl,
13590 bool *is_non_type,
13591 bool *is_parameter_pack)
13593 tree decl = parmdecl->decl_specifiers.type;
13594 tree id = get_unqualified_id (parmdecl->declarator);
13595 tree def = parmdecl->default_argument;
13596 tree proto = DECL_INITIAL (decl);
13598 // A template parameter constrained by a variadic concept shall also
13599 // be declared as a template parameter pack.
13600 bool is_variadic = template_parameter_pack_p (proto);
13601 if (is_variadic && !*is_parameter_pack)
13602 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
13604 // Build the parameter. Return an error if the declarator was invalid.
13605 tree parm;
13606 if (TREE_CODE (proto) == TYPE_DECL)
13607 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
13608 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13609 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
13610 parmdecl);
13611 else
13612 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
13613 if (parm == error_mark_node)
13614 return error_mark_node;
13616 // Finish the parameter decl and create a node attaching the
13617 // default argument and constraint.
13618 parm = build_tree_list (def, parm);
13619 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13621 return parm;
13624 // Returns the type of the given TYPE may represent the declaration of
13625 // a template type parameter. This is a helper function for the
13626 // cp_declares_type* functions below.
13627 static inline bool
13628 maybe_type_parameter (tree type)
13630 return type
13631 && TREE_CODE (type) == TYPE_DECL
13632 && DECL_SIZE_UNIT (type)
13633 && TREE_TYPE (type);
13636 // Returns true if the parsed type actually represents a type or template
13637 // template parameter.
13638 static inline bool
13639 declares_type_parameter (tree type)
13641 if (maybe_type_parameter (type))
13643 tree_code c = TREE_CODE (TREE_TYPE (type));
13644 return c == TEMPLATE_TYPE_PARM || c == TEMPLATE_TEMPLATE_PARM;
13646 return false;
13649 // Returns true if the parsed type actually represents the declaration
13650 // of a type template-parameter.
13651 static inline bool
13652 declares_type_template_parameter (tree type)
13654 return maybe_type_parameter (type)
13655 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM;
13659 // Returns true if the parsed type actually represents the declaration of
13660 // a template template-parameter.
13661 static bool
13662 declares_template_template_parameter (tree type)
13664 return maybe_type_parameter (type)
13665 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM;
13668 // Parse a default argument for a type template-parameter.
13670 // Note that diagnostics are handled in cp_parser_template_parameter.
13671 static tree
13672 cp_parser_default_type_template_argument (cp_parser *parser)
13674 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13676 /* Consume the `=' token. */
13677 cp_lexer_consume_token (parser->lexer);
13679 /* Parse the default-argument. */
13680 push_deferring_access_checks (dk_no_deferred);
13681 tree default_argument = cp_parser_type_id (parser);
13682 pop_deferring_access_checks ();
13684 return default_argument;
13687 // Parse a default argument for a template template-parameter.
13688 static tree
13689 cp_parser_default_template_template_argument (cp_parser *parser)
13691 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13693 bool is_template;
13695 /* Consume the `='. */
13696 cp_lexer_consume_token (parser->lexer);
13697 /* Parse the id-expression. */
13698 push_deferring_access_checks (dk_no_deferred);
13699 /* save token before parsing the id-expression, for error
13700 reporting */
13701 const cp_token* token = cp_lexer_peek_token (parser->lexer);
13702 tree default_argument
13703 = cp_parser_id_expression (parser,
13704 /*template_keyword_p=*/false,
13705 /*check_dependency_p=*/true,
13706 /*template_p=*/&is_template,
13707 /*declarator_p=*/false,
13708 /*optional_p=*/false);
13709 if (TREE_CODE (default_argument) == TYPE_DECL)
13710 /* If the id-expression was a template-id that refers to
13711 a template-class, we already have the declaration here,
13712 so no further lookup is needed. */
13714 else
13715 /* Look up the name. */
13716 default_argument
13717 = cp_parser_lookup_name (parser, default_argument,
13718 none_type,
13719 /*is_template=*/is_template,
13720 /*is_namespace=*/false,
13721 /*check_dependency=*/true,
13722 /*ambiguous_decls=*/NULL,
13723 token->location);
13724 /* See if the default argument is valid. */
13725 default_argument = check_template_template_default_arg (default_argument);
13726 pop_deferring_access_checks ();
13727 return default_argument;
13730 /* Parse a template-parameter.
13732 template-parameter:
13733 type-parameter
13734 parameter-declaration
13736 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13737 the parameter. The TREE_PURPOSE is the default value, if any.
13738 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13739 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13740 set to true iff this parameter is a parameter pack. */
13742 static tree
13743 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13744 bool *is_parameter_pack)
13746 cp_token *token;
13747 cp_parameter_declarator *parameter_declarator;
13748 cp_declarator *id_declarator;
13749 tree parm;
13751 /* Assume it is a type parameter or a template parameter. */
13752 *is_non_type = false;
13753 /* Assume it not a parameter pack. */
13754 *is_parameter_pack = false;
13755 /* Peek at the next token. */
13756 token = cp_lexer_peek_token (parser->lexer);
13757 /* If it is `class' or `template', we have a type-parameter. */
13758 if (token->keyword == RID_TEMPLATE)
13759 return cp_parser_type_parameter (parser, is_parameter_pack);
13760 /* If it is `class' or `typename' we do not know yet whether it is a
13761 type parameter or a non-type parameter. Consider:
13763 template <typename T, typename T::X X> ...
13767 template <class C, class D*> ...
13769 Here, the first parameter is a type parameter, and the second is
13770 a non-type parameter. We can tell by looking at the token after
13771 the identifier -- if it is a `,', `=', or `>' then we have a type
13772 parameter. */
13773 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13775 /* Peek at the token after `class' or `typename'. */
13776 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13777 /* If it's an ellipsis, we have a template type parameter
13778 pack. */
13779 if (token->type == CPP_ELLIPSIS)
13780 return cp_parser_type_parameter (parser, is_parameter_pack);
13781 /* If it's an identifier, skip it. */
13782 if (token->type == CPP_NAME)
13783 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13784 /* Now, see if the token looks like the end of a template
13785 parameter. */
13786 if (token->type == CPP_COMMA
13787 || token->type == CPP_EQ
13788 || token->type == CPP_GREATER)
13789 return cp_parser_type_parameter (parser, is_parameter_pack);
13792 /* Otherwise, it is a non-type parameter or a constrained parameter.
13794 [temp.param]
13796 When parsing a default template-argument for a non-type
13797 template-parameter, the first non-nested `>' is taken as the end
13798 of the template parameter-list rather than a greater-than
13799 operator. */
13800 parameter_declarator
13801 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13802 /*parenthesized_p=*/NULL);
13804 if (!parameter_declarator)
13805 return error_mark_node;
13807 /* If the parameter declaration is marked as a parameter pack, set
13808 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13809 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13810 grokdeclarator. */
13811 if (parameter_declarator->declarator
13812 && parameter_declarator->declarator->parameter_pack_p)
13814 *is_parameter_pack = true;
13815 parameter_declarator->declarator->parameter_pack_p = false;
13818 tree declared_type = parameter_declarator->decl_specifiers.type;
13819 if (parameter_declarator->default_argument)
13821 /* Can happen in some cases of erroneous input (c++/34892). */
13822 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13823 /* Consume the `...' for better error recovery. */
13824 cp_lexer_consume_token (parser->lexer);
13826 /* If the next token is an ellipsis, and we don't already have it
13827 marked as a parameter pack, then we have a parameter pack (that
13828 has no declarator). */
13829 else if (!*is_parameter_pack
13830 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13831 && (declarator_can_be_parameter_pack
13832 (parameter_declarator->declarator)))
13834 /* Consume the `...'. */
13835 cp_lexer_consume_token (parser->lexer);
13836 maybe_warn_variadic_templates ();
13838 *is_parameter_pack = true;
13840 /* We might end up with a pack expansion as the type of the non-type
13841 template parameter, in which case this is a non-type template
13842 parameter pack. */
13843 else if (declared_type && PACK_EXPANSION_P (declared_type))
13845 *is_parameter_pack = true;
13846 parameter_declarator->decl_specifiers.type =
13847 PACK_EXPANSION_PATTERN (declared_type);
13850 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13852 /* Parameter packs cannot have default arguments. However, a
13853 user may try to do so, so we'll parse them and give an
13854 appropriate diagnostic here. */
13856 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13858 /* Find the name of the parameter pack. */
13859 id_declarator = parameter_declarator->declarator;
13860 while (id_declarator && id_declarator->kind != cdk_id)
13861 id_declarator = id_declarator->declarator;
13863 if (id_declarator && id_declarator->kind == cdk_id)
13864 error_at (start_token->location,
13865 "template parameter pack %qD cannot have a default argument",
13866 id_declarator->u.id.unqualified_name);
13867 else
13868 error_at (start_token->location,
13869 "template parameter pack cannot have a default argument");
13871 /* Parse the default argument, but throw away the result. */
13872 if (declares_type_template_parameter (declared_type))
13873 cp_parser_default_type_template_argument (parser);
13874 else if (declares_template_template_parameter (declared_type))
13875 cp_parser_default_template_template_argument (parser);
13876 else
13877 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13880 // The parameter may have been constrained.
13881 if (is_constrained_parameter (parameter_declarator))
13882 return finish_constrained_parameter (parser,
13883 parameter_declarator,
13884 is_non_type,
13885 is_parameter_pack);
13887 // Now we're sure that the parameter is a non-type parameter.
13888 *is_non_type = true;
13890 parm = grokdeclarator (parameter_declarator->declarator,
13891 &parameter_declarator->decl_specifiers,
13892 TPARM, /*initialized=*/0,
13893 /*attrlist=*/NULL);
13894 if (parm == error_mark_node)
13895 return error_mark_node;
13897 return build_tree_list (parameter_declarator->default_argument, parm);
13900 /* Parse a type-parameter.
13902 type-parameter:
13903 class identifier [opt]
13904 class identifier [opt] = type-id
13905 typename identifier [opt]
13906 typename identifier [opt] = type-id
13907 template < template-parameter-list > class identifier [opt]
13908 template < template-parameter-list > class identifier [opt]
13909 = id-expression
13911 GNU Extension (variadic templates):
13913 type-parameter:
13914 class ... identifier [opt]
13915 typename ... identifier [opt]
13917 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13918 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13919 the declaration of the parameter.
13921 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13923 static tree
13924 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13926 cp_token *token;
13927 tree parameter;
13929 /* Look for a keyword to tell us what kind of parameter this is. */
13930 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13931 if (!token)
13932 return error_mark_node;
13934 switch (token->keyword)
13936 case RID_CLASS:
13937 case RID_TYPENAME:
13939 tree identifier;
13940 tree default_argument;
13942 /* If the next token is an ellipsis, we have a template
13943 argument pack. */
13944 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13946 /* Consume the `...' token. */
13947 cp_lexer_consume_token (parser->lexer);
13948 maybe_warn_variadic_templates ();
13950 *is_parameter_pack = true;
13953 /* If the next token is an identifier, then it names the
13954 parameter. */
13955 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13956 identifier = cp_parser_identifier (parser);
13957 else
13958 identifier = NULL_TREE;
13960 /* Create the parameter. */
13961 parameter = finish_template_type_parm (class_type_node, identifier);
13963 /* If the next token is an `=', we have a default argument. */
13964 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13966 default_argument
13967 = cp_parser_default_type_template_argument (parser);
13969 /* Template parameter packs cannot have default
13970 arguments. */
13971 if (*is_parameter_pack)
13973 if (identifier)
13974 error_at (token->location,
13975 "template parameter pack %qD cannot have a "
13976 "default argument", identifier);
13977 else
13978 error_at (token->location,
13979 "template parameter packs cannot have "
13980 "default arguments");
13981 default_argument = NULL_TREE;
13983 else if (check_for_bare_parameter_packs (default_argument))
13984 default_argument = error_mark_node;
13986 /* ANS: FIXME: This appears to be a stray pop. Check again once
13987 concept introdutions have been disabled. */
13988 // pop_deferring_access_checks ();
13990 else
13991 default_argument = NULL_TREE;
13993 /* Create the combined representation of the parameter and the
13994 default argument. */
13995 parameter = build_tree_list (default_argument, parameter);
13997 break;
13999 case RID_TEMPLATE:
14001 tree identifier;
14002 tree default_argument;
14004 /* Look for the `<'. */
14005 cp_parser_require (parser, CPP_LESS, RT_LESS);
14006 /* Parse the template-parameter-list. */
14007 cp_parser_template_parameter_list (parser);
14008 /* Look for the `>'. */
14009 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14011 // If template requirements are present, parse them.
14012 if (flag_concepts)
14014 tree reqs = get_shorthand_constraints (current_template_parms);
14015 if (tree r = cp_parser_requires_clause_opt (parser))
14016 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14017 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14020 /* Look for the `class' or 'typename' keywords. */
14021 cp_parser_type_parameter_key (parser);
14022 /* If the next token is an ellipsis, we have a template
14023 argument pack. */
14024 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14026 /* Consume the `...' token. */
14027 cp_lexer_consume_token (parser->lexer);
14028 maybe_warn_variadic_templates ();
14030 *is_parameter_pack = true;
14032 /* If the next token is an `=', then there is a
14033 default-argument. If the next token is a `>', we are at
14034 the end of the parameter-list. If the next token is a `,',
14035 then we are at the end of this parameter. */
14036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14037 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14038 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14040 identifier = cp_parser_identifier (parser);
14041 /* Treat invalid names as if the parameter were nameless. */
14042 if (identifier == error_mark_node)
14043 identifier = NULL_TREE;
14045 else
14046 identifier = NULL_TREE;
14048 /* Create the template parameter. */
14049 parameter = finish_template_template_parm (class_type_node,
14050 identifier);
14052 /* If the next token is an `=', then there is a
14053 default-argument. */
14054 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14056 default_argument
14057 = cp_parser_default_template_template_argument (parser);
14059 /* Template parameter packs cannot have default
14060 arguments. */
14061 if (*is_parameter_pack)
14063 if (identifier)
14064 error_at (token->location,
14065 "template parameter pack %qD cannot "
14066 "have a default argument",
14067 identifier);
14068 else
14069 error_at (token->location, "template parameter packs cannot "
14070 "have default arguments");
14071 default_argument = NULL_TREE;
14074 else
14075 default_argument = NULL_TREE;
14077 /* Create the combined representation of the parameter and the
14078 default argument. */
14079 parameter = build_tree_list (default_argument, parameter);
14081 break;
14083 default:
14084 gcc_unreachable ();
14085 break;
14088 return parameter;
14091 /* Parse a template-id.
14093 template-id:
14094 template-name < template-argument-list [opt] >
14096 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14097 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14098 returned. Otherwise, if the template-name names a function, or set
14099 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14100 names a class, returns a TYPE_DECL for the specialization.
14102 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14103 uninstantiated templates. */
14105 static tree
14106 cp_parser_template_id (cp_parser *parser,
14107 bool template_keyword_p,
14108 bool check_dependency_p,
14109 enum tag_types tag_type,
14110 bool is_declaration)
14112 int i;
14113 tree templ;
14114 tree arguments;
14115 tree template_id;
14116 cp_token_position start_of_id = 0;
14117 deferred_access_check *chk;
14118 vec<deferred_access_check, va_gc> *access_check;
14119 cp_token *next_token = NULL, *next_token_2 = NULL;
14120 bool is_identifier;
14122 /* If the next token corresponds to a template-id, there is no need
14123 to reparse it. */
14124 next_token = cp_lexer_peek_token (parser->lexer);
14125 if (next_token->type == CPP_TEMPLATE_ID)
14127 struct tree_check *check_value;
14129 /* Get the stored value. */
14130 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
14131 /* Perform any access checks that were deferred. */
14132 access_check = check_value->checks;
14133 if (access_check)
14135 FOR_EACH_VEC_ELT (*access_check, i, chk)
14136 perform_or_defer_access_check (chk->binfo,
14137 chk->decl,
14138 chk->diag_decl,
14139 tf_warning_or_error);
14141 /* Return the stored value. */
14142 return check_value->value;
14145 /* Avoid performing name lookup if there is no possibility of
14146 finding a template-id. */
14147 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14148 || (next_token->type == CPP_NAME
14149 && !cp_parser_nth_token_starts_template_argument_list_p
14150 (parser, 2)))
14152 cp_parser_error (parser, "expected template-id");
14153 return error_mark_node;
14156 /* Remember where the template-id starts. */
14157 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14158 start_of_id = cp_lexer_token_position (parser->lexer, false);
14160 push_deferring_access_checks (dk_deferred);
14162 /* Parse the template-name. */
14163 is_identifier = false;
14164 templ = cp_parser_template_name (parser, template_keyword_p,
14165 check_dependency_p,
14166 is_declaration,
14167 tag_type,
14168 &is_identifier);
14169 if (templ == error_mark_node || is_identifier)
14171 pop_deferring_access_checks ();
14172 return templ;
14175 /* If we find the sequence `[:' after a template-name, it's probably
14176 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14177 parse correctly the argument list. */
14178 next_token = cp_lexer_peek_token (parser->lexer);
14179 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14180 if (next_token->type == CPP_OPEN_SQUARE
14181 && next_token->flags & DIGRAPH
14182 && next_token_2->type == CPP_COLON
14183 && !(next_token_2->flags & PREV_WHITE))
14185 cp_parser_parse_tentatively (parser);
14186 /* Change `:' into `::'. */
14187 next_token_2->type = CPP_SCOPE;
14188 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14189 CPP_LESS. */
14190 cp_lexer_consume_token (parser->lexer);
14192 /* Parse the arguments. */
14193 arguments = cp_parser_enclosed_template_argument_list (parser);
14194 if (!cp_parser_parse_definitely (parser))
14196 /* If we couldn't parse an argument list, then we revert our changes
14197 and return simply an error. Maybe this is not a template-id
14198 after all. */
14199 next_token_2->type = CPP_COLON;
14200 cp_parser_error (parser, "expected %<<%>");
14201 pop_deferring_access_checks ();
14202 return error_mark_node;
14204 /* Otherwise, emit an error about the invalid digraph, but continue
14205 parsing because we got our argument list. */
14206 if (permerror (next_token->location,
14207 "%<<::%> cannot begin a template-argument list"))
14209 static bool hint = false;
14210 inform (next_token->location,
14211 "%<<:%> is an alternate spelling for %<[%>."
14212 " Insert whitespace between %<<%> and %<::%>");
14213 if (!hint && !flag_permissive)
14215 inform (next_token->location, "(if you use %<-fpermissive%> "
14216 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14217 "accept your code)");
14218 hint = true;
14222 else
14224 /* Look for the `<' that starts the template-argument-list. */
14225 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14227 pop_deferring_access_checks ();
14228 return error_mark_node;
14230 /* Parse the arguments. */
14231 arguments = cp_parser_enclosed_template_argument_list (parser);
14234 /* Build a representation of the specialization. */
14235 if (identifier_p (templ))
14236 template_id = build_min_nt_loc (next_token->location,
14237 TEMPLATE_ID_EXPR,
14238 templ, arguments);
14239 else if (DECL_TYPE_TEMPLATE_P (templ)
14240 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14242 bool entering_scope;
14243 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14244 template (rather than some instantiation thereof) only if
14245 is not nested within some other construct. For example, in
14246 "template <typename T> void f(T) { A<T>::", A<T> is just an
14247 instantiation of A. */
14248 entering_scope = (template_parm_scope_p ()
14249 && cp_lexer_next_token_is (parser->lexer,
14250 CPP_SCOPE));
14251 template_id
14252 = finish_template_type (templ, arguments, entering_scope);
14254 else if (variable_template_p (templ))
14256 template_id = lookup_template_variable (templ, arguments);
14258 else
14260 /* If it's not a class-template or a template-template, it should be
14261 a function-template. */
14262 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14263 || TREE_CODE (templ) == OVERLOAD
14264 || BASELINK_P (templ)));
14266 // If the template + args designate a concept, then return
14267 // something else.
14268 if (tree id = cp_maybe_partial_concept_id (parser, templ, arguments))
14269 return id;
14271 template_id = lookup_template_function (templ, arguments);
14274 /* If parsing tentatively, replace the sequence of tokens that makes
14275 up the template-id with a CPP_TEMPLATE_ID token. That way,
14276 should we re-parse the token stream, we will not have to repeat
14277 the effort required to do the parse, nor will we issue duplicate
14278 error messages about problems during instantiation of the
14279 template. */
14280 if (start_of_id
14281 /* Don't do this if we had a parse error in a declarator; re-parsing
14282 might succeed if a name changes meaning (60361). */
14283 && !(cp_parser_error_occurred (parser)
14284 && cp_parser_parsing_tentatively (parser)
14285 && parser->in_declarator_p))
14287 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14289 /* Reset the contents of the START_OF_ID token. */
14290 token->type = CPP_TEMPLATE_ID;
14291 /* Retrieve any deferred checks. Do not pop this access checks yet
14292 so the memory will not be reclaimed during token replacing below. */
14293 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14294 token->u.tree_check_value->value = template_id;
14295 token->u.tree_check_value->checks = get_deferred_access_checks ();
14296 token->keyword = RID_MAX;
14298 /* Purge all subsequent tokens. */
14299 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14301 /* ??? Can we actually assume that, if template_id ==
14302 error_mark_node, we will have issued a diagnostic to the
14303 user, as opposed to simply marking the tentative parse as
14304 failed? */
14305 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14306 error_at (token->location, "parse error in template argument list");
14309 pop_to_parent_deferring_access_checks ();
14311 return template_id;
14314 /* Parse a template-name.
14316 template-name:
14317 identifier
14319 The standard should actually say:
14321 template-name:
14322 identifier
14323 operator-function-id
14325 A defect report has been filed about this issue.
14327 A conversion-function-id cannot be a template name because they cannot
14328 be part of a template-id. In fact, looking at this code:
14330 a.operator K<int>()
14332 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14333 It is impossible to call a templated conversion-function-id with an
14334 explicit argument list, since the only allowed template parameter is
14335 the type to which it is converting.
14337 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14338 `template' keyword, in a construction like:
14340 T::template f<3>()
14342 In that case `f' is taken to be a template-name, even though there
14343 is no way of knowing for sure.
14345 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
14346 name refers to a set of overloaded functions, at least one of which
14347 is a template, or an IDENTIFIER_NODE with the name of the template,
14348 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
14349 names are looked up inside uninstantiated templates. */
14351 static tree
14352 cp_parser_template_name (cp_parser* parser,
14353 bool template_keyword_p,
14354 bool check_dependency_p,
14355 bool is_declaration,
14356 enum tag_types tag_type,
14357 bool *is_identifier)
14359 tree identifier;
14360 tree decl;
14361 tree fns;
14362 cp_token *token = cp_lexer_peek_token (parser->lexer);
14364 /* If the next token is `operator', then we have either an
14365 operator-function-id or a conversion-function-id. */
14366 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
14368 /* We don't know whether we're looking at an
14369 operator-function-id or a conversion-function-id. */
14370 cp_parser_parse_tentatively (parser);
14371 /* Try an operator-function-id. */
14372 identifier = cp_parser_operator_function_id (parser);
14373 /* If that didn't work, try a conversion-function-id. */
14374 if (!cp_parser_parse_definitely (parser))
14376 cp_parser_error (parser, "expected template-name");
14377 return error_mark_node;
14380 /* Look for the identifier. */
14381 else
14382 identifier = cp_parser_identifier (parser);
14384 /* If we didn't find an identifier, we don't have a template-id. */
14385 if (identifier == error_mark_node)
14386 return error_mark_node;
14388 /* If the name immediately followed the `template' keyword, then it
14389 is a template-name. However, if the next token is not `<', then
14390 we do not treat it as a template-name, since it is not being used
14391 as part of a template-id. This enables us to handle constructs
14392 like:
14394 template <typename T> struct S { S(); };
14395 template <typename T> S<T>::S();
14397 correctly. We would treat `S' as a template -- if it were `S<T>'
14398 -- but we do not if there is no `<'. */
14400 if (processing_template_decl
14401 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14403 /* In a declaration, in a dependent context, we pretend that the
14404 "template" keyword was present in order to improve error
14405 recovery. For example, given:
14407 template <typename T> void f(T::X<int>);
14409 we want to treat "X<int>" as a template-id. */
14410 if (is_declaration
14411 && !template_keyword_p
14412 && parser->scope && TYPE_P (parser->scope)
14413 && check_dependency_p
14414 && dependent_scope_p (parser->scope)
14415 /* Do not do this for dtors (or ctors), since they never
14416 need the template keyword before their name. */
14417 && !constructor_name_p (identifier, parser->scope))
14419 cp_token_position start = 0;
14421 /* Explain what went wrong. */
14422 error_at (token->location, "non-template %qD used as template",
14423 identifier);
14424 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14425 parser->scope, identifier);
14426 /* If parsing tentatively, find the location of the "<" token. */
14427 if (cp_parser_simulate_error (parser))
14428 start = cp_lexer_token_position (parser->lexer, true);
14429 /* Parse the template arguments so that we can issue error
14430 messages about them. */
14431 cp_lexer_consume_token (parser->lexer);
14432 cp_parser_enclosed_template_argument_list (parser);
14433 /* Skip tokens until we find a good place from which to
14434 continue parsing. */
14435 cp_parser_skip_to_closing_parenthesis (parser,
14436 /*recovering=*/true,
14437 /*or_comma=*/true,
14438 /*consume_paren=*/false);
14439 /* If parsing tentatively, permanently remove the
14440 template argument list. That will prevent duplicate
14441 error messages from being issued about the missing
14442 "template" keyword. */
14443 if (start)
14444 cp_lexer_purge_tokens_after (parser->lexer, start);
14445 if (is_identifier)
14446 *is_identifier = true;
14447 return identifier;
14450 /* If the "template" keyword is present, then there is generally
14451 no point in doing name-lookup, so we just return IDENTIFIER.
14452 But, if the qualifying scope is non-dependent then we can
14453 (and must) do name-lookup normally. */
14454 if (template_keyword_p
14455 && (!parser->scope
14456 || (TYPE_P (parser->scope)
14457 && dependent_type_p (parser->scope))))
14458 return identifier;
14461 /* Look up the name. */
14462 decl = cp_parser_lookup_name (parser, identifier,
14463 tag_type,
14464 /*is_template=*/true,
14465 /*is_namespace=*/false,
14466 check_dependency_p,
14467 /*ambiguous_decls=*/NULL,
14468 token->location);
14470 decl = strip_using_decl (decl);
14472 /* If DECL is a template, then the name was a template-name. */
14473 if (TREE_CODE (decl) == TEMPLATE_DECL)
14475 if (TREE_DEPRECATED (decl)
14476 && deprecated_state != DEPRECATED_SUPPRESS)
14477 warn_deprecated_use (decl, NULL_TREE);
14479 else
14481 tree fn = NULL_TREE;
14483 /* The standard does not explicitly indicate whether a name that
14484 names a set of overloaded declarations, some of which are
14485 templates, is a template-name. However, such a name should
14486 be a template-name; otherwise, there is no way to form a
14487 template-id for the overloaded templates. */
14488 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14489 if (TREE_CODE (fns) == OVERLOAD)
14490 for (fn = fns; fn; fn = OVL_NEXT (fn))
14491 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14492 break;
14494 if (!fn)
14496 /* The name does not name a template. */
14497 cp_parser_error (parser, "expected template-name");
14498 return error_mark_node;
14502 /* If DECL is dependent, and refers to a function, then just return
14503 its name; we will look it up again during template instantiation. */
14504 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14506 tree scope = ovl_scope (decl);
14507 if (TYPE_P (scope) && dependent_type_p (scope))
14508 return identifier;
14511 return decl;
14514 /* Parse a template-argument-list.
14516 template-argument-list:
14517 template-argument ... [opt]
14518 template-argument-list , template-argument ... [opt]
14520 Returns a TREE_VEC containing the arguments. */
14522 static tree
14523 cp_parser_template_argument_list (cp_parser* parser)
14525 tree fixed_args[10];
14526 unsigned n_args = 0;
14527 unsigned alloced = 10;
14528 tree *arg_ary = fixed_args;
14529 tree vec;
14530 bool saved_in_template_argument_list_p;
14531 bool saved_ice_p;
14532 bool saved_non_ice_p;
14534 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14535 parser->in_template_argument_list_p = true;
14536 /* Even if the template-id appears in an integral
14537 constant-expression, the contents of the argument list do
14538 not. */
14539 saved_ice_p = parser->integral_constant_expression_p;
14540 parser->integral_constant_expression_p = false;
14541 saved_non_ice_p = parser->non_integral_constant_expression_p;
14542 parser->non_integral_constant_expression_p = false;
14544 /* Parse the arguments. */
14547 tree argument;
14549 if (n_args)
14550 /* Consume the comma. */
14551 cp_lexer_consume_token (parser->lexer);
14553 /* Parse the template-argument. */
14554 argument = cp_parser_template_argument (parser);
14556 /* If the next token is an ellipsis, we're expanding a template
14557 argument pack. */
14558 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14560 if (argument == error_mark_node)
14562 cp_token *token = cp_lexer_peek_token (parser->lexer);
14563 error_at (token->location,
14564 "expected parameter pack before %<...%>");
14566 /* Consume the `...' token. */
14567 cp_lexer_consume_token (parser->lexer);
14569 /* Make the argument into a TYPE_PACK_EXPANSION or
14570 EXPR_PACK_EXPANSION. */
14571 argument = make_pack_expansion (argument);
14574 // If we end up with a template decl argument, check if this is actually
14575 // a template template parameter and drop the outer wrapping.
14576 if (DECL_TEMPLATE_TEMPLATE_PARM_P (argument))
14577 argument = TREE_TYPE (argument);
14579 if (n_args == alloced)
14581 alloced *= 2;
14583 if (arg_ary == fixed_args)
14585 arg_ary = XNEWVEC (tree, alloced);
14586 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14588 else
14589 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14591 arg_ary[n_args++] = argument;
14593 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14595 vec = make_tree_vec (n_args);
14597 while (n_args--)
14598 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14600 if (arg_ary != fixed_args)
14601 free (arg_ary);
14602 parser->non_integral_constant_expression_p = saved_non_ice_p;
14603 parser->integral_constant_expression_p = saved_ice_p;
14604 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14605 #ifdef ENABLE_CHECKING
14606 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14607 #endif
14608 return vec;
14611 /* Parse a template-argument.
14613 template-argument:
14614 assignment-expression
14615 type-id
14616 id-expression
14618 The representation is that of an assignment-expression, type-id, or
14619 id-expression -- except that the qualified id-expression is
14620 evaluated, so that the value returned is either a DECL or an
14621 OVERLOAD.
14623 Although the standard says "assignment-expression", it forbids
14624 throw-expressions or assignments in the template argument.
14625 Therefore, we use "conditional-expression" instead. */
14627 static tree
14628 cp_parser_template_argument (cp_parser* parser)
14630 tree argument;
14631 bool template_p;
14632 bool address_p;
14633 bool maybe_type_id = false;
14634 cp_token *token = NULL, *argument_start_token = NULL;
14635 location_t loc = 0;
14636 cp_id_kind idk;
14638 /* There's really no way to know what we're looking at, so we just
14639 try each alternative in order.
14641 [temp.arg]
14643 In a template-argument, an ambiguity between a type-id and an
14644 expression is resolved to a type-id, regardless of the form of
14645 the corresponding template-parameter.
14647 Therefore, we try a type-id first. */
14648 cp_parser_parse_tentatively (parser);
14649 argument = cp_parser_template_type_arg (parser);
14650 /* If there was no error parsing the type-id but the next token is a
14651 '>>', our behavior depends on which dialect of C++ we're
14652 parsing. In C++98, we probably found a typo for '> >'. But there
14653 are type-id which are also valid expressions. For instance:
14655 struct X { int operator >> (int); };
14656 template <int V> struct Foo {};
14657 Foo<X () >> 5> r;
14659 Here 'X()' is a valid type-id of a function type, but the user just
14660 wanted to write the expression "X() >> 5". Thus, we remember that we
14661 found a valid type-id, but we still try to parse the argument as an
14662 expression to see what happens.
14664 In C++0x, the '>>' will be considered two separate '>'
14665 tokens. */
14666 if (!cp_parser_error_occurred (parser)
14667 && cxx_dialect == cxx98
14668 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14670 maybe_type_id = true;
14671 cp_parser_abort_tentative_parse (parser);
14673 else
14675 /* If the next token isn't a `,' or a `>', then this argument wasn't
14676 really finished. This means that the argument is not a valid
14677 type-id. */
14678 if (!cp_parser_next_token_ends_template_argument_p (parser))
14679 cp_parser_error (parser, "expected template-argument");
14680 /* If that worked, we're done. */
14681 if (cp_parser_parse_definitely (parser))
14682 return argument;
14684 /* We're still not sure what the argument will be. */
14685 cp_parser_parse_tentatively (parser);
14686 /* Try a template. */
14687 argument_start_token = cp_lexer_peek_token (parser->lexer);
14688 argument = cp_parser_id_expression (parser,
14689 /*template_keyword_p=*/false,
14690 /*check_dependency_p=*/true,
14691 &template_p,
14692 /*declarator_p=*/false,
14693 /*optional_p=*/false);
14694 /* If the next token isn't a `,' or a `>', then this argument wasn't
14695 really finished. */
14696 if (!cp_parser_next_token_ends_template_argument_p (parser))
14697 cp_parser_error (parser, "expected template-argument");
14698 if (!cp_parser_error_occurred (parser))
14700 /* Figure out what is being referred to. If the id-expression
14701 was for a class template specialization, then we will have a
14702 TYPE_DECL at this point. There is no need to do name lookup
14703 at this point in that case. */
14704 if (TREE_CODE (argument) != TYPE_DECL)
14705 argument = cp_parser_lookup_name (parser, argument,
14706 none_type,
14707 /*is_template=*/template_p,
14708 /*is_namespace=*/false,
14709 /*check_dependency=*/true,
14710 /*ambiguous_decls=*/NULL,
14711 argument_start_token->location);
14712 if (TREE_CODE (argument) != TEMPLATE_DECL
14713 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14714 cp_parser_error (parser, "expected template-name");
14716 if (cp_parser_parse_definitely (parser))
14718 if (TREE_DEPRECATED (argument))
14719 warn_deprecated_use (argument, NULL_TREE);
14720 return argument;
14722 /* It must be a non-type argument. There permitted cases are given
14723 in [temp.arg.nontype]:
14725 -- an integral constant-expression of integral or enumeration
14726 type; or
14728 -- the name of a non-type template-parameter; or
14730 -- the name of an object or function with external linkage...
14732 -- the address of an object or function with external linkage...
14734 -- a pointer to member... */
14735 /* Look for a non-type template parameter. */
14736 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14738 cp_parser_parse_tentatively (parser);
14739 argument = cp_parser_primary_expression (parser,
14740 /*address_p=*/false,
14741 /*cast_p=*/false,
14742 /*template_arg_p=*/true,
14743 &idk);
14744 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14745 || !cp_parser_next_token_ends_template_argument_p (parser))
14746 cp_parser_simulate_error (parser);
14747 if (cp_parser_parse_definitely (parser))
14748 return argument;
14751 /* If the next token is "&", the argument must be the address of an
14752 object or function with external linkage. */
14753 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14754 if (address_p)
14756 loc = cp_lexer_peek_token (parser->lexer)->location;
14757 cp_lexer_consume_token (parser->lexer);
14759 /* See if we might have an id-expression. */
14760 token = cp_lexer_peek_token (parser->lexer);
14761 if (token->type == CPP_NAME
14762 || token->keyword == RID_OPERATOR
14763 || token->type == CPP_SCOPE
14764 || token->type == CPP_TEMPLATE_ID
14765 || token->type == CPP_NESTED_NAME_SPECIFIER)
14767 cp_parser_parse_tentatively (parser);
14768 argument = cp_parser_primary_expression (parser,
14769 address_p,
14770 /*cast_p=*/false,
14771 /*template_arg_p=*/true,
14772 &idk);
14773 if (cp_parser_error_occurred (parser)
14774 || !cp_parser_next_token_ends_template_argument_p (parser))
14775 cp_parser_abort_tentative_parse (parser);
14776 else
14778 tree probe;
14780 if (INDIRECT_REF_P (argument))
14782 /* Strip the dereference temporarily. */
14783 gcc_assert (REFERENCE_REF_P (argument));
14784 argument = TREE_OPERAND (argument, 0);
14787 /* If we're in a template, we represent a qualified-id referring
14788 to a static data member as a SCOPE_REF even if the scope isn't
14789 dependent so that we can check access control later. */
14790 probe = argument;
14791 if (TREE_CODE (probe) == SCOPE_REF)
14792 probe = TREE_OPERAND (probe, 1);
14793 if (VAR_P (probe))
14795 /* A variable without external linkage might still be a
14796 valid constant-expression, so no error is issued here
14797 if the external-linkage check fails. */
14798 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14799 cp_parser_simulate_error (parser);
14801 else if (is_overloaded_fn (argument))
14802 /* All overloaded functions are allowed; if the external
14803 linkage test does not pass, an error will be issued
14804 later. */
14806 else if (address_p
14807 && (TREE_CODE (argument) == OFFSET_REF
14808 || TREE_CODE (argument) == SCOPE_REF))
14809 /* A pointer-to-member. */
14811 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14813 else
14814 cp_parser_simulate_error (parser);
14816 if (cp_parser_parse_definitely (parser))
14818 if (address_p)
14819 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14820 tf_warning_or_error);
14821 else
14822 argument = convert_from_reference (argument);
14823 return argument;
14827 /* If the argument started with "&", there are no other valid
14828 alternatives at this point. */
14829 if (address_p)
14831 cp_parser_error (parser, "invalid non-type template argument");
14832 return error_mark_node;
14835 /* If the argument wasn't successfully parsed as a type-id followed
14836 by '>>', the argument can only be a constant expression now.
14837 Otherwise, we try parsing the constant-expression tentatively,
14838 because the argument could really be a type-id. */
14839 if (maybe_type_id)
14840 cp_parser_parse_tentatively (parser);
14841 argument = cp_parser_constant_expression (parser);
14843 if (!maybe_type_id)
14844 return argument;
14845 if (!cp_parser_next_token_ends_template_argument_p (parser))
14846 cp_parser_error (parser, "expected template-argument");
14847 if (cp_parser_parse_definitely (parser))
14848 return argument;
14849 /* We did our best to parse the argument as a non type-id, but that
14850 was the only alternative that matched (albeit with a '>' after
14851 it). We can assume it's just a typo from the user, and a
14852 diagnostic will then be issued. */
14853 return cp_parser_template_type_arg (parser);
14856 /* Parse an explicit-instantiation.
14858 explicit-instantiation:
14859 template declaration
14861 Although the standard says `declaration', what it really means is:
14863 explicit-instantiation:
14864 template decl-specifier-seq [opt] declarator [opt] ;
14866 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14867 supposed to be allowed. A defect report has been filed about this
14868 issue.
14870 GNU Extension:
14872 explicit-instantiation:
14873 storage-class-specifier template
14874 decl-specifier-seq [opt] declarator [opt] ;
14875 function-specifier template
14876 decl-specifier-seq [opt] declarator [opt] ; */
14878 static void
14879 cp_parser_explicit_instantiation (cp_parser* parser)
14881 int declares_class_or_enum;
14882 cp_decl_specifier_seq decl_specifiers;
14883 tree extension_specifier = NULL_TREE;
14885 timevar_push (TV_TEMPLATE_INST);
14887 /* Look for an (optional) storage-class-specifier or
14888 function-specifier. */
14889 if (cp_parser_allow_gnu_extensions_p (parser))
14891 extension_specifier
14892 = cp_parser_storage_class_specifier_opt (parser);
14893 if (!extension_specifier)
14894 extension_specifier
14895 = cp_parser_function_specifier_opt (parser,
14896 /*decl_specs=*/NULL);
14899 /* Look for the `template' keyword. */
14900 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14901 /* Let the front end know that we are processing an explicit
14902 instantiation. */
14903 begin_explicit_instantiation ();
14904 /* [temp.explicit] says that we are supposed to ignore access
14905 control while processing explicit instantiation directives. */
14906 push_deferring_access_checks (dk_no_check);
14907 /* Parse a decl-specifier-seq. */
14908 cp_parser_decl_specifier_seq (parser,
14909 CP_PARSER_FLAGS_OPTIONAL,
14910 &decl_specifiers,
14911 &declares_class_or_enum);
14912 /* If there was exactly one decl-specifier, and it declared a class,
14913 and there's no declarator, then we have an explicit type
14914 instantiation. */
14915 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14917 tree type;
14919 type = check_tag_decl (&decl_specifiers,
14920 /*explicit_type_instantiation_p=*/true);
14921 /* Turn access control back on for names used during
14922 template instantiation. */
14923 pop_deferring_access_checks ();
14924 if (type)
14925 do_type_instantiation (type, extension_specifier,
14926 /*complain=*/tf_error);
14928 else
14930 cp_declarator *declarator;
14931 tree decl;
14933 /* Parse the declarator. */
14934 declarator
14935 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14936 /*ctor_dtor_or_conv_p=*/NULL,
14937 /*parenthesized_p=*/NULL,
14938 /*member_p=*/false,
14939 /*friend_p=*/false);
14940 if (declares_class_or_enum & 2)
14941 cp_parser_check_for_definition_in_return_type (declarator,
14942 decl_specifiers.type,
14943 decl_specifiers.locations[ds_type_spec]);
14944 if (declarator != cp_error_declarator)
14946 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14947 permerror (decl_specifiers.locations[ds_inline],
14948 "explicit instantiation shall not use"
14949 " %<inline%> specifier");
14950 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14951 permerror (decl_specifiers.locations[ds_constexpr],
14952 "explicit instantiation shall not use"
14953 " %<constexpr%> specifier");
14955 decl = grokdeclarator (declarator, &decl_specifiers,
14956 NORMAL, 0, &decl_specifiers.attributes);
14957 /* Turn access control back on for names used during
14958 template instantiation. */
14959 pop_deferring_access_checks ();
14960 /* Do the explicit instantiation. */
14961 do_decl_instantiation (decl, extension_specifier);
14963 else
14965 pop_deferring_access_checks ();
14966 /* Skip the body of the explicit instantiation. */
14967 cp_parser_skip_to_end_of_statement (parser);
14970 /* We're done with the instantiation. */
14971 end_explicit_instantiation ();
14973 cp_parser_consume_semicolon_at_end_of_statement (parser);
14975 timevar_pop (TV_TEMPLATE_INST);
14978 /* Parse an explicit-specialization.
14980 explicit-specialization:
14981 template < > declaration
14983 Although the standard says `declaration', what it really means is:
14985 explicit-specialization:
14986 template <> decl-specifier [opt] init-declarator [opt] ;
14987 template <> function-definition
14988 template <> explicit-specialization
14989 template <> template-declaration */
14991 static void
14992 cp_parser_explicit_specialization (cp_parser* parser)
14994 bool need_lang_pop;
14995 cp_token *token = cp_lexer_peek_token (parser->lexer);
14997 /* Look for the `template' keyword. */
14998 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14999 /* Look for the `<'. */
15000 cp_parser_require (parser, CPP_LESS, RT_LESS);
15001 /* Look for the `>'. */
15002 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15003 /* We have processed another parameter list. */
15004 ++parser->num_template_parameter_lists;
15005 /* [temp]
15007 A template ... explicit specialization ... shall not have C
15008 linkage. */
15009 if (current_lang_name == lang_name_c)
15011 error_at (token->location, "template specialization with C linkage");
15012 /* Give it C++ linkage to avoid confusing other parts of the
15013 front end. */
15014 push_lang_context (lang_name_cplusplus);
15015 need_lang_pop = true;
15017 else
15018 need_lang_pop = false;
15019 /* Let the front end know that we are beginning a specialization. */
15020 if (!begin_specialization ())
15022 end_specialization ();
15023 return;
15026 /* If the next keyword is `template', we need to figure out whether
15027 or not we're looking a template-declaration. */
15028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15030 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15031 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15032 cp_parser_template_declaration_after_export (parser,
15033 /*member_p=*/false);
15034 else
15035 cp_parser_explicit_specialization (parser);
15037 else
15038 /* Parse the dependent declaration. */
15039 cp_parser_single_declaration (parser,
15040 /*checks=*/NULL,
15041 /*member_p=*/false,
15042 /*explicit_specialization_p=*/true,
15043 /*friend_p=*/NULL);
15044 /* We're done with the specialization. */
15045 end_specialization ();
15046 /* For the erroneous case of a template with C linkage, we pushed an
15047 implicit C++ linkage scope; exit that scope now. */
15048 if (need_lang_pop)
15049 pop_lang_context ();
15050 /* We're done with this parameter list. */
15051 --parser->num_template_parameter_lists;
15054 /* Parse a type-specifier.
15056 type-specifier:
15057 simple-type-specifier
15058 class-specifier
15059 enum-specifier
15060 elaborated-type-specifier
15061 cv-qualifier
15063 GNU Extension:
15065 type-specifier:
15066 __complex__
15068 Returns a representation of the type-specifier. For a
15069 class-specifier, enum-specifier, or elaborated-type-specifier, a
15070 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15072 The parser flags FLAGS is used to control type-specifier parsing.
15074 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15075 in a decl-specifier-seq.
15077 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15078 class-specifier, enum-specifier, or elaborated-type-specifier, then
15079 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15080 if a type is declared; 2 if it is defined. Otherwise, it is set to
15081 zero.
15083 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15084 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15085 is set to FALSE. */
15087 static tree
15088 cp_parser_type_specifier (cp_parser* parser,
15089 cp_parser_flags flags,
15090 cp_decl_specifier_seq *decl_specs,
15091 bool is_declaration,
15092 int* declares_class_or_enum,
15093 bool* is_cv_qualifier)
15095 tree type_spec = NULL_TREE;
15096 cp_token *token;
15097 enum rid keyword;
15098 cp_decl_spec ds = ds_last;
15100 /* Assume this type-specifier does not declare a new type. */
15101 if (declares_class_or_enum)
15102 *declares_class_or_enum = 0;
15103 /* And that it does not specify a cv-qualifier. */
15104 if (is_cv_qualifier)
15105 *is_cv_qualifier = false;
15106 /* Peek at the next token. */
15107 token = cp_lexer_peek_token (parser->lexer);
15109 /* If we're looking at a keyword, we can use that to guide the
15110 production we choose. */
15111 keyword = token->keyword;
15112 switch (keyword)
15114 case RID_ENUM:
15115 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15116 goto elaborated_type_specifier;
15118 /* Look for the enum-specifier. */
15119 type_spec = cp_parser_enum_specifier (parser);
15120 /* If that worked, we're done. */
15121 if (type_spec)
15123 if (declares_class_or_enum)
15124 *declares_class_or_enum = 2;
15125 if (decl_specs)
15126 cp_parser_set_decl_spec_type (decl_specs,
15127 type_spec,
15128 token,
15129 /*type_definition_p=*/true);
15130 return type_spec;
15132 else
15133 goto elaborated_type_specifier;
15135 /* Any of these indicate either a class-specifier, or an
15136 elaborated-type-specifier. */
15137 case RID_CLASS:
15138 case RID_STRUCT:
15139 case RID_UNION:
15140 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15141 goto elaborated_type_specifier;
15143 /* Parse tentatively so that we can back up if we don't find a
15144 class-specifier. */
15145 cp_parser_parse_tentatively (parser);
15146 /* Look for the class-specifier. */
15147 type_spec = cp_parser_class_specifier (parser);
15148 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15149 /* If that worked, we're done. */
15150 if (cp_parser_parse_definitely (parser))
15152 if (declares_class_or_enum)
15153 *declares_class_or_enum = 2;
15154 if (decl_specs)
15155 cp_parser_set_decl_spec_type (decl_specs,
15156 type_spec,
15157 token,
15158 /*type_definition_p=*/true);
15159 return type_spec;
15162 /* Fall through. */
15163 elaborated_type_specifier:
15164 /* We're declaring (not defining) a class or enum. */
15165 if (declares_class_or_enum)
15166 *declares_class_or_enum = 1;
15168 /* Fall through. */
15169 case RID_TYPENAME:
15170 /* Look for an elaborated-type-specifier. */
15171 type_spec
15172 = (cp_parser_elaborated_type_specifier
15173 (parser,
15174 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15175 is_declaration));
15176 if (decl_specs)
15177 cp_parser_set_decl_spec_type (decl_specs,
15178 type_spec,
15179 token,
15180 /*type_definition_p=*/false);
15181 return type_spec;
15183 case RID_CONST:
15184 ds = ds_const;
15185 if (is_cv_qualifier)
15186 *is_cv_qualifier = true;
15187 break;
15189 case RID_VOLATILE:
15190 ds = ds_volatile;
15191 if (is_cv_qualifier)
15192 *is_cv_qualifier = true;
15193 break;
15195 case RID_RESTRICT:
15196 ds = ds_restrict;
15197 if (is_cv_qualifier)
15198 *is_cv_qualifier = true;
15199 break;
15201 case RID_COMPLEX:
15202 /* The `__complex__' keyword is a GNU extension. */
15203 ds = ds_complex;
15204 break;
15206 default:
15207 break;
15210 /* Handle simple keywords. */
15211 if (ds != ds_last)
15213 if (decl_specs)
15215 set_and_check_decl_spec_loc (decl_specs, ds, token);
15216 decl_specs->any_specifiers_p = true;
15218 return cp_lexer_consume_token (parser->lexer)->u.value;
15221 /* If we do not already have a type-specifier, assume we are looking
15222 at a simple-type-specifier. */
15223 type_spec = cp_parser_simple_type_specifier (parser,
15224 decl_specs,
15225 flags);
15227 /* If we didn't find a type-specifier, and a type-specifier was not
15228 optional in this context, issue an error message. */
15229 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15231 cp_parser_error (parser, "expected type specifier");
15232 return error_mark_node;
15235 return type_spec;
15238 /* Parse a simple-type-specifier.
15240 simple-type-specifier:
15241 :: [opt] nested-name-specifier [opt] type-name
15242 :: [opt] nested-name-specifier template template-id
15243 char
15244 wchar_t
15245 bool
15246 short
15248 long
15249 signed
15250 unsigned
15251 float
15252 double
15253 void
15255 C++0x Extension:
15257 simple-type-specifier:
15258 auto
15259 decltype ( expression )
15260 char16_t
15261 char32_t
15262 __underlying_type ( type-id )
15264 GNU Extension:
15266 simple-type-specifier:
15267 __int128
15268 __typeof__ unary-expression
15269 __typeof__ ( type-id )
15270 __typeof__ ( type-id ) { initializer-list , [opt] }
15272 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15273 appropriately updated. */
15275 static tree
15276 cp_parser_simple_type_specifier (cp_parser* parser,
15277 cp_decl_specifier_seq *decl_specs,
15278 cp_parser_flags flags)
15280 tree type = NULL_TREE;
15281 cp_token *token;
15282 int idx;
15284 /* Peek at the next token. */
15285 token = cp_lexer_peek_token (parser->lexer);
15287 /* If we're looking at a keyword, things are easy. */
15288 switch (token->keyword)
15290 case RID_CHAR:
15291 if (decl_specs)
15292 decl_specs->explicit_char_p = true;
15293 type = char_type_node;
15294 break;
15295 case RID_CHAR16:
15296 type = char16_type_node;
15297 break;
15298 case RID_CHAR32:
15299 type = char32_type_node;
15300 break;
15301 case RID_WCHAR:
15302 type = wchar_type_node;
15303 break;
15304 case RID_BOOL:
15305 type = boolean_type_node;
15306 break;
15307 case RID_SHORT:
15308 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15309 type = short_integer_type_node;
15310 break;
15311 case RID_INT:
15312 if (decl_specs)
15313 decl_specs->explicit_int_p = true;
15314 type = integer_type_node;
15315 break;
15316 case RID_INT_N_0:
15317 case RID_INT_N_1:
15318 case RID_INT_N_2:
15319 case RID_INT_N_3:
15320 idx = token->keyword - RID_INT_N_0;
15321 if (! int_n_enabled_p [idx])
15322 break;
15323 if (decl_specs)
15325 decl_specs->explicit_intN_p = true;
15326 decl_specs->int_n_idx = idx;
15328 type = int_n_trees [idx].signed_type;
15329 break;
15330 case RID_LONG:
15331 if (decl_specs)
15332 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
15333 type = long_integer_type_node;
15334 break;
15335 case RID_SIGNED:
15336 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
15337 type = integer_type_node;
15338 break;
15339 case RID_UNSIGNED:
15340 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
15341 type = unsigned_type_node;
15342 break;
15343 case RID_FLOAT:
15344 type = float_type_node;
15345 break;
15346 case RID_DOUBLE:
15347 type = double_type_node;
15348 break;
15349 case RID_VOID:
15350 type = void_type_node;
15351 break;
15353 case RID_AUTO:
15354 maybe_warn_cpp0x (CPP0X_AUTO);
15355 if (parser->auto_is_implicit_function_template_parm_p)
15357 if (cxx_dialect >= cxx14)
15358 type = synthesize_implicit_template_parm (parser, NULL_TREE);
15359 else
15360 type = error_mark_node;
15362 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
15364 if (cxx_dialect < cxx14)
15365 error_at (token->location,
15366 "use of %<auto%> in lambda parameter declaration "
15367 "only available with "
15368 "-std=c++14 or -std=gnu++14");
15370 else if (cxx_dialect < cxx14)
15371 error_at (token->location,
15372 "use of %<auto%> in parameter declaration "
15373 "only available with "
15374 "-std=c++14 or -std=gnu++14");
15375 else
15376 pedwarn (token->location, OPT_Wpedantic,
15377 "ISO C++ forbids use of %<auto%> in parameter "
15378 "declaration");
15380 else
15381 type = make_auto ();
15382 break;
15384 case RID_DECLTYPE:
15385 /* Since DR 743, decltype can either be a simple-type-specifier by
15386 itself or begin a nested-name-specifier. Parsing it will replace
15387 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15388 handling below decide what to do. */
15389 cp_parser_decltype (parser);
15390 cp_lexer_set_token_position (parser->lexer, token);
15391 break;
15393 case RID_TYPEOF:
15394 /* Consume the `typeof' token. */
15395 cp_lexer_consume_token (parser->lexer);
15396 /* Parse the operand to `typeof'. */
15397 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15398 /* If it is not already a TYPE, take its type. */
15399 if (!TYPE_P (type))
15400 type = finish_typeof (type);
15402 if (decl_specs)
15403 cp_parser_set_decl_spec_type (decl_specs, type,
15404 token,
15405 /*type_definition_p=*/false);
15407 return type;
15409 case RID_UNDERLYING_TYPE:
15410 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15411 if (decl_specs)
15412 cp_parser_set_decl_spec_type (decl_specs, type,
15413 token,
15414 /*type_definition_p=*/false);
15416 return type;
15418 case RID_BASES:
15419 case RID_DIRECT_BASES:
15420 type = cp_parser_trait_expr (parser, token->keyword);
15421 if (decl_specs)
15422 cp_parser_set_decl_spec_type (decl_specs, type,
15423 token,
15424 /*type_definition_p=*/false);
15425 return type;
15426 default:
15427 break;
15430 /* If token is an already-parsed decltype not followed by ::,
15431 it's a simple-type-specifier. */
15432 if (token->type == CPP_DECLTYPE
15433 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15435 type = token->u.value;
15436 if (decl_specs)
15438 cp_parser_set_decl_spec_type (decl_specs, type,
15439 token,
15440 /*type_definition_p=*/false);
15441 /* Remember that we are handling a decltype in order to
15442 implement the resolution of DR 1510 when the argument
15443 isn't instantiation dependent. */
15444 decl_specs->decltype_p = true;
15446 cp_lexer_consume_token (parser->lexer);
15447 return type;
15450 /* If the type-specifier was for a built-in type, we're done. */
15451 if (type)
15453 /* Record the type. */
15454 if (decl_specs
15455 && (token->keyword != RID_SIGNED
15456 && token->keyword != RID_UNSIGNED
15457 && token->keyword != RID_SHORT
15458 && token->keyword != RID_LONG))
15459 cp_parser_set_decl_spec_type (decl_specs,
15460 type,
15461 token,
15462 /*type_definition_p=*/false);
15463 if (decl_specs)
15464 decl_specs->any_specifiers_p = true;
15466 /* Consume the token. */
15467 cp_lexer_consume_token (parser->lexer);
15469 if (type == error_mark_node)
15470 return error_mark_node;
15472 /* There is no valid C++ program where a non-template type is
15473 followed by a "<". That usually indicates that the user thought
15474 that the type was a template. */
15475 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15476 token->location);
15478 return TYPE_NAME (type);
15481 /* The type-specifier must be a user-defined type. */
15482 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15484 bool qualified_p;
15485 bool global_p;
15487 /* Don't gobble tokens or issue error messages if this is an
15488 optional type-specifier. */
15489 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15490 cp_parser_parse_tentatively (parser);
15492 /* Look for the optional `::' operator. */
15493 global_p
15494 = (cp_parser_global_scope_opt (parser,
15495 /*current_scope_valid_p=*/false)
15496 != NULL_TREE);
15497 /* Look for the nested-name specifier. */
15498 qualified_p
15499 = (cp_parser_nested_name_specifier_opt (parser,
15500 /*typename_keyword_p=*/false,
15501 /*check_dependency_p=*/true,
15502 /*type_p=*/false,
15503 /*is_declaration=*/false)
15504 != NULL_TREE);
15505 token = cp_lexer_peek_token (parser->lexer);
15506 /* If we have seen a nested-name-specifier, and the next token
15507 is `template', then we are using the template-id production. */
15508 if (parser->scope
15509 && cp_parser_optional_template_keyword (parser))
15511 /* Look for the template-id. */
15512 type = cp_parser_template_id (parser,
15513 /*template_keyword_p=*/true,
15514 /*check_dependency_p=*/true,
15515 none_type,
15516 /*is_declaration=*/false);
15517 /* If the template-id did not name a type, we are out of
15518 luck. */
15519 if (TREE_CODE (type) != TYPE_DECL)
15521 cp_parser_error (parser, "expected template-id for type");
15522 type = NULL_TREE;
15525 /* Otherwise, look for a type-name. */
15526 else
15527 type = cp_parser_type_name (parser);
15529 /* Keep track of all name-lookups performed in class scopes. */
15530 if (type
15531 && !global_p
15532 && !qualified_p
15533 && TREE_CODE (type) == TYPE_DECL
15534 && identifier_p (DECL_NAME (type)))
15535 maybe_note_name_used_in_class (DECL_NAME (type), type);
15536 /* If it didn't work out, we don't have a TYPE. */
15537 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15538 && !cp_parser_parse_definitely (parser))
15539 type = NULL_TREE;
15540 if (type && decl_specs)
15541 cp_parser_set_decl_spec_type (decl_specs, type,
15542 token,
15543 /*type_definition_p=*/false);
15546 /* If we didn't get a type-name, issue an error message. */
15547 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15549 cp_parser_error (parser, "expected type-name");
15550 return error_mark_node;
15553 if (type && type != error_mark_node)
15555 /* See if TYPE is an Objective-C type, and if so, parse and
15556 accept any protocol references following it. Do this before
15557 the cp_parser_check_for_invalid_template_id() call, because
15558 Objective-C types can be followed by '<...>' which would
15559 enclose protocol names rather than template arguments, and so
15560 everything is fine. */
15561 if (c_dialect_objc () && !parser->scope
15562 && (objc_is_id (type) || objc_is_class_name (type)))
15564 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15565 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15567 /* Clobber the "unqualified" type previously entered into
15568 DECL_SPECS with the new, improved protocol-qualified version. */
15569 if (decl_specs)
15570 decl_specs->type = qual_type;
15572 return qual_type;
15575 /* There is no valid C++ program where a non-template type is
15576 followed by a "<". That usually indicates that the user
15577 thought that the type was a template. */
15578 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15579 none_type,
15580 token->location);
15583 return type;
15586 /* Parse a type-name.
15588 type-name:
15589 class-name
15590 enum-name
15591 typedef-name
15592 simple-template-id [in c++0x]
15594 enum-name:
15595 identifier
15597 typedef-name:
15598 identifier
15600 Concepts:
15602 type-name:
15603 concept-name
15604 partial-concept-id
15606 concept-name:
15607 identifier
15609 Returns a TYPE_DECL for the type. */
15611 static tree
15612 cp_parser_type_name (cp_parser* parser)
15614 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
15617 /* See above. */
15618 static tree
15619 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
15621 tree type_decl;
15623 /* We can't know yet whether it is a class-name or not. */
15624 cp_parser_parse_tentatively (parser);
15625 /* Try a class-name. */
15626 type_decl = cp_parser_class_name (parser,
15627 typename_keyword_p,
15628 /*template_keyword_p=*/false,
15629 none_type,
15630 /*check_dependency_p=*/true,
15631 /*class_head_p=*/false,
15632 /*is_declaration=*/false);
15634 /* If it's not a class-name, keep looking. */
15635 if (!cp_parser_parse_definitely (parser))
15637 if (cxx_dialect < cxx11)
15638 /* It must be a typedef-name or an enum-name. */
15639 return cp_parser_nonclass_name (parser);
15641 cp_parser_parse_tentatively (parser);
15642 /* It is either a simple-template-id representing an
15643 instantiation of an alias template... */
15644 type_decl = cp_parser_template_id (parser,
15645 /*template_keyword_p=*/false,
15646 /*check_dependency_p=*/true,
15647 none_type,
15648 /*is_declaration=*/false);
15650 /* Note that this must be an instantiation of an alias template
15651 because [temp.names]/6 says:
15653 A template-id that names an alias template specialization
15654 is a type-name.
15656 Whereas [temp.names]/7 says:
15658 A simple-template-id that names a class template
15659 specialization is a class-name.
15661 With concepts, this could also be a partial-concept-id that
15662 declares a non-type template parameter. */
15663 if (type_decl != NULL_TREE
15664 && TREE_CODE (type_decl) == TYPE_DECL
15665 && TYPE_DECL_ALIAS_P (type_decl))
15666 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15667 else if (maybe_type_parameter (type_decl))
15668 /* Don't do anything. */ ;
15669 else
15670 cp_parser_simulate_error (parser);
15672 if (!cp_parser_parse_definitely (parser))
15673 /* ... Or a typedef-name or an enum-name. */
15674 return cp_parser_nonclass_name (parser);
15677 return type_decl;
15680 // Returns true if proto is a type parameter, but not a template template
15681 // parameter.
15682 static bool
15683 cp_check_type_concept (tree fn, tree proto)
15685 if (TREE_CODE (proto) != TYPE_DECL)
15687 error ("invalid use of non-type concept %qD", fn);
15688 return false;
15690 return true;
15693 // Returns true if the parser is in a context that allows the
15694 // use of a constrained type specifier.
15695 static inline bool
15696 cp_parser_allows_constrained_type_specifier (cp_parser *parser)
15698 return (flag_concepts
15699 && (processing_template_parmlist
15700 || parser->auto_is_implicit_function_template_parm_p
15701 || parser->in_result_type_constraint_p));
15704 // Check if DECL and ARGS can form a constrained-type-specifier. If ARGS
15705 // is non-null, we try to form a concept check of the form DECL<?, ARGS>
15706 // where ? is a placeholder for any kind of template argument. If ARGS
15707 // is NULL, then we try to form a concept check of the form DECL<?>.
15708 static tree
15709 cp_maybe_constrained_type_specifier (cp_parser *parser, tree decl, tree args)
15711 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
15713 // Don't do any heavy lifting if we know we're not in a context
15714 // where it could succeed.
15715 if (!cp_parser_allows_constrained_type_specifier (parser))
15716 return NULL_TREE;
15718 // Try to build a call expression that evaluates the concept. This
15719 // can fail if the overload set refers only to non-templates.
15720 tree placeholder = build_nt (INTRODUCED_PARM_DECL);
15721 tree call = build_concept_check (decl, placeholder, args);
15722 if (call == error_mark_node)
15723 return NULL_TREE;
15725 // Deduce the checked constraint and the prototype parameter.
15726 tree fn;
15727 tree proto;
15728 if (!deduce_constrained_parameter (call, fn, proto))
15729 return NULL_TREE;
15731 // In template parameter scope, this results in a constrained parameter.
15732 // Return a descriptor of that parm.
15733 if (processing_template_parmlist)
15734 return build_constrained_parameter (fn, proto, args);
15736 // In any other context, a concept must be a type concept.
15737 if (!cp_check_type_concept (fn, proto))
15738 return error_mark_node;
15740 // In a parameter-declaration-clause, constrained-type specifiers
15741 // result in invented template parameters.
15742 if (parser->auto_is_implicit_function_template_parm_p)
15744 tree x = build_constrained_parameter (fn, proto, args);
15745 tree r = synthesize_implicit_template_parm (parser, x);
15746 return r;
15749 // A concept-name appearing in a result-type constraint is a
15750 // constrained auto. Meaning that type deduction will be applied
15751 // and the constraint checked.
15753 // TODO: Actually bind the constraint to the auto.
15754 if (parser->in_result_type_constraint_p)
15755 return make_auto();
15757 return NULL_TREE;
15761 // If DECL refers to a concept, return a TYPE_DECL representing the result
15762 // of using the constrained type specifier in the current context.
15764 // DECL refers to a concept if
15765 // - it is an overload set containing a function concept taking a single
15766 // type argument, or
15767 // - it is a variable concept taking a single type argument
15768 static tree
15769 cp_maybe_concept_name (cp_parser* parser, tree decl)
15771 return cp_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
15774 // Check if DECL and ARGS forms a partial concept id. Let C be the name
15775 // of the overload set denoted by TMPL, Args the sequence of ARGS, and
15776 // ? denote an unspecified template argument. If the template-id C<?, Args>
15777 // is valid, this denotes a partial-concept-id to be acted on.
15778 tree
15779 cp_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
15781 return cp_maybe_constrained_type_specifier (parser, decl, args);
15784 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
15785 or a concept-name.
15787 enum-name:
15788 identifier
15790 typedef-name:
15791 identifier
15793 concept-name:
15794 identifier
15796 Returns a TYPE_DECL for the type. */
15798 static tree
15799 cp_parser_nonclass_name (cp_parser* parser)
15801 tree type_decl;
15802 tree identifier;
15804 cp_token *token = cp_lexer_peek_token (parser->lexer);
15805 identifier = cp_parser_identifier (parser);
15806 if (identifier == error_mark_node)
15807 return error_mark_node;
15809 /* Look up the type-name. */
15810 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15811 type_decl = strip_using_decl (type_decl);
15813 // If we found an overload set, then it may refer to a concept-name.
15815 // TODO: The name could also refer to an introduction (if followed by '{').
15816 if (flag_concepts
15817 && (TREE_CODE (type_decl) == OVERLOAD
15818 || BASELINK_P (type_decl)
15819 || variable_concept_p (type_decl)))
15821 // Determine whether the overload refers to a concept.
15822 if (tree decl = cp_maybe_concept_name (parser, type_decl))
15823 return decl;
15826 if (TREE_CODE (type_decl) != TYPE_DECL
15827 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15829 /* See if this is an Objective-C type. */
15830 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15831 tree type = objc_get_protocol_qualified_type (identifier, protos);
15832 if (type)
15833 type_decl = TYPE_NAME (type);
15836 /* Issue an error if we did not find a type-name. */
15837 if (TREE_CODE (type_decl) != TYPE_DECL
15838 /* In Objective-C, we have the complication that class names are
15839 normally type names and start declarations (eg, the
15840 "NSObject" in "NSObject *object;"), but can be used in an
15841 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15842 is an expression. So, a classname followed by a dot is not a
15843 valid type-name. */
15844 || (objc_is_class_name (TREE_TYPE (type_decl))
15845 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15847 if (!cp_parser_simulate_error (parser))
15848 cp_parser_name_lookup_error (parser, identifier, type_decl,
15849 NLE_TYPE, token->location);
15850 return error_mark_node;
15852 /* Remember that the name was used in the definition of the
15853 current class so that we can check later to see if the
15854 meaning would have been different after the class was
15855 entirely defined. */
15856 else if (type_decl != error_mark_node
15857 && !parser->scope)
15858 maybe_note_name_used_in_class (identifier, type_decl);
15860 return type_decl;
15863 /* Parse an elaborated-type-specifier. Note that the grammar given
15864 here incorporates the resolution to DR68.
15866 elaborated-type-specifier:
15867 class-key :: [opt] nested-name-specifier [opt] identifier
15868 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15869 enum-key :: [opt] nested-name-specifier [opt] identifier
15870 typename :: [opt] nested-name-specifier identifier
15871 typename :: [opt] nested-name-specifier template [opt]
15872 template-id
15874 GNU extension:
15876 elaborated-type-specifier:
15877 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15878 class-key attributes :: [opt] nested-name-specifier [opt]
15879 template [opt] template-id
15880 enum attributes :: [opt] nested-name-specifier [opt] identifier
15882 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15883 declared `friend'. If IS_DECLARATION is TRUE, then this
15884 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15885 something is being declared.
15887 Returns the TYPE specified. */
15889 static tree
15890 cp_parser_elaborated_type_specifier (cp_parser* parser,
15891 bool is_friend,
15892 bool is_declaration)
15894 enum tag_types tag_type;
15895 tree identifier;
15896 tree type = NULL_TREE;
15897 tree attributes = NULL_TREE;
15898 tree globalscope;
15899 cp_token *token = NULL;
15901 /* See if we're looking at the `enum' keyword. */
15902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15904 /* Consume the `enum' token. */
15905 cp_lexer_consume_token (parser->lexer);
15906 /* Remember that it's an enumeration type. */
15907 tag_type = enum_type;
15908 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15909 enums) is used here. */
15910 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15911 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15913 pedwarn (input_location, 0, "elaborated-type-specifier "
15914 "for a scoped enum must not use the %<%D%> keyword",
15915 cp_lexer_peek_token (parser->lexer)->u.value);
15916 /* Consume the `struct' or `class' and parse it anyway. */
15917 cp_lexer_consume_token (parser->lexer);
15919 /* Parse the attributes. */
15920 attributes = cp_parser_attributes_opt (parser);
15922 /* Or, it might be `typename'. */
15923 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15924 RID_TYPENAME))
15926 /* Consume the `typename' token. */
15927 cp_lexer_consume_token (parser->lexer);
15928 /* Remember that it's a `typename' type. */
15929 tag_type = typename_type;
15931 /* Otherwise it must be a class-key. */
15932 else
15934 tag_type = cp_parser_class_key (parser);
15935 if (tag_type == none_type)
15936 return error_mark_node;
15937 /* Parse the attributes. */
15938 attributes = cp_parser_attributes_opt (parser);
15941 /* Look for the `::' operator. */
15942 globalscope = cp_parser_global_scope_opt (parser,
15943 /*current_scope_valid_p=*/false);
15944 /* Look for the nested-name-specifier. */
15945 if (tag_type == typename_type && !globalscope)
15947 if (!cp_parser_nested_name_specifier (parser,
15948 /*typename_keyword_p=*/true,
15949 /*check_dependency_p=*/true,
15950 /*type_p=*/true,
15951 is_declaration))
15952 return error_mark_node;
15954 else
15955 /* Even though `typename' is not present, the proposed resolution
15956 to Core Issue 180 says that in `class A<T>::B', `B' should be
15957 considered a type-name, even if `A<T>' is dependent. */
15958 cp_parser_nested_name_specifier_opt (parser,
15959 /*typename_keyword_p=*/true,
15960 /*check_dependency_p=*/true,
15961 /*type_p=*/true,
15962 is_declaration);
15963 /* For everything but enumeration types, consider a template-id.
15964 For an enumeration type, consider only a plain identifier. */
15965 if (tag_type != enum_type)
15967 bool template_p = false;
15968 tree decl;
15970 /* Allow the `template' keyword. */
15971 template_p = cp_parser_optional_template_keyword (parser);
15972 /* If we didn't see `template', we don't know if there's a
15973 template-id or not. */
15974 if (!template_p)
15975 cp_parser_parse_tentatively (parser);
15976 /* Parse the template-id. */
15977 token = cp_lexer_peek_token (parser->lexer);
15978 decl = cp_parser_template_id (parser, template_p,
15979 /*check_dependency_p=*/true,
15980 tag_type,
15981 is_declaration);
15982 /* If we didn't find a template-id, look for an ordinary
15983 identifier. */
15984 if (!template_p && !cp_parser_parse_definitely (parser))
15986 /* We can get here when cp_parser_template_id, called by
15987 cp_parser_class_name with tag_type == none_type, succeeds
15988 and caches a BASELINK. Then, when called again here,
15989 instead of failing and returning an error_mark_node
15990 returns it (see template/typename17.C in C++11).
15991 ??? Could we diagnose this earlier? */
15992 else if (tag_type == typename_type && BASELINK_P (decl))
15994 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15995 type = error_mark_node;
15997 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15998 in effect, then we must assume that, upon instantiation, the
15999 template will correspond to a class. */
16000 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16001 && tag_type == typename_type)
16002 type = make_typename_type (parser->scope, decl,
16003 typename_type,
16004 /*complain=*/tf_error);
16005 /* If the `typename' keyword is in effect and DECL is not a type
16006 decl, then type is non existent. */
16007 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16009 else if (TREE_CODE (decl) == TYPE_DECL)
16010 type = check_elaborated_type_specifier (tag_type, decl,
16011 /*allow_template_p=*/true);
16012 else if (decl == error_mark_node)
16013 type = error_mark_node;
16016 if (!type)
16018 token = cp_lexer_peek_token (parser->lexer);
16019 identifier = cp_parser_identifier (parser);
16021 if (identifier == error_mark_node)
16023 parser->scope = NULL_TREE;
16024 return error_mark_node;
16027 /* For a `typename', we needn't call xref_tag. */
16028 if (tag_type == typename_type
16029 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16030 return cp_parser_make_typename_type (parser, identifier,
16031 token->location);
16033 /* Template parameter lists apply only if we are not within a
16034 function parameter list. */
16035 bool template_parm_lists_apply
16036 = parser->num_template_parameter_lists;
16037 if (template_parm_lists_apply)
16038 for (cp_binding_level *s = current_binding_level;
16039 s && s->kind != sk_template_parms;
16040 s = s->level_chain)
16041 if (s->kind == sk_function_parms)
16042 template_parm_lists_apply = false;
16044 /* Look up a qualified name in the usual way. */
16045 if (parser->scope)
16047 tree decl;
16048 tree ambiguous_decls;
16050 decl = cp_parser_lookup_name (parser, identifier,
16051 tag_type,
16052 /*is_template=*/false,
16053 /*is_namespace=*/false,
16054 /*check_dependency=*/true,
16055 &ambiguous_decls,
16056 token->location);
16058 /* If the lookup was ambiguous, an error will already have been
16059 issued. */
16060 if (ambiguous_decls)
16061 return error_mark_node;
16063 /* If we are parsing friend declaration, DECL may be a
16064 TEMPLATE_DECL tree node here. However, we need to check
16065 whether this TEMPLATE_DECL results in valid code. Consider
16066 the following example:
16068 namespace N {
16069 template <class T> class C {};
16071 class X {
16072 template <class T> friend class N::C; // #1, valid code
16074 template <class T> class Y {
16075 friend class N::C; // #2, invalid code
16078 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16079 name lookup of `N::C'. We see that friend declaration must
16080 be template for the code to be valid. Note that
16081 processing_template_decl does not work here since it is
16082 always 1 for the above two cases. */
16084 decl = (cp_parser_maybe_treat_template_as_class
16085 (decl, /*tag_name_p=*/is_friend
16086 && template_parm_lists_apply));
16088 if (TREE_CODE (decl) != TYPE_DECL)
16090 cp_parser_diagnose_invalid_type_name (parser,
16091 identifier,
16092 token->location);
16093 return error_mark_node;
16096 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16098 bool allow_template = (template_parm_lists_apply
16099 || DECL_SELF_REFERENCE_P (decl));
16100 type = check_elaborated_type_specifier (tag_type, decl,
16101 allow_template);
16103 if (type == error_mark_node)
16104 return error_mark_node;
16107 /* Forward declarations of nested types, such as
16109 class C1::C2;
16110 class C1::C2::C3;
16112 are invalid unless all components preceding the final '::'
16113 are complete. If all enclosing types are complete, these
16114 declarations become merely pointless.
16116 Invalid forward declarations of nested types are errors
16117 caught elsewhere in parsing. Those that are pointless arrive
16118 here. */
16120 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16121 && !is_friend && !processing_explicit_instantiation)
16122 warning (0, "declaration %qD does not declare anything", decl);
16124 type = TREE_TYPE (decl);
16126 else
16128 /* An elaborated-type-specifier sometimes introduces a new type and
16129 sometimes names an existing type. Normally, the rule is that it
16130 introduces a new type only if there is not an existing type of
16131 the same name already in scope. For example, given:
16133 struct S {};
16134 void f() { struct S s; }
16136 the `struct S' in the body of `f' is the same `struct S' as in
16137 the global scope; the existing definition is used. However, if
16138 there were no global declaration, this would introduce a new
16139 local class named `S'.
16141 An exception to this rule applies to the following code:
16143 namespace N { struct S; }
16145 Here, the elaborated-type-specifier names a new type
16146 unconditionally; even if there is already an `S' in the
16147 containing scope this declaration names a new type.
16148 This exception only applies if the elaborated-type-specifier
16149 forms the complete declaration:
16151 [class.name]
16153 A declaration consisting solely of `class-key identifier ;' is
16154 either a redeclaration of the name in the current scope or a
16155 forward declaration of the identifier as a class name. It
16156 introduces the name into the current scope.
16158 We are in this situation precisely when the next token is a `;'.
16160 An exception to the exception is that a `friend' declaration does
16161 *not* name a new type; i.e., given:
16163 struct S { friend struct T; };
16165 `T' is not a new type in the scope of `S'.
16167 Also, `new struct S' or `sizeof (struct S)' never results in the
16168 definition of a new type; a new type can only be declared in a
16169 declaration context. */
16171 tag_scope ts;
16172 bool template_p;
16174 if (is_friend)
16175 /* Friends have special name lookup rules. */
16176 ts = ts_within_enclosing_non_class;
16177 else if (is_declaration
16178 && cp_lexer_next_token_is (parser->lexer,
16179 CPP_SEMICOLON))
16180 /* This is a `class-key identifier ;' */
16181 ts = ts_current;
16182 else
16183 ts = ts_global;
16185 template_p =
16186 (template_parm_lists_apply
16187 && (cp_parser_next_token_starts_class_definition_p (parser)
16188 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16189 /* An unqualified name was used to reference this type, so
16190 there were no qualifying templates. */
16191 if (template_parm_lists_apply
16192 && !cp_parser_check_template_parameters (parser,
16193 /*num_templates=*/0,
16194 token->location,
16195 /*declarator=*/NULL))
16196 return error_mark_node;
16197 type = xref_tag (tag_type, identifier, ts, template_p);
16201 if (type == error_mark_node)
16202 return error_mark_node;
16204 /* Allow attributes on forward declarations of classes. */
16205 if (attributes)
16207 if (TREE_CODE (type) == TYPENAME_TYPE)
16208 warning (OPT_Wattributes,
16209 "attributes ignored on uninstantiated type");
16210 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16211 && ! processing_explicit_instantiation)
16212 warning (OPT_Wattributes,
16213 "attributes ignored on template instantiation");
16214 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16215 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16216 else
16217 warning (OPT_Wattributes,
16218 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16221 if (tag_type != enum_type)
16223 /* Indicate whether this class was declared as a `class' or as a
16224 `struct'. */
16225 if (TREE_CODE (type) == RECORD_TYPE)
16226 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16227 cp_parser_check_class_key (tag_type, type);
16230 /* A "<" cannot follow an elaborated type specifier. If that
16231 happens, the user was probably trying to form a template-id. */
16232 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16233 token->location);
16235 return type;
16238 /* Parse an enum-specifier.
16240 enum-specifier:
16241 enum-head { enumerator-list [opt] }
16242 enum-head { enumerator-list , } [C++0x]
16244 enum-head:
16245 enum-key identifier [opt] enum-base [opt]
16246 enum-key nested-name-specifier identifier enum-base [opt]
16248 enum-key:
16249 enum
16250 enum class [C++0x]
16251 enum struct [C++0x]
16253 enum-base: [C++0x]
16254 : type-specifier-seq
16256 opaque-enum-specifier:
16257 enum-key identifier enum-base [opt] ;
16259 GNU Extensions:
16260 enum-key attributes[opt] identifier [opt] enum-base [opt]
16261 { enumerator-list [opt] }attributes[opt]
16262 enum-key attributes[opt] identifier [opt] enum-base [opt]
16263 { enumerator-list, }attributes[opt] [C++0x]
16265 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16266 if the token stream isn't an enum-specifier after all. */
16268 static tree
16269 cp_parser_enum_specifier (cp_parser* parser)
16271 tree identifier;
16272 tree type = NULL_TREE;
16273 tree prev_scope;
16274 tree nested_name_specifier = NULL_TREE;
16275 tree attributes;
16276 bool scoped_enum_p = false;
16277 bool has_underlying_type = false;
16278 bool nested_being_defined = false;
16279 bool new_value_list = false;
16280 bool is_new_type = false;
16281 bool is_anonymous = false;
16282 tree underlying_type = NULL_TREE;
16283 cp_token *type_start_token = NULL;
16284 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16286 parser->colon_corrects_to_scope_p = false;
16288 /* Parse tentatively so that we can back up if we don't find a
16289 enum-specifier. */
16290 cp_parser_parse_tentatively (parser);
16292 /* Caller guarantees that the current token is 'enum', an identifier
16293 possibly follows, and the token after that is an opening brace.
16294 If we don't have an identifier, fabricate an anonymous name for
16295 the enumeration being defined. */
16296 cp_lexer_consume_token (parser->lexer);
16298 /* Parse the "class" or "struct", which indicates a scoped
16299 enumeration type in C++0x. */
16300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16301 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16303 if (cxx_dialect < cxx11)
16304 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16306 /* Consume the `struct' or `class' token. */
16307 cp_lexer_consume_token (parser->lexer);
16309 scoped_enum_p = true;
16312 attributes = cp_parser_attributes_opt (parser);
16314 /* Clear the qualification. */
16315 parser->scope = NULL_TREE;
16316 parser->qualifying_scope = NULL_TREE;
16317 parser->object_scope = NULL_TREE;
16319 /* Figure out in what scope the declaration is being placed. */
16320 prev_scope = current_scope ();
16322 type_start_token = cp_lexer_peek_token (parser->lexer);
16324 push_deferring_access_checks (dk_no_check);
16325 nested_name_specifier
16326 = cp_parser_nested_name_specifier_opt (parser,
16327 /*typename_keyword_p=*/true,
16328 /*check_dependency_p=*/false,
16329 /*type_p=*/false,
16330 /*is_declaration=*/false);
16332 if (nested_name_specifier)
16334 tree name;
16336 identifier = cp_parser_identifier (parser);
16337 name = cp_parser_lookup_name (parser, identifier,
16338 enum_type,
16339 /*is_template=*/false,
16340 /*is_namespace=*/false,
16341 /*check_dependency=*/true,
16342 /*ambiguous_decls=*/NULL,
16343 input_location);
16344 if (name && name != error_mark_node)
16346 type = TREE_TYPE (name);
16347 if (TREE_CODE (type) == TYPENAME_TYPE)
16349 /* Are template enums allowed in ISO? */
16350 if (template_parm_scope_p ())
16351 pedwarn (type_start_token->location, OPT_Wpedantic,
16352 "%qD is an enumeration template", name);
16353 /* ignore a typename reference, for it will be solved by name
16354 in start_enum. */
16355 type = NULL_TREE;
16358 else if (nested_name_specifier == error_mark_node)
16359 /* We already issued an error. */;
16360 else
16361 error_at (type_start_token->location,
16362 "%qD is not an enumerator-name", identifier);
16364 else
16366 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16367 identifier = cp_parser_identifier (parser);
16368 else
16370 identifier = make_anon_name ();
16371 is_anonymous = true;
16372 if (scoped_enum_p)
16373 error_at (type_start_token->location,
16374 "anonymous scoped enum is not allowed");
16377 pop_deferring_access_checks ();
16379 /* Check for the `:' that denotes a specified underlying type in C++0x.
16380 Note that a ':' could also indicate a bitfield width, however. */
16381 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16383 cp_decl_specifier_seq type_specifiers;
16385 /* Consume the `:'. */
16386 cp_lexer_consume_token (parser->lexer);
16388 /* Parse the type-specifier-seq. */
16389 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16390 /*is_trailing_return=*/false,
16391 &type_specifiers);
16393 /* At this point this is surely not elaborated type specifier. */
16394 if (!cp_parser_parse_definitely (parser))
16395 return NULL_TREE;
16397 if (cxx_dialect < cxx11)
16398 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16400 has_underlying_type = true;
16402 /* If that didn't work, stop. */
16403 if (type_specifiers.type != error_mark_node)
16405 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
16406 /*initialized=*/0, NULL);
16407 if (underlying_type == error_mark_node
16408 || check_for_bare_parameter_packs (underlying_type))
16409 underlying_type = NULL_TREE;
16413 /* Look for the `{' but don't consume it yet. */
16414 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16416 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
16418 cp_parser_error (parser, "expected %<{%>");
16419 if (has_underlying_type)
16421 type = NULL_TREE;
16422 goto out;
16425 /* An opaque-enum-specifier must have a ';' here. */
16426 if ((scoped_enum_p || underlying_type)
16427 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16429 cp_parser_error (parser, "expected %<;%> or %<{%>");
16430 if (has_underlying_type)
16432 type = NULL_TREE;
16433 goto out;
16438 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
16439 return NULL_TREE;
16441 if (nested_name_specifier)
16443 if (CLASS_TYPE_P (nested_name_specifier))
16445 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
16446 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
16447 push_scope (nested_name_specifier);
16449 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16451 push_nested_namespace (nested_name_specifier);
16455 /* Issue an error message if type-definitions are forbidden here. */
16456 if (!cp_parser_check_type_definition (parser))
16457 type = error_mark_node;
16458 else
16459 /* Create the new type. We do this before consuming the opening
16460 brace so the enum will be recorded as being on the line of its
16461 tag (or the 'enum' keyword, if there is no tag). */
16462 type = start_enum (identifier, type, underlying_type,
16463 scoped_enum_p, &is_new_type);
16465 /* If the next token is not '{' it is an opaque-enum-specifier or an
16466 elaborated-type-specifier. */
16467 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16469 timevar_push (TV_PARSE_ENUM);
16470 if (nested_name_specifier
16471 && nested_name_specifier != error_mark_node)
16473 /* The following catches invalid code such as:
16474 enum class S<int>::E { A, B, C }; */
16475 if (!processing_specialization
16476 && CLASS_TYPE_P (nested_name_specifier)
16477 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
16478 error_at (type_start_token->location, "cannot add an enumerator "
16479 "list to a template instantiation");
16481 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
16483 error_at (type_start_token->location,
16484 "%<%T::%E%> has not been declared",
16485 TYPE_CONTEXT (nested_name_specifier),
16486 nested_name_specifier);
16487 type = error_mark_node;
16489 /* If that scope does not contain the scope in which the
16490 class was originally declared, the program is invalid. */
16491 else if (prev_scope && !is_ancestor (prev_scope,
16492 nested_name_specifier))
16494 if (at_namespace_scope_p ())
16495 error_at (type_start_token->location,
16496 "declaration of %qD in namespace %qD which does not "
16497 "enclose %qD",
16498 type, prev_scope, nested_name_specifier);
16499 else
16500 error_at (type_start_token->location,
16501 "declaration of %qD in %qD which does not "
16502 "enclose %qD",
16503 type, prev_scope, nested_name_specifier);
16504 type = error_mark_node;
16508 if (scoped_enum_p)
16509 begin_scope (sk_scoped_enum, type);
16511 /* Consume the opening brace. */
16512 cp_lexer_consume_token (parser->lexer);
16514 if (type == error_mark_node)
16515 ; /* Nothing to add */
16516 else if (OPAQUE_ENUM_P (type)
16517 || (cxx_dialect > cxx98 && processing_specialization))
16519 new_value_list = true;
16520 SET_OPAQUE_ENUM_P (type, false);
16521 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16523 else
16525 error_at (type_start_token->location,
16526 "multiple definition of %q#T", type);
16527 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16528 "previous definition here");
16529 type = error_mark_node;
16532 if (type == error_mark_node)
16533 cp_parser_skip_to_end_of_block_or_statement (parser);
16534 /* If the next token is not '}', then there are some enumerators. */
16535 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16537 if (is_anonymous && !scoped_enum_p)
16538 pedwarn (type_start_token->location, OPT_Wpedantic,
16539 "ISO C++ forbids empty anonymous enum");
16541 else
16542 cp_parser_enumerator_list (parser, type);
16544 /* Consume the final '}'. */
16545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16547 if (scoped_enum_p)
16548 finish_scope ();
16549 timevar_pop (TV_PARSE_ENUM);
16551 else
16553 /* If a ';' follows, then it is an opaque-enum-specifier
16554 and additional restrictions apply. */
16555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16557 if (is_anonymous)
16558 error_at (type_start_token->location,
16559 "opaque-enum-specifier without name");
16560 else if (nested_name_specifier)
16561 error_at (type_start_token->location,
16562 "opaque-enum-specifier must use a simple identifier");
16566 /* Look for trailing attributes to apply to this enumeration, and
16567 apply them if appropriate. */
16568 if (cp_parser_allow_gnu_extensions_p (parser))
16570 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16571 trailing_attr = chainon (trailing_attr, attributes);
16572 cplus_decl_attributes (&type,
16573 trailing_attr,
16574 (int) ATTR_FLAG_TYPE_IN_PLACE);
16577 /* Finish up the enumeration. */
16578 if (type != error_mark_node)
16580 if (new_value_list)
16581 finish_enum_value_list (type);
16582 if (is_new_type)
16583 finish_enum (type);
16586 if (nested_name_specifier)
16588 if (CLASS_TYPE_P (nested_name_specifier))
16590 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16591 pop_scope (nested_name_specifier);
16593 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16595 pop_nested_namespace (nested_name_specifier);
16598 out:
16599 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16600 return type;
16603 /* Parse an enumerator-list. The enumerators all have the indicated
16604 TYPE.
16606 enumerator-list:
16607 enumerator-definition
16608 enumerator-list , enumerator-definition */
16610 static void
16611 cp_parser_enumerator_list (cp_parser* parser, tree type)
16613 while (true)
16615 /* Parse an enumerator-definition. */
16616 cp_parser_enumerator_definition (parser, type);
16618 /* If the next token is not a ',', we've reached the end of
16619 the list. */
16620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16621 break;
16622 /* Otherwise, consume the `,' and keep going. */
16623 cp_lexer_consume_token (parser->lexer);
16624 /* If the next token is a `}', there is a trailing comma. */
16625 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16627 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16628 pedwarn (input_location, OPT_Wpedantic,
16629 "comma at end of enumerator list");
16630 break;
16635 /* Parse an enumerator-definition. The enumerator has the indicated
16636 TYPE.
16638 enumerator-definition:
16639 enumerator
16640 enumerator = constant-expression
16642 enumerator:
16643 identifier */
16645 static void
16646 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16648 tree identifier;
16649 tree value;
16650 location_t loc;
16652 /* Save the input location because we are interested in the location
16653 of the identifier and not the location of the explicit value. */
16654 loc = cp_lexer_peek_token (parser->lexer)->location;
16656 /* Look for the identifier. */
16657 identifier = cp_parser_identifier (parser);
16658 if (identifier == error_mark_node)
16659 return;
16661 /* If the next token is an '=', then there is an explicit value. */
16662 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16664 /* Consume the `=' token. */
16665 cp_lexer_consume_token (parser->lexer);
16666 /* Parse the value. */
16667 value = cp_parser_constant_expression (parser);
16669 else
16670 value = NULL_TREE;
16672 /* If we are processing a template, make sure the initializer of the
16673 enumerator doesn't contain any bare template parameter pack. */
16674 if (check_for_bare_parameter_packs (value))
16675 value = error_mark_node;
16677 /* Create the enumerator. */
16678 build_enumerator (identifier, value, type, loc);
16681 /* Parse a namespace-name.
16683 namespace-name:
16684 original-namespace-name
16685 namespace-alias
16687 Returns the NAMESPACE_DECL for the namespace. */
16689 static tree
16690 cp_parser_namespace_name (cp_parser* parser)
16692 tree identifier;
16693 tree namespace_decl;
16695 cp_token *token = cp_lexer_peek_token (parser->lexer);
16697 /* Get the name of the namespace. */
16698 identifier = cp_parser_identifier (parser);
16699 if (identifier == error_mark_node)
16700 return error_mark_node;
16702 /* Look up the identifier in the currently active scope. Look only
16703 for namespaces, due to:
16705 [basic.lookup.udir]
16707 When looking up a namespace-name in a using-directive or alias
16708 definition, only namespace names are considered.
16710 And:
16712 [basic.lookup.qual]
16714 During the lookup of a name preceding the :: scope resolution
16715 operator, object, function, and enumerator names are ignored.
16717 (Note that cp_parser_qualifying_entity only calls this
16718 function if the token after the name is the scope resolution
16719 operator.) */
16720 namespace_decl = cp_parser_lookup_name (parser, identifier,
16721 none_type,
16722 /*is_template=*/false,
16723 /*is_namespace=*/true,
16724 /*check_dependency=*/true,
16725 /*ambiguous_decls=*/NULL,
16726 token->location);
16727 /* If it's not a namespace, issue an error. */
16728 if (namespace_decl == error_mark_node
16729 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16731 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16732 error_at (token->location, "%qD is not a namespace-name", identifier);
16733 cp_parser_error (parser, "expected namespace-name");
16734 namespace_decl = error_mark_node;
16737 return namespace_decl;
16740 /* Parse a namespace-definition.
16742 namespace-definition:
16743 named-namespace-definition
16744 unnamed-namespace-definition
16746 named-namespace-definition:
16747 original-namespace-definition
16748 extension-namespace-definition
16750 original-namespace-definition:
16751 namespace identifier { namespace-body }
16753 extension-namespace-definition:
16754 namespace original-namespace-name { namespace-body }
16756 unnamed-namespace-definition:
16757 namespace { namespace-body } */
16759 static void
16760 cp_parser_namespace_definition (cp_parser* parser)
16762 tree identifier, attribs;
16763 bool has_visibility;
16764 bool is_inline;
16766 cp_ensure_no_omp_declare_simd (parser);
16767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16769 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16770 is_inline = true;
16771 cp_lexer_consume_token (parser->lexer);
16773 else
16774 is_inline = false;
16776 /* Look for the `namespace' keyword. */
16777 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16779 /* Get the name of the namespace. We do not attempt to distinguish
16780 between an original-namespace-definition and an
16781 extension-namespace-definition at this point. The semantic
16782 analysis routines are responsible for that. */
16783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16784 identifier = cp_parser_identifier (parser);
16785 else
16786 identifier = NULL_TREE;
16788 /* Parse any specified attributes. */
16789 attribs = cp_parser_attributes_opt (parser);
16791 /* Look for the `{' to start the namespace. */
16792 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16793 /* Start the namespace. */
16794 push_namespace (identifier);
16796 /* "inline namespace" is equivalent to a stub namespace definition
16797 followed by a strong using directive. */
16798 if (is_inline)
16800 tree name_space = current_namespace;
16801 /* Set up namespace association. */
16802 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16803 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16804 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16805 /* Import the contents of the inline namespace. */
16806 pop_namespace ();
16807 do_using_directive (name_space);
16808 push_namespace (identifier);
16811 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16813 /* Parse the body of the namespace. */
16814 cp_parser_namespace_body (parser);
16816 if (has_visibility)
16817 pop_visibility (1);
16819 /* Finish the namespace. */
16820 pop_namespace ();
16821 /* Look for the final `}'. */
16822 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16825 /* Parse a namespace-body.
16827 namespace-body:
16828 declaration-seq [opt] */
16830 static void
16831 cp_parser_namespace_body (cp_parser* parser)
16833 cp_parser_declaration_seq_opt (parser);
16836 /* Parse a namespace-alias-definition.
16838 namespace-alias-definition:
16839 namespace identifier = qualified-namespace-specifier ; */
16841 static void
16842 cp_parser_namespace_alias_definition (cp_parser* parser)
16844 tree identifier;
16845 tree namespace_specifier;
16847 cp_token *token = cp_lexer_peek_token (parser->lexer);
16849 /* Look for the `namespace' keyword. */
16850 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16851 /* Look for the identifier. */
16852 identifier = cp_parser_identifier (parser);
16853 if (identifier == error_mark_node)
16854 return;
16855 /* Look for the `=' token. */
16856 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16857 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16859 error_at (token->location, "%<namespace%> definition is not allowed here");
16860 /* Skip the definition. */
16861 cp_lexer_consume_token (parser->lexer);
16862 if (cp_parser_skip_to_closing_brace (parser))
16863 cp_lexer_consume_token (parser->lexer);
16864 return;
16866 cp_parser_require (parser, CPP_EQ, RT_EQ);
16867 /* Look for the qualified-namespace-specifier. */
16868 namespace_specifier
16869 = cp_parser_qualified_namespace_specifier (parser);
16870 /* Look for the `;' token. */
16871 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16873 /* Register the alias in the symbol table. */
16874 do_namespace_alias (identifier, namespace_specifier);
16877 /* Parse a qualified-namespace-specifier.
16879 qualified-namespace-specifier:
16880 :: [opt] nested-name-specifier [opt] namespace-name
16882 Returns a NAMESPACE_DECL corresponding to the specified
16883 namespace. */
16885 static tree
16886 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16888 /* Look for the optional `::'. */
16889 cp_parser_global_scope_opt (parser,
16890 /*current_scope_valid_p=*/false);
16892 /* Look for the optional nested-name-specifier. */
16893 cp_parser_nested_name_specifier_opt (parser,
16894 /*typename_keyword_p=*/false,
16895 /*check_dependency_p=*/true,
16896 /*type_p=*/false,
16897 /*is_declaration=*/true);
16899 return cp_parser_namespace_name (parser);
16902 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16903 access declaration.
16905 using-declaration:
16906 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16907 using :: unqualified-id ;
16909 access-declaration:
16910 qualified-id ;
16914 static bool
16915 cp_parser_using_declaration (cp_parser* parser,
16916 bool access_declaration_p)
16918 cp_token *token;
16919 bool typename_p = false;
16920 bool global_scope_p;
16921 tree decl;
16922 tree identifier;
16923 tree qscope;
16924 int oldcount = errorcount;
16925 cp_token *diag_token = NULL;
16927 if (access_declaration_p)
16929 diag_token = cp_lexer_peek_token (parser->lexer);
16930 cp_parser_parse_tentatively (parser);
16932 else
16934 /* Look for the `using' keyword. */
16935 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16937 /* Peek at the next token. */
16938 token = cp_lexer_peek_token (parser->lexer);
16939 /* See if it's `typename'. */
16940 if (token->keyword == RID_TYPENAME)
16942 /* Remember that we've seen it. */
16943 typename_p = true;
16944 /* Consume the `typename' token. */
16945 cp_lexer_consume_token (parser->lexer);
16949 /* Look for the optional global scope qualification. */
16950 global_scope_p
16951 = (cp_parser_global_scope_opt (parser,
16952 /*current_scope_valid_p=*/false)
16953 != NULL_TREE);
16955 /* If we saw `typename', or didn't see `::', then there must be a
16956 nested-name-specifier present. */
16957 if (typename_p || !global_scope_p)
16959 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16960 /*check_dependency_p=*/true,
16961 /*type_p=*/false,
16962 /*is_declaration=*/true);
16963 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16965 cp_parser_skip_to_end_of_block_or_statement (parser);
16966 return false;
16969 /* Otherwise, we could be in either of the two productions. In that
16970 case, treat the nested-name-specifier as optional. */
16971 else
16972 qscope = cp_parser_nested_name_specifier_opt (parser,
16973 /*typename_keyword_p=*/false,
16974 /*check_dependency_p=*/true,
16975 /*type_p=*/false,
16976 /*is_declaration=*/true);
16977 if (!qscope)
16978 qscope = global_namespace;
16979 else if (UNSCOPED_ENUM_P (qscope))
16980 qscope = CP_TYPE_CONTEXT (qscope);
16982 if (access_declaration_p && cp_parser_error_occurred (parser))
16983 /* Something has already gone wrong; there's no need to parse
16984 further. Since an error has occurred, the return value of
16985 cp_parser_parse_definitely will be false, as required. */
16986 return cp_parser_parse_definitely (parser);
16988 token = cp_lexer_peek_token (parser->lexer);
16989 /* Parse the unqualified-id. */
16990 identifier = cp_parser_unqualified_id (parser,
16991 /*template_keyword_p=*/false,
16992 /*check_dependency_p=*/true,
16993 /*declarator_p=*/true,
16994 /*optional_p=*/false);
16996 if (access_declaration_p)
16998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16999 cp_parser_simulate_error (parser);
17000 if (!cp_parser_parse_definitely (parser))
17001 return false;
17004 /* The function we call to handle a using-declaration is different
17005 depending on what scope we are in. */
17006 if (qscope == error_mark_node || identifier == error_mark_node)
17008 else if (!identifier_p (identifier)
17009 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17010 /* [namespace.udecl]
17012 A using declaration shall not name a template-id. */
17013 error_at (token->location,
17014 "a template-id may not appear in a using-declaration");
17015 else
17017 if (at_class_scope_p ())
17019 /* Create the USING_DECL. */
17020 decl = do_class_using_decl (parser->scope, identifier);
17022 if (decl && typename_p)
17023 USING_DECL_TYPENAME_P (decl) = 1;
17025 if (check_for_bare_parameter_packs (decl))
17027 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17028 return false;
17030 else
17031 /* Add it to the list of members in this class. */
17032 finish_member_declaration (decl);
17034 else
17036 decl = cp_parser_lookup_name_simple (parser,
17037 identifier,
17038 token->location);
17039 if (decl == error_mark_node)
17040 cp_parser_name_lookup_error (parser, identifier,
17041 decl, NLE_NULL,
17042 token->location);
17043 else if (check_for_bare_parameter_packs (decl))
17045 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17046 return false;
17048 else if (!at_namespace_scope_p ())
17049 do_local_using_decl (decl, qscope, identifier);
17050 else
17051 do_toplevel_using_decl (decl, qscope, identifier);
17055 /* Look for the final `;'. */
17056 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17058 if (access_declaration_p && errorcount == oldcount)
17059 warning_at (diag_token->location, OPT_Wdeprecated,
17060 "access declarations are deprecated "
17061 "in favour of using-declarations; "
17062 "suggestion: add the %<using%> keyword");
17064 return true;
17067 /* Parse an alias-declaration.
17069 alias-declaration:
17070 using identifier attribute-specifier-seq [opt] = type-id */
17072 static tree
17073 cp_parser_alias_declaration (cp_parser* parser)
17075 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17076 location_t id_location;
17077 cp_declarator *declarator;
17078 cp_decl_specifier_seq decl_specs;
17079 bool member_p;
17080 const char *saved_message = NULL;
17082 /* Look for the `using' keyword. */
17083 cp_token *using_token
17084 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17085 if (using_token == NULL)
17086 return error_mark_node;
17088 id_location = cp_lexer_peek_token (parser->lexer)->location;
17089 id = cp_parser_identifier (parser);
17090 if (id == error_mark_node)
17091 return error_mark_node;
17093 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17094 attributes = cp_parser_attributes_opt (parser);
17095 if (attributes == error_mark_node)
17096 return error_mark_node;
17098 cp_parser_require (parser, CPP_EQ, RT_EQ);
17100 if (cp_parser_error_occurred (parser))
17101 return error_mark_node;
17103 cp_parser_commit_to_tentative_parse (parser);
17105 /* Now we are going to parse the type-id of the declaration. */
17108 [dcl.type]/3 says:
17110 "A type-specifier-seq shall not define a class or enumeration
17111 unless it appears in the type-id of an alias-declaration (7.1.3) that
17112 is not the declaration of a template-declaration."
17114 In other words, if we currently are in an alias template, the
17115 type-id should not define a type.
17117 So let's set parser->type_definition_forbidden_message in that
17118 case; cp_parser_check_type_definition (called by
17119 cp_parser_class_specifier) will then emit an error if a type is
17120 defined in the type-id. */
17121 if (parser->num_template_parameter_lists)
17123 saved_message = parser->type_definition_forbidden_message;
17124 parser->type_definition_forbidden_message =
17125 G_("types may not be defined in alias template declarations");
17128 type = cp_parser_type_id (parser);
17130 /* Restore the error message if need be. */
17131 if (parser->num_template_parameter_lists)
17132 parser->type_definition_forbidden_message = saved_message;
17134 if (type == error_mark_node
17135 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17137 cp_parser_skip_to_end_of_block_or_statement (parser);
17138 return error_mark_node;
17141 /* A typedef-name can also be introduced by an alias-declaration. The
17142 identifier following the using keyword becomes a typedef-name. It has
17143 the same semantics as if it were introduced by the typedef
17144 specifier. In particular, it does not define a new type and it shall
17145 not appear in the type-id. */
17147 clear_decl_specs (&decl_specs);
17148 decl_specs.type = type;
17149 if (attributes != NULL_TREE)
17151 decl_specs.attributes = attributes;
17152 set_and_check_decl_spec_loc (&decl_specs,
17153 ds_attribute,
17154 attrs_token);
17156 set_and_check_decl_spec_loc (&decl_specs,
17157 ds_typedef,
17158 using_token);
17159 set_and_check_decl_spec_loc (&decl_specs,
17160 ds_alias,
17161 using_token);
17163 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17164 declarator->id_loc = id_location;
17166 member_p = at_class_scope_p ();
17167 if (member_p)
17168 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17169 NULL_TREE, attributes);
17170 else
17171 decl = start_decl (declarator, &decl_specs, 0,
17172 attributes, NULL_TREE, &pushed_scope);
17173 if (decl == error_mark_node)
17174 return decl;
17176 // Attach constraints to the alias declaration.
17177 if (flag_concepts && current_template_parms)
17179 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17180 tree constr = build_constraints (reqs, NULL_TREE);
17181 set_constraints (decl, constr);
17184 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17186 if (pushed_scope)
17187 pop_scope (pushed_scope);
17189 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17190 added into the symbol table; otherwise, return the TYPE_DECL. */
17191 if (DECL_LANG_SPECIFIC (decl)
17192 && DECL_TEMPLATE_INFO (decl)
17193 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17195 decl = DECL_TI_TEMPLATE (decl);
17196 if (member_p)
17197 check_member_template (decl);
17200 return decl;
17203 /* Parse a using-directive.
17205 using-directive:
17206 using namespace :: [opt] nested-name-specifier [opt]
17207 namespace-name ; */
17209 static void
17210 cp_parser_using_directive (cp_parser* parser)
17212 tree namespace_decl;
17213 tree attribs;
17215 /* Look for the `using' keyword. */
17216 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17217 /* And the `namespace' keyword. */
17218 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17219 /* Look for the optional `::' operator. */
17220 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17221 /* And the optional nested-name-specifier. */
17222 cp_parser_nested_name_specifier_opt (parser,
17223 /*typename_keyword_p=*/false,
17224 /*check_dependency_p=*/true,
17225 /*type_p=*/false,
17226 /*is_declaration=*/true);
17227 /* Get the namespace being used. */
17228 namespace_decl = cp_parser_namespace_name (parser);
17229 /* And any specified attributes. */
17230 attribs = cp_parser_attributes_opt (parser);
17231 /* Update the symbol table. */
17232 parse_using_directive (namespace_decl, attribs);
17233 /* Look for the final `;'. */
17234 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17237 /* Parse an asm-definition.
17239 asm-definition:
17240 asm ( string-literal ) ;
17242 GNU Extension:
17244 asm-definition:
17245 asm volatile [opt] ( string-literal ) ;
17246 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
17247 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17248 : asm-operand-list [opt] ) ;
17249 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17250 : asm-operand-list [opt]
17251 : asm-clobber-list [opt] ) ;
17252 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
17253 : asm-clobber-list [opt]
17254 : asm-goto-list ) ; */
17256 static void
17257 cp_parser_asm_definition (cp_parser* parser)
17259 tree string;
17260 tree outputs = NULL_TREE;
17261 tree inputs = NULL_TREE;
17262 tree clobbers = NULL_TREE;
17263 tree labels = NULL_TREE;
17264 tree asm_stmt;
17265 bool volatile_p = false;
17266 bool extended_p = false;
17267 bool invalid_inputs_p = false;
17268 bool invalid_outputs_p = false;
17269 bool goto_p = false;
17270 required_token missing = RT_NONE;
17272 /* Look for the `asm' keyword. */
17273 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
17275 if (parser->in_function_body
17276 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
17278 error ("%<asm%> in %<constexpr%> function");
17279 cp_function_chain->invalid_constexpr = true;
17282 /* See if the next token is `volatile'. */
17283 if (cp_parser_allow_gnu_extensions_p (parser)
17284 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
17286 /* Remember that we saw the `volatile' keyword. */
17287 volatile_p = true;
17288 /* Consume the token. */
17289 cp_lexer_consume_token (parser->lexer);
17291 if (cp_parser_allow_gnu_extensions_p (parser)
17292 && parser->in_function_body
17293 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
17295 /* Remember that we saw the `goto' keyword. */
17296 goto_p = true;
17297 /* Consume the token. */
17298 cp_lexer_consume_token (parser->lexer);
17300 /* Look for the opening `('. */
17301 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
17302 return;
17303 /* Look for the string. */
17304 string = cp_parser_string_literal (parser, false, false);
17305 if (string == error_mark_node)
17307 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17308 /*consume_paren=*/true);
17309 return;
17312 /* If we're allowing GNU extensions, check for the extended assembly
17313 syntax. Unfortunately, the `:' tokens need not be separated by
17314 a space in C, and so, for compatibility, we tolerate that here
17315 too. Doing that means that we have to treat the `::' operator as
17316 two `:' tokens. */
17317 if (cp_parser_allow_gnu_extensions_p (parser)
17318 && parser->in_function_body
17319 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
17320 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
17322 bool inputs_p = false;
17323 bool clobbers_p = false;
17324 bool labels_p = false;
17326 /* The extended syntax was used. */
17327 extended_p = true;
17329 /* Look for outputs. */
17330 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17332 /* Consume the `:'. */
17333 cp_lexer_consume_token (parser->lexer);
17334 /* Parse the output-operands. */
17335 if (cp_lexer_next_token_is_not (parser->lexer,
17336 CPP_COLON)
17337 && cp_lexer_next_token_is_not (parser->lexer,
17338 CPP_SCOPE)
17339 && cp_lexer_next_token_is_not (parser->lexer,
17340 CPP_CLOSE_PAREN)
17341 && !goto_p)
17342 outputs = cp_parser_asm_operand_list (parser);
17344 if (outputs == error_mark_node)
17345 invalid_outputs_p = true;
17347 /* If the next token is `::', there are no outputs, and the
17348 next token is the beginning of the inputs. */
17349 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17350 /* The inputs are coming next. */
17351 inputs_p = true;
17353 /* Look for inputs. */
17354 if (inputs_p
17355 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17357 /* Consume the `:' or `::'. */
17358 cp_lexer_consume_token (parser->lexer);
17359 /* Parse the output-operands. */
17360 if (cp_lexer_next_token_is_not (parser->lexer,
17361 CPP_COLON)
17362 && cp_lexer_next_token_is_not (parser->lexer,
17363 CPP_SCOPE)
17364 && cp_lexer_next_token_is_not (parser->lexer,
17365 CPP_CLOSE_PAREN))
17366 inputs = cp_parser_asm_operand_list (parser);
17368 if (inputs == error_mark_node)
17369 invalid_inputs_p = true;
17371 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17372 /* The clobbers are coming next. */
17373 clobbers_p = true;
17375 /* Look for clobbers. */
17376 if (clobbers_p
17377 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17379 clobbers_p = true;
17380 /* Consume the `:' or `::'. */
17381 cp_lexer_consume_token (parser->lexer);
17382 /* Parse the clobbers. */
17383 if (cp_lexer_next_token_is_not (parser->lexer,
17384 CPP_COLON)
17385 && cp_lexer_next_token_is_not (parser->lexer,
17386 CPP_CLOSE_PAREN))
17387 clobbers = cp_parser_asm_clobber_list (parser);
17389 else if (goto_p
17390 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17391 /* The labels are coming next. */
17392 labels_p = true;
17394 /* Look for labels. */
17395 if (labels_p
17396 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
17398 labels_p = true;
17399 /* Consume the `:' or `::'. */
17400 cp_lexer_consume_token (parser->lexer);
17401 /* Parse the labels. */
17402 labels = cp_parser_asm_label_list (parser);
17405 if (goto_p && !labels_p)
17406 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
17408 else if (goto_p)
17409 missing = RT_COLON_SCOPE;
17411 /* Look for the closing `)'. */
17412 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
17413 missing ? missing : RT_CLOSE_PAREN))
17414 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17415 /*consume_paren=*/true);
17416 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17418 if (!invalid_inputs_p && !invalid_outputs_p)
17420 /* Create the ASM_EXPR. */
17421 if (parser->in_function_body)
17423 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
17424 inputs, clobbers, labels);
17425 /* If the extended syntax was not used, mark the ASM_EXPR. */
17426 if (!extended_p)
17428 tree temp = asm_stmt;
17429 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
17430 temp = TREE_OPERAND (temp, 0);
17432 ASM_INPUT_P (temp) = 1;
17435 else
17436 symtab->finalize_toplevel_asm (string);
17440 /* Declarators [gram.dcl.decl] */
17442 /* Parse an init-declarator.
17444 init-declarator:
17445 declarator initializer [opt]
17447 GNU Extension:
17449 init-declarator:
17450 declarator asm-specification [opt] attributes [opt] initializer [opt]
17452 function-definition:
17453 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17454 function-body
17455 decl-specifier-seq [opt] declarator function-try-block
17457 GNU Extension:
17459 function-definition:
17460 __extension__ function-definition
17462 TM Extension:
17464 function-definition:
17465 decl-specifier-seq [opt] declarator function-transaction-block
17467 The DECL_SPECIFIERS apply to this declarator. Returns a
17468 representation of the entity declared. If MEMBER_P is TRUE, then
17469 this declarator appears in a class scope. The new DECL created by
17470 this declarator is returned.
17472 The CHECKS are access checks that should be performed once we know
17473 what entity is being declared (and, therefore, what classes have
17474 befriended it).
17476 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17477 for a function-definition here as well. If the declarator is a
17478 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17479 be TRUE upon return. By that point, the function-definition will
17480 have been completely parsed.
17482 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17483 is FALSE.
17485 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17486 parsed declaration if it is an uninitialized single declarator not followed
17487 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17488 if present, will not be consumed. If returned, this declarator will be
17489 created with SD_INITIALIZED but will not call cp_finish_decl.
17491 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
17492 and there is an initializer, the pointed location_t is set to the
17493 location of the '=' or `(', or '{' in C++11 token introducing the
17494 initializer. */
17496 static tree
17497 cp_parser_init_declarator (cp_parser* parser,
17498 cp_decl_specifier_seq *decl_specifiers,
17499 vec<deferred_access_check, va_gc> *checks,
17500 bool function_definition_allowed_p,
17501 bool member_p,
17502 int declares_class_or_enum,
17503 bool* function_definition_p,
17504 tree* maybe_range_for_decl,
17505 location_t* init_loc)
17507 cp_token *token = NULL, *asm_spec_start_token = NULL,
17508 *attributes_start_token = NULL;
17509 cp_declarator *declarator;
17510 tree prefix_attributes;
17511 tree attributes = NULL;
17512 tree asm_specification;
17513 tree initializer;
17514 tree decl = NULL_TREE;
17515 tree scope;
17516 int is_initialized;
17517 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17518 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17519 "(...)". */
17520 enum cpp_ttype initialization_kind;
17521 bool is_direct_init = false;
17522 bool is_non_constant_init;
17523 int ctor_dtor_or_conv_p;
17524 bool friend_p = cp_parser_friend_p (decl_specifiers);
17525 tree pushed_scope = NULL_TREE;
17526 bool range_for_decl_p = false;
17527 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17528 location_t tmp_init_loc = UNKNOWN_LOCATION;
17530 /* Gather the attributes that were provided with the
17531 decl-specifiers. */
17532 prefix_attributes = decl_specifiers->attributes;
17534 /* Assume that this is not the declarator for a function
17535 definition. */
17536 if (function_definition_p)
17537 *function_definition_p = false;
17539 /* Default arguments are only permitted for function parameters. */
17540 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17541 parser->default_arg_ok_p = false;
17543 /* Defer access checks while parsing the declarator; we cannot know
17544 what names are accessible until we know what is being
17545 declared. */
17546 resume_deferring_access_checks ();
17548 /* Parse the declarator. */
17549 token = cp_lexer_peek_token (parser->lexer);
17550 declarator
17551 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17552 &ctor_dtor_or_conv_p,
17553 /*parenthesized_p=*/NULL,
17554 member_p, friend_p);
17555 /* Gather up the deferred checks. */
17556 stop_deferring_access_checks ();
17558 parser->default_arg_ok_p = saved_default_arg_ok_p;
17560 /* If the DECLARATOR was erroneous, there's no need to go
17561 further. */
17562 if (declarator == cp_error_declarator)
17563 return error_mark_node;
17565 /* Check that the number of template-parameter-lists is OK. */
17566 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17567 token->location))
17568 return error_mark_node;
17570 if (declares_class_or_enum & 2)
17571 cp_parser_check_for_definition_in_return_type (declarator,
17572 decl_specifiers->type,
17573 decl_specifiers->locations[ds_type_spec]);
17575 /* Figure out what scope the entity declared by the DECLARATOR is
17576 located in. `grokdeclarator' sometimes changes the scope, so
17577 we compute it now. */
17578 scope = get_scope_of_declarator (declarator);
17580 /* Perform any lookups in the declared type which were thought to be
17581 dependent, but are not in the scope of the declarator. */
17582 decl_specifiers->type
17583 = maybe_update_decl_type (decl_specifiers->type, scope);
17585 /* If we're allowing GNU extensions, look for an
17586 asm-specification. */
17587 if (cp_parser_allow_gnu_extensions_p (parser))
17589 /* Look for an asm-specification. */
17590 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17591 asm_specification = cp_parser_asm_specification_opt (parser);
17593 else
17594 asm_specification = NULL_TREE;
17596 /* Look for attributes. */
17597 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17598 attributes = cp_parser_attributes_opt (parser);
17600 // Parse an optional trailing requires clause for the parsed declarator.
17602 /* Peek at the next token. */
17603 token = cp_lexer_peek_token (parser->lexer);
17605 bool bogus_implicit_tmpl = false;
17607 if (function_declarator_p (declarator))
17609 /* Check to see if the token indicates the start of a
17610 function-definition. */
17611 if (cp_parser_token_starts_function_definition_p (token))
17613 if (!function_definition_allowed_p)
17615 /* If a function-definition should not appear here, issue an
17616 error message. */
17617 cp_parser_error (parser,
17618 "a function-definition is not allowed here");
17619 return error_mark_node;
17622 location_t func_brace_location
17623 = cp_lexer_peek_token (parser->lexer)->location;
17625 /* Neither attributes nor an asm-specification are allowed
17626 on a function-definition. */
17627 if (asm_specification)
17628 error_at (asm_spec_start_token->location,
17629 "an asm-specification is not allowed "
17630 "on a function-definition");
17631 if (attributes)
17632 error_at (attributes_start_token->location,
17633 "attributes are not allowed "
17634 "on a function-definition");
17635 /* This is a function-definition. */
17636 *function_definition_p = true;
17638 /* Parse the function definition. */
17639 if (member_p)
17640 decl = cp_parser_save_member_function_body (parser,
17641 decl_specifiers,
17642 declarator,
17643 prefix_attributes);
17644 else
17645 decl =
17646 (cp_parser_function_definition_from_specifiers_and_declarator
17647 (parser, decl_specifiers, prefix_attributes, declarator));
17649 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17651 /* This is where the prologue starts... */
17652 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17653 = func_brace_location;
17656 return decl;
17659 else if (parser->fully_implicit_function_template_p)
17661 /* A non-template declaration involving a function parameter list
17662 containing an implicit template parameter will be made into a
17663 template. If the resulting declaration is not going to be an
17664 actual function then finish the template scope here to prevent it.
17665 An error message will be issued once we have a decl to talk about.
17667 FIXME probably we should do type deduction rather than create an
17668 implicit template, but the standard currently doesn't allow it. */
17669 bogus_implicit_tmpl = true;
17670 finish_fully_implicit_template (parser, NULL_TREE);
17673 /* [dcl.dcl]
17675 Only in function declarations for constructors, destructors, and
17676 type conversions can the decl-specifier-seq be omitted.
17678 We explicitly postpone this check past the point where we handle
17679 function-definitions because we tolerate function-definitions
17680 that are missing their return types in some modes. */
17681 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17683 cp_parser_error (parser,
17684 "expected constructor, destructor, or type conversion");
17685 return error_mark_node;
17688 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17689 if (token->type == CPP_EQ
17690 || token->type == CPP_OPEN_PAREN
17691 || token->type == CPP_OPEN_BRACE)
17693 is_initialized = SD_INITIALIZED;
17694 initialization_kind = token->type;
17695 if (maybe_range_for_decl)
17696 *maybe_range_for_decl = error_mark_node;
17697 tmp_init_loc = token->location;
17698 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17699 *init_loc = tmp_init_loc;
17701 if (token->type == CPP_EQ
17702 && function_declarator_p (declarator))
17704 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17705 if (t2->keyword == RID_DEFAULT)
17706 is_initialized = SD_DEFAULTED;
17707 else if (t2->keyword == RID_DELETE)
17708 is_initialized = SD_DELETED;
17711 else
17713 /* If the init-declarator isn't initialized and isn't followed by a
17714 `,' or `;', it's not a valid init-declarator. */
17715 if (token->type != CPP_COMMA
17716 && token->type != CPP_SEMICOLON)
17718 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17719 range_for_decl_p = true;
17720 else
17722 if (!maybe_range_for_decl)
17723 cp_parser_error (parser, "expected initializer");
17724 return error_mark_node;
17727 is_initialized = SD_UNINITIALIZED;
17728 initialization_kind = CPP_EOF;
17731 /* Because start_decl has side-effects, we should only call it if we
17732 know we're going ahead. By this point, we know that we cannot
17733 possibly be looking at any other construct. */
17734 cp_parser_commit_to_tentative_parse (parser);
17736 /* Enter the newly declared entry in the symbol table. If we're
17737 processing a declaration in a class-specifier, we wait until
17738 after processing the initializer. */
17739 if (!member_p)
17741 if (parser->in_unbraced_linkage_specification_p)
17742 decl_specifiers->storage_class = sc_extern;
17743 decl = start_decl (declarator, decl_specifiers,
17744 range_for_decl_p? SD_INITIALIZED : is_initialized,
17745 attributes, prefix_attributes, &pushed_scope);
17746 cp_finalize_omp_declare_simd (parser, decl);
17747 /* Adjust location of decl if declarator->id_loc is more appropriate:
17748 set, and decl wasn't merged with another decl, in which case its
17749 location would be different from input_location, and more accurate. */
17750 if (DECL_P (decl)
17751 && declarator->id_loc != UNKNOWN_LOCATION
17752 && DECL_SOURCE_LOCATION (decl) == input_location)
17753 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17755 else if (scope)
17756 /* Enter the SCOPE. That way unqualified names appearing in the
17757 initializer will be looked up in SCOPE. */
17758 pushed_scope = push_scope (scope);
17760 /* Perform deferred access control checks, now that we know in which
17761 SCOPE the declared entity resides. */
17762 if (!member_p && decl)
17764 tree saved_current_function_decl = NULL_TREE;
17766 /* If the entity being declared is a function, pretend that we
17767 are in its scope. If it is a `friend', it may have access to
17768 things that would not otherwise be accessible. */
17769 if (TREE_CODE (decl) == FUNCTION_DECL)
17771 saved_current_function_decl = current_function_decl;
17772 current_function_decl = decl;
17775 /* Perform access checks for template parameters. */
17776 cp_parser_perform_template_parameter_access_checks (checks);
17778 /* Perform the access control checks for the declarator and the
17779 decl-specifiers. */
17780 perform_deferred_access_checks (tf_warning_or_error);
17782 /* Restore the saved value. */
17783 if (TREE_CODE (decl) == FUNCTION_DECL)
17784 current_function_decl = saved_current_function_decl;
17787 /* Parse the initializer. */
17788 initializer = NULL_TREE;
17789 is_direct_init = false;
17790 is_non_constant_init = true;
17791 if (is_initialized)
17793 if (function_declarator_p (declarator))
17795 if (initialization_kind == CPP_EQ)
17796 initializer = cp_parser_pure_specifier (parser);
17797 else
17799 /* If the declaration was erroneous, we don't really
17800 know what the user intended, so just silently
17801 consume the initializer. */
17802 if (decl != error_mark_node)
17803 error_at (tmp_init_loc, "initializer provided for function");
17804 cp_parser_skip_to_closing_parenthesis (parser,
17805 /*recovering=*/true,
17806 /*or_comma=*/false,
17807 /*consume_paren=*/true);
17810 else
17812 /* We want to record the extra mangling scope for in-class
17813 initializers of class members and initializers of static data
17814 member templates. The former involves deferring
17815 parsing of the initializer until end of class as with default
17816 arguments. So right here we only handle the latter. */
17817 if (!member_p && processing_template_decl)
17818 start_lambda_scope (decl);
17819 initializer = cp_parser_initializer (parser,
17820 &is_direct_init,
17821 &is_non_constant_init);
17822 if (!member_p && processing_template_decl)
17823 finish_lambda_scope ();
17824 if (initializer == error_mark_node)
17825 cp_parser_skip_to_end_of_statement (parser);
17829 /* The old parser allows attributes to appear after a parenthesized
17830 initializer. Mark Mitchell proposed removing this functionality
17831 on the GCC mailing lists on 2002-08-13. This parser accepts the
17832 attributes -- but ignores them. */
17833 if (cp_parser_allow_gnu_extensions_p (parser)
17834 && initialization_kind == CPP_OPEN_PAREN)
17835 if (cp_parser_attributes_opt (parser))
17836 warning (OPT_Wattributes,
17837 "attributes after parenthesized initializer ignored");
17839 /* And now complain about a non-function implicit template. */
17840 if (bogus_implicit_tmpl)
17841 error_at (DECL_SOURCE_LOCATION (decl),
17842 "non-function %qD declared as implicit template", decl);
17844 /* For an in-class declaration, use `grokfield' to create the
17845 declaration. */
17846 if (member_p)
17848 if (pushed_scope)
17850 pop_scope (pushed_scope);
17851 pushed_scope = NULL_TREE;
17853 decl = grokfield (declarator, decl_specifiers,
17854 initializer, !is_non_constant_init,
17855 /*asmspec=*/NULL_TREE,
17856 chainon (attributes, prefix_attributes));
17857 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17858 cp_parser_save_default_args (parser, decl);
17859 cp_finalize_omp_declare_simd (parser, decl);
17862 /* Finish processing the declaration. But, skip member
17863 declarations. */
17864 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17866 cp_finish_decl (decl,
17867 initializer, !is_non_constant_init,
17868 asm_specification,
17869 /* If the initializer is in parentheses, then this is
17870 a direct-initialization, which means that an
17871 `explicit' constructor is OK. Otherwise, an
17872 `explicit' constructor cannot be used. */
17873 ((is_direct_init || !is_initialized)
17874 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17876 else if ((cxx_dialect != cxx98) && friend_p
17877 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17878 /* Core issue #226 (C++0x only): A default template-argument
17879 shall not be specified in a friend class template
17880 declaration. */
17881 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17882 /*is_partial=*/false, /*is_friend_decl=*/1);
17884 if (!friend_p && pushed_scope)
17885 pop_scope (pushed_scope);
17887 if (function_declarator_p (declarator)
17888 && parser->fully_implicit_function_template_p)
17890 if (member_p)
17891 decl = finish_fully_implicit_template (parser, decl);
17892 else
17893 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17896 return decl;
17899 /* Parse a declarator.
17901 declarator:
17902 direct-declarator
17903 ptr-operator declarator
17905 abstract-declarator:
17906 ptr-operator abstract-declarator [opt]
17907 direct-abstract-declarator
17909 GNU Extensions:
17911 declarator:
17912 attributes [opt] direct-declarator
17913 attributes [opt] ptr-operator declarator
17915 abstract-declarator:
17916 attributes [opt] ptr-operator abstract-declarator [opt]
17917 attributes [opt] direct-abstract-declarator
17919 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17920 detect constructor, destructor or conversion operators. It is set
17921 to -1 if the declarator is a name, and +1 if it is a
17922 function. Otherwise it is set to zero. Usually you just want to
17923 test for >0, but internally the negative value is used.
17925 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17926 a decl-specifier-seq unless it declares a constructor, destructor,
17927 or conversion. It might seem that we could check this condition in
17928 semantic analysis, rather than parsing, but that makes it difficult
17929 to handle something like `f()'. We want to notice that there are
17930 no decl-specifiers, and therefore realize that this is an
17931 expression, not a declaration.)
17933 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17934 the declarator is a direct-declarator of the form "(...)".
17936 MEMBER_P is true iff this declarator is a member-declarator.
17938 FRIEND_P is true iff this declarator is a friend. */
17940 static cp_declarator *
17941 cp_parser_basic_declarator (cp_parser* parser,
17942 cp_parser_declarator_kind dcl_kind,
17943 int* ctor_dtor_or_conv_p,
17944 bool* parenthesized_p,
17945 bool member_p, bool friend_p)
17947 cp_declarator *declarator;
17948 enum tree_code code;
17949 cp_cv_quals cv_quals;
17950 tree class_type;
17951 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17953 /* Assume this is not a constructor, destructor, or type-conversion
17954 operator. */
17955 if (ctor_dtor_or_conv_p)
17956 *ctor_dtor_or_conv_p = 0;
17958 if (cp_parser_allow_gnu_extensions_p (parser))
17959 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17961 /* Check for the ptr-operator production. */
17962 cp_parser_parse_tentatively (parser);
17963 /* Parse the ptr-operator. */
17964 code = cp_parser_ptr_operator (parser,
17965 &class_type,
17966 &cv_quals,
17967 &std_attributes);
17969 /* If that worked, then we have a ptr-operator. */
17970 if (cp_parser_parse_definitely (parser))
17972 /* If a ptr-operator was found, then this declarator was not
17973 parenthesized. */
17974 if (parenthesized_p)
17975 *parenthesized_p = true;
17976 /* The dependent declarator is optional if we are parsing an
17977 abstract-declarator. */
17978 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17979 cp_parser_parse_tentatively (parser);
17981 /* Parse the dependent declarator. */
17982 declarator = cp_parser_basic_declarator (parser, dcl_kind,
17983 /*ctor_dtor_or_conv_p=*/NULL,
17984 /*parenthesized_p=*/NULL,
17985 /*member_p=*/false,
17986 friend_p);
17988 /* If we are parsing an abstract-declarator, we must handle the
17989 case where the dependent declarator is absent. */
17990 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17991 && !cp_parser_parse_definitely (parser))
17992 declarator = NULL;
17994 declarator = cp_parser_make_indirect_declarator
17995 (code, class_type, cv_quals, declarator, std_attributes);
17997 /* Everything else is a direct-declarator. */
17998 else
18000 if (parenthesized_p)
18001 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18002 CPP_OPEN_PAREN);
18003 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18004 ctor_dtor_or_conv_p,
18005 member_p, friend_p);
18008 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18009 declarator->attributes = gnu_attributes;
18011 return declarator;
18015 // A declarator may have a trailing requires clause. Because the
18016 // requires clause is part of the declarator the function parameters
18017 // are visibile in that expression.
18018 static tree
18019 cp_parser_trailing_requires_clause (cp_parser *parser, cp_declarator *decl)
18021 tree reqs = NULL_TREE;
18022 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
18024 cp_lexer_consume_token (parser->lexer);
18025 push_function_parms (decl);
18026 ++cp_unevaluated_operand;
18027 reqs = cp_parser_requires_clause (parser);
18028 --cp_unevaluated_operand;
18029 finish_scope();
18031 return reqs;
18034 /* Parse a declarator. See cp_parser_basic_declarator for details.
18036 In concepts, we allow a requires-clause to be parsed at
18037 after a function declarator. The program is ill-formed for
18038 any other kind of declarator.
18040 declarator:
18041 basic-declarator requires-clause [opt]
18043 basic-declarator:
18044 direct-declarator
18045 ptr-operator-declarator */
18047 static cp_declarator *
18048 cp_parser_declarator (cp_parser* parser,
18049 cp_parser_declarator_kind dcl_kind,
18050 int* ctor_dtor_or_conv_p,
18051 bool* parenthesized_p,
18052 bool member_p, bool friend_p)
18054 cp_declarator *declarator =
18055 cp_parser_basic_declarator (parser, dcl_kind,
18056 ctor_dtor_or_conv_p,
18057 parenthesized_p,
18058 member_p,
18059 friend_p);
18060 if (!declarator)
18061 return declarator;
18063 // Parse the optional trailing requires clause. Note that
18064 // the requires clause is only valid for function declarators.
18065 if (flag_concepts)
18067 if (declarator->kind == cdk_function)
18069 declarator->u.function.requires_clause
18070 = cp_parser_trailing_requires_clause (parser, declarator);
18072 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
18074 error ("requires clause after non-function declaration");
18075 cp_parser_skip_to_end_of_statement (parser);
18078 return declarator;
18083 /* Parse a direct-declarator or direct-abstract-declarator.
18085 direct-declarator:
18086 declarator-id
18087 direct-declarator ( parameter-declaration-clause )
18088 cv-qualifier-seq [opt]
18089 ref-qualifier [opt]
18090 exception-specification [opt]
18091 direct-declarator [ constant-expression [opt] ]
18092 ( declarator )
18094 direct-abstract-declarator:
18095 direct-abstract-declarator [opt]
18096 ( parameter-declaration-clause )
18097 cv-qualifier-seq [opt]
18098 ref-qualifier [opt]
18099 exception-specification [opt]
18100 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18101 ( abstract-declarator )
18103 Returns a representation of the declarator. DCL_KIND is
18104 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18105 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18106 we are parsing a direct-declarator. It is
18107 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18108 of ambiguity we prefer an abstract declarator, as per
18109 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18110 as for cp_parser_declarator. */
18112 static cp_declarator *
18113 cp_parser_direct_declarator (cp_parser* parser,
18114 cp_parser_declarator_kind dcl_kind,
18115 int* ctor_dtor_or_conv_p,
18116 bool member_p, bool friend_p)
18118 cp_token *token;
18119 cp_declarator *declarator = NULL;
18120 tree scope = NULL_TREE;
18121 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18122 bool saved_in_declarator_p = parser->in_declarator_p;
18123 bool first = true;
18124 tree pushed_scope = NULL_TREE;
18126 while (true)
18128 /* Peek at the next token. */
18129 token = cp_lexer_peek_token (parser->lexer);
18130 if (token->type == CPP_OPEN_PAREN)
18132 /* This is either a parameter-declaration-clause, or a
18133 parenthesized declarator. When we know we are parsing a
18134 named declarator, it must be a parenthesized declarator
18135 if FIRST is true. For instance, `(int)' is a
18136 parameter-declaration-clause, with an omitted
18137 direct-abstract-declarator. But `((*))', is a
18138 parenthesized abstract declarator. Finally, when T is a
18139 template parameter `(T)' is a
18140 parameter-declaration-clause, and not a parenthesized
18141 named declarator.
18143 We first try and parse a parameter-declaration-clause,
18144 and then try a nested declarator (if FIRST is true).
18146 It is not an error for it not to be a
18147 parameter-declaration-clause, even when FIRST is
18148 false. Consider,
18150 int i (int);
18151 int i (3);
18153 The first is the declaration of a function while the
18154 second is the definition of a variable, including its
18155 initializer.
18157 Having seen only the parenthesis, we cannot know which of
18158 these two alternatives should be selected. Even more
18159 complex are examples like:
18161 int i (int (a));
18162 int i (int (3));
18164 The former is a function-declaration; the latter is a
18165 variable initialization.
18167 Thus again, we try a parameter-declaration-clause, and if
18168 that fails, we back out and return. */
18170 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18172 tree params;
18173 bool is_declarator = false;
18175 /* In a member-declarator, the only valid interpretation
18176 of a parenthesis is the start of a
18177 parameter-declaration-clause. (It is invalid to
18178 initialize a static data member with a parenthesized
18179 initializer; only the "=" form of initialization is
18180 permitted.) */
18181 if (!member_p)
18182 cp_parser_parse_tentatively (parser);
18184 /* Consume the `('. */
18185 cp_lexer_consume_token (parser->lexer);
18186 if (first)
18188 /* If this is going to be an abstract declarator, we're
18189 in a declarator and we can't have default args. */
18190 parser->default_arg_ok_p = false;
18191 parser->in_declarator_p = true;
18194 begin_scope (sk_function_parms, NULL_TREE);
18196 /* Parse the parameter-declaration-clause. */
18197 params = cp_parser_parameter_declaration_clause (parser);
18199 /* Consume the `)'. */
18200 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18202 /* If all went well, parse the cv-qualifier-seq,
18203 ref-qualifier and the exception-specification. */
18204 if (member_p || cp_parser_parse_definitely (parser))
18206 cp_cv_quals cv_quals;
18207 cp_virt_specifiers virt_specifiers;
18208 cp_ref_qualifier ref_qual;
18209 tree exception_specification;
18210 tree late_return;
18211 tree attrs;
18212 bool memfn = (member_p || (pushed_scope
18213 && CLASS_TYPE_P (pushed_scope)));
18215 is_declarator = true;
18217 if (ctor_dtor_or_conv_p)
18218 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18219 first = false;
18221 /* Parse the cv-qualifier-seq. */
18222 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18223 /* Parse the ref-qualifier. */
18224 ref_qual = cp_parser_ref_qualifier_opt (parser);
18225 /* And the exception-specification. */
18226 exception_specification
18227 = cp_parser_exception_specification_opt (parser);
18229 attrs = cp_parser_std_attribute_spec_seq (parser);
18231 /* In here, we handle cases where attribute is used after
18232 the function declaration. For example:
18233 void func (int x) __attribute__((vector(..))); */
18234 if (flag_cilkplus
18235 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18237 cp_parser_parse_tentatively (parser);
18238 tree attr = cp_parser_gnu_attributes_opt (parser);
18239 if (cp_lexer_next_token_is_not (parser->lexer,
18240 CPP_SEMICOLON)
18241 && cp_lexer_next_token_is_not (parser->lexer,
18242 CPP_OPEN_BRACE))
18243 cp_parser_abort_tentative_parse (parser);
18244 else if (!cp_parser_parse_definitely (parser))
18246 else
18247 attrs = chainon (attr, attrs);
18249 late_return = (cp_parser_late_return_type_opt
18250 (parser, declarator,
18251 memfn ? cv_quals : -1));
18254 /* Parse the virt-specifier-seq. */
18255 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18257 /* Create the function-declarator. */
18258 declarator = make_call_declarator (declarator,
18259 params,
18260 cv_quals,
18261 virt_specifiers,
18262 ref_qual,
18263 exception_specification,
18264 late_return,
18265 /*requires_clause=*/NULL_TREE);
18266 declarator->std_attributes = attrs;
18267 /* Any subsequent parameter lists are to do with
18268 return type, so are not those of the declared
18269 function. */
18270 parser->default_arg_ok_p = false;
18273 /* Remove the function parms from scope. */
18274 pop_bindings_and_leave_scope ();
18276 if (is_declarator)
18277 /* Repeat the main loop. */
18278 continue;
18281 /* If this is the first, we can try a parenthesized
18282 declarator. */
18283 if (first)
18285 bool saved_in_type_id_in_expr_p;
18287 parser->default_arg_ok_p = saved_default_arg_ok_p;
18288 parser->in_declarator_p = saved_in_declarator_p;
18290 /* Consume the `('. */
18291 cp_lexer_consume_token (parser->lexer);
18292 /* Parse the nested declarator. */
18293 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18294 parser->in_type_id_in_expr_p = true;
18295 declarator
18296 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
18297 /*parenthesized_p=*/NULL,
18298 member_p, friend_p);
18299 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18300 first = false;
18301 /* Expect a `)'. */
18302 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
18303 declarator = cp_error_declarator;
18304 if (declarator == cp_error_declarator)
18305 break;
18307 goto handle_declarator;
18309 /* Otherwise, we must be done. */
18310 else
18311 break;
18313 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18314 && token->type == CPP_OPEN_SQUARE
18315 && !cp_next_tokens_can_be_attribute_p (parser))
18317 /* Parse an array-declarator. */
18318 tree bounds, attrs;
18320 if (ctor_dtor_or_conv_p)
18321 *ctor_dtor_or_conv_p = 0;
18323 first = false;
18324 parser->default_arg_ok_p = false;
18325 parser->in_declarator_p = true;
18326 /* Consume the `['. */
18327 cp_lexer_consume_token (parser->lexer);
18328 /* Peek at the next token. */
18329 token = cp_lexer_peek_token (parser->lexer);
18330 /* If the next token is `]', then there is no
18331 constant-expression. */
18332 if (token->type != CPP_CLOSE_SQUARE)
18334 bool non_constant_p;
18335 bounds
18336 = cp_parser_constant_expression (parser,
18337 /*allow_non_constant=*/true,
18338 &non_constant_p);
18339 if (!non_constant_p)
18340 /* OK */;
18341 else if (error_operand_p (bounds))
18342 /* Already gave an error. */;
18343 else if (!parser->in_function_body
18344 || current_binding_level->kind == sk_function_parms)
18346 /* Normally, the array bound must be an integral constant
18347 expression. However, as an extension, we allow VLAs
18348 in function scopes as long as they aren't part of a
18349 parameter declaration. */
18350 cp_parser_error (parser,
18351 "array bound is not an integer constant");
18352 bounds = error_mark_node;
18354 else if (processing_template_decl
18355 && !type_dependent_expression_p (bounds))
18357 /* Remember this wasn't a constant-expression. */
18358 bounds = build_nop (TREE_TYPE (bounds), bounds);
18359 TREE_SIDE_EFFECTS (bounds) = 1;
18362 else
18363 bounds = NULL_TREE;
18364 /* Look for the closing `]'. */
18365 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
18367 declarator = cp_error_declarator;
18368 break;
18371 attrs = cp_parser_std_attribute_spec_seq (parser);
18372 declarator = make_array_declarator (declarator, bounds);
18373 declarator->std_attributes = attrs;
18375 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
18378 tree qualifying_scope;
18379 tree unqualified_name;
18380 tree attrs;
18381 special_function_kind sfk;
18382 bool abstract_ok;
18383 bool pack_expansion_p = false;
18384 cp_token *declarator_id_start_token;
18386 /* Parse a declarator-id */
18387 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
18388 if (abstract_ok)
18390 cp_parser_parse_tentatively (parser);
18392 /* If we see an ellipsis, we should be looking at a
18393 parameter pack. */
18394 if (token->type == CPP_ELLIPSIS)
18396 /* Consume the `...' */
18397 cp_lexer_consume_token (parser->lexer);
18399 pack_expansion_p = true;
18403 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
18404 unqualified_name
18405 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
18406 qualifying_scope = parser->scope;
18407 if (abstract_ok)
18409 bool okay = false;
18411 if (!unqualified_name && pack_expansion_p)
18413 /* Check whether an error occurred. */
18414 okay = !cp_parser_error_occurred (parser);
18416 /* We already consumed the ellipsis to mark a
18417 parameter pack, but we have no way to report it,
18418 so abort the tentative parse. We will be exiting
18419 immediately anyway. */
18420 cp_parser_abort_tentative_parse (parser);
18422 else
18423 okay = cp_parser_parse_definitely (parser);
18425 if (!okay)
18426 unqualified_name = error_mark_node;
18427 else if (unqualified_name
18428 && (qualifying_scope
18429 || (!identifier_p (unqualified_name))))
18431 cp_parser_error (parser, "expected unqualified-id");
18432 unqualified_name = error_mark_node;
18436 if (!unqualified_name)
18437 return NULL;
18438 if (unqualified_name == error_mark_node)
18440 declarator = cp_error_declarator;
18441 pack_expansion_p = false;
18442 declarator->parameter_pack_p = false;
18443 break;
18446 attrs = cp_parser_std_attribute_spec_seq (parser);
18448 if (qualifying_scope && at_namespace_scope_p ()
18449 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
18451 /* In the declaration of a member of a template class
18452 outside of the class itself, the SCOPE will sometimes
18453 be a TYPENAME_TYPE. For example, given:
18455 template <typename T>
18456 int S<T>::R::i = 3;
18458 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
18459 this context, we must resolve S<T>::R to an ordinary
18460 type, rather than a typename type.
18462 The reason we normally avoid resolving TYPENAME_TYPEs
18463 is that a specialization of `S' might render
18464 `S<T>::R' not a type. However, if `S' is
18465 specialized, then this `i' will not be used, so there
18466 is no harm in resolving the types here. */
18467 tree type;
18469 /* Resolve the TYPENAME_TYPE. */
18470 type = resolve_typename_type (qualifying_scope,
18471 /*only_current_p=*/false);
18472 /* If that failed, the declarator is invalid. */
18473 if (TREE_CODE (type) == TYPENAME_TYPE)
18475 if (typedef_variant_p (type))
18476 error_at (declarator_id_start_token->location,
18477 "cannot define member of dependent typedef "
18478 "%qT", type);
18479 else
18480 error_at (declarator_id_start_token->location,
18481 "%<%T::%E%> is not a type",
18482 TYPE_CONTEXT (qualifying_scope),
18483 TYPE_IDENTIFIER (qualifying_scope));
18485 qualifying_scope = type;
18488 sfk = sfk_none;
18490 if (unqualified_name)
18492 tree class_type;
18494 if (qualifying_scope
18495 && CLASS_TYPE_P (qualifying_scope))
18496 class_type = qualifying_scope;
18497 else
18498 class_type = current_class_type;
18500 if (TREE_CODE (unqualified_name) == TYPE_DECL)
18502 tree name_type = TREE_TYPE (unqualified_name);
18503 if (class_type && same_type_p (name_type, class_type))
18505 if (qualifying_scope
18506 && CLASSTYPE_USE_TEMPLATE (name_type))
18508 error_at (declarator_id_start_token->location,
18509 "invalid use of constructor as a template");
18510 inform (declarator_id_start_token->location,
18511 "use %<%T::%D%> instead of %<%T::%D%> to "
18512 "name the constructor in a qualified name",
18513 class_type,
18514 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
18515 class_type, name_type);
18516 declarator = cp_error_declarator;
18517 break;
18519 else
18520 unqualified_name = constructor_name (class_type);
18522 else
18524 /* We do not attempt to print the declarator
18525 here because we do not have enough
18526 information about its original syntactic
18527 form. */
18528 cp_parser_error (parser, "invalid declarator");
18529 declarator = cp_error_declarator;
18530 break;
18534 if (class_type)
18536 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
18537 sfk = sfk_destructor;
18538 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
18539 sfk = sfk_conversion;
18540 else if (/* There's no way to declare a constructor
18541 for an anonymous type, even if the type
18542 got a name for linkage purposes. */
18543 !TYPE_WAS_ANONYMOUS (class_type)
18544 /* Handle correctly (c++/19200):
18546 struct S {
18547 struct T{};
18548 friend void S(T);
18551 and also:
18553 namespace N {
18554 void S();
18557 struct S {
18558 friend void N::S();
18559 }; */
18560 && !(friend_p
18561 && class_type != qualifying_scope)
18562 && constructor_name_p (unqualified_name,
18563 class_type))
18565 unqualified_name = constructor_name (class_type);
18566 sfk = sfk_constructor;
18568 else if (is_overloaded_fn (unqualified_name)
18569 && DECL_CONSTRUCTOR_P (get_first_fn
18570 (unqualified_name)))
18571 sfk = sfk_constructor;
18573 if (ctor_dtor_or_conv_p && sfk != sfk_none)
18574 *ctor_dtor_or_conv_p = -1;
18577 declarator = make_id_declarator (qualifying_scope,
18578 unqualified_name,
18579 sfk);
18580 declarator->std_attributes = attrs;
18581 declarator->id_loc = token->location;
18582 declarator->parameter_pack_p = pack_expansion_p;
18584 if (pack_expansion_p)
18585 maybe_warn_variadic_templates ();
18588 handle_declarator:;
18589 scope = get_scope_of_declarator (declarator);
18590 if (scope)
18592 /* Any names that appear after the declarator-id for a
18593 member are looked up in the containing scope. */
18594 if (at_function_scope_p ())
18596 /* But declarations with qualified-ids can't appear in a
18597 function. */
18598 cp_parser_error (parser, "qualified-id in declaration");
18599 declarator = cp_error_declarator;
18600 break;
18602 pushed_scope = push_scope (scope);
18604 parser->in_declarator_p = true;
18605 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18606 || (declarator && declarator->kind == cdk_id))
18607 /* Default args are only allowed on function
18608 declarations. */
18609 parser->default_arg_ok_p = saved_default_arg_ok_p;
18610 else
18611 parser->default_arg_ok_p = false;
18613 first = false;
18615 /* We're done. */
18616 else
18617 break;
18620 /* For an abstract declarator, we might wind up with nothing at this
18621 point. That's an error; the declarator is not optional. */
18622 if (!declarator)
18623 cp_parser_error (parser, "expected declarator");
18625 /* If we entered a scope, we must exit it now. */
18626 if (pushed_scope)
18627 pop_scope (pushed_scope);
18629 parser->default_arg_ok_p = saved_default_arg_ok_p;
18630 parser->in_declarator_p = saved_in_declarator_p;
18632 return declarator;
18635 /* Parse a ptr-operator.
18637 ptr-operator:
18638 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18639 * cv-qualifier-seq [opt]
18641 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18642 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18644 GNU Extension:
18646 ptr-operator:
18647 & cv-qualifier-seq [opt]
18649 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18650 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18651 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18652 filled in with the TYPE containing the member. *CV_QUALS is
18653 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18654 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18655 Note that the tree codes returned by this function have nothing
18656 to do with the types of trees that will be eventually be created
18657 to represent the pointer or reference type being parsed. They are
18658 just constants with suggestive names. */
18659 static enum tree_code
18660 cp_parser_ptr_operator (cp_parser* parser,
18661 tree* type,
18662 cp_cv_quals *cv_quals,
18663 tree *attributes)
18665 enum tree_code code = ERROR_MARK;
18666 cp_token *token;
18667 tree attrs = NULL_TREE;
18669 /* Assume that it's not a pointer-to-member. */
18670 *type = NULL_TREE;
18671 /* And that there are no cv-qualifiers. */
18672 *cv_quals = TYPE_UNQUALIFIED;
18674 /* Peek at the next token. */
18675 token = cp_lexer_peek_token (parser->lexer);
18677 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18678 if (token->type == CPP_MULT)
18679 code = INDIRECT_REF;
18680 else if (token->type == CPP_AND)
18681 code = ADDR_EXPR;
18682 else if ((cxx_dialect != cxx98) &&
18683 token->type == CPP_AND_AND) /* C++0x only */
18684 code = NON_LVALUE_EXPR;
18686 if (code != ERROR_MARK)
18688 /* Consume the `*', `&' or `&&'. */
18689 cp_lexer_consume_token (parser->lexer);
18691 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18692 `&', if we are allowing GNU extensions. (The only qualifier
18693 that can legally appear after `&' is `restrict', but that is
18694 enforced during semantic analysis. */
18695 if (code == INDIRECT_REF
18696 || cp_parser_allow_gnu_extensions_p (parser))
18697 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18699 attrs = cp_parser_std_attribute_spec_seq (parser);
18700 if (attributes != NULL)
18701 *attributes = attrs;
18703 else
18705 /* Try the pointer-to-member case. */
18706 cp_parser_parse_tentatively (parser);
18707 /* Look for the optional `::' operator. */
18708 cp_parser_global_scope_opt (parser,
18709 /*current_scope_valid_p=*/false);
18710 /* Look for the nested-name specifier. */
18711 token = cp_lexer_peek_token (parser->lexer);
18712 cp_parser_nested_name_specifier (parser,
18713 /*typename_keyword_p=*/false,
18714 /*check_dependency_p=*/true,
18715 /*type_p=*/false,
18716 /*is_declaration=*/false);
18717 /* If we found it, and the next token is a `*', then we are
18718 indeed looking at a pointer-to-member operator. */
18719 if (!cp_parser_error_occurred (parser)
18720 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18722 /* Indicate that the `*' operator was used. */
18723 code = INDIRECT_REF;
18725 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18726 error_at (token->location, "%qD is a namespace", parser->scope);
18727 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18728 error_at (token->location, "cannot form pointer to member of "
18729 "non-class %q#T", parser->scope);
18730 else
18732 /* The type of which the member is a member is given by the
18733 current SCOPE. */
18734 *type = parser->scope;
18735 /* The next name will not be qualified. */
18736 parser->scope = NULL_TREE;
18737 parser->qualifying_scope = NULL_TREE;
18738 parser->object_scope = NULL_TREE;
18739 /* Look for optional c++11 attributes. */
18740 attrs = cp_parser_std_attribute_spec_seq (parser);
18741 if (attributes != NULL)
18742 *attributes = attrs;
18743 /* Look for the optional cv-qualifier-seq. */
18744 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18747 /* If that didn't work we don't have a ptr-operator. */
18748 if (!cp_parser_parse_definitely (parser))
18749 cp_parser_error (parser, "expected ptr-operator");
18752 return code;
18755 /* Parse an (optional) cv-qualifier-seq.
18757 cv-qualifier-seq:
18758 cv-qualifier cv-qualifier-seq [opt]
18760 cv-qualifier:
18761 const
18762 volatile
18764 GNU Extension:
18766 cv-qualifier:
18767 __restrict__
18769 Returns a bitmask representing the cv-qualifiers. */
18771 static cp_cv_quals
18772 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18774 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18776 while (true)
18778 cp_token *token;
18779 cp_cv_quals cv_qualifier;
18781 /* Peek at the next token. */
18782 token = cp_lexer_peek_token (parser->lexer);
18783 /* See if it's a cv-qualifier. */
18784 switch (token->keyword)
18786 case RID_CONST:
18787 cv_qualifier = TYPE_QUAL_CONST;
18788 break;
18790 case RID_VOLATILE:
18791 cv_qualifier = TYPE_QUAL_VOLATILE;
18792 break;
18794 case RID_RESTRICT:
18795 cv_qualifier = TYPE_QUAL_RESTRICT;
18796 break;
18798 default:
18799 cv_qualifier = TYPE_UNQUALIFIED;
18800 break;
18803 if (!cv_qualifier)
18804 break;
18806 if (cv_quals & cv_qualifier)
18808 error_at (token->location, "duplicate cv-qualifier");
18809 cp_lexer_purge_token (parser->lexer);
18811 else
18813 cp_lexer_consume_token (parser->lexer);
18814 cv_quals |= cv_qualifier;
18818 return cv_quals;
18821 /* Parse an (optional) ref-qualifier
18823 ref-qualifier:
18827 Returns cp_ref_qualifier representing ref-qualifier. */
18829 static cp_ref_qualifier
18830 cp_parser_ref_qualifier_opt (cp_parser* parser)
18832 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18834 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18835 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18836 return ref_qual;
18838 while (true)
18840 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18841 cp_token *token = cp_lexer_peek_token (parser->lexer);
18843 switch (token->type)
18845 case CPP_AND:
18846 curr_ref_qual = REF_QUAL_LVALUE;
18847 break;
18849 case CPP_AND_AND:
18850 curr_ref_qual = REF_QUAL_RVALUE;
18851 break;
18853 default:
18854 curr_ref_qual = REF_QUAL_NONE;
18855 break;
18858 if (!curr_ref_qual)
18859 break;
18860 else if (ref_qual)
18862 error_at (token->location, "multiple ref-qualifiers");
18863 cp_lexer_purge_token (parser->lexer);
18865 else
18867 ref_qual = curr_ref_qual;
18868 cp_lexer_consume_token (parser->lexer);
18872 return ref_qual;
18875 /* Parse an (optional) virt-specifier-seq.
18877 virt-specifier-seq:
18878 virt-specifier virt-specifier-seq [opt]
18880 virt-specifier:
18881 override
18882 final
18884 Returns a bitmask representing the virt-specifiers. */
18886 static cp_virt_specifiers
18887 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18889 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18891 while (true)
18893 cp_token *token;
18894 cp_virt_specifiers virt_specifier;
18896 /* Peek at the next token. */
18897 token = cp_lexer_peek_token (parser->lexer);
18898 /* See if it's a virt-specifier-qualifier. */
18899 if (token->type != CPP_NAME)
18900 break;
18901 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18903 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18904 virt_specifier = VIRT_SPEC_OVERRIDE;
18906 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18908 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18909 virt_specifier = VIRT_SPEC_FINAL;
18911 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18913 virt_specifier = VIRT_SPEC_FINAL;
18915 else
18916 break;
18918 if (virt_specifiers & virt_specifier)
18920 error_at (token->location, "duplicate virt-specifier");
18921 cp_lexer_purge_token (parser->lexer);
18923 else
18925 cp_lexer_consume_token (parser->lexer);
18926 virt_specifiers |= virt_specifier;
18929 return virt_specifiers;
18932 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18933 is in scope even though it isn't real. */
18935 void
18936 inject_this_parameter (tree ctype, cp_cv_quals quals)
18938 tree this_parm;
18940 if (current_class_ptr)
18942 /* We don't clear this between NSDMIs. Is it already what we want? */
18943 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18944 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18945 && cp_type_quals (type) == quals)
18946 return;
18949 this_parm = build_this_parm (ctype, quals);
18950 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18951 current_class_ptr = NULL_TREE;
18952 current_class_ref
18953 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18954 current_class_ptr = this_parm;
18957 /* Return true iff our current scope is a non-static data member
18958 initializer. */
18960 bool
18961 parsing_nsdmi (void)
18963 /* We recognize NSDMI context by the context-less 'this' pointer set up
18964 by the function above. */
18965 if (current_class_ptr
18966 && TREE_CODE (current_class_ptr) == PARM_DECL
18967 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18968 return true;
18969 return false;
18972 /* Parse a late-specified return type, if any. This is not a separate
18973 non-terminal, but part of a function declarator, which looks like
18975 -> trailing-type-specifier-seq abstract-declarator(opt)
18977 Returns the type indicated by the type-id.
18979 In addition to this this parses any queued up omp declare simd
18980 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18982 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18983 function. */
18985 static tree
18986 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18987 cp_cv_quals quals)
18989 cp_token *token;
18990 tree type = NULL_TREE;
18991 bool declare_simd_p = (parser->omp_declare_simd
18992 && declarator
18993 && declarator->kind == cdk_id);
18995 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18996 && declarator && declarator->kind == cdk_id);
18998 /* Peek at the next token. */
18999 token = cp_lexer_peek_token (parser->lexer);
19000 /* A late-specified return type is indicated by an initial '->'. */
19001 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
19002 return NULL_TREE;
19004 tree save_ccp = current_class_ptr;
19005 tree save_ccr = current_class_ref;
19006 if (quals >= 0)
19008 /* DR 1207: 'this' is in scope in the trailing return type. */
19009 inject_this_parameter (current_class_type, quals);
19012 if (token->type == CPP_DEREF)
19014 /* Consume the ->. */
19015 cp_lexer_consume_token (parser->lexer);
19016 type = cp_parser_trailing_type_id (parser);
19019 if (cilk_simd_fn_vector_p)
19020 declarator->std_attributes
19021 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19022 declarator->std_attributes);
19023 if (declare_simd_p)
19024 declarator->std_attributes
19025 = cp_parser_late_parsing_omp_declare_simd (parser,
19026 declarator->std_attributes);
19028 if (quals >= 0)
19030 current_class_ptr = save_ccp;
19031 current_class_ref = save_ccr;
19034 return type;
19037 /* Parse a declarator-id.
19039 declarator-id:
19040 id-expression
19041 :: [opt] nested-name-specifier [opt] type-name
19043 In the `id-expression' case, the value returned is as for
19044 cp_parser_id_expression if the id-expression was an unqualified-id.
19045 If the id-expression was a qualified-id, then a SCOPE_REF is
19046 returned. The first operand is the scope (either a NAMESPACE_DECL
19047 or TREE_TYPE), but the second is still just a representation of an
19048 unqualified-id. */
19050 static tree
19051 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19053 tree id;
19054 /* The expression must be an id-expression. Assume that qualified
19055 names are the names of types so that:
19057 template <class T>
19058 int S<T>::R::i = 3;
19060 will work; we must treat `S<T>::R' as the name of a type.
19061 Similarly, assume that qualified names are templates, where
19062 required, so that:
19064 template <class T>
19065 int S<T>::R<T>::i = 3;
19067 will work, too. */
19068 id = cp_parser_id_expression (parser,
19069 /*template_keyword_p=*/false,
19070 /*check_dependency_p=*/false,
19071 /*template_p=*/NULL,
19072 /*declarator_p=*/true,
19073 optional_p);
19074 if (id && BASELINK_P (id))
19075 id = BASELINK_FUNCTIONS (id);
19076 return id;
19079 /* Parse a type-id.
19081 type-id:
19082 type-specifier-seq abstract-declarator [opt]
19084 Returns the TYPE specified. */
19086 static tree
19087 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19088 bool is_trailing_return)
19090 cp_decl_specifier_seq type_specifier_seq;
19091 cp_declarator *abstract_declarator;
19093 /* Parse the type-specifier-seq. */
19094 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19095 is_trailing_return,
19096 &type_specifier_seq);
19097 if (type_specifier_seq.type == error_mark_node)
19098 return error_mark_node;
19100 /* There might or might not be an abstract declarator. */
19101 cp_parser_parse_tentatively (parser);
19102 /* Look for the declarator. */
19103 abstract_declarator
19104 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19105 /*parenthesized_p=*/NULL,
19106 /*member_p=*/false,
19107 /*friend_p=*/false);
19108 /* Check to see if there really was a declarator. */
19109 if (!cp_parser_parse_definitely (parser))
19110 abstract_declarator = NULL;
19112 if (type_specifier_seq.type
19113 /* None of the valid uses of 'auto' in C++14 involve the type-id
19114 nonterminal, but it is valid in a trailing-return-type. */
19115 && !(cxx_dialect >= cxx14 && is_trailing_return)
19116 && type_uses_auto (type_specifier_seq.type))
19118 /* A type-id with type 'auto' is only ok if the abstract declarator
19119 is a function declarator with a late-specified return type.
19121 A type-id with 'auto' is also valid in a trailing-return-type
19122 in a compound-requirment. */
19123 if (abstract_declarator
19124 && abstract_declarator->kind == cdk_function
19125 && abstract_declarator->u.function.late_return_type)
19126 /* OK */;
19127 else if (parser->in_result_type_constraint_p)
19128 /* OK */;
19129 else
19131 error ("invalid use of %<auto%>");
19132 return error_mark_node;
19136 return groktypename (&type_specifier_seq, abstract_declarator,
19137 is_template_arg);
19140 static tree cp_parser_type_id (cp_parser *parser)
19142 return cp_parser_type_id_1 (parser, false, false);
19145 static tree cp_parser_template_type_arg (cp_parser *parser)
19147 tree r;
19148 const char *saved_message = parser->type_definition_forbidden_message;
19149 parser->type_definition_forbidden_message
19150 = G_("types may not be defined in template arguments");
19151 r = cp_parser_type_id_1 (parser, true, false);
19152 parser->type_definition_forbidden_message = saved_message;
19153 if (cxx_dialect >= cxx14 && type_uses_auto (r))
19155 error ("invalid use of %<auto%> in template argument");
19156 r = error_mark_node;
19158 return r;
19161 static tree cp_parser_trailing_type_id (cp_parser *parser)
19163 return cp_parser_type_id_1 (parser, false, true);
19166 /* Parse a type-specifier-seq.
19168 type-specifier-seq:
19169 type-specifier type-specifier-seq [opt]
19171 GNU extension:
19173 type-specifier-seq:
19174 attributes type-specifier-seq [opt]
19176 If IS_DECLARATION is true, we are at the start of a "condition" or
19177 exception-declaration, so we might be followed by a declarator-id.
19179 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19180 i.e. we've just seen "->".
19182 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19184 static void
19185 cp_parser_type_specifier_seq (cp_parser* parser,
19186 bool is_declaration,
19187 bool is_trailing_return,
19188 cp_decl_specifier_seq *type_specifier_seq)
19190 bool seen_type_specifier = false;
19191 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19192 cp_token *start_token = NULL;
19194 /* Clear the TYPE_SPECIFIER_SEQ. */
19195 clear_decl_specs (type_specifier_seq);
19197 /* In the context of a trailing return type, enum E { } is an
19198 elaborated-type-specifier followed by a function-body, not an
19199 enum-specifier. */
19200 if (is_trailing_return)
19201 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19203 /* Parse the type-specifiers and attributes. */
19204 while (true)
19206 tree type_specifier;
19207 bool is_cv_qualifier;
19209 /* Check for attributes first. */
19210 if (cp_next_tokens_can_be_attribute_p (parser))
19212 type_specifier_seq->attributes =
19213 chainon (type_specifier_seq->attributes,
19214 cp_parser_attributes_opt (parser));
19215 continue;
19218 /* record the token of the beginning of the type specifier seq,
19219 for error reporting purposes*/
19220 if (!start_token)
19221 start_token = cp_lexer_peek_token (parser->lexer);
19223 /* Look for the type-specifier. */
19224 type_specifier = cp_parser_type_specifier (parser,
19225 flags,
19226 type_specifier_seq,
19227 /*is_declaration=*/false,
19228 NULL,
19229 &is_cv_qualifier);
19230 if (!type_specifier)
19232 /* If the first type-specifier could not be found, this is not a
19233 type-specifier-seq at all. */
19234 if (!seen_type_specifier)
19236 /* Set in_declarator_p to avoid skipping to the semicolon. */
19237 int in_decl = parser->in_declarator_p;
19238 parser->in_declarator_p = true;
19240 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
19241 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
19242 cp_parser_error (parser, "expected type-specifier");
19244 parser->in_declarator_p = in_decl;
19246 type_specifier_seq->type = error_mark_node;
19247 return;
19249 /* If subsequent type-specifiers could not be found, the
19250 type-specifier-seq is complete. */
19251 break;
19254 seen_type_specifier = true;
19255 /* The standard says that a condition can be:
19257 type-specifier-seq declarator = assignment-expression
19259 However, given:
19261 struct S {};
19262 if (int S = ...)
19264 we should treat the "S" as a declarator, not as a
19265 type-specifier. The standard doesn't say that explicitly for
19266 type-specifier-seq, but it does say that for
19267 decl-specifier-seq in an ordinary declaration. Perhaps it
19268 would be clearer just to allow a decl-specifier-seq here, and
19269 then add a semantic restriction that if any decl-specifiers
19270 that are not type-specifiers appear, the program is invalid. */
19271 if (is_declaration && !is_cv_qualifier)
19272 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
19276 /* Return whether the function currently being declared has an associated
19277 template parameter list. */
19279 static bool
19280 function_being_declared_is_template_p (cp_parser* parser)
19282 if (!current_template_parms || processing_template_parmlist)
19283 return false;
19285 if (parser->implicit_template_scope)
19286 return true;
19288 if (at_class_scope_p ()
19289 && TYPE_BEING_DEFINED (current_class_type))
19290 return parser->num_template_parameter_lists != 0;
19292 return ((int) parser->num_template_parameter_lists > template_class_depth
19293 (current_class_type));
19296 /* Parse a parameter-declaration-clause.
19298 parameter-declaration-clause:
19299 parameter-declaration-list [opt] ... [opt]
19300 parameter-declaration-list , ...
19302 Returns a representation for the parameter declarations. A return
19303 value of NULL indicates a parameter-declaration-clause consisting
19304 only of an ellipsis. */
19306 static tree
19307 cp_parser_parameter_declaration_clause (cp_parser* parser)
19309 tree parameters;
19310 cp_token *token;
19311 bool ellipsis_p;
19312 bool is_error;
19314 struct cleanup {
19315 cp_parser* parser;
19316 int auto_is_implicit_function_template_parm_p;
19317 ~cleanup() {
19318 parser->auto_is_implicit_function_template_parm_p
19319 = auto_is_implicit_function_template_parm_p;
19321 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
19323 (void) cleanup;
19325 if (!processing_specialization
19326 && !processing_template_parmlist
19327 && !processing_explicit_instantiation)
19328 if (!current_function_decl
19329 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
19330 parser->auto_is_implicit_function_template_parm_p = true;
19332 /* Peek at the next token. */
19333 token = cp_lexer_peek_token (parser->lexer);
19334 /* Check for trivial parameter-declaration-clauses. */
19335 if (token->type == CPP_ELLIPSIS)
19337 /* Consume the `...' token. */
19338 cp_lexer_consume_token (parser->lexer);
19339 return NULL_TREE;
19341 else if (token->type == CPP_CLOSE_PAREN)
19342 /* There are no parameters. */
19344 #ifndef NO_IMPLICIT_EXTERN_C
19345 if (in_system_header_at (input_location)
19346 && current_class_type == NULL
19347 && current_lang_name == lang_name_c)
19348 return NULL_TREE;
19349 else
19350 #endif
19351 return void_list_node;
19353 /* Check for `(void)', too, which is a special case. */
19354 else if (token->keyword == RID_VOID
19355 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19356 == CPP_CLOSE_PAREN))
19358 /* Consume the `void' token. */
19359 cp_lexer_consume_token (parser->lexer);
19360 /* There are no parameters. */
19361 return void_list_node;
19364 /* Parse the parameter-declaration-list. */
19365 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
19366 /* If a parse error occurred while parsing the
19367 parameter-declaration-list, then the entire
19368 parameter-declaration-clause is erroneous. */
19369 if (is_error)
19370 return NULL;
19372 /* Peek at the next token. */
19373 token = cp_lexer_peek_token (parser->lexer);
19374 /* If it's a `,', the clause should terminate with an ellipsis. */
19375 if (token->type == CPP_COMMA)
19377 /* Consume the `,'. */
19378 cp_lexer_consume_token (parser->lexer);
19379 /* Expect an ellipsis. */
19380 ellipsis_p
19381 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
19383 /* It might also be `...' if the optional trailing `,' was
19384 omitted. */
19385 else if (token->type == CPP_ELLIPSIS)
19387 /* Consume the `...' token. */
19388 cp_lexer_consume_token (parser->lexer);
19389 /* And remember that we saw it. */
19390 ellipsis_p = true;
19392 else
19393 ellipsis_p = false;
19395 /* Finish the parameter list. */
19396 if (!ellipsis_p)
19397 parameters = chainon (parameters, void_list_node);
19399 return parameters;
19402 /* Parse a parameter-declaration-list.
19404 parameter-declaration-list:
19405 parameter-declaration
19406 parameter-declaration-list , parameter-declaration
19408 Returns a representation of the parameter-declaration-list, as for
19409 cp_parser_parameter_declaration_clause. However, the
19410 `void_list_node' is never appended to the list. Upon return,
19411 *IS_ERROR will be true iff an error occurred. */
19413 static tree
19414 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
19416 tree parameters = NULL_TREE;
19417 tree *tail = &parameters;
19418 bool saved_in_unbraced_linkage_specification_p;
19419 int index = 0;
19421 /* Assume all will go well. */
19422 *is_error = false;
19423 /* The special considerations that apply to a function within an
19424 unbraced linkage specifications do not apply to the parameters
19425 to the function. */
19426 saved_in_unbraced_linkage_specification_p
19427 = parser->in_unbraced_linkage_specification_p;
19428 parser->in_unbraced_linkage_specification_p = false;
19430 /* Look for more parameters. */
19431 while (true)
19433 cp_parameter_declarator *parameter;
19434 tree decl = error_mark_node;
19435 bool parenthesized_p = false;
19436 int template_parm_idx = (function_being_declared_is_template_p (parser)?
19437 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
19438 (current_template_parms)) : 0);
19440 /* Parse the parameter. */
19441 parameter
19442 = cp_parser_parameter_declaration (parser,
19443 /*template_parm_p=*/false,
19444 &parenthesized_p);
19446 /* We don't know yet if the enclosing context is deprecated, so wait
19447 and warn in grokparms if appropriate. */
19448 deprecated_state = DEPRECATED_SUPPRESS;
19450 if (parameter)
19452 /* If a function parameter pack was specified and an implicit template
19453 parameter was introduced during cp_parser_parameter_declaration,
19454 change any implicit parameters introduced into packs. */
19455 if (parser->implicit_template_parms
19456 && parameter->declarator
19457 && parameter->declarator->parameter_pack_p)
19459 int latest_template_parm_idx = TREE_VEC_LENGTH
19460 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
19462 if (latest_template_parm_idx != template_parm_idx)
19463 parameter->decl_specifiers.type = convert_generic_types_to_packs
19464 (parameter->decl_specifiers.type,
19465 template_parm_idx, latest_template_parm_idx);
19468 decl = grokdeclarator (parameter->declarator,
19469 &parameter->decl_specifiers,
19470 PARM,
19471 parameter->default_argument != NULL_TREE,
19472 &parameter->decl_specifiers.attributes);
19475 deprecated_state = DEPRECATED_NORMAL;
19477 /* If a parse error occurred parsing the parameter declaration,
19478 then the entire parameter-declaration-list is erroneous. */
19479 if (decl == error_mark_node)
19481 *is_error = true;
19482 parameters = error_mark_node;
19483 break;
19486 if (parameter->decl_specifiers.attributes)
19487 cplus_decl_attributes (&decl,
19488 parameter->decl_specifiers.attributes,
19490 if (DECL_NAME (decl))
19491 decl = pushdecl (decl);
19493 if (decl != error_mark_node)
19495 retrofit_lang_decl (decl);
19496 DECL_PARM_INDEX (decl) = ++index;
19497 DECL_PARM_LEVEL (decl) = function_parm_depth ();
19500 /* Add the new parameter to the list. */
19501 *tail = build_tree_list (parameter->default_argument, decl);
19502 tail = &TREE_CHAIN (*tail);
19504 /* Peek at the next token. */
19505 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
19506 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
19507 /* These are for Objective-C++ */
19508 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19509 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19510 /* The parameter-declaration-list is complete. */
19511 break;
19512 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19514 cp_token *token;
19516 /* Peek at the next token. */
19517 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19518 /* If it's an ellipsis, then the list is complete. */
19519 if (token->type == CPP_ELLIPSIS)
19520 break;
19521 /* Otherwise, there must be more parameters. Consume the
19522 `,'. */
19523 cp_lexer_consume_token (parser->lexer);
19524 /* When parsing something like:
19526 int i(float f, double d)
19528 we can tell after seeing the declaration for "f" that we
19529 are not looking at an initialization of a variable "i",
19530 but rather at the declaration of a function "i".
19532 Due to the fact that the parsing of template arguments
19533 (as specified to a template-id) requires backtracking we
19534 cannot use this technique when inside a template argument
19535 list. */
19536 if (!parser->in_template_argument_list_p
19537 && !parser->in_type_id_in_expr_p
19538 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19539 /* However, a parameter-declaration of the form
19540 "float(f)" (which is a valid declaration of a
19541 parameter "f") can also be interpreted as an
19542 expression (the conversion of "f" to "float"). */
19543 && !parenthesized_p)
19544 cp_parser_commit_to_tentative_parse (parser);
19546 else
19548 cp_parser_error (parser, "expected %<,%> or %<...%>");
19549 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19550 cp_parser_skip_to_closing_parenthesis (parser,
19551 /*recovering=*/true,
19552 /*or_comma=*/false,
19553 /*consume_paren=*/false);
19554 break;
19558 parser->in_unbraced_linkage_specification_p
19559 = saved_in_unbraced_linkage_specification_p;
19561 /* Reset implicit_template_scope if we are about to leave the function
19562 parameter list that introduced it. Note that for out-of-line member
19563 definitions, there will be one or more class scopes before we get to
19564 the template parameter scope. */
19566 if (cp_binding_level *its = parser->implicit_template_scope)
19567 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
19569 while (maybe_its->kind == sk_class)
19570 maybe_its = maybe_its->level_chain;
19571 if (maybe_its == its)
19573 parser->implicit_template_parms = 0;
19574 parser->implicit_template_scope = 0;
19578 return parameters;
19581 /* Parse a parameter declaration.
19583 parameter-declaration:
19584 decl-specifier-seq ... [opt] declarator
19585 decl-specifier-seq declarator = assignment-expression
19586 decl-specifier-seq ... [opt] abstract-declarator [opt]
19587 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19589 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19590 declares a template parameter. (In that case, a non-nested `>'
19591 token encountered during the parsing of the assignment-expression
19592 is not interpreted as a greater-than operator.)
19594 Returns a representation of the parameter, or NULL if an error
19595 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19596 true iff the declarator is of the form "(p)". */
19598 static cp_parameter_declarator *
19599 cp_parser_parameter_declaration (cp_parser *parser,
19600 bool template_parm_p,
19601 bool *parenthesized_p)
19603 int declares_class_or_enum;
19604 cp_decl_specifier_seq decl_specifiers;
19605 cp_declarator *declarator;
19606 tree default_argument;
19607 cp_token *token = NULL, *declarator_token_start = NULL;
19608 const char *saved_message;
19610 /* In a template parameter, `>' is not an operator.
19612 [temp.param]
19614 When parsing a default template-argument for a non-type
19615 template-parameter, the first non-nested `>' is taken as the end
19616 of the template parameter-list rather than a greater-than
19617 operator. */
19619 /* Type definitions may not appear in parameter types. */
19620 saved_message = parser->type_definition_forbidden_message;
19621 parser->type_definition_forbidden_message
19622 = G_("types may not be defined in parameter types");
19624 /* Parse the declaration-specifiers. */
19625 cp_parser_decl_specifier_seq (parser,
19626 CP_PARSER_FLAGS_NONE,
19627 &decl_specifiers,
19628 &declares_class_or_enum);
19630 /* Complain about missing 'typename' or other invalid type names. */
19631 if (!decl_specifiers.any_type_specifiers_p
19632 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19633 decl_specifiers.type = error_mark_node;
19635 /* If an error occurred, there's no reason to attempt to parse the
19636 rest of the declaration. */
19637 if (cp_parser_error_occurred (parser))
19639 parser->type_definition_forbidden_message = saved_message;
19640 return NULL;
19643 /* Peek at the next token. */
19644 token = cp_lexer_peek_token (parser->lexer);
19646 /* If the next token is a `)', `,', `=', `>', or `...', then there
19647 is no declarator. However, when variadic templates are enabled,
19648 there may be a declarator following `...'. */
19649 if (token->type == CPP_CLOSE_PAREN
19650 || token->type == CPP_COMMA
19651 || token->type == CPP_EQ
19652 || token->type == CPP_GREATER)
19654 declarator = NULL;
19655 if (parenthesized_p)
19656 *parenthesized_p = false;
19658 /* Otherwise, there should be a declarator. */
19659 else
19661 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19662 parser->default_arg_ok_p = false;
19664 /* After seeing a decl-specifier-seq, if the next token is not a
19665 "(", there is no possibility that the code is a valid
19666 expression. Therefore, if parsing tentatively, we commit at
19667 this point. */
19668 if (!parser->in_template_argument_list_p
19669 /* In an expression context, having seen:
19671 (int((char ...
19673 we cannot be sure whether we are looking at a
19674 function-type (taking a "char" as a parameter) or a cast
19675 of some object of type "char" to "int". */
19676 && !parser->in_type_id_in_expr_p
19677 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19678 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19679 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19680 cp_parser_commit_to_tentative_parse (parser);
19681 /* Parse the declarator. */
19682 declarator_token_start = token;
19683 declarator = cp_parser_declarator (parser,
19684 CP_PARSER_DECLARATOR_EITHER,
19685 /*ctor_dtor_or_conv_p=*/NULL,
19686 parenthesized_p,
19687 /*member_p=*/false,
19688 /*friend_p=*/false);
19689 parser->default_arg_ok_p = saved_default_arg_ok_p;
19690 /* After the declarator, allow more attributes. */
19691 decl_specifiers.attributes
19692 = chainon (decl_specifiers.attributes,
19693 cp_parser_attributes_opt (parser));
19696 /* If the next token is an ellipsis, and we have not seen a
19697 declarator name, and the type of the declarator contains parameter
19698 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19699 a parameter pack expansion expression. Otherwise, leave the
19700 ellipsis for a C-style variadic function. */
19701 token = cp_lexer_peek_token (parser->lexer);
19702 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19704 tree type = decl_specifiers.type;
19706 if (type && DECL_P (type))
19707 type = TREE_TYPE (type);
19709 if (type
19710 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19711 && declarator_can_be_parameter_pack (declarator)
19712 && (!declarator || !declarator->parameter_pack_p)
19713 && uses_parameter_packs (type))
19715 /* Consume the `...'. */
19716 cp_lexer_consume_token (parser->lexer);
19717 maybe_warn_variadic_templates ();
19719 /* Build a pack expansion type */
19720 if (declarator)
19721 declarator->parameter_pack_p = true;
19722 else
19723 decl_specifiers.type = make_pack_expansion (type);
19727 /* The restriction on defining new types applies only to the type
19728 of the parameter, not to the default argument. */
19729 parser->type_definition_forbidden_message = saved_message;
19731 /* If the next token is `=', then process a default argument. */
19732 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19734 tree type = decl_specifiers.type;
19735 token = cp_lexer_peek_token (parser->lexer);
19736 /* If we are defining a class, then the tokens that make up the
19737 default argument must be saved and processed later. */
19738 if (!template_parm_p && at_class_scope_p ()
19739 && TYPE_BEING_DEFINED (current_class_type)
19740 && !LAMBDA_TYPE_P (current_class_type))
19741 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19743 // A constrained-type-specifier may declare a type template-parameter.
19744 else if (declares_type_template_parameter (type))
19745 default_argument
19746 = cp_parser_default_type_template_argument (parser);
19748 // A constrained-type-specifier may declare a template-template-parameter.
19749 else if (declares_template_template_parameter (type))
19750 default_argument
19751 = cp_parser_default_template_template_argument (parser);
19753 /* Outside of a class definition, we can just parse the
19754 assignment-expression. */
19755 else
19756 default_argument
19757 = cp_parser_default_argument (parser, template_parm_p);
19759 if (!parser->default_arg_ok_p)
19761 if (flag_permissive)
19762 warning (0, "deprecated use of default argument for parameter of non-function");
19763 else
19765 error_at (token->location,
19766 "default arguments are only "
19767 "permitted for function parameters");
19768 default_argument = NULL_TREE;
19771 else if ((declarator && declarator->parameter_pack_p)
19772 || (decl_specifiers.type
19773 && PACK_EXPANSION_P (decl_specifiers.type)))
19775 /* Find the name of the parameter pack. */
19776 cp_declarator *id_declarator = declarator;
19777 while (id_declarator && id_declarator->kind != cdk_id)
19778 id_declarator = id_declarator->declarator;
19780 if (id_declarator && id_declarator->kind == cdk_id)
19781 error_at (declarator_token_start->location,
19782 template_parm_p
19783 ? G_("template parameter pack %qD "
19784 "cannot have a default argument")
19785 : G_("parameter pack %qD cannot have "
19786 "a default argument"),
19787 id_declarator->u.id.unqualified_name);
19788 else
19789 error_at (declarator_token_start->location,
19790 template_parm_p
19791 ? G_("template parameter pack cannot have "
19792 "a default argument")
19793 : G_("parameter pack cannot have a "
19794 "default argument"));
19796 default_argument = NULL_TREE;
19799 else
19800 default_argument = NULL_TREE;
19802 return make_parameter_declarator (&decl_specifiers,
19803 declarator,
19804 default_argument);
19807 /* Parse a default argument and return it.
19809 TEMPLATE_PARM_P is true if this is a default argument for a
19810 non-type template parameter. */
19811 static tree
19812 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19814 tree default_argument = NULL_TREE;
19815 bool saved_greater_than_is_operator_p;
19816 bool saved_local_variables_forbidden_p;
19817 bool non_constant_p, is_direct_init;
19819 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19820 set correctly. */
19821 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19822 parser->greater_than_is_operator_p = !template_parm_p;
19823 /* Local variable names (and the `this' keyword) may not
19824 appear in a default argument. */
19825 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19826 parser->local_variables_forbidden_p = true;
19827 /* Parse the assignment-expression. */
19828 if (template_parm_p)
19829 push_deferring_access_checks (dk_no_deferred);
19830 tree saved_class_ptr = NULL_TREE;
19831 tree saved_class_ref = NULL_TREE;
19832 /* The "this" pointer is not valid in a default argument. */
19833 if (cfun)
19835 saved_class_ptr = current_class_ptr;
19836 cp_function_chain->x_current_class_ptr = NULL_TREE;
19837 saved_class_ref = current_class_ref;
19838 cp_function_chain->x_current_class_ref = NULL_TREE;
19840 default_argument
19841 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19842 /* Restore the "this" pointer. */
19843 if (cfun)
19845 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19846 cp_function_chain->x_current_class_ref = saved_class_ref;
19848 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19849 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19850 if (template_parm_p)
19851 pop_deferring_access_checks ();
19852 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19853 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19855 return default_argument;
19858 /* Parse a function-body.
19860 function-body:
19861 compound_statement */
19863 static void
19864 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19866 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19869 /* Parse a ctor-initializer-opt followed by a function-body. Return
19870 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19871 is true we are parsing a function-try-block. */
19873 static bool
19874 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19875 bool in_function_try_block)
19877 tree body, list;
19878 bool ctor_initializer_p;
19879 const bool check_body_p =
19880 DECL_CONSTRUCTOR_P (current_function_decl)
19881 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19882 tree last = NULL;
19884 /* Begin the function body. */
19885 body = begin_function_body ();
19886 /* Parse the optional ctor-initializer. */
19887 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19889 /* If we're parsing a constexpr constructor definition, we need
19890 to check that the constructor body is indeed empty. However,
19891 before we get to cp_parser_function_body lot of junk has been
19892 generated, so we can't just check that we have an empty block.
19893 Rather we take a snapshot of the outermost block, and check whether
19894 cp_parser_function_body changed its state. */
19895 if (check_body_p)
19897 list = cur_stmt_list;
19898 if (STATEMENT_LIST_TAIL (list))
19899 last = STATEMENT_LIST_TAIL (list)->stmt;
19901 /* Parse the function-body. */
19902 cp_parser_function_body (parser, in_function_try_block);
19903 if (check_body_p)
19904 check_constexpr_ctor_body (last, list, /*complain=*/true);
19905 /* Finish the function body. */
19906 finish_function_body (body);
19908 return ctor_initializer_p;
19911 /* Parse an initializer.
19913 initializer:
19914 = initializer-clause
19915 ( expression-list )
19917 Returns an expression representing the initializer. If no
19918 initializer is present, NULL_TREE is returned.
19920 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19921 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19922 set to TRUE if there is no initializer present. If there is an
19923 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19924 is set to true; otherwise it is set to false. */
19926 static tree
19927 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19928 bool* non_constant_p)
19930 cp_token *token;
19931 tree init;
19933 /* Peek at the next token. */
19934 token = cp_lexer_peek_token (parser->lexer);
19936 /* Let our caller know whether or not this initializer was
19937 parenthesized. */
19938 *is_direct_init = (token->type != CPP_EQ);
19939 /* Assume that the initializer is constant. */
19940 *non_constant_p = false;
19942 if (token->type == CPP_EQ)
19944 /* Consume the `='. */
19945 cp_lexer_consume_token (parser->lexer);
19946 /* Parse the initializer-clause. */
19947 init = cp_parser_initializer_clause (parser, non_constant_p);
19949 else if (token->type == CPP_OPEN_PAREN)
19951 vec<tree, va_gc> *vec;
19952 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19953 /*cast_p=*/false,
19954 /*allow_expansion_p=*/true,
19955 non_constant_p);
19956 if (vec == NULL)
19957 return error_mark_node;
19958 init = build_tree_list_vec (vec);
19959 release_tree_vector (vec);
19961 else if (token->type == CPP_OPEN_BRACE)
19963 cp_lexer_set_source_position (parser->lexer);
19964 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19965 init = cp_parser_braced_list (parser, non_constant_p);
19966 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19968 else
19970 /* Anything else is an error. */
19971 cp_parser_error (parser, "expected initializer");
19972 init = error_mark_node;
19975 return init;
19978 /* Parse an initializer-clause.
19980 initializer-clause:
19981 assignment-expression
19982 braced-init-list
19984 Returns an expression representing the initializer.
19986 If the `assignment-expression' production is used the value
19987 returned is simply a representation for the expression.
19989 Otherwise, calls cp_parser_braced_list. */
19991 static tree
19992 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19994 tree initializer;
19996 /* Assume the expression is constant. */
19997 *non_constant_p = false;
19999 /* If it is not a `{', then we are looking at an
20000 assignment-expression. */
20001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20003 initializer
20004 = cp_parser_constant_expression (parser,
20005 /*allow_non_constant_p=*/true,
20006 non_constant_p);
20008 else
20009 initializer = cp_parser_braced_list (parser, non_constant_p);
20011 return initializer;
20014 /* Parse a brace-enclosed initializer list.
20016 braced-init-list:
20017 { initializer-list , [opt] }
20020 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20021 the elements of the initializer-list (or NULL, if the last
20022 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20023 NULL_TREE. There is no way to detect whether or not the optional
20024 trailing `,' was provided. NON_CONSTANT_P is as for
20025 cp_parser_initializer. */
20027 static tree
20028 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20030 tree initializer;
20032 /* Consume the `{' token. */
20033 cp_lexer_consume_token (parser->lexer);
20034 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20035 initializer = make_node (CONSTRUCTOR);
20036 /* If it's not a `}', then there is a non-trivial initializer. */
20037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20039 /* Parse the initializer list. */
20040 CONSTRUCTOR_ELTS (initializer)
20041 = cp_parser_initializer_list (parser, non_constant_p);
20042 /* A trailing `,' token is allowed. */
20043 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20044 cp_lexer_consume_token (parser->lexer);
20046 else
20047 *non_constant_p = false;
20048 /* Now, there should be a trailing `}'. */
20049 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20050 TREE_TYPE (initializer) = init_list_type_node;
20051 return initializer;
20054 /* Consume tokens up to, and including, the next non-nested closing `]'.
20055 Returns true iff we found a closing `]'. */
20057 static bool
20058 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20060 unsigned square_depth = 0;
20062 while (true)
20064 cp_token * token = cp_lexer_peek_token (parser->lexer);
20066 switch (token->type)
20068 case CPP_EOF:
20069 case CPP_PRAGMA_EOL:
20070 /* If we've run out of tokens, then there is no closing `]'. */
20071 return false;
20073 case CPP_OPEN_SQUARE:
20074 ++square_depth;
20075 break;
20077 case CPP_CLOSE_SQUARE:
20078 if (!square_depth--)
20080 cp_lexer_consume_token (parser->lexer);
20081 return true;
20083 break;
20085 default:
20086 break;
20089 /* Consume the token. */
20090 cp_lexer_consume_token (parser->lexer);
20094 /* Return true if we are looking at an array-designator, false otherwise. */
20096 static bool
20097 cp_parser_array_designator_p (cp_parser *parser)
20099 /* Consume the `['. */
20100 cp_lexer_consume_token (parser->lexer);
20102 cp_lexer_save_tokens (parser->lexer);
20104 /* Skip tokens until the next token is a closing square bracket.
20105 If we find the closing `]', and the next token is a `=', then
20106 we are looking at an array designator. */
20107 bool array_designator_p
20108 = (cp_parser_skip_to_closing_square_bracket (parser)
20109 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20111 /* Roll back the tokens we skipped. */
20112 cp_lexer_rollback_tokens (parser->lexer);
20114 return array_designator_p;
20117 /* Parse an initializer-list.
20119 initializer-list:
20120 initializer-clause ... [opt]
20121 initializer-list , initializer-clause ... [opt]
20123 GNU Extension:
20125 initializer-list:
20126 designation initializer-clause ...[opt]
20127 initializer-list , designation initializer-clause ...[opt]
20129 designation:
20130 . identifier =
20131 identifier :
20132 [ constant-expression ] =
20134 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20135 for the initializer. If the INDEX of the elt is non-NULL, it is the
20136 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20137 as for cp_parser_initializer. */
20139 static vec<constructor_elt, va_gc> *
20140 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20142 vec<constructor_elt, va_gc> *v = NULL;
20144 /* Assume all of the expressions are constant. */
20145 *non_constant_p = false;
20147 /* Parse the rest of the list. */
20148 while (true)
20150 cp_token *token;
20151 tree designator;
20152 tree initializer;
20153 bool clause_non_constant_p;
20155 /* If the next token is an identifier and the following one is a
20156 colon, we are looking at the GNU designated-initializer
20157 syntax. */
20158 if (cp_parser_allow_gnu_extensions_p (parser)
20159 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20160 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20162 /* Warn the user that they are using an extension. */
20163 pedwarn (input_location, OPT_Wpedantic,
20164 "ISO C++ does not allow designated initializers");
20165 /* Consume the identifier. */
20166 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20167 /* Consume the `:'. */
20168 cp_lexer_consume_token (parser->lexer);
20170 /* Also handle the C99 syntax, '. id ='. */
20171 else if (cp_parser_allow_gnu_extensions_p (parser)
20172 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20173 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20174 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20176 /* Warn the user that they are using an extension. */
20177 pedwarn (input_location, OPT_Wpedantic,
20178 "ISO C++ does not allow C99 designated initializers");
20179 /* Consume the `.'. */
20180 cp_lexer_consume_token (parser->lexer);
20181 /* Consume the identifier. */
20182 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20183 /* Consume the `='. */
20184 cp_lexer_consume_token (parser->lexer);
20186 /* Also handle C99 array designators, '[ const ] ='. */
20187 else if (cp_parser_allow_gnu_extensions_p (parser)
20188 && !c_dialect_objc ()
20189 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20191 /* In C++11, [ could start a lambda-introducer. */
20192 bool non_const = false;
20194 cp_parser_parse_tentatively (parser);
20196 if (!cp_parser_array_designator_p (parser))
20198 cp_parser_simulate_error (parser);
20199 designator = NULL_TREE;
20201 else
20203 designator = cp_parser_constant_expression (parser, true,
20204 &non_const);
20205 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20206 cp_parser_require (parser, CPP_EQ, RT_EQ);
20209 if (!cp_parser_parse_definitely (parser))
20210 designator = NULL_TREE;
20211 else if (non_const)
20212 require_potential_rvalue_constant_expression (designator);
20214 else
20215 designator = NULL_TREE;
20217 /* Parse the initializer. */
20218 initializer = cp_parser_initializer_clause (parser,
20219 &clause_non_constant_p);
20220 /* If any clause is non-constant, so is the entire initializer. */
20221 if (clause_non_constant_p)
20222 *non_constant_p = true;
20224 /* If we have an ellipsis, this is an initializer pack
20225 expansion. */
20226 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20228 /* Consume the `...'. */
20229 cp_lexer_consume_token (parser->lexer);
20231 /* Turn the initializer into an initializer expansion. */
20232 initializer = make_pack_expansion (initializer);
20235 /* Add it to the vector. */
20236 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
20238 /* If the next token is not a comma, we have reached the end of
20239 the list. */
20240 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20241 break;
20243 /* Peek at the next token. */
20244 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20245 /* If the next token is a `}', then we're still done. An
20246 initializer-clause can have a trailing `,' after the
20247 initializer-list and before the closing `}'. */
20248 if (token->type == CPP_CLOSE_BRACE)
20249 break;
20251 /* Consume the `,' token. */
20252 cp_lexer_consume_token (parser->lexer);
20255 return v;
20258 /* Classes [gram.class] */
20260 /* Parse a class-name.
20262 class-name:
20263 identifier
20264 template-id
20266 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
20267 to indicate that names looked up in dependent types should be
20268 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
20269 keyword has been used to indicate that the name that appears next
20270 is a template. TAG_TYPE indicates the explicit tag given before
20271 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
20272 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
20273 is the class being defined in a class-head.
20275 Returns the TYPE_DECL representing the class. */
20277 static tree
20278 cp_parser_class_name (cp_parser *parser,
20279 bool typename_keyword_p,
20280 bool template_keyword_p,
20281 enum tag_types tag_type,
20282 bool check_dependency_p,
20283 bool class_head_p,
20284 bool is_declaration)
20286 tree decl;
20287 tree scope;
20288 bool typename_p;
20289 cp_token *token;
20290 tree identifier = NULL_TREE;
20292 /* All class-names start with an identifier. */
20293 token = cp_lexer_peek_token (parser->lexer);
20294 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
20296 cp_parser_error (parser, "expected class-name");
20297 return error_mark_node;
20300 /* PARSER->SCOPE can be cleared when parsing the template-arguments
20301 to a template-id, so we save it here. */
20302 scope = parser->scope;
20303 if (scope == error_mark_node)
20304 return error_mark_node;
20306 /* Any name names a type if we're following the `typename' keyword
20307 in a qualified name where the enclosing scope is type-dependent. */
20308 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
20309 && dependent_type_p (scope));
20310 /* Handle the common case (an identifier, but not a template-id)
20311 efficiently. */
20312 if (token->type == CPP_NAME
20313 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
20315 cp_token *identifier_token;
20316 bool ambiguous_p;
20318 /* Look for the identifier. */
20319 identifier_token = cp_lexer_peek_token (parser->lexer);
20320 ambiguous_p = identifier_token->error_reported;
20321 identifier = cp_parser_identifier (parser);
20322 /* If the next token isn't an identifier, we are certainly not
20323 looking at a class-name. */
20324 if (identifier == error_mark_node)
20325 decl = error_mark_node;
20326 /* If we know this is a type-name, there's no need to look it
20327 up. */
20328 else if (typename_p)
20329 decl = identifier;
20330 else
20332 tree ambiguous_decls;
20333 /* If we already know that this lookup is ambiguous, then
20334 we've already issued an error message; there's no reason
20335 to check again. */
20336 if (ambiguous_p)
20338 cp_parser_simulate_error (parser);
20339 return error_mark_node;
20341 /* If the next token is a `::', then the name must be a type
20342 name.
20344 [basic.lookup.qual]
20346 During the lookup for a name preceding the :: scope
20347 resolution operator, object, function, and enumerator
20348 names are ignored. */
20349 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20350 tag_type = typename_type;
20351 /* Look up the name. */
20352 decl = cp_parser_lookup_name (parser, identifier,
20353 tag_type,
20354 /*is_template=*/false,
20355 /*is_namespace=*/false,
20356 check_dependency_p,
20357 &ambiguous_decls,
20358 identifier_token->location);
20359 if (ambiguous_decls)
20361 if (cp_parser_parsing_tentatively (parser))
20362 cp_parser_simulate_error (parser);
20363 return error_mark_node;
20367 else
20369 /* Try a template-id. */
20370 decl = cp_parser_template_id (parser, template_keyword_p,
20371 check_dependency_p,
20372 tag_type,
20373 is_declaration);
20374 if (decl == error_mark_node)
20375 return error_mark_node;
20378 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
20380 /* If this is a typename, create a TYPENAME_TYPE. */
20381 if (typename_p && decl != error_mark_node)
20383 decl = make_typename_type (scope, decl, typename_type,
20384 /*complain=*/tf_error);
20385 if (decl != error_mark_node)
20386 decl = TYPE_NAME (decl);
20389 decl = strip_using_decl (decl);
20391 /* Check to see that it is really the name of a class. */
20392 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20393 && identifier_p (TREE_OPERAND (decl, 0))
20394 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20395 /* Situations like this:
20397 template <typename T> struct A {
20398 typename T::template X<int>::I i;
20401 are problematic. Is `T::template X<int>' a class-name? The
20402 standard does not seem to be definitive, but there is no other
20403 valid interpretation of the following `::'. Therefore, those
20404 names are considered class-names. */
20406 decl = make_typename_type (scope, decl, tag_type, tf_error);
20407 if (decl != error_mark_node)
20408 decl = TYPE_NAME (decl);
20410 else if (TREE_CODE (decl) != TYPE_DECL
20411 || TREE_TYPE (decl) == error_mark_node
20412 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
20413 /* In Objective-C 2.0, a classname followed by '.' starts a
20414 dot-syntax expression, and it's not a type-name. */
20415 || (c_dialect_objc ()
20416 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
20417 && objc_is_class_name (decl)))
20418 decl = error_mark_node;
20420 if (decl == error_mark_node)
20421 cp_parser_error (parser, "expected class-name");
20422 else if (identifier && !parser->scope)
20423 maybe_note_name_used_in_class (identifier, decl);
20425 return decl;
20428 /* Parse a class-specifier.
20430 class-specifier:
20431 class-head { member-specification [opt] }
20433 Returns the TREE_TYPE representing the class. */
20435 static tree
20436 cp_parser_class_specifier_1 (cp_parser* parser)
20438 tree type;
20439 tree attributes = NULL_TREE;
20440 bool nested_name_specifier_p;
20441 unsigned saved_num_template_parameter_lists;
20442 bool saved_in_function_body;
20443 unsigned char in_statement;
20444 bool in_switch_statement_p;
20445 bool saved_in_unbraced_linkage_specification_p;
20446 tree old_scope = NULL_TREE;
20447 tree scope = NULL_TREE;
20448 cp_token *closing_brace;
20450 push_deferring_access_checks (dk_no_deferred);
20452 /* Parse the class-head. */
20453 type = cp_parser_class_head (parser,
20454 &nested_name_specifier_p);
20456 /* If the class-head was a semantic disaster, skip the entire body
20457 of the class. */
20458 if (!type)
20460 cp_parser_skip_to_end_of_block_or_statement (parser);
20461 pop_deferring_access_checks ();
20462 return error_mark_node;
20465 /* Look for the `{'. */
20466 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
20468 pop_deferring_access_checks ();
20469 return error_mark_node;
20472 cp_ensure_no_omp_declare_simd (parser);
20474 /* Issue an error message if type-definitions are forbidden here. */
20475 cp_parser_check_type_definition (parser);
20476 /* Remember that we are defining one more class. */
20477 ++parser->num_classes_being_defined;
20478 /* Inside the class, surrounding template-parameter-lists do not
20479 apply. */
20480 saved_num_template_parameter_lists
20481 = parser->num_template_parameter_lists;
20482 parser->num_template_parameter_lists = 0;
20483 /* We are not in a function body. */
20484 saved_in_function_body = parser->in_function_body;
20485 parser->in_function_body = false;
20486 /* Or in a loop. */
20487 in_statement = parser->in_statement;
20488 parser->in_statement = 0;
20489 /* Or in a switch. */
20490 in_switch_statement_p = parser->in_switch_statement_p;
20491 parser->in_switch_statement_p = false;
20492 /* We are not immediately inside an extern "lang" block. */
20493 saved_in_unbraced_linkage_specification_p
20494 = parser->in_unbraced_linkage_specification_p;
20495 parser->in_unbraced_linkage_specification_p = false;
20497 // Associate constraints with the type.
20498 if (flag_concepts)
20499 type = associate_classtype_constraints (type);
20501 /* Start the class. */
20502 if (nested_name_specifier_p)
20504 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
20505 old_scope = push_inner_scope (scope);
20507 type = begin_class_definition (type);
20509 if (type == error_mark_node)
20510 /* If the type is erroneous, skip the entire body of the class. */
20511 cp_parser_skip_to_closing_brace (parser);
20512 else
20513 /* Parse the member-specification. */
20514 cp_parser_member_specification_opt (parser);
20516 /* Look for the trailing `}'. */
20517 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20518 /* Look for trailing attributes to apply to this class. */
20519 if (cp_parser_allow_gnu_extensions_p (parser))
20520 attributes = cp_parser_gnu_attributes_opt (parser);
20521 if (type != error_mark_node)
20522 type = finish_struct (type, attributes);
20523 if (nested_name_specifier_p)
20524 pop_inner_scope (old_scope, scope);
20526 /* We've finished a type definition. Check for the common syntax
20527 error of forgetting a semicolon after the definition. We need to
20528 be careful, as we can't just check for not-a-semicolon and be done
20529 with it; the user might have typed:
20531 class X { } c = ...;
20532 class X { } *p = ...;
20534 and so forth. Instead, enumerate all the possible tokens that
20535 might follow this production; if we don't see one of them, then
20536 complain and silently insert the semicolon. */
20538 cp_token *token = cp_lexer_peek_token (parser->lexer);
20539 bool want_semicolon = true;
20541 if (cp_next_tokens_can_be_std_attribute_p (parser))
20542 /* Don't try to parse c++11 attributes here. As per the
20543 grammar, that should be a task for
20544 cp_parser_decl_specifier_seq. */
20545 want_semicolon = false;
20547 switch (token->type)
20549 case CPP_NAME:
20550 case CPP_SEMICOLON:
20551 case CPP_MULT:
20552 case CPP_AND:
20553 case CPP_OPEN_PAREN:
20554 case CPP_CLOSE_PAREN:
20555 case CPP_COMMA:
20556 want_semicolon = false;
20557 break;
20559 /* While it's legal for type qualifiers and storage class
20560 specifiers to follow type definitions in the grammar, only
20561 compiler testsuites contain code like that. Assume that if
20562 we see such code, then what we're really seeing is a case
20563 like:
20565 class X { }
20566 const <type> var = ...;
20570 class Y { }
20571 static <type> func (...) ...
20573 i.e. the qualifier or specifier applies to the next
20574 declaration. To do so, however, we need to look ahead one
20575 more token to see if *that* token is a type specifier.
20577 This code could be improved to handle:
20579 class Z { }
20580 static const <type> var = ...; */
20581 case CPP_KEYWORD:
20582 if (keyword_is_decl_specifier (token->keyword))
20584 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
20586 /* Handling user-defined types here would be nice, but very
20587 tricky. */
20588 want_semicolon
20589 = (lookahead->type == CPP_KEYWORD
20590 && keyword_begins_type_specifier (lookahead->keyword));
20592 break;
20593 default:
20594 break;
20597 /* If we don't have a type, then something is very wrong and we
20598 shouldn't try to do anything clever. Likewise for not seeing the
20599 closing brace. */
20600 if (closing_brace && TYPE_P (type) && want_semicolon)
20602 cp_token_position prev
20603 = cp_lexer_previous_token_position (parser->lexer);
20604 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20605 location_t loc = prev_token->location;
20607 if (CLASSTYPE_DECLARED_CLASS (type))
20608 error_at (loc, "expected %<;%> after class definition");
20609 else if (TREE_CODE (type) == RECORD_TYPE)
20610 error_at (loc, "expected %<;%> after struct definition");
20611 else if (TREE_CODE (type) == UNION_TYPE)
20612 error_at (loc, "expected %<;%> after union definition");
20613 else
20614 gcc_unreachable ();
20616 /* Unget one token and smash it to look as though we encountered
20617 a semicolon in the input stream. */
20618 cp_lexer_set_token_position (parser->lexer, prev);
20619 token = cp_lexer_peek_token (parser->lexer);
20620 token->type = CPP_SEMICOLON;
20621 token->keyword = RID_MAX;
20625 /* If this class is not itself within the scope of another class,
20626 then we need to parse the bodies of all of the queued function
20627 definitions. Note that the queued functions defined in a class
20628 are not always processed immediately following the
20629 class-specifier for that class. Consider:
20631 struct A {
20632 struct B { void f() { sizeof (A); } };
20635 If `f' were processed before the processing of `A' were
20636 completed, there would be no way to compute the size of `A'.
20637 Note that the nesting we are interested in here is lexical --
20638 not the semantic nesting given by TYPE_CONTEXT. In particular,
20639 for:
20641 struct A { struct B; };
20642 struct A::B { void f() { } };
20644 there is no need to delay the parsing of `A::B::f'. */
20645 if (--parser->num_classes_being_defined == 0)
20647 tree decl;
20648 tree class_type = NULL_TREE;
20649 tree pushed_scope = NULL_TREE;
20650 unsigned ix;
20651 cp_default_arg_entry *e;
20652 tree save_ccp, save_ccr;
20654 /* In a first pass, parse default arguments to the functions.
20655 Then, in a second pass, parse the bodies of the functions.
20656 This two-phased approach handles cases like:
20658 struct S {
20659 void f() { g(); }
20660 void g(int i = 3);
20664 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20666 decl = e->decl;
20667 /* If there are default arguments that have not yet been processed,
20668 take care of them now. */
20669 if (class_type != e->class_type)
20671 if (pushed_scope)
20672 pop_scope (pushed_scope);
20673 class_type = e->class_type;
20674 pushed_scope = push_scope (class_type);
20676 /* Make sure that any template parameters are in scope. */
20677 maybe_begin_member_template_processing (decl);
20678 /* Parse the default argument expressions. */
20679 cp_parser_late_parsing_default_args (parser, decl);
20680 /* Remove any template parameters from the symbol table. */
20681 maybe_end_member_template_processing ();
20683 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20684 /* Now parse any NSDMIs. */
20685 save_ccp = current_class_ptr;
20686 save_ccr = current_class_ref;
20687 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20689 if (class_type != DECL_CONTEXT (decl))
20691 if (pushed_scope)
20692 pop_scope (pushed_scope);
20693 class_type = DECL_CONTEXT (decl);
20694 pushed_scope = push_scope (class_type);
20696 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20697 cp_parser_late_parsing_nsdmi (parser, decl);
20699 vec_safe_truncate (unparsed_nsdmis, 0);
20700 current_class_ptr = save_ccp;
20701 current_class_ref = save_ccr;
20702 if (pushed_scope)
20703 pop_scope (pushed_scope);
20705 /* Now do some post-NSDMI bookkeeping. */
20706 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20707 after_nsdmi_defaulted_late_checks (class_type);
20708 vec_safe_truncate (unparsed_classes, 0);
20709 after_nsdmi_defaulted_late_checks (type);
20711 /* Now parse the body of the functions. */
20712 if (flag_openmp)
20714 /* OpenMP UDRs need to be parsed before all other functions. */
20715 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20716 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20717 cp_parser_late_parsing_for_member (parser, decl);
20718 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20719 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20720 cp_parser_late_parsing_for_member (parser, decl);
20722 else
20723 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20724 cp_parser_late_parsing_for_member (parser, decl);
20725 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20727 else
20728 vec_safe_push (unparsed_classes, type);
20730 /* Put back any saved access checks. */
20731 pop_deferring_access_checks ();
20733 /* Restore saved state. */
20734 parser->in_switch_statement_p = in_switch_statement_p;
20735 parser->in_statement = in_statement;
20736 parser->in_function_body = saved_in_function_body;
20737 parser->num_template_parameter_lists
20738 = saved_num_template_parameter_lists;
20739 parser->in_unbraced_linkage_specification_p
20740 = saved_in_unbraced_linkage_specification_p;
20742 return type;
20745 static tree
20746 cp_parser_class_specifier (cp_parser* parser)
20748 tree ret;
20749 timevar_push (TV_PARSE_STRUCT);
20750 ret = cp_parser_class_specifier_1 (parser);
20751 timevar_pop (TV_PARSE_STRUCT);
20752 return ret;
20755 /* Parse a class-head.
20757 class-head:
20758 class-key identifier [opt] base-clause [opt]
20759 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20760 class-key nested-name-specifier [opt] template-id
20761 base-clause [opt]
20763 class-virt-specifier:
20764 final
20766 GNU Extensions:
20767 class-key attributes identifier [opt] base-clause [opt]
20768 class-key attributes nested-name-specifier identifier base-clause [opt]
20769 class-key attributes nested-name-specifier [opt] template-id
20770 base-clause [opt]
20772 Upon return BASES is initialized to the list of base classes (or
20773 NULL, if there are none) in the same form returned by
20774 cp_parser_base_clause.
20776 Returns the TYPE of the indicated class. Sets
20777 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20778 involving a nested-name-specifier was used, and FALSE otherwise.
20780 Returns error_mark_node if this is not a class-head.
20782 Returns NULL_TREE if the class-head is syntactically valid, but
20783 semantically invalid in a way that means we should skip the entire
20784 body of the class. */
20786 static tree
20787 cp_parser_class_head (cp_parser* parser,
20788 bool* nested_name_specifier_p)
20790 tree nested_name_specifier;
20791 enum tag_types class_key;
20792 tree id = NULL_TREE;
20793 tree type = NULL_TREE;
20794 tree attributes;
20795 tree bases;
20796 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20797 bool template_id_p = false;
20798 bool qualified_p = false;
20799 bool invalid_nested_name_p = false;
20800 bool invalid_explicit_specialization_p = false;
20801 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20802 tree pushed_scope = NULL_TREE;
20803 unsigned num_templates;
20804 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20805 /* Assume no nested-name-specifier will be present. */
20806 *nested_name_specifier_p = false;
20807 /* Assume no template parameter lists will be used in defining the
20808 type. */
20809 num_templates = 0;
20810 parser->colon_corrects_to_scope_p = false;
20812 /* Look for the class-key. */
20813 class_key = cp_parser_class_key (parser);
20814 if (class_key == none_type)
20815 return error_mark_node;
20817 /* Parse the attributes. */
20818 attributes = cp_parser_attributes_opt (parser);
20820 /* If the next token is `::', that is invalid -- but sometimes
20821 people do try to write:
20823 struct ::S {};
20825 Handle this gracefully by accepting the extra qualifier, and then
20826 issuing an error about it later if this really is a
20827 class-head. If it turns out just to be an elaborated type
20828 specifier, remain silent. */
20829 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20830 qualified_p = true;
20832 push_deferring_access_checks (dk_no_check);
20834 /* Determine the name of the class. Begin by looking for an
20835 optional nested-name-specifier. */
20836 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20837 nested_name_specifier
20838 = cp_parser_nested_name_specifier_opt (parser,
20839 /*typename_keyword_p=*/false,
20840 /*check_dependency_p=*/false,
20841 /*type_p=*/true,
20842 /*is_declaration=*/false);
20843 /* If there was a nested-name-specifier, then there *must* be an
20844 identifier. */
20845 if (nested_name_specifier)
20847 type_start_token = cp_lexer_peek_token (parser->lexer);
20848 /* Although the grammar says `identifier', it really means
20849 `class-name' or `template-name'. You are only allowed to
20850 define a class that has already been declared with this
20851 syntax.
20853 The proposed resolution for Core Issue 180 says that wherever
20854 you see `class T::X' you should treat `X' as a type-name.
20856 It is OK to define an inaccessible class; for example:
20858 class A { class B; };
20859 class A::B {};
20861 We do not know if we will see a class-name, or a
20862 template-name. We look for a class-name first, in case the
20863 class-name is a template-id; if we looked for the
20864 template-name first we would stop after the template-name. */
20865 cp_parser_parse_tentatively (parser);
20866 type = cp_parser_class_name (parser,
20867 /*typename_keyword_p=*/false,
20868 /*template_keyword_p=*/false,
20869 class_type,
20870 /*check_dependency_p=*/false,
20871 /*class_head_p=*/true,
20872 /*is_declaration=*/false);
20873 /* If that didn't work, ignore the nested-name-specifier. */
20874 if (!cp_parser_parse_definitely (parser))
20876 invalid_nested_name_p = true;
20877 type_start_token = cp_lexer_peek_token (parser->lexer);
20878 id = cp_parser_identifier (parser);
20879 if (id == error_mark_node)
20880 id = NULL_TREE;
20882 /* If we could not find a corresponding TYPE, treat this
20883 declaration like an unqualified declaration. */
20884 if (type == error_mark_node)
20885 nested_name_specifier = NULL_TREE;
20886 /* Otherwise, count the number of templates used in TYPE and its
20887 containing scopes. */
20888 else
20890 tree scope;
20892 for (scope = TREE_TYPE (type);
20893 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20894 scope = get_containing_scope (scope))
20895 if (TYPE_P (scope)
20896 && CLASS_TYPE_P (scope)
20897 && CLASSTYPE_TEMPLATE_INFO (scope)
20898 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20899 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20900 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20901 ++num_templates;
20904 /* Otherwise, the identifier is optional. */
20905 else
20907 /* We don't know whether what comes next is a template-id,
20908 an identifier, or nothing at all. */
20909 cp_parser_parse_tentatively (parser);
20910 /* Check for a template-id. */
20911 type_start_token = cp_lexer_peek_token (parser->lexer);
20912 id = cp_parser_template_id (parser,
20913 /*template_keyword_p=*/false,
20914 /*check_dependency_p=*/true,
20915 class_key,
20916 /*is_declaration=*/true);
20917 /* If that didn't work, it could still be an identifier. */
20918 if (!cp_parser_parse_definitely (parser))
20920 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20922 type_start_token = cp_lexer_peek_token (parser->lexer);
20923 id = cp_parser_identifier (parser);
20925 else
20926 id = NULL_TREE;
20928 else
20930 template_id_p = true;
20931 ++num_templates;
20935 pop_deferring_access_checks ();
20937 if (id)
20939 cp_parser_check_for_invalid_template_id (parser, id,
20940 class_key,
20941 type_start_token->location);
20943 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20945 /* If it's not a `:' or a `{' then we can't really be looking at a
20946 class-head, since a class-head only appears as part of a
20947 class-specifier. We have to detect this situation before calling
20948 xref_tag, since that has irreversible side-effects. */
20949 if (!cp_parser_next_token_starts_class_definition_p (parser))
20951 cp_parser_error (parser, "expected %<{%> or %<:%>");
20952 type = error_mark_node;
20953 goto out;
20956 /* At this point, we're going ahead with the class-specifier, even
20957 if some other problem occurs. */
20958 cp_parser_commit_to_tentative_parse (parser);
20959 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20961 cp_parser_error (parser,
20962 "cannot specify %<override%> for a class");
20963 type = error_mark_node;
20964 goto out;
20966 /* Issue the error about the overly-qualified name now. */
20967 if (qualified_p)
20969 cp_parser_error (parser,
20970 "global qualification of class name is invalid");
20971 type = error_mark_node;
20972 goto out;
20974 else if (invalid_nested_name_p)
20976 cp_parser_error (parser,
20977 "qualified name does not name a class");
20978 type = error_mark_node;
20979 goto out;
20981 else if (nested_name_specifier)
20983 tree scope;
20985 /* Reject typedef-names in class heads. */
20986 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20988 error_at (type_start_token->location,
20989 "invalid class name in declaration of %qD",
20990 type);
20991 type = NULL_TREE;
20992 goto done;
20995 /* Figure out in what scope the declaration is being placed. */
20996 scope = current_scope ();
20997 /* If that scope does not contain the scope in which the
20998 class was originally declared, the program is invalid. */
20999 if (scope && !is_ancestor (scope, nested_name_specifier))
21001 if (at_namespace_scope_p ())
21002 error_at (type_start_token->location,
21003 "declaration of %qD in namespace %qD which does not "
21004 "enclose %qD",
21005 type, scope, nested_name_specifier);
21006 else
21007 error_at (type_start_token->location,
21008 "declaration of %qD in %qD which does not enclose %qD",
21009 type, scope, nested_name_specifier);
21010 type = NULL_TREE;
21011 goto done;
21013 /* [dcl.meaning]
21015 A declarator-id shall not be qualified except for the
21016 definition of a ... nested class outside of its class
21017 ... [or] the definition or explicit instantiation of a
21018 class member of a namespace outside of its namespace. */
21019 if (scope == nested_name_specifier)
21021 permerror (nested_name_specifier_token_start->location,
21022 "extra qualification not allowed");
21023 nested_name_specifier = NULL_TREE;
21024 num_templates = 0;
21027 /* An explicit-specialization must be preceded by "template <>". If
21028 it is not, try to recover gracefully. */
21029 if (at_namespace_scope_p ()
21030 && parser->num_template_parameter_lists == 0
21031 && template_id_p)
21033 error_at (type_start_token->location,
21034 "an explicit specialization must be preceded by %<template <>%>");
21035 invalid_explicit_specialization_p = true;
21036 /* Take the same action that would have been taken by
21037 cp_parser_explicit_specialization. */
21038 ++parser->num_template_parameter_lists;
21039 begin_specialization ();
21041 /* There must be no "return" statements between this point and the
21042 end of this function; set "type "to the correct return value and
21043 use "goto done;" to return. */
21044 /* Make sure that the right number of template parameters were
21045 present. */
21046 if (!cp_parser_check_template_parameters (parser, num_templates,
21047 type_start_token->location,
21048 /*declarator=*/NULL))
21050 /* If something went wrong, there is no point in even trying to
21051 process the class-definition. */
21052 type = NULL_TREE;
21053 goto done;
21056 /* Look up the type. */
21057 if (template_id_p)
21059 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21060 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21061 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21063 error_at (type_start_token->location,
21064 "function template %qD redeclared as a class template", id);
21065 type = error_mark_node;
21067 else
21069 type = TREE_TYPE (id);
21070 type = maybe_process_partial_specialization (type);
21072 if (nested_name_specifier)
21073 pushed_scope = push_scope (nested_name_specifier);
21075 else if (nested_name_specifier)
21077 tree class_type;
21079 /* Given:
21081 template <typename T> struct S { struct T };
21082 template <typename T> struct S<T>::T { };
21084 we will get a TYPENAME_TYPE when processing the definition of
21085 `S::T'. We need to resolve it to the actual type before we
21086 try to define it. */
21087 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21089 class_type = resolve_typename_type (TREE_TYPE (type),
21090 /*only_current_p=*/false);
21091 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21092 type = TYPE_NAME (class_type);
21093 else
21095 cp_parser_error (parser, "could not resolve typename type");
21096 type = error_mark_node;
21100 if (maybe_process_partial_specialization (TREE_TYPE (type))
21101 == error_mark_node)
21103 type = NULL_TREE;
21104 goto done;
21107 class_type = current_class_type;
21108 /* Enter the scope indicated by the nested-name-specifier. */
21109 pushed_scope = push_scope (nested_name_specifier);
21110 /* Get the canonical version of this type. */
21111 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21112 /* Call push_template_decl if it seems like we should be defining a
21113 template either from the template headers or the type we're
21114 defining, so that we diagnose both extra and missing headers. */
21115 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21116 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21119 type = push_template_decl (type);
21120 if (type == error_mark_node)
21122 type = NULL_TREE;
21123 goto done;
21127 type = TREE_TYPE (type);
21128 *nested_name_specifier_p = true;
21130 else /* The name is not a nested name. */
21132 /* If the class was unnamed, create a dummy name. */
21133 if (!id)
21134 id = make_anon_name ();
21135 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21136 parser->num_template_parameter_lists);
21139 /* Indicate whether this class was declared as a `class' or as a
21140 `struct'. */
21141 if (TREE_CODE (type) == RECORD_TYPE)
21142 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21143 cp_parser_check_class_key (class_key, type);
21145 /* If this type was already complete, and we see another definition,
21146 that's an error. */
21147 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21149 error_at (type_start_token->location, "redefinition of %q#T",
21150 type);
21151 error_at (type_start_token->location, "previous definition of %q+#T",
21152 type);
21153 type = NULL_TREE;
21154 goto done;
21156 else if (type == error_mark_node)
21157 type = NULL_TREE;
21159 if (type)
21161 /* Apply attributes now, before any use of the class as a template
21162 argument in its base list. */
21163 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21164 fixup_attribute_variants (type);
21167 /* We will have entered the scope containing the class; the names of
21168 base classes should be looked up in that context. For example:
21170 struct A { struct B {}; struct C; };
21171 struct A::C : B {};
21173 is valid. */
21175 /* Get the list of base-classes, if there is one. */
21176 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21178 /* PR59482: enter the class scope so that base-specifiers are looked
21179 up correctly. */
21180 if (type)
21181 pushclass (type);
21182 bases = cp_parser_base_clause (parser);
21183 /* PR59482: get out of the previously pushed class scope so that the
21184 subsequent pops pop the right thing. */
21185 if (type)
21186 popclass ();
21188 else
21189 bases = NULL_TREE;
21191 /* If we're really defining a class, process the base classes.
21192 If they're invalid, fail. */
21193 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21194 && !xref_basetypes (type, bases))
21195 type = NULL_TREE;
21197 done:
21198 /* Leave the scope given by the nested-name-specifier. We will
21199 enter the class scope itself while processing the members. */
21200 if (pushed_scope)
21201 pop_scope (pushed_scope);
21203 if (invalid_explicit_specialization_p)
21205 end_specialization ();
21206 --parser->num_template_parameter_lists;
21209 if (type)
21210 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21211 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
21212 CLASSTYPE_FINAL (type) = 1;
21213 out:
21214 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21215 return type;
21218 /* Parse a class-key.
21220 class-key:
21221 class
21222 struct
21223 union
21225 Returns the kind of class-key specified, or none_type to indicate
21226 error. */
21228 static enum tag_types
21229 cp_parser_class_key (cp_parser* parser)
21231 cp_token *token;
21232 enum tag_types tag_type;
21234 /* Look for the class-key. */
21235 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
21236 if (!token)
21237 return none_type;
21239 /* Check to see if the TOKEN is a class-key. */
21240 tag_type = cp_parser_token_is_class_key (token);
21241 if (!tag_type)
21242 cp_parser_error (parser, "expected class-key");
21243 return tag_type;
21246 /* Parse a type-parameter-key.
21248 type-parameter-key:
21249 class
21250 typename
21253 static void
21254 cp_parser_type_parameter_key (cp_parser* parser)
21256 /* Look for the type-parameter-key. */
21257 enum tag_types tag_type = none_type;
21258 cp_token *token = cp_lexer_peek_token (parser->lexer);
21259 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
21261 cp_lexer_consume_token (parser->lexer);
21262 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
21263 /* typename is not allowed in a template template parameter
21264 by the standard until C++1Z. */
21265 pedwarn (token->location, OPT_Wpedantic,
21266 "ISO C++ forbids typename key in template template parameter;"
21267 " use -std=c++1z or -std=gnu++1z");
21269 else
21270 cp_parser_error (parser, "expected %<class%> or %<typename%>");
21272 return;
21275 /* Parse an (optional) member-specification.
21277 member-specification:
21278 member-declaration member-specification [opt]
21279 access-specifier : member-specification [opt] */
21281 static void
21282 cp_parser_member_specification_opt (cp_parser* parser)
21284 while (true)
21286 cp_token *token;
21287 enum rid keyword;
21289 /* Peek at the next token. */
21290 token = cp_lexer_peek_token (parser->lexer);
21291 /* If it's a `}', or EOF then we've seen all the members. */
21292 if (token->type == CPP_CLOSE_BRACE
21293 || token->type == CPP_EOF
21294 || token->type == CPP_PRAGMA_EOL)
21295 break;
21297 /* See if this token is a keyword. */
21298 keyword = token->keyword;
21299 switch (keyword)
21301 case RID_PUBLIC:
21302 case RID_PROTECTED:
21303 case RID_PRIVATE:
21304 /* Consume the access-specifier. */
21305 cp_lexer_consume_token (parser->lexer);
21306 /* Remember which access-specifier is active. */
21307 current_access_specifier = token->u.value;
21308 /* Look for the `:'. */
21309 cp_parser_require (parser, CPP_COLON, RT_COLON);
21310 break;
21312 default:
21313 /* Accept #pragmas at class scope. */
21314 if (token->type == CPP_PRAGMA)
21316 cp_parser_pragma (parser, pragma_member);
21317 break;
21320 /* Otherwise, the next construction must be a
21321 member-declaration. */
21322 cp_parser_member_declaration (parser);
21327 /* Parse a member-declaration.
21329 member-declaration:
21330 decl-specifier-seq [opt] member-declarator-list [opt] ;
21331 function-definition ; [opt]
21332 :: [opt] nested-name-specifier template [opt] unqualified-id ;
21333 using-declaration
21334 template-declaration
21335 alias-declaration
21337 member-declarator-list:
21338 member-declarator
21339 member-declarator-list , member-declarator
21341 member-declarator:
21342 declarator pure-specifier [opt]
21343 declarator constant-initializer [opt]
21344 identifier [opt] : constant-expression
21346 GNU Extensions:
21348 member-declaration:
21349 __extension__ member-declaration
21351 member-declarator:
21352 declarator attributes [opt] pure-specifier [opt]
21353 declarator attributes [opt] constant-initializer [opt]
21354 identifier [opt] attributes [opt] : constant-expression
21356 C++0x Extensions:
21358 member-declaration:
21359 static_assert-declaration */
21361 static void
21362 cp_parser_member_declaration (cp_parser* parser)
21364 cp_decl_specifier_seq decl_specifiers;
21365 tree prefix_attributes;
21366 tree decl;
21367 int declares_class_or_enum;
21368 bool friend_p;
21369 cp_token *token = NULL;
21370 cp_token *decl_spec_token_start = NULL;
21371 cp_token *initializer_token_start = NULL;
21372 int saved_pedantic;
21373 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21375 /* Check for the `__extension__' keyword. */
21376 if (cp_parser_extension_opt (parser, &saved_pedantic))
21378 /* Recurse. */
21379 cp_parser_member_declaration (parser);
21380 /* Restore the old value of the PEDANTIC flag. */
21381 pedantic = saved_pedantic;
21383 return;
21386 /* Check for a template-declaration. */
21387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21389 /* An explicit specialization here is an error condition, and we
21390 expect the specialization handler to detect and report this. */
21391 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
21392 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
21393 cp_parser_explicit_specialization (parser);
21394 else
21395 cp_parser_template_declaration (parser, /*member_p=*/true);
21397 return;
21400 /* Check for a using-declaration. */
21401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21403 if (cxx_dialect < cxx11)
21405 /* Parse the using-declaration. */
21406 cp_parser_using_declaration (parser,
21407 /*access_declaration_p=*/false);
21408 return;
21410 else
21412 tree decl;
21413 bool alias_decl_expected;
21414 cp_parser_parse_tentatively (parser);
21415 decl = cp_parser_alias_declaration (parser);
21416 /* Note that if we actually see the '=' token after the
21417 identifier, cp_parser_alias_declaration commits the
21418 tentative parse. In that case, we really expect an
21419 alias-declaration. Otherwise, we expect a using
21420 declaration. */
21421 alias_decl_expected =
21422 !cp_parser_uncommitted_to_tentative_parse_p (parser);
21423 cp_parser_parse_definitely (parser);
21425 if (alias_decl_expected)
21426 finish_member_declaration (decl);
21427 else
21428 cp_parser_using_declaration (parser,
21429 /*access_declaration_p=*/false);
21430 return;
21434 /* Check for @defs. */
21435 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
21437 tree ivar, member;
21438 tree ivar_chains = cp_parser_objc_defs_expression (parser);
21439 ivar = ivar_chains;
21440 while (ivar)
21442 member = ivar;
21443 ivar = TREE_CHAIN (member);
21444 TREE_CHAIN (member) = NULL_TREE;
21445 finish_member_declaration (member);
21447 return;
21450 /* If the next token is `static_assert' we have a static assertion. */
21451 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
21453 cp_parser_static_assert (parser, /*member_p=*/true);
21454 return;
21457 /* Tentatively parse for a template since we may have a concept
21458 introduction. */
21459 cp_parser_parse_tentatively (parser);
21460 cp_parser_template_declaration (parser, /*member_p=*/true);
21461 if (cp_parser_parse_definitely (parser))
21462 return;
21464 parser->colon_corrects_to_scope_p = false;
21466 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
21467 goto out;
21469 /* Parse the decl-specifier-seq. */
21470 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21471 cp_parser_decl_specifier_seq (parser,
21472 CP_PARSER_FLAGS_OPTIONAL,
21473 &decl_specifiers,
21474 &declares_class_or_enum);
21475 /* Check for an invalid type-name. */
21476 if (!decl_specifiers.any_type_specifiers_p
21477 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21478 goto out;
21479 /* If there is no declarator, then the decl-specifier-seq should
21480 specify a type. */
21481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21483 /* If there was no decl-specifier-seq, and the next token is a
21484 `;', then we have something like:
21486 struct S { ; };
21488 [class.mem]
21490 Each member-declaration shall declare at least one member
21491 name of the class. */
21492 if (!decl_specifiers.any_specifiers_p)
21494 cp_token *token = cp_lexer_peek_token (parser->lexer);
21495 if (!in_system_header_at (token->location))
21496 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
21498 else
21500 tree type;
21502 /* See if this declaration is a friend. */
21503 friend_p = cp_parser_friend_p (&decl_specifiers);
21504 /* If there were decl-specifiers, check to see if there was
21505 a class-declaration. */
21506 type = check_tag_decl (&decl_specifiers,
21507 /*explicit_type_instantiation_p=*/false);
21508 /* Nested classes have already been added to the class, but
21509 a `friend' needs to be explicitly registered. */
21510 if (friend_p)
21512 /* If the `friend' keyword was present, the friend must
21513 be introduced with a class-key. */
21514 if (!declares_class_or_enum && cxx_dialect < cxx11)
21515 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
21516 "in C++03 a class-key must be used "
21517 "when declaring a friend");
21518 /* In this case:
21520 template <typename T> struct A {
21521 friend struct A<T>::B;
21524 A<T>::B will be represented by a TYPENAME_TYPE, and
21525 therefore not recognized by check_tag_decl. */
21526 if (!type)
21528 type = decl_specifiers.type;
21529 if (type && TREE_CODE (type) == TYPE_DECL)
21530 type = TREE_TYPE (type);
21532 if (!type || !TYPE_P (type))
21533 error_at (decl_spec_token_start->location,
21534 "friend declaration does not name a class or "
21535 "function");
21536 else
21537 make_friend_class (current_class_type, type,
21538 /*complain=*/true);
21540 /* If there is no TYPE, an error message will already have
21541 been issued. */
21542 else if (!type || type == error_mark_node)
21544 /* An anonymous aggregate has to be handled specially; such
21545 a declaration really declares a data member (with a
21546 particular type), as opposed to a nested class. */
21547 else if (ANON_AGGR_TYPE_P (type))
21549 /* C++11 9.5/6. */
21550 if (decl_specifiers.storage_class != sc_none)
21551 error_at (decl_spec_token_start->location,
21552 "a storage class on an anonymous aggregate "
21553 "in class scope is not allowed");
21555 /* Remove constructors and such from TYPE, now that we
21556 know it is an anonymous aggregate. */
21557 fixup_anonymous_aggr (type);
21558 /* And make the corresponding data member. */
21559 decl = build_decl (decl_spec_token_start->location,
21560 FIELD_DECL, NULL_TREE, type);
21561 /* Add it to the class. */
21562 finish_member_declaration (decl);
21564 else
21565 cp_parser_check_access_in_redeclaration
21566 (TYPE_NAME (type),
21567 decl_spec_token_start->location);
21570 else
21572 bool assume_semicolon = false;
21574 /* Clear attributes from the decl_specifiers but keep them
21575 around as prefix attributes that apply them to the entity
21576 being declared. */
21577 prefix_attributes = decl_specifiers.attributes;
21578 decl_specifiers.attributes = NULL_TREE;
21580 /* See if these declarations will be friends. */
21581 friend_p = cp_parser_friend_p (&decl_specifiers);
21583 /* Keep going until we hit the `;' at the end of the
21584 declaration. */
21585 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21587 tree attributes = NULL_TREE;
21588 tree first_attribute;
21590 /* Peek at the next token. */
21591 token = cp_lexer_peek_token (parser->lexer);
21593 /* Check for a bitfield declaration. */
21594 if (token->type == CPP_COLON
21595 || (token->type == CPP_NAME
21596 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
21597 == CPP_COLON))
21599 tree identifier;
21600 tree width;
21602 /* Get the name of the bitfield. Note that we cannot just
21603 check TOKEN here because it may have been invalidated by
21604 the call to cp_lexer_peek_nth_token above. */
21605 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
21606 identifier = cp_parser_identifier (parser);
21607 else
21608 identifier = NULL_TREE;
21610 /* Consume the `:' token. */
21611 cp_lexer_consume_token (parser->lexer);
21612 /* Get the width of the bitfield. */
21613 width
21614 = cp_parser_constant_expression (parser);
21616 /* Look for attributes that apply to the bitfield. */
21617 attributes = cp_parser_attributes_opt (parser);
21618 /* Remember which attributes are prefix attributes and
21619 which are not. */
21620 first_attribute = attributes;
21621 /* Combine the attributes. */
21622 attributes = chainon (prefix_attributes, attributes);
21624 /* Create the bitfield declaration. */
21625 decl = grokbitfield (identifier
21626 ? make_id_declarator (NULL_TREE,
21627 identifier,
21628 sfk_none)
21629 : NULL,
21630 &decl_specifiers,
21631 width,
21632 attributes);
21634 else
21636 cp_declarator *declarator;
21637 tree initializer;
21638 tree asm_specification;
21639 int ctor_dtor_or_conv_p;
21641 /* Parse the declarator. */
21642 declarator
21643 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21644 &ctor_dtor_or_conv_p,
21645 /*parenthesized_p=*/NULL,
21646 /*member_p=*/true,
21647 friend_p);
21649 /* If something went wrong parsing the declarator, make sure
21650 that we at least consume some tokens. */
21651 if (declarator == cp_error_declarator)
21653 /* Skip to the end of the statement. */
21654 cp_parser_skip_to_end_of_statement (parser);
21655 /* If the next token is not a semicolon, that is
21656 probably because we just skipped over the body of
21657 a function. So, we consume a semicolon if
21658 present, but do not issue an error message if it
21659 is not present. */
21660 if (cp_lexer_next_token_is (parser->lexer,
21661 CPP_SEMICOLON))
21662 cp_lexer_consume_token (parser->lexer);
21663 goto out;
21666 if (declares_class_or_enum & 2)
21667 cp_parser_check_for_definition_in_return_type
21668 (declarator, decl_specifiers.type,
21669 decl_specifiers.locations[ds_type_spec]);
21671 /* Look for an asm-specification. */
21672 asm_specification = cp_parser_asm_specification_opt (parser);
21673 /* Look for attributes that apply to the declaration. */
21674 attributes = cp_parser_attributes_opt (parser);
21675 /* Remember which attributes are prefix attributes and
21676 which are not. */
21677 first_attribute = attributes;
21678 /* Combine the attributes. */
21679 attributes = chainon (prefix_attributes, attributes);
21681 /* If it's an `=', then we have a constant-initializer or a
21682 pure-specifier. It is not correct to parse the
21683 initializer before registering the member declaration
21684 since the member declaration should be in scope while
21685 its initializer is processed. However, the rest of the
21686 front end does not yet provide an interface that allows
21687 us to handle this correctly. */
21688 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21690 /* In [class.mem]:
21692 A pure-specifier shall be used only in the declaration of
21693 a virtual function.
21695 A member-declarator can contain a constant-initializer
21696 only if it declares a static member of integral or
21697 enumeration type.
21699 Therefore, if the DECLARATOR is for a function, we look
21700 for a pure-specifier; otherwise, we look for a
21701 constant-initializer. When we call `grokfield', it will
21702 perform more stringent semantics checks. */
21703 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21704 if (function_declarator_p (declarator)
21705 || (decl_specifiers.type
21706 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21707 && declarator->kind == cdk_id
21708 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21709 == FUNCTION_TYPE)))
21710 initializer = cp_parser_pure_specifier (parser);
21711 else if (decl_specifiers.storage_class != sc_static)
21712 initializer = cp_parser_save_nsdmi (parser);
21713 else if (cxx_dialect >= cxx11)
21715 bool nonconst;
21716 /* Don't require a constant rvalue in C++11, since we
21717 might want a reference constant. We'll enforce
21718 constancy later. */
21719 cp_lexer_consume_token (parser->lexer);
21720 /* Parse the initializer. */
21721 initializer = cp_parser_initializer_clause (parser,
21722 &nonconst);
21724 else
21725 /* Parse the initializer. */
21726 initializer = cp_parser_constant_initializer (parser);
21728 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21729 && !function_declarator_p (declarator))
21731 bool x;
21732 if (decl_specifiers.storage_class != sc_static)
21733 initializer = cp_parser_save_nsdmi (parser);
21734 else
21735 initializer = cp_parser_initializer (parser, &x, &x);
21737 /* Otherwise, there is no initializer. */
21738 else
21739 initializer = NULL_TREE;
21741 /* See if we are probably looking at a function
21742 definition. We are certainly not looking at a
21743 member-declarator. Calling `grokfield' has
21744 side-effects, so we must not do it unless we are sure
21745 that we are looking at a member-declarator. */
21746 if (cp_parser_token_starts_function_definition_p
21747 (cp_lexer_peek_token (parser->lexer)))
21749 /* The grammar does not allow a pure-specifier to be
21750 used when a member function is defined. (It is
21751 possible that this fact is an oversight in the
21752 standard, since a pure function may be defined
21753 outside of the class-specifier. */
21754 if (initializer && initializer_token_start)
21755 error_at (initializer_token_start->location,
21756 "pure-specifier on function-definition");
21757 decl = cp_parser_save_member_function_body (parser,
21758 &decl_specifiers,
21759 declarator,
21760 attributes);
21761 if (parser->fully_implicit_function_template_p)
21762 decl = finish_fully_implicit_template (parser, decl);
21763 /* If the member was not a friend, declare it here. */
21764 if (!friend_p)
21765 finish_member_declaration (decl);
21767 /* Peek at the next token. */
21768 token = cp_lexer_peek_token (parser->lexer);
21769 /* If the next token is a semicolon, consume it. */
21770 if (token->type == CPP_SEMICOLON)
21771 cp_lexer_consume_token (parser->lexer);
21772 goto out;
21774 else
21775 if (declarator->kind == cdk_function)
21776 declarator->id_loc = token->location;
21777 /* Create the declaration. */
21778 decl = grokfield (declarator, &decl_specifiers,
21779 initializer, /*init_const_expr_p=*/true,
21780 asm_specification, attributes);
21781 if (parser->fully_implicit_function_template_p)
21783 if (friend_p)
21784 finish_fully_implicit_template (parser, 0);
21785 else
21786 decl = finish_fully_implicit_template (parser, decl);
21790 cp_finalize_omp_declare_simd (parser, decl);
21792 /* Reset PREFIX_ATTRIBUTES. */
21793 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21794 attributes = TREE_CHAIN (attributes);
21795 if (attributes)
21796 TREE_CHAIN (attributes) = NULL_TREE;
21798 /* If there is any qualification still in effect, clear it
21799 now; we will be starting fresh with the next declarator. */
21800 parser->scope = NULL_TREE;
21801 parser->qualifying_scope = NULL_TREE;
21802 parser->object_scope = NULL_TREE;
21803 /* If it's a `,', then there are more declarators. */
21804 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21806 cp_lexer_consume_token (parser->lexer);
21807 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21809 cp_token *token = cp_lexer_previous_token (parser->lexer);
21810 error_at (token->location,
21811 "stray %<,%> at end of member declaration");
21814 /* If the next token isn't a `;', then we have a parse error. */
21815 else if (cp_lexer_next_token_is_not (parser->lexer,
21816 CPP_SEMICOLON))
21818 /* The next token might be a ways away from where the
21819 actual semicolon is missing. Find the previous token
21820 and use that for our error position. */
21821 cp_token *token = cp_lexer_previous_token (parser->lexer);
21822 error_at (token->location ? token->location : input_location,
21823 "expected %<;%> at end of member declaration");
21825 /* Assume that the user meant to provide a semicolon. If
21826 we were to cp_parser_skip_to_end_of_statement, we might
21827 skip to a semicolon inside a member function definition
21828 and issue nonsensical error messages. */
21829 assume_semicolon = true;
21832 if (decl)
21834 /* Add DECL to the list of members. */
21835 if (!friend_p
21836 /* Explicitly include, eg, NSDMIs, for better error
21837 recovery (c++/58650). */
21838 || !DECL_DECLARES_FUNCTION_P (decl))
21839 finish_member_declaration (decl);
21841 if (TREE_CODE (decl) == FUNCTION_DECL)
21842 cp_parser_save_default_args (parser, decl);
21843 else if (TREE_CODE (decl) == FIELD_DECL
21844 && !DECL_C_BIT_FIELD (decl)
21845 && DECL_INITIAL (decl))
21846 /* Add DECL to the queue of NSDMI to be parsed later. */
21847 vec_safe_push (unparsed_nsdmis, decl);
21850 if (assume_semicolon)
21851 goto out;
21855 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21856 out:
21857 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21860 /* Parse a pure-specifier.
21862 pure-specifier:
21865 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21866 Otherwise, ERROR_MARK_NODE is returned. */
21868 static tree
21869 cp_parser_pure_specifier (cp_parser* parser)
21871 cp_token *token;
21873 /* Look for the `=' token. */
21874 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21875 return error_mark_node;
21876 /* Look for the `0' token. */
21877 token = cp_lexer_peek_token (parser->lexer);
21879 if (token->type == CPP_EOF
21880 || token->type == CPP_PRAGMA_EOL)
21881 return error_mark_node;
21883 cp_lexer_consume_token (parser->lexer);
21885 /* Accept = default or = delete in c++0x mode. */
21886 if (token->keyword == RID_DEFAULT
21887 || token->keyword == RID_DELETE)
21889 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21890 return token->u.value;
21893 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21894 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21896 cp_parser_error (parser,
21897 "invalid pure specifier (only %<= 0%> is allowed)");
21898 cp_parser_skip_to_end_of_statement (parser);
21899 return error_mark_node;
21901 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21903 error_at (token->location, "templates may not be %<virtual%>");
21904 return error_mark_node;
21907 return integer_zero_node;
21910 /* Parse a constant-initializer.
21912 constant-initializer:
21913 = constant-expression
21915 Returns a representation of the constant-expression. */
21917 static tree
21918 cp_parser_constant_initializer (cp_parser* parser)
21920 /* Look for the `=' token. */
21921 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21922 return error_mark_node;
21924 /* It is invalid to write:
21926 struct S { static const int i = { 7 }; };
21929 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21931 cp_parser_error (parser,
21932 "a brace-enclosed initializer is not allowed here");
21933 /* Consume the opening brace. */
21934 cp_lexer_consume_token (parser->lexer);
21935 /* Skip the initializer. */
21936 cp_parser_skip_to_closing_brace (parser);
21937 /* Look for the trailing `}'. */
21938 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21940 return error_mark_node;
21943 return cp_parser_constant_expression (parser);
21946 /* Derived classes [gram.class.derived] */
21948 /* Parse a base-clause.
21950 base-clause:
21951 : base-specifier-list
21953 base-specifier-list:
21954 base-specifier ... [opt]
21955 base-specifier-list , base-specifier ... [opt]
21957 Returns a TREE_LIST representing the base-classes, in the order in
21958 which they were declared. The representation of each node is as
21959 described by cp_parser_base_specifier.
21961 In the case that no bases are specified, this function will return
21962 NULL_TREE, not ERROR_MARK_NODE. */
21964 static tree
21965 cp_parser_base_clause (cp_parser* parser)
21967 tree bases = NULL_TREE;
21969 /* Look for the `:' that begins the list. */
21970 cp_parser_require (parser, CPP_COLON, RT_COLON);
21972 /* Scan the base-specifier-list. */
21973 while (true)
21975 cp_token *token;
21976 tree base;
21977 bool pack_expansion_p = false;
21979 /* Look for the base-specifier. */
21980 base = cp_parser_base_specifier (parser);
21981 /* Look for the (optional) ellipsis. */
21982 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21984 /* Consume the `...'. */
21985 cp_lexer_consume_token (parser->lexer);
21987 pack_expansion_p = true;
21990 /* Add BASE to the front of the list. */
21991 if (base && base != error_mark_node)
21993 if (pack_expansion_p)
21994 /* Make this a pack expansion type. */
21995 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21997 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21999 TREE_CHAIN (base) = bases;
22000 bases = base;
22003 /* Peek at the next token. */
22004 token = cp_lexer_peek_token (parser->lexer);
22005 /* If it's not a comma, then the list is complete. */
22006 if (token->type != CPP_COMMA)
22007 break;
22008 /* Consume the `,'. */
22009 cp_lexer_consume_token (parser->lexer);
22012 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22013 base class had a qualified name. However, the next name that
22014 appears is certainly not qualified. */
22015 parser->scope = NULL_TREE;
22016 parser->qualifying_scope = NULL_TREE;
22017 parser->object_scope = NULL_TREE;
22019 return nreverse (bases);
22022 /* Parse a base-specifier.
22024 base-specifier:
22025 :: [opt] nested-name-specifier [opt] class-name
22026 virtual access-specifier [opt] :: [opt] nested-name-specifier
22027 [opt] class-name
22028 access-specifier virtual [opt] :: [opt] nested-name-specifier
22029 [opt] class-name
22031 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22032 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22033 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22034 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22036 static tree
22037 cp_parser_base_specifier (cp_parser* parser)
22039 cp_token *token;
22040 bool done = false;
22041 bool virtual_p = false;
22042 bool duplicate_virtual_error_issued_p = false;
22043 bool duplicate_access_error_issued_p = false;
22044 bool class_scope_p, template_p;
22045 tree access = access_default_node;
22046 tree type;
22048 /* Process the optional `virtual' and `access-specifier'. */
22049 while (!done)
22051 /* Peek at the next token. */
22052 token = cp_lexer_peek_token (parser->lexer);
22053 /* Process `virtual'. */
22054 switch (token->keyword)
22056 case RID_VIRTUAL:
22057 /* If `virtual' appears more than once, issue an error. */
22058 if (virtual_p && !duplicate_virtual_error_issued_p)
22060 cp_parser_error (parser,
22061 "%<virtual%> specified more than once in base-specified");
22062 duplicate_virtual_error_issued_p = true;
22065 virtual_p = true;
22067 /* Consume the `virtual' token. */
22068 cp_lexer_consume_token (parser->lexer);
22070 break;
22072 case RID_PUBLIC:
22073 case RID_PROTECTED:
22074 case RID_PRIVATE:
22075 /* If more than one access specifier appears, issue an
22076 error. */
22077 if (access != access_default_node
22078 && !duplicate_access_error_issued_p)
22080 cp_parser_error (parser,
22081 "more than one access specifier in base-specified");
22082 duplicate_access_error_issued_p = true;
22085 access = ridpointers[(int) token->keyword];
22087 /* Consume the access-specifier. */
22088 cp_lexer_consume_token (parser->lexer);
22090 break;
22092 default:
22093 done = true;
22094 break;
22097 /* It is not uncommon to see programs mechanically, erroneously, use
22098 the 'typename' keyword to denote (dependent) qualified types
22099 as base classes. */
22100 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22102 token = cp_lexer_peek_token (parser->lexer);
22103 if (!processing_template_decl)
22104 error_at (token->location,
22105 "keyword %<typename%> not allowed outside of templates");
22106 else
22107 error_at (token->location,
22108 "keyword %<typename%> not allowed in this context "
22109 "(the base class is implicitly a type)");
22110 cp_lexer_consume_token (parser->lexer);
22113 /* Look for the optional `::' operator. */
22114 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22115 /* Look for the nested-name-specifier. The simplest way to
22116 implement:
22118 [temp.res]
22120 The keyword `typename' is not permitted in a base-specifier or
22121 mem-initializer; in these contexts a qualified name that
22122 depends on a template-parameter is implicitly assumed to be a
22123 type name.
22125 is to pretend that we have seen the `typename' keyword at this
22126 point. */
22127 cp_parser_nested_name_specifier_opt (parser,
22128 /*typename_keyword_p=*/true,
22129 /*check_dependency_p=*/true,
22130 typename_type,
22131 /*is_declaration=*/true);
22132 /* If the base class is given by a qualified name, assume that names
22133 we see are type names or templates, as appropriate. */
22134 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22135 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22137 if (!parser->scope
22138 && cp_lexer_next_token_is_decltype (parser->lexer))
22139 /* DR 950 allows decltype as a base-specifier. */
22140 type = cp_parser_decltype (parser);
22141 else
22143 /* Otherwise, look for the class-name. */
22144 type = cp_parser_class_name (parser,
22145 class_scope_p,
22146 template_p,
22147 typename_type,
22148 /*check_dependency_p=*/true,
22149 /*class_head_p=*/false,
22150 /*is_declaration=*/true);
22151 type = TREE_TYPE (type);
22154 if (type == error_mark_node)
22155 return error_mark_node;
22157 return finish_base_specifier (type, access, virtual_p);
22160 /* Exception handling [gram.exception] */
22162 /* Parse an (optional) noexcept-specification.
22164 noexcept-specification:
22165 noexcept ( constant-expression ) [opt]
22167 If no noexcept-specification is present, returns NULL_TREE.
22168 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22169 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22170 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22171 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22172 in which case a boolean condition is returned instead. */
22174 static tree
22175 cp_parser_noexcept_specification_opt (cp_parser* parser,
22176 bool require_constexpr,
22177 bool* consumed_expr,
22178 bool return_cond)
22180 cp_token *token;
22181 const char *saved_message;
22183 /* Peek at the next token. */
22184 token = cp_lexer_peek_token (parser->lexer);
22186 /* Is it a noexcept-specification? */
22187 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22189 tree expr;
22190 cp_lexer_consume_token (parser->lexer);
22192 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22194 cp_lexer_consume_token (parser->lexer);
22196 if (require_constexpr)
22198 /* Types may not be defined in an exception-specification. */
22199 saved_message = parser->type_definition_forbidden_message;
22200 parser->type_definition_forbidden_message
22201 = G_("types may not be defined in an exception-specification");
22203 expr = cp_parser_constant_expression (parser);
22205 /* Restore the saved message. */
22206 parser->type_definition_forbidden_message = saved_message;
22208 else
22210 expr = cp_parser_expression (parser);
22211 *consumed_expr = true;
22214 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22216 else
22218 expr = boolean_true_node;
22219 if (!require_constexpr)
22220 *consumed_expr = false;
22223 /* We cannot build a noexcept-spec right away because this will check
22224 that expr is a constexpr. */
22225 if (!return_cond)
22226 return build_noexcept_spec (expr, tf_warning_or_error);
22227 else
22228 return expr;
22230 else
22231 return NULL_TREE;
22234 /* Parse an (optional) exception-specification.
22236 exception-specification:
22237 throw ( type-id-list [opt] )
22239 Returns a TREE_LIST representing the exception-specification. The
22240 TREE_VALUE of each node is a type. */
22242 static tree
22243 cp_parser_exception_specification_opt (cp_parser* parser)
22245 cp_token *token;
22246 tree type_id_list;
22247 const char *saved_message;
22249 /* Peek at the next token. */
22250 token = cp_lexer_peek_token (parser->lexer);
22252 /* Is it a noexcept-specification? */
22253 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
22254 false);
22255 if (type_id_list != NULL_TREE)
22256 return type_id_list;
22258 /* If it's not `throw', then there's no exception-specification. */
22259 if (!cp_parser_is_keyword (token, RID_THROW))
22260 return NULL_TREE;
22262 #if 0
22263 /* Enable this once a lot of code has transitioned to noexcept? */
22264 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
22265 warning (OPT_Wdeprecated, "dynamic exception specifications are "
22266 "deprecated in C++0x; use %<noexcept%> instead");
22267 #endif
22269 /* Consume the `throw'. */
22270 cp_lexer_consume_token (parser->lexer);
22272 /* Look for the `('. */
22273 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22275 /* Peek at the next token. */
22276 token = cp_lexer_peek_token (parser->lexer);
22277 /* If it's not a `)', then there is a type-id-list. */
22278 if (token->type != CPP_CLOSE_PAREN)
22280 /* Types may not be defined in an exception-specification. */
22281 saved_message = parser->type_definition_forbidden_message;
22282 parser->type_definition_forbidden_message
22283 = G_("types may not be defined in an exception-specification");
22284 /* Parse the type-id-list. */
22285 type_id_list = cp_parser_type_id_list (parser);
22286 /* Restore the saved message. */
22287 parser->type_definition_forbidden_message = saved_message;
22289 else
22290 type_id_list = empty_except_spec;
22292 /* Look for the `)'. */
22293 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22295 return type_id_list;
22298 /* Parse an (optional) type-id-list.
22300 type-id-list:
22301 type-id ... [opt]
22302 type-id-list , type-id ... [opt]
22304 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
22305 in the order that the types were presented. */
22307 static tree
22308 cp_parser_type_id_list (cp_parser* parser)
22310 tree types = NULL_TREE;
22312 while (true)
22314 cp_token *token;
22315 tree type;
22317 /* Get the next type-id. */
22318 type = cp_parser_type_id (parser);
22319 /* Parse the optional ellipsis. */
22320 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22322 /* Consume the `...'. */
22323 cp_lexer_consume_token (parser->lexer);
22325 /* Turn the type into a pack expansion expression. */
22326 type = make_pack_expansion (type);
22328 /* Add it to the list. */
22329 types = add_exception_specifier (types, type, /*complain=*/1);
22330 /* Peek at the next token. */
22331 token = cp_lexer_peek_token (parser->lexer);
22332 /* If it is not a `,', we are done. */
22333 if (token->type != CPP_COMMA)
22334 break;
22335 /* Consume the `,'. */
22336 cp_lexer_consume_token (parser->lexer);
22339 return nreverse (types);
22342 /* Parse a try-block.
22344 try-block:
22345 try compound-statement handler-seq */
22347 static tree
22348 cp_parser_try_block (cp_parser* parser)
22350 tree try_block;
22352 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
22353 if (parser->in_function_body
22354 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
22355 error ("%<try%> in %<constexpr%> function");
22357 try_block = begin_try_block ();
22358 cp_parser_compound_statement (parser, NULL, true, false);
22359 finish_try_block (try_block);
22360 cp_parser_handler_seq (parser);
22361 finish_handler_sequence (try_block);
22363 return try_block;
22366 /* Parse a function-try-block.
22368 function-try-block:
22369 try ctor-initializer [opt] function-body handler-seq */
22371 static bool
22372 cp_parser_function_try_block (cp_parser* parser)
22374 tree compound_stmt;
22375 tree try_block;
22376 bool ctor_initializer_p;
22378 /* Look for the `try' keyword. */
22379 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
22380 return false;
22381 /* Let the rest of the front end know where we are. */
22382 try_block = begin_function_try_block (&compound_stmt);
22383 /* Parse the function-body. */
22384 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22385 (parser, /*in_function_try_block=*/true);
22386 /* We're done with the `try' part. */
22387 finish_function_try_block (try_block);
22388 /* Parse the handlers. */
22389 cp_parser_handler_seq (parser);
22390 /* We're done with the handlers. */
22391 finish_function_handler_sequence (try_block, compound_stmt);
22393 return ctor_initializer_p;
22396 /* Parse a handler-seq.
22398 handler-seq:
22399 handler handler-seq [opt] */
22401 static void
22402 cp_parser_handler_seq (cp_parser* parser)
22404 while (true)
22406 cp_token *token;
22408 /* Parse the handler. */
22409 cp_parser_handler (parser);
22410 /* Peek at the next token. */
22411 token = cp_lexer_peek_token (parser->lexer);
22412 /* If it's not `catch' then there are no more handlers. */
22413 if (!cp_parser_is_keyword (token, RID_CATCH))
22414 break;
22418 /* Parse a handler.
22420 handler:
22421 catch ( exception-declaration ) compound-statement */
22423 static void
22424 cp_parser_handler (cp_parser* parser)
22426 tree handler;
22427 tree declaration;
22429 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
22430 handler = begin_handler ();
22431 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22432 declaration = cp_parser_exception_declaration (parser);
22433 finish_handler_parms (declaration, handler);
22434 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22435 cp_parser_compound_statement (parser, NULL, false, false);
22436 finish_handler (handler);
22439 /* Parse an exception-declaration.
22441 exception-declaration:
22442 type-specifier-seq declarator
22443 type-specifier-seq abstract-declarator
22444 type-specifier-seq
22447 Returns a VAR_DECL for the declaration, or NULL_TREE if the
22448 ellipsis variant is used. */
22450 static tree
22451 cp_parser_exception_declaration (cp_parser* parser)
22453 cp_decl_specifier_seq type_specifiers;
22454 cp_declarator *declarator;
22455 const char *saved_message;
22457 /* If it's an ellipsis, it's easy to handle. */
22458 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22460 /* Consume the `...' token. */
22461 cp_lexer_consume_token (parser->lexer);
22462 return NULL_TREE;
22465 /* Types may not be defined in exception-declarations. */
22466 saved_message = parser->type_definition_forbidden_message;
22467 parser->type_definition_forbidden_message
22468 = G_("types may not be defined in exception-declarations");
22470 /* Parse the type-specifier-seq. */
22471 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22472 /*is_trailing_return=*/false,
22473 &type_specifiers);
22474 /* If it's a `)', then there is no declarator. */
22475 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22476 declarator = NULL;
22477 else
22478 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
22479 /*ctor_dtor_or_conv_p=*/NULL,
22480 /*parenthesized_p=*/NULL,
22481 /*member_p=*/false,
22482 /*friend_p=*/false);
22484 /* Restore the saved message. */
22485 parser->type_definition_forbidden_message = saved_message;
22487 if (!type_specifiers.any_specifiers_p)
22488 return error_mark_node;
22490 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
22493 /* Parse a throw-expression.
22495 throw-expression:
22496 throw assignment-expression [opt]
22498 Returns a THROW_EXPR representing the throw-expression. */
22500 static tree
22501 cp_parser_throw_expression (cp_parser* parser)
22503 tree expression;
22504 cp_token* token;
22506 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
22507 token = cp_lexer_peek_token (parser->lexer);
22508 /* Figure out whether or not there is an assignment-expression
22509 following the "throw" keyword. */
22510 if (token->type == CPP_COMMA
22511 || token->type == CPP_SEMICOLON
22512 || token->type == CPP_CLOSE_PAREN
22513 || token->type == CPP_CLOSE_SQUARE
22514 || token->type == CPP_CLOSE_BRACE
22515 || token->type == CPP_COLON)
22516 expression = NULL_TREE;
22517 else
22518 expression = cp_parser_assignment_expression (parser);
22520 return build_throw (expression);
22523 /* GNU Extensions */
22525 /* Parse an (optional) asm-specification.
22527 asm-specification:
22528 asm ( string-literal )
22530 If the asm-specification is present, returns a STRING_CST
22531 corresponding to the string-literal. Otherwise, returns
22532 NULL_TREE. */
22534 static tree
22535 cp_parser_asm_specification_opt (cp_parser* parser)
22537 cp_token *token;
22538 tree asm_specification;
22540 /* Peek at the next token. */
22541 token = cp_lexer_peek_token (parser->lexer);
22542 /* If the next token isn't the `asm' keyword, then there's no
22543 asm-specification. */
22544 if (!cp_parser_is_keyword (token, RID_ASM))
22545 return NULL_TREE;
22547 /* Consume the `asm' token. */
22548 cp_lexer_consume_token (parser->lexer);
22549 /* Look for the `('. */
22550 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22552 /* Look for the string-literal. */
22553 asm_specification = cp_parser_string_literal (parser, false, false);
22555 /* Look for the `)'. */
22556 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22558 return asm_specification;
22561 /* Parse an asm-operand-list.
22563 asm-operand-list:
22564 asm-operand
22565 asm-operand-list , asm-operand
22567 asm-operand:
22568 string-literal ( expression )
22569 [ string-literal ] string-literal ( expression )
22571 Returns a TREE_LIST representing the operands. The TREE_VALUE of
22572 each node is the expression. The TREE_PURPOSE is itself a
22573 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
22574 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
22575 is a STRING_CST for the string literal before the parenthesis. Returns
22576 ERROR_MARK_NODE if any of the operands are invalid. */
22578 static tree
22579 cp_parser_asm_operand_list (cp_parser* parser)
22581 tree asm_operands = NULL_TREE;
22582 bool invalid_operands = false;
22584 while (true)
22586 tree string_literal;
22587 tree expression;
22588 tree name;
22590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22592 /* Consume the `[' token. */
22593 cp_lexer_consume_token (parser->lexer);
22594 /* Read the operand name. */
22595 name = cp_parser_identifier (parser);
22596 if (name != error_mark_node)
22597 name = build_string (IDENTIFIER_LENGTH (name),
22598 IDENTIFIER_POINTER (name));
22599 /* Look for the closing `]'. */
22600 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22602 else
22603 name = NULL_TREE;
22604 /* Look for the string-literal. */
22605 string_literal = cp_parser_string_literal (parser, false, false);
22607 /* Look for the `('. */
22608 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22609 /* Parse the expression. */
22610 expression = cp_parser_expression (parser);
22611 /* Look for the `)'. */
22612 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22614 if (name == error_mark_node
22615 || string_literal == error_mark_node
22616 || expression == error_mark_node)
22617 invalid_operands = true;
22619 /* Add this operand to the list. */
22620 asm_operands = tree_cons (build_tree_list (name, string_literal),
22621 expression,
22622 asm_operands);
22623 /* If the next token is not a `,', there are no more
22624 operands. */
22625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22626 break;
22627 /* Consume the `,'. */
22628 cp_lexer_consume_token (parser->lexer);
22631 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22634 /* Parse an asm-clobber-list.
22636 asm-clobber-list:
22637 string-literal
22638 asm-clobber-list , string-literal
22640 Returns a TREE_LIST, indicating the clobbers in the order that they
22641 appeared. The TREE_VALUE of each node is a STRING_CST. */
22643 static tree
22644 cp_parser_asm_clobber_list (cp_parser* parser)
22646 tree clobbers = NULL_TREE;
22648 while (true)
22650 tree string_literal;
22652 /* Look for the string literal. */
22653 string_literal = cp_parser_string_literal (parser, false, false);
22654 /* Add it to the list. */
22655 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22656 /* If the next token is not a `,', then the list is
22657 complete. */
22658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22659 break;
22660 /* Consume the `,' token. */
22661 cp_lexer_consume_token (parser->lexer);
22664 return clobbers;
22667 /* Parse an asm-label-list.
22669 asm-label-list:
22670 identifier
22671 asm-label-list , identifier
22673 Returns a TREE_LIST, indicating the labels in the order that they
22674 appeared. The TREE_VALUE of each node is a label. */
22676 static tree
22677 cp_parser_asm_label_list (cp_parser* parser)
22679 tree labels = NULL_TREE;
22681 while (true)
22683 tree identifier, label, name;
22685 /* Look for the identifier. */
22686 identifier = cp_parser_identifier (parser);
22687 if (!error_operand_p (identifier))
22689 label = lookup_label (identifier);
22690 if (TREE_CODE (label) == LABEL_DECL)
22692 TREE_USED (label) = 1;
22693 check_goto (label);
22694 name = build_string (IDENTIFIER_LENGTH (identifier),
22695 IDENTIFIER_POINTER (identifier));
22696 labels = tree_cons (name, label, labels);
22699 /* If the next token is not a `,', then the list is
22700 complete. */
22701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22702 break;
22703 /* Consume the `,' token. */
22704 cp_lexer_consume_token (parser->lexer);
22707 return nreverse (labels);
22710 /* Return TRUE iff the next tokens in the stream are possibly the
22711 beginning of a GNU extension attribute. */
22713 static bool
22714 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22716 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22719 /* Return TRUE iff the next tokens in the stream are possibly the
22720 beginning of a standard C++-11 attribute specifier. */
22722 static bool
22723 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22725 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22728 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22729 beginning of a standard C++-11 attribute specifier. */
22731 static bool
22732 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22734 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22736 return (cxx_dialect >= cxx11
22737 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22738 || (token->type == CPP_OPEN_SQUARE
22739 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22740 && token->type == CPP_OPEN_SQUARE)));
22743 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22744 beginning of a GNU extension attribute. */
22746 static bool
22747 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22749 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22751 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22754 /* Return true iff the next tokens can be the beginning of either a
22755 GNU attribute list, or a standard C++11 attribute sequence. */
22757 static bool
22758 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22760 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22761 || cp_next_tokens_can_be_std_attribute_p (parser));
22764 /* Return true iff the next Nth tokens can be the beginning of either
22765 a GNU attribute list, or a standard C++11 attribute sequence. */
22767 static bool
22768 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22770 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22771 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22774 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22775 of GNU attributes, or return NULL. */
22777 static tree
22778 cp_parser_attributes_opt (cp_parser *parser)
22780 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22781 return cp_parser_gnu_attributes_opt (parser);
22782 return cp_parser_std_attribute_spec_seq (parser);
22785 #define CILK_SIMD_FN_CLAUSE_MASK \
22786 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22787 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22788 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22789 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22790 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22792 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22793 vector [(<clauses>)] */
22795 static void
22796 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22798 bool first_p = parser->cilk_simd_fn_info == NULL;
22799 cp_token *token = v_token;
22800 if (first_p)
22802 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22803 parser->cilk_simd_fn_info->error_seen = false;
22804 parser->cilk_simd_fn_info->fndecl_seen = false;
22805 parser->cilk_simd_fn_info->tokens = vNULL;
22807 int paren_scope = 0;
22808 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22810 cp_lexer_consume_token (parser->lexer);
22811 v_token = cp_lexer_peek_token (parser->lexer);
22812 paren_scope++;
22814 while (paren_scope > 0)
22816 token = cp_lexer_peek_token (parser->lexer);
22817 if (token->type == CPP_OPEN_PAREN)
22818 paren_scope++;
22819 else if (token->type == CPP_CLOSE_PAREN)
22820 paren_scope--;
22821 /* Do not push the last ')' */
22822 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22823 cp_lexer_consume_token (parser->lexer);
22826 token->type = CPP_PRAGMA_EOL;
22827 parser->lexer->next_token = token;
22828 cp_lexer_consume_token (parser->lexer);
22830 struct cp_token_cache *cp
22831 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22832 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22835 /* Parse an (optional) series of attributes.
22837 attributes:
22838 attributes attribute
22840 attribute:
22841 __attribute__ (( attribute-list [opt] ))
22843 The return value is as for cp_parser_gnu_attribute_list. */
22845 static tree
22846 cp_parser_gnu_attributes_opt (cp_parser* parser)
22848 tree attributes = NULL_TREE;
22850 while (true)
22852 cp_token *token;
22853 tree attribute_list;
22854 bool ok = true;
22856 /* Peek at the next token. */
22857 token = cp_lexer_peek_token (parser->lexer);
22858 /* If it's not `__attribute__', then we're done. */
22859 if (token->keyword != RID_ATTRIBUTE)
22860 break;
22862 /* Consume the `__attribute__' keyword. */
22863 cp_lexer_consume_token (parser->lexer);
22864 /* Look for the two `(' tokens. */
22865 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22868 /* Peek at the next token. */
22869 token = cp_lexer_peek_token (parser->lexer);
22870 if (token->type != CPP_CLOSE_PAREN)
22871 /* Parse the attribute-list. */
22872 attribute_list = cp_parser_gnu_attribute_list (parser);
22873 else
22874 /* If the next token is a `)', then there is no attribute
22875 list. */
22876 attribute_list = NULL;
22878 /* Look for the two `)' tokens. */
22879 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22880 ok = false;
22881 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22882 ok = false;
22883 if (!ok)
22884 cp_parser_skip_to_end_of_statement (parser);
22886 /* Add these new attributes to the list. */
22887 attributes = chainon (attributes, attribute_list);
22890 return attributes;
22893 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22894 "__vector" or "__vector__." */
22896 static inline bool
22897 is_cilkplus_vector_p (tree name)
22899 if (flag_cilkplus && is_attribute_p ("vector", name))
22900 return true;
22901 return false;
22904 /* Parse a GNU attribute-list.
22906 attribute-list:
22907 attribute
22908 attribute-list , attribute
22910 attribute:
22911 identifier
22912 identifier ( identifier )
22913 identifier ( identifier , expression-list )
22914 identifier ( expression-list )
22916 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22917 to an attribute. The TREE_PURPOSE of each node is the identifier
22918 indicating which attribute is in use. The TREE_VALUE represents
22919 the arguments, if any. */
22921 static tree
22922 cp_parser_gnu_attribute_list (cp_parser* parser)
22924 tree attribute_list = NULL_TREE;
22925 bool save_translate_strings_p = parser->translate_strings_p;
22927 parser->translate_strings_p = false;
22928 while (true)
22930 cp_token *token;
22931 tree identifier;
22932 tree attribute;
22934 /* Look for the identifier. We also allow keywords here; for
22935 example `__attribute__ ((const))' is legal. */
22936 token = cp_lexer_peek_token (parser->lexer);
22937 if (token->type == CPP_NAME
22938 || token->type == CPP_KEYWORD)
22940 tree arguments = NULL_TREE;
22942 /* Consume the token, but save it since we need it for the
22943 SIMD enabled function parsing. */
22944 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22946 /* Save away the identifier that indicates which attribute
22947 this is. */
22948 identifier = (token->type == CPP_KEYWORD)
22949 /* For keywords, use the canonical spelling, not the
22950 parsed identifier. */
22951 ? ridpointers[(int) token->keyword]
22952 : id_token->u.value;
22954 attribute = build_tree_list (identifier, NULL_TREE);
22956 /* Peek at the next token. */
22957 token = cp_lexer_peek_token (parser->lexer);
22958 /* If it's an `(', then parse the attribute arguments. */
22959 if (token->type == CPP_OPEN_PAREN)
22961 vec<tree, va_gc> *vec;
22962 int attr_flag = (attribute_takes_identifier_p (identifier)
22963 ? id_attr : normal_attr);
22964 if (is_cilkplus_vector_p (identifier))
22966 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22967 continue;
22969 else
22970 vec = cp_parser_parenthesized_expression_list
22971 (parser, attr_flag, /*cast_p=*/false,
22972 /*allow_expansion_p=*/false,
22973 /*non_constant_p=*/NULL);
22974 if (vec == NULL)
22975 arguments = error_mark_node;
22976 else
22978 arguments = build_tree_list_vec (vec);
22979 release_tree_vector (vec);
22981 /* Save the arguments away. */
22982 TREE_VALUE (attribute) = arguments;
22984 else if (is_cilkplus_vector_p (identifier))
22986 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22987 continue;
22990 if (arguments != error_mark_node)
22992 /* Add this attribute to the list. */
22993 TREE_CHAIN (attribute) = attribute_list;
22994 attribute_list = attribute;
22997 token = cp_lexer_peek_token (parser->lexer);
22999 /* Now, look for more attributes. If the next token isn't a
23000 `,', we're done. */
23001 if (token->type != CPP_COMMA)
23002 break;
23004 /* Consume the comma and keep going. */
23005 cp_lexer_consume_token (parser->lexer);
23007 parser->translate_strings_p = save_translate_strings_p;
23009 /* We built up the list in reverse order. */
23010 return nreverse (attribute_list);
23013 /* Parse a standard C++11 attribute.
23015 The returned representation is a TREE_LIST which TREE_PURPOSE is
23016 the scoped name of the attribute, and the TREE_VALUE is its
23017 arguments list.
23019 Note that the scoped name of the attribute is itself a TREE_LIST
23020 which TREE_PURPOSE is the namespace of the attribute, and
23021 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23022 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23023 and which TREE_PURPOSE is directly the attribute name.
23025 Clients of the attribute code should use get_attribute_namespace
23026 and get_attribute_name to get the actual namespace and name of
23027 attributes, regardless of their being GNU or C++11 attributes.
23029 attribute:
23030 attribute-token attribute-argument-clause [opt]
23032 attribute-token:
23033 identifier
23034 attribute-scoped-token
23036 attribute-scoped-token:
23037 attribute-namespace :: identifier
23039 attribute-namespace:
23040 identifier
23042 attribute-argument-clause:
23043 ( balanced-token-seq )
23045 balanced-token-seq:
23046 balanced-token [opt]
23047 balanced-token-seq balanced-token
23049 balanced-token:
23050 ( balanced-token-seq )
23051 [ balanced-token-seq ]
23052 { balanced-token-seq }. */
23054 static tree
23055 cp_parser_std_attribute (cp_parser *parser)
23057 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23058 cp_token *token;
23060 /* First, parse name of the the attribute, a.k.a
23061 attribute-token. */
23063 token = cp_lexer_peek_token (parser->lexer);
23064 if (token->type == CPP_NAME)
23065 attr_id = token->u.value;
23066 else if (token->type == CPP_KEYWORD)
23067 attr_id = ridpointers[(int) token->keyword];
23068 else if (token->flags & NAMED_OP)
23069 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23071 if (attr_id == NULL_TREE)
23072 return NULL_TREE;
23074 cp_lexer_consume_token (parser->lexer);
23076 token = cp_lexer_peek_token (parser->lexer);
23077 if (token->type == CPP_SCOPE)
23079 /* We are seeing a scoped attribute token. */
23081 cp_lexer_consume_token (parser->lexer);
23082 attr_ns = attr_id;
23084 token = cp_lexer_consume_token (parser->lexer);
23085 if (token->type == CPP_NAME)
23086 attr_id = token->u.value;
23087 else if (token->type == CPP_KEYWORD)
23088 attr_id = ridpointers[(int) token->keyword];
23089 else
23091 error_at (token->location,
23092 "expected an identifier for the attribute name");
23093 return error_mark_node;
23095 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23096 NULL_TREE);
23097 token = cp_lexer_peek_token (parser->lexer);
23099 else
23101 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23102 NULL_TREE);
23103 /* C++11 noreturn attribute is equivalent to GNU's. */
23104 if (is_attribute_p ("noreturn", attr_id))
23105 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23106 /* C++14 deprecated attribute is equivalent to GNU's. */
23107 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23109 if (cxx_dialect == cxx11)
23110 pedwarn (token->location, OPT_Wpedantic,
23111 "%<deprecated%> is a C++14 feature;"
23112 " use %<gnu::deprecated%>");
23113 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23117 /* Now parse the optional argument clause of the attribute. */
23119 if (token->type != CPP_OPEN_PAREN)
23120 return attribute;
23123 vec<tree, va_gc> *vec;
23124 int attr_flag = normal_attr;
23126 if (attr_ns == get_identifier ("gnu")
23127 && attribute_takes_identifier_p (attr_id))
23128 /* A GNU attribute that takes an identifier in parameter. */
23129 attr_flag = id_attr;
23131 vec = cp_parser_parenthesized_expression_list
23132 (parser, attr_flag, /*cast_p=*/false,
23133 /*allow_expansion_p=*/true,
23134 /*non_constant_p=*/NULL);
23135 if (vec == NULL)
23136 arguments = error_mark_node;
23137 else
23139 arguments = build_tree_list_vec (vec);
23140 release_tree_vector (vec);
23143 if (arguments == error_mark_node)
23144 attribute = error_mark_node;
23145 else
23146 TREE_VALUE (attribute) = arguments;
23149 return attribute;
23152 /* Parse a list of standard C++-11 attributes.
23154 attribute-list:
23155 attribute [opt]
23156 attribute-list , attribute[opt]
23157 attribute ...
23158 attribute-list , attribute ...
23161 static tree
23162 cp_parser_std_attribute_list (cp_parser *parser)
23164 tree attributes = NULL_TREE, attribute = NULL_TREE;
23165 cp_token *token = NULL;
23167 while (true)
23169 attribute = cp_parser_std_attribute (parser);
23170 if (attribute == error_mark_node)
23171 break;
23172 if (attribute != NULL_TREE)
23174 TREE_CHAIN (attribute) = attributes;
23175 attributes = attribute;
23177 token = cp_lexer_peek_token (parser->lexer);
23178 if (token->type != CPP_COMMA)
23179 break;
23180 cp_lexer_consume_token (parser->lexer);
23182 attributes = nreverse (attributes);
23183 return attributes;
23186 /* Parse a standard C++-11 attribute specifier.
23188 attribute-specifier:
23189 [ [ attribute-list ] ]
23190 alignment-specifier
23192 alignment-specifier:
23193 alignas ( type-id ... [opt] )
23194 alignas ( alignment-expression ... [opt] ). */
23196 static tree
23197 cp_parser_std_attribute_spec (cp_parser *parser)
23199 tree attributes = NULL_TREE;
23200 cp_token *token = cp_lexer_peek_token (parser->lexer);
23202 if (token->type == CPP_OPEN_SQUARE
23203 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
23205 cp_lexer_consume_token (parser->lexer);
23206 cp_lexer_consume_token (parser->lexer);
23208 attributes = cp_parser_std_attribute_list (parser);
23210 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
23211 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23212 cp_parser_skip_to_end_of_statement (parser);
23213 else
23214 /* Warn about parsing c++11 attribute in non-c++1 mode, only
23215 when we are sure that we have actually parsed them. */
23216 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23218 else
23220 tree alignas_expr;
23222 /* Look for an alignment-specifier. */
23224 token = cp_lexer_peek_token (parser->lexer);
23226 if (token->type != CPP_KEYWORD
23227 || token->keyword != RID_ALIGNAS)
23228 return NULL_TREE;
23230 cp_lexer_consume_token (parser->lexer);
23231 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23233 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
23235 cp_parser_error (parser, "expected %<(%>");
23236 return error_mark_node;
23239 cp_parser_parse_tentatively (parser);
23240 alignas_expr = cp_parser_type_id (parser);
23242 if (!cp_parser_parse_definitely (parser))
23244 gcc_assert (alignas_expr == error_mark_node
23245 || alignas_expr == NULL_TREE);
23247 alignas_expr =
23248 cp_parser_assignment_expression (parser);
23249 if (alignas_expr == error_mark_node)
23250 cp_parser_skip_to_end_of_statement (parser);
23251 if (alignas_expr == NULL_TREE
23252 || alignas_expr == error_mark_node)
23253 return alignas_expr;
23256 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
23258 cp_parser_error (parser, "expected %<)%>");
23259 return error_mark_node;
23262 alignas_expr = cxx_alignas_expr (alignas_expr);
23264 /* Build the C++-11 representation of an 'aligned'
23265 attribute. */
23266 attributes =
23267 build_tree_list (build_tree_list (get_identifier ("gnu"),
23268 get_identifier ("aligned")),
23269 build_tree_list (NULL_TREE, alignas_expr));
23272 return attributes;
23275 /* Parse a standard C++-11 attribute-specifier-seq.
23277 attribute-specifier-seq:
23278 attribute-specifier-seq [opt] attribute-specifier
23281 static tree
23282 cp_parser_std_attribute_spec_seq (cp_parser *parser)
23284 tree attr_specs = NULL;
23286 while (true)
23288 tree attr_spec = cp_parser_std_attribute_spec (parser);
23289 if (attr_spec == NULL_TREE)
23290 break;
23291 if (attr_spec == error_mark_node)
23292 return error_mark_node;
23294 TREE_CHAIN (attr_spec) = attr_specs;
23295 attr_specs = attr_spec;
23298 attr_specs = nreverse (attr_specs);
23299 return attr_specs;
23302 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
23303 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
23304 current value of the PEDANTIC flag, regardless of whether or not
23305 the `__extension__' keyword is present. The caller is responsible
23306 for restoring the value of the PEDANTIC flag. */
23308 static bool
23309 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
23311 /* Save the old value of the PEDANTIC flag. */
23312 *saved_pedantic = pedantic;
23314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
23316 /* Consume the `__extension__' token. */
23317 cp_lexer_consume_token (parser->lexer);
23318 /* We're not being pedantic while the `__extension__' keyword is
23319 in effect. */
23320 pedantic = 0;
23322 return true;
23325 return false;
23328 /* Parse a label declaration.
23330 label-declaration:
23331 __label__ label-declarator-seq ;
23333 label-declarator-seq:
23334 identifier , label-declarator-seq
23335 identifier */
23337 static void
23338 cp_parser_label_declaration (cp_parser* parser)
23340 /* Look for the `__label__' keyword. */
23341 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
23343 while (true)
23345 tree identifier;
23347 /* Look for an identifier. */
23348 identifier = cp_parser_identifier (parser);
23349 /* If we failed, stop. */
23350 if (identifier == error_mark_node)
23351 break;
23352 /* Declare it as a label. */
23353 finish_label_decl (identifier);
23354 /* If the next token is a `;', stop. */
23355 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23356 break;
23357 /* Look for the `,' separating the label declarations. */
23358 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
23361 /* Look for the final `;'. */
23362 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23365 // -------------------------------------------------------------------------- //
23366 // Requires Clause
23368 // Parse a requires clause.
23370 // requires-clause:
23371 // 'requires' logical-or-expression
23373 // The required logical-or-expression must be a constant expression. Note
23374 // that we don't check that the expression is constepxr here. We defer until
23375 // we analyze constraints and then, we only check atomic constraints.
23376 static tree
23377 cp_parser_requires_clause (cp_parser *parser)
23379 // Parse the requires clause so that it is not automatically folded.
23380 ++processing_template_decl;
23381 tree expr =
23382 cp_parser_binary_expression (parser, false, false, PREC_NOT_OPERATOR, NULL);
23383 --processing_template_decl;
23384 return expr;
23387 // Optionally parse a requires clause:
23389 // requires-clause:
23390 // 'requires' logical-or-expression
23392 // The required logical-or-expression must be a constant expression.
23393 static tree
23394 cp_parser_requires_clause_opt (cp_parser *parser)
23396 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23397 return NULL_TREE;
23398 cp_lexer_consume_token (parser->lexer);
23399 return cp_parser_requires_clause (parser);
23403 /*---------------------------------------------------------------------------
23404 Requires expressions
23405 ---------------------------------------------------------------------------*/
23407 /* Parse a requires expression
23409 requirement-expression:
23410 'requires' requirement-parameter-list [opt] requirement-body */
23411 static tree
23412 cp_parser_requires_expression (cp_parser *parser)
23414 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
23415 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
23417 /* A requires-expression shall appear only within a concept
23418 definition or a requires-clause.
23420 TODO: Implement this diagnostic correctly. */
23421 if (!processing_template_decl)
23423 error_at (loc, "a requires expression cannot appear outside a template");
23424 cp_parser_skip_to_end_of_statement (parser);
23425 return error_mark_node;
23428 /* Local parameters are delared as variables within the scope
23429 of the expression. They are not visible past the end of
23430 the expression. */
23431 struct scope_sentinel
23433 scope_sentinel ()
23435 begin_scope (sk_block, NULL_TREE);
23438 ~scope_sentinel ()
23440 pop_bindings_and_leave_scope ();
23442 } s;
23444 /* Parse the optional parameter list. */
23445 tree parms = NULL_TREE;
23446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23448 parms = cp_parser_requirement_parameter_list (parser);
23449 if (parms == error_mark_node)
23450 return error_mark_node;
23453 /* Parse the requirement body. */
23454 tree reqs = cp_parser_requirement_body (parser);
23455 if (reqs == error_mark_node)
23456 return error_mark_node;
23458 return finish_requires_expr (parms, reqs);
23461 /* Parse a parameterized requirement.
23463 requirement-parameter-list:
23464 '(' parameter-declaration-clause ')' */
23465 static tree
23466 cp_parser_requirement_parameter_list (cp_parser *parser)
23468 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23469 return error_mark_node;
23471 tree parms = cp_parser_parameter_declaration_clause (parser);
23472 if (parms == error_mark_node)
23473 return error_mark_node;
23475 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23476 return error_mark_node;
23478 return parms;
23481 /* Parse the body of a requirement.
23483 requirement-body:
23484 '{' requirement-list '}' */
23485 static tree
23486 cp_parser_requirement_body (cp_parser *parser)
23488 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23489 return error_mark_node;
23491 tree reqs = cp_parser_requirement_list (parser);
23493 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23494 return error_mark_node;
23496 return reqs;
23499 /* Parse a list of requirements.
23501 requirement-list:
23502 requirement
23503 requirement-list ';' requirement[opt] */
23504 static tree
23505 cp_parser_requirement_list (cp_parser *parser)
23507 tree result = NULL_TREE;
23508 while (true)
23510 tree req = cp_parser_requirement (parser);
23511 if (req == error_mark_node)
23512 return error_mark_node;
23514 result = tree_cons (NULL_TREE, req, result);
23516 /* If we see a semi-colon, consume it. */
23517 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23518 cp_lexer_consume_token (parser->lexer);
23520 /* Stop processing at the end of the list. */
23521 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23522 break;
23525 /* Reverse the order of requirements so they are analyzed in
23526 declaration order. */
23527 return nreverse (result);
23530 /* Parse a syntactic requirement or type requirement.
23532 requirement:
23533 simple-requirement
23534 compound-requirement
23535 type-requirement
23536 nested-requirement */
23537 static tree
23538 cp_parser_requirement (cp_parser *parser)
23540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23541 return cp_parser_compound_requirement (parser);
23542 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23543 return cp_parser_type_requirement (parser);
23544 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23545 return cp_parser_nested_requirement (parser);
23546 else
23547 return cp_parser_simple_requirement (parser);
23550 /* Parse a simple requirement.
23552 simple-requirement:
23553 expression ';' */
23554 static tree
23555 cp_parser_simple_requirement (cp_parser *parser)
23557 tree expr = cp_parser_expression (parser, NULL, false, false);
23558 if (!expr || expr == error_mark_node)
23559 return error_mark_node;
23561 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23562 return error_mark_node;
23564 return finish_simple_requirement (expr);
23567 /* Parse a type requirement
23569 type-requirement
23570 nested-name-specifier [opt] type-name ';' */
23571 static tree
23572 cp_parser_type_requirement (cp_parser *parser)
23574 cp_lexer_consume_token (parser->lexer);
23576 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
23577 cp_parser_nested_name_specifier_opt (parser,
23578 /*typename_keyword_p=*/true,
23579 /*check_dependency_p=*/false,
23580 /*type_p=*/true,
23581 /*is_declaration=*/false);
23583 tree type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
23584 if (type == error_mark_node)
23585 return error_mark_node;
23587 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23588 return error_mark_node;
23590 return finish_type_requirement (type);
23593 /* Parse a compound requirement
23595 compound-requirement:
23596 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
23597 static tree
23598 cp_parser_compound_requirement (cp_parser *parser)
23600 /* Parse an expression enclosed in '{ }'s. */
23601 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23602 return error_mark_node;
23604 tree expr = cp_parser_expression (parser, NULL, false, false);
23605 if (!expr || expr == error_mark_node)
23606 return error_mark_node;
23608 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23609 return error_mark_node;
23611 /* Parse the optional noexcept. */
23612 bool noexcept_p = false;
23613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
23615 cp_lexer_consume_token (parser->lexer);
23616 noexcept_p = true;
23619 /* Parse the optional trailing return type. */
23620 tree type = NULL_TREE;
23621 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
23623 cp_lexer_consume_token (parser->lexer);
23624 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
23625 parser->in_result_type_constraint_p = true;
23626 type = cp_parser_trailing_type_id (parser);
23627 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
23628 if (type == error_mark_node)
23629 return error_mark_node;
23632 return finish_compound_requirement (expr, type, noexcept_p);
23635 /* Parse a nested requirement. This is the same as a requires clause.
23637 nested-requirement:
23638 requires-clause */
23639 static tree
23640 cp_parser_nested_requirement (cp_parser *parser)
23642 cp_lexer_consume_token (parser->lexer);
23643 tree req = cp_parser_requires_clause (parser);
23644 if (req == error_mark_node)
23645 return error_mark_node;
23646 return finish_nested_requirement (req);
23649 /* Support Functions */
23651 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
23652 NAME should have one of the representations used for an
23653 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
23654 is returned. If PARSER->SCOPE is a dependent type, then a
23655 SCOPE_REF is returned.
23657 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
23658 returned; the name was already resolved when the TEMPLATE_ID_EXPR
23659 was formed. Abstractly, such entities should not be passed to this
23660 function, because they do not need to be looked up, but it is
23661 simpler to check for this special case here, rather than at the
23662 call-sites.
23664 In cases not explicitly covered above, this function returns a
23665 DECL, OVERLOAD, or baselink representing the result of the lookup.
23666 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
23667 is returned.
23669 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
23670 (e.g., "struct") that was used. In that case bindings that do not
23671 refer to types are ignored.
23673 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
23674 ignored.
23676 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
23677 are ignored.
23679 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
23680 types.
23682 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
23683 TREE_LIST of candidates if name-lookup results in an ambiguity, and
23684 NULL_TREE otherwise. */
23686 static tree
23687 cp_parser_lookup_name (cp_parser *parser, tree name,
23688 enum tag_types tag_type,
23689 bool is_template,
23690 bool is_namespace,
23691 bool check_dependency,
23692 tree *ambiguous_decls,
23693 location_t name_location)
23695 tree decl;
23696 tree object_type = parser->context->object_type;
23698 /* Assume that the lookup will be unambiguous. */
23699 if (ambiguous_decls)
23700 *ambiguous_decls = NULL_TREE;
23702 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
23703 no longer valid. Note that if we are parsing tentatively, and
23704 the parse fails, OBJECT_TYPE will be automatically restored. */
23705 parser->context->object_type = NULL_TREE;
23707 if (name == error_mark_node)
23708 return error_mark_node;
23710 /* A template-id has already been resolved; there is no lookup to
23711 do. */
23712 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
23713 return name;
23714 if (BASELINK_P (name))
23716 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
23717 == TEMPLATE_ID_EXPR);
23718 return name;
23721 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
23722 it should already have been checked to make sure that the name
23723 used matches the type being destroyed. */
23724 if (TREE_CODE (name) == BIT_NOT_EXPR)
23726 tree type;
23728 /* Figure out to which type this destructor applies. */
23729 if (parser->scope)
23730 type = parser->scope;
23731 else if (object_type)
23732 type = object_type;
23733 else
23734 type = current_class_type;
23735 /* If that's not a class type, there is no destructor. */
23736 if (!type || !CLASS_TYPE_P (type))
23737 return error_mark_node;
23738 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
23739 lazily_declare_fn (sfk_destructor, type);
23740 if (!CLASSTYPE_DESTRUCTORS (type))
23741 return error_mark_node;
23742 /* If it was a class type, return the destructor. */
23743 return CLASSTYPE_DESTRUCTORS (type);
23746 /* By this point, the NAME should be an ordinary identifier. If
23747 the id-expression was a qualified name, the qualifying scope is
23748 stored in PARSER->SCOPE at this point. */
23749 gcc_assert (identifier_p (name));
23751 /* Perform the lookup. */
23752 if (parser->scope)
23754 bool dependent_p;
23756 if (parser->scope == error_mark_node)
23757 return error_mark_node;
23759 /* If the SCOPE is dependent, the lookup must be deferred until
23760 the template is instantiated -- unless we are explicitly
23761 looking up names in uninstantiated templates. Even then, we
23762 cannot look up the name if the scope is not a class type; it
23763 might, for example, be a template type parameter. */
23764 dependent_p = (TYPE_P (parser->scope)
23765 && dependent_scope_p (parser->scope));
23766 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
23767 && dependent_p)
23768 /* Defer lookup. */
23769 decl = error_mark_node;
23770 else
23772 tree pushed_scope = NULL_TREE;
23774 /* If PARSER->SCOPE is a dependent type, then it must be a
23775 class type, and we must not be checking dependencies;
23776 otherwise, we would have processed this lookup above. So
23777 that PARSER->SCOPE is not considered a dependent base by
23778 lookup_member, we must enter the scope here. */
23779 if (dependent_p)
23780 pushed_scope = push_scope (parser->scope);
23782 /* If the PARSER->SCOPE is a template specialization, it
23783 may be instantiated during name lookup. In that case,
23784 errors may be issued. Even if we rollback the current
23785 tentative parse, those errors are valid. */
23786 decl = lookup_qualified_name (parser->scope, name,
23787 tag_type != none_type,
23788 /*complain=*/true);
23790 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
23791 lookup result and the nested-name-specifier nominates a class C:
23792 * if the name specified after the nested-name-specifier, when
23793 looked up in C, is the injected-class-name of C (Clause 9), or
23794 * if the name specified after the nested-name-specifier is the
23795 same as the identifier or the simple-template-id's template-
23796 name in the last component of the nested-name-specifier,
23797 the name is instead considered to name the constructor of
23798 class C. [ Note: for example, the constructor is not an
23799 acceptable lookup result in an elaborated-type-specifier so
23800 the constructor would not be used in place of the
23801 injected-class-name. --end note ] Such a constructor name
23802 shall be used only in the declarator-id of a declaration that
23803 names a constructor or in a using-declaration. */
23804 if (tag_type == none_type
23805 && DECL_SELF_REFERENCE_P (decl)
23806 && same_type_p (DECL_CONTEXT (decl), parser->scope))
23807 decl = lookup_qualified_name (parser->scope, ctor_identifier,
23808 tag_type != none_type,
23809 /*complain=*/true);
23811 /* If we have a single function from a using decl, pull it out. */
23812 if (TREE_CODE (decl) == OVERLOAD
23813 && !really_overloaded_fn (decl))
23814 decl = OVL_FUNCTION (decl);
23816 if (pushed_scope)
23817 pop_scope (pushed_scope);
23820 /* If the scope is a dependent type and either we deferred lookup or
23821 we did lookup but didn't find the name, rememeber the name. */
23822 if (decl == error_mark_node && TYPE_P (parser->scope)
23823 && dependent_type_p (parser->scope))
23825 if (tag_type)
23827 tree type;
23829 /* The resolution to Core Issue 180 says that `struct
23830 A::B' should be considered a type-name, even if `A'
23831 is dependent. */
23832 type = make_typename_type (parser->scope, name, tag_type,
23833 /*complain=*/tf_error);
23834 if (type != error_mark_node)
23835 decl = TYPE_NAME (type);
23837 else if (is_template
23838 && (cp_parser_next_token_ends_template_argument_p (parser)
23839 || cp_lexer_next_token_is (parser->lexer,
23840 CPP_CLOSE_PAREN)))
23841 decl = make_unbound_class_template (parser->scope,
23842 name, NULL_TREE,
23843 /*complain=*/tf_error);
23844 else
23845 decl = build_qualified_name (/*type=*/NULL_TREE,
23846 parser->scope, name,
23847 is_template);
23849 parser->qualifying_scope = parser->scope;
23850 parser->object_scope = NULL_TREE;
23852 else if (object_type)
23854 /* Look up the name in the scope of the OBJECT_TYPE, unless the
23855 OBJECT_TYPE is not a class. */
23856 if (CLASS_TYPE_P (object_type))
23857 /* If the OBJECT_TYPE is a template specialization, it may
23858 be instantiated during name lookup. In that case, errors
23859 may be issued. Even if we rollback the current tentative
23860 parse, those errors are valid. */
23861 decl = lookup_member (object_type,
23862 name,
23863 /*protect=*/0,
23864 tag_type != none_type,
23865 tf_warning_or_error);
23866 else
23867 decl = NULL_TREE;
23869 if (!decl)
23870 /* Look it up in the enclosing context. */
23871 decl = lookup_name_real (name, tag_type != none_type,
23872 /*nonclass=*/0,
23873 /*block_p=*/true, is_namespace, 0);
23874 parser->object_scope = object_type;
23875 parser->qualifying_scope = NULL_TREE;
23877 else
23879 decl = lookup_name_real (name, tag_type != none_type,
23880 /*nonclass=*/0,
23881 /*block_p=*/true, is_namespace, 0);
23882 parser->qualifying_scope = NULL_TREE;
23883 parser->object_scope = NULL_TREE;
23886 /* If the lookup failed, let our caller know. */
23887 if (!decl || decl == error_mark_node)
23888 return error_mark_node;
23890 /* Pull out the template from an injected-class-name (or multiple). */
23891 if (is_template)
23892 decl = maybe_get_template_decl_from_type_decl (decl);
23894 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23895 if (TREE_CODE (decl) == TREE_LIST)
23897 if (ambiguous_decls)
23898 *ambiguous_decls = decl;
23899 /* The error message we have to print is too complicated for
23900 cp_parser_error, so we incorporate its actions directly. */
23901 if (!cp_parser_simulate_error (parser))
23903 error_at (name_location, "reference to %qD is ambiguous",
23904 name);
23905 print_candidates (decl);
23907 return error_mark_node;
23910 gcc_assert (DECL_P (decl)
23911 || TREE_CODE (decl) == OVERLOAD
23912 || TREE_CODE (decl) == SCOPE_REF
23913 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23914 || BASELINK_P (decl));
23916 /* If we have resolved the name of a member declaration, check to
23917 see if the declaration is accessible. When the name resolves to
23918 set of overloaded functions, accessibility is checked when
23919 overload resolution is done.
23921 During an explicit instantiation, access is not checked at all,
23922 as per [temp.explicit]. */
23923 if (DECL_P (decl))
23924 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23926 maybe_record_typedef_use (decl);
23928 return decl;
23931 /* Like cp_parser_lookup_name, but for use in the typical case where
23932 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23933 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23935 static tree
23936 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23938 return cp_parser_lookup_name (parser, name,
23939 none_type,
23940 /*is_template=*/false,
23941 /*is_namespace=*/false,
23942 /*check_dependency=*/true,
23943 /*ambiguous_decls=*/NULL,
23944 location);
23947 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23948 the current context, return the TYPE_DECL. If TAG_NAME_P is
23949 true, the DECL indicates the class being defined in a class-head,
23950 or declared in an elaborated-type-specifier.
23952 Otherwise, return DECL. */
23954 static tree
23955 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23957 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23958 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23960 struct A {
23961 template <typename T> struct B;
23964 template <typename T> struct A::B {};
23966 Similarly, in an elaborated-type-specifier:
23968 namespace N { struct X{}; }
23970 struct A {
23971 template <typename T> friend struct N::X;
23974 However, if the DECL refers to a class type, and we are in
23975 the scope of the class, then the name lookup automatically
23976 finds the TYPE_DECL created by build_self_reference rather
23977 than a TEMPLATE_DECL. For example, in:
23979 template <class T> struct S {
23980 S s;
23983 there is no need to handle such case. */
23985 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23986 return DECL_TEMPLATE_RESULT (decl);
23988 return decl;
23991 /* If too many, or too few, template-parameter lists apply to the
23992 declarator, issue an error message. Returns TRUE if all went well,
23993 and FALSE otherwise. */
23995 static bool
23996 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23997 cp_declarator *declarator,
23998 location_t declarator_location)
24000 switch (declarator->kind)
24002 case cdk_id:
24004 unsigned num_templates = 0;
24005 tree scope = declarator->u.id.qualifying_scope;
24007 if (scope)
24008 num_templates = num_template_headers_for_class (scope);
24009 else if (TREE_CODE (declarator->u.id.unqualified_name)
24010 == TEMPLATE_ID_EXPR)
24011 /* If the DECLARATOR has the form `X<y>' then it uses one
24012 additional level of template parameters. */
24013 ++num_templates;
24015 return cp_parser_check_template_parameters
24016 (parser, num_templates, declarator_location, declarator);
24019 case cdk_function:
24020 case cdk_array:
24021 case cdk_pointer:
24022 case cdk_reference:
24023 case cdk_ptrmem:
24024 return (cp_parser_check_declarator_template_parameters
24025 (parser, declarator->declarator, declarator_location));
24027 case cdk_error:
24028 return true;
24030 default:
24031 gcc_unreachable ();
24033 return false;
24036 /* NUM_TEMPLATES were used in the current declaration. If that is
24037 invalid, return FALSE and issue an error messages. Otherwise,
24038 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24039 declarator and we can print more accurate diagnostics. */
24041 static bool
24042 cp_parser_check_template_parameters (cp_parser* parser,
24043 unsigned num_templates,
24044 location_t location,
24045 cp_declarator *declarator)
24047 /* If there are the same number of template classes and parameter
24048 lists, that's OK. */
24049 if (parser->num_template_parameter_lists == num_templates)
24050 return true;
24051 /* If there are more, but only one more, then we are referring to a
24052 member template. That's OK too. */
24053 if (parser->num_template_parameter_lists == num_templates + 1)
24054 return true;
24055 /* If there are more template classes than parameter lists, we have
24056 something like:
24058 template <class T> void S<T>::R<T>::f (); */
24059 if (parser->num_template_parameter_lists < num_templates)
24061 if (declarator && !current_function_decl)
24062 error_at (location, "specializing member %<%T::%E%> "
24063 "requires %<template<>%> syntax",
24064 declarator->u.id.qualifying_scope,
24065 declarator->u.id.unqualified_name);
24066 else if (declarator)
24067 error_at (location, "invalid declaration of %<%T::%E%>",
24068 declarator->u.id.qualifying_scope,
24069 declarator->u.id.unqualified_name);
24070 else
24071 error_at (location, "too few template-parameter-lists");
24072 return false;
24074 /* Otherwise, there are too many template parameter lists. We have
24075 something like:
24077 template <class T> template <class U> void S::f(); */
24078 error_at (location, "too many template-parameter-lists");
24079 return false;
24082 /* Parse an optional `::' token indicating that the following name is
24083 from the global namespace. If so, PARSER->SCOPE is set to the
24084 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24085 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24086 Returns the new value of PARSER->SCOPE, if the `::' token is
24087 present, and NULL_TREE otherwise. */
24089 static tree
24090 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24092 cp_token *token;
24094 /* Peek at the next token. */
24095 token = cp_lexer_peek_token (parser->lexer);
24096 /* If we're looking at a `::' token then we're starting from the
24097 global namespace, not our current location. */
24098 if (token->type == CPP_SCOPE)
24100 /* Consume the `::' token. */
24101 cp_lexer_consume_token (parser->lexer);
24102 /* Set the SCOPE so that we know where to start the lookup. */
24103 parser->scope = global_namespace;
24104 parser->qualifying_scope = global_namespace;
24105 parser->object_scope = NULL_TREE;
24107 return parser->scope;
24109 else if (!current_scope_valid_p)
24111 parser->scope = NULL_TREE;
24112 parser->qualifying_scope = NULL_TREE;
24113 parser->object_scope = NULL_TREE;
24116 return NULL_TREE;
24119 /* Returns TRUE if the upcoming token sequence is the start of a
24120 constructor declarator. If FRIEND_P is true, the declarator is
24121 preceded by the `friend' specifier. */
24123 static bool
24124 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
24126 bool constructor_p;
24127 bool outside_class_specifier_p;
24128 tree nested_name_specifier;
24129 cp_token *next_token;
24131 /* The common case is that this is not a constructor declarator, so
24132 try to avoid doing lots of work if at all possible. It's not
24133 valid declare a constructor at function scope. */
24134 if (parser->in_function_body)
24135 return false;
24136 /* And only certain tokens can begin a constructor declarator. */
24137 next_token = cp_lexer_peek_token (parser->lexer);
24138 if (next_token->type != CPP_NAME
24139 && next_token->type != CPP_SCOPE
24140 && next_token->type != CPP_NESTED_NAME_SPECIFIER
24141 && next_token->type != CPP_TEMPLATE_ID)
24142 return false;
24144 /* Parse tentatively; we are going to roll back all of the tokens
24145 consumed here. */
24146 cp_parser_parse_tentatively (parser);
24147 /* Assume that we are looking at a constructor declarator. */
24148 constructor_p = true;
24150 /* Look for the optional `::' operator. */
24151 cp_parser_global_scope_opt (parser,
24152 /*current_scope_valid_p=*/false);
24153 /* Look for the nested-name-specifier. */
24154 nested_name_specifier
24155 = (cp_parser_nested_name_specifier_opt (parser,
24156 /*typename_keyword_p=*/false,
24157 /*check_dependency_p=*/false,
24158 /*type_p=*/false,
24159 /*is_declaration=*/false));
24161 outside_class_specifier_p = (!at_class_scope_p ()
24162 || !TYPE_BEING_DEFINED (current_class_type)
24163 || friend_p);
24165 /* Outside of a class-specifier, there must be a
24166 nested-name-specifier. */
24167 if (!nested_name_specifier && outside_class_specifier_p)
24168 constructor_p = false;
24169 else if (nested_name_specifier == error_mark_node)
24170 constructor_p = false;
24172 /* If we have a class scope, this is easy; DR 147 says that S::S always
24173 names the constructor, and no other qualified name could. */
24174 if (constructor_p && nested_name_specifier
24175 && CLASS_TYPE_P (nested_name_specifier))
24177 tree id = cp_parser_unqualified_id (parser,
24178 /*template_keyword_p=*/false,
24179 /*check_dependency_p=*/false,
24180 /*declarator_p=*/true,
24181 /*optional_p=*/false);
24182 if (is_overloaded_fn (id))
24183 id = DECL_NAME (get_first_fn (id));
24184 if (!constructor_name_p (id, nested_name_specifier))
24185 constructor_p = false;
24187 /* If we still think that this might be a constructor-declarator,
24188 look for a class-name. */
24189 else if (constructor_p)
24191 /* If we have:
24193 template <typename T> struct S {
24194 S();
24197 we must recognize that the nested `S' names a class. */
24198 tree type_decl;
24199 type_decl = cp_parser_class_name (parser,
24200 /*typename_keyword_p=*/false,
24201 /*template_keyword_p=*/false,
24202 none_type,
24203 /*check_dependency_p=*/false,
24204 /*class_head_p=*/false,
24205 /*is_declaration=*/false);
24206 /* If there was no class-name, then this is not a constructor.
24207 Otherwise, if we are in a class-specifier and we aren't
24208 handling a friend declaration, check that its type matches
24209 current_class_type (c++/38313). Note: error_mark_node
24210 is left alone for error recovery purposes. */
24211 constructor_p = (!cp_parser_error_occurred (parser)
24212 && (outside_class_specifier_p
24213 || type_decl == error_mark_node
24214 || same_type_p (current_class_type,
24215 TREE_TYPE (type_decl))));
24217 /* If we're still considering a constructor, we have to see a `(',
24218 to begin the parameter-declaration-clause, followed by either a
24219 `)', an `...', or a decl-specifier. We need to check for a
24220 type-specifier to avoid being fooled into thinking that:
24222 S (f) (int);
24224 is a constructor. (It is actually a function named `f' that
24225 takes one parameter (of type `int') and returns a value of type
24226 `S'. */
24227 if (constructor_p
24228 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24229 constructor_p = false;
24231 if (constructor_p
24232 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
24233 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
24234 /* A parameter declaration begins with a decl-specifier,
24235 which is either the "attribute" keyword, a storage class
24236 specifier, or (usually) a type-specifier. */
24237 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
24239 tree type;
24240 tree pushed_scope = NULL_TREE;
24241 unsigned saved_num_template_parameter_lists;
24243 /* Names appearing in the type-specifier should be looked up
24244 in the scope of the class. */
24245 if (current_class_type)
24246 type = NULL_TREE;
24247 else
24249 type = TREE_TYPE (type_decl);
24250 if (TREE_CODE (type) == TYPENAME_TYPE)
24252 type = resolve_typename_type (type,
24253 /*only_current_p=*/false);
24254 if (TREE_CODE (type) == TYPENAME_TYPE)
24256 cp_parser_abort_tentative_parse (parser);
24257 return false;
24260 pushed_scope = push_scope (type);
24263 /* Inside the constructor parameter list, surrounding
24264 template-parameter-lists do not apply. */
24265 saved_num_template_parameter_lists
24266 = parser->num_template_parameter_lists;
24267 parser->num_template_parameter_lists = 0;
24269 /* Look for the type-specifier. */
24270 cp_parser_type_specifier (parser,
24271 CP_PARSER_FLAGS_NONE,
24272 /*decl_specs=*/NULL,
24273 /*is_declarator=*/true,
24274 /*declares_class_or_enum=*/NULL,
24275 /*is_cv_qualifier=*/NULL);
24277 parser->num_template_parameter_lists
24278 = saved_num_template_parameter_lists;
24280 /* Leave the scope of the class. */
24281 if (pushed_scope)
24282 pop_scope (pushed_scope);
24284 constructor_p = !cp_parser_error_occurred (parser);
24288 /* We did not really want to consume any tokens. */
24289 cp_parser_abort_tentative_parse (parser);
24291 return constructor_p;
24294 /* Parse the definition of the function given by the DECL_SPECIFIERS,
24295 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
24296 they must be performed once we are in the scope of the function.
24298 Returns the function defined. */
24300 static tree
24301 cp_parser_function_definition_from_specifiers_and_declarator
24302 (cp_parser* parser,
24303 cp_decl_specifier_seq *decl_specifiers,
24304 tree attributes,
24305 const cp_declarator *declarator)
24307 tree fn;
24308 bool success_p;
24310 /* Begin the function-definition. */
24311 success_p = start_function (decl_specifiers, declarator, attributes);
24313 /* The things we're about to see are not directly qualified by any
24314 template headers we've seen thus far. */
24315 reset_specialization ();
24317 /* If there were names looked up in the decl-specifier-seq that we
24318 did not check, check them now. We must wait until we are in the
24319 scope of the function to perform the checks, since the function
24320 might be a friend. */
24321 perform_deferred_access_checks (tf_warning_or_error);
24323 if (success_p)
24325 cp_finalize_omp_declare_simd (parser, current_function_decl);
24326 parser->omp_declare_simd = NULL;
24329 if (!success_p)
24331 /* Skip the entire function. */
24332 cp_parser_skip_to_end_of_block_or_statement (parser);
24333 fn = error_mark_node;
24335 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
24337 /* Seen already, skip it. An error message has already been output. */
24338 cp_parser_skip_to_end_of_block_or_statement (parser);
24339 fn = current_function_decl;
24340 current_function_decl = NULL_TREE;
24341 /* If this is a function from a class, pop the nested class. */
24342 if (current_class_name)
24343 pop_nested_class ();
24345 else
24347 timevar_id_t tv;
24348 if (DECL_DECLARED_INLINE_P (current_function_decl))
24349 tv = TV_PARSE_INLINE;
24350 else
24351 tv = TV_PARSE_FUNC;
24352 timevar_push (tv);
24353 fn = cp_parser_function_definition_after_declarator (parser,
24354 /*inline_p=*/false);
24355 timevar_pop (tv);
24358 return fn;
24361 /* Parse the part of a function-definition that follows the
24362 declarator. INLINE_P is TRUE iff this function is an inline
24363 function defined within a class-specifier.
24365 Returns the function defined. */
24367 static tree
24368 cp_parser_function_definition_after_declarator (cp_parser* parser,
24369 bool inline_p)
24371 tree fn;
24372 bool ctor_initializer_p = false;
24373 bool saved_in_unbraced_linkage_specification_p;
24374 bool saved_in_function_body;
24375 unsigned saved_num_template_parameter_lists;
24376 cp_token *token;
24377 bool fully_implicit_function_template_p
24378 = parser->fully_implicit_function_template_p;
24379 parser->fully_implicit_function_template_p = false;
24380 tree implicit_template_parms
24381 = parser->implicit_template_parms;
24382 parser->implicit_template_parms = 0;
24383 cp_binding_level* implicit_template_scope
24384 = parser->implicit_template_scope;
24385 parser->implicit_template_scope = 0;
24387 saved_in_function_body = parser->in_function_body;
24388 parser->in_function_body = true;
24389 /* If the next token is `return', then the code may be trying to
24390 make use of the "named return value" extension that G++ used to
24391 support. */
24392 token = cp_lexer_peek_token (parser->lexer);
24393 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
24395 /* Consume the `return' keyword. */
24396 cp_lexer_consume_token (parser->lexer);
24397 /* Look for the identifier that indicates what value is to be
24398 returned. */
24399 cp_parser_identifier (parser);
24400 /* Issue an error message. */
24401 error_at (token->location,
24402 "named return values are no longer supported");
24403 /* Skip tokens until we reach the start of the function body. */
24404 while (true)
24406 cp_token *token = cp_lexer_peek_token (parser->lexer);
24407 if (token->type == CPP_OPEN_BRACE
24408 || token->type == CPP_EOF
24409 || token->type == CPP_PRAGMA_EOL)
24410 break;
24411 cp_lexer_consume_token (parser->lexer);
24414 /* The `extern' in `extern "C" void f () { ... }' does not apply to
24415 anything declared inside `f'. */
24416 saved_in_unbraced_linkage_specification_p
24417 = parser->in_unbraced_linkage_specification_p;
24418 parser->in_unbraced_linkage_specification_p = false;
24419 /* Inside the function, surrounding template-parameter-lists do not
24420 apply. */
24421 saved_num_template_parameter_lists
24422 = parser->num_template_parameter_lists;
24423 parser->num_template_parameter_lists = 0;
24425 start_lambda_scope (current_function_decl);
24427 /* If the next token is `try', `__transaction_atomic', or
24428 `__transaction_relaxed`, then we are looking at either function-try-block
24429 or function-transaction-block. Note that all of these include the
24430 function-body. */
24431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
24432 ctor_initializer_p = cp_parser_function_transaction (parser,
24433 RID_TRANSACTION_ATOMIC);
24434 else if (cp_lexer_next_token_is_keyword (parser->lexer,
24435 RID_TRANSACTION_RELAXED))
24436 ctor_initializer_p = cp_parser_function_transaction (parser,
24437 RID_TRANSACTION_RELAXED);
24438 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24439 ctor_initializer_p = cp_parser_function_try_block (parser);
24440 else
24441 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24442 (parser, /*in_function_try_block=*/false);
24444 finish_lambda_scope ();
24446 /* Finish the function. */
24447 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
24448 (inline_p ? 2 : 0));
24449 /* Generate code for it, if necessary. */
24450 expand_or_defer_fn (fn);
24451 /* Restore the saved values. */
24452 parser->in_unbraced_linkage_specification_p
24453 = saved_in_unbraced_linkage_specification_p;
24454 parser->num_template_parameter_lists
24455 = saved_num_template_parameter_lists;
24456 parser->in_function_body = saved_in_function_body;
24458 parser->fully_implicit_function_template_p
24459 = fully_implicit_function_template_p;
24460 parser->implicit_template_parms
24461 = implicit_template_parms;
24462 parser->implicit_template_scope
24463 = implicit_template_scope;
24465 if (parser->fully_implicit_function_template_p)
24466 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
24468 return fn;
24471 /* Parse a concept introduction header for a template-declaration. If
24472 successful, returns the template parameters. Otherwise returns
24473 error_mark_node. */
24475 static tree
24476 cp_parser_template_introduction (cp_parser* parser)
24478 // Look for the optional `::' operator.
24479 cp_parser_global_scope_opt (parser,
24480 /*current_scope_valid_p=*/true);
24481 // Look for the nested-name-specifier.
24482 cp_parser_nested_name_specifier_opt (parser,
24483 /*typename_keyword_p=*/false,
24484 /*check_dependency_p=*/true,
24485 /*type_p=*/false,
24486 /*is_declaration=*/false);
24488 cp_token *token = cp_lexer_peek_token (parser->lexer);
24489 tree concept_name = cp_parser_identifier (parser);
24490 if (concept_name == error_mark_node)
24491 return error_mark_node;
24493 // Look for opening brace for introduction
24494 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24495 return error_mark_node;
24497 // This must be a concept introduction.
24498 if (cp_parser_parsing_tentatively (parser))
24499 cp_parser_commit_to_tentative_parse (parser);
24501 // Build vector of placeholder parameters and grab matching identifiers.
24502 tree introduction_list = cp_parser_introduction_list (parser);
24504 // The introduction-list shall not be empty
24505 int nargs = TREE_VEC_LENGTH (introduction_list);
24506 if (nargs == 0)
24508 error ("an introduction-list shall not be empty");
24509 return error_mark_node;
24512 // Look for closing brace for introduction
24513 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24514 return error_mark_node;
24516 // Look up the concept for which we will be matching template parameters.
24517 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
24518 token->location);
24519 if (tmpl_decl == error_mark_node)
24521 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
24522 token->location);
24523 return error_mark_node;
24526 // Build and associate the constraint.
24527 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
24528 if (parms)
24529 return parms;
24531 error_at (token->location, "no matching concept for introduction-list");
24532 return error_mark_node;
24535 /* Parse a template-declaration, assuming that the `export' (and
24536 `extern') keywords, if present, has already been scanned. MEMBER_P
24537 is as for cp_parser_template_declaration. */
24539 static void
24540 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
24542 tree decl = NULL_TREE;
24543 vec<deferred_access_check, va_gc> *checks;
24544 tree parameter_list;
24545 bool friend_p = false;
24546 bool need_lang_pop;
24547 cp_token *token;
24549 /* Look for the `template' keyword. */
24550 token = cp_lexer_peek_token (parser->lexer);
24552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24554 if (cp_parser_parsing_tentatively (parser))
24555 cp_parser_commit_to_tentative_parse (parser);
24556 cp_lexer_consume_token (parser->lexer);
24558 /* Look for the `<'. */
24559 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
24560 return;
24561 if (at_class_scope_p () && current_function_decl)
24563 /* 14.5.2.2 [temp.mem]
24565 A local class shall not have member templates. */
24566 error_at (token->location,
24567 "invalid declaration of member template in local class");
24568 cp_parser_skip_to_end_of_block_or_statement (parser);
24569 return;
24571 /* [temp]
24573 A template ... shall not have C linkage. */
24574 if (current_lang_name == lang_name_c)
24576 error_at (token->location, "template with C linkage");
24577 /* Give it C++ linkage to avoid confusing other parts of the
24578 front end. */
24579 push_lang_context (lang_name_cplusplus);
24580 need_lang_pop = true;
24582 else
24583 need_lang_pop = false;
24585 /* We cannot perform access checks on the template parameter
24586 declarations until we know what is being declared, just as we
24587 cannot check the decl-specifier list. */
24588 push_deferring_access_checks (dk_deferred);
24590 /* If the next token is `>', then we have an invalid
24591 specialization. Rather than complain about an invalid template
24592 parameter, issue an error message here. */
24593 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
24595 cp_parser_error (parser, "invalid explicit specialization");
24596 begin_specialization ();
24597 parameter_list = NULL_TREE;
24599 else
24601 /* Parse the template parameters. */
24602 parameter_list = cp_parser_template_parameter_list (parser);
24605 /* Get the deferred access checks from the parameter list. These
24606 will be checked once we know what is being declared, as for a
24607 member template the checks must be performed in the scope of the
24608 class containing the member. */
24609 checks = get_deferred_access_checks ();
24611 /* Look for the `>'. */
24612 cp_parser_skip_to_end_of_template_parameter_list (parser);
24614 /* Manage template requirements */
24615 if (flag_concepts)
24617 tree reqs = get_shorthand_constraints (current_template_parms);
24618 if (tree r = cp_parser_requires_clause_opt (parser)) {
24619 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
24621 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
24624 else if (flag_concepts)
24626 need_lang_pop = false;
24627 checks = NULL;
24629 // Scope may be changed by a nested-name-specifier.
24630 tree saved_scope = parser->scope;
24631 tree saved_qualifying_scope = parser->qualifying_scope;
24632 tree saved_object_scope = parser->object_scope;
24634 push_deferring_access_checks (dk_deferred);
24635 parameter_list = cp_parser_template_introduction (parser);
24636 if (parameter_list == error_mark_node)
24638 pop_deferring_access_checks ();
24639 cp_parser_skip_to_end_of_statement (parser);
24640 return;
24643 parser->scope = saved_scope;
24644 parser->qualifying_scope = saved_qualifying_scope;
24645 parser->object_scope = saved_object_scope;
24648 else
24650 if (!cp_parser_simulate_error (parser))
24651 error ("expected template header");
24652 return;
24655 /* We just processed one more parameter list. */
24656 ++parser->num_template_parameter_lists;
24658 /* Look for another template. We need to start a tentative parse here as
24659 the next header could be a concept introduction. */
24660 cp_parser_parse_tentatively (parser);
24661 cp_parser_template_declaration_after_export (parser, member_p);
24662 if (cp_parser_parse_definitely (parser))
24663 /* Found another template. */;
24664 else if (cxx_dialect >= cxx11
24665 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24666 decl = cp_parser_alias_declaration (parser);
24667 else
24669 /* There are no access checks when parsing a template, as we do not
24670 know if a specialization will be a friend. */
24671 push_deferring_access_checks (dk_no_check);
24672 token = cp_lexer_peek_token (parser->lexer);
24673 decl = cp_parser_single_declaration (parser,
24674 checks,
24675 member_p,
24676 /*explicit_specialization_p=*/false,
24677 &friend_p);
24678 pop_deferring_access_checks ();
24680 /* If this is a member template declaration, let the front
24681 end know. */
24682 if (member_p && !friend_p && decl)
24684 if (TREE_CODE (decl) == TYPE_DECL)
24685 cp_parser_check_access_in_redeclaration (decl, token->location);
24687 decl = finish_member_template_decl (decl);
24689 else if (friend_p && decl
24690 && DECL_DECLARES_TYPE_P (decl))
24691 make_friend_class (current_class_type, TREE_TYPE (decl),
24692 /*complain=*/true);
24694 /* We are done with the current parameter list. */
24695 --parser->num_template_parameter_lists;
24697 pop_deferring_access_checks ();
24699 /* Finish up. */
24700 finish_template_decl (parameter_list);
24702 /* Check the template arguments for a literal operator template. */
24703 if (decl
24704 && DECL_DECLARES_FUNCTION_P (decl)
24705 && UDLIT_OPER_P (DECL_NAME (decl)))
24707 bool ok = true;
24708 if (parameter_list == NULL_TREE)
24709 ok = false;
24710 else
24712 int num_parms = TREE_VEC_LENGTH (parameter_list);
24713 if (num_parms == 1)
24715 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
24716 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24717 if (TREE_TYPE (parm) != char_type_node
24718 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24719 ok = false;
24721 else if (num_parms == 2 && cxx_dialect >= cxx14)
24723 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
24724 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
24725 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
24726 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24727 if (TREE_TYPE (parm) != TREE_TYPE (type)
24728 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24729 ok = false;
24731 else
24732 ok = false;
24734 if (!ok)
24736 if (cxx_dialect >= cxx14)
24737 error ("literal operator template %qD has invalid parameter list."
24738 " Expected non-type template argument pack <char...>"
24739 " or <typename CharT, CharT...>",
24740 decl);
24741 else
24742 error ("literal operator template %qD has invalid parameter list."
24743 " Expected non-type template argument pack <char...>",
24744 decl);
24748 /* Register member declarations. */
24749 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
24750 finish_member_declaration (decl);
24751 /* For the erroneous case of a template with C linkage, we pushed an
24752 implicit C++ linkage scope; exit that scope now. */
24753 if (need_lang_pop)
24754 pop_lang_context ();
24755 /* If DECL is a function template, we must return to parse it later.
24756 (Even though there is no definition, there might be default
24757 arguments that need handling.) */
24758 if (member_p && decl
24759 && DECL_DECLARES_FUNCTION_P (decl))
24760 vec_safe_push (unparsed_funs_with_definitions, decl);
24763 /* Perform the deferred access checks from a template-parameter-list.
24764 CHECKS is a TREE_LIST of access checks, as returned by
24765 get_deferred_access_checks. */
24767 static void
24768 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
24770 ++processing_template_parmlist;
24771 perform_access_checks (checks, tf_warning_or_error);
24772 --processing_template_parmlist;
24775 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
24776 `function-definition' sequence that follows a template header.
24777 If MEMBER_P is true, this declaration appears in a class scope.
24779 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
24780 *FRIEND_P is set to TRUE iff the declaration is a friend. */
24782 static tree
24783 cp_parser_single_declaration (cp_parser* parser,
24784 vec<deferred_access_check, va_gc> *checks,
24785 bool member_p,
24786 bool explicit_specialization_p,
24787 bool* friend_p)
24789 int declares_class_or_enum;
24790 tree decl = NULL_TREE;
24791 cp_decl_specifier_seq decl_specifiers;
24792 bool function_definition_p = false;
24793 cp_token *decl_spec_token_start;
24795 /* This function is only used when processing a template
24796 declaration. */
24797 gcc_assert (innermost_scope_kind () == sk_template_parms
24798 || innermost_scope_kind () == sk_template_spec);
24800 /* Defer access checks until we know what is being declared. */
24801 push_deferring_access_checks (dk_deferred);
24803 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
24804 alternative. */
24805 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24806 cp_parser_decl_specifier_seq (parser,
24807 CP_PARSER_FLAGS_OPTIONAL,
24808 &decl_specifiers,
24809 &declares_class_or_enum);
24810 if (friend_p)
24811 *friend_p = cp_parser_friend_p (&decl_specifiers);
24813 /* There are no template typedefs. */
24814 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
24816 error_at (decl_spec_token_start->location,
24817 "template declaration of %<typedef%>");
24818 decl = error_mark_node;
24821 /* Gather up the access checks that occurred the
24822 decl-specifier-seq. */
24823 stop_deferring_access_checks ();
24825 /* Check for the declaration of a template class. */
24826 if (declares_class_or_enum)
24828 if (cp_parser_declares_only_class_p (parser))
24830 // If this is a declaration, but not a definition, associate
24831 // any constraints with the type declaration. Constraints
24832 // are associated with definitions in cp_parser_class_specifier.
24833 if (declares_class_or_enum == 1)
24834 associate_classtype_constraints (decl_specifiers.type);
24836 decl = shadow_tag (&decl_specifiers);
24838 /* In this case:
24840 struct C {
24841 friend template <typename T> struct A<T>::B;
24844 A<T>::B will be represented by a TYPENAME_TYPE, and
24845 therefore not recognized by shadow_tag. */
24846 if (friend_p && *friend_p
24847 && !decl
24848 && decl_specifiers.type
24849 && TYPE_P (decl_specifiers.type))
24850 decl = decl_specifiers.type;
24852 if (decl && decl != error_mark_node)
24853 decl = TYPE_NAME (decl);
24854 else
24855 decl = error_mark_node;
24857 /* Perform access checks for template parameters. */
24858 cp_parser_perform_template_parameter_access_checks (checks);
24862 /* Complain about missing 'typename' or other invalid type names. */
24863 if (!decl_specifiers.any_type_specifiers_p
24864 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24866 /* cp_parser_parse_and_diagnose_invalid_type_name calls
24867 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
24868 the rest of this declaration. */
24869 decl = error_mark_node;
24870 goto out;
24873 /* If it's not a template class, try for a template function. If
24874 the next token is a `;', then this declaration does not declare
24875 anything. But, if there were errors in the decl-specifiers, then
24876 the error might well have come from an attempted class-specifier.
24877 In that case, there's no need to warn about a missing declarator. */
24878 if (!decl
24879 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
24880 || decl_specifiers.type != error_mark_node))
24882 decl = cp_parser_init_declarator (parser,
24883 &decl_specifiers,
24884 checks,
24885 /*function_definition_allowed_p=*/true,
24886 member_p,
24887 declares_class_or_enum,
24888 &function_definition_p,
24889 NULL, NULL);
24891 /* 7.1.1-1 [dcl.stc]
24893 A storage-class-specifier shall not be specified in an explicit
24894 specialization... */
24895 if (decl
24896 && explicit_specialization_p
24897 && decl_specifiers.storage_class != sc_none)
24899 error_at (decl_spec_token_start->location,
24900 "explicit template specialization cannot have a storage class");
24901 decl = error_mark_node;
24904 if (decl && VAR_P (decl))
24905 check_template_variable (decl);
24908 /* Look for a trailing `;' after the declaration. */
24909 if (!function_definition_p
24910 && (decl == error_mark_node
24911 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
24912 cp_parser_skip_to_end_of_block_or_statement (parser);
24914 out:
24915 pop_deferring_access_checks ();
24917 /* Clear any current qualification; whatever comes next is the start
24918 of something new. */
24919 parser->scope = NULL_TREE;
24920 parser->qualifying_scope = NULL_TREE;
24921 parser->object_scope = NULL_TREE;
24923 return decl;
24926 /* Parse a cast-expression that is not the operand of a unary "&". */
24928 static tree
24929 cp_parser_simple_cast_expression (cp_parser *parser)
24931 return cp_parser_cast_expression (parser, /*address_p=*/false,
24932 /*cast_p=*/false, /*decltype*/false, NULL);
24935 /* Parse a functional cast to TYPE. Returns an expression
24936 representing the cast. */
24938 static tree
24939 cp_parser_functional_cast (cp_parser* parser, tree type)
24941 vec<tree, va_gc> *vec;
24942 tree expression_list;
24943 tree cast;
24944 bool nonconst_p;
24946 if (!type)
24947 type = error_mark_node;
24949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24951 cp_lexer_set_source_position (parser->lexer);
24952 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24953 expression_list = cp_parser_braced_list (parser, &nonconst_p);
24954 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
24955 if (TREE_CODE (type) == TYPE_DECL)
24956 type = TREE_TYPE (type);
24957 return finish_compound_literal (type, expression_list,
24958 tf_warning_or_error);
24962 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
24963 /*cast_p=*/true,
24964 /*allow_expansion_p=*/true,
24965 /*non_constant_p=*/NULL);
24966 if (vec == NULL)
24967 expression_list = error_mark_node;
24968 else
24970 expression_list = build_tree_list_vec (vec);
24971 release_tree_vector (vec);
24974 cast = build_functional_cast (type, expression_list,
24975 tf_warning_or_error);
24976 /* [expr.const]/1: In an integral constant expression "only type
24977 conversions to integral or enumeration type can be used". */
24978 if (TREE_CODE (type) == TYPE_DECL)
24979 type = TREE_TYPE (type);
24980 if (cast != error_mark_node
24981 && !cast_valid_in_integral_constant_expression_p (type)
24982 && cp_parser_non_integral_constant_expression (parser,
24983 NIC_CONSTRUCTOR))
24984 return error_mark_node;
24985 return cast;
24988 /* Save the tokens that make up the body of a member function defined
24989 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
24990 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
24991 specifiers applied to the declaration. Returns the FUNCTION_DECL
24992 for the member function. */
24994 static tree
24995 cp_parser_save_member_function_body (cp_parser* parser,
24996 cp_decl_specifier_seq *decl_specifiers,
24997 cp_declarator *declarator,
24998 tree attributes)
25000 cp_token *first;
25001 cp_token *last;
25002 tree fn;
25004 /* Create the FUNCTION_DECL. */
25005 fn = grokmethod (decl_specifiers, declarator, attributes);
25006 cp_finalize_omp_declare_simd (parser, fn);
25007 /* If something went badly wrong, bail out now. */
25008 if (fn == error_mark_node)
25010 /* If there's a function-body, skip it. */
25011 if (cp_parser_token_starts_function_definition_p
25012 (cp_lexer_peek_token (parser->lexer)))
25013 cp_parser_skip_to_end_of_block_or_statement (parser);
25014 return error_mark_node;
25017 /* Remember it, if there default args to post process. */
25018 cp_parser_save_default_args (parser, fn);
25020 /* Save away the tokens that make up the body of the
25021 function. */
25022 first = parser->lexer->next_token;
25023 /* Handle function try blocks. */
25024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25025 cp_lexer_consume_token (parser->lexer);
25026 /* We can have braced-init-list mem-initializers before the fn body. */
25027 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25029 cp_lexer_consume_token (parser->lexer);
25030 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25032 /* cache_group will stop after an un-nested { } pair, too. */
25033 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25034 break;
25036 /* variadic mem-inits have ... after the ')'. */
25037 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25038 cp_lexer_consume_token (parser->lexer);
25041 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25042 /* Handle function try blocks. */
25043 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25044 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25045 last = parser->lexer->next_token;
25047 /* Save away the inline definition; we will process it when the
25048 class is complete. */
25049 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25050 DECL_PENDING_INLINE_P (fn) = 1;
25052 /* We need to know that this was defined in the class, so that
25053 friend templates are handled correctly. */
25054 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25056 /* Add FN to the queue of functions to be parsed later. */
25057 vec_safe_push (unparsed_funs_with_definitions, fn);
25059 return fn;
25062 /* Save the tokens that make up the in-class initializer for a non-static
25063 data member. Returns a DEFAULT_ARG. */
25065 static tree
25066 cp_parser_save_nsdmi (cp_parser* parser)
25068 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
25071 /* Parse a template-argument-list, as well as the trailing ">" (but
25072 not the opening "<"). See cp_parser_template_argument_list for the
25073 return value. */
25075 static tree
25076 cp_parser_enclosed_template_argument_list (cp_parser* parser)
25078 tree arguments;
25079 tree saved_scope;
25080 tree saved_qualifying_scope;
25081 tree saved_object_scope;
25082 bool saved_greater_than_is_operator_p;
25083 int saved_unevaluated_operand;
25084 int saved_inhibit_evaluation_warnings;
25086 /* [temp.names]
25088 When parsing a template-id, the first non-nested `>' is taken as
25089 the end of the template-argument-list rather than a greater-than
25090 operator. */
25091 saved_greater_than_is_operator_p
25092 = parser->greater_than_is_operator_p;
25093 parser->greater_than_is_operator_p = false;
25094 /* Parsing the argument list may modify SCOPE, so we save it
25095 here. */
25096 saved_scope = parser->scope;
25097 saved_qualifying_scope = parser->qualifying_scope;
25098 saved_object_scope = parser->object_scope;
25099 /* We need to evaluate the template arguments, even though this
25100 template-id may be nested within a "sizeof". */
25101 saved_unevaluated_operand = cp_unevaluated_operand;
25102 cp_unevaluated_operand = 0;
25103 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25104 c_inhibit_evaluation_warnings = 0;
25105 /* Parse the template-argument-list itself. */
25106 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
25107 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25108 arguments = NULL_TREE;
25109 else
25110 arguments = cp_parser_template_argument_list (parser);
25111 /* Look for the `>' that ends the template-argument-list. If we find
25112 a '>>' instead, it's probably just a typo. */
25113 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25115 if (cxx_dialect != cxx98)
25117 /* In C++0x, a `>>' in a template argument list or cast
25118 expression is considered to be two separate `>'
25119 tokens. So, change the current token to a `>', but don't
25120 consume it: it will be consumed later when the outer
25121 template argument list (or cast expression) is parsed.
25122 Note that this replacement of `>' for `>>' is necessary
25123 even if we are parsing tentatively: in the tentative
25124 case, after calling
25125 cp_parser_enclosed_template_argument_list we will always
25126 throw away all of the template arguments and the first
25127 closing `>', either because the template argument list
25128 was erroneous or because we are replacing those tokens
25129 with a CPP_TEMPLATE_ID token. The second `>' (which will
25130 not have been thrown away) is needed either to close an
25131 outer template argument list or to complete a new-style
25132 cast. */
25133 cp_token *token = cp_lexer_peek_token (parser->lexer);
25134 token->type = CPP_GREATER;
25136 else if (!saved_greater_than_is_operator_p)
25138 /* If we're in a nested template argument list, the '>>' has
25139 to be a typo for '> >'. We emit the error message, but we
25140 continue parsing and we push a '>' as next token, so that
25141 the argument list will be parsed correctly. Note that the
25142 global source location is still on the token before the
25143 '>>', so we need to say explicitly where we want it. */
25144 cp_token *token = cp_lexer_peek_token (parser->lexer);
25145 error_at (token->location, "%<>>%> should be %<> >%> "
25146 "within a nested template argument list");
25148 token->type = CPP_GREATER;
25150 else
25152 /* If this is not a nested template argument list, the '>>'
25153 is a typo for '>'. Emit an error message and continue.
25154 Same deal about the token location, but here we can get it
25155 right by consuming the '>>' before issuing the diagnostic. */
25156 cp_token *token = cp_lexer_consume_token (parser->lexer);
25157 error_at (token->location,
25158 "spurious %<>>%>, use %<>%> to terminate "
25159 "a template argument list");
25162 else
25163 cp_parser_skip_to_end_of_template_parameter_list (parser);
25164 /* The `>' token might be a greater-than operator again now. */
25165 parser->greater_than_is_operator_p
25166 = saved_greater_than_is_operator_p;
25167 /* Restore the SAVED_SCOPE. */
25168 parser->scope = saved_scope;
25169 parser->qualifying_scope = saved_qualifying_scope;
25170 parser->object_scope = saved_object_scope;
25171 cp_unevaluated_operand = saved_unevaluated_operand;
25172 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25174 return arguments;
25177 /* MEMBER_FUNCTION is a member function, or a friend. If default
25178 arguments, or the body of the function have not yet been parsed,
25179 parse them now. */
25181 static void
25182 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
25184 timevar_push (TV_PARSE_INMETH);
25185 /* If this member is a template, get the underlying
25186 FUNCTION_DECL. */
25187 if (DECL_FUNCTION_TEMPLATE_P (member_function))
25188 member_function = DECL_TEMPLATE_RESULT (member_function);
25190 /* There should not be any class definitions in progress at this
25191 point; the bodies of members are only parsed outside of all class
25192 definitions. */
25193 gcc_assert (parser->num_classes_being_defined == 0);
25194 /* While we're parsing the member functions we might encounter more
25195 classes. We want to handle them right away, but we don't want
25196 them getting mixed up with functions that are currently in the
25197 queue. */
25198 push_unparsed_function_queues (parser);
25200 /* Make sure that any template parameters are in scope. */
25201 maybe_begin_member_template_processing (member_function);
25203 /* If the body of the function has not yet been parsed, parse it
25204 now. */
25205 if (DECL_PENDING_INLINE_P (member_function))
25207 tree function_scope;
25208 cp_token_cache *tokens;
25210 /* The function is no longer pending; we are processing it. */
25211 tokens = DECL_PENDING_INLINE_INFO (member_function);
25212 DECL_PENDING_INLINE_INFO (member_function) = NULL;
25213 DECL_PENDING_INLINE_P (member_function) = 0;
25215 /* If this is a local class, enter the scope of the containing
25216 function. */
25217 function_scope = current_function_decl;
25218 if (function_scope)
25219 push_function_context ();
25221 /* Push the body of the function onto the lexer stack. */
25222 cp_parser_push_lexer_for_tokens (parser, tokens);
25224 /* Let the front end know that we going to be defining this
25225 function. */
25226 start_preparsed_function (member_function, NULL_TREE,
25227 SF_PRE_PARSED | SF_INCLASS_INLINE);
25229 /* Don't do access checking if it is a templated function. */
25230 if (processing_template_decl)
25231 push_deferring_access_checks (dk_no_check);
25233 /* #pragma omp declare reduction needs special parsing. */
25234 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
25236 parser->lexer->in_pragma = true;
25237 cp_parser_omp_declare_reduction_exprs (member_function, parser);
25238 finish_function (/*inline*/2);
25239 cp_check_omp_declare_reduction (member_function);
25241 else
25242 /* Now, parse the body of the function. */
25243 cp_parser_function_definition_after_declarator (parser,
25244 /*inline_p=*/true);
25246 if (processing_template_decl)
25247 pop_deferring_access_checks ();
25249 /* Leave the scope of the containing function. */
25250 if (function_scope)
25251 pop_function_context ();
25252 cp_parser_pop_lexer (parser);
25255 /* Remove any template parameters from the symbol table. */
25256 maybe_end_member_template_processing ();
25258 /* Restore the queue. */
25259 pop_unparsed_function_queues (parser);
25260 timevar_pop (TV_PARSE_INMETH);
25263 /* If DECL contains any default args, remember it on the unparsed
25264 functions queue. */
25266 static void
25267 cp_parser_save_default_args (cp_parser* parser, tree decl)
25269 tree probe;
25271 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
25272 probe;
25273 probe = TREE_CHAIN (probe))
25274 if (TREE_PURPOSE (probe))
25276 cp_default_arg_entry entry = {current_class_type, decl};
25277 vec_safe_push (unparsed_funs_with_default_args, entry);
25278 break;
25282 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
25283 which is either a FIELD_DECL or PARM_DECL. Parse it and return
25284 the result. For a PARM_DECL, PARMTYPE is the corresponding type
25285 from the parameter-type-list. */
25287 static tree
25288 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
25289 tree default_arg, tree parmtype)
25291 cp_token_cache *tokens;
25292 tree parsed_arg;
25293 bool dummy;
25295 if (default_arg == error_mark_node)
25296 return error_mark_node;
25298 /* Push the saved tokens for the default argument onto the parser's
25299 lexer stack. */
25300 tokens = DEFARG_TOKENS (default_arg);
25301 cp_parser_push_lexer_for_tokens (parser, tokens);
25303 start_lambda_scope (decl);
25305 /* Parse the default argument. */
25306 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
25307 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
25308 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25310 finish_lambda_scope ();
25312 if (parsed_arg == error_mark_node)
25313 cp_parser_skip_to_end_of_statement (parser);
25315 if (!processing_template_decl)
25317 /* In a non-template class, check conversions now. In a template,
25318 we'll wait and instantiate these as needed. */
25319 if (TREE_CODE (decl) == PARM_DECL)
25320 parsed_arg = check_default_argument (parmtype, parsed_arg,
25321 tf_warning_or_error);
25322 else
25323 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
25326 /* If the token stream has not been completely used up, then
25327 there was extra junk after the end of the default
25328 argument. */
25329 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25331 if (TREE_CODE (decl) == PARM_DECL)
25332 cp_parser_error (parser, "expected %<,%>");
25333 else
25334 cp_parser_error (parser, "expected %<;%>");
25337 /* Revert to the main lexer. */
25338 cp_parser_pop_lexer (parser);
25340 return parsed_arg;
25343 /* FIELD is a non-static data member with an initializer which we saved for
25344 later; parse it now. */
25346 static void
25347 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
25349 tree def;
25351 maybe_begin_member_template_processing (field);
25353 push_unparsed_function_queues (parser);
25354 def = cp_parser_late_parse_one_default_arg (parser, field,
25355 DECL_INITIAL (field),
25356 NULL_TREE);
25357 pop_unparsed_function_queues (parser);
25359 maybe_end_member_template_processing ();
25361 DECL_INITIAL (field) = def;
25364 /* FN is a FUNCTION_DECL which may contains a parameter with an
25365 unparsed DEFAULT_ARG. Parse the default args now. This function
25366 assumes that the current scope is the scope in which the default
25367 argument should be processed. */
25369 static void
25370 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
25372 bool saved_local_variables_forbidden_p;
25373 tree parm, parmdecl;
25375 /* While we're parsing the default args, we might (due to the
25376 statement expression extension) encounter more classes. We want
25377 to handle them right away, but we don't want them getting mixed
25378 up with default args that are currently in the queue. */
25379 push_unparsed_function_queues (parser);
25381 /* Local variable names (and the `this' keyword) may not appear
25382 in a default argument. */
25383 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25384 parser->local_variables_forbidden_p = true;
25386 push_defarg_context (fn);
25388 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
25389 parmdecl = DECL_ARGUMENTS (fn);
25390 parm && parm != void_list_node;
25391 parm = TREE_CHAIN (parm),
25392 parmdecl = DECL_CHAIN (parmdecl))
25394 tree default_arg = TREE_PURPOSE (parm);
25395 tree parsed_arg;
25396 vec<tree, va_gc> *insts;
25397 tree copy;
25398 unsigned ix;
25400 if (!default_arg)
25401 continue;
25403 if (TREE_CODE (default_arg) != DEFAULT_ARG)
25404 /* This can happen for a friend declaration for a function
25405 already declared with default arguments. */
25406 continue;
25408 parsed_arg
25409 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
25410 default_arg,
25411 TREE_VALUE (parm));
25412 if (parsed_arg == error_mark_node)
25414 continue;
25417 TREE_PURPOSE (parm) = parsed_arg;
25419 /* Update any instantiations we've already created. */
25420 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
25421 vec_safe_iterate (insts, ix, &copy); ix++)
25422 TREE_PURPOSE (copy) = parsed_arg;
25425 pop_defarg_context ();
25427 /* Make sure no default arg is missing. */
25428 check_default_args (fn);
25430 /* Restore the state of local_variables_forbidden_p. */
25431 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25433 /* Restore the queue. */
25434 pop_unparsed_function_queues (parser);
25437 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
25439 sizeof ... ( identifier )
25441 where the 'sizeof' token has already been consumed. */
25443 static tree
25444 cp_parser_sizeof_pack (cp_parser *parser)
25446 /* Consume the `...'. */
25447 cp_lexer_consume_token (parser->lexer);
25448 maybe_warn_variadic_templates ();
25450 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
25451 if (paren)
25452 cp_lexer_consume_token (parser->lexer);
25453 else
25454 permerror (cp_lexer_peek_token (parser->lexer)->location,
25455 "%<sizeof...%> argument must be surrounded by parentheses");
25457 cp_token *token = cp_lexer_peek_token (parser->lexer);
25458 tree name = cp_parser_identifier (parser);
25459 if (name == error_mark_node)
25460 return error_mark_node;
25461 /* The name is not qualified. */
25462 parser->scope = NULL_TREE;
25463 parser->qualifying_scope = NULL_TREE;
25464 parser->object_scope = NULL_TREE;
25465 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
25466 if (expr == error_mark_node)
25467 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
25468 token->location);
25469 if (TREE_CODE (expr) == TYPE_DECL)
25470 expr = TREE_TYPE (expr);
25471 else if (TREE_CODE (expr) == CONST_DECL)
25472 expr = DECL_INITIAL (expr);
25473 expr = make_pack_expansion (expr);
25475 if (paren)
25476 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25478 return expr;
25481 /* Parse the operand of `sizeof' (or a similar operator). Returns
25482 either a TYPE or an expression, depending on the form of the
25483 input. The KEYWORD indicates which kind of expression we have
25484 encountered. */
25486 static tree
25487 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
25489 tree expr = NULL_TREE;
25490 const char *saved_message;
25491 char *tmp;
25492 bool saved_integral_constant_expression_p;
25493 bool saved_non_integral_constant_expression_p;
25495 /* If it's a `...', then we are computing the length of a parameter
25496 pack. */
25497 if (keyword == RID_SIZEOF
25498 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25499 return cp_parser_sizeof_pack (parser);
25501 /* Types cannot be defined in a `sizeof' expression. Save away the
25502 old message. */
25503 saved_message = parser->type_definition_forbidden_message;
25504 /* And create the new one. */
25505 tmp = concat ("types may not be defined in %<",
25506 IDENTIFIER_POINTER (ridpointers[keyword]),
25507 "%> expressions", NULL);
25508 parser->type_definition_forbidden_message = tmp;
25510 /* The restrictions on constant-expressions do not apply inside
25511 sizeof expressions. */
25512 saved_integral_constant_expression_p
25513 = parser->integral_constant_expression_p;
25514 saved_non_integral_constant_expression_p
25515 = parser->non_integral_constant_expression_p;
25516 parser->integral_constant_expression_p = false;
25518 /* Do not actually evaluate the expression. */
25519 ++cp_unevaluated_operand;
25520 ++c_inhibit_evaluation_warnings;
25521 /* If it's a `(', then we might be looking at the type-id
25522 construction. */
25523 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25525 tree type = NULL_TREE;
25527 /* We can't be sure yet whether we're looking at a type-id or an
25528 expression. */
25529 cp_parser_parse_tentatively (parser);
25530 /* Note: as a GNU Extension, compound literals are considered
25531 postfix-expressions as they are in C99, so they are valid
25532 arguments to sizeof. See comment in cp_parser_cast_expression
25533 for details. */
25534 if (cp_parser_compound_literal_p (parser))
25535 cp_parser_simulate_error (parser);
25536 else
25538 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
25539 parser->in_type_id_in_expr_p = true;
25540 /* Look for the type-id. */
25541 type = cp_parser_type_id (parser);
25542 /* Look for the closing `)'. */
25543 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25544 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
25547 /* If all went well, then we're done. */
25548 if (cp_parser_parse_definitely (parser))
25550 cp_decl_specifier_seq decl_specs;
25552 /* Build a trivial decl-specifier-seq. */
25553 clear_decl_specs (&decl_specs);
25554 decl_specs.type = type;
25556 /* Call grokdeclarator to figure out what type this is. */
25557 expr = grokdeclarator (NULL,
25558 &decl_specs,
25559 TYPENAME,
25560 /*initialized=*/0,
25561 /*attrlist=*/NULL);
25565 /* If the type-id production did not work out, then we must be
25566 looking at the unary-expression production. */
25567 if (!expr)
25568 expr = cp_parser_unary_expression (parser);
25570 /* Go back to evaluating expressions. */
25571 --cp_unevaluated_operand;
25572 --c_inhibit_evaluation_warnings;
25574 /* Free the message we created. */
25575 free (tmp);
25576 /* And restore the old one. */
25577 parser->type_definition_forbidden_message = saved_message;
25578 parser->integral_constant_expression_p
25579 = saved_integral_constant_expression_p;
25580 parser->non_integral_constant_expression_p
25581 = saved_non_integral_constant_expression_p;
25583 return expr;
25586 /* If the current declaration has no declarator, return true. */
25588 static bool
25589 cp_parser_declares_only_class_p (cp_parser *parser)
25591 /* If the next token is a `;' or a `,' then there is no
25592 declarator. */
25593 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25594 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
25597 /* Update the DECL_SPECS to reflect the storage class indicated by
25598 KEYWORD. */
25600 static void
25601 cp_parser_set_storage_class (cp_parser *parser,
25602 cp_decl_specifier_seq *decl_specs,
25603 enum rid keyword,
25604 cp_token *token)
25606 cp_storage_class storage_class;
25608 if (parser->in_unbraced_linkage_specification_p)
25610 error_at (token->location, "invalid use of %qD in linkage specification",
25611 ridpointers[keyword]);
25612 return;
25614 else if (decl_specs->storage_class != sc_none)
25616 decl_specs->conflicting_specifiers_p = true;
25617 return;
25620 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
25621 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
25622 && decl_specs->gnu_thread_keyword_p)
25624 pedwarn (decl_specs->locations[ds_thread], 0,
25625 "%<__thread%> before %qD", ridpointers[keyword]);
25628 switch (keyword)
25630 case RID_AUTO:
25631 storage_class = sc_auto;
25632 break;
25633 case RID_REGISTER:
25634 storage_class = sc_register;
25635 break;
25636 case RID_STATIC:
25637 storage_class = sc_static;
25638 break;
25639 case RID_EXTERN:
25640 storage_class = sc_extern;
25641 break;
25642 case RID_MUTABLE:
25643 storage_class = sc_mutable;
25644 break;
25645 default:
25646 gcc_unreachable ();
25648 decl_specs->storage_class = storage_class;
25649 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
25651 /* A storage class specifier cannot be applied alongside a typedef
25652 specifier. If there is a typedef specifier present then set
25653 conflicting_specifiers_p which will trigger an error later
25654 on in grokdeclarator. */
25655 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
25656 decl_specs->conflicting_specifiers_p = true;
25659 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
25660 is true, the type is a class or enum definition. */
25662 static void
25663 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
25664 tree type_spec,
25665 cp_token *token,
25666 bool type_definition_p)
25668 decl_specs->any_specifiers_p = true;
25670 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
25671 (with, for example, in "typedef int wchar_t;") we remember that
25672 this is what happened. In system headers, we ignore these
25673 declarations so that G++ can work with system headers that are not
25674 C++-safe. */
25675 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
25676 && !type_definition_p
25677 && (type_spec == boolean_type_node
25678 || type_spec == char16_type_node
25679 || type_spec == char32_type_node
25680 || type_spec == wchar_type_node)
25681 && (decl_specs->type
25682 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
25683 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
25684 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
25685 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
25687 decl_specs->redefined_builtin_type = type_spec;
25688 set_and_check_decl_spec_loc (decl_specs,
25689 ds_redefined_builtin_type_spec,
25690 token);
25691 if (!decl_specs->type)
25693 decl_specs->type = type_spec;
25694 decl_specs->type_definition_p = false;
25695 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
25698 else if (decl_specs->type)
25699 decl_specs->multiple_types_p = true;
25700 else
25702 decl_specs->type = type_spec;
25703 decl_specs->type_definition_p = type_definition_p;
25704 decl_specs->redefined_builtin_type = NULL_TREE;
25705 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
25709 /* True iff TOKEN is the GNU keyword __thread. */
25711 static bool
25712 token_is__thread (cp_token *token)
25714 gcc_assert (token->keyword == RID_THREAD);
25715 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
25718 /* Set the location for a declarator specifier and check if it is
25719 duplicated.
25721 DECL_SPECS is the sequence of declarator specifiers onto which to
25722 set the location.
25724 DS is the single declarator specifier to set which location is to
25725 be set onto the existing sequence of declarators.
25727 LOCATION is the location for the declarator specifier to
25728 consider. */
25730 static void
25731 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
25732 cp_decl_spec ds, cp_token *token)
25734 gcc_assert (ds < ds_last);
25736 if (decl_specs == NULL)
25737 return;
25739 source_location location = token->location;
25741 if (decl_specs->locations[ds] == 0)
25743 decl_specs->locations[ds] = location;
25744 if (ds == ds_thread)
25745 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
25747 else
25749 if (ds == ds_long)
25751 if (decl_specs->locations[ds_long_long] != 0)
25752 error_at (location,
25753 "%<long long long%> is too long for GCC");
25754 else
25756 decl_specs->locations[ds_long_long] = location;
25757 pedwarn_cxx98 (location,
25758 OPT_Wlong_long,
25759 "ISO C++ 1998 does not support %<long long%>");
25762 else if (ds == ds_thread)
25764 bool gnu = token_is__thread (token);
25765 if (gnu != decl_specs->gnu_thread_keyword_p)
25766 error_at (location,
25767 "both %<__thread%> and %<thread_local%> specified");
25768 else
25769 error_at (location, "duplicate %qD", token->u.value);
25771 else
25773 static const char *const decl_spec_names[] = {
25774 "signed",
25775 "unsigned",
25776 "short",
25777 "long",
25778 "const",
25779 "volatile",
25780 "restrict",
25781 "inline",
25782 "virtual",
25783 "explicit",
25784 "friend",
25785 "typedef",
25786 "using",
25787 "constexpr",
25788 "__complex"
25790 error_at (location,
25791 "duplicate %qs", decl_spec_names[ds]);
25796 /* Return true iff the declarator specifier DS is present in the
25797 sequence of declarator specifiers DECL_SPECS. */
25799 bool
25800 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
25801 cp_decl_spec ds)
25803 gcc_assert (ds < ds_last);
25805 if (decl_specs == NULL)
25806 return false;
25808 return decl_specs->locations[ds] != 0;
25811 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
25812 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
25814 static bool
25815 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
25817 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
25820 /* Issue an error message indicating that TOKEN_DESC was expected.
25821 If KEYWORD is true, it indicated this function is called by
25822 cp_parser_require_keword and the required token can only be
25823 a indicated keyword. */
25825 static void
25826 cp_parser_required_error (cp_parser *parser,
25827 required_token token_desc,
25828 bool keyword)
25830 switch (token_desc)
25832 case RT_NEW:
25833 cp_parser_error (parser, "expected %<new%>");
25834 return;
25835 case RT_DELETE:
25836 cp_parser_error (parser, "expected %<delete%>");
25837 return;
25838 case RT_RETURN:
25839 cp_parser_error (parser, "expected %<return%>");
25840 return;
25841 case RT_WHILE:
25842 cp_parser_error (parser, "expected %<while%>");
25843 return;
25844 case RT_EXTERN:
25845 cp_parser_error (parser, "expected %<extern%>");
25846 return;
25847 case RT_STATIC_ASSERT:
25848 cp_parser_error (parser, "expected %<static_assert%>");
25849 return;
25850 case RT_DECLTYPE:
25851 cp_parser_error (parser, "expected %<decltype%>");
25852 return;
25853 case RT_OPERATOR:
25854 cp_parser_error (parser, "expected %<operator%>");
25855 return;
25856 case RT_CLASS:
25857 cp_parser_error (parser, "expected %<class%>");
25858 return;
25859 case RT_TEMPLATE:
25860 cp_parser_error (parser, "expected %<template%>");
25861 return;
25862 case RT_NAMESPACE:
25863 cp_parser_error (parser, "expected %<namespace%>");
25864 return;
25865 case RT_USING:
25866 cp_parser_error (parser, "expected %<using%>");
25867 return;
25868 case RT_ASM:
25869 cp_parser_error (parser, "expected %<asm%>");
25870 return;
25871 case RT_TRY:
25872 cp_parser_error (parser, "expected %<try%>");
25873 return;
25874 case RT_CATCH:
25875 cp_parser_error (parser, "expected %<catch%>");
25876 return;
25877 case RT_THROW:
25878 cp_parser_error (parser, "expected %<throw%>");
25879 return;
25880 case RT_LABEL:
25881 cp_parser_error (parser, "expected %<__label__%>");
25882 return;
25883 case RT_AT_TRY:
25884 cp_parser_error (parser, "expected %<@try%>");
25885 return;
25886 case RT_AT_SYNCHRONIZED:
25887 cp_parser_error (parser, "expected %<@synchronized%>");
25888 return;
25889 case RT_AT_THROW:
25890 cp_parser_error (parser, "expected %<@throw%>");
25891 return;
25892 case RT_TRANSACTION_ATOMIC:
25893 cp_parser_error (parser, "expected %<__transaction_atomic%>");
25894 return;
25895 case RT_TRANSACTION_RELAXED:
25896 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
25897 return;
25898 default:
25899 break;
25901 if (!keyword)
25903 switch (token_desc)
25905 case RT_SEMICOLON:
25906 cp_parser_error (parser, "expected %<;%>");
25907 return;
25908 case RT_OPEN_PAREN:
25909 cp_parser_error (parser, "expected %<(%>");
25910 return;
25911 case RT_CLOSE_BRACE:
25912 cp_parser_error (parser, "expected %<}%>");
25913 return;
25914 case RT_OPEN_BRACE:
25915 cp_parser_error (parser, "expected %<{%>");
25916 return;
25917 case RT_CLOSE_SQUARE:
25918 cp_parser_error (parser, "expected %<]%>");
25919 return;
25920 case RT_OPEN_SQUARE:
25921 cp_parser_error (parser, "expected %<[%>");
25922 return;
25923 case RT_COMMA:
25924 cp_parser_error (parser, "expected %<,%>");
25925 return;
25926 case RT_SCOPE:
25927 cp_parser_error (parser, "expected %<::%>");
25928 return;
25929 case RT_LESS:
25930 cp_parser_error (parser, "expected %<<%>");
25931 return;
25932 case RT_GREATER:
25933 cp_parser_error (parser, "expected %<>%>");
25934 return;
25935 case RT_EQ:
25936 cp_parser_error (parser, "expected %<=%>");
25937 return;
25938 case RT_ELLIPSIS:
25939 cp_parser_error (parser, "expected %<...%>");
25940 return;
25941 case RT_MULT:
25942 cp_parser_error (parser, "expected %<*%>");
25943 return;
25944 case RT_COMPL:
25945 cp_parser_error (parser, "expected %<~%>");
25946 return;
25947 case RT_COLON:
25948 cp_parser_error (parser, "expected %<:%>");
25949 return;
25950 case RT_COLON_SCOPE:
25951 cp_parser_error (parser, "expected %<:%> or %<::%>");
25952 return;
25953 case RT_CLOSE_PAREN:
25954 cp_parser_error (parser, "expected %<)%>");
25955 return;
25956 case RT_COMMA_CLOSE_PAREN:
25957 cp_parser_error (parser, "expected %<,%> or %<)%>");
25958 return;
25959 case RT_PRAGMA_EOL:
25960 cp_parser_error (parser, "expected end of line");
25961 return;
25962 case RT_NAME:
25963 cp_parser_error (parser, "expected identifier");
25964 return;
25965 case RT_SELECT:
25966 cp_parser_error (parser, "expected selection-statement");
25967 return;
25968 case RT_INTERATION:
25969 cp_parser_error (parser, "expected iteration-statement");
25970 return;
25971 case RT_JUMP:
25972 cp_parser_error (parser, "expected jump-statement");
25973 return;
25974 case RT_CLASS_KEY:
25975 cp_parser_error (parser, "expected class-key");
25976 return;
25977 case RT_CLASS_TYPENAME_TEMPLATE:
25978 cp_parser_error (parser,
25979 "expected %<class%>, %<typename%>, or %<template%>");
25980 return;
25981 default:
25982 gcc_unreachable ();
25985 else
25986 gcc_unreachable ();
25991 /* If the next token is of the indicated TYPE, consume it. Otherwise,
25992 issue an error message indicating that TOKEN_DESC was expected.
25994 Returns the token consumed, if the token had the appropriate type.
25995 Otherwise, returns NULL. */
25997 static cp_token *
25998 cp_parser_require (cp_parser* parser,
25999 enum cpp_ttype type,
26000 required_token token_desc)
26002 if (cp_lexer_next_token_is (parser->lexer, type))
26003 return cp_lexer_consume_token (parser->lexer);
26004 else
26006 /* Output the MESSAGE -- unless we're parsing tentatively. */
26007 if (!cp_parser_simulate_error (parser))
26008 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26009 return NULL;
26013 /* An error message is produced if the next token is not '>'.
26014 All further tokens are skipped until the desired token is
26015 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26017 static void
26018 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26020 /* Current level of '< ... >'. */
26021 unsigned level = 0;
26022 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26023 unsigned nesting_depth = 0;
26025 /* Are we ready, yet? If not, issue error message. */
26026 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26027 return;
26029 /* Skip tokens until the desired token is found. */
26030 while (true)
26032 /* Peek at the next token. */
26033 switch (cp_lexer_peek_token (parser->lexer)->type)
26035 case CPP_LESS:
26036 if (!nesting_depth)
26037 ++level;
26038 break;
26040 case CPP_RSHIFT:
26041 if (cxx_dialect == cxx98)
26042 /* C++0x views the `>>' operator as two `>' tokens, but
26043 C++98 does not. */
26044 break;
26045 else if (!nesting_depth && level-- == 0)
26047 /* We've hit a `>>' where the first `>' closes the
26048 template argument list, and the second `>' is
26049 spurious. Just consume the `>>' and stop; we've
26050 already produced at least one error. */
26051 cp_lexer_consume_token (parser->lexer);
26052 return;
26054 /* Fall through for C++0x, so we handle the second `>' in
26055 the `>>'. */
26057 case CPP_GREATER:
26058 if (!nesting_depth && level-- == 0)
26060 /* We've reached the token we want, consume it and stop. */
26061 cp_lexer_consume_token (parser->lexer);
26062 return;
26064 break;
26066 case CPP_OPEN_PAREN:
26067 case CPP_OPEN_SQUARE:
26068 ++nesting_depth;
26069 break;
26071 case CPP_CLOSE_PAREN:
26072 case CPP_CLOSE_SQUARE:
26073 if (nesting_depth-- == 0)
26074 return;
26075 break;
26077 case CPP_EOF:
26078 case CPP_PRAGMA_EOL:
26079 case CPP_SEMICOLON:
26080 case CPP_OPEN_BRACE:
26081 case CPP_CLOSE_BRACE:
26082 /* The '>' was probably forgotten, don't look further. */
26083 return;
26085 default:
26086 break;
26089 /* Consume this token. */
26090 cp_lexer_consume_token (parser->lexer);
26094 /* If the next token is the indicated keyword, consume it. Otherwise,
26095 issue an error message indicating that TOKEN_DESC was expected.
26097 Returns the token consumed, if the token had the appropriate type.
26098 Otherwise, returns NULL. */
26100 static cp_token *
26101 cp_parser_require_keyword (cp_parser* parser,
26102 enum rid keyword,
26103 required_token token_desc)
26105 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
26107 if (token && token->keyword != keyword)
26109 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
26110 return NULL;
26113 return token;
26116 /* Returns TRUE iff TOKEN is a token that can begin the body of a
26117 function-definition. */
26119 static bool
26120 cp_parser_token_starts_function_definition_p (cp_token* token)
26122 return (/* An ordinary function-body begins with an `{'. */
26123 token->type == CPP_OPEN_BRACE
26124 /* A ctor-initializer begins with a `:'. */
26125 || token->type == CPP_COLON
26126 /* A function-try-block begins with `try'. */
26127 || token->keyword == RID_TRY
26128 /* A function-transaction-block begins with `__transaction_atomic'
26129 or `__transaction_relaxed'. */
26130 || token->keyword == RID_TRANSACTION_ATOMIC
26131 || token->keyword == RID_TRANSACTION_RELAXED
26132 /* The named return value extension begins with `return'. */
26133 || token->keyword == RID_RETURN);
26136 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
26137 definition. */
26139 static bool
26140 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
26142 cp_token *token;
26144 token = cp_lexer_peek_token (parser->lexer);
26145 return (token->type == CPP_OPEN_BRACE
26146 || (token->type == CPP_COLON
26147 && !parser->colon_doesnt_start_class_def_p));
26150 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
26151 C++0x) ending a template-argument. */
26153 static bool
26154 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
26156 cp_token *token;
26158 token = cp_lexer_peek_token (parser->lexer);
26159 return (token->type == CPP_COMMA
26160 || token->type == CPP_GREATER
26161 || token->type == CPP_ELLIPSIS
26162 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
26165 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
26166 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
26168 static bool
26169 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
26170 size_t n)
26172 cp_token *token;
26174 token = cp_lexer_peek_nth_token (parser->lexer, n);
26175 if (token->type == CPP_LESS)
26176 return true;
26177 /* Check for the sequence `<::' in the original code. It would be lexed as
26178 `[:', where `[' is a digraph, and there is no whitespace before
26179 `:'. */
26180 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
26182 cp_token *token2;
26183 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
26184 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
26185 return true;
26187 return false;
26190 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
26191 or none_type otherwise. */
26193 static enum tag_types
26194 cp_parser_token_is_class_key (cp_token* token)
26196 switch (token->keyword)
26198 case RID_CLASS:
26199 return class_type;
26200 case RID_STRUCT:
26201 return record_type;
26202 case RID_UNION:
26203 return union_type;
26205 default:
26206 return none_type;
26210 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
26211 or none_type otherwise or if the token is null. */
26213 static enum tag_types
26214 cp_parser_token_is_type_parameter_key (cp_token* token)
26216 if (!token)
26217 return none_type;
26219 switch (token->keyword)
26221 case RID_CLASS:
26222 return class_type;
26223 case RID_TYPENAME:
26224 return typename_type;
26226 default:
26227 return none_type;
26231 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
26233 static void
26234 cp_parser_check_class_key (enum tag_types class_key, tree type)
26236 if (type == error_mark_node)
26237 return;
26238 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
26240 if (permerror (input_location, "%qs tag used in naming %q#T",
26241 class_key == union_type ? "union"
26242 : class_key == record_type ? "struct" : "class",
26243 type))
26244 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
26245 "%q#T was previously declared here", type);
26249 /* Issue an error message if DECL is redeclared with different
26250 access than its original declaration [class.access.spec/3].
26251 This applies to nested classes and nested class templates.
26252 [class.mem/1]. */
26254 static void
26255 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
26257 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
26258 return;
26260 if ((TREE_PRIVATE (decl)
26261 != (current_access_specifier == access_private_node))
26262 || (TREE_PROTECTED (decl)
26263 != (current_access_specifier == access_protected_node)))
26264 error_at (location, "%qD redeclared with different access", decl);
26267 /* Look for the `template' keyword, as a syntactic disambiguator.
26268 Return TRUE iff it is present, in which case it will be
26269 consumed. */
26271 static bool
26272 cp_parser_optional_template_keyword (cp_parser *parser)
26274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26276 /* In C++98 the `template' keyword can only be used within templates;
26277 outside templates the parser can always figure out what is a
26278 template and what is not. In C++11, per the resolution of DR 468,
26279 `template' is allowed in cases where it is not strictly necessary. */
26280 if (!processing_template_decl
26281 && pedantic && cxx_dialect == cxx98)
26283 cp_token *token = cp_lexer_peek_token (parser->lexer);
26284 pedwarn (token->location, OPT_Wpedantic,
26285 "in C++98 %<template%> (as a disambiguator) is only "
26286 "allowed within templates");
26287 /* If this part of the token stream is rescanned, the same
26288 error message would be generated. So, we purge the token
26289 from the stream. */
26290 cp_lexer_purge_token (parser->lexer);
26291 return false;
26293 else
26295 /* Consume the `template' keyword. */
26296 cp_lexer_consume_token (parser->lexer);
26297 return true;
26300 return false;
26303 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
26304 set PARSER->SCOPE, and perform other related actions. */
26306 static void
26307 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
26309 int i;
26310 struct tree_check *check_value;
26311 deferred_access_check *chk;
26312 vec<deferred_access_check, va_gc> *checks;
26314 /* Get the stored value. */
26315 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
26316 /* Perform any access checks that were deferred. */
26317 checks = check_value->checks;
26318 if (checks)
26320 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
26321 perform_or_defer_access_check (chk->binfo,
26322 chk->decl,
26323 chk->diag_decl, tf_warning_or_error);
26325 /* Set the scope from the stored value. */
26326 parser->scope = check_value->value;
26327 parser->qualifying_scope = check_value->qualifying_scope;
26328 parser->object_scope = NULL_TREE;
26331 /* Consume tokens up through a non-nested END token. Returns TRUE if we
26332 encounter the end of a block before what we were looking for. */
26334 static bool
26335 cp_parser_cache_group (cp_parser *parser,
26336 enum cpp_ttype end,
26337 unsigned depth)
26339 while (true)
26341 cp_token *token = cp_lexer_peek_token (parser->lexer);
26343 /* Abort a parenthesized expression if we encounter a semicolon. */
26344 if ((end == CPP_CLOSE_PAREN || depth == 0)
26345 && token->type == CPP_SEMICOLON)
26346 return true;
26347 /* If we've reached the end of the file, stop. */
26348 if (token->type == CPP_EOF
26349 || (end != CPP_PRAGMA_EOL
26350 && token->type == CPP_PRAGMA_EOL))
26351 return true;
26352 if (token->type == CPP_CLOSE_BRACE && depth == 0)
26353 /* We've hit the end of an enclosing block, so there's been some
26354 kind of syntax error. */
26355 return true;
26357 /* Consume the token. */
26358 cp_lexer_consume_token (parser->lexer);
26359 /* See if it starts a new group. */
26360 if (token->type == CPP_OPEN_BRACE)
26362 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
26363 /* In theory this should probably check end == '}', but
26364 cp_parser_save_member_function_body needs it to exit
26365 after either '}' or ')' when called with ')'. */
26366 if (depth == 0)
26367 return false;
26369 else if (token->type == CPP_OPEN_PAREN)
26371 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
26372 if (depth == 0 && end == CPP_CLOSE_PAREN)
26373 return false;
26375 else if (token->type == CPP_PRAGMA)
26376 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
26377 else if (token->type == end)
26378 return false;
26382 /* Like above, for caching a default argument or NSDMI. Both of these are
26383 terminated by a non-nested comma, but it can be unclear whether or not a
26384 comma is nested in a template argument list unless we do more parsing.
26385 In order to handle this ambiguity, when we encounter a ',' after a '<'
26386 we try to parse what follows as a parameter-declaration-list (in the
26387 case of a default argument) or a member-declarator (in the case of an
26388 NSDMI). If that succeeds, then we stop caching. */
26390 static tree
26391 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
26393 unsigned depth = 0;
26394 int maybe_template_id = 0;
26395 cp_token *first_token;
26396 cp_token *token;
26397 tree default_argument;
26399 /* Add tokens until we have processed the entire default
26400 argument. We add the range [first_token, token). */
26401 first_token = cp_lexer_peek_token (parser->lexer);
26402 if (first_token->type == CPP_OPEN_BRACE)
26404 /* For list-initialization, this is straightforward. */
26405 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26406 token = cp_lexer_peek_token (parser->lexer);
26408 else while (true)
26410 bool done = false;
26412 /* Peek at the next token. */
26413 token = cp_lexer_peek_token (parser->lexer);
26414 /* What we do depends on what token we have. */
26415 switch (token->type)
26417 /* In valid code, a default argument must be
26418 immediately followed by a `,' `)', or `...'. */
26419 case CPP_COMMA:
26420 if (depth == 0 && maybe_template_id)
26422 /* If we've seen a '<', we might be in a
26423 template-argument-list. Until Core issue 325 is
26424 resolved, we don't know how this situation ought
26425 to be handled, so try to DTRT. We check whether
26426 what comes after the comma is a valid parameter
26427 declaration list. If it is, then the comma ends
26428 the default argument; otherwise the default
26429 argument continues. */
26430 bool error = false;
26432 /* Set ITALP so cp_parser_parameter_declaration_list
26433 doesn't decide to commit to this parse. */
26434 bool saved_italp = parser->in_template_argument_list_p;
26435 parser->in_template_argument_list_p = true;
26437 cp_parser_parse_tentatively (parser);
26438 cp_lexer_consume_token (parser->lexer);
26440 if (nsdmi)
26442 int ctor_dtor_or_conv_p;
26443 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26444 &ctor_dtor_or_conv_p,
26445 /*parenthesized_p=*/NULL,
26446 /*member_p=*/true,
26447 /*friend_p=*/false);
26449 else
26451 begin_scope (sk_function_parms, NULL_TREE);
26452 cp_parser_parameter_declaration_list (parser, &error);
26453 pop_bindings_and_leave_scope ();
26455 if (!cp_parser_error_occurred (parser) && !error)
26456 done = true;
26457 cp_parser_abort_tentative_parse (parser);
26459 parser->in_template_argument_list_p = saved_italp;
26460 break;
26462 case CPP_CLOSE_PAREN:
26463 case CPP_ELLIPSIS:
26464 /* If we run into a non-nested `;', `}', or `]',
26465 then the code is invalid -- but the default
26466 argument is certainly over. */
26467 case CPP_SEMICOLON:
26468 case CPP_CLOSE_BRACE:
26469 case CPP_CLOSE_SQUARE:
26470 if (depth == 0
26471 /* Handle correctly int n = sizeof ... ( p ); */
26472 && token->type != CPP_ELLIPSIS)
26473 done = true;
26474 /* Update DEPTH, if necessary. */
26475 else if (token->type == CPP_CLOSE_PAREN
26476 || token->type == CPP_CLOSE_BRACE
26477 || token->type == CPP_CLOSE_SQUARE)
26478 --depth;
26479 break;
26481 case CPP_OPEN_PAREN:
26482 case CPP_OPEN_SQUARE:
26483 case CPP_OPEN_BRACE:
26484 ++depth;
26485 break;
26487 case CPP_LESS:
26488 if (depth == 0)
26489 /* This might be the comparison operator, or it might
26490 start a template argument list. */
26491 ++maybe_template_id;
26492 break;
26494 case CPP_RSHIFT:
26495 if (cxx_dialect == cxx98)
26496 break;
26497 /* Fall through for C++0x, which treats the `>>'
26498 operator like two `>' tokens in certain
26499 cases. */
26501 case CPP_GREATER:
26502 if (depth == 0)
26504 /* This might be an operator, or it might close a
26505 template argument list. But if a previous '<'
26506 started a template argument list, this will have
26507 closed it, so we can't be in one anymore. */
26508 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
26509 if (maybe_template_id < 0)
26510 maybe_template_id = 0;
26512 break;
26514 /* If we run out of tokens, issue an error message. */
26515 case CPP_EOF:
26516 case CPP_PRAGMA_EOL:
26517 error_at (token->location, "file ends in default argument");
26518 done = true;
26519 break;
26521 case CPP_NAME:
26522 case CPP_SCOPE:
26523 /* In these cases, we should look for template-ids.
26524 For example, if the default argument is
26525 `X<int, double>()', we need to do name lookup to
26526 figure out whether or not `X' is a template; if
26527 so, the `,' does not end the default argument.
26529 That is not yet done. */
26530 break;
26532 default:
26533 break;
26536 /* If we've reached the end, stop. */
26537 if (done)
26538 break;
26540 /* Add the token to the token block. */
26541 token = cp_lexer_consume_token (parser->lexer);
26544 /* Create a DEFAULT_ARG to represent the unparsed default
26545 argument. */
26546 default_argument = make_node (DEFAULT_ARG);
26547 DEFARG_TOKENS (default_argument)
26548 = cp_token_cache_new (first_token, token);
26549 DEFARG_INSTANTIATIONS (default_argument) = NULL;
26551 return default_argument;
26554 /* Begin parsing tentatively. We always save tokens while parsing
26555 tentatively so that if the tentative parsing fails we can restore the
26556 tokens. */
26558 static void
26559 cp_parser_parse_tentatively (cp_parser* parser)
26561 /* Enter a new parsing context. */
26562 parser->context = cp_parser_context_new (parser->context);
26563 /* Begin saving tokens. */
26564 cp_lexer_save_tokens (parser->lexer);
26565 /* In order to avoid repetitive access control error messages,
26566 access checks are queued up until we are no longer parsing
26567 tentatively. */
26568 push_deferring_access_checks (dk_deferred);
26571 /* Commit to the currently active tentative parse. */
26573 static void
26574 cp_parser_commit_to_tentative_parse (cp_parser* parser)
26576 cp_parser_context *context;
26577 cp_lexer *lexer;
26579 /* Mark all of the levels as committed. */
26580 lexer = parser->lexer;
26581 for (context = parser->context; context->next; context = context->next)
26583 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
26584 break;
26585 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
26586 while (!cp_lexer_saving_tokens (lexer))
26587 lexer = lexer->next;
26588 cp_lexer_commit_tokens (lexer);
26592 /* Commit to the topmost currently active tentative parse.
26594 Note that this function shouldn't be called when there are
26595 irreversible side-effects while in a tentative state. For
26596 example, we shouldn't create a permanent entry in the symbol
26597 table, or issue an error message that might not apply if the
26598 tentative parse is aborted. */
26600 static void
26601 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
26603 cp_parser_context *context = parser->context;
26604 cp_lexer *lexer = parser->lexer;
26606 if (context)
26608 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
26609 return;
26610 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
26612 while (!cp_lexer_saving_tokens (lexer))
26613 lexer = lexer->next;
26614 cp_lexer_commit_tokens (lexer);
26618 /* Abort the currently active tentative parse. All consumed tokens
26619 will be rolled back, and no diagnostics will be issued. */
26621 static void
26622 cp_parser_abort_tentative_parse (cp_parser* parser)
26624 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
26625 || errorcount > 0);
26626 cp_parser_simulate_error (parser);
26627 /* Now, pretend that we want to see if the construct was
26628 successfully parsed. */
26629 cp_parser_parse_definitely (parser);
26632 /* Stop parsing tentatively. If a parse error has occurred, restore the
26633 token stream. Otherwise, commit to the tokens we have consumed.
26634 Returns true if no error occurred; false otherwise. */
26636 static bool
26637 cp_parser_parse_definitely (cp_parser* parser)
26639 bool error_occurred;
26640 cp_parser_context *context;
26642 /* Remember whether or not an error occurred, since we are about to
26643 destroy that information. */
26644 error_occurred = cp_parser_error_occurred (parser);
26645 /* Remove the topmost context from the stack. */
26646 context = parser->context;
26647 parser->context = context->next;
26648 /* If no parse errors occurred, commit to the tentative parse. */
26649 if (!error_occurred)
26651 /* Commit to the tokens read tentatively, unless that was
26652 already done. */
26653 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
26654 cp_lexer_commit_tokens (parser->lexer);
26656 pop_to_parent_deferring_access_checks ();
26658 /* Otherwise, if errors occurred, roll back our state so that things
26659 are just as they were before we began the tentative parse. */
26660 else
26662 cp_lexer_rollback_tokens (parser->lexer);
26663 pop_deferring_access_checks ();
26665 /* Add the context to the front of the free list. */
26666 context->next = cp_parser_context_free_list;
26667 cp_parser_context_free_list = context;
26669 return !error_occurred;
26672 /* Returns true if we are parsing tentatively and are not committed to
26673 this tentative parse. */
26675 static bool
26676 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
26678 return (cp_parser_parsing_tentatively (parser)
26679 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
26682 /* Returns nonzero iff an error has occurred during the most recent
26683 tentative parse. */
26685 static bool
26686 cp_parser_error_occurred (cp_parser* parser)
26688 return (cp_parser_parsing_tentatively (parser)
26689 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
26692 /* Returns nonzero if GNU extensions are allowed. */
26694 static bool
26695 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
26697 return parser->allow_gnu_extensions_p;
26700 /* Objective-C++ Productions */
26703 /* Parse an Objective-C expression, which feeds into a primary-expression
26704 above.
26706 objc-expression:
26707 objc-message-expression
26708 objc-string-literal
26709 objc-encode-expression
26710 objc-protocol-expression
26711 objc-selector-expression
26713 Returns a tree representation of the expression. */
26715 static tree
26716 cp_parser_objc_expression (cp_parser* parser)
26718 /* Try to figure out what kind of declaration is present. */
26719 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26721 switch (kwd->type)
26723 case CPP_OPEN_SQUARE:
26724 return cp_parser_objc_message_expression (parser);
26726 case CPP_OBJC_STRING:
26727 kwd = cp_lexer_consume_token (parser->lexer);
26728 return objc_build_string_object (kwd->u.value);
26730 case CPP_KEYWORD:
26731 switch (kwd->keyword)
26733 case RID_AT_ENCODE:
26734 return cp_parser_objc_encode_expression (parser);
26736 case RID_AT_PROTOCOL:
26737 return cp_parser_objc_protocol_expression (parser);
26739 case RID_AT_SELECTOR:
26740 return cp_parser_objc_selector_expression (parser);
26742 default:
26743 break;
26745 default:
26746 error_at (kwd->location,
26747 "misplaced %<@%D%> Objective-C++ construct",
26748 kwd->u.value);
26749 cp_parser_skip_to_end_of_block_or_statement (parser);
26752 return error_mark_node;
26755 /* Parse an Objective-C message expression.
26757 objc-message-expression:
26758 [ objc-message-receiver objc-message-args ]
26760 Returns a representation of an Objective-C message. */
26762 static tree
26763 cp_parser_objc_message_expression (cp_parser* parser)
26765 tree receiver, messageargs;
26767 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
26768 receiver = cp_parser_objc_message_receiver (parser);
26769 messageargs = cp_parser_objc_message_args (parser);
26770 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26772 return objc_build_message_expr (receiver, messageargs);
26775 /* Parse an objc-message-receiver.
26777 objc-message-receiver:
26778 expression
26779 simple-type-specifier
26781 Returns a representation of the type or expression. */
26783 static tree
26784 cp_parser_objc_message_receiver (cp_parser* parser)
26786 tree rcv;
26788 /* An Objective-C message receiver may be either (1) a type
26789 or (2) an expression. */
26790 cp_parser_parse_tentatively (parser);
26791 rcv = cp_parser_expression (parser);
26793 /* If that worked out, fine. */
26794 if (cp_parser_parse_definitely (parser))
26795 return rcv;
26797 cp_parser_parse_tentatively (parser);
26798 rcv = cp_parser_simple_type_specifier (parser,
26799 /*decl_specs=*/NULL,
26800 CP_PARSER_FLAGS_NONE);
26802 if (cp_parser_parse_definitely (parser))
26803 return objc_get_class_reference (rcv);
26805 cp_parser_error (parser, "objective-c++ message receiver expected");
26806 return error_mark_node;
26809 /* Parse the arguments and selectors comprising an Objective-C message.
26811 objc-message-args:
26812 objc-selector
26813 objc-selector-args
26814 objc-selector-args , objc-comma-args
26816 objc-selector-args:
26817 objc-selector [opt] : assignment-expression
26818 objc-selector-args objc-selector [opt] : assignment-expression
26820 objc-comma-args:
26821 assignment-expression
26822 objc-comma-args , assignment-expression
26824 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
26825 selector arguments and TREE_VALUE containing a list of comma
26826 arguments. */
26828 static tree
26829 cp_parser_objc_message_args (cp_parser* parser)
26831 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
26832 bool maybe_unary_selector_p = true;
26833 cp_token *token = cp_lexer_peek_token (parser->lexer);
26835 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26837 tree selector = NULL_TREE, arg;
26839 if (token->type != CPP_COLON)
26840 selector = cp_parser_objc_selector (parser);
26842 /* Detect if we have a unary selector. */
26843 if (maybe_unary_selector_p
26844 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26845 return build_tree_list (selector, NULL_TREE);
26847 maybe_unary_selector_p = false;
26848 cp_parser_require (parser, CPP_COLON, RT_COLON);
26849 arg = cp_parser_assignment_expression (parser);
26851 sel_args
26852 = chainon (sel_args,
26853 build_tree_list (selector, arg));
26855 token = cp_lexer_peek_token (parser->lexer);
26858 /* Handle non-selector arguments, if any. */
26859 while (token->type == CPP_COMMA)
26861 tree arg;
26863 cp_lexer_consume_token (parser->lexer);
26864 arg = cp_parser_assignment_expression (parser);
26866 addl_args
26867 = chainon (addl_args,
26868 build_tree_list (NULL_TREE, arg));
26870 token = cp_lexer_peek_token (parser->lexer);
26873 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
26875 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
26876 return build_tree_list (error_mark_node, error_mark_node);
26879 return build_tree_list (sel_args, addl_args);
26882 /* Parse an Objective-C encode expression.
26884 objc-encode-expression:
26885 @encode objc-typename
26887 Returns an encoded representation of the type argument. */
26889 static tree
26890 cp_parser_objc_encode_expression (cp_parser* parser)
26892 tree type;
26893 cp_token *token;
26895 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
26896 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26897 token = cp_lexer_peek_token (parser->lexer);
26898 type = complete_type (cp_parser_type_id (parser));
26899 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26901 if (!type)
26903 error_at (token->location,
26904 "%<@encode%> must specify a type as an argument");
26905 return error_mark_node;
26908 /* This happens if we find @encode(T) (where T is a template
26909 typename or something dependent on a template typename) when
26910 parsing a template. In that case, we can't compile it
26911 immediately, but we rather create an AT_ENCODE_EXPR which will
26912 need to be instantiated when the template is used.
26914 if (dependent_type_p (type))
26916 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
26917 TREE_READONLY (value) = 1;
26918 return value;
26921 return objc_build_encode_expr (type);
26924 /* Parse an Objective-C @defs expression. */
26926 static tree
26927 cp_parser_objc_defs_expression (cp_parser *parser)
26929 tree name;
26931 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
26932 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26933 name = cp_parser_identifier (parser);
26934 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26936 return objc_get_class_ivars (name);
26939 /* Parse an Objective-C protocol expression.
26941 objc-protocol-expression:
26942 @protocol ( identifier )
26944 Returns a representation of the protocol expression. */
26946 static tree
26947 cp_parser_objc_protocol_expression (cp_parser* parser)
26949 tree proto;
26951 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26952 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26953 proto = cp_parser_identifier (parser);
26954 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26956 return objc_build_protocol_expr (proto);
26959 /* Parse an Objective-C selector expression.
26961 objc-selector-expression:
26962 @selector ( objc-method-signature )
26964 objc-method-signature:
26965 objc-selector
26966 objc-selector-seq
26968 objc-selector-seq:
26969 objc-selector :
26970 objc-selector-seq objc-selector :
26972 Returns a representation of the method selector. */
26974 static tree
26975 cp_parser_objc_selector_expression (cp_parser* parser)
26977 tree sel_seq = NULL_TREE;
26978 bool maybe_unary_selector_p = true;
26979 cp_token *token;
26980 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26982 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
26983 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26984 token = cp_lexer_peek_token (parser->lexer);
26986 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
26987 || token->type == CPP_SCOPE)
26989 tree selector = NULL_TREE;
26991 if (token->type != CPP_COLON
26992 || token->type == CPP_SCOPE)
26993 selector = cp_parser_objc_selector (parser);
26995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
26996 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
26998 /* Detect if we have a unary selector. */
26999 if (maybe_unary_selector_p)
27001 sel_seq = selector;
27002 goto finish_selector;
27004 else
27006 cp_parser_error (parser, "expected %<:%>");
27009 maybe_unary_selector_p = false;
27010 token = cp_lexer_consume_token (parser->lexer);
27012 if (token->type == CPP_SCOPE)
27014 sel_seq
27015 = chainon (sel_seq,
27016 build_tree_list (selector, NULL_TREE));
27017 sel_seq
27018 = chainon (sel_seq,
27019 build_tree_list (NULL_TREE, NULL_TREE));
27021 else
27022 sel_seq
27023 = chainon (sel_seq,
27024 build_tree_list (selector, NULL_TREE));
27026 token = cp_lexer_peek_token (parser->lexer);
27029 finish_selector:
27030 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27032 return objc_build_selector_expr (loc, sel_seq);
27035 /* Parse a list of identifiers.
27037 objc-identifier-list:
27038 identifier
27039 objc-identifier-list , identifier
27041 Returns a TREE_LIST of identifier nodes. */
27043 static tree
27044 cp_parser_objc_identifier_list (cp_parser* parser)
27046 tree identifier;
27047 tree list;
27048 cp_token *sep;
27050 identifier = cp_parser_identifier (parser);
27051 if (identifier == error_mark_node)
27052 return error_mark_node;
27054 list = build_tree_list (NULL_TREE, identifier);
27055 sep = cp_lexer_peek_token (parser->lexer);
27057 while (sep->type == CPP_COMMA)
27059 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27060 identifier = cp_parser_identifier (parser);
27061 if (identifier == error_mark_node)
27062 return list;
27064 list = chainon (list, build_tree_list (NULL_TREE,
27065 identifier));
27066 sep = cp_lexer_peek_token (parser->lexer);
27069 return list;
27072 /* Parse an Objective-C alias declaration.
27074 objc-alias-declaration:
27075 @compatibility_alias identifier identifier ;
27077 This function registers the alias mapping with the Objective-C front end.
27078 It returns nothing. */
27080 static void
27081 cp_parser_objc_alias_declaration (cp_parser* parser)
27083 tree alias, orig;
27085 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
27086 alias = cp_parser_identifier (parser);
27087 orig = cp_parser_identifier (parser);
27088 objc_declare_alias (alias, orig);
27089 cp_parser_consume_semicolon_at_end_of_statement (parser);
27092 /* Parse an Objective-C class forward-declaration.
27094 objc-class-declaration:
27095 @class objc-identifier-list ;
27097 The function registers the forward declarations with the Objective-C
27098 front end. It returns nothing. */
27100 static void
27101 cp_parser_objc_class_declaration (cp_parser* parser)
27103 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
27104 while (true)
27106 tree id;
27108 id = cp_parser_identifier (parser);
27109 if (id == error_mark_node)
27110 break;
27112 objc_declare_class (id);
27114 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27115 cp_lexer_consume_token (parser->lexer);
27116 else
27117 break;
27119 cp_parser_consume_semicolon_at_end_of_statement (parser);
27122 /* Parse a list of Objective-C protocol references.
27124 objc-protocol-refs-opt:
27125 objc-protocol-refs [opt]
27127 objc-protocol-refs:
27128 < objc-identifier-list >
27130 Returns a TREE_LIST of identifiers, if any. */
27132 static tree
27133 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
27135 tree protorefs = NULL_TREE;
27137 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
27139 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
27140 protorefs = cp_parser_objc_identifier_list (parser);
27141 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
27144 return protorefs;
27147 /* Parse a Objective-C visibility specification. */
27149 static void
27150 cp_parser_objc_visibility_spec (cp_parser* parser)
27152 cp_token *vis = cp_lexer_peek_token (parser->lexer);
27154 switch (vis->keyword)
27156 case RID_AT_PRIVATE:
27157 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27158 break;
27159 case RID_AT_PROTECTED:
27160 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27161 break;
27162 case RID_AT_PUBLIC:
27163 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
27164 break;
27165 case RID_AT_PACKAGE:
27166 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27167 break;
27168 default:
27169 return;
27172 /* Eat '@private'/'@protected'/'@public'. */
27173 cp_lexer_consume_token (parser->lexer);
27176 /* Parse an Objective-C method type. Return 'true' if it is a class
27177 (+) method, and 'false' if it is an instance (-) method. */
27179 static inline bool
27180 cp_parser_objc_method_type (cp_parser* parser)
27182 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
27183 return true;
27184 else
27185 return false;
27188 /* Parse an Objective-C protocol qualifier. */
27190 static tree
27191 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
27193 tree quals = NULL_TREE, node;
27194 cp_token *token = cp_lexer_peek_token (parser->lexer);
27196 node = token->u.value;
27198 while (node && identifier_p (node)
27199 && (node == ridpointers [(int) RID_IN]
27200 || node == ridpointers [(int) RID_OUT]
27201 || node == ridpointers [(int) RID_INOUT]
27202 || node == ridpointers [(int) RID_BYCOPY]
27203 || node == ridpointers [(int) RID_BYREF]
27204 || node == ridpointers [(int) RID_ONEWAY]))
27206 quals = tree_cons (NULL_TREE, node, quals);
27207 cp_lexer_consume_token (parser->lexer);
27208 token = cp_lexer_peek_token (parser->lexer);
27209 node = token->u.value;
27212 return quals;
27215 /* Parse an Objective-C typename. */
27217 static tree
27218 cp_parser_objc_typename (cp_parser* parser)
27220 tree type_name = NULL_TREE;
27222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27224 tree proto_quals, cp_type = NULL_TREE;
27226 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27227 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
27229 /* An ObjC type name may consist of just protocol qualifiers, in which
27230 case the type shall default to 'id'. */
27231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27233 cp_type = cp_parser_type_id (parser);
27235 /* If the type could not be parsed, an error has already
27236 been produced. For error recovery, behave as if it had
27237 not been specified, which will use the default type
27238 'id'. */
27239 if (cp_type == error_mark_node)
27241 cp_type = NULL_TREE;
27242 /* We need to skip to the closing parenthesis as
27243 cp_parser_type_id() does not seem to do it for
27244 us. */
27245 cp_parser_skip_to_closing_parenthesis (parser,
27246 /*recovering=*/true,
27247 /*or_comma=*/false,
27248 /*consume_paren=*/false);
27252 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27253 type_name = build_tree_list (proto_quals, cp_type);
27256 return type_name;
27259 /* Check to see if TYPE refers to an Objective-C selector name. */
27261 static bool
27262 cp_parser_objc_selector_p (enum cpp_ttype type)
27264 return (type == CPP_NAME || type == CPP_KEYWORD
27265 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
27266 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
27267 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
27268 || type == CPP_XOR || type == CPP_XOR_EQ);
27271 /* Parse an Objective-C selector. */
27273 static tree
27274 cp_parser_objc_selector (cp_parser* parser)
27276 cp_token *token = cp_lexer_consume_token (parser->lexer);
27278 if (!cp_parser_objc_selector_p (token->type))
27280 error_at (token->location, "invalid Objective-C++ selector name");
27281 return error_mark_node;
27284 /* C++ operator names are allowed to appear in ObjC selectors. */
27285 switch (token->type)
27287 case CPP_AND_AND: return get_identifier ("and");
27288 case CPP_AND_EQ: return get_identifier ("and_eq");
27289 case CPP_AND: return get_identifier ("bitand");
27290 case CPP_OR: return get_identifier ("bitor");
27291 case CPP_COMPL: return get_identifier ("compl");
27292 case CPP_NOT: return get_identifier ("not");
27293 case CPP_NOT_EQ: return get_identifier ("not_eq");
27294 case CPP_OR_OR: return get_identifier ("or");
27295 case CPP_OR_EQ: return get_identifier ("or_eq");
27296 case CPP_XOR: return get_identifier ("xor");
27297 case CPP_XOR_EQ: return get_identifier ("xor_eq");
27298 default: return token->u.value;
27302 /* Parse an Objective-C params list. */
27304 static tree
27305 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
27307 tree params = NULL_TREE;
27308 bool maybe_unary_selector_p = true;
27309 cp_token *token = cp_lexer_peek_token (parser->lexer);
27311 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27313 tree selector = NULL_TREE, type_name, identifier;
27314 tree parm_attr = NULL_TREE;
27316 if (token->keyword == RID_ATTRIBUTE)
27317 break;
27319 if (token->type != CPP_COLON)
27320 selector = cp_parser_objc_selector (parser);
27322 /* Detect if we have a unary selector. */
27323 if (maybe_unary_selector_p
27324 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27326 params = selector; /* Might be followed by attributes. */
27327 break;
27330 maybe_unary_selector_p = false;
27331 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27333 /* Something went quite wrong. There should be a colon
27334 here, but there is not. Stop parsing parameters. */
27335 break;
27337 type_name = cp_parser_objc_typename (parser);
27338 /* New ObjC allows attributes on parameters too. */
27339 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27340 parm_attr = cp_parser_attributes_opt (parser);
27341 identifier = cp_parser_identifier (parser);
27343 params
27344 = chainon (params,
27345 objc_build_keyword_decl (selector,
27346 type_name,
27347 identifier,
27348 parm_attr));
27350 token = cp_lexer_peek_token (parser->lexer);
27353 if (params == NULL_TREE)
27355 cp_parser_error (parser, "objective-c++ method declaration is expected");
27356 return error_mark_node;
27359 /* We allow tail attributes for the method. */
27360 if (token->keyword == RID_ATTRIBUTE)
27362 *attributes = cp_parser_attributes_opt (parser);
27363 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27364 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27365 return params;
27366 cp_parser_error (parser,
27367 "method attributes must be specified at the end");
27368 return error_mark_node;
27371 if (params == NULL_TREE)
27373 cp_parser_error (parser, "objective-c++ method declaration is expected");
27374 return error_mark_node;
27376 return params;
27379 /* Parse the non-keyword Objective-C params. */
27381 static tree
27382 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
27383 tree* attributes)
27385 tree params = make_node (TREE_LIST);
27386 cp_token *token = cp_lexer_peek_token (parser->lexer);
27387 *ellipsisp = false; /* Initially, assume no ellipsis. */
27389 while (token->type == CPP_COMMA)
27391 cp_parameter_declarator *parmdecl;
27392 tree parm;
27394 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27395 token = cp_lexer_peek_token (parser->lexer);
27397 if (token->type == CPP_ELLIPSIS)
27399 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
27400 *ellipsisp = true;
27401 token = cp_lexer_peek_token (parser->lexer);
27402 break;
27405 /* TODO: parse attributes for tail parameters. */
27406 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
27407 parm = grokdeclarator (parmdecl->declarator,
27408 &parmdecl->decl_specifiers,
27409 PARM, /*initialized=*/0,
27410 /*attrlist=*/NULL);
27412 chainon (params, build_tree_list (NULL_TREE, parm));
27413 token = cp_lexer_peek_token (parser->lexer);
27416 /* We allow tail attributes for the method. */
27417 if (token->keyword == RID_ATTRIBUTE)
27419 if (*attributes == NULL_TREE)
27421 *attributes = cp_parser_attributes_opt (parser);
27422 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27423 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27424 return params;
27426 else
27427 /* We have an error, but parse the attributes, so that we can
27428 carry on. */
27429 *attributes = cp_parser_attributes_opt (parser);
27431 cp_parser_error (parser,
27432 "method attributes must be specified at the end");
27433 return error_mark_node;
27436 return params;
27439 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
27441 static void
27442 cp_parser_objc_interstitial_code (cp_parser* parser)
27444 cp_token *token = cp_lexer_peek_token (parser->lexer);
27446 /* If the next token is `extern' and the following token is a string
27447 literal, then we have a linkage specification. */
27448 if (token->keyword == RID_EXTERN
27449 && cp_parser_is_pure_string_literal
27450 (cp_lexer_peek_nth_token (parser->lexer, 2)))
27451 cp_parser_linkage_specification (parser);
27452 /* Handle #pragma, if any. */
27453 else if (token->type == CPP_PRAGMA)
27454 cp_parser_pragma (parser, pragma_objc_icode);
27455 /* Allow stray semicolons. */
27456 else if (token->type == CPP_SEMICOLON)
27457 cp_lexer_consume_token (parser->lexer);
27458 /* Mark methods as optional or required, when building protocols. */
27459 else if (token->keyword == RID_AT_OPTIONAL)
27461 cp_lexer_consume_token (parser->lexer);
27462 objc_set_method_opt (true);
27464 else if (token->keyword == RID_AT_REQUIRED)
27466 cp_lexer_consume_token (parser->lexer);
27467 objc_set_method_opt (false);
27469 else if (token->keyword == RID_NAMESPACE)
27470 cp_parser_namespace_definition (parser);
27471 /* Other stray characters must generate errors. */
27472 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
27474 cp_lexer_consume_token (parser->lexer);
27475 error ("stray %qs between Objective-C++ methods",
27476 token->type == CPP_OPEN_BRACE ? "{" : "}");
27478 /* Finally, try to parse a block-declaration, or a function-definition. */
27479 else
27480 cp_parser_block_declaration (parser, /*statement_p=*/false);
27483 /* Parse a method signature. */
27485 static tree
27486 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
27488 tree rettype, kwdparms, optparms;
27489 bool ellipsis = false;
27490 bool is_class_method;
27492 is_class_method = cp_parser_objc_method_type (parser);
27493 rettype = cp_parser_objc_typename (parser);
27494 *attributes = NULL_TREE;
27495 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
27496 if (kwdparms == error_mark_node)
27497 return error_mark_node;
27498 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
27499 if (optparms == error_mark_node)
27500 return error_mark_node;
27502 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
27505 static bool
27506 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
27508 tree tattr;
27509 cp_lexer_save_tokens (parser->lexer);
27510 tattr = cp_parser_attributes_opt (parser);
27511 gcc_assert (tattr) ;
27513 /* If the attributes are followed by a method introducer, this is not allowed.
27514 Dump the attributes and flag the situation. */
27515 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
27516 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
27517 return true;
27519 /* Otherwise, the attributes introduce some interstitial code, possibly so
27520 rewind to allow that check. */
27521 cp_lexer_rollback_tokens (parser->lexer);
27522 return false;
27525 /* Parse an Objective-C method prototype list. */
27527 static void
27528 cp_parser_objc_method_prototype_list (cp_parser* parser)
27530 cp_token *token = cp_lexer_peek_token (parser->lexer);
27532 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27534 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27536 tree attributes, sig;
27537 bool is_class_method;
27538 if (token->type == CPP_PLUS)
27539 is_class_method = true;
27540 else
27541 is_class_method = false;
27542 sig = cp_parser_objc_method_signature (parser, &attributes);
27543 if (sig == error_mark_node)
27545 cp_parser_skip_to_end_of_block_or_statement (parser);
27546 token = cp_lexer_peek_token (parser->lexer);
27547 continue;
27549 objc_add_method_declaration (is_class_method, sig, attributes);
27550 cp_parser_consume_semicolon_at_end_of_statement (parser);
27552 else if (token->keyword == RID_AT_PROPERTY)
27553 cp_parser_objc_at_property_declaration (parser);
27554 else if (token->keyword == RID_ATTRIBUTE
27555 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
27556 warning_at (cp_lexer_peek_token (parser->lexer)->location,
27557 OPT_Wattributes,
27558 "prefix attributes are ignored for methods");
27559 else
27560 /* Allow for interspersed non-ObjC++ code. */
27561 cp_parser_objc_interstitial_code (parser);
27563 token = cp_lexer_peek_token (parser->lexer);
27566 if (token->type != CPP_EOF)
27567 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27568 else
27569 cp_parser_error (parser, "expected %<@end%>");
27571 objc_finish_interface ();
27574 /* Parse an Objective-C method definition list. */
27576 static void
27577 cp_parser_objc_method_definition_list (cp_parser* parser)
27579 cp_token *token = cp_lexer_peek_token (parser->lexer);
27581 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27583 tree meth;
27585 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27587 cp_token *ptk;
27588 tree sig, attribute;
27589 bool is_class_method;
27590 if (token->type == CPP_PLUS)
27591 is_class_method = true;
27592 else
27593 is_class_method = false;
27594 push_deferring_access_checks (dk_deferred);
27595 sig = cp_parser_objc_method_signature (parser, &attribute);
27596 if (sig == error_mark_node)
27598 cp_parser_skip_to_end_of_block_or_statement (parser);
27599 token = cp_lexer_peek_token (parser->lexer);
27600 continue;
27602 objc_start_method_definition (is_class_method, sig, attribute,
27603 NULL_TREE);
27605 /* For historical reasons, we accept an optional semicolon. */
27606 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27607 cp_lexer_consume_token (parser->lexer);
27609 ptk = cp_lexer_peek_token (parser->lexer);
27610 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
27611 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
27613 perform_deferred_access_checks (tf_warning_or_error);
27614 stop_deferring_access_checks ();
27615 meth = cp_parser_function_definition_after_declarator (parser,
27616 false);
27617 pop_deferring_access_checks ();
27618 objc_finish_method_definition (meth);
27621 /* The following case will be removed once @synthesize is
27622 completely implemented. */
27623 else if (token->keyword == RID_AT_PROPERTY)
27624 cp_parser_objc_at_property_declaration (parser);
27625 else if (token->keyword == RID_AT_SYNTHESIZE)
27626 cp_parser_objc_at_synthesize_declaration (parser);
27627 else if (token->keyword == RID_AT_DYNAMIC)
27628 cp_parser_objc_at_dynamic_declaration (parser);
27629 else if (token->keyword == RID_ATTRIBUTE
27630 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
27631 warning_at (token->location, OPT_Wattributes,
27632 "prefix attributes are ignored for methods");
27633 else
27634 /* Allow for interspersed non-ObjC++ code. */
27635 cp_parser_objc_interstitial_code (parser);
27637 token = cp_lexer_peek_token (parser->lexer);
27640 if (token->type != CPP_EOF)
27641 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27642 else
27643 cp_parser_error (parser, "expected %<@end%>");
27645 objc_finish_implementation ();
27648 /* Parse Objective-C ivars. */
27650 static void
27651 cp_parser_objc_class_ivars (cp_parser* parser)
27653 cp_token *token = cp_lexer_peek_token (parser->lexer);
27655 if (token->type != CPP_OPEN_BRACE)
27656 return; /* No ivars specified. */
27658 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
27659 token = cp_lexer_peek_token (parser->lexer);
27661 while (token->type != CPP_CLOSE_BRACE
27662 && token->keyword != RID_AT_END && token->type != CPP_EOF)
27664 cp_decl_specifier_seq declspecs;
27665 int decl_class_or_enum_p;
27666 tree prefix_attributes;
27668 cp_parser_objc_visibility_spec (parser);
27670 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27671 break;
27673 cp_parser_decl_specifier_seq (parser,
27674 CP_PARSER_FLAGS_OPTIONAL,
27675 &declspecs,
27676 &decl_class_or_enum_p);
27678 /* auto, register, static, extern, mutable. */
27679 if (declspecs.storage_class != sc_none)
27681 cp_parser_error (parser, "invalid type for instance variable");
27682 declspecs.storage_class = sc_none;
27685 /* thread_local. */
27686 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27688 cp_parser_error (parser, "invalid type for instance variable");
27689 declspecs.locations[ds_thread] = 0;
27692 /* typedef. */
27693 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27695 cp_parser_error (parser, "invalid type for instance variable");
27696 declspecs.locations[ds_typedef] = 0;
27699 prefix_attributes = declspecs.attributes;
27700 declspecs.attributes = NULL_TREE;
27702 /* Keep going until we hit the `;' at the end of the
27703 declaration. */
27704 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27706 tree width = NULL_TREE, attributes, first_attribute, decl;
27707 cp_declarator *declarator = NULL;
27708 int ctor_dtor_or_conv_p;
27710 /* Check for a (possibly unnamed) bitfield declaration. */
27711 token = cp_lexer_peek_token (parser->lexer);
27712 if (token->type == CPP_COLON)
27713 goto eat_colon;
27715 if (token->type == CPP_NAME
27716 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
27717 == CPP_COLON))
27719 /* Get the name of the bitfield. */
27720 declarator = make_id_declarator (NULL_TREE,
27721 cp_parser_identifier (parser),
27722 sfk_none);
27724 eat_colon:
27725 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
27726 /* Get the width of the bitfield. */
27727 width
27728 = cp_parser_constant_expression (parser);
27730 else
27732 /* Parse the declarator. */
27733 declarator
27734 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27735 &ctor_dtor_or_conv_p,
27736 /*parenthesized_p=*/NULL,
27737 /*member_p=*/false,
27738 /*friend_p=*/false);
27741 /* Look for attributes that apply to the ivar. */
27742 attributes = cp_parser_attributes_opt (parser);
27743 /* Remember which attributes are prefix attributes and
27744 which are not. */
27745 first_attribute = attributes;
27746 /* Combine the attributes. */
27747 attributes = chainon (prefix_attributes, attributes);
27749 if (width)
27750 /* Create the bitfield declaration. */
27751 decl = grokbitfield (declarator, &declspecs,
27752 width,
27753 attributes);
27754 else
27755 decl = grokfield (declarator, &declspecs,
27756 NULL_TREE, /*init_const_expr_p=*/false,
27757 NULL_TREE, attributes);
27759 /* Add the instance variable. */
27760 if (decl != error_mark_node && decl != NULL_TREE)
27761 objc_add_instance_variable (decl);
27763 /* Reset PREFIX_ATTRIBUTES. */
27764 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27765 attributes = TREE_CHAIN (attributes);
27766 if (attributes)
27767 TREE_CHAIN (attributes) = NULL_TREE;
27769 token = cp_lexer_peek_token (parser->lexer);
27771 if (token->type == CPP_COMMA)
27773 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27774 continue;
27776 break;
27779 cp_parser_consume_semicolon_at_end_of_statement (parser);
27780 token = cp_lexer_peek_token (parser->lexer);
27783 if (token->keyword == RID_AT_END)
27784 cp_parser_error (parser, "expected %<}%>");
27786 /* Do not consume the RID_AT_END, so it will be read again as terminating
27787 the @interface of @implementation. */
27788 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
27789 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
27791 /* For historical reasons, we accept an optional semicolon. */
27792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27793 cp_lexer_consume_token (parser->lexer);
27796 /* Parse an Objective-C protocol declaration. */
27798 static void
27799 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
27801 tree proto, protorefs;
27802 cp_token *tok;
27804 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
27807 tok = cp_lexer_peek_token (parser->lexer);
27808 error_at (tok->location, "identifier expected after %<@protocol%>");
27809 cp_parser_consume_semicolon_at_end_of_statement (parser);
27810 return;
27813 /* See if we have a forward declaration or a definition. */
27814 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
27816 /* Try a forward declaration first. */
27817 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
27819 while (true)
27821 tree id;
27823 id = cp_parser_identifier (parser);
27824 if (id == error_mark_node)
27825 break;
27827 objc_declare_protocol (id, attributes);
27829 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27830 cp_lexer_consume_token (parser->lexer);
27831 else
27832 break;
27834 cp_parser_consume_semicolon_at_end_of_statement (parser);
27837 /* Ok, we got a full-fledged definition (or at least should). */
27838 else
27840 proto = cp_parser_identifier (parser);
27841 protorefs = cp_parser_objc_protocol_refs_opt (parser);
27842 objc_start_protocol (proto, protorefs, attributes);
27843 cp_parser_objc_method_prototype_list (parser);
27847 /* Parse an Objective-C superclass or category. */
27849 static void
27850 cp_parser_objc_superclass_or_category (cp_parser *parser,
27851 bool iface_p,
27852 tree *super,
27853 tree *categ, bool *is_class_extension)
27855 cp_token *next = cp_lexer_peek_token (parser->lexer);
27857 *super = *categ = NULL_TREE;
27858 *is_class_extension = false;
27859 if (next->type == CPP_COLON)
27861 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
27862 *super = cp_parser_identifier (parser);
27864 else if (next->type == CPP_OPEN_PAREN)
27866 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27868 /* If there is no category name, and this is an @interface, we
27869 have a class extension. */
27870 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27872 *categ = NULL_TREE;
27873 *is_class_extension = true;
27875 else
27876 *categ = cp_parser_identifier (parser);
27878 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27882 /* Parse an Objective-C class interface. */
27884 static void
27885 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
27887 tree name, super, categ, protos;
27888 bool is_class_extension;
27890 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
27891 name = cp_parser_identifier (parser);
27892 if (name == error_mark_node)
27894 /* It's hard to recover because even if valid @interface stuff
27895 is to follow, we can't compile it (or validate it) if we
27896 don't even know which class it refers to. Let's assume this
27897 was a stray '@interface' token in the stream and skip it.
27899 return;
27901 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
27902 &is_class_extension);
27903 protos = cp_parser_objc_protocol_refs_opt (parser);
27905 /* We have either a class or a category on our hands. */
27906 if (categ || is_class_extension)
27907 objc_start_category_interface (name, categ, protos, attributes);
27908 else
27910 objc_start_class_interface (name, super, protos, attributes);
27911 /* Handle instance variable declarations, if any. */
27912 cp_parser_objc_class_ivars (parser);
27913 objc_continue_interface ();
27916 cp_parser_objc_method_prototype_list (parser);
27919 /* Parse an Objective-C class implementation. */
27921 static void
27922 cp_parser_objc_class_implementation (cp_parser* parser)
27924 tree name, super, categ;
27925 bool is_class_extension;
27927 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
27928 name = cp_parser_identifier (parser);
27929 if (name == error_mark_node)
27931 /* It's hard to recover because even if valid @implementation
27932 stuff is to follow, we can't compile it (or validate it) if
27933 we don't even know which class it refers to. Let's assume
27934 this was a stray '@implementation' token in the stream and
27935 skip it.
27937 return;
27939 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
27940 &is_class_extension);
27942 /* We have either a class or a category on our hands. */
27943 if (categ)
27944 objc_start_category_implementation (name, categ);
27945 else
27947 objc_start_class_implementation (name, super);
27948 /* Handle instance variable declarations, if any. */
27949 cp_parser_objc_class_ivars (parser);
27950 objc_continue_implementation ();
27953 cp_parser_objc_method_definition_list (parser);
27956 /* Consume the @end token and finish off the implementation. */
27958 static void
27959 cp_parser_objc_end_implementation (cp_parser* parser)
27961 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27962 objc_finish_implementation ();
27965 /* Parse an Objective-C declaration. */
27967 static void
27968 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
27970 /* Try to figure out what kind of declaration is present. */
27971 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27973 if (attributes)
27974 switch (kwd->keyword)
27976 case RID_AT_ALIAS:
27977 case RID_AT_CLASS:
27978 case RID_AT_END:
27979 error_at (kwd->location, "attributes may not be specified before"
27980 " the %<@%D%> Objective-C++ keyword",
27981 kwd->u.value);
27982 attributes = NULL;
27983 break;
27984 case RID_AT_IMPLEMENTATION:
27985 warning_at (kwd->location, OPT_Wattributes,
27986 "prefix attributes are ignored before %<@%D%>",
27987 kwd->u.value);
27988 attributes = NULL;
27989 default:
27990 break;
27993 switch (kwd->keyword)
27995 case RID_AT_ALIAS:
27996 cp_parser_objc_alias_declaration (parser);
27997 break;
27998 case RID_AT_CLASS:
27999 cp_parser_objc_class_declaration (parser);
28000 break;
28001 case RID_AT_PROTOCOL:
28002 cp_parser_objc_protocol_declaration (parser, attributes);
28003 break;
28004 case RID_AT_INTERFACE:
28005 cp_parser_objc_class_interface (parser, attributes);
28006 break;
28007 case RID_AT_IMPLEMENTATION:
28008 cp_parser_objc_class_implementation (parser);
28009 break;
28010 case RID_AT_END:
28011 cp_parser_objc_end_implementation (parser);
28012 break;
28013 default:
28014 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28015 kwd->u.value);
28016 cp_parser_skip_to_end_of_block_or_statement (parser);
28020 /* Parse an Objective-C try-catch-finally statement.
28022 objc-try-catch-finally-stmt:
28023 @try compound-statement objc-catch-clause-seq [opt]
28024 objc-finally-clause [opt]
28026 objc-catch-clause-seq:
28027 objc-catch-clause objc-catch-clause-seq [opt]
28029 objc-catch-clause:
28030 @catch ( objc-exception-declaration ) compound-statement
28032 objc-finally-clause:
28033 @finally compound-statement
28035 objc-exception-declaration:
28036 parameter-declaration
28037 '...'
28039 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28041 Returns NULL_TREE.
28043 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28044 for C. Keep them in sync. */
28046 static tree
28047 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
28049 location_t location;
28050 tree stmt;
28052 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
28053 location = cp_lexer_peek_token (parser->lexer)->location;
28054 objc_maybe_warn_exceptions (location);
28055 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
28056 node, lest it get absorbed into the surrounding block. */
28057 stmt = push_stmt_list ();
28058 cp_parser_compound_statement (parser, NULL, false, false);
28059 objc_begin_try_stmt (location, pop_stmt_list (stmt));
28061 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
28063 cp_parameter_declarator *parm;
28064 tree parameter_declaration = error_mark_node;
28065 bool seen_open_paren = false;
28067 cp_lexer_consume_token (parser->lexer);
28068 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28069 seen_open_paren = true;
28070 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28072 /* We have "@catch (...)" (where the '...' are literally
28073 what is in the code). Skip the '...'.
28074 parameter_declaration is set to NULL_TREE, and
28075 objc_being_catch_clauses() knows that that means
28076 '...'. */
28077 cp_lexer_consume_token (parser->lexer);
28078 parameter_declaration = NULL_TREE;
28080 else
28082 /* We have "@catch (NSException *exception)" or something
28083 like that. Parse the parameter declaration. */
28084 parm = cp_parser_parameter_declaration (parser, false, NULL);
28085 if (parm == NULL)
28086 parameter_declaration = error_mark_node;
28087 else
28088 parameter_declaration = grokdeclarator (parm->declarator,
28089 &parm->decl_specifiers,
28090 PARM, /*initialized=*/0,
28091 /*attrlist=*/NULL);
28093 if (seen_open_paren)
28094 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28095 else
28097 /* If there was no open parenthesis, we are recovering from
28098 an error, and we are trying to figure out what mistake
28099 the user has made. */
28101 /* If there is an immediate closing parenthesis, the user
28102 probably forgot the opening one (ie, they typed "@catch
28103 NSException *e)". Parse the closing parenthesis and keep
28104 going. */
28105 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28106 cp_lexer_consume_token (parser->lexer);
28108 /* If these is no immediate closing parenthesis, the user
28109 probably doesn't know that parenthesis are required at
28110 all (ie, they typed "@catch NSException *e"). So, just
28111 forget about the closing parenthesis and keep going. */
28113 objc_begin_catch_clause (parameter_declaration);
28114 cp_parser_compound_statement (parser, NULL, false, false);
28115 objc_finish_catch_clause ();
28117 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
28119 cp_lexer_consume_token (parser->lexer);
28120 location = cp_lexer_peek_token (parser->lexer)->location;
28121 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
28122 node, lest it get absorbed into the surrounding block. */
28123 stmt = push_stmt_list ();
28124 cp_parser_compound_statement (parser, NULL, false, false);
28125 objc_build_finally_clause (location, pop_stmt_list (stmt));
28128 return objc_finish_try_stmt ();
28131 /* Parse an Objective-C synchronized statement.
28133 objc-synchronized-stmt:
28134 @synchronized ( expression ) compound-statement
28136 Returns NULL_TREE. */
28138 static tree
28139 cp_parser_objc_synchronized_statement (cp_parser *parser)
28141 location_t location;
28142 tree lock, stmt;
28144 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
28146 location = cp_lexer_peek_token (parser->lexer)->location;
28147 objc_maybe_warn_exceptions (location);
28148 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28149 lock = cp_parser_expression (parser);
28150 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28152 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
28153 node, lest it get absorbed into the surrounding block. */
28154 stmt = push_stmt_list ();
28155 cp_parser_compound_statement (parser, NULL, false, false);
28157 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
28160 /* Parse an Objective-C throw statement.
28162 objc-throw-stmt:
28163 @throw assignment-expression [opt] ;
28165 Returns a constructed '@throw' statement. */
28167 static tree
28168 cp_parser_objc_throw_statement (cp_parser *parser)
28170 tree expr = NULL_TREE;
28171 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28173 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
28175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28176 expr = cp_parser_expression (parser);
28178 cp_parser_consume_semicolon_at_end_of_statement (parser);
28180 return objc_build_throw_stmt (loc, expr);
28183 /* Parse an Objective-C statement. */
28185 static tree
28186 cp_parser_objc_statement (cp_parser * parser)
28188 /* Try to figure out what kind of declaration is present. */
28189 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28191 switch (kwd->keyword)
28193 case RID_AT_TRY:
28194 return cp_parser_objc_try_catch_finally_statement (parser);
28195 case RID_AT_SYNCHRONIZED:
28196 return cp_parser_objc_synchronized_statement (parser);
28197 case RID_AT_THROW:
28198 return cp_parser_objc_throw_statement (parser);
28199 default:
28200 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28201 kwd->u.value);
28202 cp_parser_skip_to_end_of_block_or_statement (parser);
28205 return error_mark_node;
28208 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
28209 look ahead to see if an objc keyword follows the attributes. This
28210 is to detect the use of prefix attributes on ObjC @interface and
28211 @protocol. */
28213 static bool
28214 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
28216 cp_lexer_save_tokens (parser->lexer);
28217 *attrib = cp_parser_attributes_opt (parser);
28218 gcc_assert (*attrib);
28219 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
28221 cp_lexer_commit_tokens (parser->lexer);
28222 return true;
28224 cp_lexer_rollback_tokens (parser->lexer);
28225 return false;
28228 /* This routine is a minimal replacement for
28229 c_parser_struct_declaration () used when parsing the list of
28230 types/names or ObjC++ properties. For example, when parsing the
28231 code
28233 @property (readonly) int a, b, c;
28235 this function is responsible for parsing "int a, int b, int c" and
28236 returning the declarations as CHAIN of DECLs.
28238 TODO: Share this code with cp_parser_objc_class_ivars. It's very
28239 similar parsing. */
28240 static tree
28241 cp_parser_objc_struct_declaration (cp_parser *parser)
28243 tree decls = NULL_TREE;
28244 cp_decl_specifier_seq declspecs;
28245 int decl_class_or_enum_p;
28246 tree prefix_attributes;
28248 cp_parser_decl_specifier_seq (parser,
28249 CP_PARSER_FLAGS_NONE,
28250 &declspecs,
28251 &decl_class_or_enum_p);
28253 if (declspecs.type == error_mark_node)
28254 return error_mark_node;
28256 /* auto, register, static, extern, mutable. */
28257 if (declspecs.storage_class != sc_none)
28259 cp_parser_error (parser, "invalid type for property");
28260 declspecs.storage_class = sc_none;
28263 /* thread_local. */
28264 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28266 cp_parser_error (parser, "invalid type for property");
28267 declspecs.locations[ds_thread] = 0;
28270 /* typedef. */
28271 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28273 cp_parser_error (parser, "invalid type for property");
28274 declspecs.locations[ds_typedef] = 0;
28277 prefix_attributes = declspecs.attributes;
28278 declspecs.attributes = NULL_TREE;
28280 /* Keep going until we hit the `;' at the end of the declaration. */
28281 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28283 tree attributes, first_attribute, decl;
28284 cp_declarator *declarator;
28285 cp_token *token;
28287 /* Parse the declarator. */
28288 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28289 NULL, NULL, false, false);
28291 /* Look for attributes that apply to the ivar. */
28292 attributes = cp_parser_attributes_opt (parser);
28293 /* Remember which attributes are prefix attributes and
28294 which are not. */
28295 first_attribute = attributes;
28296 /* Combine the attributes. */
28297 attributes = chainon (prefix_attributes, attributes);
28299 decl = grokfield (declarator, &declspecs,
28300 NULL_TREE, /*init_const_expr_p=*/false,
28301 NULL_TREE, attributes);
28303 if (decl == error_mark_node || decl == NULL_TREE)
28304 return error_mark_node;
28306 /* Reset PREFIX_ATTRIBUTES. */
28307 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28308 attributes = TREE_CHAIN (attributes);
28309 if (attributes)
28310 TREE_CHAIN (attributes) = NULL_TREE;
28312 DECL_CHAIN (decl) = decls;
28313 decls = decl;
28315 token = cp_lexer_peek_token (parser->lexer);
28316 if (token->type == CPP_COMMA)
28318 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28319 continue;
28321 else
28322 break;
28324 return decls;
28327 /* Parse an Objective-C @property declaration. The syntax is:
28329 objc-property-declaration:
28330 '@property' objc-property-attributes[opt] struct-declaration ;
28332 objc-property-attributes:
28333 '(' objc-property-attribute-list ')'
28335 objc-property-attribute-list:
28336 objc-property-attribute
28337 objc-property-attribute-list, objc-property-attribute
28339 objc-property-attribute
28340 'getter' = identifier
28341 'setter' = identifier
28342 'readonly'
28343 'readwrite'
28344 'assign'
28345 'retain'
28346 'copy'
28347 'nonatomic'
28349 For example:
28350 @property NSString *name;
28351 @property (readonly) id object;
28352 @property (retain, nonatomic, getter=getTheName) id name;
28353 @property int a, b, c;
28355 PS: This function is identical to
28356 c_parser_objc_at_property_declaration for C. Keep them in sync. */
28357 static void
28358 cp_parser_objc_at_property_declaration (cp_parser *parser)
28360 /* The following variables hold the attributes of the properties as
28361 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
28362 seen. When we see an attribute, we set them to 'true' (if they
28363 are boolean properties) or to the identifier (if they have an
28364 argument, ie, for getter and setter). Note that here we only
28365 parse the list of attributes, check the syntax and accumulate the
28366 attributes that we find. objc_add_property_declaration() will
28367 then process the information. */
28368 bool property_assign = false;
28369 bool property_copy = false;
28370 tree property_getter_ident = NULL_TREE;
28371 bool property_nonatomic = false;
28372 bool property_readonly = false;
28373 bool property_readwrite = false;
28374 bool property_retain = false;
28375 tree property_setter_ident = NULL_TREE;
28377 /* 'properties' is the list of properties that we read. Usually a
28378 single one, but maybe more (eg, in "@property int a, b, c;" there
28379 are three). */
28380 tree properties;
28381 location_t loc;
28383 loc = cp_lexer_peek_token (parser->lexer)->location;
28385 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
28387 /* Parse the optional attribute list... */
28388 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28390 /* Eat the '('. */
28391 cp_lexer_consume_token (parser->lexer);
28393 while (true)
28395 bool syntax_error = false;
28396 cp_token *token = cp_lexer_peek_token (parser->lexer);
28397 enum rid keyword;
28399 if (token->type != CPP_NAME)
28401 cp_parser_error (parser, "expected identifier");
28402 break;
28404 keyword = C_RID_CODE (token->u.value);
28405 cp_lexer_consume_token (parser->lexer);
28406 switch (keyword)
28408 case RID_ASSIGN: property_assign = true; break;
28409 case RID_COPY: property_copy = true; break;
28410 case RID_NONATOMIC: property_nonatomic = true; break;
28411 case RID_READONLY: property_readonly = true; break;
28412 case RID_READWRITE: property_readwrite = true; break;
28413 case RID_RETAIN: property_retain = true; break;
28415 case RID_GETTER:
28416 case RID_SETTER:
28417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28419 if (keyword == RID_GETTER)
28420 cp_parser_error (parser,
28421 "missing %<=%> (after %<getter%> attribute)");
28422 else
28423 cp_parser_error (parser,
28424 "missing %<=%> (after %<setter%> attribute)");
28425 syntax_error = true;
28426 break;
28428 cp_lexer_consume_token (parser->lexer); /* eat the = */
28429 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
28431 cp_parser_error (parser, "expected identifier");
28432 syntax_error = true;
28433 break;
28435 if (keyword == RID_SETTER)
28437 if (property_setter_ident != NULL_TREE)
28439 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
28440 cp_lexer_consume_token (parser->lexer);
28442 else
28443 property_setter_ident = cp_parser_objc_selector (parser);
28444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28445 cp_parser_error (parser, "setter name must terminate with %<:%>");
28446 else
28447 cp_lexer_consume_token (parser->lexer);
28449 else
28451 if (property_getter_ident != NULL_TREE)
28453 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
28454 cp_lexer_consume_token (parser->lexer);
28456 else
28457 property_getter_ident = cp_parser_objc_selector (parser);
28459 break;
28460 default:
28461 cp_parser_error (parser, "unknown property attribute");
28462 syntax_error = true;
28463 break;
28466 if (syntax_error)
28467 break;
28469 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28470 cp_lexer_consume_token (parser->lexer);
28471 else
28472 break;
28475 /* FIXME: "@property (setter, assign);" will generate a spurious
28476 "error: expected ‘)’ before ‘,’ token". This is because
28477 cp_parser_require, unlike the C counterpart, will produce an
28478 error even if we are in error recovery. */
28479 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28481 cp_parser_skip_to_closing_parenthesis (parser,
28482 /*recovering=*/true,
28483 /*or_comma=*/false,
28484 /*consume_paren=*/true);
28488 /* ... and the property declaration(s). */
28489 properties = cp_parser_objc_struct_declaration (parser);
28491 if (properties == error_mark_node)
28493 cp_parser_skip_to_end_of_statement (parser);
28494 /* If the next token is now a `;', consume it. */
28495 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28496 cp_lexer_consume_token (parser->lexer);
28497 return;
28500 if (properties == NULL_TREE)
28501 cp_parser_error (parser, "expected identifier");
28502 else
28504 /* Comma-separated properties are chained together in
28505 reverse order; add them one by one. */
28506 properties = nreverse (properties);
28508 for (; properties; properties = TREE_CHAIN (properties))
28509 objc_add_property_declaration (loc, copy_node (properties),
28510 property_readonly, property_readwrite,
28511 property_assign, property_retain,
28512 property_copy, property_nonatomic,
28513 property_getter_ident, property_setter_ident);
28516 cp_parser_consume_semicolon_at_end_of_statement (parser);
28519 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
28521 objc-synthesize-declaration:
28522 @synthesize objc-synthesize-identifier-list ;
28524 objc-synthesize-identifier-list:
28525 objc-synthesize-identifier
28526 objc-synthesize-identifier-list, objc-synthesize-identifier
28528 objc-synthesize-identifier
28529 identifier
28530 identifier = identifier
28532 For example:
28533 @synthesize MyProperty;
28534 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
28536 PS: This function is identical to c_parser_objc_at_synthesize_declaration
28537 for C. Keep them in sync.
28539 static void
28540 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
28542 tree list = NULL_TREE;
28543 location_t loc;
28544 loc = cp_lexer_peek_token (parser->lexer)->location;
28546 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
28547 while (true)
28549 tree property, ivar;
28550 property = cp_parser_identifier (parser);
28551 if (property == error_mark_node)
28553 cp_parser_consume_semicolon_at_end_of_statement (parser);
28554 return;
28556 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28558 cp_lexer_consume_token (parser->lexer);
28559 ivar = cp_parser_identifier (parser);
28560 if (ivar == error_mark_node)
28562 cp_parser_consume_semicolon_at_end_of_statement (parser);
28563 return;
28566 else
28567 ivar = NULL_TREE;
28568 list = chainon (list, build_tree_list (ivar, property));
28569 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28570 cp_lexer_consume_token (parser->lexer);
28571 else
28572 break;
28574 cp_parser_consume_semicolon_at_end_of_statement (parser);
28575 objc_add_synthesize_declaration (loc, list);
28578 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
28580 objc-dynamic-declaration:
28581 @dynamic identifier-list ;
28583 For example:
28584 @dynamic MyProperty;
28585 @dynamic MyProperty, AnotherProperty;
28587 PS: This function is identical to c_parser_objc_at_dynamic_declaration
28588 for C. Keep them in sync.
28590 static void
28591 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
28593 tree list = NULL_TREE;
28594 location_t loc;
28595 loc = cp_lexer_peek_token (parser->lexer)->location;
28597 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
28598 while (true)
28600 tree property;
28601 property = cp_parser_identifier (parser);
28602 if (property == error_mark_node)
28604 cp_parser_consume_semicolon_at_end_of_statement (parser);
28605 return;
28607 list = chainon (list, build_tree_list (NULL, property));
28608 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28609 cp_lexer_consume_token (parser->lexer);
28610 else
28611 break;
28613 cp_parser_consume_semicolon_at_end_of_statement (parser);
28614 objc_add_dynamic_declaration (loc, list);
28618 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
28620 /* Returns name of the next clause.
28621 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
28622 the token is not consumed. Otherwise appropriate pragma_omp_clause is
28623 returned and the token is consumed. */
28625 static pragma_omp_clause
28626 cp_parser_omp_clause_name (cp_parser *parser)
28628 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
28630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
28631 result = PRAGMA_OMP_CLAUSE_IF;
28632 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
28633 result = PRAGMA_OMP_CLAUSE_DEFAULT;
28634 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
28635 result = PRAGMA_OACC_CLAUSE_DELETE;
28636 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
28637 result = PRAGMA_OMP_CLAUSE_PRIVATE;
28638 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28639 result = PRAGMA_OMP_CLAUSE_FOR;
28640 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28642 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28643 const char *p = IDENTIFIER_POINTER (id);
28645 switch (p[0])
28647 case 'a':
28648 if (!strcmp ("aligned", p))
28649 result = PRAGMA_OMP_CLAUSE_ALIGNED;
28650 else if (!strcmp ("async", p))
28651 result = PRAGMA_OACC_CLAUSE_ASYNC;
28652 break;
28653 case 'c':
28654 if (!strcmp ("collapse", p))
28655 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
28656 else if (!strcmp ("copy", p))
28657 result = PRAGMA_OACC_CLAUSE_COPY;
28658 else if (!strcmp ("copyin", p))
28659 result = PRAGMA_OMP_CLAUSE_COPYIN;
28660 else if (!strcmp ("copyout", p))
28661 result = PRAGMA_OACC_CLAUSE_COPYOUT;
28662 else if (!strcmp ("copyprivate", p))
28663 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
28664 else if (!strcmp ("create", p))
28665 result = PRAGMA_OACC_CLAUSE_CREATE;
28666 break;
28667 case 'd':
28668 if (!strcmp ("depend", p))
28669 result = PRAGMA_OMP_CLAUSE_DEPEND;
28670 else if (!strcmp ("device", p))
28671 result = PRAGMA_OMP_CLAUSE_DEVICE;
28672 else if (!strcmp ("deviceptr", p))
28673 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
28674 else if (!strcmp ("dist_schedule", p))
28675 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
28676 break;
28677 case 'f':
28678 if (!strcmp ("final", p))
28679 result = PRAGMA_OMP_CLAUSE_FINAL;
28680 else if (!strcmp ("firstprivate", p))
28681 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
28682 else if (!strcmp ("from", p))
28683 result = PRAGMA_OMP_CLAUSE_FROM;
28684 break;
28685 case 'h':
28686 if (!strcmp ("host", p))
28687 result = PRAGMA_OACC_CLAUSE_HOST;
28688 break;
28689 case 'i':
28690 if (!strcmp ("inbranch", p))
28691 result = PRAGMA_OMP_CLAUSE_INBRANCH;
28692 break;
28693 case 'l':
28694 if (!strcmp ("lastprivate", p))
28695 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
28696 else if (!strcmp ("linear", p))
28697 result = PRAGMA_OMP_CLAUSE_LINEAR;
28698 break;
28699 case 'm':
28700 if (!strcmp ("map", p))
28701 result = PRAGMA_OMP_CLAUSE_MAP;
28702 else if (!strcmp ("mergeable", p))
28703 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
28704 else if (flag_cilkplus && !strcmp ("mask", p))
28705 result = PRAGMA_CILK_CLAUSE_MASK;
28706 break;
28707 case 'n':
28708 if (!strcmp ("notinbranch", p))
28709 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
28710 else if (!strcmp ("nowait", p))
28711 result = PRAGMA_OMP_CLAUSE_NOWAIT;
28712 else if (flag_cilkplus && !strcmp ("nomask", p))
28713 result = PRAGMA_CILK_CLAUSE_NOMASK;
28714 else if (!strcmp ("num_gangs", p))
28715 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
28716 else if (!strcmp ("num_teams", p))
28717 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
28718 else if (!strcmp ("num_threads", p))
28719 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
28720 else if (!strcmp ("num_workers", p))
28721 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
28722 break;
28723 case 'o':
28724 if (!strcmp ("ordered", p))
28725 result = PRAGMA_OMP_CLAUSE_ORDERED;
28726 break;
28727 case 'p':
28728 if (!strcmp ("parallel", p))
28729 result = PRAGMA_OMP_CLAUSE_PARALLEL;
28730 else if (!strcmp ("present", p))
28731 result = PRAGMA_OACC_CLAUSE_PRESENT;
28732 else if (!strcmp ("present_or_copy", p)
28733 || !strcmp ("pcopy", p))
28734 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
28735 else if (!strcmp ("present_or_copyin", p)
28736 || !strcmp ("pcopyin", p))
28737 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
28738 else if (!strcmp ("present_or_copyout", p)
28739 || !strcmp ("pcopyout", p))
28740 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
28741 else if (!strcmp ("present_or_create", p)
28742 || !strcmp ("pcreate", p))
28743 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
28744 else if (!strcmp ("proc_bind", p))
28745 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
28746 break;
28747 case 'r':
28748 if (!strcmp ("reduction", p))
28749 result = PRAGMA_OMP_CLAUSE_REDUCTION;
28750 break;
28751 case 's':
28752 if (!strcmp ("safelen", p))
28753 result = PRAGMA_OMP_CLAUSE_SAFELEN;
28754 else if (!strcmp ("schedule", p))
28755 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
28756 else if (!strcmp ("sections", p))
28757 result = PRAGMA_OMP_CLAUSE_SECTIONS;
28758 else if (!strcmp ("self", p))
28759 result = PRAGMA_OACC_CLAUSE_SELF;
28760 else if (!strcmp ("shared", p))
28761 result = PRAGMA_OMP_CLAUSE_SHARED;
28762 else if (!strcmp ("simdlen", p))
28763 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
28764 break;
28765 case 't':
28766 if (!strcmp ("taskgroup", p))
28767 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
28768 else if (!strcmp ("thread_limit", p))
28769 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
28770 else if (!strcmp ("to", p))
28771 result = PRAGMA_OMP_CLAUSE_TO;
28772 break;
28773 case 'u':
28774 if (!strcmp ("uniform", p))
28775 result = PRAGMA_OMP_CLAUSE_UNIFORM;
28776 else if (!strcmp ("untied", p))
28777 result = PRAGMA_OMP_CLAUSE_UNTIED;
28778 break;
28779 case 'v':
28780 if (!strcmp ("vector_length", p))
28781 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
28782 else if (flag_cilkplus && !strcmp ("vectorlength", p))
28783 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
28784 break;
28785 case 'w':
28786 if (!strcmp ("wait", p))
28787 result = PRAGMA_OACC_CLAUSE_WAIT;
28788 break;
28792 if (result != PRAGMA_OMP_CLAUSE_NONE)
28793 cp_lexer_consume_token (parser->lexer);
28795 return result;
28798 /* Validate that a clause of the given type does not already exist. */
28800 static void
28801 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
28802 const char *name, location_t location)
28804 tree c;
28806 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
28807 if (OMP_CLAUSE_CODE (c) == code)
28809 error_at (location, "too many %qs clauses", name);
28810 break;
28814 /* OpenMP 2.5:
28815 variable-list:
28816 identifier
28817 variable-list , identifier
28819 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
28820 colon). An opening parenthesis will have been consumed by the caller.
28822 If KIND is nonzero, create the appropriate node and install the decl
28823 in OMP_CLAUSE_DECL and add the node to the head of the list.
28825 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
28826 return the list created.
28828 COLON can be NULL if only closing parenthesis should end the list,
28829 or pointer to bool which will receive false if the list is terminated
28830 by closing parenthesis or true if the list is terminated by colon. */
28832 static tree
28833 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
28834 tree list, bool *colon)
28836 cp_token *token;
28837 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28838 if (colon)
28840 parser->colon_corrects_to_scope_p = false;
28841 *colon = false;
28843 while (1)
28845 tree name, decl;
28847 token = cp_lexer_peek_token (parser->lexer);
28848 name = cp_parser_id_expression (parser, /*template_p=*/false,
28849 /*check_dependency_p=*/true,
28850 /*template_p=*/NULL,
28851 /*declarator_p=*/false,
28852 /*optional_p=*/false);
28853 if (name == error_mark_node)
28854 goto skip_comma;
28856 decl = cp_parser_lookup_name_simple (parser, name, token->location);
28857 if (decl == error_mark_node)
28858 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
28859 token->location);
28860 else if (kind != 0)
28862 switch (kind)
28864 case OMP_CLAUSE__CACHE_:
28865 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
28867 error_at (token->location, "expected %<[%>");
28868 decl = error_mark_node;
28869 break;
28871 /* FALL THROUGH. */
28872 case OMP_CLAUSE_MAP:
28873 case OMP_CLAUSE_FROM:
28874 case OMP_CLAUSE_TO:
28875 case OMP_CLAUSE_DEPEND:
28876 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28878 tree low_bound = NULL_TREE, length = NULL_TREE;
28880 parser->colon_corrects_to_scope_p = false;
28881 cp_lexer_consume_token (parser->lexer);
28882 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28883 low_bound = cp_parser_expression (parser);
28884 if (!colon)
28885 parser->colon_corrects_to_scope_p
28886 = saved_colon_corrects_to_scope_p;
28887 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
28888 length = integer_one_node;
28889 else
28891 /* Look for `:'. */
28892 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28893 goto skip_comma;
28894 if (!cp_lexer_next_token_is (parser->lexer,
28895 CPP_CLOSE_SQUARE))
28896 length = cp_parser_expression (parser);
28898 /* Look for the closing `]'. */
28899 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
28900 RT_CLOSE_SQUARE))
28901 goto skip_comma;
28903 if (kind == OMP_CLAUSE__CACHE_)
28905 if (TREE_CODE (low_bound) != INTEGER_CST
28906 && !TREE_READONLY (low_bound))
28908 error_at (token->location,
28909 "%qD is not a constant", low_bound);
28910 decl = error_mark_node;
28913 if (TREE_CODE (length) != INTEGER_CST
28914 && !TREE_READONLY (length))
28916 error_at (token->location,
28917 "%qD is not a constant", length);
28918 decl = error_mark_node;
28922 decl = tree_cons (low_bound, length, decl);
28924 break;
28925 default:
28926 break;
28929 tree u = build_omp_clause (token->location, kind);
28930 OMP_CLAUSE_DECL (u) = decl;
28931 OMP_CLAUSE_CHAIN (u) = list;
28932 list = u;
28934 else
28935 list = tree_cons (decl, NULL_TREE, list);
28937 get_comma:
28938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28939 break;
28940 cp_lexer_consume_token (parser->lexer);
28943 if (colon)
28944 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28946 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28948 *colon = true;
28949 cp_parser_require (parser, CPP_COLON, RT_COLON);
28950 return list;
28953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28955 int ending;
28957 /* Try to resync to an unnested comma. Copied from
28958 cp_parser_parenthesized_expression_list. */
28959 skip_comma:
28960 if (colon)
28961 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28962 ending = cp_parser_skip_to_closing_parenthesis (parser,
28963 /*recovering=*/true,
28964 /*or_comma=*/true,
28965 /*consume_paren=*/true);
28966 if (ending < 0)
28967 goto get_comma;
28970 return list;
28973 /* Similarly, but expect leading and trailing parenthesis. This is a very
28974 common case for omp clauses. */
28976 static tree
28977 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
28979 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28980 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
28981 return list;
28984 /* OpenACC 2.0:
28985 copy ( variable-list )
28986 copyin ( variable-list )
28987 copyout ( variable-list )
28988 create ( variable-list )
28989 delete ( variable-list )
28990 present ( variable-list )
28991 present_or_copy ( variable-list )
28992 pcopy ( variable-list )
28993 present_or_copyin ( variable-list )
28994 pcopyin ( variable-list )
28995 present_or_copyout ( variable-list )
28996 pcopyout ( variable-list )
28997 present_or_create ( variable-list )
28998 pcreate ( variable-list ) */
29000 static tree
29001 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
29002 tree list)
29004 enum gomp_map_kind kind;
29005 switch (c_kind)
29007 case PRAGMA_OACC_CLAUSE_COPY:
29008 kind = GOMP_MAP_FORCE_TOFROM;
29009 break;
29010 case PRAGMA_OACC_CLAUSE_COPYIN:
29011 kind = GOMP_MAP_FORCE_TO;
29012 break;
29013 case PRAGMA_OACC_CLAUSE_COPYOUT:
29014 kind = GOMP_MAP_FORCE_FROM;
29015 break;
29016 case PRAGMA_OACC_CLAUSE_CREATE:
29017 kind = GOMP_MAP_FORCE_ALLOC;
29018 break;
29019 case PRAGMA_OACC_CLAUSE_DELETE:
29020 kind = GOMP_MAP_FORCE_DEALLOC;
29021 break;
29022 case PRAGMA_OACC_CLAUSE_DEVICE:
29023 kind = GOMP_MAP_FORCE_TO;
29024 break;
29025 case PRAGMA_OACC_CLAUSE_HOST:
29026 case PRAGMA_OACC_CLAUSE_SELF:
29027 kind = GOMP_MAP_FORCE_FROM;
29028 break;
29029 case PRAGMA_OACC_CLAUSE_PRESENT:
29030 kind = GOMP_MAP_FORCE_PRESENT;
29031 break;
29032 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29033 kind = GOMP_MAP_TOFROM;
29034 break;
29035 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29036 kind = GOMP_MAP_TO;
29037 break;
29038 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29039 kind = GOMP_MAP_FROM;
29040 break;
29041 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29042 kind = GOMP_MAP_ALLOC;
29043 break;
29044 default:
29045 gcc_unreachable ();
29047 tree nl, c;
29048 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
29050 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
29051 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29053 return nl;
29056 /* OpenACC 2.0:
29057 deviceptr ( variable-list ) */
29059 static tree
29060 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
29062 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29063 tree vars, t;
29065 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
29066 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
29067 variable-list must only allow for pointer variables. */
29068 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29069 for (t = vars; t; t = TREE_CHAIN (t))
29071 tree v = TREE_PURPOSE (t);
29073 /* FIXME diagnostics: Ideally we should keep individual
29074 locations for all the variables in the var list to make the
29075 following errors more precise. Perhaps
29076 c_parser_omp_var_list_parens should construct a list of
29077 locations to go along with the var list. */
29079 if (TREE_CODE (v) != VAR_DECL)
29080 error_at (loc, "%qD is not a variable", v);
29081 else if (TREE_TYPE (v) == error_mark_node)
29083 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
29084 error_at (loc, "%qD is not a pointer variable", v);
29086 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
29087 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
29088 OMP_CLAUSE_DECL (u) = v;
29089 OMP_CLAUSE_CHAIN (u) = list;
29090 list = u;
29093 return list;
29096 /* OpenACC:
29097 vector_length ( expression ) */
29099 static tree
29100 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
29102 tree t, c;
29103 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29104 bool error = false;
29106 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29107 return list;
29109 t = cp_parser_condition (parser);
29110 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
29112 error_at (location, "expected positive integer expression");
29113 error = true;
29116 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29118 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29119 /*or_comma=*/false,
29120 /*consume_paren=*/true);
29121 return list;
29124 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
29125 location);
29127 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
29128 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
29129 OMP_CLAUSE_CHAIN (c) = list;
29130 list = c;
29132 return list;
29135 /* OpenACC 2.0
29136 Parse wait clause or directive parameters. */
29138 static tree
29139 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
29141 vec<tree, va_gc> *args;
29142 tree t, args_tree;
29144 args = cp_parser_parenthesized_expression_list (parser, non_attr,
29145 /*cast_p=*/false,
29146 /*allow_expansion_p=*/true,
29147 /*non_constant_p=*/NULL);
29149 if (args == NULL || args->length () == 0)
29151 cp_parser_error (parser, "expected integer expression before ')'");
29152 if (args != NULL)
29153 release_tree_vector (args);
29154 return list;
29157 args_tree = build_tree_list_vec (args);
29159 release_tree_vector (args);
29161 for (t = args_tree; t; t = TREE_CHAIN (t))
29163 tree targ = TREE_VALUE (t);
29165 if (targ != error_mark_node)
29167 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
29168 error ("%<wait%> expression must be integral");
29169 else
29171 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
29173 mark_rvalue_use (targ);
29174 OMP_CLAUSE_DECL (c) = targ;
29175 OMP_CLAUSE_CHAIN (c) = list;
29176 list = c;
29181 return list;
29184 /* OpenACC:
29185 wait ( int-expr-list ) */
29187 static tree
29188 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
29190 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29192 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
29193 return list;
29195 list = cp_parser_oacc_wait_list (parser, location, list);
29197 return list;
29200 /* OpenMP 3.0:
29201 collapse ( constant-expression ) */
29203 static tree
29204 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
29206 tree c, num;
29207 location_t loc;
29208 HOST_WIDE_INT n;
29210 loc = cp_lexer_peek_token (parser->lexer)->location;
29211 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29212 return list;
29214 num = cp_parser_constant_expression (parser);
29216 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29217 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29218 /*or_comma=*/false,
29219 /*consume_paren=*/true);
29221 if (num == error_mark_node)
29222 return list;
29223 num = fold_non_dependent_expr (num);
29224 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
29225 || !tree_fits_shwi_p (num)
29226 || (n = tree_to_shwi (num)) <= 0
29227 || (int) n != n)
29229 error_at (loc, "collapse argument needs positive constant integer expression");
29230 return list;
29233 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
29234 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
29235 OMP_CLAUSE_CHAIN (c) = list;
29236 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
29238 return c;
29241 /* OpenMP 2.5:
29242 default ( shared | none ) */
29244 static tree
29245 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
29247 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
29248 tree c;
29250 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29251 return list;
29252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29254 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29255 const char *p = IDENTIFIER_POINTER (id);
29257 switch (p[0])
29259 case 'n':
29260 if (strcmp ("none", p) != 0)
29261 goto invalid_kind;
29262 kind = OMP_CLAUSE_DEFAULT_NONE;
29263 break;
29265 case 's':
29266 if (strcmp ("shared", p) != 0)
29267 goto invalid_kind;
29268 kind = OMP_CLAUSE_DEFAULT_SHARED;
29269 break;
29271 default:
29272 goto invalid_kind;
29275 cp_lexer_consume_token (parser->lexer);
29277 else
29279 invalid_kind:
29280 cp_parser_error (parser, "expected %<none%> or %<shared%>");
29283 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29285 /*or_comma=*/false,
29286 /*consume_paren=*/true);
29288 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
29289 return list;
29291 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
29292 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
29293 OMP_CLAUSE_CHAIN (c) = list;
29294 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
29296 return c;
29299 /* OpenMP 3.1:
29300 final ( expression ) */
29302 static tree
29303 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
29305 tree t, c;
29307 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29308 return list;
29310 t = cp_parser_condition (parser);
29312 if (t == error_mark_node
29313 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29314 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29315 /*or_comma=*/false,
29316 /*consume_paren=*/true);
29318 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
29320 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
29321 OMP_CLAUSE_FINAL_EXPR (c) = t;
29322 OMP_CLAUSE_CHAIN (c) = list;
29324 return c;
29327 /* OpenMP 2.5:
29328 if ( expression ) */
29330 static tree
29331 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
29333 tree t, c;
29335 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29336 return list;
29338 t = cp_parser_condition (parser);
29340 if (t == error_mark_node
29341 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29342 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29343 /*or_comma=*/false,
29344 /*consume_paren=*/true);
29346 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
29348 c = build_omp_clause (location, OMP_CLAUSE_IF);
29349 OMP_CLAUSE_IF_EXPR (c) = t;
29350 OMP_CLAUSE_CHAIN (c) = list;
29352 return c;
29355 /* OpenMP 3.1:
29356 mergeable */
29358 static tree
29359 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
29360 tree list, location_t location)
29362 tree c;
29364 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
29365 location);
29367 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
29368 OMP_CLAUSE_CHAIN (c) = list;
29369 return c;
29372 /* OpenMP 2.5:
29373 nowait */
29375 static tree
29376 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
29377 tree list, location_t location)
29379 tree c;
29381 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
29383 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
29384 OMP_CLAUSE_CHAIN (c) = list;
29385 return c;
29388 /* OpenACC:
29389 num_gangs ( expression ) */
29391 static tree
29392 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
29394 tree t, c;
29395 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29398 return list;
29400 t = cp_parser_condition (parser);
29402 if (t == error_mark_node
29403 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29405 /*or_comma=*/false,
29406 /*consume_paren=*/true);
29408 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
29410 error_at (location, "expected positive integer expression");
29411 return list;
29414 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
29416 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
29417 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
29418 OMP_CLAUSE_CHAIN (c) = list;
29419 list = c;
29421 return list;
29424 /* OpenMP 2.5:
29425 num_threads ( expression ) */
29427 static tree
29428 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
29429 location_t location)
29431 tree t, c;
29433 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29434 return list;
29436 t = cp_parser_expression (parser);
29438 if (t == error_mark_node
29439 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29440 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29441 /*or_comma=*/false,
29442 /*consume_paren=*/true);
29444 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
29445 "num_threads", location);
29447 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
29448 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
29449 OMP_CLAUSE_CHAIN (c) = list;
29451 return c;
29454 /* OpenACC:
29455 num_workers ( expression ) */
29457 static tree
29458 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
29460 tree t, c;
29461 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29464 return list;
29466 t = cp_parser_condition (parser);
29468 if (t == error_mark_node
29469 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29470 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29471 /*or_comma=*/false,
29472 /*consume_paren=*/true);
29474 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
29476 error_at (location, "expected positive integer expression");
29477 return list;
29480 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
29481 location);
29483 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
29484 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
29485 OMP_CLAUSE_CHAIN (c) = list;
29486 list = c;
29488 return list;
29491 /* OpenMP 2.5:
29492 ordered */
29494 static tree
29495 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
29496 tree list, location_t location)
29498 tree c;
29500 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
29501 "ordered", location);
29503 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
29504 OMP_CLAUSE_CHAIN (c) = list;
29505 return c;
29508 /* OpenMP 2.5:
29509 reduction ( reduction-operator : variable-list )
29511 reduction-operator:
29512 One of: + * - & ^ | && ||
29514 OpenMP 3.1:
29516 reduction-operator:
29517 One of: + * - & ^ | && || min max
29519 OpenMP 4.0:
29521 reduction-operator:
29522 One of: + * - & ^ | && ||
29523 id-expression */
29525 static tree
29526 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
29528 enum tree_code code = ERROR_MARK;
29529 tree nlist, c, id = NULL_TREE;
29531 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29532 return list;
29534 switch (cp_lexer_peek_token (parser->lexer)->type)
29536 case CPP_PLUS: code = PLUS_EXPR; break;
29537 case CPP_MULT: code = MULT_EXPR; break;
29538 case CPP_MINUS: code = MINUS_EXPR; break;
29539 case CPP_AND: code = BIT_AND_EXPR; break;
29540 case CPP_XOR: code = BIT_XOR_EXPR; break;
29541 case CPP_OR: code = BIT_IOR_EXPR; break;
29542 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
29543 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
29544 default: break;
29547 if (code != ERROR_MARK)
29548 cp_lexer_consume_token (parser->lexer);
29549 else
29551 bool saved_colon_corrects_to_scope_p;
29552 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29553 parser->colon_corrects_to_scope_p = false;
29554 id = cp_parser_id_expression (parser, /*template_p=*/false,
29555 /*check_dependency_p=*/true,
29556 /*template_p=*/NULL,
29557 /*declarator_p=*/false,
29558 /*optional_p=*/false);
29559 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29560 if (identifier_p (id))
29562 const char *p = IDENTIFIER_POINTER (id);
29564 if (strcmp (p, "min") == 0)
29565 code = MIN_EXPR;
29566 else if (strcmp (p, "max") == 0)
29567 code = MAX_EXPR;
29568 else if (id == ansi_opname (PLUS_EXPR))
29569 code = PLUS_EXPR;
29570 else if (id == ansi_opname (MULT_EXPR))
29571 code = MULT_EXPR;
29572 else if (id == ansi_opname (MINUS_EXPR))
29573 code = MINUS_EXPR;
29574 else if (id == ansi_opname (BIT_AND_EXPR))
29575 code = BIT_AND_EXPR;
29576 else if (id == ansi_opname (BIT_IOR_EXPR))
29577 code = BIT_IOR_EXPR;
29578 else if (id == ansi_opname (BIT_XOR_EXPR))
29579 code = BIT_XOR_EXPR;
29580 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
29581 code = TRUTH_ANDIF_EXPR;
29582 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
29583 code = TRUTH_ORIF_EXPR;
29584 id = omp_reduction_id (code, id, NULL_TREE);
29585 tree scope = parser->scope;
29586 if (scope)
29587 id = build_qualified_name (NULL_TREE, scope, id, false);
29588 parser->scope = NULL_TREE;
29589 parser->qualifying_scope = NULL_TREE;
29590 parser->object_scope = NULL_TREE;
29592 else
29594 error ("invalid reduction-identifier");
29595 resync_fail:
29596 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29597 /*or_comma=*/false,
29598 /*consume_paren=*/true);
29599 return list;
29603 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29604 goto resync_fail;
29606 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
29607 NULL);
29608 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29610 OMP_CLAUSE_REDUCTION_CODE (c) = code;
29611 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
29614 return nlist;
29617 /* OpenMP 2.5:
29618 schedule ( schedule-kind )
29619 schedule ( schedule-kind , expression )
29621 schedule-kind:
29622 static | dynamic | guided | runtime | auto */
29624 static tree
29625 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
29627 tree c, t;
29629 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29630 return list;
29632 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
29634 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29636 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29637 const char *p = IDENTIFIER_POINTER (id);
29639 switch (p[0])
29641 case 'd':
29642 if (strcmp ("dynamic", p) != 0)
29643 goto invalid_kind;
29644 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
29645 break;
29647 case 'g':
29648 if (strcmp ("guided", p) != 0)
29649 goto invalid_kind;
29650 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
29651 break;
29653 case 'r':
29654 if (strcmp ("runtime", p) != 0)
29655 goto invalid_kind;
29656 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
29657 break;
29659 default:
29660 goto invalid_kind;
29663 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29664 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
29665 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29666 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
29667 else
29668 goto invalid_kind;
29669 cp_lexer_consume_token (parser->lexer);
29671 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29673 cp_token *token;
29674 cp_lexer_consume_token (parser->lexer);
29676 token = cp_lexer_peek_token (parser->lexer);
29677 t = cp_parser_assignment_expression (parser);
29679 if (t == error_mark_node)
29680 goto resync_fail;
29681 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
29682 error_at (token->location, "schedule %<runtime%> does not take "
29683 "a %<chunk_size%> parameter");
29684 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
29685 error_at (token->location, "schedule %<auto%> does not take "
29686 "a %<chunk_size%> parameter");
29687 else
29688 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
29690 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29691 goto resync_fail;
29693 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29694 goto resync_fail;
29696 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
29697 OMP_CLAUSE_CHAIN (c) = list;
29698 return c;
29700 invalid_kind:
29701 cp_parser_error (parser, "invalid schedule kind");
29702 resync_fail:
29703 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29704 /*or_comma=*/false,
29705 /*consume_paren=*/true);
29706 return list;
29709 /* OpenMP 3.0:
29710 untied */
29712 static tree
29713 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
29714 tree list, location_t location)
29716 tree c;
29718 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
29720 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
29721 OMP_CLAUSE_CHAIN (c) = list;
29722 return c;
29725 /* OpenMP 4.0:
29726 inbranch
29727 notinbranch */
29729 static tree
29730 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
29731 tree list, location_t location)
29733 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29734 tree c = build_omp_clause (location, code);
29735 OMP_CLAUSE_CHAIN (c) = list;
29736 return c;
29739 /* OpenMP 4.0:
29740 parallel
29742 sections
29743 taskgroup */
29745 static tree
29746 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
29747 enum omp_clause_code code,
29748 tree list, location_t location)
29750 tree c = build_omp_clause (location, code);
29751 OMP_CLAUSE_CHAIN (c) = list;
29752 return c;
29755 /* OpenMP 4.0:
29756 num_teams ( expression ) */
29758 static tree
29759 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
29760 location_t location)
29762 tree t, c;
29764 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29765 return list;
29767 t = cp_parser_expression (parser);
29769 if (t == error_mark_node
29770 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29771 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29772 /*or_comma=*/false,
29773 /*consume_paren=*/true);
29775 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
29776 "num_teams", location);
29778 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
29779 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
29780 OMP_CLAUSE_CHAIN (c) = list;
29782 return c;
29785 /* OpenMP 4.0:
29786 thread_limit ( expression ) */
29788 static tree
29789 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
29790 location_t location)
29792 tree t, c;
29794 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29795 return list;
29797 t = cp_parser_expression (parser);
29799 if (t == error_mark_node
29800 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29801 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29802 /*or_comma=*/false,
29803 /*consume_paren=*/true);
29805 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
29806 "thread_limit", location);
29808 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
29809 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
29810 OMP_CLAUSE_CHAIN (c) = list;
29812 return c;
29815 /* OpenMP 4.0:
29816 aligned ( variable-list )
29817 aligned ( variable-list : constant-expression ) */
29819 static tree
29820 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
29822 tree nlist, c, alignment = NULL_TREE;
29823 bool colon;
29825 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29826 return list;
29828 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
29829 &colon);
29831 if (colon)
29833 alignment = cp_parser_constant_expression (parser);
29835 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29836 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29837 /*or_comma=*/false,
29838 /*consume_paren=*/true);
29840 if (alignment == error_mark_node)
29841 alignment = NULL_TREE;
29844 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29845 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
29847 return nlist;
29850 /* OpenMP 4.0:
29851 linear ( variable-list )
29852 linear ( variable-list : expression ) */
29854 static tree
29855 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
29856 bool is_cilk_simd_fn)
29858 tree nlist, c, step = integer_one_node;
29859 bool colon;
29861 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29862 return list;
29864 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
29865 &colon);
29867 if (colon)
29869 step = cp_parser_expression (parser);
29871 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
29873 sorry ("using parameters for %<linear%> step is not supported yet");
29874 step = integer_one_node;
29876 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29877 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29878 /*or_comma=*/false,
29879 /*consume_paren=*/true);
29881 if (step == error_mark_node)
29882 return list;
29885 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29886 OMP_CLAUSE_LINEAR_STEP (c) = step;
29888 return nlist;
29891 /* OpenMP 4.0:
29892 safelen ( constant-expression ) */
29894 static tree
29895 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
29896 location_t location)
29898 tree t, c;
29900 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29901 return list;
29903 t = cp_parser_constant_expression (parser);
29905 if (t == error_mark_node
29906 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29907 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29908 /*or_comma=*/false,
29909 /*consume_paren=*/true);
29911 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
29913 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
29914 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
29915 OMP_CLAUSE_CHAIN (c) = list;
29917 return c;
29920 /* OpenMP 4.0:
29921 simdlen ( constant-expression ) */
29923 static tree
29924 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
29925 location_t location)
29927 tree t, c;
29929 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29930 return list;
29932 t = cp_parser_constant_expression (parser);
29934 if (t == error_mark_node
29935 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29936 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29937 /*or_comma=*/false,
29938 /*consume_paren=*/true);
29940 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
29942 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
29943 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
29944 OMP_CLAUSE_CHAIN (c) = list;
29946 return c;
29949 /* OpenMP 4.0:
29950 depend ( depend-kind : variable-list )
29952 depend-kind:
29953 in | out | inout */
29955 static tree
29956 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
29958 tree nlist, c;
29959 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
29961 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29962 return list;
29964 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29966 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29967 const char *p = IDENTIFIER_POINTER (id);
29969 if (strcmp ("in", p) == 0)
29970 kind = OMP_CLAUSE_DEPEND_IN;
29971 else if (strcmp ("inout", p) == 0)
29972 kind = OMP_CLAUSE_DEPEND_INOUT;
29973 else if (strcmp ("out", p) == 0)
29974 kind = OMP_CLAUSE_DEPEND_OUT;
29975 else
29976 goto invalid_kind;
29978 else
29979 goto invalid_kind;
29981 cp_lexer_consume_token (parser->lexer);
29982 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29983 goto resync_fail;
29985 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
29986 NULL);
29988 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29989 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29991 return nlist;
29993 invalid_kind:
29994 cp_parser_error (parser, "invalid depend kind");
29995 resync_fail:
29996 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29997 /*or_comma=*/false,
29998 /*consume_paren=*/true);
29999 return list;
30002 /* OpenMP 4.0:
30003 map ( map-kind : variable-list )
30004 map ( variable-list )
30006 map-kind:
30007 alloc | to | from | tofrom */
30009 static tree
30010 cp_parser_omp_clause_map (cp_parser *parser, tree list)
30012 tree nlist, c;
30013 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
30015 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30016 return list;
30018 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
30019 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
30021 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30022 const char *p = IDENTIFIER_POINTER (id);
30024 if (strcmp ("alloc", p) == 0)
30025 kind = GOMP_MAP_ALLOC;
30026 else if (strcmp ("to", p) == 0)
30027 kind = GOMP_MAP_TO;
30028 else if (strcmp ("from", p) == 0)
30029 kind = GOMP_MAP_FROM;
30030 else if (strcmp ("tofrom", p) == 0)
30031 kind = GOMP_MAP_TOFROM;
30032 else
30034 cp_parser_error (parser, "invalid map kind");
30035 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30036 /*or_comma=*/false,
30037 /*consume_paren=*/true);
30038 return list;
30040 cp_lexer_consume_token (parser->lexer);
30041 cp_lexer_consume_token (parser->lexer);
30044 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
30045 NULL);
30047 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30048 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30050 return nlist;
30053 /* OpenMP 4.0:
30054 device ( expression ) */
30056 static tree
30057 cp_parser_omp_clause_device (cp_parser *parser, tree list,
30058 location_t location)
30060 tree t, c;
30062 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30063 return list;
30065 t = cp_parser_expression (parser);
30067 if (t == error_mark_node
30068 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30069 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30070 /*or_comma=*/false,
30071 /*consume_paren=*/true);
30073 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
30074 "device", location);
30076 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
30077 OMP_CLAUSE_DEVICE_ID (c) = t;
30078 OMP_CLAUSE_CHAIN (c) = list;
30080 return c;
30083 /* OpenMP 4.0:
30084 dist_schedule ( static )
30085 dist_schedule ( static , expression ) */
30087 static tree
30088 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
30089 location_t location)
30091 tree c, t;
30093 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30094 return list;
30096 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
30098 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
30099 goto invalid_kind;
30100 cp_lexer_consume_token (parser->lexer);
30102 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30104 cp_lexer_consume_token (parser->lexer);
30106 t = cp_parser_assignment_expression (parser);
30108 if (t == error_mark_node)
30109 goto resync_fail;
30110 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
30112 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30113 goto resync_fail;
30115 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30116 goto resync_fail;
30118 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
30119 location);
30120 OMP_CLAUSE_CHAIN (c) = list;
30121 return c;
30123 invalid_kind:
30124 cp_parser_error (parser, "invalid dist_schedule kind");
30125 resync_fail:
30126 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30127 /*or_comma=*/false,
30128 /*consume_paren=*/true);
30129 return list;
30132 /* OpenMP 4.0:
30133 proc_bind ( proc-bind-kind )
30135 proc-bind-kind:
30136 master | close | spread */
30138 static tree
30139 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
30140 location_t location)
30142 tree c;
30143 enum omp_clause_proc_bind_kind kind;
30145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30146 return list;
30148 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30150 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30151 const char *p = IDENTIFIER_POINTER (id);
30153 if (strcmp ("master", p) == 0)
30154 kind = OMP_CLAUSE_PROC_BIND_MASTER;
30155 else if (strcmp ("close", p) == 0)
30156 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
30157 else if (strcmp ("spread", p) == 0)
30158 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
30159 else
30160 goto invalid_kind;
30162 else
30163 goto invalid_kind;
30165 cp_lexer_consume_token (parser->lexer);
30166 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30167 goto resync_fail;
30169 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
30170 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
30171 location);
30172 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
30173 OMP_CLAUSE_CHAIN (c) = list;
30174 return c;
30176 invalid_kind:
30177 cp_parser_error (parser, "invalid depend kind");
30178 resync_fail:
30179 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30180 /*or_comma=*/false,
30181 /*consume_paren=*/true);
30182 return list;
30185 /* OpenACC:
30186 async [( int-expr )] */
30188 static tree
30189 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
30191 tree c, t;
30192 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30194 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
30196 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
30198 cp_lexer_consume_token (parser->lexer);
30200 t = cp_parser_expression (parser);
30201 if (t == error_mark_node
30202 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30203 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30204 /*or_comma=*/false,
30205 /*consume_paren=*/true);
30208 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
30210 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
30211 OMP_CLAUSE_ASYNC_EXPR (c) = t;
30212 OMP_CLAUSE_CHAIN (c) = list;
30213 list = c;
30215 return list;
30218 /* Parse all OpenACC clauses. The set clauses allowed by the directive
30219 is a bitmask in MASK. Return the list of clauses found. */
30221 static tree
30222 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
30223 const char *where, cp_token *pragma_tok,
30224 bool finish_p = true)
30226 tree clauses = NULL;
30227 bool first = true;
30229 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30231 location_t here;
30232 pragma_omp_clause c_kind;
30233 const char *c_name;
30234 tree prev = clauses;
30236 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30237 cp_lexer_consume_token (parser->lexer);
30239 here = cp_lexer_peek_token (parser->lexer)->location;
30240 c_kind = cp_parser_omp_clause_name (parser);
30242 switch (c_kind)
30244 case PRAGMA_OACC_CLAUSE_ASYNC:
30245 clauses = cp_parser_oacc_clause_async (parser, clauses);
30246 c_name = "async";
30247 break;
30248 case PRAGMA_OACC_CLAUSE_COLLAPSE:
30249 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
30250 c_name = "collapse";
30251 break;
30252 case PRAGMA_OACC_CLAUSE_COPY:
30253 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30254 c_name = "copy";
30255 break;
30256 case PRAGMA_OACC_CLAUSE_COPYIN:
30257 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30258 c_name = "copyin";
30259 break;
30260 case PRAGMA_OACC_CLAUSE_COPYOUT:
30261 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30262 c_name = "copyout";
30263 break;
30264 case PRAGMA_OACC_CLAUSE_CREATE:
30265 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30266 c_name = "create";
30267 break;
30268 case PRAGMA_OACC_CLAUSE_DELETE:
30269 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30270 c_name = "delete";
30271 break;
30272 case PRAGMA_OACC_CLAUSE_DEVICE:
30273 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30274 c_name = "device";
30275 break;
30276 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
30277 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
30278 c_name = "deviceptr";
30279 break;
30280 case PRAGMA_OACC_CLAUSE_HOST:
30281 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30282 c_name = "host";
30283 break;
30284 case PRAGMA_OACC_CLAUSE_IF:
30285 clauses = cp_parser_omp_clause_if (parser, clauses, here);
30286 c_name = "if";
30287 break;
30288 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
30289 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
30290 c_name = "num_gangs";
30291 break;
30292 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
30293 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
30294 c_name = "num_workers";
30295 break;
30296 case PRAGMA_OACC_CLAUSE_PRESENT:
30297 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30298 c_name = "present";
30299 break;
30300 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30301 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30302 c_name = "present_or_copy";
30303 break;
30304 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30305 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30306 c_name = "present_or_copyin";
30307 break;
30308 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30309 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30310 c_name = "present_or_copyout";
30311 break;
30312 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30313 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30314 c_name = "present_or_create";
30315 break;
30316 case PRAGMA_OACC_CLAUSE_REDUCTION:
30317 clauses = cp_parser_omp_clause_reduction (parser, clauses);
30318 c_name = "reduction";
30319 break;
30320 case PRAGMA_OACC_CLAUSE_SELF:
30321 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30322 c_name = "self";
30323 break;
30324 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
30325 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
30326 c_name = "vector_length";
30327 break;
30328 case PRAGMA_OACC_CLAUSE_WAIT:
30329 clauses = cp_parser_oacc_clause_wait (parser, clauses);
30330 c_name = "wait";
30331 break;
30332 default:
30333 cp_parser_error (parser, "expected %<#pragma acc%> clause");
30334 goto saw_error;
30337 first = false;
30339 if (((mask >> c_kind) & 1) == 0)
30341 /* Remove the invalid clause(s) from the list to avoid
30342 confusing the rest of the compiler. */
30343 clauses = prev;
30344 error_at (here, "%qs is not valid for %qs", c_name, where);
30348 saw_error:
30349 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30351 if (finish_p)
30352 return finish_omp_clauses (clauses);
30354 return clauses;
30357 /* Parse all OpenMP clauses. The set clauses allowed by the directive
30358 is a bitmask in MASK. Return the list of clauses found; the result
30359 of clause default goes in *pdefault. */
30361 static tree
30362 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
30363 const char *where, cp_token *pragma_tok,
30364 bool finish_p = true)
30366 tree clauses = NULL;
30367 bool first = true;
30368 cp_token *token = NULL;
30369 bool cilk_simd_fn = false;
30371 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30373 pragma_omp_clause c_kind;
30374 const char *c_name;
30375 tree prev = clauses;
30377 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30378 cp_lexer_consume_token (parser->lexer);
30380 token = cp_lexer_peek_token (parser->lexer);
30381 c_kind = cp_parser_omp_clause_name (parser);
30383 switch (c_kind)
30385 case PRAGMA_OMP_CLAUSE_COLLAPSE:
30386 clauses = cp_parser_omp_clause_collapse (parser, clauses,
30387 token->location);
30388 c_name = "collapse";
30389 break;
30390 case PRAGMA_OMP_CLAUSE_COPYIN:
30391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
30392 c_name = "copyin";
30393 break;
30394 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
30395 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
30396 clauses);
30397 c_name = "copyprivate";
30398 break;
30399 case PRAGMA_OMP_CLAUSE_DEFAULT:
30400 clauses = cp_parser_omp_clause_default (parser, clauses,
30401 token->location);
30402 c_name = "default";
30403 break;
30404 case PRAGMA_OMP_CLAUSE_FINAL:
30405 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
30406 c_name = "final";
30407 break;
30408 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
30409 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
30410 clauses);
30411 c_name = "firstprivate";
30412 break;
30413 case PRAGMA_OMP_CLAUSE_IF:
30414 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
30415 c_name = "if";
30416 break;
30417 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
30418 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
30419 clauses);
30420 c_name = "lastprivate";
30421 break;
30422 case PRAGMA_OMP_CLAUSE_MERGEABLE:
30423 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
30424 token->location);
30425 c_name = "mergeable";
30426 break;
30427 case PRAGMA_OMP_CLAUSE_NOWAIT:
30428 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
30429 c_name = "nowait";
30430 break;
30431 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
30432 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
30433 token->location);
30434 c_name = "num_threads";
30435 break;
30436 case PRAGMA_OMP_CLAUSE_ORDERED:
30437 clauses = cp_parser_omp_clause_ordered (parser, clauses,
30438 token->location);
30439 c_name = "ordered";
30440 break;
30441 case PRAGMA_OMP_CLAUSE_PRIVATE:
30442 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
30443 clauses);
30444 c_name = "private";
30445 break;
30446 case PRAGMA_OMP_CLAUSE_REDUCTION:
30447 clauses = cp_parser_omp_clause_reduction (parser, clauses);
30448 c_name = "reduction";
30449 break;
30450 case PRAGMA_OMP_CLAUSE_SCHEDULE:
30451 clauses = cp_parser_omp_clause_schedule (parser, clauses,
30452 token->location);
30453 c_name = "schedule";
30454 break;
30455 case PRAGMA_OMP_CLAUSE_SHARED:
30456 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
30457 clauses);
30458 c_name = "shared";
30459 break;
30460 case PRAGMA_OMP_CLAUSE_UNTIED:
30461 clauses = cp_parser_omp_clause_untied (parser, clauses,
30462 token->location);
30463 c_name = "untied";
30464 break;
30465 case PRAGMA_OMP_CLAUSE_INBRANCH:
30466 case PRAGMA_CILK_CLAUSE_MASK:
30467 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
30468 clauses, token->location);
30469 c_name = "inbranch";
30470 break;
30471 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
30472 case PRAGMA_CILK_CLAUSE_NOMASK:
30473 clauses = cp_parser_omp_clause_branch (parser,
30474 OMP_CLAUSE_NOTINBRANCH,
30475 clauses, token->location);
30476 c_name = "notinbranch";
30477 break;
30478 case PRAGMA_OMP_CLAUSE_PARALLEL:
30479 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
30480 clauses, token->location);
30481 c_name = "parallel";
30482 if (!first)
30484 clause_not_first:
30485 error_at (token->location, "%qs must be the first clause of %qs",
30486 c_name, where);
30487 clauses = prev;
30489 break;
30490 case PRAGMA_OMP_CLAUSE_FOR:
30491 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
30492 clauses, token->location);
30493 c_name = "for";
30494 if (!first)
30495 goto clause_not_first;
30496 break;
30497 case PRAGMA_OMP_CLAUSE_SECTIONS:
30498 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
30499 clauses, token->location);
30500 c_name = "sections";
30501 if (!first)
30502 goto clause_not_first;
30503 break;
30504 case PRAGMA_OMP_CLAUSE_TASKGROUP:
30505 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
30506 clauses, token->location);
30507 c_name = "taskgroup";
30508 if (!first)
30509 goto clause_not_first;
30510 break;
30511 case PRAGMA_OMP_CLAUSE_TO:
30512 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
30513 clauses);
30514 c_name = "to";
30515 break;
30516 case PRAGMA_OMP_CLAUSE_FROM:
30517 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
30518 clauses);
30519 c_name = "from";
30520 break;
30521 case PRAGMA_OMP_CLAUSE_UNIFORM:
30522 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
30523 clauses);
30524 c_name = "uniform";
30525 break;
30526 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
30527 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
30528 token->location);
30529 c_name = "num_teams";
30530 break;
30531 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
30532 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
30533 token->location);
30534 c_name = "thread_limit";
30535 break;
30536 case PRAGMA_OMP_CLAUSE_ALIGNED:
30537 clauses = cp_parser_omp_clause_aligned (parser, clauses);
30538 c_name = "aligned";
30539 break;
30540 case PRAGMA_OMP_CLAUSE_LINEAR:
30541 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
30542 cilk_simd_fn = true;
30543 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
30544 c_name = "linear";
30545 break;
30546 case PRAGMA_OMP_CLAUSE_DEPEND:
30547 clauses = cp_parser_omp_clause_depend (parser, clauses);
30548 c_name = "depend";
30549 break;
30550 case PRAGMA_OMP_CLAUSE_MAP:
30551 clauses = cp_parser_omp_clause_map (parser, clauses);
30552 c_name = "map";
30553 break;
30554 case PRAGMA_OMP_CLAUSE_DEVICE:
30555 clauses = cp_parser_omp_clause_device (parser, clauses,
30556 token->location);
30557 c_name = "device";
30558 break;
30559 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
30560 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
30561 token->location);
30562 c_name = "dist_schedule";
30563 break;
30564 case PRAGMA_OMP_CLAUSE_PROC_BIND:
30565 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
30566 token->location);
30567 c_name = "proc_bind";
30568 break;
30569 case PRAGMA_OMP_CLAUSE_SAFELEN:
30570 clauses = cp_parser_omp_clause_safelen (parser, clauses,
30571 token->location);
30572 c_name = "safelen";
30573 break;
30574 case PRAGMA_OMP_CLAUSE_SIMDLEN:
30575 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
30576 token->location);
30577 c_name = "simdlen";
30578 break;
30579 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
30580 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
30581 c_name = "simdlen";
30582 break;
30583 default:
30584 cp_parser_error (parser, "expected %<#pragma omp%> clause");
30585 goto saw_error;
30588 first = false;
30590 if (((mask >> c_kind) & 1) == 0)
30592 /* Remove the invalid clause(s) from the list to avoid
30593 confusing the rest of the compiler. */
30594 clauses = prev;
30595 error_at (token->location, "%qs is not valid for %qs", c_name, where);
30598 saw_error:
30599 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
30600 no reason to skip to the end. */
30601 if (!(flag_cilkplus && pragma_tok == NULL))
30602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30603 if (finish_p)
30604 return finish_omp_clauses (clauses);
30605 return clauses;
30608 /* OpenMP 2.5:
30609 structured-block:
30610 statement
30612 In practice, we're also interested in adding the statement to an
30613 outer node. So it is convenient if we work around the fact that
30614 cp_parser_statement calls add_stmt. */
30616 static unsigned
30617 cp_parser_begin_omp_structured_block (cp_parser *parser)
30619 unsigned save = parser->in_statement;
30621 /* Only move the values to IN_OMP_BLOCK if they weren't false.
30622 This preserves the "not within loop or switch" style error messages
30623 for nonsense cases like
30624 void foo() {
30625 #pragma omp single
30626 break;
30629 if (parser->in_statement)
30630 parser->in_statement = IN_OMP_BLOCK;
30632 return save;
30635 static void
30636 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
30638 parser->in_statement = save;
30641 static tree
30642 cp_parser_omp_structured_block (cp_parser *parser)
30644 tree stmt = begin_omp_structured_block ();
30645 unsigned int save = cp_parser_begin_omp_structured_block (parser);
30647 cp_parser_statement (parser, NULL_TREE, false, NULL);
30649 cp_parser_end_omp_structured_block (parser, save);
30650 return finish_omp_structured_block (stmt);
30653 /* OpenMP 2.5:
30654 # pragma omp atomic new-line
30655 expression-stmt
30657 expression-stmt:
30658 x binop= expr | x++ | ++x | x-- | --x
30659 binop:
30660 +, *, -, /, &, ^, |, <<, >>
30662 where x is an lvalue expression with scalar type.
30664 OpenMP 3.1:
30665 # pragma omp atomic new-line
30666 update-stmt
30668 # pragma omp atomic read new-line
30669 read-stmt
30671 # pragma omp atomic write new-line
30672 write-stmt
30674 # pragma omp atomic update new-line
30675 update-stmt
30677 # pragma omp atomic capture new-line
30678 capture-stmt
30680 # pragma omp atomic capture new-line
30681 capture-block
30683 read-stmt:
30684 v = x
30685 write-stmt:
30686 x = expr
30687 update-stmt:
30688 expression-stmt | x = x binop expr
30689 capture-stmt:
30690 v = expression-stmt
30691 capture-block:
30692 { v = x; update-stmt; } | { update-stmt; v = x; }
30694 OpenMP 4.0:
30695 update-stmt:
30696 expression-stmt | x = x binop expr | x = expr binop x
30697 capture-stmt:
30698 v = update-stmt
30699 capture-block:
30700 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
30702 where x and v are lvalue expressions with scalar type. */
30704 static void
30705 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
30707 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
30708 tree rhs1 = NULL_TREE, orig_lhs;
30709 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
30710 bool structured_block = false;
30711 bool seq_cst = false;
30713 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30715 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30716 const char *p = IDENTIFIER_POINTER (id);
30718 if (!strcmp (p, "seq_cst"))
30720 seq_cst = true;
30721 cp_lexer_consume_token (parser->lexer);
30722 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30723 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30724 cp_lexer_consume_token (parser->lexer);
30727 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30729 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30730 const char *p = IDENTIFIER_POINTER (id);
30732 if (!strcmp (p, "read"))
30733 code = OMP_ATOMIC_READ;
30734 else if (!strcmp (p, "write"))
30735 code = NOP_EXPR;
30736 else if (!strcmp (p, "update"))
30737 code = OMP_ATOMIC;
30738 else if (!strcmp (p, "capture"))
30739 code = OMP_ATOMIC_CAPTURE_NEW;
30740 else
30741 p = NULL;
30742 if (p)
30743 cp_lexer_consume_token (parser->lexer);
30745 if (!seq_cst)
30747 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30748 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30749 cp_lexer_consume_token (parser->lexer);
30751 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30753 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30754 const char *p = IDENTIFIER_POINTER (id);
30756 if (!strcmp (p, "seq_cst"))
30758 seq_cst = true;
30759 cp_lexer_consume_token (parser->lexer);
30763 cp_parser_require_pragma_eol (parser, pragma_tok);
30765 switch (code)
30767 case OMP_ATOMIC_READ:
30768 case NOP_EXPR: /* atomic write */
30769 v = cp_parser_unary_expression (parser);
30770 if (v == error_mark_node)
30771 goto saw_error;
30772 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30773 goto saw_error;
30774 if (code == NOP_EXPR)
30775 lhs = cp_parser_expression (parser);
30776 else
30777 lhs = cp_parser_unary_expression (parser);
30778 if (lhs == error_mark_node)
30779 goto saw_error;
30780 if (code == NOP_EXPR)
30782 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
30783 opcode. */
30784 code = OMP_ATOMIC;
30785 rhs = lhs;
30786 lhs = v;
30787 v = NULL_TREE;
30789 goto done;
30790 case OMP_ATOMIC_CAPTURE_NEW:
30791 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30793 cp_lexer_consume_token (parser->lexer);
30794 structured_block = true;
30796 else
30798 v = cp_parser_unary_expression (parser);
30799 if (v == error_mark_node)
30800 goto saw_error;
30801 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30802 goto saw_error;
30804 default:
30805 break;
30808 restart:
30809 lhs = cp_parser_unary_expression (parser);
30810 orig_lhs = lhs;
30811 switch (TREE_CODE (lhs))
30813 case ERROR_MARK:
30814 goto saw_error;
30816 case POSTINCREMENT_EXPR:
30817 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
30818 code = OMP_ATOMIC_CAPTURE_OLD;
30819 /* FALLTHROUGH */
30820 case PREINCREMENT_EXPR:
30821 lhs = TREE_OPERAND (lhs, 0);
30822 opcode = PLUS_EXPR;
30823 rhs = integer_one_node;
30824 break;
30826 case POSTDECREMENT_EXPR:
30827 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
30828 code = OMP_ATOMIC_CAPTURE_OLD;
30829 /* FALLTHROUGH */
30830 case PREDECREMENT_EXPR:
30831 lhs = TREE_OPERAND (lhs, 0);
30832 opcode = MINUS_EXPR;
30833 rhs = integer_one_node;
30834 break;
30836 case COMPOUND_EXPR:
30837 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
30838 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
30839 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
30840 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
30841 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
30842 (TREE_OPERAND (lhs, 1), 0), 0)))
30843 == BOOLEAN_TYPE)
30844 /* Undo effects of boolean_increment for post {in,de}crement. */
30845 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
30846 /* FALLTHRU */
30847 case MODIFY_EXPR:
30848 if (TREE_CODE (lhs) == MODIFY_EXPR
30849 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
30851 /* Undo effects of boolean_increment. */
30852 if (integer_onep (TREE_OPERAND (lhs, 1)))
30854 /* This is pre or post increment. */
30855 rhs = TREE_OPERAND (lhs, 1);
30856 lhs = TREE_OPERAND (lhs, 0);
30857 opcode = NOP_EXPR;
30858 if (code == OMP_ATOMIC_CAPTURE_NEW
30859 && !structured_block
30860 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
30861 code = OMP_ATOMIC_CAPTURE_OLD;
30862 break;
30865 /* FALLTHRU */
30866 default:
30867 switch (cp_lexer_peek_token (parser->lexer)->type)
30869 case CPP_MULT_EQ:
30870 opcode = MULT_EXPR;
30871 break;
30872 case CPP_DIV_EQ:
30873 opcode = TRUNC_DIV_EXPR;
30874 break;
30875 case CPP_PLUS_EQ:
30876 opcode = PLUS_EXPR;
30877 break;
30878 case CPP_MINUS_EQ:
30879 opcode = MINUS_EXPR;
30880 break;
30881 case CPP_LSHIFT_EQ:
30882 opcode = LSHIFT_EXPR;
30883 break;
30884 case CPP_RSHIFT_EQ:
30885 opcode = RSHIFT_EXPR;
30886 break;
30887 case CPP_AND_EQ:
30888 opcode = BIT_AND_EXPR;
30889 break;
30890 case CPP_OR_EQ:
30891 opcode = BIT_IOR_EXPR;
30892 break;
30893 case CPP_XOR_EQ:
30894 opcode = BIT_XOR_EXPR;
30895 break;
30896 case CPP_EQ:
30897 enum cp_parser_prec oprec;
30898 cp_token *token;
30899 cp_lexer_consume_token (parser->lexer);
30900 cp_parser_parse_tentatively (parser);
30901 rhs1 = cp_parser_simple_cast_expression (parser);
30902 if (rhs1 == error_mark_node)
30904 cp_parser_abort_tentative_parse (parser);
30905 cp_parser_simple_cast_expression (parser);
30906 goto saw_error;
30908 token = cp_lexer_peek_token (parser->lexer);
30909 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
30911 cp_parser_abort_tentative_parse (parser);
30912 cp_parser_parse_tentatively (parser);
30913 rhs = cp_parser_binary_expression (parser, false, true,
30914 PREC_NOT_OPERATOR, NULL);
30915 if (rhs == error_mark_node)
30917 cp_parser_abort_tentative_parse (parser);
30918 cp_parser_binary_expression (parser, false, true,
30919 PREC_NOT_OPERATOR, NULL);
30920 goto saw_error;
30922 switch (TREE_CODE (rhs))
30924 case MULT_EXPR:
30925 case TRUNC_DIV_EXPR:
30926 case RDIV_EXPR:
30927 case PLUS_EXPR:
30928 case MINUS_EXPR:
30929 case LSHIFT_EXPR:
30930 case RSHIFT_EXPR:
30931 case BIT_AND_EXPR:
30932 case BIT_IOR_EXPR:
30933 case BIT_XOR_EXPR:
30934 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
30936 if (cp_parser_parse_definitely (parser))
30938 opcode = TREE_CODE (rhs);
30939 rhs1 = TREE_OPERAND (rhs, 0);
30940 rhs = TREE_OPERAND (rhs, 1);
30941 goto stmt_done;
30943 else
30944 goto saw_error;
30946 break;
30947 default:
30948 break;
30950 cp_parser_abort_tentative_parse (parser);
30951 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
30953 rhs = cp_parser_expression (parser);
30954 if (rhs == error_mark_node)
30955 goto saw_error;
30956 opcode = NOP_EXPR;
30957 rhs1 = NULL_TREE;
30958 goto stmt_done;
30960 cp_parser_error (parser,
30961 "invalid form of %<#pragma omp atomic%>");
30962 goto saw_error;
30964 if (!cp_parser_parse_definitely (parser))
30965 goto saw_error;
30966 switch (token->type)
30968 case CPP_SEMICOLON:
30969 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30971 code = OMP_ATOMIC_CAPTURE_OLD;
30972 v = lhs;
30973 lhs = NULL_TREE;
30974 lhs1 = rhs1;
30975 rhs1 = NULL_TREE;
30976 cp_lexer_consume_token (parser->lexer);
30977 goto restart;
30979 else if (structured_block)
30981 opcode = NOP_EXPR;
30982 rhs = rhs1;
30983 rhs1 = NULL_TREE;
30984 goto stmt_done;
30986 cp_parser_error (parser,
30987 "invalid form of %<#pragma omp atomic%>");
30988 goto saw_error;
30989 case CPP_MULT:
30990 opcode = MULT_EXPR;
30991 break;
30992 case CPP_DIV:
30993 opcode = TRUNC_DIV_EXPR;
30994 break;
30995 case CPP_PLUS:
30996 opcode = PLUS_EXPR;
30997 break;
30998 case CPP_MINUS:
30999 opcode = MINUS_EXPR;
31000 break;
31001 case CPP_LSHIFT:
31002 opcode = LSHIFT_EXPR;
31003 break;
31004 case CPP_RSHIFT:
31005 opcode = RSHIFT_EXPR;
31006 break;
31007 case CPP_AND:
31008 opcode = BIT_AND_EXPR;
31009 break;
31010 case CPP_OR:
31011 opcode = BIT_IOR_EXPR;
31012 break;
31013 case CPP_XOR:
31014 opcode = BIT_XOR_EXPR;
31015 break;
31016 default:
31017 cp_parser_error (parser,
31018 "invalid operator for %<#pragma omp atomic%>");
31019 goto saw_error;
31021 oprec = TOKEN_PRECEDENCE (token);
31022 gcc_assert (oprec != PREC_NOT_OPERATOR);
31023 if (commutative_tree_code (opcode))
31024 oprec = (enum cp_parser_prec) (oprec - 1);
31025 cp_lexer_consume_token (parser->lexer);
31026 rhs = cp_parser_binary_expression (parser, false, false,
31027 oprec, NULL);
31028 if (rhs == error_mark_node)
31029 goto saw_error;
31030 goto stmt_done;
31031 /* FALLTHROUGH */
31032 default:
31033 cp_parser_error (parser,
31034 "invalid operator for %<#pragma omp atomic%>");
31035 goto saw_error;
31037 cp_lexer_consume_token (parser->lexer);
31039 rhs = cp_parser_expression (parser);
31040 if (rhs == error_mark_node)
31041 goto saw_error;
31042 break;
31044 stmt_done:
31045 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
31047 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
31048 goto saw_error;
31049 v = cp_parser_unary_expression (parser);
31050 if (v == error_mark_node)
31051 goto saw_error;
31052 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
31053 goto saw_error;
31054 lhs1 = cp_parser_unary_expression (parser);
31055 if (lhs1 == error_mark_node)
31056 goto saw_error;
31058 if (structured_block)
31060 cp_parser_consume_semicolon_at_end_of_statement (parser);
31061 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
31063 done:
31064 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
31065 if (!structured_block)
31066 cp_parser_consume_semicolon_at_end_of_statement (parser);
31067 return;
31069 saw_error:
31070 cp_parser_skip_to_end_of_block_or_statement (parser);
31071 if (structured_block)
31073 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31074 cp_lexer_consume_token (parser->lexer);
31075 else if (code == OMP_ATOMIC_CAPTURE_NEW)
31077 cp_parser_skip_to_end_of_block_or_statement (parser);
31078 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31079 cp_lexer_consume_token (parser->lexer);
31085 /* OpenMP 2.5:
31086 # pragma omp barrier new-line */
31088 static void
31089 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
31091 cp_parser_require_pragma_eol (parser, pragma_tok);
31092 finish_omp_barrier ();
31095 /* OpenMP 2.5:
31096 # pragma omp critical [(name)] new-line
31097 structured-block */
31099 static tree
31100 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
31102 tree stmt, name = NULL;
31104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31106 cp_lexer_consume_token (parser->lexer);
31108 name = cp_parser_identifier (parser);
31110 if (name == error_mark_node
31111 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31112 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31113 /*or_comma=*/false,
31114 /*consume_paren=*/true);
31115 if (name == error_mark_node)
31116 name = NULL;
31118 cp_parser_require_pragma_eol (parser, pragma_tok);
31120 stmt = cp_parser_omp_structured_block (parser);
31121 return c_finish_omp_critical (input_location, stmt, name);
31124 /* OpenMP 2.5:
31125 # pragma omp flush flush-vars[opt] new-line
31127 flush-vars:
31128 ( variable-list ) */
31130 static void
31131 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
31133 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31134 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31135 cp_parser_require_pragma_eol (parser, pragma_tok);
31137 finish_omp_flush ();
31140 /* Helper function, to parse omp for increment expression. */
31142 static tree
31143 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
31145 tree cond = cp_parser_binary_expression (parser, false, true,
31146 PREC_NOT_OPERATOR, NULL);
31147 if (cond == error_mark_node
31148 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31150 cp_parser_skip_to_end_of_statement (parser);
31151 return error_mark_node;
31154 switch (TREE_CODE (cond))
31156 case GT_EXPR:
31157 case GE_EXPR:
31158 case LT_EXPR:
31159 case LE_EXPR:
31160 break;
31161 case NE_EXPR:
31162 if (code == CILK_SIMD || code == CILK_FOR)
31163 break;
31164 /* Fall through: OpenMP disallows NE_EXPR. */
31165 default:
31166 return error_mark_node;
31169 /* If decl is an iterator, preserve LHS and RHS of the relational
31170 expr until finish_omp_for. */
31171 if (decl
31172 && (type_dependent_expression_p (decl)
31173 || CLASS_TYPE_P (TREE_TYPE (decl))))
31174 return cond;
31176 return build_x_binary_op (input_location, TREE_CODE (cond),
31177 TREE_OPERAND (cond, 0), ERROR_MARK,
31178 TREE_OPERAND (cond, 1), ERROR_MARK,
31179 /*overload=*/NULL, tf_warning_or_error);
31182 /* Helper function, to parse omp for increment expression. */
31184 static tree
31185 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
31187 cp_token *token = cp_lexer_peek_token (parser->lexer);
31188 enum tree_code op;
31189 tree lhs, rhs;
31190 cp_id_kind idk;
31191 bool decl_first;
31193 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
31195 op = (token->type == CPP_PLUS_PLUS
31196 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
31197 cp_lexer_consume_token (parser->lexer);
31198 lhs = cp_parser_simple_cast_expression (parser);
31199 if (lhs != decl)
31200 return error_mark_node;
31201 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
31204 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
31205 if (lhs != decl)
31206 return error_mark_node;
31208 token = cp_lexer_peek_token (parser->lexer);
31209 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
31211 op = (token->type == CPP_PLUS_PLUS
31212 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
31213 cp_lexer_consume_token (parser->lexer);
31214 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
31217 op = cp_parser_assignment_operator_opt (parser);
31218 if (op == ERROR_MARK)
31219 return error_mark_node;
31221 if (op != NOP_EXPR)
31223 rhs = cp_parser_assignment_expression (parser);
31224 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
31225 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
31228 lhs = cp_parser_binary_expression (parser, false, false,
31229 PREC_ADDITIVE_EXPRESSION, NULL);
31230 token = cp_lexer_peek_token (parser->lexer);
31231 decl_first = lhs == decl;
31232 if (decl_first)
31233 lhs = NULL_TREE;
31234 if (token->type != CPP_PLUS
31235 && token->type != CPP_MINUS)
31236 return error_mark_node;
31240 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
31241 cp_lexer_consume_token (parser->lexer);
31242 rhs = cp_parser_binary_expression (parser, false, false,
31243 PREC_ADDITIVE_EXPRESSION, NULL);
31244 token = cp_lexer_peek_token (parser->lexer);
31245 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
31247 if (lhs == NULL_TREE)
31249 if (op == PLUS_EXPR)
31250 lhs = rhs;
31251 else
31252 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
31253 tf_warning_or_error);
31255 else
31256 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
31257 ERROR_MARK, NULL, tf_warning_or_error);
31260 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
31262 if (!decl_first)
31264 if (rhs != decl || op == MINUS_EXPR)
31265 return error_mark_node;
31266 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
31268 else
31269 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
31271 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
31274 /* Parse the initialization statement of either an OpenMP for loop or
31275 a Cilk Plus for loop.
31277 Return true if the resulting construct should have an
31278 OMP_CLAUSE_PRIVATE added to it. */
31280 static bool
31281 cp_parser_omp_for_loop_init (cp_parser *parser,
31282 enum tree_code code,
31283 tree &this_pre_body,
31284 vec<tree, va_gc> *for_block,
31285 tree &init,
31286 tree &decl,
31287 tree &real_decl)
31289 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31290 return false;
31292 bool add_private_clause = false;
31294 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
31296 init-expr:
31297 var = lb
31298 integer-type var = lb
31299 random-access-iterator-type var = lb
31300 pointer-type var = lb
31302 cp_decl_specifier_seq type_specifiers;
31304 /* First, try to parse as an initialized declaration. See
31305 cp_parser_condition, from whence the bulk of this is copied. */
31307 cp_parser_parse_tentatively (parser);
31308 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
31309 /*is_trailing_return=*/false,
31310 &type_specifiers);
31311 if (cp_parser_parse_definitely (parser))
31313 /* If parsing a type specifier seq succeeded, then this
31314 MUST be a initialized declaration. */
31315 tree asm_specification, attributes;
31316 cp_declarator *declarator;
31318 declarator = cp_parser_declarator (parser,
31319 CP_PARSER_DECLARATOR_NAMED,
31320 /*ctor_dtor_or_conv_p=*/NULL,
31321 /*parenthesized_p=*/NULL,
31322 /*member_p=*/false,
31323 /*friend_p=*/false);
31324 attributes = cp_parser_attributes_opt (parser);
31325 asm_specification = cp_parser_asm_specification_opt (parser);
31327 if (declarator == cp_error_declarator)
31328 cp_parser_skip_to_end_of_statement (parser);
31330 else
31332 tree pushed_scope, auto_node;
31334 decl = start_decl (declarator, &type_specifiers,
31335 SD_INITIALIZED, attributes,
31336 /*prefix_attributes=*/NULL_TREE,
31337 &pushed_scope);
31339 auto_node = type_uses_auto (TREE_TYPE (decl));
31340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31342 if (cp_lexer_next_token_is (parser->lexer,
31343 CPP_OPEN_PAREN))
31345 if (code != CILK_SIMD && code != CILK_FOR)
31346 error ("parenthesized initialization is not allowed in "
31347 "OpenMP %<for%> loop");
31348 else
31349 error ("parenthesized initialization is "
31350 "not allowed in for-loop");
31352 else
31353 /* Trigger an error. */
31354 cp_parser_require (parser, CPP_EQ, RT_EQ);
31356 init = error_mark_node;
31357 cp_parser_skip_to_end_of_statement (parser);
31359 else if (CLASS_TYPE_P (TREE_TYPE (decl))
31360 || type_dependent_expression_p (decl)
31361 || auto_node)
31363 bool is_direct_init, is_non_constant_init;
31365 init = cp_parser_initializer (parser,
31366 &is_direct_init,
31367 &is_non_constant_init);
31369 if (auto_node)
31371 TREE_TYPE (decl)
31372 = do_auto_deduction (TREE_TYPE (decl), init,
31373 auto_node);
31375 if (!CLASS_TYPE_P (TREE_TYPE (decl))
31376 && !type_dependent_expression_p (decl))
31377 goto non_class;
31380 cp_finish_decl (decl, init, !is_non_constant_init,
31381 asm_specification,
31382 LOOKUP_ONLYCONVERTING);
31383 if (CLASS_TYPE_P (TREE_TYPE (decl)))
31385 vec_safe_push (for_block, this_pre_body);
31386 init = NULL_TREE;
31388 else
31389 init = pop_stmt_list (this_pre_body);
31390 this_pre_body = NULL_TREE;
31392 else
31394 /* Consume '='. */
31395 cp_lexer_consume_token (parser->lexer);
31396 init = cp_parser_assignment_expression (parser);
31398 non_class:
31399 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
31400 init = error_mark_node;
31401 else
31402 cp_finish_decl (decl, NULL_TREE,
31403 /*init_const_expr_p=*/false,
31404 asm_specification,
31405 LOOKUP_ONLYCONVERTING);
31408 if (pushed_scope)
31409 pop_scope (pushed_scope);
31412 else
31414 cp_id_kind idk;
31415 /* If parsing a type specifier sequence failed, then
31416 this MUST be a simple expression. */
31417 if (code == CILK_FOR)
31418 error ("%<_Cilk_for%> allows expression instead of declaration only "
31419 "in C, not in C++");
31420 cp_parser_parse_tentatively (parser);
31421 decl = cp_parser_primary_expression (parser, false, false,
31422 false, &idk);
31423 if (!cp_parser_error_occurred (parser)
31424 && decl
31425 && DECL_P (decl)
31426 && CLASS_TYPE_P (TREE_TYPE (decl)))
31428 tree rhs;
31430 cp_parser_parse_definitely (parser);
31431 cp_parser_require (parser, CPP_EQ, RT_EQ);
31432 rhs = cp_parser_assignment_expression (parser);
31433 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
31434 decl, NOP_EXPR,
31435 rhs,
31436 tf_warning_or_error));
31437 add_private_clause = true;
31439 else
31441 decl = NULL;
31442 cp_parser_abort_tentative_parse (parser);
31443 init = cp_parser_expression (parser);
31444 if (init)
31446 if (TREE_CODE (init) == MODIFY_EXPR
31447 || TREE_CODE (init) == MODOP_EXPR)
31448 real_decl = TREE_OPERAND (init, 0);
31452 return add_private_clause;
31455 /* Parse the restricted form of the for statement allowed by OpenMP. */
31457 static tree
31458 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
31459 tree *cclauses)
31461 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
31462 tree real_decl, initv, condv, incrv, declv;
31463 tree this_pre_body, cl;
31464 location_t loc_first;
31465 bool collapse_err = false;
31466 int i, collapse = 1, nbraces = 0;
31467 vec<tree, va_gc> *for_block = make_tree_vector ();
31469 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
31470 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
31471 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
31473 gcc_assert (collapse >= 1);
31475 declv = make_tree_vec (collapse);
31476 initv = make_tree_vec (collapse);
31477 condv = make_tree_vec (collapse);
31478 incrv = make_tree_vec (collapse);
31480 loc_first = cp_lexer_peek_token (parser->lexer)->location;
31482 for (i = 0; i < collapse; i++)
31484 int bracecount = 0;
31485 bool add_private_clause = false;
31486 location_t loc;
31488 if (code != CILK_FOR
31489 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31491 cp_parser_error (parser, "for statement expected");
31492 return NULL;
31494 if (code == CILK_FOR
31495 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31497 cp_parser_error (parser, "_Cilk_for statement expected");
31498 return NULL;
31500 loc = cp_lexer_consume_token (parser->lexer)->location;
31502 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31503 return NULL;
31505 init = decl = real_decl = NULL;
31506 this_pre_body = push_stmt_list ();
31508 add_private_clause
31509 |= cp_parser_omp_for_loop_init (parser, code,
31510 this_pre_body, for_block,
31511 init, decl, real_decl);
31513 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31514 if (this_pre_body)
31516 this_pre_body = pop_stmt_list (this_pre_body);
31517 if (pre_body)
31519 tree t = pre_body;
31520 pre_body = push_stmt_list ();
31521 add_stmt (t);
31522 add_stmt (this_pre_body);
31523 pre_body = pop_stmt_list (pre_body);
31525 else
31526 pre_body = this_pre_body;
31529 if (decl)
31530 real_decl = decl;
31531 if (cclauses != NULL
31532 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
31533 && real_decl != NULL_TREE)
31535 tree *c;
31536 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
31537 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
31538 && OMP_CLAUSE_DECL (*c) == real_decl)
31540 error_at (loc, "iteration variable %qD"
31541 " should not be firstprivate", real_decl);
31542 *c = OMP_CLAUSE_CHAIN (*c);
31544 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
31545 && OMP_CLAUSE_DECL (*c) == real_decl)
31547 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
31548 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
31549 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
31550 OMP_CLAUSE_DECL (l) = real_decl;
31551 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
31552 if (code == OMP_SIMD)
31554 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31555 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
31557 else
31559 OMP_CLAUSE_CHAIN (l) = clauses;
31560 clauses = l;
31562 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
31563 CP_OMP_CLAUSE_INFO (*c) = NULL;
31564 add_private_clause = false;
31566 else
31568 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
31569 && OMP_CLAUSE_DECL (*c) == real_decl)
31570 add_private_clause = false;
31571 c = &OMP_CLAUSE_CHAIN (*c);
31575 if (add_private_clause)
31577 tree c;
31578 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31580 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
31581 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
31582 && OMP_CLAUSE_DECL (c) == decl)
31583 break;
31584 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
31585 && OMP_CLAUSE_DECL (c) == decl)
31586 error_at (loc, "iteration variable %qD "
31587 "should not be firstprivate",
31588 decl);
31589 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
31590 && OMP_CLAUSE_DECL (c) == decl)
31591 error_at (loc, "iteration variable %qD should not be reduction",
31592 decl);
31594 if (c == NULL)
31596 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
31597 OMP_CLAUSE_DECL (c) = decl;
31598 c = finish_omp_clauses (c);
31599 if (c)
31601 OMP_CLAUSE_CHAIN (c) = clauses;
31602 clauses = c;
31607 cond = NULL;
31608 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31609 cond = cp_parser_omp_for_cond (parser, decl, code);
31610 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31612 incr = NULL;
31613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31615 /* If decl is an iterator, preserve the operator on decl
31616 until finish_omp_for. */
31617 if (real_decl
31618 && ((processing_template_decl
31619 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
31620 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
31621 incr = cp_parser_omp_for_incr (parser, real_decl);
31622 else
31623 incr = cp_parser_expression (parser);
31624 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
31625 SET_EXPR_LOCATION (incr, input_location);
31628 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31629 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31630 /*or_comma=*/false,
31631 /*consume_paren=*/true);
31633 TREE_VEC_ELT (declv, i) = decl;
31634 TREE_VEC_ELT (initv, i) = init;
31635 TREE_VEC_ELT (condv, i) = cond;
31636 TREE_VEC_ELT (incrv, i) = incr;
31638 if (i == collapse - 1)
31639 break;
31641 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
31642 in between the collapsed for loops to be still considered perfectly
31643 nested. Hopefully the final version clarifies this.
31644 For now handle (multiple) {'s and empty statements. */
31645 cp_parser_parse_tentatively (parser);
31648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31649 break;
31650 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31652 cp_lexer_consume_token (parser->lexer);
31653 bracecount++;
31655 else if (bracecount
31656 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31657 cp_lexer_consume_token (parser->lexer);
31658 else
31660 loc = cp_lexer_peek_token (parser->lexer)->location;
31661 error_at (loc, "not enough collapsed for loops");
31662 collapse_err = true;
31663 cp_parser_abort_tentative_parse (parser);
31664 declv = NULL_TREE;
31665 break;
31668 while (1);
31670 if (declv)
31672 cp_parser_parse_definitely (parser);
31673 nbraces += bracecount;
31677 /* Note that we saved the original contents of this flag when we entered
31678 the structured block, and so we don't need to re-save it here. */
31679 if (code == CILK_SIMD || code == CILK_FOR)
31680 parser->in_statement = IN_CILK_SIMD_FOR;
31681 else
31682 parser->in_statement = IN_OMP_FOR;
31684 /* Note that the grammar doesn't call for a structured block here,
31685 though the loop as a whole is a structured block. */
31686 body = push_stmt_list ();
31687 cp_parser_statement (parser, NULL_TREE, false, NULL);
31688 body = pop_stmt_list (body);
31690 if (declv == NULL_TREE)
31691 ret = NULL_TREE;
31692 else
31693 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
31694 pre_body, clauses);
31696 while (nbraces)
31698 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31700 cp_lexer_consume_token (parser->lexer);
31701 nbraces--;
31703 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31704 cp_lexer_consume_token (parser->lexer);
31705 else
31707 if (!collapse_err)
31709 error_at (cp_lexer_peek_token (parser->lexer)->location,
31710 "collapsed loops not perfectly nested");
31712 collapse_err = true;
31713 cp_parser_statement_seq_opt (parser, NULL);
31714 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
31715 break;
31719 while (!for_block->is_empty ())
31720 add_stmt (pop_stmt_list (for_block->pop ()));
31721 release_tree_vector (for_block);
31723 return ret;
31726 /* Helper function for OpenMP parsing, split clauses and call
31727 finish_omp_clauses on each of the set of clauses afterwards. */
31729 static void
31730 cp_omp_split_clauses (location_t loc, enum tree_code code,
31731 omp_clause_mask mask, tree clauses, tree *cclauses)
31733 int i;
31734 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
31735 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
31736 if (cclauses[i])
31737 cclauses[i] = finish_omp_clauses (cclauses[i]);
31740 /* OpenMP 4.0:
31741 #pragma omp simd simd-clause[optseq] new-line
31742 for-loop */
31744 #define OMP_SIMD_CLAUSE_MASK \
31745 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
31746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31753 static tree
31754 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
31755 char *p_name, omp_clause_mask mask, tree *cclauses)
31757 tree clauses, sb, ret;
31758 unsigned int save;
31759 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31761 strcat (p_name, " simd");
31762 mask |= OMP_SIMD_CLAUSE_MASK;
31763 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
31765 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31766 cclauses == NULL);
31767 if (cclauses)
31769 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
31770 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
31773 sb = begin_omp_structured_block ();
31774 save = cp_parser_begin_omp_structured_block (parser);
31776 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
31778 cp_parser_end_omp_structured_block (parser, save);
31779 add_stmt (finish_omp_structured_block (sb));
31781 return ret;
31784 /* OpenMP 2.5:
31785 #pragma omp for for-clause[optseq] new-line
31786 for-loop
31788 OpenMP 4.0:
31789 #pragma omp for simd for-simd-clause[optseq] new-line
31790 for-loop */
31792 #define OMP_FOR_CLAUSE_MASK \
31793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
31798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
31799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
31800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31802 static tree
31803 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
31804 char *p_name, omp_clause_mask mask, tree *cclauses)
31806 tree clauses, sb, ret;
31807 unsigned int save;
31808 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31810 strcat (p_name, " for");
31811 mask |= OMP_FOR_CLAUSE_MASK;
31812 if (cclauses)
31813 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31815 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31817 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31818 const char *p = IDENTIFIER_POINTER (id);
31820 if (strcmp (p, "simd") == 0)
31822 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31823 if (cclauses == NULL)
31824 cclauses = cclauses_buf;
31826 cp_lexer_consume_token (parser->lexer);
31827 if (!flag_openmp) /* flag_openmp_simd */
31828 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31829 cclauses);
31830 sb = begin_omp_structured_block ();
31831 save = cp_parser_begin_omp_structured_block (parser);
31832 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31833 cclauses);
31834 cp_parser_end_omp_structured_block (parser, save);
31835 tree body = finish_omp_structured_block (sb);
31836 if (ret == NULL)
31837 return ret;
31838 ret = make_node (OMP_FOR);
31839 TREE_TYPE (ret) = void_type_node;
31840 OMP_FOR_BODY (ret) = body;
31841 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31842 SET_EXPR_LOCATION (ret, loc);
31843 add_stmt (ret);
31844 return ret;
31847 if (!flag_openmp) /* flag_openmp_simd */
31849 cp_parser_require_pragma_eol (parser, pragma_tok);
31850 return NULL_TREE;
31853 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31854 cclauses == NULL);
31855 if (cclauses)
31857 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
31858 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31861 sb = begin_omp_structured_block ();
31862 save = cp_parser_begin_omp_structured_block (parser);
31864 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
31866 cp_parser_end_omp_structured_block (parser, save);
31867 add_stmt (finish_omp_structured_block (sb));
31869 return ret;
31872 /* OpenMP 2.5:
31873 # pragma omp master new-line
31874 structured-block */
31876 static tree
31877 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
31879 cp_parser_require_pragma_eol (parser, pragma_tok);
31880 return c_finish_omp_master (input_location,
31881 cp_parser_omp_structured_block (parser));
31884 /* OpenMP 2.5:
31885 # pragma omp ordered new-line
31886 structured-block */
31888 static tree
31889 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
31891 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31892 cp_parser_require_pragma_eol (parser, pragma_tok);
31893 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
31896 /* OpenMP 2.5:
31898 section-scope:
31899 { section-sequence }
31901 section-sequence:
31902 section-directive[opt] structured-block
31903 section-sequence section-directive structured-block */
31905 static tree
31906 cp_parser_omp_sections_scope (cp_parser *parser)
31908 tree stmt, substmt;
31909 bool error_suppress = false;
31910 cp_token *tok;
31912 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
31913 return NULL_TREE;
31915 stmt = push_stmt_list ();
31917 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
31919 substmt = cp_parser_omp_structured_block (parser);
31920 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31921 add_stmt (substmt);
31924 while (1)
31926 tok = cp_lexer_peek_token (parser->lexer);
31927 if (tok->type == CPP_CLOSE_BRACE)
31928 break;
31929 if (tok->type == CPP_EOF)
31930 break;
31932 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
31934 cp_lexer_consume_token (parser->lexer);
31935 cp_parser_require_pragma_eol (parser, tok);
31936 error_suppress = false;
31938 else if (!error_suppress)
31940 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
31941 error_suppress = true;
31944 substmt = cp_parser_omp_structured_block (parser);
31945 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31946 add_stmt (substmt);
31948 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
31950 substmt = pop_stmt_list (stmt);
31952 stmt = make_node (OMP_SECTIONS);
31953 TREE_TYPE (stmt) = void_type_node;
31954 OMP_SECTIONS_BODY (stmt) = substmt;
31956 add_stmt (stmt);
31957 return stmt;
31960 /* OpenMP 2.5:
31961 # pragma omp sections sections-clause[optseq] newline
31962 sections-scope */
31964 #define OMP_SECTIONS_CLAUSE_MASK \
31965 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31971 static tree
31972 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
31973 char *p_name, omp_clause_mask mask, tree *cclauses)
31975 tree clauses, ret;
31976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31978 strcat (p_name, " sections");
31979 mask |= OMP_SECTIONS_CLAUSE_MASK;
31980 if (cclauses)
31981 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31983 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31984 cclauses == NULL);
31985 if (cclauses)
31987 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
31988 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
31991 ret = cp_parser_omp_sections_scope (parser);
31992 if (ret)
31993 OMP_SECTIONS_CLAUSES (ret) = clauses;
31995 return ret;
31998 /* OpenMP 2.5:
31999 # pragma omp parallel parallel-clause[optseq] new-line
32000 structured-block
32001 # pragma omp parallel for parallel-for-clause[optseq] new-line
32002 structured-block
32003 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
32004 structured-block
32006 OpenMP 4.0:
32007 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
32008 structured-block */
32010 #define OMP_PARALLEL_CLAUSE_MASK \
32011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
32015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
32017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
32019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
32021 static tree
32022 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
32023 char *p_name, omp_clause_mask mask, tree *cclauses)
32025 tree stmt, clauses, block;
32026 unsigned int save;
32027 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32029 strcat (p_name, " parallel");
32030 mask |= OMP_PARALLEL_CLAUSE_MASK;
32032 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32034 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32035 if (cclauses == NULL)
32036 cclauses = cclauses_buf;
32038 cp_lexer_consume_token (parser->lexer);
32039 if (!flag_openmp) /* flag_openmp_simd */
32040 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
32041 block = begin_omp_parallel ();
32042 save = cp_parser_begin_omp_structured_block (parser);
32043 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
32044 cp_parser_end_omp_structured_block (parser, save);
32045 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
32046 block);
32047 if (ret == NULL_TREE)
32048 return ret;
32049 OMP_PARALLEL_COMBINED (stmt) = 1;
32050 return stmt;
32052 else if (cclauses)
32054 error_at (loc, "expected %<for%> after %qs", p_name);
32055 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32056 return NULL_TREE;
32058 else if (!flag_openmp) /* flag_openmp_simd */
32060 cp_parser_require_pragma_eol (parser, pragma_tok);
32061 return NULL_TREE;
32063 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32066 const char *p = IDENTIFIER_POINTER (id);
32067 if (strcmp (p, "sections") == 0)
32069 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32070 cclauses = cclauses_buf;
32072 cp_lexer_consume_token (parser->lexer);
32073 block = begin_omp_parallel ();
32074 save = cp_parser_begin_omp_structured_block (parser);
32075 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
32076 cp_parser_end_omp_structured_block (parser, save);
32077 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
32078 block);
32079 OMP_PARALLEL_COMBINED (stmt) = 1;
32080 return stmt;
32084 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
32086 block = begin_omp_parallel ();
32087 save = cp_parser_begin_omp_structured_block (parser);
32088 cp_parser_statement (parser, NULL_TREE, false, NULL);
32089 cp_parser_end_omp_structured_block (parser, save);
32090 stmt = finish_omp_parallel (clauses, block);
32091 return stmt;
32094 /* OpenMP 2.5:
32095 # pragma omp single single-clause[optseq] new-line
32096 structured-block */
32098 #define OMP_SINGLE_CLAUSE_MASK \
32099 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
32102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32104 static tree
32105 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
32107 tree stmt = make_node (OMP_SINGLE);
32108 TREE_TYPE (stmt) = void_type_node;
32110 OMP_SINGLE_CLAUSES (stmt)
32111 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
32112 "#pragma omp single", pragma_tok);
32113 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
32115 return add_stmt (stmt);
32118 /* OpenMP 3.0:
32119 # pragma omp task task-clause[optseq] new-line
32120 structured-block */
32122 #define OMP_TASK_CLAUSE_MASK \
32123 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
32125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
32126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
32130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
32131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
32133 static tree
32134 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
32136 tree clauses, block;
32137 unsigned int save;
32139 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
32140 "#pragma omp task", pragma_tok);
32141 block = begin_omp_task ();
32142 save = cp_parser_begin_omp_structured_block (parser);
32143 cp_parser_statement (parser, NULL_TREE, false, NULL);
32144 cp_parser_end_omp_structured_block (parser, save);
32145 return finish_omp_task (clauses, block);
32148 /* OpenMP 3.0:
32149 # pragma omp taskwait new-line */
32151 static void
32152 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
32154 cp_parser_require_pragma_eol (parser, pragma_tok);
32155 finish_omp_taskwait ();
32158 /* OpenMP 3.1:
32159 # pragma omp taskyield new-line */
32161 static void
32162 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
32164 cp_parser_require_pragma_eol (parser, pragma_tok);
32165 finish_omp_taskyield ();
32168 /* OpenMP 4.0:
32169 # pragma omp taskgroup new-line
32170 structured-block */
32172 static tree
32173 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
32175 cp_parser_require_pragma_eol (parser, pragma_tok);
32176 return c_finish_omp_taskgroup (input_location,
32177 cp_parser_omp_structured_block (parser));
32181 /* OpenMP 2.5:
32182 # pragma omp threadprivate (variable-list) */
32184 static void
32185 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
32187 tree vars;
32189 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32190 cp_parser_require_pragma_eol (parser, pragma_tok);
32192 finish_omp_threadprivate (vars);
32195 /* OpenMP 4.0:
32196 # pragma omp cancel cancel-clause[optseq] new-line */
32198 #define OMP_CANCEL_CLAUSE_MASK \
32199 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
32200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
32201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
32202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
32203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32205 static void
32206 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
32208 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
32209 "#pragma omp cancel", pragma_tok);
32210 finish_omp_cancel (clauses);
32213 /* OpenMP 4.0:
32214 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
32216 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
32217 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
32218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
32219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
32220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
32222 static void
32223 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
32225 tree clauses;
32226 bool point_seen = false;
32228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32230 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32231 const char *p = IDENTIFIER_POINTER (id);
32233 if (strcmp (p, "point") == 0)
32235 cp_lexer_consume_token (parser->lexer);
32236 point_seen = true;
32239 if (!point_seen)
32241 cp_parser_error (parser, "expected %<point%>");
32242 cp_parser_require_pragma_eol (parser, pragma_tok);
32243 return;
32246 clauses = cp_parser_omp_all_clauses (parser,
32247 OMP_CANCELLATION_POINT_CLAUSE_MASK,
32248 "#pragma omp cancellation point",
32249 pragma_tok);
32250 finish_omp_cancellation_point (clauses);
32253 /* OpenMP 4.0:
32254 #pragma omp distribute distribute-clause[optseq] new-line
32255 for-loop */
32257 #define OMP_DISTRIBUTE_CLAUSE_MASK \
32258 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
32261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
32263 static tree
32264 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
32265 char *p_name, omp_clause_mask mask, tree *cclauses)
32267 tree clauses, sb, ret;
32268 unsigned int save;
32269 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32271 strcat (p_name, " distribute");
32272 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
32274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32276 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32277 const char *p = IDENTIFIER_POINTER (id);
32278 bool simd = false;
32279 bool parallel = false;
32281 if (strcmp (p, "simd") == 0)
32282 simd = true;
32283 else
32284 parallel = strcmp (p, "parallel") == 0;
32285 if (parallel || simd)
32287 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32288 if (cclauses == NULL)
32289 cclauses = cclauses_buf;
32290 cp_lexer_consume_token (parser->lexer);
32291 if (!flag_openmp) /* flag_openmp_simd */
32293 if (simd)
32294 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32295 cclauses);
32296 else
32297 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
32298 cclauses);
32300 sb = begin_omp_structured_block ();
32301 save = cp_parser_begin_omp_structured_block (parser);
32302 if (simd)
32303 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32304 cclauses);
32305 else
32306 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
32307 cclauses);
32308 cp_parser_end_omp_structured_block (parser, save);
32309 tree body = finish_omp_structured_block (sb);
32310 if (ret == NULL)
32311 return ret;
32312 ret = make_node (OMP_DISTRIBUTE);
32313 TREE_TYPE (ret) = void_type_node;
32314 OMP_FOR_BODY (ret) = body;
32315 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
32316 SET_EXPR_LOCATION (ret, loc);
32317 add_stmt (ret);
32318 return ret;
32321 if (!flag_openmp) /* flag_openmp_simd */
32323 cp_parser_require_pragma_eol (parser, pragma_tok);
32324 return NULL_TREE;
32327 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32328 cclauses == NULL);
32329 if (cclauses)
32331 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
32332 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
32335 sb = begin_omp_structured_block ();
32336 save = cp_parser_begin_omp_structured_block (parser);
32338 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
32340 cp_parser_end_omp_structured_block (parser, save);
32341 add_stmt (finish_omp_structured_block (sb));
32343 return ret;
32346 /* OpenMP 4.0:
32347 # pragma omp teams teams-clause[optseq] new-line
32348 structured-block */
32350 #define OMP_TEAMS_CLAUSE_MASK \
32351 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
32356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
32357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
32359 static tree
32360 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
32361 char *p_name, omp_clause_mask mask, tree *cclauses)
32363 tree clauses, sb, ret;
32364 unsigned int save;
32365 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32367 strcat (p_name, " teams");
32368 mask |= OMP_TEAMS_CLAUSE_MASK;
32370 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32372 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32373 const char *p = IDENTIFIER_POINTER (id);
32374 if (strcmp (p, "distribute") == 0)
32376 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32377 if (cclauses == NULL)
32378 cclauses = cclauses_buf;
32380 cp_lexer_consume_token (parser->lexer);
32381 if (!flag_openmp) /* flag_openmp_simd */
32382 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
32383 cclauses);
32384 sb = begin_omp_structured_block ();
32385 save = cp_parser_begin_omp_structured_block (parser);
32386 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
32387 cclauses);
32388 cp_parser_end_omp_structured_block (parser, save);
32389 tree body = finish_omp_structured_block (sb);
32390 if (ret == NULL)
32391 return ret;
32392 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
32393 ret = make_node (OMP_TEAMS);
32394 TREE_TYPE (ret) = void_type_node;
32395 OMP_TEAMS_CLAUSES (ret) = clauses;
32396 OMP_TEAMS_BODY (ret) = body;
32397 return add_stmt (ret);
32400 if (!flag_openmp) /* flag_openmp_simd */
32402 cp_parser_require_pragma_eol (parser, pragma_tok);
32403 return NULL_TREE;
32406 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32407 cclauses == NULL);
32408 if (cclauses)
32410 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
32411 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
32414 tree stmt = make_node (OMP_TEAMS);
32415 TREE_TYPE (stmt) = void_type_node;
32416 OMP_TEAMS_CLAUSES (stmt) = clauses;
32417 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
32419 return add_stmt (stmt);
32422 /* OpenMP 4.0:
32423 # pragma omp target data target-data-clause[optseq] new-line
32424 structured-block */
32426 #define OMP_TARGET_DATA_CLAUSE_MASK \
32427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32431 static tree
32432 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
32434 tree stmt = make_node (OMP_TARGET_DATA);
32435 TREE_TYPE (stmt) = void_type_node;
32437 OMP_TARGET_DATA_CLAUSES (stmt)
32438 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
32439 "#pragma omp target data", pragma_tok);
32440 keep_next_level (true);
32441 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
32443 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32444 return add_stmt (stmt);
32447 /* OpenMP 4.0:
32448 # pragma omp target update target-update-clause[optseq] new-line */
32450 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
32451 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
32452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
32453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32456 static bool
32457 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
32458 enum pragma_context context)
32460 if (context == pragma_stmt)
32462 error_at (pragma_tok->location,
32463 "%<#pragma omp target update%> may only be "
32464 "used in compound statements");
32465 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32466 return false;
32469 tree clauses
32470 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
32471 "#pragma omp target update", pragma_tok);
32472 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
32473 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
32475 error_at (pragma_tok->location,
32476 "%<#pragma omp target update must contain at least one "
32477 "%<from%> or %<to%> clauses");
32478 return false;
32481 tree stmt = make_node (OMP_TARGET_UPDATE);
32482 TREE_TYPE (stmt) = void_type_node;
32483 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
32484 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32485 add_stmt (stmt);
32486 return false;
32489 /* OpenMP 4.0:
32490 # pragma omp target target-clause[optseq] new-line
32491 structured-block */
32493 #define OMP_TARGET_CLAUSE_MASK \
32494 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32498 static bool
32499 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
32500 enum pragma_context context)
32502 if (context != pragma_stmt && context != pragma_compound)
32504 cp_parser_error (parser, "expected declaration specifiers");
32505 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32506 return false;
32509 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32511 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32512 const char *p = IDENTIFIER_POINTER (id);
32514 if (strcmp (p, "teams") == 0)
32516 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
32517 char p_name[sizeof ("#pragma omp target teams distribute "
32518 "parallel for simd")];
32520 cp_lexer_consume_token (parser->lexer);
32521 strcpy (p_name, "#pragma omp target");
32522 if (!flag_openmp) /* flag_openmp_simd */
32524 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
32525 OMP_TARGET_CLAUSE_MASK,
32526 cclauses);
32527 return stmt != NULL_TREE;
32529 keep_next_level (true);
32530 tree sb = begin_omp_structured_block ();
32531 unsigned save = cp_parser_begin_omp_structured_block (parser);
32532 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
32533 OMP_TARGET_CLAUSE_MASK, cclauses);
32534 cp_parser_end_omp_structured_block (parser, save);
32535 tree body = finish_omp_structured_block (sb);
32536 if (ret == NULL_TREE)
32537 return false;
32538 tree stmt = make_node (OMP_TARGET);
32539 TREE_TYPE (stmt) = void_type_node;
32540 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
32541 OMP_TARGET_BODY (stmt) = body;
32542 add_stmt (stmt);
32543 return true;
32545 else if (!flag_openmp) /* flag_openmp_simd */
32547 cp_parser_require_pragma_eol (parser, pragma_tok);
32548 return false;
32550 else if (strcmp (p, "data") == 0)
32552 cp_lexer_consume_token (parser->lexer);
32553 cp_parser_omp_target_data (parser, pragma_tok);
32554 return true;
32556 else if (strcmp (p, "update") == 0)
32558 cp_lexer_consume_token (parser->lexer);
32559 return cp_parser_omp_target_update (parser, pragma_tok, context);
32563 tree stmt = make_node (OMP_TARGET);
32564 TREE_TYPE (stmt) = void_type_node;
32566 OMP_TARGET_CLAUSES (stmt)
32567 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
32568 "#pragma omp target", pragma_tok);
32569 keep_next_level (true);
32570 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
32572 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32573 add_stmt (stmt);
32574 return true;
32577 /* OpenACC 2.0:
32578 # pragma acc cache (variable-list) new-line
32581 static tree
32582 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
32584 tree stmt, clauses;
32586 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
32587 clauses = finish_omp_clauses (clauses);
32589 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
32591 stmt = make_node (OACC_CACHE);
32592 TREE_TYPE (stmt) = void_type_node;
32593 OACC_CACHE_CLAUSES (stmt) = clauses;
32594 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32595 add_stmt (stmt);
32597 return stmt;
32600 /* OpenACC 2.0:
32601 # pragma acc data oacc-data-clause[optseq] new-line
32602 structured-block */
32604 #define OACC_DATA_CLAUSE_MASK \
32605 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
32617 static tree
32618 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
32620 tree stmt, clauses, block;
32621 unsigned int save;
32623 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
32624 "#pragma acc data", pragma_tok);
32626 block = begin_omp_parallel ();
32627 save = cp_parser_begin_omp_structured_block (parser);
32628 cp_parser_statement (parser, NULL_TREE, false, NULL);
32629 cp_parser_end_omp_structured_block (parser, save);
32630 stmt = finish_oacc_data (clauses, block);
32631 return stmt;
32634 /* OpenACC 2.0:
32635 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
32639 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
32641 LOC is the location of the #pragma token.
32644 #define OACC_ENTER_DATA_CLAUSE_MASK \
32645 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32653 #define OACC_EXIT_DATA_CLAUSE_MASK \
32654 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
32658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32660 static tree
32661 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
32662 bool enter)
32664 tree stmt, clauses;
32666 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
32667 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32669 cp_parser_error (parser, enter
32670 ? "expected %<data%> in %<#pragma acc enter data%>"
32671 : "expected %<data%> in %<#pragma acc exit data%>");
32672 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32673 return NULL_TREE;
32676 const char *p =
32677 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
32678 if (strcmp (p, "data") != 0)
32680 cp_parser_error (parser, "invalid pragma");
32681 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32682 return NULL_TREE;
32685 cp_lexer_consume_token (parser->lexer);
32687 if (enter)
32688 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
32689 "#pragma acc enter data", pragma_tok);
32690 else
32691 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
32692 "#pragma acc exit data", pragma_tok);
32694 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32696 error_at (pragma_tok->location,
32697 "%<#pragma acc enter data%> has no data movement clause");
32698 return NULL_TREE;
32701 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
32702 TREE_TYPE (stmt) = void_type_node;
32703 if (enter)
32704 OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
32705 else
32706 OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
32707 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32708 add_stmt (stmt);
32709 return stmt;
32712 /* OpenACC 2.0:
32713 # pragma acc kernels oacc-kernels-clause[optseq] new-line
32714 structured-block */
32716 #define OACC_KERNELS_CLAUSE_MASK \
32717 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32731 static tree
32732 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
32734 tree stmt, clauses, block;
32735 unsigned int save;
32737 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
32738 "#pragma acc kernels", pragma_tok);
32740 block = begin_omp_parallel ();
32741 save = cp_parser_begin_omp_structured_block (parser);
32742 cp_parser_statement (parser, NULL_TREE, false, NULL);
32743 cp_parser_end_omp_structured_block (parser, save);
32744 stmt = finish_oacc_kernels (clauses, block);
32745 return stmt;
32748 /* OpenACC 2.0:
32749 # pragma acc loop oacc-loop-clause[optseq] new-line
32750 structured-block */
32752 #define OACC_LOOP_CLAUSE_MASK \
32753 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
32754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
32756 static tree
32757 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
32759 tree stmt, clauses, block;
32760 int save;
32762 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
32763 "#pragma acc loop", pragma_tok);
32765 block = begin_omp_structured_block ();
32766 save = cp_parser_begin_omp_structured_block (parser);
32767 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
32768 cp_parser_end_omp_structured_block (parser, save);
32769 add_stmt (finish_omp_structured_block (block));
32770 return stmt;
32773 /* OpenACC 2.0:
32774 # pragma acc parallel oacc-parallel-clause[optseq] new-line
32775 structured-block */
32777 #define OACC_PARALLEL_CLAUSE_MASK \
32778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
32786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
32787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
32793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
32794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32796 static tree
32797 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
32799 tree stmt, clauses, block;
32800 unsigned int save;
32802 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
32803 "#pragma acc parallel", pragma_tok);
32805 block = begin_omp_parallel ();
32806 save = cp_parser_begin_omp_structured_block (parser);
32807 cp_parser_statement (parser, NULL_TREE, false, NULL);
32808 cp_parser_end_omp_structured_block (parser, save);
32809 stmt = finish_oacc_parallel (clauses, block);
32810 return stmt;
32813 /* OpenACC 2.0:
32814 # pragma acc update oacc-update-clause[optseq] new-line
32817 #define OACC_UPDATE_CLAUSE_MASK \
32818 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
32820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
32821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
32823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32825 static tree
32826 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
32828 tree stmt, clauses;
32830 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
32831 "#pragma acc update", pragma_tok);
32833 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32835 error_at (pragma_tok->location,
32836 "%<#pragma acc update%> must contain at least one "
32837 "%<device%> or %<host/self%> clause");
32838 return NULL_TREE;
32841 stmt = make_node (OACC_UPDATE);
32842 TREE_TYPE (stmt) = void_type_node;
32843 OACC_UPDATE_CLAUSES (stmt) = clauses;
32844 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32845 add_stmt (stmt);
32846 return stmt;
32849 /* OpenACC 2.0:
32850 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
32852 LOC is the location of the #pragma token.
32855 #define OACC_WAIT_CLAUSE_MASK \
32856 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
32858 static tree
32859 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
32861 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
32862 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32864 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32865 list = cp_parser_oacc_wait_list (parser, loc, list);
32867 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
32868 "#pragma acc wait", pragma_tok);
32870 stmt = c_finish_oacc_wait (loc, list, clauses);
32872 return stmt;
32875 /* OpenMP 4.0:
32876 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
32878 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
32879 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
32880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
32881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
32882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
32883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
32884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
32886 static void
32887 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
32888 enum pragma_context context)
32890 bool first_p = parser->omp_declare_simd == NULL;
32891 cp_omp_declare_simd_data data;
32892 if (first_p)
32894 data.error_seen = false;
32895 data.fndecl_seen = false;
32896 data.tokens = vNULL;
32897 parser->omp_declare_simd = &data;
32899 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32900 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32901 cp_lexer_consume_token (parser->lexer);
32902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32903 parser->omp_declare_simd->error_seen = true;
32904 cp_parser_require_pragma_eol (parser, pragma_tok);
32905 struct cp_token_cache *cp
32906 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
32907 parser->omp_declare_simd->tokens.safe_push (cp);
32908 if (first_p)
32910 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
32911 cp_parser_pragma (parser, context);
32912 switch (context)
32914 case pragma_external:
32915 cp_parser_declaration (parser);
32916 break;
32917 case pragma_member:
32918 cp_parser_member_declaration (parser);
32919 break;
32920 case pragma_objc_icode:
32921 cp_parser_block_declaration (parser, /*statement_p=*/false);
32922 break;
32923 default:
32924 cp_parser_declaration_statement (parser);
32925 break;
32927 if (parser->omp_declare_simd
32928 && !parser->omp_declare_simd->error_seen
32929 && !parser->omp_declare_simd->fndecl_seen)
32930 error_at (pragma_tok->location,
32931 "%<#pragma omp declare simd%> not immediately followed by "
32932 "function declaration or definition");
32933 data.tokens.release ();
32934 parser->omp_declare_simd = NULL;
32938 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
32939 This function is modelled similar to the late parsing of omp declare
32940 simd. */
32942 static tree
32943 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
32945 struct cp_token_cache *ce;
32946 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
32947 int ii = 0;
32949 if (parser->omp_declare_simd != NULL)
32951 error ("%<#pragma omp declare simd%> cannot be used in the same function"
32952 " marked as a Cilk Plus SIMD-enabled function");
32953 XDELETE (parser->cilk_simd_fn_info);
32954 parser->cilk_simd_fn_info = NULL;
32955 return attrs;
32957 if (!info->error_seen && info->fndecl_seen)
32959 error ("vector attribute not immediately followed by a single function"
32960 " declaration or definition");
32961 info->error_seen = true;
32963 if (info->error_seen)
32964 return attrs;
32966 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
32968 tree c, cl;
32970 cp_parser_push_lexer_for_tokens (parser, ce);
32971 parser->lexer->in_pragma = true;
32972 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
32973 "SIMD-enabled functions attribute",
32974 NULL);
32975 cp_parser_pop_lexer (parser);
32976 if (cl)
32977 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32979 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
32980 TREE_CHAIN (c) = attrs;
32981 attrs = c;
32983 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32984 TREE_CHAIN (c) = attrs;
32985 if (processing_template_decl)
32986 ATTR_IS_DEPENDENT (c) = 1;
32987 attrs = c;
32989 info->fndecl_seen = true;
32990 XDELETE (parser->cilk_simd_fn_info);
32991 parser->cilk_simd_fn_info = NULL;
32992 return attrs;
32995 /* Finalize #pragma omp declare simd clauses after direct declarator has
32996 been parsed, and put that into "omp declare simd" attribute. */
32998 static tree
32999 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
33001 struct cp_token_cache *ce;
33002 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
33003 int i;
33005 if (!data->error_seen && data->fndecl_seen)
33007 error ("%<#pragma omp declare simd%> not immediately followed by "
33008 "a single function declaration or definition");
33009 data->error_seen = true;
33010 return attrs;
33012 if (data->error_seen)
33013 return attrs;
33015 FOR_EACH_VEC_ELT (data->tokens, i, ce)
33017 tree c, cl;
33019 cp_parser_push_lexer_for_tokens (parser, ce);
33020 parser->lexer->in_pragma = true;
33021 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
33022 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
33023 cp_lexer_consume_token (parser->lexer);
33024 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
33025 "#pragma omp declare simd", pragma_tok);
33026 cp_parser_pop_lexer (parser);
33027 if (cl)
33028 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
33029 c = build_tree_list (get_identifier ("omp declare simd"), cl);
33030 TREE_CHAIN (c) = attrs;
33031 if (processing_template_decl)
33032 ATTR_IS_DEPENDENT (c) = 1;
33033 attrs = c;
33036 data->fndecl_seen = true;
33037 return attrs;
33041 /* OpenMP 4.0:
33042 # pragma omp declare target new-line
33043 declarations and definitions
33044 # pragma omp end declare target new-line */
33046 static void
33047 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
33049 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33050 scope_chain->omp_declare_target_attribute++;
33053 static void
33054 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
33056 const char *p = "";
33057 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33059 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33060 p = IDENTIFIER_POINTER (id);
33062 if (strcmp (p, "declare") == 0)
33064 cp_lexer_consume_token (parser->lexer);
33065 p = "";
33066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33068 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33069 p = IDENTIFIER_POINTER (id);
33071 if (strcmp (p, "target") == 0)
33072 cp_lexer_consume_token (parser->lexer);
33073 else
33075 cp_parser_error (parser, "expected %<target%>");
33076 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33077 return;
33080 else
33082 cp_parser_error (parser, "expected %<declare%>");
33083 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33084 return;
33086 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33087 if (!scope_chain->omp_declare_target_attribute)
33088 error_at (pragma_tok->location,
33089 "%<#pragma omp end declare target%> without corresponding "
33090 "%<#pragma omp declare target%>");
33091 else
33092 scope_chain->omp_declare_target_attribute--;
33095 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
33096 expression and optional initializer clause of
33097 #pragma omp declare reduction. We store the expression(s) as
33098 either 3, 6 or 7 special statements inside of the artificial function's
33099 body. The first two statements are DECL_EXPRs for the artificial
33100 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
33101 expression that uses those variables.
33102 If there was any INITIALIZER clause, this is followed by further statements,
33103 the fourth and fifth statements are DECL_EXPRs for the artificial
33104 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
33105 constructor variant (first token after open paren is not omp_priv),
33106 then the sixth statement is a statement with the function call expression
33107 that uses the OMP_PRIV and optionally OMP_ORIG variable.
33108 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
33109 to initialize the OMP_PRIV artificial variable and there is seventh
33110 statement, a DECL_EXPR of the OMP_PRIV statement again. */
33112 static bool
33113 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
33115 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
33116 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
33117 type = TREE_TYPE (type);
33118 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
33119 DECL_ARTIFICIAL (omp_out) = 1;
33120 pushdecl (omp_out);
33121 add_decl_expr (omp_out);
33122 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
33123 DECL_ARTIFICIAL (omp_in) = 1;
33124 pushdecl (omp_in);
33125 add_decl_expr (omp_in);
33126 tree combiner;
33127 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
33129 keep_next_level (true);
33130 tree block = begin_omp_structured_block ();
33131 combiner = cp_parser_expression (parser);
33132 finish_expr_stmt (combiner);
33133 block = finish_omp_structured_block (block);
33134 add_stmt (block);
33136 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33137 return false;
33139 const char *p = "";
33140 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33142 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33143 p = IDENTIFIER_POINTER (id);
33146 if (strcmp (p, "initializer") == 0)
33148 cp_lexer_consume_token (parser->lexer);
33149 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33150 return false;
33152 p = "";
33153 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33155 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33156 p = IDENTIFIER_POINTER (id);
33159 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
33160 DECL_ARTIFICIAL (omp_priv) = 1;
33161 pushdecl (omp_priv);
33162 add_decl_expr (omp_priv);
33163 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
33164 DECL_ARTIFICIAL (omp_orig) = 1;
33165 pushdecl (omp_orig);
33166 add_decl_expr (omp_orig);
33168 keep_next_level (true);
33169 block = begin_omp_structured_block ();
33171 bool ctor = false;
33172 if (strcmp (p, "omp_priv") == 0)
33174 bool is_direct_init, is_non_constant_init;
33175 ctor = true;
33176 cp_lexer_consume_token (parser->lexer);
33177 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
33178 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
33179 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33180 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
33181 == CPP_CLOSE_PAREN
33182 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
33183 == CPP_CLOSE_PAREN))
33185 finish_omp_structured_block (block);
33186 error ("invalid initializer clause");
33187 return false;
33189 initializer = cp_parser_initializer (parser, &is_direct_init,
33190 &is_non_constant_init);
33191 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
33192 NULL_TREE, LOOKUP_ONLYCONVERTING);
33194 else
33196 cp_parser_parse_tentatively (parser);
33197 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
33198 /*check_dependency_p=*/true,
33199 /*template_p=*/NULL,
33200 /*declarator_p=*/false,
33201 /*optional_p=*/false);
33202 vec<tree, va_gc> *args;
33203 if (fn_name == error_mark_node
33204 || cp_parser_error_occurred (parser)
33205 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33206 || ((args = cp_parser_parenthesized_expression_list
33207 (parser, non_attr, /*cast_p=*/false,
33208 /*allow_expansion_p=*/true,
33209 /*non_constant_p=*/NULL)),
33210 cp_parser_error_occurred (parser)))
33212 finish_omp_structured_block (block);
33213 cp_parser_abort_tentative_parse (parser);
33214 cp_parser_error (parser, "expected id-expression (arguments)");
33215 return false;
33217 unsigned int i;
33218 tree arg;
33219 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
33220 if (arg == omp_priv
33221 || (TREE_CODE (arg) == ADDR_EXPR
33222 && TREE_OPERAND (arg, 0) == omp_priv))
33223 break;
33224 cp_parser_abort_tentative_parse (parser);
33225 if (arg == NULL_TREE)
33226 error ("one of the initializer call arguments should be %<omp_priv%>"
33227 " or %<&omp_priv%>");
33228 initializer = cp_parser_postfix_expression (parser, false, false, false,
33229 false, NULL);
33230 finish_expr_stmt (initializer);
33233 block = finish_omp_structured_block (block);
33234 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
33235 add_stmt (block);
33237 if (ctor)
33238 add_decl_expr (omp_orig);
33240 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33241 return false;
33244 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
33245 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
33247 return true;
33250 /* OpenMP 4.0
33251 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33252 initializer-clause[opt] new-line
33254 initializer-clause:
33255 initializer (omp_priv initializer)
33256 initializer (function-name (argument-list)) */
33258 static void
33259 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
33260 enum pragma_context)
33262 auto_vec<tree> types;
33263 enum tree_code reduc_code = ERROR_MARK;
33264 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
33265 unsigned int i;
33266 cp_token *first_token;
33267 cp_token_cache *cp;
33268 int errs;
33269 void *p;
33271 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
33272 p = obstack_alloc (&declarator_obstack, 0);
33274 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33275 goto fail;
33277 switch (cp_lexer_peek_token (parser->lexer)->type)
33279 case CPP_PLUS:
33280 reduc_code = PLUS_EXPR;
33281 break;
33282 case CPP_MULT:
33283 reduc_code = MULT_EXPR;
33284 break;
33285 case CPP_MINUS:
33286 reduc_code = MINUS_EXPR;
33287 break;
33288 case CPP_AND:
33289 reduc_code = BIT_AND_EXPR;
33290 break;
33291 case CPP_XOR:
33292 reduc_code = BIT_XOR_EXPR;
33293 break;
33294 case CPP_OR:
33295 reduc_code = BIT_IOR_EXPR;
33296 break;
33297 case CPP_AND_AND:
33298 reduc_code = TRUTH_ANDIF_EXPR;
33299 break;
33300 case CPP_OR_OR:
33301 reduc_code = TRUTH_ORIF_EXPR;
33302 break;
33303 case CPP_NAME:
33304 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
33305 break;
33306 default:
33307 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
33308 "%<|%>, %<&&%>, %<||%> or identifier");
33309 goto fail;
33312 if (reduc_code != ERROR_MARK)
33313 cp_lexer_consume_token (parser->lexer);
33315 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
33316 if (reduc_id == error_mark_node)
33317 goto fail;
33319 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33320 goto fail;
33322 /* Types may not be defined in declare reduction type list. */
33323 const char *saved_message;
33324 saved_message = parser->type_definition_forbidden_message;
33325 parser->type_definition_forbidden_message
33326 = G_("types may not be defined in declare reduction type list");
33327 bool saved_colon_corrects_to_scope_p;
33328 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33329 parser->colon_corrects_to_scope_p = false;
33330 bool saved_colon_doesnt_start_class_def_p;
33331 saved_colon_doesnt_start_class_def_p
33332 = parser->colon_doesnt_start_class_def_p;
33333 parser->colon_doesnt_start_class_def_p = true;
33335 while (true)
33337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33338 type = cp_parser_type_id (parser);
33339 if (type == error_mark_node)
33341 else if (ARITHMETIC_TYPE_P (type)
33342 && (orig_reduc_id == NULL_TREE
33343 || (TREE_CODE (type) != COMPLEX_TYPE
33344 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33345 "min") == 0
33346 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33347 "max") == 0))))
33348 error_at (loc, "predeclared arithmetic type %qT in "
33349 "%<#pragma omp declare reduction%>", type);
33350 else if (TREE_CODE (type) == FUNCTION_TYPE
33351 || TREE_CODE (type) == METHOD_TYPE
33352 || TREE_CODE (type) == ARRAY_TYPE)
33353 error_at (loc, "function or array type %qT in "
33354 "%<#pragma omp declare reduction%>", type);
33355 else if (TREE_CODE (type) == REFERENCE_TYPE)
33356 error_at (loc, "reference type %qT in "
33357 "%<#pragma omp declare reduction%>", type);
33358 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
33359 error_at (loc, "const, volatile or __restrict qualified type %qT in "
33360 "%<#pragma omp declare reduction%>", type);
33361 else
33362 types.safe_push (type);
33364 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33365 cp_lexer_consume_token (parser->lexer);
33366 else
33367 break;
33370 /* Restore the saved message. */
33371 parser->type_definition_forbidden_message = saved_message;
33372 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33373 parser->colon_doesnt_start_class_def_p
33374 = saved_colon_doesnt_start_class_def_p;
33376 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
33377 || types.is_empty ())
33379 fail:
33380 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33381 goto done;
33384 first_token = cp_lexer_peek_token (parser->lexer);
33385 cp = NULL;
33386 errs = errorcount;
33387 FOR_EACH_VEC_ELT (types, i, type)
33389 tree fntype
33390 = build_function_type_list (void_type_node,
33391 cp_build_reference_type (type, false),
33392 NULL_TREE);
33393 tree this_reduc_id = reduc_id;
33394 if (!dependent_type_p (type))
33395 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
33396 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
33397 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
33398 DECL_ARTIFICIAL (fndecl) = 1;
33399 DECL_EXTERNAL (fndecl) = 1;
33400 DECL_DECLARED_INLINE_P (fndecl) = 1;
33401 DECL_IGNORED_P (fndecl) = 1;
33402 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
33403 DECL_ATTRIBUTES (fndecl)
33404 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
33405 DECL_ATTRIBUTES (fndecl));
33406 if (processing_template_decl)
33407 fndecl = push_template_decl (fndecl);
33408 bool block_scope = false;
33409 tree block = NULL_TREE;
33410 if (current_function_decl)
33412 block_scope = true;
33413 DECL_CONTEXT (fndecl) = global_namespace;
33414 if (!processing_template_decl)
33415 pushdecl (fndecl);
33417 else if (current_class_type)
33419 if (cp == NULL)
33421 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33422 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
33423 cp_lexer_consume_token (parser->lexer);
33424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33425 goto fail;
33426 cp = cp_token_cache_new (first_token,
33427 cp_lexer_peek_nth_token (parser->lexer,
33428 2));
33430 DECL_STATIC_FUNCTION_P (fndecl) = 1;
33431 finish_member_declaration (fndecl);
33432 DECL_PENDING_INLINE_INFO (fndecl) = cp;
33433 DECL_PENDING_INLINE_P (fndecl) = 1;
33434 vec_safe_push (unparsed_funs_with_definitions, fndecl);
33435 continue;
33437 else
33439 DECL_CONTEXT (fndecl) = current_namespace;
33440 pushdecl (fndecl);
33442 if (!block_scope)
33443 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
33444 else
33445 block = begin_omp_structured_block ();
33446 if (cp)
33448 cp_parser_push_lexer_for_tokens (parser, cp);
33449 parser->lexer->in_pragma = true;
33451 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
33453 if (!block_scope)
33454 finish_function (0);
33455 else
33456 DECL_CONTEXT (fndecl) = current_function_decl;
33457 if (cp)
33458 cp_parser_pop_lexer (parser);
33459 goto fail;
33461 if (cp)
33462 cp_parser_pop_lexer (parser);
33463 if (!block_scope)
33464 finish_function (0);
33465 else
33467 DECL_CONTEXT (fndecl) = current_function_decl;
33468 block = finish_omp_structured_block (block);
33469 if (TREE_CODE (block) == BIND_EXPR)
33470 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
33471 else if (TREE_CODE (block) == STATEMENT_LIST)
33472 DECL_SAVED_TREE (fndecl) = block;
33473 if (processing_template_decl)
33474 add_decl_expr (fndecl);
33476 cp_check_omp_declare_reduction (fndecl);
33477 if (cp == NULL && types.length () > 1)
33478 cp = cp_token_cache_new (first_token,
33479 cp_lexer_peek_nth_token (parser->lexer, 2));
33480 if (errs != errorcount)
33481 break;
33484 cp_parser_require_pragma_eol (parser, pragma_tok);
33486 done:
33487 /* Free any declarators allocated. */
33488 obstack_free (&declarator_obstack, p);
33491 /* OpenMP 4.0
33492 #pragma omp declare simd declare-simd-clauses[optseq] new-line
33493 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33494 initializer-clause[opt] new-line
33495 #pragma omp declare target new-line */
33497 static void
33498 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
33499 enum pragma_context context)
33501 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33503 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33504 const char *p = IDENTIFIER_POINTER (id);
33506 if (strcmp (p, "simd") == 0)
33508 cp_lexer_consume_token (parser->lexer);
33509 cp_parser_omp_declare_simd (parser, pragma_tok,
33510 context);
33511 return;
33513 cp_ensure_no_omp_declare_simd (parser);
33514 if (strcmp (p, "reduction") == 0)
33516 cp_lexer_consume_token (parser->lexer);
33517 cp_parser_omp_declare_reduction (parser, pragma_tok,
33518 context);
33519 return;
33521 if (!flag_openmp) /* flag_openmp_simd */
33523 cp_parser_require_pragma_eol (parser, pragma_tok);
33524 return;
33526 if (strcmp (p, "target") == 0)
33528 cp_lexer_consume_token (parser->lexer);
33529 cp_parser_omp_declare_target (parser, pragma_tok);
33530 return;
33533 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
33534 "or %<target%>");
33535 cp_parser_require_pragma_eol (parser, pragma_tok);
33538 /* Main entry point to OpenMP statement pragmas. */
33540 static void
33541 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
33543 tree stmt;
33544 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
33545 omp_clause_mask mask (0);
33547 switch (pragma_tok->pragma_kind)
33549 case PRAGMA_OACC_CACHE:
33550 stmt = cp_parser_oacc_cache (parser, pragma_tok);
33551 break;
33552 case PRAGMA_OACC_DATA:
33553 stmt = cp_parser_oacc_data (parser, pragma_tok);
33554 break;
33555 case PRAGMA_OACC_ENTER_DATA:
33556 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
33557 break;
33558 case PRAGMA_OACC_EXIT_DATA:
33559 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
33560 break;
33561 case PRAGMA_OACC_KERNELS:
33562 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
33563 break;
33564 case PRAGMA_OACC_LOOP:
33565 stmt = cp_parser_oacc_loop (parser, pragma_tok);
33566 break;
33567 case PRAGMA_OACC_PARALLEL:
33568 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
33569 break;
33570 case PRAGMA_OACC_UPDATE:
33571 stmt = cp_parser_oacc_update (parser, pragma_tok);
33572 break;
33573 case PRAGMA_OACC_WAIT:
33574 stmt = cp_parser_oacc_wait (parser, pragma_tok);
33575 break;
33576 case PRAGMA_OMP_ATOMIC:
33577 cp_parser_omp_atomic (parser, pragma_tok);
33578 return;
33579 case PRAGMA_OMP_CRITICAL:
33580 stmt = cp_parser_omp_critical (parser, pragma_tok);
33581 break;
33582 case PRAGMA_OMP_DISTRIBUTE:
33583 strcpy (p_name, "#pragma omp");
33584 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
33585 break;
33586 case PRAGMA_OMP_FOR:
33587 strcpy (p_name, "#pragma omp");
33588 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
33589 break;
33590 case PRAGMA_OMP_MASTER:
33591 stmt = cp_parser_omp_master (parser, pragma_tok);
33592 break;
33593 case PRAGMA_OMP_ORDERED:
33594 stmt = cp_parser_omp_ordered (parser, pragma_tok);
33595 break;
33596 case PRAGMA_OMP_PARALLEL:
33597 strcpy (p_name, "#pragma omp");
33598 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
33599 break;
33600 case PRAGMA_OMP_SECTIONS:
33601 strcpy (p_name, "#pragma omp");
33602 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
33603 break;
33604 case PRAGMA_OMP_SIMD:
33605 strcpy (p_name, "#pragma omp");
33606 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
33607 break;
33608 case PRAGMA_OMP_SINGLE:
33609 stmt = cp_parser_omp_single (parser, pragma_tok);
33610 break;
33611 case PRAGMA_OMP_TASK:
33612 stmt = cp_parser_omp_task (parser, pragma_tok);
33613 break;
33614 case PRAGMA_OMP_TASKGROUP:
33615 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
33616 break;
33617 case PRAGMA_OMP_TEAMS:
33618 strcpy (p_name, "#pragma omp");
33619 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
33620 break;
33621 default:
33622 gcc_unreachable ();
33625 if (stmt)
33626 SET_EXPR_LOCATION (stmt, pragma_tok->location);
33629 /* Transactional Memory parsing routines. */
33631 /* Parse a transaction attribute.
33633 txn-attribute:
33634 attribute
33635 [ [ identifier ] ]
33637 ??? Simplify this when C++0x bracket attributes are
33638 implemented properly. */
33640 static tree
33641 cp_parser_txn_attribute_opt (cp_parser *parser)
33643 cp_token *token;
33644 tree attr_name, attr = NULL;
33646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
33647 return cp_parser_attributes_opt (parser);
33649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
33650 return NULL_TREE;
33651 cp_lexer_consume_token (parser->lexer);
33652 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
33653 goto error1;
33655 token = cp_lexer_peek_token (parser->lexer);
33656 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
33658 token = cp_lexer_consume_token (parser->lexer);
33660 attr_name = (token->type == CPP_KEYWORD
33661 /* For keywords, use the canonical spelling,
33662 not the parsed identifier. */
33663 ? ridpointers[(int) token->keyword]
33664 : token->u.value);
33665 attr = build_tree_list (attr_name, NULL_TREE);
33667 else
33668 cp_parser_error (parser, "expected identifier");
33670 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33671 error1:
33672 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33673 return attr;
33676 /* Parse a __transaction_atomic or __transaction_relaxed statement.
33678 transaction-statement:
33679 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
33680 compound-statement
33681 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
33684 static tree
33685 cp_parser_transaction (cp_parser *parser, enum rid keyword)
33687 unsigned char old_in = parser->in_transaction;
33688 unsigned char this_in = 1, new_in;
33689 cp_token *token;
33690 tree stmt, attrs, noex;
33692 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33693 || keyword == RID_TRANSACTION_RELAXED);
33694 token = cp_parser_require_keyword (parser, keyword,
33695 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33696 : RT_TRANSACTION_RELAXED));
33697 gcc_assert (token != NULL);
33699 if (keyword == RID_TRANSACTION_RELAXED)
33700 this_in |= TM_STMT_ATTR_RELAXED;
33701 else
33703 attrs = cp_parser_txn_attribute_opt (parser);
33704 if (attrs)
33705 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
33708 /* Parse a noexcept specification. */
33709 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
33711 /* Keep track if we're in the lexical scope of an outer transaction. */
33712 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
33714 stmt = begin_transaction_stmt (token->location, NULL, this_in);
33716 parser->in_transaction = new_in;
33717 cp_parser_compound_statement (parser, NULL, false, false);
33718 parser->in_transaction = old_in;
33720 finish_transaction_stmt (stmt, NULL, this_in, noex);
33722 return stmt;
33725 /* Parse a __transaction_atomic or __transaction_relaxed expression.
33727 transaction-expression:
33728 __transaction_atomic txn-noexcept-spec[opt] ( expression )
33729 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
33732 static tree
33733 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
33735 unsigned char old_in = parser->in_transaction;
33736 unsigned char this_in = 1;
33737 cp_token *token;
33738 tree expr, noex;
33739 bool noex_expr;
33741 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33742 || keyword == RID_TRANSACTION_RELAXED);
33744 if (!flag_tm)
33745 error (keyword == RID_TRANSACTION_RELAXED
33746 ? G_("%<__transaction_relaxed%> without transactional memory "
33747 "support enabled")
33748 : G_("%<__transaction_atomic%> without transactional memory "
33749 "support enabled"));
33751 token = cp_parser_require_keyword (parser, keyword,
33752 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33753 : RT_TRANSACTION_RELAXED));
33754 gcc_assert (token != NULL);
33756 if (keyword == RID_TRANSACTION_RELAXED)
33757 this_in |= TM_STMT_ATTR_RELAXED;
33759 /* Set this early. This might mean that we allow transaction_cancel in
33760 an expression that we find out later actually has to be a constexpr.
33761 However, we expect that cxx_constant_value will be able to deal with
33762 this; also, if the noexcept has no constexpr, then what we parse next
33763 really is a transaction's body. */
33764 parser->in_transaction = this_in;
33766 /* Parse a noexcept specification. */
33767 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
33768 true);
33770 if (!noex || !noex_expr
33771 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33773 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
33775 expr = cp_parser_expression (parser);
33776 expr = finish_parenthesized_expr (expr);
33778 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
33780 else
33782 /* The only expression that is available got parsed for the noexcept
33783 already. noexcept is true then. */
33784 expr = noex;
33785 noex = boolean_true_node;
33788 expr = build_transaction_expr (token->location, expr, this_in, noex);
33789 parser->in_transaction = old_in;
33791 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
33792 return error_mark_node;
33794 return (flag_tm ? expr : error_mark_node);
33797 /* Parse a function-transaction-block.
33799 function-transaction-block:
33800 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
33801 function-body
33802 __transaction_atomic txn-attribute[opt] function-try-block
33803 __transaction_relaxed ctor-initializer[opt] function-body
33804 __transaction_relaxed function-try-block
33807 static bool
33808 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
33810 unsigned char old_in = parser->in_transaction;
33811 unsigned char new_in = 1;
33812 tree compound_stmt, stmt, attrs;
33813 bool ctor_initializer_p;
33814 cp_token *token;
33816 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33817 || keyword == RID_TRANSACTION_RELAXED);
33818 token = cp_parser_require_keyword (parser, keyword,
33819 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33820 : RT_TRANSACTION_RELAXED));
33821 gcc_assert (token != NULL);
33823 if (keyword == RID_TRANSACTION_RELAXED)
33824 new_in |= TM_STMT_ATTR_RELAXED;
33825 else
33827 attrs = cp_parser_txn_attribute_opt (parser);
33828 if (attrs)
33829 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
33832 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
33834 parser->in_transaction = new_in;
33836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33837 ctor_initializer_p = cp_parser_function_try_block (parser);
33838 else
33839 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
33840 (parser, /*in_function_try_block=*/false);
33842 parser->in_transaction = old_in;
33844 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
33846 return ctor_initializer_p;
33849 /* Parse a __transaction_cancel statement.
33851 cancel-statement:
33852 __transaction_cancel txn-attribute[opt] ;
33853 __transaction_cancel txn-attribute[opt] throw-expression ;
33855 ??? Cancel and throw is not yet implemented. */
33857 static tree
33858 cp_parser_transaction_cancel (cp_parser *parser)
33860 cp_token *token;
33861 bool is_outer = false;
33862 tree stmt, attrs;
33864 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
33865 RT_TRANSACTION_CANCEL);
33866 gcc_assert (token != NULL);
33868 attrs = cp_parser_txn_attribute_opt (parser);
33869 if (attrs)
33870 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
33872 /* ??? Parse cancel-and-throw here. */
33874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33876 if (!flag_tm)
33878 error_at (token->location, "%<__transaction_cancel%> without "
33879 "transactional memory support enabled");
33880 return error_mark_node;
33882 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
33884 error_at (token->location, "%<__transaction_cancel%> within a "
33885 "%<__transaction_relaxed%>");
33886 return error_mark_node;
33888 else if (is_outer)
33890 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
33891 && !is_tm_may_cancel_outer (current_function_decl))
33893 error_at (token->location, "outer %<__transaction_cancel%> not "
33894 "within outer %<__transaction_atomic%>");
33895 error_at (token->location,
33896 " or a %<transaction_may_cancel_outer%> function");
33897 return error_mark_node;
33900 else if (parser->in_transaction == 0)
33902 error_at (token->location, "%<__transaction_cancel%> not within "
33903 "%<__transaction_atomic%>");
33904 return error_mark_node;
33907 stmt = build_tm_abort_call (token->location, is_outer);
33908 add_stmt (stmt);
33910 return stmt;
33913 /* The parser. */
33915 static GTY (()) cp_parser *the_parser;
33918 /* Special handling for the first token or line in the file. The first
33919 thing in the file might be #pragma GCC pch_preprocess, which loads a
33920 PCH file, which is a GC collection point. So we need to handle this
33921 first pragma without benefit of an existing lexer structure.
33923 Always returns one token to the caller in *FIRST_TOKEN. This is
33924 either the true first token of the file, or the first token after
33925 the initial pragma. */
33927 static void
33928 cp_parser_initial_pragma (cp_token *first_token)
33930 tree name = NULL;
33932 cp_lexer_get_preprocessor_token (NULL, first_token);
33933 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
33934 return;
33936 cp_lexer_get_preprocessor_token (NULL, first_token);
33937 if (first_token->type == CPP_STRING)
33939 name = first_token->u.value;
33941 cp_lexer_get_preprocessor_token (NULL, first_token);
33942 if (first_token->type != CPP_PRAGMA_EOL)
33943 error_at (first_token->location,
33944 "junk at end of %<#pragma GCC pch_preprocess%>");
33946 else
33947 error_at (first_token->location, "expected string literal");
33949 /* Skip to the end of the pragma. */
33950 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
33951 cp_lexer_get_preprocessor_token (NULL, first_token);
33953 /* Now actually load the PCH file. */
33954 if (name)
33955 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
33957 /* Read one more token to return to our caller. We have to do this
33958 after reading the PCH file in, since its pointers have to be
33959 live. */
33960 cp_lexer_get_preprocessor_token (NULL, first_token);
33963 /* Parses the grainsize pragma for the _Cilk_for statement.
33964 Syntax:
33965 #pragma cilk grainsize = <VALUE>. */
33967 static void
33968 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
33970 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
33972 tree exp = cp_parser_binary_expression (parser, false, false,
33973 PREC_NOT_OPERATOR, NULL);
33974 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33975 if (!exp || exp == error_mark_node)
33977 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
33978 return;
33981 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
33982 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33983 cp_parser_cilk_for (parser, exp);
33984 else
33985 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
33986 "%<#pragma cilk grainsize%> is not followed by "
33987 "%<_Cilk_for%>");
33988 return;
33990 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33993 /* Normal parsing of a pragma token. Here we can (and must) use the
33994 regular lexer. */
33996 static bool
33997 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
33999 cp_token *pragma_tok;
34000 unsigned int id;
34002 pragma_tok = cp_lexer_consume_token (parser->lexer);
34003 gcc_assert (pragma_tok->type == CPP_PRAGMA);
34004 parser->lexer->in_pragma = true;
34006 id = pragma_tok->pragma_kind;
34007 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
34008 cp_ensure_no_omp_declare_simd (parser);
34009 switch (id)
34011 case PRAGMA_GCC_PCH_PREPROCESS:
34012 error_at (pragma_tok->location,
34013 "%<#pragma GCC pch_preprocess%> must be first");
34014 break;
34016 case PRAGMA_OMP_BARRIER:
34017 switch (context)
34019 case pragma_compound:
34020 cp_parser_omp_barrier (parser, pragma_tok);
34021 return false;
34022 case pragma_stmt:
34023 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
34024 "used in compound statements");
34025 break;
34026 default:
34027 goto bad_stmt;
34029 break;
34031 case PRAGMA_OMP_FLUSH:
34032 switch (context)
34034 case pragma_compound:
34035 cp_parser_omp_flush (parser, pragma_tok);
34036 return false;
34037 case pragma_stmt:
34038 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
34039 "used in compound statements");
34040 break;
34041 default:
34042 goto bad_stmt;
34044 break;
34046 case PRAGMA_OMP_TASKWAIT:
34047 switch (context)
34049 case pragma_compound:
34050 cp_parser_omp_taskwait (parser, pragma_tok);
34051 return false;
34052 case pragma_stmt:
34053 error_at (pragma_tok->location,
34054 "%<#pragma omp taskwait%> may only be "
34055 "used in compound statements");
34056 break;
34057 default:
34058 goto bad_stmt;
34060 break;
34062 case PRAGMA_OMP_TASKYIELD:
34063 switch (context)
34065 case pragma_compound:
34066 cp_parser_omp_taskyield (parser, pragma_tok);
34067 return false;
34068 case pragma_stmt:
34069 error_at (pragma_tok->location,
34070 "%<#pragma omp taskyield%> may only be "
34071 "used in compound statements");
34072 break;
34073 default:
34074 goto bad_stmt;
34076 break;
34078 case PRAGMA_OMP_CANCEL:
34079 switch (context)
34081 case pragma_compound:
34082 cp_parser_omp_cancel (parser, pragma_tok);
34083 return false;
34084 case pragma_stmt:
34085 error_at (pragma_tok->location,
34086 "%<#pragma omp cancel%> may only be "
34087 "used in compound statements");
34088 break;
34089 default:
34090 goto bad_stmt;
34092 break;
34094 case PRAGMA_OMP_CANCELLATION_POINT:
34095 switch (context)
34097 case pragma_compound:
34098 cp_parser_omp_cancellation_point (parser, pragma_tok);
34099 return false;
34100 case pragma_stmt:
34101 error_at (pragma_tok->location,
34102 "%<#pragma omp cancellation point%> may only be "
34103 "used in compound statements");
34104 break;
34105 default:
34106 goto bad_stmt;
34108 break;
34110 case PRAGMA_OMP_THREADPRIVATE:
34111 cp_parser_omp_threadprivate (parser, pragma_tok);
34112 return false;
34114 case PRAGMA_OMP_DECLARE_REDUCTION:
34115 cp_parser_omp_declare (parser, pragma_tok, context);
34116 return false;
34118 case PRAGMA_OACC_CACHE:
34119 case PRAGMA_OACC_DATA:
34120 case PRAGMA_OACC_ENTER_DATA:
34121 case PRAGMA_OACC_EXIT_DATA:
34122 case PRAGMA_OACC_KERNELS:
34123 case PRAGMA_OACC_PARALLEL:
34124 case PRAGMA_OACC_LOOP:
34125 case PRAGMA_OACC_UPDATE:
34126 case PRAGMA_OACC_WAIT:
34127 case PRAGMA_OMP_ATOMIC:
34128 case PRAGMA_OMP_CRITICAL:
34129 case PRAGMA_OMP_DISTRIBUTE:
34130 case PRAGMA_OMP_FOR:
34131 case PRAGMA_OMP_MASTER:
34132 case PRAGMA_OMP_ORDERED:
34133 case PRAGMA_OMP_PARALLEL:
34134 case PRAGMA_OMP_SECTIONS:
34135 case PRAGMA_OMP_SIMD:
34136 case PRAGMA_OMP_SINGLE:
34137 case PRAGMA_OMP_TASK:
34138 case PRAGMA_OMP_TASKGROUP:
34139 case PRAGMA_OMP_TEAMS:
34140 if (context != pragma_stmt && context != pragma_compound)
34141 goto bad_stmt;
34142 cp_parser_omp_construct (parser, pragma_tok);
34143 return true;
34145 case PRAGMA_OMP_TARGET:
34146 return cp_parser_omp_target (parser, pragma_tok, context);
34148 case PRAGMA_OMP_END_DECLARE_TARGET:
34149 cp_parser_omp_end_declare_target (parser, pragma_tok);
34150 return false;
34152 case PRAGMA_OMP_SECTION:
34153 error_at (pragma_tok->location,
34154 "%<#pragma omp section%> may only be used in "
34155 "%<#pragma omp sections%> construct");
34156 break;
34158 case PRAGMA_IVDEP:
34160 if (context == pragma_external)
34162 error_at (pragma_tok->location,
34163 "%<#pragma GCC ivdep%> must be inside a function");
34164 break;
34166 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34167 cp_token *tok;
34168 tok = cp_lexer_peek_token (the_parser->lexer);
34169 if (tok->type != CPP_KEYWORD
34170 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
34171 && tok->keyword != RID_DO))
34173 cp_parser_error (parser, "for, while or do statement expected");
34174 return false;
34176 cp_parser_iteration_statement (parser, true);
34177 return true;
34180 case PRAGMA_CILK_SIMD:
34181 if (context == pragma_external)
34183 error_at (pragma_tok->location,
34184 "%<#pragma simd%> must be inside a function");
34185 break;
34187 cp_parser_cilk_simd (parser, pragma_tok);
34188 return true;
34190 case PRAGMA_CILK_GRAINSIZE:
34191 if (context == pragma_external)
34193 error_at (pragma_tok->location,
34194 "%<#pragma cilk grainsize%> must be inside a function");
34195 break;
34198 /* Ignore the pragma if Cilk Plus is not enabled. */
34199 if (flag_cilkplus)
34201 cp_parser_cilk_grainsize (parser, pragma_tok);
34202 return true;
34204 else
34206 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
34207 "%<#pragma cilk grainsize%>");
34208 break;
34211 default:
34212 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
34213 c_invoke_pragma_handler (id);
34214 break;
34216 bad_stmt:
34217 cp_parser_error (parser, "expected declaration specifiers");
34218 break;
34221 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34222 return false;
34225 /* The interface the pragma parsers have to the lexer. */
34227 enum cpp_ttype
34228 pragma_lex (tree *value)
34230 cp_token *tok;
34231 enum cpp_ttype ret;
34233 tok = cp_lexer_peek_token (the_parser->lexer);
34235 ret = tok->type;
34236 *value = tok->u.value;
34238 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
34239 ret = CPP_EOF;
34240 else if (ret == CPP_STRING)
34241 *value = cp_parser_string_literal (the_parser, false, false);
34242 else
34244 cp_lexer_consume_token (the_parser->lexer);
34245 if (ret == CPP_KEYWORD)
34246 ret = CPP_NAME;
34249 return ret;
34253 /* External interface. */
34255 /* Parse one entire translation unit. */
34257 void
34258 c_parse_file (void)
34260 static bool already_called = false;
34262 if (already_called)
34263 fatal_error (input_location,
34264 "inter-module optimizations not implemented for C++");
34265 already_called = true;
34267 the_parser = cp_parser_new ();
34268 push_deferring_access_checks (flag_access_control
34269 ? dk_no_deferred : dk_no_check);
34270 cp_parser_translation_unit (the_parser);
34271 the_parser = NULL;
34274 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
34275 vectorlength clause:
34276 Syntax:
34277 vectorlength ( constant-expression ) */
34279 static tree
34280 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
34281 bool is_simd_fn)
34283 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34284 tree expr;
34285 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
34286 safelen clause. Thus, vectorlength is represented as OMP 4.0
34287 safelen. For SIMD-enabled function it is represented by OMP 4.0
34288 simdlen. */
34289 if (!is_simd_fn)
34290 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
34291 loc);
34292 else
34293 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
34294 loc);
34296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34297 return error_mark_node;
34299 expr = cp_parser_constant_expression (parser);
34300 expr = maybe_constant_value (expr);
34302 /* If expr == error_mark_node, then don't emit any errors nor
34303 create a clause. if any of the above functions returns
34304 error mark node then they would have emitted an error message. */
34305 if (expr == error_mark_node)
34307 else if (!TREE_TYPE (expr)
34308 || !TREE_CONSTANT (expr)
34309 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
34310 error_at (loc, "vectorlength must be an integer constant");
34311 else if (TREE_CONSTANT (expr)
34312 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
34313 error_at (loc, "vectorlength must be a power of 2");
34314 else
34316 tree c;
34317 if (!is_simd_fn)
34319 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
34320 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
34321 OMP_CLAUSE_CHAIN (c) = clauses;
34322 clauses = c;
34324 else
34326 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
34327 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
34328 OMP_CLAUSE_CHAIN (c) = clauses;
34329 clauses = c;
34333 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34334 return error_mark_node;
34335 return clauses;
34338 /* Handles the Cilk Plus #pragma simd linear clause.
34339 Syntax:
34340 linear ( simd-linear-variable-list )
34342 simd-linear-variable-list:
34343 simd-linear-variable
34344 simd-linear-variable-list , simd-linear-variable
34346 simd-linear-variable:
34347 id-expression
34348 id-expression : simd-linear-step
34350 simd-linear-step:
34351 conditional-expression */
34353 static tree
34354 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
34356 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34359 return clauses;
34360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34362 cp_parser_error (parser, "expected identifier");
34363 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34364 return error_mark_node;
34367 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34368 parser->colon_corrects_to_scope_p = false;
34369 while (1)
34371 cp_token *token = cp_lexer_peek_token (parser->lexer);
34372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34374 cp_parser_error (parser, "expected variable-name");
34375 clauses = error_mark_node;
34376 break;
34379 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
34380 false, false);
34381 tree decl = cp_parser_lookup_name_simple (parser, var_name,
34382 token->location);
34383 if (decl == error_mark_node)
34385 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
34386 token->location);
34387 clauses = error_mark_node;
34389 else
34391 tree e = NULL_TREE;
34392 tree step_size = integer_one_node;
34394 /* If present, parse the linear step. Otherwise, assume the default
34395 value of 1. */
34396 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
34398 cp_lexer_consume_token (parser->lexer);
34400 e = cp_parser_assignment_expression (parser);
34401 e = maybe_constant_value (e);
34403 if (e == error_mark_node)
34405 /* If an error has occurred, then the whole pragma is
34406 considered ill-formed. Thus, no reason to keep
34407 parsing. */
34408 clauses = error_mark_node;
34409 break;
34411 else if (type_dependent_expression_p (e)
34412 || value_dependent_expression_p (e)
34413 || (TREE_TYPE (e)
34414 && INTEGRAL_TYPE_P (TREE_TYPE (e))
34415 && (TREE_CONSTANT (e)
34416 || DECL_P (e))))
34417 step_size = e;
34418 else
34419 cp_parser_error (parser,
34420 "step size must be an integer constant "
34421 "expression or an integer variable");
34424 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
34425 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34426 OMP_CLAUSE_DECL (l) = decl;
34427 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
34428 OMP_CLAUSE_CHAIN (l) = clauses;
34429 clauses = l;
34431 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34432 cp_lexer_consume_token (parser->lexer);
34433 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34434 break;
34435 else
34437 error_at (cp_lexer_peek_token (parser->lexer)->location,
34438 "expected %<,%> or %<)%> after %qE", decl);
34439 clauses = error_mark_node;
34440 break;
34443 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34444 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34445 return clauses;
34448 /* Returns the name of the next clause. If the clause is not
34449 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
34450 token is not consumed. Otherwise, the appropriate enum from the
34451 pragma_simd_clause is returned and the token is consumed. */
34453 static pragma_omp_clause
34454 cp_parser_cilk_simd_clause_name (cp_parser *parser)
34456 pragma_omp_clause clause_type;
34457 cp_token *token = cp_lexer_peek_token (parser->lexer);
34459 if (token->keyword == RID_PRIVATE)
34460 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
34461 else if (!token->u.value || token->type != CPP_NAME)
34462 return PRAGMA_CILK_CLAUSE_NONE;
34463 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
34464 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
34465 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
34466 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
34467 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
34468 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
34469 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
34470 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
34471 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
34472 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
34473 else
34474 return PRAGMA_CILK_CLAUSE_NONE;
34476 cp_lexer_consume_token (parser->lexer);
34477 return clause_type;
34480 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
34482 static tree
34483 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
34485 tree clauses = NULL_TREE;
34487 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
34488 && clauses != error_mark_node)
34490 pragma_omp_clause c_kind;
34491 c_kind = cp_parser_cilk_simd_clause_name (parser);
34492 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
34493 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
34494 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
34495 clauses = cp_parser_cilk_simd_linear (parser, clauses);
34496 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
34497 /* Use the OpenMP 4.0 equivalent function. */
34498 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
34499 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
34500 /* Use the OpenMP 4.0 equivalent function. */
34501 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34502 clauses);
34503 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
34504 /* Use the OMP 4.0 equivalent function. */
34505 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34506 clauses);
34507 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
34508 /* Use the OMP 4.0 equivalent function. */
34509 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34510 else
34512 clauses = error_mark_node;
34513 cp_parser_error (parser, "expected %<#pragma simd%> clause");
34514 break;
34518 cp_parser_skip_to_pragma_eol (parser, pragma_token);
34520 if (clauses == error_mark_node)
34521 return error_mark_node;
34522 else
34523 return c_finish_cilk_clauses (clauses);
34526 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
34528 static void
34529 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
34531 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
34533 if (clauses == error_mark_node)
34534 return;
34536 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
34538 error_at (cp_lexer_peek_token (parser->lexer)->location,
34539 "for statement expected");
34540 return;
34543 tree sb = begin_omp_structured_block ();
34544 int save = cp_parser_begin_omp_structured_block (parser);
34545 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
34546 if (ret)
34547 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
34548 cp_parser_end_omp_structured_block (parser, save);
34549 add_stmt (finish_omp_structured_block (sb));
34552 /* Main entry-point for parsing Cilk Plus _Cilk_for
34553 loops. The return value is error_mark_node
34554 when errors happen and CILK_FOR tree on success. */
34556 static tree
34557 cp_parser_cilk_for (cp_parser *parser, tree grain)
34559 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
34560 gcc_unreachable ();
34562 tree sb = begin_omp_structured_block ();
34563 int save = cp_parser_begin_omp_structured_block (parser);
34565 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
34566 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
34567 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
34568 clauses = finish_omp_clauses (clauses);
34570 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
34571 if (ret)
34572 cpp_validate_cilk_plus_loop (ret);
34573 else
34574 ret = error_mark_node;
34576 cp_parser_end_omp_structured_block (parser, save);
34577 add_stmt (finish_omp_structured_block (sb));
34578 return ret;
34581 /* Create an identifier for a generic parameter type (a synthesized
34582 template parameter implied by `auto' or a concept identifier). */
34584 static GTY(()) int generic_parm_count;
34585 static tree
34586 make_generic_type_name ()
34588 char buf[32];
34589 sprintf (buf, "auto:%d", ++generic_parm_count);
34590 return get_identifier (buf);
34593 /* Predicate that behaves as is_auto_or_concept but matches the parent
34594 node of the generic type rather than the generic type itself. This
34595 allows for type transformation in add_implicit_template_parms. */
34597 static inline bool
34598 tree_type_is_auto_or_concept (const_tree t)
34600 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
34603 /* Returns the template declaration being called or evaluated as
34604 part of the constraint check. Note that T must be a predicate
34605 constraint (it can't be any other kind of constraint). */
34606 static tree
34607 get_concept_from_constraint (tree t)
34609 gcc_assert (TREE_CODE (t) == PRED_CONSTR);
34610 t = PRED_CONSTR_EXPR (t);
34611 gcc_assert (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == TEMPLATE_ID_EXPR);
34613 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
34614 return DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
34615 else
34617 tree fn = CALL_EXPR_FN (t);
34618 tree ovl = TREE_OPERAND (fn, 0);
34619 tree tmpl = OVL_FUNCTION (ovl);
34620 return DECL_TEMPLATE_RESULT (tmpl);
34624 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
34625 (creating a new template parameter list if necessary). Returns the newly
34626 created template type parm. */
34628 tree
34629 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
34631 gcc_assert (current_binding_level->kind == sk_function_parms);
34633 // Before committing to modifying any scope, if we're in an implicit
34634 // template scope, and we're trying to synthesize a constrained
34635 // parameter, try to find a previous parameter with the same name.
34636 if (parser->implicit_template_scope && constr)
34638 tree t = parser->implicit_template_parms;
34639 while (t)
34641 tree c = get_concept_from_constraint (TREE_TYPE (t));
34642 if (c == DECL_SIZE_UNIT (constr))
34643 return TREE_VALUE (t);
34644 t = TREE_CHAIN (t);
34648 /* We are either continuing a function template that already contains implicit
34649 template parameters, creating a new fully-implicit function template, or
34650 extending an existing explicit function template with implicit template
34651 parameters. */
34653 cp_binding_level *const entry_scope = current_binding_level;
34655 bool become_template = false;
34656 cp_binding_level *parent_scope = 0;
34658 if (parser->implicit_template_scope)
34660 gcc_assert (parser->implicit_template_parms);
34662 current_binding_level = parser->implicit_template_scope;
34664 else
34666 /* Roll back to the existing template parameter scope (in the case of
34667 extending an explicit function template) or introduce a new template
34668 parameter scope ahead of the function parameter scope (or class scope
34669 in the case of out-of-line member definitions). The function scope is
34670 added back after template parameter synthesis below. */
34672 cp_binding_level *scope = entry_scope;
34674 while (scope->kind == sk_function_parms)
34676 parent_scope = scope;
34677 scope = scope->level_chain;
34679 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
34681 /* If not defining a class, then any class scope is a scope level in
34682 an out-of-line member definition. In this case simply wind back
34683 beyond the first such scope to inject the template parameter list.
34684 Otherwise wind back to the class being defined. The latter can
34685 occur in class member friend declarations such as:
34687 class A {
34688 void foo (auto);
34690 class B {
34691 friend void A::foo (auto);
34694 The template parameter list synthesized for the friend declaration
34695 must be injected in the scope of 'B'. This can also occur in
34696 erroneous cases such as:
34698 struct A {
34699 struct B {
34700 void foo (auto);
34702 void B::foo (auto) {}
34705 Here the attempted definition of 'B::foo' within 'A' is ill-formed
34706 but, nevertheless, the template parameter list synthesized for the
34707 declarator should be injected into the scope of 'A' as if the
34708 ill-formed template was specified explicitly. */
34710 while (scope->kind == sk_class && !scope->defining_class_p)
34712 parent_scope = scope;
34713 scope = scope->level_chain;
34717 current_binding_level = scope;
34719 if (scope->kind != sk_template_parms
34720 || !function_being_declared_is_template_p (parser))
34722 /* Introduce a new template parameter list for implicit template
34723 parameters. */
34724 become_template = true;
34726 parser->implicit_template_scope
34727 = begin_scope (sk_template_parms, NULL);
34729 ++processing_template_decl;
34731 parser->fully_implicit_function_template_p = true;
34732 ++parser->num_template_parameter_lists;
34734 else
34736 /* Synthesize implicit template parameters at the end of the explicit
34737 template parameter list. */
34739 gcc_assert (current_template_parms);
34741 parser->implicit_template_scope = scope;
34743 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
34744 parser->implicit_template_parms
34745 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
34749 /* Synthesize a new template parameter and track the current template
34750 parameter chain with implicit_template_parms. */
34752 tree synth_id = make_generic_type_name ();
34753 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
34754 synth_id);
34756 // Attach the constraint to the parm before processing.
34757 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
34758 TREE_TYPE (node) = constr;
34759 tree new_parm
34760 = process_template_parm (parser->implicit_template_parms,
34761 input_location,
34762 node,
34763 /*non_type=*/false,
34764 /*param_pack=*/false);
34766 // Chain the new parameter to the list of implicit parameters.
34767 if (parser->implicit_template_parms)
34768 parser->implicit_template_parms
34769 = TREE_CHAIN (parser->implicit_template_parms);
34770 else
34771 parser->implicit_template_parms = new_parm;
34773 tree new_type = TREE_TYPE (getdecls ());
34775 /* If creating a fully implicit function template, start the new implicit
34776 template parameter list with this synthesized type, otherwise grow the
34777 current template parameter list. */
34779 if (become_template)
34781 parent_scope->level_chain = current_binding_level;
34783 tree new_parms = make_tree_vec (1);
34784 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
34785 current_template_parms = tree_cons (size_int (processing_template_decl),
34786 new_parms, current_template_parms);
34788 else
34790 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
34791 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
34792 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
34793 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
34796 // If the new parameter was constrained, we need to add that to the
34797 // constraints in the tmeplate parameter list.
34798 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
34800 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
34801 reqs = conjoin_constraints (reqs, req);
34802 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
34805 current_binding_level = entry_scope;
34807 return new_type;
34810 /* Finish the declaration of a fully implicit function template. Such a
34811 template has no explicit template parameter list so has not been through the
34812 normal template head and tail processing. synthesize_implicit_template_parm
34813 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
34814 provided if the declaration is a class member such that its template
34815 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
34816 form is returned. Otherwise NULL_TREE is returned. */
34818 tree
34819 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
34821 gcc_assert (parser->fully_implicit_function_template_p);
34823 if (member_decl_opt && member_decl_opt != error_mark_node
34824 && DECL_VIRTUAL_P (member_decl_opt))
34826 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
34827 "implicit templates may not be %<virtual%>");
34828 DECL_VIRTUAL_P (member_decl_opt) = false;
34831 if (member_decl_opt)
34832 member_decl_opt = finish_member_template_decl (member_decl_opt);
34833 end_template_decl ();
34835 parser->fully_implicit_function_template_p = false;
34836 --parser->num_template_parameter_lists;
34838 return member_decl_opt;
34841 #include "gt-cp-parser.h"