Merge from trunk.
[official-gcc.git] / gcc / cp / parser.c
blobdb5d6682981713c302c41fb6de57d88db409a53d
1 /* C++ Parser.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "target.h"
35 #include "cgraph.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "plugin.h"
39 #include "tree-pretty-print.h"
40 #include "parser.h"
41 #include "type-utils.h"
42 #include "omp-low.h"
46 namespace {
47 // A helper function. Returns the object pointed to by P
48 // and sets P to NULL.
49 template<typename T>
50 inline T* release(T*& p)
52 T* q = p;
53 p = NULL;
54 return q;
56 } // namespace
59 /* The lexer. */
61 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
62 and c-lex.c) and the C++ parser. */
64 static cp_token eof_token =
66 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
69 /* The various kinds of non integral constant we encounter. */
70 typedef enum non_integral_constant {
71 NIC_NONE,
72 /* floating-point literal */
73 NIC_FLOAT,
74 /* %<this%> */
75 NIC_THIS,
76 /* %<__FUNCTION__%> */
77 NIC_FUNC_NAME,
78 /* %<__PRETTY_FUNCTION__%> */
79 NIC_PRETTY_FUNC,
80 /* %<__func__%> */
81 NIC_C99_FUNC,
82 /* "%<va_arg%> */
83 NIC_VA_ARG,
84 /* a cast */
85 NIC_CAST,
86 /* %<typeid%> operator */
87 NIC_TYPEID,
88 /* non-constant compound literals */
89 NIC_NCC,
90 /* a function call */
91 NIC_FUNC_CALL,
92 /* an increment */
93 NIC_INC,
94 /* an decrement */
95 NIC_DEC,
96 /* an array reference */
97 NIC_ARRAY_REF,
98 /* %<->%> */
99 NIC_ARROW,
100 /* %<.%> */
101 NIC_POINT,
102 /* the address of a label */
103 NIC_ADDR_LABEL,
104 /* %<*%> */
105 NIC_STAR,
106 /* %<&%> */
107 NIC_ADDR,
108 /* %<++%> */
109 NIC_PREINCREMENT,
110 /* %<--%> */
111 NIC_PREDECREMENT,
112 /* %<new%> */
113 NIC_NEW,
114 /* %<delete%> */
115 NIC_DEL,
116 /* calls to overloaded operators */
117 NIC_OVERLOADED,
118 /* an assignment */
119 NIC_ASSIGNMENT,
120 /* a comma operator */
121 NIC_COMMA,
122 /* a call to a constructor */
123 NIC_CONSTRUCTOR,
124 /* a transaction expression */
125 NIC_TRANSACTION
126 } non_integral_constant;
128 /* The various kinds of errors about name-lookup failing. */
129 typedef enum name_lookup_error {
130 /* NULL */
131 NLE_NULL,
132 /* is not a type */
133 NLE_TYPE,
134 /* is not a class or namespace */
135 NLE_CXX98,
136 /* is not a class, namespace, or enumeration */
137 NLE_NOT_CXX98
138 } name_lookup_error;
140 /* The various kinds of required token */
141 typedef enum required_token {
142 RT_NONE,
143 RT_SEMICOLON, /* ';' */
144 RT_OPEN_PAREN, /* '(' */
145 RT_CLOSE_BRACE, /* '}' */
146 RT_OPEN_BRACE, /* '{' */
147 RT_CLOSE_SQUARE, /* ']' */
148 RT_OPEN_SQUARE, /* '[' */
149 RT_COMMA, /* ',' */
150 RT_SCOPE, /* '::' */
151 RT_LESS, /* '<' */
152 RT_GREATER, /* '>' */
153 RT_EQ, /* '=' */
154 RT_ELLIPSIS, /* '...' */
155 RT_MULT, /* '*' */
156 RT_COMPL, /* '~' */
157 RT_COLON, /* ':' */
158 RT_COLON_SCOPE, /* ':' or '::' */
159 RT_CLOSE_PAREN, /* ')' */
160 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
161 RT_PRAGMA_EOL, /* end of line */
162 RT_NAME, /* identifier */
164 /* The type is CPP_KEYWORD */
165 RT_NEW, /* new */
166 RT_DELETE, /* delete */
167 RT_RETURN, /* return */
168 RT_WHILE, /* while */
169 RT_EXTERN, /* extern */
170 RT_STATIC_ASSERT, /* static_assert */
171 RT_DECLTYPE, /* decltype */
172 RT_OPERATOR, /* operator */
173 RT_CLASS, /* class */
174 RT_TEMPLATE, /* template */
175 RT_NAMESPACE, /* namespace */
176 RT_USING, /* using */
177 RT_ASM, /* asm */
178 RT_TRY, /* try */
179 RT_CATCH, /* catch */
180 RT_THROW, /* throw */
181 RT_LABEL, /* __label__ */
182 RT_AT_TRY, /* @try */
183 RT_AT_SYNCHRONIZED, /* @synchronized */
184 RT_AT_THROW, /* @throw */
186 RT_SELECT, /* selection-statement */
187 RT_INTERATION, /* iteration-statement */
188 RT_JUMP, /* jump-statement */
189 RT_CLASS_KEY, /* class-key */
190 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
191 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
192 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
193 RT_TRANSACTION_CANCEL /* __transaction_cancel */
194 } required_token;
196 /* Prototypes. */
198 static cp_lexer *cp_lexer_new_main
199 (void);
200 static cp_lexer *cp_lexer_new_from_tokens
201 (cp_token_cache *tokens);
202 static void cp_lexer_destroy
203 (cp_lexer *);
204 static int cp_lexer_saving_tokens
205 (const cp_lexer *);
206 static cp_token *cp_lexer_token_at
207 (cp_lexer *, cp_token_position);
208 static void cp_lexer_get_preprocessor_token
209 (cp_lexer *, cp_token *);
210 static inline cp_token *cp_lexer_peek_token
211 (cp_lexer *);
212 static cp_token *cp_lexer_peek_nth_token
213 (cp_lexer *, size_t);
214 static inline bool cp_lexer_next_token_is
215 (cp_lexer *, enum cpp_ttype);
216 static bool cp_lexer_next_token_is_not
217 (cp_lexer *, enum cpp_ttype);
218 static bool cp_lexer_next_token_is_keyword
219 (cp_lexer *, enum rid);
220 static cp_token *cp_lexer_consume_token
221 (cp_lexer *);
222 static void cp_lexer_purge_token
223 (cp_lexer *);
224 static void cp_lexer_purge_tokens_after
225 (cp_lexer *, cp_token_position);
226 static void cp_lexer_save_tokens
227 (cp_lexer *);
228 static void cp_lexer_commit_tokens
229 (cp_lexer *);
230 static void cp_lexer_rollback_tokens
231 (cp_lexer *);
232 static void cp_lexer_print_token
233 (FILE *, cp_token *);
234 static inline bool cp_lexer_debugging_p
235 (cp_lexer *);
236 static void cp_lexer_start_debugging
237 (cp_lexer *) ATTRIBUTE_UNUSED;
238 static void cp_lexer_stop_debugging
239 (cp_lexer *) ATTRIBUTE_UNUSED;
241 static cp_token_cache *cp_token_cache_new
242 (cp_token *, cp_token *);
244 static void cp_parser_initial_pragma
245 (cp_token *);
247 static tree cp_literal_operator_id
248 (const char *);
250 static void cp_parser_cilk_simd
251 (cp_parser *, cp_token *);
252 static bool cp_parser_omp_declare_reduction_exprs
253 (tree, cp_parser *);
255 /* Manifest constants. */
256 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
257 #define CP_SAVED_TOKEN_STACK 5
259 /* Variables. */
261 /* The stream to which debugging output should be written. */
262 static FILE *cp_lexer_debug_stream;
264 /* Nonzero if we are parsing an unevaluated operand: an operand to
265 sizeof, typeof, or alignof. */
266 int cp_unevaluated_operand;
268 /* Dump up to NUM tokens in BUFFER to FILE starting with token
269 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
270 first token in BUFFER. If NUM is 0, dump all the tokens. If
271 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
272 highlighted by surrounding it in [[ ]]. */
274 static void
275 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
276 cp_token *start_token, unsigned num,
277 cp_token *curr_token)
279 unsigned i, nprinted;
280 cp_token *token;
281 bool do_print;
283 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
285 if (buffer == NULL)
286 return;
288 if (num == 0)
289 num = buffer->length ();
291 if (start_token == NULL)
292 start_token = buffer->address ();
294 if (start_token > buffer->address ())
296 cp_lexer_print_token (file, &(*buffer)[0]);
297 fprintf (file, " ... ");
300 do_print = false;
301 nprinted = 0;
302 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
304 if (token == start_token)
305 do_print = true;
307 if (!do_print)
308 continue;
310 nprinted++;
311 if (token == curr_token)
312 fprintf (file, "[[");
314 cp_lexer_print_token (file, token);
316 if (token == curr_token)
317 fprintf (file, "]]");
319 switch (token->type)
321 case CPP_SEMICOLON:
322 case CPP_OPEN_BRACE:
323 case CPP_CLOSE_BRACE:
324 case CPP_EOF:
325 fputc ('\n', file);
326 break;
328 default:
329 fputc (' ', file);
333 if (i == num && i < buffer->length ())
335 fprintf (file, " ... ");
336 cp_lexer_print_token (file, &buffer->last ());
339 fprintf (file, "\n");
343 /* Dump all tokens in BUFFER to stderr. */
345 void
346 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
348 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
351 DEBUG_FUNCTION void
352 debug (vec<cp_token, va_gc> &ref)
354 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
357 DEBUG_FUNCTION void
358 debug (vec<cp_token, va_gc> *ptr)
360 if (ptr)
361 debug (*ptr);
362 else
363 fprintf (stderr, "<nil>\n");
367 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
368 description for T. */
370 static void
371 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
373 if (t)
375 fprintf (file, "%s: ", desc);
376 print_node_brief (file, "", t, 0);
381 /* Dump parser context C to FILE. */
383 static void
384 cp_debug_print_context (FILE *file, cp_parser_context *c)
386 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
387 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
388 print_node_brief (file, "", c->object_type, 0);
389 fprintf (file, "}\n");
393 /* Print the stack of parsing contexts to FILE starting with FIRST. */
395 static void
396 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
398 unsigned i;
399 cp_parser_context *c;
401 fprintf (file, "Parsing context stack:\n");
402 for (i = 0, c = first; c; c = c->next, i++)
404 fprintf (file, "\t#%u: ", i);
405 cp_debug_print_context (file, c);
410 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
412 static void
413 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
415 if (flag)
416 fprintf (file, "%s: true\n", desc);
420 /* Print an unparsed function entry UF to FILE. */
422 static void
423 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
425 unsigned i;
426 cp_default_arg_entry *default_arg_fn;
427 tree fn;
429 fprintf (file, "\tFunctions with default args:\n");
430 for (i = 0;
431 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
432 i++)
434 fprintf (file, "\t\tClass type: ");
435 print_node_brief (file, "", default_arg_fn->class_type, 0);
436 fprintf (file, "\t\tDeclaration: ");
437 print_node_brief (file, "", default_arg_fn->decl, 0);
438 fprintf (file, "\n");
441 fprintf (file, "\n\tFunctions with definitions that require "
442 "post-processing\n\t\t");
443 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
445 print_node_brief (file, "", fn, 0);
446 fprintf (file, " ");
448 fprintf (file, "\n");
450 fprintf (file, "\n\tNon-static data members with initializers that require "
451 "post-processing\n\t\t");
452 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
454 print_node_brief (file, "", fn, 0);
455 fprintf (file, " ");
457 fprintf (file, "\n");
461 /* Print the stack of unparsed member functions S to FILE. */
463 static void
464 cp_debug_print_unparsed_queues (FILE *file,
465 vec<cp_unparsed_functions_entry, va_gc> *s)
467 unsigned i;
468 cp_unparsed_functions_entry *uf;
470 fprintf (file, "Unparsed functions\n");
471 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
473 fprintf (file, "#%u:\n", i);
474 cp_debug_print_unparsed_function (file, uf);
479 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
480 the given PARSER. If FILE is NULL, the output is printed on stderr. */
482 static void
483 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
485 cp_token *next_token, *first_token, *start_token;
487 if (file == NULL)
488 file = stderr;
490 next_token = parser->lexer->next_token;
491 first_token = parser->lexer->buffer->address ();
492 start_token = (next_token > first_token + window_size / 2)
493 ? next_token - window_size / 2
494 : first_token;
495 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
496 next_token);
500 /* Dump debugging information for the given PARSER. If FILE is NULL,
501 the output is printed on stderr. */
503 void
504 cp_debug_parser (FILE *file, cp_parser *parser)
506 const size_t window_size = 20;
507 cp_token *token;
508 expanded_location eloc;
510 if (file == NULL)
511 file = stderr;
513 fprintf (file, "Parser state\n\n");
514 fprintf (file, "Number of tokens: %u\n",
515 vec_safe_length (parser->lexer->buffer));
516 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
517 cp_debug_print_tree_if_set (file, "Object scope",
518 parser->object_scope);
519 cp_debug_print_tree_if_set (file, "Qualifying scope",
520 parser->qualifying_scope);
521 cp_debug_print_context_stack (file, parser->context);
522 cp_debug_print_flag (file, "Allow GNU extensions",
523 parser->allow_gnu_extensions_p);
524 cp_debug_print_flag (file, "'>' token is greater-than",
525 parser->greater_than_is_operator_p);
526 cp_debug_print_flag (file, "Default args allowed in current "
527 "parameter list", parser->default_arg_ok_p);
528 cp_debug_print_flag (file, "Parsing integral constant-expression",
529 parser->integral_constant_expression_p);
530 cp_debug_print_flag (file, "Allow non-constant expression in current "
531 "constant-expression",
532 parser->allow_non_integral_constant_expression_p);
533 cp_debug_print_flag (file, "Seen non-constant expression",
534 parser->non_integral_constant_expression_p);
535 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
536 "current context",
537 parser->local_variables_forbidden_p);
538 cp_debug_print_flag (file, "In unbraced linkage specification",
539 parser->in_unbraced_linkage_specification_p);
540 cp_debug_print_flag (file, "Parsing a declarator",
541 parser->in_declarator_p);
542 cp_debug_print_flag (file, "In template argument list",
543 parser->in_template_argument_list_p);
544 cp_debug_print_flag (file, "Parsing an iteration statement",
545 parser->in_statement & IN_ITERATION_STMT);
546 cp_debug_print_flag (file, "Parsing a switch statement",
547 parser->in_statement & IN_SWITCH_STMT);
548 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
549 parser->in_statement & IN_OMP_BLOCK);
550 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
551 parser->in_statement & IN_CILK_SIMD_FOR);
552 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
553 parser->in_statement & IN_OMP_FOR);
554 cp_debug_print_flag (file, "Parsing an if statement",
555 parser->in_statement & IN_IF_STMT);
556 cp_debug_print_flag (file, "Parsing a type-id in an expression "
557 "context", parser->in_type_id_in_expr_p);
558 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
559 parser->implicit_extern_c);
560 cp_debug_print_flag (file, "String expressions should be translated "
561 "to execution character set",
562 parser->translate_strings_p);
563 cp_debug_print_flag (file, "Parsing function body outside of a "
564 "local class", parser->in_function_body);
565 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
566 parser->colon_corrects_to_scope_p);
567 cp_debug_print_flag (file, "Colon doesn't start a class definition",
568 parser->colon_doesnt_start_class_def_p);
569 if (parser->type_definition_forbidden_message)
570 fprintf (file, "Error message for forbidden type definitions: %s\n",
571 parser->type_definition_forbidden_message);
572 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
573 fprintf (file, "Number of class definitions in progress: %u\n",
574 parser->num_classes_being_defined);
575 fprintf (file, "Number of template parameter lists for the current "
576 "declaration: %u\n", parser->num_template_parameter_lists);
577 cp_debug_parser_tokens (file, parser, window_size);
578 token = parser->lexer->next_token;
579 fprintf (file, "Next token to parse:\n");
580 fprintf (file, "\tToken: ");
581 cp_lexer_print_token (file, token);
582 eloc = expand_location (token->location);
583 fprintf (file, "\n\tFile: %s\n", eloc.file);
584 fprintf (file, "\tLine: %d\n", eloc.line);
585 fprintf (file, "\tColumn: %d\n", eloc.column);
588 DEBUG_FUNCTION void
589 debug (cp_parser &ref)
591 cp_debug_parser (stderr, &ref);
594 DEBUG_FUNCTION void
595 debug (cp_parser *ptr)
597 if (ptr)
598 debug (*ptr);
599 else
600 fprintf (stderr, "<nil>\n");
603 /* Allocate memory for a new lexer object and return it. */
605 static cp_lexer *
606 cp_lexer_alloc (void)
608 cp_lexer *lexer;
610 c_common_no_more_pch ();
612 /* Allocate the memory. */
613 lexer = ggc_alloc_cleared_cp_lexer ();
615 /* Initially we are not debugging. */
616 lexer->debugging_p = false;
618 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620 /* Create the buffer. */
621 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623 return lexer;
627 /* Create a new main C++ lexer, the lexer that gets tokens from the
628 preprocessor. */
630 static cp_lexer *
631 cp_lexer_new_main (void)
633 cp_lexer *lexer;
634 cp_token token;
636 /* It's possible that parsing the first pragma will load a PCH file,
637 which is a GC collection point. So we have to do that before
638 allocating any memory. */
639 cp_parser_initial_pragma (&token);
641 lexer = cp_lexer_alloc ();
643 /* Put the first token in the buffer. */
644 lexer->buffer->quick_push (token);
646 /* Get the remaining tokens from the preprocessor. */
647 while (token.type != CPP_EOF)
649 cp_lexer_get_preprocessor_token (lexer, &token);
650 vec_safe_push (lexer->buffer, token);
653 lexer->last_token = lexer->buffer->address ()
654 + lexer->buffer->length ()
655 - 1;
656 lexer->next_token = lexer->buffer->length ()
657 ? lexer->buffer->address ()
658 : &eof_token;
660 /* Subsequent preprocessor diagnostics should use compiler
661 diagnostic functions to get the compiler source location. */
662 done_lexing = true;
664 gcc_assert (!lexer->next_token->purged_p);
665 return lexer;
668 /* Create a new lexer whose token stream is primed with the tokens in
669 CACHE. When these tokens are exhausted, no new tokens will be read. */
671 static cp_lexer *
672 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 cp_token *first = cache->first;
675 cp_token *last = cache->last;
676 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
678 /* We do not own the buffer. */
679 lexer->buffer = NULL;
680 lexer->next_token = first == last ? &eof_token : first;
681 lexer->last_token = last;
683 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685 /* Initially we are not debugging. */
686 lexer->debugging_p = false;
688 gcc_assert (!lexer->next_token->purged_p);
689 return lexer;
692 /* Frees all resources associated with LEXER. */
694 static void
695 cp_lexer_destroy (cp_lexer *lexer)
697 vec_free (lexer->buffer);
698 lexer->saved_tokens.release ();
699 ggc_free (lexer);
702 /* Returns nonzero if debugging information should be output. */
704 static inline bool
705 cp_lexer_debugging_p (cp_lexer *lexer)
707 return lexer->debugging_p;
711 static inline cp_token_position
712 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
714 gcc_assert (!previous_p || lexer->next_token != &eof_token);
716 return lexer->next_token - previous_p;
719 static inline cp_token *
720 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
722 return pos;
725 static inline void
726 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
728 lexer->next_token = cp_lexer_token_at (lexer, pos);
731 static inline cp_token_position
732 cp_lexer_previous_token_position (cp_lexer *lexer)
734 if (lexer->next_token == &eof_token)
735 return lexer->last_token - 1;
736 else
737 return cp_lexer_token_position (lexer, true);
740 static inline cp_token *
741 cp_lexer_previous_token (cp_lexer *lexer)
743 cp_token_position tp = cp_lexer_previous_token_position (lexer);
745 return cp_lexer_token_at (lexer, tp);
748 /* nonzero if we are presently saving tokens. */
750 static inline int
751 cp_lexer_saving_tokens (const cp_lexer* lexer)
753 return lexer->saved_tokens.length () != 0;
756 /* Store the next token from the preprocessor in *TOKEN. Return true
757 if we reach EOF. If LEXER is NULL, assume we are handling an
758 initial #pragma pch_preprocess, and thus want the lexer to return
759 processed strings. */
761 static void
762 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
764 static int is_extern_c = 0;
766 /* Get a new token from the preprocessor. */
767 token->type
768 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
769 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
770 token->keyword = RID_MAX;
771 token->pragma_kind = PRAGMA_NONE;
772 token->purged_p = false;
774 /* On some systems, some header files are surrounded by an
775 implicit extern "C" block. Set a flag in the token if it
776 comes from such a header. */
777 is_extern_c += pending_lang_change;
778 pending_lang_change = 0;
779 token->implicit_extern_c = is_extern_c > 0;
781 /* Check to see if this token is a keyword. */
782 if (token->type == CPP_NAME)
784 if (C_IS_RESERVED_WORD (token->u.value))
786 /* Mark this token as a keyword. */
787 token->type = CPP_KEYWORD;
788 /* Record which keyword. */
789 token->keyword = C_RID_CODE (token->u.value);
791 else
793 if (warn_cxx0x_compat
794 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
795 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
797 /* Warn about the C++0x keyword (but still treat it as
798 an identifier). */
799 warning (OPT_Wc__0x_compat,
800 "identifier %qE is a keyword in C++11",
801 token->u.value);
803 /* Clear out the C_RID_CODE so we don't warn about this
804 particular identifier-turned-keyword again. */
805 C_SET_RID_CODE (token->u.value, RID_MAX);
808 token->ambiguous_p = false;
809 token->keyword = RID_MAX;
812 else if (token->type == CPP_AT_NAME)
814 /* This only happens in Objective-C++; it must be a keyword. */
815 token->type = CPP_KEYWORD;
816 switch (C_RID_CODE (token->u.value))
818 /* Replace 'class' with '@class', 'private' with '@private',
819 etc. This prevents confusion with the C++ keyword
820 'class', and makes the tokens consistent with other
821 Objective-C 'AT' keywords. For example '@class' is
822 reported as RID_AT_CLASS which is consistent with
823 '@synchronized', which is reported as
824 RID_AT_SYNCHRONIZED.
826 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
827 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
828 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
829 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
830 case RID_THROW: token->keyword = RID_AT_THROW; break;
831 case RID_TRY: token->keyword = RID_AT_TRY; break;
832 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
833 default: token->keyword = C_RID_CODE (token->u.value);
836 else if (token->type == CPP_PRAGMA)
838 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
839 token->pragma_kind = ((enum pragma_kind)
840 TREE_INT_CST_LOW (token->u.value));
841 token->u.value = NULL_TREE;
845 /* Update the globals input_location and the input file stack from TOKEN. */
846 static inline void
847 cp_lexer_set_source_position_from_token (cp_token *token)
849 if (token->type != CPP_EOF)
851 input_location = token->location;
855 /* Return a pointer to the next token in the token stream, but do not
856 consume it. */
858 static inline cp_token *
859 cp_lexer_peek_token (cp_lexer *lexer)
861 if (cp_lexer_debugging_p (lexer))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865 putc ('\n', cp_lexer_debug_stream);
867 return lexer->next_token;
870 /* Return true if the next token has the indicated TYPE. */
872 static inline bool
873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
875 return cp_lexer_peek_token (lexer)->type == type;
878 /* Return true if the next token does not have the indicated TYPE. */
880 static inline bool
881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
883 return !cp_lexer_next_token_is (lexer, type);
886 /* Return true if the next token is the indicated KEYWORD. */
888 static inline bool
889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
891 return cp_lexer_peek_token (lexer)->keyword == keyword;
894 static inline bool
895 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
897 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
900 /* Return true if the next token is not the indicated KEYWORD. */
902 static inline bool
903 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
905 return cp_lexer_peek_token (lexer)->keyword != keyword;
908 /* Return true if the next token is a keyword for a decl-specifier. */
910 static bool
911 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
913 cp_token *token;
915 token = cp_lexer_peek_token (lexer);
916 switch (token->keyword)
918 /* auto specifier: storage-class-specifier in C++,
919 simple-type-specifier in C++0x. */
920 case RID_AUTO:
921 /* Storage classes. */
922 case RID_REGISTER:
923 case RID_STATIC:
924 case RID_EXTERN:
925 case RID_MUTABLE:
926 case RID_THREAD:
927 /* Elaborated type specifiers. */
928 case RID_ENUM:
929 case RID_CLASS:
930 case RID_STRUCT:
931 case RID_UNION:
932 case RID_TYPENAME:
933 /* Simple type specifiers. */
934 case RID_CHAR:
935 case RID_CHAR16:
936 case RID_CHAR32:
937 case RID_WCHAR:
938 case RID_BOOL:
939 case RID_SHORT:
940 case RID_INT:
941 case RID_LONG:
942 case RID_INT128:
943 case RID_SIGNED:
944 case RID_UNSIGNED:
945 case RID_FLOAT:
946 case RID_DOUBLE:
947 case RID_VOID:
948 /* GNU extensions. */
949 case RID_ATTRIBUTE:
950 case RID_TYPEOF:
951 /* C++0x extensions. */
952 case RID_DECLTYPE:
953 case RID_UNDERLYING_TYPE:
954 return true;
956 default:
957 return false;
961 /* Returns TRUE iff the token T begins a decltype type. */
963 static bool
964 token_is_decltype (cp_token *t)
966 return (t->keyword == RID_DECLTYPE
967 || t->type == CPP_DECLTYPE);
970 /* Returns TRUE iff the next token begins a decltype type. */
972 static bool
973 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
975 cp_token *t = cp_lexer_peek_token (lexer);
976 return token_is_decltype (t);
979 /* Return a pointer to the Nth token in the token stream. If N is 1,
980 then this is precisely equivalent to cp_lexer_peek_token (except
981 that it is not inline). One would like to disallow that case, but
982 there is one case (cp_parser_nth_token_starts_template_id) where
983 the caller passes a variable for N and it might be 1. */
985 static cp_token *
986 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
988 cp_token *token;
990 /* N is 1-based, not zero-based. */
991 gcc_assert (n > 0);
993 if (cp_lexer_debugging_p (lexer))
994 fprintf (cp_lexer_debug_stream,
995 "cp_lexer: peeking ahead %ld at token: ", (long)n);
997 --n;
998 token = lexer->next_token;
999 gcc_assert (!n || token != &eof_token);
1000 while (n != 0)
1002 ++token;
1003 if (token == lexer->last_token)
1005 token = &eof_token;
1006 break;
1009 if (!token->purged_p)
1010 --n;
1013 if (cp_lexer_debugging_p (lexer))
1015 cp_lexer_print_token (cp_lexer_debug_stream, token);
1016 putc ('\n', cp_lexer_debug_stream);
1019 return token;
1022 /* Return the next token, and advance the lexer's next_token pointer
1023 to point to the next non-purged token. */
1025 static cp_token *
1026 cp_lexer_consume_token (cp_lexer* lexer)
1028 cp_token *token = lexer->next_token;
1030 gcc_assert (token != &eof_token);
1031 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1035 lexer->next_token++;
1036 if (lexer->next_token == lexer->last_token)
1038 lexer->next_token = &eof_token;
1039 break;
1043 while (lexer->next_token->purged_p);
1045 cp_lexer_set_source_position_from_token (token);
1047 /* Provide debugging output. */
1048 if (cp_lexer_debugging_p (lexer))
1050 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1051 cp_lexer_print_token (cp_lexer_debug_stream, token);
1052 putc ('\n', cp_lexer_debug_stream);
1055 return token;
1058 /* Permanently remove the next token from the token stream, and
1059 advance the next_token pointer to refer to the next non-purged
1060 token. */
1062 static void
1063 cp_lexer_purge_token (cp_lexer *lexer)
1065 cp_token *tok = lexer->next_token;
1067 gcc_assert (tok != &eof_token);
1068 tok->purged_p = true;
1069 tok->location = UNKNOWN_LOCATION;
1070 tok->u.value = NULL_TREE;
1071 tok->keyword = RID_MAX;
1075 tok++;
1076 if (tok == lexer->last_token)
1078 tok = &eof_token;
1079 break;
1082 while (tok->purged_p);
1083 lexer->next_token = tok;
1086 /* Permanently remove all tokens after TOK, up to, but not
1087 including, the token that will be returned next by
1088 cp_lexer_peek_token. */
1090 static void
1091 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1093 cp_token *peek = lexer->next_token;
1095 if (peek == &eof_token)
1096 peek = lexer->last_token;
1098 gcc_assert (tok < peek);
1100 for ( tok += 1; tok != peek; tok += 1)
1102 tok->purged_p = true;
1103 tok->location = UNKNOWN_LOCATION;
1104 tok->u.value = NULL_TREE;
1105 tok->keyword = RID_MAX;
1109 /* Begin saving tokens. All tokens consumed after this point will be
1110 preserved. */
1112 static void
1113 cp_lexer_save_tokens (cp_lexer* lexer)
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1117 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1119 lexer->saved_tokens.safe_push (lexer->next_token);
1122 /* Commit to the portion of the token stream most recently saved. */
1124 static void
1125 cp_lexer_commit_tokens (cp_lexer* lexer)
1127 /* Provide debugging output. */
1128 if (cp_lexer_debugging_p (lexer))
1129 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1131 lexer->saved_tokens.pop ();
1134 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1135 to the token stream. Stop saving tokens. */
1137 static void
1138 cp_lexer_rollback_tokens (cp_lexer* lexer)
1140 /* Provide debugging output. */
1141 if (cp_lexer_debugging_p (lexer))
1142 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1144 lexer->next_token = lexer->saved_tokens.pop ();
1147 /* Print a representation of the TOKEN on the STREAM. */
1149 static void
1150 cp_lexer_print_token (FILE * stream, cp_token *token)
1152 /* We don't use cpp_type2name here because the parser defines
1153 a few tokens of its own. */
1154 static const char *const token_names[] = {
1155 /* cpplib-defined token types */
1156 #define OP(e, s) #e,
1157 #define TK(e, s) #e,
1158 TTYPE_TABLE
1159 #undef OP
1160 #undef TK
1161 /* C++ parser token types - see "Manifest constants", above. */
1162 "KEYWORD",
1163 "TEMPLATE_ID",
1164 "NESTED_NAME_SPECIFIER",
1167 /* For some tokens, print the associated data. */
1168 switch (token->type)
1170 case CPP_KEYWORD:
1171 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1172 For example, `struct' is mapped to an INTEGER_CST. */
1173 if (!identifier_p (token->u.value))
1174 break;
1175 /* else fall through */
1176 case CPP_NAME:
1177 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1178 break;
1180 case CPP_STRING:
1181 case CPP_STRING16:
1182 case CPP_STRING32:
1183 case CPP_WSTRING:
1184 case CPP_UTF8STRING:
1185 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1186 break;
1188 case CPP_NUMBER:
1189 print_generic_expr (stream, token->u.value, 0);
1190 break;
1192 default:
1193 /* If we have a name for the token, print it out. Otherwise, we
1194 simply give the numeric code. */
1195 if (token->type < ARRAY_SIZE(token_names))
1196 fputs (token_names[token->type], stream);
1197 else
1198 fprintf (stream, "[%d]", token->type);
1199 break;
1203 DEBUG_FUNCTION void
1204 debug (cp_token &ref)
1206 cp_lexer_print_token (stderr, &ref);
1207 fprintf (stderr, "\n");
1210 DEBUG_FUNCTION void
1211 debug (cp_token *ptr)
1213 if (ptr)
1214 debug (*ptr);
1215 else
1216 fprintf (stderr, "<nil>\n");
1220 /* Start emitting debugging information. */
1222 static void
1223 cp_lexer_start_debugging (cp_lexer* lexer)
1225 lexer->debugging_p = true;
1226 cp_lexer_debug_stream = stderr;
1229 /* Stop emitting debugging information. */
1231 static void
1232 cp_lexer_stop_debugging (cp_lexer* lexer)
1234 lexer->debugging_p = false;
1235 cp_lexer_debug_stream = NULL;
1238 /* Create a new cp_token_cache, representing a range of tokens. */
1240 static cp_token_cache *
1241 cp_token_cache_new (cp_token *first, cp_token *last)
1243 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1244 cache->first = first;
1245 cache->last = last;
1246 return cache;
1249 /* Diagnose if #pragma omp declare simd isn't followed immediately
1250 by function declaration or definition. */
1252 static inline void
1253 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1255 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1257 error ("%<#pragma omp declare simd%> not immediately followed by "
1258 "function declaration or definition");
1259 parser->omp_declare_simd = NULL;
1263 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1264 and put that into "omp declare simd" attribute. */
1266 static inline void
1267 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1269 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1271 if (fndecl == error_mark_node)
1273 parser->omp_declare_simd = NULL;
1274 return;
1276 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1278 cp_ensure_no_omp_declare_simd (parser);
1279 return;
1284 /* Decl-specifiers. */
1286 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1288 static void
1289 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1291 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1294 /* Declarators. */
1296 /* Nothing other than the parser should be creating declarators;
1297 declarators are a semi-syntactic representation of C++ entities.
1298 Other parts of the front end that need to create entities (like
1299 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1301 static cp_declarator *make_call_declarator
1302 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1303 static cp_declarator *make_array_declarator
1304 (cp_declarator *, tree);
1305 static cp_declarator *make_pointer_declarator
1306 (cp_cv_quals, cp_declarator *, tree);
1307 static cp_declarator *make_reference_declarator
1308 (cp_cv_quals, cp_declarator *, bool, tree);
1309 static cp_parameter_declarator *make_parameter_declarator
1310 (cp_decl_specifier_seq *, cp_declarator *, tree);
1311 static cp_declarator *make_ptrmem_declarator
1312 (cp_cv_quals, tree, cp_declarator *, tree);
1314 /* An erroneous declarator. */
1315 static cp_declarator *cp_error_declarator;
1317 /* The obstack on which declarators and related data structures are
1318 allocated. */
1319 static struct obstack declarator_obstack;
1321 /* Alloc BYTES from the declarator memory pool. */
1323 static inline void *
1324 alloc_declarator (size_t bytes)
1326 return obstack_alloc (&declarator_obstack, bytes);
1329 /* Allocate a declarator of the indicated KIND. Clear fields that are
1330 common to all declarators. */
1332 static cp_declarator *
1333 make_declarator (cp_declarator_kind kind)
1335 cp_declarator *declarator;
1337 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1338 declarator->kind = kind;
1339 declarator->attributes = NULL_TREE;
1340 declarator->std_attributes = NULL_TREE;
1341 declarator->declarator = NULL;
1342 declarator->parameter_pack_p = false;
1343 declarator->id_loc = UNKNOWN_LOCATION;
1345 return declarator;
1348 /* Make a declarator for a generalized identifier. If
1349 QUALIFYING_SCOPE is non-NULL, the identifier is
1350 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1351 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1352 is, if any. */
1354 static cp_declarator *
1355 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1356 special_function_kind sfk)
1358 cp_declarator *declarator;
1360 /* It is valid to write:
1362 class C { void f(); };
1363 typedef C D;
1364 void D::f();
1366 The standard is not clear about whether `typedef const C D' is
1367 legal; as of 2002-09-15 the committee is considering that
1368 question. EDG 3.0 allows that syntax. Therefore, we do as
1369 well. */
1370 if (qualifying_scope && TYPE_P (qualifying_scope))
1371 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1373 gcc_assert (identifier_p (unqualified_name)
1374 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1375 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1377 declarator = make_declarator (cdk_id);
1378 declarator->u.id.qualifying_scope = qualifying_scope;
1379 declarator->u.id.unqualified_name = unqualified_name;
1380 declarator->u.id.sfk = sfk;
1382 return declarator;
1385 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1386 of modifiers such as const or volatile to apply to the pointer
1387 type, represented as identifiers. ATTRIBUTES represent the attributes that
1388 appertain to the pointer or reference. */
1390 cp_declarator *
1391 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1392 tree attributes)
1394 cp_declarator *declarator;
1396 declarator = make_declarator (cdk_pointer);
1397 declarator->declarator = target;
1398 declarator->u.pointer.qualifiers = cv_qualifiers;
1399 declarator->u.pointer.class_type = NULL_TREE;
1400 if (target)
1402 declarator->id_loc = target->id_loc;
1403 declarator->parameter_pack_p = target->parameter_pack_p;
1404 target->parameter_pack_p = false;
1406 else
1407 declarator->parameter_pack_p = false;
1409 declarator->std_attributes = attributes;
1411 return declarator;
1414 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1415 represent the attributes that appertain to the pointer or
1416 reference. */
1418 cp_declarator *
1419 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1420 bool rvalue_ref, tree attributes)
1422 cp_declarator *declarator;
1424 declarator = make_declarator (cdk_reference);
1425 declarator->declarator = target;
1426 declarator->u.reference.qualifiers = cv_qualifiers;
1427 declarator->u.reference.rvalue_ref = rvalue_ref;
1428 if (target)
1430 declarator->id_loc = target->id_loc;
1431 declarator->parameter_pack_p = target->parameter_pack_p;
1432 target->parameter_pack_p = false;
1434 else
1435 declarator->parameter_pack_p = false;
1437 declarator->std_attributes = attributes;
1439 return declarator;
1442 /* Like make_pointer_declarator -- but for a pointer to a non-static
1443 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1444 appertain to the pointer or reference. */
1446 cp_declarator *
1447 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1448 cp_declarator *pointee,
1449 tree attributes)
1451 cp_declarator *declarator;
1453 declarator = make_declarator (cdk_ptrmem);
1454 declarator->declarator = pointee;
1455 declarator->u.pointer.qualifiers = cv_qualifiers;
1456 declarator->u.pointer.class_type = class_type;
1458 if (pointee)
1460 declarator->parameter_pack_p = pointee->parameter_pack_p;
1461 pointee->parameter_pack_p = false;
1463 else
1464 declarator->parameter_pack_p = false;
1466 declarator->std_attributes = attributes;
1468 return declarator;
1471 /* Make a declarator for the function given by TARGET, with the
1472 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1473 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1474 indicates what exceptions can be thrown. */
1476 cp_declarator *
1477 make_call_declarator (cp_declarator *target,
1478 tree parms,
1479 cp_cv_quals cv_qualifiers,
1480 cp_virt_specifiers virt_specifiers,
1481 cp_ref_qualifier ref_qualifier,
1482 tree exception_specification,
1483 tree late_return_type)
1485 cp_declarator *declarator;
1487 declarator = make_declarator (cdk_function);
1488 declarator->declarator = target;
1489 declarator->u.function.parameters = parms;
1490 declarator->u.function.qualifiers = cv_qualifiers;
1491 declarator->u.function.virt_specifiers = virt_specifiers;
1492 declarator->u.function.ref_qualifier = ref_qualifier;
1493 declarator->u.function.exception_specification = exception_specification;
1494 declarator->u.function.late_return_type = late_return_type;
1495 if (target)
1497 declarator->id_loc = target->id_loc;
1498 declarator->parameter_pack_p = target->parameter_pack_p;
1499 target->parameter_pack_p = false;
1501 else
1502 declarator->parameter_pack_p = false;
1504 return declarator;
1507 /* Make a declarator for an array of BOUNDS elements, each of which is
1508 defined by ELEMENT. */
1510 cp_declarator *
1511 make_array_declarator (cp_declarator *element, tree bounds)
1513 cp_declarator *declarator;
1515 declarator = make_declarator (cdk_array);
1516 declarator->declarator = element;
1517 declarator->u.array.bounds = bounds;
1518 if (element)
1520 declarator->id_loc = element->id_loc;
1521 declarator->parameter_pack_p = element->parameter_pack_p;
1522 element->parameter_pack_p = false;
1524 else
1525 declarator->parameter_pack_p = false;
1527 return declarator;
1530 /* Determine whether the declarator we've seen so far can be a
1531 parameter pack, when followed by an ellipsis. */
1532 static bool
1533 declarator_can_be_parameter_pack (cp_declarator *declarator)
1535 /* Search for a declarator name, or any other declarator that goes
1536 after the point where the ellipsis could appear in a parameter
1537 pack. If we find any of these, then this declarator can not be
1538 made into a parameter pack. */
1539 bool found = false;
1540 while (declarator && !found)
1542 switch ((int)declarator->kind)
1544 case cdk_id:
1545 case cdk_array:
1546 found = true;
1547 break;
1549 case cdk_error:
1550 return true;
1552 default:
1553 declarator = declarator->declarator;
1554 break;
1558 return !found;
1561 cp_parameter_declarator *no_parameters;
1563 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1564 DECLARATOR and DEFAULT_ARGUMENT. */
1566 cp_parameter_declarator *
1567 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1568 cp_declarator *declarator,
1569 tree default_argument)
1571 cp_parameter_declarator *parameter;
1573 parameter = ((cp_parameter_declarator *)
1574 alloc_declarator (sizeof (cp_parameter_declarator)));
1575 parameter->next = NULL;
1576 if (decl_specifiers)
1577 parameter->decl_specifiers = *decl_specifiers;
1578 else
1579 clear_decl_specs (&parameter->decl_specifiers);
1580 parameter->declarator = declarator;
1581 parameter->default_argument = default_argument;
1582 parameter->ellipsis_p = false;
1584 return parameter;
1587 /* Returns true iff DECLARATOR is a declaration for a function. */
1589 static bool
1590 function_declarator_p (const cp_declarator *declarator)
1592 while (declarator)
1594 if (declarator->kind == cdk_function
1595 && declarator->declarator->kind == cdk_id)
1596 return true;
1597 if (declarator->kind == cdk_id
1598 || declarator->kind == cdk_error)
1599 return false;
1600 declarator = declarator->declarator;
1602 return false;
1605 /* The parser. */
1607 /* Overview
1608 --------
1610 A cp_parser parses the token stream as specified by the C++
1611 grammar. Its job is purely parsing, not semantic analysis. For
1612 example, the parser breaks the token stream into declarators,
1613 expressions, statements, and other similar syntactic constructs.
1614 It does not check that the types of the expressions on either side
1615 of an assignment-statement are compatible, or that a function is
1616 not declared with a parameter of type `void'.
1618 The parser invokes routines elsewhere in the compiler to perform
1619 semantic analysis and to build up the abstract syntax tree for the
1620 code processed.
1622 The parser (and the template instantiation code, which is, in a
1623 way, a close relative of parsing) are the only parts of the
1624 compiler that should be calling push_scope and pop_scope, or
1625 related functions. The parser (and template instantiation code)
1626 keeps track of what scope is presently active; everything else
1627 should simply honor that. (The code that generates static
1628 initializers may also need to set the scope, in order to check
1629 access control correctly when emitting the initializers.)
1631 Methodology
1632 -----------
1634 The parser is of the standard recursive-descent variety. Upcoming
1635 tokens in the token stream are examined in order to determine which
1636 production to use when parsing a non-terminal. Some C++ constructs
1637 require arbitrary look ahead to disambiguate. For example, it is
1638 impossible, in the general case, to tell whether a statement is an
1639 expression or declaration without scanning the entire statement.
1640 Therefore, the parser is capable of "parsing tentatively." When the
1641 parser is not sure what construct comes next, it enters this mode.
1642 Then, while we attempt to parse the construct, the parser queues up
1643 error messages, rather than issuing them immediately, and saves the
1644 tokens it consumes. If the construct is parsed successfully, the
1645 parser "commits", i.e., it issues any queued error messages and
1646 the tokens that were being preserved are permanently discarded.
1647 If, however, the construct is not parsed successfully, the parser
1648 rolls back its state completely so that it can resume parsing using
1649 a different alternative.
1651 Future Improvements
1652 -------------------
1654 The performance of the parser could probably be improved substantially.
1655 We could often eliminate the need to parse tentatively by looking ahead
1656 a little bit. In some places, this approach might not entirely eliminate
1657 the need to parse tentatively, but it might still speed up the average
1658 case. */
1660 /* Flags that are passed to some parsing functions. These values can
1661 be bitwise-ored together. */
1663 enum
1665 /* No flags. */
1666 CP_PARSER_FLAGS_NONE = 0x0,
1667 /* The construct is optional. If it is not present, then no error
1668 should be issued. */
1669 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1670 /* When parsing a type-specifier, treat user-defined type-names
1671 as non-type identifiers. */
1672 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1673 /* When parsing a type-specifier, do not try to parse a class-specifier
1674 or enum-specifier. */
1675 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1676 /* When parsing a decl-specifier-seq, only allow type-specifier or
1677 constexpr. */
1678 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1681 /* This type is used for parameters and variables which hold
1682 combinations of the above flags. */
1683 typedef int cp_parser_flags;
1685 /* The different kinds of declarators we want to parse. */
1687 typedef enum cp_parser_declarator_kind
1689 /* We want an abstract declarator. */
1690 CP_PARSER_DECLARATOR_ABSTRACT,
1691 /* We want a named declarator. */
1692 CP_PARSER_DECLARATOR_NAMED,
1693 /* We don't mind, but the name must be an unqualified-id. */
1694 CP_PARSER_DECLARATOR_EITHER
1695 } cp_parser_declarator_kind;
1697 /* The precedence values used to parse binary expressions. The minimum value
1698 of PREC must be 1, because zero is reserved to quickly discriminate
1699 binary operators from other tokens. */
1701 enum cp_parser_prec
1703 PREC_NOT_OPERATOR,
1704 PREC_LOGICAL_OR_EXPRESSION,
1705 PREC_LOGICAL_AND_EXPRESSION,
1706 PREC_INCLUSIVE_OR_EXPRESSION,
1707 PREC_EXCLUSIVE_OR_EXPRESSION,
1708 PREC_AND_EXPRESSION,
1709 PREC_EQUALITY_EXPRESSION,
1710 PREC_RELATIONAL_EXPRESSION,
1711 PREC_SHIFT_EXPRESSION,
1712 PREC_ADDITIVE_EXPRESSION,
1713 PREC_MULTIPLICATIVE_EXPRESSION,
1714 PREC_PM_EXPRESSION,
1715 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1718 /* A mapping from a token type to a corresponding tree node type, with a
1719 precedence value. */
1721 typedef struct cp_parser_binary_operations_map_node
1723 /* The token type. */
1724 enum cpp_ttype token_type;
1725 /* The corresponding tree code. */
1726 enum tree_code tree_type;
1727 /* The precedence of this operator. */
1728 enum cp_parser_prec prec;
1729 } cp_parser_binary_operations_map_node;
1731 typedef struct cp_parser_expression_stack_entry
1733 /* Left hand side of the binary operation we are currently
1734 parsing. */
1735 tree lhs;
1736 /* Original tree code for left hand side, if it was a binary
1737 expression itself (used for -Wparentheses). */
1738 enum tree_code lhs_type;
1739 /* Tree code for the binary operation we are parsing. */
1740 enum tree_code tree_type;
1741 /* Precedence of the binary operation we are parsing. */
1742 enum cp_parser_prec prec;
1743 /* Location of the binary operation we are parsing. */
1744 location_t loc;
1745 } cp_parser_expression_stack_entry;
1747 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1748 entries because precedence levels on the stack are monotonically
1749 increasing. */
1750 typedef struct cp_parser_expression_stack_entry
1751 cp_parser_expression_stack[NUM_PREC_VALUES];
1753 /* Prototypes. */
1755 /* Constructors and destructors. */
1757 static cp_parser_context *cp_parser_context_new
1758 (cp_parser_context *);
1760 /* Class variables. */
1762 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1764 /* The operator-precedence table used by cp_parser_binary_expression.
1765 Transformed into an associative array (binops_by_token) by
1766 cp_parser_new. */
1768 static const cp_parser_binary_operations_map_node binops[] = {
1769 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1770 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1772 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1773 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1774 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1776 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1777 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1779 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1780 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1782 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1783 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1784 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1785 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1787 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1788 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1790 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1792 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1794 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1796 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1798 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1801 /* The same as binops, but initialized by cp_parser_new so that
1802 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1803 for speed. */
1804 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1806 /* Constructors and destructors. */
1808 /* Construct a new context. The context below this one on the stack
1809 is given by NEXT. */
1811 static cp_parser_context *
1812 cp_parser_context_new (cp_parser_context* next)
1814 cp_parser_context *context;
1816 /* Allocate the storage. */
1817 if (cp_parser_context_free_list != NULL)
1819 /* Pull the first entry from the free list. */
1820 context = cp_parser_context_free_list;
1821 cp_parser_context_free_list = context->next;
1822 memset (context, 0, sizeof (*context));
1824 else
1825 context = ggc_alloc_cleared_cp_parser_context ();
1827 /* No errors have occurred yet in this context. */
1828 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1829 /* If this is not the bottommost context, copy information that we
1830 need from the previous context. */
1831 if (next)
1833 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1834 expression, then we are parsing one in this context, too. */
1835 context->object_type = next->object_type;
1836 /* Thread the stack. */
1837 context->next = next;
1840 return context;
1843 /* Managing the unparsed function queues. */
1845 #define unparsed_funs_with_default_args \
1846 parser->unparsed_queues->last ().funs_with_default_args
1847 #define unparsed_funs_with_definitions \
1848 parser->unparsed_queues->last ().funs_with_definitions
1849 #define unparsed_nsdmis \
1850 parser->unparsed_queues->last ().nsdmis
1852 static void
1853 push_unparsed_function_queues (cp_parser *parser)
1855 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1856 vec_safe_push (parser->unparsed_queues, e);
1859 static void
1860 pop_unparsed_function_queues (cp_parser *parser)
1862 release_tree_vector (unparsed_funs_with_definitions);
1863 parser->unparsed_queues->pop ();
1866 /* Prototypes. */
1868 /* Constructors and destructors. */
1870 static cp_parser *cp_parser_new
1871 (void);
1873 /* Routines to parse various constructs.
1875 Those that return `tree' will return the error_mark_node (rather
1876 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1877 Sometimes, they will return an ordinary node if error-recovery was
1878 attempted, even though a parse error occurred. So, to check
1879 whether or not a parse error occurred, you should always use
1880 cp_parser_error_occurred. If the construct is optional (indicated
1881 either by an `_opt' in the name of the function that does the
1882 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1883 the construct is not present. */
1885 /* Lexical conventions [gram.lex] */
1887 static tree cp_parser_identifier
1888 (cp_parser *);
1889 static tree cp_parser_string_literal
1890 (cp_parser *, bool, bool);
1891 static tree cp_parser_userdef_char_literal
1892 (cp_parser *);
1893 static tree cp_parser_userdef_string_literal
1894 (cp_token *);
1895 static tree cp_parser_userdef_numeric_literal
1896 (cp_parser *);
1898 /* Basic concepts [gram.basic] */
1900 static bool cp_parser_translation_unit
1901 (cp_parser *);
1903 /* Expressions [gram.expr] */
1905 static tree cp_parser_primary_expression
1906 (cp_parser *, bool, bool, bool, cp_id_kind *);
1907 static tree cp_parser_id_expression
1908 (cp_parser *, bool, bool, bool *, bool, bool);
1909 static tree cp_parser_unqualified_id
1910 (cp_parser *, bool, bool, bool, bool);
1911 static tree cp_parser_nested_name_specifier_opt
1912 (cp_parser *, bool, bool, bool, bool);
1913 static tree cp_parser_nested_name_specifier
1914 (cp_parser *, bool, bool, bool, bool);
1915 static tree cp_parser_qualifying_entity
1916 (cp_parser *, bool, bool, bool, bool, bool);
1917 static tree cp_parser_postfix_expression
1918 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1919 static tree cp_parser_postfix_open_square_expression
1920 (cp_parser *, tree, bool, bool);
1921 static tree cp_parser_postfix_dot_deref_expression
1922 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1923 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1924 (cp_parser *, int, bool, bool, bool *);
1925 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1926 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1927 static void cp_parser_pseudo_destructor_name
1928 (cp_parser *, tree, tree *, tree *);
1929 static tree cp_parser_unary_expression
1930 (cp_parser *, bool, bool, cp_id_kind *);
1931 static enum tree_code cp_parser_unary_operator
1932 (cp_token *);
1933 static tree cp_parser_new_expression
1934 (cp_parser *);
1935 static vec<tree, va_gc> *cp_parser_new_placement
1936 (cp_parser *);
1937 static tree cp_parser_new_type_id
1938 (cp_parser *, tree *);
1939 static cp_declarator *cp_parser_new_declarator_opt
1940 (cp_parser *);
1941 static cp_declarator *cp_parser_direct_new_declarator
1942 (cp_parser *);
1943 static vec<tree, va_gc> *cp_parser_new_initializer
1944 (cp_parser *);
1945 static tree cp_parser_delete_expression
1946 (cp_parser *);
1947 static tree cp_parser_cast_expression
1948 (cp_parser *, bool, bool, bool, cp_id_kind *);
1949 static tree cp_parser_binary_expression
1950 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1951 static tree cp_parser_question_colon_clause
1952 (cp_parser *, tree);
1953 static tree cp_parser_assignment_expression
1954 (cp_parser *, bool, cp_id_kind *);
1955 static enum tree_code cp_parser_assignment_operator_opt
1956 (cp_parser *);
1957 static tree cp_parser_expression
1958 (cp_parser *, bool, cp_id_kind *);
1959 static tree cp_parser_expression
1960 (cp_parser *, bool, bool, cp_id_kind *);
1961 static tree cp_parser_constant_expression
1962 (cp_parser *, bool, bool *);
1963 static tree cp_parser_builtin_offsetof
1964 (cp_parser *);
1965 static tree cp_parser_lambda_expression
1966 (cp_parser *);
1967 static void cp_parser_lambda_introducer
1968 (cp_parser *, tree);
1969 static bool cp_parser_lambda_declarator_opt
1970 (cp_parser *, tree);
1971 static void cp_parser_lambda_body
1972 (cp_parser *, tree);
1974 /* Statements [gram.stmt.stmt] */
1976 static void cp_parser_statement
1977 (cp_parser *, tree, bool, bool *);
1978 static void cp_parser_label_for_labeled_statement
1979 (cp_parser *, tree);
1980 static tree cp_parser_expression_statement
1981 (cp_parser *, tree);
1982 static tree cp_parser_compound_statement
1983 (cp_parser *, tree, bool, bool);
1984 static void cp_parser_statement_seq_opt
1985 (cp_parser *, tree);
1986 static tree cp_parser_selection_statement
1987 (cp_parser *, bool *);
1988 static tree cp_parser_condition
1989 (cp_parser *);
1990 static tree cp_parser_iteration_statement
1991 (cp_parser *, bool);
1992 static bool cp_parser_for_init_statement
1993 (cp_parser *, tree *decl);
1994 static tree cp_parser_for
1995 (cp_parser *, bool);
1996 static tree cp_parser_c_for
1997 (cp_parser *, tree, tree, bool);
1998 static tree cp_parser_range_for
1999 (cp_parser *, tree, tree, tree, bool);
2000 static void do_range_for_auto_deduction
2001 (tree, tree);
2002 static tree cp_parser_perform_range_for_lookup
2003 (tree, tree *, tree *);
2004 static tree cp_parser_range_for_member_function
2005 (tree, tree);
2006 static tree cp_parser_jump_statement
2007 (cp_parser *);
2008 static void cp_parser_declaration_statement
2009 (cp_parser *);
2011 static tree cp_parser_implicitly_scoped_statement
2012 (cp_parser *, bool *);
2013 static void cp_parser_already_scoped_statement
2014 (cp_parser *);
2016 /* Declarations [gram.dcl.dcl] */
2018 static void cp_parser_declaration_seq_opt
2019 (cp_parser *);
2020 static void cp_parser_declaration
2021 (cp_parser *);
2022 static void cp_parser_block_declaration
2023 (cp_parser *, bool);
2024 static void cp_parser_simple_declaration
2025 (cp_parser *, bool, tree *);
2026 static void cp_parser_decl_specifier_seq
2027 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2028 static tree cp_parser_storage_class_specifier_opt
2029 (cp_parser *);
2030 static tree cp_parser_function_specifier_opt
2031 (cp_parser *, cp_decl_specifier_seq *);
2032 static tree cp_parser_type_specifier
2033 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2034 int *, bool *);
2035 static tree cp_parser_simple_type_specifier
2036 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2037 static tree cp_parser_type_name
2038 (cp_parser *);
2039 static tree cp_parser_nonclass_name
2040 (cp_parser* parser);
2041 static tree cp_parser_elaborated_type_specifier
2042 (cp_parser *, bool, bool);
2043 static tree cp_parser_enum_specifier
2044 (cp_parser *);
2045 static void cp_parser_enumerator_list
2046 (cp_parser *, tree);
2047 static void cp_parser_enumerator_definition
2048 (cp_parser *, tree);
2049 static tree cp_parser_namespace_name
2050 (cp_parser *);
2051 static void cp_parser_namespace_definition
2052 (cp_parser *);
2053 static void cp_parser_namespace_body
2054 (cp_parser *);
2055 static tree cp_parser_qualified_namespace_specifier
2056 (cp_parser *);
2057 static void cp_parser_namespace_alias_definition
2058 (cp_parser *);
2059 static bool cp_parser_using_declaration
2060 (cp_parser *, bool);
2061 static void cp_parser_using_directive
2062 (cp_parser *);
2063 static tree cp_parser_alias_declaration
2064 (cp_parser *);
2065 static void cp_parser_asm_definition
2066 (cp_parser *);
2067 static void cp_parser_linkage_specification
2068 (cp_parser *);
2069 static void cp_parser_static_assert
2070 (cp_parser *, bool);
2071 static tree cp_parser_decltype
2072 (cp_parser *);
2074 /* Declarators [gram.dcl.decl] */
2076 static tree cp_parser_init_declarator
2077 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2078 static cp_declarator *cp_parser_declarator
2079 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2080 static cp_declarator *cp_parser_direct_declarator
2081 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2082 static enum tree_code cp_parser_ptr_operator
2083 (cp_parser *, tree *, cp_cv_quals *, tree *);
2084 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2085 (cp_parser *);
2086 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2087 (cp_parser *);
2088 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2089 (cp_parser *);
2090 static tree cp_parser_late_return_type_opt
2091 (cp_parser *, cp_declarator *, cp_cv_quals);
2092 static tree cp_parser_declarator_id
2093 (cp_parser *, bool);
2094 static tree cp_parser_type_id
2095 (cp_parser *);
2096 static tree cp_parser_template_type_arg
2097 (cp_parser *);
2098 static tree cp_parser_trailing_type_id (cp_parser *);
2099 static tree cp_parser_type_id_1
2100 (cp_parser *, bool, bool);
2101 static void cp_parser_type_specifier_seq
2102 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2103 static tree cp_parser_parameter_declaration_clause
2104 (cp_parser *);
2105 static tree cp_parser_parameter_declaration_list
2106 (cp_parser *, bool *);
2107 static cp_parameter_declarator *cp_parser_parameter_declaration
2108 (cp_parser *, bool, bool *);
2109 static tree cp_parser_default_argument
2110 (cp_parser *, bool);
2111 static void cp_parser_function_body
2112 (cp_parser *, bool);
2113 static tree cp_parser_initializer
2114 (cp_parser *, bool *, bool *);
2115 static tree cp_parser_initializer_clause
2116 (cp_parser *, bool *);
2117 static tree cp_parser_braced_list
2118 (cp_parser*, bool*);
2119 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2120 (cp_parser *, bool *);
2122 static bool cp_parser_ctor_initializer_opt_and_function_body
2123 (cp_parser *, bool);
2125 static tree cp_parser_late_parsing_omp_declare_simd
2126 (cp_parser *, tree);
2128 static tree synthesize_implicit_template_parm
2129 (cp_parser *);
2130 static tree finish_fully_implicit_template
2131 (cp_parser *, tree);
2133 /* Classes [gram.class] */
2135 static tree cp_parser_class_name
2136 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2137 static tree cp_parser_class_specifier
2138 (cp_parser *);
2139 static tree cp_parser_class_head
2140 (cp_parser *, bool *);
2141 static enum tag_types cp_parser_class_key
2142 (cp_parser *);
2143 static void cp_parser_member_specification_opt
2144 (cp_parser *);
2145 static void cp_parser_member_declaration
2146 (cp_parser *);
2147 static tree cp_parser_pure_specifier
2148 (cp_parser *);
2149 static tree cp_parser_constant_initializer
2150 (cp_parser *);
2152 /* Derived classes [gram.class.derived] */
2154 static tree cp_parser_base_clause
2155 (cp_parser *);
2156 static tree cp_parser_base_specifier
2157 (cp_parser *);
2159 /* Special member functions [gram.special] */
2161 static tree cp_parser_conversion_function_id
2162 (cp_parser *);
2163 static tree cp_parser_conversion_type_id
2164 (cp_parser *);
2165 static cp_declarator *cp_parser_conversion_declarator_opt
2166 (cp_parser *);
2167 static bool cp_parser_ctor_initializer_opt
2168 (cp_parser *);
2169 static void cp_parser_mem_initializer_list
2170 (cp_parser *);
2171 static tree cp_parser_mem_initializer
2172 (cp_parser *);
2173 static tree cp_parser_mem_initializer_id
2174 (cp_parser *);
2176 /* Overloading [gram.over] */
2178 static tree cp_parser_operator_function_id
2179 (cp_parser *);
2180 static tree cp_parser_operator
2181 (cp_parser *);
2183 /* Templates [gram.temp] */
2185 static void cp_parser_template_declaration
2186 (cp_parser *, bool);
2187 static tree cp_parser_template_parameter_list
2188 (cp_parser *);
2189 static tree cp_parser_template_parameter
2190 (cp_parser *, bool *, bool *);
2191 static tree cp_parser_type_parameter
2192 (cp_parser *, bool *);
2193 static tree cp_parser_template_id
2194 (cp_parser *, bool, bool, enum tag_types, bool);
2195 static tree cp_parser_template_name
2196 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2197 static tree cp_parser_template_argument_list
2198 (cp_parser *);
2199 static tree cp_parser_template_argument
2200 (cp_parser *);
2201 static void cp_parser_explicit_instantiation
2202 (cp_parser *);
2203 static void cp_parser_explicit_specialization
2204 (cp_parser *);
2206 /* Exception handling [gram.exception] */
2208 static tree cp_parser_try_block
2209 (cp_parser *);
2210 static bool cp_parser_function_try_block
2211 (cp_parser *);
2212 static void cp_parser_handler_seq
2213 (cp_parser *);
2214 static void cp_parser_handler
2215 (cp_parser *);
2216 static tree cp_parser_exception_declaration
2217 (cp_parser *);
2218 static tree cp_parser_throw_expression
2219 (cp_parser *);
2220 static tree cp_parser_exception_specification_opt
2221 (cp_parser *);
2222 static tree cp_parser_type_id_list
2223 (cp_parser *);
2225 /* GNU Extensions */
2227 static tree cp_parser_asm_specification_opt
2228 (cp_parser *);
2229 static tree cp_parser_asm_operand_list
2230 (cp_parser *);
2231 static tree cp_parser_asm_clobber_list
2232 (cp_parser *);
2233 static tree cp_parser_asm_label_list
2234 (cp_parser *);
2235 static bool cp_next_tokens_can_be_attribute_p
2236 (cp_parser *);
2237 static bool cp_next_tokens_can_be_gnu_attribute_p
2238 (cp_parser *);
2239 static bool cp_next_tokens_can_be_std_attribute_p
2240 (cp_parser *);
2241 static bool cp_nth_tokens_can_be_std_attribute_p
2242 (cp_parser *, size_t);
2243 static bool cp_nth_tokens_can_be_gnu_attribute_p
2244 (cp_parser *, size_t);
2245 static bool cp_nth_tokens_can_be_attribute_p
2246 (cp_parser *, size_t);
2247 static tree cp_parser_attributes_opt
2248 (cp_parser *);
2249 static tree cp_parser_gnu_attributes_opt
2250 (cp_parser *);
2251 static tree cp_parser_gnu_attribute_list
2252 (cp_parser *);
2253 static tree cp_parser_std_attribute
2254 (cp_parser *);
2255 static tree cp_parser_std_attribute_spec
2256 (cp_parser *);
2257 static tree cp_parser_std_attribute_spec_seq
2258 (cp_parser *);
2259 static bool cp_parser_extension_opt
2260 (cp_parser *, int *);
2261 static void cp_parser_label_declaration
2262 (cp_parser *);
2264 /* Concept Extensions */
2266 static tree cp_parser_requires_clause
2267 (cp_parser *);
2268 static tree cp_parser_requires_clause_opt
2269 (cp_parser *);
2270 static tree cp_parser_requires_expression
2271 (cp_parser *);
2272 static tree cp_parser_requirement_parameter_list
2273 (cp_parser *);
2274 static tree cp_parser_requirement_body
2275 (cp_parser *);
2276 static tree cp_parser_requirement_list
2277 (cp_parser *);
2278 static tree cp_parser_requirement
2279 (cp_parser *);
2280 static tree cp_parser_simple_requirement
2281 (cp_parser *);
2282 static tree cp_parser_compound_requirement
2283 (cp_parser *);
2284 static tree cp_parser_type_requirement
2285 (cp_parser *);
2286 static tree cp_parser_nested_requirement
2287 (cp_parser *);
2288 static tree cp_parser_constexpr_constraint_spec
2289 (cp_parser *, tree);
2290 static tree cp_parser_noexcept_constraint_spec
2291 (cp_parser *, tree);
2292 static tree cp_parser_constraint_spec
2293 (cp_parser *, tree);
2294 static tree cp_parser_constraint_specifier_seq
2295 (cp_parser *, tree);
2297 /* Transactional Memory Extensions */
2299 static tree cp_parser_transaction
2300 (cp_parser *, enum rid);
2301 static tree cp_parser_transaction_expression
2302 (cp_parser *, enum rid);
2303 static bool cp_parser_function_transaction
2304 (cp_parser *, enum rid);
2305 static tree cp_parser_transaction_cancel
2306 (cp_parser *);
2308 enum pragma_context {
2309 pragma_external,
2310 pragma_member,
2311 pragma_objc_icode,
2312 pragma_stmt,
2313 pragma_compound
2315 static bool cp_parser_pragma
2316 (cp_parser *, enum pragma_context);
2318 /* Objective-C++ Productions */
2320 static tree cp_parser_objc_message_receiver
2321 (cp_parser *);
2322 static tree cp_parser_objc_message_args
2323 (cp_parser *);
2324 static tree cp_parser_objc_message_expression
2325 (cp_parser *);
2326 static tree cp_parser_objc_encode_expression
2327 (cp_parser *);
2328 static tree cp_parser_objc_defs_expression
2329 (cp_parser *);
2330 static tree cp_parser_objc_protocol_expression
2331 (cp_parser *);
2332 static tree cp_parser_objc_selector_expression
2333 (cp_parser *);
2334 static tree cp_parser_objc_expression
2335 (cp_parser *);
2336 static bool cp_parser_objc_selector_p
2337 (enum cpp_ttype);
2338 static tree cp_parser_objc_selector
2339 (cp_parser *);
2340 static tree cp_parser_objc_protocol_refs_opt
2341 (cp_parser *);
2342 static void cp_parser_objc_declaration
2343 (cp_parser *, tree);
2344 static tree cp_parser_objc_statement
2345 (cp_parser *);
2346 static bool cp_parser_objc_valid_prefix_attributes
2347 (cp_parser *, tree *);
2348 static void cp_parser_objc_at_property_declaration
2349 (cp_parser *) ;
2350 static void cp_parser_objc_at_synthesize_declaration
2351 (cp_parser *) ;
2352 static void cp_parser_objc_at_dynamic_declaration
2353 (cp_parser *) ;
2354 static tree cp_parser_objc_struct_declaration
2355 (cp_parser *) ;
2357 /* Utility Routines */
2359 static tree cp_parser_lookup_name
2360 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2361 static tree cp_parser_lookup_name_simple
2362 (cp_parser *, tree, location_t);
2363 static tree cp_parser_maybe_treat_template_as_class
2364 (tree, bool);
2365 static bool cp_parser_check_declarator_template_parameters
2366 (cp_parser *, cp_declarator *, location_t);
2367 static bool cp_parser_check_template_parameters
2368 (cp_parser *, unsigned, location_t, cp_declarator *);
2369 static tree cp_parser_simple_cast_expression
2370 (cp_parser *);
2371 static tree cp_parser_global_scope_opt
2372 (cp_parser *, bool);
2373 static bool cp_parser_constructor_declarator_p
2374 (cp_parser *, bool);
2375 static tree cp_parser_function_definition_from_specifiers_and_declarator
2376 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2377 static tree cp_parser_function_definition_after_declarator
2378 (cp_parser *, bool);
2379 static void cp_parser_template_declaration_after_export
2380 (cp_parser *, bool);
2381 static void cp_parser_perform_template_parameter_access_checks
2382 (vec<deferred_access_check, va_gc> *);
2383 static tree cp_parser_single_declaration
2384 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2385 static tree cp_parser_functional_cast
2386 (cp_parser *, tree);
2387 static tree cp_parser_save_member_function_body
2388 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2389 static tree cp_parser_save_nsdmi
2390 (cp_parser *);
2391 static tree cp_parser_enclosed_template_argument_list
2392 (cp_parser *);
2393 static void cp_parser_save_default_args
2394 (cp_parser *, tree);
2395 static void cp_parser_late_parsing_for_member
2396 (cp_parser *, tree);
2397 static tree cp_parser_late_parse_one_default_arg
2398 (cp_parser *, tree, tree, tree);
2399 static void cp_parser_late_parsing_nsdmi
2400 (cp_parser *, tree);
2401 static void cp_parser_late_parsing_default_args
2402 (cp_parser *, tree);
2403 static tree cp_parser_sizeof_operand
2404 (cp_parser *, enum rid);
2405 static tree cp_parser_trait_expr
2406 (cp_parser *, enum rid);
2407 static bool cp_parser_declares_only_class_p
2408 (cp_parser *);
2409 static void cp_parser_set_storage_class
2410 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2411 static void cp_parser_set_decl_spec_type
2412 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2413 static void set_and_check_decl_spec_loc
2414 (cp_decl_specifier_seq *decl_specs,
2415 cp_decl_spec ds, cp_token *);
2416 static bool cp_parser_friend_p
2417 (const cp_decl_specifier_seq *);
2418 static void cp_parser_required_error
2419 (cp_parser *, required_token, bool);
2420 static cp_token *cp_parser_require
2421 (cp_parser *, enum cpp_ttype, required_token);
2422 static cp_token *cp_parser_require_keyword
2423 (cp_parser *, enum rid, required_token);
2424 static bool cp_parser_token_starts_function_definition_p
2425 (cp_token *);
2426 static bool cp_parser_next_token_starts_class_definition_p
2427 (cp_parser *);
2428 static bool cp_parser_next_token_ends_template_argument_p
2429 (cp_parser *);
2430 static bool cp_parser_nth_token_starts_template_argument_list_p
2431 (cp_parser *, size_t);
2432 static enum tag_types cp_parser_token_is_class_key
2433 (cp_token *);
2434 static void cp_parser_check_class_key
2435 (enum tag_types, tree type);
2436 static void cp_parser_check_access_in_redeclaration
2437 (tree type, location_t location);
2438 static bool cp_parser_optional_template_keyword
2439 (cp_parser *);
2440 static void cp_parser_pre_parsed_nested_name_specifier
2441 (cp_parser *);
2442 static bool cp_parser_cache_group
2443 (cp_parser *, enum cpp_ttype, unsigned);
2444 static tree cp_parser_cache_defarg
2445 (cp_parser *parser, bool nsdmi);
2446 static void cp_parser_parse_tentatively
2447 (cp_parser *);
2448 static void cp_parser_commit_to_tentative_parse
2449 (cp_parser *);
2450 static void cp_parser_commit_to_topmost_tentative_parse
2451 (cp_parser *);
2452 static void cp_parser_abort_tentative_parse
2453 (cp_parser *);
2454 static bool cp_parser_parse_definitely
2455 (cp_parser *);
2456 static inline bool cp_parser_parsing_tentatively
2457 (cp_parser *);
2458 static bool cp_parser_uncommitted_to_tentative_parse_p
2459 (cp_parser *);
2460 static void cp_parser_error
2461 (cp_parser *, const char *);
2462 static void cp_parser_name_lookup_error
2463 (cp_parser *, tree, tree, name_lookup_error, location_t);
2464 static bool cp_parser_simulate_error
2465 (cp_parser *);
2466 static bool cp_parser_check_type_definition
2467 (cp_parser *);
2468 static void cp_parser_check_for_definition_in_return_type
2469 (cp_declarator *, tree, location_t type_location);
2470 static void cp_parser_check_for_invalid_template_id
2471 (cp_parser *, tree, enum tag_types, location_t location);
2472 static bool cp_parser_non_integral_constant_expression
2473 (cp_parser *, non_integral_constant);
2474 static void cp_parser_diagnose_invalid_type_name
2475 (cp_parser *, tree, tree, location_t);
2476 static bool cp_parser_parse_and_diagnose_invalid_type_name
2477 (cp_parser *);
2478 static int cp_parser_skip_to_closing_parenthesis
2479 (cp_parser *, bool, bool, bool);
2480 static void cp_parser_skip_to_end_of_statement
2481 (cp_parser *);
2482 static void cp_parser_consume_semicolon_at_end_of_statement
2483 (cp_parser *);
2484 static void cp_parser_skip_to_end_of_block_or_statement
2485 (cp_parser *);
2486 static bool cp_parser_skip_to_closing_brace
2487 (cp_parser *);
2488 static void cp_parser_skip_to_end_of_template_parameter_list
2489 (cp_parser *);
2490 static void cp_parser_skip_to_pragma_eol
2491 (cp_parser*, cp_token *);
2492 static bool cp_parser_error_occurred
2493 (cp_parser *);
2494 static bool cp_parser_allow_gnu_extensions_p
2495 (cp_parser *);
2496 static bool cp_parser_is_pure_string_literal
2497 (cp_token *);
2498 static bool cp_parser_is_string_literal
2499 (cp_token *);
2500 static bool cp_parser_is_keyword
2501 (cp_token *, enum rid);
2502 static tree cp_parser_make_typename_type
2503 (cp_parser *, tree, tree, location_t location);
2504 static cp_declarator * cp_parser_make_indirect_declarator
2505 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2509 // -------------------------------------------------------------------------- //
2510 // Unevaluated Operand Guard
2512 // Implementation of an RAII helper for unevaluated operand parsing.
2513 cp_unevaluated::cp_unevaluated ()
2515 ++cp_unevaluated_operand;
2516 ++c_inhibit_evaluation_warnings;
2519 cp_unevaluated::~cp_unevaluated ()
2521 --c_inhibit_evaluation_warnings;
2522 --cp_unevaluated_operand;
2525 // -------------------------------------------------------------------------- //
2526 // Tentative Parsing
2528 /* Returns nonzero if we are parsing tentatively. */
2530 static inline bool
2531 cp_parser_parsing_tentatively (cp_parser* parser)
2533 return parser->context->next != NULL;
2536 /* Returns nonzero if TOKEN is a string literal. */
2538 static bool
2539 cp_parser_is_pure_string_literal (cp_token* token)
2541 return (token->type == CPP_STRING ||
2542 token->type == CPP_STRING16 ||
2543 token->type == CPP_STRING32 ||
2544 token->type == CPP_WSTRING ||
2545 token->type == CPP_UTF8STRING);
2548 /* Returns nonzero if TOKEN is a string literal
2549 of a user-defined string literal. */
2551 static bool
2552 cp_parser_is_string_literal (cp_token* token)
2554 return (cp_parser_is_pure_string_literal (token) ||
2555 token->type == CPP_STRING_USERDEF ||
2556 token->type == CPP_STRING16_USERDEF ||
2557 token->type == CPP_STRING32_USERDEF ||
2558 token->type == CPP_WSTRING_USERDEF ||
2559 token->type == CPP_UTF8STRING_USERDEF);
2562 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2564 static bool
2565 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2567 return token->keyword == keyword;
2570 /* If not parsing tentatively, issue a diagnostic of the form
2571 FILE:LINE: MESSAGE before TOKEN
2572 where TOKEN is the next token in the input stream. MESSAGE
2573 (specified by the caller) is usually of the form "expected
2574 OTHER-TOKEN". */
2576 static void
2577 cp_parser_error (cp_parser* parser, const char* gmsgid)
2579 if (!cp_parser_simulate_error (parser))
2581 cp_token *token = cp_lexer_peek_token (parser->lexer);
2582 /* This diagnostic makes more sense if it is tagged to the line
2583 of the token we just peeked at. */
2584 cp_lexer_set_source_position_from_token (token);
2586 if (token->type == CPP_PRAGMA)
2588 error_at (token->location,
2589 "%<#pragma%> is not allowed here");
2590 cp_parser_skip_to_pragma_eol (parser, token);
2591 return;
2594 c_parse_error (gmsgid,
2595 /* Because c_parser_error does not understand
2596 CPP_KEYWORD, keywords are treated like
2597 identifiers. */
2598 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2599 token->u.value, token->flags);
2603 /* Issue an error about name-lookup failing. NAME is the
2604 IDENTIFIER_NODE DECL is the result of
2605 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2606 the thing that we hoped to find. */
2608 static void
2609 cp_parser_name_lookup_error (cp_parser* parser,
2610 tree name,
2611 tree decl,
2612 name_lookup_error desired,
2613 location_t location)
2615 /* If name lookup completely failed, tell the user that NAME was not
2616 declared. */
2617 if (decl == error_mark_node)
2619 if (parser->scope && parser->scope != global_namespace)
2620 error_at (location, "%<%E::%E%> has not been declared",
2621 parser->scope, name);
2622 else if (parser->scope == global_namespace)
2623 error_at (location, "%<::%E%> has not been declared", name);
2624 else if (parser->object_scope
2625 && !CLASS_TYPE_P (parser->object_scope))
2626 error_at (location, "request for member %qE in non-class type %qT",
2627 name, parser->object_scope);
2628 else if (parser->object_scope)
2629 error_at (location, "%<%T::%E%> has not been declared",
2630 parser->object_scope, name);
2631 else
2632 error_at (location, "%qE has not been declared", name);
2634 else if (parser->scope && parser->scope != global_namespace)
2636 switch (desired)
2638 case NLE_TYPE:
2639 error_at (location, "%<%E::%E%> is not a type",
2640 parser->scope, name);
2641 break;
2642 case NLE_CXX98:
2643 error_at (location, "%<%E::%E%> is not a class or namespace",
2644 parser->scope, name);
2645 break;
2646 case NLE_NOT_CXX98:
2647 error_at (location,
2648 "%<%E::%E%> is not a class, namespace, or enumeration",
2649 parser->scope, name);
2650 break;
2651 default:
2652 gcc_unreachable ();
2656 else if (parser->scope == global_namespace)
2658 switch (desired)
2660 case NLE_TYPE:
2661 error_at (location, "%<::%E%> is not a type", name);
2662 break;
2663 case NLE_CXX98:
2664 error_at (location, "%<::%E%> is not a class or namespace", name);
2665 break;
2666 case NLE_NOT_CXX98:
2667 error_at (location,
2668 "%<::%E%> is not a class, namespace, or enumeration",
2669 name);
2670 break;
2671 default:
2672 gcc_unreachable ();
2675 else
2677 switch (desired)
2679 case NLE_TYPE:
2680 error_at (location, "%qE is not a type", name);
2681 break;
2682 case NLE_CXX98:
2683 error_at (location, "%qE is not a class or namespace", name);
2684 break;
2685 case NLE_NOT_CXX98:
2686 error_at (location,
2687 "%qE is not a class, namespace, or enumeration", name);
2688 break;
2689 default:
2690 gcc_unreachable ();
2695 /* If we are parsing tentatively, remember that an error has occurred
2696 during this tentative parse. Returns true if the error was
2697 simulated; false if a message should be issued by the caller. */
2699 static bool
2700 cp_parser_simulate_error (cp_parser* parser)
2702 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2704 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2705 return true;
2707 return false;
2710 /* This function is called when a type is defined. If type
2711 definitions are forbidden at this point, an error message is
2712 issued. */
2714 static bool
2715 cp_parser_check_type_definition (cp_parser* parser)
2717 /* If types are forbidden here, issue a message. */
2718 if (parser->type_definition_forbidden_message)
2720 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2721 in the message need to be interpreted. */
2722 error (parser->type_definition_forbidden_message);
2723 return false;
2725 return true;
2728 /* This function is called when the DECLARATOR is processed. The TYPE
2729 was a type defined in the decl-specifiers. If it is invalid to
2730 define a type in the decl-specifiers for DECLARATOR, an error is
2731 issued. TYPE_LOCATION is the location of TYPE and is used
2732 for error reporting. */
2734 static void
2735 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2736 tree type, location_t type_location)
2738 /* [dcl.fct] forbids type definitions in return types.
2739 Unfortunately, it's not easy to know whether or not we are
2740 processing a return type until after the fact. */
2741 while (declarator
2742 && (declarator->kind == cdk_pointer
2743 || declarator->kind == cdk_reference
2744 || declarator->kind == cdk_ptrmem))
2745 declarator = declarator->declarator;
2746 if (declarator
2747 && declarator->kind == cdk_function)
2749 error_at (type_location,
2750 "new types may not be defined in a return type");
2751 inform (type_location,
2752 "(perhaps a semicolon is missing after the definition of %qT)",
2753 type);
2757 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2758 "<" in any valid C++ program. If the next token is indeed "<",
2759 issue a message warning the user about what appears to be an
2760 invalid attempt to form a template-id. LOCATION is the location
2761 of the type-specifier (TYPE) */
2763 static void
2764 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2765 tree type,
2766 enum tag_types tag_type,
2767 location_t location)
2769 cp_token_position start = 0;
2771 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2773 if (TYPE_P (type))
2774 error_at (location, "%qT is not a template", type);
2775 else if (identifier_p (type))
2777 if (tag_type != none_type)
2778 error_at (location, "%qE is not a class template", type);
2779 else
2780 error_at (location, "%qE is not a template", type);
2782 else
2783 error_at (location, "invalid template-id");
2784 /* Remember the location of the invalid "<". */
2785 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2786 start = cp_lexer_token_position (parser->lexer, true);
2787 /* Consume the "<". */
2788 cp_lexer_consume_token (parser->lexer);
2789 /* Parse the template arguments. */
2790 cp_parser_enclosed_template_argument_list (parser);
2791 /* Permanently remove the invalid template arguments so that
2792 this error message is not issued again. */
2793 if (start)
2794 cp_lexer_purge_tokens_after (parser->lexer, start);
2798 /* If parsing an integral constant-expression, issue an error message
2799 about the fact that THING appeared and return true. Otherwise,
2800 return false. In either case, set
2801 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2803 static bool
2804 cp_parser_non_integral_constant_expression (cp_parser *parser,
2805 non_integral_constant thing)
2807 parser->non_integral_constant_expression_p = true;
2808 if (parser->integral_constant_expression_p)
2810 if (!parser->allow_non_integral_constant_expression_p)
2812 const char *msg = NULL;
2813 switch (thing)
2815 case NIC_FLOAT:
2816 error ("floating-point literal "
2817 "cannot appear in a constant-expression");
2818 return true;
2819 case NIC_CAST:
2820 error ("a cast to a type other than an integral or "
2821 "enumeration type cannot appear in a "
2822 "constant-expression");
2823 return true;
2824 case NIC_TYPEID:
2825 error ("%<typeid%> operator "
2826 "cannot appear in a constant-expression");
2827 return true;
2828 case NIC_NCC:
2829 error ("non-constant compound literals "
2830 "cannot appear in a constant-expression");
2831 return true;
2832 case NIC_FUNC_CALL:
2833 error ("a function call "
2834 "cannot appear in a constant-expression");
2835 return true;
2836 case NIC_INC:
2837 error ("an increment "
2838 "cannot appear in a constant-expression");
2839 return true;
2840 case NIC_DEC:
2841 error ("an decrement "
2842 "cannot appear in a constant-expression");
2843 return true;
2844 case NIC_ARRAY_REF:
2845 error ("an array reference "
2846 "cannot appear in a constant-expression");
2847 return true;
2848 case NIC_ADDR_LABEL:
2849 error ("the address of a label "
2850 "cannot appear in a constant-expression");
2851 return true;
2852 case NIC_OVERLOADED:
2853 error ("calls to overloaded operators "
2854 "cannot appear in a constant-expression");
2855 return true;
2856 case NIC_ASSIGNMENT:
2857 error ("an assignment cannot appear in a constant-expression");
2858 return true;
2859 case NIC_COMMA:
2860 error ("a comma operator "
2861 "cannot appear in a constant-expression");
2862 return true;
2863 case NIC_CONSTRUCTOR:
2864 error ("a call to a constructor "
2865 "cannot appear in a constant-expression");
2866 return true;
2867 case NIC_TRANSACTION:
2868 error ("a transaction expression "
2869 "cannot appear in a constant-expression");
2870 return true;
2871 case NIC_THIS:
2872 msg = "this";
2873 break;
2874 case NIC_FUNC_NAME:
2875 msg = "__FUNCTION__";
2876 break;
2877 case NIC_PRETTY_FUNC:
2878 msg = "__PRETTY_FUNCTION__";
2879 break;
2880 case NIC_C99_FUNC:
2881 msg = "__func__";
2882 break;
2883 case NIC_VA_ARG:
2884 msg = "va_arg";
2885 break;
2886 case NIC_ARROW:
2887 msg = "->";
2888 break;
2889 case NIC_POINT:
2890 msg = ".";
2891 break;
2892 case NIC_STAR:
2893 msg = "*";
2894 break;
2895 case NIC_ADDR:
2896 msg = "&";
2897 break;
2898 case NIC_PREINCREMENT:
2899 msg = "++";
2900 break;
2901 case NIC_PREDECREMENT:
2902 msg = "--";
2903 break;
2904 case NIC_NEW:
2905 msg = "new";
2906 break;
2907 case NIC_DEL:
2908 msg = "delete";
2909 break;
2910 default:
2911 gcc_unreachable ();
2913 if (msg)
2914 error ("%qs cannot appear in a constant-expression", msg);
2915 return true;
2918 return false;
2921 /* Emit a diagnostic for an invalid type name. SCOPE is the
2922 qualifying scope (or NULL, if none) for ID. This function commits
2923 to the current active tentative parse, if any. (Otherwise, the
2924 problematic construct might be encountered again later, resulting
2925 in duplicate error messages.) LOCATION is the location of ID. */
2927 static void
2928 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2929 tree scope, tree id,
2930 location_t location)
2932 tree decl, old_scope;
2933 cp_parser_commit_to_tentative_parse (parser);
2934 /* Try to lookup the identifier. */
2935 old_scope = parser->scope;
2936 parser->scope = scope;
2937 decl = cp_parser_lookup_name_simple (parser, id, location);
2938 parser->scope = old_scope;
2939 /* If the lookup found a template-name, it means that the user forgot
2940 to specify an argument list. Emit a useful error message. */
2941 if (TREE_CODE (decl) == TEMPLATE_DECL)
2942 error_at (location,
2943 "invalid use of template-name %qE without an argument list",
2944 decl);
2945 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2946 error_at (location, "invalid use of destructor %qD as a type", id);
2947 else if (TREE_CODE (decl) == TYPE_DECL)
2948 /* Something like 'unsigned A a;' */
2949 error_at (location, "invalid combination of multiple type-specifiers");
2950 else if (!parser->scope)
2952 /* Issue an error message. */
2953 error_at (location, "%qE does not name a type", id);
2954 /* If we're in a template class, it's possible that the user was
2955 referring to a type from a base class. For example:
2957 template <typename T> struct A { typedef T X; };
2958 template <typename T> struct B : public A<T> { X x; };
2960 The user should have said "typename A<T>::X". */
2961 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2962 inform (location, "C++11 %<constexpr%> only available with "
2963 "-std=c++11 or -std=gnu++11");
2964 else if (processing_template_decl && current_class_type
2965 && TYPE_BINFO (current_class_type))
2967 tree b;
2969 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2971 b = TREE_CHAIN (b))
2973 tree base_type = BINFO_TYPE (b);
2974 if (CLASS_TYPE_P (base_type)
2975 && dependent_type_p (base_type))
2977 tree field;
2978 /* Go from a particular instantiation of the
2979 template (which will have an empty TYPE_FIELDs),
2980 to the main version. */
2981 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2982 for (field = TYPE_FIELDS (base_type);
2983 field;
2984 field = DECL_CHAIN (field))
2985 if (TREE_CODE (field) == TYPE_DECL
2986 && DECL_NAME (field) == id)
2988 inform (location,
2989 "(perhaps %<typename %T::%E%> was intended)",
2990 BINFO_TYPE (b), id);
2991 break;
2993 if (field)
2994 break;
2999 /* Here we diagnose qualified-ids where the scope is actually correct,
3000 but the identifier does not resolve to a valid type name. */
3001 else if (parser->scope != error_mark_node)
3003 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3005 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3006 error_at (location_of (id),
3007 "%qE in namespace %qE does not name a template type",
3008 id, parser->scope);
3009 else
3010 error_at (location_of (id),
3011 "%qE in namespace %qE does not name a type",
3012 id, parser->scope);
3014 else if (CLASS_TYPE_P (parser->scope)
3015 && constructor_name_p (id, parser->scope))
3017 /* A<T>::A<T>() */
3018 error_at (location, "%<%T::%E%> names the constructor, not"
3019 " the type", parser->scope, id);
3020 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3021 error_at (location, "and %qT has no template constructors",
3022 parser->scope);
3024 else if (TYPE_P (parser->scope)
3025 && dependent_scope_p (parser->scope))
3026 error_at (location, "need %<typename%> before %<%T::%E%> because "
3027 "%qT is a dependent scope",
3028 parser->scope, id, parser->scope);
3029 else if (TYPE_P (parser->scope))
3031 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3032 error_at (location_of (id),
3033 "%qE in %q#T does not name a template type",
3034 id, parser->scope);
3035 else
3036 error_at (location_of (id),
3037 "%qE in %q#T does not name a type",
3038 id, parser->scope);
3040 else
3041 gcc_unreachable ();
3045 /* Check for a common situation where a type-name should be present,
3046 but is not, and issue a sensible error message. Returns true if an
3047 invalid type-name was detected.
3049 The situation handled by this function are variable declarations of the
3050 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3051 Usually, `ID' should name a type, but if we got here it means that it
3052 does not. We try to emit the best possible error message depending on
3053 how exactly the id-expression looks like. */
3055 static bool
3056 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3058 tree id;
3059 cp_token *token = cp_lexer_peek_token (parser->lexer);
3061 /* Avoid duplicate error about ambiguous lookup. */
3062 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3064 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3065 if (next->type == CPP_NAME && next->ambiguous_p)
3066 goto out;
3069 cp_parser_parse_tentatively (parser);
3070 id = cp_parser_id_expression (parser,
3071 /*template_keyword_p=*/false,
3072 /*check_dependency_p=*/true,
3073 /*template_p=*/NULL,
3074 /*declarator_p=*/true,
3075 /*optional_p=*/false);
3076 /* If the next token is a (, this is a function with no explicit return
3077 type, i.e. constructor, destructor or conversion op. */
3078 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3079 || TREE_CODE (id) == TYPE_DECL)
3081 cp_parser_abort_tentative_parse (parser);
3082 return false;
3084 if (!cp_parser_parse_definitely (parser))
3085 return false;
3087 /* Emit a diagnostic for the invalid type. */
3088 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3089 id, token->location);
3090 out:
3091 /* If we aren't in the middle of a declarator (i.e. in a
3092 parameter-declaration-clause), skip to the end of the declaration;
3093 there's no point in trying to process it. */
3094 if (!parser->in_declarator_p)
3095 cp_parser_skip_to_end_of_block_or_statement (parser);
3096 return true;
3099 /* Consume tokens up to, and including, the next non-nested closing `)'.
3100 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3101 are doing error recovery. Returns -1 if OR_COMMA is true and we
3102 found an unnested comma. */
3104 static int
3105 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3106 bool recovering,
3107 bool or_comma,
3108 bool consume_paren)
3110 unsigned paren_depth = 0;
3111 unsigned brace_depth = 0;
3112 unsigned square_depth = 0;
3114 if (recovering && !or_comma
3115 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3116 return 0;
3118 while (true)
3120 cp_token * token = cp_lexer_peek_token (parser->lexer);
3122 switch (token->type)
3124 case CPP_EOF:
3125 case CPP_PRAGMA_EOL:
3126 /* If we've run out of tokens, then there is no closing `)'. */
3127 return 0;
3129 /* This is good for lambda expression capture-lists. */
3130 case CPP_OPEN_SQUARE:
3131 ++square_depth;
3132 break;
3133 case CPP_CLOSE_SQUARE:
3134 if (!square_depth--)
3135 return 0;
3136 break;
3138 case CPP_SEMICOLON:
3139 /* This matches the processing in skip_to_end_of_statement. */
3140 if (!brace_depth)
3141 return 0;
3142 break;
3144 case CPP_OPEN_BRACE:
3145 ++brace_depth;
3146 break;
3147 case CPP_CLOSE_BRACE:
3148 if (!brace_depth--)
3149 return 0;
3150 break;
3152 case CPP_COMMA:
3153 if (recovering && or_comma && !brace_depth && !paren_depth
3154 && !square_depth)
3155 return -1;
3156 break;
3158 case CPP_OPEN_PAREN:
3159 if (!brace_depth)
3160 ++paren_depth;
3161 break;
3163 case CPP_CLOSE_PAREN:
3164 if (!brace_depth && !paren_depth--)
3166 if (consume_paren)
3167 cp_lexer_consume_token (parser->lexer);
3168 return 1;
3170 break;
3172 default:
3173 break;
3176 /* Consume the token. */
3177 cp_lexer_consume_token (parser->lexer);
3181 /* Consume tokens until we reach the end of the current statement.
3182 Normally, that will be just before consuming a `;'. However, if a
3183 non-nested `}' comes first, then we stop before consuming that. */
3185 static void
3186 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3188 unsigned nesting_depth = 0;
3190 while (true)
3192 cp_token *token = cp_lexer_peek_token (parser->lexer);
3194 switch (token->type)
3196 case CPP_EOF:
3197 case CPP_PRAGMA_EOL:
3198 /* If we've run out of tokens, stop. */
3199 return;
3201 case CPP_SEMICOLON:
3202 /* If the next token is a `;', we have reached the end of the
3203 statement. */
3204 if (!nesting_depth)
3205 return;
3206 break;
3208 case CPP_CLOSE_BRACE:
3209 /* If this is a non-nested '}', stop before consuming it.
3210 That way, when confronted with something like:
3212 { 3 + }
3214 we stop before consuming the closing '}', even though we
3215 have not yet reached a `;'. */
3216 if (nesting_depth == 0)
3217 return;
3219 /* If it is the closing '}' for a block that we have
3220 scanned, stop -- but only after consuming the token.
3221 That way given:
3223 void f g () { ... }
3224 typedef int I;
3226 we will stop after the body of the erroneously declared
3227 function, but before consuming the following `typedef'
3228 declaration. */
3229 if (--nesting_depth == 0)
3231 cp_lexer_consume_token (parser->lexer);
3232 return;
3235 case CPP_OPEN_BRACE:
3236 ++nesting_depth;
3237 break;
3239 default:
3240 break;
3243 /* Consume the token. */
3244 cp_lexer_consume_token (parser->lexer);
3248 /* This function is called at the end of a statement or declaration.
3249 If the next token is a semicolon, it is consumed; otherwise, error
3250 recovery is attempted. */
3252 static void
3253 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3255 /* Look for the trailing `;'. */
3256 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3258 /* If there is additional (erroneous) input, skip to the end of
3259 the statement. */
3260 cp_parser_skip_to_end_of_statement (parser);
3261 /* If the next token is now a `;', consume it. */
3262 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3263 cp_lexer_consume_token (parser->lexer);
3267 /* Skip tokens until we have consumed an entire block, or until we
3268 have consumed a non-nested `;'. */
3270 static void
3271 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3273 int nesting_depth = 0;
3275 while (nesting_depth >= 0)
3277 cp_token *token = cp_lexer_peek_token (parser->lexer);
3279 switch (token->type)
3281 case CPP_EOF:
3282 case CPP_PRAGMA_EOL:
3283 /* If we've run out of tokens, stop. */
3284 return;
3286 case CPP_SEMICOLON:
3287 /* Stop if this is an unnested ';'. */
3288 if (!nesting_depth)
3289 nesting_depth = -1;
3290 break;
3292 case CPP_CLOSE_BRACE:
3293 /* Stop if this is an unnested '}', or closes the outermost
3294 nesting level. */
3295 nesting_depth--;
3296 if (nesting_depth < 0)
3297 return;
3298 if (!nesting_depth)
3299 nesting_depth = -1;
3300 break;
3302 case CPP_OPEN_BRACE:
3303 /* Nest. */
3304 nesting_depth++;
3305 break;
3307 default:
3308 break;
3311 /* Consume the token. */
3312 cp_lexer_consume_token (parser->lexer);
3316 /* Skip tokens until a non-nested closing curly brace is the next
3317 token, or there are no more tokens. Return true in the first case,
3318 false otherwise. */
3320 static bool
3321 cp_parser_skip_to_closing_brace (cp_parser *parser)
3323 unsigned nesting_depth = 0;
3325 while (true)
3327 cp_token *token = cp_lexer_peek_token (parser->lexer);
3329 switch (token->type)
3331 case CPP_EOF:
3332 case CPP_PRAGMA_EOL:
3333 /* If we've run out of tokens, stop. */
3334 return false;
3336 case CPP_CLOSE_BRACE:
3337 /* If the next token is a non-nested `}', then we have reached
3338 the end of the current block. */
3339 if (nesting_depth-- == 0)
3340 return true;
3341 break;
3343 case CPP_OPEN_BRACE:
3344 /* If it the next token is a `{', then we are entering a new
3345 block. Consume the entire block. */
3346 ++nesting_depth;
3347 break;
3349 default:
3350 break;
3353 /* Consume the token. */
3354 cp_lexer_consume_token (parser->lexer);
3358 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3359 parameter is the PRAGMA token, allowing us to purge the entire pragma
3360 sequence. */
3362 static void
3363 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3365 cp_token *token;
3367 parser->lexer->in_pragma = false;
3370 token = cp_lexer_consume_token (parser->lexer);
3371 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3373 /* Ensure that the pragma is not parsed again. */
3374 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3377 /* Require pragma end of line, resyncing with it as necessary. The
3378 arguments are as for cp_parser_skip_to_pragma_eol. */
3380 static void
3381 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3383 parser->lexer->in_pragma = false;
3384 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3385 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3388 /* This is a simple wrapper around make_typename_type. When the id is
3389 an unresolved identifier node, we can provide a superior diagnostic
3390 using cp_parser_diagnose_invalid_type_name. */
3392 static tree
3393 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3394 tree id, location_t id_location)
3396 tree result;
3397 if (identifier_p (id))
3399 result = make_typename_type (scope, id, typename_type,
3400 /*complain=*/tf_none);
3401 if (result == error_mark_node)
3402 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3403 return result;
3405 return make_typename_type (scope, id, typename_type, tf_error);
3408 /* This is a wrapper around the
3409 make_{pointer,ptrmem,reference}_declarator functions that decides
3410 which one to call based on the CODE and CLASS_TYPE arguments. The
3411 CODE argument should be one of the values returned by
3412 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3413 appertain to the pointer or reference. */
3415 static cp_declarator *
3416 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3417 cp_cv_quals cv_qualifiers,
3418 cp_declarator *target,
3419 tree attributes)
3421 if (code == ERROR_MARK)
3422 return cp_error_declarator;
3424 if (code == INDIRECT_REF)
3425 if (class_type == NULL_TREE)
3426 return make_pointer_declarator (cv_qualifiers, target, attributes);
3427 else
3428 return make_ptrmem_declarator (cv_qualifiers, class_type,
3429 target, attributes);
3430 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3431 return make_reference_declarator (cv_qualifiers, target,
3432 false, attributes);
3433 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3434 return make_reference_declarator (cv_qualifiers, target,
3435 true, attributes);
3436 gcc_unreachable ();
3439 /* Create a new C++ parser. */
3441 static cp_parser *
3442 cp_parser_new (void)
3444 cp_parser *parser;
3445 cp_lexer *lexer;
3446 unsigned i;
3448 /* cp_lexer_new_main is called before doing GC allocation because
3449 cp_lexer_new_main might load a PCH file. */
3450 lexer = cp_lexer_new_main ();
3452 /* Initialize the binops_by_token so that we can get the tree
3453 directly from the token. */
3454 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3455 binops_by_token[binops[i].token_type] = binops[i];
3457 parser = ggc_alloc_cleared_cp_parser ();
3458 parser->lexer = lexer;
3459 parser->context = cp_parser_context_new (NULL);
3461 /* For now, we always accept GNU extensions. */
3462 parser->allow_gnu_extensions_p = 1;
3464 /* The `>' token is a greater-than operator, not the end of a
3465 template-id. */
3466 parser->greater_than_is_operator_p = true;
3468 parser->default_arg_ok_p = true;
3470 /* We are not parsing a constant-expression. */
3471 parser->integral_constant_expression_p = false;
3472 parser->allow_non_integral_constant_expression_p = false;
3473 parser->non_integral_constant_expression_p = false;
3475 /* Local variable names are not forbidden. */
3476 parser->local_variables_forbidden_p = false;
3478 /* We are not processing an `extern "C"' declaration. */
3479 parser->in_unbraced_linkage_specification_p = false;
3481 /* We are not processing a declarator. */
3482 parser->in_declarator_p = false;
3484 /* We are not processing a template-argument-list. */
3485 parser->in_template_argument_list_p = false;
3487 /* We are not in an iteration statement. */
3488 parser->in_statement = 0;
3490 /* We are not in a switch statement. */
3491 parser->in_switch_statement_p = false;
3493 /* We are not parsing a type-id inside an expression. */
3494 parser->in_type_id_in_expr_p = false;
3496 /* Declarations aren't implicitly extern "C". */
3497 parser->implicit_extern_c = false;
3499 /* String literals should be translated to the execution character set. */
3500 parser->translate_strings_p = true;
3502 /* We are not parsing a function body. */
3503 parser->in_function_body = false;
3505 /* We can correct until told otherwise. */
3506 parser->colon_corrects_to_scope_p = true;
3508 /* The unparsed function queue is empty. */
3509 push_unparsed_function_queues (parser);
3511 /* There are no classes being defined. */
3512 parser->num_classes_being_defined = 0;
3514 /* No template parameters apply. */
3515 parser->num_template_parameter_lists = 0;
3517 /* Not declaring an implicit function template. */
3518 parser->auto_is_implicit_function_template_parm_p = false;
3519 parser->fully_implicit_function_template_p = false;
3520 parser->implicit_template_parms = 0;
3521 parser->implicit_template_scope = 0;
3523 return parser;
3526 /* Create a cp_lexer structure which will emit the tokens in CACHE
3527 and push it onto the parser's lexer stack. This is used for delayed
3528 parsing of in-class method bodies and default arguments, and should
3529 not be confused with tentative parsing. */
3530 static void
3531 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3533 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3534 lexer->next = parser->lexer;
3535 parser->lexer = lexer;
3537 /* Move the current source position to that of the first token in the
3538 new lexer. */
3539 cp_lexer_set_source_position_from_token (lexer->next_token);
3542 /* Pop the top lexer off the parser stack. This is never used for the
3543 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3544 static void
3545 cp_parser_pop_lexer (cp_parser *parser)
3547 cp_lexer *lexer = parser->lexer;
3548 parser->lexer = lexer->next;
3549 cp_lexer_destroy (lexer);
3551 /* Put the current source position back where it was before this
3552 lexer was pushed. */
3553 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3556 /* Lexical conventions [gram.lex] */
3558 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3559 identifier. */
3561 static tree
3562 cp_parser_identifier (cp_parser* parser)
3564 cp_token *token;
3566 /* Look for the identifier. */
3567 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3568 /* Return the value. */
3569 return token ? token->u.value : error_mark_node;
3572 /* Parse a sequence of adjacent string constants. Returns a
3573 TREE_STRING representing the combined, nul-terminated string
3574 constant. If TRANSLATE is true, translate the string to the
3575 execution character set. If WIDE_OK is true, a wide string is
3576 invalid here.
3578 C++98 [lex.string] says that if a narrow string literal token is
3579 adjacent to a wide string literal token, the behavior is undefined.
3580 However, C99 6.4.5p4 says that this results in a wide string literal.
3581 We follow C99 here, for consistency with the C front end.
3583 This code is largely lifted from lex_string() in c-lex.c.
3585 FUTURE: ObjC++ will need to handle @-strings here. */
3586 static tree
3587 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3589 tree value;
3590 size_t count;
3591 struct obstack str_ob;
3592 cpp_string str, istr, *strs;
3593 cp_token *tok;
3594 enum cpp_ttype type, curr_type;
3595 int have_suffix_p = 0;
3596 tree string_tree;
3597 tree suffix_id = NULL_TREE;
3598 bool curr_tok_is_userdef_p = false;
3600 tok = cp_lexer_peek_token (parser->lexer);
3601 if (!cp_parser_is_string_literal (tok))
3603 cp_parser_error (parser, "expected string-literal");
3604 return error_mark_node;
3607 if (cpp_userdef_string_p (tok->type))
3609 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3610 curr_type = cpp_userdef_string_remove_type (tok->type);
3611 curr_tok_is_userdef_p = true;
3613 else
3615 string_tree = tok->u.value;
3616 curr_type = tok->type;
3618 type = curr_type;
3620 /* Try to avoid the overhead of creating and destroying an obstack
3621 for the common case of just one string. */
3622 if (!cp_parser_is_string_literal
3623 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3625 cp_lexer_consume_token (parser->lexer);
3627 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3628 str.len = TREE_STRING_LENGTH (string_tree);
3629 count = 1;
3631 if (curr_tok_is_userdef_p)
3633 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3634 have_suffix_p = 1;
3635 curr_type = cpp_userdef_string_remove_type (tok->type);
3637 else
3638 curr_type = tok->type;
3640 strs = &str;
3642 else
3644 gcc_obstack_init (&str_ob);
3645 count = 0;
3649 cp_lexer_consume_token (parser->lexer);
3650 count++;
3651 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3652 str.len = TREE_STRING_LENGTH (string_tree);
3654 if (curr_tok_is_userdef_p)
3656 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3657 if (have_suffix_p == 0)
3659 suffix_id = curr_suffix_id;
3660 have_suffix_p = 1;
3662 else if (have_suffix_p == 1
3663 && curr_suffix_id != suffix_id)
3665 error ("inconsistent user-defined literal suffixes"
3666 " %qD and %qD in string literal",
3667 suffix_id, curr_suffix_id);
3668 have_suffix_p = -1;
3670 curr_type = cpp_userdef_string_remove_type (tok->type);
3672 else
3673 curr_type = tok->type;
3675 if (type != curr_type)
3677 if (type == CPP_STRING)
3678 type = curr_type;
3679 else if (curr_type != CPP_STRING)
3680 error_at (tok->location,
3681 "unsupported non-standard concatenation "
3682 "of string literals");
3685 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3687 tok = cp_lexer_peek_token (parser->lexer);
3688 if (cpp_userdef_string_p (tok->type))
3690 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3691 curr_type = cpp_userdef_string_remove_type (tok->type);
3692 curr_tok_is_userdef_p = true;
3694 else
3696 string_tree = tok->u.value;
3697 curr_type = tok->type;
3698 curr_tok_is_userdef_p = false;
3701 while (cp_parser_is_string_literal (tok));
3703 strs = (cpp_string *) obstack_finish (&str_ob);
3706 if (type != CPP_STRING && !wide_ok)
3708 cp_parser_error (parser, "a wide string is invalid in this context");
3709 type = CPP_STRING;
3712 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3713 (parse_in, strs, count, &istr, type))
3715 value = build_string (istr.len, (const char *)istr.text);
3716 free (CONST_CAST (unsigned char *, istr.text));
3718 switch (type)
3720 default:
3721 case CPP_STRING:
3722 case CPP_UTF8STRING:
3723 TREE_TYPE (value) = char_array_type_node;
3724 break;
3725 case CPP_STRING16:
3726 TREE_TYPE (value) = char16_array_type_node;
3727 break;
3728 case CPP_STRING32:
3729 TREE_TYPE (value) = char32_array_type_node;
3730 break;
3731 case CPP_WSTRING:
3732 TREE_TYPE (value) = wchar_array_type_node;
3733 break;
3736 value = fix_string_type (value);
3738 if (have_suffix_p)
3740 tree literal = build_userdef_literal (suffix_id, value,
3741 OT_NONE, NULL_TREE);
3742 tok->u.value = literal;
3743 return cp_parser_userdef_string_literal (tok);
3746 else
3747 /* cpp_interpret_string has issued an error. */
3748 value = error_mark_node;
3750 if (count > 1)
3751 obstack_free (&str_ob, 0);
3753 return value;
3756 /* Look up a literal operator with the name and the exact arguments. */
3758 static tree
3759 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3761 tree decl, fns;
3762 decl = lookup_name (name);
3763 if (!decl || !is_overloaded_fn (decl))
3764 return error_mark_node;
3766 for (fns = decl; fns; fns = OVL_NEXT (fns))
3768 unsigned int ix;
3769 bool found = true;
3770 tree fn = OVL_CURRENT (fns);
3771 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3772 if (parmtypes != NULL_TREE)
3774 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3775 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3777 tree tparm = TREE_VALUE (parmtypes);
3778 tree targ = TREE_TYPE ((*args)[ix]);
3779 bool ptr = TYPE_PTR_P (tparm);
3780 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3781 if ((ptr || arr || !same_type_p (tparm, targ))
3782 && (!ptr || !arr
3783 || !same_type_p (TREE_TYPE (tparm),
3784 TREE_TYPE (targ))))
3785 found = false;
3787 if (found
3788 && ix == vec_safe_length (args)
3789 /* May be this should be sufficient_parms_p instead,
3790 depending on how exactly should user-defined literals
3791 work in presence of default arguments on the literal
3792 operator parameters. */
3793 && parmtypes == void_list_node)
3794 return fn;
3798 return error_mark_node;
3801 /* Parse a user-defined char constant. Returns a call to a user-defined
3802 literal operator taking the character as an argument. */
3804 static tree
3805 cp_parser_userdef_char_literal (cp_parser *parser)
3807 cp_token *token = cp_lexer_consume_token (parser->lexer);
3808 tree literal = token->u.value;
3809 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3810 tree value = USERDEF_LITERAL_VALUE (literal);
3811 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3812 tree decl, result;
3814 /* Build up a call to the user-defined operator */
3815 /* Lookup the name we got back from the id-expression. */
3816 vec<tree, va_gc> *args = make_tree_vector ();
3817 vec_safe_push (args, value);
3818 decl = lookup_literal_operator (name, args);
3819 if (!decl || decl == error_mark_node)
3821 error ("unable to find character literal operator %qD with %qT argument",
3822 name, TREE_TYPE (value));
3823 release_tree_vector (args);
3824 return error_mark_node;
3826 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3827 release_tree_vector (args);
3828 if (result != error_mark_node)
3829 return result;
3831 error ("unable to find character literal operator %qD with %qT argument",
3832 name, TREE_TYPE (value));
3833 return error_mark_node;
3836 /* A subroutine of cp_parser_userdef_numeric_literal to
3837 create a char... template parameter pack from a string node. */
3839 static tree
3840 make_char_string_pack (tree value)
3842 tree charvec;
3843 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3844 const char *str = TREE_STRING_POINTER (value);
3845 int i, len = TREE_STRING_LENGTH (value) - 1;
3846 tree argvec = make_tree_vec (1);
3848 /* Fill in CHARVEC with all of the parameters. */
3849 charvec = make_tree_vec (len);
3850 for (i = 0; i < len; ++i)
3851 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3853 /* Build the argument packs. */
3854 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3855 TREE_TYPE (argpack) = char_type_node;
3857 TREE_VEC_ELT (argvec, 0) = argpack;
3859 return argvec;
3862 /* A subroutine of cp_parser_userdef_numeric_literal to
3863 create a char... template parameter pack from a string node. */
3865 static tree
3866 make_string_pack (tree value)
3868 tree charvec;
3869 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3870 const char *str = TREE_STRING_POINTER (value);
3871 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3872 int len = TREE_STRING_LENGTH (value) / sz - 1;
3873 tree argvec = make_tree_vec (2);
3875 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3876 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3878 /* First template parm is character type. */
3879 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3881 /* Fill in CHARVEC with all of the parameters. */
3882 charvec = make_tree_vec (len);
3883 if (sz == 1)
3885 for (int i = 0; i < len; ++i)
3886 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, str[i]);
3888 else if (sz == 2)
3890 const uint16_t *num = (const uint16_t *)str;
3891 for (int i = 0; i < len; ++i)
3892 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]);
3894 else if (sz == 4)
3896 const uint32_t *num = (const uint32_t *)str;
3897 for (int i = 0; i < len; ++i)
3898 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]);
3901 /* Build the argument packs. */
3902 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3903 TREE_TYPE (argpack) = str_char_type_node;
3905 TREE_VEC_ELT (argvec, 1) = argpack;
3907 return argvec;
3910 /* Parse a user-defined numeric constant. returns a call to a user-defined
3911 literal operator. */
3913 static tree
3914 cp_parser_userdef_numeric_literal (cp_parser *parser)
3916 cp_token *token = cp_lexer_consume_token (parser->lexer);
3917 tree literal = token->u.value;
3918 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3919 tree value = USERDEF_LITERAL_VALUE (literal);
3920 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3921 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3922 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3923 tree decl, result;
3924 vec<tree, va_gc> *args;
3926 /* Look for a literal operator taking the exact type of numeric argument
3927 as the literal value. */
3928 args = make_tree_vector ();
3929 vec_safe_push (args, value);
3930 decl = lookup_literal_operator (name, args);
3931 if (decl && decl != error_mark_node)
3933 result = finish_call_expr (decl, &args, false, true, tf_none);
3934 if (result != error_mark_node)
3936 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3937 warning_at (token->location, OPT_Woverflow,
3938 "integer literal exceeds range of %qT type",
3939 long_long_unsigned_type_node);
3940 else
3942 if (overflow > 0)
3943 warning_at (token->location, OPT_Woverflow,
3944 "floating literal exceeds range of %qT type",
3945 long_double_type_node);
3946 else if (overflow < 0)
3947 warning_at (token->location, OPT_Woverflow,
3948 "floating literal truncated to zero");
3950 release_tree_vector (args);
3951 return result;
3954 release_tree_vector (args);
3956 /* If the numeric argument didn't work, look for a raw literal
3957 operator taking a const char* argument consisting of the number
3958 in string format. */
3959 args = make_tree_vector ();
3960 vec_safe_push (args, num_string);
3961 decl = lookup_literal_operator (name, args);
3962 if (decl && decl != error_mark_node)
3964 result = finish_call_expr (decl, &args, false, true, tf_none);
3965 if (result != error_mark_node)
3967 release_tree_vector (args);
3968 return result;
3971 release_tree_vector (args);
3973 /* If the raw literal didn't work, look for a non-type template
3974 function with parameter pack char.... Call the function with
3975 template parameter characters representing the number. */
3976 args = make_tree_vector ();
3977 decl = lookup_literal_operator (name, args);
3978 if (decl && decl != error_mark_node)
3980 tree tmpl_args = make_char_string_pack (num_string);
3981 decl = lookup_template_function (decl, tmpl_args);
3982 result = finish_call_expr (decl, &args, false, true, tf_none);
3983 if (result != error_mark_node)
3985 release_tree_vector (args);
3986 return result;
3989 release_tree_vector (args);
3991 error ("unable to find numeric literal operator %qD", name);
3992 return error_mark_node;
3995 /* Parse a user-defined string constant. Returns a call to a user-defined
3996 literal operator taking a character pointer and the length of the string
3997 as arguments. */
3999 static tree
4000 cp_parser_userdef_string_literal (cp_token *token)
4002 tree literal = token->u.value;
4003 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4004 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4005 tree value = USERDEF_LITERAL_VALUE (literal);
4006 int len = TREE_STRING_LENGTH (value)
4007 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4008 tree decl, result;
4009 vec<tree, va_gc> *args;
4011 /* Look for a template function with typename parameter CharT
4012 and parameter pack CharT... Call the function with
4013 template parameter characters representing the string. */
4014 args = make_tree_vector ();
4015 decl = lookup_literal_operator (name, args);
4016 if (decl && decl != error_mark_node)
4018 tree tmpl_args = make_string_pack (value);
4019 decl = lookup_template_function (decl, tmpl_args);
4020 result = finish_call_expr (decl, &args, false, true, tf_none);
4021 if (result != error_mark_node)
4023 release_tree_vector (args);
4024 return result;
4027 release_tree_vector (args);
4029 /* Build up a call to the user-defined operator */
4030 /* Lookup the name we got back from the id-expression. */
4031 args = make_tree_vector ();
4032 vec_safe_push (args, value);
4033 vec_safe_push (args, build_int_cst (size_type_node, len));
4034 decl = lookup_name (name);
4035 if (!decl || decl == error_mark_node)
4037 error ("unable to find string literal operator %qD", name);
4038 release_tree_vector (args);
4039 return error_mark_node;
4041 result = finish_call_expr (decl, &args, false, true, tf_none);
4042 release_tree_vector (args);
4043 if (result != error_mark_node)
4044 return result;
4046 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4047 name, TREE_TYPE (value), size_type_node);
4048 return error_mark_node;
4052 /* Basic concepts [gram.basic] */
4054 /* Parse a translation-unit.
4056 translation-unit:
4057 declaration-seq [opt]
4059 Returns TRUE if all went well. */
4061 static bool
4062 cp_parser_translation_unit (cp_parser* parser)
4064 /* The address of the first non-permanent object on the declarator
4065 obstack. */
4066 static void *declarator_obstack_base;
4068 bool success;
4070 /* Create the declarator obstack, if necessary. */
4071 if (!cp_error_declarator)
4073 gcc_obstack_init (&declarator_obstack);
4074 /* Create the error declarator. */
4075 cp_error_declarator = make_declarator (cdk_error);
4076 /* Create the empty parameter list. */
4077 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4078 /* Remember where the base of the declarator obstack lies. */
4079 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4082 cp_parser_declaration_seq_opt (parser);
4084 /* If there are no tokens left then all went well. */
4085 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4087 /* Get rid of the token array; we don't need it any more. */
4088 cp_lexer_destroy (parser->lexer);
4089 parser->lexer = NULL;
4091 /* This file might have been a context that's implicitly extern
4092 "C". If so, pop the lang context. (Only relevant for PCH.) */
4093 if (parser->implicit_extern_c)
4095 pop_lang_context ();
4096 parser->implicit_extern_c = false;
4099 /* Finish up. */
4100 finish_translation_unit ();
4102 success = true;
4104 else
4106 cp_parser_error (parser, "expected declaration");
4107 success = false;
4110 /* Make sure the declarator obstack was fully cleaned up. */
4111 gcc_assert (obstack_next_free (&declarator_obstack)
4112 == declarator_obstack_base);
4114 /* All went well. */
4115 return success;
4118 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4119 decltype context. */
4121 static inline tsubst_flags_t
4122 complain_flags (bool decltype_p)
4124 tsubst_flags_t complain = tf_warning_or_error;
4125 if (decltype_p)
4126 complain |= tf_decltype;
4127 return complain;
4130 /* Expressions [gram.expr] */
4132 /* Parse a primary-expression.
4134 primary-expression:
4135 literal
4136 this
4137 ( expression )
4138 id-expression
4140 GNU Extensions:
4142 primary-expression:
4143 ( compound-statement )
4144 __builtin_va_arg ( assignment-expression , type-id )
4145 __builtin_offsetof ( type-id , offsetof-expression )
4147 C++ Extensions:
4148 __has_nothrow_assign ( type-id )
4149 __has_nothrow_constructor ( type-id )
4150 __has_nothrow_copy ( type-id )
4151 __has_trivial_assign ( type-id )
4152 __has_trivial_constructor ( type-id )
4153 __has_trivial_copy ( type-id )
4154 __has_trivial_destructor ( type-id )
4155 __has_virtual_destructor ( type-id )
4156 __is_abstract ( type-id )
4157 __is_base_of ( type-id , type-id )
4158 __is_class ( type-id )
4159 __is_convertible_to ( type-id , type-id )
4160 __is_empty ( type-id )
4161 __is_enum ( type-id )
4162 __is_final ( type-id )
4163 __is_literal_type ( type-id )
4164 __is_pod ( type-id )
4165 __is_polymorphic ( type-id )
4166 __is_std_layout ( type-id )
4167 __is_trivial ( type-id )
4168 __is_union ( type-id )
4170 Objective-C++ Extension:
4172 primary-expression:
4173 objc-expression
4175 literal:
4176 __null
4178 ADDRESS_P is true iff this expression was immediately preceded by
4179 "&" and therefore might denote a pointer-to-member. CAST_P is true
4180 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4181 true iff this expression is a template argument.
4183 Returns a representation of the expression. Upon return, *IDK
4184 indicates what kind of id-expression (if any) was present. */
4186 static tree
4187 cp_parser_primary_expression (cp_parser *parser,
4188 bool address_p,
4189 bool cast_p,
4190 bool template_arg_p,
4191 bool decltype_p,
4192 cp_id_kind *idk)
4194 cp_token *token = NULL;
4196 /* Assume the primary expression is not an id-expression. */
4197 *idk = CP_ID_KIND_NONE;
4199 /* Peek at the next token. */
4200 token = cp_lexer_peek_token (parser->lexer);
4201 switch (token->type)
4203 /* literal:
4204 integer-literal
4205 character-literal
4206 floating-literal
4207 string-literal
4208 boolean-literal
4209 pointer-literal
4210 user-defined-literal */
4211 case CPP_CHAR:
4212 case CPP_CHAR16:
4213 case CPP_CHAR32:
4214 case CPP_WCHAR:
4215 case CPP_NUMBER:
4216 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4217 return cp_parser_userdef_numeric_literal (parser);
4218 token = cp_lexer_consume_token (parser->lexer);
4219 if (TREE_CODE (token->u.value) == FIXED_CST)
4221 error_at (token->location,
4222 "fixed-point types not supported in C++");
4223 return error_mark_node;
4225 /* Floating-point literals are only allowed in an integral
4226 constant expression if they are cast to an integral or
4227 enumeration type. */
4228 if (TREE_CODE (token->u.value) == REAL_CST
4229 && parser->integral_constant_expression_p
4230 && pedantic)
4232 /* CAST_P will be set even in invalid code like "int(2.7 +
4233 ...)". Therefore, we have to check that the next token
4234 is sure to end the cast. */
4235 if (cast_p)
4237 cp_token *next_token;
4239 next_token = cp_lexer_peek_token (parser->lexer);
4240 if (/* The comma at the end of an
4241 enumerator-definition. */
4242 next_token->type != CPP_COMMA
4243 /* The curly brace at the end of an enum-specifier. */
4244 && next_token->type != CPP_CLOSE_BRACE
4245 /* The end of a statement. */
4246 && next_token->type != CPP_SEMICOLON
4247 /* The end of the cast-expression. */
4248 && next_token->type != CPP_CLOSE_PAREN
4249 /* The end of an array bound. */
4250 && next_token->type != CPP_CLOSE_SQUARE
4251 /* The closing ">" in a template-argument-list. */
4252 && (next_token->type != CPP_GREATER
4253 || parser->greater_than_is_operator_p)
4254 /* C++0x only: A ">>" treated like two ">" tokens,
4255 in a template-argument-list. */
4256 && (next_token->type != CPP_RSHIFT
4257 || (cxx_dialect == cxx98)
4258 || parser->greater_than_is_operator_p))
4259 cast_p = false;
4262 /* If we are within a cast, then the constraint that the
4263 cast is to an integral or enumeration type will be
4264 checked at that point. If we are not within a cast, then
4265 this code is invalid. */
4266 if (!cast_p)
4267 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4269 return token->u.value;
4271 case CPP_CHAR_USERDEF:
4272 case CPP_CHAR16_USERDEF:
4273 case CPP_CHAR32_USERDEF:
4274 case CPP_WCHAR_USERDEF:
4275 return cp_parser_userdef_char_literal (parser);
4277 case CPP_STRING:
4278 case CPP_STRING16:
4279 case CPP_STRING32:
4280 case CPP_WSTRING:
4281 case CPP_UTF8STRING:
4282 case CPP_STRING_USERDEF:
4283 case CPP_STRING16_USERDEF:
4284 case CPP_STRING32_USERDEF:
4285 case CPP_WSTRING_USERDEF:
4286 case CPP_UTF8STRING_USERDEF:
4287 /* ??? Should wide strings be allowed when parser->translate_strings_p
4288 is false (i.e. in attributes)? If not, we can kill the third
4289 argument to cp_parser_string_literal. */
4290 return cp_parser_string_literal (parser,
4291 parser->translate_strings_p,
4292 true);
4294 case CPP_OPEN_PAREN:
4296 tree expr;
4297 bool saved_greater_than_is_operator_p;
4299 /* Consume the `('. */
4300 cp_lexer_consume_token (parser->lexer);
4301 /* Within a parenthesized expression, a `>' token is always
4302 the greater-than operator. */
4303 saved_greater_than_is_operator_p
4304 = parser->greater_than_is_operator_p;
4305 parser->greater_than_is_operator_p = true;
4306 /* If we see `( { ' then we are looking at the beginning of
4307 a GNU statement-expression. */
4308 if (cp_parser_allow_gnu_extensions_p (parser)
4309 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4311 /* Statement-expressions are not allowed by the standard. */
4312 pedwarn (token->location, OPT_Wpedantic,
4313 "ISO C++ forbids braced-groups within expressions");
4315 /* And they're not allowed outside of a function-body; you
4316 cannot, for example, write:
4318 int i = ({ int j = 3; j + 1; });
4320 at class or namespace scope. */
4321 if (!parser->in_function_body
4322 || parser->in_template_argument_list_p)
4324 error_at (token->location,
4325 "statement-expressions are not allowed outside "
4326 "functions nor in template-argument lists");
4327 cp_parser_skip_to_end_of_block_or_statement (parser);
4328 expr = error_mark_node;
4330 else
4332 /* Start the statement-expression. */
4333 expr = begin_stmt_expr ();
4334 /* Parse the compound-statement. */
4335 cp_parser_compound_statement (parser, expr, false, false);
4336 /* Finish up. */
4337 expr = finish_stmt_expr (expr, false);
4340 else
4342 /* Parse the parenthesized expression. */
4343 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4344 /* Let the front end know that this expression was
4345 enclosed in parentheses. This matters in case, for
4346 example, the expression is of the form `A::B', since
4347 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4348 not. */
4349 expr = finish_parenthesized_expr (expr);
4350 /* DR 705: Wrapping an unqualified name in parentheses
4351 suppresses arg-dependent lookup. We want to pass back
4352 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4353 (c++/37862), but none of the others. */
4354 if (*idk != CP_ID_KIND_QUALIFIED)
4355 *idk = CP_ID_KIND_NONE;
4357 /* The `>' token might be the end of a template-id or
4358 template-parameter-list now. */
4359 parser->greater_than_is_operator_p
4360 = saved_greater_than_is_operator_p;
4361 /* Consume the `)'. */
4362 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4363 cp_parser_skip_to_end_of_statement (parser);
4365 return expr;
4368 case CPP_OPEN_SQUARE:
4369 if (c_dialect_objc ())
4370 /* We have an Objective-C++ message. */
4371 return cp_parser_objc_expression (parser);
4373 tree lam = cp_parser_lambda_expression (parser);
4374 /* Don't warn about a failed tentative parse. */
4375 if (cp_parser_error_occurred (parser))
4376 return error_mark_node;
4377 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4378 return lam;
4381 case CPP_OBJC_STRING:
4382 if (c_dialect_objc ())
4383 /* We have an Objective-C++ string literal. */
4384 return cp_parser_objc_expression (parser);
4385 cp_parser_error (parser, "expected primary-expression");
4386 return error_mark_node;
4388 case CPP_KEYWORD:
4389 switch (token->keyword)
4391 /* These two are the boolean literals. */
4392 case RID_TRUE:
4393 cp_lexer_consume_token (parser->lexer);
4394 return boolean_true_node;
4395 case RID_FALSE:
4396 cp_lexer_consume_token (parser->lexer);
4397 return boolean_false_node;
4399 /* The `__null' literal. */
4400 case RID_NULL:
4401 cp_lexer_consume_token (parser->lexer);
4402 return null_node;
4404 /* The `nullptr' literal. */
4405 case RID_NULLPTR:
4406 cp_lexer_consume_token (parser->lexer);
4407 return nullptr_node;
4409 /* Recognize the `this' keyword. */
4410 case RID_THIS:
4411 cp_lexer_consume_token (parser->lexer);
4412 if (parser->local_variables_forbidden_p)
4414 error_at (token->location,
4415 "%<this%> may not be used in this context");
4416 return error_mark_node;
4418 /* Pointers cannot appear in constant-expressions. */
4419 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4420 return error_mark_node;
4421 return finish_this_expr ();
4423 /* The `operator' keyword can be the beginning of an
4424 id-expression. */
4425 case RID_OPERATOR:
4426 goto id_expression;
4428 case RID_FUNCTION_NAME:
4429 case RID_PRETTY_FUNCTION_NAME:
4430 case RID_C99_FUNCTION_NAME:
4432 non_integral_constant name;
4434 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4435 __func__ are the names of variables -- but they are
4436 treated specially. Therefore, they are handled here,
4437 rather than relying on the generic id-expression logic
4438 below. Grammatically, these names are id-expressions.
4440 Consume the token. */
4441 token = cp_lexer_consume_token (parser->lexer);
4443 switch (token->keyword)
4445 case RID_FUNCTION_NAME:
4446 name = NIC_FUNC_NAME;
4447 break;
4448 case RID_PRETTY_FUNCTION_NAME:
4449 name = NIC_PRETTY_FUNC;
4450 break;
4451 case RID_C99_FUNCTION_NAME:
4452 name = NIC_C99_FUNC;
4453 break;
4454 default:
4455 gcc_unreachable ();
4458 if (cp_parser_non_integral_constant_expression (parser, name))
4459 return error_mark_node;
4461 /* Look up the name. */
4462 return finish_fname (token->u.value);
4465 case RID_VA_ARG:
4467 tree expression;
4468 tree type;
4469 source_location type_location;
4471 /* The `__builtin_va_arg' construct is used to handle
4472 `va_arg'. Consume the `__builtin_va_arg' token. */
4473 cp_lexer_consume_token (parser->lexer);
4474 /* Look for the opening `('. */
4475 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4476 /* Now, parse the assignment-expression. */
4477 expression = cp_parser_assignment_expression (parser,
4478 /*cast_p=*/false, NULL);
4479 /* Look for the `,'. */
4480 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4481 type_location = cp_lexer_peek_token (parser->lexer)->location;
4482 /* Parse the type-id. */
4483 type = cp_parser_type_id (parser);
4484 /* Look for the closing `)'. */
4485 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4486 /* Using `va_arg' in a constant-expression is not
4487 allowed. */
4488 if (cp_parser_non_integral_constant_expression (parser,
4489 NIC_VA_ARG))
4490 return error_mark_node;
4491 return build_x_va_arg (type_location, expression, type);
4494 case RID_OFFSETOF:
4495 return cp_parser_builtin_offsetof (parser);
4497 case RID_HAS_NOTHROW_ASSIGN:
4498 case RID_HAS_NOTHROW_CONSTRUCTOR:
4499 case RID_HAS_NOTHROW_COPY:
4500 case RID_HAS_TRIVIAL_ASSIGN:
4501 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4502 case RID_HAS_TRIVIAL_COPY:
4503 case RID_HAS_TRIVIAL_DESTRUCTOR:
4504 case RID_HAS_VIRTUAL_DESTRUCTOR:
4505 case RID_IS_ABSTRACT:
4506 case RID_IS_BASE_OF:
4507 case RID_IS_CLASS:
4508 case RID_IS_CONVERTIBLE_TO:
4509 case RID_IS_EMPTY:
4510 case RID_IS_ENUM:
4511 case RID_IS_FINAL:
4512 case RID_IS_LITERAL_TYPE:
4513 case RID_IS_POD:
4514 case RID_IS_POLYMORPHIC:
4515 case RID_IS_SAME_AS:
4516 case RID_IS_STD_LAYOUT:
4517 case RID_IS_TRIVIAL:
4518 case RID_IS_UNION:
4519 return cp_parser_trait_expr (parser, token->keyword);
4521 // C++ concepts
4522 case RID_REQUIRES:
4523 return cp_parser_requires_expression (parser);
4525 /* Objective-C++ expressions. */
4526 case RID_AT_ENCODE:
4527 case RID_AT_PROTOCOL:
4528 case RID_AT_SELECTOR:
4529 return cp_parser_objc_expression (parser);
4531 case RID_TEMPLATE:
4532 if (parser->in_function_body
4533 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4534 == CPP_LESS))
4536 error_at (token->location,
4537 "a template declaration cannot appear at block scope");
4538 cp_parser_skip_to_end_of_block_or_statement (parser);
4539 return error_mark_node;
4541 default:
4542 cp_parser_error (parser, "expected primary-expression");
4543 return error_mark_node;
4546 /* An id-expression can start with either an identifier, a
4547 `::' as the beginning of a qualified-id, or the "operator"
4548 keyword. */
4549 case CPP_NAME:
4550 case CPP_SCOPE:
4551 case CPP_TEMPLATE_ID:
4552 case CPP_NESTED_NAME_SPECIFIER:
4554 tree id_expression;
4555 tree decl;
4556 const char *error_msg;
4557 bool template_p;
4558 bool done;
4559 cp_token *id_expr_token;
4561 id_expression:
4562 /* Parse the id-expression. */
4563 id_expression
4564 = cp_parser_id_expression (parser,
4565 /*template_keyword_p=*/false,
4566 /*check_dependency_p=*/true,
4567 &template_p,
4568 /*declarator_p=*/false,
4569 /*optional_p=*/false);
4570 if (id_expression == error_mark_node)
4571 return error_mark_node;
4572 id_expr_token = token;
4573 token = cp_lexer_peek_token (parser->lexer);
4574 done = (token->type != CPP_OPEN_SQUARE
4575 && token->type != CPP_OPEN_PAREN
4576 && token->type != CPP_DOT
4577 && token->type != CPP_DEREF
4578 && token->type != CPP_PLUS_PLUS
4579 && token->type != CPP_MINUS_MINUS);
4580 /* If we have a template-id, then no further lookup is
4581 required. If the template-id was for a template-class, we
4582 will sometimes have a TYPE_DECL at this point. */
4583 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4584 || TREE_CODE (id_expression) == TYPE_DECL)
4585 decl = id_expression;
4586 /* Look up the name. */
4587 else
4589 tree ambiguous_decls;
4591 /* If we already know that this lookup is ambiguous, then
4592 we've already issued an error message; there's no reason
4593 to check again. */
4594 if (id_expr_token->type == CPP_NAME
4595 && id_expr_token->ambiguous_p)
4597 cp_parser_simulate_error (parser);
4598 return error_mark_node;
4601 decl = cp_parser_lookup_name (parser, id_expression,
4602 none_type,
4603 template_p,
4604 /*is_namespace=*/false,
4605 /*check_dependency=*/true,
4606 &ambiguous_decls,
4607 id_expr_token->location);
4608 /* If the lookup was ambiguous, an error will already have
4609 been issued. */
4610 if (ambiguous_decls)
4611 return error_mark_node;
4613 /* In Objective-C++, we may have an Objective-C 2.0
4614 dot-syntax for classes here. */
4615 if (c_dialect_objc ()
4616 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4617 && TREE_CODE (decl) == TYPE_DECL
4618 && objc_is_class_name (decl))
4620 tree component;
4621 cp_lexer_consume_token (parser->lexer);
4622 component = cp_parser_identifier (parser);
4623 if (component == error_mark_node)
4624 return error_mark_node;
4626 return objc_build_class_component_ref (id_expression, component);
4629 /* In Objective-C++, an instance variable (ivar) may be preferred
4630 to whatever cp_parser_lookup_name() found. */
4631 decl = objc_lookup_ivar (decl, id_expression);
4633 /* If name lookup gives us a SCOPE_REF, then the
4634 qualifying scope was dependent. */
4635 if (TREE_CODE (decl) == SCOPE_REF)
4637 /* At this point, we do not know if DECL is a valid
4638 integral constant expression. We assume that it is
4639 in fact such an expression, so that code like:
4641 template <int N> struct A {
4642 int a[B<N>::i];
4645 is accepted. At template-instantiation time, we
4646 will check that B<N>::i is actually a constant. */
4647 return decl;
4649 /* Check to see if DECL is a local variable in a context
4650 where that is forbidden. */
4651 if (parser->local_variables_forbidden_p
4652 && local_variable_p (decl))
4654 /* It might be that we only found DECL because we are
4655 trying to be generous with pre-ISO scoping rules.
4656 For example, consider:
4658 int i;
4659 void g() {
4660 for (int i = 0; i < 10; ++i) {}
4661 extern void f(int j = i);
4664 Here, name look up will originally find the out
4665 of scope `i'. We need to issue a warning message,
4666 but then use the global `i'. */
4667 decl = check_for_out_of_scope_variable (decl);
4668 if (local_variable_p (decl))
4670 error_at (id_expr_token->location,
4671 "local variable %qD may not appear in this context",
4672 decl);
4673 return error_mark_node;
4678 decl = (finish_id_expression
4679 (id_expression, decl, parser->scope,
4680 idk,
4681 parser->integral_constant_expression_p,
4682 parser->allow_non_integral_constant_expression_p,
4683 &parser->non_integral_constant_expression_p,
4684 template_p, done, address_p,
4685 template_arg_p,
4686 &error_msg,
4687 id_expr_token->location));
4688 if (error_msg)
4689 cp_parser_error (parser, error_msg);
4690 return decl;
4693 /* Anything else is an error. */
4694 default:
4695 cp_parser_error (parser, "expected primary-expression");
4696 return error_mark_node;
4700 static inline tree
4701 cp_parser_primary_expression (cp_parser *parser,
4702 bool address_p,
4703 bool cast_p,
4704 bool template_arg_p,
4705 cp_id_kind *idk)
4707 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4708 /*decltype*/false, idk);
4711 /* Parse an id-expression.
4713 id-expression:
4714 unqualified-id
4715 qualified-id
4717 qualified-id:
4718 :: [opt] nested-name-specifier template [opt] unqualified-id
4719 :: identifier
4720 :: operator-function-id
4721 :: template-id
4723 Return a representation of the unqualified portion of the
4724 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4725 a `::' or nested-name-specifier.
4727 Often, if the id-expression was a qualified-id, the caller will
4728 want to make a SCOPE_REF to represent the qualified-id. This
4729 function does not do this in order to avoid wastefully creating
4730 SCOPE_REFs when they are not required.
4732 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4733 `template' keyword.
4735 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4736 uninstantiated templates.
4738 If *TEMPLATE_P is non-NULL, it is set to true iff the
4739 `template' keyword is used to explicitly indicate that the entity
4740 named is a template.
4742 If DECLARATOR_P is true, the id-expression is appearing as part of
4743 a declarator, rather than as part of an expression. */
4745 static tree
4746 cp_parser_id_expression (cp_parser *parser,
4747 bool template_keyword_p,
4748 bool check_dependency_p,
4749 bool *template_p,
4750 bool declarator_p,
4751 bool optional_p)
4753 bool global_scope_p;
4754 bool nested_name_specifier_p;
4756 /* Assume the `template' keyword was not used. */
4757 if (template_p)
4758 *template_p = template_keyword_p;
4760 /* Look for the optional `::' operator. */
4761 global_scope_p
4762 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4763 != NULL_TREE);
4764 /* Look for the optional nested-name-specifier. */
4765 nested_name_specifier_p
4766 = (cp_parser_nested_name_specifier_opt (parser,
4767 /*typename_keyword_p=*/false,
4768 check_dependency_p,
4769 /*type_p=*/false,
4770 declarator_p)
4771 != NULL_TREE);
4772 /* If there is a nested-name-specifier, then we are looking at
4773 the first qualified-id production. */
4774 if (nested_name_specifier_p)
4776 tree saved_scope;
4777 tree saved_object_scope;
4778 tree saved_qualifying_scope;
4779 tree unqualified_id;
4780 bool is_template;
4782 /* See if the next token is the `template' keyword. */
4783 if (!template_p)
4784 template_p = &is_template;
4785 *template_p = cp_parser_optional_template_keyword (parser);
4786 /* Name lookup we do during the processing of the
4787 unqualified-id might obliterate SCOPE. */
4788 saved_scope = parser->scope;
4789 saved_object_scope = parser->object_scope;
4790 saved_qualifying_scope = parser->qualifying_scope;
4791 /* Process the final unqualified-id. */
4792 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4793 check_dependency_p,
4794 declarator_p,
4795 /*optional_p=*/false);
4796 /* Restore the SAVED_SCOPE for our caller. */
4797 parser->scope = saved_scope;
4798 parser->object_scope = saved_object_scope;
4799 parser->qualifying_scope = saved_qualifying_scope;
4801 return unqualified_id;
4803 /* Otherwise, if we are in global scope, then we are looking at one
4804 of the other qualified-id productions. */
4805 else if (global_scope_p)
4807 cp_token *token;
4808 tree id;
4810 /* Peek at the next token. */
4811 token = cp_lexer_peek_token (parser->lexer);
4813 /* If it's an identifier, and the next token is not a "<", then
4814 we can avoid the template-id case. This is an optimization
4815 for this common case. */
4816 if (token->type == CPP_NAME
4817 && !cp_parser_nth_token_starts_template_argument_list_p
4818 (parser, 2))
4819 return cp_parser_identifier (parser);
4821 cp_parser_parse_tentatively (parser);
4822 /* Try a template-id. */
4823 id = cp_parser_template_id (parser,
4824 /*template_keyword_p=*/false,
4825 /*check_dependency_p=*/true,
4826 none_type,
4827 declarator_p);
4828 /* If that worked, we're done. */
4829 if (cp_parser_parse_definitely (parser))
4830 return id;
4832 /* Peek at the next token. (Changes in the token buffer may
4833 have invalidated the pointer obtained above.) */
4834 token = cp_lexer_peek_token (parser->lexer);
4836 switch (token->type)
4838 case CPP_NAME:
4839 return cp_parser_identifier (parser);
4841 case CPP_KEYWORD:
4842 if (token->keyword == RID_OPERATOR)
4843 return cp_parser_operator_function_id (parser);
4844 /* Fall through. */
4846 default:
4847 cp_parser_error (parser, "expected id-expression");
4848 return error_mark_node;
4851 else
4852 return cp_parser_unqualified_id (parser, template_keyword_p,
4853 /*check_dependency_p=*/true,
4854 declarator_p,
4855 optional_p);
4858 /* Parse an unqualified-id.
4860 unqualified-id:
4861 identifier
4862 operator-function-id
4863 conversion-function-id
4864 ~ class-name
4865 template-id
4867 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4868 keyword, in a construct like `A::template ...'.
4870 Returns a representation of unqualified-id. For the `identifier'
4871 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4872 production a BIT_NOT_EXPR is returned; the operand of the
4873 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4874 other productions, see the documentation accompanying the
4875 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4876 names are looked up in uninstantiated templates. If DECLARATOR_P
4877 is true, the unqualified-id is appearing as part of a declarator,
4878 rather than as part of an expression. */
4880 static tree
4881 cp_parser_unqualified_id (cp_parser* parser,
4882 bool template_keyword_p,
4883 bool check_dependency_p,
4884 bool declarator_p,
4885 bool optional_p)
4887 cp_token *token;
4889 /* Peek at the next token. */
4890 token = cp_lexer_peek_token (parser->lexer);
4892 switch (token->type)
4894 case CPP_NAME:
4896 tree id;
4898 /* We don't know yet whether or not this will be a
4899 template-id. */
4900 cp_parser_parse_tentatively (parser);
4901 /* Try a template-id. */
4902 id = cp_parser_template_id (parser, template_keyword_p,
4903 check_dependency_p,
4904 none_type,
4905 declarator_p);
4906 /* If it worked, we're done. */
4907 if (cp_parser_parse_definitely (parser))
4908 return id;
4909 /* Otherwise, it's an ordinary identifier. */
4910 return cp_parser_identifier (parser);
4913 case CPP_TEMPLATE_ID:
4914 return cp_parser_template_id (parser, template_keyword_p,
4915 check_dependency_p,
4916 none_type,
4917 declarator_p);
4919 case CPP_COMPL:
4921 tree type_decl;
4922 tree qualifying_scope;
4923 tree object_scope;
4924 tree scope;
4925 bool done;
4927 /* Consume the `~' token. */
4928 cp_lexer_consume_token (parser->lexer);
4929 /* Parse the class-name. The standard, as written, seems to
4930 say that:
4932 template <typename T> struct S { ~S (); };
4933 template <typename T> S<T>::~S() {}
4935 is invalid, since `~' must be followed by a class-name, but
4936 `S<T>' is dependent, and so not known to be a class.
4937 That's not right; we need to look in uninstantiated
4938 templates. A further complication arises from:
4940 template <typename T> void f(T t) {
4941 t.T::~T();
4944 Here, it is not possible to look up `T' in the scope of `T'
4945 itself. We must look in both the current scope, and the
4946 scope of the containing complete expression.
4948 Yet another issue is:
4950 struct S {
4951 int S;
4952 ~S();
4955 S::~S() {}
4957 The standard does not seem to say that the `S' in `~S'
4958 should refer to the type `S' and not the data member
4959 `S::S'. */
4961 /* DR 244 says that we look up the name after the "~" in the
4962 same scope as we looked up the qualifying name. That idea
4963 isn't fully worked out; it's more complicated than that. */
4964 scope = parser->scope;
4965 object_scope = parser->object_scope;
4966 qualifying_scope = parser->qualifying_scope;
4968 /* Check for invalid scopes. */
4969 if (scope == error_mark_node)
4971 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4972 cp_lexer_consume_token (parser->lexer);
4973 return error_mark_node;
4975 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4977 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4978 error_at (token->location,
4979 "scope %qT before %<~%> is not a class-name",
4980 scope);
4981 cp_parser_simulate_error (parser);
4982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4983 cp_lexer_consume_token (parser->lexer);
4984 return error_mark_node;
4986 gcc_assert (!scope || TYPE_P (scope));
4988 /* If the name is of the form "X::~X" it's OK even if X is a
4989 typedef. */
4990 token = cp_lexer_peek_token (parser->lexer);
4991 if (scope
4992 && token->type == CPP_NAME
4993 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4994 != CPP_LESS)
4995 && (token->u.value == TYPE_IDENTIFIER (scope)
4996 || (CLASS_TYPE_P (scope)
4997 && constructor_name_p (token->u.value, scope))))
4999 cp_lexer_consume_token (parser->lexer);
5000 return build_nt (BIT_NOT_EXPR, scope);
5003 /* ~auto means the destructor of whatever the object is. */
5004 if (cp_parser_is_keyword (token, RID_AUTO))
5006 if (cxx_dialect < cxx1y)
5007 pedwarn (input_location, 0,
5008 "%<~auto%> only available with "
5009 "-std=c++1y or -std=gnu++1y");
5010 cp_lexer_consume_token (parser->lexer);
5011 return build_nt (BIT_NOT_EXPR, make_auto ());
5014 /* If there was an explicit qualification (S::~T), first look
5015 in the scope given by the qualification (i.e., S).
5017 Note: in the calls to cp_parser_class_name below we pass
5018 typename_type so that lookup finds the injected-class-name
5019 rather than the constructor. */
5020 done = false;
5021 type_decl = NULL_TREE;
5022 if (scope)
5024 cp_parser_parse_tentatively (parser);
5025 type_decl = cp_parser_class_name (parser,
5026 /*typename_keyword_p=*/false,
5027 /*template_keyword_p=*/false,
5028 typename_type,
5029 /*check_dependency=*/false,
5030 /*class_head_p=*/false,
5031 declarator_p);
5032 if (cp_parser_parse_definitely (parser))
5033 done = true;
5035 /* In "N::S::~S", look in "N" as well. */
5036 if (!done && scope && qualifying_scope)
5038 cp_parser_parse_tentatively (parser);
5039 parser->scope = qualifying_scope;
5040 parser->object_scope = NULL_TREE;
5041 parser->qualifying_scope = NULL_TREE;
5042 type_decl
5043 = cp_parser_class_name (parser,
5044 /*typename_keyword_p=*/false,
5045 /*template_keyword_p=*/false,
5046 typename_type,
5047 /*check_dependency=*/false,
5048 /*class_head_p=*/false,
5049 declarator_p);
5050 if (cp_parser_parse_definitely (parser))
5051 done = true;
5053 /* In "p->S::~T", look in the scope given by "*p" as well. */
5054 else if (!done && object_scope)
5056 cp_parser_parse_tentatively (parser);
5057 parser->scope = object_scope;
5058 parser->object_scope = NULL_TREE;
5059 parser->qualifying_scope = NULL_TREE;
5060 type_decl
5061 = cp_parser_class_name (parser,
5062 /*typename_keyword_p=*/false,
5063 /*template_keyword_p=*/false,
5064 typename_type,
5065 /*check_dependency=*/false,
5066 /*class_head_p=*/false,
5067 declarator_p);
5068 if (cp_parser_parse_definitely (parser))
5069 done = true;
5071 /* Look in the surrounding context. */
5072 if (!done)
5074 parser->scope = NULL_TREE;
5075 parser->object_scope = NULL_TREE;
5076 parser->qualifying_scope = NULL_TREE;
5077 if (processing_template_decl)
5078 cp_parser_parse_tentatively (parser);
5079 type_decl
5080 = cp_parser_class_name (parser,
5081 /*typename_keyword_p=*/false,
5082 /*template_keyword_p=*/false,
5083 typename_type,
5084 /*check_dependency=*/false,
5085 /*class_head_p=*/false,
5086 declarator_p);
5087 if (processing_template_decl
5088 && ! cp_parser_parse_definitely (parser))
5090 /* We couldn't find a type with this name, so just accept
5091 it and check for a match at instantiation time. */
5092 type_decl = cp_parser_identifier (parser);
5093 if (type_decl != error_mark_node)
5094 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5095 return type_decl;
5098 /* If an error occurred, assume that the name of the
5099 destructor is the same as the name of the qualifying
5100 class. That allows us to keep parsing after running
5101 into ill-formed destructor names. */
5102 if (type_decl == error_mark_node && scope)
5103 return build_nt (BIT_NOT_EXPR, scope);
5104 else if (type_decl == error_mark_node)
5105 return error_mark_node;
5107 /* Check that destructor name and scope match. */
5108 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5110 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5111 error_at (token->location,
5112 "declaration of %<~%T%> as member of %qT",
5113 type_decl, scope);
5114 cp_parser_simulate_error (parser);
5115 return error_mark_node;
5118 /* [class.dtor]
5120 A typedef-name that names a class shall not be used as the
5121 identifier in the declarator for a destructor declaration. */
5122 if (declarator_p
5123 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5124 && !DECL_SELF_REFERENCE_P (type_decl)
5125 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5126 error_at (token->location,
5127 "typedef-name %qD used as destructor declarator",
5128 type_decl);
5130 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5133 case CPP_KEYWORD:
5134 if (token->keyword == RID_OPERATOR)
5136 tree id;
5138 /* This could be a template-id, so we try that first. */
5139 cp_parser_parse_tentatively (parser);
5140 /* Try a template-id. */
5141 id = cp_parser_template_id (parser, template_keyword_p,
5142 /*check_dependency_p=*/true,
5143 none_type,
5144 declarator_p);
5145 /* If that worked, we're done. */
5146 if (cp_parser_parse_definitely (parser))
5147 return id;
5148 /* We still don't know whether we're looking at an
5149 operator-function-id or a conversion-function-id. */
5150 cp_parser_parse_tentatively (parser);
5151 /* Try an operator-function-id. */
5152 id = cp_parser_operator_function_id (parser);
5153 /* If that didn't work, try a conversion-function-id. */
5154 if (!cp_parser_parse_definitely (parser))
5155 id = cp_parser_conversion_function_id (parser);
5156 else if (UDLIT_OPER_P (id))
5158 /* 17.6.3.3.5 */
5159 const char *name = UDLIT_OP_SUFFIX (id);
5160 if (name[0] != '_' && !in_system_header && declarator_p)
5161 warning (0, "literal operator suffixes not preceded by %<_%>"
5162 " are reserved for future standardization");
5165 return id;
5167 /* Fall through. */
5169 default:
5170 if (optional_p)
5171 return NULL_TREE;
5172 cp_parser_error (parser, "expected unqualified-id");
5173 return error_mark_node;
5177 /* Parse an (optional) nested-name-specifier.
5179 nested-name-specifier: [C++98]
5180 class-or-namespace-name :: nested-name-specifier [opt]
5181 class-or-namespace-name :: template nested-name-specifier [opt]
5183 nested-name-specifier: [C++0x]
5184 type-name ::
5185 namespace-name ::
5186 nested-name-specifier identifier ::
5187 nested-name-specifier template [opt] simple-template-id ::
5189 PARSER->SCOPE should be set appropriately before this function is
5190 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5191 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5192 in name lookups.
5194 Sets PARSER->SCOPE to the class (TYPE) or namespace
5195 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5196 it unchanged if there is no nested-name-specifier. Returns the new
5197 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5199 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5200 part of a declaration and/or decl-specifier. */
5202 static tree
5203 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5204 bool typename_keyword_p,
5205 bool check_dependency_p,
5206 bool type_p,
5207 bool is_declaration)
5209 bool success = false;
5210 cp_token_position start = 0;
5211 cp_token *token;
5213 /* Remember where the nested-name-specifier starts. */
5214 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5216 start = cp_lexer_token_position (parser->lexer, false);
5217 push_deferring_access_checks (dk_deferred);
5220 while (true)
5222 tree new_scope;
5223 tree old_scope;
5224 tree saved_qualifying_scope;
5225 bool template_keyword_p;
5227 /* Spot cases that cannot be the beginning of a
5228 nested-name-specifier. */
5229 token = cp_lexer_peek_token (parser->lexer);
5231 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5232 the already parsed nested-name-specifier. */
5233 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5235 /* Grab the nested-name-specifier and continue the loop. */
5236 cp_parser_pre_parsed_nested_name_specifier (parser);
5237 /* If we originally encountered this nested-name-specifier
5238 with IS_DECLARATION set to false, we will not have
5239 resolved TYPENAME_TYPEs, so we must do so here. */
5240 if (is_declaration
5241 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5243 new_scope = resolve_typename_type (parser->scope,
5244 /*only_current_p=*/false);
5245 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5246 parser->scope = new_scope;
5248 success = true;
5249 continue;
5252 /* Spot cases that cannot be the beginning of a
5253 nested-name-specifier. On the second and subsequent times
5254 through the loop, we look for the `template' keyword. */
5255 if (success && token->keyword == RID_TEMPLATE)
5257 /* A template-id can start a nested-name-specifier. */
5258 else if (token->type == CPP_TEMPLATE_ID)
5260 /* DR 743: decltype can be used in a nested-name-specifier. */
5261 else if (token_is_decltype (token))
5263 else
5265 /* If the next token is not an identifier, then it is
5266 definitely not a type-name or namespace-name. */
5267 if (token->type != CPP_NAME)
5268 break;
5269 /* If the following token is neither a `<' (to begin a
5270 template-id), nor a `::', then we are not looking at a
5271 nested-name-specifier. */
5272 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5274 if (token->type == CPP_COLON
5275 && parser->colon_corrects_to_scope_p
5276 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5278 error_at (token->location,
5279 "found %<:%> in nested-name-specifier, expected %<::%>");
5280 token->type = CPP_SCOPE;
5283 if (token->type != CPP_SCOPE
5284 && !cp_parser_nth_token_starts_template_argument_list_p
5285 (parser, 2))
5286 break;
5289 /* The nested-name-specifier is optional, so we parse
5290 tentatively. */
5291 cp_parser_parse_tentatively (parser);
5293 /* Look for the optional `template' keyword, if this isn't the
5294 first time through the loop. */
5295 if (success)
5296 template_keyword_p = cp_parser_optional_template_keyword (parser);
5297 else
5298 template_keyword_p = false;
5300 /* Save the old scope since the name lookup we are about to do
5301 might destroy it. */
5302 old_scope = parser->scope;
5303 saved_qualifying_scope = parser->qualifying_scope;
5304 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5305 look up names in "X<T>::I" in order to determine that "Y" is
5306 a template. So, if we have a typename at this point, we make
5307 an effort to look through it. */
5308 if (is_declaration
5309 && !typename_keyword_p
5310 && parser->scope
5311 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5312 parser->scope = resolve_typename_type (parser->scope,
5313 /*only_current_p=*/false);
5314 /* Parse the qualifying entity. */
5315 new_scope
5316 = cp_parser_qualifying_entity (parser,
5317 typename_keyword_p,
5318 template_keyword_p,
5319 check_dependency_p,
5320 type_p,
5321 is_declaration);
5322 /* Look for the `::' token. */
5323 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5325 /* If we found what we wanted, we keep going; otherwise, we're
5326 done. */
5327 if (!cp_parser_parse_definitely (parser))
5329 bool error_p = false;
5331 /* Restore the OLD_SCOPE since it was valid before the
5332 failed attempt at finding the last
5333 class-or-namespace-name. */
5334 parser->scope = old_scope;
5335 parser->qualifying_scope = saved_qualifying_scope;
5337 /* If the next token is a decltype, and the one after that is a
5338 `::', then the decltype has failed to resolve to a class or
5339 enumeration type. Give this error even when parsing
5340 tentatively since it can't possibly be valid--and we're going
5341 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5342 won't get another chance.*/
5343 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5344 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5345 == CPP_SCOPE))
5347 token = cp_lexer_consume_token (parser->lexer);
5348 error_at (token->location, "decltype evaluates to %qT, "
5349 "which is not a class or enumeration type",
5350 token->u.value);
5351 parser->scope = error_mark_node;
5352 error_p = true;
5353 /* As below. */
5354 success = true;
5355 cp_lexer_consume_token (parser->lexer);
5358 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5359 break;
5360 /* If the next token is an identifier, and the one after
5361 that is a `::', then any valid interpretation would have
5362 found a class-or-namespace-name. */
5363 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5364 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5365 == CPP_SCOPE)
5366 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5367 != CPP_COMPL))
5369 token = cp_lexer_consume_token (parser->lexer);
5370 if (!error_p)
5372 if (!token->ambiguous_p)
5374 tree decl;
5375 tree ambiguous_decls;
5377 decl = cp_parser_lookup_name (parser, token->u.value,
5378 none_type,
5379 /*is_template=*/false,
5380 /*is_namespace=*/false,
5381 /*check_dependency=*/true,
5382 &ambiguous_decls,
5383 token->location);
5384 if (TREE_CODE (decl) == TEMPLATE_DECL)
5385 error_at (token->location,
5386 "%qD used without template parameters",
5387 decl);
5388 else if (ambiguous_decls)
5390 // cp_parser_lookup_name has the same diagnostic,
5391 // thus make sure to emit it at most once.
5392 if (cp_parser_uncommitted_to_tentative_parse_p
5393 (parser))
5395 error_at (token->location,
5396 "reference to %qD is ambiguous",
5397 token->u.value);
5398 print_candidates (ambiguous_decls);
5400 decl = error_mark_node;
5402 else
5404 if (cxx_dialect != cxx98)
5405 cp_parser_name_lookup_error
5406 (parser, token->u.value, decl, NLE_NOT_CXX98,
5407 token->location);
5408 else
5409 cp_parser_name_lookup_error
5410 (parser, token->u.value, decl, NLE_CXX98,
5411 token->location);
5414 parser->scope = error_mark_node;
5415 error_p = true;
5416 /* Treat this as a successful nested-name-specifier
5417 due to:
5419 [basic.lookup.qual]
5421 If the name found is not a class-name (clause
5422 _class_) or namespace-name (_namespace.def_), the
5423 program is ill-formed. */
5424 success = true;
5426 cp_lexer_consume_token (parser->lexer);
5428 break;
5430 /* We've found one valid nested-name-specifier. */
5431 success = true;
5432 /* Name lookup always gives us a DECL. */
5433 if (TREE_CODE (new_scope) == TYPE_DECL)
5434 new_scope = TREE_TYPE (new_scope);
5435 /* Uses of "template" must be followed by actual templates. */
5436 if (template_keyword_p
5437 && !(CLASS_TYPE_P (new_scope)
5438 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5439 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5440 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5441 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5442 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5443 == TEMPLATE_ID_EXPR)))
5444 permerror (input_location, TYPE_P (new_scope)
5445 ? G_("%qT is not a template")
5446 : G_("%qD is not a template"),
5447 new_scope);
5448 /* If it is a class scope, try to complete it; we are about to
5449 be looking up names inside the class. */
5450 if (TYPE_P (new_scope)
5451 /* Since checking types for dependency can be expensive,
5452 avoid doing it if the type is already complete. */
5453 && !COMPLETE_TYPE_P (new_scope)
5454 /* Do not try to complete dependent types. */
5455 && !dependent_type_p (new_scope))
5457 new_scope = complete_type (new_scope);
5458 /* If it is a typedef to current class, use the current
5459 class instead, as the typedef won't have any names inside
5460 it yet. */
5461 if (!COMPLETE_TYPE_P (new_scope)
5462 && currently_open_class (new_scope))
5463 new_scope = TYPE_MAIN_VARIANT (new_scope);
5465 /* Make sure we look in the right scope the next time through
5466 the loop. */
5467 parser->scope = new_scope;
5470 /* If parsing tentatively, replace the sequence of tokens that makes
5471 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5472 token. That way, should we re-parse the token stream, we will
5473 not have to repeat the effort required to do the parse, nor will
5474 we issue duplicate error messages. */
5475 if (success && start)
5477 cp_token *token;
5479 token = cp_lexer_token_at (parser->lexer, start);
5480 /* Reset the contents of the START token. */
5481 token->type = CPP_NESTED_NAME_SPECIFIER;
5482 /* Retrieve any deferred checks. Do not pop this access checks yet
5483 so the memory will not be reclaimed during token replacing below. */
5484 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5485 token->u.tree_check_value->value = parser->scope;
5486 token->u.tree_check_value->checks = get_deferred_access_checks ();
5487 token->u.tree_check_value->qualifying_scope =
5488 parser->qualifying_scope;
5489 token->keyword = RID_MAX;
5491 /* Purge all subsequent tokens. */
5492 cp_lexer_purge_tokens_after (parser->lexer, start);
5495 if (start)
5496 pop_to_parent_deferring_access_checks ();
5498 return success ? parser->scope : NULL_TREE;
5501 /* Parse a nested-name-specifier. See
5502 cp_parser_nested_name_specifier_opt for details. This function
5503 behaves identically, except that it will an issue an error if no
5504 nested-name-specifier is present. */
5506 static tree
5507 cp_parser_nested_name_specifier (cp_parser *parser,
5508 bool typename_keyword_p,
5509 bool check_dependency_p,
5510 bool type_p,
5511 bool is_declaration)
5513 tree scope;
5515 /* Look for the nested-name-specifier. */
5516 scope = cp_parser_nested_name_specifier_opt (parser,
5517 typename_keyword_p,
5518 check_dependency_p,
5519 type_p,
5520 is_declaration);
5521 /* If it was not present, issue an error message. */
5522 if (!scope)
5524 cp_parser_error (parser, "expected nested-name-specifier");
5525 parser->scope = NULL_TREE;
5528 return scope;
5531 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5532 this is either a class-name or a namespace-name (which corresponds
5533 to the class-or-namespace-name production in the grammar). For
5534 C++0x, it can also be a type-name that refers to an enumeration
5535 type or a simple-template-id.
5537 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5538 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5539 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5540 TYPE_P is TRUE iff the next name should be taken as a class-name,
5541 even the same name is declared to be another entity in the same
5542 scope.
5544 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5545 specified by the class-or-namespace-name. If neither is found the
5546 ERROR_MARK_NODE is returned. */
5548 static tree
5549 cp_parser_qualifying_entity (cp_parser *parser,
5550 bool typename_keyword_p,
5551 bool template_keyword_p,
5552 bool check_dependency_p,
5553 bool type_p,
5554 bool is_declaration)
5556 tree saved_scope;
5557 tree saved_qualifying_scope;
5558 tree saved_object_scope;
5559 tree scope;
5560 bool only_class_p;
5561 bool successful_parse_p;
5563 /* DR 743: decltype can appear in a nested-name-specifier. */
5564 if (cp_lexer_next_token_is_decltype (parser->lexer))
5566 scope = cp_parser_decltype (parser);
5567 if (TREE_CODE (scope) != ENUMERAL_TYPE
5568 && !MAYBE_CLASS_TYPE_P (scope))
5570 cp_parser_simulate_error (parser);
5571 return error_mark_node;
5573 if (TYPE_NAME (scope))
5574 scope = TYPE_NAME (scope);
5575 return scope;
5578 /* Before we try to parse the class-name, we must save away the
5579 current PARSER->SCOPE since cp_parser_class_name will destroy
5580 it. */
5581 saved_scope = parser->scope;
5582 saved_qualifying_scope = parser->qualifying_scope;
5583 saved_object_scope = parser->object_scope;
5584 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5585 there is no need to look for a namespace-name. */
5586 only_class_p = template_keyword_p
5587 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5588 if (!only_class_p)
5589 cp_parser_parse_tentatively (parser);
5590 scope = cp_parser_class_name (parser,
5591 typename_keyword_p,
5592 template_keyword_p,
5593 type_p ? class_type : none_type,
5594 check_dependency_p,
5595 /*class_head_p=*/false,
5596 is_declaration);
5597 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5598 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5599 if (!only_class_p
5600 && cxx_dialect != cxx98
5601 && !successful_parse_p)
5603 /* Restore the saved scope. */
5604 parser->scope = saved_scope;
5605 parser->qualifying_scope = saved_qualifying_scope;
5606 parser->object_scope = saved_object_scope;
5608 /* Parse tentatively. */
5609 cp_parser_parse_tentatively (parser);
5611 /* Parse a type-name */
5612 scope = cp_parser_type_name (parser);
5614 /* "If the name found does not designate a namespace or a class,
5615 enumeration, or dependent type, the program is ill-formed."
5617 We cover classes and dependent types above and namespaces below,
5618 so this code is only looking for enums. */
5619 if (!scope || TREE_CODE (scope) != TYPE_DECL
5620 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5621 cp_parser_simulate_error (parser);
5623 successful_parse_p = cp_parser_parse_definitely (parser);
5625 /* If that didn't work, try for a namespace-name. */
5626 if (!only_class_p && !successful_parse_p)
5628 /* Restore the saved scope. */
5629 parser->scope = saved_scope;
5630 parser->qualifying_scope = saved_qualifying_scope;
5631 parser->object_scope = saved_object_scope;
5632 /* If we are not looking at an identifier followed by the scope
5633 resolution operator, then this is not part of a
5634 nested-name-specifier. (Note that this function is only used
5635 to parse the components of a nested-name-specifier.) */
5636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5637 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5638 return error_mark_node;
5639 scope = cp_parser_namespace_name (parser);
5642 return scope;
5645 /* Parse a postfix-expression.
5647 postfix-expression:
5648 primary-expression
5649 postfix-expression [ expression ]
5650 postfix-expression ( expression-list [opt] )
5651 simple-type-specifier ( expression-list [opt] )
5652 typename :: [opt] nested-name-specifier identifier
5653 ( expression-list [opt] )
5654 typename :: [opt] nested-name-specifier template [opt] template-id
5655 ( expression-list [opt] )
5656 postfix-expression . template [opt] id-expression
5657 postfix-expression -> template [opt] id-expression
5658 postfix-expression . pseudo-destructor-name
5659 postfix-expression -> pseudo-destructor-name
5660 postfix-expression ++
5661 postfix-expression --
5662 dynamic_cast < type-id > ( expression )
5663 static_cast < type-id > ( expression )
5664 reinterpret_cast < type-id > ( expression )
5665 const_cast < type-id > ( expression )
5666 typeid ( expression )
5667 typeid ( type-id )
5669 GNU Extension:
5671 postfix-expression:
5672 ( type-id ) { initializer-list , [opt] }
5674 This extension is a GNU version of the C99 compound-literal
5675 construct. (The C99 grammar uses `type-name' instead of `type-id',
5676 but they are essentially the same concept.)
5678 If ADDRESS_P is true, the postfix expression is the operand of the
5679 `&' operator. CAST_P is true if this expression is the target of a
5680 cast.
5682 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5683 class member access expressions [expr.ref].
5685 Returns a representation of the expression. */
5687 static tree
5688 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5689 bool member_access_only_p, bool decltype_p,
5690 cp_id_kind * pidk_return)
5692 cp_token *token;
5693 location_t loc;
5694 enum rid keyword;
5695 cp_id_kind idk = CP_ID_KIND_NONE;
5696 tree postfix_expression = NULL_TREE;
5697 bool is_member_access = false;
5699 /* Peek at the next token. */
5700 token = cp_lexer_peek_token (parser->lexer);
5701 loc = token->location;
5702 /* Some of the productions are determined by keywords. */
5703 keyword = token->keyword;
5704 switch (keyword)
5706 case RID_DYNCAST:
5707 case RID_STATCAST:
5708 case RID_REINTCAST:
5709 case RID_CONSTCAST:
5711 tree type;
5712 tree expression;
5713 const char *saved_message;
5714 bool saved_in_type_id_in_expr_p;
5716 /* All of these can be handled in the same way from the point
5717 of view of parsing. Begin by consuming the token
5718 identifying the cast. */
5719 cp_lexer_consume_token (parser->lexer);
5721 /* New types cannot be defined in the cast. */
5722 saved_message = parser->type_definition_forbidden_message;
5723 parser->type_definition_forbidden_message
5724 = G_("types may not be defined in casts");
5726 /* Look for the opening `<'. */
5727 cp_parser_require (parser, CPP_LESS, RT_LESS);
5728 /* Parse the type to which we are casting. */
5729 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5730 parser->in_type_id_in_expr_p = true;
5731 type = cp_parser_type_id (parser);
5732 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5733 /* Look for the closing `>'. */
5734 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5735 /* Restore the old message. */
5736 parser->type_definition_forbidden_message = saved_message;
5738 bool saved_greater_than_is_operator_p
5739 = parser->greater_than_is_operator_p;
5740 parser->greater_than_is_operator_p = true;
5742 /* And the expression which is being cast. */
5743 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5744 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5747 parser->greater_than_is_operator_p
5748 = saved_greater_than_is_operator_p;
5750 /* Only type conversions to integral or enumeration types
5751 can be used in constant-expressions. */
5752 if (!cast_valid_in_integral_constant_expression_p (type)
5753 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5754 return error_mark_node;
5756 switch (keyword)
5758 case RID_DYNCAST:
5759 postfix_expression
5760 = build_dynamic_cast (type, expression, tf_warning_or_error);
5761 break;
5762 case RID_STATCAST:
5763 postfix_expression
5764 = build_static_cast (type, expression, tf_warning_or_error);
5765 break;
5766 case RID_REINTCAST:
5767 postfix_expression
5768 = build_reinterpret_cast (type, expression,
5769 tf_warning_or_error);
5770 break;
5771 case RID_CONSTCAST:
5772 postfix_expression
5773 = build_const_cast (type, expression, tf_warning_or_error);
5774 break;
5775 default:
5776 gcc_unreachable ();
5779 break;
5781 case RID_TYPEID:
5783 tree type;
5784 const char *saved_message;
5785 bool saved_in_type_id_in_expr_p;
5787 /* Consume the `typeid' token. */
5788 cp_lexer_consume_token (parser->lexer);
5789 /* Look for the `(' token. */
5790 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5791 /* Types cannot be defined in a `typeid' expression. */
5792 saved_message = parser->type_definition_forbidden_message;
5793 parser->type_definition_forbidden_message
5794 = G_("types may not be defined in a %<typeid%> expression");
5795 /* We can't be sure yet whether we're looking at a type-id or an
5796 expression. */
5797 cp_parser_parse_tentatively (parser);
5798 /* Try a type-id first. */
5799 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5800 parser->in_type_id_in_expr_p = true;
5801 type = cp_parser_type_id (parser);
5802 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5803 /* Look for the `)' token. Otherwise, we can't be sure that
5804 we're not looking at an expression: consider `typeid (int
5805 (3))', for example. */
5806 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5807 /* If all went well, simply lookup the type-id. */
5808 if (cp_parser_parse_definitely (parser))
5809 postfix_expression = get_typeid (type, tf_warning_or_error);
5810 /* Otherwise, fall back to the expression variant. */
5811 else
5813 tree expression;
5815 /* Look for an expression. */
5816 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5817 /* Compute its typeid. */
5818 postfix_expression = build_typeid (expression, tf_warning_or_error);
5819 /* Look for the `)' token. */
5820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5822 /* Restore the saved message. */
5823 parser->type_definition_forbidden_message = saved_message;
5824 /* `typeid' may not appear in an integral constant expression. */
5825 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5826 return error_mark_node;
5828 break;
5830 case RID_TYPENAME:
5832 tree type;
5833 /* The syntax permitted here is the same permitted for an
5834 elaborated-type-specifier. */
5835 type = cp_parser_elaborated_type_specifier (parser,
5836 /*is_friend=*/false,
5837 /*is_declaration=*/false);
5838 postfix_expression = cp_parser_functional_cast (parser, type);
5840 break;
5842 case RID_BUILTIN_SHUFFLE:
5844 vec<tree, va_gc> *vec;
5845 unsigned int i;
5846 tree p;
5848 cp_lexer_consume_token (parser->lexer);
5849 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5850 /*cast_p=*/false, /*allow_expansion_p=*/true,
5851 /*non_constant_p=*/NULL);
5852 if (vec == NULL)
5853 return error_mark_node;
5855 FOR_EACH_VEC_ELT (*vec, i, p)
5856 mark_exp_read (p);
5858 if (vec->length () == 2)
5859 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5860 tf_warning_or_error);
5861 else if (vec->length () == 3)
5862 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5863 tf_warning_or_error);
5864 else
5866 error_at (loc, "wrong number of arguments to "
5867 "%<__builtin_shuffle%>");
5868 return error_mark_node;
5870 break;
5873 default:
5875 tree type;
5877 /* If the next thing is a simple-type-specifier, we may be
5878 looking at a functional cast. We could also be looking at
5879 an id-expression. So, we try the functional cast, and if
5880 that doesn't work we fall back to the primary-expression. */
5881 cp_parser_parse_tentatively (parser);
5882 /* Look for the simple-type-specifier. */
5883 type = cp_parser_simple_type_specifier (parser,
5884 /*decl_specs=*/NULL,
5885 CP_PARSER_FLAGS_NONE);
5886 /* Parse the cast itself. */
5887 if (!cp_parser_error_occurred (parser))
5888 postfix_expression
5889 = cp_parser_functional_cast (parser, type);
5890 /* If that worked, we're done. */
5891 if (cp_parser_parse_definitely (parser))
5892 break;
5894 /* If the functional-cast didn't work out, try a
5895 compound-literal. */
5896 if (cp_parser_allow_gnu_extensions_p (parser)
5897 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5899 tree initializer = NULL_TREE;
5900 bool compound_literal_p;
5902 cp_parser_parse_tentatively (parser);
5903 /* Consume the `('. */
5904 cp_lexer_consume_token (parser->lexer);
5906 /* Avoid calling cp_parser_type_id pointlessly, see comment
5907 in cp_parser_cast_expression about c++/29234. */
5908 cp_lexer_save_tokens (parser->lexer);
5910 compound_literal_p
5911 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5912 /*consume_paren=*/true)
5913 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5915 /* Roll back the tokens we skipped. */
5916 cp_lexer_rollback_tokens (parser->lexer);
5918 if (!compound_literal_p)
5919 cp_parser_simulate_error (parser);
5920 else
5922 /* Parse the type. */
5923 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5924 parser->in_type_id_in_expr_p = true;
5925 type = cp_parser_type_id (parser);
5926 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5927 /* Look for the `)'. */
5928 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5931 /* If things aren't going well, there's no need to
5932 keep going. */
5933 if (!cp_parser_error_occurred (parser))
5935 bool non_constant_p;
5936 /* Parse the brace-enclosed initializer list. */
5937 initializer = cp_parser_braced_list (parser,
5938 &non_constant_p);
5940 /* If that worked, we're definitely looking at a
5941 compound-literal expression. */
5942 if (cp_parser_parse_definitely (parser))
5944 /* Warn the user that a compound literal is not
5945 allowed in standard C++. */
5946 pedwarn (input_location, OPT_Wpedantic,
5947 "ISO C++ forbids compound-literals");
5948 /* For simplicity, we disallow compound literals in
5949 constant-expressions. We could
5950 allow compound literals of integer type, whose
5951 initializer was a constant, in constant
5952 expressions. Permitting that usage, as a further
5953 extension, would not change the meaning of any
5954 currently accepted programs. (Of course, as
5955 compound literals are not part of ISO C++, the
5956 standard has nothing to say.) */
5957 if (cp_parser_non_integral_constant_expression (parser,
5958 NIC_NCC))
5960 postfix_expression = error_mark_node;
5961 break;
5963 /* Form the representation of the compound-literal. */
5964 postfix_expression
5965 = finish_compound_literal (type, initializer,
5966 tf_warning_or_error);
5967 break;
5971 /* It must be a primary-expression. */
5972 postfix_expression
5973 = cp_parser_primary_expression (parser, address_p, cast_p,
5974 /*template_arg_p=*/false,
5975 decltype_p,
5976 &idk);
5978 break;
5981 /* Note that we don't need to worry about calling build_cplus_new on a
5982 class-valued CALL_EXPR in decltype when it isn't the end of the
5983 postfix-expression; unary_complex_lvalue will take care of that for
5984 all these cases. */
5986 /* Keep looping until the postfix-expression is complete. */
5987 while (true)
5989 if (idk == CP_ID_KIND_UNQUALIFIED
5990 && identifier_p (postfix_expression)
5991 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5992 /* It is not a Koenig lookup function call. */
5993 postfix_expression
5994 = unqualified_name_lookup_error (postfix_expression);
5996 /* Peek at the next token. */
5997 token = cp_lexer_peek_token (parser->lexer);
5999 switch (token->type)
6001 case CPP_OPEN_SQUARE:
6002 if (cp_next_tokens_can_be_std_attribute_p (parser))
6004 cp_parser_error (parser,
6005 "two consecutive %<[%> shall "
6006 "only introduce an attribute");
6007 return error_mark_node;
6009 postfix_expression
6010 = cp_parser_postfix_open_square_expression (parser,
6011 postfix_expression,
6012 false,
6013 decltype_p);
6014 idk = CP_ID_KIND_NONE;
6015 is_member_access = false;
6016 break;
6018 case CPP_OPEN_PAREN:
6019 /* postfix-expression ( expression-list [opt] ) */
6021 bool koenig_p;
6022 bool is_builtin_constant_p;
6023 bool saved_integral_constant_expression_p = false;
6024 bool saved_non_integral_constant_expression_p = false;
6025 tsubst_flags_t complain = complain_flags (decltype_p);
6026 vec<tree, va_gc> *args;
6028 is_member_access = false;
6030 is_builtin_constant_p
6031 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6032 if (is_builtin_constant_p)
6034 /* The whole point of __builtin_constant_p is to allow
6035 non-constant expressions to appear as arguments. */
6036 saved_integral_constant_expression_p
6037 = parser->integral_constant_expression_p;
6038 saved_non_integral_constant_expression_p
6039 = parser->non_integral_constant_expression_p;
6040 parser->integral_constant_expression_p = false;
6042 args = (cp_parser_parenthesized_expression_list
6043 (parser, non_attr,
6044 /*cast_p=*/false, /*allow_expansion_p=*/true,
6045 /*non_constant_p=*/NULL));
6046 if (is_builtin_constant_p)
6048 parser->integral_constant_expression_p
6049 = saved_integral_constant_expression_p;
6050 parser->non_integral_constant_expression_p
6051 = saved_non_integral_constant_expression_p;
6054 if (args == NULL)
6056 postfix_expression = error_mark_node;
6057 break;
6060 /* Function calls are not permitted in
6061 constant-expressions. */
6062 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6063 && cp_parser_non_integral_constant_expression (parser,
6064 NIC_FUNC_CALL))
6066 postfix_expression = error_mark_node;
6067 release_tree_vector (args);
6068 break;
6071 koenig_p = false;
6072 if (idk == CP_ID_KIND_UNQUALIFIED
6073 || idk == CP_ID_KIND_TEMPLATE_ID)
6075 if (identifier_p (postfix_expression))
6077 if (!args->is_empty ())
6079 koenig_p = true;
6080 if (!any_type_dependent_arguments_p (args))
6081 postfix_expression
6082 = perform_koenig_lookup (postfix_expression, args,
6083 /*include_std=*/false,
6084 complain);
6086 else
6087 postfix_expression
6088 = unqualified_fn_lookup_error (postfix_expression);
6090 /* We do not perform argument-dependent lookup if
6091 normal lookup finds a non-function, in accordance
6092 with the expected resolution of DR 218. */
6093 else if (!args->is_empty ()
6094 && is_overloaded_fn (postfix_expression))
6096 tree fn = get_first_fn (postfix_expression);
6097 fn = STRIP_TEMPLATE (fn);
6099 /* Do not do argument dependent lookup if regular
6100 lookup finds a member function or a block-scope
6101 function declaration. [basic.lookup.argdep]/3 */
6102 if (!DECL_FUNCTION_MEMBER_P (fn)
6103 && !DECL_LOCAL_FUNCTION_P (fn))
6105 koenig_p = true;
6106 if (!any_type_dependent_arguments_p (args))
6107 postfix_expression
6108 = perform_koenig_lookup (postfix_expression, args,
6109 /*include_std=*/false,
6110 complain);
6115 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6117 tree instance = TREE_OPERAND (postfix_expression, 0);
6118 tree fn = TREE_OPERAND (postfix_expression, 1);
6120 if (processing_template_decl
6121 && (type_dependent_expression_p (instance)
6122 || (!BASELINK_P (fn)
6123 && TREE_CODE (fn) != FIELD_DECL)
6124 || type_dependent_expression_p (fn)
6125 || any_type_dependent_arguments_p (args)))
6127 postfix_expression
6128 = build_nt_call_vec (postfix_expression, args);
6129 release_tree_vector (args);
6130 break;
6133 if (BASELINK_P (fn))
6135 postfix_expression
6136 = (build_new_method_call
6137 (instance, fn, &args, NULL_TREE,
6138 (idk == CP_ID_KIND_QUALIFIED
6139 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6140 : LOOKUP_NORMAL),
6141 /*fn_p=*/NULL,
6142 complain));
6144 else
6145 postfix_expression
6146 = finish_call_expr (postfix_expression, &args,
6147 /*disallow_virtual=*/false,
6148 /*koenig_p=*/false,
6149 complain);
6151 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6152 || TREE_CODE (postfix_expression) == MEMBER_REF
6153 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6154 postfix_expression = (build_offset_ref_call_from_tree
6155 (postfix_expression, &args,
6156 complain));
6157 else if (idk == CP_ID_KIND_QUALIFIED)
6158 /* A call to a static class member, or a namespace-scope
6159 function. */
6160 postfix_expression
6161 = finish_call_expr (postfix_expression, &args,
6162 /*disallow_virtual=*/true,
6163 koenig_p,
6164 complain);
6165 else
6166 /* All other function calls. */
6167 postfix_expression
6168 = finish_call_expr (postfix_expression, &args,
6169 /*disallow_virtual=*/false,
6170 koenig_p,
6171 complain);
6173 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6174 idk = CP_ID_KIND_NONE;
6176 release_tree_vector (args);
6178 break;
6180 case CPP_DOT:
6181 case CPP_DEREF:
6182 /* postfix-expression . template [opt] id-expression
6183 postfix-expression . pseudo-destructor-name
6184 postfix-expression -> template [opt] id-expression
6185 postfix-expression -> pseudo-destructor-name */
6187 /* Consume the `.' or `->' operator. */
6188 cp_lexer_consume_token (parser->lexer);
6190 postfix_expression
6191 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6192 postfix_expression,
6193 false, &idk, loc);
6195 is_member_access = true;
6196 break;
6198 case CPP_PLUS_PLUS:
6199 /* postfix-expression ++ */
6200 /* Consume the `++' token. */
6201 cp_lexer_consume_token (parser->lexer);
6202 /* Generate a representation for the complete expression. */
6203 postfix_expression
6204 = finish_increment_expr (postfix_expression,
6205 POSTINCREMENT_EXPR);
6206 /* Increments may not appear in constant-expressions. */
6207 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6208 postfix_expression = error_mark_node;
6209 idk = CP_ID_KIND_NONE;
6210 is_member_access = false;
6211 break;
6213 case CPP_MINUS_MINUS:
6214 /* postfix-expression -- */
6215 /* Consume the `--' token. */
6216 cp_lexer_consume_token (parser->lexer);
6217 /* Generate a representation for the complete expression. */
6218 postfix_expression
6219 = finish_increment_expr (postfix_expression,
6220 POSTDECREMENT_EXPR);
6221 /* Decrements may not appear in constant-expressions. */
6222 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6223 postfix_expression = error_mark_node;
6224 idk = CP_ID_KIND_NONE;
6225 is_member_access = false;
6226 break;
6228 default:
6229 if (pidk_return != NULL)
6230 * pidk_return = idk;
6231 if (member_access_only_p)
6232 return is_member_access? postfix_expression : error_mark_node;
6233 else
6234 return postfix_expression;
6238 /* We should never get here. */
6239 gcc_unreachable ();
6240 return error_mark_node;
6243 /* This function parses Cilk Plus array notations. If a normal array expr. is
6244 parsed then the array index is passed back to the caller through *INIT_INDEX
6245 and the function returns a NULL_TREE. If array notation expr. is parsed,
6246 then *INIT_INDEX is ignored by the caller and the function returns
6247 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6248 error_mark_node. */
6250 static tree
6251 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6252 tree array_value)
6254 cp_token *token = NULL;
6255 tree length_index, stride = NULL_TREE, value_tree, array_type;
6256 if (!array_value || array_value == error_mark_node)
6258 cp_parser_skip_to_end_of_statement (parser);
6259 return error_mark_node;
6262 array_type = TREE_TYPE (array_value);
6264 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6265 parser->colon_corrects_to_scope_p = false;
6266 token = cp_lexer_peek_token (parser->lexer);
6268 if (!token)
6270 cp_parser_error (parser, "expected %<:%> or numeral");
6271 return error_mark_node;
6273 else if (token->type == CPP_COLON)
6275 /* Consume the ':'. */
6276 cp_lexer_consume_token (parser->lexer);
6278 /* If we are here, then we have a case like this A[:]. */
6279 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6281 cp_parser_error (parser, "expected %<]%>");
6282 cp_parser_skip_to_end_of_statement (parser);
6283 return error_mark_node;
6285 *init_index = NULL_TREE;
6286 stride = NULL_TREE;
6287 length_index = NULL_TREE;
6289 else
6291 /* If we are here, then there are three valid possibilities:
6292 1. ARRAY [ EXP ]
6293 2. ARRAY [ EXP : EXP ]
6294 3. ARRAY [ EXP : EXP : EXP ] */
6296 *init_index = cp_parser_expression (parser, false, NULL);
6297 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6299 /* This indicates that we have a normal array expression. */
6300 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6301 return NULL_TREE;
6304 /* Consume the ':'. */
6305 cp_lexer_consume_token (parser->lexer);
6306 length_index = cp_parser_expression (parser, false, NULL);
6307 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6309 cp_lexer_consume_token (parser->lexer);
6310 stride = cp_parser_expression (parser, false, NULL);
6313 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6315 if (*init_index == error_mark_node || length_index == error_mark_node
6316 || stride == error_mark_node)
6318 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6319 cp_lexer_consume_token (parser->lexer);
6320 return error_mark_node;
6322 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6324 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6325 length_index, stride, array_type);
6326 return value_tree;
6329 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6330 by cp_parser_builtin_offsetof. We're looking for
6332 postfix-expression [ expression ]
6333 postfix-expression [ braced-init-list ] (C++11)
6335 FOR_OFFSETOF is set if we're being called in that context, which
6336 changes how we deal with integer constant expressions. */
6338 static tree
6339 cp_parser_postfix_open_square_expression (cp_parser *parser,
6340 tree postfix_expression,
6341 bool for_offsetof,
6342 bool decltype_p)
6344 tree index = NULL_TREE;
6345 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6346 bool saved_greater_than_is_operator_p;
6348 /* Consume the `[' token. */
6349 cp_lexer_consume_token (parser->lexer);
6351 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6352 parser->greater_than_is_operator_p = true;
6354 /* Parse the index expression. */
6355 /* ??? For offsetof, there is a question of what to allow here. If
6356 offsetof is not being used in an integral constant expression context,
6357 then we *could* get the right answer by computing the value at runtime.
6358 If we are in an integral constant expression context, then we might
6359 could accept any constant expression; hard to say without analysis.
6360 Rather than open the barn door too wide right away, allow only integer
6361 constant expressions here. */
6362 if (for_offsetof)
6363 index = cp_parser_constant_expression (parser, false, NULL);
6364 else
6366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6368 bool expr_nonconst_p;
6369 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6370 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6371 if (flag_enable_cilkplus
6372 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6374 error_at (cp_lexer_peek_token (parser->lexer)->location,
6375 "braced list index is not allowed with array "
6376 "notation");
6377 cp_parser_skip_to_end_of_statement (parser);
6378 return error_mark_node;
6381 else if (flag_enable_cilkplus)
6383 /* Here are have these two options:
6384 ARRAY[EXP : EXP] - Array notation expr with default
6385 stride of 1.
6386 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6387 stride. */
6388 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6389 postfix_expression);
6390 if (an_exp)
6391 return an_exp;
6393 else
6394 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6397 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6399 /* Look for the closing `]'. */
6400 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6402 /* Build the ARRAY_REF. */
6403 postfix_expression = grok_array_decl (loc, postfix_expression,
6404 index, decltype_p);
6406 /* When not doing offsetof, array references are not permitted in
6407 constant-expressions. */
6408 if (!for_offsetof
6409 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6410 postfix_expression = error_mark_node;
6412 return postfix_expression;
6415 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6416 by cp_parser_builtin_offsetof. We're looking for
6418 postfix-expression . template [opt] id-expression
6419 postfix-expression . pseudo-destructor-name
6420 postfix-expression -> template [opt] id-expression
6421 postfix-expression -> pseudo-destructor-name
6423 FOR_OFFSETOF is set if we're being called in that context. That sorta
6424 limits what of the above we'll actually accept, but nevermind.
6425 TOKEN_TYPE is the "." or "->" token, which will already have been
6426 removed from the stream. */
6428 static tree
6429 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6430 enum cpp_ttype token_type,
6431 tree postfix_expression,
6432 bool for_offsetof, cp_id_kind *idk,
6433 location_t location)
6435 tree name;
6436 bool dependent_p;
6437 bool pseudo_destructor_p;
6438 tree scope = NULL_TREE;
6440 /* If this is a `->' operator, dereference the pointer. */
6441 if (token_type == CPP_DEREF)
6442 postfix_expression = build_x_arrow (location, postfix_expression,
6443 tf_warning_or_error);
6444 /* Check to see whether or not the expression is type-dependent. */
6445 dependent_p = type_dependent_expression_p (postfix_expression);
6446 /* The identifier following the `->' or `.' is not qualified. */
6447 parser->scope = NULL_TREE;
6448 parser->qualifying_scope = NULL_TREE;
6449 parser->object_scope = NULL_TREE;
6450 *idk = CP_ID_KIND_NONE;
6452 /* Enter the scope corresponding to the type of the object
6453 given by the POSTFIX_EXPRESSION. */
6454 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6456 scope = TREE_TYPE (postfix_expression);
6457 /* According to the standard, no expression should ever have
6458 reference type. Unfortunately, we do not currently match
6459 the standard in this respect in that our internal representation
6460 of an expression may have reference type even when the standard
6461 says it does not. Therefore, we have to manually obtain the
6462 underlying type here. */
6463 scope = non_reference (scope);
6464 /* The type of the POSTFIX_EXPRESSION must be complete. */
6465 if (scope == unknown_type_node)
6467 error_at (location, "%qE does not have class type",
6468 postfix_expression);
6469 scope = NULL_TREE;
6471 /* Unlike the object expression in other contexts, *this is not
6472 required to be of complete type for purposes of class member
6473 access (5.2.5) outside the member function body. */
6474 else if (postfix_expression != current_class_ref
6475 && !(processing_template_decl && scope == current_class_type))
6476 scope = complete_type_or_else (scope, NULL_TREE);
6477 /* Let the name lookup machinery know that we are processing a
6478 class member access expression. */
6479 parser->context->object_type = scope;
6480 /* If something went wrong, we want to be able to discern that case,
6481 as opposed to the case where there was no SCOPE due to the type
6482 of expression being dependent. */
6483 if (!scope)
6484 scope = error_mark_node;
6485 /* If the SCOPE was erroneous, make the various semantic analysis
6486 functions exit quickly -- and without issuing additional error
6487 messages. */
6488 if (scope == error_mark_node)
6489 postfix_expression = error_mark_node;
6492 /* Assume this expression is not a pseudo-destructor access. */
6493 pseudo_destructor_p = false;
6495 /* If the SCOPE is a scalar type, then, if this is a valid program,
6496 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6497 is type dependent, it can be pseudo-destructor-name or something else.
6498 Try to parse it as pseudo-destructor-name first. */
6499 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6501 tree s;
6502 tree type;
6504 cp_parser_parse_tentatively (parser);
6505 /* Parse the pseudo-destructor-name. */
6506 s = NULL_TREE;
6507 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6508 &s, &type);
6509 if (dependent_p
6510 && (cp_parser_error_occurred (parser)
6511 || !SCALAR_TYPE_P (type)))
6512 cp_parser_abort_tentative_parse (parser);
6513 else if (cp_parser_parse_definitely (parser))
6515 pseudo_destructor_p = true;
6516 postfix_expression
6517 = finish_pseudo_destructor_expr (postfix_expression,
6518 s, type, location);
6522 if (!pseudo_destructor_p)
6524 /* If the SCOPE is not a scalar type, we are looking at an
6525 ordinary class member access expression, rather than a
6526 pseudo-destructor-name. */
6527 bool template_p;
6528 cp_token *token = cp_lexer_peek_token (parser->lexer);
6529 /* Parse the id-expression. */
6530 name = (cp_parser_id_expression
6531 (parser,
6532 cp_parser_optional_template_keyword (parser),
6533 /*check_dependency_p=*/true,
6534 &template_p,
6535 /*declarator_p=*/false,
6536 /*optional_p=*/false));
6537 /* In general, build a SCOPE_REF if the member name is qualified.
6538 However, if the name was not dependent and has already been
6539 resolved; there is no need to build the SCOPE_REF. For example;
6541 struct X { void f(); };
6542 template <typename T> void f(T* t) { t->X::f(); }
6544 Even though "t" is dependent, "X::f" is not and has been resolved
6545 to a BASELINK; there is no need to include scope information. */
6547 /* But we do need to remember that there was an explicit scope for
6548 virtual function calls. */
6549 if (parser->scope)
6550 *idk = CP_ID_KIND_QUALIFIED;
6552 /* If the name is a template-id that names a type, we will get a
6553 TYPE_DECL here. That is invalid code. */
6554 if (TREE_CODE (name) == TYPE_DECL)
6556 error_at (token->location, "invalid use of %qD", name);
6557 postfix_expression = error_mark_node;
6559 else
6561 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6563 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6565 error_at (token->location, "%<%D::%D%> is not a class member",
6566 parser->scope, name);
6567 postfix_expression = error_mark_node;
6569 else
6570 name = build_qualified_name (/*type=*/NULL_TREE,
6571 parser->scope,
6572 name,
6573 template_p);
6574 parser->scope = NULL_TREE;
6575 parser->qualifying_scope = NULL_TREE;
6576 parser->object_scope = NULL_TREE;
6578 if (parser->scope && name && BASELINK_P (name))
6579 adjust_result_of_qualified_name_lookup
6580 (name, parser->scope, scope);
6581 postfix_expression
6582 = finish_class_member_access_expr (postfix_expression, name,
6583 template_p,
6584 tf_warning_or_error);
6588 /* We no longer need to look up names in the scope of the object on
6589 the left-hand side of the `.' or `->' operator. */
6590 parser->context->object_type = NULL_TREE;
6592 /* Outside of offsetof, these operators may not appear in
6593 constant-expressions. */
6594 if (!for_offsetof
6595 && (cp_parser_non_integral_constant_expression
6596 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6597 postfix_expression = error_mark_node;
6599 return postfix_expression;
6602 /* Parse a parenthesized expression-list.
6604 expression-list:
6605 assignment-expression
6606 expression-list, assignment-expression
6608 attribute-list:
6609 expression-list
6610 identifier
6611 identifier, expression-list
6613 CAST_P is true if this expression is the target of a cast.
6615 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6616 argument pack.
6618 Returns a vector of trees. Each element is a representation of an
6619 assignment-expression. NULL is returned if the ( and or ) are
6620 missing. An empty, but allocated, vector is returned on no
6621 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6622 if we are parsing an attribute list for an attribute that wants a
6623 plain identifier argument, normal_attr for an attribute that wants
6624 an expression, or non_attr if we aren't parsing an attribute list. If
6625 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6626 not all of the expressions in the list were constant. */
6628 static vec<tree, va_gc> *
6629 cp_parser_parenthesized_expression_list (cp_parser* parser,
6630 int is_attribute_list,
6631 bool cast_p,
6632 bool allow_expansion_p,
6633 bool *non_constant_p)
6635 vec<tree, va_gc> *expression_list;
6636 bool fold_expr_p = is_attribute_list != non_attr;
6637 tree identifier = NULL_TREE;
6638 bool saved_greater_than_is_operator_p;
6640 /* Assume all the expressions will be constant. */
6641 if (non_constant_p)
6642 *non_constant_p = false;
6644 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6645 return NULL;
6647 expression_list = make_tree_vector ();
6649 /* Within a parenthesized expression, a `>' token is always
6650 the greater-than operator. */
6651 saved_greater_than_is_operator_p
6652 = parser->greater_than_is_operator_p;
6653 parser->greater_than_is_operator_p = true;
6655 /* Consume expressions until there are no more. */
6656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6657 while (true)
6659 tree expr;
6661 /* At the beginning of attribute lists, check to see if the
6662 next token is an identifier. */
6663 if (is_attribute_list == id_attr
6664 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6666 cp_token *token;
6668 /* Consume the identifier. */
6669 token = cp_lexer_consume_token (parser->lexer);
6670 /* Save the identifier. */
6671 identifier = token->u.value;
6673 else
6675 bool expr_non_constant_p;
6677 /* Parse the next assignment-expression. */
6678 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6680 /* A braced-init-list. */
6681 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6682 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6683 if (non_constant_p && expr_non_constant_p)
6684 *non_constant_p = true;
6686 else if (non_constant_p)
6688 expr = (cp_parser_constant_expression
6689 (parser, /*allow_non_constant_p=*/true,
6690 &expr_non_constant_p));
6691 if (expr_non_constant_p)
6692 *non_constant_p = true;
6694 else
6695 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6697 if (fold_expr_p)
6698 expr = fold_non_dependent_expr (expr);
6700 /* If we have an ellipsis, then this is an expression
6701 expansion. */
6702 if (allow_expansion_p
6703 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6705 /* Consume the `...'. */
6706 cp_lexer_consume_token (parser->lexer);
6708 /* Build the argument pack. */
6709 expr = make_pack_expansion (expr);
6712 /* Add it to the list. We add error_mark_node
6713 expressions to the list, so that we can still tell if
6714 the correct form for a parenthesized expression-list
6715 is found. That gives better errors. */
6716 vec_safe_push (expression_list, expr);
6718 if (expr == error_mark_node)
6719 goto skip_comma;
6722 /* After the first item, attribute lists look the same as
6723 expression lists. */
6724 is_attribute_list = non_attr;
6726 get_comma:;
6727 /* If the next token isn't a `,', then we are done. */
6728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6729 break;
6731 /* Otherwise, consume the `,' and keep going. */
6732 cp_lexer_consume_token (parser->lexer);
6735 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6737 int ending;
6739 skip_comma:;
6740 /* We try and resync to an unnested comma, as that will give the
6741 user better diagnostics. */
6742 ending = cp_parser_skip_to_closing_parenthesis (parser,
6743 /*recovering=*/true,
6744 /*or_comma=*/true,
6745 /*consume_paren=*/true);
6746 if (ending < 0)
6747 goto get_comma;
6748 if (!ending)
6750 parser->greater_than_is_operator_p
6751 = saved_greater_than_is_operator_p;
6752 return NULL;
6756 parser->greater_than_is_operator_p
6757 = saved_greater_than_is_operator_p;
6759 if (identifier)
6760 vec_safe_insert (expression_list, 0, identifier);
6762 return expression_list;
6765 /* Parse a pseudo-destructor-name.
6767 pseudo-destructor-name:
6768 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6769 :: [opt] nested-name-specifier template template-id :: ~ type-name
6770 :: [opt] nested-name-specifier [opt] ~ type-name
6772 If either of the first two productions is used, sets *SCOPE to the
6773 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6774 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6775 or ERROR_MARK_NODE if the parse fails. */
6777 static void
6778 cp_parser_pseudo_destructor_name (cp_parser* parser,
6779 tree object,
6780 tree* scope,
6781 tree* type)
6783 bool nested_name_specifier_p;
6785 /* Handle ~auto. */
6786 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6787 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6788 && !type_dependent_expression_p (object))
6790 if (cxx_dialect < cxx1y)
6791 pedwarn (input_location, 0,
6792 "%<~auto%> only available with "
6793 "-std=c++1y or -std=gnu++1y");
6794 cp_lexer_consume_token (parser->lexer);
6795 cp_lexer_consume_token (parser->lexer);
6796 *scope = NULL_TREE;
6797 *type = TREE_TYPE (object);
6798 return;
6801 /* Assume that things will not work out. */
6802 *type = error_mark_node;
6804 /* Look for the optional `::' operator. */
6805 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6806 /* Look for the optional nested-name-specifier. */
6807 nested_name_specifier_p
6808 = (cp_parser_nested_name_specifier_opt (parser,
6809 /*typename_keyword_p=*/false,
6810 /*check_dependency_p=*/true,
6811 /*type_p=*/false,
6812 /*is_declaration=*/false)
6813 != NULL_TREE);
6814 /* Now, if we saw a nested-name-specifier, we might be doing the
6815 second production. */
6816 if (nested_name_specifier_p
6817 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6819 /* Consume the `template' keyword. */
6820 cp_lexer_consume_token (parser->lexer);
6821 /* Parse the template-id. */
6822 cp_parser_template_id (parser,
6823 /*template_keyword_p=*/true,
6824 /*check_dependency_p=*/false,
6825 class_type,
6826 /*is_declaration=*/true);
6827 /* Look for the `::' token. */
6828 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6830 /* If the next token is not a `~', then there might be some
6831 additional qualification. */
6832 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6834 /* At this point, we're looking for "type-name :: ~". The type-name
6835 must not be a class-name, since this is a pseudo-destructor. So,
6836 it must be either an enum-name, or a typedef-name -- both of which
6837 are just identifiers. So, we peek ahead to check that the "::"
6838 and "~" tokens are present; if they are not, then we can avoid
6839 calling type_name. */
6840 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6841 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6842 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6844 cp_parser_error (parser, "non-scalar type");
6845 return;
6848 /* Look for the type-name. */
6849 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6850 if (*scope == error_mark_node)
6851 return;
6853 /* Look for the `::' token. */
6854 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6856 else
6857 *scope = NULL_TREE;
6859 /* Look for the `~'. */
6860 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6862 /* Once we see the ~, this has to be a pseudo-destructor. */
6863 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6864 cp_parser_commit_to_topmost_tentative_parse (parser);
6866 /* Look for the type-name again. We are not responsible for
6867 checking that it matches the first type-name. */
6868 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6871 /* Parse a unary-expression.
6873 unary-expression:
6874 postfix-expression
6875 ++ cast-expression
6876 -- cast-expression
6877 unary-operator cast-expression
6878 sizeof unary-expression
6879 sizeof ( type-id )
6880 alignof ( type-id ) [C++0x]
6881 new-expression
6882 delete-expression
6884 GNU Extensions:
6886 unary-expression:
6887 __extension__ cast-expression
6888 __alignof__ unary-expression
6889 __alignof__ ( type-id )
6890 alignof unary-expression [C++0x]
6891 __real__ cast-expression
6892 __imag__ cast-expression
6893 && identifier
6894 sizeof ( type-id ) { initializer-list , [opt] }
6895 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6896 __alignof__ ( type-id ) { initializer-list , [opt] }
6898 ADDRESS_P is true iff the unary-expression is appearing as the
6899 operand of the `&' operator. CAST_P is true if this expression is
6900 the target of a cast.
6902 Returns a representation of the expression. */
6904 static tree
6905 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6906 bool decltype_p, cp_id_kind * pidk)
6908 cp_token *token;
6909 enum tree_code unary_operator;
6911 /* Peek at the next token. */
6912 token = cp_lexer_peek_token (parser->lexer);
6913 /* Some keywords give away the kind of expression. */
6914 if (token->type == CPP_KEYWORD)
6916 enum rid keyword = token->keyword;
6918 switch (keyword)
6920 case RID_ALIGNOF:
6921 case RID_SIZEOF:
6923 tree operand, ret;
6924 enum tree_code op;
6925 location_t first_loc;
6927 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6928 /* Consume the token. */
6929 cp_lexer_consume_token (parser->lexer);
6930 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6931 /* Parse the operand. */
6932 operand = cp_parser_sizeof_operand (parser, keyword);
6934 if (TYPE_P (operand))
6935 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6936 else
6938 /* ISO C++ defines alignof only with types, not with
6939 expressions. So pedwarn if alignof is used with a non-
6940 type expression. However, __alignof__ is ok. */
6941 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6942 pedwarn (token->location, OPT_Wpedantic,
6943 "ISO C++ does not allow %<alignof%> "
6944 "with a non-type");
6946 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6948 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6949 SIZEOF_EXPR with the original operand. */
6950 if (op == SIZEOF_EXPR && ret != error_mark_node)
6952 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6954 if (!processing_template_decl && TYPE_P (operand))
6956 ret = build_min (SIZEOF_EXPR, size_type_node,
6957 build1 (NOP_EXPR, operand,
6958 error_mark_node));
6959 SIZEOF_EXPR_TYPE_P (ret) = 1;
6961 else
6962 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6963 TREE_SIDE_EFFECTS (ret) = 0;
6964 TREE_READONLY (ret) = 1;
6966 SET_EXPR_LOCATION (ret, first_loc);
6968 return ret;
6971 case RID_NEW:
6972 return cp_parser_new_expression (parser);
6974 case RID_DELETE:
6975 return cp_parser_delete_expression (parser);
6977 case RID_EXTENSION:
6979 /* The saved value of the PEDANTIC flag. */
6980 int saved_pedantic;
6981 tree expr;
6983 /* Save away the PEDANTIC flag. */
6984 cp_parser_extension_opt (parser, &saved_pedantic);
6985 /* Parse the cast-expression. */
6986 expr = cp_parser_simple_cast_expression (parser);
6987 /* Restore the PEDANTIC flag. */
6988 pedantic = saved_pedantic;
6990 return expr;
6993 case RID_REALPART:
6994 case RID_IMAGPART:
6996 tree expression;
6998 /* Consume the `__real__' or `__imag__' token. */
6999 cp_lexer_consume_token (parser->lexer);
7000 /* Parse the cast-expression. */
7001 expression = cp_parser_simple_cast_expression (parser);
7002 /* Create the complete representation. */
7003 return build_x_unary_op (token->location,
7004 (keyword == RID_REALPART
7005 ? REALPART_EXPR : IMAGPART_EXPR),
7006 expression,
7007 tf_warning_or_error);
7009 break;
7011 case RID_TRANSACTION_ATOMIC:
7012 case RID_TRANSACTION_RELAXED:
7013 return cp_parser_transaction_expression (parser, keyword);
7015 case RID_NOEXCEPT:
7017 tree expr;
7018 const char *saved_message;
7019 bool saved_integral_constant_expression_p;
7020 bool saved_non_integral_constant_expression_p;
7021 bool saved_greater_than_is_operator_p;
7023 cp_lexer_consume_token (parser->lexer);
7024 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7026 saved_message = parser->type_definition_forbidden_message;
7027 parser->type_definition_forbidden_message
7028 = G_("types may not be defined in %<noexcept%> expressions");
7030 saved_integral_constant_expression_p
7031 = parser->integral_constant_expression_p;
7032 saved_non_integral_constant_expression_p
7033 = parser->non_integral_constant_expression_p;
7034 parser->integral_constant_expression_p = false;
7036 saved_greater_than_is_operator_p
7037 = parser->greater_than_is_operator_p;
7038 parser->greater_than_is_operator_p = true;
7040 ++cp_unevaluated_operand;
7041 ++c_inhibit_evaluation_warnings;
7042 expr = cp_parser_expression (parser, false, NULL);
7043 --c_inhibit_evaluation_warnings;
7044 --cp_unevaluated_operand;
7046 parser->greater_than_is_operator_p
7047 = saved_greater_than_is_operator_p;
7049 parser->integral_constant_expression_p
7050 = saved_integral_constant_expression_p;
7051 parser->non_integral_constant_expression_p
7052 = saved_non_integral_constant_expression_p;
7054 parser->type_definition_forbidden_message = saved_message;
7056 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7057 return finish_noexcept_expr (expr, tf_warning_or_error);
7060 default:
7061 break;
7065 /* Look for the `:: new' and `:: delete', which also signal the
7066 beginning of a new-expression, or delete-expression,
7067 respectively. If the next token is `::', then it might be one of
7068 these. */
7069 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7071 enum rid keyword;
7073 /* See if the token after the `::' is one of the keywords in
7074 which we're interested. */
7075 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7076 /* If it's `new', we have a new-expression. */
7077 if (keyword == RID_NEW)
7078 return cp_parser_new_expression (parser);
7079 /* Similarly, for `delete'. */
7080 else if (keyword == RID_DELETE)
7081 return cp_parser_delete_expression (parser);
7084 /* Look for a unary operator. */
7085 unary_operator = cp_parser_unary_operator (token);
7086 /* The `++' and `--' operators can be handled similarly, even though
7087 they are not technically unary-operators in the grammar. */
7088 if (unary_operator == ERROR_MARK)
7090 if (token->type == CPP_PLUS_PLUS)
7091 unary_operator = PREINCREMENT_EXPR;
7092 else if (token->type == CPP_MINUS_MINUS)
7093 unary_operator = PREDECREMENT_EXPR;
7094 /* Handle the GNU address-of-label extension. */
7095 else if (cp_parser_allow_gnu_extensions_p (parser)
7096 && token->type == CPP_AND_AND)
7098 tree identifier;
7099 tree expression;
7100 location_t loc = token->location;
7102 /* Consume the '&&' token. */
7103 cp_lexer_consume_token (parser->lexer);
7104 /* Look for the identifier. */
7105 identifier = cp_parser_identifier (parser);
7106 /* Create an expression representing the address. */
7107 expression = finish_label_address_expr (identifier, loc);
7108 if (cp_parser_non_integral_constant_expression (parser,
7109 NIC_ADDR_LABEL))
7110 expression = error_mark_node;
7111 return expression;
7114 if (unary_operator != ERROR_MARK)
7116 tree cast_expression;
7117 tree expression = error_mark_node;
7118 non_integral_constant non_constant_p = NIC_NONE;
7119 location_t loc = token->location;
7120 tsubst_flags_t complain = complain_flags (decltype_p);
7122 /* Consume the operator token. */
7123 token = cp_lexer_consume_token (parser->lexer);
7124 /* Parse the cast-expression. */
7125 cast_expression
7126 = cp_parser_cast_expression (parser,
7127 unary_operator == ADDR_EXPR,
7128 /*cast_p=*/false,
7129 /*decltype*/false,
7130 pidk);
7131 /* Now, build an appropriate representation. */
7132 switch (unary_operator)
7134 case INDIRECT_REF:
7135 non_constant_p = NIC_STAR;
7136 expression = build_x_indirect_ref (loc, cast_expression,
7137 RO_UNARY_STAR,
7138 complain);
7139 break;
7141 case ADDR_EXPR:
7142 non_constant_p = NIC_ADDR;
7143 /* Fall through. */
7144 case BIT_NOT_EXPR:
7145 expression = build_x_unary_op (loc, unary_operator,
7146 cast_expression,
7147 complain);
7148 break;
7150 case PREINCREMENT_EXPR:
7151 case PREDECREMENT_EXPR:
7152 non_constant_p = unary_operator == PREINCREMENT_EXPR
7153 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7154 /* Fall through. */
7155 case UNARY_PLUS_EXPR:
7156 case NEGATE_EXPR:
7157 case TRUTH_NOT_EXPR:
7158 expression = finish_unary_op_expr (loc, unary_operator,
7159 cast_expression, complain);
7160 break;
7162 default:
7163 gcc_unreachable ();
7166 if (non_constant_p != NIC_NONE
7167 && cp_parser_non_integral_constant_expression (parser,
7168 non_constant_p))
7169 expression = error_mark_node;
7171 return expression;
7174 return cp_parser_postfix_expression (parser, address_p, cast_p,
7175 /*member_access_only_p=*/false,
7176 decltype_p,
7177 pidk);
7180 static inline tree
7181 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7182 cp_id_kind * pidk)
7184 return cp_parser_unary_expression (parser, address_p, cast_p,
7185 /*decltype*/false, pidk);
7188 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7189 unary-operator, the corresponding tree code is returned. */
7191 static enum tree_code
7192 cp_parser_unary_operator (cp_token* token)
7194 switch (token->type)
7196 case CPP_MULT:
7197 return INDIRECT_REF;
7199 case CPP_AND:
7200 return ADDR_EXPR;
7202 case CPP_PLUS:
7203 return UNARY_PLUS_EXPR;
7205 case CPP_MINUS:
7206 return NEGATE_EXPR;
7208 case CPP_NOT:
7209 return TRUTH_NOT_EXPR;
7211 case CPP_COMPL:
7212 return BIT_NOT_EXPR;
7214 default:
7215 return ERROR_MARK;
7219 /* Parse a new-expression.
7221 new-expression:
7222 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7223 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7225 Returns a representation of the expression. */
7227 static tree
7228 cp_parser_new_expression (cp_parser* parser)
7230 bool global_scope_p;
7231 vec<tree, va_gc> *placement;
7232 tree type;
7233 vec<tree, va_gc> *initializer;
7234 tree nelts = NULL_TREE;
7235 tree ret;
7237 /* Look for the optional `::' operator. */
7238 global_scope_p
7239 = (cp_parser_global_scope_opt (parser,
7240 /*current_scope_valid_p=*/false)
7241 != NULL_TREE);
7242 /* Look for the `new' operator. */
7243 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7244 /* There's no easy way to tell a new-placement from the
7245 `( type-id )' construct. */
7246 cp_parser_parse_tentatively (parser);
7247 /* Look for a new-placement. */
7248 placement = cp_parser_new_placement (parser);
7249 /* If that didn't work out, there's no new-placement. */
7250 if (!cp_parser_parse_definitely (parser))
7252 if (placement != NULL)
7253 release_tree_vector (placement);
7254 placement = NULL;
7257 /* If the next token is a `(', then we have a parenthesized
7258 type-id. */
7259 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7261 cp_token *token;
7262 const char *saved_message = parser->type_definition_forbidden_message;
7264 /* Consume the `('. */
7265 cp_lexer_consume_token (parser->lexer);
7267 /* Parse the type-id. */
7268 parser->type_definition_forbidden_message
7269 = G_("types may not be defined in a new-expression");
7270 type = cp_parser_type_id (parser);
7271 parser->type_definition_forbidden_message = saved_message;
7273 /* Look for the closing `)'. */
7274 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7275 token = cp_lexer_peek_token (parser->lexer);
7276 /* There should not be a direct-new-declarator in this production,
7277 but GCC used to allowed this, so we check and emit a sensible error
7278 message for this case. */
7279 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7281 error_at (token->location,
7282 "array bound forbidden after parenthesized type-id");
7283 inform (token->location,
7284 "try removing the parentheses around the type-id");
7285 cp_parser_direct_new_declarator (parser);
7288 /* Otherwise, there must be a new-type-id. */
7289 else
7290 type = cp_parser_new_type_id (parser, &nelts);
7292 /* If the next token is a `(' or '{', then we have a new-initializer. */
7293 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7294 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7295 initializer = cp_parser_new_initializer (parser);
7296 else
7297 initializer = NULL;
7299 /* A new-expression may not appear in an integral constant
7300 expression. */
7301 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7302 ret = error_mark_node;
7303 else
7305 /* Create a representation of the new-expression. */
7306 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7307 tf_warning_or_error);
7310 if (placement != NULL)
7311 release_tree_vector (placement);
7312 if (initializer != NULL)
7313 release_tree_vector (initializer);
7315 return ret;
7318 /* Parse a new-placement.
7320 new-placement:
7321 ( expression-list )
7323 Returns the same representation as for an expression-list. */
7325 static vec<tree, va_gc> *
7326 cp_parser_new_placement (cp_parser* parser)
7328 vec<tree, va_gc> *expression_list;
7330 /* Parse the expression-list. */
7331 expression_list = (cp_parser_parenthesized_expression_list
7332 (parser, non_attr, /*cast_p=*/false,
7333 /*allow_expansion_p=*/true,
7334 /*non_constant_p=*/NULL));
7336 return expression_list;
7339 /* Parse a new-type-id.
7341 new-type-id:
7342 type-specifier-seq new-declarator [opt]
7344 Returns the TYPE allocated. If the new-type-id indicates an array
7345 type, *NELTS is set to the number of elements in the last array
7346 bound; the TYPE will not include the last array bound. */
7348 static tree
7349 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7351 cp_decl_specifier_seq type_specifier_seq;
7352 cp_declarator *new_declarator;
7353 cp_declarator *declarator;
7354 cp_declarator *outer_declarator;
7355 const char *saved_message;
7357 /* The type-specifier sequence must not contain type definitions.
7358 (It cannot contain declarations of new types either, but if they
7359 are not definitions we will catch that because they are not
7360 complete.) */
7361 saved_message = parser->type_definition_forbidden_message;
7362 parser->type_definition_forbidden_message
7363 = G_("types may not be defined in a new-type-id");
7364 /* Parse the type-specifier-seq. */
7365 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7366 /*is_trailing_return=*/false,
7367 &type_specifier_seq);
7368 /* Restore the old message. */
7369 parser->type_definition_forbidden_message = saved_message;
7371 if (type_specifier_seq.type == error_mark_node)
7372 return error_mark_node;
7374 /* Parse the new-declarator. */
7375 new_declarator = cp_parser_new_declarator_opt (parser);
7377 /* Determine the number of elements in the last array dimension, if
7378 any. */
7379 *nelts = NULL_TREE;
7380 /* Skip down to the last array dimension. */
7381 declarator = new_declarator;
7382 outer_declarator = NULL;
7383 while (declarator && (declarator->kind == cdk_pointer
7384 || declarator->kind == cdk_ptrmem))
7386 outer_declarator = declarator;
7387 declarator = declarator->declarator;
7389 while (declarator
7390 && declarator->kind == cdk_array
7391 && declarator->declarator
7392 && declarator->declarator->kind == cdk_array)
7394 outer_declarator = declarator;
7395 declarator = declarator->declarator;
7398 if (declarator && declarator->kind == cdk_array)
7400 *nelts = declarator->u.array.bounds;
7401 if (*nelts == error_mark_node)
7402 *nelts = integer_one_node;
7404 if (outer_declarator)
7405 outer_declarator->declarator = declarator->declarator;
7406 else
7407 new_declarator = NULL;
7410 return groktypename (&type_specifier_seq, new_declarator, false);
7413 /* Parse an (optional) new-declarator.
7415 new-declarator:
7416 ptr-operator new-declarator [opt]
7417 direct-new-declarator
7419 Returns the declarator. */
7421 static cp_declarator *
7422 cp_parser_new_declarator_opt (cp_parser* parser)
7424 enum tree_code code;
7425 tree type, std_attributes = NULL_TREE;
7426 cp_cv_quals cv_quals;
7428 /* We don't know if there's a ptr-operator next, or not. */
7429 cp_parser_parse_tentatively (parser);
7430 /* Look for a ptr-operator. */
7431 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7432 /* If that worked, look for more new-declarators. */
7433 if (cp_parser_parse_definitely (parser))
7435 cp_declarator *declarator;
7437 /* Parse another optional declarator. */
7438 declarator = cp_parser_new_declarator_opt (parser);
7440 declarator = cp_parser_make_indirect_declarator
7441 (code, type, cv_quals, declarator, std_attributes);
7443 return declarator;
7446 /* If the next token is a `[', there is a direct-new-declarator. */
7447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7448 return cp_parser_direct_new_declarator (parser);
7450 return NULL;
7453 /* Parse a direct-new-declarator.
7455 direct-new-declarator:
7456 [ expression ]
7457 direct-new-declarator [constant-expression]
7461 static cp_declarator *
7462 cp_parser_direct_new_declarator (cp_parser* parser)
7464 cp_declarator *declarator = NULL;
7466 while (true)
7468 tree expression;
7469 cp_token *token;
7471 /* Look for the opening `['. */
7472 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7474 token = cp_lexer_peek_token (parser->lexer);
7475 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7476 /* The standard requires that the expression have integral
7477 type. DR 74 adds enumeration types. We believe that the
7478 real intent is that these expressions be handled like the
7479 expression in a `switch' condition, which also allows
7480 classes with a single conversion to integral or
7481 enumeration type. */
7482 if (!processing_template_decl)
7484 expression
7485 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7486 expression,
7487 /*complain=*/true);
7488 if (!expression)
7490 error_at (token->location,
7491 "expression in new-declarator must have integral "
7492 "or enumeration type");
7493 expression = error_mark_node;
7497 /* Look for the closing `]'. */
7498 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7500 /* Add this bound to the declarator. */
7501 declarator = make_array_declarator (declarator, expression);
7503 /* If the next token is not a `[', then there are no more
7504 bounds. */
7505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7506 break;
7509 return declarator;
7512 /* Parse a new-initializer.
7514 new-initializer:
7515 ( expression-list [opt] )
7516 braced-init-list
7518 Returns a representation of the expression-list. */
7520 static vec<tree, va_gc> *
7521 cp_parser_new_initializer (cp_parser* parser)
7523 vec<tree, va_gc> *expression_list;
7525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7527 tree t;
7528 bool expr_non_constant_p;
7529 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7530 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7531 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7532 expression_list = make_tree_vector_single (t);
7534 else
7535 expression_list = (cp_parser_parenthesized_expression_list
7536 (parser, non_attr, /*cast_p=*/false,
7537 /*allow_expansion_p=*/true,
7538 /*non_constant_p=*/NULL));
7540 return expression_list;
7543 /* Parse a delete-expression.
7545 delete-expression:
7546 :: [opt] delete cast-expression
7547 :: [opt] delete [ ] cast-expression
7549 Returns a representation of the expression. */
7551 static tree
7552 cp_parser_delete_expression (cp_parser* parser)
7554 bool global_scope_p;
7555 bool array_p;
7556 tree expression;
7558 /* Look for the optional `::' operator. */
7559 global_scope_p
7560 = (cp_parser_global_scope_opt (parser,
7561 /*current_scope_valid_p=*/false)
7562 != NULL_TREE);
7563 /* Look for the `delete' keyword. */
7564 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7565 /* See if the array syntax is in use. */
7566 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7568 /* Consume the `[' token. */
7569 cp_lexer_consume_token (parser->lexer);
7570 /* Look for the `]' token. */
7571 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7572 /* Remember that this is the `[]' construct. */
7573 array_p = true;
7575 else
7576 array_p = false;
7578 /* Parse the cast-expression. */
7579 expression = cp_parser_simple_cast_expression (parser);
7581 /* A delete-expression may not appear in an integral constant
7582 expression. */
7583 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7584 return error_mark_node;
7586 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7587 tf_warning_or_error);
7590 /* Returns true if TOKEN may start a cast-expression and false
7591 otherwise. */
7593 static bool
7594 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7596 cp_token *token = cp_lexer_peek_token (parser->lexer);
7597 switch (token->type)
7599 case CPP_COMMA:
7600 case CPP_SEMICOLON:
7601 case CPP_QUERY:
7602 case CPP_COLON:
7603 case CPP_CLOSE_SQUARE:
7604 case CPP_CLOSE_PAREN:
7605 case CPP_CLOSE_BRACE:
7606 case CPP_OPEN_BRACE:
7607 case CPP_DOT:
7608 case CPP_DOT_STAR:
7609 case CPP_DEREF:
7610 case CPP_DEREF_STAR:
7611 case CPP_DIV:
7612 case CPP_MOD:
7613 case CPP_LSHIFT:
7614 case CPP_RSHIFT:
7615 case CPP_LESS:
7616 case CPP_GREATER:
7617 case CPP_LESS_EQ:
7618 case CPP_GREATER_EQ:
7619 case CPP_EQ_EQ:
7620 case CPP_NOT_EQ:
7621 case CPP_EQ:
7622 case CPP_MULT_EQ:
7623 case CPP_DIV_EQ:
7624 case CPP_MOD_EQ:
7625 case CPP_PLUS_EQ:
7626 case CPP_MINUS_EQ:
7627 case CPP_RSHIFT_EQ:
7628 case CPP_LSHIFT_EQ:
7629 case CPP_AND_EQ:
7630 case CPP_XOR_EQ:
7631 case CPP_OR_EQ:
7632 case CPP_XOR:
7633 case CPP_OR:
7634 case CPP_OR_OR:
7635 case CPP_EOF:
7636 return false;
7638 case CPP_OPEN_PAREN:
7639 /* In ((type ()) () the last () isn't a valid cast-expression,
7640 so the whole must be parsed as postfix-expression. */
7641 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7642 != CPP_CLOSE_PAREN;
7644 /* '[' may start a primary-expression in obj-c++. */
7645 case CPP_OPEN_SQUARE:
7646 return c_dialect_objc ();
7648 default:
7649 return true;
7653 /* Parse a cast-expression.
7655 cast-expression:
7656 unary-expression
7657 ( type-id ) cast-expression
7659 ADDRESS_P is true iff the unary-expression is appearing as the
7660 operand of the `&' operator. CAST_P is true if this expression is
7661 the target of a cast.
7663 Returns a representation of the expression. */
7665 static tree
7666 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7667 bool decltype_p, cp_id_kind * pidk)
7669 /* If it's a `(', then we might be looking at a cast. */
7670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7672 tree type = NULL_TREE;
7673 tree expr = NULL_TREE;
7674 bool cast_expression_p;
7675 const char *saved_message;
7677 /* There's no way to know yet whether or not this is a cast.
7678 For example, `(int (3))' is a unary-expression, while `(int)
7679 3' is a cast. So, we resort to parsing tentatively. */
7680 cp_parser_parse_tentatively (parser);
7681 /* Types may not be defined in a cast. */
7682 saved_message = parser->type_definition_forbidden_message;
7683 parser->type_definition_forbidden_message
7684 = G_("types may not be defined in casts");
7685 /* Consume the `('. */
7686 cp_lexer_consume_token (parser->lexer);
7687 /* A very tricky bit is that `(struct S) { 3 }' is a
7688 compound-literal (which we permit in C++ as an extension).
7689 But, that construct is not a cast-expression -- it is a
7690 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7691 is legal; if the compound-literal were a cast-expression,
7692 you'd need an extra set of parentheses.) But, if we parse
7693 the type-id, and it happens to be a class-specifier, then we
7694 will commit to the parse at that point, because we cannot
7695 undo the action that is done when creating a new class. So,
7696 then we cannot back up and do a postfix-expression.
7697 Another tricky case is the following (c++/29234):
7699 struct S { void operator () (); };
7701 void foo ()
7703 ( S()() );
7706 As a type-id we parse the parenthesized S()() as a function
7707 returning a function, groktypename complains and we cannot
7708 back up in this case either.
7710 Therefore, we scan ahead to the closing `)', and check to see
7711 if the tokens after the `)' can start a cast-expression. Otherwise
7712 we are dealing with an unary-expression, a postfix-expression
7713 or something else.
7715 Save tokens so that we can put them back. */
7716 cp_lexer_save_tokens (parser->lexer);
7718 /* We may be looking at a cast-expression. */
7719 cast_expression_p
7720 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7721 /*consume_paren=*/true)
7722 && cp_parser_tokens_start_cast_expression (parser));
7724 /* Roll back the tokens we skipped. */
7725 cp_lexer_rollback_tokens (parser->lexer);
7726 /* If we aren't looking at a cast-expression, simulate an error so
7727 that the call to cp_parser_parse_definitely below will fail. */
7728 if (!cast_expression_p)
7729 cp_parser_simulate_error (parser);
7730 else
7732 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7733 parser->in_type_id_in_expr_p = true;
7734 /* Look for the type-id. */
7735 type = cp_parser_type_id (parser);
7736 /* Look for the closing `)'. */
7737 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7738 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7741 /* Restore the saved message. */
7742 parser->type_definition_forbidden_message = saved_message;
7744 /* At this point this can only be either a cast or a
7745 parenthesized ctor such as `(T ())' that looks like a cast to
7746 function returning T. */
7747 if (!cp_parser_error_occurred (parser))
7749 cp_parser_parse_definitely (parser);
7750 expr = cp_parser_cast_expression (parser,
7751 /*address_p=*/false,
7752 /*cast_p=*/true,
7753 /*decltype_p=*/false,
7754 pidk);
7756 /* Warn about old-style casts, if so requested. */
7757 if (warn_old_style_cast
7758 && !in_system_header
7759 && !VOID_TYPE_P (type)
7760 && current_lang_name != lang_name_c)
7761 warning (OPT_Wold_style_cast, "use of old-style cast");
7763 /* Only type conversions to integral or enumeration types
7764 can be used in constant-expressions. */
7765 if (!cast_valid_in_integral_constant_expression_p (type)
7766 && cp_parser_non_integral_constant_expression (parser,
7767 NIC_CAST))
7768 return error_mark_node;
7770 /* Perform the cast. */
7771 expr = build_c_cast (input_location, type, expr);
7772 return expr;
7774 else
7775 cp_parser_abort_tentative_parse (parser);
7778 /* If we get here, then it's not a cast, so it must be a
7779 unary-expression. */
7780 return cp_parser_unary_expression (parser, address_p, cast_p,
7781 decltype_p, pidk);
7784 /* Parse a binary expression of the general form:
7786 pm-expression:
7787 cast-expression
7788 pm-expression .* cast-expression
7789 pm-expression ->* cast-expression
7791 multiplicative-expression:
7792 pm-expression
7793 multiplicative-expression * pm-expression
7794 multiplicative-expression / pm-expression
7795 multiplicative-expression % pm-expression
7797 additive-expression:
7798 multiplicative-expression
7799 additive-expression + multiplicative-expression
7800 additive-expression - multiplicative-expression
7802 shift-expression:
7803 additive-expression
7804 shift-expression << additive-expression
7805 shift-expression >> additive-expression
7807 relational-expression:
7808 shift-expression
7809 relational-expression < shift-expression
7810 relational-expression > shift-expression
7811 relational-expression <= shift-expression
7812 relational-expression >= shift-expression
7814 GNU Extension:
7816 relational-expression:
7817 relational-expression <? shift-expression
7818 relational-expression >? shift-expression
7820 equality-expression:
7821 relational-expression
7822 equality-expression == relational-expression
7823 equality-expression != relational-expression
7825 and-expression:
7826 equality-expression
7827 and-expression & equality-expression
7829 exclusive-or-expression:
7830 and-expression
7831 exclusive-or-expression ^ and-expression
7833 inclusive-or-expression:
7834 exclusive-or-expression
7835 inclusive-or-expression | exclusive-or-expression
7837 logical-and-expression:
7838 inclusive-or-expression
7839 logical-and-expression && inclusive-or-expression
7841 logical-or-expression:
7842 logical-and-expression
7843 logical-or-expression || logical-and-expression
7845 All these are implemented with a single function like:
7847 binary-expression:
7848 simple-cast-expression
7849 binary-expression <token> binary-expression
7851 CAST_P is true if this expression is the target of a cast.
7853 The binops_by_token map is used to get the tree codes for each <token> type.
7854 binary-expressions are associated according to a precedence table. */
7856 #define TOKEN_PRECEDENCE(token) \
7857 (((token->type == CPP_GREATER \
7858 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7859 && !parser->greater_than_is_operator_p) \
7860 ? PREC_NOT_OPERATOR \
7861 : binops_by_token[token->type].prec)
7863 static tree
7864 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7865 bool no_toplevel_fold_p,
7866 bool decltype_p,
7867 enum cp_parser_prec prec,
7868 cp_id_kind * pidk)
7870 cp_parser_expression_stack stack;
7871 cp_parser_expression_stack_entry *sp = &stack[0];
7872 cp_parser_expression_stack_entry current;
7873 tree rhs;
7874 cp_token *token;
7875 enum tree_code rhs_type;
7876 enum cp_parser_prec new_prec, lookahead_prec;
7877 tree overload;
7879 /* Parse the first expression. */
7880 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7881 cast_p, decltype_p, pidk);
7882 current.lhs_type = ERROR_MARK;
7883 current.prec = prec;
7885 if (cp_parser_error_occurred (parser))
7886 return error_mark_node;
7888 for (;;)
7890 /* Get an operator token. */
7891 token = cp_lexer_peek_token (parser->lexer);
7893 if (warn_cxx0x_compat
7894 && token->type == CPP_RSHIFT
7895 && !parser->greater_than_is_operator_p)
7897 if (warning_at (token->location, OPT_Wc__0x_compat,
7898 "%<>>%> operator is treated"
7899 " as two right angle brackets in C++11"))
7900 inform (token->location,
7901 "suggest parentheses around %<>>%> expression");
7904 new_prec = TOKEN_PRECEDENCE (token);
7906 /* Popping an entry off the stack means we completed a subexpression:
7907 - either we found a token which is not an operator (`>' where it is not
7908 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7909 will happen repeatedly;
7910 - or, we found an operator which has lower priority. This is the case
7911 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7912 parsing `3 * 4'. */
7913 if (new_prec <= current.prec)
7915 if (sp == stack)
7916 break;
7917 else
7918 goto pop;
7921 get_rhs:
7922 current.tree_type = binops_by_token[token->type].tree_type;
7923 current.loc = token->location;
7925 /* We used the operator token. */
7926 cp_lexer_consume_token (parser->lexer);
7928 /* For "false && x" or "true || x", x will never be executed;
7929 disable warnings while evaluating it. */
7930 if (current.tree_type == TRUTH_ANDIF_EXPR)
7931 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7932 else if (current.tree_type == TRUTH_ORIF_EXPR)
7933 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7935 /* Extract another operand. It may be the RHS of this expression
7936 or the LHS of a new, higher priority expression. */
7937 rhs = cp_parser_simple_cast_expression (parser);
7938 rhs_type = ERROR_MARK;
7940 /* Get another operator token. Look up its precedence to avoid
7941 building a useless (immediately popped) stack entry for common
7942 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7943 token = cp_lexer_peek_token (parser->lexer);
7944 lookahead_prec = TOKEN_PRECEDENCE (token);
7945 if (lookahead_prec > new_prec)
7947 /* ... and prepare to parse the RHS of the new, higher priority
7948 expression. Since precedence levels on the stack are
7949 monotonically increasing, we do not have to care about
7950 stack overflows. */
7951 *sp = current;
7952 ++sp;
7953 current.lhs = rhs;
7954 current.lhs_type = rhs_type;
7955 current.prec = new_prec;
7956 new_prec = lookahead_prec;
7957 goto get_rhs;
7959 pop:
7960 lookahead_prec = new_prec;
7961 /* If the stack is not empty, we have parsed into LHS the right side
7962 (`4' in the example above) of an expression we had suspended.
7963 We can use the information on the stack to recover the LHS (`3')
7964 from the stack together with the tree code (`MULT_EXPR'), and
7965 the precedence of the higher level subexpression
7966 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7967 which will be used to actually build the additive expression. */
7968 rhs = current.lhs;
7969 rhs_type = current.lhs_type;
7970 --sp;
7971 current = *sp;
7974 /* Undo the disabling of warnings done above. */
7975 if (current.tree_type == TRUTH_ANDIF_EXPR)
7976 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7977 else if (current.tree_type == TRUTH_ORIF_EXPR)
7978 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7980 overload = NULL;
7981 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7982 ERROR_MARK for everything that is not a binary expression.
7983 This makes warn_about_parentheses miss some warnings that
7984 involve unary operators. For unary expressions we should
7985 pass the correct tree_code unless the unary expression was
7986 surrounded by parentheses.
7988 if (no_toplevel_fold_p
7989 && lookahead_prec <= current.prec
7990 && sp == stack)
7991 current.lhs = build2 (current.tree_type,
7992 TREE_CODE_CLASS (current.tree_type)
7993 == tcc_comparison
7994 ? boolean_type_node : TREE_TYPE (current.lhs),
7995 current.lhs, rhs);
7996 else
7997 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7998 current.lhs, current.lhs_type,
7999 rhs, rhs_type, &overload,
8000 complain_flags (decltype_p));
8001 current.lhs_type = current.tree_type;
8002 if (EXPR_P (current.lhs))
8003 SET_EXPR_LOCATION (current.lhs, current.loc);
8005 /* If the binary operator required the use of an overloaded operator,
8006 then this expression cannot be an integral constant-expression.
8007 An overloaded operator can be used even if both operands are
8008 otherwise permissible in an integral constant-expression if at
8009 least one of the operands is of enumeration type. */
8011 if (overload
8012 && cp_parser_non_integral_constant_expression (parser,
8013 NIC_OVERLOADED))
8014 return error_mark_node;
8017 return current.lhs;
8020 static tree
8021 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8022 bool no_toplevel_fold_p,
8023 enum cp_parser_prec prec,
8024 cp_id_kind * pidk)
8026 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8027 /*decltype*/false, prec, pidk);
8030 /* Parse the `? expression : assignment-expression' part of a
8031 conditional-expression. The LOGICAL_OR_EXPR is the
8032 logical-or-expression that started the conditional-expression.
8033 Returns a representation of the entire conditional-expression.
8035 This routine is used by cp_parser_assignment_expression.
8037 ? expression : assignment-expression
8039 GNU Extensions:
8041 ? : assignment-expression */
8043 static tree
8044 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8046 tree expr;
8047 tree assignment_expr;
8048 struct cp_token *token;
8049 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8051 /* Consume the `?' token. */
8052 cp_lexer_consume_token (parser->lexer);
8053 token = cp_lexer_peek_token (parser->lexer);
8054 if (cp_parser_allow_gnu_extensions_p (parser)
8055 && token->type == CPP_COLON)
8057 pedwarn (token->location, OPT_Wpedantic,
8058 "ISO C++ does not allow ?: with omitted middle operand");
8059 /* Implicit true clause. */
8060 expr = NULL_TREE;
8061 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8062 warn_for_omitted_condop (token->location, logical_or_expr);
8064 else
8066 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8067 parser->colon_corrects_to_scope_p = false;
8068 /* Parse the expression. */
8069 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8070 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8071 c_inhibit_evaluation_warnings +=
8072 ((logical_or_expr == truthvalue_true_node)
8073 - (logical_or_expr == truthvalue_false_node));
8074 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8077 /* The next token should be a `:'. */
8078 cp_parser_require (parser, CPP_COLON, RT_COLON);
8079 /* Parse the assignment-expression. */
8080 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8081 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8083 /* Build the conditional-expression. */
8084 return build_x_conditional_expr (loc, logical_or_expr,
8085 expr,
8086 assignment_expr,
8087 tf_warning_or_error);
8090 /* Parse an assignment-expression.
8092 assignment-expression:
8093 conditional-expression
8094 logical-or-expression assignment-operator assignment_expression
8095 throw-expression
8097 CAST_P is true if this expression is the target of a cast.
8098 DECLTYPE_P is true if this expression is the operand of decltype.
8100 Returns a representation for the expression. */
8102 static tree
8103 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8104 bool decltype_p, cp_id_kind * pidk)
8106 tree expr;
8108 /* If the next token is the `throw' keyword, then we're looking at
8109 a throw-expression. */
8110 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8111 expr = cp_parser_throw_expression (parser);
8112 /* Otherwise, it must be that we are looking at a
8113 logical-or-expression. */
8114 else
8116 /* Parse the binary expressions (logical-or-expression). */
8117 expr = cp_parser_binary_expression (parser, cast_p, false,
8118 decltype_p,
8119 PREC_NOT_OPERATOR, pidk);
8120 /* If the next token is a `?' then we're actually looking at a
8121 conditional-expression. */
8122 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8123 return cp_parser_question_colon_clause (parser, expr);
8124 else
8126 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8128 /* If it's an assignment-operator, we're using the second
8129 production. */
8130 enum tree_code assignment_operator
8131 = cp_parser_assignment_operator_opt (parser);
8132 if (assignment_operator != ERROR_MARK)
8134 bool non_constant_p;
8135 location_t saved_input_location;
8137 /* Parse the right-hand side of the assignment. */
8138 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8140 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8141 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8143 /* An assignment may not appear in a
8144 constant-expression. */
8145 if (cp_parser_non_integral_constant_expression (parser,
8146 NIC_ASSIGNMENT))
8147 return error_mark_node;
8148 /* Build the assignment expression. Its default
8149 location is the location of the '=' token. */
8150 saved_input_location = input_location;
8151 input_location = loc;
8152 expr = build_x_modify_expr (loc, expr,
8153 assignment_operator,
8154 rhs,
8155 complain_flags (decltype_p));
8156 input_location = saved_input_location;
8161 return expr;
8164 static tree
8165 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8166 cp_id_kind * pidk)
8168 return cp_parser_assignment_expression (parser, cast_p,
8169 /*decltype*/false, pidk);
8172 /* Parse an (optional) assignment-operator.
8174 assignment-operator: one of
8175 = *= /= %= += -= >>= <<= &= ^= |=
8177 GNU Extension:
8179 assignment-operator: one of
8180 <?= >?=
8182 If the next token is an assignment operator, the corresponding tree
8183 code is returned, and the token is consumed. For example, for
8184 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8185 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8186 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8187 operator, ERROR_MARK is returned. */
8189 static enum tree_code
8190 cp_parser_assignment_operator_opt (cp_parser* parser)
8192 enum tree_code op;
8193 cp_token *token;
8195 /* Peek at the next token. */
8196 token = cp_lexer_peek_token (parser->lexer);
8198 switch (token->type)
8200 case CPP_EQ:
8201 op = NOP_EXPR;
8202 break;
8204 case CPP_MULT_EQ:
8205 op = MULT_EXPR;
8206 break;
8208 case CPP_DIV_EQ:
8209 op = TRUNC_DIV_EXPR;
8210 break;
8212 case CPP_MOD_EQ:
8213 op = TRUNC_MOD_EXPR;
8214 break;
8216 case CPP_PLUS_EQ:
8217 op = PLUS_EXPR;
8218 break;
8220 case CPP_MINUS_EQ:
8221 op = MINUS_EXPR;
8222 break;
8224 case CPP_RSHIFT_EQ:
8225 op = RSHIFT_EXPR;
8226 break;
8228 case CPP_LSHIFT_EQ:
8229 op = LSHIFT_EXPR;
8230 break;
8232 case CPP_AND_EQ:
8233 op = BIT_AND_EXPR;
8234 break;
8236 case CPP_XOR_EQ:
8237 op = BIT_XOR_EXPR;
8238 break;
8240 case CPP_OR_EQ:
8241 op = BIT_IOR_EXPR;
8242 break;
8244 default:
8245 /* Nothing else is an assignment operator. */
8246 op = ERROR_MARK;
8249 /* If it was an assignment operator, consume it. */
8250 if (op != ERROR_MARK)
8251 cp_lexer_consume_token (parser->lexer);
8253 return op;
8256 /* Parse an expression.
8258 expression:
8259 assignment-expression
8260 expression , assignment-expression
8262 CAST_P is true if this expression is the target of a cast.
8263 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8264 except possibly parenthesized or on the RHS of a comma (N3276).
8266 Returns a representation of the expression. */
8268 static tree
8269 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8270 cp_id_kind * pidk)
8272 tree expression = NULL_TREE;
8273 location_t loc = UNKNOWN_LOCATION;
8275 while (true)
8277 tree assignment_expression;
8279 /* Parse the next assignment-expression. */
8280 assignment_expression
8281 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8283 /* We don't create a temporary for a call that is the immediate operand
8284 of decltype or on the RHS of a comma. But when we see a comma, we
8285 need to create a temporary for a call on the LHS. */
8286 if (decltype_p && !processing_template_decl
8287 && TREE_CODE (assignment_expression) == CALL_EXPR
8288 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8289 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8290 assignment_expression
8291 = build_cplus_new (TREE_TYPE (assignment_expression),
8292 assignment_expression, tf_warning_or_error);
8294 /* If this is the first assignment-expression, we can just
8295 save it away. */
8296 if (!expression)
8297 expression = assignment_expression;
8298 else
8299 expression = build_x_compound_expr (loc, expression,
8300 assignment_expression,
8301 complain_flags (decltype_p));
8302 /* If the next token is not a comma, then we are done with the
8303 expression. */
8304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8305 break;
8306 /* Consume the `,'. */
8307 loc = cp_lexer_peek_token (parser->lexer)->location;
8308 cp_lexer_consume_token (parser->lexer);
8309 /* A comma operator cannot appear in a constant-expression. */
8310 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8311 expression = error_mark_node;
8314 return expression;
8317 static inline tree
8318 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8320 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8323 /* Parse a constant-expression.
8325 constant-expression:
8326 conditional-expression
8328 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8329 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8330 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8331 is false, NON_CONSTANT_P should be NULL. */
8333 static tree
8334 cp_parser_constant_expression (cp_parser* parser,
8335 bool allow_non_constant_p,
8336 bool *non_constant_p)
8338 bool saved_integral_constant_expression_p;
8339 bool saved_allow_non_integral_constant_expression_p;
8340 bool saved_non_integral_constant_expression_p;
8341 tree expression;
8343 /* It might seem that we could simply parse the
8344 conditional-expression, and then check to see if it were
8345 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8346 one that the compiler can figure out is constant, possibly after
8347 doing some simplifications or optimizations. The standard has a
8348 precise definition of constant-expression, and we must honor
8349 that, even though it is somewhat more restrictive.
8351 For example:
8353 int i[(2, 3)];
8355 is not a legal declaration, because `(2, 3)' is not a
8356 constant-expression. The `,' operator is forbidden in a
8357 constant-expression. However, GCC's constant-folding machinery
8358 will fold this operation to an INTEGER_CST for `3'. */
8360 /* Save the old settings. */
8361 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8362 saved_allow_non_integral_constant_expression_p
8363 = parser->allow_non_integral_constant_expression_p;
8364 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8365 /* We are now parsing a constant-expression. */
8366 parser->integral_constant_expression_p = true;
8367 parser->allow_non_integral_constant_expression_p
8368 = (allow_non_constant_p || cxx_dialect >= cxx11);
8369 parser->non_integral_constant_expression_p = false;
8370 /* Although the grammar says "conditional-expression", we parse an
8371 "assignment-expression", which also permits "throw-expression"
8372 and the use of assignment operators. In the case that
8373 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8374 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8375 actually essential that we look for an assignment-expression.
8376 For example, cp_parser_initializer_clauses uses this function to
8377 determine whether a particular assignment-expression is in fact
8378 constant. */
8379 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8380 /* Restore the old settings. */
8381 parser->integral_constant_expression_p
8382 = saved_integral_constant_expression_p;
8383 parser->allow_non_integral_constant_expression_p
8384 = saved_allow_non_integral_constant_expression_p;
8385 if (cxx_dialect >= cxx11)
8387 /* Require an rvalue constant expression here; that's what our
8388 callers expect. Reference constant expressions are handled
8389 separately in e.g. cp_parser_template_argument. */
8390 bool is_const = potential_rvalue_constant_expression (expression);
8391 parser->non_integral_constant_expression_p = !is_const;
8392 if (!is_const && !allow_non_constant_p)
8393 require_potential_rvalue_constant_expression (expression);
8395 if (allow_non_constant_p)
8396 *non_constant_p = parser->non_integral_constant_expression_p;
8397 parser->non_integral_constant_expression_p
8398 = saved_non_integral_constant_expression_p;
8400 return expression;
8403 /* Parse __builtin_offsetof.
8405 offsetof-expression:
8406 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8408 offsetof-member-designator:
8409 id-expression
8410 | offsetof-member-designator "." id-expression
8411 | offsetof-member-designator "[" expression "]"
8412 | offsetof-member-designator "->" id-expression */
8414 static tree
8415 cp_parser_builtin_offsetof (cp_parser *parser)
8417 int save_ice_p, save_non_ice_p;
8418 tree type, expr;
8419 cp_id_kind dummy;
8420 cp_token *token;
8422 /* We're about to accept non-integral-constant things, but will
8423 definitely yield an integral constant expression. Save and
8424 restore these values around our local parsing. */
8425 save_ice_p = parser->integral_constant_expression_p;
8426 save_non_ice_p = parser->non_integral_constant_expression_p;
8428 /* Consume the "__builtin_offsetof" token. */
8429 cp_lexer_consume_token (parser->lexer);
8430 /* Consume the opening `('. */
8431 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8432 /* Parse the type-id. */
8433 type = cp_parser_type_id (parser);
8434 /* Look for the `,'. */
8435 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8436 token = cp_lexer_peek_token (parser->lexer);
8438 /* Build the (type *)null that begins the traditional offsetof macro. */
8439 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8440 tf_warning_or_error);
8442 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8443 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8444 true, &dummy, token->location);
8445 while (true)
8447 token = cp_lexer_peek_token (parser->lexer);
8448 switch (token->type)
8450 case CPP_OPEN_SQUARE:
8451 /* offsetof-member-designator "[" expression "]" */
8452 expr = cp_parser_postfix_open_square_expression (parser, expr,
8453 true, false);
8454 break;
8456 case CPP_DEREF:
8457 /* offsetof-member-designator "->" identifier */
8458 expr = grok_array_decl (token->location, expr,
8459 integer_zero_node, false);
8460 /* FALLTHRU */
8462 case CPP_DOT:
8463 /* offsetof-member-designator "." identifier */
8464 cp_lexer_consume_token (parser->lexer);
8465 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8466 expr, true, &dummy,
8467 token->location);
8468 break;
8470 case CPP_CLOSE_PAREN:
8471 /* Consume the ")" token. */
8472 cp_lexer_consume_token (parser->lexer);
8473 goto success;
8475 default:
8476 /* Error. We know the following require will fail, but
8477 that gives the proper error message. */
8478 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8479 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8480 expr = error_mark_node;
8481 goto failure;
8485 success:
8486 /* If we're processing a template, we can't finish the semantics yet.
8487 Otherwise we can fold the entire expression now. */
8488 if (processing_template_decl)
8489 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8490 else
8491 expr = finish_offsetof (expr);
8493 failure:
8494 parser->integral_constant_expression_p = save_ice_p;
8495 parser->non_integral_constant_expression_p = save_non_ice_p;
8497 return expr;
8500 /* Parse a trait expression.
8502 Returns a representation of the expression, the underlying type
8503 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8505 static tree
8506 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8508 cp_trait_kind kind;
8509 tree type1, type2 = NULL_TREE;
8510 bool binary = false;
8511 cp_decl_specifier_seq decl_specs;
8513 switch (keyword)
8515 case RID_HAS_NOTHROW_ASSIGN:
8516 kind = CPTK_HAS_NOTHROW_ASSIGN;
8517 break;
8518 case RID_HAS_NOTHROW_CONSTRUCTOR:
8519 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8520 break;
8521 case RID_HAS_NOTHROW_COPY:
8522 kind = CPTK_HAS_NOTHROW_COPY;
8523 break;
8524 case RID_HAS_TRIVIAL_ASSIGN:
8525 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8526 break;
8527 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8528 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8529 break;
8530 case RID_HAS_TRIVIAL_COPY:
8531 kind = CPTK_HAS_TRIVIAL_COPY;
8532 break;
8533 case RID_HAS_TRIVIAL_DESTRUCTOR:
8534 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8535 break;
8536 case RID_HAS_VIRTUAL_DESTRUCTOR:
8537 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8538 break;
8539 case RID_IS_ABSTRACT:
8540 kind = CPTK_IS_ABSTRACT;
8541 break;
8542 case RID_IS_BASE_OF:
8543 kind = CPTK_IS_BASE_OF;
8544 binary = true;
8545 break;
8546 case RID_IS_CLASS:
8547 kind = CPTK_IS_CLASS;
8548 break;
8549 case RID_IS_CONVERTIBLE_TO:
8550 kind = CPTK_IS_CONVERTIBLE_TO;
8551 binary = true;
8552 break;
8553 case RID_IS_EMPTY:
8554 kind = CPTK_IS_EMPTY;
8555 break;
8556 case RID_IS_ENUM:
8557 kind = CPTK_IS_ENUM;
8558 break;
8559 case RID_IS_FINAL:
8560 kind = CPTK_IS_FINAL;
8561 break;
8562 case RID_IS_LITERAL_TYPE:
8563 kind = CPTK_IS_LITERAL_TYPE;
8564 break;
8565 case RID_IS_POD:
8566 kind = CPTK_IS_POD;
8567 break;
8568 case RID_IS_POLYMORPHIC:
8569 kind = CPTK_IS_POLYMORPHIC;
8570 break;
8571 case RID_IS_SAME_AS:
8572 kind = CPTK_IS_SAME_AS;
8573 binary = true;
8574 break;
8575 case RID_IS_STD_LAYOUT:
8576 kind = CPTK_IS_STD_LAYOUT;
8577 break;
8578 case RID_IS_TRIVIAL:
8579 kind = CPTK_IS_TRIVIAL;
8580 break;
8581 case RID_IS_UNION:
8582 kind = CPTK_IS_UNION;
8583 break;
8584 case RID_UNDERLYING_TYPE:
8585 kind = CPTK_UNDERLYING_TYPE;
8586 break;
8587 case RID_BASES:
8588 kind = CPTK_BASES;
8589 break;
8590 case RID_DIRECT_BASES:
8591 kind = CPTK_DIRECT_BASES;
8592 break;
8593 default:
8594 gcc_unreachable ();
8597 /* Consume the token. */
8598 cp_lexer_consume_token (parser->lexer);
8600 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8602 type1 = cp_parser_type_id (parser);
8604 if (type1 == error_mark_node)
8605 return error_mark_node;
8607 /* Build a trivial decl-specifier-seq. */
8608 clear_decl_specs (&decl_specs);
8609 decl_specs.type = type1;
8611 /* Call grokdeclarator to figure out what type this is. */
8612 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8613 /*initialized=*/0, /*attrlist=*/NULL);
8615 if (binary)
8617 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8619 type2 = cp_parser_type_id (parser);
8621 if (type2 == error_mark_node)
8622 return error_mark_node;
8624 /* Build a trivial decl-specifier-seq. */
8625 clear_decl_specs (&decl_specs);
8626 decl_specs.type = type2;
8628 /* Call grokdeclarator to figure out what type this is. */
8629 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8630 /*initialized=*/0, /*attrlist=*/NULL);
8633 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8635 /* Complete the trait expression, which may mean either processing
8636 the trait expr now or saving it for template instantiation. */
8637 switch(kind)
8639 case CPTK_UNDERLYING_TYPE:
8640 return finish_underlying_type (type1);
8641 case CPTK_BASES:
8642 return finish_bases (type1, false);
8643 case CPTK_DIRECT_BASES:
8644 return finish_bases (type1, true);
8645 default:
8646 return finish_trait_expr (kind, type1, type2);
8650 /* Lambdas that appear in variable initializer or default argument scope
8651 get that in their mangling, so we need to record it. We might as well
8652 use the count for function and namespace scopes as well. */
8653 static GTY(()) tree lambda_scope;
8654 static GTY(()) int lambda_count;
8655 typedef struct GTY(()) tree_int
8657 tree t;
8658 int i;
8659 } tree_int;
8660 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8662 static void
8663 start_lambda_scope (tree decl)
8665 tree_int ti;
8666 gcc_assert (decl);
8667 /* Once we're inside a function, we ignore other scopes and just push
8668 the function again so that popping works properly. */
8669 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8670 decl = current_function_decl;
8671 ti.t = lambda_scope;
8672 ti.i = lambda_count;
8673 vec_safe_push (lambda_scope_stack, ti);
8674 if (lambda_scope != decl)
8676 /* Don't reset the count if we're still in the same function. */
8677 lambda_scope = decl;
8678 lambda_count = 0;
8682 static void
8683 record_lambda_scope (tree lambda)
8685 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8686 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8689 static void
8690 finish_lambda_scope (void)
8692 tree_int *p = &lambda_scope_stack->last ();
8693 if (lambda_scope != p->t)
8695 lambda_scope = p->t;
8696 lambda_count = p->i;
8698 lambda_scope_stack->pop ();
8701 /* Parse a lambda expression.
8703 lambda-expression:
8704 lambda-introducer lambda-declarator [opt] compound-statement
8706 Returns a representation of the expression. */
8708 static tree
8709 cp_parser_lambda_expression (cp_parser* parser)
8711 tree lambda_expr = build_lambda_expr ();
8712 tree type;
8713 bool ok;
8715 LAMBDA_EXPR_LOCATION (lambda_expr)
8716 = cp_lexer_peek_token (parser->lexer)->location;
8718 if (cp_unevaluated_operand)
8719 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8720 "lambda-expression in unevaluated context");
8722 /* We may be in the middle of deferred access check. Disable
8723 it now. */
8724 push_deferring_access_checks (dk_no_deferred);
8726 cp_parser_lambda_introducer (parser, lambda_expr);
8728 type = begin_lambda_type (lambda_expr);
8729 if (type == error_mark_node)
8730 return error_mark_node;
8732 record_lambda_scope (lambda_expr);
8734 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8735 determine_visibility (TYPE_NAME (type));
8737 /* Now that we've started the type, add the capture fields for any
8738 explicit captures. */
8739 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8742 /* Inside the class, surrounding template-parameter-lists do not apply. */
8743 unsigned int saved_num_template_parameter_lists
8744 = parser->num_template_parameter_lists;
8745 unsigned char in_statement = parser->in_statement;
8746 bool in_switch_statement_p = parser->in_switch_statement_p;
8747 bool fully_implicit_function_template_p
8748 = parser->fully_implicit_function_template_p;
8749 tree implicit_template_parms = parser->implicit_template_parms;
8750 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8752 parser->num_template_parameter_lists = 0;
8753 parser->in_statement = 0;
8754 parser->in_switch_statement_p = false;
8755 parser->fully_implicit_function_template_p = false;
8756 parser->implicit_template_parms = 0;
8757 parser->implicit_template_scope = 0;
8759 /* By virtue of defining a local class, a lambda expression has access to
8760 the private variables of enclosing classes. */
8762 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8764 if (ok)
8765 cp_parser_lambda_body (parser, lambda_expr);
8766 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8767 cp_parser_skip_to_end_of_block_or_statement (parser);
8769 /* The capture list was built up in reverse order; fix that now. */
8770 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8771 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8773 if (ok)
8774 maybe_add_lambda_conv_op (type);
8776 type = finish_struct (type, /*attributes=*/NULL_TREE);
8778 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8779 parser->in_statement = in_statement;
8780 parser->in_switch_statement_p = in_switch_statement_p;
8781 parser->fully_implicit_function_template_p
8782 = fully_implicit_function_template_p;
8783 parser->implicit_template_parms = implicit_template_parms;
8784 parser->implicit_template_scope = implicit_template_scope;
8787 pop_deferring_access_checks ();
8789 /* This field is only used during parsing of the lambda. */
8790 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8792 /* This lambda shouldn't have any proxies left at this point. */
8793 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8794 /* And now that we're done, push proxies for an enclosing lambda. */
8795 insert_pending_capture_proxies ();
8797 if (ok)
8798 return build_lambda_object (lambda_expr);
8799 else
8800 return error_mark_node;
8803 /* Parse the beginning of a lambda expression.
8805 lambda-introducer:
8806 [ lambda-capture [opt] ]
8808 LAMBDA_EXPR is the current representation of the lambda expression. */
8810 static void
8811 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8813 /* Need commas after the first capture. */
8814 bool first = true;
8816 /* Eat the leading `['. */
8817 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8819 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8820 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8821 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8822 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8823 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8824 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8826 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8828 cp_lexer_consume_token (parser->lexer);
8829 first = false;
8832 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8834 cp_token* capture_token;
8835 tree capture_id;
8836 tree capture_init_expr;
8837 cp_id_kind idk = CP_ID_KIND_NONE;
8838 bool explicit_init_p = false;
8840 enum capture_kind_type
8842 BY_COPY,
8843 BY_REFERENCE
8845 enum capture_kind_type capture_kind = BY_COPY;
8847 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8849 error ("expected end of capture-list");
8850 return;
8853 if (first)
8854 first = false;
8855 else
8856 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8858 /* Possibly capture `this'. */
8859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8862 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8863 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8864 "with by-copy capture default");
8865 cp_lexer_consume_token (parser->lexer);
8866 add_capture (lambda_expr,
8867 /*id=*/this_identifier,
8868 /*initializer=*/finish_this_expr(),
8869 /*by_reference_p=*/false,
8870 explicit_init_p);
8871 continue;
8874 /* Remember whether we want to capture as a reference or not. */
8875 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8877 capture_kind = BY_REFERENCE;
8878 cp_lexer_consume_token (parser->lexer);
8881 /* Get the identifier. */
8882 capture_token = cp_lexer_peek_token (parser->lexer);
8883 capture_id = cp_parser_identifier (parser);
8885 if (capture_id == error_mark_node)
8886 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8887 delimiters, but I modified this to stop on unnested ']' as well. It
8888 was already changed to stop on unnested '}', so the
8889 "closing_parenthesis" name is no more misleading with my change. */
8891 cp_parser_skip_to_closing_parenthesis (parser,
8892 /*recovering=*/true,
8893 /*or_comma=*/true,
8894 /*consume_paren=*/true);
8895 break;
8898 /* Find the initializer for this capture. */
8899 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8900 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8901 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8903 bool direct, non_constant;
8904 /* An explicit initializer exists. */
8905 if (cxx_dialect < cxx1y)
8906 pedwarn (input_location, 0,
8907 "lambda capture initializers "
8908 "only available with -std=c++1y or -std=gnu++1y");
8909 capture_init_expr = cp_parser_initializer (parser, &direct,
8910 &non_constant);
8911 explicit_init_p = true;
8913 else
8915 const char* error_msg;
8917 /* Turn the identifier into an id-expression. */
8918 capture_init_expr
8919 = cp_parser_lookup_name_simple (parser, capture_id,
8920 capture_token->location);
8922 if (capture_init_expr == error_mark_node)
8924 unqualified_name_lookup_error (capture_id);
8925 continue;
8927 else if (DECL_P (capture_init_expr)
8928 && (!VAR_P (capture_init_expr)
8929 && TREE_CODE (capture_init_expr) != PARM_DECL))
8931 error_at (capture_token->location,
8932 "capture of non-variable %qD ",
8933 capture_init_expr);
8934 inform (0, "%q+#D declared here", capture_init_expr);
8935 continue;
8937 if (VAR_P (capture_init_expr)
8938 && decl_storage_duration (capture_init_expr) != dk_auto)
8940 pedwarn (capture_token->location, 0, "capture of variable "
8941 "%qD with non-automatic storage duration",
8942 capture_init_expr);
8943 inform (0, "%q+#D declared here", capture_init_expr);
8944 continue;
8947 capture_init_expr
8948 = finish_id_expression
8949 (capture_id,
8950 capture_init_expr,
8951 parser->scope,
8952 &idk,
8953 /*integral_constant_expression_p=*/false,
8954 /*allow_non_integral_constant_expression_p=*/false,
8955 /*non_integral_constant_expression_p=*/NULL,
8956 /*template_p=*/false,
8957 /*done=*/true,
8958 /*address_p=*/false,
8959 /*template_arg_p=*/false,
8960 &error_msg,
8961 capture_token->location);
8963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8965 cp_lexer_consume_token (parser->lexer);
8966 capture_init_expr = make_pack_expansion (capture_init_expr);
8968 else
8969 check_for_bare_parameter_packs (capture_init_expr);
8972 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8973 && !explicit_init_p)
8975 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8976 && capture_kind == BY_COPY)
8977 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8978 "of %qD redundant with by-copy capture default",
8979 capture_id);
8980 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8981 && capture_kind == BY_REFERENCE)
8982 pedwarn (capture_token->location, 0, "explicit by-reference "
8983 "capture of %qD redundant with by-reference capture "
8984 "default", capture_id);
8987 add_capture (lambda_expr,
8988 capture_id,
8989 capture_init_expr,
8990 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8991 explicit_init_p);
8994 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8997 /* Parse the (optional) middle of a lambda expression.
8999 lambda-declarator:
9000 < template-parameter-list [opt] >
9001 ( parameter-declaration-clause [opt] )
9002 attribute-specifier [opt]
9003 mutable [opt]
9004 exception-specification [opt]
9005 lambda-return-type-clause [opt]
9007 LAMBDA_EXPR is the current representation of the lambda expression. */
9009 static bool
9010 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9012 /* 5.1.1.4 of the standard says:
9013 If a lambda-expression does not include a lambda-declarator, it is as if
9014 the lambda-declarator were ().
9015 This means an empty parameter list, no attributes, and no exception
9016 specification. */
9017 tree param_list = void_list_node;
9018 tree attributes = NULL_TREE;
9019 tree exception_spec = NULL_TREE;
9020 tree template_param_list = NULL_TREE;
9022 /* The template-parameter-list is optional, but must begin with
9023 an opening angle if present. */
9024 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9026 if (cxx_dialect < cxx1y)
9027 pedwarn (parser->lexer->next_token->location, 0,
9028 "lambda templates are only available with "
9029 "-std=c++1y or -std=gnu++1y");
9031 cp_lexer_consume_token (parser->lexer);
9033 template_param_list = cp_parser_template_parameter_list (parser);
9035 cp_parser_skip_to_end_of_template_parameter_list (parser);
9037 /* We just processed one more parameter list. */
9038 ++parser->num_template_parameter_lists;
9041 /* The parameter-declaration-clause is optional (unless
9042 template-parameter-list was given), but must begin with an
9043 opening parenthesis if present. */
9044 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9046 cp_lexer_consume_token (parser->lexer);
9048 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9050 /* Parse parameters. */
9051 param_list = cp_parser_parameter_declaration_clause (parser);
9053 /* Default arguments shall not be specified in the
9054 parameter-declaration-clause of a lambda-declarator. */
9055 for (tree t = param_list; t; t = TREE_CHAIN (t))
9056 if (TREE_PURPOSE (t))
9057 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9058 "default argument specified for lambda parameter");
9060 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9062 attributes = cp_parser_attributes_opt (parser);
9064 /* Parse optional `mutable' keyword. */
9065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9067 cp_lexer_consume_token (parser->lexer);
9068 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9071 /* Parse optional exception specification. */
9072 exception_spec = cp_parser_exception_specification_opt (parser);
9074 /* Parse optional trailing return type. */
9075 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9077 cp_lexer_consume_token (parser->lexer);
9078 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9079 = cp_parser_trailing_type_id (parser);
9082 /* The function parameters must be in scope all the way until after the
9083 trailing-return-type in case of decltype. */
9084 pop_bindings_and_leave_scope ();
9086 else if (template_param_list != NULL_TREE) // generate diagnostic
9087 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9089 /* Create the function call operator.
9091 Messing with declarators like this is no uglier than building up the
9092 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9093 other code. */
9095 cp_decl_specifier_seq return_type_specs;
9096 cp_declarator* declarator;
9097 tree fco;
9098 int quals;
9099 void *p;
9101 clear_decl_specs (&return_type_specs);
9102 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9103 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9104 else
9105 /* Maybe we will deduce the return type later. */
9106 return_type_specs.type = make_auto ();
9108 p = obstack_alloc (&declarator_obstack, 0);
9110 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9111 sfk_none);
9113 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9114 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9115 declarator = make_call_declarator (declarator, param_list, quals,
9116 VIRT_SPEC_UNSPECIFIED,
9117 REF_QUAL_NONE,
9118 exception_spec,
9119 /*late_return_type=*/NULL_TREE);
9120 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9122 fco = grokmethod (&return_type_specs,
9123 declarator,
9124 attributes);
9125 if (fco != error_mark_node)
9127 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9128 DECL_ARTIFICIAL (fco) = 1;
9129 /* Give the object parameter a different name. */
9130 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9131 if (template_param_list)
9133 fco = finish_member_template_decl (fco);
9134 finish_template_decl (template_param_list);
9135 --parser->num_template_parameter_lists;
9137 else if (parser->fully_implicit_function_template_p)
9138 fco = finish_fully_implicit_template (parser, fco);
9141 finish_member_declaration (fco);
9143 obstack_free (&declarator_obstack, p);
9145 return (fco != error_mark_node);
9149 /* Parse the body of a lambda expression, which is simply
9151 compound-statement
9153 but which requires special handling.
9154 LAMBDA_EXPR is the current representation of the lambda expression. */
9156 static void
9157 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9159 bool nested = (current_function_decl != NULL_TREE);
9160 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9161 if (nested)
9162 push_function_context ();
9163 else
9164 /* Still increment function_depth so that we don't GC in the
9165 middle of an expression. */
9166 ++function_depth;
9167 /* Clear this in case we're in the middle of a default argument. */
9168 parser->local_variables_forbidden_p = false;
9170 /* Finish the function call operator
9171 - class_specifier
9172 + late_parsing_for_member
9173 + function_definition_after_declarator
9174 + ctor_initializer_opt_and_function_body */
9176 tree fco = lambda_function (lambda_expr);
9177 tree body;
9178 bool done = false;
9179 tree compound_stmt;
9180 tree cap;
9182 /* Let the front end know that we are going to be defining this
9183 function. */
9184 start_preparsed_function (fco,
9185 NULL_TREE,
9186 SF_PRE_PARSED | SF_INCLASS_INLINE);
9188 start_lambda_scope (fco);
9189 body = begin_function_body ();
9191 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9192 goto out;
9194 /* Push the proxies for any explicit captures. */
9195 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9196 cap = TREE_CHAIN (cap))
9197 build_capture_proxy (TREE_PURPOSE (cap));
9199 compound_stmt = begin_compound_stmt (0);
9201 /* 5.1.1.4 of the standard says:
9202 If a lambda-expression does not include a trailing-return-type, it
9203 is as if the trailing-return-type denotes the following type:
9204 * if the compound-statement is of the form
9205 { return attribute-specifier [opt] expression ; }
9206 the type of the returned expression after lvalue-to-rvalue
9207 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9208 (_conv.array_ 4.2), and function-to-pointer conversion
9209 (_conv.func_ 4.3);
9210 * otherwise, void. */
9212 /* In a lambda that has neither a lambda-return-type-clause
9213 nor a deducible form, errors should be reported for return statements
9214 in the body. Since we used void as the placeholder return type, parsing
9215 the body as usual will give such desired behavior. */
9216 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9217 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9218 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9220 tree expr = NULL_TREE;
9221 cp_id_kind idk = CP_ID_KIND_NONE;
9223 /* Parse tentatively in case there's more after the initial return
9224 statement. */
9225 cp_parser_parse_tentatively (parser);
9227 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9229 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9231 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9232 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9234 if (cp_parser_parse_definitely (parser))
9236 if (!processing_template_decl)
9237 apply_deduced_return_type (fco, lambda_return_type (expr));
9239 /* Will get error here if type not deduced yet. */
9240 finish_return_stmt (expr);
9242 done = true;
9246 if (!done)
9248 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9249 cp_parser_label_declaration (parser);
9250 cp_parser_statement_seq_opt (parser, NULL_TREE);
9251 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9254 finish_compound_stmt (compound_stmt);
9256 out:
9257 finish_function_body (body);
9258 finish_lambda_scope ();
9260 /* Finish the function and generate code for it if necessary. */
9261 tree fn = finish_function (/*inline*/2);
9263 /* Only expand if the call op is not a template. */
9264 if (!DECL_TEMPLATE_INFO (fco))
9265 expand_or_defer_fn (fn);
9268 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9269 if (nested)
9270 pop_function_context();
9271 else
9272 --function_depth;
9275 /* Statements [gram.stmt.stmt] */
9277 /* Parse a statement.
9279 statement:
9280 labeled-statement
9281 expression-statement
9282 compound-statement
9283 selection-statement
9284 iteration-statement
9285 jump-statement
9286 declaration-statement
9287 try-block
9289 C++11:
9291 statement:
9292 labeled-statement
9293 attribute-specifier-seq (opt) expression-statement
9294 attribute-specifier-seq (opt) compound-statement
9295 attribute-specifier-seq (opt) selection-statement
9296 attribute-specifier-seq (opt) iteration-statement
9297 attribute-specifier-seq (opt) jump-statement
9298 declaration-statement
9299 attribute-specifier-seq (opt) try-block
9301 TM Extension:
9303 statement:
9304 atomic-statement
9306 IN_COMPOUND is true when the statement is nested inside a
9307 cp_parser_compound_statement; this matters for certain pragmas.
9309 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9310 is a (possibly labeled) if statement which is not enclosed in braces
9311 and has an else clause. This is used to implement -Wparentheses. */
9313 static void
9314 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9315 bool in_compound, bool *if_p)
9317 tree statement, std_attrs = NULL_TREE;
9318 cp_token *token;
9319 location_t statement_location, attrs_location;
9321 restart:
9322 if (if_p != NULL)
9323 *if_p = false;
9324 /* There is no statement yet. */
9325 statement = NULL_TREE;
9327 cp_lexer_save_tokens (parser->lexer);
9328 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9329 if (c_dialect_objc ())
9330 /* In obj-c++, seeing '[[' might be the either the beginning of
9331 c++11 attributes, or a nested objc-message-expression. So
9332 let's parse the c++11 attributes tentatively. */
9333 cp_parser_parse_tentatively (parser);
9334 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9335 if (c_dialect_objc ())
9337 if (!cp_parser_parse_definitely (parser))
9338 std_attrs = NULL_TREE;
9341 /* Peek at the next token. */
9342 token = cp_lexer_peek_token (parser->lexer);
9343 /* Remember the location of the first token in the statement. */
9344 statement_location = token->location;
9345 /* If this is a keyword, then that will often determine what kind of
9346 statement we have. */
9347 if (token->type == CPP_KEYWORD)
9349 enum rid keyword = token->keyword;
9351 switch (keyword)
9353 case RID_CASE:
9354 case RID_DEFAULT:
9355 /* Looks like a labeled-statement with a case label.
9356 Parse the label, and then use tail recursion to parse
9357 the statement. */
9358 cp_parser_label_for_labeled_statement (parser, std_attrs);
9359 goto restart;
9361 case RID_IF:
9362 case RID_SWITCH:
9363 statement = cp_parser_selection_statement (parser, if_p);
9364 break;
9366 case RID_WHILE:
9367 case RID_DO:
9368 case RID_FOR:
9369 statement = cp_parser_iteration_statement (parser, false);
9370 break;
9372 case RID_BREAK:
9373 case RID_CONTINUE:
9374 case RID_RETURN:
9375 case RID_GOTO:
9376 statement = cp_parser_jump_statement (parser);
9377 break;
9379 /* Objective-C++ exception-handling constructs. */
9380 case RID_AT_TRY:
9381 case RID_AT_CATCH:
9382 case RID_AT_FINALLY:
9383 case RID_AT_SYNCHRONIZED:
9384 case RID_AT_THROW:
9385 statement = cp_parser_objc_statement (parser);
9386 break;
9388 case RID_TRY:
9389 statement = cp_parser_try_block (parser);
9390 break;
9392 case RID_NAMESPACE:
9393 /* This must be a namespace alias definition. */
9394 cp_parser_declaration_statement (parser);
9395 return;
9397 case RID_TRANSACTION_ATOMIC:
9398 case RID_TRANSACTION_RELAXED:
9399 statement = cp_parser_transaction (parser, keyword);
9400 break;
9401 case RID_TRANSACTION_CANCEL:
9402 statement = cp_parser_transaction_cancel (parser);
9403 break;
9405 default:
9406 /* It might be a keyword like `int' that can start a
9407 declaration-statement. */
9408 break;
9411 else if (token->type == CPP_NAME)
9413 /* If the next token is a `:', then we are looking at a
9414 labeled-statement. */
9415 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9416 if (token->type == CPP_COLON)
9418 /* Looks like a labeled-statement with an ordinary label.
9419 Parse the label, and then use tail recursion to parse
9420 the statement. */
9422 cp_parser_label_for_labeled_statement (parser, std_attrs);
9423 goto restart;
9426 /* Anything that starts with a `{' must be a compound-statement. */
9427 else if (token->type == CPP_OPEN_BRACE)
9428 statement = cp_parser_compound_statement (parser, NULL, false, false);
9429 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9430 a statement all its own. */
9431 else if (token->type == CPP_PRAGMA)
9433 /* Only certain OpenMP pragmas are attached to statements, and thus
9434 are considered statements themselves. All others are not. In
9435 the context of a compound, accept the pragma as a "statement" and
9436 return so that we can check for a close brace. Otherwise we
9437 require a real statement and must go back and read one. */
9438 if (in_compound)
9439 cp_parser_pragma (parser, pragma_compound);
9440 else if (!cp_parser_pragma (parser, pragma_stmt))
9441 goto restart;
9442 return;
9444 else if (token->type == CPP_EOF)
9446 cp_parser_error (parser, "expected statement");
9447 return;
9450 /* Everything else must be a declaration-statement or an
9451 expression-statement. Try for the declaration-statement
9452 first, unless we are looking at a `;', in which case we know that
9453 we have an expression-statement. */
9454 if (!statement)
9456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9458 if (std_attrs != NULL_TREE)
9460 /* Attributes should be parsed as part of the the
9461 declaration, so let's un-parse them. */
9462 cp_lexer_rollback_tokens (parser->lexer);
9463 std_attrs = NULL_TREE;
9466 cp_parser_parse_tentatively (parser);
9467 /* Try to parse the declaration-statement. */
9468 cp_parser_declaration_statement (parser);
9469 /* If that worked, we're done. */
9470 if (cp_parser_parse_definitely (parser))
9471 return;
9473 /* Look for an expression-statement instead. */
9474 statement = cp_parser_expression_statement (parser, in_statement_expr);
9477 /* Set the line number for the statement. */
9478 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9479 SET_EXPR_LOCATION (statement, statement_location);
9481 /* Note that for now, we don't do anything with c++11 statements
9482 parsed at this level. */
9483 if (std_attrs != NULL_TREE)
9484 warning_at (attrs_location,
9485 OPT_Wattributes,
9486 "attributes at the beginning of statement are ignored");
9489 /* Parse the label for a labeled-statement, i.e.
9491 identifier :
9492 case constant-expression :
9493 default :
9495 GNU Extension:
9496 case constant-expression ... constant-expression : statement
9498 When a label is parsed without errors, the label is added to the
9499 parse tree by the finish_* functions, so this function doesn't
9500 have to return the label. */
9502 static void
9503 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9505 cp_token *token;
9506 tree label = NULL_TREE;
9507 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9509 /* The next token should be an identifier. */
9510 token = cp_lexer_peek_token (parser->lexer);
9511 if (token->type != CPP_NAME
9512 && token->type != CPP_KEYWORD)
9514 cp_parser_error (parser, "expected labeled-statement");
9515 return;
9518 parser->colon_corrects_to_scope_p = false;
9519 switch (token->keyword)
9521 case RID_CASE:
9523 tree expr, expr_hi;
9524 cp_token *ellipsis;
9526 /* Consume the `case' token. */
9527 cp_lexer_consume_token (parser->lexer);
9528 /* Parse the constant-expression. */
9529 expr = cp_parser_constant_expression (parser,
9530 /*allow_non_constant_p=*/false,
9531 NULL);
9533 ellipsis = cp_lexer_peek_token (parser->lexer);
9534 if (ellipsis->type == CPP_ELLIPSIS)
9536 /* Consume the `...' token. */
9537 cp_lexer_consume_token (parser->lexer);
9538 expr_hi =
9539 cp_parser_constant_expression (parser,
9540 /*allow_non_constant_p=*/false,
9541 NULL);
9542 /* We don't need to emit warnings here, as the common code
9543 will do this for us. */
9545 else
9546 expr_hi = NULL_TREE;
9548 if (parser->in_switch_statement_p)
9549 finish_case_label (token->location, expr, expr_hi);
9550 else
9551 error_at (token->location,
9552 "case label %qE not within a switch statement",
9553 expr);
9555 break;
9557 case RID_DEFAULT:
9558 /* Consume the `default' token. */
9559 cp_lexer_consume_token (parser->lexer);
9561 if (parser->in_switch_statement_p)
9562 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9563 else
9564 error_at (token->location, "case label not within a switch statement");
9565 break;
9567 default:
9568 /* Anything else must be an ordinary label. */
9569 label = finish_label_stmt (cp_parser_identifier (parser));
9570 break;
9573 /* Require the `:' token. */
9574 cp_parser_require (parser, CPP_COLON, RT_COLON);
9576 /* An ordinary label may optionally be followed by attributes.
9577 However, this is only permitted if the attributes are then
9578 followed by a semicolon. This is because, for backward
9579 compatibility, when parsing
9580 lab: __attribute__ ((unused)) int i;
9581 we want the attribute to attach to "i", not "lab". */
9582 if (label != NULL_TREE
9583 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9585 tree attrs;
9586 cp_parser_parse_tentatively (parser);
9587 attrs = cp_parser_gnu_attributes_opt (parser);
9588 if (attrs == NULL_TREE
9589 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9590 cp_parser_abort_tentative_parse (parser);
9591 else if (!cp_parser_parse_definitely (parser))
9593 else
9594 attributes = chainon (attributes, attrs);
9597 if (attributes != NULL_TREE)
9598 cplus_decl_attributes (&label, attributes, 0);
9600 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9603 /* Parse an expression-statement.
9605 expression-statement:
9606 expression [opt] ;
9608 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9609 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9610 indicates whether this expression-statement is part of an
9611 expression statement. */
9613 static tree
9614 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9616 tree statement = NULL_TREE;
9617 cp_token *token = cp_lexer_peek_token (parser->lexer);
9619 /* If the next token is a ';', then there is no expression
9620 statement. */
9621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9623 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9624 if (statement == error_mark_node
9625 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9627 cp_parser_skip_to_end_of_block_or_statement (parser);
9628 return error_mark_node;
9632 /* Give a helpful message for "A<T>::type t;" and the like. */
9633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9634 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9636 if (TREE_CODE (statement) == SCOPE_REF)
9637 error_at (token->location, "need %<typename%> before %qE because "
9638 "%qT is a dependent scope",
9639 statement, TREE_OPERAND (statement, 0));
9640 else if (is_overloaded_fn (statement)
9641 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9643 /* A::A a; */
9644 tree fn = get_first_fn (statement);
9645 error_at (token->location,
9646 "%<%T::%D%> names the constructor, not the type",
9647 DECL_CONTEXT (fn), DECL_NAME (fn));
9651 /* Consume the final `;'. */
9652 cp_parser_consume_semicolon_at_end_of_statement (parser);
9654 if (in_statement_expr
9655 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9656 /* This is the final expression statement of a statement
9657 expression. */
9658 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9659 else if (statement)
9660 statement = finish_expr_stmt (statement);
9662 return statement;
9665 /* Parse a compound-statement.
9667 compound-statement:
9668 { statement-seq [opt] }
9670 GNU extension:
9672 compound-statement:
9673 { label-declaration-seq [opt] statement-seq [opt] }
9675 label-declaration-seq:
9676 label-declaration
9677 label-declaration-seq label-declaration
9679 Returns a tree representing the statement. */
9681 static tree
9682 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9683 bool in_try, bool function_body)
9685 tree compound_stmt;
9687 /* Consume the `{'. */
9688 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9689 return error_mark_node;
9690 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9691 && !function_body)
9692 pedwarn (input_location, OPT_Wpedantic,
9693 "compound-statement in constexpr function");
9694 /* Begin the compound-statement. */
9695 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9696 /* If the next keyword is `__label__' we have a label declaration. */
9697 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9698 cp_parser_label_declaration (parser);
9699 /* Parse an (optional) statement-seq. */
9700 cp_parser_statement_seq_opt (parser, in_statement_expr);
9701 /* Finish the compound-statement. */
9702 finish_compound_stmt (compound_stmt);
9703 /* Consume the `}'. */
9704 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9706 return compound_stmt;
9709 /* Parse an (optional) statement-seq.
9711 statement-seq:
9712 statement
9713 statement-seq [opt] statement */
9715 static void
9716 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9718 /* Scan statements until there aren't any more. */
9719 while (true)
9721 cp_token *token = cp_lexer_peek_token (parser->lexer);
9723 /* If we are looking at a `}', then we have run out of
9724 statements; the same is true if we have reached the end
9725 of file, or have stumbled upon a stray '@end'. */
9726 if (token->type == CPP_CLOSE_BRACE
9727 || token->type == CPP_EOF
9728 || token->type == CPP_PRAGMA_EOL
9729 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9730 break;
9732 /* If we are in a compound statement and find 'else' then
9733 something went wrong. */
9734 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9736 if (parser->in_statement & IN_IF_STMT)
9737 break;
9738 else
9740 token = cp_lexer_consume_token (parser->lexer);
9741 error_at (token->location, "%<else%> without a previous %<if%>");
9745 /* Parse the statement. */
9746 cp_parser_statement (parser, in_statement_expr, true, NULL);
9750 /* Parse a selection-statement.
9752 selection-statement:
9753 if ( condition ) statement
9754 if ( condition ) statement else statement
9755 switch ( condition ) statement
9757 Returns the new IF_STMT or SWITCH_STMT.
9759 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9760 is a (possibly labeled) if statement which is not enclosed in
9761 braces and has an else clause. This is used to implement
9762 -Wparentheses. */
9764 static tree
9765 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9767 cp_token *token;
9768 enum rid keyword;
9770 if (if_p != NULL)
9771 *if_p = false;
9773 /* Peek at the next token. */
9774 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9776 /* See what kind of keyword it is. */
9777 keyword = token->keyword;
9778 switch (keyword)
9780 case RID_IF:
9781 case RID_SWITCH:
9783 tree statement;
9784 tree condition;
9786 /* Look for the `('. */
9787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9789 cp_parser_skip_to_end_of_statement (parser);
9790 return error_mark_node;
9793 /* Begin the selection-statement. */
9794 if (keyword == RID_IF)
9795 statement = begin_if_stmt ();
9796 else
9797 statement = begin_switch_stmt ();
9799 /* Parse the condition. */
9800 condition = cp_parser_condition (parser);
9801 /* Look for the `)'. */
9802 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9803 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9804 /*consume_paren=*/true);
9806 if (keyword == RID_IF)
9808 bool nested_if;
9809 unsigned char in_statement;
9811 /* Add the condition. */
9812 finish_if_stmt_cond (condition, statement);
9814 /* Parse the then-clause. */
9815 in_statement = parser->in_statement;
9816 parser->in_statement |= IN_IF_STMT;
9817 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9819 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9820 add_stmt (build_empty_stmt (loc));
9821 cp_lexer_consume_token (parser->lexer);
9822 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9823 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9824 "empty body in an %<if%> statement");
9825 nested_if = false;
9827 else
9828 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9829 parser->in_statement = in_statement;
9831 finish_then_clause (statement);
9833 /* If the next token is `else', parse the else-clause. */
9834 if (cp_lexer_next_token_is_keyword (parser->lexer,
9835 RID_ELSE))
9837 /* Consume the `else' keyword. */
9838 cp_lexer_consume_token (parser->lexer);
9839 begin_else_clause (statement);
9840 /* Parse the else-clause. */
9841 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9843 location_t loc;
9844 loc = cp_lexer_peek_token (parser->lexer)->location;
9845 warning_at (loc,
9846 OPT_Wempty_body, "suggest braces around "
9847 "empty body in an %<else%> statement");
9848 add_stmt (build_empty_stmt (loc));
9849 cp_lexer_consume_token (parser->lexer);
9851 else
9852 cp_parser_implicitly_scoped_statement (parser, NULL);
9854 finish_else_clause (statement);
9856 /* If we are currently parsing a then-clause, then
9857 IF_P will not be NULL. We set it to true to
9858 indicate that this if statement has an else clause.
9859 This may trigger the Wparentheses warning below
9860 when we get back up to the parent if statement. */
9861 if (if_p != NULL)
9862 *if_p = true;
9864 else
9866 /* This if statement does not have an else clause. If
9867 NESTED_IF is true, then the then-clause is an if
9868 statement which does have an else clause. We warn
9869 about the potential ambiguity. */
9870 if (nested_if)
9871 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9872 "suggest explicit braces to avoid ambiguous"
9873 " %<else%>");
9876 /* Now we're all done with the if-statement. */
9877 finish_if_stmt (statement);
9879 else
9881 bool in_switch_statement_p;
9882 unsigned char in_statement;
9884 /* Add the condition. */
9885 finish_switch_cond (condition, statement);
9887 /* Parse the body of the switch-statement. */
9888 in_switch_statement_p = parser->in_switch_statement_p;
9889 in_statement = parser->in_statement;
9890 parser->in_switch_statement_p = true;
9891 parser->in_statement |= IN_SWITCH_STMT;
9892 cp_parser_implicitly_scoped_statement (parser, NULL);
9893 parser->in_switch_statement_p = in_switch_statement_p;
9894 parser->in_statement = in_statement;
9896 /* Now we're all done with the switch-statement. */
9897 finish_switch_stmt (statement);
9900 return statement;
9902 break;
9904 default:
9905 cp_parser_error (parser, "expected selection-statement");
9906 return error_mark_node;
9910 /* Parse a condition.
9912 condition:
9913 expression
9914 type-specifier-seq declarator = initializer-clause
9915 type-specifier-seq declarator braced-init-list
9917 GNU Extension:
9919 condition:
9920 type-specifier-seq declarator asm-specification [opt]
9921 attributes [opt] = assignment-expression
9923 Returns the expression that should be tested. */
9925 static tree
9926 cp_parser_condition (cp_parser* parser)
9928 cp_decl_specifier_seq type_specifiers;
9929 const char *saved_message;
9930 int declares_class_or_enum;
9932 /* Try the declaration first. */
9933 cp_parser_parse_tentatively (parser);
9934 /* New types are not allowed in the type-specifier-seq for a
9935 condition. */
9936 saved_message = parser->type_definition_forbidden_message;
9937 parser->type_definition_forbidden_message
9938 = G_("types may not be defined in conditions");
9939 /* Parse the type-specifier-seq. */
9940 cp_parser_decl_specifier_seq (parser,
9941 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9942 &type_specifiers,
9943 &declares_class_or_enum);
9944 /* Restore the saved message. */
9945 parser->type_definition_forbidden_message = saved_message;
9946 /* If all is well, we might be looking at a declaration. */
9947 if (!cp_parser_error_occurred (parser))
9949 tree decl;
9950 tree asm_specification;
9951 tree attributes;
9952 cp_declarator *declarator;
9953 tree initializer = NULL_TREE;
9955 /* Parse the declarator. */
9956 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9957 /*ctor_dtor_or_conv_p=*/NULL,
9958 /*parenthesized_p=*/NULL,
9959 /*member_p=*/false);
9960 /* Parse the attributes. */
9961 attributes = cp_parser_attributes_opt (parser);
9962 /* Parse the asm-specification. */
9963 asm_specification = cp_parser_asm_specification_opt (parser);
9964 /* If the next token is not an `=' or '{', then we might still be
9965 looking at an expression. For example:
9967 if (A(a).x)
9969 looks like a decl-specifier-seq and a declarator -- but then
9970 there is no `=', so this is an expression. */
9971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9972 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9973 cp_parser_simulate_error (parser);
9975 /* If we did see an `=' or '{', then we are looking at a declaration
9976 for sure. */
9977 if (cp_parser_parse_definitely (parser))
9979 tree pushed_scope;
9980 bool non_constant_p;
9981 bool flags = LOOKUP_ONLYCONVERTING;
9983 /* Create the declaration. */
9984 decl = start_decl (declarator, &type_specifiers,
9985 /*initialized_p=*/true,
9986 attributes, /*prefix_attributes=*/NULL_TREE,
9987 &pushed_scope);
9989 /* Parse the initializer. */
9990 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9992 initializer = cp_parser_braced_list (parser, &non_constant_p);
9993 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9994 flags = 0;
9996 else
9998 /* Consume the `='. */
9999 cp_parser_require (parser, CPP_EQ, RT_EQ);
10000 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10002 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10003 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10005 /* Process the initializer. */
10006 cp_finish_decl (decl,
10007 initializer, !non_constant_p,
10008 asm_specification,
10009 flags);
10011 if (pushed_scope)
10012 pop_scope (pushed_scope);
10014 return convert_from_reference (decl);
10017 /* If we didn't even get past the declarator successfully, we are
10018 definitely not looking at a declaration. */
10019 else
10020 cp_parser_abort_tentative_parse (parser);
10022 /* Otherwise, we are looking at an expression. */
10023 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10026 /* Parses a for-statement or range-for-statement until the closing ')',
10027 not included. */
10029 static tree
10030 cp_parser_for (cp_parser *parser, bool ivdep)
10032 tree init, scope, decl;
10033 bool is_range_for;
10035 /* Begin the for-statement. */
10036 scope = begin_for_scope (&init);
10038 /* Parse the initialization. */
10039 is_range_for = cp_parser_for_init_statement (parser, &decl);
10041 if (is_range_for)
10042 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10043 else
10044 return cp_parser_c_for (parser, scope, init, ivdep);
10047 static tree
10048 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10050 /* Normal for loop */
10051 tree condition = NULL_TREE;
10052 tree expression = NULL_TREE;
10053 tree stmt;
10055 stmt = begin_for_stmt (scope, init);
10056 /* The for-init-statement has already been parsed in
10057 cp_parser_for_init_statement, so no work is needed here. */
10058 finish_for_init_stmt (stmt);
10060 /* If there's a condition, process it. */
10061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10062 condition = cp_parser_condition (parser);
10063 else if (ivdep)
10065 cp_parser_error (parser, "missing loop condition in loop with "
10066 "%<GCC ivdep%> pragma");
10067 condition = error_mark_node;
10069 finish_for_cond (condition, stmt, ivdep);
10070 /* Look for the `;'. */
10071 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10073 /* If there's an expression, process it. */
10074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10075 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10076 finish_for_expr (expression, stmt);
10078 return stmt;
10081 /* Tries to parse a range-based for-statement:
10083 range-based-for:
10084 decl-specifier-seq declarator : expression
10086 The decl-specifier-seq declarator and the `:' are already parsed by
10087 cp_parser_for_init_statement. If processing_template_decl it returns a
10088 newly created RANGE_FOR_STMT; if not, it is converted to a
10089 regular FOR_STMT. */
10091 static tree
10092 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10093 bool ivdep)
10095 tree stmt, range_expr;
10097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10099 bool expr_non_constant_p;
10100 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10102 else
10103 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10105 /* If in template, STMT is converted to a normal for-statement
10106 at instantiation. If not, it is done just ahead. */
10107 if (processing_template_decl)
10109 if (check_for_bare_parameter_packs (range_expr))
10110 range_expr = error_mark_node;
10111 stmt = begin_range_for_stmt (scope, init);
10112 if (ivdep)
10113 RANGE_FOR_IVDEP (stmt) = 1;
10114 finish_range_for_decl (stmt, range_decl, range_expr);
10115 if (!type_dependent_expression_p (range_expr)
10116 /* do_auto_deduction doesn't mess with template init-lists. */
10117 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10118 do_range_for_auto_deduction (range_decl, range_expr);
10120 else
10122 stmt = begin_for_stmt (scope, init);
10123 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10125 return stmt;
10128 /* Subroutine of cp_convert_range_for: given the initializer expression,
10129 builds up the range temporary. */
10131 static tree
10132 build_range_temp (tree range_expr)
10134 tree range_type, range_temp;
10136 /* Find out the type deduced by the declaration
10137 `auto &&__range = range_expr'. */
10138 range_type = cp_build_reference_type (make_auto (), true);
10139 range_type = do_auto_deduction (range_type, range_expr,
10140 type_uses_auto (range_type));
10142 /* Create the __range variable. */
10143 range_temp = build_decl (input_location, VAR_DECL,
10144 get_identifier ("__for_range"), range_type);
10145 TREE_USED (range_temp) = 1;
10146 DECL_ARTIFICIAL (range_temp) = 1;
10148 return range_temp;
10151 /* Used by cp_parser_range_for in template context: we aren't going to
10152 do a full conversion yet, but we still need to resolve auto in the
10153 type of the for-range-declaration if present. This is basically
10154 a shortcut version of cp_convert_range_for. */
10156 static void
10157 do_range_for_auto_deduction (tree decl, tree range_expr)
10159 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10160 if (auto_node)
10162 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10163 range_temp = convert_from_reference (build_range_temp (range_expr));
10164 iter_type = (cp_parser_perform_range_for_lookup
10165 (range_temp, &begin_dummy, &end_dummy));
10166 if (iter_type)
10168 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10169 iter_type);
10170 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10171 tf_warning_or_error);
10172 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10173 iter_decl, auto_node);
10178 /* Converts a range-based for-statement into a normal
10179 for-statement, as per the definition.
10181 for (RANGE_DECL : RANGE_EXPR)
10182 BLOCK
10184 should be equivalent to:
10187 auto &&__range = RANGE_EXPR;
10188 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10189 __begin != __end;
10190 ++__begin)
10192 RANGE_DECL = *__begin;
10193 BLOCK
10197 If RANGE_EXPR is an array:
10198 BEGIN_EXPR = __range
10199 END_EXPR = __range + ARRAY_SIZE(__range)
10200 Else if RANGE_EXPR has a member 'begin' or 'end':
10201 BEGIN_EXPR = __range.begin()
10202 END_EXPR = __range.end()
10203 Else:
10204 BEGIN_EXPR = begin(__range)
10205 END_EXPR = end(__range);
10207 If __range has a member 'begin' but not 'end', or vice versa, we must
10208 still use the second alternative (it will surely fail, however).
10209 When calling begin()/end() in the third alternative we must use
10210 argument dependent lookup, but always considering 'std' as an associated
10211 namespace. */
10213 tree
10214 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10215 bool ivdep)
10217 tree begin, end;
10218 tree iter_type, begin_expr, end_expr;
10219 tree condition, expression;
10221 if (range_decl == error_mark_node || range_expr == error_mark_node)
10222 /* If an error happened previously do nothing or else a lot of
10223 unhelpful errors would be issued. */
10224 begin_expr = end_expr = iter_type = error_mark_node;
10225 else
10227 tree range_temp;
10229 if (TREE_CODE (range_expr) == VAR_DECL
10230 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10231 /* Can't bind a reference to an array of runtime bound. */
10232 range_temp = range_expr;
10233 else
10235 range_temp = build_range_temp (range_expr);
10236 pushdecl (range_temp);
10237 cp_finish_decl (range_temp, range_expr,
10238 /*is_constant_init*/false, NULL_TREE,
10239 LOOKUP_ONLYCONVERTING);
10240 range_temp = convert_from_reference (range_temp);
10242 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10243 &begin_expr, &end_expr);
10246 /* The new for initialization statement. */
10247 begin = build_decl (input_location, VAR_DECL,
10248 get_identifier ("__for_begin"), iter_type);
10249 TREE_USED (begin) = 1;
10250 DECL_ARTIFICIAL (begin) = 1;
10251 pushdecl (begin);
10252 cp_finish_decl (begin, begin_expr,
10253 /*is_constant_init*/false, NULL_TREE,
10254 LOOKUP_ONLYCONVERTING);
10256 end = build_decl (input_location, VAR_DECL,
10257 get_identifier ("__for_end"), iter_type);
10258 TREE_USED (end) = 1;
10259 DECL_ARTIFICIAL (end) = 1;
10260 pushdecl (end);
10261 cp_finish_decl (end, end_expr,
10262 /*is_constant_init*/false, NULL_TREE,
10263 LOOKUP_ONLYCONVERTING);
10265 finish_for_init_stmt (statement);
10267 /* The new for condition. */
10268 condition = build_x_binary_op (input_location, NE_EXPR,
10269 begin, ERROR_MARK,
10270 end, ERROR_MARK,
10271 NULL, tf_warning_or_error);
10272 finish_for_cond (condition, statement, ivdep);
10274 /* The new increment expression. */
10275 expression = finish_unary_op_expr (input_location,
10276 PREINCREMENT_EXPR, begin,
10277 tf_warning_or_error);
10278 finish_for_expr (expression, statement);
10280 /* The declaration is initialized with *__begin inside the loop body. */
10281 cp_finish_decl (range_decl,
10282 build_x_indirect_ref (input_location, begin, RO_NULL,
10283 tf_warning_or_error),
10284 /*is_constant_init*/false, NULL_TREE,
10285 LOOKUP_ONLYCONVERTING);
10287 return statement;
10290 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10291 We need to solve both at the same time because the method used
10292 depends on the existence of members begin or end.
10293 Returns the type deduced for the iterator expression. */
10295 static tree
10296 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10298 if (error_operand_p (range))
10300 *begin = *end = error_mark_node;
10301 return error_mark_node;
10304 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10306 error ("range-based %<for%> expression of type %qT "
10307 "has incomplete type", TREE_TYPE (range));
10308 *begin = *end = error_mark_node;
10309 return error_mark_node;
10311 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10313 /* If RANGE is an array, we will use pointer arithmetic. */
10314 *begin = range;
10315 *end = build_binary_op (input_location, PLUS_EXPR,
10316 range,
10317 array_type_nelts_top (TREE_TYPE (range)),
10319 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10321 else
10323 /* If it is not an array, we must do a bit of magic. */
10324 tree id_begin, id_end;
10325 tree member_begin, member_end;
10327 *begin = *end = error_mark_node;
10329 id_begin = get_identifier ("begin");
10330 id_end = get_identifier ("end");
10331 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10332 /*protect=*/2, /*want_type=*/false,
10333 tf_warning_or_error);
10334 member_end = lookup_member (TREE_TYPE (range), id_end,
10335 /*protect=*/2, /*want_type=*/false,
10336 tf_warning_or_error);
10338 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10340 /* Use the member functions. */
10341 if (member_begin != NULL_TREE)
10342 *begin = cp_parser_range_for_member_function (range, id_begin);
10343 else
10344 error ("range-based %<for%> expression of type %qT has an "
10345 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10347 if (member_end != NULL_TREE)
10348 *end = cp_parser_range_for_member_function (range, id_end);
10349 else
10350 error ("range-based %<for%> expression of type %qT has a "
10351 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10353 else
10355 /* Use global functions with ADL. */
10356 vec<tree, va_gc> *vec;
10357 vec = make_tree_vector ();
10359 vec_safe_push (vec, range);
10361 member_begin = perform_koenig_lookup (id_begin, vec,
10362 /*include_std=*/true,
10363 tf_warning_or_error);
10364 *begin = finish_call_expr (member_begin, &vec, false, true,
10365 tf_warning_or_error);
10366 member_end = perform_koenig_lookup (id_end, vec,
10367 /*include_std=*/true,
10368 tf_warning_or_error);
10369 *end = finish_call_expr (member_end, &vec, false, true,
10370 tf_warning_or_error);
10372 release_tree_vector (vec);
10375 /* Last common checks. */
10376 if (*begin == error_mark_node || *end == error_mark_node)
10378 /* If one of the expressions is an error do no more checks. */
10379 *begin = *end = error_mark_node;
10380 return error_mark_node;
10382 else if (type_dependent_expression_p (*begin)
10383 || type_dependent_expression_p (*end))
10384 /* Can happen, when, eg, in a template context, Koenig lookup
10385 can't resolve begin/end (c++/58503). */
10386 return NULL_TREE;
10387 else
10389 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10390 /* The unqualified type of the __begin and __end temporaries should
10391 be the same, as required by the multiple auto declaration. */
10392 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10393 error ("inconsistent begin/end types in range-based %<for%> "
10394 "statement: %qT and %qT",
10395 TREE_TYPE (*begin), TREE_TYPE (*end));
10396 return iter_type;
10401 /* Helper function for cp_parser_perform_range_for_lookup.
10402 Builds a tree for RANGE.IDENTIFIER(). */
10404 static tree
10405 cp_parser_range_for_member_function (tree range, tree identifier)
10407 tree member, res;
10408 vec<tree, va_gc> *vec;
10410 member = finish_class_member_access_expr (range, identifier,
10411 false, tf_warning_or_error);
10412 if (member == error_mark_node)
10413 return error_mark_node;
10415 vec = make_tree_vector ();
10416 res = finish_call_expr (member, &vec,
10417 /*disallow_virtual=*/false,
10418 /*koenig_p=*/false,
10419 tf_warning_or_error);
10420 release_tree_vector (vec);
10421 return res;
10424 /* Parse an iteration-statement.
10426 iteration-statement:
10427 while ( condition ) statement
10428 do statement while ( expression ) ;
10429 for ( for-init-statement condition [opt] ; expression [opt] )
10430 statement
10432 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10434 static tree
10435 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10437 cp_token *token;
10438 enum rid keyword;
10439 tree statement;
10440 unsigned char in_statement;
10442 /* Peek at the next token. */
10443 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10444 if (!token)
10445 return error_mark_node;
10447 /* Remember whether or not we are already within an iteration
10448 statement. */
10449 in_statement = parser->in_statement;
10451 /* See what kind of keyword it is. */
10452 keyword = token->keyword;
10453 switch (keyword)
10455 case RID_WHILE:
10457 tree condition;
10459 /* Begin the while-statement. */
10460 statement = begin_while_stmt ();
10461 /* Look for the `('. */
10462 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10463 /* Parse the condition. */
10464 condition = cp_parser_condition (parser);
10465 finish_while_stmt_cond (condition, statement, ivdep);
10466 /* Look for the `)'. */
10467 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10468 /* Parse the dependent statement. */
10469 parser->in_statement = IN_ITERATION_STMT;
10470 cp_parser_already_scoped_statement (parser);
10471 parser->in_statement = in_statement;
10472 /* We're done with the while-statement. */
10473 finish_while_stmt (statement);
10475 break;
10477 case RID_DO:
10479 tree expression;
10481 /* Begin the do-statement. */
10482 statement = begin_do_stmt ();
10483 /* Parse the body of the do-statement. */
10484 parser->in_statement = IN_ITERATION_STMT;
10485 cp_parser_implicitly_scoped_statement (parser, NULL);
10486 parser->in_statement = in_statement;
10487 finish_do_body (statement);
10488 /* Look for the `while' keyword. */
10489 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10490 /* Look for the `('. */
10491 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10492 /* Parse the expression. */
10493 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10494 /* We're done with the do-statement. */
10495 finish_do_stmt (expression, statement, ivdep);
10496 /* Look for the `)'. */
10497 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10498 /* Look for the `;'. */
10499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10501 break;
10503 case RID_FOR:
10505 /* Look for the `('. */
10506 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10508 statement = cp_parser_for (parser, ivdep);
10510 /* Look for the `)'. */
10511 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10513 /* Parse the body of the for-statement. */
10514 parser->in_statement = IN_ITERATION_STMT;
10515 cp_parser_already_scoped_statement (parser);
10516 parser->in_statement = in_statement;
10518 /* We're done with the for-statement. */
10519 finish_for_stmt (statement);
10521 break;
10523 default:
10524 cp_parser_error (parser, "expected iteration-statement");
10525 statement = error_mark_node;
10526 break;
10529 return statement;
10532 /* Parse a for-init-statement or the declarator of a range-based-for.
10533 Returns true if a range-based-for declaration is seen.
10535 for-init-statement:
10536 expression-statement
10537 simple-declaration */
10539 static bool
10540 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10542 /* If the next token is a `;', then we have an empty
10543 expression-statement. Grammatically, this is also a
10544 simple-declaration, but an invalid one, because it does not
10545 declare anything. Therefore, if we did not handle this case
10546 specially, we would issue an error message about an invalid
10547 declaration. */
10548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10550 bool is_range_for = false;
10551 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10553 parser->colon_corrects_to_scope_p = false;
10555 /* We're going to speculatively look for a declaration, falling back
10556 to an expression, if necessary. */
10557 cp_parser_parse_tentatively (parser);
10558 /* Parse the declaration. */
10559 cp_parser_simple_declaration (parser,
10560 /*function_definition_allowed_p=*/false,
10561 decl);
10562 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10563 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10565 /* It is a range-for, consume the ':' */
10566 cp_lexer_consume_token (parser->lexer);
10567 is_range_for = true;
10568 if (cxx_dialect < cxx11)
10570 error_at (cp_lexer_peek_token (parser->lexer)->location,
10571 "range-based %<for%> loops are not allowed "
10572 "in C++98 mode");
10573 *decl = error_mark_node;
10576 else
10577 /* The ';' is not consumed yet because we told
10578 cp_parser_simple_declaration not to. */
10579 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10581 if (cp_parser_parse_definitely (parser))
10582 return is_range_for;
10583 /* If the tentative parse failed, then we shall need to look for an
10584 expression-statement. */
10586 /* If we are here, it is an expression-statement. */
10587 cp_parser_expression_statement (parser, NULL_TREE);
10588 return false;
10591 /* Parse a jump-statement.
10593 jump-statement:
10594 break ;
10595 continue ;
10596 return expression [opt] ;
10597 return braced-init-list ;
10598 goto identifier ;
10600 GNU extension:
10602 jump-statement:
10603 goto * expression ;
10605 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10607 static tree
10608 cp_parser_jump_statement (cp_parser* parser)
10610 tree statement = error_mark_node;
10611 cp_token *token;
10612 enum rid keyword;
10613 unsigned char in_statement;
10615 /* Peek at the next token. */
10616 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10617 if (!token)
10618 return error_mark_node;
10620 /* See what kind of keyword it is. */
10621 keyword = token->keyword;
10622 switch (keyword)
10624 case RID_BREAK:
10625 in_statement = parser->in_statement & ~IN_IF_STMT;
10626 switch (in_statement)
10628 case 0:
10629 error_at (token->location, "break statement not within loop or switch");
10630 break;
10631 default:
10632 gcc_assert ((in_statement & IN_SWITCH_STMT)
10633 || in_statement == IN_ITERATION_STMT);
10634 statement = finish_break_stmt ();
10635 break;
10636 case IN_OMP_BLOCK:
10637 error_at (token->location, "invalid exit from OpenMP structured block");
10638 break;
10639 case IN_OMP_FOR:
10640 error_at (token->location, "break statement used with OpenMP for loop");
10641 break;
10642 case IN_CILK_SIMD_FOR:
10643 error_at (token->location, "break statement used with Cilk Plus for loop");
10644 break;
10646 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10647 break;
10649 case RID_CONTINUE:
10650 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10652 case 0:
10653 error_at (token->location, "continue statement not within a loop");
10654 break;
10655 case IN_CILK_SIMD_FOR:
10656 error_at (token->location,
10657 "continue statement within %<#pragma simd%> loop body");
10658 /* Fall through. */
10659 case IN_ITERATION_STMT:
10660 case IN_OMP_FOR:
10661 statement = finish_continue_stmt ();
10662 break;
10663 case IN_OMP_BLOCK:
10664 error_at (token->location, "invalid exit from OpenMP structured block");
10665 break;
10666 default:
10667 gcc_unreachable ();
10669 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10670 break;
10672 case RID_RETURN:
10674 tree expr;
10675 bool expr_non_constant_p;
10677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10679 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10680 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10682 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10683 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10684 else
10685 /* If the next token is a `;', then there is no
10686 expression. */
10687 expr = NULL_TREE;
10688 /* Build the return-statement. */
10689 statement = finish_return_stmt (expr);
10690 /* Look for the final `;'. */
10691 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10693 break;
10695 case RID_GOTO:
10696 /* Create the goto-statement. */
10697 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10699 /* Issue a warning about this use of a GNU extension. */
10700 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10701 /* Consume the '*' token. */
10702 cp_lexer_consume_token (parser->lexer);
10703 /* Parse the dependent expression. */
10704 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10706 else
10707 finish_goto_stmt (cp_parser_identifier (parser));
10708 /* Look for the final `;'. */
10709 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10710 break;
10712 default:
10713 cp_parser_error (parser, "expected jump-statement");
10714 break;
10717 return statement;
10720 /* Parse a declaration-statement.
10722 declaration-statement:
10723 block-declaration */
10725 static void
10726 cp_parser_declaration_statement (cp_parser* parser)
10728 void *p;
10730 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10731 p = obstack_alloc (&declarator_obstack, 0);
10733 /* Parse the block-declaration. */
10734 cp_parser_block_declaration (parser, /*statement_p=*/true);
10736 /* Free any declarators allocated. */
10737 obstack_free (&declarator_obstack, p);
10740 /* Some dependent statements (like `if (cond) statement'), are
10741 implicitly in their own scope. In other words, if the statement is
10742 a single statement (as opposed to a compound-statement), it is
10743 none-the-less treated as if it were enclosed in braces. Any
10744 declarations appearing in the dependent statement are out of scope
10745 after control passes that point. This function parses a statement,
10746 but ensures that is in its own scope, even if it is not a
10747 compound-statement.
10749 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10750 is a (possibly labeled) if statement which is not enclosed in
10751 braces and has an else clause. This is used to implement
10752 -Wparentheses.
10754 Returns the new statement. */
10756 static tree
10757 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10759 tree statement;
10761 if (if_p != NULL)
10762 *if_p = false;
10764 /* Mark if () ; with a special NOP_EXPR. */
10765 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10767 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10768 cp_lexer_consume_token (parser->lexer);
10769 statement = add_stmt (build_empty_stmt (loc));
10771 /* if a compound is opened, we simply parse the statement directly. */
10772 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10773 statement = cp_parser_compound_statement (parser, NULL, false, false);
10774 /* If the token is not a `{', then we must take special action. */
10775 else
10777 /* Create a compound-statement. */
10778 statement = begin_compound_stmt (0);
10779 /* Parse the dependent-statement. */
10780 cp_parser_statement (parser, NULL_TREE, false, if_p);
10781 /* Finish the dummy compound-statement. */
10782 finish_compound_stmt (statement);
10785 /* Return the statement. */
10786 return statement;
10789 /* For some dependent statements (like `while (cond) statement'), we
10790 have already created a scope. Therefore, even if the dependent
10791 statement is a compound-statement, we do not want to create another
10792 scope. */
10794 static void
10795 cp_parser_already_scoped_statement (cp_parser* parser)
10797 /* If the token is a `{', then we must take special action. */
10798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10799 cp_parser_statement (parser, NULL_TREE, false, NULL);
10800 else
10802 /* Avoid calling cp_parser_compound_statement, so that we
10803 don't create a new scope. Do everything else by hand. */
10804 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10805 /* If the next keyword is `__label__' we have a label declaration. */
10806 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10807 cp_parser_label_declaration (parser);
10808 /* Parse an (optional) statement-seq. */
10809 cp_parser_statement_seq_opt (parser, NULL_TREE);
10810 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10814 /* Declarations [gram.dcl.dcl] */
10816 /* Parse an optional declaration-sequence.
10818 declaration-seq:
10819 declaration
10820 declaration-seq declaration */
10822 static void
10823 cp_parser_declaration_seq_opt (cp_parser* parser)
10825 while (true)
10827 cp_token *token;
10829 token = cp_lexer_peek_token (parser->lexer);
10831 if (token->type == CPP_CLOSE_BRACE
10832 || token->type == CPP_EOF
10833 || token->type == CPP_PRAGMA_EOL)
10834 break;
10836 if (token->type == CPP_SEMICOLON)
10838 /* A declaration consisting of a single semicolon is
10839 invalid. Allow it unless we're being pedantic. */
10840 cp_lexer_consume_token (parser->lexer);
10841 if (!in_system_header)
10842 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10843 continue;
10846 /* If we're entering or exiting a region that's implicitly
10847 extern "C", modify the lang context appropriately. */
10848 if (!parser->implicit_extern_c && token->implicit_extern_c)
10850 push_lang_context (lang_name_c);
10851 parser->implicit_extern_c = true;
10853 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10855 pop_lang_context ();
10856 parser->implicit_extern_c = false;
10859 if (token->type == CPP_PRAGMA)
10861 /* A top-level declaration can consist solely of a #pragma.
10862 A nested declaration cannot, so this is done here and not
10863 in cp_parser_declaration. (A #pragma at block scope is
10864 handled in cp_parser_statement.) */
10865 cp_parser_pragma (parser, pragma_external);
10866 continue;
10869 /* Parse the declaration itself. */
10870 cp_parser_declaration (parser);
10874 /* Parse a declaration.
10876 declaration:
10877 block-declaration
10878 function-definition
10879 template-declaration
10880 explicit-instantiation
10881 explicit-specialization
10882 linkage-specification
10883 namespace-definition
10885 GNU extension:
10887 declaration:
10888 __extension__ declaration */
10890 static void
10891 cp_parser_declaration (cp_parser* parser)
10893 cp_token token1;
10894 cp_token token2;
10895 int saved_pedantic;
10896 void *p;
10897 tree attributes = NULL_TREE;
10899 /* Check for the `__extension__' keyword. */
10900 if (cp_parser_extension_opt (parser, &saved_pedantic))
10902 /* Parse the qualified declaration. */
10903 cp_parser_declaration (parser);
10904 /* Restore the PEDANTIC flag. */
10905 pedantic = saved_pedantic;
10907 return;
10910 /* Try to figure out what kind of declaration is present. */
10911 token1 = *cp_lexer_peek_token (parser->lexer);
10913 if (token1.type != CPP_EOF)
10914 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10915 else
10917 token2.type = CPP_EOF;
10918 token2.keyword = RID_MAX;
10921 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10922 p = obstack_alloc (&declarator_obstack, 0);
10924 /* If the next token is `extern' and the following token is a string
10925 literal, then we have a linkage specification. */
10926 if (token1.keyword == RID_EXTERN
10927 && cp_parser_is_pure_string_literal (&token2))
10928 cp_parser_linkage_specification (parser);
10929 /* If the next token is `template', then we have either a template
10930 declaration, an explicit instantiation, or an explicit
10931 specialization. */
10932 else if (token1.keyword == RID_TEMPLATE)
10934 /* `template <>' indicates a template specialization. */
10935 if (token2.type == CPP_LESS
10936 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10937 cp_parser_explicit_specialization (parser);
10938 /* `template <' indicates a template declaration. */
10939 else if (token2.type == CPP_LESS)
10940 cp_parser_template_declaration (parser, /*member_p=*/false);
10941 /* Anything else must be an explicit instantiation. */
10942 else
10943 cp_parser_explicit_instantiation (parser);
10945 /* If the next token is `export', then we have a template
10946 declaration. */
10947 else if (token1.keyword == RID_EXPORT)
10948 cp_parser_template_declaration (parser, /*member_p=*/false);
10949 /* If the next token is `extern', 'static' or 'inline' and the one
10950 after that is `template', we have a GNU extended explicit
10951 instantiation directive. */
10952 else if (cp_parser_allow_gnu_extensions_p (parser)
10953 && (token1.keyword == RID_EXTERN
10954 || token1.keyword == RID_STATIC
10955 || token1.keyword == RID_INLINE)
10956 && token2.keyword == RID_TEMPLATE)
10957 cp_parser_explicit_instantiation (parser);
10958 /* If the next token is `namespace', check for a named or unnamed
10959 namespace definition. */
10960 else if (token1.keyword == RID_NAMESPACE
10961 && (/* A named namespace definition. */
10962 (token2.type == CPP_NAME
10963 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10964 != CPP_EQ))
10965 /* An unnamed namespace definition. */
10966 || token2.type == CPP_OPEN_BRACE
10967 || token2.keyword == RID_ATTRIBUTE))
10968 cp_parser_namespace_definition (parser);
10969 /* An inline (associated) namespace definition. */
10970 else if (token1.keyword == RID_INLINE
10971 && token2.keyword == RID_NAMESPACE)
10972 cp_parser_namespace_definition (parser);
10973 /* Objective-C++ declaration/definition. */
10974 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10975 cp_parser_objc_declaration (parser, NULL_TREE);
10976 else if (c_dialect_objc ()
10977 && token1.keyword == RID_ATTRIBUTE
10978 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10979 cp_parser_objc_declaration (parser, attributes);
10980 /* We must have either a block declaration or a function
10981 definition. */
10982 else
10983 /* Try to parse a block-declaration, or a function-definition. */
10984 cp_parser_block_declaration (parser, /*statement_p=*/false);
10986 /* Free any declarators allocated. */
10987 obstack_free (&declarator_obstack, p);
10990 /* Parse a block-declaration.
10992 block-declaration:
10993 simple-declaration
10994 asm-definition
10995 namespace-alias-definition
10996 using-declaration
10997 using-directive
10999 GNU Extension:
11001 block-declaration:
11002 __extension__ block-declaration
11004 C++0x Extension:
11006 block-declaration:
11007 static_assert-declaration
11009 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11010 part of a declaration-statement. */
11012 static void
11013 cp_parser_block_declaration (cp_parser *parser,
11014 bool statement_p)
11016 cp_token *token1;
11017 int saved_pedantic;
11019 /* Check for the `__extension__' keyword. */
11020 if (cp_parser_extension_opt (parser, &saved_pedantic))
11022 /* Parse the qualified declaration. */
11023 cp_parser_block_declaration (parser, statement_p);
11024 /* Restore the PEDANTIC flag. */
11025 pedantic = saved_pedantic;
11027 return;
11030 /* Peek at the next token to figure out which kind of declaration is
11031 present. */
11032 token1 = cp_lexer_peek_token (parser->lexer);
11034 /* If the next keyword is `asm', we have an asm-definition. */
11035 if (token1->keyword == RID_ASM)
11037 if (statement_p)
11038 cp_parser_commit_to_tentative_parse (parser);
11039 cp_parser_asm_definition (parser);
11041 /* If the next keyword is `namespace', we have a
11042 namespace-alias-definition. */
11043 else if (token1->keyword == RID_NAMESPACE)
11044 cp_parser_namespace_alias_definition (parser);
11045 /* If the next keyword is `using', we have a
11046 using-declaration, a using-directive, or an alias-declaration. */
11047 else if (token1->keyword == RID_USING)
11049 cp_token *token2;
11051 if (statement_p)
11052 cp_parser_commit_to_tentative_parse (parser);
11053 /* If the token after `using' is `namespace', then we have a
11054 using-directive. */
11055 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11056 if (token2->keyword == RID_NAMESPACE)
11057 cp_parser_using_directive (parser);
11058 /* If the second token after 'using' is '=', then we have an
11059 alias-declaration. */
11060 else if (cxx_dialect >= cxx11
11061 && token2->type == CPP_NAME
11062 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11063 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11064 cp_parser_alias_declaration (parser);
11065 /* Otherwise, it's a using-declaration. */
11066 else
11067 cp_parser_using_declaration (parser,
11068 /*access_declaration_p=*/false);
11070 /* If the next keyword is `__label__' we have a misplaced label
11071 declaration. */
11072 else if (token1->keyword == RID_LABEL)
11074 cp_lexer_consume_token (parser->lexer);
11075 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11076 cp_parser_skip_to_end_of_statement (parser);
11077 /* If the next token is now a `;', consume it. */
11078 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11079 cp_lexer_consume_token (parser->lexer);
11081 /* If the next token is `static_assert' we have a static assertion. */
11082 else if (token1->keyword == RID_STATIC_ASSERT)
11083 cp_parser_static_assert (parser, /*member_p=*/false);
11084 /* Anything else must be a simple-declaration. */
11085 else
11086 cp_parser_simple_declaration (parser, !statement_p,
11087 /*maybe_range_for_decl*/NULL);
11090 /* Parse a simple-declaration.
11092 simple-declaration:
11093 decl-specifier-seq [opt] init-declarator-list [opt] ;
11095 init-declarator-list:
11096 init-declarator
11097 init-declarator-list , init-declarator
11099 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11100 function-definition as a simple-declaration.
11102 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11103 parsed declaration if it is an uninitialized single declarator not followed
11104 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11105 if present, will not be consumed. */
11107 static void
11108 cp_parser_simple_declaration (cp_parser* parser,
11109 bool function_definition_allowed_p,
11110 tree *maybe_range_for_decl)
11112 cp_decl_specifier_seq decl_specifiers;
11113 int declares_class_or_enum;
11114 bool saw_declarator;
11116 if (maybe_range_for_decl)
11117 *maybe_range_for_decl = NULL_TREE;
11119 /* Defer access checks until we know what is being declared; the
11120 checks for names appearing in the decl-specifier-seq should be
11121 done as if we were in the scope of the thing being declared. */
11122 push_deferring_access_checks (dk_deferred);
11124 /* Parse the decl-specifier-seq. We have to keep track of whether
11125 or not the decl-specifier-seq declares a named class or
11126 enumeration type, since that is the only case in which the
11127 init-declarator-list is allowed to be empty.
11129 [dcl.dcl]
11131 In a simple-declaration, the optional init-declarator-list can be
11132 omitted only when declaring a class or enumeration, that is when
11133 the decl-specifier-seq contains either a class-specifier, an
11134 elaborated-type-specifier, or an enum-specifier. */
11135 cp_parser_decl_specifier_seq (parser,
11136 CP_PARSER_FLAGS_OPTIONAL,
11137 &decl_specifiers,
11138 &declares_class_or_enum);
11139 /* We no longer need to defer access checks. */
11140 stop_deferring_access_checks ();
11142 /* In a block scope, a valid declaration must always have a
11143 decl-specifier-seq. By not trying to parse declarators, we can
11144 resolve the declaration/expression ambiguity more quickly. */
11145 if (!function_definition_allowed_p
11146 && !decl_specifiers.any_specifiers_p)
11148 cp_parser_error (parser, "expected declaration");
11149 goto done;
11152 /* If the next two tokens are both identifiers, the code is
11153 erroneous. The usual cause of this situation is code like:
11155 T t;
11157 where "T" should name a type -- but does not. */
11158 if (!decl_specifiers.any_type_specifiers_p
11159 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11161 /* If parsing tentatively, we should commit; we really are
11162 looking at a declaration. */
11163 cp_parser_commit_to_tentative_parse (parser);
11164 /* Give up. */
11165 goto done;
11168 /* If we have seen at least one decl-specifier, and the next token
11169 is not a parenthesis, then we must be looking at a declaration.
11170 (After "int (" we might be looking at a functional cast.) */
11171 if (decl_specifiers.any_specifiers_p
11172 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11173 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11174 && !cp_parser_error_occurred (parser))
11175 cp_parser_commit_to_tentative_parse (parser);
11177 /* Keep going until we hit the `;' at the end of the simple
11178 declaration. */
11179 saw_declarator = false;
11180 while (cp_lexer_next_token_is_not (parser->lexer,
11181 CPP_SEMICOLON))
11183 cp_token *token;
11184 bool function_definition_p;
11185 tree decl;
11187 if (saw_declarator)
11189 /* If we are processing next declarator, coma is expected */
11190 token = cp_lexer_peek_token (parser->lexer);
11191 gcc_assert (token->type == CPP_COMMA);
11192 cp_lexer_consume_token (parser->lexer);
11193 if (maybe_range_for_decl)
11194 *maybe_range_for_decl = error_mark_node;
11196 else
11197 saw_declarator = true;
11199 /* Parse the init-declarator. */
11200 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11201 /*checks=*/NULL,
11202 function_definition_allowed_p,
11203 /*member_p=*/false,
11204 declares_class_or_enum,
11205 &function_definition_p,
11206 maybe_range_for_decl);
11207 /* If an error occurred while parsing tentatively, exit quickly.
11208 (That usually happens when in the body of a function; each
11209 statement is treated as a declaration-statement until proven
11210 otherwise.) */
11211 if (cp_parser_error_occurred (parser))
11212 goto done;
11213 /* Handle function definitions specially. */
11214 if (function_definition_p)
11216 /* If the next token is a `,', then we are probably
11217 processing something like:
11219 void f() {}, *p;
11221 which is erroneous. */
11222 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11224 cp_token *token = cp_lexer_peek_token (parser->lexer);
11225 error_at (token->location,
11226 "mixing"
11227 " declarations and function-definitions is forbidden");
11229 /* Otherwise, we're done with the list of declarators. */
11230 else
11232 pop_deferring_access_checks ();
11233 return;
11236 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11237 *maybe_range_for_decl = decl;
11238 /* The next token should be either a `,' or a `;'. */
11239 token = cp_lexer_peek_token (parser->lexer);
11240 /* If it's a `,', there are more declarators to come. */
11241 if (token->type == CPP_COMMA)
11242 /* will be consumed next time around */;
11243 /* If it's a `;', we are done. */
11244 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11245 break;
11246 /* Anything else is an error. */
11247 else
11249 /* If we have already issued an error message we don't need
11250 to issue another one. */
11251 if (decl != error_mark_node
11252 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11253 cp_parser_error (parser, "expected %<,%> or %<;%>");
11254 /* Skip tokens until we reach the end of the statement. */
11255 cp_parser_skip_to_end_of_statement (parser);
11256 /* If the next token is now a `;', consume it. */
11257 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11258 cp_lexer_consume_token (parser->lexer);
11259 goto done;
11261 /* After the first time around, a function-definition is not
11262 allowed -- even if it was OK at first. For example:
11264 int i, f() {}
11266 is not valid. */
11267 function_definition_allowed_p = false;
11270 /* Issue an error message if no declarators are present, and the
11271 decl-specifier-seq does not itself declare a class or
11272 enumeration: [dcl.dcl]/3. */
11273 if (!saw_declarator)
11275 if (cp_parser_declares_only_class_p (parser))
11277 if (!declares_class_or_enum
11278 && decl_specifiers.type
11279 && OVERLOAD_TYPE_P (decl_specifiers.type))
11280 /* Ensure an error is issued anyway when finish_decltype_type,
11281 called via cp_parser_decl_specifier_seq, returns a class or
11282 an enumeration (c++/51786). */
11283 decl_specifiers.type = NULL_TREE;
11284 shadow_tag (&decl_specifiers);
11286 /* Perform any deferred access checks. */
11287 perform_deferred_access_checks (tf_warning_or_error);
11290 /* Consume the `;'. */
11291 if (!maybe_range_for_decl)
11292 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11294 done:
11295 pop_deferring_access_checks ();
11298 /* Parse a decl-specifier-seq.
11300 decl-specifier-seq:
11301 decl-specifier-seq [opt] decl-specifier
11302 decl-specifier attribute-specifier-seq [opt] (C++11)
11304 decl-specifier:
11305 storage-class-specifier
11306 type-specifier
11307 function-specifier
11308 friend
11309 typedef
11311 GNU Extension:
11313 decl-specifier:
11314 attributes
11316 Concepts Extension:
11318 decl-specifier:
11319 concept
11322 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11324 The parser flags FLAGS is used to control type-specifier parsing.
11326 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11327 flags:
11329 1: one of the decl-specifiers is an elaborated-type-specifier
11330 (i.e., a type declaration)
11331 2: one of the decl-specifiers is an enum-specifier or a
11332 class-specifier (i.e., a type definition)
11336 static void
11337 cp_parser_decl_specifier_seq (cp_parser* parser,
11338 cp_parser_flags flags,
11339 cp_decl_specifier_seq *decl_specs,
11340 int* declares_class_or_enum)
11342 bool constructor_possible_p = !parser->in_declarator_p;
11343 bool found_decl_spec = false;
11344 cp_token *start_token = NULL;
11345 cp_decl_spec ds;
11347 /* Clear DECL_SPECS. */
11348 clear_decl_specs (decl_specs);
11350 /* Assume no class or enumeration type is declared. */
11351 *declares_class_or_enum = 0;
11353 /* Keep reading specifiers until there are no more to read. */
11354 while (true)
11356 bool constructor_p;
11357 cp_token *token;
11358 ds = ds_last;
11360 /* Peek at the next token. */
11361 token = cp_lexer_peek_token (parser->lexer);
11363 /* Save the first token of the decl spec list for error
11364 reporting. */
11365 if (!start_token)
11366 start_token = token;
11367 /* Handle attributes. */
11368 if (cp_next_tokens_can_be_attribute_p (parser))
11370 /* Parse the attributes. */
11371 tree attrs = cp_parser_attributes_opt (parser);
11373 /* In a sequence of declaration specifiers, c++11 attributes
11374 appertain to the type that precede them. In that case
11375 [dcl.spec]/1 says:
11377 The attribute-specifier-seq affects the type only for
11378 the declaration it appears in, not other declarations
11379 involving the same type.
11381 But for now let's force the user to position the
11382 attribute either at the beginning of the declaration or
11383 after the declarator-id, which would clearly mean that it
11384 applies to the declarator. */
11385 if (cxx11_attribute_p (attrs))
11387 if (!found_decl_spec)
11388 /* The c++11 attribute is at the beginning of the
11389 declaration. It appertains to the entity being
11390 declared. */;
11391 else
11393 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11395 /* This is an attribute following a
11396 class-specifier. */
11397 if (decl_specs->type_definition_p)
11398 warn_misplaced_attr_for_class_type (token->location,
11399 decl_specs->type);
11400 attrs = NULL_TREE;
11402 else
11404 decl_specs->std_attributes
11405 = chainon (decl_specs->std_attributes,
11406 attrs);
11407 if (decl_specs->locations[ds_std_attribute] == 0)
11408 decl_specs->locations[ds_std_attribute] = token->location;
11410 continue;
11414 decl_specs->attributes
11415 = chainon (decl_specs->attributes,
11416 attrs);
11417 if (decl_specs->locations[ds_attribute] == 0)
11418 decl_specs->locations[ds_attribute] = token->location;
11419 continue;
11421 /* Assume we will find a decl-specifier keyword. */
11422 found_decl_spec = true;
11423 /* If the next token is an appropriate keyword, we can simply
11424 add it to the list. */
11425 switch (token->keyword)
11427 /* decl-specifier:
11428 friend
11429 constexpr */
11430 case RID_FRIEND:
11431 if (!at_class_scope_p ())
11433 error_at (token->location, "%<friend%> used outside of class");
11434 cp_lexer_purge_token (parser->lexer);
11436 else
11438 ds = ds_friend;
11439 /* Consume the token. */
11440 cp_lexer_consume_token (parser->lexer);
11442 break;
11444 case RID_CONSTEXPR:
11445 ds = ds_constexpr;
11446 cp_lexer_consume_token (parser->lexer);
11447 break;
11449 case RID_CONCEPT:
11450 ds = ds_concept;
11451 cp_lexer_consume_token (parser->lexer);
11452 break;
11454 /* function-specifier:
11455 inline
11456 virtual
11457 explicit */
11458 case RID_INLINE:
11459 case RID_VIRTUAL:
11460 case RID_EXPLICIT:
11461 cp_parser_function_specifier_opt (parser, decl_specs);
11462 break;
11464 /* decl-specifier:
11465 typedef */
11466 case RID_TYPEDEF:
11467 ds = ds_typedef;
11468 /* Consume the token. */
11469 cp_lexer_consume_token (parser->lexer);
11470 /* A constructor declarator cannot appear in a typedef. */
11471 constructor_possible_p = false;
11472 /* The "typedef" keyword can only occur in a declaration; we
11473 may as well commit at this point. */
11474 cp_parser_commit_to_tentative_parse (parser);
11476 if (decl_specs->storage_class != sc_none)
11477 decl_specs->conflicting_specifiers_p = true;
11478 break;
11480 /* storage-class-specifier:
11481 auto
11482 register
11483 static
11484 extern
11485 mutable
11487 GNU Extension:
11488 thread */
11489 case RID_AUTO:
11490 if (cxx_dialect == cxx98)
11492 /* Consume the token. */
11493 cp_lexer_consume_token (parser->lexer);
11495 /* Complain about `auto' as a storage specifier, if
11496 we're complaining about C++0x compatibility. */
11497 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11498 " changes meaning in C++11; please remove it");
11500 /* Set the storage class anyway. */
11501 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11502 token);
11504 else
11505 /* C++0x auto type-specifier. */
11506 found_decl_spec = false;
11507 break;
11509 case RID_REGISTER:
11510 case RID_STATIC:
11511 case RID_EXTERN:
11512 case RID_MUTABLE:
11513 /* Consume the token. */
11514 cp_lexer_consume_token (parser->lexer);
11515 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11516 token);
11517 break;
11518 case RID_THREAD:
11519 /* Consume the token. */
11520 ds = ds_thread;
11521 cp_lexer_consume_token (parser->lexer);
11522 break;
11524 default:
11525 /* We did not yet find a decl-specifier yet. */
11526 found_decl_spec = false;
11527 break;
11530 if (found_decl_spec
11531 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11532 && token->keyword != RID_CONSTEXPR)
11533 error ("decl-specifier invalid in condition");
11535 if (ds != ds_last)
11536 set_and_check_decl_spec_loc (decl_specs, ds, token);
11538 /* Constructors are a special case. The `S' in `S()' is not a
11539 decl-specifier; it is the beginning of the declarator. */
11540 constructor_p
11541 = (!found_decl_spec
11542 && constructor_possible_p
11543 && (cp_parser_constructor_declarator_p
11544 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11546 /* If we don't have a DECL_SPEC yet, then we must be looking at
11547 a type-specifier. */
11548 if (!found_decl_spec && !constructor_p)
11550 int decl_spec_declares_class_or_enum;
11551 bool is_cv_qualifier;
11552 tree type_spec;
11554 type_spec
11555 = cp_parser_type_specifier (parser, flags,
11556 decl_specs,
11557 /*is_declaration=*/true,
11558 &decl_spec_declares_class_or_enum,
11559 &is_cv_qualifier);
11560 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11562 /* If this type-specifier referenced a user-defined type
11563 (a typedef, class-name, etc.), then we can't allow any
11564 more such type-specifiers henceforth.
11566 [dcl.spec]
11568 The longest sequence of decl-specifiers that could
11569 possibly be a type name is taken as the
11570 decl-specifier-seq of a declaration. The sequence shall
11571 be self-consistent as described below.
11573 [dcl.type]
11575 As a general rule, at most one type-specifier is allowed
11576 in the complete decl-specifier-seq of a declaration. The
11577 only exceptions are the following:
11579 -- const or volatile can be combined with any other
11580 type-specifier.
11582 -- signed or unsigned can be combined with char, long,
11583 short, or int.
11585 -- ..
11587 Example:
11589 typedef char* Pc;
11590 void g (const int Pc);
11592 Here, Pc is *not* part of the decl-specifier seq; it's
11593 the declarator. Therefore, once we see a type-specifier
11594 (other than a cv-qualifier), we forbid any additional
11595 user-defined types. We *do* still allow things like `int
11596 int' to be considered a decl-specifier-seq, and issue the
11597 error message later. */
11598 if (type_spec && !is_cv_qualifier)
11599 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11600 /* A constructor declarator cannot follow a type-specifier. */
11601 if (type_spec)
11603 constructor_possible_p = false;
11604 found_decl_spec = true;
11605 if (!is_cv_qualifier)
11606 decl_specs->any_type_specifiers_p = true;
11610 /* If we still do not have a DECL_SPEC, then there are no more
11611 decl-specifiers. */
11612 if (!found_decl_spec)
11613 break;
11615 decl_specs->any_specifiers_p = true;
11616 /* After we see one decl-specifier, further decl-specifiers are
11617 always optional. */
11618 flags |= CP_PARSER_FLAGS_OPTIONAL;
11621 /* Don't allow a friend specifier with a class definition. */
11622 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11623 && (*declares_class_or_enum & 2))
11624 error_at (decl_specs->locations[ds_friend],
11625 "class definition may not be declared a friend");
11628 /* Parse an (optional) storage-class-specifier.
11630 storage-class-specifier:
11631 auto
11632 register
11633 static
11634 extern
11635 mutable
11637 GNU Extension:
11639 storage-class-specifier:
11640 thread
11642 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11644 static tree
11645 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11647 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11649 case RID_AUTO:
11650 if (cxx_dialect != cxx98)
11651 return NULL_TREE;
11652 /* Fall through for C++98. */
11654 case RID_REGISTER:
11655 case RID_STATIC:
11656 case RID_EXTERN:
11657 case RID_MUTABLE:
11658 case RID_THREAD:
11659 /* Consume the token. */
11660 return cp_lexer_consume_token (parser->lexer)->u.value;
11662 default:
11663 return NULL_TREE;
11667 /* Parse an (optional) function-specifier.
11669 function-specifier:
11670 inline
11671 virtual
11672 explicit
11674 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11675 Updates DECL_SPECS, if it is non-NULL. */
11677 static tree
11678 cp_parser_function_specifier_opt (cp_parser* parser,
11679 cp_decl_specifier_seq *decl_specs)
11681 cp_token *token = cp_lexer_peek_token (parser->lexer);
11682 switch (token->keyword)
11684 case RID_INLINE:
11685 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11686 break;
11688 case RID_VIRTUAL:
11689 /* 14.5.2.3 [temp.mem]
11691 A member function template shall not be virtual. */
11692 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11693 error_at (token->location, "templates may not be %<virtual%>");
11694 else
11695 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11696 break;
11698 case RID_EXPLICIT:
11699 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11700 break;
11702 default:
11703 return NULL_TREE;
11706 /* Consume the token. */
11707 return cp_lexer_consume_token (parser->lexer)->u.value;
11710 /* Parse a linkage-specification.
11712 linkage-specification:
11713 extern string-literal { declaration-seq [opt] }
11714 extern string-literal declaration */
11716 static void
11717 cp_parser_linkage_specification (cp_parser* parser)
11719 tree linkage;
11721 /* Look for the `extern' keyword. */
11722 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11724 /* Look for the string-literal. */
11725 linkage = cp_parser_string_literal (parser, false, false);
11727 /* Transform the literal into an identifier. If the literal is a
11728 wide-character string, or contains embedded NULs, then we can't
11729 handle it as the user wants. */
11730 if (strlen (TREE_STRING_POINTER (linkage))
11731 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11733 cp_parser_error (parser, "invalid linkage-specification");
11734 /* Assume C++ linkage. */
11735 linkage = lang_name_cplusplus;
11737 else
11738 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11740 /* We're now using the new linkage. */
11741 push_lang_context (linkage);
11743 /* If the next token is a `{', then we're using the first
11744 production. */
11745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11747 cp_ensure_no_omp_declare_simd (parser);
11749 /* Consume the `{' token. */
11750 cp_lexer_consume_token (parser->lexer);
11751 /* Parse the declarations. */
11752 cp_parser_declaration_seq_opt (parser);
11753 /* Look for the closing `}'. */
11754 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11756 /* Otherwise, there's just one declaration. */
11757 else
11759 bool saved_in_unbraced_linkage_specification_p;
11761 saved_in_unbraced_linkage_specification_p
11762 = parser->in_unbraced_linkage_specification_p;
11763 parser->in_unbraced_linkage_specification_p = true;
11764 cp_parser_declaration (parser);
11765 parser->in_unbraced_linkage_specification_p
11766 = saved_in_unbraced_linkage_specification_p;
11769 /* We're done with the linkage-specification. */
11770 pop_lang_context ();
11773 /* Parse a static_assert-declaration.
11775 static_assert-declaration:
11776 static_assert ( constant-expression , string-literal ) ;
11778 If MEMBER_P, this static_assert is a class member. */
11780 static void
11781 cp_parser_static_assert(cp_parser *parser, bool member_p)
11783 tree condition;
11784 tree message;
11785 cp_token *token;
11786 location_t saved_loc;
11787 bool dummy;
11789 /* Peek at the `static_assert' token so we can keep track of exactly
11790 where the static assertion started. */
11791 token = cp_lexer_peek_token (parser->lexer);
11792 saved_loc = token->location;
11794 /* Look for the `static_assert' keyword. */
11795 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11796 RT_STATIC_ASSERT))
11797 return;
11799 /* We know we are in a static assertion; commit to any tentative
11800 parse. */
11801 if (cp_parser_parsing_tentatively (parser))
11802 cp_parser_commit_to_tentative_parse (parser);
11804 /* Parse the `(' starting the static assertion condition. */
11805 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11807 /* Parse the constant-expression. Allow a non-constant expression
11808 here in order to give better diagnostics in finish_static_assert. */
11809 condition =
11810 cp_parser_constant_expression (parser,
11811 /*allow_non_constant_p=*/true,
11812 /*non_constant_p=*/&dummy);
11814 /* Parse the separating `,'. */
11815 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11817 /* Parse the string-literal message. */
11818 message = cp_parser_string_literal (parser,
11819 /*translate=*/false,
11820 /*wide_ok=*/true);
11822 /* A `)' completes the static assertion. */
11823 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11824 cp_parser_skip_to_closing_parenthesis (parser,
11825 /*recovering=*/true,
11826 /*or_comma=*/false,
11827 /*consume_paren=*/true);
11829 /* A semicolon terminates the declaration. */
11830 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11832 /* Complete the static assertion, which may mean either processing
11833 the static assert now or saving it for template instantiation. */
11834 finish_static_assert (condition, message, saved_loc, member_p);
11837 /* Parse the expression in decltype ( expression ). */
11839 static tree
11840 cp_parser_decltype_expr (cp_parser *parser,
11841 bool &id_expression_or_member_access_p)
11843 cp_token *id_expr_start_token;
11844 tree expr;
11846 /* First, try parsing an id-expression. */
11847 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11848 cp_parser_parse_tentatively (parser);
11849 expr = cp_parser_id_expression (parser,
11850 /*template_keyword_p=*/false,
11851 /*check_dependency_p=*/true,
11852 /*template_p=*/NULL,
11853 /*declarator_p=*/false,
11854 /*optional_p=*/false);
11856 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11858 bool non_integral_constant_expression_p = false;
11859 tree id_expression = expr;
11860 cp_id_kind idk;
11861 const char *error_msg;
11863 if (identifier_p (expr))
11864 /* Lookup the name we got back from the id-expression. */
11865 expr = cp_parser_lookup_name_simple (parser, expr,
11866 id_expr_start_token->location);
11868 if (expr
11869 && expr != error_mark_node
11870 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11871 && TREE_CODE (expr) != TYPE_DECL
11872 && (TREE_CODE (expr) != BIT_NOT_EXPR
11873 || !TYPE_P (TREE_OPERAND (expr, 0)))
11874 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11876 /* Complete lookup of the id-expression. */
11877 expr = (finish_id_expression
11878 (id_expression, expr, parser->scope, &idk,
11879 /*integral_constant_expression_p=*/false,
11880 /*allow_non_integral_constant_expression_p=*/true,
11881 &non_integral_constant_expression_p,
11882 /*template_p=*/false,
11883 /*done=*/true,
11884 /*address_p=*/false,
11885 /*template_arg_p=*/false,
11886 &error_msg,
11887 id_expr_start_token->location));
11889 if (expr == error_mark_node)
11890 /* We found an id-expression, but it was something that we
11891 should not have found. This is an error, not something
11892 we can recover from, so note that we found an
11893 id-expression and we'll recover as gracefully as
11894 possible. */
11895 id_expression_or_member_access_p = true;
11898 if (expr
11899 && expr != error_mark_node
11900 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11901 /* We have an id-expression. */
11902 id_expression_or_member_access_p = true;
11905 if (!id_expression_or_member_access_p)
11907 /* Abort the id-expression parse. */
11908 cp_parser_abort_tentative_parse (parser);
11910 /* Parsing tentatively, again. */
11911 cp_parser_parse_tentatively (parser);
11913 /* Parse a class member access. */
11914 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11915 /*cast_p=*/false, /*decltype*/true,
11916 /*member_access_only_p=*/true, NULL);
11918 if (expr
11919 && expr != error_mark_node
11920 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11921 /* We have an id-expression. */
11922 id_expression_or_member_access_p = true;
11925 if (id_expression_or_member_access_p)
11926 /* We have parsed the complete id-expression or member access. */
11927 cp_parser_parse_definitely (parser);
11928 else
11930 /* Abort our attempt to parse an id-expression or member access
11931 expression. */
11932 cp_parser_abort_tentative_parse (parser);
11934 /* Parse a full expression. */
11935 expr = cp_parser_expression (parser, /*cast_p=*/false,
11936 /*decltype*/true, NULL);
11939 return expr;
11942 /* Parse a `decltype' type. Returns the type.
11944 simple-type-specifier:
11945 decltype ( expression )
11946 C++14 proposal:
11947 decltype ( auto ) */
11949 static tree
11950 cp_parser_decltype (cp_parser *parser)
11952 tree expr;
11953 bool id_expression_or_member_access_p = false;
11954 const char *saved_message;
11955 bool saved_integral_constant_expression_p;
11956 bool saved_non_integral_constant_expression_p;
11957 bool saved_greater_than_is_operator_p;
11958 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11960 if (start_token->type == CPP_DECLTYPE)
11962 /* Already parsed. */
11963 cp_lexer_consume_token (parser->lexer);
11964 return start_token->u.value;
11967 /* Look for the `decltype' token. */
11968 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11969 return error_mark_node;
11971 /* Parse the opening `('. */
11972 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11973 return error_mark_node;
11975 /* decltype (auto) */
11976 if (cxx_dialect >= cxx1y
11977 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11979 cp_lexer_consume_token (parser->lexer);
11980 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11981 return error_mark_node;
11982 expr = make_decltype_auto ();
11983 AUTO_IS_DECLTYPE (expr) = true;
11984 goto rewrite;
11987 /* Types cannot be defined in a `decltype' expression. Save away the
11988 old message. */
11989 saved_message = parser->type_definition_forbidden_message;
11991 /* And create the new one. */
11992 parser->type_definition_forbidden_message
11993 = G_("types may not be defined in %<decltype%> expressions");
11995 /* The restrictions on constant-expressions do not apply inside
11996 decltype expressions. */
11997 saved_integral_constant_expression_p
11998 = parser->integral_constant_expression_p;
11999 saved_non_integral_constant_expression_p
12000 = parser->non_integral_constant_expression_p;
12001 parser->integral_constant_expression_p = false;
12003 /* Within a parenthesized expression, a `>' token is always
12004 the greater-than operator. */
12005 saved_greater_than_is_operator_p
12006 = parser->greater_than_is_operator_p;
12007 parser->greater_than_is_operator_p = true;
12009 /* Do not actually evaluate the expression. */
12010 ++cp_unevaluated_operand;
12012 /* Do not warn about problems with the expression. */
12013 ++c_inhibit_evaluation_warnings;
12015 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12017 /* Go back to evaluating expressions. */
12018 --cp_unevaluated_operand;
12019 --c_inhibit_evaluation_warnings;
12021 /* The `>' token might be the end of a template-id or
12022 template-parameter-list now. */
12023 parser->greater_than_is_operator_p
12024 = saved_greater_than_is_operator_p;
12026 /* Restore the old message and the integral constant expression
12027 flags. */
12028 parser->type_definition_forbidden_message = saved_message;
12029 parser->integral_constant_expression_p
12030 = saved_integral_constant_expression_p;
12031 parser->non_integral_constant_expression_p
12032 = saved_non_integral_constant_expression_p;
12034 /* Parse to the closing `)'. */
12035 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12037 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12038 /*consume_paren=*/true);
12039 return error_mark_node;
12042 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12043 tf_warning_or_error);
12045 rewrite:
12046 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12047 it again. */
12048 start_token->type = CPP_DECLTYPE;
12049 start_token->u.value = expr;
12050 start_token->keyword = RID_MAX;
12051 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12053 return expr;
12056 /* Special member functions [gram.special] */
12058 /* Parse a conversion-function-id.
12060 conversion-function-id:
12061 operator conversion-type-id
12063 Returns an IDENTIFIER_NODE representing the operator. */
12065 static tree
12066 cp_parser_conversion_function_id (cp_parser* parser)
12068 tree type;
12069 tree saved_scope;
12070 tree saved_qualifying_scope;
12071 tree saved_object_scope;
12072 tree pushed_scope = NULL_TREE;
12074 /* Look for the `operator' token. */
12075 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12076 return error_mark_node;
12077 /* When we parse the conversion-type-id, the current scope will be
12078 reset. However, we need that information in able to look up the
12079 conversion function later, so we save it here. */
12080 saved_scope = parser->scope;
12081 saved_qualifying_scope = parser->qualifying_scope;
12082 saved_object_scope = parser->object_scope;
12083 /* We must enter the scope of the class so that the names of
12084 entities declared within the class are available in the
12085 conversion-type-id. For example, consider:
12087 struct S {
12088 typedef int I;
12089 operator I();
12092 S::operator I() { ... }
12094 In order to see that `I' is a type-name in the definition, we
12095 must be in the scope of `S'. */
12096 if (saved_scope)
12097 pushed_scope = push_scope (saved_scope);
12098 /* Parse the conversion-type-id. */
12099 type = cp_parser_conversion_type_id (parser);
12100 /* Leave the scope of the class, if any. */
12101 if (pushed_scope)
12102 pop_scope (pushed_scope);
12103 /* Restore the saved scope. */
12104 parser->scope = saved_scope;
12105 parser->qualifying_scope = saved_qualifying_scope;
12106 parser->object_scope = saved_object_scope;
12107 /* If the TYPE is invalid, indicate failure. */
12108 if (type == error_mark_node)
12109 return error_mark_node;
12110 return mangle_conv_op_name_for_type (type);
12113 /* Parse a conversion-type-id:
12115 conversion-type-id:
12116 type-specifier-seq conversion-declarator [opt]
12118 Returns the TYPE specified. */
12120 static tree
12121 cp_parser_conversion_type_id (cp_parser* parser)
12123 tree attributes;
12124 cp_decl_specifier_seq type_specifiers;
12125 cp_declarator *declarator;
12126 tree type_specified;
12127 const char *saved_message;
12129 /* Parse the attributes. */
12130 attributes = cp_parser_attributes_opt (parser);
12132 saved_message = parser->type_definition_forbidden_message;
12133 parser->type_definition_forbidden_message
12134 = G_("types may not be defined in a conversion-type-id");
12136 /* Parse the type-specifiers. */
12137 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12138 /*is_trailing_return=*/false,
12139 &type_specifiers);
12141 parser->type_definition_forbidden_message = saved_message;
12143 /* If that didn't work, stop. */
12144 if (type_specifiers.type == error_mark_node)
12145 return error_mark_node;
12146 /* Parse the conversion-declarator. */
12147 declarator = cp_parser_conversion_declarator_opt (parser);
12149 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12150 /*initialized=*/0, &attributes);
12151 if (attributes)
12152 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12154 /* Don't give this error when parsing tentatively. This happens to
12155 work because we always parse this definitively once. */
12156 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12157 && type_uses_auto (type_specified))
12159 if (cxx_dialect < cxx1y)
12161 error ("invalid use of %<auto%> in conversion operator");
12162 return error_mark_node;
12164 else if (template_parm_scope_p ())
12165 warning (0, "use of %<auto%> in member template "
12166 "conversion operator can never be deduced");
12169 return type_specified;
12172 /* Parse an (optional) conversion-declarator.
12174 conversion-declarator:
12175 ptr-operator conversion-declarator [opt]
12179 static cp_declarator *
12180 cp_parser_conversion_declarator_opt (cp_parser* parser)
12182 enum tree_code code;
12183 tree class_type, std_attributes = NULL_TREE;
12184 cp_cv_quals cv_quals;
12186 /* We don't know if there's a ptr-operator next, or not. */
12187 cp_parser_parse_tentatively (parser);
12188 /* Try the ptr-operator. */
12189 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12190 &std_attributes);
12191 /* If it worked, look for more conversion-declarators. */
12192 if (cp_parser_parse_definitely (parser))
12194 cp_declarator *declarator;
12196 /* Parse another optional declarator. */
12197 declarator = cp_parser_conversion_declarator_opt (parser);
12199 declarator = cp_parser_make_indirect_declarator
12200 (code, class_type, cv_quals, declarator, std_attributes);
12202 return declarator;
12205 return NULL;
12208 /* Parse an (optional) ctor-initializer.
12210 ctor-initializer:
12211 : mem-initializer-list
12213 Returns TRUE iff the ctor-initializer was actually present. */
12215 static bool
12216 cp_parser_ctor_initializer_opt (cp_parser* parser)
12218 /* If the next token is not a `:', then there is no
12219 ctor-initializer. */
12220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12222 /* Do default initialization of any bases and members. */
12223 if (DECL_CONSTRUCTOR_P (current_function_decl))
12224 finish_mem_initializers (NULL_TREE);
12226 return false;
12229 /* Consume the `:' token. */
12230 cp_lexer_consume_token (parser->lexer);
12231 /* And the mem-initializer-list. */
12232 cp_parser_mem_initializer_list (parser);
12234 return true;
12237 /* Parse a mem-initializer-list.
12239 mem-initializer-list:
12240 mem-initializer ... [opt]
12241 mem-initializer ... [opt] , mem-initializer-list */
12243 static void
12244 cp_parser_mem_initializer_list (cp_parser* parser)
12246 tree mem_initializer_list = NULL_TREE;
12247 tree target_ctor = error_mark_node;
12248 cp_token *token = cp_lexer_peek_token (parser->lexer);
12250 /* Let the semantic analysis code know that we are starting the
12251 mem-initializer-list. */
12252 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12253 error_at (token->location,
12254 "only constructors take member initializers");
12256 /* Loop through the list. */
12257 while (true)
12259 tree mem_initializer;
12261 token = cp_lexer_peek_token (parser->lexer);
12262 /* Parse the mem-initializer. */
12263 mem_initializer = cp_parser_mem_initializer (parser);
12264 /* If the next token is a `...', we're expanding member initializers. */
12265 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12267 /* Consume the `...'. */
12268 cp_lexer_consume_token (parser->lexer);
12270 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12271 can be expanded but members cannot. */
12272 if (mem_initializer != error_mark_node
12273 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12275 error_at (token->location,
12276 "cannot expand initializer for member %<%D%>",
12277 TREE_PURPOSE (mem_initializer));
12278 mem_initializer = error_mark_node;
12281 /* Construct the pack expansion type. */
12282 if (mem_initializer != error_mark_node)
12283 mem_initializer = make_pack_expansion (mem_initializer);
12285 if (target_ctor != error_mark_node
12286 && mem_initializer != error_mark_node)
12288 error ("mem-initializer for %qD follows constructor delegation",
12289 TREE_PURPOSE (mem_initializer));
12290 mem_initializer = error_mark_node;
12292 /* Look for a target constructor. */
12293 if (mem_initializer != error_mark_node
12294 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12295 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12297 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12298 if (mem_initializer_list)
12300 error ("constructor delegation follows mem-initializer for %qD",
12301 TREE_PURPOSE (mem_initializer_list));
12302 mem_initializer = error_mark_node;
12304 target_ctor = mem_initializer;
12306 /* Add it to the list, unless it was erroneous. */
12307 if (mem_initializer != error_mark_node)
12309 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12310 mem_initializer_list = mem_initializer;
12312 /* If the next token is not a `,', we're done. */
12313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12314 break;
12315 /* Consume the `,' token. */
12316 cp_lexer_consume_token (parser->lexer);
12319 /* Perform semantic analysis. */
12320 if (DECL_CONSTRUCTOR_P (current_function_decl))
12321 finish_mem_initializers (mem_initializer_list);
12324 /* Parse a mem-initializer.
12326 mem-initializer:
12327 mem-initializer-id ( expression-list [opt] )
12328 mem-initializer-id braced-init-list
12330 GNU extension:
12332 mem-initializer:
12333 ( expression-list [opt] )
12335 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12336 class) or FIELD_DECL (for a non-static data member) to initialize;
12337 the TREE_VALUE is the expression-list. An empty initialization
12338 list is represented by void_list_node. */
12340 static tree
12341 cp_parser_mem_initializer (cp_parser* parser)
12343 tree mem_initializer_id;
12344 tree expression_list;
12345 tree member;
12346 cp_token *token = cp_lexer_peek_token (parser->lexer);
12348 /* Find out what is being initialized. */
12349 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12351 permerror (token->location,
12352 "anachronistic old-style base class initializer");
12353 mem_initializer_id = NULL_TREE;
12355 else
12357 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12358 if (mem_initializer_id == error_mark_node)
12359 return mem_initializer_id;
12361 member = expand_member_init (mem_initializer_id);
12362 if (member && !DECL_P (member))
12363 in_base_initializer = 1;
12365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12367 bool expr_non_constant_p;
12368 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12369 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12370 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12371 expression_list = build_tree_list (NULL_TREE, expression_list);
12373 else
12375 vec<tree, va_gc> *vec;
12376 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12377 /*cast_p=*/false,
12378 /*allow_expansion_p=*/true,
12379 /*non_constant_p=*/NULL);
12380 if (vec == NULL)
12381 return error_mark_node;
12382 expression_list = build_tree_list_vec (vec);
12383 release_tree_vector (vec);
12386 if (expression_list == error_mark_node)
12387 return error_mark_node;
12388 if (!expression_list)
12389 expression_list = void_type_node;
12391 in_base_initializer = 0;
12393 return member ? build_tree_list (member, expression_list) : error_mark_node;
12396 /* Parse a mem-initializer-id.
12398 mem-initializer-id:
12399 :: [opt] nested-name-specifier [opt] class-name
12400 identifier
12402 Returns a TYPE indicating the class to be initializer for the first
12403 production. Returns an IDENTIFIER_NODE indicating the data member
12404 to be initialized for the second production. */
12406 static tree
12407 cp_parser_mem_initializer_id (cp_parser* parser)
12409 bool global_scope_p;
12410 bool nested_name_specifier_p;
12411 bool template_p = false;
12412 tree id;
12414 cp_token *token = cp_lexer_peek_token (parser->lexer);
12416 /* `typename' is not allowed in this context ([temp.res]). */
12417 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12419 error_at (token->location,
12420 "keyword %<typename%> not allowed in this context (a qualified "
12421 "member initializer is implicitly a type)");
12422 cp_lexer_consume_token (parser->lexer);
12424 /* Look for the optional `::' operator. */
12425 global_scope_p
12426 = (cp_parser_global_scope_opt (parser,
12427 /*current_scope_valid_p=*/false)
12428 != NULL_TREE);
12429 /* Look for the optional nested-name-specifier. The simplest way to
12430 implement:
12432 [temp.res]
12434 The keyword `typename' is not permitted in a base-specifier or
12435 mem-initializer; in these contexts a qualified name that
12436 depends on a template-parameter is implicitly assumed to be a
12437 type name.
12439 is to assume that we have seen the `typename' keyword at this
12440 point. */
12441 nested_name_specifier_p
12442 = (cp_parser_nested_name_specifier_opt (parser,
12443 /*typename_keyword_p=*/true,
12444 /*check_dependency_p=*/true,
12445 /*type_p=*/true,
12446 /*is_declaration=*/true)
12447 != NULL_TREE);
12448 if (nested_name_specifier_p)
12449 template_p = cp_parser_optional_template_keyword (parser);
12450 /* If there is a `::' operator or a nested-name-specifier, then we
12451 are definitely looking for a class-name. */
12452 if (global_scope_p || nested_name_specifier_p)
12453 return cp_parser_class_name (parser,
12454 /*typename_keyword_p=*/true,
12455 /*template_keyword_p=*/template_p,
12456 typename_type,
12457 /*check_dependency_p=*/true,
12458 /*class_head_p=*/false,
12459 /*is_declaration=*/true);
12460 /* Otherwise, we could also be looking for an ordinary identifier. */
12461 cp_parser_parse_tentatively (parser);
12462 /* Try a class-name. */
12463 id = cp_parser_class_name (parser,
12464 /*typename_keyword_p=*/true,
12465 /*template_keyword_p=*/false,
12466 none_type,
12467 /*check_dependency_p=*/true,
12468 /*class_head_p=*/false,
12469 /*is_declaration=*/true);
12470 /* If we found one, we're done. */
12471 if (cp_parser_parse_definitely (parser))
12472 return id;
12473 /* Otherwise, look for an ordinary identifier. */
12474 return cp_parser_identifier (parser);
12477 /* Overloading [gram.over] */
12479 /* Parse an operator-function-id.
12481 operator-function-id:
12482 operator operator
12484 Returns an IDENTIFIER_NODE for the operator which is a
12485 human-readable spelling of the identifier, e.g., `operator +'. */
12487 static tree
12488 cp_parser_operator_function_id (cp_parser* parser)
12490 /* Look for the `operator' keyword. */
12491 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12492 return error_mark_node;
12493 /* And then the name of the operator itself. */
12494 return cp_parser_operator (parser);
12497 /* Return an identifier node for a user-defined literal operator.
12498 The suffix identifier is chained to the operator name identifier. */
12500 static tree
12501 cp_literal_operator_id (const char* name)
12503 tree identifier;
12504 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12505 + strlen (name) + 10);
12506 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12507 identifier = get_identifier (buffer);
12509 return identifier;
12512 /* Parse an operator.
12514 operator:
12515 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12516 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12517 || ++ -- , ->* -> () []
12519 GNU Extensions:
12521 operator:
12522 <? >? <?= >?=
12524 Returns an IDENTIFIER_NODE for the operator which is a
12525 human-readable spelling of the identifier, e.g., `operator +'. */
12527 static tree
12528 cp_parser_operator (cp_parser* parser)
12530 tree id = NULL_TREE;
12531 cp_token *token;
12532 bool bad_encoding_prefix = false;
12534 /* Peek at the next token. */
12535 token = cp_lexer_peek_token (parser->lexer);
12536 /* Figure out which operator we have. */
12537 switch (token->type)
12539 case CPP_KEYWORD:
12541 enum tree_code op;
12543 /* The keyword should be either `new' or `delete'. */
12544 if (token->keyword == RID_NEW)
12545 op = NEW_EXPR;
12546 else if (token->keyword == RID_DELETE)
12547 op = DELETE_EXPR;
12548 else
12549 break;
12551 /* Consume the `new' or `delete' token. */
12552 cp_lexer_consume_token (parser->lexer);
12554 /* Peek at the next token. */
12555 token = cp_lexer_peek_token (parser->lexer);
12556 /* If it's a `[' token then this is the array variant of the
12557 operator. */
12558 if (token->type == CPP_OPEN_SQUARE)
12560 /* Consume the `[' token. */
12561 cp_lexer_consume_token (parser->lexer);
12562 /* Look for the `]' token. */
12563 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12564 id = ansi_opname (op == NEW_EXPR
12565 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12567 /* Otherwise, we have the non-array variant. */
12568 else
12569 id = ansi_opname (op);
12571 return id;
12574 case CPP_PLUS:
12575 id = ansi_opname (PLUS_EXPR);
12576 break;
12578 case CPP_MINUS:
12579 id = ansi_opname (MINUS_EXPR);
12580 break;
12582 case CPP_MULT:
12583 id = ansi_opname (MULT_EXPR);
12584 break;
12586 case CPP_DIV:
12587 id = ansi_opname (TRUNC_DIV_EXPR);
12588 break;
12590 case CPP_MOD:
12591 id = ansi_opname (TRUNC_MOD_EXPR);
12592 break;
12594 case CPP_XOR:
12595 id = ansi_opname (BIT_XOR_EXPR);
12596 break;
12598 case CPP_AND:
12599 id = ansi_opname (BIT_AND_EXPR);
12600 break;
12602 case CPP_OR:
12603 id = ansi_opname (BIT_IOR_EXPR);
12604 break;
12606 case CPP_COMPL:
12607 id = ansi_opname (BIT_NOT_EXPR);
12608 break;
12610 case CPP_NOT:
12611 id = ansi_opname (TRUTH_NOT_EXPR);
12612 break;
12614 case CPP_EQ:
12615 id = ansi_assopname (NOP_EXPR);
12616 break;
12618 case CPP_LESS:
12619 id = ansi_opname (LT_EXPR);
12620 break;
12622 case CPP_GREATER:
12623 id = ansi_opname (GT_EXPR);
12624 break;
12626 case CPP_PLUS_EQ:
12627 id = ansi_assopname (PLUS_EXPR);
12628 break;
12630 case CPP_MINUS_EQ:
12631 id = ansi_assopname (MINUS_EXPR);
12632 break;
12634 case CPP_MULT_EQ:
12635 id = ansi_assopname (MULT_EXPR);
12636 break;
12638 case CPP_DIV_EQ:
12639 id = ansi_assopname (TRUNC_DIV_EXPR);
12640 break;
12642 case CPP_MOD_EQ:
12643 id = ansi_assopname (TRUNC_MOD_EXPR);
12644 break;
12646 case CPP_XOR_EQ:
12647 id = ansi_assopname (BIT_XOR_EXPR);
12648 break;
12650 case CPP_AND_EQ:
12651 id = ansi_assopname (BIT_AND_EXPR);
12652 break;
12654 case CPP_OR_EQ:
12655 id = ansi_assopname (BIT_IOR_EXPR);
12656 break;
12658 case CPP_LSHIFT:
12659 id = ansi_opname (LSHIFT_EXPR);
12660 break;
12662 case CPP_RSHIFT:
12663 id = ansi_opname (RSHIFT_EXPR);
12664 break;
12666 case CPP_LSHIFT_EQ:
12667 id = ansi_assopname (LSHIFT_EXPR);
12668 break;
12670 case CPP_RSHIFT_EQ:
12671 id = ansi_assopname (RSHIFT_EXPR);
12672 break;
12674 case CPP_EQ_EQ:
12675 id = ansi_opname (EQ_EXPR);
12676 break;
12678 case CPP_NOT_EQ:
12679 id = ansi_opname (NE_EXPR);
12680 break;
12682 case CPP_LESS_EQ:
12683 id = ansi_opname (LE_EXPR);
12684 break;
12686 case CPP_GREATER_EQ:
12687 id = ansi_opname (GE_EXPR);
12688 break;
12690 case CPP_AND_AND:
12691 id = ansi_opname (TRUTH_ANDIF_EXPR);
12692 break;
12694 case CPP_OR_OR:
12695 id = ansi_opname (TRUTH_ORIF_EXPR);
12696 break;
12698 case CPP_PLUS_PLUS:
12699 id = ansi_opname (POSTINCREMENT_EXPR);
12700 break;
12702 case CPP_MINUS_MINUS:
12703 id = ansi_opname (PREDECREMENT_EXPR);
12704 break;
12706 case CPP_COMMA:
12707 id = ansi_opname (COMPOUND_EXPR);
12708 break;
12710 case CPP_DEREF_STAR:
12711 id = ansi_opname (MEMBER_REF);
12712 break;
12714 case CPP_DEREF:
12715 id = ansi_opname (COMPONENT_REF);
12716 break;
12718 case CPP_OPEN_PAREN:
12719 /* Consume the `('. */
12720 cp_lexer_consume_token (parser->lexer);
12721 /* Look for the matching `)'. */
12722 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12723 return ansi_opname (CALL_EXPR);
12725 case CPP_OPEN_SQUARE:
12726 /* Consume the `['. */
12727 cp_lexer_consume_token (parser->lexer);
12728 /* Look for the matching `]'. */
12729 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12730 return ansi_opname (ARRAY_REF);
12732 case CPP_WSTRING:
12733 case CPP_STRING16:
12734 case CPP_STRING32:
12735 case CPP_UTF8STRING:
12736 bad_encoding_prefix = true;
12737 /* Fall through. */
12739 case CPP_STRING:
12740 if (cxx_dialect == cxx98)
12741 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12742 if (bad_encoding_prefix)
12744 error ("invalid encoding prefix in literal operator");
12745 return error_mark_node;
12747 if (TREE_STRING_LENGTH (token->u.value) > 2)
12749 error ("expected empty string after %<operator%> keyword");
12750 return error_mark_node;
12752 /* Consume the string. */
12753 cp_lexer_consume_token (parser->lexer);
12754 /* Look for the suffix identifier. */
12755 token = cp_lexer_peek_token (parser->lexer);
12756 if (token->type == CPP_NAME)
12758 id = cp_parser_identifier (parser);
12759 if (id != error_mark_node)
12761 const char *name = IDENTIFIER_POINTER (id);
12762 return cp_literal_operator_id (name);
12765 else if (token->type == CPP_KEYWORD)
12767 error ("unexpected keyword;"
12768 " remove space between quotes and suffix identifier");
12769 return error_mark_node;
12771 else
12773 error ("expected suffix identifier");
12774 return error_mark_node;
12777 case CPP_WSTRING_USERDEF:
12778 case CPP_STRING16_USERDEF:
12779 case CPP_STRING32_USERDEF:
12780 case CPP_UTF8STRING_USERDEF:
12781 bad_encoding_prefix = true;
12782 /* Fall through. */
12784 case CPP_STRING_USERDEF:
12785 if (cxx_dialect == cxx98)
12786 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12787 if (bad_encoding_prefix)
12789 error ("invalid encoding prefix in literal operator");
12790 return error_mark_node;
12793 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12794 if (TREE_STRING_LENGTH (string_tree) > 2)
12796 error ("expected empty string after %<operator%> keyword");
12797 return error_mark_node;
12799 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12800 /* Consume the user-defined string literal. */
12801 cp_lexer_consume_token (parser->lexer);
12802 if (id != error_mark_node)
12804 const char *name = IDENTIFIER_POINTER (id);
12805 return cp_literal_operator_id (name);
12807 else
12808 return error_mark_node;
12811 default:
12812 /* Anything else is an error. */
12813 break;
12816 /* If we have selected an identifier, we need to consume the
12817 operator token. */
12818 if (id)
12819 cp_lexer_consume_token (parser->lexer);
12820 /* Otherwise, no valid operator name was present. */
12821 else
12823 cp_parser_error (parser, "expected operator");
12824 id = error_mark_node;
12827 return id;
12830 /* Parse a template-declaration.
12832 template-declaration:
12833 export [opt] template < template-parameter-list > declaration
12835 If MEMBER_P is TRUE, this template-declaration occurs within a
12836 class-specifier.
12838 The grammar rule given by the standard isn't correct. What
12839 is really meant is:
12841 template-declaration:
12842 export [opt] template-parameter-list-seq
12843 decl-specifier-seq [opt] init-declarator [opt] ;
12844 export [opt] template-parameter-list-seq
12845 function-definition
12847 template-parameter-list-seq:
12848 template-parameter-list-seq [opt]
12849 template < template-parameter-list >
12851 Concept Extensions:
12853 template-parameter-list-seq:
12854 template < template-parameter-list > template-requirement [opt]
12856 template-requirement:
12857 requires logical-or-expression
12860 static void
12861 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12863 /* Check for `export'. */
12864 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12866 /* Consume the `export' token. */
12867 cp_lexer_consume_token (parser->lexer);
12868 /* Warn that we do not support `export'. */
12869 warning (0, "keyword %<export%> not implemented, and will be ignored");
12872 cp_parser_template_declaration_after_export (parser, member_p);
12875 /* Parse a template-parameter-list.
12877 template-parameter-list:
12878 template-parameter
12879 template-parameter-list , template-parameter
12881 Returns a TREE_LIST. Each node represents a template parameter.
12882 The nodes are connected via their TREE_CHAINs. */
12884 static tree
12885 cp_parser_template_parameter_list (cp_parser* parser)
12887 tree parameter_list = NULL_TREE;
12889 begin_template_parm_list ();
12891 /* The loop below parses the template parms. We first need to know
12892 the total number of template parms to be able to compute proper
12893 canonical types of each dependent type. So after the loop, when
12894 we know the total number of template parms,
12895 end_template_parm_list computes the proper canonical types and
12896 fixes up the dependent types accordingly. */
12897 while (true)
12899 tree parameter;
12900 bool is_non_type;
12901 bool is_parameter_pack;
12902 location_t parm_loc;
12904 /* Parse the template-parameter. */
12905 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12906 parameter = cp_parser_template_parameter (parser,
12907 &is_non_type,
12908 &is_parameter_pack);
12909 /* Add it to the list. */
12910 if (parameter != error_mark_node)
12911 parameter_list = process_template_parm (parameter_list,
12912 parm_loc,
12913 parameter,
12914 is_non_type,
12915 is_parameter_pack);
12916 else
12918 tree err_parm = build_tree_list (parameter, parameter);
12919 parameter_list = chainon (parameter_list, err_parm);
12922 /* If the next token is not a `,', we're done. */
12923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12924 break;
12925 /* Otherwise, consume the `,' token. */
12926 cp_lexer_consume_token (parser->lexer);
12929 return end_template_parm_list (parameter_list);
12932 // Returns a constrained parameter if PARM denotes a constrained
12933 // template parameter.
12934 static inline bool
12935 cp_is_constrained_parameter (cp_parameter_declarator *parm)
12937 gcc_assert (parm);
12938 tree decl = parm->decl_specifiers.type;
12939 return (decl
12940 && TREE_CODE (decl) == TYPE_DECL
12941 && DECL_INITIAL (decl)
12942 && DECL_SIZE_UNIT (decl)
12943 && TREE_CODE (DECL_SIZE_UNIT (decl)) == FUNCTION_DECL);
12946 // Finish parsing/processing a template type parameter.
12947 static inline tree
12948 cp_finish_template_type_parm (tree id)
12950 return finish_template_type_parm (class_type_node, id);
12953 // Finish parsing/processing a template template parameter by borrowing
12954 // the template parameter list from the prototype parameter.
12955 static tree
12956 cp_finish_template_template_parm (tree proto, tree id)
12958 tree saved_parms = current_template_parms;
12960 // FIXME: This should probably be copied, and we may need to adjust
12961 // the template parameter depths.
12962 begin_template_parm_list ();
12963 current_template_parms = DECL_TEMPLATE_PARMS (proto);
12964 end_template_parm_list ();
12966 tree parm = finish_template_template_parm (class_type_node, id);
12967 current_template_parms = saved_parms;
12968 return parm;
12971 // Create a new non-type template parameter from the given PARM declarator.
12972 static tree
12973 cp_finish_non_type_template_parm (cp_parameter_declarator *parm)
12975 cp_declarator *decl = parm->declarator;
12976 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
12977 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
12978 return grokdeclarator (decl, specs, TPARM, 0, NULL);
12981 // Build a constrained template parameter based on the PARMDECL
12982 // declarator. The type of PARMDECL is the constrained type, which
12983 // refers to the prototype template parameter that ultimately
12984 // specifies the type of the declared parameter.
12985 static tree
12986 cp_finish_constrained_parameter (cp_parameter_declarator *parmdecl,
12987 bool *is_non_type,
12988 bool *is_parameter_pack)
12990 tree decl = parmdecl->decl_specifiers.type;
12991 tree id = parmdecl->declarator->u.id.unqualified_name;
12992 tree def = parmdecl->default_argument;
12993 tree proto = DECL_INITIAL (decl);
12995 // Remember if the user declared this as a parameter pack and
12996 // erase that flag on the annotation. Template packs are dealt
12997 // with separately.
12998 bool is_pack = parmdecl->declarator->parameter_pack_p;
12999 if (is_pack)
13000 parmdecl->declarator->parameter_pack_p = false;
13002 // Is the prototype a parameter pack? If so, but the declaration
13003 // does not include "...", then emit an error.
13004 bool is_variadic = template_parameter_pack_p (proto);
13005 if (is_variadic && !is_pack)
13006 error ("variadic constraint introduced without %<...%>");
13008 // The prototype is a template parameter pack, then the resulting
13009 // parameter also needs to be a pack.
13010 if (is_pack || is_variadic)
13011 *is_parameter_pack = true;
13013 tree parm;
13014 if (TREE_CODE (proto) == TYPE_DECL)
13015 parm = cp_finish_template_type_parm (id);
13016 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13017 parm = cp_finish_template_template_parm (proto, id);
13018 else
13020 *is_non_type = true;
13021 parm = cp_finish_non_type_template_parm (parmdecl);
13024 // Finish the parameter decl and create a node attaching the
13025 // default argument and constraint.
13026 parm = build_tree_list (def, parm);
13027 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13028 return parm;
13032 /* Parse a template-parameter.
13034 template-parameter:
13035 type-parameter
13036 parameter-declaration
13038 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13039 the parameter. The TREE_PURPOSE is the default value, if any.
13040 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13041 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13042 set to true iff this parameter is a parameter pack. */
13044 static tree
13045 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13046 bool *is_parameter_pack)
13048 cp_token *token;
13049 cp_parameter_declarator *parameter_declarator;
13050 cp_declarator *id_declarator;
13051 tree parm;
13053 /* Assume it is a type parameter or a template parameter. */
13054 *is_non_type = false;
13055 /* Assume it not a parameter pack. */
13056 *is_parameter_pack = false;
13057 /* Peek at the next token. */
13058 token = cp_lexer_peek_token (parser->lexer);
13059 /* If it is `class' or `template', we have a type-parameter. */
13060 if (token->keyword == RID_TEMPLATE)
13061 return cp_parser_type_parameter (parser, is_parameter_pack);
13062 /* If it is `class' or `typename' we do not know yet whether it is a
13063 type parameter or a non-type parameter. Consider:
13065 template <typename T, typename T::X X> ...
13069 template <class C, class D*> ...
13071 Here, the first parameter is a type parameter, and the second is
13072 a non-type parameter. We can tell by looking at the token after
13073 the identifier -- if it is a `,', `=', or `>' then we have a type
13074 parameter. */
13075 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13077 /* Peek at the token after `class' or `typename'. */
13078 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13079 /* If it's an ellipsis, we have a template type parameter
13080 pack. */
13081 if (token->type == CPP_ELLIPSIS)
13082 return cp_parser_type_parameter (parser, is_parameter_pack);
13083 /* If it's an identifier, skip it. */
13084 if (token->type == CPP_NAME)
13085 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13086 /* Now, see if the token looks like the end of a template
13087 parameter. */
13088 if (token->type == CPP_COMMA
13089 || token->type == CPP_EQ
13090 || token->type == CPP_GREATER)
13091 return cp_parser_type_parameter (parser, is_parameter_pack);
13094 /* Otherwise, it is a non-type parameter.
13096 [temp.param]
13098 When parsing a default template-argument for a non-type
13099 template-parameter, the first non-nested `>' is taken as the end
13100 of the template parameter-list rather than a greater-than
13101 operator. */
13102 parameter_declarator
13103 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13104 /*parenthesized_p=*/NULL);
13106 // The parameter may have been constrained.
13107 if (cp_is_constrained_parameter (parameter_declarator))
13108 return cp_finish_constrained_parameter (parameter_declarator,
13109 is_non_type,
13110 is_parameter_pack);
13112 // Now we're sure that the parameter is a non-type parameter.
13113 *is_non_type = true;
13116 /* If the parameter declaration is marked as a parameter pack, set
13117 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13118 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13119 grokdeclarator. */
13120 if (parameter_declarator
13121 && parameter_declarator->declarator
13122 && parameter_declarator->declarator->parameter_pack_p)
13124 *is_parameter_pack = true;
13125 parameter_declarator->declarator->parameter_pack_p = false;
13128 if (parameter_declarator
13129 && parameter_declarator->default_argument)
13131 /* Can happen in some cases of erroneous input (c++/34892). */
13132 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13133 /* Consume the `...' for better error recovery. */
13134 cp_lexer_consume_token (parser->lexer);
13136 /* If the next token is an ellipsis, and we don't already have it
13137 marked as a parameter pack, then we have a parameter pack (that
13138 has no declarator). */
13139 else if (!*is_parameter_pack
13140 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13141 && (declarator_can_be_parameter_pack
13142 (parameter_declarator->declarator)))
13144 /* Consume the `...'. */
13145 cp_lexer_consume_token (parser->lexer);
13146 maybe_warn_variadic_templates ();
13148 *is_parameter_pack = true;
13150 /* We might end up with a pack expansion as the type of the non-type
13151 template parameter, in which case this is a non-type template
13152 parameter pack. */
13153 else if (parameter_declarator
13154 && parameter_declarator->decl_specifiers.type
13155 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13157 *is_parameter_pack = true;
13158 parameter_declarator->decl_specifiers.type =
13159 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13162 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13164 /* Parameter packs cannot have default arguments. However, a
13165 user may try to do so, so we'll parse them and give an
13166 appropriate diagnostic here. */
13168 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13170 /* Find the name of the parameter pack. */
13171 id_declarator = parameter_declarator->declarator;
13172 while (id_declarator && id_declarator->kind != cdk_id)
13173 id_declarator = id_declarator->declarator;
13175 if (id_declarator && id_declarator->kind == cdk_id)
13176 error_at (start_token->location,
13177 "template parameter pack %qD cannot have a default argument",
13178 id_declarator->u.id.unqualified_name);
13179 else
13180 error_at (start_token->location,
13181 "template parameter pack cannot have a default argument");
13183 /* Parse the default argument, but throw away the result. */
13184 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13187 parm = grokdeclarator (parameter_declarator->declarator,
13188 &parameter_declarator->decl_specifiers,
13189 TPARM, /*initialized=*/0,
13190 /*attrlist=*/NULL);
13191 if (parm == error_mark_node)
13192 return error_mark_node;
13194 return build_tree_list (parameter_declarator->default_argument, parm);
13197 /* Parse a type-parameter.
13199 type-parameter:
13200 class identifier [opt]
13201 class identifier [opt] = type-id
13202 typename identifier [opt]
13203 typename identifier [opt] = type-id
13204 template < template-parameter-list > class identifier [opt]
13205 template < template-parameter-list > class identifier [opt]
13206 = id-expression
13208 GNU Extension (variadic templates):
13210 type-parameter:
13211 class ... identifier [opt]
13212 typename ... identifier [opt]
13214 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13215 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13216 the declaration of the parameter.
13218 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13220 static tree
13221 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13223 cp_token *token;
13224 tree parameter;
13226 /* Look for a keyword to tell us what kind of parameter this is. */
13227 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13228 if (!token)
13229 return error_mark_node;
13231 switch (token->keyword)
13233 case RID_CLASS:
13234 case RID_TYPENAME:
13236 tree identifier;
13237 tree default_argument;
13239 /* If the next token is an ellipsis, we have a template
13240 argument pack. */
13241 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13243 /* Consume the `...' token. */
13244 cp_lexer_consume_token (parser->lexer);
13245 maybe_warn_variadic_templates ();
13247 *is_parameter_pack = true;
13250 /* If the next token is an identifier, then it names the
13251 parameter. */
13252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13253 identifier = cp_parser_identifier (parser);
13254 else
13255 identifier = NULL_TREE;
13257 /* Create the parameter. */
13258 parameter = finish_template_type_parm (class_type_node, identifier);
13260 /* If the next token is an `=', we have a default argument. */
13261 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13263 /* Consume the `=' token. */
13264 cp_lexer_consume_token (parser->lexer);
13265 /* Parse the default-argument. */
13266 push_deferring_access_checks (dk_no_deferred);
13267 default_argument = cp_parser_type_id (parser);
13269 /* Template parameter packs cannot have default
13270 arguments. */
13271 if (*is_parameter_pack)
13273 if (identifier)
13274 error_at (token->location,
13275 "template parameter pack %qD cannot have a "
13276 "default argument", identifier);
13277 else
13278 error_at (token->location,
13279 "template parameter packs cannot have "
13280 "default arguments");
13281 default_argument = NULL_TREE;
13283 pop_deferring_access_checks ();
13285 else
13286 default_argument = NULL_TREE;
13288 /* Create the combined representation of the parameter and the
13289 default argument. */
13290 parameter = build_tree_list (default_argument, parameter);
13292 break;
13294 case RID_TEMPLATE:
13296 tree identifier;
13297 tree default_argument;
13299 // Save the current requirements before parsing the
13300 // template parameter list.
13301 tree saved_template_reqs = release (current_template_reqs);
13303 /* Look for the `<'. */
13304 cp_parser_require (parser, CPP_LESS, RT_LESS);
13305 /* Parse the template-parameter-list. */
13306 cp_parser_template_parameter_list (parser);
13307 /* Look for the `>'. */
13308 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13310 // If template requirements are present, parse them.
13311 if (flag_concepts)
13313 tree reqs = get_shorthand_requirements (current_template_parms);
13314 if (tree r = cp_parser_requires_clause_opt (parser))
13315 reqs = conjoin_requirements (reqs, r);
13316 current_template_reqs = finish_template_requirements (reqs);
13318 // Attach the constraints to the parameter list.
13319 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
13320 = current_template_reqs;
13323 /* Look for the `class' keyword. */
13324 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13325 /* If the next token is an ellipsis, we have a template
13326 argument pack. */
13327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13329 /* Consume the `...' token. */
13330 cp_lexer_consume_token (parser->lexer);
13331 maybe_warn_variadic_templates ();
13333 *is_parameter_pack = true;
13335 /* If the next token is an `=', then there is a
13336 default-argument. If the next token is a `>', we are at
13337 the end of the parameter-list. If the next token is a `,',
13338 then we are at the end of this parameter. */
13339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13340 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13341 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13343 identifier = cp_parser_identifier (parser);
13344 /* Treat invalid names as if the parameter were nameless. */
13345 if (identifier == error_mark_node)
13346 identifier = NULL_TREE;
13348 else
13349 identifier = NULL_TREE;
13351 /* Create the template parameter. */
13352 parameter = finish_template_template_parm (class_type_node,
13353 identifier);
13355 // Restore the saved constraints.
13356 current_template_reqs = saved_template_reqs;
13358 /* If the next token is an `=', then there is a
13359 default-argument. */
13360 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13362 bool is_template;
13364 /* Consume the `='. */
13365 cp_lexer_consume_token (parser->lexer);
13366 /* Parse the id-expression. */
13367 push_deferring_access_checks (dk_no_deferred);
13368 /* save token before parsing the id-expression, for error
13369 reporting */
13370 token = cp_lexer_peek_token (parser->lexer);
13371 default_argument
13372 = cp_parser_id_expression (parser,
13373 /*template_keyword_p=*/false,
13374 /*check_dependency_p=*/true,
13375 /*template_p=*/&is_template,
13376 /*declarator_p=*/false,
13377 /*optional_p=*/false);
13378 if (TREE_CODE (default_argument) == TYPE_DECL)
13379 /* If the id-expression was a template-id that refers to
13380 a template-class, we already have the declaration here,
13381 so no further lookup is needed. */
13383 else
13384 /* Look up the name. */
13385 default_argument
13386 = cp_parser_lookup_name (parser, default_argument,
13387 none_type,
13388 /*is_template=*/is_template,
13389 /*is_namespace=*/false,
13390 /*check_dependency=*/true,
13391 /*ambiguous_decls=*/NULL,
13392 token->location);
13393 /* See if the default argument is valid. */
13394 default_argument
13395 = check_template_template_default_arg (default_argument);
13397 /* Template parameter packs cannot have default
13398 arguments. */
13399 if (*is_parameter_pack)
13401 if (identifier)
13402 error_at (token->location,
13403 "template parameter pack %qD cannot "
13404 "have a default argument",
13405 identifier);
13406 else
13407 error_at (token->location, "template parameter packs cannot "
13408 "have default arguments");
13409 default_argument = NULL_TREE;
13411 pop_deferring_access_checks ();
13413 else
13414 default_argument = NULL_TREE;
13416 /* Create the combined representation of the parameter and the
13417 default argument. */
13418 parameter = build_tree_list (default_argument, parameter);
13420 break;
13422 default:
13423 gcc_unreachable ();
13424 break;
13427 return parameter;
13430 /* Parse a template-id.
13432 template-id:
13433 template-name < template-argument-list [opt] >
13435 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13436 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13437 returned. Otherwise, if the template-name names a function, or set
13438 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13439 names a class, returns a TYPE_DECL for the specialization.
13441 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13442 uninstantiated templates. */
13444 static tree
13445 cp_parser_template_id (cp_parser *parser,
13446 bool template_keyword_p,
13447 bool check_dependency_p,
13448 enum tag_types tag_type,
13449 bool is_declaration)
13451 int i;
13452 tree templ;
13453 tree arguments;
13454 tree template_id;
13455 cp_token_position start_of_id = 0;
13456 deferred_access_check *chk;
13457 vec<deferred_access_check, va_gc> *access_check;
13458 cp_token *next_token = NULL, *next_token_2 = NULL;
13459 bool is_identifier;
13461 /* If the next token corresponds to a template-id, there is no need
13462 to reparse it. */
13463 next_token = cp_lexer_peek_token (parser->lexer);
13464 if (next_token->type == CPP_TEMPLATE_ID)
13466 struct tree_check *check_value;
13468 /* Get the stored value. */
13469 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13470 /* Perform any access checks that were deferred. */
13471 access_check = check_value->checks;
13472 if (access_check)
13474 FOR_EACH_VEC_ELT (*access_check, i, chk)
13475 perform_or_defer_access_check (chk->binfo,
13476 chk->decl,
13477 chk->diag_decl,
13478 tf_warning_or_error);
13480 /* Return the stored value. */
13481 return check_value->value;
13484 /* Avoid performing name lookup if there is no possibility of
13485 finding a template-id. */
13486 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13487 || (next_token->type == CPP_NAME
13488 && !cp_parser_nth_token_starts_template_argument_list_p
13489 (parser, 2)))
13491 cp_parser_error (parser, "expected template-id");
13492 return error_mark_node;
13495 /* Remember where the template-id starts. */
13496 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13497 start_of_id = cp_lexer_token_position (parser->lexer, false);
13499 push_deferring_access_checks (dk_deferred);
13501 /* Parse the template-name. */
13502 is_identifier = false;
13503 templ = cp_parser_template_name (parser, template_keyword_p,
13504 check_dependency_p,
13505 is_declaration,
13506 tag_type,
13507 &is_identifier);
13508 if (templ == error_mark_node || is_identifier)
13510 pop_deferring_access_checks ();
13511 return templ;
13514 /* If we find the sequence `[:' after a template-name, it's probably
13515 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13516 parse correctly the argument list. */
13517 next_token = cp_lexer_peek_token (parser->lexer);
13518 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13519 if (next_token->type == CPP_OPEN_SQUARE
13520 && next_token->flags & DIGRAPH
13521 && next_token_2->type == CPP_COLON
13522 && !(next_token_2->flags & PREV_WHITE))
13524 cp_parser_parse_tentatively (parser);
13525 /* Change `:' into `::'. */
13526 next_token_2->type = CPP_SCOPE;
13527 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13528 CPP_LESS. */
13529 cp_lexer_consume_token (parser->lexer);
13531 /* Parse the arguments. */
13532 arguments = cp_parser_enclosed_template_argument_list (parser);
13533 if (!cp_parser_parse_definitely (parser))
13535 /* If we couldn't parse an argument list, then we revert our changes
13536 and return simply an error. Maybe this is not a template-id
13537 after all. */
13538 next_token_2->type = CPP_COLON;
13539 cp_parser_error (parser, "expected %<<%>");
13540 pop_deferring_access_checks ();
13541 return error_mark_node;
13543 /* Otherwise, emit an error about the invalid digraph, but continue
13544 parsing because we got our argument list. */
13545 if (permerror (next_token->location,
13546 "%<<::%> cannot begin a template-argument list"))
13548 static bool hint = false;
13549 inform (next_token->location,
13550 "%<<:%> is an alternate spelling for %<[%>."
13551 " Insert whitespace between %<<%> and %<::%>");
13552 if (!hint && !flag_permissive)
13554 inform (next_token->location, "(if you use %<-fpermissive%> "
13555 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13556 "accept your code)");
13557 hint = true;
13561 else
13563 /* Look for the `<' that starts the template-argument-list. */
13564 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13566 pop_deferring_access_checks ();
13567 return error_mark_node;
13569 /* Parse the arguments. */
13570 arguments = cp_parser_enclosed_template_argument_list (parser);
13573 /* Build a representation of the specialization. */
13574 if (identifier_p (templ))
13575 template_id = build_min_nt_loc (next_token->location,
13576 TEMPLATE_ID_EXPR,
13577 templ, arguments);
13578 else if (DECL_TYPE_TEMPLATE_P (templ)
13579 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13581 bool entering_scope;
13582 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13583 template (rather than some instantiation thereof) only if
13584 is not nested within some other construct. For example, in
13585 "template <typename T> void f(T) { A<T>::", A<T> is just an
13586 instantiation of A. */
13587 entering_scope = (template_parm_scope_p ()
13588 && cp_lexer_next_token_is (parser->lexer,
13589 CPP_SCOPE));
13590 template_id
13591 = finish_template_type (templ, arguments, entering_scope);
13593 else
13595 /* If it's not a class-template or a template-template, it should be
13596 a function-template. */
13597 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13598 || TREE_CODE (templ) == OVERLOAD
13599 || BASELINK_P (templ)));
13601 template_id = lookup_template_function (templ, arguments);
13604 /* If parsing tentatively, replace the sequence of tokens that makes
13605 up the template-id with a CPP_TEMPLATE_ID token. That way,
13606 should we re-parse the token stream, we will not have to repeat
13607 the effort required to do the parse, nor will we issue duplicate
13608 error messages about problems during instantiation of the
13609 template. */
13610 if (start_of_id)
13612 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13614 /* Reset the contents of the START_OF_ID token. */
13615 token->type = CPP_TEMPLATE_ID;
13616 /* Retrieve any deferred checks. Do not pop this access checks yet
13617 so the memory will not be reclaimed during token replacing below. */
13618 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13619 token->u.tree_check_value->value = template_id;
13620 token->u.tree_check_value->checks = get_deferred_access_checks ();
13621 token->keyword = RID_MAX;
13623 /* Purge all subsequent tokens. */
13624 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13626 /* ??? Can we actually assume that, if template_id ==
13627 error_mark_node, we will have issued a diagnostic to the
13628 user, as opposed to simply marking the tentative parse as
13629 failed? */
13630 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13631 error_at (token->location, "parse error in template argument list");
13634 pop_to_parent_deferring_access_checks ();
13635 return template_id;
13638 /* Parse a template-name.
13640 template-name:
13641 identifier
13643 The standard should actually say:
13645 template-name:
13646 identifier
13647 operator-function-id
13649 A defect report has been filed about this issue.
13651 A conversion-function-id cannot be a template name because they cannot
13652 be part of a template-id. In fact, looking at this code:
13654 a.operator K<int>()
13656 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13657 It is impossible to call a templated conversion-function-id with an
13658 explicit argument list, since the only allowed template parameter is
13659 the type to which it is converting.
13661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13662 `template' keyword, in a construction like:
13664 T::template f<3>()
13666 In that case `f' is taken to be a template-name, even though there
13667 is no way of knowing for sure.
13669 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13670 name refers to a set of overloaded functions, at least one of which
13671 is a template, or an IDENTIFIER_NODE with the name of the template,
13672 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13673 names are looked up inside uninstantiated templates. */
13675 static tree
13676 cp_parser_template_name (cp_parser* parser,
13677 bool template_keyword_p,
13678 bool check_dependency_p,
13679 bool is_declaration,
13680 enum tag_types tag_type,
13681 bool *is_identifier)
13683 tree identifier;
13684 tree decl;
13685 tree fns;
13686 cp_token *token = cp_lexer_peek_token (parser->lexer);
13688 /* If the next token is `operator', then we have either an
13689 operator-function-id or a conversion-function-id. */
13690 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13692 /* We don't know whether we're looking at an
13693 operator-function-id or a conversion-function-id. */
13694 cp_parser_parse_tentatively (parser);
13695 /* Try an operator-function-id. */
13696 identifier = cp_parser_operator_function_id (parser);
13697 /* If that didn't work, try a conversion-function-id. */
13698 if (!cp_parser_parse_definitely (parser))
13700 cp_parser_error (parser, "expected template-name");
13701 return error_mark_node;
13704 /* Look for the identifier. */
13705 else
13706 identifier = cp_parser_identifier (parser);
13708 /* If we didn't find an identifier, we don't have a template-id. */
13709 if (identifier == error_mark_node)
13710 return error_mark_node;
13712 /* If the name immediately followed the `template' keyword, then it
13713 is a template-name. However, if the next token is not `<', then
13714 we do not treat it as a template-name, since it is not being used
13715 as part of a template-id. This enables us to handle constructs
13716 like:
13718 template <typename T> struct S { S(); };
13719 template <typename T> S<T>::S();
13721 correctly. We would treat `S' as a template -- if it were `S<T>'
13722 -- but we do not if there is no `<'. */
13724 if (processing_template_decl
13725 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13727 /* In a declaration, in a dependent context, we pretend that the
13728 "template" keyword was present in order to improve error
13729 recovery. For example, given:
13731 template <typename T> void f(T::X<int>);
13733 we want to treat "X<int>" as a template-id. */
13734 if (is_declaration
13735 && !template_keyword_p
13736 && parser->scope && TYPE_P (parser->scope)
13737 && check_dependency_p
13738 && dependent_scope_p (parser->scope)
13739 /* Do not do this for dtors (or ctors), since they never
13740 need the template keyword before their name. */
13741 && !constructor_name_p (identifier, parser->scope))
13743 cp_token_position start = 0;
13745 /* Explain what went wrong. */
13746 error_at (token->location, "non-template %qD used as template",
13747 identifier);
13748 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13749 parser->scope, identifier);
13750 /* If parsing tentatively, find the location of the "<" token. */
13751 if (cp_parser_simulate_error (parser))
13752 start = cp_lexer_token_position (parser->lexer, true);
13753 /* Parse the template arguments so that we can issue error
13754 messages about them. */
13755 cp_lexer_consume_token (parser->lexer);
13756 cp_parser_enclosed_template_argument_list (parser);
13757 /* Skip tokens until we find a good place from which to
13758 continue parsing. */
13759 cp_parser_skip_to_closing_parenthesis (parser,
13760 /*recovering=*/true,
13761 /*or_comma=*/true,
13762 /*consume_paren=*/false);
13763 /* If parsing tentatively, permanently remove the
13764 template argument list. That will prevent duplicate
13765 error messages from being issued about the missing
13766 "template" keyword. */
13767 if (start)
13768 cp_lexer_purge_tokens_after (parser->lexer, start);
13769 if (is_identifier)
13770 *is_identifier = true;
13771 return identifier;
13774 /* If the "template" keyword is present, then there is generally
13775 no point in doing name-lookup, so we just return IDENTIFIER.
13776 But, if the qualifying scope is non-dependent then we can
13777 (and must) do name-lookup normally. */
13778 if (template_keyword_p
13779 && (!parser->scope
13780 || (TYPE_P (parser->scope)
13781 && dependent_type_p (parser->scope))))
13782 return identifier;
13785 /* Look up the name. */
13786 decl = cp_parser_lookup_name (parser, identifier,
13787 tag_type,
13788 /*is_template=*/true,
13789 /*is_namespace=*/false,
13790 check_dependency_p,
13791 /*ambiguous_decls=*/NULL,
13792 token->location);
13794 /* If DECL is a template, then the name was a template-name. */
13795 if (TREE_CODE (decl) == TEMPLATE_DECL)
13797 else
13799 tree fn = NULL_TREE;
13801 /* The standard does not explicitly indicate whether a name that
13802 names a set of overloaded declarations, some of which are
13803 templates, is a template-name. However, such a name should
13804 be a template-name; otherwise, there is no way to form a
13805 template-id for the overloaded templates. */
13806 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13807 if (TREE_CODE (fns) == OVERLOAD)
13808 for (fn = fns; fn; fn = OVL_NEXT (fn))
13809 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13810 break;
13812 if (!fn)
13814 /* The name does not name a template. */
13815 cp_parser_error (parser, "expected template-name");
13816 return error_mark_node;
13820 /* If DECL is dependent, and refers to a function, then just return
13821 its name; we will look it up again during template instantiation. */
13822 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13824 tree scope = ovl_scope (decl);
13825 if (TYPE_P (scope) && dependent_type_p (scope))
13826 return identifier;
13829 return decl;
13832 /* Parse a template-argument-list.
13834 template-argument-list:
13835 template-argument ... [opt]
13836 template-argument-list , template-argument ... [opt]
13838 Returns a TREE_VEC containing the arguments. */
13840 static tree
13841 cp_parser_template_argument_list (cp_parser* parser)
13843 tree fixed_args[10];
13844 unsigned n_args = 0;
13845 unsigned alloced = 10;
13846 tree *arg_ary = fixed_args;
13847 tree vec;
13848 bool saved_in_template_argument_list_p;
13849 bool saved_ice_p;
13850 bool saved_non_ice_p;
13852 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13853 parser->in_template_argument_list_p = true;
13854 /* Even if the template-id appears in an integral
13855 constant-expression, the contents of the argument list do
13856 not. */
13857 saved_ice_p = parser->integral_constant_expression_p;
13858 parser->integral_constant_expression_p = false;
13859 saved_non_ice_p = parser->non_integral_constant_expression_p;
13860 parser->non_integral_constant_expression_p = false;
13862 /* Parse the arguments. */
13865 tree argument;
13867 if (n_args)
13868 /* Consume the comma. */
13869 cp_lexer_consume_token (parser->lexer);
13871 /* Parse the template-argument. */
13872 argument = cp_parser_template_argument (parser);
13874 /* If the next token is an ellipsis, we're expanding a template
13875 argument pack. */
13876 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13878 if (argument == error_mark_node)
13880 cp_token *token = cp_lexer_peek_token (parser->lexer);
13881 error_at (token->location,
13882 "expected parameter pack before %<...%>");
13884 /* Consume the `...' token. */
13885 cp_lexer_consume_token (parser->lexer);
13887 /* Make the argument into a TYPE_PACK_EXPANSION or
13888 EXPR_PACK_EXPANSION. */
13889 argument = make_pack_expansion (argument);
13892 if (n_args == alloced)
13894 alloced *= 2;
13896 if (arg_ary == fixed_args)
13898 arg_ary = XNEWVEC (tree, alloced);
13899 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13901 else
13902 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13904 arg_ary[n_args++] = argument;
13906 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13908 vec = make_tree_vec (n_args);
13910 while (n_args--)
13911 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13913 if (arg_ary != fixed_args)
13914 free (arg_ary);
13915 parser->non_integral_constant_expression_p = saved_non_ice_p;
13916 parser->integral_constant_expression_p = saved_ice_p;
13917 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13918 #ifdef ENABLE_CHECKING
13919 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13920 #endif
13921 return vec;
13924 /* Parse a template-argument.
13926 template-argument:
13927 assignment-expression
13928 type-id
13929 id-expression
13931 The representation is that of an assignment-expression, type-id, or
13932 id-expression -- except that the qualified id-expression is
13933 evaluated, so that the value returned is either a DECL or an
13934 OVERLOAD.
13936 Although the standard says "assignment-expression", it forbids
13937 throw-expressions or assignments in the template argument.
13938 Therefore, we use "conditional-expression" instead. */
13940 static tree
13941 cp_parser_template_argument (cp_parser* parser)
13943 tree argument;
13944 bool template_p;
13945 bool address_p;
13946 bool maybe_type_id = false;
13947 cp_token *token = NULL, *argument_start_token = NULL;
13948 location_t loc = 0;
13949 cp_id_kind idk;
13951 /* There's really no way to know what we're looking at, so we just
13952 try each alternative in order.
13954 [temp.arg]
13956 In a template-argument, an ambiguity between a type-id and an
13957 expression is resolved to a type-id, regardless of the form of
13958 the corresponding template-parameter.
13960 Therefore, we try a type-id first. */
13961 cp_parser_parse_tentatively (parser);
13962 argument = cp_parser_template_type_arg (parser);
13963 /* If there was no error parsing the type-id but the next token is a
13964 '>>', our behavior depends on which dialect of C++ we're
13965 parsing. In C++98, we probably found a typo for '> >'. But there
13966 are type-id which are also valid expressions. For instance:
13968 struct X { int operator >> (int); };
13969 template <int V> struct Foo {};
13970 Foo<X () >> 5> r;
13972 Here 'X()' is a valid type-id of a function type, but the user just
13973 wanted to write the expression "X() >> 5". Thus, we remember that we
13974 found a valid type-id, but we still try to parse the argument as an
13975 expression to see what happens.
13977 In C++0x, the '>>' will be considered two separate '>'
13978 tokens. */
13979 if (!cp_parser_error_occurred (parser)
13980 && cxx_dialect == cxx98
13981 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13983 maybe_type_id = true;
13984 cp_parser_abort_tentative_parse (parser);
13986 else
13988 /* If the next token isn't a `,' or a `>', then this argument wasn't
13989 really finished. This means that the argument is not a valid
13990 type-id. */
13991 if (!cp_parser_next_token_ends_template_argument_p (parser))
13992 cp_parser_error (parser, "expected template-argument");
13993 /* If that worked, we're done. */
13994 if (cp_parser_parse_definitely (parser))
13995 return argument;
13997 /* We're still not sure what the argument will be. */
13998 cp_parser_parse_tentatively (parser);
13999 /* Try a template. */
14000 argument_start_token = cp_lexer_peek_token (parser->lexer);
14001 argument = cp_parser_id_expression (parser,
14002 /*template_keyword_p=*/false,
14003 /*check_dependency_p=*/true,
14004 &template_p,
14005 /*declarator_p=*/false,
14006 /*optional_p=*/false);
14007 /* If the next token isn't a `,' or a `>', then this argument wasn't
14008 really finished. */
14009 if (!cp_parser_next_token_ends_template_argument_p (parser))
14010 cp_parser_error (parser, "expected template-argument");
14011 if (!cp_parser_error_occurred (parser))
14013 /* Figure out what is being referred to. If the id-expression
14014 was for a class template specialization, then we will have a
14015 TYPE_DECL at this point. There is no need to do name lookup
14016 at this point in that case. */
14017 if (TREE_CODE (argument) != TYPE_DECL)
14018 argument = cp_parser_lookup_name (parser, argument,
14019 none_type,
14020 /*is_template=*/template_p,
14021 /*is_namespace=*/false,
14022 /*check_dependency=*/true,
14023 /*ambiguous_decls=*/NULL,
14024 argument_start_token->location);
14025 if (TREE_CODE (argument) != TEMPLATE_DECL
14026 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14027 cp_parser_error (parser, "expected template-name");
14029 if (cp_parser_parse_definitely (parser))
14030 return argument;
14031 /* It must be a non-type argument. There permitted cases are given
14032 in [temp.arg.nontype]:
14034 -- an integral constant-expression of integral or enumeration
14035 type; or
14037 -- the name of a non-type template-parameter; or
14039 -- the name of an object or function with external linkage...
14041 -- the address of an object or function with external linkage...
14043 -- a pointer to member... */
14044 /* Look for a non-type template parameter. */
14045 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14047 cp_parser_parse_tentatively (parser);
14048 argument = cp_parser_primary_expression (parser,
14049 /*address_p=*/false,
14050 /*cast_p=*/false,
14051 /*template_arg_p=*/true,
14052 &idk);
14053 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14054 || !cp_parser_next_token_ends_template_argument_p (parser))
14055 cp_parser_simulate_error (parser);
14056 if (cp_parser_parse_definitely (parser))
14057 return argument;
14060 /* If the next token is "&", the argument must be the address of an
14061 object or function with external linkage. */
14062 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14063 if (address_p)
14065 loc = cp_lexer_peek_token (parser->lexer)->location;
14066 cp_lexer_consume_token (parser->lexer);
14068 /* See if we might have an id-expression. */
14069 token = cp_lexer_peek_token (parser->lexer);
14070 if (token->type == CPP_NAME
14071 || token->keyword == RID_OPERATOR
14072 || token->type == CPP_SCOPE
14073 || token->type == CPP_TEMPLATE_ID
14074 || token->type == CPP_NESTED_NAME_SPECIFIER)
14076 cp_parser_parse_tentatively (parser);
14077 argument = cp_parser_primary_expression (parser,
14078 address_p,
14079 /*cast_p=*/false,
14080 /*template_arg_p=*/true,
14081 &idk);
14082 if (cp_parser_error_occurred (parser)
14083 || !cp_parser_next_token_ends_template_argument_p (parser))
14084 cp_parser_abort_tentative_parse (parser);
14085 else
14087 tree probe;
14089 if (INDIRECT_REF_P (argument))
14091 gcc_assert (REFERENCE_REF_P (argument));
14092 argument = TREE_OPERAND (argument, 0);
14095 /* If we're in a template, we represent a qualified-id referring
14096 to a static data member as a SCOPE_REF even if the scope isn't
14097 dependent so that we can check access control later. */
14098 probe = argument;
14099 if (TREE_CODE (probe) == SCOPE_REF)
14100 probe = TREE_OPERAND (probe, 1);
14101 if (VAR_P (probe))
14103 /* A variable without external linkage might still be a
14104 valid constant-expression, so no error is issued here
14105 if the external-linkage check fails. */
14106 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14107 cp_parser_simulate_error (parser);
14109 else if (is_overloaded_fn (argument))
14110 /* All overloaded functions are allowed; if the external
14111 linkage test does not pass, an error will be issued
14112 later. */
14114 else if (address_p
14115 && (TREE_CODE (argument) == OFFSET_REF
14116 || TREE_CODE (argument) == SCOPE_REF))
14117 /* A pointer-to-member. */
14119 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14121 else
14122 cp_parser_simulate_error (parser);
14124 if (cp_parser_parse_definitely (parser))
14126 if (address_p)
14127 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14128 tf_warning_or_error);
14129 return argument;
14133 /* If the argument started with "&", there are no other valid
14134 alternatives at this point. */
14135 if (address_p)
14137 cp_parser_error (parser, "invalid non-type template argument");
14138 return error_mark_node;
14141 /* If the argument wasn't successfully parsed as a type-id followed
14142 by '>>', the argument can only be a constant expression now.
14143 Otherwise, we try parsing the constant-expression tentatively,
14144 because the argument could really be a type-id. */
14145 if (maybe_type_id)
14146 cp_parser_parse_tentatively (parser);
14147 argument = cp_parser_constant_expression (parser,
14148 /*allow_non_constant_p=*/false,
14149 /*non_constant_p=*/NULL);
14150 if (!maybe_type_id)
14151 return argument;
14152 if (!cp_parser_next_token_ends_template_argument_p (parser))
14153 cp_parser_error (parser, "expected template-argument");
14154 if (cp_parser_parse_definitely (parser))
14155 return argument;
14156 /* We did our best to parse the argument as a non type-id, but that
14157 was the only alternative that matched (albeit with a '>' after
14158 it). We can assume it's just a typo from the user, and a
14159 diagnostic will then be issued. */
14160 return cp_parser_template_type_arg (parser);
14163 /* Parse an explicit-instantiation.
14165 explicit-instantiation:
14166 template declaration
14168 Although the standard says `declaration', what it really means is:
14170 explicit-instantiation:
14171 template decl-specifier-seq [opt] declarator [opt] ;
14173 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14174 supposed to be allowed. A defect report has been filed about this
14175 issue.
14177 GNU Extension:
14179 explicit-instantiation:
14180 storage-class-specifier template
14181 decl-specifier-seq [opt] declarator [opt] ;
14182 function-specifier template
14183 decl-specifier-seq [opt] declarator [opt] ; */
14185 static void
14186 cp_parser_explicit_instantiation (cp_parser* parser)
14188 int declares_class_or_enum;
14189 cp_decl_specifier_seq decl_specifiers;
14190 tree extension_specifier = NULL_TREE;
14192 timevar_push (TV_TEMPLATE_INST);
14194 /* Look for an (optional) storage-class-specifier or
14195 function-specifier. */
14196 if (cp_parser_allow_gnu_extensions_p (parser))
14198 extension_specifier
14199 = cp_parser_storage_class_specifier_opt (parser);
14200 if (!extension_specifier)
14201 extension_specifier
14202 = cp_parser_function_specifier_opt (parser,
14203 /*decl_specs=*/NULL);
14206 /* Look for the `template' keyword. */
14207 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14208 /* Let the front end know that we are processing an explicit
14209 instantiation. */
14210 begin_explicit_instantiation ();
14211 /* [temp.explicit] says that we are supposed to ignore access
14212 control while processing explicit instantiation directives. */
14213 push_deferring_access_checks (dk_no_check);
14214 /* Parse a decl-specifier-seq. */
14215 cp_parser_decl_specifier_seq (parser,
14216 CP_PARSER_FLAGS_OPTIONAL,
14217 &decl_specifiers,
14218 &declares_class_or_enum);
14219 /* If there was exactly one decl-specifier, and it declared a class,
14220 and there's no declarator, then we have an explicit type
14221 instantiation. */
14222 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14224 tree type;
14226 type = check_tag_decl (&decl_specifiers,
14227 /*explicit_type_instantiation_p=*/true);
14228 /* Turn access control back on for names used during
14229 template instantiation. */
14230 pop_deferring_access_checks ();
14231 if (type)
14232 do_type_instantiation (type, extension_specifier,
14233 /*complain=*/tf_error);
14235 else
14237 cp_declarator *declarator;
14238 tree decl;
14240 /* Parse the declarator. */
14241 declarator
14242 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14243 /*ctor_dtor_or_conv_p=*/NULL,
14244 /*parenthesized_p=*/NULL,
14245 /*member_p=*/false);
14246 if (declares_class_or_enum & 2)
14247 cp_parser_check_for_definition_in_return_type (declarator,
14248 decl_specifiers.type,
14249 decl_specifiers.locations[ds_type_spec]);
14250 if (declarator != cp_error_declarator)
14252 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14253 permerror (decl_specifiers.locations[ds_inline],
14254 "explicit instantiation shall not use"
14255 " %<inline%> specifier");
14256 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14257 permerror (decl_specifiers.locations[ds_constexpr],
14258 "explicit instantiation shall not use"
14259 " %<constexpr%> specifier");
14261 decl = grokdeclarator (declarator, &decl_specifiers,
14262 NORMAL, 0, &decl_specifiers.attributes);
14263 /* Turn access control back on for names used during
14264 template instantiation. */
14265 pop_deferring_access_checks ();
14266 /* Do the explicit instantiation. */
14267 do_decl_instantiation (decl, extension_specifier);
14269 else
14271 pop_deferring_access_checks ();
14272 /* Skip the body of the explicit instantiation. */
14273 cp_parser_skip_to_end_of_statement (parser);
14276 /* We're done with the instantiation. */
14277 end_explicit_instantiation ();
14279 cp_parser_consume_semicolon_at_end_of_statement (parser);
14281 timevar_pop (TV_TEMPLATE_INST);
14284 /* Parse an explicit-specialization.
14286 explicit-specialization:
14287 template < > declaration
14289 Although the standard says `declaration', what it really means is:
14291 explicit-specialization:
14292 template <> decl-specifier [opt] init-declarator [opt] ;
14293 template <> function-definition
14294 template <> explicit-specialization
14295 template <> template-declaration */
14297 static void
14298 cp_parser_explicit_specialization (cp_parser* parser)
14300 bool need_lang_pop;
14301 cp_token *token = cp_lexer_peek_token (parser->lexer);
14303 /* Look for the `template' keyword. */
14304 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14305 /* Look for the `<'. */
14306 cp_parser_require (parser, CPP_LESS, RT_LESS);
14307 /* Look for the `>'. */
14308 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14309 /* We have processed another parameter list. */
14310 ++parser->num_template_parameter_lists;
14311 /* [temp]
14313 A template ... explicit specialization ... shall not have C
14314 linkage. */
14315 if (current_lang_name == lang_name_c)
14317 error_at (token->location, "template specialization with C linkage");
14318 /* Give it C++ linkage to avoid confusing other parts of the
14319 front end. */
14320 push_lang_context (lang_name_cplusplus);
14321 need_lang_pop = true;
14323 else
14324 need_lang_pop = false;
14325 /* Let the front end know that we are beginning a specialization. */
14326 if (!begin_specialization ())
14328 end_specialization ();
14329 return;
14332 /* If the next keyword is `template', we need to figure out whether
14333 or not we're looking a template-declaration. */
14334 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14336 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14337 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14338 cp_parser_template_declaration_after_export (parser,
14339 /*member_p=*/false);
14340 else
14341 cp_parser_explicit_specialization (parser);
14343 else
14344 /* Parse the dependent declaration. */
14345 cp_parser_single_declaration (parser,
14346 /*checks=*/NULL,
14347 /*member_p=*/false,
14348 /*explicit_specialization_p=*/true,
14349 /*friend_p=*/NULL);
14350 /* We're done with the specialization. */
14351 end_specialization ();
14352 /* For the erroneous case of a template with C linkage, we pushed an
14353 implicit C++ linkage scope; exit that scope now. */
14354 if (need_lang_pop)
14355 pop_lang_context ();
14356 /* We're done with this parameter list. */
14357 --parser->num_template_parameter_lists;
14360 /* Parse a type-specifier.
14362 type-specifier:
14363 simple-type-specifier
14364 class-specifier
14365 enum-specifier
14366 elaborated-type-specifier
14367 cv-qualifier
14369 GNU Extension:
14371 type-specifier:
14372 __complex__
14374 Returns a representation of the type-specifier. For a
14375 class-specifier, enum-specifier, or elaborated-type-specifier, a
14376 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14378 The parser flags FLAGS is used to control type-specifier parsing.
14380 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14381 in a decl-specifier-seq.
14383 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14384 class-specifier, enum-specifier, or elaborated-type-specifier, then
14385 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14386 if a type is declared; 2 if it is defined. Otherwise, it is set to
14387 zero.
14389 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14390 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14391 is set to FALSE. */
14393 static tree
14394 cp_parser_type_specifier (cp_parser* parser,
14395 cp_parser_flags flags,
14396 cp_decl_specifier_seq *decl_specs,
14397 bool is_declaration,
14398 int* declares_class_or_enum,
14399 bool* is_cv_qualifier)
14401 tree type_spec = NULL_TREE;
14402 cp_token *token;
14403 enum rid keyword;
14404 cp_decl_spec ds = ds_last;
14406 /* Assume this type-specifier does not declare a new type. */
14407 if (declares_class_or_enum)
14408 *declares_class_or_enum = 0;
14409 /* And that it does not specify a cv-qualifier. */
14410 if (is_cv_qualifier)
14411 *is_cv_qualifier = false;
14412 /* Peek at the next token. */
14413 token = cp_lexer_peek_token (parser->lexer);
14415 /* If we're looking at a keyword, we can use that to guide the
14416 production we choose. */
14417 keyword = token->keyword;
14418 switch (keyword)
14420 case RID_ENUM:
14421 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14422 goto elaborated_type_specifier;
14424 /* Look for the enum-specifier. */
14425 type_spec = cp_parser_enum_specifier (parser);
14426 /* If that worked, we're done. */
14427 if (type_spec)
14429 if (declares_class_or_enum)
14430 *declares_class_or_enum = 2;
14431 if (decl_specs)
14432 cp_parser_set_decl_spec_type (decl_specs,
14433 type_spec,
14434 token,
14435 /*type_definition_p=*/true);
14436 return type_spec;
14438 else
14439 goto elaborated_type_specifier;
14441 /* Any of these indicate either a class-specifier, or an
14442 elaborated-type-specifier. */
14443 case RID_CLASS:
14444 case RID_STRUCT:
14445 case RID_UNION:
14446 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14447 goto elaborated_type_specifier;
14449 /* Parse tentatively so that we can back up if we don't find a
14450 class-specifier. */
14451 cp_parser_parse_tentatively (parser);
14452 /* Look for the class-specifier. */
14453 type_spec = cp_parser_class_specifier (parser);
14454 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14455 /* If that worked, we're done. */
14456 if (cp_parser_parse_definitely (parser))
14458 if (declares_class_or_enum)
14459 *declares_class_or_enum = 2;
14460 if (decl_specs)
14461 cp_parser_set_decl_spec_type (decl_specs,
14462 type_spec,
14463 token,
14464 /*type_definition_p=*/true);
14465 return type_spec;
14468 /* Fall through. */
14469 elaborated_type_specifier:
14470 /* We're declaring (not defining) a class or enum. */
14471 if (declares_class_or_enum)
14472 *declares_class_or_enum = 1;
14474 /* Fall through. */
14475 case RID_TYPENAME:
14476 /* Look for an elaborated-type-specifier. */
14477 type_spec
14478 = (cp_parser_elaborated_type_specifier
14479 (parser,
14480 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14481 is_declaration));
14482 if (decl_specs)
14483 cp_parser_set_decl_spec_type (decl_specs,
14484 type_spec,
14485 token,
14486 /*type_definition_p=*/false);
14487 return type_spec;
14489 case RID_CONST:
14490 ds = ds_const;
14491 if (is_cv_qualifier)
14492 *is_cv_qualifier = true;
14493 break;
14495 case RID_VOLATILE:
14496 ds = ds_volatile;
14497 if (is_cv_qualifier)
14498 *is_cv_qualifier = true;
14499 break;
14501 case RID_RESTRICT:
14502 ds = ds_restrict;
14503 if (is_cv_qualifier)
14504 *is_cv_qualifier = true;
14505 break;
14507 case RID_COMPLEX:
14508 /* The `__complex__' keyword is a GNU extension. */
14509 ds = ds_complex;
14510 break;
14512 default:
14513 break;
14516 /* Handle simple keywords. */
14517 if (ds != ds_last)
14519 if (decl_specs)
14521 set_and_check_decl_spec_loc (decl_specs, ds, token);
14522 decl_specs->any_specifiers_p = true;
14524 return cp_lexer_consume_token (parser->lexer)->u.value;
14527 /* If we do not already have a type-specifier, assume we are looking
14528 at a simple-type-specifier. */
14529 type_spec = cp_parser_simple_type_specifier (parser,
14530 decl_specs,
14531 flags);
14533 /* If we didn't find a type-specifier, and a type-specifier was not
14534 optional in this context, issue an error message. */
14535 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14537 cp_parser_error (parser, "expected type specifier");
14538 return error_mark_node;
14541 return type_spec;
14544 /* Parse a simple-type-specifier.
14546 simple-type-specifier:
14547 :: [opt] nested-name-specifier [opt] type-name
14548 :: [opt] nested-name-specifier template template-id
14549 char
14550 wchar_t
14551 bool
14552 short
14554 long
14555 signed
14556 unsigned
14557 float
14558 double
14559 void
14561 C++0x Extension:
14563 simple-type-specifier:
14564 auto
14565 decltype ( expression )
14566 char16_t
14567 char32_t
14568 __underlying_type ( type-id )
14570 GNU Extension:
14572 simple-type-specifier:
14573 __int128
14574 __typeof__ unary-expression
14575 __typeof__ ( type-id )
14576 __typeof__ ( type-id ) { initializer-list , [opt] }
14578 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14579 appropriately updated. */
14581 static tree
14582 cp_parser_simple_type_specifier (cp_parser* parser,
14583 cp_decl_specifier_seq *decl_specs,
14584 cp_parser_flags flags)
14586 tree type = NULL_TREE;
14587 cp_token *token;
14589 /* Peek at the next token. */
14590 token = cp_lexer_peek_token (parser->lexer);
14592 /* If we're looking at a keyword, things are easy. */
14593 switch (token->keyword)
14595 case RID_CHAR:
14596 if (decl_specs)
14597 decl_specs->explicit_char_p = true;
14598 type = char_type_node;
14599 break;
14600 case RID_CHAR16:
14601 type = char16_type_node;
14602 break;
14603 case RID_CHAR32:
14604 type = char32_type_node;
14605 break;
14606 case RID_WCHAR:
14607 type = wchar_type_node;
14608 break;
14609 case RID_BOOL:
14610 type = boolean_type_node;
14611 break;
14612 case RID_SHORT:
14613 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14614 type = short_integer_type_node;
14615 break;
14616 case RID_INT:
14617 if (decl_specs)
14618 decl_specs->explicit_int_p = true;
14619 type = integer_type_node;
14620 break;
14621 case RID_INT128:
14622 if (!int128_integer_type_node)
14623 break;
14624 if (decl_specs)
14625 decl_specs->explicit_int128_p = true;
14626 type = int128_integer_type_node;
14627 break;
14628 case RID_LONG:
14629 if (decl_specs)
14630 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14631 type = long_integer_type_node;
14632 break;
14633 case RID_SIGNED:
14634 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14635 type = integer_type_node;
14636 break;
14637 case RID_UNSIGNED:
14638 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14639 type = unsigned_type_node;
14640 break;
14641 case RID_FLOAT:
14642 type = float_type_node;
14643 break;
14644 case RID_DOUBLE:
14645 type = double_type_node;
14646 break;
14647 case RID_VOID:
14648 type = void_type_node;
14649 break;
14651 case RID_AUTO:
14652 maybe_warn_cpp0x (CPP0X_AUTO);
14653 if (parser->auto_is_implicit_function_template_parm_p)
14655 type = synthesize_implicit_template_parm (parser);
14657 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14659 if (cxx_dialect < cxx1y)
14660 pedwarn (location_of (type), 0,
14661 "use of %<auto%> in lambda parameter declaration "
14662 "only available with "
14663 "-std=c++1y or -std=gnu++1y");
14665 else if (cxx_dialect < cxx1y)
14666 pedwarn (location_of (type), 0,
14667 "use of %<auto%> in parameter declaration "
14668 "only available with "
14669 "-std=c++1y or -std=gnu++1y");
14670 else
14671 pedwarn (location_of (type), OPT_Wpedantic,
14672 "ISO C++ forbids use of %<auto%> in parameter "
14673 "declaration");
14675 else
14676 type = make_auto ();
14677 break;
14679 case RID_DECLTYPE:
14680 /* Since DR 743, decltype can either be a simple-type-specifier by
14681 itself or begin a nested-name-specifier. Parsing it will replace
14682 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14683 handling below decide what to do. */
14684 cp_parser_decltype (parser);
14685 cp_lexer_set_token_position (parser->lexer, token);
14686 break;
14688 case RID_TYPEOF:
14689 /* Consume the `typeof' token. */
14690 cp_lexer_consume_token (parser->lexer);
14691 /* Parse the operand to `typeof'. */
14692 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14693 /* If it is not already a TYPE, take its type. */
14694 if (!TYPE_P (type))
14695 type = finish_typeof (type);
14697 if (decl_specs)
14698 cp_parser_set_decl_spec_type (decl_specs, type,
14699 token,
14700 /*type_definition_p=*/false);
14702 return type;
14704 case RID_UNDERLYING_TYPE:
14705 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14706 if (decl_specs)
14707 cp_parser_set_decl_spec_type (decl_specs, type,
14708 token,
14709 /*type_definition_p=*/false);
14711 return type;
14713 case RID_BASES:
14714 case RID_DIRECT_BASES:
14715 type = cp_parser_trait_expr (parser, token->keyword);
14716 if (decl_specs)
14717 cp_parser_set_decl_spec_type (decl_specs, type,
14718 token,
14719 /*type_definition_p=*/false);
14720 return type;
14721 default:
14722 break;
14725 /* If token is an already-parsed decltype not followed by ::,
14726 it's a simple-type-specifier. */
14727 if (token->type == CPP_DECLTYPE
14728 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14730 type = token->u.value;
14731 if (decl_specs)
14732 cp_parser_set_decl_spec_type (decl_specs, type,
14733 token,
14734 /*type_definition_p=*/false);
14735 cp_lexer_consume_token (parser->lexer);
14736 return type;
14739 /* If the type-specifier was for a built-in type, we're done. */
14740 if (type)
14742 /* Record the type. */
14743 if (decl_specs
14744 && (token->keyword != RID_SIGNED
14745 && token->keyword != RID_UNSIGNED
14746 && token->keyword != RID_SHORT
14747 && token->keyword != RID_LONG))
14748 cp_parser_set_decl_spec_type (decl_specs,
14749 type,
14750 token,
14751 /*type_definition_p=*/false);
14752 if (decl_specs)
14753 decl_specs->any_specifiers_p = true;
14755 /* Consume the token. */
14756 cp_lexer_consume_token (parser->lexer);
14758 /* There is no valid C++ program where a non-template type is
14759 followed by a "<". That usually indicates that the user thought
14760 that the type was a template. */
14761 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14762 token->location);
14764 return TYPE_NAME (type);
14767 /* The type-specifier must be a user-defined type. */
14768 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14770 bool qualified_p;
14771 bool global_p;
14773 /* Don't gobble tokens or issue error messages if this is an
14774 optional type-specifier. */
14775 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14776 cp_parser_parse_tentatively (parser);
14778 /* Look for the optional `::' operator. */
14779 global_p
14780 = (cp_parser_global_scope_opt (parser,
14781 /*current_scope_valid_p=*/false)
14782 != NULL_TREE);
14783 /* Look for the nested-name specifier. */
14784 qualified_p
14785 = (cp_parser_nested_name_specifier_opt (parser,
14786 /*typename_keyword_p=*/false,
14787 /*check_dependency_p=*/true,
14788 /*type_p=*/false,
14789 /*is_declaration=*/false)
14790 != NULL_TREE);
14791 token = cp_lexer_peek_token (parser->lexer);
14792 /* If we have seen a nested-name-specifier, and the next token
14793 is `template', then we are using the template-id production. */
14794 if (parser->scope
14795 && cp_parser_optional_template_keyword (parser))
14797 /* Look for the template-id. */
14798 type = cp_parser_template_id (parser,
14799 /*template_keyword_p=*/true,
14800 /*check_dependency_p=*/true,
14801 none_type,
14802 /*is_declaration=*/false);
14803 /* If the template-id did not name a type, we are out of
14804 luck. */
14805 if (TREE_CODE (type) != TYPE_DECL)
14807 cp_parser_error (parser, "expected template-id for type");
14808 type = NULL_TREE;
14811 /* Otherwise, look for a type-name. */
14812 else
14813 type = cp_parser_type_name (parser);
14814 /* Keep track of all name-lookups performed in class scopes. */
14815 if (type
14816 && !global_p
14817 && !qualified_p
14818 && TREE_CODE (type) == TYPE_DECL
14819 && identifier_p (DECL_NAME (type)))
14820 maybe_note_name_used_in_class (DECL_NAME (type), type);
14821 /* If it didn't work out, we don't have a TYPE. */
14822 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14823 && !cp_parser_parse_definitely (parser))
14824 type = NULL_TREE;
14825 if (type && decl_specs)
14826 cp_parser_set_decl_spec_type (decl_specs, type,
14827 token,
14828 /*type_definition_p=*/false);
14831 /* If we didn't get a type-name, issue an error message. */
14832 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14834 cp_parser_error (parser, "expected type-name");
14835 return error_mark_node;
14838 if (type && type != error_mark_node)
14840 /* See if TYPE is an Objective-C type, and if so, parse and
14841 accept any protocol references following it. Do this before
14842 the cp_parser_check_for_invalid_template_id() call, because
14843 Objective-C types can be followed by '<...>' which would
14844 enclose protocol names rather than template arguments, and so
14845 everything is fine. */
14846 if (c_dialect_objc () && !parser->scope
14847 && (objc_is_id (type) || objc_is_class_name (type)))
14849 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14850 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14852 /* Clobber the "unqualified" type previously entered into
14853 DECL_SPECS with the new, improved protocol-qualified version. */
14854 if (decl_specs)
14855 decl_specs->type = qual_type;
14857 return qual_type;
14860 /* There is no valid C++ program where a non-template type is
14861 followed by a "<". That usually indicates that the user
14862 thought that the type was a template. */
14863 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14864 none_type,
14865 token->location);
14868 return type;
14871 /* Parse a type-name.
14873 type-name:
14874 class-name
14875 enum-name
14876 typedef-name
14877 simple-template-id [in c++0x]
14879 enum-name:
14880 identifier
14882 typedef-name:
14883 identifier
14885 Concepts:
14887 type-name:
14888 concept-name
14890 concept-name:
14891 identifier
14893 Returns a TYPE_DECL for the type. */
14895 static tree
14896 cp_parser_type_name (cp_parser* parser)
14898 tree type_decl;
14900 /* We can't know yet whether it is a class-name or not. */
14901 cp_parser_parse_tentatively (parser);
14902 /* Try a class-name. */
14903 type_decl = cp_parser_class_name (parser,
14904 /*typename_keyword_p=*/false,
14905 /*template_keyword_p=*/false,
14906 none_type,
14907 /*check_dependency_p=*/true,
14908 /*class_head_p=*/false,
14909 /*is_declaration=*/false);
14910 /* If it's not a class-name, keep looking. */
14911 if (!cp_parser_parse_definitely (parser))
14913 if (cxx_dialect < cxx11)
14914 /* It must be a typedef-name or an enum-name. */
14915 return cp_parser_nonclass_name (parser);
14917 cp_parser_parse_tentatively (parser);
14918 /* It is either a simple-template-id representing an
14919 instantiation of an alias template... */
14920 type_decl = cp_parser_template_id (parser,
14921 /*template_keyword_p=*/false,
14922 /*check_dependency_p=*/false,
14923 none_type,
14924 /*is_declaration=*/false);
14925 /* Note that this must be an instantiation of an alias template
14926 because [temp.names]/6 says:
14928 A template-id that names an alias template specialization
14929 is a type-name.
14931 Whereas [temp.names]/7 says:
14933 A simple-template-id that names a class template
14934 specialization is a class-name. */
14935 if (type_decl != NULL_TREE
14936 && TREE_CODE (type_decl) == TYPE_DECL
14937 && TYPE_DECL_ALIAS_P (type_decl))
14938 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14939 else
14940 cp_parser_simulate_error (parser);
14942 if (!cp_parser_parse_definitely (parser))
14943 /* ... Or a typedef-name or an enum-name. */
14944 return cp_parser_nonclass_name (parser);
14947 return type_decl;
14950 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
14951 or a concept-name.
14953 enum-name:
14954 identifier
14956 typedef-name:
14957 identifier
14959 concept-name:
14960 identifier
14962 Returns a TYPE_DECL for the type. */
14964 static tree
14965 cp_parser_nonclass_name (cp_parser* parser)
14967 tree type_decl;
14968 tree identifier;
14970 cp_token *token = cp_lexer_peek_token (parser->lexer);
14971 identifier = cp_parser_identifier (parser);
14972 if (identifier == error_mark_node)
14973 return error_mark_node;
14975 /* Look up the type-name. */
14976 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14978 // If we found an overload set, then it may refer to a concept-name.
14980 // TODO: The name could also refer to a variable template or an
14981 // introduction (if followed by '{').
14982 if (TREE_CODE (type_decl) == OVERLOAD)
14984 // Determine whether the overload refers to a concept.
14985 if (tree decl = finish_concept_name (type_decl))
14986 return decl;
14989 if (TREE_CODE (type_decl) == USING_DECL)
14991 if (!DECL_DEPENDENT_P (type_decl))
14992 type_decl = strip_using_decl (type_decl);
14993 else if (USING_DECL_TYPENAME_P (type_decl))
14995 /* We have found a type introduced by a using
14996 declaration at class scope that refers to a dependent
14997 type.
14999 using typename :: [opt] nested-name-specifier unqualified-id ;
15001 type_decl = make_typename_type (TREE_TYPE (type_decl),
15002 DECL_NAME (type_decl),
15003 typename_type, tf_error);
15004 if (type_decl != error_mark_node)
15005 type_decl = TYPE_NAME (type_decl);
15009 if (TREE_CODE (type_decl) != TYPE_DECL
15010 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15012 /* See if this is an Objective-C type. */
15013 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15014 tree type = objc_get_protocol_qualified_type (identifier, protos);
15015 if (type)
15016 type_decl = TYPE_NAME (type);
15019 /* Issue an error if we did not find a type-name. */
15020 if (TREE_CODE (type_decl) != TYPE_DECL
15021 /* In Objective-C, we have the complication that class names are
15022 normally type names and start declarations (eg, the
15023 "NSObject" in "NSObject *object;"), but can be used in an
15024 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15025 is an expression. So, a classname followed by a dot is not a
15026 valid type-name. */
15027 || (objc_is_class_name (TREE_TYPE (type_decl))
15028 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15030 if (!cp_parser_simulate_error (parser))
15031 cp_parser_name_lookup_error (parser, identifier, type_decl,
15032 NLE_TYPE, token->location);
15033 return error_mark_node;
15035 /* Remember that the name was used in the definition of the
15036 current class so that we can check later to see if the
15037 meaning would have been different after the class was
15038 entirely defined. */
15039 else if (type_decl != error_mark_node
15040 && !parser->scope)
15041 maybe_note_name_used_in_class (identifier, type_decl);
15043 return type_decl;
15046 /* Parse an elaborated-type-specifier. Note that the grammar given
15047 here incorporates the resolution to DR68.
15049 elaborated-type-specifier:
15050 class-key :: [opt] nested-name-specifier [opt] identifier
15051 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15052 enum-key :: [opt] nested-name-specifier [opt] identifier
15053 typename :: [opt] nested-name-specifier identifier
15054 typename :: [opt] nested-name-specifier template [opt]
15055 template-id
15057 GNU extension:
15059 elaborated-type-specifier:
15060 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15061 class-key attributes :: [opt] nested-name-specifier [opt]
15062 template [opt] template-id
15063 enum attributes :: [opt] nested-name-specifier [opt] identifier
15065 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15066 declared `friend'. If IS_DECLARATION is TRUE, then this
15067 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15068 something is being declared.
15070 Returns the TYPE specified. */
15072 static tree
15073 cp_parser_elaborated_type_specifier (cp_parser* parser,
15074 bool is_friend,
15075 bool is_declaration)
15077 enum tag_types tag_type;
15078 tree identifier;
15079 tree type = NULL_TREE;
15080 tree attributes = NULL_TREE;
15081 tree globalscope;
15082 cp_token *token = NULL;
15084 /* See if we're looking at the `enum' keyword. */
15085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15087 /* Consume the `enum' token. */
15088 cp_lexer_consume_token (parser->lexer);
15089 /* Remember that it's an enumeration type. */
15090 tag_type = enum_type;
15091 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15092 enums) is used here. */
15093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15094 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15096 pedwarn (input_location, 0, "elaborated-type-specifier "
15097 "for a scoped enum must not use the %<%D%> keyword",
15098 cp_lexer_peek_token (parser->lexer)->u.value);
15099 /* Consume the `struct' or `class' and parse it anyway. */
15100 cp_lexer_consume_token (parser->lexer);
15102 /* Parse the attributes. */
15103 attributes = cp_parser_attributes_opt (parser);
15105 /* Or, it might be `typename'. */
15106 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15107 RID_TYPENAME))
15109 /* Consume the `typename' token. */
15110 cp_lexer_consume_token (parser->lexer);
15111 /* Remember that it's a `typename' type. */
15112 tag_type = typename_type;
15114 /* Otherwise it must be a class-key. */
15115 else
15117 tag_type = cp_parser_class_key (parser);
15118 if (tag_type == none_type)
15119 return error_mark_node;
15120 /* Parse the attributes. */
15121 attributes = cp_parser_attributes_opt (parser);
15124 /* Look for the `::' operator. */
15125 globalscope = cp_parser_global_scope_opt (parser,
15126 /*current_scope_valid_p=*/false);
15127 /* Look for the nested-name-specifier. */
15128 if (tag_type == typename_type && !globalscope)
15130 if (!cp_parser_nested_name_specifier (parser,
15131 /*typename_keyword_p=*/true,
15132 /*check_dependency_p=*/true,
15133 /*type_p=*/true,
15134 is_declaration))
15135 return error_mark_node;
15137 else
15138 /* Even though `typename' is not present, the proposed resolution
15139 to Core Issue 180 says that in `class A<T>::B', `B' should be
15140 considered a type-name, even if `A<T>' is dependent. */
15141 cp_parser_nested_name_specifier_opt (parser,
15142 /*typename_keyword_p=*/true,
15143 /*check_dependency_p=*/true,
15144 /*type_p=*/true,
15145 is_declaration);
15146 /* For everything but enumeration types, consider a template-id.
15147 For an enumeration type, consider only a plain identifier. */
15148 if (tag_type != enum_type)
15150 bool template_p = false;
15151 tree decl;
15153 /* Allow the `template' keyword. */
15154 template_p = cp_parser_optional_template_keyword (parser);
15155 /* If we didn't see `template', we don't know if there's a
15156 template-id or not. */
15157 if (!template_p)
15158 cp_parser_parse_tentatively (parser);
15159 /* Parse the template-id. */
15160 token = cp_lexer_peek_token (parser->lexer);
15161 decl = cp_parser_template_id (parser, template_p,
15162 /*check_dependency_p=*/true,
15163 tag_type,
15164 is_declaration);
15165 /* If we didn't find a template-id, look for an ordinary
15166 identifier. */
15167 if (!template_p && !cp_parser_parse_definitely (parser))
15169 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15170 in effect, then we must assume that, upon instantiation, the
15171 template will correspond to a class. */
15172 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15173 && tag_type == typename_type)
15174 type = make_typename_type (parser->scope, decl,
15175 typename_type,
15176 /*complain=*/tf_error);
15177 /* If the `typename' keyword is in effect and DECL is not a type
15178 decl, then type is non existent. */
15179 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15181 else if (TREE_CODE (decl) == TYPE_DECL)
15182 type = check_elaborated_type_specifier (tag_type, decl,
15183 /*allow_template_p=*/true);
15184 else if (decl == error_mark_node)
15185 type = error_mark_node;
15188 if (!type)
15190 token = cp_lexer_peek_token (parser->lexer);
15191 identifier = cp_parser_identifier (parser);
15193 if (identifier == error_mark_node)
15195 parser->scope = NULL_TREE;
15196 return error_mark_node;
15199 /* For a `typename', we needn't call xref_tag. */
15200 if (tag_type == typename_type
15201 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15202 return cp_parser_make_typename_type (parser, parser->scope,
15203 identifier,
15204 token->location);
15205 /* Look up a qualified name in the usual way. */
15206 if (parser->scope)
15208 tree decl;
15209 tree ambiguous_decls;
15211 decl = cp_parser_lookup_name (parser, identifier,
15212 tag_type,
15213 /*is_template=*/false,
15214 /*is_namespace=*/false,
15215 /*check_dependency=*/true,
15216 &ambiguous_decls,
15217 token->location);
15219 /* If the lookup was ambiguous, an error will already have been
15220 issued. */
15221 if (ambiguous_decls)
15222 return error_mark_node;
15224 /* If we are parsing friend declaration, DECL may be a
15225 TEMPLATE_DECL tree node here. However, we need to check
15226 whether this TEMPLATE_DECL results in valid code. Consider
15227 the following example:
15229 namespace N {
15230 template <class T> class C {};
15232 class X {
15233 template <class T> friend class N::C; // #1, valid code
15235 template <class T> class Y {
15236 friend class N::C; // #2, invalid code
15239 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15240 name lookup of `N::C'. We see that friend declaration must
15241 be template for the code to be valid. Note that
15242 processing_template_decl does not work here since it is
15243 always 1 for the above two cases. */
15245 decl = (cp_parser_maybe_treat_template_as_class
15246 (decl, /*tag_name_p=*/is_friend
15247 && parser->num_template_parameter_lists));
15249 if (TREE_CODE (decl) != TYPE_DECL)
15251 cp_parser_diagnose_invalid_type_name (parser,
15252 parser->scope,
15253 identifier,
15254 token->location);
15255 return error_mark_node;
15258 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15260 bool allow_template = (parser->num_template_parameter_lists
15261 || DECL_SELF_REFERENCE_P (decl));
15262 type = check_elaborated_type_specifier (tag_type, decl,
15263 allow_template);
15265 if (type == error_mark_node)
15266 return error_mark_node;
15269 /* Forward declarations of nested types, such as
15271 class C1::C2;
15272 class C1::C2::C3;
15274 are invalid unless all components preceding the final '::'
15275 are complete. If all enclosing types are complete, these
15276 declarations become merely pointless.
15278 Invalid forward declarations of nested types are errors
15279 caught elsewhere in parsing. Those that are pointless arrive
15280 here. */
15282 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15283 && !is_friend && !processing_explicit_instantiation)
15284 warning (0, "declaration %qD does not declare anything", decl);
15286 type = TREE_TYPE (decl);
15288 else
15290 /* An elaborated-type-specifier sometimes introduces a new type and
15291 sometimes names an existing type. Normally, the rule is that it
15292 introduces a new type only if there is not an existing type of
15293 the same name already in scope. For example, given:
15295 struct S {};
15296 void f() { struct S s; }
15298 the `struct S' in the body of `f' is the same `struct S' as in
15299 the global scope; the existing definition is used. However, if
15300 there were no global declaration, this would introduce a new
15301 local class named `S'.
15303 An exception to this rule applies to the following code:
15305 namespace N { struct S; }
15307 Here, the elaborated-type-specifier names a new type
15308 unconditionally; even if there is already an `S' in the
15309 containing scope this declaration names a new type.
15310 This exception only applies if the elaborated-type-specifier
15311 forms the complete declaration:
15313 [class.name]
15315 A declaration consisting solely of `class-key identifier ;' is
15316 either a redeclaration of the name in the current scope or a
15317 forward declaration of the identifier as a class name. It
15318 introduces the name into the current scope.
15320 We are in this situation precisely when the next token is a `;'.
15322 An exception to the exception is that a `friend' declaration does
15323 *not* name a new type; i.e., given:
15325 struct S { friend struct T; };
15327 `T' is not a new type in the scope of `S'.
15329 Also, `new struct S' or `sizeof (struct S)' never results in the
15330 definition of a new type; a new type can only be declared in a
15331 declaration context. */
15333 tag_scope ts;
15334 bool template_p;
15336 if (is_friend)
15337 /* Friends have special name lookup rules. */
15338 ts = ts_within_enclosing_non_class;
15339 else if (is_declaration
15340 && cp_lexer_next_token_is (parser->lexer,
15341 CPP_SEMICOLON))
15342 /* This is a `class-key identifier ;' */
15343 ts = ts_current;
15344 else
15345 ts = ts_global;
15347 template_p =
15348 (parser->num_template_parameter_lists
15349 && (cp_parser_next_token_starts_class_definition_p (parser)
15350 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15351 /* An unqualified name was used to reference this type, so
15352 there were no qualifying templates. */
15353 if (!cp_parser_check_template_parameters (parser,
15354 /*num_templates=*/0,
15355 token->location,
15356 /*declarator=*/NULL))
15357 return error_mark_node;
15358 type = xref_tag (tag_type, identifier, ts, template_p);
15362 if (type == error_mark_node)
15363 return error_mark_node;
15365 /* Allow attributes on forward declarations of classes. */
15366 if (attributes)
15368 if (TREE_CODE (type) == TYPENAME_TYPE)
15369 warning (OPT_Wattributes,
15370 "attributes ignored on uninstantiated type");
15371 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15372 && ! processing_explicit_instantiation)
15373 warning (OPT_Wattributes,
15374 "attributes ignored on template instantiation");
15375 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15376 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15377 else
15378 warning (OPT_Wattributes,
15379 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15382 if (tag_type != enum_type)
15384 /* Indicate whether this class was declared as a `class' or as a
15385 `struct'. */
15386 if (TREE_CODE (type) == RECORD_TYPE)
15387 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15388 cp_parser_check_class_key (tag_type, type);
15391 /* A "<" cannot follow an elaborated type specifier. If that
15392 happens, the user was probably trying to form a template-id. */
15393 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15394 token->location);
15396 return type;
15399 /* Parse an enum-specifier.
15401 enum-specifier:
15402 enum-head { enumerator-list [opt] }
15403 enum-head { enumerator-list , } [C++0x]
15405 enum-head:
15406 enum-key identifier [opt] enum-base [opt]
15407 enum-key nested-name-specifier identifier enum-base [opt]
15409 enum-key:
15410 enum
15411 enum class [C++0x]
15412 enum struct [C++0x]
15414 enum-base: [C++0x]
15415 : type-specifier-seq
15417 opaque-enum-specifier:
15418 enum-key identifier enum-base [opt] ;
15420 GNU Extensions:
15421 enum-key attributes[opt] identifier [opt] enum-base [opt]
15422 { enumerator-list [opt] }attributes[opt]
15423 enum-key attributes[opt] identifier [opt] enum-base [opt]
15424 { enumerator-list, }attributes[opt] [C++0x]
15426 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15427 if the token stream isn't an enum-specifier after all. */
15429 static tree
15430 cp_parser_enum_specifier (cp_parser* parser)
15432 tree identifier;
15433 tree type = NULL_TREE;
15434 tree prev_scope;
15435 tree nested_name_specifier = NULL_TREE;
15436 tree attributes;
15437 bool scoped_enum_p = false;
15438 bool has_underlying_type = false;
15439 bool nested_being_defined = false;
15440 bool new_value_list = false;
15441 bool is_new_type = false;
15442 bool is_anonymous = false;
15443 tree underlying_type = NULL_TREE;
15444 cp_token *type_start_token = NULL;
15445 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15447 parser->colon_corrects_to_scope_p = false;
15449 /* Parse tentatively so that we can back up if we don't find a
15450 enum-specifier. */
15451 cp_parser_parse_tentatively (parser);
15453 /* Caller guarantees that the current token is 'enum', an identifier
15454 possibly follows, and the token after that is an opening brace.
15455 If we don't have an identifier, fabricate an anonymous name for
15456 the enumeration being defined. */
15457 cp_lexer_consume_token (parser->lexer);
15459 /* Parse the "class" or "struct", which indicates a scoped
15460 enumeration type in C++0x. */
15461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15462 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15464 if (cxx_dialect < cxx11)
15465 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15467 /* Consume the `struct' or `class' token. */
15468 cp_lexer_consume_token (parser->lexer);
15470 scoped_enum_p = true;
15473 attributes = cp_parser_attributes_opt (parser);
15475 /* Clear the qualification. */
15476 parser->scope = NULL_TREE;
15477 parser->qualifying_scope = NULL_TREE;
15478 parser->object_scope = NULL_TREE;
15480 /* Figure out in what scope the declaration is being placed. */
15481 prev_scope = current_scope ();
15483 type_start_token = cp_lexer_peek_token (parser->lexer);
15485 push_deferring_access_checks (dk_no_check);
15486 nested_name_specifier
15487 = cp_parser_nested_name_specifier_opt (parser,
15488 /*typename_keyword_p=*/true,
15489 /*check_dependency_p=*/false,
15490 /*type_p=*/false,
15491 /*is_declaration=*/false);
15493 if (nested_name_specifier)
15495 tree name;
15497 identifier = cp_parser_identifier (parser);
15498 name = cp_parser_lookup_name (parser, identifier,
15499 enum_type,
15500 /*is_template=*/false,
15501 /*is_namespace=*/false,
15502 /*check_dependency=*/true,
15503 /*ambiguous_decls=*/NULL,
15504 input_location);
15505 if (name && name != error_mark_node)
15507 type = TREE_TYPE (name);
15508 if (TREE_CODE (type) == TYPENAME_TYPE)
15510 /* Are template enums allowed in ISO? */
15511 if (template_parm_scope_p ())
15512 pedwarn (type_start_token->location, OPT_Wpedantic,
15513 "%qD is an enumeration template", name);
15514 /* ignore a typename reference, for it will be solved by name
15515 in start_enum. */
15516 type = NULL_TREE;
15519 else if (nested_name_specifier == error_mark_node)
15520 /* We already issued an error. */;
15521 else
15522 error_at (type_start_token->location,
15523 "%qD is not an enumerator-name", identifier);
15525 else
15527 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15528 identifier = cp_parser_identifier (parser);
15529 else
15531 identifier = make_anon_name ();
15532 is_anonymous = true;
15533 if (scoped_enum_p)
15534 error_at (type_start_token->location,
15535 "anonymous scoped enum is not allowed");
15538 pop_deferring_access_checks ();
15540 /* Check for the `:' that denotes a specified underlying type in C++0x.
15541 Note that a ':' could also indicate a bitfield width, however. */
15542 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15544 cp_decl_specifier_seq type_specifiers;
15546 /* Consume the `:'. */
15547 cp_lexer_consume_token (parser->lexer);
15549 /* Parse the type-specifier-seq. */
15550 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15551 /*is_trailing_return=*/false,
15552 &type_specifiers);
15554 /* At this point this is surely not elaborated type specifier. */
15555 if (!cp_parser_parse_definitely (parser))
15556 return NULL_TREE;
15558 if (cxx_dialect < cxx11)
15559 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15561 has_underlying_type = true;
15563 /* If that didn't work, stop. */
15564 if (type_specifiers.type != error_mark_node)
15566 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15567 /*initialized=*/0, NULL);
15568 if (underlying_type == error_mark_node)
15569 underlying_type = NULL_TREE;
15573 /* Look for the `{' but don't consume it yet. */
15574 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15576 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15578 cp_parser_error (parser, "expected %<{%>");
15579 if (has_underlying_type)
15581 type = NULL_TREE;
15582 goto out;
15585 /* An opaque-enum-specifier must have a ';' here. */
15586 if ((scoped_enum_p || underlying_type)
15587 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15589 cp_parser_error (parser, "expected %<;%> or %<{%>");
15590 if (has_underlying_type)
15592 type = NULL_TREE;
15593 goto out;
15598 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15599 return NULL_TREE;
15601 if (nested_name_specifier)
15603 if (CLASS_TYPE_P (nested_name_specifier))
15605 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15606 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15607 push_scope (nested_name_specifier);
15609 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15611 push_nested_namespace (nested_name_specifier);
15615 /* Issue an error message if type-definitions are forbidden here. */
15616 if (!cp_parser_check_type_definition (parser))
15617 type = error_mark_node;
15618 else
15619 /* Create the new type. We do this before consuming the opening
15620 brace so the enum will be recorded as being on the line of its
15621 tag (or the 'enum' keyword, if there is no tag). */
15622 type = start_enum (identifier, type, underlying_type,
15623 scoped_enum_p, &is_new_type);
15625 /* If the next token is not '{' it is an opaque-enum-specifier or an
15626 elaborated-type-specifier. */
15627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15629 timevar_push (TV_PARSE_ENUM);
15630 if (nested_name_specifier
15631 && nested_name_specifier != error_mark_node)
15633 /* The following catches invalid code such as:
15634 enum class S<int>::E { A, B, C }; */
15635 if (!processing_specialization
15636 && CLASS_TYPE_P (nested_name_specifier)
15637 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15638 error_at (type_start_token->location, "cannot add an enumerator "
15639 "list to a template instantiation");
15641 /* If that scope does not contain the scope in which the
15642 class was originally declared, the program is invalid. */
15643 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
15645 if (at_namespace_scope_p ())
15646 error_at (type_start_token->location,
15647 "declaration of %qD in namespace %qD which does not "
15648 "enclose %qD",
15649 type, prev_scope, nested_name_specifier);
15650 else
15651 error_at (type_start_token->location,
15652 "declaration of %qD in %qD which does not enclose %qD",
15653 type, prev_scope, nested_name_specifier);
15654 type = error_mark_node;
15658 if (scoped_enum_p)
15659 begin_scope (sk_scoped_enum, type);
15661 /* Consume the opening brace. */
15662 cp_lexer_consume_token (parser->lexer);
15664 if (type == error_mark_node)
15665 ; /* Nothing to add */
15666 else if (OPAQUE_ENUM_P (type)
15667 || (cxx_dialect > cxx98 && processing_specialization))
15669 new_value_list = true;
15670 SET_OPAQUE_ENUM_P (type, false);
15671 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15673 else
15675 error_at (type_start_token->location, "multiple definition of %q#T", type);
15676 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15677 "previous definition here");
15678 type = error_mark_node;
15681 if (type == error_mark_node)
15682 cp_parser_skip_to_end_of_block_or_statement (parser);
15683 /* If the next token is not '}', then there are some enumerators. */
15684 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15686 if (is_anonymous && !scoped_enum_p)
15687 pedwarn (type_start_token->location, OPT_Wpedantic,
15688 "ISO C++ forbids empty anonymous enum");
15690 else
15691 cp_parser_enumerator_list (parser, type);
15693 /* Consume the final '}'. */
15694 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15696 if (scoped_enum_p)
15697 finish_scope ();
15698 timevar_pop (TV_PARSE_ENUM);
15700 else
15702 /* If a ';' follows, then it is an opaque-enum-specifier
15703 and additional restrictions apply. */
15704 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15706 if (is_anonymous)
15707 error_at (type_start_token->location,
15708 "opaque-enum-specifier without name");
15709 else if (nested_name_specifier)
15710 error_at (type_start_token->location,
15711 "opaque-enum-specifier must use a simple identifier");
15715 /* Look for trailing attributes to apply to this enumeration, and
15716 apply them if appropriate. */
15717 if (cp_parser_allow_gnu_extensions_p (parser))
15719 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15720 trailing_attr = chainon (trailing_attr, attributes);
15721 cplus_decl_attributes (&type,
15722 trailing_attr,
15723 (int) ATTR_FLAG_TYPE_IN_PLACE);
15726 /* Finish up the enumeration. */
15727 if (type != error_mark_node)
15729 if (new_value_list)
15730 finish_enum_value_list (type);
15731 if (is_new_type)
15732 finish_enum (type);
15735 if (nested_name_specifier)
15737 if (CLASS_TYPE_P (nested_name_specifier))
15739 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15740 pop_scope (nested_name_specifier);
15742 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15744 pop_nested_namespace (nested_name_specifier);
15747 out:
15748 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15749 return type;
15752 /* Parse an enumerator-list. The enumerators all have the indicated
15753 TYPE.
15755 enumerator-list:
15756 enumerator-definition
15757 enumerator-list , enumerator-definition */
15759 static void
15760 cp_parser_enumerator_list (cp_parser* parser, tree type)
15762 while (true)
15764 /* Parse an enumerator-definition. */
15765 cp_parser_enumerator_definition (parser, type);
15767 /* If the next token is not a ',', we've reached the end of
15768 the list. */
15769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15770 break;
15771 /* Otherwise, consume the `,' and keep going. */
15772 cp_lexer_consume_token (parser->lexer);
15773 /* If the next token is a `}', there is a trailing comma. */
15774 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15776 if (cxx_dialect < cxx11 && !in_system_header)
15777 pedwarn (input_location, OPT_Wpedantic,
15778 "comma at end of enumerator list");
15779 break;
15784 /* Parse an enumerator-definition. The enumerator has the indicated
15785 TYPE.
15787 enumerator-definition:
15788 enumerator
15789 enumerator = constant-expression
15791 enumerator:
15792 identifier */
15794 static void
15795 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15797 tree identifier;
15798 tree value;
15799 location_t loc;
15801 /* Save the input location because we are interested in the location
15802 of the identifier and not the location of the explicit value. */
15803 loc = cp_lexer_peek_token (parser->lexer)->location;
15805 /* Look for the identifier. */
15806 identifier = cp_parser_identifier (parser);
15807 if (identifier == error_mark_node)
15808 return;
15810 /* If the next token is an '=', then there is an explicit value. */
15811 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15813 /* Consume the `=' token. */
15814 cp_lexer_consume_token (parser->lexer);
15815 /* Parse the value. */
15816 value = cp_parser_constant_expression (parser,
15817 /*allow_non_constant_p=*/false,
15818 NULL);
15820 else
15821 value = NULL_TREE;
15823 /* If we are processing a template, make sure the initializer of the
15824 enumerator doesn't contain any bare template parameter pack. */
15825 if (check_for_bare_parameter_packs (value))
15826 value = error_mark_node;
15828 /* integral_constant_value will pull out this expression, so make sure
15829 it's folded as appropriate. */
15830 value = fold_non_dependent_expr (value);
15832 /* Create the enumerator. */
15833 build_enumerator (identifier, value, type, loc);
15836 /* Parse a namespace-name.
15838 namespace-name:
15839 original-namespace-name
15840 namespace-alias
15842 Returns the NAMESPACE_DECL for the namespace. */
15844 static tree
15845 cp_parser_namespace_name (cp_parser* parser)
15847 tree identifier;
15848 tree namespace_decl;
15850 cp_token *token = cp_lexer_peek_token (parser->lexer);
15852 /* Get the name of the namespace. */
15853 identifier = cp_parser_identifier (parser);
15854 if (identifier == error_mark_node)
15855 return error_mark_node;
15857 /* Look up the identifier in the currently active scope. Look only
15858 for namespaces, due to:
15860 [basic.lookup.udir]
15862 When looking up a namespace-name in a using-directive or alias
15863 definition, only namespace names are considered.
15865 And:
15867 [basic.lookup.qual]
15869 During the lookup of a name preceding the :: scope resolution
15870 operator, object, function, and enumerator names are ignored.
15872 (Note that cp_parser_qualifying_entity only calls this
15873 function if the token after the name is the scope resolution
15874 operator.) */
15875 namespace_decl = cp_parser_lookup_name (parser, identifier,
15876 none_type,
15877 /*is_template=*/false,
15878 /*is_namespace=*/true,
15879 /*check_dependency=*/true,
15880 /*ambiguous_decls=*/NULL,
15881 token->location);
15882 /* If it's not a namespace, issue an error. */
15883 if (namespace_decl == error_mark_node
15884 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15886 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15887 error_at (token->location, "%qD is not a namespace-name", identifier);
15888 cp_parser_error (parser, "expected namespace-name");
15889 namespace_decl = error_mark_node;
15892 return namespace_decl;
15895 /* Parse a namespace-definition.
15897 namespace-definition:
15898 named-namespace-definition
15899 unnamed-namespace-definition
15901 named-namespace-definition:
15902 original-namespace-definition
15903 extension-namespace-definition
15905 original-namespace-definition:
15906 namespace identifier { namespace-body }
15908 extension-namespace-definition:
15909 namespace original-namespace-name { namespace-body }
15911 unnamed-namespace-definition:
15912 namespace { namespace-body } */
15914 static void
15915 cp_parser_namespace_definition (cp_parser* parser)
15917 tree identifier, attribs;
15918 bool has_visibility;
15919 bool is_inline;
15921 cp_ensure_no_omp_declare_simd (parser);
15922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15924 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15925 is_inline = true;
15926 cp_lexer_consume_token (parser->lexer);
15928 else
15929 is_inline = false;
15931 /* Look for the `namespace' keyword. */
15932 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15934 /* Get the name of the namespace. We do not attempt to distinguish
15935 between an original-namespace-definition and an
15936 extension-namespace-definition at this point. The semantic
15937 analysis routines are responsible for that. */
15938 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15939 identifier = cp_parser_identifier (parser);
15940 else
15941 identifier = NULL_TREE;
15943 /* Parse any specified attributes. */
15944 attribs = cp_parser_attributes_opt (parser);
15946 /* Look for the `{' to start the namespace. */
15947 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15948 /* Start the namespace. */
15949 push_namespace (identifier);
15951 /* "inline namespace" is equivalent to a stub namespace definition
15952 followed by a strong using directive. */
15953 if (is_inline)
15955 tree name_space = current_namespace;
15956 /* Set up namespace association. */
15957 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15958 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15959 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15960 /* Import the contents of the inline namespace. */
15961 pop_namespace ();
15962 do_using_directive (name_space);
15963 push_namespace (identifier);
15966 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15968 /* Parse the body of the namespace. */
15969 cp_parser_namespace_body (parser);
15971 if (has_visibility)
15972 pop_visibility (1);
15974 /* Finish the namespace. */
15975 pop_namespace ();
15976 /* Look for the final `}'. */
15977 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15980 /* Parse a namespace-body.
15982 namespace-body:
15983 declaration-seq [opt] */
15985 static void
15986 cp_parser_namespace_body (cp_parser* parser)
15988 cp_parser_declaration_seq_opt (parser);
15991 /* Parse a namespace-alias-definition.
15993 namespace-alias-definition:
15994 namespace identifier = qualified-namespace-specifier ; */
15996 static void
15997 cp_parser_namespace_alias_definition (cp_parser* parser)
15999 tree identifier;
16000 tree namespace_specifier;
16002 cp_token *token = cp_lexer_peek_token (parser->lexer);
16004 /* Look for the `namespace' keyword. */
16005 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16006 /* Look for the identifier. */
16007 identifier = cp_parser_identifier (parser);
16008 if (identifier == error_mark_node)
16009 return;
16010 /* Look for the `=' token. */
16011 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16012 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16014 error_at (token->location, "%<namespace%> definition is not allowed here");
16015 /* Skip the definition. */
16016 cp_lexer_consume_token (parser->lexer);
16017 if (cp_parser_skip_to_closing_brace (parser))
16018 cp_lexer_consume_token (parser->lexer);
16019 return;
16021 cp_parser_require (parser, CPP_EQ, RT_EQ);
16022 /* Look for the qualified-namespace-specifier. */
16023 namespace_specifier
16024 = cp_parser_qualified_namespace_specifier (parser);
16025 /* Look for the `;' token. */
16026 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16028 /* Register the alias in the symbol table. */
16029 do_namespace_alias (identifier, namespace_specifier);
16032 /* Parse a qualified-namespace-specifier.
16034 qualified-namespace-specifier:
16035 :: [opt] nested-name-specifier [opt] namespace-name
16037 Returns a NAMESPACE_DECL corresponding to the specified
16038 namespace. */
16040 static tree
16041 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16043 /* Look for the optional `::'. */
16044 cp_parser_global_scope_opt (parser,
16045 /*current_scope_valid_p=*/false);
16047 /* Look for the optional nested-name-specifier. */
16048 cp_parser_nested_name_specifier_opt (parser,
16049 /*typename_keyword_p=*/false,
16050 /*check_dependency_p=*/true,
16051 /*type_p=*/false,
16052 /*is_declaration=*/true);
16054 return cp_parser_namespace_name (parser);
16057 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16058 access declaration.
16060 using-declaration:
16061 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16062 using :: unqualified-id ;
16064 access-declaration:
16065 qualified-id ;
16069 static bool
16070 cp_parser_using_declaration (cp_parser* parser,
16071 bool access_declaration_p)
16073 cp_token *token;
16074 bool typename_p = false;
16075 bool global_scope_p;
16076 tree decl;
16077 tree identifier;
16078 tree qscope;
16079 int oldcount = errorcount;
16080 cp_token *diag_token = NULL;
16082 if (access_declaration_p)
16084 diag_token = cp_lexer_peek_token (parser->lexer);
16085 cp_parser_parse_tentatively (parser);
16087 else
16089 /* Look for the `using' keyword. */
16090 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16092 /* Peek at the next token. */
16093 token = cp_lexer_peek_token (parser->lexer);
16094 /* See if it's `typename'. */
16095 if (token->keyword == RID_TYPENAME)
16097 /* Remember that we've seen it. */
16098 typename_p = true;
16099 /* Consume the `typename' token. */
16100 cp_lexer_consume_token (parser->lexer);
16104 /* Look for the optional global scope qualification. */
16105 global_scope_p
16106 = (cp_parser_global_scope_opt (parser,
16107 /*current_scope_valid_p=*/false)
16108 != NULL_TREE);
16110 /* If we saw `typename', or didn't see `::', then there must be a
16111 nested-name-specifier present. */
16112 if (typename_p || !global_scope_p)
16113 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16114 /*check_dependency_p=*/true,
16115 /*type_p=*/false,
16116 /*is_declaration=*/true);
16117 /* Otherwise, we could be in either of the two productions. In that
16118 case, treat the nested-name-specifier as optional. */
16119 else
16120 qscope = cp_parser_nested_name_specifier_opt (parser,
16121 /*typename_keyword_p=*/false,
16122 /*check_dependency_p=*/true,
16123 /*type_p=*/false,
16124 /*is_declaration=*/true);
16125 if (!qscope)
16126 qscope = global_namespace;
16128 if (access_declaration_p && cp_parser_error_occurred (parser))
16129 /* Something has already gone wrong; there's no need to parse
16130 further. Since an error has occurred, the return value of
16131 cp_parser_parse_definitely will be false, as required. */
16132 return cp_parser_parse_definitely (parser);
16134 token = cp_lexer_peek_token (parser->lexer);
16135 /* Parse the unqualified-id. */
16136 identifier = cp_parser_unqualified_id (parser,
16137 /*template_keyword_p=*/false,
16138 /*check_dependency_p=*/true,
16139 /*declarator_p=*/true,
16140 /*optional_p=*/false);
16142 if (access_declaration_p)
16144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16145 cp_parser_simulate_error (parser);
16146 if (!cp_parser_parse_definitely (parser))
16147 return false;
16150 /* The function we call to handle a using-declaration is different
16151 depending on what scope we are in. */
16152 if (qscope == error_mark_node || identifier == error_mark_node)
16154 else if (!identifier_p (identifier)
16155 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16156 /* [namespace.udecl]
16158 A using declaration shall not name a template-id. */
16159 error_at (token->location,
16160 "a template-id may not appear in a using-declaration");
16161 else
16163 if (at_class_scope_p ())
16165 /* Create the USING_DECL. */
16166 decl = do_class_using_decl (parser->scope, identifier);
16168 if (decl && typename_p)
16169 USING_DECL_TYPENAME_P (decl) = 1;
16171 if (check_for_bare_parameter_packs (decl))
16172 return false;
16173 else
16174 /* Add it to the list of members in this class. */
16175 finish_member_declaration (decl);
16177 else
16179 decl = cp_parser_lookup_name_simple (parser,
16180 identifier,
16181 token->location);
16182 if (decl == error_mark_node)
16183 cp_parser_name_lookup_error (parser, identifier,
16184 decl, NLE_NULL,
16185 token->location);
16186 else if (check_for_bare_parameter_packs (decl))
16187 return false;
16188 else if (!at_namespace_scope_p ())
16189 do_local_using_decl (decl, qscope, identifier);
16190 else
16191 do_toplevel_using_decl (decl, qscope, identifier);
16195 /* Look for the final `;'. */
16196 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16198 if (access_declaration_p && errorcount == oldcount)
16199 warning_at (diag_token->location, OPT_Wdeprecated,
16200 "access declarations are deprecated "
16201 "in favour of using-declarations; "
16202 "suggestion: add the %<using%> keyword");
16204 return true;
16207 /* Parse an alias-declaration.
16209 alias-declaration:
16210 using identifier attribute-specifier-seq [opt] = type-id */
16212 static tree
16213 cp_parser_alias_declaration (cp_parser* parser)
16215 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16216 location_t id_location;
16217 cp_declarator *declarator;
16218 cp_decl_specifier_seq decl_specs;
16219 bool member_p;
16220 const char *saved_message = NULL;
16222 /* Look for the `using' keyword. */
16223 cp_token *using_token
16224 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16225 if (using_token == NULL)
16226 return error_mark_node;
16228 id_location = cp_lexer_peek_token (parser->lexer)->location;
16229 id = cp_parser_identifier (parser);
16230 if (id == error_mark_node)
16231 return error_mark_node;
16233 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16234 attributes = cp_parser_attributes_opt (parser);
16235 if (attributes == error_mark_node)
16236 return error_mark_node;
16238 cp_parser_require (parser, CPP_EQ, RT_EQ);
16240 if (cp_parser_error_occurred (parser))
16241 return error_mark_node;
16243 cp_parser_commit_to_tentative_parse (parser);
16245 /* Now we are going to parse the type-id of the declaration. */
16248 [dcl.type]/3 says:
16250 "A type-specifier-seq shall not define a class or enumeration
16251 unless it appears in the type-id of an alias-declaration (7.1.3) that
16252 is not the declaration of a template-declaration."
16254 In other words, if we currently are in an alias template, the
16255 type-id should not define a type.
16257 So let's set parser->type_definition_forbidden_message in that
16258 case; cp_parser_check_type_definition (called by
16259 cp_parser_class_specifier) will then emit an error if a type is
16260 defined in the type-id. */
16261 if (parser->num_template_parameter_lists)
16263 saved_message = parser->type_definition_forbidden_message;
16264 parser->type_definition_forbidden_message =
16265 G_("types may not be defined in alias template declarations");
16268 type = cp_parser_type_id (parser);
16270 /* Restore the error message if need be. */
16271 if (parser->num_template_parameter_lists)
16272 parser->type_definition_forbidden_message = saved_message;
16274 if (type == error_mark_node)
16276 cp_parser_skip_to_end_of_block_or_statement (parser);
16277 return error_mark_node;
16280 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16282 if (cp_parser_error_occurred (parser))
16284 cp_parser_skip_to_end_of_block_or_statement (parser);
16285 return error_mark_node;
16288 /* A typedef-name can also be introduced by an alias-declaration. The
16289 identifier following the using keyword becomes a typedef-name. It has
16290 the same semantics as if it were introduced by the typedef
16291 specifier. In particular, it does not define a new type and it shall
16292 not appear in the type-id. */
16294 clear_decl_specs (&decl_specs);
16295 decl_specs.type = type;
16296 if (attributes != NULL_TREE)
16298 decl_specs.attributes = attributes;
16299 set_and_check_decl_spec_loc (&decl_specs,
16300 ds_attribute,
16301 attrs_token);
16303 set_and_check_decl_spec_loc (&decl_specs,
16304 ds_typedef,
16305 using_token);
16306 set_and_check_decl_spec_loc (&decl_specs,
16307 ds_alias,
16308 using_token);
16310 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16311 declarator->id_loc = id_location;
16313 member_p = at_class_scope_p ();
16314 if (member_p)
16315 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16316 NULL_TREE, attributes);
16317 else
16318 decl = start_decl (declarator, &decl_specs, 0,
16319 attributes, NULL_TREE, &pushed_scope);
16320 if (decl == error_mark_node)
16321 return decl;
16323 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16325 if (pushed_scope)
16326 pop_scope (pushed_scope);
16328 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16329 added into the symbol table; otherwise, return the TYPE_DECL. */
16330 if (DECL_LANG_SPECIFIC (decl)
16331 && DECL_TEMPLATE_INFO (decl)
16332 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16334 decl = DECL_TI_TEMPLATE (decl);
16335 if (member_p)
16336 check_member_template (decl);
16339 return decl;
16342 /* Parse a using-directive.
16344 using-directive:
16345 using namespace :: [opt] nested-name-specifier [opt]
16346 namespace-name ; */
16348 static void
16349 cp_parser_using_directive (cp_parser* parser)
16351 tree namespace_decl;
16352 tree attribs;
16354 /* Look for the `using' keyword. */
16355 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16356 /* And the `namespace' keyword. */
16357 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16358 /* Look for the optional `::' operator. */
16359 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16360 /* And the optional nested-name-specifier. */
16361 cp_parser_nested_name_specifier_opt (parser,
16362 /*typename_keyword_p=*/false,
16363 /*check_dependency_p=*/true,
16364 /*type_p=*/false,
16365 /*is_declaration=*/true);
16366 /* Get the namespace being used. */
16367 namespace_decl = cp_parser_namespace_name (parser);
16368 /* And any specified attributes. */
16369 attribs = cp_parser_attributes_opt (parser);
16370 /* Update the symbol table. */
16371 parse_using_directive (namespace_decl, attribs);
16372 /* Look for the final `;'. */
16373 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16376 /* Parse an asm-definition.
16378 asm-definition:
16379 asm ( string-literal ) ;
16381 GNU Extension:
16383 asm-definition:
16384 asm volatile [opt] ( string-literal ) ;
16385 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16386 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16387 : asm-operand-list [opt] ) ;
16388 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16389 : asm-operand-list [opt]
16390 : asm-clobber-list [opt] ) ;
16391 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16392 : asm-clobber-list [opt]
16393 : asm-goto-list ) ; */
16395 static void
16396 cp_parser_asm_definition (cp_parser* parser)
16398 tree string;
16399 tree outputs = NULL_TREE;
16400 tree inputs = NULL_TREE;
16401 tree clobbers = NULL_TREE;
16402 tree labels = NULL_TREE;
16403 tree asm_stmt;
16404 bool volatile_p = false;
16405 bool extended_p = false;
16406 bool invalid_inputs_p = false;
16407 bool invalid_outputs_p = false;
16408 bool goto_p = false;
16409 required_token missing = RT_NONE;
16411 /* Look for the `asm' keyword. */
16412 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16413 /* See if the next token is `volatile'. */
16414 if (cp_parser_allow_gnu_extensions_p (parser)
16415 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16417 /* Remember that we saw the `volatile' keyword. */
16418 volatile_p = true;
16419 /* Consume the token. */
16420 cp_lexer_consume_token (parser->lexer);
16422 if (cp_parser_allow_gnu_extensions_p (parser)
16423 && parser->in_function_body
16424 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16426 /* Remember that we saw the `goto' keyword. */
16427 goto_p = true;
16428 /* Consume the token. */
16429 cp_lexer_consume_token (parser->lexer);
16431 /* Look for the opening `('. */
16432 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16433 return;
16434 /* Look for the string. */
16435 string = cp_parser_string_literal (parser, false, false);
16436 if (string == error_mark_node)
16438 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16439 /*consume_paren=*/true);
16440 return;
16443 /* If we're allowing GNU extensions, check for the extended assembly
16444 syntax. Unfortunately, the `:' tokens need not be separated by
16445 a space in C, and so, for compatibility, we tolerate that here
16446 too. Doing that means that we have to treat the `::' operator as
16447 two `:' tokens. */
16448 if (cp_parser_allow_gnu_extensions_p (parser)
16449 && parser->in_function_body
16450 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16451 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16453 bool inputs_p = false;
16454 bool clobbers_p = false;
16455 bool labels_p = false;
16457 /* The extended syntax was used. */
16458 extended_p = true;
16460 /* Look for outputs. */
16461 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16463 /* Consume the `:'. */
16464 cp_lexer_consume_token (parser->lexer);
16465 /* Parse the output-operands. */
16466 if (cp_lexer_next_token_is_not (parser->lexer,
16467 CPP_COLON)
16468 && cp_lexer_next_token_is_not (parser->lexer,
16469 CPP_SCOPE)
16470 && cp_lexer_next_token_is_not (parser->lexer,
16471 CPP_CLOSE_PAREN)
16472 && !goto_p)
16473 outputs = cp_parser_asm_operand_list (parser);
16475 if (outputs == error_mark_node)
16476 invalid_outputs_p = true;
16478 /* If the next token is `::', there are no outputs, and the
16479 next token is the beginning of the inputs. */
16480 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16481 /* The inputs are coming next. */
16482 inputs_p = true;
16484 /* Look for inputs. */
16485 if (inputs_p
16486 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16488 /* Consume the `:' or `::'. */
16489 cp_lexer_consume_token (parser->lexer);
16490 /* Parse the output-operands. */
16491 if (cp_lexer_next_token_is_not (parser->lexer,
16492 CPP_COLON)
16493 && cp_lexer_next_token_is_not (parser->lexer,
16494 CPP_SCOPE)
16495 && cp_lexer_next_token_is_not (parser->lexer,
16496 CPP_CLOSE_PAREN))
16497 inputs = cp_parser_asm_operand_list (parser);
16499 if (inputs == error_mark_node)
16500 invalid_inputs_p = true;
16502 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16503 /* The clobbers are coming next. */
16504 clobbers_p = true;
16506 /* Look for clobbers. */
16507 if (clobbers_p
16508 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16510 clobbers_p = true;
16511 /* Consume the `:' or `::'. */
16512 cp_lexer_consume_token (parser->lexer);
16513 /* Parse the clobbers. */
16514 if (cp_lexer_next_token_is_not (parser->lexer,
16515 CPP_COLON)
16516 && cp_lexer_next_token_is_not (parser->lexer,
16517 CPP_CLOSE_PAREN))
16518 clobbers = cp_parser_asm_clobber_list (parser);
16520 else if (goto_p
16521 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16522 /* The labels are coming next. */
16523 labels_p = true;
16525 /* Look for labels. */
16526 if (labels_p
16527 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16529 labels_p = true;
16530 /* Consume the `:' or `::'. */
16531 cp_lexer_consume_token (parser->lexer);
16532 /* Parse the labels. */
16533 labels = cp_parser_asm_label_list (parser);
16536 if (goto_p && !labels_p)
16537 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16539 else if (goto_p)
16540 missing = RT_COLON_SCOPE;
16542 /* Look for the closing `)'. */
16543 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16544 missing ? missing : RT_CLOSE_PAREN))
16545 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16546 /*consume_paren=*/true);
16547 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16549 if (!invalid_inputs_p && !invalid_outputs_p)
16551 /* Create the ASM_EXPR. */
16552 if (parser->in_function_body)
16554 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16555 inputs, clobbers, labels);
16556 /* If the extended syntax was not used, mark the ASM_EXPR. */
16557 if (!extended_p)
16559 tree temp = asm_stmt;
16560 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16561 temp = TREE_OPERAND (temp, 0);
16563 ASM_INPUT_P (temp) = 1;
16566 else
16567 add_asm_node (string);
16571 /* Declarators [gram.dcl.decl] */
16573 /* Parse an init-declarator.
16575 init-declarator:
16576 declarator initializer [opt]
16578 GNU Extension:
16580 init-declarator:
16581 declarator asm-specification [opt] attributes [opt] initializer [opt]
16583 function-definition:
16584 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16585 function-body
16586 decl-specifier-seq [opt] declarator function-try-block
16588 GNU Extension:
16590 function-definition:
16591 __extension__ function-definition
16593 TM Extension:
16595 function-definition:
16596 decl-specifier-seq [opt] declarator function-transaction-block
16598 The DECL_SPECIFIERS apply to this declarator. Returns a
16599 representation of the entity declared. If MEMBER_P is TRUE, then
16600 this declarator appears in a class scope. The new DECL created by
16601 this declarator is returned.
16603 The CHECKS are access checks that should be performed once we know
16604 what entity is being declared (and, therefore, what classes have
16605 befriended it).
16607 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16608 for a function-definition here as well. If the declarator is a
16609 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16610 be TRUE upon return. By that point, the function-definition will
16611 have been completely parsed.
16613 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16614 is FALSE.
16616 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16617 parsed declaration if it is an uninitialized single declarator not followed
16618 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16619 if present, will not be consumed. If returned, this declarator will be
16620 created with SD_INITIALIZED but will not call cp_finish_decl. */
16622 static tree
16623 cp_parser_init_declarator (cp_parser* parser,
16624 cp_decl_specifier_seq *decl_specifiers,
16625 vec<deferred_access_check, va_gc> *checks,
16626 bool function_definition_allowed_p,
16627 bool member_p,
16628 int declares_class_or_enum,
16629 bool* function_definition_p,
16630 tree* maybe_range_for_decl)
16632 cp_token *token = NULL, *asm_spec_start_token = NULL,
16633 *attributes_start_token = NULL;
16634 cp_declarator *declarator;
16635 tree prefix_attributes;
16636 tree attributes = NULL;
16637 tree asm_specification;
16638 tree initializer;
16639 tree decl = NULL_TREE;
16640 tree scope;
16641 int is_initialized;
16642 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16643 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16644 "(...)". */
16645 enum cpp_ttype initialization_kind;
16646 bool is_direct_init = false;
16647 bool is_non_constant_init;
16648 int ctor_dtor_or_conv_p;
16649 bool friend_p;
16650 tree pushed_scope = NULL_TREE;
16651 bool range_for_decl_p = false;
16652 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16654 /* Gather the attributes that were provided with the
16655 decl-specifiers. */
16656 prefix_attributes = decl_specifiers->attributes;
16658 /* Assume that this is not the declarator for a function
16659 definition. */
16660 if (function_definition_p)
16661 *function_definition_p = false;
16663 /* Default arguments are only permitted for function parameters. */
16664 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16665 parser->default_arg_ok_p = false;
16667 /* Defer access checks while parsing the declarator; we cannot know
16668 what names are accessible until we know what is being
16669 declared. */
16670 resume_deferring_access_checks ();
16672 /* Parse the declarator. */
16673 token = cp_lexer_peek_token (parser->lexer);
16674 declarator
16675 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16676 &ctor_dtor_or_conv_p,
16677 /*parenthesized_p=*/NULL,
16678 member_p);
16679 /* Gather up the deferred checks. */
16680 stop_deferring_access_checks ();
16682 parser->default_arg_ok_p = saved_default_arg_ok_p;
16684 /* If the DECLARATOR was erroneous, there's no need to go
16685 further. */
16686 if (declarator == cp_error_declarator)
16687 return error_mark_node;
16689 /* Check that the number of template-parameter-lists is OK. */
16690 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16691 token->location))
16692 return error_mark_node;
16694 if (declares_class_or_enum & 2)
16695 cp_parser_check_for_definition_in_return_type (declarator,
16696 decl_specifiers->type,
16697 decl_specifiers->locations[ds_type_spec]);
16699 /* Figure out what scope the entity declared by the DECLARATOR is
16700 located in. `grokdeclarator' sometimes changes the scope, so
16701 we compute it now. */
16702 scope = get_scope_of_declarator (declarator);
16704 /* Perform any lookups in the declared type which were thought to be
16705 dependent, but are not in the scope of the declarator. */
16706 decl_specifiers->type
16707 = maybe_update_decl_type (decl_specifiers->type, scope);
16709 /* If we're allowing GNU extensions, look for an
16710 asm-specification. */
16711 if (cp_parser_allow_gnu_extensions_p (parser))
16713 /* Look for an asm-specification. */
16714 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16715 asm_specification = cp_parser_asm_specification_opt (parser);
16717 else
16718 asm_specification = NULL_TREE;
16720 /* Look for attributes. */
16721 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16722 attributes = cp_parser_attributes_opt (parser);
16724 // Save off the current template constraints. These will apply
16725 // to the nested scope, not the declarator. Consider, an out-of-class
16726 // member definition:
16728 // template<typename T>
16729 // requires C<T>()
16730 // void S<T>::f() requires D<T>() { ... }
16732 // At the point we parse the 2nd requires clause, the previous the
16733 // current constraints will have been used to resolve the enclosing
16734 // class S<T>. The D<T>() requirement applies only to the definition
16735 // of f and do not include C<T>().
16736 tree saved_template_reqs = release (current_template_reqs);
16738 // Parse an optional requires clause. Currently, requirements can
16739 // be written for out-of-class member function definitions.
16741 // TODO: It may be better to always parse and diagnose the error
16742 // as a semantic one later on.
16743 if (flag_concepts && scope && function_declarator_p (declarator))
16745 if (tree r = cp_parser_requires_clause_opt (parser))
16746 current_template_reqs = finish_template_requirements (r);
16748 else
16749 current_template_reqs = saved_template_reqs;
16752 /* Peek at the next token. */
16753 token = cp_lexer_peek_token (parser->lexer);
16755 if (function_declarator_p (declarator))
16757 /* Check to see if the token indicates the start of a
16758 function-definition. */
16759 if (cp_parser_token_starts_function_definition_p (token))
16761 if (!function_definition_allowed_p)
16763 /* If a function-definition should not appear here, issue an
16764 error message. */
16765 cp_parser_error (parser,
16766 "a function-definition is not allowed here");
16767 return error_mark_node;
16770 location_t func_brace_location
16771 = cp_lexer_peek_token (parser->lexer)->location;
16773 /* Neither attributes nor an asm-specification are allowed
16774 on a function-definition. */
16775 if (asm_specification)
16776 error_at (asm_spec_start_token->location,
16777 "an asm-specification is not allowed "
16778 "on a function-definition");
16779 if (attributes)
16780 error_at (attributes_start_token->location,
16781 "attributes are not allowed "
16782 "on a function-definition");
16783 /* This is a function-definition. */
16784 *function_definition_p = true;
16786 /* Parse the function definition. */
16787 if (member_p)
16788 decl = cp_parser_save_member_function_body (parser,
16789 decl_specifiers,
16790 declarator,
16791 prefix_attributes);
16792 else
16793 decl =
16794 (cp_parser_function_definition_from_specifiers_and_declarator
16795 (parser, decl_specifiers, prefix_attributes, declarator));
16797 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16799 /* This is where the prologue starts... */
16800 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16801 = func_brace_location;
16804 // Restore the current requirements before returing.
16805 current_template_reqs = saved_template_reqs;
16807 return decl;
16811 /* [dcl.dcl]
16813 Only in function declarations for constructors, destructors, and
16814 type conversions can the decl-specifier-seq be omitted.
16816 We explicitly postpone this check past the point where we handle
16817 function-definitions because we tolerate function-definitions
16818 that are missing their return types in some modes. */
16819 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16821 cp_parser_error (parser,
16822 "expected constructor, destructor, or type conversion");
16823 return error_mark_node;
16826 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16827 if (token->type == CPP_EQ
16828 || token->type == CPP_OPEN_PAREN
16829 || token->type == CPP_OPEN_BRACE)
16831 is_initialized = SD_INITIALIZED;
16832 initialization_kind = token->type;
16833 if (maybe_range_for_decl)
16834 *maybe_range_for_decl = error_mark_node;
16836 if (token->type == CPP_EQ
16837 && function_declarator_p (declarator))
16839 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16840 if (t2->keyword == RID_DEFAULT)
16841 is_initialized = SD_DEFAULTED;
16842 else if (t2->keyword == RID_DELETE)
16843 is_initialized = SD_DELETED;
16846 else
16848 /* If the init-declarator isn't initialized and isn't followed by a
16849 `,' or `;', it's not a valid init-declarator. */
16850 if (token->type != CPP_COMMA
16851 && token->type != CPP_SEMICOLON)
16853 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16854 range_for_decl_p = true;
16855 else
16857 cp_parser_error (parser, "expected initializer");
16858 return error_mark_node;
16861 is_initialized = SD_UNINITIALIZED;
16862 initialization_kind = CPP_EOF;
16865 /* Because start_decl has side-effects, we should only call it if we
16866 know we're going ahead. By this point, we know that we cannot
16867 possibly be looking at any other construct. */
16868 cp_parser_commit_to_tentative_parse (parser);
16870 /* If the decl specifiers were bad, issue an error now that we're
16871 sure this was intended to be a declarator. Then continue
16872 declaring the variable(s), as int, to try to cut down on further
16873 errors. */
16874 if (decl_specifiers->any_specifiers_p
16875 && decl_specifiers->type == error_mark_node)
16877 cp_parser_error (parser, "invalid type in declaration");
16878 decl_specifiers->type = integer_type_node;
16881 /* Check to see whether or not this declaration is a friend. */
16882 friend_p = cp_parser_friend_p (decl_specifiers);
16884 /* Enter the newly declared entry in the symbol table. If we're
16885 processing a declaration in a class-specifier, we wait until
16886 after processing the initializer. */
16887 if (!member_p)
16889 if (parser->in_unbraced_linkage_specification_p)
16890 decl_specifiers->storage_class = sc_extern;
16891 decl = start_decl (declarator, decl_specifiers,
16892 range_for_decl_p? SD_INITIALIZED : is_initialized,
16893 attributes, prefix_attributes, &pushed_scope);
16894 cp_finalize_omp_declare_simd (parser, decl);
16895 /* Adjust location of decl if declarator->id_loc is more appropriate:
16896 set, and decl wasn't merged with another decl, in which case its
16897 location would be different from input_location, and more accurate. */
16898 if (DECL_P (decl)
16899 && declarator->id_loc != UNKNOWN_LOCATION
16900 && DECL_SOURCE_LOCATION (decl) == input_location)
16901 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16903 else if (scope)
16904 /* Enter the SCOPE. That way unqualified names appearing in the
16905 initializer will be looked up in SCOPE. */
16906 pushed_scope = push_scope (scope);
16908 /* Perform deferred access control checks, now that we know in which
16909 SCOPE the declared entity resides. */
16910 if (!member_p && decl)
16912 tree saved_current_function_decl = NULL_TREE;
16914 /* If the entity being declared is a function, pretend that we
16915 are in its scope. If it is a `friend', it may have access to
16916 things that would not otherwise be accessible. */
16917 if (TREE_CODE (decl) == FUNCTION_DECL)
16919 saved_current_function_decl = current_function_decl;
16920 current_function_decl = decl;
16923 /* Perform access checks for template parameters. */
16924 cp_parser_perform_template_parameter_access_checks (checks);
16926 /* Perform the access control checks for the declarator and the
16927 decl-specifiers. */
16928 perform_deferred_access_checks (tf_warning_or_error);
16930 /* Restore the saved value. */
16931 if (TREE_CODE (decl) == FUNCTION_DECL)
16932 current_function_decl = saved_current_function_decl;
16935 /* Parse the initializer. */
16936 initializer = NULL_TREE;
16937 is_direct_init = false;
16938 is_non_constant_init = true;
16939 if (is_initialized)
16941 if (function_declarator_p (declarator))
16943 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16944 if (initialization_kind == CPP_EQ)
16945 initializer = cp_parser_pure_specifier (parser);
16946 else
16948 /* If the declaration was erroneous, we don't really
16949 know what the user intended, so just silently
16950 consume the initializer. */
16951 if (decl != error_mark_node)
16952 error_at (initializer_start_token->location,
16953 "initializer provided for function");
16954 cp_parser_skip_to_closing_parenthesis (parser,
16955 /*recovering=*/true,
16956 /*or_comma=*/false,
16957 /*consume_paren=*/true);
16960 else
16962 /* We want to record the extra mangling scope for in-class
16963 initializers of class members and initializers of static data
16964 member templates. The former involves deferring
16965 parsing of the initializer until end of class as with default
16966 arguments. So right here we only handle the latter. */
16967 if (!member_p && processing_template_decl)
16968 start_lambda_scope (decl);
16969 initializer = cp_parser_initializer (parser,
16970 &is_direct_init,
16971 &is_non_constant_init);
16972 if (!member_p && processing_template_decl)
16973 finish_lambda_scope ();
16974 if (initializer == error_mark_node)
16975 cp_parser_skip_to_end_of_statement (parser);
16979 /* The old parser allows attributes to appear after a parenthesized
16980 initializer. Mark Mitchell proposed removing this functionality
16981 on the GCC mailing lists on 2002-08-13. This parser accepts the
16982 attributes -- but ignores them. */
16983 if (cp_parser_allow_gnu_extensions_p (parser)
16984 && initialization_kind == CPP_OPEN_PAREN)
16985 if (cp_parser_attributes_opt (parser))
16986 warning (OPT_Wattributes,
16987 "attributes after parenthesized initializer ignored");
16989 /* For an in-class declaration, use `grokfield' to create the
16990 declaration. */
16991 if (member_p)
16993 if (pushed_scope)
16995 pop_scope (pushed_scope);
16996 pushed_scope = NULL_TREE;
16998 decl = grokfield (declarator, decl_specifiers,
16999 initializer, !is_non_constant_init,
17000 /*asmspec=*/NULL_TREE,
17001 chainon (attributes, prefix_attributes));
17002 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17003 cp_parser_save_default_args (parser, decl);
17004 cp_finalize_omp_declare_simd (parser, decl);
17007 /* Finish processing the declaration. But, skip member
17008 declarations. */
17009 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17011 cp_finish_decl (decl,
17012 initializer, !is_non_constant_init,
17013 asm_specification,
17014 /* If the initializer is in parentheses, then this is
17015 a direct-initialization, which means that an
17016 `explicit' constructor is OK. Otherwise, an
17017 `explicit' constructor cannot be used. */
17018 ((is_direct_init || !is_initialized)
17019 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17021 else if ((cxx_dialect != cxx98) && friend_p
17022 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17023 /* Core issue #226 (C++0x only): A default template-argument
17024 shall not be specified in a friend class template
17025 declaration. */
17026 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17027 /*is_partial=*/false, /*is_friend_decl=*/1);
17029 if (!friend_p && pushed_scope)
17030 pop_scope (pushed_scope);
17032 if (function_declarator_p (declarator)
17033 && parser->fully_implicit_function_template_p)
17035 if (member_p)
17036 decl = finish_fully_implicit_template (parser, decl);
17037 else
17038 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17041 return decl;
17044 /* Parse a declarator.
17046 declarator:
17047 direct-declarator
17048 ptr-operator declarator
17050 abstract-declarator:
17051 ptr-operator abstract-declarator [opt]
17052 direct-abstract-declarator
17054 GNU Extensions:
17056 declarator:
17057 attributes [opt] direct-declarator
17058 attributes [opt] ptr-operator declarator
17060 abstract-declarator:
17061 attributes [opt] ptr-operator abstract-declarator [opt]
17062 attributes [opt] direct-abstract-declarator
17064 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17065 detect constructor, destructor or conversion operators. It is set
17066 to -1 if the declarator is a name, and +1 if it is a
17067 function. Otherwise it is set to zero. Usually you just want to
17068 test for >0, but internally the negative value is used.
17070 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17071 a decl-specifier-seq unless it declares a constructor, destructor,
17072 or conversion. It might seem that we could check this condition in
17073 semantic analysis, rather than parsing, but that makes it difficult
17074 to handle something like `f()'. We want to notice that there are
17075 no decl-specifiers, and therefore realize that this is an
17076 expression, not a declaration.)
17078 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17079 the declarator is a direct-declarator of the form "(...)".
17081 MEMBER_P is true iff this declarator is a member-declarator. */
17083 static cp_declarator *
17084 cp_parser_declarator (cp_parser* parser,
17085 cp_parser_declarator_kind dcl_kind,
17086 int* ctor_dtor_or_conv_p,
17087 bool* parenthesized_p,
17088 bool member_p)
17090 cp_declarator *declarator;
17091 enum tree_code code;
17092 cp_cv_quals cv_quals;
17093 tree class_type;
17094 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17096 /* Assume this is not a constructor, destructor, or type-conversion
17097 operator. */
17098 if (ctor_dtor_or_conv_p)
17099 *ctor_dtor_or_conv_p = 0;
17101 if (cp_parser_allow_gnu_extensions_p (parser))
17102 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17104 /* Check for the ptr-operator production. */
17105 cp_parser_parse_tentatively (parser);
17106 /* Parse the ptr-operator. */
17107 code = cp_parser_ptr_operator (parser,
17108 &class_type,
17109 &cv_quals,
17110 &std_attributes);
17112 /* If that worked, then we have a ptr-operator. */
17113 if (cp_parser_parse_definitely (parser))
17115 /* If a ptr-operator was found, then this declarator was not
17116 parenthesized. */
17117 if (parenthesized_p)
17118 *parenthesized_p = true;
17119 /* The dependent declarator is optional if we are parsing an
17120 abstract-declarator. */
17121 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17122 cp_parser_parse_tentatively (parser);
17124 /* Parse the dependent declarator. */
17125 declarator = cp_parser_declarator (parser, dcl_kind,
17126 /*ctor_dtor_or_conv_p=*/NULL,
17127 /*parenthesized_p=*/NULL,
17128 /*member_p=*/false);
17130 /* If we are parsing an abstract-declarator, we must handle the
17131 case where the dependent declarator is absent. */
17132 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17133 && !cp_parser_parse_definitely (parser))
17134 declarator = NULL;
17136 declarator = cp_parser_make_indirect_declarator
17137 (code, class_type, cv_quals, declarator, std_attributes);
17139 /* Everything else is a direct-declarator. */
17140 else
17142 if (parenthesized_p)
17143 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17144 CPP_OPEN_PAREN);
17145 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17146 ctor_dtor_or_conv_p,
17147 member_p);
17150 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17151 declarator->attributes = gnu_attributes;
17152 return declarator;
17155 /* Parse a direct-declarator or direct-abstract-declarator.
17157 direct-declarator:
17158 declarator-id
17159 direct-declarator ( parameter-declaration-clause )
17160 cv-qualifier-seq [opt]
17161 ref-qualifier [opt]
17162 exception-specification [opt]
17163 direct-declarator [ constant-expression [opt] ]
17164 ( declarator )
17166 direct-abstract-declarator:
17167 direct-abstract-declarator [opt]
17168 ( parameter-declaration-clause )
17169 cv-qualifier-seq [opt]
17170 ref-qualifier [opt]
17171 exception-specification [opt]
17172 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17173 ( abstract-declarator )
17175 Returns a representation of the declarator. DCL_KIND is
17176 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17177 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17178 we are parsing a direct-declarator. It is
17179 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17180 of ambiguity we prefer an abstract declarator, as per
17181 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17182 cp_parser_declarator. */
17184 static cp_declarator *
17185 cp_parser_direct_declarator (cp_parser* parser,
17186 cp_parser_declarator_kind dcl_kind,
17187 int* ctor_dtor_or_conv_p,
17188 bool member_p)
17190 cp_token *token;
17191 cp_declarator *declarator = NULL;
17192 tree scope = NULL_TREE;
17193 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17194 bool saved_in_declarator_p = parser->in_declarator_p;
17195 bool first = true;
17196 tree pushed_scope = NULL_TREE;
17198 while (true)
17200 /* Peek at the next token. */
17201 token = cp_lexer_peek_token (parser->lexer);
17202 if (token->type == CPP_OPEN_PAREN)
17204 /* This is either a parameter-declaration-clause, or a
17205 parenthesized declarator. When we know we are parsing a
17206 named declarator, it must be a parenthesized declarator
17207 if FIRST is true. For instance, `(int)' is a
17208 parameter-declaration-clause, with an omitted
17209 direct-abstract-declarator. But `((*))', is a
17210 parenthesized abstract declarator. Finally, when T is a
17211 template parameter `(T)' is a
17212 parameter-declaration-clause, and not a parenthesized
17213 named declarator.
17215 We first try and parse a parameter-declaration-clause,
17216 and then try a nested declarator (if FIRST is true).
17218 It is not an error for it not to be a
17219 parameter-declaration-clause, even when FIRST is
17220 false. Consider,
17222 int i (int);
17223 int i (3);
17225 The first is the declaration of a function while the
17226 second is the definition of a variable, including its
17227 initializer.
17229 Having seen only the parenthesis, we cannot know which of
17230 these two alternatives should be selected. Even more
17231 complex are examples like:
17233 int i (int (a));
17234 int i (int (3));
17236 The former is a function-declaration; the latter is a
17237 variable initialization.
17239 Thus again, we try a parameter-declaration-clause, and if
17240 that fails, we back out and return. */
17242 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17244 tree params;
17245 unsigned saved_num_template_parameter_lists;
17246 bool is_declarator = false;
17248 /* In a member-declarator, the only valid interpretation
17249 of a parenthesis is the start of a
17250 parameter-declaration-clause. (It is invalid to
17251 initialize a static data member with a parenthesized
17252 initializer; only the "=" form of initialization is
17253 permitted.) */
17254 if (!member_p)
17255 cp_parser_parse_tentatively (parser);
17257 /* Consume the `('. */
17258 cp_lexer_consume_token (parser->lexer);
17259 if (first)
17261 /* If this is going to be an abstract declarator, we're
17262 in a declarator and we can't have default args. */
17263 parser->default_arg_ok_p = false;
17264 parser->in_declarator_p = true;
17267 /* Inside the function parameter list, surrounding
17268 template-parameter-lists do not apply. */
17269 saved_num_template_parameter_lists
17270 = parser->num_template_parameter_lists;
17271 parser->num_template_parameter_lists = 0;
17273 begin_scope (sk_function_parms, NULL_TREE);
17275 /* Parse the parameter-declaration-clause. */
17276 params = cp_parser_parameter_declaration_clause (parser);
17278 /* Restore saved template parameter lists accounting for implicit
17279 template parameters. */
17280 parser->num_template_parameter_lists
17281 += saved_num_template_parameter_lists;
17283 /* Consume the `)'. */
17284 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17286 /* If all went well, parse the cv-qualifier-seq,
17287 ref-qualifier and the exception-specification. */
17288 if (member_p || cp_parser_parse_definitely (parser))
17290 cp_cv_quals cv_quals;
17291 cp_virt_specifiers virt_specifiers;
17292 cp_ref_qualifier ref_qual;
17293 tree exception_specification;
17294 tree late_return;
17295 tree attrs;
17296 bool memfn = (member_p || (pushed_scope
17297 && CLASS_TYPE_P (pushed_scope)));
17299 is_declarator = true;
17301 if (ctor_dtor_or_conv_p)
17302 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17303 first = false;
17305 /* Parse the cv-qualifier-seq. */
17306 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17307 /* Parse the ref-qualifier. */
17308 ref_qual = cp_parser_ref_qualifier_opt (parser);
17309 /* And the exception-specification. */
17310 exception_specification
17311 = cp_parser_exception_specification_opt (parser);
17313 attrs = cp_parser_std_attribute_spec_seq (parser);
17315 late_return = (cp_parser_late_return_type_opt
17316 (parser, declarator,
17317 memfn ? cv_quals : -1));
17320 /* Parse the virt-specifier-seq. */
17321 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17323 /* Create the function-declarator. */
17324 declarator = make_call_declarator (declarator,
17325 params,
17326 cv_quals,
17327 virt_specifiers,
17328 ref_qual,
17329 exception_specification,
17330 late_return);
17331 declarator->std_attributes = attrs;
17332 /* Any subsequent parameter lists are to do with
17333 return type, so are not those of the declared
17334 function. */
17335 parser->default_arg_ok_p = false;
17338 /* Remove the function parms from scope. */
17339 pop_bindings_and_leave_scope ();
17341 if (is_declarator)
17342 /* Repeat the main loop. */
17343 continue;
17346 /* If this is the first, we can try a parenthesized
17347 declarator. */
17348 if (first)
17350 bool saved_in_type_id_in_expr_p;
17352 parser->default_arg_ok_p = saved_default_arg_ok_p;
17353 parser->in_declarator_p = saved_in_declarator_p;
17355 /* Consume the `('. */
17356 cp_lexer_consume_token (parser->lexer);
17357 /* Parse the nested declarator. */
17358 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17359 parser->in_type_id_in_expr_p = true;
17360 declarator
17361 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17362 /*parenthesized_p=*/NULL,
17363 member_p);
17364 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17365 first = false;
17366 /* Expect a `)'. */
17367 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17368 declarator = cp_error_declarator;
17369 if (declarator == cp_error_declarator)
17370 break;
17372 goto handle_declarator;
17374 /* Otherwise, we must be done. */
17375 else
17376 break;
17378 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17379 && token->type == CPP_OPEN_SQUARE
17380 && !cp_next_tokens_can_be_attribute_p (parser))
17382 /* Parse an array-declarator. */
17383 tree bounds, attrs;
17385 if (ctor_dtor_or_conv_p)
17386 *ctor_dtor_or_conv_p = 0;
17388 first = false;
17389 parser->default_arg_ok_p = false;
17390 parser->in_declarator_p = true;
17391 /* Consume the `['. */
17392 cp_lexer_consume_token (parser->lexer);
17393 /* Peek at the next token. */
17394 token = cp_lexer_peek_token (parser->lexer);
17395 /* If the next token is `]', then there is no
17396 constant-expression. */
17397 if (token->type != CPP_CLOSE_SQUARE)
17399 bool non_constant_p;
17400 bounds
17401 = cp_parser_constant_expression (parser,
17402 /*allow_non_constant=*/true,
17403 &non_constant_p);
17404 if (!non_constant_p)
17405 /* OK */;
17406 else if (error_operand_p (bounds))
17407 /* Already gave an error. */;
17408 else if (!parser->in_function_body
17409 || current_binding_level->kind == sk_function_parms)
17411 /* Normally, the array bound must be an integral constant
17412 expression. However, as an extension, we allow VLAs
17413 in function scopes as long as they aren't part of a
17414 parameter declaration. */
17415 cp_parser_error (parser,
17416 "array bound is not an integer constant");
17417 bounds = error_mark_node;
17419 else if (processing_template_decl)
17421 /* Remember this wasn't a constant-expression. */
17422 bounds = build_nop (TREE_TYPE (bounds), bounds);
17423 TREE_SIDE_EFFECTS (bounds) = 1;
17426 else
17427 bounds = NULL_TREE;
17428 /* Look for the closing `]'. */
17429 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17431 declarator = cp_error_declarator;
17432 break;
17435 attrs = cp_parser_std_attribute_spec_seq (parser);
17436 declarator = make_array_declarator (declarator, bounds);
17437 declarator->std_attributes = attrs;
17439 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17442 tree qualifying_scope;
17443 tree unqualified_name;
17444 tree attrs;
17445 special_function_kind sfk;
17446 bool abstract_ok;
17447 bool pack_expansion_p = false;
17448 cp_token *declarator_id_start_token;
17450 /* Parse a declarator-id */
17451 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17452 if (abstract_ok)
17454 cp_parser_parse_tentatively (parser);
17456 /* If we see an ellipsis, we should be looking at a
17457 parameter pack. */
17458 if (token->type == CPP_ELLIPSIS)
17460 /* Consume the `...' */
17461 cp_lexer_consume_token (parser->lexer);
17463 pack_expansion_p = true;
17467 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17468 unqualified_name
17469 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17470 qualifying_scope = parser->scope;
17471 if (abstract_ok)
17473 bool okay = false;
17475 if (!unqualified_name && pack_expansion_p)
17477 /* Check whether an error occurred. */
17478 okay = !cp_parser_error_occurred (parser);
17480 /* We already consumed the ellipsis to mark a
17481 parameter pack, but we have no way to report it,
17482 so abort the tentative parse. We will be exiting
17483 immediately anyway. */
17484 cp_parser_abort_tentative_parse (parser);
17486 else
17487 okay = cp_parser_parse_definitely (parser);
17489 if (!okay)
17490 unqualified_name = error_mark_node;
17491 else if (unqualified_name
17492 && (qualifying_scope
17493 || (!identifier_p (unqualified_name))))
17495 cp_parser_error (parser, "expected unqualified-id");
17496 unqualified_name = error_mark_node;
17500 if (!unqualified_name)
17501 return NULL;
17502 if (unqualified_name == error_mark_node)
17504 declarator = cp_error_declarator;
17505 pack_expansion_p = false;
17506 declarator->parameter_pack_p = false;
17507 break;
17510 attrs = cp_parser_std_attribute_spec_seq (parser);
17512 if (qualifying_scope && at_namespace_scope_p ()
17513 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17515 /* In the declaration of a member of a template class
17516 outside of the class itself, the SCOPE will sometimes
17517 be a TYPENAME_TYPE. For example, given:
17519 template <typename T>
17520 int S<T>::R::i = 3;
17522 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17523 this context, we must resolve S<T>::R to an ordinary
17524 type, rather than a typename type.
17526 The reason we normally avoid resolving TYPENAME_TYPEs
17527 is that a specialization of `S' might render
17528 `S<T>::R' not a type. However, if `S' is
17529 specialized, then this `i' will not be used, so there
17530 is no harm in resolving the types here. */
17531 tree type;
17533 /* Resolve the TYPENAME_TYPE. */
17534 type = resolve_typename_type (qualifying_scope,
17535 /*only_current_p=*/false);
17536 /* If that failed, the declarator is invalid. */
17537 if (TREE_CODE (type) == TYPENAME_TYPE)
17539 if (typedef_variant_p (type))
17540 error_at (declarator_id_start_token->location,
17541 "cannot define member of dependent typedef "
17542 "%qT", type);
17543 else
17544 error_at (declarator_id_start_token->location,
17545 "%<%T::%E%> is not a type",
17546 TYPE_CONTEXT (qualifying_scope),
17547 TYPE_IDENTIFIER (qualifying_scope));
17549 qualifying_scope = type;
17552 sfk = sfk_none;
17554 if (unqualified_name)
17556 tree class_type;
17558 if (qualifying_scope
17559 && CLASS_TYPE_P (qualifying_scope))
17560 class_type = qualifying_scope;
17561 else
17562 class_type = current_class_type;
17564 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17566 tree name_type = TREE_TYPE (unqualified_name);
17567 if (class_type && same_type_p (name_type, class_type))
17569 if (qualifying_scope
17570 && CLASSTYPE_USE_TEMPLATE (name_type))
17572 error_at (declarator_id_start_token->location,
17573 "invalid use of constructor as a template");
17574 inform (declarator_id_start_token->location,
17575 "use %<%T::%D%> instead of %<%T::%D%> to "
17576 "name the constructor in a qualified name",
17577 class_type,
17578 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17579 class_type, name_type);
17580 declarator = cp_error_declarator;
17581 break;
17583 else
17584 unqualified_name = constructor_name (class_type);
17586 else
17588 /* We do not attempt to print the declarator
17589 here because we do not have enough
17590 information about its original syntactic
17591 form. */
17592 cp_parser_error (parser, "invalid declarator");
17593 declarator = cp_error_declarator;
17594 break;
17598 if (class_type)
17600 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17601 sfk = sfk_destructor;
17602 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17603 sfk = sfk_conversion;
17604 else if (/* There's no way to declare a constructor
17605 for an anonymous type, even if the type
17606 got a name for linkage purposes. */
17607 !TYPE_WAS_ANONYMOUS (class_type)
17608 && constructor_name_p (unqualified_name,
17609 class_type))
17611 unqualified_name = constructor_name (class_type);
17612 sfk = sfk_constructor;
17614 else if (is_overloaded_fn (unqualified_name)
17615 && DECL_CONSTRUCTOR_P (get_first_fn
17616 (unqualified_name)))
17617 sfk = sfk_constructor;
17619 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17620 *ctor_dtor_or_conv_p = -1;
17623 declarator = make_id_declarator (qualifying_scope,
17624 unqualified_name,
17625 sfk);
17626 declarator->std_attributes = attrs;
17627 declarator->id_loc = token->location;
17628 declarator->parameter_pack_p = pack_expansion_p;
17630 if (pack_expansion_p)
17631 maybe_warn_variadic_templates ();
17634 handle_declarator:;
17635 scope = get_scope_of_declarator (declarator);
17636 if (scope)
17638 /* Any names that appear after the declarator-id for a
17639 member are looked up in the containing scope. */
17640 if (at_function_scope_p ())
17642 /* But declarations with qualified-ids can't appear in a
17643 function. */
17644 cp_parser_error (parser, "qualified-id in declaration");
17645 break;
17647 pushed_scope = push_scope (scope);
17649 parser->in_declarator_p = true;
17650 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17651 || (declarator && declarator->kind == cdk_id))
17652 /* Default args are only allowed on function
17653 declarations. */
17654 parser->default_arg_ok_p = saved_default_arg_ok_p;
17655 else
17656 parser->default_arg_ok_p = false;
17658 first = false;
17660 /* We're done. */
17661 else
17662 break;
17665 /* For an abstract declarator, we might wind up with nothing at this
17666 point. That's an error; the declarator is not optional. */
17667 if (!declarator)
17668 cp_parser_error (parser, "expected declarator");
17670 /* If we entered a scope, we must exit it now. */
17671 if (pushed_scope)
17672 pop_scope (pushed_scope);
17674 parser->default_arg_ok_p = saved_default_arg_ok_p;
17675 parser->in_declarator_p = saved_in_declarator_p;
17677 return declarator;
17680 /* Parse a ptr-operator.
17682 ptr-operator:
17683 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17684 * cv-qualifier-seq [opt]
17686 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17687 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17689 GNU Extension:
17691 ptr-operator:
17692 & cv-qualifier-seq [opt]
17694 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17695 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17696 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17697 filled in with the TYPE containing the member. *CV_QUALS is
17698 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17699 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17700 Note that the tree codes returned by this function have nothing
17701 to do with the types of trees that will be eventually be created
17702 to represent the pointer or reference type being parsed. They are
17703 just constants with suggestive names. */
17704 static enum tree_code
17705 cp_parser_ptr_operator (cp_parser* parser,
17706 tree* type,
17707 cp_cv_quals *cv_quals,
17708 tree *attributes)
17710 enum tree_code code = ERROR_MARK;
17711 cp_token *token;
17712 tree attrs = NULL_TREE;
17714 /* Assume that it's not a pointer-to-member. */
17715 *type = NULL_TREE;
17716 /* And that there are no cv-qualifiers. */
17717 *cv_quals = TYPE_UNQUALIFIED;
17719 /* Peek at the next token. */
17720 token = cp_lexer_peek_token (parser->lexer);
17722 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17723 if (token->type == CPP_MULT)
17724 code = INDIRECT_REF;
17725 else if (token->type == CPP_AND)
17726 code = ADDR_EXPR;
17727 else if ((cxx_dialect != cxx98) &&
17728 token->type == CPP_AND_AND) /* C++0x only */
17729 code = NON_LVALUE_EXPR;
17731 if (code != ERROR_MARK)
17733 /* Consume the `*', `&' or `&&'. */
17734 cp_lexer_consume_token (parser->lexer);
17736 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17737 `&', if we are allowing GNU extensions. (The only qualifier
17738 that can legally appear after `&' is `restrict', but that is
17739 enforced during semantic analysis. */
17740 if (code == INDIRECT_REF
17741 || cp_parser_allow_gnu_extensions_p (parser))
17742 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17744 attrs = cp_parser_std_attribute_spec_seq (parser);
17745 if (attributes != NULL)
17746 *attributes = attrs;
17748 else
17750 /* Try the pointer-to-member case. */
17751 cp_parser_parse_tentatively (parser);
17752 /* Look for the optional `::' operator. */
17753 cp_parser_global_scope_opt (parser,
17754 /*current_scope_valid_p=*/false);
17755 /* Look for the nested-name specifier. */
17756 token = cp_lexer_peek_token (parser->lexer);
17757 cp_parser_nested_name_specifier (parser,
17758 /*typename_keyword_p=*/false,
17759 /*check_dependency_p=*/true,
17760 /*type_p=*/false,
17761 /*is_declaration=*/false);
17762 /* If we found it, and the next token is a `*', then we are
17763 indeed looking at a pointer-to-member operator. */
17764 if (!cp_parser_error_occurred (parser)
17765 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17767 /* Indicate that the `*' operator was used. */
17768 code = INDIRECT_REF;
17770 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17771 error_at (token->location, "%qD is a namespace", parser->scope);
17772 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17773 error_at (token->location, "cannot form pointer to member of "
17774 "non-class %q#T", parser->scope);
17775 else
17777 /* The type of which the member is a member is given by the
17778 current SCOPE. */
17779 *type = parser->scope;
17780 /* The next name will not be qualified. */
17781 parser->scope = NULL_TREE;
17782 parser->qualifying_scope = NULL_TREE;
17783 parser->object_scope = NULL_TREE;
17784 /* Look for optional c++11 attributes. */
17785 attrs = cp_parser_std_attribute_spec_seq (parser);
17786 if (attributes != NULL)
17787 *attributes = attrs;
17788 /* Look for the optional cv-qualifier-seq. */
17789 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17792 /* If that didn't work we don't have a ptr-operator. */
17793 if (!cp_parser_parse_definitely (parser))
17794 cp_parser_error (parser, "expected ptr-operator");
17797 return code;
17800 /* Parse an (optional) cv-qualifier-seq.
17802 cv-qualifier-seq:
17803 cv-qualifier cv-qualifier-seq [opt]
17805 cv-qualifier:
17806 const
17807 volatile
17809 GNU Extension:
17811 cv-qualifier:
17812 __restrict__
17814 Returns a bitmask representing the cv-qualifiers. */
17816 static cp_cv_quals
17817 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17819 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17821 while (true)
17823 cp_token *token;
17824 cp_cv_quals cv_qualifier;
17826 /* Peek at the next token. */
17827 token = cp_lexer_peek_token (parser->lexer);
17828 /* See if it's a cv-qualifier. */
17829 switch (token->keyword)
17831 case RID_CONST:
17832 cv_qualifier = TYPE_QUAL_CONST;
17833 break;
17835 case RID_VOLATILE:
17836 cv_qualifier = TYPE_QUAL_VOLATILE;
17837 break;
17839 case RID_RESTRICT:
17840 cv_qualifier = TYPE_QUAL_RESTRICT;
17841 break;
17843 default:
17844 cv_qualifier = TYPE_UNQUALIFIED;
17845 break;
17848 if (!cv_qualifier)
17849 break;
17851 if (cv_quals & cv_qualifier)
17853 error_at (token->location, "duplicate cv-qualifier");
17854 cp_lexer_purge_token (parser->lexer);
17856 else
17858 cp_lexer_consume_token (parser->lexer);
17859 cv_quals |= cv_qualifier;
17863 return cv_quals;
17866 /* Parse an (optional) ref-qualifier
17868 ref-qualifier:
17872 Returns cp_ref_qualifier representing ref-qualifier. */
17874 static cp_ref_qualifier
17875 cp_parser_ref_qualifier_opt (cp_parser* parser)
17877 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17879 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17880 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17881 return ref_qual;
17883 while (true)
17885 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17886 cp_token *token = cp_lexer_peek_token (parser->lexer);
17888 switch (token->type)
17890 case CPP_AND:
17891 curr_ref_qual = REF_QUAL_LVALUE;
17892 break;
17894 case CPP_AND_AND:
17895 curr_ref_qual = REF_QUAL_RVALUE;
17896 break;
17898 default:
17899 curr_ref_qual = REF_QUAL_NONE;
17900 break;
17903 if (!curr_ref_qual)
17904 break;
17905 else if (ref_qual)
17907 error_at (token->location, "multiple ref-qualifiers");
17908 cp_lexer_purge_token (parser->lexer);
17910 else
17912 ref_qual = curr_ref_qual;
17913 cp_lexer_consume_token (parser->lexer);
17917 return ref_qual;
17920 /* Parse an (optional) virt-specifier-seq.
17922 virt-specifier-seq:
17923 virt-specifier virt-specifier-seq [opt]
17925 virt-specifier:
17926 override
17927 final
17929 Returns a bitmask representing the virt-specifiers. */
17931 static cp_virt_specifiers
17932 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17934 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17936 while (true)
17938 cp_token *token;
17939 cp_virt_specifiers virt_specifier;
17941 /* Peek at the next token. */
17942 token = cp_lexer_peek_token (parser->lexer);
17943 /* See if it's a virt-specifier-qualifier. */
17944 if (token->type != CPP_NAME)
17945 break;
17946 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17948 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17949 virt_specifier = VIRT_SPEC_OVERRIDE;
17951 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17953 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17954 virt_specifier = VIRT_SPEC_FINAL;
17956 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17958 virt_specifier = VIRT_SPEC_FINAL;
17960 else
17961 break;
17963 if (virt_specifiers & virt_specifier)
17965 error_at (token->location, "duplicate virt-specifier");
17966 cp_lexer_purge_token (parser->lexer);
17968 else
17970 cp_lexer_consume_token (parser->lexer);
17971 virt_specifiers |= virt_specifier;
17974 return virt_specifiers;
17977 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17978 is in scope even though it isn't real. */
17980 static void
17981 inject_this_parameter (tree ctype, cp_cv_quals quals)
17983 tree this_parm;
17985 if (current_class_ptr)
17987 /* We don't clear this between NSDMIs. Is it already what we want? */
17988 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17989 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17990 && cp_type_quals (type) == quals)
17991 return;
17994 this_parm = build_this_parm (ctype, quals);
17995 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17996 current_class_ptr = NULL_TREE;
17997 current_class_ref
17998 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17999 current_class_ptr = this_parm;
18002 /* Return true iff our current scope is a non-static data member
18003 initializer. */
18005 bool
18006 parsing_nsdmi (void)
18008 /* We recognize NSDMI context by the context-less 'this' pointer set up
18009 by the function above. */
18010 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18011 return true;
18012 return false;
18015 /* Parse a late-specified return type, if any. This is not a separate
18016 non-terminal, but part of a function declarator, which looks like
18018 -> trailing-type-specifier-seq abstract-declarator(opt)
18020 Returns the type indicated by the type-id.
18022 In addition to this this parses any queued up omp declare simd
18023 clauses.
18025 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18026 function. */
18028 static tree
18029 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18030 cp_cv_quals quals)
18032 cp_token *token;
18033 tree type = NULL_TREE;
18034 bool declare_simd_p = (parser->omp_declare_simd
18035 && declarator
18036 && declarator->kind == cdk_id);
18038 /* Peek at the next token. */
18039 token = cp_lexer_peek_token (parser->lexer);
18040 /* A late-specified return type is indicated by an initial '->'. */
18041 if (token->type != CPP_DEREF && !declare_simd_p)
18042 return NULL_TREE;
18044 tree save_ccp = current_class_ptr;
18045 tree save_ccr = current_class_ref;
18046 if (quals >= 0)
18048 /* DR 1207: 'this' is in scope in the trailing return type. */
18049 inject_this_parameter (current_class_type, quals);
18052 if (token->type == CPP_DEREF)
18054 /* Consume the ->. */
18055 cp_lexer_consume_token (parser->lexer);
18057 type = cp_parser_trailing_type_id (parser);
18060 if (declare_simd_p)
18061 declarator->std_attributes
18062 = cp_parser_late_parsing_omp_declare_simd (parser,
18063 declarator->std_attributes);
18065 if (quals >= 0)
18067 current_class_ptr = save_ccp;
18068 current_class_ref = save_ccr;
18071 return type;
18074 /* Parse a declarator-id.
18076 declarator-id:
18077 id-expression
18078 :: [opt] nested-name-specifier [opt] type-name
18080 In the `id-expression' case, the value returned is as for
18081 cp_parser_id_expression if the id-expression was an unqualified-id.
18082 If the id-expression was a qualified-id, then a SCOPE_REF is
18083 returned. The first operand is the scope (either a NAMESPACE_DECL
18084 or TREE_TYPE), but the second is still just a representation of an
18085 unqualified-id. */
18087 static tree
18088 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18090 tree id;
18091 /* The expression must be an id-expression. Assume that qualified
18092 names are the names of types so that:
18094 template <class T>
18095 int S<T>::R::i = 3;
18097 will work; we must treat `S<T>::R' as the name of a type.
18098 Similarly, assume that qualified names are templates, where
18099 required, so that:
18101 template <class T>
18102 int S<T>::R<T>::i = 3;
18104 will work, too. */
18105 id = cp_parser_id_expression (parser,
18106 /*template_keyword_p=*/false,
18107 /*check_dependency_p=*/false,
18108 /*template_p=*/NULL,
18109 /*declarator_p=*/true,
18110 optional_p);
18111 if (id && BASELINK_P (id))
18112 id = BASELINK_FUNCTIONS (id);
18113 return id;
18116 /* Parse a type-id.
18118 type-id:
18119 type-specifier-seq abstract-declarator [opt]
18121 Returns the TYPE specified. */
18123 static tree
18124 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18125 bool is_trailing_return)
18127 cp_decl_specifier_seq type_specifier_seq;
18128 cp_declarator *abstract_declarator;
18130 /* Parse the type-specifier-seq. */
18131 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18132 is_trailing_return,
18133 &type_specifier_seq);
18134 if (type_specifier_seq.type == error_mark_node)
18135 return error_mark_node;
18137 /* There might or might not be an abstract declarator. */
18138 cp_parser_parse_tentatively (parser);
18139 /* Look for the declarator. */
18140 abstract_declarator
18141 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18142 /*parenthesized_p=*/NULL,
18143 /*member_p=*/false);
18144 /* Check to see if there really was a declarator. */
18145 if (!cp_parser_parse_definitely (parser))
18146 abstract_declarator = NULL;
18148 if (type_specifier_seq.type
18149 && cxx_dialect < cxx1y
18150 && type_uses_auto (type_specifier_seq.type))
18152 /* A type-id with type 'auto' is only ok if the abstract declarator
18153 is a function declarator with a late-specified return type. */
18154 if (abstract_declarator
18155 && abstract_declarator->kind == cdk_function
18156 && abstract_declarator->u.function.late_return_type)
18157 /* OK */;
18158 else
18160 error ("invalid use of %<auto%>");
18161 return error_mark_node;
18165 return groktypename (&type_specifier_seq, abstract_declarator,
18166 is_template_arg);
18169 static tree cp_parser_type_id (cp_parser *parser)
18171 return cp_parser_type_id_1 (parser, false, false);
18174 static tree cp_parser_template_type_arg (cp_parser *parser)
18176 tree r;
18177 const char *saved_message = parser->type_definition_forbidden_message;
18178 parser->type_definition_forbidden_message
18179 = G_("types may not be defined in template arguments");
18180 r = cp_parser_type_id_1 (parser, true, false);
18181 parser->type_definition_forbidden_message = saved_message;
18182 return r;
18185 static tree cp_parser_trailing_type_id (cp_parser *parser)
18187 return cp_parser_type_id_1 (parser, false, true);
18190 /* Parse a type-specifier-seq.
18192 type-specifier-seq:
18193 type-specifier type-specifier-seq [opt]
18195 GNU extension:
18197 type-specifier-seq:
18198 attributes type-specifier-seq [opt]
18200 If IS_DECLARATION is true, we are at the start of a "condition" or
18201 exception-declaration, so we might be followed by a declarator-id.
18203 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18204 i.e. we've just seen "->".
18206 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18208 static void
18209 cp_parser_type_specifier_seq (cp_parser* parser,
18210 bool is_declaration,
18211 bool is_trailing_return,
18212 cp_decl_specifier_seq *type_specifier_seq)
18214 bool seen_type_specifier = false;
18215 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18216 cp_token *start_token = NULL;
18218 /* Clear the TYPE_SPECIFIER_SEQ. */
18219 clear_decl_specs (type_specifier_seq);
18221 /* In the context of a trailing return type, enum E { } is an
18222 elaborated-type-specifier followed by a function-body, not an
18223 enum-specifier. */
18224 if (is_trailing_return)
18225 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18227 /* Parse the type-specifiers and attributes. */
18228 while (true)
18230 tree type_specifier;
18231 bool is_cv_qualifier;
18233 /* Check for attributes first. */
18234 if (cp_next_tokens_can_be_attribute_p (parser))
18236 type_specifier_seq->attributes =
18237 chainon (type_specifier_seq->attributes,
18238 cp_parser_attributes_opt (parser));
18239 continue;
18242 /* record the token of the beginning of the type specifier seq,
18243 for error reporting purposes*/
18244 if (!start_token)
18245 start_token = cp_lexer_peek_token (parser->lexer);
18247 /* Look for the type-specifier. */
18248 type_specifier = cp_parser_type_specifier (parser,
18249 flags,
18250 type_specifier_seq,
18251 /*is_declaration=*/false,
18252 NULL,
18253 &is_cv_qualifier);
18254 if (!type_specifier)
18256 /* If the first type-specifier could not be found, this is not a
18257 type-specifier-seq at all. */
18258 if (!seen_type_specifier)
18260 cp_parser_error (parser, "expected type-specifier");
18261 type_specifier_seq->type = error_mark_node;
18262 return;
18264 /* If subsequent type-specifiers could not be found, the
18265 type-specifier-seq is complete. */
18266 break;
18269 seen_type_specifier = true;
18270 /* The standard says that a condition can be:
18272 type-specifier-seq declarator = assignment-expression
18274 However, given:
18276 struct S {};
18277 if (int S = ...)
18279 we should treat the "S" as a declarator, not as a
18280 type-specifier. The standard doesn't say that explicitly for
18281 type-specifier-seq, but it does say that for
18282 decl-specifier-seq in an ordinary declaration. Perhaps it
18283 would be clearer just to allow a decl-specifier-seq here, and
18284 then add a semantic restriction that if any decl-specifiers
18285 that are not type-specifiers appear, the program is invalid. */
18286 if (is_declaration && !is_cv_qualifier)
18287 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18291 /* Parse a parameter-declaration-clause.
18293 parameter-declaration-clause:
18294 parameter-declaration-list [opt] ... [opt]
18295 parameter-declaration-list , ...
18297 Returns a representation for the parameter declarations. A return
18298 value of NULL indicates a parameter-declaration-clause consisting
18299 only of an ellipsis. */
18301 static tree
18302 cp_parser_parameter_declaration_clause (cp_parser* parser)
18304 tree parameters;
18305 cp_token *token;
18306 bool ellipsis_p;
18307 bool is_error;
18309 struct cleanup {
18310 cp_parser* parser;
18311 int auto_is_implicit_function_template_parm_p;
18312 ~cleanup() {
18313 parser->auto_is_implicit_function_template_parm_p
18314 = auto_is_implicit_function_template_parm_p;
18316 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18318 (void) cleanup;
18320 if (!processing_specialization)
18321 parser->auto_is_implicit_function_template_parm_p = true;
18323 /* Peek at the next token. */
18324 token = cp_lexer_peek_token (parser->lexer);
18325 /* Check for trivial parameter-declaration-clauses. */
18326 if (token->type == CPP_ELLIPSIS)
18328 /* Consume the `...' token. */
18329 cp_lexer_consume_token (parser->lexer);
18330 return NULL_TREE;
18332 else if (token->type == CPP_CLOSE_PAREN)
18333 /* There are no parameters. */
18335 #ifndef NO_IMPLICIT_EXTERN_C
18336 if (in_system_header && current_class_type == NULL
18337 && current_lang_name == lang_name_c)
18338 return NULL_TREE;
18339 else
18340 #endif
18341 return void_list_node;
18343 /* Check for `(void)', too, which is a special case. */
18344 else if (token->keyword == RID_VOID
18345 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18346 == CPP_CLOSE_PAREN))
18348 /* Consume the `void' token. */
18349 cp_lexer_consume_token (parser->lexer);
18350 /* There are no parameters. */
18351 return void_list_node;
18354 /* Parse the parameter-declaration-list. */
18355 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18356 /* If a parse error occurred while parsing the
18357 parameter-declaration-list, then the entire
18358 parameter-declaration-clause is erroneous. */
18359 if (is_error)
18360 return NULL;
18362 /* Peek at the next token. */
18363 token = cp_lexer_peek_token (parser->lexer);
18364 /* If it's a `,', the clause should terminate with an ellipsis. */
18365 if (token->type == CPP_COMMA)
18367 /* Consume the `,'. */
18368 cp_lexer_consume_token (parser->lexer);
18369 /* Expect an ellipsis. */
18370 ellipsis_p
18371 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18373 /* It might also be `...' if the optional trailing `,' was
18374 omitted. */
18375 else if (token->type == CPP_ELLIPSIS)
18377 /* Consume the `...' token. */
18378 cp_lexer_consume_token (parser->lexer);
18379 /* And remember that we saw it. */
18380 ellipsis_p = true;
18382 else
18383 ellipsis_p = false;
18385 /* Finish the parameter list. */
18386 if (!ellipsis_p)
18387 parameters = chainon (parameters, void_list_node);
18389 return parameters;
18392 /* Parse a parameter-declaration-list.
18394 parameter-declaration-list:
18395 parameter-declaration
18396 parameter-declaration-list , parameter-declaration
18398 Returns a representation of the parameter-declaration-list, as for
18399 cp_parser_parameter_declaration_clause. However, the
18400 `void_list_node' is never appended to the list. Upon return,
18401 *IS_ERROR will be true iff an error occurred. */
18403 static tree
18404 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18406 tree parameters = NULL_TREE;
18407 tree *tail = &parameters;
18408 bool saved_in_unbraced_linkage_specification_p;
18409 int index = 0;
18411 /* Assume all will go well. */
18412 *is_error = false;
18413 /* The special considerations that apply to a function within an
18414 unbraced linkage specifications do not apply to the parameters
18415 to the function. */
18416 saved_in_unbraced_linkage_specification_p
18417 = parser->in_unbraced_linkage_specification_p;
18418 parser->in_unbraced_linkage_specification_p = false;
18420 /* Look for more parameters. */
18421 while (true)
18423 cp_parameter_declarator *parameter;
18424 tree decl = error_mark_node;
18425 bool parenthesized_p = false;
18426 int template_parm_idx = (parser->num_template_parameter_lists?
18427 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18428 (current_template_parms)) : 0);
18430 /* Parse the parameter. */
18431 parameter
18432 = cp_parser_parameter_declaration (parser,
18433 /*template_parm_p=*/false,
18434 &parenthesized_p);
18436 /* We don't know yet if the enclosing context is deprecated, so wait
18437 and warn in grokparms if appropriate. */
18438 deprecated_state = DEPRECATED_SUPPRESS;
18440 if (parameter)
18442 /* If a function parameter pack was specified and an implicit template
18443 parameter was introduced during cp_parser_parameter_declaration,
18444 change any implicit parameters introduced into packs. */
18445 if (parser->implicit_template_parms
18446 && parameter->declarator
18447 && parameter->declarator->parameter_pack_p)
18449 int latest_template_parm_idx = TREE_VEC_LENGTH
18450 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18452 if (latest_template_parm_idx != template_parm_idx)
18453 parameter->decl_specifiers.type = convert_generic_types_to_packs
18454 (parameter->decl_specifiers.type,
18455 template_parm_idx, latest_template_parm_idx);
18458 decl = grokdeclarator (parameter->declarator,
18459 &parameter->decl_specifiers,
18460 PARM,
18461 parameter->default_argument != NULL_TREE,
18462 &parameter->decl_specifiers.attributes);
18465 deprecated_state = DEPRECATED_NORMAL;
18467 /* If a parse error occurred parsing the parameter declaration,
18468 then the entire parameter-declaration-list is erroneous. */
18469 if (decl == error_mark_node)
18471 *is_error = true;
18472 parameters = error_mark_node;
18473 break;
18476 if (parameter->decl_specifiers.attributes)
18477 cplus_decl_attributes (&decl,
18478 parameter->decl_specifiers.attributes,
18480 if (DECL_NAME (decl))
18481 decl = pushdecl (decl);
18483 if (decl != error_mark_node)
18485 retrofit_lang_decl (decl);
18486 DECL_PARM_INDEX (decl) = ++index;
18487 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18490 /* Add the new parameter to the list. */
18491 *tail = build_tree_list (parameter->default_argument, decl);
18492 tail = &TREE_CHAIN (*tail);
18494 /* Peek at the next token. */
18495 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18496 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18497 /* These are for Objective-C++ */
18498 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18499 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18500 /* The parameter-declaration-list is complete. */
18501 break;
18502 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18504 cp_token *token;
18506 /* Peek at the next token. */
18507 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18508 /* If it's an ellipsis, then the list is complete. */
18509 if (token->type == CPP_ELLIPSIS)
18510 break;
18511 /* Otherwise, there must be more parameters. Consume the
18512 `,'. */
18513 cp_lexer_consume_token (parser->lexer);
18514 /* When parsing something like:
18516 int i(float f, double d)
18518 we can tell after seeing the declaration for "f" that we
18519 are not looking at an initialization of a variable "i",
18520 but rather at the declaration of a function "i".
18522 Due to the fact that the parsing of template arguments
18523 (as specified to a template-id) requires backtracking we
18524 cannot use this technique when inside a template argument
18525 list. */
18526 if (!parser->in_template_argument_list_p
18527 && !parser->in_type_id_in_expr_p
18528 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18529 /* However, a parameter-declaration of the form
18530 "float(f)" (which is a valid declaration of a
18531 parameter "f") can also be interpreted as an
18532 expression (the conversion of "f" to "float"). */
18533 && !parenthesized_p)
18534 cp_parser_commit_to_tentative_parse (parser);
18536 else
18538 cp_parser_error (parser, "expected %<,%> or %<...%>");
18539 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18540 cp_parser_skip_to_closing_parenthesis (parser,
18541 /*recovering=*/true,
18542 /*or_comma=*/false,
18543 /*consume_paren=*/false);
18544 break;
18548 parser->in_unbraced_linkage_specification_p
18549 = saved_in_unbraced_linkage_specification_p;
18551 if (cp_binding_level *its = parser->implicit_template_scope)
18552 if (current_binding_level->level_chain == its)
18554 parser->implicit_template_parms = 0;
18555 parser->implicit_template_scope = 0;
18558 return parameters;
18561 /* Parse a parameter declaration.
18563 parameter-declaration:
18564 decl-specifier-seq ... [opt] declarator
18565 decl-specifier-seq declarator = assignment-expression
18566 decl-specifier-seq ... [opt] abstract-declarator [opt]
18567 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18569 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18570 declares a template parameter. (In that case, a non-nested `>'
18571 token encountered during the parsing of the assignment-expression
18572 is not interpreted as a greater-than operator.)
18574 Returns a representation of the parameter, or NULL if an error
18575 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18576 true iff the declarator is of the form "(p)". */
18578 static cp_parameter_declarator *
18579 cp_parser_parameter_declaration (cp_parser *parser,
18580 bool template_parm_p,
18581 bool *parenthesized_p)
18583 int declares_class_or_enum;
18584 cp_decl_specifier_seq decl_specifiers;
18585 cp_declarator *declarator;
18586 tree default_argument;
18587 cp_token *token = NULL, *declarator_token_start = NULL;
18588 const char *saved_message;
18590 /* In a template parameter, `>' is not an operator.
18592 [temp.param]
18594 When parsing a default template-argument for a non-type
18595 template-parameter, the first non-nested `>' is taken as the end
18596 of the template parameter-list rather than a greater-than
18597 operator. */
18599 /* Type definitions may not appear in parameter types. */
18600 saved_message = parser->type_definition_forbidden_message;
18601 parser->type_definition_forbidden_message
18602 = G_("types may not be defined in parameter types");
18604 /* Parse the declaration-specifiers. */
18605 cp_parser_decl_specifier_seq (parser,
18606 CP_PARSER_FLAGS_NONE,
18607 &decl_specifiers,
18608 &declares_class_or_enum);
18610 /* Complain about missing 'typename' or other invalid type names. */
18611 if (!decl_specifiers.any_type_specifiers_p
18612 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18613 decl_specifiers.type = error_mark_node;
18615 /* If an error occurred, there's no reason to attempt to parse the
18616 rest of the declaration. */
18617 if (cp_parser_error_occurred (parser))
18619 parser->type_definition_forbidden_message = saved_message;
18620 return NULL;
18623 /* Peek at the next token. */
18624 token = cp_lexer_peek_token (parser->lexer);
18626 /* If the next token is a `)', `,', `=', `>', or `...', then there
18627 is no declarator. However, when variadic templates are enabled,
18628 there may be a declarator following `...'. */
18629 if (token->type == CPP_CLOSE_PAREN
18630 || token->type == CPP_COMMA
18631 || token->type == CPP_EQ
18632 || token->type == CPP_GREATER)
18634 declarator = NULL;
18635 if (parenthesized_p)
18636 *parenthesized_p = false;
18638 /* Otherwise, there should be a declarator. */
18639 else
18641 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18642 parser->default_arg_ok_p = false;
18644 /* After seeing a decl-specifier-seq, if the next token is not a
18645 "(", there is no possibility that the code is a valid
18646 expression. Therefore, if parsing tentatively, we commit at
18647 this point. */
18648 if (!parser->in_template_argument_list_p
18649 /* In an expression context, having seen:
18651 (int((char ...
18653 we cannot be sure whether we are looking at a
18654 function-type (taking a "char" as a parameter) or a cast
18655 of some object of type "char" to "int". */
18656 && !parser->in_type_id_in_expr_p
18657 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18658 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18659 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18660 cp_parser_commit_to_tentative_parse (parser);
18661 /* Parse the declarator. */
18662 declarator_token_start = token;
18663 declarator = cp_parser_declarator (parser,
18664 CP_PARSER_DECLARATOR_EITHER,
18665 /*ctor_dtor_or_conv_p=*/NULL,
18666 parenthesized_p,
18667 /*member_p=*/false);
18668 parser->default_arg_ok_p = saved_default_arg_ok_p;
18669 /* After the declarator, allow more attributes. */
18670 decl_specifiers.attributes
18671 = chainon (decl_specifiers.attributes,
18672 cp_parser_attributes_opt (parser));
18675 /* If the next token is an ellipsis, and we have not seen a
18676 declarator name, and the type of the declarator contains parameter
18677 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18678 a parameter pack expansion expression. Otherwise, leave the
18679 ellipsis for a C-style variadic function. */
18680 token = cp_lexer_peek_token (parser->lexer);
18681 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18683 tree type = decl_specifiers.type;
18685 if (type && DECL_P (type))
18686 type = TREE_TYPE (type);
18688 if (type
18689 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18690 && declarator_can_be_parameter_pack (declarator)
18691 && (!declarator || !declarator->parameter_pack_p)
18692 && uses_parameter_packs (type))
18694 /* Consume the `...'. */
18695 cp_lexer_consume_token (parser->lexer);
18696 maybe_warn_variadic_templates ();
18698 /* Build a pack expansion type */
18699 if (declarator)
18700 declarator->parameter_pack_p = true;
18701 else
18702 decl_specifiers.type = make_pack_expansion (type);
18706 /* The restriction on defining new types applies only to the type
18707 of the parameter, not to the default argument. */
18708 parser->type_definition_forbidden_message = saved_message;
18710 /* If the next token is `=', then process a default argument. */
18711 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18713 token = cp_lexer_peek_token (parser->lexer);
18714 /* If we are defining a class, then the tokens that make up the
18715 default argument must be saved and processed later. */
18716 if (!template_parm_p && at_class_scope_p ()
18717 && TYPE_BEING_DEFINED (current_class_type)
18718 && !LAMBDA_TYPE_P (current_class_type))
18719 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18720 /* Outside of a class definition, we can just parse the
18721 assignment-expression. */
18722 else
18723 default_argument
18724 = cp_parser_default_argument (parser, template_parm_p);
18726 if (!parser->default_arg_ok_p)
18728 if (flag_permissive)
18729 warning (0, "deprecated use of default argument for parameter of non-function");
18730 else
18732 error_at (token->location,
18733 "default arguments are only "
18734 "permitted for function parameters");
18735 default_argument = NULL_TREE;
18738 else if ((declarator && declarator->parameter_pack_p)
18739 || (decl_specifiers.type
18740 && PACK_EXPANSION_P (decl_specifiers.type)))
18742 /* Find the name of the parameter pack. */
18743 cp_declarator *id_declarator = declarator;
18744 while (id_declarator && id_declarator->kind != cdk_id)
18745 id_declarator = id_declarator->declarator;
18747 if (id_declarator && id_declarator->kind == cdk_id)
18748 error_at (declarator_token_start->location,
18749 template_parm_p
18750 ? G_("template parameter pack %qD "
18751 "cannot have a default argument")
18752 : G_("parameter pack %qD cannot have "
18753 "a default argument"),
18754 id_declarator->u.id.unqualified_name);
18755 else
18756 error_at (declarator_token_start->location,
18757 template_parm_p
18758 ? G_("template parameter pack cannot have "
18759 "a default argument")
18760 : G_("parameter pack cannot have a "
18761 "default argument"));
18763 default_argument = NULL_TREE;
18766 else
18767 default_argument = NULL_TREE;
18769 return make_parameter_declarator (&decl_specifiers,
18770 declarator,
18771 default_argument);
18774 /* Parse a default argument and return it.
18776 TEMPLATE_PARM_P is true if this is a default argument for a
18777 non-type template parameter. */
18778 static tree
18779 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18781 tree default_argument = NULL_TREE;
18782 bool saved_greater_than_is_operator_p;
18783 bool saved_local_variables_forbidden_p;
18784 bool non_constant_p, is_direct_init;
18786 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18787 set correctly. */
18788 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18789 parser->greater_than_is_operator_p = !template_parm_p;
18790 /* Local variable names (and the `this' keyword) may not
18791 appear in a default argument. */
18792 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18793 parser->local_variables_forbidden_p = true;
18794 /* Parse the assignment-expression. */
18795 if (template_parm_p)
18796 push_deferring_access_checks (dk_no_deferred);
18797 default_argument
18798 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18799 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18800 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18801 if (template_parm_p)
18802 pop_deferring_access_checks ();
18803 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18804 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18806 return default_argument;
18809 /* Parse a function-body.
18811 function-body:
18812 compound_statement */
18814 static void
18815 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18817 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18820 /* Parse a ctor-initializer-opt followed by a function-body. Return
18821 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18822 is true we are parsing a function-try-block. */
18824 static bool
18825 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18826 bool in_function_try_block)
18828 tree body, list;
18829 bool ctor_initializer_p;
18830 const bool check_body_p =
18831 DECL_CONSTRUCTOR_P (current_function_decl)
18832 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18833 tree last = NULL;
18835 /* Begin the function body. */
18836 body = begin_function_body ();
18837 /* Parse the optional ctor-initializer. */
18838 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18840 /* If we're parsing a constexpr constructor definition, we need
18841 to check that the constructor body is indeed empty. However,
18842 before we get to cp_parser_function_body lot of junk has been
18843 generated, so we can't just check that we have an empty block.
18844 Rather we take a snapshot of the outermost block, and check whether
18845 cp_parser_function_body changed its state. */
18846 if (check_body_p)
18848 list = cur_stmt_list;
18849 if (STATEMENT_LIST_TAIL (list))
18850 last = STATEMENT_LIST_TAIL (list)->stmt;
18852 /* Parse the function-body. */
18853 cp_parser_function_body (parser, in_function_try_block);
18854 if (check_body_p)
18855 check_constexpr_ctor_body (last, list);
18856 /* Finish the function body. */
18857 finish_function_body (body);
18859 return ctor_initializer_p;
18862 /* Parse an initializer.
18864 initializer:
18865 = initializer-clause
18866 ( expression-list )
18868 Returns an expression representing the initializer. If no
18869 initializer is present, NULL_TREE is returned.
18871 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18872 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18873 set to TRUE if there is no initializer present. If there is an
18874 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18875 is set to true; otherwise it is set to false. */
18877 static tree
18878 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18879 bool* non_constant_p)
18881 cp_token *token;
18882 tree init;
18884 /* Peek at the next token. */
18885 token = cp_lexer_peek_token (parser->lexer);
18887 /* Let our caller know whether or not this initializer was
18888 parenthesized. */
18889 *is_direct_init = (token->type != CPP_EQ);
18890 /* Assume that the initializer is constant. */
18891 *non_constant_p = false;
18893 if (token->type == CPP_EQ)
18895 /* Consume the `='. */
18896 cp_lexer_consume_token (parser->lexer);
18897 /* Parse the initializer-clause. */
18898 init = cp_parser_initializer_clause (parser, non_constant_p);
18900 else if (token->type == CPP_OPEN_PAREN)
18902 vec<tree, va_gc> *vec;
18903 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18904 /*cast_p=*/false,
18905 /*allow_expansion_p=*/true,
18906 non_constant_p);
18907 if (vec == NULL)
18908 return error_mark_node;
18909 init = build_tree_list_vec (vec);
18910 release_tree_vector (vec);
18912 else if (token->type == CPP_OPEN_BRACE)
18914 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18915 init = cp_parser_braced_list (parser, non_constant_p);
18916 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18918 else
18920 /* Anything else is an error. */
18921 cp_parser_error (parser, "expected initializer");
18922 init = error_mark_node;
18925 return init;
18928 /* Parse an initializer-clause.
18930 initializer-clause:
18931 assignment-expression
18932 braced-init-list
18934 Returns an expression representing the initializer.
18936 If the `assignment-expression' production is used the value
18937 returned is simply a representation for the expression.
18939 Otherwise, calls cp_parser_braced_list. */
18941 static tree
18942 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18944 tree initializer;
18946 /* Assume the expression is constant. */
18947 *non_constant_p = false;
18949 /* If it is not a `{', then we are looking at an
18950 assignment-expression. */
18951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18953 initializer
18954 = cp_parser_constant_expression (parser,
18955 /*allow_non_constant_p=*/true,
18956 non_constant_p);
18958 else
18959 initializer = cp_parser_braced_list (parser, non_constant_p);
18961 return initializer;
18964 /* Parse a brace-enclosed initializer list.
18966 braced-init-list:
18967 { initializer-list , [opt] }
18970 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18971 the elements of the initializer-list (or NULL, if the last
18972 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18973 NULL_TREE. There is no way to detect whether or not the optional
18974 trailing `,' was provided. NON_CONSTANT_P is as for
18975 cp_parser_initializer. */
18977 static tree
18978 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18980 tree initializer;
18982 /* Consume the `{' token. */
18983 cp_lexer_consume_token (parser->lexer);
18984 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18985 initializer = make_node (CONSTRUCTOR);
18986 /* If it's not a `}', then there is a non-trivial initializer. */
18987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18989 /* Parse the initializer list. */
18990 CONSTRUCTOR_ELTS (initializer)
18991 = cp_parser_initializer_list (parser, non_constant_p);
18992 /* A trailing `,' token is allowed. */
18993 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18994 cp_lexer_consume_token (parser->lexer);
18996 else
18997 *non_constant_p = false;
18998 /* Now, there should be a trailing `}'. */
18999 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19000 TREE_TYPE (initializer) = init_list_type_node;
19001 return initializer;
19004 /* Parse an initializer-list.
19006 initializer-list:
19007 initializer-clause ... [opt]
19008 initializer-list , initializer-clause ... [opt]
19010 GNU Extension:
19012 initializer-list:
19013 designation initializer-clause ...[opt]
19014 initializer-list , designation initializer-clause ...[opt]
19016 designation:
19017 . identifier =
19018 identifier :
19019 [ constant-expression ] =
19021 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19022 for the initializer. If the INDEX of the elt is non-NULL, it is the
19023 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19024 as for cp_parser_initializer. */
19026 static vec<constructor_elt, va_gc> *
19027 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19029 vec<constructor_elt, va_gc> *v = NULL;
19031 /* Assume all of the expressions are constant. */
19032 *non_constant_p = false;
19034 /* Parse the rest of the list. */
19035 while (true)
19037 cp_token *token;
19038 tree designator;
19039 tree initializer;
19040 bool clause_non_constant_p;
19042 /* If the next token is an identifier and the following one is a
19043 colon, we are looking at the GNU designated-initializer
19044 syntax. */
19045 if (cp_parser_allow_gnu_extensions_p (parser)
19046 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19047 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19049 /* Warn the user that they are using an extension. */
19050 pedwarn (input_location, OPT_Wpedantic,
19051 "ISO C++ does not allow designated initializers");
19052 /* Consume the identifier. */
19053 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19054 /* Consume the `:'. */
19055 cp_lexer_consume_token (parser->lexer);
19057 /* Also handle the C99 syntax, '. id ='. */
19058 else if (cp_parser_allow_gnu_extensions_p (parser)
19059 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19060 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19061 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19063 /* Warn the user that they are using an extension. */
19064 pedwarn (input_location, OPT_Wpedantic,
19065 "ISO C++ does not allow C99 designated initializers");
19066 /* Consume the `.'. */
19067 cp_lexer_consume_token (parser->lexer);
19068 /* Consume the identifier. */
19069 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19070 /* Consume the `='. */
19071 cp_lexer_consume_token (parser->lexer);
19073 /* Also handle C99 array designators, '[ const ] ='. */
19074 else if (cp_parser_allow_gnu_extensions_p (parser)
19075 && !c_dialect_objc ()
19076 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19078 /* In C++11, [ could start a lambda-introducer. */
19079 bool non_const = false;
19081 cp_parser_parse_tentatively (parser);
19082 cp_lexer_consume_token (parser->lexer);
19083 designator = cp_parser_constant_expression (parser, true, &non_const);
19084 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19085 cp_parser_require (parser, CPP_EQ, RT_EQ);
19086 if (!cp_parser_parse_definitely (parser))
19087 designator = NULL_TREE;
19088 else if (non_const)
19089 require_potential_rvalue_constant_expression (designator);
19091 else
19092 designator = NULL_TREE;
19094 /* Parse the initializer. */
19095 initializer = cp_parser_initializer_clause (parser,
19096 &clause_non_constant_p);
19097 /* If any clause is non-constant, so is the entire initializer. */
19098 if (clause_non_constant_p)
19099 *non_constant_p = true;
19101 /* If we have an ellipsis, this is an initializer pack
19102 expansion. */
19103 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19105 /* Consume the `...'. */
19106 cp_lexer_consume_token (parser->lexer);
19108 /* Turn the initializer into an initializer expansion. */
19109 initializer = make_pack_expansion (initializer);
19112 /* Add it to the vector. */
19113 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19115 /* If the next token is not a comma, we have reached the end of
19116 the list. */
19117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19118 break;
19120 /* Peek at the next token. */
19121 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19122 /* If the next token is a `}', then we're still done. An
19123 initializer-clause can have a trailing `,' after the
19124 initializer-list and before the closing `}'. */
19125 if (token->type == CPP_CLOSE_BRACE)
19126 break;
19128 /* Consume the `,' token. */
19129 cp_lexer_consume_token (parser->lexer);
19132 return v;
19135 /* Classes [gram.class] */
19137 /* Parse a class-name.
19139 class-name:
19140 identifier
19141 template-id
19143 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19144 to indicate that names looked up in dependent types should be
19145 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19146 keyword has been used to indicate that the name that appears next
19147 is a template. TAG_TYPE indicates the explicit tag given before
19148 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19149 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19150 is the class being defined in a class-head.
19152 Returns the TYPE_DECL representing the class. */
19154 static tree
19155 cp_parser_class_name (cp_parser *parser,
19156 bool typename_keyword_p,
19157 bool template_keyword_p,
19158 enum tag_types tag_type,
19159 bool check_dependency_p,
19160 bool class_head_p,
19161 bool is_declaration)
19163 tree decl;
19164 tree scope;
19165 bool typename_p;
19166 cp_token *token;
19167 tree identifier = NULL_TREE;
19169 /* All class-names start with an identifier. */
19170 token = cp_lexer_peek_token (parser->lexer);
19171 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19173 cp_parser_error (parser, "expected class-name");
19174 return error_mark_node;
19177 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19178 to a template-id, so we save it here. */
19179 scope = parser->scope;
19180 if (scope == error_mark_node)
19181 return error_mark_node;
19183 /* Any name names a type if we're following the `typename' keyword
19184 in a qualified name where the enclosing scope is type-dependent. */
19185 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19186 && dependent_type_p (scope));
19187 /* Handle the common case (an identifier, but not a template-id)
19188 efficiently. */
19189 if (token->type == CPP_NAME
19190 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19192 cp_token *identifier_token;
19193 bool ambiguous_p;
19195 /* Look for the identifier. */
19196 identifier_token = cp_lexer_peek_token (parser->lexer);
19197 ambiguous_p = identifier_token->ambiguous_p;
19198 identifier = cp_parser_identifier (parser);
19199 /* If the next token isn't an identifier, we are certainly not
19200 looking at a class-name. */
19201 if (identifier == error_mark_node)
19202 decl = error_mark_node;
19203 /* If we know this is a type-name, there's no need to look it
19204 up. */
19205 else if (typename_p)
19206 decl = identifier;
19207 else
19209 tree ambiguous_decls;
19210 /* If we already know that this lookup is ambiguous, then
19211 we've already issued an error message; there's no reason
19212 to check again. */
19213 if (ambiguous_p)
19215 cp_parser_simulate_error (parser);
19216 return error_mark_node;
19218 /* If the next token is a `::', then the name must be a type
19219 name.
19221 [basic.lookup.qual]
19223 During the lookup for a name preceding the :: scope
19224 resolution operator, object, function, and enumerator
19225 names are ignored. */
19226 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19227 tag_type = typename_type;
19228 /* Look up the name. */
19229 decl = cp_parser_lookup_name (parser, identifier,
19230 tag_type,
19231 /*is_template=*/false,
19232 /*is_namespace=*/false,
19233 check_dependency_p,
19234 &ambiguous_decls,
19235 identifier_token->location);
19236 if (ambiguous_decls)
19238 if (cp_parser_parsing_tentatively (parser))
19239 cp_parser_simulate_error (parser);
19240 return error_mark_node;
19244 else
19246 /* Try a template-id. */
19247 decl = cp_parser_template_id (parser, template_keyword_p,
19248 check_dependency_p,
19249 tag_type,
19250 is_declaration);
19251 if (decl == error_mark_node)
19252 return error_mark_node;
19255 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19257 /* If this is a typename, create a TYPENAME_TYPE. */
19258 if (typename_p && decl != error_mark_node)
19260 decl = make_typename_type (scope, decl, typename_type,
19261 /*complain=*/tf_error);
19262 if (decl != error_mark_node)
19263 decl = TYPE_NAME (decl);
19266 decl = strip_using_decl (decl);
19268 /* Check to see that it is really the name of a class. */
19269 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19270 && identifier_p (TREE_OPERAND (decl, 0))
19271 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19272 /* Situations like this:
19274 template <typename T> struct A {
19275 typename T::template X<int>::I i;
19278 are problematic. Is `T::template X<int>' a class-name? The
19279 standard does not seem to be definitive, but there is no other
19280 valid interpretation of the following `::'. Therefore, those
19281 names are considered class-names. */
19283 decl = make_typename_type (scope, decl, tag_type, tf_error);
19284 if (decl != error_mark_node)
19285 decl = TYPE_NAME (decl);
19287 else if (TREE_CODE (decl) != TYPE_DECL
19288 || TREE_TYPE (decl) == error_mark_node
19289 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19290 /* In Objective-C 2.0, a classname followed by '.' starts a
19291 dot-syntax expression, and it's not a type-name. */
19292 || (c_dialect_objc ()
19293 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19294 && objc_is_class_name (decl)))
19295 decl = error_mark_node;
19297 if (decl == error_mark_node)
19298 cp_parser_error (parser, "expected class-name");
19299 else if (identifier && !parser->scope)
19300 maybe_note_name_used_in_class (identifier, decl);
19302 return decl;
19305 /* Parse a class-specifier.
19307 class-specifier:
19308 class-head { member-specification [opt] }
19310 Returns the TREE_TYPE representing the class. */
19312 static tree
19313 cp_parser_class_specifier_1 (cp_parser* parser)
19315 tree type;
19316 tree attributes = NULL_TREE;
19317 bool nested_name_specifier_p;
19318 unsigned saved_num_template_parameter_lists;
19319 bool saved_in_function_body;
19320 unsigned char in_statement;
19321 bool in_switch_statement_p;
19322 bool saved_in_unbraced_linkage_specification_p;
19323 tree old_scope = NULL_TREE;
19324 tree scope = NULL_TREE;
19325 cp_token *closing_brace;
19327 push_deferring_access_checks (dk_no_deferred);
19329 /* Parse the class-head. */
19330 type = cp_parser_class_head (parser,
19331 &nested_name_specifier_p);
19332 /* If the class-head was a semantic disaster, skip the entire body
19333 of the class. */
19334 if (!type)
19336 cp_parser_skip_to_end_of_block_or_statement (parser);
19337 pop_deferring_access_checks ();
19338 return error_mark_node;
19341 /* Look for the `{'. */
19342 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19344 pop_deferring_access_checks ();
19345 return error_mark_node;
19348 cp_ensure_no_omp_declare_simd (parser);
19350 /* Issue an error message if type-definitions are forbidden here. */
19351 cp_parser_check_type_definition (parser);
19352 /* Remember that we are defining one more class. */
19353 ++parser->num_classes_being_defined;
19354 /* Inside the class, surrounding template-parameter-lists do not
19355 apply. */
19356 saved_num_template_parameter_lists
19357 = parser->num_template_parameter_lists;
19358 parser->num_template_parameter_lists = 0;
19359 /* We are not in a function body. */
19360 saved_in_function_body = parser->in_function_body;
19361 parser->in_function_body = false;
19362 /* Or in a loop. */
19363 in_statement = parser->in_statement;
19364 parser->in_statement = 0;
19365 /* Or in a switch. */
19366 in_switch_statement_p = parser->in_switch_statement_p;
19367 parser->in_switch_statement_p = false;
19368 /* We are not immediately inside an extern "lang" block. */
19369 saved_in_unbraced_linkage_specification_p
19370 = parser->in_unbraced_linkage_specification_p;
19371 parser->in_unbraced_linkage_specification_p = false;
19373 /* Start the class. */
19374 if (nested_name_specifier_p)
19376 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19377 old_scope = push_inner_scope (scope);
19379 type = begin_class_definition (type);
19381 if (type == error_mark_node)
19382 /* If the type is erroneous, skip the entire body of the class. */
19383 cp_parser_skip_to_closing_brace (parser);
19384 else
19385 /* Parse the member-specification. */
19386 cp_parser_member_specification_opt (parser);
19388 /* Look for the trailing `}'. */
19389 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19390 /* Look for trailing attributes to apply to this class. */
19391 if (cp_parser_allow_gnu_extensions_p (parser))
19392 attributes = cp_parser_gnu_attributes_opt (parser);
19393 if (type != error_mark_node)
19394 type = finish_struct (type, attributes);
19395 if (nested_name_specifier_p)
19396 pop_inner_scope (old_scope, scope);
19398 /* We've finished a type definition. Check for the common syntax
19399 error of forgetting a semicolon after the definition. We need to
19400 be careful, as we can't just check for not-a-semicolon and be done
19401 with it; the user might have typed:
19403 class X { } c = ...;
19404 class X { } *p = ...;
19406 and so forth. Instead, enumerate all the possible tokens that
19407 might follow this production; if we don't see one of them, then
19408 complain and silently insert the semicolon. */
19410 cp_token *token = cp_lexer_peek_token (parser->lexer);
19411 bool want_semicolon = true;
19413 if (cp_next_tokens_can_be_std_attribute_p (parser))
19414 /* Don't try to parse c++11 attributes here. As per the
19415 grammar, that should be a task for
19416 cp_parser_decl_specifier_seq. */
19417 want_semicolon = false;
19419 switch (token->type)
19421 case CPP_NAME:
19422 case CPP_SEMICOLON:
19423 case CPP_MULT:
19424 case CPP_AND:
19425 case CPP_OPEN_PAREN:
19426 case CPP_CLOSE_PAREN:
19427 case CPP_COMMA:
19428 want_semicolon = false;
19429 break;
19431 /* While it's legal for type qualifiers and storage class
19432 specifiers to follow type definitions in the grammar, only
19433 compiler testsuites contain code like that. Assume that if
19434 we see such code, then what we're really seeing is a case
19435 like:
19437 class X { }
19438 const <type> var = ...;
19442 class Y { }
19443 static <type> func (...) ...
19445 i.e. the qualifier or specifier applies to the next
19446 declaration. To do so, however, we need to look ahead one
19447 more token to see if *that* token is a type specifier.
19449 This code could be improved to handle:
19451 class Z { }
19452 static const <type> var = ...; */
19453 case CPP_KEYWORD:
19454 if (keyword_is_decl_specifier (token->keyword))
19456 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19458 /* Handling user-defined types here would be nice, but very
19459 tricky. */
19460 want_semicolon
19461 = (lookahead->type == CPP_KEYWORD
19462 && keyword_begins_type_specifier (lookahead->keyword));
19464 break;
19465 default:
19466 break;
19469 /* If we don't have a type, then something is very wrong and we
19470 shouldn't try to do anything clever. Likewise for not seeing the
19471 closing brace. */
19472 if (closing_brace && TYPE_P (type) && want_semicolon)
19474 cp_token_position prev
19475 = cp_lexer_previous_token_position (parser->lexer);
19476 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19477 location_t loc = prev_token->location;
19479 if (CLASSTYPE_DECLARED_CLASS (type))
19480 error_at (loc, "expected %<;%> after class definition");
19481 else if (TREE_CODE (type) == RECORD_TYPE)
19482 error_at (loc, "expected %<;%> after struct definition");
19483 else if (TREE_CODE (type) == UNION_TYPE)
19484 error_at (loc, "expected %<;%> after union definition");
19485 else
19486 gcc_unreachable ();
19488 /* Unget one token and smash it to look as though we encountered
19489 a semicolon in the input stream. */
19490 cp_lexer_set_token_position (parser->lexer, prev);
19491 token = cp_lexer_peek_token (parser->lexer);
19492 token->type = CPP_SEMICOLON;
19493 token->keyword = RID_MAX;
19497 /* If this class is not itself within the scope of another class,
19498 then we need to parse the bodies of all of the queued function
19499 definitions. Note that the queued functions defined in a class
19500 are not always processed immediately following the
19501 class-specifier for that class. Consider:
19503 struct A {
19504 struct B { void f() { sizeof (A); } };
19507 If `f' were processed before the processing of `A' were
19508 completed, there would be no way to compute the size of `A'.
19509 Note that the nesting we are interested in here is lexical --
19510 not the semantic nesting given by TYPE_CONTEXT. In particular,
19511 for:
19513 struct A { struct B; };
19514 struct A::B { void f() { } };
19516 there is no need to delay the parsing of `A::B::f'. */
19517 if (--parser->num_classes_being_defined == 0)
19519 tree decl;
19520 tree class_type = NULL_TREE;
19521 tree pushed_scope = NULL_TREE;
19522 unsigned ix;
19523 cp_default_arg_entry *e;
19524 tree save_ccp, save_ccr;
19526 /* In a first pass, parse default arguments to the functions.
19527 Then, in a second pass, parse the bodies of the functions.
19528 This two-phased approach handles cases like:
19530 struct S {
19531 void f() { g(); }
19532 void g(int i = 3);
19536 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19538 decl = e->decl;
19539 /* If there are default arguments that have not yet been processed,
19540 take care of them now. */
19541 if (class_type != e->class_type)
19543 if (pushed_scope)
19544 pop_scope (pushed_scope);
19545 class_type = e->class_type;
19546 pushed_scope = push_scope (class_type);
19548 /* Make sure that any template parameters are in scope. */
19549 maybe_begin_member_template_processing (decl);
19550 /* Parse the default argument expressions. */
19551 cp_parser_late_parsing_default_args (parser, decl);
19552 /* Remove any template parameters from the symbol table. */
19553 maybe_end_member_template_processing ();
19555 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19556 /* Now parse any NSDMIs. */
19557 save_ccp = current_class_ptr;
19558 save_ccr = current_class_ref;
19559 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19561 if (class_type != DECL_CONTEXT (decl))
19563 if (pushed_scope)
19564 pop_scope (pushed_scope);
19565 class_type = DECL_CONTEXT (decl);
19566 pushed_scope = push_scope (class_type);
19568 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19569 cp_parser_late_parsing_nsdmi (parser, decl);
19571 vec_safe_truncate (unparsed_nsdmis, 0);
19572 current_class_ptr = save_ccp;
19573 current_class_ref = save_ccr;
19574 if (pushed_scope)
19575 pop_scope (pushed_scope);
19576 /* Now parse the body of the functions. */
19577 if (flag_openmp)
19579 /* OpenMP UDRs need to be parsed before all other functions. */
19580 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19581 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19582 cp_parser_late_parsing_for_member (parser, decl);
19583 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19584 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19585 cp_parser_late_parsing_for_member (parser, decl);
19587 else
19588 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19589 cp_parser_late_parsing_for_member (parser, decl);
19590 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19593 /* Put back any saved access checks. */
19594 pop_deferring_access_checks ();
19596 /* Restore saved state. */
19597 parser->in_switch_statement_p = in_switch_statement_p;
19598 parser->in_statement = in_statement;
19599 parser->in_function_body = saved_in_function_body;
19600 parser->num_template_parameter_lists
19601 = saved_num_template_parameter_lists;
19602 parser->in_unbraced_linkage_specification_p
19603 = saved_in_unbraced_linkage_specification_p;
19605 return type;
19608 static tree
19609 cp_parser_class_specifier (cp_parser* parser)
19611 tree ret;
19612 timevar_push (TV_PARSE_STRUCT);
19613 ret = cp_parser_class_specifier_1 (parser);
19614 timevar_pop (TV_PARSE_STRUCT);
19615 return ret;
19618 /* Parse a class-head.
19620 class-head:
19621 class-key identifier [opt] base-clause [opt]
19622 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19623 class-key nested-name-specifier [opt] template-id
19624 base-clause [opt]
19626 class-virt-specifier:
19627 final
19629 GNU Extensions:
19630 class-key attributes identifier [opt] base-clause [opt]
19631 class-key attributes nested-name-specifier identifier base-clause [opt]
19632 class-key attributes nested-name-specifier [opt] template-id
19633 base-clause [opt]
19635 Upon return BASES is initialized to the list of base classes (or
19636 NULL, if there are none) in the same form returned by
19637 cp_parser_base_clause.
19639 Returns the TYPE of the indicated class. Sets
19640 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19641 involving a nested-name-specifier was used, and FALSE otherwise.
19643 Returns error_mark_node if this is not a class-head.
19645 Returns NULL_TREE if the class-head is syntactically valid, but
19646 semantically invalid in a way that means we should skip the entire
19647 body of the class. */
19649 static tree
19650 cp_parser_class_head (cp_parser* parser,
19651 bool* nested_name_specifier_p)
19653 tree nested_name_specifier;
19654 enum tag_types class_key;
19655 tree id = NULL_TREE;
19656 tree type = NULL_TREE;
19657 tree attributes;
19658 tree bases;
19659 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19660 bool template_id_p = false;
19661 bool qualified_p = false;
19662 bool invalid_nested_name_p = false;
19663 bool invalid_explicit_specialization_p = false;
19664 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19665 tree pushed_scope = NULL_TREE;
19666 unsigned num_templates;
19667 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19668 /* Assume no nested-name-specifier will be present. */
19669 *nested_name_specifier_p = false;
19670 /* Assume no template parameter lists will be used in defining the
19671 type. */
19672 num_templates = 0;
19673 parser->colon_corrects_to_scope_p = false;
19675 /* Look for the class-key. */
19676 class_key = cp_parser_class_key (parser);
19677 if (class_key == none_type)
19678 return error_mark_node;
19680 /* Parse the attributes. */
19681 attributes = cp_parser_attributes_opt (parser);
19683 /* If the next token is `::', that is invalid -- but sometimes
19684 people do try to write:
19686 struct ::S {};
19688 Handle this gracefully by accepting the extra qualifier, and then
19689 issuing an error about it later if this really is a
19690 class-head. If it turns out just to be an elaborated type
19691 specifier, remain silent. */
19692 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19693 qualified_p = true;
19695 push_deferring_access_checks (dk_no_check);
19697 /* Determine the name of the class. Begin by looking for an
19698 optional nested-name-specifier. */
19699 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19700 nested_name_specifier
19701 = cp_parser_nested_name_specifier_opt (parser,
19702 /*typename_keyword_p=*/false,
19703 /*check_dependency_p=*/false,
19704 /*type_p=*/true,
19705 /*is_declaration=*/false);
19706 /* If there was a nested-name-specifier, then there *must* be an
19707 identifier. */
19708 if (nested_name_specifier)
19710 type_start_token = cp_lexer_peek_token (parser->lexer);
19711 /* Although the grammar says `identifier', it really means
19712 `class-name' or `template-name'. You are only allowed to
19713 define a class that has already been declared with this
19714 syntax.
19716 The proposed resolution for Core Issue 180 says that wherever
19717 you see `class T::X' you should treat `X' as a type-name.
19719 It is OK to define an inaccessible class; for example:
19721 class A { class B; };
19722 class A::B {};
19724 We do not know if we will see a class-name, or a
19725 template-name. We look for a class-name first, in case the
19726 class-name is a template-id; if we looked for the
19727 template-name first we would stop after the template-name. */
19728 cp_parser_parse_tentatively (parser);
19729 type = cp_parser_class_name (parser,
19730 /*typename_keyword_p=*/false,
19731 /*template_keyword_p=*/false,
19732 class_type,
19733 /*check_dependency_p=*/false,
19734 /*class_head_p=*/true,
19735 /*is_declaration=*/false);
19736 /* If that didn't work, ignore the nested-name-specifier. */
19737 if (!cp_parser_parse_definitely (parser))
19739 invalid_nested_name_p = true;
19740 type_start_token = cp_lexer_peek_token (parser->lexer);
19741 id = cp_parser_identifier (parser);
19742 if (id == error_mark_node)
19743 id = NULL_TREE;
19745 /* If we could not find a corresponding TYPE, treat this
19746 declaration like an unqualified declaration. */
19747 if (type == error_mark_node)
19748 nested_name_specifier = NULL_TREE;
19749 /* Otherwise, count the number of templates used in TYPE and its
19750 containing scopes. */
19751 else
19753 tree scope;
19755 for (scope = TREE_TYPE (type);
19756 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19757 scope = get_containing_scope (scope))
19758 if (TYPE_P (scope)
19759 && CLASS_TYPE_P (scope)
19760 && CLASSTYPE_TEMPLATE_INFO (scope)
19761 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19762 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19763 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19764 ++num_templates;
19767 /* Otherwise, the identifier is optional. */
19768 else
19770 /* We don't know whether what comes next is a template-id,
19771 an identifier, or nothing at all. */
19772 cp_parser_parse_tentatively (parser);
19773 /* Check for a template-id. */
19774 type_start_token = cp_lexer_peek_token (parser->lexer);
19775 id = cp_parser_template_id (parser,
19776 /*template_keyword_p=*/false,
19777 /*check_dependency_p=*/true,
19778 class_key,
19779 /*is_declaration=*/true);
19780 /* If that didn't work, it could still be an identifier. */
19781 if (!cp_parser_parse_definitely (parser))
19783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19785 type_start_token = cp_lexer_peek_token (parser->lexer);
19786 id = cp_parser_identifier (parser);
19788 else
19789 id = NULL_TREE;
19791 else
19793 template_id_p = true;
19794 ++num_templates;
19798 pop_deferring_access_checks ();
19800 if (id)
19802 cp_parser_check_for_invalid_template_id (parser, id,
19803 class_key,
19804 type_start_token->location);
19806 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19808 /* If it's not a `:' or a `{' then we can't really be looking at a
19809 class-head, since a class-head only appears as part of a
19810 class-specifier. We have to detect this situation before calling
19811 xref_tag, since that has irreversible side-effects. */
19812 if (!cp_parser_next_token_starts_class_definition_p (parser))
19814 cp_parser_error (parser, "expected %<{%> or %<:%>");
19815 type = error_mark_node;
19816 goto out;
19819 /* At this point, we're going ahead with the class-specifier, even
19820 if some other problem occurs. */
19821 cp_parser_commit_to_tentative_parse (parser);
19822 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19824 cp_parser_error (parser,
19825 "cannot specify %<override%> for a class");
19826 type = error_mark_node;
19827 goto out;
19829 /* Issue the error about the overly-qualified name now. */
19830 if (qualified_p)
19832 cp_parser_error (parser,
19833 "global qualification of class name is invalid");
19834 type = error_mark_node;
19835 goto out;
19837 else if (invalid_nested_name_p)
19839 cp_parser_error (parser,
19840 "qualified name does not name a class");
19841 type = error_mark_node;
19842 goto out;
19844 else if (nested_name_specifier)
19846 tree scope;
19848 /* Reject typedef-names in class heads. */
19849 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19851 error_at (type_start_token->location,
19852 "invalid class name in declaration of %qD",
19853 type);
19854 type = NULL_TREE;
19855 goto done;
19858 /* Figure out in what scope the declaration is being placed. */
19859 scope = current_scope ();
19860 /* If that scope does not contain the scope in which the
19861 class was originally declared, the program is invalid. */
19862 if (scope && !is_ancestor (scope, nested_name_specifier))
19864 if (at_namespace_scope_p ())
19865 error_at (type_start_token->location,
19866 "declaration of %qD in namespace %qD which does not "
19867 "enclose %qD",
19868 type, scope, nested_name_specifier);
19869 else
19870 error_at (type_start_token->location,
19871 "declaration of %qD in %qD which does not enclose %qD",
19872 type, scope, nested_name_specifier);
19873 type = NULL_TREE;
19874 goto done;
19876 /* [dcl.meaning]
19878 A declarator-id shall not be qualified except for the
19879 definition of a ... nested class outside of its class
19880 ... [or] the definition or explicit instantiation of a
19881 class member of a namespace outside of its namespace. */
19882 if (scope == nested_name_specifier)
19884 permerror (nested_name_specifier_token_start->location,
19885 "extra qualification not allowed");
19886 nested_name_specifier = NULL_TREE;
19887 num_templates = 0;
19890 /* An explicit-specialization must be preceded by "template <>". If
19891 it is not, try to recover gracefully. */
19892 if (at_namespace_scope_p ()
19893 && parser->num_template_parameter_lists == 0
19894 && template_id_p)
19896 error_at (type_start_token->location,
19897 "an explicit specialization must be preceded by %<template <>%>");
19898 invalid_explicit_specialization_p = true;
19899 /* Take the same action that would have been taken by
19900 cp_parser_explicit_specialization. */
19901 ++parser->num_template_parameter_lists;
19902 begin_specialization ();
19904 /* There must be no "return" statements between this point and the
19905 end of this function; set "type "to the correct return value and
19906 use "goto done;" to return. */
19907 /* Make sure that the right number of template parameters were
19908 present. */
19909 if (!cp_parser_check_template_parameters (parser, num_templates,
19910 type_start_token->location,
19911 /*declarator=*/NULL))
19913 /* If something went wrong, there is no point in even trying to
19914 process the class-definition. */
19915 type = NULL_TREE;
19916 goto done;
19919 /* Look up the type. */
19920 if (template_id_p)
19922 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19923 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19924 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19926 error_at (type_start_token->location,
19927 "function template %qD redeclared as a class template", id);
19928 type = error_mark_node;
19930 else
19932 type = TREE_TYPE (id);
19933 type = maybe_process_partial_specialization (type);
19935 if (nested_name_specifier)
19936 pushed_scope = push_scope (nested_name_specifier);
19938 else if (nested_name_specifier)
19940 tree class_type;
19942 /* Given:
19944 template <typename T> struct S { struct T };
19945 template <typename T> struct S<T>::T { };
19947 we will get a TYPENAME_TYPE when processing the definition of
19948 `S::T'. We need to resolve it to the actual type before we
19949 try to define it. */
19950 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19952 class_type = resolve_typename_type (TREE_TYPE (type),
19953 /*only_current_p=*/false);
19954 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19955 type = TYPE_NAME (class_type);
19956 else
19958 cp_parser_error (parser, "could not resolve typename type");
19959 type = error_mark_node;
19963 if (maybe_process_partial_specialization (TREE_TYPE (type))
19964 == error_mark_node)
19966 type = NULL_TREE;
19967 goto done;
19970 class_type = current_class_type;
19971 /* Enter the scope indicated by the nested-name-specifier. */
19972 pushed_scope = push_scope (nested_name_specifier);
19973 /* Get the canonical version of this type. */
19974 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19975 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19976 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19978 type = push_template_decl (type);
19979 if (type == error_mark_node)
19981 type = NULL_TREE;
19982 goto done;
19986 type = TREE_TYPE (type);
19987 *nested_name_specifier_p = true;
19989 else /* The name is not a nested name. */
19991 /* If the class was unnamed, create a dummy name. */
19992 if (!id)
19993 id = make_anon_name ();
19994 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19995 parser->num_template_parameter_lists);
19998 /* Indicate whether this class was declared as a `class' or as a
19999 `struct'. */
20000 if (TREE_CODE (type) == RECORD_TYPE)
20001 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20002 cp_parser_check_class_key (class_key, type);
20004 /* If this type was already complete, and we see another definition,
20005 that's an error. */
20006 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20008 error_at (type_start_token->location, "redefinition of %q#T",
20009 type);
20010 error_at (type_start_token->location, "previous definition of %q+#T",
20011 type);
20012 type = NULL_TREE;
20013 goto done;
20015 else if (type == error_mark_node)
20016 type = NULL_TREE;
20018 if (type)
20020 /* Apply attributes now, before any use of the class as a template
20021 argument in its base list. */
20022 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20023 fixup_attribute_variants (type);
20026 /* We will have entered the scope containing the class; the names of
20027 base classes should be looked up in that context. For example:
20029 struct A { struct B {}; struct C; };
20030 struct A::C : B {};
20032 is valid. */
20034 /* Get the list of base-classes, if there is one. */
20035 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20036 bases = cp_parser_base_clause (parser);
20037 else
20038 bases = NULL_TREE;
20040 /* If we're really defining a class, process the base classes.
20041 If they're invalid, fail. */
20042 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20043 && !xref_basetypes (type, bases))
20044 type = NULL_TREE;
20046 done:
20047 /* Leave the scope given by the nested-name-specifier. We will
20048 enter the class scope itself while processing the members. */
20049 if (pushed_scope)
20050 pop_scope (pushed_scope);
20052 if (invalid_explicit_specialization_p)
20054 end_specialization ();
20055 --parser->num_template_parameter_lists;
20058 if (type)
20059 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20060 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20061 CLASSTYPE_FINAL (type) = 1;
20062 out:
20063 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20064 return type;
20067 /* Parse a class-key.
20069 class-key:
20070 class
20071 struct
20072 union
20074 Returns the kind of class-key specified, or none_type to indicate
20075 error. */
20077 static enum tag_types
20078 cp_parser_class_key (cp_parser* parser)
20080 cp_token *token;
20081 enum tag_types tag_type;
20083 /* Look for the class-key. */
20084 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20085 if (!token)
20086 return none_type;
20088 /* Check to see if the TOKEN is a class-key. */
20089 tag_type = cp_parser_token_is_class_key (token);
20090 if (!tag_type)
20091 cp_parser_error (parser, "expected class-key");
20092 return tag_type;
20095 /* Parse an (optional) member-specification.
20097 member-specification:
20098 member-declaration member-specification [opt]
20099 access-specifier : member-specification [opt] */
20101 static void
20102 cp_parser_member_specification_opt (cp_parser* parser)
20104 while (true)
20106 cp_token *token;
20107 enum rid keyword;
20109 /* Peek at the next token. */
20110 token = cp_lexer_peek_token (parser->lexer);
20111 /* If it's a `}', or EOF then we've seen all the members. */
20112 if (token->type == CPP_CLOSE_BRACE
20113 || token->type == CPP_EOF
20114 || token->type == CPP_PRAGMA_EOL)
20115 break;
20117 /* See if this token is a keyword. */
20118 keyword = token->keyword;
20119 switch (keyword)
20121 case RID_PUBLIC:
20122 case RID_PROTECTED:
20123 case RID_PRIVATE:
20124 /* Consume the access-specifier. */
20125 cp_lexer_consume_token (parser->lexer);
20126 /* Remember which access-specifier is active. */
20127 current_access_specifier = token->u.value;
20128 /* Look for the `:'. */
20129 cp_parser_require (parser, CPP_COLON, RT_COLON);
20130 break;
20132 default:
20133 /* Accept #pragmas at class scope. */
20134 if (token->type == CPP_PRAGMA)
20136 cp_parser_pragma (parser, pragma_member);
20137 break;
20140 /* Otherwise, the next construction must be a
20141 member-declaration. */
20142 cp_parser_member_declaration (parser);
20147 /* Parse a member-declaration.
20149 member-declaration:
20150 decl-specifier-seq [opt] member-declarator-list [opt] ;
20151 function-definition ; [opt]
20152 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20153 using-declaration
20154 template-declaration
20155 alias-declaration
20157 member-declarator-list:
20158 member-declarator
20159 member-declarator-list , member-declarator
20161 member-declarator:
20162 declarator pure-specifier [opt]
20163 declarator constant-initializer [opt]
20164 identifier [opt] : constant-expression
20166 GNU Extensions:
20168 member-declaration:
20169 __extension__ member-declaration
20171 member-declarator:
20172 declarator attributes [opt] pure-specifier [opt]
20173 declarator attributes [opt] constant-initializer [opt]
20174 identifier [opt] attributes [opt] : constant-expression
20176 C++0x Extensions:
20178 member-declaration:
20179 static_assert-declaration */
20181 static void
20182 cp_parser_member_declaration (cp_parser* parser)
20184 cp_decl_specifier_seq decl_specifiers;
20185 tree prefix_attributes;
20186 tree decl;
20187 int declares_class_or_enum;
20188 bool friend_p;
20189 cp_token *token = NULL;
20190 cp_token *decl_spec_token_start = NULL;
20191 cp_token *initializer_token_start = NULL;
20192 int saved_pedantic;
20193 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20195 /* Check for the `__extension__' keyword. */
20196 if (cp_parser_extension_opt (parser, &saved_pedantic))
20198 /* Recurse. */
20199 cp_parser_member_declaration (parser);
20200 /* Restore the old value of the PEDANTIC flag. */
20201 pedantic = saved_pedantic;
20203 return;
20206 /* Check for a template-declaration. */
20207 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20209 /* An explicit specialization here is an error condition, and we
20210 expect the specialization handler to detect and report this. */
20211 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20212 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20213 cp_parser_explicit_specialization (parser);
20214 else
20215 cp_parser_template_declaration (parser, /*member_p=*/true);
20217 return;
20220 /* Check for a using-declaration. */
20221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20223 if (cxx_dialect < cxx11)
20225 /* Parse the using-declaration. */
20226 cp_parser_using_declaration (parser,
20227 /*access_declaration_p=*/false);
20228 return;
20230 else
20232 tree decl;
20233 bool alias_decl_expected;
20234 cp_parser_parse_tentatively (parser);
20235 decl = cp_parser_alias_declaration (parser);
20236 /* Note that if we actually see the '=' token after the
20237 identifier, cp_parser_alias_declaration commits the
20238 tentative parse. In that case, we really expects an
20239 alias-declaration. Otherwise, we expect a using
20240 declaration. */
20241 alias_decl_expected =
20242 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20243 cp_parser_parse_definitely (parser);
20245 if (alias_decl_expected)
20246 finish_member_declaration (decl);
20247 else
20248 cp_parser_using_declaration (parser,
20249 /*access_declaration_p=*/false);
20250 return;
20254 /* Check for @defs. */
20255 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20257 tree ivar, member;
20258 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20259 ivar = ivar_chains;
20260 while (ivar)
20262 member = ivar;
20263 ivar = TREE_CHAIN (member);
20264 TREE_CHAIN (member) = NULL_TREE;
20265 finish_member_declaration (member);
20267 return;
20270 /* If the next token is `static_assert' we have a static assertion. */
20271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20273 cp_parser_static_assert (parser, /*member_p=*/true);
20274 return;
20277 parser->colon_corrects_to_scope_p = false;
20279 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20280 goto out;
20282 /* Parse the decl-specifier-seq. */
20283 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20284 cp_parser_decl_specifier_seq (parser,
20285 CP_PARSER_FLAGS_OPTIONAL,
20286 &decl_specifiers,
20287 &declares_class_or_enum);
20288 /* Check for an invalid type-name. */
20289 if (!decl_specifiers.any_type_specifiers_p
20290 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20291 goto out;
20292 /* If there is no declarator, then the decl-specifier-seq should
20293 specify a type. */
20294 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20296 /* If there was no decl-specifier-seq, and the next token is a
20297 `;', then we have something like:
20299 struct S { ; };
20301 [class.mem]
20303 Each member-declaration shall declare at least one member
20304 name of the class. */
20305 if (!decl_specifiers.any_specifiers_p)
20307 cp_token *token = cp_lexer_peek_token (parser->lexer);
20308 if (!in_system_header_at (token->location))
20309 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20311 else
20313 tree type;
20315 /* See if this declaration is a friend. */
20316 friend_p = cp_parser_friend_p (&decl_specifiers);
20317 /* If there were decl-specifiers, check to see if there was
20318 a class-declaration. */
20319 type = check_tag_decl (&decl_specifiers,
20320 /*explicit_type_instantiation_p=*/false);
20321 /* Nested classes have already been added to the class, but
20322 a `friend' needs to be explicitly registered. */
20323 if (friend_p)
20325 /* If the `friend' keyword was present, the friend must
20326 be introduced with a class-key. */
20327 if (!declares_class_or_enum && cxx_dialect < cxx11)
20328 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20329 "in C++03 a class-key must be used "
20330 "when declaring a friend");
20331 /* In this case:
20333 template <typename T> struct A {
20334 friend struct A<T>::B;
20337 A<T>::B will be represented by a TYPENAME_TYPE, and
20338 therefore not recognized by check_tag_decl. */
20339 if (!type)
20341 type = decl_specifiers.type;
20342 if (type && TREE_CODE (type) == TYPE_DECL)
20343 type = TREE_TYPE (type);
20345 if (!type || !TYPE_P (type))
20346 error_at (decl_spec_token_start->location,
20347 "friend declaration does not name a class or "
20348 "function");
20349 else
20350 make_friend_class (current_class_type, type,
20351 /*complain=*/true);
20353 /* If there is no TYPE, an error message will already have
20354 been issued. */
20355 else if (!type || type == error_mark_node)
20357 /* An anonymous aggregate has to be handled specially; such
20358 a declaration really declares a data member (with a
20359 particular type), as opposed to a nested class. */
20360 else if (ANON_AGGR_TYPE_P (type))
20362 /* C++11 9.5/6. */
20363 if (decl_specifiers.storage_class != sc_none)
20364 error_at (decl_spec_token_start->location,
20365 "a storage class on an anonymous aggregate "
20366 "in class scope is not allowed");
20368 /* Remove constructors and such from TYPE, now that we
20369 know it is an anonymous aggregate. */
20370 fixup_anonymous_aggr (type);
20371 /* And make the corresponding data member. */
20372 decl = build_decl (decl_spec_token_start->location,
20373 FIELD_DECL, NULL_TREE, type);
20374 /* Add it to the class. */
20375 finish_member_declaration (decl);
20377 else
20378 cp_parser_check_access_in_redeclaration
20379 (TYPE_NAME (type),
20380 decl_spec_token_start->location);
20383 else
20385 bool assume_semicolon = false;
20387 /* Clear attributes from the decl_specifiers but keep them
20388 around as prefix attributes that apply them to the entity
20389 being declared. */
20390 prefix_attributes = decl_specifiers.attributes;
20391 decl_specifiers.attributes = NULL_TREE;
20393 /* See if these declarations will be friends. */
20394 friend_p = cp_parser_friend_p (&decl_specifiers);
20396 /* Keep going until we hit the `;' at the end of the
20397 declaration. */
20398 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20400 tree attributes = NULL_TREE;
20401 tree first_attribute;
20403 /* Peek at the next token. */
20404 token = cp_lexer_peek_token (parser->lexer);
20406 /* Check for a bitfield declaration. */
20407 if (token->type == CPP_COLON
20408 || (token->type == CPP_NAME
20409 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20410 == CPP_COLON))
20412 tree identifier;
20413 tree width;
20415 /* Get the name of the bitfield. Note that we cannot just
20416 check TOKEN here because it may have been invalidated by
20417 the call to cp_lexer_peek_nth_token above. */
20418 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20419 identifier = cp_parser_identifier (parser);
20420 else
20421 identifier = NULL_TREE;
20423 /* Consume the `:' token. */
20424 cp_lexer_consume_token (parser->lexer);
20425 /* Get the width of the bitfield. */
20426 width
20427 = cp_parser_constant_expression (parser,
20428 /*allow_non_constant=*/false,
20429 NULL);
20431 /* Look for attributes that apply to the bitfield. */
20432 attributes = cp_parser_attributes_opt (parser);
20433 /* Remember which attributes are prefix attributes and
20434 which are not. */
20435 first_attribute = attributes;
20436 /* Combine the attributes. */
20437 attributes = chainon (prefix_attributes, attributes);
20439 /* Create the bitfield declaration. */
20440 decl = grokbitfield (identifier
20441 ? make_id_declarator (NULL_TREE,
20442 identifier,
20443 sfk_none)
20444 : NULL,
20445 &decl_specifiers,
20446 width,
20447 attributes);
20449 else
20451 cp_declarator *declarator;
20452 tree initializer;
20453 tree asm_specification;
20454 int ctor_dtor_or_conv_p;
20456 /* Parse the declarator. */
20457 declarator
20458 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20459 &ctor_dtor_or_conv_p,
20460 /*parenthesized_p=*/NULL,
20461 /*member_p=*/true);
20463 /* If something went wrong parsing the declarator, make sure
20464 that we at least consume some tokens. */
20465 if (declarator == cp_error_declarator)
20467 /* Skip to the end of the statement. */
20468 cp_parser_skip_to_end_of_statement (parser);
20469 /* If the next token is not a semicolon, that is
20470 probably because we just skipped over the body of
20471 a function. So, we consume a semicolon if
20472 present, but do not issue an error message if it
20473 is not present. */
20474 if (cp_lexer_next_token_is (parser->lexer,
20475 CPP_SEMICOLON))
20476 cp_lexer_consume_token (parser->lexer);
20477 goto out;
20480 if (declares_class_or_enum & 2)
20481 cp_parser_check_for_definition_in_return_type
20482 (declarator, decl_specifiers.type,
20483 decl_specifiers.locations[ds_type_spec]);
20485 /* Look for an asm-specification. */
20486 asm_specification = cp_parser_asm_specification_opt (parser);
20487 /* Look for attributes that apply to the declaration. */
20488 attributes = cp_parser_attributes_opt (parser);
20489 /* Remember which attributes are prefix attributes and
20490 which are not. */
20491 first_attribute = attributes;
20492 /* Combine the attributes. */
20493 attributes = chainon (prefix_attributes, attributes);
20495 /* If it's an `=', then we have a constant-initializer or a
20496 pure-specifier. It is not correct to parse the
20497 initializer before registering the member declaration
20498 since the member declaration should be in scope while
20499 its initializer is processed. However, the rest of the
20500 front end does not yet provide an interface that allows
20501 us to handle this correctly. */
20502 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20504 /* In [class.mem]:
20506 A pure-specifier shall be used only in the declaration of
20507 a virtual function.
20509 A member-declarator can contain a constant-initializer
20510 only if it declares a static member of integral or
20511 enumeration type.
20513 Therefore, if the DECLARATOR is for a function, we look
20514 for a pure-specifier; otherwise, we look for a
20515 constant-initializer. When we call `grokfield', it will
20516 perform more stringent semantics checks. */
20517 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20518 if (function_declarator_p (declarator)
20519 || (decl_specifiers.type
20520 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20521 && declarator->kind == cdk_id
20522 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20523 == FUNCTION_TYPE)))
20524 initializer = cp_parser_pure_specifier (parser);
20525 else if (decl_specifiers.storage_class != sc_static)
20526 initializer = cp_parser_save_nsdmi (parser);
20527 else if (cxx_dialect >= cxx11)
20529 bool nonconst;
20530 /* Don't require a constant rvalue in C++11, since we
20531 might want a reference constant. We'll enforce
20532 constancy later. */
20533 cp_lexer_consume_token (parser->lexer);
20534 /* Parse the initializer. */
20535 initializer = cp_parser_initializer_clause (parser,
20536 &nonconst);
20538 else
20539 /* Parse the initializer. */
20540 initializer = cp_parser_constant_initializer (parser);
20542 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20543 && !function_declarator_p (declarator))
20545 bool x;
20546 if (decl_specifiers.storage_class != sc_static)
20547 initializer = cp_parser_save_nsdmi (parser);
20548 else
20549 initializer = cp_parser_initializer (parser, &x, &x);
20551 /* Otherwise, there is no initializer. */
20552 else
20553 initializer = NULL_TREE;
20555 // If we're looking at a member function declaration, then a
20556 // requires clause may follow the declaration.
20557 tree saved_template_reqs = release (current_template_reqs);
20558 if (flag_concepts && function_declarator_p (declarator))
20560 // Because this is a non-template member, there are no
20561 // template requirements to conjoin.
20562 if (tree r = cp_parser_requires_clause_opt (parser))
20563 current_template_reqs = finish_template_requirements (r);
20566 /* See if we are probably looking at a function
20567 definition. We are certainly not looking at a
20568 member-declarator. Calling `grokfield' has
20569 side-effects, so we must not do it unless we are sure
20570 that we are looking at a member-declarator. */
20571 if (cp_parser_token_starts_function_definition_p
20572 (cp_lexer_peek_token (parser->lexer)))
20574 /* The grammar does not allow a pure-specifier to be
20575 used when a member function is defined. (It is
20576 possible that this fact is an oversight in the
20577 standard, since a pure function may be defined
20578 outside of the class-specifier. */
20579 if (initializer && initializer_token_start)
20580 error_at (initializer_token_start->location,
20581 "pure-specifier on function-definition");
20582 decl = cp_parser_save_member_function_body (parser,
20583 &decl_specifiers,
20584 declarator,
20585 attributes);
20586 /* If the member was not a friend, declare it here. */
20587 if (!friend_p)
20589 if (parser->fully_implicit_function_template_p)
20590 decl = finish_fully_implicit_template (parser, decl);
20591 finish_member_declaration (decl);
20594 if (friend_p)
20595 check_constrained_friend (decl, current_template_reqs);
20597 /* Peek at the next token. */
20598 token = cp_lexer_peek_token (parser->lexer);
20599 /* If the next token is a semicolon, consume it. */
20600 if (token->type == CPP_SEMICOLON)
20601 cp_lexer_consume_token (parser->lexer);
20603 // Restore the current template requirements.
20604 current_template_reqs = saved_template_reqs;
20606 goto out;
20608 else
20609 if (declarator->kind == cdk_function)
20610 declarator->id_loc = token->location;
20611 /* Create the declaration. */
20612 decl = grokfield (declarator, &decl_specifiers,
20613 initializer, /*init_const_expr_p=*/true,
20614 asm_specification, attributes);
20615 if (parser->fully_implicit_function_template_p)
20616 decl = finish_fully_implicit_template (parser, decl);
20618 // Restore the current template requirments.
20619 current_template_reqs = saved_template_reqs;
20622 cp_finalize_omp_declare_simd (parser, decl);
20624 /* Reset PREFIX_ATTRIBUTES. */
20625 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20626 attributes = TREE_CHAIN (attributes);
20627 if (attributes)
20628 TREE_CHAIN (attributes) = NULL_TREE;
20630 /* If there is any qualification still in effect, clear it
20631 now; we will be starting fresh with the next declarator. */
20632 parser->scope = NULL_TREE;
20633 parser->qualifying_scope = NULL_TREE;
20634 parser->object_scope = NULL_TREE;
20635 /* If it's a `,', then there are more declarators. */
20636 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20638 cp_lexer_consume_token (parser->lexer);
20639 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20641 cp_token *token = cp_lexer_previous_token (parser->lexer);
20642 error_at (token->location,
20643 "stray %<,%> at end of member declaration");
20646 /* If the next token isn't a `;', then we have a parse error. */
20647 else if (cp_lexer_next_token_is_not (parser->lexer,
20648 CPP_SEMICOLON))
20650 /* The next token might be a ways away from where the
20651 actual semicolon is missing. Find the previous token
20652 and use that for our error position. */
20653 cp_token *token = cp_lexer_previous_token (parser->lexer);
20654 error_at (token->location,
20655 "expected %<;%> at end of member declaration");
20657 /* Assume that the user meant to provide a semicolon. If
20658 we were to cp_parser_skip_to_end_of_statement, we might
20659 skip to a semicolon inside a member function definition
20660 and issue nonsensical error messages. */
20661 assume_semicolon = true;
20664 if (decl)
20666 /* Add DECL to the list of members. */
20667 if (!friend_p)
20668 finish_member_declaration (decl);
20670 if (TREE_CODE (decl) == FUNCTION_DECL)
20671 cp_parser_save_default_args (parser, decl);
20672 else if (TREE_CODE (decl) == FIELD_DECL
20673 && !DECL_C_BIT_FIELD (decl)
20674 && DECL_INITIAL (decl))
20675 /* Add DECL to the queue of NSDMI to be parsed later. */
20676 vec_safe_push (unparsed_nsdmis, decl);
20679 if (assume_semicolon)
20680 goto out;
20684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20685 out:
20686 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20689 /* Parse a pure-specifier.
20691 pure-specifier:
20694 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20695 Otherwise, ERROR_MARK_NODE is returned. */
20697 static tree
20698 cp_parser_pure_specifier (cp_parser* parser)
20700 cp_token *token;
20702 /* Look for the `=' token. */
20703 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20704 return error_mark_node;
20705 /* Look for the `0' token. */
20706 token = cp_lexer_peek_token (parser->lexer);
20708 if (token->type == CPP_EOF
20709 || token->type == CPP_PRAGMA_EOL)
20710 return error_mark_node;
20712 cp_lexer_consume_token (parser->lexer);
20714 /* Accept = default or = delete in c++0x mode. */
20715 if (token->keyword == RID_DEFAULT
20716 || token->keyword == RID_DELETE)
20718 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20719 return token->u.value;
20722 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20723 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20725 cp_parser_error (parser,
20726 "invalid pure specifier (only %<= 0%> is allowed)");
20727 cp_parser_skip_to_end_of_statement (parser);
20728 return error_mark_node;
20730 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20732 error_at (token->location, "templates may not be %<virtual%>");
20733 return error_mark_node;
20736 return integer_zero_node;
20739 /* Parse a constant-initializer.
20741 constant-initializer:
20742 = constant-expression
20744 Returns a representation of the constant-expression. */
20746 static tree
20747 cp_parser_constant_initializer (cp_parser* parser)
20749 /* Look for the `=' token. */
20750 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20751 return error_mark_node;
20753 /* It is invalid to write:
20755 struct S { static const int i = { 7 }; };
20758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20760 cp_parser_error (parser,
20761 "a brace-enclosed initializer is not allowed here");
20762 /* Consume the opening brace. */
20763 cp_lexer_consume_token (parser->lexer);
20764 /* Skip the initializer. */
20765 cp_parser_skip_to_closing_brace (parser);
20766 /* Look for the trailing `}'. */
20767 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20769 return error_mark_node;
20772 return cp_parser_constant_expression (parser,
20773 /*allow_non_constant=*/false,
20774 NULL);
20777 /* Derived classes [gram.class.derived] */
20779 /* Parse a base-clause.
20781 base-clause:
20782 : base-specifier-list
20784 base-specifier-list:
20785 base-specifier ... [opt]
20786 base-specifier-list , base-specifier ... [opt]
20788 Returns a TREE_LIST representing the base-classes, in the order in
20789 which they were declared. The representation of each node is as
20790 described by cp_parser_base_specifier.
20792 In the case that no bases are specified, this function will return
20793 NULL_TREE, not ERROR_MARK_NODE. */
20795 static tree
20796 cp_parser_base_clause (cp_parser* parser)
20798 tree bases = NULL_TREE;
20800 /* Look for the `:' that begins the list. */
20801 cp_parser_require (parser, CPP_COLON, RT_COLON);
20803 /* Scan the base-specifier-list. */
20804 while (true)
20806 cp_token *token;
20807 tree base;
20808 bool pack_expansion_p = false;
20810 /* Look for the base-specifier. */
20811 base = cp_parser_base_specifier (parser);
20812 /* Look for the (optional) ellipsis. */
20813 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20815 /* Consume the `...'. */
20816 cp_lexer_consume_token (parser->lexer);
20818 pack_expansion_p = true;
20821 /* Add BASE to the front of the list. */
20822 if (base && base != error_mark_node)
20824 if (pack_expansion_p)
20825 /* Make this a pack expansion type. */
20826 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20828 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20830 TREE_CHAIN (base) = bases;
20831 bases = base;
20834 /* Peek at the next token. */
20835 token = cp_lexer_peek_token (parser->lexer);
20836 /* If it's not a comma, then the list is complete. */
20837 if (token->type != CPP_COMMA)
20838 break;
20839 /* Consume the `,'. */
20840 cp_lexer_consume_token (parser->lexer);
20843 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20844 base class had a qualified name. However, the next name that
20845 appears is certainly not qualified. */
20846 parser->scope = NULL_TREE;
20847 parser->qualifying_scope = NULL_TREE;
20848 parser->object_scope = NULL_TREE;
20850 return nreverse (bases);
20853 /* Parse a base-specifier.
20855 base-specifier:
20856 :: [opt] nested-name-specifier [opt] class-name
20857 virtual access-specifier [opt] :: [opt] nested-name-specifier
20858 [opt] class-name
20859 access-specifier virtual [opt] :: [opt] nested-name-specifier
20860 [opt] class-name
20862 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20863 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20864 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20865 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20867 static tree
20868 cp_parser_base_specifier (cp_parser* parser)
20870 cp_token *token;
20871 bool done = false;
20872 bool virtual_p = false;
20873 bool duplicate_virtual_error_issued_p = false;
20874 bool duplicate_access_error_issued_p = false;
20875 bool class_scope_p, template_p;
20876 tree access = access_default_node;
20877 tree type;
20879 /* Process the optional `virtual' and `access-specifier'. */
20880 while (!done)
20882 /* Peek at the next token. */
20883 token = cp_lexer_peek_token (parser->lexer);
20884 /* Process `virtual'. */
20885 switch (token->keyword)
20887 case RID_VIRTUAL:
20888 /* If `virtual' appears more than once, issue an error. */
20889 if (virtual_p && !duplicate_virtual_error_issued_p)
20891 cp_parser_error (parser,
20892 "%<virtual%> specified more than once in base-specified");
20893 duplicate_virtual_error_issued_p = true;
20896 virtual_p = true;
20898 /* Consume the `virtual' token. */
20899 cp_lexer_consume_token (parser->lexer);
20901 break;
20903 case RID_PUBLIC:
20904 case RID_PROTECTED:
20905 case RID_PRIVATE:
20906 /* If more than one access specifier appears, issue an
20907 error. */
20908 if (access != access_default_node
20909 && !duplicate_access_error_issued_p)
20911 cp_parser_error (parser,
20912 "more than one access specifier in base-specified");
20913 duplicate_access_error_issued_p = true;
20916 access = ridpointers[(int) token->keyword];
20918 /* Consume the access-specifier. */
20919 cp_lexer_consume_token (parser->lexer);
20921 break;
20923 default:
20924 done = true;
20925 break;
20928 /* It is not uncommon to see programs mechanically, erroneously, use
20929 the 'typename' keyword to denote (dependent) qualified types
20930 as base classes. */
20931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20933 token = cp_lexer_peek_token (parser->lexer);
20934 if (!processing_template_decl)
20935 error_at (token->location,
20936 "keyword %<typename%> not allowed outside of templates");
20937 else
20938 error_at (token->location,
20939 "keyword %<typename%> not allowed in this context "
20940 "(the base class is implicitly a type)");
20941 cp_lexer_consume_token (parser->lexer);
20944 /* Look for the optional `::' operator. */
20945 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20946 /* Look for the nested-name-specifier. The simplest way to
20947 implement:
20949 [temp.res]
20951 The keyword `typename' is not permitted in a base-specifier or
20952 mem-initializer; in these contexts a qualified name that
20953 depends on a template-parameter is implicitly assumed to be a
20954 type name.
20956 is to pretend that we have seen the `typename' keyword at this
20957 point. */
20958 cp_parser_nested_name_specifier_opt (parser,
20959 /*typename_keyword_p=*/true,
20960 /*check_dependency_p=*/true,
20961 typename_type,
20962 /*is_declaration=*/true);
20963 /* If the base class is given by a qualified name, assume that names
20964 we see are type names or templates, as appropriate. */
20965 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20966 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20968 if (!parser->scope
20969 && cp_lexer_next_token_is_decltype (parser->lexer))
20970 /* DR 950 allows decltype as a base-specifier. */
20971 type = cp_parser_decltype (parser);
20972 else
20974 /* Otherwise, look for the class-name. */
20975 type = cp_parser_class_name (parser,
20976 class_scope_p,
20977 template_p,
20978 typename_type,
20979 /*check_dependency_p=*/true,
20980 /*class_head_p=*/false,
20981 /*is_declaration=*/true);
20982 type = TREE_TYPE (type);
20985 if (type == error_mark_node)
20986 return error_mark_node;
20988 return finish_base_specifier (type, access, virtual_p);
20991 /* Exception handling [gram.exception] */
20993 /* Parse an (optional) noexcept-specification.
20995 noexcept-specification:
20996 noexcept ( constant-expression ) [opt]
20998 If no noexcept-specification is present, returns NULL_TREE.
20999 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21000 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21001 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21002 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21003 in which case a boolean condition is returned instead. */
21005 static tree
21006 cp_parser_noexcept_specification_opt (cp_parser* parser,
21007 bool require_constexpr,
21008 bool* consumed_expr,
21009 bool return_cond)
21011 cp_token *token;
21012 const char *saved_message;
21014 /* Peek at the next token. */
21015 token = cp_lexer_peek_token (parser->lexer);
21017 /* Is it a noexcept-specification? */
21018 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21020 tree expr;
21021 cp_lexer_consume_token (parser->lexer);
21023 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21025 cp_lexer_consume_token (parser->lexer);
21027 if (require_constexpr)
21029 /* Types may not be defined in an exception-specification. */
21030 saved_message = parser->type_definition_forbidden_message;
21031 parser->type_definition_forbidden_message
21032 = G_("types may not be defined in an exception-specification");
21034 expr = cp_parser_constant_expression (parser, false, NULL);
21036 /* Restore the saved message. */
21037 parser->type_definition_forbidden_message = saved_message;
21039 else
21041 expr = cp_parser_expression (parser, false, NULL);
21042 *consumed_expr = true;
21045 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21047 else
21049 expr = boolean_true_node;
21050 if (!require_constexpr)
21051 *consumed_expr = false;
21054 /* We cannot build a noexcept-spec right away because this will check
21055 that expr is a constexpr. */
21056 if (!return_cond)
21057 return build_noexcept_spec (expr, tf_warning_or_error);
21058 else
21059 return expr;
21061 else
21062 return NULL_TREE;
21065 /* Parse an (optional) exception-specification.
21067 exception-specification:
21068 throw ( type-id-list [opt] )
21070 Returns a TREE_LIST representing the exception-specification. The
21071 TREE_VALUE of each node is a type. */
21073 static tree
21074 cp_parser_exception_specification_opt (cp_parser* parser)
21076 cp_token *token;
21077 tree type_id_list;
21078 const char *saved_message;
21080 /* Peek at the next token. */
21081 token = cp_lexer_peek_token (parser->lexer);
21083 /* Is it a noexcept-specification? */
21084 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21085 false);
21086 if (type_id_list != NULL_TREE)
21087 return type_id_list;
21089 /* If it's not `throw', then there's no exception-specification. */
21090 if (!cp_parser_is_keyword (token, RID_THROW))
21091 return NULL_TREE;
21093 #if 0
21094 /* Enable this once a lot of code has transitioned to noexcept? */
21095 if (cxx_dialect >= cxx11 && !in_system_header)
21096 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21097 "deprecated in C++0x; use %<noexcept%> instead");
21098 #endif
21100 /* Consume the `throw'. */
21101 cp_lexer_consume_token (parser->lexer);
21103 /* Look for the `('. */
21104 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21106 /* Peek at the next token. */
21107 token = cp_lexer_peek_token (parser->lexer);
21108 /* If it's not a `)', then there is a type-id-list. */
21109 if (token->type != CPP_CLOSE_PAREN)
21111 /* Types may not be defined in an exception-specification. */
21112 saved_message = parser->type_definition_forbidden_message;
21113 parser->type_definition_forbidden_message
21114 = G_("types may not be defined in an exception-specification");
21115 /* Parse the type-id-list. */
21116 type_id_list = cp_parser_type_id_list (parser);
21117 /* Restore the saved message. */
21118 parser->type_definition_forbidden_message = saved_message;
21120 else
21121 type_id_list = empty_except_spec;
21123 /* Look for the `)'. */
21124 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21126 return type_id_list;
21129 /* Parse an (optional) type-id-list.
21131 type-id-list:
21132 type-id ... [opt]
21133 type-id-list , type-id ... [opt]
21135 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21136 in the order that the types were presented. */
21138 static tree
21139 cp_parser_type_id_list (cp_parser* parser)
21141 tree types = NULL_TREE;
21143 while (true)
21145 cp_token *token;
21146 tree type;
21148 /* Get the next type-id. */
21149 type = cp_parser_type_id (parser);
21150 /* Parse the optional ellipsis. */
21151 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21153 /* Consume the `...'. */
21154 cp_lexer_consume_token (parser->lexer);
21156 /* Turn the type into a pack expansion expression. */
21157 type = make_pack_expansion (type);
21159 /* Add it to the list. */
21160 types = add_exception_specifier (types, type, /*complain=*/1);
21161 /* Peek at the next token. */
21162 token = cp_lexer_peek_token (parser->lexer);
21163 /* If it is not a `,', we are done. */
21164 if (token->type != CPP_COMMA)
21165 break;
21166 /* Consume the `,'. */
21167 cp_lexer_consume_token (parser->lexer);
21170 return nreverse (types);
21173 /* Parse a try-block.
21175 try-block:
21176 try compound-statement handler-seq */
21178 static tree
21179 cp_parser_try_block (cp_parser* parser)
21181 tree try_block;
21183 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21184 try_block = begin_try_block ();
21185 cp_parser_compound_statement (parser, NULL, true, false);
21186 finish_try_block (try_block);
21187 cp_parser_handler_seq (parser);
21188 finish_handler_sequence (try_block);
21190 return try_block;
21193 /* Parse a function-try-block.
21195 function-try-block:
21196 try ctor-initializer [opt] function-body handler-seq */
21198 static bool
21199 cp_parser_function_try_block (cp_parser* parser)
21201 tree compound_stmt;
21202 tree try_block;
21203 bool ctor_initializer_p;
21205 /* Look for the `try' keyword. */
21206 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21207 return false;
21208 /* Let the rest of the front end know where we are. */
21209 try_block = begin_function_try_block (&compound_stmt);
21210 /* Parse the function-body. */
21211 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21212 (parser, /*in_function_try_block=*/true);
21213 /* We're done with the `try' part. */
21214 finish_function_try_block (try_block);
21215 /* Parse the handlers. */
21216 cp_parser_handler_seq (parser);
21217 /* We're done with the handlers. */
21218 finish_function_handler_sequence (try_block, compound_stmt);
21220 return ctor_initializer_p;
21223 /* Parse a handler-seq.
21225 handler-seq:
21226 handler handler-seq [opt] */
21228 static void
21229 cp_parser_handler_seq (cp_parser* parser)
21231 while (true)
21233 cp_token *token;
21235 /* Parse the handler. */
21236 cp_parser_handler (parser);
21237 /* Peek at the next token. */
21238 token = cp_lexer_peek_token (parser->lexer);
21239 /* If it's not `catch' then there are no more handlers. */
21240 if (!cp_parser_is_keyword (token, RID_CATCH))
21241 break;
21245 /* Parse a handler.
21247 handler:
21248 catch ( exception-declaration ) compound-statement */
21250 static void
21251 cp_parser_handler (cp_parser* parser)
21253 tree handler;
21254 tree declaration;
21256 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21257 handler = begin_handler ();
21258 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21259 declaration = cp_parser_exception_declaration (parser);
21260 finish_handler_parms (declaration, handler);
21261 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21262 cp_parser_compound_statement (parser, NULL, false, false);
21263 finish_handler (handler);
21266 /* Parse an exception-declaration.
21268 exception-declaration:
21269 type-specifier-seq declarator
21270 type-specifier-seq abstract-declarator
21271 type-specifier-seq
21274 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21275 ellipsis variant is used. */
21277 static tree
21278 cp_parser_exception_declaration (cp_parser* parser)
21280 cp_decl_specifier_seq type_specifiers;
21281 cp_declarator *declarator;
21282 const char *saved_message;
21284 /* If it's an ellipsis, it's easy to handle. */
21285 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21287 /* Consume the `...' token. */
21288 cp_lexer_consume_token (parser->lexer);
21289 return NULL_TREE;
21292 /* Types may not be defined in exception-declarations. */
21293 saved_message = parser->type_definition_forbidden_message;
21294 parser->type_definition_forbidden_message
21295 = G_("types may not be defined in exception-declarations");
21297 /* Parse the type-specifier-seq. */
21298 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21299 /*is_trailing_return=*/false,
21300 &type_specifiers);
21301 /* If it's a `)', then there is no declarator. */
21302 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21303 declarator = NULL;
21304 else
21305 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21306 /*ctor_dtor_or_conv_p=*/NULL,
21307 /*parenthesized_p=*/NULL,
21308 /*member_p=*/false);
21310 /* Restore the saved message. */
21311 parser->type_definition_forbidden_message = saved_message;
21313 if (!type_specifiers.any_specifiers_p)
21314 return error_mark_node;
21316 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21319 /* Parse a throw-expression.
21321 throw-expression:
21322 throw assignment-expression [opt]
21324 Returns a THROW_EXPR representing the throw-expression. */
21326 static tree
21327 cp_parser_throw_expression (cp_parser* parser)
21329 tree expression;
21330 cp_token* token;
21332 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21333 token = cp_lexer_peek_token (parser->lexer);
21334 /* Figure out whether or not there is an assignment-expression
21335 following the "throw" keyword. */
21336 if (token->type == CPP_COMMA
21337 || token->type == CPP_SEMICOLON
21338 || token->type == CPP_CLOSE_PAREN
21339 || token->type == CPP_CLOSE_SQUARE
21340 || token->type == CPP_CLOSE_BRACE
21341 || token->type == CPP_COLON)
21342 expression = NULL_TREE;
21343 else
21344 expression = cp_parser_assignment_expression (parser,
21345 /*cast_p=*/false, NULL);
21347 return build_throw (expression);
21350 /* GNU Extensions */
21352 /* Parse an (optional) asm-specification.
21354 asm-specification:
21355 asm ( string-literal )
21357 If the asm-specification is present, returns a STRING_CST
21358 corresponding to the string-literal. Otherwise, returns
21359 NULL_TREE. */
21361 static tree
21362 cp_parser_asm_specification_opt (cp_parser* parser)
21364 cp_token *token;
21365 tree asm_specification;
21367 /* Peek at the next token. */
21368 token = cp_lexer_peek_token (parser->lexer);
21369 /* If the next token isn't the `asm' keyword, then there's no
21370 asm-specification. */
21371 if (!cp_parser_is_keyword (token, RID_ASM))
21372 return NULL_TREE;
21374 /* Consume the `asm' token. */
21375 cp_lexer_consume_token (parser->lexer);
21376 /* Look for the `('. */
21377 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21379 /* Look for the string-literal. */
21380 asm_specification = cp_parser_string_literal (parser, false, false);
21382 /* Look for the `)'. */
21383 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21385 return asm_specification;
21388 /* Parse an asm-operand-list.
21390 asm-operand-list:
21391 asm-operand
21392 asm-operand-list , asm-operand
21394 asm-operand:
21395 string-literal ( expression )
21396 [ string-literal ] string-literal ( expression )
21398 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21399 each node is the expression. The TREE_PURPOSE is itself a
21400 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21401 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21402 is a STRING_CST for the string literal before the parenthesis. Returns
21403 ERROR_MARK_NODE if any of the operands are invalid. */
21405 static tree
21406 cp_parser_asm_operand_list (cp_parser* parser)
21408 tree asm_operands = NULL_TREE;
21409 bool invalid_operands = false;
21411 while (true)
21413 tree string_literal;
21414 tree expression;
21415 tree name;
21417 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21419 /* Consume the `[' token. */
21420 cp_lexer_consume_token (parser->lexer);
21421 /* Read the operand name. */
21422 name = cp_parser_identifier (parser);
21423 if (name != error_mark_node)
21424 name = build_string (IDENTIFIER_LENGTH (name),
21425 IDENTIFIER_POINTER (name));
21426 /* Look for the closing `]'. */
21427 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21429 else
21430 name = NULL_TREE;
21431 /* Look for the string-literal. */
21432 string_literal = cp_parser_string_literal (parser, false, false);
21434 /* Look for the `('. */
21435 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21436 /* Parse the expression. */
21437 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21438 /* Look for the `)'. */
21439 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21441 if (name == error_mark_node
21442 || string_literal == error_mark_node
21443 || expression == error_mark_node)
21444 invalid_operands = true;
21446 /* Add this operand to the list. */
21447 asm_operands = tree_cons (build_tree_list (name, string_literal),
21448 expression,
21449 asm_operands);
21450 /* If the next token is not a `,', there are no more
21451 operands. */
21452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21453 break;
21454 /* Consume the `,'. */
21455 cp_lexer_consume_token (parser->lexer);
21458 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21461 /* Parse an asm-clobber-list.
21463 asm-clobber-list:
21464 string-literal
21465 asm-clobber-list , string-literal
21467 Returns a TREE_LIST, indicating the clobbers in the order that they
21468 appeared. The TREE_VALUE of each node is a STRING_CST. */
21470 static tree
21471 cp_parser_asm_clobber_list (cp_parser* parser)
21473 tree clobbers = NULL_TREE;
21475 while (true)
21477 tree string_literal;
21479 /* Look for the string literal. */
21480 string_literal = cp_parser_string_literal (parser, false, false);
21481 /* Add it to the list. */
21482 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21483 /* If the next token is not a `,', then the list is
21484 complete. */
21485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21486 break;
21487 /* Consume the `,' token. */
21488 cp_lexer_consume_token (parser->lexer);
21491 return clobbers;
21494 /* Parse an asm-label-list.
21496 asm-label-list:
21497 identifier
21498 asm-label-list , identifier
21500 Returns a TREE_LIST, indicating the labels in the order that they
21501 appeared. The TREE_VALUE of each node is a label. */
21503 static tree
21504 cp_parser_asm_label_list (cp_parser* parser)
21506 tree labels = NULL_TREE;
21508 while (true)
21510 tree identifier, label, name;
21512 /* Look for the identifier. */
21513 identifier = cp_parser_identifier (parser);
21514 if (!error_operand_p (identifier))
21516 label = lookup_label (identifier);
21517 if (TREE_CODE (label) == LABEL_DECL)
21519 TREE_USED (label) = 1;
21520 check_goto (label);
21521 name = build_string (IDENTIFIER_LENGTH (identifier),
21522 IDENTIFIER_POINTER (identifier));
21523 labels = tree_cons (name, label, labels);
21526 /* If the next token is not a `,', then the list is
21527 complete. */
21528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21529 break;
21530 /* Consume the `,' token. */
21531 cp_lexer_consume_token (parser->lexer);
21534 return nreverse (labels);
21537 /* Return TRUE iff the next tokens in the stream are possibly the
21538 beginning of a GNU extension attribute. */
21540 static bool
21541 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21543 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21546 /* Return TRUE iff the next tokens in the stream are possibly the
21547 beginning of a standard C++-11 attribute specifier. */
21549 static bool
21550 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21552 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21555 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21556 beginning of a standard C++-11 attribute specifier. */
21558 static bool
21559 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21561 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21563 return (cxx_dialect >= cxx11
21564 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21565 || (token->type == CPP_OPEN_SQUARE
21566 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21567 && token->type == CPP_OPEN_SQUARE)));
21570 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21571 beginning of a GNU extension attribute. */
21573 static bool
21574 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21576 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21578 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21581 /* Return true iff the next tokens can be the beginning of either a
21582 GNU attribute list, or a standard C++11 attribute sequence. */
21584 static bool
21585 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21587 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21588 || cp_next_tokens_can_be_std_attribute_p (parser));
21591 /* Return true iff the next Nth tokens can be the beginning of either
21592 a GNU attribute list, or a standard C++11 attribute sequence. */
21594 static bool
21595 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21597 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21598 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21601 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21602 of GNU attributes, or return NULL. */
21604 static tree
21605 cp_parser_attributes_opt (cp_parser *parser)
21607 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21608 return cp_parser_gnu_attributes_opt (parser);
21609 return cp_parser_std_attribute_spec_seq (parser);
21612 /* Parse an (optional) series of attributes.
21614 attributes:
21615 attributes attribute
21617 attribute:
21618 __attribute__ (( attribute-list [opt] ))
21620 The return value is as for cp_parser_gnu_attribute_list. */
21622 static tree
21623 cp_parser_gnu_attributes_opt (cp_parser* parser)
21625 tree attributes = NULL_TREE;
21627 while (true)
21629 cp_token *token;
21630 tree attribute_list;
21631 bool ok = true;
21633 /* Peek at the next token. */
21634 token = cp_lexer_peek_token (parser->lexer);
21635 /* If it's not `__attribute__', then we're done. */
21636 if (token->keyword != RID_ATTRIBUTE)
21637 break;
21639 /* Consume the `__attribute__' keyword. */
21640 cp_lexer_consume_token (parser->lexer);
21641 /* Look for the two `(' tokens. */
21642 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21643 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21645 /* Peek at the next token. */
21646 token = cp_lexer_peek_token (parser->lexer);
21647 if (token->type != CPP_CLOSE_PAREN)
21648 /* Parse the attribute-list. */
21649 attribute_list = cp_parser_gnu_attribute_list (parser);
21650 else
21651 /* If the next token is a `)', then there is no attribute
21652 list. */
21653 attribute_list = NULL;
21655 /* Look for the two `)' tokens. */
21656 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21657 ok = false;
21658 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21659 ok = false;
21660 if (!ok)
21661 cp_parser_skip_to_end_of_statement (parser);
21663 /* Add these new attributes to the list. */
21664 attributes = chainon (attributes, attribute_list);
21667 return attributes;
21670 /* Parse a GNU attribute-list.
21672 attribute-list:
21673 attribute
21674 attribute-list , attribute
21676 attribute:
21677 identifier
21678 identifier ( identifier )
21679 identifier ( identifier , expression-list )
21680 identifier ( expression-list )
21682 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21683 to an attribute. The TREE_PURPOSE of each node is the identifier
21684 indicating which attribute is in use. The TREE_VALUE represents
21685 the arguments, if any. */
21687 static tree
21688 cp_parser_gnu_attribute_list (cp_parser* parser)
21690 tree attribute_list = NULL_TREE;
21691 bool save_translate_strings_p = parser->translate_strings_p;
21693 parser->translate_strings_p = false;
21694 while (true)
21696 cp_token *token;
21697 tree identifier;
21698 tree attribute;
21700 /* Look for the identifier. We also allow keywords here; for
21701 example `__attribute__ ((const))' is legal. */
21702 token = cp_lexer_peek_token (parser->lexer);
21703 if (token->type == CPP_NAME
21704 || token->type == CPP_KEYWORD)
21706 tree arguments = NULL_TREE;
21708 /* Consume the token. */
21709 token = cp_lexer_consume_token (parser->lexer);
21711 /* Save away the identifier that indicates which attribute
21712 this is. */
21713 identifier = (token->type == CPP_KEYWORD)
21714 /* For keywords, use the canonical spelling, not the
21715 parsed identifier. */
21716 ? ridpointers[(int) token->keyword]
21717 : token->u.value;
21719 attribute = build_tree_list (identifier, NULL_TREE);
21721 /* Peek at the next token. */
21722 token = cp_lexer_peek_token (parser->lexer);
21723 /* If it's an `(', then parse the attribute arguments. */
21724 if (token->type == CPP_OPEN_PAREN)
21726 vec<tree, va_gc> *vec;
21727 int attr_flag = (attribute_takes_identifier_p (identifier)
21728 ? id_attr : normal_attr);
21729 vec = cp_parser_parenthesized_expression_list
21730 (parser, attr_flag, /*cast_p=*/false,
21731 /*allow_expansion_p=*/false,
21732 /*non_constant_p=*/NULL);
21733 if (vec == NULL)
21734 arguments = error_mark_node;
21735 else
21737 arguments = build_tree_list_vec (vec);
21738 release_tree_vector (vec);
21740 /* Save the arguments away. */
21741 TREE_VALUE (attribute) = arguments;
21744 if (arguments != error_mark_node)
21746 /* Add this attribute to the list. */
21747 TREE_CHAIN (attribute) = attribute_list;
21748 attribute_list = attribute;
21751 token = cp_lexer_peek_token (parser->lexer);
21753 /* Now, look for more attributes. If the next token isn't a
21754 `,', we're done. */
21755 if (token->type != CPP_COMMA)
21756 break;
21758 /* Consume the comma and keep going. */
21759 cp_lexer_consume_token (parser->lexer);
21761 parser->translate_strings_p = save_translate_strings_p;
21763 /* We built up the list in reverse order. */
21764 return nreverse (attribute_list);
21767 /* Parse a standard C++11 attribute.
21769 The returned representation is a TREE_LIST which TREE_PURPOSE is
21770 the scoped name of the attribute, and the TREE_VALUE is its
21771 arguments list.
21773 Note that the scoped name of the attribute is itself a TREE_LIST
21774 which TREE_PURPOSE is the namespace of the attribute, and
21775 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21776 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21777 and which TREE_PURPOSE is directly the attribute name.
21779 Clients of the attribute code should use get_attribute_namespace
21780 and get_attribute_name to get the actual namespace and name of
21781 attributes, regardless of their being GNU or C++11 attributes.
21783 attribute:
21784 attribute-token attribute-argument-clause [opt]
21786 attribute-token:
21787 identifier
21788 attribute-scoped-token
21790 attribute-scoped-token:
21791 attribute-namespace :: identifier
21793 attribute-namespace:
21794 identifier
21796 attribute-argument-clause:
21797 ( balanced-token-seq )
21799 balanced-token-seq:
21800 balanced-token [opt]
21801 balanced-token-seq balanced-token
21803 balanced-token:
21804 ( balanced-token-seq )
21805 [ balanced-token-seq ]
21806 { balanced-token-seq }. */
21808 static tree
21809 cp_parser_std_attribute (cp_parser *parser)
21811 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21812 cp_token *token;
21814 /* First, parse name of the the attribute, a.k.a
21815 attribute-token. */
21817 token = cp_lexer_peek_token (parser->lexer);
21818 if (token->type == CPP_NAME)
21819 attr_id = token->u.value;
21820 else if (token->type == CPP_KEYWORD)
21821 attr_id = ridpointers[(int) token->keyword];
21822 else if (token->flags & NAMED_OP)
21823 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21825 if (attr_id == NULL_TREE)
21826 return NULL_TREE;
21828 cp_lexer_consume_token (parser->lexer);
21830 token = cp_lexer_peek_token (parser->lexer);
21831 if (token->type == CPP_SCOPE)
21833 /* We are seeing a scoped attribute token. */
21835 cp_lexer_consume_token (parser->lexer);
21836 attr_ns = attr_id;
21838 token = cp_lexer_consume_token (parser->lexer);
21839 if (token->type == CPP_NAME)
21840 attr_id = token->u.value;
21841 else if (token->type == CPP_KEYWORD)
21842 attr_id = ridpointers[(int) token->keyword];
21843 else
21845 error_at (token->location,
21846 "expected an identifier for the attribute name");
21847 return error_mark_node;
21849 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21850 NULL_TREE);
21851 token = cp_lexer_peek_token (parser->lexer);
21853 else
21855 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21856 NULL_TREE);
21857 /* C++11 noreturn attribute is equivalent to GNU's. */
21858 if (is_attribute_p ("noreturn", attr_id))
21859 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21860 /* C++14 deprecated attribute is equivalent to GNU's. */
21861 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21862 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21865 /* Now parse the optional argument clause of the attribute. */
21867 if (token->type != CPP_OPEN_PAREN)
21868 return attribute;
21871 vec<tree, va_gc> *vec;
21872 int attr_flag = normal_attr;
21874 if (attr_ns == get_identifier ("gnu")
21875 && attribute_takes_identifier_p (attr_id))
21876 /* A GNU attribute that takes an identifier in parameter. */
21877 attr_flag = id_attr;
21879 vec = cp_parser_parenthesized_expression_list
21880 (parser, attr_flag, /*cast_p=*/false,
21881 /*allow_expansion_p=*/true,
21882 /*non_constant_p=*/NULL);
21883 if (vec == NULL)
21884 arguments = error_mark_node;
21885 else
21887 arguments = build_tree_list_vec (vec);
21888 release_tree_vector (vec);
21891 if (arguments == error_mark_node)
21892 attribute = error_mark_node;
21893 else
21894 TREE_VALUE (attribute) = arguments;
21897 return attribute;
21900 /* Parse a list of standard C++-11 attributes.
21902 attribute-list:
21903 attribute [opt]
21904 attribute-list , attribute[opt]
21905 attribute ...
21906 attribute-list , attribute ...
21909 static tree
21910 cp_parser_std_attribute_list (cp_parser *parser)
21912 tree attributes = NULL_TREE, attribute = NULL_TREE;
21913 cp_token *token = NULL;
21915 while (true)
21917 attribute = cp_parser_std_attribute (parser);
21918 if (attribute == error_mark_node)
21919 break;
21920 if (attribute != NULL_TREE)
21922 TREE_CHAIN (attribute) = attributes;
21923 attributes = attribute;
21925 token = cp_lexer_peek_token (parser->lexer);
21926 if (token->type != CPP_COMMA)
21927 break;
21928 cp_lexer_consume_token (parser->lexer);
21930 attributes = nreverse (attributes);
21931 return attributes;
21934 /* Parse a standard C++-11 attribute specifier.
21936 attribute-specifier:
21937 [ [ attribute-list ] ]
21938 alignment-specifier
21940 alignment-specifier:
21941 alignas ( type-id ... [opt] )
21942 alignas ( alignment-expression ... [opt] ). */
21944 static tree
21945 cp_parser_std_attribute_spec (cp_parser *parser)
21947 tree attributes = NULL_TREE;
21948 cp_token *token = cp_lexer_peek_token (parser->lexer);
21950 if (token->type == CPP_OPEN_SQUARE
21951 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21953 cp_lexer_consume_token (parser->lexer);
21954 cp_lexer_consume_token (parser->lexer);
21956 attributes = cp_parser_std_attribute_list (parser);
21958 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21959 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21960 cp_parser_skip_to_end_of_statement (parser);
21961 else
21962 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21963 when we are sure that we have actually parsed them. */
21964 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21966 else
21968 tree alignas_expr;
21970 /* Look for an alignment-specifier. */
21972 token = cp_lexer_peek_token (parser->lexer);
21974 if (token->type != CPP_KEYWORD
21975 || token->keyword != RID_ALIGNAS)
21976 return NULL_TREE;
21978 cp_lexer_consume_token (parser->lexer);
21979 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21981 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21983 cp_parser_error (parser, "expected %<(%>");
21984 return error_mark_node;
21987 cp_parser_parse_tentatively (parser);
21988 alignas_expr = cp_parser_type_id (parser);
21990 if (!cp_parser_parse_definitely (parser))
21992 gcc_assert (alignas_expr == error_mark_node
21993 || alignas_expr == NULL_TREE);
21995 alignas_expr =
21996 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21997 /**cp_id_kind=*/NULL);
21998 if (alignas_expr == error_mark_node)
21999 cp_parser_skip_to_end_of_statement (parser);
22000 if (alignas_expr == NULL_TREE
22001 || alignas_expr == error_mark_node)
22002 return alignas_expr;
22005 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22007 cp_parser_error (parser, "expected %<)%>");
22008 return error_mark_node;
22011 alignas_expr = cxx_alignas_expr (alignas_expr);
22013 /* Build the C++-11 representation of an 'aligned'
22014 attribute. */
22015 attributes =
22016 build_tree_list (build_tree_list (get_identifier ("gnu"),
22017 get_identifier ("aligned")),
22018 build_tree_list (NULL_TREE, alignas_expr));
22021 return attributes;
22024 /* Parse a standard C++-11 attribute-specifier-seq.
22026 attribute-specifier-seq:
22027 attribute-specifier-seq [opt] attribute-specifier
22030 static tree
22031 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22033 tree attr_specs = NULL;
22035 while (true)
22037 tree attr_spec = cp_parser_std_attribute_spec (parser);
22038 if (attr_spec == NULL_TREE)
22039 break;
22040 if (attr_spec == error_mark_node)
22041 return error_mark_node;
22043 TREE_CHAIN (attr_spec) = attr_specs;
22044 attr_specs = attr_spec;
22047 attr_specs = nreverse (attr_specs);
22048 return attr_specs;
22051 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22052 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22053 current value of the PEDANTIC flag, regardless of whether or not
22054 the `__extension__' keyword is present. The caller is responsible
22055 for restoring the value of the PEDANTIC flag. */
22057 static bool
22058 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22060 /* Save the old value of the PEDANTIC flag. */
22061 *saved_pedantic = pedantic;
22063 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22065 /* Consume the `__extension__' token. */
22066 cp_lexer_consume_token (parser->lexer);
22067 /* We're not being pedantic while the `__extension__' keyword is
22068 in effect. */
22069 pedantic = 0;
22071 return true;
22074 return false;
22077 /* Parse a label declaration.
22079 label-declaration:
22080 __label__ label-declarator-seq ;
22082 label-declarator-seq:
22083 identifier , label-declarator-seq
22084 identifier */
22086 static void
22087 cp_parser_label_declaration (cp_parser* parser)
22089 /* Look for the `__label__' keyword. */
22090 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22092 while (true)
22094 tree identifier;
22096 /* Look for an identifier. */
22097 identifier = cp_parser_identifier (parser);
22098 /* If we failed, stop. */
22099 if (identifier == error_mark_node)
22100 break;
22101 /* Declare it as a label. */
22102 finish_label_decl (identifier);
22103 /* If the next token is a `;', stop. */
22104 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22105 break;
22106 /* Look for the `,' separating the label declarations. */
22107 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22110 /* Look for the final `;'. */
22111 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22114 // -------------------------------------------------------------------------- //
22115 // Requires Clause
22117 // Parse a requires clause.
22119 // requires-clause:
22120 // 'requires' logical-or-expression
22122 // The required logical-or-expression must be a constant expression.
22123 static tree
22124 cp_parser_requires_clause (cp_parser *parser)
22126 // Parse the constant expression.
22127 tree expr =
22128 cp_parser_binary_expression (parser, false, false, PREC_NOT_OPERATOR, NULL);
22129 if (!require_potential_rvalue_constant_expression (expr))
22130 return error_mark_node;
22131 return expr;
22134 // Optionally parse a requires clause:
22136 // requires-clause:
22137 // 'requires' logical-or-expression
22139 // The required logical-or-expression must be a constant expression.
22140 static tree
22141 cp_parser_requires_clause_opt (cp_parser *parser)
22143 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
22144 return NULL_TREE;
22145 cp_lexer_consume_token (parser->lexer);
22146 return cp_parser_requires_clause (parser);
22150 // -------------------------------------------------------------------------- //
22151 // Requires Expression
22154 // An RAII helper that provides scoped control for entering and exiting
22155 // the local scope defined by a requires expression. Name bindings introduced
22156 // within the scope are popped prior to exiting the scope.
22157 struct cp_parser_requires_expr_scope
22159 // Enter a scope of kind K belonging to the decl D.
22160 cp_parser_requires_expr_scope ()
22162 begin_scope (sk_block, NULL_TREE);
22165 ~cp_parser_requires_expr_scope ()
22167 for (tree t = current_binding_level->names; t; t = DECL_CHAIN (t))
22168 pop_binding (DECL_NAME (t), t);
22169 leave_scope ();
22173 // Parse a requires expression
22175 // requirement-expression:
22176 // 'requires' requirement-parameter-list requirement-body
22177 static tree
22178 cp_parser_requires_expression (cp_parser *parser)
22180 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
22181 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
22183 // TODO: Earlier versions of the concepts lite spec did not allow
22184 // requires expressions outside of template declarations. That
22185 // restriction was relaxed in Chicago, but it has not been implemented.
22186 if (!processing_template_decl)
22188 error_at (loc, "a requires expression cannot appear outside a template");
22189 cp_parser_skip_to_end_of_statement (parser);
22190 return error_mark_node;
22194 // TODO: Check that requires expressions are only written inside of
22195 // template declarations. They don't need to be concepts, just templates.
22197 // Parse the optional parameter list. Any local parameter declarations
22198 // are added to a new scope and are visible within the nested
22199 // requirement list.
22200 cp_parser_requires_expr_scope guard;
22201 tree parms = cp_parser_requirement_parameter_list (parser);
22202 if (parms == error_mark_node)
22203 return error_mark_node;
22205 // Parse the requirement body.
22206 tree reqs = cp_parser_requirement_body (parser);
22207 if (reqs == error_mark_node)
22208 return error_mark_node;
22210 return finish_requires_expr (parms, reqs);
22213 // Parse a parameterized requirement.
22215 // requirement-parameter-list:
22216 // '(' parameter-declaration-clause ')'
22217 static tree
22218 cp_parser_requirement_parameter_list (cp_parser *parser)
22220 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22221 return error_mark_node;
22223 // Parse the nested parameter declaration clause.
22224 tree parms = cp_parser_parameter_declaration_clause (parser);
22225 if (parms == error_mark_node)
22226 return error_mark_node;
22228 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22229 return error_mark_node;
22231 return parms;
22234 // Parse the body of a requirement.
22236 // requirement-body:
22237 // '{' requirement-list '}'
22238 static tree
22239 cp_parser_requirement_body (cp_parser *parser)
22241 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22242 return error_mark_node;
22244 tree reqs = cp_parser_requirement_list (parser);
22246 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
22247 return error_mark_node;
22249 return reqs;
22252 // Parse a list of requirements.
22254 // requirement-list:
22255 // requirement
22256 // requirement-list ';' requirement[opt]
22257 static tree
22258 cp_parser_requirement_list (cp_parser *parser)
22260 tree result = NULL_TREE;
22261 while (true)
22263 tree req = cp_parser_requirement (parser);
22264 if (req == error_mark_node)
22265 return req;
22267 result = tree_cons (NULL_TREE, req, result);
22269 // If we see a semi-colon, consume it.
22270 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22271 cp_lexer_consume_token (parser->lexer);
22273 // If we've found the end of the list, stop processing
22274 // the list.
22275 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22276 break;
22279 // Reverse the order of requirements so they are analyzed in
22280 // declaration order.
22281 return nreverse (result);
22284 // Parse a syntactic requirement or type requirement.
22286 // requirement:
22287 // simple-requirement
22288 // compound-requirement
22289 // type-requirement
22290 // nested-requirement
22291 static tree
22292 cp_parser_requirement (cp_parser *parser)
22294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
22295 return cp_parser_nested_requirement (parser);
22297 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22298 return cp_parser_compound_requirement (parser);
22300 // Try parsing a type requirement first.
22301 cp_parser_parse_tentatively (parser);
22302 tree req = cp_parser_type_requirement (parser);
22303 if (!cp_parser_parse_definitely (parser))
22304 req = cp_parser_simple_requirement (parser);
22305 return req;
22308 // Parse a nested requirement. This is the same as a requires clause.
22310 // nested-requirement:
22311 // requires-clause
22312 static tree
22313 cp_parser_nested_requirement (cp_parser *parser)
22315 cp_lexer_consume_token (parser->lexer);
22316 tree req = cp_parser_requires_clause (parser);
22317 if (req == error_mark_node)
22318 return error_mark_node;
22319 return finish_nested_requirement (req);
22322 // Parse a simple requirement.
22324 // simple-requirement:
22325 // expression
22326 static tree
22327 cp_parser_simple_requirement (cp_parser *parser)
22329 tree expr = cp_parser_expression (parser, false, false, NULL);
22330 if (!expr || expr == error_mark_node)
22331 return error_mark_node;
22332 return finish_expr_requirement (expr);
22335 // Parse a compound requirement
22337 // compound-requirement:
22338 // '{' expression '}' trailing-constraint-specifiers
22340 // trailing-constraint-specifiers:
22341 // constraint-specifiers-seq[opt] result-type-requirement[opt]
22343 // result-type-requirement:
22344 // '->' type-id
22345 static tree
22346 cp_parser_compound_requirement (cp_parser *parser)
22348 // Parse an expression enclosed in '{ }'s.
22349 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22350 return error_mark_node;
22352 tree expr = cp_parser_expression (parser, false, false, NULL);
22353 if (!expr || expr == error_mark_node)
22354 return error_mark_node;
22356 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
22357 return error_mark_node;
22359 // Parse trailing expression specifiers.
22360 tree cs = cp_parser_constraint_specifier_seq (parser, expr);
22362 // Parse the optional trailing type requirement.
22363 tree type = NULL_TREE;
22364 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
22366 cp_lexer_consume_token (parser->lexer);
22367 type = cp_parser_trailing_type_id (parser);
22368 if (type == error_mark_node)
22369 return error_mark_node;
22372 return finish_expr_requirement (expr, type, cs);
22375 // Parse a type requirement
22377 // type-requirement
22378 // type-id
22379 static tree
22380 cp_parser_type_requirement (cp_parser *parser)
22382 // Try parsing the type-id
22383 tree type = cp_parser_type_id (parser);
22384 if (type == error_mark_node)
22385 return error_mark_node;
22387 // It can only be a type requirement if nothing comes after it.
22388 // For example, this:
22390 // typename T::value_type x = a;
22392 // Is not a type requirement even though it stars with a type-id.
22393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22394 return error_mark_node;
22396 return finish_type_requirement (type);
22399 // Parse an optional constexpr specifier in a constraint expression.
22400 static tree
22401 cp_parser_constexpr_constraint_spec (cp_parser *parser, tree expr)
22403 cp_lexer_consume_token (parser->lexer);
22404 return finish_constexpr_requirement (expr);
22407 // Parse an optional noexcept specifier in a constraint expression.
22408 static tree
22409 cp_parser_noexcept_constraint_spec (cp_parser *parser, tree expr)
22411 cp_lexer_consume_token (parser->lexer);
22412 return finish_noexcept_requirement (expr);
22415 static tree
22416 cp_parser_constraint_spec (cp_parser *parser, tree expr)
22418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONSTEXPR))
22419 return cp_parser_constexpr_constraint_spec (parser, expr);
22420 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
22421 return cp_parser_noexcept_constraint_spec (parser, expr);
22422 return NULL_TREE;
22425 // Parse an optional expression specifier sequence.
22427 // constraint-specifier-sequence:
22428 // constexpr [opt] noexcept [opt]
22429 static tree
22430 cp_parser_constraint_specifier_seq (cp_parser *parser, tree expr)
22432 tree result = NULL_TREE;
22433 while (1)
22435 // If we can parse a constraint specifier, insert it into
22436 // the list of requirements.
22437 if (tree spec = cp_parser_constraint_spec (parser, expr))
22439 result = tree_cons (NULL_TREE, spec, result);
22440 continue;
22442 break;
22444 return result;
22447 /* Support Functions */
22449 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22450 NAME should have one of the representations used for an
22451 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22452 is returned. If PARSER->SCOPE is a dependent type, then a
22453 SCOPE_REF is returned.
22455 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22456 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22457 was formed. Abstractly, such entities should not be passed to this
22458 function, because they do not need to be looked up, but it is
22459 simpler to check for this special case here, rather than at the
22460 call-sites.
22462 In cases not explicitly covered above, this function returns a
22463 DECL, OVERLOAD, or baselink representing the result of the lookup.
22464 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22465 is returned.
22467 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22468 (e.g., "struct") that was used. In that case bindings that do not
22469 refer to types are ignored.
22471 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22472 ignored.
22474 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22475 are ignored.
22477 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22478 types.
22480 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22481 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22482 NULL_TREE otherwise. */
22484 static tree
22485 cp_parser_lookup_name (cp_parser *parser, tree name,
22486 enum tag_types tag_type,
22487 bool is_template,
22488 bool is_namespace,
22489 bool check_dependency,
22490 tree *ambiguous_decls,
22491 location_t name_location)
22493 tree decl;
22494 tree object_type = parser->context->object_type;
22496 /* Assume that the lookup will be unambiguous. */
22497 if (ambiguous_decls)
22498 *ambiguous_decls = NULL_TREE;
22500 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22501 no longer valid. Note that if we are parsing tentatively, and
22502 the parse fails, OBJECT_TYPE will be automatically restored. */
22503 parser->context->object_type = NULL_TREE;
22505 if (name == error_mark_node)
22506 return error_mark_node;
22508 /* A template-id has already been resolved; there is no lookup to
22509 do. */
22510 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22511 return name;
22512 if (BASELINK_P (name))
22514 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22515 == TEMPLATE_ID_EXPR);
22516 return name;
22519 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22520 it should already have been checked to make sure that the name
22521 used matches the type being destroyed. */
22522 if (TREE_CODE (name) == BIT_NOT_EXPR)
22524 tree type;
22526 /* Figure out to which type this destructor applies. */
22527 if (parser->scope)
22528 type = parser->scope;
22529 else if (object_type)
22530 type = object_type;
22531 else
22532 type = current_class_type;
22533 /* If that's not a class type, there is no destructor. */
22534 if (!type || !CLASS_TYPE_P (type))
22535 return error_mark_node;
22536 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22537 lazily_declare_fn (sfk_destructor, type);
22538 if (!CLASSTYPE_DESTRUCTORS (type))
22539 return error_mark_node;
22540 /* If it was a class type, return the destructor. */
22541 return CLASSTYPE_DESTRUCTORS (type);
22544 /* By this point, the NAME should be an ordinary identifier. If
22545 the id-expression was a qualified name, the qualifying scope is
22546 stored in PARSER->SCOPE at this point. */
22547 gcc_assert (identifier_p (name));
22549 /* Perform the lookup. */
22550 if (parser->scope)
22552 bool dependent_p;
22554 if (parser->scope == error_mark_node)
22555 return error_mark_node;
22557 /* If the SCOPE is dependent, the lookup must be deferred until
22558 the template is instantiated -- unless we are explicitly
22559 looking up names in uninstantiated templates. Even then, we
22560 cannot look up the name if the scope is not a class type; it
22561 might, for example, be a template type parameter. */
22562 dependent_p = (TYPE_P (parser->scope)
22563 && dependent_scope_p (parser->scope));
22564 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22565 && dependent_p)
22566 /* Defer lookup. */
22567 decl = error_mark_node;
22568 else
22570 tree pushed_scope = NULL_TREE;
22572 /* If PARSER->SCOPE is a dependent type, then it must be a
22573 class type, and we must not be checking dependencies;
22574 otherwise, we would have processed this lookup above. So
22575 that PARSER->SCOPE is not considered a dependent base by
22576 lookup_member, we must enter the scope here. */
22577 if (dependent_p)
22578 pushed_scope = push_scope (parser->scope);
22580 /* If the PARSER->SCOPE is a template specialization, it
22581 may be instantiated during name lookup. In that case,
22582 errors may be issued. Even if we rollback the current
22583 tentative parse, those errors are valid. */
22584 decl = lookup_qualified_name (parser->scope, name,
22585 tag_type != none_type,
22586 /*complain=*/true);
22588 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22589 lookup result and the nested-name-specifier nominates a class C:
22590 * if the name specified after the nested-name-specifier, when
22591 looked up in C, is the injected-class-name of C (Clause 9), or
22592 * if the name specified after the nested-name-specifier is the
22593 same as the identifier or the simple-template-id's template-
22594 name in the last component of the nested-name-specifier,
22595 the name is instead considered to name the constructor of
22596 class C. [ Note: for example, the constructor is not an
22597 acceptable lookup result in an elaborated-type-specifier so
22598 the constructor would not be used in place of the
22599 injected-class-name. --end note ] Such a constructor name
22600 shall be used only in the declarator-id of a declaration that
22601 names a constructor or in a using-declaration. */
22602 if (tag_type == none_type
22603 && DECL_SELF_REFERENCE_P (decl)
22604 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22605 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22606 tag_type != none_type,
22607 /*complain=*/true);
22609 /* If we have a single function from a using decl, pull it out. */
22610 if (TREE_CODE (decl) == OVERLOAD
22611 && !really_overloaded_fn (decl))
22612 decl = OVL_FUNCTION (decl);
22614 if (pushed_scope)
22615 pop_scope (pushed_scope);
22618 /* If the scope is a dependent type and either we deferred lookup or
22619 we did lookup but didn't find the name, rememeber the name. */
22620 if (decl == error_mark_node && TYPE_P (parser->scope)
22621 && dependent_type_p (parser->scope))
22623 if (tag_type)
22625 tree type;
22627 /* The resolution to Core Issue 180 says that `struct
22628 A::B' should be considered a type-name, even if `A'
22629 is dependent. */
22630 type = make_typename_type (parser->scope, name, tag_type,
22631 /*complain=*/tf_error);
22632 if (type != error_mark_node)
22633 decl = TYPE_NAME (type);
22635 else if (is_template
22636 && (cp_parser_next_token_ends_template_argument_p (parser)
22637 || cp_lexer_next_token_is (parser->lexer,
22638 CPP_CLOSE_PAREN)))
22639 decl = make_unbound_class_template (parser->scope,
22640 name, NULL_TREE,
22641 /*complain=*/tf_error);
22642 else
22643 decl = build_qualified_name (/*type=*/NULL_TREE,
22644 parser->scope, name,
22645 is_template);
22647 parser->qualifying_scope = parser->scope;
22648 parser->object_scope = NULL_TREE;
22650 else if (object_type)
22652 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22653 OBJECT_TYPE is not a class. */
22654 if (CLASS_TYPE_P (object_type))
22655 /* If the OBJECT_TYPE is a template specialization, it may
22656 be instantiated during name lookup. In that case, errors
22657 may be issued. Even if we rollback the current tentative
22658 parse, those errors are valid. */
22659 decl = lookup_member (object_type,
22660 name,
22661 /*protect=*/0,
22662 tag_type != none_type,
22663 tf_warning_or_error);
22664 else
22665 decl = NULL_TREE;
22667 if (!decl)
22668 /* Look it up in the enclosing context. */
22669 decl = lookup_name_real (name, tag_type != none_type,
22670 /*nonclass=*/0,
22671 /*block_p=*/true, is_namespace, 0);
22672 parser->object_scope = object_type;
22673 parser->qualifying_scope = NULL_TREE;
22675 else
22677 decl = lookup_name_real (name, tag_type != none_type,
22678 /*nonclass=*/0,
22679 /*block_p=*/true, is_namespace, 0);
22680 parser->qualifying_scope = NULL_TREE;
22681 parser->object_scope = NULL_TREE;
22684 /* If the lookup failed, let our caller know. */
22685 if (!decl || decl == error_mark_node)
22686 return error_mark_node;
22688 /* Pull out the template from an injected-class-name (or multiple). */
22689 if (is_template)
22690 decl = maybe_get_template_decl_from_type_decl (decl);
22692 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22693 if (TREE_CODE (decl) == TREE_LIST)
22695 if (ambiguous_decls)
22696 *ambiguous_decls = decl;
22697 /* The error message we have to print is too complicated for
22698 cp_parser_error, so we incorporate its actions directly. */
22699 if (!cp_parser_simulate_error (parser))
22701 error_at (name_location, "reference to %qD is ambiguous",
22702 name);
22703 print_candidates (decl);
22705 return error_mark_node;
22708 gcc_assert (DECL_P (decl)
22709 || TREE_CODE (decl) == OVERLOAD
22710 || TREE_CODE (decl) == SCOPE_REF
22711 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22712 || BASELINK_P (decl));
22714 /* If we have resolved the name of a member declaration, check to
22715 see if the declaration is accessible. When the name resolves to
22716 set of overloaded functions, accessibility is checked when
22717 overload resolution is done.
22719 During an explicit instantiation, access is not checked at all,
22720 as per [temp.explicit]. */
22721 if (DECL_P (decl))
22722 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22724 maybe_record_typedef_use (decl);
22726 return decl;
22729 /* Like cp_parser_lookup_name, but for use in the typical case where
22730 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22731 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22733 static tree
22734 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22736 return cp_parser_lookup_name (parser, name,
22737 none_type,
22738 /*is_template=*/false,
22739 /*is_namespace=*/false,
22740 /*check_dependency=*/true,
22741 /*ambiguous_decls=*/NULL,
22742 location);
22745 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22746 the current context, return the TYPE_DECL. If TAG_NAME_P is
22747 true, the DECL indicates the class being defined in a class-head,
22748 or declared in an elaborated-type-specifier.
22750 Otherwise, return DECL. */
22752 static tree
22753 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22755 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22756 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22758 struct A {
22759 template <typename T> struct B;
22762 template <typename T> struct A::B {};
22764 Similarly, in an elaborated-type-specifier:
22766 namespace N { struct X{}; }
22768 struct A {
22769 template <typename T> friend struct N::X;
22772 However, if the DECL refers to a class type, and we are in
22773 the scope of the class, then the name lookup automatically
22774 finds the TYPE_DECL created by build_self_reference rather
22775 than a TEMPLATE_DECL. For example, in:
22777 template <class T> struct S {
22778 S s;
22781 there is no need to handle such case. */
22783 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22784 return DECL_TEMPLATE_RESULT (decl);
22786 return decl;
22789 /* If too many, or too few, template-parameter lists apply to the
22790 declarator, issue an error message. Returns TRUE if all went well,
22791 and FALSE otherwise. */
22793 static bool
22794 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22795 cp_declarator *declarator,
22796 location_t declarator_location)
22798 switch (declarator->kind)
22800 case cdk_id:
22802 unsigned num_templates = 0;
22803 tree scope = declarator->u.id.qualifying_scope;
22805 if (scope)
22806 num_templates = num_template_headers_for_class (scope);
22807 else if (TREE_CODE (declarator->u.id.unqualified_name)
22808 == TEMPLATE_ID_EXPR)
22809 /* If the DECLARATOR has the form `X<y>' then it uses one
22810 additional level of template parameters. */
22811 ++num_templates;
22813 return cp_parser_check_template_parameters
22814 (parser, num_templates, declarator_location, declarator);
22817 case cdk_function:
22818 case cdk_array:
22819 case cdk_pointer:
22820 case cdk_reference:
22821 case cdk_ptrmem:
22822 return (cp_parser_check_declarator_template_parameters
22823 (parser, declarator->declarator, declarator_location));
22825 case cdk_error:
22826 return true;
22828 default:
22829 gcc_unreachable ();
22831 return false;
22834 /* NUM_TEMPLATES were used in the current declaration. If that is
22835 invalid, return FALSE and issue an error messages. Otherwise,
22836 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22837 declarator and we can print more accurate diagnostics. */
22839 static bool
22840 cp_parser_check_template_parameters (cp_parser* parser,
22841 unsigned num_templates,
22842 location_t location,
22843 cp_declarator *declarator)
22845 /* If there are the same number of template classes and parameter
22846 lists, that's OK. */
22847 if (parser->num_template_parameter_lists == num_templates)
22848 return true;
22849 /* If there are more, but only one more, then we are referring to a
22850 member template. That's OK too. */
22851 if (parser->num_template_parameter_lists == num_templates + 1)
22852 return true;
22853 /* If there are more template classes than parameter lists, we have
22854 something like:
22856 template <class T> void S<T>::R<T>::f (); */
22857 if (parser->num_template_parameter_lists < num_templates)
22859 if (declarator && !current_function_decl)
22860 error_at (location, "specializing member %<%T::%E%> "
22861 "requires %<template<>%> syntax",
22862 declarator->u.id.qualifying_scope,
22863 declarator->u.id.unqualified_name);
22864 else if (declarator)
22865 error_at (location, "invalid declaration of %<%T::%E%>",
22866 declarator->u.id.qualifying_scope,
22867 declarator->u.id.unqualified_name);
22868 else
22869 error_at (location, "too few template-parameter-lists");
22870 return false;
22872 /* Otherwise, there are too many template parameter lists. We have
22873 something like:
22875 template <class T> template <class U> void S::f(); */
22876 error_at (location, "too many template-parameter-lists");
22877 return false;
22880 /* Parse an optional `::' token indicating that the following name is
22881 from the global namespace. If so, PARSER->SCOPE is set to the
22882 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22883 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22884 Returns the new value of PARSER->SCOPE, if the `::' token is
22885 present, and NULL_TREE otherwise. */
22887 static tree
22888 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22890 cp_token *token;
22892 /* Peek at the next token. */
22893 token = cp_lexer_peek_token (parser->lexer);
22894 /* If we're looking at a `::' token then we're starting from the
22895 global namespace, not our current location. */
22896 if (token->type == CPP_SCOPE)
22898 /* Consume the `::' token. */
22899 cp_lexer_consume_token (parser->lexer);
22900 /* Set the SCOPE so that we know where to start the lookup. */
22901 parser->scope = global_namespace;
22902 parser->qualifying_scope = global_namespace;
22903 parser->object_scope = NULL_TREE;
22905 return parser->scope;
22907 else if (!current_scope_valid_p)
22909 parser->scope = NULL_TREE;
22910 parser->qualifying_scope = NULL_TREE;
22911 parser->object_scope = NULL_TREE;
22914 return NULL_TREE;
22917 /* Returns TRUE if the upcoming token sequence is the start of a
22918 constructor declarator. If FRIEND_P is true, the declarator is
22919 preceded by the `friend' specifier. */
22921 static bool
22922 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22924 bool constructor_p;
22925 bool outside_class_specifier_p;
22926 tree nested_name_specifier;
22927 cp_token *next_token;
22929 /* The common case is that this is not a constructor declarator, so
22930 try to avoid doing lots of work if at all possible. It's not
22931 valid declare a constructor at function scope. */
22932 if (parser->in_function_body)
22933 return false;
22934 /* And only certain tokens can begin a constructor declarator. */
22935 next_token = cp_lexer_peek_token (parser->lexer);
22936 if (next_token->type != CPP_NAME
22937 && next_token->type != CPP_SCOPE
22938 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22939 && next_token->type != CPP_TEMPLATE_ID)
22940 return false;
22942 /* Parse tentatively; we are going to roll back all of the tokens
22943 consumed here. */
22944 cp_parser_parse_tentatively (parser);
22945 /* Assume that we are looking at a constructor declarator. */
22946 constructor_p = true;
22948 /* Look for the optional `::' operator. */
22949 cp_parser_global_scope_opt (parser,
22950 /*current_scope_valid_p=*/false);
22951 /* Look for the nested-name-specifier. */
22952 nested_name_specifier
22953 = (cp_parser_nested_name_specifier_opt (parser,
22954 /*typename_keyword_p=*/false,
22955 /*check_dependency_p=*/false,
22956 /*type_p=*/false,
22957 /*is_declaration=*/false));
22959 outside_class_specifier_p = (!at_class_scope_p ()
22960 || !TYPE_BEING_DEFINED (current_class_type)
22961 || friend_p);
22963 /* Outside of a class-specifier, there must be a
22964 nested-name-specifier. */
22965 if (!nested_name_specifier && outside_class_specifier_p)
22966 constructor_p = false;
22967 else if (nested_name_specifier == error_mark_node)
22968 constructor_p = false;
22970 /* If we have a class scope, this is easy; DR 147 says that S::S always
22971 names the constructor, and no other qualified name could. */
22972 if (constructor_p && nested_name_specifier
22973 && CLASS_TYPE_P (nested_name_specifier))
22975 tree id = cp_parser_unqualified_id (parser,
22976 /*template_keyword_p=*/false,
22977 /*check_dependency_p=*/false,
22978 /*declarator_p=*/true,
22979 /*optional_p=*/false);
22980 if (is_overloaded_fn (id))
22981 id = DECL_NAME (get_first_fn (id));
22982 if (!constructor_name_p (id, nested_name_specifier))
22983 constructor_p = false;
22985 /* If we still think that this might be a constructor-declarator,
22986 look for a class-name. */
22987 else if (constructor_p)
22989 /* If we have:
22991 template <typename T> struct S {
22992 S();
22995 we must recognize that the nested `S' names a class. */
22996 tree type_decl;
22997 type_decl = cp_parser_class_name (parser,
22998 /*typename_keyword_p=*/false,
22999 /*template_keyword_p=*/false,
23000 none_type,
23001 /*check_dependency_p=*/false,
23002 /*class_head_p=*/false,
23003 /*is_declaration=*/false);
23004 /* If there was no class-name, then this is not a constructor.
23005 Otherwise, if we are in a class-specifier and we aren't
23006 handling a friend declaration, check that its type matches
23007 current_class_type (c++/38313). Note: error_mark_node
23008 is left alone for error recovery purposes. */
23009 constructor_p = (!cp_parser_error_occurred (parser)
23010 && (outside_class_specifier_p
23011 || type_decl == error_mark_node
23012 || same_type_p (current_class_type,
23013 TREE_TYPE (type_decl))));
23015 /* If we're still considering a constructor, we have to see a `(',
23016 to begin the parameter-declaration-clause, followed by either a
23017 `)', an `...', or a decl-specifier. We need to check for a
23018 type-specifier to avoid being fooled into thinking that:
23020 S (f) (int);
23022 is a constructor. (It is actually a function named `f' that
23023 takes one parameter (of type `int') and returns a value of type
23024 `S'. */
23025 if (constructor_p
23026 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23027 constructor_p = false;
23029 if (constructor_p
23030 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23031 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23032 /* A parameter declaration begins with a decl-specifier,
23033 which is either the "attribute" keyword, a storage class
23034 specifier, or (usually) a type-specifier. */
23035 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23037 tree type;
23038 tree pushed_scope = NULL_TREE;
23039 unsigned saved_num_template_parameter_lists;
23041 /* Names appearing in the type-specifier should be looked up
23042 in the scope of the class. */
23043 if (current_class_type)
23044 type = NULL_TREE;
23045 else
23047 type = TREE_TYPE (type_decl);
23048 if (TREE_CODE (type) == TYPENAME_TYPE)
23050 type = resolve_typename_type (type,
23051 /*only_current_p=*/false);
23052 if (TREE_CODE (type) == TYPENAME_TYPE)
23054 cp_parser_abort_tentative_parse (parser);
23055 return false;
23058 pushed_scope = push_scope (type);
23061 /* Inside the constructor parameter list, surrounding
23062 template-parameter-lists do not apply. */
23063 saved_num_template_parameter_lists
23064 = parser->num_template_parameter_lists;
23065 parser->num_template_parameter_lists = 0;
23067 /* Look for the type-specifier. */
23068 cp_parser_type_specifier (parser,
23069 CP_PARSER_FLAGS_NONE,
23070 /*decl_specs=*/NULL,
23071 /*is_declarator=*/true,
23072 /*declares_class_or_enum=*/NULL,
23073 /*is_cv_qualifier=*/NULL);
23075 parser->num_template_parameter_lists
23076 = saved_num_template_parameter_lists;
23078 /* Leave the scope of the class. */
23079 if (pushed_scope)
23080 pop_scope (pushed_scope);
23082 constructor_p = !cp_parser_error_occurred (parser);
23086 /* We did not really want to consume any tokens. */
23087 cp_parser_abort_tentative_parse (parser);
23089 return constructor_p;
23092 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23093 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23094 they must be performed once we are in the scope of the function.
23096 Returns the function defined. */
23098 static tree
23099 cp_parser_function_definition_from_specifiers_and_declarator
23100 (cp_parser* parser,
23101 cp_decl_specifier_seq *decl_specifiers,
23102 tree attributes,
23103 const cp_declarator *declarator)
23105 tree fn;
23106 bool success_p;
23108 /* Begin the function-definition. */
23109 success_p = start_function (decl_specifiers, declarator, attributes);
23111 /* The things we're about to see are not directly qualified by any
23112 template headers we've seen thus far. */
23113 reset_specialization ();
23115 /* If there were names looked up in the decl-specifier-seq that we
23116 did not check, check them now. We must wait until we are in the
23117 scope of the function to perform the checks, since the function
23118 might be a friend. */
23119 perform_deferred_access_checks (tf_warning_or_error);
23121 if (success_p)
23123 cp_finalize_omp_declare_simd (parser, current_function_decl);
23124 parser->omp_declare_simd = NULL;
23127 if (!success_p)
23129 /* Skip the entire function. */
23130 cp_parser_skip_to_end_of_block_or_statement (parser);
23131 fn = error_mark_node;
23133 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23135 /* Seen already, skip it. An error message has already been output. */
23136 cp_parser_skip_to_end_of_block_or_statement (parser);
23137 fn = current_function_decl;
23138 current_function_decl = NULL_TREE;
23139 /* If this is a function from a class, pop the nested class. */
23140 if (current_class_name)
23141 pop_nested_class ();
23143 else
23145 timevar_id_t tv;
23146 if (DECL_DECLARED_INLINE_P (current_function_decl))
23147 tv = TV_PARSE_INLINE;
23148 else
23149 tv = TV_PARSE_FUNC;
23150 timevar_push (tv);
23151 fn = cp_parser_function_definition_after_declarator (parser,
23152 /*inline_p=*/false);
23153 timevar_pop (tv);
23156 return fn;
23159 /* Parse the part of a function-definition that follows the
23160 declarator. INLINE_P is TRUE iff this function is an inline
23161 function defined within a class-specifier.
23163 Returns the function defined. */
23165 static tree
23166 cp_parser_function_definition_after_declarator (cp_parser* parser,
23167 bool inline_p)
23169 tree fn;
23170 bool ctor_initializer_p = false;
23171 bool saved_in_unbraced_linkage_specification_p;
23172 bool saved_in_function_body;
23173 unsigned saved_num_template_parameter_lists;
23174 cp_token *token;
23175 bool fully_implicit_function_template_p
23176 = parser->fully_implicit_function_template_p;
23177 parser->fully_implicit_function_template_p = false;
23178 tree implicit_template_parms
23179 = parser->implicit_template_parms;
23180 parser->implicit_template_parms = 0;
23181 cp_binding_level* implicit_template_scope
23182 = parser->implicit_template_scope;
23183 parser->implicit_template_scope = 0;
23185 saved_in_function_body = parser->in_function_body;
23186 parser->in_function_body = true;
23187 /* If the next token is `return', then the code may be trying to
23188 make use of the "named return value" extension that G++ used to
23189 support. */
23190 token = cp_lexer_peek_token (parser->lexer);
23191 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23193 /* Consume the `return' keyword. */
23194 cp_lexer_consume_token (parser->lexer);
23195 /* Look for the identifier that indicates what value is to be
23196 returned. */
23197 cp_parser_identifier (parser);
23198 /* Issue an error message. */
23199 error_at (token->location,
23200 "named return values are no longer supported");
23201 /* Skip tokens until we reach the start of the function body. */
23202 while (true)
23204 cp_token *token = cp_lexer_peek_token (parser->lexer);
23205 if (token->type == CPP_OPEN_BRACE
23206 || token->type == CPP_EOF
23207 || token->type == CPP_PRAGMA_EOL)
23208 break;
23209 cp_lexer_consume_token (parser->lexer);
23212 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23213 anything declared inside `f'. */
23214 saved_in_unbraced_linkage_specification_p
23215 = parser->in_unbraced_linkage_specification_p;
23216 parser->in_unbraced_linkage_specification_p = false;
23217 /* Inside the function, surrounding template-parameter-lists do not
23218 apply. */
23219 saved_num_template_parameter_lists
23220 = parser->num_template_parameter_lists;
23221 parser->num_template_parameter_lists = 0;
23223 start_lambda_scope (current_function_decl);
23225 /* If the next token is `try', `__transaction_atomic', or
23226 `__transaction_relaxed`, then we are looking at either function-try-block
23227 or function-transaction-block. Note that all of these include the
23228 function-body. */
23229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23230 ctor_initializer_p = cp_parser_function_transaction (parser,
23231 RID_TRANSACTION_ATOMIC);
23232 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23233 RID_TRANSACTION_RELAXED))
23234 ctor_initializer_p = cp_parser_function_transaction (parser,
23235 RID_TRANSACTION_RELAXED);
23236 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23237 ctor_initializer_p = cp_parser_function_try_block (parser);
23238 else
23239 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23240 (parser, /*in_function_try_block=*/false);
23242 finish_lambda_scope ();
23244 /* Finish the function. */
23245 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23246 (inline_p ? 2 : 0));
23247 /* Generate code for it, if necessary. */
23248 expand_or_defer_fn (fn);
23249 /* Restore the saved values. */
23250 parser->in_unbraced_linkage_specification_p
23251 = saved_in_unbraced_linkage_specification_p;
23252 parser->num_template_parameter_lists
23253 = saved_num_template_parameter_lists;
23254 parser->in_function_body = saved_in_function_body;
23256 parser->fully_implicit_function_template_p
23257 = fully_implicit_function_template_p;
23258 parser->implicit_template_parms
23259 = implicit_template_parms;
23260 parser->implicit_template_scope
23261 = implicit_template_scope;
23263 if (parser->fully_implicit_function_template_p)
23264 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23266 return fn;
23269 /* Parse a template-declaration, assuming that the `export' (and
23270 `extern') keywords, if present, has already been scanned. MEMBER_P
23271 is as for cp_parser_template_declaration. */
23273 static void
23274 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23276 tree decl = NULL_TREE;
23277 vec<deferred_access_check, va_gc> *checks;
23278 tree parameter_list;
23279 bool friend_p = false;
23280 bool need_lang_pop;
23281 cp_token *token;
23283 /* Look for the `template' keyword. */
23284 token = cp_lexer_peek_token (parser->lexer);
23285 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23286 return;
23288 /* And the `<'. */
23289 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23290 return;
23291 if (at_class_scope_p () && current_function_decl)
23293 /* 14.5.2.2 [temp.mem]
23295 A local class shall not have member templates. */
23296 error_at (token->location,
23297 "invalid declaration of member template in local class");
23298 cp_parser_skip_to_end_of_block_or_statement (parser);
23299 return;
23301 /* [temp]
23303 A template ... shall not have C linkage. */
23304 if (current_lang_name == lang_name_c)
23306 error_at (token->location, "template with C linkage");
23307 /* Give it C++ linkage to avoid confusing other parts of the
23308 front end. */
23309 push_lang_context (lang_name_cplusplus);
23310 need_lang_pop = true;
23312 else
23313 need_lang_pop = false;
23315 /* We cannot perform access checks on the template parameter
23316 declarations until we know what is being declared, just as we
23317 cannot check the decl-specifier list. */
23318 push_deferring_access_checks (dk_deferred);
23320 // Save the current template requirements.
23321 tree saved_template_reqs = release (current_template_reqs);
23323 /* If the next token is `>', then we have an invalid
23324 specialization. Rather than complain about an invalid template
23325 parameter, issue an error message here. */
23326 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23328 cp_parser_error (parser, "invalid explicit specialization");
23329 begin_specialization ();
23330 parameter_list = NULL_TREE;
23332 else
23334 /* Parse the template parameters. */
23335 parameter_list = cp_parser_template_parameter_list (parser);
23338 /* Get the deferred access checks from the parameter list. These
23339 will be checked once we know what is being declared, as for a
23340 member template the checks must be performed in the scope of the
23341 class containing the member. */
23342 checks = get_deferred_access_checks ();
23344 /* Look for the `>'. */
23345 cp_parser_skip_to_end_of_template_parameter_list (parser);
23346 /* We just processed one more parameter list. */
23347 ++parser->num_template_parameter_lists;
23349 // Manage template requirements
23350 if (flag_concepts)
23352 tree reqs = get_shorthand_requirements (current_template_parms);
23353 if (tree r = cp_parser_requires_clause_opt (parser))
23354 reqs = conjoin_requirements (reqs, r);
23355 current_template_reqs = finish_template_requirements (reqs);
23357 // Attach the constraints to the template parameter list.
23358 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
23359 = current_template_reqs;
23362 /* If the next token is `template', there are more template
23363 parameters. */
23364 if (cp_lexer_next_token_is_keyword (parser->lexer,
23365 RID_TEMPLATE))
23366 cp_parser_template_declaration_after_export (parser, member_p);
23367 else if (cxx_dialect >= cxx11
23368 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23369 decl = cp_parser_alias_declaration (parser);
23370 else
23372 /* There are no access checks when parsing a template, as we do not
23373 know if a specialization will be a friend. */
23374 push_deferring_access_checks (dk_no_check);
23375 token = cp_lexer_peek_token (parser->lexer);
23376 decl = cp_parser_single_declaration (parser,
23377 checks,
23378 member_p,
23379 /*explicit_specialization_p=*/false,
23380 &friend_p);
23381 pop_deferring_access_checks ();
23383 /* If this is a member template declaration, let the front
23384 end know. */
23385 if (member_p && !friend_p && decl)
23387 if (TREE_CODE (decl) == TYPE_DECL)
23388 cp_parser_check_access_in_redeclaration (decl, token->location);
23390 decl = finish_member_template_decl (decl);
23392 else if (friend_p && decl
23393 && DECL_DECLARES_TYPE_P (decl))
23394 make_friend_class (current_class_type, TREE_TYPE (decl),
23395 /*complain=*/true);
23397 /* We are done with the current parameter list. */
23398 --parser->num_template_parameter_lists;
23400 pop_deferring_access_checks ();
23402 /* Finish up. */
23403 finish_template_decl (parameter_list);
23405 // Restore the current template requirements.
23406 current_template_reqs = saved_template_reqs;
23408 /* Check the template arguments for a literal operator template. */
23409 if (decl
23410 && DECL_DECLARES_FUNCTION_P (decl)
23411 && UDLIT_OPER_P (DECL_NAME (decl)))
23413 bool ok = true;
23414 if (parameter_list == NULL_TREE)
23415 ok = false;
23416 else
23418 int num_parms = TREE_VEC_LENGTH (parameter_list);
23419 if (num_parms == 1)
23421 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23422 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23423 if (TREE_TYPE (parm) != char_type_node
23424 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23425 ok = false;
23427 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23429 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23430 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23431 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23432 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23433 if (TREE_TYPE (parm) != TREE_TYPE (type)
23434 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23435 ok = false;
23437 else
23438 ok = false;
23440 if (!ok)
23441 error ("literal operator template %qD has invalid parameter list."
23442 " Expected non-type template argument pack <char...>"
23443 " or <typename CharT, CharT...>",
23444 decl);
23446 /* Register member declarations. */
23447 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23448 finish_member_declaration (decl);
23449 /* For the erroneous case of a template with C linkage, we pushed an
23450 implicit C++ linkage scope; exit that scope now. */
23451 if (need_lang_pop)
23452 pop_lang_context ();
23453 /* If DECL is a function template, we must return to parse it later.
23454 (Even though there is no definition, there might be default
23455 arguments that need handling.) */
23456 if (member_p && decl
23457 && DECL_DECLARES_FUNCTION_P (decl))
23458 vec_safe_push (unparsed_funs_with_definitions, decl);
23461 /* Perform the deferred access checks from a template-parameter-list.
23462 CHECKS is a TREE_LIST of access checks, as returned by
23463 get_deferred_access_checks. */
23465 static void
23466 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23468 ++processing_template_parmlist;
23469 perform_access_checks (checks, tf_warning_or_error);
23470 --processing_template_parmlist;
23473 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23474 `function-definition' sequence that follows a template header.
23475 If MEMBER_P is true, this declaration appears in a class scope.
23477 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23478 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23480 static tree
23481 cp_parser_single_declaration (cp_parser* parser,
23482 vec<deferred_access_check, va_gc> *checks,
23483 bool member_p,
23484 bool explicit_specialization_p,
23485 bool* friend_p)
23487 int declares_class_or_enum;
23488 tree decl = NULL_TREE;
23489 cp_decl_specifier_seq decl_specifiers;
23490 bool function_definition_p = false;
23491 cp_token *decl_spec_token_start;
23493 /* This function is only used when processing a template
23494 declaration. */
23495 gcc_assert (innermost_scope_kind () == sk_template_parms
23496 || innermost_scope_kind () == sk_template_spec);
23498 /* Defer access checks until we know what is being declared. */
23499 push_deferring_access_checks (dk_deferred);
23501 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23502 alternative. */
23503 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23504 cp_parser_decl_specifier_seq (parser,
23505 CP_PARSER_FLAGS_OPTIONAL,
23506 &decl_specifiers,
23507 &declares_class_or_enum);
23508 if (friend_p)
23509 *friend_p = cp_parser_friend_p (&decl_specifiers);
23511 /* There are no template typedefs. */
23512 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23514 error_at (decl_spec_token_start->location,
23515 "template declaration of %<typedef%>");
23516 decl = error_mark_node;
23519 /* Gather up the access checks that occurred the
23520 decl-specifier-seq. */
23521 stop_deferring_access_checks ();
23523 /* Check for the declaration of a template class. */
23524 if (declares_class_or_enum)
23526 if (cp_parser_declares_only_class_p (parser))
23528 decl = shadow_tag (&decl_specifiers);
23530 /* In this case:
23532 struct C {
23533 friend template <typename T> struct A<T>::B;
23536 A<T>::B will be represented by a TYPENAME_TYPE, and
23537 therefore not recognized by shadow_tag. */
23538 if (friend_p && *friend_p
23539 && !decl
23540 && decl_specifiers.type
23541 && TYPE_P (decl_specifiers.type))
23542 decl = decl_specifiers.type;
23544 if (decl && decl != error_mark_node)
23545 decl = TYPE_NAME (decl);
23546 else
23547 decl = error_mark_node;
23549 /* Perform access checks for template parameters. */
23550 cp_parser_perform_template_parameter_access_checks (checks);
23554 /* Complain about missing 'typename' or other invalid type names. */
23555 if (!decl_specifiers.any_type_specifiers_p
23556 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23558 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23559 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23560 the rest of this declaration. */
23561 decl = error_mark_node;
23562 goto out;
23565 /* If it's not a template class, try for a template function. If
23566 the next token is a `;', then this declaration does not declare
23567 anything. But, if there were errors in the decl-specifiers, then
23568 the error might well have come from an attempted class-specifier.
23569 In that case, there's no need to warn about a missing declarator. */
23570 if (!decl
23571 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23572 || decl_specifiers.type != error_mark_node))
23574 decl = cp_parser_init_declarator (parser,
23575 &decl_specifiers,
23576 checks,
23577 /*function_definition_allowed_p=*/true,
23578 member_p,
23579 declares_class_or_enum,
23580 &function_definition_p,
23581 NULL);
23583 /* 7.1.1-1 [dcl.stc]
23585 A storage-class-specifier shall not be specified in an explicit
23586 specialization... */
23587 if (decl
23588 && explicit_specialization_p
23589 && decl_specifiers.storage_class != sc_none)
23591 error_at (decl_spec_token_start->location,
23592 "explicit template specialization cannot have a storage class");
23593 decl = error_mark_node;
23596 if (decl && VAR_P (decl))
23597 check_template_variable (decl);
23600 /* Look for a trailing `;' after the declaration. */
23601 if (!function_definition_p
23602 && (decl == error_mark_node
23603 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23604 cp_parser_skip_to_end_of_block_or_statement (parser);
23606 out:
23607 pop_deferring_access_checks ();
23609 /* Clear any current qualification; whatever comes next is the start
23610 of something new. */
23611 parser->scope = NULL_TREE;
23612 parser->qualifying_scope = NULL_TREE;
23613 parser->object_scope = NULL_TREE;
23615 return decl;
23618 /* Parse a cast-expression that is not the operand of a unary "&". */
23620 static tree
23621 cp_parser_simple_cast_expression (cp_parser *parser)
23623 return cp_parser_cast_expression (parser, /*address_p=*/false,
23624 /*cast_p=*/false, /*decltype*/false, NULL);
23627 /* Parse a functional cast to TYPE. Returns an expression
23628 representing the cast. */
23630 static tree
23631 cp_parser_functional_cast (cp_parser* parser, tree type)
23633 vec<tree, va_gc> *vec;
23634 tree expression_list;
23635 tree cast;
23636 bool nonconst_p;
23638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23640 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23641 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23642 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23643 if (TREE_CODE (type) == TYPE_DECL)
23644 type = TREE_TYPE (type);
23645 return finish_compound_literal (type, expression_list,
23646 tf_warning_or_error);
23650 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23651 /*cast_p=*/true,
23652 /*allow_expansion_p=*/true,
23653 /*non_constant_p=*/NULL);
23654 if (vec == NULL)
23655 expression_list = error_mark_node;
23656 else
23658 expression_list = build_tree_list_vec (vec);
23659 release_tree_vector (vec);
23662 cast = build_functional_cast (type, expression_list,
23663 tf_warning_or_error);
23664 /* [expr.const]/1: In an integral constant expression "only type
23665 conversions to integral or enumeration type can be used". */
23666 if (TREE_CODE (type) == TYPE_DECL)
23667 type = TREE_TYPE (type);
23668 if (cast != error_mark_node
23669 && !cast_valid_in_integral_constant_expression_p (type)
23670 && cp_parser_non_integral_constant_expression (parser,
23671 NIC_CONSTRUCTOR))
23672 return error_mark_node;
23673 return cast;
23676 /* Save the tokens that make up the body of a member function defined
23677 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23678 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23679 specifiers applied to the declaration. Returns the FUNCTION_DECL
23680 for the member function. */
23682 static tree
23683 cp_parser_save_member_function_body (cp_parser* parser,
23684 cp_decl_specifier_seq *decl_specifiers,
23685 cp_declarator *declarator,
23686 tree attributes)
23688 cp_token *first;
23689 cp_token *last;
23690 tree fn;
23692 /* Create the FUNCTION_DECL. */
23693 fn = grokmethod (decl_specifiers, declarator, attributes);
23694 cp_finalize_omp_declare_simd (parser, fn);
23695 /* If something went badly wrong, bail out now. */
23696 if (fn == error_mark_node)
23698 /* If there's a function-body, skip it. */
23699 if (cp_parser_token_starts_function_definition_p
23700 (cp_lexer_peek_token (parser->lexer)))
23701 cp_parser_skip_to_end_of_block_or_statement (parser);
23702 return error_mark_node;
23705 /* Remember it, if there default args to post process. */
23706 cp_parser_save_default_args (parser, fn);
23708 /* Save away the tokens that make up the body of the
23709 function. */
23710 first = parser->lexer->next_token;
23711 /* Handle function try blocks. */
23712 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23713 cp_lexer_consume_token (parser->lexer);
23714 /* We can have braced-init-list mem-initializers before the fn body. */
23715 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23717 cp_lexer_consume_token (parser->lexer);
23718 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23720 /* cache_group will stop after an un-nested { } pair, too. */
23721 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23722 break;
23724 /* variadic mem-inits have ... after the ')'. */
23725 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23726 cp_lexer_consume_token (parser->lexer);
23729 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23730 /* Handle function try blocks. */
23731 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23732 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23733 last = parser->lexer->next_token;
23735 /* Save away the inline definition; we will process it when the
23736 class is complete. */
23737 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23738 DECL_PENDING_INLINE_P (fn) = 1;
23740 /* We need to know that this was defined in the class, so that
23741 friend templates are handled correctly. */
23742 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23744 /* Add FN to the queue of functions to be parsed later. */
23745 vec_safe_push (unparsed_funs_with_definitions, fn);
23747 return fn;
23750 /* Save the tokens that make up the in-class initializer for a non-static
23751 data member. Returns a DEFAULT_ARG. */
23753 static tree
23754 cp_parser_save_nsdmi (cp_parser* parser)
23756 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23759 /* Parse a template-argument-list, as well as the trailing ">" (but
23760 not the opening "<"). See cp_parser_template_argument_list for the
23761 return value. */
23763 static tree
23764 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23766 tree arguments;
23767 tree saved_scope;
23768 tree saved_qualifying_scope;
23769 tree saved_object_scope;
23770 bool saved_greater_than_is_operator_p;
23771 int saved_unevaluated_operand;
23772 int saved_inhibit_evaluation_warnings;
23774 /* [temp.names]
23776 When parsing a template-id, the first non-nested `>' is taken as
23777 the end of the template-argument-list rather than a greater-than
23778 operator. */
23779 saved_greater_than_is_operator_p
23780 = parser->greater_than_is_operator_p;
23781 parser->greater_than_is_operator_p = false;
23782 /* Parsing the argument list may modify SCOPE, so we save it
23783 here. */
23784 saved_scope = parser->scope;
23785 saved_qualifying_scope = parser->qualifying_scope;
23786 saved_object_scope = parser->object_scope;
23787 /* We need to evaluate the template arguments, even though this
23788 template-id may be nested within a "sizeof". */
23789 saved_unevaluated_operand = cp_unevaluated_operand;
23790 cp_unevaluated_operand = 0;
23791 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23792 c_inhibit_evaluation_warnings = 0;
23793 /* Parse the template-argument-list itself. */
23794 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23795 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23796 arguments = NULL_TREE;
23797 else
23798 arguments = cp_parser_template_argument_list (parser);
23799 /* Look for the `>' that ends the template-argument-list. If we find
23800 a '>>' instead, it's probably just a typo. */
23801 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23803 if (cxx_dialect != cxx98)
23805 /* In C++0x, a `>>' in a template argument list or cast
23806 expression is considered to be two separate `>'
23807 tokens. So, change the current token to a `>', but don't
23808 consume it: it will be consumed later when the outer
23809 template argument list (or cast expression) is parsed.
23810 Note that this replacement of `>' for `>>' is necessary
23811 even if we are parsing tentatively: in the tentative
23812 case, after calling
23813 cp_parser_enclosed_template_argument_list we will always
23814 throw away all of the template arguments and the first
23815 closing `>', either because the template argument list
23816 was erroneous or because we are replacing those tokens
23817 with a CPP_TEMPLATE_ID token. The second `>' (which will
23818 not have been thrown away) is needed either to close an
23819 outer template argument list or to complete a new-style
23820 cast. */
23821 cp_token *token = cp_lexer_peek_token (parser->lexer);
23822 token->type = CPP_GREATER;
23824 else if (!saved_greater_than_is_operator_p)
23826 /* If we're in a nested template argument list, the '>>' has
23827 to be a typo for '> >'. We emit the error message, but we
23828 continue parsing and we push a '>' as next token, so that
23829 the argument list will be parsed correctly. Note that the
23830 global source location is still on the token before the
23831 '>>', so we need to say explicitly where we want it. */
23832 cp_token *token = cp_lexer_peek_token (parser->lexer);
23833 error_at (token->location, "%<>>%> should be %<> >%> "
23834 "within a nested template argument list");
23836 token->type = CPP_GREATER;
23838 else
23840 /* If this is not a nested template argument list, the '>>'
23841 is a typo for '>'. Emit an error message and continue.
23842 Same deal about the token location, but here we can get it
23843 right by consuming the '>>' before issuing the diagnostic. */
23844 cp_token *token = cp_lexer_consume_token (parser->lexer);
23845 error_at (token->location,
23846 "spurious %<>>%>, use %<>%> to terminate "
23847 "a template argument list");
23850 else
23851 cp_parser_skip_to_end_of_template_parameter_list (parser);
23852 /* The `>' token might be a greater-than operator again now. */
23853 parser->greater_than_is_operator_p
23854 = saved_greater_than_is_operator_p;
23855 /* Restore the SAVED_SCOPE. */
23856 parser->scope = saved_scope;
23857 parser->qualifying_scope = saved_qualifying_scope;
23858 parser->object_scope = saved_object_scope;
23859 cp_unevaluated_operand = saved_unevaluated_operand;
23860 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23862 return arguments;
23865 /* MEMBER_FUNCTION is a member function, or a friend. If default
23866 arguments, or the body of the function have not yet been parsed,
23867 parse them now. */
23869 static void
23870 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23872 timevar_push (TV_PARSE_INMETH);
23873 /* If this member is a template, get the underlying
23874 FUNCTION_DECL. */
23875 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23876 member_function = DECL_TEMPLATE_RESULT (member_function);
23878 /* There should not be any class definitions in progress at this
23879 point; the bodies of members are only parsed outside of all class
23880 definitions. */
23881 gcc_assert (parser->num_classes_being_defined == 0);
23882 /* While we're parsing the member functions we might encounter more
23883 classes. We want to handle them right away, but we don't want
23884 them getting mixed up with functions that are currently in the
23885 queue. */
23886 push_unparsed_function_queues (parser);
23888 /* Make sure that any template parameters are in scope. */
23889 maybe_begin_member_template_processing (member_function);
23891 // Restore the declaration's requirements for the parsing of
23892 // the definition.
23894 tree saved_template_reqs = release (current_template_reqs);
23895 current_template_reqs = get_constraints (member_function);
23897 /* If the body of the function has not yet been parsed, parse it
23898 now. */
23899 if (DECL_PENDING_INLINE_P (member_function))
23901 tree function_scope;
23902 cp_token_cache *tokens;
23904 /* The function is no longer pending; we are processing it. */
23905 tokens = DECL_PENDING_INLINE_INFO (member_function);
23906 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23907 DECL_PENDING_INLINE_P (member_function) = 0;
23909 /* If this is a local class, enter the scope of the containing
23910 function. */
23911 function_scope = current_function_decl;
23912 if (function_scope)
23913 push_function_context ();
23915 /* Push the body of the function onto the lexer stack. */
23916 cp_parser_push_lexer_for_tokens (parser, tokens);
23918 /* Let the front end know that we going to be defining this
23919 function. */
23920 start_preparsed_function (member_function, NULL_TREE,
23921 SF_PRE_PARSED | SF_INCLASS_INLINE);
23923 /* Don't do access checking if it is a templated function. */
23924 if (processing_template_decl)
23925 push_deferring_access_checks (dk_no_check);
23927 /* #pragma omp declare reduction needs special parsing. */
23928 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23930 parser->lexer->in_pragma = true;
23931 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23932 finish_function (0);
23933 cp_check_omp_declare_reduction (member_function);
23935 else
23936 /* Now, parse the body of the function. */
23937 cp_parser_function_definition_after_declarator (parser,
23938 /*inline_p=*/true);
23940 if (processing_template_decl)
23941 pop_deferring_access_checks ();
23943 /* Leave the scope of the containing function. */
23944 if (function_scope)
23945 pop_function_context ();
23946 cp_parser_pop_lexer (parser);
23949 /* Remove any template parameters from the symbol table. */
23950 maybe_end_member_template_processing ();
23952 // Restore the template requirements.
23953 current_template_reqs = saved_template_reqs;
23955 /* Restore the queue. */
23956 pop_unparsed_function_queues (parser);
23957 timevar_pop (TV_PARSE_INMETH);
23960 /* If DECL contains any default args, remember it on the unparsed
23961 functions queue. */
23963 static void
23964 cp_parser_save_default_args (cp_parser* parser, tree decl)
23966 tree probe;
23968 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23969 probe;
23970 probe = TREE_CHAIN (probe))
23971 if (TREE_PURPOSE (probe))
23973 cp_default_arg_entry entry = {current_class_type, decl};
23974 vec_safe_push (unparsed_funs_with_default_args, entry);
23975 break;
23979 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23980 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23981 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23982 from the parameter-type-list. */
23984 static tree
23985 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23986 tree default_arg, tree parmtype)
23988 cp_token_cache *tokens;
23989 tree parsed_arg;
23990 bool dummy;
23992 if (default_arg == error_mark_node)
23993 return error_mark_node;
23995 /* Push the saved tokens for the default argument onto the parser's
23996 lexer stack. */
23997 tokens = DEFARG_TOKENS (default_arg);
23998 cp_parser_push_lexer_for_tokens (parser, tokens);
24000 start_lambda_scope (decl);
24002 /* Parse the default argument. */
24003 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24004 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24005 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24007 finish_lambda_scope ();
24009 if (parsed_arg == error_mark_node)
24010 cp_parser_skip_to_end_of_statement (parser);
24012 if (!processing_template_decl)
24014 /* In a non-template class, check conversions now. In a template,
24015 we'll wait and instantiate these as needed. */
24016 if (TREE_CODE (decl) == PARM_DECL)
24017 parsed_arg = check_default_argument (parmtype, parsed_arg,
24018 tf_warning_or_error);
24019 else
24021 int flags = LOOKUP_IMPLICIT;
24022 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
24023 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
24024 flags = LOOKUP_NORMAL;
24025 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
24026 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
24027 /* This represents the whole initialization. */
24028 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
24032 /* If the token stream has not been completely used up, then
24033 there was extra junk after the end of the default
24034 argument. */
24035 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24037 if (TREE_CODE (decl) == PARM_DECL)
24038 cp_parser_error (parser, "expected %<,%>");
24039 else
24040 cp_parser_error (parser, "expected %<;%>");
24043 /* Revert to the main lexer. */
24044 cp_parser_pop_lexer (parser);
24046 return parsed_arg;
24049 /* FIELD is a non-static data member with an initializer which we saved for
24050 later; parse it now. */
24052 static void
24053 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24055 tree def;
24057 maybe_begin_member_template_processing (field);
24059 push_unparsed_function_queues (parser);
24060 def = cp_parser_late_parse_one_default_arg (parser, field,
24061 DECL_INITIAL (field),
24062 NULL_TREE);
24063 pop_unparsed_function_queues (parser);
24065 maybe_end_member_template_processing ();
24067 DECL_INITIAL (field) = def;
24070 /* FN is a FUNCTION_DECL which may contains a parameter with an
24071 unparsed DEFAULT_ARG. Parse the default args now. This function
24072 assumes that the current scope is the scope in which the default
24073 argument should be processed. */
24075 static void
24076 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24078 bool saved_local_variables_forbidden_p;
24079 tree parm, parmdecl;
24081 /* While we're parsing the default args, we might (due to the
24082 statement expression extension) encounter more classes. We want
24083 to handle them right away, but we don't want them getting mixed
24084 up with default args that are currently in the queue. */
24085 push_unparsed_function_queues (parser);
24087 /* Local variable names (and the `this' keyword) may not appear
24088 in a default argument. */
24089 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24090 parser->local_variables_forbidden_p = true;
24092 push_defarg_context (fn);
24094 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24095 parmdecl = DECL_ARGUMENTS (fn);
24096 parm && parm != void_list_node;
24097 parm = TREE_CHAIN (parm),
24098 parmdecl = DECL_CHAIN (parmdecl))
24100 tree default_arg = TREE_PURPOSE (parm);
24101 tree parsed_arg;
24102 vec<tree, va_gc> *insts;
24103 tree copy;
24104 unsigned ix;
24106 if (!default_arg)
24107 continue;
24109 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24110 /* This can happen for a friend declaration for a function
24111 already declared with default arguments. */
24112 continue;
24114 parsed_arg
24115 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24116 default_arg,
24117 TREE_VALUE (parm));
24118 if (parsed_arg == error_mark_node)
24120 continue;
24123 TREE_PURPOSE (parm) = parsed_arg;
24125 /* Update any instantiations we've already created. */
24126 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24127 vec_safe_iterate (insts, ix, &copy); ix++)
24128 TREE_PURPOSE (copy) = parsed_arg;
24131 pop_defarg_context ();
24133 /* Make sure no default arg is missing. */
24134 check_default_args (fn);
24136 /* Restore the state of local_variables_forbidden_p. */
24137 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24139 /* Restore the queue. */
24140 pop_unparsed_function_queues (parser);
24143 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24145 sizeof ... ( identifier )
24147 where the 'sizeof' token has already been consumed. */
24149 static tree
24150 cp_parser_sizeof_pack (cp_parser *parser)
24152 /* Consume the `...'. */
24153 cp_lexer_consume_token (parser->lexer);
24154 maybe_warn_variadic_templates ();
24156 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24157 if (paren)
24158 cp_lexer_consume_token (parser->lexer);
24159 else
24160 permerror (cp_lexer_peek_token (parser->lexer)->location,
24161 "%<sizeof...%> argument must be surrounded by parentheses");
24163 cp_token *token = cp_lexer_peek_token (parser->lexer);
24164 tree name = cp_parser_identifier (parser);
24165 if (name == error_mark_node)
24166 return error_mark_node;
24167 /* The name is not qualified. */
24168 parser->scope = NULL_TREE;
24169 parser->qualifying_scope = NULL_TREE;
24170 parser->object_scope = NULL_TREE;
24171 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24172 if (expr == error_mark_node)
24173 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24174 token->location);
24175 if (TREE_CODE (expr) == TYPE_DECL)
24176 expr = TREE_TYPE (expr);
24177 else if (TREE_CODE (expr) == CONST_DECL)
24178 expr = DECL_INITIAL (expr);
24179 expr = make_pack_expansion (expr);
24181 if (paren)
24182 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24184 return expr;
24187 /* Parse the operand of `sizeof' (or a similar operator). Returns
24188 either a TYPE or an expression, depending on the form of the
24189 input. The KEYWORD indicates which kind of expression we have
24190 encountered. */
24192 static tree
24193 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24195 tree expr = NULL_TREE;
24196 const char *saved_message;
24197 char *tmp;
24198 bool saved_integral_constant_expression_p;
24199 bool saved_non_integral_constant_expression_p;
24201 /* If it's a `...', then we are computing the length of a parameter
24202 pack. */
24203 if (keyword == RID_SIZEOF
24204 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24205 return cp_parser_sizeof_pack (parser);
24207 /* Types cannot be defined in a `sizeof' expression. Save away the
24208 old message. */
24209 saved_message = parser->type_definition_forbidden_message;
24210 /* And create the new one. */
24211 tmp = concat ("types may not be defined in %<",
24212 IDENTIFIER_POINTER (ridpointers[keyword]),
24213 "%> expressions", NULL);
24214 parser->type_definition_forbidden_message = tmp;
24216 /* The restrictions on constant-expressions do not apply inside
24217 sizeof expressions. */
24218 saved_integral_constant_expression_p
24219 = parser->integral_constant_expression_p;
24220 saved_non_integral_constant_expression_p
24221 = parser->non_integral_constant_expression_p;
24222 parser->integral_constant_expression_p = false;
24224 /* Do not actually evaluate the expression. */
24225 ++cp_unevaluated_operand;
24226 ++c_inhibit_evaluation_warnings;
24227 /* If it's a `(', then we might be looking at the type-id
24228 construction. */
24229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24231 tree type = NULL_TREE;
24232 bool compound_literal_p;
24234 /* We can't be sure yet whether we're looking at a type-id or an
24235 expression. */
24236 cp_parser_parse_tentatively (parser);
24237 /* Consume the `('. */
24238 cp_lexer_consume_token (parser->lexer);
24239 /* Note: as a GNU Extension, compound literals are considered
24240 postfix-expressions as they are in C99, so they are valid
24241 arguments to sizeof. See comment in cp_parser_cast_expression
24242 for details. */
24243 cp_lexer_save_tokens (parser->lexer);
24244 /* Skip tokens until the next token is a closing parenthesis.
24245 If we find the closing `)', and the next token is a `{', then
24246 we are looking at a compound-literal. */
24247 compound_literal_p
24248 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
24249 /*consume_paren=*/true)
24250 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
24251 /* Roll back the tokens we skipped. */
24252 cp_lexer_rollback_tokens (parser->lexer);
24253 /* If we were looking at a compound-literal, simulate an error
24254 so that the call to cp_parser_parse_definitely below will
24255 fail. */
24256 if (compound_literal_p)
24257 cp_parser_simulate_error (parser);
24258 else
24260 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24261 parser->in_type_id_in_expr_p = true;
24262 /* Look for the type-id. */
24263 type = cp_parser_type_id (parser);
24264 /* Look for the closing `)'. */
24265 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24266 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24269 /* If all went well, then we're done. */
24270 if (cp_parser_parse_definitely (parser))
24272 cp_decl_specifier_seq decl_specs;
24274 /* Build a trivial decl-specifier-seq. */
24275 clear_decl_specs (&decl_specs);
24276 decl_specs.type = type;
24278 /* Call grokdeclarator to figure out what type this is. */
24279 expr = grokdeclarator (NULL,
24280 &decl_specs,
24281 TYPENAME,
24282 /*initialized=*/0,
24283 /*attrlist=*/NULL);
24287 /* If the type-id production did not work out, then we must be
24288 looking at the unary-expression production. */
24289 if (!expr)
24290 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24291 /*cast_p=*/false, NULL);
24293 /* Go back to evaluating expressions. */
24294 --cp_unevaluated_operand;
24295 --c_inhibit_evaluation_warnings;
24297 /* Free the message we created. */
24298 free (tmp);
24299 /* And restore the old one. */
24300 parser->type_definition_forbidden_message = saved_message;
24301 parser->integral_constant_expression_p
24302 = saved_integral_constant_expression_p;
24303 parser->non_integral_constant_expression_p
24304 = saved_non_integral_constant_expression_p;
24306 return expr;
24309 /* If the current declaration has no declarator, return true. */
24311 static bool
24312 cp_parser_declares_only_class_p (cp_parser *parser)
24314 /* If the next token is a `;' or a `,' then there is no
24315 declarator. */
24316 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24317 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24320 /* Update the DECL_SPECS to reflect the storage class indicated by
24321 KEYWORD. */
24323 static void
24324 cp_parser_set_storage_class (cp_parser *parser,
24325 cp_decl_specifier_seq *decl_specs,
24326 enum rid keyword,
24327 cp_token *token)
24329 cp_storage_class storage_class;
24331 if (parser->in_unbraced_linkage_specification_p)
24333 error_at (token->location, "invalid use of %qD in linkage specification",
24334 ridpointers[keyword]);
24335 return;
24337 else if (decl_specs->storage_class != sc_none)
24339 decl_specs->conflicting_specifiers_p = true;
24340 return;
24343 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24344 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24345 && decl_specs->gnu_thread_keyword_p)
24347 pedwarn (decl_specs->locations[ds_thread], 0,
24348 "%<__thread%> before %qD", ridpointers[keyword]);
24351 switch (keyword)
24353 case RID_AUTO:
24354 storage_class = sc_auto;
24355 break;
24356 case RID_REGISTER:
24357 storage_class = sc_register;
24358 break;
24359 case RID_STATIC:
24360 storage_class = sc_static;
24361 break;
24362 case RID_EXTERN:
24363 storage_class = sc_extern;
24364 break;
24365 case RID_MUTABLE:
24366 storage_class = sc_mutable;
24367 break;
24368 default:
24369 gcc_unreachable ();
24371 decl_specs->storage_class = storage_class;
24372 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24374 /* A storage class specifier cannot be applied alongside a typedef
24375 specifier. If there is a typedef specifier present then set
24376 conflicting_specifiers_p which will trigger an error later
24377 on in grokdeclarator. */
24378 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24379 decl_specs->conflicting_specifiers_p = true;
24382 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24383 is true, the type is a class or enum definition. */
24385 static void
24386 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24387 tree type_spec,
24388 cp_token *token,
24389 bool type_definition_p)
24391 decl_specs->any_specifiers_p = true;
24393 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24394 (with, for example, in "typedef int wchar_t;") we remember that
24395 this is what happened. In system headers, we ignore these
24396 declarations so that G++ can work with system headers that are not
24397 C++-safe. */
24398 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24399 && !type_definition_p
24400 && (type_spec == boolean_type_node
24401 || type_spec == char16_type_node
24402 || type_spec == char32_type_node
24403 || type_spec == wchar_type_node)
24404 && (decl_specs->type
24405 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24406 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24407 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24408 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24410 decl_specs->redefined_builtin_type = type_spec;
24411 set_and_check_decl_spec_loc (decl_specs,
24412 ds_redefined_builtin_type_spec,
24413 token);
24414 if (!decl_specs->type)
24416 decl_specs->type = type_spec;
24417 decl_specs->type_definition_p = false;
24418 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24421 else if (decl_specs->type)
24422 decl_specs->multiple_types_p = true;
24423 else
24425 decl_specs->type = type_spec;
24426 decl_specs->type_definition_p = type_definition_p;
24427 decl_specs->redefined_builtin_type = NULL_TREE;
24428 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24432 /* True iff TOKEN is the GNU keyword __thread. */
24434 static bool
24435 token_is__thread (cp_token *token)
24437 gcc_assert (token->keyword == RID_THREAD);
24438 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24441 /* Set the location for a declarator specifier and check if it is
24442 duplicated.
24444 DECL_SPECS is the sequence of declarator specifiers onto which to
24445 set the location.
24447 DS is the single declarator specifier to set which location is to
24448 be set onto the existing sequence of declarators.
24450 LOCATION is the location for the declarator specifier to
24451 consider. */
24453 static void
24454 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24455 cp_decl_spec ds, cp_token *token)
24457 gcc_assert (ds < ds_last);
24459 if (decl_specs == NULL)
24460 return;
24462 source_location location = token->location;
24464 if (decl_specs->locations[ds] == 0)
24466 decl_specs->locations[ds] = location;
24467 if (ds == ds_thread)
24468 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24470 else
24472 if (ds == ds_long)
24474 if (decl_specs->locations[ds_long_long] != 0)
24475 error_at (location,
24476 "%<long long long%> is too long for GCC");
24477 else
24479 decl_specs->locations[ds_long_long] = location;
24480 pedwarn_cxx98 (location,
24481 OPT_Wlong_long,
24482 "ISO C++ 1998 does not support %<long long%>");
24485 else if (ds == ds_thread)
24487 bool gnu = token_is__thread (token);
24488 if (gnu != decl_specs->gnu_thread_keyword_p)
24489 error_at (location,
24490 "both %<__thread%> and %<thread_local%> specified");
24491 else
24492 error_at (location, "duplicate %qD", token->u.value);
24494 else
24496 static const char *const decl_spec_names[] = {
24497 "signed",
24498 "unsigned",
24499 "short",
24500 "long",
24501 "const",
24502 "volatile",
24503 "restrict",
24504 "inline",
24505 "virtual",
24506 "explicit",
24507 "friend",
24508 "typedef",
24509 "using",
24510 "constexpr",
24511 "__complex"
24513 error_at (location,
24514 "duplicate %qs", decl_spec_names[ds]);
24519 /* Return true iff the declarator specifier DS is present in the
24520 sequence of declarator specifiers DECL_SPECS. */
24522 bool
24523 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24524 cp_decl_spec ds)
24526 gcc_assert (ds < ds_last);
24528 if (decl_specs == NULL)
24529 return false;
24531 return decl_specs->locations[ds] != 0;
24534 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24535 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24537 static bool
24538 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24540 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24543 /* Issue an error message indicating that TOKEN_DESC was expected.
24544 If KEYWORD is true, it indicated this function is called by
24545 cp_parser_require_keword and the required token can only be
24546 a indicated keyword. */
24548 static void
24549 cp_parser_required_error (cp_parser *parser,
24550 required_token token_desc,
24551 bool keyword)
24553 switch (token_desc)
24555 case RT_NEW:
24556 cp_parser_error (parser, "expected %<new%>");
24557 return;
24558 case RT_DELETE:
24559 cp_parser_error (parser, "expected %<delete%>");
24560 return;
24561 case RT_RETURN:
24562 cp_parser_error (parser, "expected %<return%>");
24563 return;
24564 case RT_WHILE:
24565 cp_parser_error (parser, "expected %<while%>");
24566 return;
24567 case RT_EXTERN:
24568 cp_parser_error (parser, "expected %<extern%>");
24569 return;
24570 case RT_STATIC_ASSERT:
24571 cp_parser_error (parser, "expected %<static_assert%>");
24572 return;
24573 case RT_DECLTYPE:
24574 cp_parser_error (parser, "expected %<decltype%>");
24575 return;
24576 case RT_OPERATOR:
24577 cp_parser_error (parser, "expected %<operator%>");
24578 return;
24579 case RT_CLASS:
24580 cp_parser_error (parser, "expected %<class%>");
24581 return;
24582 case RT_TEMPLATE:
24583 cp_parser_error (parser, "expected %<template%>");
24584 return;
24585 case RT_NAMESPACE:
24586 cp_parser_error (parser, "expected %<namespace%>");
24587 return;
24588 case RT_USING:
24589 cp_parser_error (parser, "expected %<using%>");
24590 return;
24591 case RT_ASM:
24592 cp_parser_error (parser, "expected %<asm%>");
24593 return;
24594 case RT_TRY:
24595 cp_parser_error (parser, "expected %<try%>");
24596 return;
24597 case RT_CATCH:
24598 cp_parser_error (parser, "expected %<catch%>");
24599 return;
24600 case RT_THROW:
24601 cp_parser_error (parser, "expected %<throw%>");
24602 return;
24603 case RT_LABEL:
24604 cp_parser_error (parser, "expected %<__label__%>");
24605 return;
24606 case RT_AT_TRY:
24607 cp_parser_error (parser, "expected %<@try%>");
24608 return;
24609 case RT_AT_SYNCHRONIZED:
24610 cp_parser_error (parser, "expected %<@synchronized%>");
24611 return;
24612 case RT_AT_THROW:
24613 cp_parser_error (parser, "expected %<@throw%>");
24614 return;
24615 case RT_TRANSACTION_ATOMIC:
24616 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24617 return;
24618 case RT_TRANSACTION_RELAXED:
24619 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24620 return;
24621 default:
24622 break;
24624 if (!keyword)
24626 switch (token_desc)
24628 case RT_SEMICOLON:
24629 cp_parser_error (parser, "expected %<;%>");
24630 return;
24631 case RT_OPEN_PAREN:
24632 cp_parser_error (parser, "expected %<(%>");
24633 return;
24634 case RT_CLOSE_BRACE:
24635 cp_parser_error (parser, "expected %<}%>");
24636 return;
24637 case RT_OPEN_BRACE:
24638 cp_parser_error (parser, "expected %<{%>");
24639 return;
24640 case RT_CLOSE_SQUARE:
24641 cp_parser_error (parser, "expected %<]%>");
24642 return;
24643 case RT_OPEN_SQUARE:
24644 cp_parser_error (parser, "expected %<[%>");
24645 return;
24646 case RT_COMMA:
24647 cp_parser_error (parser, "expected %<,%>");
24648 return;
24649 case RT_SCOPE:
24650 cp_parser_error (parser, "expected %<::%>");
24651 return;
24652 case RT_LESS:
24653 cp_parser_error (parser, "expected %<<%>");
24654 return;
24655 case RT_GREATER:
24656 cp_parser_error (parser, "expected %<>%>");
24657 return;
24658 case RT_EQ:
24659 cp_parser_error (parser, "expected %<=%>");
24660 return;
24661 case RT_ELLIPSIS:
24662 cp_parser_error (parser, "expected %<...%>");
24663 return;
24664 case RT_MULT:
24665 cp_parser_error (parser, "expected %<*%>");
24666 return;
24667 case RT_COMPL:
24668 cp_parser_error (parser, "expected %<~%>");
24669 return;
24670 case RT_COLON:
24671 cp_parser_error (parser, "expected %<:%>");
24672 return;
24673 case RT_COLON_SCOPE:
24674 cp_parser_error (parser, "expected %<:%> or %<::%>");
24675 return;
24676 case RT_CLOSE_PAREN:
24677 cp_parser_error (parser, "expected %<)%>");
24678 return;
24679 case RT_COMMA_CLOSE_PAREN:
24680 cp_parser_error (parser, "expected %<,%> or %<)%>");
24681 return;
24682 case RT_PRAGMA_EOL:
24683 cp_parser_error (parser, "expected end of line");
24684 return;
24685 case RT_NAME:
24686 cp_parser_error (parser, "expected identifier");
24687 return;
24688 case RT_SELECT:
24689 cp_parser_error (parser, "expected selection-statement");
24690 return;
24691 case RT_INTERATION:
24692 cp_parser_error (parser, "expected iteration-statement");
24693 return;
24694 case RT_JUMP:
24695 cp_parser_error (parser, "expected jump-statement");
24696 return;
24697 case RT_CLASS_KEY:
24698 cp_parser_error (parser, "expected class-key");
24699 return;
24700 case RT_CLASS_TYPENAME_TEMPLATE:
24701 cp_parser_error (parser,
24702 "expected %<class%>, %<typename%>, or %<template%>");
24703 return;
24704 default:
24705 gcc_unreachable ();
24708 else
24709 gcc_unreachable ();
24714 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24715 issue an error message indicating that TOKEN_DESC was expected.
24717 Returns the token consumed, if the token had the appropriate type.
24718 Otherwise, returns NULL. */
24720 static cp_token *
24721 cp_parser_require (cp_parser* parser,
24722 enum cpp_ttype type,
24723 required_token token_desc)
24725 if (cp_lexer_next_token_is (parser->lexer, type))
24726 return cp_lexer_consume_token (parser->lexer);
24727 else
24729 /* Output the MESSAGE -- unless we're parsing tentatively. */
24730 if (!cp_parser_simulate_error (parser))
24731 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24732 return NULL;
24736 /* An error message is produced if the next token is not '>'.
24737 All further tokens are skipped until the desired token is
24738 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24740 static void
24741 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24743 /* Current level of '< ... >'. */
24744 unsigned level = 0;
24745 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24746 unsigned nesting_depth = 0;
24748 /* Are we ready, yet? If not, issue error message. */
24749 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24750 return;
24752 /* Skip tokens until the desired token is found. */
24753 while (true)
24755 /* Peek at the next token. */
24756 switch (cp_lexer_peek_token (parser->lexer)->type)
24758 case CPP_LESS:
24759 if (!nesting_depth)
24760 ++level;
24761 break;
24763 case CPP_RSHIFT:
24764 if (cxx_dialect == cxx98)
24765 /* C++0x views the `>>' operator as two `>' tokens, but
24766 C++98 does not. */
24767 break;
24768 else if (!nesting_depth && level-- == 0)
24770 /* We've hit a `>>' where the first `>' closes the
24771 template argument list, and the second `>' is
24772 spurious. Just consume the `>>' and stop; we've
24773 already produced at least one error. */
24774 cp_lexer_consume_token (parser->lexer);
24775 return;
24777 /* Fall through for C++0x, so we handle the second `>' in
24778 the `>>'. */
24780 case CPP_GREATER:
24781 if (!nesting_depth && level-- == 0)
24783 /* We've reached the token we want, consume it and stop. */
24784 cp_lexer_consume_token (parser->lexer);
24785 return;
24787 break;
24789 case CPP_OPEN_PAREN:
24790 case CPP_OPEN_SQUARE:
24791 ++nesting_depth;
24792 break;
24794 case CPP_CLOSE_PAREN:
24795 case CPP_CLOSE_SQUARE:
24796 if (nesting_depth-- == 0)
24797 return;
24798 break;
24800 case CPP_EOF:
24801 case CPP_PRAGMA_EOL:
24802 case CPP_SEMICOLON:
24803 case CPP_OPEN_BRACE:
24804 case CPP_CLOSE_BRACE:
24805 /* The '>' was probably forgotten, don't look further. */
24806 return;
24808 default:
24809 break;
24812 /* Consume this token. */
24813 cp_lexer_consume_token (parser->lexer);
24817 /* If the next token is the indicated keyword, consume it. Otherwise,
24818 issue an error message indicating that TOKEN_DESC was expected.
24820 Returns the token consumed, if the token had the appropriate type.
24821 Otherwise, returns NULL. */
24823 static cp_token *
24824 cp_parser_require_keyword (cp_parser* parser,
24825 enum rid keyword,
24826 required_token token_desc)
24828 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24830 if (token && token->keyword != keyword)
24832 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24833 return NULL;
24836 return token;
24839 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24840 function-definition. */
24842 static bool
24843 cp_parser_token_starts_function_definition_p (cp_token* token)
24845 return (/* An ordinary function-body begins with an `{'. */
24846 token->type == CPP_OPEN_BRACE
24847 /* A ctor-initializer begins with a `:'. */
24848 || token->type == CPP_COLON
24849 /* A function-try-block begins with `try'. */
24850 || token->keyword == RID_TRY
24851 /* A function-transaction-block begins with `__transaction_atomic'
24852 or `__transaction_relaxed'. */
24853 || token->keyword == RID_TRANSACTION_ATOMIC
24854 || token->keyword == RID_TRANSACTION_RELAXED
24855 /* The named return value extension begins with `return'. */
24856 || token->keyword == RID_RETURN);
24859 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24860 definition. */
24862 static bool
24863 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24865 cp_token *token;
24867 token = cp_lexer_peek_token (parser->lexer);
24868 return (token->type == CPP_OPEN_BRACE
24869 || (token->type == CPP_COLON
24870 && !parser->colon_doesnt_start_class_def_p));
24873 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24874 C++0x) ending a template-argument. */
24876 static bool
24877 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24879 cp_token *token;
24881 token = cp_lexer_peek_token (parser->lexer);
24882 return (token->type == CPP_COMMA
24883 || token->type == CPP_GREATER
24884 || token->type == CPP_ELLIPSIS
24885 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24888 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24889 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24891 static bool
24892 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24893 size_t n)
24895 cp_token *token;
24897 token = cp_lexer_peek_nth_token (parser->lexer, n);
24898 if (token->type == CPP_LESS)
24899 return true;
24900 /* Check for the sequence `<::' in the original code. It would be lexed as
24901 `[:', where `[' is a digraph, and there is no whitespace before
24902 `:'. */
24903 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24905 cp_token *token2;
24906 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24907 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24908 return true;
24910 return false;
24913 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24914 or none_type otherwise. */
24916 static enum tag_types
24917 cp_parser_token_is_class_key (cp_token* token)
24919 switch (token->keyword)
24921 case RID_CLASS:
24922 return class_type;
24923 case RID_STRUCT:
24924 return record_type;
24925 case RID_UNION:
24926 return union_type;
24928 default:
24929 return none_type;
24933 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24935 static void
24936 cp_parser_check_class_key (enum tag_types class_key, tree type)
24938 if (type == error_mark_node)
24939 return;
24940 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24942 if (permerror (input_location, "%qs tag used in naming %q#T",
24943 class_key == union_type ? "union"
24944 : class_key == record_type ? "struct" : "class",
24945 type))
24946 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24947 "%q#T was previously declared here", type);
24951 /* Issue an error message if DECL is redeclared with different
24952 access than its original declaration [class.access.spec/3].
24953 This applies to nested classes and nested class templates.
24954 [class.mem/1]. */
24956 static void
24957 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24959 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24960 return;
24962 if ((TREE_PRIVATE (decl)
24963 != (current_access_specifier == access_private_node))
24964 || (TREE_PROTECTED (decl)
24965 != (current_access_specifier == access_protected_node)))
24966 error_at (location, "%qD redeclared with different access", decl);
24969 /* Look for the `template' keyword, as a syntactic disambiguator.
24970 Return TRUE iff it is present, in which case it will be
24971 consumed. */
24973 static bool
24974 cp_parser_optional_template_keyword (cp_parser *parser)
24976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24978 /* In C++98 the `template' keyword can only be used within templates;
24979 outside templates the parser can always figure out what is a
24980 template and what is not. In C++11, per the resolution of DR 468,
24981 `template' is allowed in cases where it is not strictly necessary. */
24982 if (!processing_template_decl
24983 && pedantic && cxx_dialect == cxx98)
24985 cp_token *token = cp_lexer_peek_token (parser->lexer);
24986 pedwarn (token->location, OPT_Wpedantic,
24987 "in C++98 %<template%> (as a disambiguator) is only "
24988 "allowed within templates");
24989 /* If this part of the token stream is rescanned, the same
24990 error message would be generated. So, we purge the token
24991 from the stream. */
24992 cp_lexer_purge_token (parser->lexer);
24993 return false;
24995 else
24997 /* Consume the `template' keyword. */
24998 cp_lexer_consume_token (parser->lexer);
24999 return true;
25002 return false;
25005 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25006 set PARSER->SCOPE, and perform other related actions. */
25008 static void
25009 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25011 int i;
25012 struct tree_check *check_value;
25013 deferred_access_check *chk;
25014 vec<deferred_access_check, va_gc> *checks;
25016 /* Get the stored value. */
25017 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25018 /* Perform any access checks that were deferred. */
25019 checks = check_value->checks;
25020 if (checks)
25022 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25023 perform_or_defer_access_check (chk->binfo,
25024 chk->decl,
25025 chk->diag_decl, tf_warning_or_error);
25027 /* Set the scope from the stored value. */
25028 parser->scope = check_value->value;
25029 parser->qualifying_scope = check_value->qualifying_scope;
25030 parser->object_scope = NULL_TREE;
25033 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25034 encounter the end of a block before what we were looking for. */
25036 static bool
25037 cp_parser_cache_group (cp_parser *parser,
25038 enum cpp_ttype end,
25039 unsigned depth)
25041 while (true)
25043 cp_token *token = cp_lexer_peek_token (parser->lexer);
25045 /* Abort a parenthesized expression if we encounter a semicolon. */
25046 if ((end == CPP_CLOSE_PAREN || depth == 0)
25047 && token->type == CPP_SEMICOLON)
25048 return true;
25049 /* If we've reached the end of the file, stop. */
25050 if (token->type == CPP_EOF
25051 || (end != CPP_PRAGMA_EOL
25052 && token->type == CPP_PRAGMA_EOL))
25053 return true;
25054 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25055 /* We've hit the end of an enclosing block, so there's been some
25056 kind of syntax error. */
25057 return true;
25059 /* Consume the token. */
25060 cp_lexer_consume_token (parser->lexer);
25061 /* See if it starts a new group. */
25062 if (token->type == CPP_OPEN_BRACE)
25064 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25065 /* In theory this should probably check end == '}', but
25066 cp_parser_save_member_function_body needs it to exit
25067 after either '}' or ')' when called with ')'. */
25068 if (depth == 0)
25069 return false;
25071 else if (token->type == CPP_OPEN_PAREN)
25073 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25074 if (depth == 0 && end == CPP_CLOSE_PAREN)
25075 return false;
25077 else if (token->type == CPP_PRAGMA)
25078 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25079 else if (token->type == end)
25080 return false;
25084 /* Like above, for caching a default argument or NSDMI. Both of these are
25085 terminated by a non-nested comma, but it can be unclear whether or not a
25086 comma is nested in a template argument list unless we do more parsing.
25087 In order to handle this ambiguity, when we encounter a ',' after a '<'
25088 we try to parse what follows as a parameter-declaration-list (in the
25089 case of a default argument) or a member-declarator (in the case of an
25090 NSDMI). If that succeeds, then we stop caching. */
25092 static tree
25093 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25095 unsigned depth = 0;
25096 int maybe_template_id = 0;
25097 cp_token *first_token;
25098 cp_token *token;
25099 tree default_argument;
25101 /* Add tokens until we have processed the entire default
25102 argument. We add the range [first_token, token). */
25103 first_token = cp_lexer_peek_token (parser->lexer);
25104 if (first_token->type == CPP_OPEN_BRACE)
25106 /* For list-initialization, this is straightforward. */
25107 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25108 token = cp_lexer_peek_token (parser->lexer);
25110 else while (true)
25112 bool done = false;
25114 /* Peek at the next token. */
25115 token = cp_lexer_peek_token (parser->lexer);
25116 /* What we do depends on what token we have. */
25117 switch (token->type)
25119 /* In valid code, a default argument must be
25120 immediately followed by a `,' `)', or `...'. */
25121 case CPP_COMMA:
25122 if (depth == 0 && maybe_template_id)
25124 /* If we've seen a '<', we might be in a
25125 template-argument-list. Until Core issue 325 is
25126 resolved, we don't know how this situation ought
25127 to be handled, so try to DTRT. We check whether
25128 what comes after the comma is a valid parameter
25129 declaration list. If it is, then the comma ends
25130 the default argument; otherwise the default
25131 argument continues. */
25132 bool error = false;
25134 /* Set ITALP so cp_parser_parameter_declaration_list
25135 doesn't decide to commit to this parse. */
25136 bool saved_italp = parser->in_template_argument_list_p;
25137 parser->in_template_argument_list_p = true;
25139 cp_parser_parse_tentatively (parser);
25140 cp_lexer_consume_token (parser->lexer);
25142 if (nsdmi)
25144 int ctor_dtor_or_conv_p;
25145 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25146 &ctor_dtor_or_conv_p,
25147 /*parenthesized_p=*/NULL,
25148 /*member_p=*/true);
25150 else
25152 begin_scope (sk_function_parms, NULL_TREE);
25153 cp_parser_parameter_declaration_list (parser, &error);
25154 pop_bindings_and_leave_scope ();
25156 if (!cp_parser_error_occurred (parser) && !error)
25157 done = true;
25158 cp_parser_abort_tentative_parse (parser);
25160 parser->in_template_argument_list_p = saved_italp;
25161 break;
25163 case CPP_CLOSE_PAREN:
25164 case CPP_ELLIPSIS:
25165 /* If we run into a non-nested `;', `}', or `]',
25166 then the code is invalid -- but the default
25167 argument is certainly over. */
25168 case CPP_SEMICOLON:
25169 case CPP_CLOSE_BRACE:
25170 case CPP_CLOSE_SQUARE:
25171 if (depth == 0
25172 /* Handle correctly int n = sizeof ... ( p ); */
25173 && !(nsdmi && token->type == CPP_ELLIPSIS))
25174 done = true;
25175 /* Update DEPTH, if necessary. */
25176 else if (token->type == CPP_CLOSE_PAREN
25177 || token->type == CPP_CLOSE_BRACE
25178 || token->type == CPP_CLOSE_SQUARE)
25179 --depth;
25180 break;
25182 case CPP_OPEN_PAREN:
25183 case CPP_OPEN_SQUARE:
25184 case CPP_OPEN_BRACE:
25185 ++depth;
25186 break;
25188 case CPP_LESS:
25189 if (depth == 0)
25190 /* This might be the comparison operator, or it might
25191 start a template argument list. */
25192 ++maybe_template_id;
25193 break;
25195 case CPP_RSHIFT:
25196 if (cxx_dialect == cxx98)
25197 break;
25198 /* Fall through for C++0x, which treats the `>>'
25199 operator like two `>' tokens in certain
25200 cases. */
25202 case CPP_GREATER:
25203 if (depth == 0)
25205 /* This might be an operator, or it might close a
25206 template argument list. But if a previous '<'
25207 started a template argument list, this will have
25208 closed it, so we can't be in one anymore. */
25209 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25210 if (maybe_template_id < 0)
25211 maybe_template_id = 0;
25213 break;
25215 /* If we run out of tokens, issue an error message. */
25216 case CPP_EOF:
25217 case CPP_PRAGMA_EOL:
25218 error_at (token->location, "file ends in default argument");
25219 done = true;
25220 break;
25222 case CPP_NAME:
25223 case CPP_SCOPE:
25224 /* In these cases, we should look for template-ids.
25225 For example, if the default argument is
25226 `X<int, double>()', we need to do name lookup to
25227 figure out whether or not `X' is a template; if
25228 so, the `,' does not end the default argument.
25230 That is not yet done. */
25231 break;
25233 default:
25234 break;
25237 /* If we've reached the end, stop. */
25238 if (done)
25239 break;
25241 /* Add the token to the token block. */
25242 token = cp_lexer_consume_token (parser->lexer);
25245 /* Create a DEFAULT_ARG to represent the unparsed default
25246 argument. */
25247 default_argument = make_node (DEFAULT_ARG);
25248 DEFARG_TOKENS (default_argument)
25249 = cp_token_cache_new (first_token, token);
25250 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25252 return default_argument;
25255 /* Begin parsing tentatively. We always save tokens while parsing
25256 tentatively so that if the tentative parsing fails we can restore the
25257 tokens. */
25259 static void
25260 cp_parser_parse_tentatively (cp_parser* parser)
25262 /* Enter a new parsing context. */
25263 parser->context = cp_parser_context_new (parser->context);
25264 /* Begin saving tokens. */
25265 cp_lexer_save_tokens (parser->lexer);
25266 /* In order to avoid repetitive access control error messages,
25267 access checks are queued up until we are no longer parsing
25268 tentatively. */
25269 push_deferring_access_checks (dk_deferred);
25272 /* Commit to the currently active tentative parse. */
25274 static void
25275 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25277 cp_parser_context *context;
25278 cp_lexer *lexer;
25280 /* Mark all of the levels as committed. */
25281 lexer = parser->lexer;
25282 for (context = parser->context; context->next; context = context->next)
25284 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25285 break;
25286 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25287 while (!cp_lexer_saving_tokens (lexer))
25288 lexer = lexer->next;
25289 cp_lexer_commit_tokens (lexer);
25293 /* Commit to the topmost currently active tentative parse.
25295 Note that this function shouldn't be called when there are
25296 irreversible side-effects while in a tentative state. For
25297 example, we shouldn't create a permanent entry in the symbol
25298 table, or issue an error message that might not apply if the
25299 tentative parse is aborted. */
25301 static void
25302 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25304 cp_parser_context *context = parser->context;
25305 cp_lexer *lexer = parser->lexer;
25307 if (context)
25309 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25310 return;
25311 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25313 while (!cp_lexer_saving_tokens (lexer))
25314 lexer = lexer->next;
25315 cp_lexer_commit_tokens (lexer);
25319 /* Abort the currently active tentative parse. All consumed tokens
25320 will be rolled back, and no diagnostics will be issued. */
25322 static void
25323 cp_parser_abort_tentative_parse (cp_parser* parser)
25325 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25326 || errorcount > 0);
25327 cp_parser_simulate_error (parser);
25328 /* Now, pretend that we want to see if the construct was
25329 successfully parsed. */
25330 cp_parser_parse_definitely (parser);
25333 /* Stop parsing tentatively. If a parse error has occurred, restore the
25334 token stream. Otherwise, commit to the tokens we have consumed.
25335 Returns true if no error occurred; false otherwise. */
25337 static bool
25338 cp_parser_parse_definitely (cp_parser* parser)
25340 bool error_occurred;
25341 cp_parser_context *context;
25343 /* Remember whether or not an error occurred, since we are about to
25344 destroy that information. */
25345 error_occurred = cp_parser_error_occurred (parser);
25346 /* Remove the topmost context from the stack. */
25347 context = parser->context;
25348 parser->context = context->next;
25349 /* If no parse errors occurred, commit to the tentative parse. */
25350 if (!error_occurred)
25352 /* Commit to the tokens read tentatively, unless that was
25353 already done. */
25354 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25355 cp_lexer_commit_tokens (parser->lexer);
25357 pop_to_parent_deferring_access_checks ();
25359 /* Otherwise, if errors occurred, roll back our state so that things
25360 are just as they were before we began the tentative parse. */
25361 else
25363 cp_lexer_rollback_tokens (parser->lexer);
25364 pop_deferring_access_checks ();
25366 /* Add the context to the front of the free list. */
25367 context->next = cp_parser_context_free_list;
25368 cp_parser_context_free_list = context;
25370 return !error_occurred;
25373 /* Returns true if we are parsing tentatively and are not committed to
25374 this tentative parse. */
25376 static bool
25377 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25379 return (cp_parser_parsing_tentatively (parser)
25380 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25383 /* Returns nonzero iff an error has occurred during the most recent
25384 tentative parse. */
25386 static bool
25387 cp_parser_error_occurred (cp_parser* parser)
25389 return (cp_parser_parsing_tentatively (parser)
25390 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25393 /* Returns nonzero if GNU extensions are allowed. */
25395 static bool
25396 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25398 return parser->allow_gnu_extensions_p;
25401 /* Objective-C++ Productions */
25404 /* Parse an Objective-C expression, which feeds into a primary-expression
25405 above.
25407 objc-expression:
25408 objc-message-expression
25409 objc-string-literal
25410 objc-encode-expression
25411 objc-protocol-expression
25412 objc-selector-expression
25414 Returns a tree representation of the expression. */
25416 static tree
25417 cp_parser_objc_expression (cp_parser* parser)
25419 /* Try to figure out what kind of declaration is present. */
25420 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25422 switch (kwd->type)
25424 case CPP_OPEN_SQUARE:
25425 return cp_parser_objc_message_expression (parser);
25427 case CPP_OBJC_STRING:
25428 kwd = cp_lexer_consume_token (parser->lexer);
25429 return objc_build_string_object (kwd->u.value);
25431 case CPP_KEYWORD:
25432 switch (kwd->keyword)
25434 case RID_AT_ENCODE:
25435 return cp_parser_objc_encode_expression (parser);
25437 case RID_AT_PROTOCOL:
25438 return cp_parser_objc_protocol_expression (parser);
25440 case RID_AT_SELECTOR:
25441 return cp_parser_objc_selector_expression (parser);
25443 default:
25444 break;
25446 default:
25447 error_at (kwd->location,
25448 "misplaced %<@%D%> Objective-C++ construct",
25449 kwd->u.value);
25450 cp_parser_skip_to_end_of_block_or_statement (parser);
25453 return error_mark_node;
25456 /* Parse an Objective-C message expression.
25458 objc-message-expression:
25459 [ objc-message-receiver objc-message-args ]
25461 Returns a representation of an Objective-C message. */
25463 static tree
25464 cp_parser_objc_message_expression (cp_parser* parser)
25466 tree receiver, messageargs;
25468 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25469 receiver = cp_parser_objc_message_receiver (parser);
25470 messageargs = cp_parser_objc_message_args (parser);
25471 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25473 return objc_build_message_expr (receiver, messageargs);
25476 /* Parse an objc-message-receiver.
25478 objc-message-receiver:
25479 expression
25480 simple-type-specifier
25482 Returns a representation of the type or expression. */
25484 static tree
25485 cp_parser_objc_message_receiver (cp_parser* parser)
25487 tree rcv;
25489 /* An Objective-C message receiver may be either (1) a type
25490 or (2) an expression. */
25491 cp_parser_parse_tentatively (parser);
25492 rcv = cp_parser_expression (parser, false, NULL);
25494 if (cp_parser_parse_definitely (parser))
25495 return rcv;
25497 rcv = cp_parser_simple_type_specifier (parser,
25498 /*decl_specs=*/NULL,
25499 CP_PARSER_FLAGS_NONE);
25501 return objc_get_class_reference (rcv);
25504 /* Parse the arguments and selectors comprising an Objective-C message.
25506 objc-message-args:
25507 objc-selector
25508 objc-selector-args
25509 objc-selector-args , objc-comma-args
25511 objc-selector-args:
25512 objc-selector [opt] : assignment-expression
25513 objc-selector-args objc-selector [opt] : assignment-expression
25515 objc-comma-args:
25516 assignment-expression
25517 objc-comma-args , assignment-expression
25519 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25520 selector arguments and TREE_VALUE containing a list of comma
25521 arguments. */
25523 static tree
25524 cp_parser_objc_message_args (cp_parser* parser)
25526 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25527 bool maybe_unary_selector_p = true;
25528 cp_token *token = cp_lexer_peek_token (parser->lexer);
25530 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25532 tree selector = NULL_TREE, arg;
25534 if (token->type != CPP_COLON)
25535 selector = cp_parser_objc_selector (parser);
25537 /* Detect if we have a unary selector. */
25538 if (maybe_unary_selector_p
25539 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25540 return build_tree_list (selector, NULL_TREE);
25542 maybe_unary_selector_p = false;
25543 cp_parser_require (parser, CPP_COLON, RT_COLON);
25544 arg = cp_parser_assignment_expression (parser, false, NULL);
25546 sel_args
25547 = chainon (sel_args,
25548 build_tree_list (selector, arg));
25550 token = cp_lexer_peek_token (parser->lexer);
25553 /* Handle non-selector arguments, if any. */
25554 while (token->type == CPP_COMMA)
25556 tree arg;
25558 cp_lexer_consume_token (parser->lexer);
25559 arg = cp_parser_assignment_expression (parser, false, NULL);
25561 addl_args
25562 = chainon (addl_args,
25563 build_tree_list (NULL_TREE, arg));
25565 token = cp_lexer_peek_token (parser->lexer);
25568 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25570 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25571 return build_tree_list (error_mark_node, error_mark_node);
25574 return build_tree_list (sel_args, addl_args);
25577 /* Parse an Objective-C encode expression.
25579 objc-encode-expression:
25580 @encode objc-typename
25582 Returns an encoded representation of the type argument. */
25584 static tree
25585 cp_parser_objc_encode_expression (cp_parser* parser)
25587 tree type;
25588 cp_token *token;
25590 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25591 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25592 token = cp_lexer_peek_token (parser->lexer);
25593 type = complete_type (cp_parser_type_id (parser));
25594 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25596 if (!type)
25598 error_at (token->location,
25599 "%<@encode%> must specify a type as an argument");
25600 return error_mark_node;
25603 /* This happens if we find @encode(T) (where T is a template
25604 typename or something dependent on a template typename) when
25605 parsing a template. In that case, we can't compile it
25606 immediately, but we rather create an AT_ENCODE_EXPR which will
25607 need to be instantiated when the template is used.
25609 if (dependent_type_p (type))
25611 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25612 TREE_READONLY (value) = 1;
25613 return value;
25616 return objc_build_encode_expr (type);
25619 /* Parse an Objective-C @defs expression. */
25621 static tree
25622 cp_parser_objc_defs_expression (cp_parser *parser)
25624 tree name;
25626 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25627 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25628 name = cp_parser_identifier (parser);
25629 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25631 return objc_get_class_ivars (name);
25634 /* Parse an Objective-C protocol expression.
25636 objc-protocol-expression:
25637 @protocol ( identifier )
25639 Returns a representation of the protocol expression. */
25641 static tree
25642 cp_parser_objc_protocol_expression (cp_parser* parser)
25644 tree proto;
25646 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25647 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25648 proto = cp_parser_identifier (parser);
25649 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25651 return objc_build_protocol_expr (proto);
25654 /* Parse an Objective-C selector expression.
25656 objc-selector-expression:
25657 @selector ( objc-method-signature )
25659 objc-method-signature:
25660 objc-selector
25661 objc-selector-seq
25663 objc-selector-seq:
25664 objc-selector :
25665 objc-selector-seq objc-selector :
25667 Returns a representation of the method selector. */
25669 static tree
25670 cp_parser_objc_selector_expression (cp_parser* parser)
25672 tree sel_seq = NULL_TREE;
25673 bool maybe_unary_selector_p = true;
25674 cp_token *token;
25675 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25677 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25678 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25679 token = cp_lexer_peek_token (parser->lexer);
25681 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25682 || token->type == CPP_SCOPE)
25684 tree selector = NULL_TREE;
25686 if (token->type != CPP_COLON
25687 || token->type == CPP_SCOPE)
25688 selector = cp_parser_objc_selector (parser);
25690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25691 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25693 /* Detect if we have a unary selector. */
25694 if (maybe_unary_selector_p)
25696 sel_seq = selector;
25697 goto finish_selector;
25699 else
25701 cp_parser_error (parser, "expected %<:%>");
25704 maybe_unary_selector_p = false;
25705 token = cp_lexer_consume_token (parser->lexer);
25707 if (token->type == CPP_SCOPE)
25709 sel_seq
25710 = chainon (sel_seq,
25711 build_tree_list (selector, NULL_TREE));
25712 sel_seq
25713 = chainon (sel_seq,
25714 build_tree_list (NULL_TREE, NULL_TREE));
25716 else
25717 sel_seq
25718 = chainon (sel_seq,
25719 build_tree_list (selector, NULL_TREE));
25721 token = cp_lexer_peek_token (parser->lexer);
25724 finish_selector:
25725 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25727 return objc_build_selector_expr (loc, sel_seq);
25730 /* Parse a list of identifiers.
25732 objc-identifier-list:
25733 identifier
25734 objc-identifier-list , identifier
25736 Returns a TREE_LIST of identifier nodes. */
25738 static tree
25739 cp_parser_objc_identifier_list (cp_parser* parser)
25741 tree identifier;
25742 tree list;
25743 cp_token *sep;
25745 identifier = cp_parser_identifier (parser);
25746 if (identifier == error_mark_node)
25747 return error_mark_node;
25749 list = build_tree_list (NULL_TREE, identifier);
25750 sep = cp_lexer_peek_token (parser->lexer);
25752 while (sep->type == CPP_COMMA)
25754 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25755 identifier = cp_parser_identifier (parser);
25756 if (identifier == error_mark_node)
25757 return list;
25759 list = chainon (list, build_tree_list (NULL_TREE,
25760 identifier));
25761 sep = cp_lexer_peek_token (parser->lexer);
25764 return list;
25767 /* Parse an Objective-C alias declaration.
25769 objc-alias-declaration:
25770 @compatibility_alias identifier identifier ;
25772 This function registers the alias mapping with the Objective-C front end.
25773 It returns nothing. */
25775 static void
25776 cp_parser_objc_alias_declaration (cp_parser* parser)
25778 tree alias, orig;
25780 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25781 alias = cp_parser_identifier (parser);
25782 orig = cp_parser_identifier (parser);
25783 objc_declare_alias (alias, orig);
25784 cp_parser_consume_semicolon_at_end_of_statement (parser);
25787 /* Parse an Objective-C class forward-declaration.
25789 objc-class-declaration:
25790 @class objc-identifier-list ;
25792 The function registers the forward declarations with the Objective-C
25793 front end. It returns nothing. */
25795 static void
25796 cp_parser_objc_class_declaration (cp_parser* parser)
25798 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25799 while (true)
25801 tree id;
25803 id = cp_parser_identifier (parser);
25804 if (id == error_mark_node)
25805 break;
25807 objc_declare_class (id);
25809 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25810 cp_lexer_consume_token (parser->lexer);
25811 else
25812 break;
25814 cp_parser_consume_semicolon_at_end_of_statement (parser);
25817 /* Parse a list of Objective-C protocol references.
25819 objc-protocol-refs-opt:
25820 objc-protocol-refs [opt]
25822 objc-protocol-refs:
25823 < objc-identifier-list >
25825 Returns a TREE_LIST of identifiers, if any. */
25827 static tree
25828 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25830 tree protorefs = NULL_TREE;
25832 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25834 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25835 protorefs = cp_parser_objc_identifier_list (parser);
25836 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25839 return protorefs;
25842 /* Parse a Objective-C visibility specification. */
25844 static void
25845 cp_parser_objc_visibility_spec (cp_parser* parser)
25847 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25849 switch (vis->keyword)
25851 case RID_AT_PRIVATE:
25852 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25853 break;
25854 case RID_AT_PROTECTED:
25855 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25856 break;
25857 case RID_AT_PUBLIC:
25858 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25859 break;
25860 case RID_AT_PACKAGE:
25861 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25862 break;
25863 default:
25864 return;
25867 /* Eat '@private'/'@protected'/'@public'. */
25868 cp_lexer_consume_token (parser->lexer);
25871 /* Parse an Objective-C method type. Return 'true' if it is a class
25872 (+) method, and 'false' if it is an instance (-) method. */
25874 static inline bool
25875 cp_parser_objc_method_type (cp_parser* parser)
25877 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25878 return true;
25879 else
25880 return false;
25883 /* Parse an Objective-C protocol qualifier. */
25885 static tree
25886 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25888 tree quals = NULL_TREE, node;
25889 cp_token *token = cp_lexer_peek_token (parser->lexer);
25891 node = token->u.value;
25893 while (node && identifier_p (node)
25894 && (node == ridpointers [(int) RID_IN]
25895 || node == ridpointers [(int) RID_OUT]
25896 || node == ridpointers [(int) RID_INOUT]
25897 || node == ridpointers [(int) RID_BYCOPY]
25898 || node == ridpointers [(int) RID_BYREF]
25899 || node == ridpointers [(int) RID_ONEWAY]))
25901 quals = tree_cons (NULL_TREE, node, quals);
25902 cp_lexer_consume_token (parser->lexer);
25903 token = cp_lexer_peek_token (parser->lexer);
25904 node = token->u.value;
25907 return quals;
25910 /* Parse an Objective-C typename. */
25912 static tree
25913 cp_parser_objc_typename (cp_parser* parser)
25915 tree type_name = NULL_TREE;
25917 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25919 tree proto_quals, cp_type = NULL_TREE;
25921 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25922 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25924 /* An ObjC type name may consist of just protocol qualifiers, in which
25925 case the type shall default to 'id'. */
25926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25928 cp_type = cp_parser_type_id (parser);
25930 /* If the type could not be parsed, an error has already
25931 been produced. For error recovery, behave as if it had
25932 not been specified, which will use the default type
25933 'id'. */
25934 if (cp_type == error_mark_node)
25936 cp_type = NULL_TREE;
25937 /* We need to skip to the closing parenthesis as
25938 cp_parser_type_id() does not seem to do it for
25939 us. */
25940 cp_parser_skip_to_closing_parenthesis (parser,
25941 /*recovering=*/true,
25942 /*or_comma=*/false,
25943 /*consume_paren=*/false);
25947 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25948 type_name = build_tree_list (proto_quals, cp_type);
25951 return type_name;
25954 /* Check to see if TYPE refers to an Objective-C selector name. */
25956 static bool
25957 cp_parser_objc_selector_p (enum cpp_ttype type)
25959 return (type == CPP_NAME || type == CPP_KEYWORD
25960 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25961 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25962 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25963 || type == CPP_XOR || type == CPP_XOR_EQ);
25966 /* Parse an Objective-C selector. */
25968 static tree
25969 cp_parser_objc_selector (cp_parser* parser)
25971 cp_token *token = cp_lexer_consume_token (parser->lexer);
25973 if (!cp_parser_objc_selector_p (token->type))
25975 error_at (token->location, "invalid Objective-C++ selector name");
25976 return error_mark_node;
25979 /* C++ operator names are allowed to appear in ObjC selectors. */
25980 switch (token->type)
25982 case CPP_AND_AND: return get_identifier ("and");
25983 case CPP_AND_EQ: return get_identifier ("and_eq");
25984 case CPP_AND: return get_identifier ("bitand");
25985 case CPP_OR: return get_identifier ("bitor");
25986 case CPP_COMPL: return get_identifier ("compl");
25987 case CPP_NOT: return get_identifier ("not");
25988 case CPP_NOT_EQ: return get_identifier ("not_eq");
25989 case CPP_OR_OR: return get_identifier ("or");
25990 case CPP_OR_EQ: return get_identifier ("or_eq");
25991 case CPP_XOR: return get_identifier ("xor");
25992 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25993 default: return token->u.value;
25997 /* Parse an Objective-C params list. */
25999 static tree
26000 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26002 tree params = NULL_TREE;
26003 bool maybe_unary_selector_p = true;
26004 cp_token *token = cp_lexer_peek_token (parser->lexer);
26006 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26008 tree selector = NULL_TREE, type_name, identifier;
26009 tree parm_attr = NULL_TREE;
26011 if (token->keyword == RID_ATTRIBUTE)
26012 break;
26014 if (token->type != CPP_COLON)
26015 selector = cp_parser_objc_selector (parser);
26017 /* Detect if we have a unary selector. */
26018 if (maybe_unary_selector_p
26019 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26021 params = selector; /* Might be followed by attributes. */
26022 break;
26025 maybe_unary_selector_p = false;
26026 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26028 /* Something went quite wrong. There should be a colon
26029 here, but there is not. Stop parsing parameters. */
26030 break;
26032 type_name = cp_parser_objc_typename (parser);
26033 /* New ObjC allows attributes on parameters too. */
26034 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26035 parm_attr = cp_parser_attributes_opt (parser);
26036 identifier = cp_parser_identifier (parser);
26038 params
26039 = chainon (params,
26040 objc_build_keyword_decl (selector,
26041 type_name,
26042 identifier,
26043 parm_attr));
26045 token = cp_lexer_peek_token (parser->lexer);
26048 if (params == NULL_TREE)
26050 cp_parser_error (parser, "objective-c++ method declaration is expected");
26051 return error_mark_node;
26054 /* We allow tail attributes for the method. */
26055 if (token->keyword == RID_ATTRIBUTE)
26057 *attributes = cp_parser_attributes_opt (parser);
26058 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26059 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26060 return params;
26061 cp_parser_error (parser,
26062 "method attributes must be specified at the end");
26063 return error_mark_node;
26066 if (params == NULL_TREE)
26068 cp_parser_error (parser, "objective-c++ method declaration is expected");
26069 return error_mark_node;
26071 return params;
26074 /* Parse the non-keyword Objective-C params. */
26076 static tree
26077 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26078 tree* attributes)
26080 tree params = make_node (TREE_LIST);
26081 cp_token *token = cp_lexer_peek_token (parser->lexer);
26082 *ellipsisp = false; /* Initially, assume no ellipsis. */
26084 while (token->type == CPP_COMMA)
26086 cp_parameter_declarator *parmdecl;
26087 tree parm;
26089 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26090 token = cp_lexer_peek_token (parser->lexer);
26092 if (token->type == CPP_ELLIPSIS)
26094 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26095 *ellipsisp = true;
26096 token = cp_lexer_peek_token (parser->lexer);
26097 break;
26100 /* TODO: parse attributes for tail parameters. */
26101 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26102 parm = grokdeclarator (parmdecl->declarator,
26103 &parmdecl->decl_specifiers,
26104 PARM, /*initialized=*/0,
26105 /*attrlist=*/NULL);
26107 chainon (params, build_tree_list (NULL_TREE, parm));
26108 token = cp_lexer_peek_token (parser->lexer);
26111 /* We allow tail attributes for the method. */
26112 if (token->keyword == RID_ATTRIBUTE)
26114 if (*attributes == NULL_TREE)
26116 *attributes = cp_parser_attributes_opt (parser);
26117 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26118 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26119 return params;
26121 else
26122 /* We have an error, but parse the attributes, so that we can
26123 carry on. */
26124 *attributes = cp_parser_attributes_opt (parser);
26126 cp_parser_error (parser,
26127 "method attributes must be specified at the end");
26128 return error_mark_node;
26131 return params;
26134 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26136 static void
26137 cp_parser_objc_interstitial_code (cp_parser* parser)
26139 cp_token *token = cp_lexer_peek_token (parser->lexer);
26141 /* If the next token is `extern' and the following token is a string
26142 literal, then we have a linkage specification. */
26143 if (token->keyword == RID_EXTERN
26144 && cp_parser_is_pure_string_literal
26145 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26146 cp_parser_linkage_specification (parser);
26147 /* Handle #pragma, if any. */
26148 else if (token->type == CPP_PRAGMA)
26149 cp_parser_pragma (parser, pragma_objc_icode);
26150 /* Allow stray semicolons. */
26151 else if (token->type == CPP_SEMICOLON)
26152 cp_lexer_consume_token (parser->lexer);
26153 /* Mark methods as optional or required, when building protocols. */
26154 else if (token->keyword == RID_AT_OPTIONAL)
26156 cp_lexer_consume_token (parser->lexer);
26157 objc_set_method_opt (true);
26159 else if (token->keyword == RID_AT_REQUIRED)
26161 cp_lexer_consume_token (parser->lexer);
26162 objc_set_method_opt (false);
26164 else if (token->keyword == RID_NAMESPACE)
26165 cp_parser_namespace_definition (parser);
26166 /* Other stray characters must generate errors. */
26167 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26169 cp_lexer_consume_token (parser->lexer);
26170 error ("stray %qs between Objective-C++ methods",
26171 token->type == CPP_OPEN_BRACE ? "{" : "}");
26173 /* Finally, try to parse a block-declaration, or a function-definition. */
26174 else
26175 cp_parser_block_declaration (parser, /*statement_p=*/false);
26178 /* Parse a method signature. */
26180 static tree
26181 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26183 tree rettype, kwdparms, optparms;
26184 bool ellipsis = false;
26185 bool is_class_method;
26187 is_class_method = cp_parser_objc_method_type (parser);
26188 rettype = cp_parser_objc_typename (parser);
26189 *attributes = NULL_TREE;
26190 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26191 if (kwdparms == error_mark_node)
26192 return error_mark_node;
26193 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26194 if (optparms == error_mark_node)
26195 return error_mark_node;
26197 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26200 static bool
26201 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26203 tree tattr;
26204 cp_lexer_save_tokens (parser->lexer);
26205 tattr = cp_parser_attributes_opt (parser);
26206 gcc_assert (tattr) ;
26208 /* If the attributes are followed by a method introducer, this is not allowed.
26209 Dump the attributes and flag the situation. */
26210 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26211 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26212 return true;
26214 /* Otherwise, the attributes introduce some interstitial code, possibly so
26215 rewind to allow that check. */
26216 cp_lexer_rollback_tokens (parser->lexer);
26217 return false;
26220 /* Parse an Objective-C method prototype list. */
26222 static void
26223 cp_parser_objc_method_prototype_list (cp_parser* parser)
26225 cp_token *token = cp_lexer_peek_token (parser->lexer);
26227 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26229 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26231 tree attributes, sig;
26232 bool is_class_method;
26233 if (token->type == CPP_PLUS)
26234 is_class_method = true;
26235 else
26236 is_class_method = false;
26237 sig = cp_parser_objc_method_signature (parser, &attributes);
26238 if (sig == error_mark_node)
26240 cp_parser_skip_to_end_of_block_or_statement (parser);
26241 token = cp_lexer_peek_token (parser->lexer);
26242 continue;
26244 objc_add_method_declaration (is_class_method, sig, attributes);
26245 cp_parser_consume_semicolon_at_end_of_statement (parser);
26247 else if (token->keyword == RID_AT_PROPERTY)
26248 cp_parser_objc_at_property_declaration (parser);
26249 else if (token->keyword == RID_ATTRIBUTE
26250 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26251 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26252 OPT_Wattributes,
26253 "prefix attributes are ignored for methods");
26254 else
26255 /* Allow for interspersed non-ObjC++ code. */
26256 cp_parser_objc_interstitial_code (parser);
26258 token = cp_lexer_peek_token (parser->lexer);
26261 if (token->type != CPP_EOF)
26262 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26263 else
26264 cp_parser_error (parser, "expected %<@end%>");
26266 objc_finish_interface ();
26269 /* Parse an Objective-C method definition list. */
26271 static void
26272 cp_parser_objc_method_definition_list (cp_parser* parser)
26274 cp_token *token = cp_lexer_peek_token (parser->lexer);
26276 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26278 tree meth;
26280 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26282 cp_token *ptk;
26283 tree sig, attribute;
26284 bool is_class_method;
26285 if (token->type == CPP_PLUS)
26286 is_class_method = true;
26287 else
26288 is_class_method = false;
26289 push_deferring_access_checks (dk_deferred);
26290 sig = cp_parser_objc_method_signature (parser, &attribute);
26291 if (sig == error_mark_node)
26293 cp_parser_skip_to_end_of_block_or_statement (parser);
26294 token = cp_lexer_peek_token (parser->lexer);
26295 continue;
26297 objc_start_method_definition (is_class_method, sig, attribute,
26298 NULL_TREE);
26300 /* For historical reasons, we accept an optional semicolon. */
26301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26302 cp_lexer_consume_token (parser->lexer);
26304 ptk = cp_lexer_peek_token (parser->lexer);
26305 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26306 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26308 perform_deferred_access_checks (tf_warning_or_error);
26309 stop_deferring_access_checks ();
26310 meth = cp_parser_function_definition_after_declarator (parser,
26311 false);
26312 pop_deferring_access_checks ();
26313 objc_finish_method_definition (meth);
26316 /* The following case will be removed once @synthesize is
26317 completely implemented. */
26318 else if (token->keyword == RID_AT_PROPERTY)
26319 cp_parser_objc_at_property_declaration (parser);
26320 else if (token->keyword == RID_AT_SYNTHESIZE)
26321 cp_parser_objc_at_synthesize_declaration (parser);
26322 else if (token->keyword == RID_AT_DYNAMIC)
26323 cp_parser_objc_at_dynamic_declaration (parser);
26324 else if (token->keyword == RID_ATTRIBUTE
26325 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26326 warning_at (token->location, OPT_Wattributes,
26327 "prefix attributes are ignored for methods");
26328 else
26329 /* Allow for interspersed non-ObjC++ code. */
26330 cp_parser_objc_interstitial_code (parser);
26332 token = cp_lexer_peek_token (parser->lexer);
26335 if (token->type != CPP_EOF)
26336 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26337 else
26338 cp_parser_error (parser, "expected %<@end%>");
26340 objc_finish_implementation ();
26343 /* Parse Objective-C ivars. */
26345 static void
26346 cp_parser_objc_class_ivars (cp_parser* parser)
26348 cp_token *token = cp_lexer_peek_token (parser->lexer);
26350 if (token->type != CPP_OPEN_BRACE)
26351 return; /* No ivars specified. */
26353 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26354 token = cp_lexer_peek_token (parser->lexer);
26356 while (token->type != CPP_CLOSE_BRACE
26357 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26359 cp_decl_specifier_seq declspecs;
26360 int decl_class_or_enum_p;
26361 tree prefix_attributes;
26363 cp_parser_objc_visibility_spec (parser);
26365 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26366 break;
26368 cp_parser_decl_specifier_seq (parser,
26369 CP_PARSER_FLAGS_OPTIONAL,
26370 &declspecs,
26371 &decl_class_or_enum_p);
26373 /* auto, register, static, extern, mutable. */
26374 if (declspecs.storage_class != sc_none)
26376 cp_parser_error (parser, "invalid type for instance variable");
26377 declspecs.storage_class = sc_none;
26380 /* thread_local. */
26381 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26383 cp_parser_error (parser, "invalid type for instance variable");
26384 declspecs.locations[ds_thread] = 0;
26387 /* typedef. */
26388 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26390 cp_parser_error (parser, "invalid type for instance variable");
26391 declspecs.locations[ds_typedef] = 0;
26394 prefix_attributes = declspecs.attributes;
26395 declspecs.attributes = NULL_TREE;
26397 /* Keep going until we hit the `;' at the end of the
26398 declaration. */
26399 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26401 tree width = NULL_TREE, attributes, first_attribute, decl;
26402 cp_declarator *declarator = NULL;
26403 int ctor_dtor_or_conv_p;
26405 /* Check for a (possibly unnamed) bitfield declaration. */
26406 token = cp_lexer_peek_token (parser->lexer);
26407 if (token->type == CPP_COLON)
26408 goto eat_colon;
26410 if (token->type == CPP_NAME
26411 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26412 == CPP_COLON))
26414 /* Get the name of the bitfield. */
26415 declarator = make_id_declarator (NULL_TREE,
26416 cp_parser_identifier (parser),
26417 sfk_none);
26419 eat_colon:
26420 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26421 /* Get the width of the bitfield. */
26422 width
26423 = cp_parser_constant_expression (parser,
26424 /*allow_non_constant=*/false,
26425 NULL);
26427 else
26429 /* Parse the declarator. */
26430 declarator
26431 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26432 &ctor_dtor_or_conv_p,
26433 /*parenthesized_p=*/NULL,
26434 /*member_p=*/false);
26437 /* Look for attributes that apply to the ivar. */
26438 attributes = cp_parser_attributes_opt (parser);
26439 /* Remember which attributes are prefix attributes and
26440 which are not. */
26441 first_attribute = attributes;
26442 /* Combine the attributes. */
26443 attributes = chainon (prefix_attributes, attributes);
26445 if (width)
26446 /* Create the bitfield declaration. */
26447 decl = grokbitfield (declarator, &declspecs,
26448 width,
26449 attributes);
26450 else
26451 decl = grokfield (declarator, &declspecs,
26452 NULL_TREE, /*init_const_expr_p=*/false,
26453 NULL_TREE, attributes);
26455 /* Add the instance variable. */
26456 if (decl != error_mark_node && decl != NULL_TREE)
26457 objc_add_instance_variable (decl);
26459 /* Reset PREFIX_ATTRIBUTES. */
26460 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26461 attributes = TREE_CHAIN (attributes);
26462 if (attributes)
26463 TREE_CHAIN (attributes) = NULL_TREE;
26465 token = cp_lexer_peek_token (parser->lexer);
26467 if (token->type == CPP_COMMA)
26469 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26470 continue;
26472 break;
26475 cp_parser_consume_semicolon_at_end_of_statement (parser);
26476 token = cp_lexer_peek_token (parser->lexer);
26479 if (token->keyword == RID_AT_END)
26480 cp_parser_error (parser, "expected %<}%>");
26482 /* Do not consume the RID_AT_END, so it will be read again as terminating
26483 the @interface of @implementation. */
26484 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26485 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26487 /* For historical reasons, we accept an optional semicolon. */
26488 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26489 cp_lexer_consume_token (parser->lexer);
26492 /* Parse an Objective-C protocol declaration. */
26494 static void
26495 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26497 tree proto, protorefs;
26498 cp_token *tok;
26500 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26503 tok = cp_lexer_peek_token (parser->lexer);
26504 error_at (tok->location, "identifier expected after %<@protocol%>");
26505 cp_parser_consume_semicolon_at_end_of_statement (parser);
26506 return;
26509 /* See if we have a forward declaration or a definition. */
26510 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26512 /* Try a forward declaration first. */
26513 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26515 while (true)
26517 tree id;
26519 id = cp_parser_identifier (parser);
26520 if (id == error_mark_node)
26521 break;
26523 objc_declare_protocol (id, attributes);
26525 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26526 cp_lexer_consume_token (parser->lexer);
26527 else
26528 break;
26530 cp_parser_consume_semicolon_at_end_of_statement (parser);
26533 /* Ok, we got a full-fledged definition (or at least should). */
26534 else
26536 proto = cp_parser_identifier (parser);
26537 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26538 objc_start_protocol (proto, protorefs, attributes);
26539 cp_parser_objc_method_prototype_list (parser);
26543 /* Parse an Objective-C superclass or category. */
26545 static void
26546 cp_parser_objc_superclass_or_category (cp_parser *parser,
26547 bool iface_p,
26548 tree *super,
26549 tree *categ, bool *is_class_extension)
26551 cp_token *next = cp_lexer_peek_token (parser->lexer);
26553 *super = *categ = NULL_TREE;
26554 *is_class_extension = false;
26555 if (next->type == CPP_COLON)
26557 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26558 *super = cp_parser_identifier (parser);
26560 else if (next->type == CPP_OPEN_PAREN)
26562 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26564 /* If there is no category name, and this is an @interface, we
26565 have a class extension. */
26566 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26568 *categ = NULL_TREE;
26569 *is_class_extension = true;
26571 else
26572 *categ = cp_parser_identifier (parser);
26574 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26578 /* Parse an Objective-C class interface. */
26580 static void
26581 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26583 tree name, super, categ, protos;
26584 bool is_class_extension;
26586 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26587 name = cp_parser_identifier (parser);
26588 if (name == error_mark_node)
26590 /* It's hard to recover because even if valid @interface stuff
26591 is to follow, we can't compile it (or validate it) if we
26592 don't even know which class it refers to. Let's assume this
26593 was a stray '@interface' token in the stream and skip it.
26595 return;
26597 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26598 &is_class_extension);
26599 protos = cp_parser_objc_protocol_refs_opt (parser);
26601 /* We have either a class or a category on our hands. */
26602 if (categ || is_class_extension)
26603 objc_start_category_interface (name, categ, protos, attributes);
26604 else
26606 objc_start_class_interface (name, super, protos, attributes);
26607 /* Handle instance variable declarations, if any. */
26608 cp_parser_objc_class_ivars (parser);
26609 objc_continue_interface ();
26612 cp_parser_objc_method_prototype_list (parser);
26615 /* Parse an Objective-C class implementation. */
26617 static void
26618 cp_parser_objc_class_implementation (cp_parser* parser)
26620 tree name, super, categ;
26621 bool is_class_extension;
26623 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26624 name = cp_parser_identifier (parser);
26625 if (name == error_mark_node)
26627 /* It's hard to recover because even if valid @implementation
26628 stuff is to follow, we can't compile it (or validate it) if
26629 we don't even know which class it refers to. Let's assume
26630 this was a stray '@implementation' token in the stream and
26631 skip it.
26633 return;
26635 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26636 &is_class_extension);
26638 /* We have either a class or a category on our hands. */
26639 if (categ)
26640 objc_start_category_implementation (name, categ);
26641 else
26643 objc_start_class_implementation (name, super);
26644 /* Handle instance variable declarations, if any. */
26645 cp_parser_objc_class_ivars (parser);
26646 objc_continue_implementation ();
26649 cp_parser_objc_method_definition_list (parser);
26652 /* Consume the @end token and finish off the implementation. */
26654 static void
26655 cp_parser_objc_end_implementation (cp_parser* parser)
26657 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26658 objc_finish_implementation ();
26661 /* Parse an Objective-C declaration. */
26663 static void
26664 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26666 /* Try to figure out what kind of declaration is present. */
26667 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26669 if (attributes)
26670 switch (kwd->keyword)
26672 case RID_AT_ALIAS:
26673 case RID_AT_CLASS:
26674 case RID_AT_END:
26675 error_at (kwd->location, "attributes may not be specified before"
26676 " the %<@%D%> Objective-C++ keyword",
26677 kwd->u.value);
26678 attributes = NULL;
26679 break;
26680 case RID_AT_IMPLEMENTATION:
26681 warning_at (kwd->location, OPT_Wattributes,
26682 "prefix attributes are ignored before %<@%D%>",
26683 kwd->u.value);
26684 attributes = NULL;
26685 default:
26686 break;
26689 switch (kwd->keyword)
26691 case RID_AT_ALIAS:
26692 cp_parser_objc_alias_declaration (parser);
26693 break;
26694 case RID_AT_CLASS:
26695 cp_parser_objc_class_declaration (parser);
26696 break;
26697 case RID_AT_PROTOCOL:
26698 cp_parser_objc_protocol_declaration (parser, attributes);
26699 break;
26700 case RID_AT_INTERFACE:
26701 cp_parser_objc_class_interface (parser, attributes);
26702 break;
26703 case RID_AT_IMPLEMENTATION:
26704 cp_parser_objc_class_implementation (parser);
26705 break;
26706 case RID_AT_END:
26707 cp_parser_objc_end_implementation (parser);
26708 break;
26709 default:
26710 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26711 kwd->u.value);
26712 cp_parser_skip_to_end_of_block_or_statement (parser);
26716 /* Parse an Objective-C try-catch-finally statement.
26718 objc-try-catch-finally-stmt:
26719 @try compound-statement objc-catch-clause-seq [opt]
26720 objc-finally-clause [opt]
26722 objc-catch-clause-seq:
26723 objc-catch-clause objc-catch-clause-seq [opt]
26725 objc-catch-clause:
26726 @catch ( objc-exception-declaration ) compound-statement
26728 objc-finally-clause:
26729 @finally compound-statement
26731 objc-exception-declaration:
26732 parameter-declaration
26733 '...'
26735 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26737 Returns NULL_TREE.
26739 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26740 for C. Keep them in sync. */
26742 static tree
26743 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26745 location_t location;
26746 tree stmt;
26748 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26749 location = cp_lexer_peek_token (parser->lexer)->location;
26750 objc_maybe_warn_exceptions (location);
26751 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26752 node, lest it get absorbed into the surrounding block. */
26753 stmt = push_stmt_list ();
26754 cp_parser_compound_statement (parser, NULL, false, false);
26755 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26757 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26759 cp_parameter_declarator *parm;
26760 tree parameter_declaration = error_mark_node;
26761 bool seen_open_paren = false;
26763 cp_lexer_consume_token (parser->lexer);
26764 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26765 seen_open_paren = true;
26766 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26768 /* We have "@catch (...)" (where the '...' are literally
26769 what is in the code). Skip the '...'.
26770 parameter_declaration is set to NULL_TREE, and
26771 objc_being_catch_clauses() knows that that means
26772 '...'. */
26773 cp_lexer_consume_token (parser->lexer);
26774 parameter_declaration = NULL_TREE;
26776 else
26778 /* We have "@catch (NSException *exception)" or something
26779 like that. Parse the parameter declaration. */
26780 parm = cp_parser_parameter_declaration (parser, false, NULL);
26781 if (parm == NULL)
26782 parameter_declaration = error_mark_node;
26783 else
26784 parameter_declaration = grokdeclarator (parm->declarator,
26785 &parm->decl_specifiers,
26786 PARM, /*initialized=*/0,
26787 /*attrlist=*/NULL);
26789 if (seen_open_paren)
26790 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26791 else
26793 /* If there was no open parenthesis, we are recovering from
26794 an error, and we are trying to figure out what mistake
26795 the user has made. */
26797 /* If there is an immediate closing parenthesis, the user
26798 probably forgot the opening one (ie, they typed "@catch
26799 NSException *e)". Parse the closing parenthesis and keep
26800 going. */
26801 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26802 cp_lexer_consume_token (parser->lexer);
26804 /* If these is no immediate closing parenthesis, the user
26805 probably doesn't know that parenthesis are required at
26806 all (ie, they typed "@catch NSException *e"). So, just
26807 forget about the closing parenthesis and keep going. */
26809 objc_begin_catch_clause (parameter_declaration);
26810 cp_parser_compound_statement (parser, NULL, false, false);
26811 objc_finish_catch_clause ();
26813 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26815 cp_lexer_consume_token (parser->lexer);
26816 location = cp_lexer_peek_token (parser->lexer)->location;
26817 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26818 node, lest it get absorbed into the surrounding block. */
26819 stmt = push_stmt_list ();
26820 cp_parser_compound_statement (parser, NULL, false, false);
26821 objc_build_finally_clause (location, pop_stmt_list (stmt));
26824 return objc_finish_try_stmt ();
26827 /* Parse an Objective-C synchronized statement.
26829 objc-synchronized-stmt:
26830 @synchronized ( expression ) compound-statement
26832 Returns NULL_TREE. */
26834 static tree
26835 cp_parser_objc_synchronized_statement (cp_parser *parser)
26837 location_t location;
26838 tree lock, stmt;
26840 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26842 location = cp_lexer_peek_token (parser->lexer)->location;
26843 objc_maybe_warn_exceptions (location);
26844 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26845 lock = cp_parser_expression (parser, false, NULL);
26846 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26848 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26849 node, lest it get absorbed into the surrounding block. */
26850 stmt = push_stmt_list ();
26851 cp_parser_compound_statement (parser, NULL, false, false);
26853 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26856 /* Parse an Objective-C throw statement.
26858 objc-throw-stmt:
26859 @throw assignment-expression [opt] ;
26861 Returns a constructed '@throw' statement. */
26863 static tree
26864 cp_parser_objc_throw_statement (cp_parser *parser)
26866 tree expr = NULL_TREE;
26867 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26869 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26872 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26874 cp_parser_consume_semicolon_at_end_of_statement (parser);
26876 return objc_build_throw_stmt (loc, expr);
26879 /* Parse an Objective-C statement. */
26881 static tree
26882 cp_parser_objc_statement (cp_parser * parser)
26884 /* Try to figure out what kind of declaration is present. */
26885 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26887 switch (kwd->keyword)
26889 case RID_AT_TRY:
26890 return cp_parser_objc_try_catch_finally_statement (parser);
26891 case RID_AT_SYNCHRONIZED:
26892 return cp_parser_objc_synchronized_statement (parser);
26893 case RID_AT_THROW:
26894 return cp_parser_objc_throw_statement (parser);
26895 default:
26896 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26897 kwd->u.value);
26898 cp_parser_skip_to_end_of_block_or_statement (parser);
26901 return error_mark_node;
26904 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26905 look ahead to see if an objc keyword follows the attributes. This
26906 is to detect the use of prefix attributes on ObjC @interface and
26907 @protocol. */
26909 static bool
26910 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26912 cp_lexer_save_tokens (parser->lexer);
26913 *attrib = cp_parser_attributes_opt (parser);
26914 gcc_assert (*attrib);
26915 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26917 cp_lexer_commit_tokens (parser->lexer);
26918 return true;
26920 cp_lexer_rollback_tokens (parser->lexer);
26921 return false;
26924 /* This routine is a minimal replacement for
26925 c_parser_struct_declaration () used when parsing the list of
26926 types/names or ObjC++ properties. For example, when parsing the
26927 code
26929 @property (readonly) int a, b, c;
26931 this function is responsible for parsing "int a, int b, int c" and
26932 returning the declarations as CHAIN of DECLs.
26934 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26935 similar parsing. */
26936 static tree
26937 cp_parser_objc_struct_declaration (cp_parser *parser)
26939 tree decls = NULL_TREE;
26940 cp_decl_specifier_seq declspecs;
26941 int decl_class_or_enum_p;
26942 tree prefix_attributes;
26944 cp_parser_decl_specifier_seq (parser,
26945 CP_PARSER_FLAGS_NONE,
26946 &declspecs,
26947 &decl_class_or_enum_p);
26949 if (declspecs.type == error_mark_node)
26950 return error_mark_node;
26952 /* auto, register, static, extern, mutable. */
26953 if (declspecs.storage_class != sc_none)
26955 cp_parser_error (parser, "invalid type for property");
26956 declspecs.storage_class = sc_none;
26959 /* thread_local. */
26960 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26962 cp_parser_error (parser, "invalid type for property");
26963 declspecs.locations[ds_thread] = 0;
26966 /* typedef. */
26967 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26969 cp_parser_error (parser, "invalid type for property");
26970 declspecs.locations[ds_typedef] = 0;
26973 prefix_attributes = declspecs.attributes;
26974 declspecs.attributes = NULL_TREE;
26976 /* Keep going until we hit the `;' at the end of the declaration. */
26977 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26979 tree attributes, first_attribute, decl;
26980 cp_declarator *declarator;
26981 cp_token *token;
26983 /* Parse the declarator. */
26984 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26985 NULL, NULL, false);
26987 /* Look for attributes that apply to the ivar. */
26988 attributes = cp_parser_attributes_opt (parser);
26989 /* Remember which attributes are prefix attributes and
26990 which are not. */
26991 first_attribute = attributes;
26992 /* Combine the attributes. */
26993 attributes = chainon (prefix_attributes, attributes);
26995 decl = grokfield (declarator, &declspecs,
26996 NULL_TREE, /*init_const_expr_p=*/false,
26997 NULL_TREE, attributes);
26999 if (decl == error_mark_node || decl == NULL_TREE)
27000 return error_mark_node;
27002 /* Reset PREFIX_ATTRIBUTES. */
27003 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27004 attributes = TREE_CHAIN (attributes);
27005 if (attributes)
27006 TREE_CHAIN (attributes) = NULL_TREE;
27008 DECL_CHAIN (decl) = decls;
27009 decls = decl;
27011 token = cp_lexer_peek_token (parser->lexer);
27012 if (token->type == CPP_COMMA)
27014 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27015 continue;
27017 else
27018 break;
27020 return decls;
27023 /* Parse an Objective-C @property declaration. The syntax is:
27025 objc-property-declaration:
27026 '@property' objc-property-attributes[opt] struct-declaration ;
27028 objc-property-attributes:
27029 '(' objc-property-attribute-list ')'
27031 objc-property-attribute-list:
27032 objc-property-attribute
27033 objc-property-attribute-list, objc-property-attribute
27035 objc-property-attribute
27036 'getter' = identifier
27037 'setter' = identifier
27038 'readonly'
27039 'readwrite'
27040 'assign'
27041 'retain'
27042 'copy'
27043 'nonatomic'
27045 For example:
27046 @property NSString *name;
27047 @property (readonly) id object;
27048 @property (retain, nonatomic, getter=getTheName) id name;
27049 @property int a, b, c;
27051 PS: This function is identical to
27052 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27053 static void
27054 cp_parser_objc_at_property_declaration (cp_parser *parser)
27056 /* The following variables hold the attributes of the properties as
27057 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27058 seen. When we see an attribute, we set them to 'true' (if they
27059 are boolean properties) or to the identifier (if they have an
27060 argument, ie, for getter and setter). Note that here we only
27061 parse the list of attributes, check the syntax and accumulate the
27062 attributes that we find. objc_add_property_declaration() will
27063 then process the information. */
27064 bool property_assign = false;
27065 bool property_copy = false;
27066 tree property_getter_ident = NULL_TREE;
27067 bool property_nonatomic = false;
27068 bool property_readonly = false;
27069 bool property_readwrite = false;
27070 bool property_retain = false;
27071 tree property_setter_ident = NULL_TREE;
27073 /* 'properties' is the list of properties that we read. Usually a
27074 single one, but maybe more (eg, in "@property int a, b, c;" there
27075 are three). */
27076 tree properties;
27077 location_t loc;
27079 loc = cp_lexer_peek_token (parser->lexer)->location;
27081 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27083 /* Parse the optional attribute list... */
27084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27086 /* Eat the '('. */
27087 cp_lexer_consume_token (parser->lexer);
27089 while (true)
27091 bool syntax_error = false;
27092 cp_token *token = cp_lexer_peek_token (parser->lexer);
27093 enum rid keyword;
27095 if (token->type != CPP_NAME)
27097 cp_parser_error (parser, "expected identifier");
27098 break;
27100 keyword = C_RID_CODE (token->u.value);
27101 cp_lexer_consume_token (parser->lexer);
27102 switch (keyword)
27104 case RID_ASSIGN: property_assign = true; break;
27105 case RID_COPY: property_copy = true; break;
27106 case RID_NONATOMIC: property_nonatomic = true; break;
27107 case RID_READONLY: property_readonly = true; break;
27108 case RID_READWRITE: property_readwrite = true; break;
27109 case RID_RETAIN: property_retain = true; break;
27111 case RID_GETTER:
27112 case RID_SETTER:
27113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27115 if (keyword == RID_GETTER)
27116 cp_parser_error (parser,
27117 "missing %<=%> (after %<getter%> attribute)");
27118 else
27119 cp_parser_error (parser,
27120 "missing %<=%> (after %<setter%> attribute)");
27121 syntax_error = true;
27122 break;
27124 cp_lexer_consume_token (parser->lexer); /* eat the = */
27125 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27127 cp_parser_error (parser, "expected identifier");
27128 syntax_error = true;
27129 break;
27131 if (keyword == RID_SETTER)
27133 if (property_setter_ident != NULL_TREE)
27135 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27136 cp_lexer_consume_token (parser->lexer);
27138 else
27139 property_setter_ident = cp_parser_objc_selector (parser);
27140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27141 cp_parser_error (parser, "setter name must terminate with %<:%>");
27142 else
27143 cp_lexer_consume_token (parser->lexer);
27145 else
27147 if (property_getter_ident != NULL_TREE)
27149 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27150 cp_lexer_consume_token (parser->lexer);
27152 else
27153 property_getter_ident = cp_parser_objc_selector (parser);
27155 break;
27156 default:
27157 cp_parser_error (parser, "unknown property attribute");
27158 syntax_error = true;
27159 break;
27162 if (syntax_error)
27163 break;
27165 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27166 cp_lexer_consume_token (parser->lexer);
27167 else
27168 break;
27171 /* FIXME: "@property (setter, assign);" will generate a spurious
27172 "error: expected ‘)’ before ‘,’ token". This is because
27173 cp_parser_require, unlike the C counterpart, will produce an
27174 error even if we are in error recovery. */
27175 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27177 cp_parser_skip_to_closing_parenthesis (parser,
27178 /*recovering=*/true,
27179 /*or_comma=*/false,
27180 /*consume_paren=*/true);
27184 /* ... and the property declaration(s). */
27185 properties = cp_parser_objc_struct_declaration (parser);
27187 if (properties == error_mark_node)
27189 cp_parser_skip_to_end_of_statement (parser);
27190 /* If the next token is now a `;', consume it. */
27191 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27192 cp_lexer_consume_token (parser->lexer);
27193 return;
27196 if (properties == NULL_TREE)
27197 cp_parser_error (parser, "expected identifier");
27198 else
27200 /* Comma-separated properties are chained together in
27201 reverse order; add them one by one. */
27202 properties = nreverse (properties);
27204 for (; properties; properties = TREE_CHAIN (properties))
27205 objc_add_property_declaration (loc, copy_node (properties),
27206 property_readonly, property_readwrite,
27207 property_assign, property_retain,
27208 property_copy, property_nonatomic,
27209 property_getter_ident, property_setter_ident);
27212 cp_parser_consume_semicolon_at_end_of_statement (parser);
27215 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27217 objc-synthesize-declaration:
27218 @synthesize objc-synthesize-identifier-list ;
27220 objc-synthesize-identifier-list:
27221 objc-synthesize-identifier
27222 objc-synthesize-identifier-list, objc-synthesize-identifier
27224 objc-synthesize-identifier
27225 identifier
27226 identifier = identifier
27228 For example:
27229 @synthesize MyProperty;
27230 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27232 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27233 for C. Keep them in sync.
27235 static void
27236 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27238 tree list = NULL_TREE;
27239 location_t loc;
27240 loc = cp_lexer_peek_token (parser->lexer)->location;
27242 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27243 while (true)
27245 tree property, ivar;
27246 property = cp_parser_identifier (parser);
27247 if (property == error_mark_node)
27249 cp_parser_consume_semicolon_at_end_of_statement (parser);
27250 return;
27252 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27254 cp_lexer_consume_token (parser->lexer);
27255 ivar = cp_parser_identifier (parser);
27256 if (ivar == error_mark_node)
27258 cp_parser_consume_semicolon_at_end_of_statement (parser);
27259 return;
27262 else
27263 ivar = NULL_TREE;
27264 list = chainon (list, build_tree_list (ivar, property));
27265 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27266 cp_lexer_consume_token (parser->lexer);
27267 else
27268 break;
27270 cp_parser_consume_semicolon_at_end_of_statement (parser);
27271 objc_add_synthesize_declaration (loc, list);
27274 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27276 objc-dynamic-declaration:
27277 @dynamic identifier-list ;
27279 For example:
27280 @dynamic MyProperty;
27281 @dynamic MyProperty, AnotherProperty;
27283 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27284 for C. Keep them in sync.
27286 static void
27287 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27289 tree list = NULL_TREE;
27290 location_t loc;
27291 loc = cp_lexer_peek_token (parser->lexer)->location;
27293 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27294 while (true)
27296 tree property;
27297 property = cp_parser_identifier (parser);
27298 if (property == error_mark_node)
27300 cp_parser_consume_semicolon_at_end_of_statement (parser);
27301 return;
27303 list = chainon (list, build_tree_list (NULL, property));
27304 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27305 cp_lexer_consume_token (parser->lexer);
27306 else
27307 break;
27309 cp_parser_consume_semicolon_at_end_of_statement (parser);
27310 objc_add_dynamic_declaration (loc, list);
27314 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27316 /* Returns name of the next clause.
27317 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27318 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27319 returned and the token is consumed. */
27321 static pragma_omp_clause
27322 cp_parser_omp_clause_name (cp_parser *parser)
27324 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27326 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27327 result = PRAGMA_OMP_CLAUSE_IF;
27328 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27329 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27330 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27331 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27332 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27333 result = PRAGMA_OMP_CLAUSE_FOR;
27334 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27336 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27337 const char *p = IDENTIFIER_POINTER (id);
27339 switch (p[0])
27341 case 'a':
27342 if (!strcmp ("aligned", p))
27343 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27344 break;
27345 case 'c':
27346 if (!strcmp ("collapse", p))
27347 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27348 else if (!strcmp ("copyin", p))
27349 result = PRAGMA_OMP_CLAUSE_COPYIN;
27350 else if (!strcmp ("copyprivate", p))
27351 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27352 break;
27353 case 'd':
27354 if (!strcmp ("depend", p))
27355 result = PRAGMA_OMP_CLAUSE_DEPEND;
27356 else if (!strcmp ("device", p))
27357 result = PRAGMA_OMP_CLAUSE_DEVICE;
27358 else if (!strcmp ("dist_schedule", p))
27359 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27360 break;
27361 case 'f':
27362 if (!strcmp ("final", p))
27363 result = PRAGMA_OMP_CLAUSE_FINAL;
27364 else if (!strcmp ("firstprivate", p))
27365 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27366 else if (!strcmp ("from", p))
27367 result = PRAGMA_OMP_CLAUSE_FROM;
27368 break;
27369 case 'i':
27370 if (!strcmp ("inbranch", p))
27371 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27372 break;
27373 case 'l':
27374 if (!strcmp ("lastprivate", p))
27375 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27376 else if (!strcmp ("linear", p))
27377 result = PRAGMA_OMP_CLAUSE_LINEAR;
27378 break;
27379 case 'm':
27380 if (!strcmp ("map", p))
27381 result = PRAGMA_OMP_CLAUSE_MAP;
27382 else if (!strcmp ("mergeable", p))
27383 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27384 break;
27385 case 'n':
27386 if (!strcmp ("notinbranch", p))
27387 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27388 else if (!strcmp ("nowait", p))
27389 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27390 else if (!strcmp ("num_teams", p))
27391 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27392 else if (!strcmp ("num_threads", p))
27393 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27394 break;
27395 case 'o':
27396 if (!strcmp ("ordered", p))
27397 result = PRAGMA_OMP_CLAUSE_ORDERED;
27398 break;
27399 case 'p':
27400 if (!strcmp ("parallel", p))
27401 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27402 else if (!strcmp ("proc_bind", p))
27403 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27404 break;
27405 case 'r':
27406 if (!strcmp ("reduction", p))
27407 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27408 break;
27409 case 's':
27410 if (!strcmp ("safelen", p))
27411 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27412 else if (!strcmp ("schedule", p))
27413 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27414 else if (!strcmp ("sections", p))
27415 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27416 else if (!strcmp ("shared", p))
27417 result = PRAGMA_OMP_CLAUSE_SHARED;
27418 else if (!strcmp ("simdlen", p))
27419 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27420 break;
27421 case 't':
27422 if (!strcmp ("taskgroup", p))
27423 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27424 else if (!strcmp ("thread_limit", p))
27425 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27426 else if (!strcmp ("to", p))
27427 result = PRAGMA_OMP_CLAUSE_TO;
27428 break;
27429 case 'u':
27430 if (!strcmp ("uniform", p))
27431 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27432 else if (!strcmp ("untied", p))
27433 result = PRAGMA_OMP_CLAUSE_UNTIED;
27434 break;
27438 if (result != PRAGMA_OMP_CLAUSE_NONE)
27439 cp_lexer_consume_token (parser->lexer);
27441 return result;
27444 /* Validate that a clause of the given type does not already exist. */
27446 static void
27447 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27448 const char *name, location_t location)
27450 tree c;
27452 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27453 if (OMP_CLAUSE_CODE (c) == code)
27455 error_at (location, "too many %qs clauses", name);
27456 break;
27460 /* OpenMP 2.5:
27461 variable-list:
27462 identifier
27463 variable-list , identifier
27465 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27466 colon). An opening parenthesis will have been consumed by the caller.
27468 If KIND is nonzero, create the appropriate node and install the decl
27469 in OMP_CLAUSE_DECL and add the node to the head of the list.
27471 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27472 return the list created.
27474 COLON can be NULL if only closing parenthesis should end the list,
27475 or pointer to bool which will receive false if the list is terminated
27476 by closing parenthesis or true if the list is terminated by colon. */
27478 static tree
27479 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27480 tree list, bool *colon)
27482 cp_token *token;
27483 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27484 if (colon)
27486 parser->colon_corrects_to_scope_p = false;
27487 *colon = false;
27489 while (1)
27491 tree name, decl;
27493 token = cp_lexer_peek_token (parser->lexer);
27494 name = cp_parser_id_expression (parser, /*template_p=*/false,
27495 /*check_dependency_p=*/true,
27496 /*template_p=*/NULL,
27497 /*declarator_p=*/false,
27498 /*optional_p=*/false);
27499 if (name == error_mark_node)
27500 goto skip_comma;
27502 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27503 if (decl == error_mark_node)
27504 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27505 token->location);
27506 else if (kind != 0)
27508 switch (kind)
27510 case OMP_CLAUSE_MAP:
27511 case OMP_CLAUSE_FROM:
27512 case OMP_CLAUSE_TO:
27513 case OMP_CLAUSE_DEPEND:
27514 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27516 tree low_bound = NULL_TREE, length = NULL_TREE;
27518 parser->colon_corrects_to_scope_p = false;
27519 cp_lexer_consume_token (parser->lexer);
27520 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27521 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27522 NULL);
27523 if (!colon)
27524 parser->colon_corrects_to_scope_p
27525 = saved_colon_corrects_to_scope_p;
27526 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27527 length = integer_one_node;
27528 else
27530 /* Look for `:'. */
27531 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27532 goto skip_comma;
27533 if (!cp_lexer_next_token_is (parser->lexer,
27534 CPP_CLOSE_SQUARE))
27535 length = cp_parser_expression (parser,
27536 /*cast_p=*/false,
27537 NULL);
27539 /* Look for the closing `]'. */
27540 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27541 RT_CLOSE_SQUARE))
27542 goto skip_comma;
27543 decl = tree_cons (low_bound, length, decl);
27545 break;
27546 default:
27547 break;
27550 tree u = build_omp_clause (token->location, kind);
27551 OMP_CLAUSE_DECL (u) = decl;
27552 OMP_CLAUSE_CHAIN (u) = list;
27553 list = u;
27555 else
27556 list = tree_cons (decl, NULL_TREE, list);
27558 get_comma:
27559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27560 break;
27561 cp_lexer_consume_token (parser->lexer);
27564 if (colon)
27565 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27567 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27569 *colon = true;
27570 cp_parser_require (parser, CPP_COLON, RT_COLON);
27571 return list;
27574 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27576 int ending;
27578 /* Try to resync to an unnested comma. Copied from
27579 cp_parser_parenthesized_expression_list. */
27580 skip_comma:
27581 if (colon)
27582 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27583 ending = cp_parser_skip_to_closing_parenthesis (parser,
27584 /*recovering=*/true,
27585 /*or_comma=*/true,
27586 /*consume_paren=*/true);
27587 if (ending < 0)
27588 goto get_comma;
27591 return list;
27594 /* Similarly, but expect leading and trailing parenthesis. This is a very
27595 common case for omp clauses. */
27597 static tree
27598 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27600 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27601 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27602 return list;
27605 /* OpenMP 3.0:
27606 collapse ( constant-expression ) */
27608 static tree
27609 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27611 tree c, num;
27612 location_t loc;
27613 HOST_WIDE_INT n;
27615 loc = cp_lexer_peek_token (parser->lexer)->location;
27616 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27617 return list;
27619 num = cp_parser_constant_expression (parser, false, NULL);
27621 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27622 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27623 /*or_comma=*/false,
27624 /*consume_paren=*/true);
27626 if (num == error_mark_node)
27627 return list;
27628 num = fold_non_dependent_expr (num);
27629 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27630 || !host_integerp (num, 0)
27631 || (n = tree_low_cst (num, 0)) <= 0
27632 || (int) n != n)
27634 error_at (loc, "collapse argument needs positive constant integer expression");
27635 return list;
27638 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27639 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27640 OMP_CLAUSE_CHAIN (c) = list;
27641 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27643 return c;
27646 /* OpenMP 2.5:
27647 default ( shared | none ) */
27649 static tree
27650 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27652 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27653 tree c;
27655 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27656 return list;
27657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27659 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27660 const char *p = IDENTIFIER_POINTER (id);
27662 switch (p[0])
27664 case 'n':
27665 if (strcmp ("none", p) != 0)
27666 goto invalid_kind;
27667 kind = OMP_CLAUSE_DEFAULT_NONE;
27668 break;
27670 case 's':
27671 if (strcmp ("shared", p) != 0)
27672 goto invalid_kind;
27673 kind = OMP_CLAUSE_DEFAULT_SHARED;
27674 break;
27676 default:
27677 goto invalid_kind;
27680 cp_lexer_consume_token (parser->lexer);
27682 else
27684 invalid_kind:
27685 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27688 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27689 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27690 /*or_comma=*/false,
27691 /*consume_paren=*/true);
27693 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27694 return list;
27696 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27697 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27698 OMP_CLAUSE_CHAIN (c) = list;
27699 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27701 return c;
27704 /* OpenMP 3.1:
27705 final ( expression ) */
27707 static tree
27708 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27710 tree t, c;
27712 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27713 return list;
27715 t = cp_parser_condition (parser);
27717 if (t == error_mark_node
27718 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27720 /*or_comma=*/false,
27721 /*consume_paren=*/true);
27723 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27725 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27726 OMP_CLAUSE_FINAL_EXPR (c) = t;
27727 OMP_CLAUSE_CHAIN (c) = list;
27729 return c;
27732 /* OpenMP 2.5:
27733 if ( expression ) */
27735 static tree
27736 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27738 tree t, c;
27740 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27741 return list;
27743 t = cp_parser_condition (parser);
27745 if (t == error_mark_node
27746 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27747 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27748 /*or_comma=*/false,
27749 /*consume_paren=*/true);
27751 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27753 c = build_omp_clause (location, OMP_CLAUSE_IF);
27754 OMP_CLAUSE_IF_EXPR (c) = t;
27755 OMP_CLAUSE_CHAIN (c) = list;
27757 return c;
27760 /* OpenMP 3.1:
27761 mergeable */
27763 static tree
27764 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27765 tree list, location_t location)
27767 tree c;
27769 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27770 location);
27772 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27773 OMP_CLAUSE_CHAIN (c) = list;
27774 return c;
27777 /* OpenMP 2.5:
27778 nowait */
27780 static tree
27781 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27782 tree list, location_t location)
27784 tree c;
27786 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27788 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27789 OMP_CLAUSE_CHAIN (c) = list;
27790 return c;
27793 /* OpenMP 2.5:
27794 num_threads ( expression ) */
27796 static tree
27797 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27798 location_t location)
27800 tree t, c;
27802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27803 return list;
27805 t = cp_parser_expression (parser, false, NULL);
27807 if (t == error_mark_node
27808 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27809 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27810 /*or_comma=*/false,
27811 /*consume_paren=*/true);
27813 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27814 "num_threads", location);
27816 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27817 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27818 OMP_CLAUSE_CHAIN (c) = list;
27820 return c;
27823 /* OpenMP 2.5:
27824 ordered */
27826 static tree
27827 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27828 tree list, location_t location)
27830 tree c;
27832 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27833 "ordered", location);
27835 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27836 OMP_CLAUSE_CHAIN (c) = list;
27837 return c;
27840 /* OpenMP 2.5:
27841 reduction ( reduction-operator : variable-list )
27843 reduction-operator:
27844 One of: + * - & ^ | && ||
27846 OpenMP 3.1:
27848 reduction-operator:
27849 One of: + * - & ^ | && || min max
27851 OpenMP 4.0:
27853 reduction-operator:
27854 One of: + * - & ^ | && ||
27855 id-expression */
27857 static tree
27858 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27860 enum tree_code code = ERROR_MARK;
27861 tree nlist, c, id = NULL_TREE;
27863 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27864 return list;
27866 switch (cp_lexer_peek_token (parser->lexer)->type)
27868 case CPP_PLUS: code = PLUS_EXPR; break;
27869 case CPP_MULT: code = MULT_EXPR; break;
27870 case CPP_MINUS: code = MINUS_EXPR; break;
27871 case CPP_AND: code = BIT_AND_EXPR; break;
27872 case CPP_XOR: code = BIT_XOR_EXPR; break;
27873 case CPP_OR: code = BIT_IOR_EXPR; break;
27874 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27875 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27876 default: break;
27879 if (code != ERROR_MARK)
27880 cp_lexer_consume_token (parser->lexer);
27881 else
27883 bool saved_colon_corrects_to_scope_p;
27884 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27885 parser->colon_corrects_to_scope_p = false;
27886 id = cp_parser_id_expression (parser, /*template_p=*/false,
27887 /*check_dependency_p=*/true,
27888 /*template_p=*/NULL,
27889 /*declarator_p=*/false,
27890 /*optional_p=*/false);
27891 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27892 if (identifier_p (id))
27894 const char *p = IDENTIFIER_POINTER (id);
27896 if (strcmp (p, "min") == 0)
27897 code = MIN_EXPR;
27898 else if (strcmp (p, "max") == 0)
27899 code = MAX_EXPR;
27900 else if (id == ansi_opname (PLUS_EXPR))
27901 code = PLUS_EXPR;
27902 else if (id == ansi_opname (MULT_EXPR))
27903 code = MULT_EXPR;
27904 else if (id == ansi_opname (MINUS_EXPR))
27905 code = MINUS_EXPR;
27906 else if (id == ansi_opname (BIT_AND_EXPR))
27907 code = BIT_AND_EXPR;
27908 else if (id == ansi_opname (BIT_IOR_EXPR))
27909 code = BIT_IOR_EXPR;
27910 else if (id == ansi_opname (BIT_XOR_EXPR))
27911 code = BIT_XOR_EXPR;
27912 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27913 code = TRUTH_ANDIF_EXPR;
27914 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27915 code = TRUTH_ORIF_EXPR;
27916 id = omp_reduction_id (code, id, NULL_TREE);
27917 tree scope = parser->scope;
27918 if (scope)
27919 id = build_qualified_name (NULL_TREE, scope, id, false);
27920 parser->scope = NULL_TREE;
27921 parser->qualifying_scope = NULL_TREE;
27922 parser->object_scope = NULL_TREE;
27924 else
27926 error ("invalid reduction-identifier");
27927 resync_fail:
27928 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27929 /*or_comma=*/false,
27930 /*consume_paren=*/true);
27931 return list;
27935 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27936 goto resync_fail;
27938 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27939 NULL);
27940 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27942 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27943 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27946 return nlist;
27949 /* OpenMP 2.5:
27950 schedule ( schedule-kind )
27951 schedule ( schedule-kind , expression )
27953 schedule-kind:
27954 static | dynamic | guided | runtime | auto */
27956 static tree
27957 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27959 tree c, t;
27961 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27962 return list;
27964 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27966 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27968 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27969 const char *p = IDENTIFIER_POINTER (id);
27971 switch (p[0])
27973 case 'd':
27974 if (strcmp ("dynamic", p) != 0)
27975 goto invalid_kind;
27976 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27977 break;
27979 case 'g':
27980 if (strcmp ("guided", p) != 0)
27981 goto invalid_kind;
27982 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27983 break;
27985 case 'r':
27986 if (strcmp ("runtime", p) != 0)
27987 goto invalid_kind;
27988 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27989 break;
27991 default:
27992 goto invalid_kind;
27995 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27996 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27997 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27998 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27999 else
28000 goto invalid_kind;
28001 cp_lexer_consume_token (parser->lexer);
28003 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28005 cp_token *token;
28006 cp_lexer_consume_token (parser->lexer);
28008 token = cp_lexer_peek_token (parser->lexer);
28009 t = cp_parser_assignment_expression (parser, false, NULL);
28011 if (t == error_mark_node)
28012 goto resync_fail;
28013 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28014 error_at (token->location, "schedule %<runtime%> does not take "
28015 "a %<chunk_size%> parameter");
28016 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28017 error_at (token->location, "schedule %<auto%> does not take "
28018 "a %<chunk_size%> parameter");
28019 else
28020 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28022 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28023 goto resync_fail;
28025 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28026 goto resync_fail;
28028 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28029 OMP_CLAUSE_CHAIN (c) = list;
28030 return c;
28032 invalid_kind:
28033 cp_parser_error (parser, "invalid schedule kind");
28034 resync_fail:
28035 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28036 /*or_comma=*/false,
28037 /*consume_paren=*/true);
28038 return list;
28041 /* OpenMP 3.0:
28042 untied */
28044 static tree
28045 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28046 tree list, location_t location)
28048 tree c;
28050 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28052 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28053 OMP_CLAUSE_CHAIN (c) = list;
28054 return c;
28057 /* OpenMP 4.0:
28058 inbranch
28059 notinbranch */
28061 static tree
28062 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28063 tree list, location_t location)
28065 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28066 tree c = build_omp_clause (location, code);
28067 OMP_CLAUSE_CHAIN (c) = list;
28068 return c;
28071 /* OpenMP 4.0:
28072 parallel
28074 sections
28075 taskgroup */
28077 static tree
28078 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28079 enum omp_clause_code code,
28080 tree list, location_t location)
28082 tree c = build_omp_clause (location, code);
28083 OMP_CLAUSE_CHAIN (c) = list;
28084 return c;
28087 /* OpenMP 4.0:
28088 num_teams ( expression ) */
28090 static tree
28091 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28092 location_t location)
28094 tree t, c;
28096 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28097 return list;
28099 t = cp_parser_expression (parser, false, NULL);
28101 if (t == error_mark_node
28102 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28103 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28104 /*or_comma=*/false,
28105 /*consume_paren=*/true);
28107 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28108 "num_teams", location);
28110 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28111 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28112 OMP_CLAUSE_CHAIN (c) = list;
28114 return c;
28117 /* OpenMP 4.0:
28118 thread_limit ( expression ) */
28120 static tree
28121 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28122 location_t location)
28124 tree t, c;
28126 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28127 return list;
28129 t = cp_parser_expression (parser, false, NULL);
28131 if (t == error_mark_node
28132 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28133 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28134 /*or_comma=*/false,
28135 /*consume_paren=*/true);
28137 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28138 "thread_limit", location);
28140 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28141 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28142 OMP_CLAUSE_CHAIN (c) = list;
28144 return c;
28147 /* OpenMP 4.0:
28148 aligned ( variable-list )
28149 aligned ( variable-list : constant-expression ) */
28151 static tree
28152 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28154 tree nlist, c, alignment = NULL_TREE;
28155 bool colon;
28157 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28158 return list;
28160 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28161 &colon);
28163 if (colon)
28165 alignment = cp_parser_constant_expression (parser, false, NULL);
28167 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28168 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28169 /*or_comma=*/false,
28170 /*consume_paren=*/true);
28172 if (alignment == error_mark_node)
28173 alignment = NULL_TREE;
28176 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28177 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28179 return nlist;
28182 /* OpenMP 4.0:
28183 linear ( variable-list )
28184 linear ( variable-list : expression ) */
28186 static tree
28187 cp_parser_omp_clause_linear (cp_parser *parser, tree list)
28189 tree nlist, c, step = integer_one_node;
28190 bool colon;
28192 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28193 return list;
28195 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28196 &colon);
28198 if (colon)
28200 step = cp_parser_expression (parser, false, NULL);
28202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28203 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28204 /*or_comma=*/false,
28205 /*consume_paren=*/true);
28207 if (step == error_mark_node)
28208 return list;
28211 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28212 OMP_CLAUSE_LINEAR_STEP (c) = step;
28214 return nlist;
28217 /* OpenMP 4.0:
28218 safelen ( constant-expression ) */
28220 static tree
28221 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28222 location_t location)
28224 tree t, c;
28226 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28227 return list;
28229 t = cp_parser_constant_expression (parser, false, NULL);
28231 if (t == error_mark_node
28232 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28233 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28234 /*or_comma=*/false,
28235 /*consume_paren=*/true);
28237 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28239 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28240 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28241 OMP_CLAUSE_CHAIN (c) = list;
28243 return c;
28246 /* OpenMP 4.0:
28247 simdlen ( constant-expression ) */
28249 static tree
28250 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28251 location_t location)
28253 tree t, c;
28255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28256 return list;
28258 t = cp_parser_constant_expression (parser, false, NULL);
28260 if (t == error_mark_node
28261 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28262 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28263 /*or_comma=*/false,
28264 /*consume_paren=*/true);
28266 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28268 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28269 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28270 OMP_CLAUSE_CHAIN (c) = list;
28272 return c;
28275 /* OpenMP 4.0:
28276 depend ( depend-kind : variable-list )
28278 depend-kind:
28279 in | out | inout */
28281 static tree
28282 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28284 tree nlist, c;
28285 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28287 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28288 return list;
28290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28293 const char *p = IDENTIFIER_POINTER (id);
28295 if (strcmp ("in", p) == 0)
28296 kind = OMP_CLAUSE_DEPEND_IN;
28297 else if (strcmp ("inout", p) == 0)
28298 kind = OMP_CLAUSE_DEPEND_INOUT;
28299 else if (strcmp ("out", p) == 0)
28300 kind = OMP_CLAUSE_DEPEND_OUT;
28301 else
28302 goto invalid_kind;
28304 else
28305 goto invalid_kind;
28307 cp_lexer_consume_token (parser->lexer);
28308 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28309 goto resync_fail;
28311 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28312 NULL);
28314 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28315 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28317 return nlist;
28319 invalid_kind:
28320 cp_parser_error (parser, "invalid depend kind");
28321 resync_fail:
28322 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28323 /*or_comma=*/false,
28324 /*consume_paren=*/true);
28325 return list;
28328 /* OpenMP 4.0:
28329 map ( map-kind : variable-list )
28330 map ( variable-list )
28332 map-kind:
28333 alloc | to | from | tofrom */
28335 static tree
28336 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28338 tree nlist, c;
28339 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28341 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28342 return list;
28344 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28345 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28347 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28348 const char *p = IDENTIFIER_POINTER (id);
28350 if (strcmp ("alloc", p) == 0)
28351 kind = OMP_CLAUSE_MAP_ALLOC;
28352 else if (strcmp ("to", p) == 0)
28353 kind = OMP_CLAUSE_MAP_TO;
28354 else if (strcmp ("from", p) == 0)
28355 kind = OMP_CLAUSE_MAP_FROM;
28356 else if (strcmp ("tofrom", p) == 0)
28357 kind = OMP_CLAUSE_MAP_TOFROM;
28358 else
28360 cp_parser_error (parser, "invalid map kind");
28361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28362 /*or_comma=*/false,
28363 /*consume_paren=*/true);
28364 return list;
28366 cp_lexer_consume_token (parser->lexer);
28367 cp_lexer_consume_token (parser->lexer);
28370 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28371 NULL);
28373 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28374 OMP_CLAUSE_MAP_KIND (c) = kind;
28376 return nlist;
28379 /* OpenMP 4.0:
28380 device ( expression ) */
28382 static tree
28383 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28384 location_t location)
28386 tree t, c;
28388 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28389 return list;
28391 t = cp_parser_expression (parser, false, NULL);
28393 if (t == error_mark_node
28394 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28396 /*or_comma=*/false,
28397 /*consume_paren=*/true);
28399 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28400 "device", location);
28402 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28403 OMP_CLAUSE_DEVICE_ID (c) = t;
28404 OMP_CLAUSE_CHAIN (c) = list;
28406 return c;
28409 /* OpenMP 4.0:
28410 dist_schedule ( static )
28411 dist_schedule ( static , expression ) */
28413 static tree
28414 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28415 location_t location)
28417 tree c, t;
28419 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28420 return list;
28422 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28424 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28425 goto invalid_kind;
28426 cp_lexer_consume_token (parser->lexer);
28428 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28430 cp_lexer_consume_token (parser->lexer);
28432 t = cp_parser_assignment_expression (parser, false, NULL);
28434 if (t == error_mark_node)
28435 goto resync_fail;
28436 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28439 goto resync_fail;
28441 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28442 goto resync_fail;
28444 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28445 location);
28446 OMP_CLAUSE_CHAIN (c) = list;
28447 return c;
28449 invalid_kind:
28450 cp_parser_error (parser, "invalid dist_schedule kind");
28451 resync_fail:
28452 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28453 /*or_comma=*/false,
28454 /*consume_paren=*/true);
28455 return list;
28458 /* OpenMP 4.0:
28459 proc_bind ( proc-bind-kind )
28461 proc-bind-kind:
28462 master | close | spread */
28464 static tree
28465 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28466 location_t location)
28468 tree c;
28469 enum omp_clause_proc_bind_kind kind;
28471 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28472 return list;
28474 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28476 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28477 const char *p = IDENTIFIER_POINTER (id);
28479 if (strcmp ("master", p) == 0)
28480 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28481 else if (strcmp ("close", p) == 0)
28482 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28483 else if (strcmp ("spread", p) == 0)
28484 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28485 else
28486 goto invalid_kind;
28488 else
28489 goto invalid_kind;
28491 cp_lexer_consume_token (parser->lexer);
28492 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28493 goto resync_fail;
28495 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28496 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28497 location);
28498 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28499 OMP_CLAUSE_CHAIN (c) = list;
28500 return c;
28502 invalid_kind:
28503 cp_parser_error (parser, "invalid depend kind");
28504 resync_fail:
28505 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28506 /*or_comma=*/false,
28507 /*consume_paren=*/true);
28508 return list;
28511 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28512 is a bitmask in MASK. Return the list of clauses found; the result
28513 of clause default goes in *pdefault. */
28515 static tree
28516 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28517 const char *where, cp_token *pragma_tok,
28518 bool finish_p = true)
28520 tree clauses = NULL;
28521 bool first = true;
28522 cp_token *token = NULL;
28524 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28526 pragma_omp_clause c_kind;
28527 const char *c_name;
28528 tree prev = clauses;
28530 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28531 cp_lexer_consume_token (parser->lexer);
28533 token = cp_lexer_peek_token (parser->lexer);
28534 c_kind = cp_parser_omp_clause_name (parser);
28536 switch (c_kind)
28538 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28539 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28540 token->location);
28541 c_name = "collapse";
28542 break;
28543 case PRAGMA_OMP_CLAUSE_COPYIN:
28544 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28545 c_name = "copyin";
28546 break;
28547 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28548 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28549 clauses);
28550 c_name = "copyprivate";
28551 break;
28552 case PRAGMA_OMP_CLAUSE_DEFAULT:
28553 clauses = cp_parser_omp_clause_default (parser, clauses,
28554 token->location);
28555 c_name = "default";
28556 break;
28557 case PRAGMA_OMP_CLAUSE_FINAL:
28558 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28559 c_name = "final";
28560 break;
28561 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28562 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28563 clauses);
28564 c_name = "firstprivate";
28565 break;
28566 case PRAGMA_OMP_CLAUSE_IF:
28567 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28568 c_name = "if";
28569 break;
28570 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28571 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28572 clauses);
28573 c_name = "lastprivate";
28574 break;
28575 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28576 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28577 token->location);
28578 c_name = "mergeable";
28579 break;
28580 case PRAGMA_OMP_CLAUSE_NOWAIT:
28581 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28582 c_name = "nowait";
28583 break;
28584 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28585 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28586 token->location);
28587 c_name = "num_threads";
28588 break;
28589 case PRAGMA_OMP_CLAUSE_ORDERED:
28590 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28591 token->location);
28592 c_name = "ordered";
28593 break;
28594 case PRAGMA_OMP_CLAUSE_PRIVATE:
28595 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28596 clauses);
28597 c_name = "private";
28598 break;
28599 case PRAGMA_OMP_CLAUSE_REDUCTION:
28600 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28601 c_name = "reduction";
28602 break;
28603 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28604 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28605 token->location);
28606 c_name = "schedule";
28607 break;
28608 case PRAGMA_OMP_CLAUSE_SHARED:
28609 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28610 clauses);
28611 c_name = "shared";
28612 break;
28613 case PRAGMA_OMP_CLAUSE_UNTIED:
28614 clauses = cp_parser_omp_clause_untied (parser, clauses,
28615 token->location);
28616 c_name = "untied";
28617 break;
28618 case PRAGMA_OMP_CLAUSE_INBRANCH:
28619 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28620 clauses, token->location);
28621 c_name = "inbranch";
28622 break;
28623 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28624 clauses = cp_parser_omp_clause_branch (parser,
28625 OMP_CLAUSE_NOTINBRANCH,
28626 clauses, token->location);
28627 c_name = "notinbranch";
28628 break;
28629 case PRAGMA_OMP_CLAUSE_PARALLEL:
28630 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28631 clauses, token->location);
28632 c_name = "parallel";
28633 if (!first)
28635 clause_not_first:
28636 error_at (token->location, "%qs must be the first clause of %qs",
28637 c_name, where);
28638 clauses = prev;
28640 break;
28641 case PRAGMA_OMP_CLAUSE_FOR:
28642 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28643 clauses, token->location);
28644 c_name = "for";
28645 if (!first)
28646 goto clause_not_first;
28647 break;
28648 case PRAGMA_OMP_CLAUSE_SECTIONS:
28649 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28650 clauses, token->location);
28651 c_name = "sections";
28652 if (!first)
28653 goto clause_not_first;
28654 break;
28655 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28656 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28657 clauses, token->location);
28658 c_name = "taskgroup";
28659 if (!first)
28660 goto clause_not_first;
28661 break;
28662 case PRAGMA_OMP_CLAUSE_TO:
28663 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28664 clauses);
28665 c_name = "to";
28666 break;
28667 case PRAGMA_OMP_CLAUSE_FROM:
28668 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28669 clauses);
28670 c_name = "from";
28671 break;
28672 case PRAGMA_OMP_CLAUSE_UNIFORM:
28673 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28674 clauses);
28675 c_name = "uniform";
28676 break;
28677 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28678 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28679 token->location);
28680 c_name = "num_teams";
28681 break;
28682 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28683 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28684 token->location);
28685 c_name = "thread_limit";
28686 break;
28687 case PRAGMA_OMP_CLAUSE_ALIGNED:
28688 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28689 c_name = "aligned";
28690 break;
28691 case PRAGMA_OMP_CLAUSE_LINEAR:
28692 clauses = cp_parser_omp_clause_linear (parser, clauses);
28693 c_name = "linear";
28694 break;
28695 case PRAGMA_OMP_CLAUSE_DEPEND:
28696 clauses = cp_parser_omp_clause_depend (parser, clauses);
28697 c_name = "depend";
28698 break;
28699 case PRAGMA_OMP_CLAUSE_MAP:
28700 clauses = cp_parser_omp_clause_map (parser, clauses);
28701 c_name = "map";
28702 break;
28703 case PRAGMA_OMP_CLAUSE_DEVICE:
28704 clauses = cp_parser_omp_clause_device (parser, clauses,
28705 token->location);
28706 c_name = "device";
28707 break;
28708 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28709 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28710 token->location);
28711 c_name = "dist_schedule";
28712 break;
28713 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28714 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28715 token->location);
28716 c_name = "proc_bind";
28717 break;
28718 case PRAGMA_OMP_CLAUSE_SAFELEN:
28719 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28720 token->location);
28721 c_name = "safelen";
28722 break;
28723 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28724 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28725 token->location);
28726 c_name = "simdlen";
28727 break;
28728 default:
28729 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28730 goto saw_error;
28733 first = false;
28735 if (((mask >> c_kind) & 1) == 0)
28737 /* Remove the invalid clause(s) from the list to avoid
28738 confusing the rest of the compiler. */
28739 clauses = prev;
28740 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28743 saw_error:
28744 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28745 if (finish_p)
28746 return finish_omp_clauses (clauses);
28747 return clauses;
28750 /* OpenMP 2.5:
28751 structured-block:
28752 statement
28754 In practice, we're also interested in adding the statement to an
28755 outer node. So it is convenient if we work around the fact that
28756 cp_parser_statement calls add_stmt. */
28758 static unsigned
28759 cp_parser_begin_omp_structured_block (cp_parser *parser)
28761 unsigned save = parser->in_statement;
28763 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28764 This preserves the "not within loop or switch" style error messages
28765 for nonsense cases like
28766 void foo() {
28767 #pragma omp single
28768 break;
28771 if (parser->in_statement)
28772 parser->in_statement = IN_OMP_BLOCK;
28774 return save;
28777 static void
28778 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28780 parser->in_statement = save;
28783 static tree
28784 cp_parser_omp_structured_block (cp_parser *parser)
28786 tree stmt = begin_omp_structured_block ();
28787 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28789 cp_parser_statement (parser, NULL_TREE, false, NULL);
28791 cp_parser_end_omp_structured_block (parser, save);
28792 return finish_omp_structured_block (stmt);
28795 /* OpenMP 2.5:
28796 # pragma omp atomic new-line
28797 expression-stmt
28799 expression-stmt:
28800 x binop= expr | x++ | ++x | x-- | --x
28801 binop:
28802 +, *, -, /, &, ^, |, <<, >>
28804 where x is an lvalue expression with scalar type.
28806 OpenMP 3.1:
28807 # pragma omp atomic new-line
28808 update-stmt
28810 # pragma omp atomic read new-line
28811 read-stmt
28813 # pragma omp atomic write new-line
28814 write-stmt
28816 # pragma omp atomic update new-line
28817 update-stmt
28819 # pragma omp atomic capture new-line
28820 capture-stmt
28822 # pragma omp atomic capture new-line
28823 capture-block
28825 read-stmt:
28826 v = x
28827 write-stmt:
28828 x = expr
28829 update-stmt:
28830 expression-stmt | x = x binop expr
28831 capture-stmt:
28832 v = expression-stmt
28833 capture-block:
28834 { v = x; update-stmt; } | { update-stmt; v = x; }
28836 OpenMP 4.0:
28837 update-stmt:
28838 expression-stmt | x = x binop expr | x = expr binop x
28839 capture-stmt:
28840 v = update-stmt
28841 capture-block:
28842 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28844 where x and v are lvalue expressions with scalar type. */
28846 static void
28847 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28849 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28850 tree rhs1 = NULL_TREE, orig_lhs;
28851 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28852 bool structured_block = false;
28853 bool seq_cst = false;
28855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28857 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28858 const char *p = IDENTIFIER_POINTER (id);
28860 if (!strcmp (p, "read"))
28861 code = OMP_ATOMIC_READ;
28862 else if (!strcmp (p, "write"))
28863 code = NOP_EXPR;
28864 else if (!strcmp (p, "update"))
28865 code = OMP_ATOMIC;
28866 else if (!strcmp (p, "capture"))
28867 code = OMP_ATOMIC_CAPTURE_NEW;
28868 else
28869 p = NULL;
28870 if (p)
28871 cp_lexer_consume_token (parser->lexer);
28874 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28876 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28877 const char *p = IDENTIFIER_POINTER (id);
28879 if (!strcmp (p, "seq_cst"))
28881 seq_cst = true;
28882 cp_lexer_consume_token (parser->lexer);
28885 cp_parser_require_pragma_eol (parser, pragma_tok);
28887 switch (code)
28889 case OMP_ATOMIC_READ:
28890 case NOP_EXPR: /* atomic write */
28891 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28892 /*cast_p=*/false, NULL);
28893 if (v == error_mark_node)
28894 goto saw_error;
28895 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28896 goto saw_error;
28897 if (code == NOP_EXPR)
28898 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28899 else
28900 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28901 /*cast_p=*/false, NULL);
28902 if (lhs == error_mark_node)
28903 goto saw_error;
28904 if (code == NOP_EXPR)
28906 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28907 opcode. */
28908 code = OMP_ATOMIC;
28909 rhs = lhs;
28910 lhs = v;
28911 v = NULL_TREE;
28913 goto done;
28914 case OMP_ATOMIC_CAPTURE_NEW:
28915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28917 cp_lexer_consume_token (parser->lexer);
28918 structured_block = true;
28920 else
28922 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28923 /*cast_p=*/false, NULL);
28924 if (v == error_mark_node)
28925 goto saw_error;
28926 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28927 goto saw_error;
28929 default:
28930 break;
28933 restart:
28934 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28935 /*cast_p=*/false, NULL);
28936 orig_lhs = lhs;
28937 switch (TREE_CODE (lhs))
28939 case ERROR_MARK:
28940 goto saw_error;
28942 case POSTINCREMENT_EXPR:
28943 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28944 code = OMP_ATOMIC_CAPTURE_OLD;
28945 /* FALLTHROUGH */
28946 case PREINCREMENT_EXPR:
28947 lhs = TREE_OPERAND (lhs, 0);
28948 opcode = PLUS_EXPR;
28949 rhs = integer_one_node;
28950 break;
28952 case POSTDECREMENT_EXPR:
28953 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28954 code = OMP_ATOMIC_CAPTURE_OLD;
28955 /* FALLTHROUGH */
28956 case PREDECREMENT_EXPR:
28957 lhs = TREE_OPERAND (lhs, 0);
28958 opcode = MINUS_EXPR;
28959 rhs = integer_one_node;
28960 break;
28962 case COMPOUND_EXPR:
28963 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28964 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28965 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28966 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28967 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28968 (TREE_OPERAND (lhs, 1), 0), 0)))
28969 == BOOLEAN_TYPE)
28970 /* Undo effects of boolean_increment for post {in,de}crement. */
28971 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28972 /* FALLTHRU */
28973 case MODIFY_EXPR:
28974 if (TREE_CODE (lhs) == MODIFY_EXPR
28975 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28977 /* Undo effects of boolean_increment. */
28978 if (integer_onep (TREE_OPERAND (lhs, 1)))
28980 /* This is pre or post increment. */
28981 rhs = TREE_OPERAND (lhs, 1);
28982 lhs = TREE_OPERAND (lhs, 0);
28983 opcode = NOP_EXPR;
28984 if (code == OMP_ATOMIC_CAPTURE_NEW
28985 && !structured_block
28986 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28987 code = OMP_ATOMIC_CAPTURE_OLD;
28988 break;
28991 /* FALLTHRU */
28992 default:
28993 switch (cp_lexer_peek_token (parser->lexer)->type)
28995 case CPP_MULT_EQ:
28996 opcode = MULT_EXPR;
28997 break;
28998 case CPP_DIV_EQ:
28999 opcode = TRUNC_DIV_EXPR;
29000 break;
29001 case CPP_PLUS_EQ:
29002 opcode = PLUS_EXPR;
29003 break;
29004 case CPP_MINUS_EQ:
29005 opcode = MINUS_EXPR;
29006 break;
29007 case CPP_LSHIFT_EQ:
29008 opcode = LSHIFT_EXPR;
29009 break;
29010 case CPP_RSHIFT_EQ:
29011 opcode = RSHIFT_EXPR;
29012 break;
29013 case CPP_AND_EQ:
29014 opcode = BIT_AND_EXPR;
29015 break;
29016 case CPP_OR_EQ:
29017 opcode = BIT_IOR_EXPR;
29018 break;
29019 case CPP_XOR_EQ:
29020 opcode = BIT_XOR_EXPR;
29021 break;
29022 case CPP_EQ:
29023 enum cp_parser_prec oprec;
29024 cp_token *token;
29025 cp_lexer_consume_token (parser->lexer);
29026 cp_parser_parse_tentatively (parser);
29027 rhs1 = cp_parser_simple_cast_expression (parser);
29028 if (rhs1 == error_mark_node)
29030 cp_parser_abort_tentative_parse (parser);
29031 cp_parser_simple_cast_expression (parser);
29032 goto saw_error;
29034 token = cp_lexer_peek_token (parser->lexer);
29035 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29037 cp_parser_abort_tentative_parse (parser);
29038 cp_parser_parse_tentatively (parser);
29039 rhs = cp_parser_binary_expression (parser, false, true,
29040 PREC_NOT_OPERATOR, NULL);
29041 if (rhs == error_mark_node)
29043 cp_parser_abort_tentative_parse (parser);
29044 cp_parser_binary_expression (parser, false, true,
29045 PREC_NOT_OPERATOR, NULL);
29046 goto saw_error;
29048 switch (TREE_CODE (rhs))
29050 case MULT_EXPR:
29051 case TRUNC_DIV_EXPR:
29052 case PLUS_EXPR:
29053 case MINUS_EXPR:
29054 case LSHIFT_EXPR:
29055 case RSHIFT_EXPR:
29056 case BIT_AND_EXPR:
29057 case BIT_IOR_EXPR:
29058 case BIT_XOR_EXPR:
29059 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29061 if (cp_parser_parse_definitely (parser))
29063 opcode = TREE_CODE (rhs);
29064 rhs1 = TREE_OPERAND (rhs, 0);
29065 rhs = TREE_OPERAND (rhs, 1);
29066 goto stmt_done;
29068 else
29069 goto saw_error;
29071 break;
29072 default:
29073 break;
29075 cp_parser_abort_tentative_parse (parser);
29076 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29078 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
29079 if (rhs == error_mark_node)
29080 goto saw_error;
29081 opcode = NOP_EXPR;
29082 rhs1 = NULL_TREE;
29083 goto stmt_done;
29085 cp_parser_error (parser,
29086 "invalid form of %<#pragma omp atomic%>");
29087 goto saw_error;
29089 if (!cp_parser_parse_definitely (parser))
29090 goto saw_error;
29091 switch (token->type)
29093 case CPP_SEMICOLON:
29094 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29096 code = OMP_ATOMIC_CAPTURE_OLD;
29097 v = lhs;
29098 lhs = NULL_TREE;
29099 lhs1 = rhs1;
29100 rhs1 = NULL_TREE;
29101 cp_lexer_consume_token (parser->lexer);
29102 goto restart;
29104 else if (structured_block)
29106 opcode = NOP_EXPR;
29107 rhs = rhs1;
29108 rhs1 = NULL_TREE;
29109 goto stmt_done;
29111 cp_parser_error (parser,
29112 "invalid form of %<#pragma omp atomic%>");
29113 goto saw_error;
29114 case CPP_MULT:
29115 opcode = MULT_EXPR;
29116 break;
29117 case CPP_DIV:
29118 opcode = TRUNC_DIV_EXPR;
29119 break;
29120 case CPP_PLUS:
29121 opcode = PLUS_EXPR;
29122 break;
29123 case CPP_MINUS:
29124 opcode = MINUS_EXPR;
29125 break;
29126 case CPP_LSHIFT:
29127 opcode = LSHIFT_EXPR;
29128 break;
29129 case CPP_RSHIFT:
29130 opcode = RSHIFT_EXPR;
29131 break;
29132 case CPP_AND:
29133 opcode = BIT_AND_EXPR;
29134 break;
29135 case CPP_OR:
29136 opcode = BIT_IOR_EXPR;
29137 break;
29138 case CPP_XOR:
29139 opcode = BIT_XOR_EXPR;
29140 break;
29141 default:
29142 cp_parser_error (parser,
29143 "invalid operator for %<#pragma omp atomic%>");
29144 goto saw_error;
29146 oprec = TOKEN_PRECEDENCE (token);
29147 gcc_assert (oprec != PREC_NOT_OPERATOR);
29148 if (commutative_tree_code (opcode))
29149 oprec = (enum cp_parser_prec) (oprec - 1);
29150 cp_lexer_consume_token (parser->lexer);
29151 rhs = cp_parser_binary_expression (parser, false, false,
29152 oprec, NULL);
29153 if (rhs == error_mark_node)
29154 goto saw_error;
29155 goto stmt_done;
29156 /* FALLTHROUGH */
29157 default:
29158 cp_parser_error (parser,
29159 "invalid operator for %<#pragma omp atomic%>");
29160 goto saw_error;
29162 cp_lexer_consume_token (parser->lexer);
29164 rhs = cp_parser_expression (parser, false, NULL);
29165 if (rhs == error_mark_node)
29166 goto saw_error;
29167 break;
29169 stmt_done:
29170 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29172 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29173 goto saw_error;
29174 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29175 /*cast_p=*/false, NULL);
29176 if (v == error_mark_node)
29177 goto saw_error;
29178 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29179 goto saw_error;
29180 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29181 /*cast_p=*/false, NULL);
29182 if (lhs1 == error_mark_node)
29183 goto saw_error;
29185 if (structured_block)
29187 cp_parser_consume_semicolon_at_end_of_statement (parser);
29188 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29190 done:
29191 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29192 if (!structured_block)
29193 cp_parser_consume_semicolon_at_end_of_statement (parser);
29194 return;
29196 saw_error:
29197 cp_parser_skip_to_end_of_block_or_statement (parser);
29198 if (structured_block)
29200 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29201 cp_lexer_consume_token (parser->lexer);
29202 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29204 cp_parser_skip_to_end_of_block_or_statement (parser);
29205 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29206 cp_lexer_consume_token (parser->lexer);
29212 /* OpenMP 2.5:
29213 # pragma omp barrier new-line */
29215 static void
29216 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29218 cp_parser_require_pragma_eol (parser, pragma_tok);
29219 finish_omp_barrier ();
29222 /* OpenMP 2.5:
29223 # pragma omp critical [(name)] new-line
29224 structured-block */
29226 static tree
29227 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29229 tree stmt, name = NULL;
29231 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29233 cp_lexer_consume_token (parser->lexer);
29235 name = cp_parser_identifier (parser);
29237 if (name == error_mark_node
29238 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29239 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29240 /*or_comma=*/false,
29241 /*consume_paren=*/true);
29242 if (name == error_mark_node)
29243 name = NULL;
29245 cp_parser_require_pragma_eol (parser, pragma_tok);
29247 stmt = cp_parser_omp_structured_block (parser);
29248 return c_finish_omp_critical (input_location, stmt, name);
29251 /* OpenMP 2.5:
29252 # pragma omp flush flush-vars[opt] new-line
29254 flush-vars:
29255 ( variable-list ) */
29257 static void
29258 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29260 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29261 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29262 cp_parser_require_pragma_eol (parser, pragma_tok);
29264 finish_omp_flush ();
29267 /* Helper function, to parse omp for increment expression. */
29269 static tree
29270 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29272 tree cond = cp_parser_binary_expression (parser, false, true,
29273 PREC_NOT_OPERATOR, NULL);
29274 if (cond == error_mark_node
29275 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29277 cp_parser_skip_to_end_of_statement (parser);
29278 return error_mark_node;
29281 switch (TREE_CODE (cond))
29283 case GT_EXPR:
29284 case GE_EXPR:
29285 case LT_EXPR:
29286 case LE_EXPR:
29287 break;
29288 case NE_EXPR:
29289 if (code == CILK_SIMD)
29290 break;
29291 /* Fall through: OpenMP disallows NE_EXPR. */
29292 default:
29293 return error_mark_node;
29296 /* If decl is an iterator, preserve LHS and RHS of the relational
29297 expr until finish_omp_for. */
29298 if (decl
29299 && (type_dependent_expression_p (decl)
29300 || CLASS_TYPE_P (TREE_TYPE (decl))))
29301 return cond;
29303 return build_x_binary_op (input_location, TREE_CODE (cond),
29304 TREE_OPERAND (cond, 0), ERROR_MARK,
29305 TREE_OPERAND (cond, 1), ERROR_MARK,
29306 /*overload=*/NULL, tf_warning_or_error);
29309 /* Helper function, to parse omp for increment expression. */
29311 static tree
29312 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29314 cp_token *token = cp_lexer_peek_token (parser->lexer);
29315 enum tree_code op;
29316 tree lhs, rhs;
29317 cp_id_kind idk;
29318 bool decl_first;
29320 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29322 op = (token->type == CPP_PLUS_PLUS
29323 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29324 cp_lexer_consume_token (parser->lexer);
29325 lhs = cp_parser_simple_cast_expression (parser);
29326 if (lhs != decl)
29327 return error_mark_node;
29328 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29331 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29332 if (lhs != decl)
29333 return error_mark_node;
29335 token = cp_lexer_peek_token (parser->lexer);
29336 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29338 op = (token->type == CPP_PLUS_PLUS
29339 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29340 cp_lexer_consume_token (parser->lexer);
29341 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29344 op = cp_parser_assignment_operator_opt (parser);
29345 if (op == ERROR_MARK)
29346 return error_mark_node;
29348 if (op != NOP_EXPR)
29350 rhs = cp_parser_assignment_expression (parser, false, NULL);
29351 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29352 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29355 lhs = cp_parser_binary_expression (parser, false, false,
29356 PREC_ADDITIVE_EXPRESSION, NULL);
29357 token = cp_lexer_peek_token (parser->lexer);
29358 decl_first = lhs == decl;
29359 if (decl_first)
29360 lhs = NULL_TREE;
29361 if (token->type != CPP_PLUS
29362 && token->type != CPP_MINUS)
29363 return error_mark_node;
29367 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29368 cp_lexer_consume_token (parser->lexer);
29369 rhs = cp_parser_binary_expression (parser, false, false,
29370 PREC_ADDITIVE_EXPRESSION, NULL);
29371 token = cp_lexer_peek_token (parser->lexer);
29372 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29374 if (lhs == NULL_TREE)
29376 if (op == PLUS_EXPR)
29377 lhs = rhs;
29378 else
29379 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29380 tf_warning_or_error);
29382 else
29383 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29384 ERROR_MARK, NULL, tf_warning_or_error);
29387 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29389 if (!decl_first)
29391 if (rhs != decl || op == MINUS_EXPR)
29392 return error_mark_node;
29393 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29395 else
29396 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29398 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29401 /* Parse the initialization statement of either an OpenMP for loop or
29402 a Cilk Plus for loop.
29404 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29405 Plus.
29407 Return true if the resulting construct should have an
29408 OMP_CLAUSE_PRIVATE added to it. */
29410 static bool
29411 cp_parser_omp_for_loop_init (cp_parser *parser,
29412 bool parsing_openmp,
29413 tree &this_pre_body,
29414 vec<tree, va_gc> *for_block,
29415 tree &init,
29416 tree &decl,
29417 tree &real_decl)
29419 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29420 return false;
29422 bool add_private_clause = false;
29424 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29426 init-expr:
29427 var = lb
29428 integer-type var = lb
29429 random-access-iterator-type var = lb
29430 pointer-type var = lb
29432 cp_decl_specifier_seq type_specifiers;
29434 /* First, try to parse as an initialized declaration. See
29435 cp_parser_condition, from whence the bulk of this is copied. */
29437 cp_parser_parse_tentatively (parser);
29438 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29439 /*is_trailing_return=*/false,
29440 &type_specifiers);
29441 if (cp_parser_parse_definitely (parser))
29443 /* If parsing a type specifier seq succeeded, then this
29444 MUST be a initialized declaration. */
29445 tree asm_specification, attributes;
29446 cp_declarator *declarator;
29448 declarator = cp_parser_declarator (parser,
29449 CP_PARSER_DECLARATOR_NAMED,
29450 /*ctor_dtor_or_conv_p=*/NULL,
29451 /*parenthesized_p=*/NULL,
29452 /*member_p=*/false);
29453 attributes = cp_parser_attributes_opt (parser);
29454 asm_specification = cp_parser_asm_specification_opt (parser);
29456 if (declarator == cp_error_declarator)
29457 cp_parser_skip_to_end_of_statement (parser);
29459 else
29461 tree pushed_scope, auto_node;
29463 decl = start_decl (declarator, &type_specifiers,
29464 SD_INITIALIZED, attributes,
29465 /*prefix_attributes=*/NULL_TREE,
29466 &pushed_scope);
29468 auto_node = type_uses_auto (TREE_TYPE (decl));
29469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29471 if (cp_lexer_next_token_is (parser->lexer,
29472 CPP_OPEN_PAREN))
29474 if (parsing_openmp)
29475 error ("parenthesized initialization is not allowed in "
29476 "OpenMP %<for%> loop");
29477 else
29478 error ("parenthesized initialization is "
29479 "not allowed in for-loop");
29481 else
29482 /* Trigger an error. */
29483 cp_parser_require (parser, CPP_EQ, RT_EQ);
29485 init = error_mark_node;
29486 cp_parser_skip_to_end_of_statement (parser);
29488 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29489 || type_dependent_expression_p (decl)
29490 || auto_node)
29492 bool is_direct_init, is_non_constant_init;
29494 init = cp_parser_initializer (parser,
29495 &is_direct_init,
29496 &is_non_constant_init);
29498 if (auto_node)
29500 TREE_TYPE (decl)
29501 = do_auto_deduction (TREE_TYPE (decl), init,
29502 auto_node);
29504 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29505 && !type_dependent_expression_p (decl))
29506 goto non_class;
29509 cp_finish_decl (decl, init, !is_non_constant_init,
29510 asm_specification,
29511 LOOKUP_ONLYCONVERTING);
29512 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29514 vec_safe_push (for_block, this_pre_body);
29515 init = NULL_TREE;
29517 else
29518 init = pop_stmt_list (this_pre_body);
29519 this_pre_body = NULL_TREE;
29521 else
29523 /* Consume '='. */
29524 cp_lexer_consume_token (parser->lexer);
29525 init = cp_parser_assignment_expression (parser, false, NULL);
29527 non_class:
29528 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29529 init = error_mark_node;
29530 else
29531 cp_finish_decl (decl, NULL_TREE,
29532 /*init_const_expr_p=*/false,
29533 asm_specification,
29534 LOOKUP_ONLYCONVERTING);
29537 if (pushed_scope)
29538 pop_scope (pushed_scope);
29541 else
29543 cp_id_kind idk;
29544 /* If parsing a type specifier sequence failed, then
29545 this MUST be a simple expression. */
29546 cp_parser_parse_tentatively (parser);
29547 decl = cp_parser_primary_expression (parser, false, false,
29548 false, &idk);
29549 if (!cp_parser_error_occurred (parser)
29550 && decl
29551 && DECL_P (decl)
29552 && CLASS_TYPE_P (TREE_TYPE (decl)))
29554 tree rhs;
29556 cp_parser_parse_definitely (parser);
29557 cp_parser_require (parser, CPP_EQ, RT_EQ);
29558 rhs = cp_parser_assignment_expression (parser, false, NULL);
29559 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29560 decl, NOP_EXPR,
29561 rhs,
29562 tf_warning_or_error));
29563 add_private_clause = true;
29565 else
29567 decl = NULL;
29568 cp_parser_abort_tentative_parse (parser);
29569 init = cp_parser_expression (parser, false, NULL);
29570 if (init)
29572 if (TREE_CODE (init) == MODIFY_EXPR
29573 || TREE_CODE (init) == MODOP_EXPR)
29574 real_decl = TREE_OPERAND (init, 0);
29578 return add_private_clause;
29581 /* Parse the restricted form of the for statement allowed by OpenMP. */
29583 static tree
29584 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29585 tree *cclauses)
29587 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29588 tree real_decl, initv, condv, incrv, declv;
29589 tree this_pre_body, cl;
29590 location_t loc_first;
29591 bool collapse_err = false;
29592 int i, collapse = 1, nbraces = 0;
29593 vec<tree, va_gc> *for_block = make_tree_vector ();
29595 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29596 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29597 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
29599 gcc_assert (collapse >= 1);
29601 declv = make_tree_vec (collapse);
29602 initv = make_tree_vec (collapse);
29603 condv = make_tree_vec (collapse);
29604 incrv = make_tree_vec (collapse);
29606 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29608 for (i = 0; i < collapse; i++)
29610 int bracecount = 0;
29611 bool add_private_clause = false;
29612 location_t loc;
29614 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29616 cp_parser_error (parser, "for statement expected");
29617 return NULL;
29619 loc = cp_lexer_consume_token (parser->lexer)->location;
29621 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29622 return NULL;
29624 init = decl = real_decl = NULL;
29625 this_pre_body = push_stmt_list ();
29627 add_private_clause
29628 |= cp_parser_omp_for_loop_init (parser,
29629 /*parsing_openmp=*/code != CILK_SIMD,
29630 this_pre_body, for_block,
29631 init, decl, real_decl);
29633 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29634 if (this_pre_body)
29636 this_pre_body = pop_stmt_list (this_pre_body);
29637 if (pre_body)
29639 tree t = pre_body;
29640 pre_body = push_stmt_list ();
29641 add_stmt (t);
29642 add_stmt (this_pre_body);
29643 pre_body = pop_stmt_list (pre_body);
29645 else
29646 pre_body = this_pre_body;
29649 if (decl)
29650 real_decl = decl;
29651 if (cclauses != NULL
29652 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29653 && real_decl != NULL_TREE)
29655 tree *c;
29656 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29657 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29658 && OMP_CLAUSE_DECL (*c) == real_decl)
29660 error_at (loc, "iteration variable %qD"
29661 " should not be firstprivate", real_decl);
29662 *c = OMP_CLAUSE_CHAIN (*c);
29664 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29665 && OMP_CLAUSE_DECL (*c) == real_decl)
29667 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29668 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29669 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29670 OMP_CLAUSE_DECL (l) = real_decl;
29671 OMP_CLAUSE_CHAIN (l) = clauses;
29672 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29673 clauses = l;
29674 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29675 CP_OMP_CLAUSE_INFO (*c) = NULL;
29676 add_private_clause = false;
29678 else
29680 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29681 && OMP_CLAUSE_DECL (*c) == real_decl)
29682 add_private_clause = false;
29683 c = &OMP_CLAUSE_CHAIN (*c);
29687 if (add_private_clause)
29689 tree c;
29690 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29692 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29693 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29694 && OMP_CLAUSE_DECL (c) == decl)
29695 break;
29696 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29697 && OMP_CLAUSE_DECL (c) == decl)
29698 error_at (loc, "iteration variable %qD "
29699 "should not be firstprivate",
29700 decl);
29701 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29702 && OMP_CLAUSE_DECL (c) == decl)
29703 error_at (loc, "iteration variable %qD should not be reduction",
29704 decl);
29706 if (c == NULL)
29708 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29709 OMP_CLAUSE_DECL (c) = decl;
29710 c = finish_omp_clauses (c);
29711 if (c)
29713 OMP_CLAUSE_CHAIN (c) = clauses;
29714 clauses = c;
29719 cond = NULL;
29720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29721 cond = cp_parser_omp_for_cond (parser, decl, code);
29722 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29724 incr = NULL;
29725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29727 /* If decl is an iterator, preserve the operator on decl
29728 until finish_omp_for. */
29729 if (real_decl
29730 && ((processing_template_decl
29731 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29732 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29733 incr = cp_parser_omp_for_incr (parser, real_decl);
29734 else
29735 incr = cp_parser_expression (parser, false, NULL);
29736 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29737 SET_EXPR_LOCATION (incr, input_location);
29740 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29741 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29742 /*or_comma=*/false,
29743 /*consume_paren=*/true);
29745 TREE_VEC_ELT (declv, i) = decl;
29746 TREE_VEC_ELT (initv, i) = init;
29747 TREE_VEC_ELT (condv, i) = cond;
29748 TREE_VEC_ELT (incrv, i) = incr;
29750 if (i == collapse - 1)
29751 break;
29753 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29754 in between the collapsed for loops to be still considered perfectly
29755 nested. Hopefully the final version clarifies this.
29756 For now handle (multiple) {'s and empty statements. */
29757 cp_parser_parse_tentatively (parser);
29760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29761 break;
29762 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29764 cp_lexer_consume_token (parser->lexer);
29765 bracecount++;
29767 else if (bracecount
29768 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29769 cp_lexer_consume_token (parser->lexer);
29770 else
29772 loc = cp_lexer_peek_token (parser->lexer)->location;
29773 error_at (loc, "not enough collapsed for loops");
29774 collapse_err = true;
29775 cp_parser_abort_tentative_parse (parser);
29776 declv = NULL_TREE;
29777 break;
29780 while (1);
29782 if (declv)
29784 cp_parser_parse_definitely (parser);
29785 nbraces += bracecount;
29789 /* Note that we saved the original contents of this flag when we entered
29790 the structured block, and so we don't need to re-save it here. */
29791 if (code == CILK_SIMD)
29792 parser->in_statement = IN_CILK_SIMD_FOR;
29793 else
29794 parser->in_statement = IN_OMP_FOR;
29796 /* Note that the grammar doesn't call for a structured block here,
29797 though the loop as a whole is a structured block. */
29798 body = push_stmt_list ();
29799 cp_parser_statement (parser, NULL_TREE, false, NULL);
29800 body = pop_stmt_list (body);
29802 if (declv == NULL_TREE)
29803 ret = NULL_TREE;
29804 else
29805 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29806 pre_body, clauses);
29808 while (nbraces)
29810 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29812 cp_lexer_consume_token (parser->lexer);
29813 nbraces--;
29815 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29816 cp_lexer_consume_token (parser->lexer);
29817 else
29819 if (!collapse_err)
29821 error_at (cp_lexer_peek_token (parser->lexer)->location,
29822 "collapsed loops not perfectly nested");
29824 collapse_err = true;
29825 cp_parser_statement_seq_opt (parser, NULL);
29826 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29827 break;
29831 while (!for_block->is_empty ())
29832 add_stmt (pop_stmt_list (for_block->pop ()));
29833 release_tree_vector (for_block);
29835 return ret;
29838 /* Helper function for OpenMP parsing, split clauses and call
29839 finish_omp_clauses on each of the set of clauses afterwards. */
29841 static void
29842 cp_omp_split_clauses (location_t loc, enum tree_code code,
29843 omp_clause_mask mask, tree clauses, tree *cclauses)
29845 int i;
29846 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29847 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29848 if (cclauses[i])
29849 cclauses[i] = finish_omp_clauses (cclauses[i]);
29852 /* OpenMP 4.0:
29853 #pragma omp simd simd-clause[optseq] new-line
29854 for-loop */
29856 #define OMP_SIMD_CLAUSE_MASK \
29857 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29865 static tree
29866 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29867 char *p_name, omp_clause_mask mask, tree *cclauses)
29869 tree clauses, sb, ret;
29870 unsigned int save;
29871 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29873 strcat (p_name, " simd");
29874 mask |= OMP_SIMD_CLAUSE_MASK;
29875 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29877 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29878 cclauses == NULL);
29879 if (cclauses)
29881 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29882 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29885 sb = begin_omp_structured_block ();
29886 save = cp_parser_begin_omp_structured_block (parser);
29888 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29890 cp_parser_end_omp_structured_block (parser, save);
29891 add_stmt (finish_omp_structured_block (sb));
29893 return ret;
29896 /* OpenMP 2.5:
29897 #pragma omp for for-clause[optseq] new-line
29898 for-loop
29900 OpenMP 4.0:
29901 #pragma omp for simd for-simd-clause[optseq] new-line
29902 for-loop */
29904 #define OMP_FOR_CLAUSE_MASK \
29905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29914 static tree
29915 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29916 char *p_name, omp_clause_mask mask, tree *cclauses)
29918 tree clauses, sb, ret;
29919 unsigned int save;
29920 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29922 strcat (p_name, " for");
29923 mask |= OMP_FOR_CLAUSE_MASK;
29924 if (cclauses)
29925 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29927 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29929 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29930 const char *p = IDENTIFIER_POINTER (id);
29932 if (strcmp (p, "simd") == 0)
29934 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29935 if (cclauses == NULL)
29936 cclauses = cclauses_buf;
29938 cp_lexer_consume_token (parser->lexer);
29939 if (!flag_openmp) /* flag_openmp_simd */
29940 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29941 cclauses);
29942 sb = begin_omp_structured_block ();
29943 save = cp_parser_begin_omp_structured_block (parser);
29944 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29945 cclauses);
29946 cp_parser_end_omp_structured_block (parser, save);
29947 tree body = finish_omp_structured_block (sb);
29948 if (ret == NULL)
29949 return ret;
29950 ret = make_node (OMP_FOR);
29951 TREE_TYPE (ret) = void_type_node;
29952 OMP_FOR_BODY (ret) = body;
29953 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29954 SET_EXPR_LOCATION (ret, loc);
29955 add_stmt (ret);
29956 return ret;
29959 if (!flag_openmp) /* flag_openmp_simd */
29961 cp_parser_require_pragma_eol (parser, pragma_tok);
29962 return NULL_TREE;
29965 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29966 cclauses == NULL);
29967 if (cclauses)
29969 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29970 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29973 sb = begin_omp_structured_block ();
29974 save = cp_parser_begin_omp_structured_block (parser);
29976 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29978 cp_parser_end_omp_structured_block (parser, save);
29979 add_stmt (finish_omp_structured_block (sb));
29981 return ret;
29984 /* OpenMP 2.5:
29985 # pragma omp master new-line
29986 structured-block */
29988 static tree
29989 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29991 cp_parser_require_pragma_eol (parser, pragma_tok);
29992 return c_finish_omp_master (input_location,
29993 cp_parser_omp_structured_block (parser));
29996 /* OpenMP 2.5:
29997 # pragma omp ordered new-line
29998 structured-block */
30000 static tree
30001 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30003 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30004 cp_parser_require_pragma_eol (parser, pragma_tok);
30005 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30008 /* OpenMP 2.5:
30010 section-scope:
30011 { section-sequence }
30013 section-sequence:
30014 section-directive[opt] structured-block
30015 section-sequence section-directive structured-block */
30017 static tree
30018 cp_parser_omp_sections_scope (cp_parser *parser)
30020 tree stmt, substmt;
30021 bool error_suppress = false;
30022 cp_token *tok;
30024 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30025 return NULL_TREE;
30027 stmt = push_stmt_list ();
30029 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30031 substmt = cp_parser_omp_structured_block (parser);
30032 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30033 add_stmt (substmt);
30036 while (1)
30038 tok = cp_lexer_peek_token (parser->lexer);
30039 if (tok->type == CPP_CLOSE_BRACE)
30040 break;
30041 if (tok->type == CPP_EOF)
30042 break;
30044 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30046 cp_lexer_consume_token (parser->lexer);
30047 cp_parser_require_pragma_eol (parser, tok);
30048 error_suppress = false;
30050 else if (!error_suppress)
30052 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30053 error_suppress = true;
30056 substmt = cp_parser_omp_structured_block (parser);
30057 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30058 add_stmt (substmt);
30060 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30062 substmt = pop_stmt_list (stmt);
30064 stmt = make_node (OMP_SECTIONS);
30065 TREE_TYPE (stmt) = void_type_node;
30066 OMP_SECTIONS_BODY (stmt) = substmt;
30068 add_stmt (stmt);
30069 return stmt;
30072 /* OpenMP 2.5:
30073 # pragma omp sections sections-clause[optseq] newline
30074 sections-scope */
30076 #define OMP_SECTIONS_CLAUSE_MASK \
30077 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30083 static tree
30084 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30085 char *p_name, omp_clause_mask mask, tree *cclauses)
30087 tree clauses, ret;
30088 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30090 strcat (p_name, " sections");
30091 mask |= OMP_SECTIONS_CLAUSE_MASK;
30092 if (cclauses)
30093 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30095 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30096 cclauses == NULL);
30097 if (cclauses)
30099 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30100 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30103 ret = cp_parser_omp_sections_scope (parser);
30104 if (ret)
30105 OMP_SECTIONS_CLAUSES (ret) = clauses;
30107 return ret;
30110 /* OpenMP 2.5:
30111 # pragma parallel parallel-clause new-line
30112 # pragma parallel for parallel-for-clause new-line
30113 # pragma parallel sections parallel-sections-clause new-line
30115 OpenMP 4.0:
30116 # pragma parallel for simd parallel-for-simd-clause new-line */
30118 #define OMP_PARALLEL_CLAUSE_MASK \
30119 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30129 static tree
30130 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30131 char *p_name, omp_clause_mask mask, tree *cclauses)
30133 tree stmt, clauses, block;
30134 unsigned int save;
30135 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30137 strcat (p_name, " parallel");
30138 mask |= OMP_PARALLEL_CLAUSE_MASK;
30140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30142 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30143 if (cclauses == NULL)
30144 cclauses = cclauses_buf;
30146 cp_lexer_consume_token (parser->lexer);
30147 if (!flag_openmp) /* flag_openmp_simd */
30148 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30149 block = begin_omp_parallel ();
30150 save = cp_parser_begin_omp_structured_block (parser);
30151 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30152 cp_parser_end_omp_structured_block (parser, save);
30153 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30154 block);
30155 OMP_PARALLEL_COMBINED (stmt) = 1;
30156 return stmt;
30158 else if (cclauses)
30160 error_at (loc, "expected %<for%> after %qs", p_name);
30161 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30162 return NULL_TREE;
30164 else if (!flag_openmp) /* flag_openmp_simd */
30166 cp_parser_require_pragma_eol (parser, pragma_tok);
30167 return NULL_TREE;
30169 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30171 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30172 const char *p = IDENTIFIER_POINTER (id);
30173 if (strcmp (p, "sections") == 0)
30175 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30176 cclauses = cclauses_buf;
30178 cp_lexer_consume_token (parser->lexer);
30179 block = begin_omp_parallel ();
30180 save = cp_parser_begin_omp_structured_block (parser);
30181 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30182 cp_parser_end_omp_structured_block (parser, save);
30183 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30184 block);
30185 OMP_PARALLEL_COMBINED (stmt) = 1;
30186 return stmt;
30190 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30192 block = begin_omp_parallel ();
30193 save = cp_parser_begin_omp_structured_block (parser);
30194 cp_parser_statement (parser, NULL_TREE, false, NULL);
30195 cp_parser_end_omp_structured_block (parser, save);
30196 stmt = finish_omp_parallel (clauses, block);
30197 return stmt;
30200 /* OpenMP 2.5:
30201 # pragma omp single single-clause[optseq] new-line
30202 structured-block */
30204 #define OMP_SINGLE_CLAUSE_MASK \
30205 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30210 static tree
30211 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30213 tree stmt = make_node (OMP_SINGLE);
30214 TREE_TYPE (stmt) = void_type_node;
30216 OMP_SINGLE_CLAUSES (stmt)
30217 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30218 "#pragma omp single", pragma_tok);
30219 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30221 return add_stmt (stmt);
30224 /* OpenMP 3.0:
30225 # pragma omp task task-clause[optseq] new-line
30226 structured-block */
30228 #define OMP_TASK_CLAUSE_MASK \
30229 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30239 static tree
30240 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30242 tree clauses, block;
30243 unsigned int save;
30245 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30246 "#pragma omp task", pragma_tok);
30247 block = begin_omp_task ();
30248 save = cp_parser_begin_omp_structured_block (parser);
30249 cp_parser_statement (parser, NULL_TREE, false, NULL);
30250 cp_parser_end_omp_structured_block (parser, save);
30251 return finish_omp_task (clauses, block);
30254 /* OpenMP 3.0:
30255 # pragma omp taskwait new-line */
30257 static void
30258 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30260 cp_parser_require_pragma_eol (parser, pragma_tok);
30261 finish_omp_taskwait ();
30264 /* OpenMP 3.1:
30265 # pragma omp taskyield new-line */
30267 static void
30268 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30270 cp_parser_require_pragma_eol (parser, pragma_tok);
30271 finish_omp_taskyield ();
30274 /* OpenMP 4.0:
30275 # pragma omp taskgroup new-line
30276 structured-block */
30278 static tree
30279 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30281 cp_parser_require_pragma_eol (parser, pragma_tok);
30282 return c_finish_omp_taskgroup (input_location,
30283 cp_parser_omp_structured_block (parser));
30287 /* OpenMP 2.5:
30288 # pragma omp threadprivate (variable-list) */
30290 static void
30291 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30293 tree vars;
30295 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30296 cp_parser_require_pragma_eol (parser, pragma_tok);
30298 finish_omp_threadprivate (vars);
30301 /* OpenMP 4.0:
30302 # pragma omp cancel cancel-clause[optseq] new-line */
30304 #define OMP_CANCEL_CLAUSE_MASK \
30305 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30311 static void
30312 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30314 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30315 "#pragma omp cancel", pragma_tok);
30316 finish_omp_cancel (clauses);
30319 /* OpenMP 4.0:
30320 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30322 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30323 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30328 static void
30329 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30331 tree clauses;
30332 bool point_seen = false;
30334 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30336 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30337 const char *p = IDENTIFIER_POINTER (id);
30339 if (strcmp (p, "point") == 0)
30341 cp_lexer_consume_token (parser->lexer);
30342 point_seen = true;
30345 if (!point_seen)
30347 cp_parser_error (parser, "expected %<point%>");
30348 cp_parser_require_pragma_eol (parser, pragma_tok);
30349 return;
30352 clauses = cp_parser_omp_all_clauses (parser,
30353 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30354 "#pragma omp cancellation point",
30355 pragma_tok);
30356 finish_omp_cancellation_point (clauses);
30359 /* OpenMP 4.0:
30360 #pragma omp distribute distribute-clause[optseq] new-line
30361 for-loop */
30363 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30369 static tree
30370 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30371 char *p_name, omp_clause_mask mask, tree *cclauses)
30373 tree clauses, sb, ret;
30374 unsigned int save;
30375 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30377 strcat (p_name, " distribute");
30378 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30380 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30382 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30383 const char *p = IDENTIFIER_POINTER (id);
30384 bool simd = false;
30385 bool parallel = false;
30387 if (strcmp (p, "simd") == 0)
30388 simd = true;
30389 else
30390 parallel = strcmp (p, "parallel") == 0;
30391 if (parallel || simd)
30393 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30394 if (cclauses == NULL)
30395 cclauses = cclauses_buf;
30396 cp_lexer_consume_token (parser->lexer);
30397 if (!flag_openmp) /* flag_openmp_simd */
30399 if (simd)
30400 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30401 cclauses);
30402 else
30403 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30404 cclauses);
30406 sb = begin_omp_structured_block ();
30407 save = cp_parser_begin_omp_structured_block (parser);
30408 if (simd)
30409 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30410 cclauses);
30411 else
30412 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30413 cclauses);
30414 cp_parser_end_omp_structured_block (parser, save);
30415 tree body = finish_omp_structured_block (sb);
30416 if (ret == NULL)
30417 return ret;
30418 ret = make_node (OMP_DISTRIBUTE);
30419 TREE_TYPE (ret) = void_type_node;
30420 OMP_FOR_BODY (ret) = body;
30421 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30422 SET_EXPR_LOCATION (ret, loc);
30423 add_stmt (ret);
30424 return ret;
30427 if (!flag_openmp) /* flag_openmp_simd */
30429 cp_parser_require_pragma_eol (parser, pragma_tok);
30430 return NULL_TREE;
30433 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30434 cclauses == NULL);
30435 if (cclauses)
30437 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30438 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30441 sb = begin_omp_structured_block ();
30442 save = cp_parser_begin_omp_structured_block (parser);
30444 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30446 cp_parser_end_omp_structured_block (parser, save);
30447 add_stmt (finish_omp_structured_block (sb));
30449 return ret;
30452 /* OpenMP 4.0:
30453 # pragma omp teams teams-clause[optseq] new-line
30454 structured-block */
30456 #define OMP_TEAMS_CLAUSE_MASK \
30457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30465 static tree
30466 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30467 char *p_name, omp_clause_mask mask, tree *cclauses)
30469 tree clauses, sb, ret;
30470 unsigned int save;
30471 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30473 strcat (p_name, " teams");
30474 mask |= OMP_TEAMS_CLAUSE_MASK;
30476 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30478 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30479 const char *p = IDENTIFIER_POINTER (id);
30480 if (strcmp (p, "distribute") == 0)
30482 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30483 if (cclauses == NULL)
30484 cclauses = cclauses_buf;
30486 cp_lexer_consume_token (parser->lexer);
30487 if (!flag_openmp) /* flag_openmp_simd */
30488 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30489 cclauses);
30490 sb = begin_omp_structured_block ();
30491 save = cp_parser_begin_omp_structured_block (parser);
30492 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30493 cclauses);
30494 cp_parser_end_omp_structured_block (parser, save);
30495 tree body = finish_omp_structured_block (sb);
30496 if (ret == NULL)
30497 return ret;
30498 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30499 ret = make_node (OMP_TEAMS);
30500 TREE_TYPE (ret) = void_type_node;
30501 OMP_TEAMS_CLAUSES (ret) = clauses;
30502 OMP_TEAMS_BODY (ret) = body;
30503 return add_stmt (ret);
30506 if (!flag_openmp) /* flag_openmp_simd */
30508 cp_parser_require_pragma_eol (parser, pragma_tok);
30509 return NULL_TREE;
30512 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30513 cclauses == NULL);
30514 if (cclauses)
30516 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30517 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30520 tree stmt = make_node (OMP_TEAMS);
30521 TREE_TYPE (stmt) = void_type_node;
30522 OMP_TEAMS_CLAUSES (stmt) = clauses;
30523 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30525 return add_stmt (stmt);
30528 /* OpenMP 4.0:
30529 # pragma omp target data target-data-clause[optseq] new-line
30530 structured-block */
30532 #define OMP_TARGET_DATA_CLAUSE_MASK \
30533 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30537 static tree
30538 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30540 tree stmt = make_node (OMP_TARGET_DATA);
30541 TREE_TYPE (stmt) = void_type_node;
30543 OMP_TARGET_DATA_CLAUSES (stmt)
30544 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30545 "#pragma omp target data", pragma_tok);
30546 keep_next_level (true);
30547 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30549 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30550 return add_stmt (stmt);
30553 /* OpenMP 4.0:
30554 # pragma omp target update target-update-clause[optseq] new-line */
30556 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30557 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30562 static bool
30563 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30564 enum pragma_context context)
30566 if (context == pragma_stmt)
30568 error_at (pragma_tok->location,
30569 "%<#pragma omp target update%> may only be "
30570 "used in compound statements");
30571 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30572 return false;
30575 tree clauses
30576 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30577 "#pragma omp target update", pragma_tok);
30578 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30579 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30581 error_at (pragma_tok->location,
30582 "%<#pragma omp target update must contain at least one "
30583 "%<from%> or %<to%> clauses");
30584 return false;
30587 tree stmt = make_node (OMP_TARGET_UPDATE);
30588 TREE_TYPE (stmt) = void_type_node;
30589 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30590 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30591 add_stmt (stmt);
30592 return false;
30595 /* OpenMP 4.0:
30596 # pragma omp target target-clause[optseq] new-line
30597 structured-block */
30599 #define OMP_TARGET_CLAUSE_MASK \
30600 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30604 static bool
30605 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30606 enum pragma_context context)
30608 if (context != pragma_stmt && context != pragma_compound)
30610 cp_parser_error (parser, "expected declaration specifiers");
30611 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30612 return false;
30615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30617 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30618 const char *p = IDENTIFIER_POINTER (id);
30620 if (strcmp (p, "teams") == 0)
30622 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30623 char p_name[sizeof ("#pragma omp target teams distribute "
30624 "parallel for simd")];
30626 cp_lexer_consume_token (parser->lexer);
30627 strcpy (p_name, "#pragma omp target");
30628 keep_next_level (true);
30629 if (!flag_openmp) /* flag_openmp_simd */
30630 return cp_parser_omp_teams (parser, pragma_tok, p_name,
30631 OMP_TARGET_CLAUSE_MASK, cclauses);
30632 tree sb = begin_omp_structured_block ();
30633 unsigned save = cp_parser_begin_omp_structured_block (parser);
30634 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30635 OMP_TARGET_CLAUSE_MASK, cclauses);
30636 cp_parser_end_omp_structured_block (parser, save);
30637 tree body = finish_omp_structured_block (sb);
30638 if (ret == NULL)
30639 return ret;
30640 tree stmt = make_node (OMP_TARGET);
30641 TREE_TYPE (stmt) = void_type_node;
30642 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30643 OMP_TARGET_BODY (stmt) = body;
30644 add_stmt (stmt);
30645 return true;
30647 else if (!flag_openmp) /* flag_openmp_simd */
30649 cp_parser_require_pragma_eol (parser, pragma_tok);
30650 return NULL_TREE;
30652 else if (strcmp (p, "data") == 0)
30654 cp_lexer_consume_token (parser->lexer);
30655 cp_parser_omp_target_data (parser, pragma_tok);
30656 return true;
30658 else if (strcmp (p, "update") == 0)
30660 cp_lexer_consume_token (parser->lexer);
30661 return cp_parser_omp_target_update (parser, pragma_tok, context);
30665 tree stmt = make_node (OMP_TARGET);
30666 TREE_TYPE (stmt) = void_type_node;
30668 OMP_TARGET_CLAUSES (stmt)
30669 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30670 "#pragma omp target", pragma_tok);
30671 keep_next_level (true);
30672 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30674 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30675 add_stmt (stmt);
30676 return true;
30679 /* OpenMP 4.0:
30680 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30682 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30683 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30690 static void
30691 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30692 enum pragma_context context)
30694 bool first_p = parser->omp_declare_simd == NULL;
30695 cp_omp_declare_simd_data data;
30696 if (first_p)
30698 data.error_seen = false;
30699 data.fndecl_seen = false;
30700 data.tokens = vNULL;
30701 parser->omp_declare_simd = &data;
30703 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30704 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30705 cp_lexer_consume_token (parser->lexer);
30706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30707 parser->omp_declare_simd->error_seen = true;
30708 cp_parser_require_pragma_eol (parser, pragma_tok);
30709 struct cp_token_cache *cp
30710 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30711 parser->omp_declare_simd->tokens.safe_push (cp);
30712 if (first_p)
30714 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30715 cp_parser_pragma (parser, context);
30716 switch (context)
30718 case pragma_external:
30719 cp_parser_declaration (parser);
30720 break;
30721 case pragma_member:
30722 cp_parser_member_declaration (parser);
30723 break;
30724 case pragma_objc_icode:
30725 cp_parser_block_declaration (parser, /*statement_p=*/false);
30726 break;
30727 default:
30728 cp_parser_declaration_statement (parser);
30729 break;
30731 if (parser->omp_declare_simd
30732 && !parser->omp_declare_simd->error_seen
30733 && !parser->omp_declare_simd->fndecl_seen)
30734 error_at (pragma_tok->location,
30735 "%<#pragma omp declare simd%> not immediately followed by "
30736 "function declaration or definition");
30737 data.tokens.release ();
30738 parser->omp_declare_simd = NULL;
30742 /* Finalize #pragma omp declare simd clauses after direct declarator has
30743 been parsed, and put that into "omp declare simd" attribute. */
30745 static tree
30746 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30748 struct cp_token_cache *ce;
30749 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30750 int i;
30752 if (!data->error_seen && data->fndecl_seen)
30754 error ("%<#pragma omp declare simd%> not immediately followed by "
30755 "a single function declaration or definition");
30756 data->error_seen = true;
30757 return attrs;
30759 if (data->error_seen)
30760 return attrs;
30762 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30764 tree c, cl;
30766 cp_parser_push_lexer_for_tokens (parser, ce);
30767 parser->lexer->in_pragma = true;
30768 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30769 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30770 cp_lexer_consume_token (parser->lexer);
30771 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30772 "#pragma omp declare simd", pragma_tok);
30773 cp_parser_pop_lexer (parser);
30774 if (cl)
30775 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30776 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30777 TREE_CHAIN (c) = attrs;
30778 if (processing_template_decl)
30779 ATTR_IS_DEPENDENT (c) = 1;
30780 attrs = c;
30783 data->fndecl_seen = true;
30784 return attrs;
30788 /* OpenMP 4.0:
30789 # pragma omp declare target new-line
30790 declarations and definitions
30791 # pragma omp end declare target new-line */
30793 static void
30794 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30796 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30797 scope_chain->omp_declare_target_attribute++;
30800 static void
30801 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30803 const char *p = "";
30804 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30806 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30807 p = IDENTIFIER_POINTER (id);
30809 if (strcmp (p, "declare") == 0)
30811 cp_lexer_consume_token (parser->lexer);
30812 p = "";
30813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30815 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30816 p = IDENTIFIER_POINTER (id);
30818 if (strcmp (p, "target") == 0)
30819 cp_lexer_consume_token (parser->lexer);
30820 else
30822 cp_parser_error (parser, "expected %<target%>");
30823 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30824 return;
30827 else
30829 cp_parser_error (parser, "expected %<declare%>");
30830 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30831 return;
30833 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30834 if (!scope_chain->omp_declare_target_attribute)
30835 error_at (pragma_tok->location,
30836 "%<#pragma omp end declare target%> without corresponding "
30837 "%<#pragma omp declare target%>");
30838 else
30839 scope_chain->omp_declare_target_attribute--;
30842 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30843 expression and optional initializer clause of
30844 #pragma omp declare reduction. We store the expression(s) as
30845 either 3, 6 or 7 special statements inside of the artificial function's
30846 body. The first two statements are DECL_EXPRs for the artificial
30847 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30848 expression that uses those variables.
30849 If there was any INITIALIZER clause, this is followed by further statements,
30850 the fourth and fifth statements are DECL_EXPRs for the artificial
30851 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30852 constructor variant (first token after open paren is not omp_priv),
30853 then the sixth statement is a statement with the function call expression
30854 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30855 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30856 to initialize the OMP_PRIV artificial variable and there is seventh
30857 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30859 static bool
30860 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30862 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30863 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30864 type = TREE_TYPE (type);
30865 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30866 DECL_ARTIFICIAL (omp_out) = 1;
30867 pushdecl (omp_out);
30868 add_decl_expr (omp_out);
30869 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30870 DECL_ARTIFICIAL (omp_in) = 1;
30871 pushdecl (omp_in);
30872 add_decl_expr (omp_in);
30873 tree combiner;
30874 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30876 keep_next_level (true);
30877 tree block = begin_omp_structured_block ();
30878 combiner = cp_parser_expression (parser, false, NULL);
30879 finish_expr_stmt (combiner);
30880 block = finish_omp_structured_block (block);
30881 add_stmt (block);
30883 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30884 return false;
30886 const char *p = "";
30887 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30889 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30890 p = IDENTIFIER_POINTER (id);
30893 if (strcmp (p, "initializer") == 0)
30895 cp_lexer_consume_token (parser->lexer);
30896 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30897 return false;
30899 p = "";
30900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30902 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30903 p = IDENTIFIER_POINTER (id);
30906 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30907 DECL_ARTIFICIAL (omp_priv) = 1;
30908 pushdecl (omp_priv);
30909 add_decl_expr (omp_priv);
30910 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30911 DECL_ARTIFICIAL (omp_orig) = 1;
30912 pushdecl (omp_orig);
30913 add_decl_expr (omp_orig);
30915 keep_next_level (true);
30916 block = begin_omp_structured_block ();
30918 bool ctor = false;
30919 if (strcmp (p, "omp_priv") == 0)
30921 bool is_direct_init, is_non_constant_init;
30922 ctor = true;
30923 cp_lexer_consume_token (parser->lexer);
30924 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30925 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30926 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30927 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30928 == CPP_CLOSE_PAREN
30929 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30930 == CPP_CLOSE_PAREN))
30932 finish_omp_structured_block (block);
30933 error ("invalid initializer clause");
30934 return false;
30936 initializer = cp_parser_initializer (parser, &is_direct_init,
30937 &is_non_constant_init);
30938 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30939 NULL_TREE, LOOKUP_ONLYCONVERTING);
30941 else
30943 cp_parser_parse_tentatively (parser);
30944 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30945 /*check_dependency_p=*/true,
30946 /*template_p=*/NULL,
30947 /*declarator_p=*/false,
30948 /*optional_p=*/false);
30949 vec<tree, va_gc> *args;
30950 if (fn_name == error_mark_node
30951 || cp_parser_error_occurred (parser)
30952 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30953 || ((args = cp_parser_parenthesized_expression_list
30954 (parser, non_attr, /*cast_p=*/false,
30955 /*allow_expansion_p=*/true,
30956 /*non_constant_p=*/NULL)),
30957 cp_parser_error_occurred (parser)))
30959 finish_omp_structured_block (block);
30960 cp_parser_abort_tentative_parse (parser);
30961 cp_parser_error (parser, "expected id-expression (arguments)");
30962 return false;
30964 unsigned int i;
30965 tree arg;
30966 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30967 if (arg == omp_priv
30968 || (TREE_CODE (arg) == ADDR_EXPR
30969 && TREE_OPERAND (arg, 0) == omp_priv))
30970 break;
30971 cp_parser_abort_tentative_parse (parser);
30972 if (arg == NULL_TREE)
30973 error ("one of the initializer call arguments should be %<omp_priv%>"
30974 " or %<&omp_priv%>");
30975 initializer = cp_parser_postfix_expression (parser, false, false, false,
30976 false, NULL);
30977 finish_expr_stmt (initializer);
30980 block = finish_omp_structured_block (block);
30981 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30982 finish_expr_stmt (block);
30984 if (ctor)
30985 add_decl_expr (omp_orig);
30987 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30988 return false;
30991 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30992 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30994 return true;
30997 /* OpenMP 4.0
30998 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30999 initializer-clause[opt] new-line
31001 initializer-clause:
31002 initializer (omp_priv initializer)
31003 initializer (function-name (argument-list)) */
31005 static void
31006 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31007 enum pragma_context)
31009 vec<tree> types = vNULL;
31010 enum tree_code reduc_code = ERROR_MARK;
31011 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31012 unsigned int i;
31013 cp_token *first_token;
31014 cp_token_cache *cp;
31015 int errs;
31017 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31018 goto fail;
31020 switch (cp_lexer_peek_token (parser->lexer)->type)
31022 case CPP_PLUS:
31023 reduc_code = PLUS_EXPR;
31024 break;
31025 case CPP_MULT:
31026 reduc_code = MULT_EXPR;
31027 break;
31028 case CPP_MINUS:
31029 reduc_code = MINUS_EXPR;
31030 break;
31031 case CPP_AND:
31032 reduc_code = BIT_AND_EXPR;
31033 break;
31034 case CPP_XOR:
31035 reduc_code = BIT_XOR_EXPR;
31036 break;
31037 case CPP_OR:
31038 reduc_code = BIT_IOR_EXPR;
31039 break;
31040 case CPP_AND_AND:
31041 reduc_code = TRUTH_ANDIF_EXPR;
31042 break;
31043 case CPP_OR_OR:
31044 reduc_code = TRUTH_ORIF_EXPR;
31045 break;
31046 case CPP_NAME:
31047 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31048 break;
31049 default:
31050 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31051 "%<|%>, %<&&%>, %<||%> or identifier");
31052 goto fail;
31055 if (reduc_code != ERROR_MARK)
31056 cp_lexer_consume_token (parser->lexer);
31058 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31059 if (reduc_id == error_mark_node)
31060 goto fail;
31062 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31063 goto fail;
31065 /* Types may not be defined in declare reduction type list. */
31066 const char *saved_message;
31067 saved_message = parser->type_definition_forbidden_message;
31068 parser->type_definition_forbidden_message
31069 = G_("types may not be defined in declare reduction type list");
31070 bool saved_colon_corrects_to_scope_p;
31071 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31072 parser->colon_corrects_to_scope_p = false;
31073 bool saved_colon_doesnt_start_class_def_p;
31074 saved_colon_doesnt_start_class_def_p
31075 = parser->colon_doesnt_start_class_def_p;
31076 parser->colon_doesnt_start_class_def_p = true;
31078 while (true)
31080 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31081 type = cp_parser_type_id (parser);
31082 if (type == error_mark_node)
31084 else if (ARITHMETIC_TYPE_P (type)
31085 && (orig_reduc_id == NULL_TREE
31086 || (TREE_CODE (type) != COMPLEX_TYPE
31087 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31088 "min") == 0
31089 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31090 "max") == 0))))
31091 error_at (loc, "predeclared arithmetic type %qT in "
31092 "%<#pragma omp declare reduction%>", type);
31093 else if (TREE_CODE (type) == FUNCTION_TYPE
31094 || TREE_CODE (type) == METHOD_TYPE
31095 || TREE_CODE (type) == ARRAY_TYPE)
31096 error_at (loc, "function or array type %qT in "
31097 "%<#pragma omp declare reduction%>", type);
31098 else if (TREE_CODE (type) == REFERENCE_TYPE)
31099 error_at (loc, "reference type %qT in "
31100 "%<#pragma omp declare reduction%>", type);
31101 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31102 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31103 "%<#pragma omp declare reduction%>", type);
31104 else
31105 types.safe_push (type);
31107 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31108 cp_lexer_consume_token (parser->lexer);
31109 else
31110 break;
31113 /* Restore the saved message. */
31114 parser->type_definition_forbidden_message = saved_message;
31115 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31116 parser->colon_doesnt_start_class_def_p
31117 = saved_colon_doesnt_start_class_def_p;
31119 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31120 || types.is_empty ())
31122 fail:
31123 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31124 types.release ();
31125 return;
31128 first_token = cp_lexer_peek_token (parser->lexer);
31129 cp = NULL;
31130 errs = errorcount;
31131 FOR_EACH_VEC_ELT (types, i, type)
31133 tree fntype
31134 = build_function_type_list (void_type_node,
31135 cp_build_reference_type (type, false),
31136 NULL_TREE);
31137 tree this_reduc_id = reduc_id;
31138 if (!dependent_type_p (type))
31139 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31140 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31141 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31142 DECL_ARTIFICIAL (fndecl) = 1;
31143 DECL_EXTERNAL (fndecl) = 1;
31144 DECL_DECLARED_INLINE_P (fndecl) = 1;
31145 DECL_IGNORED_P (fndecl) = 1;
31146 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31147 DECL_ATTRIBUTES (fndecl)
31148 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31149 DECL_ATTRIBUTES (fndecl));
31150 if (processing_template_decl)
31151 fndecl = push_template_decl (fndecl);
31152 bool block_scope = false;
31153 tree block = NULL_TREE;
31154 if (current_function_decl)
31156 block_scope = true;
31157 DECL_CONTEXT (fndecl) = global_namespace;
31158 if (!processing_template_decl)
31159 pushdecl (fndecl);
31161 else if (current_class_type)
31163 if (cp == NULL)
31165 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31166 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31167 cp_lexer_consume_token (parser->lexer);
31168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31169 goto fail;
31170 cp = cp_token_cache_new (first_token,
31171 cp_lexer_peek_nth_token (parser->lexer,
31172 2));
31174 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31175 finish_member_declaration (fndecl);
31176 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31177 DECL_PENDING_INLINE_P (fndecl) = 1;
31178 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31179 continue;
31181 else
31183 DECL_CONTEXT (fndecl) = current_namespace;
31184 pushdecl (fndecl);
31186 if (!block_scope)
31187 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31188 else
31189 block = begin_omp_structured_block ();
31190 if (cp)
31192 cp_parser_push_lexer_for_tokens (parser, cp);
31193 parser->lexer->in_pragma = true;
31195 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31197 if (!block_scope)
31198 finish_function (0);
31199 else
31200 DECL_CONTEXT (fndecl) = current_function_decl;
31201 if (cp)
31202 cp_parser_pop_lexer (parser);
31203 goto fail;
31205 if (cp)
31206 cp_parser_pop_lexer (parser);
31207 if (!block_scope)
31208 finish_function (0);
31209 else
31211 DECL_CONTEXT (fndecl) = current_function_decl;
31212 block = finish_omp_structured_block (block);
31213 if (TREE_CODE (block) == BIND_EXPR)
31214 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31215 else if (TREE_CODE (block) == STATEMENT_LIST)
31216 DECL_SAVED_TREE (fndecl) = block;
31217 if (processing_template_decl)
31218 add_decl_expr (fndecl);
31220 cp_check_omp_declare_reduction (fndecl);
31221 if (cp == NULL && types.length () > 1)
31222 cp = cp_token_cache_new (first_token,
31223 cp_lexer_peek_nth_token (parser->lexer, 2));
31224 if (errs != errorcount)
31225 break;
31228 cp_parser_require_pragma_eol (parser, pragma_tok);
31229 types.release ();
31232 /* OpenMP 4.0
31233 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31234 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31235 initializer-clause[opt] new-line
31236 #pragma omp declare target new-line */
31238 static void
31239 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31240 enum pragma_context context)
31242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31245 const char *p = IDENTIFIER_POINTER (id);
31247 if (strcmp (p, "simd") == 0)
31249 cp_lexer_consume_token (parser->lexer);
31250 cp_parser_omp_declare_simd (parser, pragma_tok,
31251 context);
31252 return;
31254 cp_ensure_no_omp_declare_simd (parser);
31255 if (strcmp (p, "reduction") == 0)
31257 cp_lexer_consume_token (parser->lexer);
31258 cp_parser_omp_declare_reduction (parser, pragma_tok,
31259 context);
31260 return;
31262 if (!flag_openmp) /* flag_openmp_simd */
31264 cp_parser_require_pragma_eol (parser, pragma_tok);
31265 return;
31267 if (strcmp (p, "target") == 0)
31269 cp_lexer_consume_token (parser->lexer);
31270 cp_parser_omp_declare_target (parser, pragma_tok);
31271 return;
31274 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31275 "or %<target%>");
31276 cp_parser_require_pragma_eol (parser, pragma_tok);
31279 /* Main entry point to OpenMP statement pragmas. */
31281 static void
31282 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31284 tree stmt;
31285 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31286 omp_clause_mask mask (0);
31288 switch (pragma_tok->pragma_kind)
31290 case PRAGMA_OMP_ATOMIC:
31291 cp_parser_omp_atomic (parser, pragma_tok);
31292 return;
31293 case PRAGMA_OMP_CRITICAL:
31294 stmt = cp_parser_omp_critical (parser, pragma_tok);
31295 break;
31296 case PRAGMA_OMP_DISTRIBUTE:
31297 strcpy (p_name, "#pragma omp");
31298 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31299 break;
31300 case PRAGMA_OMP_FOR:
31301 strcpy (p_name, "#pragma omp");
31302 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31303 break;
31304 case PRAGMA_OMP_MASTER:
31305 stmt = cp_parser_omp_master (parser, pragma_tok);
31306 break;
31307 case PRAGMA_OMP_ORDERED:
31308 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31309 break;
31310 case PRAGMA_OMP_PARALLEL:
31311 strcpy (p_name, "#pragma omp");
31312 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31313 break;
31314 case PRAGMA_OMP_SECTIONS:
31315 strcpy (p_name, "#pragma omp");
31316 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31317 break;
31318 case PRAGMA_OMP_SIMD:
31319 strcpy (p_name, "#pragma omp");
31320 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31321 break;
31322 case PRAGMA_OMP_SINGLE:
31323 stmt = cp_parser_omp_single (parser, pragma_tok);
31324 break;
31325 case PRAGMA_OMP_TASK:
31326 stmt = cp_parser_omp_task (parser, pragma_tok);
31327 break;
31328 case PRAGMA_OMP_TASKGROUP:
31329 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31330 break;
31331 case PRAGMA_OMP_TEAMS:
31332 strcpy (p_name, "#pragma omp");
31333 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31334 break;
31335 default:
31336 gcc_unreachable ();
31339 if (stmt)
31340 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31343 /* Transactional Memory parsing routines. */
31345 /* Parse a transaction attribute.
31347 txn-attribute:
31348 attribute
31349 [ [ identifier ] ]
31351 ??? Simplify this when C++0x bracket attributes are
31352 implemented properly. */
31354 static tree
31355 cp_parser_txn_attribute_opt (cp_parser *parser)
31357 cp_token *token;
31358 tree attr_name, attr = NULL;
31360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31361 return cp_parser_attributes_opt (parser);
31363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31364 return NULL_TREE;
31365 cp_lexer_consume_token (parser->lexer);
31366 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31367 goto error1;
31369 token = cp_lexer_peek_token (parser->lexer);
31370 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31372 token = cp_lexer_consume_token (parser->lexer);
31374 attr_name = (token->type == CPP_KEYWORD
31375 /* For keywords, use the canonical spelling,
31376 not the parsed identifier. */
31377 ? ridpointers[(int) token->keyword]
31378 : token->u.value);
31379 attr = build_tree_list (attr_name, NULL_TREE);
31381 else
31382 cp_parser_error (parser, "expected identifier");
31384 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31385 error1:
31386 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31387 return attr;
31390 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31392 transaction-statement:
31393 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31394 compound-statement
31395 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31398 static tree
31399 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31401 unsigned char old_in = parser->in_transaction;
31402 unsigned char this_in = 1, new_in;
31403 cp_token *token;
31404 tree stmt, attrs, noex;
31406 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31407 || keyword == RID_TRANSACTION_RELAXED);
31408 token = cp_parser_require_keyword (parser, keyword,
31409 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31410 : RT_TRANSACTION_RELAXED));
31411 gcc_assert (token != NULL);
31413 if (keyword == RID_TRANSACTION_RELAXED)
31414 this_in |= TM_STMT_ATTR_RELAXED;
31415 else
31417 attrs = cp_parser_txn_attribute_opt (parser);
31418 if (attrs)
31419 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31422 /* Parse a noexcept specification. */
31423 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31425 /* Keep track if we're in the lexical scope of an outer transaction. */
31426 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31428 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31430 parser->in_transaction = new_in;
31431 cp_parser_compound_statement (parser, NULL, false, false);
31432 parser->in_transaction = old_in;
31434 finish_transaction_stmt (stmt, NULL, this_in, noex);
31436 return stmt;
31439 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31441 transaction-expression:
31442 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31443 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31446 static tree
31447 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31449 unsigned char old_in = parser->in_transaction;
31450 unsigned char this_in = 1;
31451 cp_token *token;
31452 tree expr, noex;
31453 bool noex_expr;
31455 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31456 || keyword == RID_TRANSACTION_RELAXED);
31458 if (!flag_tm)
31459 error (keyword == RID_TRANSACTION_RELAXED
31460 ? G_("%<__transaction_relaxed%> without transactional memory "
31461 "support enabled")
31462 : G_("%<__transaction_atomic%> without transactional memory "
31463 "support enabled"));
31465 token = cp_parser_require_keyword (parser, keyword,
31466 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31467 : RT_TRANSACTION_RELAXED));
31468 gcc_assert (token != NULL);
31470 if (keyword == RID_TRANSACTION_RELAXED)
31471 this_in |= TM_STMT_ATTR_RELAXED;
31473 /* Set this early. This might mean that we allow transaction_cancel in
31474 an expression that we find out later actually has to be a constexpr.
31475 However, we expect that cxx_constant_value will be able to deal with
31476 this; also, if the noexcept has no constexpr, then what we parse next
31477 really is a transaction's body. */
31478 parser->in_transaction = this_in;
31480 /* Parse a noexcept specification. */
31481 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31482 true);
31484 if (!noex || !noex_expr
31485 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31487 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31489 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31490 expr = finish_parenthesized_expr (expr);
31492 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31494 else
31496 /* The only expression that is available got parsed for the noexcept
31497 already. noexcept is true then. */
31498 expr = noex;
31499 noex = boolean_true_node;
31502 expr = build_transaction_expr (token->location, expr, this_in, noex);
31503 parser->in_transaction = old_in;
31505 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31506 return error_mark_node;
31508 return (flag_tm ? expr : error_mark_node);
31511 /* Parse a function-transaction-block.
31513 function-transaction-block:
31514 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31515 function-body
31516 __transaction_atomic txn-attribute[opt] function-try-block
31517 __transaction_relaxed ctor-initializer[opt] function-body
31518 __transaction_relaxed function-try-block
31521 static bool
31522 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31524 unsigned char old_in = parser->in_transaction;
31525 unsigned char new_in = 1;
31526 tree compound_stmt, stmt, attrs;
31527 bool ctor_initializer_p;
31528 cp_token *token;
31530 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31531 || keyword == RID_TRANSACTION_RELAXED);
31532 token = cp_parser_require_keyword (parser, keyword,
31533 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31534 : RT_TRANSACTION_RELAXED));
31535 gcc_assert (token != NULL);
31537 if (keyword == RID_TRANSACTION_RELAXED)
31538 new_in |= TM_STMT_ATTR_RELAXED;
31539 else
31541 attrs = cp_parser_txn_attribute_opt (parser);
31542 if (attrs)
31543 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31546 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31548 parser->in_transaction = new_in;
31550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31551 ctor_initializer_p = cp_parser_function_try_block (parser);
31552 else
31553 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31554 (parser, /*in_function_try_block=*/false);
31556 parser->in_transaction = old_in;
31558 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31560 return ctor_initializer_p;
31563 /* Parse a __transaction_cancel statement.
31565 cancel-statement:
31566 __transaction_cancel txn-attribute[opt] ;
31567 __transaction_cancel txn-attribute[opt] throw-expression ;
31569 ??? Cancel and throw is not yet implemented. */
31571 static tree
31572 cp_parser_transaction_cancel (cp_parser *parser)
31574 cp_token *token;
31575 bool is_outer = false;
31576 tree stmt, attrs;
31578 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31579 RT_TRANSACTION_CANCEL);
31580 gcc_assert (token != NULL);
31582 attrs = cp_parser_txn_attribute_opt (parser);
31583 if (attrs)
31584 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31586 /* ??? Parse cancel-and-throw here. */
31588 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31590 if (!flag_tm)
31592 error_at (token->location, "%<__transaction_cancel%> without "
31593 "transactional memory support enabled");
31594 return error_mark_node;
31596 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31598 error_at (token->location, "%<__transaction_cancel%> within a "
31599 "%<__transaction_relaxed%>");
31600 return error_mark_node;
31602 else if (is_outer)
31604 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31605 && !is_tm_may_cancel_outer (current_function_decl))
31607 error_at (token->location, "outer %<__transaction_cancel%> not "
31608 "within outer %<__transaction_atomic%>");
31609 error_at (token->location,
31610 " or a %<transaction_may_cancel_outer%> function");
31611 return error_mark_node;
31614 else if (parser->in_transaction == 0)
31616 error_at (token->location, "%<__transaction_cancel%> not within "
31617 "%<__transaction_atomic%>");
31618 return error_mark_node;
31621 stmt = build_tm_abort_call (token->location, is_outer);
31622 add_stmt (stmt);
31624 return stmt;
31627 /* The parser. */
31629 static GTY (()) cp_parser *the_parser;
31632 /* Special handling for the first token or line in the file. The first
31633 thing in the file might be #pragma GCC pch_preprocess, which loads a
31634 PCH file, which is a GC collection point. So we need to handle this
31635 first pragma without benefit of an existing lexer structure.
31637 Always returns one token to the caller in *FIRST_TOKEN. This is
31638 either the true first token of the file, or the first token after
31639 the initial pragma. */
31641 static void
31642 cp_parser_initial_pragma (cp_token *first_token)
31644 tree name = NULL;
31646 cp_lexer_get_preprocessor_token (NULL, first_token);
31647 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31648 return;
31650 cp_lexer_get_preprocessor_token (NULL, first_token);
31651 if (first_token->type == CPP_STRING)
31653 name = first_token->u.value;
31655 cp_lexer_get_preprocessor_token (NULL, first_token);
31656 if (first_token->type != CPP_PRAGMA_EOL)
31657 error_at (first_token->location,
31658 "junk at end of %<#pragma GCC pch_preprocess%>");
31660 else
31661 error_at (first_token->location, "expected string literal");
31663 /* Skip to the end of the pragma. */
31664 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31665 cp_lexer_get_preprocessor_token (NULL, first_token);
31667 /* Now actually load the PCH file. */
31668 if (name)
31669 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31671 /* Read one more token to return to our caller. We have to do this
31672 after reading the PCH file in, since its pointers have to be
31673 live. */
31674 cp_lexer_get_preprocessor_token (NULL, first_token);
31677 /* Normal parsing of a pragma token. Here we can (and must) use the
31678 regular lexer. */
31680 static bool
31681 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31683 cp_token *pragma_tok;
31684 unsigned int id;
31686 pragma_tok = cp_lexer_consume_token (parser->lexer);
31687 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31688 parser->lexer->in_pragma = true;
31690 id = pragma_tok->pragma_kind;
31691 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31692 cp_ensure_no_omp_declare_simd (parser);
31693 switch (id)
31695 case PRAGMA_GCC_PCH_PREPROCESS:
31696 error_at (pragma_tok->location,
31697 "%<#pragma GCC pch_preprocess%> must be first");
31698 break;
31700 case PRAGMA_OMP_BARRIER:
31701 switch (context)
31703 case pragma_compound:
31704 cp_parser_omp_barrier (parser, pragma_tok);
31705 return false;
31706 case pragma_stmt:
31707 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31708 "used in compound statements");
31709 break;
31710 default:
31711 goto bad_stmt;
31713 break;
31715 case PRAGMA_OMP_FLUSH:
31716 switch (context)
31718 case pragma_compound:
31719 cp_parser_omp_flush (parser, pragma_tok);
31720 return false;
31721 case pragma_stmt:
31722 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31723 "used in compound statements");
31724 break;
31725 default:
31726 goto bad_stmt;
31728 break;
31730 case PRAGMA_OMP_TASKWAIT:
31731 switch (context)
31733 case pragma_compound:
31734 cp_parser_omp_taskwait (parser, pragma_tok);
31735 return false;
31736 case pragma_stmt:
31737 error_at (pragma_tok->location,
31738 "%<#pragma omp taskwait%> may only be "
31739 "used in compound statements");
31740 break;
31741 default:
31742 goto bad_stmt;
31744 break;
31746 case PRAGMA_OMP_TASKYIELD:
31747 switch (context)
31749 case pragma_compound:
31750 cp_parser_omp_taskyield (parser, pragma_tok);
31751 return false;
31752 case pragma_stmt:
31753 error_at (pragma_tok->location,
31754 "%<#pragma omp taskyield%> may only be "
31755 "used in compound statements");
31756 break;
31757 default:
31758 goto bad_stmt;
31760 break;
31762 case PRAGMA_OMP_CANCEL:
31763 switch (context)
31765 case pragma_compound:
31766 cp_parser_omp_cancel (parser, pragma_tok);
31767 return false;
31768 case pragma_stmt:
31769 error_at (pragma_tok->location,
31770 "%<#pragma omp cancel%> may only be "
31771 "used in compound statements");
31772 break;
31773 default:
31774 goto bad_stmt;
31776 break;
31778 case PRAGMA_OMP_CANCELLATION_POINT:
31779 switch (context)
31781 case pragma_compound:
31782 cp_parser_omp_cancellation_point (parser, pragma_tok);
31783 return false;
31784 case pragma_stmt:
31785 error_at (pragma_tok->location,
31786 "%<#pragma omp cancellation point%> may only be "
31787 "used in compound statements");
31788 break;
31789 default:
31790 goto bad_stmt;
31792 break;
31794 case PRAGMA_OMP_THREADPRIVATE:
31795 cp_parser_omp_threadprivate (parser, pragma_tok);
31796 return false;
31798 case PRAGMA_OMP_DECLARE_REDUCTION:
31799 cp_parser_omp_declare (parser, pragma_tok, context);
31800 return false;
31802 case PRAGMA_OMP_ATOMIC:
31803 case PRAGMA_OMP_CRITICAL:
31804 case PRAGMA_OMP_DISTRIBUTE:
31805 case PRAGMA_OMP_FOR:
31806 case PRAGMA_OMP_MASTER:
31807 case PRAGMA_OMP_ORDERED:
31808 case PRAGMA_OMP_PARALLEL:
31809 case PRAGMA_OMP_SECTIONS:
31810 case PRAGMA_OMP_SIMD:
31811 case PRAGMA_OMP_SINGLE:
31812 case PRAGMA_OMP_TASK:
31813 case PRAGMA_OMP_TASKGROUP:
31814 case PRAGMA_OMP_TEAMS:
31815 if (context != pragma_stmt && context != pragma_compound)
31816 goto bad_stmt;
31817 cp_parser_omp_construct (parser, pragma_tok);
31818 return true;
31820 case PRAGMA_OMP_TARGET:
31821 return cp_parser_omp_target (parser, pragma_tok, context);
31823 case PRAGMA_OMP_END_DECLARE_TARGET:
31824 cp_parser_omp_end_declare_target (parser, pragma_tok);
31825 return false;
31827 case PRAGMA_OMP_SECTION:
31828 error_at (pragma_tok->location,
31829 "%<#pragma omp section%> may only be used in "
31830 "%<#pragma omp sections%> construct");
31831 break;
31833 case PRAGMA_IVDEP:
31835 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31836 cp_token *tok;
31837 tok = cp_lexer_peek_token (the_parser->lexer);
31838 if (tok->type != CPP_KEYWORD
31839 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31840 && tok->keyword != RID_DO))
31842 cp_parser_error (parser, "for, while or do statement expected");
31843 return false;
31845 cp_parser_iteration_statement (parser, true);
31846 return true;
31849 case PRAGMA_CILK_SIMD:
31850 if (context == pragma_external)
31852 error_at (pragma_tok->location,
31853 "%<#pragma simd%> must be inside a function");
31854 break;
31856 cp_parser_cilk_simd (parser, pragma_tok);
31857 return true;
31859 default:
31860 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31861 c_invoke_pragma_handler (id);
31862 break;
31864 bad_stmt:
31865 cp_parser_error (parser, "expected declaration specifiers");
31866 break;
31869 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31870 return false;
31873 /* The interface the pragma parsers have to the lexer. */
31875 enum cpp_ttype
31876 pragma_lex (tree *value)
31878 cp_token *tok;
31879 enum cpp_ttype ret;
31881 tok = cp_lexer_peek_token (the_parser->lexer);
31883 ret = tok->type;
31884 *value = tok->u.value;
31886 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31887 ret = CPP_EOF;
31888 else if (ret == CPP_STRING)
31889 *value = cp_parser_string_literal (the_parser, false, false);
31890 else
31892 cp_lexer_consume_token (the_parser->lexer);
31893 if (ret == CPP_KEYWORD)
31894 ret = CPP_NAME;
31897 return ret;
31901 /* External interface. */
31903 /* Parse one entire translation unit. */
31905 void
31906 c_parse_file (void)
31908 static bool already_called = false;
31910 if (already_called)
31912 sorry ("inter-module optimizations not implemented for C++");
31913 return;
31915 already_called = true;
31917 the_parser = cp_parser_new ();
31918 push_deferring_access_checks (flag_access_control
31919 ? dk_no_deferred : dk_no_check);
31920 cp_parser_translation_unit (the_parser);
31921 the_parser = NULL;
31924 /* Parses the Cilk Plus #pragma simd vectorlength clause:
31925 Syntax:
31926 vectorlength ( constant-expression ) */
31928 static tree
31929 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses)
31931 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31932 tree expr;
31933 /* The vectorlength clause behaves exactly like OpenMP's safelen
31934 clause. Thus, vectorlength is represented as OMP 4.0
31935 safelen. */
31936 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", loc);
31938 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31939 return error_mark_node;
31941 expr = cp_parser_constant_expression (parser, false, NULL);
31942 expr = maybe_constant_value (expr);
31944 if (TREE_CONSTANT (expr)
31945 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31946 error_at (loc, "vectorlength must be a power of 2");
31947 else if (expr != error_mark_node)
31949 tree c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31950 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31951 OMP_CLAUSE_CHAIN (c) = clauses;
31952 clauses = c;
31955 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31956 return error_mark_node;
31957 return clauses;
31960 /* Handles the Cilk Plus #pragma simd linear clause.
31961 Syntax:
31962 linear ( simd-linear-variable-list )
31964 simd-linear-variable-list:
31965 simd-linear-variable
31966 simd-linear-variable-list , simd-linear-variable
31968 simd-linear-variable:
31969 id-expression
31970 id-expression : simd-linear-step
31972 simd-linear-step:
31973 conditional-expression */
31975 static tree
31976 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31978 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31980 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31981 return clauses;
31982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31984 cp_parser_error (parser, "expected identifier");
31985 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31986 return error_mark_node;
31989 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31990 parser->colon_corrects_to_scope_p = false;
31991 while (1)
31993 cp_token *token = cp_lexer_peek_token (parser->lexer);
31994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31996 cp_parser_error (parser, "expected variable-name");
31997 clauses = error_mark_node;
31998 break;
32001 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32002 false, false);
32003 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32004 token->location);
32005 if (decl == error_mark_node)
32007 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32008 token->location);
32009 clauses = error_mark_node;
32011 else
32013 tree e = NULL_TREE;
32014 tree step_size = integer_one_node;
32016 /* If present, parse the linear step. Otherwise, assume the default
32017 value of 1. */
32018 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32020 cp_lexer_consume_token (parser->lexer);
32022 e = cp_parser_assignment_expression (parser, false, NULL);
32023 e = maybe_constant_value (e);
32025 if (e == error_mark_node)
32027 /* If an error has occurred, then the whole pragma is
32028 considered ill-formed. Thus, no reason to keep
32029 parsing. */
32030 clauses = error_mark_node;
32031 break;
32033 else if (type_dependent_expression_p (e)
32034 || value_dependent_expression_p (e)
32035 || (TREE_TYPE (e)
32036 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32037 && (TREE_CONSTANT (e)
32038 || DECL_P (e))))
32039 step_size = e;
32040 else
32041 cp_parser_error (parser,
32042 "step size must be an integer constant "
32043 "expression or an integer variable");
32046 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32047 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32048 OMP_CLAUSE_DECL (l) = decl;
32049 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32050 OMP_CLAUSE_CHAIN (l) = clauses;
32051 clauses = l;
32053 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32054 cp_lexer_consume_token (parser->lexer);
32055 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32056 break;
32057 else
32059 error_at (cp_lexer_peek_token (parser->lexer)->location,
32060 "expected %<,%> or %<)%> after %qE", decl);
32061 clauses = error_mark_node;
32062 break;
32065 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32066 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32067 return clauses;
32070 /* Returns the name of the next clause. If the clause is not
32071 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32072 token is not consumed. Otherwise, the appropriate enum from the
32073 pragma_simd_clause is returned and the token is consumed. */
32075 static pragma_cilk_clause
32076 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32078 pragma_cilk_clause clause_type;
32079 cp_token *token = cp_lexer_peek_token (parser->lexer);
32081 if (token->keyword == RID_PRIVATE)
32082 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32083 else if (!token->u.value || token->type != CPP_NAME)
32084 return PRAGMA_CILK_CLAUSE_NONE;
32085 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32086 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32087 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32088 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32089 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32090 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32091 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32092 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32093 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32094 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32095 else
32096 return PRAGMA_CILK_CLAUSE_NONE;
32098 cp_lexer_consume_token (parser->lexer);
32099 return clause_type;
32102 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32104 static tree
32105 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32107 tree clauses = NULL_TREE;
32109 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32110 && clauses != error_mark_node)
32112 pragma_cilk_clause c_kind;
32113 c_kind = cp_parser_cilk_simd_clause_name (parser);
32114 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32115 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses);
32116 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32117 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32118 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32119 /* Use the OpenMP 4.0 equivalent function. */
32120 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32121 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32122 /* Use the OpenMP 4.0 equivalent function. */
32123 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32124 clauses);
32125 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32126 /* Use the OMP 4.0 equivalent function. */
32127 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32128 clauses);
32129 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32130 /* Use the OMP 4.0 equivalent function. */
32131 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32132 else
32134 clauses = error_mark_node;
32135 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32136 break;
32140 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32142 if (clauses == error_mark_node)
32143 return error_mark_node;
32144 else
32145 return c_finish_cilk_clauses (clauses);
32148 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32150 static void
32151 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32153 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32155 if (clauses == error_mark_node)
32156 return;
32158 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32160 error_at (cp_lexer_peek_token (parser->lexer)->location,
32161 "for statement expected");
32162 return;
32165 tree sb = begin_omp_structured_block ();
32166 int save = cp_parser_begin_omp_structured_block (parser);
32167 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32168 if (ret)
32169 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32170 cp_parser_end_omp_structured_block (parser, save);
32171 add_stmt (finish_omp_structured_block (sb));
32172 return;
32175 /* Create an identifier for a generic parameter type (a synthesized
32176 template parameter implied by `auto' or a concept identifier). */
32178 static GTY(()) int generic_parm_count;
32179 static tree
32180 make_generic_type_name ()
32182 char buf[32];
32183 sprintf (buf, "auto:%d", ++generic_parm_count);
32184 return get_identifier (buf);
32187 /* Predicate that behaves as is_auto_or_concept but matches the parent
32188 node of the generic type rather than the generic type itself. This
32189 allows for type transformation in add_implicit_template_parms. */
32191 static inline bool
32192 tree_type_is_auto_or_concept (const_tree t)
32194 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32197 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32198 (creating a new template parameter list if necessary). Returns the newly
32199 created template type parm. */
32201 tree
32202 synthesize_implicit_template_parm (cp_parser *parser)
32204 gcc_assert (current_binding_level->kind == sk_function_parms);
32206 /* We are either continuing a function template that already contains implicit
32207 template parameters, creating a new fully-implicit function template, or
32208 extending an existing explicit function template with implicit template
32209 parameters. */
32211 cp_binding_level *const entry_scope = current_binding_level;
32213 bool become_template = false;
32214 cp_binding_level *parent_scope = 0;
32216 if (parser->implicit_template_scope)
32218 gcc_assert (parser->implicit_template_parms);
32220 current_binding_level = parser->implicit_template_scope;
32222 else
32224 /* Roll back to the existing template parameter scope (in the case of
32225 extending an explicit function template) or introduce a new template
32226 parameter scope ahead of the function parameter scope (or class scope
32227 in the case of out-of-line member definitions). The function scope is
32228 added back after template parameter synthesis below. */
32230 cp_binding_level *scope = entry_scope;
32232 while (scope->kind == sk_function_parms)
32234 parent_scope = scope;
32235 scope = scope->level_chain;
32237 if (current_class_type && !LAMBDA_TYPE_P (current_class_type)
32238 && parser->num_classes_being_defined == 0)
32239 while (scope->kind == sk_class)
32241 parent_scope = scope;
32242 scope = scope->level_chain;
32245 current_binding_level = scope;
32247 if (scope->kind != sk_template_parms)
32249 /* Introduce a new template parameter list for implicit template
32250 parameters. */
32252 become_template = true;
32254 parser->implicit_template_scope
32255 = begin_scope (sk_template_parms, NULL);
32257 ++processing_template_decl;
32259 parser->fully_implicit_function_template_p = true;
32260 ++parser->num_template_parameter_lists;
32262 else
32264 /* Synthesize implicit template parameters at the end of the explicit
32265 template parameter list. */
32267 gcc_assert (current_template_parms);
32269 parser->implicit_template_scope = scope;
32271 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32272 parser->implicit_template_parms
32273 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32277 /* Synthesize a new template parameter and track the current template
32278 parameter chain with implicit_template_parms. */
32280 tree synth_id = make_generic_type_name ();
32281 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32282 synth_id);
32283 tree new_parm
32284 = process_template_parm (parser->implicit_template_parms,
32285 input_location,
32286 build_tree_list (NULL_TREE, synth_tmpl_parm),
32287 /*non_type=*/false,
32288 /*param_pack=*/false);
32291 if (parser->implicit_template_parms)
32292 parser->implicit_template_parms
32293 = TREE_CHAIN (parser->implicit_template_parms);
32294 else
32295 parser->implicit_template_parms = new_parm;
32297 tree new_type = TREE_TYPE (getdecls ());
32299 /* If creating a fully implicit function template, start the new implicit
32300 template parameter list with this synthesized type, otherwise grow the
32301 current template parameter list. */
32303 if (become_template)
32305 parent_scope->level_chain = current_binding_level;
32307 tree new_parms = make_tree_vec (1);
32308 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32309 current_template_parms = tree_cons (size_int (processing_template_decl),
32310 new_parms, current_template_parms);
32312 else
32314 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32315 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32316 new_parms = grow_tree_vec_stat (new_parms, new_parm_idx + 1);
32317 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32320 current_binding_level = entry_scope;
32322 return new_type;
32325 /* Finish the declaration of a fully implicit function template. Such a
32326 template has no explicit template parameter list so has not been through the
32327 normal template head and tail processing. synthesize_implicit_template_parm
32328 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32329 provided if the declaration is a class member such that its template
32330 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32331 form is returned. Otherwise NULL_TREE is returned. */
32333 tree
32334 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32336 gcc_assert (parser->fully_implicit_function_template_p);
32338 if (member_decl_opt && member_decl_opt != error_mark_node
32339 && DECL_VIRTUAL_P (member_decl_opt))
32341 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32342 "implicit templates may not be %<virtual%>");
32343 DECL_VIRTUAL_P (member_decl_opt) = false;
32346 if (member_decl_opt)
32347 member_decl_opt = finish_member_template_decl (member_decl_opt);
32348 end_template_decl ();
32350 parser->fully_implicit_function_template_p = false;
32351 --parser->num_template_parameter_lists;
32353 return member_decl_opt;
32356 #include "gt-cp-parser.h"