Reverting merge from trunk
[official-gcc.git] / gcc / cp / parser.c
blob6a03f6eb298a33f0f725292c421a818d319d985e
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 *, tree);
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, NULL_TREE);
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 // If the result is a TYPE_DECL, its DECL_NAME is the name of the
14951 // concept (without arguments), its TREE_TYPE refers to the type of the
14952 // first template parameter of concept definition (the prototype parameter),
14953 // its DECL_INITIAL is the declaration of the prototype parameter, and
14954 // its DECL_SIZE_UNIT is the constraining concept declaration.
14956 // TODO: A variable template may refer to a concept. The concept-name
14957 // could introduce a constrained placeholder type in the terse template
14958 // syntax.
14960 // FIXME: This shouldn't be here... or the synthesize_implicit_template_parm
14961 // should be more accessible.
14962 static tree
14963 finish_concept_name (cp_parser* parser, tree decl)
14965 gcc_assert (TREE_CODE (decl) == OVERLOAD);
14967 // Try to build a call expression that evaluates the concept. This
14968 // can fail if the overload set refers only to non-templates.
14969 tree call = build_concept_check (decl, build_nt(PLACEHOLDER_EXPR));
14970 if (call == error_mark_node)
14971 return NULL_TREE;
14973 // Resolve the constraint check to deduce the declared parameter.
14974 tree check = resolve_constraint_check (call);
14975 if (!check)
14976 return NULL_TREE;
14978 // Get function and argument from the resolved check expression. If
14979 // the argument was a pack expansion, then get the first element
14980 // of that pack.
14981 tree fn = TREE_VALUE (check);
14982 tree arg = TREE_VEC_ELT (TREE_PURPOSE (check), 0);
14983 if (ARGUMENT_PACK_P (arg))
14984 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
14986 // Get the protyping parameter bound to the placeholder.
14987 tree proto = TREE_TYPE (arg);
14989 // How we process the constrained declaration depends on the scope.
14990 // In template scope, we return a "description" that will later be
14991 // transformed into a real template parameter by process_template_parm.
14992 if (template_parm_scope_p ())
14993 return describe_template_parm (proto, fn);
14995 // If we can synthesize a template paramter, we should do that here.
14996 if (parser->auto_is_implicit_function_template_parm_p)
14998 tree x = describe_template_parm (proto, fn);
14999 tree r = synthesize_implicit_template_parm (parser, x);
15000 return r;
15003 return NULL_TREE;
15007 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
15008 or a concept-name.
15010 enum-name:
15011 identifier
15013 typedef-name:
15014 identifier
15016 concept-name:
15017 identifier
15019 Returns a TYPE_DECL for the type. */
15021 static tree
15022 cp_parser_nonclass_name (cp_parser* parser)
15024 tree type_decl;
15025 tree identifier;
15027 cp_token *token = cp_lexer_peek_token (parser->lexer);
15028 identifier = cp_parser_identifier (parser);
15029 if (identifier == error_mark_node)
15030 return error_mark_node;
15032 /* Look up the type-name. */
15033 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15035 // If we found an overload set, then it may refer to a concept-name.
15037 // TODO: The name could also refer to a variable template or an
15038 // introduction (if followed by '{').
15039 if (TREE_CODE (type_decl) == OVERLOAD)
15041 // Determine whether the overload refers to a concept.
15042 if (tree decl = finish_concept_name (parser, type_decl))
15043 return decl;
15046 if (TREE_CODE (type_decl) == USING_DECL)
15048 if (!DECL_DEPENDENT_P (type_decl))
15049 type_decl = strip_using_decl (type_decl);
15050 else if (USING_DECL_TYPENAME_P (type_decl))
15052 /* We have found a type introduced by a using
15053 declaration at class scope that refers to a dependent
15054 type.
15056 using typename :: [opt] nested-name-specifier unqualified-id ;
15058 type_decl = make_typename_type (TREE_TYPE (type_decl),
15059 DECL_NAME (type_decl),
15060 typename_type, tf_error);
15061 if (type_decl != error_mark_node)
15062 type_decl = TYPE_NAME (type_decl);
15066 if (TREE_CODE (type_decl) != TYPE_DECL
15067 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15069 /* See if this is an Objective-C type. */
15070 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15071 tree type = objc_get_protocol_qualified_type (identifier, protos);
15072 if (type)
15073 type_decl = TYPE_NAME (type);
15076 /* Issue an error if we did not find a type-name. */
15077 if (TREE_CODE (type_decl) != TYPE_DECL
15078 /* In Objective-C, we have the complication that class names are
15079 normally type names and start declarations (eg, the
15080 "NSObject" in "NSObject *object;"), but can be used in an
15081 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15082 is an expression. So, a classname followed by a dot is not a
15083 valid type-name. */
15084 || (objc_is_class_name (TREE_TYPE (type_decl))
15085 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15087 if (!cp_parser_simulate_error (parser))
15088 cp_parser_name_lookup_error (parser, identifier, type_decl,
15089 NLE_TYPE, token->location);
15090 return error_mark_node;
15092 /* Remember that the name was used in the definition of the
15093 current class so that we can check later to see if the
15094 meaning would have been different after the class was
15095 entirely defined. */
15096 else if (type_decl != error_mark_node
15097 && !parser->scope)
15098 maybe_note_name_used_in_class (identifier, type_decl);
15100 return type_decl;
15103 /* Parse an elaborated-type-specifier. Note that the grammar given
15104 here incorporates the resolution to DR68.
15106 elaborated-type-specifier:
15107 class-key :: [opt] nested-name-specifier [opt] identifier
15108 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15109 enum-key :: [opt] nested-name-specifier [opt] identifier
15110 typename :: [opt] nested-name-specifier identifier
15111 typename :: [opt] nested-name-specifier template [opt]
15112 template-id
15114 GNU extension:
15116 elaborated-type-specifier:
15117 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15118 class-key attributes :: [opt] nested-name-specifier [opt]
15119 template [opt] template-id
15120 enum attributes :: [opt] nested-name-specifier [opt] identifier
15122 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15123 declared `friend'. If IS_DECLARATION is TRUE, then this
15124 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15125 something is being declared.
15127 Returns the TYPE specified. */
15129 static tree
15130 cp_parser_elaborated_type_specifier (cp_parser* parser,
15131 bool is_friend,
15132 bool is_declaration)
15134 enum tag_types tag_type;
15135 tree identifier;
15136 tree type = NULL_TREE;
15137 tree attributes = NULL_TREE;
15138 tree globalscope;
15139 cp_token *token = NULL;
15141 /* See if we're looking at the `enum' keyword. */
15142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15144 /* Consume the `enum' token. */
15145 cp_lexer_consume_token (parser->lexer);
15146 /* Remember that it's an enumeration type. */
15147 tag_type = enum_type;
15148 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15149 enums) is used here. */
15150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15151 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15153 pedwarn (input_location, 0, "elaborated-type-specifier "
15154 "for a scoped enum must not use the %<%D%> keyword",
15155 cp_lexer_peek_token (parser->lexer)->u.value);
15156 /* Consume the `struct' or `class' and parse it anyway. */
15157 cp_lexer_consume_token (parser->lexer);
15159 /* Parse the attributes. */
15160 attributes = cp_parser_attributes_opt (parser);
15162 /* Or, it might be `typename'. */
15163 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15164 RID_TYPENAME))
15166 /* Consume the `typename' token. */
15167 cp_lexer_consume_token (parser->lexer);
15168 /* Remember that it's a `typename' type. */
15169 tag_type = typename_type;
15171 /* Otherwise it must be a class-key. */
15172 else
15174 tag_type = cp_parser_class_key (parser);
15175 if (tag_type == none_type)
15176 return error_mark_node;
15177 /* Parse the attributes. */
15178 attributes = cp_parser_attributes_opt (parser);
15181 /* Look for the `::' operator. */
15182 globalscope = cp_parser_global_scope_opt (parser,
15183 /*current_scope_valid_p=*/false);
15184 /* Look for the nested-name-specifier. */
15185 if (tag_type == typename_type && !globalscope)
15187 if (!cp_parser_nested_name_specifier (parser,
15188 /*typename_keyword_p=*/true,
15189 /*check_dependency_p=*/true,
15190 /*type_p=*/true,
15191 is_declaration))
15192 return error_mark_node;
15194 else
15195 /* Even though `typename' is not present, the proposed resolution
15196 to Core Issue 180 says that in `class A<T>::B', `B' should be
15197 considered a type-name, even if `A<T>' is dependent. */
15198 cp_parser_nested_name_specifier_opt (parser,
15199 /*typename_keyword_p=*/true,
15200 /*check_dependency_p=*/true,
15201 /*type_p=*/true,
15202 is_declaration);
15203 /* For everything but enumeration types, consider a template-id.
15204 For an enumeration type, consider only a plain identifier. */
15205 if (tag_type != enum_type)
15207 bool template_p = false;
15208 tree decl;
15210 /* Allow the `template' keyword. */
15211 template_p = cp_parser_optional_template_keyword (parser);
15212 /* If we didn't see `template', we don't know if there's a
15213 template-id or not. */
15214 if (!template_p)
15215 cp_parser_parse_tentatively (parser);
15216 /* Parse the template-id. */
15217 token = cp_lexer_peek_token (parser->lexer);
15218 decl = cp_parser_template_id (parser, template_p,
15219 /*check_dependency_p=*/true,
15220 tag_type,
15221 is_declaration);
15222 /* If we didn't find a template-id, look for an ordinary
15223 identifier. */
15224 if (!template_p && !cp_parser_parse_definitely (parser))
15226 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15227 in effect, then we must assume that, upon instantiation, the
15228 template will correspond to a class. */
15229 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15230 && tag_type == typename_type)
15231 type = make_typename_type (parser->scope, decl,
15232 typename_type,
15233 /*complain=*/tf_error);
15234 /* If the `typename' keyword is in effect and DECL is not a type
15235 decl, then type is non existent. */
15236 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15238 else if (TREE_CODE (decl) == TYPE_DECL)
15239 type = check_elaborated_type_specifier (tag_type, decl,
15240 /*allow_template_p=*/true);
15241 else if (decl == error_mark_node)
15242 type = error_mark_node;
15245 if (!type)
15247 token = cp_lexer_peek_token (parser->lexer);
15248 identifier = cp_parser_identifier (parser);
15250 if (identifier == error_mark_node)
15252 parser->scope = NULL_TREE;
15253 return error_mark_node;
15256 /* For a `typename', we needn't call xref_tag. */
15257 if (tag_type == typename_type
15258 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15259 return cp_parser_make_typename_type (parser, parser->scope,
15260 identifier,
15261 token->location);
15262 /* Look up a qualified name in the usual way. */
15263 if (parser->scope)
15265 tree decl;
15266 tree ambiguous_decls;
15268 decl = cp_parser_lookup_name (parser, identifier,
15269 tag_type,
15270 /*is_template=*/false,
15271 /*is_namespace=*/false,
15272 /*check_dependency=*/true,
15273 &ambiguous_decls,
15274 token->location);
15276 /* If the lookup was ambiguous, an error will already have been
15277 issued. */
15278 if (ambiguous_decls)
15279 return error_mark_node;
15281 /* If we are parsing friend declaration, DECL may be a
15282 TEMPLATE_DECL tree node here. However, we need to check
15283 whether this TEMPLATE_DECL results in valid code. Consider
15284 the following example:
15286 namespace N {
15287 template <class T> class C {};
15289 class X {
15290 template <class T> friend class N::C; // #1, valid code
15292 template <class T> class Y {
15293 friend class N::C; // #2, invalid code
15296 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15297 name lookup of `N::C'. We see that friend declaration must
15298 be template for the code to be valid. Note that
15299 processing_template_decl does not work here since it is
15300 always 1 for the above two cases. */
15302 decl = (cp_parser_maybe_treat_template_as_class
15303 (decl, /*tag_name_p=*/is_friend
15304 && parser->num_template_parameter_lists));
15306 if (TREE_CODE (decl) != TYPE_DECL)
15308 cp_parser_diagnose_invalid_type_name (parser,
15309 parser->scope,
15310 identifier,
15311 token->location);
15312 return error_mark_node;
15315 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15317 bool allow_template = (parser->num_template_parameter_lists
15318 || DECL_SELF_REFERENCE_P (decl));
15319 type = check_elaborated_type_specifier (tag_type, decl,
15320 allow_template);
15322 if (type == error_mark_node)
15323 return error_mark_node;
15326 /* Forward declarations of nested types, such as
15328 class C1::C2;
15329 class C1::C2::C3;
15331 are invalid unless all components preceding the final '::'
15332 are complete. If all enclosing types are complete, these
15333 declarations become merely pointless.
15335 Invalid forward declarations of nested types are errors
15336 caught elsewhere in parsing. Those that are pointless arrive
15337 here. */
15339 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15340 && !is_friend && !processing_explicit_instantiation)
15341 warning (0, "declaration %qD does not declare anything", decl);
15343 type = TREE_TYPE (decl);
15345 else
15347 /* An elaborated-type-specifier sometimes introduces a new type and
15348 sometimes names an existing type. Normally, the rule is that it
15349 introduces a new type only if there is not an existing type of
15350 the same name already in scope. For example, given:
15352 struct S {};
15353 void f() { struct S s; }
15355 the `struct S' in the body of `f' is the same `struct S' as in
15356 the global scope; the existing definition is used. However, if
15357 there were no global declaration, this would introduce a new
15358 local class named `S'.
15360 An exception to this rule applies to the following code:
15362 namespace N { struct S; }
15364 Here, the elaborated-type-specifier names a new type
15365 unconditionally; even if there is already an `S' in the
15366 containing scope this declaration names a new type.
15367 This exception only applies if the elaborated-type-specifier
15368 forms the complete declaration:
15370 [class.name]
15372 A declaration consisting solely of `class-key identifier ;' is
15373 either a redeclaration of the name in the current scope or a
15374 forward declaration of the identifier as a class name. It
15375 introduces the name into the current scope.
15377 We are in this situation precisely when the next token is a `;'.
15379 An exception to the exception is that a `friend' declaration does
15380 *not* name a new type; i.e., given:
15382 struct S { friend struct T; };
15384 `T' is not a new type in the scope of `S'.
15386 Also, `new struct S' or `sizeof (struct S)' never results in the
15387 definition of a new type; a new type can only be declared in a
15388 declaration context. */
15390 tag_scope ts;
15391 bool template_p;
15393 if (is_friend)
15394 /* Friends have special name lookup rules. */
15395 ts = ts_within_enclosing_non_class;
15396 else if (is_declaration
15397 && cp_lexer_next_token_is (parser->lexer,
15398 CPP_SEMICOLON))
15399 /* This is a `class-key identifier ;' */
15400 ts = ts_current;
15401 else
15402 ts = ts_global;
15404 template_p =
15405 (parser->num_template_parameter_lists
15406 && (cp_parser_next_token_starts_class_definition_p (parser)
15407 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15408 /* An unqualified name was used to reference this type, so
15409 there were no qualifying templates. */
15410 if (!cp_parser_check_template_parameters (parser,
15411 /*num_templates=*/0,
15412 token->location,
15413 /*declarator=*/NULL))
15414 return error_mark_node;
15415 type = xref_tag (tag_type, identifier, ts, template_p);
15419 if (type == error_mark_node)
15420 return error_mark_node;
15422 /* Allow attributes on forward declarations of classes. */
15423 if (attributes)
15425 if (TREE_CODE (type) == TYPENAME_TYPE)
15426 warning (OPT_Wattributes,
15427 "attributes ignored on uninstantiated type");
15428 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15429 && ! processing_explicit_instantiation)
15430 warning (OPT_Wattributes,
15431 "attributes ignored on template instantiation");
15432 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15433 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15434 else
15435 warning (OPT_Wattributes,
15436 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15439 if (tag_type != enum_type)
15441 /* Indicate whether this class was declared as a `class' or as a
15442 `struct'. */
15443 if (TREE_CODE (type) == RECORD_TYPE)
15444 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15445 cp_parser_check_class_key (tag_type, type);
15448 /* A "<" cannot follow an elaborated type specifier. If that
15449 happens, the user was probably trying to form a template-id. */
15450 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15451 token->location);
15453 return type;
15456 /* Parse an enum-specifier.
15458 enum-specifier:
15459 enum-head { enumerator-list [opt] }
15460 enum-head { enumerator-list , } [C++0x]
15462 enum-head:
15463 enum-key identifier [opt] enum-base [opt]
15464 enum-key nested-name-specifier identifier enum-base [opt]
15466 enum-key:
15467 enum
15468 enum class [C++0x]
15469 enum struct [C++0x]
15471 enum-base: [C++0x]
15472 : type-specifier-seq
15474 opaque-enum-specifier:
15475 enum-key identifier enum-base [opt] ;
15477 GNU Extensions:
15478 enum-key attributes[opt] identifier [opt] enum-base [opt]
15479 { enumerator-list [opt] }attributes[opt]
15480 enum-key attributes[opt] identifier [opt] enum-base [opt]
15481 { enumerator-list, }attributes[opt] [C++0x]
15483 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15484 if the token stream isn't an enum-specifier after all. */
15486 static tree
15487 cp_parser_enum_specifier (cp_parser* parser)
15489 tree identifier;
15490 tree type = NULL_TREE;
15491 tree prev_scope;
15492 tree nested_name_specifier = NULL_TREE;
15493 tree attributes;
15494 bool scoped_enum_p = false;
15495 bool has_underlying_type = false;
15496 bool nested_being_defined = false;
15497 bool new_value_list = false;
15498 bool is_new_type = false;
15499 bool is_anonymous = false;
15500 tree underlying_type = NULL_TREE;
15501 cp_token *type_start_token = NULL;
15502 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15504 parser->colon_corrects_to_scope_p = false;
15506 /* Parse tentatively so that we can back up if we don't find a
15507 enum-specifier. */
15508 cp_parser_parse_tentatively (parser);
15510 /* Caller guarantees that the current token is 'enum', an identifier
15511 possibly follows, and the token after that is an opening brace.
15512 If we don't have an identifier, fabricate an anonymous name for
15513 the enumeration being defined. */
15514 cp_lexer_consume_token (parser->lexer);
15516 /* Parse the "class" or "struct", which indicates a scoped
15517 enumeration type in C++0x. */
15518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15519 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15521 if (cxx_dialect < cxx11)
15522 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15524 /* Consume the `struct' or `class' token. */
15525 cp_lexer_consume_token (parser->lexer);
15527 scoped_enum_p = true;
15530 attributes = cp_parser_attributes_opt (parser);
15532 /* Clear the qualification. */
15533 parser->scope = NULL_TREE;
15534 parser->qualifying_scope = NULL_TREE;
15535 parser->object_scope = NULL_TREE;
15537 /* Figure out in what scope the declaration is being placed. */
15538 prev_scope = current_scope ();
15540 type_start_token = cp_lexer_peek_token (parser->lexer);
15542 push_deferring_access_checks (dk_no_check);
15543 nested_name_specifier
15544 = cp_parser_nested_name_specifier_opt (parser,
15545 /*typename_keyword_p=*/true,
15546 /*check_dependency_p=*/false,
15547 /*type_p=*/false,
15548 /*is_declaration=*/false);
15550 if (nested_name_specifier)
15552 tree name;
15554 identifier = cp_parser_identifier (parser);
15555 name = cp_parser_lookup_name (parser, identifier,
15556 enum_type,
15557 /*is_template=*/false,
15558 /*is_namespace=*/false,
15559 /*check_dependency=*/true,
15560 /*ambiguous_decls=*/NULL,
15561 input_location);
15562 if (name && name != error_mark_node)
15564 type = TREE_TYPE (name);
15565 if (TREE_CODE (type) == TYPENAME_TYPE)
15567 /* Are template enums allowed in ISO? */
15568 if (template_parm_scope_p ())
15569 pedwarn (type_start_token->location, OPT_Wpedantic,
15570 "%qD is an enumeration template", name);
15571 /* ignore a typename reference, for it will be solved by name
15572 in start_enum. */
15573 type = NULL_TREE;
15576 else if (nested_name_specifier == error_mark_node)
15577 /* We already issued an error. */;
15578 else
15579 error_at (type_start_token->location,
15580 "%qD is not an enumerator-name", identifier);
15582 else
15584 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15585 identifier = cp_parser_identifier (parser);
15586 else
15588 identifier = make_anon_name ();
15589 is_anonymous = true;
15590 if (scoped_enum_p)
15591 error_at (type_start_token->location,
15592 "anonymous scoped enum is not allowed");
15595 pop_deferring_access_checks ();
15597 /* Check for the `:' that denotes a specified underlying type in C++0x.
15598 Note that a ':' could also indicate a bitfield width, however. */
15599 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15601 cp_decl_specifier_seq type_specifiers;
15603 /* Consume the `:'. */
15604 cp_lexer_consume_token (parser->lexer);
15606 /* Parse the type-specifier-seq. */
15607 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15608 /*is_trailing_return=*/false,
15609 &type_specifiers);
15611 /* At this point this is surely not elaborated type specifier. */
15612 if (!cp_parser_parse_definitely (parser))
15613 return NULL_TREE;
15615 if (cxx_dialect < cxx11)
15616 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15618 has_underlying_type = true;
15620 /* If that didn't work, stop. */
15621 if (type_specifiers.type != error_mark_node)
15623 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15624 /*initialized=*/0, NULL);
15625 if (underlying_type == error_mark_node)
15626 underlying_type = NULL_TREE;
15630 /* Look for the `{' but don't consume it yet. */
15631 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15633 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15635 cp_parser_error (parser, "expected %<{%>");
15636 if (has_underlying_type)
15638 type = NULL_TREE;
15639 goto out;
15642 /* An opaque-enum-specifier must have a ';' here. */
15643 if ((scoped_enum_p || underlying_type)
15644 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15646 cp_parser_error (parser, "expected %<;%> or %<{%>");
15647 if (has_underlying_type)
15649 type = NULL_TREE;
15650 goto out;
15655 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15656 return NULL_TREE;
15658 if (nested_name_specifier)
15660 if (CLASS_TYPE_P (nested_name_specifier))
15662 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15663 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15664 push_scope (nested_name_specifier);
15666 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15668 push_nested_namespace (nested_name_specifier);
15672 /* Issue an error message if type-definitions are forbidden here. */
15673 if (!cp_parser_check_type_definition (parser))
15674 type = error_mark_node;
15675 else
15676 /* Create the new type. We do this before consuming the opening
15677 brace so the enum will be recorded as being on the line of its
15678 tag (or the 'enum' keyword, if there is no tag). */
15679 type = start_enum (identifier, type, underlying_type,
15680 scoped_enum_p, &is_new_type);
15682 /* If the next token is not '{' it is an opaque-enum-specifier or an
15683 elaborated-type-specifier. */
15684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15686 timevar_push (TV_PARSE_ENUM);
15687 if (nested_name_specifier
15688 && nested_name_specifier != error_mark_node)
15690 /* The following catches invalid code such as:
15691 enum class S<int>::E { A, B, C }; */
15692 if (!processing_specialization
15693 && CLASS_TYPE_P (nested_name_specifier)
15694 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15695 error_at (type_start_token->location, "cannot add an enumerator "
15696 "list to a template instantiation");
15698 /* If that scope does not contain the scope in which the
15699 class was originally declared, the program is invalid. */
15700 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
15702 if (at_namespace_scope_p ())
15703 error_at (type_start_token->location,
15704 "declaration of %qD in namespace %qD which does not "
15705 "enclose %qD",
15706 type, prev_scope, nested_name_specifier);
15707 else
15708 error_at (type_start_token->location,
15709 "declaration of %qD in %qD which does not enclose %qD",
15710 type, prev_scope, nested_name_specifier);
15711 type = error_mark_node;
15715 if (scoped_enum_p)
15716 begin_scope (sk_scoped_enum, type);
15718 /* Consume the opening brace. */
15719 cp_lexer_consume_token (parser->lexer);
15721 if (type == error_mark_node)
15722 ; /* Nothing to add */
15723 else if (OPAQUE_ENUM_P (type)
15724 || (cxx_dialect > cxx98 && processing_specialization))
15726 new_value_list = true;
15727 SET_OPAQUE_ENUM_P (type, false);
15728 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15730 else
15732 error_at (type_start_token->location, "multiple definition of %q#T", type);
15733 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15734 "previous definition here");
15735 type = error_mark_node;
15738 if (type == error_mark_node)
15739 cp_parser_skip_to_end_of_block_or_statement (parser);
15740 /* If the next token is not '}', then there are some enumerators. */
15741 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15743 if (is_anonymous && !scoped_enum_p)
15744 pedwarn (type_start_token->location, OPT_Wpedantic,
15745 "ISO C++ forbids empty anonymous enum");
15747 else
15748 cp_parser_enumerator_list (parser, type);
15750 /* Consume the final '}'. */
15751 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15753 if (scoped_enum_p)
15754 finish_scope ();
15755 timevar_pop (TV_PARSE_ENUM);
15757 else
15759 /* If a ';' follows, then it is an opaque-enum-specifier
15760 and additional restrictions apply. */
15761 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15763 if (is_anonymous)
15764 error_at (type_start_token->location,
15765 "opaque-enum-specifier without name");
15766 else if (nested_name_specifier)
15767 error_at (type_start_token->location,
15768 "opaque-enum-specifier must use a simple identifier");
15772 /* Look for trailing attributes to apply to this enumeration, and
15773 apply them if appropriate. */
15774 if (cp_parser_allow_gnu_extensions_p (parser))
15776 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15777 trailing_attr = chainon (trailing_attr, attributes);
15778 cplus_decl_attributes (&type,
15779 trailing_attr,
15780 (int) ATTR_FLAG_TYPE_IN_PLACE);
15783 /* Finish up the enumeration. */
15784 if (type != error_mark_node)
15786 if (new_value_list)
15787 finish_enum_value_list (type);
15788 if (is_new_type)
15789 finish_enum (type);
15792 if (nested_name_specifier)
15794 if (CLASS_TYPE_P (nested_name_specifier))
15796 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15797 pop_scope (nested_name_specifier);
15799 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15801 pop_nested_namespace (nested_name_specifier);
15804 out:
15805 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15806 return type;
15809 /* Parse an enumerator-list. The enumerators all have the indicated
15810 TYPE.
15812 enumerator-list:
15813 enumerator-definition
15814 enumerator-list , enumerator-definition */
15816 static void
15817 cp_parser_enumerator_list (cp_parser* parser, tree type)
15819 while (true)
15821 /* Parse an enumerator-definition. */
15822 cp_parser_enumerator_definition (parser, type);
15824 /* If the next token is not a ',', we've reached the end of
15825 the list. */
15826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15827 break;
15828 /* Otherwise, consume the `,' and keep going. */
15829 cp_lexer_consume_token (parser->lexer);
15830 /* If the next token is a `}', there is a trailing comma. */
15831 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15833 if (cxx_dialect < cxx11 && !in_system_header)
15834 pedwarn (input_location, OPT_Wpedantic,
15835 "comma at end of enumerator list");
15836 break;
15841 /* Parse an enumerator-definition. The enumerator has the indicated
15842 TYPE.
15844 enumerator-definition:
15845 enumerator
15846 enumerator = constant-expression
15848 enumerator:
15849 identifier */
15851 static void
15852 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15854 tree identifier;
15855 tree value;
15856 location_t loc;
15858 /* Save the input location because we are interested in the location
15859 of the identifier and not the location of the explicit value. */
15860 loc = cp_lexer_peek_token (parser->lexer)->location;
15862 /* Look for the identifier. */
15863 identifier = cp_parser_identifier (parser);
15864 if (identifier == error_mark_node)
15865 return;
15867 /* If the next token is an '=', then there is an explicit value. */
15868 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15870 /* Consume the `=' token. */
15871 cp_lexer_consume_token (parser->lexer);
15872 /* Parse the value. */
15873 value = cp_parser_constant_expression (parser,
15874 /*allow_non_constant_p=*/false,
15875 NULL);
15877 else
15878 value = NULL_TREE;
15880 /* If we are processing a template, make sure the initializer of the
15881 enumerator doesn't contain any bare template parameter pack. */
15882 if (check_for_bare_parameter_packs (value))
15883 value = error_mark_node;
15885 /* integral_constant_value will pull out this expression, so make sure
15886 it's folded as appropriate. */
15887 value = fold_non_dependent_expr (value);
15889 /* Create the enumerator. */
15890 build_enumerator (identifier, value, type, loc);
15893 /* Parse a namespace-name.
15895 namespace-name:
15896 original-namespace-name
15897 namespace-alias
15899 Returns the NAMESPACE_DECL for the namespace. */
15901 static tree
15902 cp_parser_namespace_name (cp_parser* parser)
15904 tree identifier;
15905 tree namespace_decl;
15907 cp_token *token = cp_lexer_peek_token (parser->lexer);
15909 /* Get the name of the namespace. */
15910 identifier = cp_parser_identifier (parser);
15911 if (identifier == error_mark_node)
15912 return error_mark_node;
15914 /* Look up the identifier in the currently active scope. Look only
15915 for namespaces, due to:
15917 [basic.lookup.udir]
15919 When looking up a namespace-name in a using-directive or alias
15920 definition, only namespace names are considered.
15922 And:
15924 [basic.lookup.qual]
15926 During the lookup of a name preceding the :: scope resolution
15927 operator, object, function, and enumerator names are ignored.
15929 (Note that cp_parser_qualifying_entity only calls this
15930 function if the token after the name is the scope resolution
15931 operator.) */
15932 namespace_decl = cp_parser_lookup_name (parser, identifier,
15933 none_type,
15934 /*is_template=*/false,
15935 /*is_namespace=*/true,
15936 /*check_dependency=*/true,
15937 /*ambiguous_decls=*/NULL,
15938 token->location);
15939 /* If it's not a namespace, issue an error. */
15940 if (namespace_decl == error_mark_node
15941 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15943 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15944 error_at (token->location, "%qD is not a namespace-name", identifier);
15945 cp_parser_error (parser, "expected namespace-name");
15946 namespace_decl = error_mark_node;
15949 return namespace_decl;
15952 /* Parse a namespace-definition.
15954 namespace-definition:
15955 named-namespace-definition
15956 unnamed-namespace-definition
15958 named-namespace-definition:
15959 original-namespace-definition
15960 extension-namespace-definition
15962 original-namespace-definition:
15963 namespace identifier { namespace-body }
15965 extension-namespace-definition:
15966 namespace original-namespace-name { namespace-body }
15968 unnamed-namespace-definition:
15969 namespace { namespace-body } */
15971 static void
15972 cp_parser_namespace_definition (cp_parser* parser)
15974 tree identifier, attribs;
15975 bool has_visibility;
15976 bool is_inline;
15978 cp_ensure_no_omp_declare_simd (parser);
15979 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15981 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15982 is_inline = true;
15983 cp_lexer_consume_token (parser->lexer);
15985 else
15986 is_inline = false;
15988 /* Look for the `namespace' keyword. */
15989 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15991 /* Get the name of the namespace. We do not attempt to distinguish
15992 between an original-namespace-definition and an
15993 extension-namespace-definition at this point. The semantic
15994 analysis routines are responsible for that. */
15995 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15996 identifier = cp_parser_identifier (parser);
15997 else
15998 identifier = NULL_TREE;
16000 /* Parse any specified attributes. */
16001 attribs = cp_parser_attributes_opt (parser);
16003 /* Look for the `{' to start the namespace. */
16004 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16005 /* Start the namespace. */
16006 push_namespace (identifier);
16008 /* "inline namespace" is equivalent to a stub namespace definition
16009 followed by a strong using directive. */
16010 if (is_inline)
16012 tree name_space = current_namespace;
16013 /* Set up namespace association. */
16014 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16015 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16016 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16017 /* Import the contents of the inline namespace. */
16018 pop_namespace ();
16019 do_using_directive (name_space);
16020 push_namespace (identifier);
16023 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16025 /* Parse the body of the namespace. */
16026 cp_parser_namespace_body (parser);
16028 if (has_visibility)
16029 pop_visibility (1);
16031 /* Finish the namespace. */
16032 pop_namespace ();
16033 /* Look for the final `}'. */
16034 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16037 /* Parse a namespace-body.
16039 namespace-body:
16040 declaration-seq [opt] */
16042 static void
16043 cp_parser_namespace_body (cp_parser* parser)
16045 cp_parser_declaration_seq_opt (parser);
16048 /* Parse a namespace-alias-definition.
16050 namespace-alias-definition:
16051 namespace identifier = qualified-namespace-specifier ; */
16053 static void
16054 cp_parser_namespace_alias_definition (cp_parser* parser)
16056 tree identifier;
16057 tree namespace_specifier;
16059 cp_token *token = cp_lexer_peek_token (parser->lexer);
16061 /* Look for the `namespace' keyword. */
16062 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16063 /* Look for the identifier. */
16064 identifier = cp_parser_identifier (parser);
16065 if (identifier == error_mark_node)
16066 return;
16067 /* Look for the `=' token. */
16068 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16069 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16071 error_at (token->location, "%<namespace%> definition is not allowed here");
16072 /* Skip the definition. */
16073 cp_lexer_consume_token (parser->lexer);
16074 if (cp_parser_skip_to_closing_brace (parser))
16075 cp_lexer_consume_token (parser->lexer);
16076 return;
16078 cp_parser_require (parser, CPP_EQ, RT_EQ);
16079 /* Look for the qualified-namespace-specifier. */
16080 namespace_specifier
16081 = cp_parser_qualified_namespace_specifier (parser);
16082 /* Look for the `;' token. */
16083 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16085 /* Register the alias in the symbol table. */
16086 do_namespace_alias (identifier, namespace_specifier);
16089 /* Parse a qualified-namespace-specifier.
16091 qualified-namespace-specifier:
16092 :: [opt] nested-name-specifier [opt] namespace-name
16094 Returns a NAMESPACE_DECL corresponding to the specified
16095 namespace. */
16097 static tree
16098 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16100 /* Look for the optional `::'. */
16101 cp_parser_global_scope_opt (parser,
16102 /*current_scope_valid_p=*/false);
16104 /* Look for the optional nested-name-specifier. */
16105 cp_parser_nested_name_specifier_opt (parser,
16106 /*typename_keyword_p=*/false,
16107 /*check_dependency_p=*/true,
16108 /*type_p=*/false,
16109 /*is_declaration=*/true);
16111 return cp_parser_namespace_name (parser);
16114 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16115 access declaration.
16117 using-declaration:
16118 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16119 using :: unqualified-id ;
16121 access-declaration:
16122 qualified-id ;
16126 static bool
16127 cp_parser_using_declaration (cp_parser* parser,
16128 bool access_declaration_p)
16130 cp_token *token;
16131 bool typename_p = false;
16132 bool global_scope_p;
16133 tree decl;
16134 tree identifier;
16135 tree qscope;
16136 int oldcount = errorcount;
16137 cp_token *diag_token = NULL;
16139 if (access_declaration_p)
16141 diag_token = cp_lexer_peek_token (parser->lexer);
16142 cp_parser_parse_tentatively (parser);
16144 else
16146 /* Look for the `using' keyword. */
16147 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16149 /* Peek at the next token. */
16150 token = cp_lexer_peek_token (parser->lexer);
16151 /* See if it's `typename'. */
16152 if (token->keyword == RID_TYPENAME)
16154 /* Remember that we've seen it. */
16155 typename_p = true;
16156 /* Consume the `typename' token. */
16157 cp_lexer_consume_token (parser->lexer);
16161 /* Look for the optional global scope qualification. */
16162 global_scope_p
16163 = (cp_parser_global_scope_opt (parser,
16164 /*current_scope_valid_p=*/false)
16165 != NULL_TREE);
16167 /* If we saw `typename', or didn't see `::', then there must be a
16168 nested-name-specifier present. */
16169 if (typename_p || !global_scope_p)
16170 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16171 /*check_dependency_p=*/true,
16172 /*type_p=*/false,
16173 /*is_declaration=*/true);
16174 /* Otherwise, we could be in either of the two productions. In that
16175 case, treat the nested-name-specifier as optional. */
16176 else
16177 qscope = cp_parser_nested_name_specifier_opt (parser,
16178 /*typename_keyword_p=*/false,
16179 /*check_dependency_p=*/true,
16180 /*type_p=*/false,
16181 /*is_declaration=*/true);
16182 if (!qscope)
16183 qscope = global_namespace;
16185 if (access_declaration_p && cp_parser_error_occurred (parser))
16186 /* Something has already gone wrong; there's no need to parse
16187 further. Since an error has occurred, the return value of
16188 cp_parser_parse_definitely will be false, as required. */
16189 return cp_parser_parse_definitely (parser);
16191 token = cp_lexer_peek_token (parser->lexer);
16192 /* Parse the unqualified-id. */
16193 identifier = cp_parser_unqualified_id (parser,
16194 /*template_keyword_p=*/false,
16195 /*check_dependency_p=*/true,
16196 /*declarator_p=*/true,
16197 /*optional_p=*/false);
16199 if (access_declaration_p)
16201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16202 cp_parser_simulate_error (parser);
16203 if (!cp_parser_parse_definitely (parser))
16204 return false;
16207 /* The function we call to handle a using-declaration is different
16208 depending on what scope we are in. */
16209 if (qscope == error_mark_node || identifier == error_mark_node)
16211 else if (!identifier_p (identifier)
16212 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16213 /* [namespace.udecl]
16215 A using declaration shall not name a template-id. */
16216 error_at (token->location,
16217 "a template-id may not appear in a using-declaration");
16218 else
16220 if (at_class_scope_p ())
16222 /* Create the USING_DECL. */
16223 decl = do_class_using_decl (parser->scope, identifier);
16225 if (decl && typename_p)
16226 USING_DECL_TYPENAME_P (decl) = 1;
16228 if (check_for_bare_parameter_packs (decl))
16229 return false;
16230 else
16231 /* Add it to the list of members in this class. */
16232 finish_member_declaration (decl);
16234 else
16236 decl = cp_parser_lookup_name_simple (parser,
16237 identifier,
16238 token->location);
16239 if (decl == error_mark_node)
16240 cp_parser_name_lookup_error (parser, identifier,
16241 decl, NLE_NULL,
16242 token->location);
16243 else if (check_for_bare_parameter_packs (decl))
16244 return false;
16245 else if (!at_namespace_scope_p ())
16246 do_local_using_decl (decl, qscope, identifier);
16247 else
16248 do_toplevel_using_decl (decl, qscope, identifier);
16252 /* Look for the final `;'. */
16253 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16255 if (access_declaration_p && errorcount == oldcount)
16256 warning_at (diag_token->location, OPT_Wdeprecated,
16257 "access declarations are deprecated "
16258 "in favour of using-declarations; "
16259 "suggestion: add the %<using%> keyword");
16261 return true;
16264 /* Parse an alias-declaration.
16266 alias-declaration:
16267 using identifier attribute-specifier-seq [opt] = type-id */
16269 static tree
16270 cp_parser_alias_declaration (cp_parser* parser)
16272 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16273 location_t id_location;
16274 cp_declarator *declarator;
16275 cp_decl_specifier_seq decl_specs;
16276 bool member_p;
16277 const char *saved_message = NULL;
16279 /* Look for the `using' keyword. */
16280 cp_token *using_token
16281 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16282 if (using_token == NULL)
16283 return error_mark_node;
16285 id_location = cp_lexer_peek_token (parser->lexer)->location;
16286 id = cp_parser_identifier (parser);
16287 if (id == error_mark_node)
16288 return error_mark_node;
16290 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16291 attributes = cp_parser_attributes_opt (parser);
16292 if (attributes == error_mark_node)
16293 return error_mark_node;
16295 cp_parser_require (parser, CPP_EQ, RT_EQ);
16297 if (cp_parser_error_occurred (parser))
16298 return error_mark_node;
16300 cp_parser_commit_to_tentative_parse (parser);
16302 /* Now we are going to parse the type-id of the declaration. */
16305 [dcl.type]/3 says:
16307 "A type-specifier-seq shall not define a class or enumeration
16308 unless it appears in the type-id of an alias-declaration (7.1.3) that
16309 is not the declaration of a template-declaration."
16311 In other words, if we currently are in an alias template, the
16312 type-id should not define a type.
16314 So let's set parser->type_definition_forbidden_message in that
16315 case; cp_parser_check_type_definition (called by
16316 cp_parser_class_specifier) will then emit an error if a type is
16317 defined in the type-id. */
16318 if (parser->num_template_parameter_lists)
16320 saved_message = parser->type_definition_forbidden_message;
16321 parser->type_definition_forbidden_message =
16322 G_("types may not be defined in alias template declarations");
16325 type = cp_parser_type_id (parser);
16327 /* Restore the error message if need be. */
16328 if (parser->num_template_parameter_lists)
16329 parser->type_definition_forbidden_message = saved_message;
16331 if (type == error_mark_node)
16333 cp_parser_skip_to_end_of_block_or_statement (parser);
16334 return error_mark_node;
16337 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16339 if (cp_parser_error_occurred (parser))
16341 cp_parser_skip_to_end_of_block_or_statement (parser);
16342 return error_mark_node;
16345 /* A typedef-name can also be introduced by an alias-declaration. The
16346 identifier following the using keyword becomes a typedef-name. It has
16347 the same semantics as if it were introduced by the typedef
16348 specifier. In particular, it does not define a new type and it shall
16349 not appear in the type-id. */
16351 clear_decl_specs (&decl_specs);
16352 decl_specs.type = type;
16353 if (attributes != NULL_TREE)
16355 decl_specs.attributes = attributes;
16356 set_and_check_decl_spec_loc (&decl_specs,
16357 ds_attribute,
16358 attrs_token);
16360 set_and_check_decl_spec_loc (&decl_specs,
16361 ds_typedef,
16362 using_token);
16363 set_and_check_decl_spec_loc (&decl_specs,
16364 ds_alias,
16365 using_token);
16367 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16368 declarator->id_loc = id_location;
16370 member_p = at_class_scope_p ();
16371 if (member_p)
16372 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16373 NULL_TREE, attributes);
16374 else
16375 decl = start_decl (declarator, &decl_specs, 0,
16376 attributes, NULL_TREE, &pushed_scope);
16377 if (decl == error_mark_node)
16378 return decl;
16380 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16382 if (pushed_scope)
16383 pop_scope (pushed_scope);
16385 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16386 added into the symbol table; otherwise, return the TYPE_DECL. */
16387 if (DECL_LANG_SPECIFIC (decl)
16388 && DECL_TEMPLATE_INFO (decl)
16389 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16391 decl = DECL_TI_TEMPLATE (decl);
16392 if (member_p)
16393 check_member_template (decl);
16396 return decl;
16399 /* Parse a using-directive.
16401 using-directive:
16402 using namespace :: [opt] nested-name-specifier [opt]
16403 namespace-name ; */
16405 static void
16406 cp_parser_using_directive (cp_parser* parser)
16408 tree namespace_decl;
16409 tree attribs;
16411 /* Look for the `using' keyword. */
16412 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16413 /* And the `namespace' keyword. */
16414 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16415 /* Look for the optional `::' operator. */
16416 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16417 /* And the optional nested-name-specifier. */
16418 cp_parser_nested_name_specifier_opt (parser,
16419 /*typename_keyword_p=*/false,
16420 /*check_dependency_p=*/true,
16421 /*type_p=*/false,
16422 /*is_declaration=*/true);
16423 /* Get the namespace being used. */
16424 namespace_decl = cp_parser_namespace_name (parser);
16425 /* And any specified attributes. */
16426 attribs = cp_parser_attributes_opt (parser);
16427 /* Update the symbol table. */
16428 parse_using_directive (namespace_decl, attribs);
16429 /* Look for the final `;'. */
16430 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16433 /* Parse an asm-definition.
16435 asm-definition:
16436 asm ( string-literal ) ;
16438 GNU Extension:
16440 asm-definition:
16441 asm volatile [opt] ( string-literal ) ;
16442 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16443 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16444 : asm-operand-list [opt] ) ;
16445 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16446 : asm-operand-list [opt]
16447 : asm-clobber-list [opt] ) ;
16448 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16449 : asm-clobber-list [opt]
16450 : asm-goto-list ) ; */
16452 static void
16453 cp_parser_asm_definition (cp_parser* parser)
16455 tree string;
16456 tree outputs = NULL_TREE;
16457 tree inputs = NULL_TREE;
16458 tree clobbers = NULL_TREE;
16459 tree labels = NULL_TREE;
16460 tree asm_stmt;
16461 bool volatile_p = false;
16462 bool extended_p = false;
16463 bool invalid_inputs_p = false;
16464 bool invalid_outputs_p = false;
16465 bool goto_p = false;
16466 required_token missing = RT_NONE;
16468 /* Look for the `asm' keyword. */
16469 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16470 /* See if the next token is `volatile'. */
16471 if (cp_parser_allow_gnu_extensions_p (parser)
16472 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16474 /* Remember that we saw the `volatile' keyword. */
16475 volatile_p = true;
16476 /* Consume the token. */
16477 cp_lexer_consume_token (parser->lexer);
16479 if (cp_parser_allow_gnu_extensions_p (parser)
16480 && parser->in_function_body
16481 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16483 /* Remember that we saw the `goto' keyword. */
16484 goto_p = true;
16485 /* Consume the token. */
16486 cp_lexer_consume_token (parser->lexer);
16488 /* Look for the opening `('. */
16489 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16490 return;
16491 /* Look for the string. */
16492 string = cp_parser_string_literal (parser, false, false);
16493 if (string == error_mark_node)
16495 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16496 /*consume_paren=*/true);
16497 return;
16500 /* If we're allowing GNU extensions, check for the extended assembly
16501 syntax. Unfortunately, the `:' tokens need not be separated by
16502 a space in C, and so, for compatibility, we tolerate that here
16503 too. Doing that means that we have to treat the `::' operator as
16504 two `:' tokens. */
16505 if (cp_parser_allow_gnu_extensions_p (parser)
16506 && parser->in_function_body
16507 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16508 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16510 bool inputs_p = false;
16511 bool clobbers_p = false;
16512 bool labels_p = false;
16514 /* The extended syntax was used. */
16515 extended_p = true;
16517 /* Look for outputs. */
16518 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16520 /* Consume the `:'. */
16521 cp_lexer_consume_token (parser->lexer);
16522 /* Parse the output-operands. */
16523 if (cp_lexer_next_token_is_not (parser->lexer,
16524 CPP_COLON)
16525 && cp_lexer_next_token_is_not (parser->lexer,
16526 CPP_SCOPE)
16527 && cp_lexer_next_token_is_not (parser->lexer,
16528 CPP_CLOSE_PAREN)
16529 && !goto_p)
16530 outputs = cp_parser_asm_operand_list (parser);
16532 if (outputs == error_mark_node)
16533 invalid_outputs_p = true;
16535 /* If the next token is `::', there are no outputs, and the
16536 next token is the beginning of the inputs. */
16537 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16538 /* The inputs are coming next. */
16539 inputs_p = true;
16541 /* Look for inputs. */
16542 if (inputs_p
16543 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16545 /* Consume the `:' or `::'. */
16546 cp_lexer_consume_token (parser->lexer);
16547 /* Parse the output-operands. */
16548 if (cp_lexer_next_token_is_not (parser->lexer,
16549 CPP_COLON)
16550 && cp_lexer_next_token_is_not (parser->lexer,
16551 CPP_SCOPE)
16552 && cp_lexer_next_token_is_not (parser->lexer,
16553 CPP_CLOSE_PAREN))
16554 inputs = cp_parser_asm_operand_list (parser);
16556 if (inputs == error_mark_node)
16557 invalid_inputs_p = true;
16559 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16560 /* The clobbers are coming next. */
16561 clobbers_p = true;
16563 /* Look for clobbers. */
16564 if (clobbers_p
16565 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16567 clobbers_p = true;
16568 /* Consume the `:' or `::'. */
16569 cp_lexer_consume_token (parser->lexer);
16570 /* Parse the clobbers. */
16571 if (cp_lexer_next_token_is_not (parser->lexer,
16572 CPP_COLON)
16573 && cp_lexer_next_token_is_not (parser->lexer,
16574 CPP_CLOSE_PAREN))
16575 clobbers = cp_parser_asm_clobber_list (parser);
16577 else if (goto_p
16578 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16579 /* The labels are coming next. */
16580 labels_p = true;
16582 /* Look for labels. */
16583 if (labels_p
16584 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16586 labels_p = true;
16587 /* Consume the `:' or `::'. */
16588 cp_lexer_consume_token (parser->lexer);
16589 /* Parse the labels. */
16590 labels = cp_parser_asm_label_list (parser);
16593 if (goto_p && !labels_p)
16594 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16596 else if (goto_p)
16597 missing = RT_COLON_SCOPE;
16599 /* Look for the closing `)'. */
16600 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16601 missing ? missing : RT_CLOSE_PAREN))
16602 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16603 /*consume_paren=*/true);
16604 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16606 if (!invalid_inputs_p && !invalid_outputs_p)
16608 /* Create the ASM_EXPR. */
16609 if (parser->in_function_body)
16611 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16612 inputs, clobbers, labels);
16613 /* If the extended syntax was not used, mark the ASM_EXPR. */
16614 if (!extended_p)
16616 tree temp = asm_stmt;
16617 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16618 temp = TREE_OPERAND (temp, 0);
16620 ASM_INPUT_P (temp) = 1;
16623 else
16624 add_asm_node (string);
16628 /* Declarators [gram.dcl.decl] */
16630 /* Parse an init-declarator.
16632 init-declarator:
16633 declarator initializer [opt]
16635 GNU Extension:
16637 init-declarator:
16638 declarator asm-specification [opt] attributes [opt] initializer [opt]
16640 function-definition:
16641 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16642 function-body
16643 decl-specifier-seq [opt] declarator function-try-block
16645 GNU Extension:
16647 function-definition:
16648 __extension__ function-definition
16650 TM Extension:
16652 function-definition:
16653 decl-specifier-seq [opt] declarator function-transaction-block
16655 The DECL_SPECIFIERS apply to this declarator. Returns a
16656 representation of the entity declared. If MEMBER_P is TRUE, then
16657 this declarator appears in a class scope. The new DECL created by
16658 this declarator is returned.
16660 The CHECKS are access checks that should be performed once we know
16661 what entity is being declared (and, therefore, what classes have
16662 befriended it).
16664 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16665 for a function-definition here as well. If the declarator is a
16666 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16667 be TRUE upon return. By that point, the function-definition will
16668 have been completely parsed.
16670 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16671 is FALSE.
16673 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16674 parsed declaration if it is an uninitialized single declarator not followed
16675 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16676 if present, will not be consumed. If returned, this declarator will be
16677 created with SD_INITIALIZED but will not call cp_finish_decl. */
16679 static tree
16680 cp_parser_init_declarator (cp_parser* parser,
16681 cp_decl_specifier_seq *decl_specifiers,
16682 vec<deferred_access_check, va_gc> *checks,
16683 bool function_definition_allowed_p,
16684 bool member_p,
16685 int declares_class_or_enum,
16686 bool* function_definition_p,
16687 tree* maybe_range_for_decl)
16689 cp_token *token = NULL, *asm_spec_start_token = NULL,
16690 *attributes_start_token = NULL;
16691 cp_declarator *declarator;
16692 tree prefix_attributes;
16693 tree attributes = NULL;
16694 tree asm_specification;
16695 tree initializer;
16696 tree decl = NULL_TREE;
16697 tree scope;
16698 int is_initialized;
16699 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16700 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16701 "(...)". */
16702 enum cpp_ttype initialization_kind;
16703 bool is_direct_init = false;
16704 bool is_non_constant_init;
16705 int ctor_dtor_or_conv_p;
16706 bool friend_p;
16707 tree pushed_scope = NULL_TREE;
16708 bool range_for_decl_p = false;
16709 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16711 /* Gather the attributes that were provided with the
16712 decl-specifiers. */
16713 prefix_attributes = decl_specifiers->attributes;
16715 /* Assume that this is not the declarator for a function
16716 definition. */
16717 if (function_definition_p)
16718 *function_definition_p = false;
16720 /* Default arguments are only permitted for function parameters. */
16721 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16722 parser->default_arg_ok_p = false;
16724 /* Defer access checks while parsing the declarator; we cannot know
16725 what names are accessible until we know what is being
16726 declared. */
16727 resume_deferring_access_checks ();
16729 /* Parse the declarator. */
16730 token = cp_lexer_peek_token (parser->lexer);
16731 declarator
16732 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16733 &ctor_dtor_or_conv_p,
16734 /*parenthesized_p=*/NULL,
16735 member_p);
16736 /* Gather up the deferred checks. */
16737 stop_deferring_access_checks ();
16739 parser->default_arg_ok_p = saved_default_arg_ok_p;
16741 /* If the DECLARATOR was erroneous, there's no need to go
16742 further. */
16743 if (declarator == cp_error_declarator)
16744 return error_mark_node;
16746 /* Check that the number of template-parameter-lists is OK. */
16747 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16748 token->location))
16749 return error_mark_node;
16751 if (declares_class_or_enum & 2)
16752 cp_parser_check_for_definition_in_return_type (declarator,
16753 decl_specifiers->type,
16754 decl_specifiers->locations[ds_type_spec]);
16756 /* Figure out what scope the entity declared by the DECLARATOR is
16757 located in. `grokdeclarator' sometimes changes the scope, so
16758 we compute it now. */
16759 scope = get_scope_of_declarator (declarator);
16761 /* Perform any lookups in the declared type which were thought to be
16762 dependent, but are not in the scope of the declarator. */
16763 decl_specifiers->type
16764 = maybe_update_decl_type (decl_specifiers->type, scope);
16766 /* If we're allowing GNU extensions, look for an
16767 asm-specification. */
16768 if (cp_parser_allow_gnu_extensions_p (parser))
16770 /* Look for an asm-specification. */
16771 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16772 asm_specification = cp_parser_asm_specification_opt (parser);
16774 else
16775 asm_specification = NULL_TREE;
16777 /* Look for attributes. */
16778 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16779 attributes = cp_parser_attributes_opt (parser);
16781 // Save off the current template constraints. These will apply
16782 // to the nested scope, not the declarator. Consider, an out-of-class
16783 // member definition:
16785 // template<typename T>
16786 // requires C<T>()
16787 // void S<T>::f() requires D<T>() { ... }
16789 // At the point we parse the 2nd requires clause, the previous the
16790 // current constraints will have been used to resolve the enclosing
16791 // class S<T>. The D<T>() requirement applies only to the definition
16792 // of f and do not include C<T>().
16793 tree saved_template_reqs = release (current_template_reqs);
16795 // Parse an optional requires clause. Currently, requirements can
16796 // be written for out-of-class member function definitions.
16798 // TODO: It may be better to always parse and diagnose the error
16799 // as a semantic one later on.
16800 if (flag_concepts && scope && function_declarator_p (declarator))
16802 if (tree r = cp_parser_requires_clause_opt (parser))
16803 current_template_reqs = finish_template_requirements (r);
16805 else
16806 current_template_reqs = saved_template_reqs;
16809 /* Peek at the next token. */
16810 token = cp_lexer_peek_token (parser->lexer);
16812 if (function_declarator_p (declarator))
16814 /* Check to see if the token indicates the start of a
16815 function-definition. */
16816 if (cp_parser_token_starts_function_definition_p (token))
16818 if (!function_definition_allowed_p)
16820 /* If a function-definition should not appear here, issue an
16821 error message. */
16822 cp_parser_error (parser,
16823 "a function-definition is not allowed here");
16824 return error_mark_node;
16827 location_t func_brace_location
16828 = cp_lexer_peek_token (parser->lexer)->location;
16830 /* Neither attributes nor an asm-specification are allowed
16831 on a function-definition. */
16832 if (asm_specification)
16833 error_at (asm_spec_start_token->location,
16834 "an asm-specification is not allowed "
16835 "on a function-definition");
16836 if (attributes)
16837 error_at (attributes_start_token->location,
16838 "attributes are not allowed "
16839 "on a function-definition");
16840 /* This is a function-definition. */
16841 *function_definition_p = true;
16843 /* Parse the function definition. */
16844 if (member_p)
16845 decl = cp_parser_save_member_function_body (parser,
16846 decl_specifiers,
16847 declarator,
16848 prefix_attributes);
16849 else
16850 decl =
16851 (cp_parser_function_definition_from_specifiers_and_declarator
16852 (parser, decl_specifiers, prefix_attributes, declarator));
16854 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16856 /* This is where the prologue starts... */
16857 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16858 = func_brace_location;
16861 // Restore the current requirements before returing.
16862 current_template_reqs = saved_template_reqs;
16864 return decl;
16868 /* [dcl.dcl]
16870 Only in function declarations for constructors, destructors, and
16871 type conversions can the decl-specifier-seq be omitted.
16873 We explicitly postpone this check past the point where we handle
16874 function-definitions because we tolerate function-definitions
16875 that are missing their return types in some modes. */
16876 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16878 cp_parser_error (parser,
16879 "expected constructor, destructor, or type conversion");
16880 return error_mark_node;
16883 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16884 if (token->type == CPP_EQ
16885 || token->type == CPP_OPEN_PAREN
16886 || token->type == CPP_OPEN_BRACE)
16888 is_initialized = SD_INITIALIZED;
16889 initialization_kind = token->type;
16890 if (maybe_range_for_decl)
16891 *maybe_range_for_decl = error_mark_node;
16893 if (token->type == CPP_EQ
16894 && function_declarator_p (declarator))
16896 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16897 if (t2->keyword == RID_DEFAULT)
16898 is_initialized = SD_DEFAULTED;
16899 else if (t2->keyword == RID_DELETE)
16900 is_initialized = SD_DELETED;
16903 else
16905 /* If the init-declarator isn't initialized and isn't followed by a
16906 `,' or `;', it's not a valid init-declarator. */
16907 if (token->type != CPP_COMMA
16908 && token->type != CPP_SEMICOLON)
16910 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16911 range_for_decl_p = true;
16912 else
16914 cp_parser_error (parser, "expected initializer");
16915 return error_mark_node;
16918 is_initialized = SD_UNINITIALIZED;
16919 initialization_kind = CPP_EOF;
16922 /* Because start_decl has side-effects, we should only call it if we
16923 know we're going ahead. By this point, we know that we cannot
16924 possibly be looking at any other construct. */
16925 cp_parser_commit_to_tentative_parse (parser);
16927 /* If the decl specifiers were bad, issue an error now that we're
16928 sure this was intended to be a declarator. Then continue
16929 declaring the variable(s), as int, to try to cut down on further
16930 errors. */
16931 if (decl_specifiers->any_specifiers_p
16932 && decl_specifiers->type == error_mark_node)
16934 cp_parser_error (parser, "invalid type in declaration");
16935 decl_specifiers->type = integer_type_node;
16938 /* Check to see whether or not this declaration is a friend. */
16939 friend_p = cp_parser_friend_p (decl_specifiers);
16941 /* Enter the newly declared entry in the symbol table. If we're
16942 processing a declaration in a class-specifier, we wait until
16943 after processing the initializer. */
16944 if (!member_p)
16946 if (parser->in_unbraced_linkage_specification_p)
16947 decl_specifiers->storage_class = sc_extern;
16948 decl = start_decl (declarator, decl_specifiers,
16949 range_for_decl_p? SD_INITIALIZED : is_initialized,
16950 attributes, prefix_attributes, &pushed_scope);
16951 cp_finalize_omp_declare_simd (parser, decl);
16952 /* Adjust location of decl if declarator->id_loc is more appropriate:
16953 set, and decl wasn't merged with another decl, in which case its
16954 location would be different from input_location, and more accurate. */
16955 if (DECL_P (decl)
16956 && declarator->id_loc != UNKNOWN_LOCATION
16957 && DECL_SOURCE_LOCATION (decl) == input_location)
16958 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16960 else if (scope)
16961 /* Enter the SCOPE. That way unqualified names appearing in the
16962 initializer will be looked up in SCOPE. */
16963 pushed_scope = push_scope (scope);
16965 /* Perform deferred access control checks, now that we know in which
16966 SCOPE the declared entity resides. */
16967 if (!member_p && decl)
16969 tree saved_current_function_decl = NULL_TREE;
16971 /* If the entity being declared is a function, pretend that we
16972 are in its scope. If it is a `friend', it may have access to
16973 things that would not otherwise be accessible. */
16974 if (TREE_CODE (decl) == FUNCTION_DECL)
16976 saved_current_function_decl = current_function_decl;
16977 current_function_decl = decl;
16980 /* Perform access checks for template parameters. */
16981 cp_parser_perform_template_parameter_access_checks (checks);
16983 /* Perform the access control checks for the declarator and the
16984 decl-specifiers. */
16985 perform_deferred_access_checks (tf_warning_or_error);
16987 /* Restore the saved value. */
16988 if (TREE_CODE (decl) == FUNCTION_DECL)
16989 current_function_decl = saved_current_function_decl;
16992 /* Parse the initializer. */
16993 initializer = NULL_TREE;
16994 is_direct_init = false;
16995 is_non_constant_init = true;
16996 if (is_initialized)
16998 if (function_declarator_p (declarator))
17000 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17001 if (initialization_kind == CPP_EQ)
17002 initializer = cp_parser_pure_specifier (parser);
17003 else
17005 /* If the declaration was erroneous, we don't really
17006 know what the user intended, so just silently
17007 consume the initializer. */
17008 if (decl != error_mark_node)
17009 error_at (initializer_start_token->location,
17010 "initializer provided for function");
17011 cp_parser_skip_to_closing_parenthesis (parser,
17012 /*recovering=*/true,
17013 /*or_comma=*/false,
17014 /*consume_paren=*/true);
17017 else
17019 /* We want to record the extra mangling scope for in-class
17020 initializers of class members and initializers of static data
17021 member templates. The former involves deferring
17022 parsing of the initializer until end of class as with default
17023 arguments. So right here we only handle the latter. */
17024 if (!member_p && processing_template_decl)
17025 start_lambda_scope (decl);
17026 initializer = cp_parser_initializer (parser,
17027 &is_direct_init,
17028 &is_non_constant_init);
17029 if (!member_p && processing_template_decl)
17030 finish_lambda_scope ();
17031 if (initializer == error_mark_node)
17032 cp_parser_skip_to_end_of_statement (parser);
17036 /* The old parser allows attributes to appear after a parenthesized
17037 initializer. Mark Mitchell proposed removing this functionality
17038 on the GCC mailing lists on 2002-08-13. This parser accepts the
17039 attributes -- but ignores them. */
17040 if (cp_parser_allow_gnu_extensions_p (parser)
17041 && initialization_kind == CPP_OPEN_PAREN)
17042 if (cp_parser_attributes_opt (parser))
17043 warning (OPT_Wattributes,
17044 "attributes after parenthesized initializer ignored");
17046 /* For an in-class declaration, use `grokfield' to create the
17047 declaration. */
17048 if (member_p)
17050 if (pushed_scope)
17052 pop_scope (pushed_scope);
17053 pushed_scope = NULL_TREE;
17055 decl = grokfield (declarator, decl_specifiers,
17056 initializer, !is_non_constant_init,
17057 /*asmspec=*/NULL_TREE,
17058 chainon (attributes, prefix_attributes));
17059 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17060 cp_parser_save_default_args (parser, decl);
17061 cp_finalize_omp_declare_simd (parser, decl);
17064 /* Finish processing the declaration. But, skip member
17065 declarations. */
17066 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17068 cp_finish_decl (decl,
17069 initializer, !is_non_constant_init,
17070 asm_specification,
17071 /* If the initializer is in parentheses, then this is
17072 a direct-initialization, which means that an
17073 `explicit' constructor is OK. Otherwise, an
17074 `explicit' constructor cannot be used. */
17075 ((is_direct_init || !is_initialized)
17076 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17078 else if ((cxx_dialect != cxx98) && friend_p
17079 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17080 /* Core issue #226 (C++0x only): A default template-argument
17081 shall not be specified in a friend class template
17082 declaration. */
17083 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17084 /*is_partial=*/false, /*is_friend_decl=*/1);
17086 if (!friend_p && pushed_scope)
17087 pop_scope (pushed_scope);
17089 if (function_declarator_p (declarator)
17090 && parser->fully_implicit_function_template_p)
17092 if (member_p)
17093 decl = finish_fully_implicit_template (parser, decl);
17094 else
17095 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17098 return decl;
17101 /* Parse a declarator.
17103 declarator:
17104 direct-declarator
17105 ptr-operator declarator
17107 abstract-declarator:
17108 ptr-operator abstract-declarator [opt]
17109 direct-abstract-declarator
17111 GNU Extensions:
17113 declarator:
17114 attributes [opt] direct-declarator
17115 attributes [opt] ptr-operator declarator
17117 abstract-declarator:
17118 attributes [opt] ptr-operator abstract-declarator [opt]
17119 attributes [opt] direct-abstract-declarator
17121 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17122 detect constructor, destructor or conversion operators. It is set
17123 to -1 if the declarator is a name, and +1 if it is a
17124 function. Otherwise it is set to zero. Usually you just want to
17125 test for >0, but internally the negative value is used.
17127 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17128 a decl-specifier-seq unless it declares a constructor, destructor,
17129 or conversion. It might seem that we could check this condition in
17130 semantic analysis, rather than parsing, but that makes it difficult
17131 to handle something like `f()'. We want to notice that there are
17132 no decl-specifiers, and therefore realize that this is an
17133 expression, not a declaration.)
17135 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17136 the declarator is a direct-declarator of the form "(...)".
17138 MEMBER_P is true iff this declarator is a member-declarator. */
17140 static cp_declarator *
17141 cp_parser_declarator (cp_parser* parser,
17142 cp_parser_declarator_kind dcl_kind,
17143 int* ctor_dtor_or_conv_p,
17144 bool* parenthesized_p,
17145 bool member_p)
17147 cp_declarator *declarator;
17148 enum tree_code code;
17149 cp_cv_quals cv_quals;
17150 tree class_type;
17151 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17153 /* Assume this is not a constructor, destructor, or type-conversion
17154 operator. */
17155 if (ctor_dtor_or_conv_p)
17156 *ctor_dtor_or_conv_p = 0;
17158 if (cp_parser_allow_gnu_extensions_p (parser))
17159 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17161 /* Check for the ptr-operator production. */
17162 cp_parser_parse_tentatively (parser);
17163 /* Parse the ptr-operator. */
17164 code = cp_parser_ptr_operator (parser,
17165 &class_type,
17166 &cv_quals,
17167 &std_attributes);
17169 /* If that worked, then we have a ptr-operator. */
17170 if (cp_parser_parse_definitely (parser))
17172 /* If a ptr-operator was found, then this declarator was not
17173 parenthesized. */
17174 if (parenthesized_p)
17175 *parenthesized_p = true;
17176 /* The dependent declarator is optional if we are parsing an
17177 abstract-declarator. */
17178 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17179 cp_parser_parse_tentatively (parser);
17181 /* Parse the dependent declarator. */
17182 declarator = cp_parser_declarator (parser, dcl_kind,
17183 /*ctor_dtor_or_conv_p=*/NULL,
17184 /*parenthesized_p=*/NULL,
17185 /*member_p=*/false);
17187 /* If we are parsing an abstract-declarator, we must handle the
17188 case where the dependent declarator is absent. */
17189 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17190 && !cp_parser_parse_definitely (parser))
17191 declarator = NULL;
17193 declarator = cp_parser_make_indirect_declarator
17194 (code, class_type, cv_quals, declarator, std_attributes);
17196 /* Everything else is a direct-declarator. */
17197 else
17199 if (parenthesized_p)
17200 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17201 CPP_OPEN_PAREN);
17202 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17203 ctor_dtor_or_conv_p,
17204 member_p);
17207 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17208 declarator->attributes = gnu_attributes;
17210 return declarator;
17213 /* Parse a direct-declarator or direct-abstract-declarator.
17215 direct-declarator:
17216 declarator-id
17217 direct-declarator ( parameter-declaration-clause )
17218 cv-qualifier-seq [opt]
17219 ref-qualifier [opt]
17220 exception-specification [opt]
17221 direct-declarator [ constant-expression [opt] ]
17222 ( declarator )
17224 direct-abstract-declarator:
17225 direct-abstract-declarator [opt]
17226 ( parameter-declaration-clause )
17227 cv-qualifier-seq [opt]
17228 ref-qualifier [opt]
17229 exception-specification [opt]
17230 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17231 ( abstract-declarator )
17233 Returns a representation of the declarator. DCL_KIND is
17234 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17235 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17236 we are parsing a direct-declarator. It is
17237 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17238 of ambiguity we prefer an abstract declarator, as per
17239 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17240 cp_parser_declarator. */
17242 static cp_declarator *
17243 cp_parser_direct_declarator (cp_parser* parser,
17244 cp_parser_declarator_kind dcl_kind,
17245 int* ctor_dtor_or_conv_p,
17246 bool member_p)
17248 cp_token *token;
17249 cp_declarator *declarator = NULL;
17250 tree scope = NULL_TREE;
17251 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17252 bool saved_in_declarator_p = parser->in_declarator_p;
17253 bool first = true;
17254 tree pushed_scope = NULL_TREE;
17256 while (true)
17258 /* Peek at the next token. */
17259 token = cp_lexer_peek_token (parser->lexer);
17260 if (token->type == CPP_OPEN_PAREN)
17262 /* This is either a parameter-declaration-clause, or a
17263 parenthesized declarator. When we know we are parsing a
17264 named declarator, it must be a parenthesized declarator
17265 if FIRST is true. For instance, `(int)' is a
17266 parameter-declaration-clause, with an omitted
17267 direct-abstract-declarator. But `((*))', is a
17268 parenthesized abstract declarator. Finally, when T is a
17269 template parameter `(T)' is a
17270 parameter-declaration-clause, and not a parenthesized
17271 named declarator.
17273 We first try and parse a parameter-declaration-clause,
17274 and then try a nested declarator (if FIRST is true).
17276 It is not an error for it not to be a
17277 parameter-declaration-clause, even when FIRST is
17278 false. Consider,
17280 int i (int);
17281 int i (3);
17283 The first is the declaration of a function while the
17284 second is the definition of a variable, including its
17285 initializer.
17287 Having seen only the parenthesis, we cannot know which of
17288 these two alternatives should be selected. Even more
17289 complex are examples like:
17291 int i (int (a));
17292 int i (int (3));
17294 The former is a function-declaration; the latter is a
17295 variable initialization.
17297 Thus again, we try a parameter-declaration-clause, and if
17298 that fails, we back out and return. */
17300 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17302 tree params;
17303 unsigned saved_num_template_parameter_lists;
17304 bool is_declarator = false;
17306 /* In a member-declarator, the only valid interpretation
17307 of a parenthesis is the start of a
17308 parameter-declaration-clause. (It is invalid to
17309 initialize a static data member with a parenthesized
17310 initializer; only the "=" form of initialization is
17311 permitted.) */
17312 if (!member_p)
17313 cp_parser_parse_tentatively (parser);
17315 /* Consume the `('. */
17316 cp_lexer_consume_token (parser->lexer);
17317 if (first)
17319 /* If this is going to be an abstract declarator, we're
17320 in a declarator and we can't have default args. */
17321 parser->default_arg_ok_p = false;
17322 parser->in_declarator_p = true;
17325 /* Inside the function parameter list, surrounding
17326 template-parameter-lists do not apply. */
17327 saved_num_template_parameter_lists
17328 = parser->num_template_parameter_lists;
17329 parser->num_template_parameter_lists = 0;
17331 begin_scope (sk_function_parms, NULL_TREE);
17333 /* Parse the parameter-declaration-clause. */
17334 params = cp_parser_parameter_declaration_clause (parser);
17336 /* Restore saved template parameter lists accounting for implicit
17337 template parameters. */
17338 parser->num_template_parameter_lists
17339 += saved_num_template_parameter_lists;
17341 /* Consume the `)'. */
17342 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17344 /* If all went well, parse the cv-qualifier-seq,
17345 ref-qualifier and the exception-specification. */
17346 if (member_p || cp_parser_parse_definitely (parser))
17348 cp_cv_quals cv_quals;
17349 cp_virt_specifiers virt_specifiers;
17350 cp_ref_qualifier ref_qual;
17351 tree exception_specification;
17352 tree late_return;
17353 tree attrs;
17354 bool memfn = (member_p || (pushed_scope
17355 && CLASS_TYPE_P (pushed_scope)));
17357 is_declarator = true;
17359 if (ctor_dtor_or_conv_p)
17360 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17361 first = false;
17363 /* Parse the cv-qualifier-seq. */
17364 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17365 /* Parse the ref-qualifier. */
17366 ref_qual = cp_parser_ref_qualifier_opt (parser);
17367 /* And the exception-specification. */
17368 exception_specification
17369 = cp_parser_exception_specification_opt (parser);
17371 attrs = cp_parser_std_attribute_spec_seq (parser);
17373 late_return = (cp_parser_late_return_type_opt
17374 (parser, declarator,
17375 memfn ? cv_quals : -1));
17378 /* Parse the virt-specifier-seq. */
17379 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17381 /* Create the function-declarator. */
17382 declarator = make_call_declarator (declarator,
17383 params,
17384 cv_quals,
17385 virt_specifiers,
17386 ref_qual,
17387 exception_specification,
17388 late_return);
17389 declarator->std_attributes = attrs;
17390 /* Any subsequent parameter lists are to do with
17391 return type, so are not those of the declared
17392 function. */
17393 parser->default_arg_ok_p = false;
17396 /* Remove the function parms from scope. */
17397 pop_bindings_and_leave_scope ();
17399 if (is_declarator)
17400 /* Repeat the main loop. */
17401 continue;
17404 /* If this is the first, we can try a parenthesized
17405 declarator. */
17406 if (first)
17408 bool saved_in_type_id_in_expr_p;
17410 parser->default_arg_ok_p = saved_default_arg_ok_p;
17411 parser->in_declarator_p = saved_in_declarator_p;
17413 /* Consume the `('. */
17414 cp_lexer_consume_token (parser->lexer);
17415 /* Parse the nested declarator. */
17416 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17417 parser->in_type_id_in_expr_p = true;
17418 declarator
17419 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17420 /*parenthesized_p=*/NULL,
17421 member_p);
17422 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17423 first = false;
17424 /* Expect a `)'. */
17425 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17426 declarator = cp_error_declarator;
17427 if (declarator == cp_error_declarator)
17428 break;
17430 goto handle_declarator;
17432 /* Otherwise, we must be done. */
17433 else
17434 break;
17436 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17437 && token->type == CPP_OPEN_SQUARE
17438 && !cp_next_tokens_can_be_attribute_p (parser))
17440 /* Parse an array-declarator. */
17441 tree bounds, attrs;
17443 if (ctor_dtor_or_conv_p)
17444 *ctor_dtor_or_conv_p = 0;
17446 first = false;
17447 parser->default_arg_ok_p = false;
17448 parser->in_declarator_p = true;
17449 /* Consume the `['. */
17450 cp_lexer_consume_token (parser->lexer);
17451 /* Peek at the next token. */
17452 token = cp_lexer_peek_token (parser->lexer);
17453 /* If the next token is `]', then there is no
17454 constant-expression. */
17455 if (token->type != CPP_CLOSE_SQUARE)
17457 bool non_constant_p;
17458 bounds
17459 = cp_parser_constant_expression (parser,
17460 /*allow_non_constant=*/true,
17461 &non_constant_p);
17462 if (!non_constant_p)
17463 /* OK */;
17464 else if (error_operand_p (bounds))
17465 /* Already gave an error. */;
17466 else if (!parser->in_function_body
17467 || current_binding_level->kind == sk_function_parms)
17469 /* Normally, the array bound must be an integral constant
17470 expression. However, as an extension, we allow VLAs
17471 in function scopes as long as they aren't part of a
17472 parameter declaration. */
17473 cp_parser_error (parser,
17474 "array bound is not an integer constant");
17475 bounds = error_mark_node;
17477 else if (processing_template_decl)
17479 /* Remember this wasn't a constant-expression. */
17480 bounds = build_nop (TREE_TYPE (bounds), bounds);
17481 TREE_SIDE_EFFECTS (bounds) = 1;
17484 else
17485 bounds = NULL_TREE;
17486 /* Look for the closing `]'. */
17487 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17489 declarator = cp_error_declarator;
17490 break;
17493 attrs = cp_parser_std_attribute_spec_seq (parser);
17494 declarator = make_array_declarator (declarator, bounds);
17495 declarator->std_attributes = attrs;
17497 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17500 tree qualifying_scope;
17501 tree unqualified_name;
17502 tree attrs;
17503 special_function_kind sfk;
17504 bool abstract_ok;
17505 bool pack_expansion_p = false;
17506 cp_token *declarator_id_start_token;
17508 /* Parse a declarator-id */
17509 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17510 if (abstract_ok)
17512 cp_parser_parse_tentatively (parser);
17514 /* If we see an ellipsis, we should be looking at a
17515 parameter pack. */
17516 if (token->type == CPP_ELLIPSIS)
17518 /* Consume the `...' */
17519 cp_lexer_consume_token (parser->lexer);
17521 pack_expansion_p = true;
17525 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17526 unqualified_name
17527 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17528 qualifying_scope = parser->scope;
17529 if (abstract_ok)
17531 bool okay = false;
17533 if (!unqualified_name && pack_expansion_p)
17535 /* Check whether an error occurred. */
17536 okay = !cp_parser_error_occurred (parser);
17538 /* We already consumed the ellipsis to mark a
17539 parameter pack, but we have no way to report it,
17540 so abort the tentative parse. We will be exiting
17541 immediately anyway. */
17542 cp_parser_abort_tentative_parse (parser);
17544 else
17545 okay = cp_parser_parse_definitely (parser);
17547 if (!okay)
17548 unqualified_name = error_mark_node;
17549 else if (unqualified_name
17550 && (qualifying_scope
17551 || (!identifier_p (unqualified_name))))
17553 cp_parser_error (parser, "expected unqualified-id");
17554 unqualified_name = error_mark_node;
17558 if (!unqualified_name)
17559 return NULL;
17560 if (unqualified_name == error_mark_node)
17562 declarator = cp_error_declarator;
17563 pack_expansion_p = false;
17564 declarator->parameter_pack_p = false;
17565 break;
17568 attrs = cp_parser_std_attribute_spec_seq (parser);
17570 if (qualifying_scope && at_namespace_scope_p ()
17571 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17573 /* In the declaration of a member of a template class
17574 outside of the class itself, the SCOPE will sometimes
17575 be a TYPENAME_TYPE. For example, given:
17577 template <typename T>
17578 int S<T>::R::i = 3;
17580 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17581 this context, we must resolve S<T>::R to an ordinary
17582 type, rather than a typename type.
17584 The reason we normally avoid resolving TYPENAME_TYPEs
17585 is that a specialization of `S' might render
17586 `S<T>::R' not a type. However, if `S' is
17587 specialized, then this `i' will not be used, so there
17588 is no harm in resolving the types here. */
17589 tree type;
17591 /* Resolve the TYPENAME_TYPE. */
17592 type = resolve_typename_type (qualifying_scope,
17593 /*only_current_p=*/false);
17594 /* If that failed, the declarator is invalid. */
17595 if (TREE_CODE (type) == TYPENAME_TYPE)
17597 if (typedef_variant_p (type))
17598 error_at (declarator_id_start_token->location,
17599 "cannot define member of dependent typedef "
17600 "%qT", type);
17601 else
17602 error_at (declarator_id_start_token->location,
17603 "%<%T::%E%> is not a type",
17604 TYPE_CONTEXT (qualifying_scope),
17605 TYPE_IDENTIFIER (qualifying_scope));
17607 qualifying_scope = type;
17610 sfk = sfk_none;
17612 if (unqualified_name)
17614 tree class_type;
17616 if (qualifying_scope
17617 && CLASS_TYPE_P (qualifying_scope))
17618 class_type = qualifying_scope;
17619 else
17620 class_type = current_class_type;
17622 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17624 tree name_type = TREE_TYPE (unqualified_name);
17625 if (class_type && same_type_p (name_type, class_type))
17627 if (qualifying_scope
17628 && CLASSTYPE_USE_TEMPLATE (name_type))
17630 error_at (declarator_id_start_token->location,
17631 "invalid use of constructor as a template");
17632 inform (declarator_id_start_token->location,
17633 "use %<%T::%D%> instead of %<%T::%D%> to "
17634 "name the constructor in a qualified name",
17635 class_type,
17636 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17637 class_type, name_type);
17638 declarator = cp_error_declarator;
17639 break;
17641 else
17642 unqualified_name = constructor_name (class_type);
17644 else
17646 /* We do not attempt to print the declarator
17647 here because we do not have enough
17648 information about its original syntactic
17649 form. */
17650 cp_parser_error (parser, "invalid declarator");
17651 declarator = cp_error_declarator;
17652 break;
17656 if (class_type)
17658 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17659 sfk = sfk_destructor;
17660 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17661 sfk = sfk_conversion;
17662 else if (/* There's no way to declare a constructor
17663 for an anonymous type, even if the type
17664 got a name for linkage purposes. */
17665 !TYPE_WAS_ANONYMOUS (class_type)
17666 && constructor_name_p (unqualified_name,
17667 class_type))
17669 unqualified_name = constructor_name (class_type);
17670 sfk = sfk_constructor;
17672 else if (is_overloaded_fn (unqualified_name)
17673 && DECL_CONSTRUCTOR_P (get_first_fn
17674 (unqualified_name)))
17675 sfk = sfk_constructor;
17677 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17678 *ctor_dtor_or_conv_p = -1;
17681 declarator = make_id_declarator (qualifying_scope,
17682 unqualified_name,
17683 sfk);
17684 declarator->std_attributes = attrs;
17685 declarator->id_loc = token->location;
17686 declarator->parameter_pack_p = pack_expansion_p;
17688 if (pack_expansion_p)
17689 maybe_warn_variadic_templates ();
17692 handle_declarator:;
17693 scope = get_scope_of_declarator (declarator);
17694 if (scope)
17696 /* Any names that appear after the declarator-id for a
17697 member are looked up in the containing scope. */
17698 if (at_function_scope_p ())
17700 /* But declarations with qualified-ids can't appear in a
17701 function. */
17702 cp_parser_error (parser, "qualified-id in declaration");
17703 break;
17705 pushed_scope = push_scope (scope);
17707 parser->in_declarator_p = true;
17708 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17709 || (declarator && declarator->kind == cdk_id))
17710 /* Default args are only allowed on function
17711 declarations. */
17712 parser->default_arg_ok_p = saved_default_arg_ok_p;
17713 else
17714 parser->default_arg_ok_p = false;
17716 first = false;
17718 /* We're done. */
17719 else
17720 break;
17723 /* For an abstract declarator, we might wind up with nothing at this
17724 point. That's an error; the declarator is not optional. */
17725 if (!declarator)
17726 cp_parser_error (parser, "expected declarator");
17728 /* If we entered a scope, we must exit it now. */
17729 if (pushed_scope)
17730 pop_scope (pushed_scope);
17732 parser->default_arg_ok_p = saved_default_arg_ok_p;
17733 parser->in_declarator_p = saved_in_declarator_p;
17735 return declarator;
17738 /* Parse a ptr-operator.
17740 ptr-operator:
17741 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17742 * cv-qualifier-seq [opt]
17744 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17745 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17747 GNU Extension:
17749 ptr-operator:
17750 & cv-qualifier-seq [opt]
17752 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17753 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17754 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17755 filled in with the TYPE containing the member. *CV_QUALS is
17756 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17757 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17758 Note that the tree codes returned by this function have nothing
17759 to do with the types of trees that will be eventually be created
17760 to represent the pointer or reference type being parsed. They are
17761 just constants with suggestive names. */
17762 static enum tree_code
17763 cp_parser_ptr_operator (cp_parser* parser,
17764 tree* type,
17765 cp_cv_quals *cv_quals,
17766 tree *attributes)
17768 enum tree_code code = ERROR_MARK;
17769 cp_token *token;
17770 tree attrs = NULL_TREE;
17772 /* Assume that it's not a pointer-to-member. */
17773 *type = NULL_TREE;
17774 /* And that there are no cv-qualifiers. */
17775 *cv_quals = TYPE_UNQUALIFIED;
17777 /* Peek at the next token. */
17778 token = cp_lexer_peek_token (parser->lexer);
17780 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17781 if (token->type == CPP_MULT)
17782 code = INDIRECT_REF;
17783 else if (token->type == CPP_AND)
17784 code = ADDR_EXPR;
17785 else if ((cxx_dialect != cxx98) &&
17786 token->type == CPP_AND_AND) /* C++0x only */
17787 code = NON_LVALUE_EXPR;
17789 if (code != ERROR_MARK)
17791 /* Consume the `*', `&' or `&&'. */
17792 cp_lexer_consume_token (parser->lexer);
17794 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17795 `&', if we are allowing GNU extensions. (The only qualifier
17796 that can legally appear after `&' is `restrict', but that is
17797 enforced during semantic analysis. */
17798 if (code == INDIRECT_REF
17799 || cp_parser_allow_gnu_extensions_p (parser))
17800 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17802 attrs = cp_parser_std_attribute_spec_seq (parser);
17803 if (attributes != NULL)
17804 *attributes = attrs;
17806 else
17808 /* Try the pointer-to-member case. */
17809 cp_parser_parse_tentatively (parser);
17810 /* Look for the optional `::' operator. */
17811 cp_parser_global_scope_opt (parser,
17812 /*current_scope_valid_p=*/false);
17813 /* Look for the nested-name specifier. */
17814 token = cp_lexer_peek_token (parser->lexer);
17815 cp_parser_nested_name_specifier (parser,
17816 /*typename_keyword_p=*/false,
17817 /*check_dependency_p=*/true,
17818 /*type_p=*/false,
17819 /*is_declaration=*/false);
17820 /* If we found it, and the next token is a `*', then we are
17821 indeed looking at a pointer-to-member operator. */
17822 if (!cp_parser_error_occurred (parser)
17823 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17825 /* Indicate that the `*' operator was used. */
17826 code = INDIRECT_REF;
17828 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17829 error_at (token->location, "%qD is a namespace", parser->scope);
17830 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17831 error_at (token->location, "cannot form pointer to member of "
17832 "non-class %q#T", parser->scope);
17833 else
17835 /* The type of which the member is a member is given by the
17836 current SCOPE. */
17837 *type = parser->scope;
17838 /* The next name will not be qualified. */
17839 parser->scope = NULL_TREE;
17840 parser->qualifying_scope = NULL_TREE;
17841 parser->object_scope = NULL_TREE;
17842 /* Look for optional c++11 attributes. */
17843 attrs = cp_parser_std_attribute_spec_seq (parser);
17844 if (attributes != NULL)
17845 *attributes = attrs;
17846 /* Look for the optional cv-qualifier-seq. */
17847 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17850 /* If that didn't work we don't have a ptr-operator. */
17851 if (!cp_parser_parse_definitely (parser))
17852 cp_parser_error (parser, "expected ptr-operator");
17855 return code;
17858 /* Parse an (optional) cv-qualifier-seq.
17860 cv-qualifier-seq:
17861 cv-qualifier cv-qualifier-seq [opt]
17863 cv-qualifier:
17864 const
17865 volatile
17867 GNU Extension:
17869 cv-qualifier:
17870 __restrict__
17872 Returns a bitmask representing the cv-qualifiers. */
17874 static cp_cv_quals
17875 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17877 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17879 while (true)
17881 cp_token *token;
17882 cp_cv_quals cv_qualifier;
17884 /* Peek at the next token. */
17885 token = cp_lexer_peek_token (parser->lexer);
17886 /* See if it's a cv-qualifier. */
17887 switch (token->keyword)
17889 case RID_CONST:
17890 cv_qualifier = TYPE_QUAL_CONST;
17891 break;
17893 case RID_VOLATILE:
17894 cv_qualifier = TYPE_QUAL_VOLATILE;
17895 break;
17897 case RID_RESTRICT:
17898 cv_qualifier = TYPE_QUAL_RESTRICT;
17899 break;
17901 default:
17902 cv_qualifier = TYPE_UNQUALIFIED;
17903 break;
17906 if (!cv_qualifier)
17907 break;
17909 if (cv_quals & cv_qualifier)
17911 error_at (token->location, "duplicate cv-qualifier");
17912 cp_lexer_purge_token (parser->lexer);
17914 else
17916 cp_lexer_consume_token (parser->lexer);
17917 cv_quals |= cv_qualifier;
17921 return cv_quals;
17924 /* Parse an (optional) ref-qualifier
17926 ref-qualifier:
17930 Returns cp_ref_qualifier representing ref-qualifier. */
17932 static cp_ref_qualifier
17933 cp_parser_ref_qualifier_opt (cp_parser* parser)
17935 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17937 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17938 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17939 return ref_qual;
17941 while (true)
17943 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17944 cp_token *token = cp_lexer_peek_token (parser->lexer);
17946 switch (token->type)
17948 case CPP_AND:
17949 curr_ref_qual = REF_QUAL_LVALUE;
17950 break;
17952 case CPP_AND_AND:
17953 curr_ref_qual = REF_QUAL_RVALUE;
17954 break;
17956 default:
17957 curr_ref_qual = REF_QUAL_NONE;
17958 break;
17961 if (!curr_ref_qual)
17962 break;
17963 else if (ref_qual)
17965 error_at (token->location, "multiple ref-qualifiers");
17966 cp_lexer_purge_token (parser->lexer);
17968 else
17970 ref_qual = curr_ref_qual;
17971 cp_lexer_consume_token (parser->lexer);
17975 return ref_qual;
17978 /* Parse an (optional) virt-specifier-seq.
17980 virt-specifier-seq:
17981 virt-specifier virt-specifier-seq [opt]
17983 virt-specifier:
17984 override
17985 final
17987 Returns a bitmask representing the virt-specifiers. */
17989 static cp_virt_specifiers
17990 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17992 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17994 while (true)
17996 cp_token *token;
17997 cp_virt_specifiers virt_specifier;
17999 /* Peek at the next token. */
18000 token = cp_lexer_peek_token (parser->lexer);
18001 /* See if it's a virt-specifier-qualifier. */
18002 if (token->type != CPP_NAME)
18003 break;
18004 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18006 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18007 virt_specifier = VIRT_SPEC_OVERRIDE;
18009 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18011 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18012 virt_specifier = VIRT_SPEC_FINAL;
18014 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18016 virt_specifier = VIRT_SPEC_FINAL;
18018 else
18019 break;
18021 if (virt_specifiers & virt_specifier)
18023 error_at (token->location, "duplicate virt-specifier");
18024 cp_lexer_purge_token (parser->lexer);
18026 else
18028 cp_lexer_consume_token (parser->lexer);
18029 virt_specifiers |= virt_specifier;
18032 return virt_specifiers;
18035 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18036 is in scope even though it isn't real. */
18038 static void
18039 inject_this_parameter (tree ctype, cp_cv_quals quals)
18041 tree this_parm;
18043 if (current_class_ptr)
18045 /* We don't clear this between NSDMIs. Is it already what we want? */
18046 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18047 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18048 && cp_type_quals (type) == quals)
18049 return;
18052 this_parm = build_this_parm (ctype, quals);
18053 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18054 current_class_ptr = NULL_TREE;
18055 current_class_ref
18056 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18057 current_class_ptr = this_parm;
18060 /* Return true iff our current scope is a non-static data member
18061 initializer. */
18063 bool
18064 parsing_nsdmi (void)
18066 /* We recognize NSDMI context by the context-less 'this' pointer set up
18067 by the function above. */
18068 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18069 return true;
18070 return false;
18073 /* Parse a late-specified return type, if any. This is not a separate
18074 non-terminal, but part of a function declarator, which looks like
18076 -> trailing-type-specifier-seq abstract-declarator(opt)
18078 Returns the type indicated by the type-id.
18080 In addition to this this parses any queued up omp declare simd
18081 clauses.
18083 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18084 function. */
18086 static tree
18087 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18088 cp_cv_quals quals)
18090 cp_token *token;
18091 tree type = NULL_TREE;
18092 bool declare_simd_p = (parser->omp_declare_simd
18093 && declarator
18094 && declarator->kind == cdk_id);
18096 /* Peek at the next token. */
18097 token = cp_lexer_peek_token (parser->lexer);
18098 /* A late-specified return type is indicated by an initial '->'. */
18099 if (token->type != CPP_DEREF && !declare_simd_p)
18100 return NULL_TREE;
18102 tree save_ccp = current_class_ptr;
18103 tree save_ccr = current_class_ref;
18104 if (quals >= 0)
18106 /* DR 1207: 'this' is in scope in the trailing return type. */
18107 inject_this_parameter (current_class_type, quals);
18110 if (token->type == CPP_DEREF)
18112 /* Consume the ->. */
18113 cp_lexer_consume_token (parser->lexer);
18115 type = cp_parser_trailing_type_id (parser);
18118 if (declare_simd_p)
18119 declarator->std_attributes
18120 = cp_parser_late_parsing_omp_declare_simd (parser,
18121 declarator->std_attributes);
18123 if (quals >= 0)
18125 current_class_ptr = save_ccp;
18126 current_class_ref = save_ccr;
18129 return type;
18132 /* Parse a declarator-id.
18134 declarator-id:
18135 id-expression
18136 :: [opt] nested-name-specifier [opt] type-name
18138 In the `id-expression' case, the value returned is as for
18139 cp_parser_id_expression if the id-expression was an unqualified-id.
18140 If the id-expression was a qualified-id, then a SCOPE_REF is
18141 returned. The first operand is the scope (either a NAMESPACE_DECL
18142 or TREE_TYPE), but the second is still just a representation of an
18143 unqualified-id. */
18145 static tree
18146 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18148 tree id;
18149 /* The expression must be an id-expression. Assume that qualified
18150 names are the names of types so that:
18152 template <class T>
18153 int S<T>::R::i = 3;
18155 will work; we must treat `S<T>::R' as the name of a type.
18156 Similarly, assume that qualified names are templates, where
18157 required, so that:
18159 template <class T>
18160 int S<T>::R<T>::i = 3;
18162 will work, too. */
18163 id = cp_parser_id_expression (parser,
18164 /*template_keyword_p=*/false,
18165 /*check_dependency_p=*/false,
18166 /*template_p=*/NULL,
18167 /*declarator_p=*/true,
18168 optional_p);
18169 if (id && BASELINK_P (id))
18170 id = BASELINK_FUNCTIONS (id);
18171 return id;
18174 /* Parse a type-id.
18176 type-id:
18177 type-specifier-seq abstract-declarator [opt]
18179 Returns the TYPE specified. */
18181 static tree
18182 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18183 bool is_trailing_return)
18185 cp_decl_specifier_seq type_specifier_seq;
18186 cp_declarator *abstract_declarator;
18188 /* Parse the type-specifier-seq. */
18189 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18190 is_trailing_return,
18191 &type_specifier_seq);
18192 if (type_specifier_seq.type == error_mark_node)
18193 return error_mark_node;
18195 /* There might or might not be an abstract declarator. */
18196 cp_parser_parse_tentatively (parser);
18197 /* Look for the declarator. */
18198 abstract_declarator
18199 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18200 /*parenthesized_p=*/NULL,
18201 /*member_p=*/false);
18202 /* Check to see if there really was a declarator. */
18203 if (!cp_parser_parse_definitely (parser))
18204 abstract_declarator = NULL;
18206 if (type_specifier_seq.type
18207 && cxx_dialect < cxx1y
18208 && type_uses_auto (type_specifier_seq.type))
18210 /* A type-id with type 'auto' is only ok if the abstract declarator
18211 is a function declarator with a late-specified return type. */
18212 if (abstract_declarator
18213 && abstract_declarator->kind == cdk_function
18214 && abstract_declarator->u.function.late_return_type)
18215 /* OK */;
18216 else
18218 error ("invalid use of %<auto%>");
18219 return error_mark_node;
18223 return groktypename (&type_specifier_seq, abstract_declarator,
18224 is_template_arg);
18227 static tree cp_parser_type_id (cp_parser *parser)
18229 return cp_parser_type_id_1 (parser, false, false);
18232 static tree cp_parser_template_type_arg (cp_parser *parser)
18234 tree r;
18235 const char *saved_message = parser->type_definition_forbidden_message;
18236 parser->type_definition_forbidden_message
18237 = G_("types may not be defined in template arguments");
18238 r = cp_parser_type_id_1 (parser, true, false);
18239 parser->type_definition_forbidden_message = saved_message;
18240 return r;
18243 static tree cp_parser_trailing_type_id (cp_parser *parser)
18245 return cp_parser_type_id_1 (parser, false, true);
18248 /* Parse a type-specifier-seq.
18250 type-specifier-seq:
18251 type-specifier type-specifier-seq [opt]
18253 GNU extension:
18255 type-specifier-seq:
18256 attributes type-specifier-seq [opt]
18258 If IS_DECLARATION is true, we are at the start of a "condition" or
18259 exception-declaration, so we might be followed by a declarator-id.
18261 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18262 i.e. we've just seen "->".
18264 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18266 static void
18267 cp_parser_type_specifier_seq (cp_parser* parser,
18268 bool is_declaration,
18269 bool is_trailing_return,
18270 cp_decl_specifier_seq *type_specifier_seq)
18272 bool seen_type_specifier = false;
18273 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18274 cp_token *start_token = NULL;
18276 /* Clear the TYPE_SPECIFIER_SEQ. */
18277 clear_decl_specs (type_specifier_seq);
18279 /* In the context of a trailing return type, enum E { } is an
18280 elaborated-type-specifier followed by a function-body, not an
18281 enum-specifier. */
18282 if (is_trailing_return)
18283 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18285 /* Parse the type-specifiers and attributes. */
18286 while (true)
18288 tree type_specifier;
18289 bool is_cv_qualifier;
18291 /* Check for attributes first. */
18292 if (cp_next_tokens_can_be_attribute_p (parser))
18294 type_specifier_seq->attributes =
18295 chainon (type_specifier_seq->attributes,
18296 cp_parser_attributes_opt (parser));
18297 continue;
18300 /* record the token of the beginning of the type specifier seq,
18301 for error reporting purposes*/
18302 if (!start_token)
18303 start_token = cp_lexer_peek_token (parser->lexer);
18305 /* Look for the type-specifier. */
18306 type_specifier = cp_parser_type_specifier (parser,
18307 flags,
18308 type_specifier_seq,
18309 /*is_declaration=*/false,
18310 NULL,
18311 &is_cv_qualifier);
18312 if (!type_specifier)
18314 /* If the first type-specifier could not be found, this is not a
18315 type-specifier-seq at all. */
18316 if (!seen_type_specifier)
18318 cp_parser_error (parser, "expected type-specifier");
18319 type_specifier_seq->type = error_mark_node;
18320 return;
18322 /* If subsequent type-specifiers could not be found, the
18323 type-specifier-seq is complete. */
18324 break;
18327 seen_type_specifier = true;
18328 /* The standard says that a condition can be:
18330 type-specifier-seq declarator = assignment-expression
18332 However, given:
18334 struct S {};
18335 if (int S = ...)
18337 we should treat the "S" as a declarator, not as a
18338 type-specifier. The standard doesn't say that explicitly for
18339 type-specifier-seq, but it does say that for
18340 decl-specifier-seq in an ordinary declaration. Perhaps it
18341 would be clearer just to allow a decl-specifier-seq here, and
18342 then add a semantic restriction that if any decl-specifiers
18343 that are not type-specifiers appear, the program is invalid. */
18344 if (is_declaration && !is_cv_qualifier)
18345 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18349 /* Parse a parameter-declaration-clause.
18351 parameter-declaration-clause:
18352 parameter-declaration-list [opt] ... [opt]
18353 parameter-declaration-list , ...
18355 Returns a representation for the parameter declarations. A return
18356 value of NULL indicates a parameter-declaration-clause consisting
18357 only of an ellipsis. */
18359 static tree
18360 cp_parser_parameter_declaration_clause (cp_parser* parser)
18362 tree parameters;
18363 cp_token *token;
18364 bool ellipsis_p;
18365 bool is_error;
18367 struct cleanup {
18368 cp_parser* parser;
18369 int auto_is_implicit_function_template_parm_p;
18370 ~cleanup() {
18371 parser->auto_is_implicit_function_template_parm_p
18372 = auto_is_implicit_function_template_parm_p;
18374 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18376 (void) cleanup;
18378 if (!processing_specialization)
18379 parser->auto_is_implicit_function_template_parm_p = true;
18381 /* Peek at the next token. */
18382 token = cp_lexer_peek_token (parser->lexer);
18383 /* Check for trivial parameter-declaration-clauses. */
18384 if (token->type == CPP_ELLIPSIS)
18386 /* Consume the `...' token. */
18387 cp_lexer_consume_token (parser->lexer);
18388 return NULL_TREE;
18390 else if (token->type == CPP_CLOSE_PAREN)
18391 /* There are no parameters. */
18393 #ifndef NO_IMPLICIT_EXTERN_C
18394 if (in_system_header && current_class_type == NULL
18395 && current_lang_name == lang_name_c)
18396 return NULL_TREE;
18397 else
18398 #endif
18399 return void_list_node;
18401 /* Check for `(void)', too, which is a special case. */
18402 else if (token->keyword == RID_VOID
18403 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18404 == CPP_CLOSE_PAREN))
18406 /* Consume the `void' token. */
18407 cp_lexer_consume_token (parser->lexer);
18408 /* There are no parameters. */
18409 return void_list_node;
18412 /* Parse the parameter-declaration-list. */
18413 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18414 /* If a parse error occurred while parsing the
18415 parameter-declaration-list, then the entire
18416 parameter-declaration-clause is erroneous. */
18417 if (is_error)
18418 return NULL;
18420 /* Peek at the next token. */
18421 token = cp_lexer_peek_token (parser->lexer);
18422 /* If it's a `,', the clause should terminate with an ellipsis. */
18423 if (token->type == CPP_COMMA)
18425 /* Consume the `,'. */
18426 cp_lexer_consume_token (parser->lexer);
18427 /* Expect an ellipsis. */
18428 ellipsis_p
18429 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18431 /* It might also be `...' if the optional trailing `,' was
18432 omitted. */
18433 else if (token->type == CPP_ELLIPSIS)
18435 /* Consume the `...' token. */
18436 cp_lexer_consume_token (parser->lexer);
18437 /* And remember that we saw it. */
18438 ellipsis_p = true;
18440 else
18441 ellipsis_p = false;
18443 /* Finish the parameter list. */
18444 if (!ellipsis_p)
18445 parameters = chainon (parameters, void_list_node);
18447 return parameters;
18450 /* Parse a parameter-declaration-list.
18452 parameter-declaration-list:
18453 parameter-declaration
18454 parameter-declaration-list , parameter-declaration
18456 Returns a representation of the parameter-declaration-list, as for
18457 cp_parser_parameter_declaration_clause. However, the
18458 `void_list_node' is never appended to the list. Upon return,
18459 *IS_ERROR will be true iff an error occurred. */
18461 static tree
18462 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18464 tree parameters = NULL_TREE;
18465 tree *tail = &parameters;
18466 bool saved_in_unbraced_linkage_specification_p;
18467 int index = 0;
18469 /* Assume all will go well. */
18470 *is_error = false;
18471 /* The special considerations that apply to a function within an
18472 unbraced linkage specifications do not apply to the parameters
18473 to the function. */
18474 saved_in_unbraced_linkage_specification_p
18475 = parser->in_unbraced_linkage_specification_p;
18476 parser->in_unbraced_linkage_specification_p = false;
18478 /* Look for more parameters. */
18479 while (true)
18481 cp_parameter_declarator *parameter;
18482 tree decl = error_mark_node;
18483 bool parenthesized_p = false;
18484 int template_parm_idx = (parser->num_template_parameter_lists?
18485 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18486 (current_template_parms)) : 0);
18488 /* Parse the parameter. */
18489 parameter
18490 = cp_parser_parameter_declaration (parser,
18491 /*template_parm_p=*/false,
18492 &parenthesized_p);
18494 /* We don't know yet if the enclosing context is deprecated, so wait
18495 and warn in grokparms if appropriate. */
18496 deprecated_state = DEPRECATED_SUPPRESS;
18498 if (parameter)
18500 /* If a function parameter pack was specified and an implicit template
18501 parameter was introduced during cp_parser_parameter_declaration,
18502 change any implicit parameters introduced into packs. */
18503 if (parser->implicit_template_parms
18504 && parameter->declarator
18505 && parameter->declarator->parameter_pack_p)
18507 int latest_template_parm_idx = TREE_VEC_LENGTH
18508 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18510 if (latest_template_parm_idx != template_parm_idx)
18511 parameter->decl_specifiers.type = convert_generic_types_to_packs
18512 (parameter->decl_specifiers.type,
18513 template_parm_idx, latest_template_parm_idx);
18516 decl = grokdeclarator (parameter->declarator,
18517 &parameter->decl_specifiers,
18518 PARM,
18519 parameter->default_argument != NULL_TREE,
18520 &parameter->decl_specifiers.attributes);
18523 deprecated_state = DEPRECATED_NORMAL;
18525 /* If a parse error occurred parsing the parameter declaration,
18526 then the entire parameter-declaration-list is erroneous. */
18527 if (decl == error_mark_node)
18529 *is_error = true;
18530 parameters = error_mark_node;
18531 break;
18534 if (parameter->decl_specifiers.attributes)
18535 cplus_decl_attributes (&decl,
18536 parameter->decl_specifiers.attributes,
18538 if (DECL_NAME (decl))
18539 decl = pushdecl (decl);
18541 if (decl != error_mark_node)
18543 retrofit_lang_decl (decl);
18544 DECL_PARM_INDEX (decl) = ++index;
18545 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18548 /* Add the new parameter to the list. */
18549 *tail = build_tree_list (parameter->default_argument, decl);
18550 tail = &TREE_CHAIN (*tail);
18552 /* Peek at the next token. */
18553 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18554 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18555 /* These are for Objective-C++ */
18556 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18557 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18558 /* The parameter-declaration-list is complete. */
18559 break;
18560 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18562 cp_token *token;
18564 /* Peek at the next token. */
18565 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18566 /* If it's an ellipsis, then the list is complete. */
18567 if (token->type == CPP_ELLIPSIS)
18568 break;
18569 /* Otherwise, there must be more parameters. Consume the
18570 `,'. */
18571 cp_lexer_consume_token (parser->lexer);
18572 /* When parsing something like:
18574 int i(float f, double d)
18576 we can tell after seeing the declaration for "f" that we
18577 are not looking at an initialization of a variable "i",
18578 but rather at the declaration of a function "i".
18580 Due to the fact that the parsing of template arguments
18581 (as specified to a template-id) requires backtracking we
18582 cannot use this technique when inside a template argument
18583 list. */
18584 if (!parser->in_template_argument_list_p
18585 && !parser->in_type_id_in_expr_p
18586 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18587 /* However, a parameter-declaration of the form
18588 "float(f)" (which is a valid declaration of a
18589 parameter "f") can also be interpreted as an
18590 expression (the conversion of "f" to "float"). */
18591 && !parenthesized_p)
18592 cp_parser_commit_to_tentative_parse (parser);
18594 else
18596 cp_parser_error (parser, "expected %<,%> or %<...%>");
18597 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18598 cp_parser_skip_to_closing_parenthesis (parser,
18599 /*recovering=*/true,
18600 /*or_comma=*/false,
18601 /*consume_paren=*/false);
18602 break;
18606 parser->in_unbraced_linkage_specification_p
18607 = saved_in_unbraced_linkage_specification_p;
18609 if (cp_binding_level *its = parser->implicit_template_scope)
18610 if (current_binding_level->level_chain == its)
18612 parser->implicit_template_parms = 0;
18613 parser->implicit_template_scope = 0;
18616 return parameters;
18619 /* Parse a parameter declaration.
18621 parameter-declaration:
18622 decl-specifier-seq ... [opt] declarator
18623 decl-specifier-seq declarator = assignment-expression
18624 decl-specifier-seq ... [opt] abstract-declarator [opt]
18625 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18627 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18628 declares a template parameter. (In that case, a non-nested `>'
18629 token encountered during the parsing of the assignment-expression
18630 is not interpreted as a greater-than operator.)
18632 Returns a representation of the parameter, or NULL if an error
18633 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18634 true iff the declarator is of the form "(p)". */
18636 static cp_parameter_declarator *
18637 cp_parser_parameter_declaration (cp_parser *parser,
18638 bool template_parm_p,
18639 bool *parenthesized_p)
18641 int declares_class_or_enum;
18642 cp_decl_specifier_seq decl_specifiers;
18643 cp_declarator *declarator;
18644 tree default_argument;
18645 cp_token *token = NULL, *declarator_token_start = NULL;
18646 const char *saved_message;
18648 /* In a template parameter, `>' is not an operator.
18650 [temp.param]
18652 When parsing a default template-argument for a non-type
18653 template-parameter, the first non-nested `>' is taken as the end
18654 of the template parameter-list rather than a greater-than
18655 operator. */
18657 /* Type definitions may not appear in parameter types. */
18658 saved_message = parser->type_definition_forbidden_message;
18659 parser->type_definition_forbidden_message
18660 = G_("types may not be defined in parameter types");
18662 /* Parse the declaration-specifiers. */
18663 cp_parser_decl_specifier_seq (parser,
18664 CP_PARSER_FLAGS_NONE,
18665 &decl_specifiers,
18666 &declares_class_or_enum);
18668 /* Complain about missing 'typename' or other invalid type names. */
18669 if (!decl_specifiers.any_type_specifiers_p
18670 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18671 decl_specifiers.type = error_mark_node;
18673 /* If an error occurred, there's no reason to attempt to parse the
18674 rest of the declaration. */
18675 if (cp_parser_error_occurred (parser))
18677 parser->type_definition_forbidden_message = saved_message;
18678 return NULL;
18681 /* Peek at the next token. */
18682 token = cp_lexer_peek_token (parser->lexer);
18684 /* If the next token is a `)', `,', `=', `>', or `...', then there
18685 is no declarator. However, when variadic templates are enabled,
18686 there may be a declarator following `...'. */
18687 if (token->type == CPP_CLOSE_PAREN
18688 || token->type == CPP_COMMA
18689 || token->type == CPP_EQ
18690 || token->type == CPP_GREATER)
18692 declarator = NULL;
18693 if (parenthesized_p)
18694 *parenthesized_p = false;
18696 /* Otherwise, there should be a declarator. */
18697 else
18699 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18700 parser->default_arg_ok_p = false;
18702 /* After seeing a decl-specifier-seq, if the next token is not a
18703 "(", there is no possibility that the code is a valid
18704 expression. Therefore, if parsing tentatively, we commit at
18705 this point. */
18706 if (!parser->in_template_argument_list_p
18707 /* In an expression context, having seen:
18709 (int((char ...
18711 we cannot be sure whether we are looking at a
18712 function-type (taking a "char" as a parameter) or a cast
18713 of some object of type "char" to "int". */
18714 && !parser->in_type_id_in_expr_p
18715 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18716 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18717 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18718 cp_parser_commit_to_tentative_parse (parser);
18719 /* Parse the declarator. */
18720 declarator_token_start = token;
18721 declarator = cp_parser_declarator (parser,
18722 CP_PARSER_DECLARATOR_EITHER,
18723 /*ctor_dtor_or_conv_p=*/NULL,
18724 parenthesized_p,
18725 /*member_p=*/false);
18726 parser->default_arg_ok_p = saved_default_arg_ok_p;
18727 /* After the declarator, allow more attributes. */
18728 decl_specifiers.attributes
18729 = chainon (decl_specifiers.attributes,
18730 cp_parser_attributes_opt (parser));
18733 /* If the next token is an ellipsis, and we have not seen a
18734 declarator name, and the type of the declarator contains parameter
18735 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18736 a parameter pack expansion expression. Otherwise, leave the
18737 ellipsis for a C-style variadic function. */
18738 token = cp_lexer_peek_token (parser->lexer);
18739 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18741 tree type = decl_specifiers.type;
18743 if (type && DECL_P (type))
18744 type = TREE_TYPE (type);
18746 if (type
18747 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18748 && declarator_can_be_parameter_pack (declarator)
18749 && (!declarator || !declarator->parameter_pack_p)
18750 && uses_parameter_packs (type))
18752 /* Consume the `...'. */
18753 cp_lexer_consume_token (parser->lexer);
18754 maybe_warn_variadic_templates ();
18756 /* Build a pack expansion type */
18757 if (declarator)
18758 declarator->parameter_pack_p = true;
18759 else
18760 decl_specifiers.type = make_pack_expansion (type);
18764 /* The restriction on defining new types applies only to the type
18765 of the parameter, not to the default argument. */
18766 parser->type_definition_forbidden_message = saved_message;
18768 /* If the next token is `=', then process a default argument. */
18769 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18771 token = cp_lexer_peek_token (parser->lexer);
18772 /* If we are defining a class, then the tokens that make up the
18773 default argument must be saved and processed later. */
18774 if (!template_parm_p && at_class_scope_p ()
18775 && TYPE_BEING_DEFINED (current_class_type)
18776 && !LAMBDA_TYPE_P (current_class_type))
18777 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18778 /* Outside of a class definition, we can just parse the
18779 assignment-expression. */
18780 else
18781 default_argument
18782 = cp_parser_default_argument (parser, template_parm_p);
18784 if (!parser->default_arg_ok_p)
18786 if (flag_permissive)
18787 warning (0, "deprecated use of default argument for parameter of non-function");
18788 else
18790 error_at (token->location,
18791 "default arguments are only "
18792 "permitted for function parameters");
18793 default_argument = NULL_TREE;
18796 else if ((declarator && declarator->parameter_pack_p)
18797 || (decl_specifiers.type
18798 && PACK_EXPANSION_P (decl_specifiers.type)))
18800 /* Find the name of the parameter pack. */
18801 cp_declarator *id_declarator = declarator;
18802 while (id_declarator && id_declarator->kind != cdk_id)
18803 id_declarator = id_declarator->declarator;
18805 if (id_declarator && id_declarator->kind == cdk_id)
18806 error_at (declarator_token_start->location,
18807 template_parm_p
18808 ? G_("template parameter pack %qD "
18809 "cannot have a default argument")
18810 : G_("parameter pack %qD cannot have "
18811 "a default argument"),
18812 id_declarator->u.id.unqualified_name);
18813 else
18814 error_at (declarator_token_start->location,
18815 template_parm_p
18816 ? G_("template parameter pack cannot have "
18817 "a default argument")
18818 : G_("parameter pack cannot have a "
18819 "default argument"));
18821 default_argument = NULL_TREE;
18824 else
18825 default_argument = NULL_TREE;
18827 return make_parameter_declarator (&decl_specifiers,
18828 declarator,
18829 default_argument);
18832 /* Parse a default argument and return it.
18834 TEMPLATE_PARM_P is true if this is a default argument for a
18835 non-type template parameter. */
18836 static tree
18837 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18839 tree default_argument = NULL_TREE;
18840 bool saved_greater_than_is_operator_p;
18841 bool saved_local_variables_forbidden_p;
18842 bool non_constant_p, is_direct_init;
18844 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18845 set correctly. */
18846 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18847 parser->greater_than_is_operator_p = !template_parm_p;
18848 /* Local variable names (and the `this' keyword) may not
18849 appear in a default argument. */
18850 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18851 parser->local_variables_forbidden_p = true;
18852 /* Parse the assignment-expression. */
18853 if (template_parm_p)
18854 push_deferring_access_checks (dk_no_deferred);
18855 default_argument
18856 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18857 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18858 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18859 if (template_parm_p)
18860 pop_deferring_access_checks ();
18861 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18862 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18864 return default_argument;
18867 /* Parse a function-body.
18869 function-body:
18870 compound_statement */
18872 static void
18873 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18875 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18878 /* Parse a ctor-initializer-opt followed by a function-body. Return
18879 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18880 is true we are parsing a function-try-block. */
18882 static bool
18883 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18884 bool in_function_try_block)
18886 tree body, list;
18887 bool ctor_initializer_p;
18888 const bool check_body_p =
18889 DECL_CONSTRUCTOR_P (current_function_decl)
18890 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18891 tree last = NULL;
18893 /* Begin the function body. */
18894 body = begin_function_body ();
18895 /* Parse the optional ctor-initializer. */
18896 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18898 /* If we're parsing a constexpr constructor definition, we need
18899 to check that the constructor body is indeed empty. However,
18900 before we get to cp_parser_function_body lot of junk has been
18901 generated, so we can't just check that we have an empty block.
18902 Rather we take a snapshot of the outermost block, and check whether
18903 cp_parser_function_body changed its state. */
18904 if (check_body_p)
18906 list = cur_stmt_list;
18907 if (STATEMENT_LIST_TAIL (list))
18908 last = STATEMENT_LIST_TAIL (list)->stmt;
18910 /* Parse the function-body. */
18911 cp_parser_function_body (parser, in_function_try_block);
18912 if (check_body_p)
18913 check_constexpr_ctor_body (last, list);
18914 /* Finish the function body. */
18915 finish_function_body (body);
18917 return ctor_initializer_p;
18920 /* Parse an initializer.
18922 initializer:
18923 = initializer-clause
18924 ( expression-list )
18926 Returns an expression representing the initializer. If no
18927 initializer is present, NULL_TREE is returned.
18929 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18930 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18931 set to TRUE if there is no initializer present. If there is an
18932 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18933 is set to true; otherwise it is set to false. */
18935 static tree
18936 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18937 bool* non_constant_p)
18939 cp_token *token;
18940 tree init;
18942 /* Peek at the next token. */
18943 token = cp_lexer_peek_token (parser->lexer);
18945 /* Let our caller know whether or not this initializer was
18946 parenthesized. */
18947 *is_direct_init = (token->type != CPP_EQ);
18948 /* Assume that the initializer is constant. */
18949 *non_constant_p = false;
18951 if (token->type == CPP_EQ)
18953 /* Consume the `='. */
18954 cp_lexer_consume_token (parser->lexer);
18955 /* Parse the initializer-clause. */
18956 init = cp_parser_initializer_clause (parser, non_constant_p);
18958 else if (token->type == CPP_OPEN_PAREN)
18960 vec<tree, va_gc> *vec;
18961 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18962 /*cast_p=*/false,
18963 /*allow_expansion_p=*/true,
18964 non_constant_p);
18965 if (vec == NULL)
18966 return error_mark_node;
18967 init = build_tree_list_vec (vec);
18968 release_tree_vector (vec);
18970 else if (token->type == CPP_OPEN_BRACE)
18972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18973 init = cp_parser_braced_list (parser, non_constant_p);
18974 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18976 else
18978 /* Anything else is an error. */
18979 cp_parser_error (parser, "expected initializer");
18980 init = error_mark_node;
18983 return init;
18986 /* Parse an initializer-clause.
18988 initializer-clause:
18989 assignment-expression
18990 braced-init-list
18992 Returns an expression representing the initializer.
18994 If the `assignment-expression' production is used the value
18995 returned is simply a representation for the expression.
18997 Otherwise, calls cp_parser_braced_list. */
18999 static tree
19000 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19002 tree initializer;
19004 /* Assume the expression is constant. */
19005 *non_constant_p = false;
19007 /* If it is not a `{', then we are looking at an
19008 assignment-expression. */
19009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19011 initializer
19012 = cp_parser_constant_expression (parser,
19013 /*allow_non_constant_p=*/true,
19014 non_constant_p);
19016 else
19017 initializer = cp_parser_braced_list (parser, non_constant_p);
19019 return initializer;
19022 /* Parse a brace-enclosed initializer list.
19024 braced-init-list:
19025 { initializer-list , [opt] }
19028 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19029 the elements of the initializer-list (or NULL, if the last
19030 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19031 NULL_TREE. There is no way to detect whether or not the optional
19032 trailing `,' was provided. NON_CONSTANT_P is as for
19033 cp_parser_initializer. */
19035 static tree
19036 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19038 tree initializer;
19040 /* Consume the `{' token. */
19041 cp_lexer_consume_token (parser->lexer);
19042 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19043 initializer = make_node (CONSTRUCTOR);
19044 /* If it's not a `}', then there is a non-trivial initializer. */
19045 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19047 /* Parse the initializer list. */
19048 CONSTRUCTOR_ELTS (initializer)
19049 = cp_parser_initializer_list (parser, non_constant_p);
19050 /* A trailing `,' token is allowed. */
19051 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19052 cp_lexer_consume_token (parser->lexer);
19054 else
19055 *non_constant_p = false;
19056 /* Now, there should be a trailing `}'. */
19057 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19058 TREE_TYPE (initializer) = init_list_type_node;
19059 return initializer;
19062 /* Parse an initializer-list.
19064 initializer-list:
19065 initializer-clause ... [opt]
19066 initializer-list , initializer-clause ... [opt]
19068 GNU Extension:
19070 initializer-list:
19071 designation initializer-clause ...[opt]
19072 initializer-list , designation initializer-clause ...[opt]
19074 designation:
19075 . identifier =
19076 identifier :
19077 [ constant-expression ] =
19079 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19080 for the initializer. If the INDEX of the elt is non-NULL, it is the
19081 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19082 as for cp_parser_initializer. */
19084 static vec<constructor_elt, va_gc> *
19085 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19087 vec<constructor_elt, va_gc> *v = NULL;
19089 /* Assume all of the expressions are constant. */
19090 *non_constant_p = false;
19092 /* Parse the rest of the list. */
19093 while (true)
19095 cp_token *token;
19096 tree designator;
19097 tree initializer;
19098 bool clause_non_constant_p;
19100 /* If the next token is an identifier and the following one is a
19101 colon, we are looking at the GNU designated-initializer
19102 syntax. */
19103 if (cp_parser_allow_gnu_extensions_p (parser)
19104 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19105 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19107 /* Warn the user that they are using an extension. */
19108 pedwarn (input_location, OPT_Wpedantic,
19109 "ISO C++ does not allow designated initializers");
19110 /* Consume the identifier. */
19111 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19112 /* Consume the `:'. */
19113 cp_lexer_consume_token (parser->lexer);
19115 /* Also handle the C99 syntax, '. id ='. */
19116 else if (cp_parser_allow_gnu_extensions_p (parser)
19117 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19118 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19119 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19121 /* Warn the user that they are using an extension. */
19122 pedwarn (input_location, OPT_Wpedantic,
19123 "ISO C++ does not allow C99 designated initializers");
19124 /* Consume the `.'. */
19125 cp_lexer_consume_token (parser->lexer);
19126 /* Consume the identifier. */
19127 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19128 /* Consume the `='. */
19129 cp_lexer_consume_token (parser->lexer);
19131 /* Also handle C99 array designators, '[ const ] ='. */
19132 else if (cp_parser_allow_gnu_extensions_p (parser)
19133 && !c_dialect_objc ()
19134 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19136 /* In C++11, [ could start a lambda-introducer. */
19137 bool non_const = false;
19139 cp_parser_parse_tentatively (parser);
19140 cp_lexer_consume_token (parser->lexer);
19141 designator = cp_parser_constant_expression (parser, true, &non_const);
19142 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19143 cp_parser_require (parser, CPP_EQ, RT_EQ);
19144 if (!cp_parser_parse_definitely (parser))
19145 designator = NULL_TREE;
19146 else if (non_const)
19147 require_potential_rvalue_constant_expression (designator);
19149 else
19150 designator = NULL_TREE;
19152 /* Parse the initializer. */
19153 initializer = cp_parser_initializer_clause (parser,
19154 &clause_non_constant_p);
19155 /* If any clause is non-constant, so is the entire initializer. */
19156 if (clause_non_constant_p)
19157 *non_constant_p = true;
19159 /* If we have an ellipsis, this is an initializer pack
19160 expansion. */
19161 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19163 /* Consume the `...'. */
19164 cp_lexer_consume_token (parser->lexer);
19166 /* Turn the initializer into an initializer expansion. */
19167 initializer = make_pack_expansion (initializer);
19170 /* Add it to the vector. */
19171 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19173 /* If the next token is not a comma, we have reached the end of
19174 the list. */
19175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19176 break;
19178 /* Peek at the next token. */
19179 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19180 /* If the next token is a `}', then we're still done. An
19181 initializer-clause can have a trailing `,' after the
19182 initializer-list and before the closing `}'. */
19183 if (token->type == CPP_CLOSE_BRACE)
19184 break;
19186 /* Consume the `,' token. */
19187 cp_lexer_consume_token (parser->lexer);
19190 return v;
19193 /* Classes [gram.class] */
19195 /* Parse a class-name.
19197 class-name:
19198 identifier
19199 template-id
19201 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19202 to indicate that names looked up in dependent types should be
19203 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19204 keyword has been used to indicate that the name that appears next
19205 is a template. TAG_TYPE indicates the explicit tag given before
19206 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19207 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19208 is the class being defined in a class-head.
19210 Returns the TYPE_DECL representing the class. */
19212 static tree
19213 cp_parser_class_name (cp_parser *parser,
19214 bool typename_keyword_p,
19215 bool template_keyword_p,
19216 enum tag_types tag_type,
19217 bool check_dependency_p,
19218 bool class_head_p,
19219 bool is_declaration)
19221 tree decl;
19222 tree scope;
19223 bool typename_p;
19224 cp_token *token;
19225 tree identifier = NULL_TREE;
19227 /* All class-names start with an identifier. */
19228 token = cp_lexer_peek_token (parser->lexer);
19229 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19231 cp_parser_error (parser, "expected class-name");
19232 return error_mark_node;
19235 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19236 to a template-id, so we save it here. */
19237 scope = parser->scope;
19238 if (scope == error_mark_node)
19239 return error_mark_node;
19241 /* Any name names a type if we're following the `typename' keyword
19242 in a qualified name where the enclosing scope is type-dependent. */
19243 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19244 && dependent_type_p (scope));
19245 /* Handle the common case (an identifier, but not a template-id)
19246 efficiently. */
19247 if (token->type == CPP_NAME
19248 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19250 cp_token *identifier_token;
19251 bool ambiguous_p;
19253 /* Look for the identifier. */
19254 identifier_token = cp_lexer_peek_token (parser->lexer);
19255 ambiguous_p = identifier_token->ambiguous_p;
19256 identifier = cp_parser_identifier (parser);
19257 /* If the next token isn't an identifier, we are certainly not
19258 looking at a class-name. */
19259 if (identifier == error_mark_node)
19260 decl = error_mark_node;
19261 /* If we know this is a type-name, there's no need to look it
19262 up. */
19263 else if (typename_p)
19264 decl = identifier;
19265 else
19267 tree ambiguous_decls;
19268 /* If we already know that this lookup is ambiguous, then
19269 we've already issued an error message; there's no reason
19270 to check again. */
19271 if (ambiguous_p)
19273 cp_parser_simulate_error (parser);
19274 return error_mark_node;
19276 /* If the next token is a `::', then the name must be a type
19277 name.
19279 [basic.lookup.qual]
19281 During the lookup for a name preceding the :: scope
19282 resolution operator, object, function, and enumerator
19283 names are ignored. */
19284 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19285 tag_type = typename_type;
19286 /* Look up the name. */
19287 decl = cp_parser_lookup_name (parser, identifier,
19288 tag_type,
19289 /*is_template=*/false,
19290 /*is_namespace=*/false,
19291 check_dependency_p,
19292 &ambiguous_decls,
19293 identifier_token->location);
19294 if (ambiguous_decls)
19296 if (cp_parser_parsing_tentatively (parser))
19297 cp_parser_simulate_error (parser);
19298 return error_mark_node;
19302 else
19304 /* Try a template-id. */
19305 decl = cp_parser_template_id (parser, template_keyword_p,
19306 check_dependency_p,
19307 tag_type,
19308 is_declaration);
19309 if (decl == error_mark_node)
19310 return error_mark_node;
19313 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19315 /* If this is a typename, create a TYPENAME_TYPE. */
19316 if (typename_p && decl != error_mark_node)
19318 decl = make_typename_type (scope, decl, typename_type,
19319 /*complain=*/tf_error);
19320 if (decl != error_mark_node)
19321 decl = TYPE_NAME (decl);
19324 decl = strip_using_decl (decl);
19326 /* Check to see that it is really the name of a class. */
19327 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19328 && identifier_p (TREE_OPERAND (decl, 0))
19329 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19330 /* Situations like this:
19332 template <typename T> struct A {
19333 typename T::template X<int>::I i;
19336 are problematic. Is `T::template X<int>' a class-name? The
19337 standard does not seem to be definitive, but there is no other
19338 valid interpretation of the following `::'. Therefore, those
19339 names are considered class-names. */
19341 decl = make_typename_type (scope, decl, tag_type, tf_error);
19342 if (decl != error_mark_node)
19343 decl = TYPE_NAME (decl);
19345 else if (TREE_CODE (decl) != TYPE_DECL
19346 || TREE_TYPE (decl) == error_mark_node
19347 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19348 /* In Objective-C 2.0, a classname followed by '.' starts a
19349 dot-syntax expression, and it's not a type-name. */
19350 || (c_dialect_objc ()
19351 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19352 && objc_is_class_name (decl)))
19353 decl = error_mark_node;
19355 if (decl == error_mark_node)
19356 cp_parser_error (parser, "expected class-name");
19357 else if (identifier && !parser->scope)
19358 maybe_note_name_used_in_class (identifier, decl);
19360 return decl;
19363 /* Parse a class-specifier.
19365 class-specifier:
19366 class-head { member-specification [opt] }
19368 Returns the TREE_TYPE representing the class. */
19370 static tree
19371 cp_parser_class_specifier_1 (cp_parser* parser)
19373 tree type;
19374 tree attributes = NULL_TREE;
19375 bool nested_name_specifier_p;
19376 unsigned saved_num_template_parameter_lists;
19377 bool saved_in_function_body;
19378 unsigned char in_statement;
19379 bool in_switch_statement_p;
19380 bool saved_in_unbraced_linkage_specification_p;
19381 tree old_scope = NULL_TREE;
19382 tree scope = NULL_TREE;
19383 cp_token *closing_brace;
19385 push_deferring_access_checks (dk_no_deferred);
19387 /* Parse the class-head. */
19388 type = cp_parser_class_head (parser,
19389 &nested_name_specifier_p);
19390 /* If the class-head was a semantic disaster, skip the entire body
19391 of the class. */
19392 if (!type)
19394 cp_parser_skip_to_end_of_block_or_statement (parser);
19395 pop_deferring_access_checks ();
19396 return error_mark_node;
19399 /* Look for the `{'. */
19400 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19402 pop_deferring_access_checks ();
19403 return error_mark_node;
19406 cp_ensure_no_omp_declare_simd (parser);
19408 /* Issue an error message if type-definitions are forbidden here. */
19409 cp_parser_check_type_definition (parser);
19410 /* Remember that we are defining one more class. */
19411 ++parser->num_classes_being_defined;
19412 /* Inside the class, surrounding template-parameter-lists do not
19413 apply. */
19414 saved_num_template_parameter_lists
19415 = parser->num_template_parameter_lists;
19416 parser->num_template_parameter_lists = 0;
19417 /* We are not in a function body. */
19418 saved_in_function_body = parser->in_function_body;
19419 parser->in_function_body = false;
19420 /* Or in a loop. */
19421 in_statement = parser->in_statement;
19422 parser->in_statement = 0;
19423 /* Or in a switch. */
19424 in_switch_statement_p = parser->in_switch_statement_p;
19425 parser->in_switch_statement_p = false;
19426 /* We are not immediately inside an extern "lang" block. */
19427 saved_in_unbraced_linkage_specification_p
19428 = parser->in_unbraced_linkage_specification_p;
19429 parser->in_unbraced_linkage_specification_p = false;
19431 /* Start the class. */
19432 if (nested_name_specifier_p)
19434 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19435 old_scope = push_inner_scope (scope);
19437 type = begin_class_definition (type);
19439 if (type == error_mark_node)
19440 /* If the type is erroneous, skip the entire body of the class. */
19441 cp_parser_skip_to_closing_brace (parser);
19442 else
19443 /* Parse the member-specification. */
19444 cp_parser_member_specification_opt (parser);
19446 /* Look for the trailing `}'. */
19447 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19448 /* Look for trailing attributes to apply to this class. */
19449 if (cp_parser_allow_gnu_extensions_p (parser))
19450 attributes = cp_parser_gnu_attributes_opt (parser);
19451 if (type != error_mark_node)
19452 type = finish_struct (type, attributes);
19453 if (nested_name_specifier_p)
19454 pop_inner_scope (old_scope, scope);
19456 /* We've finished a type definition. Check for the common syntax
19457 error of forgetting a semicolon after the definition. We need to
19458 be careful, as we can't just check for not-a-semicolon and be done
19459 with it; the user might have typed:
19461 class X { } c = ...;
19462 class X { } *p = ...;
19464 and so forth. Instead, enumerate all the possible tokens that
19465 might follow this production; if we don't see one of them, then
19466 complain and silently insert the semicolon. */
19468 cp_token *token = cp_lexer_peek_token (parser->lexer);
19469 bool want_semicolon = true;
19471 if (cp_next_tokens_can_be_std_attribute_p (parser))
19472 /* Don't try to parse c++11 attributes here. As per the
19473 grammar, that should be a task for
19474 cp_parser_decl_specifier_seq. */
19475 want_semicolon = false;
19477 switch (token->type)
19479 case CPP_NAME:
19480 case CPP_SEMICOLON:
19481 case CPP_MULT:
19482 case CPP_AND:
19483 case CPP_OPEN_PAREN:
19484 case CPP_CLOSE_PAREN:
19485 case CPP_COMMA:
19486 want_semicolon = false;
19487 break;
19489 /* While it's legal for type qualifiers and storage class
19490 specifiers to follow type definitions in the grammar, only
19491 compiler testsuites contain code like that. Assume that if
19492 we see such code, then what we're really seeing is a case
19493 like:
19495 class X { }
19496 const <type> var = ...;
19500 class Y { }
19501 static <type> func (...) ...
19503 i.e. the qualifier or specifier applies to the next
19504 declaration. To do so, however, we need to look ahead one
19505 more token to see if *that* token is a type specifier.
19507 This code could be improved to handle:
19509 class Z { }
19510 static const <type> var = ...; */
19511 case CPP_KEYWORD:
19512 if (keyword_is_decl_specifier (token->keyword))
19514 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19516 /* Handling user-defined types here would be nice, but very
19517 tricky. */
19518 want_semicolon
19519 = (lookahead->type == CPP_KEYWORD
19520 && keyword_begins_type_specifier (lookahead->keyword));
19522 break;
19523 default:
19524 break;
19527 /* If we don't have a type, then something is very wrong and we
19528 shouldn't try to do anything clever. Likewise for not seeing the
19529 closing brace. */
19530 if (closing_brace && TYPE_P (type) && want_semicolon)
19532 cp_token_position prev
19533 = cp_lexer_previous_token_position (parser->lexer);
19534 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19535 location_t loc = prev_token->location;
19537 if (CLASSTYPE_DECLARED_CLASS (type))
19538 error_at (loc, "expected %<;%> after class definition");
19539 else if (TREE_CODE (type) == RECORD_TYPE)
19540 error_at (loc, "expected %<;%> after struct definition");
19541 else if (TREE_CODE (type) == UNION_TYPE)
19542 error_at (loc, "expected %<;%> after union definition");
19543 else
19544 gcc_unreachable ();
19546 /* Unget one token and smash it to look as though we encountered
19547 a semicolon in the input stream. */
19548 cp_lexer_set_token_position (parser->lexer, prev);
19549 token = cp_lexer_peek_token (parser->lexer);
19550 token->type = CPP_SEMICOLON;
19551 token->keyword = RID_MAX;
19555 /* If this class is not itself within the scope of another class,
19556 then we need to parse the bodies of all of the queued function
19557 definitions. Note that the queued functions defined in a class
19558 are not always processed immediately following the
19559 class-specifier for that class. Consider:
19561 struct A {
19562 struct B { void f() { sizeof (A); } };
19565 If `f' were processed before the processing of `A' were
19566 completed, there would be no way to compute the size of `A'.
19567 Note that the nesting we are interested in here is lexical --
19568 not the semantic nesting given by TYPE_CONTEXT. In particular,
19569 for:
19571 struct A { struct B; };
19572 struct A::B { void f() { } };
19574 there is no need to delay the parsing of `A::B::f'. */
19575 if (--parser->num_classes_being_defined == 0)
19577 tree decl;
19578 tree class_type = NULL_TREE;
19579 tree pushed_scope = NULL_TREE;
19580 unsigned ix;
19581 cp_default_arg_entry *e;
19582 tree save_ccp, save_ccr;
19584 /* In a first pass, parse default arguments to the functions.
19585 Then, in a second pass, parse the bodies of the functions.
19586 This two-phased approach handles cases like:
19588 struct S {
19589 void f() { g(); }
19590 void g(int i = 3);
19594 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19596 decl = e->decl;
19597 /* If there are default arguments that have not yet been processed,
19598 take care of them now. */
19599 if (class_type != e->class_type)
19601 if (pushed_scope)
19602 pop_scope (pushed_scope);
19603 class_type = e->class_type;
19604 pushed_scope = push_scope (class_type);
19606 /* Make sure that any template parameters are in scope. */
19607 maybe_begin_member_template_processing (decl);
19608 /* Parse the default argument expressions. */
19609 cp_parser_late_parsing_default_args (parser, decl);
19610 /* Remove any template parameters from the symbol table. */
19611 maybe_end_member_template_processing ();
19613 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19614 /* Now parse any NSDMIs. */
19615 save_ccp = current_class_ptr;
19616 save_ccr = current_class_ref;
19617 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19619 if (class_type != DECL_CONTEXT (decl))
19621 if (pushed_scope)
19622 pop_scope (pushed_scope);
19623 class_type = DECL_CONTEXT (decl);
19624 pushed_scope = push_scope (class_type);
19626 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19627 cp_parser_late_parsing_nsdmi (parser, decl);
19629 vec_safe_truncate (unparsed_nsdmis, 0);
19630 current_class_ptr = save_ccp;
19631 current_class_ref = save_ccr;
19632 if (pushed_scope)
19633 pop_scope (pushed_scope);
19634 /* Now parse the body of the functions. */
19635 if (flag_openmp)
19637 /* OpenMP UDRs need to be parsed before all other functions. */
19638 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19639 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19640 cp_parser_late_parsing_for_member (parser, decl);
19641 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19642 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19643 cp_parser_late_parsing_for_member (parser, decl);
19645 else
19646 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19647 cp_parser_late_parsing_for_member (parser, decl);
19648 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19651 /* Put back any saved access checks. */
19652 pop_deferring_access_checks ();
19654 /* Restore saved state. */
19655 parser->in_switch_statement_p = in_switch_statement_p;
19656 parser->in_statement = in_statement;
19657 parser->in_function_body = saved_in_function_body;
19658 parser->num_template_parameter_lists
19659 = saved_num_template_parameter_lists;
19660 parser->in_unbraced_linkage_specification_p
19661 = saved_in_unbraced_linkage_specification_p;
19663 return type;
19666 static tree
19667 cp_parser_class_specifier (cp_parser* parser)
19669 tree ret;
19670 timevar_push (TV_PARSE_STRUCT);
19671 ret = cp_parser_class_specifier_1 (parser);
19672 timevar_pop (TV_PARSE_STRUCT);
19673 return ret;
19676 /* Parse a class-head.
19678 class-head:
19679 class-key identifier [opt] base-clause [opt]
19680 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19681 class-key nested-name-specifier [opt] template-id
19682 base-clause [opt]
19684 class-virt-specifier:
19685 final
19687 GNU Extensions:
19688 class-key attributes identifier [opt] base-clause [opt]
19689 class-key attributes nested-name-specifier identifier base-clause [opt]
19690 class-key attributes nested-name-specifier [opt] template-id
19691 base-clause [opt]
19693 Upon return BASES is initialized to the list of base classes (or
19694 NULL, if there are none) in the same form returned by
19695 cp_parser_base_clause.
19697 Returns the TYPE of the indicated class. Sets
19698 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19699 involving a nested-name-specifier was used, and FALSE otherwise.
19701 Returns error_mark_node if this is not a class-head.
19703 Returns NULL_TREE if the class-head is syntactically valid, but
19704 semantically invalid in a way that means we should skip the entire
19705 body of the class. */
19707 static tree
19708 cp_parser_class_head (cp_parser* parser,
19709 bool* nested_name_specifier_p)
19711 tree nested_name_specifier;
19712 enum tag_types class_key;
19713 tree id = NULL_TREE;
19714 tree type = NULL_TREE;
19715 tree attributes;
19716 tree bases;
19717 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19718 bool template_id_p = false;
19719 bool qualified_p = false;
19720 bool invalid_nested_name_p = false;
19721 bool invalid_explicit_specialization_p = false;
19722 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19723 tree pushed_scope = NULL_TREE;
19724 unsigned num_templates;
19725 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19726 /* Assume no nested-name-specifier will be present. */
19727 *nested_name_specifier_p = false;
19728 /* Assume no template parameter lists will be used in defining the
19729 type. */
19730 num_templates = 0;
19731 parser->colon_corrects_to_scope_p = false;
19733 /* Look for the class-key. */
19734 class_key = cp_parser_class_key (parser);
19735 if (class_key == none_type)
19736 return error_mark_node;
19738 /* Parse the attributes. */
19739 attributes = cp_parser_attributes_opt (parser);
19741 /* If the next token is `::', that is invalid -- but sometimes
19742 people do try to write:
19744 struct ::S {};
19746 Handle this gracefully by accepting the extra qualifier, and then
19747 issuing an error about it later if this really is a
19748 class-head. If it turns out just to be an elaborated type
19749 specifier, remain silent. */
19750 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19751 qualified_p = true;
19753 push_deferring_access_checks (dk_no_check);
19755 /* Determine the name of the class. Begin by looking for an
19756 optional nested-name-specifier. */
19757 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19758 nested_name_specifier
19759 = cp_parser_nested_name_specifier_opt (parser,
19760 /*typename_keyword_p=*/false,
19761 /*check_dependency_p=*/false,
19762 /*type_p=*/true,
19763 /*is_declaration=*/false);
19764 /* If there was a nested-name-specifier, then there *must* be an
19765 identifier. */
19766 if (nested_name_specifier)
19768 type_start_token = cp_lexer_peek_token (parser->lexer);
19769 /* Although the grammar says `identifier', it really means
19770 `class-name' or `template-name'. You are only allowed to
19771 define a class that has already been declared with this
19772 syntax.
19774 The proposed resolution for Core Issue 180 says that wherever
19775 you see `class T::X' you should treat `X' as a type-name.
19777 It is OK to define an inaccessible class; for example:
19779 class A { class B; };
19780 class A::B {};
19782 We do not know if we will see a class-name, or a
19783 template-name. We look for a class-name first, in case the
19784 class-name is a template-id; if we looked for the
19785 template-name first we would stop after the template-name. */
19786 cp_parser_parse_tentatively (parser);
19787 type = cp_parser_class_name (parser,
19788 /*typename_keyword_p=*/false,
19789 /*template_keyword_p=*/false,
19790 class_type,
19791 /*check_dependency_p=*/false,
19792 /*class_head_p=*/true,
19793 /*is_declaration=*/false);
19794 /* If that didn't work, ignore the nested-name-specifier. */
19795 if (!cp_parser_parse_definitely (parser))
19797 invalid_nested_name_p = true;
19798 type_start_token = cp_lexer_peek_token (parser->lexer);
19799 id = cp_parser_identifier (parser);
19800 if (id == error_mark_node)
19801 id = NULL_TREE;
19803 /* If we could not find a corresponding TYPE, treat this
19804 declaration like an unqualified declaration. */
19805 if (type == error_mark_node)
19806 nested_name_specifier = NULL_TREE;
19807 /* Otherwise, count the number of templates used in TYPE and its
19808 containing scopes. */
19809 else
19811 tree scope;
19813 for (scope = TREE_TYPE (type);
19814 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19815 scope = get_containing_scope (scope))
19816 if (TYPE_P (scope)
19817 && CLASS_TYPE_P (scope)
19818 && CLASSTYPE_TEMPLATE_INFO (scope)
19819 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19820 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19821 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19822 ++num_templates;
19825 /* Otherwise, the identifier is optional. */
19826 else
19828 /* We don't know whether what comes next is a template-id,
19829 an identifier, or nothing at all. */
19830 cp_parser_parse_tentatively (parser);
19831 /* Check for a template-id. */
19832 type_start_token = cp_lexer_peek_token (parser->lexer);
19833 id = cp_parser_template_id (parser,
19834 /*template_keyword_p=*/false,
19835 /*check_dependency_p=*/true,
19836 class_key,
19837 /*is_declaration=*/true);
19838 /* If that didn't work, it could still be an identifier. */
19839 if (!cp_parser_parse_definitely (parser))
19841 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19843 type_start_token = cp_lexer_peek_token (parser->lexer);
19844 id = cp_parser_identifier (parser);
19846 else
19847 id = NULL_TREE;
19849 else
19851 template_id_p = true;
19852 ++num_templates;
19856 pop_deferring_access_checks ();
19858 if (id)
19860 cp_parser_check_for_invalid_template_id (parser, id,
19861 class_key,
19862 type_start_token->location);
19864 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19866 /* If it's not a `:' or a `{' then we can't really be looking at a
19867 class-head, since a class-head only appears as part of a
19868 class-specifier. We have to detect this situation before calling
19869 xref_tag, since that has irreversible side-effects. */
19870 if (!cp_parser_next_token_starts_class_definition_p (parser))
19872 cp_parser_error (parser, "expected %<{%> or %<:%>");
19873 type = error_mark_node;
19874 goto out;
19877 /* At this point, we're going ahead with the class-specifier, even
19878 if some other problem occurs. */
19879 cp_parser_commit_to_tentative_parse (parser);
19880 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19882 cp_parser_error (parser,
19883 "cannot specify %<override%> for a class");
19884 type = error_mark_node;
19885 goto out;
19887 /* Issue the error about the overly-qualified name now. */
19888 if (qualified_p)
19890 cp_parser_error (parser,
19891 "global qualification of class name is invalid");
19892 type = error_mark_node;
19893 goto out;
19895 else if (invalid_nested_name_p)
19897 cp_parser_error (parser,
19898 "qualified name does not name a class");
19899 type = error_mark_node;
19900 goto out;
19902 else if (nested_name_specifier)
19904 tree scope;
19906 /* Reject typedef-names in class heads. */
19907 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19909 error_at (type_start_token->location,
19910 "invalid class name in declaration of %qD",
19911 type);
19912 type = NULL_TREE;
19913 goto done;
19916 /* Figure out in what scope the declaration is being placed. */
19917 scope = current_scope ();
19918 /* If that scope does not contain the scope in which the
19919 class was originally declared, the program is invalid. */
19920 if (scope && !is_ancestor (scope, nested_name_specifier))
19922 if (at_namespace_scope_p ())
19923 error_at (type_start_token->location,
19924 "declaration of %qD in namespace %qD which does not "
19925 "enclose %qD",
19926 type, scope, nested_name_specifier);
19927 else
19928 error_at (type_start_token->location,
19929 "declaration of %qD in %qD which does not enclose %qD",
19930 type, scope, nested_name_specifier);
19931 type = NULL_TREE;
19932 goto done;
19934 /* [dcl.meaning]
19936 A declarator-id shall not be qualified except for the
19937 definition of a ... nested class outside of its class
19938 ... [or] the definition or explicit instantiation of a
19939 class member of a namespace outside of its namespace. */
19940 if (scope == nested_name_specifier)
19942 permerror (nested_name_specifier_token_start->location,
19943 "extra qualification not allowed");
19944 nested_name_specifier = NULL_TREE;
19945 num_templates = 0;
19948 /* An explicit-specialization must be preceded by "template <>". If
19949 it is not, try to recover gracefully. */
19950 if (at_namespace_scope_p ()
19951 && parser->num_template_parameter_lists == 0
19952 && template_id_p)
19954 error_at (type_start_token->location,
19955 "an explicit specialization must be preceded by %<template <>%>");
19956 invalid_explicit_specialization_p = true;
19957 /* Take the same action that would have been taken by
19958 cp_parser_explicit_specialization. */
19959 ++parser->num_template_parameter_lists;
19960 begin_specialization ();
19962 /* There must be no "return" statements between this point and the
19963 end of this function; set "type "to the correct return value and
19964 use "goto done;" to return. */
19965 /* Make sure that the right number of template parameters were
19966 present. */
19967 if (!cp_parser_check_template_parameters (parser, num_templates,
19968 type_start_token->location,
19969 /*declarator=*/NULL))
19971 /* If something went wrong, there is no point in even trying to
19972 process the class-definition. */
19973 type = NULL_TREE;
19974 goto done;
19977 /* Look up the type. */
19978 if (template_id_p)
19980 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19981 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19982 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19984 error_at (type_start_token->location,
19985 "function template %qD redeclared as a class template", id);
19986 type = error_mark_node;
19988 else
19990 type = TREE_TYPE (id);
19991 type = maybe_process_partial_specialization (type);
19993 if (nested_name_specifier)
19994 pushed_scope = push_scope (nested_name_specifier);
19996 else if (nested_name_specifier)
19998 tree class_type;
20000 /* Given:
20002 template <typename T> struct S { struct T };
20003 template <typename T> struct S<T>::T { };
20005 we will get a TYPENAME_TYPE when processing the definition of
20006 `S::T'. We need to resolve it to the actual type before we
20007 try to define it. */
20008 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20010 class_type = resolve_typename_type (TREE_TYPE (type),
20011 /*only_current_p=*/false);
20012 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20013 type = TYPE_NAME (class_type);
20014 else
20016 cp_parser_error (parser, "could not resolve typename type");
20017 type = error_mark_node;
20021 if (maybe_process_partial_specialization (TREE_TYPE (type))
20022 == error_mark_node)
20024 type = NULL_TREE;
20025 goto done;
20028 class_type = current_class_type;
20029 /* Enter the scope indicated by the nested-name-specifier. */
20030 pushed_scope = push_scope (nested_name_specifier);
20031 /* Get the canonical version of this type. */
20032 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20033 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
20034 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20036 type = push_template_decl (type);
20037 if (type == error_mark_node)
20039 type = NULL_TREE;
20040 goto done;
20044 type = TREE_TYPE (type);
20045 *nested_name_specifier_p = true;
20047 else /* The name is not a nested name. */
20049 /* If the class was unnamed, create a dummy name. */
20050 if (!id)
20051 id = make_anon_name ();
20052 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20053 parser->num_template_parameter_lists);
20056 /* Indicate whether this class was declared as a `class' or as a
20057 `struct'. */
20058 if (TREE_CODE (type) == RECORD_TYPE)
20059 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20060 cp_parser_check_class_key (class_key, type);
20062 /* If this type was already complete, and we see another definition,
20063 that's an error. */
20064 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20066 error_at (type_start_token->location, "redefinition of %q#T",
20067 type);
20068 error_at (type_start_token->location, "previous definition of %q+#T",
20069 type);
20070 type = NULL_TREE;
20071 goto done;
20073 else if (type == error_mark_node)
20074 type = NULL_TREE;
20076 if (type)
20078 /* Apply attributes now, before any use of the class as a template
20079 argument in its base list. */
20080 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20081 fixup_attribute_variants (type);
20084 /* We will have entered the scope containing the class; the names of
20085 base classes should be looked up in that context. For example:
20087 struct A { struct B {}; struct C; };
20088 struct A::C : B {};
20090 is valid. */
20092 /* Get the list of base-classes, if there is one. */
20093 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20094 bases = cp_parser_base_clause (parser);
20095 else
20096 bases = NULL_TREE;
20098 /* If we're really defining a class, process the base classes.
20099 If they're invalid, fail. */
20100 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20101 && !xref_basetypes (type, bases))
20102 type = NULL_TREE;
20104 done:
20105 /* Leave the scope given by the nested-name-specifier. We will
20106 enter the class scope itself while processing the members. */
20107 if (pushed_scope)
20108 pop_scope (pushed_scope);
20110 if (invalid_explicit_specialization_p)
20112 end_specialization ();
20113 --parser->num_template_parameter_lists;
20116 if (type)
20117 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20118 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20119 CLASSTYPE_FINAL (type) = 1;
20120 out:
20121 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20122 return type;
20125 /* Parse a class-key.
20127 class-key:
20128 class
20129 struct
20130 union
20132 Returns the kind of class-key specified, or none_type to indicate
20133 error. */
20135 static enum tag_types
20136 cp_parser_class_key (cp_parser* parser)
20138 cp_token *token;
20139 enum tag_types tag_type;
20141 /* Look for the class-key. */
20142 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20143 if (!token)
20144 return none_type;
20146 /* Check to see if the TOKEN is a class-key. */
20147 tag_type = cp_parser_token_is_class_key (token);
20148 if (!tag_type)
20149 cp_parser_error (parser, "expected class-key");
20150 return tag_type;
20153 /* Parse an (optional) member-specification.
20155 member-specification:
20156 member-declaration member-specification [opt]
20157 access-specifier : member-specification [opt] */
20159 static void
20160 cp_parser_member_specification_opt (cp_parser* parser)
20162 while (true)
20164 cp_token *token;
20165 enum rid keyword;
20167 /* Peek at the next token. */
20168 token = cp_lexer_peek_token (parser->lexer);
20169 /* If it's a `}', or EOF then we've seen all the members. */
20170 if (token->type == CPP_CLOSE_BRACE
20171 || token->type == CPP_EOF
20172 || token->type == CPP_PRAGMA_EOL)
20173 break;
20175 /* See if this token is a keyword. */
20176 keyword = token->keyword;
20177 switch (keyword)
20179 case RID_PUBLIC:
20180 case RID_PROTECTED:
20181 case RID_PRIVATE:
20182 /* Consume the access-specifier. */
20183 cp_lexer_consume_token (parser->lexer);
20184 /* Remember which access-specifier is active. */
20185 current_access_specifier = token->u.value;
20186 /* Look for the `:'. */
20187 cp_parser_require (parser, CPP_COLON, RT_COLON);
20188 break;
20190 default:
20191 /* Accept #pragmas at class scope. */
20192 if (token->type == CPP_PRAGMA)
20194 cp_parser_pragma (parser, pragma_member);
20195 break;
20198 /* Otherwise, the next construction must be a
20199 member-declaration. */
20200 cp_parser_member_declaration (parser);
20205 /* Parse a member-declaration.
20207 member-declaration:
20208 decl-specifier-seq [opt] member-declarator-list [opt] ;
20209 function-definition ; [opt]
20210 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20211 using-declaration
20212 template-declaration
20213 alias-declaration
20215 member-declarator-list:
20216 member-declarator
20217 member-declarator-list , member-declarator
20219 member-declarator:
20220 declarator pure-specifier [opt]
20221 declarator constant-initializer [opt]
20222 identifier [opt] : constant-expression
20224 GNU Extensions:
20226 member-declaration:
20227 __extension__ member-declaration
20229 member-declarator:
20230 declarator attributes [opt] pure-specifier [opt]
20231 declarator attributes [opt] constant-initializer [opt]
20232 identifier [opt] attributes [opt] : constant-expression
20234 C++0x Extensions:
20236 member-declaration:
20237 static_assert-declaration */
20239 static void
20240 cp_parser_member_declaration (cp_parser* parser)
20242 cp_decl_specifier_seq decl_specifiers;
20243 tree prefix_attributes;
20244 tree decl;
20245 int declares_class_or_enum;
20246 bool friend_p;
20247 cp_token *token = NULL;
20248 cp_token *decl_spec_token_start = NULL;
20249 cp_token *initializer_token_start = NULL;
20250 int saved_pedantic;
20251 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20253 /* Check for the `__extension__' keyword. */
20254 if (cp_parser_extension_opt (parser, &saved_pedantic))
20256 /* Recurse. */
20257 cp_parser_member_declaration (parser);
20258 /* Restore the old value of the PEDANTIC flag. */
20259 pedantic = saved_pedantic;
20261 return;
20264 /* Check for a template-declaration. */
20265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20267 /* An explicit specialization here is an error condition, and we
20268 expect the specialization handler to detect and report this. */
20269 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20270 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20271 cp_parser_explicit_specialization (parser);
20272 else
20273 cp_parser_template_declaration (parser, /*member_p=*/true);
20275 return;
20278 /* Check for a using-declaration. */
20279 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20281 if (cxx_dialect < cxx11)
20283 /* Parse the using-declaration. */
20284 cp_parser_using_declaration (parser,
20285 /*access_declaration_p=*/false);
20286 return;
20288 else
20290 tree decl;
20291 bool alias_decl_expected;
20292 cp_parser_parse_tentatively (parser);
20293 decl = cp_parser_alias_declaration (parser);
20294 /* Note that if we actually see the '=' token after the
20295 identifier, cp_parser_alias_declaration commits the
20296 tentative parse. In that case, we really expects an
20297 alias-declaration. Otherwise, we expect a using
20298 declaration. */
20299 alias_decl_expected =
20300 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20301 cp_parser_parse_definitely (parser);
20303 if (alias_decl_expected)
20304 finish_member_declaration (decl);
20305 else
20306 cp_parser_using_declaration (parser,
20307 /*access_declaration_p=*/false);
20308 return;
20312 /* Check for @defs. */
20313 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20315 tree ivar, member;
20316 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20317 ivar = ivar_chains;
20318 while (ivar)
20320 member = ivar;
20321 ivar = TREE_CHAIN (member);
20322 TREE_CHAIN (member) = NULL_TREE;
20323 finish_member_declaration (member);
20325 return;
20328 /* If the next token is `static_assert' we have a static assertion. */
20329 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20331 cp_parser_static_assert (parser, /*member_p=*/true);
20332 return;
20335 parser->colon_corrects_to_scope_p = false;
20337 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20338 goto out;
20340 /* Parse the decl-specifier-seq. */
20341 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20342 cp_parser_decl_specifier_seq (parser,
20343 CP_PARSER_FLAGS_OPTIONAL,
20344 &decl_specifiers,
20345 &declares_class_or_enum);
20346 /* Check for an invalid type-name. */
20347 if (!decl_specifiers.any_type_specifiers_p
20348 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20349 goto out;
20350 /* If there is no declarator, then the decl-specifier-seq should
20351 specify a type. */
20352 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20354 /* If there was no decl-specifier-seq, and the next token is a
20355 `;', then we have something like:
20357 struct S { ; };
20359 [class.mem]
20361 Each member-declaration shall declare at least one member
20362 name of the class. */
20363 if (!decl_specifiers.any_specifiers_p)
20365 cp_token *token = cp_lexer_peek_token (parser->lexer);
20366 if (!in_system_header_at (token->location))
20367 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20369 else
20371 tree type;
20373 /* See if this declaration is a friend. */
20374 friend_p = cp_parser_friend_p (&decl_specifiers);
20375 /* If there were decl-specifiers, check to see if there was
20376 a class-declaration. */
20377 type = check_tag_decl (&decl_specifiers,
20378 /*explicit_type_instantiation_p=*/false);
20379 /* Nested classes have already been added to the class, but
20380 a `friend' needs to be explicitly registered. */
20381 if (friend_p)
20383 /* If the `friend' keyword was present, the friend must
20384 be introduced with a class-key. */
20385 if (!declares_class_or_enum && cxx_dialect < cxx11)
20386 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20387 "in C++03 a class-key must be used "
20388 "when declaring a friend");
20389 /* In this case:
20391 template <typename T> struct A {
20392 friend struct A<T>::B;
20395 A<T>::B will be represented by a TYPENAME_TYPE, and
20396 therefore not recognized by check_tag_decl. */
20397 if (!type)
20399 type = decl_specifiers.type;
20400 if (type && TREE_CODE (type) == TYPE_DECL)
20401 type = TREE_TYPE (type);
20403 if (!type || !TYPE_P (type))
20404 error_at (decl_spec_token_start->location,
20405 "friend declaration does not name a class or "
20406 "function");
20407 else
20408 make_friend_class (current_class_type, type,
20409 /*complain=*/true);
20411 /* If there is no TYPE, an error message will already have
20412 been issued. */
20413 else if (!type || type == error_mark_node)
20415 /* An anonymous aggregate has to be handled specially; such
20416 a declaration really declares a data member (with a
20417 particular type), as opposed to a nested class. */
20418 else if (ANON_AGGR_TYPE_P (type))
20420 /* C++11 9.5/6. */
20421 if (decl_specifiers.storage_class != sc_none)
20422 error_at (decl_spec_token_start->location,
20423 "a storage class on an anonymous aggregate "
20424 "in class scope is not allowed");
20426 /* Remove constructors and such from TYPE, now that we
20427 know it is an anonymous aggregate. */
20428 fixup_anonymous_aggr (type);
20429 /* And make the corresponding data member. */
20430 decl = build_decl (decl_spec_token_start->location,
20431 FIELD_DECL, NULL_TREE, type);
20432 /* Add it to the class. */
20433 finish_member_declaration (decl);
20435 else
20436 cp_parser_check_access_in_redeclaration
20437 (TYPE_NAME (type),
20438 decl_spec_token_start->location);
20441 else
20443 bool assume_semicolon = false;
20445 /* Clear attributes from the decl_specifiers but keep them
20446 around as prefix attributes that apply them to the entity
20447 being declared. */
20448 prefix_attributes = decl_specifiers.attributes;
20449 decl_specifiers.attributes = NULL_TREE;
20451 /* See if these declarations will be friends. */
20452 friend_p = cp_parser_friend_p (&decl_specifiers);
20454 /* Keep going until we hit the `;' at the end of the
20455 declaration. */
20456 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20458 tree attributes = NULL_TREE;
20459 tree first_attribute;
20461 /* Peek at the next token. */
20462 token = cp_lexer_peek_token (parser->lexer);
20464 /* Check for a bitfield declaration. */
20465 if (token->type == CPP_COLON
20466 || (token->type == CPP_NAME
20467 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20468 == CPP_COLON))
20470 tree identifier;
20471 tree width;
20473 /* Get the name of the bitfield. Note that we cannot just
20474 check TOKEN here because it may have been invalidated by
20475 the call to cp_lexer_peek_nth_token above. */
20476 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20477 identifier = cp_parser_identifier (parser);
20478 else
20479 identifier = NULL_TREE;
20481 /* Consume the `:' token. */
20482 cp_lexer_consume_token (parser->lexer);
20483 /* Get the width of the bitfield. */
20484 width
20485 = cp_parser_constant_expression (parser,
20486 /*allow_non_constant=*/false,
20487 NULL);
20489 /* Look for attributes that apply to the bitfield. */
20490 attributes = cp_parser_attributes_opt (parser);
20491 /* Remember which attributes are prefix attributes and
20492 which are not. */
20493 first_attribute = attributes;
20494 /* Combine the attributes. */
20495 attributes = chainon (prefix_attributes, attributes);
20497 /* Create the bitfield declaration. */
20498 decl = grokbitfield (identifier
20499 ? make_id_declarator (NULL_TREE,
20500 identifier,
20501 sfk_none)
20502 : NULL,
20503 &decl_specifiers,
20504 width,
20505 attributes);
20507 else
20509 cp_declarator *declarator;
20510 tree initializer;
20511 tree asm_specification;
20512 int ctor_dtor_or_conv_p;
20514 /* Parse the declarator. */
20515 declarator
20516 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20517 &ctor_dtor_or_conv_p,
20518 /*parenthesized_p=*/NULL,
20519 /*member_p=*/true);
20521 /* If something went wrong parsing the declarator, make sure
20522 that we at least consume some tokens. */
20523 if (declarator == cp_error_declarator)
20525 /* Skip to the end of the statement. */
20526 cp_parser_skip_to_end_of_statement (parser);
20527 /* If the next token is not a semicolon, that is
20528 probably because we just skipped over the body of
20529 a function. So, we consume a semicolon if
20530 present, but do not issue an error message if it
20531 is not present. */
20532 if (cp_lexer_next_token_is (parser->lexer,
20533 CPP_SEMICOLON))
20534 cp_lexer_consume_token (parser->lexer);
20535 goto out;
20538 if (declares_class_or_enum & 2)
20539 cp_parser_check_for_definition_in_return_type
20540 (declarator, decl_specifiers.type,
20541 decl_specifiers.locations[ds_type_spec]);
20543 /* Look for an asm-specification. */
20544 asm_specification = cp_parser_asm_specification_opt (parser);
20545 /* Look for attributes that apply to the declaration. */
20546 attributes = cp_parser_attributes_opt (parser);
20547 /* Remember which attributes are prefix attributes and
20548 which are not. */
20549 first_attribute = attributes;
20550 /* Combine the attributes. */
20551 attributes = chainon (prefix_attributes, attributes);
20553 /* If it's an `=', then we have a constant-initializer or a
20554 pure-specifier. It is not correct to parse the
20555 initializer before registering the member declaration
20556 since the member declaration should be in scope while
20557 its initializer is processed. However, the rest of the
20558 front end does not yet provide an interface that allows
20559 us to handle this correctly. */
20560 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20562 /* In [class.mem]:
20564 A pure-specifier shall be used only in the declaration of
20565 a virtual function.
20567 A member-declarator can contain a constant-initializer
20568 only if it declares a static member of integral or
20569 enumeration type.
20571 Therefore, if the DECLARATOR is for a function, we look
20572 for a pure-specifier; otherwise, we look for a
20573 constant-initializer. When we call `grokfield', it will
20574 perform more stringent semantics checks. */
20575 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20576 if (function_declarator_p (declarator)
20577 || (decl_specifiers.type
20578 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20579 && declarator->kind == cdk_id
20580 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20581 == FUNCTION_TYPE)))
20582 initializer = cp_parser_pure_specifier (parser);
20583 else if (decl_specifiers.storage_class != sc_static)
20584 initializer = cp_parser_save_nsdmi (parser);
20585 else if (cxx_dialect >= cxx11)
20587 bool nonconst;
20588 /* Don't require a constant rvalue in C++11, since we
20589 might want a reference constant. We'll enforce
20590 constancy later. */
20591 cp_lexer_consume_token (parser->lexer);
20592 /* Parse the initializer. */
20593 initializer = cp_parser_initializer_clause (parser,
20594 &nonconst);
20596 else
20597 /* Parse the initializer. */
20598 initializer = cp_parser_constant_initializer (parser);
20600 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20601 && !function_declarator_p (declarator))
20603 bool x;
20604 if (decl_specifiers.storage_class != sc_static)
20605 initializer = cp_parser_save_nsdmi (parser);
20606 else
20607 initializer = cp_parser_initializer (parser, &x, &x);
20609 /* Otherwise, there is no initializer. */
20610 else
20611 initializer = NULL_TREE;
20613 // If we're looking at a member function declaration, then a
20614 // requires clause may follow the declaration.
20615 tree saved_template_reqs = release (current_template_reqs);
20616 if (flag_concepts && function_declarator_p (declarator))
20618 // Because this is a non-template member, there are no
20619 // template requirements to conjoin.
20620 if (tree r = cp_parser_requires_clause_opt (parser))
20621 current_template_reqs = finish_template_requirements (r);
20624 /* See if we are probably looking at a function
20625 definition. We are certainly not looking at a
20626 member-declarator. Calling `grokfield' has
20627 side-effects, so we must not do it unless we are sure
20628 that we are looking at a member-declarator. */
20629 if (cp_parser_token_starts_function_definition_p
20630 (cp_lexer_peek_token (parser->lexer)))
20632 /* The grammar does not allow a pure-specifier to be
20633 used when a member function is defined. (It is
20634 possible that this fact is an oversight in the
20635 standard, since a pure function may be defined
20636 outside of the class-specifier. */
20637 if (initializer && initializer_token_start)
20638 error_at (initializer_token_start->location,
20639 "pure-specifier on function-definition");
20640 decl = cp_parser_save_member_function_body (parser,
20641 &decl_specifiers,
20642 declarator,
20643 attributes);
20644 /* If the member was not a friend, declare it here. */
20645 if (!friend_p)
20647 if (parser->fully_implicit_function_template_p)
20648 decl = finish_fully_implicit_template (parser, decl);
20649 finish_member_declaration (decl);
20652 if (friend_p)
20653 check_constrained_friend (decl, current_template_reqs);
20655 /* Peek at the next token. */
20656 token = cp_lexer_peek_token (parser->lexer);
20657 /* If the next token is a semicolon, consume it. */
20658 if (token->type == CPP_SEMICOLON)
20659 cp_lexer_consume_token (parser->lexer);
20661 // Restore the current template requirements.
20662 current_template_reqs = saved_template_reqs;
20664 goto out;
20666 else
20667 if (declarator->kind == cdk_function)
20668 declarator->id_loc = token->location;
20669 /* Create the declaration. */
20670 decl = grokfield (declarator, &decl_specifiers,
20671 initializer, /*init_const_expr_p=*/true,
20672 asm_specification, attributes);
20673 if (parser->fully_implicit_function_template_p)
20674 decl = finish_fully_implicit_template (parser, decl);
20676 // Restore the current template requirments.
20677 current_template_reqs = saved_template_reqs;
20680 cp_finalize_omp_declare_simd (parser, decl);
20682 /* Reset PREFIX_ATTRIBUTES. */
20683 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20684 attributes = TREE_CHAIN (attributes);
20685 if (attributes)
20686 TREE_CHAIN (attributes) = NULL_TREE;
20688 /* If there is any qualification still in effect, clear it
20689 now; we will be starting fresh with the next declarator. */
20690 parser->scope = NULL_TREE;
20691 parser->qualifying_scope = NULL_TREE;
20692 parser->object_scope = NULL_TREE;
20693 /* If it's a `,', then there are more declarators. */
20694 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20696 cp_lexer_consume_token (parser->lexer);
20697 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20699 cp_token *token = cp_lexer_previous_token (parser->lexer);
20700 error_at (token->location,
20701 "stray %<,%> at end of member declaration");
20704 /* If the next token isn't a `;', then we have a parse error. */
20705 else if (cp_lexer_next_token_is_not (parser->lexer,
20706 CPP_SEMICOLON))
20708 /* The next token might be a ways away from where the
20709 actual semicolon is missing. Find the previous token
20710 and use that for our error position. */
20711 cp_token *token = cp_lexer_previous_token (parser->lexer);
20712 error_at (token->location,
20713 "expected %<;%> at end of member declaration");
20715 /* Assume that the user meant to provide a semicolon. If
20716 we were to cp_parser_skip_to_end_of_statement, we might
20717 skip to a semicolon inside a member function definition
20718 and issue nonsensical error messages. */
20719 assume_semicolon = true;
20722 if (decl)
20724 /* Add DECL to the list of members. */
20725 if (!friend_p)
20726 finish_member_declaration (decl);
20728 if (TREE_CODE (decl) == FUNCTION_DECL)
20729 cp_parser_save_default_args (parser, decl);
20730 else if (TREE_CODE (decl) == FIELD_DECL
20731 && !DECL_C_BIT_FIELD (decl)
20732 && DECL_INITIAL (decl))
20733 /* Add DECL to the queue of NSDMI to be parsed later. */
20734 vec_safe_push (unparsed_nsdmis, decl);
20737 if (assume_semicolon)
20738 goto out;
20742 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20743 out:
20744 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20747 /* Parse a pure-specifier.
20749 pure-specifier:
20752 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20753 Otherwise, ERROR_MARK_NODE is returned. */
20755 static tree
20756 cp_parser_pure_specifier (cp_parser* parser)
20758 cp_token *token;
20760 /* Look for the `=' token. */
20761 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20762 return error_mark_node;
20763 /* Look for the `0' token. */
20764 token = cp_lexer_peek_token (parser->lexer);
20766 if (token->type == CPP_EOF
20767 || token->type == CPP_PRAGMA_EOL)
20768 return error_mark_node;
20770 cp_lexer_consume_token (parser->lexer);
20772 /* Accept = default or = delete in c++0x mode. */
20773 if (token->keyword == RID_DEFAULT
20774 || token->keyword == RID_DELETE)
20776 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20777 return token->u.value;
20780 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20781 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20783 cp_parser_error (parser,
20784 "invalid pure specifier (only %<= 0%> is allowed)");
20785 cp_parser_skip_to_end_of_statement (parser);
20786 return error_mark_node;
20788 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20790 error_at (token->location, "templates may not be %<virtual%>");
20791 return error_mark_node;
20794 return integer_zero_node;
20797 /* Parse a constant-initializer.
20799 constant-initializer:
20800 = constant-expression
20802 Returns a representation of the constant-expression. */
20804 static tree
20805 cp_parser_constant_initializer (cp_parser* parser)
20807 /* Look for the `=' token. */
20808 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20809 return error_mark_node;
20811 /* It is invalid to write:
20813 struct S { static const int i = { 7 }; };
20816 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20818 cp_parser_error (parser,
20819 "a brace-enclosed initializer is not allowed here");
20820 /* Consume the opening brace. */
20821 cp_lexer_consume_token (parser->lexer);
20822 /* Skip the initializer. */
20823 cp_parser_skip_to_closing_brace (parser);
20824 /* Look for the trailing `}'. */
20825 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20827 return error_mark_node;
20830 return cp_parser_constant_expression (parser,
20831 /*allow_non_constant=*/false,
20832 NULL);
20835 /* Derived classes [gram.class.derived] */
20837 /* Parse a base-clause.
20839 base-clause:
20840 : base-specifier-list
20842 base-specifier-list:
20843 base-specifier ... [opt]
20844 base-specifier-list , base-specifier ... [opt]
20846 Returns a TREE_LIST representing the base-classes, in the order in
20847 which they were declared. The representation of each node is as
20848 described by cp_parser_base_specifier.
20850 In the case that no bases are specified, this function will return
20851 NULL_TREE, not ERROR_MARK_NODE. */
20853 static tree
20854 cp_parser_base_clause (cp_parser* parser)
20856 tree bases = NULL_TREE;
20858 /* Look for the `:' that begins the list. */
20859 cp_parser_require (parser, CPP_COLON, RT_COLON);
20861 /* Scan the base-specifier-list. */
20862 while (true)
20864 cp_token *token;
20865 tree base;
20866 bool pack_expansion_p = false;
20868 /* Look for the base-specifier. */
20869 base = cp_parser_base_specifier (parser);
20870 /* Look for the (optional) ellipsis. */
20871 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20873 /* Consume the `...'. */
20874 cp_lexer_consume_token (parser->lexer);
20876 pack_expansion_p = true;
20879 /* Add BASE to the front of the list. */
20880 if (base && base != error_mark_node)
20882 if (pack_expansion_p)
20883 /* Make this a pack expansion type. */
20884 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20886 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20888 TREE_CHAIN (base) = bases;
20889 bases = base;
20892 /* Peek at the next token. */
20893 token = cp_lexer_peek_token (parser->lexer);
20894 /* If it's not a comma, then the list is complete. */
20895 if (token->type != CPP_COMMA)
20896 break;
20897 /* Consume the `,'. */
20898 cp_lexer_consume_token (parser->lexer);
20901 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20902 base class had a qualified name. However, the next name that
20903 appears is certainly not qualified. */
20904 parser->scope = NULL_TREE;
20905 parser->qualifying_scope = NULL_TREE;
20906 parser->object_scope = NULL_TREE;
20908 return nreverse (bases);
20911 /* Parse a base-specifier.
20913 base-specifier:
20914 :: [opt] nested-name-specifier [opt] class-name
20915 virtual access-specifier [opt] :: [opt] nested-name-specifier
20916 [opt] class-name
20917 access-specifier virtual [opt] :: [opt] nested-name-specifier
20918 [opt] class-name
20920 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20921 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20922 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20923 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20925 static tree
20926 cp_parser_base_specifier (cp_parser* parser)
20928 cp_token *token;
20929 bool done = false;
20930 bool virtual_p = false;
20931 bool duplicate_virtual_error_issued_p = false;
20932 bool duplicate_access_error_issued_p = false;
20933 bool class_scope_p, template_p;
20934 tree access = access_default_node;
20935 tree type;
20937 /* Process the optional `virtual' and `access-specifier'. */
20938 while (!done)
20940 /* Peek at the next token. */
20941 token = cp_lexer_peek_token (parser->lexer);
20942 /* Process `virtual'. */
20943 switch (token->keyword)
20945 case RID_VIRTUAL:
20946 /* If `virtual' appears more than once, issue an error. */
20947 if (virtual_p && !duplicate_virtual_error_issued_p)
20949 cp_parser_error (parser,
20950 "%<virtual%> specified more than once in base-specified");
20951 duplicate_virtual_error_issued_p = true;
20954 virtual_p = true;
20956 /* Consume the `virtual' token. */
20957 cp_lexer_consume_token (parser->lexer);
20959 break;
20961 case RID_PUBLIC:
20962 case RID_PROTECTED:
20963 case RID_PRIVATE:
20964 /* If more than one access specifier appears, issue an
20965 error. */
20966 if (access != access_default_node
20967 && !duplicate_access_error_issued_p)
20969 cp_parser_error (parser,
20970 "more than one access specifier in base-specified");
20971 duplicate_access_error_issued_p = true;
20974 access = ridpointers[(int) token->keyword];
20976 /* Consume the access-specifier. */
20977 cp_lexer_consume_token (parser->lexer);
20979 break;
20981 default:
20982 done = true;
20983 break;
20986 /* It is not uncommon to see programs mechanically, erroneously, use
20987 the 'typename' keyword to denote (dependent) qualified types
20988 as base classes. */
20989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20991 token = cp_lexer_peek_token (parser->lexer);
20992 if (!processing_template_decl)
20993 error_at (token->location,
20994 "keyword %<typename%> not allowed outside of templates");
20995 else
20996 error_at (token->location,
20997 "keyword %<typename%> not allowed in this context "
20998 "(the base class is implicitly a type)");
20999 cp_lexer_consume_token (parser->lexer);
21002 /* Look for the optional `::' operator. */
21003 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21004 /* Look for the nested-name-specifier. The simplest way to
21005 implement:
21007 [temp.res]
21009 The keyword `typename' is not permitted in a base-specifier or
21010 mem-initializer; in these contexts a qualified name that
21011 depends on a template-parameter is implicitly assumed to be a
21012 type name.
21014 is to pretend that we have seen the `typename' keyword at this
21015 point. */
21016 cp_parser_nested_name_specifier_opt (parser,
21017 /*typename_keyword_p=*/true,
21018 /*check_dependency_p=*/true,
21019 typename_type,
21020 /*is_declaration=*/true);
21021 /* If the base class is given by a qualified name, assume that names
21022 we see are type names or templates, as appropriate. */
21023 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21024 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21026 if (!parser->scope
21027 && cp_lexer_next_token_is_decltype (parser->lexer))
21028 /* DR 950 allows decltype as a base-specifier. */
21029 type = cp_parser_decltype (parser);
21030 else
21032 /* Otherwise, look for the class-name. */
21033 type = cp_parser_class_name (parser,
21034 class_scope_p,
21035 template_p,
21036 typename_type,
21037 /*check_dependency_p=*/true,
21038 /*class_head_p=*/false,
21039 /*is_declaration=*/true);
21040 type = TREE_TYPE (type);
21043 if (type == error_mark_node)
21044 return error_mark_node;
21046 return finish_base_specifier (type, access, virtual_p);
21049 /* Exception handling [gram.exception] */
21051 /* Parse an (optional) noexcept-specification.
21053 noexcept-specification:
21054 noexcept ( constant-expression ) [opt]
21056 If no noexcept-specification is present, returns NULL_TREE.
21057 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21058 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21059 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21060 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21061 in which case a boolean condition is returned instead. */
21063 static tree
21064 cp_parser_noexcept_specification_opt (cp_parser* parser,
21065 bool require_constexpr,
21066 bool* consumed_expr,
21067 bool return_cond)
21069 cp_token *token;
21070 const char *saved_message;
21072 /* Peek at the next token. */
21073 token = cp_lexer_peek_token (parser->lexer);
21075 /* Is it a noexcept-specification? */
21076 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21078 tree expr;
21079 cp_lexer_consume_token (parser->lexer);
21081 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21083 cp_lexer_consume_token (parser->lexer);
21085 if (require_constexpr)
21087 /* Types may not be defined in an exception-specification. */
21088 saved_message = parser->type_definition_forbidden_message;
21089 parser->type_definition_forbidden_message
21090 = G_("types may not be defined in an exception-specification");
21092 expr = cp_parser_constant_expression (parser, false, NULL);
21094 /* Restore the saved message. */
21095 parser->type_definition_forbidden_message = saved_message;
21097 else
21099 expr = cp_parser_expression (parser, false, NULL);
21100 *consumed_expr = true;
21103 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21105 else
21107 expr = boolean_true_node;
21108 if (!require_constexpr)
21109 *consumed_expr = false;
21112 /* We cannot build a noexcept-spec right away because this will check
21113 that expr is a constexpr. */
21114 if (!return_cond)
21115 return build_noexcept_spec (expr, tf_warning_or_error);
21116 else
21117 return expr;
21119 else
21120 return NULL_TREE;
21123 /* Parse an (optional) exception-specification.
21125 exception-specification:
21126 throw ( type-id-list [opt] )
21128 Returns a TREE_LIST representing the exception-specification. The
21129 TREE_VALUE of each node is a type. */
21131 static tree
21132 cp_parser_exception_specification_opt (cp_parser* parser)
21134 cp_token *token;
21135 tree type_id_list;
21136 const char *saved_message;
21138 /* Peek at the next token. */
21139 token = cp_lexer_peek_token (parser->lexer);
21141 /* Is it a noexcept-specification? */
21142 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21143 false);
21144 if (type_id_list != NULL_TREE)
21145 return type_id_list;
21147 /* If it's not `throw', then there's no exception-specification. */
21148 if (!cp_parser_is_keyword (token, RID_THROW))
21149 return NULL_TREE;
21151 #if 0
21152 /* Enable this once a lot of code has transitioned to noexcept? */
21153 if (cxx_dialect >= cxx11 && !in_system_header)
21154 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21155 "deprecated in C++0x; use %<noexcept%> instead");
21156 #endif
21158 /* Consume the `throw'. */
21159 cp_lexer_consume_token (parser->lexer);
21161 /* Look for the `('. */
21162 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21164 /* Peek at the next token. */
21165 token = cp_lexer_peek_token (parser->lexer);
21166 /* If it's not a `)', then there is a type-id-list. */
21167 if (token->type != CPP_CLOSE_PAREN)
21169 /* Types may not be defined in an exception-specification. */
21170 saved_message = parser->type_definition_forbidden_message;
21171 parser->type_definition_forbidden_message
21172 = G_("types may not be defined in an exception-specification");
21173 /* Parse the type-id-list. */
21174 type_id_list = cp_parser_type_id_list (parser);
21175 /* Restore the saved message. */
21176 parser->type_definition_forbidden_message = saved_message;
21178 else
21179 type_id_list = empty_except_spec;
21181 /* Look for the `)'. */
21182 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21184 return type_id_list;
21187 /* Parse an (optional) type-id-list.
21189 type-id-list:
21190 type-id ... [opt]
21191 type-id-list , type-id ... [opt]
21193 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21194 in the order that the types were presented. */
21196 static tree
21197 cp_parser_type_id_list (cp_parser* parser)
21199 tree types = NULL_TREE;
21201 while (true)
21203 cp_token *token;
21204 tree type;
21206 /* Get the next type-id. */
21207 type = cp_parser_type_id (parser);
21208 /* Parse the optional ellipsis. */
21209 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21211 /* Consume the `...'. */
21212 cp_lexer_consume_token (parser->lexer);
21214 /* Turn the type into a pack expansion expression. */
21215 type = make_pack_expansion (type);
21217 /* Add it to the list. */
21218 types = add_exception_specifier (types, type, /*complain=*/1);
21219 /* Peek at the next token. */
21220 token = cp_lexer_peek_token (parser->lexer);
21221 /* If it is not a `,', we are done. */
21222 if (token->type != CPP_COMMA)
21223 break;
21224 /* Consume the `,'. */
21225 cp_lexer_consume_token (parser->lexer);
21228 return nreverse (types);
21231 /* Parse a try-block.
21233 try-block:
21234 try compound-statement handler-seq */
21236 static tree
21237 cp_parser_try_block (cp_parser* parser)
21239 tree try_block;
21241 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21242 try_block = begin_try_block ();
21243 cp_parser_compound_statement (parser, NULL, true, false);
21244 finish_try_block (try_block);
21245 cp_parser_handler_seq (parser);
21246 finish_handler_sequence (try_block);
21248 return try_block;
21251 /* Parse a function-try-block.
21253 function-try-block:
21254 try ctor-initializer [opt] function-body handler-seq */
21256 static bool
21257 cp_parser_function_try_block (cp_parser* parser)
21259 tree compound_stmt;
21260 tree try_block;
21261 bool ctor_initializer_p;
21263 /* Look for the `try' keyword. */
21264 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21265 return false;
21266 /* Let the rest of the front end know where we are. */
21267 try_block = begin_function_try_block (&compound_stmt);
21268 /* Parse the function-body. */
21269 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21270 (parser, /*in_function_try_block=*/true);
21271 /* We're done with the `try' part. */
21272 finish_function_try_block (try_block);
21273 /* Parse the handlers. */
21274 cp_parser_handler_seq (parser);
21275 /* We're done with the handlers. */
21276 finish_function_handler_sequence (try_block, compound_stmt);
21278 return ctor_initializer_p;
21281 /* Parse a handler-seq.
21283 handler-seq:
21284 handler handler-seq [opt] */
21286 static void
21287 cp_parser_handler_seq (cp_parser* parser)
21289 while (true)
21291 cp_token *token;
21293 /* Parse the handler. */
21294 cp_parser_handler (parser);
21295 /* Peek at the next token. */
21296 token = cp_lexer_peek_token (parser->lexer);
21297 /* If it's not `catch' then there are no more handlers. */
21298 if (!cp_parser_is_keyword (token, RID_CATCH))
21299 break;
21303 /* Parse a handler.
21305 handler:
21306 catch ( exception-declaration ) compound-statement */
21308 static void
21309 cp_parser_handler (cp_parser* parser)
21311 tree handler;
21312 tree declaration;
21314 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21315 handler = begin_handler ();
21316 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21317 declaration = cp_parser_exception_declaration (parser);
21318 finish_handler_parms (declaration, handler);
21319 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21320 cp_parser_compound_statement (parser, NULL, false, false);
21321 finish_handler (handler);
21324 /* Parse an exception-declaration.
21326 exception-declaration:
21327 type-specifier-seq declarator
21328 type-specifier-seq abstract-declarator
21329 type-specifier-seq
21332 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21333 ellipsis variant is used. */
21335 static tree
21336 cp_parser_exception_declaration (cp_parser* parser)
21338 cp_decl_specifier_seq type_specifiers;
21339 cp_declarator *declarator;
21340 const char *saved_message;
21342 /* If it's an ellipsis, it's easy to handle. */
21343 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21345 /* Consume the `...' token. */
21346 cp_lexer_consume_token (parser->lexer);
21347 return NULL_TREE;
21350 /* Types may not be defined in exception-declarations. */
21351 saved_message = parser->type_definition_forbidden_message;
21352 parser->type_definition_forbidden_message
21353 = G_("types may not be defined in exception-declarations");
21355 /* Parse the type-specifier-seq. */
21356 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21357 /*is_trailing_return=*/false,
21358 &type_specifiers);
21359 /* If it's a `)', then there is no declarator. */
21360 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21361 declarator = NULL;
21362 else
21363 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21364 /*ctor_dtor_or_conv_p=*/NULL,
21365 /*parenthesized_p=*/NULL,
21366 /*member_p=*/false);
21368 /* Restore the saved message. */
21369 parser->type_definition_forbidden_message = saved_message;
21371 if (!type_specifiers.any_specifiers_p)
21372 return error_mark_node;
21374 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21377 /* Parse a throw-expression.
21379 throw-expression:
21380 throw assignment-expression [opt]
21382 Returns a THROW_EXPR representing the throw-expression. */
21384 static tree
21385 cp_parser_throw_expression (cp_parser* parser)
21387 tree expression;
21388 cp_token* token;
21390 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21391 token = cp_lexer_peek_token (parser->lexer);
21392 /* Figure out whether or not there is an assignment-expression
21393 following the "throw" keyword. */
21394 if (token->type == CPP_COMMA
21395 || token->type == CPP_SEMICOLON
21396 || token->type == CPP_CLOSE_PAREN
21397 || token->type == CPP_CLOSE_SQUARE
21398 || token->type == CPP_CLOSE_BRACE
21399 || token->type == CPP_COLON)
21400 expression = NULL_TREE;
21401 else
21402 expression = cp_parser_assignment_expression (parser,
21403 /*cast_p=*/false, NULL);
21405 return build_throw (expression);
21408 /* GNU Extensions */
21410 /* Parse an (optional) asm-specification.
21412 asm-specification:
21413 asm ( string-literal )
21415 If the asm-specification is present, returns a STRING_CST
21416 corresponding to the string-literal. Otherwise, returns
21417 NULL_TREE. */
21419 static tree
21420 cp_parser_asm_specification_opt (cp_parser* parser)
21422 cp_token *token;
21423 tree asm_specification;
21425 /* Peek at the next token. */
21426 token = cp_lexer_peek_token (parser->lexer);
21427 /* If the next token isn't the `asm' keyword, then there's no
21428 asm-specification. */
21429 if (!cp_parser_is_keyword (token, RID_ASM))
21430 return NULL_TREE;
21432 /* Consume the `asm' token. */
21433 cp_lexer_consume_token (parser->lexer);
21434 /* Look for the `('. */
21435 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21437 /* Look for the string-literal. */
21438 asm_specification = cp_parser_string_literal (parser, false, false);
21440 /* Look for the `)'. */
21441 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21443 return asm_specification;
21446 /* Parse an asm-operand-list.
21448 asm-operand-list:
21449 asm-operand
21450 asm-operand-list , asm-operand
21452 asm-operand:
21453 string-literal ( expression )
21454 [ string-literal ] string-literal ( expression )
21456 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21457 each node is the expression. The TREE_PURPOSE is itself a
21458 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21459 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21460 is a STRING_CST for the string literal before the parenthesis. Returns
21461 ERROR_MARK_NODE if any of the operands are invalid. */
21463 static tree
21464 cp_parser_asm_operand_list (cp_parser* parser)
21466 tree asm_operands = NULL_TREE;
21467 bool invalid_operands = false;
21469 while (true)
21471 tree string_literal;
21472 tree expression;
21473 tree name;
21475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21477 /* Consume the `[' token. */
21478 cp_lexer_consume_token (parser->lexer);
21479 /* Read the operand name. */
21480 name = cp_parser_identifier (parser);
21481 if (name != error_mark_node)
21482 name = build_string (IDENTIFIER_LENGTH (name),
21483 IDENTIFIER_POINTER (name));
21484 /* Look for the closing `]'. */
21485 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21487 else
21488 name = NULL_TREE;
21489 /* Look for the string-literal. */
21490 string_literal = cp_parser_string_literal (parser, false, false);
21492 /* Look for the `('. */
21493 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21494 /* Parse the expression. */
21495 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21496 /* Look for the `)'. */
21497 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21499 if (name == error_mark_node
21500 || string_literal == error_mark_node
21501 || expression == error_mark_node)
21502 invalid_operands = true;
21504 /* Add this operand to the list. */
21505 asm_operands = tree_cons (build_tree_list (name, string_literal),
21506 expression,
21507 asm_operands);
21508 /* If the next token is not a `,', there are no more
21509 operands. */
21510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21511 break;
21512 /* Consume the `,'. */
21513 cp_lexer_consume_token (parser->lexer);
21516 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21519 /* Parse an asm-clobber-list.
21521 asm-clobber-list:
21522 string-literal
21523 asm-clobber-list , string-literal
21525 Returns a TREE_LIST, indicating the clobbers in the order that they
21526 appeared. The TREE_VALUE of each node is a STRING_CST. */
21528 static tree
21529 cp_parser_asm_clobber_list (cp_parser* parser)
21531 tree clobbers = NULL_TREE;
21533 while (true)
21535 tree string_literal;
21537 /* Look for the string literal. */
21538 string_literal = cp_parser_string_literal (parser, false, false);
21539 /* Add it to the list. */
21540 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21541 /* If the next token is not a `,', then the list is
21542 complete. */
21543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21544 break;
21545 /* Consume the `,' token. */
21546 cp_lexer_consume_token (parser->lexer);
21549 return clobbers;
21552 /* Parse an asm-label-list.
21554 asm-label-list:
21555 identifier
21556 asm-label-list , identifier
21558 Returns a TREE_LIST, indicating the labels in the order that they
21559 appeared. The TREE_VALUE of each node is a label. */
21561 static tree
21562 cp_parser_asm_label_list (cp_parser* parser)
21564 tree labels = NULL_TREE;
21566 while (true)
21568 tree identifier, label, name;
21570 /* Look for the identifier. */
21571 identifier = cp_parser_identifier (parser);
21572 if (!error_operand_p (identifier))
21574 label = lookup_label (identifier);
21575 if (TREE_CODE (label) == LABEL_DECL)
21577 TREE_USED (label) = 1;
21578 check_goto (label);
21579 name = build_string (IDENTIFIER_LENGTH (identifier),
21580 IDENTIFIER_POINTER (identifier));
21581 labels = tree_cons (name, label, labels);
21584 /* If the next token is not a `,', then the list is
21585 complete. */
21586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21587 break;
21588 /* Consume the `,' token. */
21589 cp_lexer_consume_token (parser->lexer);
21592 return nreverse (labels);
21595 /* Return TRUE iff the next tokens in the stream are possibly the
21596 beginning of a GNU extension attribute. */
21598 static bool
21599 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21601 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21604 /* Return TRUE iff the next tokens in the stream are possibly the
21605 beginning of a standard C++-11 attribute specifier. */
21607 static bool
21608 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21610 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21613 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21614 beginning of a standard C++-11 attribute specifier. */
21616 static bool
21617 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21619 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21621 return (cxx_dialect >= cxx11
21622 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21623 || (token->type == CPP_OPEN_SQUARE
21624 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21625 && token->type == CPP_OPEN_SQUARE)));
21628 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21629 beginning of a GNU extension attribute. */
21631 static bool
21632 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21634 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21636 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21639 /* Return true iff the next tokens can be the beginning of either a
21640 GNU attribute list, or a standard C++11 attribute sequence. */
21642 static bool
21643 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21645 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21646 || cp_next_tokens_can_be_std_attribute_p (parser));
21649 /* Return true iff the next Nth tokens can be the beginning of either
21650 a GNU attribute list, or a standard C++11 attribute sequence. */
21652 static bool
21653 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21655 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21656 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21659 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21660 of GNU attributes, or return NULL. */
21662 static tree
21663 cp_parser_attributes_opt (cp_parser *parser)
21665 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21666 return cp_parser_gnu_attributes_opt (parser);
21667 return cp_parser_std_attribute_spec_seq (parser);
21670 /* Parse an (optional) series of attributes.
21672 attributes:
21673 attributes attribute
21675 attribute:
21676 __attribute__ (( attribute-list [opt] ))
21678 The return value is as for cp_parser_gnu_attribute_list. */
21680 static tree
21681 cp_parser_gnu_attributes_opt (cp_parser* parser)
21683 tree attributes = NULL_TREE;
21685 while (true)
21687 cp_token *token;
21688 tree attribute_list;
21689 bool ok = true;
21691 /* Peek at the next token. */
21692 token = cp_lexer_peek_token (parser->lexer);
21693 /* If it's not `__attribute__', then we're done. */
21694 if (token->keyword != RID_ATTRIBUTE)
21695 break;
21697 /* Consume the `__attribute__' keyword. */
21698 cp_lexer_consume_token (parser->lexer);
21699 /* Look for the two `(' tokens. */
21700 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21701 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21703 /* Peek at the next token. */
21704 token = cp_lexer_peek_token (parser->lexer);
21705 if (token->type != CPP_CLOSE_PAREN)
21706 /* Parse the attribute-list. */
21707 attribute_list = cp_parser_gnu_attribute_list (parser);
21708 else
21709 /* If the next token is a `)', then there is no attribute
21710 list. */
21711 attribute_list = NULL;
21713 /* Look for the two `)' tokens. */
21714 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21715 ok = false;
21716 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21717 ok = false;
21718 if (!ok)
21719 cp_parser_skip_to_end_of_statement (parser);
21721 /* Add these new attributes to the list. */
21722 attributes = chainon (attributes, attribute_list);
21725 return attributes;
21728 /* Parse a GNU attribute-list.
21730 attribute-list:
21731 attribute
21732 attribute-list , attribute
21734 attribute:
21735 identifier
21736 identifier ( identifier )
21737 identifier ( identifier , expression-list )
21738 identifier ( expression-list )
21740 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21741 to an attribute. The TREE_PURPOSE of each node is the identifier
21742 indicating which attribute is in use. The TREE_VALUE represents
21743 the arguments, if any. */
21745 static tree
21746 cp_parser_gnu_attribute_list (cp_parser* parser)
21748 tree attribute_list = NULL_TREE;
21749 bool save_translate_strings_p = parser->translate_strings_p;
21751 parser->translate_strings_p = false;
21752 while (true)
21754 cp_token *token;
21755 tree identifier;
21756 tree attribute;
21758 /* Look for the identifier. We also allow keywords here; for
21759 example `__attribute__ ((const))' is legal. */
21760 token = cp_lexer_peek_token (parser->lexer);
21761 if (token->type == CPP_NAME
21762 || token->type == CPP_KEYWORD)
21764 tree arguments = NULL_TREE;
21766 /* Consume the token. */
21767 token = cp_lexer_consume_token (parser->lexer);
21769 /* Save away the identifier that indicates which attribute
21770 this is. */
21771 identifier = (token->type == CPP_KEYWORD)
21772 /* For keywords, use the canonical spelling, not the
21773 parsed identifier. */
21774 ? ridpointers[(int) token->keyword]
21775 : token->u.value;
21777 attribute = build_tree_list (identifier, NULL_TREE);
21779 /* Peek at the next token. */
21780 token = cp_lexer_peek_token (parser->lexer);
21781 /* If it's an `(', then parse the attribute arguments. */
21782 if (token->type == CPP_OPEN_PAREN)
21784 vec<tree, va_gc> *vec;
21785 int attr_flag = (attribute_takes_identifier_p (identifier)
21786 ? id_attr : normal_attr);
21787 vec = cp_parser_parenthesized_expression_list
21788 (parser, attr_flag, /*cast_p=*/false,
21789 /*allow_expansion_p=*/false,
21790 /*non_constant_p=*/NULL);
21791 if (vec == NULL)
21792 arguments = error_mark_node;
21793 else
21795 arguments = build_tree_list_vec (vec);
21796 release_tree_vector (vec);
21798 /* Save the arguments away. */
21799 TREE_VALUE (attribute) = arguments;
21802 if (arguments != error_mark_node)
21804 /* Add this attribute to the list. */
21805 TREE_CHAIN (attribute) = attribute_list;
21806 attribute_list = attribute;
21809 token = cp_lexer_peek_token (parser->lexer);
21811 /* Now, look for more attributes. If the next token isn't a
21812 `,', we're done. */
21813 if (token->type != CPP_COMMA)
21814 break;
21816 /* Consume the comma and keep going. */
21817 cp_lexer_consume_token (parser->lexer);
21819 parser->translate_strings_p = save_translate_strings_p;
21821 /* We built up the list in reverse order. */
21822 return nreverse (attribute_list);
21825 /* Parse a standard C++11 attribute.
21827 The returned representation is a TREE_LIST which TREE_PURPOSE is
21828 the scoped name of the attribute, and the TREE_VALUE is its
21829 arguments list.
21831 Note that the scoped name of the attribute is itself a TREE_LIST
21832 which TREE_PURPOSE is the namespace of the attribute, and
21833 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21834 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21835 and which TREE_PURPOSE is directly the attribute name.
21837 Clients of the attribute code should use get_attribute_namespace
21838 and get_attribute_name to get the actual namespace and name of
21839 attributes, regardless of their being GNU or C++11 attributes.
21841 attribute:
21842 attribute-token attribute-argument-clause [opt]
21844 attribute-token:
21845 identifier
21846 attribute-scoped-token
21848 attribute-scoped-token:
21849 attribute-namespace :: identifier
21851 attribute-namespace:
21852 identifier
21854 attribute-argument-clause:
21855 ( balanced-token-seq )
21857 balanced-token-seq:
21858 balanced-token [opt]
21859 balanced-token-seq balanced-token
21861 balanced-token:
21862 ( balanced-token-seq )
21863 [ balanced-token-seq ]
21864 { balanced-token-seq }. */
21866 static tree
21867 cp_parser_std_attribute (cp_parser *parser)
21869 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21870 cp_token *token;
21872 /* First, parse name of the the attribute, a.k.a
21873 attribute-token. */
21875 token = cp_lexer_peek_token (parser->lexer);
21876 if (token->type == CPP_NAME)
21877 attr_id = token->u.value;
21878 else if (token->type == CPP_KEYWORD)
21879 attr_id = ridpointers[(int) token->keyword];
21880 else if (token->flags & NAMED_OP)
21881 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21883 if (attr_id == NULL_TREE)
21884 return NULL_TREE;
21886 cp_lexer_consume_token (parser->lexer);
21888 token = cp_lexer_peek_token (parser->lexer);
21889 if (token->type == CPP_SCOPE)
21891 /* We are seeing a scoped attribute token. */
21893 cp_lexer_consume_token (parser->lexer);
21894 attr_ns = attr_id;
21896 token = cp_lexer_consume_token (parser->lexer);
21897 if (token->type == CPP_NAME)
21898 attr_id = token->u.value;
21899 else if (token->type == CPP_KEYWORD)
21900 attr_id = ridpointers[(int) token->keyword];
21901 else
21903 error_at (token->location,
21904 "expected an identifier for the attribute name");
21905 return error_mark_node;
21907 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21908 NULL_TREE);
21909 token = cp_lexer_peek_token (parser->lexer);
21911 else
21913 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21914 NULL_TREE);
21915 /* C++11 noreturn attribute is equivalent to GNU's. */
21916 if (is_attribute_p ("noreturn", attr_id))
21917 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21918 /* C++14 deprecated attribute is equivalent to GNU's. */
21919 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21920 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21923 /* Now parse the optional argument clause of the attribute. */
21925 if (token->type != CPP_OPEN_PAREN)
21926 return attribute;
21929 vec<tree, va_gc> *vec;
21930 int attr_flag = normal_attr;
21932 if (attr_ns == get_identifier ("gnu")
21933 && attribute_takes_identifier_p (attr_id))
21934 /* A GNU attribute that takes an identifier in parameter. */
21935 attr_flag = id_attr;
21937 vec = cp_parser_parenthesized_expression_list
21938 (parser, attr_flag, /*cast_p=*/false,
21939 /*allow_expansion_p=*/true,
21940 /*non_constant_p=*/NULL);
21941 if (vec == NULL)
21942 arguments = error_mark_node;
21943 else
21945 arguments = build_tree_list_vec (vec);
21946 release_tree_vector (vec);
21949 if (arguments == error_mark_node)
21950 attribute = error_mark_node;
21951 else
21952 TREE_VALUE (attribute) = arguments;
21955 return attribute;
21958 /* Parse a list of standard C++-11 attributes.
21960 attribute-list:
21961 attribute [opt]
21962 attribute-list , attribute[opt]
21963 attribute ...
21964 attribute-list , attribute ...
21967 static tree
21968 cp_parser_std_attribute_list (cp_parser *parser)
21970 tree attributes = NULL_TREE, attribute = NULL_TREE;
21971 cp_token *token = NULL;
21973 while (true)
21975 attribute = cp_parser_std_attribute (parser);
21976 if (attribute == error_mark_node)
21977 break;
21978 if (attribute != NULL_TREE)
21980 TREE_CHAIN (attribute) = attributes;
21981 attributes = attribute;
21983 token = cp_lexer_peek_token (parser->lexer);
21984 if (token->type != CPP_COMMA)
21985 break;
21986 cp_lexer_consume_token (parser->lexer);
21988 attributes = nreverse (attributes);
21989 return attributes;
21992 /* Parse a standard C++-11 attribute specifier.
21994 attribute-specifier:
21995 [ [ attribute-list ] ]
21996 alignment-specifier
21998 alignment-specifier:
21999 alignas ( type-id ... [opt] )
22000 alignas ( alignment-expression ... [opt] ). */
22002 static tree
22003 cp_parser_std_attribute_spec (cp_parser *parser)
22005 tree attributes = NULL_TREE;
22006 cp_token *token = cp_lexer_peek_token (parser->lexer);
22008 if (token->type == CPP_OPEN_SQUARE
22009 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22011 cp_lexer_consume_token (parser->lexer);
22012 cp_lexer_consume_token (parser->lexer);
22014 attributes = cp_parser_std_attribute_list (parser);
22016 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22017 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22018 cp_parser_skip_to_end_of_statement (parser);
22019 else
22020 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22021 when we are sure that we have actually parsed them. */
22022 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22024 else
22026 tree alignas_expr;
22028 /* Look for an alignment-specifier. */
22030 token = cp_lexer_peek_token (parser->lexer);
22032 if (token->type != CPP_KEYWORD
22033 || token->keyword != RID_ALIGNAS)
22034 return NULL_TREE;
22036 cp_lexer_consume_token (parser->lexer);
22037 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22039 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22041 cp_parser_error (parser, "expected %<(%>");
22042 return error_mark_node;
22045 cp_parser_parse_tentatively (parser);
22046 alignas_expr = cp_parser_type_id (parser);
22048 if (!cp_parser_parse_definitely (parser))
22050 gcc_assert (alignas_expr == error_mark_node
22051 || alignas_expr == NULL_TREE);
22053 alignas_expr =
22054 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22055 /**cp_id_kind=*/NULL);
22056 if (alignas_expr == error_mark_node)
22057 cp_parser_skip_to_end_of_statement (parser);
22058 if (alignas_expr == NULL_TREE
22059 || alignas_expr == error_mark_node)
22060 return alignas_expr;
22063 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22065 cp_parser_error (parser, "expected %<)%>");
22066 return error_mark_node;
22069 alignas_expr = cxx_alignas_expr (alignas_expr);
22071 /* Build the C++-11 representation of an 'aligned'
22072 attribute. */
22073 attributes =
22074 build_tree_list (build_tree_list (get_identifier ("gnu"),
22075 get_identifier ("aligned")),
22076 build_tree_list (NULL_TREE, alignas_expr));
22079 return attributes;
22082 /* Parse a standard C++-11 attribute-specifier-seq.
22084 attribute-specifier-seq:
22085 attribute-specifier-seq [opt] attribute-specifier
22088 static tree
22089 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22091 tree attr_specs = NULL;
22093 while (true)
22095 tree attr_spec = cp_parser_std_attribute_spec (parser);
22096 if (attr_spec == NULL_TREE)
22097 break;
22098 if (attr_spec == error_mark_node)
22099 return error_mark_node;
22101 TREE_CHAIN (attr_spec) = attr_specs;
22102 attr_specs = attr_spec;
22105 attr_specs = nreverse (attr_specs);
22106 return attr_specs;
22109 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22110 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22111 current value of the PEDANTIC flag, regardless of whether or not
22112 the `__extension__' keyword is present. The caller is responsible
22113 for restoring the value of the PEDANTIC flag. */
22115 static bool
22116 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22118 /* Save the old value of the PEDANTIC flag. */
22119 *saved_pedantic = pedantic;
22121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22123 /* Consume the `__extension__' token. */
22124 cp_lexer_consume_token (parser->lexer);
22125 /* We're not being pedantic while the `__extension__' keyword is
22126 in effect. */
22127 pedantic = 0;
22129 return true;
22132 return false;
22135 /* Parse a label declaration.
22137 label-declaration:
22138 __label__ label-declarator-seq ;
22140 label-declarator-seq:
22141 identifier , label-declarator-seq
22142 identifier */
22144 static void
22145 cp_parser_label_declaration (cp_parser* parser)
22147 /* Look for the `__label__' keyword. */
22148 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22150 while (true)
22152 tree identifier;
22154 /* Look for an identifier. */
22155 identifier = cp_parser_identifier (parser);
22156 /* If we failed, stop. */
22157 if (identifier == error_mark_node)
22158 break;
22159 /* Declare it as a label. */
22160 finish_label_decl (identifier);
22161 /* If the next token is a `;', stop. */
22162 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22163 break;
22164 /* Look for the `,' separating the label declarations. */
22165 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22168 /* Look for the final `;'. */
22169 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22172 // -------------------------------------------------------------------------- //
22173 // Requires Clause
22175 // Parse a requires clause.
22177 // requires-clause:
22178 // 'requires' logical-or-expression
22180 // The required logical-or-expression must be a constant expression.
22181 static tree
22182 cp_parser_requires_clause (cp_parser *parser)
22184 // Parse the constant expression.
22185 tree expr =
22186 cp_parser_binary_expression (parser, false, false, PREC_NOT_OPERATOR, NULL);
22187 if (!require_potential_rvalue_constant_expression (expr))
22188 return error_mark_node;
22189 return expr;
22192 // Optionally parse a requires clause:
22194 // requires-clause:
22195 // 'requires' logical-or-expression
22197 // The required logical-or-expression must be a constant expression.
22198 static tree
22199 cp_parser_requires_clause_opt (cp_parser *parser)
22201 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
22202 return NULL_TREE;
22203 cp_lexer_consume_token (parser->lexer);
22204 return cp_parser_requires_clause (parser);
22208 // -------------------------------------------------------------------------- //
22209 // Requires Expression
22212 // An RAII helper that provides scoped control for entering and exiting
22213 // the local scope defined by a requires expression. Name bindings introduced
22214 // within the scope are popped prior to exiting the scope.
22215 struct cp_parser_requires_expr_scope
22217 // Enter a scope of kind K belonging to the decl D.
22218 cp_parser_requires_expr_scope ()
22220 begin_scope (sk_block, NULL_TREE);
22223 ~cp_parser_requires_expr_scope ()
22225 for (tree t = current_binding_level->names; t; t = DECL_CHAIN (t))
22226 pop_binding (DECL_NAME (t), t);
22227 leave_scope ();
22231 // Parse a requires expression
22233 // requirement-expression:
22234 // 'requires' requirement-parameter-list requirement-body
22235 static tree
22236 cp_parser_requires_expression (cp_parser *parser)
22238 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
22239 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
22241 // TODO: Earlier versions of the concepts lite spec did not allow
22242 // requires expressions outside of template declarations. That
22243 // restriction was relaxed in Chicago, but it has not been implemented.
22244 if (!processing_template_decl)
22246 error_at (loc, "a requires expression cannot appear outside a template");
22247 cp_parser_skip_to_end_of_statement (parser);
22248 return error_mark_node;
22252 // TODO: Check that requires expressions are only written inside of
22253 // template declarations. They don't need to be concepts, just templates.
22255 // Parse the optional parameter list. Any local parameter declarations
22256 // are added to a new scope and are visible within the nested
22257 // requirement list.
22258 cp_parser_requires_expr_scope guard;
22259 tree parms = cp_parser_requirement_parameter_list (parser);
22260 if (parms == error_mark_node)
22261 return error_mark_node;
22263 // Parse the requirement body.
22264 tree reqs = cp_parser_requirement_body (parser);
22265 if (reqs == error_mark_node)
22266 return error_mark_node;
22268 return finish_requires_expr (parms, reqs);
22271 // Parse a parameterized requirement.
22273 // requirement-parameter-list:
22274 // '(' parameter-declaration-clause ')'
22275 static tree
22276 cp_parser_requirement_parameter_list (cp_parser *parser)
22278 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22279 return error_mark_node;
22281 // Parse the nested parameter declaration clause.
22282 tree parms = cp_parser_parameter_declaration_clause (parser);
22283 if (parms == error_mark_node)
22284 return error_mark_node;
22286 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22287 return error_mark_node;
22289 return parms;
22292 // Parse the body of a requirement.
22294 // requirement-body:
22295 // '{' requirement-list '}'
22296 static tree
22297 cp_parser_requirement_body (cp_parser *parser)
22299 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22300 return error_mark_node;
22302 tree reqs = cp_parser_requirement_list (parser);
22304 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
22305 return error_mark_node;
22307 return reqs;
22310 // Parse a list of requirements.
22312 // requirement-list:
22313 // requirement
22314 // requirement-list ';' requirement[opt]
22315 static tree
22316 cp_parser_requirement_list (cp_parser *parser)
22318 tree result = NULL_TREE;
22319 while (true)
22321 tree req = cp_parser_requirement (parser);
22322 if (req == error_mark_node)
22323 return req;
22325 result = tree_cons (NULL_TREE, req, result);
22327 // If we see a semi-colon, consume it.
22328 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22329 cp_lexer_consume_token (parser->lexer);
22331 // If we've found the end of the list, stop processing
22332 // the list.
22333 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22334 break;
22337 // Reverse the order of requirements so they are analyzed in
22338 // declaration order.
22339 return nreverse (result);
22342 // Parse a syntactic requirement or type requirement.
22344 // requirement:
22345 // simple-requirement
22346 // compound-requirement
22347 // type-requirement
22348 // nested-requirement
22349 static tree
22350 cp_parser_requirement (cp_parser *parser)
22352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
22353 return cp_parser_nested_requirement (parser);
22355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22356 return cp_parser_compound_requirement (parser);
22358 // Try parsing a type requirement first.
22359 cp_parser_parse_tentatively (parser);
22360 tree req = cp_parser_type_requirement (parser);
22361 if (!cp_parser_parse_definitely (parser))
22362 req = cp_parser_simple_requirement (parser);
22363 return req;
22366 // Parse a nested requirement. This is the same as a requires clause.
22368 // nested-requirement:
22369 // requires-clause
22370 static tree
22371 cp_parser_nested_requirement (cp_parser *parser)
22373 cp_lexer_consume_token (parser->lexer);
22374 tree req = cp_parser_requires_clause (parser);
22375 if (req == error_mark_node)
22376 return error_mark_node;
22377 return finish_nested_requirement (req);
22380 // Parse a simple requirement.
22382 // simple-requirement:
22383 // expression
22384 static tree
22385 cp_parser_simple_requirement (cp_parser *parser)
22387 tree expr = cp_parser_expression (parser, false, false, NULL);
22388 if (!expr || expr == error_mark_node)
22389 return error_mark_node;
22390 return finish_expr_requirement (expr);
22393 // Parse a compound requirement
22395 // compound-requirement:
22396 // '{' expression '}' trailing-constraint-specifiers
22398 // trailing-constraint-specifiers:
22399 // constraint-specifiers-seq[opt] result-type-requirement[opt]
22401 // result-type-requirement:
22402 // '->' type-id
22403 static tree
22404 cp_parser_compound_requirement (cp_parser *parser)
22406 // Parse an expression enclosed in '{ }'s.
22407 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22408 return error_mark_node;
22410 tree expr = cp_parser_expression (parser, false, false, NULL);
22411 if (!expr || expr == error_mark_node)
22412 return error_mark_node;
22414 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
22415 return error_mark_node;
22417 // Parse trailing expression specifiers.
22418 tree cs = cp_parser_constraint_specifier_seq (parser, expr);
22420 // Parse the optional trailing type requirement.
22421 tree type = NULL_TREE;
22422 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
22424 cp_lexer_consume_token (parser->lexer);
22425 type = cp_parser_trailing_type_id (parser);
22426 if (type == error_mark_node)
22427 return error_mark_node;
22430 return finish_expr_requirement (expr, type, cs);
22433 // Parse a type requirement
22435 // type-requirement
22436 // type-id
22437 static tree
22438 cp_parser_type_requirement (cp_parser *parser)
22440 // Try parsing the type-id
22441 tree type = cp_parser_type_id (parser);
22442 if (type == error_mark_node)
22443 return error_mark_node;
22445 // It can only be a type requirement if nothing comes after it.
22446 // For example, this:
22448 // typename T::value_type x = a;
22450 // Is not a type requirement even though it stars with a type-id.
22451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22452 return error_mark_node;
22454 return finish_type_requirement (type);
22457 // Parse an optional constexpr specifier in a constraint expression.
22458 static tree
22459 cp_parser_constexpr_constraint_spec (cp_parser *parser, tree expr)
22461 cp_lexer_consume_token (parser->lexer);
22462 return finish_constexpr_requirement (expr);
22465 // Parse an optional noexcept specifier in a constraint expression.
22466 static tree
22467 cp_parser_noexcept_constraint_spec (cp_parser *parser, tree expr)
22469 cp_lexer_consume_token (parser->lexer);
22470 return finish_noexcept_requirement (expr);
22473 static tree
22474 cp_parser_constraint_spec (cp_parser *parser, tree expr)
22476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONSTEXPR))
22477 return cp_parser_constexpr_constraint_spec (parser, expr);
22478 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
22479 return cp_parser_noexcept_constraint_spec (parser, expr);
22480 return NULL_TREE;
22483 // Parse an optional expression specifier sequence.
22485 // constraint-specifier-sequence:
22486 // constexpr [opt] noexcept [opt]
22487 static tree
22488 cp_parser_constraint_specifier_seq (cp_parser *parser, tree expr)
22490 tree result = NULL_TREE;
22491 while (1)
22493 // If we can parse a constraint specifier, insert it into
22494 // the list of requirements.
22495 if (tree spec = cp_parser_constraint_spec (parser, expr))
22497 result = tree_cons (NULL_TREE, spec, result);
22498 continue;
22500 break;
22502 return result;
22505 /* Support Functions */
22507 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22508 NAME should have one of the representations used for an
22509 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22510 is returned. If PARSER->SCOPE is a dependent type, then a
22511 SCOPE_REF is returned.
22513 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22514 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22515 was formed. Abstractly, such entities should not be passed to this
22516 function, because they do not need to be looked up, but it is
22517 simpler to check for this special case here, rather than at the
22518 call-sites.
22520 In cases not explicitly covered above, this function returns a
22521 DECL, OVERLOAD, or baselink representing the result of the lookup.
22522 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22523 is returned.
22525 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22526 (e.g., "struct") that was used. In that case bindings that do not
22527 refer to types are ignored.
22529 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22530 ignored.
22532 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22533 are ignored.
22535 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22536 types.
22538 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22539 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22540 NULL_TREE otherwise. */
22542 static tree
22543 cp_parser_lookup_name (cp_parser *parser, tree name,
22544 enum tag_types tag_type,
22545 bool is_template,
22546 bool is_namespace,
22547 bool check_dependency,
22548 tree *ambiguous_decls,
22549 location_t name_location)
22551 tree decl;
22552 tree object_type = parser->context->object_type;
22554 /* Assume that the lookup will be unambiguous. */
22555 if (ambiguous_decls)
22556 *ambiguous_decls = NULL_TREE;
22558 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22559 no longer valid. Note that if we are parsing tentatively, and
22560 the parse fails, OBJECT_TYPE will be automatically restored. */
22561 parser->context->object_type = NULL_TREE;
22563 if (name == error_mark_node)
22564 return error_mark_node;
22566 /* A template-id has already been resolved; there is no lookup to
22567 do. */
22568 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22569 return name;
22570 if (BASELINK_P (name))
22572 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22573 == TEMPLATE_ID_EXPR);
22574 return name;
22577 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22578 it should already have been checked to make sure that the name
22579 used matches the type being destroyed. */
22580 if (TREE_CODE (name) == BIT_NOT_EXPR)
22582 tree type;
22584 /* Figure out to which type this destructor applies. */
22585 if (parser->scope)
22586 type = parser->scope;
22587 else if (object_type)
22588 type = object_type;
22589 else
22590 type = current_class_type;
22591 /* If that's not a class type, there is no destructor. */
22592 if (!type || !CLASS_TYPE_P (type))
22593 return error_mark_node;
22594 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22595 lazily_declare_fn (sfk_destructor, type);
22596 if (!CLASSTYPE_DESTRUCTORS (type))
22597 return error_mark_node;
22598 /* If it was a class type, return the destructor. */
22599 return CLASSTYPE_DESTRUCTORS (type);
22602 /* By this point, the NAME should be an ordinary identifier. If
22603 the id-expression was a qualified name, the qualifying scope is
22604 stored in PARSER->SCOPE at this point. */
22605 gcc_assert (identifier_p (name));
22607 /* Perform the lookup. */
22608 if (parser->scope)
22610 bool dependent_p;
22612 if (parser->scope == error_mark_node)
22613 return error_mark_node;
22615 /* If the SCOPE is dependent, the lookup must be deferred until
22616 the template is instantiated -- unless we are explicitly
22617 looking up names in uninstantiated templates. Even then, we
22618 cannot look up the name if the scope is not a class type; it
22619 might, for example, be a template type parameter. */
22620 dependent_p = (TYPE_P (parser->scope)
22621 && dependent_scope_p (parser->scope));
22622 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22623 && dependent_p)
22624 /* Defer lookup. */
22625 decl = error_mark_node;
22626 else
22628 tree pushed_scope = NULL_TREE;
22630 /* If PARSER->SCOPE is a dependent type, then it must be a
22631 class type, and we must not be checking dependencies;
22632 otherwise, we would have processed this lookup above. So
22633 that PARSER->SCOPE is not considered a dependent base by
22634 lookup_member, we must enter the scope here. */
22635 if (dependent_p)
22636 pushed_scope = push_scope (parser->scope);
22638 /* If the PARSER->SCOPE is a template specialization, it
22639 may be instantiated during name lookup. In that case,
22640 errors may be issued. Even if we rollback the current
22641 tentative parse, those errors are valid. */
22642 decl = lookup_qualified_name (parser->scope, name,
22643 tag_type != none_type,
22644 /*complain=*/true);
22646 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22647 lookup result and the nested-name-specifier nominates a class C:
22648 * if the name specified after the nested-name-specifier, when
22649 looked up in C, is the injected-class-name of C (Clause 9), or
22650 * if the name specified after the nested-name-specifier is the
22651 same as the identifier or the simple-template-id's template-
22652 name in the last component of the nested-name-specifier,
22653 the name is instead considered to name the constructor of
22654 class C. [ Note: for example, the constructor is not an
22655 acceptable lookup result in an elaborated-type-specifier so
22656 the constructor would not be used in place of the
22657 injected-class-name. --end note ] Such a constructor name
22658 shall be used only in the declarator-id of a declaration that
22659 names a constructor or in a using-declaration. */
22660 if (tag_type == none_type
22661 && DECL_SELF_REFERENCE_P (decl)
22662 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22663 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22664 tag_type != none_type,
22665 /*complain=*/true);
22667 /* If we have a single function from a using decl, pull it out. */
22668 if (TREE_CODE (decl) == OVERLOAD
22669 && !really_overloaded_fn (decl))
22670 decl = OVL_FUNCTION (decl);
22672 if (pushed_scope)
22673 pop_scope (pushed_scope);
22676 /* If the scope is a dependent type and either we deferred lookup or
22677 we did lookup but didn't find the name, rememeber the name. */
22678 if (decl == error_mark_node && TYPE_P (parser->scope)
22679 && dependent_type_p (parser->scope))
22681 if (tag_type)
22683 tree type;
22685 /* The resolution to Core Issue 180 says that `struct
22686 A::B' should be considered a type-name, even if `A'
22687 is dependent. */
22688 type = make_typename_type (parser->scope, name, tag_type,
22689 /*complain=*/tf_error);
22690 if (type != error_mark_node)
22691 decl = TYPE_NAME (type);
22693 else if (is_template
22694 && (cp_parser_next_token_ends_template_argument_p (parser)
22695 || cp_lexer_next_token_is (parser->lexer,
22696 CPP_CLOSE_PAREN)))
22697 decl = make_unbound_class_template (parser->scope,
22698 name, NULL_TREE,
22699 /*complain=*/tf_error);
22700 else
22701 decl = build_qualified_name (/*type=*/NULL_TREE,
22702 parser->scope, name,
22703 is_template);
22705 parser->qualifying_scope = parser->scope;
22706 parser->object_scope = NULL_TREE;
22708 else if (object_type)
22710 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22711 OBJECT_TYPE is not a class. */
22712 if (CLASS_TYPE_P (object_type))
22713 /* If the OBJECT_TYPE is a template specialization, it may
22714 be instantiated during name lookup. In that case, errors
22715 may be issued. Even if we rollback the current tentative
22716 parse, those errors are valid. */
22717 decl = lookup_member (object_type,
22718 name,
22719 /*protect=*/0,
22720 tag_type != none_type,
22721 tf_warning_or_error);
22722 else
22723 decl = NULL_TREE;
22725 if (!decl)
22726 /* Look it up in the enclosing context. */
22727 decl = lookup_name_real (name, tag_type != none_type,
22728 /*nonclass=*/0,
22729 /*block_p=*/true, is_namespace, 0);
22730 parser->object_scope = object_type;
22731 parser->qualifying_scope = NULL_TREE;
22733 else
22735 decl = lookup_name_real (name, tag_type != none_type,
22736 /*nonclass=*/0,
22737 /*block_p=*/true, is_namespace, 0);
22738 parser->qualifying_scope = NULL_TREE;
22739 parser->object_scope = NULL_TREE;
22742 /* If the lookup failed, let our caller know. */
22743 if (!decl || decl == error_mark_node)
22744 return error_mark_node;
22746 /* Pull out the template from an injected-class-name (or multiple). */
22747 if (is_template)
22748 decl = maybe_get_template_decl_from_type_decl (decl);
22750 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22751 if (TREE_CODE (decl) == TREE_LIST)
22753 if (ambiguous_decls)
22754 *ambiguous_decls = decl;
22755 /* The error message we have to print is too complicated for
22756 cp_parser_error, so we incorporate its actions directly. */
22757 if (!cp_parser_simulate_error (parser))
22759 error_at (name_location, "reference to %qD is ambiguous",
22760 name);
22761 print_candidates (decl);
22763 return error_mark_node;
22766 gcc_assert (DECL_P (decl)
22767 || TREE_CODE (decl) == OVERLOAD
22768 || TREE_CODE (decl) == SCOPE_REF
22769 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22770 || BASELINK_P (decl));
22772 /* If we have resolved the name of a member declaration, check to
22773 see if the declaration is accessible. When the name resolves to
22774 set of overloaded functions, accessibility is checked when
22775 overload resolution is done.
22777 During an explicit instantiation, access is not checked at all,
22778 as per [temp.explicit]. */
22779 if (DECL_P (decl))
22780 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22782 maybe_record_typedef_use (decl);
22784 return decl;
22787 /* Like cp_parser_lookup_name, but for use in the typical case where
22788 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22789 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22791 static tree
22792 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22794 return cp_parser_lookup_name (parser, name,
22795 none_type,
22796 /*is_template=*/false,
22797 /*is_namespace=*/false,
22798 /*check_dependency=*/true,
22799 /*ambiguous_decls=*/NULL,
22800 location);
22803 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22804 the current context, return the TYPE_DECL. If TAG_NAME_P is
22805 true, the DECL indicates the class being defined in a class-head,
22806 or declared in an elaborated-type-specifier.
22808 Otherwise, return DECL. */
22810 static tree
22811 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22813 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22814 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22816 struct A {
22817 template <typename T> struct B;
22820 template <typename T> struct A::B {};
22822 Similarly, in an elaborated-type-specifier:
22824 namespace N { struct X{}; }
22826 struct A {
22827 template <typename T> friend struct N::X;
22830 However, if the DECL refers to a class type, and we are in
22831 the scope of the class, then the name lookup automatically
22832 finds the TYPE_DECL created by build_self_reference rather
22833 than a TEMPLATE_DECL. For example, in:
22835 template <class T> struct S {
22836 S s;
22839 there is no need to handle such case. */
22841 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22842 return DECL_TEMPLATE_RESULT (decl);
22844 return decl;
22847 /* If too many, or too few, template-parameter lists apply to the
22848 declarator, issue an error message. Returns TRUE if all went well,
22849 and FALSE otherwise. */
22851 static bool
22852 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22853 cp_declarator *declarator,
22854 location_t declarator_location)
22856 switch (declarator->kind)
22858 case cdk_id:
22860 unsigned num_templates = 0;
22861 tree scope = declarator->u.id.qualifying_scope;
22863 if (scope)
22864 num_templates = num_template_headers_for_class (scope);
22865 else if (TREE_CODE (declarator->u.id.unqualified_name)
22866 == TEMPLATE_ID_EXPR)
22867 /* If the DECLARATOR has the form `X<y>' then it uses one
22868 additional level of template parameters. */
22869 ++num_templates;
22871 return cp_parser_check_template_parameters
22872 (parser, num_templates, declarator_location, declarator);
22875 case cdk_function:
22876 case cdk_array:
22877 case cdk_pointer:
22878 case cdk_reference:
22879 case cdk_ptrmem:
22880 return (cp_parser_check_declarator_template_parameters
22881 (parser, declarator->declarator, declarator_location));
22883 case cdk_error:
22884 return true;
22886 default:
22887 gcc_unreachable ();
22889 return false;
22892 /* NUM_TEMPLATES were used in the current declaration. If that is
22893 invalid, return FALSE and issue an error messages. Otherwise,
22894 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22895 declarator and we can print more accurate diagnostics. */
22897 static bool
22898 cp_parser_check_template_parameters (cp_parser* parser,
22899 unsigned num_templates,
22900 location_t location,
22901 cp_declarator *declarator)
22903 /* If there are the same number of template classes and parameter
22904 lists, that's OK. */
22905 if (parser->num_template_parameter_lists == num_templates)
22906 return true;
22907 /* If there are more, but only one more, then we are referring to a
22908 member template. That's OK too. */
22909 if (parser->num_template_parameter_lists == num_templates + 1)
22910 return true;
22911 /* If there are more template classes than parameter lists, we have
22912 something like:
22914 template <class T> void S<T>::R<T>::f (); */
22915 if (parser->num_template_parameter_lists < num_templates)
22917 if (declarator && !current_function_decl)
22918 error_at (location, "specializing member %<%T::%E%> "
22919 "requires %<template<>%> syntax",
22920 declarator->u.id.qualifying_scope,
22921 declarator->u.id.unqualified_name);
22922 else if (declarator)
22923 error_at (location, "invalid declaration of %<%T::%E%>",
22924 declarator->u.id.qualifying_scope,
22925 declarator->u.id.unqualified_name);
22926 else
22927 error_at (location, "too few template-parameter-lists");
22928 return false;
22930 /* Otherwise, there are too many template parameter lists. We have
22931 something like:
22933 template <class T> template <class U> void S::f(); */
22934 error_at (location, "too many template-parameter-lists");
22935 return false;
22938 /* Parse an optional `::' token indicating that the following name is
22939 from the global namespace. If so, PARSER->SCOPE is set to the
22940 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22941 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22942 Returns the new value of PARSER->SCOPE, if the `::' token is
22943 present, and NULL_TREE otherwise. */
22945 static tree
22946 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22948 cp_token *token;
22950 /* Peek at the next token. */
22951 token = cp_lexer_peek_token (parser->lexer);
22952 /* If we're looking at a `::' token then we're starting from the
22953 global namespace, not our current location. */
22954 if (token->type == CPP_SCOPE)
22956 /* Consume the `::' token. */
22957 cp_lexer_consume_token (parser->lexer);
22958 /* Set the SCOPE so that we know where to start the lookup. */
22959 parser->scope = global_namespace;
22960 parser->qualifying_scope = global_namespace;
22961 parser->object_scope = NULL_TREE;
22963 return parser->scope;
22965 else if (!current_scope_valid_p)
22967 parser->scope = NULL_TREE;
22968 parser->qualifying_scope = NULL_TREE;
22969 parser->object_scope = NULL_TREE;
22972 return NULL_TREE;
22975 /* Returns TRUE if the upcoming token sequence is the start of a
22976 constructor declarator. If FRIEND_P is true, the declarator is
22977 preceded by the `friend' specifier. */
22979 static bool
22980 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22982 bool constructor_p;
22983 bool outside_class_specifier_p;
22984 tree nested_name_specifier;
22985 cp_token *next_token;
22987 /* The common case is that this is not a constructor declarator, so
22988 try to avoid doing lots of work if at all possible. It's not
22989 valid declare a constructor at function scope. */
22990 if (parser->in_function_body)
22991 return false;
22992 /* And only certain tokens can begin a constructor declarator. */
22993 next_token = cp_lexer_peek_token (parser->lexer);
22994 if (next_token->type != CPP_NAME
22995 && next_token->type != CPP_SCOPE
22996 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22997 && next_token->type != CPP_TEMPLATE_ID)
22998 return false;
23000 /* Parse tentatively; we are going to roll back all of the tokens
23001 consumed here. */
23002 cp_parser_parse_tentatively (parser);
23003 /* Assume that we are looking at a constructor declarator. */
23004 constructor_p = true;
23006 /* Look for the optional `::' operator. */
23007 cp_parser_global_scope_opt (parser,
23008 /*current_scope_valid_p=*/false);
23009 /* Look for the nested-name-specifier. */
23010 nested_name_specifier
23011 = (cp_parser_nested_name_specifier_opt (parser,
23012 /*typename_keyword_p=*/false,
23013 /*check_dependency_p=*/false,
23014 /*type_p=*/false,
23015 /*is_declaration=*/false));
23017 outside_class_specifier_p = (!at_class_scope_p ()
23018 || !TYPE_BEING_DEFINED (current_class_type)
23019 || friend_p);
23021 /* Outside of a class-specifier, there must be a
23022 nested-name-specifier. */
23023 if (!nested_name_specifier && outside_class_specifier_p)
23024 constructor_p = false;
23025 else if (nested_name_specifier == error_mark_node)
23026 constructor_p = false;
23028 /* If we have a class scope, this is easy; DR 147 says that S::S always
23029 names the constructor, and no other qualified name could. */
23030 if (constructor_p && nested_name_specifier
23031 && CLASS_TYPE_P (nested_name_specifier))
23033 tree id = cp_parser_unqualified_id (parser,
23034 /*template_keyword_p=*/false,
23035 /*check_dependency_p=*/false,
23036 /*declarator_p=*/true,
23037 /*optional_p=*/false);
23038 if (is_overloaded_fn (id))
23039 id = DECL_NAME (get_first_fn (id));
23040 if (!constructor_name_p (id, nested_name_specifier))
23041 constructor_p = false;
23043 /* If we still think that this might be a constructor-declarator,
23044 look for a class-name. */
23045 else if (constructor_p)
23047 /* If we have:
23049 template <typename T> struct S {
23050 S();
23053 we must recognize that the nested `S' names a class. */
23054 tree type_decl;
23055 type_decl = cp_parser_class_name (parser,
23056 /*typename_keyword_p=*/false,
23057 /*template_keyword_p=*/false,
23058 none_type,
23059 /*check_dependency_p=*/false,
23060 /*class_head_p=*/false,
23061 /*is_declaration=*/false);
23062 /* If there was no class-name, then this is not a constructor.
23063 Otherwise, if we are in a class-specifier and we aren't
23064 handling a friend declaration, check that its type matches
23065 current_class_type (c++/38313). Note: error_mark_node
23066 is left alone for error recovery purposes. */
23067 constructor_p = (!cp_parser_error_occurred (parser)
23068 && (outside_class_specifier_p
23069 || type_decl == error_mark_node
23070 || same_type_p (current_class_type,
23071 TREE_TYPE (type_decl))));
23073 /* If we're still considering a constructor, we have to see a `(',
23074 to begin the parameter-declaration-clause, followed by either a
23075 `)', an `...', or a decl-specifier. We need to check for a
23076 type-specifier to avoid being fooled into thinking that:
23078 S (f) (int);
23080 is a constructor. (It is actually a function named `f' that
23081 takes one parameter (of type `int') and returns a value of type
23082 `S'. */
23083 if (constructor_p
23084 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23085 constructor_p = false;
23087 if (constructor_p
23088 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23089 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23090 /* A parameter declaration begins with a decl-specifier,
23091 which is either the "attribute" keyword, a storage class
23092 specifier, or (usually) a type-specifier. */
23093 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23095 tree type;
23096 tree pushed_scope = NULL_TREE;
23097 unsigned saved_num_template_parameter_lists;
23099 /* Names appearing in the type-specifier should be looked up
23100 in the scope of the class. */
23101 if (current_class_type)
23102 type = NULL_TREE;
23103 else
23105 type = TREE_TYPE (type_decl);
23106 if (TREE_CODE (type) == TYPENAME_TYPE)
23108 type = resolve_typename_type (type,
23109 /*only_current_p=*/false);
23110 if (TREE_CODE (type) == TYPENAME_TYPE)
23112 cp_parser_abort_tentative_parse (parser);
23113 return false;
23116 pushed_scope = push_scope (type);
23119 /* Inside the constructor parameter list, surrounding
23120 template-parameter-lists do not apply. */
23121 saved_num_template_parameter_lists
23122 = parser->num_template_parameter_lists;
23123 parser->num_template_parameter_lists = 0;
23125 /* Look for the type-specifier. */
23126 cp_parser_type_specifier (parser,
23127 CP_PARSER_FLAGS_NONE,
23128 /*decl_specs=*/NULL,
23129 /*is_declarator=*/true,
23130 /*declares_class_or_enum=*/NULL,
23131 /*is_cv_qualifier=*/NULL);
23133 parser->num_template_parameter_lists
23134 = saved_num_template_parameter_lists;
23136 /* Leave the scope of the class. */
23137 if (pushed_scope)
23138 pop_scope (pushed_scope);
23140 constructor_p = !cp_parser_error_occurred (parser);
23144 /* We did not really want to consume any tokens. */
23145 cp_parser_abort_tentative_parse (parser);
23147 return constructor_p;
23150 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23151 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23152 they must be performed once we are in the scope of the function.
23154 Returns the function defined. */
23156 static tree
23157 cp_parser_function_definition_from_specifiers_and_declarator
23158 (cp_parser* parser,
23159 cp_decl_specifier_seq *decl_specifiers,
23160 tree attributes,
23161 const cp_declarator *declarator)
23163 tree fn;
23164 bool success_p;
23166 /* Begin the function-definition. */
23167 success_p = start_function (decl_specifiers, declarator, attributes);
23169 /* The things we're about to see are not directly qualified by any
23170 template headers we've seen thus far. */
23171 reset_specialization ();
23173 /* If there were names looked up in the decl-specifier-seq that we
23174 did not check, check them now. We must wait until we are in the
23175 scope of the function to perform the checks, since the function
23176 might be a friend. */
23177 perform_deferred_access_checks (tf_warning_or_error);
23179 if (success_p)
23181 cp_finalize_omp_declare_simd (parser, current_function_decl);
23182 parser->omp_declare_simd = NULL;
23185 if (!success_p)
23187 /* Skip the entire function. */
23188 cp_parser_skip_to_end_of_block_or_statement (parser);
23189 fn = error_mark_node;
23191 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23193 /* Seen already, skip it. An error message has already been output. */
23194 cp_parser_skip_to_end_of_block_or_statement (parser);
23195 fn = current_function_decl;
23196 current_function_decl = NULL_TREE;
23197 /* If this is a function from a class, pop the nested class. */
23198 if (current_class_name)
23199 pop_nested_class ();
23201 else
23203 timevar_id_t tv;
23204 if (DECL_DECLARED_INLINE_P (current_function_decl))
23205 tv = TV_PARSE_INLINE;
23206 else
23207 tv = TV_PARSE_FUNC;
23208 timevar_push (tv);
23209 fn = cp_parser_function_definition_after_declarator (parser,
23210 /*inline_p=*/false);
23211 timevar_pop (tv);
23214 return fn;
23217 /* Parse the part of a function-definition that follows the
23218 declarator. INLINE_P is TRUE iff this function is an inline
23219 function defined within a class-specifier.
23221 Returns the function defined. */
23223 static tree
23224 cp_parser_function_definition_after_declarator (cp_parser* parser,
23225 bool inline_p)
23227 tree fn;
23228 bool ctor_initializer_p = false;
23229 bool saved_in_unbraced_linkage_specification_p;
23230 bool saved_in_function_body;
23231 unsigned saved_num_template_parameter_lists;
23232 cp_token *token;
23233 bool fully_implicit_function_template_p
23234 = parser->fully_implicit_function_template_p;
23235 parser->fully_implicit_function_template_p = false;
23236 tree implicit_template_parms
23237 = parser->implicit_template_parms;
23238 parser->implicit_template_parms = 0;
23239 cp_binding_level* implicit_template_scope
23240 = parser->implicit_template_scope;
23241 parser->implicit_template_scope = 0;
23243 saved_in_function_body = parser->in_function_body;
23244 parser->in_function_body = true;
23245 /* If the next token is `return', then the code may be trying to
23246 make use of the "named return value" extension that G++ used to
23247 support. */
23248 token = cp_lexer_peek_token (parser->lexer);
23249 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23251 /* Consume the `return' keyword. */
23252 cp_lexer_consume_token (parser->lexer);
23253 /* Look for the identifier that indicates what value is to be
23254 returned. */
23255 cp_parser_identifier (parser);
23256 /* Issue an error message. */
23257 error_at (token->location,
23258 "named return values are no longer supported");
23259 /* Skip tokens until we reach the start of the function body. */
23260 while (true)
23262 cp_token *token = cp_lexer_peek_token (parser->lexer);
23263 if (token->type == CPP_OPEN_BRACE
23264 || token->type == CPP_EOF
23265 || token->type == CPP_PRAGMA_EOL)
23266 break;
23267 cp_lexer_consume_token (parser->lexer);
23270 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23271 anything declared inside `f'. */
23272 saved_in_unbraced_linkage_specification_p
23273 = parser->in_unbraced_linkage_specification_p;
23274 parser->in_unbraced_linkage_specification_p = false;
23275 /* Inside the function, surrounding template-parameter-lists do not
23276 apply. */
23277 saved_num_template_parameter_lists
23278 = parser->num_template_parameter_lists;
23279 parser->num_template_parameter_lists = 0;
23281 start_lambda_scope (current_function_decl);
23283 /* If the next token is `try', `__transaction_atomic', or
23284 `__transaction_relaxed`, then we are looking at either function-try-block
23285 or function-transaction-block. Note that all of these include the
23286 function-body. */
23287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23288 ctor_initializer_p = cp_parser_function_transaction (parser,
23289 RID_TRANSACTION_ATOMIC);
23290 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23291 RID_TRANSACTION_RELAXED))
23292 ctor_initializer_p = cp_parser_function_transaction (parser,
23293 RID_TRANSACTION_RELAXED);
23294 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23295 ctor_initializer_p = cp_parser_function_try_block (parser);
23296 else
23297 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23298 (parser, /*in_function_try_block=*/false);
23300 finish_lambda_scope ();
23302 /* Finish the function. */
23303 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23304 (inline_p ? 2 : 0));
23305 /* Generate code for it, if necessary. */
23306 expand_or_defer_fn (fn);
23307 /* Restore the saved values. */
23308 parser->in_unbraced_linkage_specification_p
23309 = saved_in_unbraced_linkage_specification_p;
23310 parser->num_template_parameter_lists
23311 = saved_num_template_parameter_lists;
23312 parser->in_function_body = saved_in_function_body;
23314 parser->fully_implicit_function_template_p
23315 = fully_implicit_function_template_p;
23316 parser->implicit_template_parms
23317 = implicit_template_parms;
23318 parser->implicit_template_scope
23319 = implicit_template_scope;
23321 if (parser->fully_implicit_function_template_p) {
23322 // Attach implicit constraints to the function template.
23323 tree constr = get_shorthand_requirements (current_template_parms);
23324 tree cinfo = finish_template_requirements (constr);
23325 DECL_CONSTRAINTS (DECL_TI_TEMPLATE (fn)) = cinfo;
23326 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23329 return fn;
23332 /* Parse a template-declaration, assuming that the `export' (and
23333 `extern') keywords, if present, has already been scanned. MEMBER_P
23334 is as for cp_parser_template_declaration. */
23336 static void
23337 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23339 tree decl = NULL_TREE;
23340 vec<deferred_access_check, va_gc> *checks;
23341 tree parameter_list;
23342 bool friend_p = false;
23343 bool need_lang_pop;
23344 cp_token *token;
23346 /* Look for the `template' keyword. */
23347 token = cp_lexer_peek_token (parser->lexer);
23348 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23349 return;
23351 /* And the `<'. */
23352 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23353 return;
23354 if (at_class_scope_p () && current_function_decl)
23356 /* 14.5.2.2 [temp.mem]
23358 A local class shall not have member templates. */
23359 error_at (token->location,
23360 "invalid declaration of member template in local class");
23361 cp_parser_skip_to_end_of_block_or_statement (parser);
23362 return;
23364 /* [temp]
23366 A template ... shall not have C linkage. */
23367 if (current_lang_name == lang_name_c)
23369 error_at (token->location, "template with C linkage");
23370 /* Give it C++ linkage to avoid confusing other parts of the
23371 front end. */
23372 push_lang_context (lang_name_cplusplus);
23373 need_lang_pop = true;
23375 else
23376 need_lang_pop = false;
23378 /* We cannot perform access checks on the template parameter
23379 declarations until we know what is being declared, just as we
23380 cannot check the decl-specifier list. */
23381 push_deferring_access_checks (dk_deferred);
23383 // Save the current template requirements.
23384 tree saved_template_reqs = release (current_template_reqs);
23386 /* If the next token is `>', then we have an invalid
23387 specialization. Rather than complain about an invalid template
23388 parameter, issue an error message here. */
23389 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23391 cp_parser_error (parser, "invalid explicit specialization");
23392 begin_specialization ();
23393 parameter_list = NULL_TREE;
23395 else
23397 /* Parse the template parameters. */
23398 parameter_list = cp_parser_template_parameter_list (parser);
23401 /* Get the deferred access checks from the parameter list. These
23402 will be checked once we know what is being declared, as for a
23403 member template the checks must be performed in the scope of the
23404 class containing the member. */
23405 checks = get_deferred_access_checks ();
23407 /* Look for the `>'. */
23408 cp_parser_skip_to_end_of_template_parameter_list (parser);
23409 /* We just processed one more parameter list. */
23410 ++parser->num_template_parameter_lists;
23412 // Manage template requirements
23413 if (flag_concepts)
23415 tree reqs = get_shorthand_requirements (current_template_parms);
23416 if (tree r = cp_parser_requires_clause_opt (parser))
23417 reqs = conjoin_requirements (reqs, r);
23418 current_template_reqs = finish_template_requirements (reqs);
23420 // Attach the constraints to the template parameter list.
23421 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
23422 = current_template_reqs;
23425 /* If the next token is `template', there are more template
23426 parameters. */
23427 if (cp_lexer_next_token_is_keyword (parser->lexer,
23428 RID_TEMPLATE))
23429 cp_parser_template_declaration_after_export (parser, member_p);
23430 else if (cxx_dialect >= cxx11
23431 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23432 decl = cp_parser_alias_declaration (parser);
23433 else
23435 /* There are no access checks when parsing a template, as we do not
23436 know if a specialization will be a friend. */
23437 push_deferring_access_checks (dk_no_check);
23438 token = cp_lexer_peek_token (parser->lexer);
23439 decl = cp_parser_single_declaration (parser,
23440 checks,
23441 member_p,
23442 /*explicit_specialization_p=*/false,
23443 &friend_p);
23444 pop_deferring_access_checks ();
23446 /* If this is a member template declaration, let the front
23447 end know. */
23448 if (member_p && !friend_p && decl)
23450 if (TREE_CODE (decl) == TYPE_DECL)
23451 cp_parser_check_access_in_redeclaration (decl, token->location);
23453 decl = finish_member_template_decl (decl);
23455 else if (friend_p && decl
23456 && DECL_DECLARES_TYPE_P (decl))
23457 make_friend_class (current_class_type, TREE_TYPE (decl),
23458 /*complain=*/true);
23460 /* We are done with the current parameter list. */
23461 --parser->num_template_parameter_lists;
23463 pop_deferring_access_checks ();
23465 /* Finish up. */
23466 finish_template_decl (parameter_list);
23468 // Restore the current template requirements.
23469 current_template_reqs = saved_template_reqs;
23471 /* Check the template arguments for a literal operator template. */
23472 if (decl
23473 && DECL_DECLARES_FUNCTION_P (decl)
23474 && UDLIT_OPER_P (DECL_NAME (decl)))
23476 bool ok = true;
23477 if (parameter_list == NULL_TREE)
23478 ok = false;
23479 else
23481 int num_parms = TREE_VEC_LENGTH (parameter_list);
23482 if (num_parms == 1)
23484 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23485 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23486 if (TREE_TYPE (parm) != char_type_node
23487 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23488 ok = false;
23490 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23492 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23493 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23494 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23495 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23496 if (TREE_TYPE (parm) != TREE_TYPE (type)
23497 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23498 ok = false;
23500 else
23501 ok = false;
23503 if (!ok)
23504 error ("literal operator template %qD has invalid parameter list."
23505 " Expected non-type template argument pack <char...>"
23506 " or <typename CharT, CharT...>",
23507 decl);
23509 /* Register member declarations. */
23510 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23511 finish_member_declaration (decl);
23512 /* For the erroneous case of a template with C linkage, we pushed an
23513 implicit C++ linkage scope; exit that scope now. */
23514 if (need_lang_pop)
23515 pop_lang_context ();
23516 /* If DECL is a function template, we must return to parse it later.
23517 (Even though there is no definition, there might be default
23518 arguments that need handling.) */
23519 if (member_p && decl
23520 && DECL_DECLARES_FUNCTION_P (decl))
23521 vec_safe_push (unparsed_funs_with_definitions, decl);
23524 /* Perform the deferred access checks from a template-parameter-list.
23525 CHECKS is a TREE_LIST of access checks, as returned by
23526 get_deferred_access_checks. */
23528 static void
23529 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23531 ++processing_template_parmlist;
23532 perform_access_checks (checks, tf_warning_or_error);
23533 --processing_template_parmlist;
23536 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23537 `function-definition' sequence that follows a template header.
23538 If MEMBER_P is true, this declaration appears in a class scope.
23540 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23541 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23543 static tree
23544 cp_parser_single_declaration (cp_parser* parser,
23545 vec<deferred_access_check, va_gc> *checks,
23546 bool member_p,
23547 bool explicit_specialization_p,
23548 bool* friend_p)
23550 int declares_class_or_enum;
23551 tree decl = NULL_TREE;
23552 cp_decl_specifier_seq decl_specifiers;
23553 bool function_definition_p = false;
23554 cp_token *decl_spec_token_start;
23556 /* This function is only used when processing a template
23557 declaration. */
23558 gcc_assert (innermost_scope_kind () == sk_template_parms
23559 || innermost_scope_kind () == sk_template_spec);
23561 /* Defer access checks until we know what is being declared. */
23562 push_deferring_access_checks (dk_deferred);
23564 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23565 alternative. */
23566 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23567 cp_parser_decl_specifier_seq (parser,
23568 CP_PARSER_FLAGS_OPTIONAL,
23569 &decl_specifiers,
23570 &declares_class_or_enum);
23571 if (friend_p)
23572 *friend_p = cp_parser_friend_p (&decl_specifiers);
23574 /* There are no template typedefs. */
23575 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23577 error_at (decl_spec_token_start->location,
23578 "template declaration of %<typedef%>");
23579 decl = error_mark_node;
23582 /* Gather up the access checks that occurred the
23583 decl-specifier-seq. */
23584 stop_deferring_access_checks ();
23586 /* Check for the declaration of a template class. */
23587 if (declares_class_or_enum)
23589 if (cp_parser_declares_only_class_p (parser))
23591 decl = shadow_tag (&decl_specifiers);
23593 /* In this case:
23595 struct C {
23596 friend template <typename T> struct A<T>::B;
23599 A<T>::B will be represented by a TYPENAME_TYPE, and
23600 therefore not recognized by shadow_tag. */
23601 if (friend_p && *friend_p
23602 && !decl
23603 && decl_specifiers.type
23604 && TYPE_P (decl_specifiers.type))
23605 decl = decl_specifiers.type;
23607 if (decl && decl != error_mark_node)
23608 decl = TYPE_NAME (decl);
23609 else
23610 decl = error_mark_node;
23612 /* Perform access checks for template parameters. */
23613 cp_parser_perform_template_parameter_access_checks (checks);
23617 /* Complain about missing 'typename' or other invalid type names. */
23618 if (!decl_specifiers.any_type_specifiers_p
23619 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23621 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23622 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23623 the rest of this declaration. */
23624 decl = error_mark_node;
23625 goto out;
23628 /* If it's not a template class, try for a template function. If
23629 the next token is a `;', then this declaration does not declare
23630 anything. But, if there were errors in the decl-specifiers, then
23631 the error might well have come from an attempted class-specifier.
23632 In that case, there's no need to warn about a missing declarator. */
23633 if (!decl
23634 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23635 || decl_specifiers.type != error_mark_node))
23637 decl = cp_parser_init_declarator (parser,
23638 &decl_specifiers,
23639 checks,
23640 /*function_definition_allowed_p=*/true,
23641 member_p,
23642 declares_class_or_enum,
23643 &function_definition_p,
23644 NULL);
23646 /* 7.1.1-1 [dcl.stc]
23648 A storage-class-specifier shall not be specified in an explicit
23649 specialization... */
23650 if (decl
23651 && explicit_specialization_p
23652 && decl_specifiers.storage_class != sc_none)
23654 error_at (decl_spec_token_start->location,
23655 "explicit template specialization cannot have a storage class");
23656 decl = error_mark_node;
23659 if (decl && VAR_P (decl))
23660 check_template_variable (decl);
23663 /* Look for a trailing `;' after the declaration. */
23664 if (!function_definition_p
23665 && (decl == error_mark_node
23666 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23667 cp_parser_skip_to_end_of_block_or_statement (parser);
23669 out:
23670 pop_deferring_access_checks ();
23672 /* Clear any current qualification; whatever comes next is the start
23673 of something new. */
23674 parser->scope = NULL_TREE;
23675 parser->qualifying_scope = NULL_TREE;
23676 parser->object_scope = NULL_TREE;
23678 return decl;
23681 /* Parse a cast-expression that is not the operand of a unary "&". */
23683 static tree
23684 cp_parser_simple_cast_expression (cp_parser *parser)
23686 return cp_parser_cast_expression (parser, /*address_p=*/false,
23687 /*cast_p=*/false, /*decltype*/false, NULL);
23690 /* Parse a functional cast to TYPE. Returns an expression
23691 representing the cast. */
23693 static tree
23694 cp_parser_functional_cast (cp_parser* parser, tree type)
23696 vec<tree, va_gc> *vec;
23697 tree expression_list;
23698 tree cast;
23699 bool nonconst_p;
23701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23703 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23704 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23705 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23706 if (TREE_CODE (type) == TYPE_DECL)
23707 type = TREE_TYPE (type);
23708 return finish_compound_literal (type, expression_list,
23709 tf_warning_or_error);
23713 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23714 /*cast_p=*/true,
23715 /*allow_expansion_p=*/true,
23716 /*non_constant_p=*/NULL);
23717 if (vec == NULL)
23718 expression_list = error_mark_node;
23719 else
23721 expression_list = build_tree_list_vec (vec);
23722 release_tree_vector (vec);
23725 cast = build_functional_cast (type, expression_list,
23726 tf_warning_or_error);
23727 /* [expr.const]/1: In an integral constant expression "only type
23728 conversions to integral or enumeration type can be used". */
23729 if (TREE_CODE (type) == TYPE_DECL)
23730 type = TREE_TYPE (type);
23731 if (cast != error_mark_node
23732 && !cast_valid_in_integral_constant_expression_p (type)
23733 && cp_parser_non_integral_constant_expression (parser,
23734 NIC_CONSTRUCTOR))
23735 return error_mark_node;
23736 return cast;
23739 /* Save the tokens that make up the body of a member function defined
23740 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23741 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23742 specifiers applied to the declaration. Returns the FUNCTION_DECL
23743 for the member function. */
23745 static tree
23746 cp_parser_save_member_function_body (cp_parser* parser,
23747 cp_decl_specifier_seq *decl_specifiers,
23748 cp_declarator *declarator,
23749 tree attributes)
23751 cp_token *first;
23752 cp_token *last;
23753 tree fn;
23755 /* Create the FUNCTION_DECL. */
23756 fn = grokmethod (decl_specifiers, declarator, attributes);
23757 cp_finalize_omp_declare_simd (parser, fn);
23758 /* If something went badly wrong, bail out now. */
23759 if (fn == error_mark_node)
23761 /* If there's a function-body, skip it. */
23762 if (cp_parser_token_starts_function_definition_p
23763 (cp_lexer_peek_token (parser->lexer)))
23764 cp_parser_skip_to_end_of_block_or_statement (parser);
23765 return error_mark_node;
23768 /* Remember it, if there default args to post process. */
23769 cp_parser_save_default_args (parser, fn);
23771 /* Save away the tokens that make up the body of the
23772 function. */
23773 first = parser->lexer->next_token;
23774 /* Handle function try blocks. */
23775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23776 cp_lexer_consume_token (parser->lexer);
23777 /* We can have braced-init-list mem-initializers before the fn body. */
23778 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23780 cp_lexer_consume_token (parser->lexer);
23781 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23783 /* cache_group will stop after an un-nested { } pair, too. */
23784 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23785 break;
23787 /* variadic mem-inits have ... after the ')'. */
23788 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23789 cp_lexer_consume_token (parser->lexer);
23792 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23793 /* Handle function try blocks. */
23794 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23795 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23796 last = parser->lexer->next_token;
23798 /* Save away the inline definition; we will process it when the
23799 class is complete. */
23800 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23801 DECL_PENDING_INLINE_P (fn) = 1;
23803 /* We need to know that this was defined in the class, so that
23804 friend templates are handled correctly. */
23805 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23807 /* Add FN to the queue of functions to be parsed later. */
23808 vec_safe_push (unparsed_funs_with_definitions, fn);
23810 return fn;
23813 /* Save the tokens that make up the in-class initializer for a non-static
23814 data member. Returns a DEFAULT_ARG. */
23816 static tree
23817 cp_parser_save_nsdmi (cp_parser* parser)
23819 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23822 /* Parse a template-argument-list, as well as the trailing ">" (but
23823 not the opening "<"). See cp_parser_template_argument_list for the
23824 return value. */
23826 static tree
23827 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23829 tree arguments;
23830 tree saved_scope;
23831 tree saved_qualifying_scope;
23832 tree saved_object_scope;
23833 bool saved_greater_than_is_operator_p;
23834 int saved_unevaluated_operand;
23835 int saved_inhibit_evaluation_warnings;
23837 /* [temp.names]
23839 When parsing a template-id, the first non-nested `>' is taken as
23840 the end of the template-argument-list rather than a greater-than
23841 operator. */
23842 saved_greater_than_is_operator_p
23843 = parser->greater_than_is_operator_p;
23844 parser->greater_than_is_operator_p = false;
23845 /* Parsing the argument list may modify SCOPE, so we save it
23846 here. */
23847 saved_scope = parser->scope;
23848 saved_qualifying_scope = parser->qualifying_scope;
23849 saved_object_scope = parser->object_scope;
23850 /* We need to evaluate the template arguments, even though this
23851 template-id may be nested within a "sizeof". */
23852 saved_unevaluated_operand = cp_unevaluated_operand;
23853 cp_unevaluated_operand = 0;
23854 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23855 c_inhibit_evaluation_warnings = 0;
23856 /* Parse the template-argument-list itself. */
23857 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23858 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23859 arguments = NULL_TREE;
23860 else
23861 arguments = cp_parser_template_argument_list (parser);
23862 /* Look for the `>' that ends the template-argument-list. If we find
23863 a '>>' instead, it's probably just a typo. */
23864 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23866 if (cxx_dialect != cxx98)
23868 /* In C++0x, a `>>' in a template argument list or cast
23869 expression is considered to be two separate `>'
23870 tokens. So, change the current token to a `>', but don't
23871 consume it: it will be consumed later when the outer
23872 template argument list (or cast expression) is parsed.
23873 Note that this replacement of `>' for `>>' is necessary
23874 even if we are parsing tentatively: in the tentative
23875 case, after calling
23876 cp_parser_enclosed_template_argument_list we will always
23877 throw away all of the template arguments and the first
23878 closing `>', either because the template argument list
23879 was erroneous or because we are replacing those tokens
23880 with a CPP_TEMPLATE_ID token. The second `>' (which will
23881 not have been thrown away) is needed either to close an
23882 outer template argument list or to complete a new-style
23883 cast. */
23884 cp_token *token = cp_lexer_peek_token (parser->lexer);
23885 token->type = CPP_GREATER;
23887 else if (!saved_greater_than_is_operator_p)
23889 /* If we're in a nested template argument list, the '>>' has
23890 to be a typo for '> >'. We emit the error message, but we
23891 continue parsing and we push a '>' as next token, so that
23892 the argument list will be parsed correctly. Note that the
23893 global source location is still on the token before the
23894 '>>', so we need to say explicitly where we want it. */
23895 cp_token *token = cp_lexer_peek_token (parser->lexer);
23896 error_at (token->location, "%<>>%> should be %<> >%> "
23897 "within a nested template argument list");
23899 token->type = CPP_GREATER;
23901 else
23903 /* If this is not a nested template argument list, the '>>'
23904 is a typo for '>'. Emit an error message and continue.
23905 Same deal about the token location, but here we can get it
23906 right by consuming the '>>' before issuing the diagnostic. */
23907 cp_token *token = cp_lexer_consume_token (parser->lexer);
23908 error_at (token->location,
23909 "spurious %<>>%>, use %<>%> to terminate "
23910 "a template argument list");
23913 else
23914 cp_parser_skip_to_end_of_template_parameter_list (parser);
23915 /* The `>' token might be a greater-than operator again now. */
23916 parser->greater_than_is_operator_p
23917 = saved_greater_than_is_operator_p;
23918 /* Restore the SAVED_SCOPE. */
23919 parser->scope = saved_scope;
23920 parser->qualifying_scope = saved_qualifying_scope;
23921 parser->object_scope = saved_object_scope;
23922 cp_unevaluated_operand = saved_unevaluated_operand;
23923 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23925 return arguments;
23928 /* MEMBER_FUNCTION is a member function, or a friend. If default
23929 arguments, or the body of the function have not yet been parsed,
23930 parse them now. */
23932 static void
23933 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23935 timevar_push (TV_PARSE_INMETH);
23936 /* If this member is a template, get the underlying
23937 FUNCTION_DECL. */
23938 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23939 member_function = DECL_TEMPLATE_RESULT (member_function);
23941 /* There should not be any class definitions in progress at this
23942 point; the bodies of members are only parsed outside of all class
23943 definitions. */
23944 gcc_assert (parser->num_classes_being_defined == 0);
23945 /* While we're parsing the member functions we might encounter more
23946 classes. We want to handle them right away, but we don't want
23947 them getting mixed up with functions that are currently in the
23948 queue. */
23949 push_unparsed_function_queues (parser);
23951 /* Make sure that any template parameters are in scope. */
23952 maybe_begin_member_template_processing (member_function);
23954 // Restore the declaration's requirements for the parsing of
23955 // the definition.
23957 tree saved_template_reqs = release (current_template_reqs);
23958 current_template_reqs = get_constraints (member_function);
23960 /* If the body of the function has not yet been parsed, parse it
23961 now. */
23962 if (DECL_PENDING_INLINE_P (member_function))
23964 tree function_scope;
23965 cp_token_cache *tokens;
23967 /* The function is no longer pending; we are processing it. */
23968 tokens = DECL_PENDING_INLINE_INFO (member_function);
23969 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23970 DECL_PENDING_INLINE_P (member_function) = 0;
23972 /* If this is a local class, enter the scope of the containing
23973 function. */
23974 function_scope = current_function_decl;
23975 if (function_scope)
23976 push_function_context ();
23978 /* Push the body of the function onto the lexer stack. */
23979 cp_parser_push_lexer_for_tokens (parser, tokens);
23981 /* Let the front end know that we going to be defining this
23982 function. */
23983 start_preparsed_function (member_function, NULL_TREE,
23984 SF_PRE_PARSED | SF_INCLASS_INLINE);
23986 /* Don't do access checking if it is a templated function. */
23987 if (processing_template_decl)
23988 push_deferring_access_checks (dk_no_check);
23990 /* #pragma omp declare reduction needs special parsing. */
23991 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23993 parser->lexer->in_pragma = true;
23994 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23995 finish_function (0);
23996 cp_check_omp_declare_reduction (member_function);
23998 else
23999 /* Now, parse the body of the function. */
24000 cp_parser_function_definition_after_declarator (parser,
24001 /*inline_p=*/true);
24003 if (processing_template_decl)
24004 pop_deferring_access_checks ();
24006 /* Leave the scope of the containing function. */
24007 if (function_scope)
24008 pop_function_context ();
24009 cp_parser_pop_lexer (parser);
24012 /* Remove any template parameters from the symbol table. */
24013 maybe_end_member_template_processing ();
24015 // Restore the template requirements.
24016 current_template_reqs = saved_template_reqs;
24018 /* Restore the queue. */
24019 pop_unparsed_function_queues (parser);
24020 timevar_pop (TV_PARSE_INMETH);
24023 /* If DECL contains any default args, remember it on the unparsed
24024 functions queue. */
24026 static void
24027 cp_parser_save_default_args (cp_parser* parser, tree decl)
24029 tree probe;
24031 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24032 probe;
24033 probe = TREE_CHAIN (probe))
24034 if (TREE_PURPOSE (probe))
24036 cp_default_arg_entry entry = {current_class_type, decl};
24037 vec_safe_push (unparsed_funs_with_default_args, entry);
24038 break;
24042 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24043 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24044 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24045 from the parameter-type-list. */
24047 static tree
24048 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24049 tree default_arg, tree parmtype)
24051 cp_token_cache *tokens;
24052 tree parsed_arg;
24053 bool dummy;
24055 if (default_arg == error_mark_node)
24056 return error_mark_node;
24058 /* Push the saved tokens for the default argument onto the parser's
24059 lexer stack. */
24060 tokens = DEFARG_TOKENS (default_arg);
24061 cp_parser_push_lexer_for_tokens (parser, tokens);
24063 start_lambda_scope (decl);
24065 /* Parse the default argument. */
24066 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24067 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24068 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24070 finish_lambda_scope ();
24072 if (parsed_arg == error_mark_node)
24073 cp_parser_skip_to_end_of_statement (parser);
24075 if (!processing_template_decl)
24077 /* In a non-template class, check conversions now. In a template,
24078 we'll wait and instantiate these as needed. */
24079 if (TREE_CODE (decl) == PARM_DECL)
24080 parsed_arg = check_default_argument (parmtype, parsed_arg,
24081 tf_warning_or_error);
24082 else
24084 int flags = LOOKUP_IMPLICIT;
24085 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
24086 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
24087 flags = LOOKUP_NORMAL;
24088 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
24089 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
24090 /* This represents the whole initialization. */
24091 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
24095 /* If the token stream has not been completely used up, then
24096 there was extra junk after the end of the default
24097 argument. */
24098 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24100 if (TREE_CODE (decl) == PARM_DECL)
24101 cp_parser_error (parser, "expected %<,%>");
24102 else
24103 cp_parser_error (parser, "expected %<;%>");
24106 /* Revert to the main lexer. */
24107 cp_parser_pop_lexer (parser);
24109 return parsed_arg;
24112 /* FIELD is a non-static data member with an initializer which we saved for
24113 later; parse it now. */
24115 static void
24116 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24118 tree def;
24120 maybe_begin_member_template_processing (field);
24122 push_unparsed_function_queues (parser);
24123 def = cp_parser_late_parse_one_default_arg (parser, field,
24124 DECL_INITIAL (field),
24125 NULL_TREE);
24126 pop_unparsed_function_queues (parser);
24128 maybe_end_member_template_processing ();
24130 DECL_INITIAL (field) = def;
24133 /* FN is a FUNCTION_DECL which may contains a parameter with an
24134 unparsed DEFAULT_ARG. Parse the default args now. This function
24135 assumes that the current scope is the scope in which the default
24136 argument should be processed. */
24138 static void
24139 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24141 bool saved_local_variables_forbidden_p;
24142 tree parm, parmdecl;
24144 /* While we're parsing the default args, we might (due to the
24145 statement expression extension) encounter more classes. We want
24146 to handle them right away, but we don't want them getting mixed
24147 up with default args that are currently in the queue. */
24148 push_unparsed_function_queues (parser);
24150 /* Local variable names (and the `this' keyword) may not appear
24151 in a default argument. */
24152 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24153 parser->local_variables_forbidden_p = true;
24155 push_defarg_context (fn);
24157 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24158 parmdecl = DECL_ARGUMENTS (fn);
24159 parm && parm != void_list_node;
24160 parm = TREE_CHAIN (parm),
24161 parmdecl = DECL_CHAIN (parmdecl))
24163 tree default_arg = TREE_PURPOSE (parm);
24164 tree parsed_arg;
24165 vec<tree, va_gc> *insts;
24166 tree copy;
24167 unsigned ix;
24169 if (!default_arg)
24170 continue;
24172 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24173 /* This can happen for a friend declaration for a function
24174 already declared with default arguments. */
24175 continue;
24177 parsed_arg
24178 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24179 default_arg,
24180 TREE_VALUE (parm));
24181 if (parsed_arg == error_mark_node)
24183 continue;
24186 TREE_PURPOSE (parm) = parsed_arg;
24188 /* Update any instantiations we've already created. */
24189 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24190 vec_safe_iterate (insts, ix, &copy); ix++)
24191 TREE_PURPOSE (copy) = parsed_arg;
24194 pop_defarg_context ();
24196 /* Make sure no default arg is missing. */
24197 check_default_args (fn);
24199 /* Restore the state of local_variables_forbidden_p. */
24200 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24202 /* Restore the queue. */
24203 pop_unparsed_function_queues (parser);
24206 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24208 sizeof ... ( identifier )
24210 where the 'sizeof' token has already been consumed. */
24212 static tree
24213 cp_parser_sizeof_pack (cp_parser *parser)
24215 /* Consume the `...'. */
24216 cp_lexer_consume_token (parser->lexer);
24217 maybe_warn_variadic_templates ();
24219 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24220 if (paren)
24221 cp_lexer_consume_token (parser->lexer);
24222 else
24223 permerror (cp_lexer_peek_token (parser->lexer)->location,
24224 "%<sizeof...%> argument must be surrounded by parentheses");
24226 cp_token *token = cp_lexer_peek_token (parser->lexer);
24227 tree name = cp_parser_identifier (parser);
24228 if (name == error_mark_node)
24229 return error_mark_node;
24230 /* The name is not qualified. */
24231 parser->scope = NULL_TREE;
24232 parser->qualifying_scope = NULL_TREE;
24233 parser->object_scope = NULL_TREE;
24234 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24235 if (expr == error_mark_node)
24236 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24237 token->location);
24238 if (TREE_CODE (expr) == TYPE_DECL)
24239 expr = TREE_TYPE (expr);
24240 else if (TREE_CODE (expr) == CONST_DECL)
24241 expr = DECL_INITIAL (expr);
24242 expr = make_pack_expansion (expr);
24244 if (paren)
24245 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24247 return expr;
24250 /* Parse the operand of `sizeof' (or a similar operator). Returns
24251 either a TYPE or an expression, depending on the form of the
24252 input. The KEYWORD indicates which kind of expression we have
24253 encountered. */
24255 static tree
24256 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24258 tree expr = NULL_TREE;
24259 const char *saved_message;
24260 char *tmp;
24261 bool saved_integral_constant_expression_p;
24262 bool saved_non_integral_constant_expression_p;
24264 /* If it's a `...', then we are computing the length of a parameter
24265 pack. */
24266 if (keyword == RID_SIZEOF
24267 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24268 return cp_parser_sizeof_pack (parser);
24270 /* Types cannot be defined in a `sizeof' expression. Save away the
24271 old message. */
24272 saved_message = parser->type_definition_forbidden_message;
24273 /* And create the new one. */
24274 tmp = concat ("types may not be defined in %<",
24275 IDENTIFIER_POINTER (ridpointers[keyword]),
24276 "%> expressions", NULL);
24277 parser->type_definition_forbidden_message = tmp;
24279 /* The restrictions on constant-expressions do not apply inside
24280 sizeof expressions. */
24281 saved_integral_constant_expression_p
24282 = parser->integral_constant_expression_p;
24283 saved_non_integral_constant_expression_p
24284 = parser->non_integral_constant_expression_p;
24285 parser->integral_constant_expression_p = false;
24287 /* Do not actually evaluate the expression. */
24288 ++cp_unevaluated_operand;
24289 ++c_inhibit_evaluation_warnings;
24290 /* If it's a `(', then we might be looking at the type-id
24291 construction. */
24292 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24294 tree type = NULL_TREE;
24295 bool compound_literal_p;
24297 /* We can't be sure yet whether we're looking at a type-id or an
24298 expression. */
24299 cp_parser_parse_tentatively (parser);
24300 /* Consume the `('. */
24301 cp_lexer_consume_token (parser->lexer);
24302 /* Note: as a GNU Extension, compound literals are considered
24303 postfix-expressions as they are in C99, so they are valid
24304 arguments to sizeof. See comment in cp_parser_cast_expression
24305 for details. */
24306 cp_lexer_save_tokens (parser->lexer);
24307 /* Skip tokens until the next token is a closing parenthesis.
24308 If we find the closing `)', and the next token is a `{', then
24309 we are looking at a compound-literal. */
24310 compound_literal_p
24311 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
24312 /*consume_paren=*/true)
24313 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
24314 /* Roll back the tokens we skipped. */
24315 cp_lexer_rollback_tokens (parser->lexer);
24316 /* If we were looking at a compound-literal, simulate an error
24317 so that the call to cp_parser_parse_definitely below will
24318 fail. */
24319 if (compound_literal_p)
24320 cp_parser_simulate_error (parser);
24321 else
24323 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24324 parser->in_type_id_in_expr_p = true;
24325 /* Look for the type-id. */
24326 type = cp_parser_type_id (parser);
24327 /* Look for the closing `)'. */
24328 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24329 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24332 /* If all went well, then we're done. */
24333 if (cp_parser_parse_definitely (parser))
24335 cp_decl_specifier_seq decl_specs;
24337 /* Build a trivial decl-specifier-seq. */
24338 clear_decl_specs (&decl_specs);
24339 decl_specs.type = type;
24341 /* Call grokdeclarator to figure out what type this is. */
24342 expr = grokdeclarator (NULL,
24343 &decl_specs,
24344 TYPENAME,
24345 /*initialized=*/0,
24346 /*attrlist=*/NULL);
24350 /* If the type-id production did not work out, then we must be
24351 looking at the unary-expression production. */
24352 if (!expr)
24353 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24354 /*cast_p=*/false, NULL);
24356 /* Go back to evaluating expressions. */
24357 --cp_unevaluated_operand;
24358 --c_inhibit_evaluation_warnings;
24360 /* Free the message we created. */
24361 free (tmp);
24362 /* And restore the old one. */
24363 parser->type_definition_forbidden_message = saved_message;
24364 parser->integral_constant_expression_p
24365 = saved_integral_constant_expression_p;
24366 parser->non_integral_constant_expression_p
24367 = saved_non_integral_constant_expression_p;
24369 return expr;
24372 /* If the current declaration has no declarator, return true. */
24374 static bool
24375 cp_parser_declares_only_class_p (cp_parser *parser)
24377 /* If the next token is a `;' or a `,' then there is no
24378 declarator. */
24379 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24380 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24383 /* Update the DECL_SPECS to reflect the storage class indicated by
24384 KEYWORD. */
24386 static void
24387 cp_parser_set_storage_class (cp_parser *parser,
24388 cp_decl_specifier_seq *decl_specs,
24389 enum rid keyword,
24390 cp_token *token)
24392 cp_storage_class storage_class;
24394 if (parser->in_unbraced_linkage_specification_p)
24396 error_at (token->location, "invalid use of %qD in linkage specification",
24397 ridpointers[keyword]);
24398 return;
24400 else if (decl_specs->storage_class != sc_none)
24402 decl_specs->conflicting_specifiers_p = true;
24403 return;
24406 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24407 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24408 && decl_specs->gnu_thread_keyword_p)
24410 pedwarn (decl_specs->locations[ds_thread], 0,
24411 "%<__thread%> before %qD", ridpointers[keyword]);
24414 switch (keyword)
24416 case RID_AUTO:
24417 storage_class = sc_auto;
24418 break;
24419 case RID_REGISTER:
24420 storage_class = sc_register;
24421 break;
24422 case RID_STATIC:
24423 storage_class = sc_static;
24424 break;
24425 case RID_EXTERN:
24426 storage_class = sc_extern;
24427 break;
24428 case RID_MUTABLE:
24429 storage_class = sc_mutable;
24430 break;
24431 default:
24432 gcc_unreachable ();
24434 decl_specs->storage_class = storage_class;
24435 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24437 /* A storage class specifier cannot be applied alongside a typedef
24438 specifier. If there is a typedef specifier present then set
24439 conflicting_specifiers_p which will trigger an error later
24440 on in grokdeclarator. */
24441 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24442 decl_specs->conflicting_specifiers_p = true;
24445 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24446 is true, the type is a class or enum definition. */
24448 static void
24449 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24450 tree type_spec,
24451 cp_token *token,
24452 bool type_definition_p)
24454 decl_specs->any_specifiers_p = true;
24456 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24457 (with, for example, in "typedef int wchar_t;") we remember that
24458 this is what happened. In system headers, we ignore these
24459 declarations so that G++ can work with system headers that are not
24460 C++-safe. */
24461 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24462 && !type_definition_p
24463 && (type_spec == boolean_type_node
24464 || type_spec == char16_type_node
24465 || type_spec == char32_type_node
24466 || type_spec == wchar_type_node)
24467 && (decl_specs->type
24468 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24469 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24470 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24471 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24473 decl_specs->redefined_builtin_type = type_spec;
24474 set_and_check_decl_spec_loc (decl_specs,
24475 ds_redefined_builtin_type_spec,
24476 token);
24477 if (!decl_specs->type)
24479 decl_specs->type = type_spec;
24480 decl_specs->type_definition_p = false;
24481 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24484 else if (decl_specs->type)
24485 decl_specs->multiple_types_p = true;
24486 else
24488 decl_specs->type = type_spec;
24489 decl_specs->type_definition_p = type_definition_p;
24490 decl_specs->redefined_builtin_type = NULL_TREE;
24491 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24495 /* True iff TOKEN is the GNU keyword __thread. */
24497 static bool
24498 token_is__thread (cp_token *token)
24500 gcc_assert (token->keyword == RID_THREAD);
24501 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24504 /* Set the location for a declarator specifier and check if it is
24505 duplicated.
24507 DECL_SPECS is the sequence of declarator specifiers onto which to
24508 set the location.
24510 DS is the single declarator specifier to set which location is to
24511 be set onto the existing sequence of declarators.
24513 LOCATION is the location for the declarator specifier to
24514 consider. */
24516 static void
24517 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24518 cp_decl_spec ds, cp_token *token)
24520 gcc_assert (ds < ds_last);
24522 if (decl_specs == NULL)
24523 return;
24525 source_location location = token->location;
24527 if (decl_specs->locations[ds] == 0)
24529 decl_specs->locations[ds] = location;
24530 if (ds == ds_thread)
24531 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24533 else
24535 if (ds == ds_long)
24537 if (decl_specs->locations[ds_long_long] != 0)
24538 error_at (location,
24539 "%<long long long%> is too long for GCC");
24540 else
24542 decl_specs->locations[ds_long_long] = location;
24543 pedwarn_cxx98 (location,
24544 OPT_Wlong_long,
24545 "ISO C++ 1998 does not support %<long long%>");
24548 else if (ds == ds_thread)
24550 bool gnu = token_is__thread (token);
24551 if (gnu != decl_specs->gnu_thread_keyword_p)
24552 error_at (location,
24553 "both %<__thread%> and %<thread_local%> specified");
24554 else
24555 error_at (location, "duplicate %qD", token->u.value);
24557 else
24559 static const char *const decl_spec_names[] = {
24560 "signed",
24561 "unsigned",
24562 "short",
24563 "long",
24564 "const",
24565 "volatile",
24566 "restrict",
24567 "inline",
24568 "virtual",
24569 "explicit",
24570 "friend",
24571 "typedef",
24572 "using",
24573 "constexpr",
24574 "__complex"
24576 error_at (location,
24577 "duplicate %qs", decl_spec_names[ds]);
24582 /* Return true iff the declarator specifier DS is present in the
24583 sequence of declarator specifiers DECL_SPECS. */
24585 bool
24586 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24587 cp_decl_spec ds)
24589 gcc_assert (ds < ds_last);
24591 if (decl_specs == NULL)
24592 return false;
24594 return decl_specs->locations[ds] != 0;
24597 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24598 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24600 static bool
24601 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24603 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24606 /* Issue an error message indicating that TOKEN_DESC was expected.
24607 If KEYWORD is true, it indicated this function is called by
24608 cp_parser_require_keword and the required token can only be
24609 a indicated keyword. */
24611 static void
24612 cp_parser_required_error (cp_parser *parser,
24613 required_token token_desc,
24614 bool keyword)
24616 switch (token_desc)
24618 case RT_NEW:
24619 cp_parser_error (parser, "expected %<new%>");
24620 return;
24621 case RT_DELETE:
24622 cp_parser_error (parser, "expected %<delete%>");
24623 return;
24624 case RT_RETURN:
24625 cp_parser_error (parser, "expected %<return%>");
24626 return;
24627 case RT_WHILE:
24628 cp_parser_error (parser, "expected %<while%>");
24629 return;
24630 case RT_EXTERN:
24631 cp_parser_error (parser, "expected %<extern%>");
24632 return;
24633 case RT_STATIC_ASSERT:
24634 cp_parser_error (parser, "expected %<static_assert%>");
24635 return;
24636 case RT_DECLTYPE:
24637 cp_parser_error (parser, "expected %<decltype%>");
24638 return;
24639 case RT_OPERATOR:
24640 cp_parser_error (parser, "expected %<operator%>");
24641 return;
24642 case RT_CLASS:
24643 cp_parser_error (parser, "expected %<class%>");
24644 return;
24645 case RT_TEMPLATE:
24646 cp_parser_error (parser, "expected %<template%>");
24647 return;
24648 case RT_NAMESPACE:
24649 cp_parser_error (parser, "expected %<namespace%>");
24650 return;
24651 case RT_USING:
24652 cp_parser_error (parser, "expected %<using%>");
24653 return;
24654 case RT_ASM:
24655 cp_parser_error (parser, "expected %<asm%>");
24656 return;
24657 case RT_TRY:
24658 cp_parser_error (parser, "expected %<try%>");
24659 return;
24660 case RT_CATCH:
24661 cp_parser_error (parser, "expected %<catch%>");
24662 return;
24663 case RT_THROW:
24664 cp_parser_error (parser, "expected %<throw%>");
24665 return;
24666 case RT_LABEL:
24667 cp_parser_error (parser, "expected %<__label__%>");
24668 return;
24669 case RT_AT_TRY:
24670 cp_parser_error (parser, "expected %<@try%>");
24671 return;
24672 case RT_AT_SYNCHRONIZED:
24673 cp_parser_error (parser, "expected %<@synchronized%>");
24674 return;
24675 case RT_AT_THROW:
24676 cp_parser_error (parser, "expected %<@throw%>");
24677 return;
24678 case RT_TRANSACTION_ATOMIC:
24679 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24680 return;
24681 case RT_TRANSACTION_RELAXED:
24682 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24683 return;
24684 default:
24685 break;
24687 if (!keyword)
24689 switch (token_desc)
24691 case RT_SEMICOLON:
24692 cp_parser_error (parser, "expected %<;%>");
24693 return;
24694 case RT_OPEN_PAREN:
24695 cp_parser_error (parser, "expected %<(%>");
24696 return;
24697 case RT_CLOSE_BRACE:
24698 cp_parser_error (parser, "expected %<}%>");
24699 return;
24700 case RT_OPEN_BRACE:
24701 cp_parser_error (parser, "expected %<{%>");
24702 return;
24703 case RT_CLOSE_SQUARE:
24704 cp_parser_error (parser, "expected %<]%>");
24705 return;
24706 case RT_OPEN_SQUARE:
24707 cp_parser_error (parser, "expected %<[%>");
24708 return;
24709 case RT_COMMA:
24710 cp_parser_error (parser, "expected %<,%>");
24711 return;
24712 case RT_SCOPE:
24713 cp_parser_error (parser, "expected %<::%>");
24714 return;
24715 case RT_LESS:
24716 cp_parser_error (parser, "expected %<<%>");
24717 return;
24718 case RT_GREATER:
24719 cp_parser_error (parser, "expected %<>%>");
24720 return;
24721 case RT_EQ:
24722 cp_parser_error (parser, "expected %<=%>");
24723 return;
24724 case RT_ELLIPSIS:
24725 cp_parser_error (parser, "expected %<...%>");
24726 return;
24727 case RT_MULT:
24728 cp_parser_error (parser, "expected %<*%>");
24729 return;
24730 case RT_COMPL:
24731 cp_parser_error (parser, "expected %<~%>");
24732 return;
24733 case RT_COLON:
24734 cp_parser_error (parser, "expected %<:%>");
24735 return;
24736 case RT_COLON_SCOPE:
24737 cp_parser_error (parser, "expected %<:%> or %<::%>");
24738 return;
24739 case RT_CLOSE_PAREN:
24740 cp_parser_error (parser, "expected %<)%>");
24741 return;
24742 case RT_COMMA_CLOSE_PAREN:
24743 cp_parser_error (parser, "expected %<,%> or %<)%>");
24744 return;
24745 case RT_PRAGMA_EOL:
24746 cp_parser_error (parser, "expected end of line");
24747 return;
24748 case RT_NAME:
24749 cp_parser_error (parser, "expected identifier");
24750 return;
24751 case RT_SELECT:
24752 cp_parser_error (parser, "expected selection-statement");
24753 return;
24754 case RT_INTERATION:
24755 cp_parser_error (parser, "expected iteration-statement");
24756 return;
24757 case RT_JUMP:
24758 cp_parser_error (parser, "expected jump-statement");
24759 return;
24760 case RT_CLASS_KEY:
24761 cp_parser_error (parser, "expected class-key");
24762 return;
24763 case RT_CLASS_TYPENAME_TEMPLATE:
24764 cp_parser_error (parser,
24765 "expected %<class%>, %<typename%>, or %<template%>");
24766 return;
24767 default:
24768 gcc_unreachable ();
24771 else
24772 gcc_unreachable ();
24777 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24778 issue an error message indicating that TOKEN_DESC was expected.
24780 Returns the token consumed, if the token had the appropriate type.
24781 Otherwise, returns NULL. */
24783 static cp_token *
24784 cp_parser_require (cp_parser* parser,
24785 enum cpp_ttype type,
24786 required_token token_desc)
24788 if (cp_lexer_next_token_is (parser->lexer, type))
24789 return cp_lexer_consume_token (parser->lexer);
24790 else
24792 /* Output the MESSAGE -- unless we're parsing tentatively. */
24793 if (!cp_parser_simulate_error (parser))
24794 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24795 return NULL;
24799 /* An error message is produced if the next token is not '>'.
24800 All further tokens are skipped until the desired token is
24801 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24803 static void
24804 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24806 /* Current level of '< ... >'. */
24807 unsigned level = 0;
24808 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24809 unsigned nesting_depth = 0;
24811 /* Are we ready, yet? If not, issue error message. */
24812 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24813 return;
24815 /* Skip tokens until the desired token is found. */
24816 while (true)
24818 /* Peek at the next token. */
24819 switch (cp_lexer_peek_token (parser->lexer)->type)
24821 case CPP_LESS:
24822 if (!nesting_depth)
24823 ++level;
24824 break;
24826 case CPP_RSHIFT:
24827 if (cxx_dialect == cxx98)
24828 /* C++0x views the `>>' operator as two `>' tokens, but
24829 C++98 does not. */
24830 break;
24831 else if (!nesting_depth && level-- == 0)
24833 /* We've hit a `>>' where the first `>' closes the
24834 template argument list, and the second `>' is
24835 spurious. Just consume the `>>' and stop; we've
24836 already produced at least one error. */
24837 cp_lexer_consume_token (parser->lexer);
24838 return;
24840 /* Fall through for C++0x, so we handle the second `>' in
24841 the `>>'. */
24843 case CPP_GREATER:
24844 if (!nesting_depth && level-- == 0)
24846 /* We've reached the token we want, consume it and stop. */
24847 cp_lexer_consume_token (parser->lexer);
24848 return;
24850 break;
24852 case CPP_OPEN_PAREN:
24853 case CPP_OPEN_SQUARE:
24854 ++nesting_depth;
24855 break;
24857 case CPP_CLOSE_PAREN:
24858 case CPP_CLOSE_SQUARE:
24859 if (nesting_depth-- == 0)
24860 return;
24861 break;
24863 case CPP_EOF:
24864 case CPP_PRAGMA_EOL:
24865 case CPP_SEMICOLON:
24866 case CPP_OPEN_BRACE:
24867 case CPP_CLOSE_BRACE:
24868 /* The '>' was probably forgotten, don't look further. */
24869 return;
24871 default:
24872 break;
24875 /* Consume this token. */
24876 cp_lexer_consume_token (parser->lexer);
24880 /* If the next token is the indicated keyword, consume it. Otherwise,
24881 issue an error message indicating that TOKEN_DESC was expected.
24883 Returns the token consumed, if the token had the appropriate type.
24884 Otherwise, returns NULL. */
24886 static cp_token *
24887 cp_parser_require_keyword (cp_parser* parser,
24888 enum rid keyword,
24889 required_token token_desc)
24891 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24893 if (token && token->keyword != keyword)
24895 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24896 return NULL;
24899 return token;
24902 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24903 function-definition. */
24905 static bool
24906 cp_parser_token_starts_function_definition_p (cp_token* token)
24908 return (/* An ordinary function-body begins with an `{'. */
24909 token->type == CPP_OPEN_BRACE
24910 /* A ctor-initializer begins with a `:'. */
24911 || token->type == CPP_COLON
24912 /* A function-try-block begins with `try'. */
24913 || token->keyword == RID_TRY
24914 /* A function-transaction-block begins with `__transaction_atomic'
24915 or `__transaction_relaxed'. */
24916 || token->keyword == RID_TRANSACTION_ATOMIC
24917 || token->keyword == RID_TRANSACTION_RELAXED
24918 /* The named return value extension begins with `return'. */
24919 || token->keyword == RID_RETURN);
24922 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24923 definition. */
24925 static bool
24926 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24928 cp_token *token;
24930 token = cp_lexer_peek_token (parser->lexer);
24931 return (token->type == CPP_OPEN_BRACE
24932 || (token->type == CPP_COLON
24933 && !parser->colon_doesnt_start_class_def_p));
24936 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24937 C++0x) ending a template-argument. */
24939 static bool
24940 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24942 cp_token *token;
24944 token = cp_lexer_peek_token (parser->lexer);
24945 return (token->type == CPP_COMMA
24946 || token->type == CPP_GREATER
24947 || token->type == CPP_ELLIPSIS
24948 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24951 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24952 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24954 static bool
24955 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24956 size_t n)
24958 cp_token *token;
24960 token = cp_lexer_peek_nth_token (parser->lexer, n);
24961 if (token->type == CPP_LESS)
24962 return true;
24963 /* Check for the sequence `<::' in the original code. It would be lexed as
24964 `[:', where `[' is a digraph, and there is no whitespace before
24965 `:'. */
24966 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24968 cp_token *token2;
24969 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24970 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24971 return true;
24973 return false;
24976 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24977 or none_type otherwise. */
24979 static enum tag_types
24980 cp_parser_token_is_class_key (cp_token* token)
24982 switch (token->keyword)
24984 case RID_CLASS:
24985 return class_type;
24986 case RID_STRUCT:
24987 return record_type;
24988 case RID_UNION:
24989 return union_type;
24991 default:
24992 return none_type;
24996 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24998 static void
24999 cp_parser_check_class_key (enum tag_types class_key, tree type)
25001 if (type == error_mark_node)
25002 return;
25003 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25005 if (permerror (input_location, "%qs tag used in naming %q#T",
25006 class_key == union_type ? "union"
25007 : class_key == record_type ? "struct" : "class",
25008 type))
25009 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25010 "%q#T was previously declared here", type);
25014 /* Issue an error message if DECL is redeclared with different
25015 access than its original declaration [class.access.spec/3].
25016 This applies to nested classes and nested class templates.
25017 [class.mem/1]. */
25019 static void
25020 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25022 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25023 return;
25025 if ((TREE_PRIVATE (decl)
25026 != (current_access_specifier == access_private_node))
25027 || (TREE_PROTECTED (decl)
25028 != (current_access_specifier == access_protected_node)))
25029 error_at (location, "%qD redeclared with different access", decl);
25032 /* Look for the `template' keyword, as a syntactic disambiguator.
25033 Return TRUE iff it is present, in which case it will be
25034 consumed. */
25036 static bool
25037 cp_parser_optional_template_keyword (cp_parser *parser)
25039 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25041 /* In C++98 the `template' keyword can only be used within templates;
25042 outside templates the parser can always figure out what is a
25043 template and what is not. In C++11, per the resolution of DR 468,
25044 `template' is allowed in cases where it is not strictly necessary. */
25045 if (!processing_template_decl
25046 && pedantic && cxx_dialect == cxx98)
25048 cp_token *token = cp_lexer_peek_token (parser->lexer);
25049 pedwarn (token->location, OPT_Wpedantic,
25050 "in C++98 %<template%> (as a disambiguator) is only "
25051 "allowed within templates");
25052 /* If this part of the token stream is rescanned, the same
25053 error message would be generated. So, we purge the token
25054 from the stream. */
25055 cp_lexer_purge_token (parser->lexer);
25056 return false;
25058 else
25060 /* Consume the `template' keyword. */
25061 cp_lexer_consume_token (parser->lexer);
25062 return true;
25065 return false;
25068 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25069 set PARSER->SCOPE, and perform other related actions. */
25071 static void
25072 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25074 int i;
25075 struct tree_check *check_value;
25076 deferred_access_check *chk;
25077 vec<deferred_access_check, va_gc> *checks;
25079 /* Get the stored value. */
25080 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25081 /* Perform any access checks that were deferred. */
25082 checks = check_value->checks;
25083 if (checks)
25085 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25086 perform_or_defer_access_check (chk->binfo,
25087 chk->decl,
25088 chk->diag_decl, tf_warning_or_error);
25090 /* Set the scope from the stored value. */
25091 parser->scope = check_value->value;
25092 parser->qualifying_scope = check_value->qualifying_scope;
25093 parser->object_scope = NULL_TREE;
25096 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25097 encounter the end of a block before what we were looking for. */
25099 static bool
25100 cp_parser_cache_group (cp_parser *parser,
25101 enum cpp_ttype end,
25102 unsigned depth)
25104 while (true)
25106 cp_token *token = cp_lexer_peek_token (parser->lexer);
25108 /* Abort a parenthesized expression if we encounter a semicolon. */
25109 if ((end == CPP_CLOSE_PAREN || depth == 0)
25110 && token->type == CPP_SEMICOLON)
25111 return true;
25112 /* If we've reached the end of the file, stop. */
25113 if (token->type == CPP_EOF
25114 || (end != CPP_PRAGMA_EOL
25115 && token->type == CPP_PRAGMA_EOL))
25116 return true;
25117 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25118 /* We've hit the end of an enclosing block, so there's been some
25119 kind of syntax error. */
25120 return true;
25122 /* Consume the token. */
25123 cp_lexer_consume_token (parser->lexer);
25124 /* See if it starts a new group. */
25125 if (token->type == CPP_OPEN_BRACE)
25127 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25128 /* In theory this should probably check end == '}', but
25129 cp_parser_save_member_function_body needs it to exit
25130 after either '}' or ')' when called with ')'. */
25131 if (depth == 0)
25132 return false;
25134 else if (token->type == CPP_OPEN_PAREN)
25136 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25137 if (depth == 0 && end == CPP_CLOSE_PAREN)
25138 return false;
25140 else if (token->type == CPP_PRAGMA)
25141 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25142 else if (token->type == end)
25143 return false;
25147 /* Like above, for caching a default argument or NSDMI. Both of these are
25148 terminated by a non-nested comma, but it can be unclear whether or not a
25149 comma is nested in a template argument list unless we do more parsing.
25150 In order to handle this ambiguity, when we encounter a ',' after a '<'
25151 we try to parse what follows as a parameter-declaration-list (in the
25152 case of a default argument) or a member-declarator (in the case of an
25153 NSDMI). If that succeeds, then we stop caching. */
25155 static tree
25156 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25158 unsigned depth = 0;
25159 int maybe_template_id = 0;
25160 cp_token *first_token;
25161 cp_token *token;
25162 tree default_argument;
25164 /* Add tokens until we have processed the entire default
25165 argument. We add the range [first_token, token). */
25166 first_token = cp_lexer_peek_token (parser->lexer);
25167 if (first_token->type == CPP_OPEN_BRACE)
25169 /* For list-initialization, this is straightforward. */
25170 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25171 token = cp_lexer_peek_token (parser->lexer);
25173 else while (true)
25175 bool done = false;
25177 /* Peek at the next token. */
25178 token = cp_lexer_peek_token (parser->lexer);
25179 /* What we do depends on what token we have. */
25180 switch (token->type)
25182 /* In valid code, a default argument must be
25183 immediately followed by a `,' `)', or `...'. */
25184 case CPP_COMMA:
25185 if (depth == 0 && maybe_template_id)
25187 /* If we've seen a '<', we might be in a
25188 template-argument-list. Until Core issue 325 is
25189 resolved, we don't know how this situation ought
25190 to be handled, so try to DTRT. We check whether
25191 what comes after the comma is a valid parameter
25192 declaration list. If it is, then the comma ends
25193 the default argument; otherwise the default
25194 argument continues. */
25195 bool error = false;
25197 /* Set ITALP so cp_parser_parameter_declaration_list
25198 doesn't decide to commit to this parse. */
25199 bool saved_italp = parser->in_template_argument_list_p;
25200 parser->in_template_argument_list_p = true;
25202 cp_parser_parse_tentatively (parser);
25203 cp_lexer_consume_token (parser->lexer);
25205 if (nsdmi)
25207 int ctor_dtor_or_conv_p;
25208 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25209 &ctor_dtor_or_conv_p,
25210 /*parenthesized_p=*/NULL,
25211 /*member_p=*/true);
25213 else
25215 begin_scope (sk_function_parms, NULL_TREE);
25216 cp_parser_parameter_declaration_list (parser, &error);
25217 pop_bindings_and_leave_scope ();
25219 if (!cp_parser_error_occurred (parser) && !error)
25220 done = true;
25221 cp_parser_abort_tentative_parse (parser);
25223 parser->in_template_argument_list_p = saved_italp;
25224 break;
25226 case CPP_CLOSE_PAREN:
25227 case CPP_ELLIPSIS:
25228 /* If we run into a non-nested `;', `}', or `]',
25229 then the code is invalid -- but the default
25230 argument is certainly over. */
25231 case CPP_SEMICOLON:
25232 case CPP_CLOSE_BRACE:
25233 case CPP_CLOSE_SQUARE:
25234 if (depth == 0
25235 /* Handle correctly int n = sizeof ... ( p ); */
25236 && !(nsdmi && token->type == CPP_ELLIPSIS))
25237 done = true;
25238 /* Update DEPTH, if necessary. */
25239 else if (token->type == CPP_CLOSE_PAREN
25240 || token->type == CPP_CLOSE_BRACE
25241 || token->type == CPP_CLOSE_SQUARE)
25242 --depth;
25243 break;
25245 case CPP_OPEN_PAREN:
25246 case CPP_OPEN_SQUARE:
25247 case CPP_OPEN_BRACE:
25248 ++depth;
25249 break;
25251 case CPP_LESS:
25252 if (depth == 0)
25253 /* This might be the comparison operator, or it might
25254 start a template argument list. */
25255 ++maybe_template_id;
25256 break;
25258 case CPP_RSHIFT:
25259 if (cxx_dialect == cxx98)
25260 break;
25261 /* Fall through for C++0x, which treats the `>>'
25262 operator like two `>' tokens in certain
25263 cases. */
25265 case CPP_GREATER:
25266 if (depth == 0)
25268 /* This might be an operator, or it might close a
25269 template argument list. But if a previous '<'
25270 started a template argument list, this will have
25271 closed it, so we can't be in one anymore. */
25272 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25273 if (maybe_template_id < 0)
25274 maybe_template_id = 0;
25276 break;
25278 /* If we run out of tokens, issue an error message. */
25279 case CPP_EOF:
25280 case CPP_PRAGMA_EOL:
25281 error_at (token->location, "file ends in default argument");
25282 done = true;
25283 break;
25285 case CPP_NAME:
25286 case CPP_SCOPE:
25287 /* In these cases, we should look for template-ids.
25288 For example, if the default argument is
25289 `X<int, double>()', we need to do name lookup to
25290 figure out whether or not `X' is a template; if
25291 so, the `,' does not end the default argument.
25293 That is not yet done. */
25294 break;
25296 default:
25297 break;
25300 /* If we've reached the end, stop. */
25301 if (done)
25302 break;
25304 /* Add the token to the token block. */
25305 token = cp_lexer_consume_token (parser->lexer);
25308 /* Create a DEFAULT_ARG to represent the unparsed default
25309 argument. */
25310 default_argument = make_node (DEFAULT_ARG);
25311 DEFARG_TOKENS (default_argument)
25312 = cp_token_cache_new (first_token, token);
25313 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25315 return default_argument;
25318 /* Begin parsing tentatively. We always save tokens while parsing
25319 tentatively so that if the tentative parsing fails we can restore the
25320 tokens. */
25322 static void
25323 cp_parser_parse_tentatively (cp_parser* parser)
25325 /* Enter a new parsing context. */
25326 parser->context = cp_parser_context_new (parser->context);
25327 /* Begin saving tokens. */
25328 cp_lexer_save_tokens (parser->lexer);
25329 /* In order to avoid repetitive access control error messages,
25330 access checks are queued up until we are no longer parsing
25331 tentatively. */
25332 push_deferring_access_checks (dk_deferred);
25335 /* Commit to the currently active tentative parse. */
25337 static void
25338 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25340 cp_parser_context *context;
25341 cp_lexer *lexer;
25343 /* Mark all of the levels as committed. */
25344 lexer = parser->lexer;
25345 for (context = parser->context; context->next; context = context->next)
25347 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25348 break;
25349 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25350 while (!cp_lexer_saving_tokens (lexer))
25351 lexer = lexer->next;
25352 cp_lexer_commit_tokens (lexer);
25356 /* Commit to the topmost currently active tentative parse.
25358 Note that this function shouldn't be called when there are
25359 irreversible side-effects while in a tentative state. For
25360 example, we shouldn't create a permanent entry in the symbol
25361 table, or issue an error message that might not apply if the
25362 tentative parse is aborted. */
25364 static void
25365 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25367 cp_parser_context *context = parser->context;
25368 cp_lexer *lexer = parser->lexer;
25370 if (context)
25372 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25373 return;
25374 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25376 while (!cp_lexer_saving_tokens (lexer))
25377 lexer = lexer->next;
25378 cp_lexer_commit_tokens (lexer);
25382 /* Abort the currently active tentative parse. All consumed tokens
25383 will be rolled back, and no diagnostics will be issued. */
25385 static void
25386 cp_parser_abort_tentative_parse (cp_parser* parser)
25388 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25389 || errorcount > 0);
25390 cp_parser_simulate_error (parser);
25391 /* Now, pretend that we want to see if the construct was
25392 successfully parsed. */
25393 cp_parser_parse_definitely (parser);
25396 /* Stop parsing tentatively. If a parse error has occurred, restore the
25397 token stream. Otherwise, commit to the tokens we have consumed.
25398 Returns true if no error occurred; false otherwise. */
25400 static bool
25401 cp_parser_parse_definitely (cp_parser* parser)
25403 bool error_occurred;
25404 cp_parser_context *context;
25406 /* Remember whether or not an error occurred, since we are about to
25407 destroy that information. */
25408 error_occurred = cp_parser_error_occurred (parser);
25409 /* Remove the topmost context from the stack. */
25410 context = parser->context;
25411 parser->context = context->next;
25412 /* If no parse errors occurred, commit to the tentative parse. */
25413 if (!error_occurred)
25415 /* Commit to the tokens read tentatively, unless that was
25416 already done. */
25417 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25418 cp_lexer_commit_tokens (parser->lexer);
25420 pop_to_parent_deferring_access_checks ();
25422 /* Otherwise, if errors occurred, roll back our state so that things
25423 are just as they were before we began the tentative parse. */
25424 else
25426 cp_lexer_rollback_tokens (parser->lexer);
25427 pop_deferring_access_checks ();
25429 /* Add the context to the front of the free list. */
25430 context->next = cp_parser_context_free_list;
25431 cp_parser_context_free_list = context;
25433 return !error_occurred;
25436 /* Returns true if we are parsing tentatively and are not committed to
25437 this tentative parse. */
25439 static bool
25440 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25442 return (cp_parser_parsing_tentatively (parser)
25443 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25446 /* Returns nonzero iff an error has occurred during the most recent
25447 tentative parse. */
25449 static bool
25450 cp_parser_error_occurred (cp_parser* parser)
25452 return (cp_parser_parsing_tentatively (parser)
25453 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25456 /* Returns nonzero if GNU extensions are allowed. */
25458 static bool
25459 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25461 return parser->allow_gnu_extensions_p;
25464 /* Objective-C++ Productions */
25467 /* Parse an Objective-C expression, which feeds into a primary-expression
25468 above.
25470 objc-expression:
25471 objc-message-expression
25472 objc-string-literal
25473 objc-encode-expression
25474 objc-protocol-expression
25475 objc-selector-expression
25477 Returns a tree representation of the expression. */
25479 static tree
25480 cp_parser_objc_expression (cp_parser* parser)
25482 /* Try to figure out what kind of declaration is present. */
25483 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25485 switch (kwd->type)
25487 case CPP_OPEN_SQUARE:
25488 return cp_parser_objc_message_expression (parser);
25490 case CPP_OBJC_STRING:
25491 kwd = cp_lexer_consume_token (parser->lexer);
25492 return objc_build_string_object (kwd->u.value);
25494 case CPP_KEYWORD:
25495 switch (kwd->keyword)
25497 case RID_AT_ENCODE:
25498 return cp_parser_objc_encode_expression (parser);
25500 case RID_AT_PROTOCOL:
25501 return cp_parser_objc_protocol_expression (parser);
25503 case RID_AT_SELECTOR:
25504 return cp_parser_objc_selector_expression (parser);
25506 default:
25507 break;
25509 default:
25510 error_at (kwd->location,
25511 "misplaced %<@%D%> Objective-C++ construct",
25512 kwd->u.value);
25513 cp_parser_skip_to_end_of_block_or_statement (parser);
25516 return error_mark_node;
25519 /* Parse an Objective-C message expression.
25521 objc-message-expression:
25522 [ objc-message-receiver objc-message-args ]
25524 Returns a representation of an Objective-C message. */
25526 static tree
25527 cp_parser_objc_message_expression (cp_parser* parser)
25529 tree receiver, messageargs;
25531 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25532 receiver = cp_parser_objc_message_receiver (parser);
25533 messageargs = cp_parser_objc_message_args (parser);
25534 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25536 return objc_build_message_expr (receiver, messageargs);
25539 /* Parse an objc-message-receiver.
25541 objc-message-receiver:
25542 expression
25543 simple-type-specifier
25545 Returns a representation of the type or expression. */
25547 static tree
25548 cp_parser_objc_message_receiver (cp_parser* parser)
25550 tree rcv;
25552 /* An Objective-C message receiver may be either (1) a type
25553 or (2) an expression. */
25554 cp_parser_parse_tentatively (parser);
25555 rcv = cp_parser_expression (parser, false, NULL);
25557 if (cp_parser_parse_definitely (parser))
25558 return rcv;
25560 rcv = cp_parser_simple_type_specifier (parser,
25561 /*decl_specs=*/NULL,
25562 CP_PARSER_FLAGS_NONE);
25564 return objc_get_class_reference (rcv);
25567 /* Parse the arguments and selectors comprising an Objective-C message.
25569 objc-message-args:
25570 objc-selector
25571 objc-selector-args
25572 objc-selector-args , objc-comma-args
25574 objc-selector-args:
25575 objc-selector [opt] : assignment-expression
25576 objc-selector-args objc-selector [opt] : assignment-expression
25578 objc-comma-args:
25579 assignment-expression
25580 objc-comma-args , assignment-expression
25582 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25583 selector arguments and TREE_VALUE containing a list of comma
25584 arguments. */
25586 static tree
25587 cp_parser_objc_message_args (cp_parser* parser)
25589 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25590 bool maybe_unary_selector_p = true;
25591 cp_token *token = cp_lexer_peek_token (parser->lexer);
25593 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25595 tree selector = NULL_TREE, arg;
25597 if (token->type != CPP_COLON)
25598 selector = cp_parser_objc_selector (parser);
25600 /* Detect if we have a unary selector. */
25601 if (maybe_unary_selector_p
25602 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25603 return build_tree_list (selector, NULL_TREE);
25605 maybe_unary_selector_p = false;
25606 cp_parser_require (parser, CPP_COLON, RT_COLON);
25607 arg = cp_parser_assignment_expression (parser, false, NULL);
25609 sel_args
25610 = chainon (sel_args,
25611 build_tree_list (selector, arg));
25613 token = cp_lexer_peek_token (parser->lexer);
25616 /* Handle non-selector arguments, if any. */
25617 while (token->type == CPP_COMMA)
25619 tree arg;
25621 cp_lexer_consume_token (parser->lexer);
25622 arg = cp_parser_assignment_expression (parser, false, NULL);
25624 addl_args
25625 = chainon (addl_args,
25626 build_tree_list (NULL_TREE, arg));
25628 token = cp_lexer_peek_token (parser->lexer);
25631 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25633 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25634 return build_tree_list (error_mark_node, error_mark_node);
25637 return build_tree_list (sel_args, addl_args);
25640 /* Parse an Objective-C encode expression.
25642 objc-encode-expression:
25643 @encode objc-typename
25645 Returns an encoded representation of the type argument. */
25647 static tree
25648 cp_parser_objc_encode_expression (cp_parser* parser)
25650 tree type;
25651 cp_token *token;
25653 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25654 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25655 token = cp_lexer_peek_token (parser->lexer);
25656 type = complete_type (cp_parser_type_id (parser));
25657 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25659 if (!type)
25661 error_at (token->location,
25662 "%<@encode%> must specify a type as an argument");
25663 return error_mark_node;
25666 /* This happens if we find @encode(T) (where T is a template
25667 typename or something dependent on a template typename) when
25668 parsing a template. In that case, we can't compile it
25669 immediately, but we rather create an AT_ENCODE_EXPR which will
25670 need to be instantiated when the template is used.
25672 if (dependent_type_p (type))
25674 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25675 TREE_READONLY (value) = 1;
25676 return value;
25679 return objc_build_encode_expr (type);
25682 /* Parse an Objective-C @defs expression. */
25684 static tree
25685 cp_parser_objc_defs_expression (cp_parser *parser)
25687 tree name;
25689 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25690 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25691 name = cp_parser_identifier (parser);
25692 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25694 return objc_get_class_ivars (name);
25697 /* Parse an Objective-C protocol expression.
25699 objc-protocol-expression:
25700 @protocol ( identifier )
25702 Returns a representation of the protocol expression. */
25704 static tree
25705 cp_parser_objc_protocol_expression (cp_parser* parser)
25707 tree proto;
25709 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25710 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25711 proto = cp_parser_identifier (parser);
25712 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25714 return objc_build_protocol_expr (proto);
25717 /* Parse an Objective-C selector expression.
25719 objc-selector-expression:
25720 @selector ( objc-method-signature )
25722 objc-method-signature:
25723 objc-selector
25724 objc-selector-seq
25726 objc-selector-seq:
25727 objc-selector :
25728 objc-selector-seq objc-selector :
25730 Returns a representation of the method selector. */
25732 static tree
25733 cp_parser_objc_selector_expression (cp_parser* parser)
25735 tree sel_seq = NULL_TREE;
25736 bool maybe_unary_selector_p = true;
25737 cp_token *token;
25738 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25740 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25741 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25742 token = cp_lexer_peek_token (parser->lexer);
25744 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25745 || token->type == CPP_SCOPE)
25747 tree selector = NULL_TREE;
25749 if (token->type != CPP_COLON
25750 || token->type == CPP_SCOPE)
25751 selector = cp_parser_objc_selector (parser);
25753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25754 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25756 /* Detect if we have a unary selector. */
25757 if (maybe_unary_selector_p)
25759 sel_seq = selector;
25760 goto finish_selector;
25762 else
25764 cp_parser_error (parser, "expected %<:%>");
25767 maybe_unary_selector_p = false;
25768 token = cp_lexer_consume_token (parser->lexer);
25770 if (token->type == CPP_SCOPE)
25772 sel_seq
25773 = chainon (sel_seq,
25774 build_tree_list (selector, NULL_TREE));
25775 sel_seq
25776 = chainon (sel_seq,
25777 build_tree_list (NULL_TREE, NULL_TREE));
25779 else
25780 sel_seq
25781 = chainon (sel_seq,
25782 build_tree_list (selector, NULL_TREE));
25784 token = cp_lexer_peek_token (parser->lexer);
25787 finish_selector:
25788 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25790 return objc_build_selector_expr (loc, sel_seq);
25793 /* Parse a list of identifiers.
25795 objc-identifier-list:
25796 identifier
25797 objc-identifier-list , identifier
25799 Returns a TREE_LIST of identifier nodes. */
25801 static tree
25802 cp_parser_objc_identifier_list (cp_parser* parser)
25804 tree identifier;
25805 tree list;
25806 cp_token *sep;
25808 identifier = cp_parser_identifier (parser);
25809 if (identifier == error_mark_node)
25810 return error_mark_node;
25812 list = build_tree_list (NULL_TREE, identifier);
25813 sep = cp_lexer_peek_token (parser->lexer);
25815 while (sep->type == CPP_COMMA)
25817 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25818 identifier = cp_parser_identifier (parser);
25819 if (identifier == error_mark_node)
25820 return list;
25822 list = chainon (list, build_tree_list (NULL_TREE,
25823 identifier));
25824 sep = cp_lexer_peek_token (parser->lexer);
25827 return list;
25830 /* Parse an Objective-C alias declaration.
25832 objc-alias-declaration:
25833 @compatibility_alias identifier identifier ;
25835 This function registers the alias mapping with the Objective-C front end.
25836 It returns nothing. */
25838 static void
25839 cp_parser_objc_alias_declaration (cp_parser* parser)
25841 tree alias, orig;
25843 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25844 alias = cp_parser_identifier (parser);
25845 orig = cp_parser_identifier (parser);
25846 objc_declare_alias (alias, orig);
25847 cp_parser_consume_semicolon_at_end_of_statement (parser);
25850 /* Parse an Objective-C class forward-declaration.
25852 objc-class-declaration:
25853 @class objc-identifier-list ;
25855 The function registers the forward declarations with the Objective-C
25856 front end. It returns nothing. */
25858 static void
25859 cp_parser_objc_class_declaration (cp_parser* parser)
25861 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25862 while (true)
25864 tree id;
25866 id = cp_parser_identifier (parser);
25867 if (id == error_mark_node)
25868 break;
25870 objc_declare_class (id);
25872 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25873 cp_lexer_consume_token (parser->lexer);
25874 else
25875 break;
25877 cp_parser_consume_semicolon_at_end_of_statement (parser);
25880 /* Parse a list of Objective-C protocol references.
25882 objc-protocol-refs-opt:
25883 objc-protocol-refs [opt]
25885 objc-protocol-refs:
25886 < objc-identifier-list >
25888 Returns a TREE_LIST of identifiers, if any. */
25890 static tree
25891 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25893 tree protorefs = NULL_TREE;
25895 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25897 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25898 protorefs = cp_parser_objc_identifier_list (parser);
25899 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25902 return protorefs;
25905 /* Parse a Objective-C visibility specification. */
25907 static void
25908 cp_parser_objc_visibility_spec (cp_parser* parser)
25910 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25912 switch (vis->keyword)
25914 case RID_AT_PRIVATE:
25915 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25916 break;
25917 case RID_AT_PROTECTED:
25918 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25919 break;
25920 case RID_AT_PUBLIC:
25921 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25922 break;
25923 case RID_AT_PACKAGE:
25924 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25925 break;
25926 default:
25927 return;
25930 /* Eat '@private'/'@protected'/'@public'. */
25931 cp_lexer_consume_token (parser->lexer);
25934 /* Parse an Objective-C method type. Return 'true' if it is a class
25935 (+) method, and 'false' if it is an instance (-) method. */
25937 static inline bool
25938 cp_parser_objc_method_type (cp_parser* parser)
25940 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25941 return true;
25942 else
25943 return false;
25946 /* Parse an Objective-C protocol qualifier. */
25948 static tree
25949 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25951 tree quals = NULL_TREE, node;
25952 cp_token *token = cp_lexer_peek_token (parser->lexer);
25954 node = token->u.value;
25956 while (node && identifier_p (node)
25957 && (node == ridpointers [(int) RID_IN]
25958 || node == ridpointers [(int) RID_OUT]
25959 || node == ridpointers [(int) RID_INOUT]
25960 || node == ridpointers [(int) RID_BYCOPY]
25961 || node == ridpointers [(int) RID_BYREF]
25962 || node == ridpointers [(int) RID_ONEWAY]))
25964 quals = tree_cons (NULL_TREE, node, quals);
25965 cp_lexer_consume_token (parser->lexer);
25966 token = cp_lexer_peek_token (parser->lexer);
25967 node = token->u.value;
25970 return quals;
25973 /* Parse an Objective-C typename. */
25975 static tree
25976 cp_parser_objc_typename (cp_parser* parser)
25978 tree type_name = NULL_TREE;
25980 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25982 tree proto_quals, cp_type = NULL_TREE;
25984 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25985 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25987 /* An ObjC type name may consist of just protocol qualifiers, in which
25988 case the type shall default to 'id'. */
25989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25991 cp_type = cp_parser_type_id (parser);
25993 /* If the type could not be parsed, an error has already
25994 been produced. For error recovery, behave as if it had
25995 not been specified, which will use the default type
25996 'id'. */
25997 if (cp_type == error_mark_node)
25999 cp_type = NULL_TREE;
26000 /* We need to skip to the closing parenthesis as
26001 cp_parser_type_id() does not seem to do it for
26002 us. */
26003 cp_parser_skip_to_closing_parenthesis (parser,
26004 /*recovering=*/true,
26005 /*or_comma=*/false,
26006 /*consume_paren=*/false);
26010 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26011 type_name = build_tree_list (proto_quals, cp_type);
26014 return type_name;
26017 /* Check to see if TYPE refers to an Objective-C selector name. */
26019 static bool
26020 cp_parser_objc_selector_p (enum cpp_ttype type)
26022 return (type == CPP_NAME || type == CPP_KEYWORD
26023 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26024 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26025 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26026 || type == CPP_XOR || type == CPP_XOR_EQ);
26029 /* Parse an Objective-C selector. */
26031 static tree
26032 cp_parser_objc_selector (cp_parser* parser)
26034 cp_token *token = cp_lexer_consume_token (parser->lexer);
26036 if (!cp_parser_objc_selector_p (token->type))
26038 error_at (token->location, "invalid Objective-C++ selector name");
26039 return error_mark_node;
26042 /* C++ operator names are allowed to appear in ObjC selectors. */
26043 switch (token->type)
26045 case CPP_AND_AND: return get_identifier ("and");
26046 case CPP_AND_EQ: return get_identifier ("and_eq");
26047 case CPP_AND: return get_identifier ("bitand");
26048 case CPP_OR: return get_identifier ("bitor");
26049 case CPP_COMPL: return get_identifier ("compl");
26050 case CPP_NOT: return get_identifier ("not");
26051 case CPP_NOT_EQ: return get_identifier ("not_eq");
26052 case CPP_OR_OR: return get_identifier ("or");
26053 case CPP_OR_EQ: return get_identifier ("or_eq");
26054 case CPP_XOR: return get_identifier ("xor");
26055 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26056 default: return token->u.value;
26060 /* Parse an Objective-C params list. */
26062 static tree
26063 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26065 tree params = NULL_TREE;
26066 bool maybe_unary_selector_p = true;
26067 cp_token *token = cp_lexer_peek_token (parser->lexer);
26069 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26071 tree selector = NULL_TREE, type_name, identifier;
26072 tree parm_attr = NULL_TREE;
26074 if (token->keyword == RID_ATTRIBUTE)
26075 break;
26077 if (token->type != CPP_COLON)
26078 selector = cp_parser_objc_selector (parser);
26080 /* Detect if we have a unary selector. */
26081 if (maybe_unary_selector_p
26082 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26084 params = selector; /* Might be followed by attributes. */
26085 break;
26088 maybe_unary_selector_p = false;
26089 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26091 /* Something went quite wrong. There should be a colon
26092 here, but there is not. Stop parsing parameters. */
26093 break;
26095 type_name = cp_parser_objc_typename (parser);
26096 /* New ObjC allows attributes on parameters too. */
26097 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26098 parm_attr = cp_parser_attributes_opt (parser);
26099 identifier = cp_parser_identifier (parser);
26101 params
26102 = chainon (params,
26103 objc_build_keyword_decl (selector,
26104 type_name,
26105 identifier,
26106 parm_attr));
26108 token = cp_lexer_peek_token (parser->lexer);
26111 if (params == NULL_TREE)
26113 cp_parser_error (parser, "objective-c++ method declaration is expected");
26114 return error_mark_node;
26117 /* We allow tail attributes for the method. */
26118 if (token->keyword == RID_ATTRIBUTE)
26120 *attributes = cp_parser_attributes_opt (parser);
26121 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26122 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26123 return params;
26124 cp_parser_error (parser,
26125 "method attributes must be specified at the end");
26126 return error_mark_node;
26129 if (params == NULL_TREE)
26131 cp_parser_error (parser, "objective-c++ method declaration is expected");
26132 return error_mark_node;
26134 return params;
26137 /* Parse the non-keyword Objective-C params. */
26139 static tree
26140 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26141 tree* attributes)
26143 tree params = make_node (TREE_LIST);
26144 cp_token *token = cp_lexer_peek_token (parser->lexer);
26145 *ellipsisp = false; /* Initially, assume no ellipsis. */
26147 while (token->type == CPP_COMMA)
26149 cp_parameter_declarator *parmdecl;
26150 tree parm;
26152 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26153 token = cp_lexer_peek_token (parser->lexer);
26155 if (token->type == CPP_ELLIPSIS)
26157 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26158 *ellipsisp = true;
26159 token = cp_lexer_peek_token (parser->lexer);
26160 break;
26163 /* TODO: parse attributes for tail parameters. */
26164 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26165 parm = grokdeclarator (parmdecl->declarator,
26166 &parmdecl->decl_specifiers,
26167 PARM, /*initialized=*/0,
26168 /*attrlist=*/NULL);
26170 chainon (params, build_tree_list (NULL_TREE, parm));
26171 token = cp_lexer_peek_token (parser->lexer);
26174 /* We allow tail attributes for the method. */
26175 if (token->keyword == RID_ATTRIBUTE)
26177 if (*attributes == NULL_TREE)
26179 *attributes = cp_parser_attributes_opt (parser);
26180 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26181 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26182 return params;
26184 else
26185 /* We have an error, but parse the attributes, so that we can
26186 carry on. */
26187 *attributes = cp_parser_attributes_opt (parser);
26189 cp_parser_error (parser,
26190 "method attributes must be specified at the end");
26191 return error_mark_node;
26194 return params;
26197 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26199 static void
26200 cp_parser_objc_interstitial_code (cp_parser* parser)
26202 cp_token *token = cp_lexer_peek_token (parser->lexer);
26204 /* If the next token is `extern' and the following token is a string
26205 literal, then we have a linkage specification. */
26206 if (token->keyword == RID_EXTERN
26207 && cp_parser_is_pure_string_literal
26208 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26209 cp_parser_linkage_specification (parser);
26210 /* Handle #pragma, if any. */
26211 else if (token->type == CPP_PRAGMA)
26212 cp_parser_pragma (parser, pragma_objc_icode);
26213 /* Allow stray semicolons. */
26214 else if (token->type == CPP_SEMICOLON)
26215 cp_lexer_consume_token (parser->lexer);
26216 /* Mark methods as optional or required, when building protocols. */
26217 else if (token->keyword == RID_AT_OPTIONAL)
26219 cp_lexer_consume_token (parser->lexer);
26220 objc_set_method_opt (true);
26222 else if (token->keyword == RID_AT_REQUIRED)
26224 cp_lexer_consume_token (parser->lexer);
26225 objc_set_method_opt (false);
26227 else if (token->keyword == RID_NAMESPACE)
26228 cp_parser_namespace_definition (parser);
26229 /* Other stray characters must generate errors. */
26230 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26232 cp_lexer_consume_token (parser->lexer);
26233 error ("stray %qs between Objective-C++ methods",
26234 token->type == CPP_OPEN_BRACE ? "{" : "}");
26236 /* Finally, try to parse a block-declaration, or a function-definition. */
26237 else
26238 cp_parser_block_declaration (parser, /*statement_p=*/false);
26241 /* Parse a method signature. */
26243 static tree
26244 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26246 tree rettype, kwdparms, optparms;
26247 bool ellipsis = false;
26248 bool is_class_method;
26250 is_class_method = cp_parser_objc_method_type (parser);
26251 rettype = cp_parser_objc_typename (parser);
26252 *attributes = NULL_TREE;
26253 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26254 if (kwdparms == error_mark_node)
26255 return error_mark_node;
26256 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26257 if (optparms == error_mark_node)
26258 return error_mark_node;
26260 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26263 static bool
26264 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26266 tree tattr;
26267 cp_lexer_save_tokens (parser->lexer);
26268 tattr = cp_parser_attributes_opt (parser);
26269 gcc_assert (tattr) ;
26271 /* If the attributes are followed by a method introducer, this is not allowed.
26272 Dump the attributes and flag the situation. */
26273 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26274 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26275 return true;
26277 /* Otherwise, the attributes introduce some interstitial code, possibly so
26278 rewind to allow that check. */
26279 cp_lexer_rollback_tokens (parser->lexer);
26280 return false;
26283 /* Parse an Objective-C method prototype list. */
26285 static void
26286 cp_parser_objc_method_prototype_list (cp_parser* parser)
26288 cp_token *token = cp_lexer_peek_token (parser->lexer);
26290 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26292 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26294 tree attributes, sig;
26295 bool is_class_method;
26296 if (token->type == CPP_PLUS)
26297 is_class_method = true;
26298 else
26299 is_class_method = false;
26300 sig = cp_parser_objc_method_signature (parser, &attributes);
26301 if (sig == error_mark_node)
26303 cp_parser_skip_to_end_of_block_or_statement (parser);
26304 token = cp_lexer_peek_token (parser->lexer);
26305 continue;
26307 objc_add_method_declaration (is_class_method, sig, attributes);
26308 cp_parser_consume_semicolon_at_end_of_statement (parser);
26310 else if (token->keyword == RID_AT_PROPERTY)
26311 cp_parser_objc_at_property_declaration (parser);
26312 else if (token->keyword == RID_ATTRIBUTE
26313 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26314 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26315 OPT_Wattributes,
26316 "prefix attributes are ignored for methods");
26317 else
26318 /* Allow for interspersed non-ObjC++ code. */
26319 cp_parser_objc_interstitial_code (parser);
26321 token = cp_lexer_peek_token (parser->lexer);
26324 if (token->type != CPP_EOF)
26325 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26326 else
26327 cp_parser_error (parser, "expected %<@end%>");
26329 objc_finish_interface ();
26332 /* Parse an Objective-C method definition list. */
26334 static void
26335 cp_parser_objc_method_definition_list (cp_parser* parser)
26337 cp_token *token = cp_lexer_peek_token (parser->lexer);
26339 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26341 tree meth;
26343 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26345 cp_token *ptk;
26346 tree sig, attribute;
26347 bool is_class_method;
26348 if (token->type == CPP_PLUS)
26349 is_class_method = true;
26350 else
26351 is_class_method = false;
26352 push_deferring_access_checks (dk_deferred);
26353 sig = cp_parser_objc_method_signature (parser, &attribute);
26354 if (sig == error_mark_node)
26356 cp_parser_skip_to_end_of_block_or_statement (parser);
26357 token = cp_lexer_peek_token (parser->lexer);
26358 continue;
26360 objc_start_method_definition (is_class_method, sig, attribute,
26361 NULL_TREE);
26363 /* For historical reasons, we accept an optional semicolon. */
26364 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26365 cp_lexer_consume_token (parser->lexer);
26367 ptk = cp_lexer_peek_token (parser->lexer);
26368 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26369 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26371 perform_deferred_access_checks (tf_warning_or_error);
26372 stop_deferring_access_checks ();
26373 meth = cp_parser_function_definition_after_declarator (parser,
26374 false);
26375 pop_deferring_access_checks ();
26376 objc_finish_method_definition (meth);
26379 /* The following case will be removed once @synthesize is
26380 completely implemented. */
26381 else if (token->keyword == RID_AT_PROPERTY)
26382 cp_parser_objc_at_property_declaration (parser);
26383 else if (token->keyword == RID_AT_SYNTHESIZE)
26384 cp_parser_objc_at_synthesize_declaration (parser);
26385 else if (token->keyword == RID_AT_DYNAMIC)
26386 cp_parser_objc_at_dynamic_declaration (parser);
26387 else if (token->keyword == RID_ATTRIBUTE
26388 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26389 warning_at (token->location, OPT_Wattributes,
26390 "prefix attributes are ignored for methods");
26391 else
26392 /* Allow for interspersed non-ObjC++ code. */
26393 cp_parser_objc_interstitial_code (parser);
26395 token = cp_lexer_peek_token (parser->lexer);
26398 if (token->type != CPP_EOF)
26399 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26400 else
26401 cp_parser_error (parser, "expected %<@end%>");
26403 objc_finish_implementation ();
26406 /* Parse Objective-C ivars. */
26408 static void
26409 cp_parser_objc_class_ivars (cp_parser* parser)
26411 cp_token *token = cp_lexer_peek_token (parser->lexer);
26413 if (token->type != CPP_OPEN_BRACE)
26414 return; /* No ivars specified. */
26416 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26417 token = cp_lexer_peek_token (parser->lexer);
26419 while (token->type != CPP_CLOSE_BRACE
26420 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26422 cp_decl_specifier_seq declspecs;
26423 int decl_class_or_enum_p;
26424 tree prefix_attributes;
26426 cp_parser_objc_visibility_spec (parser);
26428 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26429 break;
26431 cp_parser_decl_specifier_seq (parser,
26432 CP_PARSER_FLAGS_OPTIONAL,
26433 &declspecs,
26434 &decl_class_or_enum_p);
26436 /* auto, register, static, extern, mutable. */
26437 if (declspecs.storage_class != sc_none)
26439 cp_parser_error (parser, "invalid type for instance variable");
26440 declspecs.storage_class = sc_none;
26443 /* thread_local. */
26444 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26446 cp_parser_error (parser, "invalid type for instance variable");
26447 declspecs.locations[ds_thread] = 0;
26450 /* typedef. */
26451 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26453 cp_parser_error (parser, "invalid type for instance variable");
26454 declspecs.locations[ds_typedef] = 0;
26457 prefix_attributes = declspecs.attributes;
26458 declspecs.attributes = NULL_TREE;
26460 /* Keep going until we hit the `;' at the end of the
26461 declaration. */
26462 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26464 tree width = NULL_TREE, attributes, first_attribute, decl;
26465 cp_declarator *declarator = NULL;
26466 int ctor_dtor_or_conv_p;
26468 /* Check for a (possibly unnamed) bitfield declaration. */
26469 token = cp_lexer_peek_token (parser->lexer);
26470 if (token->type == CPP_COLON)
26471 goto eat_colon;
26473 if (token->type == CPP_NAME
26474 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26475 == CPP_COLON))
26477 /* Get the name of the bitfield. */
26478 declarator = make_id_declarator (NULL_TREE,
26479 cp_parser_identifier (parser),
26480 sfk_none);
26482 eat_colon:
26483 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26484 /* Get the width of the bitfield. */
26485 width
26486 = cp_parser_constant_expression (parser,
26487 /*allow_non_constant=*/false,
26488 NULL);
26490 else
26492 /* Parse the declarator. */
26493 declarator
26494 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26495 &ctor_dtor_or_conv_p,
26496 /*parenthesized_p=*/NULL,
26497 /*member_p=*/false);
26500 /* Look for attributes that apply to the ivar. */
26501 attributes = cp_parser_attributes_opt (parser);
26502 /* Remember which attributes are prefix attributes and
26503 which are not. */
26504 first_attribute = attributes;
26505 /* Combine the attributes. */
26506 attributes = chainon (prefix_attributes, attributes);
26508 if (width)
26509 /* Create the bitfield declaration. */
26510 decl = grokbitfield (declarator, &declspecs,
26511 width,
26512 attributes);
26513 else
26514 decl = grokfield (declarator, &declspecs,
26515 NULL_TREE, /*init_const_expr_p=*/false,
26516 NULL_TREE, attributes);
26518 /* Add the instance variable. */
26519 if (decl != error_mark_node && decl != NULL_TREE)
26520 objc_add_instance_variable (decl);
26522 /* Reset PREFIX_ATTRIBUTES. */
26523 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26524 attributes = TREE_CHAIN (attributes);
26525 if (attributes)
26526 TREE_CHAIN (attributes) = NULL_TREE;
26528 token = cp_lexer_peek_token (parser->lexer);
26530 if (token->type == CPP_COMMA)
26532 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26533 continue;
26535 break;
26538 cp_parser_consume_semicolon_at_end_of_statement (parser);
26539 token = cp_lexer_peek_token (parser->lexer);
26542 if (token->keyword == RID_AT_END)
26543 cp_parser_error (parser, "expected %<}%>");
26545 /* Do not consume the RID_AT_END, so it will be read again as terminating
26546 the @interface of @implementation. */
26547 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26548 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26550 /* For historical reasons, we accept an optional semicolon. */
26551 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26552 cp_lexer_consume_token (parser->lexer);
26555 /* Parse an Objective-C protocol declaration. */
26557 static void
26558 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26560 tree proto, protorefs;
26561 cp_token *tok;
26563 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26566 tok = cp_lexer_peek_token (parser->lexer);
26567 error_at (tok->location, "identifier expected after %<@protocol%>");
26568 cp_parser_consume_semicolon_at_end_of_statement (parser);
26569 return;
26572 /* See if we have a forward declaration or a definition. */
26573 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26575 /* Try a forward declaration first. */
26576 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26578 while (true)
26580 tree id;
26582 id = cp_parser_identifier (parser);
26583 if (id == error_mark_node)
26584 break;
26586 objc_declare_protocol (id, attributes);
26588 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26589 cp_lexer_consume_token (parser->lexer);
26590 else
26591 break;
26593 cp_parser_consume_semicolon_at_end_of_statement (parser);
26596 /* Ok, we got a full-fledged definition (or at least should). */
26597 else
26599 proto = cp_parser_identifier (parser);
26600 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26601 objc_start_protocol (proto, protorefs, attributes);
26602 cp_parser_objc_method_prototype_list (parser);
26606 /* Parse an Objective-C superclass or category. */
26608 static void
26609 cp_parser_objc_superclass_or_category (cp_parser *parser,
26610 bool iface_p,
26611 tree *super,
26612 tree *categ, bool *is_class_extension)
26614 cp_token *next = cp_lexer_peek_token (parser->lexer);
26616 *super = *categ = NULL_TREE;
26617 *is_class_extension = false;
26618 if (next->type == CPP_COLON)
26620 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26621 *super = cp_parser_identifier (parser);
26623 else if (next->type == CPP_OPEN_PAREN)
26625 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26627 /* If there is no category name, and this is an @interface, we
26628 have a class extension. */
26629 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26631 *categ = NULL_TREE;
26632 *is_class_extension = true;
26634 else
26635 *categ = cp_parser_identifier (parser);
26637 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26641 /* Parse an Objective-C class interface. */
26643 static void
26644 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26646 tree name, super, categ, protos;
26647 bool is_class_extension;
26649 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26650 name = cp_parser_identifier (parser);
26651 if (name == error_mark_node)
26653 /* It's hard to recover because even if valid @interface stuff
26654 is to follow, we can't compile it (or validate it) if we
26655 don't even know which class it refers to. Let's assume this
26656 was a stray '@interface' token in the stream and skip it.
26658 return;
26660 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26661 &is_class_extension);
26662 protos = cp_parser_objc_protocol_refs_opt (parser);
26664 /* We have either a class or a category on our hands. */
26665 if (categ || is_class_extension)
26666 objc_start_category_interface (name, categ, protos, attributes);
26667 else
26669 objc_start_class_interface (name, super, protos, attributes);
26670 /* Handle instance variable declarations, if any. */
26671 cp_parser_objc_class_ivars (parser);
26672 objc_continue_interface ();
26675 cp_parser_objc_method_prototype_list (parser);
26678 /* Parse an Objective-C class implementation. */
26680 static void
26681 cp_parser_objc_class_implementation (cp_parser* parser)
26683 tree name, super, categ;
26684 bool is_class_extension;
26686 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26687 name = cp_parser_identifier (parser);
26688 if (name == error_mark_node)
26690 /* It's hard to recover because even if valid @implementation
26691 stuff is to follow, we can't compile it (or validate it) if
26692 we don't even know which class it refers to. Let's assume
26693 this was a stray '@implementation' token in the stream and
26694 skip it.
26696 return;
26698 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26699 &is_class_extension);
26701 /* We have either a class or a category on our hands. */
26702 if (categ)
26703 objc_start_category_implementation (name, categ);
26704 else
26706 objc_start_class_implementation (name, super);
26707 /* Handle instance variable declarations, if any. */
26708 cp_parser_objc_class_ivars (parser);
26709 objc_continue_implementation ();
26712 cp_parser_objc_method_definition_list (parser);
26715 /* Consume the @end token and finish off the implementation. */
26717 static void
26718 cp_parser_objc_end_implementation (cp_parser* parser)
26720 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26721 objc_finish_implementation ();
26724 /* Parse an Objective-C declaration. */
26726 static void
26727 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26729 /* Try to figure out what kind of declaration is present. */
26730 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26732 if (attributes)
26733 switch (kwd->keyword)
26735 case RID_AT_ALIAS:
26736 case RID_AT_CLASS:
26737 case RID_AT_END:
26738 error_at (kwd->location, "attributes may not be specified before"
26739 " the %<@%D%> Objective-C++ keyword",
26740 kwd->u.value);
26741 attributes = NULL;
26742 break;
26743 case RID_AT_IMPLEMENTATION:
26744 warning_at (kwd->location, OPT_Wattributes,
26745 "prefix attributes are ignored before %<@%D%>",
26746 kwd->u.value);
26747 attributes = NULL;
26748 default:
26749 break;
26752 switch (kwd->keyword)
26754 case RID_AT_ALIAS:
26755 cp_parser_objc_alias_declaration (parser);
26756 break;
26757 case RID_AT_CLASS:
26758 cp_parser_objc_class_declaration (parser);
26759 break;
26760 case RID_AT_PROTOCOL:
26761 cp_parser_objc_protocol_declaration (parser, attributes);
26762 break;
26763 case RID_AT_INTERFACE:
26764 cp_parser_objc_class_interface (parser, attributes);
26765 break;
26766 case RID_AT_IMPLEMENTATION:
26767 cp_parser_objc_class_implementation (parser);
26768 break;
26769 case RID_AT_END:
26770 cp_parser_objc_end_implementation (parser);
26771 break;
26772 default:
26773 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26774 kwd->u.value);
26775 cp_parser_skip_to_end_of_block_or_statement (parser);
26779 /* Parse an Objective-C try-catch-finally statement.
26781 objc-try-catch-finally-stmt:
26782 @try compound-statement objc-catch-clause-seq [opt]
26783 objc-finally-clause [opt]
26785 objc-catch-clause-seq:
26786 objc-catch-clause objc-catch-clause-seq [opt]
26788 objc-catch-clause:
26789 @catch ( objc-exception-declaration ) compound-statement
26791 objc-finally-clause:
26792 @finally compound-statement
26794 objc-exception-declaration:
26795 parameter-declaration
26796 '...'
26798 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26800 Returns NULL_TREE.
26802 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26803 for C. Keep them in sync. */
26805 static tree
26806 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26808 location_t location;
26809 tree stmt;
26811 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26812 location = cp_lexer_peek_token (parser->lexer)->location;
26813 objc_maybe_warn_exceptions (location);
26814 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26815 node, lest it get absorbed into the surrounding block. */
26816 stmt = push_stmt_list ();
26817 cp_parser_compound_statement (parser, NULL, false, false);
26818 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26820 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26822 cp_parameter_declarator *parm;
26823 tree parameter_declaration = error_mark_node;
26824 bool seen_open_paren = false;
26826 cp_lexer_consume_token (parser->lexer);
26827 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26828 seen_open_paren = true;
26829 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26831 /* We have "@catch (...)" (where the '...' are literally
26832 what is in the code). Skip the '...'.
26833 parameter_declaration is set to NULL_TREE, and
26834 objc_being_catch_clauses() knows that that means
26835 '...'. */
26836 cp_lexer_consume_token (parser->lexer);
26837 parameter_declaration = NULL_TREE;
26839 else
26841 /* We have "@catch (NSException *exception)" or something
26842 like that. Parse the parameter declaration. */
26843 parm = cp_parser_parameter_declaration (parser, false, NULL);
26844 if (parm == NULL)
26845 parameter_declaration = error_mark_node;
26846 else
26847 parameter_declaration = grokdeclarator (parm->declarator,
26848 &parm->decl_specifiers,
26849 PARM, /*initialized=*/0,
26850 /*attrlist=*/NULL);
26852 if (seen_open_paren)
26853 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26854 else
26856 /* If there was no open parenthesis, we are recovering from
26857 an error, and we are trying to figure out what mistake
26858 the user has made. */
26860 /* If there is an immediate closing parenthesis, the user
26861 probably forgot the opening one (ie, they typed "@catch
26862 NSException *e)". Parse the closing parenthesis and keep
26863 going. */
26864 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26865 cp_lexer_consume_token (parser->lexer);
26867 /* If these is no immediate closing parenthesis, the user
26868 probably doesn't know that parenthesis are required at
26869 all (ie, they typed "@catch NSException *e"). So, just
26870 forget about the closing parenthesis and keep going. */
26872 objc_begin_catch_clause (parameter_declaration);
26873 cp_parser_compound_statement (parser, NULL, false, false);
26874 objc_finish_catch_clause ();
26876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26878 cp_lexer_consume_token (parser->lexer);
26879 location = cp_lexer_peek_token (parser->lexer)->location;
26880 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26881 node, lest it get absorbed into the surrounding block. */
26882 stmt = push_stmt_list ();
26883 cp_parser_compound_statement (parser, NULL, false, false);
26884 objc_build_finally_clause (location, pop_stmt_list (stmt));
26887 return objc_finish_try_stmt ();
26890 /* Parse an Objective-C synchronized statement.
26892 objc-synchronized-stmt:
26893 @synchronized ( expression ) compound-statement
26895 Returns NULL_TREE. */
26897 static tree
26898 cp_parser_objc_synchronized_statement (cp_parser *parser)
26900 location_t location;
26901 tree lock, stmt;
26903 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26905 location = cp_lexer_peek_token (parser->lexer)->location;
26906 objc_maybe_warn_exceptions (location);
26907 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26908 lock = cp_parser_expression (parser, false, NULL);
26909 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26911 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26912 node, lest it get absorbed into the surrounding block. */
26913 stmt = push_stmt_list ();
26914 cp_parser_compound_statement (parser, NULL, false, false);
26916 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26919 /* Parse an Objective-C throw statement.
26921 objc-throw-stmt:
26922 @throw assignment-expression [opt] ;
26924 Returns a constructed '@throw' statement. */
26926 static tree
26927 cp_parser_objc_throw_statement (cp_parser *parser)
26929 tree expr = NULL_TREE;
26930 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26932 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26935 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26937 cp_parser_consume_semicolon_at_end_of_statement (parser);
26939 return objc_build_throw_stmt (loc, expr);
26942 /* Parse an Objective-C statement. */
26944 static tree
26945 cp_parser_objc_statement (cp_parser * parser)
26947 /* Try to figure out what kind of declaration is present. */
26948 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26950 switch (kwd->keyword)
26952 case RID_AT_TRY:
26953 return cp_parser_objc_try_catch_finally_statement (parser);
26954 case RID_AT_SYNCHRONIZED:
26955 return cp_parser_objc_synchronized_statement (parser);
26956 case RID_AT_THROW:
26957 return cp_parser_objc_throw_statement (parser);
26958 default:
26959 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26960 kwd->u.value);
26961 cp_parser_skip_to_end_of_block_or_statement (parser);
26964 return error_mark_node;
26967 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26968 look ahead to see if an objc keyword follows the attributes. This
26969 is to detect the use of prefix attributes on ObjC @interface and
26970 @protocol. */
26972 static bool
26973 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26975 cp_lexer_save_tokens (parser->lexer);
26976 *attrib = cp_parser_attributes_opt (parser);
26977 gcc_assert (*attrib);
26978 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26980 cp_lexer_commit_tokens (parser->lexer);
26981 return true;
26983 cp_lexer_rollback_tokens (parser->lexer);
26984 return false;
26987 /* This routine is a minimal replacement for
26988 c_parser_struct_declaration () used when parsing the list of
26989 types/names or ObjC++ properties. For example, when parsing the
26990 code
26992 @property (readonly) int a, b, c;
26994 this function is responsible for parsing "int a, int b, int c" and
26995 returning the declarations as CHAIN of DECLs.
26997 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26998 similar parsing. */
26999 static tree
27000 cp_parser_objc_struct_declaration (cp_parser *parser)
27002 tree decls = NULL_TREE;
27003 cp_decl_specifier_seq declspecs;
27004 int decl_class_or_enum_p;
27005 tree prefix_attributes;
27007 cp_parser_decl_specifier_seq (parser,
27008 CP_PARSER_FLAGS_NONE,
27009 &declspecs,
27010 &decl_class_or_enum_p);
27012 if (declspecs.type == error_mark_node)
27013 return error_mark_node;
27015 /* auto, register, static, extern, mutable. */
27016 if (declspecs.storage_class != sc_none)
27018 cp_parser_error (parser, "invalid type for property");
27019 declspecs.storage_class = sc_none;
27022 /* thread_local. */
27023 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27025 cp_parser_error (parser, "invalid type for property");
27026 declspecs.locations[ds_thread] = 0;
27029 /* typedef. */
27030 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27032 cp_parser_error (parser, "invalid type for property");
27033 declspecs.locations[ds_typedef] = 0;
27036 prefix_attributes = declspecs.attributes;
27037 declspecs.attributes = NULL_TREE;
27039 /* Keep going until we hit the `;' at the end of the declaration. */
27040 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27042 tree attributes, first_attribute, decl;
27043 cp_declarator *declarator;
27044 cp_token *token;
27046 /* Parse the declarator. */
27047 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27048 NULL, NULL, false);
27050 /* Look for attributes that apply to the ivar. */
27051 attributes = cp_parser_attributes_opt (parser);
27052 /* Remember which attributes are prefix attributes and
27053 which are not. */
27054 first_attribute = attributes;
27055 /* Combine the attributes. */
27056 attributes = chainon (prefix_attributes, attributes);
27058 decl = grokfield (declarator, &declspecs,
27059 NULL_TREE, /*init_const_expr_p=*/false,
27060 NULL_TREE, attributes);
27062 if (decl == error_mark_node || decl == NULL_TREE)
27063 return error_mark_node;
27065 /* Reset PREFIX_ATTRIBUTES. */
27066 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27067 attributes = TREE_CHAIN (attributes);
27068 if (attributes)
27069 TREE_CHAIN (attributes) = NULL_TREE;
27071 DECL_CHAIN (decl) = decls;
27072 decls = decl;
27074 token = cp_lexer_peek_token (parser->lexer);
27075 if (token->type == CPP_COMMA)
27077 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27078 continue;
27080 else
27081 break;
27083 return decls;
27086 /* Parse an Objective-C @property declaration. The syntax is:
27088 objc-property-declaration:
27089 '@property' objc-property-attributes[opt] struct-declaration ;
27091 objc-property-attributes:
27092 '(' objc-property-attribute-list ')'
27094 objc-property-attribute-list:
27095 objc-property-attribute
27096 objc-property-attribute-list, objc-property-attribute
27098 objc-property-attribute
27099 'getter' = identifier
27100 'setter' = identifier
27101 'readonly'
27102 'readwrite'
27103 'assign'
27104 'retain'
27105 'copy'
27106 'nonatomic'
27108 For example:
27109 @property NSString *name;
27110 @property (readonly) id object;
27111 @property (retain, nonatomic, getter=getTheName) id name;
27112 @property int a, b, c;
27114 PS: This function is identical to
27115 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27116 static void
27117 cp_parser_objc_at_property_declaration (cp_parser *parser)
27119 /* The following variables hold the attributes of the properties as
27120 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27121 seen. When we see an attribute, we set them to 'true' (if they
27122 are boolean properties) or to the identifier (if they have an
27123 argument, ie, for getter and setter). Note that here we only
27124 parse the list of attributes, check the syntax and accumulate the
27125 attributes that we find. objc_add_property_declaration() will
27126 then process the information. */
27127 bool property_assign = false;
27128 bool property_copy = false;
27129 tree property_getter_ident = NULL_TREE;
27130 bool property_nonatomic = false;
27131 bool property_readonly = false;
27132 bool property_readwrite = false;
27133 bool property_retain = false;
27134 tree property_setter_ident = NULL_TREE;
27136 /* 'properties' is the list of properties that we read. Usually a
27137 single one, but maybe more (eg, in "@property int a, b, c;" there
27138 are three). */
27139 tree properties;
27140 location_t loc;
27142 loc = cp_lexer_peek_token (parser->lexer)->location;
27144 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27146 /* Parse the optional attribute list... */
27147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27149 /* Eat the '('. */
27150 cp_lexer_consume_token (parser->lexer);
27152 while (true)
27154 bool syntax_error = false;
27155 cp_token *token = cp_lexer_peek_token (parser->lexer);
27156 enum rid keyword;
27158 if (token->type != CPP_NAME)
27160 cp_parser_error (parser, "expected identifier");
27161 break;
27163 keyword = C_RID_CODE (token->u.value);
27164 cp_lexer_consume_token (parser->lexer);
27165 switch (keyword)
27167 case RID_ASSIGN: property_assign = true; break;
27168 case RID_COPY: property_copy = true; break;
27169 case RID_NONATOMIC: property_nonatomic = true; break;
27170 case RID_READONLY: property_readonly = true; break;
27171 case RID_READWRITE: property_readwrite = true; break;
27172 case RID_RETAIN: property_retain = true; break;
27174 case RID_GETTER:
27175 case RID_SETTER:
27176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27178 if (keyword == RID_GETTER)
27179 cp_parser_error (parser,
27180 "missing %<=%> (after %<getter%> attribute)");
27181 else
27182 cp_parser_error (parser,
27183 "missing %<=%> (after %<setter%> attribute)");
27184 syntax_error = true;
27185 break;
27187 cp_lexer_consume_token (parser->lexer); /* eat the = */
27188 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27190 cp_parser_error (parser, "expected identifier");
27191 syntax_error = true;
27192 break;
27194 if (keyword == RID_SETTER)
27196 if (property_setter_ident != NULL_TREE)
27198 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27199 cp_lexer_consume_token (parser->lexer);
27201 else
27202 property_setter_ident = cp_parser_objc_selector (parser);
27203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27204 cp_parser_error (parser, "setter name must terminate with %<:%>");
27205 else
27206 cp_lexer_consume_token (parser->lexer);
27208 else
27210 if (property_getter_ident != NULL_TREE)
27212 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27213 cp_lexer_consume_token (parser->lexer);
27215 else
27216 property_getter_ident = cp_parser_objc_selector (parser);
27218 break;
27219 default:
27220 cp_parser_error (parser, "unknown property attribute");
27221 syntax_error = true;
27222 break;
27225 if (syntax_error)
27226 break;
27228 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27229 cp_lexer_consume_token (parser->lexer);
27230 else
27231 break;
27234 /* FIXME: "@property (setter, assign);" will generate a spurious
27235 "error: expected ‘)’ before ‘,’ token". This is because
27236 cp_parser_require, unlike the C counterpart, will produce an
27237 error even if we are in error recovery. */
27238 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27240 cp_parser_skip_to_closing_parenthesis (parser,
27241 /*recovering=*/true,
27242 /*or_comma=*/false,
27243 /*consume_paren=*/true);
27247 /* ... and the property declaration(s). */
27248 properties = cp_parser_objc_struct_declaration (parser);
27250 if (properties == error_mark_node)
27252 cp_parser_skip_to_end_of_statement (parser);
27253 /* If the next token is now a `;', consume it. */
27254 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27255 cp_lexer_consume_token (parser->lexer);
27256 return;
27259 if (properties == NULL_TREE)
27260 cp_parser_error (parser, "expected identifier");
27261 else
27263 /* Comma-separated properties are chained together in
27264 reverse order; add them one by one. */
27265 properties = nreverse (properties);
27267 for (; properties; properties = TREE_CHAIN (properties))
27268 objc_add_property_declaration (loc, copy_node (properties),
27269 property_readonly, property_readwrite,
27270 property_assign, property_retain,
27271 property_copy, property_nonatomic,
27272 property_getter_ident, property_setter_ident);
27275 cp_parser_consume_semicolon_at_end_of_statement (parser);
27278 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27280 objc-synthesize-declaration:
27281 @synthesize objc-synthesize-identifier-list ;
27283 objc-synthesize-identifier-list:
27284 objc-synthesize-identifier
27285 objc-synthesize-identifier-list, objc-synthesize-identifier
27287 objc-synthesize-identifier
27288 identifier
27289 identifier = identifier
27291 For example:
27292 @synthesize MyProperty;
27293 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27295 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27296 for C. Keep them in sync.
27298 static void
27299 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27301 tree list = NULL_TREE;
27302 location_t loc;
27303 loc = cp_lexer_peek_token (parser->lexer)->location;
27305 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27306 while (true)
27308 tree property, ivar;
27309 property = cp_parser_identifier (parser);
27310 if (property == error_mark_node)
27312 cp_parser_consume_semicolon_at_end_of_statement (parser);
27313 return;
27315 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27317 cp_lexer_consume_token (parser->lexer);
27318 ivar = cp_parser_identifier (parser);
27319 if (ivar == error_mark_node)
27321 cp_parser_consume_semicolon_at_end_of_statement (parser);
27322 return;
27325 else
27326 ivar = NULL_TREE;
27327 list = chainon (list, build_tree_list (ivar, property));
27328 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27329 cp_lexer_consume_token (parser->lexer);
27330 else
27331 break;
27333 cp_parser_consume_semicolon_at_end_of_statement (parser);
27334 objc_add_synthesize_declaration (loc, list);
27337 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27339 objc-dynamic-declaration:
27340 @dynamic identifier-list ;
27342 For example:
27343 @dynamic MyProperty;
27344 @dynamic MyProperty, AnotherProperty;
27346 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27347 for C. Keep them in sync.
27349 static void
27350 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27352 tree list = NULL_TREE;
27353 location_t loc;
27354 loc = cp_lexer_peek_token (parser->lexer)->location;
27356 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27357 while (true)
27359 tree property;
27360 property = cp_parser_identifier (parser);
27361 if (property == error_mark_node)
27363 cp_parser_consume_semicolon_at_end_of_statement (parser);
27364 return;
27366 list = chainon (list, build_tree_list (NULL, property));
27367 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27368 cp_lexer_consume_token (parser->lexer);
27369 else
27370 break;
27372 cp_parser_consume_semicolon_at_end_of_statement (parser);
27373 objc_add_dynamic_declaration (loc, list);
27377 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27379 /* Returns name of the next clause.
27380 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27381 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27382 returned and the token is consumed. */
27384 static pragma_omp_clause
27385 cp_parser_omp_clause_name (cp_parser *parser)
27387 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27390 result = PRAGMA_OMP_CLAUSE_IF;
27391 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27392 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27393 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27394 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27395 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27396 result = PRAGMA_OMP_CLAUSE_FOR;
27397 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27399 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27400 const char *p = IDENTIFIER_POINTER (id);
27402 switch (p[0])
27404 case 'a':
27405 if (!strcmp ("aligned", p))
27406 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27407 break;
27408 case 'c':
27409 if (!strcmp ("collapse", p))
27410 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27411 else if (!strcmp ("copyin", p))
27412 result = PRAGMA_OMP_CLAUSE_COPYIN;
27413 else if (!strcmp ("copyprivate", p))
27414 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27415 break;
27416 case 'd':
27417 if (!strcmp ("depend", p))
27418 result = PRAGMA_OMP_CLAUSE_DEPEND;
27419 else if (!strcmp ("device", p))
27420 result = PRAGMA_OMP_CLAUSE_DEVICE;
27421 else if (!strcmp ("dist_schedule", p))
27422 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27423 break;
27424 case 'f':
27425 if (!strcmp ("final", p))
27426 result = PRAGMA_OMP_CLAUSE_FINAL;
27427 else if (!strcmp ("firstprivate", p))
27428 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27429 else if (!strcmp ("from", p))
27430 result = PRAGMA_OMP_CLAUSE_FROM;
27431 break;
27432 case 'i':
27433 if (!strcmp ("inbranch", p))
27434 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27435 break;
27436 case 'l':
27437 if (!strcmp ("lastprivate", p))
27438 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27439 else if (!strcmp ("linear", p))
27440 result = PRAGMA_OMP_CLAUSE_LINEAR;
27441 break;
27442 case 'm':
27443 if (!strcmp ("map", p))
27444 result = PRAGMA_OMP_CLAUSE_MAP;
27445 else if (!strcmp ("mergeable", p))
27446 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27447 break;
27448 case 'n':
27449 if (!strcmp ("notinbranch", p))
27450 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27451 else if (!strcmp ("nowait", p))
27452 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27453 else if (!strcmp ("num_teams", p))
27454 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27455 else if (!strcmp ("num_threads", p))
27456 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27457 break;
27458 case 'o':
27459 if (!strcmp ("ordered", p))
27460 result = PRAGMA_OMP_CLAUSE_ORDERED;
27461 break;
27462 case 'p':
27463 if (!strcmp ("parallel", p))
27464 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27465 else if (!strcmp ("proc_bind", p))
27466 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27467 break;
27468 case 'r':
27469 if (!strcmp ("reduction", p))
27470 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27471 break;
27472 case 's':
27473 if (!strcmp ("safelen", p))
27474 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27475 else if (!strcmp ("schedule", p))
27476 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27477 else if (!strcmp ("sections", p))
27478 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27479 else if (!strcmp ("shared", p))
27480 result = PRAGMA_OMP_CLAUSE_SHARED;
27481 else if (!strcmp ("simdlen", p))
27482 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27483 break;
27484 case 't':
27485 if (!strcmp ("taskgroup", p))
27486 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27487 else if (!strcmp ("thread_limit", p))
27488 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27489 else if (!strcmp ("to", p))
27490 result = PRAGMA_OMP_CLAUSE_TO;
27491 break;
27492 case 'u':
27493 if (!strcmp ("uniform", p))
27494 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27495 else if (!strcmp ("untied", p))
27496 result = PRAGMA_OMP_CLAUSE_UNTIED;
27497 break;
27501 if (result != PRAGMA_OMP_CLAUSE_NONE)
27502 cp_lexer_consume_token (parser->lexer);
27504 return result;
27507 /* Validate that a clause of the given type does not already exist. */
27509 static void
27510 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27511 const char *name, location_t location)
27513 tree c;
27515 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27516 if (OMP_CLAUSE_CODE (c) == code)
27518 error_at (location, "too many %qs clauses", name);
27519 break;
27523 /* OpenMP 2.5:
27524 variable-list:
27525 identifier
27526 variable-list , identifier
27528 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27529 colon). An opening parenthesis will have been consumed by the caller.
27531 If KIND is nonzero, create the appropriate node and install the decl
27532 in OMP_CLAUSE_DECL and add the node to the head of the list.
27534 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27535 return the list created.
27537 COLON can be NULL if only closing parenthesis should end the list,
27538 or pointer to bool which will receive false if the list is terminated
27539 by closing parenthesis or true if the list is terminated by colon. */
27541 static tree
27542 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27543 tree list, bool *colon)
27545 cp_token *token;
27546 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27547 if (colon)
27549 parser->colon_corrects_to_scope_p = false;
27550 *colon = false;
27552 while (1)
27554 tree name, decl;
27556 token = cp_lexer_peek_token (parser->lexer);
27557 name = cp_parser_id_expression (parser, /*template_p=*/false,
27558 /*check_dependency_p=*/true,
27559 /*template_p=*/NULL,
27560 /*declarator_p=*/false,
27561 /*optional_p=*/false);
27562 if (name == error_mark_node)
27563 goto skip_comma;
27565 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27566 if (decl == error_mark_node)
27567 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27568 token->location);
27569 else if (kind != 0)
27571 switch (kind)
27573 case OMP_CLAUSE_MAP:
27574 case OMP_CLAUSE_FROM:
27575 case OMP_CLAUSE_TO:
27576 case OMP_CLAUSE_DEPEND:
27577 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27579 tree low_bound = NULL_TREE, length = NULL_TREE;
27581 parser->colon_corrects_to_scope_p = false;
27582 cp_lexer_consume_token (parser->lexer);
27583 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27584 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27585 NULL);
27586 if (!colon)
27587 parser->colon_corrects_to_scope_p
27588 = saved_colon_corrects_to_scope_p;
27589 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27590 length = integer_one_node;
27591 else
27593 /* Look for `:'. */
27594 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27595 goto skip_comma;
27596 if (!cp_lexer_next_token_is (parser->lexer,
27597 CPP_CLOSE_SQUARE))
27598 length = cp_parser_expression (parser,
27599 /*cast_p=*/false,
27600 NULL);
27602 /* Look for the closing `]'. */
27603 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27604 RT_CLOSE_SQUARE))
27605 goto skip_comma;
27606 decl = tree_cons (low_bound, length, decl);
27608 break;
27609 default:
27610 break;
27613 tree u = build_omp_clause (token->location, kind);
27614 OMP_CLAUSE_DECL (u) = decl;
27615 OMP_CLAUSE_CHAIN (u) = list;
27616 list = u;
27618 else
27619 list = tree_cons (decl, NULL_TREE, list);
27621 get_comma:
27622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27623 break;
27624 cp_lexer_consume_token (parser->lexer);
27627 if (colon)
27628 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27630 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27632 *colon = true;
27633 cp_parser_require (parser, CPP_COLON, RT_COLON);
27634 return list;
27637 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27639 int ending;
27641 /* Try to resync to an unnested comma. Copied from
27642 cp_parser_parenthesized_expression_list. */
27643 skip_comma:
27644 if (colon)
27645 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27646 ending = cp_parser_skip_to_closing_parenthesis (parser,
27647 /*recovering=*/true,
27648 /*or_comma=*/true,
27649 /*consume_paren=*/true);
27650 if (ending < 0)
27651 goto get_comma;
27654 return list;
27657 /* Similarly, but expect leading and trailing parenthesis. This is a very
27658 common case for omp clauses. */
27660 static tree
27661 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27663 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27664 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27665 return list;
27668 /* OpenMP 3.0:
27669 collapse ( constant-expression ) */
27671 static tree
27672 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27674 tree c, num;
27675 location_t loc;
27676 HOST_WIDE_INT n;
27678 loc = cp_lexer_peek_token (parser->lexer)->location;
27679 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27680 return list;
27682 num = cp_parser_constant_expression (parser, false, NULL);
27684 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27685 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27686 /*or_comma=*/false,
27687 /*consume_paren=*/true);
27689 if (num == error_mark_node)
27690 return list;
27691 num = fold_non_dependent_expr (num);
27692 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27693 || !host_integerp (num, 0)
27694 || (n = tree_low_cst (num, 0)) <= 0
27695 || (int) n != n)
27697 error_at (loc, "collapse argument needs positive constant integer expression");
27698 return list;
27701 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27702 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27703 OMP_CLAUSE_CHAIN (c) = list;
27704 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27706 return c;
27709 /* OpenMP 2.5:
27710 default ( shared | none ) */
27712 static tree
27713 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27715 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27716 tree c;
27718 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27719 return list;
27720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27722 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27723 const char *p = IDENTIFIER_POINTER (id);
27725 switch (p[0])
27727 case 'n':
27728 if (strcmp ("none", p) != 0)
27729 goto invalid_kind;
27730 kind = OMP_CLAUSE_DEFAULT_NONE;
27731 break;
27733 case 's':
27734 if (strcmp ("shared", p) != 0)
27735 goto invalid_kind;
27736 kind = OMP_CLAUSE_DEFAULT_SHARED;
27737 break;
27739 default:
27740 goto invalid_kind;
27743 cp_lexer_consume_token (parser->lexer);
27745 else
27747 invalid_kind:
27748 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27751 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27752 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27753 /*or_comma=*/false,
27754 /*consume_paren=*/true);
27756 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27757 return list;
27759 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27760 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27761 OMP_CLAUSE_CHAIN (c) = list;
27762 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27764 return c;
27767 /* OpenMP 3.1:
27768 final ( expression ) */
27770 static tree
27771 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27773 tree t, c;
27775 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27776 return list;
27778 t = cp_parser_condition (parser);
27780 if (t == error_mark_node
27781 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27782 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27783 /*or_comma=*/false,
27784 /*consume_paren=*/true);
27786 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27788 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27789 OMP_CLAUSE_FINAL_EXPR (c) = t;
27790 OMP_CLAUSE_CHAIN (c) = list;
27792 return c;
27795 /* OpenMP 2.5:
27796 if ( expression ) */
27798 static tree
27799 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27801 tree t, c;
27803 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27804 return list;
27806 t = cp_parser_condition (parser);
27808 if (t == error_mark_node
27809 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27810 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27811 /*or_comma=*/false,
27812 /*consume_paren=*/true);
27814 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27816 c = build_omp_clause (location, OMP_CLAUSE_IF);
27817 OMP_CLAUSE_IF_EXPR (c) = t;
27818 OMP_CLAUSE_CHAIN (c) = list;
27820 return c;
27823 /* OpenMP 3.1:
27824 mergeable */
27826 static tree
27827 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27828 tree list, location_t location)
27830 tree c;
27832 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27833 location);
27835 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27836 OMP_CLAUSE_CHAIN (c) = list;
27837 return c;
27840 /* OpenMP 2.5:
27841 nowait */
27843 static tree
27844 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27845 tree list, location_t location)
27847 tree c;
27849 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27851 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27852 OMP_CLAUSE_CHAIN (c) = list;
27853 return c;
27856 /* OpenMP 2.5:
27857 num_threads ( expression ) */
27859 static tree
27860 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27861 location_t location)
27863 tree t, c;
27865 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27866 return list;
27868 t = cp_parser_expression (parser, false, NULL);
27870 if (t == error_mark_node
27871 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27872 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27873 /*or_comma=*/false,
27874 /*consume_paren=*/true);
27876 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27877 "num_threads", location);
27879 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27880 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27881 OMP_CLAUSE_CHAIN (c) = list;
27883 return c;
27886 /* OpenMP 2.5:
27887 ordered */
27889 static tree
27890 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27891 tree list, location_t location)
27893 tree c;
27895 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27896 "ordered", location);
27898 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27899 OMP_CLAUSE_CHAIN (c) = list;
27900 return c;
27903 /* OpenMP 2.5:
27904 reduction ( reduction-operator : variable-list )
27906 reduction-operator:
27907 One of: + * - & ^ | && ||
27909 OpenMP 3.1:
27911 reduction-operator:
27912 One of: + * - & ^ | && || min max
27914 OpenMP 4.0:
27916 reduction-operator:
27917 One of: + * - & ^ | && ||
27918 id-expression */
27920 static tree
27921 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27923 enum tree_code code = ERROR_MARK;
27924 tree nlist, c, id = NULL_TREE;
27926 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27927 return list;
27929 switch (cp_lexer_peek_token (parser->lexer)->type)
27931 case CPP_PLUS: code = PLUS_EXPR; break;
27932 case CPP_MULT: code = MULT_EXPR; break;
27933 case CPP_MINUS: code = MINUS_EXPR; break;
27934 case CPP_AND: code = BIT_AND_EXPR; break;
27935 case CPP_XOR: code = BIT_XOR_EXPR; break;
27936 case CPP_OR: code = BIT_IOR_EXPR; break;
27937 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27938 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27939 default: break;
27942 if (code != ERROR_MARK)
27943 cp_lexer_consume_token (parser->lexer);
27944 else
27946 bool saved_colon_corrects_to_scope_p;
27947 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27948 parser->colon_corrects_to_scope_p = false;
27949 id = cp_parser_id_expression (parser, /*template_p=*/false,
27950 /*check_dependency_p=*/true,
27951 /*template_p=*/NULL,
27952 /*declarator_p=*/false,
27953 /*optional_p=*/false);
27954 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27955 if (identifier_p (id))
27957 const char *p = IDENTIFIER_POINTER (id);
27959 if (strcmp (p, "min") == 0)
27960 code = MIN_EXPR;
27961 else if (strcmp (p, "max") == 0)
27962 code = MAX_EXPR;
27963 else if (id == ansi_opname (PLUS_EXPR))
27964 code = PLUS_EXPR;
27965 else if (id == ansi_opname (MULT_EXPR))
27966 code = MULT_EXPR;
27967 else if (id == ansi_opname (MINUS_EXPR))
27968 code = MINUS_EXPR;
27969 else if (id == ansi_opname (BIT_AND_EXPR))
27970 code = BIT_AND_EXPR;
27971 else if (id == ansi_opname (BIT_IOR_EXPR))
27972 code = BIT_IOR_EXPR;
27973 else if (id == ansi_opname (BIT_XOR_EXPR))
27974 code = BIT_XOR_EXPR;
27975 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27976 code = TRUTH_ANDIF_EXPR;
27977 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27978 code = TRUTH_ORIF_EXPR;
27979 id = omp_reduction_id (code, id, NULL_TREE);
27980 tree scope = parser->scope;
27981 if (scope)
27982 id = build_qualified_name (NULL_TREE, scope, id, false);
27983 parser->scope = NULL_TREE;
27984 parser->qualifying_scope = NULL_TREE;
27985 parser->object_scope = NULL_TREE;
27987 else
27989 error ("invalid reduction-identifier");
27990 resync_fail:
27991 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27992 /*or_comma=*/false,
27993 /*consume_paren=*/true);
27994 return list;
27998 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27999 goto resync_fail;
28001 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28002 NULL);
28003 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28005 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28006 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28009 return nlist;
28012 /* OpenMP 2.5:
28013 schedule ( schedule-kind )
28014 schedule ( schedule-kind , expression )
28016 schedule-kind:
28017 static | dynamic | guided | runtime | auto */
28019 static tree
28020 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28022 tree c, t;
28024 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28025 return list;
28027 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28031 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28032 const char *p = IDENTIFIER_POINTER (id);
28034 switch (p[0])
28036 case 'd':
28037 if (strcmp ("dynamic", p) != 0)
28038 goto invalid_kind;
28039 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28040 break;
28042 case 'g':
28043 if (strcmp ("guided", p) != 0)
28044 goto invalid_kind;
28045 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28046 break;
28048 case 'r':
28049 if (strcmp ("runtime", p) != 0)
28050 goto invalid_kind;
28051 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28052 break;
28054 default:
28055 goto invalid_kind;
28058 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28059 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28060 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28061 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28062 else
28063 goto invalid_kind;
28064 cp_lexer_consume_token (parser->lexer);
28066 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28068 cp_token *token;
28069 cp_lexer_consume_token (parser->lexer);
28071 token = cp_lexer_peek_token (parser->lexer);
28072 t = cp_parser_assignment_expression (parser, false, NULL);
28074 if (t == error_mark_node)
28075 goto resync_fail;
28076 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28077 error_at (token->location, "schedule %<runtime%> does not take "
28078 "a %<chunk_size%> parameter");
28079 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28080 error_at (token->location, "schedule %<auto%> does not take "
28081 "a %<chunk_size%> parameter");
28082 else
28083 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28085 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28086 goto resync_fail;
28088 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28089 goto resync_fail;
28091 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28092 OMP_CLAUSE_CHAIN (c) = list;
28093 return c;
28095 invalid_kind:
28096 cp_parser_error (parser, "invalid schedule kind");
28097 resync_fail:
28098 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28099 /*or_comma=*/false,
28100 /*consume_paren=*/true);
28101 return list;
28104 /* OpenMP 3.0:
28105 untied */
28107 static tree
28108 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28109 tree list, location_t location)
28111 tree c;
28113 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28115 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28116 OMP_CLAUSE_CHAIN (c) = list;
28117 return c;
28120 /* OpenMP 4.0:
28121 inbranch
28122 notinbranch */
28124 static tree
28125 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28126 tree list, location_t location)
28128 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28129 tree c = build_omp_clause (location, code);
28130 OMP_CLAUSE_CHAIN (c) = list;
28131 return c;
28134 /* OpenMP 4.0:
28135 parallel
28137 sections
28138 taskgroup */
28140 static tree
28141 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28142 enum omp_clause_code code,
28143 tree list, location_t location)
28145 tree c = build_omp_clause (location, code);
28146 OMP_CLAUSE_CHAIN (c) = list;
28147 return c;
28150 /* OpenMP 4.0:
28151 num_teams ( expression ) */
28153 static tree
28154 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28155 location_t location)
28157 tree t, c;
28159 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28160 return list;
28162 t = cp_parser_expression (parser, false, NULL);
28164 if (t == error_mark_node
28165 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28166 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28167 /*or_comma=*/false,
28168 /*consume_paren=*/true);
28170 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28171 "num_teams", location);
28173 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28174 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28175 OMP_CLAUSE_CHAIN (c) = list;
28177 return c;
28180 /* OpenMP 4.0:
28181 thread_limit ( expression ) */
28183 static tree
28184 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28185 location_t location)
28187 tree t, c;
28189 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28190 return list;
28192 t = cp_parser_expression (parser, false, NULL);
28194 if (t == error_mark_node
28195 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28196 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28197 /*or_comma=*/false,
28198 /*consume_paren=*/true);
28200 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28201 "thread_limit", location);
28203 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28204 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28205 OMP_CLAUSE_CHAIN (c) = list;
28207 return c;
28210 /* OpenMP 4.0:
28211 aligned ( variable-list )
28212 aligned ( variable-list : constant-expression ) */
28214 static tree
28215 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28217 tree nlist, c, alignment = NULL_TREE;
28218 bool colon;
28220 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28221 return list;
28223 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28224 &colon);
28226 if (colon)
28228 alignment = cp_parser_constant_expression (parser, false, NULL);
28230 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28231 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28232 /*or_comma=*/false,
28233 /*consume_paren=*/true);
28235 if (alignment == error_mark_node)
28236 alignment = NULL_TREE;
28239 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28240 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28242 return nlist;
28245 /* OpenMP 4.0:
28246 linear ( variable-list )
28247 linear ( variable-list : expression ) */
28249 static tree
28250 cp_parser_omp_clause_linear (cp_parser *parser, tree list)
28252 tree nlist, c, step = integer_one_node;
28253 bool colon;
28255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28256 return list;
28258 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28259 &colon);
28261 if (colon)
28263 step = cp_parser_expression (parser, false, NULL);
28265 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28266 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28267 /*or_comma=*/false,
28268 /*consume_paren=*/true);
28270 if (step == error_mark_node)
28271 return list;
28274 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28275 OMP_CLAUSE_LINEAR_STEP (c) = step;
28277 return nlist;
28280 /* OpenMP 4.0:
28281 safelen ( constant-expression ) */
28283 static tree
28284 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28285 location_t location)
28287 tree t, c;
28289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28290 return list;
28292 t = cp_parser_constant_expression (parser, false, NULL);
28294 if (t == error_mark_node
28295 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28297 /*or_comma=*/false,
28298 /*consume_paren=*/true);
28300 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28302 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28303 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28304 OMP_CLAUSE_CHAIN (c) = list;
28306 return c;
28309 /* OpenMP 4.0:
28310 simdlen ( constant-expression ) */
28312 static tree
28313 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28314 location_t location)
28316 tree t, c;
28318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28319 return list;
28321 t = cp_parser_constant_expression (parser, false, NULL);
28323 if (t == error_mark_node
28324 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28325 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28326 /*or_comma=*/false,
28327 /*consume_paren=*/true);
28329 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28331 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28332 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28333 OMP_CLAUSE_CHAIN (c) = list;
28335 return c;
28338 /* OpenMP 4.0:
28339 depend ( depend-kind : variable-list )
28341 depend-kind:
28342 in | out | inout */
28344 static tree
28345 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28347 tree nlist, c;
28348 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28350 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28351 return list;
28353 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28355 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28356 const char *p = IDENTIFIER_POINTER (id);
28358 if (strcmp ("in", p) == 0)
28359 kind = OMP_CLAUSE_DEPEND_IN;
28360 else if (strcmp ("inout", p) == 0)
28361 kind = OMP_CLAUSE_DEPEND_INOUT;
28362 else if (strcmp ("out", p) == 0)
28363 kind = OMP_CLAUSE_DEPEND_OUT;
28364 else
28365 goto invalid_kind;
28367 else
28368 goto invalid_kind;
28370 cp_lexer_consume_token (parser->lexer);
28371 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28372 goto resync_fail;
28374 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28375 NULL);
28377 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28378 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28380 return nlist;
28382 invalid_kind:
28383 cp_parser_error (parser, "invalid depend kind");
28384 resync_fail:
28385 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28386 /*or_comma=*/false,
28387 /*consume_paren=*/true);
28388 return list;
28391 /* OpenMP 4.0:
28392 map ( map-kind : variable-list )
28393 map ( variable-list )
28395 map-kind:
28396 alloc | to | from | tofrom */
28398 static tree
28399 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28401 tree nlist, c;
28402 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28404 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28405 return list;
28407 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28408 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28410 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28411 const char *p = IDENTIFIER_POINTER (id);
28413 if (strcmp ("alloc", p) == 0)
28414 kind = OMP_CLAUSE_MAP_ALLOC;
28415 else if (strcmp ("to", p) == 0)
28416 kind = OMP_CLAUSE_MAP_TO;
28417 else if (strcmp ("from", p) == 0)
28418 kind = OMP_CLAUSE_MAP_FROM;
28419 else if (strcmp ("tofrom", p) == 0)
28420 kind = OMP_CLAUSE_MAP_TOFROM;
28421 else
28423 cp_parser_error (parser, "invalid map kind");
28424 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28425 /*or_comma=*/false,
28426 /*consume_paren=*/true);
28427 return list;
28429 cp_lexer_consume_token (parser->lexer);
28430 cp_lexer_consume_token (parser->lexer);
28433 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28434 NULL);
28436 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28437 OMP_CLAUSE_MAP_KIND (c) = kind;
28439 return nlist;
28442 /* OpenMP 4.0:
28443 device ( expression ) */
28445 static tree
28446 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28447 location_t location)
28449 tree t, c;
28451 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28452 return list;
28454 t = cp_parser_expression (parser, false, NULL);
28456 if (t == error_mark_node
28457 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28458 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28459 /*or_comma=*/false,
28460 /*consume_paren=*/true);
28462 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28463 "device", location);
28465 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28466 OMP_CLAUSE_DEVICE_ID (c) = t;
28467 OMP_CLAUSE_CHAIN (c) = list;
28469 return c;
28472 /* OpenMP 4.0:
28473 dist_schedule ( static )
28474 dist_schedule ( static , expression ) */
28476 static tree
28477 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28478 location_t location)
28480 tree c, t;
28482 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28483 return list;
28485 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28487 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28488 goto invalid_kind;
28489 cp_lexer_consume_token (parser->lexer);
28491 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28493 cp_lexer_consume_token (parser->lexer);
28495 t = cp_parser_assignment_expression (parser, false, NULL);
28497 if (t == error_mark_node)
28498 goto resync_fail;
28499 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28501 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28502 goto resync_fail;
28504 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28505 goto resync_fail;
28507 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28508 location);
28509 OMP_CLAUSE_CHAIN (c) = list;
28510 return c;
28512 invalid_kind:
28513 cp_parser_error (parser, "invalid dist_schedule kind");
28514 resync_fail:
28515 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28516 /*or_comma=*/false,
28517 /*consume_paren=*/true);
28518 return list;
28521 /* OpenMP 4.0:
28522 proc_bind ( proc-bind-kind )
28524 proc-bind-kind:
28525 master | close | spread */
28527 static tree
28528 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28529 location_t location)
28531 tree c;
28532 enum omp_clause_proc_bind_kind kind;
28534 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28535 return list;
28537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28539 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28540 const char *p = IDENTIFIER_POINTER (id);
28542 if (strcmp ("master", p) == 0)
28543 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28544 else if (strcmp ("close", p) == 0)
28545 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28546 else if (strcmp ("spread", p) == 0)
28547 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28548 else
28549 goto invalid_kind;
28551 else
28552 goto invalid_kind;
28554 cp_lexer_consume_token (parser->lexer);
28555 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28556 goto resync_fail;
28558 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28559 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28560 location);
28561 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28562 OMP_CLAUSE_CHAIN (c) = list;
28563 return c;
28565 invalid_kind:
28566 cp_parser_error (parser, "invalid depend kind");
28567 resync_fail:
28568 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28569 /*or_comma=*/false,
28570 /*consume_paren=*/true);
28571 return list;
28574 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28575 is a bitmask in MASK. Return the list of clauses found; the result
28576 of clause default goes in *pdefault. */
28578 static tree
28579 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28580 const char *where, cp_token *pragma_tok,
28581 bool finish_p = true)
28583 tree clauses = NULL;
28584 bool first = true;
28585 cp_token *token = NULL;
28587 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28589 pragma_omp_clause c_kind;
28590 const char *c_name;
28591 tree prev = clauses;
28593 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28594 cp_lexer_consume_token (parser->lexer);
28596 token = cp_lexer_peek_token (parser->lexer);
28597 c_kind = cp_parser_omp_clause_name (parser);
28599 switch (c_kind)
28601 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28602 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28603 token->location);
28604 c_name = "collapse";
28605 break;
28606 case PRAGMA_OMP_CLAUSE_COPYIN:
28607 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28608 c_name = "copyin";
28609 break;
28610 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28611 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28612 clauses);
28613 c_name = "copyprivate";
28614 break;
28615 case PRAGMA_OMP_CLAUSE_DEFAULT:
28616 clauses = cp_parser_omp_clause_default (parser, clauses,
28617 token->location);
28618 c_name = "default";
28619 break;
28620 case PRAGMA_OMP_CLAUSE_FINAL:
28621 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28622 c_name = "final";
28623 break;
28624 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28625 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28626 clauses);
28627 c_name = "firstprivate";
28628 break;
28629 case PRAGMA_OMP_CLAUSE_IF:
28630 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28631 c_name = "if";
28632 break;
28633 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28634 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28635 clauses);
28636 c_name = "lastprivate";
28637 break;
28638 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28639 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28640 token->location);
28641 c_name = "mergeable";
28642 break;
28643 case PRAGMA_OMP_CLAUSE_NOWAIT:
28644 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28645 c_name = "nowait";
28646 break;
28647 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28648 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28649 token->location);
28650 c_name = "num_threads";
28651 break;
28652 case PRAGMA_OMP_CLAUSE_ORDERED:
28653 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28654 token->location);
28655 c_name = "ordered";
28656 break;
28657 case PRAGMA_OMP_CLAUSE_PRIVATE:
28658 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28659 clauses);
28660 c_name = "private";
28661 break;
28662 case PRAGMA_OMP_CLAUSE_REDUCTION:
28663 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28664 c_name = "reduction";
28665 break;
28666 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28667 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28668 token->location);
28669 c_name = "schedule";
28670 break;
28671 case PRAGMA_OMP_CLAUSE_SHARED:
28672 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28673 clauses);
28674 c_name = "shared";
28675 break;
28676 case PRAGMA_OMP_CLAUSE_UNTIED:
28677 clauses = cp_parser_omp_clause_untied (parser, clauses,
28678 token->location);
28679 c_name = "untied";
28680 break;
28681 case PRAGMA_OMP_CLAUSE_INBRANCH:
28682 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28683 clauses, token->location);
28684 c_name = "inbranch";
28685 break;
28686 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28687 clauses = cp_parser_omp_clause_branch (parser,
28688 OMP_CLAUSE_NOTINBRANCH,
28689 clauses, token->location);
28690 c_name = "notinbranch";
28691 break;
28692 case PRAGMA_OMP_CLAUSE_PARALLEL:
28693 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28694 clauses, token->location);
28695 c_name = "parallel";
28696 if (!first)
28698 clause_not_first:
28699 error_at (token->location, "%qs must be the first clause of %qs",
28700 c_name, where);
28701 clauses = prev;
28703 break;
28704 case PRAGMA_OMP_CLAUSE_FOR:
28705 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28706 clauses, token->location);
28707 c_name = "for";
28708 if (!first)
28709 goto clause_not_first;
28710 break;
28711 case PRAGMA_OMP_CLAUSE_SECTIONS:
28712 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28713 clauses, token->location);
28714 c_name = "sections";
28715 if (!first)
28716 goto clause_not_first;
28717 break;
28718 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28719 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28720 clauses, token->location);
28721 c_name = "taskgroup";
28722 if (!first)
28723 goto clause_not_first;
28724 break;
28725 case PRAGMA_OMP_CLAUSE_TO:
28726 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28727 clauses);
28728 c_name = "to";
28729 break;
28730 case PRAGMA_OMP_CLAUSE_FROM:
28731 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28732 clauses);
28733 c_name = "from";
28734 break;
28735 case PRAGMA_OMP_CLAUSE_UNIFORM:
28736 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28737 clauses);
28738 c_name = "uniform";
28739 break;
28740 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28741 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28742 token->location);
28743 c_name = "num_teams";
28744 break;
28745 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28746 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28747 token->location);
28748 c_name = "thread_limit";
28749 break;
28750 case PRAGMA_OMP_CLAUSE_ALIGNED:
28751 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28752 c_name = "aligned";
28753 break;
28754 case PRAGMA_OMP_CLAUSE_LINEAR:
28755 clauses = cp_parser_omp_clause_linear (parser, clauses);
28756 c_name = "linear";
28757 break;
28758 case PRAGMA_OMP_CLAUSE_DEPEND:
28759 clauses = cp_parser_omp_clause_depend (parser, clauses);
28760 c_name = "depend";
28761 break;
28762 case PRAGMA_OMP_CLAUSE_MAP:
28763 clauses = cp_parser_omp_clause_map (parser, clauses);
28764 c_name = "map";
28765 break;
28766 case PRAGMA_OMP_CLAUSE_DEVICE:
28767 clauses = cp_parser_omp_clause_device (parser, clauses,
28768 token->location);
28769 c_name = "device";
28770 break;
28771 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28772 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28773 token->location);
28774 c_name = "dist_schedule";
28775 break;
28776 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28777 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28778 token->location);
28779 c_name = "proc_bind";
28780 break;
28781 case PRAGMA_OMP_CLAUSE_SAFELEN:
28782 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28783 token->location);
28784 c_name = "safelen";
28785 break;
28786 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28787 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28788 token->location);
28789 c_name = "simdlen";
28790 break;
28791 default:
28792 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28793 goto saw_error;
28796 first = false;
28798 if (((mask >> c_kind) & 1) == 0)
28800 /* Remove the invalid clause(s) from the list to avoid
28801 confusing the rest of the compiler. */
28802 clauses = prev;
28803 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28806 saw_error:
28807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28808 if (finish_p)
28809 return finish_omp_clauses (clauses);
28810 return clauses;
28813 /* OpenMP 2.5:
28814 structured-block:
28815 statement
28817 In practice, we're also interested in adding the statement to an
28818 outer node. So it is convenient if we work around the fact that
28819 cp_parser_statement calls add_stmt. */
28821 static unsigned
28822 cp_parser_begin_omp_structured_block (cp_parser *parser)
28824 unsigned save = parser->in_statement;
28826 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28827 This preserves the "not within loop or switch" style error messages
28828 for nonsense cases like
28829 void foo() {
28830 #pragma omp single
28831 break;
28834 if (parser->in_statement)
28835 parser->in_statement = IN_OMP_BLOCK;
28837 return save;
28840 static void
28841 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28843 parser->in_statement = save;
28846 static tree
28847 cp_parser_omp_structured_block (cp_parser *parser)
28849 tree stmt = begin_omp_structured_block ();
28850 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28852 cp_parser_statement (parser, NULL_TREE, false, NULL);
28854 cp_parser_end_omp_structured_block (parser, save);
28855 return finish_omp_structured_block (stmt);
28858 /* OpenMP 2.5:
28859 # pragma omp atomic new-line
28860 expression-stmt
28862 expression-stmt:
28863 x binop= expr | x++ | ++x | x-- | --x
28864 binop:
28865 +, *, -, /, &, ^, |, <<, >>
28867 where x is an lvalue expression with scalar type.
28869 OpenMP 3.1:
28870 # pragma omp atomic new-line
28871 update-stmt
28873 # pragma omp atomic read new-line
28874 read-stmt
28876 # pragma omp atomic write new-line
28877 write-stmt
28879 # pragma omp atomic update new-line
28880 update-stmt
28882 # pragma omp atomic capture new-line
28883 capture-stmt
28885 # pragma omp atomic capture new-line
28886 capture-block
28888 read-stmt:
28889 v = x
28890 write-stmt:
28891 x = expr
28892 update-stmt:
28893 expression-stmt | x = x binop expr
28894 capture-stmt:
28895 v = expression-stmt
28896 capture-block:
28897 { v = x; update-stmt; } | { update-stmt; v = x; }
28899 OpenMP 4.0:
28900 update-stmt:
28901 expression-stmt | x = x binop expr | x = expr binop x
28902 capture-stmt:
28903 v = update-stmt
28904 capture-block:
28905 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28907 where x and v are lvalue expressions with scalar type. */
28909 static void
28910 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28912 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28913 tree rhs1 = NULL_TREE, orig_lhs;
28914 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28915 bool structured_block = false;
28916 bool seq_cst = false;
28918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28921 const char *p = IDENTIFIER_POINTER (id);
28923 if (!strcmp (p, "read"))
28924 code = OMP_ATOMIC_READ;
28925 else if (!strcmp (p, "write"))
28926 code = NOP_EXPR;
28927 else if (!strcmp (p, "update"))
28928 code = OMP_ATOMIC;
28929 else if (!strcmp (p, "capture"))
28930 code = OMP_ATOMIC_CAPTURE_NEW;
28931 else
28932 p = NULL;
28933 if (p)
28934 cp_lexer_consume_token (parser->lexer);
28937 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28939 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28940 const char *p = IDENTIFIER_POINTER (id);
28942 if (!strcmp (p, "seq_cst"))
28944 seq_cst = true;
28945 cp_lexer_consume_token (parser->lexer);
28948 cp_parser_require_pragma_eol (parser, pragma_tok);
28950 switch (code)
28952 case OMP_ATOMIC_READ:
28953 case NOP_EXPR: /* atomic write */
28954 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28955 /*cast_p=*/false, NULL);
28956 if (v == error_mark_node)
28957 goto saw_error;
28958 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28959 goto saw_error;
28960 if (code == NOP_EXPR)
28961 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28962 else
28963 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28964 /*cast_p=*/false, NULL);
28965 if (lhs == error_mark_node)
28966 goto saw_error;
28967 if (code == NOP_EXPR)
28969 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28970 opcode. */
28971 code = OMP_ATOMIC;
28972 rhs = lhs;
28973 lhs = v;
28974 v = NULL_TREE;
28976 goto done;
28977 case OMP_ATOMIC_CAPTURE_NEW:
28978 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28980 cp_lexer_consume_token (parser->lexer);
28981 structured_block = true;
28983 else
28985 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28986 /*cast_p=*/false, NULL);
28987 if (v == error_mark_node)
28988 goto saw_error;
28989 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28990 goto saw_error;
28992 default:
28993 break;
28996 restart:
28997 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28998 /*cast_p=*/false, NULL);
28999 orig_lhs = lhs;
29000 switch (TREE_CODE (lhs))
29002 case ERROR_MARK:
29003 goto saw_error;
29005 case POSTINCREMENT_EXPR:
29006 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29007 code = OMP_ATOMIC_CAPTURE_OLD;
29008 /* FALLTHROUGH */
29009 case PREINCREMENT_EXPR:
29010 lhs = TREE_OPERAND (lhs, 0);
29011 opcode = PLUS_EXPR;
29012 rhs = integer_one_node;
29013 break;
29015 case POSTDECREMENT_EXPR:
29016 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29017 code = OMP_ATOMIC_CAPTURE_OLD;
29018 /* FALLTHROUGH */
29019 case PREDECREMENT_EXPR:
29020 lhs = TREE_OPERAND (lhs, 0);
29021 opcode = MINUS_EXPR;
29022 rhs = integer_one_node;
29023 break;
29025 case COMPOUND_EXPR:
29026 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29027 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29028 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29029 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29030 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29031 (TREE_OPERAND (lhs, 1), 0), 0)))
29032 == BOOLEAN_TYPE)
29033 /* Undo effects of boolean_increment for post {in,de}crement. */
29034 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29035 /* FALLTHRU */
29036 case MODIFY_EXPR:
29037 if (TREE_CODE (lhs) == MODIFY_EXPR
29038 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29040 /* Undo effects of boolean_increment. */
29041 if (integer_onep (TREE_OPERAND (lhs, 1)))
29043 /* This is pre or post increment. */
29044 rhs = TREE_OPERAND (lhs, 1);
29045 lhs = TREE_OPERAND (lhs, 0);
29046 opcode = NOP_EXPR;
29047 if (code == OMP_ATOMIC_CAPTURE_NEW
29048 && !structured_block
29049 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29050 code = OMP_ATOMIC_CAPTURE_OLD;
29051 break;
29054 /* FALLTHRU */
29055 default:
29056 switch (cp_lexer_peek_token (parser->lexer)->type)
29058 case CPP_MULT_EQ:
29059 opcode = MULT_EXPR;
29060 break;
29061 case CPP_DIV_EQ:
29062 opcode = TRUNC_DIV_EXPR;
29063 break;
29064 case CPP_PLUS_EQ:
29065 opcode = PLUS_EXPR;
29066 break;
29067 case CPP_MINUS_EQ:
29068 opcode = MINUS_EXPR;
29069 break;
29070 case CPP_LSHIFT_EQ:
29071 opcode = LSHIFT_EXPR;
29072 break;
29073 case CPP_RSHIFT_EQ:
29074 opcode = RSHIFT_EXPR;
29075 break;
29076 case CPP_AND_EQ:
29077 opcode = BIT_AND_EXPR;
29078 break;
29079 case CPP_OR_EQ:
29080 opcode = BIT_IOR_EXPR;
29081 break;
29082 case CPP_XOR_EQ:
29083 opcode = BIT_XOR_EXPR;
29084 break;
29085 case CPP_EQ:
29086 enum cp_parser_prec oprec;
29087 cp_token *token;
29088 cp_lexer_consume_token (parser->lexer);
29089 cp_parser_parse_tentatively (parser);
29090 rhs1 = cp_parser_simple_cast_expression (parser);
29091 if (rhs1 == error_mark_node)
29093 cp_parser_abort_tentative_parse (parser);
29094 cp_parser_simple_cast_expression (parser);
29095 goto saw_error;
29097 token = cp_lexer_peek_token (parser->lexer);
29098 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29100 cp_parser_abort_tentative_parse (parser);
29101 cp_parser_parse_tentatively (parser);
29102 rhs = cp_parser_binary_expression (parser, false, true,
29103 PREC_NOT_OPERATOR, NULL);
29104 if (rhs == error_mark_node)
29106 cp_parser_abort_tentative_parse (parser);
29107 cp_parser_binary_expression (parser, false, true,
29108 PREC_NOT_OPERATOR, NULL);
29109 goto saw_error;
29111 switch (TREE_CODE (rhs))
29113 case MULT_EXPR:
29114 case TRUNC_DIV_EXPR:
29115 case PLUS_EXPR:
29116 case MINUS_EXPR:
29117 case LSHIFT_EXPR:
29118 case RSHIFT_EXPR:
29119 case BIT_AND_EXPR:
29120 case BIT_IOR_EXPR:
29121 case BIT_XOR_EXPR:
29122 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29124 if (cp_parser_parse_definitely (parser))
29126 opcode = TREE_CODE (rhs);
29127 rhs1 = TREE_OPERAND (rhs, 0);
29128 rhs = TREE_OPERAND (rhs, 1);
29129 goto stmt_done;
29131 else
29132 goto saw_error;
29134 break;
29135 default:
29136 break;
29138 cp_parser_abort_tentative_parse (parser);
29139 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29141 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
29142 if (rhs == error_mark_node)
29143 goto saw_error;
29144 opcode = NOP_EXPR;
29145 rhs1 = NULL_TREE;
29146 goto stmt_done;
29148 cp_parser_error (parser,
29149 "invalid form of %<#pragma omp atomic%>");
29150 goto saw_error;
29152 if (!cp_parser_parse_definitely (parser))
29153 goto saw_error;
29154 switch (token->type)
29156 case CPP_SEMICOLON:
29157 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29159 code = OMP_ATOMIC_CAPTURE_OLD;
29160 v = lhs;
29161 lhs = NULL_TREE;
29162 lhs1 = rhs1;
29163 rhs1 = NULL_TREE;
29164 cp_lexer_consume_token (parser->lexer);
29165 goto restart;
29167 else if (structured_block)
29169 opcode = NOP_EXPR;
29170 rhs = rhs1;
29171 rhs1 = NULL_TREE;
29172 goto stmt_done;
29174 cp_parser_error (parser,
29175 "invalid form of %<#pragma omp atomic%>");
29176 goto saw_error;
29177 case CPP_MULT:
29178 opcode = MULT_EXPR;
29179 break;
29180 case CPP_DIV:
29181 opcode = TRUNC_DIV_EXPR;
29182 break;
29183 case CPP_PLUS:
29184 opcode = PLUS_EXPR;
29185 break;
29186 case CPP_MINUS:
29187 opcode = MINUS_EXPR;
29188 break;
29189 case CPP_LSHIFT:
29190 opcode = LSHIFT_EXPR;
29191 break;
29192 case CPP_RSHIFT:
29193 opcode = RSHIFT_EXPR;
29194 break;
29195 case CPP_AND:
29196 opcode = BIT_AND_EXPR;
29197 break;
29198 case CPP_OR:
29199 opcode = BIT_IOR_EXPR;
29200 break;
29201 case CPP_XOR:
29202 opcode = BIT_XOR_EXPR;
29203 break;
29204 default:
29205 cp_parser_error (parser,
29206 "invalid operator for %<#pragma omp atomic%>");
29207 goto saw_error;
29209 oprec = TOKEN_PRECEDENCE (token);
29210 gcc_assert (oprec != PREC_NOT_OPERATOR);
29211 if (commutative_tree_code (opcode))
29212 oprec = (enum cp_parser_prec) (oprec - 1);
29213 cp_lexer_consume_token (parser->lexer);
29214 rhs = cp_parser_binary_expression (parser, false, false,
29215 oprec, NULL);
29216 if (rhs == error_mark_node)
29217 goto saw_error;
29218 goto stmt_done;
29219 /* FALLTHROUGH */
29220 default:
29221 cp_parser_error (parser,
29222 "invalid operator for %<#pragma omp atomic%>");
29223 goto saw_error;
29225 cp_lexer_consume_token (parser->lexer);
29227 rhs = cp_parser_expression (parser, false, NULL);
29228 if (rhs == error_mark_node)
29229 goto saw_error;
29230 break;
29232 stmt_done:
29233 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29235 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29236 goto saw_error;
29237 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29238 /*cast_p=*/false, NULL);
29239 if (v == error_mark_node)
29240 goto saw_error;
29241 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29242 goto saw_error;
29243 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29244 /*cast_p=*/false, NULL);
29245 if (lhs1 == error_mark_node)
29246 goto saw_error;
29248 if (structured_block)
29250 cp_parser_consume_semicolon_at_end_of_statement (parser);
29251 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29253 done:
29254 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29255 if (!structured_block)
29256 cp_parser_consume_semicolon_at_end_of_statement (parser);
29257 return;
29259 saw_error:
29260 cp_parser_skip_to_end_of_block_or_statement (parser);
29261 if (structured_block)
29263 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29264 cp_lexer_consume_token (parser->lexer);
29265 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29267 cp_parser_skip_to_end_of_block_or_statement (parser);
29268 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29269 cp_lexer_consume_token (parser->lexer);
29275 /* OpenMP 2.5:
29276 # pragma omp barrier new-line */
29278 static void
29279 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29281 cp_parser_require_pragma_eol (parser, pragma_tok);
29282 finish_omp_barrier ();
29285 /* OpenMP 2.5:
29286 # pragma omp critical [(name)] new-line
29287 structured-block */
29289 static tree
29290 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29292 tree stmt, name = NULL;
29294 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29296 cp_lexer_consume_token (parser->lexer);
29298 name = cp_parser_identifier (parser);
29300 if (name == error_mark_node
29301 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29303 /*or_comma=*/false,
29304 /*consume_paren=*/true);
29305 if (name == error_mark_node)
29306 name = NULL;
29308 cp_parser_require_pragma_eol (parser, pragma_tok);
29310 stmt = cp_parser_omp_structured_block (parser);
29311 return c_finish_omp_critical (input_location, stmt, name);
29314 /* OpenMP 2.5:
29315 # pragma omp flush flush-vars[opt] new-line
29317 flush-vars:
29318 ( variable-list ) */
29320 static void
29321 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29323 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29324 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29325 cp_parser_require_pragma_eol (parser, pragma_tok);
29327 finish_omp_flush ();
29330 /* Helper function, to parse omp for increment expression. */
29332 static tree
29333 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29335 tree cond = cp_parser_binary_expression (parser, false, true,
29336 PREC_NOT_OPERATOR, NULL);
29337 if (cond == error_mark_node
29338 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29340 cp_parser_skip_to_end_of_statement (parser);
29341 return error_mark_node;
29344 switch (TREE_CODE (cond))
29346 case GT_EXPR:
29347 case GE_EXPR:
29348 case LT_EXPR:
29349 case LE_EXPR:
29350 break;
29351 case NE_EXPR:
29352 if (code == CILK_SIMD)
29353 break;
29354 /* Fall through: OpenMP disallows NE_EXPR. */
29355 default:
29356 return error_mark_node;
29359 /* If decl is an iterator, preserve LHS and RHS of the relational
29360 expr until finish_omp_for. */
29361 if (decl
29362 && (type_dependent_expression_p (decl)
29363 || CLASS_TYPE_P (TREE_TYPE (decl))))
29364 return cond;
29366 return build_x_binary_op (input_location, TREE_CODE (cond),
29367 TREE_OPERAND (cond, 0), ERROR_MARK,
29368 TREE_OPERAND (cond, 1), ERROR_MARK,
29369 /*overload=*/NULL, tf_warning_or_error);
29372 /* Helper function, to parse omp for increment expression. */
29374 static tree
29375 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29377 cp_token *token = cp_lexer_peek_token (parser->lexer);
29378 enum tree_code op;
29379 tree lhs, rhs;
29380 cp_id_kind idk;
29381 bool decl_first;
29383 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29385 op = (token->type == CPP_PLUS_PLUS
29386 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29387 cp_lexer_consume_token (parser->lexer);
29388 lhs = cp_parser_simple_cast_expression (parser);
29389 if (lhs != decl)
29390 return error_mark_node;
29391 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29394 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29395 if (lhs != decl)
29396 return error_mark_node;
29398 token = cp_lexer_peek_token (parser->lexer);
29399 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29401 op = (token->type == CPP_PLUS_PLUS
29402 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29403 cp_lexer_consume_token (parser->lexer);
29404 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29407 op = cp_parser_assignment_operator_opt (parser);
29408 if (op == ERROR_MARK)
29409 return error_mark_node;
29411 if (op != NOP_EXPR)
29413 rhs = cp_parser_assignment_expression (parser, false, NULL);
29414 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29415 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29418 lhs = cp_parser_binary_expression (parser, false, false,
29419 PREC_ADDITIVE_EXPRESSION, NULL);
29420 token = cp_lexer_peek_token (parser->lexer);
29421 decl_first = lhs == decl;
29422 if (decl_first)
29423 lhs = NULL_TREE;
29424 if (token->type != CPP_PLUS
29425 && token->type != CPP_MINUS)
29426 return error_mark_node;
29430 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29431 cp_lexer_consume_token (parser->lexer);
29432 rhs = cp_parser_binary_expression (parser, false, false,
29433 PREC_ADDITIVE_EXPRESSION, NULL);
29434 token = cp_lexer_peek_token (parser->lexer);
29435 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29437 if (lhs == NULL_TREE)
29439 if (op == PLUS_EXPR)
29440 lhs = rhs;
29441 else
29442 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29443 tf_warning_or_error);
29445 else
29446 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29447 ERROR_MARK, NULL, tf_warning_or_error);
29450 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29452 if (!decl_first)
29454 if (rhs != decl || op == MINUS_EXPR)
29455 return error_mark_node;
29456 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29458 else
29459 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29461 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29464 /* Parse the initialization statement of either an OpenMP for loop or
29465 a Cilk Plus for loop.
29467 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29468 Plus.
29470 Return true if the resulting construct should have an
29471 OMP_CLAUSE_PRIVATE added to it. */
29473 static bool
29474 cp_parser_omp_for_loop_init (cp_parser *parser,
29475 bool parsing_openmp,
29476 tree &this_pre_body,
29477 vec<tree, va_gc> *for_block,
29478 tree &init,
29479 tree &decl,
29480 tree &real_decl)
29482 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29483 return false;
29485 bool add_private_clause = false;
29487 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29489 init-expr:
29490 var = lb
29491 integer-type var = lb
29492 random-access-iterator-type var = lb
29493 pointer-type var = lb
29495 cp_decl_specifier_seq type_specifiers;
29497 /* First, try to parse as an initialized declaration. See
29498 cp_parser_condition, from whence the bulk of this is copied. */
29500 cp_parser_parse_tentatively (parser);
29501 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29502 /*is_trailing_return=*/false,
29503 &type_specifiers);
29504 if (cp_parser_parse_definitely (parser))
29506 /* If parsing a type specifier seq succeeded, then this
29507 MUST be a initialized declaration. */
29508 tree asm_specification, attributes;
29509 cp_declarator *declarator;
29511 declarator = cp_parser_declarator (parser,
29512 CP_PARSER_DECLARATOR_NAMED,
29513 /*ctor_dtor_or_conv_p=*/NULL,
29514 /*parenthesized_p=*/NULL,
29515 /*member_p=*/false);
29516 attributes = cp_parser_attributes_opt (parser);
29517 asm_specification = cp_parser_asm_specification_opt (parser);
29519 if (declarator == cp_error_declarator)
29520 cp_parser_skip_to_end_of_statement (parser);
29522 else
29524 tree pushed_scope, auto_node;
29526 decl = start_decl (declarator, &type_specifiers,
29527 SD_INITIALIZED, attributes,
29528 /*prefix_attributes=*/NULL_TREE,
29529 &pushed_scope);
29531 auto_node = type_uses_auto (TREE_TYPE (decl));
29532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29534 if (cp_lexer_next_token_is (parser->lexer,
29535 CPP_OPEN_PAREN))
29537 if (parsing_openmp)
29538 error ("parenthesized initialization is not allowed in "
29539 "OpenMP %<for%> loop");
29540 else
29541 error ("parenthesized initialization is "
29542 "not allowed in for-loop");
29544 else
29545 /* Trigger an error. */
29546 cp_parser_require (parser, CPP_EQ, RT_EQ);
29548 init = error_mark_node;
29549 cp_parser_skip_to_end_of_statement (parser);
29551 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29552 || type_dependent_expression_p (decl)
29553 || auto_node)
29555 bool is_direct_init, is_non_constant_init;
29557 init = cp_parser_initializer (parser,
29558 &is_direct_init,
29559 &is_non_constant_init);
29561 if (auto_node)
29563 TREE_TYPE (decl)
29564 = do_auto_deduction (TREE_TYPE (decl), init,
29565 auto_node);
29567 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29568 && !type_dependent_expression_p (decl))
29569 goto non_class;
29572 cp_finish_decl (decl, init, !is_non_constant_init,
29573 asm_specification,
29574 LOOKUP_ONLYCONVERTING);
29575 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29577 vec_safe_push (for_block, this_pre_body);
29578 init = NULL_TREE;
29580 else
29581 init = pop_stmt_list (this_pre_body);
29582 this_pre_body = NULL_TREE;
29584 else
29586 /* Consume '='. */
29587 cp_lexer_consume_token (parser->lexer);
29588 init = cp_parser_assignment_expression (parser, false, NULL);
29590 non_class:
29591 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29592 init = error_mark_node;
29593 else
29594 cp_finish_decl (decl, NULL_TREE,
29595 /*init_const_expr_p=*/false,
29596 asm_specification,
29597 LOOKUP_ONLYCONVERTING);
29600 if (pushed_scope)
29601 pop_scope (pushed_scope);
29604 else
29606 cp_id_kind idk;
29607 /* If parsing a type specifier sequence failed, then
29608 this MUST be a simple expression. */
29609 cp_parser_parse_tentatively (parser);
29610 decl = cp_parser_primary_expression (parser, false, false,
29611 false, &idk);
29612 if (!cp_parser_error_occurred (parser)
29613 && decl
29614 && DECL_P (decl)
29615 && CLASS_TYPE_P (TREE_TYPE (decl)))
29617 tree rhs;
29619 cp_parser_parse_definitely (parser);
29620 cp_parser_require (parser, CPP_EQ, RT_EQ);
29621 rhs = cp_parser_assignment_expression (parser, false, NULL);
29622 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29623 decl, NOP_EXPR,
29624 rhs,
29625 tf_warning_or_error));
29626 add_private_clause = true;
29628 else
29630 decl = NULL;
29631 cp_parser_abort_tentative_parse (parser);
29632 init = cp_parser_expression (parser, false, NULL);
29633 if (init)
29635 if (TREE_CODE (init) == MODIFY_EXPR
29636 || TREE_CODE (init) == MODOP_EXPR)
29637 real_decl = TREE_OPERAND (init, 0);
29641 return add_private_clause;
29644 /* Parse the restricted form of the for statement allowed by OpenMP. */
29646 static tree
29647 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29648 tree *cclauses)
29650 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29651 tree real_decl, initv, condv, incrv, declv;
29652 tree this_pre_body, cl;
29653 location_t loc_first;
29654 bool collapse_err = false;
29655 int i, collapse = 1, nbraces = 0;
29656 vec<tree, va_gc> *for_block = make_tree_vector ();
29658 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29659 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29660 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
29662 gcc_assert (collapse >= 1);
29664 declv = make_tree_vec (collapse);
29665 initv = make_tree_vec (collapse);
29666 condv = make_tree_vec (collapse);
29667 incrv = make_tree_vec (collapse);
29669 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29671 for (i = 0; i < collapse; i++)
29673 int bracecount = 0;
29674 bool add_private_clause = false;
29675 location_t loc;
29677 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29679 cp_parser_error (parser, "for statement expected");
29680 return NULL;
29682 loc = cp_lexer_consume_token (parser->lexer)->location;
29684 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29685 return NULL;
29687 init = decl = real_decl = NULL;
29688 this_pre_body = push_stmt_list ();
29690 add_private_clause
29691 |= cp_parser_omp_for_loop_init (parser,
29692 /*parsing_openmp=*/code != CILK_SIMD,
29693 this_pre_body, for_block,
29694 init, decl, real_decl);
29696 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29697 if (this_pre_body)
29699 this_pre_body = pop_stmt_list (this_pre_body);
29700 if (pre_body)
29702 tree t = pre_body;
29703 pre_body = push_stmt_list ();
29704 add_stmt (t);
29705 add_stmt (this_pre_body);
29706 pre_body = pop_stmt_list (pre_body);
29708 else
29709 pre_body = this_pre_body;
29712 if (decl)
29713 real_decl = decl;
29714 if (cclauses != NULL
29715 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29716 && real_decl != NULL_TREE)
29718 tree *c;
29719 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29720 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29721 && OMP_CLAUSE_DECL (*c) == real_decl)
29723 error_at (loc, "iteration variable %qD"
29724 " should not be firstprivate", real_decl);
29725 *c = OMP_CLAUSE_CHAIN (*c);
29727 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29728 && OMP_CLAUSE_DECL (*c) == real_decl)
29730 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29731 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29732 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29733 OMP_CLAUSE_DECL (l) = real_decl;
29734 OMP_CLAUSE_CHAIN (l) = clauses;
29735 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29736 clauses = l;
29737 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29738 CP_OMP_CLAUSE_INFO (*c) = NULL;
29739 add_private_clause = false;
29741 else
29743 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29744 && OMP_CLAUSE_DECL (*c) == real_decl)
29745 add_private_clause = false;
29746 c = &OMP_CLAUSE_CHAIN (*c);
29750 if (add_private_clause)
29752 tree c;
29753 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29755 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29756 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29757 && OMP_CLAUSE_DECL (c) == decl)
29758 break;
29759 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29760 && OMP_CLAUSE_DECL (c) == decl)
29761 error_at (loc, "iteration variable %qD "
29762 "should not be firstprivate",
29763 decl);
29764 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29765 && OMP_CLAUSE_DECL (c) == decl)
29766 error_at (loc, "iteration variable %qD should not be reduction",
29767 decl);
29769 if (c == NULL)
29771 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29772 OMP_CLAUSE_DECL (c) = decl;
29773 c = finish_omp_clauses (c);
29774 if (c)
29776 OMP_CLAUSE_CHAIN (c) = clauses;
29777 clauses = c;
29782 cond = NULL;
29783 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29784 cond = cp_parser_omp_for_cond (parser, decl, code);
29785 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29787 incr = NULL;
29788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29790 /* If decl is an iterator, preserve the operator on decl
29791 until finish_omp_for. */
29792 if (real_decl
29793 && ((processing_template_decl
29794 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29795 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29796 incr = cp_parser_omp_for_incr (parser, real_decl);
29797 else
29798 incr = cp_parser_expression (parser, false, NULL);
29799 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29800 SET_EXPR_LOCATION (incr, input_location);
29803 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29804 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29805 /*or_comma=*/false,
29806 /*consume_paren=*/true);
29808 TREE_VEC_ELT (declv, i) = decl;
29809 TREE_VEC_ELT (initv, i) = init;
29810 TREE_VEC_ELT (condv, i) = cond;
29811 TREE_VEC_ELT (incrv, i) = incr;
29813 if (i == collapse - 1)
29814 break;
29816 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29817 in between the collapsed for loops to be still considered perfectly
29818 nested. Hopefully the final version clarifies this.
29819 For now handle (multiple) {'s and empty statements. */
29820 cp_parser_parse_tentatively (parser);
29823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29824 break;
29825 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29827 cp_lexer_consume_token (parser->lexer);
29828 bracecount++;
29830 else if (bracecount
29831 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29832 cp_lexer_consume_token (parser->lexer);
29833 else
29835 loc = cp_lexer_peek_token (parser->lexer)->location;
29836 error_at (loc, "not enough collapsed for loops");
29837 collapse_err = true;
29838 cp_parser_abort_tentative_parse (parser);
29839 declv = NULL_TREE;
29840 break;
29843 while (1);
29845 if (declv)
29847 cp_parser_parse_definitely (parser);
29848 nbraces += bracecount;
29852 /* Note that we saved the original contents of this flag when we entered
29853 the structured block, and so we don't need to re-save it here. */
29854 if (code == CILK_SIMD)
29855 parser->in_statement = IN_CILK_SIMD_FOR;
29856 else
29857 parser->in_statement = IN_OMP_FOR;
29859 /* Note that the grammar doesn't call for a structured block here,
29860 though the loop as a whole is a structured block. */
29861 body = push_stmt_list ();
29862 cp_parser_statement (parser, NULL_TREE, false, NULL);
29863 body = pop_stmt_list (body);
29865 if (declv == NULL_TREE)
29866 ret = NULL_TREE;
29867 else
29868 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29869 pre_body, clauses);
29871 while (nbraces)
29873 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29875 cp_lexer_consume_token (parser->lexer);
29876 nbraces--;
29878 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29879 cp_lexer_consume_token (parser->lexer);
29880 else
29882 if (!collapse_err)
29884 error_at (cp_lexer_peek_token (parser->lexer)->location,
29885 "collapsed loops not perfectly nested");
29887 collapse_err = true;
29888 cp_parser_statement_seq_opt (parser, NULL);
29889 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29890 break;
29894 while (!for_block->is_empty ())
29895 add_stmt (pop_stmt_list (for_block->pop ()));
29896 release_tree_vector (for_block);
29898 return ret;
29901 /* Helper function for OpenMP parsing, split clauses and call
29902 finish_omp_clauses on each of the set of clauses afterwards. */
29904 static void
29905 cp_omp_split_clauses (location_t loc, enum tree_code code,
29906 omp_clause_mask mask, tree clauses, tree *cclauses)
29908 int i;
29909 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29910 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29911 if (cclauses[i])
29912 cclauses[i] = finish_omp_clauses (cclauses[i]);
29915 /* OpenMP 4.0:
29916 #pragma omp simd simd-clause[optseq] new-line
29917 for-loop */
29919 #define OMP_SIMD_CLAUSE_MASK \
29920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29928 static tree
29929 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29930 char *p_name, omp_clause_mask mask, tree *cclauses)
29932 tree clauses, sb, ret;
29933 unsigned int save;
29934 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29936 strcat (p_name, " simd");
29937 mask |= OMP_SIMD_CLAUSE_MASK;
29938 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29940 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29941 cclauses == NULL);
29942 if (cclauses)
29944 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29945 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29948 sb = begin_omp_structured_block ();
29949 save = cp_parser_begin_omp_structured_block (parser);
29951 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29953 cp_parser_end_omp_structured_block (parser, save);
29954 add_stmt (finish_omp_structured_block (sb));
29956 return ret;
29959 /* OpenMP 2.5:
29960 #pragma omp for for-clause[optseq] new-line
29961 for-loop
29963 OpenMP 4.0:
29964 #pragma omp for simd for-simd-clause[optseq] new-line
29965 for-loop */
29967 #define OMP_FOR_CLAUSE_MASK \
29968 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29977 static tree
29978 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29979 char *p_name, omp_clause_mask mask, tree *cclauses)
29981 tree clauses, sb, ret;
29982 unsigned int save;
29983 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29985 strcat (p_name, " for");
29986 mask |= OMP_FOR_CLAUSE_MASK;
29987 if (cclauses)
29988 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29992 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29993 const char *p = IDENTIFIER_POINTER (id);
29995 if (strcmp (p, "simd") == 0)
29997 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29998 if (cclauses == NULL)
29999 cclauses = cclauses_buf;
30001 cp_lexer_consume_token (parser->lexer);
30002 if (!flag_openmp) /* flag_openmp_simd */
30003 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30004 cclauses);
30005 sb = begin_omp_structured_block ();
30006 save = cp_parser_begin_omp_structured_block (parser);
30007 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30008 cclauses);
30009 cp_parser_end_omp_structured_block (parser, save);
30010 tree body = finish_omp_structured_block (sb);
30011 if (ret == NULL)
30012 return ret;
30013 ret = make_node (OMP_FOR);
30014 TREE_TYPE (ret) = void_type_node;
30015 OMP_FOR_BODY (ret) = body;
30016 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30017 SET_EXPR_LOCATION (ret, loc);
30018 add_stmt (ret);
30019 return ret;
30022 if (!flag_openmp) /* flag_openmp_simd */
30024 cp_parser_require_pragma_eol (parser, pragma_tok);
30025 return NULL_TREE;
30028 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30029 cclauses == NULL);
30030 if (cclauses)
30032 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30033 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30036 sb = begin_omp_structured_block ();
30037 save = cp_parser_begin_omp_structured_block (parser);
30039 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30041 cp_parser_end_omp_structured_block (parser, save);
30042 add_stmt (finish_omp_structured_block (sb));
30044 return ret;
30047 /* OpenMP 2.5:
30048 # pragma omp master new-line
30049 structured-block */
30051 static tree
30052 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30054 cp_parser_require_pragma_eol (parser, pragma_tok);
30055 return c_finish_omp_master (input_location,
30056 cp_parser_omp_structured_block (parser));
30059 /* OpenMP 2.5:
30060 # pragma omp ordered new-line
30061 structured-block */
30063 static tree
30064 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30066 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30067 cp_parser_require_pragma_eol (parser, pragma_tok);
30068 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30071 /* OpenMP 2.5:
30073 section-scope:
30074 { section-sequence }
30076 section-sequence:
30077 section-directive[opt] structured-block
30078 section-sequence section-directive structured-block */
30080 static tree
30081 cp_parser_omp_sections_scope (cp_parser *parser)
30083 tree stmt, substmt;
30084 bool error_suppress = false;
30085 cp_token *tok;
30087 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30088 return NULL_TREE;
30090 stmt = push_stmt_list ();
30092 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30094 substmt = cp_parser_omp_structured_block (parser);
30095 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30096 add_stmt (substmt);
30099 while (1)
30101 tok = cp_lexer_peek_token (parser->lexer);
30102 if (tok->type == CPP_CLOSE_BRACE)
30103 break;
30104 if (tok->type == CPP_EOF)
30105 break;
30107 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30109 cp_lexer_consume_token (parser->lexer);
30110 cp_parser_require_pragma_eol (parser, tok);
30111 error_suppress = false;
30113 else if (!error_suppress)
30115 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30116 error_suppress = true;
30119 substmt = cp_parser_omp_structured_block (parser);
30120 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30121 add_stmt (substmt);
30123 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30125 substmt = pop_stmt_list (stmt);
30127 stmt = make_node (OMP_SECTIONS);
30128 TREE_TYPE (stmt) = void_type_node;
30129 OMP_SECTIONS_BODY (stmt) = substmt;
30131 add_stmt (stmt);
30132 return stmt;
30135 /* OpenMP 2.5:
30136 # pragma omp sections sections-clause[optseq] newline
30137 sections-scope */
30139 #define OMP_SECTIONS_CLAUSE_MASK \
30140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30146 static tree
30147 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30148 char *p_name, omp_clause_mask mask, tree *cclauses)
30150 tree clauses, ret;
30151 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30153 strcat (p_name, " sections");
30154 mask |= OMP_SECTIONS_CLAUSE_MASK;
30155 if (cclauses)
30156 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30158 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30159 cclauses == NULL);
30160 if (cclauses)
30162 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30163 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30166 ret = cp_parser_omp_sections_scope (parser);
30167 if (ret)
30168 OMP_SECTIONS_CLAUSES (ret) = clauses;
30170 return ret;
30173 /* OpenMP 2.5:
30174 # pragma parallel parallel-clause new-line
30175 # pragma parallel for parallel-for-clause new-line
30176 # pragma parallel sections parallel-sections-clause new-line
30178 OpenMP 4.0:
30179 # pragma parallel for simd parallel-for-simd-clause new-line */
30181 #define OMP_PARALLEL_CLAUSE_MASK \
30182 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30192 static tree
30193 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30194 char *p_name, omp_clause_mask mask, tree *cclauses)
30196 tree stmt, clauses, block;
30197 unsigned int save;
30198 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30200 strcat (p_name, " parallel");
30201 mask |= OMP_PARALLEL_CLAUSE_MASK;
30203 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30205 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30206 if (cclauses == NULL)
30207 cclauses = cclauses_buf;
30209 cp_lexer_consume_token (parser->lexer);
30210 if (!flag_openmp) /* flag_openmp_simd */
30211 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30212 block = begin_omp_parallel ();
30213 save = cp_parser_begin_omp_structured_block (parser);
30214 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30215 cp_parser_end_omp_structured_block (parser, save);
30216 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30217 block);
30218 OMP_PARALLEL_COMBINED (stmt) = 1;
30219 return stmt;
30221 else if (cclauses)
30223 error_at (loc, "expected %<for%> after %qs", p_name);
30224 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30225 return NULL_TREE;
30227 else if (!flag_openmp) /* flag_openmp_simd */
30229 cp_parser_require_pragma_eol (parser, pragma_tok);
30230 return NULL_TREE;
30232 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30234 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30235 const char *p = IDENTIFIER_POINTER (id);
30236 if (strcmp (p, "sections") == 0)
30238 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30239 cclauses = cclauses_buf;
30241 cp_lexer_consume_token (parser->lexer);
30242 block = begin_omp_parallel ();
30243 save = cp_parser_begin_omp_structured_block (parser);
30244 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30245 cp_parser_end_omp_structured_block (parser, save);
30246 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30247 block);
30248 OMP_PARALLEL_COMBINED (stmt) = 1;
30249 return stmt;
30253 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30255 block = begin_omp_parallel ();
30256 save = cp_parser_begin_omp_structured_block (parser);
30257 cp_parser_statement (parser, NULL_TREE, false, NULL);
30258 cp_parser_end_omp_structured_block (parser, save);
30259 stmt = finish_omp_parallel (clauses, block);
30260 return stmt;
30263 /* OpenMP 2.5:
30264 # pragma omp single single-clause[optseq] new-line
30265 structured-block */
30267 #define OMP_SINGLE_CLAUSE_MASK \
30268 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30273 static tree
30274 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30276 tree stmt = make_node (OMP_SINGLE);
30277 TREE_TYPE (stmt) = void_type_node;
30279 OMP_SINGLE_CLAUSES (stmt)
30280 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30281 "#pragma omp single", pragma_tok);
30282 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30284 return add_stmt (stmt);
30287 /* OpenMP 3.0:
30288 # pragma omp task task-clause[optseq] new-line
30289 structured-block */
30291 #define OMP_TASK_CLAUSE_MASK \
30292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30302 static tree
30303 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30305 tree clauses, block;
30306 unsigned int save;
30308 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30309 "#pragma omp task", pragma_tok);
30310 block = begin_omp_task ();
30311 save = cp_parser_begin_omp_structured_block (parser);
30312 cp_parser_statement (parser, NULL_TREE, false, NULL);
30313 cp_parser_end_omp_structured_block (parser, save);
30314 return finish_omp_task (clauses, block);
30317 /* OpenMP 3.0:
30318 # pragma omp taskwait new-line */
30320 static void
30321 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30323 cp_parser_require_pragma_eol (parser, pragma_tok);
30324 finish_omp_taskwait ();
30327 /* OpenMP 3.1:
30328 # pragma omp taskyield new-line */
30330 static void
30331 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30333 cp_parser_require_pragma_eol (parser, pragma_tok);
30334 finish_omp_taskyield ();
30337 /* OpenMP 4.0:
30338 # pragma omp taskgroup new-line
30339 structured-block */
30341 static tree
30342 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30344 cp_parser_require_pragma_eol (parser, pragma_tok);
30345 return c_finish_omp_taskgroup (input_location,
30346 cp_parser_omp_structured_block (parser));
30350 /* OpenMP 2.5:
30351 # pragma omp threadprivate (variable-list) */
30353 static void
30354 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30356 tree vars;
30358 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30359 cp_parser_require_pragma_eol (parser, pragma_tok);
30361 finish_omp_threadprivate (vars);
30364 /* OpenMP 4.0:
30365 # pragma omp cancel cancel-clause[optseq] new-line */
30367 #define OMP_CANCEL_CLAUSE_MASK \
30368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30374 static void
30375 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30377 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30378 "#pragma omp cancel", pragma_tok);
30379 finish_omp_cancel (clauses);
30382 /* OpenMP 4.0:
30383 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30385 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30391 static void
30392 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30394 tree clauses;
30395 bool point_seen = false;
30397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30399 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30400 const char *p = IDENTIFIER_POINTER (id);
30402 if (strcmp (p, "point") == 0)
30404 cp_lexer_consume_token (parser->lexer);
30405 point_seen = true;
30408 if (!point_seen)
30410 cp_parser_error (parser, "expected %<point%>");
30411 cp_parser_require_pragma_eol (parser, pragma_tok);
30412 return;
30415 clauses = cp_parser_omp_all_clauses (parser,
30416 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30417 "#pragma omp cancellation point",
30418 pragma_tok);
30419 finish_omp_cancellation_point (clauses);
30422 /* OpenMP 4.0:
30423 #pragma omp distribute distribute-clause[optseq] new-line
30424 for-loop */
30426 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30432 static tree
30433 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30434 char *p_name, omp_clause_mask mask, tree *cclauses)
30436 tree clauses, sb, ret;
30437 unsigned int save;
30438 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30440 strcat (p_name, " distribute");
30441 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30445 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30446 const char *p = IDENTIFIER_POINTER (id);
30447 bool simd = false;
30448 bool parallel = false;
30450 if (strcmp (p, "simd") == 0)
30451 simd = true;
30452 else
30453 parallel = strcmp (p, "parallel") == 0;
30454 if (parallel || simd)
30456 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30457 if (cclauses == NULL)
30458 cclauses = cclauses_buf;
30459 cp_lexer_consume_token (parser->lexer);
30460 if (!flag_openmp) /* flag_openmp_simd */
30462 if (simd)
30463 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30464 cclauses);
30465 else
30466 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30467 cclauses);
30469 sb = begin_omp_structured_block ();
30470 save = cp_parser_begin_omp_structured_block (parser);
30471 if (simd)
30472 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30473 cclauses);
30474 else
30475 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30476 cclauses);
30477 cp_parser_end_omp_structured_block (parser, save);
30478 tree body = finish_omp_structured_block (sb);
30479 if (ret == NULL)
30480 return ret;
30481 ret = make_node (OMP_DISTRIBUTE);
30482 TREE_TYPE (ret) = void_type_node;
30483 OMP_FOR_BODY (ret) = body;
30484 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30485 SET_EXPR_LOCATION (ret, loc);
30486 add_stmt (ret);
30487 return ret;
30490 if (!flag_openmp) /* flag_openmp_simd */
30492 cp_parser_require_pragma_eol (parser, pragma_tok);
30493 return NULL_TREE;
30496 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30497 cclauses == NULL);
30498 if (cclauses)
30500 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30501 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30504 sb = begin_omp_structured_block ();
30505 save = cp_parser_begin_omp_structured_block (parser);
30507 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30509 cp_parser_end_omp_structured_block (parser, save);
30510 add_stmt (finish_omp_structured_block (sb));
30512 return ret;
30515 /* OpenMP 4.0:
30516 # pragma omp teams teams-clause[optseq] new-line
30517 structured-block */
30519 #define OMP_TEAMS_CLAUSE_MASK \
30520 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30528 static tree
30529 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30530 char *p_name, omp_clause_mask mask, tree *cclauses)
30532 tree clauses, sb, ret;
30533 unsigned int save;
30534 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30536 strcat (p_name, " teams");
30537 mask |= OMP_TEAMS_CLAUSE_MASK;
30539 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30541 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30542 const char *p = IDENTIFIER_POINTER (id);
30543 if (strcmp (p, "distribute") == 0)
30545 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30546 if (cclauses == NULL)
30547 cclauses = cclauses_buf;
30549 cp_lexer_consume_token (parser->lexer);
30550 if (!flag_openmp) /* flag_openmp_simd */
30551 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30552 cclauses);
30553 sb = begin_omp_structured_block ();
30554 save = cp_parser_begin_omp_structured_block (parser);
30555 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30556 cclauses);
30557 cp_parser_end_omp_structured_block (parser, save);
30558 tree body = finish_omp_structured_block (sb);
30559 if (ret == NULL)
30560 return ret;
30561 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30562 ret = make_node (OMP_TEAMS);
30563 TREE_TYPE (ret) = void_type_node;
30564 OMP_TEAMS_CLAUSES (ret) = clauses;
30565 OMP_TEAMS_BODY (ret) = body;
30566 return add_stmt (ret);
30569 if (!flag_openmp) /* flag_openmp_simd */
30571 cp_parser_require_pragma_eol (parser, pragma_tok);
30572 return NULL_TREE;
30575 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30576 cclauses == NULL);
30577 if (cclauses)
30579 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30580 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30583 tree stmt = make_node (OMP_TEAMS);
30584 TREE_TYPE (stmt) = void_type_node;
30585 OMP_TEAMS_CLAUSES (stmt) = clauses;
30586 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30588 return add_stmt (stmt);
30591 /* OpenMP 4.0:
30592 # pragma omp target data target-data-clause[optseq] new-line
30593 structured-block */
30595 #define OMP_TARGET_DATA_CLAUSE_MASK \
30596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30600 static tree
30601 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30603 tree stmt = make_node (OMP_TARGET_DATA);
30604 TREE_TYPE (stmt) = void_type_node;
30606 OMP_TARGET_DATA_CLAUSES (stmt)
30607 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30608 "#pragma omp target data", pragma_tok);
30609 keep_next_level (true);
30610 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30612 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30613 return add_stmt (stmt);
30616 /* OpenMP 4.0:
30617 # pragma omp target update target-update-clause[optseq] new-line */
30619 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30620 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30625 static bool
30626 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30627 enum pragma_context context)
30629 if (context == pragma_stmt)
30631 error_at (pragma_tok->location,
30632 "%<#pragma omp target update%> may only be "
30633 "used in compound statements");
30634 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30635 return false;
30638 tree clauses
30639 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30640 "#pragma omp target update", pragma_tok);
30641 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30642 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30644 error_at (pragma_tok->location,
30645 "%<#pragma omp target update must contain at least one "
30646 "%<from%> or %<to%> clauses");
30647 return false;
30650 tree stmt = make_node (OMP_TARGET_UPDATE);
30651 TREE_TYPE (stmt) = void_type_node;
30652 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30653 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30654 add_stmt (stmt);
30655 return false;
30658 /* OpenMP 4.0:
30659 # pragma omp target target-clause[optseq] new-line
30660 structured-block */
30662 #define OMP_TARGET_CLAUSE_MASK \
30663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30667 static bool
30668 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30669 enum pragma_context context)
30671 if (context != pragma_stmt && context != pragma_compound)
30673 cp_parser_error (parser, "expected declaration specifiers");
30674 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30675 return false;
30678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30681 const char *p = IDENTIFIER_POINTER (id);
30683 if (strcmp (p, "teams") == 0)
30685 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30686 char p_name[sizeof ("#pragma omp target teams distribute "
30687 "parallel for simd")];
30689 cp_lexer_consume_token (parser->lexer);
30690 strcpy (p_name, "#pragma omp target");
30691 keep_next_level (true);
30692 if (!flag_openmp) /* flag_openmp_simd */
30693 return cp_parser_omp_teams (parser, pragma_tok, p_name,
30694 OMP_TARGET_CLAUSE_MASK, cclauses);
30695 tree sb = begin_omp_structured_block ();
30696 unsigned save = cp_parser_begin_omp_structured_block (parser);
30697 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30698 OMP_TARGET_CLAUSE_MASK, cclauses);
30699 cp_parser_end_omp_structured_block (parser, save);
30700 tree body = finish_omp_structured_block (sb);
30701 if (ret == NULL)
30702 return ret;
30703 tree stmt = make_node (OMP_TARGET);
30704 TREE_TYPE (stmt) = void_type_node;
30705 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30706 OMP_TARGET_BODY (stmt) = body;
30707 add_stmt (stmt);
30708 return true;
30710 else if (!flag_openmp) /* flag_openmp_simd */
30712 cp_parser_require_pragma_eol (parser, pragma_tok);
30713 return NULL_TREE;
30715 else if (strcmp (p, "data") == 0)
30717 cp_lexer_consume_token (parser->lexer);
30718 cp_parser_omp_target_data (parser, pragma_tok);
30719 return true;
30721 else if (strcmp (p, "update") == 0)
30723 cp_lexer_consume_token (parser->lexer);
30724 return cp_parser_omp_target_update (parser, pragma_tok, context);
30728 tree stmt = make_node (OMP_TARGET);
30729 TREE_TYPE (stmt) = void_type_node;
30731 OMP_TARGET_CLAUSES (stmt)
30732 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30733 "#pragma omp target", pragma_tok);
30734 keep_next_level (true);
30735 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30737 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30738 add_stmt (stmt);
30739 return true;
30742 /* OpenMP 4.0:
30743 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30745 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30753 static void
30754 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30755 enum pragma_context context)
30757 bool first_p = parser->omp_declare_simd == NULL;
30758 cp_omp_declare_simd_data data;
30759 if (first_p)
30761 data.error_seen = false;
30762 data.fndecl_seen = false;
30763 data.tokens = vNULL;
30764 parser->omp_declare_simd = &data;
30766 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30767 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30768 cp_lexer_consume_token (parser->lexer);
30769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30770 parser->omp_declare_simd->error_seen = true;
30771 cp_parser_require_pragma_eol (parser, pragma_tok);
30772 struct cp_token_cache *cp
30773 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30774 parser->omp_declare_simd->tokens.safe_push (cp);
30775 if (first_p)
30777 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30778 cp_parser_pragma (parser, context);
30779 switch (context)
30781 case pragma_external:
30782 cp_parser_declaration (parser);
30783 break;
30784 case pragma_member:
30785 cp_parser_member_declaration (parser);
30786 break;
30787 case pragma_objc_icode:
30788 cp_parser_block_declaration (parser, /*statement_p=*/false);
30789 break;
30790 default:
30791 cp_parser_declaration_statement (parser);
30792 break;
30794 if (parser->omp_declare_simd
30795 && !parser->omp_declare_simd->error_seen
30796 && !parser->omp_declare_simd->fndecl_seen)
30797 error_at (pragma_tok->location,
30798 "%<#pragma omp declare simd%> not immediately followed by "
30799 "function declaration or definition");
30800 data.tokens.release ();
30801 parser->omp_declare_simd = NULL;
30805 /* Finalize #pragma omp declare simd clauses after direct declarator has
30806 been parsed, and put that into "omp declare simd" attribute. */
30808 static tree
30809 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30811 struct cp_token_cache *ce;
30812 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30813 int i;
30815 if (!data->error_seen && data->fndecl_seen)
30817 error ("%<#pragma omp declare simd%> not immediately followed by "
30818 "a single function declaration or definition");
30819 data->error_seen = true;
30820 return attrs;
30822 if (data->error_seen)
30823 return attrs;
30825 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30827 tree c, cl;
30829 cp_parser_push_lexer_for_tokens (parser, ce);
30830 parser->lexer->in_pragma = true;
30831 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30832 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30833 cp_lexer_consume_token (parser->lexer);
30834 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30835 "#pragma omp declare simd", pragma_tok);
30836 cp_parser_pop_lexer (parser);
30837 if (cl)
30838 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30839 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30840 TREE_CHAIN (c) = attrs;
30841 if (processing_template_decl)
30842 ATTR_IS_DEPENDENT (c) = 1;
30843 attrs = c;
30846 data->fndecl_seen = true;
30847 return attrs;
30851 /* OpenMP 4.0:
30852 # pragma omp declare target new-line
30853 declarations and definitions
30854 # pragma omp end declare target new-line */
30856 static void
30857 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30859 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30860 scope_chain->omp_declare_target_attribute++;
30863 static void
30864 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30866 const char *p = "";
30867 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30869 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30870 p = IDENTIFIER_POINTER (id);
30872 if (strcmp (p, "declare") == 0)
30874 cp_lexer_consume_token (parser->lexer);
30875 p = "";
30876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30878 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30879 p = IDENTIFIER_POINTER (id);
30881 if (strcmp (p, "target") == 0)
30882 cp_lexer_consume_token (parser->lexer);
30883 else
30885 cp_parser_error (parser, "expected %<target%>");
30886 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30887 return;
30890 else
30892 cp_parser_error (parser, "expected %<declare%>");
30893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30894 return;
30896 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30897 if (!scope_chain->omp_declare_target_attribute)
30898 error_at (pragma_tok->location,
30899 "%<#pragma omp end declare target%> without corresponding "
30900 "%<#pragma omp declare target%>");
30901 else
30902 scope_chain->omp_declare_target_attribute--;
30905 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30906 expression and optional initializer clause of
30907 #pragma omp declare reduction. We store the expression(s) as
30908 either 3, 6 or 7 special statements inside of the artificial function's
30909 body. The first two statements are DECL_EXPRs for the artificial
30910 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30911 expression that uses those variables.
30912 If there was any INITIALIZER clause, this is followed by further statements,
30913 the fourth and fifth statements are DECL_EXPRs for the artificial
30914 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30915 constructor variant (first token after open paren is not omp_priv),
30916 then the sixth statement is a statement with the function call expression
30917 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30918 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30919 to initialize the OMP_PRIV artificial variable and there is seventh
30920 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30922 static bool
30923 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30925 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30926 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30927 type = TREE_TYPE (type);
30928 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30929 DECL_ARTIFICIAL (omp_out) = 1;
30930 pushdecl (omp_out);
30931 add_decl_expr (omp_out);
30932 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30933 DECL_ARTIFICIAL (omp_in) = 1;
30934 pushdecl (omp_in);
30935 add_decl_expr (omp_in);
30936 tree combiner;
30937 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30939 keep_next_level (true);
30940 tree block = begin_omp_structured_block ();
30941 combiner = cp_parser_expression (parser, false, NULL);
30942 finish_expr_stmt (combiner);
30943 block = finish_omp_structured_block (block);
30944 add_stmt (block);
30946 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30947 return false;
30949 const char *p = "";
30950 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30952 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30953 p = IDENTIFIER_POINTER (id);
30956 if (strcmp (p, "initializer") == 0)
30958 cp_lexer_consume_token (parser->lexer);
30959 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30960 return false;
30962 p = "";
30963 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30965 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30966 p = IDENTIFIER_POINTER (id);
30969 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30970 DECL_ARTIFICIAL (omp_priv) = 1;
30971 pushdecl (omp_priv);
30972 add_decl_expr (omp_priv);
30973 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30974 DECL_ARTIFICIAL (omp_orig) = 1;
30975 pushdecl (omp_orig);
30976 add_decl_expr (omp_orig);
30978 keep_next_level (true);
30979 block = begin_omp_structured_block ();
30981 bool ctor = false;
30982 if (strcmp (p, "omp_priv") == 0)
30984 bool is_direct_init, is_non_constant_init;
30985 ctor = true;
30986 cp_lexer_consume_token (parser->lexer);
30987 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30988 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30989 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30990 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30991 == CPP_CLOSE_PAREN
30992 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30993 == CPP_CLOSE_PAREN))
30995 finish_omp_structured_block (block);
30996 error ("invalid initializer clause");
30997 return false;
30999 initializer = cp_parser_initializer (parser, &is_direct_init,
31000 &is_non_constant_init);
31001 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31002 NULL_TREE, LOOKUP_ONLYCONVERTING);
31004 else
31006 cp_parser_parse_tentatively (parser);
31007 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31008 /*check_dependency_p=*/true,
31009 /*template_p=*/NULL,
31010 /*declarator_p=*/false,
31011 /*optional_p=*/false);
31012 vec<tree, va_gc> *args;
31013 if (fn_name == error_mark_node
31014 || cp_parser_error_occurred (parser)
31015 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31016 || ((args = cp_parser_parenthesized_expression_list
31017 (parser, non_attr, /*cast_p=*/false,
31018 /*allow_expansion_p=*/true,
31019 /*non_constant_p=*/NULL)),
31020 cp_parser_error_occurred (parser)))
31022 finish_omp_structured_block (block);
31023 cp_parser_abort_tentative_parse (parser);
31024 cp_parser_error (parser, "expected id-expression (arguments)");
31025 return false;
31027 unsigned int i;
31028 tree arg;
31029 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31030 if (arg == omp_priv
31031 || (TREE_CODE (arg) == ADDR_EXPR
31032 && TREE_OPERAND (arg, 0) == omp_priv))
31033 break;
31034 cp_parser_abort_tentative_parse (parser);
31035 if (arg == NULL_TREE)
31036 error ("one of the initializer call arguments should be %<omp_priv%>"
31037 " or %<&omp_priv%>");
31038 initializer = cp_parser_postfix_expression (parser, false, false, false,
31039 false, NULL);
31040 finish_expr_stmt (initializer);
31043 block = finish_omp_structured_block (block);
31044 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31045 finish_expr_stmt (block);
31047 if (ctor)
31048 add_decl_expr (omp_orig);
31050 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31051 return false;
31054 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31055 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31057 return true;
31060 /* OpenMP 4.0
31061 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31062 initializer-clause[opt] new-line
31064 initializer-clause:
31065 initializer (omp_priv initializer)
31066 initializer (function-name (argument-list)) */
31068 static void
31069 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31070 enum pragma_context)
31072 vec<tree> types = vNULL;
31073 enum tree_code reduc_code = ERROR_MARK;
31074 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31075 unsigned int i;
31076 cp_token *first_token;
31077 cp_token_cache *cp;
31078 int errs;
31080 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31081 goto fail;
31083 switch (cp_lexer_peek_token (parser->lexer)->type)
31085 case CPP_PLUS:
31086 reduc_code = PLUS_EXPR;
31087 break;
31088 case CPP_MULT:
31089 reduc_code = MULT_EXPR;
31090 break;
31091 case CPP_MINUS:
31092 reduc_code = MINUS_EXPR;
31093 break;
31094 case CPP_AND:
31095 reduc_code = BIT_AND_EXPR;
31096 break;
31097 case CPP_XOR:
31098 reduc_code = BIT_XOR_EXPR;
31099 break;
31100 case CPP_OR:
31101 reduc_code = BIT_IOR_EXPR;
31102 break;
31103 case CPP_AND_AND:
31104 reduc_code = TRUTH_ANDIF_EXPR;
31105 break;
31106 case CPP_OR_OR:
31107 reduc_code = TRUTH_ORIF_EXPR;
31108 break;
31109 case CPP_NAME:
31110 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31111 break;
31112 default:
31113 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31114 "%<|%>, %<&&%>, %<||%> or identifier");
31115 goto fail;
31118 if (reduc_code != ERROR_MARK)
31119 cp_lexer_consume_token (parser->lexer);
31121 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31122 if (reduc_id == error_mark_node)
31123 goto fail;
31125 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31126 goto fail;
31128 /* Types may not be defined in declare reduction type list. */
31129 const char *saved_message;
31130 saved_message = parser->type_definition_forbidden_message;
31131 parser->type_definition_forbidden_message
31132 = G_("types may not be defined in declare reduction type list");
31133 bool saved_colon_corrects_to_scope_p;
31134 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31135 parser->colon_corrects_to_scope_p = false;
31136 bool saved_colon_doesnt_start_class_def_p;
31137 saved_colon_doesnt_start_class_def_p
31138 = parser->colon_doesnt_start_class_def_p;
31139 parser->colon_doesnt_start_class_def_p = true;
31141 while (true)
31143 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31144 type = cp_parser_type_id (parser);
31145 if (type == error_mark_node)
31147 else if (ARITHMETIC_TYPE_P (type)
31148 && (orig_reduc_id == NULL_TREE
31149 || (TREE_CODE (type) != COMPLEX_TYPE
31150 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31151 "min") == 0
31152 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31153 "max") == 0))))
31154 error_at (loc, "predeclared arithmetic type %qT in "
31155 "%<#pragma omp declare reduction%>", type);
31156 else if (TREE_CODE (type) == FUNCTION_TYPE
31157 || TREE_CODE (type) == METHOD_TYPE
31158 || TREE_CODE (type) == ARRAY_TYPE)
31159 error_at (loc, "function or array type %qT in "
31160 "%<#pragma omp declare reduction%>", type);
31161 else if (TREE_CODE (type) == REFERENCE_TYPE)
31162 error_at (loc, "reference type %qT in "
31163 "%<#pragma omp declare reduction%>", type);
31164 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31165 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31166 "%<#pragma omp declare reduction%>", type);
31167 else
31168 types.safe_push (type);
31170 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31171 cp_lexer_consume_token (parser->lexer);
31172 else
31173 break;
31176 /* Restore the saved message. */
31177 parser->type_definition_forbidden_message = saved_message;
31178 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31179 parser->colon_doesnt_start_class_def_p
31180 = saved_colon_doesnt_start_class_def_p;
31182 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31183 || types.is_empty ())
31185 fail:
31186 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31187 types.release ();
31188 return;
31191 first_token = cp_lexer_peek_token (parser->lexer);
31192 cp = NULL;
31193 errs = errorcount;
31194 FOR_EACH_VEC_ELT (types, i, type)
31196 tree fntype
31197 = build_function_type_list (void_type_node,
31198 cp_build_reference_type (type, false),
31199 NULL_TREE);
31200 tree this_reduc_id = reduc_id;
31201 if (!dependent_type_p (type))
31202 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31203 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31204 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31205 DECL_ARTIFICIAL (fndecl) = 1;
31206 DECL_EXTERNAL (fndecl) = 1;
31207 DECL_DECLARED_INLINE_P (fndecl) = 1;
31208 DECL_IGNORED_P (fndecl) = 1;
31209 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31210 DECL_ATTRIBUTES (fndecl)
31211 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31212 DECL_ATTRIBUTES (fndecl));
31213 if (processing_template_decl)
31214 fndecl = push_template_decl (fndecl);
31215 bool block_scope = false;
31216 tree block = NULL_TREE;
31217 if (current_function_decl)
31219 block_scope = true;
31220 DECL_CONTEXT (fndecl) = global_namespace;
31221 if (!processing_template_decl)
31222 pushdecl (fndecl);
31224 else if (current_class_type)
31226 if (cp == NULL)
31228 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31229 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31230 cp_lexer_consume_token (parser->lexer);
31231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31232 goto fail;
31233 cp = cp_token_cache_new (first_token,
31234 cp_lexer_peek_nth_token (parser->lexer,
31235 2));
31237 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31238 finish_member_declaration (fndecl);
31239 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31240 DECL_PENDING_INLINE_P (fndecl) = 1;
31241 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31242 continue;
31244 else
31246 DECL_CONTEXT (fndecl) = current_namespace;
31247 pushdecl (fndecl);
31249 if (!block_scope)
31250 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31251 else
31252 block = begin_omp_structured_block ();
31253 if (cp)
31255 cp_parser_push_lexer_for_tokens (parser, cp);
31256 parser->lexer->in_pragma = true;
31258 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31260 if (!block_scope)
31261 finish_function (0);
31262 else
31263 DECL_CONTEXT (fndecl) = current_function_decl;
31264 if (cp)
31265 cp_parser_pop_lexer (parser);
31266 goto fail;
31268 if (cp)
31269 cp_parser_pop_lexer (parser);
31270 if (!block_scope)
31271 finish_function (0);
31272 else
31274 DECL_CONTEXT (fndecl) = current_function_decl;
31275 block = finish_omp_structured_block (block);
31276 if (TREE_CODE (block) == BIND_EXPR)
31277 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31278 else if (TREE_CODE (block) == STATEMENT_LIST)
31279 DECL_SAVED_TREE (fndecl) = block;
31280 if (processing_template_decl)
31281 add_decl_expr (fndecl);
31283 cp_check_omp_declare_reduction (fndecl);
31284 if (cp == NULL && types.length () > 1)
31285 cp = cp_token_cache_new (first_token,
31286 cp_lexer_peek_nth_token (parser->lexer, 2));
31287 if (errs != errorcount)
31288 break;
31291 cp_parser_require_pragma_eol (parser, pragma_tok);
31292 types.release ();
31295 /* OpenMP 4.0
31296 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31297 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31298 initializer-clause[opt] new-line
31299 #pragma omp declare target new-line */
31301 static void
31302 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31303 enum pragma_context context)
31305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31307 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31308 const char *p = IDENTIFIER_POINTER (id);
31310 if (strcmp (p, "simd") == 0)
31312 cp_lexer_consume_token (parser->lexer);
31313 cp_parser_omp_declare_simd (parser, pragma_tok,
31314 context);
31315 return;
31317 cp_ensure_no_omp_declare_simd (parser);
31318 if (strcmp (p, "reduction") == 0)
31320 cp_lexer_consume_token (parser->lexer);
31321 cp_parser_omp_declare_reduction (parser, pragma_tok,
31322 context);
31323 return;
31325 if (!flag_openmp) /* flag_openmp_simd */
31327 cp_parser_require_pragma_eol (parser, pragma_tok);
31328 return;
31330 if (strcmp (p, "target") == 0)
31332 cp_lexer_consume_token (parser->lexer);
31333 cp_parser_omp_declare_target (parser, pragma_tok);
31334 return;
31337 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31338 "or %<target%>");
31339 cp_parser_require_pragma_eol (parser, pragma_tok);
31342 /* Main entry point to OpenMP statement pragmas. */
31344 static void
31345 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31347 tree stmt;
31348 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31349 omp_clause_mask mask (0);
31351 switch (pragma_tok->pragma_kind)
31353 case PRAGMA_OMP_ATOMIC:
31354 cp_parser_omp_atomic (parser, pragma_tok);
31355 return;
31356 case PRAGMA_OMP_CRITICAL:
31357 stmt = cp_parser_omp_critical (parser, pragma_tok);
31358 break;
31359 case PRAGMA_OMP_DISTRIBUTE:
31360 strcpy (p_name, "#pragma omp");
31361 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31362 break;
31363 case PRAGMA_OMP_FOR:
31364 strcpy (p_name, "#pragma omp");
31365 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31366 break;
31367 case PRAGMA_OMP_MASTER:
31368 stmt = cp_parser_omp_master (parser, pragma_tok);
31369 break;
31370 case PRAGMA_OMP_ORDERED:
31371 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31372 break;
31373 case PRAGMA_OMP_PARALLEL:
31374 strcpy (p_name, "#pragma omp");
31375 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31376 break;
31377 case PRAGMA_OMP_SECTIONS:
31378 strcpy (p_name, "#pragma omp");
31379 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31380 break;
31381 case PRAGMA_OMP_SIMD:
31382 strcpy (p_name, "#pragma omp");
31383 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31384 break;
31385 case PRAGMA_OMP_SINGLE:
31386 stmt = cp_parser_omp_single (parser, pragma_tok);
31387 break;
31388 case PRAGMA_OMP_TASK:
31389 stmt = cp_parser_omp_task (parser, pragma_tok);
31390 break;
31391 case PRAGMA_OMP_TASKGROUP:
31392 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31393 break;
31394 case PRAGMA_OMP_TEAMS:
31395 strcpy (p_name, "#pragma omp");
31396 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31397 break;
31398 default:
31399 gcc_unreachable ();
31402 if (stmt)
31403 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31406 /* Transactional Memory parsing routines. */
31408 /* Parse a transaction attribute.
31410 txn-attribute:
31411 attribute
31412 [ [ identifier ] ]
31414 ??? Simplify this when C++0x bracket attributes are
31415 implemented properly. */
31417 static tree
31418 cp_parser_txn_attribute_opt (cp_parser *parser)
31420 cp_token *token;
31421 tree attr_name, attr = NULL;
31423 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31424 return cp_parser_attributes_opt (parser);
31426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31427 return NULL_TREE;
31428 cp_lexer_consume_token (parser->lexer);
31429 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31430 goto error1;
31432 token = cp_lexer_peek_token (parser->lexer);
31433 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31435 token = cp_lexer_consume_token (parser->lexer);
31437 attr_name = (token->type == CPP_KEYWORD
31438 /* For keywords, use the canonical spelling,
31439 not the parsed identifier. */
31440 ? ridpointers[(int) token->keyword]
31441 : token->u.value);
31442 attr = build_tree_list (attr_name, NULL_TREE);
31444 else
31445 cp_parser_error (parser, "expected identifier");
31447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31448 error1:
31449 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31450 return attr;
31453 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31455 transaction-statement:
31456 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31457 compound-statement
31458 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31461 static tree
31462 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31464 unsigned char old_in = parser->in_transaction;
31465 unsigned char this_in = 1, new_in;
31466 cp_token *token;
31467 tree stmt, attrs, noex;
31469 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31470 || keyword == RID_TRANSACTION_RELAXED);
31471 token = cp_parser_require_keyword (parser, keyword,
31472 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31473 : RT_TRANSACTION_RELAXED));
31474 gcc_assert (token != NULL);
31476 if (keyword == RID_TRANSACTION_RELAXED)
31477 this_in |= TM_STMT_ATTR_RELAXED;
31478 else
31480 attrs = cp_parser_txn_attribute_opt (parser);
31481 if (attrs)
31482 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31485 /* Parse a noexcept specification. */
31486 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31488 /* Keep track if we're in the lexical scope of an outer transaction. */
31489 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31491 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31493 parser->in_transaction = new_in;
31494 cp_parser_compound_statement (parser, NULL, false, false);
31495 parser->in_transaction = old_in;
31497 finish_transaction_stmt (stmt, NULL, this_in, noex);
31499 return stmt;
31502 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31504 transaction-expression:
31505 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31506 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31509 static tree
31510 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31512 unsigned char old_in = parser->in_transaction;
31513 unsigned char this_in = 1;
31514 cp_token *token;
31515 tree expr, noex;
31516 bool noex_expr;
31518 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31519 || keyword == RID_TRANSACTION_RELAXED);
31521 if (!flag_tm)
31522 error (keyword == RID_TRANSACTION_RELAXED
31523 ? G_("%<__transaction_relaxed%> without transactional memory "
31524 "support enabled")
31525 : G_("%<__transaction_atomic%> without transactional memory "
31526 "support enabled"));
31528 token = cp_parser_require_keyword (parser, keyword,
31529 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31530 : RT_TRANSACTION_RELAXED));
31531 gcc_assert (token != NULL);
31533 if (keyword == RID_TRANSACTION_RELAXED)
31534 this_in |= TM_STMT_ATTR_RELAXED;
31536 /* Set this early. This might mean that we allow transaction_cancel in
31537 an expression that we find out later actually has to be a constexpr.
31538 However, we expect that cxx_constant_value will be able to deal with
31539 this; also, if the noexcept has no constexpr, then what we parse next
31540 really is a transaction's body. */
31541 parser->in_transaction = this_in;
31543 /* Parse a noexcept specification. */
31544 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31545 true);
31547 if (!noex || !noex_expr
31548 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31550 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31552 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31553 expr = finish_parenthesized_expr (expr);
31555 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31557 else
31559 /* The only expression that is available got parsed for the noexcept
31560 already. noexcept is true then. */
31561 expr = noex;
31562 noex = boolean_true_node;
31565 expr = build_transaction_expr (token->location, expr, this_in, noex);
31566 parser->in_transaction = old_in;
31568 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31569 return error_mark_node;
31571 return (flag_tm ? expr : error_mark_node);
31574 /* Parse a function-transaction-block.
31576 function-transaction-block:
31577 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31578 function-body
31579 __transaction_atomic txn-attribute[opt] function-try-block
31580 __transaction_relaxed ctor-initializer[opt] function-body
31581 __transaction_relaxed function-try-block
31584 static bool
31585 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31587 unsigned char old_in = parser->in_transaction;
31588 unsigned char new_in = 1;
31589 tree compound_stmt, stmt, attrs;
31590 bool ctor_initializer_p;
31591 cp_token *token;
31593 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31594 || keyword == RID_TRANSACTION_RELAXED);
31595 token = cp_parser_require_keyword (parser, keyword,
31596 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31597 : RT_TRANSACTION_RELAXED));
31598 gcc_assert (token != NULL);
31600 if (keyword == RID_TRANSACTION_RELAXED)
31601 new_in |= TM_STMT_ATTR_RELAXED;
31602 else
31604 attrs = cp_parser_txn_attribute_opt (parser);
31605 if (attrs)
31606 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31609 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31611 parser->in_transaction = new_in;
31613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31614 ctor_initializer_p = cp_parser_function_try_block (parser);
31615 else
31616 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31617 (parser, /*in_function_try_block=*/false);
31619 parser->in_transaction = old_in;
31621 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31623 return ctor_initializer_p;
31626 /* Parse a __transaction_cancel statement.
31628 cancel-statement:
31629 __transaction_cancel txn-attribute[opt] ;
31630 __transaction_cancel txn-attribute[opt] throw-expression ;
31632 ??? Cancel and throw is not yet implemented. */
31634 static tree
31635 cp_parser_transaction_cancel (cp_parser *parser)
31637 cp_token *token;
31638 bool is_outer = false;
31639 tree stmt, attrs;
31641 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31642 RT_TRANSACTION_CANCEL);
31643 gcc_assert (token != NULL);
31645 attrs = cp_parser_txn_attribute_opt (parser);
31646 if (attrs)
31647 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31649 /* ??? Parse cancel-and-throw here. */
31651 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31653 if (!flag_tm)
31655 error_at (token->location, "%<__transaction_cancel%> without "
31656 "transactional memory support enabled");
31657 return error_mark_node;
31659 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31661 error_at (token->location, "%<__transaction_cancel%> within a "
31662 "%<__transaction_relaxed%>");
31663 return error_mark_node;
31665 else if (is_outer)
31667 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31668 && !is_tm_may_cancel_outer (current_function_decl))
31670 error_at (token->location, "outer %<__transaction_cancel%> not "
31671 "within outer %<__transaction_atomic%>");
31672 error_at (token->location,
31673 " or a %<transaction_may_cancel_outer%> function");
31674 return error_mark_node;
31677 else if (parser->in_transaction == 0)
31679 error_at (token->location, "%<__transaction_cancel%> not within "
31680 "%<__transaction_atomic%>");
31681 return error_mark_node;
31684 stmt = build_tm_abort_call (token->location, is_outer);
31685 add_stmt (stmt);
31687 return stmt;
31690 /* The parser. */
31692 static GTY (()) cp_parser *the_parser;
31695 /* Special handling for the first token or line in the file. The first
31696 thing in the file might be #pragma GCC pch_preprocess, which loads a
31697 PCH file, which is a GC collection point. So we need to handle this
31698 first pragma without benefit of an existing lexer structure.
31700 Always returns one token to the caller in *FIRST_TOKEN. This is
31701 either the true first token of the file, or the first token after
31702 the initial pragma. */
31704 static void
31705 cp_parser_initial_pragma (cp_token *first_token)
31707 tree name = NULL;
31709 cp_lexer_get_preprocessor_token (NULL, first_token);
31710 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31711 return;
31713 cp_lexer_get_preprocessor_token (NULL, first_token);
31714 if (first_token->type == CPP_STRING)
31716 name = first_token->u.value;
31718 cp_lexer_get_preprocessor_token (NULL, first_token);
31719 if (first_token->type != CPP_PRAGMA_EOL)
31720 error_at (first_token->location,
31721 "junk at end of %<#pragma GCC pch_preprocess%>");
31723 else
31724 error_at (first_token->location, "expected string literal");
31726 /* Skip to the end of the pragma. */
31727 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31728 cp_lexer_get_preprocessor_token (NULL, first_token);
31730 /* Now actually load the PCH file. */
31731 if (name)
31732 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31734 /* Read one more token to return to our caller. We have to do this
31735 after reading the PCH file in, since its pointers have to be
31736 live. */
31737 cp_lexer_get_preprocessor_token (NULL, first_token);
31740 /* Normal parsing of a pragma token. Here we can (and must) use the
31741 regular lexer. */
31743 static bool
31744 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31746 cp_token *pragma_tok;
31747 unsigned int id;
31749 pragma_tok = cp_lexer_consume_token (parser->lexer);
31750 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31751 parser->lexer->in_pragma = true;
31753 id = pragma_tok->pragma_kind;
31754 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31755 cp_ensure_no_omp_declare_simd (parser);
31756 switch (id)
31758 case PRAGMA_GCC_PCH_PREPROCESS:
31759 error_at (pragma_tok->location,
31760 "%<#pragma GCC pch_preprocess%> must be first");
31761 break;
31763 case PRAGMA_OMP_BARRIER:
31764 switch (context)
31766 case pragma_compound:
31767 cp_parser_omp_barrier (parser, pragma_tok);
31768 return false;
31769 case pragma_stmt:
31770 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31771 "used in compound statements");
31772 break;
31773 default:
31774 goto bad_stmt;
31776 break;
31778 case PRAGMA_OMP_FLUSH:
31779 switch (context)
31781 case pragma_compound:
31782 cp_parser_omp_flush (parser, pragma_tok);
31783 return false;
31784 case pragma_stmt:
31785 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31786 "used in compound statements");
31787 break;
31788 default:
31789 goto bad_stmt;
31791 break;
31793 case PRAGMA_OMP_TASKWAIT:
31794 switch (context)
31796 case pragma_compound:
31797 cp_parser_omp_taskwait (parser, pragma_tok);
31798 return false;
31799 case pragma_stmt:
31800 error_at (pragma_tok->location,
31801 "%<#pragma omp taskwait%> may only be "
31802 "used in compound statements");
31803 break;
31804 default:
31805 goto bad_stmt;
31807 break;
31809 case PRAGMA_OMP_TASKYIELD:
31810 switch (context)
31812 case pragma_compound:
31813 cp_parser_omp_taskyield (parser, pragma_tok);
31814 return false;
31815 case pragma_stmt:
31816 error_at (pragma_tok->location,
31817 "%<#pragma omp taskyield%> may only be "
31818 "used in compound statements");
31819 break;
31820 default:
31821 goto bad_stmt;
31823 break;
31825 case PRAGMA_OMP_CANCEL:
31826 switch (context)
31828 case pragma_compound:
31829 cp_parser_omp_cancel (parser, pragma_tok);
31830 return false;
31831 case pragma_stmt:
31832 error_at (pragma_tok->location,
31833 "%<#pragma omp cancel%> may only be "
31834 "used in compound statements");
31835 break;
31836 default:
31837 goto bad_stmt;
31839 break;
31841 case PRAGMA_OMP_CANCELLATION_POINT:
31842 switch (context)
31844 case pragma_compound:
31845 cp_parser_omp_cancellation_point (parser, pragma_tok);
31846 return false;
31847 case pragma_stmt:
31848 error_at (pragma_tok->location,
31849 "%<#pragma omp cancellation point%> may only be "
31850 "used in compound statements");
31851 break;
31852 default:
31853 goto bad_stmt;
31855 break;
31857 case PRAGMA_OMP_THREADPRIVATE:
31858 cp_parser_omp_threadprivate (parser, pragma_tok);
31859 return false;
31861 case PRAGMA_OMP_DECLARE_REDUCTION:
31862 cp_parser_omp_declare (parser, pragma_tok, context);
31863 return false;
31865 case PRAGMA_OMP_ATOMIC:
31866 case PRAGMA_OMP_CRITICAL:
31867 case PRAGMA_OMP_DISTRIBUTE:
31868 case PRAGMA_OMP_FOR:
31869 case PRAGMA_OMP_MASTER:
31870 case PRAGMA_OMP_ORDERED:
31871 case PRAGMA_OMP_PARALLEL:
31872 case PRAGMA_OMP_SECTIONS:
31873 case PRAGMA_OMP_SIMD:
31874 case PRAGMA_OMP_SINGLE:
31875 case PRAGMA_OMP_TASK:
31876 case PRAGMA_OMP_TASKGROUP:
31877 case PRAGMA_OMP_TEAMS:
31878 if (context != pragma_stmt && context != pragma_compound)
31879 goto bad_stmt;
31880 cp_parser_omp_construct (parser, pragma_tok);
31881 return true;
31883 case PRAGMA_OMP_TARGET:
31884 return cp_parser_omp_target (parser, pragma_tok, context);
31886 case PRAGMA_OMP_END_DECLARE_TARGET:
31887 cp_parser_omp_end_declare_target (parser, pragma_tok);
31888 return false;
31890 case PRAGMA_OMP_SECTION:
31891 error_at (pragma_tok->location,
31892 "%<#pragma omp section%> may only be used in "
31893 "%<#pragma omp sections%> construct");
31894 break;
31896 case PRAGMA_IVDEP:
31898 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31899 cp_token *tok;
31900 tok = cp_lexer_peek_token (the_parser->lexer);
31901 if (tok->type != CPP_KEYWORD
31902 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31903 && tok->keyword != RID_DO))
31905 cp_parser_error (parser, "for, while or do statement expected");
31906 return false;
31908 cp_parser_iteration_statement (parser, true);
31909 return true;
31912 case PRAGMA_CILK_SIMD:
31913 if (context == pragma_external)
31915 error_at (pragma_tok->location,
31916 "%<#pragma simd%> must be inside a function");
31917 break;
31919 cp_parser_cilk_simd (parser, pragma_tok);
31920 return true;
31922 default:
31923 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31924 c_invoke_pragma_handler (id);
31925 break;
31927 bad_stmt:
31928 cp_parser_error (parser, "expected declaration specifiers");
31929 break;
31932 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31933 return false;
31936 /* The interface the pragma parsers have to the lexer. */
31938 enum cpp_ttype
31939 pragma_lex (tree *value)
31941 cp_token *tok;
31942 enum cpp_ttype ret;
31944 tok = cp_lexer_peek_token (the_parser->lexer);
31946 ret = tok->type;
31947 *value = tok->u.value;
31949 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31950 ret = CPP_EOF;
31951 else if (ret == CPP_STRING)
31952 *value = cp_parser_string_literal (the_parser, false, false);
31953 else
31955 cp_lexer_consume_token (the_parser->lexer);
31956 if (ret == CPP_KEYWORD)
31957 ret = CPP_NAME;
31960 return ret;
31964 /* External interface. */
31966 /* Parse one entire translation unit. */
31968 void
31969 c_parse_file (void)
31971 static bool already_called = false;
31973 if (already_called)
31975 sorry ("inter-module optimizations not implemented for C++");
31976 return;
31978 already_called = true;
31980 the_parser = cp_parser_new ();
31981 push_deferring_access_checks (flag_access_control
31982 ? dk_no_deferred : dk_no_check);
31983 cp_parser_translation_unit (the_parser);
31984 the_parser = NULL;
31987 /* Parses the Cilk Plus #pragma simd vectorlength clause:
31988 Syntax:
31989 vectorlength ( constant-expression ) */
31991 static tree
31992 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses)
31994 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31995 tree expr;
31996 /* The vectorlength clause behaves exactly like OpenMP's safelen
31997 clause. Thus, vectorlength is represented as OMP 4.0
31998 safelen. */
31999 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", loc);
32001 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32002 return error_mark_node;
32004 expr = cp_parser_constant_expression (parser, false, NULL);
32005 expr = maybe_constant_value (expr);
32007 if (TREE_CONSTANT (expr)
32008 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32009 error_at (loc, "vectorlength must be a power of 2");
32010 else if (expr != error_mark_node)
32012 tree c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32013 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32014 OMP_CLAUSE_CHAIN (c) = clauses;
32015 clauses = c;
32018 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32019 return error_mark_node;
32020 return clauses;
32023 /* Handles the Cilk Plus #pragma simd linear clause.
32024 Syntax:
32025 linear ( simd-linear-variable-list )
32027 simd-linear-variable-list:
32028 simd-linear-variable
32029 simd-linear-variable-list , simd-linear-variable
32031 simd-linear-variable:
32032 id-expression
32033 id-expression : simd-linear-step
32035 simd-linear-step:
32036 conditional-expression */
32038 static tree
32039 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32041 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32043 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32044 return clauses;
32045 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32047 cp_parser_error (parser, "expected identifier");
32048 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32049 return error_mark_node;
32052 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32053 parser->colon_corrects_to_scope_p = false;
32054 while (1)
32056 cp_token *token = cp_lexer_peek_token (parser->lexer);
32057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32059 cp_parser_error (parser, "expected variable-name");
32060 clauses = error_mark_node;
32061 break;
32064 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32065 false, false);
32066 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32067 token->location);
32068 if (decl == error_mark_node)
32070 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32071 token->location);
32072 clauses = error_mark_node;
32074 else
32076 tree e = NULL_TREE;
32077 tree step_size = integer_one_node;
32079 /* If present, parse the linear step. Otherwise, assume the default
32080 value of 1. */
32081 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32083 cp_lexer_consume_token (parser->lexer);
32085 e = cp_parser_assignment_expression (parser, false, NULL);
32086 e = maybe_constant_value (e);
32088 if (e == error_mark_node)
32090 /* If an error has occurred, then the whole pragma is
32091 considered ill-formed. Thus, no reason to keep
32092 parsing. */
32093 clauses = error_mark_node;
32094 break;
32096 else if (type_dependent_expression_p (e)
32097 || value_dependent_expression_p (e)
32098 || (TREE_TYPE (e)
32099 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32100 && (TREE_CONSTANT (e)
32101 || DECL_P (e))))
32102 step_size = e;
32103 else
32104 cp_parser_error (parser,
32105 "step size must be an integer constant "
32106 "expression or an integer variable");
32109 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32110 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32111 OMP_CLAUSE_DECL (l) = decl;
32112 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32113 OMP_CLAUSE_CHAIN (l) = clauses;
32114 clauses = l;
32116 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32117 cp_lexer_consume_token (parser->lexer);
32118 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32119 break;
32120 else
32122 error_at (cp_lexer_peek_token (parser->lexer)->location,
32123 "expected %<,%> or %<)%> after %qE", decl);
32124 clauses = error_mark_node;
32125 break;
32128 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32129 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32130 return clauses;
32133 /* Returns the name of the next clause. If the clause is not
32134 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32135 token is not consumed. Otherwise, the appropriate enum from the
32136 pragma_simd_clause is returned and the token is consumed. */
32138 static pragma_cilk_clause
32139 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32141 pragma_cilk_clause clause_type;
32142 cp_token *token = cp_lexer_peek_token (parser->lexer);
32144 if (token->keyword == RID_PRIVATE)
32145 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32146 else if (!token->u.value || token->type != CPP_NAME)
32147 return PRAGMA_CILK_CLAUSE_NONE;
32148 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32149 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32150 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32151 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32152 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32153 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32154 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32155 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32156 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32157 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32158 else
32159 return PRAGMA_CILK_CLAUSE_NONE;
32161 cp_lexer_consume_token (parser->lexer);
32162 return clause_type;
32165 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32167 static tree
32168 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32170 tree clauses = NULL_TREE;
32172 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32173 && clauses != error_mark_node)
32175 pragma_cilk_clause c_kind;
32176 c_kind = cp_parser_cilk_simd_clause_name (parser);
32177 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32178 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses);
32179 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32180 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32181 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32182 /* Use the OpenMP 4.0 equivalent function. */
32183 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32184 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32185 /* Use the OpenMP 4.0 equivalent function. */
32186 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32187 clauses);
32188 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32189 /* Use the OMP 4.0 equivalent function. */
32190 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32191 clauses);
32192 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32193 /* Use the OMP 4.0 equivalent function. */
32194 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32195 else
32197 clauses = error_mark_node;
32198 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32199 break;
32203 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32205 if (clauses == error_mark_node)
32206 return error_mark_node;
32207 else
32208 return c_finish_cilk_clauses (clauses);
32211 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32213 static void
32214 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32216 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32218 if (clauses == error_mark_node)
32219 return;
32221 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32223 error_at (cp_lexer_peek_token (parser->lexer)->location,
32224 "for statement expected");
32225 return;
32228 tree sb = begin_omp_structured_block ();
32229 int save = cp_parser_begin_omp_structured_block (parser);
32230 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32231 if (ret)
32232 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32233 cp_parser_end_omp_structured_block (parser, save);
32234 add_stmt (finish_omp_structured_block (sb));
32235 return;
32238 /* Create an identifier for a generic parameter type (a synthesized
32239 template parameter implied by `auto' or a concept identifier). */
32241 static GTY(()) int generic_parm_count;
32242 static tree
32243 make_generic_type_name ()
32245 char buf[32];
32246 sprintf (buf, "auto:%d", ++generic_parm_count);
32247 return get_identifier (buf);
32250 /* Predicate that behaves as is_auto_or_concept but matches the parent
32251 node of the generic type rather than the generic type itself. This
32252 allows for type transformation in add_implicit_template_parms. */
32254 static inline bool
32255 tree_type_is_auto_or_concept (const_tree t)
32257 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32260 // Return the template decl being called or evaluated as part of the
32261 // constraint check.
32263 // TODO: This is a bit of a hack. When we finish the template parameter
32264 // the constraint is just a call expression, but we don't have the full
32265 // context that we used to build that call expression. Since we're going
32266 // to be comparing declarations, it would helpful to have that. This
32267 // means we'll have to make the TREE_TYPE of the parameter node a pair
32268 // containing the context (the TYPE_DECL) and the constraint.
32269 static tree
32270 get_concept_from_constraint (tree t)
32272 gcc_assert (TREE_CODE (t) == CALL_EXPR);
32273 tree fn = CALL_EXPR_FN (t);
32274 tree ovl = TREE_OPERAND (fn, 0);
32275 tree tmpl = OVL_FUNCTION (ovl);
32276 return DECL_TEMPLATE_RESULT (tmpl);
32279 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32280 (creating a new template parameter list if necessary). Returns the newly
32281 created template type parm. */
32283 tree
32284 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
32286 gcc_assert (current_binding_level->kind == sk_function_parms);
32288 // Before committing to modifying any scope, if we're in an implicit
32289 // template scope, and we're trying to synthesize a constrained
32290 // parameter, try to find a previous parameter with the same name.
32291 if (parser->implicit_template_scope && constr)
32293 tree t = parser->implicit_template_parms;
32294 while (t)
32296 tree c = get_concept_from_constraint (TREE_TYPE (t));
32297 if (c == DECL_SIZE_UNIT (constr))
32298 return TREE_VALUE (t);
32299 t = TREE_CHAIN (t);
32304 /* We are either continuing a function template that already contains implicit
32305 template parameters, creating a new fully-implicit function template, or
32306 extending an existing explicit function template with implicit template
32307 parameters. */
32309 cp_binding_level *const entry_scope = current_binding_level;
32311 bool become_template = false;
32312 cp_binding_level *parent_scope = 0;
32314 if (parser->implicit_template_scope)
32316 gcc_assert (parser->implicit_template_parms);
32318 current_binding_level = parser->implicit_template_scope;
32320 else
32322 /* Roll back to the existing template parameter scope (in the case of
32323 extending an explicit function template) or introduce a new template
32324 parameter scope ahead of the function parameter scope (or class scope
32325 in the case of out-of-line member definitions). The function scope is
32326 added back after template parameter synthesis below. */
32328 cp_binding_level *scope = entry_scope;
32330 while (scope->kind == sk_function_parms)
32332 parent_scope = scope;
32333 scope = scope->level_chain;
32335 if (current_class_type && !LAMBDA_TYPE_P (current_class_type)
32336 && parser->num_classes_being_defined == 0)
32337 while (scope->kind == sk_class)
32339 parent_scope = scope;
32340 scope = scope->level_chain;
32343 current_binding_level = scope;
32345 if (scope->kind != sk_template_parms)
32347 /* Introduce a new template parameter list for implicit template
32348 parameters. */
32350 become_template = true;
32352 parser->implicit_template_scope
32353 = begin_scope (sk_template_parms, NULL);
32355 ++processing_template_decl;
32357 parser->fully_implicit_function_template_p = true;
32358 ++parser->num_template_parameter_lists;
32360 else
32362 /* Synthesize implicit template parameters at the end of the explicit
32363 template parameter list. */
32365 gcc_assert (current_template_parms);
32367 parser->implicit_template_scope = scope;
32369 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32370 parser->implicit_template_parms
32371 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32375 /* Synthesize a new template parameter and track the current template
32376 parameter chain with implicit_template_parms. */
32378 tree synth_id = make_generic_type_name ();
32379 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32380 synth_id);
32382 // Attach the constraint to the parm before processing.
32383 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
32384 TREE_TYPE (node) = constr;
32386 tree new_parm
32387 = process_template_parm (parser->implicit_template_parms,
32388 input_location,
32389 node,
32390 /*non_type=*/false,
32391 /*param_pack=*/false);
32394 if (parser->implicit_template_parms)
32395 parser->implicit_template_parms
32396 = TREE_CHAIN (parser->implicit_template_parms);
32397 else
32398 parser->implicit_template_parms = new_parm;
32400 tree new_type = TREE_TYPE (getdecls ());
32402 /* If creating a fully implicit function template, start the new implicit
32403 template parameter list with this synthesized type, otherwise grow the
32404 current template parameter list. */
32406 if (become_template)
32408 parent_scope->level_chain = current_binding_level;
32410 tree new_parms = make_tree_vec (1);
32411 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32412 current_template_parms = tree_cons (size_int (processing_template_decl),
32413 new_parms, current_template_parms);
32415 else
32417 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32418 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32419 new_parms = grow_tree_vec_stat (new_parms, new_parm_idx + 1);
32420 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32423 current_binding_level = entry_scope;
32425 return new_type;
32428 /* Finish the declaration of a fully implicit function template. Such a
32429 template has no explicit template parameter list so has not been through the
32430 normal template head and tail processing. synthesize_implicit_template_parm
32431 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32432 provided if the declaration is a class member such that its template
32433 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32434 form is returned. Otherwise NULL_TREE is returned. */
32436 tree
32437 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32439 gcc_assert (parser->fully_implicit_function_template_p);
32441 if (member_decl_opt && member_decl_opt != error_mark_node
32442 && DECL_VIRTUAL_P (member_decl_opt))
32444 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32445 "implicit templates may not be %<virtual%>");
32446 DECL_VIRTUAL_P (member_decl_opt) = false;
32449 if (member_decl_opt)
32450 member_decl_opt = finish_member_template_decl (member_decl_opt);
32451 end_template_decl ();
32453 parser->fully_implicit_function_template_p = false;
32454 --parser->num_template_parameter_lists;
32456 return member_decl_opt;
32459 #include "gt-cp-parser.h"