2015-05-08 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / parser.c
blob4aa0f42e29cac50927999f39b705a0244abf8254
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 return get_function_declarator (declarator) != NULL;
1667 /* The parser. */
1669 /* Overview
1670 --------
1672 A cp_parser parses the token stream as specified by the C++
1673 grammar. Its job is purely parsing, not semantic analysis. For
1674 example, the parser breaks the token stream into declarators,
1675 expressions, statements, and other similar syntactic constructs.
1676 It does not check that the types of the expressions on either side
1677 of an assignment-statement are compatible, or that a function is
1678 not declared with a parameter of type `void'.
1680 The parser invokes routines elsewhere in the compiler to perform
1681 semantic analysis and to build up the abstract syntax tree for the
1682 code processed.
1684 The parser (and the template instantiation code, which is, in a
1685 way, a close relative of parsing) are the only parts of the
1686 compiler that should be calling push_scope and pop_scope, or
1687 related functions. The parser (and template instantiation code)
1688 keeps track of what scope is presently active; everything else
1689 should simply honor that. (The code that generates static
1690 initializers may also need to set the scope, in order to check
1691 access control correctly when emitting the initializers.)
1693 Methodology
1694 -----------
1696 The parser is of the standard recursive-descent variety. Upcoming
1697 tokens in the token stream are examined in order to determine which
1698 production to use when parsing a non-terminal. Some C++ constructs
1699 require arbitrary look ahead to disambiguate. For example, it is
1700 impossible, in the general case, to tell whether a statement is an
1701 expression or declaration without scanning the entire statement.
1702 Therefore, the parser is capable of "parsing tentatively." When the
1703 parser is not sure what construct comes next, it enters this mode.
1704 Then, while we attempt to parse the construct, the parser queues up
1705 error messages, rather than issuing them immediately, and saves the
1706 tokens it consumes. If the construct is parsed successfully, the
1707 parser "commits", i.e., it issues any queued error messages and
1708 the tokens that were being preserved are permanently discarded.
1709 If, however, the construct is not parsed successfully, the parser
1710 rolls back its state completely so that it can resume parsing using
1711 a different alternative.
1713 Future Improvements
1714 -------------------
1716 The performance of the parser could probably be improved substantially.
1717 We could often eliminate the need to parse tentatively by looking ahead
1718 a little bit. In some places, this approach might not entirely eliminate
1719 the need to parse tentatively, but it might still speed up the average
1720 case. */
1722 /* Flags that are passed to some parsing functions. These values can
1723 be bitwise-ored together. */
1725 enum
1727 /* No flags. */
1728 CP_PARSER_FLAGS_NONE = 0x0,
1729 /* The construct is optional. If it is not present, then no error
1730 should be issued. */
1731 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1732 /* When parsing a type-specifier, treat user-defined type-names
1733 as non-type identifiers. */
1734 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1735 /* When parsing a type-specifier, do not try to parse a class-specifier
1736 or enum-specifier. */
1737 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1738 /* When parsing a decl-specifier-seq, only allow type-specifier or
1739 constexpr. */
1740 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1743 /* This type is used for parameters and variables which hold
1744 combinations of the above flags. */
1745 typedef int cp_parser_flags;
1747 /* The different kinds of declarators we want to parse. */
1749 typedef enum cp_parser_declarator_kind
1751 /* We want an abstract declarator. */
1752 CP_PARSER_DECLARATOR_ABSTRACT,
1753 /* We want a named declarator. */
1754 CP_PARSER_DECLARATOR_NAMED,
1755 /* We don't mind, but the name must be an unqualified-id. */
1756 CP_PARSER_DECLARATOR_EITHER
1757 } cp_parser_declarator_kind;
1759 /* The precedence values used to parse binary expressions. The minimum value
1760 of PREC must be 1, because zero is reserved to quickly discriminate
1761 binary operators from other tokens. */
1763 enum cp_parser_prec
1765 PREC_NOT_OPERATOR,
1766 PREC_LOGICAL_OR_EXPRESSION,
1767 PREC_LOGICAL_AND_EXPRESSION,
1768 PREC_INCLUSIVE_OR_EXPRESSION,
1769 PREC_EXCLUSIVE_OR_EXPRESSION,
1770 PREC_AND_EXPRESSION,
1771 PREC_EQUALITY_EXPRESSION,
1772 PREC_RELATIONAL_EXPRESSION,
1773 PREC_SHIFT_EXPRESSION,
1774 PREC_ADDITIVE_EXPRESSION,
1775 PREC_MULTIPLICATIVE_EXPRESSION,
1776 PREC_PM_EXPRESSION,
1777 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1780 /* A mapping from a token type to a corresponding tree node type, with a
1781 precedence value. */
1783 typedef struct cp_parser_binary_operations_map_node
1785 /* The token type. */
1786 enum cpp_ttype token_type;
1787 /* The corresponding tree code. */
1788 enum tree_code tree_type;
1789 /* The precedence of this operator. */
1790 enum cp_parser_prec prec;
1791 } cp_parser_binary_operations_map_node;
1793 typedef struct cp_parser_expression_stack_entry
1795 /* Left hand side of the binary operation we are currently
1796 parsing. */
1797 tree lhs;
1798 /* Original tree code for left hand side, if it was a binary
1799 expression itself (used for -Wparentheses). */
1800 enum tree_code lhs_type;
1801 /* Tree code for the binary operation we are parsing. */
1802 enum tree_code tree_type;
1803 /* Precedence of the binary operation we are parsing. */
1804 enum cp_parser_prec prec;
1805 /* Location of the binary operation we are parsing. */
1806 location_t loc;
1807 } cp_parser_expression_stack_entry;
1809 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1810 entries because precedence levels on the stack are monotonically
1811 increasing. */
1812 typedef struct cp_parser_expression_stack_entry
1813 cp_parser_expression_stack[NUM_PREC_VALUES];
1815 /* Prototypes. */
1817 /* Constructors and destructors. */
1819 static cp_parser_context *cp_parser_context_new
1820 (cp_parser_context *);
1822 /* Class variables. */
1824 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1826 /* The operator-precedence table used by cp_parser_binary_expression.
1827 Transformed into an associative array (binops_by_token) by
1828 cp_parser_new. */
1830 static const cp_parser_binary_operations_map_node binops[] = {
1831 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1832 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1834 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1835 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1836 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1838 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1839 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1841 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1842 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1844 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1845 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1846 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1847 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1849 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1850 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1852 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1854 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1856 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1858 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1860 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1863 /* The same as binops, but initialized by cp_parser_new so that
1864 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1865 for speed. */
1866 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1868 /* Constructors and destructors. */
1870 /* Construct a new context. The context below this one on the stack
1871 is given by NEXT. */
1873 static cp_parser_context *
1874 cp_parser_context_new (cp_parser_context* next)
1876 cp_parser_context *context;
1878 /* Allocate the storage. */
1879 if (cp_parser_context_free_list != NULL)
1881 /* Pull the first entry from the free list. */
1882 context = cp_parser_context_free_list;
1883 cp_parser_context_free_list = context->next;
1884 memset (context, 0, sizeof (*context));
1886 else
1887 context = ggc_cleared_alloc<cp_parser_context> ();
1889 /* No errors have occurred yet in this context. */
1890 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1891 /* If this is not the bottommost context, copy information that we
1892 need from the previous context. */
1893 if (next)
1895 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1896 expression, then we are parsing one in this context, too. */
1897 context->object_type = next->object_type;
1898 /* Thread the stack. */
1899 context->next = next;
1902 return context;
1905 /* Managing the unparsed function queues. */
1907 #define unparsed_funs_with_default_args \
1908 parser->unparsed_queues->last ().funs_with_default_args
1909 #define unparsed_funs_with_definitions \
1910 parser->unparsed_queues->last ().funs_with_definitions
1911 #define unparsed_nsdmis \
1912 parser->unparsed_queues->last ().nsdmis
1913 #define unparsed_classes \
1914 parser->unparsed_queues->last ().classes
1916 static void
1917 push_unparsed_function_queues (cp_parser *parser)
1919 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1920 vec_safe_push (parser->unparsed_queues, e);
1923 static void
1924 pop_unparsed_function_queues (cp_parser *parser)
1926 release_tree_vector (unparsed_funs_with_definitions);
1927 parser->unparsed_queues->pop ();
1930 /* Prototypes. */
1932 /* Constructors and destructors. */
1934 static cp_parser *cp_parser_new
1935 (void);
1937 /* Routines to parse various constructs.
1939 Those that return `tree' will return the error_mark_node (rather
1940 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1941 Sometimes, they will return an ordinary node if error-recovery was
1942 attempted, even though a parse error occurred. So, to check
1943 whether or not a parse error occurred, you should always use
1944 cp_parser_error_occurred. If the construct is optional (indicated
1945 either by an `_opt' in the name of the function that does the
1946 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1947 the construct is not present. */
1949 /* Lexical conventions [gram.lex] */
1951 static tree cp_parser_identifier
1952 (cp_parser *);
1953 static tree cp_parser_string_literal
1954 (cp_parser *, bool, bool, bool);
1955 static tree cp_parser_userdef_char_literal
1956 (cp_parser *);
1957 static tree cp_parser_userdef_string_literal
1958 (tree);
1959 static tree cp_parser_userdef_numeric_literal
1960 (cp_parser *);
1962 /* Basic concepts [gram.basic] */
1964 static bool cp_parser_translation_unit
1965 (cp_parser *);
1967 /* Expressions [gram.expr] */
1969 static tree cp_parser_primary_expression
1970 (cp_parser *, bool, bool, bool, cp_id_kind *);
1971 static tree cp_parser_id_expression
1972 (cp_parser *, bool, bool, bool *, bool, bool);
1973 static tree cp_parser_unqualified_id
1974 (cp_parser *, bool, bool, bool, bool);
1975 static tree cp_parser_nested_name_specifier_opt
1976 (cp_parser *, bool, bool, bool, bool);
1977 static tree cp_parser_nested_name_specifier
1978 (cp_parser *, bool, bool, bool, bool);
1979 static tree cp_parser_qualifying_entity
1980 (cp_parser *, bool, bool, bool, bool, bool);
1981 static tree cp_parser_postfix_expression
1982 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1983 static tree cp_parser_postfix_open_square_expression
1984 (cp_parser *, tree, bool, bool);
1985 static tree cp_parser_postfix_dot_deref_expression
1986 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1987 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1988 (cp_parser *, int, bool, bool, bool *, bool = false);
1989 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1990 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1991 static void cp_parser_pseudo_destructor_name
1992 (cp_parser *, tree, tree *, tree *);
1993 static tree cp_parser_unary_expression
1994 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1995 static enum tree_code cp_parser_unary_operator
1996 (cp_token *);
1997 static tree cp_parser_new_expression
1998 (cp_parser *);
1999 static vec<tree, va_gc> *cp_parser_new_placement
2000 (cp_parser *);
2001 static tree cp_parser_new_type_id
2002 (cp_parser *, tree *);
2003 static cp_declarator *cp_parser_new_declarator_opt
2004 (cp_parser *);
2005 static cp_declarator *cp_parser_direct_new_declarator
2006 (cp_parser *);
2007 static vec<tree, va_gc> *cp_parser_new_initializer
2008 (cp_parser *);
2009 static tree cp_parser_delete_expression
2010 (cp_parser *);
2011 static tree cp_parser_cast_expression
2012 (cp_parser *, bool, bool, bool, cp_id_kind *);
2013 static tree cp_parser_binary_expression
2014 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2015 static tree cp_parser_question_colon_clause
2016 (cp_parser *, tree);
2017 static tree cp_parser_assignment_expression
2018 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2019 static enum tree_code cp_parser_assignment_operator_opt
2020 (cp_parser *);
2021 static tree cp_parser_expression
2022 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2023 static tree cp_parser_constant_expression
2024 (cp_parser *, bool = false, bool * = NULL);
2025 static tree cp_parser_builtin_offsetof
2026 (cp_parser *);
2027 static tree cp_parser_lambda_expression
2028 (cp_parser *);
2029 static void cp_parser_lambda_introducer
2030 (cp_parser *, tree);
2031 static bool cp_parser_lambda_declarator_opt
2032 (cp_parser *, tree);
2033 static void cp_parser_lambda_body
2034 (cp_parser *, tree);
2036 /* Statements [gram.stmt.stmt] */
2038 static void cp_parser_statement
2039 (cp_parser *, tree, bool, bool *);
2040 static void cp_parser_label_for_labeled_statement
2041 (cp_parser *, tree);
2042 static tree cp_parser_expression_statement
2043 (cp_parser *, tree);
2044 static tree cp_parser_compound_statement
2045 (cp_parser *, tree, bool, bool);
2046 static void cp_parser_statement_seq_opt
2047 (cp_parser *, tree);
2048 static tree cp_parser_selection_statement
2049 (cp_parser *, bool *);
2050 static tree cp_parser_condition
2051 (cp_parser *);
2052 static tree cp_parser_iteration_statement
2053 (cp_parser *, bool);
2054 static bool cp_parser_for_init_statement
2055 (cp_parser *, tree *decl);
2056 static tree cp_parser_for
2057 (cp_parser *, bool);
2058 static tree cp_parser_c_for
2059 (cp_parser *, tree, tree, bool);
2060 static tree cp_parser_range_for
2061 (cp_parser *, tree, tree, tree, bool);
2062 static void do_range_for_auto_deduction
2063 (tree, tree);
2064 static tree cp_parser_perform_range_for_lookup
2065 (tree, tree *, tree *);
2066 static tree cp_parser_range_for_member_function
2067 (tree, tree);
2068 static tree cp_parser_jump_statement
2069 (cp_parser *);
2070 static void cp_parser_declaration_statement
2071 (cp_parser *);
2073 static tree cp_parser_implicitly_scoped_statement
2074 (cp_parser *, bool *);
2075 static void cp_parser_already_scoped_statement
2076 (cp_parser *);
2078 /* Declarations [gram.dcl.dcl] */
2080 static void cp_parser_declaration_seq_opt
2081 (cp_parser *);
2082 static void cp_parser_declaration
2083 (cp_parser *);
2084 static void cp_parser_block_declaration
2085 (cp_parser *, bool);
2086 static void cp_parser_simple_declaration
2087 (cp_parser *, bool, tree *);
2088 static void cp_parser_decl_specifier_seq
2089 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2090 static tree cp_parser_storage_class_specifier_opt
2091 (cp_parser *);
2092 static tree cp_parser_function_specifier_opt
2093 (cp_parser *, cp_decl_specifier_seq *);
2094 static tree cp_parser_type_specifier
2095 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2096 int *, bool *);
2097 static tree cp_parser_simple_type_specifier
2098 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2099 static tree cp_parser_type_name
2100 (cp_parser *, bool);
2101 static tree cp_parser_type_name
2102 (cp_parser *);
2103 static tree cp_parser_nonclass_name
2104 (cp_parser* parser);
2105 static tree cp_parser_elaborated_type_specifier
2106 (cp_parser *, bool, bool);
2107 static tree cp_parser_enum_specifier
2108 (cp_parser *);
2109 static void cp_parser_enumerator_list
2110 (cp_parser *, tree);
2111 static void cp_parser_enumerator_definition
2112 (cp_parser *, tree);
2113 static tree cp_parser_namespace_name
2114 (cp_parser *);
2115 static void cp_parser_namespace_definition
2116 (cp_parser *);
2117 static void cp_parser_namespace_body
2118 (cp_parser *);
2119 static tree cp_parser_qualified_namespace_specifier
2120 (cp_parser *);
2121 static void cp_parser_namespace_alias_definition
2122 (cp_parser *);
2123 static bool cp_parser_using_declaration
2124 (cp_parser *, bool);
2125 static void cp_parser_using_directive
2126 (cp_parser *);
2127 static tree cp_parser_alias_declaration
2128 (cp_parser *);
2129 static void cp_parser_asm_definition
2130 (cp_parser *);
2131 static void cp_parser_linkage_specification
2132 (cp_parser *);
2133 static void cp_parser_static_assert
2134 (cp_parser *, bool);
2135 static tree cp_parser_decltype
2136 (cp_parser *);
2138 /* Declarators [gram.dcl.decl] */
2140 static tree cp_parser_init_declarator
2141 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2142 bool, bool, int, bool *, tree *, location_t *);
2143 static cp_declarator *cp_parser_declarator
2144 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2145 static cp_declarator *cp_parser_direct_declarator
2146 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2147 static enum tree_code cp_parser_ptr_operator
2148 (cp_parser *, tree *, cp_cv_quals *, tree *);
2149 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2150 (cp_parser *);
2151 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2152 (cp_parser *);
2153 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_late_return_type_opt
2156 (cp_parser *, cp_declarator *, cp_cv_quals);
2157 static tree cp_parser_declarator_id
2158 (cp_parser *, bool);
2159 static tree cp_parser_type_id
2160 (cp_parser *);
2161 static tree cp_parser_template_type_arg
2162 (cp_parser *);
2163 static tree cp_parser_trailing_type_id (cp_parser *);
2164 static tree cp_parser_type_id_1
2165 (cp_parser *, bool, bool);
2166 static void cp_parser_type_specifier_seq
2167 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2168 static tree cp_parser_parameter_declaration_clause
2169 (cp_parser *);
2170 static tree cp_parser_parameter_declaration_list
2171 (cp_parser *, bool *);
2172 static cp_parameter_declarator *cp_parser_parameter_declaration
2173 (cp_parser *, bool, bool *);
2174 static tree cp_parser_default_argument
2175 (cp_parser *, bool);
2176 static void cp_parser_function_body
2177 (cp_parser *, bool);
2178 static tree cp_parser_initializer
2179 (cp_parser *, bool *, bool *);
2180 static tree cp_parser_initializer_clause
2181 (cp_parser *, bool *);
2182 static tree cp_parser_braced_list
2183 (cp_parser*, bool*);
2184 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2185 (cp_parser *, bool *);
2187 static bool cp_parser_ctor_initializer_opt_and_function_body
2188 (cp_parser *, bool);
2190 static tree cp_parser_late_parsing_omp_declare_simd
2191 (cp_parser *, tree);
2193 static tree cp_parser_late_parsing_cilk_simd_fn_info
2194 (cp_parser *, tree);
2196 static tree synthesize_implicit_template_parm
2197 (cp_parser *, tree);
2198 static tree finish_fully_implicit_template
2199 (cp_parser *, tree);
2201 /* Classes [gram.class] */
2203 static tree cp_parser_class_name
2204 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2205 static tree cp_parser_class_specifier
2206 (cp_parser *);
2207 static tree cp_parser_class_head
2208 (cp_parser *, bool *);
2209 static enum tag_types cp_parser_class_key
2210 (cp_parser *);
2211 static void cp_parser_type_parameter_key
2212 (cp_parser* parser);
2213 static void cp_parser_member_specification_opt
2214 (cp_parser *);
2215 static void cp_parser_member_declaration
2216 (cp_parser *);
2217 static tree cp_parser_pure_specifier
2218 (cp_parser *);
2219 static tree cp_parser_constant_initializer
2220 (cp_parser *);
2222 /* Derived classes [gram.class.derived] */
2224 static tree cp_parser_base_clause
2225 (cp_parser *);
2226 static tree cp_parser_base_specifier
2227 (cp_parser *);
2229 /* Special member functions [gram.special] */
2231 static tree cp_parser_conversion_function_id
2232 (cp_parser *);
2233 static tree cp_parser_conversion_type_id
2234 (cp_parser *);
2235 static cp_declarator *cp_parser_conversion_declarator_opt
2236 (cp_parser *);
2237 static bool cp_parser_ctor_initializer_opt
2238 (cp_parser *);
2239 static void cp_parser_mem_initializer_list
2240 (cp_parser *);
2241 static tree cp_parser_mem_initializer
2242 (cp_parser *);
2243 static tree cp_parser_mem_initializer_id
2244 (cp_parser *);
2246 /* Overloading [gram.over] */
2248 static tree cp_parser_operator_function_id
2249 (cp_parser *);
2250 static tree cp_parser_operator
2251 (cp_parser *);
2253 /* Templates [gram.temp] */
2255 static void cp_parser_template_declaration
2256 (cp_parser *, bool);
2257 static tree cp_parser_template_parameter_list
2258 (cp_parser *);
2259 static tree cp_parser_template_parameter
2260 (cp_parser *, bool *, bool *);
2261 static tree cp_parser_type_parameter
2262 (cp_parser *, bool *);
2263 static tree cp_parser_template_id
2264 (cp_parser *, bool, bool, enum tag_types, bool);
2265 static tree cp_parser_template_name
2266 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2267 static tree cp_parser_template_argument_list
2268 (cp_parser *);
2269 static tree cp_parser_template_argument
2270 (cp_parser *);
2271 static void cp_parser_explicit_instantiation
2272 (cp_parser *);
2273 static void cp_parser_explicit_specialization
2274 (cp_parser *);
2276 /* Exception handling [gram.exception] */
2278 static tree cp_parser_try_block
2279 (cp_parser *);
2280 static bool cp_parser_function_try_block
2281 (cp_parser *);
2282 static void cp_parser_handler_seq
2283 (cp_parser *);
2284 static void cp_parser_handler
2285 (cp_parser *);
2286 static tree cp_parser_exception_declaration
2287 (cp_parser *);
2288 static tree cp_parser_throw_expression
2289 (cp_parser *);
2290 static tree cp_parser_exception_specification_opt
2291 (cp_parser *);
2292 static tree cp_parser_type_id_list
2293 (cp_parser *);
2295 /* GNU Extensions */
2297 static tree cp_parser_asm_specification_opt
2298 (cp_parser *);
2299 static tree cp_parser_asm_operand_list
2300 (cp_parser *);
2301 static tree cp_parser_asm_clobber_list
2302 (cp_parser *);
2303 static tree cp_parser_asm_label_list
2304 (cp_parser *);
2305 static bool cp_next_tokens_can_be_attribute_p
2306 (cp_parser *);
2307 static bool cp_next_tokens_can_be_gnu_attribute_p
2308 (cp_parser *);
2309 static bool cp_next_tokens_can_be_std_attribute_p
2310 (cp_parser *);
2311 static bool cp_nth_tokens_can_be_std_attribute_p
2312 (cp_parser *, size_t);
2313 static bool cp_nth_tokens_can_be_gnu_attribute_p
2314 (cp_parser *, size_t);
2315 static bool cp_nth_tokens_can_be_attribute_p
2316 (cp_parser *, size_t);
2317 static tree cp_parser_attributes_opt
2318 (cp_parser *);
2319 static tree cp_parser_gnu_attributes_opt
2320 (cp_parser *);
2321 static tree cp_parser_gnu_attribute_list
2322 (cp_parser *);
2323 static tree cp_parser_std_attribute
2324 (cp_parser *);
2325 static tree cp_parser_std_attribute_spec
2326 (cp_parser *);
2327 static tree cp_parser_std_attribute_spec_seq
2328 (cp_parser *);
2329 static bool cp_parser_extension_opt
2330 (cp_parser *, int *);
2331 static void cp_parser_label_declaration
2332 (cp_parser *);
2334 /* Concept Extensions */
2336 static tree cp_parser_requires_clause
2337 (cp_parser *);
2338 static tree cp_parser_requires_clause_opt
2339 (cp_parser *);
2340 static tree cp_parser_requires_expression
2341 (cp_parser *);
2342 static tree cp_parser_requirement_parameter_list
2343 (cp_parser *);
2344 static tree cp_parser_requirement_body
2345 (cp_parser *);
2346 static tree cp_parser_requirement_list
2347 (cp_parser *);
2348 static tree cp_parser_requirement
2349 (cp_parser *);
2350 static tree cp_parser_simple_requirement
2351 (cp_parser *);
2352 static tree cp_parser_compound_requirement
2353 (cp_parser *);
2354 static tree cp_parser_type_requirement
2355 (cp_parser *);
2356 static tree cp_parser_nested_requirement
2357 (cp_parser *);
2359 /* Transactional Memory Extensions */
2361 static tree cp_parser_transaction
2362 (cp_parser *, enum rid);
2363 static tree cp_parser_transaction_expression
2364 (cp_parser *, enum rid);
2365 static bool cp_parser_function_transaction
2366 (cp_parser *, enum rid);
2367 static tree cp_parser_transaction_cancel
2368 (cp_parser *);
2370 enum pragma_context {
2371 pragma_external,
2372 pragma_member,
2373 pragma_objc_icode,
2374 pragma_stmt,
2375 pragma_compound
2377 static bool cp_parser_pragma
2378 (cp_parser *, enum pragma_context);
2380 /* Objective-C++ Productions */
2382 static tree cp_parser_objc_message_receiver
2383 (cp_parser *);
2384 static tree cp_parser_objc_message_args
2385 (cp_parser *);
2386 static tree cp_parser_objc_message_expression
2387 (cp_parser *);
2388 static tree cp_parser_objc_encode_expression
2389 (cp_parser *);
2390 static tree cp_parser_objc_defs_expression
2391 (cp_parser *);
2392 static tree cp_parser_objc_protocol_expression
2393 (cp_parser *);
2394 static tree cp_parser_objc_selector_expression
2395 (cp_parser *);
2396 static tree cp_parser_objc_expression
2397 (cp_parser *);
2398 static bool cp_parser_objc_selector_p
2399 (enum cpp_ttype);
2400 static tree cp_parser_objc_selector
2401 (cp_parser *);
2402 static tree cp_parser_objc_protocol_refs_opt
2403 (cp_parser *);
2404 static void cp_parser_objc_declaration
2405 (cp_parser *, tree);
2406 static tree cp_parser_objc_statement
2407 (cp_parser *);
2408 static bool cp_parser_objc_valid_prefix_attributes
2409 (cp_parser *, tree *);
2410 static void cp_parser_objc_at_property_declaration
2411 (cp_parser *) ;
2412 static void cp_parser_objc_at_synthesize_declaration
2413 (cp_parser *) ;
2414 static void cp_parser_objc_at_dynamic_declaration
2415 (cp_parser *) ;
2416 static tree cp_parser_objc_struct_declaration
2417 (cp_parser *) ;
2419 /* Utility Routines */
2421 static tree cp_parser_lookup_name
2422 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2423 static tree cp_parser_lookup_name_simple
2424 (cp_parser *, tree, location_t);
2425 static tree cp_parser_maybe_treat_template_as_class
2426 (tree, bool);
2427 static bool cp_parser_check_declarator_template_parameters
2428 (cp_parser *, cp_declarator *, location_t);
2429 static bool cp_parser_check_template_parameters
2430 (cp_parser *, unsigned, location_t, cp_declarator *);
2431 static tree cp_parser_simple_cast_expression
2432 (cp_parser *);
2433 static tree cp_parser_global_scope_opt
2434 (cp_parser *, bool);
2435 static bool cp_parser_constructor_declarator_p
2436 (cp_parser *, bool);
2437 static tree cp_parser_function_definition_from_specifiers_and_declarator
2438 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2439 static tree cp_parser_function_definition_after_declarator
2440 (cp_parser *, bool);
2441 static void cp_parser_template_declaration_after_export
2442 (cp_parser *, bool);
2443 static void cp_parser_perform_template_parameter_access_checks
2444 (vec<deferred_access_check, va_gc> *);
2445 static tree cp_parser_single_declaration
2446 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2447 static tree cp_parser_functional_cast
2448 (cp_parser *, tree);
2449 static tree cp_parser_save_member_function_body
2450 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2451 static tree cp_parser_save_nsdmi
2452 (cp_parser *);
2453 static tree cp_parser_enclosed_template_argument_list
2454 (cp_parser *);
2455 static void cp_parser_save_default_args
2456 (cp_parser *, tree);
2457 static void cp_parser_late_parsing_for_member
2458 (cp_parser *, tree);
2459 static tree cp_parser_late_parse_one_default_arg
2460 (cp_parser *, tree, tree, tree);
2461 static void cp_parser_late_parsing_nsdmi
2462 (cp_parser *, tree);
2463 static void cp_parser_late_parsing_default_args
2464 (cp_parser *, tree);
2465 static tree cp_parser_sizeof_operand
2466 (cp_parser *, enum rid);
2467 static tree cp_parser_trait_expr
2468 (cp_parser *, enum rid);
2469 static bool cp_parser_declares_only_class_p
2470 (cp_parser *);
2471 static void cp_parser_set_storage_class
2472 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2473 static void cp_parser_set_decl_spec_type
2474 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2475 static void set_and_check_decl_spec_loc
2476 (cp_decl_specifier_seq *decl_specs,
2477 cp_decl_spec ds, cp_token *);
2478 static bool cp_parser_friend_p
2479 (const cp_decl_specifier_seq *);
2480 static void cp_parser_required_error
2481 (cp_parser *, required_token, bool);
2482 static cp_token *cp_parser_require
2483 (cp_parser *, enum cpp_ttype, required_token);
2484 static cp_token *cp_parser_require_keyword
2485 (cp_parser *, enum rid, required_token);
2486 static bool cp_parser_token_starts_function_definition_p
2487 (cp_token *);
2488 static bool cp_parser_next_token_starts_class_definition_p
2489 (cp_parser *);
2490 static bool cp_parser_next_token_ends_template_argument_p
2491 (cp_parser *);
2492 static bool cp_parser_nth_token_starts_template_argument_list_p
2493 (cp_parser *, size_t);
2494 static enum tag_types cp_parser_token_is_class_key
2495 (cp_token *);
2496 static enum tag_types cp_parser_token_is_type_parameter_key
2497 (cp_token *);
2498 static void cp_parser_check_class_key
2499 (enum tag_types, tree type);
2500 static void cp_parser_check_access_in_redeclaration
2501 (tree type, location_t location);
2502 static bool cp_parser_optional_template_keyword
2503 (cp_parser *);
2504 static void cp_parser_pre_parsed_nested_name_specifier
2505 (cp_parser *);
2506 static bool cp_parser_cache_group
2507 (cp_parser *, enum cpp_ttype, unsigned);
2508 static tree cp_parser_cache_defarg
2509 (cp_parser *parser, bool nsdmi);
2510 static void cp_parser_parse_tentatively
2511 (cp_parser *);
2512 static void cp_parser_commit_to_tentative_parse
2513 (cp_parser *);
2514 static void cp_parser_commit_to_topmost_tentative_parse
2515 (cp_parser *);
2516 static void cp_parser_abort_tentative_parse
2517 (cp_parser *);
2518 static bool cp_parser_parse_definitely
2519 (cp_parser *);
2520 static inline bool cp_parser_parsing_tentatively
2521 (cp_parser *);
2522 static bool cp_parser_uncommitted_to_tentative_parse_p
2523 (cp_parser *);
2524 static void cp_parser_error
2525 (cp_parser *, const char *);
2526 static void cp_parser_name_lookup_error
2527 (cp_parser *, tree, tree, name_lookup_error, location_t);
2528 static bool cp_parser_simulate_error
2529 (cp_parser *);
2530 static bool cp_parser_check_type_definition
2531 (cp_parser *);
2532 static void cp_parser_check_for_definition_in_return_type
2533 (cp_declarator *, tree, location_t type_location);
2534 static void cp_parser_check_for_invalid_template_id
2535 (cp_parser *, tree, enum tag_types, location_t location);
2536 static bool cp_parser_non_integral_constant_expression
2537 (cp_parser *, non_integral_constant);
2538 static void cp_parser_diagnose_invalid_type_name
2539 (cp_parser *, tree, location_t);
2540 static bool cp_parser_parse_and_diagnose_invalid_type_name
2541 (cp_parser *);
2542 static int cp_parser_skip_to_closing_parenthesis
2543 (cp_parser *, bool, bool, bool);
2544 static void cp_parser_skip_to_end_of_statement
2545 (cp_parser *);
2546 static void cp_parser_consume_semicolon_at_end_of_statement
2547 (cp_parser *);
2548 static void cp_parser_skip_to_end_of_block_or_statement
2549 (cp_parser *);
2550 static bool cp_parser_skip_to_closing_brace
2551 (cp_parser *);
2552 static void cp_parser_skip_to_end_of_template_parameter_list
2553 (cp_parser *);
2554 static void cp_parser_skip_to_pragma_eol
2555 (cp_parser*, cp_token *);
2556 static bool cp_parser_error_occurred
2557 (cp_parser *);
2558 static bool cp_parser_allow_gnu_extensions_p
2559 (cp_parser *);
2560 static bool cp_parser_is_pure_string_literal
2561 (cp_token *);
2562 static bool cp_parser_is_string_literal
2563 (cp_token *);
2564 static bool cp_parser_is_keyword
2565 (cp_token *, enum rid);
2566 static tree cp_parser_make_typename_type
2567 (cp_parser *, tree, location_t location);
2568 static cp_declarator * cp_parser_make_indirect_declarator
2569 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2570 static bool cp_parser_compound_literal_p
2571 (cp_parser *);
2572 static bool cp_parser_array_designator_p
2573 (cp_parser *);
2574 static bool cp_parser_skip_to_closing_square_bracket
2575 (cp_parser *);
2577 /* Concept-related syntactic transformations */
2579 static tree cp_maybe_concept_name (cp_parser *, tree);
2580 static bool cp_maybe_partial_concept_id (cp_parser *, tree, tree, tree*);
2582 // -------------------------------------------------------------------------- //
2583 // Unevaluated Operand Guard
2585 // Implementation of an RAII helper for unevaluated operand parsing.
2586 cp_unevaluated::cp_unevaluated ()
2588 ++cp_unevaluated_operand;
2589 ++c_inhibit_evaluation_warnings;
2592 cp_unevaluated::~cp_unevaluated ()
2594 --c_inhibit_evaluation_warnings;
2595 --cp_unevaluated_operand;
2598 // -------------------------------------------------------------------------- //
2599 // Tentative Parsing
2601 /* Returns nonzero if we are parsing tentatively. */
2603 static inline bool
2604 cp_parser_parsing_tentatively (cp_parser* parser)
2606 return parser->context->next != NULL;
2609 /* Returns nonzero if TOKEN is a string literal. */
2611 static bool
2612 cp_parser_is_pure_string_literal (cp_token* token)
2614 return (token->type == CPP_STRING ||
2615 token->type == CPP_STRING16 ||
2616 token->type == CPP_STRING32 ||
2617 token->type == CPP_WSTRING ||
2618 token->type == CPP_UTF8STRING);
2621 /* Returns nonzero if TOKEN is a string literal
2622 of a user-defined string literal. */
2624 static bool
2625 cp_parser_is_string_literal (cp_token* token)
2627 return (cp_parser_is_pure_string_literal (token) ||
2628 token->type == CPP_STRING_USERDEF ||
2629 token->type == CPP_STRING16_USERDEF ||
2630 token->type == CPP_STRING32_USERDEF ||
2631 token->type == CPP_WSTRING_USERDEF ||
2632 token->type == CPP_UTF8STRING_USERDEF);
2635 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2637 static bool
2638 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2640 return token->keyword == keyword;
2643 /* If not parsing tentatively, issue a diagnostic of the form
2644 FILE:LINE: MESSAGE before TOKEN
2645 where TOKEN is the next token in the input stream. MESSAGE
2646 (specified by the caller) is usually of the form "expected
2647 OTHER-TOKEN". */
2649 static void
2650 cp_parser_error (cp_parser* parser, const char* gmsgid)
2652 if (!cp_parser_simulate_error (parser))
2654 cp_token *token = cp_lexer_peek_token (parser->lexer);
2655 /* This diagnostic makes more sense if it is tagged to the line
2656 of the token we just peeked at. */
2657 cp_lexer_set_source_position_from_token (token);
2659 if (token->type == CPP_PRAGMA)
2661 error_at (token->location,
2662 "%<#pragma%> is not allowed here");
2663 cp_parser_skip_to_pragma_eol (parser, token);
2664 return;
2667 c_parse_error (gmsgid,
2668 /* Because c_parser_error does not understand
2669 CPP_KEYWORD, keywords are treated like
2670 identifiers. */
2671 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2672 token->u.value, token->flags);
2676 /* Issue an error about name-lookup failing. NAME is the
2677 IDENTIFIER_NODE DECL is the result of
2678 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2679 the thing that we hoped to find. */
2681 static void
2682 cp_parser_name_lookup_error (cp_parser* parser,
2683 tree name,
2684 tree decl,
2685 name_lookup_error desired,
2686 location_t location)
2688 /* If name lookup completely failed, tell the user that NAME was not
2689 declared. */
2690 if (decl == error_mark_node)
2692 if (parser->scope && parser->scope != global_namespace)
2693 error_at (location, "%<%E::%E%> has not been declared",
2694 parser->scope, name);
2695 else if (parser->scope == global_namespace)
2696 error_at (location, "%<::%E%> has not been declared", name);
2697 else if (parser->object_scope
2698 && !CLASS_TYPE_P (parser->object_scope))
2699 error_at (location, "request for member %qE in non-class type %qT",
2700 name, parser->object_scope);
2701 else if (parser->object_scope)
2702 error_at (location, "%<%T::%E%> has not been declared",
2703 parser->object_scope, name);
2704 else
2705 error_at (location, "%qE has not been declared", name);
2707 else if (parser->scope && parser->scope != global_namespace)
2709 switch (desired)
2711 case NLE_TYPE:
2712 error_at (location, "%<%E::%E%> is not a type",
2713 parser->scope, name);
2714 break;
2715 case NLE_CXX98:
2716 error_at (location, "%<%E::%E%> is not a class or namespace",
2717 parser->scope, name);
2718 break;
2719 case NLE_NOT_CXX98:
2720 error_at (location,
2721 "%<%E::%E%> is not a class, namespace, or enumeration",
2722 parser->scope, name);
2723 break;
2724 default:
2725 gcc_unreachable ();
2729 else if (parser->scope == global_namespace)
2731 switch (desired)
2733 case NLE_TYPE:
2734 error_at (location, "%<::%E%> is not a type", name);
2735 break;
2736 case NLE_CXX98:
2737 error_at (location, "%<::%E%> is not a class or namespace", name);
2738 break;
2739 case NLE_NOT_CXX98:
2740 error_at (location,
2741 "%<::%E%> is not a class, namespace, or enumeration",
2742 name);
2743 break;
2744 default:
2745 gcc_unreachable ();
2748 else
2750 switch (desired)
2752 case NLE_TYPE:
2753 error_at (location, "%qE is not a type", name);
2754 break;
2755 case NLE_CXX98:
2756 error_at (location, "%qE is not a class or namespace", name);
2757 break;
2758 case NLE_NOT_CXX98:
2759 error_at (location,
2760 "%qE is not a class, namespace, or enumeration", name);
2761 break;
2762 default:
2763 gcc_unreachable ();
2768 /* If we are parsing tentatively, remember that an error has occurred
2769 during this tentative parse. Returns true if the error was
2770 simulated; false if a message should be issued by the caller. */
2772 static bool
2773 cp_parser_simulate_error (cp_parser* parser)
2775 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2777 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2778 return true;
2780 return false;
2783 /* This function is called when a type is defined. If type
2784 definitions are forbidden at this point, an error message is
2785 issued. */
2787 static bool
2788 cp_parser_check_type_definition (cp_parser* parser)
2790 /* If types are forbidden here, issue a message. */
2791 if (parser->type_definition_forbidden_message)
2793 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2794 in the message need to be interpreted. */
2795 error (parser->type_definition_forbidden_message);
2796 return false;
2798 return true;
2801 /* This function is called when the DECLARATOR is processed. The TYPE
2802 was a type defined in the decl-specifiers. If it is invalid to
2803 define a type in the decl-specifiers for DECLARATOR, an error is
2804 issued. TYPE_LOCATION is the location of TYPE and is used
2805 for error reporting. */
2807 static void
2808 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2809 tree type, location_t type_location)
2811 /* [dcl.fct] forbids type definitions in return types.
2812 Unfortunately, it's not easy to know whether or not we are
2813 processing a return type until after the fact. */
2814 while (declarator
2815 && (declarator->kind == cdk_pointer
2816 || declarator->kind == cdk_reference
2817 || declarator->kind == cdk_ptrmem))
2818 declarator = declarator->declarator;
2819 if (declarator
2820 && declarator->kind == cdk_function)
2822 error_at (type_location,
2823 "new types may not be defined in a return type");
2824 inform (type_location,
2825 "(perhaps a semicolon is missing after the definition of %qT)",
2826 type);
2830 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2831 "<" in any valid C++ program. If the next token is indeed "<",
2832 issue a message warning the user about what appears to be an
2833 invalid attempt to form a template-id. LOCATION is the location
2834 of the type-specifier (TYPE) */
2836 static void
2837 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2838 tree type,
2839 enum tag_types tag_type,
2840 location_t location)
2842 cp_token_position start = 0;
2844 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2846 if (TYPE_P (type))
2847 error_at (location, "%qT is not a template", type);
2848 else if (identifier_p (type))
2850 if (tag_type != none_type)
2851 error_at (location, "%qE is not a class template", type);
2852 else
2853 error_at (location, "%qE is not a template", type);
2855 else
2856 error_at (location, "invalid template-id");
2857 /* Remember the location of the invalid "<". */
2858 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2859 start = cp_lexer_token_position (parser->lexer, true);
2860 /* Consume the "<". */
2861 cp_lexer_consume_token (parser->lexer);
2862 /* Parse the template arguments. */
2863 cp_parser_enclosed_template_argument_list (parser);
2864 /* Permanently remove the invalid template arguments so that
2865 this error message is not issued again. */
2866 if (start)
2867 cp_lexer_purge_tokens_after (parser->lexer, start);
2871 /* If parsing an integral constant-expression, issue an error message
2872 about the fact that THING appeared and return true. Otherwise,
2873 return false. In either case, set
2874 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2876 static bool
2877 cp_parser_non_integral_constant_expression (cp_parser *parser,
2878 non_integral_constant thing)
2880 parser->non_integral_constant_expression_p = true;
2881 if (parser->integral_constant_expression_p)
2883 if (!parser->allow_non_integral_constant_expression_p)
2885 const char *msg = NULL;
2886 switch (thing)
2888 case NIC_FLOAT:
2889 error ("floating-point literal "
2890 "cannot appear in a constant-expression");
2891 return true;
2892 case NIC_CAST:
2893 error ("a cast to a type other than an integral or "
2894 "enumeration type cannot appear in a "
2895 "constant-expression");
2896 return true;
2897 case NIC_TYPEID:
2898 error ("%<typeid%> operator "
2899 "cannot appear in a constant-expression");
2900 return true;
2901 case NIC_NCC:
2902 error ("non-constant compound literals "
2903 "cannot appear in a constant-expression");
2904 return true;
2905 case NIC_FUNC_CALL:
2906 error ("a function call "
2907 "cannot appear in a constant-expression");
2908 return true;
2909 case NIC_INC:
2910 error ("an increment "
2911 "cannot appear in a constant-expression");
2912 return true;
2913 case NIC_DEC:
2914 error ("an decrement "
2915 "cannot appear in a constant-expression");
2916 return true;
2917 case NIC_ARRAY_REF:
2918 error ("an array reference "
2919 "cannot appear in a constant-expression");
2920 return true;
2921 case NIC_ADDR_LABEL:
2922 error ("the address of a label "
2923 "cannot appear in a constant-expression");
2924 return true;
2925 case NIC_OVERLOADED:
2926 error ("calls to overloaded operators "
2927 "cannot appear in a constant-expression");
2928 return true;
2929 case NIC_ASSIGNMENT:
2930 error ("an assignment cannot appear in a constant-expression");
2931 return true;
2932 case NIC_COMMA:
2933 error ("a comma operator "
2934 "cannot appear in a constant-expression");
2935 return true;
2936 case NIC_CONSTRUCTOR:
2937 error ("a call to a constructor "
2938 "cannot appear in a constant-expression");
2939 return true;
2940 case NIC_TRANSACTION:
2941 error ("a transaction expression "
2942 "cannot appear in a constant-expression");
2943 return true;
2944 case NIC_THIS:
2945 msg = "this";
2946 break;
2947 case NIC_FUNC_NAME:
2948 msg = "__FUNCTION__";
2949 break;
2950 case NIC_PRETTY_FUNC:
2951 msg = "__PRETTY_FUNCTION__";
2952 break;
2953 case NIC_C99_FUNC:
2954 msg = "__func__";
2955 break;
2956 case NIC_VA_ARG:
2957 msg = "va_arg";
2958 break;
2959 case NIC_ARROW:
2960 msg = "->";
2961 break;
2962 case NIC_POINT:
2963 msg = ".";
2964 break;
2965 case NIC_STAR:
2966 msg = "*";
2967 break;
2968 case NIC_ADDR:
2969 msg = "&";
2970 break;
2971 case NIC_PREINCREMENT:
2972 msg = "++";
2973 break;
2974 case NIC_PREDECREMENT:
2975 msg = "--";
2976 break;
2977 case NIC_NEW:
2978 msg = "new";
2979 break;
2980 case NIC_DEL:
2981 msg = "delete";
2982 break;
2983 default:
2984 gcc_unreachable ();
2986 if (msg)
2987 error ("%qs cannot appear in a constant-expression", msg);
2988 return true;
2991 return false;
2994 /* Emit a diagnostic for an invalid type name. This function commits
2995 to the current active tentative parse, if any. (Otherwise, the
2996 problematic construct might be encountered again later, resulting
2997 in duplicate error messages.) LOCATION is the location of ID. */
2999 static void
3000 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3001 location_t location)
3003 tree decl, ambiguous_decls;
3004 cp_parser_commit_to_tentative_parse (parser);
3005 /* Try to lookup the identifier. */
3006 decl = cp_parser_lookup_name (parser, id, none_type,
3007 /*is_template=*/false,
3008 /*is_namespace=*/false,
3009 /*check_dependency=*/true,
3010 &ambiguous_decls, location);
3011 if (ambiguous_decls)
3012 /* If the lookup was ambiguous, an error will already have
3013 been issued. */
3014 return;
3015 /* If the lookup found a template-name, it means that the user forgot
3016 to specify an argument list. Emit a useful error message. */
3017 if (TREE_CODE (decl) == TEMPLATE_DECL)
3018 error_at (location,
3019 "invalid use of template-name %qE without an argument list",
3020 decl);
3021 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3022 error_at (location, "invalid use of destructor %qD as a type", id);
3023 else if (TREE_CODE (decl) == TYPE_DECL)
3024 /* Something like 'unsigned A a;' */
3025 error_at (location, "invalid combination of multiple type-specifiers");
3026 else if (!parser->scope)
3028 /* Issue an error message. */
3029 error_at (location, "%qE does not name a type", id);
3030 /* If we're in a template class, it's possible that the user was
3031 referring to a type from a base class. For example:
3033 template <typename T> struct A { typedef T X; };
3034 template <typename T> struct B : public A<T> { X x; };
3036 The user should have said "typename A<T>::X". */
3037 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3038 inform (location, "C++11 %<constexpr%> only available with "
3039 "-std=c++11 or -std=gnu++11");
3040 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3041 inform (location, "C++11 %<noexcept%> only available with "
3042 "-std=c++11 or -std=gnu++11");
3043 else if (cxx_dialect < cxx11
3044 && TREE_CODE (id) == IDENTIFIER_NODE
3045 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3046 inform (location, "C++11 %<thread_local%> only available with "
3047 "-std=c++11 or -std=gnu++11");
3048 else if (processing_template_decl && current_class_type
3049 && TYPE_BINFO (current_class_type))
3051 tree b;
3053 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3055 b = TREE_CHAIN (b))
3057 tree base_type = BINFO_TYPE (b);
3058 if (CLASS_TYPE_P (base_type)
3059 && dependent_type_p (base_type))
3061 tree field;
3062 /* Go from a particular instantiation of the
3063 template (which will have an empty TYPE_FIELDs),
3064 to the main version. */
3065 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3066 for (field = TYPE_FIELDS (base_type);
3067 field;
3068 field = DECL_CHAIN (field))
3069 if (TREE_CODE (field) == TYPE_DECL
3070 && DECL_NAME (field) == id)
3072 inform (location,
3073 "(perhaps %<typename %T::%E%> was intended)",
3074 BINFO_TYPE (b), id);
3075 break;
3077 if (field)
3078 break;
3083 /* Here we diagnose qualified-ids where the scope is actually correct,
3084 but the identifier does not resolve to a valid type name. */
3085 else if (parser->scope != error_mark_node)
3087 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3089 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3090 error_at (location_of (id),
3091 "%qE in namespace %qE does not name a template type",
3092 id, parser->scope);
3093 else
3094 error_at (location_of (id),
3095 "%qE in namespace %qE does not name a type",
3096 id, parser->scope);
3098 else if (CLASS_TYPE_P (parser->scope)
3099 && constructor_name_p (id, parser->scope))
3101 /* A<T>::A<T>() */
3102 error_at (location, "%<%T::%E%> names the constructor, not"
3103 " the type", parser->scope, id);
3104 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3105 error_at (location, "and %qT has no template constructors",
3106 parser->scope);
3108 else if (TYPE_P (parser->scope)
3109 && dependent_scope_p (parser->scope))
3110 error_at (location, "need %<typename%> before %<%T::%E%> because "
3111 "%qT is a dependent scope",
3112 parser->scope, id, parser->scope);
3113 else if (TYPE_P (parser->scope))
3115 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3116 error_at (location_of (id),
3117 "%qE in %q#T does not name a template type",
3118 id, parser->scope);
3119 else
3120 error_at (location_of (id),
3121 "%qE in %q#T does not name a type",
3122 id, parser->scope);
3124 else
3125 gcc_unreachable ();
3129 /* Check for a common situation where a type-name should be present,
3130 but is not, and issue a sensible error message. Returns true if an
3131 invalid type-name was detected.
3133 The situation handled by this function are variable declarations of the
3134 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3135 Usually, `ID' should name a type, but if we got here it means that it
3136 does not. We try to emit the best possible error message depending on
3137 how exactly the id-expression looks like. */
3139 static bool
3140 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3142 tree id;
3143 cp_token *token = cp_lexer_peek_token (parser->lexer);
3145 /* Avoid duplicate error about ambiguous lookup. */
3146 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3148 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3149 if (next->type == CPP_NAME && next->error_reported)
3150 goto out;
3153 cp_parser_parse_tentatively (parser);
3154 id = cp_parser_id_expression (parser,
3155 /*template_keyword_p=*/false,
3156 /*check_dependency_p=*/true,
3157 /*template_p=*/NULL,
3158 /*declarator_p=*/true,
3159 /*optional_p=*/false);
3160 /* If the next token is a (, this is a function with no explicit return
3161 type, i.e. constructor, destructor or conversion op. */
3162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3163 || TREE_CODE (id) == TYPE_DECL)
3165 cp_parser_abort_tentative_parse (parser);
3166 return false;
3168 if (!cp_parser_parse_definitely (parser))
3169 return false;
3171 /* Emit a diagnostic for the invalid type. */
3172 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3173 out:
3174 /* If we aren't in the middle of a declarator (i.e. in a
3175 parameter-declaration-clause), skip to the end of the declaration;
3176 there's no point in trying to process it. */
3177 if (!parser->in_declarator_p)
3178 cp_parser_skip_to_end_of_block_or_statement (parser);
3179 return true;
3182 /* Consume tokens up to, and including, the next non-nested closing `)'.
3183 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3184 are doing error recovery. Returns -1 if OR_COMMA is true and we
3185 found an unnested comma. */
3187 static int
3188 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3189 bool recovering,
3190 bool or_comma,
3191 bool consume_paren)
3193 unsigned paren_depth = 0;
3194 unsigned brace_depth = 0;
3195 unsigned square_depth = 0;
3197 if (recovering && !or_comma
3198 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3199 return 0;
3201 while (true)
3203 cp_token * token = cp_lexer_peek_token (parser->lexer);
3205 switch (token->type)
3207 case CPP_EOF:
3208 case CPP_PRAGMA_EOL:
3209 /* If we've run out of tokens, then there is no closing `)'. */
3210 return 0;
3212 /* This is good for lambda expression capture-lists. */
3213 case CPP_OPEN_SQUARE:
3214 ++square_depth;
3215 break;
3216 case CPP_CLOSE_SQUARE:
3217 if (!square_depth--)
3218 return 0;
3219 break;
3221 case CPP_SEMICOLON:
3222 /* This matches the processing in skip_to_end_of_statement. */
3223 if (!brace_depth)
3224 return 0;
3225 break;
3227 case CPP_OPEN_BRACE:
3228 ++brace_depth;
3229 break;
3230 case CPP_CLOSE_BRACE:
3231 if (!brace_depth--)
3232 return 0;
3233 break;
3235 case CPP_COMMA:
3236 if (recovering && or_comma && !brace_depth && !paren_depth
3237 && !square_depth)
3238 return -1;
3239 break;
3241 case CPP_OPEN_PAREN:
3242 if (!brace_depth)
3243 ++paren_depth;
3244 break;
3246 case CPP_CLOSE_PAREN:
3247 if (!brace_depth && !paren_depth--)
3249 if (consume_paren)
3250 cp_lexer_consume_token (parser->lexer);
3251 return 1;
3253 break;
3255 default:
3256 break;
3259 /* Consume the token. */
3260 cp_lexer_consume_token (parser->lexer);
3264 /* Consume tokens until we reach the end of the current statement.
3265 Normally, that will be just before consuming a `;'. However, if a
3266 non-nested `}' comes first, then we stop before consuming that. */
3268 static void
3269 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3271 unsigned nesting_depth = 0;
3273 /* Unwind generic function template scope if necessary. */
3274 if (parser->fully_implicit_function_template_p)
3275 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3277 while (true)
3279 cp_token *token = cp_lexer_peek_token (parser->lexer);
3281 switch (token->type)
3283 case CPP_EOF:
3284 case CPP_PRAGMA_EOL:
3285 /* If we've run out of tokens, stop. */
3286 return;
3288 case CPP_SEMICOLON:
3289 /* If the next token is a `;', we have reached the end of the
3290 statement. */
3291 if (!nesting_depth)
3292 return;
3293 break;
3295 case CPP_CLOSE_BRACE:
3296 /* If this is a non-nested '}', stop before consuming it.
3297 That way, when confronted with something like:
3299 { 3 + }
3301 we stop before consuming the closing '}', even though we
3302 have not yet reached a `;'. */
3303 if (nesting_depth == 0)
3304 return;
3306 /* If it is the closing '}' for a block that we have
3307 scanned, stop -- but only after consuming the token.
3308 That way given:
3310 void f g () { ... }
3311 typedef int I;
3313 we will stop after the body of the erroneously declared
3314 function, but before consuming the following `typedef'
3315 declaration. */
3316 if (--nesting_depth == 0)
3318 cp_lexer_consume_token (parser->lexer);
3319 return;
3322 case CPP_OPEN_BRACE:
3323 ++nesting_depth;
3324 break;
3326 default:
3327 break;
3330 /* Consume the token. */
3331 cp_lexer_consume_token (parser->lexer);
3335 /* This function is called at the end of a statement or declaration.
3336 If the next token is a semicolon, it is consumed; otherwise, error
3337 recovery is attempted. */
3339 static void
3340 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3342 /* Look for the trailing `;'. */
3343 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3345 /* If there is additional (erroneous) input, skip to the end of
3346 the statement. */
3347 cp_parser_skip_to_end_of_statement (parser);
3348 /* If the next token is now a `;', consume it. */
3349 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3350 cp_lexer_consume_token (parser->lexer);
3354 /* Skip tokens until we have consumed an entire block, or until we
3355 have consumed a non-nested `;'. */
3357 static void
3358 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3360 int nesting_depth = 0;
3362 /* Unwind generic function template scope if necessary. */
3363 if (parser->fully_implicit_function_template_p)
3364 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3366 while (nesting_depth >= 0)
3368 cp_token *token = cp_lexer_peek_token (parser->lexer);
3370 switch (token->type)
3372 case CPP_EOF:
3373 case CPP_PRAGMA_EOL:
3374 /* If we've run out of tokens, stop. */
3375 return;
3377 case CPP_SEMICOLON:
3378 /* Stop if this is an unnested ';'. */
3379 if (!nesting_depth)
3380 nesting_depth = -1;
3381 break;
3383 case CPP_CLOSE_BRACE:
3384 /* Stop if this is an unnested '}', or closes the outermost
3385 nesting level. */
3386 nesting_depth--;
3387 if (nesting_depth < 0)
3388 return;
3389 if (!nesting_depth)
3390 nesting_depth = -1;
3391 break;
3393 case CPP_OPEN_BRACE:
3394 /* Nest. */
3395 nesting_depth++;
3396 break;
3398 default:
3399 break;
3402 /* Consume the token. */
3403 cp_lexer_consume_token (parser->lexer);
3407 /* Skip tokens until a non-nested closing curly brace is the next
3408 token, or there are no more tokens. Return true in the first case,
3409 false otherwise. */
3411 static bool
3412 cp_parser_skip_to_closing_brace (cp_parser *parser)
3414 unsigned nesting_depth = 0;
3416 while (true)
3418 cp_token *token = cp_lexer_peek_token (parser->lexer);
3420 switch (token->type)
3422 case CPP_EOF:
3423 case CPP_PRAGMA_EOL:
3424 /* If we've run out of tokens, stop. */
3425 return false;
3427 case CPP_CLOSE_BRACE:
3428 /* If the next token is a non-nested `}', then we have reached
3429 the end of the current block. */
3430 if (nesting_depth-- == 0)
3431 return true;
3432 break;
3434 case CPP_OPEN_BRACE:
3435 /* If it the next token is a `{', then we are entering a new
3436 block. Consume the entire block. */
3437 ++nesting_depth;
3438 break;
3440 default:
3441 break;
3444 /* Consume the token. */
3445 cp_lexer_consume_token (parser->lexer);
3449 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3450 parameter is the PRAGMA token, allowing us to purge the entire pragma
3451 sequence. */
3453 static void
3454 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3456 cp_token *token;
3458 parser->lexer->in_pragma = false;
3461 token = cp_lexer_consume_token (parser->lexer);
3462 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3464 /* Ensure that the pragma is not parsed again. */
3465 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3468 /* Require pragma end of line, resyncing with it as necessary. The
3469 arguments are as for cp_parser_skip_to_pragma_eol. */
3471 static void
3472 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3474 parser->lexer->in_pragma = false;
3475 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3476 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3479 /* This is a simple wrapper around make_typename_type. When the id is
3480 an unresolved identifier node, we can provide a superior diagnostic
3481 using cp_parser_diagnose_invalid_type_name. */
3483 static tree
3484 cp_parser_make_typename_type (cp_parser *parser, tree id,
3485 location_t id_location)
3487 tree result;
3488 if (identifier_p (id))
3490 result = make_typename_type (parser->scope, id, typename_type,
3491 /*complain=*/tf_none);
3492 if (result == error_mark_node)
3493 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3494 return result;
3496 return make_typename_type (parser->scope, id, typename_type, tf_error);
3499 /* This is a wrapper around the
3500 make_{pointer,ptrmem,reference}_declarator functions that decides
3501 which one to call based on the CODE and CLASS_TYPE arguments. The
3502 CODE argument should be one of the values returned by
3503 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3504 appertain to the pointer or reference. */
3506 static cp_declarator *
3507 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3508 cp_cv_quals cv_qualifiers,
3509 cp_declarator *target,
3510 tree attributes)
3512 if (code == ERROR_MARK)
3513 return cp_error_declarator;
3515 if (code == INDIRECT_REF)
3516 if (class_type == NULL_TREE)
3517 return make_pointer_declarator (cv_qualifiers, target, attributes);
3518 else
3519 return make_ptrmem_declarator (cv_qualifiers, class_type,
3520 target, attributes);
3521 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3522 return make_reference_declarator (cv_qualifiers, target,
3523 false, attributes);
3524 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3525 return make_reference_declarator (cv_qualifiers, target,
3526 true, attributes);
3527 gcc_unreachable ();
3530 /* Create a new C++ parser. */
3532 static cp_parser *
3533 cp_parser_new (void)
3535 cp_parser *parser;
3536 cp_lexer *lexer;
3537 unsigned i;
3539 /* cp_lexer_new_main is called before doing GC allocation because
3540 cp_lexer_new_main might load a PCH file. */
3541 lexer = cp_lexer_new_main ();
3543 /* Initialize the binops_by_token so that we can get the tree
3544 directly from the token. */
3545 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3546 binops_by_token[binops[i].token_type] = binops[i];
3548 parser = ggc_cleared_alloc<cp_parser> ();
3549 parser->lexer = lexer;
3550 parser->context = cp_parser_context_new (NULL);
3552 /* For now, we always accept GNU extensions. */
3553 parser->allow_gnu_extensions_p = 1;
3555 /* The `>' token is a greater-than operator, not the end of a
3556 template-id. */
3557 parser->greater_than_is_operator_p = true;
3559 parser->default_arg_ok_p = true;
3561 /* We are not parsing a constant-expression. */
3562 parser->integral_constant_expression_p = false;
3563 parser->allow_non_integral_constant_expression_p = false;
3564 parser->non_integral_constant_expression_p = false;
3566 /* Local variable names are not forbidden. */
3567 parser->local_variables_forbidden_p = false;
3569 /* We are not processing an `extern "C"' declaration. */
3570 parser->in_unbraced_linkage_specification_p = false;
3572 /* We are not processing a declarator. */
3573 parser->in_declarator_p = false;
3575 /* We are not processing a template-argument-list. */
3576 parser->in_template_argument_list_p = false;
3578 /* We are not in an iteration statement. */
3579 parser->in_statement = 0;
3581 /* We are not in a switch statement. */
3582 parser->in_switch_statement_p = false;
3584 /* We are not parsing a type-id inside an expression. */
3585 parser->in_type_id_in_expr_p = false;
3587 /* Declarations aren't implicitly extern "C". */
3588 parser->implicit_extern_c = false;
3590 /* String literals should be translated to the execution character set. */
3591 parser->translate_strings_p = true;
3593 /* We are not parsing a function body. */
3594 parser->in_function_body = false;
3596 /* We can correct until told otherwise. */
3597 parser->colon_corrects_to_scope_p = true;
3599 /* The unparsed function queue is empty. */
3600 push_unparsed_function_queues (parser);
3602 /* There are no classes being defined. */
3603 parser->num_classes_being_defined = 0;
3605 /* No template parameters apply. */
3606 parser->num_template_parameter_lists = 0;
3608 /* Not declaring an implicit function template. */
3609 parser->auto_is_implicit_function_template_parm_p = false;
3610 parser->fully_implicit_function_template_p = false;
3611 parser->implicit_template_parms = 0;
3612 parser->implicit_template_scope = 0;
3614 /* Allow constrained-type-specifiers. */
3615 parser->prevent_constrained_type_specifiers = 0;
3617 return parser;
3620 /* Create a cp_lexer structure which will emit the tokens in CACHE
3621 and push it onto the parser's lexer stack. This is used for delayed
3622 parsing of in-class method bodies and default arguments, and should
3623 not be confused with tentative parsing. */
3624 static void
3625 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3627 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3628 lexer->next = parser->lexer;
3629 parser->lexer = lexer;
3631 /* Move the current source position to that of the first token in the
3632 new lexer. */
3633 cp_lexer_set_source_position_from_token (lexer->next_token);
3636 /* Pop the top lexer off the parser stack. This is never used for the
3637 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3638 static void
3639 cp_parser_pop_lexer (cp_parser *parser)
3641 cp_lexer *lexer = parser->lexer;
3642 parser->lexer = lexer->next;
3643 cp_lexer_destroy (lexer);
3645 /* Put the current source position back where it was before this
3646 lexer was pushed. */
3647 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3650 /* Lexical conventions [gram.lex] */
3652 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3653 identifier. */
3655 static tree
3656 cp_parser_identifier (cp_parser* parser)
3658 cp_token *token;
3660 /* Look for the identifier. */
3661 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3662 /* Return the value. */
3663 return token ? token->u.value : error_mark_node;
3666 /* Parse a sequence of adjacent string constants. Returns a
3667 TREE_STRING representing the combined, nul-terminated string
3668 constant. If TRANSLATE is true, translate the string to the
3669 execution character set. If WIDE_OK is true, a wide string is
3670 invalid here.
3672 C++98 [lex.string] says that if a narrow string literal token is
3673 adjacent to a wide string literal token, the behavior is undefined.
3674 However, C99 6.4.5p4 says that this results in a wide string literal.
3675 We follow C99 here, for consistency with the C front end.
3677 This code is largely lifted from lex_string() in c-lex.c.
3679 FUTURE: ObjC++ will need to handle @-strings here. */
3680 static tree
3681 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3682 bool lookup_udlit = true)
3684 tree value;
3685 size_t count;
3686 struct obstack str_ob;
3687 cpp_string str, istr, *strs;
3688 cp_token *tok;
3689 enum cpp_ttype type, curr_type;
3690 int have_suffix_p = 0;
3691 tree string_tree;
3692 tree suffix_id = NULL_TREE;
3693 bool curr_tok_is_userdef_p = false;
3695 tok = cp_lexer_peek_token (parser->lexer);
3696 if (!cp_parser_is_string_literal (tok))
3698 cp_parser_error (parser, "expected string-literal");
3699 return error_mark_node;
3702 if (cpp_userdef_string_p (tok->type))
3704 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3705 curr_type = cpp_userdef_string_remove_type (tok->type);
3706 curr_tok_is_userdef_p = true;
3708 else
3710 string_tree = tok->u.value;
3711 curr_type = tok->type;
3713 type = curr_type;
3715 /* Try to avoid the overhead of creating and destroying an obstack
3716 for the common case of just one string. */
3717 if (!cp_parser_is_string_literal
3718 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3720 cp_lexer_consume_token (parser->lexer);
3722 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3723 str.len = TREE_STRING_LENGTH (string_tree);
3724 count = 1;
3726 if (curr_tok_is_userdef_p)
3728 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3729 have_suffix_p = 1;
3730 curr_type = cpp_userdef_string_remove_type (tok->type);
3732 else
3733 curr_type = tok->type;
3735 strs = &str;
3737 else
3739 gcc_obstack_init (&str_ob);
3740 count = 0;
3744 cp_lexer_consume_token (parser->lexer);
3745 count++;
3746 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3747 str.len = TREE_STRING_LENGTH (string_tree);
3749 if (curr_tok_is_userdef_p)
3751 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3752 if (have_suffix_p == 0)
3754 suffix_id = curr_suffix_id;
3755 have_suffix_p = 1;
3757 else if (have_suffix_p == 1
3758 && curr_suffix_id != suffix_id)
3760 error ("inconsistent user-defined literal suffixes"
3761 " %qD and %qD in string literal",
3762 suffix_id, curr_suffix_id);
3763 have_suffix_p = -1;
3765 curr_type = cpp_userdef_string_remove_type (tok->type);
3767 else
3768 curr_type = tok->type;
3770 if (type != curr_type)
3772 if (type == CPP_STRING)
3773 type = curr_type;
3774 else if (curr_type != CPP_STRING)
3775 error_at (tok->location,
3776 "unsupported non-standard concatenation "
3777 "of string literals");
3780 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3782 tok = cp_lexer_peek_token (parser->lexer);
3783 if (cpp_userdef_string_p (tok->type))
3785 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3786 curr_type = cpp_userdef_string_remove_type (tok->type);
3787 curr_tok_is_userdef_p = true;
3789 else
3791 string_tree = tok->u.value;
3792 curr_type = tok->type;
3793 curr_tok_is_userdef_p = false;
3796 while (cp_parser_is_string_literal (tok));
3798 strs = (cpp_string *) obstack_finish (&str_ob);
3801 if (type != CPP_STRING && !wide_ok)
3803 cp_parser_error (parser, "a wide string is invalid in this context");
3804 type = CPP_STRING;
3807 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3808 (parse_in, strs, count, &istr, type))
3810 value = build_string (istr.len, (const char *)istr.text);
3811 free (CONST_CAST (unsigned char *, istr.text));
3813 switch (type)
3815 default:
3816 case CPP_STRING:
3817 case CPP_UTF8STRING:
3818 TREE_TYPE (value) = char_array_type_node;
3819 break;
3820 case CPP_STRING16:
3821 TREE_TYPE (value) = char16_array_type_node;
3822 break;
3823 case CPP_STRING32:
3824 TREE_TYPE (value) = char32_array_type_node;
3825 break;
3826 case CPP_WSTRING:
3827 TREE_TYPE (value) = wchar_array_type_node;
3828 break;
3831 value = fix_string_type (value);
3833 if (have_suffix_p)
3835 tree literal = build_userdef_literal (suffix_id, value,
3836 OT_NONE, NULL_TREE);
3837 if (lookup_udlit)
3838 value = cp_parser_userdef_string_literal (literal);
3839 else
3840 value = literal;
3843 else
3844 /* cpp_interpret_string has issued an error. */
3845 value = error_mark_node;
3847 if (count > 1)
3848 obstack_free (&str_ob, 0);
3850 return value;
3853 /* Look up a literal operator with the name and the exact arguments. */
3855 static tree
3856 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3858 tree decl, fns;
3859 decl = lookup_name (name);
3860 if (!decl || !is_overloaded_fn (decl))
3861 return error_mark_node;
3863 for (fns = decl; fns; fns = OVL_NEXT (fns))
3865 unsigned int ix;
3866 bool found = true;
3867 tree fn = OVL_CURRENT (fns);
3868 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3869 if (parmtypes != NULL_TREE)
3871 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3872 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3874 tree tparm = TREE_VALUE (parmtypes);
3875 tree targ = TREE_TYPE ((*args)[ix]);
3876 bool ptr = TYPE_PTR_P (tparm);
3877 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3878 if ((ptr || arr || !same_type_p (tparm, targ))
3879 && (!ptr || !arr
3880 || !same_type_p (TREE_TYPE (tparm),
3881 TREE_TYPE (targ))))
3882 found = false;
3884 if (found
3885 && ix == vec_safe_length (args)
3886 /* May be this should be sufficient_parms_p instead,
3887 depending on how exactly should user-defined literals
3888 work in presence of default arguments on the literal
3889 operator parameters. */
3890 && parmtypes == void_list_node)
3891 return decl;
3895 return error_mark_node;
3898 /* Parse a user-defined char constant. Returns a call to a user-defined
3899 literal operator taking the character as an argument. */
3901 static tree
3902 cp_parser_userdef_char_literal (cp_parser *parser)
3904 cp_token *token = cp_lexer_consume_token (parser->lexer);
3905 tree literal = token->u.value;
3906 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3907 tree value = USERDEF_LITERAL_VALUE (literal);
3908 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3909 tree decl, result;
3911 /* Build up a call to the user-defined operator */
3912 /* Lookup the name we got back from the id-expression. */
3913 vec<tree, va_gc> *args = make_tree_vector ();
3914 vec_safe_push (args, value);
3915 decl = lookup_literal_operator (name, args);
3916 if (!decl || decl == error_mark_node)
3918 error ("unable to find character literal operator %qD with %qT argument",
3919 name, TREE_TYPE (value));
3920 release_tree_vector (args);
3921 return error_mark_node;
3923 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3924 release_tree_vector (args);
3925 return result;
3928 /* A subroutine of cp_parser_userdef_numeric_literal to
3929 create a char... template parameter pack from a string node. */
3931 static tree
3932 make_char_string_pack (tree value)
3934 tree charvec;
3935 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3936 const char *str = TREE_STRING_POINTER (value);
3937 int i, len = TREE_STRING_LENGTH (value) - 1;
3938 tree argvec = make_tree_vec (1);
3940 /* Fill in CHARVEC with all of the parameters. */
3941 charvec = make_tree_vec (len);
3942 for (i = 0; i < len; ++i)
3943 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3945 /* Build the argument packs. */
3946 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3947 TREE_TYPE (argpack) = char_type_node;
3949 TREE_VEC_ELT (argvec, 0) = argpack;
3951 return argvec;
3954 /* A subroutine of cp_parser_userdef_numeric_literal to
3955 create a char... template parameter pack from a string node. */
3957 static tree
3958 make_string_pack (tree value)
3960 tree charvec;
3961 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3962 const unsigned char *str
3963 = (const unsigned char *) TREE_STRING_POINTER (value);
3964 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3965 int len = TREE_STRING_LENGTH (value) / sz - 1;
3966 tree argvec = make_tree_vec (2);
3968 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3969 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3971 /* First template parm is character type. */
3972 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3974 /* Fill in CHARVEC with all of the parameters. */
3975 charvec = make_tree_vec (len);
3976 for (int i = 0; i < len; ++i)
3977 TREE_VEC_ELT (charvec, i)
3978 = double_int_to_tree (str_char_type_node,
3979 double_int::from_buffer (str + i * sz, sz));
3981 /* Build the argument packs. */
3982 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3983 TREE_TYPE (argpack) = str_char_type_node;
3985 TREE_VEC_ELT (argvec, 1) = argpack;
3987 return argvec;
3990 /* Parse a user-defined numeric constant. returns a call to a user-defined
3991 literal operator. */
3993 static tree
3994 cp_parser_userdef_numeric_literal (cp_parser *parser)
3996 cp_token *token = cp_lexer_consume_token (parser->lexer);
3997 tree literal = token->u.value;
3998 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3999 tree value = USERDEF_LITERAL_VALUE (literal);
4000 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4001 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4002 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4003 tree decl, result;
4004 vec<tree, va_gc> *args;
4006 /* Look for a literal operator taking the exact type of numeric argument
4007 as the literal value. */
4008 args = make_tree_vector ();
4009 vec_safe_push (args, value);
4010 decl = lookup_literal_operator (name, args);
4011 if (decl && decl != error_mark_node)
4013 result = finish_call_expr (decl, &args, false, true,
4014 tf_warning_or_error);
4016 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4018 warning_at (token->location, OPT_Woverflow,
4019 "integer literal exceeds range of %qT type",
4020 long_long_unsigned_type_node);
4022 else
4024 if (overflow > 0)
4025 warning_at (token->location, OPT_Woverflow,
4026 "floating literal exceeds range of %qT type",
4027 long_double_type_node);
4028 else if (overflow < 0)
4029 warning_at (token->location, OPT_Woverflow,
4030 "floating literal truncated to zero");
4033 release_tree_vector (args);
4034 return result;
4036 release_tree_vector (args);
4038 /* If the numeric argument didn't work, look for a raw literal
4039 operator taking a const char* argument consisting of the number
4040 in string format. */
4041 args = make_tree_vector ();
4042 vec_safe_push (args, num_string);
4043 decl = lookup_literal_operator (name, args);
4044 if (decl && decl != error_mark_node)
4046 result = finish_call_expr (decl, &args, false, true,
4047 tf_warning_or_error);
4048 release_tree_vector (args);
4049 return result;
4051 release_tree_vector (args);
4053 /* If the raw literal didn't work, look for a non-type template
4054 function with parameter pack char.... Call the function with
4055 template parameter characters representing the number. */
4056 args = make_tree_vector ();
4057 decl = lookup_literal_operator (name, args);
4058 if (decl && decl != error_mark_node)
4060 tree tmpl_args = make_char_string_pack (num_string);
4061 decl = lookup_template_function (decl, tmpl_args);
4062 result = finish_call_expr (decl, &args, false, true,
4063 tf_warning_or_error);
4064 release_tree_vector (args);
4065 return result;
4068 release_tree_vector (args);
4070 error ("unable to find numeric literal operator %qD", name);
4071 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4072 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4073 "to enable more built-in suffixes");
4074 return error_mark_node;
4077 /* Parse a user-defined string constant. Returns a call to a user-defined
4078 literal operator taking a character pointer and the length of the string
4079 as arguments. */
4081 static tree
4082 cp_parser_userdef_string_literal (tree literal)
4084 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4085 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4086 tree value = USERDEF_LITERAL_VALUE (literal);
4087 int len = TREE_STRING_LENGTH (value)
4088 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4089 tree decl, result;
4090 vec<tree, va_gc> *args;
4092 /* Build up a call to the user-defined operator. */
4093 /* Lookup the name we got back from the id-expression. */
4094 args = make_tree_vector ();
4095 vec_safe_push (args, value);
4096 vec_safe_push (args, build_int_cst (size_type_node, len));
4097 decl = lookup_literal_operator (name, args);
4099 if (decl && decl != error_mark_node)
4101 result = finish_call_expr (decl, &args, false, true,
4102 tf_warning_or_error);
4103 release_tree_vector (args);
4104 return result;
4106 release_tree_vector (args);
4108 /* Look for a template function with typename parameter CharT
4109 and parameter pack CharT... Call the function with
4110 template parameter characters representing the string. */
4111 args = make_tree_vector ();
4112 decl = lookup_literal_operator (name, args);
4113 if (decl && decl != error_mark_node)
4115 tree tmpl_args = make_string_pack (value);
4116 decl = lookup_template_function (decl, tmpl_args);
4117 result = finish_call_expr (decl, &args, false, true,
4118 tf_warning_or_error);
4119 release_tree_vector (args);
4120 return result;
4122 release_tree_vector (args);
4124 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4125 name, TREE_TYPE (value), size_type_node);
4126 return error_mark_node;
4130 /* Basic concepts [gram.basic] */
4132 /* Parse a translation-unit.
4134 translation-unit:
4135 declaration-seq [opt]
4137 Returns TRUE if all went well. */
4139 static bool
4140 cp_parser_translation_unit (cp_parser* parser)
4142 /* The address of the first non-permanent object on the declarator
4143 obstack. */
4144 static void *declarator_obstack_base;
4146 bool success;
4148 /* Create the declarator obstack, if necessary. */
4149 if (!cp_error_declarator)
4151 gcc_obstack_init (&declarator_obstack);
4152 /* Create the error declarator. */
4153 cp_error_declarator = make_declarator (cdk_error);
4154 /* Create the empty parameter list. */
4155 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4156 /* Remember where the base of the declarator obstack lies. */
4157 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4160 cp_parser_declaration_seq_opt (parser);
4162 /* If there are no tokens left then all went well. */
4163 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4165 /* Get rid of the token array; we don't need it any more. */
4166 cp_lexer_destroy (parser->lexer);
4167 parser->lexer = NULL;
4169 /* This file might have been a context that's implicitly extern
4170 "C". If so, pop the lang context. (Only relevant for PCH.) */
4171 if (parser->implicit_extern_c)
4173 pop_lang_context ();
4174 parser->implicit_extern_c = false;
4177 /* Finish up. */
4178 finish_translation_unit ();
4180 success = true;
4182 else
4184 cp_parser_error (parser, "expected declaration");
4185 success = false;
4188 /* Make sure the declarator obstack was fully cleaned up. */
4189 gcc_assert (obstack_next_free (&declarator_obstack)
4190 == declarator_obstack_base);
4192 /* All went well. */
4193 return success;
4196 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4197 decltype context. */
4199 static inline tsubst_flags_t
4200 complain_flags (bool decltype_p)
4202 tsubst_flags_t complain = tf_warning_or_error;
4203 if (decltype_p)
4204 complain |= tf_decltype;
4205 return complain;
4208 /* We're about to parse a collection of statements. If we're currently
4209 parsing tentatively, set up a firewall so that any nested
4210 cp_parser_commit_to_tentative_parse won't affect the current context. */
4212 static cp_token_position
4213 cp_parser_start_tentative_firewall (cp_parser *parser)
4215 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4216 return 0;
4218 cp_parser_parse_tentatively (parser);
4219 cp_parser_commit_to_topmost_tentative_parse (parser);
4220 return cp_lexer_token_position (parser->lexer, false);
4223 /* We've finished parsing the collection of statements. Wrap up the
4224 firewall and replace the relevant tokens with the parsed form. */
4226 static void
4227 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4228 tree expr)
4230 if (!start)
4231 return;
4233 /* Finish the firewall level. */
4234 cp_parser_parse_definitely (parser);
4235 /* And remember the result of the parse for when we try again. */
4236 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4237 token->type = CPP_PREPARSED_EXPR;
4238 token->u.value = expr;
4239 token->keyword = RID_MAX;
4240 cp_lexer_purge_tokens_after (parser->lexer, start);
4243 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4244 enclosing parentheses. */
4246 static tree
4247 cp_parser_statement_expr (cp_parser *parser)
4249 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4251 /* Consume the '('. */
4252 cp_lexer_consume_token (parser->lexer);
4253 /* Start the statement-expression. */
4254 tree expr = begin_stmt_expr ();
4255 /* Parse the compound-statement. */
4256 cp_parser_compound_statement (parser, expr, false, false);
4257 /* Finish up. */
4258 expr = finish_stmt_expr (expr, false);
4259 /* Consume the ')'. */
4260 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4261 cp_parser_skip_to_end_of_statement (parser);
4263 cp_parser_end_tentative_firewall (parser, start, expr);
4264 return expr;
4267 /* Expressions [gram.expr] */
4269 /* Parse a primary-expression.
4271 primary-expression:
4272 literal
4273 this
4274 ( expression )
4275 id-expression
4276 lambda-expression (C++11)
4278 GNU Extensions:
4280 primary-expression:
4281 ( compound-statement )
4282 __builtin_va_arg ( assignment-expression , type-id )
4283 __builtin_offsetof ( type-id , offsetof-expression )
4285 C++ Extensions:
4286 __has_nothrow_assign ( type-id )
4287 __has_nothrow_constructor ( type-id )
4288 __has_nothrow_copy ( type-id )
4289 __has_trivial_assign ( type-id )
4290 __has_trivial_constructor ( type-id )
4291 __has_trivial_copy ( type-id )
4292 __has_trivial_destructor ( type-id )
4293 __has_virtual_destructor ( type-id )
4294 __is_abstract ( type-id )
4295 __is_base_of ( type-id , type-id )
4296 __is_class ( type-id )
4297 __is_empty ( type-id )
4298 __is_enum ( type-id )
4299 __is_final ( type-id )
4300 __is_literal_type ( type-id )
4301 __is_pod ( type-id )
4302 __is_polymorphic ( type-id )
4303 __is_std_layout ( type-id )
4304 __is_trivial ( type-id )
4305 __is_union ( type-id )
4307 Objective-C++ Extension:
4309 primary-expression:
4310 objc-expression
4312 literal:
4313 __null
4315 ADDRESS_P is true iff this expression was immediately preceded by
4316 "&" and therefore might denote a pointer-to-member. CAST_P is true
4317 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4318 true iff this expression is a template argument.
4320 Returns a representation of the expression. Upon return, *IDK
4321 indicates what kind of id-expression (if any) was present. */
4323 static tree
4324 cp_parser_primary_expression (cp_parser *parser,
4325 bool address_p,
4326 bool cast_p,
4327 bool template_arg_p,
4328 bool decltype_p,
4329 cp_id_kind *idk)
4331 cp_token *token = NULL;
4333 /* Assume the primary expression is not an id-expression. */
4334 *idk = CP_ID_KIND_NONE;
4336 /* Peek at the next token. */
4337 token = cp_lexer_peek_token (parser->lexer);
4338 switch ((int) token->type)
4340 /* literal:
4341 integer-literal
4342 character-literal
4343 floating-literal
4344 string-literal
4345 boolean-literal
4346 pointer-literal
4347 user-defined-literal */
4348 case CPP_CHAR:
4349 case CPP_CHAR16:
4350 case CPP_CHAR32:
4351 case CPP_WCHAR:
4352 case CPP_NUMBER:
4353 case CPP_PREPARSED_EXPR:
4354 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4355 return cp_parser_userdef_numeric_literal (parser);
4356 token = cp_lexer_consume_token (parser->lexer);
4357 if (TREE_CODE (token->u.value) == FIXED_CST)
4359 error_at (token->location,
4360 "fixed-point types not supported in C++");
4361 return error_mark_node;
4363 /* Floating-point literals are only allowed in an integral
4364 constant expression if they are cast to an integral or
4365 enumeration type. */
4366 if (TREE_CODE (token->u.value) == REAL_CST
4367 && parser->integral_constant_expression_p
4368 && pedantic)
4370 /* CAST_P will be set even in invalid code like "int(2.7 +
4371 ...)". Therefore, we have to check that the next token
4372 is sure to end the cast. */
4373 if (cast_p)
4375 cp_token *next_token;
4377 next_token = cp_lexer_peek_token (parser->lexer);
4378 if (/* The comma at the end of an
4379 enumerator-definition. */
4380 next_token->type != CPP_COMMA
4381 /* The curly brace at the end of an enum-specifier. */
4382 && next_token->type != CPP_CLOSE_BRACE
4383 /* The end of a statement. */
4384 && next_token->type != CPP_SEMICOLON
4385 /* The end of the cast-expression. */
4386 && next_token->type != CPP_CLOSE_PAREN
4387 /* The end of an array bound. */
4388 && next_token->type != CPP_CLOSE_SQUARE
4389 /* The closing ">" in a template-argument-list. */
4390 && (next_token->type != CPP_GREATER
4391 || parser->greater_than_is_operator_p)
4392 /* C++0x only: A ">>" treated like two ">" tokens,
4393 in a template-argument-list. */
4394 && (next_token->type != CPP_RSHIFT
4395 || (cxx_dialect == cxx98)
4396 || parser->greater_than_is_operator_p))
4397 cast_p = false;
4400 /* If we are within a cast, then the constraint that the
4401 cast is to an integral or enumeration type will be
4402 checked at that point. If we are not within a cast, then
4403 this code is invalid. */
4404 if (!cast_p)
4405 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4407 return token->u.value;
4409 case CPP_CHAR_USERDEF:
4410 case CPP_CHAR16_USERDEF:
4411 case CPP_CHAR32_USERDEF:
4412 case CPP_WCHAR_USERDEF:
4413 return cp_parser_userdef_char_literal (parser);
4415 case CPP_STRING:
4416 case CPP_STRING16:
4417 case CPP_STRING32:
4418 case CPP_WSTRING:
4419 case CPP_UTF8STRING:
4420 case CPP_STRING_USERDEF:
4421 case CPP_STRING16_USERDEF:
4422 case CPP_STRING32_USERDEF:
4423 case CPP_WSTRING_USERDEF:
4424 case CPP_UTF8STRING_USERDEF:
4425 /* ??? Should wide strings be allowed when parser->translate_strings_p
4426 is false (i.e. in attributes)? If not, we can kill the third
4427 argument to cp_parser_string_literal. */
4428 return cp_parser_string_literal (parser,
4429 parser->translate_strings_p,
4430 true);
4432 case CPP_OPEN_PAREN:
4433 /* If we see `( { ' then we are looking at the beginning of
4434 a GNU statement-expression. */
4435 if (cp_parser_allow_gnu_extensions_p (parser)
4436 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4438 /* Statement-expressions are not allowed by the standard. */
4439 pedwarn (token->location, OPT_Wpedantic,
4440 "ISO C++ forbids braced-groups within expressions");
4442 /* And they're not allowed outside of a function-body; you
4443 cannot, for example, write:
4445 int i = ({ int j = 3; j + 1; });
4447 at class or namespace scope. */
4448 if (!parser->in_function_body
4449 || parser->in_template_argument_list_p)
4451 error_at (token->location,
4452 "statement-expressions are not allowed outside "
4453 "functions nor in template-argument lists");
4454 cp_parser_skip_to_end_of_block_or_statement (parser);
4455 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4456 cp_lexer_consume_token (parser->lexer);
4457 return error_mark_node;
4459 else
4460 return cp_parser_statement_expr (parser);
4462 /* Otherwise it's a normal parenthesized expression. */
4464 tree expr;
4465 bool saved_greater_than_is_operator_p;
4467 /* Consume the `('. */
4468 cp_lexer_consume_token (parser->lexer);
4469 /* Within a parenthesized expression, a `>' token is always
4470 the greater-than operator. */
4471 saved_greater_than_is_operator_p
4472 = parser->greater_than_is_operator_p;
4473 parser->greater_than_is_operator_p = true;
4475 /* Parse the parenthesized expression. */
4476 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4477 /* Let the front end know that this expression was
4478 enclosed in parentheses. This matters in case, for
4479 example, the expression is of the form `A::B', since
4480 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4481 not. */
4482 expr = finish_parenthesized_expr (expr);
4483 /* DR 705: Wrapping an unqualified name in parentheses
4484 suppresses arg-dependent lookup. We want to pass back
4485 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4486 (c++/37862), but none of the others. */
4487 if (*idk != CP_ID_KIND_QUALIFIED)
4488 *idk = CP_ID_KIND_NONE;
4490 /* The `>' token might be the end of a template-id or
4491 template-parameter-list now. */
4492 parser->greater_than_is_operator_p
4493 = saved_greater_than_is_operator_p;
4494 /* Consume the `)'. */
4495 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4496 cp_parser_skip_to_end_of_statement (parser);
4498 return expr;
4501 case CPP_OPEN_SQUARE:
4503 if (c_dialect_objc ())
4505 /* We might have an Objective-C++ message. */
4506 cp_parser_parse_tentatively (parser);
4507 tree msg = cp_parser_objc_message_expression (parser);
4508 /* If that works out, we're done ... */
4509 if (cp_parser_parse_definitely (parser))
4510 return msg;
4511 /* ... else, fall though to see if it's a lambda. */
4513 tree lam = cp_parser_lambda_expression (parser);
4514 /* Don't warn about a failed tentative parse. */
4515 if (cp_parser_error_occurred (parser))
4516 return error_mark_node;
4517 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4518 return lam;
4521 case CPP_OBJC_STRING:
4522 if (c_dialect_objc ())
4523 /* We have an Objective-C++ string literal. */
4524 return cp_parser_objc_expression (parser);
4525 cp_parser_error (parser, "expected primary-expression");
4526 return error_mark_node;
4528 case CPP_KEYWORD:
4529 switch (token->keyword)
4531 /* These two are the boolean literals. */
4532 case RID_TRUE:
4533 cp_lexer_consume_token (parser->lexer);
4534 return boolean_true_node;
4535 case RID_FALSE:
4536 cp_lexer_consume_token (parser->lexer);
4537 return boolean_false_node;
4539 /* The `__null' literal. */
4540 case RID_NULL:
4541 cp_lexer_consume_token (parser->lexer);
4542 return null_node;
4544 /* The `nullptr' literal. */
4545 case RID_NULLPTR:
4546 cp_lexer_consume_token (parser->lexer);
4547 return nullptr_node;
4549 /* Recognize the `this' keyword. */
4550 case RID_THIS:
4551 cp_lexer_consume_token (parser->lexer);
4552 if (parser->local_variables_forbidden_p)
4554 error_at (token->location,
4555 "%<this%> may not be used in this context");
4556 return error_mark_node;
4558 /* Pointers cannot appear in constant-expressions. */
4559 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4560 return error_mark_node;
4561 return finish_this_expr ();
4563 /* The `operator' keyword can be the beginning of an
4564 id-expression. */
4565 case RID_OPERATOR:
4566 goto id_expression;
4568 case RID_FUNCTION_NAME:
4569 case RID_PRETTY_FUNCTION_NAME:
4570 case RID_C99_FUNCTION_NAME:
4572 non_integral_constant name;
4574 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4575 __func__ are the names of variables -- but they are
4576 treated specially. Therefore, they are handled here,
4577 rather than relying on the generic id-expression logic
4578 below. Grammatically, these names are id-expressions.
4580 Consume the token. */
4581 token = cp_lexer_consume_token (parser->lexer);
4583 switch (token->keyword)
4585 case RID_FUNCTION_NAME:
4586 name = NIC_FUNC_NAME;
4587 break;
4588 case RID_PRETTY_FUNCTION_NAME:
4589 name = NIC_PRETTY_FUNC;
4590 break;
4591 case RID_C99_FUNCTION_NAME:
4592 name = NIC_C99_FUNC;
4593 break;
4594 default:
4595 gcc_unreachable ();
4598 if (cp_parser_non_integral_constant_expression (parser, name))
4599 return error_mark_node;
4601 /* Look up the name. */
4602 return finish_fname (token->u.value);
4605 case RID_VA_ARG:
4607 tree expression;
4608 tree type;
4609 source_location type_location;
4611 /* The `__builtin_va_arg' construct is used to handle
4612 `va_arg'. Consume the `__builtin_va_arg' token. */
4613 cp_lexer_consume_token (parser->lexer);
4614 /* Look for the opening `('. */
4615 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4616 /* Now, parse the assignment-expression. */
4617 expression = cp_parser_assignment_expression (parser);
4618 /* Look for the `,'. */
4619 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4620 type_location = cp_lexer_peek_token (parser->lexer)->location;
4621 /* Parse the type-id. */
4622 type = cp_parser_type_id (parser);
4623 /* Look for the closing `)'. */
4624 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4625 /* Using `va_arg' in a constant-expression is not
4626 allowed. */
4627 if (cp_parser_non_integral_constant_expression (parser,
4628 NIC_VA_ARG))
4629 return error_mark_node;
4630 return build_x_va_arg (type_location, expression, type);
4633 case RID_OFFSETOF:
4634 return cp_parser_builtin_offsetof (parser);
4636 case RID_HAS_NOTHROW_ASSIGN:
4637 case RID_HAS_NOTHROW_CONSTRUCTOR:
4638 case RID_HAS_NOTHROW_COPY:
4639 case RID_HAS_TRIVIAL_ASSIGN:
4640 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4641 case RID_HAS_TRIVIAL_COPY:
4642 case RID_HAS_TRIVIAL_DESTRUCTOR:
4643 case RID_HAS_VIRTUAL_DESTRUCTOR:
4644 case RID_IS_ABSTRACT:
4645 case RID_IS_BASE_OF:
4646 case RID_IS_CLASS:
4647 case RID_IS_EMPTY:
4648 case RID_IS_ENUM:
4649 case RID_IS_FINAL:
4650 case RID_IS_LITERAL_TYPE:
4651 case RID_IS_POD:
4652 case RID_IS_POLYMORPHIC:
4653 case RID_IS_SAME_AS:
4654 case RID_IS_STD_LAYOUT:
4655 case RID_IS_TRIVIAL:
4656 case RID_IS_TRIVIALLY_ASSIGNABLE:
4657 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4658 case RID_IS_TRIVIALLY_COPYABLE:
4659 case RID_IS_UNION:
4660 return cp_parser_trait_expr (parser, token->keyword);
4662 // C++ concepts
4663 case RID_REQUIRES:
4664 return cp_parser_requires_expression (parser);
4666 /* Objective-C++ expressions. */
4667 case RID_AT_ENCODE:
4668 case RID_AT_PROTOCOL:
4669 case RID_AT_SELECTOR:
4670 return cp_parser_objc_expression (parser);
4672 case RID_TEMPLATE:
4673 if (parser->in_function_body
4674 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4675 == CPP_LESS))
4677 error_at (token->location,
4678 "a template declaration cannot appear at block scope");
4679 cp_parser_skip_to_end_of_block_or_statement (parser);
4680 return error_mark_node;
4682 default:
4683 cp_parser_error (parser, "expected primary-expression");
4684 return error_mark_node;
4687 /* An id-expression can start with either an identifier, a
4688 `::' as the beginning of a qualified-id, or the "operator"
4689 keyword. */
4690 case CPP_NAME:
4691 case CPP_SCOPE:
4692 case CPP_TEMPLATE_ID:
4693 case CPP_NESTED_NAME_SPECIFIER:
4695 tree id_expression;
4696 tree decl;
4697 const char *error_msg;
4698 bool template_p;
4699 bool done;
4700 cp_token *id_expr_token;
4702 id_expression:
4703 /* Parse the id-expression. */
4704 id_expression
4705 = cp_parser_id_expression (parser,
4706 /*template_keyword_p=*/false,
4707 /*check_dependency_p=*/true,
4708 &template_p,
4709 /*declarator_p=*/false,
4710 /*optional_p=*/false);
4711 if (id_expression == error_mark_node)
4712 return error_mark_node;
4713 id_expr_token = token;
4714 token = cp_lexer_peek_token (parser->lexer);
4715 done = (token->type != CPP_OPEN_SQUARE
4716 && token->type != CPP_OPEN_PAREN
4717 && token->type != CPP_DOT
4718 && token->type != CPP_DEREF
4719 && token->type != CPP_PLUS_PLUS
4720 && token->type != CPP_MINUS_MINUS);
4721 /* If we have a template-id, then no further lookup is
4722 required. If the template-id was for a template-class, we
4723 will sometimes have a TYPE_DECL at this point. */
4724 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4725 || TREE_CODE (id_expression) == TYPE_DECL)
4726 decl = id_expression;
4727 /* Look up the name. */
4728 else
4730 tree ambiguous_decls;
4732 /* If we already know that this lookup is ambiguous, then
4733 we've already issued an error message; there's no reason
4734 to check again. */
4735 if (id_expr_token->type == CPP_NAME
4736 && id_expr_token->error_reported)
4738 cp_parser_simulate_error (parser);
4739 return error_mark_node;
4742 decl = cp_parser_lookup_name (parser, id_expression,
4743 none_type,
4744 template_p,
4745 /*is_namespace=*/false,
4746 /*check_dependency=*/true,
4747 &ambiguous_decls,
4748 id_expr_token->location);
4749 /* If the lookup was ambiguous, an error will already have
4750 been issued. */
4751 if (ambiguous_decls)
4752 return error_mark_node;
4754 /* In Objective-C++, we may have an Objective-C 2.0
4755 dot-syntax for classes here. */
4756 if (c_dialect_objc ()
4757 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4758 && TREE_CODE (decl) == TYPE_DECL
4759 && objc_is_class_name (decl))
4761 tree component;
4762 cp_lexer_consume_token (parser->lexer);
4763 component = cp_parser_identifier (parser);
4764 if (component == error_mark_node)
4765 return error_mark_node;
4767 return objc_build_class_component_ref (id_expression, component);
4770 /* In Objective-C++, an instance variable (ivar) may be preferred
4771 to whatever cp_parser_lookup_name() found. */
4772 decl = objc_lookup_ivar (decl, id_expression);
4774 /* If name lookup gives us a SCOPE_REF, then the
4775 qualifying scope was dependent. */
4776 if (TREE_CODE (decl) == SCOPE_REF)
4778 /* At this point, we do not know if DECL is a valid
4779 integral constant expression. We assume that it is
4780 in fact such an expression, so that code like:
4782 template <int N> struct A {
4783 int a[B<N>::i];
4786 is accepted. At template-instantiation time, we
4787 will check that B<N>::i is actually a constant. */
4788 return decl;
4790 /* Check to see if DECL is a local variable in a context
4791 where that is forbidden. */
4792 if (parser->local_variables_forbidden_p
4793 && local_variable_p (decl))
4795 /* It might be that we only found DECL because we are
4796 trying to be generous with pre-ISO scoping rules.
4797 For example, consider:
4799 int i;
4800 void g() {
4801 for (int i = 0; i < 10; ++i) {}
4802 extern void f(int j = i);
4805 Here, name look up will originally find the out
4806 of scope `i'. We need to issue a warning message,
4807 but then use the global `i'. */
4808 decl = check_for_out_of_scope_variable (decl);
4809 if (local_variable_p (decl))
4811 error_at (id_expr_token->location,
4812 "local variable %qD may not appear in this context",
4813 decl);
4814 return error_mark_node;
4819 decl = (finish_id_expression
4820 (id_expression, decl, parser->scope,
4821 idk,
4822 parser->integral_constant_expression_p,
4823 parser->allow_non_integral_constant_expression_p,
4824 &parser->non_integral_constant_expression_p,
4825 template_p, done, address_p,
4826 template_arg_p,
4827 &error_msg,
4828 id_expr_token->location));
4829 if (error_msg)
4830 cp_parser_error (parser, error_msg);
4831 return decl;
4834 /* Anything else is an error. */
4835 default:
4836 cp_parser_error (parser, "expected primary-expression");
4837 return error_mark_node;
4841 static inline tree
4842 cp_parser_primary_expression (cp_parser *parser,
4843 bool address_p,
4844 bool cast_p,
4845 bool template_arg_p,
4846 cp_id_kind *idk)
4848 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4849 /*decltype*/false, idk);
4852 /* Parse an id-expression.
4854 id-expression:
4855 unqualified-id
4856 qualified-id
4858 qualified-id:
4859 :: [opt] nested-name-specifier template [opt] unqualified-id
4860 :: identifier
4861 :: operator-function-id
4862 :: template-id
4864 Return a representation of the unqualified portion of the
4865 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4866 a `::' or nested-name-specifier.
4868 Often, if the id-expression was a qualified-id, the caller will
4869 want to make a SCOPE_REF to represent the qualified-id. This
4870 function does not do this in order to avoid wastefully creating
4871 SCOPE_REFs when they are not required.
4873 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4874 `template' keyword.
4876 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4877 uninstantiated templates.
4879 If *TEMPLATE_P is non-NULL, it is set to true iff the
4880 `template' keyword is used to explicitly indicate that the entity
4881 named is a template.
4883 If DECLARATOR_P is true, the id-expression is appearing as part of
4884 a declarator, rather than as part of an expression. */
4886 static tree
4887 cp_parser_id_expression (cp_parser *parser,
4888 bool template_keyword_p,
4889 bool check_dependency_p,
4890 bool *template_p,
4891 bool declarator_p,
4892 bool optional_p)
4894 bool global_scope_p;
4895 bool nested_name_specifier_p;
4897 /* Assume the `template' keyword was not used. */
4898 if (template_p)
4899 *template_p = template_keyword_p;
4901 /* Look for the optional `::' operator. */
4902 global_scope_p
4903 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4904 != NULL_TREE);
4905 /* Look for the optional nested-name-specifier. */
4906 nested_name_specifier_p
4907 = (cp_parser_nested_name_specifier_opt (parser,
4908 /*typename_keyword_p=*/false,
4909 check_dependency_p,
4910 /*type_p=*/false,
4911 declarator_p)
4912 != NULL_TREE);
4913 /* If there is a nested-name-specifier, then we are looking at
4914 the first qualified-id production. */
4915 if (nested_name_specifier_p)
4917 tree saved_scope;
4918 tree saved_object_scope;
4919 tree saved_qualifying_scope;
4920 tree unqualified_id;
4921 bool is_template;
4923 /* See if the next token is the `template' keyword. */
4924 if (!template_p)
4925 template_p = &is_template;
4926 *template_p = cp_parser_optional_template_keyword (parser);
4927 /* Name lookup we do during the processing of the
4928 unqualified-id might obliterate SCOPE. */
4929 saved_scope = parser->scope;
4930 saved_object_scope = parser->object_scope;
4931 saved_qualifying_scope = parser->qualifying_scope;
4932 /* Process the final unqualified-id. */
4933 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4934 check_dependency_p,
4935 declarator_p,
4936 /*optional_p=*/false);
4937 /* Restore the SAVED_SCOPE for our caller. */
4938 parser->scope = saved_scope;
4939 parser->object_scope = saved_object_scope;
4940 parser->qualifying_scope = saved_qualifying_scope;
4942 return unqualified_id;
4944 /* Otherwise, if we are in global scope, then we are looking at one
4945 of the other qualified-id productions. */
4946 else if (global_scope_p)
4948 cp_token *token;
4949 tree id;
4951 /* Peek at the next token. */
4952 token = cp_lexer_peek_token (parser->lexer);
4954 /* If it's an identifier, and the next token is not a "<", then
4955 we can avoid the template-id case. This is an optimization
4956 for this common case. */
4957 if (token->type == CPP_NAME
4958 && !cp_parser_nth_token_starts_template_argument_list_p
4959 (parser, 2))
4960 return cp_parser_identifier (parser);
4962 cp_parser_parse_tentatively (parser);
4963 /* Try a template-id. */
4964 id = cp_parser_template_id (parser,
4965 /*template_keyword_p=*/false,
4966 /*check_dependency_p=*/true,
4967 none_type,
4968 declarator_p);
4969 /* If that worked, we're done. */
4970 if (cp_parser_parse_definitely (parser))
4971 return id;
4973 /* Peek at the next token. (Changes in the token buffer may
4974 have invalidated the pointer obtained above.) */
4975 token = cp_lexer_peek_token (parser->lexer);
4977 switch (token->type)
4979 case CPP_NAME:
4980 return cp_parser_identifier (parser);
4982 case CPP_KEYWORD:
4983 if (token->keyword == RID_OPERATOR)
4984 return cp_parser_operator_function_id (parser);
4985 /* Fall through. */
4987 default:
4988 cp_parser_error (parser, "expected id-expression");
4989 return error_mark_node;
4992 else
4993 return cp_parser_unqualified_id (parser, template_keyword_p,
4994 /*check_dependency_p=*/true,
4995 declarator_p,
4996 optional_p);
4999 /* Parse an unqualified-id.
5001 unqualified-id:
5002 identifier
5003 operator-function-id
5004 conversion-function-id
5005 ~ class-name
5006 template-id
5008 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5009 keyword, in a construct like `A::template ...'.
5011 Returns a representation of unqualified-id. For the `identifier'
5012 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5013 production a BIT_NOT_EXPR is returned; the operand of the
5014 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5015 other productions, see the documentation accompanying the
5016 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5017 names are looked up in uninstantiated templates. If DECLARATOR_P
5018 is true, the unqualified-id is appearing as part of a declarator,
5019 rather than as part of an expression. */
5021 static tree
5022 cp_parser_unqualified_id (cp_parser* parser,
5023 bool template_keyword_p,
5024 bool check_dependency_p,
5025 bool declarator_p,
5026 bool optional_p)
5028 cp_token *token;
5030 /* Peek at the next token. */
5031 token = cp_lexer_peek_token (parser->lexer);
5033 switch ((int) token->type)
5035 case CPP_NAME:
5037 tree id;
5039 /* We don't know yet whether or not this will be a
5040 template-id. */
5041 cp_parser_parse_tentatively (parser);
5042 /* Try a template-id. */
5043 id = cp_parser_template_id (parser, template_keyword_p,
5044 check_dependency_p,
5045 none_type,
5046 declarator_p);
5047 /* If it worked, we're done. */
5048 if (cp_parser_parse_definitely (parser))
5049 return id;
5050 /* Otherwise, it's an ordinary identifier. */
5051 return cp_parser_identifier (parser);
5054 case CPP_TEMPLATE_ID:
5055 return cp_parser_template_id (parser, template_keyword_p,
5056 check_dependency_p,
5057 none_type,
5058 declarator_p);
5060 case CPP_COMPL:
5062 tree type_decl;
5063 tree qualifying_scope;
5064 tree object_scope;
5065 tree scope;
5066 bool done;
5068 /* Consume the `~' token. */
5069 cp_lexer_consume_token (parser->lexer);
5070 /* Parse the class-name. The standard, as written, seems to
5071 say that:
5073 template <typename T> struct S { ~S (); };
5074 template <typename T> S<T>::~S() {}
5076 is invalid, since `~' must be followed by a class-name, but
5077 `S<T>' is dependent, and so not known to be a class.
5078 That's not right; we need to look in uninstantiated
5079 templates. A further complication arises from:
5081 template <typename T> void f(T t) {
5082 t.T::~T();
5085 Here, it is not possible to look up `T' in the scope of `T'
5086 itself. We must look in both the current scope, and the
5087 scope of the containing complete expression.
5089 Yet another issue is:
5091 struct S {
5092 int S;
5093 ~S();
5096 S::~S() {}
5098 The standard does not seem to say that the `S' in `~S'
5099 should refer to the type `S' and not the data member
5100 `S::S'. */
5102 /* DR 244 says that we look up the name after the "~" in the
5103 same scope as we looked up the qualifying name. That idea
5104 isn't fully worked out; it's more complicated than that. */
5105 scope = parser->scope;
5106 object_scope = parser->object_scope;
5107 qualifying_scope = parser->qualifying_scope;
5109 /* Check for invalid scopes. */
5110 if (scope == error_mark_node)
5112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5113 cp_lexer_consume_token (parser->lexer);
5114 return error_mark_node;
5116 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5118 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5119 error_at (token->location,
5120 "scope %qT before %<~%> is not a class-name",
5121 scope);
5122 cp_parser_simulate_error (parser);
5123 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5124 cp_lexer_consume_token (parser->lexer);
5125 return error_mark_node;
5127 gcc_assert (!scope || TYPE_P (scope));
5129 /* If the name is of the form "X::~X" it's OK even if X is a
5130 typedef. */
5131 token = cp_lexer_peek_token (parser->lexer);
5132 if (scope
5133 && token->type == CPP_NAME
5134 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5135 != CPP_LESS)
5136 && (token->u.value == TYPE_IDENTIFIER (scope)
5137 || (CLASS_TYPE_P (scope)
5138 && constructor_name_p (token->u.value, scope))))
5140 cp_lexer_consume_token (parser->lexer);
5141 return build_nt (BIT_NOT_EXPR, scope);
5144 /* ~auto means the destructor of whatever the object is. */
5145 if (cp_parser_is_keyword (token, RID_AUTO))
5147 if (cxx_dialect < cxx14)
5148 pedwarn (input_location, 0,
5149 "%<~auto%> only available with "
5150 "-std=c++14 or -std=gnu++14");
5151 cp_lexer_consume_token (parser->lexer);
5152 return build_nt (BIT_NOT_EXPR, make_auto ());
5155 /* If there was an explicit qualification (S::~T), first look
5156 in the scope given by the qualification (i.e., S).
5158 Note: in the calls to cp_parser_class_name below we pass
5159 typename_type so that lookup finds the injected-class-name
5160 rather than the constructor. */
5161 done = false;
5162 type_decl = NULL_TREE;
5163 if (scope)
5165 cp_parser_parse_tentatively (parser);
5166 type_decl = cp_parser_class_name (parser,
5167 /*typename_keyword_p=*/false,
5168 /*template_keyword_p=*/false,
5169 typename_type,
5170 /*check_dependency=*/false,
5171 /*class_head_p=*/false,
5172 declarator_p);
5173 if (cp_parser_parse_definitely (parser))
5174 done = true;
5176 /* In "N::S::~S", look in "N" as well. */
5177 if (!done && scope && qualifying_scope)
5179 cp_parser_parse_tentatively (parser);
5180 parser->scope = qualifying_scope;
5181 parser->object_scope = NULL_TREE;
5182 parser->qualifying_scope = NULL_TREE;
5183 type_decl
5184 = cp_parser_class_name (parser,
5185 /*typename_keyword_p=*/false,
5186 /*template_keyword_p=*/false,
5187 typename_type,
5188 /*check_dependency=*/false,
5189 /*class_head_p=*/false,
5190 declarator_p);
5191 if (cp_parser_parse_definitely (parser))
5192 done = true;
5194 /* In "p->S::~T", look in the scope given by "*p" as well. */
5195 else if (!done && object_scope)
5197 cp_parser_parse_tentatively (parser);
5198 parser->scope = object_scope;
5199 parser->object_scope = NULL_TREE;
5200 parser->qualifying_scope = NULL_TREE;
5201 type_decl
5202 = cp_parser_class_name (parser,
5203 /*typename_keyword_p=*/false,
5204 /*template_keyword_p=*/false,
5205 typename_type,
5206 /*check_dependency=*/false,
5207 /*class_head_p=*/false,
5208 declarator_p);
5209 if (cp_parser_parse_definitely (parser))
5210 done = true;
5212 /* Look in the surrounding context. */
5213 if (!done)
5215 parser->scope = NULL_TREE;
5216 parser->object_scope = NULL_TREE;
5217 parser->qualifying_scope = NULL_TREE;
5218 if (processing_template_decl)
5219 cp_parser_parse_tentatively (parser);
5220 type_decl
5221 = cp_parser_class_name (parser,
5222 /*typename_keyword_p=*/false,
5223 /*template_keyword_p=*/false,
5224 typename_type,
5225 /*check_dependency=*/false,
5226 /*class_head_p=*/false,
5227 declarator_p);
5228 if (processing_template_decl
5229 && ! cp_parser_parse_definitely (parser))
5231 /* We couldn't find a type with this name, so just accept
5232 it and check for a match at instantiation time. */
5233 type_decl = cp_parser_identifier (parser);
5234 if (type_decl != error_mark_node)
5235 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5236 return type_decl;
5239 /* If an error occurred, assume that the name of the
5240 destructor is the same as the name of the qualifying
5241 class. That allows us to keep parsing after running
5242 into ill-formed destructor names. */
5243 if (type_decl == error_mark_node && scope)
5244 return build_nt (BIT_NOT_EXPR, scope);
5245 else if (type_decl == error_mark_node)
5246 return error_mark_node;
5248 /* Check that destructor name and scope match. */
5249 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5251 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5252 error_at (token->location,
5253 "declaration of %<~%T%> as member of %qT",
5254 type_decl, scope);
5255 cp_parser_simulate_error (parser);
5256 return error_mark_node;
5259 /* [class.dtor]
5261 A typedef-name that names a class shall not be used as the
5262 identifier in the declarator for a destructor declaration. */
5263 if (declarator_p
5264 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5265 && !DECL_SELF_REFERENCE_P (type_decl)
5266 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5267 error_at (token->location,
5268 "typedef-name %qD used as destructor declarator",
5269 type_decl);
5271 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5274 case CPP_KEYWORD:
5275 if (token->keyword == RID_OPERATOR)
5277 tree id;
5279 /* This could be a template-id, so we try that first. */
5280 cp_parser_parse_tentatively (parser);
5281 /* Try a template-id. */
5282 id = cp_parser_template_id (parser, template_keyword_p,
5283 /*check_dependency_p=*/true,
5284 none_type,
5285 declarator_p);
5286 /* If that worked, we're done. */
5287 if (cp_parser_parse_definitely (parser))
5288 return id;
5289 /* We still don't know whether we're looking at an
5290 operator-function-id or a conversion-function-id. */
5291 cp_parser_parse_tentatively (parser);
5292 /* Try an operator-function-id. */
5293 id = cp_parser_operator_function_id (parser);
5294 /* If that didn't work, try a conversion-function-id. */
5295 if (!cp_parser_parse_definitely (parser))
5296 id = cp_parser_conversion_function_id (parser);
5297 else if (UDLIT_OPER_P (id))
5299 /* 17.6.3.3.5 */
5300 const char *name = UDLIT_OP_SUFFIX (id);
5301 if (name[0] != '_' && !in_system_header_at (input_location)
5302 && declarator_p)
5303 warning (0, "literal operator suffixes not preceded by %<_%>"
5304 " are reserved for future standardization");
5307 return id;
5309 /* Fall through. */
5311 default:
5312 if (optional_p)
5313 return NULL_TREE;
5314 cp_parser_error (parser, "expected unqualified-id");
5315 return error_mark_node;
5319 /* Parse an (optional) nested-name-specifier.
5321 nested-name-specifier: [C++98]
5322 class-or-namespace-name :: nested-name-specifier [opt]
5323 class-or-namespace-name :: template nested-name-specifier [opt]
5325 nested-name-specifier: [C++0x]
5326 type-name ::
5327 namespace-name ::
5328 nested-name-specifier identifier ::
5329 nested-name-specifier template [opt] simple-template-id ::
5331 PARSER->SCOPE should be set appropriately before this function is
5332 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5333 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5334 in name lookups.
5336 Sets PARSER->SCOPE to the class (TYPE) or namespace
5337 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5338 it unchanged if there is no nested-name-specifier. Returns the new
5339 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5341 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5342 part of a declaration and/or decl-specifier. */
5344 static tree
5345 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5346 bool typename_keyword_p,
5347 bool check_dependency_p,
5348 bool type_p,
5349 bool is_declaration)
5351 bool success = false;
5352 cp_token_position start = 0;
5353 cp_token *token;
5355 /* Remember where the nested-name-specifier starts. */
5356 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5358 start = cp_lexer_token_position (parser->lexer, false);
5359 push_deferring_access_checks (dk_deferred);
5362 while (true)
5364 tree new_scope;
5365 tree old_scope;
5366 tree saved_qualifying_scope;
5367 bool template_keyword_p;
5369 /* Spot cases that cannot be the beginning of a
5370 nested-name-specifier. */
5371 token = cp_lexer_peek_token (parser->lexer);
5373 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5374 the already parsed nested-name-specifier. */
5375 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5377 /* Grab the nested-name-specifier and continue the loop. */
5378 cp_parser_pre_parsed_nested_name_specifier (parser);
5379 /* If we originally encountered this nested-name-specifier
5380 with IS_DECLARATION set to false, we will not have
5381 resolved TYPENAME_TYPEs, so we must do so here. */
5382 if (is_declaration
5383 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5385 new_scope = resolve_typename_type (parser->scope,
5386 /*only_current_p=*/false);
5387 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5388 parser->scope = new_scope;
5390 success = true;
5391 continue;
5394 /* Spot cases that cannot be the beginning of a
5395 nested-name-specifier. On the second and subsequent times
5396 through the loop, we look for the `template' keyword. */
5397 if (success && token->keyword == RID_TEMPLATE)
5399 /* A template-id can start a nested-name-specifier. */
5400 else if (token->type == CPP_TEMPLATE_ID)
5402 /* DR 743: decltype can be used in a nested-name-specifier. */
5403 else if (token_is_decltype (token))
5405 else
5407 /* If the next token is not an identifier, then it is
5408 definitely not a type-name or namespace-name. */
5409 if (token->type != CPP_NAME)
5410 break;
5411 /* If the following token is neither a `<' (to begin a
5412 template-id), nor a `::', then we are not looking at a
5413 nested-name-specifier. */
5414 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5416 if (token->type == CPP_COLON
5417 && parser->colon_corrects_to_scope_p
5418 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5420 error_at (token->location,
5421 "found %<:%> in nested-name-specifier, expected %<::%>");
5422 token->type = CPP_SCOPE;
5425 if (token->type != CPP_SCOPE
5426 && !cp_parser_nth_token_starts_template_argument_list_p
5427 (parser, 2))
5428 break;
5431 /* The nested-name-specifier is optional, so we parse
5432 tentatively. */
5433 cp_parser_parse_tentatively (parser);
5435 /* Look for the optional `template' keyword, if this isn't the
5436 first time through the loop. */
5437 if (success)
5438 template_keyword_p = cp_parser_optional_template_keyword (parser);
5439 else
5440 template_keyword_p = false;
5442 /* Save the old scope since the name lookup we are about to do
5443 might destroy it. */
5444 old_scope = parser->scope;
5445 saved_qualifying_scope = parser->qualifying_scope;
5446 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5447 look up names in "X<T>::I" in order to determine that "Y" is
5448 a template. So, if we have a typename at this point, we make
5449 an effort to look through it. */
5450 if (is_declaration
5451 && !typename_keyword_p
5452 && parser->scope
5453 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5454 parser->scope = resolve_typename_type (parser->scope,
5455 /*only_current_p=*/false);
5456 /* Parse the qualifying entity. */
5457 new_scope
5458 = cp_parser_qualifying_entity (parser,
5459 typename_keyword_p,
5460 template_keyword_p,
5461 check_dependency_p,
5462 type_p,
5463 is_declaration);
5464 /* Look for the `::' token. */
5465 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5467 /* If we found what we wanted, we keep going; otherwise, we're
5468 done. */
5469 if (!cp_parser_parse_definitely (parser))
5471 bool error_p = false;
5473 /* Restore the OLD_SCOPE since it was valid before the
5474 failed attempt at finding the last
5475 class-or-namespace-name. */
5476 parser->scope = old_scope;
5477 parser->qualifying_scope = saved_qualifying_scope;
5479 /* If the next token is a decltype, and the one after that is a
5480 `::', then the decltype has failed to resolve to a class or
5481 enumeration type. Give this error even when parsing
5482 tentatively since it can't possibly be valid--and we're going
5483 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5484 won't get another chance.*/
5485 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5486 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5487 == CPP_SCOPE))
5489 token = cp_lexer_consume_token (parser->lexer);
5490 error_at (token->location, "decltype evaluates to %qT, "
5491 "which is not a class or enumeration type",
5492 token->u.value);
5493 parser->scope = error_mark_node;
5494 error_p = true;
5495 /* As below. */
5496 success = true;
5497 cp_lexer_consume_token (parser->lexer);
5500 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5501 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5503 /* If we have a non-type template-id followed by ::, it can't
5504 possibly be valid. */
5505 token = cp_lexer_peek_token (parser->lexer);
5506 tree tid = token->u.tree_check_value->value;
5507 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5508 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5510 tree tmpl = NULL_TREE;
5511 if (is_overloaded_fn (tid))
5513 tree fns = get_fns (tid);
5514 if (!OVL_CHAIN (fns))
5515 tmpl = OVL_CURRENT (fns);
5516 error_at (token->location, "function template-id %qD "
5517 "in nested-name-specifier", tid);
5519 else
5521 /* Variable template. */
5522 tmpl = TREE_OPERAND (tid, 0);
5523 gcc_assert (variable_template_p (tmpl));
5524 error_at (token->location, "variable template-id %qD "
5525 "in nested-name-specifier", tid);
5527 if (tmpl)
5528 inform (DECL_SOURCE_LOCATION (tmpl),
5529 "%qD declared here", tmpl);
5531 parser->scope = error_mark_node;
5532 error_p = true;
5533 /* As below. */
5534 success = true;
5535 cp_lexer_consume_token (parser->lexer);
5536 cp_lexer_consume_token (parser->lexer);
5540 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5541 break;
5542 /* If the next token is an identifier, and the one after
5543 that is a `::', then any valid interpretation would have
5544 found a class-or-namespace-name. */
5545 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5546 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5547 == CPP_SCOPE)
5548 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5549 != CPP_COMPL))
5551 token = cp_lexer_consume_token (parser->lexer);
5552 if (!error_p)
5554 if (!token->error_reported)
5556 tree decl;
5557 tree ambiguous_decls;
5559 decl = cp_parser_lookup_name (parser, token->u.value,
5560 none_type,
5561 /*is_template=*/false,
5562 /*is_namespace=*/false,
5563 /*check_dependency=*/true,
5564 &ambiguous_decls,
5565 token->location);
5566 if (TREE_CODE (decl) == TEMPLATE_DECL)
5567 error_at (token->location,
5568 "%qD used without template parameters",
5569 decl);
5570 else if (ambiguous_decls)
5572 // cp_parser_lookup_name has the same diagnostic,
5573 // thus make sure to emit it at most once.
5574 if (cp_parser_uncommitted_to_tentative_parse_p
5575 (parser))
5577 error_at (token->location,
5578 "reference to %qD is ambiguous",
5579 token->u.value);
5580 print_candidates (ambiguous_decls);
5582 decl = error_mark_node;
5584 else
5586 if (cxx_dialect != cxx98)
5587 cp_parser_name_lookup_error
5588 (parser, token->u.value, decl, NLE_NOT_CXX98,
5589 token->location);
5590 else
5591 cp_parser_name_lookup_error
5592 (parser, token->u.value, decl, NLE_CXX98,
5593 token->location);
5596 parser->scope = error_mark_node;
5597 error_p = true;
5598 /* Treat this as a successful nested-name-specifier
5599 due to:
5601 [basic.lookup.qual]
5603 If the name found is not a class-name (clause
5604 _class_) or namespace-name (_namespace.def_), the
5605 program is ill-formed. */
5606 success = true;
5608 cp_lexer_consume_token (parser->lexer);
5610 break;
5612 /* We've found one valid nested-name-specifier. */
5613 success = true;
5614 /* Name lookup always gives us a DECL. */
5615 if (TREE_CODE (new_scope) == TYPE_DECL)
5616 new_scope = TREE_TYPE (new_scope);
5617 /* Uses of "template" must be followed by actual templates. */
5618 if (template_keyword_p
5619 && !(CLASS_TYPE_P (new_scope)
5620 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5621 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5622 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5623 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5624 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5625 == TEMPLATE_ID_EXPR)))
5626 permerror (input_location, TYPE_P (new_scope)
5627 ? G_("%qT is not a template")
5628 : G_("%qD is not a template"),
5629 new_scope);
5630 /* If it is a class scope, try to complete it; we are about to
5631 be looking up names inside the class. */
5632 if (TYPE_P (new_scope)
5633 /* Since checking types for dependency can be expensive,
5634 avoid doing it if the type is already complete. */
5635 && !COMPLETE_TYPE_P (new_scope)
5636 /* Do not try to complete dependent types. */
5637 && !dependent_type_p (new_scope))
5639 new_scope = complete_type (new_scope);
5640 /* If it is a typedef to current class, use the current
5641 class instead, as the typedef won't have any names inside
5642 it yet. */
5643 if (!COMPLETE_TYPE_P (new_scope)
5644 && currently_open_class (new_scope))
5645 new_scope = TYPE_MAIN_VARIANT (new_scope);
5647 /* Make sure we look in the right scope the next time through
5648 the loop. */
5649 parser->scope = new_scope;
5652 /* If parsing tentatively, replace the sequence of tokens that makes
5653 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5654 token. That way, should we re-parse the token stream, we will
5655 not have to repeat the effort required to do the parse, nor will
5656 we issue duplicate error messages. */
5657 if (success && start)
5659 cp_token *token;
5661 token = cp_lexer_token_at (parser->lexer, start);
5662 /* Reset the contents of the START token. */
5663 token->type = CPP_NESTED_NAME_SPECIFIER;
5664 /* Retrieve any deferred checks. Do not pop this access checks yet
5665 so the memory will not be reclaimed during token replacing below. */
5666 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5667 token->u.tree_check_value->value = parser->scope;
5668 token->u.tree_check_value->checks = get_deferred_access_checks ();
5669 token->u.tree_check_value->qualifying_scope =
5670 parser->qualifying_scope;
5671 token->keyword = RID_MAX;
5673 /* Purge all subsequent tokens. */
5674 cp_lexer_purge_tokens_after (parser->lexer, start);
5677 if (start)
5678 pop_to_parent_deferring_access_checks ();
5680 return success ? parser->scope : NULL_TREE;
5683 /* Parse a nested-name-specifier. See
5684 cp_parser_nested_name_specifier_opt for details. This function
5685 behaves identically, except that it will an issue an error if no
5686 nested-name-specifier is present. */
5688 static tree
5689 cp_parser_nested_name_specifier (cp_parser *parser,
5690 bool typename_keyword_p,
5691 bool check_dependency_p,
5692 bool type_p,
5693 bool is_declaration)
5695 tree scope;
5697 /* Look for the nested-name-specifier. */
5698 scope = cp_parser_nested_name_specifier_opt (parser,
5699 typename_keyword_p,
5700 check_dependency_p,
5701 type_p,
5702 is_declaration);
5703 /* If it was not present, issue an error message. */
5704 if (!scope)
5706 cp_parser_error (parser, "expected nested-name-specifier");
5707 parser->scope = NULL_TREE;
5710 return scope;
5713 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5714 this is either a class-name or a namespace-name (which corresponds
5715 to the class-or-namespace-name production in the grammar). For
5716 C++0x, it can also be a type-name that refers to an enumeration
5717 type or a simple-template-id.
5719 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5720 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5721 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5722 TYPE_P is TRUE iff the next name should be taken as a class-name,
5723 even the same name is declared to be another entity in the same
5724 scope.
5726 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5727 specified by the class-or-namespace-name. If neither is found the
5728 ERROR_MARK_NODE is returned. */
5730 static tree
5731 cp_parser_qualifying_entity (cp_parser *parser,
5732 bool typename_keyword_p,
5733 bool template_keyword_p,
5734 bool check_dependency_p,
5735 bool type_p,
5736 bool is_declaration)
5738 tree saved_scope;
5739 tree saved_qualifying_scope;
5740 tree saved_object_scope;
5741 tree scope;
5742 bool only_class_p;
5743 bool successful_parse_p;
5745 /* DR 743: decltype can appear in a nested-name-specifier. */
5746 if (cp_lexer_next_token_is_decltype (parser->lexer))
5748 scope = cp_parser_decltype (parser);
5749 if (TREE_CODE (scope) != ENUMERAL_TYPE
5750 && !MAYBE_CLASS_TYPE_P (scope))
5752 cp_parser_simulate_error (parser);
5753 return error_mark_node;
5755 if (TYPE_NAME (scope))
5756 scope = TYPE_NAME (scope);
5757 return scope;
5760 /* Before we try to parse the class-name, we must save away the
5761 current PARSER->SCOPE since cp_parser_class_name will destroy
5762 it. */
5763 saved_scope = parser->scope;
5764 saved_qualifying_scope = parser->qualifying_scope;
5765 saved_object_scope = parser->object_scope;
5766 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5767 there is no need to look for a namespace-name. */
5768 only_class_p = template_keyword_p
5769 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5770 if (!only_class_p)
5771 cp_parser_parse_tentatively (parser);
5772 scope = cp_parser_class_name (parser,
5773 typename_keyword_p,
5774 template_keyword_p,
5775 type_p ? class_type : none_type,
5776 check_dependency_p,
5777 /*class_head_p=*/false,
5778 is_declaration);
5779 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5780 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5781 if (!only_class_p
5782 && cxx_dialect != cxx98
5783 && !successful_parse_p)
5785 /* Restore the saved scope. */
5786 parser->scope = saved_scope;
5787 parser->qualifying_scope = saved_qualifying_scope;
5788 parser->object_scope = saved_object_scope;
5790 /* Parse tentatively. */
5791 cp_parser_parse_tentatively (parser);
5793 /* Parse a type-name */
5794 scope = cp_parser_type_name (parser);
5796 /* "If the name found does not designate a namespace or a class,
5797 enumeration, or dependent type, the program is ill-formed."
5799 We cover classes and dependent types above and namespaces below,
5800 so this code is only looking for enums. */
5801 if (!scope || TREE_CODE (scope) != TYPE_DECL
5802 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5803 cp_parser_simulate_error (parser);
5805 successful_parse_p = cp_parser_parse_definitely (parser);
5807 /* If that didn't work, try for a namespace-name. */
5808 if (!only_class_p && !successful_parse_p)
5810 /* Restore the saved scope. */
5811 parser->scope = saved_scope;
5812 parser->qualifying_scope = saved_qualifying_scope;
5813 parser->object_scope = saved_object_scope;
5814 /* If we are not looking at an identifier followed by the scope
5815 resolution operator, then this is not part of a
5816 nested-name-specifier. (Note that this function is only used
5817 to parse the components of a nested-name-specifier.) */
5818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5819 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5820 return error_mark_node;
5821 scope = cp_parser_namespace_name (parser);
5824 return scope;
5827 /* Return true if we are looking at a compound-literal, false otherwise. */
5829 static bool
5830 cp_parser_compound_literal_p (cp_parser *parser)
5832 /* Consume the `('. */
5833 cp_lexer_consume_token (parser->lexer);
5835 cp_lexer_save_tokens (parser->lexer);
5837 /* Skip tokens until the next token is a closing parenthesis.
5838 If we find the closing `)', and the next token is a `{', then
5839 we are looking at a compound-literal. */
5840 bool compound_literal_p
5841 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5842 /*consume_paren=*/true)
5843 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5845 /* Roll back the tokens we skipped. */
5846 cp_lexer_rollback_tokens (parser->lexer);
5848 return compound_literal_p;
5851 /* Parse a postfix-expression.
5853 postfix-expression:
5854 primary-expression
5855 postfix-expression [ expression ]
5856 postfix-expression ( expression-list [opt] )
5857 simple-type-specifier ( expression-list [opt] )
5858 typename :: [opt] nested-name-specifier identifier
5859 ( expression-list [opt] )
5860 typename :: [opt] nested-name-specifier template [opt] template-id
5861 ( expression-list [opt] )
5862 postfix-expression . template [opt] id-expression
5863 postfix-expression -> template [opt] id-expression
5864 postfix-expression . pseudo-destructor-name
5865 postfix-expression -> pseudo-destructor-name
5866 postfix-expression ++
5867 postfix-expression --
5868 dynamic_cast < type-id > ( expression )
5869 static_cast < type-id > ( expression )
5870 reinterpret_cast < type-id > ( expression )
5871 const_cast < type-id > ( expression )
5872 typeid ( expression )
5873 typeid ( type-id )
5875 GNU Extension:
5877 postfix-expression:
5878 ( type-id ) { initializer-list , [opt] }
5880 This extension is a GNU version of the C99 compound-literal
5881 construct. (The C99 grammar uses `type-name' instead of `type-id',
5882 but they are essentially the same concept.)
5884 If ADDRESS_P is true, the postfix expression is the operand of the
5885 `&' operator. CAST_P is true if this expression is the target of a
5886 cast.
5888 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5889 class member access expressions [expr.ref].
5891 Returns a representation of the expression. */
5893 static tree
5894 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5895 bool member_access_only_p, bool decltype_p,
5896 cp_id_kind * pidk_return)
5898 cp_token *token;
5899 location_t loc;
5900 enum rid keyword;
5901 cp_id_kind idk = CP_ID_KIND_NONE;
5902 tree postfix_expression = NULL_TREE;
5903 bool is_member_access = false;
5904 int saved_in_statement = -1;
5906 /* Peek at the next token. */
5907 token = cp_lexer_peek_token (parser->lexer);
5908 loc = token->location;
5909 /* Some of the productions are determined by keywords. */
5910 keyword = token->keyword;
5911 switch (keyword)
5913 case RID_DYNCAST:
5914 case RID_STATCAST:
5915 case RID_REINTCAST:
5916 case RID_CONSTCAST:
5918 tree type;
5919 tree expression;
5920 const char *saved_message;
5921 bool saved_in_type_id_in_expr_p;
5923 /* All of these can be handled in the same way from the point
5924 of view of parsing. Begin by consuming the token
5925 identifying the cast. */
5926 cp_lexer_consume_token (parser->lexer);
5928 /* New types cannot be defined in the cast. */
5929 saved_message = parser->type_definition_forbidden_message;
5930 parser->type_definition_forbidden_message
5931 = G_("types may not be defined in casts");
5933 /* Look for the opening `<'. */
5934 cp_parser_require (parser, CPP_LESS, RT_LESS);
5935 /* Parse the type to which we are casting. */
5936 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5937 parser->in_type_id_in_expr_p = true;
5938 type = cp_parser_type_id (parser);
5939 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5940 /* Look for the closing `>'. */
5941 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5942 /* Restore the old message. */
5943 parser->type_definition_forbidden_message = saved_message;
5945 bool saved_greater_than_is_operator_p
5946 = parser->greater_than_is_operator_p;
5947 parser->greater_than_is_operator_p = true;
5949 /* And the expression which is being cast. */
5950 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5951 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5954 parser->greater_than_is_operator_p
5955 = saved_greater_than_is_operator_p;
5957 /* Only type conversions to integral or enumeration types
5958 can be used in constant-expressions. */
5959 if (!cast_valid_in_integral_constant_expression_p (type)
5960 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5961 return error_mark_node;
5963 switch (keyword)
5965 case RID_DYNCAST:
5966 postfix_expression
5967 = build_dynamic_cast (type, expression, tf_warning_or_error);
5968 break;
5969 case RID_STATCAST:
5970 postfix_expression
5971 = build_static_cast (type, expression, tf_warning_or_error);
5972 break;
5973 case RID_REINTCAST:
5974 postfix_expression
5975 = build_reinterpret_cast (type, expression,
5976 tf_warning_or_error);
5977 break;
5978 case RID_CONSTCAST:
5979 postfix_expression
5980 = build_const_cast (type, expression, tf_warning_or_error);
5981 break;
5982 default:
5983 gcc_unreachable ();
5986 break;
5988 case RID_TYPEID:
5990 tree type;
5991 const char *saved_message;
5992 bool saved_in_type_id_in_expr_p;
5994 /* Consume the `typeid' token. */
5995 cp_lexer_consume_token (parser->lexer);
5996 /* Look for the `(' token. */
5997 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5998 /* Types cannot be defined in a `typeid' expression. */
5999 saved_message = parser->type_definition_forbidden_message;
6000 parser->type_definition_forbidden_message
6001 = G_("types may not be defined in a %<typeid%> expression");
6002 /* We can't be sure yet whether we're looking at a type-id or an
6003 expression. */
6004 cp_parser_parse_tentatively (parser);
6005 /* Try a type-id first. */
6006 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6007 parser->in_type_id_in_expr_p = true;
6008 type = cp_parser_type_id (parser);
6009 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6010 /* Look for the `)' token. Otherwise, we can't be sure that
6011 we're not looking at an expression: consider `typeid (int
6012 (3))', for example. */
6013 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6014 /* If all went well, simply lookup the type-id. */
6015 if (cp_parser_parse_definitely (parser))
6016 postfix_expression = get_typeid (type, tf_warning_or_error);
6017 /* Otherwise, fall back to the expression variant. */
6018 else
6020 tree expression;
6022 /* Look for an expression. */
6023 expression = cp_parser_expression (parser, & idk);
6024 /* Compute its typeid. */
6025 postfix_expression = build_typeid (expression, tf_warning_or_error);
6026 /* Look for the `)' token. */
6027 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6029 /* Restore the saved message. */
6030 parser->type_definition_forbidden_message = saved_message;
6031 /* `typeid' may not appear in an integral constant expression. */
6032 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6033 return error_mark_node;
6035 break;
6037 case RID_TYPENAME:
6039 tree type;
6040 /* The syntax permitted here is the same permitted for an
6041 elaborated-type-specifier. */
6042 ++parser->prevent_constrained_type_specifiers;
6043 type = cp_parser_elaborated_type_specifier (parser,
6044 /*is_friend=*/false,
6045 /*is_declaration=*/false);
6046 --parser->prevent_constrained_type_specifiers;
6047 postfix_expression = cp_parser_functional_cast (parser, type);
6049 break;
6051 case RID_CILK_SPAWN:
6053 cp_lexer_consume_token (parser->lexer);
6054 token = cp_lexer_peek_token (parser->lexer);
6055 if (token->type == CPP_SEMICOLON)
6057 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6058 "an expression");
6059 postfix_expression = error_mark_node;
6060 break;
6062 else if (!current_function_decl)
6064 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6065 "inside a function");
6066 postfix_expression = error_mark_node;
6067 break;
6069 else
6071 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6072 saved_in_statement = parser->in_statement;
6073 parser->in_statement |= IN_CILK_SPAWN;
6075 cfun->calls_cilk_spawn = 1;
6076 postfix_expression =
6077 cp_parser_postfix_expression (parser, false, false,
6078 false, false, &idk);
6079 if (!flag_cilkplus)
6081 error_at (token->location, "-fcilkplus must be enabled to use"
6082 " %<_Cilk_spawn%>");
6083 cfun->calls_cilk_spawn = 0;
6085 else if (saved_in_statement & IN_CILK_SPAWN)
6087 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6088 "are not permitted");
6089 postfix_expression = error_mark_node;
6090 cfun->calls_cilk_spawn = 0;
6092 else
6094 postfix_expression = build_cilk_spawn (token->location,
6095 postfix_expression);
6096 if (postfix_expression != error_mark_node)
6097 SET_EXPR_LOCATION (postfix_expression, input_location);
6098 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6100 break;
6103 case RID_BUILTIN_SHUFFLE:
6105 vec<tree, va_gc> *vec;
6106 unsigned int i;
6107 tree p;
6109 cp_lexer_consume_token (parser->lexer);
6110 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6111 /*cast_p=*/false, /*allow_expansion_p=*/true,
6112 /*non_constant_p=*/NULL);
6113 if (vec == NULL)
6114 return error_mark_node;
6116 FOR_EACH_VEC_ELT (*vec, i, p)
6117 mark_exp_read (p);
6119 if (vec->length () == 2)
6120 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6121 tf_warning_or_error);
6122 else if (vec->length () == 3)
6123 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6124 tf_warning_or_error);
6125 else
6127 error_at (loc, "wrong number of arguments to "
6128 "%<__builtin_shuffle%>");
6129 return error_mark_node;
6131 break;
6134 default:
6136 tree type;
6138 /* If the next thing is a simple-type-specifier, we may be
6139 looking at a functional cast. We could also be looking at
6140 an id-expression. So, we try the functional cast, and if
6141 that doesn't work we fall back to the primary-expression. */
6142 cp_parser_parse_tentatively (parser);
6143 /* Look for the simple-type-specifier. */
6144 ++parser->prevent_constrained_type_specifiers;
6145 type = cp_parser_simple_type_specifier (parser,
6146 /*decl_specs=*/NULL,
6147 CP_PARSER_FLAGS_NONE);
6148 --parser->prevent_constrained_type_specifiers;
6149 /* Parse the cast itself. */
6150 if (!cp_parser_error_occurred (parser))
6151 postfix_expression
6152 = cp_parser_functional_cast (parser, type);
6153 /* If that worked, we're done. */
6154 if (cp_parser_parse_definitely (parser))
6155 break;
6157 /* If the functional-cast didn't work out, try a
6158 compound-literal. */
6159 if (cp_parser_allow_gnu_extensions_p (parser)
6160 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6162 tree initializer = NULL_TREE;
6164 cp_parser_parse_tentatively (parser);
6166 /* Avoid calling cp_parser_type_id pointlessly, see comment
6167 in cp_parser_cast_expression about c++/29234. */
6168 if (!cp_parser_compound_literal_p (parser))
6169 cp_parser_simulate_error (parser);
6170 else
6172 /* Parse the type. */
6173 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6174 parser->in_type_id_in_expr_p = true;
6175 type = cp_parser_type_id (parser);
6176 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6177 /* Look for the `)'. */
6178 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6181 /* If things aren't going well, there's no need to
6182 keep going. */
6183 if (!cp_parser_error_occurred (parser))
6185 bool non_constant_p;
6186 /* Parse the brace-enclosed initializer list. */
6187 initializer = cp_parser_braced_list (parser,
6188 &non_constant_p);
6190 /* If that worked, we're definitely looking at a
6191 compound-literal expression. */
6192 if (cp_parser_parse_definitely (parser))
6194 /* Warn the user that a compound literal is not
6195 allowed in standard C++. */
6196 pedwarn (input_location, OPT_Wpedantic,
6197 "ISO C++ forbids compound-literals");
6198 /* For simplicity, we disallow compound literals in
6199 constant-expressions. We could
6200 allow compound literals of integer type, whose
6201 initializer was a constant, in constant
6202 expressions. Permitting that usage, as a further
6203 extension, would not change the meaning of any
6204 currently accepted programs. (Of course, as
6205 compound literals are not part of ISO C++, the
6206 standard has nothing to say.) */
6207 if (cp_parser_non_integral_constant_expression (parser,
6208 NIC_NCC))
6210 postfix_expression = error_mark_node;
6211 break;
6213 /* Form the representation of the compound-literal. */
6214 postfix_expression
6215 = finish_compound_literal (type, initializer,
6216 tf_warning_or_error);
6217 break;
6221 /* It must be a primary-expression. */
6222 postfix_expression
6223 = cp_parser_primary_expression (parser, address_p, cast_p,
6224 /*template_arg_p=*/false,
6225 decltype_p,
6226 &idk);
6228 break;
6231 /* Note that we don't need to worry about calling build_cplus_new on a
6232 class-valued CALL_EXPR in decltype when it isn't the end of the
6233 postfix-expression; unary_complex_lvalue will take care of that for
6234 all these cases. */
6236 /* Keep looping until the postfix-expression is complete. */
6237 while (true)
6239 if (idk == CP_ID_KIND_UNQUALIFIED
6240 && identifier_p (postfix_expression)
6241 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6242 /* It is not a Koenig lookup function call. */
6243 postfix_expression
6244 = unqualified_name_lookup_error (postfix_expression);
6246 /* Peek at the next token. */
6247 token = cp_lexer_peek_token (parser->lexer);
6249 switch (token->type)
6251 case CPP_OPEN_SQUARE:
6252 if (cp_next_tokens_can_be_std_attribute_p (parser))
6254 cp_parser_error (parser,
6255 "two consecutive %<[%> shall "
6256 "only introduce an attribute");
6257 return error_mark_node;
6259 postfix_expression
6260 = cp_parser_postfix_open_square_expression (parser,
6261 postfix_expression,
6262 false,
6263 decltype_p);
6264 idk = CP_ID_KIND_NONE;
6265 is_member_access = false;
6266 break;
6268 case CPP_OPEN_PAREN:
6269 /* postfix-expression ( expression-list [opt] ) */
6271 bool koenig_p;
6272 bool is_builtin_constant_p;
6273 bool saved_integral_constant_expression_p = false;
6274 bool saved_non_integral_constant_expression_p = false;
6275 tsubst_flags_t complain = complain_flags (decltype_p);
6276 vec<tree, va_gc> *args;
6278 is_member_access = false;
6280 is_builtin_constant_p
6281 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6282 if (is_builtin_constant_p)
6284 /* The whole point of __builtin_constant_p is to allow
6285 non-constant expressions to appear as arguments. */
6286 saved_integral_constant_expression_p
6287 = parser->integral_constant_expression_p;
6288 saved_non_integral_constant_expression_p
6289 = parser->non_integral_constant_expression_p;
6290 parser->integral_constant_expression_p = false;
6292 args = (cp_parser_parenthesized_expression_list
6293 (parser, non_attr,
6294 /*cast_p=*/false, /*allow_expansion_p=*/true,
6295 /*non_constant_p=*/NULL,
6296 /*want_literal_zero_p=*/warn_memset_transposed_args));
6297 if (is_builtin_constant_p)
6299 parser->integral_constant_expression_p
6300 = saved_integral_constant_expression_p;
6301 parser->non_integral_constant_expression_p
6302 = saved_non_integral_constant_expression_p;
6305 if (args == NULL)
6307 postfix_expression = error_mark_node;
6308 break;
6311 /* Function calls are not permitted in
6312 constant-expressions. */
6313 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6314 && cp_parser_non_integral_constant_expression (parser,
6315 NIC_FUNC_CALL))
6317 postfix_expression = error_mark_node;
6318 release_tree_vector (args);
6319 break;
6322 koenig_p = false;
6323 if (idk == CP_ID_KIND_UNQUALIFIED
6324 || idk == CP_ID_KIND_TEMPLATE_ID)
6326 if (identifier_p (postfix_expression))
6328 if (!args->is_empty ())
6330 koenig_p = true;
6331 if (!any_type_dependent_arguments_p (args))
6332 postfix_expression
6333 = perform_koenig_lookup (postfix_expression, args,
6334 complain);
6336 else
6337 postfix_expression
6338 = unqualified_fn_lookup_error (postfix_expression);
6340 /* We do not perform argument-dependent lookup if
6341 normal lookup finds a non-function, in accordance
6342 with the expected resolution of DR 218. */
6343 else if (!args->is_empty ()
6344 && is_overloaded_fn (postfix_expression))
6346 tree fn = get_first_fn (postfix_expression);
6347 fn = STRIP_TEMPLATE (fn);
6349 /* Do not do argument dependent lookup if regular
6350 lookup finds a member function or a block-scope
6351 function declaration. [basic.lookup.argdep]/3 */
6352 if (!DECL_FUNCTION_MEMBER_P (fn)
6353 && !DECL_LOCAL_FUNCTION_P (fn))
6355 koenig_p = true;
6356 if (!any_type_dependent_arguments_p (args))
6357 postfix_expression
6358 = perform_koenig_lookup (postfix_expression, args,
6359 complain);
6364 if (warn_memset_transposed_args)
6366 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6367 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6368 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6369 && vec_safe_length (args) == 3
6370 && integer_zerop ((*args)[2])
6371 && LITERAL_ZERO_P ((*args)[2])
6372 && !(integer_zerop ((*args)[1])
6373 && LITERAL_ZERO_P ((*args)[1])))
6374 warning (OPT_Wmemset_transposed_args,
6375 "%<memset%> used with constant zero length "
6376 "parameter; this could be due to transposed "
6377 "parameters");
6379 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6380 to avoid leaking those into folder and middle-end. */
6381 unsigned int i;
6382 tree arg;
6383 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6384 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6385 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6388 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6390 tree instance = TREE_OPERAND (postfix_expression, 0);
6391 tree fn = TREE_OPERAND (postfix_expression, 1);
6393 if (processing_template_decl
6394 && (type_dependent_expression_p (instance)
6395 || (!BASELINK_P (fn)
6396 && TREE_CODE (fn) != FIELD_DECL)
6397 || type_dependent_expression_p (fn)
6398 || any_type_dependent_arguments_p (args)))
6400 postfix_expression
6401 = build_nt_call_vec (postfix_expression, args);
6402 release_tree_vector (args);
6403 break;
6406 if (BASELINK_P (fn))
6408 postfix_expression
6409 = (build_new_method_call
6410 (instance, fn, &args, NULL_TREE,
6411 (idk == CP_ID_KIND_QUALIFIED
6412 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6413 : LOOKUP_NORMAL),
6414 /*fn_p=*/NULL,
6415 complain));
6417 else
6418 postfix_expression
6419 = finish_call_expr (postfix_expression, &args,
6420 /*disallow_virtual=*/false,
6421 /*koenig_p=*/false,
6422 complain);
6424 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6425 || TREE_CODE (postfix_expression) == MEMBER_REF
6426 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6427 postfix_expression = (build_offset_ref_call_from_tree
6428 (postfix_expression, &args,
6429 complain));
6430 else if (idk == CP_ID_KIND_QUALIFIED)
6431 /* A call to a static class member, or a namespace-scope
6432 function. */
6433 postfix_expression
6434 = finish_call_expr (postfix_expression, &args,
6435 /*disallow_virtual=*/true,
6436 koenig_p,
6437 complain);
6438 else
6439 /* All other function calls. */
6440 postfix_expression
6441 = finish_call_expr (postfix_expression, &args,
6442 /*disallow_virtual=*/false,
6443 koenig_p,
6444 complain);
6446 protected_set_expr_location (postfix_expression, token->location);
6448 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6449 idk = CP_ID_KIND_NONE;
6451 release_tree_vector (args);
6453 break;
6455 case CPP_DOT:
6456 case CPP_DEREF:
6457 /* postfix-expression . template [opt] id-expression
6458 postfix-expression . pseudo-destructor-name
6459 postfix-expression -> template [opt] id-expression
6460 postfix-expression -> pseudo-destructor-name */
6462 /* Consume the `.' or `->' operator. */
6463 cp_lexer_consume_token (parser->lexer);
6465 postfix_expression
6466 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6467 postfix_expression,
6468 false, &idk, loc);
6470 is_member_access = true;
6471 break;
6473 case CPP_PLUS_PLUS:
6474 /* postfix-expression ++ */
6475 /* Consume the `++' token. */
6476 cp_lexer_consume_token (parser->lexer);
6477 /* Generate a representation for the complete expression. */
6478 postfix_expression
6479 = finish_increment_expr (postfix_expression,
6480 POSTINCREMENT_EXPR);
6481 /* Increments may not appear in constant-expressions. */
6482 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6483 postfix_expression = error_mark_node;
6484 idk = CP_ID_KIND_NONE;
6485 is_member_access = false;
6486 break;
6488 case CPP_MINUS_MINUS:
6489 /* postfix-expression -- */
6490 /* Consume the `--' token. */
6491 cp_lexer_consume_token (parser->lexer);
6492 /* Generate a representation for the complete expression. */
6493 postfix_expression
6494 = finish_increment_expr (postfix_expression,
6495 POSTDECREMENT_EXPR);
6496 /* Decrements may not appear in constant-expressions. */
6497 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6498 postfix_expression = error_mark_node;
6499 idk = CP_ID_KIND_NONE;
6500 is_member_access = false;
6501 break;
6503 default:
6504 if (pidk_return != NULL)
6505 * pidk_return = idk;
6506 if (member_access_only_p)
6507 return is_member_access? postfix_expression : error_mark_node;
6508 else
6509 return postfix_expression;
6513 /* We should never get here. */
6514 gcc_unreachable ();
6515 return error_mark_node;
6518 /* This function parses Cilk Plus array notations. If a normal array expr. is
6519 parsed then the array index is passed back to the caller through *INIT_INDEX
6520 and the function returns a NULL_TREE. If array notation expr. is parsed,
6521 then *INIT_INDEX is ignored by the caller and the function returns
6522 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6523 error_mark_node. */
6525 static tree
6526 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6527 tree array_value)
6529 cp_token *token = NULL;
6530 tree length_index, stride = NULL_TREE, value_tree, array_type;
6531 if (!array_value || array_value == error_mark_node)
6533 cp_parser_skip_to_end_of_statement (parser);
6534 return error_mark_node;
6537 array_type = TREE_TYPE (array_value);
6539 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6540 parser->colon_corrects_to_scope_p = false;
6541 token = cp_lexer_peek_token (parser->lexer);
6543 if (!token)
6545 cp_parser_error (parser, "expected %<:%> or numeral");
6546 return error_mark_node;
6548 else if (token->type == CPP_COLON)
6550 /* Consume the ':'. */
6551 cp_lexer_consume_token (parser->lexer);
6553 /* If we are here, then we have a case like this A[:]. */
6554 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6556 cp_parser_error (parser, "expected %<]%>");
6557 cp_parser_skip_to_end_of_statement (parser);
6558 return error_mark_node;
6560 *init_index = NULL_TREE;
6561 stride = NULL_TREE;
6562 length_index = NULL_TREE;
6564 else
6566 /* If we are here, then there are three valid possibilities:
6567 1. ARRAY [ EXP ]
6568 2. ARRAY [ EXP : EXP ]
6569 3. ARRAY [ EXP : EXP : EXP ] */
6571 *init_index = cp_parser_expression (parser);
6572 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6574 /* This indicates that we have a normal array expression. */
6575 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6576 return NULL_TREE;
6579 /* Consume the ':'. */
6580 cp_lexer_consume_token (parser->lexer);
6581 length_index = cp_parser_expression (parser);
6582 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6584 cp_lexer_consume_token (parser->lexer);
6585 stride = cp_parser_expression (parser);
6588 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6590 if (*init_index == error_mark_node || length_index == error_mark_node
6591 || stride == error_mark_node || array_type == error_mark_node)
6593 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6594 cp_lexer_consume_token (parser->lexer);
6595 return error_mark_node;
6597 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6599 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6600 length_index, stride, array_type);
6601 return value_tree;
6604 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6605 by cp_parser_builtin_offsetof. We're looking for
6607 postfix-expression [ expression ]
6608 postfix-expression [ braced-init-list ] (C++11)
6610 FOR_OFFSETOF is set if we're being called in that context, which
6611 changes how we deal with integer constant expressions. */
6613 static tree
6614 cp_parser_postfix_open_square_expression (cp_parser *parser,
6615 tree postfix_expression,
6616 bool for_offsetof,
6617 bool decltype_p)
6619 tree index = NULL_TREE;
6620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6621 bool saved_greater_than_is_operator_p;
6623 /* Consume the `[' token. */
6624 cp_lexer_consume_token (parser->lexer);
6626 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6627 parser->greater_than_is_operator_p = true;
6629 /* Parse the index expression. */
6630 /* ??? For offsetof, there is a question of what to allow here. If
6631 offsetof is not being used in an integral constant expression context,
6632 then we *could* get the right answer by computing the value at runtime.
6633 If we are in an integral constant expression context, then we might
6634 could accept any constant expression; hard to say without analysis.
6635 Rather than open the barn door too wide right away, allow only integer
6636 constant expressions here. */
6637 if (for_offsetof)
6638 index = cp_parser_constant_expression (parser);
6639 else
6641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6643 bool expr_nonconst_p;
6644 cp_lexer_set_source_position (parser->lexer);
6645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6646 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6647 if (flag_cilkplus
6648 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6650 error_at (cp_lexer_peek_token (parser->lexer)->location,
6651 "braced list index is not allowed with array "
6652 "notation");
6653 cp_parser_skip_to_end_of_statement (parser);
6654 return error_mark_node;
6657 else if (flag_cilkplus)
6659 /* Here are have these two options:
6660 ARRAY[EXP : EXP] - Array notation expr with default
6661 stride of 1.
6662 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6663 stride. */
6664 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6665 postfix_expression);
6666 if (an_exp)
6667 return an_exp;
6669 else
6670 index = cp_parser_expression (parser);
6673 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6675 /* Look for the closing `]'. */
6676 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6678 /* Build the ARRAY_REF. */
6679 postfix_expression = grok_array_decl (loc, postfix_expression,
6680 index, decltype_p);
6682 /* When not doing offsetof, array references are not permitted in
6683 constant-expressions. */
6684 if (!for_offsetof
6685 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6686 postfix_expression = error_mark_node;
6688 return postfix_expression;
6691 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6692 by cp_parser_builtin_offsetof. We're looking for
6694 postfix-expression . template [opt] id-expression
6695 postfix-expression . pseudo-destructor-name
6696 postfix-expression -> template [opt] id-expression
6697 postfix-expression -> pseudo-destructor-name
6699 FOR_OFFSETOF is set if we're being called in that context. That sorta
6700 limits what of the above we'll actually accept, but nevermind.
6701 TOKEN_TYPE is the "." or "->" token, which will already have been
6702 removed from the stream. */
6704 static tree
6705 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6706 enum cpp_ttype token_type,
6707 tree postfix_expression,
6708 bool for_offsetof, cp_id_kind *idk,
6709 location_t location)
6711 tree name;
6712 bool dependent_p;
6713 bool pseudo_destructor_p;
6714 tree scope = NULL_TREE;
6716 /* If this is a `->' operator, dereference the pointer. */
6717 if (token_type == CPP_DEREF)
6718 postfix_expression = build_x_arrow (location, postfix_expression,
6719 tf_warning_or_error);
6720 /* Check to see whether or not the expression is type-dependent. */
6721 dependent_p = type_dependent_expression_p (postfix_expression);
6722 /* The identifier following the `->' or `.' is not qualified. */
6723 parser->scope = NULL_TREE;
6724 parser->qualifying_scope = NULL_TREE;
6725 parser->object_scope = NULL_TREE;
6726 *idk = CP_ID_KIND_NONE;
6728 /* Enter the scope corresponding to the type of the object
6729 given by the POSTFIX_EXPRESSION. */
6730 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6732 scope = TREE_TYPE (postfix_expression);
6733 /* According to the standard, no expression should ever have
6734 reference type. Unfortunately, we do not currently match
6735 the standard in this respect in that our internal representation
6736 of an expression may have reference type even when the standard
6737 says it does not. Therefore, we have to manually obtain the
6738 underlying type here. */
6739 scope = non_reference (scope);
6740 /* The type of the POSTFIX_EXPRESSION must be complete. */
6741 if (scope == unknown_type_node)
6743 error_at (location, "%qE does not have class type",
6744 postfix_expression);
6745 scope = NULL_TREE;
6747 /* Unlike the object expression in other contexts, *this is not
6748 required to be of complete type for purposes of class member
6749 access (5.2.5) outside the member function body. */
6750 else if (postfix_expression != current_class_ref
6751 && !(processing_template_decl && scope == current_class_type))
6752 scope = complete_type_or_else (scope, NULL_TREE);
6753 /* Let the name lookup machinery know that we are processing a
6754 class member access expression. */
6755 parser->context->object_type = scope;
6756 /* If something went wrong, we want to be able to discern that case,
6757 as opposed to the case where there was no SCOPE due to the type
6758 of expression being dependent. */
6759 if (!scope)
6760 scope = error_mark_node;
6761 /* If the SCOPE was erroneous, make the various semantic analysis
6762 functions exit quickly -- and without issuing additional error
6763 messages. */
6764 if (scope == error_mark_node)
6765 postfix_expression = error_mark_node;
6768 /* Assume this expression is not a pseudo-destructor access. */
6769 pseudo_destructor_p = false;
6771 /* If the SCOPE is a scalar type, then, if this is a valid program,
6772 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6773 is type dependent, it can be pseudo-destructor-name or something else.
6774 Try to parse it as pseudo-destructor-name first. */
6775 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6777 tree s;
6778 tree type;
6780 cp_parser_parse_tentatively (parser);
6781 /* Parse the pseudo-destructor-name. */
6782 s = NULL_TREE;
6783 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6784 &s, &type);
6785 if (dependent_p
6786 && (cp_parser_error_occurred (parser)
6787 || !SCALAR_TYPE_P (type)))
6788 cp_parser_abort_tentative_parse (parser);
6789 else if (cp_parser_parse_definitely (parser))
6791 pseudo_destructor_p = true;
6792 postfix_expression
6793 = finish_pseudo_destructor_expr (postfix_expression,
6794 s, type, location);
6798 if (!pseudo_destructor_p)
6800 /* If the SCOPE is not a scalar type, we are looking at an
6801 ordinary class member access expression, rather than a
6802 pseudo-destructor-name. */
6803 bool template_p;
6804 cp_token *token = cp_lexer_peek_token (parser->lexer);
6805 /* Parse the id-expression. */
6806 name = (cp_parser_id_expression
6807 (parser,
6808 cp_parser_optional_template_keyword (parser),
6809 /*check_dependency_p=*/true,
6810 &template_p,
6811 /*declarator_p=*/false,
6812 /*optional_p=*/false));
6813 /* In general, build a SCOPE_REF if the member name is qualified.
6814 However, if the name was not dependent and has already been
6815 resolved; there is no need to build the SCOPE_REF. For example;
6817 struct X { void f(); };
6818 template <typename T> void f(T* t) { t->X::f(); }
6820 Even though "t" is dependent, "X::f" is not and has been resolved
6821 to a BASELINK; there is no need to include scope information. */
6823 /* But we do need to remember that there was an explicit scope for
6824 virtual function calls. */
6825 if (parser->scope)
6826 *idk = CP_ID_KIND_QUALIFIED;
6828 /* If the name is a template-id that names a type, we will get a
6829 TYPE_DECL here. That is invalid code. */
6830 if (TREE_CODE (name) == TYPE_DECL)
6832 error_at (token->location, "invalid use of %qD", name);
6833 postfix_expression = error_mark_node;
6835 else
6837 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6839 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6841 error_at (token->location, "%<%D::%D%> is not a class member",
6842 parser->scope, name);
6843 postfix_expression = error_mark_node;
6845 else
6846 name = build_qualified_name (/*type=*/NULL_TREE,
6847 parser->scope,
6848 name,
6849 template_p);
6850 parser->scope = NULL_TREE;
6851 parser->qualifying_scope = NULL_TREE;
6852 parser->object_scope = NULL_TREE;
6854 if (parser->scope && name && BASELINK_P (name))
6855 adjust_result_of_qualified_name_lookup
6856 (name, parser->scope, scope);
6857 postfix_expression
6858 = finish_class_member_access_expr (postfix_expression, name,
6859 template_p,
6860 tf_warning_or_error);
6864 /* We no longer need to look up names in the scope of the object on
6865 the left-hand side of the `.' or `->' operator. */
6866 parser->context->object_type = NULL_TREE;
6868 /* Outside of offsetof, these operators may not appear in
6869 constant-expressions. */
6870 if (!for_offsetof
6871 && (cp_parser_non_integral_constant_expression
6872 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6873 postfix_expression = error_mark_node;
6875 return postfix_expression;
6878 /* Cache of LITERAL_ZERO_P constants. */
6880 static GTY(()) tree literal_zeros[itk_none];
6882 /* Parse a parenthesized expression-list.
6884 expression-list:
6885 assignment-expression
6886 expression-list, assignment-expression
6888 attribute-list:
6889 expression-list
6890 identifier
6891 identifier, expression-list
6893 CAST_P is true if this expression is the target of a cast.
6895 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6896 argument pack.
6898 Returns a vector of trees. Each element is a representation of an
6899 assignment-expression. NULL is returned if the ( and or ) are
6900 missing. An empty, but allocated, vector is returned on no
6901 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6902 if we are parsing an attribute list for an attribute that wants a
6903 plain identifier argument, normal_attr for an attribute that wants
6904 an expression, or non_attr if we aren't parsing an attribute list. If
6905 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6906 not all of the expressions in the list were constant.
6907 WANT_LITERAL_ZERO_P is true if the caller is interested in
6908 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6909 immediately, this can be removed. */
6911 static vec<tree, va_gc> *
6912 cp_parser_parenthesized_expression_list (cp_parser* parser,
6913 int is_attribute_list,
6914 bool cast_p,
6915 bool allow_expansion_p,
6916 bool *non_constant_p,
6917 bool want_literal_zero_p)
6919 vec<tree, va_gc> *expression_list;
6920 bool fold_expr_p = is_attribute_list != non_attr;
6921 tree identifier = NULL_TREE;
6922 bool saved_greater_than_is_operator_p;
6924 /* Assume all the expressions will be constant. */
6925 if (non_constant_p)
6926 *non_constant_p = false;
6928 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6929 return NULL;
6931 expression_list = make_tree_vector ();
6933 /* Within a parenthesized expression, a `>' token is always
6934 the greater-than operator. */
6935 saved_greater_than_is_operator_p
6936 = parser->greater_than_is_operator_p;
6937 parser->greater_than_is_operator_p = true;
6939 /* Consume expressions until there are no more. */
6940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6941 while (true)
6943 tree expr;
6945 /* At the beginning of attribute lists, check to see if the
6946 next token is an identifier. */
6947 if (is_attribute_list == id_attr
6948 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6950 cp_token *token;
6952 /* Consume the identifier. */
6953 token = cp_lexer_consume_token (parser->lexer);
6954 /* Save the identifier. */
6955 identifier = token->u.value;
6957 else
6959 bool expr_non_constant_p;
6961 /* Parse the next assignment-expression. */
6962 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6964 /* A braced-init-list. */
6965 cp_lexer_set_source_position (parser->lexer);
6966 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6967 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6968 if (non_constant_p && expr_non_constant_p)
6969 *non_constant_p = true;
6971 else if (non_constant_p)
6973 expr = (cp_parser_constant_expression
6974 (parser, /*allow_non_constant_p=*/true,
6975 &expr_non_constant_p));
6976 if (expr_non_constant_p)
6977 *non_constant_p = true;
6979 else
6981 expr = NULL_TREE;
6982 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6983 switch (tok->type)
6985 case CPP_NUMBER:
6986 case CPP_CHAR:
6987 case CPP_WCHAR:
6988 case CPP_CHAR16:
6989 case CPP_CHAR32:
6990 /* If a parameter is literal zero alone, remember it
6991 for -Wmemset-transposed-args warning. */
6992 if (integer_zerop (tok->u.value)
6993 && !TREE_OVERFLOW (tok->u.value)
6994 && want_literal_zero_p
6995 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6996 == CPP_COMMA
6997 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6998 == CPP_CLOSE_PAREN))
7000 unsigned int i;
7001 for (i = 0; i < itk_none; ++i)
7002 if (TREE_TYPE (tok->u.value) == integer_types[i])
7003 break;
7004 if (i < itk_none && literal_zeros[i])
7005 expr = literal_zeros[i];
7006 else
7008 expr = copy_node (tok->u.value);
7009 LITERAL_ZERO_P (expr) = 1;
7010 if (i < itk_none)
7011 literal_zeros[i] = expr;
7013 /* Consume the 0 token (or '\0', 0LL etc.). */
7014 cp_lexer_consume_token (parser->lexer);
7016 break;
7017 default:
7018 break;
7020 if (expr == NULL_TREE)
7021 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7022 cast_p);
7025 if (fold_expr_p)
7026 expr = instantiate_non_dependent_expr (expr);
7028 /* If we have an ellipsis, then this is an expression
7029 expansion. */
7030 if (allow_expansion_p
7031 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7033 /* Consume the `...'. */
7034 cp_lexer_consume_token (parser->lexer);
7036 /* Build the argument pack. */
7037 expr = make_pack_expansion (expr);
7040 /* Add it to the list. We add error_mark_node
7041 expressions to the list, so that we can still tell if
7042 the correct form for a parenthesized expression-list
7043 is found. That gives better errors. */
7044 vec_safe_push (expression_list, expr);
7046 if (expr == error_mark_node)
7047 goto skip_comma;
7050 /* After the first item, attribute lists look the same as
7051 expression lists. */
7052 is_attribute_list = non_attr;
7054 get_comma:;
7055 /* If the next token isn't a `,', then we are done. */
7056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7057 break;
7059 /* Otherwise, consume the `,' and keep going. */
7060 cp_lexer_consume_token (parser->lexer);
7063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7065 int ending;
7067 skip_comma:;
7068 /* We try and resync to an unnested comma, as that will give the
7069 user better diagnostics. */
7070 ending = cp_parser_skip_to_closing_parenthesis (parser,
7071 /*recovering=*/true,
7072 /*or_comma=*/true,
7073 /*consume_paren=*/true);
7074 if (ending < 0)
7075 goto get_comma;
7076 if (!ending)
7078 parser->greater_than_is_operator_p
7079 = saved_greater_than_is_operator_p;
7080 return NULL;
7084 parser->greater_than_is_operator_p
7085 = saved_greater_than_is_operator_p;
7087 if (identifier)
7088 vec_safe_insert (expression_list, 0, identifier);
7090 return expression_list;
7093 /* Parse a pseudo-destructor-name.
7095 pseudo-destructor-name:
7096 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7097 :: [opt] nested-name-specifier template template-id :: ~ type-name
7098 :: [opt] nested-name-specifier [opt] ~ type-name
7100 If either of the first two productions is used, sets *SCOPE to the
7101 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7102 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7103 or ERROR_MARK_NODE if the parse fails. */
7105 static void
7106 cp_parser_pseudo_destructor_name (cp_parser* parser,
7107 tree object,
7108 tree* scope,
7109 tree* type)
7111 bool nested_name_specifier_p;
7113 /* Handle ~auto. */
7114 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7115 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7116 && !type_dependent_expression_p (object))
7118 if (cxx_dialect < cxx14)
7119 pedwarn (input_location, 0,
7120 "%<~auto%> only available with "
7121 "-std=c++14 or -std=gnu++14");
7122 cp_lexer_consume_token (parser->lexer);
7123 cp_lexer_consume_token (parser->lexer);
7124 *scope = NULL_TREE;
7125 *type = TREE_TYPE (object);
7126 return;
7129 /* Assume that things will not work out. */
7130 *type = error_mark_node;
7132 /* Look for the optional `::' operator. */
7133 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7134 /* Look for the optional nested-name-specifier. */
7135 nested_name_specifier_p
7136 = (cp_parser_nested_name_specifier_opt (parser,
7137 /*typename_keyword_p=*/false,
7138 /*check_dependency_p=*/true,
7139 /*type_p=*/false,
7140 /*is_declaration=*/false)
7141 != NULL_TREE);
7142 /* Now, if we saw a nested-name-specifier, we might be doing the
7143 second production. */
7144 if (nested_name_specifier_p
7145 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7147 /* Consume the `template' keyword. */
7148 cp_lexer_consume_token (parser->lexer);
7149 /* Parse the template-id. */
7150 cp_parser_template_id (parser,
7151 /*template_keyword_p=*/true,
7152 /*check_dependency_p=*/false,
7153 class_type,
7154 /*is_declaration=*/true);
7155 /* Look for the `::' token. */
7156 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7158 /* If the next token is not a `~', then there might be some
7159 additional qualification. */
7160 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7162 /* At this point, we're looking for "type-name :: ~". The type-name
7163 must not be a class-name, since this is a pseudo-destructor. So,
7164 it must be either an enum-name, or a typedef-name -- both of which
7165 are just identifiers. So, we peek ahead to check that the "::"
7166 and "~" tokens are present; if they are not, then we can avoid
7167 calling type_name. */
7168 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7169 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7170 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7172 cp_parser_error (parser, "non-scalar type");
7173 return;
7176 /* Look for the type-name. */
7177 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7178 if (*scope == error_mark_node)
7179 return;
7181 /* Look for the `::' token. */
7182 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7184 else
7185 *scope = NULL_TREE;
7187 /* Look for the `~'. */
7188 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7190 /* Once we see the ~, this has to be a pseudo-destructor. */
7191 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7192 cp_parser_commit_to_topmost_tentative_parse (parser);
7194 /* Look for the type-name again. We are not responsible for
7195 checking that it matches the first type-name. */
7196 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7199 /* Parse a unary-expression.
7201 unary-expression:
7202 postfix-expression
7203 ++ cast-expression
7204 -- cast-expression
7205 unary-operator cast-expression
7206 sizeof unary-expression
7207 sizeof ( type-id )
7208 alignof ( type-id ) [C++0x]
7209 new-expression
7210 delete-expression
7212 GNU Extensions:
7214 unary-expression:
7215 __extension__ cast-expression
7216 __alignof__ unary-expression
7217 __alignof__ ( type-id )
7218 alignof unary-expression [C++0x]
7219 __real__ cast-expression
7220 __imag__ cast-expression
7221 && identifier
7222 sizeof ( type-id ) { initializer-list , [opt] }
7223 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7224 __alignof__ ( type-id ) { initializer-list , [opt] }
7226 ADDRESS_P is true iff the unary-expression is appearing as the
7227 operand of the `&' operator. CAST_P is true if this expression is
7228 the target of a cast.
7230 Returns a representation of the expression. */
7232 static tree
7233 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7234 bool address_p, bool cast_p, bool decltype_p)
7236 cp_token *token;
7237 enum tree_code unary_operator;
7239 /* Peek at the next token. */
7240 token = cp_lexer_peek_token (parser->lexer);
7241 /* Some keywords give away the kind of expression. */
7242 if (token->type == CPP_KEYWORD)
7244 enum rid keyword = token->keyword;
7246 switch (keyword)
7248 case RID_ALIGNOF:
7249 case RID_SIZEOF:
7251 tree operand, ret;
7252 enum tree_code op;
7253 location_t first_loc;
7255 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7256 /* Consume the token. */
7257 cp_lexer_consume_token (parser->lexer);
7258 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7259 /* Parse the operand. */
7260 operand = cp_parser_sizeof_operand (parser, keyword);
7262 if (TYPE_P (operand))
7263 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7264 else
7266 /* ISO C++ defines alignof only with types, not with
7267 expressions. So pedwarn if alignof is used with a non-
7268 type expression. However, __alignof__ is ok. */
7269 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7270 pedwarn (token->location, OPT_Wpedantic,
7271 "ISO C++ does not allow %<alignof%> "
7272 "with a non-type");
7274 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7276 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7277 SIZEOF_EXPR with the original operand. */
7278 if (op == SIZEOF_EXPR && ret != error_mark_node)
7280 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7282 if (!processing_template_decl && TYPE_P (operand))
7284 ret = build_min (SIZEOF_EXPR, size_type_node,
7285 build1 (NOP_EXPR, operand,
7286 error_mark_node));
7287 SIZEOF_EXPR_TYPE_P (ret) = 1;
7289 else
7290 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7291 TREE_SIDE_EFFECTS (ret) = 0;
7292 TREE_READONLY (ret) = 1;
7294 SET_EXPR_LOCATION (ret, first_loc);
7296 return ret;
7299 case RID_NEW:
7300 return cp_parser_new_expression (parser);
7302 case RID_DELETE:
7303 return cp_parser_delete_expression (parser);
7305 case RID_EXTENSION:
7307 /* The saved value of the PEDANTIC flag. */
7308 int saved_pedantic;
7309 tree expr;
7311 /* Save away the PEDANTIC flag. */
7312 cp_parser_extension_opt (parser, &saved_pedantic);
7313 /* Parse the cast-expression. */
7314 expr = cp_parser_simple_cast_expression (parser);
7315 /* Restore the PEDANTIC flag. */
7316 pedantic = saved_pedantic;
7318 return expr;
7321 case RID_REALPART:
7322 case RID_IMAGPART:
7324 tree expression;
7326 /* Consume the `__real__' or `__imag__' token. */
7327 cp_lexer_consume_token (parser->lexer);
7328 /* Parse the cast-expression. */
7329 expression = cp_parser_simple_cast_expression (parser);
7330 /* Create the complete representation. */
7331 return build_x_unary_op (token->location,
7332 (keyword == RID_REALPART
7333 ? REALPART_EXPR : IMAGPART_EXPR),
7334 expression,
7335 tf_warning_or_error);
7337 break;
7339 case RID_TRANSACTION_ATOMIC:
7340 case RID_TRANSACTION_RELAXED:
7341 return cp_parser_transaction_expression (parser, keyword);
7343 case RID_NOEXCEPT:
7345 tree expr;
7346 const char *saved_message;
7347 bool saved_integral_constant_expression_p;
7348 bool saved_non_integral_constant_expression_p;
7349 bool saved_greater_than_is_operator_p;
7351 cp_lexer_consume_token (parser->lexer);
7352 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7354 saved_message = parser->type_definition_forbidden_message;
7355 parser->type_definition_forbidden_message
7356 = G_("types may not be defined in %<noexcept%> expressions");
7358 saved_integral_constant_expression_p
7359 = parser->integral_constant_expression_p;
7360 saved_non_integral_constant_expression_p
7361 = parser->non_integral_constant_expression_p;
7362 parser->integral_constant_expression_p = false;
7364 saved_greater_than_is_operator_p
7365 = parser->greater_than_is_operator_p;
7366 parser->greater_than_is_operator_p = true;
7368 ++cp_unevaluated_operand;
7369 ++c_inhibit_evaluation_warnings;
7370 ++cp_noexcept_operand;
7371 expr = cp_parser_expression (parser);
7372 --cp_noexcept_operand;
7373 --c_inhibit_evaluation_warnings;
7374 --cp_unevaluated_operand;
7376 parser->greater_than_is_operator_p
7377 = saved_greater_than_is_operator_p;
7379 parser->integral_constant_expression_p
7380 = saved_integral_constant_expression_p;
7381 parser->non_integral_constant_expression_p
7382 = saved_non_integral_constant_expression_p;
7384 parser->type_definition_forbidden_message = saved_message;
7386 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7387 return finish_noexcept_expr (expr, tf_warning_or_error);
7390 default:
7391 break;
7395 /* Look for the `:: new' and `:: delete', which also signal the
7396 beginning of a new-expression, or delete-expression,
7397 respectively. If the next token is `::', then it might be one of
7398 these. */
7399 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7401 enum rid keyword;
7403 /* See if the token after the `::' is one of the keywords in
7404 which we're interested. */
7405 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7406 /* If it's `new', we have a new-expression. */
7407 if (keyword == RID_NEW)
7408 return cp_parser_new_expression (parser);
7409 /* Similarly, for `delete'. */
7410 else if (keyword == RID_DELETE)
7411 return cp_parser_delete_expression (parser);
7414 /* Look for a unary operator. */
7415 unary_operator = cp_parser_unary_operator (token);
7416 /* The `++' and `--' operators can be handled similarly, even though
7417 they are not technically unary-operators in the grammar. */
7418 if (unary_operator == ERROR_MARK)
7420 if (token->type == CPP_PLUS_PLUS)
7421 unary_operator = PREINCREMENT_EXPR;
7422 else if (token->type == CPP_MINUS_MINUS)
7423 unary_operator = PREDECREMENT_EXPR;
7424 /* Handle the GNU address-of-label extension. */
7425 else if (cp_parser_allow_gnu_extensions_p (parser)
7426 && token->type == CPP_AND_AND)
7428 tree identifier;
7429 tree expression;
7430 location_t loc = token->location;
7432 /* Consume the '&&' token. */
7433 cp_lexer_consume_token (parser->lexer);
7434 /* Look for the identifier. */
7435 identifier = cp_parser_identifier (parser);
7436 /* Create an expression representing the address. */
7437 expression = finish_label_address_expr (identifier, loc);
7438 if (cp_parser_non_integral_constant_expression (parser,
7439 NIC_ADDR_LABEL))
7440 expression = error_mark_node;
7441 return expression;
7444 if (unary_operator != ERROR_MARK)
7446 tree cast_expression;
7447 tree expression = error_mark_node;
7448 non_integral_constant non_constant_p = NIC_NONE;
7449 location_t loc = token->location;
7450 tsubst_flags_t complain = complain_flags (decltype_p);
7452 /* Consume the operator token. */
7453 token = cp_lexer_consume_token (parser->lexer);
7454 /* Parse the cast-expression. */
7455 cast_expression
7456 = cp_parser_cast_expression (parser,
7457 unary_operator == ADDR_EXPR,
7458 /*cast_p=*/false,
7459 /*decltype*/false,
7460 pidk);
7461 /* Now, build an appropriate representation. */
7462 switch (unary_operator)
7464 case INDIRECT_REF:
7465 non_constant_p = NIC_STAR;
7466 expression = build_x_indirect_ref (loc, cast_expression,
7467 RO_UNARY_STAR,
7468 complain);
7469 break;
7471 case ADDR_EXPR:
7472 non_constant_p = NIC_ADDR;
7473 /* Fall through. */
7474 case BIT_NOT_EXPR:
7475 expression = build_x_unary_op (loc, unary_operator,
7476 cast_expression,
7477 complain);
7478 break;
7480 case PREINCREMENT_EXPR:
7481 case PREDECREMENT_EXPR:
7482 non_constant_p = unary_operator == PREINCREMENT_EXPR
7483 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7484 /* Fall through. */
7485 case UNARY_PLUS_EXPR:
7486 case NEGATE_EXPR:
7487 case TRUTH_NOT_EXPR:
7488 expression = finish_unary_op_expr (loc, unary_operator,
7489 cast_expression, complain);
7490 break;
7492 default:
7493 gcc_unreachable ();
7496 if (non_constant_p != NIC_NONE
7497 && cp_parser_non_integral_constant_expression (parser,
7498 non_constant_p))
7499 expression = error_mark_node;
7501 return expression;
7504 return cp_parser_postfix_expression (parser, address_p, cast_p,
7505 /*member_access_only_p=*/false,
7506 decltype_p,
7507 pidk);
7510 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7511 unary-operator, the corresponding tree code is returned. */
7513 static enum tree_code
7514 cp_parser_unary_operator (cp_token* token)
7516 switch (token->type)
7518 case CPP_MULT:
7519 return INDIRECT_REF;
7521 case CPP_AND:
7522 return ADDR_EXPR;
7524 case CPP_PLUS:
7525 return UNARY_PLUS_EXPR;
7527 case CPP_MINUS:
7528 return NEGATE_EXPR;
7530 case CPP_NOT:
7531 return TRUTH_NOT_EXPR;
7533 case CPP_COMPL:
7534 return BIT_NOT_EXPR;
7536 default:
7537 return ERROR_MARK;
7541 /* Parse a new-expression.
7543 new-expression:
7544 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7545 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7547 Returns a representation of the expression. */
7549 static tree
7550 cp_parser_new_expression (cp_parser* parser)
7552 bool global_scope_p;
7553 vec<tree, va_gc> *placement;
7554 tree type;
7555 vec<tree, va_gc> *initializer;
7556 tree nelts = NULL_TREE;
7557 tree ret;
7559 /* Look for the optional `::' operator. */
7560 global_scope_p
7561 = (cp_parser_global_scope_opt (parser,
7562 /*current_scope_valid_p=*/false)
7563 != NULL_TREE);
7564 /* Look for the `new' operator. */
7565 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7566 /* There's no easy way to tell a new-placement from the
7567 `( type-id )' construct. */
7568 cp_parser_parse_tentatively (parser);
7569 /* Look for a new-placement. */
7570 placement = cp_parser_new_placement (parser);
7571 /* If that didn't work out, there's no new-placement. */
7572 if (!cp_parser_parse_definitely (parser))
7574 if (placement != NULL)
7575 release_tree_vector (placement);
7576 placement = NULL;
7579 /* If the next token is a `(', then we have a parenthesized
7580 type-id. */
7581 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7583 cp_token *token;
7584 const char *saved_message = parser->type_definition_forbidden_message;
7586 /* Consume the `('. */
7587 cp_lexer_consume_token (parser->lexer);
7589 /* Parse the type-id. */
7590 parser->type_definition_forbidden_message
7591 = G_("types may not be defined in a new-expression");
7592 type = cp_parser_type_id (parser);
7593 parser->type_definition_forbidden_message = saved_message;
7595 /* Look for the closing `)'. */
7596 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7597 token = cp_lexer_peek_token (parser->lexer);
7598 /* There should not be a direct-new-declarator in this production,
7599 but GCC used to allowed this, so we check and emit a sensible error
7600 message for this case. */
7601 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7603 error_at (token->location,
7604 "array bound forbidden after parenthesized type-id");
7605 inform (token->location,
7606 "try removing the parentheses around the type-id");
7607 cp_parser_direct_new_declarator (parser);
7610 /* Otherwise, there must be a new-type-id. */
7611 else
7612 type = cp_parser_new_type_id (parser, &nelts);
7614 /* If the next token is a `(' or '{', then we have a new-initializer. */
7615 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7616 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7617 initializer = cp_parser_new_initializer (parser);
7618 else
7619 initializer = NULL;
7621 /* A new-expression may not appear in an integral constant
7622 expression. */
7623 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7624 ret = error_mark_node;
7625 else
7627 /* Create a representation of the new-expression. */
7628 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7629 tf_warning_or_error);
7632 if (placement != NULL)
7633 release_tree_vector (placement);
7634 if (initializer != NULL)
7635 release_tree_vector (initializer);
7637 return ret;
7640 /* Parse a new-placement.
7642 new-placement:
7643 ( expression-list )
7645 Returns the same representation as for an expression-list. */
7647 static vec<tree, va_gc> *
7648 cp_parser_new_placement (cp_parser* parser)
7650 vec<tree, va_gc> *expression_list;
7652 /* Parse the expression-list. */
7653 expression_list = (cp_parser_parenthesized_expression_list
7654 (parser, non_attr, /*cast_p=*/false,
7655 /*allow_expansion_p=*/true,
7656 /*non_constant_p=*/NULL));
7658 return expression_list;
7661 /* Parse a new-type-id.
7663 new-type-id:
7664 type-specifier-seq new-declarator [opt]
7666 Returns the TYPE allocated. If the new-type-id indicates an array
7667 type, *NELTS is set to the number of elements in the last array
7668 bound; the TYPE will not include the last array bound. */
7670 static tree
7671 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7673 cp_decl_specifier_seq type_specifier_seq;
7674 cp_declarator *new_declarator;
7675 cp_declarator *declarator;
7676 cp_declarator *outer_declarator;
7677 const char *saved_message;
7679 /* The type-specifier sequence must not contain type definitions.
7680 (It cannot contain declarations of new types either, but if they
7681 are not definitions we will catch that because they are not
7682 complete.) */
7683 saved_message = parser->type_definition_forbidden_message;
7684 parser->type_definition_forbidden_message
7685 = G_("types may not be defined in a new-type-id");
7686 /* Parse the type-specifier-seq. */
7687 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7688 /*is_trailing_return=*/false,
7689 &type_specifier_seq);
7690 /* Restore the old message. */
7691 parser->type_definition_forbidden_message = saved_message;
7693 if (type_specifier_seq.type == error_mark_node)
7694 return error_mark_node;
7696 /* Parse the new-declarator. */
7697 new_declarator = cp_parser_new_declarator_opt (parser);
7699 /* Determine the number of elements in the last array dimension, if
7700 any. */
7701 *nelts = NULL_TREE;
7702 /* Skip down to the last array dimension. */
7703 declarator = new_declarator;
7704 outer_declarator = NULL;
7705 while (declarator && (declarator->kind == cdk_pointer
7706 || declarator->kind == cdk_ptrmem))
7708 outer_declarator = declarator;
7709 declarator = declarator->declarator;
7711 while (declarator
7712 && declarator->kind == cdk_array
7713 && declarator->declarator
7714 && declarator->declarator->kind == cdk_array)
7716 outer_declarator = declarator;
7717 declarator = declarator->declarator;
7720 if (declarator && declarator->kind == cdk_array)
7722 *nelts = declarator->u.array.bounds;
7723 if (*nelts == error_mark_node)
7724 *nelts = integer_one_node;
7726 if (outer_declarator)
7727 outer_declarator->declarator = declarator->declarator;
7728 else
7729 new_declarator = NULL;
7732 return groktypename (&type_specifier_seq, new_declarator, false);
7735 /* Parse an (optional) new-declarator.
7737 new-declarator:
7738 ptr-operator new-declarator [opt]
7739 direct-new-declarator
7741 Returns the declarator. */
7743 static cp_declarator *
7744 cp_parser_new_declarator_opt (cp_parser* parser)
7746 enum tree_code code;
7747 tree type, std_attributes = NULL_TREE;
7748 cp_cv_quals cv_quals;
7750 /* We don't know if there's a ptr-operator next, or not. */
7751 cp_parser_parse_tentatively (parser);
7752 /* Look for a ptr-operator. */
7753 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7754 /* If that worked, look for more new-declarators. */
7755 if (cp_parser_parse_definitely (parser))
7757 cp_declarator *declarator;
7759 /* Parse another optional declarator. */
7760 declarator = cp_parser_new_declarator_opt (parser);
7762 declarator = cp_parser_make_indirect_declarator
7763 (code, type, cv_quals, declarator, std_attributes);
7765 return declarator;
7768 /* If the next token is a `[', there is a direct-new-declarator. */
7769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7770 return cp_parser_direct_new_declarator (parser);
7772 return NULL;
7775 /* Parse a direct-new-declarator.
7777 direct-new-declarator:
7778 [ expression ]
7779 direct-new-declarator [constant-expression]
7783 static cp_declarator *
7784 cp_parser_direct_new_declarator (cp_parser* parser)
7786 cp_declarator *declarator = NULL;
7788 while (true)
7790 tree expression;
7791 cp_token *token;
7793 /* Look for the opening `['. */
7794 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7796 token = cp_lexer_peek_token (parser->lexer);
7797 expression = cp_parser_expression (parser);
7798 /* The standard requires that the expression have integral
7799 type. DR 74 adds enumeration types. We believe that the
7800 real intent is that these expressions be handled like the
7801 expression in a `switch' condition, which also allows
7802 classes with a single conversion to integral or
7803 enumeration type. */
7804 if (!processing_template_decl)
7806 expression
7807 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7808 expression,
7809 /*complain=*/true);
7810 if (!expression)
7812 error_at (token->location,
7813 "expression in new-declarator must have integral "
7814 "or enumeration type");
7815 expression = error_mark_node;
7819 /* Look for the closing `]'. */
7820 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7822 /* Add this bound to the declarator. */
7823 declarator = make_array_declarator (declarator, expression);
7825 /* If the next token is not a `[', then there are no more
7826 bounds. */
7827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7828 break;
7831 return declarator;
7834 /* Parse a new-initializer.
7836 new-initializer:
7837 ( expression-list [opt] )
7838 braced-init-list
7840 Returns a representation of the expression-list. */
7842 static vec<tree, va_gc> *
7843 cp_parser_new_initializer (cp_parser* parser)
7845 vec<tree, va_gc> *expression_list;
7847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7849 tree t;
7850 bool expr_non_constant_p;
7851 cp_lexer_set_source_position (parser->lexer);
7852 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7853 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7854 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7855 expression_list = make_tree_vector_single (t);
7857 else
7858 expression_list = (cp_parser_parenthesized_expression_list
7859 (parser, non_attr, /*cast_p=*/false,
7860 /*allow_expansion_p=*/true,
7861 /*non_constant_p=*/NULL));
7863 return expression_list;
7866 /* Parse a delete-expression.
7868 delete-expression:
7869 :: [opt] delete cast-expression
7870 :: [opt] delete [ ] cast-expression
7872 Returns a representation of the expression. */
7874 static tree
7875 cp_parser_delete_expression (cp_parser* parser)
7877 bool global_scope_p;
7878 bool array_p;
7879 tree expression;
7881 /* Look for the optional `::' operator. */
7882 global_scope_p
7883 = (cp_parser_global_scope_opt (parser,
7884 /*current_scope_valid_p=*/false)
7885 != NULL_TREE);
7886 /* Look for the `delete' keyword. */
7887 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7888 /* See if the array syntax is in use. */
7889 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7891 /* Consume the `[' token. */
7892 cp_lexer_consume_token (parser->lexer);
7893 /* Look for the `]' token. */
7894 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7895 /* Remember that this is the `[]' construct. */
7896 array_p = true;
7898 else
7899 array_p = false;
7901 /* Parse the cast-expression. */
7902 expression = cp_parser_simple_cast_expression (parser);
7904 /* A delete-expression may not appear in an integral constant
7905 expression. */
7906 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7907 return error_mark_node;
7909 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7910 tf_warning_or_error);
7913 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7914 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7915 0 otherwise. */
7917 static int
7918 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7920 cp_token *token = cp_lexer_peek_token (parser->lexer);
7921 switch (token->type)
7923 case CPP_COMMA:
7924 case CPP_SEMICOLON:
7925 case CPP_QUERY:
7926 case CPP_COLON:
7927 case CPP_CLOSE_SQUARE:
7928 case CPP_CLOSE_PAREN:
7929 case CPP_CLOSE_BRACE:
7930 case CPP_OPEN_BRACE:
7931 case CPP_DOT:
7932 case CPP_DOT_STAR:
7933 case CPP_DEREF:
7934 case CPP_DEREF_STAR:
7935 case CPP_DIV:
7936 case CPP_MOD:
7937 case CPP_LSHIFT:
7938 case CPP_RSHIFT:
7939 case CPP_LESS:
7940 case CPP_GREATER:
7941 case CPP_LESS_EQ:
7942 case CPP_GREATER_EQ:
7943 case CPP_EQ_EQ:
7944 case CPP_NOT_EQ:
7945 case CPP_EQ:
7946 case CPP_MULT_EQ:
7947 case CPP_DIV_EQ:
7948 case CPP_MOD_EQ:
7949 case CPP_PLUS_EQ:
7950 case CPP_MINUS_EQ:
7951 case CPP_RSHIFT_EQ:
7952 case CPP_LSHIFT_EQ:
7953 case CPP_AND_EQ:
7954 case CPP_XOR_EQ:
7955 case CPP_OR_EQ:
7956 case CPP_XOR:
7957 case CPP_OR:
7958 case CPP_OR_OR:
7959 case CPP_EOF:
7960 case CPP_ELLIPSIS:
7961 return 0;
7963 case CPP_OPEN_PAREN:
7964 /* In ((type ()) () the last () isn't a valid cast-expression,
7965 so the whole must be parsed as postfix-expression. */
7966 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7967 != CPP_CLOSE_PAREN;
7969 case CPP_OPEN_SQUARE:
7970 /* '[' may start a primary-expression in obj-c++ and in C++11,
7971 as a lambda-expression, eg, '(void)[]{}'. */
7972 if (cxx_dialect >= cxx11)
7973 return -1;
7974 return c_dialect_objc ();
7976 case CPP_PLUS_PLUS:
7977 case CPP_MINUS_MINUS:
7978 /* '++' and '--' may or may not start a cast-expression:
7980 struct T { void operator++(int); };
7981 void f() { (T())++; }
7985 int a;
7986 (int)++a; */
7987 return -1;
7989 default:
7990 return 1;
7994 /* Parse a cast-expression.
7996 cast-expression:
7997 unary-expression
7998 ( type-id ) cast-expression
8000 ADDRESS_P is true iff the unary-expression is appearing as the
8001 operand of the `&' operator. CAST_P is true if this expression is
8002 the target of a cast.
8004 Returns a representation of the expression. */
8006 static tree
8007 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8008 bool decltype_p, cp_id_kind * pidk)
8010 /* If it's a `(', then we might be looking at a cast. */
8011 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8013 tree type = NULL_TREE;
8014 tree expr = NULL_TREE;
8015 int cast_expression = 0;
8016 const char *saved_message;
8018 /* There's no way to know yet whether or not this is a cast.
8019 For example, `(int (3))' is a unary-expression, while `(int)
8020 3' is a cast. So, we resort to parsing tentatively. */
8021 cp_parser_parse_tentatively (parser);
8022 /* Types may not be defined in a cast. */
8023 saved_message = parser->type_definition_forbidden_message;
8024 parser->type_definition_forbidden_message
8025 = G_("types may not be defined in casts");
8026 /* Consume the `('. */
8027 cp_lexer_consume_token (parser->lexer);
8028 /* A very tricky bit is that `(struct S) { 3 }' is a
8029 compound-literal (which we permit in C++ as an extension).
8030 But, that construct is not a cast-expression -- it is a
8031 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8032 is legal; if the compound-literal were a cast-expression,
8033 you'd need an extra set of parentheses.) But, if we parse
8034 the type-id, and it happens to be a class-specifier, then we
8035 will commit to the parse at that point, because we cannot
8036 undo the action that is done when creating a new class. So,
8037 then we cannot back up and do a postfix-expression.
8039 Another tricky case is the following (c++/29234):
8041 struct S { void operator () (); };
8043 void foo ()
8045 ( S()() );
8048 As a type-id we parse the parenthesized S()() as a function
8049 returning a function, groktypename complains and we cannot
8050 back up in this case either.
8052 Therefore, we scan ahead to the closing `)', and check to see
8053 if the tokens after the `)' can start a cast-expression. Otherwise
8054 we are dealing with an unary-expression, a postfix-expression
8055 or something else.
8057 Yet another tricky case, in C++11, is the following (c++/54891):
8059 (void)[]{};
8061 The issue is that usually, besides the case of lambda-expressions,
8062 the parenthesized type-id cannot be followed by '[', and, eg, we
8063 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8064 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8065 we don't commit, we try a cast-expression, then an unary-expression.
8067 Save tokens so that we can put them back. */
8068 cp_lexer_save_tokens (parser->lexer);
8070 /* We may be looking at a cast-expression. */
8071 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8072 /*consume_paren=*/true))
8073 cast_expression
8074 = cp_parser_tokens_start_cast_expression (parser);
8076 /* Roll back the tokens we skipped. */
8077 cp_lexer_rollback_tokens (parser->lexer);
8078 /* If we aren't looking at a cast-expression, simulate an error so
8079 that the call to cp_parser_error_occurred below returns true. */
8080 if (!cast_expression)
8081 cp_parser_simulate_error (parser);
8082 else
8084 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8085 parser->in_type_id_in_expr_p = true;
8086 /* Look for the type-id. */
8087 type = cp_parser_type_id (parser);
8088 /* Look for the closing `)'. */
8089 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8090 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8093 /* Restore the saved message. */
8094 parser->type_definition_forbidden_message = saved_message;
8096 /* At this point this can only be either a cast or a
8097 parenthesized ctor such as `(T ())' that looks like a cast to
8098 function returning T. */
8099 if (!cp_parser_error_occurred (parser))
8101 /* Only commit if the cast-expression doesn't start with
8102 '++', '--', or '[' in C++11. */
8103 if (cast_expression > 0)
8104 cp_parser_commit_to_topmost_tentative_parse (parser);
8106 expr = cp_parser_cast_expression (parser,
8107 /*address_p=*/false,
8108 /*cast_p=*/true,
8109 /*decltype_p=*/false,
8110 pidk);
8112 if (cp_parser_parse_definitely (parser))
8114 /* Warn about old-style casts, if so requested. */
8115 if (warn_old_style_cast
8116 && !in_system_header_at (input_location)
8117 && !VOID_TYPE_P (type)
8118 && current_lang_name != lang_name_c)
8119 warning (OPT_Wold_style_cast, "use of old-style cast");
8121 /* Only type conversions to integral or enumeration types
8122 can be used in constant-expressions. */
8123 if (!cast_valid_in_integral_constant_expression_p (type)
8124 && cp_parser_non_integral_constant_expression (parser,
8125 NIC_CAST))
8126 return error_mark_node;
8128 /* Perform the cast. */
8129 expr = build_c_cast (input_location, type, expr);
8130 return expr;
8133 else
8134 cp_parser_abort_tentative_parse (parser);
8137 /* If we get here, then it's not a cast, so it must be a
8138 unary-expression. */
8139 return cp_parser_unary_expression (parser, pidk, address_p,
8140 cast_p, decltype_p);
8143 /* Parse a binary expression of the general form:
8145 pm-expression:
8146 cast-expression
8147 pm-expression .* cast-expression
8148 pm-expression ->* cast-expression
8150 multiplicative-expression:
8151 pm-expression
8152 multiplicative-expression * pm-expression
8153 multiplicative-expression / pm-expression
8154 multiplicative-expression % pm-expression
8156 additive-expression:
8157 multiplicative-expression
8158 additive-expression + multiplicative-expression
8159 additive-expression - multiplicative-expression
8161 shift-expression:
8162 additive-expression
8163 shift-expression << additive-expression
8164 shift-expression >> additive-expression
8166 relational-expression:
8167 shift-expression
8168 relational-expression < shift-expression
8169 relational-expression > shift-expression
8170 relational-expression <= shift-expression
8171 relational-expression >= shift-expression
8173 GNU Extension:
8175 relational-expression:
8176 relational-expression <? shift-expression
8177 relational-expression >? shift-expression
8179 equality-expression:
8180 relational-expression
8181 equality-expression == relational-expression
8182 equality-expression != relational-expression
8184 and-expression:
8185 equality-expression
8186 and-expression & equality-expression
8188 exclusive-or-expression:
8189 and-expression
8190 exclusive-or-expression ^ and-expression
8192 inclusive-or-expression:
8193 exclusive-or-expression
8194 inclusive-or-expression | exclusive-or-expression
8196 logical-and-expression:
8197 inclusive-or-expression
8198 logical-and-expression && inclusive-or-expression
8200 logical-or-expression:
8201 logical-and-expression
8202 logical-or-expression || logical-and-expression
8204 All these are implemented with a single function like:
8206 binary-expression:
8207 simple-cast-expression
8208 binary-expression <token> binary-expression
8210 CAST_P is true if this expression is the target of a cast.
8212 The binops_by_token map is used to get the tree codes for each <token> type.
8213 binary-expressions are associated according to a precedence table. */
8215 #define TOKEN_PRECEDENCE(token) \
8216 (((token->type == CPP_GREATER \
8217 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8218 && !parser->greater_than_is_operator_p) \
8219 ? PREC_NOT_OPERATOR \
8220 : binops_by_token[token->type].prec)
8222 static tree
8223 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8224 bool no_toplevel_fold_p,
8225 bool decltype_p,
8226 enum cp_parser_prec prec,
8227 cp_id_kind * pidk)
8229 cp_parser_expression_stack stack;
8230 cp_parser_expression_stack_entry *sp = &stack[0];
8231 cp_parser_expression_stack_entry current;
8232 tree rhs;
8233 cp_token *token;
8234 enum tree_code rhs_type;
8235 enum cp_parser_prec new_prec, lookahead_prec;
8236 tree overload;
8238 /* Parse the first expression. */
8239 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8240 ? TRUTH_NOT_EXPR : ERROR_MARK);
8241 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8242 cast_p, decltype_p, pidk);
8243 current.prec = prec;
8245 if (cp_parser_error_occurred (parser))
8246 return error_mark_node;
8248 for (;;)
8250 /* Get an operator token. */
8251 token = cp_lexer_peek_token (parser->lexer);
8253 if (warn_cxx0x_compat
8254 && token->type == CPP_RSHIFT
8255 && !parser->greater_than_is_operator_p)
8257 if (warning_at (token->location, OPT_Wc__0x_compat,
8258 "%<>>%> operator is treated"
8259 " as two right angle brackets in C++11"))
8260 inform (token->location,
8261 "suggest parentheses around %<>>%> expression");
8264 new_prec = TOKEN_PRECEDENCE (token);
8266 /* Popping an entry off the stack means we completed a subexpression:
8267 - either we found a token which is not an operator (`>' where it is not
8268 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8269 will happen repeatedly;
8270 - or, we found an operator which has lower priority. This is the case
8271 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8272 parsing `3 * 4'. */
8273 if (new_prec <= current.prec)
8275 if (sp == stack)
8276 break;
8277 else
8278 goto pop;
8281 get_rhs:
8282 current.tree_type = binops_by_token[token->type].tree_type;
8283 current.loc = token->location;
8285 /* We used the operator token. */
8286 cp_lexer_consume_token (parser->lexer);
8288 /* For "false && x" or "true || x", x will never be executed;
8289 disable warnings while evaluating it. */
8290 if (current.tree_type == TRUTH_ANDIF_EXPR)
8291 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8292 else if (current.tree_type == TRUTH_ORIF_EXPR)
8293 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8295 /* Extract another operand. It may be the RHS of this expression
8296 or the LHS of a new, higher priority expression. */
8297 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8298 ? TRUTH_NOT_EXPR : ERROR_MARK);
8299 rhs = cp_parser_simple_cast_expression (parser);
8301 /* Get another operator token. Look up its precedence to avoid
8302 building a useless (immediately popped) stack entry for common
8303 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8304 token = cp_lexer_peek_token (parser->lexer);
8305 lookahead_prec = TOKEN_PRECEDENCE (token);
8306 if (lookahead_prec > new_prec)
8308 /* ... and prepare to parse the RHS of the new, higher priority
8309 expression. Since precedence levels on the stack are
8310 monotonically increasing, we do not have to care about
8311 stack overflows. */
8312 *sp = current;
8313 ++sp;
8314 current.lhs = rhs;
8315 current.lhs_type = rhs_type;
8316 current.prec = new_prec;
8317 new_prec = lookahead_prec;
8318 goto get_rhs;
8320 pop:
8321 lookahead_prec = new_prec;
8322 /* If the stack is not empty, we have parsed into LHS the right side
8323 (`4' in the example above) of an expression we had suspended.
8324 We can use the information on the stack to recover the LHS (`3')
8325 from the stack together with the tree code (`MULT_EXPR'), and
8326 the precedence of the higher level subexpression
8327 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8328 which will be used to actually build the additive expression. */
8329 rhs = current.lhs;
8330 rhs_type = current.lhs_type;
8331 --sp;
8332 current = *sp;
8335 /* Undo the disabling of warnings done above. */
8336 if (current.tree_type == TRUTH_ANDIF_EXPR)
8337 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8338 else if (current.tree_type == TRUTH_ORIF_EXPR)
8339 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8341 if (warn_logical_not_paren
8342 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8343 && current.lhs_type == TRUTH_NOT_EXPR
8344 /* Avoid warning for !!x == y. */
8345 && (TREE_CODE (current.lhs) != NE_EXPR
8346 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8347 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8348 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8349 /* Avoid warning for !b == y where b is boolean. */
8350 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8351 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8352 != BOOLEAN_TYPE))))
8353 /* Avoid warning for !!b == y where b is boolean. */
8354 && (!DECL_P (current.lhs)
8355 || TREE_TYPE (current.lhs) == NULL_TREE
8356 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8357 warn_logical_not_parentheses (current.loc, current.tree_type,
8358 maybe_constant_value (rhs));
8360 overload = NULL;
8361 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8362 ERROR_MARK for everything that is not a binary expression.
8363 This makes warn_about_parentheses miss some warnings that
8364 involve unary operators. For unary expressions we should
8365 pass the correct tree_code unless the unary expression was
8366 surrounded by parentheses.
8368 if (no_toplevel_fold_p
8369 && lookahead_prec <= current.prec
8370 && sp == stack)
8371 current.lhs = build2 (current.tree_type,
8372 TREE_CODE_CLASS (current.tree_type)
8373 == tcc_comparison
8374 ? boolean_type_node : TREE_TYPE (current.lhs),
8375 current.lhs, rhs);
8376 else
8377 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8378 current.lhs, current.lhs_type,
8379 rhs, rhs_type, &overload,
8380 complain_flags (decltype_p));
8381 current.lhs_type = current.tree_type;
8382 if (EXPR_P (current.lhs))
8383 SET_EXPR_LOCATION (current.lhs, current.loc);
8385 /* If the binary operator required the use of an overloaded operator,
8386 then this expression cannot be an integral constant-expression.
8387 An overloaded operator can be used even if both operands are
8388 otherwise permissible in an integral constant-expression if at
8389 least one of the operands is of enumeration type. */
8391 if (overload
8392 && cp_parser_non_integral_constant_expression (parser,
8393 NIC_OVERLOADED))
8394 return error_mark_node;
8397 return current.lhs;
8400 static tree
8401 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8402 bool no_toplevel_fold_p,
8403 enum cp_parser_prec prec,
8404 cp_id_kind * pidk)
8406 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8407 /*decltype*/false, prec, pidk);
8410 /* Parse the `? expression : assignment-expression' part of a
8411 conditional-expression. The LOGICAL_OR_EXPR is the
8412 logical-or-expression that started the conditional-expression.
8413 Returns a representation of the entire conditional-expression.
8415 This routine is used by cp_parser_assignment_expression.
8417 ? expression : assignment-expression
8419 GNU Extensions:
8421 ? : assignment-expression */
8423 static tree
8424 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8426 tree expr;
8427 tree assignment_expr;
8428 struct cp_token *token;
8429 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8431 /* Consume the `?' token. */
8432 cp_lexer_consume_token (parser->lexer);
8433 token = cp_lexer_peek_token (parser->lexer);
8434 if (cp_parser_allow_gnu_extensions_p (parser)
8435 && token->type == CPP_COLON)
8437 pedwarn (token->location, OPT_Wpedantic,
8438 "ISO C++ does not allow ?: with omitted middle operand");
8439 /* Implicit true clause. */
8440 expr = NULL_TREE;
8441 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8442 warn_for_omitted_condop (token->location, logical_or_expr);
8444 else
8446 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8447 parser->colon_corrects_to_scope_p = false;
8448 /* Parse the expression. */
8449 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8450 expr = cp_parser_expression (parser);
8451 c_inhibit_evaluation_warnings +=
8452 ((logical_or_expr == truthvalue_true_node)
8453 - (logical_or_expr == truthvalue_false_node));
8454 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8457 /* The next token should be a `:'. */
8458 cp_parser_require (parser, CPP_COLON, RT_COLON);
8459 /* Parse the assignment-expression. */
8460 assignment_expr = cp_parser_assignment_expression (parser);
8461 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8463 /* Build the conditional-expression. */
8464 return build_x_conditional_expr (loc, logical_or_expr,
8465 expr,
8466 assignment_expr,
8467 tf_warning_or_error);
8470 /* Parse an assignment-expression.
8472 assignment-expression:
8473 conditional-expression
8474 logical-or-expression assignment-operator assignment_expression
8475 throw-expression
8477 CAST_P is true if this expression is the target of a cast.
8478 DECLTYPE_P is true if this expression is the operand of decltype.
8480 Returns a representation for the expression. */
8482 static tree
8483 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8484 bool cast_p, bool decltype_p)
8486 tree expr;
8488 /* If the next token is the `throw' keyword, then we're looking at
8489 a throw-expression. */
8490 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8491 expr = cp_parser_throw_expression (parser);
8492 /* Otherwise, it must be that we are looking at a
8493 logical-or-expression. */
8494 else
8496 /* Parse the binary expressions (logical-or-expression). */
8497 expr = cp_parser_binary_expression (parser, cast_p, false,
8498 decltype_p,
8499 PREC_NOT_OPERATOR, pidk);
8500 /* If the next token is a `?' then we're actually looking at a
8501 conditional-expression. */
8502 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8503 return cp_parser_question_colon_clause (parser, expr);
8504 else
8506 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8508 /* If it's an assignment-operator, we're using the second
8509 production. */
8510 enum tree_code assignment_operator
8511 = cp_parser_assignment_operator_opt (parser);
8512 if (assignment_operator != ERROR_MARK)
8514 bool non_constant_p;
8515 location_t saved_input_location;
8517 /* Parse the right-hand side of the assignment. */
8518 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8520 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8521 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8523 /* An assignment may not appear in a
8524 constant-expression. */
8525 if (cp_parser_non_integral_constant_expression (parser,
8526 NIC_ASSIGNMENT))
8527 return error_mark_node;
8528 /* Build the assignment expression. Its default
8529 location is the location of the '=' token. */
8530 saved_input_location = input_location;
8531 input_location = loc;
8532 expr = build_x_modify_expr (loc, expr,
8533 assignment_operator,
8534 rhs,
8535 complain_flags (decltype_p));
8536 input_location = saved_input_location;
8541 return expr;
8544 /* Parse an (optional) assignment-operator.
8546 assignment-operator: one of
8547 = *= /= %= += -= >>= <<= &= ^= |=
8549 GNU Extension:
8551 assignment-operator: one of
8552 <?= >?=
8554 If the next token is an assignment operator, the corresponding tree
8555 code is returned, and the token is consumed. For example, for
8556 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8557 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8558 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8559 operator, ERROR_MARK is returned. */
8561 static enum tree_code
8562 cp_parser_assignment_operator_opt (cp_parser* parser)
8564 enum tree_code op;
8565 cp_token *token;
8567 /* Peek at the next token. */
8568 token = cp_lexer_peek_token (parser->lexer);
8570 switch (token->type)
8572 case CPP_EQ:
8573 op = NOP_EXPR;
8574 break;
8576 case CPP_MULT_EQ:
8577 op = MULT_EXPR;
8578 break;
8580 case CPP_DIV_EQ:
8581 op = TRUNC_DIV_EXPR;
8582 break;
8584 case CPP_MOD_EQ:
8585 op = TRUNC_MOD_EXPR;
8586 break;
8588 case CPP_PLUS_EQ:
8589 op = PLUS_EXPR;
8590 break;
8592 case CPP_MINUS_EQ:
8593 op = MINUS_EXPR;
8594 break;
8596 case CPP_RSHIFT_EQ:
8597 op = RSHIFT_EXPR;
8598 break;
8600 case CPP_LSHIFT_EQ:
8601 op = LSHIFT_EXPR;
8602 break;
8604 case CPP_AND_EQ:
8605 op = BIT_AND_EXPR;
8606 break;
8608 case CPP_XOR_EQ:
8609 op = BIT_XOR_EXPR;
8610 break;
8612 case CPP_OR_EQ:
8613 op = BIT_IOR_EXPR;
8614 break;
8616 default:
8617 /* Nothing else is an assignment operator. */
8618 op = ERROR_MARK;
8621 /* If it was an assignment operator, consume it. */
8622 if (op != ERROR_MARK)
8623 cp_lexer_consume_token (parser->lexer);
8625 return op;
8628 /* Parse an expression.
8630 expression:
8631 assignment-expression
8632 expression , assignment-expression
8634 CAST_P is true if this expression is the target of a cast.
8635 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8636 except possibly parenthesized or on the RHS of a comma (N3276).
8638 Returns a representation of the expression. */
8640 static tree
8641 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8642 bool cast_p, bool decltype_p)
8644 tree expression = NULL_TREE;
8645 location_t loc = UNKNOWN_LOCATION;
8647 while (true)
8649 tree assignment_expression;
8651 /* Parse the next assignment-expression. */
8652 assignment_expression
8653 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8655 /* We don't create a temporary for a call that is the immediate operand
8656 of decltype or on the RHS of a comma. But when we see a comma, we
8657 need to create a temporary for a call on the LHS. */
8658 if (decltype_p && !processing_template_decl
8659 && TREE_CODE (assignment_expression) == CALL_EXPR
8660 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8661 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8662 assignment_expression
8663 = build_cplus_new (TREE_TYPE (assignment_expression),
8664 assignment_expression, tf_warning_or_error);
8666 /* If this is the first assignment-expression, we can just
8667 save it away. */
8668 if (!expression)
8669 expression = assignment_expression;
8670 else
8671 expression = build_x_compound_expr (loc, expression,
8672 assignment_expression,
8673 complain_flags (decltype_p));
8674 /* If the next token is not a comma, then we are done with the
8675 expression. */
8676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8677 break;
8678 /* Consume the `,'. */
8679 loc = cp_lexer_peek_token (parser->lexer)->location;
8680 cp_lexer_consume_token (parser->lexer);
8681 /* A comma operator cannot appear in a constant-expression. */
8682 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8683 expression = error_mark_node;
8686 return expression;
8689 /* Parse a constant-expression.
8691 constant-expression:
8692 conditional-expression
8694 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8695 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8696 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8697 is false, NON_CONSTANT_P should be NULL. */
8699 static tree
8700 cp_parser_constant_expression (cp_parser* parser,
8701 bool allow_non_constant_p,
8702 bool *non_constant_p)
8704 bool saved_integral_constant_expression_p;
8705 bool saved_allow_non_integral_constant_expression_p;
8706 bool saved_non_integral_constant_expression_p;
8707 tree expression;
8709 /* It might seem that we could simply parse the
8710 conditional-expression, and then check to see if it were
8711 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8712 one that the compiler can figure out is constant, possibly after
8713 doing some simplifications or optimizations. The standard has a
8714 precise definition of constant-expression, and we must honor
8715 that, even though it is somewhat more restrictive.
8717 For example:
8719 int i[(2, 3)];
8721 is not a legal declaration, because `(2, 3)' is not a
8722 constant-expression. The `,' operator is forbidden in a
8723 constant-expression. However, GCC's constant-folding machinery
8724 will fold this operation to an INTEGER_CST for `3'. */
8726 /* Save the old settings. */
8727 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8728 saved_allow_non_integral_constant_expression_p
8729 = parser->allow_non_integral_constant_expression_p;
8730 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8731 /* We are now parsing a constant-expression. */
8732 parser->integral_constant_expression_p = true;
8733 parser->allow_non_integral_constant_expression_p
8734 = (allow_non_constant_p || cxx_dialect >= cxx11);
8735 parser->non_integral_constant_expression_p = false;
8736 /* Although the grammar says "conditional-expression", we parse an
8737 "assignment-expression", which also permits "throw-expression"
8738 and the use of assignment operators. In the case that
8739 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8740 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8741 actually essential that we look for an assignment-expression.
8742 For example, cp_parser_initializer_clauses uses this function to
8743 determine whether a particular assignment-expression is in fact
8744 constant. */
8745 expression = cp_parser_assignment_expression (parser);
8746 /* Restore the old settings. */
8747 parser->integral_constant_expression_p
8748 = saved_integral_constant_expression_p;
8749 parser->allow_non_integral_constant_expression_p
8750 = saved_allow_non_integral_constant_expression_p;
8751 if (cxx_dialect >= cxx11)
8753 /* Require an rvalue constant expression here; that's what our
8754 callers expect. Reference constant expressions are handled
8755 separately in e.g. cp_parser_template_argument. */
8756 bool is_const = potential_rvalue_constant_expression (expression);
8757 parser->non_integral_constant_expression_p = !is_const;
8758 if (!is_const && !allow_non_constant_p)
8759 require_potential_rvalue_constant_expression (expression);
8761 if (allow_non_constant_p)
8762 *non_constant_p = parser->non_integral_constant_expression_p;
8763 parser->non_integral_constant_expression_p
8764 = saved_non_integral_constant_expression_p;
8766 return expression;
8769 /* Parse __builtin_offsetof.
8771 offsetof-expression:
8772 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8774 offsetof-member-designator:
8775 id-expression
8776 | offsetof-member-designator "." id-expression
8777 | offsetof-member-designator "[" expression "]"
8778 | offsetof-member-designator "->" id-expression */
8780 static tree
8781 cp_parser_builtin_offsetof (cp_parser *parser)
8783 int save_ice_p, save_non_ice_p;
8784 tree type, expr;
8785 cp_id_kind dummy;
8786 cp_token *token;
8788 /* We're about to accept non-integral-constant things, but will
8789 definitely yield an integral constant expression. Save and
8790 restore these values around our local parsing. */
8791 save_ice_p = parser->integral_constant_expression_p;
8792 save_non_ice_p = parser->non_integral_constant_expression_p;
8794 /* Consume the "__builtin_offsetof" token. */
8795 cp_lexer_consume_token (parser->lexer);
8796 /* Consume the opening `('. */
8797 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8798 /* Parse the type-id. */
8799 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8800 type = cp_parser_type_id (parser);
8801 /* Look for the `,'. */
8802 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8803 token = cp_lexer_peek_token (parser->lexer);
8805 /* Build the (type *)null that begins the traditional offsetof macro. */
8806 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8807 tf_warning_or_error);
8809 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8810 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8811 true, &dummy, token->location);
8812 while (true)
8814 token = cp_lexer_peek_token (parser->lexer);
8815 switch (token->type)
8817 case CPP_OPEN_SQUARE:
8818 /* offsetof-member-designator "[" expression "]" */
8819 expr = cp_parser_postfix_open_square_expression (parser, expr,
8820 true, false);
8821 break;
8823 case CPP_DEREF:
8824 /* offsetof-member-designator "->" identifier */
8825 expr = grok_array_decl (token->location, expr,
8826 integer_zero_node, false);
8827 /* FALLTHRU */
8829 case CPP_DOT:
8830 /* offsetof-member-designator "." identifier */
8831 cp_lexer_consume_token (parser->lexer);
8832 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8833 expr, true, &dummy,
8834 token->location);
8835 break;
8837 case CPP_CLOSE_PAREN:
8838 /* Consume the ")" token. */
8839 cp_lexer_consume_token (parser->lexer);
8840 goto success;
8842 default:
8843 /* Error. We know the following require will fail, but
8844 that gives the proper error message. */
8845 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8846 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8847 expr = error_mark_node;
8848 goto failure;
8852 success:
8853 expr = finish_offsetof (expr, loc);
8855 failure:
8856 parser->integral_constant_expression_p = save_ice_p;
8857 parser->non_integral_constant_expression_p = save_non_ice_p;
8859 return expr;
8862 /* Parse a trait expression.
8864 Returns a representation of the expression, the underlying type
8865 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8867 static tree
8868 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8870 cp_trait_kind kind;
8871 tree type1, type2 = NULL_TREE;
8872 bool binary = false;
8873 bool variadic = false;
8875 switch (keyword)
8877 case RID_HAS_NOTHROW_ASSIGN:
8878 kind = CPTK_HAS_NOTHROW_ASSIGN;
8879 break;
8880 case RID_HAS_NOTHROW_CONSTRUCTOR:
8881 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8882 break;
8883 case RID_HAS_NOTHROW_COPY:
8884 kind = CPTK_HAS_NOTHROW_COPY;
8885 break;
8886 case RID_HAS_TRIVIAL_ASSIGN:
8887 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8888 break;
8889 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8890 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8891 break;
8892 case RID_HAS_TRIVIAL_COPY:
8893 kind = CPTK_HAS_TRIVIAL_COPY;
8894 break;
8895 case RID_HAS_TRIVIAL_DESTRUCTOR:
8896 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8897 break;
8898 case RID_HAS_VIRTUAL_DESTRUCTOR:
8899 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8900 break;
8901 case RID_IS_ABSTRACT:
8902 kind = CPTK_IS_ABSTRACT;
8903 break;
8904 case RID_IS_BASE_OF:
8905 kind = CPTK_IS_BASE_OF;
8906 binary = true;
8907 break;
8908 case RID_IS_CLASS:
8909 kind = CPTK_IS_CLASS;
8910 break;
8911 case RID_IS_EMPTY:
8912 kind = CPTK_IS_EMPTY;
8913 break;
8914 case RID_IS_ENUM:
8915 kind = CPTK_IS_ENUM;
8916 break;
8917 case RID_IS_FINAL:
8918 kind = CPTK_IS_FINAL;
8919 break;
8920 case RID_IS_LITERAL_TYPE:
8921 kind = CPTK_IS_LITERAL_TYPE;
8922 break;
8923 case RID_IS_POD:
8924 kind = CPTK_IS_POD;
8925 break;
8926 case RID_IS_POLYMORPHIC:
8927 kind = CPTK_IS_POLYMORPHIC;
8928 break;
8929 case RID_IS_SAME_AS:
8930 kind = CPTK_IS_SAME_AS;
8931 binary = true;
8932 break;
8933 case RID_IS_STD_LAYOUT:
8934 kind = CPTK_IS_STD_LAYOUT;
8935 break;
8936 case RID_IS_TRIVIAL:
8937 kind = CPTK_IS_TRIVIAL;
8938 break;
8939 case RID_IS_TRIVIALLY_ASSIGNABLE:
8940 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8941 binary = true;
8942 break;
8943 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8944 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8945 variadic = true;
8946 break;
8947 case RID_IS_TRIVIALLY_COPYABLE:
8948 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8949 break;
8950 case RID_IS_UNION:
8951 kind = CPTK_IS_UNION;
8952 break;
8953 case RID_UNDERLYING_TYPE:
8954 kind = CPTK_UNDERLYING_TYPE;
8955 break;
8956 case RID_BASES:
8957 kind = CPTK_BASES;
8958 break;
8959 case RID_DIRECT_BASES:
8960 kind = CPTK_DIRECT_BASES;
8961 break;
8962 default:
8963 gcc_unreachable ();
8966 /* Consume the token. */
8967 cp_lexer_consume_token (parser->lexer);
8969 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8971 type1 = cp_parser_type_id (parser);
8973 if (type1 == error_mark_node)
8974 return error_mark_node;
8976 if (binary)
8978 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8980 type2 = cp_parser_type_id (parser);
8982 if (type2 == error_mark_node)
8983 return error_mark_node;
8985 else if (variadic)
8987 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8989 cp_lexer_consume_token (parser->lexer);
8990 tree elt = cp_parser_type_id (parser);
8991 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8993 cp_lexer_consume_token (parser->lexer);
8994 elt = make_pack_expansion (elt);
8996 if (elt == error_mark_node)
8997 return error_mark_node;
8998 type2 = tree_cons (NULL_TREE, elt, type2);
9002 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9004 /* Complete the trait expression, which may mean either processing
9005 the trait expr now or saving it for template instantiation. */
9006 switch(kind)
9008 case CPTK_UNDERLYING_TYPE:
9009 return finish_underlying_type (type1);
9010 case CPTK_BASES:
9011 return finish_bases (type1, false);
9012 case CPTK_DIRECT_BASES:
9013 return finish_bases (type1, true);
9014 default:
9015 return finish_trait_expr (kind, type1, type2);
9019 /* Lambdas that appear in variable initializer or default argument scope
9020 get that in their mangling, so we need to record it. We might as well
9021 use the count for function and namespace scopes as well. */
9022 static GTY(()) tree lambda_scope;
9023 static GTY(()) int lambda_count;
9024 typedef struct GTY(()) tree_int
9026 tree t;
9027 int i;
9028 } tree_int;
9029 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9031 static void
9032 start_lambda_scope (tree decl)
9034 tree_int ti;
9035 gcc_assert (decl);
9036 /* Once we're inside a function, we ignore other scopes and just push
9037 the function again so that popping works properly. */
9038 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9039 decl = current_function_decl;
9040 ti.t = lambda_scope;
9041 ti.i = lambda_count;
9042 vec_safe_push (lambda_scope_stack, ti);
9043 if (lambda_scope != decl)
9045 /* Don't reset the count if we're still in the same function. */
9046 lambda_scope = decl;
9047 lambda_count = 0;
9051 static void
9052 record_lambda_scope (tree lambda)
9054 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9055 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9058 static void
9059 finish_lambda_scope (void)
9061 tree_int *p = &lambda_scope_stack->last ();
9062 if (lambda_scope != p->t)
9064 lambda_scope = p->t;
9065 lambda_count = p->i;
9067 lambda_scope_stack->pop ();
9070 /* Parse a lambda expression.
9072 lambda-expression:
9073 lambda-introducer lambda-declarator [opt] compound-statement
9075 Returns a representation of the expression. */
9077 static tree
9078 cp_parser_lambda_expression (cp_parser* parser)
9080 tree lambda_expr = build_lambda_expr ();
9081 tree type;
9082 bool ok = true;
9083 cp_token *token = cp_lexer_peek_token (parser->lexer);
9084 cp_token_position start = 0;
9086 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9088 if (cp_unevaluated_operand)
9090 if (!token->error_reported)
9092 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9093 "lambda-expression in unevaluated context");
9094 token->error_reported = true;
9096 ok = false;
9098 else if (parser->in_template_argument_list_p)
9100 if (!token->error_reported)
9102 error_at (token->location, "lambda-expression in template-argument");
9103 token->error_reported = true;
9105 ok = false;
9108 /* We may be in the middle of deferred access check. Disable
9109 it now. */
9110 push_deferring_access_checks (dk_no_deferred);
9112 cp_parser_lambda_introducer (parser, lambda_expr);
9114 type = begin_lambda_type (lambda_expr);
9115 if (type == error_mark_node)
9116 return error_mark_node;
9118 record_lambda_scope (lambda_expr);
9120 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9121 determine_visibility (TYPE_NAME (type));
9123 /* Now that we've started the type, add the capture fields for any
9124 explicit captures. */
9125 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9128 /* Inside the class, surrounding template-parameter-lists do not apply. */
9129 unsigned int saved_num_template_parameter_lists
9130 = parser->num_template_parameter_lists;
9131 unsigned char in_statement = parser->in_statement;
9132 bool in_switch_statement_p = parser->in_switch_statement_p;
9133 bool fully_implicit_function_template_p
9134 = parser->fully_implicit_function_template_p;
9135 tree implicit_template_parms = parser->implicit_template_parms;
9136 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9137 bool auto_is_implicit_function_template_parm_p
9138 = parser->auto_is_implicit_function_template_parm_p;
9140 parser->num_template_parameter_lists = 0;
9141 parser->in_statement = 0;
9142 parser->in_switch_statement_p = false;
9143 parser->fully_implicit_function_template_p = false;
9144 parser->implicit_template_parms = 0;
9145 parser->implicit_template_scope = 0;
9146 parser->auto_is_implicit_function_template_parm_p = false;
9148 /* By virtue of defining a local class, a lambda expression has access to
9149 the private variables of enclosing classes. */
9151 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9153 if (ok)
9155 if (!cp_parser_error_occurred (parser)
9156 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9157 && cp_parser_start_tentative_firewall (parser))
9158 start = token;
9159 cp_parser_lambda_body (parser, lambda_expr);
9161 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9163 if (cp_parser_skip_to_closing_brace (parser))
9164 cp_lexer_consume_token (parser->lexer);
9167 /* The capture list was built up in reverse order; fix that now. */
9168 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9169 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9171 if (ok)
9172 maybe_add_lambda_conv_op (type);
9174 type = finish_struct (type, /*attributes=*/NULL_TREE);
9176 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9177 parser->in_statement = in_statement;
9178 parser->in_switch_statement_p = in_switch_statement_p;
9179 parser->fully_implicit_function_template_p
9180 = fully_implicit_function_template_p;
9181 parser->implicit_template_parms = implicit_template_parms;
9182 parser->implicit_template_scope = implicit_template_scope;
9183 parser->auto_is_implicit_function_template_parm_p
9184 = auto_is_implicit_function_template_parm_p;
9187 pop_deferring_access_checks ();
9189 /* This field is only used during parsing of the lambda. */
9190 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9192 /* This lambda shouldn't have any proxies left at this point. */
9193 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9194 /* And now that we're done, push proxies for an enclosing lambda. */
9195 insert_pending_capture_proxies ();
9197 if (ok)
9198 lambda_expr = build_lambda_object (lambda_expr);
9199 else
9200 lambda_expr = error_mark_node;
9202 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9204 return lambda_expr;
9207 /* Parse the beginning of a lambda expression.
9209 lambda-introducer:
9210 [ lambda-capture [opt] ]
9212 LAMBDA_EXPR is the current representation of the lambda expression. */
9214 static void
9215 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9217 /* Need commas after the first capture. */
9218 bool first = true;
9220 /* Eat the leading `['. */
9221 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9223 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9224 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9225 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9226 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9227 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9228 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9230 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9232 cp_lexer_consume_token (parser->lexer);
9233 first = false;
9236 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9238 cp_token* capture_token;
9239 tree capture_id;
9240 tree capture_init_expr;
9241 cp_id_kind idk = CP_ID_KIND_NONE;
9242 bool explicit_init_p = false;
9244 enum capture_kind_type
9246 BY_COPY,
9247 BY_REFERENCE
9249 enum capture_kind_type capture_kind = BY_COPY;
9251 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9253 error ("expected end of capture-list");
9254 return;
9257 if (first)
9258 first = false;
9259 else
9260 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9262 /* Possibly capture `this'. */
9263 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9265 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9266 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9267 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9268 "with by-copy capture default");
9269 cp_lexer_consume_token (parser->lexer);
9270 add_capture (lambda_expr,
9271 /*id=*/this_identifier,
9272 /*initializer=*/finish_this_expr(),
9273 /*by_reference_p=*/false,
9274 explicit_init_p);
9275 continue;
9278 /* Remember whether we want to capture as a reference or not. */
9279 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9281 capture_kind = BY_REFERENCE;
9282 cp_lexer_consume_token (parser->lexer);
9285 /* Get the identifier. */
9286 capture_token = cp_lexer_peek_token (parser->lexer);
9287 capture_id = cp_parser_identifier (parser);
9289 if (capture_id == error_mark_node)
9290 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9291 delimiters, but I modified this to stop on unnested ']' as well. It
9292 was already changed to stop on unnested '}', so the
9293 "closing_parenthesis" name is no more misleading with my change. */
9295 cp_parser_skip_to_closing_parenthesis (parser,
9296 /*recovering=*/true,
9297 /*or_comma=*/true,
9298 /*consume_paren=*/true);
9299 break;
9302 /* Find the initializer for this capture. */
9303 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9304 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9305 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9307 bool direct, non_constant;
9308 /* An explicit initializer exists. */
9309 if (cxx_dialect < cxx14)
9310 pedwarn (input_location, 0,
9311 "lambda capture initializers "
9312 "only available with -std=c++14 or -std=gnu++14");
9313 capture_init_expr = cp_parser_initializer (parser, &direct,
9314 &non_constant);
9315 explicit_init_p = true;
9316 if (capture_init_expr == NULL_TREE)
9318 error ("empty initializer for lambda init-capture");
9319 capture_init_expr = error_mark_node;
9322 else
9324 const char* error_msg;
9326 /* Turn the identifier into an id-expression. */
9327 capture_init_expr
9328 = cp_parser_lookup_name_simple (parser, capture_id,
9329 capture_token->location);
9331 if (capture_init_expr == error_mark_node)
9333 unqualified_name_lookup_error (capture_id);
9334 continue;
9336 else if (DECL_P (capture_init_expr)
9337 && (!VAR_P (capture_init_expr)
9338 && TREE_CODE (capture_init_expr) != PARM_DECL))
9340 error_at (capture_token->location,
9341 "capture of non-variable %qD ",
9342 capture_init_expr);
9343 inform (0, "%q+#D declared here", capture_init_expr);
9344 continue;
9346 if (VAR_P (capture_init_expr)
9347 && decl_storage_duration (capture_init_expr) != dk_auto)
9349 if (pedwarn (capture_token->location, 0, "capture of variable "
9350 "%qD with non-automatic storage duration",
9351 capture_init_expr))
9352 inform (0, "%q+#D declared here", capture_init_expr);
9353 continue;
9356 capture_init_expr
9357 = finish_id_expression
9358 (capture_id,
9359 capture_init_expr,
9360 parser->scope,
9361 &idk,
9362 /*integral_constant_expression_p=*/false,
9363 /*allow_non_integral_constant_expression_p=*/false,
9364 /*non_integral_constant_expression_p=*/NULL,
9365 /*template_p=*/false,
9366 /*done=*/true,
9367 /*address_p=*/false,
9368 /*template_arg_p=*/false,
9369 &error_msg,
9370 capture_token->location);
9372 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9374 cp_lexer_consume_token (parser->lexer);
9375 capture_init_expr = make_pack_expansion (capture_init_expr);
9377 else
9378 check_for_bare_parameter_packs (capture_init_expr);
9381 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9382 && !explicit_init_p)
9384 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9385 && capture_kind == BY_COPY)
9386 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9387 "of %qD redundant with by-copy capture default",
9388 capture_id);
9389 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9390 && capture_kind == BY_REFERENCE)
9391 pedwarn (capture_token->location, 0, "explicit by-reference "
9392 "capture of %qD redundant with by-reference capture "
9393 "default", capture_id);
9396 add_capture (lambda_expr,
9397 capture_id,
9398 capture_init_expr,
9399 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9400 explicit_init_p);
9403 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9406 /* Parse the (optional) middle of a lambda expression.
9408 lambda-declarator:
9409 < template-parameter-list [opt] >
9410 ( parameter-declaration-clause [opt] )
9411 attribute-specifier [opt]
9412 mutable [opt]
9413 exception-specification [opt]
9414 lambda-return-type-clause [opt]
9416 LAMBDA_EXPR is the current representation of the lambda expression. */
9418 static bool
9419 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9421 /* 5.1.1.4 of the standard says:
9422 If a lambda-expression does not include a lambda-declarator, it is as if
9423 the lambda-declarator were ().
9424 This means an empty parameter list, no attributes, and no exception
9425 specification. */
9426 tree param_list = void_list_node;
9427 tree attributes = NULL_TREE;
9428 tree exception_spec = NULL_TREE;
9429 tree template_param_list = NULL_TREE;
9431 /* The template-parameter-list is optional, but must begin with
9432 an opening angle if present. */
9433 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9435 if (cxx_dialect < cxx14)
9436 pedwarn (parser->lexer->next_token->location, 0,
9437 "lambda templates are only available with "
9438 "-std=c++14 or -std=gnu++14");
9440 cp_lexer_consume_token (parser->lexer);
9442 template_param_list = cp_parser_template_parameter_list (parser);
9444 cp_parser_skip_to_end_of_template_parameter_list (parser);
9446 /* We just processed one more parameter list. */
9447 ++parser->num_template_parameter_lists;
9450 /* The parameter-declaration-clause is optional (unless
9451 template-parameter-list was given), but must begin with an
9452 opening parenthesis if present. */
9453 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9455 cp_lexer_consume_token (parser->lexer);
9457 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9459 /* Parse parameters. */
9460 param_list = cp_parser_parameter_declaration_clause (parser);
9462 /* Default arguments shall not be specified in the
9463 parameter-declaration-clause of a lambda-declarator. */
9464 for (tree t = param_list; t; t = TREE_CHAIN (t))
9465 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9466 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9467 "default argument specified for lambda parameter");
9469 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9471 attributes = cp_parser_attributes_opt (parser);
9473 /* Parse optional `mutable' keyword. */
9474 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9476 cp_lexer_consume_token (parser->lexer);
9477 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9480 /* Parse optional exception specification. */
9481 exception_spec = cp_parser_exception_specification_opt (parser);
9483 /* Parse optional trailing return type. */
9484 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9486 cp_lexer_consume_token (parser->lexer);
9487 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9488 = cp_parser_trailing_type_id (parser);
9491 /* The function parameters must be in scope all the way until after the
9492 trailing-return-type in case of decltype. */
9493 pop_bindings_and_leave_scope ();
9495 else if (template_param_list != NULL_TREE) // generate diagnostic
9496 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9498 /* Create the function call operator.
9500 Messing with declarators like this is no uglier than building up the
9501 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9502 other code. */
9504 cp_decl_specifier_seq return_type_specs;
9505 cp_declarator* declarator;
9506 tree fco;
9507 int quals;
9508 void *p;
9510 clear_decl_specs (&return_type_specs);
9511 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9512 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9513 else
9514 /* Maybe we will deduce the return type later. */
9515 return_type_specs.type = make_auto ();
9517 p = obstack_alloc (&declarator_obstack, 0);
9519 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9520 sfk_none);
9522 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9523 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9524 declarator = make_call_declarator (declarator, param_list, quals,
9525 VIRT_SPEC_UNSPECIFIED,
9526 REF_QUAL_NONE,
9527 exception_spec,
9528 /*late_return_type=*/NULL_TREE,
9529 /*requires_clause*/NULL_TREE);
9530 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9533 fco = grokmethod (&return_type_specs,
9534 declarator,
9535 attributes);
9536 if (fco != error_mark_node)
9538 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9539 DECL_ARTIFICIAL (fco) = 1;
9540 /* Give the object parameter a different name. */
9541 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9542 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9543 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9545 if (template_param_list)
9547 fco = finish_member_template_decl (fco);
9548 finish_template_decl (template_param_list);
9549 --parser->num_template_parameter_lists;
9551 else if (parser->fully_implicit_function_template_p)
9552 fco = finish_fully_implicit_template (parser, fco);
9554 finish_member_declaration (fco);
9556 obstack_free (&declarator_obstack, p);
9558 return (fco != error_mark_node);
9562 /* Parse the body of a lambda expression, which is simply
9564 compound-statement
9566 but which requires special handling.
9567 LAMBDA_EXPR is the current representation of the lambda expression. */
9569 static void
9570 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9572 bool nested = (current_function_decl != NULL_TREE);
9573 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9574 if (nested)
9575 push_function_context ();
9576 else
9577 /* Still increment function_depth so that we don't GC in the
9578 middle of an expression. */
9579 ++function_depth;
9580 /* Clear this in case we're in the middle of a default argument. */
9581 parser->local_variables_forbidden_p = false;
9583 /* Finish the function call operator
9584 - class_specifier
9585 + late_parsing_for_member
9586 + function_definition_after_declarator
9587 + ctor_initializer_opt_and_function_body */
9589 tree fco = lambda_function (lambda_expr);
9590 tree body;
9591 bool done = false;
9592 tree compound_stmt;
9593 tree cap;
9595 /* Let the front end know that we are going to be defining this
9596 function. */
9597 start_preparsed_function (fco,
9598 NULL_TREE,
9599 SF_PRE_PARSED | SF_INCLASS_INLINE);
9601 start_lambda_scope (fco);
9602 body = begin_function_body ();
9604 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9605 goto out;
9607 /* Push the proxies for any explicit captures. */
9608 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9609 cap = TREE_CHAIN (cap))
9610 build_capture_proxy (TREE_PURPOSE (cap));
9612 compound_stmt = begin_compound_stmt (0);
9614 /* 5.1.1.4 of the standard says:
9615 If a lambda-expression does not include a trailing-return-type, it
9616 is as if the trailing-return-type denotes the following type:
9617 * if the compound-statement is of the form
9618 { return attribute-specifier [opt] expression ; }
9619 the type of the returned expression after lvalue-to-rvalue
9620 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9621 (_conv.array_ 4.2), and function-to-pointer conversion
9622 (_conv.func_ 4.3);
9623 * otherwise, void. */
9625 /* In a lambda that has neither a lambda-return-type-clause
9626 nor a deducible form, errors should be reported for return statements
9627 in the body. Since we used void as the placeholder return type, parsing
9628 the body as usual will give such desired behavior. */
9629 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9630 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9631 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9633 tree expr = NULL_TREE;
9634 cp_id_kind idk = CP_ID_KIND_NONE;
9636 /* Parse tentatively in case there's more after the initial return
9637 statement. */
9638 cp_parser_parse_tentatively (parser);
9640 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9642 expr = cp_parser_expression (parser, &idk);
9644 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9645 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9647 if (cp_parser_parse_definitely (parser))
9649 if (!processing_template_decl)
9650 apply_deduced_return_type (fco, lambda_return_type (expr));
9652 /* Will get error here if type not deduced yet. */
9653 finish_return_stmt (expr);
9655 done = true;
9659 if (!done)
9661 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9662 cp_parser_label_declaration (parser);
9663 cp_parser_statement_seq_opt (parser, NULL_TREE);
9664 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9667 finish_compound_stmt (compound_stmt);
9669 out:
9670 finish_function_body (body);
9671 finish_lambda_scope ();
9673 /* Finish the function and generate code for it if necessary. */
9674 tree fn = finish_function (/*inline*/2);
9676 /* Only expand if the call op is not a template. */
9677 if (!DECL_TEMPLATE_INFO (fco))
9678 expand_or_defer_fn (fn);
9681 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9682 if (nested)
9683 pop_function_context();
9684 else
9685 --function_depth;
9688 /* Statements [gram.stmt.stmt] */
9690 /* Parse a statement.
9692 statement:
9693 labeled-statement
9694 expression-statement
9695 compound-statement
9696 selection-statement
9697 iteration-statement
9698 jump-statement
9699 declaration-statement
9700 try-block
9702 C++11:
9704 statement:
9705 labeled-statement
9706 attribute-specifier-seq (opt) expression-statement
9707 attribute-specifier-seq (opt) compound-statement
9708 attribute-specifier-seq (opt) selection-statement
9709 attribute-specifier-seq (opt) iteration-statement
9710 attribute-specifier-seq (opt) jump-statement
9711 declaration-statement
9712 attribute-specifier-seq (opt) try-block
9714 TM Extension:
9716 statement:
9717 atomic-statement
9719 IN_COMPOUND is true when the statement is nested inside a
9720 cp_parser_compound_statement; this matters for certain pragmas.
9722 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9723 is a (possibly labeled) if statement which is not enclosed in braces
9724 and has an else clause. This is used to implement -Wparentheses. */
9726 static void
9727 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9728 bool in_compound, bool *if_p)
9730 tree statement, std_attrs = NULL_TREE;
9731 cp_token *token;
9732 location_t statement_location, attrs_location;
9734 restart:
9735 if (if_p != NULL)
9736 *if_p = false;
9737 /* There is no statement yet. */
9738 statement = NULL_TREE;
9740 saved_token_sentinel saved_tokens (parser->lexer);
9741 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9742 if (c_dialect_objc ())
9743 /* In obj-c++, seeing '[[' might be the either the beginning of
9744 c++11 attributes, or a nested objc-message-expression. So
9745 let's parse the c++11 attributes tentatively. */
9746 cp_parser_parse_tentatively (parser);
9747 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9748 if (c_dialect_objc ())
9750 if (!cp_parser_parse_definitely (parser))
9751 std_attrs = NULL_TREE;
9754 /* Peek at the next token. */
9755 token = cp_lexer_peek_token (parser->lexer);
9756 /* Remember the location of the first token in the statement. */
9757 statement_location = token->location;
9758 /* If this is a keyword, then that will often determine what kind of
9759 statement we have. */
9760 if (token->type == CPP_KEYWORD)
9762 enum rid keyword = token->keyword;
9764 switch (keyword)
9766 case RID_CASE:
9767 case RID_DEFAULT:
9768 /* Looks like a labeled-statement with a case label.
9769 Parse the label, and then use tail recursion to parse
9770 the statement. */
9771 cp_parser_label_for_labeled_statement (parser, std_attrs);
9772 goto restart;
9774 case RID_IF:
9775 case RID_SWITCH:
9776 statement = cp_parser_selection_statement (parser, if_p);
9777 break;
9779 case RID_WHILE:
9780 case RID_DO:
9781 case RID_FOR:
9782 statement = cp_parser_iteration_statement (parser, false);
9783 break;
9785 case RID_CILK_FOR:
9786 if (!flag_cilkplus)
9788 error_at (cp_lexer_peek_token (parser->lexer)->location,
9789 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9790 cp_lexer_consume_token (parser->lexer);
9791 statement = error_mark_node;
9793 else
9794 statement = cp_parser_cilk_for (parser, integer_zero_node);
9795 break;
9797 case RID_BREAK:
9798 case RID_CONTINUE:
9799 case RID_RETURN:
9800 case RID_GOTO:
9801 statement = cp_parser_jump_statement (parser);
9802 break;
9804 case RID_CILK_SYNC:
9805 cp_lexer_consume_token (parser->lexer);
9806 if (flag_cilkplus)
9808 tree sync_expr = build_cilk_sync ();
9809 SET_EXPR_LOCATION (sync_expr,
9810 token->location);
9811 statement = finish_expr_stmt (sync_expr);
9813 else
9815 error_at (token->location, "-fcilkplus must be enabled to use"
9816 " %<_Cilk_sync%>");
9817 statement = error_mark_node;
9819 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9820 break;
9822 /* Objective-C++ exception-handling constructs. */
9823 case RID_AT_TRY:
9824 case RID_AT_CATCH:
9825 case RID_AT_FINALLY:
9826 case RID_AT_SYNCHRONIZED:
9827 case RID_AT_THROW:
9828 statement = cp_parser_objc_statement (parser);
9829 break;
9831 case RID_TRY:
9832 statement = cp_parser_try_block (parser);
9833 break;
9835 case RID_NAMESPACE:
9836 /* This must be a namespace alias definition. */
9837 cp_parser_declaration_statement (parser);
9838 return;
9840 case RID_TRANSACTION_ATOMIC:
9841 case RID_TRANSACTION_RELAXED:
9842 statement = cp_parser_transaction (parser, keyword);
9843 break;
9844 case RID_TRANSACTION_CANCEL:
9845 statement = cp_parser_transaction_cancel (parser);
9846 break;
9848 default:
9849 /* It might be a keyword like `int' that can start a
9850 declaration-statement. */
9851 break;
9854 else if (token->type == CPP_NAME)
9856 /* If the next token is a `:', then we are looking at a
9857 labeled-statement. */
9858 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9859 if (token->type == CPP_COLON)
9861 /* Looks like a labeled-statement with an ordinary label.
9862 Parse the label, and then use tail recursion to parse
9863 the statement. */
9865 cp_parser_label_for_labeled_statement (parser, std_attrs);
9866 goto restart;
9869 /* Anything that starts with a `{' must be a compound-statement. */
9870 else if (token->type == CPP_OPEN_BRACE)
9871 statement = cp_parser_compound_statement (parser, NULL, false, false);
9872 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9873 a statement all its own. */
9874 else if (token->type == CPP_PRAGMA)
9876 /* Only certain OpenMP pragmas are attached to statements, and thus
9877 are considered statements themselves. All others are not. In
9878 the context of a compound, accept the pragma as a "statement" and
9879 return so that we can check for a close brace. Otherwise we
9880 require a real statement and must go back and read one. */
9881 if (in_compound)
9882 cp_parser_pragma (parser, pragma_compound);
9883 else if (!cp_parser_pragma (parser, pragma_stmt))
9884 goto restart;
9885 return;
9887 else if (token->type == CPP_EOF)
9889 cp_parser_error (parser, "expected statement");
9890 return;
9893 /* Everything else must be a declaration-statement or an
9894 expression-statement. Try for the declaration-statement
9895 first, unless we are looking at a `;', in which case we know that
9896 we have an expression-statement. */
9897 if (!statement)
9899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9901 if (std_attrs != NULL_TREE)
9903 /* Attributes should be parsed as part of the the
9904 declaration, so let's un-parse them. */
9905 saved_tokens.rollback();
9906 std_attrs = NULL_TREE;
9909 cp_parser_parse_tentatively (parser);
9910 /* Try to parse the declaration-statement. */
9911 cp_parser_declaration_statement (parser);
9912 /* If that worked, we're done. */
9913 if (cp_parser_parse_definitely (parser))
9914 return;
9916 /* Look for an expression-statement instead. */
9917 statement = cp_parser_expression_statement (parser, in_statement_expr);
9920 /* Set the line number for the statement. */
9921 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9922 SET_EXPR_LOCATION (statement, statement_location);
9924 /* Note that for now, we don't do anything with c++11 statements
9925 parsed at this level. */
9926 if (std_attrs != NULL_TREE)
9927 warning_at (attrs_location,
9928 OPT_Wattributes,
9929 "attributes at the beginning of statement are ignored");
9932 /* Parse the label for a labeled-statement, i.e.
9934 identifier :
9935 case constant-expression :
9936 default :
9938 GNU Extension:
9939 case constant-expression ... constant-expression : statement
9941 When a label is parsed without errors, the label is added to the
9942 parse tree by the finish_* functions, so this function doesn't
9943 have to return the label. */
9945 static void
9946 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9948 cp_token *token;
9949 tree label = NULL_TREE;
9950 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9952 /* The next token should be an identifier. */
9953 token = cp_lexer_peek_token (parser->lexer);
9954 if (token->type != CPP_NAME
9955 && token->type != CPP_KEYWORD)
9957 cp_parser_error (parser, "expected labeled-statement");
9958 return;
9961 parser->colon_corrects_to_scope_p = false;
9962 switch (token->keyword)
9964 case RID_CASE:
9966 tree expr, expr_hi;
9967 cp_token *ellipsis;
9969 /* Consume the `case' token. */
9970 cp_lexer_consume_token (parser->lexer);
9971 /* Parse the constant-expression. */
9972 expr = cp_parser_constant_expression (parser);
9973 if (check_for_bare_parameter_packs (expr))
9974 expr = error_mark_node;
9976 ellipsis = cp_lexer_peek_token (parser->lexer);
9977 if (ellipsis->type == CPP_ELLIPSIS)
9979 /* Consume the `...' token. */
9980 cp_lexer_consume_token (parser->lexer);
9981 expr_hi = cp_parser_constant_expression (parser);
9982 if (check_for_bare_parameter_packs (expr_hi))
9983 expr_hi = error_mark_node;
9985 /* We don't need to emit warnings here, as the common code
9986 will do this for us. */
9988 else
9989 expr_hi = NULL_TREE;
9991 if (parser->in_switch_statement_p)
9992 finish_case_label (token->location, expr, expr_hi);
9993 else
9994 error_at (token->location,
9995 "case label %qE not within a switch statement",
9996 expr);
9998 break;
10000 case RID_DEFAULT:
10001 /* Consume the `default' token. */
10002 cp_lexer_consume_token (parser->lexer);
10004 if (parser->in_switch_statement_p)
10005 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10006 else
10007 error_at (token->location, "case label not within a switch statement");
10008 break;
10010 default:
10011 /* Anything else must be an ordinary label. */
10012 label = finish_label_stmt (cp_parser_identifier (parser));
10013 break;
10016 /* Require the `:' token. */
10017 cp_parser_require (parser, CPP_COLON, RT_COLON);
10019 /* An ordinary label may optionally be followed by attributes.
10020 However, this is only permitted if the attributes are then
10021 followed by a semicolon. This is because, for backward
10022 compatibility, when parsing
10023 lab: __attribute__ ((unused)) int i;
10024 we want the attribute to attach to "i", not "lab". */
10025 if (label != NULL_TREE
10026 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10028 tree attrs;
10029 cp_parser_parse_tentatively (parser);
10030 attrs = cp_parser_gnu_attributes_opt (parser);
10031 if (attrs == NULL_TREE
10032 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10033 cp_parser_abort_tentative_parse (parser);
10034 else if (!cp_parser_parse_definitely (parser))
10036 else
10037 attributes = chainon (attributes, attrs);
10040 if (attributes != NULL_TREE)
10041 cplus_decl_attributes (&label, attributes, 0);
10043 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10046 /* Parse an expression-statement.
10048 expression-statement:
10049 expression [opt] ;
10051 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10052 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10053 indicates whether this expression-statement is part of an
10054 expression statement. */
10056 static tree
10057 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10059 tree statement = NULL_TREE;
10060 cp_token *token = cp_lexer_peek_token (parser->lexer);
10062 /* If the next token is a ';', then there is no expression
10063 statement. */
10064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10066 statement = cp_parser_expression (parser);
10067 if (statement == error_mark_node
10068 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10070 cp_parser_skip_to_end_of_block_or_statement (parser);
10071 return error_mark_node;
10075 /* Give a helpful message for "A<T>::type t;" and the like. */
10076 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10077 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10079 if (TREE_CODE (statement) == SCOPE_REF)
10080 error_at (token->location, "need %<typename%> before %qE because "
10081 "%qT is a dependent scope",
10082 statement, TREE_OPERAND (statement, 0));
10083 else if (is_overloaded_fn (statement)
10084 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10086 /* A::A a; */
10087 tree fn = get_first_fn (statement);
10088 error_at (token->location,
10089 "%<%T::%D%> names the constructor, not the type",
10090 DECL_CONTEXT (fn), DECL_NAME (fn));
10094 /* Consume the final `;'. */
10095 cp_parser_consume_semicolon_at_end_of_statement (parser);
10097 if (in_statement_expr
10098 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10099 /* This is the final expression statement of a statement
10100 expression. */
10101 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10102 else if (statement)
10103 statement = finish_expr_stmt (statement);
10105 return statement;
10108 /* Parse a compound-statement.
10110 compound-statement:
10111 { statement-seq [opt] }
10113 GNU extension:
10115 compound-statement:
10116 { label-declaration-seq [opt] statement-seq [opt] }
10118 label-declaration-seq:
10119 label-declaration
10120 label-declaration-seq label-declaration
10122 Returns a tree representing the statement. */
10124 static tree
10125 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10126 bool in_try, bool function_body)
10128 tree compound_stmt;
10130 /* Consume the `{'. */
10131 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10132 return error_mark_node;
10133 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10134 && !function_body && cxx_dialect < cxx14)
10135 pedwarn (input_location, OPT_Wpedantic,
10136 "compound-statement in constexpr function");
10137 /* Begin the compound-statement. */
10138 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10139 /* If the next keyword is `__label__' we have a label declaration. */
10140 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10141 cp_parser_label_declaration (parser);
10142 /* Parse an (optional) statement-seq. */
10143 cp_parser_statement_seq_opt (parser, in_statement_expr);
10144 /* Finish the compound-statement. */
10145 finish_compound_stmt (compound_stmt);
10146 /* Consume the `}'. */
10147 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10149 return compound_stmt;
10152 /* Parse an (optional) statement-seq.
10154 statement-seq:
10155 statement
10156 statement-seq [opt] statement */
10158 static void
10159 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10161 /* Scan statements until there aren't any more. */
10162 while (true)
10164 cp_token *token = cp_lexer_peek_token (parser->lexer);
10166 /* If we are looking at a `}', then we have run out of
10167 statements; the same is true if we have reached the end
10168 of file, or have stumbled upon a stray '@end'. */
10169 if (token->type == CPP_CLOSE_BRACE
10170 || token->type == CPP_EOF
10171 || token->type == CPP_PRAGMA_EOL
10172 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10173 break;
10175 /* If we are in a compound statement and find 'else' then
10176 something went wrong. */
10177 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10179 if (parser->in_statement & IN_IF_STMT)
10180 break;
10181 else
10183 token = cp_lexer_consume_token (parser->lexer);
10184 error_at (token->location, "%<else%> without a previous %<if%>");
10188 /* Parse the statement. */
10189 cp_parser_statement (parser, in_statement_expr, true, NULL);
10193 /* Parse a selection-statement.
10195 selection-statement:
10196 if ( condition ) statement
10197 if ( condition ) statement else statement
10198 switch ( condition ) statement
10200 Returns the new IF_STMT or SWITCH_STMT.
10202 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10203 is a (possibly labeled) if statement which is not enclosed in
10204 braces and has an else clause. This is used to implement
10205 -Wparentheses. */
10207 static tree
10208 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10210 cp_token *token;
10211 enum rid keyword;
10213 if (if_p != NULL)
10214 *if_p = false;
10216 /* Peek at the next token. */
10217 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10219 /* See what kind of keyword it is. */
10220 keyword = token->keyword;
10221 switch (keyword)
10223 case RID_IF:
10224 case RID_SWITCH:
10226 tree statement;
10227 tree condition;
10229 /* Look for the `('. */
10230 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10232 cp_parser_skip_to_end_of_statement (parser);
10233 return error_mark_node;
10236 /* Begin the selection-statement. */
10237 if (keyword == RID_IF)
10238 statement = begin_if_stmt ();
10239 else
10240 statement = begin_switch_stmt ();
10242 /* Parse the condition. */
10243 condition = cp_parser_condition (parser);
10244 /* Look for the `)'. */
10245 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10246 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10247 /*consume_paren=*/true);
10249 if (keyword == RID_IF)
10251 bool nested_if;
10252 unsigned char in_statement;
10254 /* Add the condition. */
10255 finish_if_stmt_cond (condition, statement);
10257 /* Parse the then-clause. */
10258 in_statement = parser->in_statement;
10259 parser->in_statement |= IN_IF_STMT;
10260 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10262 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10263 add_stmt (build_empty_stmt (loc));
10264 cp_lexer_consume_token (parser->lexer);
10265 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10266 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10267 "empty body in an %<if%> statement");
10268 nested_if = false;
10270 else
10271 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10272 parser->in_statement = in_statement;
10274 finish_then_clause (statement);
10276 /* If the next token is `else', parse the else-clause. */
10277 if (cp_lexer_next_token_is_keyword (parser->lexer,
10278 RID_ELSE))
10280 /* Consume the `else' keyword. */
10281 cp_lexer_consume_token (parser->lexer);
10282 begin_else_clause (statement);
10283 /* Parse the else-clause. */
10284 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10286 location_t loc;
10287 loc = cp_lexer_peek_token (parser->lexer)->location;
10288 warning_at (loc,
10289 OPT_Wempty_body, "suggest braces around "
10290 "empty body in an %<else%> statement");
10291 add_stmt (build_empty_stmt (loc));
10292 cp_lexer_consume_token (parser->lexer);
10294 else
10295 cp_parser_implicitly_scoped_statement (parser, NULL);
10297 finish_else_clause (statement);
10299 /* If we are currently parsing a then-clause, then
10300 IF_P will not be NULL. We set it to true to
10301 indicate that this if statement has an else clause.
10302 This may trigger the Wparentheses warning below
10303 when we get back up to the parent if statement. */
10304 if (if_p != NULL)
10305 *if_p = true;
10307 else
10309 /* This if statement does not have an else clause. If
10310 NESTED_IF is true, then the then-clause is an if
10311 statement which does have an else clause. We warn
10312 about the potential ambiguity. */
10313 if (nested_if)
10314 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10315 "suggest explicit braces to avoid ambiguous"
10316 " %<else%>");
10319 /* Now we're all done with the if-statement. */
10320 finish_if_stmt (statement);
10322 else
10324 bool in_switch_statement_p;
10325 unsigned char in_statement;
10327 /* Add the condition. */
10328 finish_switch_cond (condition, statement);
10330 /* Parse the body of the switch-statement. */
10331 in_switch_statement_p = parser->in_switch_statement_p;
10332 in_statement = parser->in_statement;
10333 parser->in_switch_statement_p = true;
10334 parser->in_statement |= IN_SWITCH_STMT;
10335 cp_parser_implicitly_scoped_statement (parser, NULL);
10336 parser->in_switch_statement_p = in_switch_statement_p;
10337 parser->in_statement = in_statement;
10339 /* Now we're all done with the switch-statement. */
10340 finish_switch_stmt (statement);
10343 return statement;
10345 break;
10347 default:
10348 cp_parser_error (parser, "expected selection-statement");
10349 return error_mark_node;
10353 /* Parse a condition.
10355 condition:
10356 expression
10357 type-specifier-seq declarator = initializer-clause
10358 type-specifier-seq declarator braced-init-list
10360 GNU Extension:
10362 condition:
10363 type-specifier-seq declarator asm-specification [opt]
10364 attributes [opt] = assignment-expression
10366 Returns the expression that should be tested. */
10368 static tree
10369 cp_parser_condition (cp_parser* parser)
10371 cp_decl_specifier_seq type_specifiers;
10372 const char *saved_message;
10373 int declares_class_or_enum;
10375 /* Try the declaration first. */
10376 cp_parser_parse_tentatively (parser);
10377 /* New types are not allowed in the type-specifier-seq for a
10378 condition. */
10379 saved_message = parser->type_definition_forbidden_message;
10380 parser->type_definition_forbidden_message
10381 = G_("types may not be defined in conditions");
10382 /* Parse the type-specifier-seq. */
10383 cp_parser_decl_specifier_seq (parser,
10384 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10385 &type_specifiers,
10386 &declares_class_or_enum);
10387 /* Restore the saved message. */
10388 parser->type_definition_forbidden_message = saved_message;
10389 /* If all is well, we might be looking at a declaration. */
10390 if (!cp_parser_error_occurred (parser))
10392 tree decl;
10393 tree asm_specification;
10394 tree attributes;
10395 cp_declarator *declarator;
10396 tree initializer = NULL_TREE;
10398 /* Parse the declarator. */
10399 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10400 /*ctor_dtor_or_conv_p=*/NULL,
10401 /*parenthesized_p=*/NULL,
10402 /*member_p=*/false,
10403 /*friend_p=*/false);
10404 /* Parse the attributes. */
10405 attributes = cp_parser_attributes_opt (parser);
10406 /* Parse the asm-specification. */
10407 asm_specification = cp_parser_asm_specification_opt (parser);
10408 /* If the next token is not an `=' or '{', then we might still be
10409 looking at an expression. For example:
10411 if (A(a).x)
10413 looks like a decl-specifier-seq and a declarator -- but then
10414 there is no `=', so this is an expression. */
10415 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10416 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10417 cp_parser_simulate_error (parser);
10419 /* If we did see an `=' or '{', then we are looking at a declaration
10420 for sure. */
10421 if (cp_parser_parse_definitely (parser))
10423 tree pushed_scope;
10424 bool non_constant_p;
10425 bool flags = LOOKUP_ONLYCONVERTING;
10427 /* Create the declaration. */
10428 decl = start_decl (declarator, &type_specifiers,
10429 /*initialized_p=*/true,
10430 attributes, /*prefix_attributes=*/NULL_TREE,
10431 &pushed_scope);
10433 /* Parse the initializer. */
10434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10436 initializer = cp_parser_braced_list (parser, &non_constant_p);
10437 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10438 flags = 0;
10440 else
10442 /* Consume the `='. */
10443 cp_parser_require (parser, CPP_EQ, RT_EQ);
10444 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10446 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10447 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10449 /* Process the initializer. */
10450 cp_finish_decl (decl,
10451 initializer, !non_constant_p,
10452 asm_specification,
10453 flags);
10455 if (pushed_scope)
10456 pop_scope (pushed_scope);
10458 return convert_from_reference (decl);
10461 /* If we didn't even get past the declarator successfully, we are
10462 definitely not looking at a declaration. */
10463 else
10464 cp_parser_abort_tentative_parse (parser);
10466 /* Otherwise, we are looking at an expression. */
10467 return cp_parser_expression (parser);
10470 /* Parses a for-statement or range-for-statement until the closing ')',
10471 not included. */
10473 static tree
10474 cp_parser_for (cp_parser *parser, bool ivdep)
10476 tree init, scope, decl;
10477 bool is_range_for;
10479 /* Begin the for-statement. */
10480 scope = begin_for_scope (&init);
10482 /* Parse the initialization. */
10483 is_range_for = cp_parser_for_init_statement (parser, &decl);
10485 if (is_range_for)
10486 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10487 else
10488 return cp_parser_c_for (parser, scope, init, ivdep);
10491 static tree
10492 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10494 /* Normal for loop */
10495 tree condition = NULL_TREE;
10496 tree expression = NULL_TREE;
10497 tree stmt;
10499 stmt = begin_for_stmt (scope, init);
10500 /* The for-init-statement has already been parsed in
10501 cp_parser_for_init_statement, so no work is needed here. */
10502 finish_for_init_stmt (stmt);
10504 /* If there's a condition, process it. */
10505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10506 condition = cp_parser_condition (parser);
10507 else if (ivdep)
10509 cp_parser_error (parser, "missing loop condition in loop with "
10510 "%<GCC ivdep%> pragma");
10511 condition = error_mark_node;
10513 finish_for_cond (condition, stmt, ivdep);
10514 /* Look for the `;'. */
10515 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10517 /* If there's an expression, process it. */
10518 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10519 expression = cp_parser_expression (parser);
10520 finish_for_expr (expression, stmt);
10522 return stmt;
10525 /* Tries to parse a range-based for-statement:
10527 range-based-for:
10528 decl-specifier-seq declarator : expression
10530 The decl-specifier-seq declarator and the `:' are already parsed by
10531 cp_parser_for_init_statement. If processing_template_decl it returns a
10532 newly created RANGE_FOR_STMT; if not, it is converted to a
10533 regular FOR_STMT. */
10535 static tree
10536 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10537 bool ivdep)
10539 tree stmt, range_expr;
10541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10543 bool expr_non_constant_p;
10544 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10546 else
10547 range_expr = cp_parser_expression (parser);
10549 /* If in template, STMT is converted to a normal for-statement
10550 at instantiation. If not, it is done just ahead. */
10551 if (processing_template_decl)
10553 if (check_for_bare_parameter_packs (range_expr))
10554 range_expr = error_mark_node;
10555 stmt = begin_range_for_stmt (scope, init);
10556 if (ivdep)
10557 RANGE_FOR_IVDEP (stmt) = 1;
10558 finish_range_for_decl (stmt, range_decl, range_expr);
10559 if (!type_dependent_expression_p (range_expr)
10560 /* do_auto_deduction doesn't mess with template init-lists. */
10561 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10562 do_range_for_auto_deduction (range_decl, range_expr);
10564 else
10566 stmt = begin_for_stmt (scope, init);
10567 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10569 return stmt;
10572 /* Subroutine of cp_convert_range_for: given the initializer expression,
10573 builds up the range temporary. */
10575 static tree
10576 build_range_temp (tree range_expr)
10578 tree range_type, range_temp;
10580 /* Find out the type deduced by the declaration
10581 `auto &&__range = range_expr'. */
10582 range_type = cp_build_reference_type (make_auto (), true);
10583 range_type = do_auto_deduction (range_type, range_expr,
10584 type_uses_auto (range_type));
10586 /* Create the __range variable. */
10587 range_temp = build_decl (input_location, VAR_DECL,
10588 get_identifier ("__for_range"), range_type);
10589 TREE_USED (range_temp) = 1;
10590 DECL_ARTIFICIAL (range_temp) = 1;
10592 return range_temp;
10595 /* Used by cp_parser_range_for in template context: we aren't going to
10596 do a full conversion yet, but we still need to resolve auto in the
10597 type of the for-range-declaration if present. This is basically
10598 a shortcut version of cp_convert_range_for. */
10600 static void
10601 do_range_for_auto_deduction (tree decl, tree range_expr)
10603 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10604 if (auto_node)
10606 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10607 range_temp = convert_from_reference (build_range_temp (range_expr));
10608 iter_type = (cp_parser_perform_range_for_lookup
10609 (range_temp, &begin_dummy, &end_dummy));
10610 if (iter_type)
10612 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10613 iter_type);
10614 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10615 tf_warning_or_error);
10616 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10617 iter_decl, auto_node);
10622 /* Converts a range-based for-statement into a normal
10623 for-statement, as per the definition.
10625 for (RANGE_DECL : RANGE_EXPR)
10626 BLOCK
10628 should be equivalent to:
10631 auto &&__range = RANGE_EXPR;
10632 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10633 __begin != __end;
10634 ++__begin)
10636 RANGE_DECL = *__begin;
10637 BLOCK
10641 If RANGE_EXPR is an array:
10642 BEGIN_EXPR = __range
10643 END_EXPR = __range + ARRAY_SIZE(__range)
10644 Else if RANGE_EXPR has a member 'begin' or 'end':
10645 BEGIN_EXPR = __range.begin()
10646 END_EXPR = __range.end()
10647 Else:
10648 BEGIN_EXPR = begin(__range)
10649 END_EXPR = end(__range);
10651 If __range has a member 'begin' but not 'end', or vice versa, we must
10652 still use the second alternative (it will surely fail, however).
10653 When calling begin()/end() in the third alternative we must use
10654 argument dependent lookup, but always considering 'std' as an associated
10655 namespace. */
10657 tree
10658 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10659 bool ivdep)
10661 tree begin, end;
10662 tree iter_type, begin_expr, end_expr;
10663 tree condition, expression;
10665 if (range_decl == error_mark_node || range_expr == error_mark_node)
10666 /* If an error happened previously do nothing or else a lot of
10667 unhelpful errors would be issued. */
10668 begin_expr = end_expr = iter_type = error_mark_node;
10669 else
10671 tree range_temp;
10673 if (TREE_CODE (range_expr) == VAR_DECL
10674 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10675 /* Can't bind a reference to an array of runtime bound. */
10676 range_temp = range_expr;
10677 else
10679 range_temp = build_range_temp (range_expr);
10680 pushdecl (range_temp);
10681 cp_finish_decl (range_temp, range_expr,
10682 /*is_constant_init*/false, NULL_TREE,
10683 LOOKUP_ONLYCONVERTING);
10684 range_temp = convert_from_reference (range_temp);
10686 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10687 &begin_expr, &end_expr);
10690 /* The new for initialization statement. */
10691 begin = build_decl (input_location, VAR_DECL,
10692 get_identifier ("__for_begin"), iter_type);
10693 TREE_USED (begin) = 1;
10694 DECL_ARTIFICIAL (begin) = 1;
10695 pushdecl (begin);
10696 cp_finish_decl (begin, begin_expr,
10697 /*is_constant_init*/false, NULL_TREE,
10698 LOOKUP_ONLYCONVERTING);
10700 end = build_decl (input_location, VAR_DECL,
10701 get_identifier ("__for_end"), iter_type);
10702 TREE_USED (end) = 1;
10703 DECL_ARTIFICIAL (end) = 1;
10704 pushdecl (end);
10705 cp_finish_decl (end, end_expr,
10706 /*is_constant_init*/false, NULL_TREE,
10707 LOOKUP_ONLYCONVERTING);
10709 finish_for_init_stmt (statement);
10711 /* The new for condition. */
10712 condition = build_x_binary_op (input_location, NE_EXPR,
10713 begin, ERROR_MARK,
10714 end, ERROR_MARK,
10715 NULL, tf_warning_or_error);
10716 finish_for_cond (condition, statement, ivdep);
10718 /* The new increment expression. */
10719 expression = finish_unary_op_expr (input_location,
10720 PREINCREMENT_EXPR, begin,
10721 tf_warning_or_error);
10722 finish_for_expr (expression, statement);
10724 /* The declaration is initialized with *__begin inside the loop body. */
10725 cp_finish_decl (range_decl,
10726 build_x_indirect_ref (input_location, begin, RO_NULL,
10727 tf_warning_or_error),
10728 /*is_constant_init*/false, NULL_TREE,
10729 LOOKUP_ONLYCONVERTING);
10731 return statement;
10734 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10735 We need to solve both at the same time because the method used
10736 depends on the existence of members begin or end.
10737 Returns the type deduced for the iterator expression. */
10739 static tree
10740 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10742 if (error_operand_p (range))
10744 *begin = *end = error_mark_node;
10745 return error_mark_node;
10748 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10750 error ("range-based %<for%> expression of type %qT "
10751 "has incomplete type", TREE_TYPE (range));
10752 *begin = *end = error_mark_node;
10753 return error_mark_node;
10755 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10757 /* If RANGE is an array, we will use pointer arithmetic. */
10758 *begin = range;
10759 *end = build_binary_op (input_location, PLUS_EXPR,
10760 range,
10761 array_type_nelts_top (TREE_TYPE (range)),
10763 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10765 else
10767 /* If it is not an array, we must do a bit of magic. */
10768 tree id_begin, id_end;
10769 tree member_begin, member_end;
10771 *begin = *end = error_mark_node;
10773 id_begin = get_identifier ("begin");
10774 id_end = get_identifier ("end");
10775 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10776 /*protect=*/2, /*want_type=*/false,
10777 tf_warning_or_error);
10778 member_end = lookup_member (TREE_TYPE (range), id_end,
10779 /*protect=*/2, /*want_type=*/false,
10780 tf_warning_or_error);
10782 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10784 /* Use the member functions. */
10785 if (member_begin != NULL_TREE)
10786 *begin = cp_parser_range_for_member_function (range, id_begin);
10787 else
10788 error ("range-based %<for%> expression of type %qT has an "
10789 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10791 if (member_end != NULL_TREE)
10792 *end = cp_parser_range_for_member_function (range, id_end);
10793 else
10794 error ("range-based %<for%> expression of type %qT has a "
10795 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10797 else
10799 /* Use global functions with ADL. */
10800 vec<tree, va_gc> *vec;
10801 vec = make_tree_vector ();
10803 vec_safe_push (vec, range);
10805 member_begin = perform_koenig_lookup (id_begin, vec,
10806 tf_warning_or_error);
10807 *begin = finish_call_expr (member_begin, &vec, false, true,
10808 tf_warning_or_error);
10809 member_end = perform_koenig_lookup (id_end, vec,
10810 tf_warning_or_error);
10811 *end = finish_call_expr (member_end, &vec, false, true,
10812 tf_warning_or_error);
10814 release_tree_vector (vec);
10817 /* Last common checks. */
10818 if (*begin == error_mark_node || *end == error_mark_node)
10820 /* If one of the expressions is an error do no more checks. */
10821 *begin = *end = error_mark_node;
10822 return error_mark_node;
10824 else if (type_dependent_expression_p (*begin)
10825 || type_dependent_expression_p (*end))
10826 /* Can happen, when, eg, in a template context, Koenig lookup
10827 can't resolve begin/end (c++/58503). */
10828 return NULL_TREE;
10829 else
10831 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10832 /* The unqualified type of the __begin and __end temporaries should
10833 be the same, as required by the multiple auto declaration. */
10834 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10835 error ("inconsistent begin/end types in range-based %<for%> "
10836 "statement: %qT and %qT",
10837 TREE_TYPE (*begin), TREE_TYPE (*end));
10838 return iter_type;
10843 /* Helper function for cp_parser_perform_range_for_lookup.
10844 Builds a tree for RANGE.IDENTIFIER(). */
10846 static tree
10847 cp_parser_range_for_member_function (tree range, tree identifier)
10849 tree member, res;
10850 vec<tree, va_gc> *vec;
10852 member = finish_class_member_access_expr (range, identifier,
10853 false, tf_warning_or_error);
10854 if (member == error_mark_node)
10855 return error_mark_node;
10857 vec = make_tree_vector ();
10858 res = finish_call_expr (member, &vec,
10859 /*disallow_virtual=*/false,
10860 /*koenig_p=*/false,
10861 tf_warning_or_error);
10862 release_tree_vector (vec);
10863 return res;
10866 /* Parse an iteration-statement.
10868 iteration-statement:
10869 while ( condition ) statement
10870 do statement while ( expression ) ;
10871 for ( for-init-statement condition [opt] ; expression [opt] )
10872 statement
10874 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10876 static tree
10877 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10879 cp_token *token;
10880 enum rid keyword;
10881 tree statement;
10882 unsigned char in_statement;
10884 /* Peek at the next token. */
10885 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10886 if (!token)
10887 return error_mark_node;
10889 /* Remember whether or not we are already within an iteration
10890 statement. */
10891 in_statement = parser->in_statement;
10893 /* See what kind of keyword it is. */
10894 keyword = token->keyword;
10895 switch (keyword)
10897 case RID_WHILE:
10899 tree condition;
10901 /* Begin the while-statement. */
10902 statement = begin_while_stmt ();
10903 /* Look for the `('. */
10904 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10905 /* Parse the condition. */
10906 condition = cp_parser_condition (parser);
10907 finish_while_stmt_cond (condition, statement, ivdep);
10908 /* Look for the `)'. */
10909 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10910 /* Parse the dependent statement. */
10911 parser->in_statement = IN_ITERATION_STMT;
10912 cp_parser_already_scoped_statement (parser);
10913 parser->in_statement = in_statement;
10914 /* We're done with the while-statement. */
10915 finish_while_stmt (statement);
10917 break;
10919 case RID_DO:
10921 tree expression;
10923 /* Begin the do-statement. */
10924 statement = begin_do_stmt ();
10925 /* Parse the body of the do-statement. */
10926 parser->in_statement = IN_ITERATION_STMT;
10927 cp_parser_implicitly_scoped_statement (parser, NULL);
10928 parser->in_statement = in_statement;
10929 finish_do_body (statement);
10930 /* Look for the `while' keyword. */
10931 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10932 /* Look for the `('. */
10933 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10934 /* Parse the expression. */
10935 expression = cp_parser_expression (parser);
10936 /* We're done with the do-statement. */
10937 finish_do_stmt (expression, statement, ivdep);
10938 /* Look for the `)'. */
10939 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10940 /* Look for the `;'. */
10941 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10943 break;
10945 case RID_FOR:
10947 /* Look for the `('. */
10948 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10950 statement = cp_parser_for (parser, ivdep);
10952 /* Look for the `)'. */
10953 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10955 /* Parse the body of the for-statement. */
10956 parser->in_statement = IN_ITERATION_STMT;
10957 cp_parser_already_scoped_statement (parser);
10958 parser->in_statement = in_statement;
10960 /* We're done with the for-statement. */
10961 finish_for_stmt (statement);
10963 break;
10965 default:
10966 cp_parser_error (parser, "expected iteration-statement");
10967 statement = error_mark_node;
10968 break;
10971 return statement;
10974 /* Parse a for-init-statement or the declarator of a range-based-for.
10975 Returns true if a range-based-for declaration is seen.
10977 for-init-statement:
10978 expression-statement
10979 simple-declaration */
10981 static bool
10982 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10984 /* If the next token is a `;', then we have an empty
10985 expression-statement. Grammatically, this is also a
10986 simple-declaration, but an invalid one, because it does not
10987 declare anything. Therefore, if we did not handle this case
10988 specially, we would issue an error message about an invalid
10989 declaration. */
10990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10992 bool is_range_for = false;
10993 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10995 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10996 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10998 /* N3994 -- for (id : init) ... */
10999 if (cxx_dialect < cxx1z)
11000 pedwarn (input_location, 0, "range-based for loop without a "
11001 "type-specifier only available with "
11002 "-std=c++1z or -std=gnu++1z");
11003 tree name = cp_parser_identifier (parser);
11004 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
11005 *decl = build_decl (input_location, VAR_DECL, name, type);
11006 pushdecl (*decl);
11007 cp_lexer_consume_token (parser->lexer);
11008 return true;
11011 /* A colon is used in range-based for. */
11012 parser->colon_corrects_to_scope_p = false;
11014 /* We're going to speculatively look for a declaration, falling back
11015 to an expression, if necessary. */
11016 cp_parser_parse_tentatively (parser);
11017 /* Parse the declaration. */
11018 cp_parser_simple_declaration (parser,
11019 /*function_definition_allowed_p=*/false,
11020 decl);
11021 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11022 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11024 /* It is a range-for, consume the ':' */
11025 cp_lexer_consume_token (parser->lexer);
11026 is_range_for = true;
11027 if (cxx_dialect < cxx11)
11029 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11030 "range-based %<for%> loops only available with "
11031 "-std=c++11 or -std=gnu++11");
11032 *decl = error_mark_node;
11035 else
11036 /* The ';' is not consumed yet because we told
11037 cp_parser_simple_declaration not to. */
11038 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11040 if (cp_parser_parse_definitely (parser))
11041 return is_range_for;
11042 /* If the tentative parse failed, then we shall need to look for an
11043 expression-statement. */
11045 /* If we are here, it is an expression-statement. */
11046 cp_parser_expression_statement (parser, NULL_TREE);
11047 return false;
11050 /* Parse a jump-statement.
11052 jump-statement:
11053 break ;
11054 continue ;
11055 return expression [opt] ;
11056 return braced-init-list ;
11057 goto identifier ;
11059 GNU extension:
11061 jump-statement:
11062 goto * expression ;
11064 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11066 static tree
11067 cp_parser_jump_statement (cp_parser* parser)
11069 tree statement = error_mark_node;
11070 cp_token *token;
11071 enum rid keyword;
11072 unsigned char in_statement;
11074 /* Peek at the next token. */
11075 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11076 if (!token)
11077 return error_mark_node;
11079 /* See what kind of keyword it is. */
11080 keyword = token->keyword;
11081 switch (keyword)
11083 case RID_BREAK:
11084 in_statement = parser->in_statement & ~IN_IF_STMT;
11085 switch (in_statement)
11087 case 0:
11088 error_at (token->location, "break statement not within loop or switch");
11089 break;
11090 default:
11091 gcc_assert ((in_statement & IN_SWITCH_STMT)
11092 || in_statement == IN_ITERATION_STMT);
11093 statement = finish_break_stmt ();
11094 if (in_statement == IN_ITERATION_STMT)
11095 break_maybe_infinite_loop ();
11096 break;
11097 case IN_OMP_BLOCK:
11098 error_at (token->location, "invalid exit from OpenMP structured block");
11099 break;
11100 case IN_OMP_FOR:
11101 error_at (token->location, "break statement used with OpenMP for loop");
11102 break;
11103 case IN_CILK_SIMD_FOR:
11104 error_at (token->location, "break statement used with Cilk Plus for loop");
11105 break;
11107 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11108 break;
11110 case RID_CONTINUE:
11111 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11113 case 0:
11114 error_at (token->location, "continue statement not within a loop");
11115 break;
11116 case IN_CILK_SIMD_FOR:
11117 error_at (token->location,
11118 "continue statement within %<#pragma simd%> loop body");
11119 /* Fall through. */
11120 case IN_ITERATION_STMT:
11121 case IN_OMP_FOR:
11122 statement = finish_continue_stmt ();
11123 break;
11124 case IN_OMP_BLOCK:
11125 error_at (token->location, "invalid exit from OpenMP structured block");
11126 break;
11127 default:
11128 gcc_unreachable ();
11130 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11131 break;
11133 case RID_RETURN:
11135 tree expr;
11136 bool expr_non_constant_p;
11138 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11140 cp_lexer_set_source_position (parser->lexer);
11141 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11142 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11144 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11145 expr = cp_parser_expression (parser);
11146 else
11147 /* If the next token is a `;', then there is no
11148 expression. */
11149 expr = NULL_TREE;
11150 /* Build the return-statement. */
11151 statement = finish_return_stmt (expr);
11152 /* Look for the final `;'. */
11153 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11155 break;
11157 case RID_GOTO:
11158 if (parser->in_function_body
11159 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11161 error ("%<goto%> in %<constexpr%> function");
11162 cp_function_chain->invalid_constexpr = true;
11165 /* Create the goto-statement. */
11166 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11168 /* Issue a warning about this use of a GNU extension. */
11169 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11170 /* Consume the '*' token. */
11171 cp_lexer_consume_token (parser->lexer);
11172 /* Parse the dependent expression. */
11173 finish_goto_stmt (cp_parser_expression (parser));
11175 else
11176 finish_goto_stmt (cp_parser_identifier (parser));
11177 /* Look for the final `;'. */
11178 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11179 break;
11181 default:
11182 cp_parser_error (parser, "expected jump-statement");
11183 break;
11186 return statement;
11189 /* Parse a declaration-statement.
11191 declaration-statement:
11192 block-declaration */
11194 static void
11195 cp_parser_declaration_statement (cp_parser* parser)
11197 void *p;
11199 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11200 p = obstack_alloc (&declarator_obstack, 0);
11202 /* Parse the block-declaration. */
11203 cp_parser_block_declaration (parser, /*statement_p=*/true);
11205 /* Free any declarators allocated. */
11206 obstack_free (&declarator_obstack, p);
11209 /* Some dependent statements (like `if (cond) statement'), are
11210 implicitly in their own scope. In other words, if the statement is
11211 a single statement (as opposed to a compound-statement), it is
11212 none-the-less treated as if it were enclosed in braces. Any
11213 declarations appearing in the dependent statement are out of scope
11214 after control passes that point. This function parses a statement,
11215 but ensures that is in its own scope, even if it is not a
11216 compound-statement.
11218 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11219 is a (possibly labeled) if statement which is not enclosed in
11220 braces and has an else clause. This is used to implement
11221 -Wparentheses.
11223 Returns the new statement. */
11225 static tree
11226 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11228 tree statement;
11230 if (if_p != NULL)
11231 *if_p = false;
11233 /* Mark if () ; with a special NOP_EXPR. */
11234 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11236 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11237 cp_lexer_consume_token (parser->lexer);
11238 statement = add_stmt (build_empty_stmt (loc));
11240 /* if a compound is opened, we simply parse the statement directly. */
11241 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11242 statement = cp_parser_compound_statement (parser, NULL, false, false);
11243 /* If the token is not a `{', then we must take special action. */
11244 else
11246 /* Create a compound-statement. */
11247 statement = begin_compound_stmt (0);
11248 /* Parse the dependent-statement. */
11249 cp_parser_statement (parser, NULL_TREE, false, if_p);
11250 /* Finish the dummy compound-statement. */
11251 finish_compound_stmt (statement);
11254 /* Return the statement. */
11255 return statement;
11258 /* For some dependent statements (like `while (cond) statement'), we
11259 have already created a scope. Therefore, even if the dependent
11260 statement is a compound-statement, we do not want to create another
11261 scope. */
11263 static void
11264 cp_parser_already_scoped_statement (cp_parser* parser)
11266 /* If the token is a `{', then we must take special action. */
11267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11268 cp_parser_statement (parser, NULL_TREE, false, NULL);
11269 else
11271 /* Avoid calling cp_parser_compound_statement, so that we
11272 don't create a new scope. Do everything else by hand. */
11273 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11274 /* If the next keyword is `__label__' we have a label declaration. */
11275 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11276 cp_parser_label_declaration (parser);
11277 /* Parse an (optional) statement-seq. */
11278 cp_parser_statement_seq_opt (parser, NULL_TREE);
11279 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11283 /* Declarations [gram.dcl.dcl] */
11285 /* Parse an optional declaration-sequence.
11287 declaration-seq:
11288 declaration
11289 declaration-seq declaration */
11291 static void
11292 cp_parser_declaration_seq_opt (cp_parser* parser)
11294 while (true)
11296 cp_token *token;
11298 token = cp_lexer_peek_token (parser->lexer);
11300 if (token->type == CPP_CLOSE_BRACE
11301 || token->type == CPP_EOF
11302 || token->type == CPP_PRAGMA_EOL)
11303 break;
11305 if (token->type == CPP_SEMICOLON)
11307 /* A declaration consisting of a single semicolon is
11308 invalid. Allow it unless we're being pedantic. */
11309 cp_lexer_consume_token (parser->lexer);
11310 if (!in_system_header_at (input_location))
11311 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11312 continue;
11315 /* If we're entering or exiting a region that's implicitly
11316 extern "C", modify the lang context appropriately. */
11317 if (!parser->implicit_extern_c && token->implicit_extern_c)
11319 push_lang_context (lang_name_c);
11320 parser->implicit_extern_c = true;
11322 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11324 pop_lang_context ();
11325 parser->implicit_extern_c = false;
11328 if (token->type == CPP_PRAGMA)
11330 /* A top-level declaration can consist solely of a #pragma.
11331 A nested declaration cannot, so this is done here and not
11332 in cp_parser_declaration. (A #pragma at block scope is
11333 handled in cp_parser_statement.) */
11334 cp_parser_pragma (parser, pragma_external);
11335 continue;
11338 /* Parse the declaration itself. */
11339 cp_parser_declaration (parser);
11343 /* Parse a declaration.
11345 declaration:
11346 block-declaration
11347 function-definition
11348 template-declaration
11349 explicit-instantiation
11350 explicit-specialization
11351 linkage-specification
11352 namespace-definition
11354 GNU extension:
11356 declaration:
11357 __extension__ declaration */
11359 static void
11360 cp_parser_declaration (cp_parser* parser)
11362 cp_token token1;
11363 cp_token token2;
11364 int saved_pedantic;
11365 void *p;
11366 tree attributes = NULL_TREE;
11368 /* Check for the `__extension__' keyword. */
11369 if (cp_parser_extension_opt (parser, &saved_pedantic))
11371 /* Parse the qualified declaration. */
11372 cp_parser_declaration (parser);
11373 /* Restore the PEDANTIC flag. */
11374 pedantic = saved_pedantic;
11376 return;
11379 /* Try to figure out what kind of declaration is present. */
11380 token1 = *cp_lexer_peek_token (parser->lexer);
11382 if (token1.type != CPP_EOF)
11383 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11384 else
11386 token2.type = CPP_EOF;
11387 token2.keyword = RID_MAX;
11390 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11391 p = obstack_alloc (&declarator_obstack, 0);
11393 /* If the next token is `extern' and the following token is a string
11394 literal, then we have a linkage specification. */
11395 if (token1.keyword == RID_EXTERN
11396 && cp_parser_is_pure_string_literal (&token2))
11397 cp_parser_linkage_specification (parser);
11398 /* If the next token is `template', then we have either a template
11399 declaration, an explicit instantiation, or an explicit
11400 specialization. */
11401 else if (token1.keyword == RID_TEMPLATE)
11403 /* `template <>' indicates a template specialization. */
11404 if (token2.type == CPP_LESS
11405 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11406 cp_parser_explicit_specialization (parser);
11407 /* `template <' indicates a template declaration. */
11408 else if (token2.type == CPP_LESS)
11409 cp_parser_template_declaration (parser, /*member_p=*/false);
11410 /* Anything else must be an explicit instantiation. */
11411 else
11412 cp_parser_explicit_instantiation (parser);
11414 /* If the next token is `export', then we have a template
11415 declaration. */
11416 else if (token1.keyword == RID_EXPORT)
11417 cp_parser_template_declaration (parser, /*member_p=*/false);
11418 /* If the next token is `extern', 'static' or 'inline' and the one
11419 after that is `template', we have a GNU extended explicit
11420 instantiation directive. */
11421 else if (cp_parser_allow_gnu_extensions_p (parser)
11422 && (token1.keyword == RID_EXTERN
11423 || token1.keyword == RID_STATIC
11424 || token1.keyword == RID_INLINE)
11425 && token2.keyword == RID_TEMPLATE)
11426 cp_parser_explicit_instantiation (parser);
11427 /* If the next token is `namespace', check for a named or unnamed
11428 namespace definition. */
11429 else if (token1.keyword == RID_NAMESPACE
11430 && (/* A named namespace definition. */
11431 (token2.type == CPP_NAME
11432 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11433 != CPP_EQ))
11434 /* An unnamed namespace definition. */
11435 || token2.type == CPP_OPEN_BRACE
11436 || token2.keyword == RID_ATTRIBUTE))
11437 cp_parser_namespace_definition (parser);
11438 /* An inline (associated) namespace definition. */
11439 else if (token1.keyword == RID_INLINE
11440 && token2.keyword == RID_NAMESPACE)
11441 cp_parser_namespace_definition (parser);
11442 /* Objective-C++ declaration/definition. */
11443 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11444 cp_parser_objc_declaration (parser, NULL_TREE);
11445 else if (c_dialect_objc ()
11446 && token1.keyword == RID_ATTRIBUTE
11447 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11448 cp_parser_objc_declaration (parser, attributes);
11449 /* We must have either a block declaration or a function
11450 definition. */
11451 else
11453 /* At this point we may have a template declared by a concept
11454 introduction. */
11455 cp_parser_parse_tentatively (parser);
11456 cp_parser_template_declaration (parser, /*member_p=*/false);
11458 if (!cp_parser_parse_definitely (parser))
11459 /* Try to parse a block-declaration, or a function-definition. */
11460 cp_parser_block_declaration (parser, /*statement_p=*/false);
11463 /* Free any declarators allocated. */
11464 obstack_free (&declarator_obstack, p);
11467 /* Parse a block-declaration.
11469 block-declaration:
11470 simple-declaration
11471 asm-definition
11472 namespace-alias-definition
11473 using-declaration
11474 using-directive
11476 GNU Extension:
11478 block-declaration:
11479 __extension__ block-declaration
11481 C++0x Extension:
11483 block-declaration:
11484 static_assert-declaration
11486 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11487 part of a declaration-statement. */
11489 static void
11490 cp_parser_block_declaration (cp_parser *parser,
11491 bool statement_p)
11493 cp_token *token1;
11494 int saved_pedantic;
11496 /* Check for the `__extension__' keyword. */
11497 if (cp_parser_extension_opt (parser, &saved_pedantic))
11499 /* Parse the qualified declaration. */
11500 cp_parser_block_declaration (parser, statement_p);
11501 /* Restore the PEDANTIC flag. */
11502 pedantic = saved_pedantic;
11504 return;
11507 /* Peek at the next token to figure out which kind of declaration is
11508 present. */
11509 token1 = cp_lexer_peek_token (parser->lexer);
11511 /* If the next keyword is `asm', we have an asm-definition. */
11512 if (token1->keyword == RID_ASM)
11514 if (statement_p)
11515 cp_parser_commit_to_tentative_parse (parser);
11516 cp_parser_asm_definition (parser);
11518 /* If the next keyword is `namespace', we have a
11519 namespace-alias-definition. */
11520 else if (token1->keyword == RID_NAMESPACE)
11521 cp_parser_namespace_alias_definition (parser);
11522 /* If the next keyword is `using', we have a
11523 using-declaration, a using-directive, or an alias-declaration. */
11524 else if (token1->keyword == RID_USING)
11526 cp_token *token2;
11528 if (statement_p)
11529 cp_parser_commit_to_tentative_parse (parser);
11530 /* If the token after `using' is `namespace', then we have a
11531 using-directive. */
11532 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11533 if (token2->keyword == RID_NAMESPACE)
11534 cp_parser_using_directive (parser);
11535 /* If the second token after 'using' is '=', then we have an
11536 alias-declaration. */
11537 else if (cxx_dialect >= cxx11
11538 && token2->type == CPP_NAME
11539 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11540 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11541 cp_parser_alias_declaration (parser);
11542 /* Otherwise, it's a using-declaration. */
11543 else
11544 cp_parser_using_declaration (parser,
11545 /*access_declaration_p=*/false);
11547 /* If the next keyword is `__label__' we have a misplaced label
11548 declaration. */
11549 else if (token1->keyword == RID_LABEL)
11551 cp_lexer_consume_token (parser->lexer);
11552 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11553 cp_parser_skip_to_end_of_statement (parser);
11554 /* If the next token is now a `;', consume it. */
11555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11556 cp_lexer_consume_token (parser->lexer);
11558 /* If the next token is `static_assert' we have a static assertion. */
11559 else if (token1->keyword == RID_STATIC_ASSERT)
11560 cp_parser_static_assert (parser, /*member_p=*/false);
11561 /* Anything else must be a simple-declaration. */
11562 else
11563 cp_parser_simple_declaration (parser, !statement_p,
11564 /*maybe_range_for_decl*/NULL);
11567 /* Parse a simple-declaration.
11569 simple-declaration:
11570 decl-specifier-seq [opt] init-declarator-list [opt] ;
11572 init-declarator-list:
11573 init-declarator
11574 init-declarator-list , init-declarator
11576 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11577 function-definition as a simple-declaration.
11579 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11580 parsed declaration if it is an uninitialized single declarator not followed
11581 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11582 if present, will not be consumed. */
11584 static void
11585 cp_parser_simple_declaration (cp_parser* parser,
11586 bool function_definition_allowed_p,
11587 tree *maybe_range_for_decl)
11589 cp_decl_specifier_seq decl_specifiers;
11590 int declares_class_or_enum;
11591 bool saw_declarator;
11592 location_t comma_loc = UNKNOWN_LOCATION;
11593 location_t init_loc = UNKNOWN_LOCATION;
11595 if (maybe_range_for_decl)
11596 *maybe_range_for_decl = NULL_TREE;
11598 /* Defer access checks until we know what is being declared; the
11599 checks for names appearing in the decl-specifier-seq should be
11600 done as if we were in the scope of the thing being declared. */
11601 push_deferring_access_checks (dk_deferred);
11603 /* Parse the decl-specifier-seq. We have to keep track of whether
11604 or not the decl-specifier-seq declares a named class or
11605 enumeration type, since that is the only case in which the
11606 init-declarator-list is allowed to be empty.
11608 [dcl.dcl]
11610 In a simple-declaration, the optional init-declarator-list can be
11611 omitted only when declaring a class or enumeration, that is when
11612 the decl-specifier-seq contains either a class-specifier, an
11613 elaborated-type-specifier, or an enum-specifier. */
11614 cp_parser_decl_specifier_seq (parser,
11615 CP_PARSER_FLAGS_OPTIONAL,
11616 &decl_specifiers,
11617 &declares_class_or_enum);
11618 /* We no longer need to defer access checks. */
11619 stop_deferring_access_checks ();
11621 /* In a block scope, a valid declaration must always have a
11622 decl-specifier-seq. By not trying to parse declarators, we can
11623 resolve the declaration/expression ambiguity more quickly. */
11624 if (!function_definition_allowed_p
11625 && !decl_specifiers.any_specifiers_p)
11627 cp_parser_error (parser, "expected declaration");
11628 goto done;
11631 /* If the next two tokens are both identifiers, the code is
11632 erroneous. The usual cause of this situation is code like:
11634 T t;
11636 where "T" should name a type -- but does not. */
11637 if (!decl_specifiers.any_type_specifiers_p
11638 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11640 /* If parsing tentatively, we should commit; we really are
11641 looking at a declaration. */
11642 cp_parser_commit_to_tentative_parse (parser);
11643 /* Give up. */
11644 goto done;
11647 /* If we have seen at least one decl-specifier, and the next token
11648 is not a parenthesis, then we must be looking at a declaration.
11649 (After "int (" we might be looking at a functional cast.) */
11650 if (decl_specifiers.any_specifiers_p
11651 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11652 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11653 && !cp_parser_error_occurred (parser))
11654 cp_parser_commit_to_tentative_parse (parser);
11656 /* Keep going until we hit the `;' at the end of the simple
11657 declaration. */
11658 saw_declarator = false;
11659 while (cp_lexer_next_token_is_not (parser->lexer,
11660 CPP_SEMICOLON))
11662 cp_token *token;
11663 bool function_definition_p;
11664 tree decl;
11666 if (saw_declarator)
11668 /* If we are processing next declarator, comma is expected */
11669 token = cp_lexer_peek_token (parser->lexer);
11670 gcc_assert (token->type == CPP_COMMA);
11671 cp_lexer_consume_token (parser->lexer);
11672 if (maybe_range_for_decl)
11674 *maybe_range_for_decl = error_mark_node;
11675 if (comma_loc == UNKNOWN_LOCATION)
11676 comma_loc = token->location;
11679 else
11680 saw_declarator = true;
11682 /* Parse the init-declarator. */
11683 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11684 /*checks=*/NULL,
11685 function_definition_allowed_p,
11686 /*member_p=*/false,
11687 declares_class_or_enum,
11688 &function_definition_p,
11689 maybe_range_for_decl,
11690 &init_loc);
11691 /* If an error occurred while parsing tentatively, exit quickly.
11692 (That usually happens when in the body of a function; each
11693 statement is treated as a declaration-statement until proven
11694 otherwise.) */
11695 if (cp_parser_error_occurred (parser))
11696 goto done;
11697 /* Handle function definitions specially. */
11698 if (function_definition_p)
11700 /* If the next token is a `,', then we are probably
11701 processing something like:
11703 void f() {}, *p;
11705 which is erroneous. */
11706 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11708 cp_token *token = cp_lexer_peek_token (parser->lexer);
11709 error_at (token->location,
11710 "mixing"
11711 " declarations and function-definitions is forbidden");
11713 /* Otherwise, we're done with the list of declarators. */
11714 else
11716 pop_deferring_access_checks ();
11717 return;
11720 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11721 *maybe_range_for_decl = decl;
11722 /* The next token should be either a `,' or a `;'. */
11723 token = cp_lexer_peek_token (parser->lexer);
11724 /* If it's a `,', there are more declarators to come. */
11725 if (token->type == CPP_COMMA)
11726 /* will be consumed next time around */;
11727 /* If it's a `;', we are done. */
11728 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11729 break;
11730 /* Anything else is an error. */
11731 else
11733 /* If we have already issued an error message we don't need
11734 to issue another one. */
11735 if (decl != error_mark_node
11736 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11737 cp_parser_error (parser, "expected %<,%> or %<;%>");
11738 /* Skip tokens until we reach the end of the statement. */
11739 cp_parser_skip_to_end_of_statement (parser);
11740 /* If the next token is now a `;', consume it. */
11741 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11742 cp_lexer_consume_token (parser->lexer);
11743 goto done;
11745 /* After the first time around, a function-definition is not
11746 allowed -- even if it was OK at first. For example:
11748 int i, f() {}
11750 is not valid. */
11751 function_definition_allowed_p = false;
11754 /* Issue an error message if no declarators are present, and the
11755 decl-specifier-seq does not itself declare a class or
11756 enumeration: [dcl.dcl]/3. */
11757 if (!saw_declarator)
11759 if (cp_parser_declares_only_class_p (parser))
11761 if (!declares_class_or_enum
11762 && decl_specifiers.type
11763 && OVERLOAD_TYPE_P (decl_specifiers.type))
11764 /* Ensure an error is issued anyway when finish_decltype_type,
11765 called via cp_parser_decl_specifier_seq, returns a class or
11766 an enumeration (c++/51786). */
11767 decl_specifiers.type = NULL_TREE;
11768 shadow_tag (&decl_specifiers);
11770 /* Perform any deferred access checks. */
11771 perform_deferred_access_checks (tf_warning_or_error);
11774 /* Consume the `;'. */
11775 if (!maybe_range_for_decl)
11776 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11777 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11779 if (init_loc != UNKNOWN_LOCATION)
11780 error_at (init_loc, "initializer in range-based %<for%> loop");
11781 if (comma_loc != UNKNOWN_LOCATION)
11782 error_at (comma_loc,
11783 "multiple declarations in range-based %<for%> loop");
11786 done:
11787 pop_deferring_access_checks ();
11790 /* Parse a decl-specifier-seq.
11792 decl-specifier-seq:
11793 decl-specifier-seq [opt] decl-specifier
11794 decl-specifier attribute-specifier-seq [opt] (C++11)
11796 decl-specifier:
11797 storage-class-specifier
11798 type-specifier
11799 function-specifier
11800 friend
11801 typedef
11803 GNU Extension:
11805 decl-specifier:
11806 attributes
11808 Concepts Extension:
11810 decl-specifier:
11811 concept
11814 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11816 The parser flags FLAGS is used to control type-specifier parsing.
11818 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11819 flags:
11821 1: one of the decl-specifiers is an elaborated-type-specifier
11822 (i.e., a type declaration)
11823 2: one of the decl-specifiers is an enum-specifier or a
11824 class-specifier (i.e., a type definition)
11828 static void
11829 cp_parser_decl_specifier_seq (cp_parser* parser,
11830 cp_parser_flags flags,
11831 cp_decl_specifier_seq *decl_specs,
11832 int* declares_class_or_enum)
11834 bool constructor_possible_p = !parser->in_declarator_p;
11835 bool found_decl_spec = false;
11836 cp_token *start_token = NULL;
11837 cp_decl_spec ds;
11839 /* Clear DECL_SPECS. */
11840 clear_decl_specs (decl_specs);
11842 /* Assume no class or enumeration type is declared. */
11843 *declares_class_or_enum = 0;
11845 /* Keep reading specifiers until there are no more to read. */
11846 while (true)
11848 bool constructor_p;
11849 cp_token *token;
11850 ds = ds_last;
11852 /* Peek at the next token. */
11853 token = cp_lexer_peek_token (parser->lexer);
11855 /* Save the first token of the decl spec list for error
11856 reporting. */
11857 if (!start_token)
11858 start_token = token;
11859 /* Handle attributes. */
11860 if (cp_next_tokens_can_be_attribute_p (parser))
11862 /* Parse the attributes. */
11863 tree attrs = cp_parser_attributes_opt (parser);
11865 /* In a sequence of declaration specifiers, c++11 attributes
11866 appertain to the type that precede them. In that case
11867 [dcl.spec]/1 says:
11869 The attribute-specifier-seq affects the type only for
11870 the declaration it appears in, not other declarations
11871 involving the same type.
11873 But for now let's force the user to position the
11874 attribute either at the beginning of the declaration or
11875 after the declarator-id, which would clearly mean that it
11876 applies to the declarator. */
11877 if (cxx11_attribute_p (attrs))
11879 if (!found_decl_spec)
11880 /* The c++11 attribute is at the beginning of the
11881 declaration. It appertains to the entity being
11882 declared. */;
11883 else
11885 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11887 /* This is an attribute following a
11888 class-specifier. */
11889 if (decl_specs->type_definition_p)
11890 warn_misplaced_attr_for_class_type (token->location,
11891 decl_specs->type);
11892 attrs = NULL_TREE;
11894 else
11896 decl_specs->std_attributes
11897 = chainon (decl_specs->std_attributes,
11898 attrs);
11899 if (decl_specs->locations[ds_std_attribute] == 0)
11900 decl_specs->locations[ds_std_attribute] = token->location;
11902 continue;
11906 decl_specs->attributes
11907 = chainon (decl_specs->attributes,
11908 attrs);
11909 if (decl_specs->locations[ds_attribute] == 0)
11910 decl_specs->locations[ds_attribute] = token->location;
11911 continue;
11913 /* Assume we will find a decl-specifier keyword. */
11914 found_decl_spec = true;
11915 /* If the next token is an appropriate keyword, we can simply
11916 add it to the list. */
11917 switch (token->keyword)
11919 /* decl-specifier:
11920 friend
11921 constexpr */
11922 case RID_FRIEND:
11923 if (!at_class_scope_p ())
11925 error_at (token->location, "%<friend%> used outside of class");
11926 cp_lexer_purge_token (parser->lexer);
11928 else
11930 ds = ds_friend;
11931 /* Consume the token. */
11932 cp_lexer_consume_token (parser->lexer);
11934 break;
11936 case RID_CONSTEXPR:
11937 ds = ds_constexpr;
11938 cp_lexer_consume_token (parser->lexer);
11939 break;
11941 case RID_CONCEPT:
11942 ds = ds_concept;
11943 cp_lexer_consume_token (parser->lexer);
11944 break;
11946 /* function-specifier:
11947 inline
11948 virtual
11949 explicit */
11950 case RID_INLINE:
11951 case RID_VIRTUAL:
11952 case RID_EXPLICIT:
11953 cp_parser_function_specifier_opt (parser, decl_specs);
11954 break;
11956 /* decl-specifier:
11957 typedef */
11958 case RID_TYPEDEF:
11959 ds = ds_typedef;
11960 /* Consume the token. */
11961 cp_lexer_consume_token (parser->lexer);
11962 /* A constructor declarator cannot appear in a typedef. */
11963 constructor_possible_p = false;
11964 /* The "typedef" keyword can only occur in a declaration; we
11965 may as well commit at this point. */
11966 cp_parser_commit_to_tentative_parse (parser);
11968 if (decl_specs->storage_class != sc_none)
11969 decl_specs->conflicting_specifiers_p = true;
11970 break;
11972 /* storage-class-specifier:
11973 auto
11974 register
11975 static
11976 extern
11977 mutable
11979 GNU Extension:
11980 thread */
11981 case RID_AUTO:
11982 if (cxx_dialect == cxx98)
11984 /* Consume the token. */
11985 cp_lexer_consume_token (parser->lexer);
11987 /* Complain about `auto' as a storage specifier, if
11988 we're complaining about C++0x compatibility. */
11989 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11990 " changes meaning in C++11; please remove it");
11992 /* Set the storage class anyway. */
11993 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11994 token);
11996 else
11997 /* C++0x auto type-specifier. */
11998 found_decl_spec = false;
11999 break;
12001 case RID_REGISTER:
12002 case RID_STATIC:
12003 case RID_EXTERN:
12004 case RID_MUTABLE:
12005 /* Consume the token. */
12006 cp_lexer_consume_token (parser->lexer);
12007 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12008 token);
12009 break;
12010 case RID_THREAD:
12011 /* Consume the token. */
12012 ds = ds_thread;
12013 cp_lexer_consume_token (parser->lexer);
12014 break;
12016 default:
12017 /* We did not yet find a decl-specifier yet. */
12018 found_decl_spec = false;
12019 break;
12022 if (found_decl_spec
12023 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12024 && token->keyword != RID_CONSTEXPR)
12025 error ("decl-specifier invalid in condition");
12027 if (ds != ds_last)
12028 set_and_check_decl_spec_loc (decl_specs, ds, token);
12030 /* Constructors are a special case. The `S' in `S()' is not a
12031 decl-specifier; it is the beginning of the declarator. */
12032 constructor_p
12033 = (!found_decl_spec
12034 && constructor_possible_p
12035 && (cp_parser_constructor_declarator_p
12036 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12038 /* If we don't have a DECL_SPEC yet, then we must be looking at
12039 a type-specifier. */
12040 if (!found_decl_spec && !constructor_p)
12042 int decl_spec_declares_class_or_enum;
12043 bool is_cv_qualifier;
12044 tree type_spec;
12046 type_spec
12047 = cp_parser_type_specifier (parser, flags,
12048 decl_specs,
12049 /*is_declaration=*/true,
12050 &decl_spec_declares_class_or_enum,
12051 &is_cv_qualifier);
12052 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12054 /* If this type-specifier referenced a user-defined type
12055 (a typedef, class-name, etc.), then we can't allow any
12056 more such type-specifiers henceforth.
12058 [dcl.spec]
12060 The longest sequence of decl-specifiers that could
12061 possibly be a type name is taken as the
12062 decl-specifier-seq of a declaration. The sequence shall
12063 be self-consistent as described below.
12065 [dcl.type]
12067 As a general rule, at most one type-specifier is allowed
12068 in the complete decl-specifier-seq of a declaration. The
12069 only exceptions are the following:
12071 -- const or volatile can be combined with any other
12072 type-specifier.
12074 -- signed or unsigned can be combined with char, long,
12075 short, or int.
12077 -- ..
12079 Example:
12081 typedef char* Pc;
12082 void g (const int Pc);
12084 Here, Pc is *not* part of the decl-specifier seq; it's
12085 the declarator. Therefore, once we see a type-specifier
12086 (other than a cv-qualifier), we forbid any additional
12087 user-defined types. We *do* still allow things like `int
12088 int' to be considered a decl-specifier-seq, and issue the
12089 error message later. */
12090 if (type_spec && !is_cv_qualifier)
12091 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12092 /* A constructor declarator cannot follow a type-specifier. */
12093 if (type_spec)
12095 constructor_possible_p = false;
12096 found_decl_spec = true;
12097 if (!is_cv_qualifier)
12098 decl_specs->any_type_specifiers_p = true;
12102 /* If we still do not have a DECL_SPEC, then there are no more
12103 decl-specifiers. */
12104 if (!found_decl_spec)
12105 break;
12107 decl_specs->any_specifiers_p = true;
12108 /* After we see one decl-specifier, further decl-specifiers are
12109 always optional. */
12110 flags |= CP_PARSER_FLAGS_OPTIONAL;
12113 /* Don't allow a friend specifier with a class definition. */
12114 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12115 && (*declares_class_or_enum & 2))
12116 error_at (decl_specs->locations[ds_friend],
12117 "class definition may not be declared a friend");
12120 /* Parse an (optional) storage-class-specifier.
12122 storage-class-specifier:
12123 auto
12124 register
12125 static
12126 extern
12127 mutable
12129 GNU Extension:
12131 storage-class-specifier:
12132 thread
12134 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12136 static tree
12137 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12139 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12141 case RID_AUTO:
12142 if (cxx_dialect != cxx98)
12143 return NULL_TREE;
12144 /* Fall through for C++98. */
12146 case RID_REGISTER:
12147 case RID_STATIC:
12148 case RID_EXTERN:
12149 case RID_MUTABLE:
12150 case RID_THREAD:
12151 /* Consume the token. */
12152 return cp_lexer_consume_token (parser->lexer)->u.value;
12154 default:
12155 return NULL_TREE;
12159 /* Parse an (optional) function-specifier.
12161 function-specifier:
12162 inline
12163 virtual
12164 explicit
12166 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12167 Updates DECL_SPECS, if it is non-NULL. */
12169 static tree
12170 cp_parser_function_specifier_opt (cp_parser* parser,
12171 cp_decl_specifier_seq *decl_specs)
12173 cp_token *token = cp_lexer_peek_token (parser->lexer);
12174 switch (token->keyword)
12176 case RID_INLINE:
12177 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12178 break;
12180 case RID_VIRTUAL:
12181 /* 14.5.2.3 [temp.mem]
12183 A member function template shall not be virtual. */
12184 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12185 error_at (token->location, "templates may not be %<virtual%>");
12186 else
12187 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12188 break;
12190 case RID_EXPLICIT:
12191 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12192 break;
12194 default:
12195 return NULL_TREE;
12198 /* Consume the token. */
12199 return cp_lexer_consume_token (parser->lexer)->u.value;
12202 /* Parse a linkage-specification.
12204 linkage-specification:
12205 extern string-literal { declaration-seq [opt] }
12206 extern string-literal declaration */
12208 static void
12209 cp_parser_linkage_specification (cp_parser* parser)
12211 tree linkage;
12213 /* Look for the `extern' keyword. */
12214 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12216 /* Look for the string-literal. */
12217 linkage = cp_parser_string_literal (parser, false, false);
12219 /* Transform the literal into an identifier. If the literal is a
12220 wide-character string, or contains embedded NULs, then we can't
12221 handle it as the user wants. */
12222 if (strlen (TREE_STRING_POINTER (linkage))
12223 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12225 cp_parser_error (parser, "invalid linkage-specification");
12226 /* Assume C++ linkage. */
12227 linkage = lang_name_cplusplus;
12229 else
12230 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12232 /* We're now using the new linkage. */
12233 push_lang_context (linkage);
12235 /* If the next token is a `{', then we're using the first
12236 production. */
12237 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12239 cp_ensure_no_omp_declare_simd (parser);
12241 /* Consume the `{' token. */
12242 cp_lexer_consume_token (parser->lexer);
12243 /* Parse the declarations. */
12244 cp_parser_declaration_seq_opt (parser);
12245 /* Look for the closing `}'. */
12246 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12248 /* Otherwise, there's just one declaration. */
12249 else
12251 bool saved_in_unbraced_linkage_specification_p;
12253 saved_in_unbraced_linkage_specification_p
12254 = parser->in_unbraced_linkage_specification_p;
12255 parser->in_unbraced_linkage_specification_p = true;
12256 cp_parser_declaration (parser);
12257 parser->in_unbraced_linkage_specification_p
12258 = saved_in_unbraced_linkage_specification_p;
12261 /* We're done with the linkage-specification. */
12262 pop_lang_context ();
12265 /* Parse a static_assert-declaration.
12267 static_assert-declaration:
12268 static_assert ( constant-expression , string-literal ) ;
12270 If MEMBER_P, this static_assert is a class member. */
12272 static void
12273 cp_parser_static_assert(cp_parser *parser, bool member_p)
12275 tree condition;
12276 tree message;
12277 cp_token *token;
12278 location_t saved_loc;
12279 bool dummy;
12281 /* Peek at the `static_assert' token so we can keep track of exactly
12282 where the static assertion started. */
12283 token = cp_lexer_peek_token (parser->lexer);
12284 saved_loc = token->location;
12286 /* Look for the `static_assert' keyword. */
12287 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12288 RT_STATIC_ASSERT))
12289 return;
12291 /* We know we are in a static assertion; commit to any tentative
12292 parse. */
12293 if (cp_parser_parsing_tentatively (parser))
12294 cp_parser_commit_to_tentative_parse (parser);
12296 /* Parse the `(' starting the static assertion condition. */
12297 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12299 /* Parse the constant-expression. Allow a non-constant expression
12300 here in order to give better diagnostics in finish_static_assert. */
12301 condition =
12302 cp_parser_constant_expression (parser,
12303 /*allow_non_constant_p=*/true,
12304 /*non_constant_p=*/&dummy);
12306 /* Parse the separating `,'. */
12307 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12309 /* Parse the string-literal message. */
12310 message = cp_parser_string_literal (parser,
12311 /*translate=*/false,
12312 /*wide_ok=*/true);
12314 /* A `)' completes the static assertion. */
12315 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12316 cp_parser_skip_to_closing_parenthesis (parser,
12317 /*recovering=*/true,
12318 /*or_comma=*/false,
12319 /*consume_paren=*/true);
12321 /* A semicolon terminates the declaration. */
12322 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12324 /* Complete the static assertion, which may mean either processing
12325 the static assert now or saving it for template instantiation. */
12326 finish_static_assert (condition, message, saved_loc, member_p);
12329 /* Parse the expression in decltype ( expression ). */
12331 static tree
12332 cp_parser_decltype_expr (cp_parser *parser,
12333 bool &id_expression_or_member_access_p)
12335 cp_token *id_expr_start_token;
12336 tree expr;
12338 /* First, try parsing an id-expression. */
12339 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12340 cp_parser_parse_tentatively (parser);
12341 expr = cp_parser_id_expression (parser,
12342 /*template_keyword_p=*/false,
12343 /*check_dependency_p=*/true,
12344 /*template_p=*/NULL,
12345 /*declarator_p=*/false,
12346 /*optional_p=*/false);
12348 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12350 bool non_integral_constant_expression_p = false;
12351 tree id_expression = expr;
12352 cp_id_kind idk;
12353 const char *error_msg;
12355 if (identifier_p (expr))
12356 /* Lookup the name we got back from the id-expression. */
12357 expr = cp_parser_lookup_name_simple (parser, expr,
12358 id_expr_start_token->location);
12360 if (expr
12361 && expr != error_mark_node
12362 && TREE_CODE (expr) != TYPE_DECL
12363 && (TREE_CODE (expr) != BIT_NOT_EXPR
12364 || !TYPE_P (TREE_OPERAND (expr, 0)))
12365 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12367 /* Complete lookup of the id-expression. */
12368 expr = (finish_id_expression
12369 (id_expression, expr, parser->scope, &idk,
12370 /*integral_constant_expression_p=*/false,
12371 /*allow_non_integral_constant_expression_p=*/true,
12372 &non_integral_constant_expression_p,
12373 /*template_p=*/false,
12374 /*done=*/true,
12375 /*address_p=*/false,
12376 /*template_arg_p=*/false,
12377 &error_msg,
12378 id_expr_start_token->location));
12380 if (expr == error_mark_node)
12381 /* We found an id-expression, but it was something that we
12382 should not have found. This is an error, not something
12383 we can recover from, so note that we found an
12384 id-expression and we'll recover as gracefully as
12385 possible. */
12386 id_expression_or_member_access_p = true;
12389 if (expr
12390 && expr != error_mark_node
12391 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12392 /* We have an id-expression. */
12393 id_expression_or_member_access_p = true;
12396 if (!id_expression_or_member_access_p)
12398 /* Abort the id-expression parse. */
12399 cp_parser_abort_tentative_parse (parser);
12401 /* Parsing tentatively, again. */
12402 cp_parser_parse_tentatively (parser);
12404 /* Parse a class member access. */
12405 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12406 /*cast_p=*/false, /*decltype*/true,
12407 /*member_access_only_p=*/true, NULL);
12409 if (expr
12410 && expr != error_mark_node
12411 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12412 /* We have an id-expression. */
12413 id_expression_or_member_access_p = true;
12416 if (id_expression_or_member_access_p)
12417 /* We have parsed the complete id-expression or member access. */
12418 cp_parser_parse_definitely (parser);
12419 else
12421 /* Abort our attempt to parse an id-expression or member access
12422 expression. */
12423 cp_parser_abort_tentative_parse (parser);
12425 /* Parse a full expression. */
12426 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12427 /*decltype_p=*/true);
12430 return expr;
12433 /* Parse a `decltype' type. Returns the type.
12435 simple-type-specifier:
12436 decltype ( expression )
12437 C++14 proposal:
12438 decltype ( auto ) */
12440 static tree
12441 cp_parser_decltype (cp_parser *parser)
12443 tree expr;
12444 bool id_expression_or_member_access_p = false;
12445 const char *saved_message;
12446 bool saved_integral_constant_expression_p;
12447 bool saved_non_integral_constant_expression_p;
12448 bool saved_greater_than_is_operator_p;
12449 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12451 if (start_token->type == CPP_DECLTYPE)
12453 /* Already parsed. */
12454 cp_lexer_consume_token (parser->lexer);
12455 return start_token->u.value;
12458 /* Look for the `decltype' token. */
12459 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12460 return error_mark_node;
12462 /* Parse the opening `('. */
12463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12464 return error_mark_node;
12466 /* decltype (auto) */
12467 if (cxx_dialect >= cxx14
12468 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12470 cp_lexer_consume_token (parser->lexer);
12471 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12472 return error_mark_node;
12473 expr = make_decltype_auto ();
12474 AUTO_IS_DECLTYPE (expr) = true;
12475 goto rewrite;
12478 /* Types cannot be defined in a `decltype' expression. Save away the
12479 old message. */
12480 saved_message = parser->type_definition_forbidden_message;
12482 /* And create the new one. */
12483 parser->type_definition_forbidden_message
12484 = G_("types may not be defined in %<decltype%> expressions");
12486 /* The restrictions on constant-expressions do not apply inside
12487 decltype expressions. */
12488 saved_integral_constant_expression_p
12489 = parser->integral_constant_expression_p;
12490 saved_non_integral_constant_expression_p
12491 = parser->non_integral_constant_expression_p;
12492 parser->integral_constant_expression_p = false;
12494 /* Within a parenthesized expression, a `>' token is always
12495 the greater-than operator. */
12496 saved_greater_than_is_operator_p
12497 = parser->greater_than_is_operator_p;
12498 parser->greater_than_is_operator_p = true;
12500 /* Do not actually evaluate the expression. */
12501 ++cp_unevaluated_operand;
12503 /* Do not warn about problems with the expression. */
12504 ++c_inhibit_evaluation_warnings;
12506 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12508 /* Go back to evaluating expressions. */
12509 --cp_unevaluated_operand;
12510 --c_inhibit_evaluation_warnings;
12512 /* The `>' token might be the end of a template-id or
12513 template-parameter-list now. */
12514 parser->greater_than_is_operator_p
12515 = saved_greater_than_is_operator_p;
12517 /* Restore the old message and the integral constant expression
12518 flags. */
12519 parser->type_definition_forbidden_message = saved_message;
12520 parser->integral_constant_expression_p
12521 = saved_integral_constant_expression_p;
12522 parser->non_integral_constant_expression_p
12523 = saved_non_integral_constant_expression_p;
12525 /* Parse to the closing `)'. */
12526 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12528 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12529 /*consume_paren=*/true);
12530 return error_mark_node;
12533 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12534 tf_warning_or_error);
12536 rewrite:
12537 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12538 it again. */
12539 start_token->type = CPP_DECLTYPE;
12540 start_token->u.value = expr;
12541 start_token->keyword = RID_MAX;
12542 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12544 return expr;
12547 /* Special member functions [gram.special] */
12549 /* Parse a conversion-function-id.
12551 conversion-function-id:
12552 operator conversion-type-id
12554 Returns an IDENTIFIER_NODE representing the operator. */
12556 static tree
12557 cp_parser_conversion_function_id (cp_parser* parser)
12559 tree type;
12560 tree saved_scope;
12561 tree saved_qualifying_scope;
12562 tree saved_object_scope;
12563 tree pushed_scope = NULL_TREE;
12565 /* Look for the `operator' token. */
12566 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12567 return error_mark_node;
12568 /* When we parse the conversion-type-id, the current scope will be
12569 reset. However, we need that information in able to look up the
12570 conversion function later, so we save it here. */
12571 saved_scope = parser->scope;
12572 saved_qualifying_scope = parser->qualifying_scope;
12573 saved_object_scope = parser->object_scope;
12574 /* We must enter the scope of the class so that the names of
12575 entities declared within the class are available in the
12576 conversion-type-id. For example, consider:
12578 struct S {
12579 typedef int I;
12580 operator I();
12583 S::operator I() { ... }
12585 In order to see that `I' is a type-name in the definition, we
12586 must be in the scope of `S'. */
12587 if (saved_scope)
12588 pushed_scope = push_scope (saved_scope);
12589 /* Parse the conversion-type-id. */
12590 type = cp_parser_conversion_type_id (parser);
12591 /* Leave the scope of the class, if any. */
12592 if (pushed_scope)
12593 pop_scope (pushed_scope);
12594 /* Restore the saved scope. */
12595 parser->scope = saved_scope;
12596 parser->qualifying_scope = saved_qualifying_scope;
12597 parser->object_scope = saved_object_scope;
12598 /* If the TYPE is invalid, indicate failure. */
12599 if (type == error_mark_node)
12600 return error_mark_node;
12601 return mangle_conv_op_name_for_type (type);
12604 /* Parse a conversion-type-id:
12606 conversion-type-id:
12607 type-specifier-seq conversion-declarator [opt]
12609 Returns the TYPE specified. */
12611 static tree
12612 cp_parser_conversion_type_id (cp_parser* parser)
12614 tree attributes;
12615 cp_decl_specifier_seq type_specifiers;
12616 cp_declarator *declarator;
12617 tree type_specified;
12618 const char *saved_message;
12620 /* Parse the attributes. */
12621 attributes = cp_parser_attributes_opt (parser);
12623 saved_message = parser->type_definition_forbidden_message;
12624 parser->type_definition_forbidden_message
12625 = G_("types may not be defined in a conversion-type-id");
12627 /* Parse the type-specifiers. */
12628 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12629 /*is_trailing_return=*/false,
12630 &type_specifiers);
12632 parser->type_definition_forbidden_message = saved_message;
12634 /* If that didn't work, stop. */
12635 if (type_specifiers.type == error_mark_node)
12636 return error_mark_node;
12637 /* Parse the conversion-declarator. */
12638 declarator = cp_parser_conversion_declarator_opt (parser);
12640 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12641 /*initialized=*/0, &attributes);
12642 if (attributes)
12643 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12645 /* Don't give this error when parsing tentatively. This happens to
12646 work because we always parse this definitively once. */
12647 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12648 && type_uses_auto (type_specified))
12650 if (cxx_dialect < cxx14)
12652 error ("invalid use of %<auto%> in conversion operator");
12653 return error_mark_node;
12655 else if (template_parm_scope_p ())
12656 warning (0, "use of %<auto%> in member template "
12657 "conversion operator can never be deduced");
12660 return type_specified;
12663 /* Parse an (optional) conversion-declarator.
12665 conversion-declarator:
12666 ptr-operator conversion-declarator [opt]
12670 static cp_declarator *
12671 cp_parser_conversion_declarator_opt (cp_parser* parser)
12673 enum tree_code code;
12674 tree class_type, std_attributes = NULL_TREE;
12675 cp_cv_quals cv_quals;
12677 /* We don't know if there's a ptr-operator next, or not. */
12678 cp_parser_parse_tentatively (parser);
12679 /* Try the ptr-operator. */
12680 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12681 &std_attributes);
12682 /* If it worked, look for more conversion-declarators. */
12683 if (cp_parser_parse_definitely (parser))
12685 cp_declarator *declarator;
12687 /* Parse another optional declarator. */
12688 declarator = cp_parser_conversion_declarator_opt (parser);
12690 declarator = cp_parser_make_indirect_declarator
12691 (code, class_type, cv_quals, declarator, std_attributes);
12693 return declarator;
12696 return NULL;
12699 /* Parse an (optional) ctor-initializer.
12701 ctor-initializer:
12702 : mem-initializer-list
12704 Returns TRUE iff the ctor-initializer was actually present. */
12706 static bool
12707 cp_parser_ctor_initializer_opt (cp_parser* parser)
12709 /* If the next token is not a `:', then there is no
12710 ctor-initializer. */
12711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12713 /* Do default initialization of any bases and members. */
12714 if (DECL_CONSTRUCTOR_P (current_function_decl))
12715 finish_mem_initializers (NULL_TREE);
12717 return false;
12720 /* Consume the `:' token. */
12721 cp_lexer_consume_token (parser->lexer);
12722 /* And the mem-initializer-list. */
12723 cp_parser_mem_initializer_list (parser);
12725 return true;
12728 /* Parse a mem-initializer-list.
12730 mem-initializer-list:
12731 mem-initializer ... [opt]
12732 mem-initializer ... [opt] , mem-initializer-list */
12734 static void
12735 cp_parser_mem_initializer_list (cp_parser* parser)
12737 tree mem_initializer_list = NULL_TREE;
12738 tree target_ctor = error_mark_node;
12739 cp_token *token = cp_lexer_peek_token (parser->lexer);
12741 /* Let the semantic analysis code know that we are starting the
12742 mem-initializer-list. */
12743 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12744 error_at (token->location,
12745 "only constructors take member initializers");
12747 /* Loop through the list. */
12748 while (true)
12750 tree mem_initializer;
12752 token = cp_lexer_peek_token (parser->lexer);
12753 /* Parse the mem-initializer. */
12754 mem_initializer = cp_parser_mem_initializer (parser);
12755 /* If the next token is a `...', we're expanding member initializers. */
12756 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12758 /* Consume the `...'. */
12759 cp_lexer_consume_token (parser->lexer);
12761 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12762 can be expanded but members cannot. */
12763 if (mem_initializer != error_mark_node
12764 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12766 error_at (token->location,
12767 "cannot expand initializer for member %<%D%>",
12768 TREE_PURPOSE (mem_initializer));
12769 mem_initializer = error_mark_node;
12772 /* Construct the pack expansion type. */
12773 if (mem_initializer != error_mark_node)
12774 mem_initializer = make_pack_expansion (mem_initializer);
12776 if (target_ctor != error_mark_node
12777 && mem_initializer != error_mark_node)
12779 error ("mem-initializer for %qD follows constructor delegation",
12780 TREE_PURPOSE (mem_initializer));
12781 mem_initializer = error_mark_node;
12783 /* Look for a target constructor. */
12784 if (mem_initializer != error_mark_node
12785 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12786 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12788 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12789 if (mem_initializer_list)
12791 error ("constructor delegation follows mem-initializer for %qD",
12792 TREE_PURPOSE (mem_initializer_list));
12793 mem_initializer = error_mark_node;
12795 target_ctor = mem_initializer;
12797 /* Add it to the list, unless it was erroneous. */
12798 if (mem_initializer != error_mark_node)
12800 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12801 mem_initializer_list = mem_initializer;
12803 /* If the next token is not a `,', we're done. */
12804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12805 break;
12806 /* Consume the `,' token. */
12807 cp_lexer_consume_token (parser->lexer);
12810 /* Perform semantic analysis. */
12811 if (DECL_CONSTRUCTOR_P (current_function_decl))
12812 finish_mem_initializers (mem_initializer_list);
12815 /* Parse a mem-initializer.
12817 mem-initializer:
12818 mem-initializer-id ( expression-list [opt] )
12819 mem-initializer-id braced-init-list
12821 GNU extension:
12823 mem-initializer:
12824 ( expression-list [opt] )
12826 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12827 class) or FIELD_DECL (for a non-static data member) to initialize;
12828 the TREE_VALUE is the expression-list. An empty initialization
12829 list is represented by void_list_node. */
12831 static tree
12832 cp_parser_mem_initializer (cp_parser* parser)
12834 tree mem_initializer_id;
12835 tree expression_list;
12836 tree member;
12837 cp_token *token = cp_lexer_peek_token (parser->lexer);
12839 /* Find out what is being initialized. */
12840 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12842 permerror (token->location,
12843 "anachronistic old-style base class initializer");
12844 mem_initializer_id = NULL_TREE;
12846 else
12848 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12849 if (mem_initializer_id == error_mark_node)
12850 return mem_initializer_id;
12852 member = expand_member_init (mem_initializer_id);
12853 if (member && !DECL_P (member))
12854 in_base_initializer = 1;
12856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12858 bool expr_non_constant_p;
12859 cp_lexer_set_source_position (parser->lexer);
12860 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12861 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12862 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12863 expression_list = build_tree_list (NULL_TREE, expression_list);
12865 else
12867 vec<tree, va_gc> *vec;
12868 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12869 /*cast_p=*/false,
12870 /*allow_expansion_p=*/true,
12871 /*non_constant_p=*/NULL);
12872 if (vec == NULL)
12873 return error_mark_node;
12874 expression_list = build_tree_list_vec (vec);
12875 release_tree_vector (vec);
12878 if (expression_list == error_mark_node)
12879 return error_mark_node;
12880 if (!expression_list)
12881 expression_list = void_type_node;
12883 in_base_initializer = 0;
12885 return member ? build_tree_list (member, expression_list) : error_mark_node;
12888 /* Parse a mem-initializer-id.
12890 mem-initializer-id:
12891 :: [opt] nested-name-specifier [opt] class-name
12892 identifier
12894 Returns a TYPE indicating the class to be initializer for the first
12895 production. Returns an IDENTIFIER_NODE indicating the data member
12896 to be initialized for the second production. */
12898 static tree
12899 cp_parser_mem_initializer_id (cp_parser* parser)
12901 bool global_scope_p;
12902 bool nested_name_specifier_p;
12903 bool template_p = false;
12904 tree id;
12906 cp_token *token = cp_lexer_peek_token (parser->lexer);
12908 /* `typename' is not allowed in this context ([temp.res]). */
12909 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12911 error_at (token->location,
12912 "keyword %<typename%> not allowed in this context (a qualified "
12913 "member initializer is implicitly a type)");
12914 cp_lexer_consume_token (parser->lexer);
12916 /* Look for the optional `::' operator. */
12917 global_scope_p
12918 = (cp_parser_global_scope_opt (parser,
12919 /*current_scope_valid_p=*/false)
12920 != NULL_TREE);
12921 /* Look for the optional nested-name-specifier. The simplest way to
12922 implement:
12924 [temp.res]
12926 The keyword `typename' is not permitted in a base-specifier or
12927 mem-initializer; in these contexts a qualified name that
12928 depends on a template-parameter is implicitly assumed to be a
12929 type name.
12931 is to assume that we have seen the `typename' keyword at this
12932 point. */
12933 nested_name_specifier_p
12934 = (cp_parser_nested_name_specifier_opt (parser,
12935 /*typename_keyword_p=*/true,
12936 /*check_dependency_p=*/true,
12937 /*type_p=*/true,
12938 /*is_declaration=*/true)
12939 != NULL_TREE);
12940 if (nested_name_specifier_p)
12941 template_p = cp_parser_optional_template_keyword (parser);
12942 /* If there is a `::' operator or a nested-name-specifier, then we
12943 are definitely looking for a class-name. */
12944 if (global_scope_p || nested_name_specifier_p)
12945 return cp_parser_class_name (parser,
12946 /*typename_keyword_p=*/true,
12947 /*template_keyword_p=*/template_p,
12948 typename_type,
12949 /*check_dependency_p=*/true,
12950 /*class_head_p=*/false,
12951 /*is_declaration=*/true);
12952 /* Otherwise, we could also be looking for an ordinary identifier. */
12953 cp_parser_parse_tentatively (parser);
12954 /* Try a class-name. */
12955 id = cp_parser_class_name (parser,
12956 /*typename_keyword_p=*/true,
12957 /*template_keyword_p=*/false,
12958 none_type,
12959 /*check_dependency_p=*/true,
12960 /*class_head_p=*/false,
12961 /*is_declaration=*/true);
12962 /* If we found one, we're done. */
12963 if (cp_parser_parse_definitely (parser))
12964 return id;
12965 /* Otherwise, look for an ordinary identifier. */
12966 return cp_parser_identifier (parser);
12969 /* Overloading [gram.over] */
12971 /* Parse an operator-function-id.
12973 operator-function-id:
12974 operator operator
12976 Returns an IDENTIFIER_NODE for the operator which is a
12977 human-readable spelling of the identifier, e.g., `operator +'. */
12979 static tree
12980 cp_parser_operator_function_id (cp_parser* parser)
12982 /* Look for the `operator' keyword. */
12983 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12984 return error_mark_node;
12985 /* And then the name of the operator itself. */
12986 return cp_parser_operator (parser);
12989 /* Return an identifier node for a user-defined literal operator.
12990 The suffix identifier is chained to the operator name identifier. */
12992 static tree
12993 cp_literal_operator_id (const char* name)
12995 tree identifier;
12996 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12997 + strlen (name) + 10);
12998 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12999 identifier = get_identifier (buffer);
13001 return identifier;
13004 /* Parse an operator.
13006 operator:
13007 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13008 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13009 || ++ -- , ->* -> () []
13011 GNU Extensions:
13013 operator:
13014 <? >? <?= >?=
13016 Returns an IDENTIFIER_NODE for the operator which is a
13017 human-readable spelling of the identifier, e.g., `operator +'. */
13019 static tree
13020 cp_parser_operator (cp_parser* parser)
13022 tree id = NULL_TREE;
13023 cp_token *token;
13024 bool utf8 = false;
13026 /* Peek at the next token. */
13027 token = cp_lexer_peek_token (parser->lexer);
13028 /* Figure out which operator we have. */
13029 switch (token->type)
13031 case CPP_KEYWORD:
13033 enum tree_code op;
13035 /* The keyword should be either `new' or `delete'. */
13036 if (token->keyword == RID_NEW)
13037 op = NEW_EXPR;
13038 else if (token->keyword == RID_DELETE)
13039 op = DELETE_EXPR;
13040 else
13041 break;
13043 /* Consume the `new' or `delete' token. */
13044 cp_lexer_consume_token (parser->lexer);
13046 /* Peek at the next token. */
13047 token = cp_lexer_peek_token (parser->lexer);
13048 /* If it's a `[' token then this is the array variant of the
13049 operator. */
13050 if (token->type == CPP_OPEN_SQUARE)
13052 /* Consume the `[' token. */
13053 cp_lexer_consume_token (parser->lexer);
13054 /* Look for the `]' token. */
13055 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13056 id = ansi_opname (op == NEW_EXPR
13057 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13059 /* Otherwise, we have the non-array variant. */
13060 else
13061 id = ansi_opname (op);
13063 return id;
13066 case CPP_PLUS:
13067 id = ansi_opname (PLUS_EXPR);
13068 break;
13070 case CPP_MINUS:
13071 id = ansi_opname (MINUS_EXPR);
13072 break;
13074 case CPP_MULT:
13075 id = ansi_opname (MULT_EXPR);
13076 break;
13078 case CPP_DIV:
13079 id = ansi_opname (TRUNC_DIV_EXPR);
13080 break;
13082 case CPP_MOD:
13083 id = ansi_opname (TRUNC_MOD_EXPR);
13084 break;
13086 case CPP_XOR:
13087 id = ansi_opname (BIT_XOR_EXPR);
13088 break;
13090 case CPP_AND:
13091 id = ansi_opname (BIT_AND_EXPR);
13092 break;
13094 case CPP_OR:
13095 id = ansi_opname (BIT_IOR_EXPR);
13096 break;
13098 case CPP_COMPL:
13099 id = ansi_opname (BIT_NOT_EXPR);
13100 break;
13102 case CPP_NOT:
13103 id = ansi_opname (TRUTH_NOT_EXPR);
13104 break;
13106 case CPP_EQ:
13107 id = ansi_assopname (NOP_EXPR);
13108 break;
13110 case CPP_LESS:
13111 id = ansi_opname (LT_EXPR);
13112 break;
13114 case CPP_GREATER:
13115 id = ansi_opname (GT_EXPR);
13116 break;
13118 case CPP_PLUS_EQ:
13119 id = ansi_assopname (PLUS_EXPR);
13120 break;
13122 case CPP_MINUS_EQ:
13123 id = ansi_assopname (MINUS_EXPR);
13124 break;
13126 case CPP_MULT_EQ:
13127 id = ansi_assopname (MULT_EXPR);
13128 break;
13130 case CPP_DIV_EQ:
13131 id = ansi_assopname (TRUNC_DIV_EXPR);
13132 break;
13134 case CPP_MOD_EQ:
13135 id = ansi_assopname (TRUNC_MOD_EXPR);
13136 break;
13138 case CPP_XOR_EQ:
13139 id = ansi_assopname (BIT_XOR_EXPR);
13140 break;
13142 case CPP_AND_EQ:
13143 id = ansi_assopname (BIT_AND_EXPR);
13144 break;
13146 case CPP_OR_EQ:
13147 id = ansi_assopname (BIT_IOR_EXPR);
13148 break;
13150 case CPP_LSHIFT:
13151 id = ansi_opname (LSHIFT_EXPR);
13152 break;
13154 case CPP_RSHIFT:
13155 id = ansi_opname (RSHIFT_EXPR);
13156 break;
13158 case CPP_LSHIFT_EQ:
13159 id = ansi_assopname (LSHIFT_EXPR);
13160 break;
13162 case CPP_RSHIFT_EQ:
13163 id = ansi_assopname (RSHIFT_EXPR);
13164 break;
13166 case CPP_EQ_EQ:
13167 id = ansi_opname (EQ_EXPR);
13168 break;
13170 case CPP_NOT_EQ:
13171 id = ansi_opname (NE_EXPR);
13172 break;
13174 case CPP_LESS_EQ:
13175 id = ansi_opname (LE_EXPR);
13176 break;
13178 case CPP_GREATER_EQ:
13179 id = ansi_opname (GE_EXPR);
13180 break;
13182 case CPP_AND_AND:
13183 id = ansi_opname (TRUTH_ANDIF_EXPR);
13184 break;
13186 case CPP_OR_OR:
13187 id = ansi_opname (TRUTH_ORIF_EXPR);
13188 break;
13190 case CPP_PLUS_PLUS:
13191 id = ansi_opname (POSTINCREMENT_EXPR);
13192 break;
13194 case CPP_MINUS_MINUS:
13195 id = ansi_opname (PREDECREMENT_EXPR);
13196 break;
13198 case CPP_COMMA:
13199 id = ansi_opname (COMPOUND_EXPR);
13200 break;
13202 case CPP_DEREF_STAR:
13203 id = ansi_opname (MEMBER_REF);
13204 break;
13206 case CPP_DEREF:
13207 id = ansi_opname (COMPONENT_REF);
13208 break;
13210 case CPP_OPEN_PAREN:
13211 /* Consume the `('. */
13212 cp_lexer_consume_token (parser->lexer);
13213 /* Look for the matching `)'. */
13214 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13215 return ansi_opname (CALL_EXPR);
13217 case CPP_OPEN_SQUARE:
13218 /* Consume the `['. */
13219 cp_lexer_consume_token (parser->lexer);
13220 /* Look for the matching `]'. */
13221 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13222 return ansi_opname (ARRAY_REF);
13224 case CPP_UTF8STRING:
13225 case CPP_UTF8STRING_USERDEF:
13226 utf8 = true;
13227 case CPP_STRING:
13228 case CPP_WSTRING:
13229 case CPP_STRING16:
13230 case CPP_STRING32:
13231 case CPP_STRING_USERDEF:
13232 case CPP_WSTRING_USERDEF:
13233 case CPP_STRING16_USERDEF:
13234 case CPP_STRING32_USERDEF:
13236 tree str, string_tree;
13237 int sz, len;
13239 if (cxx_dialect == cxx98)
13240 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13242 /* Consume the string. */
13243 str = cp_parser_string_literal (parser, /*translate=*/true,
13244 /*wide_ok=*/true, /*lookup_udlit=*/false);
13245 if (str == error_mark_node)
13246 return error_mark_node;
13247 else if (TREE_CODE (str) == USERDEF_LITERAL)
13249 string_tree = USERDEF_LITERAL_VALUE (str);
13250 id = USERDEF_LITERAL_SUFFIX_ID (str);
13252 else
13254 string_tree = str;
13255 /* Look for the suffix identifier. */
13256 token = cp_lexer_peek_token (parser->lexer);
13257 if (token->type == CPP_NAME)
13258 id = cp_parser_identifier (parser);
13259 else if (token->type == CPP_KEYWORD)
13261 error ("unexpected keyword;"
13262 " remove space between quotes and suffix identifier");
13263 return error_mark_node;
13265 else
13267 error ("expected suffix identifier");
13268 return error_mark_node;
13271 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13272 (TREE_TYPE (TREE_TYPE (string_tree))));
13273 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13274 if (len != 0)
13276 error ("expected empty string after %<operator%> keyword");
13277 return error_mark_node;
13279 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13280 != char_type_node)
13282 error ("invalid encoding prefix in literal operator");
13283 return error_mark_node;
13285 if (id != error_mark_node)
13287 const char *name = IDENTIFIER_POINTER (id);
13288 id = cp_literal_operator_id (name);
13290 return id;
13293 default:
13294 /* Anything else is an error. */
13295 break;
13298 /* If we have selected an identifier, we need to consume the
13299 operator token. */
13300 if (id)
13301 cp_lexer_consume_token (parser->lexer);
13302 /* Otherwise, no valid operator name was present. */
13303 else
13305 cp_parser_error (parser, "expected operator");
13306 id = error_mark_node;
13309 return id;
13312 /* Parse a template-declaration.
13314 template-declaration:
13315 export [opt] template < template-parameter-list > declaration
13317 If MEMBER_P is TRUE, this template-declaration occurs within a
13318 class-specifier.
13320 The grammar rule given by the standard isn't correct. What
13321 is really meant is:
13323 template-declaration:
13324 export [opt] template-parameter-list-seq
13325 decl-specifier-seq [opt] init-declarator [opt] ;
13326 export [opt] template-parameter-list-seq
13327 function-definition
13329 template-parameter-list-seq:
13330 template-parameter-list-seq [opt]
13331 template < template-parameter-list >
13333 Concept Extensions:
13335 template-parameter-list-seq:
13336 template < template-parameter-list > template-requirement [opt]
13338 template-requirement:
13339 requires logical-or-expression
13342 static void
13343 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13345 /* Check for `export'. */
13346 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13348 /* Consume the `export' token. */
13349 cp_lexer_consume_token (parser->lexer);
13350 /* Warn that we do not support `export'. */
13351 warning (0, "keyword %<export%> not implemented, and will be ignored");
13354 cp_parser_template_declaration_after_export (parser, member_p);
13357 /* Parse a template-parameter-list.
13359 template-parameter-list:
13360 template-parameter
13361 template-parameter-list , template-parameter
13363 Returns a TREE_LIST. Each node represents a template parameter.
13364 The nodes are connected via their TREE_CHAINs. */
13366 static tree
13367 cp_parser_template_parameter_list (cp_parser* parser)
13369 tree parameter_list = NULL_TREE;
13371 begin_template_parm_list ();
13373 /* The loop below parses the template parms. We first need to know
13374 the total number of template parms to be able to compute proper
13375 canonical types of each dependent type. So after the loop, when
13376 we know the total number of template parms,
13377 end_template_parm_list computes the proper canonical types and
13378 fixes up the dependent types accordingly. */
13379 while (true)
13381 tree parameter;
13382 bool is_non_type;
13383 bool is_parameter_pack;
13384 location_t parm_loc;
13386 /* Parse the template-parameter. */
13387 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13388 parameter = cp_parser_template_parameter (parser,
13389 &is_non_type,
13390 &is_parameter_pack);
13391 /* Add it to the list. */
13392 if (parameter != error_mark_node)
13393 parameter_list = process_template_parm (parameter_list,
13394 parm_loc,
13395 parameter,
13396 is_non_type,
13397 is_parameter_pack);
13398 else
13400 tree err_parm = build_tree_list (parameter, parameter);
13401 parameter_list = chainon (parameter_list, err_parm);
13404 /* If the next token is not a `,', we're done. */
13405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13406 break;
13407 /* Otherwise, consume the `,' token. */
13408 cp_lexer_consume_token (parser->lexer);
13411 return end_template_parm_list (parameter_list);
13414 #if 0
13415 /* Parse a introduction-list.
13417 introduction-list:
13418 introduced-parameter
13419 introduction-list , introduced-parameter
13421 introduced-parameter:
13422 ...[opt] identifier
13424 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
13425 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
13426 WILDCARD_DECL will also have DECL_NAME set and token location in
13427 DECL_SOURCE_LOCATION. */
13429 static tree
13430 cp_parser_introduction_list (cp_parser *parser)
13432 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
13434 while (true)
13436 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
13437 if (is_pack)
13438 cp_lexer_consume_token (parser->lexer);
13440 // Build placeholder.
13441 tree parm = build_nt (WILDCARD_DECL);
13442 DECL_SOURCE_LOCATION (parm)
13443 = cp_lexer_peek_token (parser->lexer)->location;
13444 DECL_NAME (parm) = cp_parser_identifier (parser);
13445 WILDCARD_PACK_P (parm) = is_pack;
13446 vec_safe_push (introduction_vec, parm);
13448 // If the next token is not a `,', we're done.
13449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13450 break;
13451 // Otherwise, consume the `,' token.
13452 cp_lexer_consume_token (parser->lexer);
13455 // Convert the vec into a TREE_VEC.
13456 tree introduction_list = make_tree_vec (introduction_vec->length ());
13457 unsigned int n;
13458 tree parm;
13459 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
13461 TREE_VEC_ELT (introduction_list, n) = parm;
13464 release_tree_vector (introduction_vec);
13465 return introduction_list;
13467 #endif
13469 // Given a declarator, get the declarator-id part, or NULL_TREE if this
13470 // is an abstract declarator.
13471 static inline cp_declarator*
13472 get_id_declarator (cp_declarator *declarator)
13474 cp_declarator *d = declarator;
13475 while (d && d->kind != cdk_id)
13476 d = d->declarator;
13477 return d;
13480 // Get the unqualified-id from the DECLARATOR or NULL_TREE if this
13481 // is an abstract declarator.
13482 static inline tree
13483 get_unqualified_id (cp_declarator *declarator)
13485 declarator = get_id_declarator (declarator);
13486 if (declarator)
13487 return declarator->u.id.unqualified_name;
13488 else
13489 return NULL_TREE;
13492 // Returns true if PARM declares a constrained-parameter.
13493 static inline bool
13494 is_constrained_parameter (cp_parameter_declarator *parm)
13496 gcc_assert (parm);
13497 tree decl = parm->decl_specifiers.type;
13498 return (decl
13499 && TREE_CODE (decl) == TYPE_DECL
13500 && DECL_INITIAL (decl)
13501 && DECL_SIZE_UNIT (decl)
13502 && (TREE_CODE (DECL_SIZE_UNIT (decl)) == FUNCTION_DECL
13503 || TREE_CODE (DECL_SIZE_UNIT (decl)) == VAR_DECL));
13507 // Check that the type parameter is only a declarator-id, and that its
13508 // type is not cv-qualified.
13509 bool
13510 cp_parser_check_constrained_type_parm (cp_parser *parser,
13511 cp_parameter_declarator *parm)
13513 if (!parm->declarator)
13514 return true;
13516 if (parm->declarator->kind != cdk_id)
13518 cp_parser_error (parser, "invalid constrained type parameter");
13519 return false;
13522 // Don't allow cv-qualified type parameters.
13523 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
13524 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
13526 cp_parser_error (parser, "cv-qualified type parameter");
13527 return false;
13530 return true;
13533 // Finish parsing/processing a template type parameter and checking
13534 // various restrictions.
13535 static inline tree
13536 cp_parser_constrained_type_template_parm (cp_parser *parser,
13537 tree id,
13538 cp_parameter_declarator* parmdecl)
13540 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
13541 return finish_template_type_parm (class_type_node, id);
13542 else
13543 return error_mark_node;
13546 // Finish parsing/processing a template template parameter by borrowing
13547 // the template parameter list from the prototype parameter.
13548 static tree
13549 cp_parser_constrained_template_template_parm (cp_parser *parser,
13550 tree proto,
13551 tree id,
13552 cp_parameter_declarator *parmdecl)
13554 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
13555 return error_mark_node;
13557 // FIXME: This should probably be copied, and we may need to adjust
13558 // the template parameter depths.
13559 tree saved_parms = current_template_parms;
13560 begin_template_parm_list ();
13561 current_template_parms = DECL_TEMPLATE_PARMS (proto);
13562 end_template_parm_list ();
13564 tree parm = finish_template_template_parm (class_type_node, id);
13565 current_template_parms = saved_parms;
13567 return parm;
13570 // Create a new non-type template parameter from the given PARM declarator.
13571 static tree
13572 constrained_non_type_template_parm (bool *is_non_type,
13573 cp_parameter_declarator *parm)
13575 *is_non_type = true;
13576 cp_declarator *decl = parm->declarator;
13577 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
13578 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
13579 return grokdeclarator (decl, specs, TPARM, 0, NULL);
13582 // Build a constrained template parameter based on the PARMDECL
13583 // declarator. The type of PARMDECL is the constrained type, which
13584 // refers to the prototype template parameter that ultimately
13585 // specifies the type of the declared parameter.
13586 static tree
13587 finish_constrained_parameter (cp_parser *parser,
13588 cp_parameter_declarator *parmdecl,
13589 bool *is_non_type,
13590 bool *is_parameter_pack)
13592 tree decl = parmdecl->decl_specifiers.type;
13593 tree id = get_unqualified_id (parmdecl->declarator);
13594 tree def = parmdecl->default_argument;
13595 tree proto = DECL_INITIAL (decl);
13597 // A template parameter constrained by a variadic concept shall also
13598 // be declared as a template parameter pack.
13599 bool is_variadic = template_parameter_pack_p (proto);
13600 if (is_variadic && !*is_parameter_pack)
13601 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
13603 // Build the parameter. Return an error if the declarator was invalid.
13604 tree parm;
13605 if (TREE_CODE (proto) == TYPE_DECL)
13606 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
13607 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13608 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
13609 parmdecl);
13610 else
13611 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
13612 if (parm == error_mark_node)
13613 return error_mark_node;
13615 // Finish the parameter decl and create a node attaching the
13616 // default argument and constraint.
13617 parm = build_tree_list (def, parm);
13618 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13620 return parm;
13623 // Returns the type of the given TYPE may represent the declaration of
13624 // a template type parameter. This is a helper function for the
13625 // cp_declares_type* functions below.
13626 static inline bool
13627 maybe_type_parameter (tree type)
13629 return type
13630 && TREE_CODE (type) == TYPE_DECL
13631 && DECL_SIZE_UNIT (type)
13632 && TREE_TYPE (type);
13635 // Returns true if the parsed type actually represents a type or template
13636 // template parameter.
13637 static inline bool
13638 declares_type_parameter (tree type)
13640 if (maybe_type_parameter (type))
13642 tree_code c = TREE_CODE (TREE_TYPE (type));
13643 return c == TEMPLATE_TYPE_PARM || c == TEMPLATE_TEMPLATE_PARM;
13645 return false;
13648 // Returns true if the parsed type actually represents the declaration
13649 // of a type template-parameter.
13650 static inline bool
13651 declares_type_template_parameter (tree type)
13653 return maybe_type_parameter (type)
13654 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM;
13658 // Returns true if the parsed type actually represents the declaration of
13659 // a template template-parameter.
13660 static bool
13661 declares_template_template_parameter (tree type)
13663 return maybe_type_parameter (type)
13664 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM;
13667 // Parse a default argument for a type template-parameter.
13669 // Note that diagnostics are handled in cp_parser_template_parameter.
13670 static tree
13671 cp_parser_default_type_template_argument (cp_parser *parser)
13673 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13675 /* Consume the `=' token. */
13676 cp_lexer_consume_token (parser->lexer);
13678 /* Parse the default-argument. */
13679 push_deferring_access_checks (dk_no_deferred);
13680 tree default_argument = cp_parser_type_id (parser);
13681 pop_deferring_access_checks ();
13683 return default_argument;
13686 // Parse a default argument for a template template-parameter.
13687 static tree
13688 cp_parser_default_template_template_argument (cp_parser *parser)
13690 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13692 bool is_template;
13694 /* Consume the `='. */
13695 cp_lexer_consume_token (parser->lexer);
13696 /* Parse the id-expression. */
13697 push_deferring_access_checks (dk_no_deferred);
13698 /* save token before parsing the id-expression, for error
13699 reporting */
13700 const cp_token* token = cp_lexer_peek_token (parser->lexer);
13701 tree default_argument
13702 = cp_parser_id_expression (parser,
13703 /*template_keyword_p=*/false,
13704 /*check_dependency_p=*/true,
13705 /*template_p=*/&is_template,
13706 /*declarator_p=*/false,
13707 /*optional_p=*/false);
13708 if (TREE_CODE (default_argument) == TYPE_DECL)
13709 /* If the id-expression was a template-id that refers to
13710 a template-class, we already have the declaration here,
13711 so no further lookup is needed. */
13713 else
13714 /* Look up the name. */
13715 default_argument
13716 = cp_parser_lookup_name (parser, default_argument,
13717 none_type,
13718 /*is_template=*/is_template,
13719 /*is_namespace=*/false,
13720 /*check_dependency=*/true,
13721 /*ambiguous_decls=*/NULL,
13722 token->location);
13723 /* See if the default argument is valid. */
13724 default_argument = check_template_template_default_arg (default_argument);
13725 pop_deferring_access_checks ();
13726 return default_argument;
13729 /* Parse a template-parameter.
13731 template-parameter:
13732 type-parameter
13733 parameter-declaration
13735 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13736 the parameter. The TREE_PURPOSE is the default value, if any.
13737 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13738 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13739 set to true iff this parameter is a parameter pack. */
13741 static tree
13742 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13743 bool *is_parameter_pack)
13745 cp_token *token;
13746 cp_parameter_declarator *parameter_declarator;
13747 cp_declarator *id_declarator;
13748 tree parm;
13750 /* Assume it is a type parameter or a template parameter. */
13751 *is_non_type = false;
13752 /* Assume it not a parameter pack. */
13753 *is_parameter_pack = false;
13754 /* Peek at the next token. */
13755 token = cp_lexer_peek_token (parser->lexer);
13756 /* If it is `class' or `template', we have a type-parameter. */
13757 if (token->keyword == RID_TEMPLATE)
13758 return cp_parser_type_parameter (parser, is_parameter_pack);
13759 /* If it is `class' or `typename' we do not know yet whether it is a
13760 type parameter or a non-type parameter. Consider:
13762 template <typename T, typename T::X X> ...
13766 template <class C, class D*> ...
13768 Here, the first parameter is a type parameter, and the second is
13769 a non-type parameter. We can tell by looking at the token after
13770 the identifier -- if it is a `,', `=', or `>' then we have a type
13771 parameter. */
13772 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13774 /* Peek at the token after `class' or `typename'. */
13775 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13776 /* If it's an ellipsis, we have a template type parameter
13777 pack. */
13778 if (token->type == CPP_ELLIPSIS)
13779 return cp_parser_type_parameter (parser, is_parameter_pack);
13780 /* If it's an identifier, skip it. */
13781 if (token->type == CPP_NAME)
13782 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13783 /* Now, see if the token looks like the end of a template
13784 parameter. */
13785 if (token->type == CPP_COMMA
13786 || token->type == CPP_EQ
13787 || token->type == CPP_GREATER)
13788 return cp_parser_type_parameter (parser, is_parameter_pack);
13791 /* Otherwise, it is a non-type parameter or a constrained parameter.
13793 [temp.param]
13795 When parsing a default template-argument for a non-type
13796 template-parameter, the first non-nested `>' is taken as the end
13797 of the template parameter-list rather than a greater-than
13798 operator. */
13799 parameter_declarator
13800 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13801 /*parenthesized_p=*/NULL);
13803 if (!parameter_declarator)
13804 return error_mark_node;
13806 /* If the parameter declaration is marked as a parameter pack, set
13807 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13808 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13809 grokdeclarator. */
13810 if (parameter_declarator->declarator
13811 && parameter_declarator->declarator->parameter_pack_p)
13813 *is_parameter_pack = true;
13814 parameter_declarator->declarator->parameter_pack_p = false;
13817 tree declared_type = parameter_declarator->decl_specifiers.type;
13818 if (parameter_declarator->default_argument)
13820 /* Can happen in some cases of erroneous input (c++/34892). */
13821 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13822 /* Consume the `...' for better error recovery. */
13823 cp_lexer_consume_token (parser->lexer);
13825 /* If the next token is an ellipsis, and we don't already have it
13826 marked as a parameter pack, then we have a parameter pack (that
13827 has no declarator). */
13828 else if (!*is_parameter_pack
13829 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13830 && (declarator_can_be_parameter_pack
13831 (parameter_declarator->declarator)))
13833 /* Consume the `...'. */
13834 cp_lexer_consume_token (parser->lexer);
13835 maybe_warn_variadic_templates ();
13837 *is_parameter_pack = true;
13839 /* We might end up with a pack expansion as the type of the non-type
13840 template parameter, in which case this is a non-type template
13841 parameter pack. */
13842 else if (declared_type && PACK_EXPANSION_P (declared_type))
13844 *is_parameter_pack = true;
13845 parameter_declarator->decl_specifiers.type =
13846 PACK_EXPANSION_PATTERN (declared_type);
13849 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13851 /* Parameter packs cannot have default arguments. However, a
13852 user may try to do so, so we'll parse them and give an
13853 appropriate diagnostic here. */
13855 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13857 /* Find the name of the parameter pack. */
13858 id_declarator = parameter_declarator->declarator;
13859 while (id_declarator && id_declarator->kind != cdk_id)
13860 id_declarator = id_declarator->declarator;
13862 if (id_declarator && id_declarator->kind == cdk_id)
13863 error_at (start_token->location,
13864 "template parameter pack %qD cannot have a default argument",
13865 id_declarator->u.id.unqualified_name);
13866 else
13867 error_at (start_token->location,
13868 "template parameter pack cannot have a default argument");
13870 /* Parse the default argument, but throw away the result. */
13871 if (declares_type_template_parameter (declared_type))
13872 cp_parser_default_type_template_argument (parser);
13873 else if (declares_template_template_parameter (declared_type))
13874 cp_parser_default_template_template_argument (parser);
13875 else
13876 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13879 // The parameter may have been constrained.
13880 if (is_constrained_parameter (parameter_declarator))
13881 return finish_constrained_parameter (parser,
13882 parameter_declarator,
13883 is_non_type,
13884 is_parameter_pack);
13886 // Now we're sure that the parameter is a non-type parameter.
13887 *is_non_type = true;
13889 parm = grokdeclarator (parameter_declarator->declarator,
13890 &parameter_declarator->decl_specifiers,
13891 TPARM, /*initialized=*/0,
13892 /*attrlist=*/NULL);
13893 if (parm == error_mark_node)
13894 return error_mark_node;
13896 return build_tree_list (parameter_declarator->default_argument, parm);
13899 /* Parse a type-parameter.
13901 type-parameter:
13902 class identifier [opt]
13903 class identifier [opt] = type-id
13904 typename identifier [opt]
13905 typename identifier [opt] = type-id
13906 template < template-parameter-list > class identifier [opt]
13907 template < template-parameter-list > class identifier [opt]
13908 = id-expression
13910 GNU Extension (variadic templates):
13912 type-parameter:
13913 class ... identifier [opt]
13914 typename ... identifier [opt]
13916 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13917 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13918 the declaration of the parameter.
13920 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13922 static tree
13923 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13925 cp_token *token;
13926 tree parameter;
13928 /* Look for a keyword to tell us what kind of parameter this is. */
13929 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13930 if (!token)
13931 return error_mark_node;
13933 switch (token->keyword)
13935 case RID_CLASS:
13936 case RID_TYPENAME:
13938 tree identifier;
13939 tree default_argument;
13941 /* If the next token is an ellipsis, we have a template
13942 argument pack. */
13943 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13945 /* Consume the `...' token. */
13946 cp_lexer_consume_token (parser->lexer);
13947 maybe_warn_variadic_templates ();
13949 *is_parameter_pack = true;
13952 /* If the next token is an identifier, then it names the
13953 parameter. */
13954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13955 identifier = cp_parser_identifier (parser);
13956 else
13957 identifier = NULL_TREE;
13959 /* Create the parameter. */
13960 parameter = finish_template_type_parm (class_type_node, identifier);
13962 /* If the next token is an `=', we have a default argument. */
13963 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13965 default_argument
13966 = cp_parser_default_type_template_argument (parser);
13968 /* Template parameter packs cannot have default
13969 arguments. */
13970 if (*is_parameter_pack)
13972 if (identifier)
13973 error_at (token->location,
13974 "template parameter pack %qD cannot have a "
13975 "default argument", identifier);
13976 else
13977 error_at (token->location,
13978 "template parameter packs cannot have "
13979 "default arguments");
13980 default_argument = NULL_TREE;
13982 else if (check_for_bare_parameter_packs (default_argument))
13983 default_argument = error_mark_node;
13985 /* ANS: FIXME: This appears to be a stray pop. Check again once
13986 concept introdutions have been disabled. */
13987 // pop_deferring_access_checks ();
13989 else
13990 default_argument = NULL_TREE;
13992 /* Create the combined representation of the parameter and the
13993 default argument. */
13994 parameter = build_tree_list (default_argument, parameter);
13996 break;
13998 case RID_TEMPLATE:
14000 tree identifier;
14001 tree default_argument;
14003 /* Look for the `<'. */
14004 cp_parser_require (parser, CPP_LESS, RT_LESS);
14005 /* Parse the template-parameter-list. */
14006 cp_parser_template_parameter_list (parser);
14007 /* Look for the `>'. */
14008 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14010 // If template requirements are present, parse them.
14011 if (flag_concepts)
14013 tree reqs = get_shorthand_constraints (current_template_parms);
14014 if (tree r = cp_parser_requires_clause_opt (parser))
14015 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14016 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14019 /* Look for the `class' or 'typename' keywords. */
14020 cp_parser_type_parameter_key (parser);
14021 /* If the next token is an ellipsis, we have a template
14022 argument pack. */
14023 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14025 /* Consume the `...' token. */
14026 cp_lexer_consume_token (parser->lexer);
14027 maybe_warn_variadic_templates ();
14029 *is_parameter_pack = true;
14031 /* If the next token is an `=', then there is a
14032 default-argument. If the next token is a `>', we are at
14033 the end of the parameter-list. If the next token is a `,',
14034 then we are at the end of this parameter. */
14035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14036 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14037 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14039 identifier = cp_parser_identifier (parser);
14040 /* Treat invalid names as if the parameter were nameless. */
14041 if (identifier == error_mark_node)
14042 identifier = NULL_TREE;
14044 else
14045 identifier = NULL_TREE;
14047 /* Create the template parameter. */
14048 parameter = finish_template_template_parm (class_type_node,
14049 identifier);
14051 /* If the next token is an `=', then there is a
14052 default-argument. */
14053 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14055 default_argument
14056 = cp_parser_default_template_template_argument (parser);
14058 /* Template parameter packs cannot have default
14059 arguments. */
14060 if (*is_parameter_pack)
14062 if (identifier)
14063 error_at (token->location,
14064 "template parameter pack %qD cannot "
14065 "have a default argument",
14066 identifier);
14067 else
14068 error_at (token->location, "template parameter packs cannot "
14069 "have default arguments");
14070 default_argument = NULL_TREE;
14073 else
14074 default_argument = NULL_TREE;
14076 /* Create the combined representation of the parameter and the
14077 default argument. */
14078 parameter = build_tree_list (default_argument, parameter);
14080 break;
14082 default:
14083 gcc_unreachable ();
14084 break;
14087 return parameter;
14090 /* Parse a template-id.
14092 template-id:
14093 template-name < template-argument-list [opt] >
14095 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14096 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14097 returned. Otherwise, if the template-name names a function, or set
14098 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14099 names a class, returns a TYPE_DECL for the specialization.
14101 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14102 uninstantiated templates. */
14104 static tree
14105 cp_parser_template_id (cp_parser *parser,
14106 bool template_keyword_p,
14107 bool check_dependency_p,
14108 enum tag_types tag_type,
14109 bool is_declaration)
14111 int i;
14112 tree templ;
14113 tree arguments;
14114 tree template_id;
14115 cp_token_position start_of_id = 0;
14116 deferred_access_check *chk;
14117 vec<deferred_access_check, va_gc> *access_check;
14118 cp_token *next_token = NULL, *next_token_2 = NULL;
14119 bool is_identifier;
14121 /* If the next token corresponds to a template-id, there is no need
14122 to reparse it. */
14123 next_token = cp_lexer_peek_token (parser->lexer);
14124 if (next_token->type == CPP_TEMPLATE_ID)
14126 struct tree_check *check_value;
14128 /* Get the stored value. */
14129 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
14130 /* Perform any access checks that were deferred. */
14131 access_check = check_value->checks;
14132 if (access_check)
14134 FOR_EACH_VEC_ELT (*access_check, i, chk)
14135 perform_or_defer_access_check (chk->binfo,
14136 chk->decl,
14137 chk->diag_decl,
14138 tf_warning_or_error);
14140 /* Return the stored value. */
14141 return check_value->value;
14144 /* Avoid performing name lookup if there is no possibility of
14145 finding a template-id. */
14146 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14147 || (next_token->type == CPP_NAME
14148 && !cp_parser_nth_token_starts_template_argument_list_p
14149 (parser, 2)))
14151 cp_parser_error (parser, "expected template-id");
14152 return error_mark_node;
14155 /* Remember where the template-id starts. */
14156 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14157 start_of_id = cp_lexer_token_position (parser->lexer, false);
14159 push_deferring_access_checks (dk_deferred);
14161 /* Parse the template-name. */
14162 is_identifier = false;
14163 templ = cp_parser_template_name (parser, template_keyword_p,
14164 check_dependency_p,
14165 is_declaration,
14166 tag_type,
14167 &is_identifier);
14168 if (templ == error_mark_node || is_identifier)
14170 pop_deferring_access_checks ();
14171 return templ;
14174 /* If we find the sequence `[:' after a template-name, it's probably
14175 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14176 parse correctly the argument list. */
14177 next_token = cp_lexer_peek_token (parser->lexer);
14178 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14179 if (next_token->type == CPP_OPEN_SQUARE
14180 && next_token->flags & DIGRAPH
14181 && next_token_2->type == CPP_COLON
14182 && !(next_token_2->flags & PREV_WHITE))
14184 cp_parser_parse_tentatively (parser);
14185 /* Change `:' into `::'. */
14186 next_token_2->type = CPP_SCOPE;
14187 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14188 CPP_LESS. */
14189 cp_lexer_consume_token (parser->lexer);
14191 /* Parse the arguments. */
14192 arguments = cp_parser_enclosed_template_argument_list (parser);
14193 if (!cp_parser_parse_definitely (parser))
14195 /* If we couldn't parse an argument list, then we revert our changes
14196 and return simply an error. Maybe this is not a template-id
14197 after all. */
14198 next_token_2->type = CPP_COLON;
14199 cp_parser_error (parser, "expected %<<%>");
14200 pop_deferring_access_checks ();
14201 return error_mark_node;
14203 /* Otherwise, emit an error about the invalid digraph, but continue
14204 parsing because we got our argument list. */
14205 if (permerror (next_token->location,
14206 "%<<::%> cannot begin a template-argument list"))
14208 static bool hint = false;
14209 inform (next_token->location,
14210 "%<<:%> is an alternate spelling for %<[%>."
14211 " Insert whitespace between %<<%> and %<::%>");
14212 if (!hint && !flag_permissive)
14214 inform (next_token->location, "(if you use %<-fpermissive%> "
14215 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14216 "accept your code)");
14217 hint = true;
14221 else
14223 /* Look for the `<' that starts the template-argument-list. */
14224 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14226 pop_deferring_access_checks ();
14227 return error_mark_node;
14229 /* Parse the arguments. */
14230 arguments = cp_parser_enclosed_template_argument_list (parser);
14233 /* Build a representation of the specialization. */
14234 tree partial_concept_id;
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 /* A template-like identifier may be a partial concept id. */
14255 else if (flag_concepts
14256 && cp_maybe_partial_concept_id
14257 (parser, templ, arguments, &partial_concept_id))
14259 return partial_concept_id;
14261 else if (variable_template_p (templ))
14263 template_id = lookup_template_variable (templ, arguments);
14265 else
14267 /* If it's not a class-template or a template-template, it should be
14268 a function-template. */
14269 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14270 || TREE_CODE (templ) == OVERLOAD
14271 || BASELINK_P (templ)));
14272 template_id = lookup_template_function (templ, arguments);
14275 /* If parsing tentatively, replace the sequence of tokens that makes
14276 up the template-id with a CPP_TEMPLATE_ID token. That way,
14277 should we re-parse the token stream, we will not have to repeat
14278 the effort required to do the parse, nor will we issue duplicate
14279 error messages about problems during instantiation of the
14280 template. */
14281 if (start_of_id
14282 /* Don't do this if we had a parse error in a declarator; re-parsing
14283 might succeed if a name changes meaning (60361). */
14284 && !(cp_parser_error_occurred (parser)
14285 && cp_parser_parsing_tentatively (parser)
14286 && parser->in_declarator_p))
14288 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14290 /* Reset the contents of the START_OF_ID token. */
14291 token->type = CPP_TEMPLATE_ID;
14292 /* Retrieve any deferred checks. Do not pop this access checks yet
14293 so the memory will not be reclaimed during token replacing below. */
14294 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14295 token->u.tree_check_value->value = template_id;
14296 token->u.tree_check_value->checks = get_deferred_access_checks ();
14297 token->keyword = RID_MAX;
14299 /* Purge all subsequent tokens. */
14300 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14302 /* ??? Can we actually assume that, if template_id ==
14303 error_mark_node, we will have issued a diagnostic to the
14304 user, as opposed to simply marking the tentative parse as
14305 failed? */
14306 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14307 error_at (token->location, "parse error in template argument list");
14310 pop_to_parent_deferring_access_checks ();
14312 return template_id;
14315 /* Parse a template-name.
14317 template-name:
14318 identifier
14320 The standard should actually say:
14322 template-name:
14323 identifier
14324 operator-function-id
14326 A defect report has been filed about this issue.
14328 A conversion-function-id cannot be a template name because they cannot
14329 be part of a template-id. In fact, looking at this code:
14331 a.operator K<int>()
14333 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14334 It is impossible to call a templated conversion-function-id with an
14335 explicit argument list, since the only allowed template parameter is
14336 the type to which it is converting.
14338 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14339 `template' keyword, in a construction like:
14341 T::template f<3>()
14343 In that case `f' is taken to be a template-name, even though there
14344 is no way of knowing for sure.
14346 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
14347 name refers to a set of overloaded functions, at least one of which
14348 is a template, or an IDENTIFIER_NODE with the name of the template,
14349 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
14350 names are looked up inside uninstantiated templates. */
14352 static tree
14353 cp_parser_template_name (cp_parser* parser,
14354 bool template_keyword_p,
14355 bool check_dependency_p,
14356 bool is_declaration,
14357 enum tag_types tag_type,
14358 bool *is_identifier)
14360 tree identifier;
14361 tree decl;
14362 tree fns;
14363 cp_token *token = cp_lexer_peek_token (parser->lexer);
14365 /* If the next token is `operator', then we have either an
14366 operator-function-id or a conversion-function-id. */
14367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
14369 /* We don't know whether we're looking at an
14370 operator-function-id or a conversion-function-id. */
14371 cp_parser_parse_tentatively (parser);
14372 /* Try an operator-function-id. */
14373 identifier = cp_parser_operator_function_id (parser);
14374 /* If that didn't work, try a conversion-function-id. */
14375 if (!cp_parser_parse_definitely (parser))
14377 cp_parser_error (parser, "expected template-name");
14378 return error_mark_node;
14381 /* Look for the identifier. */
14382 else
14383 identifier = cp_parser_identifier (parser);
14385 /* If we didn't find an identifier, we don't have a template-id. */
14386 if (identifier == error_mark_node)
14387 return error_mark_node;
14389 /* If the name immediately followed the `template' keyword, then it
14390 is a template-name. However, if the next token is not `<', then
14391 we do not treat it as a template-name, since it is not being used
14392 as part of a template-id. This enables us to handle constructs
14393 like:
14395 template <typename T> struct S { S(); };
14396 template <typename T> S<T>::S();
14398 correctly. We would treat `S' as a template -- if it were `S<T>'
14399 -- but we do not if there is no `<'. */
14401 if (processing_template_decl
14402 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14404 /* In a declaration, in a dependent context, we pretend that the
14405 "template" keyword was present in order to improve error
14406 recovery. For example, given:
14408 template <typename T> void f(T::X<int>);
14410 we want to treat "X<int>" as a template-id. */
14411 if (is_declaration
14412 && !template_keyword_p
14413 && parser->scope && TYPE_P (parser->scope)
14414 && check_dependency_p
14415 && dependent_scope_p (parser->scope)
14416 /* Do not do this for dtors (or ctors), since they never
14417 need the template keyword before their name. */
14418 && !constructor_name_p (identifier, parser->scope))
14420 cp_token_position start = 0;
14422 /* Explain what went wrong. */
14423 error_at (token->location, "non-template %qD used as template",
14424 identifier);
14425 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14426 parser->scope, identifier);
14427 /* If parsing tentatively, find the location of the "<" token. */
14428 if (cp_parser_simulate_error (parser))
14429 start = cp_lexer_token_position (parser->lexer, true);
14430 /* Parse the template arguments so that we can issue error
14431 messages about them. */
14432 cp_lexer_consume_token (parser->lexer);
14433 cp_parser_enclosed_template_argument_list (parser);
14434 /* Skip tokens until we find a good place from which to
14435 continue parsing. */
14436 cp_parser_skip_to_closing_parenthesis (parser,
14437 /*recovering=*/true,
14438 /*or_comma=*/true,
14439 /*consume_paren=*/false);
14440 /* If parsing tentatively, permanently remove the
14441 template argument list. That will prevent duplicate
14442 error messages from being issued about the missing
14443 "template" keyword. */
14444 if (start)
14445 cp_lexer_purge_tokens_after (parser->lexer, start);
14446 if (is_identifier)
14447 *is_identifier = true;
14448 return identifier;
14451 /* If the "template" keyword is present, then there is generally
14452 no point in doing name-lookup, so we just return IDENTIFIER.
14453 But, if the qualifying scope is non-dependent then we can
14454 (and must) do name-lookup normally. */
14455 if (template_keyword_p
14456 && (!parser->scope
14457 || (TYPE_P (parser->scope)
14458 && dependent_type_p (parser->scope))))
14459 return identifier;
14462 /* Look up the name. */
14463 decl = cp_parser_lookup_name (parser, identifier,
14464 tag_type,
14465 /*is_template=*/true,
14466 /*is_namespace=*/false,
14467 check_dependency_p,
14468 /*ambiguous_decls=*/NULL,
14469 token->location);
14471 decl = strip_using_decl (decl);
14473 /* If DECL is a template, then the name was a template-name. */
14474 if (TREE_CODE (decl) == TEMPLATE_DECL)
14476 if (TREE_DEPRECATED (decl)
14477 && deprecated_state != DEPRECATED_SUPPRESS)
14478 warn_deprecated_use (decl, NULL_TREE);
14480 else
14482 tree fn = NULL_TREE;
14484 /* The standard does not explicitly indicate whether a name that
14485 names a set of overloaded declarations, some of which are
14486 templates, is a template-name. However, such a name should
14487 be a template-name; otherwise, there is no way to form a
14488 template-id for the overloaded templates. */
14489 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14490 if (TREE_CODE (fns) == OVERLOAD)
14491 for (fn = fns; fn; fn = OVL_NEXT (fn))
14492 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14493 break;
14495 if (!fn)
14497 /* The name does not name a template. */
14498 cp_parser_error (parser, "expected template-name");
14499 return error_mark_node;
14503 /* If DECL is dependent, and refers to a function, then just return
14504 its name; we will look it up again during template instantiation. */
14505 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14507 tree scope = ovl_scope (decl);
14508 if (TYPE_P (scope) && dependent_type_p (scope))
14509 return identifier;
14512 return decl;
14515 /* Parse a template-argument-list.
14517 template-argument-list:
14518 template-argument ... [opt]
14519 template-argument-list , template-argument ... [opt]
14521 Returns a TREE_VEC containing the arguments. */
14523 static tree
14524 cp_parser_template_argument_list (cp_parser* parser)
14526 tree fixed_args[10];
14527 unsigned n_args = 0;
14528 unsigned alloced = 10;
14529 tree *arg_ary = fixed_args;
14530 tree vec;
14531 bool saved_in_template_argument_list_p;
14532 bool saved_ice_p;
14533 bool saved_non_ice_p;
14535 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14536 parser->in_template_argument_list_p = true;
14537 /* Even if the template-id appears in an integral
14538 constant-expression, the contents of the argument list do
14539 not. */
14540 saved_ice_p = parser->integral_constant_expression_p;
14541 parser->integral_constant_expression_p = false;
14542 saved_non_ice_p = parser->non_integral_constant_expression_p;
14543 parser->non_integral_constant_expression_p = false;
14545 /* Parse the arguments. */
14548 tree argument;
14550 if (n_args)
14551 /* Consume the comma. */
14552 cp_lexer_consume_token (parser->lexer);
14554 /* Parse the template-argument. */
14555 argument = cp_parser_template_argument (parser);
14557 /* If the next token is an ellipsis, we're expanding a template
14558 argument pack. */
14559 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14561 if (argument == error_mark_node)
14563 cp_token *token = cp_lexer_peek_token (parser->lexer);
14564 error_at (token->location,
14565 "expected parameter pack before %<...%>");
14567 /* Consume the `...' token. */
14568 cp_lexer_consume_token (parser->lexer);
14570 /* Make the argument into a TYPE_PACK_EXPANSION or
14571 EXPR_PACK_EXPANSION. */
14572 argument = make_pack_expansion (argument);
14575 // If we end up with a template decl argument, check if this is actually
14576 // a template template parameter and drop the outer wrapping.
14577 if (DECL_TEMPLATE_TEMPLATE_PARM_P (argument))
14578 argument = TREE_TYPE (argument);
14580 if (n_args == alloced)
14582 alloced *= 2;
14584 if (arg_ary == fixed_args)
14586 arg_ary = XNEWVEC (tree, alloced);
14587 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14589 else
14590 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14592 arg_ary[n_args++] = argument;
14594 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14596 vec = make_tree_vec (n_args);
14598 while (n_args--)
14599 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14601 if (arg_ary != fixed_args)
14602 free (arg_ary);
14603 parser->non_integral_constant_expression_p = saved_non_ice_p;
14604 parser->integral_constant_expression_p = saved_ice_p;
14605 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14606 #ifdef ENABLE_CHECKING
14607 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14608 #endif
14609 return vec;
14612 /* Parse a template-argument.
14614 template-argument:
14615 assignment-expression
14616 type-id
14617 id-expression
14619 The representation is that of an assignment-expression, type-id, or
14620 id-expression -- except that the qualified id-expression is
14621 evaluated, so that the value returned is either a DECL or an
14622 OVERLOAD.
14624 Although the standard says "assignment-expression", it forbids
14625 throw-expressions or assignments in the template argument.
14626 Therefore, we use "conditional-expression" instead. */
14628 static tree
14629 cp_parser_template_argument (cp_parser* parser)
14631 tree argument;
14632 bool template_p;
14633 bool address_p;
14634 bool maybe_type_id = false;
14635 cp_token *token = NULL, *argument_start_token = NULL;
14636 location_t loc = 0;
14637 cp_id_kind idk;
14639 /* There's really no way to know what we're looking at, so we just
14640 try each alternative in order.
14642 [temp.arg]
14644 In a template-argument, an ambiguity between a type-id and an
14645 expression is resolved to a type-id, regardless of the form of
14646 the corresponding template-parameter.
14648 Therefore, we try a type-id first. */
14649 cp_parser_parse_tentatively (parser);
14650 argument = cp_parser_template_type_arg (parser);
14651 /* If there was no error parsing the type-id but the next token is a
14652 '>>', our behavior depends on which dialect of C++ we're
14653 parsing. In C++98, we probably found a typo for '> >'. But there
14654 are type-id which are also valid expressions. For instance:
14656 struct X { int operator >> (int); };
14657 template <int V> struct Foo {};
14658 Foo<X () >> 5> r;
14660 Here 'X()' is a valid type-id of a function type, but the user just
14661 wanted to write the expression "X() >> 5". Thus, we remember that we
14662 found a valid type-id, but we still try to parse the argument as an
14663 expression to see what happens.
14665 In C++0x, the '>>' will be considered two separate '>'
14666 tokens. */
14667 if (!cp_parser_error_occurred (parser)
14668 && cxx_dialect == cxx98
14669 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14671 maybe_type_id = true;
14672 cp_parser_abort_tentative_parse (parser);
14674 else
14676 /* If the next token isn't a `,' or a `>', then this argument wasn't
14677 really finished. This means that the argument is not a valid
14678 type-id. */
14679 if (!cp_parser_next_token_ends_template_argument_p (parser))
14680 cp_parser_error (parser, "expected template-argument");
14681 /* If that worked, we're done. */
14682 if (cp_parser_parse_definitely (parser))
14683 return argument;
14685 /* We're still not sure what the argument will be. */
14686 cp_parser_parse_tentatively (parser);
14687 /* Try a template. */
14688 argument_start_token = cp_lexer_peek_token (parser->lexer);
14689 argument = cp_parser_id_expression (parser,
14690 /*template_keyword_p=*/false,
14691 /*check_dependency_p=*/true,
14692 &template_p,
14693 /*declarator_p=*/false,
14694 /*optional_p=*/false);
14695 /* If the next token isn't a `,' or a `>', then this argument wasn't
14696 really finished. */
14697 if (!cp_parser_next_token_ends_template_argument_p (parser))
14698 cp_parser_error (parser, "expected template-argument");
14699 if (!cp_parser_error_occurred (parser))
14701 /* Figure out what is being referred to. If the id-expression
14702 was for a class template specialization, then we will have a
14703 TYPE_DECL at this point. There is no need to do name lookup
14704 at this point in that case. */
14705 if (TREE_CODE (argument) != TYPE_DECL)
14706 argument = cp_parser_lookup_name (parser, argument,
14707 none_type,
14708 /*is_template=*/template_p,
14709 /*is_namespace=*/false,
14710 /*check_dependency=*/true,
14711 /*ambiguous_decls=*/NULL,
14712 argument_start_token->location);
14713 if (TREE_CODE (argument) != TEMPLATE_DECL
14714 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14715 cp_parser_error (parser, "expected template-name");
14717 if (cp_parser_parse_definitely (parser))
14719 if (TREE_DEPRECATED (argument))
14720 warn_deprecated_use (argument, NULL_TREE);
14721 return argument;
14723 /* It must be a non-type argument. There permitted cases are given
14724 in [temp.arg.nontype]:
14726 -- an integral constant-expression of integral or enumeration
14727 type; or
14729 -- the name of a non-type template-parameter; or
14731 -- the name of an object or function with external linkage...
14733 -- the address of an object or function with external linkage...
14735 -- a pointer to member... */
14736 /* Look for a non-type template parameter. */
14737 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14739 cp_parser_parse_tentatively (parser);
14740 argument = cp_parser_primary_expression (parser,
14741 /*address_p=*/false,
14742 /*cast_p=*/false,
14743 /*template_arg_p=*/true,
14744 &idk);
14745 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14746 || !cp_parser_next_token_ends_template_argument_p (parser))
14747 cp_parser_simulate_error (parser);
14748 if (cp_parser_parse_definitely (parser))
14749 return argument;
14752 /* If the next token is "&", the argument must be the address of an
14753 object or function with external linkage. */
14754 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14755 if (address_p)
14757 loc = cp_lexer_peek_token (parser->lexer)->location;
14758 cp_lexer_consume_token (parser->lexer);
14760 /* See if we might have an id-expression. */
14761 token = cp_lexer_peek_token (parser->lexer);
14762 if (token->type == CPP_NAME
14763 || token->keyword == RID_OPERATOR
14764 || token->type == CPP_SCOPE
14765 || token->type == CPP_TEMPLATE_ID
14766 || token->type == CPP_NESTED_NAME_SPECIFIER)
14768 cp_parser_parse_tentatively (parser);
14769 argument = cp_parser_primary_expression (parser,
14770 address_p,
14771 /*cast_p=*/false,
14772 /*template_arg_p=*/true,
14773 &idk);
14774 if (cp_parser_error_occurred (parser)
14775 || !cp_parser_next_token_ends_template_argument_p (parser))
14776 cp_parser_abort_tentative_parse (parser);
14777 else
14779 tree probe;
14781 if (INDIRECT_REF_P (argument))
14783 /* Strip the dereference temporarily. */
14784 gcc_assert (REFERENCE_REF_P (argument));
14785 argument = TREE_OPERAND (argument, 0);
14788 /* If we're in a template, we represent a qualified-id referring
14789 to a static data member as a SCOPE_REF even if the scope isn't
14790 dependent so that we can check access control later. */
14791 probe = argument;
14792 if (TREE_CODE (probe) == SCOPE_REF)
14793 probe = TREE_OPERAND (probe, 1);
14794 if (VAR_P (probe))
14796 /* A variable without external linkage might still be a
14797 valid constant-expression, so no error is issued here
14798 if the external-linkage check fails. */
14799 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14800 cp_parser_simulate_error (parser);
14802 else if (is_overloaded_fn (argument))
14803 /* All overloaded functions are allowed; if the external
14804 linkage test does not pass, an error will be issued
14805 later. */
14807 else if (address_p
14808 && (TREE_CODE (argument) == OFFSET_REF
14809 || TREE_CODE (argument) == SCOPE_REF))
14810 /* A pointer-to-member. */
14812 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14814 else
14815 cp_parser_simulate_error (parser);
14817 if (cp_parser_parse_definitely (parser))
14819 if (address_p)
14820 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14821 tf_warning_or_error);
14822 else
14823 argument = convert_from_reference (argument);
14824 return argument;
14828 /* If the argument started with "&", there are no other valid
14829 alternatives at this point. */
14830 if (address_p)
14832 cp_parser_error (parser, "invalid non-type template argument");
14833 return error_mark_node;
14836 /* If the argument wasn't successfully parsed as a type-id followed
14837 by '>>', the argument can only be a constant expression now.
14838 Otherwise, we try parsing the constant-expression tentatively,
14839 because the argument could really be a type-id. */
14840 if (maybe_type_id)
14841 cp_parser_parse_tentatively (parser);
14842 argument = cp_parser_constant_expression (parser);
14844 if (!maybe_type_id)
14845 return argument;
14846 if (!cp_parser_next_token_ends_template_argument_p (parser))
14847 cp_parser_error (parser, "expected template-argument");
14848 if (cp_parser_parse_definitely (parser))
14849 return argument;
14850 /* We did our best to parse the argument as a non type-id, but that
14851 was the only alternative that matched (albeit with a '>' after
14852 it). We can assume it's just a typo from the user, and a
14853 diagnostic will then be issued. */
14854 return cp_parser_template_type_arg (parser);
14857 /* Parse an explicit-instantiation.
14859 explicit-instantiation:
14860 template declaration
14862 Although the standard says `declaration', what it really means is:
14864 explicit-instantiation:
14865 template decl-specifier-seq [opt] declarator [opt] ;
14867 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14868 supposed to be allowed. A defect report has been filed about this
14869 issue.
14871 GNU Extension:
14873 explicit-instantiation:
14874 storage-class-specifier template
14875 decl-specifier-seq [opt] declarator [opt] ;
14876 function-specifier template
14877 decl-specifier-seq [opt] declarator [opt] ; */
14879 static void
14880 cp_parser_explicit_instantiation (cp_parser* parser)
14882 int declares_class_or_enum;
14883 cp_decl_specifier_seq decl_specifiers;
14884 tree extension_specifier = NULL_TREE;
14886 timevar_push (TV_TEMPLATE_INST);
14888 /* Look for an (optional) storage-class-specifier or
14889 function-specifier. */
14890 if (cp_parser_allow_gnu_extensions_p (parser))
14892 extension_specifier
14893 = cp_parser_storage_class_specifier_opt (parser);
14894 if (!extension_specifier)
14895 extension_specifier
14896 = cp_parser_function_specifier_opt (parser,
14897 /*decl_specs=*/NULL);
14900 /* Look for the `template' keyword. */
14901 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14902 /* Let the front end know that we are processing an explicit
14903 instantiation. */
14904 begin_explicit_instantiation ();
14905 /* [temp.explicit] says that we are supposed to ignore access
14906 control while processing explicit instantiation directives. */
14907 push_deferring_access_checks (dk_no_check);
14908 /* Parse a decl-specifier-seq. */
14909 cp_parser_decl_specifier_seq (parser,
14910 CP_PARSER_FLAGS_OPTIONAL,
14911 &decl_specifiers,
14912 &declares_class_or_enum);
14913 /* If there was exactly one decl-specifier, and it declared a class,
14914 and there's no declarator, then we have an explicit type
14915 instantiation. */
14916 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14918 tree type;
14920 type = check_tag_decl (&decl_specifiers,
14921 /*explicit_type_instantiation_p=*/true);
14922 /* Turn access control back on for names used during
14923 template instantiation. */
14924 pop_deferring_access_checks ();
14925 if (type)
14926 do_type_instantiation (type, extension_specifier,
14927 /*complain=*/tf_error);
14929 else
14931 cp_declarator *declarator;
14932 tree decl;
14934 /* Parse the declarator. */
14935 declarator
14936 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14937 /*ctor_dtor_or_conv_p=*/NULL,
14938 /*parenthesized_p=*/NULL,
14939 /*member_p=*/false,
14940 /*friend_p=*/false);
14941 if (declares_class_or_enum & 2)
14942 cp_parser_check_for_definition_in_return_type (declarator,
14943 decl_specifiers.type,
14944 decl_specifiers.locations[ds_type_spec]);
14945 if (declarator != cp_error_declarator)
14947 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14948 permerror (decl_specifiers.locations[ds_inline],
14949 "explicit instantiation shall not use"
14950 " %<inline%> specifier");
14951 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14952 permerror (decl_specifiers.locations[ds_constexpr],
14953 "explicit instantiation shall not use"
14954 " %<constexpr%> specifier");
14956 decl = grokdeclarator (declarator, &decl_specifiers,
14957 NORMAL, 0, &decl_specifiers.attributes);
14958 /* Turn access control back on for names used during
14959 template instantiation. */
14960 pop_deferring_access_checks ();
14961 /* Do the explicit instantiation. */
14962 do_decl_instantiation (decl, extension_specifier);
14964 else
14966 pop_deferring_access_checks ();
14967 /* Skip the body of the explicit instantiation. */
14968 cp_parser_skip_to_end_of_statement (parser);
14971 /* We're done with the instantiation. */
14972 end_explicit_instantiation ();
14974 cp_parser_consume_semicolon_at_end_of_statement (parser);
14976 timevar_pop (TV_TEMPLATE_INST);
14979 /* Parse an explicit-specialization.
14981 explicit-specialization:
14982 template < > declaration
14984 Although the standard says `declaration', what it really means is:
14986 explicit-specialization:
14987 template <> decl-specifier [opt] init-declarator [opt] ;
14988 template <> function-definition
14989 template <> explicit-specialization
14990 template <> template-declaration */
14992 static void
14993 cp_parser_explicit_specialization (cp_parser* parser)
14995 bool need_lang_pop;
14996 cp_token *token = cp_lexer_peek_token (parser->lexer);
14998 /* Look for the `template' keyword. */
14999 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15000 /* Look for the `<'. */
15001 cp_parser_require (parser, CPP_LESS, RT_LESS);
15002 /* Look for the `>'. */
15003 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15004 /* We have processed another parameter list. */
15005 ++parser->num_template_parameter_lists;
15006 /* [temp]
15008 A template ... explicit specialization ... shall not have C
15009 linkage. */
15010 if (current_lang_name == lang_name_c)
15012 error_at (token->location, "template specialization with C linkage");
15013 /* Give it C++ linkage to avoid confusing other parts of the
15014 front end. */
15015 push_lang_context (lang_name_cplusplus);
15016 need_lang_pop = true;
15018 else
15019 need_lang_pop = false;
15020 /* Let the front end know that we are beginning a specialization. */
15021 if (!begin_specialization ())
15023 end_specialization ();
15024 return;
15027 /* If the next keyword is `template', we need to figure out whether
15028 or not we're looking a template-declaration. */
15029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15031 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15032 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15033 cp_parser_template_declaration_after_export (parser,
15034 /*member_p=*/false);
15035 else
15036 cp_parser_explicit_specialization (parser);
15038 else
15039 /* Parse the dependent declaration. */
15040 cp_parser_single_declaration (parser,
15041 /*checks=*/NULL,
15042 /*member_p=*/false,
15043 /*explicit_specialization_p=*/true,
15044 /*friend_p=*/NULL);
15045 /* We're done with the specialization. */
15046 end_specialization ();
15047 /* For the erroneous case of a template with C linkage, we pushed an
15048 implicit C++ linkage scope; exit that scope now. */
15049 if (need_lang_pop)
15050 pop_lang_context ();
15051 /* We're done with this parameter list. */
15052 --parser->num_template_parameter_lists;
15055 /* Parse a type-specifier.
15057 type-specifier:
15058 simple-type-specifier
15059 class-specifier
15060 enum-specifier
15061 elaborated-type-specifier
15062 cv-qualifier
15064 GNU Extension:
15066 type-specifier:
15067 __complex__
15069 Returns a representation of the type-specifier. For a
15070 class-specifier, enum-specifier, or elaborated-type-specifier, a
15071 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15073 The parser flags FLAGS is used to control type-specifier parsing.
15075 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15076 in a decl-specifier-seq.
15078 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15079 class-specifier, enum-specifier, or elaborated-type-specifier, then
15080 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15081 if a type is declared; 2 if it is defined. Otherwise, it is set to
15082 zero.
15084 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15085 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15086 is set to FALSE. */
15088 static tree
15089 cp_parser_type_specifier (cp_parser* parser,
15090 cp_parser_flags flags,
15091 cp_decl_specifier_seq *decl_specs,
15092 bool is_declaration,
15093 int* declares_class_or_enum,
15094 bool* is_cv_qualifier)
15096 tree type_spec = NULL_TREE;
15097 cp_token *token;
15098 enum rid keyword;
15099 cp_decl_spec ds = ds_last;
15101 /* Assume this type-specifier does not declare a new type. */
15102 if (declares_class_or_enum)
15103 *declares_class_or_enum = 0;
15104 /* And that it does not specify a cv-qualifier. */
15105 if (is_cv_qualifier)
15106 *is_cv_qualifier = false;
15107 /* Peek at the next token. */
15108 token = cp_lexer_peek_token (parser->lexer);
15110 /* If we're looking at a keyword, we can use that to guide the
15111 production we choose. */
15112 keyword = token->keyword;
15113 switch (keyword)
15115 case RID_ENUM:
15116 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15117 goto elaborated_type_specifier;
15119 /* Look for the enum-specifier. */
15120 type_spec = cp_parser_enum_specifier (parser);
15121 /* If that worked, we're done. */
15122 if (type_spec)
15124 if (declares_class_or_enum)
15125 *declares_class_or_enum = 2;
15126 if (decl_specs)
15127 cp_parser_set_decl_spec_type (decl_specs,
15128 type_spec,
15129 token,
15130 /*type_definition_p=*/true);
15131 return type_spec;
15133 else
15134 goto elaborated_type_specifier;
15136 /* Any of these indicate either a class-specifier, or an
15137 elaborated-type-specifier. */
15138 case RID_CLASS:
15139 case RID_STRUCT:
15140 case RID_UNION:
15141 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15142 goto elaborated_type_specifier;
15144 /* Parse tentatively so that we can back up if we don't find a
15145 class-specifier. */
15146 cp_parser_parse_tentatively (parser);
15147 /* Look for the class-specifier. */
15148 type_spec = cp_parser_class_specifier (parser);
15149 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15150 /* If that worked, we're done. */
15151 if (cp_parser_parse_definitely (parser))
15153 if (declares_class_or_enum)
15154 *declares_class_or_enum = 2;
15155 if (decl_specs)
15156 cp_parser_set_decl_spec_type (decl_specs,
15157 type_spec,
15158 token,
15159 /*type_definition_p=*/true);
15160 return type_spec;
15163 /* Fall through. */
15164 elaborated_type_specifier:
15165 /* We're declaring (not defining) a class or enum. */
15166 if (declares_class_or_enum)
15167 *declares_class_or_enum = 1;
15169 /* Fall through. */
15170 case RID_TYPENAME:
15171 /* Look for an elaborated-type-specifier. */
15172 type_spec
15173 = (cp_parser_elaborated_type_specifier
15174 (parser,
15175 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15176 is_declaration));
15177 if (decl_specs)
15178 cp_parser_set_decl_spec_type (decl_specs,
15179 type_spec,
15180 token,
15181 /*type_definition_p=*/false);
15182 return type_spec;
15184 case RID_CONST:
15185 ds = ds_const;
15186 if (is_cv_qualifier)
15187 *is_cv_qualifier = true;
15188 break;
15190 case RID_VOLATILE:
15191 ds = ds_volatile;
15192 if (is_cv_qualifier)
15193 *is_cv_qualifier = true;
15194 break;
15196 case RID_RESTRICT:
15197 ds = ds_restrict;
15198 if (is_cv_qualifier)
15199 *is_cv_qualifier = true;
15200 break;
15202 case RID_COMPLEX:
15203 /* The `__complex__' keyword is a GNU extension. */
15204 ds = ds_complex;
15205 break;
15207 default:
15208 break;
15211 /* Handle simple keywords. */
15212 if (ds != ds_last)
15214 if (decl_specs)
15216 set_and_check_decl_spec_loc (decl_specs, ds, token);
15217 decl_specs->any_specifiers_p = true;
15219 return cp_lexer_consume_token (parser->lexer)->u.value;
15222 /* If we do not already have a type-specifier, assume we are looking
15223 at a simple-type-specifier. */
15224 type_spec = cp_parser_simple_type_specifier (parser,
15225 decl_specs,
15226 flags);
15228 /* If we didn't find a type-specifier, and a type-specifier was not
15229 optional in this context, issue an error message. */
15230 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15232 cp_parser_error (parser, "expected type specifier");
15233 return error_mark_node;
15236 return type_spec;
15239 /* Parse a simple-type-specifier.
15241 simple-type-specifier:
15242 :: [opt] nested-name-specifier [opt] type-name
15243 :: [opt] nested-name-specifier template template-id
15244 char
15245 wchar_t
15246 bool
15247 short
15249 long
15250 signed
15251 unsigned
15252 float
15253 double
15254 void
15256 C++0x Extension:
15258 simple-type-specifier:
15259 auto
15260 decltype ( expression )
15261 char16_t
15262 char32_t
15263 __underlying_type ( type-id )
15265 GNU Extension:
15267 simple-type-specifier:
15268 __int128
15269 __typeof__ unary-expression
15270 __typeof__ ( type-id )
15271 __typeof__ ( type-id ) { initializer-list , [opt] }
15273 Concepts Extension:
15275 simple-type-specifier:
15276 constrained-type-specifier
15278 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15279 appropriately updated. */
15281 static tree
15282 cp_parser_simple_type_specifier (cp_parser* parser,
15283 cp_decl_specifier_seq *decl_specs,
15284 cp_parser_flags flags)
15286 tree type = NULL_TREE;
15287 cp_token *token;
15288 int idx;
15290 /* Peek at the next token. */
15291 token = cp_lexer_peek_token (parser->lexer);
15293 /* If we're looking at a keyword, things are easy. */
15294 switch (token->keyword)
15296 case RID_CHAR:
15297 if (decl_specs)
15298 decl_specs->explicit_char_p = true;
15299 type = char_type_node;
15300 break;
15301 case RID_CHAR16:
15302 type = char16_type_node;
15303 break;
15304 case RID_CHAR32:
15305 type = char32_type_node;
15306 break;
15307 case RID_WCHAR:
15308 type = wchar_type_node;
15309 break;
15310 case RID_BOOL:
15311 type = boolean_type_node;
15312 break;
15313 case RID_SHORT:
15314 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15315 type = short_integer_type_node;
15316 break;
15317 case RID_INT:
15318 if (decl_specs)
15319 decl_specs->explicit_int_p = true;
15320 type = integer_type_node;
15321 break;
15322 case RID_INT_N_0:
15323 case RID_INT_N_1:
15324 case RID_INT_N_2:
15325 case RID_INT_N_3:
15326 idx = token->keyword - RID_INT_N_0;
15327 if (! int_n_enabled_p [idx])
15328 break;
15329 if (decl_specs)
15331 decl_specs->explicit_intN_p = true;
15332 decl_specs->int_n_idx = idx;
15334 type = int_n_trees [idx].signed_type;
15335 break;
15336 case RID_LONG:
15337 if (decl_specs)
15338 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
15339 type = long_integer_type_node;
15340 break;
15341 case RID_SIGNED:
15342 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
15343 type = integer_type_node;
15344 break;
15345 case RID_UNSIGNED:
15346 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
15347 type = unsigned_type_node;
15348 break;
15349 case RID_FLOAT:
15350 type = float_type_node;
15351 break;
15352 case RID_DOUBLE:
15353 type = double_type_node;
15354 break;
15355 case RID_VOID:
15356 type = void_type_node;
15357 break;
15359 case RID_AUTO:
15360 maybe_warn_cpp0x (CPP0X_AUTO);
15361 if (parser->auto_is_implicit_function_template_parm_p)
15363 if (cxx_dialect >= cxx14)
15364 type = synthesize_implicit_template_parm (parser, NULL_TREE);
15365 else
15366 type = error_mark_node;
15368 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
15370 if (cxx_dialect < cxx14)
15371 error_at (token->location,
15372 "use of %<auto%> in lambda parameter declaration "
15373 "only available with "
15374 "-std=c++14 or -std=gnu++14");
15376 else if (cxx_dialect < cxx14)
15377 error_at (token->location,
15378 "use of %<auto%> in parameter declaration "
15379 "only available with "
15380 "-std=c++14 or -std=gnu++14");
15381 else
15382 pedwarn (token->location, OPT_Wpedantic,
15383 "ISO C++ forbids use of %<auto%> in parameter "
15384 "declaration");
15386 else
15387 type = make_auto ();
15388 break;
15390 case RID_DECLTYPE:
15391 /* Since DR 743, decltype can either be a simple-type-specifier by
15392 itself or begin a nested-name-specifier. Parsing it will replace
15393 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15394 handling below decide what to do. */
15395 cp_parser_decltype (parser);
15396 cp_lexer_set_token_position (parser->lexer, token);
15397 break;
15399 case RID_TYPEOF:
15400 /* Consume the `typeof' token. */
15401 cp_lexer_consume_token (parser->lexer);
15402 /* Parse the operand to `typeof'. */
15403 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15404 /* If it is not already a TYPE, take its type. */
15405 if (!TYPE_P (type))
15406 type = finish_typeof (type);
15408 if (decl_specs)
15409 cp_parser_set_decl_spec_type (decl_specs, type,
15410 token,
15411 /*type_definition_p=*/false);
15413 return type;
15415 case RID_UNDERLYING_TYPE:
15416 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15417 if (decl_specs)
15418 cp_parser_set_decl_spec_type (decl_specs, type,
15419 token,
15420 /*type_definition_p=*/false);
15422 return type;
15424 case RID_BASES:
15425 case RID_DIRECT_BASES:
15426 type = cp_parser_trait_expr (parser, token->keyword);
15427 if (decl_specs)
15428 cp_parser_set_decl_spec_type (decl_specs, type,
15429 token,
15430 /*type_definition_p=*/false);
15431 return type;
15432 default:
15433 break;
15436 /* If token is an already-parsed decltype not followed by ::,
15437 it's a simple-type-specifier. */
15438 if (token->type == CPP_DECLTYPE
15439 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15441 type = token->u.value;
15442 if (decl_specs)
15444 cp_parser_set_decl_spec_type (decl_specs, type,
15445 token,
15446 /*type_definition_p=*/false);
15447 /* Remember that we are handling a decltype in order to
15448 implement the resolution of DR 1510 when the argument
15449 isn't instantiation dependent. */
15450 decl_specs->decltype_p = true;
15452 cp_lexer_consume_token (parser->lexer);
15453 return type;
15456 /* If the type-specifier was for a built-in type, we're done. */
15457 if (type)
15459 /* Record the type. */
15460 if (decl_specs
15461 && (token->keyword != RID_SIGNED
15462 && token->keyword != RID_UNSIGNED
15463 && token->keyword != RID_SHORT
15464 && token->keyword != RID_LONG))
15465 cp_parser_set_decl_spec_type (decl_specs,
15466 type,
15467 token,
15468 /*type_definition_p=*/false);
15469 if (decl_specs)
15470 decl_specs->any_specifiers_p = true;
15472 /* Consume the token. */
15473 cp_lexer_consume_token (parser->lexer);
15475 if (type == error_mark_node)
15476 return error_mark_node;
15478 /* There is no valid C++ program where a non-template type is
15479 followed by a "<". That usually indicates that the user thought
15480 that the type was a template. */
15481 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15482 token->location);
15484 return TYPE_NAME (type);
15487 /* The type-specifier must be a user-defined type. */
15488 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15490 bool qualified_p;
15491 bool global_p;
15493 /* Don't gobble tokens or issue error messages if this is an
15494 optional type-specifier. */
15495 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15496 cp_parser_parse_tentatively (parser);
15498 /* Look for the optional `::' operator. */
15499 global_p
15500 = (cp_parser_global_scope_opt (parser,
15501 /*current_scope_valid_p=*/false)
15502 != NULL_TREE);
15503 /* Look for the nested-name specifier. */
15504 qualified_p
15505 = (cp_parser_nested_name_specifier_opt (parser,
15506 /*typename_keyword_p=*/false,
15507 /*check_dependency_p=*/true,
15508 /*type_p=*/false,
15509 /*is_declaration=*/false)
15510 != NULL_TREE);
15511 token = cp_lexer_peek_token (parser->lexer);
15512 /* If we have seen a nested-name-specifier, and the next token
15513 is `template', then we are using the template-id production. */
15514 if (parser->scope
15515 && cp_parser_optional_template_keyword (parser))
15517 /* Look for the template-id. */
15518 type = cp_parser_template_id (parser,
15519 /*template_keyword_p=*/true,
15520 /*check_dependency_p=*/true,
15521 none_type,
15522 /*is_declaration=*/false);
15523 /* If the template-id did not name a type, we are out of
15524 luck. */
15525 if (TREE_CODE (type) != TYPE_DECL)
15527 cp_parser_error (parser, "expected template-id for type");
15528 type = NULL_TREE;
15531 /* Otherwise, look for a type-name. */
15532 else
15533 type = cp_parser_type_name (parser);
15535 /* Keep track of all name-lookups performed in class scopes. */
15536 if (type
15537 && !global_p
15538 && !qualified_p
15539 && TREE_CODE (type) == TYPE_DECL
15540 && identifier_p (DECL_NAME (type)))
15541 maybe_note_name_used_in_class (DECL_NAME (type), type);
15542 /* If it didn't work out, we don't have a TYPE. */
15543 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15544 && !cp_parser_parse_definitely (parser))
15545 type = NULL_TREE;
15546 if (type && decl_specs)
15547 cp_parser_set_decl_spec_type (decl_specs, type,
15548 token,
15549 /*type_definition_p=*/false);
15552 /* If we didn't get a type-name, issue an error message. */
15553 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15555 cp_parser_error (parser, "expected type-name");
15556 return error_mark_node;
15559 if (type && type != error_mark_node)
15561 /* See if TYPE is an Objective-C type, and if so, parse and
15562 accept any protocol references following it. Do this before
15563 the cp_parser_check_for_invalid_template_id() call, because
15564 Objective-C types can be followed by '<...>' which would
15565 enclose protocol names rather than template arguments, and so
15566 everything is fine. */
15567 if (c_dialect_objc () && !parser->scope
15568 && (objc_is_id (type) || objc_is_class_name (type)))
15570 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15571 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15573 /* Clobber the "unqualified" type previously entered into
15574 DECL_SPECS with the new, improved protocol-qualified version. */
15575 if (decl_specs)
15576 decl_specs->type = qual_type;
15578 return qual_type;
15581 /* There is no valid C++ program where a non-template type is
15582 followed by a "<". That usually indicates that the user
15583 thought that the type was a template. */
15584 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15585 none_type,
15586 token->location);
15589 return type;
15592 /* Parse a type-name.
15594 type-name:
15595 class-name
15596 enum-name
15597 typedef-name
15598 simple-template-id [in c++0x]
15600 enum-name:
15601 identifier
15603 typedef-name:
15604 identifier
15606 Concepts:
15608 type-name:
15609 concept-name
15610 partial-concept-id
15612 concept-name:
15613 identifier
15615 Returns a TYPE_DECL for the type. */
15617 static tree
15618 cp_parser_type_name (cp_parser* parser)
15620 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
15623 /* See above. */
15624 static tree
15625 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
15627 tree type_decl;
15629 /* We can't know yet whether it is a class-name or not. */
15630 cp_parser_parse_tentatively (parser);
15631 /* Try a class-name. */
15632 type_decl = cp_parser_class_name (parser,
15633 typename_keyword_p,
15634 /*template_keyword_p=*/false,
15635 none_type,
15636 /*check_dependency_p=*/true,
15637 /*class_head_p=*/false,
15638 /*is_declaration=*/false);
15640 /* If it's not a class-name, keep looking. */
15641 if (!cp_parser_parse_definitely (parser))
15643 if (cxx_dialect < cxx11)
15644 /* It must be a typedef-name or an enum-name. */
15645 return cp_parser_nonclass_name (parser);
15647 cp_parser_parse_tentatively (parser);
15648 /* It is either a simple-template-id representing an
15649 instantiation of an alias template... */
15650 type_decl = cp_parser_template_id (parser,
15651 /*template_keyword_p=*/false,
15652 /*check_dependency_p=*/true,
15653 none_type,
15654 /*is_declaration=*/false);
15656 /* Note that this must be an instantiation of an alias template
15657 because [temp.names]/6 says:
15659 A template-id that names an alias template specialization
15660 is a type-name.
15662 Whereas [temp.names]/7 says:
15664 A simple-template-id that names a class template
15665 specialization is a class-name.
15667 With concepts, this could also be a partial-concept-id that
15668 declares a non-type template parameter. */
15669 if (type_decl != NULL_TREE
15670 && TREE_CODE (type_decl) == TYPE_DECL
15671 && TYPE_DECL_ALIAS_P (type_decl))
15672 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15673 else if (maybe_type_parameter (type_decl))
15674 /* Don't do anything. */ ;
15675 else
15676 cp_parser_simulate_error (parser);
15678 if (!cp_parser_parse_definitely (parser))
15679 /* ... Or a typedef-name or an enum-name. */
15680 return cp_parser_nonclass_name (parser);
15683 return type_decl;
15686 /* Returns true if proto is a type parameter, but not a template
15687 template parameter. */
15688 static bool
15689 cp_check_type_concept (tree fn, tree proto)
15691 if (TREE_CODE (proto) != TYPE_DECL)
15693 error ("invalid use of non-type concept %qD", fn);
15694 return false;
15696 return true;
15700 /* Check if DECL and ARGS can form a constrained-type-specifier.
15701 If ARGS is non-null, we try to form a concept check of the
15702 form DECL<?, ARGS> where ? is a wildcard that matches any
15703 kind of template argument. If ARGS is NULL, then we try to
15704 form a concept check of the form DECL<?>. */
15706 static tree
15707 cp_maybe_constrained_type_specifier (cp_parser *parser, tree decl, tree args)
15709 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
15711 /* If we a constrained-type-specifier cannot be deduced. */
15712 if (parser->prevent_constrained_type_specifiers)
15713 return NULL_TREE;
15715 /* A constrained type specifier can only be found in an
15716 overload set or as a reference to a template declaration.
15718 FIXME: This might be masking a bug. It's possible that
15719 that the deduction below is causing template specializations
15720 to be formed with the wildcard as an argument. */
15721 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
15722 return NULL_TREE;
15724 /* Try to build a call expression that evaluates the
15725 concept. This can fail if the overload set refers
15726 only to non-templates. */
15727 tree placeholder = build_nt (WILDCARD_DECL);
15728 tree check = build_concept_check (decl, placeholder, args);
15729 if (check == error_mark_node)
15730 return NULL_TREE;
15732 /* Deduce the checked constraint and the prototype parameter.
15734 FIXME: In certain cases, failure to deduce should be a
15735 diagnosable error. */
15736 tree conc;
15737 tree proto;
15738 if (!deduce_constrained_parameter (check, conc, proto))
15739 return NULL_TREE;
15741 /* In template parameter scope, this results in a constrained
15742 parameter. Return a descriptor of that parm. */
15743 if (processing_template_parmlist)
15744 return build_constrained_parameter (conc, proto, args);
15746 /* In any other context, a concept must be a type concept.
15748 FIXME: A constrained-type-specifier can be a placeholder
15749 of any kind. */
15750 if (!cp_check_type_concept (conc, proto))
15751 return error_mark_node;
15753 /* In a parameter-declaration-clause, constrained-type
15754 specifiers result in invented template parameters. */
15755 if (parser->auto_is_implicit_function_template_parm_p)
15757 tree x = build_constrained_parameter (conc, proto, args);
15758 return synthesize_implicit_template_parm (parser, x);
15760 else
15762 /* Otherwise, we're in a context where the constrained
15763 type name is deduced and the constraint applies
15764 after deduction. */
15765 return make_constrained_auto(conc, args);
15768 return NULL_TREE;
15772 /* If DECL refers to a concept, return a TYPE_DECL representing
15773 the result of using the constrained type specifier in the
15774 current context. DECL refers to a concept if
15776 - it is an overload set containing a function concept taking a single
15777 type argument, or
15779 - it is a variable concept taking a single type argument. */
15781 static tree
15782 cp_maybe_concept_name (cp_parser* parser, tree decl)
15784 return cp_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
15788 /* Check if DECL and ARGS form a partial-concept-id. If so,
15789 assign ID to the resulting constrained placeholder.
15791 Returns true if the partial-concept-id designates a placeholder
15792 and false otherwise. Note that *id is set to NULL_TREE in
15793 this case. */
15795 bool
15796 cp_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args, tree* id)
15798 *id = cp_maybe_constrained_type_specifier (parser, decl, args);
15799 return *id != NULL_TREE;
15803 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
15804 or a concept-name.
15806 enum-name:
15807 identifier
15809 typedef-name:
15810 identifier
15812 concept-name:
15813 identifier
15815 Returns a TYPE_DECL for the type. */
15817 static tree
15818 cp_parser_nonclass_name (cp_parser* parser)
15820 tree type_decl;
15821 tree identifier;
15823 cp_token *token = cp_lexer_peek_token (parser->lexer);
15824 identifier = cp_parser_identifier (parser);
15825 if (identifier == error_mark_node)
15826 return error_mark_node;
15828 /* Look up the type-name. */
15829 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15830 type_decl = strip_using_decl (type_decl);
15832 /* If we found an overload set, then it may refer to a concept-name. */
15833 if (flag_concepts
15834 && (TREE_CODE (type_decl) == OVERLOAD
15835 || BASELINK_P (type_decl)
15836 || variable_concept_p (type_decl)))
15838 /* Determine whether the overload refers to a concept. */
15839 if (tree decl = cp_maybe_concept_name (parser, type_decl))
15840 return decl;
15843 if (TREE_CODE (type_decl) != TYPE_DECL
15844 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15846 /* See if this is an Objective-C type. */
15847 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15848 tree type = objc_get_protocol_qualified_type (identifier, protos);
15849 if (type)
15850 type_decl = TYPE_NAME (type);
15853 /* Issue an error if we did not find a type-name. */
15854 if (TREE_CODE (type_decl) != TYPE_DECL
15855 /* In Objective-C, we have the complication that class names are
15856 normally type names and start declarations (eg, the
15857 "NSObject" in "NSObject *object;"), but can be used in an
15858 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15859 is an expression. So, a classname followed by a dot is not a
15860 valid type-name. */
15861 || (objc_is_class_name (TREE_TYPE (type_decl))
15862 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15864 if (!cp_parser_simulate_error (parser))
15865 cp_parser_name_lookup_error (parser, identifier, type_decl,
15866 NLE_TYPE, token->location);
15867 return error_mark_node;
15869 /* Remember that the name was used in the definition of the
15870 current class so that we can check later to see if the
15871 meaning would have been different after the class was
15872 entirely defined. */
15873 else if (type_decl != error_mark_node
15874 && !parser->scope)
15875 maybe_note_name_used_in_class (identifier, type_decl);
15877 return type_decl;
15880 /* Parse an elaborated-type-specifier. Note that the grammar given
15881 here incorporates the resolution to DR68.
15883 elaborated-type-specifier:
15884 class-key :: [opt] nested-name-specifier [opt] identifier
15885 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15886 enum-key :: [opt] nested-name-specifier [opt] identifier
15887 typename :: [opt] nested-name-specifier identifier
15888 typename :: [opt] nested-name-specifier template [opt]
15889 template-id
15891 GNU extension:
15893 elaborated-type-specifier:
15894 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15895 class-key attributes :: [opt] nested-name-specifier [opt]
15896 template [opt] template-id
15897 enum attributes :: [opt] nested-name-specifier [opt] identifier
15899 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15900 declared `friend'. If IS_DECLARATION is TRUE, then this
15901 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15902 something is being declared.
15904 Returns the TYPE specified. */
15906 static tree
15907 cp_parser_elaborated_type_specifier (cp_parser* parser,
15908 bool is_friend,
15909 bool is_declaration)
15911 enum tag_types tag_type;
15912 tree identifier;
15913 tree type = NULL_TREE;
15914 tree attributes = NULL_TREE;
15915 tree globalscope;
15916 cp_token *token = NULL;
15918 /* See if we're looking at the `enum' keyword. */
15919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15921 /* Consume the `enum' token. */
15922 cp_lexer_consume_token (parser->lexer);
15923 /* Remember that it's an enumeration type. */
15924 tag_type = enum_type;
15925 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15926 enums) is used here. */
15927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15928 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15930 pedwarn (input_location, 0, "elaborated-type-specifier "
15931 "for a scoped enum must not use the %<%D%> keyword",
15932 cp_lexer_peek_token (parser->lexer)->u.value);
15933 /* Consume the `struct' or `class' and parse it anyway. */
15934 cp_lexer_consume_token (parser->lexer);
15936 /* Parse the attributes. */
15937 attributes = cp_parser_attributes_opt (parser);
15939 /* Or, it might be `typename'. */
15940 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15941 RID_TYPENAME))
15943 /* Consume the `typename' token. */
15944 cp_lexer_consume_token (parser->lexer);
15945 /* Remember that it's a `typename' type. */
15946 tag_type = typename_type;
15948 /* Otherwise it must be a class-key. */
15949 else
15951 tag_type = cp_parser_class_key (parser);
15952 if (tag_type == none_type)
15953 return error_mark_node;
15954 /* Parse the attributes. */
15955 attributes = cp_parser_attributes_opt (parser);
15958 /* Look for the `::' operator. */
15959 globalscope = cp_parser_global_scope_opt (parser,
15960 /*current_scope_valid_p=*/false);
15961 /* Look for the nested-name-specifier. */
15962 if (tag_type == typename_type && !globalscope)
15964 if (!cp_parser_nested_name_specifier (parser,
15965 /*typename_keyword_p=*/true,
15966 /*check_dependency_p=*/true,
15967 /*type_p=*/true,
15968 is_declaration))
15969 return error_mark_node;
15971 else
15972 /* Even though `typename' is not present, the proposed resolution
15973 to Core Issue 180 says that in `class A<T>::B', `B' should be
15974 considered a type-name, even if `A<T>' is dependent. */
15975 cp_parser_nested_name_specifier_opt (parser,
15976 /*typename_keyword_p=*/true,
15977 /*check_dependency_p=*/true,
15978 /*type_p=*/true,
15979 is_declaration);
15980 /* For everything but enumeration types, consider a template-id.
15981 For an enumeration type, consider only a plain identifier. */
15982 if (tag_type != enum_type)
15984 bool template_p = false;
15985 tree decl;
15987 /* Allow the `template' keyword. */
15988 template_p = cp_parser_optional_template_keyword (parser);
15989 /* If we didn't see `template', we don't know if there's a
15990 template-id or not. */
15991 if (!template_p)
15992 cp_parser_parse_tentatively (parser);
15993 /* Parse the template-id. */
15994 token = cp_lexer_peek_token (parser->lexer);
15995 decl = cp_parser_template_id (parser, template_p,
15996 /*check_dependency_p=*/true,
15997 tag_type,
15998 is_declaration);
15999 /* If we didn't find a template-id, look for an ordinary
16000 identifier. */
16001 if (!template_p && !cp_parser_parse_definitely (parser))
16003 /* We can get here when cp_parser_template_id, called by
16004 cp_parser_class_name with tag_type == none_type, succeeds
16005 and caches a BASELINK. Then, when called again here,
16006 instead of failing and returning an error_mark_node
16007 returns it (see template/typename17.C in C++11).
16008 ??? Could we diagnose this earlier? */
16009 else if (tag_type == typename_type && BASELINK_P (decl))
16011 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16012 type = error_mark_node;
16014 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16015 in effect, then we must assume that, upon instantiation, the
16016 template will correspond to a class. */
16017 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16018 && tag_type == typename_type)
16019 type = make_typename_type (parser->scope, decl,
16020 typename_type,
16021 /*complain=*/tf_error);
16022 /* If the `typename' keyword is in effect and DECL is not a type
16023 decl, then type is non existent. */
16024 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16026 else if (TREE_CODE (decl) == TYPE_DECL)
16027 type = check_elaborated_type_specifier (tag_type, decl,
16028 /*allow_template_p=*/true);
16029 else if (decl == error_mark_node)
16030 type = error_mark_node;
16033 if (!type)
16035 token = cp_lexer_peek_token (parser->lexer);
16036 identifier = cp_parser_identifier (parser);
16038 if (identifier == error_mark_node)
16040 parser->scope = NULL_TREE;
16041 return error_mark_node;
16044 /* For a `typename', we needn't call xref_tag. */
16045 if (tag_type == typename_type
16046 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16047 return cp_parser_make_typename_type (parser, identifier,
16048 token->location);
16050 /* Template parameter lists apply only if we are not within a
16051 function parameter list. */
16052 bool template_parm_lists_apply
16053 = parser->num_template_parameter_lists;
16054 if (template_parm_lists_apply)
16055 for (cp_binding_level *s = current_binding_level;
16056 s && s->kind != sk_template_parms;
16057 s = s->level_chain)
16058 if (s->kind == sk_function_parms)
16059 template_parm_lists_apply = false;
16061 /* Look up a qualified name in the usual way. */
16062 if (parser->scope)
16064 tree decl;
16065 tree ambiguous_decls;
16067 decl = cp_parser_lookup_name (parser, identifier,
16068 tag_type,
16069 /*is_template=*/false,
16070 /*is_namespace=*/false,
16071 /*check_dependency=*/true,
16072 &ambiguous_decls,
16073 token->location);
16075 /* If the lookup was ambiguous, an error will already have been
16076 issued. */
16077 if (ambiguous_decls)
16078 return error_mark_node;
16080 /* If we are parsing friend declaration, DECL may be a
16081 TEMPLATE_DECL tree node here. However, we need to check
16082 whether this TEMPLATE_DECL results in valid code. Consider
16083 the following example:
16085 namespace N {
16086 template <class T> class C {};
16088 class X {
16089 template <class T> friend class N::C; // #1, valid code
16091 template <class T> class Y {
16092 friend class N::C; // #2, invalid code
16095 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16096 name lookup of `N::C'. We see that friend declaration must
16097 be template for the code to be valid. Note that
16098 processing_template_decl does not work here since it is
16099 always 1 for the above two cases. */
16101 decl = (cp_parser_maybe_treat_template_as_class
16102 (decl, /*tag_name_p=*/is_friend
16103 && template_parm_lists_apply));
16105 if (TREE_CODE (decl) != TYPE_DECL)
16107 cp_parser_diagnose_invalid_type_name (parser,
16108 identifier,
16109 token->location);
16110 return error_mark_node;
16113 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16115 bool allow_template = (template_parm_lists_apply
16116 || DECL_SELF_REFERENCE_P (decl));
16117 type = check_elaborated_type_specifier (tag_type, decl,
16118 allow_template);
16120 if (type == error_mark_node)
16121 return error_mark_node;
16124 /* Forward declarations of nested types, such as
16126 class C1::C2;
16127 class C1::C2::C3;
16129 are invalid unless all components preceding the final '::'
16130 are complete. If all enclosing types are complete, these
16131 declarations become merely pointless.
16133 Invalid forward declarations of nested types are errors
16134 caught elsewhere in parsing. Those that are pointless arrive
16135 here. */
16137 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16138 && !is_friend && !processing_explicit_instantiation)
16139 warning (0, "declaration %qD does not declare anything", decl);
16141 type = TREE_TYPE (decl);
16143 else
16145 /* An elaborated-type-specifier sometimes introduces a new type and
16146 sometimes names an existing type. Normally, the rule is that it
16147 introduces a new type only if there is not an existing type of
16148 the same name already in scope. For example, given:
16150 struct S {};
16151 void f() { struct S s; }
16153 the `struct S' in the body of `f' is the same `struct S' as in
16154 the global scope; the existing definition is used. However, if
16155 there were no global declaration, this would introduce a new
16156 local class named `S'.
16158 An exception to this rule applies to the following code:
16160 namespace N { struct S; }
16162 Here, the elaborated-type-specifier names a new type
16163 unconditionally; even if there is already an `S' in the
16164 containing scope this declaration names a new type.
16165 This exception only applies if the elaborated-type-specifier
16166 forms the complete declaration:
16168 [class.name]
16170 A declaration consisting solely of `class-key identifier ;' is
16171 either a redeclaration of the name in the current scope or a
16172 forward declaration of the identifier as a class name. It
16173 introduces the name into the current scope.
16175 We are in this situation precisely when the next token is a `;'.
16177 An exception to the exception is that a `friend' declaration does
16178 *not* name a new type; i.e., given:
16180 struct S { friend struct T; };
16182 `T' is not a new type in the scope of `S'.
16184 Also, `new struct S' or `sizeof (struct S)' never results in the
16185 definition of a new type; a new type can only be declared in a
16186 declaration context. */
16188 tag_scope ts;
16189 bool template_p;
16191 if (is_friend)
16192 /* Friends have special name lookup rules. */
16193 ts = ts_within_enclosing_non_class;
16194 else if (is_declaration
16195 && cp_lexer_next_token_is (parser->lexer,
16196 CPP_SEMICOLON))
16197 /* This is a `class-key identifier ;' */
16198 ts = ts_current;
16199 else
16200 ts = ts_global;
16202 template_p =
16203 (template_parm_lists_apply
16204 && (cp_parser_next_token_starts_class_definition_p (parser)
16205 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16206 /* An unqualified name was used to reference this type, so
16207 there were no qualifying templates. */
16208 if (template_parm_lists_apply
16209 && !cp_parser_check_template_parameters (parser,
16210 /*num_templates=*/0,
16211 token->location,
16212 /*declarator=*/NULL))
16213 return error_mark_node;
16214 type = xref_tag (tag_type, identifier, ts, template_p);
16218 if (type == error_mark_node)
16219 return error_mark_node;
16221 /* Allow attributes on forward declarations of classes. */
16222 if (attributes)
16224 if (TREE_CODE (type) == TYPENAME_TYPE)
16225 warning (OPT_Wattributes,
16226 "attributes ignored on uninstantiated type");
16227 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16228 && ! processing_explicit_instantiation)
16229 warning (OPT_Wattributes,
16230 "attributes ignored on template instantiation");
16231 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16232 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16233 else
16234 warning (OPT_Wattributes,
16235 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16238 if (tag_type != enum_type)
16240 /* Indicate whether this class was declared as a `class' or as a
16241 `struct'. */
16242 if (TREE_CODE (type) == RECORD_TYPE)
16243 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16244 cp_parser_check_class_key (tag_type, type);
16247 /* A "<" cannot follow an elaborated type specifier. If that
16248 happens, the user was probably trying to form a template-id. */
16249 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16250 token->location);
16252 return type;
16255 /* Parse an enum-specifier.
16257 enum-specifier:
16258 enum-head { enumerator-list [opt] }
16259 enum-head { enumerator-list , } [C++0x]
16261 enum-head:
16262 enum-key identifier [opt] enum-base [opt]
16263 enum-key nested-name-specifier identifier enum-base [opt]
16265 enum-key:
16266 enum
16267 enum class [C++0x]
16268 enum struct [C++0x]
16270 enum-base: [C++0x]
16271 : type-specifier-seq
16273 opaque-enum-specifier:
16274 enum-key identifier enum-base [opt] ;
16276 GNU Extensions:
16277 enum-key attributes[opt] identifier [opt] enum-base [opt]
16278 { enumerator-list [opt] }attributes[opt]
16279 enum-key attributes[opt] identifier [opt] enum-base [opt]
16280 { enumerator-list, }attributes[opt] [C++0x]
16282 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16283 if the token stream isn't an enum-specifier after all. */
16285 static tree
16286 cp_parser_enum_specifier (cp_parser* parser)
16288 tree identifier;
16289 tree type = NULL_TREE;
16290 tree prev_scope;
16291 tree nested_name_specifier = NULL_TREE;
16292 tree attributes;
16293 bool scoped_enum_p = false;
16294 bool has_underlying_type = false;
16295 bool nested_being_defined = false;
16296 bool new_value_list = false;
16297 bool is_new_type = false;
16298 bool is_anonymous = false;
16299 tree underlying_type = NULL_TREE;
16300 cp_token *type_start_token = NULL;
16301 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16303 parser->colon_corrects_to_scope_p = false;
16305 /* Parse tentatively so that we can back up if we don't find a
16306 enum-specifier. */
16307 cp_parser_parse_tentatively (parser);
16309 /* Caller guarantees that the current token is 'enum', an identifier
16310 possibly follows, and the token after that is an opening brace.
16311 If we don't have an identifier, fabricate an anonymous name for
16312 the enumeration being defined. */
16313 cp_lexer_consume_token (parser->lexer);
16315 /* Parse the "class" or "struct", which indicates a scoped
16316 enumeration type in C++0x. */
16317 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16318 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16320 if (cxx_dialect < cxx11)
16321 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16323 /* Consume the `struct' or `class' token. */
16324 cp_lexer_consume_token (parser->lexer);
16326 scoped_enum_p = true;
16329 attributes = cp_parser_attributes_opt (parser);
16331 /* Clear the qualification. */
16332 parser->scope = NULL_TREE;
16333 parser->qualifying_scope = NULL_TREE;
16334 parser->object_scope = NULL_TREE;
16336 /* Figure out in what scope the declaration is being placed. */
16337 prev_scope = current_scope ();
16339 type_start_token = cp_lexer_peek_token (parser->lexer);
16341 push_deferring_access_checks (dk_no_check);
16342 nested_name_specifier
16343 = cp_parser_nested_name_specifier_opt (parser,
16344 /*typename_keyword_p=*/true,
16345 /*check_dependency_p=*/false,
16346 /*type_p=*/false,
16347 /*is_declaration=*/false);
16349 if (nested_name_specifier)
16351 tree name;
16353 identifier = cp_parser_identifier (parser);
16354 name = cp_parser_lookup_name (parser, identifier,
16355 enum_type,
16356 /*is_template=*/false,
16357 /*is_namespace=*/false,
16358 /*check_dependency=*/true,
16359 /*ambiguous_decls=*/NULL,
16360 input_location);
16361 if (name && name != error_mark_node)
16363 type = TREE_TYPE (name);
16364 if (TREE_CODE (type) == TYPENAME_TYPE)
16366 /* Are template enums allowed in ISO? */
16367 if (template_parm_scope_p ())
16368 pedwarn (type_start_token->location, OPT_Wpedantic,
16369 "%qD is an enumeration template", name);
16370 /* ignore a typename reference, for it will be solved by name
16371 in start_enum. */
16372 type = NULL_TREE;
16375 else if (nested_name_specifier == error_mark_node)
16376 /* We already issued an error. */;
16377 else
16378 error_at (type_start_token->location,
16379 "%qD is not an enumerator-name", identifier);
16381 else
16383 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16384 identifier = cp_parser_identifier (parser);
16385 else
16387 identifier = make_anon_name ();
16388 is_anonymous = true;
16389 if (scoped_enum_p)
16390 error_at (type_start_token->location,
16391 "anonymous scoped enum is not allowed");
16394 pop_deferring_access_checks ();
16396 /* Check for the `:' that denotes a specified underlying type in C++0x.
16397 Note that a ':' could also indicate a bitfield width, however. */
16398 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16400 cp_decl_specifier_seq type_specifiers;
16402 /* Consume the `:'. */
16403 cp_lexer_consume_token (parser->lexer);
16405 /* Parse the type-specifier-seq. */
16406 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16407 /*is_trailing_return=*/false,
16408 &type_specifiers);
16410 /* At this point this is surely not elaborated type specifier. */
16411 if (!cp_parser_parse_definitely (parser))
16412 return NULL_TREE;
16414 if (cxx_dialect < cxx11)
16415 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16417 has_underlying_type = true;
16419 /* If that didn't work, stop. */
16420 if (type_specifiers.type != error_mark_node)
16422 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
16423 /*initialized=*/0, NULL);
16424 if (underlying_type == error_mark_node
16425 || check_for_bare_parameter_packs (underlying_type))
16426 underlying_type = NULL_TREE;
16430 /* Look for the `{' but don't consume it yet. */
16431 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16433 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
16435 cp_parser_error (parser, "expected %<{%>");
16436 if (has_underlying_type)
16438 type = NULL_TREE;
16439 goto out;
16442 /* An opaque-enum-specifier must have a ';' here. */
16443 if ((scoped_enum_p || underlying_type)
16444 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16446 cp_parser_error (parser, "expected %<;%> or %<{%>");
16447 if (has_underlying_type)
16449 type = NULL_TREE;
16450 goto out;
16455 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
16456 return NULL_TREE;
16458 if (nested_name_specifier)
16460 if (CLASS_TYPE_P (nested_name_specifier))
16462 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
16463 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
16464 push_scope (nested_name_specifier);
16466 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16468 push_nested_namespace (nested_name_specifier);
16472 /* Issue an error message if type-definitions are forbidden here. */
16473 if (!cp_parser_check_type_definition (parser))
16474 type = error_mark_node;
16475 else
16476 /* Create the new type. We do this before consuming the opening
16477 brace so the enum will be recorded as being on the line of its
16478 tag (or the 'enum' keyword, if there is no tag). */
16479 type = start_enum (identifier, type, underlying_type,
16480 scoped_enum_p, &is_new_type);
16482 /* If the next token is not '{' it is an opaque-enum-specifier or an
16483 elaborated-type-specifier. */
16484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16486 timevar_push (TV_PARSE_ENUM);
16487 if (nested_name_specifier
16488 && nested_name_specifier != error_mark_node)
16490 /* The following catches invalid code such as:
16491 enum class S<int>::E { A, B, C }; */
16492 if (!processing_specialization
16493 && CLASS_TYPE_P (nested_name_specifier)
16494 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
16495 error_at (type_start_token->location, "cannot add an enumerator "
16496 "list to a template instantiation");
16498 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
16500 error_at (type_start_token->location,
16501 "%<%T::%E%> has not been declared",
16502 TYPE_CONTEXT (nested_name_specifier),
16503 nested_name_specifier);
16504 type = error_mark_node;
16506 /* If that scope does not contain the scope in which the
16507 class was originally declared, the program is invalid. */
16508 else if (prev_scope && !is_ancestor (prev_scope,
16509 nested_name_specifier))
16511 if (at_namespace_scope_p ())
16512 error_at (type_start_token->location,
16513 "declaration of %qD in namespace %qD which does not "
16514 "enclose %qD",
16515 type, prev_scope, nested_name_specifier);
16516 else
16517 error_at (type_start_token->location,
16518 "declaration of %qD in %qD which does not "
16519 "enclose %qD",
16520 type, prev_scope, nested_name_specifier);
16521 type = error_mark_node;
16525 if (scoped_enum_p)
16526 begin_scope (sk_scoped_enum, type);
16528 /* Consume the opening brace. */
16529 cp_lexer_consume_token (parser->lexer);
16531 if (type == error_mark_node)
16532 ; /* Nothing to add */
16533 else if (OPAQUE_ENUM_P (type)
16534 || (cxx_dialect > cxx98 && processing_specialization))
16536 new_value_list = true;
16537 SET_OPAQUE_ENUM_P (type, false);
16538 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16540 else
16542 error_at (type_start_token->location,
16543 "multiple definition of %q#T", type);
16544 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16545 "previous definition here");
16546 type = error_mark_node;
16549 if (type == error_mark_node)
16550 cp_parser_skip_to_end_of_block_or_statement (parser);
16551 /* If the next token is not '}', then there are some enumerators. */
16552 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16554 if (is_anonymous && !scoped_enum_p)
16555 pedwarn (type_start_token->location, OPT_Wpedantic,
16556 "ISO C++ forbids empty anonymous enum");
16558 else
16559 cp_parser_enumerator_list (parser, type);
16561 /* Consume the final '}'. */
16562 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16564 if (scoped_enum_p)
16565 finish_scope ();
16566 timevar_pop (TV_PARSE_ENUM);
16568 else
16570 /* If a ';' follows, then it is an opaque-enum-specifier
16571 and additional restrictions apply. */
16572 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16574 if (is_anonymous)
16575 error_at (type_start_token->location,
16576 "opaque-enum-specifier without name");
16577 else if (nested_name_specifier)
16578 error_at (type_start_token->location,
16579 "opaque-enum-specifier must use a simple identifier");
16583 /* Look for trailing attributes to apply to this enumeration, and
16584 apply them if appropriate. */
16585 if (cp_parser_allow_gnu_extensions_p (parser))
16587 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16588 trailing_attr = chainon (trailing_attr, attributes);
16589 cplus_decl_attributes (&type,
16590 trailing_attr,
16591 (int) ATTR_FLAG_TYPE_IN_PLACE);
16594 /* Finish up the enumeration. */
16595 if (type != error_mark_node)
16597 if (new_value_list)
16598 finish_enum_value_list (type);
16599 if (is_new_type)
16600 finish_enum (type);
16603 if (nested_name_specifier)
16605 if (CLASS_TYPE_P (nested_name_specifier))
16607 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16608 pop_scope (nested_name_specifier);
16610 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16612 pop_nested_namespace (nested_name_specifier);
16615 out:
16616 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16617 return type;
16620 /* Parse an enumerator-list. The enumerators all have the indicated
16621 TYPE.
16623 enumerator-list:
16624 enumerator-definition
16625 enumerator-list , enumerator-definition */
16627 static void
16628 cp_parser_enumerator_list (cp_parser* parser, tree type)
16630 while (true)
16632 /* Parse an enumerator-definition. */
16633 cp_parser_enumerator_definition (parser, type);
16635 /* If the next token is not a ',', we've reached the end of
16636 the list. */
16637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16638 break;
16639 /* Otherwise, consume the `,' and keep going. */
16640 cp_lexer_consume_token (parser->lexer);
16641 /* If the next token is a `}', there is a trailing comma. */
16642 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16644 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16645 pedwarn (input_location, OPT_Wpedantic,
16646 "comma at end of enumerator list");
16647 break;
16652 /* Parse an enumerator-definition. The enumerator has the indicated
16653 TYPE.
16655 enumerator-definition:
16656 enumerator
16657 enumerator = constant-expression
16659 enumerator:
16660 identifier */
16662 static void
16663 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16665 tree identifier;
16666 tree value;
16667 location_t loc;
16669 /* Save the input location because we are interested in the location
16670 of the identifier and not the location of the explicit value. */
16671 loc = cp_lexer_peek_token (parser->lexer)->location;
16673 /* Look for the identifier. */
16674 identifier = cp_parser_identifier (parser);
16675 if (identifier == error_mark_node)
16676 return;
16678 /* If the next token is an '=', then there is an explicit value. */
16679 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16681 /* Consume the `=' token. */
16682 cp_lexer_consume_token (parser->lexer);
16683 /* Parse the value. */
16684 value = cp_parser_constant_expression (parser);
16686 else
16687 value = NULL_TREE;
16689 /* If we are processing a template, make sure the initializer of the
16690 enumerator doesn't contain any bare template parameter pack. */
16691 if (check_for_bare_parameter_packs (value))
16692 value = error_mark_node;
16694 /* Create the enumerator. */
16695 build_enumerator (identifier, value, type, loc);
16698 /* Parse a namespace-name.
16700 namespace-name:
16701 original-namespace-name
16702 namespace-alias
16704 Returns the NAMESPACE_DECL for the namespace. */
16706 static tree
16707 cp_parser_namespace_name (cp_parser* parser)
16709 tree identifier;
16710 tree namespace_decl;
16712 cp_token *token = cp_lexer_peek_token (parser->lexer);
16714 /* Get the name of the namespace. */
16715 identifier = cp_parser_identifier (parser);
16716 if (identifier == error_mark_node)
16717 return error_mark_node;
16719 /* Look up the identifier in the currently active scope. Look only
16720 for namespaces, due to:
16722 [basic.lookup.udir]
16724 When looking up a namespace-name in a using-directive or alias
16725 definition, only namespace names are considered.
16727 And:
16729 [basic.lookup.qual]
16731 During the lookup of a name preceding the :: scope resolution
16732 operator, object, function, and enumerator names are ignored.
16734 (Note that cp_parser_qualifying_entity only calls this
16735 function if the token after the name is the scope resolution
16736 operator.) */
16737 namespace_decl = cp_parser_lookup_name (parser, identifier,
16738 none_type,
16739 /*is_template=*/false,
16740 /*is_namespace=*/true,
16741 /*check_dependency=*/true,
16742 /*ambiguous_decls=*/NULL,
16743 token->location);
16744 /* If it's not a namespace, issue an error. */
16745 if (namespace_decl == error_mark_node
16746 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16748 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16749 error_at (token->location, "%qD is not a namespace-name", identifier);
16750 cp_parser_error (parser, "expected namespace-name");
16751 namespace_decl = error_mark_node;
16754 return namespace_decl;
16757 /* Parse a namespace-definition.
16759 namespace-definition:
16760 named-namespace-definition
16761 unnamed-namespace-definition
16763 named-namespace-definition:
16764 original-namespace-definition
16765 extension-namespace-definition
16767 original-namespace-definition:
16768 namespace identifier { namespace-body }
16770 extension-namespace-definition:
16771 namespace original-namespace-name { namespace-body }
16773 unnamed-namespace-definition:
16774 namespace { namespace-body } */
16776 static void
16777 cp_parser_namespace_definition (cp_parser* parser)
16779 tree identifier, attribs;
16780 bool has_visibility;
16781 bool is_inline;
16783 cp_ensure_no_omp_declare_simd (parser);
16784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16786 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16787 is_inline = true;
16788 cp_lexer_consume_token (parser->lexer);
16790 else
16791 is_inline = false;
16793 /* Look for the `namespace' keyword. */
16794 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16796 /* Get the name of the namespace. We do not attempt to distinguish
16797 between an original-namespace-definition and an
16798 extension-namespace-definition at this point. The semantic
16799 analysis routines are responsible for that. */
16800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16801 identifier = cp_parser_identifier (parser);
16802 else
16803 identifier = NULL_TREE;
16805 /* Parse any specified attributes. */
16806 attribs = cp_parser_attributes_opt (parser);
16808 /* Look for the `{' to start the namespace. */
16809 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16810 /* Start the namespace. */
16811 push_namespace (identifier);
16813 /* "inline namespace" is equivalent to a stub namespace definition
16814 followed by a strong using directive. */
16815 if (is_inline)
16817 tree name_space = current_namespace;
16818 /* Set up namespace association. */
16819 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16820 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16821 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16822 /* Import the contents of the inline namespace. */
16823 pop_namespace ();
16824 do_using_directive (name_space);
16825 push_namespace (identifier);
16828 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16830 /* Parse the body of the namespace. */
16831 cp_parser_namespace_body (parser);
16833 if (has_visibility)
16834 pop_visibility (1);
16836 /* Finish the namespace. */
16837 pop_namespace ();
16838 /* Look for the final `}'. */
16839 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16842 /* Parse a namespace-body.
16844 namespace-body:
16845 declaration-seq [opt] */
16847 static void
16848 cp_parser_namespace_body (cp_parser* parser)
16850 cp_parser_declaration_seq_opt (parser);
16853 /* Parse a namespace-alias-definition.
16855 namespace-alias-definition:
16856 namespace identifier = qualified-namespace-specifier ; */
16858 static void
16859 cp_parser_namespace_alias_definition (cp_parser* parser)
16861 tree identifier;
16862 tree namespace_specifier;
16864 cp_token *token = cp_lexer_peek_token (parser->lexer);
16866 /* Look for the `namespace' keyword. */
16867 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16868 /* Look for the identifier. */
16869 identifier = cp_parser_identifier (parser);
16870 if (identifier == error_mark_node)
16871 return;
16872 /* Look for the `=' token. */
16873 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16874 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16876 error_at (token->location, "%<namespace%> definition is not allowed here");
16877 /* Skip the definition. */
16878 cp_lexer_consume_token (parser->lexer);
16879 if (cp_parser_skip_to_closing_brace (parser))
16880 cp_lexer_consume_token (parser->lexer);
16881 return;
16883 cp_parser_require (parser, CPP_EQ, RT_EQ);
16884 /* Look for the qualified-namespace-specifier. */
16885 namespace_specifier
16886 = cp_parser_qualified_namespace_specifier (parser);
16887 /* Look for the `;' token. */
16888 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16890 /* Register the alias in the symbol table. */
16891 do_namespace_alias (identifier, namespace_specifier);
16894 /* Parse a qualified-namespace-specifier.
16896 qualified-namespace-specifier:
16897 :: [opt] nested-name-specifier [opt] namespace-name
16899 Returns a NAMESPACE_DECL corresponding to the specified
16900 namespace. */
16902 static tree
16903 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16905 /* Look for the optional `::'. */
16906 cp_parser_global_scope_opt (parser,
16907 /*current_scope_valid_p=*/false);
16909 /* Look for the optional nested-name-specifier. */
16910 cp_parser_nested_name_specifier_opt (parser,
16911 /*typename_keyword_p=*/false,
16912 /*check_dependency_p=*/true,
16913 /*type_p=*/false,
16914 /*is_declaration=*/true);
16916 return cp_parser_namespace_name (parser);
16919 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16920 access declaration.
16922 using-declaration:
16923 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16924 using :: unqualified-id ;
16926 access-declaration:
16927 qualified-id ;
16931 static bool
16932 cp_parser_using_declaration (cp_parser* parser,
16933 bool access_declaration_p)
16935 cp_token *token;
16936 bool typename_p = false;
16937 bool global_scope_p;
16938 tree decl;
16939 tree identifier;
16940 tree qscope;
16941 int oldcount = errorcount;
16942 cp_token *diag_token = NULL;
16944 if (access_declaration_p)
16946 diag_token = cp_lexer_peek_token (parser->lexer);
16947 cp_parser_parse_tentatively (parser);
16949 else
16951 /* Look for the `using' keyword. */
16952 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16954 /* Peek at the next token. */
16955 token = cp_lexer_peek_token (parser->lexer);
16956 /* See if it's `typename'. */
16957 if (token->keyword == RID_TYPENAME)
16959 /* Remember that we've seen it. */
16960 typename_p = true;
16961 /* Consume the `typename' token. */
16962 cp_lexer_consume_token (parser->lexer);
16966 /* Look for the optional global scope qualification. */
16967 global_scope_p
16968 = (cp_parser_global_scope_opt (parser,
16969 /*current_scope_valid_p=*/false)
16970 != NULL_TREE);
16972 /* If we saw `typename', or didn't see `::', then there must be a
16973 nested-name-specifier present. */
16974 if (typename_p || !global_scope_p)
16976 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16977 /*check_dependency_p=*/true,
16978 /*type_p=*/false,
16979 /*is_declaration=*/true);
16980 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16982 cp_parser_skip_to_end_of_block_or_statement (parser);
16983 return false;
16986 /* Otherwise, we could be in either of the two productions. In that
16987 case, treat the nested-name-specifier as optional. */
16988 else
16989 qscope = cp_parser_nested_name_specifier_opt (parser,
16990 /*typename_keyword_p=*/false,
16991 /*check_dependency_p=*/true,
16992 /*type_p=*/false,
16993 /*is_declaration=*/true);
16994 if (!qscope)
16995 qscope = global_namespace;
16996 else if (UNSCOPED_ENUM_P (qscope))
16997 qscope = CP_TYPE_CONTEXT (qscope);
16999 if (access_declaration_p && cp_parser_error_occurred (parser))
17000 /* Something has already gone wrong; there's no need to parse
17001 further. Since an error has occurred, the return value of
17002 cp_parser_parse_definitely will be false, as required. */
17003 return cp_parser_parse_definitely (parser);
17005 token = cp_lexer_peek_token (parser->lexer);
17006 /* Parse the unqualified-id. */
17007 identifier = cp_parser_unqualified_id (parser,
17008 /*template_keyword_p=*/false,
17009 /*check_dependency_p=*/true,
17010 /*declarator_p=*/true,
17011 /*optional_p=*/false);
17013 if (access_declaration_p)
17015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17016 cp_parser_simulate_error (parser);
17017 if (!cp_parser_parse_definitely (parser))
17018 return false;
17021 /* The function we call to handle a using-declaration is different
17022 depending on what scope we are in. */
17023 if (qscope == error_mark_node || identifier == error_mark_node)
17025 else if (!identifier_p (identifier)
17026 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17027 /* [namespace.udecl]
17029 A using declaration shall not name a template-id. */
17030 error_at (token->location,
17031 "a template-id may not appear in a using-declaration");
17032 else
17034 if (at_class_scope_p ())
17036 /* Create the USING_DECL. */
17037 decl = do_class_using_decl (parser->scope, identifier);
17039 if (decl && typename_p)
17040 USING_DECL_TYPENAME_P (decl) = 1;
17042 if (check_for_bare_parameter_packs (decl))
17044 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17045 return false;
17047 else
17048 /* Add it to the list of members in this class. */
17049 finish_member_declaration (decl);
17051 else
17053 decl = cp_parser_lookup_name_simple (parser,
17054 identifier,
17055 token->location);
17056 if (decl == error_mark_node)
17057 cp_parser_name_lookup_error (parser, identifier,
17058 decl, NLE_NULL,
17059 token->location);
17060 else if (check_for_bare_parameter_packs (decl))
17062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17063 return false;
17065 else if (!at_namespace_scope_p ())
17066 do_local_using_decl (decl, qscope, identifier);
17067 else
17068 do_toplevel_using_decl (decl, qscope, identifier);
17072 /* Look for the final `;'. */
17073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17075 if (access_declaration_p && errorcount == oldcount)
17076 warning_at (diag_token->location, OPT_Wdeprecated,
17077 "access declarations are deprecated "
17078 "in favour of using-declarations; "
17079 "suggestion: add the %<using%> keyword");
17081 return true;
17084 /* Parse an alias-declaration.
17086 alias-declaration:
17087 using identifier attribute-specifier-seq [opt] = type-id */
17089 static tree
17090 cp_parser_alias_declaration (cp_parser* parser)
17092 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17093 location_t id_location;
17094 cp_declarator *declarator;
17095 cp_decl_specifier_seq decl_specs;
17096 bool member_p;
17097 const char *saved_message = NULL;
17099 /* Look for the `using' keyword. */
17100 cp_token *using_token
17101 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17102 if (using_token == NULL)
17103 return error_mark_node;
17105 id_location = cp_lexer_peek_token (parser->lexer)->location;
17106 id = cp_parser_identifier (parser);
17107 if (id == error_mark_node)
17108 return error_mark_node;
17110 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17111 attributes = cp_parser_attributes_opt (parser);
17112 if (attributes == error_mark_node)
17113 return error_mark_node;
17115 cp_parser_require (parser, CPP_EQ, RT_EQ);
17117 if (cp_parser_error_occurred (parser))
17118 return error_mark_node;
17120 cp_parser_commit_to_tentative_parse (parser);
17122 /* Now we are going to parse the type-id of the declaration. */
17125 [dcl.type]/3 says:
17127 "A type-specifier-seq shall not define a class or enumeration
17128 unless it appears in the type-id of an alias-declaration (7.1.3) that
17129 is not the declaration of a template-declaration."
17131 In other words, if we currently are in an alias template, the
17132 type-id should not define a type.
17134 So let's set parser->type_definition_forbidden_message in that
17135 case; cp_parser_check_type_definition (called by
17136 cp_parser_class_specifier) will then emit an error if a type is
17137 defined in the type-id. */
17138 if (parser->num_template_parameter_lists)
17140 saved_message = parser->type_definition_forbidden_message;
17141 parser->type_definition_forbidden_message =
17142 G_("types may not be defined in alias template declarations");
17145 type = cp_parser_type_id (parser);
17147 /* Restore the error message if need be. */
17148 if (parser->num_template_parameter_lists)
17149 parser->type_definition_forbidden_message = saved_message;
17151 if (type == error_mark_node
17152 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17154 cp_parser_skip_to_end_of_block_or_statement (parser);
17155 return error_mark_node;
17158 /* A typedef-name can also be introduced by an alias-declaration. The
17159 identifier following the using keyword becomes a typedef-name. It has
17160 the same semantics as if it were introduced by the typedef
17161 specifier. In particular, it does not define a new type and it shall
17162 not appear in the type-id. */
17164 clear_decl_specs (&decl_specs);
17165 decl_specs.type = type;
17166 if (attributes != NULL_TREE)
17168 decl_specs.attributes = attributes;
17169 set_and_check_decl_spec_loc (&decl_specs,
17170 ds_attribute,
17171 attrs_token);
17173 set_and_check_decl_spec_loc (&decl_specs,
17174 ds_typedef,
17175 using_token);
17176 set_and_check_decl_spec_loc (&decl_specs,
17177 ds_alias,
17178 using_token);
17180 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17181 declarator->id_loc = id_location;
17183 member_p = at_class_scope_p ();
17184 if (member_p)
17185 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17186 NULL_TREE, attributes);
17187 else
17188 decl = start_decl (declarator, &decl_specs, 0,
17189 attributes, NULL_TREE, &pushed_scope);
17190 if (decl == error_mark_node)
17191 return decl;
17193 // Attach constraints to the alias declaration.
17194 if (flag_concepts && current_template_parms)
17196 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17197 tree constr = build_constraints (reqs, NULL_TREE);
17198 set_constraints (decl, constr);
17201 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17203 if (pushed_scope)
17204 pop_scope (pushed_scope);
17206 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17207 added into the symbol table; otherwise, return the TYPE_DECL. */
17208 if (DECL_LANG_SPECIFIC (decl)
17209 && DECL_TEMPLATE_INFO (decl)
17210 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17212 decl = DECL_TI_TEMPLATE (decl);
17213 if (member_p)
17214 check_member_template (decl);
17217 return decl;
17220 /* Parse a using-directive.
17222 using-directive:
17223 using namespace :: [opt] nested-name-specifier [opt]
17224 namespace-name ; */
17226 static void
17227 cp_parser_using_directive (cp_parser* parser)
17229 tree namespace_decl;
17230 tree attribs;
17232 /* Look for the `using' keyword. */
17233 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17234 /* And the `namespace' keyword. */
17235 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17236 /* Look for the optional `::' operator. */
17237 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17238 /* And the optional nested-name-specifier. */
17239 cp_parser_nested_name_specifier_opt (parser,
17240 /*typename_keyword_p=*/false,
17241 /*check_dependency_p=*/true,
17242 /*type_p=*/false,
17243 /*is_declaration=*/true);
17244 /* Get the namespace being used. */
17245 namespace_decl = cp_parser_namespace_name (parser);
17246 /* And any specified attributes. */
17247 attribs = cp_parser_attributes_opt (parser);
17248 /* Update the symbol table. */
17249 parse_using_directive (namespace_decl, attribs);
17250 /* Look for the final `;'. */
17251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17254 /* Parse an asm-definition.
17256 asm-definition:
17257 asm ( string-literal ) ;
17259 GNU Extension:
17261 asm-definition:
17262 asm volatile [opt] ( string-literal ) ;
17263 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
17264 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17265 : asm-operand-list [opt] ) ;
17266 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17267 : asm-operand-list [opt]
17268 : asm-clobber-list [opt] ) ;
17269 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
17270 : asm-clobber-list [opt]
17271 : asm-goto-list ) ; */
17273 static void
17274 cp_parser_asm_definition (cp_parser* parser)
17276 tree string;
17277 tree outputs = NULL_TREE;
17278 tree inputs = NULL_TREE;
17279 tree clobbers = NULL_TREE;
17280 tree labels = NULL_TREE;
17281 tree asm_stmt;
17282 bool volatile_p = false;
17283 bool extended_p = false;
17284 bool invalid_inputs_p = false;
17285 bool invalid_outputs_p = false;
17286 bool goto_p = false;
17287 required_token missing = RT_NONE;
17289 /* Look for the `asm' keyword. */
17290 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
17292 if (parser->in_function_body
17293 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
17295 error ("%<asm%> in %<constexpr%> function");
17296 cp_function_chain->invalid_constexpr = true;
17299 /* See if the next token is `volatile'. */
17300 if (cp_parser_allow_gnu_extensions_p (parser)
17301 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
17303 /* Remember that we saw the `volatile' keyword. */
17304 volatile_p = true;
17305 /* Consume the token. */
17306 cp_lexer_consume_token (parser->lexer);
17308 if (cp_parser_allow_gnu_extensions_p (parser)
17309 && parser->in_function_body
17310 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
17312 /* Remember that we saw the `goto' keyword. */
17313 goto_p = true;
17314 /* Consume the token. */
17315 cp_lexer_consume_token (parser->lexer);
17317 /* Look for the opening `('. */
17318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
17319 return;
17320 /* Look for the string. */
17321 string = cp_parser_string_literal (parser, false, false);
17322 if (string == error_mark_node)
17324 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17325 /*consume_paren=*/true);
17326 return;
17329 /* If we're allowing GNU extensions, check for the extended assembly
17330 syntax. Unfortunately, the `:' tokens need not be separated by
17331 a space in C, and so, for compatibility, we tolerate that here
17332 too. Doing that means that we have to treat the `::' operator as
17333 two `:' tokens. */
17334 if (cp_parser_allow_gnu_extensions_p (parser)
17335 && parser->in_function_body
17336 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
17337 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
17339 bool inputs_p = false;
17340 bool clobbers_p = false;
17341 bool labels_p = false;
17343 /* The extended syntax was used. */
17344 extended_p = true;
17346 /* Look for outputs. */
17347 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17349 /* Consume the `:'. */
17350 cp_lexer_consume_token (parser->lexer);
17351 /* Parse the output-operands. */
17352 if (cp_lexer_next_token_is_not (parser->lexer,
17353 CPP_COLON)
17354 && cp_lexer_next_token_is_not (parser->lexer,
17355 CPP_SCOPE)
17356 && cp_lexer_next_token_is_not (parser->lexer,
17357 CPP_CLOSE_PAREN)
17358 && !goto_p)
17359 outputs = cp_parser_asm_operand_list (parser);
17361 if (outputs == error_mark_node)
17362 invalid_outputs_p = true;
17364 /* If the next token is `::', there are no outputs, and the
17365 next token is the beginning of the inputs. */
17366 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17367 /* The inputs are coming next. */
17368 inputs_p = true;
17370 /* Look for inputs. */
17371 if (inputs_p
17372 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17374 /* Consume the `:' or `::'. */
17375 cp_lexer_consume_token (parser->lexer);
17376 /* Parse the output-operands. */
17377 if (cp_lexer_next_token_is_not (parser->lexer,
17378 CPP_COLON)
17379 && cp_lexer_next_token_is_not (parser->lexer,
17380 CPP_SCOPE)
17381 && cp_lexer_next_token_is_not (parser->lexer,
17382 CPP_CLOSE_PAREN))
17383 inputs = cp_parser_asm_operand_list (parser);
17385 if (inputs == error_mark_node)
17386 invalid_inputs_p = true;
17388 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17389 /* The clobbers are coming next. */
17390 clobbers_p = true;
17392 /* Look for clobbers. */
17393 if (clobbers_p
17394 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17396 clobbers_p = true;
17397 /* Consume the `:' or `::'. */
17398 cp_lexer_consume_token (parser->lexer);
17399 /* Parse the clobbers. */
17400 if (cp_lexer_next_token_is_not (parser->lexer,
17401 CPP_COLON)
17402 && cp_lexer_next_token_is_not (parser->lexer,
17403 CPP_CLOSE_PAREN))
17404 clobbers = cp_parser_asm_clobber_list (parser);
17406 else if (goto_p
17407 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17408 /* The labels are coming next. */
17409 labels_p = true;
17411 /* Look for labels. */
17412 if (labels_p
17413 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
17415 labels_p = true;
17416 /* Consume the `:' or `::'. */
17417 cp_lexer_consume_token (parser->lexer);
17418 /* Parse the labels. */
17419 labels = cp_parser_asm_label_list (parser);
17422 if (goto_p && !labels_p)
17423 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
17425 else if (goto_p)
17426 missing = RT_COLON_SCOPE;
17428 /* Look for the closing `)'. */
17429 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
17430 missing ? missing : RT_CLOSE_PAREN))
17431 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17432 /*consume_paren=*/true);
17433 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17435 if (!invalid_inputs_p && !invalid_outputs_p)
17437 /* Create the ASM_EXPR. */
17438 if (parser->in_function_body)
17440 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
17441 inputs, clobbers, labels);
17442 /* If the extended syntax was not used, mark the ASM_EXPR. */
17443 if (!extended_p)
17445 tree temp = asm_stmt;
17446 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
17447 temp = TREE_OPERAND (temp, 0);
17449 ASM_INPUT_P (temp) = 1;
17452 else
17453 symtab->finalize_toplevel_asm (string);
17457 /* Declarators [gram.dcl.decl] */
17459 /* Parse an init-declarator.
17461 init-declarator:
17462 declarator initializer [opt]
17464 GNU Extension:
17466 init-declarator:
17467 declarator asm-specification [opt] attributes [opt] initializer [opt]
17469 function-definition:
17470 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17471 function-body
17472 decl-specifier-seq [opt] declarator function-try-block
17474 GNU Extension:
17476 function-definition:
17477 __extension__ function-definition
17479 TM Extension:
17481 function-definition:
17482 decl-specifier-seq [opt] declarator function-transaction-block
17484 The DECL_SPECIFIERS apply to this declarator. Returns a
17485 representation of the entity declared. If MEMBER_P is TRUE, then
17486 this declarator appears in a class scope. The new DECL created by
17487 this declarator is returned.
17489 The CHECKS are access checks that should be performed once we know
17490 what entity is being declared (and, therefore, what classes have
17491 befriended it).
17493 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17494 for a function-definition here as well. If the declarator is a
17495 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17496 be TRUE upon return. By that point, the function-definition will
17497 have been completely parsed.
17499 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17500 is FALSE.
17502 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17503 parsed declaration if it is an uninitialized single declarator not followed
17504 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17505 if present, will not be consumed. If returned, this declarator will be
17506 created with SD_INITIALIZED but will not call cp_finish_decl.
17508 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
17509 and there is an initializer, the pointed location_t is set to the
17510 location of the '=' or `(', or '{' in C++11 token introducing the
17511 initializer. */
17513 static tree
17514 cp_parser_init_declarator (cp_parser* parser,
17515 cp_decl_specifier_seq *decl_specifiers,
17516 vec<deferred_access_check, va_gc> *checks,
17517 bool function_definition_allowed_p,
17518 bool member_p,
17519 int declares_class_or_enum,
17520 bool* function_definition_p,
17521 tree* maybe_range_for_decl,
17522 location_t* init_loc)
17524 cp_token *token = NULL, *asm_spec_start_token = NULL,
17525 *attributes_start_token = NULL;
17526 cp_declarator *declarator;
17527 tree prefix_attributes;
17528 tree attributes = NULL;
17529 tree asm_specification;
17530 tree initializer;
17531 tree decl = NULL_TREE;
17532 tree scope;
17533 int is_initialized;
17534 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17535 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17536 "(...)". */
17537 enum cpp_ttype initialization_kind;
17538 bool is_direct_init = false;
17539 bool is_non_constant_init;
17540 int ctor_dtor_or_conv_p;
17541 bool friend_p = cp_parser_friend_p (decl_specifiers);
17542 tree pushed_scope = NULL_TREE;
17543 bool range_for_decl_p = false;
17544 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17545 location_t tmp_init_loc = UNKNOWN_LOCATION;
17547 /* Gather the attributes that were provided with the
17548 decl-specifiers. */
17549 prefix_attributes = decl_specifiers->attributes;
17551 /* Assume that this is not the declarator for a function
17552 definition. */
17553 if (function_definition_p)
17554 *function_definition_p = false;
17556 /* Default arguments are only permitted for function parameters. */
17557 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17558 parser->default_arg_ok_p = false;
17560 /* Defer access checks while parsing the declarator; we cannot know
17561 what names are accessible until we know what is being
17562 declared. */
17563 resume_deferring_access_checks ();
17565 /* Parse the declarator. */
17566 token = cp_lexer_peek_token (parser->lexer);
17567 declarator
17568 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17569 &ctor_dtor_or_conv_p,
17570 /*parenthesized_p=*/NULL,
17571 member_p, friend_p);
17572 /* Gather up the deferred checks. */
17573 stop_deferring_access_checks ();
17575 parser->default_arg_ok_p = saved_default_arg_ok_p;
17577 /* If the DECLARATOR was erroneous, there's no need to go
17578 further. */
17579 if (declarator == cp_error_declarator)
17580 return error_mark_node;
17582 /* Check that the number of template-parameter-lists is OK. */
17583 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17584 token->location))
17585 return error_mark_node;
17587 if (declares_class_or_enum & 2)
17588 cp_parser_check_for_definition_in_return_type (declarator,
17589 decl_specifiers->type,
17590 decl_specifiers->locations[ds_type_spec]);
17592 /* Figure out what scope the entity declared by the DECLARATOR is
17593 located in. `grokdeclarator' sometimes changes the scope, so
17594 we compute it now. */
17595 scope = get_scope_of_declarator (declarator);
17597 /* Perform any lookups in the declared type which were thought to be
17598 dependent, but are not in the scope of the declarator. */
17599 decl_specifiers->type
17600 = maybe_update_decl_type (decl_specifiers->type, scope);
17602 /* If we're allowing GNU extensions, look for an
17603 asm-specification. */
17604 if (cp_parser_allow_gnu_extensions_p (parser))
17606 /* Look for an asm-specification. */
17607 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17608 asm_specification = cp_parser_asm_specification_opt (parser);
17610 else
17611 asm_specification = NULL_TREE;
17613 /* Look for attributes. */
17614 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17615 attributes = cp_parser_attributes_opt (parser);
17617 /* Peek at the next token. */
17618 token = cp_lexer_peek_token (parser->lexer);
17620 bool bogus_implicit_tmpl = false;
17622 if (function_declarator_p (declarator))
17624 /* Check to see if the token indicates the start of a
17625 function-definition. */
17626 if (cp_parser_token_starts_function_definition_p (token))
17628 if (!function_definition_allowed_p)
17630 /* If a function-definition should not appear here, issue an
17631 error message. */
17632 cp_parser_error (parser,
17633 "a function-definition is not allowed here");
17634 return error_mark_node;
17637 location_t func_brace_location
17638 = cp_lexer_peek_token (parser->lexer)->location;
17640 /* Neither attributes nor an asm-specification are allowed
17641 on a function-definition. */
17642 if (asm_specification)
17643 error_at (asm_spec_start_token->location,
17644 "an asm-specification is not allowed "
17645 "on a function-definition");
17646 if (attributes)
17647 error_at (attributes_start_token->location,
17648 "attributes are not allowed "
17649 "on a function-definition");
17650 /* This is a function-definition. */
17651 *function_definition_p = true;
17653 /* Parse the function definition. */
17654 if (member_p)
17655 decl = cp_parser_save_member_function_body (parser,
17656 decl_specifiers,
17657 declarator,
17658 prefix_attributes);
17659 else
17660 decl =
17661 (cp_parser_function_definition_from_specifiers_and_declarator
17662 (parser, decl_specifiers, prefix_attributes, declarator));
17664 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17666 /* This is where the prologue starts... */
17667 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17668 = func_brace_location;
17671 return decl;
17674 else if (parser->fully_implicit_function_template_p)
17676 /* A non-template declaration involving a function parameter list
17677 containing an implicit template parameter will be made into a
17678 template. If the resulting declaration is not going to be an
17679 actual function then finish the template scope here to prevent it.
17680 An error message will be issued once we have a decl to talk about.
17682 FIXME probably we should do type deduction rather than create an
17683 implicit template, but the standard currently doesn't allow it. */
17684 bogus_implicit_tmpl = true;
17685 finish_fully_implicit_template (parser, NULL_TREE);
17688 /* [dcl.dcl]
17690 Only in function declarations for constructors, destructors, and
17691 type conversions can the decl-specifier-seq be omitted.
17693 We explicitly postpone this check past the point where we handle
17694 function-definitions because we tolerate function-definitions
17695 that are missing their return types in some modes. */
17696 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17698 cp_parser_error (parser,
17699 "expected constructor, destructor, or type conversion");
17700 return error_mark_node;
17703 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17704 if (token->type == CPP_EQ
17705 || token->type == CPP_OPEN_PAREN
17706 || token->type == CPP_OPEN_BRACE)
17708 is_initialized = SD_INITIALIZED;
17709 initialization_kind = token->type;
17710 if (maybe_range_for_decl)
17711 *maybe_range_for_decl = error_mark_node;
17712 tmp_init_loc = token->location;
17713 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17714 *init_loc = tmp_init_loc;
17716 if (token->type == CPP_EQ
17717 && function_declarator_p (declarator))
17719 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17720 if (t2->keyword == RID_DEFAULT)
17721 is_initialized = SD_DEFAULTED;
17722 else if (t2->keyword == RID_DELETE)
17723 is_initialized = SD_DELETED;
17726 else
17728 /* If the init-declarator isn't initialized and isn't followed by a
17729 `,' or `;', it's not a valid init-declarator. */
17730 if (token->type != CPP_COMMA
17731 && token->type != CPP_SEMICOLON)
17733 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17734 range_for_decl_p = true;
17735 else
17737 if (!maybe_range_for_decl)
17738 cp_parser_error (parser, "expected initializer");
17739 return error_mark_node;
17742 is_initialized = SD_UNINITIALIZED;
17743 initialization_kind = CPP_EOF;
17746 /* Because start_decl has side-effects, we should only call it if we
17747 know we're going ahead. By this point, we know that we cannot
17748 possibly be looking at any other construct. */
17749 cp_parser_commit_to_tentative_parse (parser);
17751 /* Enter the newly declared entry in the symbol table. If we're
17752 processing a declaration in a class-specifier, we wait until
17753 after processing the initializer. */
17754 if (!member_p)
17756 if (parser->in_unbraced_linkage_specification_p)
17757 decl_specifiers->storage_class = sc_extern;
17758 decl = start_decl (declarator, decl_specifiers,
17759 range_for_decl_p? SD_INITIALIZED : is_initialized,
17760 attributes, prefix_attributes, &pushed_scope);
17761 cp_finalize_omp_declare_simd (parser, decl);
17762 /* Adjust location of decl if declarator->id_loc is more appropriate:
17763 set, and decl wasn't merged with another decl, in which case its
17764 location would be different from input_location, and more accurate. */
17765 if (DECL_P (decl)
17766 && declarator->id_loc != UNKNOWN_LOCATION
17767 && DECL_SOURCE_LOCATION (decl) == input_location)
17768 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17770 else if (scope)
17771 /* Enter the SCOPE. That way unqualified names appearing in the
17772 initializer will be looked up in SCOPE. */
17773 pushed_scope = push_scope (scope);
17775 /* Perform deferred access control checks, now that we know in which
17776 SCOPE the declared entity resides. */
17777 if (!member_p && decl)
17779 tree saved_current_function_decl = NULL_TREE;
17781 /* If the entity being declared is a function, pretend that we
17782 are in its scope. If it is a `friend', it may have access to
17783 things that would not otherwise be accessible. */
17784 if (TREE_CODE (decl) == FUNCTION_DECL)
17786 saved_current_function_decl = current_function_decl;
17787 current_function_decl = decl;
17790 /* Perform access checks for template parameters. */
17791 cp_parser_perform_template_parameter_access_checks (checks);
17793 /* Perform the access control checks for the declarator and the
17794 decl-specifiers. */
17795 perform_deferred_access_checks (tf_warning_or_error);
17797 /* Restore the saved value. */
17798 if (TREE_CODE (decl) == FUNCTION_DECL)
17799 current_function_decl = saved_current_function_decl;
17802 /* Parse the initializer. */
17803 initializer = NULL_TREE;
17804 is_direct_init = false;
17805 is_non_constant_init = true;
17806 if (is_initialized)
17808 if (function_declarator_p (declarator))
17810 if (initialization_kind == CPP_EQ)
17811 initializer = cp_parser_pure_specifier (parser);
17812 else
17814 /* If the declaration was erroneous, we don't really
17815 know what the user intended, so just silently
17816 consume the initializer. */
17817 if (decl != error_mark_node)
17818 error_at (tmp_init_loc, "initializer provided for function");
17819 cp_parser_skip_to_closing_parenthesis (parser,
17820 /*recovering=*/true,
17821 /*or_comma=*/false,
17822 /*consume_paren=*/true);
17825 else
17827 /* We want to record the extra mangling scope for in-class
17828 initializers of class members and initializers of static data
17829 member templates. The former involves deferring
17830 parsing of the initializer until end of class as with default
17831 arguments. So right here we only handle the latter. */
17832 if (!member_p && processing_template_decl)
17833 start_lambda_scope (decl);
17834 initializer = cp_parser_initializer (parser,
17835 &is_direct_init,
17836 &is_non_constant_init);
17837 if (!member_p && processing_template_decl)
17838 finish_lambda_scope ();
17839 if (initializer == error_mark_node)
17840 cp_parser_skip_to_end_of_statement (parser);
17844 /* The old parser allows attributes to appear after a parenthesized
17845 initializer. Mark Mitchell proposed removing this functionality
17846 on the GCC mailing lists on 2002-08-13. This parser accepts the
17847 attributes -- but ignores them. */
17848 if (cp_parser_allow_gnu_extensions_p (parser)
17849 && initialization_kind == CPP_OPEN_PAREN)
17850 if (cp_parser_attributes_opt (parser))
17851 warning (OPT_Wattributes,
17852 "attributes after parenthesized initializer ignored");
17854 /* And now complain about a non-function implicit template. */
17855 if (bogus_implicit_tmpl)
17856 error_at (DECL_SOURCE_LOCATION (decl),
17857 "non-function %qD declared as implicit template", decl);
17859 /* For an in-class declaration, use `grokfield' to create the
17860 declaration. */
17861 if (member_p)
17863 if (pushed_scope)
17865 pop_scope (pushed_scope);
17866 pushed_scope = NULL_TREE;
17868 decl = grokfield (declarator, decl_specifiers,
17869 initializer, !is_non_constant_init,
17870 /*asmspec=*/NULL_TREE,
17871 chainon (attributes, prefix_attributes));
17872 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17873 cp_parser_save_default_args (parser, decl);
17874 cp_finalize_omp_declare_simd (parser, decl);
17877 /* Finish processing the declaration. But, skip member
17878 declarations. */
17879 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17881 cp_finish_decl (decl,
17882 initializer, !is_non_constant_init,
17883 asm_specification,
17884 /* If the initializer is in parentheses, then this is
17885 a direct-initialization, which means that an
17886 `explicit' constructor is OK. Otherwise, an
17887 `explicit' constructor cannot be used. */
17888 ((is_direct_init || !is_initialized)
17889 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17891 else if ((cxx_dialect != cxx98) && friend_p
17892 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17893 /* Core issue #226 (C++0x only): A default template-argument
17894 shall not be specified in a friend class template
17895 declaration. */
17896 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17897 /*is_partial=*/false, /*is_friend_decl=*/1);
17899 if (!friend_p && pushed_scope)
17900 pop_scope (pushed_scope);
17902 if (function_declarator_p (declarator)
17903 && parser->fully_implicit_function_template_p)
17905 if (member_p)
17906 decl = finish_fully_implicit_template (parser, decl);
17907 else
17908 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17911 return decl;
17914 /* Parse a declarator.
17916 declarator:
17917 direct-declarator
17918 ptr-operator declarator
17920 abstract-declarator:
17921 ptr-operator abstract-declarator [opt]
17922 direct-abstract-declarator
17924 GNU Extensions:
17926 declarator:
17927 attributes [opt] direct-declarator
17928 attributes [opt] ptr-operator declarator
17930 abstract-declarator:
17931 attributes [opt] ptr-operator abstract-declarator [opt]
17932 attributes [opt] direct-abstract-declarator
17934 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17935 detect constructor, destructor or conversion operators. It is set
17936 to -1 if the declarator is a name, and +1 if it is a
17937 function. Otherwise it is set to zero. Usually you just want to
17938 test for >0, but internally the negative value is used.
17940 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17941 a decl-specifier-seq unless it declares a constructor, destructor,
17942 or conversion. It might seem that we could check this condition in
17943 semantic analysis, rather than parsing, but that makes it difficult
17944 to handle something like `f()'. We want to notice that there are
17945 no decl-specifiers, and therefore realize that this is an
17946 expression, not a declaration.)
17948 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17949 the declarator is a direct-declarator of the form "(...)".
17951 MEMBER_P is true iff this declarator is a member-declarator.
17953 FRIEND_P is true iff this declarator is a friend. */
17955 static cp_declarator *
17956 cp_parser_basic_declarator (cp_parser* parser,
17957 cp_parser_declarator_kind dcl_kind,
17958 int* ctor_dtor_or_conv_p,
17959 bool* parenthesized_p,
17960 bool member_p, bool friend_p)
17962 cp_declarator *declarator;
17963 enum tree_code code;
17964 cp_cv_quals cv_quals;
17965 tree class_type;
17966 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17968 /* Assume this is not a constructor, destructor, or type-conversion
17969 operator. */
17970 if (ctor_dtor_or_conv_p)
17971 *ctor_dtor_or_conv_p = 0;
17973 if (cp_parser_allow_gnu_extensions_p (parser))
17974 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17976 /* Check for the ptr-operator production. */
17977 cp_parser_parse_tentatively (parser);
17978 /* Parse the ptr-operator. */
17979 code = cp_parser_ptr_operator (parser,
17980 &class_type,
17981 &cv_quals,
17982 &std_attributes);
17984 /* If that worked, then we have a ptr-operator. */
17985 if (cp_parser_parse_definitely (parser))
17987 /* If a ptr-operator was found, then this declarator was not
17988 parenthesized. */
17989 if (parenthesized_p)
17990 *parenthesized_p = true;
17991 /* The dependent declarator is optional if we are parsing an
17992 abstract-declarator. */
17993 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17994 cp_parser_parse_tentatively (parser);
17996 /* Parse the dependent declarator. */
17997 declarator = cp_parser_basic_declarator (parser, dcl_kind,
17998 /*ctor_dtor_or_conv_p=*/NULL,
17999 /*parenthesized_p=*/NULL,
18000 /*member_p=*/false,
18001 friend_p);
18003 /* If we are parsing an abstract-declarator, we must handle the
18004 case where the dependent declarator is absent. */
18005 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18006 && !cp_parser_parse_definitely (parser))
18007 declarator = NULL;
18009 declarator = cp_parser_make_indirect_declarator
18010 (code, class_type, cv_quals, declarator, std_attributes);
18012 /* Everything else is a direct-declarator. */
18013 else
18015 if (parenthesized_p)
18016 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18017 CPP_OPEN_PAREN);
18018 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18019 ctor_dtor_or_conv_p,
18020 member_p, friend_p);
18023 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18024 declarator->attributes = gnu_attributes;
18026 return declarator;
18030 // A declarator may have a trailing requires clause. Because the
18031 // requires clause is part of the declarator the function parameters
18032 // are visibile in that expression.
18033 static tree
18034 cp_parser_trailing_requires_clause (cp_parser *parser, cp_declarator *decl)
18036 tree reqs = NULL_TREE;
18037 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
18039 cp_lexer_consume_token (parser->lexer);
18040 push_function_parms (decl);
18041 ++cp_unevaluated_operand;
18042 reqs = cp_parser_requires_clause (parser);
18043 --cp_unevaluated_operand;
18044 finish_scope();
18046 return reqs;
18049 /* Parse a declarator. See cp_parser_basic_declarator for details.
18051 In concepts, we allow a requires-clause to be parsed at
18052 after a function declarator. The program is ill-formed for
18053 any other kind of declarator.
18055 declarator:
18056 basic-declarator requires-clause [opt]
18058 basic-declarator:
18059 direct-declarator
18060 ptr-operator-declarator */
18062 static cp_declarator *
18063 cp_parser_declarator (cp_parser* parser,
18064 cp_parser_declarator_kind dcl_kind,
18065 int* ctor_dtor_or_conv_p,
18066 bool* parenthesized_p,
18067 bool member_p, bool friend_p)
18069 cp_declarator *declarator =
18070 cp_parser_basic_declarator (parser, dcl_kind,
18071 ctor_dtor_or_conv_p,
18072 parenthesized_p,
18073 member_p,
18074 friend_p);
18075 if (!declarator)
18076 return declarator;
18078 /* Function declarations may be followed by a trailing
18079 requires-clause. Declarators for function declartions
18080 are function declarators wrapping an id-declarator.
18081 If the inner declarator is anything else, it does not
18082 declare a function. These may also be reference or
18083 pointer declarators enclosing such a function declarator.
18084 In the declaration :
18086 int *f(args)
18088 the declarator is *f(args).
18090 Abstract declarators cannot have a requires-clauses
18091 because they do not declare functions. Here:
18093 void f() -> int& requires false
18095 The trailing return type contains an abstract declarator,
18096 and the requires-clause applies to the function
18097 declaration and not the abstract declarator. */
18098 if (flag_concepts && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
18100 if (cp_declarator *fndecl = get_function_declarator (declarator))
18101 fndecl->u.function.requires_clause
18102 = cp_parser_trailing_requires_clause (parser, declarator);
18104 return declarator;
18107 /* Parse a direct-declarator or direct-abstract-declarator.
18109 direct-declarator:
18110 declarator-id
18111 direct-declarator ( parameter-declaration-clause )
18112 cv-qualifier-seq [opt]
18113 ref-qualifier [opt]
18114 exception-specification [opt]
18115 direct-declarator [ constant-expression [opt] ]
18116 ( declarator )
18118 direct-abstract-declarator:
18119 direct-abstract-declarator [opt]
18120 ( parameter-declaration-clause )
18121 cv-qualifier-seq [opt]
18122 ref-qualifier [opt]
18123 exception-specification [opt]
18124 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18125 ( abstract-declarator )
18127 Returns a representation of the declarator. DCL_KIND is
18128 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18129 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18130 we are parsing a direct-declarator. It is
18131 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18132 of ambiguity we prefer an abstract declarator, as per
18133 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18134 as for cp_parser_declarator. */
18136 static cp_declarator *
18137 cp_parser_direct_declarator (cp_parser* parser,
18138 cp_parser_declarator_kind dcl_kind,
18139 int* ctor_dtor_or_conv_p,
18140 bool member_p, bool friend_p)
18142 cp_token *token;
18143 cp_declarator *declarator = NULL;
18144 tree scope = NULL_TREE;
18145 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18146 bool saved_in_declarator_p = parser->in_declarator_p;
18147 bool first = true;
18148 tree pushed_scope = NULL_TREE;
18150 while (true)
18152 /* Peek at the next token. */
18153 token = cp_lexer_peek_token (parser->lexer);
18154 if (token->type == CPP_OPEN_PAREN)
18156 /* This is either a parameter-declaration-clause, or a
18157 parenthesized declarator. When we know we are parsing a
18158 named declarator, it must be a parenthesized declarator
18159 if FIRST is true. For instance, `(int)' is a
18160 parameter-declaration-clause, with an omitted
18161 direct-abstract-declarator. But `((*))', is a
18162 parenthesized abstract declarator. Finally, when T is a
18163 template parameter `(T)' is a
18164 parameter-declaration-clause, and not a parenthesized
18165 named declarator.
18167 We first try and parse a parameter-declaration-clause,
18168 and then try a nested declarator (if FIRST is true).
18170 It is not an error for it not to be a
18171 parameter-declaration-clause, even when FIRST is
18172 false. Consider,
18174 int i (int);
18175 int i (3);
18177 The first is the declaration of a function while the
18178 second is the definition of a variable, including its
18179 initializer.
18181 Having seen only the parenthesis, we cannot know which of
18182 these two alternatives should be selected. Even more
18183 complex are examples like:
18185 int i (int (a));
18186 int i (int (3));
18188 The former is a function-declaration; the latter is a
18189 variable initialization.
18191 Thus again, we try a parameter-declaration-clause, and if
18192 that fails, we back out and return. */
18194 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18196 tree params;
18197 bool is_declarator = false;
18199 /* In a member-declarator, the only valid interpretation
18200 of a parenthesis is the start of a
18201 parameter-declaration-clause. (It is invalid to
18202 initialize a static data member with a parenthesized
18203 initializer; only the "=" form of initialization is
18204 permitted.) */
18205 if (!member_p)
18206 cp_parser_parse_tentatively (parser);
18208 /* Consume the `('. */
18209 cp_lexer_consume_token (parser->lexer);
18210 if (first)
18212 /* If this is going to be an abstract declarator, we're
18213 in a declarator and we can't have default args. */
18214 parser->default_arg_ok_p = false;
18215 parser->in_declarator_p = true;
18218 begin_scope (sk_function_parms, NULL_TREE);
18220 /* Parse the parameter-declaration-clause. */
18221 params = cp_parser_parameter_declaration_clause (parser);
18223 /* Consume the `)'. */
18224 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18226 /* If all went well, parse the cv-qualifier-seq,
18227 ref-qualifier and the exception-specification. */
18228 if (member_p || cp_parser_parse_definitely (parser))
18230 cp_cv_quals cv_quals;
18231 cp_virt_specifiers virt_specifiers;
18232 cp_ref_qualifier ref_qual;
18233 tree exception_specification;
18234 tree late_return;
18235 tree attrs;
18236 bool memfn = (member_p || (pushed_scope
18237 && CLASS_TYPE_P (pushed_scope)));
18239 is_declarator = true;
18241 if (ctor_dtor_or_conv_p)
18242 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18243 first = false;
18245 /* Parse the cv-qualifier-seq. */
18246 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18247 /* Parse the ref-qualifier. */
18248 ref_qual = cp_parser_ref_qualifier_opt (parser);
18249 /* And the exception-specification. */
18250 exception_specification
18251 = cp_parser_exception_specification_opt (parser);
18253 attrs = cp_parser_std_attribute_spec_seq (parser);
18255 /* In here, we handle cases where attribute is used after
18256 the function declaration. For example:
18257 void func (int x) __attribute__((vector(..))); */
18258 if (flag_cilkplus
18259 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18261 cp_parser_parse_tentatively (parser);
18262 tree attr = cp_parser_gnu_attributes_opt (parser);
18263 if (cp_lexer_next_token_is_not (parser->lexer,
18264 CPP_SEMICOLON)
18265 && cp_lexer_next_token_is_not (parser->lexer,
18266 CPP_OPEN_BRACE))
18267 cp_parser_abort_tentative_parse (parser);
18268 else if (!cp_parser_parse_definitely (parser))
18270 else
18271 attrs = chainon (attr, attrs);
18273 late_return = (cp_parser_late_return_type_opt
18274 (parser, declarator,
18275 memfn ? cv_quals : -1));
18278 /* Parse the virt-specifier-seq. */
18279 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18281 /* Create the function-declarator. */
18282 declarator = make_call_declarator (declarator,
18283 params,
18284 cv_quals,
18285 virt_specifiers,
18286 ref_qual,
18287 exception_specification,
18288 late_return,
18289 /*requires_clause=*/NULL_TREE);
18290 declarator->std_attributes = attrs;
18291 /* Any subsequent parameter lists are to do with
18292 return type, so are not those of the declared
18293 function. */
18294 parser->default_arg_ok_p = false;
18297 /* Remove the function parms from scope. */
18298 pop_bindings_and_leave_scope ();
18300 if (is_declarator)
18301 /* Repeat the main loop. */
18302 continue;
18305 /* If this is the first, we can try a parenthesized
18306 declarator. */
18307 if (first)
18309 bool saved_in_type_id_in_expr_p;
18311 parser->default_arg_ok_p = saved_default_arg_ok_p;
18312 parser->in_declarator_p = saved_in_declarator_p;
18314 /* Consume the `('. */
18315 cp_lexer_consume_token (parser->lexer);
18316 /* Parse the nested declarator. */
18317 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18318 parser->in_type_id_in_expr_p = true;
18319 declarator
18320 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
18321 /*parenthesized_p=*/NULL,
18322 member_p, friend_p);
18323 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18324 first = false;
18325 /* Expect a `)'. */
18326 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
18327 declarator = cp_error_declarator;
18328 if (declarator == cp_error_declarator)
18329 break;
18331 goto handle_declarator;
18333 /* Otherwise, we must be done. */
18334 else
18335 break;
18337 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18338 && token->type == CPP_OPEN_SQUARE
18339 && !cp_next_tokens_can_be_attribute_p (parser))
18341 /* Parse an array-declarator. */
18342 tree bounds, attrs;
18344 if (ctor_dtor_or_conv_p)
18345 *ctor_dtor_or_conv_p = 0;
18347 first = false;
18348 parser->default_arg_ok_p = false;
18349 parser->in_declarator_p = true;
18350 /* Consume the `['. */
18351 cp_lexer_consume_token (parser->lexer);
18352 /* Peek at the next token. */
18353 token = cp_lexer_peek_token (parser->lexer);
18354 /* If the next token is `]', then there is no
18355 constant-expression. */
18356 if (token->type != CPP_CLOSE_SQUARE)
18358 bool non_constant_p;
18359 bounds
18360 = cp_parser_constant_expression (parser,
18361 /*allow_non_constant=*/true,
18362 &non_constant_p);
18363 if (!non_constant_p)
18364 /* OK */;
18365 else if (error_operand_p (bounds))
18366 /* Already gave an error. */;
18367 else if (!parser->in_function_body
18368 || current_binding_level->kind == sk_function_parms)
18370 /* Normally, the array bound must be an integral constant
18371 expression. However, as an extension, we allow VLAs
18372 in function scopes as long as they aren't part of a
18373 parameter declaration. */
18374 cp_parser_error (parser,
18375 "array bound is not an integer constant");
18376 bounds = error_mark_node;
18378 else if (processing_template_decl
18379 && !type_dependent_expression_p (bounds))
18381 /* Remember this wasn't a constant-expression. */
18382 bounds = build_nop (TREE_TYPE (bounds), bounds);
18383 TREE_SIDE_EFFECTS (bounds) = 1;
18386 else
18387 bounds = NULL_TREE;
18388 /* Look for the closing `]'. */
18389 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
18391 declarator = cp_error_declarator;
18392 break;
18395 attrs = cp_parser_std_attribute_spec_seq (parser);
18396 declarator = make_array_declarator (declarator, bounds);
18397 declarator->std_attributes = attrs;
18399 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
18402 tree qualifying_scope;
18403 tree unqualified_name;
18404 tree attrs;
18405 special_function_kind sfk;
18406 bool abstract_ok;
18407 bool pack_expansion_p = false;
18408 cp_token *declarator_id_start_token;
18410 /* Parse a declarator-id */
18411 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
18412 if (abstract_ok)
18414 cp_parser_parse_tentatively (parser);
18416 /* If we see an ellipsis, we should be looking at a
18417 parameter pack. */
18418 if (token->type == CPP_ELLIPSIS)
18420 /* Consume the `...' */
18421 cp_lexer_consume_token (parser->lexer);
18423 pack_expansion_p = true;
18427 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
18428 unqualified_name
18429 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
18430 qualifying_scope = parser->scope;
18431 if (abstract_ok)
18433 bool okay = false;
18435 if (!unqualified_name && pack_expansion_p)
18437 /* Check whether an error occurred. */
18438 okay = !cp_parser_error_occurred (parser);
18440 /* We already consumed the ellipsis to mark a
18441 parameter pack, but we have no way to report it,
18442 so abort the tentative parse. We will be exiting
18443 immediately anyway. */
18444 cp_parser_abort_tentative_parse (parser);
18446 else
18447 okay = cp_parser_parse_definitely (parser);
18449 if (!okay)
18450 unqualified_name = error_mark_node;
18451 else if (unqualified_name
18452 && (qualifying_scope
18453 || (!identifier_p (unqualified_name))))
18455 cp_parser_error (parser, "expected unqualified-id");
18456 unqualified_name = error_mark_node;
18460 if (!unqualified_name)
18461 return NULL;
18462 if (unqualified_name == error_mark_node)
18464 declarator = cp_error_declarator;
18465 pack_expansion_p = false;
18466 declarator->parameter_pack_p = false;
18467 break;
18470 attrs = cp_parser_std_attribute_spec_seq (parser);
18472 if (qualifying_scope && at_namespace_scope_p ()
18473 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
18475 /* In the declaration of a member of a template class
18476 outside of the class itself, the SCOPE will sometimes
18477 be a TYPENAME_TYPE. For example, given:
18479 template <typename T>
18480 int S<T>::R::i = 3;
18482 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
18483 this context, we must resolve S<T>::R to an ordinary
18484 type, rather than a typename type.
18486 The reason we normally avoid resolving TYPENAME_TYPEs
18487 is that a specialization of `S' might render
18488 `S<T>::R' not a type. However, if `S' is
18489 specialized, then this `i' will not be used, so there
18490 is no harm in resolving the types here. */
18491 tree type;
18493 /* Resolve the TYPENAME_TYPE. */
18494 type = resolve_typename_type (qualifying_scope,
18495 /*only_current_p=*/false);
18496 /* If that failed, the declarator is invalid. */
18497 if (TREE_CODE (type) == TYPENAME_TYPE)
18499 if (typedef_variant_p (type))
18500 error_at (declarator_id_start_token->location,
18501 "cannot define member of dependent typedef "
18502 "%qT", type);
18503 else
18504 error_at (declarator_id_start_token->location,
18505 "%<%T::%E%> is not a type",
18506 TYPE_CONTEXT (qualifying_scope),
18507 TYPE_IDENTIFIER (qualifying_scope));
18509 qualifying_scope = type;
18512 sfk = sfk_none;
18514 if (unqualified_name)
18516 tree class_type;
18518 if (qualifying_scope
18519 && CLASS_TYPE_P (qualifying_scope))
18520 class_type = qualifying_scope;
18521 else
18522 class_type = current_class_type;
18524 if (TREE_CODE (unqualified_name) == TYPE_DECL)
18526 tree name_type = TREE_TYPE (unqualified_name);
18527 if (class_type && same_type_p (name_type, class_type))
18529 if (qualifying_scope
18530 && CLASSTYPE_USE_TEMPLATE (name_type))
18532 error_at (declarator_id_start_token->location,
18533 "invalid use of constructor as a template");
18534 inform (declarator_id_start_token->location,
18535 "use %<%T::%D%> instead of %<%T::%D%> to "
18536 "name the constructor in a qualified name",
18537 class_type,
18538 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
18539 class_type, name_type);
18540 declarator = cp_error_declarator;
18541 break;
18543 else
18544 unqualified_name = constructor_name (class_type);
18546 else
18548 /* We do not attempt to print the declarator
18549 here because we do not have enough
18550 information about its original syntactic
18551 form. */
18552 cp_parser_error (parser, "invalid declarator");
18553 declarator = cp_error_declarator;
18554 break;
18558 if (class_type)
18560 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
18561 sfk = sfk_destructor;
18562 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
18563 sfk = sfk_conversion;
18564 else if (/* There's no way to declare a constructor
18565 for an anonymous type, even if the type
18566 got a name for linkage purposes. */
18567 !TYPE_WAS_ANONYMOUS (class_type)
18568 /* Handle correctly (c++/19200):
18570 struct S {
18571 struct T{};
18572 friend void S(T);
18575 and also:
18577 namespace N {
18578 void S();
18581 struct S {
18582 friend void N::S();
18583 }; */
18584 && !(friend_p
18585 && class_type != qualifying_scope)
18586 && constructor_name_p (unqualified_name,
18587 class_type))
18589 unqualified_name = constructor_name (class_type);
18590 sfk = sfk_constructor;
18592 else if (is_overloaded_fn (unqualified_name)
18593 && DECL_CONSTRUCTOR_P (get_first_fn
18594 (unqualified_name)))
18595 sfk = sfk_constructor;
18597 if (ctor_dtor_or_conv_p && sfk != sfk_none)
18598 *ctor_dtor_or_conv_p = -1;
18601 declarator = make_id_declarator (qualifying_scope,
18602 unqualified_name,
18603 sfk);
18604 declarator->std_attributes = attrs;
18605 declarator->id_loc = token->location;
18606 declarator->parameter_pack_p = pack_expansion_p;
18608 if (pack_expansion_p)
18609 maybe_warn_variadic_templates ();
18612 handle_declarator:;
18613 scope = get_scope_of_declarator (declarator);
18614 if (scope)
18616 /* Any names that appear after the declarator-id for a
18617 member are looked up in the containing scope. */
18618 if (at_function_scope_p ())
18620 /* But declarations with qualified-ids can't appear in a
18621 function. */
18622 cp_parser_error (parser, "qualified-id in declaration");
18623 declarator = cp_error_declarator;
18624 break;
18626 pushed_scope = push_scope (scope);
18628 parser->in_declarator_p = true;
18629 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18630 || (declarator && declarator->kind == cdk_id))
18631 /* Default args are only allowed on function
18632 declarations. */
18633 parser->default_arg_ok_p = saved_default_arg_ok_p;
18634 else
18635 parser->default_arg_ok_p = false;
18637 first = false;
18639 /* We're done. */
18640 else
18641 break;
18644 /* For an abstract declarator, we might wind up with nothing at this
18645 point. That's an error; the declarator is not optional. */
18646 if (!declarator)
18647 cp_parser_error (parser, "expected declarator");
18649 /* If we entered a scope, we must exit it now. */
18650 if (pushed_scope)
18651 pop_scope (pushed_scope);
18653 parser->default_arg_ok_p = saved_default_arg_ok_p;
18654 parser->in_declarator_p = saved_in_declarator_p;
18656 return declarator;
18659 /* Parse a ptr-operator.
18661 ptr-operator:
18662 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18663 * cv-qualifier-seq [opt]
18665 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18666 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18668 GNU Extension:
18670 ptr-operator:
18671 & cv-qualifier-seq [opt]
18673 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18674 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18675 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18676 filled in with the TYPE containing the member. *CV_QUALS is
18677 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18678 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18679 Note that the tree codes returned by this function have nothing
18680 to do with the types of trees that will be eventually be created
18681 to represent the pointer or reference type being parsed. They are
18682 just constants with suggestive names. */
18683 static enum tree_code
18684 cp_parser_ptr_operator (cp_parser* parser,
18685 tree* type,
18686 cp_cv_quals *cv_quals,
18687 tree *attributes)
18689 enum tree_code code = ERROR_MARK;
18690 cp_token *token;
18691 tree attrs = NULL_TREE;
18693 /* Assume that it's not a pointer-to-member. */
18694 *type = NULL_TREE;
18695 /* And that there are no cv-qualifiers. */
18696 *cv_quals = TYPE_UNQUALIFIED;
18698 /* Peek at the next token. */
18699 token = cp_lexer_peek_token (parser->lexer);
18701 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18702 if (token->type == CPP_MULT)
18703 code = INDIRECT_REF;
18704 else if (token->type == CPP_AND)
18705 code = ADDR_EXPR;
18706 else if ((cxx_dialect != cxx98) &&
18707 token->type == CPP_AND_AND) /* C++0x only */
18708 code = NON_LVALUE_EXPR;
18710 if (code != ERROR_MARK)
18712 /* Consume the `*', `&' or `&&'. */
18713 cp_lexer_consume_token (parser->lexer);
18715 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18716 `&', if we are allowing GNU extensions. (The only qualifier
18717 that can legally appear after `&' is `restrict', but that is
18718 enforced during semantic analysis. */
18719 if (code == INDIRECT_REF
18720 || cp_parser_allow_gnu_extensions_p (parser))
18721 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18723 attrs = cp_parser_std_attribute_spec_seq (parser);
18724 if (attributes != NULL)
18725 *attributes = attrs;
18727 else
18729 /* Try the pointer-to-member case. */
18730 cp_parser_parse_tentatively (parser);
18731 /* Look for the optional `::' operator. */
18732 cp_parser_global_scope_opt (parser,
18733 /*current_scope_valid_p=*/false);
18734 /* Look for the nested-name specifier. */
18735 token = cp_lexer_peek_token (parser->lexer);
18736 cp_parser_nested_name_specifier (parser,
18737 /*typename_keyword_p=*/false,
18738 /*check_dependency_p=*/true,
18739 /*type_p=*/false,
18740 /*is_declaration=*/false);
18741 /* If we found it, and the next token is a `*', then we are
18742 indeed looking at a pointer-to-member operator. */
18743 if (!cp_parser_error_occurred (parser)
18744 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18746 /* Indicate that the `*' operator was used. */
18747 code = INDIRECT_REF;
18749 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18750 error_at (token->location, "%qD is a namespace", parser->scope);
18751 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18752 error_at (token->location, "cannot form pointer to member of "
18753 "non-class %q#T", parser->scope);
18754 else
18756 /* The type of which the member is a member is given by the
18757 current SCOPE. */
18758 *type = parser->scope;
18759 /* The next name will not be qualified. */
18760 parser->scope = NULL_TREE;
18761 parser->qualifying_scope = NULL_TREE;
18762 parser->object_scope = NULL_TREE;
18763 /* Look for optional c++11 attributes. */
18764 attrs = cp_parser_std_attribute_spec_seq (parser);
18765 if (attributes != NULL)
18766 *attributes = attrs;
18767 /* Look for the optional cv-qualifier-seq. */
18768 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18771 /* If that didn't work we don't have a ptr-operator. */
18772 if (!cp_parser_parse_definitely (parser))
18773 cp_parser_error (parser, "expected ptr-operator");
18776 return code;
18779 /* Parse an (optional) cv-qualifier-seq.
18781 cv-qualifier-seq:
18782 cv-qualifier cv-qualifier-seq [opt]
18784 cv-qualifier:
18785 const
18786 volatile
18788 GNU Extension:
18790 cv-qualifier:
18791 __restrict__
18793 Returns a bitmask representing the cv-qualifiers. */
18795 static cp_cv_quals
18796 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18798 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18800 while (true)
18802 cp_token *token;
18803 cp_cv_quals cv_qualifier;
18805 /* Peek at the next token. */
18806 token = cp_lexer_peek_token (parser->lexer);
18807 /* See if it's a cv-qualifier. */
18808 switch (token->keyword)
18810 case RID_CONST:
18811 cv_qualifier = TYPE_QUAL_CONST;
18812 break;
18814 case RID_VOLATILE:
18815 cv_qualifier = TYPE_QUAL_VOLATILE;
18816 break;
18818 case RID_RESTRICT:
18819 cv_qualifier = TYPE_QUAL_RESTRICT;
18820 break;
18822 default:
18823 cv_qualifier = TYPE_UNQUALIFIED;
18824 break;
18827 if (!cv_qualifier)
18828 break;
18830 if (cv_quals & cv_qualifier)
18832 error_at (token->location, "duplicate cv-qualifier");
18833 cp_lexer_purge_token (parser->lexer);
18835 else
18837 cp_lexer_consume_token (parser->lexer);
18838 cv_quals |= cv_qualifier;
18842 return cv_quals;
18845 /* Parse an (optional) ref-qualifier
18847 ref-qualifier:
18851 Returns cp_ref_qualifier representing ref-qualifier. */
18853 static cp_ref_qualifier
18854 cp_parser_ref_qualifier_opt (cp_parser* parser)
18856 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18858 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18859 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18860 return ref_qual;
18862 while (true)
18864 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18865 cp_token *token = cp_lexer_peek_token (parser->lexer);
18867 switch (token->type)
18869 case CPP_AND:
18870 curr_ref_qual = REF_QUAL_LVALUE;
18871 break;
18873 case CPP_AND_AND:
18874 curr_ref_qual = REF_QUAL_RVALUE;
18875 break;
18877 default:
18878 curr_ref_qual = REF_QUAL_NONE;
18879 break;
18882 if (!curr_ref_qual)
18883 break;
18884 else if (ref_qual)
18886 error_at (token->location, "multiple ref-qualifiers");
18887 cp_lexer_purge_token (parser->lexer);
18889 else
18891 ref_qual = curr_ref_qual;
18892 cp_lexer_consume_token (parser->lexer);
18896 return ref_qual;
18899 /* Parse an (optional) virt-specifier-seq.
18901 virt-specifier-seq:
18902 virt-specifier virt-specifier-seq [opt]
18904 virt-specifier:
18905 override
18906 final
18908 Returns a bitmask representing the virt-specifiers. */
18910 static cp_virt_specifiers
18911 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18913 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18915 while (true)
18917 cp_token *token;
18918 cp_virt_specifiers virt_specifier;
18920 /* Peek at the next token. */
18921 token = cp_lexer_peek_token (parser->lexer);
18922 /* See if it's a virt-specifier-qualifier. */
18923 if (token->type != CPP_NAME)
18924 break;
18925 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18927 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18928 virt_specifier = VIRT_SPEC_OVERRIDE;
18930 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18932 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18933 virt_specifier = VIRT_SPEC_FINAL;
18935 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18937 virt_specifier = VIRT_SPEC_FINAL;
18939 else
18940 break;
18942 if (virt_specifiers & virt_specifier)
18944 error_at (token->location, "duplicate virt-specifier");
18945 cp_lexer_purge_token (parser->lexer);
18947 else
18949 cp_lexer_consume_token (parser->lexer);
18950 virt_specifiers |= virt_specifier;
18953 return virt_specifiers;
18956 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18957 is in scope even though it isn't real. */
18959 void
18960 inject_this_parameter (tree ctype, cp_cv_quals quals)
18962 tree this_parm;
18964 if (current_class_ptr)
18966 /* We don't clear this between NSDMIs. Is it already what we want? */
18967 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18968 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18969 && cp_type_quals (type) == quals)
18970 return;
18973 this_parm = build_this_parm (ctype, quals);
18974 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18975 current_class_ptr = NULL_TREE;
18976 current_class_ref
18977 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18978 current_class_ptr = this_parm;
18981 /* Return true iff our current scope is a non-static data member
18982 initializer. */
18984 bool
18985 parsing_nsdmi (void)
18987 /* We recognize NSDMI context by the context-less 'this' pointer set up
18988 by the function above. */
18989 if (current_class_ptr
18990 && TREE_CODE (current_class_ptr) == PARM_DECL
18991 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18992 return true;
18993 return false;
18996 /* Parse a late-specified return type, if any. This is not a separate
18997 non-terminal, but part of a function declarator, which looks like
18999 -> trailing-type-specifier-seq abstract-declarator(opt)
19001 Returns the type indicated by the type-id.
19003 In addition to this this parses any queued up omp declare simd
19004 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19006 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19007 function. */
19009 static tree
19010 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19011 cp_cv_quals quals)
19013 cp_token *token;
19014 tree type = NULL_TREE;
19015 bool declare_simd_p = (parser->omp_declare_simd
19016 && declarator
19017 && declarator->kind == cdk_id);
19019 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19020 && declarator && declarator->kind == cdk_id);
19022 /* Peek at the next token. */
19023 token = cp_lexer_peek_token (parser->lexer);
19024 /* A late-specified return type is indicated by an initial '->'. */
19025 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
19026 return NULL_TREE;
19028 tree save_ccp = current_class_ptr;
19029 tree save_ccr = current_class_ref;
19030 if (quals >= 0)
19032 /* DR 1207: 'this' is in scope in the trailing return type. */
19033 inject_this_parameter (current_class_type, quals);
19036 if (token->type == CPP_DEREF)
19038 /* Consume the ->. */
19039 cp_lexer_consume_token (parser->lexer);
19040 type = cp_parser_trailing_type_id (parser);
19043 if (cilk_simd_fn_vector_p)
19044 declarator->std_attributes
19045 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19046 declarator->std_attributes);
19047 if (declare_simd_p)
19048 declarator->std_attributes
19049 = cp_parser_late_parsing_omp_declare_simd (parser,
19050 declarator->std_attributes);
19052 if (quals >= 0)
19054 current_class_ptr = save_ccp;
19055 current_class_ref = save_ccr;
19058 return type;
19061 /* Parse a declarator-id.
19063 declarator-id:
19064 id-expression
19065 :: [opt] nested-name-specifier [opt] type-name
19067 In the `id-expression' case, the value returned is as for
19068 cp_parser_id_expression if the id-expression was an unqualified-id.
19069 If the id-expression was a qualified-id, then a SCOPE_REF is
19070 returned. The first operand is the scope (either a NAMESPACE_DECL
19071 or TREE_TYPE), but the second is still just a representation of an
19072 unqualified-id. */
19074 static tree
19075 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19077 tree id;
19078 /* The expression must be an id-expression. Assume that qualified
19079 names are the names of types so that:
19081 template <class T>
19082 int S<T>::R::i = 3;
19084 will work; we must treat `S<T>::R' as the name of a type.
19085 Similarly, assume that qualified names are templates, where
19086 required, so that:
19088 template <class T>
19089 int S<T>::R<T>::i = 3;
19091 will work, too. */
19092 id = cp_parser_id_expression (parser,
19093 /*template_keyword_p=*/false,
19094 /*check_dependency_p=*/false,
19095 /*template_p=*/NULL,
19096 /*declarator_p=*/true,
19097 optional_p);
19098 if (id && BASELINK_P (id))
19099 id = BASELINK_FUNCTIONS (id);
19100 return id;
19103 /* Parse a type-id.
19105 type-id:
19106 type-specifier-seq abstract-declarator [opt]
19108 Returns the TYPE specified. */
19110 static tree
19111 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19112 bool is_trailing_return)
19114 cp_decl_specifier_seq type_specifier_seq;
19115 cp_declarator *abstract_declarator;
19117 /* Parse the type-specifier-seq. */
19118 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19119 is_trailing_return,
19120 &type_specifier_seq);
19121 if (type_specifier_seq.type == error_mark_node)
19122 return error_mark_node;
19124 /* There might or might not be an abstract declarator. */
19125 cp_parser_parse_tentatively (parser);
19126 /* Look for the declarator. */
19127 abstract_declarator
19128 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19129 /*parenthesized_p=*/NULL,
19130 /*member_p=*/false,
19131 /*friend_p=*/false);
19132 /* Check to see if there really was a declarator. */
19133 if (!cp_parser_parse_definitely (parser))
19134 abstract_declarator = NULL;
19136 if (type_specifier_seq.type
19137 /* None of the valid uses of 'auto' in C++14 involve the type-id
19138 nonterminal, but it is valid in a trailing-return-type. */
19139 && !(cxx_dialect >= cxx14 && is_trailing_return)
19140 && type_uses_auto (type_specifier_seq.type))
19142 /* A type-id with type 'auto' is only ok if the abstract declarator
19143 is a function declarator with a late-specified return type.
19145 A type-id with 'auto' is also valid in a trailing-return-type
19146 in a compound-requirment. */
19147 if (abstract_declarator
19148 && abstract_declarator->kind == cdk_function
19149 && abstract_declarator->u.function.late_return_type)
19150 /* OK */;
19151 else if (parser->in_result_type_constraint_p)
19152 /* OK */;
19153 else
19155 error ("invalid use of %<auto%>");
19156 return error_mark_node;
19160 return groktypename (&type_specifier_seq, abstract_declarator,
19161 is_template_arg);
19164 static tree cp_parser_type_id (cp_parser *parser)
19166 return cp_parser_type_id_1 (parser, false, false);
19169 static tree cp_parser_template_type_arg (cp_parser *parser)
19171 tree r;
19172 const char *saved_message = parser->type_definition_forbidden_message;
19173 parser->type_definition_forbidden_message
19174 = G_("types may not be defined in template arguments");
19175 r = cp_parser_type_id_1 (parser, true, false);
19176 parser->type_definition_forbidden_message = saved_message;
19177 if (cxx_dialect >= cxx14 && type_uses_auto (r))
19179 error ("invalid use of %<auto%> in template argument");
19180 r = error_mark_node;
19182 return r;
19185 static tree
19186 cp_parser_trailing_type_id (cp_parser *parser)
19188 return cp_parser_type_id_1 (parser, false, true);
19191 /* Parse a type-specifier-seq.
19193 type-specifier-seq:
19194 type-specifier type-specifier-seq [opt]
19196 GNU extension:
19198 type-specifier-seq:
19199 attributes type-specifier-seq [opt]
19201 If IS_DECLARATION is true, we are at the start of a "condition" or
19202 exception-declaration, so we might be followed by a declarator-id.
19204 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19205 i.e. we've just seen "->".
19207 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19209 static void
19210 cp_parser_type_specifier_seq (cp_parser* parser,
19211 bool is_declaration,
19212 bool is_trailing_return,
19213 cp_decl_specifier_seq *type_specifier_seq)
19215 bool seen_type_specifier = false;
19216 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19217 cp_token *start_token = NULL;
19219 /* Clear the TYPE_SPECIFIER_SEQ. */
19220 clear_decl_specs (type_specifier_seq);
19222 /* In the context of a trailing return type, enum E { } is an
19223 elaborated-type-specifier followed by a function-body, not an
19224 enum-specifier. */
19225 if (is_trailing_return)
19226 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19228 /* Parse the type-specifiers and attributes. */
19229 while (true)
19231 tree type_specifier;
19232 bool is_cv_qualifier;
19234 /* Check for attributes first. */
19235 if (cp_next_tokens_can_be_attribute_p (parser))
19237 type_specifier_seq->attributes =
19238 chainon (type_specifier_seq->attributes,
19239 cp_parser_attributes_opt (parser));
19240 continue;
19243 /* record the token of the beginning of the type specifier seq,
19244 for error reporting purposes*/
19245 if (!start_token)
19246 start_token = cp_lexer_peek_token (parser->lexer);
19248 /* Look for the type-specifier. */
19249 type_specifier = cp_parser_type_specifier (parser,
19250 flags,
19251 type_specifier_seq,
19252 /*is_declaration=*/false,
19253 NULL,
19254 &is_cv_qualifier);
19255 if (!type_specifier)
19257 /* If the first type-specifier could not be found, this is not a
19258 type-specifier-seq at all. */
19259 if (!seen_type_specifier)
19261 /* Set in_declarator_p to avoid skipping to the semicolon. */
19262 int in_decl = parser->in_declarator_p;
19263 parser->in_declarator_p = true;
19265 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
19266 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
19267 cp_parser_error (parser, "expected type-specifier");
19269 parser->in_declarator_p = in_decl;
19271 type_specifier_seq->type = error_mark_node;
19272 return;
19274 /* If subsequent type-specifiers could not be found, the
19275 type-specifier-seq is complete. */
19276 break;
19279 seen_type_specifier = true;
19280 /* The standard says that a condition can be:
19282 type-specifier-seq declarator = assignment-expression
19284 However, given:
19286 struct S {};
19287 if (int S = ...)
19289 we should treat the "S" as a declarator, not as a
19290 type-specifier. The standard doesn't say that explicitly for
19291 type-specifier-seq, but it does say that for
19292 decl-specifier-seq in an ordinary declaration. Perhaps it
19293 would be clearer just to allow a decl-specifier-seq here, and
19294 then add a semantic restriction that if any decl-specifiers
19295 that are not type-specifiers appear, the program is invalid. */
19296 if (is_declaration && !is_cv_qualifier)
19297 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
19301 /* Return whether the function currently being declared has an associated
19302 template parameter list. */
19304 static bool
19305 function_being_declared_is_template_p (cp_parser* parser)
19307 if (!current_template_parms || processing_template_parmlist)
19308 return false;
19310 if (parser->implicit_template_scope)
19311 return true;
19313 if (at_class_scope_p ()
19314 && TYPE_BEING_DEFINED (current_class_type))
19315 return parser->num_template_parameter_lists != 0;
19317 return ((int) parser->num_template_parameter_lists > template_class_depth
19318 (current_class_type));
19321 /* Parse a parameter-declaration-clause.
19323 parameter-declaration-clause:
19324 parameter-declaration-list [opt] ... [opt]
19325 parameter-declaration-list , ...
19327 Returns a representation for the parameter declarations. A return
19328 value of NULL indicates a parameter-declaration-clause consisting
19329 only of an ellipsis. */
19331 static tree
19332 cp_parser_parameter_declaration_clause (cp_parser* parser)
19334 tree parameters;
19335 cp_token *token;
19336 bool ellipsis_p;
19337 bool is_error;
19339 struct cleanup {
19340 cp_parser* parser;
19341 int auto_is_implicit_function_template_parm_p;
19342 ~cleanup() {
19343 parser->auto_is_implicit_function_template_parm_p
19344 = auto_is_implicit_function_template_parm_p;
19346 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
19348 (void) cleanup;
19350 if (!processing_specialization
19351 && !processing_template_parmlist
19352 && !processing_explicit_instantiation)
19353 if (!current_function_decl
19354 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
19355 parser->auto_is_implicit_function_template_parm_p = true;
19357 /* Peek at the next token. */
19358 token = cp_lexer_peek_token (parser->lexer);
19359 /* Check for trivial parameter-declaration-clauses. */
19360 if (token->type == CPP_ELLIPSIS)
19362 /* Consume the `...' token. */
19363 cp_lexer_consume_token (parser->lexer);
19364 return NULL_TREE;
19366 else if (token->type == CPP_CLOSE_PAREN)
19367 /* There are no parameters. */
19369 #ifndef NO_IMPLICIT_EXTERN_C
19370 if (in_system_header_at (input_location)
19371 && current_class_type == NULL
19372 && current_lang_name == lang_name_c)
19373 return NULL_TREE;
19374 else
19375 #endif
19376 return void_list_node;
19378 /* Check for `(void)', too, which is a special case. */
19379 else if (token->keyword == RID_VOID
19380 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19381 == CPP_CLOSE_PAREN))
19383 /* Consume the `void' token. */
19384 cp_lexer_consume_token (parser->lexer);
19385 /* There are no parameters. */
19386 return void_list_node;
19389 /* Parse the parameter-declaration-list. */
19390 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
19391 /* If a parse error occurred while parsing the
19392 parameter-declaration-list, then the entire
19393 parameter-declaration-clause is erroneous. */
19394 if (is_error)
19395 return NULL;
19397 /* Peek at the next token. */
19398 token = cp_lexer_peek_token (parser->lexer);
19399 /* If it's a `,', the clause should terminate with an ellipsis. */
19400 if (token->type == CPP_COMMA)
19402 /* Consume the `,'. */
19403 cp_lexer_consume_token (parser->lexer);
19404 /* Expect an ellipsis. */
19405 ellipsis_p
19406 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
19408 /* It might also be `...' if the optional trailing `,' was
19409 omitted. */
19410 else if (token->type == CPP_ELLIPSIS)
19412 /* Consume the `...' token. */
19413 cp_lexer_consume_token (parser->lexer);
19414 /* And remember that we saw it. */
19415 ellipsis_p = true;
19417 else
19418 ellipsis_p = false;
19420 /* Finish the parameter list. */
19421 if (!ellipsis_p)
19422 parameters = chainon (parameters, void_list_node);
19424 return parameters;
19427 /* Parse a parameter-declaration-list.
19429 parameter-declaration-list:
19430 parameter-declaration
19431 parameter-declaration-list , parameter-declaration
19433 Returns a representation of the parameter-declaration-list, as for
19434 cp_parser_parameter_declaration_clause. However, the
19435 `void_list_node' is never appended to the list. Upon return,
19436 *IS_ERROR will be true iff an error occurred. */
19438 static tree
19439 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
19441 tree parameters = NULL_TREE;
19442 tree *tail = &parameters;
19443 bool saved_in_unbraced_linkage_specification_p;
19444 int index = 0;
19446 /* Assume all will go well. */
19447 *is_error = false;
19448 /* The special considerations that apply to a function within an
19449 unbraced linkage specifications do not apply to the parameters
19450 to the function. */
19451 saved_in_unbraced_linkage_specification_p
19452 = parser->in_unbraced_linkage_specification_p;
19453 parser->in_unbraced_linkage_specification_p = false;
19455 /* Look for more parameters. */
19456 while (true)
19458 cp_parameter_declarator *parameter;
19459 tree decl = error_mark_node;
19460 bool parenthesized_p = false;
19461 int template_parm_idx = (function_being_declared_is_template_p (parser)?
19462 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
19463 (current_template_parms)) : 0);
19465 /* Parse the parameter. */
19466 parameter
19467 = cp_parser_parameter_declaration (parser,
19468 /*template_parm_p=*/false,
19469 &parenthesized_p);
19471 /* We don't know yet if the enclosing context is deprecated, so wait
19472 and warn in grokparms if appropriate. */
19473 deprecated_state = DEPRECATED_SUPPRESS;
19475 if (parameter)
19477 /* If a function parameter pack was specified and an implicit template
19478 parameter was introduced during cp_parser_parameter_declaration,
19479 change any implicit parameters introduced into packs. */
19480 if (parser->implicit_template_parms
19481 && parameter->declarator
19482 && parameter->declarator->parameter_pack_p)
19484 int latest_template_parm_idx = TREE_VEC_LENGTH
19485 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
19487 if (latest_template_parm_idx != template_parm_idx)
19488 parameter->decl_specifiers.type = convert_generic_types_to_packs
19489 (parameter->decl_specifiers.type,
19490 template_parm_idx, latest_template_parm_idx);
19493 decl = grokdeclarator (parameter->declarator,
19494 &parameter->decl_specifiers,
19495 PARM,
19496 parameter->default_argument != NULL_TREE,
19497 &parameter->decl_specifiers.attributes);
19500 deprecated_state = DEPRECATED_NORMAL;
19502 /* If a parse error occurred parsing the parameter declaration,
19503 then the entire parameter-declaration-list is erroneous. */
19504 if (decl == error_mark_node)
19506 *is_error = true;
19507 parameters = error_mark_node;
19508 break;
19511 if (parameter->decl_specifiers.attributes)
19512 cplus_decl_attributes (&decl,
19513 parameter->decl_specifiers.attributes,
19515 if (DECL_NAME (decl))
19516 decl = pushdecl (decl);
19518 if (decl != error_mark_node)
19520 retrofit_lang_decl (decl);
19521 DECL_PARM_INDEX (decl) = ++index;
19522 DECL_PARM_LEVEL (decl) = function_parm_depth ();
19525 /* Add the new parameter to the list. */
19526 *tail = build_tree_list (parameter->default_argument, decl);
19527 tail = &TREE_CHAIN (*tail);
19529 /* Peek at the next token. */
19530 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
19531 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
19532 /* These are for Objective-C++ */
19533 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19534 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19535 /* The parameter-declaration-list is complete. */
19536 break;
19537 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19539 cp_token *token;
19541 /* Peek at the next token. */
19542 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19543 /* If it's an ellipsis, then the list is complete. */
19544 if (token->type == CPP_ELLIPSIS)
19545 break;
19546 /* Otherwise, there must be more parameters. Consume the
19547 `,'. */
19548 cp_lexer_consume_token (parser->lexer);
19549 /* When parsing something like:
19551 int i(float f, double d)
19553 we can tell after seeing the declaration for "f" that we
19554 are not looking at an initialization of a variable "i",
19555 but rather at the declaration of a function "i".
19557 Due to the fact that the parsing of template arguments
19558 (as specified to a template-id) requires backtracking we
19559 cannot use this technique when inside a template argument
19560 list. */
19561 if (!parser->in_template_argument_list_p
19562 && !parser->in_type_id_in_expr_p
19563 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19564 /* However, a parameter-declaration of the form
19565 "float(f)" (which is a valid declaration of a
19566 parameter "f") can also be interpreted as an
19567 expression (the conversion of "f" to "float"). */
19568 && !parenthesized_p)
19569 cp_parser_commit_to_tentative_parse (parser);
19571 else
19573 cp_parser_error (parser, "expected %<,%> or %<...%>");
19574 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19575 cp_parser_skip_to_closing_parenthesis (parser,
19576 /*recovering=*/true,
19577 /*or_comma=*/false,
19578 /*consume_paren=*/false);
19579 break;
19583 parser->in_unbraced_linkage_specification_p
19584 = saved_in_unbraced_linkage_specification_p;
19586 /* Reset implicit_template_scope if we are about to leave the function
19587 parameter list that introduced it. Note that for out-of-line member
19588 definitions, there will be one or more class scopes before we get to
19589 the template parameter scope. */
19591 if (cp_binding_level *its = parser->implicit_template_scope)
19592 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
19594 while (maybe_its->kind == sk_class)
19595 maybe_its = maybe_its->level_chain;
19596 if (maybe_its == its)
19598 parser->implicit_template_parms = 0;
19599 parser->implicit_template_scope = 0;
19603 return parameters;
19606 /* Parse a parameter declaration.
19608 parameter-declaration:
19609 decl-specifier-seq ... [opt] declarator
19610 decl-specifier-seq declarator = assignment-expression
19611 decl-specifier-seq ... [opt] abstract-declarator [opt]
19612 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19614 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19615 declares a template parameter. (In that case, a non-nested `>'
19616 token encountered during the parsing of the assignment-expression
19617 is not interpreted as a greater-than operator.)
19619 Returns a representation of the parameter, or NULL if an error
19620 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19621 true iff the declarator is of the form "(p)". */
19623 static cp_parameter_declarator *
19624 cp_parser_parameter_declaration (cp_parser *parser,
19625 bool template_parm_p,
19626 bool *parenthesized_p)
19628 int declares_class_or_enum;
19629 cp_decl_specifier_seq decl_specifiers;
19630 cp_declarator *declarator;
19631 tree default_argument;
19632 cp_token *token = NULL, *declarator_token_start = NULL;
19633 const char *saved_message;
19635 /* In a template parameter, `>' is not an operator.
19637 [temp.param]
19639 When parsing a default template-argument for a non-type
19640 template-parameter, the first non-nested `>' is taken as the end
19641 of the template parameter-list rather than a greater-than
19642 operator. */
19644 /* Type definitions may not appear in parameter types. */
19645 saved_message = parser->type_definition_forbidden_message;
19646 parser->type_definition_forbidden_message
19647 = G_("types may not be defined in parameter types");
19649 /* Parse the declaration-specifiers. */
19650 cp_parser_decl_specifier_seq (parser,
19651 CP_PARSER_FLAGS_NONE,
19652 &decl_specifiers,
19653 &declares_class_or_enum);
19655 /* Complain about missing 'typename' or other invalid type names. */
19656 if (!decl_specifiers.any_type_specifiers_p
19657 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19658 decl_specifiers.type = error_mark_node;
19660 /* If an error occurred, there's no reason to attempt to parse the
19661 rest of the declaration. */
19662 if (cp_parser_error_occurred (parser))
19664 parser->type_definition_forbidden_message = saved_message;
19665 return NULL;
19668 /* Peek at the next token. */
19669 token = cp_lexer_peek_token (parser->lexer);
19671 /* If the next token is a `)', `,', `=', `>', or `...', then there
19672 is no declarator. However, when variadic templates are enabled,
19673 there may be a declarator following `...'. */
19674 if (token->type == CPP_CLOSE_PAREN
19675 || token->type == CPP_COMMA
19676 || token->type == CPP_EQ
19677 || token->type == CPP_GREATER)
19679 declarator = NULL;
19680 if (parenthesized_p)
19681 *parenthesized_p = false;
19683 /* Otherwise, there should be a declarator. */
19684 else
19686 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19687 parser->default_arg_ok_p = false;
19689 /* After seeing a decl-specifier-seq, if the next token is not a
19690 "(", there is no possibility that the code is a valid
19691 expression. Therefore, if parsing tentatively, we commit at
19692 this point. */
19693 if (!parser->in_template_argument_list_p
19694 /* In an expression context, having seen:
19696 (int((char ...
19698 we cannot be sure whether we are looking at a
19699 function-type (taking a "char" as a parameter) or a cast
19700 of some object of type "char" to "int". */
19701 && !parser->in_type_id_in_expr_p
19702 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19703 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19704 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19705 cp_parser_commit_to_tentative_parse (parser);
19706 /* Parse the declarator. */
19707 declarator_token_start = token;
19708 declarator = cp_parser_declarator (parser,
19709 CP_PARSER_DECLARATOR_EITHER,
19710 /*ctor_dtor_or_conv_p=*/NULL,
19711 parenthesized_p,
19712 /*member_p=*/false,
19713 /*friend_p=*/false);
19714 parser->default_arg_ok_p = saved_default_arg_ok_p;
19715 /* After the declarator, allow more attributes. */
19716 decl_specifiers.attributes
19717 = chainon (decl_specifiers.attributes,
19718 cp_parser_attributes_opt (parser));
19721 /* If the next token is an ellipsis, and we have not seen a
19722 declarator name, and the type of the declarator contains parameter
19723 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19724 a parameter pack expansion expression. Otherwise, leave the
19725 ellipsis for a C-style variadic function. */
19726 token = cp_lexer_peek_token (parser->lexer);
19727 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19729 tree type = decl_specifiers.type;
19731 if (type && DECL_P (type))
19732 type = TREE_TYPE (type);
19734 if (type
19735 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19736 && declarator_can_be_parameter_pack (declarator)
19737 && (!declarator || !declarator->parameter_pack_p)
19738 && uses_parameter_packs (type))
19740 /* Consume the `...'. */
19741 cp_lexer_consume_token (parser->lexer);
19742 maybe_warn_variadic_templates ();
19744 /* Build a pack expansion type */
19745 if (declarator)
19746 declarator->parameter_pack_p = true;
19747 else
19748 decl_specifiers.type = make_pack_expansion (type);
19752 /* The restriction on defining new types applies only to the type
19753 of the parameter, not to the default argument. */
19754 parser->type_definition_forbidden_message = saved_message;
19756 /* If the next token is `=', then process a default argument. */
19757 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19759 tree type = decl_specifiers.type;
19760 token = cp_lexer_peek_token (parser->lexer);
19761 /* If we are defining a class, then the tokens that make up the
19762 default argument must be saved and processed later. */
19763 if (!template_parm_p && at_class_scope_p ()
19764 && TYPE_BEING_DEFINED (current_class_type)
19765 && !LAMBDA_TYPE_P (current_class_type))
19766 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19768 // A constrained-type-specifier may declare a type template-parameter.
19769 else if (declares_type_template_parameter (type))
19770 default_argument
19771 = cp_parser_default_type_template_argument (parser);
19773 // A constrained-type-specifier may declare a template-template-parameter.
19774 else if (declares_template_template_parameter (type))
19775 default_argument
19776 = cp_parser_default_template_template_argument (parser);
19778 /* Outside of a class definition, we can just parse the
19779 assignment-expression. */
19780 else
19781 default_argument
19782 = cp_parser_default_argument (parser, template_parm_p);
19784 if (!parser->default_arg_ok_p)
19786 if (flag_permissive)
19787 warning (0, "deprecated use of default argument for parameter of non-function");
19788 else
19790 error_at (token->location,
19791 "default arguments are only "
19792 "permitted for function parameters");
19793 default_argument = NULL_TREE;
19796 else if ((declarator && declarator->parameter_pack_p)
19797 || (decl_specifiers.type
19798 && PACK_EXPANSION_P (decl_specifiers.type)))
19800 /* Find the name of the parameter pack. */
19801 cp_declarator *id_declarator = declarator;
19802 while (id_declarator && id_declarator->kind != cdk_id)
19803 id_declarator = id_declarator->declarator;
19805 if (id_declarator && id_declarator->kind == cdk_id)
19806 error_at (declarator_token_start->location,
19807 template_parm_p
19808 ? G_("template parameter pack %qD "
19809 "cannot have a default argument")
19810 : G_("parameter pack %qD cannot have "
19811 "a default argument"),
19812 id_declarator->u.id.unqualified_name);
19813 else
19814 error_at (declarator_token_start->location,
19815 template_parm_p
19816 ? G_("template parameter pack cannot have "
19817 "a default argument")
19818 : G_("parameter pack cannot have a "
19819 "default argument"));
19821 default_argument = NULL_TREE;
19824 else
19825 default_argument = NULL_TREE;
19827 return make_parameter_declarator (&decl_specifiers,
19828 declarator,
19829 default_argument);
19832 /* Parse a default argument and return it.
19834 TEMPLATE_PARM_P is true if this is a default argument for a
19835 non-type template parameter. */
19836 static tree
19837 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19839 tree default_argument = NULL_TREE;
19840 bool saved_greater_than_is_operator_p;
19841 bool saved_local_variables_forbidden_p;
19842 bool non_constant_p, is_direct_init;
19844 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19845 set correctly. */
19846 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19847 parser->greater_than_is_operator_p = !template_parm_p;
19848 /* Local variable names (and the `this' keyword) may not
19849 appear in a default argument. */
19850 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19851 parser->local_variables_forbidden_p = true;
19852 /* Parse the assignment-expression. */
19853 if (template_parm_p)
19854 push_deferring_access_checks (dk_no_deferred);
19855 tree saved_class_ptr = NULL_TREE;
19856 tree saved_class_ref = NULL_TREE;
19857 /* The "this" pointer is not valid in a default argument. */
19858 if (cfun)
19860 saved_class_ptr = current_class_ptr;
19861 cp_function_chain->x_current_class_ptr = NULL_TREE;
19862 saved_class_ref = current_class_ref;
19863 cp_function_chain->x_current_class_ref = NULL_TREE;
19865 default_argument
19866 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19867 /* Restore the "this" pointer. */
19868 if (cfun)
19870 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19871 cp_function_chain->x_current_class_ref = saved_class_ref;
19873 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19874 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19875 if (template_parm_p)
19876 pop_deferring_access_checks ();
19877 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19878 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19880 return default_argument;
19883 /* Parse a function-body.
19885 function-body:
19886 compound_statement */
19888 static void
19889 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19891 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19894 /* Parse a ctor-initializer-opt followed by a function-body. Return
19895 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19896 is true we are parsing a function-try-block. */
19898 static bool
19899 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19900 bool in_function_try_block)
19902 tree body, list;
19903 bool ctor_initializer_p;
19904 const bool check_body_p =
19905 DECL_CONSTRUCTOR_P (current_function_decl)
19906 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19907 tree last = NULL;
19909 /* Begin the function body. */
19910 body = begin_function_body ();
19911 /* Parse the optional ctor-initializer. */
19912 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19914 /* If we're parsing a constexpr constructor definition, we need
19915 to check that the constructor body is indeed empty. However,
19916 before we get to cp_parser_function_body lot of junk has been
19917 generated, so we can't just check that we have an empty block.
19918 Rather we take a snapshot of the outermost block, and check whether
19919 cp_parser_function_body changed its state. */
19920 if (check_body_p)
19922 list = cur_stmt_list;
19923 if (STATEMENT_LIST_TAIL (list))
19924 last = STATEMENT_LIST_TAIL (list)->stmt;
19926 /* Parse the function-body. */
19927 cp_parser_function_body (parser, in_function_try_block);
19928 if (check_body_p)
19929 check_constexpr_ctor_body (last, list, /*complain=*/true);
19930 /* Finish the function body. */
19931 finish_function_body (body);
19933 return ctor_initializer_p;
19936 /* Parse an initializer.
19938 initializer:
19939 = initializer-clause
19940 ( expression-list )
19942 Returns an expression representing the initializer. If no
19943 initializer is present, NULL_TREE is returned.
19945 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19946 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19947 set to TRUE if there is no initializer present. If there is an
19948 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19949 is set to true; otherwise it is set to false. */
19951 static tree
19952 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19953 bool* non_constant_p)
19955 cp_token *token;
19956 tree init;
19958 /* Peek at the next token. */
19959 token = cp_lexer_peek_token (parser->lexer);
19961 /* Let our caller know whether or not this initializer was
19962 parenthesized. */
19963 *is_direct_init = (token->type != CPP_EQ);
19964 /* Assume that the initializer is constant. */
19965 *non_constant_p = false;
19967 if (token->type == CPP_EQ)
19969 /* Consume the `='. */
19970 cp_lexer_consume_token (parser->lexer);
19971 /* Parse the initializer-clause. */
19972 init = cp_parser_initializer_clause (parser, non_constant_p);
19974 else if (token->type == CPP_OPEN_PAREN)
19976 vec<tree, va_gc> *vec;
19977 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19978 /*cast_p=*/false,
19979 /*allow_expansion_p=*/true,
19980 non_constant_p);
19981 if (vec == NULL)
19982 return error_mark_node;
19983 init = build_tree_list_vec (vec);
19984 release_tree_vector (vec);
19986 else if (token->type == CPP_OPEN_BRACE)
19988 cp_lexer_set_source_position (parser->lexer);
19989 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19990 init = cp_parser_braced_list (parser, non_constant_p);
19991 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19993 else
19995 /* Anything else is an error. */
19996 cp_parser_error (parser, "expected initializer");
19997 init = error_mark_node;
20000 return init;
20003 /* Parse an initializer-clause.
20005 initializer-clause:
20006 assignment-expression
20007 braced-init-list
20009 Returns an expression representing the initializer.
20011 If the `assignment-expression' production is used the value
20012 returned is simply a representation for the expression.
20014 Otherwise, calls cp_parser_braced_list. */
20016 static tree
20017 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20019 tree initializer;
20021 /* Assume the expression is constant. */
20022 *non_constant_p = false;
20024 /* If it is not a `{', then we are looking at an
20025 assignment-expression. */
20026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20028 initializer
20029 = cp_parser_constant_expression (parser,
20030 /*allow_non_constant_p=*/true,
20031 non_constant_p);
20033 else
20034 initializer = cp_parser_braced_list (parser, non_constant_p);
20036 return initializer;
20039 /* Parse a brace-enclosed initializer list.
20041 braced-init-list:
20042 { initializer-list , [opt] }
20045 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20046 the elements of the initializer-list (or NULL, if the last
20047 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20048 NULL_TREE. There is no way to detect whether or not the optional
20049 trailing `,' was provided. NON_CONSTANT_P is as for
20050 cp_parser_initializer. */
20052 static tree
20053 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20055 tree initializer;
20057 /* Consume the `{' token. */
20058 cp_lexer_consume_token (parser->lexer);
20059 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20060 initializer = make_node (CONSTRUCTOR);
20061 /* If it's not a `}', then there is a non-trivial initializer. */
20062 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20064 /* Parse the initializer list. */
20065 CONSTRUCTOR_ELTS (initializer)
20066 = cp_parser_initializer_list (parser, non_constant_p);
20067 /* A trailing `,' token is allowed. */
20068 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20069 cp_lexer_consume_token (parser->lexer);
20071 else
20072 *non_constant_p = false;
20073 /* Now, there should be a trailing `}'. */
20074 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20075 TREE_TYPE (initializer) = init_list_type_node;
20076 return initializer;
20079 /* Consume tokens up to, and including, the next non-nested closing `]'.
20080 Returns true iff we found a closing `]'. */
20082 static bool
20083 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20085 unsigned square_depth = 0;
20087 while (true)
20089 cp_token * token = cp_lexer_peek_token (parser->lexer);
20091 switch (token->type)
20093 case CPP_EOF:
20094 case CPP_PRAGMA_EOL:
20095 /* If we've run out of tokens, then there is no closing `]'. */
20096 return false;
20098 case CPP_OPEN_SQUARE:
20099 ++square_depth;
20100 break;
20102 case CPP_CLOSE_SQUARE:
20103 if (!square_depth--)
20105 cp_lexer_consume_token (parser->lexer);
20106 return true;
20108 break;
20110 default:
20111 break;
20114 /* Consume the token. */
20115 cp_lexer_consume_token (parser->lexer);
20119 /* Return true if we are looking at an array-designator, false otherwise. */
20121 static bool
20122 cp_parser_array_designator_p (cp_parser *parser)
20124 /* Consume the `['. */
20125 cp_lexer_consume_token (parser->lexer);
20127 cp_lexer_save_tokens (parser->lexer);
20129 /* Skip tokens until the next token is a closing square bracket.
20130 If we find the closing `]', and the next token is a `=', then
20131 we are looking at an array designator. */
20132 bool array_designator_p
20133 = (cp_parser_skip_to_closing_square_bracket (parser)
20134 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20136 /* Roll back the tokens we skipped. */
20137 cp_lexer_rollback_tokens (parser->lexer);
20139 return array_designator_p;
20142 /* Parse an initializer-list.
20144 initializer-list:
20145 initializer-clause ... [opt]
20146 initializer-list , initializer-clause ... [opt]
20148 GNU Extension:
20150 initializer-list:
20151 designation initializer-clause ...[opt]
20152 initializer-list , designation initializer-clause ...[opt]
20154 designation:
20155 . identifier =
20156 identifier :
20157 [ constant-expression ] =
20159 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20160 for the initializer. If the INDEX of the elt is non-NULL, it is the
20161 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20162 as for cp_parser_initializer. */
20164 static vec<constructor_elt, va_gc> *
20165 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20167 vec<constructor_elt, va_gc> *v = NULL;
20169 /* Assume all of the expressions are constant. */
20170 *non_constant_p = false;
20172 /* Parse the rest of the list. */
20173 while (true)
20175 cp_token *token;
20176 tree designator;
20177 tree initializer;
20178 bool clause_non_constant_p;
20180 /* If the next token is an identifier and the following one is a
20181 colon, we are looking at the GNU designated-initializer
20182 syntax. */
20183 if (cp_parser_allow_gnu_extensions_p (parser)
20184 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20185 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20187 /* Warn the user that they are using an extension. */
20188 pedwarn (input_location, OPT_Wpedantic,
20189 "ISO C++ does not allow designated initializers");
20190 /* Consume the identifier. */
20191 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20192 /* Consume the `:'. */
20193 cp_lexer_consume_token (parser->lexer);
20195 /* Also handle the C99 syntax, '. id ='. */
20196 else if (cp_parser_allow_gnu_extensions_p (parser)
20197 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20198 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20199 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20201 /* Warn the user that they are using an extension. */
20202 pedwarn (input_location, OPT_Wpedantic,
20203 "ISO C++ does not allow C99 designated initializers");
20204 /* Consume the `.'. */
20205 cp_lexer_consume_token (parser->lexer);
20206 /* Consume the identifier. */
20207 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20208 /* Consume the `='. */
20209 cp_lexer_consume_token (parser->lexer);
20211 /* Also handle C99 array designators, '[ const ] ='. */
20212 else if (cp_parser_allow_gnu_extensions_p (parser)
20213 && !c_dialect_objc ()
20214 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20216 /* In C++11, [ could start a lambda-introducer. */
20217 bool non_const = false;
20219 cp_parser_parse_tentatively (parser);
20221 if (!cp_parser_array_designator_p (parser))
20223 cp_parser_simulate_error (parser);
20224 designator = NULL_TREE;
20226 else
20228 designator = cp_parser_constant_expression (parser, true,
20229 &non_const);
20230 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20231 cp_parser_require (parser, CPP_EQ, RT_EQ);
20234 if (!cp_parser_parse_definitely (parser))
20235 designator = NULL_TREE;
20236 else if (non_const)
20237 require_potential_rvalue_constant_expression (designator);
20239 else
20240 designator = NULL_TREE;
20242 /* Parse the initializer. */
20243 initializer = cp_parser_initializer_clause (parser,
20244 &clause_non_constant_p);
20245 /* If any clause is non-constant, so is the entire initializer. */
20246 if (clause_non_constant_p)
20247 *non_constant_p = true;
20249 /* If we have an ellipsis, this is an initializer pack
20250 expansion. */
20251 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20253 /* Consume the `...'. */
20254 cp_lexer_consume_token (parser->lexer);
20256 /* Turn the initializer into an initializer expansion. */
20257 initializer = make_pack_expansion (initializer);
20260 /* Add it to the vector. */
20261 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
20263 /* If the next token is not a comma, we have reached the end of
20264 the list. */
20265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20266 break;
20268 /* Peek at the next token. */
20269 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20270 /* If the next token is a `}', then we're still done. An
20271 initializer-clause can have a trailing `,' after the
20272 initializer-list and before the closing `}'. */
20273 if (token->type == CPP_CLOSE_BRACE)
20274 break;
20276 /* Consume the `,' token. */
20277 cp_lexer_consume_token (parser->lexer);
20280 return v;
20283 /* Classes [gram.class] */
20285 /* Parse a class-name.
20287 class-name:
20288 identifier
20289 template-id
20291 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
20292 to indicate that names looked up in dependent types should be
20293 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
20294 keyword has been used to indicate that the name that appears next
20295 is a template. TAG_TYPE indicates the explicit tag given before
20296 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
20297 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
20298 is the class being defined in a class-head.
20300 Returns the TYPE_DECL representing the class. */
20302 static tree
20303 cp_parser_class_name (cp_parser *parser,
20304 bool typename_keyword_p,
20305 bool template_keyword_p,
20306 enum tag_types tag_type,
20307 bool check_dependency_p,
20308 bool class_head_p,
20309 bool is_declaration)
20311 tree decl;
20312 tree scope;
20313 bool typename_p;
20314 cp_token *token;
20315 tree identifier = NULL_TREE;
20317 /* All class-names start with an identifier. */
20318 token = cp_lexer_peek_token (parser->lexer);
20319 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
20321 cp_parser_error (parser, "expected class-name");
20322 return error_mark_node;
20325 /* PARSER->SCOPE can be cleared when parsing the template-arguments
20326 to a template-id, so we save it here. */
20327 scope = parser->scope;
20328 if (scope == error_mark_node)
20329 return error_mark_node;
20331 /* Any name names a type if we're following the `typename' keyword
20332 in a qualified name where the enclosing scope is type-dependent. */
20333 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
20334 && dependent_type_p (scope));
20335 /* Handle the common case (an identifier, but not a template-id)
20336 efficiently. */
20337 if (token->type == CPP_NAME
20338 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
20340 cp_token *identifier_token;
20341 bool ambiguous_p;
20343 /* Look for the identifier. */
20344 identifier_token = cp_lexer_peek_token (parser->lexer);
20345 ambiguous_p = identifier_token->error_reported;
20346 identifier = cp_parser_identifier (parser);
20347 /* If the next token isn't an identifier, we are certainly not
20348 looking at a class-name. */
20349 if (identifier == error_mark_node)
20350 decl = error_mark_node;
20351 /* If we know this is a type-name, there's no need to look it
20352 up. */
20353 else if (typename_p)
20354 decl = identifier;
20355 else
20357 tree ambiguous_decls;
20358 /* If we already know that this lookup is ambiguous, then
20359 we've already issued an error message; there's no reason
20360 to check again. */
20361 if (ambiguous_p)
20363 cp_parser_simulate_error (parser);
20364 return error_mark_node;
20366 /* If the next token is a `::', then the name must be a type
20367 name.
20369 [basic.lookup.qual]
20371 During the lookup for a name preceding the :: scope
20372 resolution operator, object, function, and enumerator
20373 names are ignored. */
20374 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20375 tag_type = typename_type;
20376 /* Look up the name. */
20377 decl = cp_parser_lookup_name (parser, identifier,
20378 tag_type,
20379 /*is_template=*/false,
20380 /*is_namespace=*/false,
20381 check_dependency_p,
20382 &ambiguous_decls,
20383 identifier_token->location);
20384 if (ambiguous_decls)
20386 if (cp_parser_parsing_tentatively (parser))
20387 cp_parser_simulate_error (parser);
20388 return error_mark_node;
20392 else
20394 /* Try a template-id. */
20395 decl = cp_parser_template_id (parser, template_keyword_p,
20396 check_dependency_p,
20397 tag_type,
20398 is_declaration);
20399 if (decl == error_mark_node)
20400 return error_mark_node;
20403 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
20405 /* If this is a typename, create a TYPENAME_TYPE. */
20406 if (typename_p && decl != error_mark_node)
20408 decl = make_typename_type (scope, decl, typename_type,
20409 /*complain=*/tf_error);
20410 if (decl != error_mark_node)
20411 decl = TYPE_NAME (decl);
20414 decl = strip_using_decl (decl);
20416 /* Check to see that it is really the name of a class. */
20417 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20418 && identifier_p (TREE_OPERAND (decl, 0))
20419 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20420 /* Situations like this:
20422 template <typename T> struct A {
20423 typename T::template X<int>::I i;
20426 are problematic. Is `T::template X<int>' a class-name? The
20427 standard does not seem to be definitive, but there is no other
20428 valid interpretation of the following `::'. Therefore, those
20429 names are considered class-names. */
20431 decl = make_typename_type (scope, decl, tag_type, tf_error);
20432 if (decl != error_mark_node)
20433 decl = TYPE_NAME (decl);
20435 else if (TREE_CODE (decl) != TYPE_DECL
20436 || TREE_TYPE (decl) == error_mark_node
20437 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
20438 /* In Objective-C 2.0, a classname followed by '.' starts a
20439 dot-syntax expression, and it's not a type-name. */
20440 || (c_dialect_objc ()
20441 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
20442 && objc_is_class_name (decl)))
20443 decl = error_mark_node;
20445 if (decl == error_mark_node)
20446 cp_parser_error (parser, "expected class-name");
20447 else if (identifier && !parser->scope)
20448 maybe_note_name_used_in_class (identifier, decl);
20450 return decl;
20453 /* Parse a class-specifier.
20455 class-specifier:
20456 class-head { member-specification [opt] }
20458 Returns the TREE_TYPE representing the class. */
20460 static tree
20461 cp_parser_class_specifier_1 (cp_parser* parser)
20463 tree type;
20464 tree attributes = NULL_TREE;
20465 bool nested_name_specifier_p;
20466 unsigned saved_num_template_parameter_lists;
20467 bool saved_in_function_body;
20468 unsigned char in_statement;
20469 bool in_switch_statement_p;
20470 bool saved_in_unbraced_linkage_specification_p;
20471 tree old_scope = NULL_TREE;
20472 tree scope = NULL_TREE;
20473 cp_token *closing_brace;
20475 push_deferring_access_checks (dk_no_deferred);
20477 /* Parse the class-head. */
20478 type = cp_parser_class_head (parser,
20479 &nested_name_specifier_p);
20481 /* If the class-head was a semantic disaster, skip the entire body
20482 of the class. */
20483 if (!type)
20485 cp_parser_skip_to_end_of_block_or_statement (parser);
20486 pop_deferring_access_checks ();
20487 return error_mark_node;
20490 /* Look for the `{'. */
20491 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
20493 pop_deferring_access_checks ();
20494 return error_mark_node;
20497 cp_ensure_no_omp_declare_simd (parser);
20499 /* Issue an error message if type-definitions are forbidden here. */
20500 cp_parser_check_type_definition (parser);
20501 /* Remember that we are defining one more class. */
20502 ++parser->num_classes_being_defined;
20503 /* Inside the class, surrounding template-parameter-lists do not
20504 apply. */
20505 saved_num_template_parameter_lists
20506 = parser->num_template_parameter_lists;
20507 parser->num_template_parameter_lists = 0;
20508 /* We are not in a function body. */
20509 saved_in_function_body = parser->in_function_body;
20510 parser->in_function_body = false;
20511 /* Or in a loop. */
20512 in_statement = parser->in_statement;
20513 parser->in_statement = 0;
20514 /* Or in a switch. */
20515 in_switch_statement_p = parser->in_switch_statement_p;
20516 parser->in_switch_statement_p = false;
20517 /* We are not immediately inside an extern "lang" block. */
20518 saved_in_unbraced_linkage_specification_p
20519 = parser->in_unbraced_linkage_specification_p;
20520 parser->in_unbraced_linkage_specification_p = false;
20522 // Associate constraints with the type.
20523 if (flag_concepts)
20524 type = associate_classtype_constraints (type);
20526 /* Start the class. */
20527 if (nested_name_specifier_p)
20529 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
20530 old_scope = push_inner_scope (scope);
20532 type = begin_class_definition (type);
20534 if (type == error_mark_node)
20535 /* If the type is erroneous, skip the entire body of the class. */
20536 cp_parser_skip_to_closing_brace (parser);
20537 else
20538 /* Parse the member-specification. */
20539 cp_parser_member_specification_opt (parser);
20541 /* Look for the trailing `}'. */
20542 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20543 /* Look for trailing attributes to apply to this class. */
20544 if (cp_parser_allow_gnu_extensions_p (parser))
20545 attributes = cp_parser_gnu_attributes_opt (parser);
20546 if (type != error_mark_node)
20547 type = finish_struct (type, attributes);
20548 if (nested_name_specifier_p)
20549 pop_inner_scope (old_scope, scope);
20551 /* We've finished a type definition. Check for the common syntax
20552 error of forgetting a semicolon after the definition. We need to
20553 be careful, as we can't just check for not-a-semicolon and be done
20554 with it; the user might have typed:
20556 class X { } c = ...;
20557 class X { } *p = ...;
20559 and so forth. Instead, enumerate all the possible tokens that
20560 might follow this production; if we don't see one of them, then
20561 complain and silently insert the semicolon. */
20563 cp_token *token = cp_lexer_peek_token (parser->lexer);
20564 bool want_semicolon = true;
20566 if (cp_next_tokens_can_be_std_attribute_p (parser))
20567 /* Don't try to parse c++11 attributes here. As per the
20568 grammar, that should be a task for
20569 cp_parser_decl_specifier_seq. */
20570 want_semicolon = false;
20572 switch (token->type)
20574 case CPP_NAME:
20575 case CPP_SEMICOLON:
20576 case CPP_MULT:
20577 case CPP_AND:
20578 case CPP_OPEN_PAREN:
20579 case CPP_CLOSE_PAREN:
20580 case CPP_COMMA:
20581 want_semicolon = false;
20582 break;
20584 /* While it's legal for type qualifiers and storage class
20585 specifiers to follow type definitions in the grammar, only
20586 compiler testsuites contain code like that. Assume that if
20587 we see such code, then what we're really seeing is a case
20588 like:
20590 class X { }
20591 const <type> var = ...;
20595 class Y { }
20596 static <type> func (...) ...
20598 i.e. the qualifier or specifier applies to the next
20599 declaration. To do so, however, we need to look ahead one
20600 more token to see if *that* token is a type specifier.
20602 This code could be improved to handle:
20604 class Z { }
20605 static const <type> var = ...; */
20606 case CPP_KEYWORD:
20607 if (keyword_is_decl_specifier (token->keyword))
20609 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
20611 /* Handling user-defined types here would be nice, but very
20612 tricky. */
20613 want_semicolon
20614 = (lookahead->type == CPP_KEYWORD
20615 && keyword_begins_type_specifier (lookahead->keyword));
20617 break;
20618 default:
20619 break;
20622 /* If we don't have a type, then something is very wrong and we
20623 shouldn't try to do anything clever. Likewise for not seeing the
20624 closing brace. */
20625 if (closing_brace && TYPE_P (type) && want_semicolon)
20627 cp_token_position prev
20628 = cp_lexer_previous_token_position (parser->lexer);
20629 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20630 location_t loc = prev_token->location;
20632 if (CLASSTYPE_DECLARED_CLASS (type))
20633 error_at (loc, "expected %<;%> after class definition");
20634 else if (TREE_CODE (type) == RECORD_TYPE)
20635 error_at (loc, "expected %<;%> after struct definition");
20636 else if (TREE_CODE (type) == UNION_TYPE)
20637 error_at (loc, "expected %<;%> after union definition");
20638 else
20639 gcc_unreachable ();
20641 /* Unget one token and smash it to look as though we encountered
20642 a semicolon in the input stream. */
20643 cp_lexer_set_token_position (parser->lexer, prev);
20644 token = cp_lexer_peek_token (parser->lexer);
20645 token->type = CPP_SEMICOLON;
20646 token->keyword = RID_MAX;
20650 /* If this class is not itself within the scope of another class,
20651 then we need to parse the bodies of all of the queued function
20652 definitions. Note that the queued functions defined in a class
20653 are not always processed immediately following the
20654 class-specifier for that class. Consider:
20656 struct A {
20657 struct B { void f() { sizeof (A); } };
20660 If `f' were processed before the processing of `A' were
20661 completed, there would be no way to compute the size of `A'.
20662 Note that the nesting we are interested in here is lexical --
20663 not the semantic nesting given by TYPE_CONTEXT. In particular,
20664 for:
20666 struct A { struct B; };
20667 struct A::B { void f() { } };
20669 there is no need to delay the parsing of `A::B::f'. */
20670 if (--parser->num_classes_being_defined == 0)
20672 tree decl;
20673 tree class_type = NULL_TREE;
20674 tree pushed_scope = NULL_TREE;
20675 unsigned ix;
20676 cp_default_arg_entry *e;
20677 tree save_ccp, save_ccr;
20679 /* In a first pass, parse default arguments to the functions.
20680 Then, in a second pass, parse the bodies of the functions.
20681 This two-phased approach handles cases like:
20683 struct S {
20684 void f() { g(); }
20685 void g(int i = 3);
20689 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20691 decl = e->decl;
20692 /* If there are default arguments that have not yet been processed,
20693 take care of them now. */
20694 if (class_type != e->class_type)
20696 if (pushed_scope)
20697 pop_scope (pushed_scope);
20698 class_type = e->class_type;
20699 pushed_scope = push_scope (class_type);
20701 /* Make sure that any template parameters are in scope. */
20702 maybe_begin_member_template_processing (decl);
20703 /* Parse the default argument expressions. */
20704 cp_parser_late_parsing_default_args (parser, decl);
20705 /* Remove any template parameters from the symbol table. */
20706 maybe_end_member_template_processing ();
20708 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20709 /* Now parse any NSDMIs. */
20710 save_ccp = current_class_ptr;
20711 save_ccr = current_class_ref;
20712 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20714 if (class_type != DECL_CONTEXT (decl))
20716 if (pushed_scope)
20717 pop_scope (pushed_scope);
20718 class_type = DECL_CONTEXT (decl);
20719 pushed_scope = push_scope (class_type);
20721 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20722 cp_parser_late_parsing_nsdmi (parser, decl);
20724 vec_safe_truncate (unparsed_nsdmis, 0);
20725 current_class_ptr = save_ccp;
20726 current_class_ref = save_ccr;
20727 if (pushed_scope)
20728 pop_scope (pushed_scope);
20730 /* Now do some post-NSDMI bookkeeping. */
20731 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20732 after_nsdmi_defaulted_late_checks (class_type);
20733 vec_safe_truncate (unparsed_classes, 0);
20734 after_nsdmi_defaulted_late_checks (type);
20736 /* Now parse the body of the functions. */
20737 if (flag_openmp)
20739 /* OpenMP UDRs need to be parsed before all other functions. */
20740 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20741 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20742 cp_parser_late_parsing_for_member (parser, decl);
20743 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20744 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20745 cp_parser_late_parsing_for_member (parser, decl);
20747 else
20748 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20749 cp_parser_late_parsing_for_member (parser, decl);
20750 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20752 else
20753 vec_safe_push (unparsed_classes, type);
20755 /* Put back any saved access checks. */
20756 pop_deferring_access_checks ();
20758 /* Restore saved state. */
20759 parser->in_switch_statement_p = in_switch_statement_p;
20760 parser->in_statement = in_statement;
20761 parser->in_function_body = saved_in_function_body;
20762 parser->num_template_parameter_lists
20763 = saved_num_template_parameter_lists;
20764 parser->in_unbraced_linkage_specification_p
20765 = saved_in_unbraced_linkage_specification_p;
20767 return type;
20770 static tree
20771 cp_parser_class_specifier (cp_parser* parser)
20773 tree ret;
20774 timevar_push (TV_PARSE_STRUCT);
20775 ret = cp_parser_class_specifier_1 (parser);
20776 timevar_pop (TV_PARSE_STRUCT);
20777 return ret;
20780 /* Parse a class-head.
20782 class-head:
20783 class-key identifier [opt] base-clause [opt]
20784 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20785 class-key nested-name-specifier [opt] template-id
20786 base-clause [opt]
20788 class-virt-specifier:
20789 final
20791 GNU Extensions:
20792 class-key attributes identifier [opt] base-clause [opt]
20793 class-key attributes nested-name-specifier identifier base-clause [opt]
20794 class-key attributes nested-name-specifier [opt] template-id
20795 base-clause [opt]
20797 Upon return BASES is initialized to the list of base classes (or
20798 NULL, if there are none) in the same form returned by
20799 cp_parser_base_clause.
20801 Returns the TYPE of the indicated class. Sets
20802 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20803 involving a nested-name-specifier was used, and FALSE otherwise.
20805 Returns error_mark_node if this is not a class-head.
20807 Returns NULL_TREE if the class-head is syntactically valid, but
20808 semantically invalid in a way that means we should skip the entire
20809 body of the class. */
20811 static tree
20812 cp_parser_class_head (cp_parser* parser,
20813 bool* nested_name_specifier_p)
20815 tree nested_name_specifier;
20816 enum tag_types class_key;
20817 tree id = NULL_TREE;
20818 tree type = NULL_TREE;
20819 tree attributes;
20820 tree bases;
20821 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20822 bool template_id_p = false;
20823 bool qualified_p = false;
20824 bool invalid_nested_name_p = false;
20825 bool invalid_explicit_specialization_p = false;
20826 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20827 tree pushed_scope = NULL_TREE;
20828 unsigned num_templates;
20829 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20830 /* Assume no nested-name-specifier will be present. */
20831 *nested_name_specifier_p = false;
20832 /* Assume no template parameter lists will be used in defining the
20833 type. */
20834 num_templates = 0;
20835 parser->colon_corrects_to_scope_p = false;
20837 /* Look for the class-key. */
20838 class_key = cp_parser_class_key (parser);
20839 if (class_key == none_type)
20840 return error_mark_node;
20842 /* Parse the attributes. */
20843 attributes = cp_parser_attributes_opt (parser);
20845 /* If the next token is `::', that is invalid -- but sometimes
20846 people do try to write:
20848 struct ::S {};
20850 Handle this gracefully by accepting the extra qualifier, and then
20851 issuing an error about it later if this really is a
20852 class-head. If it turns out just to be an elaborated type
20853 specifier, remain silent. */
20854 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20855 qualified_p = true;
20857 push_deferring_access_checks (dk_no_check);
20859 /* Determine the name of the class. Begin by looking for an
20860 optional nested-name-specifier. */
20861 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20862 nested_name_specifier
20863 = cp_parser_nested_name_specifier_opt (parser,
20864 /*typename_keyword_p=*/false,
20865 /*check_dependency_p=*/false,
20866 /*type_p=*/true,
20867 /*is_declaration=*/false);
20868 /* If there was a nested-name-specifier, then there *must* be an
20869 identifier. */
20870 if (nested_name_specifier)
20872 type_start_token = cp_lexer_peek_token (parser->lexer);
20873 /* Although the grammar says `identifier', it really means
20874 `class-name' or `template-name'. You are only allowed to
20875 define a class that has already been declared with this
20876 syntax.
20878 The proposed resolution for Core Issue 180 says that wherever
20879 you see `class T::X' you should treat `X' as a type-name.
20881 It is OK to define an inaccessible class; for example:
20883 class A { class B; };
20884 class A::B {};
20886 We do not know if we will see a class-name, or a
20887 template-name. We look for a class-name first, in case the
20888 class-name is a template-id; if we looked for the
20889 template-name first we would stop after the template-name. */
20890 cp_parser_parse_tentatively (parser);
20891 type = cp_parser_class_name (parser,
20892 /*typename_keyword_p=*/false,
20893 /*template_keyword_p=*/false,
20894 class_type,
20895 /*check_dependency_p=*/false,
20896 /*class_head_p=*/true,
20897 /*is_declaration=*/false);
20898 /* If that didn't work, ignore the nested-name-specifier. */
20899 if (!cp_parser_parse_definitely (parser))
20901 invalid_nested_name_p = true;
20902 type_start_token = cp_lexer_peek_token (parser->lexer);
20903 id = cp_parser_identifier (parser);
20904 if (id == error_mark_node)
20905 id = NULL_TREE;
20907 /* If we could not find a corresponding TYPE, treat this
20908 declaration like an unqualified declaration. */
20909 if (type == error_mark_node)
20910 nested_name_specifier = NULL_TREE;
20911 /* Otherwise, count the number of templates used in TYPE and its
20912 containing scopes. */
20913 else
20915 tree scope;
20917 for (scope = TREE_TYPE (type);
20918 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20919 scope = get_containing_scope (scope))
20920 if (TYPE_P (scope)
20921 && CLASS_TYPE_P (scope)
20922 && CLASSTYPE_TEMPLATE_INFO (scope)
20923 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20924 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20925 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20926 ++num_templates;
20929 /* Otherwise, the identifier is optional. */
20930 else
20932 /* We don't know whether what comes next is a template-id,
20933 an identifier, or nothing at all. */
20934 cp_parser_parse_tentatively (parser);
20935 /* Check for a template-id. */
20936 type_start_token = cp_lexer_peek_token (parser->lexer);
20937 id = cp_parser_template_id (parser,
20938 /*template_keyword_p=*/false,
20939 /*check_dependency_p=*/true,
20940 class_key,
20941 /*is_declaration=*/true);
20942 /* If that didn't work, it could still be an identifier. */
20943 if (!cp_parser_parse_definitely (parser))
20945 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20947 type_start_token = cp_lexer_peek_token (parser->lexer);
20948 id = cp_parser_identifier (parser);
20950 else
20951 id = NULL_TREE;
20953 else
20955 template_id_p = true;
20956 ++num_templates;
20960 pop_deferring_access_checks ();
20962 if (id)
20964 cp_parser_check_for_invalid_template_id (parser, id,
20965 class_key,
20966 type_start_token->location);
20968 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20970 /* If it's not a `:' or a `{' then we can't really be looking at a
20971 class-head, since a class-head only appears as part of a
20972 class-specifier. We have to detect this situation before calling
20973 xref_tag, since that has irreversible side-effects. */
20974 if (!cp_parser_next_token_starts_class_definition_p (parser))
20976 cp_parser_error (parser, "expected %<{%> or %<:%>");
20977 type = error_mark_node;
20978 goto out;
20981 /* At this point, we're going ahead with the class-specifier, even
20982 if some other problem occurs. */
20983 cp_parser_commit_to_tentative_parse (parser);
20984 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20986 cp_parser_error (parser,
20987 "cannot specify %<override%> for a class");
20988 type = error_mark_node;
20989 goto out;
20991 /* Issue the error about the overly-qualified name now. */
20992 if (qualified_p)
20994 cp_parser_error (parser,
20995 "global qualification of class name is invalid");
20996 type = error_mark_node;
20997 goto out;
20999 else if (invalid_nested_name_p)
21001 cp_parser_error (parser,
21002 "qualified name does not name a class");
21003 type = error_mark_node;
21004 goto out;
21006 else if (nested_name_specifier)
21008 tree scope;
21010 /* Reject typedef-names in class heads. */
21011 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21013 error_at (type_start_token->location,
21014 "invalid class name in declaration of %qD",
21015 type);
21016 type = NULL_TREE;
21017 goto done;
21020 /* Figure out in what scope the declaration is being placed. */
21021 scope = current_scope ();
21022 /* If that scope does not contain the scope in which the
21023 class was originally declared, the program is invalid. */
21024 if (scope && !is_ancestor (scope, nested_name_specifier))
21026 if (at_namespace_scope_p ())
21027 error_at (type_start_token->location,
21028 "declaration of %qD in namespace %qD which does not "
21029 "enclose %qD",
21030 type, scope, nested_name_specifier);
21031 else
21032 error_at (type_start_token->location,
21033 "declaration of %qD in %qD which does not enclose %qD",
21034 type, scope, nested_name_specifier);
21035 type = NULL_TREE;
21036 goto done;
21038 /* [dcl.meaning]
21040 A declarator-id shall not be qualified except for the
21041 definition of a ... nested class outside of its class
21042 ... [or] the definition or explicit instantiation of a
21043 class member of a namespace outside of its namespace. */
21044 if (scope == nested_name_specifier)
21046 permerror (nested_name_specifier_token_start->location,
21047 "extra qualification not allowed");
21048 nested_name_specifier = NULL_TREE;
21049 num_templates = 0;
21052 /* An explicit-specialization must be preceded by "template <>". If
21053 it is not, try to recover gracefully. */
21054 if (at_namespace_scope_p ()
21055 && parser->num_template_parameter_lists == 0
21056 && template_id_p)
21058 error_at (type_start_token->location,
21059 "an explicit specialization must be preceded by %<template <>%>");
21060 invalid_explicit_specialization_p = true;
21061 /* Take the same action that would have been taken by
21062 cp_parser_explicit_specialization. */
21063 ++parser->num_template_parameter_lists;
21064 begin_specialization ();
21066 /* There must be no "return" statements between this point and the
21067 end of this function; set "type "to the correct return value and
21068 use "goto done;" to return. */
21069 /* Make sure that the right number of template parameters were
21070 present. */
21071 if (!cp_parser_check_template_parameters (parser, num_templates,
21072 type_start_token->location,
21073 /*declarator=*/NULL))
21075 /* If something went wrong, there is no point in even trying to
21076 process the class-definition. */
21077 type = NULL_TREE;
21078 goto done;
21081 /* Look up the type. */
21082 if (template_id_p)
21084 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21085 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21086 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21088 error_at (type_start_token->location,
21089 "function template %qD redeclared as a class template", id);
21090 type = error_mark_node;
21092 else
21094 type = TREE_TYPE (id);
21095 type = maybe_process_partial_specialization (type);
21097 if (nested_name_specifier)
21098 pushed_scope = push_scope (nested_name_specifier);
21100 else if (nested_name_specifier)
21102 tree class_type;
21104 /* Given:
21106 template <typename T> struct S { struct T };
21107 template <typename T> struct S<T>::T { };
21109 we will get a TYPENAME_TYPE when processing the definition of
21110 `S::T'. We need to resolve it to the actual type before we
21111 try to define it. */
21112 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21114 class_type = resolve_typename_type (TREE_TYPE (type),
21115 /*only_current_p=*/false);
21116 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21117 type = TYPE_NAME (class_type);
21118 else
21120 cp_parser_error (parser, "could not resolve typename type");
21121 type = error_mark_node;
21125 if (maybe_process_partial_specialization (TREE_TYPE (type))
21126 == error_mark_node)
21128 type = NULL_TREE;
21129 goto done;
21132 class_type = current_class_type;
21133 /* Enter the scope indicated by the nested-name-specifier. */
21134 pushed_scope = push_scope (nested_name_specifier);
21135 /* Get the canonical version of this type. */
21136 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21137 /* Call push_template_decl if it seems like we should be defining a
21138 template either from the template headers or the type we're
21139 defining, so that we diagnose both extra and missing headers. */
21140 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21141 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21142 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21144 type = push_template_decl (type);
21145 if (type == error_mark_node)
21147 type = NULL_TREE;
21148 goto done;
21152 type = TREE_TYPE (type);
21153 *nested_name_specifier_p = true;
21155 else /* The name is not a nested name. */
21157 /* If the class was unnamed, create a dummy name. */
21158 if (!id)
21159 id = make_anon_name ();
21160 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21161 parser->num_template_parameter_lists);
21164 /* Indicate whether this class was declared as a `class' or as a
21165 `struct'. */
21166 if (TREE_CODE (type) == RECORD_TYPE)
21167 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21168 cp_parser_check_class_key (class_key, type);
21170 /* If this type was already complete, and we see another definition,
21171 that's an error. */
21172 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21174 error_at (type_start_token->location, "redefinition of %q#T",
21175 type);
21176 error_at (type_start_token->location, "previous definition of %q+#T",
21177 type);
21178 type = NULL_TREE;
21179 goto done;
21181 else if (type == error_mark_node)
21182 type = NULL_TREE;
21184 if (type)
21186 /* Apply attributes now, before any use of the class as a template
21187 argument in its base list. */
21188 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21189 fixup_attribute_variants (type);
21192 /* We will have entered the scope containing the class; the names of
21193 base classes should be looked up in that context. For example:
21195 struct A { struct B {}; struct C; };
21196 struct A::C : B {};
21198 is valid. */
21200 /* Get the list of base-classes, if there is one. */
21201 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21203 /* PR59482: enter the class scope so that base-specifiers are looked
21204 up correctly. */
21205 if (type)
21206 pushclass (type);
21207 bases = cp_parser_base_clause (parser);
21208 /* PR59482: get out of the previously pushed class scope so that the
21209 subsequent pops pop the right thing. */
21210 if (type)
21211 popclass ();
21213 else
21214 bases = NULL_TREE;
21216 /* If we're really defining a class, process the base classes.
21217 If they're invalid, fail. */
21218 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21219 && !xref_basetypes (type, bases))
21220 type = NULL_TREE;
21222 done:
21223 /* Leave the scope given by the nested-name-specifier. We will
21224 enter the class scope itself while processing the members. */
21225 if (pushed_scope)
21226 pop_scope (pushed_scope);
21228 if (invalid_explicit_specialization_p)
21230 end_specialization ();
21231 --parser->num_template_parameter_lists;
21234 if (type)
21235 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21236 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
21237 CLASSTYPE_FINAL (type) = 1;
21238 out:
21239 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21240 return type;
21243 /* Parse a class-key.
21245 class-key:
21246 class
21247 struct
21248 union
21250 Returns the kind of class-key specified, or none_type to indicate
21251 error. */
21253 static enum tag_types
21254 cp_parser_class_key (cp_parser* parser)
21256 cp_token *token;
21257 enum tag_types tag_type;
21259 /* Look for the class-key. */
21260 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
21261 if (!token)
21262 return none_type;
21264 /* Check to see if the TOKEN is a class-key. */
21265 tag_type = cp_parser_token_is_class_key (token);
21266 if (!tag_type)
21267 cp_parser_error (parser, "expected class-key");
21268 return tag_type;
21271 /* Parse a type-parameter-key.
21273 type-parameter-key:
21274 class
21275 typename
21278 static void
21279 cp_parser_type_parameter_key (cp_parser* parser)
21281 /* Look for the type-parameter-key. */
21282 enum tag_types tag_type = none_type;
21283 cp_token *token = cp_lexer_peek_token (parser->lexer);
21284 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
21286 cp_lexer_consume_token (parser->lexer);
21287 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
21288 /* typename is not allowed in a template template parameter
21289 by the standard until C++1Z. */
21290 pedwarn (token->location, OPT_Wpedantic,
21291 "ISO C++ forbids typename key in template template parameter;"
21292 " use -std=c++1z or -std=gnu++1z");
21294 else
21295 cp_parser_error (parser, "expected %<class%> or %<typename%>");
21297 return;
21300 /* Parse an (optional) member-specification.
21302 member-specification:
21303 member-declaration member-specification [opt]
21304 access-specifier : member-specification [opt] */
21306 static void
21307 cp_parser_member_specification_opt (cp_parser* parser)
21309 while (true)
21311 cp_token *token;
21312 enum rid keyword;
21314 /* Peek at the next token. */
21315 token = cp_lexer_peek_token (parser->lexer);
21316 /* If it's a `}', or EOF then we've seen all the members. */
21317 if (token->type == CPP_CLOSE_BRACE
21318 || token->type == CPP_EOF
21319 || token->type == CPP_PRAGMA_EOL)
21320 break;
21322 /* See if this token is a keyword. */
21323 keyword = token->keyword;
21324 switch (keyword)
21326 case RID_PUBLIC:
21327 case RID_PROTECTED:
21328 case RID_PRIVATE:
21329 /* Consume the access-specifier. */
21330 cp_lexer_consume_token (parser->lexer);
21331 /* Remember which access-specifier is active. */
21332 current_access_specifier = token->u.value;
21333 /* Look for the `:'. */
21334 cp_parser_require (parser, CPP_COLON, RT_COLON);
21335 break;
21337 default:
21338 /* Accept #pragmas at class scope. */
21339 if (token->type == CPP_PRAGMA)
21341 cp_parser_pragma (parser, pragma_member);
21342 break;
21345 /* Otherwise, the next construction must be a
21346 member-declaration. */
21347 cp_parser_member_declaration (parser);
21352 /* Parse a member-declaration.
21354 member-declaration:
21355 decl-specifier-seq [opt] member-declarator-list [opt] ;
21356 function-definition ; [opt]
21357 :: [opt] nested-name-specifier template [opt] unqualified-id ;
21358 using-declaration
21359 template-declaration
21360 alias-declaration
21362 member-declarator-list:
21363 member-declarator
21364 member-declarator-list , member-declarator
21366 member-declarator:
21367 declarator pure-specifier [opt]
21368 declarator constant-initializer [opt]
21369 identifier [opt] : constant-expression
21371 GNU Extensions:
21373 member-declaration:
21374 __extension__ member-declaration
21376 member-declarator:
21377 declarator attributes [opt] pure-specifier [opt]
21378 declarator attributes [opt] constant-initializer [opt]
21379 identifier [opt] attributes [opt] : constant-expression
21381 C++0x Extensions:
21383 member-declaration:
21384 static_assert-declaration */
21386 static void
21387 cp_parser_member_declaration (cp_parser* parser)
21389 cp_decl_specifier_seq decl_specifiers;
21390 tree prefix_attributes;
21391 tree decl;
21392 int declares_class_or_enum;
21393 bool friend_p;
21394 cp_token *token = NULL;
21395 cp_token *decl_spec_token_start = NULL;
21396 cp_token *initializer_token_start = NULL;
21397 int saved_pedantic;
21398 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21400 /* Check for the `__extension__' keyword. */
21401 if (cp_parser_extension_opt (parser, &saved_pedantic))
21403 /* Recurse. */
21404 cp_parser_member_declaration (parser);
21405 /* Restore the old value of the PEDANTIC flag. */
21406 pedantic = saved_pedantic;
21408 return;
21411 /* Check for a template-declaration. */
21412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21414 /* An explicit specialization here is an error condition, and we
21415 expect the specialization handler to detect and report this. */
21416 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
21417 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
21418 cp_parser_explicit_specialization (parser);
21419 else
21420 cp_parser_template_declaration (parser, /*member_p=*/true);
21422 return;
21425 /* Check for a using-declaration. */
21426 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21428 if (cxx_dialect < cxx11)
21430 /* Parse the using-declaration. */
21431 cp_parser_using_declaration (parser,
21432 /*access_declaration_p=*/false);
21433 return;
21435 else
21437 tree decl;
21438 bool alias_decl_expected;
21439 cp_parser_parse_tentatively (parser);
21440 decl = cp_parser_alias_declaration (parser);
21441 /* Note that if we actually see the '=' token after the
21442 identifier, cp_parser_alias_declaration commits the
21443 tentative parse. In that case, we really expect an
21444 alias-declaration. Otherwise, we expect a using
21445 declaration. */
21446 alias_decl_expected =
21447 !cp_parser_uncommitted_to_tentative_parse_p (parser);
21448 cp_parser_parse_definitely (parser);
21450 if (alias_decl_expected)
21451 finish_member_declaration (decl);
21452 else
21453 cp_parser_using_declaration (parser,
21454 /*access_declaration_p=*/false);
21455 return;
21459 /* Check for @defs. */
21460 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
21462 tree ivar, member;
21463 tree ivar_chains = cp_parser_objc_defs_expression (parser);
21464 ivar = ivar_chains;
21465 while (ivar)
21467 member = ivar;
21468 ivar = TREE_CHAIN (member);
21469 TREE_CHAIN (member) = NULL_TREE;
21470 finish_member_declaration (member);
21472 return;
21475 /* If the next token is `static_assert' we have a static assertion. */
21476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
21478 cp_parser_static_assert (parser, /*member_p=*/true);
21479 return;
21482 /* Tentatively parse for a template since we may have a concept
21483 introduction. */
21484 cp_parser_parse_tentatively (parser);
21485 cp_parser_template_declaration (parser, /*member_p=*/true);
21486 if (cp_parser_parse_definitely (parser))
21487 return;
21489 parser->colon_corrects_to_scope_p = false;
21491 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
21492 goto out;
21494 /* Parse the decl-specifier-seq. */
21495 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21496 cp_parser_decl_specifier_seq (parser,
21497 CP_PARSER_FLAGS_OPTIONAL,
21498 &decl_specifiers,
21499 &declares_class_or_enum);
21500 /* Check for an invalid type-name. */
21501 if (!decl_specifiers.any_type_specifiers_p
21502 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21503 goto out;
21504 /* If there is no declarator, then the decl-specifier-seq should
21505 specify a type. */
21506 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21508 /* If there was no decl-specifier-seq, and the next token is a
21509 `;', then we have something like:
21511 struct S { ; };
21513 [class.mem]
21515 Each member-declaration shall declare at least one member
21516 name of the class. */
21517 if (!decl_specifiers.any_specifiers_p)
21519 cp_token *token = cp_lexer_peek_token (parser->lexer);
21520 if (!in_system_header_at (token->location))
21521 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
21523 else
21525 tree type;
21527 /* See if this declaration is a friend. */
21528 friend_p = cp_parser_friend_p (&decl_specifiers);
21529 /* If there were decl-specifiers, check to see if there was
21530 a class-declaration. */
21531 type = check_tag_decl (&decl_specifiers,
21532 /*explicit_type_instantiation_p=*/false);
21533 /* Nested classes have already been added to the class, but
21534 a `friend' needs to be explicitly registered. */
21535 if (friend_p)
21537 /* If the `friend' keyword was present, the friend must
21538 be introduced with a class-key. */
21539 if (!declares_class_or_enum && cxx_dialect < cxx11)
21540 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
21541 "in C++03 a class-key must be used "
21542 "when declaring a friend");
21543 /* In this case:
21545 template <typename T> struct A {
21546 friend struct A<T>::B;
21549 A<T>::B will be represented by a TYPENAME_TYPE, and
21550 therefore not recognized by check_tag_decl. */
21551 if (!type)
21553 type = decl_specifiers.type;
21554 if (type && TREE_CODE (type) == TYPE_DECL)
21555 type = TREE_TYPE (type);
21557 if (!type || !TYPE_P (type))
21558 error_at (decl_spec_token_start->location,
21559 "friend declaration does not name a class or "
21560 "function");
21561 else
21562 make_friend_class (current_class_type, type,
21563 /*complain=*/true);
21565 /* If there is no TYPE, an error message will already have
21566 been issued. */
21567 else if (!type || type == error_mark_node)
21569 /* An anonymous aggregate has to be handled specially; such
21570 a declaration really declares a data member (with a
21571 particular type), as opposed to a nested class. */
21572 else if (ANON_AGGR_TYPE_P (type))
21574 /* C++11 9.5/6. */
21575 if (decl_specifiers.storage_class != sc_none)
21576 error_at (decl_spec_token_start->location,
21577 "a storage class on an anonymous aggregate "
21578 "in class scope is not allowed");
21580 /* Remove constructors and such from TYPE, now that we
21581 know it is an anonymous aggregate. */
21582 fixup_anonymous_aggr (type);
21583 /* And make the corresponding data member. */
21584 decl = build_decl (decl_spec_token_start->location,
21585 FIELD_DECL, NULL_TREE, type);
21586 /* Add it to the class. */
21587 finish_member_declaration (decl);
21589 else
21590 cp_parser_check_access_in_redeclaration
21591 (TYPE_NAME (type),
21592 decl_spec_token_start->location);
21595 else
21597 bool assume_semicolon = false;
21599 /* Clear attributes from the decl_specifiers but keep them
21600 around as prefix attributes that apply them to the entity
21601 being declared. */
21602 prefix_attributes = decl_specifiers.attributes;
21603 decl_specifiers.attributes = NULL_TREE;
21605 /* See if these declarations will be friends. */
21606 friend_p = cp_parser_friend_p (&decl_specifiers);
21608 /* Keep going until we hit the `;' at the end of the
21609 declaration. */
21610 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21612 tree attributes = NULL_TREE;
21613 tree first_attribute;
21615 /* Peek at the next token. */
21616 token = cp_lexer_peek_token (parser->lexer);
21618 /* Check for a bitfield declaration. */
21619 if (token->type == CPP_COLON
21620 || (token->type == CPP_NAME
21621 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
21622 == CPP_COLON))
21624 tree identifier;
21625 tree width;
21627 /* Get the name of the bitfield. Note that we cannot just
21628 check TOKEN here because it may have been invalidated by
21629 the call to cp_lexer_peek_nth_token above. */
21630 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
21631 identifier = cp_parser_identifier (parser);
21632 else
21633 identifier = NULL_TREE;
21635 /* Consume the `:' token. */
21636 cp_lexer_consume_token (parser->lexer);
21637 /* Get the width of the bitfield. */
21638 width
21639 = cp_parser_constant_expression (parser);
21641 /* Look for attributes that apply to the bitfield. */
21642 attributes = cp_parser_attributes_opt (parser);
21643 /* Remember which attributes are prefix attributes and
21644 which are not. */
21645 first_attribute = attributes;
21646 /* Combine the attributes. */
21647 attributes = chainon (prefix_attributes, attributes);
21649 /* Create the bitfield declaration. */
21650 decl = grokbitfield (identifier
21651 ? make_id_declarator (NULL_TREE,
21652 identifier,
21653 sfk_none)
21654 : NULL,
21655 &decl_specifiers,
21656 width,
21657 attributes);
21659 else
21661 cp_declarator *declarator;
21662 tree initializer;
21663 tree asm_specification;
21664 int ctor_dtor_or_conv_p;
21666 /* Parse the declarator. */
21667 declarator
21668 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21669 &ctor_dtor_or_conv_p,
21670 /*parenthesized_p=*/NULL,
21671 /*member_p=*/true,
21672 friend_p);
21674 /* If something went wrong parsing the declarator, make sure
21675 that we at least consume some tokens. */
21676 if (declarator == cp_error_declarator)
21678 /* Skip to the end of the statement. */
21679 cp_parser_skip_to_end_of_statement (parser);
21680 /* If the next token is not a semicolon, that is
21681 probably because we just skipped over the body of
21682 a function. So, we consume a semicolon if
21683 present, but do not issue an error message if it
21684 is not present. */
21685 if (cp_lexer_next_token_is (parser->lexer,
21686 CPP_SEMICOLON))
21687 cp_lexer_consume_token (parser->lexer);
21688 goto out;
21691 if (declares_class_or_enum & 2)
21692 cp_parser_check_for_definition_in_return_type
21693 (declarator, decl_specifiers.type,
21694 decl_specifiers.locations[ds_type_spec]);
21696 /* Look for an asm-specification. */
21697 asm_specification = cp_parser_asm_specification_opt (parser);
21698 /* Look for attributes that apply to the declaration. */
21699 attributes = cp_parser_attributes_opt (parser);
21700 /* Remember which attributes are prefix attributes and
21701 which are not. */
21702 first_attribute = attributes;
21703 /* Combine the attributes. */
21704 attributes = chainon (prefix_attributes, attributes);
21706 /* If it's an `=', then we have a constant-initializer or a
21707 pure-specifier. It is not correct to parse the
21708 initializer before registering the member declaration
21709 since the member declaration should be in scope while
21710 its initializer is processed. However, the rest of the
21711 front end does not yet provide an interface that allows
21712 us to handle this correctly. */
21713 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21715 /* In [class.mem]:
21717 A pure-specifier shall be used only in the declaration of
21718 a virtual function.
21720 A member-declarator can contain a constant-initializer
21721 only if it declares a static member of integral or
21722 enumeration type.
21724 Therefore, if the DECLARATOR is for a function, we look
21725 for a pure-specifier; otherwise, we look for a
21726 constant-initializer. When we call `grokfield', it will
21727 perform more stringent semantics checks. */
21728 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21729 if (function_declarator_p (declarator)
21730 || (decl_specifiers.type
21731 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21732 && declarator->kind == cdk_id
21733 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21734 == FUNCTION_TYPE)))
21735 initializer = cp_parser_pure_specifier (parser);
21736 else if (decl_specifiers.storage_class != sc_static)
21737 initializer = cp_parser_save_nsdmi (parser);
21738 else if (cxx_dialect >= cxx11)
21740 bool nonconst;
21741 /* Don't require a constant rvalue in C++11, since we
21742 might want a reference constant. We'll enforce
21743 constancy later. */
21744 cp_lexer_consume_token (parser->lexer);
21745 /* Parse the initializer. */
21746 initializer = cp_parser_initializer_clause (parser,
21747 &nonconst);
21749 else
21750 /* Parse the initializer. */
21751 initializer = cp_parser_constant_initializer (parser);
21753 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21754 && !function_declarator_p (declarator))
21756 bool x;
21757 if (decl_specifiers.storage_class != sc_static)
21758 initializer = cp_parser_save_nsdmi (parser);
21759 else
21760 initializer = cp_parser_initializer (parser, &x, &x);
21762 /* Otherwise, there is no initializer. */
21763 else
21764 initializer = NULL_TREE;
21766 /* See if we are probably looking at a function
21767 definition. We are certainly not looking at a
21768 member-declarator. Calling `grokfield' has
21769 side-effects, so we must not do it unless we are sure
21770 that we are looking at a member-declarator. */
21771 if (cp_parser_token_starts_function_definition_p
21772 (cp_lexer_peek_token (parser->lexer)))
21774 /* The grammar does not allow a pure-specifier to be
21775 used when a member function is defined. (It is
21776 possible that this fact is an oversight in the
21777 standard, since a pure function may be defined
21778 outside of the class-specifier. */
21779 if (initializer && initializer_token_start)
21780 error_at (initializer_token_start->location,
21781 "pure-specifier on function-definition");
21782 decl = cp_parser_save_member_function_body (parser,
21783 &decl_specifiers,
21784 declarator,
21785 attributes);
21786 if (parser->fully_implicit_function_template_p)
21787 decl = finish_fully_implicit_template (parser, decl);
21788 /* If the member was not a friend, declare it here. */
21789 if (!friend_p)
21790 finish_member_declaration (decl);
21792 /* Peek at the next token. */
21793 token = cp_lexer_peek_token (parser->lexer);
21794 /* If the next token is a semicolon, consume it. */
21795 if (token->type == CPP_SEMICOLON)
21796 cp_lexer_consume_token (parser->lexer);
21797 goto out;
21799 else
21800 if (declarator->kind == cdk_function)
21801 declarator->id_loc = token->location;
21802 /* Create the declaration. */
21803 decl = grokfield (declarator, &decl_specifiers,
21804 initializer, /*init_const_expr_p=*/true,
21805 asm_specification, attributes);
21806 if (parser->fully_implicit_function_template_p)
21808 if (friend_p)
21809 finish_fully_implicit_template (parser, 0);
21810 else
21811 decl = finish_fully_implicit_template (parser, decl);
21815 cp_finalize_omp_declare_simd (parser, decl);
21817 /* Reset PREFIX_ATTRIBUTES. */
21818 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21819 attributes = TREE_CHAIN (attributes);
21820 if (attributes)
21821 TREE_CHAIN (attributes) = NULL_TREE;
21823 /* If there is any qualification still in effect, clear it
21824 now; we will be starting fresh with the next declarator. */
21825 parser->scope = NULL_TREE;
21826 parser->qualifying_scope = NULL_TREE;
21827 parser->object_scope = NULL_TREE;
21828 /* If it's a `,', then there are more declarators. */
21829 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21831 cp_lexer_consume_token (parser->lexer);
21832 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21834 cp_token *token = cp_lexer_previous_token (parser->lexer);
21835 error_at (token->location,
21836 "stray %<,%> at end of member declaration");
21839 /* If the next token isn't a `;', then we have a parse error. */
21840 else if (cp_lexer_next_token_is_not (parser->lexer,
21841 CPP_SEMICOLON))
21843 /* The next token might be a ways away from where the
21844 actual semicolon is missing. Find the previous token
21845 and use that for our error position. */
21846 cp_token *token = cp_lexer_previous_token (parser->lexer);
21847 error_at (token->location ? token->location : input_location,
21848 "expected %<;%> at end of member declaration");
21850 /* Assume that the user meant to provide a semicolon. If
21851 we were to cp_parser_skip_to_end_of_statement, we might
21852 skip to a semicolon inside a member function definition
21853 and issue nonsensical error messages. */
21854 assume_semicolon = true;
21857 if (decl)
21859 /* Add DECL to the list of members. */
21860 if (!friend_p
21861 /* Explicitly include, eg, NSDMIs, for better error
21862 recovery (c++/58650). */
21863 || !DECL_DECLARES_FUNCTION_P (decl))
21864 finish_member_declaration (decl);
21866 if (TREE_CODE (decl) == FUNCTION_DECL)
21867 cp_parser_save_default_args (parser, decl);
21868 else if (TREE_CODE (decl) == FIELD_DECL
21869 && !DECL_C_BIT_FIELD (decl)
21870 && DECL_INITIAL (decl))
21871 /* Add DECL to the queue of NSDMI to be parsed later. */
21872 vec_safe_push (unparsed_nsdmis, decl);
21875 if (assume_semicolon)
21876 goto out;
21880 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21881 out:
21882 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21885 /* Parse a pure-specifier.
21887 pure-specifier:
21890 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21891 Otherwise, ERROR_MARK_NODE is returned. */
21893 static tree
21894 cp_parser_pure_specifier (cp_parser* parser)
21896 cp_token *token;
21898 /* Look for the `=' token. */
21899 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21900 return error_mark_node;
21901 /* Look for the `0' token. */
21902 token = cp_lexer_peek_token (parser->lexer);
21904 if (token->type == CPP_EOF
21905 || token->type == CPP_PRAGMA_EOL)
21906 return error_mark_node;
21908 cp_lexer_consume_token (parser->lexer);
21910 /* Accept = default or = delete in c++0x mode. */
21911 if (token->keyword == RID_DEFAULT
21912 || token->keyword == RID_DELETE)
21914 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21915 return token->u.value;
21918 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21919 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21921 cp_parser_error (parser,
21922 "invalid pure specifier (only %<= 0%> is allowed)");
21923 cp_parser_skip_to_end_of_statement (parser);
21924 return error_mark_node;
21926 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21928 error_at (token->location, "templates may not be %<virtual%>");
21929 return error_mark_node;
21932 return integer_zero_node;
21935 /* Parse a constant-initializer.
21937 constant-initializer:
21938 = constant-expression
21940 Returns a representation of the constant-expression. */
21942 static tree
21943 cp_parser_constant_initializer (cp_parser* parser)
21945 /* Look for the `=' token. */
21946 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21947 return error_mark_node;
21949 /* It is invalid to write:
21951 struct S { static const int i = { 7 }; };
21954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21956 cp_parser_error (parser,
21957 "a brace-enclosed initializer is not allowed here");
21958 /* Consume the opening brace. */
21959 cp_lexer_consume_token (parser->lexer);
21960 /* Skip the initializer. */
21961 cp_parser_skip_to_closing_brace (parser);
21962 /* Look for the trailing `}'. */
21963 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21965 return error_mark_node;
21968 return cp_parser_constant_expression (parser);
21971 /* Derived classes [gram.class.derived] */
21973 /* Parse a base-clause.
21975 base-clause:
21976 : base-specifier-list
21978 base-specifier-list:
21979 base-specifier ... [opt]
21980 base-specifier-list , base-specifier ... [opt]
21982 Returns a TREE_LIST representing the base-classes, in the order in
21983 which they were declared. The representation of each node is as
21984 described by cp_parser_base_specifier.
21986 In the case that no bases are specified, this function will return
21987 NULL_TREE, not ERROR_MARK_NODE. */
21989 static tree
21990 cp_parser_base_clause (cp_parser* parser)
21992 tree bases = NULL_TREE;
21994 /* Look for the `:' that begins the list. */
21995 cp_parser_require (parser, CPP_COLON, RT_COLON);
21997 /* Scan the base-specifier-list. */
21998 while (true)
22000 cp_token *token;
22001 tree base;
22002 bool pack_expansion_p = false;
22004 /* Look for the base-specifier. */
22005 base = cp_parser_base_specifier (parser);
22006 /* Look for the (optional) ellipsis. */
22007 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22009 /* Consume the `...'. */
22010 cp_lexer_consume_token (parser->lexer);
22012 pack_expansion_p = true;
22015 /* Add BASE to the front of the list. */
22016 if (base && base != error_mark_node)
22018 if (pack_expansion_p)
22019 /* Make this a pack expansion type. */
22020 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22022 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22024 TREE_CHAIN (base) = bases;
22025 bases = base;
22028 /* Peek at the next token. */
22029 token = cp_lexer_peek_token (parser->lexer);
22030 /* If it's not a comma, then the list is complete. */
22031 if (token->type != CPP_COMMA)
22032 break;
22033 /* Consume the `,'. */
22034 cp_lexer_consume_token (parser->lexer);
22037 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22038 base class had a qualified name. However, the next name that
22039 appears is certainly not qualified. */
22040 parser->scope = NULL_TREE;
22041 parser->qualifying_scope = NULL_TREE;
22042 parser->object_scope = NULL_TREE;
22044 return nreverse (bases);
22047 /* Parse a base-specifier.
22049 base-specifier:
22050 :: [opt] nested-name-specifier [opt] class-name
22051 virtual access-specifier [opt] :: [opt] nested-name-specifier
22052 [opt] class-name
22053 access-specifier virtual [opt] :: [opt] nested-name-specifier
22054 [opt] class-name
22056 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22057 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22058 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22059 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22061 static tree
22062 cp_parser_base_specifier (cp_parser* parser)
22064 cp_token *token;
22065 bool done = false;
22066 bool virtual_p = false;
22067 bool duplicate_virtual_error_issued_p = false;
22068 bool duplicate_access_error_issued_p = false;
22069 bool class_scope_p, template_p;
22070 tree access = access_default_node;
22071 tree type;
22073 /* Process the optional `virtual' and `access-specifier'. */
22074 while (!done)
22076 /* Peek at the next token. */
22077 token = cp_lexer_peek_token (parser->lexer);
22078 /* Process `virtual'. */
22079 switch (token->keyword)
22081 case RID_VIRTUAL:
22082 /* If `virtual' appears more than once, issue an error. */
22083 if (virtual_p && !duplicate_virtual_error_issued_p)
22085 cp_parser_error (parser,
22086 "%<virtual%> specified more than once in base-specified");
22087 duplicate_virtual_error_issued_p = true;
22090 virtual_p = true;
22092 /* Consume the `virtual' token. */
22093 cp_lexer_consume_token (parser->lexer);
22095 break;
22097 case RID_PUBLIC:
22098 case RID_PROTECTED:
22099 case RID_PRIVATE:
22100 /* If more than one access specifier appears, issue an
22101 error. */
22102 if (access != access_default_node
22103 && !duplicate_access_error_issued_p)
22105 cp_parser_error (parser,
22106 "more than one access specifier in base-specified");
22107 duplicate_access_error_issued_p = true;
22110 access = ridpointers[(int) token->keyword];
22112 /* Consume the access-specifier. */
22113 cp_lexer_consume_token (parser->lexer);
22115 break;
22117 default:
22118 done = true;
22119 break;
22122 /* It is not uncommon to see programs mechanically, erroneously, use
22123 the 'typename' keyword to denote (dependent) qualified types
22124 as base classes. */
22125 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22127 token = cp_lexer_peek_token (parser->lexer);
22128 if (!processing_template_decl)
22129 error_at (token->location,
22130 "keyword %<typename%> not allowed outside of templates");
22131 else
22132 error_at (token->location,
22133 "keyword %<typename%> not allowed in this context "
22134 "(the base class is implicitly a type)");
22135 cp_lexer_consume_token (parser->lexer);
22138 /* Look for the optional `::' operator. */
22139 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22140 /* Look for the nested-name-specifier. The simplest way to
22141 implement:
22143 [temp.res]
22145 The keyword `typename' is not permitted in a base-specifier or
22146 mem-initializer; in these contexts a qualified name that
22147 depends on a template-parameter is implicitly assumed to be a
22148 type name.
22150 is to pretend that we have seen the `typename' keyword at this
22151 point. */
22152 cp_parser_nested_name_specifier_opt (parser,
22153 /*typename_keyword_p=*/true,
22154 /*check_dependency_p=*/true,
22155 typename_type,
22156 /*is_declaration=*/true);
22157 /* If the base class is given by a qualified name, assume that names
22158 we see are type names or templates, as appropriate. */
22159 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22160 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22162 if (!parser->scope
22163 && cp_lexer_next_token_is_decltype (parser->lexer))
22164 /* DR 950 allows decltype as a base-specifier. */
22165 type = cp_parser_decltype (parser);
22166 else
22168 /* Otherwise, look for the class-name. */
22169 type = cp_parser_class_name (parser,
22170 class_scope_p,
22171 template_p,
22172 typename_type,
22173 /*check_dependency_p=*/true,
22174 /*class_head_p=*/false,
22175 /*is_declaration=*/true);
22176 type = TREE_TYPE (type);
22179 if (type == error_mark_node)
22180 return error_mark_node;
22182 return finish_base_specifier (type, access, virtual_p);
22185 /* Exception handling [gram.exception] */
22187 /* Parse an (optional) noexcept-specification.
22189 noexcept-specification:
22190 noexcept ( constant-expression ) [opt]
22192 If no noexcept-specification is present, returns NULL_TREE.
22193 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22194 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22195 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22196 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22197 in which case a boolean condition is returned instead. */
22199 static tree
22200 cp_parser_noexcept_specification_opt (cp_parser* parser,
22201 bool require_constexpr,
22202 bool* consumed_expr,
22203 bool return_cond)
22205 cp_token *token;
22206 const char *saved_message;
22208 /* Peek at the next token. */
22209 token = cp_lexer_peek_token (parser->lexer);
22211 /* Is it a noexcept-specification? */
22212 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22214 tree expr;
22215 cp_lexer_consume_token (parser->lexer);
22217 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22219 cp_lexer_consume_token (parser->lexer);
22221 if (require_constexpr)
22223 /* Types may not be defined in an exception-specification. */
22224 saved_message = parser->type_definition_forbidden_message;
22225 parser->type_definition_forbidden_message
22226 = G_("types may not be defined in an exception-specification");
22228 expr = cp_parser_constant_expression (parser);
22230 /* Restore the saved message. */
22231 parser->type_definition_forbidden_message = saved_message;
22233 else
22235 expr = cp_parser_expression (parser);
22236 *consumed_expr = true;
22239 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22241 else
22243 expr = boolean_true_node;
22244 if (!require_constexpr)
22245 *consumed_expr = false;
22248 /* We cannot build a noexcept-spec right away because this will check
22249 that expr is a constexpr. */
22250 if (!return_cond)
22251 return build_noexcept_spec (expr, tf_warning_or_error);
22252 else
22253 return expr;
22255 else
22256 return NULL_TREE;
22259 /* Parse an (optional) exception-specification.
22261 exception-specification:
22262 throw ( type-id-list [opt] )
22264 Returns a TREE_LIST representing the exception-specification. The
22265 TREE_VALUE of each node is a type. */
22267 static tree
22268 cp_parser_exception_specification_opt (cp_parser* parser)
22270 cp_token *token;
22271 tree type_id_list;
22272 const char *saved_message;
22274 /* Peek at the next token. */
22275 token = cp_lexer_peek_token (parser->lexer);
22277 /* Is it a noexcept-specification? */
22278 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
22279 false);
22280 if (type_id_list != NULL_TREE)
22281 return type_id_list;
22283 /* If it's not `throw', then there's no exception-specification. */
22284 if (!cp_parser_is_keyword (token, RID_THROW))
22285 return NULL_TREE;
22287 #if 0
22288 /* Enable this once a lot of code has transitioned to noexcept? */
22289 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
22290 warning (OPT_Wdeprecated, "dynamic exception specifications are "
22291 "deprecated in C++0x; use %<noexcept%> instead");
22292 #endif
22294 /* Consume the `throw'. */
22295 cp_lexer_consume_token (parser->lexer);
22297 /* Look for the `('. */
22298 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22300 /* Peek at the next token. */
22301 token = cp_lexer_peek_token (parser->lexer);
22302 /* If it's not a `)', then there is a type-id-list. */
22303 if (token->type != CPP_CLOSE_PAREN)
22305 /* Types may not be defined in an exception-specification. */
22306 saved_message = parser->type_definition_forbidden_message;
22307 parser->type_definition_forbidden_message
22308 = G_("types may not be defined in an exception-specification");
22309 /* Parse the type-id-list. */
22310 type_id_list = cp_parser_type_id_list (parser);
22311 /* Restore the saved message. */
22312 parser->type_definition_forbidden_message = saved_message;
22314 else
22315 type_id_list = empty_except_spec;
22317 /* Look for the `)'. */
22318 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22320 return type_id_list;
22323 /* Parse an (optional) type-id-list.
22325 type-id-list:
22326 type-id ... [opt]
22327 type-id-list , type-id ... [opt]
22329 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
22330 in the order that the types were presented. */
22332 static tree
22333 cp_parser_type_id_list (cp_parser* parser)
22335 tree types = NULL_TREE;
22337 while (true)
22339 cp_token *token;
22340 tree type;
22342 /* Get the next type-id. */
22343 type = cp_parser_type_id (parser);
22344 /* Parse the optional ellipsis. */
22345 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22347 /* Consume the `...'. */
22348 cp_lexer_consume_token (parser->lexer);
22350 /* Turn the type into a pack expansion expression. */
22351 type = make_pack_expansion (type);
22353 /* Add it to the list. */
22354 types = add_exception_specifier (types, type, /*complain=*/1);
22355 /* Peek at the next token. */
22356 token = cp_lexer_peek_token (parser->lexer);
22357 /* If it is not a `,', we are done. */
22358 if (token->type != CPP_COMMA)
22359 break;
22360 /* Consume the `,'. */
22361 cp_lexer_consume_token (parser->lexer);
22364 return nreverse (types);
22367 /* Parse a try-block.
22369 try-block:
22370 try compound-statement handler-seq */
22372 static tree
22373 cp_parser_try_block (cp_parser* parser)
22375 tree try_block;
22377 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
22378 if (parser->in_function_body
22379 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
22380 error ("%<try%> in %<constexpr%> function");
22382 try_block = begin_try_block ();
22383 cp_parser_compound_statement (parser, NULL, true, false);
22384 finish_try_block (try_block);
22385 cp_parser_handler_seq (parser);
22386 finish_handler_sequence (try_block);
22388 return try_block;
22391 /* Parse a function-try-block.
22393 function-try-block:
22394 try ctor-initializer [opt] function-body handler-seq */
22396 static bool
22397 cp_parser_function_try_block (cp_parser* parser)
22399 tree compound_stmt;
22400 tree try_block;
22401 bool ctor_initializer_p;
22403 /* Look for the `try' keyword. */
22404 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
22405 return false;
22406 /* Let the rest of the front end know where we are. */
22407 try_block = begin_function_try_block (&compound_stmt);
22408 /* Parse the function-body. */
22409 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22410 (parser, /*in_function_try_block=*/true);
22411 /* We're done with the `try' part. */
22412 finish_function_try_block (try_block);
22413 /* Parse the handlers. */
22414 cp_parser_handler_seq (parser);
22415 /* We're done with the handlers. */
22416 finish_function_handler_sequence (try_block, compound_stmt);
22418 return ctor_initializer_p;
22421 /* Parse a handler-seq.
22423 handler-seq:
22424 handler handler-seq [opt] */
22426 static void
22427 cp_parser_handler_seq (cp_parser* parser)
22429 while (true)
22431 cp_token *token;
22433 /* Parse the handler. */
22434 cp_parser_handler (parser);
22435 /* Peek at the next token. */
22436 token = cp_lexer_peek_token (parser->lexer);
22437 /* If it's not `catch' then there are no more handlers. */
22438 if (!cp_parser_is_keyword (token, RID_CATCH))
22439 break;
22443 /* Parse a handler.
22445 handler:
22446 catch ( exception-declaration ) compound-statement */
22448 static void
22449 cp_parser_handler (cp_parser* parser)
22451 tree handler;
22452 tree declaration;
22454 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
22455 handler = begin_handler ();
22456 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22457 declaration = cp_parser_exception_declaration (parser);
22458 finish_handler_parms (declaration, handler);
22459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22460 cp_parser_compound_statement (parser, NULL, false, false);
22461 finish_handler (handler);
22464 /* Parse an exception-declaration.
22466 exception-declaration:
22467 type-specifier-seq declarator
22468 type-specifier-seq abstract-declarator
22469 type-specifier-seq
22472 Returns a VAR_DECL for the declaration, or NULL_TREE if the
22473 ellipsis variant is used. */
22475 static tree
22476 cp_parser_exception_declaration (cp_parser* parser)
22478 cp_decl_specifier_seq type_specifiers;
22479 cp_declarator *declarator;
22480 const char *saved_message;
22482 /* If it's an ellipsis, it's easy to handle. */
22483 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22485 /* Consume the `...' token. */
22486 cp_lexer_consume_token (parser->lexer);
22487 return NULL_TREE;
22490 /* Types may not be defined in exception-declarations. */
22491 saved_message = parser->type_definition_forbidden_message;
22492 parser->type_definition_forbidden_message
22493 = G_("types may not be defined in exception-declarations");
22495 /* Parse the type-specifier-seq. */
22496 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22497 /*is_trailing_return=*/false,
22498 &type_specifiers);
22499 /* If it's a `)', then there is no declarator. */
22500 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22501 declarator = NULL;
22502 else
22503 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
22504 /*ctor_dtor_or_conv_p=*/NULL,
22505 /*parenthesized_p=*/NULL,
22506 /*member_p=*/false,
22507 /*friend_p=*/false);
22509 /* Restore the saved message. */
22510 parser->type_definition_forbidden_message = saved_message;
22512 if (!type_specifiers.any_specifiers_p)
22513 return error_mark_node;
22515 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
22518 /* Parse a throw-expression.
22520 throw-expression:
22521 throw assignment-expression [opt]
22523 Returns a THROW_EXPR representing the throw-expression. */
22525 static tree
22526 cp_parser_throw_expression (cp_parser* parser)
22528 tree expression;
22529 cp_token* token;
22531 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
22532 token = cp_lexer_peek_token (parser->lexer);
22533 /* Figure out whether or not there is an assignment-expression
22534 following the "throw" keyword. */
22535 if (token->type == CPP_COMMA
22536 || token->type == CPP_SEMICOLON
22537 || token->type == CPP_CLOSE_PAREN
22538 || token->type == CPP_CLOSE_SQUARE
22539 || token->type == CPP_CLOSE_BRACE
22540 || token->type == CPP_COLON)
22541 expression = NULL_TREE;
22542 else
22543 expression = cp_parser_assignment_expression (parser);
22545 return build_throw (expression);
22548 /* GNU Extensions */
22550 /* Parse an (optional) asm-specification.
22552 asm-specification:
22553 asm ( string-literal )
22555 If the asm-specification is present, returns a STRING_CST
22556 corresponding to the string-literal. Otherwise, returns
22557 NULL_TREE. */
22559 static tree
22560 cp_parser_asm_specification_opt (cp_parser* parser)
22562 cp_token *token;
22563 tree asm_specification;
22565 /* Peek at the next token. */
22566 token = cp_lexer_peek_token (parser->lexer);
22567 /* If the next token isn't the `asm' keyword, then there's no
22568 asm-specification. */
22569 if (!cp_parser_is_keyword (token, RID_ASM))
22570 return NULL_TREE;
22572 /* Consume the `asm' token. */
22573 cp_lexer_consume_token (parser->lexer);
22574 /* Look for the `('. */
22575 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22577 /* Look for the string-literal. */
22578 asm_specification = cp_parser_string_literal (parser, false, false);
22580 /* Look for the `)'. */
22581 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22583 return asm_specification;
22586 /* Parse an asm-operand-list.
22588 asm-operand-list:
22589 asm-operand
22590 asm-operand-list , asm-operand
22592 asm-operand:
22593 string-literal ( expression )
22594 [ string-literal ] string-literal ( expression )
22596 Returns a TREE_LIST representing the operands. The TREE_VALUE of
22597 each node is the expression. The TREE_PURPOSE is itself a
22598 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
22599 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
22600 is a STRING_CST for the string literal before the parenthesis. Returns
22601 ERROR_MARK_NODE if any of the operands are invalid. */
22603 static tree
22604 cp_parser_asm_operand_list (cp_parser* parser)
22606 tree asm_operands = NULL_TREE;
22607 bool invalid_operands = false;
22609 while (true)
22611 tree string_literal;
22612 tree expression;
22613 tree name;
22615 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22617 /* Consume the `[' token. */
22618 cp_lexer_consume_token (parser->lexer);
22619 /* Read the operand name. */
22620 name = cp_parser_identifier (parser);
22621 if (name != error_mark_node)
22622 name = build_string (IDENTIFIER_LENGTH (name),
22623 IDENTIFIER_POINTER (name));
22624 /* Look for the closing `]'. */
22625 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22627 else
22628 name = NULL_TREE;
22629 /* Look for the string-literal. */
22630 string_literal = cp_parser_string_literal (parser, false, false);
22632 /* Look for the `('. */
22633 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22634 /* Parse the expression. */
22635 expression = cp_parser_expression (parser);
22636 /* Look for the `)'. */
22637 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22639 if (name == error_mark_node
22640 || string_literal == error_mark_node
22641 || expression == error_mark_node)
22642 invalid_operands = true;
22644 /* Add this operand to the list. */
22645 asm_operands = tree_cons (build_tree_list (name, string_literal),
22646 expression,
22647 asm_operands);
22648 /* If the next token is not a `,', there are no more
22649 operands. */
22650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22651 break;
22652 /* Consume the `,'. */
22653 cp_lexer_consume_token (parser->lexer);
22656 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22659 /* Parse an asm-clobber-list.
22661 asm-clobber-list:
22662 string-literal
22663 asm-clobber-list , string-literal
22665 Returns a TREE_LIST, indicating the clobbers in the order that they
22666 appeared. The TREE_VALUE of each node is a STRING_CST. */
22668 static tree
22669 cp_parser_asm_clobber_list (cp_parser* parser)
22671 tree clobbers = NULL_TREE;
22673 while (true)
22675 tree string_literal;
22677 /* Look for the string literal. */
22678 string_literal = cp_parser_string_literal (parser, false, false);
22679 /* Add it to the list. */
22680 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22681 /* If the next token is not a `,', then the list is
22682 complete. */
22683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22684 break;
22685 /* Consume the `,' token. */
22686 cp_lexer_consume_token (parser->lexer);
22689 return clobbers;
22692 /* Parse an asm-label-list.
22694 asm-label-list:
22695 identifier
22696 asm-label-list , identifier
22698 Returns a TREE_LIST, indicating the labels in the order that they
22699 appeared. The TREE_VALUE of each node is a label. */
22701 static tree
22702 cp_parser_asm_label_list (cp_parser* parser)
22704 tree labels = NULL_TREE;
22706 while (true)
22708 tree identifier, label, name;
22710 /* Look for the identifier. */
22711 identifier = cp_parser_identifier (parser);
22712 if (!error_operand_p (identifier))
22714 label = lookup_label (identifier);
22715 if (TREE_CODE (label) == LABEL_DECL)
22717 TREE_USED (label) = 1;
22718 check_goto (label);
22719 name = build_string (IDENTIFIER_LENGTH (identifier),
22720 IDENTIFIER_POINTER (identifier));
22721 labels = tree_cons (name, label, labels);
22724 /* If the next token is not a `,', then the list is
22725 complete. */
22726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22727 break;
22728 /* Consume the `,' token. */
22729 cp_lexer_consume_token (parser->lexer);
22732 return nreverse (labels);
22735 /* Return TRUE iff the next tokens in the stream are possibly the
22736 beginning of a GNU extension attribute. */
22738 static bool
22739 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22741 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22744 /* Return TRUE iff the next tokens in the stream are possibly the
22745 beginning of a standard C++-11 attribute specifier. */
22747 static bool
22748 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22750 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22753 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22754 beginning of a standard C++-11 attribute specifier. */
22756 static bool
22757 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22759 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22761 return (cxx_dialect >= cxx11
22762 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22763 || (token->type == CPP_OPEN_SQUARE
22764 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22765 && token->type == CPP_OPEN_SQUARE)));
22768 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22769 beginning of a GNU extension attribute. */
22771 static bool
22772 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22774 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22776 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22779 /* Return true iff the next tokens can be the beginning of either a
22780 GNU attribute list, or a standard C++11 attribute sequence. */
22782 static bool
22783 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22785 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22786 || cp_next_tokens_can_be_std_attribute_p (parser));
22789 /* Return true iff the next Nth tokens can be the beginning of either
22790 a GNU attribute list, or a standard C++11 attribute sequence. */
22792 static bool
22793 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22795 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22796 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22799 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22800 of GNU attributes, or return NULL. */
22802 static tree
22803 cp_parser_attributes_opt (cp_parser *parser)
22805 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22806 return cp_parser_gnu_attributes_opt (parser);
22807 return cp_parser_std_attribute_spec_seq (parser);
22810 #define CILK_SIMD_FN_CLAUSE_MASK \
22811 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22812 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22813 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22814 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22815 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22817 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22818 vector [(<clauses>)] */
22820 static void
22821 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22823 bool first_p = parser->cilk_simd_fn_info == NULL;
22824 cp_token *token = v_token;
22825 if (first_p)
22827 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22828 parser->cilk_simd_fn_info->error_seen = false;
22829 parser->cilk_simd_fn_info->fndecl_seen = false;
22830 parser->cilk_simd_fn_info->tokens = vNULL;
22832 int paren_scope = 0;
22833 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22835 cp_lexer_consume_token (parser->lexer);
22836 v_token = cp_lexer_peek_token (parser->lexer);
22837 paren_scope++;
22839 while (paren_scope > 0)
22841 token = cp_lexer_peek_token (parser->lexer);
22842 if (token->type == CPP_OPEN_PAREN)
22843 paren_scope++;
22844 else if (token->type == CPP_CLOSE_PAREN)
22845 paren_scope--;
22846 /* Do not push the last ')' */
22847 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22848 cp_lexer_consume_token (parser->lexer);
22851 token->type = CPP_PRAGMA_EOL;
22852 parser->lexer->next_token = token;
22853 cp_lexer_consume_token (parser->lexer);
22855 struct cp_token_cache *cp
22856 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22857 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22860 /* Parse an (optional) series of attributes.
22862 attributes:
22863 attributes attribute
22865 attribute:
22866 __attribute__ (( attribute-list [opt] ))
22868 The return value is as for cp_parser_gnu_attribute_list. */
22870 static tree
22871 cp_parser_gnu_attributes_opt (cp_parser* parser)
22873 tree attributes = NULL_TREE;
22875 while (true)
22877 cp_token *token;
22878 tree attribute_list;
22879 bool ok = true;
22881 /* Peek at the next token. */
22882 token = cp_lexer_peek_token (parser->lexer);
22883 /* If it's not `__attribute__', then we're done. */
22884 if (token->keyword != RID_ATTRIBUTE)
22885 break;
22887 /* Consume the `__attribute__' keyword. */
22888 cp_lexer_consume_token (parser->lexer);
22889 /* Look for the two `(' tokens. */
22890 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22891 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22893 /* Peek at the next token. */
22894 token = cp_lexer_peek_token (parser->lexer);
22895 if (token->type != CPP_CLOSE_PAREN)
22896 /* Parse the attribute-list. */
22897 attribute_list = cp_parser_gnu_attribute_list (parser);
22898 else
22899 /* If the next token is a `)', then there is no attribute
22900 list. */
22901 attribute_list = NULL;
22903 /* Look for the two `)' tokens. */
22904 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22905 ok = false;
22906 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22907 ok = false;
22908 if (!ok)
22909 cp_parser_skip_to_end_of_statement (parser);
22911 /* Add these new attributes to the list. */
22912 attributes = chainon (attributes, attribute_list);
22915 return attributes;
22918 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22919 "__vector" or "__vector__." */
22921 static inline bool
22922 is_cilkplus_vector_p (tree name)
22924 if (flag_cilkplus && is_attribute_p ("vector", name))
22925 return true;
22926 return false;
22929 /* Parse a GNU attribute-list.
22931 attribute-list:
22932 attribute
22933 attribute-list , attribute
22935 attribute:
22936 identifier
22937 identifier ( identifier )
22938 identifier ( identifier , expression-list )
22939 identifier ( expression-list )
22941 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22942 to an attribute. The TREE_PURPOSE of each node is the identifier
22943 indicating which attribute is in use. The TREE_VALUE represents
22944 the arguments, if any. */
22946 static tree
22947 cp_parser_gnu_attribute_list (cp_parser* parser)
22949 tree attribute_list = NULL_TREE;
22950 bool save_translate_strings_p = parser->translate_strings_p;
22952 parser->translate_strings_p = false;
22953 while (true)
22955 cp_token *token;
22956 tree identifier;
22957 tree attribute;
22959 /* Look for the identifier. We also allow keywords here; for
22960 example `__attribute__ ((const))' is legal. */
22961 token = cp_lexer_peek_token (parser->lexer);
22962 if (token->type == CPP_NAME
22963 || token->type == CPP_KEYWORD)
22965 tree arguments = NULL_TREE;
22967 /* Consume the token, but save it since we need it for the
22968 SIMD enabled function parsing. */
22969 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22971 /* Save away the identifier that indicates which attribute
22972 this is. */
22973 identifier = (token->type == CPP_KEYWORD)
22974 /* For keywords, use the canonical spelling, not the
22975 parsed identifier. */
22976 ? ridpointers[(int) token->keyword]
22977 : id_token->u.value;
22979 attribute = build_tree_list (identifier, NULL_TREE);
22981 /* Peek at the next token. */
22982 token = cp_lexer_peek_token (parser->lexer);
22983 /* If it's an `(', then parse the attribute arguments. */
22984 if (token->type == CPP_OPEN_PAREN)
22986 vec<tree, va_gc> *vec;
22987 int attr_flag = (attribute_takes_identifier_p (identifier)
22988 ? id_attr : normal_attr);
22989 if (is_cilkplus_vector_p (identifier))
22991 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22992 continue;
22994 else
22995 vec = cp_parser_parenthesized_expression_list
22996 (parser, attr_flag, /*cast_p=*/false,
22997 /*allow_expansion_p=*/false,
22998 /*non_constant_p=*/NULL);
22999 if (vec == NULL)
23000 arguments = error_mark_node;
23001 else
23003 arguments = build_tree_list_vec (vec);
23004 release_tree_vector (vec);
23006 /* Save the arguments away. */
23007 TREE_VALUE (attribute) = arguments;
23009 else if (is_cilkplus_vector_p (identifier))
23011 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23012 continue;
23015 if (arguments != error_mark_node)
23017 /* Add this attribute to the list. */
23018 TREE_CHAIN (attribute) = attribute_list;
23019 attribute_list = attribute;
23022 token = cp_lexer_peek_token (parser->lexer);
23024 /* Now, look for more attributes. If the next token isn't a
23025 `,', we're done. */
23026 if (token->type != CPP_COMMA)
23027 break;
23029 /* Consume the comma and keep going. */
23030 cp_lexer_consume_token (parser->lexer);
23032 parser->translate_strings_p = save_translate_strings_p;
23034 /* We built up the list in reverse order. */
23035 return nreverse (attribute_list);
23038 /* Parse a standard C++11 attribute.
23040 The returned representation is a TREE_LIST which TREE_PURPOSE is
23041 the scoped name of the attribute, and the TREE_VALUE is its
23042 arguments list.
23044 Note that the scoped name of the attribute is itself a TREE_LIST
23045 which TREE_PURPOSE is the namespace of the attribute, and
23046 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23047 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23048 and which TREE_PURPOSE is directly the attribute name.
23050 Clients of the attribute code should use get_attribute_namespace
23051 and get_attribute_name to get the actual namespace and name of
23052 attributes, regardless of their being GNU or C++11 attributes.
23054 attribute:
23055 attribute-token attribute-argument-clause [opt]
23057 attribute-token:
23058 identifier
23059 attribute-scoped-token
23061 attribute-scoped-token:
23062 attribute-namespace :: identifier
23064 attribute-namespace:
23065 identifier
23067 attribute-argument-clause:
23068 ( balanced-token-seq )
23070 balanced-token-seq:
23071 balanced-token [opt]
23072 balanced-token-seq balanced-token
23074 balanced-token:
23075 ( balanced-token-seq )
23076 [ balanced-token-seq ]
23077 { balanced-token-seq }. */
23079 static tree
23080 cp_parser_std_attribute (cp_parser *parser)
23082 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23083 cp_token *token;
23085 /* First, parse name of the the attribute, a.k.a
23086 attribute-token. */
23088 token = cp_lexer_peek_token (parser->lexer);
23089 if (token->type == CPP_NAME)
23090 attr_id = token->u.value;
23091 else if (token->type == CPP_KEYWORD)
23092 attr_id = ridpointers[(int) token->keyword];
23093 else if (token->flags & NAMED_OP)
23094 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23096 if (attr_id == NULL_TREE)
23097 return NULL_TREE;
23099 cp_lexer_consume_token (parser->lexer);
23101 token = cp_lexer_peek_token (parser->lexer);
23102 if (token->type == CPP_SCOPE)
23104 /* We are seeing a scoped attribute token. */
23106 cp_lexer_consume_token (parser->lexer);
23107 attr_ns = attr_id;
23109 token = cp_lexer_consume_token (parser->lexer);
23110 if (token->type == CPP_NAME)
23111 attr_id = token->u.value;
23112 else if (token->type == CPP_KEYWORD)
23113 attr_id = ridpointers[(int) token->keyword];
23114 else
23116 error_at (token->location,
23117 "expected an identifier for the attribute name");
23118 return error_mark_node;
23120 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23121 NULL_TREE);
23122 token = cp_lexer_peek_token (parser->lexer);
23124 else
23126 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23127 NULL_TREE);
23128 /* C++11 noreturn attribute is equivalent to GNU's. */
23129 if (is_attribute_p ("noreturn", attr_id))
23130 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23131 /* C++14 deprecated attribute is equivalent to GNU's. */
23132 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23134 if (cxx_dialect == cxx11)
23135 pedwarn (token->location, OPT_Wpedantic,
23136 "%<deprecated%> is a C++14 feature;"
23137 " use %<gnu::deprecated%>");
23138 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23142 /* Now parse the optional argument clause of the attribute. */
23144 if (token->type != CPP_OPEN_PAREN)
23145 return attribute;
23148 vec<tree, va_gc> *vec;
23149 int attr_flag = normal_attr;
23151 if (attr_ns == get_identifier ("gnu")
23152 && attribute_takes_identifier_p (attr_id))
23153 /* A GNU attribute that takes an identifier in parameter. */
23154 attr_flag = id_attr;
23156 vec = cp_parser_parenthesized_expression_list
23157 (parser, attr_flag, /*cast_p=*/false,
23158 /*allow_expansion_p=*/true,
23159 /*non_constant_p=*/NULL);
23160 if (vec == NULL)
23161 arguments = error_mark_node;
23162 else
23164 arguments = build_tree_list_vec (vec);
23165 release_tree_vector (vec);
23168 if (arguments == error_mark_node)
23169 attribute = error_mark_node;
23170 else
23171 TREE_VALUE (attribute) = arguments;
23174 return attribute;
23177 /* Parse a list of standard C++-11 attributes.
23179 attribute-list:
23180 attribute [opt]
23181 attribute-list , attribute[opt]
23182 attribute ...
23183 attribute-list , attribute ...
23186 static tree
23187 cp_parser_std_attribute_list (cp_parser *parser)
23189 tree attributes = NULL_TREE, attribute = NULL_TREE;
23190 cp_token *token = NULL;
23192 while (true)
23194 attribute = cp_parser_std_attribute (parser);
23195 if (attribute == error_mark_node)
23196 break;
23197 if (attribute != NULL_TREE)
23199 TREE_CHAIN (attribute) = attributes;
23200 attributes = attribute;
23202 token = cp_lexer_peek_token (parser->lexer);
23203 if (token->type != CPP_COMMA)
23204 break;
23205 cp_lexer_consume_token (parser->lexer);
23207 attributes = nreverse (attributes);
23208 return attributes;
23211 /* Parse a standard C++-11 attribute specifier.
23213 attribute-specifier:
23214 [ [ attribute-list ] ]
23215 alignment-specifier
23217 alignment-specifier:
23218 alignas ( type-id ... [opt] )
23219 alignas ( alignment-expression ... [opt] ). */
23221 static tree
23222 cp_parser_std_attribute_spec (cp_parser *parser)
23224 tree attributes = NULL_TREE;
23225 cp_token *token = cp_lexer_peek_token (parser->lexer);
23227 if (token->type == CPP_OPEN_SQUARE
23228 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
23230 cp_lexer_consume_token (parser->lexer);
23231 cp_lexer_consume_token (parser->lexer);
23233 attributes = cp_parser_std_attribute_list (parser);
23235 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
23236 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23237 cp_parser_skip_to_end_of_statement (parser);
23238 else
23239 /* Warn about parsing c++11 attribute in non-c++1 mode, only
23240 when we are sure that we have actually parsed them. */
23241 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23243 else
23245 tree alignas_expr;
23247 /* Look for an alignment-specifier. */
23249 token = cp_lexer_peek_token (parser->lexer);
23251 if (token->type != CPP_KEYWORD
23252 || token->keyword != RID_ALIGNAS)
23253 return NULL_TREE;
23255 cp_lexer_consume_token (parser->lexer);
23256 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23258 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
23260 cp_parser_error (parser, "expected %<(%>");
23261 return error_mark_node;
23264 cp_parser_parse_tentatively (parser);
23265 alignas_expr = cp_parser_type_id (parser);
23267 if (!cp_parser_parse_definitely (parser))
23269 gcc_assert (alignas_expr == error_mark_node
23270 || alignas_expr == NULL_TREE);
23272 alignas_expr =
23273 cp_parser_assignment_expression (parser);
23274 if (alignas_expr == error_mark_node)
23275 cp_parser_skip_to_end_of_statement (parser);
23276 if (alignas_expr == NULL_TREE
23277 || alignas_expr == error_mark_node)
23278 return alignas_expr;
23281 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
23283 cp_parser_error (parser, "expected %<)%>");
23284 return error_mark_node;
23287 alignas_expr = cxx_alignas_expr (alignas_expr);
23289 /* Build the C++-11 representation of an 'aligned'
23290 attribute. */
23291 attributes =
23292 build_tree_list (build_tree_list (get_identifier ("gnu"),
23293 get_identifier ("aligned")),
23294 build_tree_list (NULL_TREE, alignas_expr));
23297 return attributes;
23300 /* Parse a standard C++-11 attribute-specifier-seq.
23302 attribute-specifier-seq:
23303 attribute-specifier-seq [opt] attribute-specifier
23306 static tree
23307 cp_parser_std_attribute_spec_seq (cp_parser *parser)
23309 tree attr_specs = NULL;
23311 while (true)
23313 tree attr_spec = cp_parser_std_attribute_spec (parser);
23314 if (attr_spec == NULL_TREE)
23315 break;
23316 if (attr_spec == error_mark_node)
23317 return error_mark_node;
23319 TREE_CHAIN (attr_spec) = attr_specs;
23320 attr_specs = attr_spec;
23323 attr_specs = nreverse (attr_specs);
23324 return attr_specs;
23327 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
23328 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
23329 current value of the PEDANTIC flag, regardless of whether or not
23330 the `__extension__' keyword is present. The caller is responsible
23331 for restoring the value of the PEDANTIC flag. */
23333 static bool
23334 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
23336 /* Save the old value of the PEDANTIC flag. */
23337 *saved_pedantic = pedantic;
23339 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
23341 /* Consume the `__extension__' token. */
23342 cp_lexer_consume_token (parser->lexer);
23343 /* We're not being pedantic while the `__extension__' keyword is
23344 in effect. */
23345 pedantic = 0;
23347 return true;
23350 return false;
23353 /* Parse a label declaration.
23355 label-declaration:
23356 __label__ label-declarator-seq ;
23358 label-declarator-seq:
23359 identifier , label-declarator-seq
23360 identifier */
23362 static void
23363 cp_parser_label_declaration (cp_parser* parser)
23365 /* Look for the `__label__' keyword. */
23366 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
23368 while (true)
23370 tree identifier;
23372 /* Look for an identifier. */
23373 identifier = cp_parser_identifier (parser);
23374 /* If we failed, stop. */
23375 if (identifier == error_mark_node)
23376 break;
23377 /* Declare it as a label. */
23378 finish_label_decl (identifier);
23379 /* If the next token is a `;', stop. */
23380 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23381 break;
23382 /* Look for the `,' separating the label declarations. */
23383 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
23386 /* Look for the final `;'. */
23387 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23390 // -------------------------------------------------------------------------- //
23391 // Requires Clause
23393 // Parse a requires clause.
23395 // requires-clause:
23396 // 'requires' logical-or-expression
23398 // The required logical-or-expression must be a constant expression. Note
23399 // that we don't check that the expression is constepxr here. We defer until
23400 // we analyze constraints and then, we only check atomic constraints.
23401 static tree
23402 cp_parser_requires_clause (cp_parser *parser)
23404 // Parse the requires clause so that it is not automatically folded.
23405 ++processing_template_decl;
23406 tree expr =
23407 cp_parser_binary_expression (parser, false, false, PREC_NOT_OPERATOR, NULL);
23408 --processing_template_decl;
23409 return expr;
23412 // Optionally parse a requires clause:
23414 // requires-clause:
23415 // 'requires' logical-or-expression
23417 // The required logical-or-expression must be a constant expression.
23418 static tree
23419 cp_parser_requires_clause_opt (cp_parser *parser)
23421 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23422 return NULL_TREE;
23423 cp_lexer_consume_token (parser->lexer);
23424 return cp_parser_requires_clause (parser);
23428 /*---------------------------------------------------------------------------
23429 Requires expressions
23430 ---------------------------------------------------------------------------*/
23432 /* Parse a requires expression
23434 requirement-expression:
23435 'requires' requirement-parameter-list [opt] requirement-body */
23436 static tree
23437 cp_parser_requires_expression (cp_parser *parser)
23439 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
23440 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
23442 /* A requires-expression shall appear only within a concept
23443 definition or a requires-clause.
23445 TODO: Implement this diagnostic correctly. */
23446 if (!processing_template_decl)
23448 error_at (loc, "a requires expression cannot appear outside a template");
23449 cp_parser_skip_to_end_of_statement (parser);
23450 return error_mark_node;
23453 /* Local parameters are delared as variables within the scope
23454 of the expression. They are not visible past the end of
23455 the expression. Expressions within the requires-expression
23456 are unevaluated. */
23457 struct scope_sentinel
23459 scope_sentinel ()
23461 ++cp_unevaluated_operand;
23462 begin_scope (sk_block, NULL_TREE);
23465 ~scope_sentinel ()
23467 pop_bindings_and_leave_scope ();
23468 --cp_unevaluated_operand;
23470 } s;
23472 /* Parse the optional parameter list. */
23473 tree parms = NULL_TREE;
23474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23476 parms = cp_parser_requirement_parameter_list (parser);
23477 if (parms == error_mark_node)
23478 return error_mark_node;
23481 /* Parse the requirement body. */
23482 tree reqs = cp_parser_requirement_body (parser);
23483 if (reqs == error_mark_node)
23484 return error_mark_node;
23486 return finish_requires_expr (parms, reqs);
23489 /* Parse a parameterized requirement.
23491 requirement-parameter-list:
23492 '(' parameter-declaration-clause ')' */
23493 static tree
23494 cp_parser_requirement_parameter_list (cp_parser *parser)
23496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23497 return error_mark_node;
23499 tree parms = cp_parser_parameter_declaration_clause (parser);
23500 if (parms == error_mark_node)
23501 return error_mark_node;
23503 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23504 return error_mark_node;
23506 return parms;
23509 /* Parse the body of a requirement.
23511 requirement-body:
23512 '{' requirement-list '}' */
23513 static tree
23514 cp_parser_requirement_body (cp_parser *parser)
23516 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23517 return error_mark_node;
23519 tree reqs = cp_parser_requirement_list (parser);
23521 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23522 return error_mark_node;
23524 return reqs;
23527 /* Parse a list of requirements.
23529 requirement-list:
23530 requirement
23531 requirement-list ';' requirement[opt] */
23532 static tree
23533 cp_parser_requirement_list (cp_parser *parser)
23535 tree result = NULL_TREE;
23536 while (true)
23538 tree req = cp_parser_requirement (parser);
23539 if (req == error_mark_node)
23540 return error_mark_node;
23542 result = tree_cons (NULL_TREE, req, result);
23544 /* If we see a semi-colon, consume it. */
23545 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23546 cp_lexer_consume_token (parser->lexer);
23548 /* Stop processing at the end of the list. */
23549 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23550 break;
23553 /* Reverse the order of requirements so they are analyzed in
23554 declaration order. */
23555 return nreverse (result);
23558 /* Parse a syntactic requirement or type requirement.
23560 requirement:
23561 simple-requirement
23562 compound-requirement
23563 type-requirement
23564 nested-requirement */
23565 static tree
23566 cp_parser_requirement (cp_parser *parser)
23568 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23569 return cp_parser_compound_requirement (parser);
23570 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23571 return cp_parser_type_requirement (parser);
23572 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23573 return cp_parser_nested_requirement (parser);
23574 else
23575 return cp_parser_simple_requirement (parser);
23578 /* Parse a simple requirement.
23580 simple-requirement:
23581 expression ';' */
23582 static tree
23583 cp_parser_simple_requirement (cp_parser *parser)
23585 tree expr = cp_parser_expression (parser, NULL, false, false);
23586 if (!expr || expr == error_mark_node)
23587 return error_mark_node;
23589 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23590 return error_mark_node;
23592 return finish_simple_requirement (expr);
23595 /* Parse a type requirement
23597 type-requirement
23598 nested-name-specifier [opt] required-type-name ';'
23600 required-type-name:
23601 type-name
23602 'template' [opt] simple-template-id */
23603 static tree
23604 cp_parser_type_requirement (cp_parser *parser)
23606 cp_lexer_consume_token (parser->lexer);
23608 // Save the scope before parsing name specifiers.
23609 tree saved_scope = parser->scope;
23610 tree saved_object_scope = parser->object_scope;
23611 tree saved_qualifying_scope = parser->qualifying_scope;
23612 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
23613 cp_parser_nested_name_specifier_opt (parser,
23614 /*typename_keyword_p=*/true,
23615 /*check_dependency_p=*/false,
23616 /*type_p=*/true,
23617 /*is_declaration=*/false);
23619 tree type;
23620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23622 cp_lexer_consume_token (parser->lexer);
23623 type = cp_parser_template_id (parser,
23624 /*template_keyword_p=*/true,
23625 /*check_dependency=*/false,
23626 /*tag_type=*/none_type,
23627 /*is_declaration=*/false);
23628 type = make_typename_type (parser->scope, type, typename_type,
23629 /*complain=*/tf_error);
23631 else
23632 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
23634 /* If we got an alias template, unwrap its aliased type. */
23635 if (DECL_P(type) && TYPE_DECL_ALIAS_P (type))
23636 type = TREE_TYPE (type);
23638 parser->scope = saved_scope;
23639 parser->object_scope = saved_object_scope;
23640 parser->qualifying_scope = saved_qualifying_scope;
23642 if (type == error_mark_node)
23643 cp_parser_skip_to_end_of_statement (parser);
23645 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23646 return error_mark_node;
23647 if (type == error_mark_node)
23648 return error_mark_node;
23650 return finish_type_requirement (type);
23653 /* Parse a compound requirement
23655 compound-requirement:
23656 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
23657 static tree
23658 cp_parser_compound_requirement (cp_parser *parser)
23660 /* Parse an expression enclosed in '{ }'s. */
23661 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23662 return error_mark_node;
23664 tree expr = cp_parser_expression (parser, NULL, false, false);
23665 if (!expr || expr == error_mark_node)
23666 return error_mark_node;
23668 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23669 return error_mark_node;
23671 /* Parse the optional noexcept. */
23672 bool noexcept_p = false;
23673 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
23675 cp_lexer_consume_token (parser->lexer);
23676 noexcept_p = true;
23679 /* Parse the optional trailing return type. */
23680 tree type = NULL_TREE;
23681 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
23683 cp_lexer_consume_token (parser->lexer);
23684 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
23685 parser->in_result_type_constraint_p = true;
23686 type = cp_parser_trailing_type_id (parser);
23687 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
23688 if (type == error_mark_node)
23689 return error_mark_node;
23692 return finish_compound_requirement (expr, type, noexcept_p);
23695 /* Parse a nested requirement. This is the same as a requires clause.
23697 nested-requirement:
23698 requires-clause */
23699 static tree
23700 cp_parser_nested_requirement (cp_parser *parser)
23702 cp_lexer_consume_token (parser->lexer);
23703 tree req = cp_parser_requires_clause (parser);
23704 if (req == error_mark_node)
23705 return error_mark_node;
23706 return finish_nested_requirement (req);
23709 /* Support Functions */
23711 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
23712 NAME should have one of the representations used for an
23713 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
23714 is returned. If PARSER->SCOPE is a dependent type, then a
23715 SCOPE_REF is returned.
23717 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
23718 returned; the name was already resolved when the TEMPLATE_ID_EXPR
23719 was formed. Abstractly, such entities should not be passed to this
23720 function, because they do not need to be looked up, but it is
23721 simpler to check for this special case here, rather than at the
23722 call-sites.
23724 In cases not explicitly covered above, this function returns a
23725 DECL, OVERLOAD, or baselink representing the result of the lookup.
23726 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
23727 is returned.
23729 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
23730 (e.g., "struct") that was used. In that case bindings that do not
23731 refer to types are ignored.
23733 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
23734 ignored.
23736 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
23737 are ignored.
23739 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
23740 types.
23742 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
23743 TREE_LIST of candidates if name-lookup results in an ambiguity, and
23744 NULL_TREE otherwise. */
23746 static tree
23747 cp_parser_lookup_name (cp_parser *parser, tree name,
23748 enum tag_types tag_type,
23749 bool is_template,
23750 bool is_namespace,
23751 bool check_dependency,
23752 tree *ambiguous_decls,
23753 location_t name_location)
23755 tree decl;
23756 tree object_type = parser->context->object_type;
23758 /* Assume that the lookup will be unambiguous. */
23759 if (ambiguous_decls)
23760 *ambiguous_decls = NULL_TREE;
23762 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
23763 no longer valid. Note that if we are parsing tentatively, and
23764 the parse fails, OBJECT_TYPE will be automatically restored. */
23765 parser->context->object_type = NULL_TREE;
23767 if (name == error_mark_node)
23768 return error_mark_node;
23770 /* A template-id has already been resolved; there is no lookup to
23771 do. */
23772 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
23773 return name;
23774 if (BASELINK_P (name))
23776 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
23777 == TEMPLATE_ID_EXPR);
23778 return name;
23781 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
23782 it should already have been checked to make sure that the name
23783 used matches the type being destroyed. */
23784 if (TREE_CODE (name) == BIT_NOT_EXPR)
23786 tree type;
23788 /* Figure out to which type this destructor applies. */
23789 if (parser->scope)
23790 type = parser->scope;
23791 else if (object_type)
23792 type = object_type;
23793 else
23794 type = current_class_type;
23795 /* If that's not a class type, there is no destructor. */
23796 if (!type || !CLASS_TYPE_P (type))
23797 return error_mark_node;
23798 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
23799 lazily_declare_fn (sfk_destructor, type);
23800 if (!CLASSTYPE_DESTRUCTORS (type))
23801 return error_mark_node;
23802 /* If it was a class type, return the destructor. */
23803 return CLASSTYPE_DESTRUCTORS (type);
23806 /* By this point, the NAME should be an ordinary identifier. If
23807 the id-expression was a qualified name, the qualifying scope is
23808 stored in PARSER->SCOPE at this point. */
23809 gcc_assert (identifier_p (name));
23811 /* Perform the lookup. */
23812 if (parser->scope)
23814 bool dependent_p;
23816 if (parser->scope == error_mark_node)
23817 return error_mark_node;
23819 /* If the SCOPE is dependent, the lookup must be deferred until
23820 the template is instantiated -- unless we are explicitly
23821 looking up names in uninstantiated templates. Even then, we
23822 cannot look up the name if the scope is not a class type; it
23823 might, for example, be a template type parameter. */
23824 dependent_p = (TYPE_P (parser->scope)
23825 && dependent_scope_p (parser->scope));
23826 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
23827 && dependent_p)
23828 /* Defer lookup. */
23829 decl = error_mark_node;
23830 else
23832 tree pushed_scope = NULL_TREE;
23834 /* If PARSER->SCOPE is a dependent type, then it must be a
23835 class type, and we must not be checking dependencies;
23836 otherwise, we would have processed this lookup above. So
23837 that PARSER->SCOPE is not considered a dependent base by
23838 lookup_member, we must enter the scope here. */
23839 if (dependent_p)
23840 pushed_scope = push_scope (parser->scope);
23842 /* If the PARSER->SCOPE is a template specialization, it
23843 may be instantiated during name lookup. In that case,
23844 errors may be issued. Even if we rollback the current
23845 tentative parse, those errors are valid. */
23846 decl = lookup_qualified_name (parser->scope, name,
23847 tag_type != none_type,
23848 /*complain=*/true);
23850 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
23851 lookup result and the nested-name-specifier nominates a class C:
23852 * if the name specified after the nested-name-specifier, when
23853 looked up in C, is the injected-class-name of C (Clause 9), or
23854 * if the name specified after the nested-name-specifier is the
23855 same as the identifier or the simple-template-id's template-
23856 name in the last component of the nested-name-specifier,
23857 the name is instead considered to name the constructor of
23858 class C. [ Note: for example, the constructor is not an
23859 acceptable lookup result in an elaborated-type-specifier so
23860 the constructor would not be used in place of the
23861 injected-class-name. --end note ] Such a constructor name
23862 shall be used only in the declarator-id of a declaration that
23863 names a constructor or in a using-declaration. */
23864 if (tag_type == none_type
23865 && DECL_SELF_REFERENCE_P (decl)
23866 && same_type_p (DECL_CONTEXT (decl), parser->scope))
23867 decl = lookup_qualified_name (parser->scope, ctor_identifier,
23868 tag_type != none_type,
23869 /*complain=*/true);
23871 /* If we have a single function from a using decl, pull it out. */
23872 if (TREE_CODE (decl) == OVERLOAD
23873 && !really_overloaded_fn (decl))
23874 decl = OVL_FUNCTION (decl);
23876 if (pushed_scope)
23877 pop_scope (pushed_scope);
23880 /* If the scope is a dependent type and either we deferred lookup or
23881 we did lookup but didn't find the name, rememeber the name. */
23882 if (decl == error_mark_node && TYPE_P (parser->scope)
23883 && dependent_type_p (parser->scope))
23885 if (tag_type)
23887 tree type;
23889 /* The resolution to Core Issue 180 says that `struct
23890 A::B' should be considered a type-name, even if `A'
23891 is dependent. */
23892 type = make_typename_type (parser->scope, name, tag_type,
23893 /*complain=*/tf_error);
23894 if (type != error_mark_node)
23895 decl = TYPE_NAME (type);
23897 else if (is_template
23898 && (cp_parser_next_token_ends_template_argument_p (parser)
23899 || cp_lexer_next_token_is (parser->lexer,
23900 CPP_CLOSE_PAREN)))
23901 decl = make_unbound_class_template (parser->scope,
23902 name, NULL_TREE,
23903 /*complain=*/tf_error);
23904 else
23905 decl = build_qualified_name (/*type=*/NULL_TREE,
23906 parser->scope, name,
23907 is_template);
23909 parser->qualifying_scope = parser->scope;
23910 parser->object_scope = NULL_TREE;
23912 else if (object_type)
23914 /* Look up the name in the scope of the OBJECT_TYPE, unless the
23915 OBJECT_TYPE is not a class. */
23916 if (CLASS_TYPE_P (object_type))
23917 /* If the OBJECT_TYPE is a template specialization, it may
23918 be instantiated during name lookup. In that case, errors
23919 may be issued. Even if we rollback the current tentative
23920 parse, those errors are valid. */
23921 decl = lookup_member (object_type,
23922 name,
23923 /*protect=*/0,
23924 tag_type != none_type,
23925 tf_warning_or_error);
23926 else
23927 decl = NULL_TREE;
23929 if (!decl)
23930 /* Look it up in the enclosing context. */
23931 decl = lookup_name_real (name, tag_type != none_type,
23932 /*nonclass=*/0,
23933 /*block_p=*/true, is_namespace, 0);
23934 parser->object_scope = object_type;
23935 parser->qualifying_scope = NULL_TREE;
23937 else
23939 decl = lookup_name_real (name, tag_type != none_type,
23940 /*nonclass=*/0,
23941 /*block_p=*/true, is_namespace, 0);
23942 parser->qualifying_scope = NULL_TREE;
23943 parser->object_scope = NULL_TREE;
23946 /* If the lookup failed, let our caller know. */
23947 if (!decl || decl == error_mark_node)
23948 return error_mark_node;
23950 /* Pull out the template from an injected-class-name (or multiple). */
23951 if (is_template)
23952 decl = maybe_get_template_decl_from_type_decl (decl);
23954 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23955 if (TREE_CODE (decl) == TREE_LIST)
23957 if (ambiguous_decls)
23958 *ambiguous_decls = decl;
23959 /* The error message we have to print is too complicated for
23960 cp_parser_error, so we incorporate its actions directly. */
23961 if (!cp_parser_simulate_error (parser))
23963 error_at (name_location, "reference to %qD is ambiguous",
23964 name);
23965 print_candidates (decl);
23967 return error_mark_node;
23970 gcc_assert (DECL_P (decl)
23971 || TREE_CODE (decl) == OVERLOAD
23972 || TREE_CODE (decl) == SCOPE_REF
23973 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23974 || BASELINK_P (decl));
23976 /* If we have resolved the name of a member declaration, check to
23977 see if the declaration is accessible. When the name resolves to
23978 set of overloaded functions, accessibility is checked when
23979 overload resolution is done.
23981 During an explicit instantiation, access is not checked at all,
23982 as per [temp.explicit]. */
23983 if (DECL_P (decl))
23984 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23986 maybe_record_typedef_use (decl);
23988 return decl;
23991 /* Like cp_parser_lookup_name, but for use in the typical case where
23992 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23993 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23995 static tree
23996 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23998 return cp_parser_lookup_name (parser, name,
23999 none_type,
24000 /*is_template=*/false,
24001 /*is_namespace=*/false,
24002 /*check_dependency=*/true,
24003 /*ambiguous_decls=*/NULL,
24004 location);
24007 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24008 the current context, return the TYPE_DECL. If TAG_NAME_P is
24009 true, the DECL indicates the class being defined in a class-head,
24010 or declared in an elaborated-type-specifier.
24012 Otherwise, return DECL. */
24014 static tree
24015 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24017 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24018 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24020 struct A {
24021 template <typename T> struct B;
24024 template <typename T> struct A::B {};
24026 Similarly, in an elaborated-type-specifier:
24028 namespace N { struct X{}; }
24030 struct A {
24031 template <typename T> friend struct N::X;
24034 However, if the DECL refers to a class type, and we are in
24035 the scope of the class, then the name lookup automatically
24036 finds the TYPE_DECL created by build_self_reference rather
24037 than a TEMPLATE_DECL. For example, in:
24039 template <class T> struct S {
24040 S s;
24043 there is no need to handle such case. */
24045 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24046 return DECL_TEMPLATE_RESULT (decl);
24048 return decl;
24051 /* If too many, or too few, template-parameter lists apply to the
24052 declarator, issue an error message. Returns TRUE if all went well,
24053 and FALSE otherwise. */
24055 static bool
24056 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24057 cp_declarator *declarator,
24058 location_t declarator_location)
24060 switch (declarator->kind)
24062 case cdk_id:
24064 unsigned num_templates = 0;
24065 tree scope = declarator->u.id.qualifying_scope;
24067 if (scope)
24068 num_templates = num_template_headers_for_class (scope);
24069 else if (TREE_CODE (declarator->u.id.unqualified_name)
24070 == TEMPLATE_ID_EXPR)
24071 /* If the DECLARATOR has the form `X<y>' then it uses one
24072 additional level of template parameters. */
24073 ++num_templates;
24075 return cp_parser_check_template_parameters
24076 (parser, num_templates, declarator_location, declarator);
24079 case cdk_function:
24080 case cdk_array:
24081 case cdk_pointer:
24082 case cdk_reference:
24083 case cdk_ptrmem:
24084 return (cp_parser_check_declarator_template_parameters
24085 (parser, declarator->declarator, declarator_location));
24087 case cdk_error:
24088 return true;
24090 default:
24091 gcc_unreachable ();
24093 return false;
24096 /* NUM_TEMPLATES were used in the current declaration. If that is
24097 invalid, return FALSE and issue an error messages. Otherwise,
24098 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24099 declarator and we can print more accurate diagnostics. */
24101 static bool
24102 cp_parser_check_template_parameters (cp_parser* parser,
24103 unsigned num_templates,
24104 location_t location,
24105 cp_declarator *declarator)
24107 /* If there are the same number of template classes and parameter
24108 lists, that's OK. */
24109 if (parser->num_template_parameter_lists == num_templates)
24110 return true;
24111 /* If there are more, but only one more, then we are referring to a
24112 member template. That's OK too. */
24113 if (parser->num_template_parameter_lists == num_templates + 1)
24114 return true;
24115 /* If there are more template classes than parameter lists, we have
24116 something like:
24118 template <class T> void S<T>::R<T>::f (); */
24119 if (parser->num_template_parameter_lists < num_templates)
24121 if (declarator && !current_function_decl)
24122 error_at (location, "specializing member %<%T::%E%> "
24123 "requires %<template<>%> syntax",
24124 declarator->u.id.qualifying_scope,
24125 declarator->u.id.unqualified_name);
24126 else if (declarator)
24127 error_at (location, "invalid declaration of %<%T::%E%>",
24128 declarator->u.id.qualifying_scope,
24129 declarator->u.id.unqualified_name);
24130 else
24131 error_at (location, "too few template-parameter-lists");
24132 return false;
24134 /* Otherwise, there are too many template parameter lists. We have
24135 something like:
24137 template <class T> template <class U> void S::f(); */
24138 error_at (location, "too many template-parameter-lists");
24139 return false;
24142 /* Parse an optional `::' token indicating that the following name is
24143 from the global namespace. If so, PARSER->SCOPE is set to the
24144 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24145 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24146 Returns the new value of PARSER->SCOPE, if the `::' token is
24147 present, and NULL_TREE otherwise. */
24149 static tree
24150 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24152 cp_token *token;
24154 /* Peek at the next token. */
24155 token = cp_lexer_peek_token (parser->lexer);
24156 /* If we're looking at a `::' token then we're starting from the
24157 global namespace, not our current location. */
24158 if (token->type == CPP_SCOPE)
24160 /* Consume the `::' token. */
24161 cp_lexer_consume_token (parser->lexer);
24162 /* Set the SCOPE so that we know where to start the lookup. */
24163 parser->scope = global_namespace;
24164 parser->qualifying_scope = global_namespace;
24165 parser->object_scope = NULL_TREE;
24167 return parser->scope;
24169 else if (!current_scope_valid_p)
24171 parser->scope = NULL_TREE;
24172 parser->qualifying_scope = NULL_TREE;
24173 parser->object_scope = NULL_TREE;
24176 return NULL_TREE;
24179 /* Returns TRUE if the upcoming token sequence is the start of a
24180 constructor declarator. If FRIEND_P is true, the declarator is
24181 preceded by the `friend' specifier. */
24183 static bool
24184 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
24186 bool constructor_p;
24187 bool outside_class_specifier_p;
24188 tree nested_name_specifier;
24189 cp_token *next_token;
24191 /* The common case is that this is not a constructor declarator, so
24192 try to avoid doing lots of work if at all possible. It's not
24193 valid declare a constructor at function scope. */
24194 if (parser->in_function_body)
24195 return false;
24196 /* And only certain tokens can begin a constructor declarator. */
24197 next_token = cp_lexer_peek_token (parser->lexer);
24198 if (next_token->type != CPP_NAME
24199 && next_token->type != CPP_SCOPE
24200 && next_token->type != CPP_NESTED_NAME_SPECIFIER
24201 && next_token->type != CPP_TEMPLATE_ID)
24202 return false;
24204 /* Parse tentatively; we are going to roll back all of the tokens
24205 consumed here. */
24206 cp_parser_parse_tentatively (parser);
24207 /* Assume that we are looking at a constructor declarator. */
24208 constructor_p = true;
24210 /* Look for the optional `::' operator. */
24211 cp_parser_global_scope_opt (parser,
24212 /*current_scope_valid_p=*/false);
24213 /* Look for the nested-name-specifier. */
24214 nested_name_specifier
24215 = (cp_parser_nested_name_specifier_opt (parser,
24216 /*typename_keyword_p=*/false,
24217 /*check_dependency_p=*/false,
24218 /*type_p=*/false,
24219 /*is_declaration=*/false));
24221 outside_class_specifier_p = (!at_class_scope_p ()
24222 || !TYPE_BEING_DEFINED (current_class_type)
24223 || friend_p);
24225 /* Outside of a class-specifier, there must be a
24226 nested-name-specifier. */
24227 if (!nested_name_specifier && outside_class_specifier_p)
24228 constructor_p = false;
24229 else if (nested_name_specifier == error_mark_node)
24230 constructor_p = false;
24232 /* If we have a class scope, this is easy; DR 147 says that S::S always
24233 names the constructor, and no other qualified name could. */
24234 if (constructor_p && nested_name_specifier
24235 && CLASS_TYPE_P (nested_name_specifier))
24237 tree id = cp_parser_unqualified_id (parser,
24238 /*template_keyword_p=*/false,
24239 /*check_dependency_p=*/false,
24240 /*declarator_p=*/true,
24241 /*optional_p=*/false);
24242 if (is_overloaded_fn (id))
24243 id = DECL_NAME (get_first_fn (id));
24244 if (!constructor_name_p (id, nested_name_specifier))
24245 constructor_p = false;
24247 /* If we still think that this might be a constructor-declarator,
24248 look for a class-name. */
24249 else if (constructor_p)
24251 /* If we have:
24253 template <typename T> struct S {
24254 S();
24257 we must recognize that the nested `S' names a class. */
24258 tree type_decl;
24259 type_decl = cp_parser_class_name (parser,
24260 /*typename_keyword_p=*/false,
24261 /*template_keyword_p=*/false,
24262 none_type,
24263 /*check_dependency_p=*/false,
24264 /*class_head_p=*/false,
24265 /*is_declaration=*/false);
24266 /* If there was no class-name, then this is not a constructor.
24267 Otherwise, if we are in a class-specifier and we aren't
24268 handling a friend declaration, check that its type matches
24269 current_class_type (c++/38313). Note: error_mark_node
24270 is left alone for error recovery purposes. */
24271 constructor_p = (!cp_parser_error_occurred (parser)
24272 && (outside_class_specifier_p
24273 || type_decl == error_mark_node
24274 || same_type_p (current_class_type,
24275 TREE_TYPE (type_decl))));
24277 /* If we're still considering a constructor, we have to see a `(',
24278 to begin the parameter-declaration-clause, followed by either a
24279 `)', an `...', or a decl-specifier. We need to check for a
24280 type-specifier to avoid being fooled into thinking that:
24282 S (f) (int);
24284 is a constructor. (It is actually a function named `f' that
24285 takes one parameter (of type `int') and returns a value of type
24286 `S'. */
24287 if (constructor_p
24288 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24289 constructor_p = false;
24291 if (constructor_p
24292 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
24293 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
24294 /* A parameter declaration begins with a decl-specifier,
24295 which is either the "attribute" keyword, a storage class
24296 specifier, or (usually) a type-specifier. */
24297 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
24299 tree type;
24300 tree pushed_scope = NULL_TREE;
24301 unsigned saved_num_template_parameter_lists;
24303 /* Names appearing in the type-specifier should be looked up
24304 in the scope of the class. */
24305 if (current_class_type)
24306 type = NULL_TREE;
24307 else
24309 type = TREE_TYPE (type_decl);
24310 if (TREE_CODE (type) == TYPENAME_TYPE)
24312 type = resolve_typename_type (type,
24313 /*only_current_p=*/false);
24314 if (TREE_CODE (type) == TYPENAME_TYPE)
24316 cp_parser_abort_tentative_parse (parser);
24317 return false;
24320 pushed_scope = push_scope (type);
24323 /* Inside the constructor parameter list, surrounding
24324 template-parameter-lists do not apply. */
24325 saved_num_template_parameter_lists
24326 = parser->num_template_parameter_lists;
24327 parser->num_template_parameter_lists = 0;
24329 /* Look for the type-specifier. */
24330 cp_parser_type_specifier (parser,
24331 CP_PARSER_FLAGS_NONE,
24332 /*decl_specs=*/NULL,
24333 /*is_declarator=*/true,
24334 /*declares_class_or_enum=*/NULL,
24335 /*is_cv_qualifier=*/NULL);
24337 parser->num_template_parameter_lists
24338 = saved_num_template_parameter_lists;
24340 /* Leave the scope of the class. */
24341 if (pushed_scope)
24342 pop_scope (pushed_scope);
24344 constructor_p = !cp_parser_error_occurred (parser);
24348 /* We did not really want to consume any tokens. */
24349 cp_parser_abort_tentative_parse (parser);
24351 return constructor_p;
24354 /* Parse the definition of the function given by the DECL_SPECIFIERS,
24355 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
24356 they must be performed once we are in the scope of the function.
24358 Returns the function defined. */
24360 static tree
24361 cp_parser_function_definition_from_specifiers_and_declarator
24362 (cp_parser* parser,
24363 cp_decl_specifier_seq *decl_specifiers,
24364 tree attributes,
24365 const cp_declarator *declarator)
24367 tree fn;
24368 bool success_p;
24370 /* Begin the function-definition. */
24371 success_p = start_function (decl_specifiers, declarator, attributes);
24373 /* The things we're about to see are not directly qualified by any
24374 template headers we've seen thus far. */
24375 reset_specialization ();
24377 /* If there were names looked up in the decl-specifier-seq that we
24378 did not check, check them now. We must wait until we are in the
24379 scope of the function to perform the checks, since the function
24380 might be a friend. */
24381 perform_deferred_access_checks (tf_warning_or_error);
24383 if (success_p)
24385 cp_finalize_omp_declare_simd (parser, current_function_decl);
24386 parser->omp_declare_simd = NULL;
24389 if (!success_p)
24391 /* Skip the entire function. */
24392 cp_parser_skip_to_end_of_block_or_statement (parser);
24393 fn = error_mark_node;
24395 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
24397 /* Seen already, skip it. An error message has already been output. */
24398 cp_parser_skip_to_end_of_block_or_statement (parser);
24399 fn = current_function_decl;
24400 current_function_decl = NULL_TREE;
24401 /* If this is a function from a class, pop the nested class. */
24402 if (current_class_name)
24403 pop_nested_class ();
24405 else
24407 timevar_id_t tv;
24408 if (DECL_DECLARED_INLINE_P (current_function_decl))
24409 tv = TV_PARSE_INLINE;
24410 else
24411 tv = TV_PARSE_FUNC;
24412 timevar_push (tv);
24413 fn = cp_parser_function_definition_after_declarator (parser,
24414 /*inline_p=*/false);
24415 timevar_pop (tv);
24418 return fn;
24421 /* Parse the part of a function-definition that follows the
24422 declarator. INLINE_P is TRUE iff this function is an inline
24423 function defined within a class-specifier.
24425 Returns the function defined. */
24427 static tree
24428 cp_parser_function_definition_after_declarator (cp_parser* parser,
24429 bool inline_p)
24431 tree fn;
24432 bool ctor_initializer_p = false;
24433 bool saved_in_unbraced_linkage_specification_p;
24434 bool saved_in_function_body;
24435 unsigned saved_num_template_parameter_lists;
24436 cp_token *token;
24437 bool fully_implicit_function_template_p
24438 = parser->fully_implicit_function_template_p;
24439 parser->fully_implicit_function_template_p = false;
24440 tree implicit_template_parms
24441 = parser->implicit_template_parms;
24442 parser->implicit_template_parms = 0;
24443 cp_binding_level* implicit_template_scope
24444 = parser->implicit_template_scope;
24445 parser->implicit_template_scope = 0;
24447 saved_in_function_body = parser->in_function_body;
24448 parser->in_function_body = true;
24449 /* If the next token is `return', then the code may be trying to
24450 make use of the "named return value" extension that G++ used to
24451 support. */
24452 token = cp_lexer_peek_token (parser->lexer);
24453 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
24455 /* Consume the `return' keyword. */
24456 cp_lexer_consume_token (parser->lexer);
24457 /* Look for the identifier that indicates what value is to be
24458 returned. */
24459 cp_parser_identifier (parser);
24460 /* Issue an error message. */
24461 error_at (token->location,
24462 "named return values are no longer supported");
24463 /* Skip tokens until we reach the start of the function body. */
24464 while (true)
24466 cp_token *token = cp_lexer_peek_token (parser->lexer);
24467 if (token->type == CPP_OPEN_BRACE
24468 || token->type == CPP_EOF
24469 || token->type == CPP_PRAGMA_EOL)
24470 break;
24471 cp_lexer_consume_token (parser->lexer);
24474 /* The `extern' in `extern "C" void f () { ... }' does not apply to
24475 anything declared inside `f'. */
24476 saved_in_unbraced_linkage_specification_p
24477 = parser->in_unbraced_linkage_specification_p;
24478 parser->in_unbraced_linkage_specification_p = false;
24479 /* Inside the function, surrounding template-parameter-lists do not
24480 apply. */
24481 saved_num_template_parameter_lists
24482 = parser->num_template_parameter_lists;
24483 parser->num_template_parameter_lists = 0;
24485 start_lambda_scope (current_function_decl);
24487 /* If the next token is `try', `__transaction_atomic', or
24488 `__transaction_relaxed`, then we are looking at either function-try-block
24489 or function-transaction-block. Note that all of these include the
24490 function-body. */
24491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
24492 ctor_initializer_p = cp_parser_function_transaction (parser,
24493 RID_TRANSACTION_ATOMIC);
24494 else if (cp_lexer_next_token_is_keyword (parser->lexer,
24495 RID_TRANSACTION_RELAXED))
24496 ctor_initializer_p = cp_parser_function_transaction (parser,
24497 RID_TRANSACTION_RELAXED);
24498 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24499 ctor_initializer_p = cp_parser_function_try_block (parser);
24500 else
24501 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24502 (parser, /*in_function_try_block=*/false);
24504 finish_lambda_scope ();
24506 /* Finish the function. */
24507 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
24508 (inline_p ? 2 : 0));
24509 /* Generate code for it, if necessary. */
24510 expand_or_defer_fn (fn);
24511 /* Restore the saved values. */
24512 parser->in_unbraced_linkage_specification_p
24513 = saved_in_unbraced_linkage_specification_p;
24514 parser->num_template_parameter_lists
24515 = saved_num_template_parameter_lists;
24516 parser->in_function_body = saved_in_function_body;
24518 parser->fully_implicit_function_template_p
24519 = fully_implicit_function_template_p;
24520 parser->implicit_template_parms
24521 = implicit_template_parms;
24522 parser->implicit_template_scope
24523 = implicit_template_scope;
24525 if (parser->fully_implicit_function_template_p)
24526 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
24528 return fn;
24531 #if 0
24532 /* Parse a concept introduction header for a template-declaration. If
24533 successful, returns the template parameters. Otherwise returns
24534 error_mark_node. */
24536 static tree
24537 cp_parser_template_introduction (cp_parser* parser)
24539 // Look for the optional `::' operator.
24540 cp_parser_global_scope_opt (parser,
24541 /*current_scope_valid_p=*/true);
24542 // Look for the nested-name-specifier.
24543 cp_parser_nested_name_specifier_opt (parser,
24544 /*typename_keyword_p=*/false,
24545 /*check_dependency_p=*/true,
24546 /*type_p=*/false,
24547 /*is_declaration=*/false);
24549 cp_token *token = cp_lexer_peek_token (parser->lexer);
24550 tree concept_name = cp_parser_identifier (parser);
24551 if (concept_name == error_mark_node)
24552 return error_mark_node;
24554 // Look for opening brace for introduction
24555 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24556 return error_mark_node;
24558 // This must be a concept introduction.
24559 if (cp_parser_parsing_tentatively (parser))
24560 cp_parser_commit_to_tentative_parse (parser);
24562 // Build vector of placeholder parameters and grab matching identifiers.
24563 tree introduction_list = cp_parser_introduction_list (parser);
24565 // The introduction-list shall not be empty
24566 int nargs = TREE_VEC_LENGTH (introduction_list);
24567 if (nargs == 0)
24569 error ("an introduction-list shall not be empty");
24570 return error_mark_node;
24573 // Look for closing brace for introduction
24574 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24575 return error_mark_node;
24577 // Look up the concept for which we will be matching template parameters.
24578 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
24579 token->location);
24580 if (tmpl_decl == error_mark_node)
24582 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
24583 token->location);
24584 return error_mark_node;
24587 // Build and associate the constraint.
24588 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
24589 if (parms)
24590 return parms;
24592 error_at (token->location, "no matching concept for introduction-list");
24593 return error_mark_node;
24595 #endif
24597 /* Parse a template-declaration, assuming that the `export' (and
24598 `extern') keywords, if present, has already been scanned. MEMBER_P
24599 is as for cp_parser_template_declaration. */
24601 static void
24602 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
24604 tree decl = NULL_TREE;
24605 vec<deferred_access_check, va_gc> *checks;
24606 tree parameter_list;
24607 bool friend_p = false;
24608 bool need_lang_pop;
24609 cp_token *token;
24611 /* Look for the `template' keyword. */
24612 token = cp_lexer_peek_token (parser->lexer);
24615 /* Look for the `template' keyword. */
24616 token = cp_lexer_peek_token (parser->lexer);
24617 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
24618 return;
24620 /* And the `<'. */
24621 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
24622 return;
24623 if (at_class_scope_p () && current_function_decl)
24625 /* 14.5.2.2 [temp.mem]
24627 A local class shall not have member templates. */
24628 error_at (token->location,
24629 "invalid declaration of member template in local class");
24630 cp_parser_skip_to_end_of_block_or_statement (parser);
24631 return;
24633 /* [temp]
24635 A template ... shall not have C linkage. */
24636 if (current_lang_name == lang_name_c)
24638 error_at (token->location, "template with C linkage");
24639 /* Give it C++ linkage to avoid confusing other parts of the
24640 front end. */
24641 push_lang_context (lang_name_cplusplus);
24642 need_lang_pop = true;
24644 else
24645 need_lang_pop = false;
24647 /* We cannot perform access checks on the template parameter
24648 declarations until we know what is being declared, just as we
24649 cannot check the decl-specifier list. */
24650 push_deferring_access_checks (dk_deferred);
24652 /* If the next token is `>', then we have an invalid
24653 specialization. Rather than complain about an invalid template
24654 parameter, issue an error message here. */
24655 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
24657 cp_parser_error (parser, "invalid explicit specialization");
24658 begin_specialization ();
24659 parameter_list = NULL_TREE;
24661 else
24663 /* Parse the template parameters. */
24664 parameter_list = cp_parser_template_parameter_list (parser);
24667 /* Get the deferred access checks from the parameter list. These
24668 will be checked once we know what is being declared, as for a
24669 member template the checks must be performed in the scope of the
24670 class containing the member. */
24671 checks = get_deferred_access_checks ();
24673 /* Look for the `>'. */
24674 cp_parser_skip_to_end_of_template_parameter_list (parser);
24676 /* Manage template requirements */
24677 if (flag_concepts)
24679 tree reqs = get_shorthand_constraints (current_template_parms);
24680 if (tree r = cp_parser_requires_clause_opt (parser)) {
24681 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
24683 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
24686 /* We just processed one more parameter list. */
24687 ++parser->num_template_parameter_lists;
24688 /* If the next token is `template', there are more template
24689 parameters. */
24690 if (cp_lexer_next_token_is_keyword (parser->lexer,
24691 RID_TEMPLATE))
24692 cp_parser_template_declaration_after_export (parser, member_p);
24693 else if (cxx_dialect >= cxx11
24694 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24695 decl = cp_parser_alias_declaration (parser);
24696 else
24698 /* There are no access checks when parsing a template, as we do not
24699 know if a specialization will be a friend. */
24700 push_deferring_access_checks (dk_no_check);
24701 token = cp_lexer_peek_token (parser->lexer);
24702 decl = cp_parser_single_declaration (parser,
24703 checks,
24704 member_p,
24705 /*explicit_specialization_p=*/false,
24706 &friend_p);
24707 pop_deferring_access_checks ();
24709 /* If this is a member template declaration, let the front
24710 end know. */
24711 if (member_p && !friend_p && decl)
24713 if (TREE_CODE (decl) == TYPE_DECL)
24714 cp_parser_check_access_in_redeclaration (decl, token->location);
24716 decl = finish_member_template_decl (decl);
24718 else if (friend_p && decl
24719 && DECL_DECLARES_TYPE_P (decl))
24720 make_friend_class (current_class_type, TREE_TYPE (decl),
24721 /*complain=*/true);
24723 /* We are done with the current parameter list. */
24724 --parser->num_template_parameter_lists;
24726 pop_deferring_access_checks ();
24728 /* Finish up. */
24729 finish_template_decl (parameter_list);
24731 /* Check the template arguments for a literal operator template. */
24732 if (decl
24733 && DECL_DECLARES_FUNCTION_P (decl)
24734 && UDLIT_OPER_P (DECL_NAME (decl)))
24736 bool ok = true;
24737 if (parameter_list == NULL_TREE)
24738 ok = false;
24739 else
24741 int num_parms = TREE_VEC_LENGTH (parameter_list);
24742 if (num_parms == 1)
24744 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
24745 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24746 if (TREE_TYPE (parm) != char_type_node
24747 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24748 ok = false;
24750 else if (num_parms == 2 && cxx_dialect >= cxx14)
24752 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
24753 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
24754 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
24755 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24756 if (TREE_TYPE (parm) != TREE_TYPE (type)
24757 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24758 ok = false;
24760 else
24761 ok = false;
24763 if (!ok)
24765 if (cxx_dialect >= cxx14)
24766 error ("literal operator template %qD has invalid parameter list."
24767 " Expected non-type template argument pack <char...>"
24768 " or <typename CharT, CharT...>",
24769 decl);
24770 else
24771 error ("literal operator template %qD has invalid parameter list."
24772 " Expected non-type template argument pack <char...>",
24773 decl);
24777 /* Register member declarations. */
24778 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
24779 finish_member_declaration (decl);
24780 /* For the erroneous case of a template with C linkage, we pushed an
24781 implicit C++ linkage scope; exit that scope now. */
24782 if (need_lang_pop)
24783 pop_lang_context ();
24784 /* If DECL is a function template, we must return to parse it later.
24785 (Even though there is no definition, there might be default
24786 arguments that need handling.) */
24787 if (member_p && decl
24788 && DECL_DECLARES_FUNCTION_P (decl))
24789 vec_safe_push (unparsed_funs_with_definitions, decl);
24792 /* Perform the deferred access checks from a template-parameter-list.
24793 CHECKS is a TREE_LIST of access checks, as returned by
24794 get_deferred_access_checks. */
24796 static void
24797 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
24799 ++processing_template_parmlist;
24800 perform_access_checks (checks, tf_warning_or_error);
24801 --processing_template_parmlist;
24804 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
24805 `function-definition' sequence that follows a template header.
24806 If MEMBER_P is true, this declaration appears in a class scope.
24808 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
24809 *FRIEND_P is set to TRUE iff the declaration is a friend. */
24811 static tree
24812 cp_parser_single_declaration (cp_parser* parser,
24813 vec<deferred_access_check, va_gc> *checks,
24814 bool member_p,
24815 bool explicit_specialization_p,
24816 bool* friend_p)
24818 int declares_class_or_enum;
24819 tree decl = NULL_TREE;
24820 cp_decl_specifier_seq decl_specifiers;
24821 bool function_definition_p = false;
24822 cp_token *decl_spec_token_start;
24824 /* This function is only used when processing a template
24825 declaration. */
24826 gcc_assert (innermost_scope_kind () == sk_template_parms
24827 || innermost_scope_kind () == sk_template_spec);
24829 /* Defer access checks until we know what is being declared. */
24830 push_deferring_access_checks (dk_deferred);
24832 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
24833 alternative. */
24834 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24835 cp_parser_decl_specifier_seq (parser,
24836 CP_PARSER_FLAGS_OPTIONAL,
24837 &decl_specifiers,
24838 &declares_class_or_enum);
24839 if (friend_p)
24840 *friend_p = cp_parser_friend_p (&decl_specifiers);
24842 /* There are no template typedefs. */
24843 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
24845 error_at (decl_spec_token_start->location,
24846 "template declaration of %<typedef%>");
24847 decl = error_mark_node;
24850 /* Gather up the access checks that occurred the
24851 decl-specifier-seq. */
24852 stop_deferring_access_checks ();
24854 /* Check for the declaration of a template class. */
24855 if (declares_class_or_enum)
24857 if (cp_parser_declares_only_class_p (parser))
24859 // If this is a declaration, but not a definition, associate
24860 // any constraints with the type declaration. Constraints
24861 // are associated with definitions in cp_parser_class_specifier.
24862 if (declares_class_or_enum == 1)
24863 associate_classtype_constraints (decl_specifiers.type);
24865 decl = shadow_tag (&decl_specifiers);
24867 /* In this case:
24869 struct C {
24870 friend template <typename T> struct A<T>::B;
24873 A<T>::B will be represented by a TYPENAME_TYPE, and
24874 therefore not recognized by shadow_tag. */
24875 if (friend_p && *friend_p
24876 && !decl
24877 && decl_specifiers.type
24878 && TYPE_P (decl_specifiers.type))
24879 decl = decl_specifiers.type;
24881 if (decl && decl != error_mark_node)
24882 decl = TYPE_NAME (decl);
24883 else
24884 decl = error_mark_node;
24886 /* Perform access checks for template parameters. */
24887 cp_parser_perform_template_parameter_access_checks (checks);
24891 /* Complain about missing 'typename' or other invalid type names. */
24892 if (!decl_specifiers.any_type_specifiers_p
24893 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24895 /* cp_parser_parse_and_diagnose_invalid_type_name calls
24896 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
24897 the rest of this declaration. */
24898 decl = error_mark_node;
24899 goto out;
24902 /* If it's not a template class, try for a template function. If
24903 the next token is a `;', then this declaration does not declare
24904 anything. But, if there were errors in the decl-specifiers, then
24905 the error might well have come from an attempted class-specifier.
24906 In that case, there's no need to warn about a missing declarator. */
24907 if (!decl
24908 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
24909 || decl_specifiers.type != error_mark_node))
24911 decl = cp_parser_init_declarator (parser,
24912 &decl_specifiers,
24913 checks,
24914 /*function_definition_allowed_p=*/true,
24915 member_p,
24916 declares_class_or_enum,
24917 &function_definition_p,
24918 NULL, NULL);
24920 /* 7.1.1-1 [dcl.stc]
24922 A storage-class-specifier shall not be specified in an explicit
24923 specialization... */
24924 if (decl
24925 && explicit_specialization_p
24926 && decl_specifiers.storage_class != sc_none)
24928 error_at (decl_spec_token_start->location,
24929 "explicit template specialization cannot have a storage class");
24930 decl = error_mark_node;
24933 if (decl && VAR_P (decl))
24934 check_template_variable (decl);
24937 /* Look for a trailing `;' after the declaration. */
24938 if (!function_definition_p
24939 && (decl == error_mark_node
24940 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
24941 cp_parser_skip_to_end_of_block_or_statement (parser);
24943 out:
24944 pop_deferring_access_checks ();
24946 /* Clear any current qualification; whatever comes next is the start
24947 of something new. */
24948 parser->scope = NULL_TREE;
24949 parser->qualifying_scope = NULL_TREE;
24950 parser->object_scope = NULL_TREE;
24952 return decl;
24955 /* Parse a cast-expression that is not the operand of a unary "&". */
24957 static tree
24958 cp_parser_simple_cast_expression (cp_parser *parser)
24960 return cp_parser_cast_expression (parser, /*address_p=*/false,
24961 /*cast_p=*/false, /*decltype*/false, NULL);
24964 /* Parse a functional cast to TYPE. Returns an expression
24965 representing the cast. */
24967 static tree
24968 cp_parser_functional_cast (cp_parser* parser, tree type)
24970 vec<tree, va_gc> *vec;
24971 tree expression_list;
24972 tree cast;
24973 bool nonconst_p;
24975 if (!type)
24976 type = error_mark_node;
24978 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24980 cp_lexer_set_source_position (parser->lexer);
24981 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24982 expression_list = cp_parser_braced_list (parser, &nonconst_p);
24983 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
24984 if (TREE_CODE (type) == TYPE_DECL)
24985 type = TREE_TYPE (type);
24986 return finish_compound_literal (type, expression_list,
24987 tf_warning_or_error);
24991 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
24992 /*cast_p=*/true,
24993 /*allow_expansion_p=*/true,
24994 /*non_constant_p=*/NULL);
24995 if (vec == NULL)
24996 expression_list = error_mark_node;
24997 else
24999 expression_list = build_tree_list_vec (vec);
25000 release_tree_vector (vec);
25003 cast = build_functional_cast (type, expression_list,
25004 tf_warning_or_error);
25005 /* [expr.const]/1: In an integral constant expression "only type
25006 conversions to integral or enumeration type can be used". */
25007 if (TREE_CODE (type) == TYPE_DECL)
25008 type = TREE_TYPE (type);
25009 if (cast != error_mark_node
25010 && !cast_valid_in_integral_constant_expression_p (type)
25011 && cp_parser_non_integral_constant_expression (parser,
25012 NIC_CONSTRUCTOR))
25013 return error_mark_node;
25014 return cast;
25017 /* Save the tokens that make up the body of a member function defined
25018 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25019 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25020 specifiers applied to the declaration. Returns the FUNCTION_DECL
25021 for the member function. */
25023 static tree
25024 cp_parser_save_member_function_body (cp_parser* parser,
25025 cp_decl_specifier_seq *decl_specifiers,
25026 cp_declarator *declarator,
25027 tree attributes)
25029 cp_token *first;
25030 cp_token *last;
25031 tree fn;
25033 /* Create the FUNCTION_DECL. */
25034 fn = grokmethod (decl_specifiers, declarator, attributes);
25035 cp_finalize_omp_declare_simd (parser, fn);
25036 /* If something went badly wrong, bail out now. */
25037 if (fn == error_mark_node)
25039 /* If there's a function-body, skip it. */
25040 if (cp_parser_token_starts_function_definition_p
25041 (cp_lexer_peek_token (parser->lexer)))
25042 cp_parser_skip_to_end_of_block_or_statement (parser);
25043 return error_mark_node;
25046 /* Remember it, if there default args to post process. */
25047 cp_parser_save_default_args (parser, fn);
25049 /* Save away the tokens that make up the body of the
25050 function. */
25051 first = parser->lexer->next_token;
25052 /* Handle function try blocks. */
25053 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25054 cp_lexer_consume_token (parser->lexer);
25055 /* We can have braced-init-list mem-initializers before the fn body. */
25056 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25058 cp_lexer_consume_token (parser->lexer);
25059 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25061 /* cache_group will stop after an un-nested { } pair, too. */
25062 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25063 break;
25065 /* variadic mem-inits have ... after the ')'. */
25066 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25067 cp_lexer_consume_token (parser->lexer);
25070 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25071 /* Handle function try blocks. */
25072 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25073 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25074 last = parser->lexer->next_token;
25076 /* Save away the inline definition; we will process it when the
25077 class is complete. */
25078 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25079 DECL_PENDING_INLINE_P (fn) = 1;
25081 /* We need to know that this was defined in the class, so that
25082 friend templates are handled correctly. */
25083 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25085 /* Add FN to the queue of functions to be parsed later. */
25086 vec_safe_push (unparsed_funs_with_definitions, fn);
25088 return fn;
25091 /* Save the tokens that make up the in-class initializer for a non-static
25092 data member. Returns a DEFAULT_ARG. */
25094 static tree
25095 cp_parser_save_nsdmi (cp_parser* parser)
25097 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
25100 /* Parse a template-argument-list, as well as the trailing ">" (but
25101 not the opening "<"). See cp_parser_template_argument_list for the
25102 return value. */
25104 static tree
25105 cp_parser_enclosed_template_argument_list (cp_parser* parser)
25107 tree arguments;
25108 tree saved_scope;
25109 tree saved_qualifying_scope;
25110 tree saved_object_scope;
25111 bool saved_greater_than_is_operator_p;
25112 int saved_unevaluated_operand;
25113 int saved_inhibit_evaluation_warnings;
25115 /* [temp.names]
25117 When parsing a template-id, the first non-nested `>' is taken as
25118 the end of the template-argument-list rather than a greater-than
25119 operator. */
25120 saved_greater_than_is_operator_p
25121 = parser->greater_than_is_operator_p;
25122 parser->greater_than_is_operator_p = false;
25123 /* Parsing the argument list may modify SCOPE, so we save it
25124 here. */
25125 saved_scope = parser->scope;
25126 saved_qualifying_scope = parser->qualifying_scope;
25127 saved_object_scope = parser->object_scope;
25128 /* We need to evaluate the template arguments, even though this
25129 template-id may be nested within a "sizeof". */
25130 saved_unevaluated_operand = cp_unevaluated_operand;
25131 cp_unevaluated_operand = 0;
25132 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25133 c_inhibit_evaluation_warnings = 0;
25134 /* Parse the template-argument-list itself. */
25135 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
25136 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25137 arguments = NULL_TREE;
25138 else
25139 arguments = cp_parser_template_argument_list (parser);
25140 /* Look for the `>' that ends the template-argument-list. If we find
25141 a '>>' instead, it's probably just a typo. */
25142 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25144 if (cxx_dialect != cxx98)
25146 /* In C++0x, a `>>' in a template argument list or cast
25147 expression is considered to be two separate `>'
25148 tokens. So, change the current token to a `>', but don't
25149 consume it: it will be consumed later when the outer
25150 template argument list (or cast expression) is parsed.
25151 Note that this replacement of `>' for `>>' is necessary
25152 even if we are parsing tentatively: in the tentative
25153 case, after calling
25154 cp_parser_enclosed_template_argument_list we will always
25155 throw away all of the template arguments and the first
25156 closing `>', either because the template argument list
25157 was erroneous or because we are replacing those tokens
25158 with a CPP_TEMPLATE_ID token. The second `>' (which will
25159 not have been thrown away) is needed either to close an
25160 outer template argument list or to complete a new-style
25161 cast. */
25162 cp_token *token = cp_lexer_peek_token (parser->lexer);
25163 token->type = CPP_GREATER;
25165 else if (!saved_greater_than_is_operator_p)
25167 /* If we're in a nested template argument list, the '>>' has
25168 to be a typo for '> >'. We emit the error message, but we
25169 continue parsing and we push a '>' as next token, so that
25170 the argument list will be parsed correctly. Note that the
25171 global source location is still on the token before the
25172 '>>', so we need to say explicitly where we want it. */
25173 cp_token *token = cp_lexer_peek_token (parser->lexer);
25174 error_at (token->location, "%<>>%> should be %<> >%> "
25175 "within a nested template argument list");
25177 token->type = CPP_GREATER;
25179 else
25181 /* If this is not a nested template argument list, the '>>'
25182 is a typo for '>'. Emit an error message and continue.
25183 Same deal about the token location, but here we can get it
25184 right by consuming the '>>' before issuing the diagnostic. */
25185 cp_token *token = cp_lexer_consume_token (parser->lexer);
25186 error_at (token->location,
25187 "spurious %<>>%>, use %<>%> to terminate "
25188 "a template argument list");
25191 else
25192 cp_parser_skip_to_end_of_template_parameter_list (parser);
25193 /* The `>' token might be a greater-than operator again now. */
25194 parser->greater_than_is_operator_p
25195 = saved_greater_than_is_operator_p;
25196 /* Restore the SAVED_SCOPE. */
25197 parser->scope = saved_scope;
25198 parser->qualifying_scope = saved_qualifying_scope;
25199 parser->object_scope = saved_object_scope;
25200 cp_unevaluated_operand = saved_unevaluated_operand;
25201 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25203 return arguments;
25206 /* MEMBER_FUNCTION is a member function, or a friend. If default
25207 arguments, or the body of the function have not yet been parsed,
25208 parse them now. */
25210 static void
25211 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
25213 timevar_push (TV_PARSE_INMETH);
25214 /* If this member is a template, get the underlying
25215 FUNCTION_DECL. */
25216 if (DECL_FUNCTION_TEMPLATE_P (member_function))
25217 member_function = DECL_TEMPLATE_RESULT (member_function);
25219 /* There should not be any class definitions in progress at this
25220 point; the bodies of members are only parsed outside of all class
25221 definitions. */
25222 gcc_assert (parser->num_classes_being_defined == 0);
25223 /* While we're parsing the member functions we might encounter more
25224 classes. We want to handle them right away, but we don't want
25225 them getting mixed up with functions that are currently in the
25226 queue. */
25227 push_unparsed_function_queues (parser);
25229 /* Make sure that any template parameters are in scope. */
25230 maybe_begin_member_template_processing (member_function);
25232 /* If the body of the function has not yet been parsed, parse it
25233 now. */
25234 if (DECL_PENDING_INLINE_P (member_function))
25236 tree function_scope;
25237 cp_token_cache *tokens;
25239 /* The function is no longer pending; we are processing it. */
25240 tokens = DECL_PENDING_INLINE_INFO (member_function);
25241 DECL_PENDING_INLINE_INFO (member_function) = NULL;
25242 DECL_PENDING_INLINE_P (member_function) = 0;
25244 /* If this is a local class, enter the scope of the containing
25245 function. */
25246 function_scope = current_function_decl;
25247 if (function_scope)
25248 push_function_context ();
25250 /* Push the body of the function onto the lexer stack. */
25251 cp_parser_push_lexer_for_tokens (parser, tokens);
25253 /* Let the front end know that we going to be defining this
25254 function. */
25255 start_preparsed_function (member_function, NULL_TREE,
25256 SF_PRE_PARSED | SF_INCLASS_INLINE);
25258 /* Don't do access checking if it is a templated function. */
25259 if (processing_template_decl)
25260 push_deferring_access_checks (dk_no_check);
25262 /* #pragma omp declare reduction needs special parsing. */
25263 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
25265 parser->lexer->in_pragma = true;
25266 cp_parser_omp_declare_reduction_exprs (member_function, parser);
25267 finish_function (/*inline*/2);
25268 cp_check_omp_declare_reduction (member_function);
25270 else
25271 /* Now, parse the body of the function. */
25272 cp_parser_function_definition_after_declarator (parser,
25273 /*inline_p=*/true);
25275 if (processing_template_decl)
25276 pop_deferring_access_checks ();
25278 /* Leave the scope of the containing function. */
25279 if (function_scope)
25280 pop_function_context ();
25281 cp_parser_pop_lexer (parser);
25284 /* Remove any template parameters from the symbol table. */
25285 maybe_end_member_template_processing ();
25287 /* Restore the queue. */
25288 pop_unparsed_function_queues (parser);
25289 timevar_pop (TV_PARSE_INMETH);
25292 /* If DECL contains any default args, remember it on the unparsed
25293 functions queue. */
25295 static void
25296 cp_parser_save_default_args (cp_parser* parser, tree decl)
25298 tree probe;
25300 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
25301 probe;
25302 probe = TREE_CHAIN (probe))
25303 if (TREE_PURPOSE (probe))
25305 cp_default_arg_entry entry = {current_class_type, decl};
25306 vec_safe_push (unparsed_funs_with_default_args, entry);
25307 break;
25311 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
25312 which is either a FIELD_DECL or PARM_DECL. Parse it and return
25313 the result. For a PARM_DECL, PARMTYPE is the corresponding type
25314 from the parameter-type-list. */
25316 static tree
25317 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
25318 tree default_arg, tree parmtype)
25320 cp_token_cache *tokens;
25321 tree parsed_arg;
25322 bool dummy;
25324 if (default_arg == error_mark_node)
25325 return error_mark_node;
25327 /* Push the saved tokens for the default argument onto the parser's
25328 lexer stack. */
25329 tokens = DEFARG_TOKENS (default_arg);
25330 cp_parser_push_lexer_for_tokens (parser, tokens);
25332 start_lambda_scope (decl);
25334 /* Parse the default argument. */
25335 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
25336 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
25337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25339 finish_lambda_scope ();
25341 if (parsed_arg == error_mark_node)
25342 cp_parser_skip_to_end_of_statement (parser);
25344 if (!processing_template_decl)
25346 /* In a non-template class, check conversions now. In a template,
25347 we'll wait and instantiate these as needed. */
25348 if (TREE_CODE (decl) == PARM_DECL)
25349 parsed_arg = check_default_argument (parmtype, parsed_arg,
25350 tf_warning_or_error);
25351 else
25352 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
25355 /* If the token stream has not been completely used up, then
25356 there was extra junk after the end of the default
25357 argument. */
25358 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25360 if (TREE_CODE (decl) == PARM_DECL)
25361 cp_parser_error (parser, "expected %<,%>");
25362 else
25363 cp_parser_error (parser, "expected %<;%>");
25366 /* Revert to the main lexer. */
25367 cp_parser_pop_lexer (parser);
25369 return parsed_arg;
25372 /* FIELD is a non-static data member with an initializer which we saved for
25373 later; parse it now. */
25375 static void
25376 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
25378 tree def;
25380 maybe_begin_member_template_processing (field);
25382 push_unparsed_function_queues (parser);
25383 def = cp_parser_late_parse_one_default_arg (parser, field,
25384 DECL_INITIAL (field),
25385 NULL_TREE);
25386 pop_unparsed_function_queues (parser);
25388 maybe_end_member_template_processing ();
25390 DECL_INITIAL (field) = def;
25393 /* FN is a FUNCTION_DECL which may contains a parameter with an
25394 unparsed DEFAULT_ARG. Parse the default args now. This function
25395 assumes that the current scope is the scope in which the default
25396 argument should be processed. */
25398 static void
25399 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
25401 bool saved_local_variables_forbidden_p;
25402 tree parm, parmdecl;
25404 /* While we're parsing the default args, we might (due to the
25405 statement expression extension) encounter more classes. We want
25406 to handle them right away, but we don't want them getting mixed
25407 up with default args that are currently in the queue. */
25408 push_unparsed_function_queues (parser);
25410 /* Local variable names (and the `this' keyword) may not appear
25411 in a default argument. */
25412 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25413 parser->local_variables_forbidden_p = true;
25415 push_defarg_context (fn);
25417 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
25418 parmdecl = DECL_ARGUMENTS (fn);
25419 parm && parm != void_list_node;
25420 parm = TREE_CHAIN (parm),
25421 parmdecl = DECL_CHAIN (parmdecl))
25423 tree default_arg = TREE_PURPOSE (parm);
25424 tree parsed_arg;
25425 vec<tree, va_gc> *insts;
25426 tree copy;
25427 unsigned ix;
25429 if (!default_arg)
25430 continue;
25432 if (TREE_CODE (default_arg) != DEFAULT_ARG)
25433 /* This can happen for a friend declaration for a function
25434 already declared with default arguments. */
25435 continue;
25437 parsed_arg
25438 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
25439 default_arg,
25440 TREE_VALUE (parm));
25441 if (parsed_arg == error_mark_node)
25443 continue;
25446 TREE_PURPOSE (parm) = parsed_arg;
25448 /* Update any instantiations we've already created. */
25449 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
25450 vec_safe_iterate (insts, ix, &copy); ix++)
25451 TREE_PURPOSE (copy) = parsed_arg;
25454 pop_defarg_context ();
25456 /* Make sure no default arg is missing. */
25457 check_default_args (fn);
25459 /* Restore the state of local_variables_forbidden_p. */
25460 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25462 /* Restore the queue. */
25463 pop_unparsed_function_queues (parser);
25466 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
25468 sizeof ... ( identifier )
25470 where the 'sizeof' token has already been consumed. */
25472 static tree
25473 cp_parser_sizeof_pack (cp_parser *parser)
25475 /* Consume the `...'. */
25476 cp_lexer_consume_token (parser->lexer);
25477 maybe_warn_variadic_templates ();
25479 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
25480 if (paren)
25481 cp_lexer_consume_token (parser->lexer);
25482 else
25483 permerror (cp_lexer_peek_token (parser->lexer)->location,
25484 "%<sizeof...%> argument must be surrounded by parentheses");
25486 cp_token *token = cp_lexer_peek_token (parser->lexer);
25487 tree name = cp_parser_identifier (parser);
25488 if (name == error_mark_node)
25489 return error_mark_node;
25490 /* The name is not qualified. */
25491 parser->scope = NULL_TREE;
25492 parser->qualifying_scope = NULL_TREE;
25493 parser->object_scope = NULL_TREE;
25494 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
25495 if (expr == error_mark_node)
25496 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
25497 token->location);
25498 if (TREE_CODE (expr) == TYPE_DECL)
25499 expr = TREE_TYPE (expr);
25500 else if (TREE_CODE (expr) == CONST_DECL)
25501 expr = DECL_INITIAL (expr);
25502 expr = make_pack_expansion (expr);
25504 if (paren)
25505 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25507 return expr;
25510 /* Parse the operand of `sizeof' (or a similar operator). Returns
25511 either a TYPE or an expression, depending on the form of the
25512 input. The KEYWORD indicates which kind of expression we have
25513 encountered. */
25515 static tree
25516 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
25518 tree expr = NULL_TREE;
25519 const char *saved_message;
25520 char *tmp;
25521 bool saved_integral_constant_expression_p;
25522 bool saved_non_integral_constant_expression_p;
25524 /* If it's a `...', then we are computing the length of a parameter
25525 pack. */
25526 if (keyword == RID_SIZEOF
25527 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25528 return cp_parser_sizeof_pack (parser);
25530 /* Types cannot be defined in a `sizeof' expression. Save away the
25531 old message. */
25532 saved_message = parser->type_definition_forbidden_message;
25533 /* And create the new one. */
25534 tmp = concat ("types may not be defined in %<",
25535 IDENTIFIER_POINTER (ridpointers[keyword]),
25536 "%> expressions", NULL);
25537 parser->type_definition_forbidden_message = tmp;
25539 /* The restrictions on constant-expressions do not apply inside
25540 sizeof expressions. */
25541 saved_integral_constant_expression_p
25542 = parser->integral_constant_expression_p;
25543 saved_non_integral_constant_expression_p
25544 = parser->non_integral_constant_expression_p;
25545 parser->integral_constant_expression_p = false;
25547 /* Do not actually evaluate the expression. */
25548 ++cp_unevaluated_operand;
25549 ++c_inhibit_evaluation_warnings;
25550 /* If it's a `(', then we might be looking at the type-id
25551 construction. */
25552 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25554 tree type = NULL_TREE;
25556 /* We can't be sure yet whether we're looking at a type-id or an
25557 expression. */
25558 cp_parser_parse_tentatively (parser);
25559 /* Note: as a GNU Extension, compound literals are considered
25560 postfix-expressions as they are in C99, so they are valid
25561 arguments to sizeof. See comment in cp_parser_cast_expression
25562 for details. */
25563 if (cp_parser_compound_literal_p (parser))
25564 cp_parser_simulate_error (parser);
25565 else
25567 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
25568 parser->in_type_id_in_expr_p = true;
25569 /* Look for the type-id. */
25570 type = cp_parser_type_id (parser);
25571 /* Look for the closing `)'. */
25572 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25573 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
25576 /* If all went well, then we're done. */
25577 if (cp_parser_parse_definitely (parser))
25579 cp_decl_specifier_seq decl_specs;
25581 /* Build a trivial decl-specifier-seq. */
25582 clear_decl_specs (&decl_specs);
25583 decl_specs.type = type;
25585 /* Call grokdeclarator to figure out what type this is. */
25586 expr = grokdeclarator (NULL,
25587 &decl_specs,
25588 TYPENAME,
25589 /*initialized=*/0,
25590 /*attrlist=*/NULL);
25594 /* If the type-id production did not work out, then we must be
25595 looking at the unary-expression production. */
25596 if (!expr)
25597 expr = cp_parser_unary_expression (parser);
25599 /* Go back to evaluating expressions. */
25600 --cp_unevaluated_operand;
25601 --c_inhibit_evaluation_warnings;
25603 /* Free the message we created. */
25604 free (tmp);
25605 /* And restore the old one. */
25606 parser->type_definition_forbidden_message = saved_message;
25607 parser->integral_constant_expression_p
25608 = saved_integral_constant_expression_p;
25609 parser->non_integral_constant_expression_p
25610 = saved_non_integral_constant_expression_p;
25612 return expr;
25615 /* If the current declaration has no declarator, return true. */
25617 static bool
25618 cp_parser_declares_only_class_p (cp_parser *parser)
25620 /* If the next token is a `;' or a `,' then there is no
25621 declarator. */
25622 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25623 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
25626 /* Update the DECL_SPECS to reflect the storage class indicated by
25627 KEYWORD. */
25629 static void
25630 cp_parser_set_storage_class (cp_parser *parser,
25631 cp_decl_specifier_seq *decl_specs,
25632 enum rid keyword,
25633 cp_token *token)
25635 cp_storage_class storage_class;
25637 if (parser->in_unbraced_linkage_specification_p)
25639 error_at (token->location, "invalid use of %qD in linkage specification",
25640 ridpointers[keyword]);
25641 return;
25643 else if (decl_specs->storage_class != sc_none)
25645 decl_specs->conflicting_specifiers_p = true;
25646 return;
25649 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
25650 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
25651 && decl_specs->gnu_thread_keyword_p)
25653 pedwarn (decl_specs->locations[ds_thread], 0,
25654 "%<__thread%> before %qD", ridpointers[keyword]);
25657 switch (keyword)
25659 case RID_AUTO:
25660 storage_class = sc_auto;
25661 break;
25662 case RID_REGISTER:
25663 storage_class = sc_register;
25664 break;
25665 case RID_STATIC:
25666 storage_class = sc_static;
25667 break;
25668 case RID_EXTERN:
25669 storage_class = sc_extern;
25670 break;
25671 case RID_MUTABLE:
25672 storage_class = sc_mutable;
25673 break;
25674 default:
25675 gcc_unreachable ();
25677 decl_specs->storage_class = storage_class;
25678 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
25680 /* A storage class specifier cannot be applied alongside a typedef
25681 specifier. If there is a typedef specifier present then set
25682 conflicting_specifiers_p which will trigger an error later
25683 on in grokdeclarator. */
25684 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
25685 decl_specs->conflicting_specifiers_p = true;
25688 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
25689 is true, the type is a class or enum definition. */
25691 static void
25692 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
25693 tree type_spec,
25694 cp_token *token,
25695 bool type_definition_p)
25697 decl_specs->any_specifiers_p = true;
25699 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
25700 (with, for example, in "typedef int wchar_t;") we remember that
25701 this is what happened. In system headers, we ignore these
25702 declarations so that G++ can work with system headers that are not
25703 C++-safe. */
25704 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
25705 && !type_definition_p
25706 && (type_spec == boolean_type_node
25707 || type_spec == char16_type_node
25708 || type_spec == char32_type_node
25709 || type_spec == wchar_type_node)
25710 && (decl_specs->type
25711 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
25712 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
25713 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
25714 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
25716 decl_specs->redefined_builtin_type = type_spec;
25717 set_and_check_decl_spec_loc (decl_specs,
25718 ds_redefined_builtin_type_spec,
25719 token);
25720 if (!decl_specs->type)
25722 decl_specs->type = type_spec;
25723 decl_specs->type_definition_p = false;
25724 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
25727 else if (decl_specs->type)
25728 decl_specs->multiple_types_p = true;
25729 else
25731 decl_specs->type = type_spec;
25732 decl_specs->type_definition_p = type_definition_p;
25733 decl_specs->redefined_builtin_type = NULL_TREE;
25734 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
25738 /* True iff TOKEN is the GNU keyword __thread. */
25740 static bool
25741 token_is__thread (cp_token *token)
25743 gcc_assert (token->keyword == RID_THREAD);
25744 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
25747 /* Set the location for a declarator specifier and check if it is
25748 duplicated.
25750 DECL_SPECS is the sequence of declarator specifiers onto which to
25751 set the location.
25753 DS is the single declarator specifier to set which location is to
25754 be set onto the existing sequence of declarators.
25756 LOCATION is the location for the declarator specifier to
25757 consider. */
25759 static void
25760 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
25761 cp_decl_spec ds, cp_token *token)
25763 gcc_assert (ds < ds_last);
25765 if (decl_specs == NULL)
25766 return;
25768 source_location location = token->location;
25770 if (decl_specs->locations[ds] == 0)
25772 decl_specs->locations[ds] = location;
25773 if (ds == ds_thread)
25774 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
25776 else
25778 if (ds == ds_long)
25780 if (decl_specs->locations[ds_long_long] != 0)
25781 error_at (location,
25782 "%<long long long%> is too long for GCC");
25783 else
25785 decl_specs->locations[ds_long_long] = location;
25786 pedwarn_cxx98 (location,
25787 OPT_Wlong_long,
25788 "ISO C++ 1998 does not support %<long long%>");
25791 else if (ds == ds_thread)
25793 bool gnu = token_is__thread (token);
25794 if (gnu != decl_specs->gnu_thread_keyword_p)
25795 error_at (location,
25796 "both %<__thread%> and %<thread_local%> specified");
25797 else
25798 error_at (location, "duplicate %qD", token->u.value);
25800 else
25802 static const char *const decl_spec_names[] = {
25803 "signed",
25804 "unsigned",
25805 "short",
25806 "long",
25807 "const",
25808 "volatile",
25809 "restrict",
25810 "inline",
25811 "virtual",
25812 "explicit",
25813 "friend",
25814 "typedef",
25815 "using",
25816 "constexpr",
25817 "__complex"
25819 error_at (location,
25820 "duplicate %qs", decl_spec_names[ds]);
25825 /* Return true iff the declarator specifier DS is present in the
25826 sequence of declarator specifiers DECL_SPECS. */
25828 bool
25829 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
25830 cp_decl_spec ds)
25832 gcc_assert (ds < ds_last);
25834 if (decl_specs == NULL)
25835 return false;
25837 return decl_specs->locations[ds] != 0;
25840 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
25841 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
25843 static bool
25844 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
25846 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
25849 /* Issue an error message indicating that TOKEN_DESC was expected.
25850 If KEYWORD is true, it indicated this function is called by
25851 cp_parser_require_keword and the required token can only be
25852 a indicated keyword. */
25854 static void
25855 cp_parser_required_error (cp_parser *parser,
25856 required_token token_desc,
25857 bool keyword)
25859 switch (token_desc)
25861 case RT_NEW:
25862 cp_parser_error (parser, "expected %<new%>");
25863 return;
25864 case RT_DELETE:
25865 cp_parser_error (parser, "expected %<delete%>");
25866 return;
25867 case RT_RETURN:
25868 cp_parser_error (parser, "expected %<return%>");
25869 return;
25870 case RT_WHILE:
25871 cp_parser_error (parser, "expected %<while%>");
25872 return;
25873 case RT_EXTERN:
25874 cp_parser_error (parser, "expected %<extern%>");
25875 return;
25876 case RT_STATIC_ASSERT:
25877 cp_parser_error (parser, "expected %<static_assert%>");
25878 return;
25879 case RT_DECLTYPE:
25880 cp_parser_error (parser, "expected %<decltype%>");
25881 return;
25882 case RT_OPERATOR:
25883 cp_parser_error (parser, "expected %<operator%>");
25884 return;
25885 case RT_CLASS:
25886 cp_parser_error (parser, "expected %<class%>");
25887 return;
25888 case RT_TEMPLATE:
25889 cp_parser_error (parser, "expected %<template%>");
25890 return;
25891 case RT_NAMESPACE:
25892 cp_parser_error (parser, "expected %<namespace%>");
25893 return;
25894 case RT_USING:
25895 cp_parser_error (parser, "expected %<using%>");
25896 return;
25897 case RT_ASM:
25898 cp_parser_error (parser, "expected %<asm%>");
25899 return;
25900 case RT_TRY:
25901 cp_parser_error (parser, "expected %<try%>");
25902 return;
25903 case RT_CATCH:
25904 cp_parser_error (parser, "expected %<catch%>");
25905 return;
25906 case RT_THROW:
25907 cp_parser_error (parser, "expected %<throw%>");
25908 return;
25909 case RT_LABEL:
25910 cp_parser_error (parser, "expected %<__label__%>");
25911 return;
25912 case RT_AT_TRY:
25913 cp_parser_error (parser, "expected %<@try%>");
25914 return;
25915 case RT_AT_SYNCHRONIZED:
25916 cp_parser_error (parser, "expected %<@synchronized%>");
25917 return;
25918 case RT_AT_THROW:
25919 cp_parser_error (parser, "expected %<@throw%>");
25920 return;
25921 case RT_TRANSACTION_ATOMIC:
25922 cp_parser_error (parser, "expected %<__transaction_atomic%>");
25923 return;
25924 case RT_TRANSACTION_RELAXED:
25925 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
25926 return;
25927 default:
25928 break;
25930 if (!keyword)
25932 switch (token_desc)
25934 case RT_SEMICOLON:
25935 cp_parser_error (parser, "expected %<;%>");
25936 return;
25937 case RT_OPEN_PAREN:
25938 cp_parser_error (parser, "expected %<(%>");
25939 return;
25940 case RT_CLOSE_BRACE:
25941 cp_parser_error (parser, "expected %<}%>");
25942 return;
25943 case RT_OPEN_BRACE:
25944 cp_parser_error (parser, "expected %<{%>");
25945 return;
25946 case RT_CLOSE_SQUARE:
25947 cp_parser_error (parser, "expected %<]%>");
25948 return;
25949 case RT_OPEN_SQUARE:
25950 cp_parser_error (parser, "expected %<[%>");
25951 return;
25952 case RT_COMMA:
25953 cp_parser_error (parser, "expected %<,%>");
25954 return;
25955 case RT_SCOPE:
25956 cp_parser_error (parser, "expected %<::%>");
25957 return;
25958 case RT_LESS:
25959 cp_parser_error (parser, "expected %<<%>");
25960 return;
25961 case RT_GREATER:
25962 cp_parser_error (parser, "expected %<>%>");
25963 return;
25964 case RT_EQ:
25965 cp_parser_error (parser, "expected %<=%>");
25966 return;
25967 case RT_ELLIPSIS:
25968 cp_parser_error (parser, "expected %<...%>");
25969 return;
25970 case RT_MULT:
25971 cp_parser_error (parser, "expected %<*%>");
25972 return;
25973 case RT_COMPL:
25974 cp_parser_error (parser, "expected %<~%>");
25975 return;
25976 case RT_COLON:
25977 cp_parser_error (parser, "expected %<:%>");
25978 return;
25979 case RT_COLON_SCOPE:
25980 cp_parser_error (parser, "expected %<:%> or %<::%>");
25981 return;
25982 case RT_CLOSE_PAREN:
25983 cp_parser_error (parser, "expected %<)%>");
25984 return;
25985 case RT_COMMA_CLOSE_PAREN:
25986 cp_parser_error (parser, "expected %<,%> or %<)%>");
25987 return;
25988 case RT_PRAGMA_EOL:
25989 cp_parser_error (parser, "expected end of line");
25990 return;
25991 case RT_NAME:
25992 cp_parser_error (parser, "expected identifier");
25993 return;
25994 case RT_SELECT:
25995 cp_parser_error (parser, "expected selection-statement");
25996 return;
25997 case RT_INTERATION:
25998 cp_parser_error (parser, "expected iteration-statement");
25999 return;
26000 case RT_JUMP:
26001 cp_parser_error (parser, "expected jump-statement");
26002 return;
26003 case RT_CLASS_KEY:
26004 cp_parser_error (parser, "expected class-key");
26005 return;
26006 case RT_CLASS_TYPENAME_TEMPLATE:
26007 cp_parser_error (parser,
26008 "expected %<class%>, %<typename%>, or %<template%>");
26009 return;
26010 default:
26011 gcc_unreachable ();
26014 else
26015 gcc_unreachable ();
26020 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26021 issue an error message indicating that TOKEN_DESC was expected.
26023 Returns the token consumed, if the token had the appropriate type.
26024 Otherwise, returns NULL. */
26026 static cp_token *
26027 cp_parser_require (cp_parser* parser,
26028 enum cpp_ttype type,
26029 required_token token_desc)
26031 if (cp_lexer_next_token_is (parser->lexer, type))
26032 return cp_lexer_consume_token (parser->lexer);
26033 else
26035 /* Output the MESSAGE -- unless we're parsing tentatively. */
26036 if (!cp_parser_simulate_error (parser))
26037 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26038 return NULL;
26042 /* An error message is produced if the next token is not '>'.
26043 All further tokens are skipped until the desired token is
26044 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26046 static void
26047 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26049 /* Current level of '< ... >'. */
26050 unsigned level = 0;
26051 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26052 unsigned nesting_depth = 0;
26054 /* Are we ready, yet? If not, issue error message. */
26055 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26056 return;
26058 /* Skip tokens until the desired token is found. */
26059 while (true)
26061 /* Peek at the next token. */
26062 switch (cp_lexer_peek_token (parser->lexer)->type)
26064 case CPP_LESS:
26065 if (!nesting_depth)
26066 ++level;
26067 break;
26069 case CPP_RSHIFT:
26070 if (cxx_dialect == cxx98)
26071 /* C++0x views the `>>' operator as two `>' tokens, but
26072 C++98 does not. */
26073 break;
26074 else if (!nesting_depth && level-- == 0)
26076 /* We've hit a `>>' where the first `>' closes the
26077 template argument list, and the second `>' is
26078 spurious. Just consume the `>>' and stop; we've
26079 already produced at least one error. */
26080 cp_lexer_consume_token (parser->lexer);
26081 return;
26083 /* Fall through for C++0x, so we handle the second `>' in
26084 the `>>'. */
26086 case CPP_GREATER:
26087 if (!nesting_depth && level-- == 0)
26089 /* We've reached the token we want, consume it and stop. */
26090 cp_lexer_consume_token (parser->lexer);
26091 return;
26093 break;
26095 case CPP_OPEN_PAREN:
26096 case CPP_OPEN_SQUARE:
26097 ++nesting_depth;
26098 break;
26100 case CPP_CLOSE_PAREN:
26101 case CPP_CLOSE_SQUARE:
26102 if (nesting_depth-- == 0)
26103 return;
26104 break;
26106 case CPP_EOF:
26107 case CPP_PRAGMA_EOL:
26108 case CPP_SEMICOLON:
26109 case CPP_OPEN_BRACE:
26110 case CPP_CLOSE_BRACE:
26111 /* The '>' was probably forgotten, don't look further. */
26112 return;
26114 default:
26115 break;
26118 /* Consume this token. */
26119 cp_lexer_consume_token (parser->lexer);
26123 /* If the next token is the indicated keyword, consume it. Otherwise,
26124 issue an error message indicating that TOKEN_DESC was expected.
26126 Returns the token consumed, if the token had the appropriate type.
26127 Otherwise, returns NULL. */
26129 static cp_token *
26130 cp_parser_require_keyword (cp_parser* parser,
26131 enum rid keyword,
26132 required_token token_desc)
26134 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
26136 if (token && token->keyword != keyword)
26138 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
26139 return NULL;
26142 return token;
26145 /* Returns TRUE iff TOKEN is a token that can begin the body of a
26146 function-definition. */
26148 static bool
26149 cp_parser_token_starts_function_definition_p (cp_token* token)
26151 return (/* An ordinary function-body begins with an `{'. */
26152 token->type == CPP_OPEN_BRACE
26153 /* A ctor-initializer begins with a `:'. */
26154 || token->type == CPP_COLON
26155 /* A function-try-block begins with `try'. */
26156 || token->keyword == RID_TRY
26157 /* A function-transaction-block begins with `__transaction_atomic'
26158 or `__transaction_relaxed'. */
26159 || token->keyword == RID_TRANSACTION_ATOMIC
26160 || token->keyword == RID_TRANSACTION_RELAXED
26161 /* The named return value extension begins with `return'. */
26162 || token->keyword == RID_RETURN);
26165 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
26166 definition. */
26168 static bool
26169 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
26171 cp_token *token;
26173 token = cp_lexer_peek_token (parser->lexer);
26174 return (token->type == CPP_OPEN_BRACE
26175 || (token->type == CPP_COLON
26176 && !parser->colon_doesnt_start_class_def_p));
26179 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
26180 C++0x) ending a template-argument. */
26182 static bool
26183 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
26185 cp_token *token;
26187 token = cp_lexer_peek_token (parser->lexer);
26188 return (token->type == CPP_COMMA
26189 || token->type == CPP_GREATER
26190 || token->type == CPP_ELLIPSIS
26191 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
26194 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
26195 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
26197 static bool
26198 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
26199 size_t n)
26201 cp_token *token;
26203 token = cp_lexer_peek_nth_token (parser->lexer, n);
26204 if (token->type == CPP_LESS)
26205 return true;
26206 /* Check for the sequence `<::' in the original code. It would be lexed as
26207 `[:', where `[' is a digraph, and there is no whitespace before
26208 `:'. */
26209 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
26211 cp_token *token2;
26212 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
26213 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
26214 return true;
26216 return false;
26219 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
26220 or none_type otherwise. */
26222 static enum tag_types
26223 cp_parser_token_is_class_key (cp_token* token)
26225 switch (token->keyword)
26227 case RID_CLASS:
26228 return class_type;
26229 case RID_STRUCT:
26230 return record_type;
26231 case RID_UNION:
26232 return union_type;
26234 default:
26235 return none_type;
26239 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
26240 or none_type otherwise or if the token is null. */
26242 static enum tag_types
26243 cp_parser_token_is_type_parameter_key (cp_token* token)
26245 if (!token)
26246 return none_type;
26248 switch (token->keyword)
26250 case RID_CLASS:
26251 return class_type;
26252 case RID_TYPENAME:
26253 return typename_type;
26255 default:
26256 return none_type;
26260 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
26262 static void
26263 cp_parser_check_class_key (enum tag_types class_key, tree type)
26265 if (type == error_mark_node)
26266 return;
26267 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
26269 if (permerror (input_location, "%qs tag used in naming %q#T",
26270 class_key == union_type ? "union"
26271 : class_key == record_type ? "struct" : "class",
26272 type))
26273 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
26274 "%q#T was previously declared here", type);
26278 /* Issue an error message if DECL is redeclared with different
26279 access than its original declaration [class.access.spec/3].
26280 This applies to nested classes and nested class templates.
26281 [class.mem/1]. */
26283 static void
26284 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
26286 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
26287 return;
26289 if ((TREE_PRIVATE (decl)
26290 != (current_access_specifier == access_private_node))
26291 || (TREE_PROTECTED (decl)
26292 != (current_access_specifier == access_protected_node)))
26293 error_at (location, "%qD redeclared with different access", decl);
26296 /* Look for the `template' keyword, as a syntactic disambiguator.
26297 Return TRUE iff it is present, in which case it will be
26298 consumed. */
26300 static bool
26301 cp_parser_optional_template_keyword (cp_parser *parser)
26303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26305 /* In C++98 the `template' keyword can only be used within templates;
26306 outside templates the parser can always figure out what is a
26307 template and what is not. In C++11, per the resolution of DR 468,
26308 `template' is allowed in cases where it is not strictly necessary. */
26309 if (!processing_template_decl
26310 && pedantic && cxx_dialect == cxx98)
26312 cp_token *token = cp_lexer_peek_token (parser->lexer);
26313 pedwarn (token->location, OPT_Wpedantic,
26314 "in C++98 %<template%> (as a disambiguator) is only "
26315 "allowed within templates");
26316 /* If this part of the token stream is rescanned, the same
26317 error message would be generated. So, we purge the token
26318 from the stream. */
26319 cp_lexer_purge_token (parser->lexer);
26320 return false;
26322 else
26324 /* Consume the `template' keyword. */
26325 cp_lexer_consume_token (parser->lexer);
26326 return true;
26329 return false;
26332 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
26333 set PARSER->SCOPE, and perform other related actions. */
26335 static void
26336 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
26338 int i;
26339 struct tree_check *check_value;
26340 deferred_access_check *chk;
26341 vec<deferred_access_check, va_gc> *checks;
26343 /* Get the stored value. */
26344 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
26345 /* Perform any access checks that were deferred. */
26346 checks = check_value->checks;
26347 if (checks)
26349 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
26350 perform_or_defer_access_check (chk->binfo,
26351 chk->decl,
26352 chk->diag_decl, tf_warning_or_error);
26354 /* Set the scope from the stored value. */
26355 parser->scope = check_value->value;
26356 parser->qualifying_scope = check_value->qualifying_scope;
26357 parser->object_scope = NULL_TREE;
26360 /* Consume tokens up through a non-nested END token. Returns TRUE if we
26361 encounter the end of a block before what we were looking for. */
26363 static bool
26364 cp_parser_cache_group (cp_parser *parser,
26365 enum cpp_ttype end,
26366 unsigned depth)
26368 while (true)
26370 cp_token *token = cp_lexer_peek_token (parser->lexer);
26372 /* Abort a parenthesized expression if we encounter a semicolon. */
26373 if ((end == CPP_CLOSE_PAREN || depth == 0)
26374 && token->type == CPP_SEMICOLON)
26375 return true;
26376 /* If we've reached the end of the file, stop. */
26377 if (token->type == CPP_EOF
26378 || (end != CPP_PRAGMA_EOL
26379 && token->type == CPP_PRAGMA_EOL))
26380 return true;
26381 if (token->type == CPP_CLOSE_BRACE && depth == 0)
26382 /* We've hit the end of an enclosing block, so there's been some
26383 kind of syntax error. */
26384 return true;
26386 /* Consume the token. */
26387 cp_lexer_consume_token (parser->lexer);
26388 /* See if it starts a new group. */
26389 if (token->type == CPP_OPEN_BRACE)
26391 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
26392 /* In theory this should probably check end == '}', but
26393 cp_parser_save_member_function_body needs it to exit
26394 after either '}' or ')' when called with ')'. */
26395 if (depth == 0)
26396 return false;
26398 else if (token->type == CPP_OPEN_PAREN)
26400 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
26401 if (depth == 0 && end == CPP_CLOSE_PAREN)
26402 return false;
26404 else if (token->type == CPP_PRAGMA)
26405 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
26406 else if (token->type == end)
26407 return false;
26411 /* Like above, for caching a default argument or NSDMI. Both of these are
26412 terminated by a non-nested comma, but it can be unclear whether or not a
26413 comma is nested in a template argument list unless we do more parsing.
26414 In order to handle this ambiguity, when we encounter a ',' after a '<'
26415 we try to parse what follows as a parameter-declaration-list (in the
26416 case of a default argument) or a member-declarator (in the case of an
26417 NSDMI). If that succeeds, then we stop caching. */
26419 static tree
26420 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
26422 unsigned depth = 0;
26423 int maybe_template_id = 0;
26424 cp_token *first_token;
26425 cp_token *token;
26426 tree default_argument;
26428 /* Add tokens until we have processed the entire default
26429 argument. We add the range [first_token, token). */
26430 first_token = cp_lexer_peek_token (parser->lexer);
26431 if (first_token->type == CPP_OPEN_BRACE)
26433 /* For list-initialization, this is straightforward. */
26434 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26435 token = cp_lexer_peek_token (parser->lexer);
26437 else while (true)
26439 bool done = false;
26441 /* Peek at the next token. */
26442 token = cp_lexer_peek_token (parser->lexer);
26443 /* What we do depends on what token we have. */
26444 switch (token->type)
26446 /* In valid code, a default argument must be
26447 immediately followed by a `,' `)', or `...'. */
26448 case CPP_COMMA:
26449 if (depth == 0 && maybe_template_id)
26451 /* If we've seen a '<', we might be in a
26452 template-argument-list. Until Core issue 325 is
26453 resolved, we don't know how this situation ought
26454 to be handled, so try to DTRT. We check whether
26455 what comes after the comma is a valid parameter
26456 declaration list. If it is, then the comma ends
26457 the default argument; otherwise the default
26458 argument continues. */
26459 bool error = false;
26461 /* Set ITALP so cp_parser_parameter_declaration_list
26462 doesn't decide to commit to this parse. */
26463 bool saved_italp = parser->in_template_argument_list_p;
26464 parser->in_template_argument_list_p = true;
26466 cp_parser_parse_tentatively (parser);
26467 cp_lexer_consume_token (parser->lexer);
26469 if (nsdmi)
26471 int ctor_dtor_or_conv_p;
26472 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26473 &ctor_dtor_or_conv_p,
26474 /*parenthesized_p=*/NULL,
26475 /*member_p=*/true,
26476 /*friend_p=*/false);
26478 else
26480 begin_scope (sk_function_parms, NULL_TREE);
26481 cp_parser_parameter_declaration_list (parser, &error);
26482 pop_bindings_and_leave_scope ();
26484 if (!cp_parser_error_occurred (parser) && !error)
26485 done = true;
26486 cp_parser_abort_tentative_parse (parser);
26488 parser->in_template_argument_list_p = saved_italp;
26489 break;
26491 case CPP_CLOSE_PAREN:
26492 case CPP_ELLIPSIS:
26493 /* If we run into a non-nested `;', `}', or `]',
26494 then the code is invalid -- but the default
26495 argument is certainly over. */
26496 case CPP_SEMICOLON:
26497 case CPP_CLOSE_BRACE:
26498 case CPP_CLOSE_SQUARE:
26499 if (depth == 0
26500 /* Handle correctly int n = sizeof ... ( p ); */
26501 && token->type != CPP_ELLIPSIS)
26502 done = true;
26503 /* Update DEPTH, if necessary. */
26504 else if (token->type == CPP_CLOSE_PAREN
26505 || token->type == CPP_CLOSE_BRACE
26506 || token->type == CPP_CLOSE_SQUARE)
26507 --depth;
26508 break;
26510 case CPP_OPEN_PAREN:
26511 case CPP_OPEN_SQUARE:
26512 case CPP_OPEN_BRACE:
26513 ++depth;
26514 break;
26516 case CPP_LESS:
26517 if (depth == 0)
26518 /* This might be the comparison operator, or it might
26519 start a template argument list. */
26520 ++maybe_template_id;
26521 break;
26523 case CPP_RSHIFT:
26524 if (cxx_dialect == cxx98)
26525 break;
26526 /* Fall through for C++0x, which treats the `>>'
26527 operator like two `>' tokens in certain
26528 cases. */
26530 case CPP_GREATER:
26531 if (depth == 0)
26533 /* This might be an operator, or it might close a
26534 template argument list. But if a previous '<'
26535 started a template argument list, this will have
26536 closed it, so we can't be in one anymore. */
26537 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
26538 if (maybe_template_id < 0)
26539 maybe_template_id = 0;
26541 break;
26543 /* If we run out of tokens, issue an error message. */
26544 case CPP_EOF:
26545 case CPP_PRAGMA_EOL:
26546 error_at (token->location, "file ends in default argument");
26547 done = true;
26548 break;
26550 case CPP_NAME:
26551 case CPP_SCOPE:
26552 /* In these cases, we should look for template-ids.
26553 For example, if the default argument is
26554 `X<int, double>()', we need to do name lookup to
26555 figure out whether or not `X' is a template; if
26556 so, the `,' does not end the default argument.
26558 That is not yet done. */
26559 break;
26561 default:
26562 break;
26565 /* If we've reached the end, stop. */
26566 if (done)
26567 break;
26569 /* Add the token to the token block. */
26570 token = cp_lexer_consume_token (parser->lexer);
26573 /* Create a DEFAULT_ARG to represent the unparsed default
26574 argument. */
26575 default_argument = make_node (DEFAULT_ARG);
26576 DEFARG_TOKENS (default_argument)
26577 = cp_token_cache_new (first_token, token);
26578 DEFARG_INSTANTIATIONS (default_argument) = NULL;
26580 return default_argument;
26583 /* Begin parsing tentatively. We always save tokens while parsing
26584 tentatively so that if the tentative parsing fails we can restore the
26585 tokens. */
26587 static void
26588 cp_parser_parse_tentatively (cp_parser* parser)
26590 /* Enter a new parsing context. */
26591 parser->context = cp_parser_context_new (parser->context);
26592 /* Begin saving tokens. */
26593 cp_lexer_save_tokens (parser->lexer);
26594 /* In order to avoid repetitive access control error messages,
26595 access checks are queued up until we are no longer parsing
26596 tentatively. */
26597 push_deferring_access_checks (dk_deferred);
26600 /* Commit to the currently active tentative parse. */
26602 static void
26603 cp_parser_commit_to_tentative_parse (cp_parser* parser)
26605 cp_parser_context *context;
26606 cp_lexer *lexer;
26608 /* Mark all of the levels as committed. */
26609 lexer = parser->lexer;
26610 for (context = parser->context; context->next; context = context->next)
26612 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
26613 break;
26614 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
26615 while (!cp_lexer_saving_tokens (lexer))
26616 lexer = lexer->next;
26617 cp_lexer_commit_tokens (lexer);
26621 /* Commit to the topmost currently active tentative parse.
26623 Note that this function shouldn't be called when there are
26624 irreversible side-effects while in a tentative state. For
26625 example, we shouldn't create a permanent entry in the symbol
26626 table, or issue an error message that might not apply if the
26627 tentative parse is aborted. */
26629 static void
26630 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
26632 cp_parser_context *context = parser->context;
26633 cp_lexer *lexer = parser->lexer;
26635 if (context)
26637 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
26638 return;
26639 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
26641 while (!cp_lexer_saving_tokens (lexer))
26642 lexer = lexer->next;
26643 cp_lexer_commit_tokens (lexer);
26647 /* Abort the currently active tentative parse. All consumed tokens
26648 will be rolled back, and no diagnostics will be issued. */
26650 static void
26651 cp_parser_abort_tentative_parse (cp_parser* parser)
26653 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
26654 || errorcount > 0);
26655 cp_parser_simulate_error (parser);
26656 /* Now, pretend that we want to see if the construct was
26657 successfully parsed. */
26658 cp_parser_parse_definitely (parser);
26661 /* Stop parsing tentatively. If a parse error has occurred, restore the
26662 token stream. Otherwise, commit to the tokens we have consumed.
26663 Returns true if no error occurred; false otherwise. */
26665 static bool
26666 cp_parser_parse_definitely (cp_parser* parser)
26668 bool error_occurred;
26669 cp_parser_context *context;
26671 /* Remember whether or not an error occurred, since we are about to
26672 destroy that information. */
26673 error_occurred = cp_parser_error_occurred (parser);
26674 /* Remove the topmost context from the stack. */
26675 context = parser->context;
26676 parser->context = context->next;
26677 /* If no parse errors occurred, commit to the tentative parse. */
26678 if (!error_occurred)
26680 /* Commit to the tokens read tentatively, unless that was
26681 already done. */
26682 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
26683 cp_lexer_commit_tokens (parser->lexer);
26685 pop_to_parent_deferring_access_checks ();
26687 /* Otherwise, if errors occurred, roll back our state so that things
26688 are just as they were before we began the tentative parse. */
26689 else
26691 cp_lexer_rollback_tokens (parser->lexer);
26692 pop_deferring_access_checks ();
26694 /* Add the context to the front of the free list. */
26695 context->next = cp_parser_context_free_list;
26696 cp_parser_context_free_list = context;
26698 return !error_occurred;
26701 /* Returns true if we are parsing tentatively and are not committed to
26702 this tentative parse. */
26704 static bool
26705 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
26707 return (cp_parser_parsing_tentatively (parser)
26708 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
26711 /* Returns nonzero iff an error has occurred during the most recent
26712 tentative parse. */
26714 static bool
26715 cp_parser_error_occurred (cp_parser* parser)
26717 return (cp_parser_parsing_tentatively (parser)
26718 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
26721 /* Returns nonzero if GNU extensions are allowed. */
26723 static bool
26724 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
26726 return parser->allow_gnu_extensions_p;
26729 /* Objective-C++ Productions */
26732 /* Parse an Objective-C expression, which feeds into a primary-expression
26733 above.
26735 objc-expression:
26736 objc-message-expression
26737 objc-string-literal
26738 objc-encode-expression
26739 objc-protocol-expression
26740 objc-selector-expression
26742 Returns a tree representation of the expression. */
26744 static tree
26745 cp_parser_objc_expression (cp_parser* parser)
26747 /* Try to figure out what kind of declaration is present. */
26748 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26750 switch (kwd->type)
26752 case CPP_OPEN_SQUARE:
26753 return cp_parser_objc_message_expression (parser);
26755 case CPP_OBJC_STRING:
26756 kwd = cp_lexer_consume_token (parser->lexer);
26757 return objc_build_string_object (kwd->u.value);
26759 case CPP_KEYWORD:
26760 switch (kwd->keyword)
26762 case RID_AT_ENCODE:
26763 return cp_parser_objc_encode_expression (parser);
26765 case RID_AT_PROTOCOL:
26766 return cp_parser_objc_protocol_expression (parser);
26768 case RID_AT_SELECTOR:
26769 return cp_parser_objc_selector_expression (parser);
26771 default:
26772 break;
26774 default:
26775 error_at (kwd->location,
26776 "misplaced %<@%D%> Objective-C++ construct",
26777 kwd->u.value);
26778 cp_parser_skip_to_end_of_block_or_statement (parser);
26781 return error_mark_node;
26784 /* Parse an Objective-C message expression.
26786 objc-message-expression:
26787 [ objc-message-receiver objc-message-args ]
26789 Returns a representation of an Objective-C message. */
26791 static tree
26792 cp_parser_objc_message_expression (cp_parser* parser)
26794 tree receiver, messageargs;
26796 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
26797 receiver = cp_parser_objc_message_receiver (parser);
26798 messageargs = cp_parser_objc_message_args (parser);
26799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26801 return objc_build_message_expr (receiver, messageargs);
26804 /* Parse an objc-message-receiver.
26806 objc-message-receiver:
26807 expression
26808 simple-type-specifier
26810 Returns a representation of the type or expression. */
26812 static tree
26813 cp_parser_objc_message_receiver (cp_parser* parser)
26815 tree rcv;
26817 /* An Objective-C message receiver may be either (1) a type
26818 or (2) an expression. */
26819 cp_parser_parse_tentatively (parser);
26820 rcv = cp_parser_expression (parser);
26822 /* If that worked out, fine. */
26823 if (cp_parser_parse_definitely (parser))
26824 return rcv;
26826 cp_parser_parse_tentatively (parser);
26827 rcv = cp_parser_simple_type_specifier (parser,
26828 /*decl_specs=*/NULL,
26829 CP_PARSER_FLAGS_NONE);
26831 if (cp_parser_parse_definitely (parser))
26832 return objc_get_class_reference (rcv);
26834 cp_parser_error (parser, "objective-c++ message receiver expected");
26835 return error_mark_node;
26838 /* Parse the arguments and selectors comprising an Objective-C message.
26840 objc-message-args:
26841 objc-selector
26842 objc-selector-args
26843 objc-selector-args , objc-comma-args
26845 objc-selector-args:
26846 objc-selector [opt] : assignment-expression
26847 objc-selector-args objc-selector [opt] : assignment-expression
26849 objc-comma-args:
26850 assignment-expression
26851 objc-comma-args , assignment-expression
26853 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
26854 selector arguments and TREE_VALUE containing a list of comma
26855 arguments. */
26857 static tree
26858 cp_parser_objc_message_args (cp_parser* parser)
26860 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
26861 bool maybe_unary_selector_p = true;
26862 cp_token *token = cp_lexer_peek_token (parser->lexer);
26864 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26866 tree selector = NULL_TREE, arg;
26868 if (token->type != CPP_COLON)
26869 selector = cp_parser_objc_selector (parser);
26871 /* Detect if we have a unary selector. */
26872 if (maybe_unary_selector_p
26873 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26874 return build_tree_list (selector, NULL_TREE);
26876 maybe_unary_selector_p = false;
26877 cp_parser_require (parser, CPP_COLON, RT_COLON);
26878 arg = cp_parser_assignment_expression (parser);
26880 sel_args
26881 = chainon (sel_args,
26882 build_tree_list (selector, arg));
26884 token = cp_lexer_peek_token (parser->lexer);
26887 /* Handle non-selector arguments, if any. */
26888 while (token->type == CPP_COMMA)
26890 tree arg;
26892 cp_lexer_consume_token (parser->lexer);
26893 arg = cp_parser_assignment_expression (parser);
26895 addl_args
26896 = chainon (addl_args,
26897 build_tree_list (NULL_TREE, arg));
26899 token = cp_lexer_peek_token (parser->lexer);
26902 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
26904 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
26905 return build_tree_list (error_mark_node, error_mark_node);
26908 return build_tree_list (sel_args, addl_args);
26911 /* Parse an Objective-C encode expression.
26913 objc-encode-expression:
26914 @encode objc-typename
26916 Returns an encoded representation of the type argument. */
26918 static tree
26919 cp_parser_objc_encode_expression (cp_parser* parser)
26921 tree type;
26922 cp_token *token;
26924 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
26925 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26926 token = cp_lexer_peek_token (parser->lexer);
26927 type = complete_type (cp_parser_type_id (parser));
26928 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26930 if (!type)
26932 error_at (token->location,
26933 "%<@encode%> must specify a type as an argument");
26934 return error_mark_node;
26937 /* This happens if we find @encode(T) (where T is a template
26938 typename or something dependent on a template typename) when
26939 parsing a template. In that case, we can't compile it
26940 immediately, but we rather create an AT_ENCODE_EXPR which will
26941 need to be instantiated when the template is used.
26943 if (dependent_type_p (type))
26945 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
26946 TREE_READONLY (value) = 1;
26947 return value;
26950 return objc_build_encode_expr (type);
26953 /* Parse an Objective-C @defs expression. */
26955 static tree
26956 cp_parser_objc_defs_expression (cp_parser *parser)
26958 tree name;
26960 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
26961 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26962 name = cp_parser_identifier (parser);
26963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26965 return objc_get_class_ivars (name);
26968 /* Parse an Objective-C protocol expression.
26970 objc-protocol-expression:
26971 @protocol ( identifier )
26973 Returns a representation of the protocol expression. */
26975 static tree
26976 cp_parser_objc_protocol_expression (cp_parser* parser)
26978 tree proto;
26980 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26981 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26982 proto = cp_parser_identifier (parser);
26983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26985 return objc_build_protocol_expr (proto);
26988 /* Parse an Objective-C selector expression.
26990 objc-selector-expression:
26991 @selector ( objc-method-signature )
26993 objc-method-signature:
26994 objc-selector
26995 objc-selector-seq
26997 objc-selector-seq:
26998 objc-selector :
26999 objc-selector-seq objc-selector :
27001 Returns a representation of the method selector. */
27003 static tree
27004 cp_parser_objc_selector_expression (cp_parser* parser)
27006 tree sel_seq = NULL_TREE;
27007 bool maybe_unary_selector_p = true;
27008 cp_token *token;
27009 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27011 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
27012 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27013 token = cp_lexer_peek_token (parser->lexer);
27015 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
27016 || token->type == CPP_SCOPE)
27018 tree selector = NULL_TREE;
27020 if (token->type != CPP_COLON
27021 || token->type == CPP_SCOPE)
27022 selector = cp_parser_objc_selector (parser);
27024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
27025 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
27027 /* Detect if we have a unary selector. */
27028 if (maybe_unary_selector_p)
27030 sel_seq = selector;
27031 goto finish_selector;
27033 else
27035 cp_parser_error (parser, "expected %<:%>");
27038 maybe_unary_selector_p = false;
27039 token = cp_lexer_consume_token (parser->lexer);
27041 if (token->type == CPP_SCOPE)
27043 sel_seq
27044 = chainon (sel_seq,
27045 build_tree_list (selector, NULL_TREE));
27046 sel_seq
27047 = chainon (sel_seq,
27048 build_tree_list (NULL_TREE, NULL_TREE));
27050 else
27051 sel_seq
27052 = chainon (sel_seq,
27053 build_tree_list (selector, NULL_TREE));
27055 token = cp_lexer_peek_token (parser->lexer);
27058 finish_selector:
27059 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27061 return objc_build_selector_expr (loc, sel_seq);
27064 /* Parse a list of identifiers.
27066 objc-identifier-list:
27067 identifier
27068 objc-identifier-list , identifier
27070 Returns a TREE_LIST of identifier nodes. */
27072 static tree
27073 cp_parser_objc_identifier_list (cp_parser* parser)
27075 tree identifier;
27076 tree list;
27077 cp_token *sep;
27079 identifier = cp_parser_identifier (parser);
27080 if (identifier == error_mark_node)
27081 return error_mark_node;
27083 list = build_tree_list (NULL_TREE, identifier);
27084 sep = cp_lexer_peek_token (parser->lexer);
27086 while (sep->type == CPP_COMMA)
27088 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27089 identifier = cp_parser_identifier (parser);
27090 if (identifier == error_mark_node)
27091 return list;
27093 list = chainon (list, build_tree_list (NULL_TREE,
27094 identifier));
27095 sep = cp_lexer_peek_token (parser->lexer);
27098 return list;
27101 /* Parse an Objective-C alias declaration.
27103 objc-alias-declaration:
27104 @compatibility_alias identifier identifier ;
27106 This function registers the alias mapping with the Objective-C front end.
27107 It returns nothing. */
27109 static void
27110 cp_parser_objc_alias_declaration (cp_parser* parser)
27112 tree alias, orig;
27114 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
27115 alias = cp_parser_identifier (parser);
27116 orig = cp_parser_identifier (parser);
27117 objc_declare_alias (alias, orig);
27118 cp_parser_consume_semicolon_at_end_of_statement (parser);
27121 /* Parse an Objective-C class forward-declaration.
27123 objc-class-declaration:
27124 @class objc-identifier-list ;
27126 The function registers the forward declarations with the Objective-C
27127 front end. It returns nothing. */
27129 static void
27130 cp_parser_objc_class_declaration (cp_parser* parser)
27132 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
27133 while (true)
27135 tree id;
27137 id = cp_parser_identifier (parser);
27138 if (id == error_mark_node)
27139 break;
27141 objc_declare_class (id);
27143 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27144 cp_lexer_consume_token (parser->lexer);
27145 else
27146 break;
27148 cp_parser_consume_semicolon_at_end_of_statement (parser);
27151 /* Parse a list of Objective-C protocol references.
27153 objc-protocol-refs-opt:
27154 objc-protocol-refs [opt]
27156 objc-protocol-refs:
27157 < objc-identifier-list >
27159 Returns a TREE_LIST of identifiers, if any. */
27161 static tree
27162 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
27164 tree protorefs = NULL_TREE;
27166 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
27168 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
27169 protorefs = cp_parser_objc_identifier_list (parser);
27170 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
27173 return protorefs;
27176 /* Parse a Objective-C visibility specification. */
27178 static void
27179 cp_parser_objc_visibility_spec (cp_parser* parser)
27181 cp_token *vis = cp_lexer_peek_token (parser->lexer);
27183 switch (vis->keyword)
27185 case RID_AT_PRIVATE:
27186 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27187 break;
27188 case RID_AT_PROTECTED:
27189 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27190 break;
27191 case RID_AT_PUBLIC:
27192 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
27193 break;
27194 case RID_AT_PACKAGE:
27195 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27196 break;
27197 default:
27198 return;
27201 /* Eat '@private'/'@protected'/'@public'. */
27202 cp_lexer_consume_token (parser->lexer);
27205 /* Parse an Objective-C method type. Return 'true' if it is a class
27206 (+) method, and 'false' if it is an instance (-) method. */
27208 static inline bool
27209 cp_parser_objc_method_type (cp_parser* parser)
27211 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
27212 return true;
27213 else
27214 return false;
27217 /* Parse an Objective-C protocol qualifier. */
27219 static tree
27220 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
27222 tree quals = NULL_TREE, node;
27223 cp_token *token = cp_lexer_peek_token (parser->lexer);
27225 node = token->u.value;
27227 while (node && identifier_p (node)
27228 && (node == ridpointers [(int) RID_IN]
27229 || node == ridpointers [(int) RID_OUT]
27230 || node == ridpointers [(int) RID_INOUT]
27231 || node == ridpointers [(int) RID_BYCOPY]
27232 || node == ridpointers [(int) RID_BYREF]
27233 || node == ridpointers [(int) RID_ONEWAY]))
27235 quals = tree_cons (NULL_TREE, node, quals);
27236 cp_lexer_consume_token (parser->lexer);
27237 token = cp_lexer_peek_token (parser->lexer);
27238 node = token->u.value;
27241 return quals;
27244 /* Parse an Objective-C typename. */
27246 static tree
27247 cp_parser_objc_typename (cp_parser* parser)
27249 tree type_name = NULL_TREE;
27251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27253 tree proto_quals, cp_type = NULL_TREE;
27255 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27256 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
27258 /* An ObjC type name may consist of just protocol qualifiers, in which
27259 case the type shall default to 'id'. */
27260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27262 cp_type = cp_parser_type_id (parser);
27264 /* If the type could not be parsed, an error has already
27265 been produced. For error recovery, behave as if it had
27266 not been specified, which will use the default type
27267 'id'. */
27268 if (cp_type == error_mark_node)
27270 cp_type = NULL_TREE;
27271 /* We need to skip to the closing parenthesis as
27272 cp_parser_type_id() does not seem to do it for
27273 us. */
27274 cp_parser_skip_to_closing_parenthesis (parser,
27275 /*recovering=*/true,
27276 /*or_comma=*/false,
27277 /*consume_paren=*/false);
27281 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27282 type_name = build_tree_list (proto_quals, cp_type);
27285 return type_name;
27288 /* Check to see if TYPE refers to an Objective-C selector name. */
27290 static bool
27291 cp_parser_objc_selector_p (enum cpp_ttype type)
27293 return (type == CPP_NAME || type == CPP_KEYWORD
27294 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
27295 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
27296 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
27297 || type == CPP_XOR || type == CPP_XOR_EQ);
27300 /* Parse an Objective-C selector. */
27302 static tree
27303 cp_parser_objc_selector (cp_parser* parser)
27305 cp_token *token = cp_lexer_consume_token (parser->lexer);
27307 if (!cp_parser_objc_selector_p (token->type))
27309 error_at (token->location, "invalid Objective-C++ selector name");
27310 return error_mark_node;
27313 /* C++ operator names are allowed to appear in ObjC selectors. */
27314 switch (token->type)
27316 case CPP_AND_AND: return get_identifier ("and");
27317 case CPP_AND_EQ: return get_identifier ("and_eq");
27318 case CPP_AND: return get_identifier ("bitand");
27319 case CPP_OR: return get_identifier ("bitor");
27320 case CPP_COMPL: return get_identifier ("compl");
27321 case CPP_NOT: return get_identifier ("not");
27322 case CPP_NOT_EQ: return get_identifier ("not_eq");
27323 case CPP_OR_OR: return get_identifier ("or");
27324 case CPP_OR_EQ: return get_identifier ("or_eq");
27325 case CPP_XOR: return get_identifier ("xor");
27326 case CPP_XOR_EQ: return get_identifier ("xor_eq");
27327 default: return token->u.value;
27331 /* Parse an Objective-C params list. */
27333 static tree
27334 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
27336 tree params = NULL_TREE;
27337 bool maybe_unary_selector_p = true;
27338 cp_token *token = cp_lexer_peek_token (parser->lexer);
27340 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27342 tree selector = NULL_TREE, type_name, identifier;
27343 tree parm_attr = NULL_TREE;
27345 if (token->keyword == RID_ATTRIBUTE)
27346 break;
27348 if (token->type != CPP_COLON)
27349 selector = cp_parser_objc_selector (parser);
27351 /* Detect if we have a unary selector. */
27352 if (maybe_unary_selector_p
27353 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27355 params = selector; /* Might be followed by attributes. */
27356 break;
27359 maybe_unary_selector_p = false;
27360 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27362 /* Something went quite wrong. There should be a colon
27363 here, but there is not. Stop parsing parameters. */
27364 break;
27366 type_name = cp_parser_objc_typename (parser);
27367 /* New ObjC allows attributes on parameters too. */
27368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27369 parm_attr = cp_parser_attributes_opt (parser);
27370 identifier = cp_parser_identifier (parser);
27372 params
27373 = chainon (params,
27374 objc_build_keyword_decl (selector,
27375 type_name,
27376 identifier,
27377 parm_attr));
27379 token = cp_lexer_peek_token (parser->lexer);
27382 if (params == NULL_TREE)
27384 cp_parser_error (parser, "objective-c++ method declaration is expected");
27385 return error_mark_node;
27388 /* We allow tail attributes for the method. */
27389 if (token->keyword == RID_ATTRIBUTE)
27391 *attributes = cp_parser_attributes_opt (parser);
27392 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27393 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27394 return params;
27395 cp_parser_error (parser,
27396 "method attributes must be specified at the end");
27397 return error_mark_node;
27400 if (params == NULL_TREE)
27402 cp_parser_error (parser, "objective-c++ method declaration is expected");
27403 return error_mark_node;
27405 return params;
27408 /* Parse the non-keyword Objective-C params. */
27410 static tree
27411 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
27412 tree* attributes)
27414 tree params = make_node (TREE_LIST);
27415 cp_token *token = cp_lexer_peek_token (parser->lexer);
27416 *ellipsisp = false; /* Initially, assume no ellipsis. */
27418 while (token->type == CPP_COMMA)
27420 cp_parameter_declarator *parmdecl;
27421 tree parm;
27423 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27424 token = cp_lexer_peek_token (parser->lexer);
27426 if (token->type == CPP_ELLIPSIS)
27428 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
27429 *ellipsisp = true;
27430 token = cp_lexer_peek_token (parser->lexer);
27431 break;
27434 /* TODO: parse attributes for tail parameters. */
27435 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
27436 parm = grokdeclarator (parmdecl->declarator,
27437 &parmdecl->decl_specifiers,
27438 PARM, /*initialized=*/0,
27439 /*attrlist=*/NULL);
27441 chainon (params, build_tree_list (NULL_TREE, parm));
27442 token = cp_lexer_peek_token (parser->lexer);
27445 /* We allow tail attributes for the method. */
27446 if (token->keyword == RID_ATTRIBUTE)
27448 if (*attributes == NULL_TREE)
27450 *attributes = cp_parser_attributes_opt (parser);
27451 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27452 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27453 return params;
27455 else
27456 /* We have an error, but parse the attributes, so that we can
27457 carry on. */
27458 *attributes = cp_parser_attributes_opt (parser);
27460 cp_parser_error (parser,
27461 "method attributes must be specified at the end");
27462 return error_mark_node;
27465 return params;
27468 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
27470 static void
27471 cp_parser_objc_interstitial_code (cp_parser* parser)
27473 cp_token *token = cp_lexer_peek_token (parser->lexer);
27475 /* If the next token is `extern' and the following token is a string
27476 literal, then we have a linkage specification. */
27477 if (token->keyword == RID_EXTERN
27478 && cp_parser_is_pure_string_literal
27479 (cp_lexer_peek_nth_token (parser->lexer, 2)))
27480 cp_parser_linkage_specification (parser);
27481 /* Handle #pragma, if any. */
27482 else if (token->type == CPP_PRAGMA)
27483 cp_parser_pragma (parser, pragma_objc_icode);
27484 /* Allow stray semicolons. */
27485 else if (token->type == CPP_SEMICOLON)
27486 cp_lexer_consume_token (parser->lexer);
27487 /* Mark methods as optional or required, when building protocols. */
27488 else if (token->keyword == RID_AT_OPTIONAL)
27490 cp_lexer_consume_token (parser->lexer);
27491 objc_set_method_opt (true);
27493 else if (token->keyword == RID_AT_REQUIRED)
27495 cp_lexer_consume_token (parser->lexer);
27496 objc_set_method_opt (false);
27498 else if (token->keyword == RID_NAMESPACE)
27499 cp_parser_namespace_definition (parser);
27500 /* Other stray characters must generate errors. */
27501 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
27503 cp_lexer_consume_token (parser->lexer);
27504 error ("stray %qs between Objective-C++ methods",
27505 token->type == CPP_OPEN_BRACE ? "{" : "}");
27507 /* Finally, try to parse a block-declaration, or a function-definition. */
27508 else
27509 cp_parser_block_declaration (parser, /*statement_p=*/false);
27512 /* Parse a method signature. */
27514 static tree
27515 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
27517 tree rettype, kwdparms, optparms;
27518 bool ellipsis = false;
27519 bool is_class_method;
27521 is_class_method = cp_parser_objc_method_type (parser);
27522 rettype = cp_parser_objc_typename (parser);
27523 *attributes = NULL_TREE;
27524 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
27525 if (kwdparms == error_mark_node)
27526 return error_mark_node;
27527 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
27528 if (optparms == error_mark_node)
27529 return error_mark_node;
27531 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
27534 static bool
27535 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
27537 tree tattr;
27538 cp_lexer_save_tokens (parser->lexer);
27539 tattr = cp_parser_attributes_opt (parser);
27540 gcc_assert (tattr) ;
27542 /* If the attributes are followed by a method introducer, this is not allowed.
27543 Dump the attributes and flag the situation. */
27544 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
27545 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
27546 return true;
27548 /* Otherwise, the attributes introduce some interstitial code, possibly so
27549 rewind to allow that check. */
27550 cp_lexer_rollback_tokens (parser->lexer);
27551 return false;
27554 /* Parse an Objective-C method prototype list. */
27556 static void
27557 cp_parser_objc_method_prototype_list (cp_parser* parser)
27559 cp_token *token = cp_lexer_peek_token (parser->lexer);
27561 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27563 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27565 tree attributes, sig;
27566 bool is_class_method;
27567 if (token->type == CPP_PLUS)
27568 is_class_method = true;
27569 else
27570 is_class_method = false;
27571 sig = cp_parser_objc_method_signature (parser, &attributes);
27572 if (sig == error_mark_node)
27574 cp_parser_skip_to_end_of_block_or_statement (parser);
27575 token = cp_lexer_peek_token (parser->lexer);
27576 continue;
27578 objc_add_method_declaration (is_class_method, sig, attributes);
27579 cp_parser_consume_semicolon_at_end_of_statement (parser);
27581 else if (token->keyword == RID_AT_PROPERTY)
27582 cp_parser_objc_at_property_declaration (parser);
27583 else if (token->keyword == RID_ATTRIBUTE
27584 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
27585 warning_at (cp_lexer_peek_token (parser->lexer)->location,
27586 OPT_Wattributes,
27587 "prefix attributes are ignored for methods");
27588 else
27589 /* Allow for interspersed non-ObjC++ code. */
27590 cp_parser_objc_interstitial_code (parser);
27592 token = cp_lexer_peek_token (parser->lexer);
27595 if (token->type != CPP_EOF)
27596 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27597 else
27598 cp_parser_error (parser, "expected %<@end%>");
27600 objc_finish_interface ();
27603 /* Parse an Objective-C method definition list. */
27605 static void
27606 cp_parser_objc_method_definition_list (cp_parser* parser)
27608 cp_token *token = cp_lexer_peek_token (parser->lexer);
27610 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27612 tree meth;
27614 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27616 cp_token *ptk;
27617 tree sig, attribute;
27618 bool is_class_method;
27619 if (token->type == CPP_PLUS)
27620 is_class_method = true;
27621 else
27622 is_class_method = false;
27623 push_deferring_access_checks (dk_deferred);
27624 sig = cp_parser_objc_method_signature (parser, &attribute);
27625 if (sig == error_mark_node)
27627 cp_parser_skip_to_end_of_block_or_statement (parser);
27628 token = cp_lexer_peek_token (parser->lexer);
27629 continue;
27631 objc_start_method_definition (is_class_method, sig, attribute,
27632 NULL_TREE);
27634 /* For historical reasons, we accept an optional semicolon. */
27635 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27636 cp_lexer_consume_token (parser->lexer);
27638 ptk = cp_lexer_peek_token (parser->lexer);
27639 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
27640 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
27642 perform_deferred_access_checks (tf_warning_or_error);
27643 stop_deferring_access_checks ();
27644 meth = cp_parser_function_definition_after_declarator (parser,
27645 false);
27646 pop_deferring_access_checks ();
27647 objc_finish_method_definition (meth);
27650 /* The following case will be removed once @synthesize is
27651 completely implemented. */
27652 else if (token->keyword == RID_AT_PROPERTY)
27653 cp_parser_objc_at_property_declaration (parser);
27654 else if (token->keyword == RID_AT_SYNTHESIZE)
27655 cp_parser_objc_at_synthesize_declaration (parser);
27656 else if (token->keyword == RID_AT_DYNAMIC)
27657 cp_parser_objc_at_dynamic_declaration (parser);
27658 else if (token->keyword == RID_ATTRIBUTE
27659 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
27660 warning_at (token->location, OPT_Wattributes,
27661 "prefix attributes are ignored for methods");
27662 else
27663 /* Allow for interspersed non-ObjC++ code. */
27664 cp_parser_objc_interstitial_code (parser);
27666 token = cp_lexer_peek_token (parser->lexer);
27669 if (token->type != CPP_EOF)
27670 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27671 else
27672 cp_parser_error (parser, "expected %<@end%>");
27674 objc_finish_implementation ();
27677 /* Parse Objective-C ivars. */
27679 static void
27680 cp_parser_objc_class_ivars (cp_parser* parser)
27682 cp_token *token = cp_lexer_peek_token (parser->lexer);
27684 if (token->type != CPP_OPEN_BRACE)
27685 return; /* No ivars specified. */
27687 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
27688 token = cp_lexer_peek_token (parser->lexer);
27690 while (token->type != CPP_CLOSE_BRACE
27691 && token->keyword != RID_AT_END && token->type != CPP_EOF)
27693 cp_decl_specifier_seq declspecs;
27694 int decl_class_or_enum_p;
27695 tree prefix_attributes;
27697 cp_parser_objc_visibility_spec (parser);
27699 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27700 break;
27702 cp_parser_decl_specifier_seq (parser,
27703 CP_PARSER_FLAGS_OPTIONAL,
27704 &declspecs,
27705 &decl_class_or_enum_p);
27707 /* auto, register, static, extern, mutable. */
27708 if (declspecs.storage_class != sc_none)
27710 cp_parser_error (parser, "invalid type for instance variable");
27711 declspecs.storage_class = sc_none;
27714 /* thread_local. */
27715 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27717 cp_parser_error (parser, "invalid type for instance variable");
27718 declspecs.locations[ds_thread] = 0;
27721 /* typedef. */
27722 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27724 cp_parser_error (parser, "invalid type for instance variable");
27725 declspecs.locations[ds_typedef] = 0;
27728 prefix_attributes = declspecs.attributes;
27729 declspecs.attributes = NULL_TREE;
27731 /* Keep going until we hit the `;' at the end of the
27732 declaration. */
27733 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27735 tree width = NULL_TREE, attributes, first_attribute, decl;
27736 cp_declarator *declarator = NULL;
27737 int ctor_dtor_or_conv_p;
27739 /* Check for a (possibly unnamed) bitfield declaration. */
27740 token = cp_lexer_peek_token (parser->lexer);
27741 if (token->type == CPP_COLON)
27742 goto eat_colon;
27744 if (token->type == CPP_NAME
27745 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
27746 == CPP_COLON))
27748 /* Get the name of the bitfield. */
27749 declarator = make_id_declarator (NULL_TREE,
27750 cp_parser_identifier (parser),
27751 sfk_none);
27753 eat_colon:
27754 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
27755 /* Get the width of the bitfield. */
27756 width
27757 = cp_parser_constant_expression (parser);
27759 else
27761 /* Parse the declarator. */
27762 declarator
27763 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27764 &ctor_dtor_or_conv_p,
27765 /*parenthesized_p=*/NULL,
27766 /*member_p=*/false,
27767 /*friend_p=*/false);
27770 /* Look for attributes that apply to the ivar. */
27771 attributes = cp_parser_attributes_opt (parser);
27772 /* Remember which attributes are prefix attributes and
27773 which are not. */
27774 first_attribute = attributes;
27775 /* Combine the attributes. */
27776 attributes = chainon (prefix_attributes, attributes);
27778 if (width)
27779 /* Create the bitfield declaration. */
27780 decl = grokbitfield (declarator, &declspecs,
27781 width,
27782 attributes);
27783 else
27784 decl = grokfield (declarator, &declspecs,
27785 NULL_TREE, /*init_const_expr_p=*/false,
27786 NULL_TREE, attributes);
27788 /* Add the instance variable. */
27789 if (decl != error_mark_node && decl != NULL_TREE)
27790 objc_add_instance_variable (decl);
27792 /* Reset PREFIX_ATTRIBUTES. */
27793 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27794 attributes = TREE_CHAIN (attributes);
27795 if (attributes)
27796 TREE_CHAIN (attributes) = NULL_TREE;
27798 token = cp_lexer_peek_token (parser->lexer);
27800 if (token->type == CPP_COMMA)
27802 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27803 continue;
27805 break;
27808 cp_parser_consume_semicolon_at_end_of_statement (parser);
27809 token = cp_lexer_peek_token (parser->lexer);
27812 if (token->keyword == RID_AT_END)
27813 cp_parser_error (parser, "expected %<}%>");
27815 /* Do not consume the RID_AT_END, so it will be read again as terminating
27816 the @interface of @implementation. */
27817 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
27818 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
27820 /* For historical reasons, we accept an optional semicolon. */
27821 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27822 cp_lexer_consume_token (parser->lexer);
27825 /* Parse an Objective-C protocol declaration. */
27827 static void
27828 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
27830 tree proto, protorefs;
27831 cp_token *tok;
27833 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
27836 tok = cp_lexer_peek_token (parser->lexer);
27837 error_at (tok->location, "identifier expected after %<@protocol%>");
27838 cp_parser_consume_semicolon_at_end_of_statement (parser);
27839 return;
27842 /* See if we have a forward declaration or a definition. */
27843 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
27845 /* Try a forward declaration first. */
27846 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
27848 while (true)
27850 tree id;
27852 id = cp_parser_identifier (parser);
27853 if (id == error_mark_node)
27854 break;
27856 objc_declare_protocol (id, attributes);
27858 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27859 cp_lexer_consume_token (parser->lexer);
27860 else
27861 break;
27863 cp_parser_consume_semicolon_at_end_of_statement (parser);
27866 /* Ok, we got a full-fledged definition (or at least should). */
27867 else
27869 proto = cp_parser_identifier (parser);
27870 protorefs = cp_parser_objc_protocol_refs_opt (parser);
27871 objc_start_protocol (proto, protorefs, attributes);
27872 cp_parser_objc_method_prototype_list (parser);
27876 /* Parse an Objective-C superclass or category. */
27878 static void
27879 cp_parser_objc_superclass_or_category (cp_parser *parser,
27880 bool iface_p,
27881 tree *super,
27882 tree *categ, bool *is_class_extension)
27884 cp_token *next = cp_lexer_peek_token (parser->lexer);
27886 *super = *categ = NULL_TREE;
27887 *is_class_extension = false;
27888 if (next->type == CPP_COLON)
27890 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
27891 *super = cp_parser_identifier (parser);
27893 else if (next->type == CPP_OPEN_PAREN)
27895 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27897 /* If there is no category name, and this is an @interface, we
27898 have a class extension. */
27899 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27901 *categ = NULL_TREE;
27902 *is_class_extension = true;
27904 else
27905 *categ = cp_parser_identifier (parser);
27907 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27911 /* Parse an Objective-C class interface. */
27913 static void
27914 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
27916 tree name, super, categ, protos;
27917 bool is_class_extension;
27919 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
27920 name = cp_parser_identifier (parser);
27921 if (name == error_mark_node)
27923 /* It's hard to recover because even if valid @interface stuff
27924 is to follow, we can't compile it (or validate it) if we
27925 don't even know which class it refers to. Let's assume this
27926 was a stray '@interface' token in the stream and skip it.
27928 return;
27930 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
27931 &is_class_extension);
27932 protos = cp_parser_objc_protocol_refs_opt (parser);
27934 /* We have either a class or a category on our hands. */
27935 if (categ || is_class_extension)
27936 objc_start_category_interface (name, categ, protos, attributes);
27937 else
27939 objc_start_class_interface (name, super, protos, attributes);
27940 /* Handle instance variable declarations, if any. */
27941 cp_parser_objc_class_ivars (parser);
27942 objc_continue_interface ();
27945 cp_parser_objc_method_prototype_list (parser);
27948 /* Parse an Objective-C class implementation. */
27950 static void
27951 cp_parser_objc_class_implementation (cp_parser* parser)
27953 tree name, super, categ;
27954 bool is_class_extension;
27956 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
27957 name = cp_parser_identifier (parser);
27958 if (name == error_mark_node)
27960 /* It's hard to recover because even if valid @implementation
27961 stuff is to follow, we can't compile it (or validate it) if
27962 we don't even know which class it refers to. Let's assume
27963 this was a stray '@implementation' token in the stream and
27964 skip it.
27966 return;
27968 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
27969 &is_class_extension);
27971 /* We have either a class or a category on our hands. */
27972 if (categ)
27973 objc_start_category_implementation (name, categ);
27974 else
27976 objc_start_class_implementation (name, super);
27977 /* Handle instance variable declarations, if any. */
27978 cp_parser_objc_class_ivars (parser);
27979 objc_continue_implementation ();
27982 cp_parser_objc_method_definition_list (parser);
27985 /* Consume the @end token and finish off the implementation. */
27987 static void
27988 cp_parser_objc_end_implementation (cp_parser* parser)
27990 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27991 objc_finish_implementation ();
27994 /* Parse an Objective-C declaration. */
27996 static void
27997 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
27999 /* Try to figure out what kind of declaration is present. */
28000 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28002 if (attributes)
28003 switch (kwd->keyword)
28005 case RID_AT_ALIAS:
28006 case RID_AT_CLASS:
28007 case RID_AT_END:
28008 error_at (kwd->location, "attributes may not be specified before"
28009 " the %<@%D%> Objective-C++ keyword",
28010 kwd->u.value);
28011 attributes = NULL;
28012 break;
28013 case RID_AT_IMPLEMENTATION:
28014 warning_at (kwd->location, OPT_Wattributes,
28015 "prefix attributes are ignored before %<@%D%>",
28016 kwd->u.value);
28017 attributes = NULL;
28018 default:
28019 break;
28022 switch (kwd->keyword)
28024 case RID_AT_ALIAS:
28025 cp_parser_objc_alias_declaration (parser);
28026 break;
28027 case RID_AT_CLASS:
28028 cp_parser_objc_class_declaration (parser);
28029 break;
28030 case RID_AT_PROTOCOL:
28031 cp_parser_objc_protocol_declaration (parser, attributes);
28032 break;
28033 case RID_AT_INTERFACE:
28034 cp_parser_objc_class_interface (parser, attributes);
28035 break;
28036 case RID_AT_IMPLEMENTATION:
28037 cp_parser_objc_class_implementation (parser);
28038 break;
28039 case RID_AT_END:
28040 cp_parser_objc_end_implementation (parser);
28041 break;
28042 default:
28043 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28044 kwd->u.value);
28045 cp_parser_skip_to_end_of_block_or_statement (parser);
28049 /* Parse an Objective-C try-catch-finally statement.
28051 objc-try-catch-finally-stmt:
28052 @try compound-statement objc-catch-clause-seq [opt]
28053 objc-finally-clause [opt]
28055 objc-catch-clause-seq:
28056 objc-catch-clause objc-catch-clause-seq [opt]
28058 objc-catch-clause:
28059 @catch ( objc-exception-declaration ) compound-statement
28061 objc-finally-clause:
28062 @finally compound-statement
28064 objc-exception-declaration:
28065 parameter-declaration
28066 '...'
28068 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28070 Returns NULL_TREE.
28072 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28073 for C. Keep them in sync. */
28075 static tree
28076 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
28078 location_t location;
28079 tree stmt;
28081 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
28082 location = cp_lexer_peek_token (parser->lexer)->location;
28083 objc_maybe_warn_exceptions (location);
28084 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
28085 node, lest it get absorbed into the surrounding block. */
28086 stmt = push_stmt_list ();
28087 cp_parser_compound_statement (parser, NULL, false, false);
28088 objc_begin_try_stmt (location, pop_stmt_list (stmt));
28090 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
28092 cp_parameter_declarator *parm;
28093 tree parameter_declaration = error_mark_node;
28094 bool seen_open_paren = false;
28096 cp_lexer_consume_token (parser->lexer);
28097 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28098 seen_open_paren = true;
28099 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28101 /* We have "@catch (...)" (where the '...' are literally
28102 what is in the code). Skip the '...'.
28103 parameter_declaration is set to NULL_TREE, and
28104 objc_being_catch_clauses() knows that that means
28105 '...'. */
28106 cp_lexer_consume_token (parser->lexer);
28107 parameter_declaration = NULL_TREE;
28109 else
28111 /* We have "@catch (NSException *exception)" or something
28112 like that. Parse the parameter declaration. */
28113 parm = cp_parser_parameter_declaration (parser, false, NULL);
28114 if (parm == NULL)
28115 parameter_declaration = error_mark_node;
28116 else
28117 parameter_declaration = grokdeclarator (parm->declarator,
28118 &parm->decl_specifiers,
28119 PARM, /*initialized=*/0,
28120 /*attrlist=*/NULL);
28122 if (seen_open_paren)
28123 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28124 else
28126 /* If there was no open parenthesis, we are recovering from
28127 an error, and we are trying to figure out what mistake
28128 the user has made. */
28130 /* If there is an immediate closing parenthesis, the user
28131 probably forgot the opening one (ie, they typed "@catch
28132 NSException *e)". Parse the closing parenthesis and keep
28133 going. */
28134 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28135 cp_lexer_consume_token (parser->lexer);
28137 /* If these is no immediate closing parenthesis, the user
28138 probably doesn't know that parenthesis are required at
28139 all (ie, they typed "@catch NSException *e"). So, just
28140 forget about the closing parenthesis and keep going. */
28142 objc_begin_catch_clause (parameter_declaration);
28143 cp_parser_compound_statement (parser, NULL, false, false);
28144 objc_finish_catch_clause ();
28146 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
28148 cp_lexer_consume_token (parser->lexer);
28149 location = cp_lexer_peek_token (parser->lexer)->location;
28150 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
28151 node, lest it get absorbed into the surrounding block. */
28152 stmt = push_stmt_list ();
28153 cp_parser_compound_statement (parser, NULL, false, false);
28154 objc_build_finally_clause (location, pop_stmt_list (stmt));
28157 return objc_finish_try_stmt ();
28160 /* Parse an Objective-C synchronized statement.
28162 objc-synchronized-stmt:
28163 @synchronized ( expression ) compound-statement
28165 Returns NULL_TREE. */
28167 static tree
28168 cp_parser_objc_synchronized_statement (cp_parser *parser)
28170 location_t location;
28171 tree lock, stmt;
28173 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
28175 location = cp_lexer_peek_token (parser->lexer)->location;
28176 objc_maybe_warn_exceptions (location);
28177 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28178 lock = cp_parser_expression (parser);
28179 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28181 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
28182 node, lest it get absorbed into the surrounding block. */
28183 stmt = push_stmt_list ();
28184 cp_parser_compound_statement (parser, NULL, false, false);
28186 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
28189 /* Parse an Objective-C throw statement.
28191 objc-throw-stmt:
28192 @throw assignment-expression [opt] ;
28194 Returns a constructed '@throw' statement. */
28196 static tree
28197 cp_parser_objc_throw_statement (cp_parser *parser)
28199 tree expr = NULL_TREE;
28200 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28202 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
28204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28205 expr = cp_parser_expression (parser);
28207 cp_parser_consume_semicolon_at_end_of_statement (parser);
28209 return objc_build_throw_stmt (loc, expr);
28212 /* Parse an Objective-C statement. */
28214 static tree
28215 cp_parser_objc_statement (cp_parser * parser)
28217 /* Try to figure out what kind of declaration is present. */
28218 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28220 switch (kwd->keyword)
28222 case RID_AT_TRY:
28223 return cp_parser_objc_try_catch_finally_statement (parser);
28224 case RID_AT_SYNCHRONIZED:
28225 return cp_parser_objc_synchronized_statement (parser);
28226 case RID_AT_THROW:
28227 return cp_parser_objc_throw_statement (parser);
28228 default:
28229 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28230 kwd->u.value);
28231 cp_parser_skip_to_end_of_block_or_statement (parser);
28234 return error_mark_node;
28237 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
28238 look ahead to see if an objc keyword follows the attributes. This
28239 is to detect the use of prefix attributes on ObjC @interface and
28240 @protocol. */
28242 static bool
28243 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
28245 cp_lexer_save_tokens (parser->lexer);
28246 *attrib = cp_parser_attributes_opt (parser);
28247 gcc_assert (*attrib);
28248 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
28250 cp_lexer_commit_tokens (parser->lexer);
28251 return true;
28253 cp_lexer_rollback_tokens (parser->lexer);
28254 return false;
28257 /* This routine is a minimal replacement for
28258 c_parser_struct_declaration () used when parsing the list of
28259 types/names or ObjC++ properties. For example, when parsing the
28260 code
28262 @property (readonly) int a, b, c;
28264 this function is responsible for parsing "int a, int b, int c" and
28265 returning the declarations as CHAIN of DECLs.
28267 TODO: Share this code with cp_parser_objc_class_ivars. It's very
28268 similar parsing. */
28269 static tree
28270 cp_parser_objc_struct_declaration (cp_parser *parser)
28272 tree decls = NULL_TREE;
28273 cp_decl_specifier_seq declspecs;
28274 int decl_class_or_enum_p;
28275 tree prefix_attributes;
28277 cp_parser_decl_specifier_seq (parser,
28278 CP_PARSER_FLAGS_NONE,
28279 &declspecs,
28280 &decl_class_or_enum_p);
28282 if (declspecs.type == error_mark_node)
28283 return error_mark_node;
28285 /* auto, register, static, extern, mutable. */
28286 if (declspecs.storage_class != sc_none)
28288 cp_parser_error (parser, "invalid type for property");
28289 declspecs.storage_class = sc_none;
28292 /* thread_local. */
28293 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28295 cp_parser_error (parser, "invalid type for property");
28296 declspecs.locations[ds_thread] = 0;
28299 /* typedef. */
28300 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28302 cp_parser_error (parser, "invalid type for property");
28303 declspecs.locations[ds_typedef] = 0;
28306 prefix_attributes = declspecs.attributes;
28307 declspecs.attributes = NULL_TREE;
28309 /* Keep going until we hit the `;' at the end of the declaration. */
28310 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28312 tree attributes, first_attribute, decl;
28313 cp_declarator *declarator;
28314 cp_token *token;
28316 /* Parse the declarator. */
28317 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28318 NULL, NULL, false, false);
28320 /* Look for attributes that apply to the ivar. */
28321 attributes = cp_parser_attributes_opt (parser);
28322 /* Remember which attributes are prefix attributes and
28323 which are not. */
28324 first_attribute = attributes;
28325 /* Combine the attributes. */
28326 attributes = chainon (prefix_attributes, attributes);
28328 decl = grokfield (declarator, &declspecs,
28329 NULL_TREE, /*init_const_expr_p=*/false,
28330 NULL_TREE, attributes);
28332 if (decl == error_mark_node || decl == NULL_TREE)
28333 return error_mark_node;
28335 /* Reset PREFIX_ATTRIBUTES. */
28336 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28337 attributes = TREE_CHAIN (attributes);
28338 if (attributes)
28339 TREE_CHAIN (attributes) = NULL_TREE;
28341 DECL_CHAIN (decl) = decls;
28342 decls = decl;
28344 token = cp_lexer_peek_token (parser->lexer);
28345 if (token->type == CPP_COMMA)
28347 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28348 continue;
28350 else
28351 break;
28353 return decls;
28356 /* Parse an Objective-C @property declaration. The syntax is:
28358 objc-property-declaration:
28359 '@property' objc-property-attributes[opt] struct-declaration ;
28361 objc-property-attributes:
28362 '(' objc-property-attribute-list ')'
28364 objc-property-attribute-list:
28365 objc-property-attribute
28366 objc-property-attribute-list, objc-property-attribute
28368 objc-property-attribute
28369 'getter' = identifier
28370 'setter' = identifier
28371 'readonly'
28372 'readwrite'
28373 'assign'
28374 'retain'
28375 'copy'
28376 'nonatomic'
28378 For example:
28379 @property NSString *name;
28380 @property (readonly) id object;
28381 @property (retain, nonatomic, getter=getTheName) id name;
28382 @property int a, b, c;
28384 PS: This function is identical to
28385 c_parser_objc_at_property_declaration for C. Keep them in sync. */
28386 static void
28387 cp_parser_objc_at_property_declaration (cp_parser *parser)
28389 /* The following variables hold the attributes of the properties as
28390 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
28391 seen. When we see an attribute, we set them to 'true' (if they
28392 are boolean properties) or to the identifier (if they have an
28393 argument, ie, for getter and setter). Note that here we only
28394 parse the list of attributes, check the syntax and accumulate the
28395 attributes that we find. objc_add_property_declaration() will
28396 then process the information. */
28397 bool property_assign = false;
28398 bool property_copy = false;
28399 tree property_getter_ident = NULL_TREE;
28400 bool property_nonatomic = false;
28401 bool property_readonly = false;
28402 bool property_readwrite = false;
28403 bool property_retain = false;
28404 tree property_setter_ident = NULL_TREE;
28406 /* 'properties' is the list of properties that we read. Usually a
28407 single one, but maybe more (eg, in "@property int a, b, c;" there
28408 are three). */
28409 tree properties;
28410 location_t loc;
28412 loc = cp_lexer_peek_token (parser->lexer)->location;
28414 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
28416 /* Parse the optional attribute list... */
28417 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28419 /* Eat the '('. */
28420 cp_lexer_consume_token (parser->lexer);
28422 while (true)
28424 bool syntax_error = false;
28425 cp_token *token = cp_lexer_peek_token (parser->lexer);
28426 enum rid keyword;
28428 if (token->type != CPP_NAME)
28430 cp_parser_error (parser, "expected identifier");
28431 break;
28433 keyword = C_RID_CODE (token->u.value);
28434 cp_lexer_consume_token (parser->lexer);
28435 switch (keyword)
28437 case RID_ASSIGN: property_assign = true; break;
28438 case RID_COPY: property_copy = true; break;
28439 case RID_NONATOMIC: property_nonatomic = true; break;
28440 case RID_READONLY: property_readonly = true; break;
28441 case RID_READWRITE: property_readwrite = true; break;
28442 case RID_RETAIN: property_retain = true; break;
28444 case RID_GETTER:
28445 case RID_SETTER:
28446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28448 if (keyword == RID_GETTER)
28449 cp_parser_error (parser,
28450 "missing %<=%> (after %<getter%> attribute)");
28451 else
28452 cp_parser_error (parser,
28453 "missing %<=%> (after %<setter%> attribute)");
28454 syntax_error = true;
28455 break;
28457 cp_lexer_consume_token (parser->lexer); /* eat the = */
28458 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
28460 cp_parser_error (parser, "expected identifier");
28461 syntax_error = true;
28462 break;
28464 if (keyword == RID_SETTER)
28466 if (property_setter_ident != NULL_TREE)
28468 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
28469 cp_lexer_consume_token (parser->lexer);
28471 else
28472 property_setter_ident = cp_parser_objc_selector (parser);
28473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28474 cp_parser_error (parser, "setter name must terminate with %<:%>");
28475 else
28476 cp_lexer_consume_token (parser->lexer);
28478 else
28480 if (property_getter_ident != NULL_TREE)
28482 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
28483 cp_lexer_consume_token (parser->lexer);
28485 else
28486 property_getter_ident = cp_parser_objc_selector (parser);
28488 break;
28489 default:
28490 cp_parser_error (parser, "unknown property attribute");
28491 syntax_error = true;
28492 break;
28495 if (syntax_error)
28496 break;
28498 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28499 cp_lexer_consume_token (parser->lexer);
28500 else
28501 break;
28504 /* FIXME: "@property (setter, assign);" will generate a spurious
28505 "error: expected ‘)’ before ‘,’ token". This is because
28506 cp_parser_require, unlike the C counterpart, will produce an
28507 error even if we are in error recovery. */
28508 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28510 cp_parser_skip_to_closing_parenthesis (parser,
28511 /*recovering=*/true,
28512 /*or_comma=*/false,
28513 /*consume_paren=*/true);
28517 /* ... and the property declaration(s). */
28518 properties = cp_parser_objc_struct_declaration (parser);
28520 if (properties == error_mark_node)
28522 cp_parser_skip_to_end_of_statement (parser);
28523 /* If the next token is now a `;', consume it. */
28524 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28525 cp_lexer_consume_token (parser->lexer);
28526 return;
28529 if (properties == NULL_TREE)
28530 cp_parser_error (parser, "expected identifier");
28531 else
28533 /* Comma-separated properties are chained together in
28534 reverse order; add them one by one. */
28535 properties = nreverse (properties);
28537 for (; properties; properties = TREE_CHAIN (properties))
28538 objc_add_property_declaration (loc, copy_node (properties),
28539 property_readonly, property_readwrite,
28540 property_assign, property_retain,
28541 property_copy, property_nonatomic,
28542 property_getter_ident, property_setter_ident);
28545 cp_parser_consume_semicolon_at_end_of_statement (parser);
28548 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
28550 objc-synthesize-declaration:
28551 @synthesize objc-synthesize-identifier-list ;
28553 objc-synthesize-identifier-list:
28554 objc-synthesize-identifier
28555 objc-synthesize-identifier-list, objc-synthesize-identifier
28557 objc-synthesize-identifier
28558 identifier
28559 identifier = identifier
28561 For example:
28562 @synthesize MyProperty;
28563 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
28565 PS: This function is identical to c_parser_objc_at_synthesize_declaration
28566 for C. Keep them in sync.
28568 static void
28569 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
28571 tree list = NULL_TREE;
28572 location_t loc;
28573 loc = cp_lexer_peek_token (parser->lexer)->location;
28575 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
28576 while (true)
28578 tree property, ivar;
28579 property = cp_parser_identifier (parser);
28580 if (property == error_mark_node)
28582 cp_parser_consume_semicolon_at_end_of_statement (parser);
28583 return;
28585 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28587 cp_lexer_consume_token (parser->lexer);
28588 ivar = cp_parser_identifier (parser);
28589 if (ivar == error_mark_node)
28591 cp_parser_consume_semicolon_at_end_of_statement (parser);
28592 return;
28595 else
28596 ivar = NULL_TREE;
28597 list = chainon (list, build_tree_list (ivar, property));
28598 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28599 cp_lexer_consume_token (parser->lexer);
28600 else
28601 break;
28603 cp_parser_consume_semicolon_at_end_of_statement (parser);
28604 objc_add_synthesize_declaration (loc, list);
28607 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
28609 objc-dynamic-declaration:
28610 @dynamic identifier-list ;
28612 For example:
28613 @dynamic MyProperty;
28614 @dynamic MyProperty, AnotherProperty;
28616 PS: This function is identical to c_parser_objc_at_dynamic_declaration
28617 for C. Keep them in sync.
28619 static void
28620 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
28622 tree list = NULL_TREE;
28623 location_t loc;
28624 loc = cp_lexer_peek_token (parser->lexer)->location;
28626 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
28627 while (true)
28629 tree property;
28630 property = cp_parser_identifier (parser);
28631 if (property == error_mark_node)
28633 cp_parser_consume_semicolon_at_end_of_statement (parser);
28634 return;
28636 list = chainon (list, build_tree_list (NULL, property));
28637 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28638 cp_lexer_consume_token (parser->lexer);
28639 else
28640 break;
28642 cp_parser_consume_semicolon_at_end_of_statement (parser);
28643 objc_add_dynamic_declaration (loc, list);
28647 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
28649 /* Returns name of the next clause.
28650 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
28651 the token is not consumed. Otherwise appropriate pragma_omp_clause is
28652 returned and the token is consumed. */
28654 static pragma_omp_clause
28655 cp_parser_omp_clause_name (cp_parser *parser)
28657 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
28659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
28660 result = PRAGMA_OMP_CLAUSE_IF;
28661 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
28662 result = PRAGMA_OMP_CLAUSE_DEFAULT;
28663 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
28664 result = PRAGMA_OACC_CLAUSE_DELETE;
28665 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
28666 result = PRAGMA_OMP_CLAUSE_PRIVATE;
28667 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28668 result = PRAGMA_OMP_CLAUSE_FOR;
28669 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28671 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28672 const char *p = IDENTIFIER_POINTER (id);
28674 switch (p[0])
28676 case 'a':
28677 if (!strcmp ("aligned", p))
28678 result = PRAGMA_OMP_CLAUSE_ALIGNED;
28679 else if (!strcmp ("async", p))
28680 result = PRAGMA_OACC_CLAUSE_ASYNC;
28681 break;
28682 case 'c':
28683 if (!strcmp ("collapse", p))
28684 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
28685 else if (!strcmp ("copy", p))
28686 result = PRAGMA_OACC_CLAUSE_COPY;
28687 else if (!strcmp ("copyin", p))
28688 result = PRAGMA_OMP_CLAUSE_COPYIN;
28689 else if (!strcmp ("copyout", p))
28690 result = PRAGMA_OACC_CLAUSE_COPYOUT;
28691 else if (!strcmp ("copyprivate", p))
28692 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
28693 else if (!strcmp ("create", p))
28694 result = PRAGMA_OACC_CLAUSE_CREATE;
28695 break;
28696 case 'd':
28697 if (!strcmp ("depend", p))
28698 result = PRAGMA_OMP_CLAUSE_DEPEND;
28699 else if (!strcmp ("device", p))
28700 result = PRAGMA_OMP_CLAUSE_DEVICE;
28701 else if (!strcmp ("deviceptr", p))
28702 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
28703 else if (!strcmp ("dist_schedule", p))
28704 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
28705 break;
28706 case 'f':
28707 if (!strcmp ("final", p))
28708 result = PRAGMA_OMP_CLAUSE_FINAL;
28709 else if (!strcmp ("firstprivate", p))
28710 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
28711 else if (!strcmp ("from", p))
28712 result = PRAGMA_OMP_CLAUSE_FROM;
28713 break;
28714 case 'h':
28715 if (!strcmp ("host", p))
28716 result = PRAGMA_OACC_CLAUSE_HOST;
28717 break;
28718 case 'i':
28719 if (!strcmp ("inbranch", p))
28720 result = PRAGMA_OMP_CLAUSE_INBRANCH;
28721 break;
28722 case 'l':
28723 if (!strcmp ("lastprivate", p))
28724 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
28725 else if (!strcmp ("linear", p))
28726 result = PRAGMA_OMP_CLAUSE_LINEAR;
28727 break;
28728 case 'm':
28729 if (!strcmp ("map", p))
28730 result = PRAGMA_OMP_CLAUSE_MAP;
28731 else if (!strcmp ("mergeable", p))
28732 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
28733 else if (flag_cilkplus && !strcmp ("mask", p))
28734 result = PRAGMA_CILK_CLAUSE_MASK;
28735 break;
28736 case 'n':
28737 if (!strcmp ("notinbranch", p))
28738 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
28739 else if (!strcmp ("nowait", p))
28740 result = PRAGMA_OMP_CLAUSE_NOWAIT;
28741 else if (flag_cilkplus && !strcmp ("nomask", p))
28742 result = PRAGMA_CILK_CLAUSE_NOMASK;
28743 else if (!strcmp ("num_gangs", p))
28744 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
28745 else if (!strcmp ("num_teams", p))
28746 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
28747 else if (!strcmp ("num_threads", p))
28748 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
28749 else if (!strcmp ("num_workers", p))
28750 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
28751 break;
28752 case 'o':
28753 if (!strcmp ("ordered", p))
28754 result = PRAGMA_OMP_CLAUSE_ORDERED;
28755 break;
28756 case 'p':
28757 if (!strcmp ("parallel", p))
28758 result = PRAGMA_OMP_CLAUSE_PARALLEL;
28759 else if (!strcmp ("present", p))
28760 result = PRAGMA_OACC_CLAUSE_PRESENT;
28761 else if (!strcmp ("present_or_copy", p)
28762 || !strcmp ("pcopy", p))
28763 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
28764 else if (!strcmp ("present_or_copyin", p)
28765 || !strcmp ("pcopyin", p))
28766 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
28767 else if (!strcmp ("present_or_copyout", p)
28768 || !strcmp ("pcopyout", p))
28769 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
28770 else if (!strcmp ("present_or_create", p)
28771 || !strcmp ("pcreate", p))
28772 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
28773 else if (!strcmp ("proc_bind", p))
28774 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
28775 break;
28776 case 'r':
28777 if (!strcmp ("reduction", p))
28778 result = PRAGMA_OMP_CLAUSE_REDUCTION;
28779 break;
28780 case 's':
28781 if (!strcmp ("safelen", p))
28782 result = PRAGMA_OMP_CLAUSE_SAFELEN;
28783 else if (!strcmp ("schedule", p))
28784 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
28785 else if (!strcmp ("sections", p))
28786 result = PRAGMA_OMP_CLAUSE_SECTIONS;
28787 else if (!strcmp ("self", p))
28788 result = PRAGMA_OACC_CLAUSE_SELF;
28789 else if (!strcmp ("shared", p))
28790 result = PRAGMA_OMP_CLAUSE_SHARED;
28791 else if (!strcmp ("simdlen", p))
28792 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
28793 break;
28794 case 't':
28795 if (!strcmp ("taskgroup", p))
28796 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
28797 else if (!strcmp ("thread_limit", p))
28798 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
28799 else if (!strcmp ("to", p))
28800 result = PRAGMA_OMP_CLAUSE_TO;
28801 break;
28802 case 'u':
28803 if (!strcmp ("uniform", p))
28804 result = PRAGMA_OMP_CLAUSE_UNIFORM;
28805 else if (!strcmp ("untied", p))
28806 result = PRAGMA_OMP_CLAUSE_UNTIED;
28807 break;
28808 case 'v':
28809 if (!strcmp ("vector_length", p))
28810 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
28811 else if (flag_cilkplus && !strcmp ("vectorlength", p))
28812 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
28813 break;
28814 case 'w':
28815 if (!strcmp ("wait", p))
28816 result = PRAGMA_OACC_CLAUSE_WAIT;
28817 break;
28821 if (result != PRAGMA_OMP_CLAUSE_NONE)
28822 cp_lexer_consume_token (parser->lexer);
28824 return result;
28827 /* Validate that a clause of the given type does not already exist. */
28829 static void
28830 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
28831 const char *name, location_t location)
28833 tree c;
28835 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
28836 if (OMP_CLAUSE_CODE (c) == code)
28838 error_at (location, "too many %qs clauses", name);
28839 break;
28843 /* OpenMP 2.5:
28844 variable-list:
28845 identifier
28846 variable-list , identifier
28848 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
28849 colon). An opening parenthesis will have been consumed by the caller.
28851 If KIND is nonzero, create the appropriate node and install the decl
28852 in OMP_CLAUSE_DECL and add the node to the head of the list.
28854 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
28855 return the list created.
28857 COLON can be NULL if only closing parenthesis should end the list,
28858 or pointer to bool which will receive false if the list is terminated
28859 by closing parenthesis or true if the list is terminated by colon. */
28861 static tree
28862 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
28863 tree list, bool *colon)
28865 cp_token *token;
28866 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28867 if (colon)
28869 parser->colon_corrects_to_scope_p = false;
28870 *colon = false;
28872 while (1)
28874 tree name, decl;
28876 token = cp_lexer_peek_token (parser->lexer);
28877 name = cp_parser_id_expression (parser, /*template_p=*/false,
28878 /*check_dependency_p=*/true,
28879 /*template_p=*/NULL,
28880 /*declarator_p=*/false,
28881 /*optional_p=*/false);
28882 if (name == error_mark_node)
28883 goto skip_comma;
28885 decl = cp_parser_lookup_name_simple (parser, name, token->location);
28886 if (decl == error_mark_node)
28887 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
28888 token->location);
28889 else if (kind != 0)
28891 switch (kind)
28893 case OMP_CLAUSE__CACHE_:
28894 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
28896 error_at (token->location, "expected %<[%>");
28897 decl = error_mark_node;
28898 break;
28900 /* FALL THROUGH. */
28901 case OMP_CLAUSE_MAP:
28902 case OMP_CLAUSE_FROM:
28903 case OMP_CLAUSE_TO:
28904 case OMP_CLAUSE_DEPEND:
28905 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28907 tree low_bound = NULL_TREE, length = NULL_TREE;
28909 parser->colon_corrects_to_scope_p = false;
28910 cp_lexer_consume_token (parser->lexer);
28911 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28912 low_bound = cp_parser_expression (parser);
28913 if (!colon)
28914 parser->colon_corrects_to_scope_p
28915 = saved_colon_corrects_to_scope_p;
28916 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
28917 length = integer_one_node;
28918 else
28920 /* Look for `:'. */
28921 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28922 goto skip_comma;
28923 if (!cp_lexer_next_token_is (parser->lexer,
28924 CPP_CLOSE_SQUARE))
28925 length = cp_parser_expression (parser);
28927 /* Look for the closing `]'. */
28928 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
28929 RT_CLOSE_SQUARE))
28930 goto skip_comma;
28932 if (kind == OMP_CLAUSE__CACHE_)
28934 if (TREE_CODE (low_bound) != INTEGER_CST
28935 && !TREE_READONLY (low_bound))
28937 error_at (token->location,
28938 "%qD is not a constant", low_bound);
28939 decl = error_mark_node;
28942 if (TREE_CODE (length) != INTEGER_CST
28943 && !TREE_READONLY (length))
28945 error_at (token->location,
28946 "%qD is not a constant", length);
28947 decl = error_mark_node;
28951 decl = tree_cons (low_bound, length, decl);
28953 break;
28954 default:
28955 break;
28958 tree u = build_omp_clause (token->location, kind);
28959 OMP_CLAUSE_DECL (u) = decl;
28960 OMP_CLAUSE_CHAIN (u) = list;
28961 list = u;
28963 else
28964 list = tree_cons (decl, NULL_TREE, list);
28966 get_comma:
28967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28968 break;
28969 cp_lexer_consume_token (parser->lexer);
28972 if (colon)
28973 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28975 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28977 *colon = true;
28978 cp_parser_require (parser, CPP_COLON, RT_COLON);
28979 return list;
28982 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28984 int ending;
28986 /* Try to resync to an unnested comma. Copied from
28987 cp_parser_parenthesized_expression_list. */
28988 skip_comma:
28989 if (colon)
28990 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28991 ending = cp_parser_skip_to_closing_parenthesis (parser,
28992 /*recovering=*/true,
28993 /*or_comma=*/true,
28994 /*consume_paren=*/true);
28995 if (ending < 0)
28996 goto get_comma;
28999 return list;
29002 /* Similarly, but expect leading and trailing parenthesis. This is a very
29003 common case for omp clauses. */
29005 static tree
29006 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
29008 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29009 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
29010 return list;
29013 /* OpenACC 2.0:
29014 copy ( variable-list )
29015 copyin ( variable-list )
29016 copyout ( variable-list )
29017 create ( variable-list )
29018 delete ( variable-list )
29019 present ( variable-list )
29020 present_or_copy ( variable-list )
29021 pcopy ( variable-list )
29022 present_or_copyin ( variable-list )
29023 pcopyin ( variable-list )
29024 present_or_copyout ( variable-list )
29025 pcopyout ( variable-list )
29026 present_or_create ( variable-list )
29027 pcreate ( variable-list ) */
29029 static tree
29030 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
29031 tree list)
29033 enum gomp_map_kind kind;
29034 switch (c_kind)
29036 case PRAGMA_OACC_CLAUSE_COPY:
29037 kind = GOMP_MAP_FORCE_TOFROM;
29038 break;
29039 case PRAGMA_OACC_CLAUSE_COPYIN:
29040 kind = GOMP_MAP_FORCE_TO;
29041 break;
29042 case PRAGMA_OACC_CLAUSE_COPYOUT:
29043 kind = GOMP_MAP_FORCE_FROM;
29044 break;
29045 case PRAGMA_OACC_CLAUSE_CREATE:
29046 kind = GOMP_MAP_FORCE_ALLOC;
29047 break;
29048 case PRAGMA_OACC_CLAUSE_DELETE:
29049 kind = GOMP_MAP_FORCE_DEALLOC;
29050 break;
29051 case PRAGMA_OACC_CLAUSE_DEVICE:
29052 kind = GOMP_MAP_FORCE_TO;
29053 break;
29054 case PRAGMA_OACC_CLAUSE_HOST:
29055 case PRAGMA_OACC_CLAUSE_SELF:
29056 kind = GOMP_MAP_FORCE_FROM;
29057 break;
29058 case PRAGMA_OACC_CLAUSE_PRESENT:
29059 kind = GOMP_MAP_FORCE_PRESENT;
29060 break;
29061 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29062 kind = GOMP_MAP_TOFROM;
29063 break;
29064 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29065 kind = GOMP_MAP_TO;
29066 break;
29067 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29068 kind = GOMP_MAP_FROM;
29069 break;
29070 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29071 kind = GOMP_MAP_ALLOC;
29072 break;
29073 default:
29074 gcc_unreachable ();
29076 tree nl, c;
29077 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
29079 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
29080 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29082 return nl;
29085 /* OpenACC 2.0:
29086 deviceptr ( variable-list ) */
29088 static tree
29089 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
29091 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29092 tree vars, t;
29094 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
29095 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
29096 variable-list must only allow for pointer variables. */
29097 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29098 for (t = vars; t; t = TREE_CHAIN (t))
29100 tree v = TREE_PURPOSE (t);
29102 /* FIXME diagnostics: Ideally we should keep individual
29103 locations for all the variables in the var list to make the
29104 following errors more precise. Perhaps
29105 c_parser_omp_var_list_parens should construct a list of
29106 locations to go along with the var list. */
29108 if (TREE_CODE (v) != VAR_DECL)
29109 error_at (loc, "%qD is not a variable", v);
29110 else if (TREE_TYPE (v) == error_mark_node)
29112 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
29113 error_at (loc, "%qD is not a pointer variable", v);
29115 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
29116 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
29117 OMP_CLAUSE_DECL (u) = v;
29118 OMP_CLAUSE_CHAIN (u) = list;
29119 list = u;
29122 return list;
29125 /* OpenACC:
29126 vector_length ( expression ) */
29128 static tree
29129 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
29131 tree t, c;
29132 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29133 bool error = false;
29135 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29136 return list;
29138 t = cp_parser_condition (parser);
29139 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
29141 error_at (location, "expected positive integer expression");
29142 error = true;
29145 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29147 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29148 /*or_comma=*/false,
29149 /*consume_paren=*/true);
29150 return list;
29153 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
29154 location);
29156 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
29157 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
29158 OMP_CLAUSE_CHAIN (c) = list;
29159 list = c;
29161 return list;
29164 /* OpenACC 2.0
29165 Parse wait clause or directive parameters. */
29167 static tree
29168 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
29170 vec<tree, va_gc> *args;
29171 tree t, args_tree;
29173 args = cp_parser_parenthesized_expression_list (parser, non_attr,
29174 /*cast_p=*/false,
29175 /*allow_expansion_p=*/true,
29176 /*non_constant_p=*/NULL);
29178 if (args == NULL || args->length () == 0)
29180 cp_parser_error (parser, "expected integer expression before ')'");
29181 if (args != NULL)
29182 release_tree_vector (args);
29183 return list;
29186 args_tree = build_tree_list_vec (args);
29188 release_tree_vector (args);
29190 for (t = args_tree; t; t = TREE_CHAIN (t))
29192 tree targ = TREE_VALUE (t);
29194 if (targ != error_mark_node)
29196 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
29197 error ("%<wait%> expression must be integral");
29198 else
29200 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
29202 mark_rvalue_use (targ);
29203 OMP_CLAUSE_DECL (c) = targ;
29204 OMP_CLAUSE_CHAIN (c) = list;
29205 list = c;
29210 return list;
29213 /* OpenACC:
29214 wait ( int-expr-list ) */
29216 static tree
29217 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
29219 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29221 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
29222 return list;
29224 list = cp_parser_oacc_wait_list (parser, location, list);
29226 return list;
29229 /* OpenMP 3.0:
29230 collapse ( constant-expression ) */
29232 static tree
29233 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
29235 tree c, num;
29236 location_t loc;
29237 HOST_WIDE_INT n;
29239 loc = cp_lexer_peek_token (parser->lexer)->location;
29240 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29241 return list;
29243 num = cp_parser_constant_expression (parser);
29245 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29247 /*or_comma=*/false,
29248 /*consume_paren=*/true);
29250 if (num == error_mark_node)
29251 return list;
29252 num = fold_non_dependent_expr (num);
29253 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
29254 || !tree_fits_shwi_p (num)
29255 || (n = tree_to_shwi (num)) <= 0
29256 || (int) n != n)
29258 error_at (loc, "collapse argument needs positive constant integer expression");
29259 return list;
29262 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
29263 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
29264 OMP_CLAUSE_CHAIN (c) = list;
29265 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
29267 return c;
29270 /* OpenMP 2.5:
29271 default ( shared | none ) */
29273 static tree
29274 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
29276 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
29277 tree c;
29279 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29280 return list;
29281 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29283 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29284 const char *p = IDENTIFIER_POINTER (id);
29286 switch (p[0])
29288 case 'n':
29289 if (strcmp ("none", p) != 0)
29290 goto invalid_kind;
29291 kind = OMP_CLAUSE_DEFAULT_NONE;
29292 break;
29294 case 's':
29295 if (strcmp ("shared", p) != 0)
29296 goto invalid_kind;
29297 kind = OMP_CLAUSE_DEFAULT_SHARED;
29298 break;
29300 default:
29301 goto invalid_kind;
29304 cp_lexer_consume_token (parser->lexer);
29306 else
29308 invalid_kind:
29309 cp_parser_error (parser, "expected %<none%> or %<shared%>");
29312 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29314 /*or_comma=*/false,
29315 /*consume_paren=*/true);
29317 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
29318 return list;
29320 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
29321 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
29322 OMP_CLAUSE_CHAIN (c) = list;
29323 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
29325 return c;
29328 /* OpenMP 3.1:
29329 final ( expression ) */
29331 static tree
29332 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
29334 tree t, c;
29336 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29337 return list;
29339 t = cp_parser_condition (parser);
29341 if (t == error_mark_node
29342 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29344 /*or_comma=*/false,
29345 /*consume_paren=*/true);
29347 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
29349 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
29350 OMP_CLAUSE_FINAL_EXPR (c) = t;
29351 OMP_CLAUSE_CHAIN (c) = list;
29353 return c;
29356 /* OpenMP 2.5:
29357 if ( expression ) */
29359 static tree
29360 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
29362 tree t, c;
29364 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29365 return list;
29367 t = cp_parser_condition (parser);
29369 if (t == error_mark_node
29370 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29372 /*or_comma=*/false,
29373 /*consume_paren=*/true);
29375 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
29377 c = build_omp_clause (location, OMP_CLAUSE_IF);
29378 OMP_CLAUSE_IF_EXPR (c) = t;
29379 OMP_CLAUSE_CHAIN (c) = list;
29381 return c;
29384 /* OpenMP 3.1:
29385 mergeable */
29387 static tree
29388 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
29389 tree list, location_t location)
29391 tree c;
29393 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
29394 location);
29396 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
29397 OMP_CLAUSE_CHAIN (c) = list;
29398 return c;
29401 /* OpenMP 2.5:
29402 nowait */
29404 static tree
29405 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
29406 tree list, location_t location)
29408 tree c;
29410 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
29412 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
29413 OMP_CLAUSE_CHAIN (c) = list;
29414 return c;
29417 /* OpenACC:
29418 num_gangs ( expression ) */
29420 static tree
29421 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
29423 tree t, c;
29424 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29426 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29427 return list;
29429 t = cp_parser_condition (parser);
29431 if (t == error_mark_node
29432 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29433 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29434 /*or_comma=*/false,
29435 /*consume_paren=*/true);
29437 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
29439 error_at (location, "expected positive integer expression");
29440 return list;
29443 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
29445 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
29446 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
29447 OMP_CLAUSE_CHAIN (c) = list;
29448 list = c;
29450 return list;
29453 /* OpenMP 2.5:
29454 num_threads ( expression ) */
29456 static tree
29457 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
29458 location_t location)
29460 tree t, c;
29462 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29463 return list;
29465 t = cp_parser_expression (parser);
29467 if (t == error_mark_node
29468 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29470 /*or_comma=*/false,
29471 /*consume_paren=*/true);
29473 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
29474 "num_threads", location);
29476 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
29477 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
29478 OMP_CLAUSE_CHAIN (c) = list;
29480 return c;
29483 /* OpenACC:
29484 num_workers ( expression ) */
29486 static tree
29487 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
29489 tree t, c;
29490 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29492 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29493 return list;
29495 t = cp_parser_condition (parser);
29497 if (t == error_mark_node
29498 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29499 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29500 /*or_comma=*/false,
29501 /*consume_paren=*/true);
29503 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
29505 error_at (location, "expected positive integer expression");
29506 return list;
29509 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
29510 location);
29512 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
29513 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
29514 OMP_CLAUSE_CHAIN (c) = list;
29515 list = c;
29517 return list;
29520 /* OpenMP 2.5:
29521 ordered */
29523 static tree
29524 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
29525 tree list, location_t location)
29527 tree c;
29529 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
29530 "ordered", location);
29532 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
29533 OMP_CLAUSE_CHAIN (c) = list;
29534 return c;
29537 /* OpenMP 2.5:
29538 reduction ( reduction-operator : variable-list )
29540 reduction-operator:
29541 One of: + * - & ^ | && ||
29543 OpenMP 3.1:
29545 reduction-operator:
29546 One of: + * - & ^ | && || min max
29548 OpenMP 4.0:
29550 reduction-operator:
29551 One of: + * - & ^ | && ||
29552 id-expression */
29554 static tree
29555 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
29557 enum tree_code code = ERROR_MARK;
29558 tree nlist, c, id = NULL_TREE;
29560 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29561 return list;
29563 switch (cp_lexer_peek_token (parser->lexer)->type)
29565 case CPP_PLUS: code = PLUS_EXPR; break;
29566 case CPP_MULT: code = MULT_EXPR; break;
29567 case CPP_MINUS: code = MINUS_EXPR; break;
29568 case CPP_AND: code = BIT_AND_EXPR; break;
29569 case CPP_XOR: code = BIT_XOR_EXPR; break;
29570 case CPP_OR: code = BIT_IOR_EXPR; break;
29571 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
29572 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
29573 default: break;
29576 if (code != ERROR_MARK)
29577 cp_lexer_consume_token (parser->lexer);
29578 else
29580 bool saved_colon_corrects_to_scope_p;
29581 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29582 parser->colon_corrects_to_scope_p = false;
29583 id = cp_parser_id_expression (parser, /*template_p=*/false,
29584 /*check_dependency_p=*/true,
29585 /*template_p=*/NULL,
29586 /*declarator_p=*/false,
29587 /*optional_p=*/false);
29588 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29589 if (identifier_p (id))
29591 const char *p = IDENTIFIER_POINTER (id);
29593 if (strcmp (p, "min") == 0)
29594 code = MIN_EXPR;
29595 else if (strcmp (p, "max") == 0)
29596 code = MAX_EXPR;
29597 else if (id == ansi_opname (PLUS_EXPR))
29598 code = PLUS_EXPR;
29599 else if (id == ansi_opname (MULT_EXPR))
29600 code = MULT_EXPR;
29601 else if (id == ansi_opname (MINUS_EXPR))
29602 code = MINUS_EXPR;
29603 else if (id == ansi_opname (BIT_AND_EXPR))
29604 code = BIT_AND_EXPR;
29605 else if (id == ansi_opname (BIT_IOR_EXPR))
29606 code = BIT_IOR_EXPR;
29607 else if (id == ansi_opname (BIT_XOR_EXPR))
29608 code = BIT_XOR_EXPR;
29609 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
29610 code = TRUTH_ANDIF_EXPR;
29611 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
29612 code = TRUTH_ORIF_EXPR;
29613 id = omp_reduction_id (code, id, NULL_TREE);
29614 tree scope = parser->scope;
29615 if (scope)
29616 id = build_qualified_name (NULL_TREE, scope, id, false);
29617 parser->scope = NULL_TREE;
29618 parser->qualifying_scope = NULL_TREE;
29619 parser->object_scope = NULL_TREE;
29621 else
29623 error ("invalid reduction-identifier");
29624 resync_fail:
29625 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29626 /*or_comma=*/false,
29627 /*consume_paren=*/true);
29628 return list;
29632 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29633 goto resync_fail;
29635 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
29636 NULL);
29637 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29639 OMP_CLAUSE_REDUCTION_CODE (c) = code;
29640 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
29643 return nlist;
29646 /* OpenMP 2.5:
29647 schedule ( schedule-kind )
29648 schedule ( schedule-kind , expression )
29650 schedule-kind:
29651 static | dynamic | guided | runtime | auto */
29653 static tree
29654 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
29656 tree c, t;
29658 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29659 return list;
29661 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
29663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29665 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29666 const char *p = IDENTIFIER_POINTER (id);
29668 switch (p[0])
29670 case 'd':
29671 if (strcmp ("dynamic", p) != 0)
29672 goto invalid_kind;
29673 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
29674 break;
29676 case 'g':
29677 if (strcmp ("guided", p) != 0)
29678 goto invalid_kind;
29679 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
29680 break;
29682 case 'r':
29683 if (strcmp ("runtime", p) != 0)
29684 goto invalid_kind;
29685 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
29686 break;
29688 default:
29689 goto invalid_kind;
29692 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29693 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
29694 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29695 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
29696 else
29697 goto invalid_kind;
29698 cp_lexer_consume_token (parser->lexer);
29700 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29702 cp_token *token;
29703 cp_lexer_consume_token (parser->lexer);
29705 token = cp_lexer_peek_token (parser->lexer);
29706 t = cp_parser_assignment_expression (parser);
29708 if (t == error_mark_node)
29709 goto resync_fail;
29710 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
29711 error_at (token->location, "schedule %<runtime%> does not take "
29712 "a %<chunk_size%> parameter");
29713 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
29714 error_at (token->location, "schedule %<auto%> does not take "
29715 "a %<chunk_size%> parameter");
29716 else
29717 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
29719 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29720 goto resync_fail;
29722 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29723 goto resync_fail;
29725 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
29726 OMP_CLAUSE_CHAIN (c) = list;
29727 return c;
29729 invalid_kind:
29730 cp_parser_error (parser, "invalid schedule kind");
29731 resync_fail:
29732 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29733 /*or_comma=*/false,
29734 /*consume_paren=*/true);
29735 return list;
29738 /* OpenMP 3.0:
29739 untied */
29741 static tree
29742 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
29743 tree list, location_t location)
29745 tree c;
29747 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
29749 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
29750 OMP_CLAUSE_CHAIN (c) = list;
29751 return c;
29754 /* OpenMP 4.0:
29755 inbranch
29756 notinbranch */
29758 static tree
29759 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
29760 tree list, location_t location)
29762 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29763 tree c = build_omp_clause (location, code);
29764 OMP_CLAUSE_CHAIN (c) = list;
29765 return c;
29768 /* OpenMP 4.0:
29769 parallel
29771 sections
29772 taskgroup */
29774 static tree
29775 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
29776 enum omp_clause_code code,
29777 tree list, location_t location)
29779 tree c = build_omp_clause (location, code);
29780 OMP_CLAUSE_CHAIN (c) = list;
29781 return c;
29784 /* OpenMP 4.0:
29785 num_teams ( expression ) */
29787 static tree
29788 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
29789 location_t location)
29791 tree t, c;
29793 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29794 return list;
29796 t = cp_parser_expression (parser);
29798 if (t == error_mark_node
29799 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29800 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29801 /*or_comma=*/false,
29802 /*consume_paren=*/true);
29804 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
29805 "num_teams", location);
29807 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
29808 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
29809 OMP_CLAUSE_CHAIN (c) = list;
29811 return c;
29814 /* OpenMP 4.0:
29815 thread_limit ( expression ) */
29817 static tree
29818 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
29819 location_t location)
29821 tree t, c;
29823 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29824 return list;
29826 t = cp_parser_expression (parser);
29828 if (t == error_mark_node
29829 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29830 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29831 /*or_comma=*/false,
29832 /*consume_paren=*/true);
29834 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
29835 "thread_limit", location);
29837 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
29838 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
29839 OMP_CLAUSE_CHAIN (c) = list;
29841 return c;
29844 /* OpenMP 4.0:
29845 aligned ( variable-list )
29846 aligned ( variable-list : constant-expression ) */
29848 static tree
29849 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
29851 tree nlist, c, alignment = NULL_TREE;
29852 bool colon;
29854 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29855 return list;
29857 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
29858 &colon);
29860 if (colon)
29862 alignment = cp_parser_constant_expression (parser);
29864 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29865 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29866 /*or_comma=*/false,
29867 /*consume_paren=*/true);
29869 if (alignment == error_mark_node)
29870 alignment = NULL_TREE;
29873 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29874 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
29876 return nlist;
29879 /* OpenMP 4.0:
29880 linear ( variable-list )
29881 linear ( variable-list : expression ) */
29883 static tree
29884 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
29885 bool is_cilk_simd_fn)
29887 tree nlist, c, step = integer_one_node;
29888 bool colon;
29890 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29891 return list;
29893 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
29894 &colon);
29896 if (colon)
29898 step = cp_parser_expression (parser);
29900 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
29902 sorry ("using parameters for %<linear%> step is not supported yet");
29903 step = integer_one_node;
29905 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29906 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29907 /*or_comma=*/false,
29908 /*consume_paren=*/true);
29910 if (step == error_mark_node)
29911 return list;
29914 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29915 OMP_CLAUSE_LINEAR_STEP (c) = step;
29917 return nlist;
29920 /* OpenMP 4.0:
29921 safelen ( constant-expression ) */
29923 static tree
29924 cp_parser_omp_clause_safelen (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_SAFELEN, "safelen", location);
29942 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
29943 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
29944 OMP_CLAUSE_CHAIN (c) = list;
29946 return c;
29949 /* OpenMP 4.0:
29950 simdlen ( constant-expression ) */
29952 static tree
29953 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
29954 location_t location)
29956 tree t, c;
29958 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29959 return list;
29961 t = cp_parser_constant_expression (parser);
29963 if (t == error_mark_node
29964 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29965 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29966 /*or_comma=*/false,
29967 /*consume_paren=*/true);
29969 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
29971 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
29972 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
29973 OMP_CLAUSE_CHAIN (c) = list;
29975 return c;
29978 /* OpenMP 4.0:
29979 depend ( depend-kind : variable-list )
29981 depend-kind:
29982 in | out | inout */
29984 static tree
29985 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
29987 tree nlist, c;
29988 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
29990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29991 return list;
29993 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29995 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29996 const char *p = IDENTIFIER_POINTER (id);
29998 if (strcmp ("in", p) == 0)
29999 kind = OMP_CLAUSE_DEPEND_IN;
30000 else if (strcmp ("inout", p) == 0)
30001 kind = OMP_CLAUSE_DEPEND_INOUT;
30002 else if (strcmp ("out", p) == 0)
30003 kind = OMP_CLAUSE_DEPEND_OUT;
30004 else
30005 goto invalid_kind;
30007 else
30008 goto invalid_kind;
30010 cp_lexer_consume_token (parser->lexer);
30011 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30012 goto resync_fail;
30014 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
30015 NULL);
30017 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30018 OMP_CLAUSE_DEPEND_KIND (c) = kind;
30020 return nlist;
30022 invalid_kind:
30023 cp_parser_error (parser, "invalid depend kind");
30024 resync_fail:
30025 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30026 /*or_comma=*/false,
30027 /*consume_paren=*/true);
30028 return list;
30031 /* OpenMP 4.0:
30032 map ( map-kind : variable-list )
30033 map ( variable-list )
30035 map-kind:
30036 alloc | to | from | tofrom */
30038 static tree
30039 cp_parser_omp_clause_map (cp_parser *parser, tree list)
30041 tree nlist, c;
30042 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
30044 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30045 return list;
30047 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
30048 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
30050 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30051 const char *p = IDENTIFIER_POINTER (id);
30053 if (strcmp ("alloc", p) == 0)
30054 kind = GOMP_MAP_ALLOC;
30055 else if (strcmp ("to", p) == 0)
30056 kind = GOMP_MAP_TO;
30057 else if (strcmp ("from", p) == 0)
30058 kind = GOMP_MAP_FROM;
30059 else if (strcmp ("tofrom", p) == 0)
30060 kind = GOMP_MAP_TOFROM;
30061 else
30063 cp_parser_error (parser, "invalid map kind");
30064 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30065 /*or_comma=*/false,
30066 /*consume_paren=*/true);
30067 return list;
30069 cp_lexer_consume_token (parser->lexer);
30070 cp_lexer_consume_token (parser->lexer);
30073 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
30074 NULL);
30076 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30077 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30079 return nlist;
30082 /* OpenMP 4.0:
30083 device ( expression ) */
30085 static tree
30086 cp_parser_omp_clause_device (cp_parser *parser, tree list,
30087 location_t location)
30089 tree t, c;
30091 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30092 return list;
30094 t = cp_parser_expression (parser);
30096 if (t == error_mark_node
30097 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30098 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30099 /*or_comma=*/false,
30100 /*consume_paren=*/true);
30102 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
30103 "device", location);
30105 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
30106 OMP_CLAUSE_DEVICE_ID (c) = t;
30107 OMP_CLAUSE_CHAIN (c) = list;
30109 return c;
30112 /* OpenMP 4.0:
30113 dist_schedule ( static )
30114 dist_schedule ( static , expression ) */
30116 static tree
30117 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
30118 location_t location)
30120 tree c, t;
30122 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30123 return list;
30125 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
30127 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
30128 goto invalid_kind;
30129 cp_lexer_consume_token (parser->lexer);
30131 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30133 cp_lexer_consume_token (parser->lexer);
30135 t = cp_parser_assignment_expression (parser);
30137 if (t == error_mark_node)
30138 goto resync_fail;
30139 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
30141 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30142 goto resync_fail;
30144 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30145 goto resync_fail;
30147 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
30148 location);
30149 OMP_CLAUSE_CHAIN (c) = list;
30150 return c;
30152 invalid_kind:
30153 cp_parser_error (parser, "invalid dist_schedule kind");
30154 resync_fail:
30155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30156 /*or_comma=*/false,
30157 /*consume_paren=*/true);
30158 return list;
30161 /* OpenMP 4.0:
30162 proc_bind ( proc-bind-kind )
30164 proc-bind-kind:
30165 master | close | spread */
30167 static tree
30168 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
30169 location_t location)
30171 tree c;
30172 enum omp_clause_proc_bind_kind kind;
30174 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30175 return list;
30177 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30179 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30180 const char *p = IDENTIFIER_POINTER (id);
30182 if (strcmp ("master", p) == 0)
30183 kind = OMP_CLAUSE_PROC_BIND_MASTER;
30184 else if (strcmp ("close", p) == 0)
30185 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
30186 else if (strcmp ("spread", p) == 0)
30187 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
30188 else
30189 goto invalid_kind;
30191 else
30192 goto invalid_kind;
30194 cp_lexer_consume_token (parser->lexer);
30195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30196 goto resync_fail;
30198 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
30199 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
30200 location);
30201 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
30202 OMP_CLAUSE_CHAIN (c) = list;
30203 return c;
30205 invalid_kind:
30206 cp_parser_error (parser, "invalid depend kind");
30207 resync_fail:
30208 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30209 /*or_comma=*/false,
30210 /*consume_paren=*/true);
30211 return list;
30214 /* OpenACC:
30215 async [( int-expr )] */
30217 static tree
30218 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
30220 tree c, t;
30221 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30223 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
30225 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
30227 cp_lexer_consume_token (parser->lexer);
30229 t = cp_parser_expression (parser);
30230 if (t == error_mark_node
30231 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30232 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30233 /*or_comma=*/false,
30234 /*consume_paren=*/true);
30237 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
30239 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
30240 OMP_CLAUSE_ASYNC_EXPR (c) = t;
30241 OMP_CLAUSE_CHAIN (c) = list;
30242 list = c;
30244 return list;
30247 /* Parse all OpenACC clauses. The set clauses allowed by the directive
30248 is a bitmask in MASK. Return the list of clauses found. */
30250 static tree
30251 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
30252 const char *where, cp_token *pragma_tok,
30253 bool finish_p = true)
30255 tree clauses = NULL;
30256 bool first = true;
30258 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30260 location_t here;
30261 pragma_omp_clause c_kind;
30262 const char *c_name;
30263 tree prev = clauses;
30265 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30266 cp_lexer_consume_token (parser->lexer);
30268 here = cp_lexer_peek_token (parser->lexer)->location;
30269 c_kind = cp_parser_omp_clause_name (parser);
30271 switch (c_kind)
30273 case PRAGMA_OACC_CLAUSE_ASYNC:
30274 clauses = cp_parser_oacc_clause_async (parser, clauses);
30275 c_name = "async";
30276 break;
30277 case PRAGMA_OACC_CLAUSE_COLLAPSE:
30278 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
30279 c_name = "collapse";
30280 break;
30281 case PRAGMA_OACC_CLAUSE_COPY:
30282 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30283 c_name = "copy";
30284 break;
30285 case PRAGMA_OACC_CLAUSE_COPYIN:
30286 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30287 c_name = "copyin";
30288 break;
30289 case PRAGMA_OACC_CLAUSE_COPYOUT:
30290 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30291 c_name = "copyout";
30292 break;
30293 case PRAGMA_OACC_CLAUSE_CREATE:
30294 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30295 c_name = "create";
30296 break;
30297 case PRAGMA_OACC_CLAUSE_DELETE:
30298 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30299 c_name = "delete";
30300 break;
30301 case PRAGMA_OACC_CLAUSE_DEVICE:
30302 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30303 c_name = "device";
30304 break;
30305 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
30306 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
30307 c_name = "deviceptr";
30308 break;
30309 case PRAGMA_OACC_CLAUSE_HOST:
30310 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30311 c_name = "host";
30312 break;
30313 case PRAGMA_OACC_CLAUSE_IF:
30314 clauses = cp_parser_omp_clause_if (parser, clauses, here);
30315 c_name = "if";
30316 break;
30317 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
30318 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
30319 c_name = "num_gangs";
30320 break;
30321 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
30322 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
30323 c_name = "num_workers";
30324 break;
30325 case PRAGMA_OACC_CLAUSE_PRESENT:
30326 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30327 c_name = "present";
30328 break;
30329 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30330 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30331 c_name = "present_or_copy";
30332 break;
30333 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30334 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30335 c_name = "present_or_copyin";
30336 break;
30337 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30338 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30339 c_name = "present_or_copyout";
30340 break;
30341 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30342 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30343 c_name = "present_or_create";
30344 break;
30345 case PRAGMA_OACC_CLAUSE_REDUCTION:
30346 clauses = cp_parser_omp_clause_reduction (parser, clauses);
30347 c_name = "reduction";
30348 break;
30349 case PRAGMA_OACC_CLAUSE_SELF:
30350 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30351 c_name = "self";
30352 break;
30353 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
30354 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
30355 c_name = "vector_length";
30356 break;
30357 case PRAGMA_OACC_CLAUSE_WAIT:
30358 clauses = cp_parser_oacc_clause_wait (parser, clauses);
30359 c_name = "wait";
30360 break;
30361 default:
30362 cp_parser_error (parser, "expected %<#pragma acc%> clause");
30363 goto saw_error;
30366 first = false;
30368 if (((mask >> c_kind) & 1) == 0)
30370 /* Remove the invalid clause(s) from the list to avoid
30371 confusing the rest of the compiler. */
30372 clauses = prev;
30373 error_at (here, "%qs is not valid for %qs", c_name, where);
30377 saw_error:
30378 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30380 if (finish_p)
30381 return finish_omp_clauses (clauses);
30383 return clauses;
30386 /* Parse all OpenMP clauses. The set clauses allowed by the directive
30387 is a bitmask in MASK. Return the list of clauses found; the result
30388 of clause default goes in *pdefault. */
30390 static tree
30391 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
30392 const char *where, cp_token *pragma_tok,
30393 bool finish_p = true)
30395 tree clauses = NULL;
30396 bool first = true;
30397 cp_token *token = NULL;
30398 bool cilk_simd_fn = false;
30400 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30402 pragma_omp_clause c_kind;
30403 const char *c_name;
30404 tree prev = clauses;
30406 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30407 cp_lexer_consume_token (parser->lexer);
30409 token = cp_lexer_peek_token (parser->lexer);
30410 c_kind = cp_parser_omp_clause_name (parser);
30412 switch (c_kind)
30414 case PRAGMA_OMP_CLAUSE_COLLAPSE:
30415 clauses = cp_parser_omp_clause_collapse (parser, clauses,
30416 token->location);
30417 c_name = "collapse";
30418 break;
30419 case PRAGMA_OMP_CLAUSE_COPYIN:
30420 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
30421 c_name = "copyin";
30422 break;
30423 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
30424 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
30425 clauses);
30426 c_name = "copyprivate";
30427 break;
30428 case PRAGMA_OMP_CLAUSE_DEFAULT:
30429 clauses = cp_parser_omp_clause_default (parser, clauses,
30430 token->location);
30431 c_name = "default";
30432 break;
30433 case PRAGMA_OMP_CLAUSE_FINAL:
30434 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
30435 c_name = "final";
30436 break;
30437 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
30438 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
30439 clauses);
30440 c_name = "firstprivate";
30441 break;
30442 case PRAGMA_OMP_CLAUSE_IF:
30443 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
30444 c_name = "if";
30445 break;
30446 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
30447 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
30448 clauses);
30449 c_name = "lastprivate";
30450 break;
30451 case PRAGMA_OMP_CLAUSE_MERGEABLE:
30452 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
30453 token->location);
30454 c_name = "mergeable";
30455 break;
30456 case PRAGMA_OMP_CLAUSE_NOWAIT:
30457 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
30458 c_name = "nowait";
30459 break;
30460 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
30461 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
30462 token->location);
30463 c_name = "num_threads";
30464 break;
30465 case PRAGMA_OMP_CLAUSE_ORDERED:
30466 clauses = cp_parser_omp_clause_ordered (parser, clauses,
30467 token->location);
30468 c_name = "ordered";
30469 break;
30470 case PRAGMA_OMP_CLAUSE_PRIVATE:
30471 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
30472 clauses);
30473 c_name = "private";
30474 break;
30475 case PRAGMA_OMP_CLAUSE_REDUCTION:
30476 clauses = cp_parser_omp_clause_reduction (parser, clauses);
30477 c_name = "reduction";
30478 break;
30479 case PRAGMA_OMP_CLAUSE_SCHEDULE:
30480 clauses = cp_parser_omp_clause_schedule (parser, clauses,
30481 token->location);
30482 c_name = "schedule";
30483 break;
30484 case PRAGMA_OMP_CLAUSE_SHARED:
30485 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
30486 clauses);
30487 c_name = "shared";
30488 break;
30489 case PRAGMA_OMP_CLAUSE_UNTIED:
30490 clauses = cp_parser_omp_clause_untied (parser, clauses,
30491 token->location);
30492 c_name = "untied";
30493 break;
30494 case PRAGMA_OMP_CLAUSE_INBRANCH:
30495 case PRAGMA_CILK_CLAUSE_MASK:
30496 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
30497 clauses, token->location);
30498 c_name = "inbranch";
30499 break;
30500 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
30501 case PRAGMA_CILK_CLAUSE_NOMASK:
30502 clauses = cp_parser_omp_clause_branch (parser,
30503 OMP_CLAUSE_NOTINBRANCH,
30504 clauses, token->location);
30505 c_name = "notinbranch";
30506 break;
30507 case PRAGMA_OMP_CLAUSE_PARALLEL:
30508 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
30509 clauses, token->location);
30510 c_name = "parallel";
30511 if (!first)
30513 clause_not_first:
30514 error_at (token->location, "%qs must be the first clause of %qs",
30515 c_name, where);
30516 clauses = prev;
30518 break;
30519 case PRAGMA_OMP_CLAUSE_FOR:
30520 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
30521 clauses, token->location);
30522 c_name = "for";
30523 if (!first)
30524 goto clause_not_first;
30525 break;
30526 case PRAGMA_OMP_CLAUSE_SECTIONS:
30527 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
30528 clauses, token->location);
30529 c_name = "sections";
30530 if (!first)
30531 goto clause_not_first;
30532 break;
30533 case PRAGMA_OMP_CLAUSE_TASKGROUP:
30534 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
30535 clauses, token->location);
30536 c_name = "taskgroup";
30537 if (!first)
30538 goto clause_not_first;
30539 break;
30540 case PRAGMA_OMP_CLAUSE_TO:
30541 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
30542 clauses);
30543 c_name = "to";
30544 break;
30545 case PRAGMA_OMP_CLAUSE_FROM:
30546 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
30547 clauses);
30548 c_name = "from";
30549 break;
30550 case PRAGMA_OMP_CLAUSE_UNIFORM:
30551 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
30552 clauses);
30553 c_name = "uniform";
30554 break;
30555 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
30556 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
30557 token->location);
30558 c_name = "num_teams";
30559 break;
30560 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
30561 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
30562 token->location);
30563 c_name = "thread_limit";
30564 break;
30565 case PRAGMA_OMP_CLAUSE_ALIGNED:
30566 clauses = cp_parser_omp_clause_aligned (parser, clauses);
30567 c_name = "aligned";
30568 break;
30569 case PRAGMA_OMP_CLAUSE_LINEAR:
30570 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
30571 cilk_simd_fn = true;
30572 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
30573 c_name = "linear";
30574 break;
30575 case PRAGMA_OMP_CLAUSE_DEPEND:
30576 clauses = cp_parser_omp_clause_depend (parser, clauses);
30577 c_name = "depend";
30578 break;
30579 case PRAGMA_OMP_CLAUSE_MAP:
30580 clauses = cp_parser_omp_clause_map (parser, clauses);
30581 c_name = "map";
30582 break;
30583 case PRAGMA_OMP_CLAUSE_DEVICE:
30584 clauses = cp_parser_omp_clause_device (parser, clauses,
30585 token->location);
30586 c_name = "device";
30587 break;
30588 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
30589 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
30590 token->location);
30591 c_name = "dist_schedule";
30592 break;
30593 case PRAGMA_OMP_CLAUSE_PROC_BIND:
30594 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
30595 token->location);
30596 c_name = "proc_bind";
30597 break;
30598 case PRAGMA_OMP_CLAUSE_SAFELEN:
30599 clauses = cp_parser_omp_clause_safelen (parser, clauses,
30600 token->location);
30601 c_name = "safelen";
30602 break;
30603 case PRAGMA_OMP_CLAUSE_SIMDLEN:
30604 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
30605 token->location);
30606 c_name = "simdlen";
30607 break;
30608 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
30609 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
30610 c_name = "simdlen";
30611 break;
30612 default:
30613 cp_parser_error (parser, "expected %<#pragma omp%> clause");
30614 goto saw_error;
30617 first = false;
30619 if (((mask >> c_kind) & 1) == 0)
30621 /* Remove the invalid clause(s) from the list to avoid
30622 confusing the rest of the compiler. */
30623 clauses = prev;
30624 error_at (token->location, "%qs is not valid for %qs", c_name, where);
30627 saw_error:
30628 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
30629 no reason to skip to the end. */
30630 if (!(flag_cilkplus && pragma_tok == NULL))
30631 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30632 if (finish_p)
30633 return finish_omp_clauses (clauses);
30634 return clauses;
30637 /* OpenMP 2.5:
30638 structured-block:
30639 statement
30641 In practice, we're also interested in adding the statement to an
30642 outer node. So it is convenient if we work around the fact that
30643 cp_parser_statement calls add_stmt. */
30645 static unsigned
30646 cp_parser_begin_omp_structured_block (cp_parser *parser)
30648 unsigned save = parser->in_statement;
30650 /* Only move the values to IN_OMP_BLOCK if they weren't false.
30651 This preserves the "not within loop or switch" style error messages
30652 for nonsense cases like
30653 void foo() {
30654 #pragma omp single
30655 break;
30658 if (parser->in_statement)
30659 parser->in_statement = IN_OMP_BLOCK;
30661 return save;
30664 static void
30665 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
30667 parser->in_statement = save;
30670 static tree
30671 cp_parser_omp_structured_block (cp_parser *parser)
30673 tree stmt = begin_omp_structured_block ();
30674 unsigned int save = cp_parser_begin_omp_structured_block (parser);
30676 cp_parser_statement (parser, NULL_TREE, false, NULL);
30678 cp_parser_end_omp_structured_block (parser, save);
30679 return finish_omp_structured_block (stmt);
30682 /* OpenMP 2.5:
30683 # pragma omp atomic new-line
30684 expression-stmt
30686 expression-stmt:
30687 x binop= expr | x++ | ++x | x-- | --x
30688 binop:
30689 +, *, -, /, &, ^, |, <<, >>
30691 where x is an lvalue expression with scalar type.
30693 OpenMP 3.1:
30694 # pragma omp atomic new-line
30695 update-stmt
30697 # pragma omp atomic read new-line
30698 read-stmt
30700 # pragma omp atomic write new-line
30701 write-stmt
30703 # pragma omp atomic update new-line
30704 update-stmt
30706 # pragma omp atomic capture new-line
30707 capture-stmt
30709 # pragma omp atomic capture new-line
30710 capture-block
30712 read-stmt:
30713 v = x
30714 write-stmt:
30715 x = expr
30716 update-stmt:
30717 expression-stmt | x = x binop expr
30718 capture-stmt:
30719 v = expression-stmt
30720 capture-block:
30721 { v = x; update-stmt; } | { update-stmt; v = x; }
30723 OpenMP 4.0:
30724 update-stmt:
30725 expression-stmt | x = x binop expr | x = expr binop x
30726 capture-stmt:
30727 v = update-stmt
30728 capture-block:
30729 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
30731 where x and v are lvalue expressions with scalar type. */
30733 static void
30734 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
30736 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
30737 tree rhs1 = NULL_TREE, orig_lhs;
30738 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
30739 bool structured_block = false;
30740 bool seq_cst = false;
30742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30744 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30745 const char *p = IDENTIFIER_POINTER (id);
30747 if (!strcmp (p, "seq_cst"))
30749 seq_cst = true;
30750 cp_lexer_consume_token (parser->lexer);
30751 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30752 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30753 cp_lexer_consume_token (parser->lexer);
30756 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30758 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30759 const char *p = IDENTIFIER_POINTER (id);
30761 if (!strcmp (p, "read"))
30762 code = OMP_ATOMIC_READ;
30763 else if (!strcmp (p, "write"))
30764 code = NOP_EXPR;
30765 else if (!strcmp (p, "update"))
30766 code = OMP_ATOMIC;
30767 else if (!strcmp (p, "capture"))
30768 code = OMP_ATOMIC_CAPTURE_NEW;
30769 else
30770 p = NULL;
30771 if (p)
30772 cp_lexer_consume_token (parser->lexer);
30774 if (!seq_cst)
30776 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30777 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30778 cp_lexer_consume_token (parser->lexer);
30780 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30782 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30783 const char *p = IDENTIFIER_POINTER (id);
30785 if (!strcmp (p, "seq_cst"))
30787 seq_cst = true;
30788 cp_lexer_consume_token (parser->lexer);
30792 cp_parser_require_pragma_eol (parser, pragma_tok);
30794 switch (code)
30796 case OMP_ATOMIC_READ:
30797 case NOP_EXPR: /* atomic write */
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;
30803 if (code == NOP_EXPR)
30804 lhs = cp_parser_expression (parser);
30805 else
30806 lhs = cp_parser_unary_expression (parser);
30807 if (lhs == error_mark_node)
30808 goto saw_error;
30809 if (code == NOP_EXPR)
30811 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
30812 opcode. */
30813 code = OMP_ATOMIC;
30814 rhs = lhs;
30815 lhs = v;
30816 v = NULL_TREE;
30818 goto done;
30819 case OMP_ATOMIC_CAPTURE_NEW:
30820 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30822 cp_lexer_consume_token (parser->lexer);
30823 structured_block = true;
30825 else
30827 v = cp_parser_unary_expression (parser);
30828 if (v == error_mark_node)
30829 goto saw_error;
30830 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30831 goto saw_error;
30833 default:
30834 break;
30837 restart:
30838 lhs = cp_parser_unary_expression (parser);
30839 orig_lhs = lhs;
30840 switch (TREE_CODE (lhs))
30842 case ERROR_MARK:
30843 goto saw_error;
30845 case POSTINCREMENT_EXPR:
30846 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
30847 code = OMP_ATOMIC_CAPTURE_OLD;
30848 /* FALLTHROUGH */
30849 case PREINCREMENT_EXPR:
30850 lhs = TREE_OPERAND (lhs, 0);
30851 opcode = PLUS_EXPR;
30852 rhs = integer_one_node;
30853 break;
30855 case POSTDECREMENT_EXPR:
30856 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
30857 code = OMP_ATOMIC_CAPTURE_OLD;
30858 /* FALLTHROUGH */
30859 case PREDECREMENT_EXPR:
30860 lhs = TREE_OPERAND (lhs, 0);
30861 opcode = MINUS_EXPR;
30862 rhs = integer_one_node;
30863 break;
30865 case COMPOUND_EXPR:
30866 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
30867 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
30868 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
30869 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
30870 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
30871 (TREE_OPERAND (lhs, 1), 0), 0)))
30872 == BOOLEAN_TYPE)
30873 /* Undo effects of boolean_increment for post {in,de}crement. */
30874 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
30875 /* FALLTHRU */
30876 case MODIFY_EXPR:
30877 if (TREE_CODE (lhs) == MODIFY_EXPR
30878 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
30880 /* Undo effects of boolean_increment. */
30881 if (integer_onep (TREE_OPERAND (lhs, 1)))
30883 /* This is pre or post increment. */
30884 rhs = TREE_OPERAND (lhs, 1);
30885 lhs = TREE_OPERAND (lhs, 0);
30886 opcode = NOP_EXPR;
30887 if (code == OMP_ATOMIC_CAPTURE_NEW
30888 && !structured_block
30889 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
30890 code = OMP_ATOMIC_CAPTURE_OLD;
30891 break;
30894 /* FALLTHRU */
30895 default:
30896 switch (cp_lexer_peek_token (parser->lexer)->type)
30898 case CPP_MULT_EQ:
30899 opcode = MULT_EXPR;
30900 break;
30901 case CPP_DIV_EQ:
30902 opcode = TRUNC_DIV_EXPR;
30903 break;
30904 case CPP_PLUS_EQ:
30905 opcode = PLUS_EXPR;
30906 break;
30907 case CPP_MINUS_EQ:
30908 opcode = MINUS_EXPR;
30909 break;
30910 case CPP_LSHIFT_EQ:
30911 opcode = LSHIFT_EXPR;
30912 break;
30913 case CPP_RSHIFT_EQ:
30914 opcode = RSHIFT_EXPR;
30915 break;
30916 case CPP_AND_EQ:
30917 opcode = BIT_AND_EXPR;
30918 break;
30919 case CPP_OR_EQ:
30920 opcode = BIT_IOR_EXPR;
30921 break;
30922 case CPP_XOR_EQ:
30923 opcode = BIT_XOR_EXPR;
30924 break;
30925 case CPP_EQ:
30926 enum cp_parser_prec oprec;
30927 cp_token *token;
30928 cp_lexer_consume_token (parser->lexer);
30929 cp_parser_parse_tentatively (parser);
30930 rhs1 = cp_parser_simple_cast_expression (parser);
30931 if (rhs1 == error_mark_node)
30933 cp_parser_abort_tentative_parse (parser);
30934 cp_parser_simple_cast_expression (parser);
30935 goto saw_error;
30937 token = cp_lexer_peek_token (parser->lexer);
30938 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
30940 cp_parser_abort_tentative_parse (parser);
30941 cp_parser_parse_tentatively (parser);
30942 rhs = cp_parser_binary_expression (parser, false, true,
30943 PREC_NOT_OPERATOR, NULL);
30944 if (rhs == error_mark_node)
30946 cp_parser_abort_tentative_parse (parser);
30947 cp_parser_binary_expression (parser, false, true,
30948 PREC_NOT_OPERATOR, NULL);
30949 goto saw_error;
30951 switch (TREE_CODE (rhs))
30953 case MULT_EXPR:
30954 case TRUNC_DIV_EXPR:
30955 case RDIV_EXPR:
30956 case PLUS_EXPR:
30957 case MINUS_EXPR:
30958 case LSHIFT_EXPR:
30959 case RSHIFT_EXPR:
30960 case BIT_AND_EXPR:
30961 case BIT_IOR_EXPR:
30962 case BIT_XOR_EXPR:
30963 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
30965 if (cp_parser_parse_definitely (parser))
30967 opcode = TREE_CODE (rhs);
30968 rhs1 = TREE_OPERAND (rhs, 0);
30969 rhs = TREE_OPERAND (rhs, 1);
30970 goto stmt_done;
30972 else
30973 goto saw_error;
30975 break;
30976 default:
30977 break;
30979 cp_parser_abort_tentative_parse (parser);
30980 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
30982 rhs = cp_parser_expression (parser);
30983 if (rhs == error_mark_node)
30984 goto saw_error;
30985 opcode = NOP_EXPR;
30986 rhs1 = NULL_TREE;
30987 goto stmt_done;
30989 cp_parser_error (parser,
30990 "invalid form of %<#pragma omp atomic%>");
30991 goto saw_error;
30993 if (!cp_parser_parse_definitely (parser))
30994 goto saw_error;
30995 switch (token->type)
30997 case CPP_SEMICOLON:
30998 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
31000 code = OMP_ATOMIC_CAPTURE_OLD;
31001 v = lhs;
31002 lhs = NULL_TREE;
31003 lhs1 = rhs1;
31004 rhs1 = NULL_TREE;
31005 cp_lexer_consume_token (parser->lexer);
31006 goto restart;
31008 else if (structured_block)
31010 opcode = NOP_EXPR;
31011 rhs = rhs1;
31012 rhs1 = NULL_TREE;
31013 goto stmt_done;
31015 cp_parser_error (parser,
31016 "invalid form of %<#pragma omp atomic%>");
31017 goto saw_error;
31018 case CPP_MULT:
31019 opcode = MULT_EXPR;
31020 break;
31021 case CPP_DIV:
31022 opcode = TRUNC_DIV_EXPR;
31023 break;
31024 case CPP_PLUS:
31025 opcode = PLUS_EXPR;
31026 break;
31027 case CPP_MINUS:
31028 opcode = MINUS_EXPR;
31029 break;
31030 case CPP_LSHIFT:
31031 opcode = LSHIFT_EXPR;
31032 break;
31033 case CPP_RSHIFT:
31034 opcode = RSHIFT_EXPR;
31035 break;
31036 case CPP_AND:
31037 opcode = BIT_AND_EXPR;
31038 break;
31039 case CPP_OR:
31040 opcode = BIT_IOR_EXPR;
31041 break;
31042 case CPP_XOR:
31043 opcode = BIT_XOR_EXPR;
31044 break;
31045 default:
31046 cp_parser_error (parser,
31047 "invalid operator for %<#pragma omp atomic%>");
31048 goto saw_error;
31050 oprec = TOKEN_PRECEDENCE (token);
31051 gcc_assert (oprec != PREC_NOT_OPERATOR);
31052 if (commutative_tree_code (opcode))
31053 oprec = (enum cp_parser_prec) (oprec - 1);
31054 cp_lexer_consume_token (parser->lexer);
31055 rhs = cp_parser_binary_expression (parser, false, false,
31056 oprec, NULL);
31057 if (rhs == error_mark_node)
31058 goto saw_error;
31059 goto stmt_done;
31060 /* FALLTHROUGH */
31061 default:
31062 cp_parser_error (parser,
31063 "invalid operator for %<#pragma omp atomic%>");
31064 goto saw_error;
31066 cp_lexer_consume_token (parser->lexer);
31068 rhs = cp_parser_expression (parser);
31069 if (rhs == error_mark_node)
31070 goto saw_error;
31071 break;
31073 stmt_done:
31074 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
31076 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
31077 goto saw_error;
31078 v = cp_parser_unary_expression (parser);
31079 if (v == error_mark_node)
31080 goto saw_error;
31081 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
31082 goto saw_error;
31083 lhs1 = cp_parser_unary_expression (parser);
31084 if (lhs1 == error_mark_node)
31085 goto saw_error;
31087 if (structured_block)
31089 cp_parser_consume_semicolon_at_end_of_statement (parser);
31090 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
31092 done:
31093 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
31094 if (!structured_block)
31095 cp_parser_consume_semicolon_at_end_of_statement (parser);
31096 return;
31098 saw_error:
31099 cp_parser_skip_to_end_of_block_or_statement (parser);
31100 if (structured_block)
31102 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31103 cp_lexer_consume_token (parser->lexer);
31104 else if (code == OMP_ATOMIC_CAPTURE_NEW)
31106 cp_parser_skip_to_end_of_block_or_statement (parser);
31107 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31108 cp_lexer_consume_token (parser->lexer);
31114 /* OpenMP 2.5:
31115 # pragma omp barrier new-line */
31117 static void
31118 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
31120 cp_parser_require_pragma_eol (parser, pragma_tok);
31121 finish_omp_barrier ();
31124 /* OpenMP 2.5:
31125 # pragma omp critical [(name)] new-line
31126 structured-block */
31128 static tree
31129 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
31131 tree stmt, name = NULL;
31133 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31135 cp_lexer_consume_token (parser->lexer);
31137 name = cp_parser_identifier (parser);
31139 if (name == error_mark_node
31140 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31141 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31142 /*or_comma=*/false,
31143 /*consume_paren=*/true);
31144 if (name == error_mark_node)
31145 name = NULL;
31147 cp_parser_require_pragma_eol (parser, pragma_tok);
31149 stmt = cp_parser_omp_structured_block (parser);
31150 return c_finish_omp_critical (input_location, stmt, name);
31153 /* OpenMP 2.5:
31154 # pragma omp flush flush-vars[opt] new-line
31156 flush-vars:
31157 ( variable-list ) */
31159 static void
31160 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
31162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31163 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31164 cp_parser_require_pragma_eol (parser, pragma_tok);
31166 finish_omp_flush ();
31169 /* Helper function, to parse omp for increment expression. */
31171 static tree
31172 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
31174 tree cond = cp_parser_binary_expression (parser, false, true,
31175 PREC_NOT_OPERATOR, NULL);
31176 if (cond == error_mark_node
31177 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31179 cp_parser_skip_to_end_of_statement (parser);
31180 return error_mark_node;
31183 switch (TREE_CODE (cond))
31185 case GT_EXPR:
31186 case GE_EXPR:
31187 case LT_EXPR:
31188 case LE_EXPR:
31189 break;
31190 case NE_EXPR:
31191 if (code == CILK_SIMD || code == CILK_FOR)
31192 break;
31193 /* Fall through: OpenMP disallows NE_EXPR. */
31194 default:
31195 return error_mark_node;
31198 /* If decl is an iterator, preserve LHS and RHS of the relational
31199 expr until finish_omp_for. */
31200 if (decl
31201 && (type_dependent_expression_p (decl)
31202 || CLASS_TYPE_P (TREE_TYPE (decl))))
31203 return cond;
31205 return build_x_binary_op (input_location, TREE_CODE (cond),
31206 TREE_OPERAND (cond, 0), ERROR_MARK,
31207 TREE_OPERAND (cond, 1), ERROR_MARK,
31208 /*overload=*/NULL, tf_warning_or_error);
31211 /* Helper function, to parse omp for increment expression. */
31213 static tree
31214 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
31216 cp_token *token = cp_lexer_peek_token (parser->lexer);
31217 enum tree_code op;
31218 tree lhs, rhs;
31219 cp_id_kind idk;
31220 bool decl_first;
31222 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
31224 op = (token->type == CPP_PLUS_PLUS
31225 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
31226 cp_lexer_consume_token (parser->lexer);
31227 lhs = cp_parser_simple_cast_expression (parser);
31228 if (lhs != decl)
31229 return error_mark_node;
31230 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
31233 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
31234 if (lhs != decl)
31235 return error_mark_node;
31237 token = cp_lexer_peek_token (parser->lexer);
31238 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
31240 op = (token->type == CPP_PLUS_PLUS
31241 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
31242 cp_lexer_consume_token (parser->lexer);
31243 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
31246 op = cp_parser_assignment_operator_opt (parser);
31247 if (op == ERROR_MARK)
31248 return error_mark_node;
31250 if (op != NOP_EXPR)
31252 rhs = cp_parser_assignment_expression (parser);
31253 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
31254 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
31257 lhs = cp_parser_binary_expression (parser, false, false,
31258 PREC_ADDITIVE_EXPRESSION, NULL);
31259 token = cp_lexer_peek_token (parser->lexer);
31260 decl_first = lhs == decl;
31261 if (decl_first)
31262 lhs = NULL_TREE;
31263 if (token->type != CPP_PLUS
31264 && token->type != CPP_MINUS)
31265 return error_mark_node;
31269 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
31270 cp_lexer_consume_token (parser->lexer);
31271 rhs = cp_parser_binary_expression (parser, false, false,
31272 PREC_ADDITIVE_EXPRESSION, NULL);
31273 token = cp_lexer_peek_token (parser->lexer);
31274 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
31276 if (lhs == NULL_TREE)
31278 if (op == PLUS_EXPR)
31279 lhs = rhs;
31280 else
31281 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
31282 tf_warning_or_error);
31284 else
31285 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
31286 ERROR_MARK, NULL, tf_warning_or_error);
31289 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
31291 if (!decl_first)
31293 if (rhs != decl || op == MINUS_EXPR)
31294 return error_mark_node;
31295 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
31297 else
31298 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
31300 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
31303 /* Parse the initialization statement of either an OpenMP for loop or
31304 a Cilk Plus for loop.
31306 Return true if the resulting construct should have an
31307 OMP_CLAUSE_PRIVATE added to it. */
31309 static bool
31310 cp_parser_omp_for_loop_init (cp_parser *parser,
31311 enum tree_code code,
31312 tree &this_pre_body,
31313 vec<tree, va_gc> *for_block,
31314 tree &init,
31315 tree &decl,
31316 tree &real_decl)
31318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31319 return false;
31321 bool add_private_clause = false;
31323 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
31325 init-expr:
31326 var = lb
31327 integer-type var = lb
31328 random-access-iterator-type var = lb
31329 pointer-type var = lb
31331 cp_decl_specifier_seq type_specifiers;
31333 /* First, try to parse as an initialized declaration. See
31334 cp_parser_condition, from whence the bulk of this is copied. */
31336 cp_parser_parse_tentatively (parser);
31337 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
31338 /*is_trailing_return=*/false,
31339 &type_specifiers);
31340 if (cp_parser_parse_definitely (parser))
31342 /* If parsing a type specifier seq succeeded, then this
31343 MUST be a initialized declaration. */
31344 tree asm_specification, attributes;
31345 cp_declarator *declarator;
31347 declarator = cp_parser_declarator (parser,
31348 CP_PARSER_DECLARATOR_NAMED,
31349 /*ctor_dtor_or_conv_p=*/NULL,
31350 /*parenthesized_p=*/NULL,
31351 /*member_p=*/false,
31352 /*friend_p=*/false);
31353 attributes = cp_parser_attributes_opt (parser);
31354 asm_specification = cp_parser_asm_specification_opt (parser);
31356 if (declarator == cp_error_declarator)
31357 cp_parser_skip_to_end_of_statement (parser);
31359 else
31361 tree pushed_scope, auto_node;
31363 decl = start_decl (declarator, &type_specifiers,
31364 SD_INITIALIZED, attributes,
31365 /*prefix_attributes=*/NULL_TREE,
31366 &pushed_scope);
31368 auto_node = type_uses_auto (TREE_TYPE (decl));
31369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31371 if (cp_lexer_next_token_is (parser->lexer,
31372 CPP_OPEN_PAREN))
31374 if (code != CILK_SIMD && code != CILK_FOR)
31375 error ("parenthesized initialization is not allowed in "
31376 "OpenMP %<for%> loop");
31377 else
31378 error ("parenthesized initialization is "
31379 "not allowed in for-loop");
31381 else
31382 /* Trigger an error. */
31383 cp_parser_require (parser, CPP_EQ, RT_EQ);
31385 init = error_mark_node;
31386 cp_parser_skip_to_end_of_statement (parser);
31388 else if (CLASS_TYPE_P (TREE_TYPE (decl))
31389 || type_dependent_expression_p (decl)
31390 || auto_node)
31392 bool is_direct_init, is_non_constant_init;
31394 init = cp_parser_initializer (parser,
31395 &is_direct_init,
31396 &is_non_constant_init);
31398 if (auto_node)
31400 TREE_TYPE (decl)
31401 = do_auto_deduction (TREE_TYPE (decl), init,
31402 auto_node);
31404 if (!CLASS_TYPE_P (TREE_TYPE (decl))
31405 && !type_dependent_expression_p (decl))
31406 goto non_class;
31409 cp_finish_decl (decl, init, !is_non_constant_init,
31410 asm_specification,
31411 LOOKUP_ONLYCONVERTING);
31412 if (CLASS_TYPE_P (TREE_TYPE (decl)))
31414 vec_safe_push (for_block, this_pre_body);
31415 init = NULL_TREE;
31417 else
31418 init = pop_stmt_list (this_pre_body);
31419 this_pre_body = NULL_TREE;
31421 else
31423 /* Consume '='. */
31424 cp_lexer_consume_token (parser->lexer);
31425 init = cp_parser_assignment_expression (parser);
31427 non_class:
31428 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
31429 init = error_mark_node;
31430 else
31431 cp_finish_decl (decl, NULL_TREE,
31432 /*init_const_expr_p=*/false,
31433 asm_specification,
31434 LOOKUP_ONLYCONVERTING);
31437 if (pushed_scope)
31438 pop_scope (pushed_scope);
31441 else
31443 cp_id_kind idk;
31444 /* If parsing a type specifier sequence failed, then
31445 this MUST be a simple expression. */
31446 if (code == CILK_FOR)
31447 error ("%<_Cilk_for%> allows expression instead of declaration only "
31448 "in C, not in C++");
31449 cp_parser_parse_tentatively (parser);
31450 decl = cp_parser_primary_expression (parser, false, false,
31451 false, &idk);
31452 if (!cp_parser_error_occurred (parser)
31453 && decl
31454 && DECL_P (decl)
31455 && CLASS_TYPE_P (TREE_TYPE (decl)))
31457 tree rhs;
31459 cp_parser_parse_definitely (parser);
31460 cp_parser_require (parser, CPP_EQ, RT_EQ);
31461 rhs = cp_parser_assignment_expression (parser);
31462 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
31463 decl, NOP_EXPR,
31464 rhs,
31465 tf_warning_or_error));
31466 add_private_clause = true;
31468 else
31470 decl = NULL;
31471 cp_parser_abort_tentative_parse (parser);
31472 init = cp_parser_expression (parser);
31473 if (init)
31475 if (TREE_CODE (init) == MODIFY_EXPR
31476 || TREE_CODE (init) == MODOP_EXPR)
31477 real_decl = TREE_OPERAND (init, 0);
31481 return add_private_clause;
31484 /* Parse the restricted form of the for statement allowed by OpenMP. */
31486 static tree
31487 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
31488 tree *cclauses)
31490 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
31491 tree real_decl, initv, condv, incrv, declv;
31492 tree this_pre_body, cl;
31493 location_t loc_first;
31494 bool collapse_err = false;
31495 int i, collapse = 1, nbraces = 0;
31496 vec<tree, va_gc> *for_block = make_tree_vector ();
31498 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
31499 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
31500 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
31502 gcc_assert (collapse >= 1);
31504 declv = make_tree_vec (collapse);
31505 initv = make_tree_vec (collapse);
31506 condv = make_tree_vec (collapse);
31507 incrv = make_tree_vec (collapse);
31509 loc_first = cp_lexer_peek_token (parser->lexer)->location;
31511 for (i = 0; i < collapse; i++)
31513 int bracecount = 0;
31514 bool add_private_clause = false;
31515 location_t loc;
31517 if (code != CILK_FOR
31518 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31520 cp_parser_error (parser, "for statement expected");
31521 return NULL;
31523 if (code == CILK_FOR
31524 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31526 cp_parser_error (parser, "_Cilk_for statement expected");
31527 return NULL;
31529 loc = cp_lexer_consume_token (parser->lexer)->location;
31531 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31532 return NULL;
31534 init = decl = real_decl = NULL;
31535 this_pre_body = push_stmt_list ();
31537 add_private_clause
31538 |= cp_parser_omp_for_loop_init (parser, code,
31539 this_pre_body, for_block,
31540 init, decl, real_decl);
31542 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31543 if (this_pre_body)
31545 this_pre_body = pop_stmt_list (this_pre_body);
31546 if (pre_body)
31548 tree t = pre_body;
31549 pre_body = push_stmt_list ();
31550 add_stmt (t);
31551 add_stmt (this_pre_body);
31552 pre_body = pop_stmt_list (pre_body);
31554 else
31555 pre_body = this_pre_body;
31558 if (decl)
31559 real_decl = decl;
31560 if (cclauses != NULL
31561 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
31562 && real_decl != NULL_TREE)
31564 tree *c;
31565 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
31566 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
31567 && OMP_CLAUSE_DECL (*c) == real_decl)
31569 error_at (loc, "iteration variable %qD"
31570 " should not be firstprivate", real_decl);
31571 *c = OMP_CLAUSE_CHAIN (*c);
31573 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
31574 && OMP_CLAUSE_DECL (*c) == real_decl)
31576 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
31577 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
31578 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
31579 OMP_CLAUSE_DECL (l) = real_decl;
31580 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
31581 if (code == OMP_SIMD)
31583 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31584 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
31586 else
31588 OMP_CLAUSE_CHAIN (l) = clauses;
31589 clauses = l;
31591 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
31592 CP_OMP_CLAUSE_INFO (*c) = NULL;
31593 add_private_clause = false;
31595 else
31597 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
31598 && OMP_CLAUSE_DECL (*c) == real_decl)
31599 add_private_clause = false;
31600 c = &OMP_CLAUSE_CHAIN (*c);
31604 if (add_private_clause)
31606 tree c;
31607 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31609 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
31610 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
31611 && OMP_CLAUSE_DECL (c) == decl)
31612 break;
31613 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
31614 && OMP_CLAUSE_DECL (c) == decl)
31615 error_at (loc, "iteration variable %qD "
31616 "should not be firstprivate",
31617 decl);
31618 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
31619 && OMP_CLAUSE_DECL (c) == decl)
31620 error_at (loc, "iteration variable %qD should not be reduction",
31621 decl);
31623 if (c == NULL)
31625 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
31626 OMP_CLAUSE_DECL (c) = decl;
31627 c = finish_omp_clauses (c);
31628 if (c)
31630 OMP_CLAUSE_CHAIN (c) = clauses;
31631 clauses = c;
31636 cond = NULL;
31637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31638 cond = cp_parser_omp_for_cond (parser, decl, code);
31639 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31641 incr = NULL;
31642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31644 /* If decl is an iterator, preserve the operator on decl
31645 until finish_omp_for. */
31646 if (real_decl
31647 && ((processing_template_decl
31648 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
31649 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
31650 incr = cp_parser_omp_for_incr (parser, real_decl);
31651 else
31652 incr = cp_parser_expression (parser);
31653 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
31654 SET_EXPR_LOCATION (incr, input_location);
31657 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31658 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31659 /*or_comma=*/false,
31660 /*consume_paren=*/true);
31662 TREE_VEC_ELT (declv, i) = decl;
31663 TREE_VEC_ELT (initv, i) = init;
31664 TREE_VEC_ELT (condv, i) = cond;
31665 TREE_VEC_ELT (incrv, i) = incr;
31667 if (i == collapse - 1)
31668 break;
31670 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
31671 in between the collapsed for loops to be still considered perfectly
31672 nested. Hopefully the final version clarifies this.
31673 For now handle (multiple) {'s and empty statements. */
31674 cp_parser_parse_tentatively (parser);
31677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31678 break;
31679 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31681 cp_lexer_consume_token (parser->lexer);
31682 bracecount++;
31684 else if (bracecount
31685 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31686 cp_lexer_consume_token (parser->lexer);
31687 else
31689 loc = cp_lexer_peek_token (parser->lexer)->location;
31690 error_at (loc, "not enough collapsed for loops");
31691 collapse_err = true;
31692 cp_parser_abort_tentative_parse (parser);
31693 declv = NULL_TREE;
31694 break;
31697 while (1);
31699 if (declv)
31701 cp_parser_parse_definitely (parser);
31702 nbraces += bracecount;
31706 /* Note that we saved the original contents of this flag when we entered
31707 the structured block, and so we don't need to re-save it here. */
31708 if (code == CILK_SIMD || code == CILK_FOR)
31709 parser->in_statement = IN_CILK_SIMD_FOR;
31710 else
31711 parser->in_statement = IN_OMP_FOR;
31713 /* Note that the grammar doesn't call for a structured block here,
31714 though the loop as a whole is a structured block. */
31715 body = push_stmt_list ();
31716 cp_parser_statement (parser, NULL_TREE, false, NULL);
31717 body = pop_stmt_list (body);
31719 if (declv == NULL_TREE)
31720 ret = NULL_TREE;
31721 else
31722 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
31723 pre_body, clauses);
31725 while (nbraces)
31727 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31729 cp_lexer_consume_token (parser->lexer);
31730 nbraces--;
31732 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31733 cp_lexer_consume_token (parser->lexer);
31734 else
31736 if (!collapse_err)
31738 error_at (cp_lexer_peek_token (parser->lexer)->location,
31739 "collapsed loops not perfectly nested");
31741 collapse_err = true;
31742 cp_parser_statement_seq_opt (parser, NULL);
31743 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
31744 break;
31748 while (!for_block->is_empty ())
31749 add_stmt (pop_stmt_list (for_block->pop ()));
31750 release_tree_vector (for_block);
31752 return ret;
31755 /* Helper function for OpenMP parsing, split clauses and call
31756 finish_omp_clauses on each of the set of clauses afterwards. */
31758 static void
31759 cp_omp_split_clauses (location_t loc, enum tree_code code,
31760 omp_clause_mask mask, tree clauses, tree *cclauses)
31762 int i;
31763 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
31764 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
31765 if (cclauses[i])
31766 cclauses[i] = finish_omp_clauses (cclauses[i]);
31769 /* OpenMP 4.0:
31770 #pragma omp simd simd-clause[optseq] new-line
31771 for-loop */
31773 #define OMP_SIMD_CLAUSE_MASK \
31774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
31775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31782 static tree
31783 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
31784 char *p_name, omp_clause_mask mask, tree *cclauses)
31786 tree clauses, sb, ret;
31787 unsigned int save;
31788 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31790 strcat (p_name, " simd");
31791 mask |= OMP_SIMD_CLAUSE_MASK;
31792 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
31794 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31795 cclauses == NULL);
31796 if (cclauses)
31798 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
31799 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
31802 sb = begin_omp_structured_block ();
31803 save = cp_parser_begin_omp_structured_block (parser);
31805 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
31807 cp_parser_end_omp_structured_block (parser, save);
31808 add_stmt (finish_omp_structured_block (sb));
31810 return ret;
31813 /* OpenMP 2.5:
31814 #pragma omp for for-clause[optseq] new-line
31815 for-loop
31817 OpenMP 4.0:
31818 #pragma omp for simd for-simd-clause[optseq] new-line
31819 for-loop */
31821 #define OMP_FOR_CLAUSE_MASK \
31822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
31827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
31828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
31829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31831 static tree
31832 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
31833 char *p_name, omp_clause_mask mask, tree *cclauses)
31835 tree clauses, sb, ret;
31836 unsigned int save;
31837 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31839 strcat (p_name, " for");
31840 mask |= OMP_FOR_CLAUSE_MASK;
31841 if (cclauses)
31842 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31846 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31847 const char *p = IDENTIFIER_POINTER (id);
31849 if (strcmp (p, "simd") == 0)
31851 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31852 if (cclauses == NULL)
31853 cclauses = cclauses_buf;
31855 cp_lexer_consume_token (parser->lexer);
31856 if (!flag_openmp) /* flag_openmp_simd */
31857 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31858 cclauses);
31859 sb = begin_omp_structured_block ();
31860 save = cp_parser_begin_omp_structured_block (parser);
31861 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31862 cclauses);
31863 cp_parser_end_omp_structured_block (parser, save);
31864 tree body = finish_omp_structured_block (sb);
31865 if (ret == NULL)
31866 return ret;
31867 ret = make_node (OMP_FOR);
31868 TREE_TYPE (ret) = void_type_node;
31869 OMP_FOR_BODY (ret) = body;
31870 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31871 SET_EXPR_LOCATION (ret, loc);
31872 add_stmt (ret);
31873 return ret;
31876 if (!flag_openmp) /* flag_openmp_simd */
31878 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31879 return NULL_TREE;
31882 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31883 cclauses == NULL);
31884 if (cclauses)
31886 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
31887 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31890 sb = begin_omp_structured_block ();
31891 save = cp_parser_begin_omp_structured_block (parser);
31893 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
31895 cp_parser_end_omp_structured_block (parser, save);
31896 add_stmt (finish_omp_structured_block (sb));
31898 return ret;
31901 /* OpenMP 2.5:
31902 # pragma omp master new-line
31903 structured-block */
31905 static tree
31906 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
31908 cp_parser_require_pragma_eol (parser, pragma_tok);
31909 return c_finish_omp_master (input_location,
31910 cp_parser_omp_structured_block (parser));
31913 /* OpenMP 2.5:
31914 # pragma omp ordered new-line
31915 structured-block */
31917 static tree
31918 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
31920 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31921 cp_parser_require_pragma_eol (parser, pragma_tok);
31922 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
31925 /* OpenMP 2.5:
31927 section-scope:
31928 { section-sequence }
31930 section-sequence:
31931 section-directive[opt] structured-block
31932 section-sequence section-directive structured-block */
31934 static tree
31935 cp_parser_omp_sections_scope (cp_parser *parser)
31937 tree stmt, substmt;
31938 bool error_suppress = false;
31939 cp_token *tok;
31941 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
31942 return NULL_TREE;
31944 stmt = push_stmt_list ();
31946 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
31948 substmt = cp_parser_omp_structured_block (parser);
31949 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31950 add_stmt (substmt);
31953 while (1)
31955 tok = cp_lexer_peek_token (parser->lexer);
31956 if (tok->type == CPP_CLOSE_BRACE)
31957 break;
31958 if (tok->type == CPP_EOF)
31959 break;
31961 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
31963 cp_lexer_consume_token (parser->lexer);
31964 cp_parser_require_pragma_eol (parser, tok);
31965 error_suppress = false;
31967 else if (!error_suppress)
31969 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
31970 error_suppress = true;
31973 substmt = cp_parser_omp_structured_block (parser);
31974 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31975 add_stmt (substmt);
31977 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
31979 substmt = pop_stmt_list (stmt);
31981 stmt = make_node (OMP_SECTIONS);
31982 TREE_TYPE (stmt) = void_type_node;
31983 OMP_SECTIONS_BODY (stmt) = substmt;
31985 add_stmt (stmt);
31986 return stmt;
31989 /* OpenMP 2.5:
31990 # pragma omp sections sections-clause[optseq] newline
31991 sections-scope */
31993 #define OMP_SECTIONS_CLAUSE_MASK \
31994 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32000 static tree
32001 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
32002 char *p_name, omp_clause_mask mask, tree *cclauses)
32004 tree clauses, ret;
32005 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32007 strcat (p_name, " sections");
32008 mask |= OMP_SECTIONS_CLAUSE_MASK;
32009 if (cclauses)
32010 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
32012 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32013 cclauses == NULL);
32014 if (cclauses)
32016 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
32017 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
32020 ret = cp_parser_omp_sections_scope (parser);
32021 if (ret)
32022 OMP_SECTIONS_CLAUSES (ret) = clauses;
32024 return ret;
32027 /* OpenMP 2.5:
32028 # pragma omp parallel parallel-clause[optseq] new-line
32029 structured-block
32030 # pragma omp parallel for parallel-for-clause[optseq] new-line
32031 structured-block
32032 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
32033 structured-block
32035 OpenMP 4.0:
32036 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
32037 structured-block */
32039 #define OMP_PARALLEL_CLAUSE_MASK \
32040 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
32044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
32046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
32048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
32050 static tree
32051 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
32052 char *p_name, omp_clause_mask mask, tree *cclauses)
32054 tree stmt, clauses, block;
32055 unsigned int save;
32056 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32058 strcat (p_name, " parallel");
32059 mask |= OMP_PARALLEL_CLAUSE_MASK;
32061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32063 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32064 if (cclauses == NULL)
32065 cclauses = cclauses_buf;
32067 cp_lexer_consume_token (parser->lexer);
32068 if (!flag_openmp) /* flag_openmp_simd */
32069 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
32070 block = begin_omp_parallel ();
32071 save = cp_parser_begin_omp_structured_block (parser);
32072 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
32073 cp_parser_end_omp_structured_block (parser, save);
32074 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
32075 block);
32076 if (ret == NULL_TREE)
32077 return ret;
32078 OMP_PARALLEL_COMBINED (stmt) = 1;
32079 return stmt;
32081 else if (cclauses)
32083 error_at (loc, "expected %<for%> after %qs", p_name);
32084 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32085 return NULL_TREE;
32087 else if (!flag_openmp) /* flag_openmp_simd */
32089 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32090 return NULL_TREE;
32092 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32094 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32095 const char *p = IDENTIFIER_POINTER (id);
32096 if (strcmp (p, "sections") == 0)
32098 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32099 cclauses = cclauses_buf;
32101 cp_lexer_consume_token (parser->lexer);
32102 block = begin_omp_parallel ();
32103 save = cp_parser_begin_omp_structured_block (parser);
32104 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
32105 cp_parser_end_omp_structured_block (parser, save);
32106 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
32107 block);
32108 OMP_PARALLEL_COMBINED (stmt) = 1;
32109 return stmt;
32113 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
32115 block = begin_omp_parallel ();
32116 save = cp_parser_begin_omp_structured_block (parser);
32117 cp_parser_statement (parser, NULL_TREE, false, NULL);
32118 cp_parser_end_omp_structured_block (parser, save);
32119 stmt = finish_omp_parallel (clauses, block);
32120 return stmt;
32123 /* OpenMP 2.5:
32124 # pragma omp single single-clause[optseq] new-line
32125 structured-block */
32127 #define OMP_SINGLE_CLAUSE_MASK \
32128 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
32131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32133 static tree
32134 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
32136 tree stmt = make_node (OMP_SINGLE);
32137 TREE_TYPE (stmt) = void_type_node;
32139 OMP_SINGLE_CLAUSES (stmt)
32140 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
32141 "#pragma omp single", pragma_tok);
32142 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
32144 return add_stmt (stmt);
32147 /* OpenMP 3.0:
32148 # pragma omp task task-clause[optseq] new-line
32149 structured-block */
32151 #define OMP_TASK_CLAUSE_MASK \
32152 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
32154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
32155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
32159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
32160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
32162 static tree
32163 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
32165 tree clauses, block;
32166 unsigned int save;
32168 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
32169 "#pragma omp task", pragma_tok);
32170 block = begin_omp_task ();
32171 save = cp_parser_begin_omp_structured_block (parser);
32172 cp_parser_statement (parser, NULL_TREE, false, NULL);
32173 cp_parser_end_omp_structured_block (parser, save);
32174 return finish_omp_task (clauses, block);
32177 /* OpenMP 3.0:
32178 # pragma omp taskwait new-line */
32180 static void
32181 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
32183 cp_parser_require_pragma_eol (parser, pragma_tok);
32184 finish_omp_taskwait ();
32187 /* OpenMP 3.1:
32188 # pragma omp taskyield new-line */
32190 static void
32191 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
32193 cp_parser_require_pragma_eol (parser, pragma_tok);
32194 finish_omp_taskyield ();
32197 /* OpenMP 4.0:
32198 # pragma omp taskgroup new-line
32199 structured-block */
32201 static tree
32202 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
32204 cp_parser_require_pragma_eol (parser, pragma_tok);
32205 return c_finish_omp_taskgroup (input_location,
32206 cp_parser_omp_structured_block (parser));
32210 /* OpenMP 2.5:
32211 # pragma omp threadprivate (variable-list) */
32213 static void
32214 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
32216 tree vars;
32218 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32219 cp_parser_require_pragma_eol (parser, pragma_tok);
32221 finish_omp_threadprivate (vars);
32224 /* OpenMP 4.0:
32225 # pragma omp cancel cancel-clause[optseq] new-line */
32227 #define OMP_CANCEL_CLAUSE_MASK \
32228 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
32229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
32230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
32231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
32232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32234 static void
32235 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
32237 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
32238 "#pragma omp cancel", pragma_tok);
32239 finish_omp_cancel (clauses);
32242 /* OpenMP 4.0:
32243 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
32245 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
32246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
32247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
32248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
32249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
32251 static void
32252 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
32254 tree clauses;
32255 bool point_seen = false;
32257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32259 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32260 const char *p = IDENTIFIER_POINTER (id);
32262 if (strcmp (p, "point") == 0)
32264 cp_lexer_consume_token (parser->lexer);
32265 point_seen = true;
32268 if (!point_seen)
32270 cp_parser_error (parser, "expected %<point%>");
32271 cp_parser_require_pragma_eol (parser, pragma_tok);
32272 return;
32275 clauses = cp_parser_omp_all_clauses (parser,
32276 OMP_CANCELLATION_POINT_CLAUSE_MASK,
32277 "#pragma omp cancellation point",
32278 pragma_tok);
32279 finish_omp_cancellation_point (clauses);
32282 /* OpenMP 4.0:
32283 #pragma omp distribute distribute-clause[optseq] new-line
32284 for-loop */
32286 #define OMP_DISTRIBUTE_CLAUSE_MASK \
32287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
32290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
32292 static tree
32293 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
32294 char *p_name, omp_clause_mask mask, tree *cclauses)
32296 tree clauses, sb, ret;
32297 unsigned int save;
32298 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32300 strcat (p_name, " distribute");
32301 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
32303 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32305 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32306 const char *p = IDENTIFIER_POINTER (id);
32307 bool simd = false;
32308 bool parallel = false;
32310 if (strcmp (p, "simd") == 0)
32311 simd = true;
32312 else
32313 parallel = strcmp (p, "parallel") == 0;
32314 if (parallel || simd)
32316 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32317 if (cclauses == NULL)
32318 cclauses = cclauses_buf;
32319 cp_lexer_consume_token (parser->lexer);
32320 if (!flag_openmp) /* flag_openmp_simd */
32322 if (simd)
32323 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32324 cclauses);
32325 else
32326 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
32327 cclauses);
32329 sb = begin_omp_structured_block ();
32330 save = cp_parser_begin_omp_structured_block (parser);
32331 if (simd)
32332 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32333 cclauses);
32334 else
32335 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
32336 cclauses);
32337 cp_parser_end_omp_structured_block (parser, save);
32338 tree body = finish_omp_structured_block (sb);
32339 if (ret == NULL)
32340 return ret;
32341 ret = make_node (OMP_DISTRIBUTE);
32342 TREE_TYPE (ret) = void_type_node;
32343 OMP_FOR_BODY (ret) = body;
32344 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
32345 SET_EXPR_LOCATION (ret, loc);
32346 add_stmt (ret);
32347 return ret;
32350 if (!flag_openmp) /* flag_openmp_simd */
32352 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32353 return NULL_TREE;
32356 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32357 cclauses == NULL);
32358 if (cclauses)
32360 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
32361 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
32364 sb = begin_omp_structured_block ();
32365 save = cp_parser_begin_omp_structured_block (parser);
32367 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
32369 cp_parser_end_omp_structured_block (parser, save);
32370 add_stmt (finish_omp_structured_block (sb));
32372 return ret;
32375 /* OpenMP 4.0:
32376 # pragma omp teams teams-clause[optseq] new-line
32377 structured-block */
32379 #define OMP_TEAMS_CLAUSE_MASK \
32380 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
32385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
32386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
32388 static tree
32389 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
32390 char *p_name, omp_clause_mask mask, tree *cclauses)
32392 tree clauses, sb, ret;
32393 unsigned int save;
32394 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32396 strcat (p_name, " teams");
32397 mask |= OMP_TEAMS_CLAUSE_MASK;
32399 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32401 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32402 const char *p = IDENTIFIER_POINTER (id);
32403 if (strcmp (p, "distribute") == 0)
32405 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32406 if (cclauses == NULL)
32407 cclauses = cclauses_buf;
32409 cp_lexer_consume_token (parser->lexer);
32410 if (!flag_openmp) /* flag_openmp_simd */
32411 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
32412 cclauses);
32413 sb = begin_omp_structured_block ();
32414 save = cp_parser_begin_omp_structured_block (parser);
32415 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
32416 cclauses);
32417 cp_parser_end_omp_structured_block (parser, save);
32418 tree body = finish_omp_structured_block (sb);
32419 if (ret == NULL)
32420 return ret;
32421 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
32422 ret = make_node (OMP_TEAMS);
32423 TREE_TYPE (ret) = void_type_node;
32424 OMP_TEAMS_CLAUSES (ret) = clauses;
32425 OMP_TEAMS_BODY (ret) = body;
32426 return add_stmt (ret);
32429 if (!flag_openmp) /* flag_openmp_simd */
32431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32432 return NULL_TREE;
32435 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32436 cclauses == NULL);
32437 if (cclauses)
32439 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
32440 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
32443 tree stmt = make_node (OMP_TEAMS);
32444 TREE_TYPE (stmt) = void_type_node;
32445 OMP_TEAMS_CLAUSES (stmt) = clauses;
32446 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
32448 return add_stmt (stmt);
32451 /* OpenMP 4.0:
32452 # pragma omp target data target-data-clause[optseq] new-line
32453 structured-block */
32455 #define OMP_TARGET_DATA_CLAUSE_MASK \
32456 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32460 static tree
32461 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
32463 tree stmt = make_node (OMP_TARGET_DATA);
32464 TREE_TYPE (stmt) = void_type_node;
32466 OMP_TARGET_DATA_CLAUSES (stmt)
32467 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
32468 "#pragma omp target data", pragma_tok);
32469 keep_next_level (true);
32470 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
32472 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32473 return add_stmt (stmt);
32476 /* OpenMP 4.0:
32477 # pragma omp target update target-update-clause[optseq] new-line */
32479 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
32480 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
32481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
32482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32485 static bool
32486 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
32487 enum pragma_context context)
32489 if (context == pragma_stmt)
32491 error_at (pragma_tok->location,
32492 "%<#pragma omp target update%> may only be "
32493 "used in compound statements");
32494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32495 return false;
32498 tree clauses
32499 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
32500 "#pragma omp target update", pragma_tok);
32501 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
32502 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
32504 error_at (pragma_tok->location,
32505 "%<#pragma omp target update must contain at least one "
32506 "%<from%> or %<to%> clauses");
32507 return false;
32510 tree stmt = make_node (OMP_TARGET_UPDATE);
32511 TREE_TYPE (stmt) = void_type_node;
32512 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
32513 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32514 add_stmt (stmt);
32515 return false;
32518 /* OpenMP 4.0:
32519 # pragma omp target target-clause[optseq] new-line
32520 structured-block */
32522 #define OMP_TARGET_CLAUSE_MASK \
32523 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32527 static bool
32528 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
32529 enum pragma_context context)
32531 if (context != pragma_stmt && context != pragma_compound)
32533 cp_parser_error (parser, "expected declaration specifiers");
32534 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32535 return false;
32538 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32540 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32541 const char *p = IDENTIFIER_POINTER (id);
32543 if (strcmp (p, "teams") == 0)
32545 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
32546 char p_name[sizeof ("#pragma omp target teams distribute "
32547 "parallel for simd")];
32549 cp_lexer_consume_token (parser->lexer);
32550 strcpy (p_name, "#pragma omp target");
32551 if (!flag_openmp) /* flag_openmp_simd */
32553 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
32554 OMP_TARGET_CLAUSE_MASK,
32555 cclauses);
32556 return stmt != NULL_TREE;
32558 keep_next_level (true);
32559 tree sb = begin_omp_structured_block ();
32560 unsigned save = cp_parser_begin_omp_structured_block (parser);
32561 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
32562 OMP_TARGET_CLAUSE_MASK, cclauses);
32563 cp_parser_end_omp_structured_block (parser, save);
32564 tree body = finish_omp_structured_block (sb);
32565 if (ret == NULL_TREE)
32566 return false;
32567 tree stmt = make_node (OMP_TARGET);
32568 TREE_TYPE (stmt) = void_type_node;
32569 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
32570 OMP_TARGET_BODY (stmt) = body;
32571 add_stmt (stmt);
32572 return true;
32574 else if (!flag_openmp) /* flag_openmp_simd */
32576 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32577 return false;
32579 else if (strcmp (p, "data") == 0)
32581 cp_lexer_consume_token (parser->lexer);
32582 cp_parser_omp_target_data (parser, pragma_tok);
32583 return true;
32585 else if (strcmp (p, "update") == 0)
32587 cp_lexer_consume_token (parser->lexer);
32588 return cp_parser_omp_target_update (parser, pragma_tok, context);
32592 tree stmt = make_node (OMP_TARGET);
32593 TREE_TYPE (stmt) = void_type_node;
32595 OMP_TARGET_CLAUSES (stmt)
32596 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
32597 "#pragma omp target", pragma_tok);
32598 keep_next_level (true);
32599 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
32601 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32602 add_stmt (stmt);
32603 return true;
32606 /* OpenACC 2.0:
32607 # pragma acc cache (variable-list) new-line
32610 static tree
32611 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
32613 tree stmt, clauses;
32615 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
32616 clauses = finish_omp_clauses (clauses);
32618 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
32620 stmt = make_node (OACC_CACHE);
32621 TREE_TYPE (stmt) = void_type_node;
32622 OACC_CACHE_CLAUSES (stmt) = clauses;
32623 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32624 add_stmt (stmt);
32626 return stmt;
32629 /* OpenACC 2.0:
32630 # pragma acc data oacc-data-clause[optseq] new-line
32631 structured-block */
32633 #define OACC_DATA_CLAUSE_MASK \
32634 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
32646 static tree
32647 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
32649 tree stmt, clauses, block;
32650 unsigned int save;
32652 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
32653 "#pragma acc data", pragma_tok);
32655 block = begin_omp_parallel ();
32656 save = cp_parser_begin_omp_structured_block (parser);
32657 cp_parser_statement (parser, NULL_TREE, false, NULL);
32658 cp_parser_end_omp_structured_block (parser, save);
32659 stmt = finish_oacc_data (clauses, block);
32660 return stmt;
32663 /* OpenACC 2.0:
32664 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
32668 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
32670 LOC is the location of the #pragma token.
32673 #define OACC_ENTER_DATA_CLAUSE_MASK \
32674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32682 #define OACC_EXIT_DATA_CLAUSE_MASK \
32683 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
32687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32689 static tree
32690 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
32691 bool enter)
32693 tree stmt, clauses;
32695 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
32696 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32698 cp_parser_error (parser, enter
32699 ? "expected %<data%> in %<#pragma acc enter data%>"
32700 : "expected %<data%> in %<#pragma acc exit data%>");
32701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32702 return NULL_TREE;
32705 const char *p =
32706 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
32707 if (strcmp (p, "data") != 0)
32709 cp_parser_error (parser, "invalid pragma");
32710 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32711 return NULL_TREE;
32714 cp_lexer_consume_token (parser->lexer);
32716 if (enter)
32717 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
32718 "#pragma acc enter data", pragma_tok);
32719 else
32720 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
32721 "#pragma acc exit data", pragma_tok);
32723 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32725 error_at (pragma_tok->location,
32726 "%<#pragma acc enter data%> has no data movement clause");
32727 return NULL_TREE;
32730 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
32731 TREE_TYPE (stmt) = void_type_node;
32732 if (enter)
32733 OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
32734 else
32735 OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
32736 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32737 add_stmt (stmt);
32738 return stmt;
32741 /* OpenACC 2.0:
32742 # pragma acc kernels oacc-kernels-clause[optseq] new-line
32743 structured-block */
32745 #define OACC_KERNELS_CLAUSE_MASK \
32746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32760 static tree
32761 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
32763 tree stmt, clauses, block;
32764 unsigned int save;
32766 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
32767 "#pragma acc kernels", pragma_tok);
32769 block = begin_omp_parallel ();
32770 save = cp_parser_begin_omp_structured_block (parser);
32771 cp_parser_statement (parser, NULL_TREE, false, NULL);
32772 cp_parser_end_omp_structured_block (parser, save);
32773 stmt = finish_oacc_kernels (clauses, block);
32774 return stmt;
32777 /* OpenACC 2.0:
32778 # pragma acc loop oacc-loop-clause[optseq] new-line
32779 structured-block */
32781 #define OACC_LOOP_CLAUSE_MASK \
32782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
32783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
32785 static tree
32786 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
32788 tree stmt, clauses, block;
32789 int save;
32791 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
32792 "#pragma acc loop", pragma_tok);
32794 block = begin_omp_structured_block ();
32795 save = cp_parser_begin_omp_structured_block (parser);
32796 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
32797 cp_parser_end_omp_structured_block (parser, save);
32798 add_stmt (finish_omp_structured_block (block));
32799 return stmt;
32802 /* OpenACC 2.0:
32803 # pragma acc parallel oacc-parallel-clause[optseq] new-line
32804 structured-block */
32806 #define OACC_PARALLEL_CLAUSE_MASK \
32807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
32815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
32816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
32822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
32823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32825 static tree
32826 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
32828 tree stmt, clauses, block;
32829 unsigned int save;
32831 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
32832 "#pragma acc parallel", pragma_tok);
32834 block = begin_omp_parallel ();
32835 save = cp_parser_begin_omp_structured_block (parser);
32836 cp_parser_statement (parser, NULL_TREE, false, NULL);
32837 cp_parser_end_omp_structured_block (parser, save);
32838 stmt = finish_oacc_parallel (clauses, block);
32839 return stmt;
32842 /* OpenACC 2.0:
32843 # pragma acc update oacc-update-clause[optseq] new-line
32846 #define OACC_UPDATE_CLAUSE_MASK \
32847 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
32849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
32850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
32852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32854 static tree
32855 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
32857 tree stmt, clauses;
32859 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
32860 "#pragma acc update", pragma_tok);
32862 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32864 error_at (pragma_tok->location,
32865 "%<#pragma acc update%> must contain at least one "
32866 "%<device%> or %<host/self%> clause");
32867 return NULL_TREE;
32870 stmt = make_node (OACC_UPDATE);
32871 TREE_TYPE (stmt) = void_type_node;
32872 OACC_UPDATE_CLAUSES (stmt) = clauses;
32873 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32874 add_stmt (stmt);
32875 return stmt;
32878 /* OpenACC 2.0:
32879 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
32881 LOC is the location of the #pragma token.
32884 #define OACC_WAIT_CLAUSE_MASK \
32885 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
32887 static tree
32888 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
32890 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
32891 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32893 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32894 list = cp_parser_oacc_wait_list (parser, loc, list);
32896 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
32897 "#pragma acc wait", pragma_tok);
32899 stmt = c_finish_oacc_wait (loc, list, clauses);
32901 return stmt;
32904 /* OpenMP 4.0:
32905 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
32907 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
32908 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
32909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
32910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
32911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
32912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
32913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
32915 static void
32916 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
32917 enum pragma_context context)
32919 bool first_p = parser->omp_declare_simd == NULL;
32920 cp_omp_declare_simd_data data;
32921 if (first_p)
32923 data.error_seen = false;
32924 data.fndecl_seen = false;
32925 data.tokens = vNULL;
32926 parser->omp_declare_simd = &data;
32928 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32929 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32930 cp_lexer_consume_token (parser->lexer);
32931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32932 parser->omp_declare_simd->error_seen = true;
32933 cp_parser_require_pragma_eol (parser, pragma_tok);
32934 struct cp_token_cache *cp
32935 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
32936 parser->omp_declare_simd->tokens.safe_push (cp);
32937 if (first_p)
32939 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
32940 cp_parser_pragma (parser, context);
32941 switch (context)
32943 case pragma_external:
32944 cp_parser_declaration (parser);
32945 break;
32946 case pragma_member:
32947 cp_parser_member_declaration (parser);
32948 break;
32949 case pragma_objc_icode:
32950 cp_parser_block_declaration (parser, /*statement_p=*/false);
32951 break;
32952 default:
32953 cp_parser_declaration_statement (parser);
32954 break;
32956 if (parser->omp_declare_simd
32957 && !parser->omp_declare_simd->error_seen
32958 && !parser->omp_declare_simd->fndecl_seen)
32959 error_at (pragma_tok->location,
32960 "%<#pragma omp declare simd%> not immediately followed by "
32961 "function declaration or definition");
32962 data.tokens.release ();
32963 parser->omp_declare_simd = NULL;
32967 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
32968 This function is modelled similar to the late parsing of omp declare
32969 simd. */
32971 static tree
32972 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
32974 struct cp_token_cache *ce;
32975 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
32976 int ii = 0;
32978 if (parser->omp_declare_simd != NULL)
32980 error ("%<#pragma omp declare simd%> cannot be used in the same function"
32981 " marked as a Cilk Plus SIMD-enabled function");
32982 XDELETE (parser->cilk_simd_fn_info);
32983 parser->cilk_simd_fn_info = NULL;
32984 return attrs;
32986 if (!info->error_seen && info->fndecl_seen)
32988 error ("vector attribute not immediately followed by a single function"
32989 " declaration or definition");
32990 info->error_seen = true;
32992 if (info->error_seen)
32993 return attrs;
32995 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
32997 tree c, cl;
32999 cp_parser_push_lexer_for_tokens (parser, ce);
33000 parser->lexer->in_pragma = true;
33001 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
33002 "SIMD-enabled functions attribute",
33003 NULL);
33004 cp_parser_pop_lexer (parser);
33005 if (cl)
33006 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
33008 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
33009 TREE_CHAIN (c) = attrs;
33010 attrs = c;
33012 c = build_tree_list (get_identifier ("omp declare simd"), cl);
33013 TREE_CHAIN (c) = attrs;
33014 if (processing_template_decl)
33015 ATTR_IS_DEPENDENT (c) = 1;
33016 attrs = c;
33018 info->fndecl_seen = true;
33019 XDELETE (parser->cilk_simd_fn_info);
33020 parser->cilk_simd_fn_info = NULL;
33021 return attrs;
33024 /* Finalize #pragma omp declare simd clauses after direct declarator has
33025 been parsed, and put that into "omp declare simd" attribute. */
33027 static tree
33028 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
33030 struct cp_token_cache *ce;
33031 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
33032 int i;
33034 if (!data->error_seen && data->fndecl_seen)
33036 error ("%<#pragma omp declare simd%> not immediately followed by "
33037 "a single function declaration or definition");
33038 data->error_seen = true;
33039 return attrs;
33041 if (data->error_seen)
33042 return attrs;
33044 FOR_EACH_VEC_ELT (data->tokens, i, ce)
33046 tree c, cl;
33048 cp_parser_push_lexer_for_tokens (parser, ce);
33049 parser->lexer->in_pragma = true;
33050 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
33051 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
33052 cp_lexer_consume_token (parser->lexer);
33053 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
33054 "#pragma omp declare simd", pragma_tok);
33055 cp_parser_pop_lexer (parser);
33056 if (cl)
33057 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
33058 c = build_tree_list (get_identifier ("omp declare simd"), cl);
33059 TREE_CHAIN (c) = attrs;
33060 if (processing_template_decl)
33061 ATTR_IS_DEPENDENT (c) = 1;
33062 attrs = c;
33065 data->fndecl_seen = true;
33066 return attrs;
33070 /* OpenMP 4.0:
33071 # pragma omp declare target new-line
33072 declarations and definitions
33073 # pragma omp end declare target new-line */
33075 static void
33076 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
33078 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33079 scope_chain->omp_declare_target_attribute++;
33082 static void
33083 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
33085 const char *p = "";
33086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33089 p = IDENTIFIER_POINTER (id);
33091 if (strcmp (p, "declare") == 0)
33093 cp_lexer_consume_token (parser->lexer);
33094 p = "";
33095 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33097 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33098 p = IDENTIFIER_POINTER (id);
33100 if (strcmp (p, "target") == 0)
33101 cp_lexer_consume_token (parser->lexer);
33102 else
33104 cp_parser_error (parser, "expected %<target%>");
33105 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33106 return;
33109 else
33111 cp_parser_error (parser, "expected %<declare%>");
33112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33113 return;
33115 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33116 if (!scope_chain->omp_declare_target_attribute)
33117 error_at (pragma_tok->location,
33118 "%<#pragma omp end declare target%> without corresponding "
33119 "%<#pragma omp declare target%>");
33120 else
33121 scope_chain->omp_declare_target_attribute--;
33124 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
33125 expression and optional initializer clause of
33126 #pragma omp declare reduction. We store the expression(s) as
33127 either 3, 6 or 7 special statements inside of the artificial function's
33128 body. The first two statements are DECL_EXPRs for the artificial
33129 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
33130 expression that uses those variables.
33131 If there was any INITIALIZER clause, this is followed by further statements,
33132 the fourth and fifth statements are DECL_EXPRs for the artificial
33133 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
33134 constructor variant (first token after open paren is not omp_priv),
33135 then the sixth statement is a statement with the function call expression
33136 that uses the OMP_PRIV and optionally OMP_ORIG variable.
33137 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
33138 to initialize the OMP_PRIV artificial variable and there is seventh
33139 statement, a DECL_EXPR of the OMP_PRIV statement again. */
33141 static bool
33142 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
33144 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
33145 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
33146 type = TREE_TYPE (type);
33147 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
33148 DECL_ARTIFICIAL (omp_out) = 1;
33149 pushdecl (omp_out);
33150 add_decl_expr (omp_out);
33151 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
33152 DECL_ARTIFICIAL (omp_in) = 1;
33153 pushdecl (omp_in);
33154 add_decl_expr (omp_in);
33155 tree combiner;
33156 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
33158 keep_next_level (true);
33159 tree block = begin_omp_structured_block ();
33160 combiner = cp_parser_expression (parser);
33161 finish_expr_stmt (combiner);
33162 block = finish_omp_structured_block (block);
33163 add_stmt (block);
33165 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33166 return false;
33168 const char *p = "";
33169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33171 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33172 p = IDENTIFIER_POINTER (id);
33175 if (strcmp (p, "initializer") == 0)
33177 cp_lexer_consume_token (parser->lexer);
33178 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33179 return false;
33181 p = "";
33182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33184 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33185 p = IDENTIFIER_POINTER (id);
33188 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
33189 DECL_ARTIFICIAL (omp_priv) = 1;
33190 pushdecl (omp_priv);
33191 add_decl_expr (omp_priv);
33192 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
33193 DECL_ARTIFICIAL (omp_orig) = 1;
33194 pushdecl (omp_orig);
33195 add_decl_expr (omp_orig);
33197 keep_next_level (true);
33198 block = begin_omp_structured_block ();
33200 bool ctor = false;
33201 if (strcmp (p, "omp_priv") == 0)
33203 bool is_direct_init, is_non_constant_init;
33204 ctor = true;
33205 cp_lexer_consume_token (parser->lexer);
33206 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
33207 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
33208 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33209 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
33210 == CPP_CLOSE_PAREN
33211 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
33212 == CPP_CLOSE_PAREN))
33214 finish_omp_structured_block (block);
33215 error ("invalid initializer clause");
33216 return false;
33218 initializer = cp_parser_initializer (parser, &is_direct_init,
33219 &is_non_constant_init);
33220 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
33221 NULL_TREE, LOOKUP_ONLYCONVERTING);
33223 else
33225 cp_parser_parse_tentatively (parser);
33226 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
33227 /*check_dependency_p=*/true,
33228 /*template_p=*/NULL,
33229 /*declarator_p=*/false,
33230 /*optional_p=*/false);
33231 vec<tree, va_gc> *args;
33232 if (fn_name == error_mark_node
33233 || cp_parser_error_occurred (parser)
33234 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33235 || ((args = cp_parser_parenthesized_expression_list
33236 (parser, non_attr, /*cast_p=*/false,
33237 /*allow_expansion_p=*/true,
33238 /*non_constant_p=*/NULL)),
33239 cp_parser_error_occurred (parser)))
33241 finish_omp_structured_block (block);
33242 cp_parser_abort_tentative_parse (parser);
33243 cp_parser_error (parser, "expected id-expression (arguments)");
33244 return false;
33246 unsigned int i;
33247 tree arg;
33248 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
33249 if (arg == omp_priv
33250 || (TREE_CODE (arg) == ADDR_EXPR
33251 && TREE_OPERAND (arg, 0) == omp_priv))
33252 break;
33253 cp_parser_abort_tentative_parse (parser);
33254 if (arg == NULL_TREE)
33255 error ("one of the initializer call arguments should be %<omp_priv%>"
33256 " or %<&omp_priv%>");
33257 initializer = cp_parser_postfix_expression (parser, false, false, false,
33258 false, NULL);
33259 finish_expr_stmt (initializer);
33262 block = finish_omp_structured_block (block);
33263 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
33264 add_stmt (block);
33266 if (ctor)
33267 add_decl_expr (omp_orig);
33269 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33270 return false;
33273 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
33274 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
33276 return true;
33279 /* OpenMP 4.0
33280 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33281 initializer-clause[opt] new-line
33283 initializer-clause:
33284 initializer (omp_priv initializer)
33285 initializer (function-name (argument-list)) */
33287 static void
33288 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
33289 enum pragma_context)
33291 auto_vec<tree> types;
33292 enum tree_code reduc_code = ERROR_MARK;
33293 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
33294 unsigned int i;
33295 cp_token *first_token;
33296 cp_token_cache *cp;
33297 int errs;
33298 void *p;
33300 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
33301 p = obstack_alloc (&declarator_obstack, 0);
33303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33304 goto fail;
33306 switch (cp_lexer_peek_token (parser->lexer)->type)
33308 case CPP_PLUS:
33309 reduc_code = PLUS_EXPR;
33310 break;
33311 case CPP_MULT:
33312 reduc_code = MULT_EXPR;
33313 break;
33314 case CPP_MINUS:
33315 reduc_code = MINUS_EXPR;
33316 break;
33317 case CPP_AND:
33318 reduc_code = BIT_AND_EXPR;
33319 break;
33320 case CPP_XOR:
33321 reduc_code = BIT_XOR_EXPR;
33322 break;
33323 case CPP_OR:
33324 reduc_code = BIT_IOR_EXPR;
33325 break;
33326 case CPP_AND_AND:
33327 reduc_code = TRUTH_ANDIF_EXPR;
33328 break;
33329 case CPP_OR_OR:
33330 reduc_code = TRUTH_ORIF_EXPR;
33331 break;
33332 case CPP_NAME:
33333 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
33334 break;
33335 default:
33336 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
33337 "%<|%>, %<&&%>, %<||%> or identifier");
33338 goto fail;
33341 if (reduc_code != ERROR_MARK)
33342 cp_lexer_consume_token (parser->lexer);
33344 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
33345 if (reduc_id == error_mark_node)
33346 goto fail;
33348 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33349 goto fail;
33351 /* Types may not be defined in declare reduction type list. */
33352 const char *saved_message;
33353 saved_message = parser->type_definition_forbidden_message;
33354 parser->type_definition_forbidden_message
33355 = G_("types may not be defined in declare reduction type list");
33356 bool saved_colon_corrects_to_scope_p;
33357 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33358 parser->colon_corrects_to_scope_p = false;
33359 bool saved_colon_doesnt_start_class_def_p;
33360 saved_colon_doesnt_start_class_def_p
33361 = parser->colon_doesnt_start_class_def_p;
33362 parser->colon_doesnt_start_class_def_p = true;
33364 while (true)
33366 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33367 type = cp_parser_type_id (parser);
33368 if (type == error_mark_node)
33370 else if (ARITHMETIC_TYPE_P (type)
33371 && (orig_reduc_id == NULL_TREE
33372 || (TREE_CODE (type) != COMPLEX_TYPE
33373 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33374 "min") == 0
33375 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33376 "max") == 0))))
33377 error_at (loc, "predeclared arithmetic type %qT in "
33378 "%<#pragma omp declare reduction%>", type);
33379 else if (TREE_CODE (type) == FUNCTION_TYPE
33380 || TREE_CODE (type) == METHOD_TYPE
33381 || TREE_CODE (type) == ARRAY_TYPE)
33382 error_at (loc, "function or array type %qT in "
33383 "%<#pragma omp declare reduction%>", type);
33384 else if (TREE_CODE (type) == REFERENCE_TYPE)
33385 error_at (loc, "reference type %qT in "
33386 "%<#pragma omp declare reduction%>", type);
33387 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
33388 error_at (loc, "const, volatile or __restrict qualified type %qT in "
33389 "%<#pragma omp declare reduction%>", type);
33390 else
33391 types.safe_push (type);
33393 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33394 cp_lexer_consume_token (parser->lexer);
33395 else
33396 break;
33399 /* Restore the saved message. */
33400 parser->type_definition_forbidden_message = saved_message;
33401 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33402 parser->colon_doesnt_start_class_def_p
33403 = saved_colon_doesnt_start_class_def_p;
33405 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
33406 || types.is_empty ())
33408 fail:
33409 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33410 goto done;
33413 first_token = cp_lexer_peek_token (parser->lexer);
33414 cp = NULL;
33415 errs = errorcount;
33416 FOR_EACH_VEC_ELT (types, i, type)
33418 tree fntype
33419 = build_function_type_list (void_type_node,
33420 cp_build_reference_type (type, false),
33421 NULL_TREE);
33422 tree this_reduc_id = reduc_id;
33423 if (!dependent_type_p (type))
33424 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
33425 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
33426 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
33427 DECL_ARTIFICIAL (fndecl) = 1;
33428 DECL_EXTERNAL (fndecl) = 1;
33429 DECL_DECLARED_INLINE_P (fndecl) = 1;
33430 DECL_IGNORED_P (fndecl) = 1;
33431 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
33432 DECL_ATTRIBUTES (fndecl)
33433 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
33434 DECL_ATTRIBUTES (fndecl));
33435 if (processing_template_decl)
33436 fndecl = push_template_decl (fndecl);
33437 bool block_scope = false;
33438 tree block = NULL_TREE;
33439 if (current_function_decl)
33441 block_scope = true;
33442 DECL_CONTEXT (fndecl) = global_namespace;
33443 if (!processing_template_decl)
33444 pushdecl (fndecl);
33446 else if (current_class_type)
33448 if (cp == NULL)
33450 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33451 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
33452 cp_lexer_consume_token (parser->lexer);
33453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33454 goto fail;
33455 cp = cp_token_cache_new (first_token,
33456 cp_lexer_peek_nth_token (parser->lexer,
33457 2));
33459 DECL_STATIC_FUNCTION_P (fndecl) = 1;
33460 finish_member_declaration (fndecl);
33461 DECL_PENDING_INLINE_INFO (fndecl) = cp;
33462 DECL_PENDING_INLINE_P (fndecl) = 1;
33463 vec_safe_push (unparsed_funs_with_definitions, fndecl);
33464 continue;
33466 else
33468 DECL_CONTEXT (fndecl) = current_namespace;
33469 pushdecl (fndecl);
33471 if (!block_scope)
33472 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
33473 else
33474 block = begin_omp_structured_block ();
33475 if (cp)
33477 cp_parser_push_lexer_for_tokens (parser, cp);
33478 parser->lexer->in_pragma = true;
33480 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
33482 if (!block_scope)
33483 finish_function (0);
33484 else
33485 DECL_CONTEXT (fndecl) = current_function_decl;
33486 if (cp)
33487 cp_parser_pop_lexer (parser);
33488 goto fail;
33490 if (cp)
33491 cp_parser_pop_lexer (parser);
33492 if (!block_scope)
33493 finish_function (0);
33494 else
33496 DECL_CONTEXT (fndecl) = current_function_decl;
33497 block = finish_omp_structured_block (block);
33498 if (TREE_CODE (block) == BIND_EXPR)
33499 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
33500 else if (TREE_CODE (block) == STATEMENT_LIST)
33501 DECL_SAVED_TREE (fndecl) = block;
33502 if (processing_template_decl)
33503 add_decl_expr (fndecl);
33505 cp_check_omp_declare_reduction (fndecl);
33506 if (cp == NULL && types.length () > 1)
33507 cp = cp_token_cache_new (first_token,
33508 cp_lexer_peek_nth_token (parser->lexer, 2));
33509 if (errs != errorcount)
33510 break;
33513 cp_parser_require_pragma_eol (parser, pragma_tok);
33515 done:
33516 /* Free any declarators allocated. */
33517 obstack_free (&declarator_obstack, p);
33520 /* OpenMP 4.0
33521 #pragma omp declare simd declare-simd-clauses[optseq] new-line
33522 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33523 initializer-clause[opt] new-line
33524 #pragma omp declare target new-line */
33526 static void
33527 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
33528 enum pragma_context context)
33530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33533 const char *p = IDENTIFIER_POINTER (id);
33535 if (strcmp (p, "simd") == 0)
33537 cp_lexer_consume_token (parser->lexer);
33538 cp_parser_omp_declare_simd (parser, pragma_tok,
33539 context);
33540 return;
33542 cp_ensure_no_omp_declare_simd (parser);
33543 if (strcmp (p, "reduction") == 0)
33545 cp_lexer_consume_token (parser->lexer);
33546 cp_parser_omp_declare_reduction (parser, pragma_tok,
33547 context);
33548 return;
33550 if (!flag_openmp) /* flag_openmp_simd */
33552 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33553 return;
33555 if (strcmp (p, "target") == 0)
33557 cp_lexer_consume_token (parser->lexer);
33558 cp_parser_omp_declare_target (parser, pragma_tok);
33559 return;
33562 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
33563 "or %<target%>");
33564 cp_parser_require_pragma_eol (parser, pragma_tok);
33567 /* Main entry point to OpenMP statement pragmas. */
33569 static void
33570 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
33572 tree stmt;
33573 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
33574 omp_clause_mask mask (0);
33576 switch (pragma_tok->pragma_kind)
33578 case PRAGMA_OACC_CACHE:
33579 stmt = cp_parser_oacc_cache (parser, pragma_tok);
33580 break;
33581 case PRAGMA_OACC_DATA:
33582 stmt = cp_parser_oacc_data (parser, pragma_tok);
33583 break;
33584 case PRAGMA_OACC_ENTER_DATA:
33585 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
33586 break;
33587 case PRAGMA_OACC_EXIT_DATA:
33588 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
33589 break;
33590 case PRAGMA_OACC_KERNELS:
33591 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
33592 break;
33593 case PRAGMA_OACC_LOOP:
33594 stmt = cp_parser_oacc_loop (parser, pragma_tok);
33595 break;
33596 case PRAGMA_OACC_PARALLEL:
33597 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
33598 break;
33599 case PRAGMA_OACC_UPDATE:
33600 stmt = cp_parser_oacc_update (parser, pragma_tok);
33601 break;
33602 case PRAGMA_OACC_WAIT:
33603 stmt = cp_parser_oacc_wait (parser, pragma_tok);
33604 break;
33605 case PRAGMA_OMP_ATOMIC:
33606 cp_parser_omp_atomic (parser, pragma_tok);
33607 return;
33608 case PRAGMA_OMP_CRITICAL:
33609 stmt = cp_parser_omp_critical (parser, pragma_tok);
33610 break;
33611 case PRAGMA_OMP_DISTRIBUTE:
33612 strcpy (p_name, "#pragma omp");
33613 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
33614 break;
33615 case PRAGMA_OMP_FOR:
33616 strcpy (p_name, "#pragma omp");
33617 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
33618 break;
33619 case PRAGMA_OMP_MASTER:
33620 stmt = cp_parser_omp_master (parser, pragma_tok);
33621 break;
33622 case PRAGMA_OMP_ORDERED:
33623 stmt = cp_parser_omp_ordered (parser, pragma_tok);
33624 break;
33625 case PRAGMA_OMP_PARALLEL:
33626 strcpy (p_name, "#pragma omp");
33627 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
33628 break;
33629 case PRAGMA_OMP_SECTIONS:
33630 strcpy (p_name, "#pragma omp");
33631 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
33632 break;
33633 case PRAGMA_OMP_SIMD:
33634 strcpy (p_name, "#pragma omp");
33635 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
33636 break;
33637 case PRAGMA_OMP_SINGLE:
33638 stmt = cp_parser_omp_single (parser, pragma_tok);
33639 break;
33640 case PRAGMA_OMP_TASK:
33641 stmt = cp_parser_omp_task (parser, pragma_tok);
33642 break;
33643 case PRAGMA_OMP_TASKGROUP:
33644 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
33645 break;
33646 case PRAGMA_OMP_TEAMS:
33647 strcpy (p_name, "#pragma omp");
33648 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
33649 break;
33650 default:
33651 gcc_unreachable ();
33654 if (stmt)
33655 SET_EXPR_LOCATION (stmt, pragma_tok->location);
33658 /* Transactional Memory parsing routines. */
33660 /* Parse a transaction attribute.
33662 txn-attribute:
33663 attribute
33664 [ [ identifier ] ]
33666 ??? Simplify this when C++0x bracket attributes are
33667 implemented properly. */
33669 static tree
33670 cp_parser_txn_attribute_opt (cp_parser *parser)
33672 cp_token *token;
33673 tree attr_name, attr = NULL;
33675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
33676 return cp_parser_attributes_opt (parser);
33678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
33679 return NULL_TREE;
33680 cp_lexer_consume_token (parser->lexer);
33681 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
33682 goto error1;
33684 token = cp_lexer_peek_token (parser->lexer);
33685 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
33687 token = cp_lexer_consume_token (parser->lexer);
33689 attr_name = (token->type == CPP_KEYWORD
33690 /* For keywords, use the canonical spelling,
33691 not the parsed identifier. */
33692 ? ridpointers[(int) token->keyword]
33693 : token->u.value);
33694 attr = build_tree_list (attr_name, NULL_TREE);
33696 else
33697 cp_parser_error (parser, "expected identifier");
33699 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33700 error1:
33701 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33702 return attr;
33705 /* Parse a __transaction_atomic or __transaction_relaxed statement.
33707 transaction-statement:
33708 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
33709 compound-statement
33710 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
33713 static tree
33714 cp_parser_transaction (cp_parser *parser, enum rid keyword)
33716 unsigned char old_in = parser->in_transaction;
33717 unsigned char this_in = 1, new_in;
33718 cp_token *token;
33719 tree stmt, attrs, noex;
33721 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33722 || keyword == RID_TRANSACTION_RELAXED);
33723 token = cp_parser_require_keyword (parser, keyword,
33724 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33725 : RT_TRANSACTION_RELAXED));
33726 gcc_assert (token != NULL);
33728 if (keyword == RID_TRANSACTION_RELAXED)
33729 this_in |= TM_STMT_ATTR_RELAXED;
33730 else
33732 attrs = cp_parser_txn_attribute_opt (parser);
33733 if (attrs)
33734 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
33737 /* Parse a noexcept specification. */
33738 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
33740 /* Keep track if we're in the lexical scope of an outer transaction. */
33741 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
33743 stmt = begin_transaction_stmt (token->location, NULL, this_in);
33745 parser->in_transaction = new_in;
33746 cp_parser_compound_statement (parser, NULL, false, false);
33747 parser->in_transaction = old_in;
33749 finish_transaction_stmt (stmt, NULL, this_in, noex);
33751 return stmt;
33754 /* Parse a __transaction_atomic or __transaction_relaxed expression.
33756 transaction-expression:
33757 __transaction_atomic txn-noexcept-spec[opt] ( expression )
33758 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
33761 static tree
33762 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
33764 unsigned char old_in = parser->in_transaction;
33765 unsigned char this_in = 1;
33766 cp_token *token;
33767 tree expr, noex;
33768 bool noex_expr;
33770 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33771 || keyword == RID_TRANSACTION_RELAXED);
33773 if (!flag_tm)
33774 error (keyword == RID_TRANSACTION_RELAXED
33775 ? G_("%<__transaction_relaxed%> without transactional memory "
33776 "support enabled")
33777 : G_("%<__transaction_atomic%> without transactional memory "
33778 "support enabled"));
33780 token = cp_parser_require_keyword (parser, keyword,
33781 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33782 : RT_TRANSACTION_RELAXED));
33783 gcc_assert (token != NULL);
33785 if (keyword == RID_TRANSACTION_RELAXED)
33786 this_in |= TM_STMT_ATTR_RELAXED;
33788 /* Set this early. This might mean that we allow transaction_cancel in
33789 an expression that we find out later actually has to be a constexpr.
33790 However, we expect that cxx_constant_value will be able to deal with
33791 this; also, if the noexcept has no constexpr, then what we parse next
33792 really is a transaction's body. */
33793 parser->in_transaction = this_in;
33795 /* Parse a noexcept specification. */
33796 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
33797 true);
33799 if (!noex || !noex_expr
33800 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33802 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
33804 expr = cp_parser_expression (parser);
33805 expr = finish_parenthesized_expr (expr);
33807 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
33809 else
33811 /* The only expression that is available got parsed for the noexcept
33812 already. noexcept is true then. */
33813 expr = noex;
33814 noex = boolean_true_node;
33817 expr = build_transaction_expr (token->location, expr, this_in, noex);
33818 parser->in_transaction = old_in;
33820 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
33821 return error_mark_node;
33823 return (flag_tm ? expr : error_mark_node);
33826 /* Parse a function-transaction-block.
33828 function-transaction-block:
33829 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
33830 function-body
33831 __transaction_atomic txn-attribute[opt] function-try-block
33832 __transaction_relaxed ctor-initializer[opt] function-body
33833 __transaction_relaxed function-try-block
33836 static bool
33837 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
33839 unsigned char old_in = parser->in_transaction;
33840 unsigned char new_in = 1;
33841 tree compound_stmt, stmt, attrs;
33842 bool ctor_initializer_p;
33843 cp_token *token;
33845 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33846 || keyword == RID_TRANSACTION_RELAXED);
33847 token = cp_parser_require_keyword (parser, keyword,
33848 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33849 : RT_TRANSACTION_RELAXED));
33850 gcc_assert (token != NULL);
33852 if (keyword == RID_TRANSACTION_RELAXED)
33853 new_in |= TM_STMT_ATTR_RELAXED;
33854 else
33856 attrs = cp_parser_txn_attribute_opt (parser);
33857 if (attrs)
33858 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
33861 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
33863 parser->in_transaction = new_in;
33865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33866 ctor_initializer_p = cp_parser_function_try_block (parser);
33867 else
33868 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
33869 (parser, /*in_function_try_block=*/false);
33871 parser->in_transaction = old_in;
33873 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
33875 return ctor_initializer_p;
33878 /* Parse a __transaction_cancel statement.
33880 cancel-statement:
33881 __transaction_cancel txn-attribute[opt] ;
33882 __transaction_cancel txn-attribute[opt] throw-expression ;
33884 ??? Cancel and throw is not yet implemented. */
33886 static tree
33887 cp_parser_transaction_cancel (cp_parser *parser)
33889 cp_token *token;
33890 bool is_outer = false;
33891 tree stmt, attrs;
33893 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
33894 RT_TRANSACTION_CANCEL);
33895 gcc_assert (token != NULL);
33897 attrs = cp_parser_txn_attribute_opt (parser);
33898 if (attrs)
33899 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
33901 /* ??? Parse cancel-and-throw here. */
33903 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33905 if (!flag_tm)
33907 error_at (token->location, "%<__transaction_cancel%> without "
33908 "transactional memory support enabled");
33909 return error_mark_node;
33911 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
33913 error_at (token->location, "%<__transaction_cancel%> within a "
33914 "%<__transaction_relaxed%>");
33915 return error_mark_node;
33917 else if (is_outer)
33919 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
33920 && !is_tm_may_cancel_outer (current_function_decl))
33922 error_at (token->location, "outer %<__transaction_cancel%> not "
33923 "within outer %<__transaction_atomic%>");
33924 error_at (token->location,
33925 " or a %<transaction_may_cancel_outer%> function");
33926 return error_mark_node;
33929 else if (parser->in_transaction == 0)
33931 error_at (token->location, "%<__transaction_cancel%> not within "
33932 "%<__transaction_atomic%>");
33933 return error_mark_node;
33936 stmt = build_tm_abort_call (token->location, is_outer);
33937 add_stmt (stmt);
33939 return stmt;
33942 /* The parser. */
33944 static GTY (()) cp_parser *the_parser;
33947 /* Special handling for the first token or line in the file. The first
33948 thing in the file might be #pragma GCC pch_preprocess, which loads a
33949 PCH file, which is a GC collection point. So we need to handle this
33950 first pragma without benefit of an existing lexer structure.
33952 Always returns one token to the caller in *FIRST_TOKEN. This is
33953 either the true first token of the file, or the first token after
33954 the initial pragma. */
33956 static void
33957 cp_parser_initial_pragma (cp_token *first_token)
33959 tree name = NULL;
33961 cp_lexer_get_preprocessor_token (NULL, first_token);
33962 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
33963 return;
33965 cp_lexer_get_preprocessor_token (NULL, first_token);
33966 if (first_token->type == CPP_STRING)
33968 name = first_token->u.value;
33970 cp_lexer_get_preprocessor_token (NULL, first_token);
33971 if (first_token->type != CPP_PRAGMA_EOL)
33972 error_at (first_token->location,
33973 "junk at end of %<#pragma GCC pch_preprocess%>");
33975 else
33976 error_at (first_token->location, "expected string literal");
33978 /* Skip to the end of the pragma. */
33979 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
33980 cp_lexer_get_preprocessor_token (NULL, first_token);
33982 /* Now actually load the PCH file. */
33983 if (name)
33984 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
33986 /* Read one more token to return to our caller. We have to do this
33987 after reading the PCH file in, since its pointers have to be
33988 live. */
33989 cp_lexer_get_preprocessor_token (NULL, first_token);
33992 /* Parses the grainsize pragma for the _Cilk_for statement.
33993 Syntax:
33994 #pragma cilk grainsize = <VALUE>. */
33996 static void
33997 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
33999 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
34001 tree exp = cp_parser_binary_expression (parser, false, false,
34002 PREC_NOT_OPERATOR, NULL);
34003 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34004 if (!exp || exp == error_mark_node)
34006 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
34007 return;
34010 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
34011 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34012 cp_parser_cilk_for (parser, exp);
34013 else
34014 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
34015 "%<#pragma cilk grainsize%> is not followed by "
34016 "%<_Cilk_for%>");
34017 return;
34019 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34022 /* Normal parsing of a pragma token. Here we can (and must) use the
34023 regular lexer. */
34025 static bool
34026 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
34028 cp_token *pragma_tok;
34029 unsigned int id;
34031 pragma_tok = cp_lexer_consume_token (parser->lexer);
34032 gcc_assert (pragma_tok->type == CPP_PRAGMA);
34033 parser->lexer->in_pragma = true;
34035 id = pragma_tok->pragma_kind;
34036 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
34037 cp_ensure_no_omp_declare_simd (parser);
34038 switch (id)
34040 case PRAGMA_GCC_PCH_PREPROCESS:
34041 error_at (pragma_tok->location,
34042 "%<#pragma GCC pch_preprocess%> must be first");
34043 break;
34045 case PRAGMA_OMP_BARRIER:
34046 switch (context)
34048 case pragma_compound:
34049 cp_parser_omp_barrier (parser, pragma_tok);
34050 return false;
34051 case pragma_stmt:
34052 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
34053 "used in compound statements");
34054 break;
34055 default:
34056 goto bad_stmt;
34058 break;
34060 case PRAGMA_OMP_FLUSH:
34061 switch (context)
34063 case pragma_compound:
34064 cp_parser_omp_flush (parser, pragma_tok);
34065 return false;
34066 case pragma_stmt:
34067 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
34068 "used in compound statements");
34069 break;
34070 default:
34071 goto bad_stmt;
34073 break;
34075 case PRAGMA_OMP_TASKWAIT:
34076 switch (context)
34078 case pragma_compound:
34079 cp_parser_omp_taskwait (parser, pragma_tok);
34080 return false;
34081 case pragma_stmt:
34082 error_at (pragma_tok->location,
34083 "%<#pragma omp taskwait%> may only be "
34084 "used in compound statements");
34085 break;
34086 default:
34087 goto bad_stmt;
34089 break;
34091 case PRAGMA_OMP_TASKYIELD:
34092 switch (context)
34094 case pragma_compound:
34095 cp_parser_omp_taskyield (parser, pragma_tok);
34096 return false;
34097 case pragma_stmt:
34098 error_at (pragma_tok->location,
34099 "%<#pragma omp taskyield%> may only be "
34100 "used in compound statements");
34101 break;
34102 default:
34103 goto bad_stmt;
34105 break;
34107 case PRAGMA_OMP_CANCEL:
34108 switch (context)
34110 case pragma_compound:
34111 cp_parser_omp_cancel (parser, pragma_tok);
34112 return false;
34113 case pragma_stmt:
34114 error_at (pragma_tok->location,
34115 "%<#pragma omp cancel%> may only be "
34116 "used in compound statements");
34117 break;
34118 default:
34119 goto bad_stmt;
34121 break;
34123 case PRAGMA_OMP_CANCELLATION_POINT:
34124 switch (context)
34126 case pragma_compound:
34127 cp_parser_omp_cancellation_point (parser, pragma_tok);
34128 return false;
34129 case pragma_stmt:
34130 error_at (pragma_tok->location,
34131 "%<#pragma omp cancellation point%> may only be "
34132 "used in compound statements");
34133 break;
34134 default:
34135 goto bad_stmt;
34137 break;
34139 case PRAGMA_OMP_THREADPRIVATE:
34140 cp_parser_omp_threadprivate (parser, pragma_tok);
34141 return false;
34143 case PRAGMA_OMP_DECLARE_REDUCTION:
34144 cp_parser_omp_declare (parser, pragma_tok, context);
34145 return false;
34147 case PRAGMA_OACC_CACHE:
34148 case PRAGMA_OACC_DATA:
34149 case PRAGMA_OACC_ENTER_DATA:
34150 case PRAGMA_OACC_EXIT_DATA:
34151 case PRAGMA_OACC_KERNELS:
34152 case PRAGMA_OACC_PARALLEL:
34153 case PRAGMA_OACC_LOOP:
34154 case PRAGMA_OACC_UPDATE:
34155 case PRAGMA_OACC_WAIT:
34156 case PRAGMA_OMP_ATOMIC:
34157 case PRAGMA_OMP_CRITICAL:
34158 case PRAGMA_OMP_DISTRIBUTE:
34159 case PRAGMA_OMP_FOR:
34160 case PRAGMA_OMP_MASTER:
34161 case PRAGMA_OMP_ORDERED:
34162 case PRAGMA_OMP_PARALLEL:
34163 case PRAGMA_OMP_SECTIONS:
34164 case PRAGMA_OMP_SIMD:
34165 case PRAGMA_OMP_SINGLE:
34166 case PRAGMA_OMP_TASK:
34167 case PRAGMA_OMP_TASKGROUP:
34168 case PRAGMA_OMP_TEAMS:
34169 if (context != pragma_stmt && context != pragma_compound)
34170 goto bad_stmt;
34171 cp_parser_omp_construct (parser, pragma_tok);
34172 return true;
34174 case PRAGMA_OMP_TARGET:
34175 return cp_parser_omp_target (parser, pragma_tok, context);
34177 case PRAGMA_OMP_END_DECLARE_TARGET:
34178 cp_parser_omp_end_declare_target (parser, pragma_tok);
34179 return false;
34181 case PRAGMA_OMP_SECTION:
34182 error_at (pragma_tok->location,
34183 "%<#pragma omp section%> may only be used in "
34184 "%<#pragma omp sections%> construct");
34185 break;
34187 case PRAGMA_IVDEP:
34189 if (context == pragma_external)
34191 error_at (pragma_tok->location,
34192 "%<#pragma GCC ivdep%> must be inside a function");
34193 break;
34195 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34196 cp_token *tok;
34197 tok = cp_lexer_peek_token (the_parser->lexer);
34198 if (tok->type != CPP_KEYWORD
34199 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
34200 && tok->keyword != RID_DO))
34202 cp_parser_error (parser, "for, while or do statement expected");
34203 return false;
34205 cp_parser_iteration_statement (parser, true);
34206 return true;
34209 case PRAGMA_CILK_SIMD:
34210 if (context == pragma_external)
34212 error_at (pragma_tok->location,
34213 "%<#pragma simd%> must be inside a function");
34214 break;
34216 cp_parser_cilk_simd (parser, pragma_tok);
34217 return true;
34219 case PRAGMA_CILK_GRAINSIZE:
34220 if (context == pragma_external)
34222 error_at (pragma_tok->location,
34223 "%<#pragma cilk grainsize%> must be inside a function");
34224 break;
34227 /* Ignore the pragma if Cilk Plus is not enabled. */
34228 if (flag_cilkplus)
34230 cp_parser_cilk_grainsize (parser, pragma_tok);
34231 return true;
34233 else
34235 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
34236 "%<#pragma cilk grainsize%>");
34237 break;
34240 default:
34241 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
34242 c_invoke_pragma_handler (id);
34243 break;
34245 bad_stmt:
34246 cp_parser_error (parser, "expected declaration specifiers");
34247 break;
34250 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34251 return false;
34254 /* The interface the pragma parsers have to the lexer. */
34256 enum cpp_ttype
34257 pragma_lex (tree *value)
34259 cp_token *tok;
34260 enum cpp_ttype ret;
34262 tok = cp_lexer_peek_token (the_parser->lexer);
34264 ret = tok->type;
34265 *value = tok->u.value;
34267 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
34268 ret = CPP_EOF;
34269 else if (ret == CPP_STRING)
34270 *value = cp_parser_string_literal (the_parser, false, false);
34271 else
34273 cp_lexer_consume_token (the_parser->lexer);
34274 if (ret == CPP_KEYWORD)
34275 ret = CPP_NAME;
34278 return ret;
34282 /* External interface. */
34284 /* Parse one entire translation unit. */
34286 void
34287 c_parse_file (void)
34289 static bool already_called = false;
34291 if (already_called)
34292 fatal_error (input_location,
34293 "inter-module optimizations not implemented for C++");
34294 already_called = true;
34296 the_parser = cp_parser_new ();
34297 push_deferring_access_checks (flag_access_control
34298 ? dk_no_deferred : dk_no_check);
34299 cp_parser_translation_unit (the_parser);
34300 the_parser = NULL;
34303 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
34304 vectorlength clause:
34305 Syntax:
34306 vectorlength ( constant-expression ) */
34308 static tree
34309 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
34310 bool is_simd_fn)
34312 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34313 tree expr;
34314 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
34315 safelen clause. Thus, vectorlength is represented as OMP 4.0
34316 safelen. For SIMD-enabled function it is represented by OMP 4.0
34317 simdlen. */
34318 if (!is_simd_fn)
34319 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
34320 loc);
34321 else
34322 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
34323 loc);
34325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34326 return error_mark_node;
34328 expr = cp_parser_constant_expression (parser);
34329 expr = maybe_constant_value (expr);
34331 /* If expr == error_mark_node, then don't emit any errors nor
34332 create a clause. if any of the above functions returns
34333 error mark node then they would have emitted an error message. */
34334 if (expr == error_mark_node)
34336 else if (!TREE_TYPE (expr)
34337 || !TREE_CONSTANT (expr)
34338 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
34339 error_at (loc, "vectorlength must be an integer constant");
34340 else if (TREE_CONSTANT (expr)
34341 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
34342 error_at (loc, "vectorlength must be a power of 2");
34343 else
34345 tree c;
34346 if (!is_simd_fn)
34348 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
34349 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
34350 OMP_CLAUSE_CHAIN (c) = clauses;
34351 clauses = c;
34353 else
34355 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
34356 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
34357 OMP_CLAUSE_CHAIN (c) = clauses;
34358 clauses = c;
34362 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34363 return error_mark_node;
34364 return clauses;
34367 /* Handles the Cilk Plus #pragma simd linear clause.
34368 Syntax:
34369 linear ( simd-linear-variable-list )
34371 simd-linear-variable-list:
34372 simd-linear-variable
34373 simd-linear-variable-list , simd-linear-variable
34375 simd-linear-variable:
34376 id-expression
34377 id-expression : simd-linear-step
34379 simd-linear-step:
34380 conditional-expression */
34382 static tree
34383 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
34385 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34388 return clauses;
34389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34391 cp_parser_error (parser, "expected identifier");
34392 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34393 return error_mark_node;
34396 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34397 parser->colon_corrects_to_scope_p = false;
34398 while (1)
34400 cp_token *token = cp_lexer_peek_token (parser->lexer);
34401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34403 cp_parser_error (parser, "expected variable-name");
34404 clauses = error_mark_node;
34405 break;
34408 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
34409 false, false);
34410 tree decl = cp_parser_lookup_name_simple (parser, var_name,
34411 token->location);
34412 if (decl == error_mark_node)
34414 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
34415 token->location);
34416 clauses = error_mark_node;
34418 else
34420 tree e = NULL_TREE;
34421 tree step_size = integer_one_node;
34423 /* If present, parse the linear step. Otherwise, assume the default
34424 value of 1. */
34425 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
34427 cp_lexer_consume_token (parser->lexer);
34429 e = cp_parser_assignment_expression (parser);
34430 e = maybe_constant_value (e);
34432 if (e == error_mark_node)
34434 /* If an error has occurred, then the whole pragma is
34435 considered ill-formed. Thus, no reason to keep
34436 parsing. */
34437 clauses = error_mark_node;
34438 break;
34440 else if (type_dependent_expression_p (e)
34441 || value_dependent_expression_p (e)
34442 || (TREE_TYPE (e)
34443 && INTEGRAL_TYPE_P (TREE_TYPE (e))
34444 && (TREE_CONSTANT (e)
34445 || DECL_P (e))))
34446 step_size = e;
34447 else
34448 cp_parser_error (parser,
34449 "step size must be an integer constant "
34450 "expression or an integer variable");
34453 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
34454 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34455 OMP_CLAUSE_DECL (l) = decl;
34456 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
34457 OMP_CLAUSE_CHAIN (l) = clauses;
34458 clauses = l;
34460 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34461 cp_lexer_consume_token (parser->lexer);
34462 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34463 break;
34464 else
34466 error_at (cp_lexer_peek_token (parser->lexer)->location,
34467 "expected %<,%> or %<)%> after %qE", decl);
34468 clauses = error_mark_node;
34469 break;
34472 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34473 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34474 return clauses;
34477 /* Returns the name of the next clause. If the clause is not
34478 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
34479 token is not consumed. Otherwise, the appropriate enum from the
34480 pragma_simd_clause is returned and the token is consumed. */
34482 static pragma_omp_clause
34483 cp_parser_cilk_simd_clause_name (cp_parser *parser)
34485 pragma_omp_clause clause_type;
34486 cp_token *token = cp_lexer_peek_token (parser->lexer);
34488 if (token->keyword == RID_PRIVATE)
34489 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
34490 else if (!token->u.value || token->type != CPP_NAME)
34491 return PRAGMA_CILK_CLAUSE_NONE;
34492 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
34493 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
34494 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
34495 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
34496 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
34497 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
34498 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
34499 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
34500 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
34501 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
34502 else
34503 return PRAGMA_CILK_CLAUSE_NONE;
34505 cp_lexer_consume_token (parser->lexer);
34506 return clause_type;
34509 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
34511 static tree
34512 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
34514 tree clauses = NULL_TREE;
34516 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
34517 && clauses != error_mark_node)
34519 pragma_omp_clause c_kind;
34520 c_kind = cp_parser_cilk_simd_clause_name (parser);
34521 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
34522 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
34523 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
34524 clauses = cp_parser_cilk_simd_linear (parser, clauses);
34525 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
34526 /* Use the OpenMP 4.0 equivalent function. */
34527 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
34528 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
34529 /* Use the OpenMP 4.0 equivalent function. */
34530 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34531 clauses);
34532 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
34533 /* Use the OMP 4.0 equivalent function. */
34534 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34535 clauses);
34536 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
34537 /* Use the OMP 4.0 equivalent function. */
34538 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34539 else
34541 clauses = error_mark_node;
34542 cp_parser_error (parser, "expected %<#pragma simd%> clause");
34543 break;
34547 cp_parser_skip_to_pragma_eol (parser, pragma_token);
34549 if (clauses == error_mark_node)
34550 return error_mark_node;
34551 else
34552 return c_finish_cilk_clauses (clauses);
34555 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
34557 static void
34558 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
34560 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
34562 if (clauses == error_mark_node)
34563 return;
34565 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
34567 error_at (cp_lexer_peek_token (parser->lexer)->location,
34568 "for statement expected");
34569 return;
34572 tree sb = begin_omp_structured_block ();
34573 int save = cp_parser_begin_omp_structured_block (parser);
34574 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
34575 if (ret)
34576 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
34577 cp_parser_end_omp_structured_block (parser, save);
34578 add_stmt (finish_omp_structured_block (sb));
34581 /* Main entry-point for parsing Cilk Plus _Cilk_for
34582 loops. The return value is error_mark_node
34583 when errors happen and CILK_FOR tree on success. */
34585 static tree
34586 cp_parser_cilk_for (cp_parser *parser, tree grain)
34588 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
34589 gcc_unreachable ();
34591 tree sb = begin_omp_structured_block ();
34592 int save = cp_parser_begin_omp_structured_block (parser);
34594 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
34595 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
34596 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
34597 clauses = finish_omp_clauses (clauses);
34599 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
34600 if (ret)
34601 cpp_validate_cilk_plus_loop (ret);
34602 else
34603 ret = error_mark_node;
34605 cp_parser_end_omp_structured_block (parser, save);
34606 add_stmt (finish_omp_structured_block (sb));
34607 return ret;
34610 /* Create an identifier for a generic parameter type (a synthesized
34611 template parameter implied by `auto' or a concept identifier). */
34613 static GTY(()) int generic_parm_count;
34614 static tree
34615 make_generic_type_name ()
34617 char buf[32];
34618 sprintf (buf, "auto:%d", ++generic_parm_count);
34619 return get_identifier (buf);
34622 /* Predicate that behaves as is_auto_or_concept but matches the parent
34623 node of the generic type rather than the generic type itself. This
34624 allows for type transformation in add_implicit_template_parms. */
34626 static inline bool
34627 tree_type_is_auto_or_concept (const_tree t)
34629 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
34632 /* Returns the template declaration being called or evaluated as
34633 part of the constraint check. Note that T must be a predicate
34634 constraint (it can't be any other kind of constraint). */
34635 static tree
34636 get_concept_from_constraint (tree t)
34638 gcc_assert (TREE_CODE (t) == PRED_CONSTR);
34639 t = PRED_CONSTR_EXPR (t);
34640 gcc_assert (TREE_CODE (t) == CALL_EXPR
34641 || TREE_CODE (t) == TEMPLATE_ID_EXPR
34642 || VAR_P (t));
34644 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
34645 return DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
34646 if (VAR_P (t))
34647 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (t));
34648 else
34650 tree fn = CALL_EXPR_FN (t);
34651 tree ovl = TREE_OPERAND (fn, 0);
34652 tree tmpl = OVL_FUNCTION (ovl);
34653 return DECL_TEMPLATE_RESULT (tmpl);
34657 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
34658 (creating a new template parameter list if necessary). Returns the newly
34659 created template type parm. */
34661 tree
34662 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
34664 gcc_assert (current_binding_level->kind == sk_function_parms);
34666 /* Before committing to modifying any scope, if we're in an
34667 implicit template scope, and we're trying to synthesize a
34668 constrained parameter, try to find a previous parameter with
34669 the same name. This is the same-type rule for abbreviated
34670 function templates. */
34671 if (parser->implicit_template_scope && constr)
34673 tree t = parser->implicit_template_parms;
34674 while (t)
34676 tree c = get_concept_from_constraint (TREE_TYPE (t));
34677 if (c == DECL_SIZE_UNIT (constr))
34678 return TREE_VALUE (t);
34679 t = TREE_CHAIN (t);
34683 /* We are either continuing a function template that already contains implicit
34684 template parameters, creating a new fully-implicit function template, or
34685 extending an existing explicit function template with implicit template
34686 parameters. */
34688 cp_binding_level *const entry_scope = current_binding_level;
34690 bool become_template = false;
34691 cp_binding_level *parent_scope = 0;
34693 if (parser->implicit_template_scope)
34695 gcc_assert (parser->implicit_template_parms);
34697 current_binding_level = parser->implicit_template_scope;
34699 else
34701 /* Roll back to the existing template parameter scope (in the case of
34702 extending an explicit function template) or introduce a new template
34703 parameter scope ahead of the function parameter scope (or class scope
34704 in the case of out-of-line member definitions). The function scope is
34705 added back after template parameter synthesis below. */
34707 cp_binding_level *scope = entry_scope;
34709 while (scope->kind == sk_function_parms)
34711 parent_scope = scope;
34712 scope = scope->level_chain;
34714 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
34716 /* If not defining a class, then any class scope is a scope level in
34717 an out-of-line member definition. In this case simply wind back
34718 beyond the first such scope to inject the template parameter list.
34719 Otherwise wind back to the class being defined. The latter can
34720 occur in class member friend declarations such as:
34722 class A {
34723 void foo (auto);
34725 class B {
34726 friend void A::foo (auto);
34729 The template parameter list synthesized for the friend declaration
34730 must be injected in the scope of 'B'. This can also occur in
34731 erroneous cases such as:
34733 struct A {
34734 struct B {
34735 void foo (auto);
34737 void B::foo (auto) {}
34740 Here the attempted definition of 'B::foo' within 'A' is ill-formed
34741 but, nevertheless, the template parameter list synthesized for the
34742 declarator should be injected into the scope of 'A' as if the
34743 ill-formed template was specified explicitly. */
34745 while (scope->kind == sk_class && !scope->defining_class_p)
34747 parent_scope = scope;
34748 scope = scope->level_chain;
34752 current_binding_level = scope;
34754 if (scope->kind != sk_template_parms
34755 || !function_being_declared_is_template_p (parser))
34757 /* Introduce a new template parameter list for implicit template
34758 parameters. */
34759 become_template = true;
34761 parser->implicit_template_scope
34762 = begin_scope (sk_template_parms, NULL);
34764 ++processing_template_decl;
34766 parser->fully_implicit_function_template_p = true;
34767 ++parser->num_template_parameter_lists;
34769 else
34771 /* Synthesize implicit template parameters at the end of the explicit
34772 template parameter list. */
34774 gcc_assert (current_template_parms);
34776 parser->implicit_template_scope = scope;
34778 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
34779 parser->implicit_template_parms
34780 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
34784 /* Synthesize a new template parameter and track the current template
34785 parameter chain with implicit_template_parms. */
34787 tree synth_id = make_generic_type_name ();
34788 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
34789 synth_id);
34791 // Attach the constraint to the parm before processing.
34792 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
34793 TREE_TYPE (node) = constr;
34794 tree new_parm
34795 = process_template_parm (parser->implicit_template_parms,
34796 input_location,
34797 node,
34798 /*non_type=*/false,
34799 /*param_pack=*/false);
34801 // Chain the new parameter to the list of implicit parameters.
34802 if (parser->implicit_template_parms)
34803 parser->implicit_template_parms
34804 = TREE_CHAIN (parser->implicit_template_parms);
34805 else
34806 parser->implicit_template_parms = new_parm;
34808 tree new_type = TREE_TYPE (getdecls ());
34810 /* If creating a fully implicit function template, start the new implicit
34811 template parameter list with this synthesized type, otherwise grow the
34812 current template parameter list. */
34814 if (become_template)
34816 parent_scope->level_chain = current_binding_level;
34818 tree new_parms = make_tree_vec (1);
34819 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
34820 current_template_parms = tree_cons (size_int (processing_template_decl),
34821 new_parms, current_template_parms);
34823 else
34825 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
34826 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
34827 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
34828 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
34831 // If the new parameter was constrained, we need to add that to the
34832 // constraints in the tmeplate parameter list.
34833 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
34835 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
34836 reqs = conjoin_constraints (reqs, req);
34837 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
34840 current_binding_level = entry_scope;
34842 return new_type;
34845 /* Finish the declaration of a fully implicit function template. Such a
34846 template has no explicit template parameter list so has not been through the
34847 normal template head and tail processing. synthesize_implicit_template_parm
34848 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
34849 provided if the declaration is a class member such that its template
34850 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
34851 form is returned. Otherwise NULL_TREE is returned. */
34853 tree
34854 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
34856 gcc_assert (parser->fully_implicit_function_template_p);
34858 if (member_decl_opt && member_decl_opt != error_mark_node
34859 && DECL_VIRTUAL_P (member_decl_opt))
34861 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
34862 "implicit templates may not be %<virtual%>");
34863 DECL_VIRTUAL_P (member_decl_opt) = false;
34866 if (member_decl_opt)
34867 member_decl_opt = finish_member_template_decl (member_decl_opt);
34868 end_template_decl ();
34870 parser->fully_implicit_function_template_p = false;
34871 --parser->num_template_parameter_lists;
34873 return member_decl_opt;
34876 #include "gt-cp-parser.h"