gcc/cp:
[official-gcc.git] / gcc / cp / parser.c
blobd55f5f9c7869059fff281143093bd0956485c52b
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"
45 /* The lexer. */
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 static cp_token eof_token =
52 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57 NIC_NONE,
58 /* floating-point literal */
59 NIC_FLOAT,
60 /* %<this%> */
61 NIC_THIS,
62 /* %<__FUNCTION__%> */
63 NIC_FUNC_NAME,
64 /* %<__PRETTY_FUNCTION__%> */
65 NIC_PRETTY_FUNC,
66 /* %<__func__%> */
67 NIC_C99_FUNC,
68 /* "%<va_arg%> */
69 NIC_VA_ARG,
70 /* a cast */
71 NIC_CAST,
72 /* %<typeid%> operator */
73 NIC_TYPEID,
74 /* non-constant compound literals */
75 NIC_NCC,
76 /* a function call */
77 NIC_FUNC_CALL,
78 /* an increment */
79 NIC_INC,
80 /* an decrement */
81 NIC_DEC,
82 /* an array reference */
83 NIC_ARRAY_REF,
84 /* %<->%> */
85 NIC_ARROW,
86 /* %<.%> */
87 NIC_POINT,
88 /* the address of a label */
89 NIC_ADDR_LABEL,
90 /* %<*%> */
91 NIC_STAR,
92 /* %<&%> */
93 NIC_ADDR,
94 /* %<++%> */
95 NIC_PREINCREMENT,
96 /* %<--%> */
97 NIC_PREDECREMENT,
98 /* %<new%> */
99 NIC_NEW,
100 /* %<delete%> */
101 NIC_DEL,
102 /* calls to overloaded operators */
103 NIC_OVERLOADED,
104 /* an assignment */
105 NIC_ASSIGNMENT,
106 /* a comma operator */
107 NIC_COMMA,
108 /* a call to a constructor */
109 NIC_CONSTRUCTOR,
110 /* a transaction expression */
111 NIC_TRANSACTION
112 } non_integral_constant;
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error {
116 /* NULL */
117 NLE_NULL,
118 /* is not a type */
119 NLE_TYPE,
120 /* is not a class or namespace */
121 NLE_CXX98,
122 /* is not a class, namespace, or enumeration */
123 NLE_NOT_CXX98
124 } name_lookup_error;
126 /* The various kinds of required token */
127 typedef enum required_token {
128 RT_NONE,
129 RT_SEMICOLON, /* ';' */
130 RT_OPEN_PAREN, /* '(' */
131 RT_CLOSE_BRACE, /* '}' */
132 RT_OPEN_BRACE, /* '{' */
133 RT_CLOSE_SQUARE, /* ']' */
134 RT_OPEN_SQUARE, /* '[' */
135 RT_COMMA, /* ',' */
136 RT_SCOPE, /* '::' */
137 RT_LESS, /* '<' */
138 RT_GREATER, /* '>' */
139 RT_EQ, /* '=' */
140 RT_ELLIPSIS, /* '...' */
141 RT_MULT, /* '*' */
142 RT_COMPL, /* '~' */
143 RT_COLON, /* ':' */
144 RT_COLON_SCOPE, /* ':' or '::' */
145 RT_CLOSE_PAREN, /* ')' */
146 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147 RT_PRAGMA_EOL, /* end of line */
148 RT_NAME, /* identifier */
150 /* The type is CPP_KEYWORD */
151 RT_NEW, /* new */
152 RT_DELETE, /* delete */
153 RT_RETURN, /* return */
154 RT_WHILE, /* while */
155 RT_EXTERN, /* extern */
156 RT_STATIC_ASSERT, /* static_assert */
157 RT_DECLTYPE, /* decltype */
158 RT_OPERATOR, /* operator */
159 RT_CLASS, /* class */
160 RT_TEMPLATE, /* template */
161 RT_NAMESPACE, /* namespace */
162 RT_USING, /* using */
163 RT_ASM, /* asm */
164 RT_TRY, /* try */
165 RT_CATCH, /* catch */
166 RT_THROW, /* throw */
167 RT_LABEL, /* __label__ */
168 RT_AT_TRY, /* @try */
169 RT_AT_SYNCHRONIZED, /* @synchronized */
170 RT_AT_THROW, /* @throw */
172 RT_SELECT, /* selection-statement */
173 RT_INTERATION, /* iteration-statement */
174 RT_JUMP, /* jump-statement */
175 RT_CLASS_KEY, /* class-key */
176 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179 RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 } required_token;
182 /* Prototypes. */
184 static cp_lexer *cp_lexer_new_main
185 (void);
186 static cp_lexer *cp_lexer_new_from_tokens
187 (cp_token_cache *tokens);
188 static void cp_lexer_destroy
189 (cp_lexer *);
190 static int cp_lexer_saving_tokens
191 (const cp_lexer *);
192 static cp_token *cp_lexer_token_at
193 (cp_lexer *, cp_token_position);
194 static void cp_lexer_get_preprocessor_token
195 (cp_lexer *, cp_token *);
196 static inline cp_token *cp_lexer_peek_token
197 (cp_lexer *);
198 static cp_token *cp_lexer_peek_nth_token
199 (cp_lexer *, size_t);
200 static inline bool cp_lexer_next_token_is
201 (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_not
203 (cp_lexer *, enum cpp_ttype);
204 static bool cp_lexer_next_token_is_keyword
205 (cp_lexer *, enum rid);
206 static cp_token *cp_lexer_consume_token
207 (cp_lexer *);
208 static void cp_lexer_purge_token
209 (cp_lexer *);
210 static void cp_lexer_purge_tokens_after
211 (cp_lexer *, cp_token_position);
212 static void cp_lexer_save_tokens
213 (cp_lexer *);
214 static void cp_lexer_commit_tokens
215 (cp_lexer *);
216 static void cp_lexer_rollback_tokens
217 (cp_lexer *);
218 static void cp_lexer_print_token
219 (FILE *, cp_token *);
220 static inline bool cp_lexer_debugging_p
221 (cp_lexer *);
222 static void cp_lexer_start_debugging
223 (cp_lexer *) ATTRIBUTE_UNUSED;
224 static void cp_lexer_stop_debugging
225 (cp_lexer *) ATTRIBUTE_UNUSED;
227 static cp_token_cache *cp_token_cache_new
228 (cp_token *, cp_token *);
230 static void cp_parser_initial_pragma
231 (cp_token *);
233 static tree cp_literal_operator_id
234 (const char *);
236 static bool cp_parser_omp_declare_reduction_exprs
237 (tree, cp_parser *);
239 /* Manifest constants. */
240 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
241 #define CP_SAVED_TOKEN_STACK 5
243 /* Variables. */
245 /* The stream to which debugging output should be written. */
246 static FILE *cp_lexer_debug_stream;
248 /* Nonzero if we are parsing an unevaluated operand: an operand to
249 sizeof, typeof, or alignof. */
250 int cp_unevaluated_operand;
252 /* Dump up to NUM tokens in BUFFER to FILE starting with token
253 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
254 first token in BUFFER. If NUM is 0, dump all the tokens. If
255 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
256 highlighted by surrounding it in [[ ]]. */
258 static void
259 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
260 cp_token *start_token, unsigned num,
261 cp_token *curr_token)
263 unsigned i, nprinted;
264 cp_token *token;
265 bool do_print;
267 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
269 if (buffer == NULL)
270 return;
272 if (num == 0)
273 num = buffer->length ();
275 if (start_token == NULL)
276 start_token = buffer->address ();
278 if (start_token > buffer->address ())
280 cp_lexer_print_token (file, &(*buffer)[0]);
281 fprintf (file, " ... ");
284 do_print = false;
285 nprinted = 0;
286 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
288 if (token == start_token)
289 do_print = true;
291 if (!do_print)
292 continue;
294 nprinted++;
295 if (token == curr_token)
296 fprintf (file, "[[");
298 cp_lexer_print_token (file, token);
300 if (token == curr_token)
301 fprintf (file, "]]");
303 switch (token->type)
305 case CPP_SEMICOLON:
306 case CPP_OPEN_BRACE:
307 case CPP_CLOSE_BRACE:
308 case CPP_EOF:
309 fputc ('\n', file);
310 break;
312 default:
313 fputc (' ', file);
317 if (i == num && i < buffer->length ())
319 fprintf (file, " ... ");
320 cp_lexer_print_token (file, &buffer->last ());
323 fprintf (file, "\n");
327 /* Dump all tokens in BUFFER to stderr. */
329 void
330 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
332 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
335 DEBUG_FUNCTION void
336 debug (vec<cp_token, va_gc> &ref)
338 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
341 DEBUG_FUNCTION void
342 debug (vec<cp_token, va_gc> *ptr)
344 if (ptr)
345 debug (*ptr);
346 else
347 fprintf (stderr, "<nil>\n");
351 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
352 description for T. */
354 static void
355 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
357 if (t)
359 fprintf (file, "%s: ", desc);
360 print_node_brief (file, "", t, 0);
365 /* Dump parser context C to FILE. */
367 static void
368 cp_debug_print_context (FILE *file, cp_parser_context *c)
370 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
371 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
372 print_node_brief (file, "", c->object_type, 0);
373 fprintf (file, "}\n");
377 /* Print the stack of parsing contexts to FILE starting with FIRST. */
379 static void
380 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
382 unsigned i;
383 cp_parser_context *c;
385 fprintf (file, "Parsing context stack:\n");
386 for (i = 0, c = first; c; c = c->next, i++)
388 fprintf (file, "\t#%u: ", i);
389 cp_debug_print_context (file, c);
394 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
396 static void
397 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
399 if (flag)
400 fprintf (file, "%s: true\n", desc);
404 /* Print an unparsed function entry UF to FILE. */
406 static void
407 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
409 unsigned i;
410 cp_default_arg_entry *default_arg_fn;
411 tree fn;
413 fprintf (file, "\tFunctions with default args:\n");
414 for (i = 0;
415 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
416 i++)
418 fprintf (file, "\t\tClass type: ");
419 print_node_brief (file, "", default_arg_fn->class_type, 0);
420 fprintf (file, "\t\tDeclaration: ");
421 print_node_brief (file, "", default_arg_fn->decl, 0);
422 fprintf (file, "\n");
425 fprintf (file, "\n\tFunctions with definitions that require "
426 "post-processing\n\t\t");
427 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
429 print_node_brief (file, "", fn, 0);
430 fprintf (file, " ");
432 fprintf (file, "\n");
434 fprintf (file, "\n\tNon-static data members with initializers that require "
435 "post-processing\n\t\t");
436 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
438 print_node_brief (file, "", fn, 0);
439 fprintf (file, " ");
441 fprintf (file, "\n");
445 /* Print the stack of unparsed member functions S to FILE. */
447 static void
448 cp_debug_print_unparsed_queues (FILE *file,
449 vec<cp_unparsed_functions_entry, va_gc> *s)
451 unsigned i;
452 cp_unparsed_functions_entry *uf;
454 fprintf (file, "Unparsed functions\n");
455 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
457 fprintf (file, "#%u:\n", i);
458 cp_debug_print_unparsed_function (file, uf);
463 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
464 the given PARSER. If FILE is NULL, the output is printed on stderr. */
466 static void
467 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
469 cp_token *next_token, *first_token, *start_token;
471 if (file == NULL)
472 file = stderr;
474 next_token = parser->lexer->next_token;
475 first_token = parser->lexer->buffer->address ();
476 start_token = (next_token > first_token + window_size / 2)
477 ? next_token - window_size / 2
478 : first_token;
479 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
480 next_token);
484 /* Dump debugging information for the given PARSER. If FILE is NULL,
485 the output is printed on stderr. */
487 void
488 cp_debug_parser (FILE *file, cp_parser *parser)
490 const size_t window_size = 20;
491 cp_token *token;
492 expanded_location eloc;
494 if (file == NULL)
495 file = stderr;
497 fprintf (file, "Parser state\n\n");
498 fprintf (file, "Number of tokens: %u\n",
499 vec_safe_length (parser->lexer->buffer));
500 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
501 cp_debug_print_tree_if_set (file, "Object scope",
502 parser->object_scope);
503 cp_debug_print_tree_if_set (file, "Qualifying scope",
504 parser->qualifying_scope);
505 cp_debug_print_context_stack (file, parser->context);
506 cp_debug_print_flag (file, "Allow GNU extensions",
507 parser->allow_gnu_extensions_p);
508 cp_debug_print_flag (file, "'>' token is greater-than",
509 parser->greater_than_is_operator_p);
510 cp_debug_print_flag (file, "Default args allowed in current "
511 "parameter list", parser->default_arg_ok_p);
512 cp_debug_print_flag (file, "Parsing integral constant-expression",
513 parser->integral_constant_expression_p);
514 cp_debug_print_flag (file, "Allow non-constant expression in current "
515 "constant-expression",
516 parser->allow_non_integral_constant_expression_p);
517 cp_debug_print_flag (file, "Seen non-constant expression",
518 parser->non_integral_constant_expression_p);
519 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
520 "current context",
521 parser->local_variables_forbidden_p);
522 cp_debug_print_flag (file, "In unbraced linkage specification",
523 parser->in_unbraced_linkage_specification_p);
524 cp_debug_print_flag (file, "Parsing a declarator",
525 parser->in_declarator_p);
526 cp_debug_print_flag (file, "In template argument list",
527 parser->in_template_argument_list_p);
528 cp_debug_print_flag (file, "Parsing an iteration statement",
529 parser->in_statement & IN_ITERATION_STMT);
530 cp_debug_print_flag (file, "Parsing a switch statement",
531 parser->in_statement & IN_SWITCH_STMT);
532 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
533 parser->in_statement & IN_OMP_BLOCK);
534 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
535 parser->in_statement & IN_OMP_FOR);
536 cp_debug_print_flag (file, "Parsing an if statement",
537 parser->in_statement & IN_IF_STMT);
538 cp_debug_print_flag (file, "Parsing a type-id in an expression "
539 "context", parser->in_type_id_in_expr_p);
540 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
541 parser->implicit_extern_c);
542 cp_debug_print_flag (file, "String expressions should be translated "
543 "to execution character set",
544 parser->translate_strings_p);
545 cp_debug_print_flag (file, "Parsing function body outside of a "
546 "local class", parser->in_function_body);
547 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
548 parser->colon_corrects_to_scope_p);
549 cp_debug_print_flag (file, "Colon doesn't start a class definition",
550 parser->colon_doesnt_start_class_def_p);
551 if (parser->type_definition_forbidden_message)
552 fprintf (file, "Error message for forbidden type definitions: %s\n",
553 parser->type_definition_forbidden_message);
554 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
555 fprintf (file, "Number of class definitions in progress: %u\n",
556 parser->num_classes_being_defined);
557 fprintf (file, "Number of template parameter lists for the current "
558 "declaration: %u\n", parser->num_template_parameter_lists);
559 cp_debug_parser_tokens (file, parser, window_size);
560 token = parser->lexer->next_token;
561 fprintf (file, "Next token to parse:\n");
562 fprintf (file, "\tToken: ");
563 cp_lexer_print_token (file, token);
564 eloc = expand_location (token->location);
565 fprintf (file, "\n\tFile: %s\n", eloc.file);
566 fprintf (file, "\tLine: %d\n", eloc.line);
567 fprintf (file, "\tColumn: %d\n", eloc.column);
570 DEBUG_FUNCTION void
571 debug (cp_parser &ref)
573 cp_debug_parser (stderr, &ref);
576 DEBUG_FUNCTION void
577 debug (cp_parser *ptr)
579 if (ptr)
580 debug (*ptr);
581 else
582 fprintf (stderr, "<nil>\n");
585 /* Allocate memory for a new lexer object and return it. */
587 static cp_lexer *
588 cp_lexer_alloc (void)
590 cp_lexer *lexer;
592 c_common_no_more_pch ();
594 /* Allocate the memory. */
595 lexer = ggc_alloc_cleared_cp_lexer ();
597 /* Initially we are not debugging. */
598 lexer->debugging_p = false;
600 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
602 /* Create the buffer. */
603 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
605 return lexer;
609 /* Create a new main C++ lexer, the lexer that gets tokens from the
610 preprocessor. */
612 static cp_lexer *
613 cp_lexer_new_main (void)
615 cp_lexer *lexer;
616 cp_token token;
618 /* It's possible that parsing the first pragma will load a PCH file,
619 which is a GC collection point. So we have to do that before
620 allocating any memory. */
621 cp_parser_initial_pragma (&token);
623 lexer = cp_lexer_alloc ();
625 /* Put the first token in the buffer. */
626 lexer->buffer->quick_push (token);
628 /* Get the remaining tokens from the preprocessor. */
629 while (token.type != CPP_EOF)
631 cp_lexer_get_preprocessor_token (lexer, &token);
632 vec_safe_push (lexer->buffer, token);
635 lexer->last_token = lexer->buffer->address ()
636 + lexer->buffer->length ()
637 - 1;
638 lexer->next_token = lexer->buffer->length ()
639 ? lexer->buffer->address ()
640 : &eof_token;
642 /* Subsequent preprocessor diagnostics should use compiler
643 diagnostic functions to get the compiler source location. */
644 done_lexing = true;
646 gcc_assert (!lexer->next_token->purged_p);
647 return lexer;
650 /* Create a new lexer whose token stream is primed with the tokens in
651 CACHE. When these tokens are exhausted, no new tokens will be read. */
653 static cp_lexer *
654 cp_lexer_new_from_tokens (cp_token_cache *cache)
656 cp_token *first = cache->first;
657 cp_token *last = cache->last;
658 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
660 /* We do not own the buffer. */
661 lexer->buffer = NULL;
662 lexer->next_token = first == last ? &eof_token : first;
663 lexer->last_token = last;
665 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
667 /* Initially we are not debugging. */
668 lexer->debugging_p = false;
670 gcc_assert (!lexer->next_token->purged_p);
671 return lexer;
674 /* Frees all resources associated with LEXER. */
676 static void
677 cp_lexer_destroy (cp_lexer *lexer)
679 vec_free (lexer->buffer);
680 lexer->saved_tokens.release ();
681 ggc_free (lexer);
684 /* Returns nonzero if debugging information should be output. */
686 static inline bool
687 cp_lexer_debugging_p (cp_lexer *lexer)
689 return lexer->debugging_p;
693 static inline cp_token_position
694 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
696 gcc_assert (!previous_p || lexer->next_token != &eof_token);
698 return lexer->next_token - previous_p;
701 static inline cp_token *
702 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
704 return pos;
707 static inline void
708 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
710 lexer->next_token = cp_lexer_token_at (lexer, pos);
713 static inline cp_token_position
714 cp_lexer_previous_token_position (cp_lexer *lexer)
716 if (lexer->next_token == &eof_token)
717 return lexer->last_token - 1;
718 else
719 return cp_lexer_token_position (lexer, true);
722 static inline cp_token *
723 cp_lexer_previous_token (cp_lexer *lexer)
725 cp_token_position tp = cp_lexer_previous_token_position (lexer);
727 return cp_lexer_token_at (lexer, tp);
730 /* nonzero if we are presently saving tokens. */
732 static inline int
733 cp_lexer_saving_tokens (const cp_lexer* lexer)
735 return lexer->saved_tokens.length () != 0;
738 /* Store the next token from the preprocessor in *TOKEN. Return true
739 if we reach EOF. If LEXER is NULL, assume we are handling an
740 initial #pragma pch_preprocess, and thus want the lexer to return
741 processed strings. */
743 static void
744 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
746 static int is_extern_c = 0;
748 /* Get a new token from the preprocessor. */
749 token->type
750 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
751 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
752 token->keyword = RID_MAX;
753 token->pragma_kind = PRAGMA_NONE;
754 token->purged_p = false;
756 /* On some systems, some header files are surrounded by an
757 implicit extern "C" block. Set a flag in the token if it
758 comes from such a header. */
759 is_extern_c += pending_lang_change;
760 pending_lang_change = 0;
761 token->implicit_extern_c = is_extern_c > 0;
763 /* Check to see if this token is a keyword. */
764 if (token->type == CPP_NAME)
766 if (C_IS_RESERVED_WORD (token->u.value))
768 /* Mark this token as a keyword. */
769 token->type = CPP_KEYWORD;
770 /* Record which keyword. */
771 token->keyword = C_RID_CODE (token->u.value);
773 else
775 if (warn_cxx0x_compat
776 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
777 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
779 /* Warn about the C++0x keyword (but still treat it as
780 an identifier). */
781 warning (OPT_Wc__0x_compat,
782 "identifier %qE is a keyword in C++11",
783 token->u.value);
785 /* Clear out the C_RID_CODE so we don't warn about this
786 particular identifier-turned-keyword again. */
787 C_SET_RID_CODE (token->u.value, RID_MAX);
790 token->ambiguous_p = false;
791 token->keyword = RID_MAX;
794 else if (token->type == CPP_AT_NAME)
796 /* This only happens in Objective-C++; it must be a keyword. */
797 token->type = CPP_KEYWORD;
798 switch (C_RID_CODE (token->u.value))
800 /* Replace 'class' with '@class', 'private' with '@private',
801 etc. This prevents confusion with the C++ keyword
802 'class', and makes the tokens consistent with other
803 Objective-C 'AT' keywords. For example '@class' is
804 reported as RID_AT_CLASS which is consistent with
805 '@synchronized', which is reported as
806 RID_AT_SYNCHRONIZED.
808 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
809 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
810 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
811 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
812 case RID_THROW: token->keyword = RID_AT_THROW; break;
813 case RID_TRY: token->keyword = RID_AT_TRY; break;
814 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
815 default: token->keyword = C_RID_CODE (token->u.value);
818 else if (token->type == CPP_PRAGMA)
820 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
821 token->pragma_kind = ((enum pragma_kind)
822 TREE_INT_CST_LOW (token->u.value));
823 token->u.value = NULL_TREE;
827 /* Update the globals input_location and the input file stack from TOKEN. */
828 static inline void
829 cp_lexer_set_source_position_from_token (cp_token *token)
831 if (token->type != CPP_EOF)
833 input_location = token->location;
837 /* Return a pointer to the next token in the token stream, but do not
838 consume it. */
840 static inline cp_token *
841 cp_lexer_peek_token (cp_lexer *lexer)
843 if (cp_lexer_debugging_p (lexer))
845 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
846 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
847 putc ('\n', cp_lexer_debug_stream);
849 return lexer->next_token;
852 /* Return true if the next token has the indicated TYPE. */
854 static inline bool
855 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
857 return cp_lexer_peek_token (lexer)->type == type;
860 /* Return true if the next token does not have the indicated TYPE. */
862 static inline bool
863 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
865 return !cp_lexer_next_token_is (lexer, type);
868 /* Return true if the next token is the indicated KEYWORD. */
870 static inline bool
871 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
873 return cp_lexer_peek_token (lexer)->keyword == keyword;
876 static inline bool
877 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
879 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
882 /* Return true if the next token is not the indicated KEYWORD. */
884 static inline bool
885 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
887 return cp_lexer_peek_token (lexer)->keyword != keyword;
890 /* Return true if the next token is a keyword for a decl-specifier. */
892 static bool
893 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
895 cp_token *token;
897 token = cp_lexer_peek_token (lexer);
898 switch (token->keyword)
900 /* auto specifier: storage-class-specifier in C++,
901 simple-type-specifier in C++0x. */
902 case RID_AUTO:
903 /* Storage classes. */
904 case RID_REGISTER:
905 case RID_STATIC:
906 case RID_EXTERN:
907 case RID_MUTABLE:
908 case RID_THREAD:
909 /* Elaborated type specifiers. */
910 case RID_ENUM:
911 case RID_CLASS:
912 case RID_STRUCT:
913 case RID_UNION:
914 case RID_TYPENAME:
915 /* Simple type specifiers. */
916 case RID_CHAR:
917 case RID_CHAR16:
918 case RID_CHAR32:
919 case RID_WCHAR:
920 case RID_BOOL:
921 case RID_SHORT:
922 case RID_INT:
923 case RID_LONG:
924 case RID_INT128:
925 case RID_SIGNED:
926 case RID_UNSIGNED:
927 case RID_FLOAT:
928 case RID_DOUBLE:
929 case RID_VOID:
930 /* GNU extensions. */
931 case RID_ATTRIBUTE:
932 case RID_TYPEOF:
933 /* C++0x extensions. */
934 case RID_DECLTYPE:
935 case RID_UNDERLYING_TYPE:
936 return true;
938 default:
939 return false;
943 /* Returns TRUE iff the token T begins a decltype type. */
945 static bool
946 token_is_decltype (cp_token *t)
948 return (t->keyword == RID_DECLTYPE
949 || t->type == CPP_DECLTYPE);
952 /* Returns TRUE iff the next token begins a decltype type. */
954 static bool
955 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
957 cp_token *t = cp_lexer_peek_token (lexer);
958 return token_is_decltype (t);
961 /* Return a pointer to the Nth token in the token stream. If N is 1,
962 then this is precisely equivalent to cp_lexer_peek_token (except
963 that it is not inline). One would like to disallow that case, but
964 there is one case (cp_parser_nth_token_starts_template_id) where
965 the caller passes a variable for N and it might be 1. */
967 static cp_token *
968 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
970 cp_token *token;
972 /* N is 1-based, not zero-based. */
973 gcc_assert (n > 0);
975 if (cp_lexer_debugging_p (lexer))
976 fprintf (cp_lexer_debug_stream,
977 "cp_lexer: peeking ahead %ld at token: ", (long)n);
979 --n;
980 token = lexer->next_token;
981 gcc_assert (!n || token != &eof_token);
982 while (n != 0)
984 ++token;
985 if (token == lexer->last_token)
987 token = &eof_token;
988 break;
991 if (!token->purged_p)
992 --n;
995 if (cp_lexer_debugging_p (lexer))
997 cp_lexer_print_token (cp_lexer_debug_stream, token);
998 putc ('\n', cp_lexer_debug_stream);
1001 return token;
1004 /* Return the next token, and advance the lexer's next_token pointer
1005 to point to the next non-purged token. */
1007 static cp_token *
1008 cp_lexer_consume_token (cp_lexer* lexer)
1010 cp_token *token = lexer->next_token;
1012 gcc_assert (token != &eof_token);
1013 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1017 lexer->next_token++;
1018 if (lexer->next_token == lexer->last_token)
1020 lexer->next_token = &eof_token;
1021 break;
1025 while (lexer->next_token->purged_p);
1027 cp_lexer_set_source_position_from_token (token);
1029 /* Provide debugging output. */
1030 if (cp_lexer_debugging_p (lexer))
1032 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1033 cp_lexer_print_token (cp_lexer_debug_stream, token);
1034 putc ('\n', cp_lexer_debug_stream);
1037 return token;
1040 /* Permanently remove the next token from the token stream, and
1041 advance the next_token pointer to refer to the next non-purged
1042 token. */
1044 static void
1045 cp_lexer_purge_token (cp_lexer *lexer)
1047 cp_token *tok = lexer->next_token;
1049 gcc_assert (tok != &eof_token);
1050 tok->purged_p = true;
1051 tok->location = UNKNOWN_LOCATION;
1052 tok->u.value = NULL_TREE;
1053 tok->keyword = RID_MAX;
1057 tok++;
1058 if (tok == lexer->last_token)
1060 tok = &eof_token;
1061 break;
1064 while (tok->purged_p);
1065 lexer->next_token = tok;
1068 /* Permanently remove all tokens after TOK, up to, but not
1069 including, the token that will be returned next by
1070 cp_lexer_peek_token. */
1072 static void
1073 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1075 cp_token *peek = lexer->next_token;
1077 if (peek == &eof_token)
1078 peek = lexer->last_token;
1080 gcc_assert (tok < peek);
1082 for ( tok += 1; tok != peek; tok += 1)
1084 tok->purged_p = true;
1085 tok->location = UNKNOWN_LOCATION;
1086 tok->u.value = NULL_TREE;
1087 tok->keyword = RID_MAX;
1091 /* Begin saving tokens. All tokens consumed after this point will be
1092 preserved. */
1094 static void
1095 cp_lexer_save_tokens (cp_lexer* lexer)
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer))
1099 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1101 lexer->saved_tokens.safe_push (lexer->next_token);
1104 /* Commit to the portion of the token stream most recently saved. */
1106 static void
1107 cp_lexer_commit_tokens (cp_lexer* lexer)
1109 /* Provide debugging output. */
1110 if (cp_lexer_debugging_p (lexer))
1111 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1113 lexer->saved_tokens.pop ();
1116 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1117 to the token stream. Stop saving tokens. */
1119 static void
1120 cp_lexer_rollback_tokens (cp_lexer* lexer)
1122 /* Provide debugging output. */
1123 if (cp_lexer_debugging_p (lexer))
1124 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1126 lexer->next_token = lexer->saved_tokens.pop ();
1129 /* Print a representation of the TOKEN on the STREAM. */
1131 static void
1132 cp_lexer_print_token (FILE * stream, cp_token *token)
1134 /* We don't use cpp_type2name here because the parser defines
1135 a few tokens of its own. */
1136 static const char *const token_names[] = {
1137 /* cpplib-defined token types */
1138 #define OP(e, s) #e,
1139 #define TK(e, s) #e,
1140 TTYPE_TABLE
1141 #undef OP
1142 #undef TK
1143 /* C++ parser token types - see "Manifest constants", above. */
1144 "KEYWORD",
1145 "TEMPLATE_ID",
1146 "NESTED_NAME_SPECIFIER",
1149 /* For some tokens, print the associated data. */
1150 switch (token->type)
1152 case CPP_KEYWORD:
1153 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1154 For example, `struct' is mapped to an INTEGER_CST. */
1155 if (!identifier_p (token->u.value))
1156 break;
1157 /* else fall through */
1158 case CPP_NAME:
1159 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1160 break;
1162 case CPP_STRING:
1163 case CPP_STRING16:
1164 case CPP_STRING32:
1165 case CPP_WSTRING:
1166 case CPP_UTF8STRING:
1167 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1168 break;
1170 case CPP_NUMBER:
1171 print_generic_expr (stream, token->u.value, 0);
1172 break;
1174 default:
1175 /* If we have a name for the token, print it out. Otherwise, we
1176 simply give the numeric code. */
1177 if (token->type < ARRAY_SIZE(token_names))
1178 fputs (token_names[token->type], stream);
1179 else
1180 fprintf (stream, "[%d]", token->type);
1181 break;
1185 DEBUG_FUNCTION void
1186 debug (cp_token &ref)
1188 cp_lexer_print_token (stderr, &ref);
1189 fprintf (stderr, "\n");
1192 DEBUG_FUNCTION void
1193 debug (cp_token *ptr)
1195 if (ptr)
1196 debug (*ptr);
1197 else
1198 fprintf (stderr, "<nil>\n");
1202 /* Start emitting debugging information. */
1204 static void
1205 cp_lexer_start_debugging (cp_lexer* lexer)
1207 lexer->debugging_p = true;
1208 cp_lexer_debug_stream = stderr;
1211 /* Stop emitting debugging information. */
1213 static void
1214 cp_lexer_stop_debugging (cp_lexer* lexer)
1216 lexer->debugging_p = false;
1217 cp_lexer_debug_stream = NULL;
1220 /* Create a new cp_token_cache, representing a range of tokens. */
1222 static cp_token_cache *
1223 cp_token_cache_new (cp_token *first, cp_token *last)
1225 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1226 cache->first = first;
1227 cache->last = last;
1228 return cache;
1231 /* Diagnose if #pragma omp declare simd isn't followed immediately
1232 by function declaration or definition. */
1234 static inline void
1235 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1237 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1239 error ("%<#pragma omp declare simd%> not immediately followed by "
1240 "function declaration or definition");
1241 parser->omp_declare_simd = NULL;
1245 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1246 and put that into "omp declare simd" attribute. */
1248 static inline void
1249 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1251 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1253 if (fndecl == error_mark_node)
1255 parser->omp_declare_simd = NULL;
1256 return;
1258 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1260 cp_ensure_no_omp_declare_simd (parser);
1261 return;
1266 /* Decl-specifiers. */
1268 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1270 static void
1271 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1273 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1276 /* Declarators. */
1278 /* Nothing other than the parser should be creating declarators;
1279 declarators are a semi-syntactic representation of C++ entities.
1280 Other parts of the front end that need to create entities (like
1281 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1283 static cp_declarator *make_call_declarator
1284 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1285 static cp_declarator *make_array_declarator
1286 (cp_declarator *, tree);
1287 static cp_declarator *make_pointer_declarator
1288 (cp_cv_quals, cp_declarator *, tree);
1289 static cp_declarator *make_reference_declarator
1290 (cp_cv_quals, cp_declarator *, bool, tree);
1291 static cp_parameter_declarator *make_parameter_declarator
1292 (cp_decl_specifier_seq *, cp_declarator *, tree);
1293 static cp_declarator *make_ptrmem_declarator
1294 (cp_cv_quals, tree, cp_declarator *, tree);
1296 /* An erroneous declarator. */
1297 static cp_declarator *cp_error_declarator;
1299 /* The obstack on which declarators and related data structures are
1300 allocated. */
1301 static struct obstack declarator_obstack;
1303 /* Alloc BYTES from the declarator memory pool. */
1305 static inline void *
1306 alloc_declarator (size_t bytes)
1308 return obstack_alloc (&declarator_obstack, bytes);
1311 /* Allocate a declarator of the indicated KIND. Clear fields that are
1312 common to all declarators. */
1314 static cp_declarator *
1315 make_declarator (cp_declarator_kind kind)
1317 cp_declarator *declarator;
1319 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1320 declarator->kind = kind;
1321 declarator->attributes = NULL_TREE;
1322 declarator->std_attributes = NULL_TREE;
1323 declarator->declarator = NULL;
1324 declarator->parameter_pack_p = false;
1325 declarator->id_loc = UNKNOWN_LOCATION;
1327 return declarator;
1330 /* Make a declarator for a generalized identifier. If
1331 QUALIFYING_SCOPE is non-NULL, the identifier is
1332 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1333 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1334 is, if any. */
1336 static cp_declarator *
1337 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1338 special_function_kind sfk)
1340 cp_declarator *declarator;
1342 /* It is valid to write:
1344 class C { void f(); };
1345 typedef C D;
1346 void D::f();
1348 The standard is not clear about whether `typedef const C D' is
1349 legal; as of 2002-09-15 the committee is considering that
1350 question. EDG 3.0 allows that syntax. Therefore, we do as
1351 well. */
1352 if (qualifying_scope && TYPE_P (qualifying_scope))
1353 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1355 gcc_assert (identifier_p (unqualified_name)
1356 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1357 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1359 declarator = make_declarator (cdk_id);
1360 declarator->u.id.qualifying_scope = qualifying_scope;
1361 declarator->u.id.unqualified_name = unqualified_name;
1362 declarator->u.id.sfk = sfk;
1364 return declarator;
1367 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1368 of modifiers such as const or volatile to apply to the pointer
1369 type, represented as identifiers. ATTRIBUTES represent the attributes that
1370 appertain to the pointer or reference. */
1372 cp_declarator *
1373 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1374 tree attributes)
1376 cp_declarator *declarator;
1378 declarator = make_declarator (cdk_pointer);
1379 declarator->declarator = target;
1380 declarator->u.pointer.qualifiers = cv_qualifiers;
1381 declarator->u.pointer.class_type = NULL_TREE;
1382 if (target)
1384 declarator->id_loc = target->id_loc;
1385 declarator->parameter_pack_p = target->parameter_pack_p;
1386 target->parameter_pack_p = false;
1388 else
1389 declarator->parameter_pack_p = false;
1391 declarator->std_attributes = attributes;
1393 return declarator;
1396 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1397 represent the attributes that appertain to the pointer or
1398 reference. */
1400 cp_declarator *
1401 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1402 bool rvalue_ref, tree attributes)
1404 cp_declarator *declarator;
1406 declarator = make_declarator (cdk_reference);
1407 declarator->declarator = target;
1408 declarator->u.reference.qualifiers = cv_qualifiers;
1409 declarator->u.reference.rvalue_ref = rvalue_ref;
1410 if (target)
1412 declarator->id_loc = target->id_loc;
1413 declarator->parameter_pack_p = target->parameter_pack_p;
1414 target->parameter_pack_p = false;
1416 else
1417 declarator->parameter_pack_p = false;
1419 declarator->std_attributes = attributes;
1421 return declarator;
1424 /* Like make_pointer_declarator -- but for a pointer to a non-static
1425 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1426 appertain to the pointer or reference. */
1428 cp_declarator *
1429 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1430 cp_declarator *pointee,
1431 tree attributes)
1433 cp_declarator *declarator;
1435 declarator = make_declarator (cdk_ptrmem);
1436 declarator->declarator = pointee;
1437 declarator->u.pointer.qualifiers = cv_qualifiers;
1438 declarator->u.pointer.class_type = class_type;
1440 if (pointee)
1442 declarator->parameter_pack_p = pointee->parameter_pack_p;
1443 pointee->parameter_pack_p = false;
1445 else
1446 declarator->parameter_pack_p = false;
1448 declarator->std_attributes = attributes;
1450 return declarator;
1453 /* Make a declarator for the function given by TARGET, with the
1454 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1455 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1456 indicates what exceptions can be thrown. */
1458 cp_declarator *
1459 make_call_declarator (cp_declarator *target,
1460 tree parms,
1461 cp_cv_quals cv_qualifiers,
1462 cp_virt_specifiers virt_specifiers,
1463 cp_ref_qualifier ref_qualifier,
1464 tree exception_specification,
1465 tree late_return_type)
1467 cp_declarator *declarator;
1469 declarator = make_declarator (cdk_function);
1470 declarator->declarator = target;
1471 declarator->u.function.parameters = parms;
1472 declarator->u.function.qualifiers = cv_qualifiers;
1473 declarator->u.function.virt_specifiers = virt_specifiers;
1474 declarator->u.function.ref_qualifier = ref_qualifier;
1475 declarator->u.function.exception_specification = exception_specification;
1476 declarator->u.function.late_return_type = late_return_type;
1477 if (target)
1479 declarator->id_loc = target->id_loc;
1480 declarator->parameter_pack_p = target->parameter_pack_p;
1481 target->parameter_pack_p = false;
1483 else
1484 declarator->parameter_pack_p = false;
1486 return declarator;
1489 /* Make a declarator for an array of BOUNDS elements, each of which is
1490 defined by ELEMENT. */
1492 cp_declarator *
1493 make_array_declarator (cp_declarator *element, tree bounds)
1495 cp_declarator *declarator;
1497 declarator = make_declarator (cdk_array);
1498 declarator->declarator = element;
1499 declarator->u.array.bounds = bounds;
1500 if (element)
1502 declarator->id_loc = element->id_loc;
1503 declarator->parameter_pack_p = element->parameter_pack_p;
1504 element->parameter_pack_p = false;
1506 else
1507 declarator->parameter_pack_p = false;
1509 return declarator;
1512 /* Determine whether the declarator we've seen so far can be a
1513 parameter pack, when followed by an ellipsis. */
1514 static bool
1515 declarator_can_be_parameter_pack (cp_declarator *declarator)
1517 /* Search for a declarator name, or any other declarator that goes
1518 after the point where the ellipsis could appear in a parameter
1519 pack. If we find any of these, then this declarator can not be
1520 made into a parameter pack. */
1521 bool found = false;
1522 while (declarator && !found)
1524 switch ((int)declarator->kind)
1526 case cdk_id:
1527 case cdk_array:
1528 found = true;
1529 break;
1531 case cdk_error:
1532 return true;
1534 default:
1535 declarator = declarator->declarator;
1536 break;
1540 return !found;
1543 cp_parameter_declarator *no_parameters;
1545 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1546 DECLARATOR and DEFAULT_ARGUMENT. */
1548 cp_parameter_declarator *
1549 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1550 cp_declarator *declarator,
1551 tree default_argument)
1553 cp_parameter_declarator *parameter;
1555 parameter = ((cp_parameter_declarator *)
1556 alloc_declarator (sizeof (cp_parameter_declarator)));
1557 parameter->next = NULL;
1558 if (decl_specifiers)
1559 parameter->decl_specifiers = *decl_specifiers;
1560 else
1561 clear_decl_specs (&parameter->decl_specifiers);
1562 parameter->declarator = declarator;
1563 parameter->default_argument = default_argument;
1564 parameter->ellipsis_p = false;
1566 return parameter;
1569 /* Returns true iff DECLARATOR is a declaration for a function. */
1571 static bool
1572 function_declarator_p (const cp_declarator *declarator)
1574 while (declarator)
1576 if (declarator->kind == cdk_function
1577 && declarator->declarator->kind == cdk_id)
1578 return true;
1579 if (declarator->kind == cdk_id
1580 || declarator->kind == cdk_error)
1581 return false;
1582 declarator = declarator->declarator;
1584 return false;
1587 /* The parser. */
1589 /* Overview
1590 --------
1592 A cp_parser parses the token stream as specified by the C++
1593 grammar. Its job is purely parsing, not semantic analysis. For
1594 example, the parser breaks the token stream into declarators,
1595 expressions, statements, and other similar syntactic constructs.
1596 It does not check that the types of the expressions on either side
1597 of an assignment-statement are compatible, or that a function is
1598 not declared with a parameter of type `void'.
1600 The parser invokes routines elsewhere in the compiler to perform
1601 semantic analysis and to build up the abstract syntax tree for the
1602 code processed.
1604 The parser (and the template instantiation code, which is, in a
1605 way, a close relative of parsing) are the only parts of the
1606 compiler that should be calling push_scope and pop_scope, or
1607 related functions. The parser (and template instantiation code)
1608 keeps track of what scope is presently active; everything else
1609 should simply honor that. (The code that generates static
1610 initializers may also need to set the scope, in order to check
1611 access control correctly when emitting the initializers.)
1613 Methodology
1614 -----------
1616 The parser is of the standard recursive-descent variety. Upcoming
1617 tokens in the token stream are examined in order to determine which
1618 production to use when parsing a non-terminal. Some C++ constructs
1619 require arbitrary look ahead to disambiguate. For example, it is
1620 impossible, in the general case, to tell whether a statement is an
1621 expression or declaration without scanning the entire statement.
1622 Therefore, the parser is capable of "parsing tentatively." When the
1623 parser is not sure what construct comes next, it enters this mode.
1624 Then, while we attempt to parse the construct, the parser queues up
1625 error messages, rather than issuing them immediately, and saves the
1626 tokens it consumes. If the construct is parsed successfully, the
1627 parser "commits", i.e., it issues any queued error messages and
1628 the tokens that were being preserved are permanently discarded.
1629 If, however, the construct is not parsed successfully, the parser
1630 rolls back its state completely so that it can resume parsing using
1631 a different alternative.
1633 Future Improvements
1634 -------------------
1636 The performance of the parser could probably be improved substantially.
1637 We could often eliminate the need to parse tentatively by looking ahead
1638 a little bit. In some places, this approach might not entirely eliminate
1639 the need to parse tentatively, but it might still speed up the average
1640 case. */
1642 /* Flags that are passed to some parsing functions. These values can
1643 be bitwise-ored together. */
1645 enum
1647 /* No flags. */
1648 CP_PARSER_FLAGS_NONE = 0x0,
1649 /* The construct is optional. If it is not present, then no error
1650 should be issued. */
1651 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1652 /* When parsing a type-specifier, treat user-defined type-names
1653 as non-type identifiers. */
1654 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1655 /* When parsing a type-specifier, do not try to parse a class-specifier
1656 or enum-specifier. */
1657 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1658 /* When parsing a decl-specifier-seq, only allow type-specifier or
1659 constexpr. */
1660 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1663 /* This type is used for parameters and variables which hold
1664 combinations of the above flags. */
1665 typedef int cp_parser_flags;
1667 /* The different kinds of declarators we want to parse. */
1669 typedef enum cp_parser_declarator_kind
1671 /* We want an abstract declarator. */
1672 CP_PARSER_DECLARATOR_ABSTRACT,
1673 /* We want a named declarator. */
1674 CP_PARSER_DECLARATOR_NAMED,
1675 /* We don't mind, but the name must be an unqualified-id. */
1676 CP_PARSER_DECLARATOR_EITHER
1677 } cp_parser_declarator_kind;
1679 /* The precedence values used to parse binary expressions. The minimum value
1680 of PREC must be 1, because zero is reserved to quickly discriminate
1681 binary operators from other tokens. */
1683 enum cp_parser_prec
1685 PREC_NOT_OPERATOR,
1686 PREC_LOGICAL_OR_EXPRESSION,
1687 PREC_LOGICAL_AND_EXPRESSION,
1688 PREC_INCLUSIVE_OR_EXPRESSION,
1689 PREC_EXCLUSIVE_OR_EXPRESSION,
1690 PREC_AND_EXPRESSION,
1691 PREC_EQUALITY_EXPRESSION,
1692 PREC_RELATIONAL_EXPRESSION,
1693 PREC_SHIFT_EXPRESSION,
1694 PREC_ADDITIVE_EXPRESSION,
1695 PREC_MULTIPLICATIVE_EXPRESSION,
1696 PREC_PM_EXPRESSION,
1697 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1700 /* A mapping from a token type to a corresponding tree node type, with a
1701 precedence value. */
1703 typedef struct cp_parser_binary_operations_map_node
1705 /* The token type. */
1706 enum cpp_ttype token_type;
1707 /* The corresponding tree code. */
1708 enum tree_code tree_type;
1709 /* The precedence of this operator. */
1710 enum cp_parser_prec prec;
1711 } cp_parser_binary_operations_map_node;
1713 typedef struct cp_parser_expression_stack_entry
1715 /* Left hand side of the binary operation we are currently
1716 parsing. */
1717 tree lhs;
1718 /* Original tree code for left hand side, if it was a binary
1719 expression itself (used for -Wparentheses). */
1720 enum tree_code lhs_type;
1721 /* Tree code for the binary operation we are parsing. */
1722 enum tree_code tree_type;
1723 /* Precedence of the binary operation we are parsing. */
1724 enum cp_parser_prec prec;
1725 /* Location of the binary operation we are parsing. */
1726 location_t loc;
1727 } cp_parser_expression_stack_entry;
1729 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1730 entries because precedence levels on the stack are monotonically
1731 increasing. */
1732 typedef struct cp_parser_expression_stack_entry
1733 cp_parser_expression_stack[NUM_PREC_VALUES];
1735 /* Prototypes. */
1737 /* Constructors and destructors. */
1739 static cp_parser_context *cp_parser_context_new
1740 (cp_parser_context *);
1742 /* Class variables. */
1744 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1746 /* The operator-precedence table used by cp_parser_binary_expression.
1747 Transformed into an associative array (binops_by_token) by
1748 cp_parser_new. */
1750 static const cp_parser_binary_operations_map_node binops[] = {
1751 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1752 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1754 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1755 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1756 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1758 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1759 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1761 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1762 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1764 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1765 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1766 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1767 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1769 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1770 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1772 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1774 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1776 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1778 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1780 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1783 /* The same as binops, but initialized by cp_parser_new so that
1784 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1785 for speed. */
1786 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1788 /* Constructors and destructors. */
1790 /* Construct a new context. The context below this one on the stack
1791 is given by NEXT. */
1793 static cp_parser_context *
1794 cp_parser_context_new (cp_parser_context* next)
1796 cp_parser_context *context;
1798 /* Allocate the storage. */
1799 if (cp_parser_context_free_list != NULL)
1801 /* Pull the first entry from the free list. */
1802 context = cp_parser_context_free_list;
1803 cp_parser_context_free_list = context->next;
1804 memset (context, 0, sizeof (*context));
1806 else
1807 context = ggc_alloc_cleared_cp_parser_context ();
1809 /* No errors have occurred yet in this context. */
1810 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1811 /* If this is not the bottommost context, copy information that we
1812 need from the previous context. */
1813 if (next)
1815 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1816 expression, then we are parsing one in this context, too. */
1817 context->object_type = next->object_type;
1818 /* Thread the stack. */
1819 context->next = next;
1822 return context;
1825 /* Managing the unparsed function queues. */
1827 #define unparsed_funs_with_default_args \
1828 parser->unparsed_queues->last ().funs_with_default_args
1829 #define unparsed_funs_with_definitions \
1830 parser->unparsed_queues->last ().funs_with_definitions
1831 #define unparsed_nsdmis \
1832 parser->unparsed_queues->last ().nsdmis
1834 static void
1835 push_unparsed_function_queues (cp_parser *parser)
1837 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1838 vec_safe_push (parser->unparsed_queues, e);
1841 static void
1842 pop_unparsed_function_queues (cp_parser *parser)
1844 release_tree_vector (unparsed_funs_with_definitions);
1845 parser->unparsed_queues->pop ();
1848 /* Prototypes. */
1850 /* Constructors and destructors. */
1852 static cp_parser *cp_parser_new
1853 (void);
1855 /* Routines to parse various constructs.
1857 Those that return `tree' will return the error_mark_node (rather
1858 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1859 Sometimes, they will return an ordinary node if error-recovery was
1860 attempted, even though a parse error occurred. So, to check
1861 whether or not a parse error occurred, you should always use
1862 cp_parser_error_occurred. If the construct is optional (indicated
1863 either by an `_opt' in the name of the function that does the
1864 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1865 the construct is not present. */
1867 /* Lexical conventions [gram.lex] */
1869 static tree cp_parser_identifier
1870 (cp_parser *);
1871 static tree cp_parser_string_literal
1872 (cp_parser *, bool, bool);
1873 static tree cp_parser_userdef_char_literal
1874 (cp_parser *);
1875 static tree cp_parser_userdef_string_literal
1876 (cp_token *);
1877 static tree cp_parser_userdef_numeric_literal
1878 (cp_parser *);
1880 /* Basic concepts [gram.basic] */
1882 static bool cp_parser_translation_unit
1883 (cp_parser *);
1885 /* Expressions [gram.expr] */
1887 static tree cp_parser_primary_expression
1888 (cp_parser *, bool, bool, bool, cp_id_kind *);
1889 static tree cp_parser_id_expression
1890 (cp_parser *, bool, bool, bool *, bool, bool);
1891 static tree cp_parser_unqualified_id
1892 (cp_parser *, bool, bool, bool, bool);
1893 static tree cp_parser_nested_name_specifier_opt
1894 (cp_parser *, bool, bool, bool, bool);
1895 static tree cp_parser_nested_name_specifier
1896 (cp_parser *, bool, bool, bool, bool);
1897 static tree cp_parser_qualifying_entity
1898 (cp_parser *, bool, bool, bool, bool, bool);
1899 static tree cp_parser_postfix_expression
1900 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1901 static tree cp_parser_postfix_open_square_expression
1902 (cp_parser *, tree, bool, bool);
1903 static tree cp_parser_postfix_dot_deref_expression
1904 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1905 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1906 (cp_parser *, int, bool, bool, bool *);
1907 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1908 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1909 static void cp_parser_pseudo_destructor_name
1910 (cp_parser *, tree, tree *, tree *);
1911 static tree cp_parser_unary_expression
1912 (cp_parser *, bool, bool, cp_id_kind *);
1913 static enum tree_code cp_parser_unary_operator
1914 (cp_token *);
1915 static tree cp_parser_new_expression
1916 (cp_parser *);
1917 static vec<tree, va_gc> *cp_parser_new_placement
1918 (cp_parser *);
1919 static tree cp_parser_new_type_id
1920 (cp_parser *, tree *);
1921 static cp_declarator *cp_parser_new_declarator_opt
1922 (cp_parser *);
1923 static cp_declarator *cp_parser_direct_new_declarator
1924 (cp_parser *);
1925 static vec<tree, va_gc> *cp_parser_new_initializer
1926 (cp_parser *);
1927 static tree cp_parser_delete_expression
1928 (cp_parser *);
1929 static tree cp_parser_cast_expression
1930 (cp_parser *, bool, bool, bool, cp_id_kind *);
1931 static tree cp_parser_binary_expression
1932 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1933 static tree cp_parser_question_colon_clause
1934 (cp_parser *, tree);
1935 static tree cp_parser_assignment_expression
1936 (cp_parser *, bool, cp_id_kind *);
1937 static enum tree_code cp_parser_assignment_operator_opt
1938 (cp_parser *);
1939 static tree cp_parser_expression
1940 (cp_parser *, bool, cp_id_kind *);
1941 static tree cp_parser_expression
1942 (cp_parser *, bool, bool, cp_id_kind *);
1943 static tree cp_parser_constant_expression
1944 (cp_parser *, bool, bool *);
1945 static tree cp_parser_builtin_offsetof
1946 (cp_parser *);
1947 static tree cp_parser_lambda_expression
1948 (cp_parser *);
1949 static void cp_parser_lambda_introducer
1950 (cp_parser *, tree);
1951 static bool cp_parser_lambda_declarator_opt
1952 (cp_parser *, tree);
1953 static void cp_parser_lambda_body
1954 (cp_parser *, tree);
1956 /* Statements [gram.stmt.stmt] */
1958 static void cp_parser_statement
1959 (cp_parser *, tree, bool, bool *);
1960 static void cp_parser_label_for_labeled_statement
1961 (cp_parser *, tree);
1962 static tree cp_parser_expression_statement
1963 (cp_parser *, tree);
1964 static tree cp_parser_compound_statement
1965 (cp_parser *, tree, bool, bool);
1966 static void cp_parser_statement_seq_opt
1967 (cp_parser *, tree);
1968 static tree cp_parser_selection_statement
1969 (cp_parser *, bool *);
1970 static tree cp_parser_condition
1971 (cp_parser *);
1972 static tree cp_parser_iteration_statement
1973 (cp_parser *, bool);
1974 static bool cp_parser_for_init_statement
1975 (cp_parser *, tree *decl);
1976 static tree cp_parser_for
1977 (cp_parser *, bool);
1978 static tree cp_parser_c_for
1979 (cp_parser *, tree, tree, bool);
1980 static tree cp_parser_range_for
1981 (cp_parser *, tree, tree, tree, bool);
1982 static void do_range_for_auto_deduction
1983 (tree, tree);
1984 static tree cp_parser_perform_range_for_lookup
1985 (tree, tree *, tree *);
1986 static tree cp_parser_range_for_member_function
1987 (tree, tree);
1988 static tree cp_parser_jump_statement
1989 (cp_parser *);
1990 static void cp_parser_declaration_statement
1991 (cp_parser *);
1993 static tree cp_parser_implicitly_scoped_statement
1994 (cp_parser *, bool *);
1995 static void cp_parser_already_scoped_statement
1996 (cp_parser *);
1998 /* Declarations [gram.dcl.dcl] */
2000 static void cp_parser_declaration_seq_opt
2001 (cp_parser *);
2002 static void cp_parser_declaration
2003 (cp_parser *);
2004 static void cp_parser_block_declaration
2005 (cp_parser *, bool);
2006 static void cp_parser_simple_declaration
2007 (cp_parser *, bool, tree *);
2008 static void cp_parser_decl_specifier_seq
2009 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2010 static tree cp_parser_storage_class_specifier_opt
2011 (cp_parser *);
2012 static tree cp_parser_function_specifier_opt
2013 (cp_parser *, cp_decl_specifier_seq *);
2014 static tree cp_parser_type_specifier
2015 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2016 int *, bool *);
2017 static tree cp_parser_simple_type_specifier
2018 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2019 static tree cp_parser_type_name
2020 (cp_parser *);
2021 static tree cp_parser_nonclass_name
2022 (cp_parser* parser);
2023 static tree cp_parser_elaborated_type_specifier
2024 (cp_parser *, bool, bool);
2025 static tree cp_parser_enum_specifier
2026 (cp_parser *);
2027 static void cp_parser_enumerator_list
2028 (cp_parser *, tree);
2029 static void cp_parser_enumerator_definition
2030 (cp_parser *, tree);
2031 static tree cp_parser_namespace_name
2032 (cp_parser *);
2033 static void cp_parser_namespace_definition
2034 (cp_parser *);
2035 static void cp_parser_namespace_body
2036 (cp_parser *);
2037 static tree cp_parser_qualified_namespace_specifier
2038 (cp_parser *);
2039 static void cp_parser_namespace_alias_definition
2040 (cp_parser *);
2041 static bool cp_parser_using_declaration
2042 (cp_parser *, bool);
2043 static void cp_parser_using_directive
2044 (cp_parser *);
2045 static tree cp_parser_alias_declaration
2046 (cp_parser *);
2047 static void cp_parser_asm_definition
2048 (cp_parser *);
2049 static void cp_parser_linkage_specification
2050 (cp_parser *);
2051 static void cp_parser_static_assert
2052 (cp_parser *, bool);
2053 static tree cp_parser_decltype
2054 (cp_parser *);
2056 /* Declarators [gram.dcl.decl] */
2058 static tree cp_parser_init_declarator
2059 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2060 static cp_declarator *cp_parser_declarator
2061 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2062 static cp_declarator *cp_parser_direct_declarator
2063 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2064 static enum tree_code cp_parser_ptr_operator
2065 (cp_parser *, tree *, cp_cv_quals *, tree *);
2066 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2067 (cp_parser *);
2068 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2069 (cp_parser *);
2070 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2071 (cp_parser *);
2072 static tree cp_parser_late_return_type_opt
2073 (cp_parser *, cp_declarator *, cp_cv_quals);
2074 static tree cp_parser_declarator_id
2075 (cp_parser *, bool);
2076 static tree cp_parser_type_id
2077 (cp_parser *);
2078 static tree cp_parser_template_type_arg
2079 (cp_parser *);
2080 static tree cp_parser_trailing_type_id (cp_parser *);
2081 static tree cp_parser_type_id_1
2082 (cp_parser *, bool, bool);
2083 static void cp_parser_type_specifier_seq
2084 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2085 static tree cp_parser_parameter_declaration_clause
2086 (cp_parser *);
2087 static tree cp_parser_parameter_declaration_list
2088 (cp_parser *, bool *);
2089 static cp_parameter_declarator *cp_parser_parameter_declaration
2090 (cp_parser *, bool, bool *);
2091 static tree cp_parser_default_argument
2092 (cp_parser *, bool);
2093 static void cp_parser_function_body
2094 (cp_parser *, bool);
2095 static tree cp_parser_initializer
2096 (cp_parser *, bool *, bool *);
2097 static tree cp_parser_initializer_clause
2098 (cp_parser *, bool *);
2099 static tree cp_parser_braced_list
2100 (cp_parser*, bool*);
2101 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2102 (cp_parser *, bool *);
2104 static bool cp_parser_ctor_initializer_opt_and_function_body
2105 (cp_parser *, bool);
2107 static tree cp_parser_late_parsing_omp_declare_simd
2108 (cp_parser *, tree);
2110 static tree add_implicit_template_parms
2111 (cp_parser *, size_t, tree);
2112 static tree finish_fully_implicit_template
2113 (cp_parser *, tree);
2115 /* Classes [gram.class] */
2117 static tree cp_parser_class_name
2118 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2119 static tree cp_parser_class_specifier
2120 (cp_parser *);
2121 static tree cp_parser_class_head
2122 (cp_parser *, bool *);
2123 static enum tag_types cp_parser_class_key
2124 (cp_parser *);
2125 static void cp_parser_member_specification_opt
2126 (cp_parser *);
2127 static void cp_parser_member_declaration
2128 (cp_parser *);
2129 static tree cp_parser_pure_specifier
2130 (cp_parser *);
2131 static tree cp_parser_constant_initializer
2132 (cp_parser *);
2134 /* Derived classes [gram.class.derived] */
2136 static tree cp_parser_base_clause
2137 (cp_parser *);
2138 static tree cp_parser_base_specifier
2139 (cp_parser *);
2141 /* Special member functions [gram.special] */
2143 static tree cp_parser_conversion_function_id
2144 (cp_parser *);
2145 static tree cp_parser_conversion_type_id
2146 (cp_parser *);
2147 static cp_declarator *cp_parser_conversion_declarator_opt
2148 (cp_parser *);
2149 static bool cp_parser_ctor_initializer_opt
2150 (cp_parser *);
2151 static void cp_parser_mem_initializer_list
2152 (cp_parser *);
2153 static tree cp_parser_mem_initializer
2154 (cp_parser *);
2155 static tree cp_parser_mem_initializer_id
2156 (cp_parser *);
2158 /* Overloading [gram.over] */
2160 static tree cp_parser_operator_function_id
2161 (cp_parser *);
2162 static tree cp_parser_operator
2163 (cp_parser *);
2165 /* Templates [gram.temp] */
2167 static void cp_parser_template_declaration
2168 (cp_parser *, bool);
2169 static tree cp_parser_template_parameter_list
2170 (cp_parser *);
2171 static tree cp_parser_template_parameter
2172 (cp_parser *, bool *, bool *);
2173 static tree cp_parser_type_parameter
2174 (cp_parser *, bool *);
2175 static tree cp_parser_template_id
2176 (cp_parser *, bool, bool, enum tag_types, bool);
2177 static tree cp_parser_template_name
2178 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2179 static tree cp_parser_template_argument_list
2180 (cp_parser *);
2181 static tree cp_parser_template_argument
2182 (cp_parser *);
2183 static void cp_parser_explicit_instantiation
2184 (cp_parser *);
2185 static void cp_parser_explicit_specialization
2186 (cp_parser *);
2188 /* Exception handling [gram.exception] */
2190 static tree cp_parser_try_block
2191 (cp_parser *);
2192 static bool cp_parser_function_try_block
2193 (cp_parser *);
2194 static void cp_parser_handler_seq
2195 (cp_parser *);
2196 static void cp_parser_handler
2197 (cp_parser *);
2198 static tree cp_parser_exception_declaration
2199 (cp_parser *);
2200 static tree cp_parser_throw_expression
2201 (cp_parser *);
2202 static tree cp_parser_exception_specification_opt
2203 (cp_parser *);
2204 static tree cp_parser_type_id_list
2205 (cp_parser *);
2207 /* GNU Extensions */
2209 static tree cp_parser_asm_specification_opt
2210 (cp_parser *);
2211 static tree cp_parser_asm_operand_list
2212 (cp_parser *);
2213 static tree cp_parser_asm_clobber_list
2214 (cp_parser *);
2215 static tree cp_parser_asm_label_list
2216 (cp_parser *);
2217 static bool cp_next_tokens_can_be_attribute_p
2218 (cp_parser *);
2219 static bool cp_next_tokens_can_be_gnu_attribute_p
2220 (cp_parser *);
2221 static bool cp_next_tokens_can_be_std_attribute_p
2222 (cp_parser *);
2223 static bool cp_nth_tokens_can_be_std_attribute_p
2224 (cp_parser *, size_t);
2225 static bool cp_nth_tokens_can_be_gnu_attribute_p
2226 (cp_parser *, size_t);
2227 static bool cp_nth_tokens_can_be_attribute_p
2228 (cp_parser *, size_t);
2229 static tree cp_parser_attributes_opt
2230 (cp_parser *);
2231 static tree cp_parser_gnu_attributes_opt
2232 (cp_parser *);
2233 static tree cp_parser_gnu_attribute_list
2234 (cp_parser *);
2235 static tree cp_parser_std_attribute
2236 (cp_parser *);
2237 static tree cp_parser_std_attribute_spec
2238 (cp_parser *);
2239 static tree cp_parser_std_attribute_spec_seq
2240 (cp_parser *);
2241 static bool cp_parser_extension_opt
2242 (cp_parser *, int *);
2243 static void cp_parser_label_declaration
2244 (cp_parser *);
2246 /* Transactional Memory Extensions */
2248 static tree cp_parser_transaction
2249 (cp_parser *, enum rid);
2250 static tree cp_parser_transaction_expression
2251 (cp_parser *, enum rid);
2252 static bool cp_parser_function_transaction
2253 (cp_parser *, enum rid);
2254 static tree cp_parser_transaction_cancel
2255 (cp_parser *);
2257 enum pragma_context {
2258 pragma_external,
2259 pragma_member,
2260 pragma_objc_icode,
2261 pragma_stmt,
2262 pragma_compound
2264 static bool cp_parser_pragma
2265 (cp_parser *, enum pragma_context);
2267 /* Objective-C++ Productions */
2269 static tree cp_parser_objc_message_receiver
2270 (cp_parser *);
2271 static tree cp_parser_objc_message_args
2272 (cp_parser *);
2273 static tree cp_parser_objc_message_expression
2274 (cp_parser *);
2275 static tree cp_parser_objc_encode_expression
2276 (cp_parser *);
2277 static tree cp_parser_objc_defs_expression
2278 (cp_parser *);
2279 static tree cp_parser_objc_protocol_expression
2280 (cp_parser *);
2281 static tree cp_parser_objc_selector_expression
2282 (cp_parser *);
2283 static tree cp_parser_objc_expression
2284 (cp_parser *);
2285 static bool cp_parser_objc_selector_p
2286 (enum cpp_ttype);
2287 static tree cp_parser_objc_selector
2288 (cp_parser *);
2289 static tree cp_parser_objc_protocol_refs_opt
2290 (cp_parser *);
2291 static void cp_parser_objc_declaration
2292 (cp_parser *, tree);
2293 static tree cp_parser_objc_statement
2294 (cp_parser *);
2295 static bool cp_parser_objc_valid_prefix_attributes
2296 (cp_parser *, tree *);
2297 static void cp_parser_objc_at_property_declaration
2298 (cp_parser *) ;
2299 static void cp_parser_objc_at_synthesize_declaration
2300 (cp_parser *) ;
2301 static void cp_parser_objc_at_dynamic_declaration
2302 (cp_parser *) ;
2303 static tree cp_parser_objc_struct_declaration
2304 (cp_parser *) ;
2306 /* Utility Routines */
2308 static tree cp_parser_lookup_name
2309 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2310 static tree cp_parser_lookup_name_simple
2311 (cp_parser *, tree, location_t);
2312 static tree cp_parser_maybe_treat_template_as_class
2313 (tree, bool);
2314 static bool cp_parser_check_declarator_template_parameters
2315 (cp_parser *, cp_declarator *, location_t);
2316 static bool cp_parser_check_template_parameters
2317 (cp_parser *, unsigned, location_t, cp_declarator *);
2318 static tree cp_parser_simple_cast_expression
2319 (cp_parser *);
2320 static tree cp_parser_global_scope_opt
2321 (cp_parser *, bool);
2322 static bool cp_parser_constructor_declarator_p
2323 (cp_parser *, bool);
2324 static tree cp_parser_function_definition_from_specifiers_and_declarator
2325 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2326 static tree cp_parser_function_definition_after_declarator
2327 (cp_parser *, bool);
2328 static void cp_parser_template_declaration_after_export
2329 (cp_parser *, bool);
2330 static void cp_parser_perform_template_parameter_access_checks
2331 (vec<deferred_access_check, va_gc> *);
2332 static tree cp_parser_single_declaration
2333 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2334 static tree cp_parser_functional_cast
2335 (cp_parser *, tree);
2336 static tree cp_parser_save_member_function_body
2337 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2338 static tree cp_parser_save_nsdmi
2339 (cp_parser *);
2340 static tree cp_parser_enclosed_template_argument_list
2341 (cp_parser *);
2342 static void cp_parser_save_default_args
2343 (cp_parser *, tree);
2344 static void cp_parser_late_parsing_for_member
2345 (cp_parser *, tree);
2346 static tree cp_parser_late_parse_one_default_arg
2347 (cp_parser *, tree, tree, tree);
2348 static void cp_parser_late_parsing_nsdmi
2349 (cp_parser *, tree);
2350 static void cp_parser_late_parsing_default_args
2351 (cp_parser *, tree);
2352 static tree cp_parser_sizeof_operand
2353 (cp_parser *, enum rid);
2354 static tree cp_parser_trait_expr
2355 (cp_parser *, enum rid);
2356 static bool cp_parser_declares_only_class_p
2357 (cp_parser *);
2358 static void cp_parser_set_storage_class
2359 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2360 static void cp_parser_set_decl_spec_type
2361 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2362 static void set_and_check_decl_spec_loc
2363 (cp_decl_specifier_seq *decl_specs,
2364 cp_decl_spec ds, cp_token *);
2365 static bool cp_parser_friend_p
2366 (const cp_decl_specifier_seq *);
2367 static void cp_parser_required_error
2368 (cp_parser *, required_token, bool);
2369 static cp_token *cp_parser_require
2370 (cp_parser *, enum cpp_ttype, required_token);
2371 static cp_token *cp_parser_require_keyword
2372 (cp_parser *, enum rid, required_token);
2373 static bool cp_parser_token_starts_function_definition_p
2374 (cp_token *);
2375 static bool cp_parser_next_token_starts_class_definition_p
2376 (cp_parser *);
2377 static bool cp_parser_next_token_ends_template_argument_p
2378 (cp_parser *);
2379 static bool cp_parser_nth_token_starts_template_argument_list_p
2380 (cp_parser *, size_t);
2381 static enum tag_types cp_parser_token_is_class_key
2382 (cp_token *);
2383 static void cp_parser_check_class_key
2384 (enum tag_types, tree type);
2385 static void cp_parser_check_access_in_redeclaration
2386 (tree type, location_t location);
2387 static bool cp_parser_optional_template_keyword
2388 (cp_parser *);
2389 static void cp_parser_pre_parsed_nested_name_specifier
2390 (cp_parser *);
2391 static bool cp_parser_cache_group
2392 (cp_parser *, enum cpp_ttype, unsigned);
2393 static tree cp_parser_cache_defarg
2394 (cp_parser *parser, bool nsdmi);
2395 static void cp_parser_parse_tentatively
2396 (cp_parser *);
2397 static void cp_parser_commit_to_tentative_parse
2398 (cp_parser *);
2399 static void cp_parser_commit_to_topmost_tentative_parse
2400 (cp_parser *);
2401 static void cp_parser_abort_tentative_parse
2402 (cp_parser *);
2403 static bool cp_parser_parse_definitely
2404 (cp_parser *);
2405 static inline bool cp_parser_parsing_tentatively
2406 (cp_parser *);
2407 static bool cp_parser_uncommitted_to_tentative_parse_p
2408 (cp_parser *);
2409 static void cp_parser_error
2410 (cp_parser *, const char *);
2411 static void cp_parser_name_lookup_error
2412 (cp_parser *, tree, tree, name_lookup_error, location_t);
2413 static bool cp_parser_simulate_error
2414 (cp_parser *);
2415 static bool cp_parser_check_type_definition
2416 (cp_parser *);
2417 static void cp_parser_check_for_definition_in_return_type
2418 (cp_declarator *, tree, location_t type_location);
2419 static void cp_parser_check_for_invalid_template_id
2420 (cp_parser *, tree, enum tag_types, location_t location);
2421 static bool cp_parser_non_integral_constant_expression
2422 (cp_parser *, non_integral_constant);
2423 static void cp_parser_diagnose_invalid_type_name
2424 (cp_parser *, tree, tree, location_t);
2425 static bool cp_parser_parse_and_diagnose_invalid_type_name
2426 (cp_parser *);
2427 static int cp_parser_skip_to_closing_parenthesis
2428 (cp_parser *, bool, bool, bool);
2429 static void cp_parser_skip_to_end_of_statement
2430 (cp_parser *);
2431 static void cp_parser_consume_semicolon_at_end_of_statement
2432 (cp_parser *);
2433 static void cp_parser_skip_to_end_of_block_or_statement
2434 (cp_parser *);
2435 static bool cp_parser_skip_to_closing_brace
2436 (cp_parser *);
2437 static void cp_parser_skip_to_end_of_template_parameter_list
2438 (cp_parser *);
2439 static void cp_parser_skip_to_pragma_eol
2440 (cp_parser*, cp_token *);
2441 static bool cp_parser_error_occurred
2442 (cp_parser *);
2443 static bool cp_parser_allow_gnu_extensions_p
2444 (cp_parser *);
2445 static bool cp_parser_is_pure_string_literal
2446 (cp_token *);
2447 static bool cp_parser_is_string_literal
2448 (cp_token *);
2449 static bool cp_parser_is_keyword
2450 (cp_token *, enum rid);
2451 static tree cp_parser_make_typename_type
2452 (cp_parser *, tree, tree, location_t location);
2453 static cp_declarator * cp_parser_make_indirect_declarator
2454 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2456 /* Returns nonzero if we are parsing tentatively. */
2458 static inline bool
2459 cp_parser_parsing_tentatively (cp_parser* parser)
2461 return parser->context->next != NULL;
2464 /* Returns nonzero if TOKEN is a string literal. */
2466 static bool
2467 cp_parser_is_pure_string_literal (cp_token* token)
2469 return (token->type == CPP_STRING ||
2470 token->type == CPP_STRING16 ||
2471 token->type == CPP_STRING32 ||
2472 token->type == CPP_WSTRING ||
2473 token->type == CPP_UTF8STRING);
2476 /* Returns nonzero if TOKEN is a string literal
2477 of a user-defined string literal. */
2479 static bool
2480 cp_parser_is_string_literal (cp_token* token)
2482 return (cp_parser_is_pure_string_literal (token) ||
2483 token->type == CPP_STRING_USERDEF ||
2484 token->type == CPP_STRING16_USERDEF ||
2485 token->type == CPP_STRING32_USERDEF ||
2486 token->type == CPP_WSTRING_USERDEF ||
2487 token->type == CPP_UTF8STRING_USERDEF);
2490 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2492 static bool
2493 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2495 return token->keyword == keyword;
2498 /* If not parsing tentatively, issue a diagnostic of the form
2499 FILE:LINE: MESSAGE before TOKEN
2500 where TOKEN is the next token in the input stream. MESSAGE
2501 (specified by the caller) is usually of the form "expected
2502 OTHER-TOKEN". */
2504 static void
2505 cp_parser_error (cp_parser* parser, const char* gmsgid)
2507 if (!cp_parser_simulate_error (parser))
2509 cp_token *token = cp_lexer_peek_token (parser->lexer);
2510 /* This diagnostic makes more sense if it is tagged to the line
2511 of the token we just peeked at. */
2512 cp_lexer_set_source_position_from_token (token);
2514 if (token->type == CPP_PRAGMA)
2516 error_at (token->location,
2517 "%<#pragma%> is not allowed here");
2518 cp_parser_skip_to_pragma_eol (parser, token);
2519 return;
2522 c_parse_error (gmsgid,
2523 /* Because c_parser_error does not understand
2524 CPP_KEYWORD, keywords are treated like
2525 identifiers. */
2526 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2527 token->u.value, token->flags);
2531 /* Issue an error about name-lookup failing. NAME is the
2532 IDENTIFIER_NODE DECL is the result of
2533 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2534 the thing that we hoped to find. */
2536 static void
2537 cp_parser_name_lookup_error (cp_parser* parser,
2538 tree name,
2539 tree decl,
2540 name_lookup_error desired,
2541 location_t location)
2543 /* If name lookup completely failed, tell the user that NAME was not
2544 declared. */
2545 if (decl == error_mark_node)
2547 if (parser->scope && parser->scope != global_namespace)
2548 error_at (location, "%<%E::%E%> has not been declared",
2549 parser->scope, name);
2550 else if (parser->scope == global_namespace)
2551 error_at (location, "%<::%E%> has not been declared", name);
2552 else if (parser->object_scope
2553 && !CLASS_TYPE_P (parser->object_scope))
2554 error_at (location, "request for member %qE in non-class type %qT",
2555 name, parser->object_scope);
2556 else if (parser->object_scope)
2557 error_at (location, "%<%T::%E%> has not been declared",
2558 parser->object_scope, name);
2559 else
2560 error_at (location, "%qE has not been declared", name);
2562 else if (parser->scope && parser->scope != global_namespace)
2564 switch (desired)
2566 case NLE_TYPE:
2567 error_at (location, "%<%E::%E%> is not a type",
2568 parser->scope, name);
2569 break;
2570 case NLE_CXX98:
2571 error_at (location, "%<%E::%E%> is not a class or namespace",
2572 parser->scope, name);
2573 break;
2574 case NLE_NOT_CXX98:
2575 error_at (location,
2576 "%<%E::%E%> is not a class, namespace, or enumeration",
2577 parser->scope, name);
2578 break;
2579 default:
2580 gcc_unreachable ();
2584 else if (parser->scope == global_namespace)
2586 switch (desired)
2588 case NLE_TYPE:
2589 error_at (location, "%<::%E%> is not a type", name);
2590 break;
2591 case NLE_CXX98:
2592 error_at (location, "%<::%E%> is not a class or namespace", name);
2593 break;
2594 case NLE_NOT_CXX98:
2595 error_at (location,
2596 "%<::%E%> is not a class, namespace, or enumeration",
2597 name);
2598 break;
2599 default:
2600 gcc_unreachable ();
2603 else
2605 switch (desired)
2607 case NLE_TYPE:
2608 error_at (location, "%qE is not a type", name);
2609 break;
2610 case NLE_CXX98:
2611 error_at (location, "%qE is not a class or namespace", name);
2612 break;
2613 case NLE_NOT_CXX98:
2614 error_at (location,
2615 "%qE is not a class, namespace, or enumeration", name);
2616 break;
2617 default:
2618 gcc_unreachable ();
2623 /* If we are parsing tentatively, remember that an error has occurred
2624 during this tentative parse. Returns true if the error was
2625 simulated; false if a message should be issued by the caller. */
2627 static bool
2628 cp_parser_simulate_error (cp_parser* parser)
2630 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2632 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2633 return true;
2635 return false;
2638 /* This function is called when a type is defined. If type
2639 definitions are forbidden at this point, an error message is
2640 issued. */
2642 static bool
2643 cp_parser_check_type_definition (cp_parser* parser)
2645 /* If types are forbidden here, issue a message. */
2646 if (parser->type_definition_forbidden_message)
2648 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2649 in the message need to be interpreted. */
2650 error (parser->type_definition_forbidden_message);
2651 return false;
2653 return true;
2656 /* This function is called when the DECLARATOR is processed. The TYPE
2657 was a type defined in the decl-specifiers. If it is invalid to
2658 define a type in the decl-specifiers for DECLARATOR, an error is
2659 issued. TYPE_LOCATION is the location of TYPE and is used
2660 for error reporting. */
2662 static void
2663 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2664 tree type, location_t type_location)
2666 /* [dcl.fct] forbids type definitions in return types.
2667 Unfortunately, it's not easy to know whether or not we are
2668 processing a return type until after the fact. */
2669 while (declarator
2670 && (declarator->kind == cdk_pointer
2671 || declarator->kind == cdk_reference
2672 || declarator->kind == cdk_ptrmem))
2673 declarator = declarator->declarator;
2674 if (declarator
2675 && declarator->kind == cdk_function)
2677 error_at (type_location,
2678 "new types may not be defined in a return type");
2679 inform (type_location,
2680 "(perhaps a semicolon is missing after the definition of %qT)",
2681 type);
2685 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2686 "<" in any valid C++ program. If the next token is indeed "<",
2687 issue a message warning the user about what appears to be an
2688 invalid attempt to form a template-id. LOCATION is the location
2689 of the type-specifier (TYPE) */
2691 static void
2692 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2693 tree type,
2694 enum tag_types tag_type,
2695 location_t location)
2697 cp_token_position start = 0;
2699 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2701 if (TYPE_P (type))
2702 error_at (location, "%qT is not a template", type);
2703 else if (identifier_p (type))
2705 if (tag_type != none_type)
2706 error_at (location, "%qE is not a class template", type);
2707 else
2708 error_at (location, "%qE is not a template", type);
2710 else
2711 error_at (location, "invalid template-id");
2712 /* Remember the location of the invalid "<". */
2713 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2714 start = cp_lexer_token_position (parser->lexer, true);
2715 /* Consume the "<". */
2716 cp_lexer_consume_token (parser->lexer);
2717 /* Parse the template arguments. */
2718 cp_parser_enclosed_template_argument_list (parser);
2719 /* Permanently remove the invalid template arguments so that
2720 this error message is not issued again. */
2721 if (start)
2722 cp_lexer_purge_tokens_after (parser->lexer, start);
2726 /* If parsing an integral constant-expression, issue an error message
2727 about the fact that THING appeared and return true. Otherwise,
2728 return false. In either case, set
2729 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2731 static bool
2732 cp_parser_non_integral_constant_expression (cp_parser *parser,
2733 non_integral_constant thing)
2735 parser->non_integral_constant_expression_p = true;
2736 if (parser->integral_constant_expression_p)
2738 if (!parser->allow_non_integral_constant_expression_p)
2740 const char *msg = NULL;
2741 switch (thing)
2743 case NIC_FLOAT:
2744 error ("floating-point literal "
2745 "cannot appear in a constant-expression");
2746 return true;
2747 case NIC_CAST:
2748 error ("a cast to a type other than an integral or "
2749 "enumeration type cannot appear in a "
2750 "constant-expression");
2751 return true;
2752 case NIC_TYPEID:
2753 error ("%<typeid%> operator "
2754 "cannot appear in a constant-expression");
2755 return true;
2756 case NIC_NCC:
2757 error ("non-constant compound literals "
2758 "cannot appear in a constant-expression");
2759 return true;
2760 case NIC_FUNC_CALL:
2761 error ("a function call "
2762 "cannot appear in a constant-expression");
2763 return true;
2764 case NIC_INC:
2765 error ("an increment "
2766 "cannot appear in a constant-expression");
2767 return true;
2768 case NIC_DEC:
2769 error ("an decrement "
2770 "cannot appear in a constant-expression");
2771 return true;
2772 case NIC_ARRAY_REF:
2773 error ("an array reference "
2774 "cannot appear in a constant-expression");
2775 return true;
2776 case NIC_ADDR_LABEL:
2777 error ("the address of a label "
2778 "cannot appear in a constant-expression");
2779 return true;
2780 case NIC_OVERLOADED:
2781 error ("calls to overloaded operators "
2782 "cannot appear in a constant-expression");
2783 return true;
2784 case NIC_ASSIGNMENT:
2785 error ("an assignment cannot appear in a constant-expression");
2786 return true;
2787 case NIC_COMMA:
2788 error ("a comma operator "
2789 "cannot appear in a constant-expression");
2790 return true;
2791 case NIC_CONSTRUCTOR:
2792 error ("a call to a constructor "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_TRANSACTION:
2796 error ("a transaction expression "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_THIS:
2800 msg = "this";
2801 break;
2802 case NIC_FUNC_NAME:
2803 msg = "__FUNCTION__";
2804 break;
2805 case NIC_PRETTY_FUNC:
2806 msg = "__PRETTY_FUNCTION__";
2807 break;
2808 case NIC_C99_FUNC:
2809 msg = "__func__";
2810 break;
2811 case NIC_VA_ARG:
2812 msg = "va_arg";
2813 break;
2814 case NIC_ARROW:
2815 msg = "->";
2816 break;
2817 case NIC_POINT:
2818 msg = ".";
2819 break;
2820 case NIC_STAR:
2821 msg = "*";
2822 break;
2823 case NIC_ADDR:
2824 msg = "&";
2825 break;
2826 case NIC_PREINCREMENT:
2827 msg = "++";
2828 break;
2829 case NIC_PREDECREMENT:
2830 msg = "--";
2831 break;
2832 case NIC_NEW:
2833 msg = "new";
2834 break;
2835 case NIC_DEL:
2836 msg = "delete";
2837 break;
2838 default:
2839 gcc_unreachable ();
2841 if (msg)
2842 error ("%qs cannot appear in a constant-expression", msg);
2843 return true;
2846 return false;
2849 /* Emit a diagnostic for an invalid type name. SCOPE is the
2850 qualifying scope (or NULL, if none) for ID. This function commits
2851 to the current active tentative parse, if any. (Otherwise, the
2852 problematic construct might be encountered again later, resulting
2853 in duplicate error messages.) LOCATION is the location of ID. */
2855 static void
2856 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2857 tree scope, tree id,
2858 location_t location)
2860 tree decl, old_scope;
2861 cp_parser_commit_to_tentative_parse (parser);
2862 /* Try to lookup the identifier. */
2863 old_scope = parser->scope;
2864 parser->scope = scope;
2865 decl = cp_parser_lookup_name_simple (parser, id, location);
2866 parser->scope = old_scope;
2867 /* If the lookup found a template-name, it means that the user forgot
2868 to specify an argument list. Emit a useful error message. */
2869 if (TREE_CODE (decl) == TEMPLATE_DECL)
2870 error_at (location,
2871 "invalid use of template-name %qE without an argument list",
2872 decl);
2873 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2874 error_at (location, "invalid use of destructor %qD as a type", id);
2875 else if (TREE_CODE (decl) == TYPE_DECL)
2876 /* Something like 'unsigned A a;' */
2877 error_at (location, "invalid combination of multiple type-specifiers");
2878 else if (!parser->scope)
2880 /* Issue an error message. */
2881 error_at (location, "%qE does not name a type", id);
2882 /* If we're in a template class, it's possible that the user was
2883 referring to a type from a base class. For example:
2885 template <typename T> struct A { typedef T X; };
2886 template <typename T> struct B : public A<T> { X x; };
2888 The user should have said "typename A<T>::X". */
2889 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2890 inform (location, "C++11 %<constexpr%> only available with "
2891 "-std=c++11 or -std=gnu++11");
2892 else if (processing_template_decl && current_class_type
2893 && TYPE_BINFO (current_class_type))
2895 tree b;
2897 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2899 b = TREE_CHAIN (b))
2901 tree base_type = BINFO_TYPE (b);
2902 if (CLASS_TYPE_P (base_type)
2903 && dependent_type_p (base_type))
2905 tree field;
2906 /* Go from a particular instantiation of the
2907 template (which will have an empty TYPE_FIELDs),
2908 to the main version. */
2909 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2910 for (field = TYPE_FIELDS (base_type);
2911 field;
2912 field = DECL_CHAIN (field))
2913 if (TREE_CODE (field) == TYPE_DECL
2914 && DECL_NAME (field) == id)
2916 inform (location,
2917 "(perhaps %<typename %T::%E%> was intended)",
2918 BINFO_TYPE (b), id);
2919 break;
2921 if (field)
2922 break;
2927 /* Here we diagnose qualified-ids where the scope is actually correct,
2928 but the identifier does not resolve to a valid type name. */
2929 else if (parser->scope != error_mark_node)
2931 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2933 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2934 error_at (location_of (id),
2935 "%qE in namespace %qE does not name a template type",
2936 id, parser->scope);
2937 else
2938 error_at (location_of (id),
2939 "%qE in namespace %qE does not name a type",
2940 id, parser->scope);
2942 else if (CLASS_TYPE_P (parser->scope)
2943 && constructor_name_p (id, parser->scope))
2945 /* A<T>::A<T>() */
2946 error_at (location, "%<%T::%E%> names the constructor, not"
2947 " the type", parser->scope, id);
2948 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2949 error_at (location, "and %qT has no template constructors",
2950 parser->scope);
2952 else if (TYPE_P (parser->scope)
2953 && dependent_scope_p (parser->scope))
2954 error_at (location, "need %<typename%> before %<%T::%E%> because "
2955 "%qT is a dependent scope",
2956 parser->scope, id, parser->scope);
2957 else if (TYPE_P (parser->scope))
2959 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2960 error_at (location_of (id),
2961 "%qE in %q#T does not name a template type",
2962 id, parser->scope);
2963 else
2964 error_at (location_of (id),
2965 "%qE in %q#T does not name a type",
2966 id, parser->scope);
2968 else
2969 gcc_unreachable ();
2973 /* Check for a common situation where a type-name should be present,
2974 but is not, and issue a sensible error message. Returns true if an
2975 invalid type-name was detected.
2977 The situation handled by this function are variable declarations of the
2978 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2979 Usually, `ID' should name a type, but if we got here it means that it
2980 does not. We try to emit the best possible error message depending on
2981 how exactly the id-expression looks like. */
2983 static bool
2984 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2986 tree id;
2987 cp_token *token = cp_lexer_peek_token (parser->lexer);
2989 /* Avoid duplicate error about ambiguous lookup. */
2990 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2992 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2993 if (next->type == CPP_NAME && next->ambiguous_p)
2994 goto out;
2997 cp_parser_parse_tentatively (parser);
2998 id = cp_parser_id_expression (parser,
2999 /*template_keyword_p=*/false,
3000 /*check_dependency_p=*/true,
3001 /*template_p=*/NULL,
3002 /*declarator_p=*/true,
3003 /*optional_p=*/false);
3004 /* If the next token is a (, this is a function with no explicit return
3005 type, i.e. constructor, destructor or conversion op. */
3006 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3007 || TREE_CODE (id) == TYPE_DECL)
3009 cp_parser_abort_tentative_parse (parser);
3010 return false;
3012 if (!cp_parser_parse_definitely (parser))
3013 return false;
3015 /* Emit a diagnostic for the invalid type. */
3016 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3017 id, token->location);
3018 out:
3019 /* If we aren't in the middle of a declarator (i.e. in a
3020 parameter-declaration-clause), skip to the end of the declaration;
3021 there's no point in trying to process it. */
3022 if (!parser->in_declarator_p)
3023 cp_parser_skip_to_end_of_block_or_statement (parser);
3024 return true;
3027 /* Consume tokens up to, and including, the next non-nested closing `)'.
3028 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3029 are doing error recovery. Returns -1 if OR_COMMA is true and we
3030 found an unnested comma. */
3032 static int
3033 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3034 bool recovering,
3035 bool or_comma,
3036 bool consume_paren)
3038 unsigned paren_depth = 0;
3039 unsigned brace_depth = 0;
3040 unsigned square_depth = 0;
3042 if (recovering && !or_comma
3043 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3044 return 0;
3046 while (true)
3048 cp_token * token = cp_lexer_peek_token (parser->lexer);
3050 switch (token->type)
3052 case CPP_EOF:
3053 case CPP_PRAGMA_EOL:
3054 /* If we've run out of tokens, then there is no closing `)'. */
3055 return 0;
3057 /* This is good for lambda expression capture-lists. */
3058 case CPP_OPEN_SQUARE:
3059 ++square_depth;
3060 break;
3061 case CPP_CLOSE_SQUARE:
3062 if (!square_depth--)
3063 return 0;
3064 break;
3066 case CPP_SEMICOLON:
3067 /* This matches the processing in skip_to_end_of_statement. */
3068 if (!brace_depth)
3069 return 0;
3070 break;
3072 case CPP_OPEN_BRACE:
3073 ++brace_depth;
3074 break;
3075 case CPP_CLOSE_BRACE:
3076 if (!brace_depth--)
3077 return 0;
3078 break;
3080 case CPP_COMMA:
3081 if (recovering && or_comma && !brace_depth && !paren_depth
3082 && !square_depth)
3083 return -1;
3084 break;
3086 case CPP_OPEN_PAREN:
3087 if (!brace_depth)
3088 ++paren_depth;
3089 break;
3091 case CPP_CLOSE_PAREN:
3092 if (!brace_depth && !paren_depth--)
3094 if (consume_paren)
3095 cp_lexer_consume_token (parser->lexer);
3096 return 1;
3098 break;
3100 default:
3101 break;
3104 /* Consume the token. */
3105 cp_lexer_consume_token (parser->lexer);
3109 /* Consume tokens until we reach the end of the current statement.
3110 Normally, that will be just before consuming a `;'. However, if a
3111 non-nested `}' comes first, then we stop before consuming that. */
3113 static void
3114 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3116 unsigned nesting_depth = 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, stop. */
3127 return;
3129 case CPP_SEMICOLON:
3130 /* If the next token is a `;', we have reached the end of the
3131 statement. */
3132 if (!nesting_depth)
3133 return;
3134 break;
3136 case CPP_CLOSE_BRACE:
3137 /* If this is a non-nested '}', stop before consuming it.
3138 That way, when confronted with something like:
3140 { 3 + }
3142 we stop before consuming the closing '}', even though we
3143 have not yet reached a `;'. */
3144 if (nesting_depth == 0)
3145 return;
3147 /* If it is the closing '}' for a block that we have
3148 scanned, stop -- but only after consuming the token.
3149 That way given:
3151 void f g () { ... }
3152 typedef int I;
3154 we will stop after the body of the erroneously declared
3155 function, but before consuming the following `typedef'
3156 declaration. */
3157 if (--nesting_depth == 0)
3159 cp_lexer_consume_token (parser->lexer);
3160 return;
3163 case CPP_OPEN_BRACE:
3164 ++nesting_depth;
3165 break;
3167 default:
3168 break;
3171 /* Consume the token. */
3172 cp_lexer_consume_token (parser->lexer);
3176 /* This function is called at the end of a statement or declaration.
3177 If the next token is a semicolon, it is consumed; otherwise, error
3178 recovery is attempted. */
3180 static void
3181 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3183 /* Look for the trailing `;'. */
3184 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3186 /* If there is additional (erroneous) input, skip to the end of
3187 the statement. */
3188 cp_parser_skip_to_end_of_statement (parser);
3189 /* If the next token is now a `;', consume it. */
3190 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3191 cp_lexer_consume_token (parser->lexer);
3195 /* Skip tokens until we have consumed an entire block, or until we
3196 have consumed a non-nested `;'. */
3198 static void
3199 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3201 int nesting_depth = 0;
3203 while (nesting_depth >= 0)
3205 cp_token *token = cp_lexer_peek_token (parser->lexer);
3207 switch (token->type)
3209 case CPP_EOF:
3210 case CPP_PRAGMA_EOL:
3211 /* If we've run out of tokens, stop. */
3212 return;
3214 case CPP_SEMICOLON:
3215 /* Stop if this is an unnested ';'. */
3216 if (!nesting_depth)
3217 nesting_depth = -1;
3218 break;
3220 case CPP_CLOSE_BRACE:
3221 /* Stop if this is an unnested '}', or closes the outermost
3222 nesting level. */
3223 nesting_depth--;
3224 if (nesting_depth < 0)
3225 return;
3226 if (!nesting_depth)
3227 nesting_depth = -1;
3228 break;
3230 case CPP_OPEN_BRACE:
3231 /* Nest. */
3232 nesting_depth++;
3233 break;
3235 default:
3236 break;
3239 /* Consume the token. */
3240 cp_lexer_consume_token (parser->lexer);
3244 /* Skip tokens until a non-nested closing curly brace is the next
3245 token, or there are no more tokens. Return true in the first case,
3246 false otherwise. */
3248 static bool
3249 cp_parser_skip_to_closing_brace (cp_parser *parser)
3251 unsigned nesting_depth = 0;
3253 while (true)
3255 cp_token *token = cp_lexer_peek_token (parser->lexer);
3257 switch (token->type)
3259 case CPP_EOF:
3260 case CPP_PRAGMA_EOL:
3261 /* If we've run out of tokens, stop. */
3262 return false;
3264 case CPP_CLOSE_BRACE:
3265 /* If the next token is a non-nested `}', then we have reached
3266 the end of the current block. */
3267 if (nesting_depth-- == 0)
3268 return true;
3269 break;
3271 case CPP_OPEN_BRACE:
3272 /* If it the next token is a `{', then we are entering a new
3273 block. Consume the entire block. */
3274 ++nesting_depth;
3275 break;
3277 default:
3278 break;
3281 /* Consume the token. */
3282 cp_lexer_consume_token (parser->lexer);
3286 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3287 parameter is the PRAGMA token, allowing us to purge the entire pragma
3288 sequence. */
3290 static void
3291 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3293 cp_token *token;
3295 parser->lexer->in_pragma = false;
3298 token = cp_lexer_consume_token (parser->lexer);
3299 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3301 /* Ensure that the pragma is not parsed again. */
3302 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3305 /* Require pragma end of line, resyncing with it as necessary. The
3306 arguments are as for cp_parser_skip_to_pragma_eol. */
3308 static void
3309 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3311 parser->lexer->in_pragma = false;
3312 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3313 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3316 /* This is a simple wrapper around make_typename_type. When the id is
3317 an unresolved identifier node, we can provide a superior diagnostic
3318 using cp_parser_diagnose_invalid_type_name. */
3320 static tree
3321 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3322 tree id, location_t id_location)
3324 tree result;
3325 if (identifier_p (id))
3327 result = make_typename_type (scope, id, typename_type,
3328 /*complain=*/tf_none);
3329 if (result == error_mark_node)
3330 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3331 return result;
3333 return make_typename_type (scope, id, typename_type, tf_error);
3336 /* This is a wrapper around the
3337 make_{pointer,ptrmem,reference}_declarator functions that decides
3338 which one to call based on the CODE and CLASS_TYPE arguments. The
3339 CODE argument should be one of the values returned by
3340 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3341 appertain to the pointer or reference. */
3343 static cp_declarator *
3344 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3345 cp_cv_quals cv_qualifiers,
3346 cp_declarator *target,
3347 tree attributes)
3349 if (code == ERROR_MARK)
3350 return cp_error_declarator;
3352 if (code == INDIRECT_REF)
3353 if (class_type == NULL_TREE)
3354 return make_pointer_declarator (cv_qualifiers, target, attributes);
3355 else
3356 return make_ptrmem_declarator (cv_qualifiers, class_type,
3357 target, attributes);
3358 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3359 return make_reference_declarator (cv_qualifiers, target,
3360 false, attributes);
3361 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3362 return make_reference_declarator (cv_qualifiers, target,
3363 true, attributes);
3364 gcc_unreachable ();
3367 /* Create a new C++ parser. */
3369 static cp_parser *
3370 cp_parser_new (void)
3372 cp_parser *parser;
3373 cp_lexer *lexer;
3374 unsigned i;
3376 /* cp_lexer_new_main is called before doing GC allocation because
3377 cp_lexer_new_main might load a PCH file. */
3378 lexer = cp_lexer_new_main ();
3380 /* Initialize the binops_by_token so that we can get the tree
3381 directly from the token. */
3382 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3383 binops_by_token[binops[i].token_type] = binops[i];
3385 parser = ggc_alloc_cleared_cp_parser ();
3386 parser->lexer = lexer;
3387 parser->context = cp_parser_context_new (NULL);
3389 /* For now, we always accept GNU extensions. */
3390 parser->allow_gnu_extensions_p = 1;
3392 /* The `>' token is a greater-than operator, not the end of a
3393 template-id. */
3394 parser->greater_than_is_operator_p = true;
3396 parser->default_arg_ok_p = true;
3398 /* We are not parsing a constant-expression. */
3399 parser->integral_constant_expression_p = false;
3400 parser->allow_non_integral_constant_expression_p = false;
3401 parser->non_integral_constant_expression_p = false;
3403 /* Local variable names are not forbidden. */
3404 parser->local_variables_forbidden_p = false;
3406 /* We are not processing an `extern "C"' declaration. */
3407 parser->in_unbraced_linkage_specification_p = false;
3409 /* We are not processing a declarator. */
3410 parser->in_declarator_p = false;
3412 /* We are not processing a template-argument-list. */
3413 parser->in_template_argument_list_p = false;
3415 /* We are not in an iteration statement. */
3416 parser->in_statement = 0;
3418 /* We are not in a switch statement. */
3419 parser->in_switch_statement_p = false;
3421 /* We are not parsing a type-id inside an expression. */
3422 parser->in_type_id_in_expr_p = false;
3424 /* Declarations aren't implicitly extern "C". */
3425 parser->implicit_extern_c = false;
3427 /* String literals should be translated to the execution character set. */
3428 parser->translate_strings_p = true;
3430 /* We are not parsing a function body. */
3431 parser->in_function_body = false;
3433 /* We can correct until told otherwise. */
3434 parser->colon_corrects_to_scope_p = true;
3436 /* The unparsed function queue is empty. */
3437 push_unparsed_function_queues (parser);
3439 /* There are no classes being defined. */
3440 parser->num_classes_being_defined = 0;
3442 /* No template parameters apply. */
3443 parser->num_template_parameter_lists = 0;
3445 /* Not declaring an implicit function template. */
3446 parser->fully_implicit_function_template_p = false;
3448 return parser;
3451 /* Create a cp_lexer structure which will emit the tokens in CACHE
3452 and push it onto the parser's lexer stack. This is used for delayed
3453 parsing of in-class method bodies and default arguments, and should
3454 not be confused with tentative parsing. */
3455 static void
3456 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3458 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3459 lexer->next = parser->lexer;
3460 parser->lexer = lexer;
3462 /* Move the current source position to that of the first token in the
3463 new lexer. */
3464 cp_lexer_set_source_position_from_token (lexer->next_token);
3467 /* Pop the top lexer off the parser stack. This is never used for the
3468 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3469 static void
3470 cp_parser_pop_lexer (cp_parser *parser)
3472 cp_lexer *lexer = parser->lexer;
3473 parser->lexer = lexer->next;
3474 cp_lexer_destroy (lexer);
3476 /* Put the current source position back where it was before this
3477 lexer was pushed. */
3478 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3481 /* Lexical conventions [gram.lex] */
3483 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3484 identifier. */
3486 static tree
3487 cp_parser_identifier (cp_parser* parser)
3489 cp_token *token;
3491 /* Look for the identifier. */
3492 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3493 /* Return the value. */
3494 return token ? token->u.value : error_mark_node;
3497 /* Parse a sequence of adjacent string constants. Returns a
3498 TREE_STRING representing the combined, nul-terminated string
3499 constant. If TRANSLATE is true, translate the string to the
3500 execution character set. If WIDE_OK is true, a wide string is
3501 invalid here.
3503 C++98 [lex.string] says that if a narrow string literal token is
3504 adjacent to a wide string literal token, the behavior is undefined.
3505 However, C99 6.4.5p4 says that this results in a wide string literal.
3506 We follow C99 here, for consistency with the C front end.
3508 This code is largely lifted from lex_string() in c-lex.c.
3510 FUTURE: ObjC++ will need to handle @-strings here. */
3511 static tree
3512 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3514 tree value;
3515 size_t count;
3516 struct obstack str_ob;
3517 cpp_string str, istr, *strs;
3518 cp_token *tok;
3519 enum cpp_ttype type, curr_type;
3520 int have_suffix_p = 0;
3521 tree string_tree;
3522 tree suffix_id = NULL_TREE;
3523 bool curr_tok_is_userdef_p = false;
3525 tok = cp_lexer_peek_token (parser->lexer);
3526 if (!cp_parser_is_string_literal (tok))
3528 cp_parser_error (parser, "expected string-literal");
3529 return error_mark_node;
3532 if (cpp_userdef_string_p (tok->type))
3534 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3535 curr_type = cpp_userdef_string_remove_type (tok->type);
3536 curr_tok_is_userdef_p = true;
3538 else
3540 string_tree = tok->u.value;
3541 curr_type = tok->type;
3543 type = curr_type;
3545 /* Try to avoid the overhead of creating and destroying an obstack
3546 for the common case of just one string. */
3547 if (!cp_parser_is_string_literal
3548 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3550 cp_lexer_consume_token (parser->lexer);
3552 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3553 str.len = TREE_STRING_LENGTH (string_tree);
3554 count = 1;
3556 if (curr_tok_is_userdef_p)
3558 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3559 have_suffix_p = 1;
3560 curr_type = cpp_userdef_string_remove_type (tok->type);
3562 else
3563 curr_type = tok->type;
3565 strs = &str;
3567 else
3569 gcc_obstack_init (&str_ob);
3570 count = 0;
3574 cp_lexer_consume_token (parser->lexer);
3575 count++;
3576 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3577 str.len = TREE_STRING_LENGTH (string_tree);
3579 if (curr_tok_is_userdef_p)
3581 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3582 if (have_suffix_p == 0)
3584 suffix_id = curr_suffix_id;
3585 have_suffix_p = 1;
3587 else if (have_suffix_p == 1
3588 && curr_suffix_id != suffix_id)
3590 error ("inconsistent user-defined literal suffixes"
3591 " %qD and %qD in string literal",
3592 suffix_id, curr_suffix_id);
3593 have_suffix_p = -1;
3595 curr_type = cpp_userdef_string_remove_type (tok->type);
3597 else
3598 curr_type = tok->type;
3600 if (type != curr_type)
3602 if (type == CPP_STRING)
3603 type = curr_type;
3604 else if (curr_type != CPP_STRING)
3605 error_at (tok->location,
3606 "unsupported non-standard concatenation "
3607 "of string literals");
3610 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3612 tok = cp_lexer_peek_token (parser->lexer);
3613 if (cpp_userdef_string_p (tok->type))
3615 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3616 curr_type = cpp_userdef_string_remove_type (tok->type);
3617 curr_tok_is_userdef_p = true;
3619 else
3621 string_tree = tok->u.value;
3622 curr_type = tok->type;
3623 curr_tok_is_userdef_p = false;
3626 while (cp_parser_is_string_literal (tok));
3628 strs = (cpp_string *) obstack_finish (&str_ob);
3631 if (type != CPP_STRING && !wide_ok)
3633 cp_parser_error (parser, "a wide string is invalid in this context");
3634 type = CPP_STRING;
3637 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3638 (parse_in, strs, count, &istr, type))
3640 value = build_string (istr.len, (const char *)istr.text);
3641 free (CONST_CAST (unsigned char *, istr.text));
3643 switch (type)
3645 default:
3646 case CPP_STRING:
3647 case CPP_UTF8STRING:
3648 TREE_TYPE (value) = char_array_type_node;
3649 break;
3650 case CPP_STRING16:
3651 TREE_TYPE (value) = char16_array_type_node;
3652 break;
3653 case CPP_STRING32:
3654 TREE_TYPE (value) = char32_array_type_node;
3655 break;
3656 case CPP_WSTRING:
3657 TREE_TYPE (value) = wchar_array_type_node;
3658 break;
3661 value = fix_string_type (value);
3663 if (have_suffix_p)
3665 tree literal = build_userdef_literal (suffix_id, value,
3666 OT_NONE, NULL_TREE);
3667 tok->u.value = literal;
3668 return cp_parser_userdef_string_literal (tok);
3671 else
3672 /* cpp_interpret_string has issued an error. */
3673 value = error_mark_node;
3675 if (count > 1)
3676 obstack_free (&str_ob, 0);
3678 return value;
3681 /* Look up a literal operator with the name and the exact arguments. */
3683 static tree
3684 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3686 tree decl, fns;
3687 decl = lookup_name (name);
3688 if (!decl || !is_overloaded_fn (decl))
3689 return error_mark_node;
3691 for (fns = decl; fns; fns = OVL_NEXT (fns))
3693 unsigned int ix;
3694 bool found = true;
3695 tree fn = OVL_CURRENT (fns);
3696 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3697 if (parmtypes != NULL_TREE)
3699 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3700 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3702 tree tparm = TREE_VALUE (parmtypes);
3703 tree targ = TREE_TYPE ((*args)[ix]);
3704 bool ptr = TYPE_PTR_P (tparm);
3705 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3706 if ((ptr || arr || !same_type_p (tparm, targ))
3707 && (!ptr || !arr
3708 || !same_type_p (TREE_TYPE (tparm),
3709 TREE_TYPE (targ))))
3710 found = false;
3712 if (found
3713 && ix == vec_safe_length (args)
3714 /* May be this should be sufficient_parms_p instead,
3715 depending on how exactly should user-defined literals
3716 work in presence of default arguments on the literal
3717 operator parameters. */
3718 && parmtypes == void_list_node)
3719 return fn;
3723 return error_mark_node;
3726 /* Parse a user-defined char constant. Returns a call to a user-defined
3727 literal operator taking the character as an argument. */
3729 static tree
3730 cp_parser_userdef_char_literal (cp_parser *parser)
3732 cp_token *token = cp_lexer_consume_token (parser->lexer);
3733 tree literal = token->u.value;
3734 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3735 tree value = USERDEF_LITERAL_VALUE (literal);
3736 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3737 tree decl, result;
3739 /* Build up a call to the user-defined operator */
3740 /* Lookup the name we got back from the id-expression. */
3741 vec<tree, va_gc> *args = make_tree_vector ();
3742 vec_safe_push (args, value);
3743 decl = lookup_literal_operator (name, args);
3744 if (!decl || decl == error_mark_node)
3746 error ("unable to find character literal operator %qD with %qT argument",
3747 name, TREE_TYPE (value));
3748 release_tree_vector (args);
3749 return error_mark_node;
3751 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3752 release_tree_vector (args);
3753 if (result != error_mark_node)
3754 return result;
3756 error ("unable to find character literal operator %qD with %qT argument",
3757 name, TREE_TYPE (value));
3758 return error_mark_node;
3761 /* A subroutine of cp_parser_userdef_numeric_literal to
3762 create a char... template parameter pack from a string node. */
3764 static tree
3765 make_char_string_pack (tree value)
3767 tree charvec;
3768 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3769 const char *str = TREE_STRING_POINTER (value);
3770 int i, len = TREE_STRING_LENGTH (value) - 1;
3771 tree argvec = make_tree_vec (1);
3773 /* Fill in CHARVEC with all of the parameters. */
3774 charvec = make_tree_vec (len);
3775 for (i = 0; i < len; ++i)
3776 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3778 /* Build the argument packs. */
3779 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3780 TREE_TYPE (argpack) = char_type_node;
3782 TREE_VEC_ELT (argvec, 0) = argpack;
3784 return argvec;
3787 /* A subroutine of cp_parser_userdef_numeric_literal to
3788 create a char... template parameter pack from a string node. */
3790 static tree
3791 make_string_pack (tree value)
3793 tree charvec;
3794 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3795 const char *str = TREE_STRING_POINTER (value);
3796 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3797 int len = TREE_STRING_LENGTH (value) / sz - 1;
3798 tree argvec = make_tree_vec (2);
3800 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3801 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3803 /* First template parm is character type. */
3804 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3806 /* Fill in CHARVEC with all of the parameters. */
3807 charvec = make_tree_vec (len);
3808 if (sz == 1)
3810 for (int i = 0; i < len; ++i)
3811 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, str[i]);
3813 else if (sz == 2)
3815 const uint16_t *num = (const uint16_t *)str;
3816 for (int i = 0; i < len; ++i)
3817 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]);
3819 else if (sz == 4)
3821 const uint32_t *num = (const uint32_t *)str;
3822 for (int i = 0; i < len; ++i)
3823 TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]);
3826 /* Build the argument packs. */
3827 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3828 TREE_TYPE (argpack) = str_char_type_node;
3830 TREE_VEC_ELT (argvec, 1) = argpack;
3832 return argvec;
3835 /* Parse a user-defined numeric constant. returns a call to a user-defined
3836 literal operator. */
3838 static tree
3839 cp_parser_userdef_numeric_literal (cp_parser *parser)
3841 cp_token *token = cp_lexer_consume_token (parser->lexer);
3842 tree literal = token->u.value;
3843 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3844 tree value = USERDEF_LITERAL_VALUE (literal);
3845 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3846 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3847 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3848 tree decl, result;
3849 vec<tree, va_gc> *args;
3851 /* Look for a literal operator taking the exact type of numeric argument
3852 as the literal value. */
3853 args = make_tree_vector ();
3854 vec_safe_push (args, value);
3855 decl = lookup_literal_operator (name, args);
3856 if (decl && decl != error_mark_node)
3858 result = finish_call_expr (decl, &args, false, true, tf_none);
3859 if (result != error_mark_node)
3861 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3862 warning_at (token->location, OPT_Woverflow,
3863 "integer literal exceeds range of %qT type",
3864 long_long_unsigned_type_node);
3865 else
3867 if (overflow > 0)
3868 warning_at (token->location, OPT_Woverflow,
3869 "floating literal exceeds range of %qT type",
3870 long_double_type_node);
3871 else if (overflow < 0)
3872 warning_at (token->location, OPT_Woverflow,
3873 "floating literal truncated to zero");
3875 release_tree_vector (args);
3876 return result;
3879 release_tree_vector (args);
3881 /* If the numeric argument didn't work, look for a raw literal
3882 operator taking a const char* argument consisting of the number
3883 in string format. */
3884 args = make_tree_vector ();
3885 vec_safe_push (args, num_string);
3886 decl = lookup_literal_operator (name, args);
3887 if (decl && decl != error_mark_node)
3889 result = finish_call_expr (decl, &args, false, true, tf_none);
3890 if (result != error_mark_node)
3892 release_tree_vector (args);
3893 return result;
3896 release_tree_vector (args);
3898 /* If the raw literal didn't work, look for a non-type template
3899 function with parameter pack char.... Call the function with
3900 template parameter characters representing the number. */
3901 args = make_tree_vector ();
3902 decl = lookup_literal_operator (name, args);
3903 if (decl && decl != error_mark_node)
3905 tree tmpl_args = make_char_string_pack (num_string);
3906 decl = lookup_template_function (decl, tmpl_args);
3907 result = finish_call_expr (decl, &args, false, true, tf_none);
3908 if (result != error_mark_node)
3910 release_tree_vector (args);
3911 return result;
3914 release_tree_vector (args);
3916 error ("unable to find numeric literal operator %qD", name);
3917 return error_mark_node;
3920 /* Parse a user-defined string constant. Returns a call to a user-defined
3921 literal operator taking a character pointer and the length of the string
3922 as arguments. */
3924 static tree
3925 cp_parser_userdef_string_literal (cp_token *token)
3927 tree literal = token->u.value;
3928 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3929 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3930 tree value = USERDEF_LITERAL_VALUE (literal);
3931 int len = TREE_STRING_LENGTH (value)
3932 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3933 tree decl, result;
3934 vec<tree, va_gc> *args;
3936 /* Look for a template function with typename parameter CharT
3937 and parameter pack CharT... Call the function with
3938 template parameter characters representing the string. */
3939 args = make_tree_vector ();
3940 decl = lookup_literal_operator (name, args);
3941 if (decl && decl != error_mark_node)
3943 tree tmpl_args = make_string_pack (value);
3944 decl = lookup_template_function (decl, tmpl_args);
3945 result = finish_call_expr (decl, &args, false, true, tf_none);
3946 if (result != error_mark_node)
3948 release_tree_vector (args);
3949 return result;
3952 release_tree_vector (args);
3954 /* Build up a call to the user-defined operator */
3955 /* Lookup the name we got back from the id-expression. */
3956 args = make_tree_vector ();
3957 vec_safe_push (args, value);
3958 vec_safe_push (args, build_int_cst (size_type_node, len));
3959 decl = lookup_name (name);
3960 if (!decl || decl == error_mark_node)
3962 error ("unable to find string literal operator %qD", name);
3963 release_tree_vector (args);
3964 return error_mark_node;
3966 result = finish_call_expr (decl, &args, false, true, tf_none);
3967 release_tree_vector (args);
3968 if (result != error_mark_node)
3969 return result;
3971 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3972 name, TREE_TYPE (value), size_type_node);
3973 return error_mark_node;
3977 /* Basic concepts [gram.basic] */
3979 /* Parse a translation-unit.
3981 translation-unit:
3982 declaration-seq [opt]
3984 Returns TRUE if all went well. */
3986 static bool
3987 cp_parser_translation_unit (cp_parser* parser)
3989 /* The address of the first non-permanent object on the declarator
3990 obstack. */
3991 static void *declarator_obstack_base;
3993 bool success;
3995 /* Create the declarator obstack, if necessary. */
3996 if (!cp_error_declarator)
3998 gcc_obstack_init (&declarator_obstack);
3999 /* Create the error declarator. */
4000 cp_error_declarator = make_declarator (cdk_error);
4001 /* Create the empty parameter list. */
4002 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4003 /* Remember where the base of the declarator obstack lies. */
4004 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4007 cp_parser_declaration_seq_opt (parser);
4009 /* If there are no tokens left then all went well. */
4010 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4012 /* Get rid of the token array; we don't need it any more. */
4013 cp_lexer_destroy (parser->lexer);
4014 parser->lexer = NULL;
4016 /* This file might have been a context that's implicitly extern
4017 "C". If so, pop the lang context. (Only relevant for PCH.) */
4018 if (parser->implicit_extern_c)
4020 pop_lang_context ();
4021 parser->implicit_extern_c = false;
4024 /* Finish up. */
4025 finish_translation_unit ();
4027 success = true;
4029 else
4031 cp_parser_error (parser, "expected declaration");
4032 success = false;
4035 /* Make sure the declarator obstack was fully cleaned up. */
4036 gcc_assert (obstack_next_free (&declarator_obstack)
4037 == declarator_obstack_base);
4039 /* All went well. */
4040 return success;
4043 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4044 decltype context. */
4046 static inline tsubst_flags_t
4047 complain_flags (bool decltype_p)
4049 tsubst_flags_t complain = tf_warning_or_error;
4050 if (decltype_p)
4051 complain |= tf_decltype;
4052 return complain;
4055 /* Expressions [gram.expr] */
4057 /* Parse a primary-expression.
4059 primary-expression:
4060 literal
4061 this
4062 ( expression )
4063 id-expression
4065 GNU Extensions:
4067 primary-expression:
4068 ( compound-statement )
4069 __builtin_va_arg ( assignment-expression , type-id )
4070 __builtin_offsetof ( type-id , offsetof-expression )
4072 C++ Extensions:
4073 __has_nothrow_assign ( type-id )
4074 __has_nothrow_constructor ( type-id )
4075 __has_nothrow_copy ( type-id )
4076 __has_trivial_assign ( type-id )
4077 __has_trivial_constructor ( type-id )
4078 __has_trivial_copy ( type-id )
4079 __has_trivial_destructor ( type-id )
4080 __has_virtual_destructor ( type-id )
4081 __is_abstract ( type-id )
4082 __is_base_of ( type-id , type-id )
4083 __is_class ( type-id )
4084 __is_convertible_to ( type-id , type-id )
4085 __is_empty ( type-id )
4086 __is_enum ( type-id )
4087 __is_final ( type-id )
4088 __is_literal_type ( type-id )
4089 __is_pod ( type-id )
4090 __is_polymorphic ( type-id )
4091 __is_std_layout ( type-id )
4092 __is_trivial ( type-id )
4093 __is_union ( type-id )
4095 Objective-C++ Extension:
4097 primary-expression:
4098 objc-expression
4100 literal:
4101 __null
4103 ADDRESS_P is true iff this expression was immediately preceded by
4104 "&" and therefore might denote a pointer-to-member. CAST_P is true
4105 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4106 true iff this expression is a template argument.
4108 Returns a representation of the expression. Upon return, *IDK
4109 indicates what kind of id-expression (if any) was present. */
4111 static tree
4112 cp_parser_primary_expression (cp_parser *parser,
4113 bool address_p,
4114 bool cast_p,
4115 bool template_arg_p,
4116 bool decltype_p,
4117 cp_id_kind *idk)
4119 cp_token *token = NULL;
4121 /* Assume the primary expression is not an id-expression. */
4122 *idk = CP_ID_KIND_NONE;
4124 /* Peek at the next token. */
4125 token = cp_lexer_peek_token (parser->lexer);
4126 switch (token->type)
4128 /* literal:
4129 integer-literal
4130 character-literal
4131 floating-literal
4132 string-literal
4133 boolean-literal
4134 pointer-literal
4135 user-defined-literal */
4136 case CPP_CHAR:
4137 case CPP_CHAR16:
4138 case CPP_CHAR32:
4139 case CPP_WCHAR:
4140 case CPP_NUMBER:
4141 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4142 return cp_parser_userdef_numeric_literal (parser);
4143 token = cp_lexer_consume_token (parser->lexer);
4144 if (TREE_CODE (token->u.value) == FIXED_CST)
4146 error_at (token->location,
4147 "fixed-point types not supported in C++");
4148 return error_mark_node;
4150 /* Floating-point literals are only allowed in an integral
4151 constant expression if they are cast to an integral or
4152 enumeration type. */
4153 if (TREE_CODE (token->u.value) == REAL_CST
4154 && parser->integral_constant_expression_p
4155 && pedantic)
4157 /* CAST_P will be set even in invalid code like "int(2.7 +
4158 ...)". Therefore, we have to check that the next token
4159 is sure to end the cast. */
4160 if (cast_p)
4162 cp_token *next_token;
4164 next_token = cp_lexer_peek_token (parser->lexer);
4165 if (/* The comma at the end of an
4166 enumerator-definition. */
4167 next_token->type != CPP_COMMA
4168 /* The curly brace at the end of an enum-specifier. */
4169 && next_token->type != CPP_CLOSE_BRACE
4170 /* The end of a statement. */
4171 && next_token->type != CPP_SEMICOLON
4172 /* The end of the cast-expression. */
4173 && next_token->type != CPP_CLOSE_PAREN
4174 /* The end of an array bound. */
4175 && next_token->type != CPP_CLOSE_SQUARE
4176 /* The closing ">" in a template-argument-list. */
4177 && (next_token->type != CPP_GREATER
4178 || parser->greater_than_is_operator_p)
4179 /* C++0x only: A ">>" treated like two ">" tokens,
4180 in a template-argument-list. */
4181 && (next_token->type != CPP_RSHIFT
4182 || (cxx_dialect == cxx98)
4183 || parser->greater_than_is_operator_p))
4184 cast_p = false;
4187 /* If we are within a cast, then the constraint that the
4188 cast is to an integral or enumeration type will be
4189 checked at that point. If we are not within a cast, then
4190 this code is invalid. */
4191 if (!cast_p)
4192 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4194 return token->u.value;
4196 case CPP_CHAR_USERDEF:
4197 case CPP_CHAR16_USERDEF:
4198 case CPP_CHAR32_USERDEF:
4199 case CPP_WCHAR_USERDEF:
4200 return cp_parser_userdef_char_literal (parser);
4202 case CPP_STRING:
4203 case CPP_STRING16:
4204 case CPP_STRING32:
4205 case CPP_WSTRING:
4206 case CPP_UTF8STRING:
4207 case CPP_STRING_USERDEF:
4208 case CPP_STRING16_USERDEF:
4209 case CPP_STRING32_USERDEF:
4210 case CPP_WSTRING_USERDEF:
4211 case CPP_UTF8STRING_USERDEF:
4212 /* ??? Should wide strings be allowed when parser->translate_strings_p
4213 is false (i.e. in attributes)? If not, we can kill the third
4214 argument to cp_parser_string_literal. */
4215 return cp_parser_string_literal (parser,
4216 parser->translate_strings_p,
4217 true);
4219 case CPP_OPEN_PAREN:
4221 tree expr;
4222 bool saved_greater_than_is_operator_p;
4224 /* Consume the `('. */
4225 cp_lexer_consume_token (parser->lexer);
4226 /* Within a parenthesized expression, a `>' token is always
4227 the greater-than operator. */
4228 saved_greater_than_is_operator_p
4229 = parser->greater_than_is_operator_p;
4230 parser->greater_than_is_operator_p = true;
4231 /* If we see `( { ' then we are looking at the beginning of
4232 a GNU statement-expression. */
4233 if (cp_parser_allow_gnu_extensions_p (parser)
4234 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4236 /* Statement-expressions are not allowed by the standard. */
4237 pedwarn (token->location, OPT_Wpedantic,
4238 "ISO C++ forbids braced-groups within expressions");
4240 /* And they're not allowed outside of a function-body; you
4241 cannot, for example, write:
4243 int i = ({ int j = 3; j + 1; });
4245 at class or namespace scope. */
4246 if (!parser->in_function_body
4247 || parser->in_template_argument_list_p)
4249 error_at (token->location,
4250 "statement-expressions are not allowed outside "
4251 "functions nor in template-argument lists");
4252 cp_parser_skip_to_end_of_block_or_statement (parser);
4253 expr = error_mark_node;
4255 else
4257 /* Start the statement-expression. */
4258 expr = begin_stmt_expr ();
4259 /* Parse the compound-statement. */
4260 cp_parser_compound_statement (parser, expr, false, false);
4261 /* Finish up. */
4262 expr = finish_stmt_expr (expr, false);
4265 else
4267 /* Parse the parenthesized expression. */
4268 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4269 /* Let the front end know that this expression was
4270 enclosed in parentheses. This matters in case, for
4271 example, the expression is of the form `A::B', since
4272 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4273 not. */
4274 expr = finish_parenthesized_expr (expr);
4275 /* DR 705: Wrapping an unqualified name in parentheses
4276 suppresses arg-dependent lookup. We want to pass back
4277 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4278 (c++/37862), but none of the others. */
4279 if (*idk != CP_ID_KIND_QUALIFIED)
4280 *idk = CP_ID_KIND_NONE;
4282 /* The `>' token might be the end of a template-id or
4283 template-parameter-list now. */
4284 parser->greater_than_is_operator_p
4285 = saved_greater_than_is_operator_p;
4286 /* Consume the `)'. */
4287 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4288 cp_parser_skip_to_end_of_statement (parser);
4290 return expr;
4293 case CPP_OPEN_SQUARE:
4294 if (c_dialect_objc ())
4295 /* We have an Objective-C++ message. */
4296 return cp_parser_objc_expression (parser);
4298 tree lam = cp_parser_lambda_expression (parser);
4299 /* Don't warn about a failed tentative parse. */
4300 if (cp_parser_error_occurred (parser))
4301 return error_mark_node;
4302 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4303 return lam;
4306 case CPP_OBJC_STRING:
4307 if (c_dialect_objc ())
4308 /* We have an Objective-C++ string literal. */
4309 return cp_parser_objc_expression (parser);
4310 cp_parser_error (parser, "expected primary-expression");
4311 return error_mark_node;
4313 case CPP_KEYWORD:
4314 switch (token->keyword)
4316 /* These two are the boolean literals. */
4317 case RID_TRUE:
4318 cp_lexer_consume_token (parser->lexer);
4319 return boolean_true_node;
4320 case RID_FALSE:
4321 cp_lexer_consume_token (parser->lexer);
4322 return boolean_false_node;
4324 /* The `__null' literal. */
4325 case RID_NULL:
4326 cp_lexer_consume_token (parser->lexer);
4327 return null_node;
4329 /* The `nullptr' literal. */
4330 case RID_NULLPTR:
4331 cp_lexer_consume_token (parser->lexer);
4332 return nullptr_node;
4334 /* Recognize the `this' keyword. */
4335 case RID_THIS:
4336 cp_lexer_consume_token (parser->lexer);
4337 if (parser->local_variables_forbidden_p)
4339 error_at (token->location,
4340 "%<this%> may not be used in this context");
4341 return error_mark_node;
4343 /* Pointers cannot appear in constant-expressions. */
4344 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4345 return error_mark_node;
4346 return finish_this_expr ();
4348 /* The `operator' keyword can be the beginning of an
4349 id-expression. */
4350 case RID_OPERATOR:
4351 goto id_expression;
4353 case RID_FUNCTION_NAME:
4354 case RID_PRETTY_FUNCTION_NAME:
4355 case RID_C99_FUNCTION_NAME:
4357 non_integral_constant name;
4359 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4360 __func__ are the names of variables -- but they are
4361 treated specially. Therefore, they are handled here,
4362 rather than relying on the generic id-expression logic
4363 below. Grammatically, these names are id-expressions.
4365 Consume the token. */
4366 token = cp_lexer_consume_token (parser->lexer);
4368 switch (token->keyword)
4370 case RID_FUNCTION_NAME:
4371 name = NIC_FUNC_NAME;
4372 break;
4373 case RID_PRETTY_FUNCTION_NAME:
4374 name = NIC_PRETTY_FUNC;
4375 break;
4376 case RID_C99_FUNCTION_NAME:
4377 name = NIC_C99_FUNC;
4378 break;
4379 default:
4380 gcc_unreachable ();
4383 if (cp_parser_non_integral_constant_expression (parser, name))
4384 return error_mark_node;
4386 /* Look up the name. */
4387 return finish_fname (token->u.value);
4390 case RID_VA_ARG:
4392 tree expression;
4393 tree type;
4394 source_location type_location;
4396 /* The `__builtin_va_arg' construct is used to handle
4397 `va_arg'. Consume the `__builtin_va_arg' token. */
4398 cp_lexer_consume_token (parser->lexer);
4399 /* Look for the opening `('. */
4400 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4401 /* Now, parse the assignment-expression. */
4402 expression = cp_parser_assignment_expression (parser,
4403 /*cast_p=*/false, NULL);
4404 /* Look for the `,'. */
4405 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4406 type_location = cp_lexer_peek_token (parser->lexer)->location;
4407 /* Parse the type-id. */
4408 type = cp_parser_type_id (parser);
4409 /* Look for the closing `)'. */
4410 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4411 /* Using `va_arg' in a constant-expression is not
4412 allowed. */
4413 if (cp_parser_non_integral_constant_expression (parser,
4414 NIC_VA_ARG))
4415 return error_mark_node;
4416 return build_x_va_arg (type_location, expression, type);
4419 case RID_OFFSETOF:
4420 return cp_parser_builtin_offsetof (parser);
4422 case RID_HAS_NOTHROW_ASSIGN:
4423 case RID_HAS_NOTHROW_CONSTRUCTOR:
4424 case RID_HAS_NOTHROW_COPY:
4425 case RID_HAS_TRIVIAL_ASSIGN:
4426 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4427 case RID_HAS_TRIVIAL_COPY:
4428 case RID_HAS_TRIVIAL_DESTRUCTOR:
4429 case RID_HAS_VIRTUAL_DESTRUCTOR:
4430 case RID_IS_ABSTRACT:
4431 case RID_IS_BASE_OF:
4432 case RID_IS_CLASS:
4433 case RID_IS_CONVERTIBLE_TO:
4434 case RID_IS_EMPTY:
4435 case RID_IS_ENUM:
4436 case RID_IS_FINAL:
4437 case RID_IS_LITERAL_TYPE:
4438 case RID_IS_POD:
4439 case RID_IS_POLYMORPHIC:
4440 case RID_IS_STD_LAYOUT:
4441 case RID_IS_TRIVIAL:
4442 case RID_IS_UNION:
4443 return cp_parser_trait_expr (parser, token->keyword);
4445 /* Objective-C++ expressions. */
4446 case RID_AT_ENCODE:
4447 case RID_AT_PROTOCOL:
4448 case RID_AT_SELECTOR:
4449 return cp_parser_objc_expression (parser);
4451 case RID_TEMPLATE:
4452 if (parser->in_function_body
4453 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4454 == CPP_LESS))
4456 error_at (token->location,
4457 "a template declaration cannot appear at block scope");
4458 cp_parser_skip_to_end_of_block_or_statement (parser);
4459 return error_mark_node;
4461 default:
4462 cp_parser_error (parser, "expected primary-expression");
4463 return error_mark_node;
4466 /* An id-expression can start with either an identifier, a
4467 `::' as the beginning of a qualified-id, or the "operator"
4468 keyword. */
4469 case CPP_NAME:
4470 case CPP_SCOPE:
4471 case CPP_TEMPLATE_ID:
4472 case CPP_NESTED_NAME_SPECIFIER:
4474 tree id_expression;
4475 tree decl;
4476 const char *error_msg;
4477 bool template_p;
4478 bool done;
4479 cp_token *id_expr_token;
4481 id_expression:
4482 /* Parse the id-expression. */
4483 id_expression
4484 = cp_parser_id_expression (parser,
4485 /*template_keyword_p=*/false,
4486 /*check_dependency_p=*/true,
4487 &template_p,
4488 /*declarator_p=*/false,
4489 /*optional_p=*/false);
4490 if (id_expression == error_mark_node)
4491 return error_mark_node;
4492 id_expr_token = token;
4493 token = cp_lexer_peek_token (parser->lexer);
4494 done = (token->type != CPP_OPEN_SQUARE
4495 && token->type != CPP_OPEN_PAREN
4496 && token->type != CPP_DOT
4497 && token->type != CPP_DEREF
4498 && token->type != CPP_PLUS_PLUS
4499 && token->type != CPP_MINUS_MINUS);
4500 /* If we have a template-id, then no further lookup is
4501 required. If the template-id was for a template-class, we
4502 will sometimes have a TYPE_DECL at this point. */
4503 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4504 || TREE_CODE (id_expression) == TYPE_DECL)
4505 decl = id_expression;
4506 /* Look up the name. */
4507 else
4509 tree ambiguous_decls;
4511 /* If we already know that this lookup is ambiguous, then
4512 we've already issued an error message; there's no reason
4513 to check again. */
4514 if (id_expr_token->type == CPP_NAME
4515 && id_expr_token->ambiguous_p)
4517 cp_parser_simulate_error (parser);
4518 return error_mark_node;
4521 decl = cp_parser_lookup_name (parser, id_expression,
4522 none_type,
4523 template_p,
4524 /*is_namespace=*/false,
4525 /*check_dependency=*/true,
4526 &ambiguous_decls,
4527 id_expr_token->location);
4528 /* If the lookup was ambiguous, an error will already have
4529 been issued. */
4530 if (ambiguous_decls)
4531 return error_mark_node;
4533 /* In Objective-C++, we may have an Objective-C 2.0
4534 dot-syntax for classes here. */
4535 if (c_dialect_objc ()
4536 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4537 && TREE_CODE (decl) == TYPE_DECL
4538 && objc_is_class_name (decl))
4540 tree component;
4541 cp_lexer_consume_token (parser->lexer);
4542 component = cp_parser_identifier (parser);
4543 if (component == error_mark_node)
4544 return error_mark_node;
4546 return objc_build_class_component_ref (id_expression, component);
4549 /* In Objective-C++, an instance variable (ivar) may be preferred
4550 to whatever cp_parser_lookup_name() found. */
4551 decl = objc_lookup_ivar (decl, id_expression);
4553 /* If name lookup gives us a SCOPE_REF, then the
4554 qualifying scope was dependent. */
4555 if (TREE_CODE (decl) == SCOPE_REF)
4557 /* At this point, we do not know if DECL is a valid
4558 integral constant expression. We assume that it is
4559 in fact such an expression, so that code like:
4561 template <int N> struct A {
4562 int a[B<N>::i];
4565 is accepted. At template-instantiation time, we
4566 will check that B<N>::i is actually a constant. */
4567 return decl;
4569 /* Check to see if DECL is a local variable in a context
4570 where that is forbidden. */
4571 if (parser->local_variables_forbidden_p
4572 && local_variable_p (decl))
4574 /* It might be that we only found DECL because we are
4575 trying to be generous with pre-ISO scoping rules.
4576 For example, consider:
4578 int i;
4579 void g() {
4580 for (int i = 0; i < 10; ++i) {}
4581 extern void f(int j = i);
4584 Here, name look up will originally find the out
4585 of scope `i'. We need to issue a warning message,
4586 but then use the global `i'. */
4587 decl = check_for_out_of_scope_variable (decl);
4588 if (local_variable_p (decl))
4590 error_at (id_expr_token->location,
4591 "local variable %qD may not appear in this context",
4592 decl);
4593 return error_mark_node;
4598 decl = (finish_id_expression
4599 (id_expression, decl, parser->scope,
4600 idk,
4601 parser->integral_constant_expression_p,
4602 parser->allow_non_integral_constant_expression_p,
4603 &parser->non_integral_constant_expression_p,
4604 template_p, done, address_p,
4605 template_arg_p,
4606 &error_msg,
4607 id_expr_token->location));
4608 if (error_msg)
4609 cp_parser_error (parser, error_msg);
4610 return decl;
4613 /* Anything else is an error. */
4614 default:
4615 cp_parser_error (parser, "expected primary-expression");
4616 return error_mark_node;
4620 static inline tree
4621 cp_parser_primary_expression (cp_parser *parser,
4622 bool address_p,
4623 bool cast_p,
4624 bool template_arg_p,
4625 cp_id_kind *idk)
4627 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4628 /*decltype*/false, idk);
4631 /* Parse an id-expression.
4633 id-expression:
4634 unqualified-id
4635 qualified-id
4637 qualified-id:
4638 :: [opt] nested-name-specifier template [opt] unqualified-id
4639 :: identifier
4640 :: operator-function-id
4641 :: template-id
4643 Return a representation of the unqualified portion of the
4644 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4645 a `::' or nested-name-specifier.
4647 Often, if the id-expression was a qualified-id, the caller will
4648 want to make a SCOPE_REF to represent the qualified-id. This
4649 function does not do this in order to avoid wastefully creating
4650 SCOPE_REFs when they are not required.
4652 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4653 `template' keyword.
4655 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4656 uninstantiated templates.
4658 If *TEMPLATE_P is non-NULL, it is set to true iff the
4659 `template' keyword is used to explicitly indicate that the entity
4660 named is a template.
4662 If DECLARATOR_P is true, the id-expression is appearing as part of
4663 a declarator, rather than as part of an expression. */
4665 static tree
4666 cp_parser_id_expression (cp_parser *parser,
4667 bool template_keyword_p,
4668 bool check_dependency_p,
4669 bool *template_p,
4670 bool declarator_p,
4671 bool optional_p)
4673 bool global_scope_p;
4674 bool nested_name_specifier_p;
4676 /* Assume the `template' keyword was not used. */
4677 if (template_p)
4678 *template_p = template_keyword_p;
4680 /* Look for the optional `::' operator. */
4681 global_scope_p
4682 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4683 != NULL_TREE);
4684 /* Look for the optional nested-name-specifier. */
4685 nested_name_specifier_p
4686 = (cp_parser_nested_name_specifier_opt (parser,
4687 /*typename_keyword_p=*/false,
4688 check_dependency_p,
4689 /*type_p=*/false,
4690 declarator_p)
4691 != NULL_TREE);
4692 /* If there is a nested-name-specifier, then we are looking at
4693 the first qualified-id production. */
4694 if (nested_name_specifier_p)
4696 tree saved_scope;
4697 tree saved_object_scope;
4698 tree saved_qualifying_scope;
4699 tree unqualified_id;
4700 bool is_template;
4702 /* See if the next token is the `template' keyword. */
4703 if (!template_p)
4704 template_p = &is_template;
4705 *template_p = cp_parser_optional_template_keyword (parser);
4706 /* Name lookup we do during the processing of the
4707 unqualified-id might obliterate SCOPE. */
4708 saved_scope = parser->scope;
4709 saved_object_scope = parser->object_scope;
4710 saved_qualifying_scope = parser->qualifying_scope;
4711 /* Process the final unqualified-id. */
4712 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4713 check_dependency_p,
4714 declarator_p,
4715 /*optional_p=*/false);
4716 /* Restore the SAVED_SCOPE for our caller. */
4717 parser->scope = saved_scope;
4718 parser->object_scope = saved_object_scope;
4719 parser->qualifying_scope = saved_qualifying_scope;
4721 return unqualified_id;
4723 /* Otherwise, if we are in global scope, then we are looking at one
4724 of the other qualified-id productions. */
4725 else if (global_scope_p)
4727 cp_token *token;
4728 tree id;
4730 /* Peek at the next token. */
4731 token = cp_lexer_peek_token (parser->lexer);
4733 /* If it's an identifier, and the next token is not a "<", then
4734 we can avoid the template-id case. This is an optimization
4735 for this common case. */
4736 if (token->type == CPP_NAME
4737 && !cp_parser_nth_token_starts_template_argument_list_p
4738 (parser, 2))
4739 return cp_parser_identifier (parser);
4741 cp_parser_parse_tentatively (parser);
4742 /* Try a template-id. */
4743 id = cp_parser_template_id (parser,
4744 /*template_keyword_p=*/false,
4745 /*check_dependency_p=*/true,
4746 none_type,
4747 declarator_p);
4748 /* If that worked, we're done. */
4749 if (cp_parser_parse_definitely (parser))
4750 return id;
4752 /* Peek at the next token. (Changes in the token buffer may
4753 have invalidated the pointer obtained above.) */
4754 token = cp_lexer_peek_token (parser->lexer);
4756 switch (token->type)
4758 case CPP_NAME:
4759 return cp_parser_identifier (parser);
4761 case CPP_KEYWORD:
4762 if (token->keyword == RID_OPERATOR)
4763 return cp_parser_operator_function_id (parser);
4764 /* Fall through. */
4766 default:
4767 cp_parser_error (parser, "expected id-expression");
4768 return error_mark_node;
4771 else
4772 return cp_parser_unqualified_id (parser, template_keyword_p,
4773 /*check_dependency_p=*/true,
4774 declarator_p,
4775 optional_p);
4778 /* Parse an unqualified-id.
4780 unqualified-id:
4781 identifier
4782 operator-function-id
4783 conversion-function-id
4784 ~ class-name
4785 template-id
4787 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4788 keyword, in a construct like `A::template ...'.
4790 Returns a representation of unqualified-id. For the `identifier'
4791 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4792 production a BIT_NOT_EXPR is returned; the operand of the
4793 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4794 other productions, see the documentation accompanying the
4795 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4796 names are looked up in uninstantiated templates. If DECLARATOR_P
4797 is true, the unqualified-id is appearing as part of a declarator,
4798 rather than as part of an expression. */
4800 static tree
4801 cp_parser_unqualified_id (cp_parser* parser,
4802 bool template_keyword_p,
4803 bool check_dependency_p,
4804 bool declarator_p,
4805 bool optional_p)
4807 cp_token *token;
4809 /* Peek at the next token. */
4810 token = cp_lexer_peek_token (parser->lexer);
4812 switch (token->type)
4814 case CPP_NAME:
4816 tree id;
4818 /* We don't know yet whether or not this will be a
4819 template-id. */
4820 cp_parser_parse_tentatively (parser);
4821 /* Try a template-id. */
4822 id = cp_parser_template_id (parser, template_keyword_p,
4823 check_dependency_p,
4824 none_type,
4825 declarator_p);
4826 /* If it worked, we're done. */
4827 if (cp_parser_parse_definitely (parser))
4828 return id;
4829 /* Otherwise, it's an ordinary identifier. */
4830 return cp_parser_identifier (parser);
4833 case CPP_TEMPLATE_ID:
4834 return cp_parser_template_id (parser, template_keyword_p,
4835 check_dependency_p,
4836 none_type,
4837 declarator_p);
4839 case CPP_COMPL:
4841 tree type_decl;
4842 tree qualifying_scope;
4843 tree object_scope;
4844 tree scope;
4845 bool done;
4847 /* Consume the `~' token. */
4848 cp_lexer_consume_token (parser->lexer);
4849 /* Parse the class-name. The standard, as written, seems to
4850 say that:
4852 template <typename T> struct S { ~S (); };
4853 template <typename T> S<T>::~S() {}
4855 is invalid, since `~' must be followed by a class-name, but
4856 `S<T>' is dependent, and so not known to be a class.
4857 That's not right; we need to look in uninstantiated
4858 templates. A further complication arises from:
4860 template <typename T> void f(T t) {
4861 t.T::~T();
4864 Here, it is not possible to look up `T' in the scope of `T'
4865 itself. We must look in both the current scope, and the
4866 scope of the containing complete expression.
4868 Yet another issue is:
4870 struct S {
4871 int S;
4872 ~S();
4875 S::~S() {}
4877 The standard does not seem to say that the `S' in `~S'
4878 should refer to the type `S' and not the data member
4879 `S::S'. */
4881 /* DR 244 says that we look up the name after the "~" in the
4882 same scope as we looked up the qualifying name. That idea
4883 isn't fully worked out; it's more complicated than that. */
4884 scope = parser->scope;
4885 object_scope = parser->object_scope;
4886 qualifying_scope = parser->qualifying_scope;
4888 /* Check for invalid scopes. */
4889 if (scope == error_mark_node)
4891 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4892 cp_lexer_consume_token (parser->lexer);
4893 return error_mark_node;
4895 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4897 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4898 error_at (token->location,
4899 "scope %qT before %<~%> is not a class-name",
4900 scope);
4901 cp_parser_simulate_error (parser);
4902 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4903 cp_lexer_consume_token (parser->lexer);
4904 return error_mark_node;
4906 gcc_assert (!scope || TYPE_P (scope));
4908 /* If the name is of the form "X::~X" it's OK even if X is a
4909 typedef. */
4910 token = cp_lexer_peek_token (parser->lexer);
4911 if (scope
4912 && token->type == CPP_NAME
4913 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4914 != CPP_LESS)
4915 && (token->u.value == TYPE_IDENTIFIER (scope)
4916 || (CLASS_TYPE_P (scope)
4917 && constructor_name_p (token->u.value, scope))))
4919 cp_lexer_consume_token (parser->lexer);
4920 return build_nt (BIT_NOT_EXPR, scope);
4923 /* ~auto means the destructor of whatever the object is. */
4924 if (cp_parser_is_keyword (token, RID_AUTO))
4926 if (cxx_dialect < cxx1y)
4927 pedwarn (input_location, 0,
4928 "%<~auto%> only available with "
4929 "-std=c++1y or -std=gnu++1y");
4930 cp_lexer_consume_token (parser->lexer);
4931 return build_nt (BIT_NOT_EXPR, make_auto ());
4934 /* If there was an explicit qualification (S::~T), first look
4935 in the scope given by the qualification (i.e., S).
4937 Note: in the calls to cp_parser_class_name below we pass
4938 typename_type so that lookup finds the injected-class-name
4939 rather than the constructor. */
4940 done = false;
4941 type_decl = NULL_TREE;
4942 if (scope)
4944 cp_parser_parse_tentatively (parser);
4945 type_decl = cp_parser_class_name (parser,
4946 /*typename_keyword_p=*/false,
4947 /*template_keyword_p=*/false,
4948 typename_type,
4949 /*check_dependency=*/false,
4950 /*class_head_p=*/false,
4951 declarator_p);
4952 if (cp_parser_parse_definitely (parser))
4953 done = true;
4955 /* In "N::S::~S", look in "N" as well. */
4956 if (!done && scope && qualifying_scope)
4958 cp_parser_parse_tentatively (parser);
4959 parser->scope = qualifying_scope;
4960 parser->object_scope = NULL_TREE;
4961 parser->qualifying_scope = NULL_TREE;
4962 type_decl
4963 = cp_parser_class_name (parser,
4964 /*typename_keyword_p=*/false,
4965 /*template_keyword_p=*/false,
4966 typename_type,
4967 /*check_dependency=*/false,
4968 /*class_head_p=*/false,
4969 declarator_p);
4970 if (cp_parser_parse_definitely (parser))
4971 done = true;
4973 /* In "p->S::~T", look in the scope given by "*p" as well. */
4974 else if (!done && object_scope)
4976 cp_parser_parse_tentatively (parser);
4977 parser->scope = object_scope;
4978 parser->object_scope = NULL_TREE;
4979 parser->qualifying_scope = NULL_TREE;
4980 type_decl
4981 = cp_parser_class_name (parser,
4982 /*typename_keyword_p=*/false,
4983 /*template_keyword_p=*/false,
4984 typename_type,
4985 /*check_dependency=*/false,
4986 /*class_head_p=*/false,
4987 declarator_p);
4988 if (cp_parser_parse_definitely (parser))
4989 done = true;
4991 /* Look in the surrounding context. */
4992 if (!done)
4994 parser->scope = NULL_TREE;
4995 parser->object_scope = NULL_TREE;
4996 parser->qualifying_scope = NULL_TREE;
4997 if (processing_template_decl)
4998 cp_parser_parse_tentatively (parser);
4999 type_decl
5000 = cp_parser_class_name (parser,
5001 /*typename_keyword_p=*/false,
5002 /*template_keyword_p=*/false,
5003 typename_type,
5004 /*check_dependency=*/false,
5005 /*class_head_p=*/false,
5006 declarator_p);
5007 if (processing_template_decl
5008 && ! cp_parser_parse_definitely (parser))
5010 /* We couldn't find a type with this name, so just accept
5011 it and check for a match at instantiation time. */
5012 type_decl = cp_parser_identifier (parser);
5013 if (type_decl != error_mark_node)
5014 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5015 return type_decl;
5018 /* If an error occurred, assume that the name of the
5019 destructor is the same as the name of the qualifying
5020 class. That allows us to keep parsing after running
5021 into ill-formed destructor names. */
5022 if (type_decl == error_mark_node && scope)
5023 return build_nt (BIT_NOT_EXPR, scope);
5024 else if (type_decl == error_mark_node)
5025 return error_mark_node;
5027 /* Check that destructor name and scope match. */
5028 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5030 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5031 error_at (token->location,
5032 "declaration of %<~%T%> as member of %qT",
5033 type_decl, scope);
5034 cp_parser_simulate_error (parser);
5035 return error_mark_node;
5038 /* [class.dtor]
5040 A typedef-name that names a class shall not be used as the
5041 identifier in the declarator for a destructor declaration. */
5042 if (declarator_p
5043 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5044 && !DECL_SELF_REFERENCE_P (type_decl)
5045 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5046 error_at (token->location,
5047 "typedef-name %qD used as destructor declarator",
5048 type_decl);
5050 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5053 case CPP_KEYWORD:
5054 if (token->keyword == RID_OPERATOR)
5056 tree id;
5058 /* This could be a template-id, so we try that first. */
5059 cp_parser_parse_tentatively (parser);
5060 /* Try a template-id. */
5061 id = cp_parser_template_id (parser, template_keyword_p,
5062 /*check_dependency_p=*/true,
5063 none_type,
5064 declarator_p);
5065 /* If that worked, we're done. */
5066 if (cp_parser_parse_definitely (parser))
5067 return id;
5068 /* We still don't know whether we're looking at an
5069 operator-function-id or a conversion-function-id. */
5070 cp_parser_parse_tentatively (parser);
5071 /* Try an operator-function-id. */
5072 id = cp_parser_operator_function_id (parser);
5073 /* If that didn't work, try a conversion-function-id. */
5074 if (!cp_parser_parse_definitely (parser))
5075 id = cp_parser_conversion_function_id (parser);
5076 else if (UDLIT_OPER_P (id))
5078 /* 17.6.3.3.5 */
5079 const char *name = UDLIT_OP_SUFFIX (id);
5080 if (name[0] != '_' && !in_system_header && declarator_p)
5081 warning (0, "literal operator suffixes not preceded by %<_%>"
5082 " are reserved for future standardization");
5085 return id;
5087 /* Fall through. */
5089 default:
5090 if (optional_p)
5091 return NULL_TREE;
5092 cp_parser_error (parser, "expected unqualified-id");
5093 return error_mark_node;
5097 /* Parse an (optional) nested-name-specifier.
5099 nested-name-specifier: [C++98]
5100 class-or-namespace-name :: nested-name-specifier [opt]
5101 class-or-namespace-name :: template nested-name-specifier [opt]
5103 nested-name-specifier: [C++0x]
5104 type-name ::
5105 namespace-name ::
5106 nested-name-specifier identifier ::
5107 nested-name-specifier template [opt] simple-template-id ::
5109 PARSER->SCOPE should be set appropriately before this function is
5110 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5111 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5112 in name lookups.
5114 Sets PARSER->SCOPE to the class (TYPE) or namespace
5115 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5116 it unchanged if there is no nested-name-specifier. Returns the new
5117 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5119 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5120 part of a declaration and/or decl-specifier. */
5122 static tree
5123 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5124 bool typename_keyword_p,
5125 bool check_dependency_p,
5126 bool type_p,
5127 bool is_declaration)
5129 bool success = false;
5130 cp_token_position start = 0;
5131 cp_token *token;
5133 /* Remember where the nested-name-specifier starts. */
5134 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5136 start = cp_lexer_token_position (parser->lexer, false);
5137 push_deferring_access_checks (dk_deferred);
5140 while (true)
5142 tree new_scope;
5143 tree old_scope;
5144 tree saved_qualifying_scope;
5145 bool template_keyword_p;
5147 /* Spot cases that cannot be the beginning of a
5148 nested-name-specifier. */
5149 token = cp_lexer_peek_token (parser->lexer);
5151 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5152 the already parsed nested-name-specifier. */
5153 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5155 /* Grab the nested-name-specifier and continue the loop. */
5156 cp_parser_pre_parsed_nested_name_specifier (parser);
5157 /* If we originally encountered this nested-name-specifier
5158 with IS_DECLARATION set to false, we will not have
5159 resolved TYPENAME_TYPEs, so we must do so here. */
5160 if (is_declaration
5161 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5163 new_scope = resolve_typename_type (parser->scope,
5164 /*only_current_p=*/false);
5165 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5166 parser->scope = new_scope;
5168 success = true;
5169 continue;
5172 /* Spot cases that cannot be the beginning of a
5173 nested-name-specifier. On the second and subsequent times
5174 through the loop, we look for the `template' keyword. */
5175 if (success && token->keyword == RID_TEMPLATE)
5177 /* A template-id can start a nested-name-specifier. */
5178 else if (token->type == CPP_TEMPLATE_ID)
5180 /* DR 743: decltype can be used in a nested-name-specifier. */
5181 else if (token_is_decltype (token))
5183 else
5185 /* If the next token is not an identifier, then it is
5186 definitely not a type-name or namespace-name. */
5187 if (token->type != CPP_NAME)
5188 break;
5189 /* If the following token is neither a `<' (to begin a
5190 template-id), nor a `::', then we are not looking at a
5191 nested-name-specifier. */
5192 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5194 if (token->type == CPP_COLON
5195 && parser->colon_corrects_to_scope_p
5196 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5198 error_at (token->location,
5199 "found %<:%> in nested-name-specifier, expected %<::%>");
5200 token->type = CPP_SCOPE;
5203 if (token->type != CPP_SCOPE
5204 && !cp_parser_nth_token_starts_template_argument_list_p
5205 (parser, 2))
5206 break;
5209 /* The nested-name-specifier is optional, so we parse
5210 tentatively. */
5211 cp_parser_parse_tentatively (parser);
5213 /* Look for the optional `template' keyword, if this isn't the
5214 first time through the loop. */
5215 if (success)
5216 template_keyword_p = cp_parser_optional_template_keyword (parser);
5217 else
5218 template_keyword_p = false;
5220 /* Save the old scope since the name lookup we are about to do
5221 might destroy it. */
5222 old_scope = parser->scope;
5223 saved_qualifying_scope = parser->qualifying_scope;
5224 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5225 look up names in "X<T>::I" in order to determine that "Y" is
5226 a template. So, if we have a typename at this point, we make
5227 an effort to look through it. */
5228 if (is_declaration
5229 && !typename_keyword_p
5230 && parser->scope
5231 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5232 parser->scope = resolve_typename_type (parser->scope,
5233 /*only_current_p=*/false);
5234 /* Parse the qualifying entity. */
5235 new_scope
5236 = cp_parser_qualifying_entity (parser,
5237 typename_keyword_p,
5238 template_keyword_p,
5239 check_dependency_p,
5240 type_p,
5241 is_declaration);
5242 /* Look for the `::' token. */
5243 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5245 /* If we found what we wanted, we keep going; otherwise, we're
5246 done. */
5247 if (!cp_parser_parse_definitely (parser))
5249 bool error_p = false;
5251 /* Restore the OLD_SCOPE since it was valid before the
5252 failed attempt at finding the last
5253 class-or-namespace-name. */
5254 parser->scope = old_scope;
5255 parser->qualifying_scope = saved_qualifying_scope;
5257 /* If the next token is a decltype, and the one after that is a
5258 `::', then the decltype has failed to resolve to a class or
5259 enumeration type. Give this error even when parsing
5260 tentatively since it can't possibly be valid--and we're going
5261 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5262 won't get another chance.*/
5263 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5264 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5265 == CPP_SCOPE))
5267 token = cp_lexer_consume_token (parser->lexer);
5268 error_at (token->location, "decltype evaluates to %qT, "
5269 "which is not a class or enumeration type",
5270 token->u.value);
5271 parser->scope = error_mark_node;
5272 error_p = true;
5273 /* As below. */
5274 success = true;
5275 cp_lexer_consume_token (parser->lexer);
5278 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5279 break;
5280 /* If the next token is an identifier, and the one after
5281 that is a `::', then any valid interpretation would have
5282 found a class-or-namespace-name. */
5283 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5284 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5285 == CPP_SCOPE)
5286 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5287 != CPP_COMPL))
5289 token = cp_lexer_consume_token (parser->lexer);
5290 if (!error_p)
5292 if (!token->ambiguous_p)
5294 tree decl;
5295 tree ambiguous_decls;
5297 decl = cp_parser_lookup_name (parser, token->u.value,
5298 none_type,
5299 /*is_template=*/false,
5300 /*is_namespace=*/false,
5301 /*check_dependency=*/true,
5302 &ambiguous_decls,
5303 token->location);
5304 if (TREE_CODE (decl) == TEMPLATE_DECL)
5305 error_at (token->location,
5306 "%qD used without template parameters",
5307 decl);
5308 else if (ambiguous_decls)
5310 // cp_parser_lookup_name has the same diagnostic,
5311 // thus make sure to emit it at most once.
5312 if (cp_parser_uncommitted_to_tentative_parse_p
5313 (parser))
5315 error_at (token->location,
5316 "reference to %qD is ambiguous",
5317 token->u.value);
5318 print_candidates (ambiguous_decls);
5320 decl = error_mark_node;
5322 else
5324 if (cxx_dialect != cxx98)
5325 cp_parser_name_lookup_error
5326 (parser, token->u.value, decl, NLE_NOT_CXX98,
5327 token->location);
5328 else
5329 cp_parser_name_lookup_error
5330 (parser, token->u.value, decl, NLE_CXX98,
5331 token->location);
5334 parser->scope = error_mark_node;
5335 error_p = true;
5336 /* Treat this as a successful nested-name-specifier
5337 due to:
5339 [basic.lookup.qual]
5341 If the name found is not a class-name (clause
5342 _class_) or namespace-name (_namespace.def_), the
5343 program is ill-formed. */
5344 success = true;
5346 cp_lexer_consume_token (parser->lexer);
5348 break;
5350 /* We've found one valid nested-name-specifier. */
5351 success = true;
5352 /* Name lookup always gives us a DECL. */
5353 if (TREE_CODE (new_scope) == TYPE_DECL)
5354 new_scope = TREE_TYPE (new_scope);
5355 /* Uses of "template" must be followed by actual templates. */
5356 if (template_keyword_p
5357 && !(CLASS_TYPE_P (new_scope)
5358 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5359 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5360 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5361 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5362 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5363 == TEMPLATE_ID_EXPR)))
5364 permerror (input_location, TYPE_P (new_scope)
5365 ? G_("%qT is not a template")
5366 : G_("%qD is not a template"),
5367 new_scope);
5368 /* If it is a class scope, try to complete it; we are about to
5369 be looking up names inside the class. */
5370 if (TYPE_P (new_scope)
5371 /* Since checking types for dependency can be expensive,
5372 avoid doing it if the type is already complete. */
5373 && !COMPLETE_TYPE_P (new_scope)
5374 /* Do not try to complete dependent types. */
5375 && !dependent_type_p (new_scope))
5377 new_scope = complete_type (new_scope);
5378 /* If it is a typedef to current class, use the current
5379 class instead, as the typedef won't have any names inside
5380 it yet. */
5381 if (!COMPLETE_TYPE_P (new_scope)
5382 && currently_open_class (new_scope))
5383 new_scope = TYPE_MAIN_VARIANT (new_scope);
5385 /* Make sure we look in the right scope the next time through
5386 the loop. */
5387 parser->scope = new_scope;
5390 /* If parsing tentatively, replace the sequence of tokens that makes
5391 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5392 token. That way, should we re-parse the token stream, we will
5393 not have to repeat the effort required to do the parse, nor will
5394 we issue duplicate error messages. */
5395 if (success && start)
5397 cp_token *token;
5399 token = cp_lexer_token_at (parser->lexer, start);
5400 /* Reset the contents of the START token. */
5401 token->type = CPP_NESTED_NAME_SPECIFIER;
5402 /* Retrieve any deferred checks. Do not pop this access checks yet
5403 so the memory will not be reclaimed during token replacing below. */
5404 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5405 token->u.tree_check_value->value = parser->scope;
5406 token->u.tree_check_value->checks = get_deferred_access_checks ();
5407 token->u.tree_check_value->qualifying_scope =
5408 parser->qualifying_scope;
5409 token->keyword = RID_MAX;
5411 /* Purge all subsequent tokens. */
5412 cp_lexer_purge_tokens_after (parser->lexer, start);
5415 if (start)
5416 pop_to_parent_deferring_access_checks ();
5418 return success ? parser->scope : NULL_TREE;
5421 /* Parse a nested-name-specifier. See
5422 cp_parser_nested_name_specifier_opt for details. This function
5423 behaves identically, except that it will an issue an error if no
5424 nested-name-specifier is present. */
5426 static tree
5427 cp_parser_nested_name_specifier (cp_parser *parser,
5428 bool typename_keyword_p,
5429 bool check_dependency_p,
5430 bool type_p,
5431 bool is_declaration)
5433 tree scope;
5435 /* Look for the nested-name-specifier. */
5436 scope = cp_parser_nested_name_specifier_opt (parser,
5437 typename_keyword_p,
5438 check_dependency_p,
5439 type_p,
5440 is_declaration);
5441 /* If it was not present, issue an error message. */
5442 if (!scope)
5444 cp_parser_error (parser, "expected nested-name-specifier");
5445 parser->scope = NULL_TREE;
5448 return scope;
5451 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5452 this is either a class-name or a namespace-name (which corresponds
5453 to the class-or-namespace-name production in the grammar). For
5454 C++0x, it can also be a type-name that refers to an enumeration
5455 type or a simple-template-id.
5457 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5458 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5459 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5460 TYPE_P is TRUE iff the next name should be taken as a class-name,
5461 even the same name is declared to be another entity in the same
5462 scope.
5464 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5465 specified by the class-or-namespace-name. If neither is found the
5466 ERROR_MARK_NODE is returned. */
5468 static tree
5469 cp_parser_qualifying_entity (cp_parser *parser,
5470 bool typename_keyword_p,
5471 bool template_keyword_p,
5472 bool check_dependency_p,
5473 bool type_p,
5474 bool is_declaration)
5476 tree saved_scope;
5477 tree saved_qualifying_scope;
5478 tree saved_object_scope;
5479 tree scope;
5480 bool only_class_p;
5481 bool successful_parse_p;
5483 /* DR 743: decltype can appear in a nested-name-specifier. */
5484 if (cp_lexer_next_token_is_decltype (parser->lexer))
5486 scope = cp_parser_decltype (parser);
5487 if (TREE_CODE (scope) != ENUMERAL_TYPE
5488 && !MAYBE_CLASS_TYPE_P (scope))
5490 cp_parser_simulate_error (parser);
5491 return error_mark_node;
5493 if (TYPE_NAME (scope))
5494 scope = TYPE_NAME (scope);
5495 return scope;
5498 /* Before we try to parse the class-name, we must save away the
5499 current PARSER->SCOPE since cp_parser_class_name will destroy
5500 it. */
5501 saved_scope = parser->scope;
5502 saved_qualifying_scope = parser->qualifying_scope;
5503 saved_object_scope = parser->object_scope;
5504 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5505 there is no need to look for a namespace-name. */
5506 only_class_p = template_keyword_p
5507 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5508 if (!only_class_p)
5509 cp_parser_parse_tentatively (parser);
5510 scope = cp_parser_class_name (parser,
5511 typename_keyword_p,
5512 template_keyword_p,
5513 type_p ? class_type : none_type,
5514 check_dependency_p,
5515 /*class_head_p=*/false,
5516 is_declaration);
5517 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5518 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5519 if (!only_class_p
5520 && cxx_dialect != cxx98
5521 && !successful_parse_p)
5523 /* Restore the saved scope. */
5524 parser->scope = saved_scope;
5525 parser->qualifying_scope = saved_qualifying_scope;
5526 parser->object_scope = saved_object_scope;
5528 /* Parse tentatively. */
5529 cp_parser_parse_tentatively (parser);
5531 /* Parse a type-name */
5532 scope = cp_parser_type_name (parser);
5534 /* "If the name found does not designate a namespace or a class,
5535 enumeration, or dependent type, the program is ill-formed."
5537 We cover classes and dependent types above and namespaces below,
5538 so this code is only looking for enums. */
5539 if (!scope || TREE_CODE (scope) != TYPE_DECL
5540 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5541 cp_parser_simulate_error (parser);
5543 successful_parse_p = cp_parser_parse_definitely (parser);
5545 /* If that didn't work, try for a namespace-name. */
5546 if (!only_class_p && !successful_parse_p)
5548 /* Restore the saved scope. */
5549 parser->scope = saved_scope;
5550 parser->qualifying_scope = saved_qualifying_scope;
5551 parser->object_scope = saved_object_scope;
5552 /* If we are not looking at an identifier followed by the scope
5553 resolution operator, then this is not part of a
5554 nested-name-specifier. (Note that this function is only used
5555 to parse the components of a nested-name-specifier.) */
5556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5557 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5558 return error_mark_node;
5559 scope = cp_parser_namespace_name (parser);
5562 return scope;
5565 /* Parse a postfix-expression.
5567 postfix-expression:
5568 primary-expression
5569 postfix-expression [ expression ]
5570 postfix-expression ( expression-list [opt] )
5571 simple-type-specifier ( expression-list [opt] )
5572 typename :: [opt] nested-name-specifier identifier
5573 ( expression-list [opt] )
5574 typename :: [opt] nested-name-specifier template [opt] template-id
5575 ( expression-list [opt] )
5576 postfix-expression . template [opt] id-expression
5577 postfix-expression -> template [opt] id-expression
5578 postfix-expression . pseudo-destructor-name
5579 postfix-expression -> pseudo-destructor-name
5580 postfix-expression ++
5581 postfix-expression --
5582 dynamic_cast < type-id > ( expression )
5583 static_cast < type-id > ( expression )
5584 reinterpret_cast < type-id > ( expression )
5585 const_cast < type-id > ( expression )
5586 typeid ( expression )
5587 typeid ( type-id )
5589 GNU Extension:
5591 postfix-expression:
5592 ( type-id ) { initializer-list , [opt] }
5594 This extension is a GNU version of the C99 compound-literal
5595 construct. (The C99 grammar uses `type-name' instead of `type-id',
5596 but they are essentially the same concept.)
5598 If ADDRESS_P is true, the postfix expression is the operand of the
5599 `&' operator. CAST_P is true if this expression is the target of a
5600 cast.
5602 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5603 class member access expressions [expr.ref].
5605 Returns a representation of the expression. */
5607 static tree
5608 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5609 bool member_access_only_p, bool decltype_p,
5610 cp_id_kind * pidk_return)
5612 cp_token *token;
5613 location_t loc;
5614 enum rid keyword;
5615 cp_id_kind idk = CP_ID_KIND_NONE;
5616 tree postfix_expression = NULL_TREE;
5617 bool is_member_access = false;
5619 /* Peek at the next token. */
5620 token = cp_lexer_peek_token (parser->lexer);
5621 loc = token->location;
5622 /* Some of the productions are determined by keywords. */
5623 keyword = token->keyword;
5624 switch (keyword)
5626 case RID_DYNCAST:
5627 case RID_STATCAST:
5628 case RID_REINTCAST:
5629 case RID_CONSTCAST:
5631 tree type;
5632 tree expression;
5633 const char *saved_message;
5634 bool saved_in_type_id_in_expr_p;
5636 /* All of these can be handled in the same way from the point
5637 of view of parsing. Begin by consuming the token
5638 identifying the cast. */
5639 cp_lexer_consume_token (parser->lexer);
5641 /* New types cannot be defined in the cast. */
5642 saved_message = parser->type_definition_forbidden_message;
5643 parser->type_definition_forbidden_message
5644 = G_("types may not be defined in casts");
5646 /* Look for the opening `<'. */
5647 cp_parser_require (parser, CPP_LESS, RT_LESS);
5648 /* Parse the type to which we are casting. */
5649 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5650 parser->in_type_id_in_expr_p = true;
5651 type = cp_parser_type_id (parser);
5652 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5653 /* Look for the closing `>'. */
5654 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5655 /* Restore the old message. */
5656 parser->type_definition_forbidden_message = saved_message;
5658 bool saved_greater_than_is_operator_p
5659 = parser->greater_than_is_operator_p;
5660 parser->greater_than_is_operator_p = true;
5662 /* And the expression which is being cast. */
5663 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5664 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5665 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5667 parser->greater_than_is_operator_p
5668 = saved_greater_than_is_operator_p;
5670 /* Only type conversions to integral or enumeration types
5671 can be used in constant-expressions. */
5672 if (!cast_valid_in_integral_constant_expression_p (type)
5673 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5674 return error_mark_node;
5676 switch (keyword)
5678 case RID_DYNCAST:
5679 postfix_expression
5680 = build_dynamic_cast (type, expression, tf_warning_or_error);
5681 break;
5682 case RID_STATCAST:
5683 postfix_expression
5684 = build_static_cast (type, expression, tf_warning_or_error);
5685 break;
5686 case RID_REINTCAST:
5687 postfix_expression
5688 = build_reinterpret_cast (type, expression,
5689 tf_warning_or_error);
5690 break;
5691 case RID_CONSTCAST:
5692 postfix_expression
5693 = build_const_cast (type, expression, tf_warning_or_error);
5694 break;
5695 default:
5696 gcc_unreachable ();
5699 break;
5701 case RID_TYPEID:
5703 tree type;
5704 const char *saved_message;
5705 bool saved_in_type_id_in_expr_p;
5707 /* Consume the `typeid' token. */
5708 cp_lexer_consume_token (parser->lexer);
5709 /* Look for the `(' token. */
5710 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5711 /* Types cannot be defined in a `typeid' expression. */
5712 saved_message = parser->type_definition_forbidden_message;
5713 parser->type_definition_forbidden_message
5714 = G_("types may not be defined in a %<typeid%> expression");
5715 /* We can't be sure yet whether we're looking at a type-id or an
5716 expression. */
5717 cp_parser_parse_tentatively (parser);
5718 /* Try a type-id first. */
5719 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5720 parser->in_type_id_in_expr_p = true;
5721 type = cp_parser_type_id (parser);
5722 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5723 /* Look for the `)' token. Otherwise, we can't be sure that
5724 we're not looking at an expression: consider `typeid (int
5725 (3))', for example. */
5726 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5727 /* If all went well, simply lookup the type-id. */
5728 if (cp_parser_parse_definitely (parser))
5729 postfix_expression = get_typeid (type, tf_warning_or_error);
5730 /* Otherwise, fall back to the expression variant. */
5731 else
5733 tree expression;
5735 /* Look for an expression. */
5736 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5737 /* Compute its typeid. */
5738 postfix_expression = build_typeid (expression, tf_warning_or_error);
5739 /* Look for the `)' token. */
5740 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5742 /* Restore the saved message. */
5743 parser->type_definition_forbidden_message = saved_message;
5744 /* `typeid' may not appear in an integral constant expression. */
5745 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5746 return error_mark_node;
5748 break;
5750 case RID_TYPENAME:
5752 tree type;
5753 /* The syntax permitted here is the same permitted for an
5754 elaborated-type-specifier. */
5755 type = cp_parser_elaborated_type_specifier (parser,
5756 /*is_friend=*/false,
5757 /*is_declaration=*/false);
5758 postfix_expression = cp_parser_functional_cast (parser, type);
5760 break;
5762 case RID_BUILTIN_SHUFFLE:
5764 vec<tree, va_gc> *vec;
5765 unsigned int i;
5766 tree p;
5768 cp_lexer_consume_token (parser->lexer);
5769 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5770 /*cast_p=*/false, /*allow_expansion_p=*/true,
5771 /*non_constant_p=*/NULL);
5772 if (vec == NULL)
5773 return error_mark_node;
5775 FOR_EACH_VEC_ELT (*vec, i, p)
5776 mark_exp_read (p);
5778 if (vec->length () == 2)
5779 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5780 tf_warning_or_error);
5781 else if (vec->length () == 3)
5782 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5783 tf_warning_or_error);
5784 else
5786 error_at (loc, "wrong number of arguments to "
5787 "%<__builtin_shuffle%>");
5788 return error_mark_node;
5790 break;
5793 default:
5795 tree type;
5797 /* If the next thing is a simple-type-specifier, we may be
5798 looking at a functional cast. We could also be looking at
5799 an id-expression. So, we try the functional cast, and if
5800 that doesn't work we fall back to the primary-expression. */
5801 cp_parser_parse_tentatively (parser);
5802 /* Look for the simple-type-specifier. */
5803 type = cp_parser_simple_type_specifier (parser,
5804 /*decl_specs=*/NULL,
5805 CP_PARSER_FLAGS_NONE);
5806 /* Parse the cast itself. */
5807 if (!cp_parser_error_occurred (parser))
5808 postfix_expression
5809 = cp_parser_functional_cast (parser, type);
5810 /* If that worked, we're done. */
5811 if (cp_parser_parse_definitely (parser))
5812 break;
5814 /* If the functional-cast didn't work out, try a
5815 compound-literal. */
5816 if (cp_parser_allow_gnu_extensions_p (parser)
5817 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5819 tree initializer = NULL_TREE;
5820 bool saved_in_type_id_in_expr_p;
5822 cp_parser_parse_tentatively (parser);
5823 /* Consume the `('. */
5824 cp_lexer_consume_token (parser->lexer);
5825 /* Parse the type. */
5826 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5827 parser->in_type_id_in_expr_p = true;
5828 type = cp_parser_type_id (parser);
5829 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5830 /* Look for the `)'. */
5831 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5832 /* If things aren't going well, there's no need to
5833 keep going. */
5834 if (!cp_parser_error_occurred (parser))
5836 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5838 bool non_constant_p;
5839 /* Parse the brace-enclosed initializer list. */
5840 initializer = cp_parser_braced_list (parser,
5841 &non_constant_p);
5843 else
5844 cp_parser_simulate_error (parser);
5846 /* If that worked, we're definitely looking at a
5847 compound-literal expression. */
5848 if (cp_parser_parse_definitely (parser))
5850 /* Warn the user that a compound literal is not
5851 allowed in standard C++. */
5852 pedwarn (input_location, OPT_Wpedantic,
5853 "ISO C++ forbids compound-literals");
5854 /* For simplicity, we disallow compound literals in
5855 constant-expressions. We could
5856 allow compound literals of integer type, whose
5857 initializer was a constant, in constant
5858 expressions. Permitting that usage, as a further
5859 extension, would not change the meaning of any
5860 currently accepted programs. (Of course, as
5861 compound literals are not part of ISO C++, the
5862 standard has nothing to say.) */
5863 if (cp_parser_non_integral_constant_expression (parser,
5864 NIC_NCC))
5866 postfix_expression = error_mark_node;
5867 break;
5869 /* Form the representation of the compound-literal. */
5870 postfix_expression
5871 = finish_compound_literal (type, initializer,
5872 tf_warning_or_error);
5873 break;
5877 /* It must be a primary-expression. */
5878 postfix_expression
5879 = cp_parser_primary_expression (parser, address_p, cast_p,
5880 /*template_arg_p=*/false,
5881 decltype_p,
5882 &idk);
5884 break;
5887 /* Note that we don't need to worry about calling build_cplus_new on a
5888 class-valued CALL_EXPR in decltype when it isn't the end of the
5889 postfix-expression; unary_complex_lvalue will take care of that for
5890 all these cases. */
5892 /* Keep looping until the postfix-expression is complete. */
5893 while (true)
5895 if (idk == CP_ID_KIND_UNQUALIFIED
5896 && identifier_p (postfix_expression)
5897 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5898 /* It is not a Koenig lookup function call. */
5899 postfix_expression
5900 = unqualified_name_lookup_error (postfix_expression);
5902 /* Peek at the next token. */
5903 token = cp_lexer_peek_token (parser->lexer);
5905 switch (token->type)
5907 case CPP_OPEN_SQUARE:
5908 if (cp_next_tokens_can_be_std_attribute_p (parser))
5910 cp_parser_error (parser,
5911 "two consecutive %<[%> shall "
5912 "only introduce an attribute");
5913 return error_mark_node;
5915 postfix_expression
5916 = cp_parser_postfix_open_square_expression (parser,
5917 postfix_expression,
5918 false,
5919 decltype_p);
5920 idk = CP_ID_KIND_NONE;
5921 is_member_access = false;
5922 break;
5924 case CPP_OPEN_PAREN:
5925 /* postfix-expression ( expression-list [opt] ) */
5927 bool koenig_p;
5928 bool is_builtin_constant_p;
5929 bool saved_integral_constant_expression_p = false;
5930 bool saved_non_integral_constant_expression_p = false;
5931 tsubst_flags_t complain = complain_flags (decltype_p);
5932 vec<tree, va_gc> *args;
5934 is_member_access = false;
5936 is_builtin_constant_p
5937 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5938 if (is_builtin_constant_p)
5940 /* The whole point of __builtin_constant_p is to allow
5941 non-constant expressions to appear as arguments. */
5942 saved_integral_constant_expression_p
5943 = parser->integral_constant_expression_p;
5944 saved_non_integral_constant_expression_p
5945 = parser->non_integral_constant_expression_p;
5946 parser->integral_constant_expression_p = false;
5948 args = (cp_parser_parenthesized_expression_list
5949 (parser, non_attr,
5950 /*cast_p=*/false, /*allow_expansion_p=*/true,
5951 /*non_constant_p=*/NULL));
5952 if (is_builtin_constant_p)
5954 parser->integral_constant_expression_p
5955 = saved_integral_constant_expression_p;
5956 parser->non_integral_constant_expression_p
5957 = saved_non_integral_constant_expression_p;
5960 if (args == NULL)
5962 postfix_expression = error_mark_node;
5963 break;
5966 /* Function calls are not permitted in
5967 constant-expressions. */
5968 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5969 && cp_parser_non_integral_constant_expression (parser,
5970 NIC_FUNC_CALL))
5972 postfix_expression = error_mark_node;
5973 release_tree_vector (args);
5974 break;
5977 koenig_p = false;
5978 if (idk == CP_ID_KIND_UNQUALIFIED
5979 || idk == CP_ID_KIND_TEMPLATE_ID)
5981 if (identifier_p (postfix_expression))
5983 if (!args->is_empty ())
5985 koenig_p = true;
5986 if (!any_type_dependent_arguments_p (args))
5987 postfix_expression
5988 = perform_koenig_lookup (postfix_expression, args,
5989 /*include_std=*/false,
5990 complain);
5992 else
5993 postfix_expression
5994 = unqualified_fn_lookup_error (postfix_expression);
5996 /* We do not perform argument-dependent lookup if
5997 normal lookup finds a non-function, in accordance
5998 with the expected resolution of DR 218. */
5999 else if (!args->is_empty ()
6000 && is_overloaded_fn (postfix_expression))
6002 tree fn = get_first_fn (postfix_expression);
6003 fn = STRIP_TEMPLATE (fn);
6005 /* Do not do argument dependent lookup if regular
6006 lookup finds a member function or a block-scope
6007 function declaration. [basic.lookup.argdep]/3 */
6008 if (!DECL_FUNCTION_MEMBER_P (fn)
6009 && !DECL_LOCAL_FUNCTION_P (fn))
6011 koenig_p = true;
6012 if (!any_type_dependent_arguments_p (args))
6013 postfix_expression
6014 = perform_koenig_lookup (postfix_expression, args,
6015 /*include_std=*/false,
6016 complain);
6021 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6023 tree instance = TREE_OPERAND (postfix_expression, 0);
6024 tree fn = TREE_OPERAND (postfix_expression, 1);
6026 if (processing_template_decl
6027 && (type_dependent_expression_p (instance)
6028 || (!BASELINK_P (fn)
6029 && TREE_CODE (fn) != FIELD_DECL)
6030 || type_dependent_expression_p (fn)
6031 || any_type_dependent_arguments_p (args)))
6033 postfix_expression
6034 = build_nt_call_vec (postfix_expression, args);
6035 release_tree_vector (args);
6036 break;
6039 if (BASELINK_P (fn))
6041 postfix_expression
6042 = (build_new_method_call
6043 (instance, fn, &args, NULL_TREE,
6044 (idk == CP_ID_KIND_QUALIFIED
6045 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6046 : LOOKUP_NORMAL),
6047 /*fn_p=*/NULL,
6048 complain));
6050 else
6051 postfix_expression
6052 = finish_call_expr (postfix_expression, &args,
6053 /*disallow_virtual=*/false,
6054 /*koenig_p=*/false,
6055 complain);
6057 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6058 || TREE_CODE (postfix_expression) == MEMBER_REF
6059 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6060 postfix_expression = (build_offset_ref_call_from_tree
6061 (postfix_expression, &args,
6062 complain));
6063 else if (idk == CP_ID_KIND_QUALIFIED)
6064 /* A call to a static class member, or a namespace-scope
6065 function. */
6066 postfix_expression
6067 = finish_call_expr (postfix_expression, &args,
6068 /*disallow_virtual=*/true,
6069 koenig_p,
6070 complain);
6071 else
6072 /* All other function calls. */
6073 postfix_expression
6074 = finish_call_expr (postfix_expression, &args,
6075 /*disallow_virtual=*/false,
6076 koenig_p,
6077 complain);
6079 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6080 idk = CP_ID_KIND_NONE;
6082 release_tree_vector (args);
6084 break;
6086 case CPP_DOT:
6087 case CPP_DEREF:
6088 /* postfix-expression . template [opt] id-expression
6089 postfix-expression . pseudo-destructor-name
6090 postfix-expression -> template [opt] id-expression
6091 postfix-expression -> pseudo-destructor-name */
6093 /* Consume the `.' or `->' operator. */
6094 cp_lexer_consume_token (parser->lexer);
6096 postfix_expression
6097 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6098 postfix_expression,
6099 false, &idk, loc);
6101 is_member_access = true;
6102 break;
6104 case CPP_PLUS_PLUS:
6105 /* postfix-expression ++ */
6106 /* Consume the `++' token. */
6107 cp_lexer_consume_token (parser->lexer);
6108 /* Generate a representation for the complete expression. */
6109 postfix_expression
6110 = finish_increment_expr (postfix_expression,
6111 POSTINCREMENT_EXPR);
6112 /* Increments may not appear in constant-expressions. */
6113 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6114 postfix_expression = error_mark_node;
6115 idk = CP_ID_KIND_NONE;
6116 is_member_access = false;
6117 break;
6119 case CPP_MINUS_MINUS:
6120 /* postfix-expression -- */
6121 /* Consume the `--' token. */
6122 cp_lexer_consume_token (parser->lexer);
6123 /* Generate a representation for the complete expression. */
6124 postfix_expression
6125 = finish_increment_expr (postfix_expression,
6126 POSTDECREMENT_EXPR);
6127 /* Decrements may not appear in constant-expressions. */
6128 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6129 postfix_expression = error_mark_node;
6130 idk = CP_ID_KIND_NONE;
6131 is_member_access = false;
6132 break;
6134 default:
6135 if (pidk_return != NULL)
6136 * pidk_return = idk;
6137 if (member_access_only_p)
6138 return is_member_access? postfix_expression : error_mark_node;
6139 else
6140 return postfix_expression;
6144 /* We should never get here. */
6145 gcc_unreachable ();
6146 return error_mark_node;
6149 /* This function parses Cilk Plus array notations. If a normal array expr. is
6150 parsed then the array index is passed back to the caller through *INIT_INDEX
6151 and the function returns a NULL_TREE. If array notation expr. is parsed,
6152 then *INIT_INDEX is ignored by the caller and the function returns
6153 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6154 error_mark_node. */
6156 static tree
6157 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6158 tree array_value)
6160 cp_token *token = NULL;
6161 tree length_index, stride = NULL_TREE, value_tree, array_type;
6162 if (!array_value || array_value == error_mark_node)
6164 cp_parser_skip_to_end_of_statement (parser);
6165 return error_mark_node;
6168 array_type = TREE_TYPE (array_value);
6170 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6171 parser->colon_corrects_to_scope_p = false;
6172 token = cp_lexer_peek_token (parser->lexer);
6174 if (!token)
6176 cp_parser_error (parser, "expected %<:%> or numeral");
6177 return error_mark_node;
6179 else if (token->type == CPP_COLON)
6181 /* Consume the ':'. */
6182 cp_lexer_consume_token (parser->lexer);
6184 /* If we are here, then we have a case like this A[:]. */
6185 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6187 cp_parser_error (parser, "expected %<]%>");
6188 cp_parser_skip_to_end_of_statement (parser);
6189 return error_mark_node;
6191 *init_index = NULL_TREE;
6192 stride = NULL_TREE;
6193 length_index = NULL_TREE;
6195 else
6197 /* If we are here, then there are three valid possibilities:
6198 1. ARRAY [ EXP ]
6199 2. ARRAY [ EXP : EXP ]
6200 3. ARRAY [ EXP : EXP : EXP ] */
6202 *init_index = cp_parser_expression (parser, false, NULL);
6203 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6205 /* This indicates that we have a normal array expression. */
6206 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6207 return NULL_TREE;
6210 /* Consume the ':'. */
6211 cp_lexer_consume_token (parser->lexer);
6212 length_index = cp_parser_expression (parser, false, NULL);
6213 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6215 cp_lexer_consume_token (parser->lexer);
6216 stride = cp_parser_expression (parser, false, NULL);
6219 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6221 if (*init_index == error_mark_node || length_index == error_mark_node
6222 || stride == error_mark_node)
6224 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6225 cp_lexer_consume_token (parser->lexer);
6226 return error_mark_node;
6228 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6230 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6231 length_index, stride, array_type);
6232 return value_tree;
6235 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6236 by cp_parser_builtin_offsetof. We're looking for
6238 postfix-expression [ expression ]
6239 postfix-expression [ braced-init-list ] (C++11)
6241 FOR_OFFSETOF is set if we're being called in that context, which
6242 changes how we deal with integer constant expressions. */
6244 static tree
6245 cp_parser_postfix_open_square_expression (cp_parser *parser,
6246 tree postfix_expression,
6247 bool for_offsetof,
6248 bool decltype_p)
6250 tree index = NULL_TREE;
6251 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6252 bool saved_greater_than_is_operator_p;
6254 /* Consume the `[' token. */
6255 cp_lexer_consume_token (parser->lexer);
6257 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6258 parser->greater_than_is_operator_p = true;
6260 /* Parse the index expression. */
6261 /* ??? For offsetof, there is a question of what to allow here. If
6262 offsetof is not being used in an integral constant expression context,
6263 then we *could* get the right answer by computing the value at runtime.
6264 If we are in an integral constant expression context, then we might
6265 could accept any constant expression; hard to say without analysis.
6266 Rather than open the barn door too wide right away, allow only integer
6267 constant expressions here. */
6268 if (for_offsetof)
6269 index = cp_parser_constant_expression (parser, false, NULL);
6270 else
6272 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6274 bool expr_nonconst_p;
6275 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6276 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6277 if (flag_enable_cilkplus
6278 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6280 error_at (cp_lexer_peek_token (parser->lexer)->location,
6281 "braced list index is not allowed with array "
6282 "notation");
6283 cp_parser_skip_to_end_of_statement (parser);
6284 return error_mark_node;
6287 else if (flag_enable_cilkplus)
6289 /* Here are have these two options:
6290 ARRAY[EXP : EXP] - Array notation expr with default
6291 stride of 1.
6292 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6293 stride. */
6294 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6295 postfix_expression);
6296 if (an_exp)
6297 return an_exp;
6299 else
6300 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6303 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6305 /* Look for the closing `]'. */
6306 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6308 /* Build the ARRAY_REF. */
6309 postfix_expression = grok_array_decl (loc, postfix_expression,
6310 index, decltype_p);
6312 /* When not doing offsetof, array references are not permitted in
6313 constant-expressions. */
6314 if (!for_offsetof
6315 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6316 postfix_expression = error_mark_node;
6318 return postfix_expression;
6321 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6322 by cp_parser_builtin_offsetof. We're looking for
6324 postfix-expression . template [opt] id-expression
6325 postfix-expression . pseudo-destructor-name
6326 postfix-expression -> template [opt] id-expression
6327 postfix-expression -> pseudo-destructor-name
6329 FOR_OFFSETOF is set if we're being called in that context. That sorta
6330 limits what of the above we'll actually accept, but nevermind.
6331 TOKEN_TYPE is the "." or "->" token, which will already have been
6332 removed from the stream. */
6334 static tree
6335 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6336 enum cpp_ttype token_type,
6337 tree postfix_expression,
6338 bool for_offsetof, cp_id_kind *idk,
6339 location_t location)
6341 tree name;
6342 bool dependent_p;
6343 bool pseudo_destructor_p;
6344 tree scope = NULL_TREE;
6346 /* If this is a `->' operator, dereference the pointer. */
6347 if (token_type == CPP_DEREF)
6348 postfix_expression = build_x_arrow (location, postfix_expression,
6349 tf_warning_or_error);
6350 /* Check to see whether or not the expression is type-dependent. */
6351 dependent_p = type_dependent_expression_p (postfix_expression);
6352 /* The identifier following the `->' or `.' is not qualified. */
6353 parser->scope = NULL_TREE;
6354 parser->qualifying_scope = NULL_TREE;
6355 parser->object_scope = NULL_TREE;
6356 *idk = CP_ID_KIND_NONE;
6358 /* Enter the scope corresponding to the type of the object
6359 given by the POSTFIX_EXPRESSION. */
6360 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6362 scope = TREE_TYPE (postfix_expression);
6363 /* According to the standard, no expression should ever have
6364 reference type. Unfortunately, we do not currently match
6365 the standard in this respect in that our internal representation
6366 of an expression may have reference type even when the standard
6367 says it does not. Therefore, we have to manually obtain the
6368 underlying type here. */
6369 scope = non_reference (scope);
6370 /* The type of the POSTFIX_EXPRESSION must be complete. */
6371 if (scope == unknown_type_node)
6373 error_at (location, "%qE does not have class type",
6374 postfix_expression);
6375 scope = NULL_TREE;
6377 /* Unlike the object expression in other contexts, *this is not
6378 required to be of complete type for purposes of class member
6379 access (5.2.5) outside the member function body. */
6380 else if (postfix_expression != current_class_ref
6381 && !(processing_template_decl && scope == current_class_type))
6382 scope = complete_type_or_else (scope, NULL_TREE);
6383 /* Let the name lookup machinery know that we are processing a
6384 class member access expression. */
6385 parser->context->object_type = scope;
6386 /* If something went wrong, we want to be able to discern that case,
6387 as opposed to the case where there was no SCOPE due to the type
6388 of expression being dependent. */
6389 if (!scope)
6390 scope = error_mark_node;
6391 /* If the SCOPE was erroneous, make the various semantic analysis
6392 functions exit quickly -- and without issuing additional error
6393 messages. */
6394 if (scope == error_mark_node)
6395 postfix_expression = error_mark_node;
6398 /* Assume this expression is not a pseudo-destructor access. */
6399 pseudo_destructor_p = false;
6401 /* If the SCOPE is a scalar type, then, if this is a valid program,
6402 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6403 is type dependent, it can be pseudo-destructor-name or something else.
6404 Try to parse it as pseudo-destructor-name first. */
6405 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6407 tree s;
6408 tree type;
6410 cp_parser_parse_tentatively (parser);
6411 /* Parse the pseudo-destructor-name. */
6412 s = NULL_TREE;
6413 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6414 &s, &type);
6415 if (dependent_p
6416 && (cp_parser_error_occurred (parser)
6417 || !SCALAR_TYPE_P (type)))
6418 cp_parser_abort_tentative_parse (parser);
6419 else if (cp_parser_parse_definitely (parser))
6421 pseudo_destructor_p = true;
6422 postfix_expression
6423 = finish_pseudo_destructor_expr (postfix_expression,
6424 s, type, location);
6428 if (!pseudo_destructor_p)
6430 /* If the SCOPE is not a scalar type, we are looking at an
6431 ordinary class member access expression, rather than a
6432 pseudo-destructor-name. */
6433 bool template_p;
6434 cp_token *token = cp_lexer_peek_token (parser->lexer);
6435 /* Parse the id-expression. */
6436 name = (cp_parser_id_expression
6437 (parser,
6438 cp_parser_optional_template_keyword (parser),
6439 /*check_dependency_p=*/true,
6440 &template_p,
6441 /*declarator_p=*/false,
6442 /*optional_p=*/false));
6443 /* In general, build a SCOPE_REF if the member name is qualified.
6444 However, if the name was not dependent and has already been
6445 resolved; there is no need to build the SCOPE_REF. For example;
6447 struct X { void f(); };
6448 template <typename T> void f(T* t) { t->X::f(); }
6450 Even though "t" is dependent, "X::f" is not and has been resolved
6451 to a BASELINK; there is no need to include scope information. */
6453 /* But we do need to remember that there was an explicit scope for
6454 virtual function calls. */
6455 if (parser->scope)
6456 *idk = CP_ID_KIND_QUALIFIED;
6458 /* If the name is a template-id that names a type, we will get a
6459 TYPE_DECL here. That is invalid code. */
6460 if (TREE_CODE (name) == TYPE_DECL)
6462 error_at (token->location, "invalid use of %qD", name);
6463 postfix_expression = error_mark_node;
6465 else
6467 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6469 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6471 error_at (token->location, "%<%D::%D%> is not a class member",
6472 parser->scope, name);
6473 postfix_expression = error_mark_node;
6475 else
6476 name = build_qualified_name (/*type=*/NULL_TREE,
6477 parser->scope,
6478 name,
6479 template_p);
6480 parser->scope = NULL_TREE;
6481 parser->qualifying_scope = NULL_TREE;
6482 parser->object_scope = NULL_TREE;
6484 if (parser->scope && name && BASELINK_P (name))
6485 adjust_result_of_qualified_name_lookup
6486 (name, parser->scope, scope);
6487 postfix_expression
6488 = finish_class_member_access_expr (postfix_expression, name,
6489 template_p,
6490 tf_warning_or_error);
6494 /* We no longer need to look up names in the scope of the object on
6495 the left-hand side of the `.' or `->' operator. */
6496 parser->context->object_type = NULL_TREE;
6498 /* Outside of offsetof, these operators may not appear in
6499 constant-expressions. */
6500 if (!for_offsetof
6501 && (cp_parser_non_integral_constant_expression
6502 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6503 postfix_expression = error_mark_node;
6505 return postfix_expression;
6508 /* Parse a parenthesized expression-list.
6510 expression-list:
6511 assignment-expression
6512 expression-list, assignment-expression
6514 attribute-list:
6515 expression-list
6516 identifier
6517 identifier, expression-list
6519 CAST_P is true if this expression is the target of a cast.
6521 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6522 argument pack.
6524 Returns a vector of trees. Each element is a representation of an
6525 assignment-expression. NULL is returned if the ( and or ) are
6526 missing. An empty, but allocated, vector is returned on no
6527 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6528 if we are parsing an attribute list for an attribute that wants a
6529 plain identifier argument, normal_attr for an attribute that wants
6530 an expression, or non_attr if we aren't parsing an attribute list. If
6531 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6532 not all of the expressions in the list were constant. */
6534 static vec<tree, va_gc> *
6535 cp_parser_parenthesized_expression_list (cp_parser* parser,
6536 int is_attribute_list,
6537 bool cast_p,
6538 bool allow_expansion_p,
6539 bool *non_constant_p)
6541 vec<tree, va_gc> *expression_list;
6542 bool fold_expr_p = is_attribute_list != non_attr;
6543 tree identifier = NULL_TREE;
6544 bool saved_greater_than_is_operator_p;
6546 /* Assume all the expressions will be constant. */
6547 if (non_constant_p)
6548 *non_constant_p = false;
6550 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6551 return NULL;
6553 expression_list = make_tree_vector ();
6555 /* Within a parenthesized expression, a `>' token is always
6556 the greater-than operator. */
6557 saved_greater_than_is_operator_p
6558 = parser->greater_than_is_operator_p;
6559 parser->greater_than_is_operator_p = true;
6561 /* Consume expressions until there are no more. */
6562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6563 while (true)
6565 tree expr;
6567 /* At the beginning of attribute lists, check to see if the
6568 next token is an identifier. */
6569 if (is_attribute_list == id_attr
6570 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6572 cp_token *token;
6574 /* Consume the identifier. */
6575 token = cp_lexer_consume_token (parser->lexer);
6576 /* Save the identifier. */
6577 identifier = token->u.value;
6579 else
6581 bool expr_non_constant_p;
6583 /* Parse the next assignment-expression. */
6584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6586 /* A braced-init-list. */
6587 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6588 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6589 if (non_constant_p && expr_non_constant_p)
6590 *non_constant_p = true;
6592 else if (non_constant_p)
6594 expr = (cp_parser_constant_expression
6595 (parser, /*allow_non_constant_p=*/true,
6596 &expr_non_constant_p));
6597 if (expr_non_constant_p)
6598 *non_constant_p = true;
6600 else
6601 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6603 if (fold_expr_p)
6604 expr = fold_non_dependent_expr (expr);
6606 /* If we have an ellipsis, then this is an expression
6607 expansion. */
6608 if (allow_expansion_p
6609 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6611 /* Consume the `...'. */
6612 cp_lexer_consume_token (parser->lexer);
6614 /* Build the argument pack. */
6615 expr = make_pack_expansion (expr);
6618 /* Add it to the list. We add error_mark_node
6619 expressions to the list, so that we can still tell if
6620 the correct form for a parenthesized expression-list
6621 is found. That gives better errors. */
6622 vec_safe_push (expression_list, expr);
6624 if (expr == error_mark_node)
6625 goto skip_comma;
6628 /* After the first item, attribute lists look the same as
6629 expression lists. */
6630 is_attribute_list = non_attr;
6632 get_comma:;
6633 /* If the next token isn't a `,', then we are done. */
6634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6635 break;
6637 /* Otherwise, consume the `,' and keep going. */
6638 cp_lexer_consume_token (parser->lexer);
6641 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6643 int ending;
6645 skip_comma:;
6646 /* We try and resync to an unnested comma, as that will give the
6647 user better diagnostics. */
6648 ending = cp_parser_skip_to_closing_parenthesis (parser,
6649 /*recovering=*/true,
6650 /*or_comma=*/true,
6651 /*consume_paren=*/true);
6652 if (ending < 0)
6653 goto get_comma;
6654 if (!ending)
6656 parser->greater_than_is_operator_p
6657 = saved_greater_than_is_operator_p;
6658 return NULL;
6662 parser->greater_than_is_operator_p
6663 = saved_greater_than_is_operator_p;
6665 if (identifier)
6666 vec_safe_insert (expression_list, 0, identifier);
6668 return expression_list;
6671 /* Parse a pseudo-destructor-name.
6673 pseudo-destructor-name:
6674 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6675 :: [opt] nested-name-specifier template template-id :: ~ type-name
6676 :: [opt] nested-name-specifier [opt] ~ type-name
6678 If either of the first two productions is used, sets *SCOPE to the
6679 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6680 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6681 or ERROR_MARK_NODE if the parse fails. */
6683 static void
6684 cp_parser_pseudo_destructor_name (cp_parser* parser,
6685 tree object,
6686 tree* scope,
6687 tree* type)
6689 bool nested_name_specifier_p;
6691 /* Handle ~auto. */
6692 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6693 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6694 && !type_dependent_expression_p (object))
6696 if (cxx_dialect < cxx1y)
6697 pedwarn (input_location, 0,
6698 "%<~auto%> only available with "
6699 "-std=c++1y or -std=gnu++1y");
6700 cp_lexer_consume_token (parser->lexer);
6701 cp_lexer_consume_token (parser->lexer);
6702 *scope = NULL_TREE;
6703 *type = TREE_TYPE (object);
6704 return;
6707 /* Assume that things will not work out. */
6708 *type = error_mark_node;
6710 /* Look for the optional `::' operator. */
6711 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6712 /* Look for the optional nested-name-specifier. */
6713 nested_name_specifier_p
6714 = (cp_parser_nested_name_specifier_opt (parser,
6715 /*typename_keyword_p=*/false,
6716 /*check_dependency_p=*/true,
6717 /*type_p=*/false,
6718 /*is_declaration=*/false)
6719 != NULL_TREE);
6720 /* Now, if we saw a nested-name-specifier, we might be doing the
6721 second production. */
6722 if (nested_name_specifier_p
6723 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6725 /* Consume the `template' keyword. */
6726 cp_lexer_consume_token (parser->lexer);
6727 /* Parse the template-id. */
6728 cp_parser_template_id (parser,
6729 /*template_keyword_p=*/true,
6730 /*check_dependency_p=*/false,
6731 class_type,
6732 /*is_declaration=*/true);
6733 /* Look for the `::' token. */
6734 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6736 /* If the next token is not a `~', then there might be some
6737 additional qualification. */
6738 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6740 /* At this point, we're looking for "type-name :: ~". The type-name
6741 must not be a class-name, since this is a pseudo-destructor. So,
6742 it must be either an enum-name, or a typedef-name -- both of which
6743 are just identifiers. So, we peek ahead to check that the "::"
6744 and "~" tokens are present; if they are not, then we can avoid
6745 calling type_name. */
6746 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6747 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6748 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6750 cp_parser_error (parser, "non-scalar type");
6751 return;
6754 /* Look for the type-name. */
6755 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6756 if (*scope == error_mark_node)
6757 return;
6759 /* Look for the `::' token. */
6760 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6762 else
6763 *scope = NULL_TREE;
6765 /* Look for the `~'. */
6766 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6768 /* Once we see the ~, this has to be a pseudo-destructor. */
6769 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6770 cp_parser_commit_to_topmost_tentative_parse (parser);
6772 /* Look for the type-name again. We are not responsible for
6773 checking that it matches the first type-name. */
6774 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6777 /* Parse a unary-expression.
6779 unary-expression:
6780 postfix-expression
6781 ++ cast-expression
6782 -- cast-expression
6783 unary-operator cast-expression
6784 sizeof unary-expression
6785 sizeof ( type-id )
6786 alignof ( type-id ) [C++0x]
6787 new-expression
6788 delete-expression
6790 GNU Extensions:
6792 unary-expression:
6793 __extension__ cast-expression
6794 __alignof__ unary-expression
6795 __alignof__ ( type-id )
6796 alignof unary-expression [C++0x]
6797 __real__ cast-expression
6798 __imag__ cast-expression
6799 && identifier
6800 sizeof ( type-id ) { initializer-list , [opt] }
6801 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6802 __alignof__ ( type-id ) { initializer-list , [opt] }
6804 ADDRESS_P is true iff the unary-expression is appearing as the
6805 operand of the `&' operator. CAST_P is true if this expression is
6806 the target of a cast.
6808 Returns a representation of the expression. */
6810 static tree
6811 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6812 bool decltype_p, cp_id_kind * pidk)
6814 cp_token *token;
6815 enum tree_code unary_operator;
6817 /* Peek at the next token. */
6818 token = cp_lexer_peek_token (parser->lexer);
6819 /* Some keywords give away the kind of expression. */
6820 if (token->type == CPP_KEYWORD)
6822 enum rid keyword = token->keyword;
6824 switch (keyword)
6826 case RID_ALIGNOF:
6827 case RID_SIZEOF:
6829 tree operand, ret;
6830 enum tree_code op;
6831 location_t first_loc;
6833 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6834 /* Consume the token. */
6835 cp_lexer_consume_token (parser->lexer);
6836 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6837 /* Parse the operand. */
6838 operand = cp_parser_sizeof_operand (parser, keyword);
6840 if (TYPE_P (operand))
6841 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6842 else
6844 /* ISO C++ defines alignof only with types, not with
6845 expressions. So pedwarn if alignof is used with a non-
6846 type expression. However, __alignof__ is ok. */
6847 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6848 pedwarn (token->location, OPT_Wpedantic,
6849 "ISO C++ does not allow %<alignof%> "
6850 "with a non-type");
6852 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6854 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6855 SIZEOF_EXPR with the original operand. */
6856 if (op == SIZEOF_EXPR && ret != error_mark_node)
6858 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6860 if (!processing_template_decl && TYPE_P (operand))
6862 ret = build_min (SIZEOF_EXPR, size_type_node,
6863 build1 (NOP_EXPR, operand,
6864 error_mark_node));
6865 SIZEOF_EXPR_TYPE_P (ret) = 1;
6867 else
6868 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6869 TREE_SIDE_EFFECTS (ret) = 0;
6870 TREE_READONLY (ret) = 1;
6872 SET_EXPR_LOCATION (ret, first_loc);
6874 return ret;
6877 case RID_NEW:
6878 return cp_parser_new_expression (parser);
6880 case RID_DELETE:
6881 return cp_parser_delete_expression (parser);
6883 case RID_EXTENSION:
6885 /* The saved value of the PEDANTIC flag. */
6886 int saved_pedantic;
6887 tree expr;
6889 /* Save away the PEDANTIC flag. */
6890 cp_parser_extension_opt (parser, &saved_pedantic);
6891 /* Parse the cast-expression. */
6892 expr = cp_parser_simple_cast_expression (parser);
6893 /* Restore the PEDANTIC flag. */
6894 pedantic = saved_pedantic;
6896 return expr;
6899 case RID_REALPART:
6900 case RID_IMAGPART:
6902 tree expression;
6904 /* Consume the `__real__' or `__imag__' token. */
6905 cp_lexer_consume_token (parser->lexer);
6906 /* Parse the cast-expression. */
6907 expression = cp_parser_simple_cast_expression (parser);
6908 /* Create the complete representation. */
6909 return build_x_unary_op (token->location,
6910 (keyword == RID_REALPART
6911 ? REALPART_EXPR : IMAGPART_EXPR),
6912 expression,
6913 tf_warning_or_error);
6915 break;
6917 case RID_TRANSACTION_ATOMIC:
6918 case RID_TRANSACTION_RELAXED:
6919 return cp_parser_transaction_expression (parser, keyword);
6921 case RID_NOEXCEPT:
6923 tree expr;
6924 const char *saved_message;
6925 bool saved_integral_constant_expression_p;
6926 bool saved_non_integral_constant_expression_p;
6927 bool saved_greater_than_is_operator_p;
6929 cp_lexer_consume_token (parser->lexer);
6930 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6932 saved_message = parser->type_definition_forbidden_message;
6933 parser->type_definition_forbidden_message
6934 = G_("types may not be defined in %<noexcept%> expressions");
6936 saved_integral_constant_expression_p
6937 = parser->integral_constant_expression_p;
6938 saved_non_integral_constant_expression_p
6939 = parser->non_integral_constant_expression_p;
6940 parser->integral_constant_expression_p = false;
6942 saved_greater_than_is_operator_p
6943 = parser->greater_than_is_operator_p;
6944 parser->greater_than_is_operator_p = true;
6946 ++cp_unevaluated_operand;
6947 ++c_inhibit_evaluation_warnings;
6948 expr = cp_parser_expression (parser, false, NULL);
6949 --c_inhibit_evaluation_warnings;
6950 --cp_unevaluated_operand;
6952 parser->greater_than_is_operator_p
6953 = saved_greater_than_is_operator_p;
6955 parser->integral_constant_expression_p
6956 = saved_integral_constant_expression_p;
6957 parser->non_integral_constant_expression_p
6958 = saved_non_integral_constant_expression_p;
6960 parser->type_definition_forbidden_message = saved_message;
6962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6963 return finish_noexcept_expr (expr, tf_warning_or_error);
6966 default:
6967 break;
6971 /* Look for the `:: new' and `:: delete', which also signal the
6972 beginning of a new-expression, or delete-expression,
6973 respectively. If the next token is `::', then it might be one of
6974 these. */
6975 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6977 enum rid keyword;
6979 /* See if the token after the `::' is one of the keywords in
6980 which we're interested. */
6981 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6982 /* If it's `new', we have a new-expression. */
6983 if (keyword == RID_NEW)
6984 return cp_parser_new_expression (parser);
6985 /* Similarly, for `delete'. */
6986 else if (keyword == RID_DELETE)
6987 return cp_parser_delete_expression (parser);
6990 /* Look for a unary operator. */
6991 unary_operator = cp_parser_unary_operator (token);
6992 /* The `++' and `--' operators can be handled similarly, even though
6993 they are not technically unary-operators in the grammar. */
6994 if (unary_operator == ERROR_MARK)
6996 if (token->type == CPP_PLUS_PLUS)
6997 unary_operator = PREINCREMENT_EXPR;
6998 else if (token->type == CPP_MINUS_MINUS)
6999 unary_operator = PREDECREMENT_EXPR;
7000 /* Handle the GNU address-of-label extension. */
7001 else if (cp_parser_allow_gnu_extensions_p (parser)
7002 && token->type == CPP_AND_AND)
7004 tree identifier;
7005 tree expression;
7006 location_t loc = token->location;
7008 /* Consume the '&&' token. */
7009 cp_lexer_consume_token (parser->lexer);
7010 /* Look for the identifier. */
7011 identifier = cp_parser_identifier (parser);
7012 /* Create an expression representing the address. */
7013 expression = finish_label_address_expr (identifier, loc);
7014 if (cp_parser_non_integral_constant_expression (parser,
7015 NIC_ADDR_LABEL))
7016 expression = error_mark_node;
7017 return expression;
7020 if (unary_operator != ERROR_MARK)
7022 tree cast_expression;
7023 tree expression = error_mark_node;
7024 non_integral_constant non_constant_p = NIC_NONE;
7025 location_t loc = token->location;
7026 tsubst_flags_t complain = complain_flags (decltype_p);
7028 /* Consume the operator token. */
7029 token = cp_lexer_consume_token (parser->lexer);
7030 /* Parse the cast-expression. */
7031 cast_expression
7032 = cp_parser_cast_expression (parser,
7033 unary_operator == ADDR_EXPR,
7034 /*cast_p=*/false,
7035 /*decltype*/false,
7036 pidk);
7037 /* Now, build an appropriate representation. */
7038 switch (unary_operator)
7040 case INDIRECT_REF:
7041 non_constant_p = NIC_STAR;
7042 expression = build_x_indirect_ref (loc, cast_expression,
7043 RO_UNARY_STAR,
7044 complain);
7045 break;
7047 case ADDR_EXPR:
7048 non_constant_p = NIC_ADDR;
7049 /* Fall through. */
7050 case BIT_NOT_EXPR:
7051 expression = build_x_unary_op (loc, unary_operator,
7052 cast_expression,
7053 complain);
7054 break;
7056 case PREINCREMENT_EXPR:
7057 case PREDECREMENT_EXPR:
7058 non_constant_p = unary_operator == PREINCREMENT_EXPR
7059 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7060 /* Fall through. */
7061 case UNARY_PLUS_EXPR:
7062 case NEGATE_EXPR:
7063 case TRUTH_NOT_EXPR:
7064 expression = finish_unary_op_expr (loc, unary_operator,
7065 cast_expression, complain);
7066 break;
7068 default:
7069 gcc_unreachable ();
7072 if (non_constant_p != NIC_NONE
7073 && cp_parser_non_integral_constant_expression (parser,
7074 non_constant_p))
7075 expression = error_mark_node;
7077 return expression;
7080 return cp_parser_postfix_expression (parser, address_p, cast_p,
7081 /*member_access_only_p=*/false,
7082 decltype_p,
7083 pidk);
7086 static inline tree
7087 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7088 cp_id_kind * pidk)
7090 return cp_parser_unary_expression (parser, address_p, cast_p,
7091 /*decltype*/false, pidk);
7094 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7095 unary-operator, the corresponding tree code is returned. */
7097 static enum tree_code
7098 cp_parser_unary_operator (cp_token* token)
7100 switch (token->type)
7102 case CPP_MULT:
7103 return INDIRECT_REF;
7105 case CPP_AND:
7106 return ADDR_EXPR;
7108 case CPP_PLUS:
7109 return UNARY_PLUS_EXPR;
7111 case CPP_MINUS:
7112 return NEGATE_EXPR;
7114 case CPP_NOT:
7115 return TRUTH_NOT_EXPR;
7117 case CPP_COMPL:
7118 return BIT_NOT_EXPR;
7120 default:
7121 return ERROR_MARK;
7125 /* Parse a new-expression.
7127 new-expression:
7128 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7129 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7131 Returns a representation of the expression. */
7133 static tree
7134 cp_parser_new_expression (cp_parser* parser)
7136 bool global_scope_p;
7137 vec<tree, va_gc> *placement;
7138 tree type;
7139 vec<tree, va_gc> *initializer;
7140 tree nelts = NULL_TREE;
7141 tree ret;
7143 /* Look for the optional `::' operator. */
7144 global_scope_p
7145 = (cp_parser_global_scope_opt (parser,
7146 /*current_scope_valid_p=*/false)
7147 != NULL_TREE);
7148 /* Look for the `new' operator. */
7149 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7150 /* There's no easy way to tell a new-placement from the
7151 `( type-id )' construct. */
7152 cp_parser_parse_tentatively (parser);
7153 /* Look for a new-placement. */
7154 placement = cp_parser_new_placement (parser);
7155 /* If that didn't work out, there's no new-placement. */
7156 if (!cp_parser_parse_definitely (parser))
7158 if (placement != NULL)
7159 release_tree_vector (placement);
7160 placement = NULL;
7163 /* If the next token is a `(', then we have a parenthesized
7164 type-id. */
7165 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7167 cp_token *token;
7168 const char *saved_message = parser->type_definition_forbidden_message;
7170 /* Consume the `('. */
7171 cp_lexer_consume_token (parser->lexer);
7173 /* Parse the type-id. */
7174 parser->type_definition_forbidden_message
7175 = G_("types may not be defined in a new-expression");
7176 type = cp_parser_type_id (parser);
7177 parser->type_definition_forbidden_message = saved_message;
7179 /* Look for the closing `)'. */
7180 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7181 token = cp_lexer_peek_token (parser->lexer);
7182 /* There should not be a direct-new-declarator in this production,
7183 but GCC used to allowed this, so we check and emit a sensible error
7184 message for this case. */
7185 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7187 error_at (token->location,
7188 "array bound forbidden after parenthesized type-id");
7189 inform (token->location,
7190 "try removing the parentheses around the type-id");
7191 cp_parser_direct_new_declarator (parser);
7194 /* Otherwise, there must be a new-type-id. */
7195 else
7196 type = cp_parser_new_type_id (parser, &nelts);
7198 /* If the next token is a `(' or '{', then we have a new-initializer. */
7199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7200 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7201 initializer = cp_parser_new_initializer (parser);
7202 else
7203 initializer = NULL;
7205 /* A new-expression may not appear in an integral constant
7206 expression. */
7207 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7208 ret = error_mark_node;
7209 else
7211 /* Create a representation of the new-expression. */
7212 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7213 tf_warning_or_error);
7216 if (placement != NULL)
7217 release_tree_vector (placement);
7218 if (initializer != NULL)
7219 release_tree_vector (initializer);
7221 return ret;
7224 /* Parse a new-placement.
7226 new-placement:
7227 ( expression-list )
7229 Returns the same representation as for an expression-list. */
7231 static vec<tree, va_gc> *
7232 cp_parser_new_placement (cp_parser* parser)
7234 vec<tree, va_gc> *expression_list;
7236 /* Parse the expression-list. */
7237 expression_list = (cp_parser_parenthesized_expression_list
7238 (parser, non_attr, /*cast_p=*/false,
7239 /*allow_expansion_p=*/true,
7240 /*non_constant_p=*/NULL));
7242 return expression_list;
7245 /* Parse a new-type-id.
7247 new-type-id:
7248 type-specifier-seq new-declarator [opt]
7250 Returns the TYPE allocated. If the new-type-id indicates an array
7251 type, *NELTS is set to the number of elements in the last array
7252 bound; the TYPE will not include the last array bound. */
7254 static tree
7255 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7257 cp_decl_specifier_seq type_specifier_seq;
7258 cp_declarator *new_declarator;
7259 cp_declarator *declarator;
7260 cp_declarator *outer_declarator;
7261 const char *saved_message;
7263 /* The type-specifier sequence must not contain type definitions.
7264 (It cannot contain declarations of new types either, but if they
7265 are not definitions we will catch that because they are not
7266 complete.) */
7267 saved_message = parser->type_definition_forbidden_message;
7268 parser->type_definition_forbidden_message
7269 = G_("types may not be defined in a new-type-id");
7270 /* Parse the type-specifier-seq. */
7271 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7272 /*is_trailing_return=*/false,
7273 &type_specifier_seq);
7274 /* Restore the old message. */
7275 parser->type_definition_forbidden_message = saved_message;
7277 if (type_specifier_seq.type == error_mark_node)
7278 return error_mark_node;
7280 /* Parse the new-declarator. */
7281 new_declarator = cp_parser_new_declarator_opt (parser);
7283 /* Determine the number of elements in the last array dimension, if
7284 any. */
7285 *nelts = NULL_TREE;
7286 /* Skip down to the last array dimension. */
7287 declarator = new_declarator;
7288 outer_declarator = NULL;
7289 while (declarator && (declarator->kind == cdk_pointer
7290 || declarator->kind == cdk_ptrmem))
7292 outer_declarator = declarator;
7293 declarator = declarator->declarator;
7295 while (declarator
7296 && declarator->kind == cdk_array
7297 && declarator->declarator
7298 && declarator->declarator->kind == cdk_array)
7300 outer_declarator = declarator;
7301 declarator = declarator->declarator;
7304 if (declarator && declarator->kind == cdk_array)
7306 *nelts = declarator->u.array.bounds;
7307 if (*nelts == error_mark_node)
7308 *nelts = integer_one_node;
7310 if (outer_declarator)
7311 outer_declarator->declarator = declarator->declarator;
7312 else
7313 new_declarator = NULL;
7316 return groktypename (&type_specifier_seq, new_declarator, false);
7319 /* Parse an (optional) new-declarator.
7321 new-declarator:
7322 ptr-operator new-declarator [opt]
7323 direct-new-declarator
7325 Returns the declarator. */
7327 static cp_declarator *
7328 cp_parser_new_declarator_opt (cp_parser* parser)
7330 enum tree_code code;
7331 tree type, std_attributes = NULL_TREE;
7332 cp_cv_quals cv_quals;
7334 /* We don't know if there's a ptr-operator next, or not. */
7335 cp_parser_parse_tentatively (parser);
7336 /* Look for a ptr-operator. */
7337 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7338 /* If that worked, look for more new-declarators. */
7339 if (cp_parser_parse_definitely (parser))
7341 cp_declarator *declarator;
7343 /* Parse another optional declarator. */
7344 declarator = cp_parser_new_declarator_opt (parser);
7346 declarator = cp_parser_make_indirect_declarator
7347 (code, type, cv_quals, declarator, std_attributes);
7349 return declarator;
7352 /* If the next token is a `[', there is a direct-new-declarator. */
7353 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7354 return cp_parser_direct_new_declarator (parser);
7356 return NULL;
7359 /* Parse a direct-new-declarator.
7361 direct-new-declarator:
7362 [ expression ]
7363 direct-new-declarator [constant-expression]
7367 static cp_declarator *
7368 cp_parser_direct_new_declarator (cp_parser* parser)
7370 cp_declarator *declarator = NULL;
7372 while (true)
7374 tree expression;
7375 cp_token *token;
7377 /* Look for the opening `['. */
7378 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7380 token = cp_lexer_peek_token (parser->lexer);
7381 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7382 /* The standard requires that the expression have integral
7383 type. DR 74 adds enumeration types. We believe that the
7384 real intent is that these expressions be handled like the
7385 expression in a `switch' condition, which also allows
7386 classes with a single conversion to integral or
7387 enumeration type. */
7388 if (!processing_template_decl)
7390 expression
7391 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7392 expression,
7393 /*complain=*/true);
7394 if (!expression)
7396 error_at (token->location,
7397 "expression in new-declarator must have integral "
7398 "or enumeration type");
7399 expression = error_mark_node;
7403 /* Look for the closing `]'. */
7404 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7406 /* Add this bound to the declarator. */
7407 declarator = make_array_declarator (declarator, expression);
7409 /* If the next token is not a `[', then there are no more
7410 bounds. */
7411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7412 break;
7415 return declarator;
7418 /* Parse a new-initializer.
7420 new-initializer:
7421 ( expression-list [opt] )
7422 braced-init-list
7424 Returns a representation of the expression-list. */
7426 static vec<tree, va_gc> *
7427 cp_parser_new_initializer (cp_parser* parser)
7429 vec<tree, va_gc> *expression_list;
7431 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7433 tree t;
7434 bool expr_non_constant_p;
7435 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7436 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7437 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7438 expression_list = make_tree_vector_single (t);
7440 else
7441 expression_list = (cp_parser_parenthesized_expression_list
7442 (parser, non_attr, /*cast_p=*/false,
7443 /*allow_expansion_p=*/true,
7444 /*non_constant_p=*/NULL));
7446 return expression_list;
7449 /* Parse a delete-expression.
7451 delete-expression:
7452 :: [opt] delete cast-expression
7453 :: [opt] delete [ ] cast-expression
7455 Returns a representation of the expression. */
7457 static tree
7458 cp_parser_delete_expression (cp_parser* parser)
7460 bool global_scope_p;
7461 bool array_p;
7462 tree expression;
7464 /* Look for the optional `::' operator. */
7465 global_scope_p
7466 = (cp_parser_global_scope_opt (parser,
7467 /*current_scope_valid_p=*/false)
7468 != NULL_TREE);
7469 /* Look for the `delete' keyword. */
7470 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7471 /* See if the array syntax is in use. */
7472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7474 /* Consume the `[' token. */
7475 cp_lexer_consume_token (parser->lexer);
7476 /* Look for the `]' token. */
7477 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7478 /* Remember that this is the `[]' construct. */
7479 array_p = true;
7481 else
7482 array_p = false;
7484 /* Parse the cast-expression. */
7485 expression = cp_parser_simple_cast_expression (parser);
7487 /* A delete-expression may not appear in an integral constant
7488 expression. */
7489 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7490 return error_mark_node;
7492 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7493 tf_warning_or_error);
7496 /* Returns true if TOKEN may start a cast-expression and false
7497 otherwise. */
7499 static bool
7500 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7502 cp_token *token = cp_lexer_peek_token (parser->lexer);
7503 switch (token->type)
7505 case CPP_COMMA:
7506 case CPP_SEMICOLON:
7507 case CPP_QUERY:
7508 case CPP_COLON:
7509 case CPP_CLOSE_SQUARE:
7510 case CPP_CLOSE_PAREN:
7511 case CPP_CLOSE_BRACE:
7512 case CPP_DOT:
7513 case CPP_DOT_STAR:
7514 case CPP_DEREF:
7515 case CPP_DEREF_STAR:
7516 case CPP_DIV:
7517 case CPP_MOD:
7518 case CPP_LSHIFT:
7519 case CPP_RSHIFT:
7520 case CPP_LESS:
7521 case CPP_GREATER:
7522 case CPP_LESS_EQ:
7523 case CPP_GREATER_EQ:
7524 case CPP_EQ_EQ:
7525 case CPP_NOT_EQ:
7526 case CPP_EQ:
7527 case CPP_MULT_EQ:
7528 case CPP_DIV_EQ:
7529 case CPP_MOD_EQ:
7530 case CPP_PLUS_EQ:
7531 case CPP_MINUS_EQ:
7532 case CPP_RSHIFT_EQ:
7533 case CPP_LSHIFT_EQ:
7534 case CPP_AND_EQ:
7535 case CPP_XOR_EQ:
7536 case CPP_OR_EQ:
7537 case CPP_XOR:
7538 case CPP_OR:
7539 case CPP_OR_OR:
7540 case CPP_EOF:
7541 return false;
7543 case CPP_OPEN_PAREN:
7544 /* In ((type ()) () the last () isn't a valid cast-expression,
7545 so the whole must be parsed as postfix-expression. */
7546 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7547 != CPP_CLOSE_PAREN;
7549 /* '[' may start a primary-expression in obj-c++. */
7550 case CPP_OPEN_SQUARE:
7551 return c_dialect_objc ();
7553 default:
7554 return true;
7558 /* Parse a cast-expression.
7560 cast-expression:
7561 unary-expression
7562 ( type-id ) cast-expression
7564 ADDRESS_P is true iff the unary-expression is appearing as the
7565 operand of the `&' operator. CAST_P is true if this expression is
7566 the target of a cast.
7568 Returns a representation of the expression. */
7570 static tree
7571 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7572 bool decltype_p, cp_id_kind * pidk)
7574 /* If it's a `(', then we might be looking at a cast. */
7575 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7577 tree type = NULL_TREE;
7578 tree expr = NULL_TREE;
7579 bool compound_literal_p;
7580 const char *saved_message;
7582 /* There's no way to know yet whether or not this is a cast.
7583 For example, `(int (3))' is a unary-expression, while `(int)
7584 3' is a cast. So, we resort to parsing tentatively. */
7585 cp_parser_parse_tentatively (parser);
7586 /* Types may not be defined in a cast. */
7587 saved_message = parser->type_definition_forbidden_message;
7588 parser->type_definition_forbidden_message
7589 = G_("types may not be defined in casts");
7590 /* Consume the `('. */
7591 cp_lexer_consume_token (parser->lexer);
7592 /* A very tricky bit is that `(struct S) { 3 }' is a
7593 compound-literal (which we permit in C++ as an extension).
7594 But, that construct is not a cast-expression -- it is a
7595 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7596 is legal; if the compound-literal were a cast-expression,
7597 you'd need an extra set of parentheses.) But, if we parse
7598 the type-id, and it happens to be a class-specifier, then we
7599 will commit to the parse at that point, because we cannot
7600 undo the action that is done when creating a new class. So,
7601 then we cannot back up and do a postfix-expression.
7603 Therefore, we scan ahead to the closing `)', and check to see
7604 if the token after the `)' is a `{'. If so, we are not
7605 looking at a cast-expression.
7607 Save tokens so that we can put them back. */
7608 cp_lexer_save_tokens (parser->lexer);
7609 /* Skip tokens until the next token is a closing parenthesis.
7610 If we find the closing `)', and the next token is a `{', then
7611 we are looking at a compound-literal. */
7612 compound_literal_p
7613 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7614 /*consume_paren=*/true)
7615 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7616 /* Roll back the tokens we skipped. */
7617 cp_lexer_rollback_tokens (parser->lexer);
7618 /* If we were looking at a compound-literal, simulate an error
7619 so that the call to cp_parser_parse_definitely below will
7620 fail. */
7621 if (compound_literal_p)
7622 cp_parser_simulate_error (parser);
7623 else
7625 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7626 parser->in_type_id_in_expr_p = true;
7627 /* Look for the type-id. */
7628 type = cp_parser_type_id (parser);
7629 /* Look for the closing `)'. */
7630 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7631 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7634 /* Restore the saved message. */
7635 parser->type_definition_forbidden_message = saved_message;
7637 /* At this point this can only be either a cast or a
7638 parenthesized ctor such as `(T ())' that looks like a cast to
7639 function returning T. */
7640 if (!cp_parser_error_occurred (parser)
7641 && cp_parser_tokens_start_cast_expression (parser))
7643 cp_parser_parse_definitely (parser);
7644 expr = cp_parser_cast_expression (parser,
7645 /*address_p=*/false,
7646 /*cast_p=*/true,
7647 /*decltype_p=*/false,
7648 pidk);
7650 /* Warn about old-style casts, if so requested. */
7651 if (warn_old_style_cast
7652 && !in_system_header
7653 && !VOID_TYPE_P (type)
7654 && current_lang_name != lang_name_c)
7655 warning (OPT_Wold_style_cast, "use of old-style cast");
7657 /* Only type conversions to integral or enumeration types
7658 can be used in constant-expressions. */
7659 if (!cast_valid_in_integral_constant_expression_p (type)
7660 && cp_parser_non_integral_constant_expression (parser,
7661 NIC_CAST))
7662 return error_mark_node;
7664 /* Perform the cast. */
7665 expr = build_c_cast (input_location, type, expr);
7666 return expr;
7668 else
7669 cp_parser_abort_tentative_parse (parser);
7672 /* If we get here, then it's not a cast, so it must be a
7673 unary-expression. */
7674 return cp_parser_unary_expression (parser, address_p, cast_p,
7675 decltype_p, pidk);
7678 /* Parse a binary expression of the general form:
7680 pm-expression:
7681 cast-expression
7682 pm-expression .* cast-expression
7683 pm-expression ->* cast-expression
7685 multiplicative-expression:
7686 pm-expression
7687 multiplicative-expression * pm-expression
7688 multiplicative-expression / pm-expression
7689 multiplicative-expression % pm-expression
7691 additive-expression:
7692 multiplicative-expression
7693 additive-expression + multiplicative-expression
7694 additive-expression - multiplicative-expression
7696 shift-expression:
7697 additive-expression
7698 shift-expression << additive-expression
7699 shift-expression >> additive-expression
7701 relational-expression:
7702 shift-expression
7703 relational-expression < shift-expression
7704 relational-expression > shift-expression
7705 relational-expression <= shift-expression
7706 relational-expression >= shift-expression
7708 GNU Extension:
7710 relational-expression:
7711 relational-expression <? shift-expression
7712 relational-expression >? shift-expression
7714 equality-expression:
7715 relational-expression
7716 equality-expression == relational-expression
7717 equality-expression != relational-expression
7719 and-expression:
7720 equality-expression
7721 and-expression & equality-expression
7723 exclusive-or-expression:
7724 and-expression
7725 exclusive-or-expression ^ and-expression
7727 inclusive-or-expression:
7728 exclusive-or-expression
7729 inclusive-or-expression | exclusive-or-expression
7731 logical-and-expression:
7732 inclusive-or-expression
7733 logical-and-expression && inclusive-or-expression
7735 logical-or-expression:
7736 logical-and-expression
7737 logical-or-expression || logical-and-expression
7739 All these are implemented with a single function like:
7741 binary-expression:
7742 simple-cast-expression
7743 binary-expression <token> binary-expression
7745 CAST_P is true if this expression is the target of a cast.
7747 The binops_by_token map is used to get the tree codes for each <token> type.
7748 binary-expressions are associated according to a precedence table. */
7750 #define TOKEN_PRECEDENCE(token) \
7751 (((token->type == CPP_GREATER \
7752 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7753 && !parser->greater_than_is_operator_p) \
7754 ? PREC_NOT_OPERATOR \
7755 : binops_by_token[token->type].prec)
7757 static tree
7758 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7759 bool no_toplevel_fold_p,
7760 bool decltype_p,
7761 enum cp_parser_prec prec,
7762 cp_id_kind * pidk)
7764 cp_parser_expression_stack stack;
7765 cp_parser_expression_stack_entry *sp = &stack[0];
7766 cp_parser_expression_stack_entry current;
7767 tree rhs;
7768 cp_token *token;
7769 enum tree_code rhs_type;
7770 enum cp_parser_prec new_prec, lookahead_prec;
7771 tree overload;
7773 /* Parse the first expression. */
7774 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7775 cast_p, decltype_p, pidk);
7776 current.lhs_type = ERROR_MARK;
7777 current.prec = prec;
7779 if (cp_parser_error_occurred (parser))
7780 return error_mark_node;
7782 for (;;)
7784 /* Get an operator token. */
7785 token = cp_lexer_peek_token (parser->lexer);
7787 if (warn_cxx0x_compat
7788 && token->type == CPP_RSHIFT
7789 && !parser->greater_than_is_operator_p)
7791 if (warning_at (token->location, OPT_Wc__0x_compat,
7792 "%<>>%> operator is treated"
7793 " as two right angle brackets in C++11"))
7794 inform (token->location,
7795 "suggest parentheses around %<>>%> expression");
7798 new_prec = TOKEN_PRECEDENCE (token);
7800 /* Popping an entry off the stack means we completed a subexpression:
7801 - either we found a token which is not an operator (`>' where it is not
7802 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7803 will happen repeatedly;
7804 - or, we found an operator which has lower priority. This is the case
7805 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7806 parsing `3 * 4'. */
7807 if (new_prec <= current.prec)
7809 if (sp == stack)
7810 break;
7811 else
7812 goto pop;
7815 get_rhs:
7816 current.tree_type = binops_by_token[token->type].tree_type;
7817 current.loc = token->location;
7819 /* We used the operator token. */
7820 cp_lexer_consume_token (parser->lexer);
7822 /* For "false && x" or "true || x", x will never be executed;
7823 disable warnings while evaluating it. */
7824 if (current.tree_type == TRUTH_ANDIF_EXPR)
7825 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7826 else if (current.tree_type == TRUTH_ORIF_EXPR)
7827 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7829 /* Extract another operand. It may be the RHS of this expression
7830 or the LHS of a new, higher priority expression. */
7831 rhs = cp_parser_simple_cast_expression (parser);
7832 rhs_type = ERROR_MARK;
7834 /* Get another operator token. Look up its precedence to avoid
7835 building a useless (immediately popped) stack entry for common
7836 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7837 token = cp_lexer_peek_token (parser->lexer);
7838 lookahead_prec = TOKEN_PRECEDENCE (token);
7839 if (lookahead_prec > new_prec)
7841 /* ... and prepare to parse the RHS of the new, higher priority
7842 expression. Since precedence levels on the stack are
7843 monotonically increasing, we do not have to care about
7844 stack overflows. */
7845 *sp = current;
7846 ++sp;
7847 current.lhs = rhs;
7848 current.lhs_type = rhs_type;
7849 current.prec = new_prec;
7850 new_prec = lookahead_prec;
7851 goto get_rhs;
7853 pop:
7854 lookahead_prec = new_prec;
7855 /* If the stack is not empty, we have parsed into LHS the right side
7856 (`4' in the example above) of an expression we had suspended.
7857 We can use the information on the stack to recover the LHS (`3')
7858 from the stack together with the tree code (`MULT_EXPR'), and
7859 the precedence of the higher level subexpression
7860 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7861 which will be used to actually build the additive expression. */
7862 rhs = current.lhs;
7863 rhs_type = current.lhs_type;
7864 --sp;
7865 current = *sp;
7868 /* Undo the disabling of warnings done above. */
7869 if (current.tree_type == TRUTH_ANDIF_EXPR)
7870 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7871 else if (current.tree_type == TRUTH_ORIF_EXPR)
7872 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7874 overload = NULL;
7875 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7876 ERROR_MARK for everything that is not a binary expression.
7877 This makes warn_about_parentheses miss some warnings that
7878 involve unary operators. For unary expressions we should
7879 pass the correct tree_code unless the unary expression was
7880 surrounded by parentheses.
7882 if (no_toplevel_fold_p
7883 && lookahead_prec <= current.prec
7884 && sp == stack)
7885 current.lhs = build2 (current.tree_type,
7886 TREE_CODE_CLASS (current.tree_type)
7887 == tcc_comparison
7888 ? boolean_type_node : TREE_TYPE (current.lhs),
7889 current.lhs, rhs);
7890 else
7891 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7892 current.lhs, current.lhs_type,
7893 rhs, rhs_type, &overload,
7894 complain_flags (decltype_p));
7895 current.lhs_type = current.tree_type;
7896 if (EXPR_P (current.lhs))
7897 SET_EXPR_LOCATION (current.lhs, current.loc);
7899 /* If the binary operator required the use of an overloaded operator,
7900 then this expression cannot be an integral constant-expression.
7901 An overloaded operator can be used even if both operands are
7902 otherwise permissible in an integral constant-expression if at
7903 least one of the operands is of enumeration type. */
7905 if (overload
7906 && cp_parser_non_integral_constant_expression (parser,
7907 NIC_OVERLOADED))
7908 return error_mark_node;
7911 return current.lhs;
7914 static tree
7915 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7916 bool no_toplevel_fold_p,
7917 enum cp_parser_prec prec,
7918 cp_id_kind * pidk)
7920 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7921 /*decltype*/false, prec, pidk);
7924 /* Parse the `? expression : assignment-expression' part of a
7925 conditional-expression. The LOGICAL_OR_EXPR is the
7926 logical-or-expression that started the conditional-expression.
7927 Returns a representation of the entire conditional-expression.
7929 This routine is used by cp_parser_assignment_expression.
7931 ? expression : assignment-expression
7933 GNU Extensions:
7935 ? : assignment-expression */
7937 static tree
7938 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7940 tree expr;
7941 tree assignment_expr;
7942 struct cp_token *token;
7943 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7945 /* Consume the `?' token. */
7946 cp_lexer_consume_token (parser->lexer);
7947 token = cp_lexer_peek_token (parser->lexer);
7948 if (cp_parser_allow_gnu_extensions_p (parser)
7949 && token->type == CPP_COLON)
7951 pedwarn (token->location, OPT_Wpedantic,
7952 "ISO C++ does not allow ?: with omitted middle operand");
7953 /* Implicit true clause. */
7954 expr = NULL_TREE;
7955 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7956 warn_for_omitted_condop (token->location, logical_or_expr);
7958 else
7960 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7961 parser->colon_corrects_to_scope_p = false;
7962 /* Parse the expression. */
7963 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7964 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7965 c_inhibit_evaluation_warnings +=
7966 ((logical_or_expr == truthvalue_true_node)
7967 - (logical_or_expr == truthvalue_false_node));
7968 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7971 /* The next token should be a `:'. */
7972 cp_parser_require (parser, CPP_COLON, RT_COLON);
7973 /* Parse the assignment-expression. */
7974 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7975 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7977 /* Build the conditional-expression. */
7978 return build_x_conditional_expr (loc, logical_or_expr,
7979 expr,
7980 assignment_expr,
7981 tf_warning_or_error);
7984 /* Parse an assignment-expression.
7986 assignment-expression:
7987 conditional-expression
7988 logical-or-expression assignment-operator assignment_expression
7989 throw-expression
7991 CAST_P is true if this expression is the target of a cast.
7992 DECLTYPE_P is true if this expression is the operand of decltype.
7994 Returns a representation for the expression. */
7996 static tree
7997 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7998 bool decltype_p, cp_id_kind * pidk)
8000 tree expr;
8002 /* If the next token is the `throw' keyword, then we're looking at
8003 a throw-expression. */
8004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8005 expr = cp_parser_throw_expression (parser);
8006 /* Otherwise, it must be that we are looking at a
8007 logical-or-expression. */
8008 else
8010 /* Parse the binary expressions (logical-or-expression). */
8011 expr = cp_parser_binary_expression (parser, cast_p, false,
8012 decltype_p,
8013 PREC_NOT_OPERATOR, pidk);
8014 /* If the next token is a `?' then we're actually looking at a
8015 conditional-expression. */
8016 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8017 return cp_parser_question_colon_clause (parser, expr);
8018 else
8020 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8022 /* If it's an assignment-operator, we're using the second
8023 production. */
8024 enum tree_code assignment_operator
8025 = cp_parser_assignment_operator_opt (parser);
8026 if (assignment_operator != ERROR_MARK)
8028 bool non_constant_p;
8029 location_t saved_input_location;
8031 /* Parse the right-hand side of the assignment. */
8032 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8034 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8035 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8037 /* An assignment may not appear in a
8038 constant-expression. */
8039 if (cp_parser_non_integral_constant_expression (parser,
8040 NIC_ASSIGNMENT))
8041 return error_mark_node;
8042 /* Build the assignment expression. Its default
8043 location is the location of the '=' token. */
8044 saved_input_location = input_location;
8045 input_location = loc;
8046 expr = build_x_modify_expr (loc, expr,
8047 assignment_operator,
8048 rhs,
8049 complain_flags (decltype_p));
8050 input_location = saved_input_location;
8055 return expr;
8058 static tree
8059 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8060 cp_id_kind * pidk)
8062 return cp_parser_assignment_expression (parser, cast_p,
8063 /*decltype*/false, pidk);
8066 /* Parse an (optional) assignment-operator.
8068 assignment-operator: one of
8069 = *= /= %= += -= >>= <<= &= ^= |=
8071 GNU Extension:
8073 assignment-operator: one of
8074 <?= >?=
8076 If the next token is an assignment operator, the corresponding tree
8077 code is returned, and the token is consumed. For example, for
8078 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8079 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8080 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8081 operator, ERROR_MARK is returned. */
8083 static enum tree_code
8084 cp_parser_assignment_operator_opt (cp_parser* parser)
8086 enum tree_code op;
8087 cp_token *token;
8089 /* Peek at the next token. */
8090 token = cp_lexer_peek_token (parser->lexer);
8092 switch (token->type)
8094 case CPP_EQ:
8095 op = NOP_EXPR;
8096 break;
8098 case CPP_MULT_EQ:
8099 op = MULT_EXPR;
8100 break;
8102 case CPP_DIV_EQ:
8103 op = TRUNC_DIV_EXPR;
8104 break;
8106 case CPP_MOD_EQ:
8107 op = TRUNC_MOD_EXPR;
8108 break;
8110 case CPP_PLUS_EQ:
8111 op = PLUS_EXPR;
8112 break;
8114 case CPP_MINUS_EQ:
8115 op = MINUS_EXPR;
8116 break;
8118 case CPP_RSHIFT_EQ:
8119 op = RSHIFT_EXPR;
8120 break;
8122 case CPP_LSHIFT_EQ:
8123 op = LSHIFT_EXPR;
8124 break;
8126 case CPP_AND_EQ:
8127 op = BIT_AND_EXPR;
8128 break;
8130 case CPP_XOR_EQ:
8131 op = BIT_XOR_EXPR;
8132 break;
8134 case CPP_OR_EQ:
8135 op = BIT_IOR_EXPR;
8136 break;
8138 default:
8139 /* Nothing else is an assignment operator. */
8140 op = ERROR_MARK;
8143 /* If it was an assignment operator, consume it. */
8144 if (op != ERROR_MARK)
8145 cp_lexer_consume_token (parser->lexer);
8147 return op;
8150 /* Parse an expression.
8152 expression:
8153 assignment-expression
8154 expression , assignment-expression
8156 CAST_P is true if this expression is the target of a cast.
8157 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8158 except possibly parenthesized or on the RHS of a comma (N3276).
8160 Returns a representation of the expression. */
8162 static tree
8163 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8164 cp_id_kind * pidk)
8166 tree expression = NULL_TREE;
8167 location_t loc = UNKNOWN_LOCATION;
8169 while (true)
8171 tree assignment_expression;
8173 /* Parse the next assignment-expression. */
8174 assignment_expression
8175 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8177 /* We don't create a temporary for a call that is the immediate operand
8178 of decltype or on the RHS of a comma. But when we see a comma, we
8179 need to create a temporary for a call on the LHS. */
8180 if (decltype_p && !processing_template_decl
8181 && TREE_CODE (assignment_expression) == CALL_EXPR
8182 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8183 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8184 assignment_expression
8185 = build_cplus_new (TREE_TYPE (assignment_expression),
8186 assignment_expression, tf_warning_or_error);
8188 /* If this is the first assignment-expression, we can just
8189 save it away. */
8190 if (!expression)
8191 expression = assignment_expression;
8192 else
8193 expression = build_x_compound_expr (loc, expression,
8194 assignment_expression,
8195 complain_flags (decltype_p));
8196 /* If the next token is not a comma, then we are done with the
8197 expression. */
8198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8199 break;
8200 /* Consume the `,'. */
8201 loc = cp_lexer_peek_token (parser->lexer)->location;
8202 cp_lexer_consume_token (parser->lexer);
8203 /* A comma operator cannot appear in a constant-expression. */
8204 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8205 expression = error_mark_node;
8208 return expression;
8211 static inline tree
8212 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8214 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8217 /* Parse a constant-expression.
8219 constant-expression:
8220 conditional-expression
8222 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8223 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8224 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8225 is false, NON_CONSTANT_P should be NULL. */
8227 static tree
8228 cp_parser_constant_expression (cp_parser* parser,
8229 bool allow_non_constant_p,
8230 bool *non_constant_p)
8232 bool saved_integral_constant_expression_p;
8233 bool saved_allow_non_integral_constant_expression_p;
8234 bool saved_non_integral_constant_expression_p;
8235 tree expression;
8237 /* It might seem that we could simply parse the
8238 conditional-expression, and then check to see if it were
8239 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8240 one that the compiler can figure out is constant, possibly after
8241 doing some simplifications or optimizations. The standard has a
8242 precise definition of constant-expression, and we must honor
8243 that, even though it is somewhat more restrictive.
8245 For example:
8247 int i[(2, 3)];
8249 is not a legal declaration, because `(2, 3)' is not a
8250 constant-expression. The `,' operator is forbidden in a
8251 constant-expression. However, GCC's constant-folding machinery
8252 will fold this operation to an INTEGER_CST for `3'. */
8254 /* Save the old settings. */
8255 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8256 saved_allow_non_integral_constant_expression_p
8257 = parser->allow_non_integral_constant_expression_p;
8258 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8259 /* We are now parsing a constant-expression. */
8260 parser->integral_constant_expression_p = true;
8261 parser->allow_non_integral_constant_expression_p
8262 = (allow_non_constant_p || cxx_dialect >= cxx11);
8263 parser->non_integral_constant_expression_p = false;
8264 /* Although the grammar says "conditional-expression", we parse an
8265 "assignment-expression", which also permits "throw-expression"
8266 and the use of assignment operators. In the case that
8267 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8268 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8269 actually essential that we look for an assignment-expression.
8270 For example, cp_parser_initializer_clauses uses this function to
8271 determine whether a particular assignment-expression is in fact
8272 constant. */
8273 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8274 /* Restore the old settings. */
8275 parser->integral_constant_expression_p
8276 = saved_integral_constant_expression_p;
8277 parser->allow_non_integral_constant_expression_p
8278 = saved_allow_non_integral_constant_expression_p;
8279 if (cxx_dialect >= cxx11)
8281 /* Require an rvalue constant expression here; that's what our
8282 callers expect. Reference constant expressions are handled
8283 separately in e.g. cp_parser_template_argument. */
8284 bool is_const = potential_rvalue_constant_expression (expression);
8285 parser->non_integral_constant_expression_p = !is_const;
8286 if (!is_const && !allow_non_constant_p)
8287 require_potential_rvalue_constant_expression (expression);
8289 if (allow_non_constant_p)
8290 *non_constant_p = parser->non_integral_constant_expression_p;
8291 parser->non_integral_constant_expression_p
8292 = saved_non_integral_constant_expression_p;
8294 return expression;
8297 /* Parse __builtin_offsetof.
8299 offsetof-expression:
8300 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8302 offsetof-member-designator:
8303 id-expression
8304 | offsetof-member-designator "." id-expression
8305 | offsetof-member-designator "[" expression "]"
8306 | offsetof-member-designator "->" id-expression */
8308 static tree
8309 cp_parser_builtin_offsetof (cp_parser *parser)
8311 int save_ice_p, save_non_ice_p;
8312 tree type, expr;
8313 cp_id_kind dummy;
8314 cp_token *token;
8316 /* We're about to accept non-integral-constant things, but will
8317 definitely yield an integral constant expression. Save and
8318 restore these values around our local parsing. */
8319 save_ice_p = parser->integral_constant_expression_p;
8320 save_non_ice_p = parser->non_integral_constant_expression_p;
8322 /* Consume the "__builtin_offsetof" token. */
8323 cp_lexer_consume_token (parser->lexer);
8324 /* Consume the opening `('. */
8325 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8326 /* Parse the type-id. */
8327 type = cp_parser_type_id (parser);
8328 /* Look for the `,'. */
8329 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8330 token = cp_lexer_peek_token (parser->lexer);
8332 /* Build the (type *)null that begins the traditional offsetof macro. */
8333 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8334 tf_warning_or_error);
8336 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8337 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8338 true, &dummy, token->location);
8339 while (true)
8341 token = cp_lexer_peek_token (parser->lexer);
8342 switch (token->type)
8344 case CPP_OPEN_SQUARE:
8345 /* offsetof-member-designator "[" expression "]" */
8346 expr = cp_parser_postfix_open_square_expression (parser, expr,
8347 true, false);
8348 break;
8350 case CPP_DEREF:
8351 /* offsetof-member-designator "->" identifier */
8352 expr = grok_array_decl (token->location, expr,
8353 integer_zero_node, false);
8354 /* FALLTHRU */
8356 case CPP_DOT:
8357 /* offsetof-member-designator "." identifier */
8358 cp_lexer_consume_token (parser->lexer);
8359 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8360 expr, true, &dummy,
8361 token->location);
8362 break;
8364 case CPP_CLOSE_PAREN:
8365 /* Consume the ")" token. */
8366 cp_lexer_consume_token (parser->lexer);
8367 goto success;
8369 default:
8370 /* Error. We know the following require will fail, but
8371 that gives the proper error message. */
8372 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8373 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8374 expr = error_mark_node;
8375 goto failure;
8379 success:
8380 /* If we're processing a template, we can't finish the semantics yet.
8381 Otherwise we can fold the entire expression now. */
8382 if (processing_template_decl)
8383 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8384 else
8385 expr = finish_offsetof (expr);
8387 failure:
8388 parser->integral_constant_expression_p = save_ice_p;
8389 parser->non_integral_constant_expression_p = save_non_ice_p;
8391 return expr;
8394 /* Parse a trait expression.
8396 Returns a representation of the expression, the underlying type
8397 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8399 static tree
8400 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8402 cp_trait_kind kind;
8403 tree type1, type2 = NULL_TREE;
8404 bool binary = false;
8405 cp_decl_specifier_seq decl_specs;
8407 switch (keyword)
8409 case RID_HAS_NOTHROW_ASSIGN:
8410 kind = CPTK_HAS_NOTHROW_ASSIGN;
8411 break;
8412 case RID_HAS_NOTHROW_CONSTRUCTOR:
8413 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8414 break;
8415 case RID_HAS_NOTHROW_COPY:
8416 kind = CPTK_HAS_NOTHROW_COPY;
8417 break;
8418 case RID_HAS_TRIVIAL_ASSIGN:
8419 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8420 break;
8421 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8422 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8423 break;
8424 case RID_HAS_TRIVIAL_COPY:
8425 kind = CPTK_HAS_TRIVIAL_COPY;
8426 break;
8427 case RID_HAS_TRIVIAL_DESTRUCTOR:
8428 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8429 break;
8430 case RID_HAS_VIRTUAL_DESTRUCTOR:
8431 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8432 break;
8433 case RID_IS_ABSTRACT:
8434 kind = CPTK_IS_ABSTRACT;
8435 break;
8436 case RID_IS_BASE_OF:
8437 kind = CPTK_IS_BASE_OF;
8438 binary = true;
8439 break;
8440 case RID_IS_CLASS:
8441 kind = CPTK_IS_CLASS;
8442 break;
8443 case RID_IS_CONVERTIBLE_TO:
8444 kind = CPTK_IS_CONVERTIBLE_TO;
8445 binary = true;
8446 break;
8447 case RID_IS_EMPTY:
8448 kind = CPTK_IS_EMPTY;
8449 break;
8450 case RID_IS_ENUM:
8451 kind = CPTK_IS_ENUM;
8452 break;
8453 case RID_IS_FINAL:
8454 kind = CPTK_IS_FINAL;
8455 break;
8456 case RID_IS_LITERAL_TYPE:
8457 kind = CPTK_IS_LITERAL_TYPE;
8458 break;
8459 case RID_IS_POD:
8460 kind = CPTK_IS_POD;
8461 break;
8462 case RID_IS_POLYMORPHIC:
8463 kind = CPTK_IS_POLYMORPHIC;
8464 break;
8465 case RID_IS_STD_LAYOUT:
8466 kind = CPTK_IS_STD_LAYOUT;
8467 break;
8468 case RID_IS_TRIVIAL:
8469 kind = CPTK_IS_TRIVIAL;
8470 break;
8471 case RID_IS_UNION:
8472 kind = CPTK_IS_UNION;
8473 break;
8474 case RID_UNDERLYING_TYPE:
8475 kind = CPTK_UNDERLYING_TYPE;
8476 break;
8477 case RID_BASES:
8478 kind = CPTK_BASES;
8479 break;
8480 case RID_DIRECT_BASES:
8481 kind = CPTK_DIRECT_BASES;
8482 break;
8483 default:
8484 gcc_unreachable ();
8487 /* Consume the token. */
8488 cp_lexer_consume_token (parser->lexer);
8490 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8492 type1 = cp_parser_type_id (parser);
8494 if (type1 == error_mark_node)
8495 return error_mark_node;
8497 /* Build a trivial decl-specifier-seq. */
8498 clear_decl_specs (&decl_specs);
8499 decl_specs.type = type1;
8501 /* Call grokdeclarator to figure out what type this is. */
8502 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8503 /*initialized=*/0, /*attrlist=*/NULL);
8505 if (binary)
8507 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8509 type2 = cp_parser_type_id (parser);
8511 if (type2 == error_mark_node)
8512 return error_mark_node;
8514 /* Build a trivial decl-specifier-seq. */
8515 clear_decl_specs (&decl_specs);
8516 decl_specs.type = type2;
8518 /* Call grokdeclarator to figure out what type this is. */
8519 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8520 /*initialized=*/0, /*attrlist=*/NULL);
8523 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8525 /* Complete the trait expression, which may mean either processing
8526 the trait expr now or saving it for template instantiation. */
8527 switch(kind)
8529 case CPTK_UNDERLYING_TYPE:
8530 return finish_underlying_type (type1);
8531 case CPTK_BASES:
8532 return finish_bases (type1, false);
8533 case CPTK_DIRECT_BASES:
8534 return finish_bases (type1, true);
8535 default:
8536 return finish_trait_expr (kind, type1, type2);
8540 /* Lambdas that appear in variable initializer or default argument scope
8541 get that in their mangling, so we need to record it. We might as well
8542 use the count for function and namespace scopes as well. */
8543 static GTY(()) tree lambda_scope;
8544 static GTY(()) int lambda_count;
8545 typedef struct GTY(()) tree_int
8547 tree t;
8548 int i;
8549 } tree_int;
8550 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8552 static void
8553 start_lambda_scope (tree decl)
8555 tree_int ti;
8556 gcc_assert (decl);
8557 /* Once we're inside a function, we ignore other scopes and just push
8558 the function again so that popping works properly. */
8559 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8560 decl = current_function_decl;
8561 ti.t = lambda_scope;
8562 ti.i = lambda_count;
8563 vec_safe_push (lambda_scope_stack, ti);
8564 if (lambda_scope != decl)
8566 /* Don't reset the count if we're still in the same function. */
8567 lambda_scope = decl;
8568 lambda_count = 0;
8572 static void
8573 record_lambda_scope (tree lambda)
8575 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8576 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8579 static void
8580 finish_lambda_scope (void)
8582 tree_int *p = &lambda_scope_stack->last ();
8583 if (lambda_scope != p->t)
8585 lambda_scope = p->t;
8586 lambda_count = p->i;
8588 lambda_scope_stack->pop ();
8591 /* Parse a lambda expression.
8593 lambda-expression:
8594 lambda-introducer lambda-declarator [opt] compound-statement
8596 Returns a representation of the expression. */
8598 static tree
8599 cp_parser_lambda_expression (cp_parser* parser)
8601 tree lambda_expr = build_lambda_expr ();
8602 tree type;
8603 bool ok;
8605 LAMBDA_EXPR_LOCATION (lambda_expr)
8606 = cp_lexer_peek_token (parser->lexer)->location;
8608 if (cp_unevaluated_operand)
8609 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8610 "lambda-expression in unevaluated context");
8612 /* We may be in the middle of deferred access check. Disable
8613 it now. */
8614 push_deferring_access_checks (dk_no_deferred);
8616 cp_parser_lambda_introducer (parser, lambda_expr);
8618 type = begin_lambda_type (lambda_expr);
8619 if (type == error_mark_node)
8620 return error_mark_node;
8622 record_lambda_scope (lambda_expr);
8624 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8625 determine_visibility (TYPE_NAME (type));
8627 /* Now that we've started the type, add the capture fields for any
8628 explicit captures. */
8629 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8632 /* Inside the class, surrounding template-parameter-lists do not apply. */
8633 unsigned int saved_num_template_parameter_lists
8634 = parser->num_template_parameter_lists;
8635 unsigned char in_statement = parser->in_statement;
8636 bool in_switch_statement_p = parser->in_switch_statement_p;
8637 bool fully_implicit_function_template_p = parser->fully_implicit_function_template_p;
8639 parser->num_template_parameter_lists = 0;
8640 parser->in_statement = 0;
8641 parser->in_switch_statement_p = false;
8642 parser->fully_implicit_function_template_p = false;
8644 /* By virtue of defining a local class, a lambda expression has access to
8645 the private variables of enclosing classes. */
8647 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8649 if (ok)
8650 cp_parser_lambda_body (parser, lambda_expr);
8651 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8652 cp_parser_skip_to_end_of_block_or_statement (parser);
8654 /* The capture list was built up in reverse order; fix that now. */
8655 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8656 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8658 if (ok)
8659 maybe_add_lambda_conv_op (type);
8661 type = finish_struct (type, /*attributes=*/NULL_TREE);
8663 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8664 parser->in_statement = in_statement;
8665 parser->in_switch_statement_p = in_switch_statement_p;
8666 parser->fully_implicit_function_template_p = fully_implicit_function_template_p;
8669 pop_deferring_access_checks ();
8671 /* This field is only used during parsing of the lambda. */
8672 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8674 /* This lambda shouldn't have any proxies left at this point. */
8675 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8676 /* And now that we're done, push proxies for an enclosing lambda. */
8677 insert_pending_capture_proxies ();
8679 if (ok)
8680 return build_lambda_object (lambda_expr);
8681 else
8682 return error_mark_node;
8685 /* Parse the beginning of a lambda expression.
8687 lambda-introducer:
8688 [ lambda-capture [opt] ]
8690 LAMBDA_EXPR is the current representation of the lambda expression. */
8692 static void
8693 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8695 /* Need commas after the first capture. */
8696 bool first = true;
8698 /* Eat the leading `['. */
8699 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8701 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8702 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8703 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8704 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8705 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8706 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8708 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8710 cp_lexer_consume_token (parser->lexer);
8711 first = false;
8714 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8716 cp_token* capture_token;
8717 tree capture_id;
8718 tree capture_init_expr;
8719 cp_id_kind idk = CP_ID_KIND_NONE;
8720 bool explicit_init_p = false;
8722 enum capture_kind_type
8724 BY_COPY,
8725 BY_REFERENCE
8727 enum capture_kind_type capture_kind = BY_COPY;
8729 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8731 error ("expected end of capture-list");
8732 return;
8735 if (first)
8736 first = false;
8737 else
8738 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8740 /* Possibly capture `this'. */
8741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8743 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8744 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8745 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8746 "with by-copy capture default");
8747 cp_lexer_consume_token (parser->lexer);
8748 add_capture (lambda_expr,
8749 /*id=*/this_identifier,
8750 /*initializer=*/finish_this_expr(),
8751 /*by_reference_p=*/false,
8752 explicit_init_p);
8753 continue;
8756 /* Remember whether we want to capture as a reference or not. */
8757 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8759 capture_kind = BY_REFERENCE;
8760 cp_lexer_consume_token (parser->lexer);
8763 /* Get the identifier. */
8764 capture_token = cp_lexer_peek_token (parser->lexer);
8765 capture_id = cp_parser_identifier (parser);
8767 if (capture_id == error_mark_node)
8768 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8769 delimiters, but I modified this to stop on unnested ']' as well. It
8770 was already changed to stop on unnested '}', so the
8771 "closing_parenthesis" name is no more misleading with my change. */
8773 cp_parser_skip_to_closing_parenthesis (parser,
8774 /*recovering=*/true,
8775 /*or_comma=*/true,
8776 /*consume_paren=*/true);
8777 break;
8780 /* Find the initializer for this capture. */
8781 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8782 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8783 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8785 bool direct, non_constant;
8786 /* An explicit initializer exists. */
8787 if (cxx_dialect < cxx1y)
8788 pedwarn (input_location, 0,
8789 "lambda capture initializers "
8790 "only available with -std=c++1y or -std=gnu++1y");
8791 capture_init_expr = cp_parser_initializer (parser, &direct,
8792 &non_constant);
8793 explicit_init_p = true;
8795 else
8797 const char* error_msg;
8799 /* Turn the identifier into an id-expression. */
8800 capture_init_expr
8801 = cp_parser_lookup_name_simple (parser, capture_id,
8802 capture_token->location);
8804 if (capture_init_expr == error_mark_node)
8806 unqualified_name_lookup_error (capture_id);
8807 continue;
8809 else if (DECL_P (capture_init_expr)
8810 && (!VAR_P (capture_init_expr)
8811 && TREE_CODE (capture_init_expr) != PARM_DECL))
8813 error_at (capture_token->location,
8814 "capture of non-variable %qD ",
8815 capture_init_expr);
8816 inform (0, "%q+#D declared here", capture_init_expr);
8817 continue;
8819 if (VAR_P (capture_init_expr)
8820 && decl_storage_duration (capture_init_expr) != dk_auto)
8822 pedwarn (capture_token->location, 0, "capture of variable "
8823 "%qD with non-automatic storage duration",
8824 capture_init_expr);
8825 inform (0, "%q+#D declared here", capture_init_expr);
8826 continue;
8829 capture_init_expr
8830 = finish_id_expression
8831 (capture_id,
8832 capture_init_expr,
8833 parser->scope,
8834 &idk,
8835 /*integral_constant_expression_p=*/false,
8836 /*allow_non_integral_constant_expression_p=*/false,
8837 /*non_integral_constant_expression_p=*/NULL,
8838 /*template_p=*/false,
8839 /*done=*/true,
8840 /*address_p=*/false,
8841 /*template_arg_p=*/false,
8842 &error_msg,
8843 capture_token->location);
8845 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8847 cp_lexer_consume_token (parser->lexer);
8848 capture_init_expr = make_pack_expansion (capture_init_expr);
8850 else
8851 check_for_bare_parameter_packs (capture_init_expr);
8854 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8855 && !explicit_init_p)
8857 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8858 && capture_kind == BY_COPY)
8859 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8860 "of %qD redundant with by-copy capture default",
8861 capture_id);
8862 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8863 && capture_kind == BY_REFERENCE)
8864 pedwarn (capture_token->location, 0, "explicit by-reference "
8865 "capture of %qD redundant with by-reference capture "
8866 "default", capture_id);
8869 add_capture (lambda_expr,
8870 capture_id,
8871 capture_init_expr,
8872 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8873 explicit_init_p);
8876 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8879 /* Parse the (optional) middle of a lambda expression.
8881 lambda-declarator:
8882 < template-parameter-list [opt] >
8883 ( parameter-declaration-clause [opt] )
8884 attribute-specifier [opt]
8885 mutable [opt]
8886 exception-specification [opt]
8887 lambda-return-type-clause [opt]
8889 LAMBDA_EXPR is the current representation of the lambda expression. */
8891 static bool
8892 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8894 /* 5.1.1.4 of the standard says:
8895 If a lambda-expression does not include a lambda-declarator, it is as if
8896 the lambda-declarator were ().
8897 This means an empty parameter list, no attributes, and no exception
8898 specification. */
8899 tree param_list = void_list_node;
8900 tree attributes = NULL_TREE;
8901 tree exception_spec = NULL_TREE;
8902 tree template_param_list = NULL_TREE;
8904 /* The template-parameter-list is optional, but must begin with
8905 an opening angle if present. */
8906 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8908 if (cxx_dialect < cxx1y)
8909 pedwarn (parser->lexer->next_token->location, 0,
8910 "lambda templates are only available with "
8911 "-std=c++1y or -std=gnu++1y");
8913 cp_lexer_consume_token (parser->lexer);
8915 template_param_list = cp_parser_template_parameter_list (parser);
8917 cp_parser_skip_to_end_of_template_parameter_list (parser);
8919 /* We just processed one more parameter list. */
8920 ++parser->num_template_parameter_lists;
8923 /* The parameter-declaration-clause is optional (unless
8924 template-parameter-list was given), but must begin with an
8925 opening parenthesis if present. */
8926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8928 cp_lexer_consume_token (parser->lexer);
8930 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8932 /* Parse parameters. */
8933 param_list = cp_parser_parameter_declaration_clause (parser);
8935 /* Default arguments shall not be specified in the
8936 parameter-declaration-clause of a lambda-declarator. */
8937 for (tree t = param_list; t; t = TREE_CHAIN (t))
8938 if (TREE_PURPOSE (t))
8939 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8940 "default argument specified for lambda parameter");
8942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8944 attributes = cp_parser_attributes_opt (parser);
8946 /* Parse optional `mutable' keyword. */
8947 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8949 cp_lexer_consume_token (parser->lexer);
8950 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8953 /* Parse optional exception specification. */
8954 exception_spec = cp_parser_exception_specification_opt (parser);
8956 /* Parse optional trailing return type. */
8957 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8959 cp_lexer_consume_token (parser->lexer);
8960 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8961 = cp_parser_trailing_type_id (parser);
8964 /* The function parameters must be in scope all the way until after the
8965 trailing-return-type in case of decltype. */
8966 pop_bindings_and_leave_scope ();
8968 else if (template_param_list != NULL_TREE) // generate diagnostic
8969 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8971 /* Create the function call operator.
8973 Messing with declarators like this is no uglier than building up the
8974 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8975 other code. */
8977 cp_decl_specifier_seq return_type_specs;
8978 cp_declarator* declarator;
8979 tree fco;
8980 int quals;
8981 void *p;
8983 clear_decl_specs (&return_type_specs);
8984 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8985 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8986 else
8987 /* Maybe we will deduce the return type later. */
8988 return_type_specs.type = make_auto ();
8990 p = obstack_alloc (&declarator_obstack, 0);
8992 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8993 sfk_none);
8995 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8996 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8997 declarator = make_call_declarator (declarator, param_list, quals,
8998 VIRT_SPEC_UNSPECIFIED,
8999 REF_QUAL_NONE,
9000 exception_spec,
9001 /*late_return_type=*/NULL_TREE);
9002 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9004 fco = grokmethod (&return_type_specs,
9005 declarator,
9006 attributes);
9007 if (fco != error_mark_node)
9009 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9010 DECL_ARTIFICIAL (fco) = 1;
9011 /* Give the object parameter a different name. */
9012 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9013 if (template_param_list)
9015 fco = finish_member_template_decl (fco);
9016 finish_template_decl (template_param_list);
9017 --parser->num_template_parameter_lists;
9019 else if (parser->fully_implicit_function_template_p)
9020 fco = finish_fully_implicit_template (parser, fco);
9023 finish_member_declaration (fco);
9025 obstack_free (&declarator_obstack, p);
9027 return (fco != error_mark_node);
9031 /* Parse the body of a lambda expression, which is simply
9033 compound-statement
9035 but which requires special handling.
9036 LAMBDA_EXPR is the current representation of the lambda expression. */
9038 static void
9039 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9041 bool nested = (current_function_decl != NULL_TREE);
9042 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9043 if (nested)
9044 push_function_context ();
9045 else
9046 /* Still increment function_depth so that we don't GC in the
9047 middle of an expression. */
9048 ++function_depth;
9049 /* Clear this in case we're in the middle of a default argument. */
9050 parser->local_variables_forbidden_p = false;
9052 /* Finish the function call operator
9053 - class_specifier
9054 + late_parsing_for_member
9055 + function_definition_after_declarator
9056 + ctor_initializer_opt_and_function_body */
9058 tree fco = lambda_function (lambda_expr);
9059 tree body;
9060 bool done = false;
9061 tree compound_stmt;
9062 tree cap;
9064 /* Let the front end know that we are going to be defining this
9065 function. */
9066 start_preparsed_function (fco,
9067 NULL_TREE,
9068 SF_PRE_PARSED | SF_INCLASS_INLINE);
9070 start_lambda_scope (fco);
9071 body = begin_function_body ();
9073 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9074 goto out;
9076 /* Push the proxies for any explicit captures. */
9077 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9078 cap = TREE_CHAIN (cap))
9079 build_capture_proxy (TREE_PURPOSE (cap));
9081 compound_stmt = begin_compound_stmt (0);
9083 /* 5.1.1.4 of the standard says:
9084 If a lambda-expression does not include a trailing-return-type, it
9085 is as if the trailing-return-type denotes the following type:
9086 * if the compound-statement is of the form
9087 { return attribute-specifier [opt] expression ; }
9088 the type of the returned expression after lvalue-to-rvalue
9089 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9090 (_conv.array_ 4.2), and function-to-pointer conversion
9091 (_conv.func_ 4.3);
9092 * otherwise, void. */
9094 /* In a lambda that has neither a lambda-return-type-clause
9095 nor a deducible form, errors should be reported for return statements
9096 in the body. Since we used void as the placeholder return type, parsing
9097 the body as usual will give such desired behavior. */
9098 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9099 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9100 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9102 tree expr = NULL_TREE;
9103 cp_id_kind idk = CP_ID_KIND_NONE;
9105 /* Parse tentatively in case there's more after the initial return
9106 statement. */
9107 cp_parser_parse_tentatively (parser);
9109 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9111 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9113 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9114 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9116 if (cp_parser_parse_definitely (parser))
9118 if (!processing_template_decl)
9119 apply_deduced_return_type (fco, lambda_return_type (expr));
9121 /* Will get error here if type not deduced yet. */
9122 finish_return_stmt (expr);
9124 done = true;
9128 if (!done)
9130 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9131 cp_parser_label_declaration (parser);
9132 cp_parser_statement_seq_opt (parser, NULL_TREE);
9133 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9136 finish_compound_stmt (compound_stmt);
9138 out:
9139 finish_function_body (body);
9140 finish_lambda_scope ();
9142 /* Finish the function and generate code for it if necessary. */
9143 tree fn = finish_function (/*inline*/2);
9145 /* Only expand if the call op is not a template. */
9146 if (!DECL_TEMPLATE_INFO (fco))
9147 expand_or_defer_fn (fn);
9150 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9151 if (nested)
9152 pop_function_context();
9153 else
9154 --function_depth;
9157 /* Statements [gram.stmt.stmt] */
9159 /* Parse a statement.
9161 statement:
9162 labeled-statement
9163 expression-statement
9164 compound-statement
9165 selection-statement
9166 iteration-statement
9167 jump-statement
9168 declaration-statement
9169 try-block
9171 C++11:
9173 statement:
9174 labeled-statement
9175 attribute-specifier-seq (opt) expression-statement
9176 attribute-specifier-seq (opt) compound-statement
9177 attribute-specifier-seq (opt) selection-statement
9178 attribute-specifier-seq (opt) iteration-statement
9179 attribute-specifier-seq (opt) jump-statement
9180 declaration-statement
9181 attribute-specifier-seq (opt) try-block
9183 TM Extension:
9185 statement:
9186 atomic-statement
9188 IN_COMPOUND is true when the statement is nested inside a
9189 cp_parser_compound_statement; this matters for certain pragmas.
9191 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9192 is a (possibly labeled) if statement which is not enclosed in braces
9193 and has an else clause. This is used to implement -Wparentheses. */
9195 static void
9196 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9197 bool in_compound, bool *if_p)
9199 tree statement, std_attrs = NULL_TREE;
9200 cp_token *token;
9201 location_t statement_location, attrs_location;
9203 restart:
9204 if (if_p != NULL)
9205 *if_p = false;
9206 /* There is no statement yet. */
9207 statement = NULL_TREE;
9209 cp_lexer_save_tokens (parser->lexer);
9210 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9211 if (c_dialect_objc ())
9212 /* In obj-c++, seeing '[[' might be the either the beginning of
9213 c++11 attributes, or a nested objc-message-expression. So
9214 let's parse the c++11 attributes tentatively. */
9215 cp_parser_parse_tentatively (parser);
9216 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9217 if (c_dialect_objc ())
9219 if (!cp_parser_parse_definitely (parser))
9220 std_attrs = NULL_TREE;
9223 /* Peek at the next token. */
9224 token = cp_lexer_peek_token (parser->lexer);
9225 /* Remember the location of the first token in the statement. */
9226 statement_location = token->location;
9227 /* If this is a keyword, then that will often determine what kind of
9228 statement we have. */
9229 if (token->type == CPP_KEYWORD)
9231 enum rid keyword = token->keyword;
9233 switch (keyword)
9235 case RID_CASE:
9236 case RID_DEFAULT:
9237 /* Looks like a labeled-statement with a case label.
9238 Parse the label, and then use tail recursion to parse
9239 the statement. */
9240 cp_parser_label_for_labeled_statement (parser, std_attrs);
9241 goto restart;
9243 case RID_IF:
9244 case RID_SWITCH:
9245 statement = cp_parser_selection_statement (parser, if_p);
9246 break;
9248 case RID_WHILE:
9249 case RID_DO:
9250 case RID_FOR:
9251 statement = cp_parser_iteration_statement (parser, false);
9252 break;
9254 case RID_BREAK:
9255 case RID_CONTINUE:
9256 case RID_RETURN:
9257 case RID_GOTO:
9258 statement = cp_parser_jump_statement (parser);
9259 break;
9261 /* Objective-C++ exception-handling constructs. */
9262 case RID_AT_TRY:
9263 case RID_AT_CATCH:
9264 case RID_AT_FINALLY:
9265 case RID_AT_SYNCHRONIZED:
9266 case RID_AT_THROW:
9267 statement = cp_parser_objc_statement (parser);
9268 break;
9270 case RID_TRY:
9271 statement = cp_parser_try_block (parser);
9272 break;
9274 case RID_NAMESPACE:
9275 /* This must be a namespace alias definition. */
9276 cp_parser_declaration_statement (parser);
9277 return;
9279 case RID_TRANSACTION_ATOMIC:
9280 case RID_TRANSACTION_RELAXED:
9281 statement = cp_parser_transaction (parser, keyword);
9282 break;
9283 case RID_TRANSACTION_CANCEL:
9284 statement = cp_parser_transaction_cancel (parser);
9285 break;
9287 default:
9288 /* It might be a keyword like `int' that can start a
9289 declaration-statement. */
9290 break;
9293 else if (token->type == CPP_NAME)
9295 /* If the next token is a `:', then we are looking at a
9296 labeled-statement. */
9297 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9298 if (token->type == CPP_COLON)
9300 /* Looks like a labeled-statement with an ordinary label.
9301 Parse the label, and then use tail recursion to parse
9302 the statement. */
9304 cp_parser_label_for_labeled_statement (parser, std_attrs);
9305 goto restart;
9308 /* Anything that starts with a `{' must be a compound-statement. */
9309 else if (token->type == CPP_OPEN_BRACE)
9310 statement = cp_parser_compound_statement (parser, NULL, false, false);
9311 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9312 a statement all its own. */
9313 else if (token->type == CPP_PRAGMA)
9315 /* Only certain OpenMP pragmas are attached to statements, and thus
9316 are considered statements themselves. All others are not. In
9317 the context of a compound, accept the pragma as a "statement" and
9318 return so that we can check for a close brace. Otherwise we
9319 require a real statement and must go back and read one. */
9320 if (in_compound)
9321 cp_parser_pragma (parser, pragma_compound);
9322 else if (!cp_parser_pragma (parser, pragma_stmt))
9323 goto restart;
9324 return;
9326 else if (token->type == CPP_EOF)
9328 cp_parser_error (parser, "expected statement");
9329 return;
9332 /* Everything else must be a declaration-statement or an
9333 expression-statement. Try for the declaration-statement
9334 first, unless we are looking at a `;', in which case we know that
9335 we have an expression-statement. */
9336 if (!statement)
9338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9340 if (std_attrs != NULL_TREE)
9342 /* Attributes should be parsed as part of the the
9343 declaration, so let's un-parse them. */
9344 cp_lexer_rollback_tokens (parser->lexer);
9345 std_attrs = NULL_TREE;
9348 cp_parser_parse_tentatively (parser);
9349 /* Try to parse the declaration-statement. */
9350 cp_parser_declaration_statement (parser);
9351 /* If that worked, we're done. */
9352 if (cp_parser_parse_definitely (parser))
9353 return;
9355 /* Look for an expression-statement instead. */
9356 statement = cp_parser_expression_statement (parser, in_statement_expr);
9359 /* Set the line number for the statement. */
9360 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9361 SET_EXPR_LOCATION (statement, statement_location);
9363 /* Note that for now, we don't do anything with c++11 statements
9364 parsed at this level. */
9365 if (std_attrs != NULL_TREE)
9366 warning_at (attrs_location,
9367 OPT_Wattributes,
9368 "attributes at the beginning of statement are ignored");
9371 /* Parse the label for a labeled-statement, i.e.
9373 identifier :
9374 case constant-expression :
9375 default :
9377 GNU Extension:
9378 case constant-expression ... constant-expression : statement
9380 When a label is parsed without errors, the label is added to the
9381 parse tree by the finish_* functions, so this function doesn't
9382 have to return the label. */
9384 static void
9385 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9387 cp_token *token;
9388 tree label = NULL_TREE;
9389 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9391 /* The next token should be an identifier. */
9392 token = cp_lexer_peek_token (parser->lexer);
9393 if (token->type != CPP_NAME
9394 && token->type != CPP_KEYWORD)
9396 cp_parser_error (parser, "expected labeled-statement");
9397 return;
9400 parser->colon_corrects_to_scope_p = false;
9401 switch (token->keyword)
9403 case RID_CASE:
9405 tree expr, expr_hi;
9406 cp_token *ellipsis;
9408 /* Consume the `case' token. */
9409 cp_lexer_consume_token (parser->lexer);
9410 /* Parse the constant-expression. */
9411 expr = cp_parser_constant_expression (parser,
9412 /*allow_non_constant_p=*/false,
9413 NULL);
9415 ellipsis = cp_lexer_peek_token (parser->lexer);
9416 if (ellipsis->type == CPP_ELLIPSIS)
9418 /* Consume the `...' token. */
9419 cp_lexer_consume_token (parser->lexer);
9420 expr_hi =
9421 cp_parser_constant_expression (parser,
9422 /*allow_non_constant_p=*/false,
9423 NULL);
9424 /* We don't need to emit warnings here, as the common code
9425 will do this for us. */
9427 else
9428 expr_hi = NULL_TREE;
9430 if (parser->in_switch_statement_p)
9431 finish_case_label (token->location, expr, expr_hi);
9432 else
9433 error_at (token->location,
9434 "case label %qE not within a switch statement",
9435 expr);
9437 break;
9439 case RID_DEFAULT:
9440 /* Consume the `default' token. */
9441 cp_lexer_consume_token (parser->lexer);
9443 if (parser->in_switch_statement_p)
9444 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9445 else
9446 error_at (token->location, "case label not within a switch statement");
9447 break;
9449 default:
9450 /* Anything else must be an ordinary label. */
9451 label = finish_label_stmt (cp_parser_identifier (parser));
9452 break;
9455 /* Require the `:' token. */
9456 cp_parser_require (parser, CPP_COLON, RT_COLON);
9458 /* An ordinary label may optionally be followed by attributes.
9459 However, this is only permitted if the attributes are then
9460 followed by a semicolon. This is because, for backward
9461 compatibility, when parsing
9462 lab: __attribute__ ((unused)) int i;
9463 we want the attribute to attach to "i", not "lab". */
9464 if (label != NULL_TREE
9465 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9467 tree attrs;
9468 cp_parser_parse_tentatively (parser);
9469 attrs = cp_parser_gnu_attributes_opt (parser);
9470 if (attrs == NULL_TREE
9471 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9472 cp_parser_abort_tentative_parse (parser);
9473 else if (!cp_parser_parse_definitely (parser))
9475 else
9476 attributes = chainon (attributes, attrs);
9479 if (attributes != NULL_TREE)
9480 cplus_decl_attributes (&label, attributes, 0);
9482 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9485 /* Parse an expression-statement.
9487 expression-statement:
9488 expression [opt] ;
9490 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9491 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9492 indicates whether this expression-statement is part of an
9493 expression statement. */
9495 static tree
9496 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9498 tree statement = NULL_TREE;
9499 cp_token *token = cp_lexer_peek_token (parser->lexer);
9501 /* If the next token is a ';', then there is no expression
9502 statement. */
9503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9505 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9506 if (statement == error_mark_node
9507 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9509 cp_parser_skip_to_end_of_block_or_statement (parser);
9510 return error_mark_node;
9514 /* Give a helpful message for "A<T>::type t;" and the like. */
9515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9516 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9518 if (TREE_CODE (statement) == SCOPE_REF)
9519 error_at (token->location, "need %<typename%> before %qE because "
9520 "%qT is a dependent scope",
9521 statement, TREE_OPERAND (statement, 0));
9522 else if (is_overloaded_fn (statement)
9523 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9525 /* A::A a; */
9526 tree fn = get_first_fn (statement);
9527 error_at (token->location,
9528 "%<%T::%D%> names the constructor, not the type",
9529 DECL_CONTEXT (fn), DECL_NAME (fn));
9533 /* Consume the final `;'. */
9534 cp_parser_consume_semicolon_at_end_of_statement (parser);
9536 if (in_statement_expr
9537 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9538 /* This is the final expression statement of a statement
9539 expression. */
9540 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9541 else if (statement)
9542 statement = finish_expr_stmt (statement);
9544 return statement;
9547 /* Parse a compound-statement.
9549 compound-statement:
9550 { statement-seq [opt] }
9552 GNU extension:
9554 compound-statement:
9555 { label-declaration-seq [opt] statement-seq [opt] }
9557 label-declaration-seq:
9558 label-declaration
9559 label-declaration-seq label-declaration
9561 Returns a tree representing the statement. */
9563 static tree
9564 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9565 bool in_try, bool function_body)
9567 tree compound_stmt;
9569 /* Consume the `{'. */
9570 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9571 return error_mark_node;
9572 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9573 && !function_body)
9574 pedwarn (input_location, OPT_Wpedantic,
9575 "compound-statement in constexpr function");
9576 /* Begin the compound-statement. */
9577 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9578 /* If the next keyword is `__label__' we have a label declaration. */
9579 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9580 cp_parser_label_declaration (parser);
9581 /* Parse an (optional) statement-seq. */
9582 cp_parser_statement_seq_opt (parser, in_statement_expr);
9583 /* Finish the compound-statement. */
9584 finish_compound_stmt (compound_stmt);
9585 /* Consume the `}'. */
9586 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9588 return compound_stmt;
9591 /* Parse an (optional) statement-seq.
9593 statement-seq:
9594 statement
9595 statement-seq [opt] statement */
9597 static void
9598 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9600 /* Scan statements until there aren't any more. */
9601 while (true)
9603 cp_token *token = cp_lexer_peek_token (parser->lexer);
9605 /* If we are looking at a `}', then we have run out of
9606 statements; the same is true if we have reached the end
9607 of file, or have stumbled upon a stray '@end'. */
9608 if (token->type == CPP_CLOSE_BRACE
9609 || token->type == CPP_EOF
9610 || token->type == CPP_PRAGMA_EOL
9611 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9612 break;
9614 /* If we are in a compound statement and find 'else' then
9615 something went wrong. */
9616 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9618 if (parser->in_statement & IN_IF_STMT)
9619 break;
9620 else
9622 token = cp_lexer_consume_token (parser->lexer);
9623 error_at (token->location, "%<else%> without a previous %<if%>");
9627 /* Parse the statement. */
9628 cp_parser_statement (parser, in_statement_expr, true, NULL);
9632 /* Parse a selection-statement.
9634 selection-statement:
9635 if ( condition ) statement
9636 if ( condition ) statement else statement
9637 switch ( condition ) statement
9639 Returns the new IF_STMT or SWITCH_STMT.
9641 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9642 is a (possibly labeled) if statement which is not enclosed in
9643 braces and has an else clause. This is used to implement
9644 -Wparentheses. */
9646 static tree
9647 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9649 cp_token *token;
9650 enum rid keyword;
9652 if (if_p != NULL)
9653 *if_p = false;
9655 /* Peek at the next token. */
9656 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9658 /* See what kind of keyword it is. */
9659 keyword = token->keyword;
9660 switch (keyword)
9662 case RID_IF:
9663 case RID_SWITCH:
9665 tree statement;
9666 tree condition;
9668 /* Look for the `('. */
9669 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9671 cp_parser_skip_to_end_of_statement (parser);
9672 return error_mark_node;
9675 /* Begin the selection-statement. */
9676 if (keyword == RID_IF)
9677 statement = begin_if_stmt ();
9678 else
9679 statement = begin_switch_stmt ();
9681 /* Parse the condition. */
9682 condition = cp_parser_condition (parser);
9683 /* Look for the `)'. */
9684 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9685 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9686 /*consume_paren=*/true);
9688 if (keyword == RID_IF)
9690 bool nested_if;
9691 unsigned char in_statement;
9693 /* Add the condition. */
9694 finish_if_stmt_cond (condition, statement);
9696 /* Parse the then-clause. */
9697 in_statement = parser->in_statement;
9698 parser->in_statement |= IN_IF_STMT;
9699 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9701 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9702 add_stmt (build_empty_stmt (loc));
9703 cp_lexer_consume_token (parser->lexer);
9704 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9705 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9706 "empty body in an %<if%> statement");
9707 nested_if = false;
9709 else
9710 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9711 parser->in_statement = in_statement;
9713 finish_then_clause (statement);
9715 /* If the next token is `else', parse the else-clause. */
9716 if (cp_lexer_next_token_is_keyword (parser->lexer,
9717 RID_ELSE))
9719 /* Consume the `else' keyword. */
9720 cp_lexer_consume_token (parser->lexer);
9721 begin_else_clause (statement);
9722 /* Parse the else-clause. */
9723 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9725 location_t loc;
9726 loc = cp_lexer_peek_token (parser->lexer)->location;
9727 warning_at (loc,
9728 OPT_Wempty_body, "suggest braces around "
9729 "empty body in an %<else%> statement");
9730 add_stmt (build_empty_stmt (loc));
9731 cp_lexer_consume_token (parser->lexer);
9733 else
9734 cp_parser_implicitly_scoped_statement (parser, NULL);
9736 finish_else_clause (statement);
9738 /* If we are currently parsing a then-clause, then
9739 IF_P will not be NULL. We set it to true to
9740 indicate that this if statement has an else clause.
9741 This may trigger the Wparentheses warning below
9742 when we get back up to the parent if statement. */
9743 if (if_p != NULL)
9744 *if_p = true;
9746 else
9748 /* This if statement does not have an else clause. If
9749 NESTED_IF is true, then the then-clause is an if
9750 statement which does have an else clause. We warn
9751 about the potential ambiguity. */
9752 if (nested_if)
9753 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9754 "suggest explicit braces to avoid ambiguous"
9755 " %<else%>");
9758 /* Now we're all done with the if-statement. */
9759 finish_if_stmt (statement);
9761 else
9763 bool in_switch_statement_p;
9764 unsigned char in_statement;
9766 /* Add the condition. */
9767 finish_switch_cond (condition, statement);
9769 /* Parse the body of the switch-statement. */
9770 in_switch_statement_p = parser->in_switch_statement_p;
9771 in_statement = parser->in_statement;
9772 parser->in_switch_statement_p = true;
9773 parser->in_statement |= IN_SWITCH_STMT;
9774 cp_parser_implicitly_scoped_statement (parser, NULL);
9775 parser->in_switch_statement_p = in_switch_statement_p;
9776 parser->in_statement = in_statement;
9778 /* Now we're all done with the switch-statement. */
9779 finish_switch_stmt (statement);
9782 return statement;
9784 break;
9786 default:
9787 cp_parser_error (parser, "expected selection-statement");
9788 return error_mark_node;
9792 /* Parse a condition.
9794 condition:
9795 expression
9796 type-specifier-seq declarator = initializer-clause
9797 type-specifier-seq declarator braced-init-list
9799 GNU Extension:
9801 condition:
9802 type-specifier-seq declarator asm-specification [opt]
9803 attributes [opt] = assignment-expression
9805 Returns the expression that should be tested. */
9807 static tree
9808 cp_parser_condition (cp_parser* parser)
9810 cp_decl_specifier_seq type_specifiers;
9811 const char *saved_message;
9812 int declares_class_or_enum;
9814 /* Try the declaration first. */
9815 cp_parser_parse_tentatively (parser);
9816 /* New types are not allowed in the type-specifier-seq for a
9817 condition. */
9818 saved_message = parser->type_definition_forbidden_message;
9819 parser->type_definition_forbidden_message
9820 = G_("types may not be defined in conditions");
9821 /* Parse the type-specifier-seq. */
9822 cp_parser_decl_specifier_seq (parser,
9823 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9824 &type_specifiers,
9825 &declares_class_or_enum);
9826 /* Restore the saved message. */
9827 parser->type_definition_forbidden_message = saved_message;
9828 /* If all is well, we might be looking at a declaration. */
9829 if (!cp_parser_error_occurred (parser))
9831 tree decl;
9832 tree asm_specification;
9833 tree attributes;
9834 cp_declarator *declarator;
9835 tree initializer = NULL_TREE;
9837 /* Parse the declarator. */
9838 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9839 /*ctor_dtor_or_conv_p=*/NULL,
9840 /*parenthesized_p=*/NULL,
9841 /*member_p=*/false);
9842 /* Parse the attributes. */
9843 attributes = cp_parser_attributes_opt (parser);
9844 /* Parse the asm-specification. */
9845 asm_specification = cp_parser_asm_specification_opt (parser);
9846 /* If the next token is not an `=' or '{', then we might still be
9847 looking at an expression. For example:
9849 if (A(a).x)
9851 looks like a decl-specifier-seq and a declarator -- but then
9852 there is no `=', so this is an expression. */
9853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9854 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9855 cp_parser_simulate_error (parser);
9857 /* If we did see an `=' or '{', then we are looking at a declaration
9858 for sure. */
9859 if (cp_parser_parse_definitely (parser))
9861 tree pushed_scope;
9862 bool non_constant_p;
9863 bool flags = LOOKUP_ONLYCONVERTING;
9865 /* Create the declaration. */
9866 decl = start_decl (declarator, &type_specifiers,
9867 /*initialized_p=*/true,
9868 attributes, /*prefix_attributes=*/NULL_TREE,
9869 &pushed_scope);
9871 /* Parse the initializer. */
9872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9874 initializer = cp_parser_braced_list (parser, &non_constant_p);
9875 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9876 flags = 0;
9878 else
9880 /* Consume the `='. */
9881 cp_parser_require (parser, CPP_EQ, RT_EQ);
9882 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9884 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9885 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9887 /* Process the initializer. */
9888 cp_finish_decl (decl,
9889 initializer, !non_constant_p,
9890 asm_specification,
9891 flags);
9893 if (pushed_scope)
9894 pop_scope (pushed_scope);
9896 return convert_from_reference (decl);
9899 /* If we didn't even get past the declarator successfully, we are
9900 definitely not looking at a declaration. */
9901 else
9902 cp_parser_abort_tentative_parse (parser);
9904 /* Otherwise, we are looking at an expression. */
9905 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9908 /* Parses a for-statement or range-for-statement until the closing ')',
9909 not included. */
9911 static tree
9912 cp_parser_for (cp_parser *parser, bool ivdep)
9914 tree init, scope, decl;
9915 bool is_range_for;
9917 /* Begin the for-statement. */
9918 scope = begin_for_scope (&init);
9920 /* Parse the initialization. */
9921 is_range_for = cp_parser_for_init_statement (parser, &decl);
9923 if (is_range_for)
9924 return cp_parser_range_for (parser, scope, init, decl, ivdep);
9925 else
9926 return cp_parser_c_for (parser, scope, init, ivdep);
9929 static tree
9930 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
9932 /* Normal for loop */
9933 tree condition = NULL_TREE;
9934 tree expression = NULL_TREE;
9935 tree stmt;
9937 stmt = begin_for_stmt (scope, init);
9938 /* The for-init-statement has already been parsed in
9939 cp_parser_for_init_statement, so no work is needed here. */
9940 finish_for_init_stmt (stmt);
9942 /* If there's a condition, process it. */
9943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9944 condition = cp_parser_condition (parser);
9945 else if (ivdep)
9947 cp_parser_error (parser, "missing loop condition in loop with "
9948 "%<GCC ivdep%> pragma");
9949 condition = error_mark_node;
9951 finish_for_cond (condition, stmt, ivdep);
9952 /* Look for the `;'. */
9953 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9955 /* If there's an expression, process it. */
9956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9957 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9958 finish_for_expr (expression, stmt);
9960 return stmt;
9963 /* Tries to parse a range-based for-statement:
9965 range-based-for:
9966 decl-specifier-seq declarator : expression
9968 The decl-specifier-seq declarator and the `:' are already parsed by
9969 cp_parser_for_init_statement. If processing_template_decl it returns a
9970 newly created RANGE_FOR_STMT; if not, it is converted to a
9971 regular FOR_STMT. */
9973 static tree
9974 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
9975 bool ivdep)
9977 tree stmt, range_expr;
9979 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9981 bool expr_non_constant_p;
9982 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9984 else
9985 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9987 /* If in template, STMT is converted to a normal for-statement
9988 at instantiation. If not, it is done just ahead. */
9989 if (processing_template_decl)
9991 if (check_for_bare_parameter_packs (range_expr))
9992 range_expr = error_mark_node;
9993 stmt = begin_range_for_stmt (scope, init);
9994 if (ivdep)
9995 RANGE_FOR_IVDEP (stmt) = 1;
9996 finish_range_for_decl (stmt, range_decl, range_expr);
9997 if (!type_dependent_expression_p (range_expr)
9998 /* do_auto_deduction doesn't mess with template init-lists. */
9999 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10000 do_range_for_auto_deduction (range_decl, range_expr);
10002 else
10004 stmt = begin_for_stmt (scope, init);
10005 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10007 return stmt;
10010 /* Subroutine of cp_convert_range_for: given the initializer expression,
10011 builds up the range temporary. */
10013 static tree
10014 build_range_temp (tree range_expr)
10016 tree range_type, range_temp;
10018 /* Find out the type deduced by the declaration
10019 `auto &&__range = range_expr'. */
10020 range_type = cp_build_reference_type (make_auto (), true);
10021 range_type = do_auto_deduction (range_type, range_expr,
10022 type_uses_auto (range_type));
10024 /* Create the __range variable. */
10025 range_temp = build_decl (input_location, VAR_DECL,
10026 get_identifier ("__for_range"), range_type);
10027 TREE_USED (range_temp) = 1;
10028 DECL_ARTIFICIAL (range_temp) = 1;
10030 return range_temp;
10033 /* Used by cp_parser_range_for in template context: we aren't going to
10034 do a full conversion yet, but we still need to resolve auto in the
10035 type of the for-range-declaration if present. This is basically
10036 a shortcut version of cp_convert_range_for. */
10038 static void
10039 do_range_for_auto_deduction (tree decl, tree range_expr)
10041 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10042 if (auto_node)
10044 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10045 range_temp = convert_from_reference (build_range_temp (range_expr));
10046 iter_type = (cp_parser_perform_range_for_lookup
10047 (range_temp, &begin_dummy, &end_dummy));
10048 if (iter_type)
10050 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10051 iter_type);
10052 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10053 tf_warning_or_error);
10054 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10055 iter_decl, auto_node);
10060 /* Converts a range-based for-statement into a normal
10061 for-statement, as per the definition.
10063 for (RANGE_DECL : RANGE_EXPR)
10064 BLOCK
10066 should be equivalent to:
10069 auto &&__range = RANGE_EXPR;
10070 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10071 __begin != __end;
10072 ++__begin)
10074 RANGE_DECL = *__begin;
10075 BLOCK
10079 If RANGE_EXPR is an array:
10080 BEGIN_EXPR = __range
10081 END_EXPR = __range + ARRAY_SIZE(__range)
10082 Else if RANGE_EXPR has a member 'begin' or 'end':
10083 BEGIN_EXPR = __range.begin()
10084 END_EXPR = __range.end()
10085 Else:
10086 BEGIN_EXPR = begin(__range)
10087 END_EXPR = end(__range);
10089 If __range has a member 'begin' but not 'end', or vice versa, we must
10090 still use the second alternative (it will surely fail, however).
10091 When calling begin()/end() in the third alternative we must use
10092 argument dependent lookup, but always considering 'std' as an associated
10093 namespace. */
10095 tree
10096 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10097 bool ivdep)
10099 tree begin, end;
10100 tree iter_type, begin_expr, end_expr;
10101 tree condition, expression;
10103 if (range_decl == error_mark_node || range_expr == error_mark_node)
10104 /* If an error happened previously do nothing or else a lot of
10105 unhelpful errors would be issued. */
10106 begin_expr = end_expr = iter_type = error_mark_node;
10107 else
10109 tree range_temp;
10111 if (TREE_CODE (range_expr) == VAR_DECL
10112 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10113 /* Can't bind a reference to an array of runtime bound. */
10114 range_temp = range_expr;
10115 else
10117 range_temp = build_range_temp (range_expr);
10118 pushdecl (range_temp);
10119 cp_finish_decl (range_temp, range_expr,
10120 /*is_constant_init*/false, NULL_TREE,
10121 LOOKUP_ONLYCONVERTING);
10122 range_temp = convert_from_reference (range_temp);
10124 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10125 &begin_expr, &end_expr);
10128 /* The new for initialization statement. */
10129 begin = build_decl (input_location, VAR_DECL,
10130 get_identifier ("__for_begin"), iter_type);
10131 TREE_USED (begin) = 1;
10132 DECL_ARTIFICIAL (begin) = 1;
10133 pushdecl (begin);
10134 cp_finish_decl (begin, begin_expr,
10135 /*is_constant_init*/false, NULL_TREE,
10136 LOOKUP_ONLYCONVERTING);
10138 end = build_decl (input_location, VAR_DECL,
10139 get_identifier ("__for_end"), iter_type);
10140 TREE_USED (end) = 1;
10141 DECL_ARTIFICIAL (end) = 1;
10142 pushdecl (end);
10143 cp_finish_decl (end, end_expr,
10144 /*is_constant_init*/false, NULL_TREE,
10145 LOOKUP_ONLYCONVERTING);
10147 finish_for_init_stmt (statement);
10149 /* The new for condition. */
10150 condition = build_x_binary_op (input_location, NE_EXPR,
10151 begin, ERROR_MARK,
10152 end, ERROR_MARK,
10153 NULL, tf_warning_or_error);
10154 finish_for_cond (condition, statement, ivdep);
10156 /* The new increment expression. */
10157 expression = finish_unary_op_expr (input_location,
10158 PREINCREMENT_EXPR, begin,
10159 tf_warning_or_error);
10160 finish_for_expr (expression, statement);
10162 /* The declaration is initialized with *__begin inside the loop body. */
10163 cp_finish_decl (range_decl,
10164 build_x_indirect_ref (input_location, begin, RO_NULL,
10165 tf_warning_or_error),
10166 /*is_constant_init*/false, NULL_TREE,
10167 LOOKUP_ONLYCONVERTING);
10169 return statement;
10172 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10173 We need to solve both at the same time because the method used
10174 depends on the existence of members begin or end.
10175 Returns the type deduced for the iterator expression. */
10177 static tree
10178 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10180 if (error_operand_p (range))
10182 *begin = *end = error_mark_node;
10183 return error_mark_node;
10186 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10188 error ("range-based %<for%> expression of type %qT "
10189 "has incomplete type", TREE_TYPE (range));
10190 *begin = *end = error_mark_node;
10191 return error_mark_node;
10193 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10195 /* If RANGE is an array, we will use pointer arithmetic. */
10196 *begin = range;
10197 *end = build_binary_op (input_location, PLUS_EXPR,
10198 range,
10199 array_type_nelts_top (TREE_TYPE (range)),
10201 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10203 else
10205 /* If it is not an array, we must do a bit of magic. */
10206 tree id_begin, id_end;
10207 tree member_begin, member_end;
10209 *begin = *end = error_mark_node;
10211 id_begin = get_identifier ("begin");
10212 id_end = get_identifier ("end");
10213 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10214 /*protect=*/2, /*want_type=*/false,
10215 tf_warning_or_error);
10216 member_end = lookup_member (TREE_TYPE (range), id_end,
10217 /*protect=*/2, /*want_type=*/false,
10218 tf_warning_or_error);
10220 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10222 /* Use the member functions. */
10223 if (member_begin != NULL_TREE)
10224 *begin = cp_parser_range_for_member_function (range, id_begin);
10225 else
10226 error ("range-based %<for%> expression of type %qT has an "
10227 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10229 if (member_end != NULL_TREE)
10230 *end = cp_parser_range_for_member_function (range, id_end);
10231 else
10232 error ("range-based %<for%> expression of type %qT has a "
10233 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10235 else
10237 /* Use global functions with ADL. */
10238 vec<tree, va_gc> *vec;
10239 vec = make_tree_vector ();
10241 vec_safe_push (vec, range);
10243 member_begin = perform_koenig_lookup (id_begin, vec,
10244 /*include_std=*/true,
10245 tf_warning_or_error);
10246 *begin = finish_call_expr (member_begin, &vec, false, true,
10247 tf_warning_or_error);
10248 member_end = perform_koenig_lookup (id_end, vec,
10249 /*include_std=*/true,
10250 tf_warning_or_error);
10251 *end = finish_call_expr (member_end, &vec, false, true,
10252 tf_warning_or_error);
10254 release_tree_vector (vec);
10257 /* Last common checks. */
10258 if (*begin == error_mark_node || *end == error_mark_node)
10260 /* If one of the expressions is an error do no more checks. */
10261 *begin = *end = error_mark_node;
10262 return error_mark_node;
10264 else if (type_dependent_expression_p (*begin)
10265 || type_dependent_expression_p (*end))
10266 /* Can happen, when, eg, in a template context, Koenig lookup
10267 can't resolve begin/end (c++/58503). */
10268 return NULL_TREE;
10269 else
10271 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10272 /* The unqualified type of the __begin and __end temporaries should
10273 be the same, as required by the multiple auto declaration. */
10274 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10275 error ("inconsistent begin/end types in range-based %<for%> "
10276 "statement: %qT and %qT",
10277 TREE_TYPE (*begin), TREE_TYPE (*end));
10278 return iter_type;
10283 /* Helper function for cp_parser_perform_range_for_lookup.
10284 Builds a tree for RANGE.IDENTIFIER(). */
10286 static tree
10287 cp_parser_range_for_member_function (tree range, tree identifier)
10289 tree member, res;
10290 vec<tree, va_gc> *vec;
10292 member = finish_class_member_access_expr (range, identifier,
10293 false, tf_warning_or_error);
10294 if (member == error_mark_node)
10295 return error_mark_node;
10297 vec = make_tree_vector ();
10298 res = finish_call_expr (member, &vec,
10299 /*disallow_virtual=*/false,
10300 /*koenig_p=*/false,
10301 tf_warning_or_error);
10302 release_tree_vector (vec);
10303 return res;
10306 /* Parse an iteration-statement.
10308 iteration-statement:
10309 while ( condition ) statement
10310 do statement while ( expression ) ;
10311 for ( for-init-statement condition [opt] ; expression [opt] )
10312 statement
10314 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10316 static tree
10317 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10319 cp_token *token;
10320 enum rid keyword;
10321 tree statement;
10322 unsigned char in_statement;
10324 /* Peek at the next token. */
10325 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10326 if (!token)
10327 return error_mark_node;
10329 /* Remember whether or not we are already within an iteration
10330 statement. */
10331 in_statement = parser->in_statement;
10333 /* See what kind of keyword it is. */
10334 keyword = token->keyword;
10335 switch (keyword)
10337 case RID_WHILE:
10339 tree condition;
10341 /* Begin the while-statement. */
10342 statement = begin_while_stmt ();
10343 /* Look for the `('. */
10344 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10345 /* Parse the condition. */
10346 condition = cp_parser_condition (parser);
10347 finish_while_stmt_cond (condition, statement, ivdep);
10348 /* Look for the `)'. */
10349 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10350 /* Parse the dependent statement. */
10351 parser->in_statement = IN_ITERATION_STMT;
10352 cp_parser_already_scoped_statement (parser);
10353 parser->in_statement = in_statement;
10354 /* We're done with the while-statement. */
10355 finish_while_stmt (statement);
10357 break;
10359 case RID_DO:
10361 tree expression;
10363 /* Begin the do-statement. */
10364 statement = begin_do_stmt ();
10365 /* Parse the body of the do-statement. */
10366 parser->in_statement = IN_ITERATION_STMT;
10367 cp_parser_implicitly_scoped_statement (parser, NULL);
10368 parser->in_statement = in_statement;
10369 finish_do_body (statement);
10370 /* Look for the `while' keyword. */
10371 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10372 /* Look for the `('. */
10373 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10374 /* Parse the expression. */
10375 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10376 /* We're done with the do-statement. */
10377 finish_do_stmt (expression, statement, ivdep);
10378 /* Look for the `)'. */
10379 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10380 /* Look for the `;'. */
10381 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10383 break;
10385 case RID_FOR:
10387 /* Look for the `('. */
10388 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10390 statement = cp_parser_for (parser, ivdep);
10392 /* Look for the `)'. */
10393 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10395 /* Parse the body of the for-statement. */
10396 parser->in_statement = IN_ITERATION_STMT;
10397 cp_parser_already_scoped_statement (parser);
10398 parser->in_statement = in_statement;
10400 /* We're done with the for-statement. */
10401 finish_for_stmt (statement);
10403 break;
10405 default:
10406 cp_parser_error (parser, "expected iteration-statement");
10407 statement = error_mark_node;
10408 break;
10411 return statement;
10414 /* Parse a for-init-statement or the declarator of a range-based-for.
10415 Returns true if a range-based-for declaration is seen.
10417 for-init-statement:
10418 expression-statement
10419 simple-declaration */
10421 static bool
10422 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10424 /* If the next token is a `;', then we have an empty
10425 expression-statement. Grammatically, this is also a
10426 simple-declaration, but an invalid one, because it does not
10427 declare anything. Therefore, if we did not handle this case
10428 specially, we would issue an error message about an invalid
10429 declaration. */
10430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10432 bool is_range_for = false;
10433 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10435 parser->colon_corrects_to_scope_p = false;
10437 /* We're going to speculatively look for a declaration, falling back
10438 to an expression, if necessary. */
10439 cp_parser_parse_tentatively (parser);
10440 /* Parse the declaration. */
10441 cp_parser_simple_declaration (parser,
10442 /*function_definition_allowed_p=*/false,
10443 decl);
10444 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10445 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10447 /* It is a range-for, consume the ':' */
10448 cp_lexer_consume_token (parser->lexer);
10449 is_range_for = true;
10450 if (cxx_dialect < cxx11)
10452 error_at (cp_lexer_peek_token (parser->lexer)->location,
10453 "range-based %<for%> loops are not allowed "
10454 "in C++98 mode");
10455 *decl = error_mark_node;
10458 else
10459 /* The ';' is not consumed yet because we told
10460 cp_parser_simple_declaration not to. */
10461 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10463 if (cp_parser_parse_definitely (parser))
10464 return is_range_for;
10465 /* If the tentative parse failed, then we shall need to look for an
10466 expression-statement. */
10468 /* If we are here, it is an expression-statement. */
10469 cp_parser_expression_statement (parser, NULL_TREE);
10470 return false;
10473 /* Parse a jump-statement.
10475 jump-statement:
10476 break ;
10477 continue ;
10478 return expression [opt] ;
10479 return braced-init-list ;
10480 goto identifier ;
10482 GNU extension:
10484 jump-statement:
10485 goto * expression ;
10487 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10489 static tree
10490 cp_parser_jump_statement (cp_parser* parser)
10492 tree statement = error_mark_node;
10493 cp_token *token;
10494 enum rid keyword;
10495 unsigned char in_statement;
10497 /* Peek at the next token. */
10498 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10499 if (!token)
10500 return error_mark_node;
10502 /* See what kind of keyword it is. */
10503 keyword = token->keyword;
10504 switch (keyword)
10506 case RID_BREAK:
10507 in_statement = parser->in_statement & ~IN_IF_STMT;
10508 switch (in_statement)
10510 case 0:
10511 error_at (token->location, "break statement not within loop or switch");
10512 break;
10513 default:
10514 gcc_assert ((in_statement & IN_SWITCH_STMT)
10515 || in_statement == IN_ITERATION_STMT);
10516 statement = finish_break_stmt ();
10517 break;
10518 case IN_OMP_BLOCK:
10519 error_at (token->location, "invalid exit from OpenMP structured block");
10520 break;
10521 case IN_OMP_FOR:
10522 error_at (token->location, "break statement used with OpenMP for loop");
10523 break;
10525 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10526 break;
10528 case RID_CONTINUE:
10529 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10531 case 0:
10532 error_at (token->location, "continue statement not within a loop");
10533 break;
10534 case IN_ITERATION_STMT:
10535 case IN_OMP_FOR:
10536 statement = finish_continue_stmt ();
10537 break;
10538 case IN_OMP_BLOCK:
10539 error_at (token->location, "invalid exit from OpenMP structured block");
10540 break;
10541 default:
10542 gcc_unreachable ();
10544 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10545 break;
10547 case RID_RETURN:
10549 tree expr;
10550 bool expr_non_constant_p;
10552 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10554 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10555 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10557 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10558 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10559 else
10560 /* If the next token is a `;', then there is no
10561 expression. */
10562 expr = NULL_TREE;
10563 /* Build the return-statement. */
10564 statement = finish_return_stmt (expr);
10565 /* Look for the final `;'. */
10566 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10568 break;
10570 case RID_GOTO:
10571 /* Create the goto-statement. */
10572 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10574 /* Issue a warning about this use of a GNU extension. */
10575 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10576 /* Consume the '*' token. */
10577 cp_lexer_consume_token (parser->lexer);
10578 /* Parse the dependent expression. */
10579 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10581 else
10582 finish_goto_stmt (cp_parser_identifier (parser));
10583 /* Look for the final `;'. */
10584 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10585 break;
10587 default:
10588 cp_parser_error (parser, "expected jump-statement");
10589 break;
10592 return statement;
10595 /* Parse a declaration-statement.
10597 declaration-statement:
10598 block-declaration */
10600 static void
10601 cp_parser_declaration_statement (cp_parser* parser)
10603 void *p;
10605 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10606 p = obstack_alloc (&declarator_obstack, 0);
10608 /* Parse the block-declaration. */
10609 cp_parser_block_declaration (parser, /*statement_p=*/true);
10611 /* Free any declarators allocated. */
10612 obstack_free (&declarator_obstack, p);
10615 /* Some dependent statements (like `if (cond) statement'), are
10616 implicitly in their own scope. In other words, if the statement is
10617 a single statement (as opposed to a compound-statement), it is
10618 none-the-less treated as if it were enclosed in braces. Any
10619 declarations appearing in the dependent statement are out of scope
10620 after control passes that point. This function parses a statement,
10621 but ensures that is in its own scope, even if it is not a
10622 compound-statement.
10624 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10625 is a (possibly labeled) if statement which is not enclosed in
10626 braces and has an else clause. This is used to implement
10627 -Wparentheses.
10629 Returns the new statement. */
10631 static tree
10632 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10634 tree statement;
10636 if (if_p != NULL)
10637 *if_p = false;
10639 /* Mark if () ; with a special NOP_EXPR. */
10640 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10642 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10643 cp_lexer_consume_token (parser->lexer);
10644 statement = add_stmt (build_empty_stmt (loc));
10646 /* if a compound is opened, we simply parse the statement directly. */
10647 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10648 statement = cp_parser_compound_statement (parser, NULL, false, false);
10649 /* If the token is not a `{', then we must take special action. */
10650 else
10652 /* Create a compound-statement. */
10653 statement = begin_compound_stmt (0);
10654 /* Parse the dependent-statement. */
10655 cp_parser_statement (parser, NULL_TREE, false, if_p);
10656 /* Finish the dummy compound-statement. */
10657 finish_compound_stmt (statement);
10660 /* Return the statement. */
10661 return statement;
10664 /* For some dependent statements (like `while (cond) statement'), we
10665 have already created a scope. Therefore, even if the dependent
10666 statement is a compound-statement, we do not want to create another
10667 scope. */
10669 static void
10670 cp_parser_already_scoped_statement (cp_parser* parser)
10672 /* If the token is a `{', then we must take special action. */
10673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10674 cp_parser_statement (parser, NULL_TREE, false, NULL);
10675 else
10677 /* Avoid calling cp_parser_compound_statement, so that we
10678 don't create a new scope. Do everything else by hand. */
10679 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10680 /* If the next keyword is `__label__' we have a label declaration. */
10681 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10682 cp_parser_label_declaration (parser);
10683 /* Parse an (optional) statement-seq. */
10684 cp_parser_statement_seq_opt (parser, NULL_TREE);
10685 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10689 /* Declarations [gram.dcl.dcl] */
10691 /* Parse an optional declaration-sequence.
10693 declaration-seq:
10694 declaration
10695 declaration-seq declaration */
10697 static void
10698 cp_parser_declaration_seq_opt (cp_parser* parser)
10700 while (true)
10702 cp_token *token;
10704 token = cp_lexer_peek_token (parser->lexer);
10706 if (token->type == CPP_CLOSE_BRACE
10707 || token->type == CPP_EOF
10708 || token->type == CPP_PRAGMA_EOL)
10709 break;
10711 if (token->type == CPP_SEMICOLON)
10713 /* A declaration consisting of a single semicolon is
10714 invalid. Allow it unless we're being pedantic. */
10715 cp_lexer_consume_token (parser->lexer);
10716 if (!in_system_header)
10717 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10718 continue;
10721 /* If we're entering or exiting a region that's implicitly
10722 extern "C", modify the lang context appropriately. */
10723 if (!parser->implicit_extern_c && token->implicit_extern_c)
10725 push_lang_context (lang_name_c);
10726 parser->implicit_extern_c = true;
10728 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10730 pop_lang_context ();
10731 parser->implicit_extern_c = false;
10734 if (token->type == CPP_PRAGMA)
10736 /* A top-level declaration can consist solely of a #pragma.
10737 A nested declaration cannot, so this is done here and not
10738 in cp_parser_declaration. (A #pragma at block scope is
10739 handled in cp_parser_statement.) */
10740 cp_parser_pragma (parser, pragma_external);
10741 continue;
10744 /* Parse the declaration itself. */
10745 cp_parser_declaration (parser);
10749 /* Parse a declaration.
10751 declaration:
10752 block-declaration
10753 function-definition
10754 template-declaration
10755 explicit-instantiation
10756 explicit-specialization
10757 linkage-specification
10758 namespace-definition
10760 GNU extension:
10762 declaration:
10763 __extension__ declaration */
10765 static void
10766 cp_parser_declaration (cp_parser* parser)
10768 cp_token token1;
10769 cp_token token2;
10770 int saved_pedantic;
10771 void *p;
10772 tree attributes = NULL_TREE;
10774 /* Check for the `__extension__' keyword. */
10775 if (cp_parser_extension_opt (parser, &saved_pedantic))
10777 /* Parse the qualified declaration. */
10778 cp_parser_declaration (parser);
10779 /* Restore the PEDANTIC flag. */
10780 pedantic = saved_pedantic;
10782 return;
10785 /* Try to figure out what kind of declaration is present. */
10786 token1 = *cp_lexer_peek_token (parser->lexer);
10788 if (token1.type != CPP_EOF)
10789 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10790 else
10792 token2.type = CPP_EOF;
10793 token2.keyword = RID_MAX;
10796 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10797 p = obstack_alloc (&declarator_obstack, 0);
10799 /* If the next token is `extern' and the following token is a string
10800 literal, then we have a linkage specification. */
10801 if (token1.keyword == RID_EXTERN
10802 && cp_parser_is_pure_string_literal (&token2))
10803 cp_parser_linkage_specification (parser);
10804 /* If the next token is `template', then we have either a template
10805 declaration, an explicit instantiation, or an explicit
10806 specialization. */
10807 else if (token1.keyword == RID_TEMPLATE)
10809 /* `template <>' indicates a template specialization. */
10810 if (token2.type == CPP_LESS
10811 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10812 cp_parser_explicit_specialization (parser);
10813 /* `template <' indicates a template declaration. */
10814 else if (token2.type == CPP_LESS)
10815 cp_parser_template_declaration (parser, /*member_p=*/false);
10816 /* Anything else must be an explicit instantiation. */
10817 else
10818 cp_parser_explicit_instantiation (parser);
10820 /* If the next token is `export', then we have a template
10821 declaration. */
10822 else if (token1.keyword == RID_EXPORT)
10823 cp_parser_template_declaration (parser, /*member_p=*/false);
10824 /* If the next token is `extern', 'static' or 'inline' and the one
10825 after that is `template', we have a GNU extended explicit
10826 instantiation directive. */
10827 else if (cp_parser_allow_gnu_extensions_p (parser)
10828 && (token1.keyword == RID_EXTERN
10829 || token1.keyword == RID_STATIC
10830 || token1.keyword == RID_INLINE)
10831 && token2.keyword == RID_TEMPLATE)
10832 cp_parser_explicit_instantiation (parser);
10833 /* If the next token is `namespace', check for a named or unnamed
10834 namespace definition. */
10835 else if (token1.keyword == RID_NAMESPACE
10836 && (/* A named namespace definition. */
10837 (token2.type == CPP_NAME
10838 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10839 != CPP_EQ))
10840 /* An unnamed namespace definition. */
10841 || token2.type == CPP_OPEN_BRACE
10842 || token2.keyword == RID_ATTRIBUTE))
10843 cp_parser_namespace_definition (parser);
10844 /* An inline (associated) namespace definition. */
10845 else if (token1.keyword == RID_INLINE
10846 && token2.keyword == RID_NAMESPACE)
10847 cp_parser_namespace_definition (parser);
10848 /* Objective-C++ declaration/definition. */
10849 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10850 cp_parser_objc_declaration (parser, NULL_TREE);
10851 else if (c_dialect_objc ()
10852 && token1.keyword == RID_ATTRIBUTE
10853 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10854 cp_parser_objc_declaration (parser, attributes);
10855 /* We must have either a block declaration or a function
10856 definition. */
10857 else
10858 /* Try to parse a block-declaration, or a function-definition. */
10859 cp_parser_block_declaration (parser, /*statement_p=*/false);
10861 /* Free any declarators allocated. */
10862 obstack_free (&declarator_obstack, p);
10865 /* Parse a block-declaration.
10867 block-declaration:
10868 simple-declaration
10869 asm-definition
10870 namespace-alias-definition
10871 using-declaration
10872 using-directive
10874 GNU Extension:
10876 block-declaration:
10877 __extension__ block-declaration
10879 C++0x Extension:
10881 block-declaration:
10882 static_assert-declaration
10884 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10885 part of a declaration-statement. */
10887 static void
10888 cp_parser_block_declaration (cp_parser *parser,
10889 bool statement_p)
10891 cp_token *token1;
10892 int saved_pedantic;
10894 /* Check for the `__extension__' keyword. */
10895 if (cp_parser_extension_opt (parser, &saved_pedantic))
10897 /* Parse the qualified declaration. */
10898 cp_parser_block_declaration (parser, statement_p);
10899 /* Restore the PEDANTIC flag. */
10900 pedantic = saved_pedantic;
10902 return;
10905 /* Peek at the next token to figure out which kind of declaration is
10906 present. */
10907 token1 = cp_lexer_peek_token (parser->lexer);
10909 /* If the next keyword is `asm', we have an asm-definition. */
10910 if (token1->keyword == RID_ASM)
10912 if (statement_p)
10913 cp_parser_commit_to_tentative_parse (parser);
10914 cp_parser_asm_definition (parser);
10916 /* If the next keyword is `namespace', we have a
10917 namespace-alias-definition. */
10918 else if (token1->keyword == RID_NAMESPACE)
10919 cp_parser_namespace_alias_definition (parser);
10920 /* If the next keyword is `using', we have a
10921 using-declaration, a using-directive, or an alias-declaration. */
10922 else if (token1->keyword == RID_USING)
10924 cp_token *token2;
10926 if (statement_p)
10927 cp_parser_commit_to_tentative_parse (parser);
10928 /* If the token after `using' is `namespace', then we have a
10929 using-directive. */
10930 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10931 if (token2->keyword == RID_NAMESPACE)
10932 cp_parser_using_directive (parser);
10933 /* If the second token after 'using' is '=', then we have an
10934 alias-declaration. */
10935 else if (cxx_dialect >= cxx11
10936 && token2->type == CPP_NAME
10937 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10938 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10939 cp_parser_alias_declaration (parser);
10940 /* Otherwise, it's a using-declaration. */
10941 else
10942 cp_parser_using_declaration (parser,
10943 /*access_declaration_p=*/false);
10945 /* If the next keyword is `__label__' we have a misplaced label
10946 declaration. */
10947 else if (token1->keyword == RID_LABEL)
10949 cp_lexer_consume_token (parser->lexer);
10950 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10951 cp_parser_skip_to_end_of_statement (parser);
10952 /* If the next token is now a `;', consume it. */
10953 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10954 cp_lexer_consume_token (parser->lexer);
10956 /* If the next token is `static_assert' we have a static assertion. */
10957 else if (token1->keyword == RID_STATIC_ASSERT)
10958 cp_parser_static_assert (parser, /*member_p=*/false);
10959 /* Anything else must be a simple-declaration. */
10960 else
10961 cp_parser_simple_declaration (parser, !statement_p,
10962 /*maybe_range_for_decl*/NULL);
10965 /* Parse a simple-declaration.
10967 simple-declaration:
10968 decl-specifier-seq [opt] init-declarator-list [opt] ;
10970 init-declarator-list:
10971 init-declarator
10972 init-declarator-list , init-declarator
10974 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10975 function-definition as a simple-declaration.
10977 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10978 parsed declaration if it is an uninitialized single declarator not followed
10979 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10980 if present, will not be consumed. */
10982 static void
10983 cp_parser_simple_declaration (cp_parser* parser,
10984 bool function_definition_allowed_p,
10985 tree *maybe_range_for_decl)
10987 cp_decl_specifier_seq decl_specifiers;
10988 int declares_class_or_enum;
10989 bool saw_declarator;
10991 if (maybe_range_for_decl)
10992 *maybe_range_for_decl = NULL_TREE;
10994 /* Defer access checks until we know what is being declared; the
10995 checks for names appearing in the decl-specifier-seq should be
10996 done as if we were in the scope of the thing being declared. */
10997 push_deferring_access_checks (dk_deferred);
10999 /* Parse the decl-specifier-seq. We have to keep track of whether
11000 or not the decl-specifier-seq declares a named class or
11001 enumeration type, since that is the only case in which the
11002 init-declarator-list is allowed to be empty.
11004 [dcl.dcl]
11006 In a simple-declaration, the optional init-declarator-list can be
11007 omitted only when declaring a class or enumeration, that is when
11008 the decl-specifier-seq contains either a class-specifier, an
11009 elaborated-type-specifier, or an enum-specifier. */
11010 cp_parser_decl_specifier_seq (parser,
11011 CP_PARSER_FLAGS_OPTIONAL,
11012 &decl_specifiers,
11013 &declares_class_or_enum);
11014 /* We no longer need to defer access checks. */
11015 stop_deferring_access_checks ();
11017 /* In a block scope, a valid declaration must always have a
11018 decl-specifier-seq. By not trying to parse declarators, we can
11019 resolve the declaration/expression ambiguity more quickly. */
11020 if (!function_definition_allowed_p
11021 && !decl_specifiers.any_specifiers_p)
11023 cp_parser_error (parser, "expected declaration");
11024 goto done;
11027 /* If the next two tokens are both identifiers, the code is
11028 erroneous. The usual cause of this situation is code like:
11030 T t;
11032 where "T" should name a type -- but does not. */
11033 if (!decl_specifiers.any_type_specifiers_p
11034 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11036 /* If parsing tentatively, we should commit; we really are
11037 looking at a declaration. */
11038 cp_parser_commit_to_tentative_parse (parser);
11039 /* Give up. */
11040 goto done;
11043 /* If we have seen at least one decl-specifier, and the next token
11044 is not a parenthesis, then we must be looking at a declaration.
11045 (After "int (" we might be looking at a functional cast.) */
11046 if (decl_specifiers.any_specifiers_p
11047 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11048 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11049 && !cp_parser_error_occurred (parser))
11050 cp_parser_commit_to_tentative_parse (parser);
11052 /* Keep going until we hit the `;' at the end of the simple
11053 declaration. */
11054 saw_declarator = false;
11055 while (cp_lexer_next_token_is_not (parser->lexer,
11056 CPP_SEMICOLON))
11058 cp_token *token;
11059 bool function_definition_p;
11060 tree decl;
11062 if (saw_declarator)
11064 /* If we are processing next declarator, coma is expected */
11065 token = cp_lexer_peek_token (parser->lexer);
11066 gcc_assert (token->type == CPP_COMMA);
11067 cp_lexer_consume_token (parser->lexer);
11068 if (maybe_range_for_decl)
11069 *maybe_range_for_decl = error_mark_node;
11071 else
11072 saw_declarator = true;
11074 /* Parse the init-declarator. */
11075 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11076 /*checks=*/NULL,
11077 function_definition_allowed_p,
11078 /*member_p=*/false,
11079 declares_class_or_enum,
11080 &function_definition_p,
11081 maybe_range_for_decl);
11082 /* If an error occurred while parsing tentatively, exit quickly.
11083 (That usually happens when in the body of a function; each
11084 statement is treated as a declaration-statement until proven
11085 otherwise.) */
11086 if (cp_parser_error_occurred (parser))
11087 goto done;
11088 /* Handle function definitions specially. */
11089 if (function_definition_p)
11091 /* If the next token is a `,', then we are probably
11092 processing something like:
11094 void f() {}, *p;
11096 which is erroneous. */
11097 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11099 cp_token *token = cp_lexer_peek_token (parser->lexer);
11100 error_at (token->location,
11101 "mixing"
11102 " declarations and function-definitions is forbidden");
11104 /* Otherwise, we're done with the list of declarators. */
11105 else
11107 pop_deferring_access_checks ();
11108 return;
11111 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11112 *maybe_range_for_decl = decl;
11113 /* The next token should be either a `,' or a `;'. */
11114 token = cp_lexer_peek_token (parser->lexer);
11115 /* If it's a `,', there are more declarators to come. */
11116 if (token->type == CPP_COMMA)
11117 /* will be consumed next time around */;
11118 /* If it's a `;', we are done. */
11119 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11120 break;
11121 /* Anything else is an error. */
11122 else
11124 /* If we have already issued an error message we don't need
11125 to issue another one. */
11126 if (decl != error_mark_node
11127 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11128 cp_parser_error (parser, "expected %<,%> or %<;%>");
11129 /* Skip tokens until we reach the end of the statement. */
11130 cp_parser_skip_to_end_of_statement (parser);
11131 /* If the next token is now a `;', consume it. */
11132 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11133 cp_lexer_consume_token (parser->lexer);
11134 goto done;
11136 /* After the first time around, a function-definition is not
11137 allowed -- even if it was OK at first. For example:
11139 int i, f() {}
11141 is not valid. */
11142 function_definition_allowed_p = false;
11145 /* Issue an error message if no declarators are present, and the
11146 decl-specifier-seq does not itself declare a class or
11147 enumeration: [dcl.dcl]/3. */
11148 if (!saw_declarator)
11150 if (cp_parser_declares_only_class_p (parser))
11152 if (!declares_class_or_enum
11153 && decl_specifiers.type
11154 && OVERLOAD_TYPE_P (decl_specifiers.type))
11155 /* Ensure an error is issued anyway when finish_decltype_type,
11156 called via cp_parser_decl_specifier_seq, returns a class or
11157 an enumeration (c++/51786). */
11158 decl_specifiers.type = NULL_TREE;
11159 shadow_tag (&decl_specifiers);
11161 /* Perform any deferred access checks. */
11162 perform_deferred_access_checks (tf_warning_or_error);
11165 /* Consume the `;'. */
11166 if (!maybe_range_for_decl)
11167 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11169 done:
11170 pop_deferring_access_checks ();
11173 /* Parse a decl-specifier-seq.
11175 decl-specifier-seq:
11176 decl-specifier-seq [opt] decl-specifier
11177 decl-specifier attribute-specifier-seq [opt] (C++11)
11179 decl-specifier:
11180 storage-class-specifier
11181 type-specifier
11182 function-specifier
11183 friend
11184 typedef
11186 GNU Extension:
11188 decl-specifier:
11189 attributes
11191 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11193 The parser flags FLAGS is used to control type-specifier parsing.
11195 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11196 flags:
11198 1: one of the decl-specifiers is an elaborated-type-specifier
11199 (i.e., a type declaration)
11200 2: one of the decl-specifiers is an enum-specifier or a
11201 class-specifier (i.e., a type definition)
11205 static void
11206 cp_parser_decl_specifier_seq (cp_parser* parser,
11207 cp_parser_flags flags,
11208 cp_decl_specifier_seq *decl_specs,
11209 int* declares_class_or_enum)
11211 bool constructor_possible_p = !parser->in_declarator_p;
11212 bool found_decl_spec = false;
11213 cp_token *start_token = NULL;
11214 cp_decl_spec ds;
11216 /* Clear DECL_SPECS. */
11217 clear_decl_specs (decl_specs);
11219 /* Assume no class or enumeration type is declared. */
11220 *declares_class_or_enum = 0;
11222 /* Keep reading specifiers until there are no more to read. */
11223 while (true)
11225 bool constructor_p;
11226 cp_token *token;
11227 ds = ds_last;
11229 /* Peek at the next token. */
11230 token = cp_lexer_peek_token (parser->lexer);
11232 /* Save the first token of the decl spec list for error
11233 reporting. */
11234 if (!start_token)
11235 start_token = token;
11236 /* Handle attributes. */
11237 if (cp_next_tokens_can_be_attribute_p (parser))
11239 /* Parse the attributes. */
11240 tree attrs = cp_parser_attributes_opt (parser);
11242 /* In a sequence of declaration specifiers, c++11 attributes
11243 appertain to the type that precede them. In that case
11244 [dcl.spec]/1 says:
11246 The attribute-specifier-seq affects the type only for
11247 the declaration it appears in, not other declarations
11248 involving the same type.
11250 But for now let's force the user to position the
11251 attribute either at the beginning of the declaration or
11252 after the declarator-id, which would clearly mean that it
11253 applies to the declarator. */
11254 if (cxx11_attribute_p (attrs))
11256 if (!found_decl_spec)
11257 /* The c++11 attribute is at the beginning of the
11258 declaration. It appertains to the entity being
11259 declared. */;
11260 else
11262 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11264 /* This is an attribute following a
11265 class-specifier. */
11266 if (decl_specs->type_definition_p)
11267 warn_misplaced_attr_for_class_type (token->location,
11268 decl_specs->type);
11269 attrs = NULL_TREE;
11271 else
11273 decl_specs->std_attributes
11274 = chainon (decl_specs->std_attributes,
11275 attrs);
11276 if (decl_specs->locations[ds_std_attribute] == 0)
11277 decl_specs->locations[ds_std_attribute] = token->location;
11279 continue;
11283 decl_specs->attributes
11284 = chainon (decl_specs->attributes,
11285 attrs);
11286 if (decl_specs->locations[ds_attribute] == 0)
11287 decl_specs->locations[ds_attribute] = token->location;
11288 continue;
11290 /* Assume we will find a decl-specifier keyword. */
11291 found_decl_spec = true;
11292 /* If the next token is an appropriate keyword, we can simply
11293 add it to the list. */
11294 switch (token->keyword)
11296 /* decl-specifier:
11297 friend
11298 constexpr */
11299 case RID_FRIEND:
11300 if (!at_class_scope_p ())
11302 error_at (token->location, "%<friend%> used outside of class");
11303 cp_lexer_purge_token (parser->lexer);
11305 else
11307 ds = ds_friend;
11308 /* Consume the token. */
11309 cp_lexer_consume_token (parser->lexer);
11311 break;
11313 case RID_CONSTEXPR:
11314 ds = ds_constexpr;
11315 cp_lexer_consume_token (parser->lexer);
11316 break;
11318 /* function-specifier:
11319 inline
11320 virtual
11321 explicit */
11322 case RID_INLINE:
11323 case RID_VIRTUAL:
11324 case RID_EXPLICIT:
11325 cp_parser_function_specifier_opt (parser, decl_specs);
11326 break;
11328 /* decl-specifier:
11329 typedef */
11330 case RID_TYPEDEF:
11331 ds = ds_typedef;
11332 /* Consume the token. */
11333 cp_lexer_consume_token (parser->lexer);
11334 /* A constructor declarator cannot appear in a typedef. */
11335 constructor_possible_p = false;
11336 /* The "typedef" keyword can only occur in a declaration; we
11337 may as well commit at this point. */
11338 cp_parser_commit_to_tentative_parse (parser);
11340 if (decl_specs->storage_class != sc_none)
11341 decl_specs->conflicting_specifiers_p = true;
11342 break;
11344 /* storage-class-specifier:
11345 auto
11346 register
11347 static
11348 extern
11349 mutable
11351 GNU Extension:
11352 thread */
11353 case RID_AUTO:
11354 if (cxx_dialect == cxx98)
11356 /* Consume the token. */
11357 cp_lexer_consume_token (parser->lexer);
11359 /* Complain about `auto' as a storage specifier, if
11360 we're complaining about C++0x compatibility. */
11361 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11362 " changes meaning in C++11; please remove it");
11364 /* Set the storage class anyway. */
11365 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11366 token);
11368 else
11369 /* C++0x auto type-specifier. */
11370 found_decl_spec = false;
11371 break;
11373 case RID_REGISTER:
11374 case RID_STATIC:
11375 case RID_EXTERN:
11376 case RID_MUTABLE:
11377 /* Consume the token. */
11378 cp_lexer_consume_token (parser->lexer);
11379 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11380 token);
11381 break;
11382 case RID_THREAD:
11383 /* Consume the token. */
11384 ds = ds_thread;
11385 cp_lexer_consume_token (parser->lexer);
11386 break;
11388 default:
11389 /* We did not yet find a decl-specifier yet. */
11390 found_decl_spec = false;
11391 break;
11394 if (found_decl_spec
11395 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11396 && token->keyword != RID_CONSTEXPR)
11397 error ("decl-specifier invalid in condition");
11399 if (ds != ds_last)
11400 set_and_check_decl_spec_loc (decl_specs, ds, token);
11402 /* Constructors are a special case. The `S' in `S()' is not a
11403 decl-specifier; it is the beginning of the declarator. */
11404 constructor_p
11405 = (!found_decl_spec
11406 && constructor_possible_p
11407 && (cp_parser_constructor_declarator_p
11408 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11410 /* If we don't have a DECL_SPEC yet, then we must be looking at
11411 a type-specifier. */
11412 if (!found_decl_spec && !constructor_p)
11414 int decl_spec_declares_class_or_enum;
11415 bool is_cv_qualifier;
11416 tree type_spec;
11418 type_spec
11419 = cp_parser_type_specifier (parser, flags,
11420 decl_specs,
11421 /*is_declaration=*/true,
11422 &decl_spec_declares_class_or_enum,
11423 &is_cv_qualifier);
11424 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11426 /* If this type-specifier referenced a user-defined type
11427 (a typedef, class-name, etc.), then we can't allow any
11428 more such type-specifiers henceforth.
11430 [dcl.spec]
11432 The longest sequence of decl-specifiers that could
11433 possibly be a type name is taken as the
11434 decl-specifier-seq of a declaration. The sequence shall
11435 be self-consistent as described below.
11437 [dcl.type]
11439 As a general rule, at most one type-specifier is allowed
11440 in the complete decl-specifier-seq of a declaration. The
11441 only exceptions are the following:
11443 -- const or volatile can be combined with any other
11444 type-specifier.
11446 -- signed or unsigned can be combined with char, long,
11447 short, or int.
11449 -- ..
11451 Example:
11453 typedef char* Pc;
11454 void g (const int Pc);
11456 Here, Pc is *not* part of the decl-specifier seq; it's
11457 the declarator. Therefore, once we see a type-specifier
11458 (other than a cv-qualifier), we forbid any additional
11459 user-defined types. We *do* still allow things like `int
11460 int' to be considered a decl-specifier-seq, and issue the
11461 error message later. */
11462 if (type_spec && !is_cv_qualifier)
11463 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11464 /* A constructor declarator cannot follow a type-specifier. */
11465 if (type_spec)
11467 constructor_possible_p = false;
11468 found_decl_spec = true;
11469 if (!is_cv_qualifier)
11470 decl_specs->any_type_specifiers_p = true;
11474 /* If we still do not have a DECL_SPEC, then there are no more
11475 decl-specifiers. */
11476 if (!found_decl_spec)
11477 break;
11479 decl_specs->any_specifiers_p = true;
11480 /* After we see one decl-specifier, further decl-specifiers are
11481 always optional. */
11482 flags |= CP_PARSER_FLAGS_OPTIONAL;
11485 /* Don't allow a friend specifier with a class definition. */
11486 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11487 && (*declares_class_or_enum & 2))
11488 error_at (decl_specs->locations[ds_friend],
11489 "class definition may not be declared a friend");
11492 /* Parse an (optional) storage-class-specifier.
11494 storage-class-specifier:
11495 auto
11496 register
11497 static
11498 extern
11499 mutable
11501 GNU Extension:
11503 storage-class-specifier:
11504 thread
11506 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11508 static tree
11509 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11511 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11513 case RID_AUTO:
11514 if (cxx_dialect != cxx98)
11515 return NULL_TREE;
11516 /* Fall through for C++98. */
11518 case RID_REGISTER:
11519 case RID_STATIC:
11520 case RID_EXTERN:
11521 case RID_MUTABLE:
11522 case RID_THREAD:
11523 /* Consume the token. */
11524 return cp_lexer_consume_token (parser->lexer)->u.value;
11526 default:
11527 return NULL_TREE;
11531 /* Parse an (optional) function-specifier.
11533 function-specifier:
11534 inline
11535 virtual
11536 explicit
11538 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11539 Updates DECL_SPECS, if it is non-NULL. */
11541 static tree
11542 cp_parser_function_specifier_opt (cp_parser* parser,
11543 cp_decl_specifier_seq *decl_specs)
11545 cp_token *token = cp_lexer_peek_token (parser->lexer);
11546 switch (token->keyword)
11548 case RID_INLINE:
11549 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11550 break;
11552 case RID_VIRTUAL:
11553 /* 14.5.2.3 [temp.mem]
11555 A member function template shall not be virtual. */
11556 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11557 error_at (token->location, "templates may not be %<virtual%>");
11558 else
11559 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11560 break;
11562 case RID_EXPLICIT:
11563 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11564 break;
11566 default:
11567 return NULL_TREE;
11570 /* Consume the token. */
11571 return cp_lexer_consume_token (parser->lexer)->u.value;
11574 /* Parse a linkage-specification.
11576 linkage-specification:
11577 extern string-literal { declaration-seq [opt] }
11578 extern string-literal declaration */
11580 static void
11581 cp_parser_linkage_specification (cp_parser* parser)
11583 tree linkage;
11585 /* Look for the `extern' keyword. */
11586 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11588 /* Look for the string-literal. */
11589 linkage = cp_parser_string_literal (parser, false, false);
11591 /* Transform the literal into an identifier. If the literal is a
11592 wide-character string, or contains embedded NULs, then we can't
11593 handle it as the user wants. */
11594 if (strlen (TREE_STRING_POINTER (linkage))
11595 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11597 cp_parser_error (parser, "invalid linkage-specification");
11598 /* Assume C++ linkage. */
11599 linkage = lang_name_cplusplus;
11601 else
11602 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11604 /* We're now using the new linkage. */
11605 push_lang_context (linkage);
11607 /* If the next token is a `{', then we're using the first
11608 production. */
11609 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11611 cp_ensure_no_omp_declare_simd (parser);
11613 /* Consume the `{' token. */
11614 cp_lexer_consume_token (parser->lexer);
11615 /* Parse the declarations. */
11616 cp_parser_declaration_seq_opt (parser);
11617 /* Look for the closing `}'. */
11618 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11620 /* Otherwise, there's just one declaration. */
11621 else
11623 bool saved_in_unbraced_linkage_specification_p;
11625 saved_in_unbraced_linkage_specification_p
11626 = parser->in_unbraced_linkage_specification_p;
11627 parser->in_unbraced_linkage_specification_p = true;
11628 cp_parser_declaration (parser);
11629 parser->in_unbraced_linkage_specification_p
11630 = saved_in_unbraced_linkage_specification_p;
11633 /* We're done with the linkage-specification. */
11634 pop_lang_context ();
11637 /* Parse a static_assert-declaration.
11639 static_assert-declaration:
11640 static_assert ( constant-expression , string-literal ) ;
11642 If MEMBER_P, this static_assert is a class member. */
11644 static void
11645 cp_parser_static_assert(cp_parser *parser, bool member_p)
11647 tree condition;
11648 tree message;
11649 cp_token *token;
11650 location_t saved_loc;
11651 bool dummy;
11653 /* Peek at the `static_assert' token so we can keep track of exactly
11654 where the static assertion started. */
11655 token = cp_lexer_peek_token (parser->lexer);
11656 saved_loc = token->location;
11658 /* Look for the `static_assert' keyword. */
11659 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11660 RT_STATIC_ASSERT))
11661 return;
11663 /* We know we are in a static assertion; commit to any tentative
11664 parse. */
11665 if (cp_parser_parsing_tentatively (parser))
11666 cp_parser_commit_to_tentative_parse (parser);
11668 /* Parse the `(' starting the static assertion condition. */
11669 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11671 /* Parse the constant-expression. Allow a non-constant expression
11672 here in order to give better diagnostics in finish_static_assert. */
11673 condition =
11674 cp_parser_constant_expression (parser,
11675 /*allow_non_constant_p=*/true,
11676 /*non_constant_p=*/&dummy);
11678 /* Parse the separating `,'. */
11679 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11681 /* Parse the string-literal message. */
11682 message = cp_parser_string_literal (parser,
11683 /*translate=*/false,
11684 /*wide_ok=*/true);
11686 /* A `)' completes the static assertion. */
11687 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11688 cp_parser_skip_to_closing_parenthesis (parser,
11689 /*recovering=*/true,
11690 /*or_comma=*/false,
11691 /*consume_paren=*/true);
11693 /* A semicolon terminates the declaration. */
11694 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11696 /* Complete the static assertion, which may mean either processing
11697 the static assert now or saving it for template instantiation. */
11698 finish_static_assert (condition, message, saved_loc, member_p);
11701 /* Parse the expression in decltype ( expression ). */
11703 static tree
11704 cp_parser_decltype_expr (cp_parser *parser,
11705 bool &id_expression_or_member_access_p)
11707 cp_token *id_expr_start_token;
11708 tree expr;
11710 /* First, try parsing an id-expression. */
11711 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11712 cp_parser_parse_tentatively (parser);
11713 expr = cp_parser_id_expression (parser,
11714 /*template_keyword_p=*/false,
11715 /*check_dependency_p=*/true,
11716 /*template_p=*/NULL,
11717 /*declarator_p=*/false,
11718 /*optional_p=*/false);
11720 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11722 bool non_integral_constant_expression_p = false;
11723 tree id_expression = expr;
11724 cp_id_kind idk;
11725 const char *error_msg;
11727 if (identifier_p (expr))
11728 /* Lookup the name we got back from the id-expression. */
11729 expr = cp_parser_lookup_name_simple (parser, expr,
11730 id_expr_start_token->location);
11732 if (expr
11733 && expr != error_mark_node
11734 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11735 && TREE_CODE (expr) != TYPE_DECL
11736 && (TREE_CODE (expr) != BIT_NOT_EXPR
11737 || !TYPE_P (TREE_OPERAND (expr, 0)))
11738 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11740 /* Complete lookup of the id-expression. */
11741 expr = (finish_id_expression
11742 (id_expression, expr, parser->scope, &idk,
11743 /*integral_constant_expression_p=*/false,
11744 /*allow_non_integral_constant_expression_p=*/true,
11745 &non_integral_constant_expression_p,
11746 /*template_p=*/false,
11747 /*done=*/true,
11748 /*address_p=*/false,
11749 /*template_arg_p=*/false,
11750 &error_msg,
11751 id_expr_start_token->location));
11753 if (expr == error_mark_node)
11754 /* We found an id-expression, but it was something that we
11755 should not have found. This is an error, not something
11756 we can recover from, so note that we found an
11757 id-expression and we'll recover as gracefully as
11758 possible. */
11759 id_expression_or_member_access_p = true;
11762 if (expr
11763 && expr != error_mark_node
11764 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11765 /* We have an id-expression. */
11766 id_expression_or_member_access_p = true;
11769 if (!id_expression_or_member_access_p)
11771 /* Abort the id-expression parse. */
11772 cp_parser_abort_tentative_parse (parser);
11774 /* Parsing tentatively, again. */
11775 cp_parser_parse_tentatively (parser);
11777 /* Parse a class member access. */
11778 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11779 /*cast_p=*/false, /*decltype*/true,
11780 /*member_access_only_p=*/true, NULL);
11782 if (expr
11783 && expr != error_mark_node
11784 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11785 /* We have an id-expression. */
11786 id_expression_or_member_access_p = true;
11789 if (id_expression_or_member_access_p)
11790 /* We have parsed the complete id-expression or member access. */
11791 cp_parser_parse_definitely (parser);
11792 else
11794 /* Abort our attempt to parse an id-expression or member access
11795 expression. */
11796 cp_parser_abort_tentative_parse (parser);
11798 /* Parse a full expression. */
11799 expr = cp_parser_expression (parser, /*cast_p=*/false,
11800 /*decltype*/true, NULL);
11803 return expr;
11806 /* Parse a `decltype' type. Returns the type.
11808 simple-type-specifier:
11809 decltype ( expression )
11810 C++14 proposal:
11811 decltype ( auto ) */
11813 static tree
11814 cp_parser_decltype (cp_parser *parser)
11816 tree expr;
11817 bool id_expression_or_member_access_p = false;
11818 const char *saved_message;
11819 bool saved_integral_constant_expression_p;
11820 bool saved_non_integral_constant_expression_p;
11821 bool saved_greater_than_is_operator_p;
11822 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11824 if (start_token->type == CPP_DECLTYPE)
11826 /* Already parsed. */
11827 cp_lexer_consume_token (parser->lexer);
11828 return start_token->u.value;
11831 /* Look for the `decltype' token. */
11832 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11833 return error_mark_node;
11835 /* Parse the opening `('. */
11836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11837 return error_mark_node;
11839 /* decltype (auto) */
11840 if (cxx_dialect >= cxx1y
11841 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11843 cp_lexer_consume_token (parser->lexer);
11844 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11845 return error_mark_node;
11846 expr = make_decltype_auto ();
11847 AUTO_IS_DECLTYPE (expr) = true;
11848 goto rewrite;
11851 /* Types cannot be defined in a `decltype' expression. Save away the
11852 old message. */
11853 saved_message = parser->type_definition_forbidden_message;
11855 /* And create the new one. */
11856 parser->type_definition_forbidden_message
11857 = G_("types may not be defined in %<decltype%> expressions");
11859 /* The restrictions on constant-expressions do not apply inside
11860 decltype expressions. */
11861 saved_integral_constant_expression_p
11862 = parser->integral_constant_expression_p;
11863 saved_non_integral_constant_expression_p
11864 = parser->non_integral_constant_expression_p;
11865 parser->integral_constant_expression_p = false;
11867 /* Within a parenthesized expression, a `>' token is always
11868 the greater-than operator. */
11869 saved_greater_than_is_operator_p
11870 = parser->greater_than_is_operator_p;
11871 parser->greater_than_is_operator_p = true;
11873 /* Do not actually evaluate the expression. */
11874 ++cp_unevaluated_operand;
11876 /* Do not warn about problems with the expression. */
11877 ++c_inhibit_evaluation_warnings;
11879 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
11881 /* Go back to evaluating expressions. */
11882 --cp_unevaluated_operand;
11883 --c_inhibit_evaluation_warnings;
11885 /* The `>' token might be the end of a template-id or
11886 template-parameter-list now. */
11887 parser->greater_than_is_operator_p
11888 = saved_greater_than_is_operator_p;
11890 /* Restore the old message and the integral constant expression
11891 flags. */
11892 parser->type_definition_forbidden_message = saved_message;
11893 parser->integral_constant_expression_p
11894 = saved_integral_constant_expression_p;
11895 parser->non_integral_constant_expression_p
11896 = saved_non_integral_constant_expression_p;
11898 /* Parse to the closing `)'. */
11899 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11901 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11902 /*consume_paren=*/true);
11903 return error_mark_node;
11906 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11907 tf_warning_or_error);
11909 rewrite:
11910 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11911 it again. */
11912 start_token->type = CPP_DECLTYPE;
11913 start_token->u.value = expr;
11914 start_token->keyword = RID_MAX;
11915 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11917 return expr;
11920 /* Special member functions [gram.special] */
11922 /* Parse a conversion-function-id.
11924 conversion-function-id:
11925 operator conversion-type-id
11927 Returns an IDENTIFIER_NODE representing the operator. */
11929 static tree
11930 cp_parser_conversion_function_id (cp_parser* parser)
11932 tree type;
11933 tree saved_scope;
11934 tree saved_qualifying_scope;
11935 tree saved_object_scope;
11936 tree pushed_scope = NULL_TREE;
11938 /* Look for the `operator' token. */
11939 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11940 return error_mark_node;
11941 /* When we parse the conversion-type-id, the current scope will be
11942 reset. However, we need that information in able to look up the
11943 conversion function later, so we save it here. */
11944 saved_scope = parser->scope;
11945 saved_qualifying_scope = parser->qualifying_scope;
11946 saved_object_scope = parser->object_scope;
11947 /* We must enter the scope of the class so that the names of
11948 entities declared within the class are available in the
11949 conversion-type-id. For example, consider:
11951 struct S {
11952 typedef int I;
11953 operator I();
11956 S::operator I() { ... }
11958 In order to see that `I' is a type-name in the definition, we
11959 must be in the scope of `S'. */
11960 if (saved_scope)
11961 pushed_scope = push_scope (saved_scope);
11962 /* Parse the conversion-type-id. */
11963 type = cp_parser_conversion_type_id (parser);
11964 /* Leave the scope of the class, if any. */
11965 if (pushed_scope)
11966 pop_scope (pushed_scope);
11967 /* Restore the saved scope. */
11968 parser->scope = saved_scope;
11969 parser->qualifying_scope = saved_qualifying_scope;
11970 parser->object_scope = saved_object_scope;
11971 /* If the TYPE is invalid, indicate failure. */
11972 if (type == error_mark_node)
11973 return error_mark_node;
11974 return mangle_conv_op_name_for_type (type);
11977 /* Parse a conversion-type-id:
11979 conversion-type-id:
11980 type-specifier-seq conversion-declarator [opt]
11982 Returns the TYPE specified. */
11984 static tree
11985 cp_parser_conversion_type_id (cp_parser* parser)
11987 tree attributes;
11988 cp_decl_specifier_seq type_specifiers;
11989 cp_declarator *declarator;
11990 tree type_specified;
11991 const char *saved_message;
11993 /* Parse the attributes. */
11994 attributes = cp_parser_attributes_opt (parser);
11996 saved_message = parser->type_definition_forbidden_message;
11997 parser->type_definition_forbidden_message
11998 = G_("types may not be defined in a conversion-type-id");
12000 /* Parse the type-specifiers. */
12001 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12002 /*is_trailing_return=*/false,
12003 &type_specifiers);
12005 parser->type_definition_forbidden_message = saved_message;
12007 /* If that didn't work, stop. */
12008 if (type_specifiers.type == error_mark_node)
12009 return error_mark_node;
12010 /* Parse the conversion-declarator. */
12011 declarator = cp_parser_conversion_declarator_opt (parser);
12013 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12014 /*initialized=*/0, &attributes);
12015 if (attributes)
12016 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12018 /* Don't give this error when parsing tentatively. This happens to
12019 work because we always parse this definitively once. */
12020 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12021 && type_uses_auto (type_specified))
12023 if (cxx_dialect < cxx1y)
12025 error ("invalid use of %<auto%> in conversion operator");
12026 return error_mark_node;
12028 else if (template_parm_scope_p ())
12029 warning (0, "use of %<auto%> in member template "
12030 "conversion operator can never be deduced");
12033 return type_specified;
12036 /* Parse an (optional) conversion-declarator.
12038 conversion-declarator:
12039 ptr-operator conversion-declarator [opt]
12043 static cp_declarator *
12044 cp_parser_conversion_declarator_opt (cp_parser* parser)
12046 enum tree_code code;
12047 tree class_type, std_attributes = NULL_TREE;
12048 cp_cv_quals cv_quals;
12050 /* We don't know if there's a ptr-operator next, or not. */
12051 cp_parser_parse_tentatively (parser);
12052 /* Try the ptr-operator. */
12053 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12054 &std_attributes);
12055 /* If it worked, look for more conversion-declarators. */
12056 if (cp_parser_parse_definitely (parser))
12058 cp_declarator *declarator;
12060 /* Parse another optional declarator. */
12061 declarator = cp_parser_conversion_declarator_opt (parser);
12063 declarator = cp_parser_make_indirect_declarator
12064 (code, class_type, cv_quals, declarator, std_attributes);
12066 return declarator;
12069 return NULL;
12072 /* Parse an (optional) ctor-initializer.
12074 ctor-initializer:
12075 : mem-initializer-list
12077 Returns TRUE iff the ctor-initializer was actually present. */
12079 static bool
12080 cp_parser_ctor_initializer_opt (cp_parser* parser)
12082 /* If the next token is not a `:', then there is no
12083 ctor-initializer. */
12084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12086 /* Do default initialization of any bases and members. */
12087 if (DECL_CONSTRUCTOR_P (current_function_decl))
12088 finish_mem_initializers (NULL_TREE);
12090 return false;
12093 /* Consume the `:' token. */
12094 cp_lexer_consume_token (parser->lexer);
12095 /* And the mem-initializer-list. */
12096 cp_parser_mem_initializer_list (parser);
12098 return true;
12101 /* Parse a mem-initializer-list.
12103 mem-initializer-list:
12104 mem-initializer ... [opt]
12105 mem-initializer ... [opt] , mem-initializer-list */
12107 static void
12108 cp_parser_mem_initializer_list (cp_parser* parser)
12110 tree mem_initializer_list = NULL_TREE;
12111 tree target_ctor = error_mark_node;
12112 cp_token *token = cp_lexer_peek_token (parser->lexer);
12114 /* Let the semantic analysis code know that we are starting the
12115 mem-initializer-list. */
12116 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12117 error_at (token->location,
12118 "only constructors take member initializers");
12120 /* Loop through the list. */
12121 while (true)
12123 tree mem_initializer;
12125 token = cp_lexer_peek_token (parser->lexer);
12126 /* Parse the mem-initializer. */
12127 mem_initializer = cp_parser_mem_initializer (parser);
12128 /* If the next token is a `...', we're expanding member initializers. */
12129 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12131 /* Consume the `...'. */
12132 cp_lexer_consume_token (parser->lexer);
12134 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12135 can be expanded but members cannot. */
12136 if (mem_initializer != error_mark_node
12137 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12139 error_at (token->location,
12140 "cannot expand initializer for member %<%D%>",
12141 TREE_PURPOSE (mem_initializer));
12142 mem_initializer = error_mark_node;
12145 /* Construct the pack expansion type. */
12146 if (mem_initializer != error_mark_node)
12147 mem_initializer = make_pack_expansion (mem_initializer);
12149 if (target_ctor != error_mark_node
12150 && mem_initializer != error_mark_node)
12152 error ("mem-initializer for %qD follows constructor delegation",
12153 TREE_PURPOSE (mem_initializer));
12154 mem_initializer = error_mark_node;
12156 /* Look for a target constructor. */
12157 if (mem_initializer != error_mark_node
12158 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12159 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12161 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12162 if (mem_initializer_list)
12164 error ("constructor delegation follows mem-initializer for %qD",
12165 TREE_PURPOSE (mem_initializer_list));
12166 mem_initializer = error_mark_node;
12168 target_ctor = mem_initializer;
12170 /* Add it to the list, unless it was erroneous. */
12171 if (mem_initializer != error_mark_node)
12173 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12174 mem_initializer_list = mem_initializer;
12176 /* If the next token is not a `,', we're done. */
12177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12178 break;
12179 /* Consume the `,' token. */
12180 cp_lexer_consume_token (parser->lexer);
12183 /* Perform semantic analysis. */
12184 if (DECL_CONSTRUCTOR_P (current_function_decl))
12185 finish_mem_initializers (mem_initializer_list);
12188 /* Parse a mem-initializer.
12190 mem-initializer:
12191 mem-initializer-id ( expression-list [opt] )
12192 mem-initializer-id braced-init-list
12194 GNU extension:
12196 mem-initializer:
12197 ( expression-list [opt] )
12199 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12200 class) or FIELD_DECL (for a non-static data member) to initialize;
12201 the TREE_VALUE is the expression-list. An empty initialization
12202 list is represented by void_list_node. */
12204 static tree
12205 cp_parser_mem_initializer (cp_parser* parser)
12207 tree mem_initializer_id;
12208 tree expression_list;
12209 tree member;
12210 cp_token *token = cp_lexer_peek_token (parser->lexer);
12212 /* Find out what is being initialized. */
12213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12215 permerror (token->location,
12216 "anachronistic old-style base class initializer");
12217 mem_initializer_id = NULL_TREE;
12219 else
12221 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12222 if (mem_initializer_id == error_mark_node)
12223 return mem_initializer_id;
12225 member = expand_member_init (mem_initializer_id);
12226 if (member && !DECL_P (member))
12227 in_base_initializer = 1;
12229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12231 bool expr_non_constant_p;
12232 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12233 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12234 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12235 expression_list = build_tree_list (NULL_TREE, expression_list);
12237 else
12239 vec<tree, va_gc> *vec;
12240 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12241 /*cast_p=*/false,
12242 /*allow_expansion_p=*/true,
12243 /*non_constant_p=*/NULL);
12244 if (vec == NULL)
12245 return error_mark_node;
12246 expression_list = build_tree_list_vec (vec);
12247 release_tree_vector (vec);
12250 if (expression_list == error_mark_node)
12251 return error_mark_node;
12252 if (!expression_list)
12253 expression_list = void_type_node;
12255 in_base_initializer = 0;
12257 return member ? build_tree_list (member, expression_list) : error_mark_node;
12260 /* Parse a mem-initializer-id.
12262 mem-initializer-id:
12263 :: [opt] nested-name-specifier [opt] class-name
12264 identifier
12266 Returns a TYPE indicating the class to be initializer for the first
12267 production. Returns an IDENTIFIER_NODE indicating the data member
12268 to be initialized for the second production. */
12270 static tree
12271 cp_parser_mem_initializer_id (cp_parser* parser)
12273 bool global_scope_p;
12274 bool nested_name_specifier_p;
12275 bool template_p = false;
12276 tree id;
12278 cp_token *token = cp_lexer_peek_token (parser->lexer);
12280 /* `typename' is not allowed in this context ([temp.res]). */
12281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12283 error_at (token->location,
12284 "keyword %<typename%> not allowed in this context (a qualified "
12285 "member initializer is implicitly a type)");
12286 cp_lexer_consume_token (parser->lexer);
12288 /* Look for the optional `::' operator. */
12289 global_scope_p
12290 = (cp_parser_global_scope_opt (parser,
12291 /*current_scope_valid_p=*/false)
12292 != NULL_TREE);
12293 /* Look for the optional nested-name-specifier. The simplest way to
12294 implement:
12296 [temp.res]
12298 The keyword `typename' is not permitted in a base-specifier or
12299 mem-initializer; in these contexts a qualified name that
12300 depends on a template-parameter is implicitly assumed to be a
12301 type name.
12303 is to assume that we have seen the `typename' keyword at this
12304 point. */
12305 nested_name_specifier_p
12306 = (cp_parser_nested_name_specifier_opt (parser,
12307 /*typename_keyword_p=*/true,
12308 /*check_dependency_p=*/true,
12309 /*type_p=*/true,
12310 /*is_declaration=*/true)
12311 != NULL_TREE);
12312 if (nested_name_specifier_p)
12313 template_p = cp_parser_optional_template_keyword (parser);
12314 /* If there is a `::' operator or a nested-name-specifier, then we
12315 are definitely looking for a class-name. */
12316 if (global_scope_p || nested_name_specifier_p)
12317 return cp_parser_class_name (parser,
12318 /*typename_keyword_p=*/true,
12319 /*template_keyword_p=*/template_p,
12320 typename_type,
12321 /*check_dependency_p=*/true,
12322 /*class_head_p=*/false,
12323 /*is_declaration=*/true);
12324 /* Otherwise, we could also be looking for an ordinary identifier. */
12325 cp_parser_parse_tentatively (parser);
12326 /* Try a class-name. */
12327 id = cp_parser_class_name (parser,
12328 /*typename_keyword_p=*/true,
12329 /*template_keyword_p=*/false,
12330 none_type,
12331 /*check_dependency_p=*/true,
12332 /*class_head_p=*/false,
12333 /*is_declaration=*/true);
12334 /* If we found one, we're done. */
12335 if (cp_parser_parse_definitely (parser))
12336 return id;
12337 /* Otherwise, look for an ordinary identifier. */
12338 return cp_parser_identifier (parser);
12341 /* Overloading [gram.over] */
12343 /* Parse an operator-function-id.
12345 operator-function-id:
12346 operator operator
12348 Returns an IDENTIFIER_NODE for the operator which is a
12349 human-readable spelling of the identifier, e.g., `operator +'. */
12351 static tree
12352 cp_parser_operator_function_id (cp_parser* parser)
12354 /* Look for the `operator' keyword. */
12355 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12356 return error_mark_node;
12357 /* And then the name of the operator itself. */
12358 return cp_parser_operator (parser);
12361 /* Return an identifier node for a user-defined literal operator.
12362 The suffix identifier is chained to the operator name identifier. */
12364 static tree
12365 cp_literal_operator_id (const char* name)
12367 tree identifier;
12368 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12369 + strlen (name) + 10);
12370 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12371 identifier = get_identifier (buffer);
12373 return identifier;
12376 /* Parse an operator.
12378 operator:
12379 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12380 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12381 || ++ -- , ->* -> () []
12383 GNU Extensions:
12385 operator:
12386 <? >? <?= >?=
12388 Returns an IDENTIFIER_NODE for the operator which is a
12389 human-readable spelling of the identifier, e.g., `operator +'. */
12391 static tree
12392 cp_parser_operator (cp_parser* parser)
12394 tree id = NULL_TREE;
12395 cp_token *token;
12396 bool bad_encoding_prefix = false;
12398 /* Peek at the next token. */
12399 token = cp_lexer_peek_token (parser->lexer);
12400 /* Figure out which operator we have. */
12401 switch (token->type)
12403 case CPP_KEYWORD:
12405 enum tree_code op;
12407 /* The keyword should be either `new' or `delete'. */
12408 if (token->keyword == RID_NEW)
12409 op = NEW_EXPR;
12410 else if (token->keyword == RID_DELETE)
12411 op = DELETE_EXPR;
12412 else
12413 break;
12415 /* Consume the `new' or `delete' token. */
12416 cp_lexer_consume_token (parser->lexer);
12418 /* Peek at the next token. */
12419 token = cp_lexer_peek_token (parser->lexer);
12420 /* If it's a `[' token then this is the array variant of the
12421 operator. */
12422 if (token->type == CPP_OPEN_SQUARE)
12424 /* Consume the `[' token. */
12425 cp_lexer_consume_token (parser->lexer);
12426 /* Look for the `]' token. */
12427 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12428 id = ansi_opname (op == NEW_EXPR
12429 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12431 /* Otherwise, we have the non-array variant. */
12432 else
12433 id = ansi_opname (op);
12435 return id;
12438 case CPP_PLUS:
12439 id = ansi_opname (PLUS_EXPR);
12440 break;
12442 case CPP_MINUS:
12443 id = ansi_opname (MINUS_EXPR);
12444 break;
12446 case CPP_MULT:
12447 id = ansi_opname (MULT_EXPR);
12448 break;
12450 case CPP_DIV:
12451 id = ansi_opname (TRUNC_DIV_EXPR);
12452 break;
12454 case CPP_MOD:
12455 id = ansi_opname (TRUNC_MOD_EXPR);
12456 break;
12458 case CPP_XOR:
12459 id = ansi_opname (BIT_XOR_EXPR);
12460 break;
12462 case CPP_AND:
12463 id = ansi_opname (BIT_AND_EXPR);
12464 break;
12466 case CPP_OR:
12467 id = ansi_opname (BIT_IOR_EXPR);
12468 break;
12470 case CPP_COMPL:
12471 id = ansi_opname (BIT_NOT_EXPR);
12472 break;
12474 case CPP_NOT:
12475 id = ansi_opname (TRUTH_NOT_EXPR);
12476 break;
12478 case CPP_EQ:
12479 id = ansi_assopname (NOP_EXPR);
12480 break;
12482 case CPP_LESS:
12483 id = ansi_opname (LT_EXPR);
12484 break;
12486 case CPP_GREATER:
12487 id = ansi_opname (GT_EXPR);
12488 break;
12490 case CPP_PLUS_EQ:
12491 id = ansi_assopname (PLUS_EXPR);
12492 break;
12494 case CPP_MINUS_EQ:
12495 id = ansi_assopname (MINUS_EXPR);
12496 break;
12498 case CPP_MULT_EQ:
12499 id = ansi_assopname (MULT_EXPR);
12500 break;
12502 case CPP_DIV_EQ:
12503 id = ansi_assopname (TRUNC_DIV_EXPR);
12504 break;
12506 case CPP_MOD_EQ:
12507 id = ansi_assopname (TRUNC_MOD_EXPR);
12508 break;
12510 case CPP_XOR_EQ:
12511 id = ansi_assopname (BIT_XOR_EXPR);
12512 break;
12514 case CPP_AND_EQ:
12515 id = ansi_assopname (BIT_AND_EXPR);
12516 break;
12518 case CPP_OR_EQ:
12519 id = ansi_assopname (BIT_IOR_EXPR);
12520 break;
12522 case CPP_LSHIFT:
12523 id = ansi_opname (LSHIFT_EXPR);
12524 break;
12526 case CPP_RSHIFT:
12527 id = ansi_opname (RSHIFT_EXPR);
12528 break;
12530 case CPP_LSHIFT_EQ:
12531 id = ansi_assopname (LSHIFT_EXPR);
12532 break;
12534 case CPP_RSHIFT_EQ:
12535 id = ansi_assopname (RSHIFT_EXPR);
12536 break;
12538 case CPP_EQ_EQ:
12539 id = ansi_opname (EQ_EXPR);
12540 break;
12542 case CPP_NOT_EQ:
12543 id = ansi_opname (NE_EXPR);
12544 break;
12546 case CPP_LESS_EQ:
12547 id = ansi_opname (LE_EXPR);
12548 break;
12550 case CPP_GREATER_EQ:
12551 id = ansi_opname (GE_EXPR);
12552 break;
12554 case CPP_AND_AND:
12555 id = ansi_opname (TRUTH_ANDIF_EXPR);
12556 break;
12558 case CPP_OR_OR:
12559 id = ansi_opname (TRUTH_ORIF_EXPR);
12560 break;
12562 case CPP_PLUS_PLUS:
12563 id = ansi_opname (POSTINCREMENT_EXPR);
12564 break;
12566 case CPP_MINUS_MINUS:
12567 id = ansi_opname (PREDECREMENT_EXPR);
12568 break;
12570 case CPP_COMMA:
12571 id = ansi_opname (COMPOUND_EXPR);
12572 break;
12574 case CPP_DEREF_STAR:
12575 id = ansi_opname (MEMBER_REF);
12576 break;
12578 case CPP_DEREF:
12579 id = ansi_opname (COMPONENT_REF);
12580 break;
12582 case CPP_OPEN_PAREN:
12583 /* Consume the `('. */
12584 cp_lexer_consume_token (parser->lexer);
12585 /* Look for the matching `)'. */
12586 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12587 return ansi_opname (CALL_EXPR);
12589 case CPP_OPEN_SQUARE:
12590 /* Consume the `['. */
12591 cp_lexer_consume_token (parser->lexer);
12592 /* Look for the matching `]'. */
12593 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12594 return ansi_opname (ARRAY_REF);
12596 case CPP_WSTRING:
12597 case CPP_STRING16:
12598 case CPP_STRING32:
12599 case CPP_UTF8STRING:
12600 bad_encoding_prefix = true;
12601 /* Fall through. */
12603 case CPP_STRING:
12604 if (cxx_dialect == cxx98)
12605 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12606 if (bad_encoding_prefix)
12608 error ("invalid encoding prefix in literal operator");
12609 return error_mark_node;
12611 if (TREE_STRING_LENGTH (token->u.value) > 2)
12613 error ("expected empty string after %<operator%> keyword");
12614 return error_mark_node;
12616 /* Consume the string. */
12617 cp_lexer_consume_token (parser->lexer);
12618 /* Look for the suffix identifier. */
12619 token = cp_lexer_peek_token (parser->lexer);
12620 if (token->type == CPP_NAME)
12622 id = cp_parser_identifier (parser);
12623 if (id != error_mark_node)
12625 const char *name = IDENTIFIER_POINTER (id);
12626 return cp_literal_operator_id (name);
12629 else if (token->type == CPP_KEYWORD)
12631 error ("unexpected keyword;"
12632 " remove space between quotes and suffix identifier");
12633 return error_mark_node;
12635 else
12637 error ("expected suffix identifier");
12638 return error_mark_node;
12641 case CPP_WSTRING_USERDEF:
12642 case CPP_STRING16_USERDEF:
12643 case CPP_STRING32_USERDEF:
12644 case CPP_UTF8STRING_USERDEF:
12645 bad_encoding_prefix = true;
12646 /* Fall through. */
12648 case CPP_STRING_USERDEF:
12649 if (cxx_dialect == cxx98)
12650 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12651 if (bad_encoding_prefix)
12653 error ("invalid encoding prefix in literal operator");
12654 return error_mark_node;
12657 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12658 if (TREE_STRING_LENGTH (string_tree) > 2)
12660 error ("expected empty string after %<operator%> keyword");
12661 return error_mark_node;
12663 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12664 /* Consume the user-defined string literal. */
12665 cp_lexer_consume_token (parser->lexer);
12666 if (id != error_mark_node)
12668 const char *name = IDENTIFIER_POINTER (id);
12669 return cp_literal_operator_id (name);
12671 else
12672 return error_mark_node;
12675 default:
12676 /* Anything else is an error. */
12677 break;
12680 /* If we have selected an identifier, we need to consume the
12681 operator token. */
12682 if (id)
12683 cp_lexer_consume_token (parser->lexer);
12684 /* Otherwise, no valid operator name was present. */
12685 else
12687 cp_parser_error (parser, "expected operator");
12688 id = error_mark_node;
12691 return id;
12694 /* Parse a template-declaration.
12696 template-declaration:
12697 export [opt] template < template-parameter-list > declaration
12699 If MEMBER_P is TRUE, this template-declaration occurs within a
12700 class-specifier.
12702 The grammar rule given by the standard isn't correct. What
12703 is really meant is:
12705 template-declaration:
12706 export [opt] template-parameter-list-seq
12707 decl-specifier-seq [opt] init-declarator [opt] ;
12708 export [opt] template-parameter-list-seq
12709 function-definition
12711 template-parameter-list-seq:
12712 template-parameter-list-seq [opt]
12713 template < template-parameter-list > */
12715 static void
12716 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12718 /* Check for `export'. */
12719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12721 /* Consume the `export' token. */
12722 cp_lexer_consume_token (parser->lexer);
12723 /* Warn that we do not support `export'. */
12724 warning (0, "keyword %<export%> not implemented, and will be ignored");
12727 cp_parser_template_declaration_after_export (parser, member_p);
12730 /* Parse a template-parameter-list.
12732 template-parameter-list:
12733 template-parameter
12734 template-parameter-list , template-parameter
12736 Returns a TREE_LIST. Each node represents a template parameter.
12737 The nodes are connected via their TREE_CHAINs. */
12739 static tree
12740 cp_parser_template_parameter_list (cp_parser* parser)
12742 tree parameter_list = NULL_TREE;
12744 begin_template_parm_list ();
12746 /* The loop below parses the template parms. We first need to know
12747 the total number of template parms to be able to compute proper
12748 canonical types of each dependent type. So after the loop, when
12749 we know the total number of template parms,
12750 end_template_parm_list computes the proper canonical types and
12751 fixes up the dependent types accordingly. */
12752 while (true)
12754 tree parameter;
12755 bool is_non_type;
12756 bool is_parameter_pack;
12757 location_t parm_loc;
12759 /* Parse the template-parameter. */
12760 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12761 parameter = cp_parser_template_parameter (parser,
12762 &is_non_type,
12763 &is_parameter_pack);
12764 /* Add it to the list. */
12765 if (parameter != error_mark_node)
12766 parameter_list = process_template_parm (parameter_list,
12767 parm_loc,
12768 parameter,
12769 is_non_type,
12770 is_parameter_pack);
12771 else
12773 tree err_parm = build_tree_list (parameter, parameter);
12774 parameter_list = chainon (parameter_list, err_parm);
12777 /* If the next token is not a `,', we're done. */
12778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12779 break;
12780 /* Otherwise, consume the `,' token. */
12781 cp_lexer_consume_token (parser->lexer);
12784 return end_template_parm_list (parameter_list);
12787 /* Parse a template-parameter.
12789 template-parameter:
12790 type-parameter
12791 parameter-declaration
12793 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12794 the parameter. The TREE_PURPOSE is the default value, if any.
12795 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12796 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12797 set to true iff this parameter is a parameter pack. */
12799 static tree
12800 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12801 bool *is_parameter_pack)
12803 cp_token *token;
12804 cp_parameter_declarator *parameter_declarator;
12805 cp_declarator *id_declarator;
12806 tree parm;
12808 /* Assume it is a type parameter or a template parameter. */
12809 *is_non_type = false;
12810 /* Assume it not a parameter pack. */
12811 *is_parameter_pack = false;
12812 /* Peek at the next token. */
12813 token = cp_lexer_peek_token (parser->lexer);
12814 /* If it is `class' or `template', we have a type-parameter. */
12815 if (token->keyword == RID_TEMPLATE)
12816 return cp_parser_type_parameter (parser, is_parameter_pack);
12817 /* If it is `class' or `typename' we do not know yet whether it is a
12818 type parameter or a non-type parameter. Consider:
12820 template <typename T, typename T::X X> ...
12824 template <class C, class D*> ...
12826 Here, the first parameter is a type parameter, and the second is
12827 a non-type parameter. We can tell by looking at the token after
12828 the identifier -- if it is a `,', `=', or `>' then we have a type
12829 parameter. */
12830 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12832 /* Peek at the token after `class' or `typename'. */
12833 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12834 /* If it's an ellipsis, we have a template type parameter
12835 pack. */
12836 if (token->type == CPP_ELLIPSIS)
12837 return cp_parser_type_parameter (parser, is_parameter_pack);
12838 /* If it's an identifier, skip it. */
12839 if (token->type == CPP_NAME)
12840 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12841 /* Now, see if the token looks like the end of a template
12842 parameter. */
12843 if (token->type == CPP_COMMA
12844 || token->type == CPP_EQ
12845 || token->type == CPP_GREATER)
12846 return cp_parser_type_parameter (parser, is_parameter_pack);
12849 /* Otherwise, it is a non-type parameter.
12851 [temp.param]
12853 When parsing a default template-argument for a non-type
12854 template-parameter, the first non-nested `>' is taken as the end
12855 of the template parameter-list rather than a greater-than
12856 operator. */
12857 *is_non_type = true;
12858 parameter_declarator
12859 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12860 /*parenthesized_p=*/NULL);
12862 /* If the parameter declaration is marked as a parameter pack, set
12863 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12864 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12865 grokdeclarator. */
12866 if (parameter_declarator
12867 && parameter_declarator->declarator
12868 && parameter_declarator->declarator->parameter_pack_p)
12870 *is_parameter_pack = true;
12871 parameter_declarator->declarator->parameter_pack_p = false;
12874 if (parameter_declarator
12875 && parameter_declarator->default_argument)
12877 /* Can happen in some cases of erroneous input (c++/34892). */
12878 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12879 /* Consume the `...' for better error recovery. */
12880 cp_lexer_consume_token (parser->lexer);
12882 /* If the next token is an ellipsis, and we don't already have it
12883 marked as a parameter pack, then we have a parameter pack (that
12884 has no declarator). */
12885 else if (!*is_parameter_pack
12886 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12887 && (declarator_can_be_parameter_pack
12888 (parameter_declarator->declarator)))
12890 /* Consume the `...'. */
12891 cp_lexer_consume_token (parser->lexer);
12892 maybe_warn_variadic_templates ();
12894 *is_parameter_pack = true;
12896 /* We might end up with a pack expansion as the type of the non-type
12897 template parameter, in which case this is a non-type template
12898 parameter pack. */
12899 else if (parameter_declarator
12900 && parameter_declarator->decl_specifiers.type
12901 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12903 *is_parameter_pack = true;
12904 parameter_declarator->decl_specifiers.type =
12905 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12908 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12910 /* Parameter packs cannot have default arguments. However, a
12911 user may try to do so, so we'll parse them and give an
12912 appropriate diagnostic here. */
12914 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12916 /* Find the name of the parameter pack. */
12917 id_declarator = parameter_declarator->declarator;
12918 while (id_declarator && id_declarator->kind != cdk_id)
12919 id_declarator = id_declarator->declarator;
12921 if (id_declarator && id_declarator->kind == cdk_id)
12922 error_at (start_token->location,
12923 "template parameter pack %qD cannot have a default argument",
12924 id_declarator->u.id.unqualified_name);
12925 else
12926 error_at (start_token->location,
12927 "template parameter pack cannot have a default argument");
12929 /* Parse the default argument, but throw away the result. */
12930 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12933 parm = grokdeclarator (parameter_declarator->declarator,
12934 &parameter_declarator->decl_specifiers,
12935 TPARM, /*initialized=*/0,
12936 /*attrlist=*/NULL);
12937 if (parm == error_mark_node)
12938 return error_mark_node;
12940 return build_tree_list (parameter_declarator->default_argument, parm);
12943 /* Parse a type-parameter.
12945 type-parameter:
12946 class identifier [opt]
12947 class identifier [opt] = type-id
12948 typename identifier [opt]
12949 typename identifier [opt] = type-id
12950 template < template-parameter-list > class identifier [opt]
12951 template < template-parameter-list > class identifier [opt]
12952 = id-expression
12954 GNU Extension (variadic templates):
12956 type-parameter:
12957 class ... identifier [opt]
12958 typename ... identifier [opt]
12960 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12961 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12962 the declaration of the parameter.
12964 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12966 static tree
12967 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12969 cp_token *token;
12970 tree parameter;
12972 /* Look for a keyword to tell us what kind of parameter this is. */
12973 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12974 if (!token)
12975 return error_mark_node;
12977 switch (token->keyword)
12979 case RID_CLASS:
12980 case RID_TYPENAME:
12982 tree identifier;
12983 tree default_argument;
12985 /* If the next token is an ellipsis, we have a template
12986 argument pack. */
12987 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12989 /* Consume the `...' token. */
12990 cp_lexer_consume_token (parser->lexer);
12991 maybe_warn_variadic_templates ();
12993 *is_parameter_pack = true;
12996 /* If the next token is an identifier, then it names the
12997 parameter. */
12998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12999 identifier = cp_parser_identifier (parser);
13000 else
13001 identifier = NULL_TREE;
13003 /* Create the parameter. */
13004 parameter = finish_template_type_parm (class_type_node, identifier);
13006 /* If the next token is an `=', we have a default argument. */
13007 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13009 /* Consume the `=' token. */
13010 cp_lexer_consume_token (parser->lexer);
13011 /* Parse the default-argument. */
13012 push_deferring_access_checks (dk_no_deferred);
13013 default_argument = cp_parser_type_id (parser);
13015 /* Template parameter packs cannot have default
13016 arguments. */
13017 if (*is_parameter_pack)
13019 if (identifier)
13020 error_at (token->location,
13021 "template parameter pack %qD cannot have a "
13022 "default argument", identifier);
13023 else
13024 error_at (token->location,
13025 "template parameter packs cannot have "
13026 "default arguments");
13027 default_argument = NULL_TREE;
13029 pop_deferring_access_checks ();
13031 else
13032 default_argument = NULL_TREE;
13034 /* Create the combined representation of the parameter and the
13035 default argument. */
13036 parameter = build_tree_list (default_argument, parameter);
13038 break;
13040 case RID_TEMPLATE:
13042 tree identifier;
13043 tree default_argument;
13045 /* Look for the `<'. */
13046 cp_parser_require (parser, CPP_LESS, RT_LESS);
13047 /* Parse the template-parameter-list. */
13048 cp_parser_template_parameter_list (parser);
13049 /* Look for the `>'. */
13050 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13051 /* Look for the `class' keyword. */
13052 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13053 /* If the next token is an ellipsis, we have a template
13054 argument pack. */
13055 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13057 /* Consume the `...' token. */
13058 cp_lexer_consume_token (parser->lexer);
13059 maybe_warn_variadic_templates ();
13061 *is_parameter_pack = true;
13063 /* If the next token is an `=', then there is a
13064 default-argument. If the next token is a `>', we are at
13065 the end of the parameter-list. If the next token is a `,',
13066 then we are at the end of this parameter. */
13067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13068 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13069 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13071 identifier = cp_parser_identifier (parser);
13072 /* Treat invalid names as if the parameter were nameless. */
13073 if (identifier == error_mark_node)
13074 identifier = NULL_TREE;
13076 else
13077 identifier = NULL_TREE;
13079 /* Create the template parameter. */
13080 parameter = finish_template_template_parm (class_type_node,
13081 identifier);
13083 /* If the next token is an `=', then there is a
13084 default-argument. */
13085 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13087 bool is_template;
13089 /* Consume the `='. */
13090 cp_lexer_consume_token (parser->lexer);
13091 /* Parse the id-expression. */
13092 push_deferring_access_checks (dk_no_deferred);
13093 /* save token before parsing the id-expression, for error
13094 reporting */
13095 token = cp_lexer_peek_token (parser->lexer);
13096 default_argument
13097 = cp_parser_id_expression (parser,
13098 /*template_keyword_p=*/false,
13099 /*check_dependency_p=*/true,
13100 /*template_p=*/&is_template,
13101 /*declarator_p=*/false,
13102 /*optional_p=*/false);
13103 if (TREE_CODE (default_argument) == TYPE_DECL)
13104 /* If the id-expression was a template-id that refers to
13105 a template-class, we already have the declaration here,
13106 so no further lookup is needed. */
13108 else
13109 /* Look up the name. */
13110 default_argument
13111 = cp_parser_lookup_name (parser, default_argument,
13112 none_type,
13113 /*is_template=*/is_template,
13114 /*is_namespace=*/false,
13115 /*check_dependency=*/true,
13116 /*ambiguous_decls=*/NULL,
13117 token->location);
13118 /* See if the default argument is valid. */
13119 default_argument
13120 = check_template_template_default_arg (default_argument);
13122 /* Template parameter packs cannot have default
13123 arguments. */
13124 if (*is_parameter_pack)
13126 if (identifier)
13127 error_at (token->location,
13128 "template parameter pack %qD cannot "
13129 "have a default argument",
13130 identifier);
13131 else
13132 error_at (token->location, "template parameter packs cannot "
13133 "have default arguments");
13134 default_argument = NULL_TREE;
13136 pop_deferring_access_checks ();
13138 else
13139 default_argument = NULL_TREE;
13141 /* Create the combined representation of the parameter and the
13142 default argument. */
13143 parameter = build_tree_list (default_argument, parameter);
13145 break;
13147 default:
13148 gcc_unreachable ();
13149 break;
13152 return parameter;
13155 /* Parse a template-id.
13157 template-id:
13158 template-name < template-argument-list [opt] >
13160 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13161 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13162 returned. Otherwise, if the template-name names a function, or set
13163 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13164 names a class, returns a TYPE_DECL for the specialization.
13166 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13167 uninstantiated templates. */
13169 static tree
13170 cp_parser_template_id (cp_parser *parser,
13171 bool template_keyword_p,
13172 bool check_dependency_p,
13173 enum tag_types tag_type,
13174 bool is_declaration)
13176 int i;
13177 tree templ;
13178 tree arguments;
13179 tree template_id;
13180 cp_token_position start_of_id = 0;
13181 deferred_access_check *chk;
13182 vec<deferred_access_check, va_gc> *access_check;
13183 cp_token *next_token = NULL, *next_token_2 = NULL;
13184 bool is_identifier;
13186 /* If the next token corresponds to a template-id, there is no need
13187 to reparse it. */
13188 next_token = cp_lexer_peek_token (parser->lexer);
13189 if (next_token->type == CPP_TEMPLATE_ID)
13191 struct tree_check *check_value;
13193 /* Get the stored value. */
13194 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13195 /* Perform any access checks that were deferred. */
13196 access_check = check_value->checks;
13197 if (access_check)
13199 FOR_EACH_VEC_ELT (*access_check, i, chk)
13200 perform_or_defer_access_check (chk->binfo,
13201 chk->decl,
13202 chk->diag_decl,
13203 tf_warning_or_error);
13205 /* Return the stored value. */
13206 return check_value->value;
13209 /* Avoid performing name lookup if there is no possibility of
13210 finding a template-id. */
13211 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13212 || (next_token->type == CPP_NAME
13213 && !cp_parser_nth_token_starts_template_argument_list_p
13214 (parser, 2)))
13216 cp_parser_error (parser, "expected template-id");
13217 return error_mark_node;
13220 /* Remember where the template-id starts. */
13221 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13222 start_of_id = cp_lexer_token_position (parser->lexer, false);
13224 push_deferring_access_checks (dk_deferred);
13226 /* Parse the template-name. */
13227 is_identifier = false;
13228 templ = cp_parser_template_name (parser, template_keyword_p,
13229 check_dependency_p,
13230 is_declaration,
13231 tag_type,
13232 &is_identifier);
13233 if (templ == error_mark_node || is_identifier)
13235 pop_deferring_access_checks ();
13236 return templ;
13239 /* If we find the sequence `[:' after a template-name, it's probably
13240 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13241 parse correctly the argument list. */
13242 next_token = cp_lexer_peek_token (parser->lexer);
13243 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13244 if (next_token->type == CPP_OPEN_SQUARE
13245 && next_token->flags & DIGRAPH
13246 && next_token_2->type == CPP_COLON
13247 && !(next_token_2->flags & PREV_WHITE))
13249 cp_parser_parse_tentatively (parser);
13250 /* Change `:' into `::'. */
13251 next_token_2->type = CPP_SCOPE;
13252 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13253 CPP_LESS. */
13254 cp_lexer_consume_token (parser->lexer);
13256 /* Parse the arguments. */
13257 arguments = cp_parser_enclosed_template_argument_list (parser);
13258 if (!cp_parser_parse_definitely (parser))
13260 /* If we couldn't parse an argument list, then we revert our changes
13261 and return simply an error. Maybe this is not a template-id
13262 after all. */
13263 next_token_2->type = CPP_COLON;
13264 cp_parser_error (parser, "expected %<<%>");
13265 pop_deferring_access_checks ();
13266 return error_mark_node;
13268 /* Otherwise, emit an error about the invalid digraph, but continue
13269 parsing because we got our argument list. */
13270 if (permerror (next_token->location,
13271 "%<<::%> cannot begin a template-argument list"))
13273 static bool hint = false;
13274 inform (next_token->location,
13275 "%<<:%> is an alternate spelling for %<[%>."
13276 " Insert whitespace between %<<%> and %<::%>");
13277 if (!hint && !flag_permissive)
13279 inform (next_token->location, "(if you use %<-fpermissive%> "
13280 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13281 "accept your code)");
13282 hint = true;
13286 else
13288 /* Look for the `<' that starts the template-argument-list. */
13289 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13291 pop_deferring_access_checks ();
13292 return error_mark_node;
13294 /* Parse the arguments. */
13295 arguments = cp_parser_enclosed_template_argument_list (parser);
13298 /* Build a representation of the specialization. */
13299 if (identifier_p (templ))
13300 template_id = build_min_nt_loc (next_token->location,
13301 TEMPLATE_ID_EXPR,
13302 templ, arguments);
13303 else if (DECL_TYPE_TEMPLATE_P (templ)
13304 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13306 bool entering_scope;
13307 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13308 template (rather than some instantiation thereof) only if
13309 is not nested within some other construct. For example, in
13310 "template <typename T> void f(T) { A<T>::", A<T> is just an
13311 instantiation of A. */
13312 entering_scope = (template_parm_scope_p ()
13313 && cp_lexer_next_token_is (parser->lexer,
13314 CPP_SCOPE));
13315 template_id
13316 = finish_template_type (templ, arguments, entering_scope);
13318 else
13320 /* If it's not a class-template or a template-template, it should be
13321 a function-template. */
13322 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13323 || TREE_CODE (templ) == OVERLOAD
13324 || BASELINK_P (templ)));
13326 template_id = lookup_template_function (templ, arguments);
13329 /* If parsing tentatively, replace the sequence of tokens that makes
13330 up the template-id with a CPP_TEMPLATE_ID token. That way,
13331 should we re-parse the token stream, we will not have to repeat
13332 the effort required to do the parse, nor will we issue duplicate
13333 error messages about problems during instantiation of the
13334 template. */
13335 if (start_of_id)
13337 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13339 /* Reset the contents of the START_OF_ID token. */
13340 token->type = CPP_TEMPLATE_ID;
13341 /* Retrieve any deferred checks. Do not pop this access checks yet
13342 so the memory will not be reclaimed during token replacing below. */
13343 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13344 token->u.tree_check_value->value = template_id;
13345 token->u.tree_check_value->checks = get_deferred_access_checks ();
13346 token->keyword = RID_MAX;
13348 /* Purge all subsequent tokens. */
13349 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13351 /* ??? Can we actually assume that, if template_id ==
13352 error_mark_node, we will have issued a diagnostic to the
13353 user, as opposed to simply marking the tentative parse as
13354 failed? */
13355 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13356 error_at (token->location, "parse error in template argument list");
13359 pop_to_parent_deferring_access_checks ();
13360 return template_id;
13363 /* Parse a template-name.
13365 template-name:
13366 identifier
13368 The standard should actually say:
13370 template-name:
13371 identifier
13372 operator-function-id
13374 A defect report has been filed about this issue.
13376 A conversion-function-id cannot be a template name because they cannot
13377 be part of a template-id. In fact, looking at this code:
13379 a.operator K<int>()
13381 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13382 It is impossible to call a templated conversion-function-id with an
13383 explicit argument list, since the only allowed template parameter is
13384 the type to which it is converting.
13386 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13387 `template' keyword, in a construction like:
13389 T::template f<3>()
13391 In that case `f' is taken to be a template-name, even though there
13392 is no way of knowing for sure.
13394 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13395 name refers to a set of overloaded functions, at least one of which
13396 is a template, or an IDENTIFIER_NODE with the name of the template,
13397 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13398 names are looked up inside uninstantiated templates. */
13400 static tree
13401 cp_parser_template_name (cp_parser* parser,
13402 bool template_keyword_p,
13403 bool check_dependency_p,
13404 bool is_declaration,
13405 enum tag_types tag_type,
13406 bool *is_identifier)
13408 tree identifier;
13409 tree decl;
13410 tree fns;
13411 cp_token *token = cp_lexer_peek_token (parser->lexer);
13413 /* If the next token is `operator', then we have either an
13414 operator-function-id or a conversion-function-id. */
13415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13417 /* We don't know whether we're looking at an
13418 operator-function-id or a conversion-function-id. */
13419 cp_parser_parse_tentatively (parser);
13420 /* Try an operator-function-id. */
13421 identifier = cp_parser_operator_function_id (parser);
13422 /* If that didn't work, try a conversion-function-id. */
13423 if (!cp_parser_parse_definitely (parser))
13425 cp_parser_error (parser, "expected template-name");
13426 return error_mark_node;
13429 /* Look for the identifier. */
13430 else
13431 identifier = cp_parser_identifier (parser);
13433 /* If we didn't find an identifier, we don't have a template-id. */
13434 if (identifier == error_mark_node)
13435 return error_mark_node;
13437 /* If the name immediately followed the `template' keyword, then it
13438 is a template-name. However, if the next token is not `<', then
13439 we do not treat it as a template-name, since it is not being used
13440 as part of a template-id. This enables us to handle constructs
13441 like:
13443 template <typename T> struct S { S(); };
13444 template <typename T> S<T>::S();
13446 correctly. We would treat `S' as a template -- if it were `S<T>'
13447 -- but we do not if there is no `<'. */
13449 if (processing_template_decl
13450 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13452 /* In a declaration, in a dependent context, we pretend that the
13453 "template" keyword was present in order to improve error
13454 recovery. For example, given:
13456 template <typename T> void f(T::X<int>);
13458 we want to treat "X<int>" as a template-id. */
13459 if (is_declaration
13460 && !template_keyword_p
13461 && parser->scope && TYPE_P (parser->scope)
13462 && check_dependency_p
13463 && dependent_scope_p (parser->scope)
13464 /* Do not do this for dtors (or ctors), since they never
13465 need the template keyword before their name. */
13466 && !constructor_name_p (identifier, parser->scope))
13468 cp_token_position start = 0;
13470 /* Explain what went wrong. */
13471 error_at (token->location, "non-template %qD used as template",
13472 identifier);
13473 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13474 parser->scope, identifier);
13475 /* If parsing tentatively, find the location of the "<" token. */
13476 if (cp_parser_simulate_error (parser))
13477 start = cp_lexer_token_position (parser->lexer, true);
13478 /* Parse the template arguments so that we can issue error
13479 messages about them. */
13480 cp_lexer_consume_token (parser->lexer);
13481 cp_parser_enclosed_template_argument_list (parser);
13482 /* Skip tokens until we find a good place from which to
13483 continue parsing. */
13484 cp_parser_skip_to_closing_parenthesis (parser,
13485 /*recovering=*/true,
13486 /*or_comma=*/true,
13487 /*consume_paren=*/false);
13488 /* If parsing tentatively, permanently remove the
13489 template argument list. That will prevent duplicate
13490 error messages from being issued about the missing
13491 "template" keyword. */
13492 if (start)
13493 cp_lexer_purge_tokens_after (parser->lexer, start);
13494 if (is_identifier)
13495 *is_identifier = true;
13496 return identifier;
13499 /* If the "template" keyword is present, then there is generally
13500 no point in doing name-lookup, so we just return IDENTIFIER.
13501 But, if the qualifying scope is non-dependent then we can
13502 (and must) do name-lookup normally. */
13503 if (template_keyword_p
13504 && (!parser->scope
13505 || (TYPE_P (parser->scope)
13506 && dependent_type_p (parser->scope))))
13507 return identifier;
13510 /* Look up the name. */
13511 decl = cp_parser_lookup_name (parser, identifier,
13512 tag_type,
13513 /*is_template=*/true,
13514 /*is_namespace=*/false,
13515 check_dependency_p,
13516 /*ambiguous_decls=*/NULL,
13517 token->location);
13519 /* If DECL is a template, then the name was a template-name. */
13520 if (TREE_CODE (decl) == TEMPLATE_DECL)
13522 else
13524 tree fn = NULL_TREE;
13526 /* The standard does not explicitly indicate whether a name that
13527 names a set of overloaded declarations, some of which are
13528 templates, is a template-name. However, such a name should
13529 be a template-name; otherwise, there is no way to form a
13530 template-id for the overloaded templates. */
13531 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13532 if (TREE_CODE (fns) == OVERLOAD)
13533 for (fn = fns; fn; fn = OVL_NEXT (fn))
13534 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13535 break;
13537 if (!fn)
13539 /* The name does not name a template. */
13540 cp_parser_error (parser, "expected template-name");
13541 return error_mark_node;
13545 /* If DECL is dependent, and refers to a function, then just return
13546 its name; we will look it up again during template instantiation. */
13547 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13549 tree scope = ovl_scope (decl);
13550 if (TYPE_P (scope) && dependent_type_p (scope))
13551 return identifier;
13554 return decl;
13557 /* Parse a template-argument-list.
13559 template-argument-list:
13560 template-argument ... [opt]
13561 template-argument-list , template-argument ... [opt]
13563 Returns a TREE_VEC containing the arguments. */
13565 static tree
13566 cp_parser_template_argument_list (cp_parser* parser)
13568 tree fixed_args[10];
13569 unsigned n_args = 0;
13570 unsigned alloced = 10;
13571 tree *arg_ary = fixed_args;
13572 tree vec;
13573 bool saved_in_template_argument_list_p;
13574 bool saved_ice_p;
13575 bool saved_non_ice_p;
13577 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13578 parser->in_template_argument_list_p = true;
13579 /* Even if the template-id appears in an integral
13580 constant-expression, the contents of the argument list do
13581 not. */
13582 saved_ice_p = parser->integral_constant_expression_p;
13583 parser->integral_constant_expression_p = false;
13584 saved_non_ice_p = parser->non_integral_constant_expression_p;
13585 parser->non_integral_constant_expression_p = false;
13587 /* Parse the arguments. */
13590 tree argument;
13592 if (n_args)
13593 /* Consume the comma. */
13594 cp_lexer_consume_token (parser->lexer);
13596 /* Parse the template-argument. */
13597 argument = cp_parser_template_argument (parser);
13599 /* If the next token is an ellipsis, we're expanding a template
13600 argument pack. */
13601 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13603 if (argument == error_mark_node)
13605 cp_token *token = cp_lexer_peek_token (parser->lexer);
13606 error_at (token->location,
13607 "expected parameter pack before %<...%>");
13609 /* Consume the `...' token. */
13610 cp_lexer_consume_token (parser->lexer);
13612 /* Make the argument into a TYPE_PACK_EXPANSION or
13613 EXPR_PACK_EXPANSION. */
13614 argument = make_pack_expansion (argument);
13617 if (n_args == alloced)
13619 alloced *= 2;
13621 if (arg_ary == fixed_args)
13623 arg_ary = XNEWVEC (tree, alloced);
13624 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13626 else
13627 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13629 arg_ary[n_args++] = argument;
13631 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13633 vec = make_tree_vec (n_args);
13635 while (n_args--)
13636 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13638 if (arg_ary != fixed_args)
13639 free (arg_ary);
13640 parser->non_integral_constant_expression_p = saved_non_ice_p;
13641 parser->integral_constant_expression_p = saved_ice_p;
13642 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13643 #ifdef ENABLE_CHECKING
13644 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13645 #endif
13646 return vec;
13649 /* Parse a template-argument.
13651 template-argument:
13652 assignment-expression
13653 type-id
13654 id-expression
13656 The representation is that of an assignment-expression, type-id, or
13657 id-expression -- except that the qualified id-expression is
13658 evaluated, so that the value returned is either a DECL or an
13659 OVERLOAD.
13661 Although the standard says "assignment-expression", it forbids
13662 throw-expressions or assignments in the template argument.
13663 Therefore, we use "conditional-expression" instead. */
13665 static tree
13666 cp_parser_template_argument (cp_parser* parser)
13668 tree argument;
13669 bool template_p;
13670 bool address_p;
13671 bool maybe_type_id = false;
13672 cp_token *token = NULL, *argument_start_token = NULL;
13673 location_t loc = 0;
13674 cp_id_kind idk;
13676 /* There's really no way to know what we're looking at, so we just
13677 try each alternative in order.
13679 [temp.arg]
13681 In a template-argument, an ambiguity between a type-id and an
13682 expression is resolved to a type-id, regardless of the form of
13683 the corresponding template-parameter.
13685 Therefore, we try a type-id first. */
13686 cp_parser_parse_tentatively (parser);
13687 argument = cp_parser_template_type_arg (parser);
13688 /* If there was no error parsing the type-id but the next token is a
13689 '>>', our behavior depends on which dialect of C++ we're
13690 parsing. In C++98, we probably found a typo for '> >'. But there
13691 are type-id which are also valid expressions. For instance:
13693 struct X { int operator >> (int); };
13694 template <int V> struct Foo {};
13695 Foo<X () >> 5> r;
13697 Here 'X()' is a valid type-id of a function type, but the user just
13698 wanted to write the expression "X() >> 5". Thus, we remember that we
13699 found a valid type-id, but we still try to parse the argument as an
13700 expression to see what happens.
13702 In C++0x, the '>>' will be considered two separate '>'
13703 tokens. */
13704 if (!cp_parser_error_occurred (parser)
13705 && cxx_dialect == cxx98
13706 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13708 maybe_type_id = true;
13709 cp_parser_abort_tentative_parse (parser);
13711 else
13713 /* If the next token isn't a `,' or a `>', then this argument wasn't
13714 really finished. This means that the argument is not a valid
13715 type-id. */
13716 if (!cp_parser_next_token_ends_template_argument_p (parser))
13717 cp_parser_error (parser, "expected template-argument");
13718 /* If that worked, we're done. */
13719 if (cp_parser_parse_definitely (parser))
13720 return argument;
13722 /* We're still not sure what the argument will be. */
13723 cp_parser_parse_tentatively (parser);
13724 /* Try a template. */
13725 argument_start_token = cp_lexer_peek_token (parser->lexer);
13726 argument = cp_parser_id_expression (parser,
13727 /*template_keyword_p=*/false,
13728 /*check_dependency_p=*/true,
13729 &template_p,
13730 /*declarator_p=*/false,
13731 /*optional_p=*/false);
13732 /* If the next token isn't a `,' or a `>', then this argument wasn't
13733 really finished. */
13734 if (!cp_parser_next_token_ends_template_argument_p (parser))
13735 cp_parser_error (parser, "expected template-argument");
13736 if (!cp_parser_error_occurred (parser))
13738 /* Figure out what is being referred to. If the id-expression
13739 was for a class template specialization, then we will have a
13740 TYPE_DECL at this point. There is no need to do name lookup
13741 at this point in that case. */
13742 if (TREE_CODE (argument) != TYPE_DECL)
13743 argument = cp_parser_lookup_name (parser, argument,
13744 none_type,
13745 /*is_template=*/template_p,
13746 /*is_namespace=*/false,
13747 /*check_dependency=*/true,
13748 /*ambiguous_decls=*/NULL,
13749 argument_start_token->location);
13750 if (TREE_CODE (argument) != TEMPLATE_DECL
13751 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13752 cp_parser_error (parser, "expected template-name");
13754 if (cp_parser_parse_definitely (parser))
13755 return argument;
13756 /* It must be a non-type argument. There permitted cases are given
13757 in [temp.arg.nontype]:
13759 -- an integral constant-expression of integral or enumeration
13760 type; or
13762 -- the name of a non-type template-parameter; or
13764 -- the name of an object or function with external linkage...
13766 -- the address of an object or function with external linkage...
13768 -- a pointer to member... */
13769 /* Look for a non-type template parameter. */
13770 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13772 cp_parser_parse_tentatively (parser);
13773 argument = cp_parser_primary_expression (parser,
13774 /*address_p=*/false,
13775 /*cast_p=*/false,
13776 /*template_arg_p=*/true,
13777 &idk);
13778 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13779 || !cp_parser_next_token_ends_template_argument_p (parser))
13780 cp_parser_simulate_error (parser);
13781 if (cp_parser_parse_definitely (parser))
13782 return argument;
13785 /* If the next token is "&", the argument must be the address of an
13786 object or function with external linkage. */
13787 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13788 if (address_p)
13790 loc = cp_lexer_peek_token (parser->lexer)->location;
13791 cp_lexer_consume_token (parser->lexer);
13793 /* See if we might have an id-expression. */
13794 token = cp_lexer_peek_token (parser->lexer);
13795 if (token->type == CPP_NAME
13796 || token->keyword == RID_OPERATOR
13797 || token->type == CPP_SCOPE
13798 || token->type == CPP_TEMPLATE_ID
13799 || token->type == CPP_NESTED_NAME_SPECIFIER)
13801 cp_parser_parse_tentatively (parser);
13802 argument = cp_parser_primary_expression (parser,
13803 address_p,
13804 /*cast_p=*/false,
13805 /*template_arg_p=*/true,
13806 &idk);
13807 if (cp_parser_error_occurred (parser)
13808 || !cp_parser_next_token_ends_template_argument_p (parser))
13809 cp_parser_abort_tentative_parse (parser);
13810 else
13812 tree probe;
13814 if (INDIRECT_REF_P (argument))
13816 gcc_assert (REFERENCE_REF_P (argument));
13817 argument = TREE_OPERAND (argument, 0);
13820 /* If we're in a template, we represent a qualified-id referring
13821 to a static data member as a SCOPE_REF even if the scope isn't
13822 dependent so that we can check access control later. */
13823 probe = argument;
13824 if (TREE_CODE (probe) == SCOPE_REF)
13825 probe = TREE_OPERAND (probe, 1);
13826 if (VAR_P (probe))
13828 /* A variable without external linkage might still be a
13829 valid constant-expression, so no error is issued here
13830 if the external-linkage check fails. */
13831 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13832 cp_parser_simulate_error (parser);
13834 else if (is_overloaded_fn (argument))
13835 /* All overloaded functions are allowed; if the external
13836 linkage test does not pass, an error will be issued
13837 later. */
13839 else if (address_p
13840 && (TREE_CODE (argument) == OFFSET_REF
13841 || TREE_CODE (argument) == SCOPE_REF))
13842 /* A pointer-to-member. */
13844 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13846 else
13847 cp_parser_simulate_error (parser);
13849 if (cp_parser_parse_definitely (parser))
13851 if (address_p)
13852 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13853 tf_warning_or_error);
13854 return argument;
13858 /* If the argument started with "&", there are no other valid
13859 alternatives at this point. */
13860 if (address_p)
13862 cp_parser_error (parser, "invalid non-type template argument");
13863 return error_mark_node;
13866 /* If the argument wasn't successfully parsed as a type-id followed
13867 by '>>', the argument can only be a constant expression now.
13868 Otherwise, we try parsing the constant-expression tentatively,
13869 because the argument could really be a type-id. */
13870 if (maybe_type_id)
13871 cp_parser_parse_tentatively (parser);
13872 argument = cp_parser_constant_expression (parser,
13873 /*allow_non_constant_p=*/false,
13874 /*non_constant_p=*/NULL);
13875 if (!maybe_type_id)
13876 return argument;
13877 if (!cp_parser_next_token_ends_template_argument_p (parser))
13878 cp_parser_error (parser, "expected template-argument");
13879 if (cp_parser_parse_definitely (parser))
13880 return argument;
13881 /* We did our best to parse the argument as a non type-id, but that
13882 was the only alternative that matched (albeit with a '>' after
13883 it). We can assume it's just a typo from the user, and a
13884 diagnostic will then be issued. */
13885 return cp_parser_template_type_arg (parser);
13888 /* Parse an explicit-instantiation.
13890 explicit-instantiation:
13891 template declaration
13893 Although the standard says `declaration', what it really means is:
13895 explicit-instantiation:
13896 template decl-specifier-seq [opt] declarator [opt] ;
13898 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13899 supposed to be allowed. A defect report has been filed about this
13900 issue.
13902 GNU Extension:
13904 explicit-instantiation:
13905 storage-class-specifier template
13906 decl-specifier-seq [opt] declarator [opt] ;
13907 function-specifier template
13908 decl-specifier-seq [opt] declarator [opt] ; */
13910 static void
13911 cp_parser_explicit_instantiation (cp_parser* parser)
13913 int declares_class_or_enum;
13914 cp_decl_specifier_seq decl_specifiers;
13915 tree extension_specifier = NULL_TREE;
13917 timevar_push (TV_TEMPLATE_INST);
13919 /* Look for an (optional) storage-class-specifier or
13920 function-specifier. */
13921 if (cp_parser_allow_gnu_extensions_p (parser))
13923 extension_specifier
13924 = cp_parser_storage_class_specifier_opt (parser);
13925 if (!extension_specifier)
13926 extension_specifier
13927 = cp_parser_function_specifier_opt (parser,
13928 /*decl_specs=*/NULL);
13931 /* Look for the `template' keyword. */
13932 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13933 /* Let the front end know that we are processing an explicit
13934 instantiation. */
13935 begin_explicit_instantiation ();
13936 /* [temp.explicit] says that we are supposed to ignore access
13937 control while processing explicit instantiation directives. */
13938 push_deferring_access_checks (dk_no_check);
13939 /* Parse a decl-specifier-seq. */
13940 cp_parser_decl_specifier_seq (parser,
13941 CP_PARSER_FLAGS_OPTIONAL,
13942 &decl_specifiers,
13943 &declares_class_or_enum);
13944 /* If there was exactly one decl-specifier, and it declared a class,
13945 and there's no declarator, then we have an explicit type
13946 instantiation. */
13947 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13949 tree type;
13951 type = check_tag_decl (&decl_specifiers,
13952 /*explicit_type_instantiation_p=*/true);
13953 /* Turn access control back on for names used during
13954 template instantiation. */
13955 pop_deferring_access_checks ();
13956 if (type)
13957 do_type_instantiation (type, extension_specifier,
13958 /*complain=*/tf_error);
13960 else
13962 cp_declarator *declarator;
13963 tree decl;
13965 /* Parse the declarator. */
13966 declarator
13967 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13968 /*ctor_dtor_or_conv_p=*/NULL,
13969 /*parenthesized_p=*/NULL,
13970 /*member_p=*/false);
13971 if (declares_class_or_enum & 2)
13972 cp_parser_check_for_definition_in_return_type (declarator,
13973 decl_specifiers.type,
13974 decl_specifiers.locations[ds_type_spec]);
13975 if (declarator != cp_error_declarator)
13977 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13978 permerror (decl_specifiers.locations[ds_inline],
13979 "explicit instantiation shall not use"
13980 " %<inline%> specifier");
13981 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13982 permerror (decl_specifiers.locations[ds_constexpr],
13983 "explicit instantiation shall not use"
13984 " %<constexpr%> specifier");
13986 decl = grokdeclarator (declarator, &decl_specifiers,
13987 NORMAL, 0, &decl_specifiers.attributes);
13988 /* Turn access control back on for names used during
13989 template instantiation. */
13990 pop_deferring_access_checks ();
13991 /* Do the explicit instantiation. */
13992 do_decl_instantiation (decl, extension_specifier);
13994 else
13996 pop_deferring_access_checks ();
13997 /* Skip the body of the explicit instantiation. */
13998 cp_parser_skip_to_end_of_statement (parser);
14001 /* We're done with the instantiation. */
14002 end_explicit_instantiation ();
14004 cp_parser_consume_semicolon_at_end_of_statement (parser);
14006 timevar_pop (TV_TEMPLATE_INST);
14009 /* Parse an explicit-specialization.
14011 explicit-specialization:
14012 template < > declaration
14014 Although the standard says `declaration', what it really means is:
14016 explicit-specialization:
14017 template <> decl-specifier [opt] init-declarator [opt] ;
14018 template <> function-definition
14019 template <> explicit-specialization
14020 template <> template-declaration */
14022 static void
14023 cp_parser_explicit_specialization (cp_parser* parser)
14025 bool need_lang_pop;
14026 cp_token *token = cp_lexer_peek_token (parser->lexer);
14028 /* Look for the `template' keyword. */
14029 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14030 /* Look for the `<'. */
14031 cp_parser_require (parser, CPP_LESS, RT_LESS);
14032 /* Look for the `>'. */
14033 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14034 /* We have processed another parameter list. */
14035 ++parser->num_template_parameter_lists;
14036 /* [temp]
14038 A template ... explicit specialization ... shall not have C
14039 linkage. */
14040 if (current_lang_name == lang_name_c)
14042 error_at (token->location, "template specialization with C linkage");
14043 /* Give it C++ linkage to avoid confusing other parts of the
14044 front end. */
14045 push_lang_context (lang_name_cplusplus);
14046 need_lang_pop = true;
14048 else
14049 need_lang_pop = false;
14050 /* Let the front end know that we are beginning a specialization. */
14051 if (!begin_specialization ())
14053 end_specialization ();
14054 return;
14057 /* If the next keyword is `template', we need to figure out whether
14058 or not we're looking a template-declaration. */
14059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14061 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14062 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14063 cp_parser_template_declaration_after_export (parser,
14064 /*member_p=*/false);
14065 else
14066 cp_parser_explicit_specialization (parser);
14068 else
14069 /* Parse the dependent declaration. */
14070 cp_parser_single_declaration (parser,
14071 /*checks=*/NULL,
14072 /*member_p=*/false,
14073 /*explicit_specialization_p=*/true,
14074 /*friend_p=*/NULL);
14075 /* We're done with the specialization. */
14076 end_specialization ();
14077 /* For the erroneous case of a template with C linkage, we pushed an
14078 implicit C++ linkage scope; exit that scope now. */
14079 if (need_lang_pop)
14080 pop_lang_context ();
14081 /* We're done with this parameter list. */
14082 --parser->num_template_parameter_lists;
14085 /* Parse a type-specifier.
14087 type-specifier:
14088 simple-type-specifier
14089 class-specifier
14090 enum-specifier
14091 elaborated-type-specifier
14092 cv-qualifier
14094 GNU Extension:
14096 type-specifier:
14097 __complex__
14099 Returns a representation of the type-specifier. For a
14100 class-specifier, enum-specifier, or elaborated-type-specifier, a
14101 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14103 The parser flags FLAGS is used to control type-specifier parsing.
14105 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14106 in a decl-specifier-seq.
14108 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14109 class-specifier, enum-specifier, or elaborated-type-specifier, then
14110 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14111 if a type is declared; 2 if it is defined. Otherwise, it is set to
14112 zero.
14114 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14115 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14116 is set to FALSE. */
14118 static tree
14119 cp_parser_type_specifier (cp_parser* parser,
14120 cp_parser_flags flags,
14121 cp_decl_specifier_seq *decl_specs,
14122 bool is_declaration,
14123 int* declares_class_or_enum,
14124 bool* is_cv_qualifier)
14126 tree type_spec = NULL_TREE;
14127 cp_token *token;
14128 enum rid keyword;
14129 cp_decl_spec ds = ds_last;
14131 /* Assume this type-specifier does not declare a new type. */
14132 if (declares_class_or_enum)
14133 *declares_class_or_enum = 0;
14134 /* And that it does not specify a cv-qualifier. */
14135 if (is_cv_qualifier)
14136 *is_cv_qualifier = false;
14137 /* Peek at the next token. */
14138 token = cp_lexer_peek_token (parser->lexer);
14140 /* If we're looking at a keyword, we can use that to guide the
14141 production we choose. */
14142 keyword = token->keyword;
14143 switch (keyword)
14145 case RID_ENUM:
14146 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14147 goto elaborated_type_specifier;
14149 /* Look for the enum-specifier. */
14150 type_spec = cp_parser_enum_specifier (parser);
14151 /* If that worked, we're done. */
14152 if (type_spec)
14154 if (declares_class_or_enum)
14155 *declares_class_or_enum = 2;
14156 if (decl_specs)
14157 cp_parser_set_decl_spec_type (decl_specs,
14158 type_spec,
14159 token,
14160 /*type_definition_p=*/true);
14161 return type_spec;
14163 else
14164 goto elaborated_type_specifier;
14166 /* Any of these indicate either a class-specifier, or an
14167 elaborated-type-specifier. */
14168 case RID_CLASS:
14169 case RID_STRUCT:
14170 case RID_UNION:
14171 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14172 goto elaborated_type_specifier;
14174 /* Parse tentatively so that we can back up if we don't find a
14175 class-specifier. */
14176 cp_parser_parse_tentatively (parser);
14177 /* Look for the class-specifier. */
14178 type_spec = cp_parser_class_specifier (parser);
14179 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14180 /* If that worked, we're done. */
14181 if (cp_parser_parse_definitely (parser))
14183 if (declares_class_or_enum)
14184 *declares_class_or_enum = 2;
14185 if (decl_specs)
14186 cp_parser_set_decl_spec_type (decl_specs,
14187 type_spec,
14188 token,
14189 /*type_definition_p=*/true);
14190 return type_spec;
14193 /* Fall through. */
14194 elaborated_type_specifier:
14195 /* We're declaring (not defining) a class or enum. */
14196 if (declares_class_or_enum)
14197 *declares_class_or_enum = 1;
14199 /* Fall through. */
14200 case RID_TYPENAME:
14201 /* Look for an elaborated-type-specifier. */
14202 type_spec
14203 = (cp_parser_elaborated_type_specifier
14204 (parser,
14205 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14206 is_declaration));
14207 if (decl_specs)
14208 cp_parser_set_decl_spec_type (decl_specs,
14209 type_spec,
14210 token,
14211 /*type_definition_p=*/false);
14212 return type_spec;
14214 case RID_CONST:
14215 ds = ds_const;
14216 if (is_cv_qualifier)
14217 *is_cv_qualifier = true;
14218 break;
14220 case RID_VOLATILE:
14221 ds = ds_volatile;
14222 if (is_cv_qualifier)
14223 *is_cv_qualifier = true;
14224 break;
14226 case RID_RESTRICT:
14227 ds = ds_restrict;
14228 if (is_cv_qualifier)
14229 *is_cv_qualifier = true;
14230 break;
14232 case RID_COMPLEX:
14233 /* The `__complex__' keyword is a GNU extension. */
14234 ds = ds_complex;
14235 break;
14237 default:
14238 break;
14241 /* Handle simple keywords. */
14242 if (ds != ds_last)
14244 if (decl_specs)
14246 set_and_check_decl_spec_loc (decl_specs, ds, token);
14247 decl_specs->any_specifiers_p = true;
14249 return cp_lexer_consume_token (parser->lexer)->u.value;
14252 /* If we do not already have a type-specifier, assume we are looking
14253 at a simple-type-specifier. */
14254 type_spec = cp_parser_simple_type_specifier (parser,
14255 decl_specs,
14256 flags);
14258 /* If we didn't find a type-specifier, and a type-specifier was not
14259 optional in this context, issue an error message. */
14260 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14262 cp_parser_error (parser, "expected type specifier");
14263 return error_mark_node;
14266 return type_spec;
14269 /* Parse a simple-type-specifier.
14271 simple-type-specifier:
14272 :: [opt] nested-name-specifier [opt] type-name
14273 :: [opt] nested-name-specifier template template-id
14274 char
14275 wchar_t
14276 bool
14277 short
14279 long
14280 signed
14281 unsigned
14282 float
14283 double
14284 void
14286 C++0x Extension:
14288 simple-type-specifier:
14289 auto
14290 decltype ( expression )
14291 char16_t
14292 char32_t
14293 __underlying_type ( type-id )
14295 GNU Extension:
14297 simple-type-specifier:
14298 __int128
14299 __typeof__ unary-expression
14300 __typeof__ ( type-id )
14301 __typeof__ ( type-id ) { initializer-list , [opt] }
14303 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14304 appropriately updated. */
14306 static tree
14307 cp_parser_simple_type_specifier (cp_parser* parser,
14308 cp_decl_specifier_seq *decl_specs,
14309 cp_parser_flags flags)
14311 tree type = NULL_TREE;
14312 cp_token *token;
14314 /* Peek at the next token. */
14315 token = cp_lexer_peek_token (parser->lexer);
14317 /* If we're looking at a keyword, things are easy. */
14318 switch (token->keyword)
14320 case RID_CHAR:
14321 if (decl_specs)
14322 decl_specs->explicit_char_p = true;
14323 type = char_type_node;
14324 break;
14325 case RID_CHAR16:
14326 type = char16_type_node;
14327 break;
14328 case RID_CHAR32:
14329 type = char32_type_node;
14330 break;
14331 case RID_WCHAR:
14332 type = wchar_type_node;
14333 break;
14334 case RID_BOOL:
14335 type = boolean_type_node;
14336 break;
14337 case RID_SHORT:
14338 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14339 type = short_integer_type_node;
14340 break;
14341 case RID_INT:
14342 if (decl_specs)
14343 decl_specs->explicit_int_p = true;
14344 type = integer_type_node;
14345 break;
14346 case RID_INT128:
14347 if (!int128_integer_type_node)
14348 break;
14349 if (decl_specs)
14350 decl_specs->explicit_int128_p = true;
14351 type = int128_integer_type_node;
14352 break;
14353 case RID_LONG:
14354 if (decl_specs)
14355 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14356 type = long_integer_type_node;
14357 break;
14358 case RID_SIGNED:
14359 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14360 type = integer_type_node;
14361 break;
14362 case RID_UNSIGNED:
14363 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14364 type = unsigned_type_node;
14365 break;
14366 case RID_FLOAT:
14367 type = float_type_node;
14368 break;
14369 case RID_DOUBLE:
14370 type = double_type_node;
14371 break;
14372 case RID_VOID:
14373 type = void_type_node;
14374 break;
14376 case RID_AUTO:
14377 maybe_warn_cpp0x (CPP0X_AUTO);
14378 type = make_auto ();
14379 break;
14381 case RID_DECLTYPE:
14382 /* Since DR 743, decltype can either be a simple-type-specifier by
14383 itself or begin a nested-name-specifier. Parsing it will replace
14384 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14385 handling below decide what to do. */
14386 cp_parser_decltype (parser);
14387 cp_lexer_set_token_position (parser->lexer, token);
14388 break;
14390 case RID_TYPEOF:
14391 /* Consume the `typeof' token. */
14392 cp_lexer_consume_token (parser->lexer);
14393 /* Parse the operand to `typeof'. */
14394 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14395 /* If it is not already a TYPE, take its type. */
14396 if (!TYPE_P (type))
14397 type = finish_typeof (type);
14399 if (decl_specs)
14400 cp_parser_set_decl_spec_type (decl_specs, type,
14401 token,
14402 /*type_definition_p=*/false);
14404 return type;
14406 case RID_UNDERLYING_TYPE:
14407 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14408 if (decl_specs)
14409 cp_parser_set_decl_spec_type (decl_specs, type,
14410 token,
14411 /*type_definition_p=*/false);
14413 return type;
14415 case RID_BASES:
14416 case RID_DIRECT_BASES:
14417 type = cp_parser_trait_expr (parser, token->keyword);
14418 if (decl_specs)
14419 cp_parser_set_decl_spec_type (decl_specs, type,
14420 token,
14421 /*type_definition_p=*/false);
14422 return type;
14423 default:
14424 break;
14427 /* If token is an already-parsed decltype not followed by ::,
14428 it's a simple-type-specifier. */
14429 if (token->type == CPP_DECLTYPE
14430 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14432 type = token->u.value;
14433 if (decl_specs)
14434 cp_parser_set_decl_spec_type (decl_specs, type,
14435 token,
14436 /*type_definition_p=*/false);
14437 cp_lexer_consume_token (parser->lexer);
14438 return type;
14441 /* If the type-specifier was for a built-in type, we're done. */
14442 if (type)
14444 /* Record the type. */
14445 if (decl_specs
14446 && (token->keyword != RID_SIGNED
14447 && token->keyword != RID_UNSIGNED
14448 && token->keyword != RID_SHORT
14449 && token->keyword != RID_LONG))
14450 cp_parser_set_decl_spec_type (decl_specs,
14451 type,
14452 token,
14453 /*type_definition_p=*/false);
14454 if (decl_specs)
14455 decl_specs->any_specifiers_p = true;
14457 /* Consume the token. */
14458 cp_lexer_consume_token (parser->lexer);
14460 /* There is no valid C++ program where a non-template type is
14461 followed by a "<". That usually indicates that the user thought
14462 that the type was a template. */
14463 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14464 token->location);
14466 return TYPE_NAME (type);
14469 /* The type-specifier must be a user-defined type. */
14470 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14472 bool qualified_p;
14473 bool global_p;
14475 /* Don't gobble tokens or issue error messages if this is an
14476 optional type-specifier. */
14477 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14478 cp_parser_parse_tentatively (parser);
14480 /* Look for the optional `::' operator. */
14481 global_p
14482 = (cp_parser_global_scope_opt (parser,
14483 /*current_scope_valid_p=*/false)
14484 != NULL_TREE);
14485 /* Look for the nested-name specifier. */
14486 qualified_p
14487 = (cp_parser_nested_name_specifier_opt (parser,
14488 /*typename_keyword_p=*/false,
14489 /*check_dependency_p=*/true,
14490 /*type_p=*/false,
14491 /*is_declaration=*/false)
14492 != NULL_TREE);
14493 token = cp_lexer_peek_token (parser->lexer);
14494 /* If we have seen a nested-name-specifier, and the next token
14495 is `template', then we are using the template-id production. */
14496 if (parser->scope
14497 && cp_parser_optional_template_keyword (parser))
14499 /* Look for the template-id. */
14500 type = cp_parser_template_id (parser,
14501 /*template_keyword_p=*/true,
14502 /*check_dependency_p=*/true,
14503 none_type,
14504 /*is_declaration=*/false);
14505 /* If the template-id did not name a type, we are out of
14506 luck. */
14507 if (TREE_CODE (type) != TYPE_DECL)
14509 cp_parser_error (parser, "expected template-id for type");
14510 type = NULL_TREE;
14513 /* Otherwise, look for a type-name. */
14514 else
14515 type = cp_parser_type_name (parser);
14516 /* Keep track of all name-lookups performed in class scopes. */
14517 if (type
14518 && !global_p
14519 && !qualified_p
14520 && TREE_CODE (type) == TYPE_DECL
14521 && identifier_p (DECL_NAME (type)))
14522 maybe_note_name_used_in_class (DECL_NAME (type), type);
14523 /* If it didn't work out, we don't have a TYPE. */
14524 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14525 && !cp_parser_parse_definitely (parser))
14526 type = NULL_TREE;
14527 if (type && decl_specs)
14528 cp_parser_set_decl_spec_type (decl_specs, type,
14529 token,
14530 /*type_definition_p=*/false);
14533 /* If we didn't get a type-name, issue an error message. */
14534 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14536 cp_parser_error (parser, "expected type-name");
14537 return error_mark_node;
14540 if (type && type != error_mark_node)
14542 /* See if TYPE is an Objective-C type, and if so, parse and
14543 accept any protocol references following it. Do this before
14544 the cp_parser_check_for_invalid_template_id() call, because
14545 Objective-C types can be followed by '<...>' which would
14546 enclose protocol names rather than template arguments, and so
14547 everything is fine. */
14548 if (c_dialect_objc () && !parser->scope
14549 && (objc_is_id (type) || objc_is_class_name (type)))
14551 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14552 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14554 /* Clobber the "unqualified" type previously entered into
14555 DECL_SPECS with the new, improved protocol-qualified version. */
14556 if (decl_specs)
14557 decl_specs->type = qual_type;
14559 return qual_type;
14562 /* There is no valid C++ program where a non-template type is
14563 followed by a "<". That usually indicates that the user
14564 thought that the type was a template. */
14565 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14566 none_type,
14567 token->location);
14570 return type;
14573 /* Parse a type-name.
14575 type-name:
14576 class-name
14577 enum-name
14578 typedef-name
14579 simple-template-id [in c++0x]
14581 enum-name:
14582 identifier
14584 typedef-name:
14585 identifier
14587 Returns a TYPE_DECL for the type. */
14589 static tree
14590 cp_parser_type_name (cp_parser* parser)
14592 tree type_decl;
14594 /* We can't know yet whether it is a class-name or not. */
14595 cp_parser_parse_tentatively (parser);
14596 /* Try a class-name. */
14597 type_decl = cp_parser_class_name (parser,
14598 /*typename_keyword_p=*/false,
14599 /*template_keyword_p=*/false,
14600 none_type,
14601 /*check_dependency_p=*/true,
14602 /*class_head_p=*/false,
14603 /*is_declaration=*/false);
14604 /* If it's not a class-name, keep looking. */
14605 if (!cp_parser_parse_definitely (parser))
14607 if (cxx_dialect < cxx11)
14608 /* It must be a typedef-name or an enum-name. */
14609 return cp_parser_nonclass_name (parser);
14611 cp_parser_parse_tentatively (parser);
14612 /* It is either a simple-template-id representing an
14613 instantiation of an alias template... */
14614 type_decl = cp_parser_template_id (parser,
14615 /*template_keyword_p=*/false,
14616 /*check_dependency_p=*/false,
14617 none_type,
14618 /*is_declaration=*/false);
14619 /* Note that this must be an instantiation of an alias template
14620 because [temp.names]/6 says:
14622 A template-id that names an alias template specialization
14623 is a type-name.
14625 Whereas [temp.names]/7 says:
14627 A simple-template-id that names a class template
14628 specialization is a class-name. */
14629 if (type_decl != NULL_TREE
14630 && TREE_CODE (type_decl) == TYPE_DECL
14631 && TYPE_DECL_ALIAS_P (type_decl))
14632 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14633 else
14634 cp_parser_simulate_error (parser);
14636 if (!cp_parser_parse_definitely (parser))
14637 /* ... Or a typedef-name or an enum-name. */
14638 return cp_parser_nonclass_name (parser);
14641 return type_decl;
14644 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14646 enum-name:
14647 identifier
14649 typedef-name:
14650 identifier
14652 Returns a TYPE_DECL for the type. */
14654 static tree
14655 cp_parser_nonclass_name (cp_parser* parser)
14657 tree type_decl;
14658 tree identifier;
14660 cp_token *token = cp_lexer_peek_token (parser->lexer);
14661 identifier = cp_parser_identifier (parser);
14662 if (identifier == error_mark_node)
14663 return error_mark_node;
14665 /* Look up the type-name. */
14666 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14668 if (TREE_CODE (type_decl) == USING_DECL)
14670 if (!DECL_DEPENDENT_P (type_decl))
14671 type_decl = strip_using_decl (type_decl);
14672 else if (USING_DECL_TYPENAME_P (type_decl))
14674 /* We have found a type introduced by a using
14675 declaration at class scope that refers to a dependent
14676 type.
14678 using typename :: [opt] nested-name-specifier unqualified-id ;
14680 type_decl = make_typename_type (TREE_TYPE (type_decl),
14681 DECL_NAME (type_decl),
14682 typename_type, tf_error);
14683 if (type_decl != error_mark_node)
14684 type_decl = TYPE_NAME (type_decl);
14688 if (TREE_CODE (type_decl) != TYPE_DECL
14689 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14691 /* See if this is an Objective-C type. */
14692 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14693 tree type = objc_get_protocol_qualified_type (identifier, protos);
14694 if (type)
14695 type_decl = TYPE_NAME (type);
14698 /* Issue an error if we did not find a type-name. */
14699 if (TREE_CODE (type_decl) != TYPE_DECL
14700 /* In Objective-C, we have the complication that class names are
14701 normally type names and start declarations (eg, the
14702 "NSObject" in "NSObject *object;"), but can be used in an
14703 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14704 is an expression. So, a classname followed by a dot is not a
14705 valid type-name. */
14706 || (objc_is_class_name (TREE_TYPE (type_decl))
14707 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14709 if (!cp_parser_simulate_error (parser))
14710 cp_parser_name_lookup_error (parser, identifier, type_decl,
14711 NLE_TYPE, token->location);
14712 return error_mark_node;
14714 /* Remember that the name was used in the definition of the
14715 current class so that we can check later to see if the
14716 meaning would have been different after the class was
14717 entirely defined. */
14718 else if (type_decl != error_mark_node
14719 && !parser->scope)
14720 maybe_note_name_used_in_class (identifier, type_decl);
14722 return type_decl;
14725 /* Parse an elaborated-type-specifier. Note that the grammar given
14726 here incorporates the resolution to DR68.
14728 elaborated-type-specifier:
14729 class-key :: [opt] nested-name-specifier [opt] identifier
14730 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14731 enum-key :: [opt] nested-name-specifier [opt] identifier
14732 typename :: [opt] nested-name-specifier identifier
14733 typename :: [opt] nested-name-specifier template [opt]
14734 template-id
14736 GNU extension:
14738 elaborated-type-specifier:
14739 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14740 class-key attributes :: [opt] nested-name-specifier [opt]
14741 template [opt] template-id
14742 enum attributes :: [opt] nested-name-specifier [opt] identifier
14744 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14745 declared `friend'. If IS_DECLARATION is TRUE, then this
14746 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14747 something is being declared.
14749 Returns the TYPE specified. */
14751 static tree
14752 cp_parser_elaborated_type_specifier (cp_parser* parser,
14753 bool is_friend,
14754 bool is_declaration)
14756 enum tag_types tag_type;
14757 tree identifier;
14758 tree type = NULL_TREE;
14759 tree attributes = NULL_TREE;
14760 tree globalscope;
14761 cp_token *token = NULL;
14763 /* See if we're looking at the `enum' keyword. */
14764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14766 /* Consume the `enum' token. */
14767 cp_lexer_consume_token (parser->lexer);
14768 /* Remember that it's an enumeration type. */
14769 tag_type = enum_type;
14770 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14771 enums) is used here. */
14772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14773 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14775 pedwarn (input_location, 0, "elaborated-type-specifier "
14776 "for a scoped enum must not use the %<%D%> keyword",
14777 cp_lexer_peek_token (parser->lexer)->u.value);
14778 /* Consume the `struct' or `class' and parse it anyway. */
14779 cp_lexer_consume_token (parser->lexer);
14781 /* Parse the attributes. */
14782 attributes = cp_parser_attributes_opt (parser);
14784 /* Or, it might be `typename'. */
14785 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14786 RID_TYPENAME))
14788 /* Consume the `typename' token. */
14789 cp_lexer_consume_token (parser->lexer);
14790 /* Remember that it's a `typename' type. */
14791 tag_type = typename_type;
14793 /* Otherwise it must be a class-key. */
14794 else
14796 tag_type = cp_parser_class_key (parser);
14797 if (tag_type == none_type)
14798 return error_mark_node;
14799 /* Parse the attributes. */
14800 attributes = cp_parser_attributes_opt (parser);
14803 /* Look for the `::' operator. */
14804 globalscope = cp_parser_global_scope_opt (parser,
14805 /*current_scope_valid_p=*/false);
14806 /* Look for the nested-name-specifier. */
14807 if (tag_type == typename_type && !globalscope)
14809 if (!cp_parser_nested_name_specifier (parser,
14810 /*typename_keyword_p=*/true,
14811 /*check_dependency_p=*/true,
14812 /*type_p=*/true,
14813 is_declaration))
14814 return error_mark_node;
14816 else
14817 /* Even though `typename' is not present, the proposed resolution
14818 to Core Issue 180 says that in `class A<T>::B', `B' should be
14819 considered a type-name, even if `A<T>' is dependent. */
14820 cp_parser_nested_name_specifier_opt (parser,
14821 /*typename_keyword_p=*/true,
14822 /*check_dependency_p=*/true,
14823 /*type_p=*/true,
14824 is_declaration);
14825 /* For everything but enumeration types, consider a template-id.
14826 For an enumeration type, consider only a plain identifier. */
14827 if (tag_type != enum_type)
14829 bool template_p = false;
14830 tree decl;
14832 /* Allow the `template' keyword. */
14833 template_p = cp_parser_optional_template_keyword (parser);
14834 /* If we didn't see `template', we don't know if there's a
14835 template-id or not. */
14836 if (!template_p)
14837 cp_parser_parse_tentatively (parser);
14838 /* Parse the template-id. */
14839 token = cp_lexer_peek_token (parser->lexer);
14840 decl = cp_parser_template_id (parser, template_p,
14841 /*check_dependency_p=*/true,
14842 tag_type,
14843 is_declaration);
14844 /* If we didn't find a template-id, look for an ordinary
14845 identifier. */
14846 if (!template_p && !cp_parser_parse_definitely (parser))
14848 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14849 in effect, then we must assume that, upon instantiation, the
14850 template will correspond to a class. */
14851 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14852 && tag_type == typename_type)
14853 type = make_typename_type (parser->scope, decl,
14854 typename_type,
14855 /*complain=*/tf_error);
14856 /* If the `typename' keyword is in effect and DECL is not a type
14857 decl, then type is non existent. */
14858 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14860 else if (TREE_CODE (decl) == TYPE_DECL)
14861 type = check_elaborated_type_specifier (tag_type, decl,
14862 /*allow_template_p=*/true);
14863 else if (decl == error_mark_node)
14864 type = error_mark_node;
14867 if (!type)
14869 token = cp_lexer_peek_token (parser->lexer);
14870 identifier = cp_parser_identifier (parser);
14872 if (identifier == error_mark_node)
14874 parser->scope = NULL_TREE;
14875 return error_mark_node;
14878 /* For a `typename', we needn't call xref_tag. */
14879 if (tag_type == typename_type
14880 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14881 return cp_parser_make_typename_type (parser, parser->scope,
14882 identifier,
14883 token->location);
14884 /* Look up a qualified name in the usual way. */
14885 if (parser->scope)
14887 tree decl;
14888 tree ambiguous_decls;
14890 decl = cp_parser_lookup_name (parser, identifier,
14891 tag_type,
14892 /*is_template=*/false,
14893 /*is_namespace=*/false,
14894 /*check_dependency=*/true,
14895 &ambiguous_decls,
14896 token->location);
14898 /* If the lookup was ambiguous, an error will already have been
14899 issued. */
14900 if (ambiguous_decls)
14901 return error_mark_node;
14903 /* If we are parsing friend declaration, DECL may be a
14904 TEMPLATE_DECL tree node here. However, we need to check
14905 whether this TEMPLATE_DECL results in valid code. Consider
14906 the following example:
14908 namespace N {
14909 template <class T> class C {};
14911 class X {
14912 template <class T> friend class N::C; // #1, valid code
14914 template <class T> class Y {
14915 friend class N::C; // #2, invalid code
14918 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14919 name lookup of `N::C'. We see that friend declaration must
14920 be template for the code to be valid. Note that
14921 processing_template_decl does not work here since it is
14922 always 1 for the above two cases. */
14924 decl = (cp_parser_maybe_treat_template_as_class
14925 (decl, /*tag_name_p=*/is_friend
14926 && parser->num_template_parameter_lists));
14928 if (TREE_CODE (decl) != TYPE_DECL)
14930 cp_parser_diagnose_invalid_type_name (parser,
14931 parser->scope,
14932 identifier,
14933 token->location);
14934 return error_mark_node;
14937 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14939 bool allow_template = (parser->num_template_parameter_lists
14940 || DECL_SELF_REFERENCE_P (decl));
14941 type = check_elaborated_type_specifier (tag_type, decl,
14942 allow_template);
14944 if (type == error_mark_node)
14945 return error_mark_node;
14948 /* Forward declarations of nested types, such as
14950 class C1::C2;
14951 class C1::C2::C3;
14953 are invalid unless all components preceding the final '::'
14954 are complete. If all enclosing types are complete, these
14955 declarations become merely pointless.
14957 Invalid forward declarations of nested types are errors
14958 caught elsewhere in parsing. Those that are pointless arrive
14959 here. */
14961 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14962 && !is_friend && !processing_explicit_instantiation)
14963 warning (0, "declaration %qD does not declare anything", decl);
14965 type = TREE_TYPE (decl);
14967 else
14969 /* An elaborated-type-specifier sometimes introduces a new type and
14970 sometimes names an existing type. Normally, the rule is that it
14971 introduces a new type only if there is not an existing type of
14972 the same name already in scope. For example, given:
14974 struct S {};
14975 void f() { struct S s; }
14977 the `struct S' in the body of `f' is the same `struct S' as in
14978 the global scope; the existing definition is used. However, if
14979 there were no global declaration, this would introduce a new
14980 local class named `S'.
14982 An exception to this rule applies to the following code:
14984 namespace N { struct S; }
14986 Here, the elaborated-type-specifier names a new type
14987 unconditionally; even if there is already an `S' in the
14988 containing scope this declaration names a new type.
14989 This exception only applies if the elaborated-type-specifier
14990 forms the complete declaration:
14992 [class.name]
14994 A declaration consisting solely of `class-key identifier ;' is
14995 either a redeclaration of the name in the current scope or a
14996 forward declaration of the identifier as a class name. It
14997 introduces the name into the current scope.
14999 We are in this situation precisely when the next token is a `;'.
15001 An exception to the exception is that a `friend' declaration does
15002 *not* name a new type; i.e., given:
15004 struct S { friend struct T; };
15006 `T' is not a new type in the scope of `S'.
15008 Also, `new struct S' or `sizeof (struct S)' never results in the
15009 definition of a new type; a new type can only be declared in a
15010 declaration context. */
15012 tag_scope ts;
15013 bool template_p;
15015 if (is_friend)
15016 /* Friends have special name lookup rules. */
15017 ts = ts_within_enclosing_non_class;
15018 else if (is_declaration
15019 && cp_lexer_next_token_is (parser->lexer,
15020 CPP_SEMICOLON))
15021 /* This is a `class-key identifier ;' */
15022 ts = ts_current;
15023 else
15024 ts = ts_global;
15026 template_p =
15027 (parser->num_template_parameter_lists
15028 && (cp_parser_next_token_starts_class_definition_p (parser)
15029 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15030 /* An unqualified name was used to reference this type, so
15031 there were no qualifying templates. */
15032 if (!cp_parser_check_template_parameters (parser,
15033 /*num_templates=*/0,
15034 token->location,
15035 /*declarator=*/NULL))
15036 return error_mark_node;
15037 type = xref_tag (tag_type, identifier, ts, template_p);
15041 if (type == error_mark_node)
15042 return error_mark_node;
15044 /* Allow attributes on forward declarations of classes. */
15045 if (attributes)
15047 if (TREE_CODE (type) == TYPENAME_TYPE)
15048 warning (OPT_Wattributes,
15049 "attributes ignored on uninstantiated type");
15050 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15051 && ! processing_explicit_instantiation)
15052 warning (OPT_Wattributes,
15053 "attributes ignored on template instantiation");
15054 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15055 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15056 else
15057 warning (OPT_Wattributes,
15058 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15061 if (tag_type != enum_type)
15063 /* Indicate whether this class was declared as a `class' or as a
15064 `struct'. */
15065 if (TREE_CODE (type) == RECORD_TYPE)
15066 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15067 cp_parser_check_class_key (tag_type, type);
15070 /* A "<" cannot follow an elaborated type specifier. If that
15071 happens, the user was probably trying to form a template-id. */
15072 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15073 token->location);
15075 return type;
15078 /* Parse an enum-specifier.
15080 enum-specifier:
15081 enum-head { enumerator-list [opt] }
15082 enum-head { enumerator-list , } [C++0x]
15084 enum-head:
15085 enum-key identifier [opt] enum-base [opt]
15086 enum-key nested-name-specifier identifier enum-base [opt]
15088 enum-key:
15089 enum
15090 enum class [C++0x]
15091 enum struct [C++0x]
15093 enum-base: [C++0x]
15094 : type-specifier-seq
15096 opaque-enum-specifier:
15097 enum-key identifier enum-base [opt] ;
15099 GNU Extensions:
15100 enum-key attributes[opt] identifier [opt] enum-base [opt]
15101 { enumerator-list [opt] }attributes[opt]
15102 enum-key attributes[opt] identifier [opt] enum-base [opt]
15103 { enumerator-list, }attributes[opt] [C++0x]
15105 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15106 if the token stream isn't an enum-specifier after all. */
15108 static tree
15109 cp_parser_enum_specifier (cp_parser* parser)
15111 tree identifier;
15112 tree type = NULL_TREE;
15113 tree prev_scope;
15114 tree nested_name_specifier = NULL_TREE;
15115 tree attributes;
15116 bool scoped_enum_p = false;
15117 bool has_underlying_type = false;
15118 bool nested_being_defined = false;
15119 bool new_value_list = false;
15120 bool is_new_type = false;
15121 bool is_anonymous = false;
15122 tree underlying_type = NULL_TREE;
15123 cp_token *type_start_token = NULL;
15124 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15126 parser->colon_corrects_to_scope_p = false;
15128 /* Parse tentatively so that we can back up if we don't find a
15129 enum-specifier. */
15130 cp_parser_parse_tentatively (parser);
15132 /* Caller guarantees that the current token is 'enum', an identifier
15133 possibly follows, and the token after that is an opening brace.
15134 If we don't have an identifier, fabricate an anonymous name for
15135 the enumeration being defined. */
15136 cp_lexer_consume_token (parser->lexer);
15138 /* Parse the "class" or "struct", which indicates a scoped
15139 enumeration type in C++0x. */
15140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15141 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15143 if (cxx_dialect < cxx11)
15144 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15146 /* Consume the `struct' or `class' token. */
15147 cp_lexer_consume_token (parser->lexer);
15149 scoped_enum_p = true;
15152 attributes = cp_parser_attributes_opt (parser);
15154 /* Clear the qualification. */
15155 parser->scope = NULL_TREE;
15156 parser->qualifying_scope = NULL_TREE;
15157 parser->object_scope = NULL_TREE;
15159 /* Figure out in what scope the declaration is being placed. */
15160 prev_scope = current_scope ();
15162 type_start_token = cp_lexer_peek_token (parser->lexer);
15164 push_deferring_access_checks (dk_no_check);
15165 nested_name_specifier
15166 = cp_parser_nested_name_specifier_opt (parser,
15167 /*typename_keyword_p=*/true,
15168 /*check_dependency_p=*/false,
15169 /*type_p=*/false,
15170 /*is_declaration=*/false);
15172 if (nested_name_specifier)
15174 tree name;
15176 identifier = cp_parser_identifier (parser);
15177 name = cp_parser_lookup_name (parser, identifier,
15178 enum_type,
15179 /*is_template=*/false,
15180 /*is_namespace=*/false,
15181 /*check_dependency=*/true,
15182 /*ambiguous_decls=*/NULL,
15183 input_location);
15184 if (name && name != error_mark_node)
15186 type = TREE_TYPE (name);
15187 if (TREE_CODE (type) == TYPENAME_TYPE)
15189 /* Are template enums allowed in ISO? */
15190 if (template_parm_scope_p ())
15191 pedwarn (type_start_token->location, OPT_Wpedantic,
15192 "%qD is an enumeration template", name);
15193 /* ignore a typename reference, for it will be solved by name
15194 in start_enum. */
15195 type = NULL_TREE;
15198 else if (nested_name_specifier == error_mark_node)
15199 /* We already issued an error. */;
15200 else
15201 error_at (type_start_token->location,
15202 "%qD is not an enumerator-name", identifier);
15204 else
15206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15207 identifier = cp_parser_identifier (parser);
15208 else
15210 identifier = make_anon_name ();
15211 is_anonymous = true;
15212 if (scoped_enum_p)
15213 error_at (type_start_token->location,
15214 "anonymous scoped enum is not allowed");
15217 pop_deferring_access_checks ();
15219 /* Check for the `:' that denotes a specified underlying type in C++0x.
15220 Note that a ':' could also indicate a bitfield width, however. */
15221 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15223 cp_decl_specifier_seq type_specifiers;
15225 /* Consume the `:'. */
15226 cp_lexer_consume_token (parser->lexer);
15228 /* Parse the type-specifier-seq. */
15229 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15230 /*is_trailing_return=*/false,
15231 &type_specifiers);
15233 /* At this point this is surely not elaborated type specifier. */
15234 if (!cp_parser_parse_definitely (parser))
15235 return NULL_TREE;
15237 if (cxx_dialect < cxx11)
15238 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15240 has_underlying_type = true;
15242 /* If that didn't work, stop. */
15243 if (type_specifiers.type != error_mark_node)
15245 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15246 /*initialized=*/0, NULL);
15247 if (underlying_type == error_mark_node)
15248 underlying_type = NULL_TREE;
15252 /* Look for the `{' but don't consume it yet. */
15253 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15255 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15257 cp_parser_error (parser, "expected %<{%>");
15258 if (has_underlying_type)
15260 type = NULL_TREE;
15261 goto out;
15264 /* An opaque-enum-specifier must have a ';' here. */
15265 if ((scoped_enum_p || underlying_type)
15266 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15268 cp_parser_error (parser, "expected %<;%> or %<{%>");
15269 if (has_underlying_type)
15271 type = NULL_TREE;
15272 goto out;
15277 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15278 return NULL_TREE;
15280 if (nested_name_specifier)
15282 if (CLASS_TYPE_P (nested_name_specifier))
15284 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15285 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15286 push_scope (nested_name_specifier);
15288 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15290 push_nested_namespace (nested_name_specifier);
15294 /* Issue an error message if type-definitions are forbidden here. */
15295 if (!cp_parser_check_type_definition (parser))
15296 type = error_mark_node;
15297 else
15298 /* Create the new type. We do this before consuming the opening
15299 brace so the enum will be recorded as being on the line of its
15300 tag (or the 'enum' keyword, if there is no tag). */
15301 type = start_enum (identifier, type, underlying_type,
15302 scoped_enum_p, &is_new_type);
15304 /* If the next token is not '{' it is an opaque-enum-specifier or an
15305 elaborated-type-specifier. */
15306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15308 timevar_push (TV_PARSE_ENUM);
15309 if (nested_name_specifier
15310 && nested_name_specifier != error_mark_node)
15312 /* The following catches invalid code such as:
15313 enum class S<int>::E { A, B, C }; */
15314 if (!processing_specialization
15315 && CLASS_TYPE_P (nested_name_specifier)
15316 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15317 error_at (type_start_token->location, "cannot add an enumerator "
15318 "list to a template instantiation");
15320 /* If that scope does not contain the scope in which the
15321 class was originally declared, the program is invalid. */
15322 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
15324 if (at_namespace_scope_p ())
15325 error_at (type_start_token->location,
15326 "declaration of %qD in namespace %qD which does not "
15327 "enclose %qD",
15328 type, prev_scope, nested_name_specifier);
15329 else
15330 error_at (type_start_token->location,
15331 "declaration of %qD in %qD which does not enclose %qD",
15332 type, prev_scope, nested_name_specifier);
15333 type = error_mark_node;
15337 if (scoped_enum_p)
15338 begin_scope (sk_scoped_enum, type);
15340 /* Consume the opening brace. */
15341 cp_lexer_consume_token (parser->lexer);
15343 if (type == error_mark_node)
15344 ; /* Nothing to add */
15345 else if (OPAQUE_ENUM_P (type)
15346 || (cxx_dialect > cxx98 && processing_specialization))
15348 new_value_list = true;
15349 SET_OPAQUE_ENUM_P (type, false);
15350 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15352 else
15354 error_at (type_start_token->location, "multiple definition of %q#T", type);
15355 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15356 "previous definition here");
15357 type = error_mark_node;
15360 if (type == error_mark_node)
15361 cp_parser_skip_to_end_of_block_or_statement (parser);
15362 /* If the next token is not '}', then there are some enumerators. */
15363 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15365 if (is_anonymous && !scoped_enum_p)
15366 pedwarn (type_start_token->location, OPT_Wpedantic,
15367 "ISO C++ forbids empty anonymous enum");
15369 else
15370 cp_parser_enumerator_list (parser, type);
15372 /* Consume the final '}'. */
15373 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15375 if (scoped_enum_p)
15376 finish_scope ();
15377 timevar_pop (TV_PARSE_ENUM);
15379 else
15381 /* If a ';' follows, then it is an opaque-enum-specifier
15382 and additional restrictions apply. */
15383 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15385 if (is_anonymous)
15386 error_at (type_start_token->location,
15387 "opaque-enum-specifier without name");
15388 else if (nested_name_specifier)
15389 error_at (type_start_token->location,
15390 "opaque-enum-specifier must use a simple identifier");
15394 /* Look for trailing attributes to apply to this enumeration, and
15395 apply them if appropriate. */
15396 if (cp_parser_allow_gnu_extensions_p (parser))
15398 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15399 trailing_attr = chainon (trailing_attr, attributes);
15400 cplus_decl_attributes (&type,
15401 trailing_attr,
15402 (int) ATTR_FLAG_TYPE_IN_PLACE);
15405 /* Finish up the enumeration. */
15406 if (type != error_mark_node)
15408 if (new_value_list)
15409 finish_enum_value_list (type);
15410 if (is_new_type)
15411 finish_enum (type);
15414 if (nested_name_specifier)
15416 if (CLASS_TYPE_P (nested_name_specifier))
15418 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15419 pop_scope (nested_name_specifier);
15421 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15423 pop_nested_namespace (nested_name_specifier);
15426 out:
15427 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15428 return type;
15431 /* Parse an enumerator-list. The enumerators all have the indicated
15432 TYPE.
15434 enumerator-list:
15435 enumerator-definition
15436 enumerator-list , enumerator-definition */
15438 static void
15439 cp_parser_enumerator_list (cp_parser* parser, tree type)
15441 while (true)
15443 /* Parse an enumerator-definition. */
15444 cp_parser_enumerator_definition (parser, type);
15446 /* If the next token is not a ',', we've reached the end of
15447 the list. */
15448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15449 break;
15450 /* Otherwise, consume the `,' and keep going. */
15451 cp_lexer_consume_token (parser->lexer);
15452 /* If the next token is a `}', there is a trailing comma. */
15453 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15455 if (cxx_dialect < cxx11 && !in_system_header)
15456 pedwarn (input_location, OPT_Wpedantic,
15457 "comma at end of enumerator list");
15458 break;
15463 /* Parse an enumerator-definition. The enumerator has the indicated
15464 TYPE.
15466 enumerator-definition:
15467 enumerator
15468 enumerator = constant-expression
15470 enumerator:
15471 identifier */
15473 static void
15474 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15476 tree identifier;
15477 tree value;
15478 location_t loc;
15480 /* Save the input location because we are interested in the location
15481 of the identifier and not the location of the explicit value. */
15482 loc = cp_lexer_peek_token (parser->lexer)->location;
15484 /* Look for the identifier. */
15485 identifier = cp_parser_identifier (parser);
15486 if (identifier == error_mark_node)
15487 return;
15489 /* If the next token is an '=', then there is an explicit value. */
15490 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15492 /* Consume the `=' token. */
15493 cp_lexer_consume_token (parser->lexer);
15494 /* Parse the value. */
15495 value = cp_parser_constant_expression (parser,
15496 /*allow_non_constant_p=*/false,
15497 NULL);
15499 else
15500 value = NULL_TREE;
15502 /* If we are processing a template, make sure the initializer of the
15503 enumerator doesn't contain any bare template parameter pack. */
15504 if (check_for_bare_parameter_packs (value))
15505 value = error_mark_node;
15507 /* integral_constant_value will pull out this expression, so make sure
15508 it's folded as appropriate. */
15509 value = fold_non_dependent_expr (value);
15511 /* Create the enumerator. */
15512 build_enumerator (identifier, value, type, loc);
15515 /* Parse a namespace-name.
15517 namespace-name:
15518 original-namespace-name
15519 namespace-alias
15521 Returns the NAMESPACE_DECL for the namespace. */
15523 static tree
15524 cp_parser_namespace_name (cp_parser* parser)
15526 tree identifier;
15527 tree namespace_decl;
15529 cp_token *token = cp_lexer_peek_token (parser->lexer);
15531 /* Get the name of the namespace. */
15532 identifier = cp_parser_identifier (parser);
15533 if (identifier == error_mark_node)
15534 return error_mark_node;
15536 /* Look up the identifier in the currently active scope. Look only
15537 for namespaces, due to:
15539 [basic.lookup.udir]
15541 When looking up a namespace-name in a using-directive or alias
15542 definition, only namespace names are considered.
15544 And:
15546 [basic.lookup.qual]
15548 During the lookup of a name preceding the :: scope resolution
15549 operator, object, function, and enumerator names are ignored.
15551 (Note that cp_parser_qualifying_entity only calls this
15552 function if the token after the name is the scope resolution
15553 operator.) */
15554 namespace_decl = cp_parser_lookup_name (parser, identifier,
15555 none_type,
15556 /*is_template=*/false,
15557 /*is_namespace=*/true,
15558 /*check_dependency=*/true,
15559 /*ambiguous_decls=*/NULL,
15560 token->location);
15561 /* If it's not a namespace, issue an error. */
15562 if (namespace_decl == error_mark_node
15563 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15565 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15566 error_at (token->location, "%qD is not a namespace-name", identifier);
15567 cp_parser_error (parser, "expected namespace-name");
15568 namespace_decl = error_mark_node;
15571 return namespace_decl;
15574 /* Parse a namespace-definition.
15576 namespace-definition:
15577 named-namespace-definition
15578 unnamed-namespace-definition
15580 named-namespace-definition:
15581 original-namespace-definition
15582 extension-namespace-definition
15584 original-namespace-definition:
15585 namespace identifier { namespace-body }
15587 extension-namespace-definition:
15588 namespace original-namespace-name { namespace-body }
15590 unnamed-namespace-definition:
15591 namespace { namespace-body } */
15593 static void
15594 cp_parser_namespace_definition (cp_parser* parser)
15596 tree identifier, attribs;
15597 bool has_visibility;
15598 bool is_inline;
15600 cp_ensure_no_omp_declare_simd (parser);
15601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15603 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15604 is_inline = true;
15605 cp_lexer_consume_token (parser->lexer);
15607 else
15608 is_inline = false;
15610 /* Look for the `namespace' keyword. */
15611 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15613 /* Get the name of the namespace. We do not attempt to distinguish
15614 between an original-namespace-definition and an
15615 extension-namespace-definition at this point. The semantic
15616 analysis routines are responsible for that. */
15617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15618 identifier = cp_parser_identifier (parser);
15619 else
15620 identifier = NULL_TREE;
15622 /* Parse any specified attributes. */
15623 attribs = cp_parser_attributes_opt (parser);
15625 /* Look for the `{' to start the namespace. */
15626 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15627 /* Start the namespace. */
15628 push_namespace (identifier);
15630 /* "inline namespace" is equivalent to a stub namespace definition
15631 followed by a strong using directive. */
15632 if (is_inline)
15634 tree name_space = current_namespace;
15635 /* Set up namespace association. */
15636 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15637 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15638 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15639 /* Import the contents of the inline namespace. */
15640 pop_namespace ();
15641 do_using_directive (name_space);
15642 push_namespace (identifier);
15645 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15647 /* Parse the body of the namespace. */
15648 cp_parser_namespace_body (parser);
15650 if (has_visibility)
15651 pop_visibility (1);
15653 /* Finish the namespace. */
15654 pop_namespace ();
15655 /* Look for the final `}'. */
15656 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15659 /* Parse a namespace-body.
15661 namespace-body:
15662 declaration-seq [opt] */
15664 static void
15665 cp_parser_namespace_body (cp_parser* parser)
15667 cp_parser_declaration_seq_opt (parser);
15670 /* Parse a namespace-alias-definition.
15672 namespace-alias-definition:
15673 namespace identifier = qualified-namespace-specifier ; */
15675 static void
15676 cp_parser_namespace_alias_definition (cp_parser* parser)
15678 tree identifier;
15679 tree namespace_specifier;
15681 cp_token *token = cp_lexer_peek_token (parser->lexer);
15683 /* Look for the `namespace' keyword. */
15684 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15685 /* Look for the identifier. */
15686 identifier = cp_parser_identifier (parser);
15687 if (identifier == error_mark_node)
15688 return;
15689 /* Look for the `=' token. */
15690 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15691 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15693 error_at (token->location, "%<namespace%> definition is not allowed here");
15694 /* Skip the definition. */
15695 cp_lexer_consume_token (parser->lexer);
15696 if (cp_parser_skip_to_closing_brace (parser))
15697 cp_lexer_consume_token (parser->lexer);
15698 return;
15700 cp_parser_require (parser, CPP_EQ, RT_EQ);
15701 /* Look for the qualified-namespace-specifier. */
15702 namespace_specifier
15703 = cp_parser_qualified_namespace_specifier (parser);
15704 /* Look for the `;' token. */
15705 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15707 /* Register the alias in the symbol table. */
15708 do_namespace_alias (identifier, namespace_specifier);
15711 /* Parse a qualified-namespace-specifier.
15713 qualified-namespace-specifier:
15714 :: [opt] nested-name-specifier [opt] namespace-name
15716 Returns a NAMESPACE_DECL corresponding to the specified
15717 namespace. */
15719 static tree
15720 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15722 /* Look for the optional `::'. */
15723 cp_parser_global_scope_opt (parser,
15724 /*current_scope_valid_p=*/false);
15726 /* Look for the optional nested-name-specifier. */
15727 cp_parser_nested_name_specifier_opt (parser,
15728 /*typename_keyword_p=*/false,
15729 /*check_dependency_p=*/true,
15730 /*type_p=*/false,
15731 /*is_declaration=*/true);
15733 return cp_parser_namespace_name (parser);
15736 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15737 access declaration.
15739 using-declaration:
15740 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15741 using :: unqualified-id ;
15743 access-declaration:
15744 qualified-id ;
15748 static bool
15749 cp_parser_using_declaration (cp_parser* parser,
15750 bool access_declaration_p)
15752 cp_token *token;
15753 bool typename_p = false;
15754 bool global_scope_p;
15755 tree decl;
15756 tree identifier;
15757 tree qscope;
15758 int oldcount = errorcount;
15759 cp_token *diag_token = NULL;
15761 if (access_declaration_p)
15763 diag_token = cp_lexer_peek_token (parser->lexer);
15764 cp_parser_parse_tentatively (parser);
15766 else
15768 /* Look for the `using' keyword. */
15769 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15771 /* Peek at the next token. */
15772 token = cp_lexer_peek_token (parser->lexer);
15773 /* See if it's `typename'. */
15774 if (token->keyword == RID_TYPENAME)
15776 /* Remember that we've seen it. */
15777 typename_p = true;
15778 /* Consume the `typename' token. */
15779 cp_lexer_consume_token (parser->lexer);
15783 /* Look for the optional global scope qualification. */
15784 global_scope_p
15785 = (cp_parser_global_scope_opt (parser,
15786 /*current_scope_valid_p=*/false)
15787 != NULL_TREE);
15789 /* If we saw `typename', or didn't see `::', then there must be a
15790 nested-name-specifier present. */
15791 if (typename_p || !global_scope_p)
15792 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15793 /*check_dependency_p=*/true,
15794 /*type_p=*/false,
15795 /*is_declaration=*/true);
15796 /* Otherwise, we could be in either of the two productions. In that
15797 case, treat the nested-name-specifier as optional. */
15798 else
15799 qscope = cp_parser_nested_name_specifier_opt (parser,
15800 /*typename_keyword_p=*/false,
15801 /*check_dependency_p=*/true,
15802 /*type_p=*/false,
15803 /*is_declaration=*/true);
15804 if (!qscope)
15805 qscope = global_namespace;
15807 if (access_declaration_p && cp_parser_error_occurred (parser))
15808 /* Something has already gone wrong; there's no need to parse
15809 further. Since an error has occurred, the return value of
15810 cp_parser_parse_definitely will be false, as required. */
15811 return cp_parser_parse_definitely (parser);
15813 token = cp_lexer_peek_token (parser->lexer);
15814 /* Parse the unqualified-id. */
15815 identifier = cp_parser_unqualified_id (parser,
15816 /*template_keyword_p=*/false,
15817 /*check_dependency_p=*/true,
15818 /*declarator_p=*/true,
15819 /*optional_p=*/false);
15821 if (access_declaration_p)
15823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15824 cp_parser_simulate_error (parser);
15825 if (!cp_parser_parse_definitely (parser))
15826 return false;
15829 /* The function we call to handle a using-declaration is different
15830 depending on what scope we are in. */
15831 if (qscope == error_mark_node || identifier == error_mark_node)
15833 else if (!identifier_p (identifier)
15834 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15835 /* [namespace.udecl]
15837 A using declaration shall not name a template-id. */
15838 error_at (token->location,
15839 "a template-id may not appear in a using-declaration");
15840 else
15842 if (at_class_scope_p ())
15844 /* Create the USING_DECL. */
15845 decl = do_class_using_decl (parser->scope, identifier);
15847 if (decl && typename_p)
15848 USING_DECL_TYPENAME_P (decl) = 1;
15850 if (check_for_bare_parameter_packs (decl))
15851 return false;
15852 else
15853 /* Add it to the list of members in this class. */
15854 finish_member_declaration (decl);
15856 else
15858 decl = cp_parser_lookup_name_simple (parser,
15859 identifier,
15860 token->location);
15861 if (decl == error_mark_node)
15862 cp_parser_name_lookup_error (parser, identifier,
15863 decl, NLE_NULL,
15864 token->location);
15865 else if (check_for_bare_parameter_packs (decl))
15866 return false;
15867 else if (!at_namespace_scope_p ())
15868 do_local_using_decl (decl, qscope, identifier);
15869 else
15870 do_toplevel_using_decl (decl, qscope, identifier);
15874 /* Look for the final `;'. */
15875 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15877 if (access_declaration_p && errorcount == oldcount)
15878 warning_at (diag_token->location, OPT_Wdeprecated,
15879 "access declarations are deprecated "
15880 "in favour of using-declarations; "
15881 "suggestion: add the %<using%> keyword");
15883 return true;
15886 /* Parse an alias-declaration.
15888 alias-declaration:
15889 using identifier attribute-specifier-seq [opt] = type-id */
15891 static tree
15892 cp_parser_alias_declaration (cp_parser* parser)
15894 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15895 location_t id_location;
15896 cp_declarator *declarator;
15897 cp_decl_specifier_seq decl_specs;
15898 bool member_p;
15899 const char *saved_message = NULL;
15901 /* Look for the `using' keyword. */
15902 cp_token *using_token
15903 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15904 if (using_token == NULL)
15905 return error_mark_node;
15907 id_location = cp_lexer_peek_token (parser->lexer)->location;
15908 id = cp_parser_identifier (parser);
15909 if (id == error_mark_node)
15910 return error_mark_node;
15912 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15913 attributes = cp_parser_attributes_opt (parser);
15914 if (attributes == error_mark_node)
15915 return error_mark_node;
15917 cp_parser_require (parser, CPP_EQ, RT_EQ);
15919 if (cp_parser_error_occurred (parser))
15920 return error_mark_node;
15922 cp_parser_commit_to_tentative_parse (parser);
15924 /* Now we are going to parse the type-id of the declaration. */
15927 [dcl.type]/3 says:
15929 "A type-specifier-seq shall not define a class or enumeration
15930 unless it appears in the type-id of an alias-declaration (7.1.3) that
15931 is not the declaration of a template-declaration."
15933 In other words, if we currently are in an alias template, the
15934 type-id should not define a type.
15936 So let's set parser->type_definition_forbidden_message in that
15937 case; cp_parser_check_type_definition (called by
15938 cp_parser_class_specifier) will then emit an error if a type is
15939 defined in the type-id. */
15940 if (parser->num_template_parameter_lists)
15942 saved_message = parser->type_definition_forbidden_message;
15943 parser->type_definition_forbidden_message =
15944 G_("types may not be defined in alias template declarations");
15947 type = cp_parser_type_id (parser);
15949 /* Restore the error message if need be. */
15950 if (parser->num_template_parameter_lists)
15951 parser->type_definition_forbidden_message = saved_message;
15953 if (type == error_mark_node)
15955 cp_parser_skip_to_end_of_block_or_statement (parser);
15956 return error_mark_node;
15959 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15961 if (cp_parser_error_occurred (parser))
15963 cp_parser_skip_to_end_of_block_or_statement (parser);
15964 return error_mark_node;
15967 /* A typedef-name can also be introduced by an alias-declaration. The
15968 identifier following the using keyword becomes a typedef-name. It has
15969 the same semantics as if it were introduced by the typedef
15970 specifier. In particular, it does not define a new type and it shall
15971 not appear in the type-id. */
15973 clear_decl_specs (&decl_specs);
15974 decl_specs.type = type;
15975 if (attributes != NULL_TREE)
15977 decl_specs.attributes = attributes;
15978 set_and_check_decl_spec_loc (&decl_specs,
15979 ds_attribute,
15980 attrs_token);
15982 set_and_check_decl_spec_loc (&decl_specs,
15983 ds_typedef,
15984 using_token);
15985 set_and_check_decl_spec_loc (&decl_specs,
15986 ds_alias,
15987 using_token);
15989 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15990 declarator->id_loc = id_location;
15992 member_p = at_class_scope_p ();
15993 if (member_p)
15994 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15995 NULL_TREE, attributes);
15996 else
15997 decl = start_decl (declarator, &decl_specs, 0,
15998 attributes, NULL_TREE, &pushed_scope);
15999 if (decl == error_mark_node)
16000 return decl;
16002 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16004 if (pushed_scope)
16005 pop_scope (pushed_scope);
16007 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16008 added into the symbol table; otherwise, return the TYPE_DECL. */
16009 if (DECL_LANG_SPECIFIC (decl)
16010 && DECL_TEMPLATE_INFO (decl)
16011 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16013 decl = DECL_TI_TEMPLATE (decl);
16014 if (member_p)
16015 check_member_template (decl);
16018 return decl;
16021 /* Parse a using-directive.
16023 using-directive:
16024 using namespace :: [opt] nested-name-specifier [opt]
16025 namespace-name ; */
16027 static void
16028 cp_parser_using_directive (cp_parser* parser)
16030 tree namespace_decl;
16031 tree attribs;
16033 /* Look for the `using' keyword. */
16034 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16035 /* And the `namespace' keyword. */
16036 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16037 /* Look for the optional `::' operator. */
16038 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16039 /* And the optional nested-name-specifier. */
16040 cp_parser_nested_name_specifier_opt (parser,
16041 /*typename_keyword_p=*/false,
16042 /*check_dependency_p=*/true,
16043 /*type_p=*/false,
16044 /*is_declaration=*/true);
16045 /* Get the namespace being used. */
16046 namespace_decl = cp_parser_namespace_name (parser);
16047 /* And any specified attributes. */
16048 attribs = cp_parser_attributes_opt (parser);
16049 /* Update the symbol table. */
16050 parse_using_directive (namespace_decl, attribs);
16051 /* Look for the final `;'. */
16052 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16055 /* Parse an asm-definition.
16057 asm-definition:
16058 asm ( string-literal ) ;
16060 GNU Extension:
16062 asm-definition:
16063 asm volatile [opt] ( string-literal ) ;
16064 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16065 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16066 : asm-operand-list [opt] ) ;
16067 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16068 : asm-operand-list [opt]
16069 : asm-clobber-list [opt] ) ;
16070 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16071 : asm-clobber-list [opt]
16072 : asm-goto-list ) ; */
16074 static void
16075 cp_parser_asm_definition (cp_parser* parser)
16077 tree string;
16078 tree outputs = NULL_TREE;
16079 tree inputs = NULL_TREE;
16080 tree clobbers = NULL_TREE;
16081 tree labels = NULL_TREE;
16082 tree asm_stmt;
16083 bool volatile_p = false;
16084 bool extended_p = false;
16085 bool invalid_inputs_p = false;
16086 bool invalid_outputs_p = false;
16087 bool goto_p = false;
16088 required_token missing = RT_NONE;
16090 /* Look for the `asm' keyword. */
16091 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16092 /* See if the next token is `volatile'. */
16093 if (cp_parser_allow_gnu_extensions_p (parser)
16094 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16096 /* Remember that we saw the `volatile' keyword. */
16097 volatile_p = true;
16098 /* Consume the token. */
16099 cp_lexer_consume_token (parser->lexer);
16101 if (cp_parser_allow_gnu_extensions_p (parser)
16102 && parser->in_function_body
16103 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16105 /* Remember that we saw the `goto' keyword. */
16106 goto_p = true;
16107 /* Consume the token. */
16108 cp_lexer_consume_token (parser->lexer);
16110 /* Look for the opening `('. */
16111 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16112 return;
16113 /* Look for the string. */
16114 string = cp_parser_string_literal (parser, false, false);
16115 if (string == error_mark_node)
16117 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16118 /*consume_paren=*/true);
16119 return;
16122 /* If we're allowing GNU extensions, check for the extended assembly
16123 syntax. Unfortunately, the `:' tokens need not be separated by
16124 a space in C, and so, for compatibility, we tolerate that here
16125 too. Doing that means that we have to treat the `::' operator as
16126 two `:' tokens. */
16127 if (cp_parser_allow_gnu_extensions_p (parser)
16128 && parser->in_function_body
16129 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16130 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16132 bool inputs_p = false;
16133 bool clobbers_p = false;
16134 bool labels_p = false;
16136 /* The extended syntax was used. */
16137 extended_p = true;
16139 /* Look for outputs. */
16140 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16142 /* Consume the `:'. */
16143 cp_lexer_consume_token (parser->lexer);
16144 /* Parse the output-operands. */
16145 if (cp_lexer_next_token_is_not (parser->lexer,
16146 CPP_COLON)
16147 && cp_lexer_next_token_is_not (parser->lexer,
16148 CPP_SCOPE)
16149 && cp_lexer_next_token_is_not (parser->lexer,
16150 CPP_CLOSE_PAREN)
16151 && !goto_p)
16152 outputs = cp_parser_asm_operand_list (parser);
16154 if (outputs == error_mark_node)
16155 invalid_outputs_p = true;
16157 /* If the next token is `::', there are no outputs, and the
16158 next token is the beginning of the inputs. */
16159 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16160 /* The inputs are coming next. */
16161 inputs_p = true;
16163 /* Look for inputs. */
16164 if (inputs_p
16165 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16167 /* Consume the `:' or `::'. */
16168 cp_lexer_consume_token (parser->lexer);
16169 /* Parse the output-operands. */
16170 if (cp_lexer_next_token_is_not (parser->lexer,
16171 CPP_COLON)
16172 && cp_lexer_next_token_is_not (parser->lexer,
16173 CPP_SCOPE)
16174 && cp_lexer_next_token_is_not (parser->lexer,
16175 CPP_CLOSE_PAREN))
16176 inputs = cp_parser_asm_operand_list (parser);
16178 if (inputs == error_mark_node)
16179 invalid_inputs_p = true;
16181 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16182 /* The clobbers are coming next. */
16183 clobbers_p = true;
16185 /* Look for clobbers. */
16186 if (clobbers_p
16187 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16189 clobbers_p = true;
16190 /* Consume the `:' or `::'. */
16191 cp_lexer_consume_token (parser->lexer);
16192 /* Parse the clobbers. */
16193 if (cp_lexer_next_token_is_not (parser->lexer,
16194 CPP_COLON)
16195 && cp_lexer_next_token_is_not (parser->lexer,
16196 CPP_CLOSE_PAREN))
16197 clobbers = cp_parser_asm_clobber_list (parser);
16199 else if (goto_p
16200 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16201 /* The labels are coming next. */
16202 labels_p = true;
16204 /* Look for labels. */
16205 if (labels_p
16206 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16208 labels_p = true;
16209 /* Consume the `:' or `::'. */
16210 cp_lexer_consume_token (parser->lexer);
16211 /* Parse the labels. */
16212 labels = cp_parser_asm_label_list (parser);
16215 if (goto_p && !labels_p)
16216 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16218 else if (goto_p)
16219 missing = RT_COLON_SCOPE;
16221 /* Look for the closing `)'. */
16222 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16223 missing ? missing : RT_CLOSE_PAREN))
16224 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16225 /*consume_paren=*/true);
16226 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16228 if (!invalid_inputs_p && !invalid_outputs_p)
16230 /* Create the ASM_EXPR. */
16231 if (parser->in_function_body)
16233 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16234 inputs, clobbers, labels);
16235 /* If the extended syntax was not used, mark the ASM_EXPR. */
16236 if (!extended_p)
16238 tree temp = asm_stmt;
16239 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16240 temp = TREE_OPERAND (temp, 0);
16242 ASM_INPUT_P (temp) = 1;
16245 else
16246 add_asm_node (string);
16250 /* Declarators [gram.dcl.decl] */
16252 /* Parse an init-declarator.
16254 init-declarator:
16255 declarator initializer [opt]
16257 GNU Extension:
16259 init-declarator:
16260 declarator asm-specification [opt] attributes [opt] initializer [opt]
16262 function-definition:
16263 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16264 function-body
16265 decl-specifier-seq [opt] declarator function-try-block
16267 GNU Extension:
16269 function-definition:
16270 __extension__ function-definition
16272 TM Extension:
16274 function-definition:
16275 decl-specifier-seq [opt] declarator function-transaction-block
16277 The DECL_SPECIFIERS apply to this declarator. Returns a
16278 representation of the entity declared. If MEMBER_P is TRUE, then
16279 this declarator appears in a class scope. The new DECL created by
16280 this declarator is returned.
16282 The CHECKS are access checks that should be performed once we know
16283 what entity is being declared (and, therefore, what classes have
16284 befriended it).
16286 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16287 for a function-definition here as well. If the declarator is a
16288 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16289 be TRUE upon return. By that point, the function-definition will
16290 have been completely parsed.
16292 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16293 is FALSE.
16295 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16296 parsed declaration if it is an uninitialized single declarator not followed
16297 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16298 if present, will not be consumed. If returned, this declarator will be
16299 created with SD_INITIALIZED but will not call cp_finish_decl. */
16301 static tree
16302 cp_parser_init_declarator (cp_parser* parser,
16303 cp_decl_specifier_seq *decl_specifiers,
16304 vec<deferred_access_check, va_gc> *checks,
16305 bool function_definition_allowed_p,
16306 bool member_p,
16307 int declares_class_or_enum,
16308 bool* function_definition_p,
16309 tree* maybe_range_for_decl)
16311 cp_token *token = NULL, *asm_spec_start_token = NULL,
16312 *attributes_start_token = NULL;
16313 cp_declarator *declarator;
16314 tree prefix_attributes;
16315 tree attributes = NULL;
16316 tree asm_specification;
16317 tree initializer;
16318 tree decl = NULL_TREE;
16319 tree scope;
16320 int is_initialized;
16321 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16322 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16323 "(...)". */
16324 enum cpp_ttype initialization_kind;
16325 bool is_direct_init = false;
16326 bool is_non_constant_init;
16327 int ctor_dtor_or_conv_p;
16328 bool friend_p;
16329 tree pushed_scope = NULL_TREE;
16330 bool range_for_decl_p = false;
16331 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16333 /* Gather the attributes that were provided with the
16334 decl-specifiers. */
16335 prefix_attributes = decl_specifiers->attributes;
16337 /* Assume that this is not the declarator for a function
16338 definition. */
16339 if (function_definition_p)
16340 *function_definition_p = false;
16342 /* Default arguments are only permitted for function parameters. */
16343 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16344 parser->default_arg_ok_p = false;
16346 /* Defer access checks while parsing the declarator; we cannot know
16347 what names are accessible until we know what is being
16348 declared. */
16349 resume_deferring_access_checks ();
16351 /* Parse the declarator. */
16352 token = cp_lexer_peek_token (parser->lexer);
16353 declarator
16354 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16355 &ctor_dtor_or_conv_p,
16356 /*parenthesized_p=*/NULL,
16357 member_p);
16358 /* Gather up the deferred checks. */
16359 stop_deferring_access_checks ();
16361 parser->default_arg_ok_p = saved_default_arg_ok_p;
16363 /* If the DECLARATOR was erroneous, there's no need to go
16364 further. */
16365 if (declarator == cp_error_declarator)
16366 return error_mark_node;
16368 /* Check that the number of template-parameter-lists is OK. */
16369 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16370 token->location))
16371 return error_mark_node;
16373 if (declares_class_or_enum & 2)
16374 cp_parser_check_for_definition_in_return_type (declarator,
16375 decl_specifiers->type,
16376 decl_specifiers->locations[ds_type_spec]);
16378 /* Figure out what scope the entity declared by the DECLARATOR is
16379 located in. `grokdeclarator' sometimes changes the scope, so
16380 we compute it now. */
16381 scope = get_scope_of_declarator (declarator);
16383 /* Perform any lookups in the declared type which were thought to be
16384 dependent, but are not in the scope of the declarator. */
16385 decl_specifiers->type
16386 = maybe_update_decl_type (decl_specifiers->type, scope);
16388 /* If we're allowing GNU extensions, look for an
16389 asm-specification. */
16390 if (cp_parser_allow_gnu_extensions_p (parser))
16392 /* Look for an asm-specification. */
16393 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16394 asm_specification = cp_parser_asm_specification_opt (parser);
16396 else
16397 asm_specification = NULL_TREE;
16399 /* Look for attributes. */
16400 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16401 attributes = cp_parser_attributes_opt (parser);
16403 /* Peek at the next token. */
16404 token = cp_lexer_peek_token (parser->lexer);
16406 if (function_declarator_p (declarator))
16408 /* Check to see if the token indicates the start of a
16409 function-definition. */
16410 if (cp_parser_token_starts_function_definition_p (token))
16412 if (!function_definition_allowed_p)
16414 /* If a function-definition should not appear here, issue an
16415 error message. */
16416 cp_parser_error (parser,
16417 "a function-definition is not allowed here");
16418 return error_mark_node;
16421 location_t func_brace_location
16422 = cp_lexer_peek_token (parser->lexer)->location;
16424 /* Neither attributes nor an asm-specification are allowed
16425 on a function-definition. */
16426 if (asm_specification)
16427 error_at (asm_spec_start_token->location,
16428 "an asm-specification is not allowed "
16429 "on a function-definition");
16430 if (attributes)
16431 error_at (attributes_start_token->location,
16432 "attributes are not allowed "
16433 "on a function-definition");
16434 /* This is a function-definition. */
16435 *function_definition_p = true;
16437 /* Parse the function definition. */
16438 if (member_p)
16439 decl = cp_parser_save_member_function_body (parser,
16440 decl_specifiers,
16441 declarator,
16442 prefix_attributes);
16443 else
16444 decl =
16445 (cp_parser_function_definition_from_specifiers_and_declarator
16446 (parser, decl_specifiers, prefix_attributes, declarator));
16448 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16450 /* This is where the prologue starts... */
16451 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16452 = func_brace_location;
16455 return decl;
16459 /* [dcl.dcl]
16461 Only in function declarations for constructors, destructors, and
16462 type conversions can the decl-specifier-seq be omitted.
16464 We explicitly postpone this check past the point where we handle
16465 function-definitions because we tolerate function-definitions
16466 that are missing their return types in some modes. */
16467 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16469 cp_parser_error (parser,
16470 "expected constructor, destructor, or type conversion");
16471 return error_mark_node;
16474 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16475 if (token->type == CPP_EQ
16476 || token->type == CPP_OPEN_PAREN
16477 || token->type == CPP_OPEN_BRACE)
16479 is_initialized = SD_INITIALIZED;
16480 initialization_kind = token->type;
16481 if (maybe_range_for_decl)
16482 *maybe_range_for_decl = error_mark_node;
16484 if (token->type == CPP_EQ
16485 && function_declarator_p (declarator))
16487 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16488 if (t2->keyword == RID_DEFAULT)
16489 is_initialized = SD_DEFAULTED;
16490 else if (t2->keyword == RID_DELETE)
16491 is_initialized = SD_DELETED;
16494 else
16496 /* If the init-declarator isn't initialized and isn't followed by a
16497 `,' or `;', it's not a valid init-declarator. */
16498 if (token->type != CPP_COMMA
16499 && token->type != CPP_SEMICOLON)
16501 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16502 range_for_decl_p = true;
16503 else
16505 cp_parser_error (parser, "expected initializer");
16506 return error_mark_node;
16509 is_initialized = SD_UNINITIALIZED;
16510 initialization_kind = CPP_EOF;
16513 /* Because start_decl has side-effects, we should only call it if we
16514 know we're going ahead. By this point, we know that we cannot
16515 possibly be looking at any other construct. */
16516 cp_parser_commit_to_tentative_parse (parser);
16518 /* If the decl specifiers were bad, issue an error now that we're
16519 sure this was intended to be a declarator. Then continue
16520 declaring the variable(s), as int, to try to cut down on further
16521 errors. */
16522 if (decl_specifiers->any_specifiers_p
16523 && decl_specifiers->type == error_mark_node)
16525 cp_parser_error (parser, "invalid type in declaration");
16526 decl_specifiers->type = integer_type_node;
16529 /* Check to see whether or not this declaration is a friend. */
16530 friend_p = cp_parser_friend_p (decl_specifiers);
16532 /* Enter the newly declared entry in the symbol table. If we're
16533 processing a declaration in a class-specifier, we wait until
16534 after processing the initializer. */
16535 if (!member_p)
16537 if (parser->in_unbraced_linkage_specification_p)
16538 decl_specifiers->storage_class = sc_extern;
16539 decl = start_decl (declarator, decl_specifiers,
16540 range_for_decl_p? SD_INITIALIZED : is_initialized,
16541 attributes, prefix_attributes, &pushed_scope);
16542 cp_finalize_omp_declare_simd (parser, decl);
16543 /* Adjust location of decl if declarator->id_loc is more appropriate:
16544 set, and decl wasn't merged with another decl, in which case its
16545 location would be different from input_location, and more accurate. */
16546 if (DECL_P (decl)
16547 && declarator->id_loc != UNKNOWN_LOCATION
16548 && DECL_SOURCE_LOCATION (decl) == input_location)
16549 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16551 else if (scope)
16552 /* Enter the SCOPE. That way unqualified names appearing in the
16553 initializer will be looked up in SCOPE. */
16554 pushed_scope = push_scope (scope);
16556 /* Perform deferred access control checks, now that we know in which
16557 SCOPE the declared entity resides. */
16558 if (!member_p && decl)
16560 tree saved_current_function_decl = NULL_TREE;
16562 /* If the entity being declared is a function, pretend that we
16563 are in its scope. If it is a `friend', it may have access to
16564 things that would not otherwise be accessible. */
16565 if (TREE_CODE (decl) == FUNCTION_DECL)
16567 saved_current_function_decl = current_function_decl;
16568 current_function_decl = decl;
16571 /* Perform access checks for template parameters. */
16572 cp_parser_perform_template_parameter_access_checks (checks);
16574 /* Perform the access control checks for the declarator and the
16575 decl-specifiers. */
16576 perform_deferred_access_checks (tf_warning_or_error);
16578 /* Restore the saved value. */
16579 if (TREE_CODE (decl) == FUNCTION_DECL)
16580 current_function_decl = saved_current_function_decl;
16583 /* Parse the initializer. */
16584 initializer = NULL_TREE;
16585 is_direct_init = false;
16586 is_non_constant_init = true;
16587 if (is_initialized)
16589 if (function_declarator_p (declarator))
16591 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16592 if (initialization_kind == CPP_EQ)
16593 initializer = cp_parser_pure_specifier (parser);
16594 else
16596 /* If the declaration was erroneous, we don't really
16597 know what the user intended, so just silently
16598 consume the initializer. */
16599 if (decl != error_mark_node)
16600 error_at (initializer_start_token->location,
16601 "initializer provided for function");
16602 cp_parser_skip_to_closing_parenthesis (parser,
16603 /*recovering=*/true,
16604 /*or_comma=*/false,
16605 /*consume_paren=*/true);
16608 else
16610 /* We want to record the extra mangling scope for in-class
16611 initializers of class members and initializers of static data
16612 member templates. The former involves deferring
16613 parsing of the initializer until end of class as with default
16614 arguments. So right here we only handle the latter. */
16615 if (!member_p && processing_template_decl)
16616 start_lambda_scope (decl);
16617 initializer = cp_parser_initializer (parser,
16618 &is_direct_init,
16619 &is_non_constant_init);
16620 if (!member_p && processing_template_decl)
16621 finish_lambda_scope ();
16622 if (initializer == error_mark_node)
16623 cp_parser_skip_to_end_of_statement (parser);
16627 /* The old parser allows attributes to appear after a parenthesized
16628 initializer. Mark Mitchell proposed removing this functionality
16629 on the GCC mailing lists on 2002-08-13. This parser accepts the
16630 attributes -- but ignores them. */
16631 if (cp_parser_allow_gnu_extensions_p (parser)
16632 && initialization_kind == CPP_OPEN_PAREN)
16633 if (cp_parser_attributes_opt (parser))
16634 warning (OPT_Wattributes,
16635 "attributes after parenthesized initializer ignored");
16637 /* For an in-class declaration, use `grokfield' to create the
16638 declaration. */
16639 if (member_p)
16641 if (pushed_scope)
16643 pop_scope (pushed_scope);
16644 pushed_scope = NULL_TREE;
16646 decl = grokfield (declarator, decl_specifiers,
16647 initializer, !is_non_constant_init,
16648 /*asmspec=*/NULL_TREE,
16649 chainon (attributes, prefix_attributes));
16650 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16651 cp_parser_save_default_args (parser, decl);
16652 cp_finalize_omp_declare_simd (parser, decl);
16655 /* Finish processing the declaration. But, skip member
16656 declarations. */
16657 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16659 cp_finish_decl (decl,
16660 initializer, !is_non_constant_init,
16661 asm_specification,
16662 /* If the initializer is in parentheses, then this is
16663 a direct-initialization, which means that an
16664 `explicit' constructor is OK. Otherwise, an
16665 `explicit' constructor cannot be used. */
16666 ((is_direct_init || !is_initialized)
16667 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16669 else if ((cxx_dialect != cxx98) && friend_p
16670 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16671 /* Core issue #226 (C++0x only): A default template-argument
16672 shall not be specified in a friend class template
16673 declaration. */
16674 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16675 /*is_partial=*/false, /*is_friend_decl=*/1);
16677 if (!friend_p && pushed_scope)
16678 pop_scope (pushed_scope);
16680 if (function_declarator_p (declarator)
16681 && parser->fully_implicit_function_template_p)
16683 if (member_p)
16684 decl = finish_fully_implicit_template (parser, decl);
16685 else
16686 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16689 return decl;
16692 /* Parse a declarator.
16694 declarator:
16695 direct-declarator
16696 ptr-operator declarator
16698 abstract-declarator:
16699 ptr-operator abstract-declarator [opt]
16700 direct-abstract-declarator
16702 GNU Extensions:
16704 declarator:
16705 attributes [opt] direct-declarator
16706 attributes [opt] ptr-operator declarator
16708 abstract-declarator:
16709 attributes [opt] ptr-operator abstract-declarator [opt]
16710 attributes [opt] direct-abstract-declarator
16712 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16713 detect constructor, destructor or conversion operators. It is set
16714 to -1 if the declarator is a name, and +1 if it is a
16715 function. Otherwise it is set to zero. Usually you just want to
16716 test for >0, but internally the negative value is used.
16718 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16719 a decl-specifier-seq unless it declares a constructor, destructor,
16720 or conversion. It might seem that we could check this condition in
16721 semantic analysis, rather than parsing, but that makes it difficult
16722 to handle something like `f()'. We want to notice that there are
16723 no decl-specifiers, and therefore realize that this is an
16724 expression, not a declaration.)
16726 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16727 the declarator is a direct-declarator of the form "(...)".
16729 MEMBER_P is true iff this declarator is a member-declarator. */
16731 static cp_declarator *
16732 cp_parser_declarator (cp_parser* parser,
16733 cp_parser_declarator_kind dcl_kind,
16734 int* ctor_dtor_or_conv_p,
16735 bool* parenthesized_p,
16736 bool member_p)
16738 cp_declarator *declarator;
16739 enum tree_code code;
16740 cp_cv_quals cv_quals;
16741 tree class_type;
16742 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16744 /* Assume this is not a constructor, destructor, or type-conversion
16745 operator. */
16746 if (ctor_dtor_or_conv_p)
16747 *ctor_dtor_or_conv_p = 0;
16749 if (cp_parser_allow_gnu_extensions_p (parser))
16750 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16752 /* Check for the ptr-operator production. */
16753 cp_parser_parse_tentatively (parser);
16754 /* Parse the ptr-operator. */
16755 code = cp_parser_ptr_operator (parser,
16756 &class_type,
16757 &cv_quals,
16758 &std_attributes);
16760 /* If that worked, then we have a ptr-operator. */
16761 if (cp_parser_parse_definitely (parser))
16763 /* If a ptr-operator was found, then this declarator was not
16764 parenthesized. */
16765 if (parenthesized_p)
16766 *parenthesized_p = true;
16767 /* The dependent declarator is optional if we are parsing an
16768 abstract-declarator. */
16769 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16770 cp_parser_parse_tentatively (parser);
16772 /* Parse the dependent declarator. */
16773 declarator = cp_parser_declarator (parser, dcl_kind,
16774 /*ctor_dtor_or_conv_p=*/NULL,
16775 /*parenthesized_p=*/NULL,
16776 /*member_p=*/false);
16778 /* If we are parsing an abstract-declarator, we must handle the
16779 case where the dependent declarator is absent. */
16780 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16781 && !cp_parser_parse_definitely (parser))
16782 declarator = NULL;
16784 declarator = cp_parser_make_indirect_declarator
16785 (code, class_type, cv_quals, declarator, std_attributes);
16787 /* Everything else is a direct-declarator. */
16788 else
16790 if (parenthesized_p)
16791 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16792 CPP_OPEN_PAREN);
16793 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16794 ctor_dtor_or_conv_p,
16795 member_p);
16798 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16799 declarator->attributes = gnu_attributes;
16800 return declarator;
16803 /* Parse a direct-declarator or direct-abstract-declarator.
16805 direct-declarator:
16806 declarator-id
16807 direct-declarator ( parameter-declaration-clause )
16808 cv-qualifier-seq [opt]
16809 ref-qualifier [opt]
16810 exception-specification [opt]
16811 direct-declarator [ constant-expression [opt] ]
16812 ( declarator )
16814 direct-abstract-declarator:
16815 direct-abstract-declarator [opt]
16816 ( parameter-declaration-clause )
16817 cv-qualifier-seq [opt]
16818 ref-qualifier [opt]
16819 exception-specification [opt]
16820 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16821 ( abstract-declarator )
16823 Returns a representation of the declarator. DCL_KIND is
16824 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16825 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16826 we are parsing a direct-declarator. It is
16827 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16828 of ambiguity we prefer an abstract declarator, as per
16829 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16830 cp_parser_declarator. */
16832 static cp_declarator *
16833 cp_parser_direct_declarator (cp_parser* parser,
16834 cp_parser_declarator_kind dcl_kind,
16835 int* ctor_dtor_or_conv_p,
16836 bool member_p)
16838 cp_token *token;
16839 cp_declarator *declarator = NULL;
16840 tree scope = NULL_TREE;
16841 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16842 bool saved_in_declarator_p = parser->in_declarator_p;
16843 bool first = true;
16844 tree pushed_scope = NULL_TREE;
16846 while (true)
16848 /* Peek at the next token. */
16849 token = cp_lexer_peek_token (parser->lexer);
16850 if (token->type == CPP_OPEN_PAREN)
16852 /* This is either a parameter-declaration-clause, or a
16853 parenthesized declarator. When we know we are parsing a
16854 named declarator, it must be a parenthesized declarator
16855 if FIRST is true. For instance, `(int)' is a
16856 parameter-declaration-clause, with an omitted
16857 direct-abstract-declarator. But `((*))', is a
16858 parenthesized abstract declarator. Finally, when T is a
16859 template parameter `(T)' is a
16860 parameter-declaration-clause, and not a parenthesized
16861 named declarator.
16863 We first try and parse a parameter-declaration-clause,
16864 and then try a nested declarator (if FIRST is true).
16866 It is not an error for it not to be a
16867 parameter-declaration-clause, even when FIRST is
16868 false. Consider,
16870 int i (int);
16871 int i (3);
16873 The first is the declaration of a function while the
16874 second is the definition of a variable, including its
16875 initializer.
16877 Having seen only the parenthesis, we cannot know which of
16878 these two alternatives should be selected. Even more
16879 complex are examples like:
16881 int i (int (a));
16882 int i (int (3));
16884 The former is a function-declaration; the latter is a
16885 variable initialization.
16887 Thus again, we try a parameter-declaration-clause, and if
16888 that fails, we back out and return. */
16890 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16892 tree params;
16893 unsigned saved_num_template_parameter_lists;
16894 bool is_declarator = false;
16896 /* In a member-declarator, the only valid interpretation
16897 of a parenthesis is the start of a
16898 parameter-declaration-clause. (It is invalid to
16899 initialize a static data member with a parenthesized
16900 initializer; only the "=" form of initialization is
16901 permitted.) */
16902 if (!member_p)
16903 cp_parser_parse_tentatively (parser);
16905 /* Consume the `('. */
16906 cp_lexer_consume_token (parser->lexer);
16907 if (first)
16909 /* If this is going to be an abstract declarator, we're
16910 in a declarator and we can't have default args. */
16911 parser->default_arg_ok_p = false;
16912 parser->in_declarator_p = true;
16915 /* Inside the function parameter list, surrounding
16916 template-parameter-lists do not apply. */
16917 saved_num_template_parameter_lists
16918 = parser->num_template_parameter_lists;
16919 parser->num_template_parameter_lists = 0;
16921 begin_scope (sk_function_parms, NULL_TREE);
16923 /* Parse the parameter-declaration-clause. */
16924 params = cp_parser_parameter_declaration_clause (parser);
16926 /* Restore saved template parameter lists accounting for implicit
16927 template parameters. */
16928 parser->num_template_parameter_lists
16929 += saved_num_template_parameter_lists;
16931 /* Consume the `)'. */
16932 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16934 /* If all went well, parse the cv-qualifier-seq,
16935 ref-qualifier and the exception-specification. */
16936 if (member_p || cp_parser_parse_definitely (parser))
16938 cp_cv_quals cv_quals;
16939 cp_virt_specifiers virt_specifiers;
16940 cp_ref_qualifier ref_qual;
16941 tree exception_specification;
16942 tree late_return;
16943 tree attrs;
16944 bool memfn = (member_p || (pushed_scope
16945 && CLASS_TYPE_P (pushed_scope)));
16947 is_declarator = true;
16949 if (ctor_dtor_or_conv_p)
16950 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16951 first = false;
16953 /* Parse the cv-qualifier-seq. */
16954 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16955 /* Parse the ref-qualifier. */
16956 ref_qual = cp_parser_ref_qualifier_opt (parser);
16957 /* And the exception-specification. */
16958 exception_specification
16959 = cp_parser_exception_specification_opt (parser);
16961 attrs = cp_parser_std_attribute_spec_seq (parser);
16963 late_return = (cp_parser_late_return_type_opt
16964 (parser, declarator,
16965 memfn ? cv_quals : -1));
16968 /* Parse the virt-specifier-seq. */
16969 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16971 /* Create the function-declarator. */
16972 declarator = make_call_declarator (declarator,
16973 params,
16974 cv_quals,
16975 virt_specifiers,
16976 ref_qual,
16977 exception_specification,
16978 late_return);
16979 declarator->std_attributes = attrs;
16980 /* Any subsequent parameter lists are to do with
16981 return type, so are not those of the declared
16982 function. */
16983 parser->default_arg_ok_p = false;
16986 /* Remove the function parms from scope. */
16987 pop_bindings_and_leave_scope ();
16989 if (is_declarator)
16990 /* Repeat the main loop. */
16991 continue;
16994 /* If this is the first, we can try a parenthesized
16995 declarator. */
16996 if (first)
16998 bool saved_in_type_id_in_expr_p;
17000 parser->default_arg_ok_p = saved_default_arg_ok_p;
17001 parser->in_declarator_p = saved_in_declarator_p;
17003 /* Consume the `('. */
17004 cp_lexer_consume_token (parser->lexer);
17005 /* Parse the nested declarator. */
17006 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17007 parser->in_type_id_in_expr_p = true;
17008 declarator
17009 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17010 /*parenthesized_p=*/NULL,
17011 member_p);
17012 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17013 first = false;
17014 /* Expect a `)'. */
17015 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17016 declarator = cp_error_declarator;
17017 if (declarator == cp_error_declarator)
17018 break;
17020 goto handle_declarator;
17022 /* Otherwise, we must be done. */
17023 else
17024 break;
17026 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17027 && token->type == CPP_OPEN_SQUARE
17028 && !cp_next_tokens_can_be_attribute_p (parser))
17030 /* Parse an array-declarator. */
17031 tree bounds, attrs;
17033 if (ctor_dtor_or_conv_p)
17034 *ctor_dtor_or_conv_p = 0;
17036 first = false;
17037 parser->default_arg_ok_p = false;
17038 parser->in_declarator_p = true;
17039 /* Consume the `['. */
17040 cp_lexer_consume_token (parser->lexer);
17041 /* Peek at the next token. */
17042 token = cp_lexer_peek_token (parser->lexer);
17043 /* If the next token is `]', then there is no
17044 constant-expression. */
17045 if (token->type != CPP_CLOSE_SQUARE)
17047 bool non_constant_p;
17048 bounds
17049 = cp_parser_constant_expression (parser,
17050 /*allow_non_constant=*/true,
17051 &non_constant_p);
17052 if (!non_constant_p)
17053 /* OK */;
17054 else if (error_operand_p (bounds))
17055 /* Already gave an error. */;
17056 else if (!parser->in_function_body
17057 || current_binding_level->kind == sk_function_parms)
17059 /* Normally, the array bound must be an integral constant
17060 expression. However, as an extension, we allow VLAs
17061 in function scopes as long as they aren't part of a
17062 parameter declaration. */
17063 cp_parser_error (parser,
17064 "array bound is not an integer constant");
17065 bounds = error_mark_node;
17067 else if (processing_template_decl)
17069 /* Remember this wasn't a constant-expression. */
17070 bounds = build_nop (TREE_TYPE (bounds), bounds);
17071 TREE_SIDE_EFFECTS (bounds) = 1;
17074 else
17075 bounds = NULL_TREE;
17076 /* Look for the closing `]'. */
17077 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17079 declarator = cp_error_declarator;
17080 break;
17083 attrs = cp_parser_std_attribute_spec_seq (parser);
17084 declarator = make_array_declarator (declarator, bounds);
17085 declarator->std_attributes = attrs;
17087 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17090 tree qualifying_scope;
17091 tree unqualified_name;
17092 tree attrs;
17093 special_function_kind sfk;
17094 bool abstract_ok;
17095 bool pack_expansion_p = false;
17096 cp_token *declarator_id_start_token;
17098 /* Parse a declarator-id */
17099 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17100 if (abstract_ok)
17102 cp_parser_parse_tentatively (parser);
17104 /* If we see an ellipsis, we should be looking at a
17105 parameter pack. */
17106 if (token->type == CPP_ELLIPSIS)
17108 /* Consume the `...' */
17109 cp_lexer_consume_token (parser->lexer);
17111 pack_expansion_p = true;
17115 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17116 unqualified_name
17117 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17118 qualifying_scope = parser->scope;
17119 if (abstract_ok)
17121 bool okay = false;
17123 if (!unqualified_name && pack_expansion_p)
17125 /* Check whether an error occurred. */
17126 okay = !cp_parser_error_occurred (parser);
17128 /* We already consumed the ellipsis to mark a
17129 parameter pack, but we have no way to report it,
17130 so abort the tentative parse. We will be exiting
17131 immediately anyway. */
17132 cp_parser_abort_tentative_parse (parser);
17134 else
17135 okay = cp_parser_parse_definitely (parser);
17137 if (!okay)
17138 unqualified_name = error_mark_node;
17139 else if (unqualified_name
17140 && (qualifying_scope
17141 || (!identifier_p (unqualified_name))))
17143 cp_parser_error (parser, "expected unqualified-id");
17144 unqualified_name = error_mark_node;
17148 if (!unqualified_name)
17149 return NULL;
17150 if (unqualified_name == error_mark_node)
17152 declarator = cp_error_declarator;
17153 pack_expansion_p = false;
17154 declarator->parameter_pack_p = false;
17155 break;
17158 attrs = cp_parser_std_attribute_spec_seq (parser);
17160 if (qualifying_scope && at_namespace_scope_p ()
17161 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17163 /* In the declaration of a member of a template class
17164 outside of the class itself, the SCOPE will sometimes
17165 be a TYPENAME_TYPE. For example, given:
17167 template <typename T>
17168 int S<T>::R::i = 3;
17170 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17171 this context, we must resolve S<T>::R to an ordinary
17172 type, rather than a typename type.
17174 The reason we normally avoid resolving TYPENAME_TYPEs
17175 is that a specialization of `S' might render
17176 `S<T>::R' not a type. However, if `S' is
17177 specialized, then this `i' will not be used, so there
17178 is no harm in resolving the types here. */
17179 tree type;
17181 /* Resolve the TYPENAME_TYPE. */
17182 type = resolve_typename_type (qualifying_scope,
17183 /*only_current_p=*/false);
17184 /* If that failed, the declarator is invalid. */
17185 if (TREE_CODE (type) == TYPENAME_TYPE)
17187 if (typedef_variant_p (type))
17188 error_at (declarator_id_start_token->location,
17189 "cannot define member of dependent typedef "
17190 "%qT", type);
17191 else
17192 error_at (declarator_id_start_token->location,
17193 "%<%T::%E%> is not a type",
17194 TYPE_CONTEXT (qualifying_scope),
17195 TYPE_IDENTIFIER (qualifying_scope));
17197 qualifying_scope = type;
17200 sfk = sfk_none;
17202 if (unqualified_name)
17204 tree class_type;
17206 if (qualifying_scope
17207 && CLASS_TYPE_P (qualifying_scope))
17208 class_type = qualifying_scope;
17209 else
17210 class_type = current_class_type;
17212 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17214 tree name_type = TREE_TYPE (unqualified_name);
17215 if (class_type && same_type_p (name_type, class_type))
17217 if (qualifying_scope
17218 && CLASSTYPE_USE_TEMPLATE (name_type))
17220 error_at (declarator_id_start_token->location,
17221 "invalid use of constructor as a template");
17222 inform (declarator_id_start_token->location,
17223 "use %<%T::%D%> instead of %<%T::%D%> to "
17224 "name the constructor in a qualified name",
17225 class_type,
17226 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17227 class_type, name_type);
17228 declarator = cp_error_declarator;
17229 break;
17231 else
17232 unqualified_name = constructor_name (class_type);
17234 else
17236 /* We do not attempt to print the declarator
17237 here because we do not have enough
17238 information about its original syntactic
17239 form. */
17240 cp_parser_error (parser, "invalid declarator");
17241 declarator = cp_error_declarator;
17242 break;
17246 if (class_type)
17248 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17249 sfk = sfk_destructor;
17250 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17251 sfk = sfk_conversion;
17252 else if (/* There's no way to declare a constructor
17253 for an anonymous type, even if the type
17254 got a name for linkage purposes. */
17255 !TYPE_WAS_ANONYMOUS (class_type)
17256 && constructor_name_p (unqualified_name,
17257 class_type))
17259 unqualified_name = constructor_name (class_type);
17260 sfk = sfk_constructor;
17262 else if (is_overloaded_fn (unqualified_name)
17263 && DECL_CONSTRUCTOR_P (get_first_fn
17264 (unqualified_name)))
17265 sfk = sfk_constructor;
17267 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17268 *ctor_dtor_or_conv_p = -1;
17271 declarator = make_id_declarator (qualifying_scope,
17272 unqualified_name,
17273 sfk);
17274 declarator->std_attributes = attrs;
17275 declarator->id_loc = token->location;
17276 declarator->parameter_pack_p = pack_expansion_p;
17278 if (pack_expansion_p)
17279 maybe_warn_variadic_templates ();
17282 handle_declarator:;
17283 scope = get_scope_of_declarator (declarator);
17284 if (scope)
17286 /* Any names that appear after the declarator-id for a
17287 member are looked up in the containing scope. */
17288 if (at_function_scope_p ())
17290 /* But declarations with qualified-ids can't appear in a
17291 function. */
17292 cp_parser_error (parser, "qualified-id in declaration");
17293 break;
17295 pushed_scope = push_scope (scope);
17297 parser->in_declarator_p = true;
17298 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17299 || (declarator && declarator->kind == cdk_id))
17300 /* Default args are only allowed on function
17301 declarations. */
17302 parser->default_arg_ok_p = saved_default_arg_ok_p;
17303 else
17304 parser->default_arg_ok_p = false;
17306 first = false;
17308 /* We're done. */
17309 else
17310 break;
17313 /* For an abstract declarator, we might wind up with nothing at this
17314 point. That's an error; the declarator is not optional. */
17315 if (!declarator)
17316 cp_parser_error (parser, "expected declarator");
17318 /* If we entered a scope, we must exit it now. */
17319 if (pushed_scope)
17320 pop_scope (pushed_scope);
17322 parser->default_arg_ok_p = saved_default_arg_ok_p;
17323 parser->in_declarator_p = saved_in_declarator_p;
17325 return declarator;
17328 /* Parse a ptr-operator.
17330 ptr-operator:
17331 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17332 * cv-qualifier-seq [opt]
17334 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17335 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17337 GNU Extension:
17339 ptr-operator:
17340 & cv-qualifier-seq [opt]
17342 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17343 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17344 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17345 filled in with the TYPE containing the member. *CV_QUALS is
17346 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17347 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17348 Note that the tree codes returned by this function have nothing
17349 to do with the types of trees that will be eventually be created
17350 to represent the pointer or reference type being parsed. They are
17351 just constants with suggestive names. */
17352 static enum tree_code
17353 cp_parser_ptr_operator (cp_parser* parser,
17354 tree* type,
17355 cp_cv_quals *cv_quals,
17356 tree *attributes)
17358 enum tree_code code = ERROR_MARK;
17359 cp_token *token;
17360 tree attrs = NULL_TREE;
17362 /* Assume that it's not a pointer-to-member. */
17363 *type = NULL_TREE;
17364 /* And that there are no cv-qualifiers. */
17365 *cv_quals = TYPE_UNQUALIFIED;
17367 /* Peek at the next token. */
17368 token = cp_lexer_peek_token (parser->lexer);
17370 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17371 if (token->type == CPP_MULT)
17372 code = INDIRECT_REF;
17373 else if (token->type == CPP_AND)
17374 code = ADDR_EXPR;
17375 else if ((cxx_dialect != cxx98) &&
17376 token->type == CPP_AND_AND) /* C++0x only */
17377 code = NON_LVALUE_EXPR;
17379 if (code != ERROR_MARK)
17381 /* Consume the `*', `&' or `&&'. */
17382 cp_lexer_consume_token (parser->lexer);
17384 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17385 `&', if we are allowing GNU extensions. (The only qualifier
17386 that can legally appear after `&' is `restrict', but that is
17387 enforced during semantic analysis. */
17388 if (code == INDIRECT_REF
17389 || cp_parser_allow_gnu_extensions_p (parser))
17390 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17392 attrs = cp_parser_std_attribute_spec_seq (parser);
17393 if (attributes != NULL)
17394 *attributes = attrs;
17396 else
17398 /* Try the pointer-to-member case. */
17399 cp_parser_parse_tentatively (parser);
17400 /* Look for the optional `::' operator. */
17401 cp_parser_global_scope_opt (parser,
17402 /*current_scope_valid_p=*/false);
17403 /* Look for the nested-name specifier. */
17404 token = cp_lexer_peek_token (parser->lexer);
17405 cp_parser_nested_name_specifier (parser,
17406 /*typename_keyword_p=*/false,
17407 /*check_dependency_p=*/true,
17408 /*type_p=*/false,
17409 /*is_declaration=*/false);
17410 /* If we found it, and the next token is a `*', then we are
17411 indeed looking at a pointer-to-member operator. */
17412 if (!cp_parser_error_occurred (parser)
17413 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17415 /* Indicate that the `*' operator was used. */
17416 code = INDIRECT_REF;
17418 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17419 error_at (token->location, "%qD is a namespace", parser->scope);
17420 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17421 error_at (token->location, "cannot form pointer to member of "
17422 "non-class %q#T", parser->scope);
17423 else
17425 /* The type of which the member is a member is given by the
17426 current SCOPE. */
17427 *type = parser->scope;
17428 /* The next name will not be qualified. */
17429 parser->scope = NULL_TREE;
17430 parser->qualifying_scope = NULL_TREE;
17431 parser->object_scope = NULL_TREE;
17432 /* Look for optional c++11 attributes. */
17433 attrs = cp_parser_std_attribute_spec_seq (parser);
17434 if (attributes != NULL)
17435 *attributes = attrs;
17436 /* Look for the optional cv-qualifier-seq. */
17437 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17440 /* If that didn't work we don't have a ptr-operator. */
17441 if (!cp_parser_parse_definitely (parser))
17442 cp_parser_error (parser, "expected ptr-operator");
17445 return code;
17448 /* Parse an (optional) cv-qualifier-seq.
17450 cv-qualifier-seq:
17451 cv-qualifier cv-qualifier-seq [opt]
17453 cv-qualifier:
17454 const
17455 volatile
17457 GNU Extension:
17459 cv-qualifier:
17460 __restrict__
17462 Returns a bitmask representing the cv-qualifiers. */
17464 static cp_cv_quals
17465 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17467 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17469 while (true)
17471 cp_token *token;
17472 cp_cv_quals cv_qualifier;
17474 /* Peek at the next token. */
17475 token = cp_lexer_peek_token (parser->lexer);
17476 /* See if it's a cv-qualifier. */
17477 switch (token->keyword)
17479 case RID_CONST:
17480 cv_qualifier = TYPE_QUAL_CONST;
17481 break;
17483 case RID_VOLATILE:
17484 cv_qualifier = TYPE_QUAL_VOLATILE;
17485 break;
17487 case RID_RESTRICT:
17488 cv_qualifier = TYPE_QUAL_RESTRICT;
17489 break;
17491 default:
17492 cv_qualifier = TYPE_UNQUALIFIED;
17493 break;
17496 if (!cv_qualifier)
17497 break;
17499 if (cv_quals & cv_qualifier)
17501 error_at (token->location, "duplicate cv-qualifier");
17502 cp_lexer_purge_token (parser->lexer);
17504 else
17506 cp_lexer_consume_token (parser->lexer);
17507 cv_quals |= cv_qualifier;
17511 return cv_quals;
17514 /* Parse an (optional) ref-qualifier
17516 ref-qualifier:
17520 Returns cp_ref_qualifier representing ref-qualifier. */
17522 static cp_ref_qualifier
17523 cp_parser_ref_qualifier_opt (cp_parser* parser)
17525 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17527 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17528 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17529 return ref_qual;
17531 while (true)
17533 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17534 cp_token *token = cp_lexer_peek_token (parser->lexer);
17536 switch (token->type)
17538 case CPP_AND:
17539 curr_ref_qual = REF_QUAL_LVALUE;
17540 break;
17542 case CPP_AND_AND:
17543 curr_ref_qual = REF_QUAL_RVALUE;
17544 break;
17546 default:
17547 curr_ref_qual = REF_QUAL_NONE;
17548 break;
17551 if (!curr_ref_qual)
17552 break;
17553 else if (ref_qual)
17555 error_at (token->location, "multiple ref-qualifiers");
17556 cp_lexer_purge_token (parser->lexer);
17558 else
17560 ref_qual = curr_ref_qual;
17561 cp_lexer_consume_token (parser->lexer);
17565 return ref_qual;
17568 /* Parse an (optional) virt-specifier-seq.
17570 virt-specifier-seq:
17571 virt-specifier virt-specifier-seq [opt]
17573 virt-specifier:
17574 override
17575 final
17577 Returns a bitmask representing the virt-specifiers. */
17579 static cp_virt_specifiers
17580 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17582 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17584 while (true)
17586 cp_token *token;
17587 cp_virt_specifiers virt_specifier;
17589 /* Peek at the next token. */
17590 token = cp_lexer_peek_token (parser->lexer);
17591 /* See if it's a virt-specifier-qualifier. */
17592 if (token->type != CPP_NAME)
17593 break;
17594 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17596 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17597 virt_specifier = VIRT_SPEC_OVERRIDE;
17599 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17601 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17602 virt_specifier = VIRT_SPEC_FINAL;
17604 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17606 virt_specifier = VIRT_SPEC_FINAL;
17608 else
17609 break;
17611 if (virt_specifiers & virt_specifier)
17613 error_at (token->location, "duplicate virt-specifier");
17614 cp_lexer_purge_token (parser->lexer);
17616 else
17618 cp_lexer_consume_token (parser->lexer);
17619 virt_specifiers |= virt_specifier;
17622 return virt_specifiers;
17625 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17626 is in scope even though it isn't real. */
17628 static void
17629 inject_this_parameter (tree ctype, cp_cv_quals quals)
17631 tree this_parm;
17633 if (current_class_ptr)
17635 /* We don't clear this between NSDMIs. Is it already what we want? */
17636 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17637 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17638 && cp_type_quals (type) == quals)
17639 return;
17642 this_parm = build_this_parm (ctype, quals);
17643 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17644 current_class_ptr = NULL_TREE;
17645 current_class_ref
17646 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17647 current_class_ptr = this_parm;
17650 /* Return true iff our current scope is a non-static data member
17651 initializer. */
17653 bool
17654 parsing_nsdmi (void)
17656 /* We recognize NSDMI context by the context-less 'this' pointer set up
17657 by the function above. */
17658 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17659 return true;
17660 return false;
17663 /* Parse a late-specified return type, if any. This is not a separate
17664 non-terminal, but part of a function declarator, which looks like
17666 -> trailing-type-specifier-seq abstract-declarator(opt)
17668 Returns the type indicated by the type-id.
17670 In addition to this this parses any queued up omp declare simd
17671 clauses.
17673 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17674 function. */
17676 static tree
17677 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17678 cp_cv_quals quals)
17680 cp_token *token;
17681 tree type = NULL_TREE;
17682 bool declare_simd_p = (parser->omp_declare_simd
17683 && declarator
17684 && declarator->kind == cdk_id);
17686 /* Peek at the next token. */
17687 token = cp_lexer_peek_token (parser->lexer);
17688 /* A late-specified return type is indicated by an initial '->'. */
17689 if (token->type != CPP_DEREF && !declare_simd_p)
17690 return NULL_TREE;
17692 tree save_ccp = current_class_ptr;
17693 tree save_ccr = current_class_ref;
17694 if (quals >= 0)
17696 /* DR 1207: 'this' is in scope in the trailing return type. */
17697 inject_this_parameter (current_class_type, quals);
17700 if (token->type == CPP_DEREF)
17702 /* Consume the ->. */
17703 cp_lexer_consume_token (parser->lexer);
17705 type = cp_parser_trailing_type_id (parser);
17708 if (declare_simd_p)
17709 declarator->std_attributes
17710 = cp_parser_late_parsing_omp_declare_simd (parser,
17711 declarator->std_attributes);
17713 if (quals >= 0)
17715 current_class_ptr = save_ccp;
17716 current_class_ref = save_ccr;
17719 return type;
17722 /* Parse a declarator-id.
17724 declarator-id:
17725 id-expression
17726 :: [opt] nested-name-specifier [opt] type-name
17728 In the `id-expression' case, the value returned is as for
17729 cp_parser_id_expression if the id-expression was an unqualified-id.
17730 If the id-expression was a qualified-id, then a SCOPE_REF is
17731 returned. The first operand is the scope (either a NAMESPACE_DECL
17732 or TREE_TYPE), but the second is still just a representation of an
17733 unqualified-id. */
17735 static tree
17736 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17738 tree id;
17739 /* The expression must be an id-expression. Assume that qualified
17740 names are the names of types so that:
17742 template <class T>
17743 int S<T>::R::i = 3;
17745 will work; we must treat `S<T>::R' as the name of a type.
17746 Similarly, assume that qualified names are templates, where
17747 required, so that:
17749 template <class T>
17750 int S<T>::R<T>::i = 3;
17752 will work, too. */
17753 id = cp_parser_id_expression (parser,
17754 /*template_keyword_p=*/false,
17755 /*check_dependency_p=*/false,
17756 /*template_p=*/NULL,
17757 /*declarator_p=*/true,
17758 optional_p);
17759 if (id && BASELINK_P (id))
17760 id = BASELINK_FUNCTIONS (id);
17761 return id;
17764 /* Parse a type-id.
17766 type-id:
17767 type-specifier-seq abstract-declarator [opt]
17769 Returns the TYPE specified. */
17771 static tree
17772 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17773 bool is_trailing_return)
17775 cp_decl_specifier_seq type_specifier_seq;
17776 cp_declarator *abstract_declarator;
17778 /* Parse the type-specifier-seq. */
17779 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17780 is_trailing_return,
17781 &type_specifier_seq);
17782 if (type_specifier_seq.type == error_mark_node)
17783 return error_mark_node;
17785 /* There might or might not be an abstract declarator. */
17786 cp_parser_parse_tentatively (parser);
17787 /* Look for the declarator. */
17788 abstract_declarator
17789 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17790 /*parenthesized_p=*/NULL,
17791 /*member_p=*/false);
17792 /* Check to see if there really was a declarator. */
17793 if (!cp_parser_parse_definitely (parser))
17794 abstract_declarator = NULL;
17796 if (type_specifier_seq.type
17797 && cxx_dialect < cxx1y
17798 && type_uses_auto (type_specifier_seq.type))
17800 /* A type-id with type 'auto' is only ok if the abstract declarator
17801 is a function declarator with a late-specified return type. */
17802 if (abstract_declarator
17803 && abstract_declarator->kind == cdk_function
17804 && abstract_declarator->u.function.late_return_type)
17805 /* OK */;
17806 else
17808 error ("invalid use of %<auto%>");
17809 return error_mark_node;
17813 return groktypename (&type_specifier_seq, abstract_declarator,
17814 is_template_arg);
17817 static tree cp_parser_type_id (cp_parser *parser)
17819 return cp_parser_type_id_1 (parser, false, false);
17822 static tree cp_parser_template_type_arg (cp_parser *parser)
17824 tree r;
17825 const char *saved_message = parser->type_definition_forbidden_message;
17826 parser->type_definition_forbidden_message
17827 = G_("types may not be defined in template arguments");
17828 r = cp_parser_type_id_1 (parser, true, false);
17829 parser->type_definition_forbidden_message = saved_message;
17830 return r;
17833 static tree cp_parser_trailing_type_id (cp_parser *parser)
17835 return cp_parser_type_id_1 (parser, false, true);
17838 /* Parse a type-specifier-seq.
17840 type-specifier-seq:
17841 type-specifier type-specifier-seq [opt]
17843 GNU extension:
17845 type-specifier-seq:
17846 attributes type-specifier-seq [opt]
17848 If IS_DECLARATION is true, we are at the start of a "condition" or
17849 exception-declaration, so we might be followed by a declarator-id.
17851 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17852 i.e. we've just seen "->".
17854 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17856 static void
17857 cp_parser_type_specifier_seq (cp_parser* parser,
17858 bool is_declaration,
17859 bool is_trailing_return,
17860 cp_decl_specifier_seq *type_specifier_seq)
17862 bool seen_type_specifier = false;
17863 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17864 cp_token *start_token = NULL;
17866 /* Clear the TYPE_SPECIFIER_SEQ. */
17867 clear_decl_specs (type_specifier_seq);
17869 /* In the context of a trailing return type, enum E { } is an
17870 elaborated-type-specifier followed by a function-body, not an
17871 enum-specifier. */
17872 if (is_trailing_return)
17873 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17875 /* Parse the type-specifiers and attributes. */
17876 while (true)
17878 tree type_specifier;
17879 bool is_cv_qualifier;
17881 /* Check for attributes first. */
17882 if (cp_next_tokens_can_be_attribute_p (parser))
17884 type_specifier_seq->attributes =
17885 chainon (type_specifier_seq->attributes,
17886 cp_parser_attributes_opt (parser));
17887 continue;
17890 /* record the token of the beginning of the type specifier seq,
17891 for error reporting purposes*/
17892 if (!start_token)
17893 start_token = cp_lexer_peek_token (parser->lexer);
17895 /* Look for the type-specifier. */
17896 type_specifier = cp_parser_type_specifier (parser,
17897 flags,
17898 type_specifier_seq,
17899 /*is_declaration=*/false,
17900 NULL,
17901 &is_cv_qualifier);
17902 if (!type_specifier)
17904 /* If the first type-specifier could not be found, this is not a
17905 type-specifier-seq at all. */
17906 if (!seen_type_specifier)
17908 cp_parser_error (parser, "expected type-specifier");
17909 type_specifier_seq->type = error_mark_node;
17910 return;
17912 /* If subsequent type-specifiers could not be found, the
17913 type-specifier-seq is complete. */
17914 break;
17917 seen_type_specifier = true;
17918 /* The standard says that a condition can be:
17920 type-specifier-seq declarator = assignment-expression
17922 However, given:
17924 struct S {};
17925 if (int S = ...)
17927 we should treat the "S" as a declarator, not as a
17928 type-specifier. The standard doesn't say that explicitly for
17929 type-specifier-seq, but it does say that for
17930 decl-specifier-seq in an ordinary declaration. Perhaps it
17931 would be clearer just to allow a decl-specifier-seq here, and
17932 then add a semantic restriction that if any decl-specifiers
17933 that are not type-specifiers appear, the program is invalid. */
17934 if (is_declaration && !is_cv_qualifier)
17935 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17939 /* Parse a parameter-declaration-clause.
17941 parameter-declaration-clause:
17942 parameter-declaration-list [opt] ... [opt]
17943 parameter-declaration-list , ...
17945 Returns a representation for the parameter declarations. A return
17946 value of NULL indicates a parameter-declaration-clause consisting
17947 only of an ellipsis. */
17949 static tree
17950 cp_parser_parameter_declaration_clause (cp_parser* parser)
17952 tree parameters;
17953 cp_token *token;
17954 bool ellipsis_p;
17955 bool is_error;
17957 /* Peek at the next token. */
17958 token = cp_lexer_peek_token (parser->lexer);
17959 /* Check for trivial parameter-declaration-clauses. */
17960 if (token->type == CPP_ELLIPSIS)
17962 /* Consume the `...' token. */
17963 cp_lexer_consume_token (parser->lexer);
17964 return NULL_TREE;
17966 else if (token->type == CPP_CLOSE_PAREN)
17967 /* There are no parameters. */
17969 #ifndef NO_IMPLICIT_EXTERN_C
17970 if (in_system_header && current_class_type == NULL
17971 && current_lang_name == lang_name_c)
17972 return NULL_TREE;
17973 else
17974 #endif
17975 return void_list_node;
17977 /* Check for `(void)', too, which is a special case. */
17978 else if (token->keyword == RID_VOID
17979 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17980 == CPP_CLOSE_PAREN))
17982 /* Consume the `void' token. */
17983 cp_lexer_consume_token (parser->lexer);
17984 /* There are no parameters. */
17985 return void_list_node;
17988 /* Parse the parameter-declaration-list. */
17989 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17990 /* If a parse error occurred while parsing the
17991 parameter-declaration-list, then the entire
17992 parameter-declaration-clause is erroneous. */
17993 if (is_error)
17994 return NULL;
17996 /* Peek at the next token. */
17997 token = cp_lexer_peek_token (parser->lexer);
17998 /* If it's a `,', the clause should terminate with an ellipsis. */
17999 if (token->type == CPP_COMMA)
18001 /* Consume the `,'. */
18002 cp_lexer_consume_token (parser->lexer);
18003 /* Expect an ellipsis. */
18004 ellipsis_p
18005 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18007 /* It might also be `...' if the optional trailing `,' was
18008 omitted. */
18009 else if (token->type == CPP_ELLIPSIS)
18011 /* Consume the `...' token. */
18012 cp_lexer_consume_token (parser->lexer);
18013 /* And remember that we saw it. */
18014 ellipsis_p = true;
18016 else
18017 ellipsis_p = false;
18019 /* Finish the parameter list. */
18020 if (!ellipsis_p)
18021 parameters = chainon (parameters, void_list_node);
18023 return parameters;
18026 /* Parse a parameter-declaration-list.
18028 parameter-declaration-list:
18029 parameter-declaration
18030 parameter-declaration-list , parameter-declaration
18032 Returns a representation of the parameter-declaration-list, as for
18033 cp_parser_parameter_declaration_clause. However, the
18034 `void_list_node' is never appended to the list. Upon return,
18035 *IS_ERROR will be true iff an error occurred. */
18037 static tree
18038 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18040 tree parameters = NULL_TREE;
18041 tree *tail = &parameters;
18042 bool saved_in_unbraced_linkage_specification_p;
18043 int index = 0;
18044 int implicit_template_parms = 0;
18046 /* Assume all will go well. */
18047 *is_error = false;
18048 /* The special considerations that apply to a function within an
18049 unbraced linkage specifications do not apply to the parameters
18050 to the function. */
18051 saved_in_unbraced_linkage_specification_p
18052 = parser->in_unbraced_linkage_specification_p;
18053 parser->in_unbraced_linkage_specification_p = false;
18055 /* Look for more parameters. */
18056 while (true)
18058 cp_parameter_declarator *parameter;
18059 tree decl = error_mark_node;
18060 bool parenthesized_p = false;
18061 /* Parse the parameter. */
18062 parameter
18063 = cp_parser_parameter_declaration (parser,
18064 /*template_parm_p=*/false,
18065 &parenthesized_p);
18067 /* We don't know yet if the enclosing context is deprecated, so wait
18068 and warn in grokparms if appropriate. */
18069 deprecated_state = DEPRECATED_SUPPRESS;
18071 if (parameter)
18073 decl = grokdeclarator (parameter->declarator,
18074 &parameter->decl_specifiers,
18075 PARM,
18076 parameter->default_argument != NULL_TREE,
18077 &parameter->decl_specifiers.attributes);
18079 if (TREE_TYPE (decl) != error_mark_node
18080 && parameter->decl_specifiers.type
18081 && is_auto_or_concept (parameter->decl_specifiers.type))
18082 ++implicit_template_parms;
18085 deprecated_state = DEPRECATED_NORMAL;
18087 /* If a parse error occurred parsing the parameter declaration,
18088 then the entire parameter-declaration-list is erroneous. */
18089 if (decl == error_mark_node)
18091 *is_error = true;
18092 parameters = error_mark_node;
18093 break;
18096 if (parameter->decl_specifiers.attributes)
18097 cplus_decl_attributes (&decl,
18098 parameter->decl_specifiers.attributes,
18100 if (DECL_NAME (decl))
18101 decl = pushdecl (decl);
18103 if (decl != error_mark_node)
18105 retrofit_lang_decl (decl);
18106 DECL_PARM_INDEX (decl) = ++index;
18107 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18110 /* Add the new parameter to the list. */
18111 *tail = build_tree_list (parameter->default_argument, decl);
18112 tail = &TREE_CHAIN (*tail);
18114 /* Peek at the next token. */
18115 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18116 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18117 /* These are for Objective-C++ */
18118 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18119 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18120 /* The parameter-declaration-list is complete. */
18121 break;
18122 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18124 cp_token *token;
18126 /* Peek at the next token. */
18127 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18128 /* If it's an ellipsis, then the list is complete. */
18129 if (token->type == CPP_ELLIPSIS)
18130 break;
18131 /* Otherwise, there must be more parameters. Consume the
18132 `,'. */
18133 cp_lexer_consume_token (parser->lexer);
18134 /* When parsing something like:
18136 int i(float f, double d)
18138 we can tell after seeing the declaration for "f" that we
18139 are not looking at an initialization of a variable "i",
18140 but rather at the declaration of a function "i".
18142 Due to the fact that the parsing of template arguments
18143 (as specified to a template-id) requires backtracking we
18144 cannot use this technique when inside a template argument
18145 list. */
18146 if (!parser->in_template_argument_list_p
18147 && !parser->in_type_id_in_expr_p
18148 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18149 /* However, a parameter-declaration of the form
18150 "float(f)" (which is a valid declaration of a
18151 parameter "f") can also be interpreted as an
18152 expression (the conversion of "f" to "float"). */
18153 && !parenthesized_p)
18154 cp_parser_commit_to_tentative_parse (parser);
18156 else
18158 cp_parser_error (parser, "expected %<,%> or %<...%>");
18159 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18160 cp_parser_skip_to_closing_parenthesis (parser,
18161 /*recovering=*/true,
18162 /*or_comma=*/false,
18163 /*consume_paren=*/false);
18164 break;
18168 parser->in_unbraced_linkage_specification_p
18169 = saved_in_unbraced_linkage_specification_p;
18171 if (parameters != error_mark_node && implicit_template_parms)
18172 parameters = add_implicit_template_parms (parser,
18173 implicit_template_parms,
18174 parameters);
18176 return parameters;
18179 /* Parse a parameter declaration.
18181 parameter-declaration:
18182 decl-specifier-seq ... [opt] declarator
18183 decl-specifier-seq declarator = assignment-expression
18184 decl-specifier-seq ... [opt] abstract-declarator [opt]
18185 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18187 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18188 declares a template parameter. (In that case, a non-nested `>'
18189 token encountered during the parsing of the assignment-expression
18190 is not interpreted as a greater-than operator.)
18192 Returns a representation of the parameter, or NULL if an error
18193 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18194 true iff the declarator is of the form "(p)". */
18196 static cp_parameter_declarator *
18197 cp_parser_parameter_declaration (cp_parser *parser,
18198 bool template_parm_p,
18199 bool *parenthesized_p)
18201 int declares_class_or_enum;
18202 cp_decl_specifier_seq decl_specifiers;
18203 cp_declarator *declarator;
18204 tree default_argument;
18205 cp_token *token = NULL, *declarator_token_start = NULL;
18206 const char *saved_message;
18208 /* In a template parameter, `>' is not an operator.
18210 [temp.param]
18212 When parsing a default template-argument for a non-type
18213 template-parameter, the first non-nested `>' is taken as the end
18214 of the template parameter-list rather than a greater-than
18215 operator. */
18217 /* Type definitions may not appear in parameter types. */
18218 saved_message = parser->type_definition_forbidden_message;
18219 parser->type_definition_forbidden_message
18220 = G_("types may not be defined in parameter types");
18222 /* Parse the declaration-specifiers. */
18223 cp_parser_decl_specifier_seq (parser,
18224 CP_PARSER_FLAGS_NONE,
18225 &decl_specifiers,
18226 &declares_class_or_enum);
18228 /* Complain about missing 'typename' or other invalid type names. */
18229 if (!decl_specifiers.any_type_specifiers_p
18230 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18231 decl_specifiers.type = error_mark_node;
18233 /* If an error occurred, there's no reason to attempt to parse the
18234 rest of the declaration. */
18235 if (cp_parser_error_occurred (parser))
18237 parser->type_definition_forbidden_message = saved_message;
18238 return NULL;
18241 /* Peek at the next token. */
18242 token = cp_lexer_peek_token (parser->lexer);
18244 /* If the next token is a `)', `,', `=', `>', or `...', then there
18245 is no declarator. However, when variadic templates are enabled,
18246 there may be a declarator following `...'. */
18247 if (token->type == CPP_CLOSE_PAREN
18248 || token->type == CPP_COMMA
18249 || token->type == CPP_EQ
18250 || token->type == CPP_GREATER)
18252 declarator = NULL;
18253 if (parenthesized_p)
18254 *parenthesized_p = false;
18256 /* Otherwise, there should be a declarator. */
18257 else
18259 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18260 parser->default_arg_ok_p = false;
18262 /* After seeing a decl-specifier-seq, if the next token is not a
18263 "(", there is no possibility that the code is a valid
18264 expression. Therefore, if parsing tentatively, we commit at
18265 this point. */
18266 if (!parser->in_template_argument_list_p
18267 /* In an expression context, having seen:
18269 (int((char ...
18271 we cannot be sure whether we are looking at a
18272 function-type (taking a "char" as a parameter) or a cast
18273 of some object of type "char" to "int". */
18274 && !parser->in_type_id_in_expr_p
18275 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18276 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18277 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18278 cp_parser_commit_to_tentative_parse (parser);
18279 /* Parse the declarator. */
18280 declarator_token_start = token;
18281 declarator = cp_parser_declarator (parser,
18282 CP_PARSER_DECLARATOR_EITHER,
18283 /*ctor_dtor_or_conv_p=*/NULL,
18284 parenthesized_p,
18285 /*member_p=*/false);
18286 parser->default_arg_ok_p = saved_default_arg_ok_p;
18287 /* After the declarator, allow more attributes. */
18288 decl_specifiers.attributes
18289 = chainon (decl_specifiers.attributes,
18290 cp_parser_attributes_opt (parser));
18293 /* If the next token is an ellipsis, and we have not seen a
18294 declarator name, and the type of the declarator contains parameter
18295 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18296 a parameter pack expansion expression. Otherwise, leave the
18297 ellipsis for a C-style variadic function. */
18298 token = cp_lexer_peek_token (parser->lexer);
18299 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18301 tree type = decl_specifiers.type;
18303 if (type && DECL_P (type))
18304 type = TREE_TYPE (type);
18306 if (type
18307 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18308 && declarator_can_be_parameter_pack (declarator)
18309 && (!declarator || !declarator->parameter_pack_p)
18310 && uses_parameter_packs (type))
18312 /* Consume the `...'. */
18313 cp_lexer_consume_token (parser->lexer);
18314 maybe_warn_variadic_templates ();
18316 /* Build a pack expansion type */
18317 if (declarator)
18318 declarator->parameter_pack_p = true;
18319 else
18320 decl_specifiers.type = make_pack_expansion (type);
18324 /* The restriction on defining new types applies only to the type
18325 of the parameter, not to the default argument. */
18326 parser->type_definition_forbidden_message = saved_message;
18328 /* If the next token is `=', then process a default argument. */
18329 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18331 token = cp_lexer_peek_token (parser->lexer);
18332 /* If we are defining a class, then the tokens that make up the
18333 default argument must be saved and processed later. */
18334 if (!template_parm_p && at_class_scope_p ()
18335 && TYPE_BEING_DEFINED (current_class_type)
18336 && !LAMBDA_TYPE_P (current_class_type))
18337 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18338 /* Outside of a class definition, we can just parse the
18339 assignment-expression. */
18340 else
18341 default_argument
18342 = cp_parser_default_argument (parser, template_parm_p);
18344 if (!parser->default_arg_ok_p)
18346 if (flag_permissive)
18347 warning (0, "deprecated use of default argument for parameter of non-function");
18348 else
18350 error_at (token->location,
18351 "default arguments are only "
18352 "permitted for function parameters");
18353 default_argument = NULL_TREE;
18356 else if ((declarator && declarator->parameter_pack_p)
18357 || (decl_specifiers.type
18358 && PACK_EXPANSION_P (decl_specifiers.type)))
18360 /* Find the name of the parameter pack. */
18361 cp_declarator *id_declarator = declarator;
18362 while (id_declarator && id_declarator->kind != cdk_id)
18363 id_declarator = id_declarator->declarator;
18365 if (id_declarator && id_declarator->kind == cdk_id)
18366 error_at (declarator_token_start->location,
18367 template_parm_p
18368 ? G_("template parameter pack %qD "
18369 "cannot have a default argument")
18370 : G_("parameter pack %qD cannot have "
18371 "a default argument"),
18372 id_declarator->u.id.unqualified_name);
18373 else
18374 error_at (declarator_token_start->location,
18375 template_parm_p
18376 ? G_("template parameter pack cannot have "
18377 "a default argument")
18378 : G_("parameter pack cannot have a "
18379 "default argument"));
18381 default_argument = NULL_TREE;
18384 else
18385 default_argument = NULL_TREE;
18387 return make_parameter_declarator (&decl_specifiers,
18388 declarator,
18389 default_argument);
18392 /* Parse a default argument and return it.
18394 TEMPLATE_PARM_P is true if this is a default argument for a
18395 non-type template parameter. */
18396 static tree
18397 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18399 tree default_argument = NULL_TREE;
18400 bool saved_greater_than_is_operator_p;
18401 bool saved_local_variables_forbidden_p;
18402 bool non_constant_p, is_direct_init;
18404 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18405 set correctly. */
18406 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18407 parser->greater_than_is_operator_p = !template_parm_p;
18408 /* Local variable names (and the `this' keyword) may not
18409 appear in a default argument. */
18410 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18411 parser->local_variables_forbidden_p = true;
18412 /* Parse the assignment-expression. */
18413 if (template_parm_p)
18414 push_deferring_access_checks (dk_no_deferred);
18415 default_argument
18416 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18417 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18418 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18419 if (template_parm_p)
18420 pop_deferring_access_checks ();
18421 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18422 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18424 return default_argument;
18427 /* Parse a function-body.
18429 function-body:
18430 compound_statement */
18432 static void
18433 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18435 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18438 /* Parse a ctor-initializer-opt followed by a function-body. Return
18439 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18440 is true we are parsing a function-try-block. */
18442 static bool
18443 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18444 bool in_function_try_block)
18446 tree body, list;
18447 bool ctor_initializer_p;
18448 const bool check_body_p =
18449 DECL_CONSTRUCTOR_P (current_function_decl)
18450 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18451 tree last = NULL;
18453 /* Begin the function body. */
18454 body = begin_function_body ();
18455 /* Parse the optional ctor-initializer. */
18456 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18458 /* If we're parsing a constexpr constructor definition, we need
18459 to check that the constructor body is indeed empty. However,
18460 before we get to cp_parser_function_body lot of junk has been
18461 generated, so we can't just check that we have an empty block.
18462 Rather we take a snapshot of the outermost block, and check whether
18463 cp_parser_function_body changed its state. */
18464 if (check_body_p)
18466 list = cur_stmt_list;
18467 if (STATEMENT_LIST_TAIL (list))
18468 last = STATEMENT_LIST_TAIL (list)->stmt;
18470 /* Parse the function-body. */
18471 cp_parser_function_body (parser, in_function_try_block);
18472 if (check_body_p)
18473 check_constexpr_ctor_body (last, list);
18474 /* Finish the function body. */
18475 finish_function_body (body);
18477 return ctor_initializer_p;
18480 /* Parse an initializer.
18482 initializer:
18483 = initializer-clause
18484 ( expression-list )
18486 Returns an expression representing the initializer. If no
18487 initializer is present, NULL_TREE is returned.
18489 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18490 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18491 set to TRUE if there is no initializer present. If there is an
18492 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18493 is set to true; otherwise it is set to false. */
18495 static tree
18496 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18497 bool* non_constant_p)
18499 cp_token *token;
18500 tree init;
18502 /* Peek at the next token. */
18503 token = cp_lexer_peek_token (parser->lexer);
18505 /* Let our caller know whether or not this initializer was
18506 parenthesized. */
18507 *is_direct_init = (token->type != CPP_EQ);
18508 /* Assume that the initializer is constant. */
18509 *non_constant_p = false;
18511 if (token->type == CPP_EQ)
18513 /* Consume the `='. */
18514 cp_lexer_consume_token (parser->lexer);
18515 /* Parse the initializer-clause. */
18516 init = cp_parser_initializer_clause (parser, non_constant_p);
18518 else if (token->type == CPP_OPEN_PAREN)
18520 vec<tree, va_gc> *vec;
18521 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18522 /*cast_p=*/false,
18523 /*allow_expansion_p=*/true,
18524 non_constant_p);
18525 if (vec == NULL)
18526 return error_mark_node;
18527 init = build_tree_list_vec (vec);
18528 release_tree_vector (vec);
18530 else if (token->type == CPP_OPEN_BRACE)
18532 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18533 init = cp_parser_braced_list (parser, non_constant_p);
18534 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18536 else
18538 /* Anything else is an error. */
18539 cp_parser_error (parser, "expected initializer");
18540 init = error_mark_node;
18543 return init;
18546 /* Parse an initializer-clause.
18548 initializer-clause:
18549 assignment-expression
18550 braced-init-list
18552 Returns an expression representing the initializer.
18554 If the `assignment-expression' production is used the value
18555 returned is simply a representation for the expression.
18557 Otherwise, calls cp_parser_braced_list. */
18559 static tree
18560 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18562 tree initializer;
18564 /* Assume the expression is constant. */
18565 *non_constant_p = false;
18567 /* If it is not a `{', then we are looking at an
18568 assignment-expression. */
18569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18571 initializer
18572 = cp_parser_constant_expression (parser,
18573 /*allow_non_constant_p=*/true,
18574 non_constant_p);
18576 else
18577 initializer = cp_parser_braced_list (parser, non_constant_p);
18579 return initializer;
18582 /* Parse a brace-enclosed initializer list.
18584 braced-init-list:
18585 { initializer-list , [opt] }
18588 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18589 the elements of the initializer-list (or NULL, if the last
18590 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18591 NULL_TREE. There is no way to detect whether or not the optional
18592 trailing `,' was provided. NON_CONSTANT_P is as for
18593 cp_parser_initializer. */
18595 static tree
18596 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18598 tree initializer;
18600 /* Consume the `{' token. */
18601 cp_lexer_consume_token (parser->lexer);
18602 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18603 initializer = make_node (CONSTRUCTOR);
18604 /* If it's not a `}', then there is a non-trivial initializer. */
18605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18607 /* Parse the initializer list. */
18608 CONSTRUCTOR_ELTS (initializer)
18609 = cp_parser_initializer_list (parser, non_constant_p);
18610 /* A trailing `,' token is allowed. */
18611 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18612 cp_lexer_consume_token (parser->lexer);
18614 else
18615 *non_constant_p = false;
18616 /* Now, there should be a trailing `}'. */
18617 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18618 TREE_TYPE (initializer) = init_list_type_node;
18619 return initializer;
18622 /* Parse an initializer-list.
18624 initializer-list:
18625 initializer-clause ... [opt]
18626 initializer-list , initializer-clause ... [opt]
18628 GNU Extension:
18630 initializer-list:
18631 designation initializer-clause ...[opt]
18632 initializer-list , designation initializer-clause ...[opt]
18634 designation:
18635 . identifier =
18636 identifier :
18637 [ constant-expression ] =
18639 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18640 for the initializer. If the INDEX of the elt is non-NULL, it is the
18641 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18642 as for cp_parser_initializer. */
18644 static vec<constructor_elt, va_gc> *
18645 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18647 vec<constructor_elt, va_gc> *v = NULL;
18649 /* Assume all of the expressions are constant. */
18650 *non_constant_p = false;
18652 /* Parse the rest of the list. */
18653 while (true)
18655 cp_token *token;
18656 tree designator;
18657 tree initializer;
18658 bool clause_non_constant_p;
18660 /* If the next token is an identifier and the following one is a
18661 colon, we are looking at the GNU designated-initializer
18662 syntax. */
18663 if (cp_parser_allow_gnu_extensions_p (parser)
18664 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18665 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18667 /* Warn the user that they are using an extension. */
18668 pedwarn (input_location, OPT_Wpedantic,
18669 "ISO C++ does not allow designated initializers");
18670 /* Consume the identifier. */
18671 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18672 /* Consume the `:'. */
18673 cp_lexer_consume_token (parser->lexer);
18675 /* Also handle the C99 syntax, '. id ='. */
18676 else if (cp_parser_allow_gnu_extensions_p (parser)
18677 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18678 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18679 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18681 /* Warn the user that they are using an extension. */
18682 pedwarn (input_location, OPT_Wpedantic,
18683 "ISO C++ does not allow C99 designated initializers");
18684 /* Consume the `.'. */
18685 cp_lexer_consume_token (parser->lexer);
18686 /* Consume the identifier. */
18687 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18688 /* Consume the `='. */
18689 cp_lexer_consume_token (parser->lexer);
18691 /* Also handle C99 array designators, '[ const ] ='. */
18692 else if (cp_parser_allow_gnu_extensions_p (parser)
18693 && !c_dialect_objc ()
18694 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18696 /* In C++11, [ could start a lambda-introducer. */
18697 bool non_const = false;
18699 cp_parser_parse_tentatively (parser);
18700 cp_lexer_consume_token (parser->lexer);
18701 designator = cp_parser_constant_expression (parser, true, &non_const);
18702 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18703 cp_parser_require (parser, CPP_EQ, RT_EQ);
18704 if (!cp_parser_parse_definitely (parser))
18705 designator = NULL_TREE;
18706 else if (non_const)
18707 require_potential_rvalue_constant_expression (designator);
18709 else
18710 designator = NULL_TREE;
18712 /* Parse the initializer. */
18713 initializer = cp_parser_initializer_clause (parser,
18714 &clause_non_constant_p);
18715 /* If any clause is non-constant, so is the entire initializer. */
18716 if (clause_non_constant_p)
18717 *non_constant_p = true;
18719 /* If we have an ellipsis, this is an initializer pack
18720 expansion. */
18721 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18723 /* Consume the `...'. */
18724 cp_lexer_consume_token (parser->lexer);
18726 /* Turn the initializer into an initializer expansion. */
18727 initializer = make_pack_expansion (initializer);
18730 /* Add it to the vector. */
18731 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18733 /* If the next token is not a comma, we have reached the end of
18734 the list. */
18735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18736 break;
18738 /* Peek at the next token. */
18739 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18740 /* If the next token is a `}', then we're still done. An
18741 initializer-clause can have a trailing `,' after the
18742 initializer-list and before the closing `}'. */
18743 if (token->type == CPP_CLOSE_BRACE)
18744 break;
18746 /* Consume the `,' token. */
18747 cp_lexer_consume_token (parser->lexer);
18750 return v;
18753 /* Classes [gram.class] */
18755 /* Parse a class-name.
18757 class-name:
18758 identifier
18759 template-id
18761 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18762 to indicate that names looked up in dependent types should be
18763 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18764 keyword has been used to indicate that the name that appears next
18765 is a template. TAG_TYPE indicates the explicit tag given before
18766 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18767 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18768 is the class being defined in a class-head.
18770 Returns the TYPE_DECL representing the class. */
18772 static tree
18773 cp_parser_class_name (cp_parser *parser,
18774 bool typename_keyword_p,
18775 bool template_keyword_p,
18776 enum tag_types tag_type,
18777 bool check_dependency_p,
18778 bool class_head_p,
18779 bool is_declaration)
18781 tree decl;
18782 tree scope;
18783 bool typename_p;
18784 cp_token *token;
18785 tree identifier = NULL_TREE;
18787 /* All class-names start with an identifier. */
18788 token = cp_lexer_peek_token (parser->lexer);
18789 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18791 cp_parser_error (parser, "expected class-name");
18792 return error_mark_node;
18795 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18796 to a template-id, so we save it here. */
18797 scope = parser->scope;
18798 if (scope == error_mark_node)
18799 return error_mark_node;
18801 /* Any name names a type if we're following the `typename' keyword
18802 in a qualified name where the enclosing scope is type-dependent. */
18803 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18804 && dependent_type_p (scope));
18805 /* Handle the common case (an identifier, but not a template-id)
18806 efficiently. */
18807 if (token->type == CPP_NAME
18808 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18810 cp_token *identifier_token;
18811 bool ambiguous_p;
18813 /* Look for the identifier. */
18814 identifier_token = cp_lexer_peek_token (parser->lexer);
18815 ambiguous_p = identifier_token->ambiguous_p;
18816 identifier = cp_parser_identifier (parser);
18817 /* If the next token isn't an identifier, we are certainly not
18818 looking at a class-name. */
18819 if (identifier == error_mark_node)
18820 decl = error_mark_node;
18821 /* If we know this is a type-name, there's no need to look it
18822 up. */
18823 else if (typename_p)
18824 decl = identifier;
18825 else
18827 tree ambiguous_decls;
18828 /* If we already know that this lookup is ambiguous, then
18829 we've already issued an error message; there's no reason
18830 to check again. */
18831 if (ambiguous_p)
18833 cp_parser_simulate_error (parser);
18834 return error_mark_node;
18836 /* If the next token is a `::', then the name must be a type
18837 name.
18839 [basic.lookup.qual]
18841 During the lookup for a name preceding the :: scope
18842 resolution operator, object, function, and enumerator
18843 names are ignored. */
18844 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18845 tag_type = typename_type;
18846 /* Look up the name. */
18847 decl = cp_parser_lookup_name (parser, identifier,
18848 tag_type,
18849 /*is_template=*/false,
18850 /*is_namespace=*/false,
18851 check_dependency_p,
18852 &ambiguous_decls,
18853 identifier_token->location);
18854 if (ambiguous_decls)
18856 if (cp_parser_parsing_tentatively (parser))
18857 cp_parser_simulate_error (parser);
18858 return error_mark_node;
18862 else
18864 /* Try a template-id. */
18865 decl = cp_parser_template_id (parser, template_keyword_p,
18866 check_dependency_p,
18867 tag_type,
18868 is_declaration);
18869 if (decl == error_mark_node)
18870 return error_mark_node;
18873 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18875 /* If this is a typename, create a TYPENAME_TYPE. */
18876 if (typename_p && decl != error_mark_node)
18878 decl = make_typename_type (scope, decl, typename_type,
18879 /*complain=*/tf_error);
18880 if (decl != error_mark_node)
18881 decl = TYPE_NAME (decl);
18884 decl = strip_using_decl (decl);
18886 /* Check to see that it is really the name of a class. */
18887 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18888 && identifier_p (TREE_OPERAND (decl, 0))
18889 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18890 /* Situations like this:
18892 template <typename T> struct A {
18893 typename T::template X<int>::I i;
18896 are problematic. Is `T::template X<int>' a class-name? The
18897 standard does not seem to be definitive, but there is no other
18898 valid interpretation of the following `::'. Therefore, those
18899 names are considered class-names. */
18901 decl = make_typename_type (scope, decl, tag_type, tf_error);
18902 if (decl != error_mark_node)
18903 decl = TYPE_NAME (decl);
18905 else if (TREE_CODE (decl) != TYPE_DECL
18906 || TREE_TYPE (decl) == error_mark_node
18907 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18908 /* In Objective-C 2.0, a classname followed by '.' starts a
18909 dot-syntax expression, and it's not a type-name. */
18910 || (c_dialect_objc ()
18911 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18912 && objc_is_class_name (decl)))
18913 decl = error_mark_node;
18915 if (decl == error_mark_node)
18916 cp_parser_error (parser, "expected class-name");
18917 else if (identifier && !parser->scope)
18918 maybe_note_name_used_in_class (identifier, decl);
18920 return decl;
18923 /* Parse a class-specifier.
18925 class-specifier:
18926 class-head { member-specification [opt] }
18928 Returns the TREE_TYPE representing the class. */
18930 static tree
18931 cp_parser_class_specifier_1 (cp_parser* parser)
18933 tree type;
18934 tree attributes = NULL_TREE;
18935 bool nested_name_specifier_p;
18936 unsigned saved_num_template_parameter_lists;
18937 bool saved_in_function_body;
18938 unsigned char in_statement;
18939 bool in_switch_statement_p;
18940 bool saved_in_unbraced_linkage_specification_p;
18941 tree old_scope = NULL_TREE;
18942 tree scope = NULL_TREE;
18943 cp_token *closing_brace;
18945 push_deferring_access_checks (dk_no_deferred);
18947 /* Parse the class-head. */
18948 type = cp_parser_class_head (parser,
18949 &nested_name_specifier_p);
18950 /* If the class-head was a semantic disaster, skip the entire body
18951 of the class. */
18952 if (!type)
18954 cp_parser_skip_to_end_of_block_or_statement (parser);
18955 pop_deferring_access_checks ();
18956 return error_mark_node;
18959 /* Look for the `{'. */
18960 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18962 pop_deferring_access_checks ();
18963 return error_mark_node;
18966 cp_ensure_no_omp_declare_simd (parser);
18968 /* Issue an error message if type-definitions are forbidden here. */
18969 cp_parser_check_type_definition (parser);
18970 /* Remember that we are defining one more class. */
18971 ++parser->num_classes_being_defined;
18972 /* Inside the class, surrounding template-parameter-lists do not
18973 apply. */
18974 saved_num_template_parameter_lists
18975 = parser->num_template_parameter_lists;
18976 parser->num_template_parameter_lists = 0;
18977 /* We are not in a function body. */
18978 saved_in_function_body = parser->in_function_body;
18979 parser->in_function_body = false;
18980 /* Or in a loop. */
18981 in_statement = parser->in_statement;
18982 parser->in_statement = 0;
18983 /* Or in a switch. */
18984 in_switch_statement_p = parser->in_switch_statement_p;
18985 parser->in_switch_statement_p = false;
18986 /* We are not immediately inside an extern "lang" block. */
18987 saved_in_unbraced_linkage_specification_p
18988 = parser->in_unbraced_linkage_specification_p;
18989 parser->in_unbraced_linkage_specification_p = false;
18991 /* Start the class. */
18992 if (nested_name_specifier_p)
18994 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18995 old_scope = push_inner_scope (scope);
18997 type = begin_class_definition (type);
18999 if (type == error_mark_node)
19000 /* If the type is erroneous, skip the entire body of the class. */
19001 cp_parser_skip_to_closing_brace (parser);
19002 else
19003 /* Parse the member-specification. */
19004 cp_parser_member_specification_opt (parser);
19006 /* Look for the trailing `}'. */
19007 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19008 /* Look for trailing attributes to apply to this class. */
19009 if (cp_parser_allow_gnu_extensions_p (parser))
19010 attributes = cp_parser_gnu_attributes_opt (parser);
19011 if (type != error_mark_node)
19012 type = finish_struct (type, attributes);
19013 if (nested_name_specifier_p)
19014 pop_inner_scope (old_scope, scope);
19016 /* We've finished a type definition. Check for the common syntax
19017 error of forgetting a semicolon after the definition. We need to
19018 be careful, as we can't just check for not-a-semicolon and be done
19019 with it; the user might have typed:
19021 class X { } c = ...;
19022 class X { } *p = ...;
19024 and so forth. Instead, enumerate all the possible tokens that
19025 might follow this production; if we don't see one of them, then
19026 complain and silently insert the semicolon. */
19028 cp_token *token = cp_lexer_peek_token (parser->lexer);
19029 bool want_semicolon = true;
19031 if (cp_next_tokens_can_be_std_attribute_p (parser))
19032 /* Don't try to parse c++11 attributes here. As per the
19033 grammar, that should be a task for
19034 cp_parser_decl_specifier_seq. */
19035 want_semicolon = false;
19037 switch (token->type)
19039 case CPP_NAME:
19040 case CPP_SEMICOLON:
19041 case CPP_MULT:
19042 case CPP_AND:
19043 case CPP_OPEN_PAREN:
19044 case CPP_CLOSE_PAREN:
19045 case CPP_COMMA:
19046 want_semicolon = false;
19047 break;
19049 /* While it's legal for type qualifiers and storage class
19050 specifiers to follow type definitions in the grammar, only
19051 compiler testsuites contain code like that. Assume that if
19052 we see such code, then what we're really seeing is a case
19053 like:
19055 class X { }
19056 const <type> var = ...;
19060 class Y { }
19061 static <type> func (...) ...
19063 i.e. the qualifier or specifier applies to the next
19064 declaration. To do so, however, we need to look ahead one
19065 more token to see if *that* token is a type specifier.
19067 This code could be improved to handle:
19069 class Z { }
19070 static const <type> var = ...; */
19071 case CPP_KEYWORD:
19072 if (keyword_is_decl_specifier (token->keyword))
19074 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19076 /* Handling user-defined types here would be nice, but very
19077 tricky. */
19078 want_semicolon
19079 = (lookahead->type == CPP_KEYWORD
19080 && keyword_begins_type_specifier (lookahead->keyword));
19082 break;
19083 default:
19084 break;
19087 /* If we don't have a type, then something is very wrong and we
19088 shouldn't try to do anything clever. Likewise for not seeing the
19089 closing brace. */
19090 if (closing_brace && TYPE_P (type) && want_semicolon)
19092 cp_token_position prev
19093 = cp_lexer_previous_token_position (parser->lexer);
19094 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19095 location_t loc = prev_token->location;
19097 if (CLASSTYPE_DECLARED_CLASS (type))
19098 error_at (loc, "expected %<;%> after class definition");
19099 else if (TREE_CODE (type) == RECORD_TYPE)
19100 error_at (loc, "expected %<;%> after struct definition");
19101 else if (TREE_CODE (type) == UNION_TYPE)
19102 error_at (loc, "expected %<;%> after union definition");
19103 else
19104 gcc_unreachable ();
19106 /* Unget one token and smash it to look as though we encountered
19107 a semicolon in the input stream. */
19108 cp_lexer_set_token_position (parser->lexer, prev);
19109 token = cp_lexer_peek_token (parser->lexer);
19110 token->type = CPP_SEMICOLON;
19111 token->keyword = RID_MAX;
19115 /* If this class is not itself within the scope of another class,
19116 then we need to parse the bodies of all of the queued function
19117 definitions. Note that the queued functions defined in a class
19118 are not always processed immediately following the
19119 class-specifier for that class. Consider:
19121 struct A {
19122 struct B { void f() { sizeof (A); } };
19125 If `f' were processed before the processing of `A' were
19126 completed, there would be no way to compute the size of `A'.
19127 Note that the nesting we are interested in here is lexical --
19128 not the semantic nesting given by TYPE_CONTEXT. In particular,
19129 for:
19131 struct A { struct B; };
19132 struct A::B { void f() { } };
19134 there is no need to delay the parsing of `A::B::f'. */
19135 if (--parser->num_classes_being_defined == 0)
19137 tree decl;
19138 tree class_type = NULL_TREE;
19139 tree pushed_scope = NULL_TREE;
19140 unsigned ix;
19141 cp_default_arg_entry *e;
19142 tree save_ccp, save_ccr;
19144 /* In a first pass, parse default arguments to the functions.
19145 Then, in a second pass, parse the bodies of the functions.
19146 This two-phased approach handles cases like:
19148 struct S {
19149 void f() { g(); }
19150 void g(int i = 3);
19154 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19156 decl = e->decl;
19157 /* If there are default arguments that have not yet been processed,
19158 take care of them now. */
19159 if (class_type != e->class_type)
19161 if (pushed_scope)
19162 pop_scope (pushed_scope);
19163 class_type = e->class_type;
19164 pushed_scope = push_scope (class_type);
19166 /* Make sure that any template parameters are in scope. */
19167 maybe_begin_member_template_processing (decl);
19168 /* Parse the default argument expressions. */
19169 cp_parser_late_parsing_default_args (parser, decl);
19170 /* Remove any template parameters from the symbol table. */
19171 maybe_end_member_template_processing ();
19173 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19174 /* Now parse any NSDMIs. */
19175 save_ccp = current_class_ptr;
19176 save_ccr = current_class_ref;
19177 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19179 if (class_type != DECL_CONTEXT (decl))
19181 if (pushed_scope)
19182 pop_scope (pushed_scope);
19183 class_type = DECL_CONTEXT (decl);
19184 pushed_scope = push_scope (class_type);
19186 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19187 cp_parser_late_parsing_nsdmi (parser, decl);
19189 vec_safe_truncate (unparsed_nsdmis, 0);
19190 current_class_ptr = save_ccp;
19191 current_class_ref = save_ccr;
19192 if (pushed_scope)
19193 pop_scope (pushed_scope);
19194 /* Now parse the body of the functions. */
19195 if (flag_openmp)
19197 /* OpenMP UDRs need to be parsed before all other functions. */
19198 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19199 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19200 cp_parser_late_parsing_for_member (parser, decl);
19201 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19202 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19203 cp_parser_late_parsing_for_member (parser, decl);
19205 else
19206 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19207 cp_parser_late_parsing_for_member (parser, decl);
19208 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19211 /* Put back any saved access checks. */
19212 pop_deferring_access_checks ();
19214 /* Restore saved state. */
19215 parser->in_switch_statement_p = in_switch_statement_p;
19216 parser->in_statement = in_statement;
19217 parser->in_function_body = saved_in_function_body;
19218 parser->num_template_parameter_lists
19219 = saved_num_template_parameter_lists;
19220 parser->in_unbraced_linkage_specification_p
19221 = saved_in_unbraced_linkage_specification_p;
19223 return type;
19226 static tree
19227 cp_parser_class_specifier (cp_parser* parser)
19229 tree ret;
19230 timevar_push (TV_PARSE_STRUCT);
19231 ret = cp_parser_class_specifier_1 (parser);
19232 timevar_pop (TV_PARSE_STRUCT);
19233 return ret;
19236 /* Parse a class-head.
19238 class-head:
19239 class-key identifier [opt] base-clause [opt]
19240 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19241 class-key nested-name-specifier [opt] template-id
19242 base-clause [opt]
19244 class-virt-specifier:
19245 final
19247 GNU Extensions:
19248 class-key attributes identifier [opt] base-clause [opt]
19249 class-key attributes nested-name-specifier identifier base-clause [opt]
19250 class-key attributes nested-name-specifier [opt] template-id
19251 base-clause [opt]
19253 Upon return BASES is initialized to the list of base classes (or
19254 NULL, if there are none) in the same form returned by
19255 cp_parser_base_clause.
19257 Returns the TYPE of the indicated class. Sets
19258 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19259 involving a nested-name-specifier was used, and FALSE otherwise.
19261 Returns error_mark_node if this is not a class-head.
19263 Returns NULL_TREE if the class-head is syntactically valid, but
19264 semantically invalid in a way that means we should skip the entire
19265 body of the class. */
19267 static tree
19268 cp_parser_class_head (cp_parser* parser,
19269 bool* nested_name_specifier_p)
19271 tree nested_name_specifier;
19272 enum tag_types class_key;
19273 tree id = NULL_TREE;
19274 tree type = NULL_TREE;
19275 tree attributes;
19276 tree bases;
19277 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19278 bool template_id_p = false;
19279 bool qualified_p = false;
19280 bool invalid_nested_name_p = false;
19281 bool invalid_explicit_specialization_p = false;
19282 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19283 tree pushed_scope = NULL_TREE;
19284 unsigned num_templates;
19285 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19286 /* Assume no nested-name-specifier will be present. */
19287 *nested_name_specifier_p = false;
19288 /* Assume no template parameter lists will be used in defining the
19289 type. */
19290 num_templates = 0;
19291 parser->colon_corrects_to_scope_p = false;
19293 /* Look for the class-key. */
19294 class_key = cp_parser_class_key (parser);
19295 if (class_key == none_type)
19296 return error_mark_node;
19298 /* Parse the attributes. */
19299 attributes = cp_parser_attributes_opt (parser);
19301 /* If the next token is `::', that is invalid -- but sometimes
19302 people do try to write:
19304 struct ::S {};
19306 Handle this gracefully by accepting the extra qualifier, and then
19307 issuing an error about it later if this really is a
19308 class-head. If it turns out just to be an elaborated type
19309 specifier, remain silent. */
19310 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19311 qualified_p = true;
19313 push_deferring_access_checks (dk_no_check);
19315 /* Determine the name of the class. Begin by looking for an
19316 optional nested-name-specifier. */
19317 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19318 nested_name_specifier
19319 = cp_parser_nested_name_specifier_opt (parser,
19320 /*typename_keyword_p=*/false,
19321 /*check_dependency_p=*/false,
19322 /*type_p=*/true,
19323 /*is_declaration=*/false);
19324 /* If there was a nested-name-specifier, then there *must* be an
19325 identifier. */
19326 if (nested_name_specifier)
19328 type_start_token = cp_lexer_peek_token (parser->lexer);
19329 /* Although the grammar says `identifier', it really means
19330 `class-name' or `template-name'. You are only allowed to
19331 define a class that has already been declared with this
19332 syntax.
19334 The proposed resolution for Core Issue 180 says that wherever
19335 you see `class T::X' you should treat `X' as a type-name.
19337 It is OK to define an inaccessible class; for example:
19339 class A { class B; };
19340 class A::B {};
19342 We do not know if we will see a class-name, or a
19343 template-name. We look for a class-name first, in case the
19344 class-name is a template-id; if we looked for the
19345 template-name first we would stop after the template-name. */
19346 cp_parser_parse_tentatively (parser);
19347 type = cp_parser_class_name (parser,
19348 /*typename_keyword_p=*/false,
19349 /*template_keyword_p=*/false,
19350 class_type,
19351 /*check_dependency_p=*/false,
19352 /*class_head_p=*/true,
19353 /*is_declaration=*/false);
19354 /* If that didn't work, ignore the nested-name-specifier. */
19355 if (!cp_parser_parse_definitely (parser))
19357 invalid_nested_name_p = true;
19358 type_start_token = cp_lexer_peek_token (parser->lexer);
19359 id = cp_parser_identifier (parser);
19360 if (id == error_mark_node)
19361 id = NULL_TREE;
19363 /* If we could not find a corresponding TYPE, treat this
19364 declaration like an unqualified declaration. */
19365 if (type == error_mark_node)
19366 nested_name_specifier = NULL_TREE;
19367 /* Otherwise, count the number of templates used in TYPE and its
19368 containing scopes. */
19369 else
19371 tree scope;
19373 for (scope = TREE_TYPE (type);
19374 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19375 scope = get_containing_scope (scope))
19376 if (TYPE_P (scope)
19377 && CLASS_TYPE_P (scope)
19378 && CLASSTYPE_TEMPLATE_INFO (scope)
19379 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19380 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19381 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19382 ++num_templates;
19385 /* Otherwise, the identifier is optional. */
19386 else
19388 /* We don't know whether what comes next is a template-id,
19389 an identifier, or nothing at all. */
19390 cp_parser_parse_tentatively (parser);
19391 /* Check for a template-id. */
19392 type_start_token = cp_lexer_peek_token (parser->lexer);
19393 id = cp_parser_template_id (parser,
19394 /*template_keyword_p=*/false,
19395 /*check_dependency_p=*/true,
19396 class_key,
19397 /*is_declaration=*/true);
19398 /* If that didn't work, it could still be an identifier. */
19399 if (!cp_parser_parse_definitely (parser))
19401 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19403 type_start_token = cp_lexer_peek_token (parser->lexer);
19404 id = cp_parser_identifier (parser);
19406 else
19407 id = NULL_TREE;
19409 else
19411 template_id_p = true;
19412 ++num_templates;
19416 pop_deferring_access_checks ();
19418 if (id)
19420 cp_parser_check_for_invalid_template_id (parser, id,
19421 class_key,
19422 type_start_token->location);
19424 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19426 /* If it's not a `:' or a `{' then we can't really be looking at a
19427 class-head, since a class-head only appears as part of a
19428 class-specifier. We have to detect this situation before calling
19429 xref_tag, since that has irreversible side-effects. */
19430 if (!cp_parser_next_token_starts_class_definition_p (parser))
19432 cp_parser_error (parser, "expected %<{%> or %<:%>");
19433 type = error_mark_node;
19434 goto out;
19437 /* At this point, we're going ahead with the class-specifier, even
19438 if some other problem occurs. */
19439 cp_parser_commit_to_tentative_parse (parser);
19440 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19442 cp_parser_error (parser,
19443 "cannot specify %<override%> for a class");
19444 type = error_mark_node;
19445 goto out;
19447 /* Issue the error about the overly-qualified name now. */
19448 if (qualified_p)
19450 cp_parser_error (parser,
19451 "global qualification of class name is invalid");
19452 type = error_mark_node;
19453 goto out;
19455 else if (invalid_nested_name_p)
19457 cp_parser_error (parser,
19458 "qualified name does not name a class");
19459 type = error_mark_node;
19460 goto out;
19462 else if (nested_name_specifier)
19464 tree scope;
19466 /* Reject typedef-names in class heads. */
19467 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19469 error_at (type_start_token->location,
19470 "invalid class name in declaration of %qD",
19471 type);
19472 type = NULL_TREE;
19473 goto done;
19476 /* Figure out in what scope the declaration is being placed. */
19477 scope = current_scope ();
19478 /* If that scope does not contain the scope in which the
19479 class was originally declared, the program is invalid. */
19480 if (scope && !is_ancestor (scope, nested_name_specifier))
19482 if (at_namespace_scope_p ())
19483 error_at (type_start_token->location,
19484 "declaration of %qD in namespace %qD which does not "
19485 "enclose %qD",
19486 type, scope, nested_name_specifier);
19487 else
19488 error_at (type_start_token->location,
19489 "declaration of %qD in %qD which does not enclose %qD",
19490 type, scope, nested_name_specifier);
19491 type = NULL_TREE;
19492 goto done;
19494 /* [dcl.meaning]
19496 A declarator-id shall not be qualified except for the
19497 definition of a ... nested class outside of its class
19498 ... [or] the definition or explicit instantiation of a
19499 class member of a namespace outside of its namespace. */
19500 if (scope == nested_name_specifier)
19502 permerror (nested_name_specifier_token_start->location,
19503 "extra qualification not allowed");
19504 nested_name_specifier = NULL_TREE;
19505 num_templates = 0;
19508 /* An explicit-specialization must be preceded by "template <>". If
19509 it is not, try to recover gracefully. */
19510 if (at_namespace_scope_p ()
19511 && parser->num_template_parameter_lists == 0
19512 && template_id_p)
19514 error_at (type_start_token->location,
19515 "an explicit specialization must be preceded by %<template <>%>");
19516 invalid_explicit_specialization_p = true;
19517 /* Take the same action that would have been taken by
19518 cp_parser_explicit_specialization. */
19519 ++parser->num_template_parameter_lists;
19520 begin_specialization ();
19522 /* There must be no "return" statements between this point and the
19523 end of this function; set "type "to the correct return value and
19524 use "goto done;" to return. */
19525 /* Make sure that the right number of template parameters were
19526 present. */
19527 if (!cp_parser_check_template_parameters (parser, num_templates,
19528 type_start_token->location,
19529 /*declarator=*/NULL))
19531 /* If something went wrong, there is no point in even trying to
19532 process the class-definition. */
19533 type = NULL_TREE;
19534 goto done;
19537 /* Look up the type. */
19538 if (template_id_p)
19540 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19541 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19542 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19544 error_at (type_start_token->location,
19545 "function template %qD redeclared as a class template", id);
19546 type = error_mark_node;
19548 else
19550 type = TREE_TYPE (id);
19551 type = maybe_process_partial_specialization (type);
19553 if (nested_name_specifier)
19554 pushed_scope = push_scope (nested_name_specifier);
19556 else if (nested_name_specifier)
19558 tree class_type;
19560 /* Given:
19562 template <typename T> struct S { struct T };
19563 template <typename T> struct S<T>::T { };
19565 we will get a TYPENAME_TYPE when processing the definition of
19566 `S::T'. We need to resolve it to the actual type before we
19567 try to define it. */
19568 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19570 class_type = resolve_typename_type (TREE_TYPE (type),
19571 /*only_current_p=*/false);
19572 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19573 type = TYPE_NAME (class_type);
19574 else
19576 cp_parser_error (parser, "could not resolve typename type");
19577 type = error_mark_node;
19581 if (maybe_process_partial_specialization (TREE_TYPE (type))
19582 == error_mark_node)
19584 type = NULL_TREE;
19585 goto done;
19588 class_type = current_class_type;
19589 /* Enter the scope indicated by the nested-name-specifier. */
19590 pushed_scope = push_scope (nested_name_specifier);
19591 /* Get the canonical version of this type. */
19592 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19593 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19594 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19596 type = push_template_decl (type);
19597 if (type == error_mark_node)
19599 type = NULL_TREE;
19600 goto done;
19604 type = TREE_TYPE (type);
19605 *nested_name_specifier_p = true;
19607 else /* The name is not a nested name. */
19609 /* If the class was unnamed, create a dummy name. */
19610 if (!id)
19611 id = make_anon_name ();
19612 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19613 parser->num_template_parameter_lists);
19616 /* Indicate whether this class was declared as a `class' or as a
19617 `struct'. */
19618 if (TREE_CODE (type) == RECORD_TYPE)
19619 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19620 cp_parser_check_class_key (class_key, type);
19622 /* If this type was already complete, and we see another definition,
19623 that's an error. */
19624 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19626 error_at (type_start_token->location, "redefinition of %q#T",
19627 type);
19628 error_at (type_start_token->location, "previous definition of %q+#T",
19629 type);
19630 type = NULL_TREE;
19631 goto done;
19633 else if (type == error_mark_node)
19634 type = NULL_TREE;
19636 if (type)
19638 /* Apply attributes now, before any use of the class as a template
19639 argument in its base list. */
19640 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19641 fixup_attribute_variants (type);
19644 /* We will have entered the scope containing the class; the names of
19645 base classes should be looked up in that context. For example:
19647 struct A { struct B {}; struct C; };
19648 struct A::C : B {};
19650 is valid. */
19652 /* Get the list of base-classes, if there is one. */
19653 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19654 bases = cp_parser_base_clause (parser);
19655 else
19656 bases = NULL_TREE;
19658 /* If we're really defining a class, process the base classes.
19659 If they're invalid, fail. */
19660 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19661 && !xref_basetypes (type, bases))
19662 type = NULL_TREE;
19664 done:
19665 /* Leave the scope given by the nested-name-specifier. We will
19666 enter the class scope itself while processing the members. */
19667 if (pushed_scope)
19668 pop_scope (pushed_scope);
19670 if (invalid_explicit_specialization_p)
19672 end_specialization ();
19673 --parser->num_template_parameter_lists;
19676 if (type)
19677 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19678 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19679 CLASSTYPE_FINAL (type) = 1;
19680 out:
19681 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19682 return type;
19685 /* Parse a class-key.
19687 class-key:
19688 class
19689 struct
19690 union
19692 Returns the kind of class-key specified, or none_type to indicate
19693 error. */
19695 static enum tag_types
19696 cp_parser_class_key (cp_parser* parser)
19698 cp_token *token;
19699 enum tag_types tag_type;
19701 /* Look for the class-key. */
19702 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19703 if (!token)
19704 return none_type;
19706 /* Check to see if the TOKEN is a class-key. */
19707 tag_type = cp_parser_token_is_class_key (token);
19708 if (!tag_type)
19709 cp_parser_error (parser, "expected class-key");
19710 return tag_type;
19713 /* Parse an (optional) member-specification.
19715 member-specification:
19716 member-declaration member-specification [opt]
19717 access-specifier : member-specification [opt] */
19719 static void
19720 cp_parser_member_specification_opt (cp_parser* parser)
19722 while (true)
19724 cp_token *token;
19725 enum rid keyword;
19727 /* Peek at the next token. */
19728 token = cp_lexer_peek_token (parser->lexer);
19729 /* If it's a `}', or EOF then we've seen all the members. */
19730 if (token->type == CPP_CLOSE_BRACE
19731 || token->type == CPP_EOF
19732 || token->type == CPP_PRAGMA_EOL)
19733 break;
19735 /* See if this token is a keyword. */
19736 keyword = token->keyword;
19737 switch (keyword)
19739 case RID_PUBLIC:
19740 case RID_PROTECTED:
19741 case RID_PRIVATE:
19742 /* Consume the access-specifier. */
19743 cp_lexer_consume_token (parser->lexer);
19744 /* Remember which access-specifier is active. */
19745 current_access_specifier = token->u.value;
19746 /* Look for the `:'. */
19747 cp_parser_require (parser, CPP_COLON, RT_COLON);
19748 break;
19750 default:
19751 /* Accept #pragmas at class scope. */
19752 if (token->type == CPP_PRAGMA)
19754 cp_parser_pragma (parser, pragma_member);
19755 break;
19758 /* Otherwise, the next construction must be a
19759 member-declaration. */
19760 cp_parser_member_declaration (parser);
19765 /* Parse a member-declaration.
19767 member-declaration:
19768 decl-specifier-seq [opt] member-declarator-list [opt] ;
19769 function-definition ; [opt]
19770 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19771 using-declaration
19772 template-declaration
19773 alias-declaration
19775 member-declarator-list:
19776 member-declarator
19777 member-declarator-list , member-declarator
19779 member-declarator:
19780 declarator pure-specifier [opt]
19781 declarator constant-initializer [opt]
19782 identifier [opt] : constant-expression
19784 GNU Extensions:
19786 member-declaration:
19787 __extension__ member-declaration
19789 member-declarator:
19790 declarator attributes [opt] pure-specifier [opt]
19791 declarator attributes [opt] constant-initializer [opt]
19792 identifier [opt] attributes [opt] : constant-expression
19794 C++0x Extensions:
19796 member-declaration:
19797 static_assert-declaration */
19799 static void
19800 cp_parser_member_declaration (cp_parser* parser)
19802 cp_decl_specifier_seq decl_specifiers;
19803 tree prefix_attributes;
19804 tree decl;
19805 int declares_class_or_enum;
19806 bool friend_p;
19807 cp_token *token = NULL;
19808 cp_token *decl_spec_token_start = NULL;
19809 cp_token *initializer_token_start = NULL;
19810 int saved_pedantic;
19811 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19813 /* Check for the `__extension__' keyword. */
19814 if (cp_parser_extension_opt (parser, &saved_pedantic))
19816 /* Recurse. */
19817 cp_parser_member_declaration (parser);
19818 /* Restore the old value of the PEDANTIC flag. */
19819 pedantic = saved_pedantic;
19821 return;
19824 /* Check for a template-declaration. */
19825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19827 /* An explicit specialization here is an error condition, and we
19828 expect the specialization handler to detect and report this. */
19829 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19830 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19831 cp_parser_explicit_specialization (parser);
19832 else
19833 cp_parser_template_declaration (parser, /*member_p=*/true);
19835 return;
19838 /* Check for a using-declaration. */
19839 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19841 if (cxx_dialect < cxx11)
19843 /* Parse the using-declaration. */
19844 cp_parser_using_declaration (parser,
19845 /*access_declaration_p=*/false);
19846 return;
19848 else
19850 tree decl;
19851 bool alias_decl_expected;
19852 cp_parser_parse_tentatively (parser);
19853 decl = cp_parser_alias_declaration (parser);
19854 /* Note that if we actually see the '=' token after the
19855 identifier, cp_parser_alias_declaration commits the
19856 tentative parse. In that case, we really expects an
19857 alias-declaration. Otherwise, we expect a using
19858 declaration. */
19859 alias_decl_expected =
19860 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19861 cp_parser_parse_definitely (parser);
19863 if (alias_decl_expected)
19864 finish_member_declaration (decl);
19865 else
19866 cp_parser_using_declaration (parser,
19867 /*access_declaration_p=*/false);
19868 return;
19872 /* Check for @defs. */
19873 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19875 tree ivar, member;
19876 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19877 ivar = ivar_chains;
19878 while (ivar)
19880 member = ivar;
19881 ivar = TREE_CHAIN (member);
19882 TREE_CHAIN (member) = NULL_TREE;
19883 finish_member_declaration (member);
19885 return;
19888 /* If the next token is `static_assert' we have a static assertion. */
19889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19891 cp_parser_static_assert (parser, /*member_p=*/true);
19892 return;
19895 parser->colon_corrects_to_scope_p = false;
19897 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19898 goto out;
19900 /* Parse the decl-specifier-seq. */
19901 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19902 cp_parser_decl_specifier_seq (parser,
19903 CP_PARSER_FLAGS_OPTIONAL,
19904 &decl_specifiers,
19905 &declares_class_or_enum);
19906 /* Check for an invalid type-name. */
19907 if (!decl_specifiers.any_type_specifiers_p
19908 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19909 goto out;
19910 /* If there is no declarator, then the decl-specifier-seq should
19911 specify a type. */
19912 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19914 /* If there was no decl-specifier-seq, and the next token is a
19915 `;', then we have something like:
19917 struct S { ; };
19919 [class.mem]
19921 Each member-declaration shall declare at least one member
19922 name of the class. */
19923 if (!decl_specifiers.any_specifiers_p)
19925 cp_token *token = cp_lexer_peek_token (parser->lexer);
19926 if (!in_system_header_at (token->location))
19927 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19929 else
19931 tree type;
19933 /* See if this declaration is a friend. */
19934 friend_p = cp_parser_friend_p (&decl_specifiers);
19935 /* If there were decl-specifiers, check to see if there was
19936 a class-declaration. */
19937 type = check_tag_decl (&decl_specifiers,
19938 /*explicit_type_instantiation_p=*/false);
19939 /* Nested classes have already been added to the class, but
19940 a `friend' needs to be explicitly registered. */
19941 if (friend_p)
19943 /* If the `friend' keyword was present, the friend must
19944 be introduced with a class-key. */
19945 if (!declares_class_or_enum && cxx_dialect < cxx11)
19946 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19947 "in C++03 a class-key must be used "
19948 "when declaring a friend");
19949 /* In this case:
19951 template <typename T> struct A {
19952 friend struct A<T>::B;
19955 A<T>::B will be represented by a TYPENAME_TYPE, and
19956 therefore not recognized by check_tag_decl. */
19957 if (!type)
19959 type = decl_specifiers.type;
19960 if (type && TREE_CODE (type) == TYPE_DECL)
19961 type = TREE_TYPE (type);
19963 if (!type || !TYPE_P (type))
19964 error_at (decl_spec_token_start->location,
19965 "friend declaration does not name a class or "
19966 "function");
19967 else
19968 make_friend_class (current_class_type, type,
19969 /*complain=*/true);
19971 /* If there is no TYPE, an error message will already have
19972 been issued. */
19973 else if (!type || type == error_mark_node)
19975 /* An anonymous aggregate has to be handled specially; such
19976 a declaration really declares a data member (with a
19977 particular type), as opposed to a nested class. */
19978 else if (ANON_AGGR_TYPE_P (type))
19980 /* C++11 9.5/6. */
19981 if (decl_specifiers.storage_class != sc_none)
19982 error_at (decl_spec_token_start->location,
19983 "a storage class on an anonymous aggregate "
19984 "in class scope is not allowed");
19986 /* Remove constructors and such from TYPE, now that we
19987 know it is an anonymous aggregate. */
19988 fixup_anonymous_aggr (type);
19989 /* And make the corresponding data member. */
19990 decl = build_decl (decl_spec_token_start->location,
19991 FIELD_DECL, NULL_TREE, type);
19992 /* Add it to the class. */
19993 finish_member_declaration (decl);
19995 else
19996 cp_parser_check_access_in_redeclaration
19997 (TYPE_NAME (type),
19998 decl_spec_token_start->location);
20001 else
20003 bool assume_semicolon = false;
20005 /* Clear attributes from the decl_specifiers but keep them
20006 around as prefix attributes that apply them to the entity
20007 being declared. */
20008 prefix_attributes = decl_specifiers.attributes;
20009 decl_specifiers.attributes = NULL_TREE;
20011 /* See if these declarations will be friends. */
20012 friend_p = cp_parser_friend_p (&decl_specifiers);
20014 /* Keep going until we hit the `;' at the end of the
20015 declaration. */
20016 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20018 tree attributes = NULL_TREE;
20019 tree first_attribute;
20021 /* Peek at the next token. */
20022 token = cp_lexer_peek_token (parser->lexer);
20024 /* Check for a bitfield declaration. */
20025 if (token->type == CPP_COLON
20026 || (token->type == CPP_NAME
20027 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20028 == CPP_COLON))
20030 tree identifier;
20031 tree width;
20033 /* Get the name of the bitfield. Note that we cannot just
20034 check TOKEN here because it may have been invalidated by
20035 the call to cp_lexer_peek_nth_token above. */
20036 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20037 identifier = cp_parser_identifier (parser);
20038 else
20039 identifier = NULL_TREE;
20041 /* Consume the `:' token. */
20042 cp_lexer_consume_token (parser->lexer);
20043 /* Get the width of the bitfield. */
20044 width
20045 = cp_parser_constant_expression (parser,
20046 /*allow_non_constant=*/false,
20047 NULL);
20049 /* Look for attributes that apply to the bitfield. */
20050 attributes = cp_parser_attributes_opt (parser);
20051 /* Remember which attributes are prefix attributes and
20052 which are not. */
20053 first_attribute = attributes;
20054 /* Combine the attributes. */
20055 attributes = chainon (prefix_attributes, attributes);
20057 /* Create the bitfield declaration. */
20058 decl = grokbitfield (identifier
20059 ? make_id_declarator (NULL_TREE,
20060 identifier,
20061 sfk_none)
20062 : NULL,
20063 &decl_specifiers,
20064 width,
20065 attributes);
20067 else
20069 cp_declarator *declarator;
20070 tree initializer;
20071 tree asm_specification;
20072 int ctor_dtor_or_conv_p;
20074 /* Parse the declarator. */
20075 declarator
20076 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20077 &ctor_dtor_or_conv_p,
20078 /*parenthesized_p=*/NULL,
20079 /*member_p=*/true);
20081 /* If something went wrong parsing the declarator, make sure
20082 that we at least consume some tokens. */
20083 if (declarator == cp_error_declarator)
20085 /* Skip to the end of the statement. */
20086 cp_parser_skip_to_end_of_statement (parser);
20087 /* If the next token is not a semicolon, that is
20088 probably because we just skipped over the body of
20089 a function. So, we consume a semicolon if
20090 present, but do not issue an error message if it
20091 is not present. */
20092 if (cp_lexer_next_token_is (parser->lexer,
20093 CPP_SEMICOLON))
20094 cp_lexer_consume_token (parser->lexer);
20095 goto out;
20098 if (declares_class_or_enum & 2)
20099 cp_parser_check_for_definition_in_return_type
20100 (declarator, decl_specifiers.type,
20101 decl_specifiers.locations[ds_type_spec]);
20103 /* Look for an asm-specification. */
20104 asm_specification = cp_parser_asm_specification_opt (parser);
20105 /* Look for attributes that apply to the declaration. */
20106 attributes = cp_parser_attributes_opt (parser);
20107 /* Remember which attributes are prefix attributes and
20108 which are not. */
20109 first_attribute = attributes;
20110 /* Combine the attributes. */
20111 attributes = chainon (prefix_attributes, attributes);
20113 /* If it's an `=', then we have a constant-initializer or a
20114 pure-specifier. It is not correct to parse the
20115 initializer before registering the member declaration
20116 since the member declaration should be in scope while
20117 its initializer is processed. However, the rest of the
20118 front end does not yet provide an interface that allows
20119 us to handle this correctly. */
20120 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20122 /* In [class.mem]:
20124 A pure-specifier shall be used only in the declaration of
20125 a virtual function.
20127 A member-declarator can contain a constant-initializer
20128 only if it declares a static member of integral or
20129 enumeration type.
20131 Therefore, if the DECLARATOR is for a function, we look
20132 for a pure-specifier; otherwise, we look for a
20133 constant-initializer. When we call `grokfield', it will
20134 perform more stringent semantics checks. */
20135 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20136 if (function_declarator_p (declarator)
20137 || (decl_specifiers.type
20138 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20139 && declarator->kind == cdk_id
20140 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20141 == FUNCTION_TYPE)))
20142 initializer = cp_parser_pure_specifier (parser);
20143 else if (decl_specifiers.storage_class != sc_static)
20144 initializer = cp_parser_save_nsdmi (parser);
20145 else if (cxx_dialect >= cxx11)
20147 bool nonconst;
20148 /* Don't require a constant rvalue in C++11, since we
20149 might want a reference constant. We'll enforce
20150 constancy later. */
20151 cp_lexer_consume_token (parser->lexer);
20152 /* Parse the initializer. */
20153 initializer = cp_parser_initializer_clause (parser,
20154 &nonconst);
20156 else
20157 /* Parse the initializer. */
20158 initializer = cp_parser_constant_initializer (parser);
20160 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20161 && !function_declarator_p (declarator))
20163 bool x;
20164 if (decl_specifiers.storage_class != sc_static)
20165 initializer = cp_parser_save_nsdmi (parser);
20166 else
20167 initializer = cp_parser_initializer (parser, &x, &x);
20169 /* Otherwise, there is no initializer. */
20170 else
20171 initializer = NULL_TREE;
20173 /* See if we are probably looking at a function
20174 definition. We are certainly not looking at a
20175 member-declarator. Calling `grokfield' has
20176 side-effects, so we must not do it unless we are sure
20177 that we are looking at a member-declarator. */
20178 if (cp_parser_token_starts_function_definition_p
20179 (cp_lexer_peek_token (parser->lexer)))
20181 /* The grammar does not allow a pure-specifier to be
20182 used when a member function is defined. (It is
20183 possible that this fact is an oversight in the
20184 standard, since a pure function may be defined
20185 outside of the class-specifier. */
20186 if (initializer && initializer_token_start)
20187 error_at (initializer_token_start->location,
20188 "pure-specifier on function-definition");
20189 decl = cp_parser_save_member_function_body (parser,
20190 &decl_specifiers,
20191 declarator,
20192 attributes);
20193 /* If the member was not a friend, declare it here. */
20194 if (!friend_p)
20196 if (parser->fully_implicit_function_template_p)
20197 decl = finish_fully_implicit_template (parser, decl);
20198 finish_member_declaration (decl);
20200 /* Peek at the next token. */
20201 token = cp_lexer_peek_token (parser->lexer);
20202 /* If the next token is a semicolon, consume it. */
20203 if (token->type == CPP_SEMICOLON)
20204 cp_lexer_consume_token (parser->lexer);
20205 goto out;
20207 else
20208 if (declarator->kind == cdk_function)
20209 declarator->id_loc = token->location;
20210 /* Create the declaration. */
20211 decl = grokfield (declarator, &decl_specifiers,
20212 initializer, /*init_const_expr_p=*/true,
20213 asm_specification, attributes);
20214 if (parser->fully_implicit_function_template_p)
20215 decl = finish_fully_implicit_template (parser, decl);
20218 cp_finalize_omp_declare_simd (parser, decl);
20220 /* Reset PREFIX_ATTRIBUTES. */
20221 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20222 attributes = TREE_CHAIN (attributes);
20223 if (attributes)
20224 TREE_CHAIN (attributes) = NULL_TREE;
20226 /* If there is any qualification still in effect, clear it
20227 now; we will be starting fresh with the next declarator. */
20228 parser->scope = NULL_TREE;
20229 parser->qualifying_scope = NULL_TREE;
20230 parser->object_scope = NULL_TREE;
20231 /* If it's a `,', then there are more declarators. */
20232 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20234 cp_lexer_consume_token (parser->lexer);
20235 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20237 cp_token *token = cp_lexer_previous_token (parser->lexer);
20238 error_at (token->location,
20239 "stray %<,%> at end of member declaration");
20242 /* If the next token isn't a `;', then we have a parse error. */
20243 else if (cp_lexer_next_token_is_not (parser->lexer,
20244 CPP_SEMICOLON))
20246 /* The next token might be a ways away from where the
20247 actual semicolon is missing. Find the previous token
20248 and use that for our error position. */
20249 cp_token *token = cp_lexer_previous_token (parser->lexer);
20250 error_at (token->location,
20251 "expected %<;%> at end of member declaration");
20253 /* Assume that the user meant to provide a semicolon. If
20254 we were to cp_parser_skip_to_end_of_statement, we might
20255 skip to a semicolon inside a member function definition
20256 and issue nonsensical error messages. */
20257 assume_semicolon = true;
20260 if (decl)
20262 /* Add DECL to the list of members. */
20263 if (!friend_p)
20264 finish_member_declaration (decl);
20266 if (TREE_CODE (decl) == FUNCTION_DECL)
20267 cp_parser_save_default_args (parser, decl);
20268 else if (TREE_CODE (decl) == FIELD_DECL
20269 && !DECL_C_BIT_FIELD (decl)
20270 && DECL_INITIAL (decl))
20271 /* Add DECL to the queue of NSDMI to be parsed later. */
20272 vec_safe_push (unparsed_nsdmis, decl);
20275 if (assume_semicolon)
20276 goto out;
20280 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20281 out:
20282 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20285 /* Parse a pure-specifier.
20287 pure-specifier:
20290 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20291 Otherwise, ERROR_MARK_NODE is returned. */
20293 static tree
20294 cp_parser_pure_specifier (cp_parser* parser)
20296 cp_token *token;
20298 /* Look for the `=' token. */
20299 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20300 return error_mark_node;
20301 /* Look for the `0' token. */
20302 token = cp_lexer_peek_token (parser->lexer);
20304 if (token->type == CPP_EOF
20305 || token->type == CPP_PRAGMA_EOL)
20306 return error_mark_node;
20308 cp_lexer_consume_token (parser->lexer);
20310 /* Accept = default or = delete in c++0x mode. */
20311 if (token->keyword == RID_DEFAULT
20312 || token->keyword == RID_DELETE)
20314 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20315 return token->u.value;
20318 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20319 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20321 cp_parser_error (parser,
20322 "invalid pure specifier (only %<= 0%> is allowed)");
20323 cp_parser_skip_to_end_of_statement (parser);
20324 return error_mark_node;
20326 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20328 error_at (token->location, "templates may not be %<virtual%>");
20329 return error_mark_node;
20332 return integer_zero_node;
20335 /* Parse a constant-initializer.
20337 constant-initializer:
20338 = constant-expression
20340 Returns a representation of the constant-expression. */
20342 static tree
20343 cp_parser_constant_initializer (cp_parser* parser)
20345 /* Look for the `=' token. */
20346 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20347 return error_mark_node;
20349 /* It is invalid to write:
20351 struct S { static const int i = { 7 }; };
20354 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20356 cp_parser_error (parser,
20357 "a brace-enclosed initializer is not allowed here");
20358 /* Consume the opening brace. */
20359 cp_lexer_consume_token (parser->lexer);
20360 /* Skip the initializer. */
20361 cp_parser_skip_to_closing_brace (parser);
20362 /* Look for the trailing `}'. */
20363 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20365 return error_mark_node;
20368 return cp_parser_constant_expression (parser,
20369 /*allow_non_constant=*/false,
20370 NULL);
20373 /* Derived classes [gram.class.derived] */
20375 /* Parse a base-clause.
20377 base-clause:
20378 : base-specifier-list
20380 base-specifier-list:
20381 base-specifier ... [opt]
20382 base-specifier-list , base-specifier ... [opt]
20384 Returns a TREE_LIST representing the base-classes, in the order in
20385 which they were declared. The representation of each node is as
20386 described by cp_parser_base_specifier.
20388 In the case that no bases are specified, this function will return
20389 NULL_TREE, not ERROR_MARK_NODE. */
20391 static tree
20392 cp_parser_base_clause (cp_parser* parser)
20394 tree bases = NULL_TREE;
20396 /* Look for the `:' that begins the list. */
20397 cp_parser_require (parser, CPP_COLON, RT_COLON);
20399 /* Scan the base-specifier-list. */
20400 while (true)
20402 cp_token *token;
20403 tree base;
20404 bool pack_expansion_p = false;
20406 /* Look for the base-specifier. */
20407 base = cp_parser_base_specifier (parser);
20408 /* Look for the (optional) ellipsis. */
20409 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20411 /* Consume the `...'. */
20412 cp_lexer_consume_token (parser->lexer);
20414 pack_expansion_p = true;
20417 /* Add BASE to the front of the list. */
20418 if (base && base != error_mark_node)
20420 if (pack_expansion_p)
20421 /* Make this a pack expansion type. */
20422 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20424 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20426 TREE_CHAIN (base) = bases;
20427 bases = base;
20430 /* Peek at the next token. */
20431 token = cp_lexer_peek_token (parser->lexer);
20432 /* If it's not a comma, then the list is complete. */
20433 if (token->type != CPP_COMMA)
20434 break;
20435 /* Consume the `,'. */
20436 cp_lexer_consume_token (parser->lexer);
20439 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20440 base class had a qualified name. However, the next name that
20441 appears is certainly not qualified. */
20442 parser->scope = NULL_TREE;
20443 parser->qualifying_scope = NULL_TREE;
20444 parser->object_scope = NULL_TREE;
20446 return nreverse (bases);
20449 /* Parse a base-specifier.
20451 base-specifier:
20452 :: [opt] nested-name-specifier [opt] class-name
20453 virtual access-specifier [opt] :: [opt] nested-name-specifier
20454 [opt] class-name
20455 access-specifier virtual [opt] :: [opt] nested-name-specifier
20456 [opt] class-name
20458 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20459 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20460 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20461 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20463 static tree
20464 cp_parser_base_specifier (cp_parser* parser)
20466 cp_token *token;
20467 bool done = false;
20468 bool virtual_p = false;
20469 bool duplicate_virtual_error_issued_p = false;
20470 bool duplicate_access_error_issued_p = false;
20471 bool class_scope_p, template_p;
20472 tree access = access_default_node;
20473 tree type;
20475 /* Process the optional `virtual' and `access-specifier'. */
20476 while (!done)
20478 /* Peek at the next token. */
20479 token = cp_lexer_peek_token (parser->lexer);
20480 /* Process `virtual'. */
20481 switch (token->keyword)
20483 case RID_VIRTUAL:
20484 /* If `virtual' appears more than once, issue an error. */
20485 if (virtual_p && !duplicate_virtual_error_issued_p)
20487 cp_parser_error (parser,
20488 "%<virtual%> specified more than once in base-specified");
20489 duplicate_virtual_error_issued_p = true;
20492 virtual_p = true;
20494 /* Consume the `virtual' token. */
20495 cp_lexer_consume_token (parser->lexer);
20497 break;
20499 case RID_PUBLIC:
20500 case RID_PROTECTED:
20501 case RID_PRIVATE:
20502 /* If more than one access specifier appears, issue an
20503 error. */
20504 if (access != access_default_node
20505 && !duplicate_access_error_issued_p)
20507 cp_parser_error (parser,
20508 "more than one access specifier in base-specified");
20509 duplicate_access_error_issued_p = true;
20512 access = ridpointers[(int) token->keyword];
20514 /* Consume the access-specifier. */
20515 cp_lexer_consume_token (parser->lexer);
20517 break;
20519 default:
20520 done = true;
20521 break;
20524 /* It is not uncommon to see programs mechanically, erroneously, use
20525 the 'typename' keyword to denote (dependent) qualified types
20526 as base classes. */
20527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20529 token = cp_lexer_peek_token (parser->lexer);
20530 if (!processing_template_decl)
20531 error_at (token->location,
20532 "keyword %<typename%> not allowed outside of templates");
20533 else
20534 error_at (token->location,
20535 "keyword %<typename%> not allowed in this context "
20536 "(the base class is implicitly a type)");
20537 cp_lexer_consume_token (parser->lexer);
20540 /* Look for the optional `::' operator. */
20541 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20542 /* Look for the nested-name-specifier. The simplest way to
20543 implement:
20545 [temp.res]
20547 The keyword `typename' is not permitted in a base-specifier or
20548 mem-initializer; in these contexts a qualified name that
20549 depends on a template-parameter is implicitly assumed to be a
20550 type name.
20552 is to pretend that we have seen the `typename' keyword at this
20553 point. */
20554 cp_parser_nested_name_specifier_opt (parser,
20555 /*typename_keyword_p=*/true,
20556 /*check_dependency_p=*/true,
20557 typename_type,
20558 /*is_declaration=*/true);
20559 /* If the base class is given by a qualified name, assume that names
20560 we see are type names or templates, as appropriate. */
20561 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20562 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20564 if (!parser->scope
20565 && cp_lexer_next_token_is_decltype (parser->lexer))
20566 /* DR 950 allows decltype as a base-specifier. */
20567 type = cp_parser_decltype (parser);
20568 else
20570 /* Otherwise, look for the class-name. */
20571 type = cp_parser_class_name (parser,
20572 class_scope_p,
20573 template_p,
20574 typename_type,
20575 /*check_dependency_p=*/true,
20576 /*class_head_p=*/false,
20577 /*is_declaration=*/true);
20578 type = TREE_TYPE (type);
20581 if (type == error_mark_node)
20582 return error_mark_node;
20584 return finish_base_specifier (type, access, virtual_p);
20587 /* Exception handling [gram.exception] */
20589 /* Parse an (optional) noexcept-specification.
20591 noexcept-specification:
20592 noexcept ( constant-expression ) [opt]
20594 If no noexcept-specification is present, returns NULL_TREE.
20595 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20596 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20597 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20598 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20599 in which case a boolean condition is returned instead. */
20601 static tree
20602 cp_parser_noexcept_specification_opt (cp_parser* parser,
20603 bool require_constexpr,
20604 bool* consumed_expr,
20605 bool return_cond)
20607 cp_token *token;
20608 const char *saved_message;
20610 /* Peek at the next token. */
20611 token = cp_lexer_peek_token (parser->lexer);
20613 /* Is it a noexcept-specification? */
20614 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20616 tree expr;
20617 cp_lexer_consume_token (parser->lexer);
20619 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20621 cp_lexer_consume_token (parser->lexer);
20623 if (require_constexpr)
20625 /* Types may not be defined in an exception-specification. */
20626 saved_message = parser->type_definition_forbidden_message;
20627 parser->type_definition_forbidden_message
20628 = G_("types may not be defined in an exception-specification");
20630 expr = cp_parser_constant_expression (parser, false, NULL);
20632 /* Restore the saved message. */
20633 parser->type_definition_forbidden_message = saved_message;
20635 else
20637 expr = cp_parser_expression (parser, false, NULL);
20638 *consumed_expr = true;
20641 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20643 else
20645 expr = boolean_true_node;
20646 if (!require_constexpr)
20647 *consumed_expr = false;
20650 /* We cannot build a noexcept-spec right away because this will check
20651 that expr is a constexpr. */
20652 if (!return_cond)
20653 return build_noexcept_spec (expr, tf_warning_or_error);
20654 else
20655 return expr;
20657 else
20658 return NULL_TREE;
20661 /* Parse an (optional) exception-specification.
20663 exception-specification:
20664 throw ( type-id-list [opt] )
20666 Returns a TREE_LIST representing the exception-specification. The
20667 TREE_VALUE of each node is a type. */
20669 static tree
20670 cp_parser_exception_specification_opt (cp_parser* parser)
20672 cp_token *token;
20673 tree type_id_list;
20674 const char *saved_message;
20676 /* Peek at the next token. */
20677 token = cp_lexer_peek_token (parser->lexer);
20679 /* Is it a noexcept-specification? */
20680 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20681 false);
20682 if (type_id_list != NULL_TREE)
20683 return type_id_list;
20685 /* If it's not `throw', then there's no exception-specification. */
20686 if (!cp_parser_is_keyword (token, RID_THROW))
20687 return NULL_TREE;
20689 #if 0
20690 /* Enable this once a lot of code has transitioned to noexcept? */
20691 if (cxx_dialect >= cxx11 && !in_system_header)
20692 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20693 "deprecated in C++0x; use %<noexcept%> instead");
20694 #endif
20696 /* Consume the `throw'. */
20697 cp_lexer_consume_token (parser->lexer);
20699 /* Look for the `('. */
20700 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20702 /* Peek at the next token. */
20703 token = cp_lexer_peek_token (parser->lexer);
20704 /* If it's not a `)', then there is a type-id-list. */
20705 if (token->type != CPP_CLOSE_PAREN)
20707 /* Types may not be defined in an exception-specification. */
20708 saved_message = parser->type_definition_forbidden_message;
20709 parser->type_definition_forbidden_message
20710 = G_("types may not be defined in an exception-specification");
20711 /* Parse the type-id-list. */
20712 type_id_list = cp_parser_type_id_list (parser);
20713 /* Restore the saved message. */
20714 parser->type_definition_forbidden_message = saved_message;
20716 else
20717 type_id_list = empty_except_spec;
20719 /* Look for the `)'. */
20720 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20722 return type_id_list;
20725 /* Parse an (optional) type-id-list.
20727 type-id-list:
20728 type-id ... [opt]
20729 type-id-list , type-id ... [opt]
20731 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20732 in the order that the types were presented. */
20734 static tree
20735 cp_parser_type_id_list (cp_parser* parser)
20737 tree types = NULL_TREE;
20739 while (true)
20741 cp_token *token;
20742 tree type;
20744 /* Get the next type-id. */
20745 type = cp_parser_type_id (parser);
20746 /* Parse the optional ellipsis. */
20747 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20749 /* Consume the `...'. */
20750 cp_lexer_consume_token (parser->lexer);
20752 /* Turn the type into a pack expansion expression. */
20753 type = make_pack_expansion (type);
20755 /* Add it to the list. */
20756 types = add_exception_specifier (types, type, /*complain=*/1);
20757 /* Peek at the next token. */
20758 token = cp_lexer_peek_token (parser->lexer);
20759 /* If it is not a `,', we are done. */
20760 if (token->type != CPP_COMMA)
20761 break;
20762 /* Consume the `,'. */
20763 cp_lexer_consume_token (parser->lexer);
20766 return nreverse (types);
20769 /* Parse a try-block.
20771 try-block:
20772 try compound-statement handler-seq */
20774 static tree
20775 cp_parser_try_block (cp_parser* parser)
20777 tree try_block;
20779 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20780 try_block = begin_try_block ();
20781 cp_parser_compound_statement (parser, NULL, true, false);
20782 finish_try_block (try_block);
20783 cp_parser_handler_seq (parser);
20784 finish_handler_sequence (try_block);
20786 return try_block;
20789 /* Parse a function-try-block.
20791 function-try-block:
20792 try ctor-initializer [opt] function-body handler-seq */
20794 static bool
20795 cp_parser_function_try_block (cp_parser* parser)
20797 tree compound_stmt;
20798 tree try_block;
20799 bool ctor_initializer_p;
20801 /* Look for the `try' keyword. */
20802 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20803 return false;
20804 /* Let the rest of the front end know where we are. */
20805 try_block = begin_function_try_block (&compound_stmt);
20806 /* Parse the function-body. */
20807 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20808 (parser, /*in_function_try_block=*/true);
20809 /* We're done with the `try' part. */
20810 finish_function_try_block (try_block);
20811 /* Parse the handlers. */
20812 cp_parser_handler_seq (parser);
20813 /* We're done with the handlers. */
20814 finish_function_handler_sequence (try_block, compound_stmt);
20816 return ctor_initializer_p;
20819 /* Parse a handler-seq.
20821 handler-seq:
20822 handler handler-seq [opt] */
20824 static void
20825 cp_parser_handler_seq (cp_parser* parser)
20827 while (true)
20829 cp_token *token;
20831 /* Parse the handler. */
20832 cp_parser_handler (parser);
20833 /* Peek at the next token. */
20834 token = cp_lexer_peek_token (parser->lexer);
20835 /* If it's not `catch' then there are no more handlers. */
20836 if (!cp_parser_is_keyword (token, RID_CATCH))
20837 break;
20841 /* Parse a handler.
20843 handler:
20844 catch ( exception-declaration ) compound-statement */
20846 static void
20847 cp_parser_handler (cp_parser* parser)
20849 tree handler;
20850 tree declaration;
20852 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20853 handler = begin_handler ();
20854 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20855 declaration = cp_parser_exception_declaration (parser);
20856 finish_handler_parms (declaration, handler);
20857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20858 cp_parser_compound_statement (parser, NULL, false, false);
20859 finish_handler (handler);
20862 /* Parse an exception-declaration.
20864 exception-declaration:
20865 type-specifier-seq declarator
20866 type-specifier-seq abstract-declarator
20867 type-specifier-seq
20870 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20871 ellipsis variant is used. */
20873 static tree
20874 cp_parser_exception_declaration (cp_parser* parser)
20876 cp_decl_specifier_seq type_specifiers;
20877 cp_declarator *declarator;
20878 const char *saved_message;
20880 /* If it's an ellipsis, it's easy to handle. */
20881 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20883 /* Consume the `...' token. */
20884 cp_lexer_consume_token (parser->lexer);
20885 return NULL_TREE;
20888 /* Types may not be defined in exception-declarations. */
20889 saved_message = parser->type_definition_forbidden_message;
20890 parser->type_definition_forbidden_message
20891 = G_("types may not be defined in exception-declarations");
20893 /* Parse the type-specifier-seq. */
20894 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20895 /*is_trailing_return=*/false,
20896 &type_specifiers);
20897 /* If it's a `)', then there is no declarator. */
20898 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20899 declarator = NULL;
20900 else
20901 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20902 /*ctor_dtor_or_conv_p=*/NULL,
20903 /*parenthesized_p=*/NULL,
20904 /*member_p=*/false);
20906 /* Restore the saved message. */
20907 parser->type_definition_forbidden_message = saved_message;
20909 if (!type_specifiers.any_specifiers_p)
20910 return error_mark_node;
20912 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20915 /* Parse a throw-expression.
20917 throw-expression:
20918 throw assignment-expression [opt]
20920 Returns a THROW_EXPR representing the throw-expression. */
20922 static tree
20923 cp_parser_throw_expression (cp_parser* parser)
20925 tree expression;
20926 cp_token* token;
20928 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20929 token = cp_lexer_peek_token (parser->lexer);
20930 /* Figure out whether or not there is an assignment-expression
20931 following the "throw" keyword. */
20932 if (token->type == CPP_COMMA
20933 || token->type == CPP_SEMICOLON
20934 || token->type == CPP_CLOSE_PAREN
20935 || token->type == CPP_CLOSE_SQUARE
20936 || token->type == CPP_CLOSE_BRACE
20937 || token->type == CPP_COLON)
20938 expression = NULL_TREE;
20939 else
20940 expression = cp_parser_assignment_expression (parser,
20941 /*cast_p=*/false, NULL);
20943 return build_throw (expression);
20946 /* GNU Extensions */
20948 /* Parse an (optional) asm-specification.
20950 asm-specification:
20951 asm ( string-literal )
20953 If the asm-specification is present, returns a STRING_CST
20954 corresponding to the string-literal. Otherwise, returns
20955 NULL_TREE. */
20957 static tree
20958 cp_parser_asm_specification_opt (cp_parser* parser)
20960 cp_token *token;
20961 tree asm_specification;
20963 /* Peek at the next token. */
20964 token = cp_lexer_peek_token (parser->lexer);
20965 /* If the next token isn't the `asm' keyword, then there's no
20966 asm-specification. */
20967 if (!cp_parser_is_keyword (token, RID_ASM))
20968 return NULL_TREE;
20970 /* Consume the `asm' token. */
20971 cp_lexer_consume_token (parser->lexer);
20972 /* Look for the `('. */
20973 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20975 /* Look for the string-literal. */
20976 asm_specification = cp_parser_string_literal (parser, false, false);
20978 /* Look for the `)'. */
20979 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20981 return asm_specification;
20984 /* Parse an asm-operand-list.
20986 asm-operand-list:
20987 asm-operand
20988 asm-operand-list , asm-operand
20990 asm-operand:
20991 string-literal ( expression )
20992 [ string-literal ] string-literal ( expression )
20994 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20995 each node is the expression. The TREE_PURPOSE is itself a
20996 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20997 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20998 is a STRING_CST for the string literal before the parenthesis. Returns
20999 ERROR_MARK_NODE if any of the operands are invalid. */
21001 static tree
21002 cp_parser_asm_operand_list (cp_parser* parser)
21004 tree asm_operands = NULL_TREE;
21005 bool invalid_operands = false;
21007 while (true)
21009 tree string_literal;
21010 tree expression;
21011 tree name;
21013 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21015 /* Consume the `[' token. */
21016 cp_lexer_consume_token (parser->lexer);
21017 /* Read the operand name. */
21018 name = cp_parser_identifier (parser);
21019 if (name != error_mark_node)
21020 name = build_string (IDENTIFIER_LENGTH (name),
21021 IDENTIFIER_POINTER (name));
21022 /* Look for the closing `]'. */
21023 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21025 else
21026 name = NULL_TREE;
21027 /* Look for the string-literal. */
21028 string_literal = cp_parser_string_literal (parser, false, false);
21030 /* Look for the `('. */
21031 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21032 /* Parse the expression. */
21033 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21034 /* Look for the `)'. */
21035 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21037 if (name == error_mark_node
21038 || string_literal == error_mark_node
21039 || expression == error_mark_node)
21040 invalid_operands = true;
21042 /* Add this operand to the list. */
21043 asm_operands = tree_cons (build_tree_list (name, string_literal),
21044 expression,
21045 asm_operands);
21046 /* If the next token is not a `,', there are no more
21047 operands. */
21048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21049 break;
21050 /* Consume the `,'. */
21051 cp_lexer_consume_token (parser->lexer);
21054 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21057 /* Parse an asm-clobber-list.
21059 asm-clobber-list:
21060 string-literal
21061 asm-clobber-list , string-literal
21063 Returns a TREE_LIST, indicating the clobbers in the order that they
21064 appeared. The TREE_VALUE of each node is a STRING_CST. */
21066 static tree
21067 cp_parser_asm_clobber_list (cp_parser* parser)
21069 tree clobbers = NULL_TREE;
21071 while (true)
21073 tree string_literal;
21075 /* Look for the string literal. */
21076 string_literal = cp_parser_string_literal (parser, false, false);
21077 /* Add it to the list. */
21078 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21079 /* If the next token is not a `,', then the list is
21080 complete. */
21081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21082 break;
21083 /* Consume the `,' token. */
21084 cp_lexer_consume_token (parser->lexer);
21087 return clobbers;
21090 /* Parse an asm-label-list.
21092 asm-label-list:
21093 identifier
21094 asm-label-list , identifier
21096 Returns a TREE_LIST, indicating the labels in the order that they
21097 appeared. The TREE_VALUE of each node is a label. */
21099 static tree
21100 cp_parser_asm_label_list (cp_parser* parser)
21102 tree labels = NULL_TREE;
21104 while (true)
21106 tree identifier, label, name;
21108 /* Look for the identifier. */
21109 identifier = cp_parser_identifier (parser);
21110 if (!error_operand_p (identifier))
21112 label = lookup_label (identifier);
21113 if (TREE_CODE (label) == LABEL_DECL)
21115 TREE_USED (label) = 1;
21116 check_goto (label);
21117 name = build_string (IDENTIFIER_LENGTH (identifier),
21118 IDENTIFIER_POINTER (identifier));
21119 labels = tree_cons (name, label, labels);
21122 /* If the next token is not a `,', then the list is
21123 complete. */
21124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21125 break;
21126 /* Consume the `,' token. */
21127 cp_lexer_consume_token (parser->lexer);
21130 return nreverse (labels);
21133 /* Return TRUE iff the next tokens in the stream are possibly the
21134 beginning of a GNU extension attribute. */
21136 static bool
21137 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21139 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21142 /* Return TRUE iff the next tokens in the stream are possibly the
21143 beginning of a standard C++-11 attribute specifier. */
21145 static bool
21146 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21148 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21151 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21152 beginning of a standard C++-11 attribute specifier. */
21154 static bool
21155 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21157 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21159 return (cxx_dialect >= cxx11
21160 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21161 || (token->type == CPP_OPEN_SQUARE
21162 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21163 && token->type == CPP_OPEN_SQUARE)));
21166 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21167 beginning of a GNU extension attribute. */
21169 static bool
21170 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21172 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21174 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21177 /* Return true iff the next tokens can be the beginning of either a
21178 GNU attribute list, or a standard C++11 attribute sequence. */
21180 static bool
21181 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21183 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21184 || cp_next_tokens_can_be_std_attribute_p (parser));
21187 /* Return true iff the next Nth tokens can be the beginning of either
21188 a GNU attribute list, or a standard C++11 attribute sequence. */
21190 static bool
21191 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21193 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21194 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21197 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21198 of GNU attributes, or return NULL. */
21200 static tree
21201 cp_parser_attributes_opt (cp_parser *parser)
21203 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21204 return cp_parser_gnu_attributes_opt (parser);
21205 return cp_parser_std_attribute_spec_seq (parser);
21208 /* Parse an (optional) series of attributes.
21210 attributes:
21211 attributes attribute
21213 attribute:
21214 __attribute__ (( attribute-list [opt] ))
21216 The return value is as for cp_parser_gnu_attribute_list. */
21218 static tree
21219 cp_parser_gnu_attributes_opt (cp_parser* parser)
21221 tree attributes = NULL_TREE;
21223 while (true)
21225 cp_token *token;
21226 tree attribute_list;
21227 bool ok = true;
21229 /* Peek at the next token. */
21230 token = cp_lexer_peek_token (parser->lexer);
21231 /* If it's not `__attribute__', then we're done. */
21232 if (token->keyword != RID_ATTRIBUTE)
21233 break;
21235 /* Consume the `__attribute__' keyword. */
21236 cp_lexer_consume_token (parser->lexer);
21237 /* Look for the two `(' tokens. */
21238 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21239 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21241 /* Peek at the next token. */
21242 token = cp_lexer_peek_token (parser->lexer);
21243 if (token->type != CPP_CLOSE_PAREN)
21244 /* Parse the attribute-list. */
21245 attribute_list = cp_parser_gnu_attribute_list (parser);
21246 else
21247 /* If the next token is a `)', then there is no attribute
21248 list. */
21249 attribute_list = NULL;
21251 /* Look for the two `)' tokens. */
21252 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21253 ok = false;
21254 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21255 ok = false;
21256 if (!ok)
21257 cp_parser_skip_to_end_of_statement (parser);
21259 /* Add these new attributes to the list. */
21260 attributes = chainon (attributes, attribute_list);
21263 return attributes;
21266 /* Parse a GNU attribute-list.
21268 attribute-list:
21269 attribute
21270 attribute-list , attribute
21272 attribute:
21273 identifier
21274 identifier ( identifier )
21275 identifier ( identifier , expression-list )
21276 identifier ( expression-list )
21278 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21279 to an attribute. The TREE_PURPOSE of each node is the identifier
21280 indicating which attribute is in use. The TREE_VALUE represents
21281 the arguments, if any. */
21283 static tree
21284 cp_parser_gnu_attribute_list (cp_parser* parser)
21286 tree attribute_list = NULL_TREE;
21287 bool save_translate_strings_p = parser->translate_strings_p;
21289 parser->translate_strings_p = false;
21290 while (true)
21292 cp_token *token;
21293 tree identifier;
21294 tree attribute;
21296 /* Look for the identifier. We also allow keywords here; for
21297 example `__attribute__ ((const))' is legal. */
21298 token = cp_lexer_peek_token (parser->lexer);
21299 if (token->type == CPP_NAME
21300 || token->type == CPP_KEYWORD)
21302 tree arguments = NULL_TREE;
21304 /* Consume the token. */
21305 token = cp_lexer_consume_token (parser->lexer);
21307 /* Save away the identifier that indicates which attribute
21308 this is. */
21309 identifier = (token->type == CPP_KEYWORD)
21310 /* For keywords, use the canonical spelling, not the
21311 parsed identifier. */
21312 ? ridpointers[(int) token->keyword]
21313 : token->u.value;
21315 attribute = build_tree_list (identifier, NULL_TREE);
21317 /* Peek at the next token. */
21318 token = cp_lexer_peek_token (parser->lexer);
21319 /* If it's an `(', then parse the attribute arguments. */
21320 if (token->type == CPP_OPEN_PAREN)
21322 vec<tree, va_gc> *vec;
21323 int attr_flag = (attribute_takes_identifier_p (identifier)
21324 ? id_attr : normal_attr);
21325 vec = cp_parser_parenthesized_expression_list
21326 (parser, attr_flag, /*cast_p=*/false,
21327 /*allow_expansion_p=*/false,
21328 /*non_constant_p=*/NULL);
21329 if (vec == NULL)
21330 arguments = error_mark_node;
21331 else
21333 arguments = build_tree_list_vec (vec);
21334 release_tree_vector (vec);
21336 /* Save the arguments away. */
21337 TREE_VALUE (attribute) = arguments;
21340 if (arguments != error_mark_node)
21342 /* Add this attribute to the list. */
21343 TREE_CHAIN (attribute) = attribute_list;
21344 attribute_list = attribute;
21347 token = cp_lexer_peek_token (parser->lexer);
21349 /* Now, look for more attributes. If the next token isn't a
21350 `,', we're done. */
21351 if (token->type != CPP_COMMA)
21352 break;
21354 /* Consume the comma and keep going. */
21355 cp_lexer_consume_token (parser->lexer);
21357 parser->translate_strings_p = save_translate_strings_p;
21359 /* We built up the list in reverse order. */
21360 return nreverse (attribute_list);
21363 /* Parse a standard C++11 attribute.
21365 The returned representation is a TREE_LIST which TREE_PURPOSE is
21366 the scoped name of the attribute, and the TREE_VALUE is its
21367 arguments list.
21369 Note that the scoped name of the attribute is itself a TREE_LIST
21370 which TREE_PURPOSE is the namespace of the attribute, and
21371 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21372 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21373 and which TREE_PURPOSE is directly the attribute name.
21375 Clients of the attribute code should use get_attribute_namespace
21376 and get_attribute_name to get the actual namespace and name of
21377 attributes, regardless of their being GNU or C++11 attributes.
21379 attribute:
21380 attribute-token attribute-argument-clause [opt]
21382 attribute-token:
21383 identifier
21384 attribute-scoped-token
21386 attribute-scoped-token:
21387 attribute-namespace :: identifier
21389 attribute-namespace:
21390 identifier
21392 attribute-argument-clause:
21393 ( balanced-token-seq )
21395 balanced-token-seq:
21396 balanced-token [opt]
21397 balanced-token-seq balanced-token
21399 balanced-token:
21400 ( balanced-token-seq )
21401 [ balanced-token-seq ]
21402 { balanced-token-seq }. */
21404 static tree
21405 cp_parser_std_attribute (cp_parser *parser)
21407 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21408 cp_token *token;
21410 /* First, parse name of the the attribute, a.k.a
21411 attribute-token. */
21413 token = cp_lexer_peek_token (parser->lexer);
21414 if (token->type == CPP_NAME)
21415 attr_id = token->u.value;
21416 else if (token->type == CPP_KEYWORD)
21417 attr_id = ridpointers[(int) token->keyword];
21418 else if (token->flags & NAMED_OP)
21419 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21421 if (attr_id == NULL_TREE)
21422 return NULL_TREE;
21424 cp_lexer_consume_token (parser->lexer);
21426 token = cp_lexer_peek_token (parser->lexer);
21427 if (token->type == CPP_SCOPE)
21429 /* We are seeing a scoped attribute token. */
21431 cp_lexer_consume_token (parser->lexer);
21432 attr_ns = attr_id;
21434 token = cp_lexer_consume_token (parser->lexer);
21435 if (token->type == CPP_NAME)
21436 attr_id = token->u.value;
21437 else if (token->type == CPP_KEYWORD)
21438 attr_id = ridpointers[(int) token->keyword];
21439 else
21441 error_at (token->location,
21442 "expected an identifier for the attribute name");
21443 return error_mark_node;
21445 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21446 NULL_TREE);
21447 token = cp_lexer_peek_token (parser->lexer);
21449 else
21451 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21452 NULL_TREE);
21453 /* C++11 noreturn attribute is equivalent to GNU's. */
21454 if (is_attribute_p ("noreturn", attr_id))
21455 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21456 /* C++14 deprecated attribute is equivalent to GNU's. */
21457 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21458 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21461 /* Now parse the optional argument clause of the attribute. */
21463 if (token->type != CPP_OPEN_PAREN)
21464 return attribute;
21467 vec<tree, va_gc> *vec;
21468 int attr_flag = normal_attr;
21470 if (attr_ns == get_identifier ("gnu")
21471 && attribute_takes_identifier_p (attr_id))
21472 /* A GNU attribute that takes an identifier in parameter. */
21473 attr_flag = id_attr;
21475 vec = cp_parser_parenthesized_expression_list
21476 (parser, attr_flag, /*cast_p=*/false,
21477 /*allow_expansion_p=*/true,
21478 /*non_constant_p=*/NULL);
21479 if (vec == NULL)
21480 arguments = error_mark_node;
21481 else
21483 arguments = build_tree_list_vec (vec);
21484 release_tree_vector (vec);
21487 if (arguments == error_mark_node)
21488 attribute = error_mark_node;
21489 else
21490 TREE_VALUE (attribute) = arguments;
21493 return attribute;
21496 /* Parse a list of standard C++-11 attributes.
21498 attribute-list:
21499 attribute [opt]
21500 attribute-list , attribute[opt]
21501 attribute ...
21502 attribute-list , attribute ...
21505 static tree
21506 cp_parser_std_attribute_list (cp_parser *parser)
21508 tree attributes = NULL_TREE, attribute = NULL_TREE;
21509 cp_token *token = NULL;
21511 while (true)
21513 attribute = cp_parser_std_attribute (parser);
21514 if (attribute == error_mark_node)
21515 break;
21516 if (attribute != NULL_TREE)
21518 TREE_CHAIN (attribute) = attributes;
21519 attributes = attribute;
21521 token = cp_lexer_peek_token (parser->lexer);
21522 if (token->type != CPP_COMMA)
21523 break;
21524 cp_lexer_consume_token (parser->lexer);
21526 attributes = nreverse (attributes);
21527 return attributes;
21530 /* Parse a standard C++-11 attribute specifier.
21532 attribute-specifier:
21533 [ [ attribute-list ] ]
21534 alignment-specifier
21536 alignment-specifier:
21537 alignas ( type-id ... [opt] )
21538 alignas ( alignment-expression ... [opt] ). */
21540 static tree
21541 cp_parser_std_attribute_spec (cp_parser *parser)
21543 tree attributes = NULL_TREE;
21544 cp_token *token = cp_lexer_peek_token (parser->lexer);
21546 if (token->type == CPP_OPEN_SQUARE
21547 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21549 cp_lexer_consume_token (parser->lexer);
21550 cp_lexer_consume_token (parser->lexer);
21552 attributes = cp_parser_std_attribute_list (parser);
21554 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21555 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21556 cp_parser_skip_to_end_of_statement (parser);
21557 else
21558 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21559 when we are sure that we have actually parsed them. */
21560 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21562 else
21564 tree alignas_expr;
21566 /* Look for an alignment-specifier. */
21568 token = cp_lexer_peek_token (parser->lexer);
21570 if (token->type != CPP_KEYWORD
21571 || token->keyword != RID_ALIGNAS)
21572 return NULL_TREE;
21574 cp_lexer_consume_token (parser->lexer);
21575 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21577 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21579 cp_parser_error (parser, "expected %<(%>");
21580 return error_mark_node;
21583 cp_parser_parse_tentatively (parser);
21584 alignas_expr = cp_parser_type_id (parser);
21586 if (!cp_parser_parse_definitely (parser))
21588 gcc_assert (alignas_expr == error_mark_node
21589 || alignas_expr == NULL_TREE);
21591 alignas_expr =
21592 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21593 /**cp_id_kind=*/NULL);
21594 if (alignas_expr == error_mark_node)
21595 cp_parser_skip_to_end_of_statement (parser);
21596 if (alignas_expr == NULL_TREE
21597 || alignas_expr == error_mark_node)
21598 return alignas_expr;
21601 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
21603 cp_parser_error (parser, "expected %<)%>");
21604 return error_mark_node;
21607 alignas_expr = cxx_alignas_expr (alignas_expr);
21609 /* Build the C++-11 representation of an 'aligned'
21610 attribute. */
21611 attributes =
21612 build_tree_list (build_tree_list (get_identifier ("gnu"),
21613 get_identifier ("aligned")),
21614 build_tree_list (NULL_TREE, alignas_expr));
21617 return attributes;
21620 /* Parse a standard C++-11 attribute-specifier-seq.
21622 attribute-specifier-seq:
21623 attribute-specifier-seq [opt] attribute-specifier
21626 static tree
21627 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21629 tree attr_specs = NULL;
21631 while (true)
21633 tree attr_spec = cp_parser_std_attribute_spec (parser);
21634 if (attr_spec == NULL_TREE)
21635 break;
21636 if (attr_spec == error_mark_node)
21637 return error_mark_node;
21639 TREE_CHAIN (attr_spec) = attr_specs;
21640 attr_specs = attr_spec;
21643 attr_specs = nreverse (attr_specs);
21644 return attr_specs;
21647 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21648 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21649 current value of the PEDANTIC flag, regardless of whether or not
21650 the `__extension__' keyword is present. The caller is responsible
21651 for restoring the value of the PEDANTIC flag. */
21653 static bool
21654 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21656 /* Save the old value of the PEDANTIC flag. */
21657 *saved_pedantic = pedantic;
21659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21661 /* Consume the `__extension__' token. */
21662 cp_lexer_consume_token (parser->lexer);
21663 /* We're not being pedantic while the `__extension__' keyword is
21664 in effect. */
21665 pedantic = 0;
21667 return true;
21670 return false;
21673 /* Parse a label declaration.
21675 label-declaration:
21676 __label__ label-declarator-seq ;
21678 label-declarator-seq:
21679 identifier , label-declarator-seq
21680 identifier */
21682 static void
21683 cp_parser_label_declaration (cp_parser* parser)
21685 /* Look for the `__label__' keyword. */
21686 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21688 while (true)
21690 tree identifier;
21692 /* Look for an identifier. */
21693 identifier = cp_parser_identifier (parser);
21694 /* If we failed, stop. */
21695 if (identifier == error_mark_node)
21696 break;
21697 /* Declare it as a label. */
21698 finish_label_decl (identifier);
21699 /* If the next token is a `;', stop. */
21700 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21701 break;
21702 /* Look for the `,' separating the label declarations. */
21703 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21706 /* Look for the final `;'. */
21707 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21710 /* Support Functions */
21712 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21713 NAME should have one of the representations used for an
21714 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21715 is returned. If PARSER->SCOPE is a dependent type, then a
21716 SCOPE_REF is returned.
21718 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21719 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21720 was formed. Abstractly, such entities should not be passed to this
21721 function, because they do not need to be looked up, but it is
21722 simpler to check for this special case here, rather than at the
21723 call-sites.
21725 In cases not explicitly covered above, this function returns a
21726 DECL, OVERLOAD, or baselink representing the result of the lookup.
21727 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21728 is returned.
21730 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21731 (e.g., "struct") that was used. In that case bindings that do not
21732 refer to types are ignored.
21734 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21735 ignored.
21737 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21738 are ignored.
21740 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21741 types.
21743 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21744 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21745 NULL_TREE otherwise. */
21747 static tree
21748 cp_parser_lookup_name (cp_parser *parser, tree name,
21749 enum tag_types tag_type,
21750 bool is_template,
21751 bool is_namespace,
21752 bool check_dependency,
21753 tree *ambiguous_decls,
21754 location_t name_location)
21756 tree decl;
21757 tree object_type = parser->context->object_type;
21759 /* Assume that the lookup will be unambiguous. */
21760 if (ambiguous_decls)
21761 *ambiguous_decls = NULL_TREE;
21763 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21764 no longer valid. Note that if we are parsing tentatively, and
21765 the parse fails, OBJECT_TYPE will be automatically restored. */
21766 parser->context->object_type = NULL_TREE;
21768 if (name == error_mark_node)
21769 return error_mark_node;
21771 /* A template-id has already been resolved; there is no lookup to
21772 do. */
21773 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21774 return name;
21775 if (BASELINK_P (name))
21777 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21778 == TEMPLATE_ID_EXPR);
21779 return name;
21782 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21783 it should already have been checked to make sure that the name
21784 used matches the type being destroyed. */
21785 if (TREE_CODE (name) == BIT_NOT_EXPR)
21787 tree type;
21789 /* Figure out to which type this destructor applies. */
21790 if (parser->scope)
21791 type = parser->scope;
21792 else if (object_type)
21793 type = object_type;
21794 else
21795 type = current_class_type;
21796 /* If that's not a class type, there is no destructor. */
21797 if (!type || !CLASS_TYPE_P (type))
21798 return error_mark_node;
21799 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21800 lazily_declare_fn (sfk_destructor, type);
21801 if (!CLASSTYPE_DESTRUCTORS (type))
21802 return error_mark_node;
21803 /* If it was a class type, return the destructor. */
21804 return CLASSTYPE_DESTRUCTORS (type);
21807 /* By this point, the NAME should be an ordinary identifier. If
21808 the id-expression was a qualified name, the qualifying scope is
21809 stored in PARSER->SCOPE at this point. */
21810 gcc_assert (identifier_p (name));
21812 /* Perform the lookup. */
21813 if (parser->scope)
21815 bool dependent_p;
21817 if (parser->scope == error_mark_node)
21818 return error_mark_node;
21820 /* If the SCOPE is dependent, the lookup must be deferred until
21821 the template is instantiated -- unless we are explicitly
21822 looking up names in uninstantiated templates. Even then, we
21823 cannot look up the name if the scope is not a class type; it
21824 might, for example, be a template type parameter. */
21825 dependent_p = (TYPE_P (parser->scope)
21826 && dependent_scope_p (parser->scope));
21827 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21828 && dependent_p)
21829 /* Defer lookup. */
21830 decl = error_mark_node;
21831 else
21833 tree pushed_scope = NULL_TREE;
21835 /* If PARSER->SCOPE is a dependent type, then it must be a
21836 class type, and we must not be checking dependencies;
21837 otherwise, we would have processed this lookup above. So
21838 that PARSER->SCOPE is not considered a dependent base by
21839 lookup_member, we must enter the scope here. */
21840 if (dependent_p)
21841 pushed_scope = push_scope (parser->scope);
21843 /* If the PARSER->SCOPE is a template specialization, it
21844 may be instantiated during name lookup. In that case,
21845 errors may be issued. Even if we rollback the current
21846 tentative parse, those errors are valid. */
21847 decl = lookup_qualified_name (parser->scope, name,
21848 tag_type != none_type,
21849 /*complain=*/true);
21851 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21852 lookup result and the nested-name-specifier nominates a class C:
21853 * if the name specified after the nested-name-specifier, when
21854 looked up in C, is the injected-class-name of C (Clause 9), or
21855 * if the name specified after the nested-name-specifier is the
21856 same as the identifier or the simple-template-id's template-
21857 name in the last component of the nested-name-specifier,
21858 the name is instead considered to name the constructor of
21859 class C. [ Note: for example, the constructor is not an
21860 acceptable lookup result in an elaborated-type-specifier so
21861 the constructor would not be used in place of the
21862 injected-class-name. --end note ] Such a constructor name
21863 shall be used only in the declarator-id of a declaration that
21864 names a constructor or in a using-declaration. */
21865 if (tag_type == none_type
21866 && DECL_SELF_REFERENCE_P (decl)
21867 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21868 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21869 tag_type != none_type,
21870 /*complain=*/true);
21872 /* If we have a single function from a using decl, pull it out. */
21873 if (TREE_CODE (decl) == OVERLOAD
21874 && !really_overloaded_fn (decl))
21875 decl = OVL_FUNCTION (decl);
21877 if (pushed_scope)
21878 pop_scope (pushed_scope);
21881 /* If the scope is a dependent type and either we deferred lookup or
21882 we did lookup but didn't find the name, rememeber the name. */
21883 if (decl == error_mark_node && TYPE_P (parser->scope)
21884 && dependent_type_p (parser->scope))
21886 if (tag_type)
21888 tree type;
21890 /* The resolution to Core Issue 180 says that `struct
21891 A::B' should be considered a type-name, even if `A'
21892 is dependent. */
21893 type = make_typename_type (parser->scope, name, tag_type,
21894 /*complain=*/tf_error);
21895 if (type != error_mark_node)
21896 decl = TYPE_NAME (type);
21898 else if (is_template
21899 && (cp_parser_next_token_ends_template_argument_p (parser)
21900 || cp_lexer_next_token_is (parser->lexer,
21901 CPP_CLOSE_PAREN)))
21902 decl = make_unbound_class_template (parser->scope,
21903 name, NULL_TREE,
21904 /*complain=*/tf_error);
21905 else
21906 decl = build_qualified_name (/*type=*/NULL_TREE,
21907 parser->scope, name,
21908 is_template);
21910 parser->qualifying_scope = parser->scope;
21911 parser->object_scope = NULL_TREE;
21913 else if (object_type)
21915 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21916 OBJECT_TYPE is not a class. */
21917 if (CLASS_TYPE_P (object_type))
21918 /* If the OBJECT_TYPE is a template specialization, it may
21919 be instantiated during name lookup. In that case, errors
21920 may be issued. Even if we rollback the current tentative
21921 parse, those errors are valid. */
21922 decl = lookup_member (object_type,
21923 name,
21924 /*protect=*/0,
21925 tag_type != none_type,
21926 tf_warning_or_error);
21927 else
21928 decl = NULL_TREE;
21930 if (!decl)
21931 /* Look it up in the enclosing context. */
21932 decl = lookup_name_real (name, tag_type != none_type,
21933 /*nonclass=*/0,
21934 /*block_p=*/true, is_namespace, 0);
21935 parser->object_scope = object_type;
21936 parser->qualifying_scope = NULL_TREE;
21938 else
21940 decl = lookup_name_real (name, tag_type != none_type,
21941 /*nonclass=*/0,
21942 /*block_p=*/true, is_namespace, 0);
21943 parser->qualifying_scope = NULL_TREE;
21944 parser->object_scope = NULL_TREE;
21947 /* If the lookup failed, let our caller know. */
21948 if (!decl || decl == error_mark_node)
21949 return error_mark_node;
21951 /* Pull out the template from an injected-class-name (or multiple). */
21952 if (is_template)
21953 decl = maybe_get_template_decl_from_type_decl (decl);
21955 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21956 if (TREE_CODE (decl) == TREE_LIST)
21958 if (ambiguous_decls)
21959 *ambiguous_decls = decl;
21960 /* The error message we have to print is too complicated for
21961 cp_parser_error, so we incorporate its actions directly. */
21962 if (!cp_parser_simulate_error (parser))
21964 error_at (name_location, "reference to %qD is ambiguous",
21965 name);
21966 print_candidates (decl);
21968 return error_mark_node;
21971 gcc_assert (DECL_P (decl)
21972 || TREE_CODE (decl) == OVERLOAD
21973 || TREE_CODE (decl) == SCOPE_REF
21974 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21975 || BASELINK_P (decl));
21977 /* If we have resolved the name of a member declaration, check to
21978 see if the declaration is accessible. When the name resolves to
21979 set of overloaded functions, accessibility is checked when
21980 overload resolution is done.
21982 During an explicit instantiation, access is not checked at all,
21983 as per [temp.explicit]. */
21984 if (DECL_P (decl))
21985 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21987 maybe_record_typedef_use (decl);
21989 return decl;
21992 /* Like cp_parser_lookup_name, but for use in the typical case where
21993 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21994 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21996 static tree
21997 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21999 return cp_parser_lookup_name (parser, name,
22000 none_type,
22001 /*is_template=*/false,
22002 /*is_namespace=*/false,
22003 /*check_dependency=*/true,
22004 /*ambiguous_decls=*/NULL,
22005 location);
22008 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22009 the current context, return the TYPE_DECL. If TAG_NAME_P is
22010 true, the DECL indicates the class being defined in a class-head,
22011 or declared in an elaborated-type-specifier.
22013 Otherwise, return DECL. */
22015 static tree
22016 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22018 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22019 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22021 struct A {
22022 template <typename T> struct B;
22025 template <typename T> struct A::B {};
22027 Similarly, in an elaborated-type-specifier:
22029 namespace N { struct X{}; }
22031 struct A {
22032 template <typename T> friend struct N::X;
22035 However, if the DECL refers to a class type, and we are in
22036 the scope of the class, then the name lookup automatically
22037 finds the TYPE_DECL created by build_self_reference rather
22038 than a TEMPLATE_DECL. For example, in:
22040 template <class T> struct S {
22041 S s;
22044 there is no need to handle such case. */
22046 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22047 return DECL_TEMPLATE_RESULT (decl);
22049 return decl;
22052 /* If too many, or too few, template-parameter lists apply to the
22053 declarator, issue an error message. Returns TRUE if all went well,
22054 and FALSE otherwise. */
22056 static bool
22057 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22058 cp_declarator *declarator,
22059 location_t declarator_location)
22061 switch (declarator->kind)
22063 case cdk_id:
22065 unsigned num_templates = 0;
22066 tree scope = declarator->u.id.qualifying_scope;
22068 if (scope)
22069 num_templates = num_template_headers_for_class (scope);
22070 else if (TREE_CODE (declarator->u.id.unqualified_name)
22071 == TEMPLATE_ID_EXPR)
22072 /* If the DECLARATOR has the form `X<y>' then it uses one
22073 additional level of template parameters. */
22074 ++num_templates;
22076 return cp_parser_check_template_parameters
22077 (parser, num_templates, declarator_location, declarator);
22080 case cdk_function:
22081 case cdk_array:
22082 case cdk_pointer:
22083 case cdk_reference:
22084 case cdk_ptrmem:
22085 return (cp_parser_check_declarator_template_parameters
22086 (parser, declarator->declarator, declarator_location));
22088 case cdk_error:
22089 return true;
22091 default:
22092 gcc_unreachable ();
22094 return false;
22097 /* NUM_TEMPLATES were used in the current declaration. If that is
22098 invalid, return FALSE and issue an error messages. Otherwise,
22099 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22100 declarator and we can print more accurate diagnostics. */
22102 static bool
22103 cp_parser_check_template_parameters (cp_parser* parser,
22104 unsigned num_templates,
22105 location_t location,
22106 cp_declarator *declarator)
22108 /* If there are the same number of template classes and parameter
22109 lists, that's OK. */
22110 if (parser->num_template_parameter_lists == num_templates)
22111 return true;
22112 /* If there are more, but only one more, then we are referring to a
22113 member template. That's OK too. */
22114 if (parser->num_template_parameter_lists == num_templates + 1)
22115 return true;
22116 /* If there are more template classes than parameter lists, we have
22117 something like:
22119 template <class T> void S<T>::R<T>::f (); */
22120 if (parser->num_template_parameter_lists < num_templates)
22122 if (declarator && !current_function_decl)
22123 error_at (location, "specializing member %<%T::%E%> "
22124 "requires %<template<>%> syntax",
22125 declarator->u.id.qualifying_scope,
22126 declarator->u.id.unqualified_name);
22127 else if (declarator)
22128 error_at (location, "invalid declaration of %<%T::%E%>",
22129 declarator->u.id.qualifying_scope,
22130 declarator->u.id.unqualified_name);
22131 else
22132 error_at (location, "too few template-parameter-lists");
22133 return false;
22135 /* Otherwise, there are too many template parameter lists. We have
22136 something like:
22138 template <class T> template <class U> void S::f(); */
22139 error_at (location, "too many template-parameter-lists");
22140 return false;
22143 /* Parse an optional `::' token indicating that the following name is
22144 from the global namespace. If so, PARSER->SCOPE is set to the
22145 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22146 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22147 Returns the new value of PARSER->SCOPE, if the `::' token is
22148 present, and NULL_TREE otherwise. */
22150 static tree
22151 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22153 cp_token *token;
22155 /* Peek at the next token. */
22156 token = cp_lexer_peek_token (parser->lexer);
22157 /* If we're looking at a `::' token then we're starting from the
22158 global namespace, not our current location. */
22159 if (token->type == CPP_SCOPE)
22161 /* Consume the `::' token. */
22162 cp_lexer_consume_token (parser->lexer);
22163 /* Set the SCOPE so that we know where to start the lookup. */
22164 parser->scope = global_namespace;
22165 parser->qualifying_scope = global_namespace;
22166 parser->object_scope = NULL_TREE;
22168 return parser->scope;
22170 else if (!current_scope_valid_p)
22172 parser->scope = NULL_TREE;
22173 parser->qualifying_scope = NULL_TREE;
22174 parser->object_scope = NULL_TREE;
22177 return NULL_TREE;
22180 /* Returns TRUE if the upcoming token sequence is the start of a
22181 constructor declarator. If FRIEND_P is true, the declarator is
22182 preceded by the `friend' specifier. */
22184 static bool
22185 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22187 bool constructor_p;
22188 tree nested_name_specifier;
22189 cp_token *next_token;
22191 /* The common case is that this is not a constructor declarator, so
22192 try to avoid doing lots of work if at all possible. It's not
22193 valid declare a constructor at function scope. */
22194 if (parser->in_function_body)
22195 return false;
22196 /* And only certain tokens can begin a constructor declarator. */
22197 next_token = cp_lexer_peek_token (parser->lexer);
22198 if (next_token->type != CPP_NAME
22199 && next_token->type != CPP_SCOPE
22200 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22201 && next_token->type != CPP_TEMPLATE_ID)
22202 return false;
22204 /* Parse tentatively; we are going to roll back all of the tokens
22205 consumed here. */
22206 cp_parser_parse_tentatively (parser);
22207 /* Assume that we are looking at a constructor declarator. */
22208 constructor_p = true;
22210 /* Look for the optional `::' operator. */
22211 cp_parser_global_scope_opt (parser,
22212 /*current_scope_valid_p=*/false);
22213 /* Look for the nested-name-specifier. */
22214 nested_name_specifier
22215 = (cp_parser_nested_name_specifier_opt (parser,
22216 /*typename_keyword_p=*/false,
22217 /*check_dependency_p=*/false,
22218 /*type_p=*/false,
22219 /*is_declaration=*/false));
22220 /* Outside of a class-specifier, there must be a
22221 nested-name-specifier. */
22222 if (!nested_name_specifier &&
22223 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
22224 || friend_p))
22225 constructor_p = false;
22226 else if (nested_name_specifier == error_mark_node)
22227 constructor_p = false;
22229 /* If we have a class scope, this is easy; DR 147 says that S::S always
22230 names the constructor, and no other qualified name could. */
22231 if (constructor_p && nested_name_specifier
22232 && CLASS_TYPE_P (nested_name_specifier))
22234 tree id = cp_parser_unqualified_id (parser,
22235 /*template_keyword_p=*/false,
22236 /*check_dependency_p=*/false,
22237 /*declarator_p=*/true,
22238 /*optional_p=*/false);
22239 if (is_overloaded_fn (id))
22240 id = DECL_NAME (get_first_fn (id));
22241 if (!constructor_name_p (id, nested_name_specifier))
22242 constructor_p = false;
22244 /* If we still think that this might be a constructor-declarator,
22245 look for a class-name. */
22246 else if (constructor_p)
22248 /* If we have:
22250 template <typename T> struct S {
22251 S();
22254 we must recognize that the nested `S' names a class. */
22255 tree type_decl;
22256 type_decl = cp_parser_class_name (parser,
22257 /*typename_keyword_p=*/false,
22258 /*template_keyword_p=*/false,
22259 none_type,
22260 /*check_dependency_p=*/false,
22261 /*class_head_p=*/false,
22262 /*is_declaration=*/false);
22263 /* If there was no class-name, then this is not a constructor. */
22264 constructor_p = !cp_parser_error_occurred (parser);
22266 /* If we're still considering a constructor, we have to see a `(',
22267 to begin the parameter-declaration-clause, followed by either a
22268 `)', an `...', or a decl-specifier. We need to check for a
22269 type-specifier to avoid being fooled into thinking that:
22271 S (f) (int);
22273 is a constructor. (It is actually a function named `f' that
22274 takes one parameter (of type `int') and returns a value of type
22275 `S'. */
22276 if (constructor_p
22277 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22278 constructor_p = false;
22280 if (constructor_p
22281 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22282 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22283 /* A parameter declaration begins with a decl-specifier,
22284 which is either the "attribute" keyword, a storage class
22285 specifier, or (usually) a type-specifier. */
22286 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22288 tree type;
22289 tree pushed_scope = NULL_TREE;
22290 unsigned saved_num_template_parameter_lists;
22292 /* Names appearing in the type-specifier should be looked up
22293 in the scope of the class. */
22294 if (current_class_type)
22295 type = NULL_TREE;
22296 else
22298 type = TREE_TYPE (type_decl);
22299 if (TREE_CODE (type) == TYPENAME_TYPE)
22301 type = resolve_typename_type (type,
22302 /*only_current_p=*/false);
22303 if (TREE_CODE (type) == TYPENAME_TYPE)
22305 cp_parser_abort_tentative_parse (parser);
22306 return false;
22309 pushed_scope = push_scope (type);
22312 /* Inside the constructor parameter list, surrounding
22313 template-parameter-lists do not apply. */
22314 saved_num_template_parameter_lists
22315 = parser->num_template_parameter_lists;
22316 parser->num_template_parameter_lists = 0;
22318 /* Look for the type-specifier. */
22319 cp_parser_type_specifier (parser,
22320 CP_PARSER_FLAGS_NONE,
22321 /*decl_specs=*/NULL,
22322 /*is_declarator=*/true,
22323 /*declares_class_or_enum=*/NULL,
22324 /*is_cv_qualifier=*/NULL);
22326 parser->num_template_parameter_lists
22327 = saved_num_template_parameter_lists;
22329 /* Leave the scope of the class. */
22330 if (pushed_scope)
22331 pop_scope (pushed_scope);
22333 constructor_p = !cp_parser_error_occurred (parser);
22337 /* We did not really want to consume any tokens. */
22338 cp_parser_abort_tentative_parse (parser);
22340 return constructor_p;
22343 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22344 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22345 they must be performed once we are in the scope of the function.
22347 Returns the function defined. */
22349 static tree
22350 cp_parser_function_definition_from_specifiers_and_declarator
22351 (cp_parser* parser,
22352 cp_decl_specifier_seq *decl_specifiers,
22353 tree attributes,
22354 const cp_declarator *declarator)
22356 tree fn;
22357 bool success_p;
22359 /* Begin the function-definition. */
22360 success_p = start_function (decl_specifiers, declarator, attributes);
22362 /* The things we're about to see are not directly qualified by any
22363 template headers we've seen thus far. */
22364 reset_specialization ();
22366 /* If there were names looked up in the decl-specifier-seq that we
22367 did not check, check them now. We must wait until we are in the
22368 scope of the function to perform the checks, since the function
22369 might be a friend. */
22370 perform_deferred_access_checks (tf_warning_or_error);
22372 if (success_p)
22374 cp_finalize_omp_declare_simd (parser, current_function_decl);
22375 parser->omp_declare_simd = NULL;
22378 if (!success_p)
22380 /* Skip the entire function. */
22381 cp_parser_skip_to_end_of_block_or_statement (parser);
22382 fn = error_mark_node;
22384 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22386 /* Seen already, skip it. An error message has already been output. */
22387 cp_parser_skip_to_end_of_block_or_statement (parser);
22388 fn = current_function_decl;
22389 current_function_decl = NULL_TREE;
22390 /* If this is a function from a class, pop the nested class. */
22391 if (current_class_name)
22392 pop_nested_class ();
22394 else
22396 timevar_id_t tv;
22397 if (DECL_DECLARED_INLINE_P (current_function_decl))
22398 tv = TV_PARSE_INLINE;
22399 else
22400 tv = TV_PARSE_FUNC;
22401 timevar_push (tv);
22402 fn = cp_parser_function_definition_after_declarator (parser,
22403 /*inline_p=*/false);
22404 timevar_pop (tv);
22407 return fn;
22410 /* Parse the part of a function-definition that follows the
22411 declarator. INLINE_P is TRUE iff this function is an inline
22412 function defined within a class-specifier.
22414 Returns the function defined. */
22416 static tree
22417 cp_parser_function_definition_after_declarator (cp_parser* parser,
22418 bool inline_p)
22420 tree fn;
22421 bool ctor_initializer_p = false;
22422 bool saved_in_unbraced_linkage_specification_p;
22423 bool saved_in_function_body;
22424 unsigned saved_num_template_parameter_lists;
22425 cp_token *token;
22427 saved_in_function_body = parser->in_function_body;
22428 parser->in_function_body = true;
22429 /* If the next token is `return', then the code may be trying to
22430 make use of the "named return value" extension that G++ used to
22431 support. */
22432 token = cp_lexer_peek_token (parser->lexer);
22433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22435 /* Consume the `return' keyword. */
22436 cp_lexer_consume_token (parser->lexer);
22437 /* Look for the identifier that indicates what value is to be
22438 returned. */
22439 cp_parser_identifier (parser);
22440 /* Issue an error message. */
22441 error_at (token->location,
22442 "named return values are no longer supported");
22443 /* Skip tokens until we reach the start of the function body. */
22444 while (true)
22446 cp_token *token = cp_lexer_peek_token (parser->lexer);
22447 if (token->type == CPP_OPEN_BRACE
22448 || token->type == CPP_EOF
22449 || token->type == CPP_PRAGMA_EOL)
22450 break;
22451 cp_lexer_consume_token (parser->lexer);
22454 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22455 anything declared inside `f'. */
22456 saved_in_unbraced_linkage_specification_p
22457 = parser->in_unbraced_linkage_specification_p;
22458 parser->in_unbraced_linkage_specification_p = false;
22459 /* Inside the function, surrounding template-parameter-lists do not
22460 apply. */
22461 saved_num_template_parameter_lists
22462 = parser->num_template_parameter_lists;
22463 parser->num_template_parameter_lists = 0;
22465 start_lambda_scope (current_function_decl);
22467 /* If the next token is `try', `__transaction_atomic', or
22468 `__transaction_relaxed`, then we are looking at either function-try-block
22469 or function-transaction-block. Note that all of these include the
22470 function-body. */
22471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22472 ctor_initializer_p = cp_parser_function_transaction (parser,
22473 RID_TRANSACTION_ATOMIC);
22474 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22475 RID_TRANSACTION_RELAXED))
22476 ctor_initializer_p = cp_parser_function_transaction (parser,
22477 RID_TRANSACTION_RELAXED);
22478 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22479 ctor_initializer_p = cp_parser_function_try_block (parser);
22480 else
22481 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22482 (parser, /*in_function_try_block=*/false);
22484 finish_lambda_scope ();
22486 /* Finish the function. */
22487 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22488 (inline_p ? 2 : 0));
22489 /* Generate code for it, if necessary. */
22490 expand_or_defer_fn (fn);
22491 /* Restore the saved values. */
22492 parser->in_unbraced_linkage_specification_p
22493 = saved_in_unbraced_linkage_specification_p;
22494 parser->num_template_parameter_lists
22495 = saved_num_template_parameter_lists;
22496 parser->in_function_body = saved_in_function_body;
22498 if (parser->fully_implicit_function_template_p)
22499 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22501 return fn;
22504 /* Parse a template-declaration, assuming that the `export' (and
22505 `extern') keywords, if present, has already been scanned. MEMBER_P
22506 is as for cp_parser_template_declaration. */
22508 static void
22509 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22511 tree decl = NULL_TREE;
22512 vec<deferred_access_check, va_gc> *checks;
22513 tree parameter_list;
22514 bool friend_p = false;
22515 bool need_lang_pop;
22516 cp_token *token;
22518 /* Look for the `template' keyword. */
22519 token = cp_lexer_peek_token (parser->lexer);
22520 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22521 return;
22523 /* And the `<'. */
22524 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22525 return;
22526 if (at_class_scope_p () && current_function_decl)
22528 /* 14.5.2.2 [temp.mem]
22530 A local class shall not have member templates. */
22531 error_at (token->location,
22532 "invalid declaration of member template in local class");
22533 cp_parser_skip_to_end_of_block_or_statement (parser);
22534 return;
22536 /* [temp]
22538 A template ... shall not have C linkage. */
22539 if (current_lang_name == lang_name_c)
22541 error_at (token->location, "template with C linkage");
22542 /* Give it C++ linkage to avoid confusing other parts of the
22543 front end. */
22544 push_lang_context (lang_name_cplusplus);
22545 need_lang_pop = true;
22547 else
22548 need_lang_pop = false;
22550 /* We cannot perform access checks on the template parameter
22551 declarations until we know what is being declared, just as we
22552 cannot check the decl-specifier list. */
22553 push_deferring_access_checks (dk_deferred);
22555 /* If the next token is `>', then we have an invalid
22556 specialization. Rather than complain about an invalid template
22557 parameter, issue an error message here. */
22558 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22560 cp_parser_error (parser, "invalid explicit specialization");
22561 begin_specialization ();
22562 parameter_list = NULL_TREE;
22564 else
22566 /* Parse the template parameters. */
22567 parameter_list = cp_parser_template_parameter_list (parser);
22570 /* Get the deferred access checks from the parameter list. These
22571 will be checked once we know what is being declared, as for a
22572 member template the checks must be performed in the scope of the
22573 class containing the member. */
22574 checks = get_deferred_access_checks ();
22576 /* Look for the `>'. */
22577 cp_parser_skip_to_end_of_template_parameter_list (parser);
22578 /* We just processed one more parameter list. */
22579 ++parser->num_template_parameter_lists;
22580 /* If the next token is `template', there are more template
22581 parameters. */
22582 if (cp_lexer_next_token_is_keyword (parser->lexer,
22583 RID_TEMPLATE))
22584 cp_parser_template_declaration_after_export (parser, member_p);
22585 else if (cxx_dialect >= cxx11
22586 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22587 decl = cp_parser_alias_declaration (parser);
22588 else
22590 /* There are no access checks when parsing a template, as we do not
22591 know if a specialization will be a friend. */
22592 push_deferring_access_checks (dk_no_check);
22593 token = cp_lexer_peek_token (parser->lexer);
22594 decl = cp_parser_single_declaration (parser,
22595 checks,
22596 member_p,
22597 /*explicit_specialization_p=*/false,
22598 &friend_p);
22599 pop_deferring_access_checks ();
22601 /* If this is a member template declaration, let the front
22602 end know. */
22603 if (member_p && !friend_p && decl)
22605 if (TREE_CODE (decl) == TYPE_DECL)
22606 cp_parser_check_access_in_redeclaration (decl, token->location);
22608 decl = finish_member_template_decl (decl);
22610 else if (friend_p && decl
22611 && DECL_DECLARES_TYPE_P (decl))
22612 make_friend_class (current_class_type, TREE_TYPE (decl),
22613 /*complain=*/true);
22615 /* We are done with the current parameter list. */
22616 --parser->num_template_parameter_lists;
22618 pop_deferring_access_checks ();
22620 /* Finish up. */
22621 finish_template_decl (parameter_list);
22623 /* Check the template arguments for a literal operator template. */
22624 if (decl
22625 && DECL_DECLARES_FUNCTION_P (decl)
22626 && UDLIT_OPER_P (DECL_NAME (decl)))
22628 bool ok = true;
22629 if (parameter_list == NULL_TREE)
22630 ok = false;
22631 else
22633 int num_parms = TREE_VEC_LENGTH (parameter_list);
22634 if (num_parms == 1)
22636 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22637 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22638 if (TREE_TYPE (parm) != char_type_node
22639 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22640 ok = false;
22642 else if (num_parms == 2 && cxx_dialect >= cxx1y)
22644 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
22645 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
22646 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
22647 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22648 if (TREE_TYPE (parm) != TREE_TYPE (type)
22649 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22650 ok = false;
22652 else
22653 ok = false;
22655 if (!ok)
22656 error ("literal operator template %qD has invalid parameter list."
22657 " Expected non-type template argument pack <char...>"
22658 " or <typename CharT, CharT...>",
22659 decl);
22661 /* Register member declarations. */
22662 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22663 finish_member_declaration (decl);
22664 /* For the erroneous case of a template with C linkage, we pushed an
22665 implicit C++ linkage scope; exit that scope now. */
22666 if (need_lang_pop)
22667 pop_lang_context ();
22668 /* If DECL is a function template, we must return to parse it later.
22669 (Even though there is no definition, there might be default
22670 arguments that need handling.) */
22671 if (member_p && decl
22672 && DECL_DECLARES_FUNCTION_P (decl))
22673 vec_safe_push (unparsed_funs_with_definitions, decl);
22676 /* Perform the deferred access checks from a template-parameter-list.
22677 CHECKS is a TREE_LIST of access checks, as returned by
22678 get_deferred_access_checks. */
22680 static void
22681 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22683 ++processing_template_parmlist;
22684 perform_access_checks (checks, tf_warning_or_error);
22685 --processing_template_parmlist;
22688 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22689 `function-definition' sequence that follows a template header.
22690 If MEMBER_P is true, this declaration appears in a class scope.
22692 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22693 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22695 static tree
22696 cp_parser_single_declaration (cp_parser* parser,
22697 vec<deferred_access_check, va_gc> *checks,
22698 bool member_p,
22699 bool explicit_specialization_p,
22700 bool* friend_p)
22702 int declares_class_or_enum;
22703 tree decl = NULL_TREE;
22704 cp_decl_specifier_seq decl_specifiers;
22705 bool function_definition_p = false;
22706 cp_token *decl_spec_token_start;
22708 /* This function is only used when processing a template
22709 declaration. */
22710 gcc_assert (innermost_scope_kind () == sk_template_parms
22711 || innermost_scope_kind () == sk_template_spec);
22713 /* Defer access checks until we know what is being declared. */
22714 push_deferring_access_checks (dk_deferred);
22716 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22717 alternative. */
22718 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22719 cp_parser_decl_specifier_seq (parser,
22720 CP_PARSER_FLAGS_OPTIONAL,
22721 &decl_specifiers,
22722 &declares_class_or_enum);
22723 if (friend_p)
22724 *friend_p = cp_parser_friend_p (&decl_specifiers);
22726 /* There are no template typedefs. */
22727 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22729 error_at (decl_spec_token_start->location,
22730 "template declaration of %<typedef%>");
22731 decl = error_mark_node;
22734 /* Gather up the access checks that occurred the
22735 decl-specifier-seq. */
22736 stop_deferring_access_checks ();
22738 /* Check for the declaration of a template class. */
22739 if (declares_class_or_enum)
22741 if (cp_parser_declares_only_class_p (parser))
22743 decl = shadow_tag (&decl_specifiers);
22745 /* In this case:
22747 struct C {
22748 friend template <typename T> struct A<T>::B;
22751 A<T>::B will be represented by a TYPENAME_TYPE, and
22752 therefore not recognized by shadow_tag. */
22753 if (friend_p && *friend_p
22754 && !decl
22755 && decl_specifiers.type
22756 && TYPE_P (decl_specifiers.type))
22757 decl = decl_specifiers.type;
22759 if (decl && decl != error_mark_node)
22760 decl = TYPE_NAME (decl);
22761 else
22762 decl = error_mark_node;
22764 /* Perform access checks for template parameters. */
22765 cp_parser_perform_template_parameter_access_checks (checks);
22769 /* Complain about missing 'typename' or other invalid type names. */
22770 if (!decl_specifiers.any_type_specifiers_p
22771 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22773 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22774 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22775 the rest of this declaration. */
22776 decl = error_mark_node;
22777 goto out;
22780 /* If it's not a template class, try for a template function. If
22781 the next token is a `;', then this declaration does not declare
22782 anything. But, if there were errors in the decl-specifiers, then
22783 the error might well have come from an attempted class-specifier.
22784 In that case, there's no need to warn about a missing declarator. */
22785 if (!decl
22786 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22787 || decl_specifiers.type != error_mark_node))
22789 decl = cp_parser_init_declarator (parser,
22790 &decl_specifiers,
22791 checks,
22792 /*function_definition_allowed_p=*/true,
22793 member_p,
22794 declares_class_or_enum,
22795 &function_definition_p,
22796 NULL);
22798 /* 7.1.1-1 [dcl.stc]
22800 A storage-class-specifier shall not be specified in an explicit
22801 specialization... */
22802 if (decl
22803 && explicit_specialization_p
22804 && decl_specifiers.storage_class != sc_none)
22806 error_at (decl_spec_token_start->location,
22807 "explicit template specialization cannot have a storage class");
22808 decl = error_mark_node;
22811 if (decl && VAR_P (decl))
22812 check_template_variable (decl);
22815 /* Look for a trailing `;' after the declaration. */
22816 if (!function_definition_p
22817 && (decl == error_mark_node
22818 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22819 cp_parser_skip_to_end_of_block_or_statement (parser);
22821 out:
22822 pop_deferring_access_checks ();
22824 /* Clear any current qualification; whatever comes next is the start
22825 of something new. */
22826 parser->scope = NULL_TREE;
22827 parser->qualifying_scope = NULL_TREE;
22828 parser->object_scope = NULL_TREE;
22830 return decl;
22833 /* Parse a cast-expression that is not the operand of a unary "&". */
22835 static tree
22836 cp_parser_simple_cast_expression (cp_parser *parser)
22838 return cp_parser_cast_expression (parser, /*address_p=*/false,
22839 /*cast_p=*/false, /*decltype*/false, NULL);
22842 /* Parse a functional cast to TYPE. Returns an expression
22843 representing the cast. */
22845 static tree
22846 cp_parser_functional_cast (cp_parser* parser, tree type)
22848 vec<tree, va_gc> *vec;
22849 tree expression_list;
22850 tree cast;
22851 bool nonconst_p;
22853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22855 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22856 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22857 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22858 if (TREE_CODE (type) == TYPE_DECL)
22859 type = TREE_TYPE (type);
22860 return finish_compound_literal (type, expression_list,
22861 tf_warning_or_error);
22865 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22866 /*cast_p=*/true,
22867 /*allow_expansion_p=*/true,
22868 /*non_constant_p=*/NULL);
22869 if (vec == NULL)
22870 expression_list = error_mark_node;
22871 else
22873 expression_list = build_tree_list_vec (vec);
22874 release_tree_vector (vec);
22877 cast = build_functional_cast (type, expression_list,
22878 tf_warning_or_error);
22879 /* [expr.const]/1: In an integral constant expression "only type
22880 conversions to integral or enumeration type can be used". */
22881 if (TREE_CODE (type) == TYPE_DECL)
22882 type = TREE_TYPE (type);
22883 if (cast != error_mark_node
22884 && !cast_valid_in_integral_constant_expression_p (type)
22885 && cp_parser_non_integral_constant_expression (parser,
22886 NIC_CONSTRUCTOR))
22887 return error_mark_node;
22888 return cast;
22891 /* Save the tokens that make up the body of a member function defined
22892 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22893 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22894 specifiers applied to the declaration. Returns the FUNCTION_DECL
22895 for the member function. */
22897 static tree
22898 cp_parser_save_member_function_body (cp_parser* parser,
22899 cp_decl_specifier_seq *decl_specifiers,
22900 cp_declarator *declarator,
22901 tree attributes)
22903 cp_token *first;
22904 cp_token *last;
22905 tree fn;
22907 /* Create the FUNCTION_DECL. */
22908 fn = grokmethod (decl_specifiers, declarator, attributes);
22909 cp_finalize_omp_declare_simd (parser, fn);
22910 /* If something went badly wrong, bail out now. */
22911 if (fn == error_mark_node)
22913 /* If there's a function-body, skip it. */
22914 if (cp_parser_token_starts_function_definition_p
22915 (cp_lexer_peek_token (parser->lexer)))
22916 cp_parser_skip_to_end_of_block_or_statement (parser);
22917 return error_mark_node;
22920 /* Remember it, if there default args to post process. */
22921 cp_parser_save_default_args (parser, fn);
22923 /* Save away the tokens that make up the body of the
22924 function. */
22925 first = parser->lexer->next_token;
22926 /* Handle function try blocks. */
22927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22928 cp_lexer_consume_token (parser->lexer);
22929 /* We can have braced-init-list mem-initializers before the fn body. */
22930 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22932 cp_lexer_consume_token (parser->lexer);
22933 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22935 /* cache_group will stop after an un-nested { } pair, too. */
22936 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22937 break;
22939 /* variadic mem-inits have ... after the ')'. */
22940 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22941 cp_lexer_consume_token (parser->lexer);
22944 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22945 /* Handle function try blocks. */
22946 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22947 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22948 last = parser->lexer->next_token;
22950 /* Save away the inline definition; we will process it when the
22951 class is complete. */
22952 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22953 DECL_PENDING_INLINE_P (fn) = 1;
22955 /* We need to know that this was defined in the class, so that
22956 friend templates are handled correctly. */
22957 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22959 /* Add FN to the queue of functions to be parsed later. */
22960 vec_safe_push (unparsed_funs_with_definitions, fn);
22962 return fn;
22965 /* Save the tokens that make up the in-class initializer for a non-static
22966 data member. Returns a DEFAULT_ARG. */
22968 static tree
22969 cp_parser_save_nsdmi (cp_parser* parser)
22971 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22974 /* Parse a template-argument-list, as well as the trailing ">" (but
22975 not the opening "<"). See cp_parser_template_argument_list for the
22976 return value. */
22978 static tree
22979 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22981 tree arguments;
22982 tree saved_scope;
22983 tree saved_qualifying_scope;
22984 tree saved_object_scope;
22985 bool saved_greater_than_is_operator_p;
22986 int saved_unevaluated_operand;
22987 int saved_inhibit_evaluation_warnings;
22989 /* [temp.names]
22991 When parsing a template-id, the first non-nested `>' is taken as
22992 the end of the template-argument-list rather than a greater-than
22993 operator. */
22994 saved_greater_than_is_operator_p
22995 = parser->greater_than_is_operator_p;
22996 parser->greater_than_is_operator_p = false;
22997 /* Parsing the argument list may modify SCOPE, so we save it
22998 here. */
22999 saved_scope = parser->scope;
23000 saved_qualifying_scope = parser->qualifying_scope;
23001 saved_object_scope = parser->object_scope;
23002 /* We need to evaluate the template arguments, even though this
23003 template-id may be nested within a "sizeof". */
23004 saved_unevaluated_operand = cp_unevaluated_operand;
23005 cp_unevaluated_operand = 0;
23006 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23007 c_inhibit_evaluation_warnings = 0;
23008 /* Parse the template-argument-list itself. */
23009 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23010 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23011 arguments = NULL_TREE;
23012 else
23013 arguments = cp_parser_template_argument_list (parser);
23014 /* Look for the `>' that ends the template-argument-list. If we find
23015 a '>>' instead, it's probably just a typo. */
23016 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23018 if (cxx_dialect != cxx98)
23020 /* In C++0x, a `>>' in a template argument list or cast
23021 expression is considered to be two separate `>'
23022 tokens. So, change the current token to a `>', but don't
23023 consume it: it will be consumed later when the outer
23024 template argument list (or cast expression) is parsed.
23025 Note that this replacement of `>' for `>>' is necessary
23026 even if we are parsing tentatively: in the tentative
23027 case, after calling
23028 cp_parser_enclosed_template_argument_list we will always
23029 throw away all of the template arguments and the first
23030 closing `>', either because the template argument list
23031 was erroneous or because we are replacing those tokens
23032 with a CPP_TEMPLATE_ID token. The second `>' (which will
23033 not have been thrown away) is needed either to close an
23034 outer template argument list or to complete a new-style
23035 cast. */
23036 cp_token *token = cp_lexer_peek_token (parser->lexer);
23037 token->type = CPP_GREATER;
23039 else if (!saved_greater_than_is_operator_p)
23041 /* If we're in a nested template argument list, the '>>' has
23042 to be a typo for '> >'. We emit the error message, but we
23043 continue parsing and we push a '>' as next token, so that
23044 the argument list will be parsed correctly. Note that the
23045 global source location is still on the token before the
23046 '>>', so we need to say explicitly where we want it. */
23047 cp_token *token = cp_lexer_peek_token (parser->lexer);
23048 error_at (token->location, "%<>>%> should be %<> >%> "
23049 "within a nested template argument list");
23051 token->type = CPP_GREATER;
23053 else
23055 /* If this is not a nested template argument list, the '>>'
23056 is a typo for '>'. Emit an error message and continue.
23057 Same deal about the token location, but here we can get it
23058 right by consuming the '>>' before issuing the diagnostic. */
23059 cp_token *token = cp_lexer_consume_token (parser->lexer);
23060 error_at (token->location,
23061 "spurious %<>>%>, use %<>%> to terminate "
23062 "a template argument list");
23065 else
23066 cp_parser_skip_to_end_of_template_parameter_list (parser);
23067 /* The `>' token might be a greater-than operator again now. */
23068 parser->greater_than_is_operator_p
23069 = saved_greater_than_is_operator_p;
23070 /* Restore the SAVED_SCOPE. */
23071 parser->scope = saved_scope;
23072 parser->qualifying_scope = saved_qualifying_scope;
23073 parser->object_scope = saved_object_scope;
23074 cp_unevaluated_operand = saved_unevaluated_operand;
23075 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23077 return arguments;
23080 /* MEMBER_FUNCTION is a member function, or a friend. If default
23081 arguments, or the body of the function have not yet been parsed,
23082 parse them now. */
23084 static void
23085 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23087 timevar_push (TV_PARSE_INMETH);
23088 /* If this member is a template, get the underlying
23089 FUNCTION_DECL. */
23090 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23091 member_function = DECL_TEMPLATE_RESULT (member_function);
23093 /* There should not be any class definitions in progress at this
23094 point; the bodies of members are only parsed outside of all class
23095 definitions. */
23096 gcc_assert (parser->num_classes_being_defined == 0);
23097 /* While we're parsing the member functions we might encounter more
23098 classes. We want to handle them right away, but we don't want
23099 them getting mixed up with functions that are currently in the
23100 queue. */
23101 push_unparsed_function_queues (parser);
23103 /* Make sure that any template parameters are in scope. */
23104 maybe_begin_member_template_processing (member_function);
23106 /* If the body of the function has not yet been parsed, parse it
23107 now. */
23108 if (DECL_PENDING_INLINE_P (member_function))
23110 tree function_scope;
23111 cp_token_cache *tokens;
23113 /* The function is no longer pending; we are processing it. */
23114 tokens = DECL_PENDING_INLINE_INFO (member_function);
23115 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23116 DECL_PENDING_INLINE_P (member_function) = 0;
23118 /* If this is a local class, enter the scope of the containing
23119 function. */
23120 function_scope = current_function_decl;
23121 if (function_scope)
23122 push_function_context ();
23124 /* Push the body of the function onto the lexer stack. */
23125 cp_parser_push_lexer_for_tokens (parser, tokens);
23127 /* Let the front end know that we going to be defining this
23128 function. */
23129 start_preparsed_function (member_function, NULL_TREE,
23130 SF_PRE_PARSED | SF_INCLASS_INLINE);
23132 /* Don't do access checking if it is a templated function. */
23133 if (processing_template_decl)
23134 push_deferring_access_checks (dk_no_check);
23136 /* #pragma omp declare reduction needs special parsing. */
23137 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23139 parser->lexer->in_pragma = true;
23140 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23141 finish_function (0);
23142 cp_check_omp_declare_reduction (member_function);
23144 else
23145 /* Now, parse the body of the function. */
23146 cp_parser_function_definition_after_declarator (parser,
23147 /*inline_p=*/true);
23149 if (processing_template_decl)
23150 pop_deferring_access_checks ();
23152 /* Leave the scope of the containing function. */
23153 if (function_scope)
23154 pop_function_context ();
23155 cp_parser_pop_lexer (parser);
23158 /* Remove any template parameters from the symbol table. */
23159 maybe_end_member_template_processing ();
23161 /* Restore the queue. */
23162 pop_unparsed_function_queues (parser);
23163 timevar_pop (TV_PARSE_INMETH);
23166 /* If DECL contains any default args, remember it on the unparsed
23167 functions queue. */
23169 static void
23170 cp_parser_save_default_args (cp_parser* parser, tree decl)
23172 tree probe;
23174 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23175 probe;
23176 probe = TREE_CHAIN (probe))
23177 if (TREE_PURPOSE (probe))
23179 cp_default_arg_entry entry = {current_class_type, decl};
23180 vec_safe_push (unparsed_funs_with_default_args, entry);
23181 break;
23185 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23186 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23187 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23188 from the parameter-type-list. */
23190 static tree
23191 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23192 tree default_arg, tree parmtype)
23194 cp_token_cache *tokens;
23195 tree parsed_arg;
23196 bool dummy;
23198 if (default_arg == error_mark_node)
23199 return error_mark_node;
23201 /* Push the saved tokens for the default argument onto the parser's
23202 lexer stack. */
23203 tokens = DEFARG_TOKENS (default_arg);
23204 cp_parser_push_lexer_for_tokens (parser, tokens);
23206 start_lambda_scope (decl);
23208 /* Parse the default argument. */
23209 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23210 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23211 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23213 finish_lambda_scope ();
23215 if (parsed_arg == error_mark_node)
23216 cp_parser_skip_to_end_of_statement (parser);
23218 if (!processing_template_decl)
23220 /* In a non-template class, check conversions now. In a template,
23221 we'll wait and instantiate these as needed. */
23222 if (TREE_CODE (decl) == PARM_DECL)
23223 parsed_arg = check_default_argument (parmtype, parsed_arg,
23224 tf_warning_or_error);
23225 else
23227 int flags = LOOKUP_IMPLICIT;
23228 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
23229 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
23230 flags = LOOKUP_NORMAL;
23231 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23232 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23233 /* This represents the whole initialization. */
23234 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23238 /* If the token stream has not been completely used up, then
23239 there was extra junk after the end of the default
23240 argument. */
23241 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23243 if (TREE_CODE (decl) == PARM_DECL)
23244 cp_parser_error (parser, "expected %<,%>");
23245 else
23246 cp_parser_error (parser, "expected %<;%>");
23249 /* Revert to the main lexer. */
23250 cp_parser_pop_lexer (parser);
23252 return parsed_arg;
23255 /* FIELD is a non-static data member with an initializer which we saved for
23256 later; parse it now. */
23258 static void
23259 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23261 tree def;
23263 push_unparsed_function_queues (parser);
23264 def = cp_parser_late_parse_one_default_arg (parser, field,
23265 DECL_INITIAL (field),
23266 NULL_TREE);
23267 pop_unparsed_function_queues (parser);
23269 DECL_INITIAL (field) = def;
23272 /* FN is a FUNCTION_DECL which may contains a parameter with an
23273 unparsed DEFAULT_ARG. Parse the default args now. This function
23274 assumes that the current scope is the scope in which the default
23275 argument should be processed. */
23277 static void
23278 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23280 bool saved_local_variables_forbidden_p;
23281 tree parm, parmdecl;
23283 /* While we're parsing the default args, we might (due to the
23284 statement expression extension) encounter more classes. We want
23285 to handle them right away, but we don't want them getting mixed
23286 up with default args that are currently in the queue. */
23287 push_unparsed_function_queues (parser);
23289 /* Local variable names (and the `this' keyword) may not appear
23290 in a default argument. */
23291 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23292 parser->local_variables_forbidden_p = true;
23294 push_defarg_context (fn);
23296 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23297 parmdecl = DECL_ARGUMENTS (fn);
23298 parm && parm != void_list_node;
23299 parm = TREE_CHAIN (parm),
23300 parmdecl = DECL_CHAIN (parmdecl))
23302 tree default_arg = TREE_PURPOSE (parm);
23303 tree parsed_arg;
23304 vec<tree, va_gc> *insts;
23305 tree copy;
23306 unsigned ix;
23308 if (!default_arg)
23309 continue;
23311 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23312 /* This can happen for a friend declaration for a function
23313 already declared with default arguments. */
23314 continue;
23316 parsed_arg
23317 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23318 default_arg,
23319 TREE_VALUE (parm));
23320 if (parsed_arg == error_mark_node)
23322 continue;
23325 TREE_PURPOSE (parm) = parsed_arg;
23327 /* Update any instantiations we've already created. */
23328 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23329 vec_safe_iterate (insts, ix, &copy); ix++)
23330 TREE_PURPOSE (copy) = parsed_arg;
23333 pop_defarg_context ();
23335 /* Make sure no default arg is missing. */
23336 check_default_args (fn);
23338 /* Restore the state of local_variables_forbidden_p. */
23339 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23341 /* Restore the queue. */
23342 pop_unparsed_function_queues (parser);
23345 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23347 sizeof ... ( identifier )
23349 where the 'sizeof' token has already been consumed. */
23351 static tree
23352 cp_parser_sizeof_pack (cp_parser *parser)
23354 /* Consume the `...'. */
23355 cp_lexer_consume_token (parser->lexer);
23356 maybe_warn_variadic_templates ();
23358 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23359 if (paren)
23360 cp_lexer_consume_token (parser->lexer);
23361 else
23362 permerror (cp_lexer_peek_token (parser->lexer)->location,
23363 "%<sizeof...%> argument must be surrounded by parentheses");
23365 cp_token *token = cp_lexer_peek_token (parser->lexer);
23366 tree name = cp_parser_identifier (parser);
23367 if (name == error_mark_node)
23368 return error_mark_node;
23369 /* The name is not qualified. */
23370 parser->scope = NULL_TREE;
23371 parser->qualifying_scope = NULL_TREE;
23372 parser->object_scope = NULL_TREE;
23373 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23374 if (expr == error_mark_node)
23375 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23376 token->location);
23377 if (TREE_CODE (expr) == TYPE_DECL)
23378 expr = TREE_TYPE (expr);
23379 else if (TREE_CODE (expr) == CONST_DECL)
23380 expr = DECL_INITIAL (expr);
23381 expr = make_pack_expansion (expr);
23383 if (paren)
23384 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23386 return expr;
23389 /* Parse the operand of `sizeof' (or a similar operator). Returns
23390 either a TYPE or an expression, depending on the form of the
23391 input. The KEYWORD indicates which kind of expression we have
23392 encountered. */
23394 static tree
23395 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23397 tree expr = NULL_TREE;
23398 const char *saved_message;
23399 char *tmp;
23400 bool saved_integral_constant_expression_p;
23401 bool saved_non_integral_constant_expression_p;
23403 /* If it's a `...', then we are computing the length of a parameter
23404 pack. */
23405 if (keyword == RID_SIZEOF
23406 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23407 return cp_parser_sizeof_pack (parser);
23409 /* Types cannot be defined in a `sizeof' expression. Save away the
23410 old message. */
23411 saved_message = parser->type_definition_forbidden_message;
23412 /* And create the new one. */
23413 tmp = concat ("types may not be defined in %<",
23414 IDENTIFIER_POINTER (ridpointers[keyword]),
23415 "%> expressions", NULL);
23416 parser->type_definition_forbidden_message = tmp;
23418 /* The restrictions on constant-expressions do not apply inside
23419 sizeof expressions. */
23420 saved_integral_constant_expression_p
23421 = parser->integral_constant_expression_p;
23422 saved_non_integral_constant_expression_p
23423 = parser->non_integral_constant_expression_p;
23424 parser->integral_constant_expression_p = false;
23426 /* Do not actually evaluate the expression. */
23427 ++cp_unevaluated_operand;
23428 ++c_inhibit_evaluation_warnings;
23429 /* If it's a `(', then we might be looking at the type-id
23430 construction. */
23431 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23433 tree type = NULL_TREE;
23434 bool compound_literal_p;
23436 /* We can't be sure yet whether we're looking at a type-id or an
23437 expression. */
23438 cp_parser_parse_tentatively (parser);
23439 /* Consume the `('. */
23440 cp_lexer_consume_token (parser->lexer);
23441 /* Note: as a GNU Extension, compound literals are considered
23442 postfix-expressions as they are in C99, so they are valid
23443 arguments to sizeof. See comment in cp_parser_cast_expression
23444 for details. */
23445 cp_lexer_save_tokens (parser->lexer);
23446 /* Skip tokens until the next token is a closing parenthesis.
23447 If we find the closing `)', and the next token is a `{', then
23448 we are looking at a compound-literal. */
23449 compound_literal_p
23450 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23451 /*consume_paren=*/true)
23452 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23453 /* Roll back the tokens we skipped. */
23454 cp_lexer_rollback_tokens (parser->lexer);
23455 /* If we were looking at a compound-literal, simulate an error
23456 so that the call to cp_parser_parse_definitely below will
23457 fail. */
23458 if (compound_literal_p)
23459 cp_parser_simulate_error (parser);
23460 else
23462 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23463 parser->in_type_id_in_expr_p = true;
23464 /* Look for the type-id. */
23465 type = cp_parser_type_id (parser);
23466 /* Look for the closing `)'. */
23467 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23468 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23471 /* If all went well, then we're done. */
23472 if (cp_parser_parse_definitely (parser))
23474 cp_decl_specifier_seq decl_specs;
23476 /* Build a trivial decl-specifier-seq. */
23477 clear_decl_specs (&decl_specs);
23478 decl_specs.type = type;
23480 /* Call grokdeclarator to figure out what type this is. */
23481 expr = grokdeclarator (NULL,
23482 &decl_specs,
23483 TYPENAME,
23484 /*initialized=*/0,
23485 /*attrlist=*/NULL);
23489 /* If the type-id production did not work out, then we must be
23490 looking at the unary-expression production. */
23491 if (!expr)
23492 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23493 /*cast_p=*/false, NULL);
23495 /* Go back to evaluating expressions. */
23496 --cp_unevaluated_operand;
23497 --c_inhibit_evaluation_warnings;
23499 /* Free the message we created. */
23500 free (tmp);
23501 /* And restore the old one. */
23502 parser->type_definition_forbidden_message = saved_message;
23503 parser->integral_constant_expression_p
23504 = saved_integral_constant_expression_p;
23505 parser->non_integral_constant_expression_p
23506 = saved_non_integral_constant_expression_p;
23508 return expr;
23511 /* If the current declaration has no declarator, return true. */
23513 static bool
23514 cp_parser_declares_only_class_p (cp_parser *parser)
23516 /* If the next token is a `;' or a `,' then there is no
23517 declarator. */
23518 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23519 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23522 /* Update the DECL_SPECS to reflect the storage class indicated by
23523 KEYWORD. */
23525 static void
23526 cp_parser_set_storage_class (cp_parser *parser,
23527 cp_decl_specifier_seq *decl_specs,
23528 enum rid keyword,
23529 cp_token *token)
23531 cp_storage_class storage_class;
23533 if (parser->in_unbraced_linkage_specification_p)
23535 error_at (token->location, "invalid use of %qD in linkage specification",
23536 ridpointers[keyword]);
23537 return;
23539 else if (decl_specs->storage_class != sc_none)
23541 decl_specs->conflicting_specifiers_p = true;
23542 return;
23545 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23546 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23547 && decl_specs->gnu_thread_keyword_p)
23549 pedwarn (decl_specs->locations[ds_thread], 0,
23550 "%<__thread%> before %qD", ridpointers[keyword]);
23553 switch (keyword)
23555 case RID_AUTO:
23556 storage_class = sc_auto;
23557 break;
23558 case RID_REGISTER:
23559 storage_class = sc_register;
23560 break;
23561 case RID_STATIC:
23562 storage_class = sc_static;
23563 break;
23564 case RID_EXTERN:
23565 storage_class = sc_extern;
23566 break;
23567 case RID_MUTABLE:
23568 storage_class = sc_mutable;
23569 break;
23570 default:
23571 gcc_unreachable ();
23573 decl_specs->storage_class = storage_class;
23574 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
23576 /* A storage class specifier cannot be applied alongside a typedef
23577 specifier. If there is a typedef specifier present then set
23578 conflicting_specifiers_p which will trigger an error later
23579 on in grokdeclarator. */
23580 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
23581 decl_specs->conflicting_specifiers_p = true;
23584 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23585 is true, the type is a class or enum definition. */
23587 static void
23588 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
23589 tree type_spec,
23590 cp_token *token,
23591 bool type_definition_p)
23593 decl_specs->any_specifiers_p = true;
23595 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23596 (with, for example, in "typedef int wchar_t;") we remember that
23597 this is what happened. In system headers, we ignore these
23598 declarations so that G++ can work with system headers that are not
23599 C++-safe. */
23600 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
23601 && !type_definition_p
23602 && (type_spec == boolean_type_node
23603 || type_spec == char16_type_node
23604 || type_spec == char32_type_node
23605 || type_spec == wchar_type_node)
23606 && (decl_specs->type
23607 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
23608 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
23609 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
23610 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
23612 decl_specs->redefined_builtin_type = type_spec;
23613 set_and_check_decl_spec_loc (decl_specs,
23614 ds_redefined_builtin_type_spec,
23615 token);
23616 if (!decl_specs->type)
23618 decl_specs->type = type_spec;
23619 decl_specs->type_definition_p = false;
23620 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
23623 else if (decl_specs->type)
23624 decl_specs->multiple_types_p = true;
23625 else
23627 decl_specs->type = type_spec;
23628 decl_specs->type_definition_p = type_definition_p;
23629 decl_specs->redefined_builtin_type = NULL_TREE;
23630 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
23634 /* True iff TOKEN is the GNU keyword __thread. */
23636 static bool
23637 token_is__thread (cp_token *token)
23639 gcc_assert (token->keyword == RID_THREAD);
23640 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
23643 /* Set the location for a declarator specifier and check if it is
23644 duplicated.
23646 DECL_SPECS is the sequence of declarator specifiers onto which to
23647 set the location.
23649 DS is the single declarator specifier to set which location is to
23650 be set onto the existing sequence of declarators.
23652 LOCATION is the location for the declarator specifier to
23653 consider. */
23655 static void
23656 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
23657 cp_decl_spec ds, cp_token *token)
23659 gcc_assert (ds < ds_last);
23661 if (decl_specs == NULL)
23662 return;
23664 source_location location = token->location;
23666 if (decl_specs->locations[ds] == 0)
23668 decl_specs->locations[ds] = location;
23669 if (ds == ds_thread)
23670 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
23672 else
23674 if (ds == ds_long)
23676 if (decl_specs->locations[ds_long_long] != 0)
23677 error_at (location,
23678 "%<long long long%> is too long for GCC");
23679 else
23681 decl_specs->locations[ds_long_long] = location;
23682 pedwarn_cxx98 (location,
23683 OPT_Wlong_long,
23684 "ISO C++ 1998 does not support %<long long%>");
23687 else if (ds == ds_thread)
23689 bool gnu = token_is__thread (token);
23690 if (gnu != decl_specs->gnu_thread_keyword_p)
23691 error_at (location,
23692 "both %<__thread%> and %<thread_local%> specified");
23693 else
23694 error_at (location, "duplicate %qD", token->u.value);
23696 else
23698 static const char *const decl_spec_names[] = {
23699 "signed",
23700 "unsigned",
23701 "short",
23702 "long",
23703 "const",
23704 "volatile",
23705 "restrict",
23706 "inline",
23707 "virtual",
23708 "explicit",
23709 "friend",
23710 "typedef",
23711 "using",
23712 "constexpr",
23713 "__complex"
23715 error_at (location,
23716 "duplicate %qs", decl_spec_names[ds]);
23721 /* Return true iff the declarator specifier DS is present in the
23722 sequence of declarator specifiers DECL_SPECS. */
23724 bool
23725 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23726 cp_decl_spec ds)
23728 gcc_assert (ds < ds_last);
23730 if (decl_specs == NULL)
23731 return false;
23733 return decl_specs->locations[ds] != 0;
23736 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23737 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23739 static bool
23740 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23742 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23745 /* Issue an error message indicating that TOKEN_DESC was expected.
23746 If KEYWORD is true, it indicated this function is called by
23747 cp_parser_require_keword and the required token can only be
23748 a indicated keyword. */
23750 static void
23751 cp_parser_required_error (cp_parser *parser,
23752 required_token token_desc,
23753 bool keyword)
23755 switch (token_desc)
23757 case RT_NEW:
23758 cp_parser_error (parser, "expected %<new%>");
23759 return;
23760 case RT_DELETE:
23761 cp_parser_error (parser, "expected %<delete%>");
23762 return;
23763 case RT_RETURN:
23764 cp_parser_error (parser, "expected %<return%>");
23765 return;
23766 case RT_WHILE:
23767 cp_parser_error (parser, "expected %<while%>");
23768 return;
23769 case RT_EXTERN:
23770 cp_parser_error (parser, "expected %<extern%>");
23771 return;
23772 case RT_STATIC_ASSERT:
23773 cp_parser_error (parser, "expected %<static_assert%>");
23774 return;
23775 case RT_DECLTYPE:
23776 cp_parser_error (parser, "expected %<decltype%>");
23777 return;
23778 case RT_OPERATOR:
23779 cp_parser_error (parser, "expected %<operator%>");
23780 return;
23781 case RT_CLASS:
23782 cp_parser_error (parser, "expected %<class%>");
23783 return;
23784 case RT_TEMPLATE:
23785 cp_parser_error (parser, "expected %<template%>");
23786 return;
23787 case RT_NAMESPACE:
23788 cp_parser_error (parser, "expected %<namespace%>");
23789 return;
23790 case RT_USING:
23791 cp_parser_error (parser, "expected %<using%>");
23792 return;
23793 case RT_ASM:
23794 cp_parser_error (parser, "expected %<asm%>");
23795 return;
23796 case RT_TRY:
23797 cp_parser_error (parser, "expected %<try%>");
23798 return;
23799 case RT_CATCH:
23800 cp_parser_error (parser, "expected %<catch%>");
23801 return;
23802 case RT_THROW:
23803 cp_parser_error (parser, "expected %<throw%>");
23804 return;
23805 case RT_LABEL:
23806 cp_parser_error (parser, "expected %<__label__%>");
23807 return;
23808 case RT_AT_TRY:
23809 cp_parser_error (parser, "expected %<@try%>");
23810 return;
23811 case RT_AT_SYNCHRONIZED:
23812 cp_parser_error (parser, "expected %<@synchronized%>");
23813 return;
23814 case RT_AT_THROW:
23815 cp_parser_error (parser, "expected %<@throw%>");
23816 return;
23817 case RT_TRANSACTION_ATOMIC:
23818 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23819 return;
23820 case RT_TRANSACTION_RELAXED:
23821 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23822 return;
23823 default:
23824 break;
23826 if (!keyword)
23828 switch (token_desc)
23830 case RT_SEMICOLON:
23831 cp_parser_error (parser, "expected %<;%>");
23832 return;
23833 case RT_OPEN_PAREN:
23834 cp_parser_error (parser, "expected %<(%>");
23835 return;
23836 case RT_CLOSE_BRACE:
23837 cp_parser_error (parser, "expected %<}%>");
23838 return;
23839 case RT_OPEN_BRACE:
23840 cp_parser_error (parser, "expected %<{%>");
23841 return;
23842 case RT_CLOSE_SQUARE:
23843 cp_parser_error (parser, "expected %<]%>");
23844 return;
23845 case RT_OPEN_SQUARE:
23846 cp_parser_error (parser, "expected %<[%>");
23847 return;
23848 case RT_COMMA:
23849 cp_parser_error (parser, "expected %<,%>");
23850 return;
23851 case RT_SCOPE:
23852 cp_parser_error (parser, "expected %<::%>");
23853 return;
23854 case RT_LESS:
23855 cp_parser_error (parser, "expected %<<%>");
23856 return;
23857 case RT_GREATER:
23858 cp_parser_error (parser, "expected %<>%>");
23859 return;
23860 case RT_EQ:
23861 cp_parser_error (parser, "expected %<=%>");
23862 return;
23863 case RT_ELLIPSIS:
23864 cp_parser_error (parser, "expected %<...%>");
23865 return;
23866 case RT_MULT:
23867 cp_parser_error (parser, "expected %<*%>");
23868 return;
23869 case RT_COMPL:
23870 cp_parser_error (parser, "expected %<~%>");
23871 return;
23872 case RT_COLON:
23873 cp_parser_error (parser, "expected %<:%>");
23874 return;
23875 case RT_COLON_SCOPE:
23876 cp_parser_error (parser, "expected %<:%> or %<::%>");
23877 return;
23878 case RT_CLOSE_PAREN:
23879 cp_parser_error (parser, "expected %<)%>");
23880 return;
23881 case RT_COMMA_CLOSE_PAREN:
23882 cp_parser_error (parser, "expected %<,%> or %<)%>");
23883 return;
23884 case RT_PRAGMA_EOL:
23885 cp_parser_error (parser, "expected end of line");
23886 return;
23887 case RT_NAME:
23888 cp_parser_error (parser, "expected identifier");
23889 return;
23890 case RT_SELECT:
23891 cp_parser_error (parser, "expected selection-statement");
23892 return;
23893 case RT_INTERATION:
23894 cp_parser_error (parser, "expected iteration-statement");
23895 return;
23896 case RT_JUMP:
23897 cp_parser_error (parser, "expected jump-statement");
23898 return;
23899 case RT_CLASS_KEY:
23900 cp_parser_error (parser, "expected class-key");
23901 return;
23902 case RT_CLASS_TYPENAME_TEMPLATE:
23903 cp_parser_error (parser,
23904 "expected %<class%>, %<typename%>, or %<template%>");
23905 return;
23906 default:
23907 gcc_unreachable ();
23910 else
23911 gcc_unreachable ();
23916 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23917 issue an error message indicating that TOKEN_DESC was expected.
23919 Returns the token consumed, if the token had the appropriate type.
23920 Otherwise, returns NULL. */
23922 static cp_token *
23923 cp_parser_require (cp_parser* parser,
23924 enum cpp_ttype type,
23925 required_token token_desc)
23927 if (cp_lexer_next_token_is (parser->lexer, type))
23928 return cp_lexer_consume_token (parser->lexer);
23929 else
23931 /* Output the MESSAGE -- unless we're parsing tentatively. */
23932 if (!cp_parser_simulate_error (parser))
23933 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23934 return NULL;
23938 /* An error message is produced if the next token is not '>'.
23939 All further tokens are skipped until the desired token is
23940 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23942 static void
23943 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23945 /* Current level of '< ... >'. */
23946 unsigned level = 0;
23947 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23948 unsigned nesting_depth = 0;
23950 /* Are we ready, yet? If not, issue error message. */
23951 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23952 return;
23954 /* Skip tokens until the desired token is found. */
23955 while (true)
23957 /* Peek at the next token. */
23958 switch (cp_lexer_peek_token (parser->lexer)->type)
23960 case CPP_LESS:
23961 if (!nesting_depth)
23962 ++level;
23963 break;
23965 case CPP_RSHIFT:
23966 if (cxx_dialect == cxx98)
23967 /* C++0x views the `>>' operator as two `>' tokens, but
23968 C++98 does not. */
23969 break;
23970 else if (!nesting_depth && level-- == 0)
23972 /* We've hit a `>>' where the first `>' closes the
23973 template argument list, and the second `>' is
23974 spurious. Just consume the `>>' and stop; we've
23975 already produced at least one error. */
23976 cp_lexer_consume_token (parser->lexer);
23977 return;
23979 /* Fall through for C++0x, so we handle the second `>' in
23980 the `>>'. */
23982 case CPP_GREATER:
23983 if (!nesting_depth && level-- == 0)
23985 /* We've reached the token we want, consume it and stop. */
23986 cp_lexer_consume_token (parser->lexer);
23987 return;
23989 break;
23991 case CPP_OPEN_PAREN:
23992 case CPP_OPEN_SQUARE:
23993 ++nesting_depth;
23994 break;
23996 case CPP_CLOSE_PAREN:
23997 case CPP_CLOSE_SQUARE:
23998 if (nesting_depth-- == 0)
23999 return;
24000 break;
24002 case CPP_EOF:
24003 case CPP_PRAGMA_EOL:
24004 case CPP_SEMICOLON:
24005 case CPP_OPEN_BRACE:
24006 case CPP_CLOSE_BRACE:
24007 /* The '>' was probably forgotten, don't look further. */
24008 return;
24010 default:
24011 break;
24014 /* Consume this token. */
24015 cp_lexer_consume_token (parser->lexer);
24019 /* If the next token is the indicated keyword, consume it. Otherwise,
24020 issue an error message indicating that TOKEN_DESC was expected.
24022 Returns the token consumed, if the token had the appropriate type.
24023 Otherwise, returns NULL. */
24025 static cp_token *
24026 cp_parser_require_keyword (cp_parser* parser,
24027 enum rid keyword,
24028 required_token token_desc)
24030 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24032 if (token && token->keyword != keyword)
24034 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24035 return NULL;
24038 return token;
24041 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24042 function-definition. */
24044 static bool
24045 cp_parser_token_starts_function_definition_p (cp_token* token)
24047 return (/* An ordinary function-body begins with an `{'. */
24048 token->type == CPP_OPEN_BRACE
24049 /* A ctor-initializer begins with a `:'. */
24050 || token->type == CPP_COLON
24051 /* A function-try-block begins with `try'. */
24052 || token->keyword == RID_TRY
24053 /* A function-transaction-block begins with `__transaction_atomic'
24054 or `__transaction_relaxed'. */
24055 || token->keyword == RID_TRANSACTION_ATOMIC
24056 || token->keyword == RID_TRANSACTION_RELAXED
24057 /* The named return value extension begins with `return'. */
24058 || token->keyword == RID_RETURN);
24061 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24062 definition. */
24064 static bool
24065 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24067 cp_token *token;
24069 token = cp_lexer_peek_token (parser->lexer);
24070 return (token->type == CPP_OPEN_BRACE
24071 || (token->type == CPP_COLON
24072 && !parser->colon_doesnt_start_class_def_p));
24075 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24076 C++0x) ending a template-argument. */
24078 static bool
24079 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24081 cp_token *token;
24083 token = cp_lexer_peek_token (parser->lexer);
24084 return (token->type == CPP_COMMA
24085 || token->type == CPP_GREATER
24086 || token->type == CPP_ELLIPSIS
24087 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24090 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24091 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24093 static bool
24094 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24095 size_t n)
24097 cp_token *token;
24099 token = cp_lexer_peek_nth_token (parser->lexer, n);
24100 if (token->type == CPP_LESS)
24101 return true;
24102 /* Check for the sequence `<::' in the original code. It would be lexed as
24103 `[:', where `[' is a digraph, and there is no whitespace before
24104 `:'. */
24105 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24107 cp_token *token2;
24108 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24109 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24110 return true;
24112 return false;
24115 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24116 or none_type otherwise. */
24118 static enum tag_types
24119 cp_parser_token_is_class_key (cp_token* token)
24121 switch (token->keyword)
24123 case RID_CLASS:
24124 return class_type;
24125 case RID_STRUCT:
24126 return record_type;
24127 case RID_UNION:
24128 return union_type;
24130 default:
24131 return none_type;
24135 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24137 static void
24138 cp_parser_check_class_key (enum tag_types class_key, tree type)
24140 if (type == error_mark_node)
24141 return;
24142 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24144 if (permerror (input_location, "%qs tag used in naming %q#T",
24145 class_key == union_type ? "union"
24146 : class_key == record_type ? "struct" : "class",
24147 type))
24148 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24149 "%q#T was previously declared here", type);
24153 /* Issue an error message if DECL is redeclared with different
24154 access than its original declaration [class.access.spec/3].
24155 This applies to nested classes and nested class templates.
24156 [class.mem/1]. */
24158 static void
24159 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24161 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24162 return;
24164 if ((TREE_PRIVATE (decl)
24165 != (current_access_specifier == access_private_node))
24166 || (TREE_PROTECTED (decl)
24167 != (current_access_specifier == access_protected_node)))
24168 error_at (location, "%qD redeclared with different access", decl);
24171 /* Look for the `template' keyword, as a syntactic disambiguator.
24172 Return TRUE iff it is present, in which case it will be
24173 consumed. */
24175 static bool
24176 cp_parser_optional_template_keyword (cp_parser *parser)
24178 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24180 /* In C++98 the `template' keyword can only be used within templates;
24181 outside templates the parser can always figure out what is a
24182 template and what is not. In C++11, per the resolution of DR 468,
24183 `template' is allowed in cases where it is not strictly necessary. */
24184 if (!processing_template_decl
24185 && pedantic && cxx_dialect == cxx98)
24187 cp_token *token = cp_lexer_peek_token (parser->lexer);
24188 pedwarn (token->location, OPT_Wpedantic,
24189 "in C++98 %<template%> (as a disambiguator) is only "
24190 "allowed within templates");
24191 /* If this part of the token stream is rescanned, the same
24192 error message would be generated. So, we purge the token
24193 from the stream. */
24194 cp_lexer_purge_token (parser->lexer);
24195 return false;
24197 else
24199 /* Consume the `template' keyword. */
24200 cp_lexer_consume_token (parser->lexer);
24201 return true;
24204 return false;
24207 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24208 set PARSER->SCOPE, and perform other related actions. */
24210 static void
24211 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24213 int i;
24214 struct tree_check *check_value;
24215 deferred_access_check *chk;
24216 vec<deferred_access_check, va_gc> *checks;
24218 /* Get the stored value. */
24219 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24220 /* Perform any access checks that were deferred. */
24221 checks = check_value->checks;
24222 if (checks)
24224 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24225 perform_or_defer_access_check (chk->binfo,
24226 chk->decl,
24227 chk->diag_decl, tf_warning_or_error);
24229 /* Set the scope from the stored value. */
24230 parser->scope = check_value->value;
24231 parser->qualifying_scope = check_value->qualifying_scope;
24232 parser->object_scope = NULL_TREE;
24235 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24236 encounter the end of a block before what we were looking for. */
24238 static bool
24239 cp_parser_cache_group (cp_parser *parser,
24240 enum cpp_ttype end,
24241 unsigned depth)
24243 while (true)
24245 cp_token *token = cp_lexer_peek_token (parser->lexer);
24247 /* Abort a parenthesized expression if we encounter a semicolon. */
24248 if ((end == CPP_CLOSE_PAREN || depth == 0)
24249 && token->type == CPP_SEMICOLON)
24250 return true;
24251 /* If we've reached the end of the file, stop. */
24252 if (token->type == CPP_EOF
24253 || (end != CPP_PRAGMA_EOL
24254 && token->type == CPP_PRAGMA_EOL))
24255 return true;
24256 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24257 /* We've hit the end of an enclosing block, so there's been some
24258 kind of syntax error. */
24259 return true;
24261 /* Consume the token. */
24262 cp_lexer_consume_token (parser->lexer);
24263 /* See if it starts a new group. */
24264 if (token->type == CPP_OPEN_BRACE)
24266 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24267 /* In theory this should probably check end == '}', but
24268 cp_parser_save_member_function_body needs it to exit
24269 after either '}' or ')' when called with ')'. */
24270 if (depth == 0)
24271 return false;
24273 else if (token->type == CPP_OPEN_PAREN)
24275 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24276 if (depth == 0 && end == CPP_CLOSE_PAREN)
24277 return false;
24279 else if (token->type == CPP_PRAGMA)
24280 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24281 else if (token->type == end)
24282 return false;
24286 /* Like above, for caching a default argument or NSDMI. Both of these are
24287 terminated by a non-nested comma, but it can be unclear whether or not a
24288 comma is nested in a template argument list unless we do more parsing.
24289 In order to handle this ambiguity, when we encounter a ',' after a '<'
24290 we try to parse what follows as a parameter-declaration-list (in the
24291 case of a default argument) or a member-declarator (in the case of an
24292 NSDMI). If that succeeds, then we stop caching. */
24294 static tree
24295 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24297 unsigned depth = 0;
24298 int maybe_template_id = 0;
24299 cp_token *first_token;
24300 cp_token *token;
24301 tree default_argument;
24303 /* Add tokens until we have processed the entire default
24304 argument. We add the range [first_token, token). */
24305 first_token = cp_lexer_peek_token (parser->lexer);
24306 if (first_token->type == CPP_OPEN_BRACE)
24308 /* For list-initialization, this is straightforward. */
24309 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24310 token = cp_lexer_peek_token (parser->lexer);
24312 else while (true)
24314 bool done = false;
24316 /* Peek at the next token. */
24317 token = cp_lexer_peek_token (parser->lexer);
24318 /* What we do depends on what token we have. */
24319 switch (token->type)
24321 /* In valid code, a default argument must be
24322 immediately followed by a `,' `)', or `...'. */
24323 case CPP_COMMA:
24324 if (depth == 0 && maybe_template_id)
24326 /* If we've seen a '<', we might be in a
24327 template-argument-list. Until Core issue 325 is
24328 resolved, we don't know how this situation ought
24329 to be handled, so try to DTRT. We check whether
24330 what comes after the comma is a valid parameter
24331 declaration list. If it is, then the comma ends
24332 the default argument; otherwise the default
24333 argument continues. */
24334 bool error = false;
24336 /* Set ITALP so cp_parser_parameter_declaration_list
24337 doesn't decide to commit to this parse. */
24338 bool saved_italp = parser->in_template_argument_list_p;
24339 parser->in_template_argument_list_p = true;
24341 cp_parser_parse_tentatively (parser);
24342 cp_lexer_consume_token (parser->lexer);
24344 if (nsdmi)
24346 int ctor_dtor_or_conv_p;
24347 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24348 &ctor_dtor_or_conv_p,
24349 /*parenthesized_p=*/NULL,
24350 /*member_p=*/true);
24352 else
24354 begin_scope (sk_function_parms, NULL_TREE);
24355 cp_parser_parameter_declaration_list (parser, &error);
24356 pop_bindings_and_leave_scope ();
24358 if (!cp_parser_error_occurred (parser) && !error)
24359 done = true;
24360 cp_parser_abort_tentative_parse (parser);
24362 parser->in_template_argument_list_p = saved_italp;
24363 break;
24365 case CPP_CLOSE_PAREN:
24366 case CPP_ELLIPSIS:
24367 /* If we run into a non-nested `;', `}', or `]',
24368 then the code is invalid -- but the default
24369 argument is certainly over. */
24370 case CPP_SEMICOLON:
24371 case CPP_CLOSE_BRACE:
24372 case CPP_CLOSE_SQUARE:
24373 if (depth == 0
24374 /* Handle correctly int n = sizeof ... ( p ); */
24375 && !(nsdmi && token->type == CPP_ELLIPSIS))
24376 done = true;
24377 /* Update DEPTH, if necessary. */
24378 else if (token->type == CPP_CLOSE_PAREN
24379 || token->type == CPP_CLOSE_BRACE
24380 || token->type == CPP_CLOSE_SQUARE)
24381 --depth;
24382 break;
24384 case CPP_OPEN_PAREN:
24385 case CPP_OPEN_SQUARE:
24386 case CPP_OPEN_BRACE:
24387 ++depth;
24388 break;
24390 case CPP_LESS:
24391 if (depth == 0)
24392 /* This might be the comparison operator, or it might
24393 start a template argument list. */
24394 ++maybe_template_id;
24395 break;
24397 case CPP_RSHIFT:
24398 if (cxx_dialect == cxx98)
24399 break;
24400 /* Fall through for C++0x, which treats the `>>'
24401 operator like two `>' tokens in certain
24402 cases. */
24404 case CPP_GREATER:
24405 if (depth == 0)
24407 /* This might be an operator, or it might close a
24408 template argument list. But if a previous '<'
24409 started a template argument list, this will have
24410 closed it, so we can't be in one anymore. */
24411 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24412 if (maybe_template_id < 0)
24413 maybe_template_id = 0;
24415 break;
24417 /* If we run out of tokens, issue an error message. */
24418 case CPP_EOF:
24419 case CPP_PRAGMA_EOL:
24420 error_at (token->location, "file ends in default argument");
24421 done = true;
24422 break;
24424 case CPP_NAME:
24425 case CPP_SCOPE:
24426 /* In these cases, we should look for template-ids.
24427 For example, if the default argument is
24428 `X<int, double>()', we need to do name lookup to
24429 figure out whether or not `X' is a template; if
24430 so, the `,' does not end the default argument.
24432 That is not yet done. */
24433 break;
24435 default:
24436 break;
24439 /* If we've reached the end, stop. */
24440 if (done)
24441 break;
24443 /* Add the token to the token block. */
24444 token = cp_lexer_consume_token (parser->lexer);
24447 /* Create a DEFAULT_ARG to represent the unparsed default
24448 argument. */
24449 default_argument = make_node (DEFAULT_ARG);
24450 DEFARG_TOKENS (default_argument)
24451 = cp_token_cache_new (first_token, token);
24452 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24454 return default_argument;
24457 /* Begin parsing tentatively. We always save tokens while parsing
24458 tentatively so that if the tentative parsing fails we can restore the
24459 tokens. */
24461 static void
24462 cp_parser_parse_tentatively (cp_parser* parser)
24464 /* Enter a new parsing context. */
24465 parser->context = cp_parser_context_new (parser->context);
24466 /* Begin saving tokens. */
24467 cp_lexer_save_tokens (parser->lexer);
24468 /* In order to avoid repetitive access control error messages,
24469 access checks are queued up until we are no longer parsing
24470 tentatively. */
24471 push_deferring_access_checks (dk_deferred);
24474 /* Commit to the currently active tentative parse. */
24476 static void
24477 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24479 cp_parser_context *context;
24480 cp_lexer *lexer;
24482 /* Mark all of the levels as committed. */
24483 lexer = parser->lexer;
24484 for (context = parser->context; context->next; context = context->next)
24486 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24487 break;
24488 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24489 while (!cp_lexer_saving_tokens (lexer))
24490 lexer = lexer->next;
24491 cp_lexer_commit_tokens (lexer);
24495 /* Commit to the topmost currently active tentative parse.
24497 Note that this function shouldn't be called when there are
24498 irreversible side-effects while in a tentative state. For
24499 example, we shouldn't create a permanent entry in the symbol
24500 table, or issue an error message that might not apply if the
24501 tentative parse is aborted. */
24503 static void
24504 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24506 cp_parser_context *context = parser->context;
24507 cp_lexer *lexer = parser->lexer;
24509 if (context)
24511 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24512 return;
24513 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24515 while (!cp_lexer_saving_tokens (lexer))
24516 lexer = lexer->next;
24517 cp_lexer_commit_tokens (lexer);
24521 /* Abort the currently active tentative parse. All consumed tokens
24522 will be rolled back, and no diagnostics will be issued. */
24524 static void
24525 cp_parser_abort_tentative_parse (cp_parser* parser)
24527 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24528 || errorcount > 0);
24529 cp_parser_simulate_error (parser);
24530 /* Now, pretend that we want to see if the construct was
24531 successfully parsed. */
24532 cp_parser_parse_definitely (parser);
24535 /* Stop parsing tentatively. If a parse error has occurred, restore the
24536 token stream. Otherwise, commit to the tokens we have consumed.
24537 Returns true if no error occurred; false otherwise. */
24539 static bool
24540 cp_parser_parse_definitely (cp_parser* parser)
24542 bool error_occurred;
24543 cp_parser_context *context;
24545 /* Remember whether or not an error occurred, since we are about to
24546 destroy that information. */
24547 error_occurred = cp_parser_error_occurred (parser);
24548 /* Remove the topmost context from the stack. */
24549 context = parser->context;
24550 parser->context = context->next;
24551 /* If no parse errors occurred, commit to the tentative parse. */
24552 if (!error_occurred)
24554 /* Commit to the tokens read tentatively, unless that was
24555 already done. */
24556 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
24557 cp_lexer_commit_tokens (parser->lexer);
24559 pop_to_parent_deferring_access_checks ();
24561 /* Otherwise, if errors occurred, roll back our state so that things
24562 are just as they were before we began the tentative parse. */
24563 else
24565 cp_lexer_rollback_tokens (parser->lexer);
24566 pop_deferring_access_checks ();
24568 /* Add the context to the front of the free list. */
24569 context->next = cp_parser_context_free_list;
24570 cp_parser_context_free_list = context;
24572 return !error_occurred;
24575 /* Returns true if we are parsing tentatively and are not committed to
24576 this tentative parse. */
24578 static bool
24579 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
24581 return (cp_parser_parsing_tentatively (parser)
24582 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
24585 /* Returns nonzero iff an error has occurred during the most recent
24586 tentative parse. */
24588 static bool
24589 cp_parser_error_occurred (cp_parser* parser)
24591 return (cp_parser_parsing_tentatively (parser)
24592 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
24595 /* Returns nonzero if GNU extensions are allowed. */
24597 static bool
24598 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
24600 return parser->allow_gnu_extensions_p;
24603 /* Objective-C++ Productions */
24606 /* Parse an Objective-C expression, which feeds into a primary-expression
24607 above.
24609 objc-expression:
24610 objc-message-expression
24611 objc-string-literal
24612 objc-encode-expression
24613 objc-protocol-expression
24614 objc-selector-expression
24616 Returns a tree representation of the expression. */
24618 static tree
24619 cp_parser_objc_expression (cp_parser* parser)
24621 /* Try to figure out what kind of declaration is present. */
24622 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24624 switch (kwd->type)
24626 case CPP_OPEN_SQUARE:
24627 return cp_parser_objc_message_expression (parser);
24629 case CPP_OBJC_STRING:
24630 kwd = cp_lexer_consume_token (parser->lexer);
24631 return objc_build_string_object (kwd->u.value);
24633 case CPP_KEYWORD:
24634 switch (kwd->keyword)
24636 case RID_AT_ENCODE:
24637 return cp_parser_objc_encode_expression (parser);
24639 case RID_AT_PROTOCOL:
24640 return cp_parser_objc_protocol_expression (parser);
24642 case RID_AT_SELECTOR:
24643 return cp_parser_objc_selector_expression (parser);
24645 default:
24646 break;
24648 default:
24649 error_at (kwd->location,
24650 "misplaced %<@%D%> Objective-C++ construct",
24651 kwd->u.value);
24652 cp_parser_skip_to_end_of_block_or_statement (parser);
24655 return error_mark_node;
24658 /* Parse an Objective-C message expression.
24660 objc-message-expression:
24661 [ objc-message-receiver objc-message-args ]
24663 Returns a representation of an Objective-C message. */
24665 static tree
24666 cp_parser_objc_message_expression (cp_parser* parser)
24668 tree receiver, messageargs;
24670 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
24671 receiver = cp_parser_objc_message_receiver (parser);
24672 messageargs = cp_parser_objc_message_args (parser);
24673 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24675 return objc_build_message_expr (receiver, messageargs);
24678 /* Parse an objc-message-receiver.
24680 objc-message-receiver:
24681 expression
24682 simple-type-specifier
24684 Returns a representation of the type or expression. */
24686 static tree
24687 cp_parser_objc_message_receiver (cp_parser* parser)
24689 tree rcv;
24691 /* An Objective-C message receiver may be either (1) a type
24692 or (2) an expression. */
24693 cp_parser_parse_tentatively (parser);
24694 rcv = cp_parser_expression (parser, false, NULL);
24696 if (cp_parser_parse_definitely (parser))
24697 return rcv;
24699 rcv = cp_parser_simple_type_specifier (parser,
24700 /*decl_specs=*/NULL,
24701 CP_PARSER_FLAGS_NONE);
24703 return objc_get_class_reference (rcv);
24706 /* Parse the arguments and selectors comprising an Objective-C message.
24708 objc-message-args:
24709 objc-selector
24710 objc-selector-args
24711 objc-selector-args , objc-comma-args
24713 objc-selector-args:
24714 objc-selector [opt] : assignment-expression
24715 objc-selector-args objc-selector [opt] : assignment-expression
24717 objc-comma-args:
24718 assignment-expression
24719 objc-comma-args , assignment-expression
24721 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24722 selector arguments and TREE_VALUE containing a list of comma
24723 arguments. */
24725 static tree
24726 cp_parser_objc_message_args (cp_parser* parser)
24728 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
24729 bool maybe_unary_selector_p = true;
24730 cp_token *token = cp_lexer_peek_token (parser->lexer);
24732 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24734 tree selector = NULL_TREE, arg;
24736 if (token->type != CPP_COLON)
24737 selector = cp_parser_objc_selector (parser);
24739 /* Detect if we have a unary selector. */
24740 if (maybe_unary_selector_p
24741 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24742 return build_tree_list (selector, NULL_TREE);
24744 maybe_unary_selector_p = false;
24745 cp_parser_require (parser, CPP_COLON, RT_COLON);
24746 arg = cp_parser_assignment_expression (parser, false, NULL);
24748 sel_args
24749 = chainon (sel_args,
24750 build_tree_list (selector, arg));
24752 token = cp_lexer_peek_token (parser->lexer);
24755 /* Handle non-selector arguments, if any. */
24756 while (token->type == CPP_COMMA)
24758 tree arg;
24760 cp_lexer_consume_token (parser->lexer);
24761 arg = cp_parser_assignment_expression (parser, false, NULL);
24763 addl_args
24764 = chainon (addl_args,
24765 build_tree_list (NULL_TREE, arg));
24767 token = cp_lexer_peek_token (parser->lexer);
24770 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24772 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24773 return build_tree_list (error_mark_node, error_mark_node);
24776 return build_tree_list (sel_args, addl_args);
24779 /* Parse an Objective-C encode expression.
24781 objc-encode-expression:
24782 @encode objc-typename
24784 Returns an encoded representation of the type argument. */
24786 static tree
24787 cp_parser_objc_encode_expression (cp_parser* parser)
24789 tree type;
24790 cp_token *token;
24792 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24793 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24794 token = cp_lexer_peek_token (parser->lexer);
24795 type = complete_type (cp_parser_type_id (parser));
24796 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24798 if (!type)
24800 error_at (token->location,
24801 "%<@encode%> must specify a type as an argument");
24802 return error_mark_node;
24805 /* This happens if we find @encode(T) (where T is a template
24806 typename or something dependent on a template typename) when
24807 parsing a template. In that case, we can't compile it
24808 immediately, but we rather create an AT_ENCODE_EXPR which will
24809 need to be instantiated when the template is used.
24811 if (dependent_type_p (type))
24813 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24814 TREE_READONLY (value) = 1;
24815 return value;
24818 return objc_build_encode_expr (type);
24821 /* Parse an Objective-C @defs expression. */
24823 static tree
24824 cp_parser_objc_defs_expression (cp_parser *parser)
24826 tree name;
24828 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24829 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24830 name = cp_parser_identifier (parser);
24831 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24833 return objc_get_class_ivars (name);
24836 /* Parse an Objective-C protocol expression.
24838 objc-protocol-expression:
24839 @protocol ( identifier )
24841 Returns a representation of the protocol expression. */
24843 static tree
24844 cp_parser_objc_protocol_expression (cp_parser* parser)
24846 tree proto;
24848 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24849 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24850 proto = cp_parser_identifier (parser);
24851 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24853 return objc_build_protocol_expr (proto);
24856 /* Parse an Objective-C selector expression.
24858 objc-selector-expression:
24859 @selector ( objc-method-signature )
24861 objc-method-signature:
24862 objc-selector
24863 objc-selector-seq
24865 objc-selector-seq:
24866 objc-selector :
24867 objc-selector-seq objc-selector :
24869 Returns a representation of the method selector. */
24871 static tree
24872 cp_parser_objc_selector_expression (cp_parser* parser)
24874 tree sel_seq = NULL_TREE;
24875 bool maybe_unary_selector_p = true;
24876 cp_token *token;
24877 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24879 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
24880 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24881 token = cp_lexer_peek_token (parser->lexer);
24883 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24884 || token->type == CPP_SCOPE)
24886 tree selector = NULL_TREE;
24888 if (token->type != CPP_COLON
24889 || token->type == CPP_SCOPE)
24890 selector = cp_parser_objc_selector (parser);
24892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24893 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24895 /* Detect if we have a unary selector. */
24896 if (maybe_unary_selector_p)
24898 sel_seq = selector;
24899 goto finish_selector;
24901 else
24903 cp_parser_error (parser, "expected %<:%>");
24906 maybe_unary_selector_p = false;
24907 token = cp_lexer_consume_token (parser->lexer);
24909 if (token->type == CPP_SCOPE)
24911 sel_seq
24912 = chainon (sel_seq,
24913 build_tree_list (selector, NULL_TREE));
24914 sel_seq
24915 = chainon (sel_seq,
24916 build_tree_list (NULL_TREE, NULL_TREE));
24918 else
24919 sel_seq
24920 = chainon (sel_seq,
24921 build_tree_list (selector, NULL_TREE));
24923 token = cp_lexer_peek_token (parser->lexer);
24926 finish_selector:
24927 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24929 return objc_build_selector_expr (loc, sel_seq);
24932 /* Parse a list of identifiers.
24934 objc-identifier-list:
24935 identifier
24936 objc-identifier-list , identifier
24938 Returns a TREE_LIST of identifier nodes. */
24940 static tree
24941 cp_parser_objc_identifier_list (cp_parser* parser)
24943 tree identifier;
24944 tree list;
24945 cp_token *sep;
24947 identifier = cp_parser_identifier (parser);
24948 if (identifier == error_mark_node)
24949 return error_mark_node;
24951 list = build_tree_list (NULL_TREE, identifier);
24952 sep = cp_lexer_peek_token (parser->lexer);
24954 while (sep->type == CPP_COMMA)
24956 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24957 identifier = cp_parser_identifier (parser);
24958 if (identifier == error_mark_node)
24959 return list;
24961 list = chainon (list, build_tree_list (NULL_TREE,
24962 identifier));
24963 sep = cp_lexer_peek_token (parser->lexer);
24966 return list;
24969 /* Parse an Objective-C alias declaration.
24971 objc-alias-declaration:
24972 @compatibility_alias identifier identifier ;
24974 This function registers the alias mapping with the Objective-C front end.
24975 It returns nothing. */
24977 static void
24978 cp_parser_objc_alias_declaration (cp_parser* parser)
24980 tree alias, orig;
24982 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24983 alias = cp_parser_identifier (parser);
24984 orig = cp_parser_identifier (parser);
24985 objc_declare_alias (alias, orig);
24986 cp_parser_consume_semicolon_at_end_of_statement (parser);
24989 /* Parse an Objective-C class forward-declaration.
24991 objc-class-declaration:
24992 @class objc-identifier-list ;
24994 The function registers the forward declarations with the Objective-C
24995 front end. It returns nothing. */
24997 static void
24998 cp_parser_objc_class_declaration (cp_parser* parser)
25000 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25001 while (true)
25003 tree id;
25005 id = cp_parser_identifier (parser);
25006 if (id == error_mark_node)
25007 break;
25009 objc_declare_class (id);
25011 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25012 cp_lexer_consume_token (parser->lexer);
25013 else
25014 break;
25016 cp_parser_consume_semicolon_at_end_of_statement (parser);
25019 /* Parse a list of Objective-C protocol references.
25021 objc-protocol-refs-opt:
25022 objc-protocol-refs [opt]
25024 objc-protocol-refs:
25025 < objc-identifier-list >
25027 Returns a TREE_LIST of identifiers, if any. */
25029 static tree
25030 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25032 tree protorefs = NULL_TREE;
25034 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25036 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25037 protorefs = cp_parser_objc_identifier_list (parser);
25038 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25041 return protorefs;
25044 /* Parse a Objective-C visibility specification. */
25046 static void
25047 cp_parser_objc_visibility_spec (cp_parser* parser)
25049 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25051 switch (vis->keyword)
25053 case RID_AT_PRIVATE:
25054 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25055 break;
25056 case RID_AT_PROTECTED:
25057 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25058 break;
25059 case RID_AT_PUBLIC:
25060 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25061 break;
25062 case RID_AT_PACKAGE:
25063 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25064 break;
25065 default:
25066 return;
25069 /* Eat '@private'/'@protected'/'@public'. */
25070 cp_lexer_consume_token (parser->lexer);
25073 /* Parse an Objective-C method type. Return 'true' if it is a class
25074 (+) method, and 'false' if it is an instance (-) method. */
25076 static inline bool
25077 cp_parser_objc_method_type (cp_parser* parser)
25079 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25080 return true;
25081 else
25082 return false;
25085 /* Parse an Objective-C protocol qualifier. */
25087 static tree
25088 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25090 tree quals = NULL_TREE, node;
25091 cp_token *token = cp_lexer_peek_token (parser->lexer);
25093 node = token->u.value;
25095 while (node && identifier_p (node)
25096 && (node == ridpointers [(int) RID_IN]
25097 || node == ridpointers [(int) RID_OUT]
25098 || node == ridpointers [(int) RID_INOUT]
25099 || node == ridpointers [(int) RID_BYCOPY]
25100 || node == ridpointers [(int) RID_BYREF]
25101 || node == ridpointers [(int) RID_ONEWAY]))
25103 quals = tree_cons (NULL_TREE, node, quals);
25104 cp_lexer_consume_token (parser->lexer);
25105 token = cp_lexer_peek_token (parser->lexer);
25106 node = token->u.value;
25109 return quals;
25112 /* Parse an Objective-C typename. */
25114 static tree
25115 cp_parser_objc_typename (cp_parser* parser)
25117 tree type_name = NULL_TREE;
25119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25121 tree proto_quals, cp_type = NULL_TREE;
25123 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25124 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25126 /* An ObjC type name may consist of just protocol qualifiers, in which
25127 case the type shall default to 'id'. */
25128 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25130 cp_type = cp_parser_type_id (parser);
25132 /* If the type could not be parsed, an error has already
25133 been produced. For error recovery, behave as if it had
25134 not been specified, which will use the default type
25135 'id'. */
25136 if (cp_type == error_mark_node)
25138 cp_type = NULL_TREE;
25139 /* We need to skip to the closing parenthesis as
25140 cp_parser_type_id() does not seem to do it for
25141 us. */
25142 cp_parser_skip_to_closing_parenthesis (parser,
25143 /*recovering=*/true,
25144 /*or_comma=*/false,
25145 /*consume_paren=*/false);
25149 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25150 type_name = build_tree_list (proto_quals, cp_type);
25153 return type_name;
25156 /* Check to see if TYPE refers to an Objective-C selector name. */
25158 static bool
25159 cp_parser_objc_selector_p (enum cpp_ttype type)
25161 return (type == CPP_NAME || type == CPP_KEYWORD
25162 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25163 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25164 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25165 || type == CPP_XOR || type == CPP_XOR_EQ);
25168 /* Parse an Objective-C selector. */
25170 static tree
25171 cp_parser_objc_selector (cp_parser* parser)
25173 cp_token *token = cp_lexer_consume_token (parser->lexer);
25175 if (!cp_parser_objc_selector_p (token->type))
25177 error_at (token->location, "invalid Objective-C++ selector name");
25178 return error_mark_node;
25181 /* C++ operator names are allowed to appear in ObjC selectors. */
25182 switch (token->type)
25184 case CPP_AND_AND: return get_identifier ("and");
25185 case CPP_AND_EQ: return get_identifier ("and_eq");
25186 case CPP_AND: return get_identifier ("bitand");
25187 case CPP_OR: return get_identifier ("bitor");
25188 case CPP_COMPL: return get_identifier ("compl");
25189 case CPP_NOT: return get_identifier ("not");
25190 case CPP_NOT_EQ: return get_identifier ("not_eq");
25191 case CPP_OR_OR: return get_identifier ("or");
25192 case CPP_OR_EQ: return get_identifier ("or_eq");
25193 case CPP_XOR: return get_identifier ("xor");
25194 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25195 default: return token->u.value;
25199 /* Parse an Objective-C params list. */
25201 static tree
25202 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25204 tree params = NULL_TREE;
25205 bool maybe_unary_selector_p = true;
25206 cp_token *token = cp_lexer_peek_token (parser->lexer);
25208 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25210 tree selector = NULL_TREE, type_name, identifier;
25211 tree parm_attr = NULL_TREE;
25213 if (token->keyword == RID_ATTRIBUTE)
25214 break;
25216 if (token->type != CPP_COLON)
25217 selector = cp_parser_objc_selector (parser);
25219 /* Detect if we have a unary selector. */
25220 if (maybe_unary_selector_p
25221 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25223 params = selector; /* Might be followed by attributes. */
25224 break;
25227 maybe_unary_selector_p = false;
25228 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25230 /* Something went quite wrong. There should be a colon
25231 here, but there is not. Stop parsing parameters. */
25232 break;
25234 type_name = cp_parser_objc_typename (parser);
25235 /* New ObjC allows attributes on parameters too. */
25236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25237 parm_attr = cp_parser_attributes_opt (parser);
25238 identifier = cp_parser_identifier (parser);
25240 params
25241 = chainon (params,
25242 objc_build_keyword_decl (selector,
25243 type_name,
25244 identifier,
25245 parm_attr));
25247 token = cp_lexer_peek_token (parser->lexer);
25250 if (params == NULL_TREE)
25252 cp_parser_error (parser, "objective-c++ method declaration is expected");
25253 return error_mark_node;
25256 /* We allow tail attributes for the method. */
25257 if (token->keyword == RID_ATTRIBUTE)
25259 *attributes = cp_parser_attributes_opt (parser);
25260 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25261 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25262 return params;
25263 cp_parser_error (parser,
25264 "method attributes must be specified at the end");
25265 return error_mark_node;
25268 if (params == NULL_TREE)
25270 cp_parser_error (parser, "objective-c++ method declaration is expected");
25271 return error_mark_node;
25273 return params;
25276 /* Parse the non-keyword Objective-C params. */
25278 static tree
25279 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25280 tree* attributes)
25282 tree params = make_node (TREE_LIST);
25283 cp_token *token = cp_lexer_peek_token (parser->lexer);
25284 *ellipsisp = false; /* Initially, assume no ellipsis. */
25286 while (token->type == CPP_COMMA)
25288 cp_parameter_declarator *parmdecl;
25289 tree parm;
25291 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25292 token = cp_lexer_peek_token (parser->lexer);
25294 if (token->type == CPP_ELLIPSIS)
25296 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25297 *ellipsisp = true;
25298 token = cp_lexer_peek_token (parser->lexer);
25299 break;
25302 /* TODO: parse attributes for tail parameters. */
25303 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25304 parm = grokdeclarator (parmdecl->declarator,
25305 &parmdecl->decl_specifiers,
25306 PARM, /*initialized=*/0,
25307 /*attrlist=*/NULL);
25309 chainon (params, build_tree_list (NULL_TREE, parm));
25310 token = cp_lexer_peek_token (parser->lexer);
25313 /* We allow tail attributes for the method. */
25314 if (token->keyword == RID_ATTRIBUTE)
25316 if (*attributes == NULL_TREE)
25318 *attributes = cp_parser_attributes_opt (parser);
25319 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25320 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25321 return params;
25323 else
25324 /* We have an error, but parse the attributes, so that we can
25325 carry on. */
25326 *attributes = cp_parser_attributes_opt (parser);
25328 cp_parser_error (parser,
25329 "method attributes must be specified at the end");
25330 return error_mark_node;
25333 return params;
25336 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25338 static void
25339 cp_parser_objc_interstitial_code (cp_parser* parser)
25341 cp_token *token = cp_lexer_peek_token (parser->lexer);
25343 /* If the next token is `extern' and the following token is a string
25344 literal, then we have a linkage specification. */
25345 if (token->keyword == RID_EXTERN
25346 && cp_parser_is_pure_string_literal
25347 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25348 cp_parser_linkage_specification (parser);
25349 /* Handle #pragma, if any. */
25350 else if (token->type == CPP_PRAGMA)
25351 cp_parser_pragma (parser, pragma_objc_icode);
25352 /* Allow stray semicolons. */
25353 else if (token->type == CPP_SEMICOLON)
25354 cp_lexer_consume_token (parser->lexer);
25355 /* Mark methods as optional or required, when building protocols. */
25356 else if (token->keyword == RID_AT_OPTIONAL)
25358 cp_lexer_consume_token (parser->lexer);
25359 objc_set_method_opt (true);
25361 else if (token->keyword == RID_AT_REQUIRED)
25363 cp_lexer_consume_token (parser->lexer);
25364 objc_set_method_opt (false);
25366 else if (token->keyword == RID_NAMESPACE)
25367 cp_parser_namespace_definition (parser);
25368 /* Other stray characters must generate errors. */
25369 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25371 cp_lexer_consume_token (parser->lexer);
25372 error ("stray %qs between Objective-C++ methods",
25373 token->type == CPP_OPEN_BRACE ? "{" : "}");
25375 /* Finally, try to parse a block-declaration, or a function-definition. */
25376 else
25377 cp_parser_block_declaration (parser, /*statement_p=*/false);
25380 /* Parse a method signature. */
25382 static tree
25383 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25385 tree rettype, kwdparms, optparms;
25386 bool ellipsis = false;
25387 bool is_class_method;
25389 is_class_method = cp_parser_objc_method_type (parser);
25390 rettype = cp_parser_objc_typename (parser);
25391 *attributes = NULL_TREE;
25392 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25393 if (kwdparms == error_mark_node)
25394 return error_mark_node;
25395 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25396 if (optparms == error_mark_node)
25397 return error_mark_node;
25399 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25402 static bool
25403 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25405 tree tattr;
25406 cp_lexer_save_tokens (parser->lexer);
25407 tattr = cp_parser_attributes_opt (parser);
25408 gcc_assert (tattr) ;
25410 /* If the attributes are followed by a method introducer, this is not allowed.
25411 Dump the attributes and flag the situation. */
25412 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25413 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25414 return true;
25416 /* Otherwise, the attributes introduce some interstitial code, possibly so
25417 rewind to allow that check. */
25418 cp_lexer_rollback_tokens (parser->lexer);
25419 return false;
25422 /* Parse an Objective-C method prototype list. */
25424 static void
25425 cp_parser_objc_method_prototype_list (cp_parser* parser)
25427 cp_token *token = cp_lexer_peek_token (parser->lexer);
25429 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25431 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25433 tree attributes, sig;
25434 bool is_class_method;
25435 if (token->type == CPP_PLUS)
25436 is_class_method = true;
25437 else
25438 is_class_method = false;
25439 sig = cp_parser_objc_method_signature (parser, &attributes);
25440 if (sig == error_mark_node)
25442 cp_parser_skip_to_end_of_block_or_statement (parser);
25443 token = cp_lexer_peek_token (parser->lexer);
25444 continue;
25446 objc_add_method_declaration (is_class_method, sig, attributes);
25447 cp_parser_consume_semicolon_at_end_of_statement (parser);
25449 else if (token->keyword == RID_AT_PROPERTY)
25450 cp_parser_objc_at_property_declaration (parser);
25451 else if (token->keyword == RID_ATTRIBUTE
25452 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25453 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25454 OPT_Wattributes,
25455 "prefix attributes are ignored for methods");
25456 else
25457 /* Allow for interspersed non-ObjC++ code. */
25458 cp_parser_objc_interstitial_code (parser);
25460 token = cp_lexer_peek_token (parser->lexer);
25463 if (token->type != CPP_EOF)
25464 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25465 else
25466 cp_parser_error (parser, "expected %<@end%>");
25468 objc_finish_interface ();
25471 /* Parse an Objective-C method definition list. */
25473 static void
25474 cp_parser_objc_method_definition_list (cp_parser* parser)
25476 cp_token *token = cp_lexer_peek_token (parser->lexer);
25478 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25480 tree meth;
25482 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25484 cp_token *ptk;
25485 tree sig, attribute;
25486 bool is_class_method;
25487 if (token->type == CPP_PLUS)
25488 is_class_method = true;
25489 else
25490 is_class_method = false;
25491 push_deferring_access_checks (dk_deferred);
25492 sig = cp_parser_objc_method_signature (parser, &attribute);
25493 if (sig == error_mark_node)
25495 cp_parser_skip_to_end_of_block_or_statement (parser);
25496 token = cp_lexer_peek_token (parser->lexer);
25497 continue;
25499 objc_start_method_definition (is_class_method, sig, attribute,
25500 NULL_TREE);
25502 /* For historical reasons, we accept an optional semicolon. */
25503 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25504 cp_lexer_consume_token (parser->lexer);
25506 ptk = cp_lexer_peek_token (parser->lexer);
25507 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25508 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25510 perform_deferred_access_checks (tf_warning_or_error);
25511 stop_deferring_access_checks ();
25512 meth = cp_parser_function_definition_after_declarator (parser,
25513 false);
25514 pop_deferring_access_checks ();
25515 objc_finish_method_definition (meth);
25518 /* The following case will be removed once @synthesize is
25519 completely implemented. */
25520 else if (token->keyword == RID_AT_PROPERTY)
25521 cp_parser_objc_at_property_declaration (parser);
25522 else if (token->keyword == RID_AT_SYNTHESIZE)
25523 cp_parser_objc_at_synthesize_declaration (parser);
25524 else if (token->keyword == RID_AT_DYNAMIC)
25525 cp_parser_objc_at_dynamic_declaration (parser);
25526 else if (token->keyword == RID_ATTRIBUTE
25527 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25528 warning_at (token->location, OPT_Wattributes,
25529 "prefix attributes are ignored for methods");
25530 else
25531 /* Allow for interspersed non-ObjC++ code. */
25532 cp_parser_objc_interstitial_code (parser);
25534 token = cp_lexer_peek_token (parser->lexer);
25537 if (token->type != CPP_EOF)
25538 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25539 else
25540 cp_parser_error (parser, "expected %<@end%>");
25542 objc_finish_implementation ();
25545 /* Parse Objective-C ivars. */
25547 static void
25548 cp_parser_objc_class_ivars (cp_parser* parser)
25550 cp_token *token = cp_lexer_peek_token (parser->lexer);
25552 if (token->type != CPP_OPEN_BRACE)
25553 return; /* No ivars specified. */
25555 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
25556 token = cp_lexer_peek_token (parser->lexer);
25558 while (token->type != CPP_CLOSE_BRACE
25559 && token->keyword != RID_AT_END && token->type != CPP_EOF)
25561 cp_decl_specifier_seq declspecs;
25562 int decl_class_or_enum_p;
25563 tree prefix_attributes;
25565 cp_parser_objc_visibility_spec (parser);
25567 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25568 break;
25570 cp_parser_decl_specifier_seq (parser,
25571 CP_PARSER_FLAGS_OPTIONAL,
25572 &declspecs,
25573 &decl_class_or_enum_p);
25575 /* auto, register, static, extern, mutable. */
25576 if (declspecs.storage_class != sc_none)
25578 cp_parser_error (parser, "invalid type for instance variable");
25579 declspecs.storage_class = sc_none;
25582 /* thread_local. */
25583 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25585 cp_parser_error (parser, "invalid type for instance variable");
25586 declspecs.locations[ds_thread] = 0;
25589 /* typedef. */
25590 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25592 cp_parser_error (parser, "invalid type for instance variable");
25593 declspecs.locations[ds_typedef] = 0;
25596 prefix_attributes = declspecs.attributes;
25597 declspecs.attributes = NULL_TREE;
25599 /* Keep going until we hit the `;' at the end of the
25600 declaration. */
25601 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25603 tree width = NULL_TREE, attributes, first_attribute, decl;
25604 cp_declarator *declarator = NULL;
25605 int ctor_dtor_or_conv_p;
25607 /* Check for a (possibly unnamed) bitfield declaration. */
25608 token = cp_lexer_peek_token (parser->lexer);
25609 if (token->type == CPP_COLON)
25610 goto eat_colon;
25612 if (token->type == CPP_NAME
25613 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25614 == CPP_COLON))
25616 /* Get the name of the bitfield. */
25617 declarator = make_id_declarator (NULL_TREE,
25618 cp_parser_identifier (parser),
25619 sfk_none);
25621 eat_colon:
25622 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25623 /* Get the width of the bitfield. */
25624 width
25625 = cp_parser_constant_expression (parser,
25626 /*allow_non_constant=*/false,
25627 NULL);
25629 else
25631 /* Parse the declarator. */
25632 declarator
25633 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25634 &ctor_dtor_or_conv_p,
25635 /*parenthesized_p=*/NULL,
25636 /*member_p=*/false);
25639 /* Look for attributes that apply to the ivar. */
25640 attributes = cp_parser_attributes_opt (parser);
25641 /* Remember which attributes are prefix attributes and
25642 which are not. */
25643 first_attribute = attributes;
25644 /* Combine the attributes. */
25645 attributes = chainon (prefix_attributes, attributes);
25647 if (width)
25648 /* Create the bitfield declaration. */
25649 decl = grokbitfield (declarator, &declspecs,
25650 width,
25651 attributes);
25652 else
25653 decl = grokfield (declarator, &declspecs,
25654 NULL_TREE, /*init_const_expr_p=*/false,
25655 NULL_TREE, attributes);
25657 /* Add the instance variable. */
25658 if (decl != error_mark_node && decl != NULL_TREE)
25659 objc_add_instance_variable (decl);
25661 /* Reset PREFIX_ATTRIBUTES. */
25662 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25663 attributes = TREE_CHAIN (attributes);
25664 if (attributes)
25665 TREE_CHAIN (attributes) = NULL_TREE;
25667 token = cp_lexer_peek_token (parser->lexer);
25669 if (token->type == CPP_COMMA)
25671 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25672 continue;
25674 break;
25677 cp_parser_consume_semicolon_at_end_of_statement (parser);
25678 token = cp_lexer_peek_token (parser->lexer);
25681 if (token->keyword == RID_AT_END)
25682 cp_parser_error (parser, "expected %<}%>");
25684 /* Do not consume the RID_AT_END, so it will be read again as terminating
25685 the @interface of @implementation. */
25686 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
25687 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
25689 /* For historical reasons, we accept an optional semicolon. */
25690 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25691 cp_lexer_consume_token (parser->lexer);
25694 /* Parse an Objective-C protocol declaration. */
25696 static void
25697 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
25699 tree proto, protorefs;
25700 cp_token *tok;
25702 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
25705 tok = cp_lexer_peek_token (parser->lexer);
25706 error_at (tok->location, "identifier expected after %<@protocol%>");
25707 cp_parser_consume_semicolon_at_end_of_statement (parser);
25708 return;
25711 /* See if we have a forward declaration or a definition. */
25712 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
25714 /* Try a forward declaration first. */
25715 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
25717 while (true)
25719 tree id;
25721 id = cp_parser_identifier (parser);
25722 if (id == error_mark_node)
25723 break;
25725 objc_declare_protocol (id, attributes);
25727 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25728 cp_lexer_consume_token (parser->lexer);
25729 else
25730 break;
25732 cp_parser_consume_semicolon_at_end_of_statement (parser);
25735 /* Ok, we got a full-fledged definition (or at least should). */
25736 else
25738 proto = cp_parser_identifier (parser);
25739 protorefs = cp_parser_objc_protocol_refs_opt (parser);
25740 objc_start_protocol (proto, protorefs, attributes);
25741 cp_parser_objc_method_prototype_list (parser);
25745 /* Parse an Objective-C superclass or category. */
25747 static void
25748 cp_parser_objc_superclass_or_category (cp_parser *parser,
25749 bool iface_p,
25750 tree *super,
25751 tree *categ, bool *is_class_extension)
25753 cp_token *next = cp_lexer_peek_token (parser->lexer);
25755 *super = *categ = NULL_TREE;
25756 *is_class_extension = false;
25757 if (next->type == CPP_COLON)
25759 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25760 *super = cp_parser_identifier (parser);
25762 else if (next->type == CPP_OPEN_PAREN)
25764 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25766 /* If there is no category name, and this is an @interface, we
25767 have a class extension. */
25768 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25770 *categ = NULL_TREE;
25771 *is_class_extension = true;
25773 else
25774 *categ = cp_parser_identifier (parser);
25776 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25780 /* Parse an Objective-C class interface. */
25782 static void
25783 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25785 tree name, super, categ, protos;
25786 bool is_class_extension;
25788 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25789 name = cp_parser_identifier (parser);
25790 if (name == error_mark_node)
25792 /* It's hard to recover because even if valid @interface stuff
25793 is to follow, we can't compile it (or validate it) if we
25794 don't even know which class it refers to. Let's assume this
25795 was a stray '@interface' token in the stream and skip it.
25797 return;
25799 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25800 &is_class_extension);
25801 protos = cp_parser_objc_protocol_refs_opt (parser);
25803 /* We have either a class or a category on our hands. */
25804 if (categ || is_class_extension)
25805 objc_start_category_interface (name, categ, protos, attributes);
25806 else
25808 objc_start_class_interface (name, super, protos, attributes);
25809 /* Handle instance variable declarations, if any. */
25810 cp_parser_objc_class_ivars (parser);
25811 objc_continue_interface ();
25814 cp_parser_objc_method_prototype_list (parser);
25817 /* Parse an Objective-C class implementation. */
25819 static void
25820 cp_parser_objc_class_implementation (cp_parser* parser)
25822 tree name, super, categ;
25823 bool is_class_extension;
25825 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25826 name = cp_parser_identifier (parser);
25827 if (name == error_mark_node)
25829 /* It's hard to recover because even if valid @implementation
25830 stuff is to follow, we can't compile it (or validate it) if
25831 we don't even know which class it refers to. Let's assume
25832 this was a stray '@implementation' token in the stream and
25833 skip it.
25835 return;
25837 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25838 &is_class_extension);
25840 /* We have either a class or a category on our hands. */
25841 if (categ)
25842 objc_start_category_implementation (name, categ);
25843 else
25845 objc_start_class_implementation (name, super);
25846 /* Handle instance variable declarations, if any. */
25847 cp_parser_objc_class_ivars (parser);
25848 objc_continue_implementation ();
25851 cp_parser_objc_method_definition_list (parser);
25854 /* Consume the @end token and finish off the implementation. */
25856 static void
25857 cp_parser_objc_end_implementation (cp_parser* parser)
25859 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25860 objc_finish_implementation ();
25863 /* Parse an Objective-C declaration. */
25865 static void
25866 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25868 /* Try to figure out what kind of declaration is present. */
25869 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25871 if (attributes)
25872 switch (kwd->keyword)
25874 case RID_AT_ALIAS:
25875 case RID_AT_CLASS:
25876 case RID_AT_END:
25877 error_at (kwd->location, "attributes may not be specified before"
25878 " the %<@%D%> Objective-C++ keyword",
25879 kwd->u.value);
25880 attributes = NULL;
25881 break;
25882 case RID_AT_IMPLEMENTATION:
25883 warning_at (kwd->location, OPT_Wattributes,
25884 "prefix attributes are ignored before %<@%D%>",
25885 kwd->u.value);
25886 attributes = NULL;
25887 default:
25888 break;
25891 switch (kwd->keyword)
25893 case RID_AT_ALIAS:
25894 cp_parser_objc_alias_declaration (parser);
25895 break;
25896 case RID_AT_CLASS:
25897 cp_parser_objc_class_declaration (parser);
25898 break;
25899 case RID_AT_PROTOCOL:
25900 cp_parser_objc_protocol_declaration (parser, attributes);
25901 break;
25902 case RID_AT_INTERFACE:
25903 cp_parser_objc_class_interface (parser, attributes);
25904 break;
25905 case RID_AT_IMPLEMENTATION:
25906 cp_parser_objc_class_implementation (parser);
25907 break;
25908 case RID_AT_END:
25909 cp_parser_objc_end_implementation (parser);
25910 break;
25911 default:
25912 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25913 kwd->u.value);
25914 cp_parser_skip_to_end_of_block_or_statement (parser);
25918 /* Parse an Objective-C try-catch-finally statement.
25920 objc-try-catch-finally-stmt:
25921 @try compound-statement objc-catch-clause-seq [opt]
25922 objc-finally-clause [opt]
25924 objc-catch-clause-seq:
25925 objc-catch-clause objc-catch-clause-seq [opt]
25927 objc-catch-clause:
25928 @catch ( objc-exception-declaration ) compound-statement
25930 objc-finally-clause:
25931 @finally compound-statement
25933 objc-exception-declaration:
25934 parameter-declaration
25935 '...'
25937 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25939 Returns NULL_TREE.
25941 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25942 for C. Keep them in sync. */
25944 static tree
25945 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25947 location_t location;
25948 tree stmt;
25950 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25951 location = cp_lexer_peek_token (parser->lexer)->location;
25952 objc_maybe_warn_exceptions (location);
25953 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25954 node, lest it get absorbed into the surrounding block. */
25955 stmt = push_stmt_list ();
25956 cp_parser_compound_statement (parser, NULL, false, false);
25957 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25959 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25961 cp_parameter_declarator *parm;
25962 tree parameter_declaration = error_mark_node;
25963 bool seen_open_paren = false;
25965 cp_lexer_consume_token (parser->lexer);
25966 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25967 seen_open_paren = true;
25968 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25970 /* We have "@catch (...)" (where the '...' are literally
25971 what is in the code). Skip the '...'.
25972 parameter_declaration is set to NULL_TREE, and
25973 objc_being_catch_clauses() knows that that means
25974 '...'. */
25975 cp_lexer_consume_token (parser->lexer);
25976 parameter_declaration = NULL_TREE;
25978 else
25980 /* We have "@catch (NSException *exception)" or something
25981 like that. Parse the parameter declaration. */
25982 parm = cp_parser_parameter_declaration (parser, false, NULL);
25983 if (parm == NULL)
25984 parameter_declaration = error_mark_node;
25985 else
25986 parameter_declaration = grokdeclarator (parm->declarator,
25987 &parm->decl_specifiers,
25988 PARM, /*initialized=*/0,
25989 /*attrlist=*/NULL);
25991 if (seen_open_paren)
25992 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25993 else
25995 /* If there was no open parenthesis, we are recovering from
25996 an error, and we are trying to figure out what mistake
25997 the user has made. */
25999 /* If there is an immediate closing parenthesis, the user
26000 probably forgot the opening one (ie, they typed "@catch
26001 NSException *e)". Parse the closing parenthesis and keep
26002 going. */
26003 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26004 cp_lexer_consume_token (parser->lexer);
26006 /* If these is no immediate closing parenthesis, the user
26007 probably doesn't know that parenthesis are required at
26008 all (ie, they typed "@catch NSException *e"). So, just
26009 forget about the closing parenthesis and keep going. */
26011 objc_begin_catch_clause (parameter_declaration);
26012 cp_parser_compound_statement (parser, NULL, false, false);
26013 objc_finish_catch_clause ();
26015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26017 cp_lexer_consume_token (parser->lexer);
26018 location = cp_lexer_peek_token (parser->lexer)->location;
26019 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26020 node, lest it get absorbed into the surrounding block. */
26021 stmt = push_stmt_list ();
26022 cp_parser_compound_statement (parser, NULL, false, false);
26023 objc_build_finally_clause (location, pop_stmt_list (stmt));
26026 return objc_finish_try_stmt ();
26029 /* Parse an Objective-C synchronized statement.
26031 objc-synchronized-stmt:
26032 @synchronized ( expression ) compound-statement
26034 Returns NULL_TREE. */
26036 static tree
26037 cp_parser_objc_synchronized_statement (cp_parser *parser)
26039 location_t location;
26040 tree lock, stmt;
26042 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26044 location = cp_lexer_peek_token (parser->lexer)->location;
26045 objc_maybe_warn_exceptions (location);
26046 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26047 lock = cp_parser_expression (parser, false, NULL);
26048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26050 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26051 node, lest it get absorbed into the surrounding block. */
26052 stmt = push_stmt_list ();
26053 cp_parser_compound_statement (parser, NULL, false, false);
26055 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26058 /* Parse an Objective-C throw statement.
26060 objc-throw-stmt:
26061 @throw assignment-expression [opt] ;
26063 Returns a constructed '@throw' statement. */
26065 static tree
26066 cp_parser_objc_throw_statement (cp_parser *parser)
26068 tree expr = NULL_TREE;
26069 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26071 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26074 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26076 cp_parser_consume_semicolon_at_end_of_statement (parser);
26078 return objc_build_throw_stmt (loc, expr);
26081 /* Parse an Objective-C statement. */
26083 static tree
26084 cp_parser_objc_statement (cp_parser * parser)
26086 /* Try to figure out what kind of declaration is present. */
26087 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26089 switch (kwd->keyword)
26091 case RID_AT_TRY:
26092 return cp_parser_objc_try_catch_finally_statement (parser);
26093 case RID_AT_SYNCHRONIZED:
26094 return cp_parser_objc_synchronized_statement (parser);
26095 case RID_AT_THROW:
26096 return cp_parser_objc_throw_statement (parser);
26097 default:
26098 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26099 kwd->u.value);
26100 cp_parser_skip_to_end_of_block_or_statement (parser);
26103 return error_mark_node;
26106 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26107 look ahead to see if an objc keyword follows the attributes. This
26108 is to detect the use of prefix attributes on ObjC @interface and
26109 @protocol. */
26111 static bool
26112 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26114 cp_lexer_save_tokens (parser->lexer);
26115 *attrib = cp_parser_attributes_opt (parser);
26116 gcc_assert (*attrib);
26117 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26119 cp_lexer_commit_tokens (parser->lexer);
26120 return true;
26122 cp_lexer_rollback_tokens (parser->lexer);
26123 return false;
26126 /* This routine is a minimal replacement for
26127 c_parser_struct_declaration () used when parsing the list of
26128 types/names or ObjC++ properties. For example, when parsing the
26129 code
26131 @property (readonly) int a, b, c;
26133 this function is responsible for parsing "int a, int b, int c" and
26134 returning the declarations as CHAIN of DECLs.
26136 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26137 similar parsing. */
26138 static tree
26139 cp_parser_objc_struct_declaration (cp_parser *parser)
26141 tree decls = NULL_TREE;
26142 cp_decl_specifier_seq declspecs;
26143 int decl_class_or_enum_p;
26144 tree prefix_attributes;
26146 cp_parser_decl_specifier_seq (parser,
26147 CP_PARSER_FLAGS_NONE,
26148 &declspecs,
26149 &decl_class_or_enum_p);
26151 if (declspecs.type == error_mark_node)
26152 return error_mark_node;
26154 /* auto, register, static, extern, mutable. */
26155 if (declspecs.storage_class != sc_none)
26157 cp_parser_error (parser, "invalid type for property");
26158 declspecs.storage_class = sc_none;
26161 /* thread_local. */
26162 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26164 cp_parser_error (parser, "invalid type for property");
26165 declspecs.locations[ds_thread] = 0;
26168 /* typedef. */
26169 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26171 cp_parser_error (parser, "invalid type for property");
26172 declspecs.locations[ds_typedef] = 0;
26175 prefix_attributes = declspecs.attributes;
26176 declspecs.attributes = NULL_TREE;
26178 /* Keep going until we hit the `;' at the end of the declaration. */
26179 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26181 tree attributes, first_attribute, decl;
26182 cp_declarator *declarator;
26183 cp_token *token;
26185 /* Parse the declarator. */
26186 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26187 NULL, NULL, false);
26189 /* Look for attributes that apply to the ivar. */
26190 attributes = cp_parser_attributes_opt (parser);
26191 /* Remember which attributes are prefix attributes and
26192 which are not. */
26193 first_attribute = attributes;
26194 /* Combine the attributes. */
26195 attributes = chainon (prefix_attributes, attributes);
26197 decl = grokfield (declarator, &declspecs,
26198 NULL_TREE, /*init_const_expr_p=*/false,
26199 NULL_TREE, attributes);
26201 if (decl == error_mark_node || decl == NULL_TREE)
26202 return error_mark_node;
26204 /* Reset PREFIX_ATTRIBUTES. */
26205 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26206 attributes = TREE_CHAIN (attributes);
26207 if (attributes)
26208 TREE_CHAIN (attributes) = NULL_TREE;
26210 DECL_CHAIN (decl) = decls;
26211 decls = decl;
26213 token = cp_lexer_peek_token (parser->lexer);
26214 if (token->type == CPP_COMMA)
26216 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26217 continue;
26219 else
26220 break;
26222 return decls;
26225 /* Parse an Objective-C @property declaration. The syntax is:
26227 objc-property-declaration:
26228 '@property' objc-property-attributes[opt] struct-declaration ;
26230 objc-property-attributes:
26231 '(' objc-property-attribute-list ')'
26233 objc-property-attribute-list:
26234 objc-property-attribute
26235 objc-property-attribute-list, objc-property-attribute
26237 objc-property-attribute
26238 'getter' = identifier
26239 'setter' = identifier
26240 'readonly'
26241 'readwrite'
26242 'assign'
26243 'retain'
26244 'copy'
26245 'nonatomic'
26247 For example:
26248 @property NSString *name;
26249 @property (readonly) id object;
26250 @property (retain, nonatomic, getter=getTheName) id name;
26251 @property int a, b, c;
26253 PS: This function is identical to
26254 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26255 static void
26256 cp_parser_objc_at_property_declaration (cp_parser *parser)
26258 /* The following variables hold the attributes of the properties as
26259 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26260 seen. When we see an attribute, we set them to 'true' (if they
26261 are boolean properties) or to the identifier (if they have an
26262 argument, ie, for getter and setter). Note that here we only
26263 parse the list of attributes, check the syntax and accumulate the
26264 attributes that we find. objc_add_property_declaration() will
26265 then process the information. */
26266 bool property_assign = false;
26267 bool property_copy = false;
26268 tree property_getter_ident = NULL_TREE;
26269 bool property_nonatomic = false;
26270 bool property_readonly = false;
26271 bool property_readwrite = false;
26272 bool property_retain = false;
26273 tree property_setter_ident = NULL_TREE;
26275 /* 'properties' is the list of properties that we read. Usually a
26276 single one, but maybe more (eg, in "@property int a, b, c;" there
26277 are three). */
26278 tree properties;
26279 location_t loc;
26281 loc = cp_lexer_peek_token (parser->lexer)->location;
26283 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26285 /* Parse the optional attribute list... */
26286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26288 /* Eat the '('. */
26289 cp_lexer_consume_token (parser->lexer);
26291 while (true)
26293 bool syntax_error = false;
26294 cp_token *token = cp_lexer_peek_token (parser->lexer);
26295 enum rid keyword;
26297 if (token->type != CPP_NAME)
26299 cp_parser_error (parser, "expected identifier");
26300 break;
26302 keyword = C_RID_CODE (token->u.value);
26303 cp_lexer_consume_token (parser->lexer);
26304 switch (keyword)
26306 case RID_ASSIGN: property_assign = true; break;
26307 case RID_COPY: property_copy = true; break;
26308 case RID_NONATOMIC: property_nonatomic = true; break;
26309 case RID_READONLY: property_readonly = true; break;
26310 case RID_READWRITE: property_readwrite = true; break;
26311 case RID_RETAIN: property_retain = true; break;
26313 case RID_GETTER:
26314 case RID_SETTER:
26315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26317 if (keyword == RID_GETTER)
26318 cp_parser_error (parser,
26319 "missing %<=%> (after %<getter%> attribute)");
26320 else
26321 cp_parser_error (parser,
26322 "missing %<=%> (after %<setter%> attribute)");
26323 syntax_error = true;
26324 break;
26326 cp_lexer_consume_token (parser->lexer); /* eat the = */
26327 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26329 cp_parser_error (parser, "expected identifier");
26330 syntax_error = true;
26331 break;
26333 if (keyword == RID_SETTER)
26335 if (property_setter_ident != NULL_TREE)
26337 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26338 cp_lexer_consume_token (parser->lexer);
26340 else
26341 property_setter_ident = cp_parser_objc_selector (parser);
26342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26343 cp_parser_error (parser, "setter name must terminate with %<:%>");
26344 else
26345 cp_lexer_consume_token (parser->lexer);
26347 else
26349 if (property_getter_ident != NULL_TREE)
26351 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26352 cp_lexer_consume_token (parser->lexer);
26354 else
26355 property_getter_ident = cp_parser_objc_selector (parser);
26357 break;
26358 default:
26359 cp_parser_error (parser, "unknown property attribute");
26360 syntax_error = true;
26361 break;
26364 if (syntax_error)
26365 break;
26367 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26368 cp_lexer_consume_token (parser->lexer);
26369 else
26370 break;
26373 /* FIXME: "@property (setter, assign);" will generate a spurious
26374 "error: expected ‘)’ before ‘,’ token". This is because
26375 cp_parser_require, unlike the C counterpart, will produce an
26376 error even if we are in error recovery. */
26377 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26379 cp_parser_skip_to_closing_parenthesis (parser,
26380 /*recovering=*/true,
26381 /*or_comma=*/false,
26382 /*consume_paren=*/true);
26386 /* ... and the property declaration(s). */
26387 properties = cp_parser_objc_struct_declaration (parser);
26389 if (properties == error_mark_node)
26391 cp_parser_skip_to_end_of_statement (parser);
26392 /* If the next token is now a `;', consume it. */
26393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26394 cp_lexer_consume_token (parser->lexer);
26395 return;
26398 if (properties == NULL_TREE)
26399 cp_parser_error (parser, "expected identifier");
26400 else
26402 /* Comma-separated properties are chained together in
26403 reverse order; add them one by one. */
26404 properties = nreverse (properties);
26406 for (; properties; properties = TREE_CHAIN (properties))
26407 objc_add_property_declaration (loc, copy_node (properties),
26408 property_readonly, property_readwrite,
26409 property_assign, property_retain,
26410 property_copy, property_nonatomic,
26411 property_getter_ident, property_setter_ident);
26414 cp_parser_consume_semicolon_at_end_of_statement (parser);
26417 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26419 objc-synthesize-declaration:
26420 @synthesize objc-synthesize-identifier-list ;
26422 objc-synthesize-identifier-list:
26423 objc-synthesize-identifier
26424 objc-synthesize-identifier-list, objc-synthesize-identifier
26426 objc-synthesize-identifier
26427 identifier
26428 identifier = identifier
26430 For example:
26431 @synthesize MyProperty;
26432 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26434 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26435 for C. Keep them in sync.
26437 static void
26438 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26440 tree list = NULL_TREE;
26441 location_t loc;
26442 loc = cp_lexer_peek_token (parser->lexer)->location;
26444 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26445 while (true)
26447 tree property, ivar;
26448 property = cp_parser_identifier (parser);
26449 if (property == error_mark_node)
26451 cp_parser_consume_semicolon_at_end_of_statement (parser);
26452 return;
26454 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26456 cp_lexer_consume_token (parser->lexer);
26457 ivar = cp_parser_identifier (parser);
26458 if (ivar == error_mark_node)
26460 cp_parser_consume_semicolon_at_end_of_statement (parser);
26461 return;
26464 else
26465 ivar = NULL_TREE;
26466 list = chainon (list, build_tree_list (ivar, property));
26467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26468 cp_lexer_consume_token (parser->lexer);
26469 else
26470 break;
26472 cp_parser_consume_semicolon_at_end_of_statement (parser);
26473 objc_add_synthesize_declaration (loc, list);
26476 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26478 objc-dynamic-declaration:
26479 @dynamic identifier-list ;
26481 For example:
26482 @dynamic MyProperty;
26483 @dynamic MyProperty, AnotherProperty;
26485 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26486 for C. Keep them in sync.
26488 static void
26489 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26491 tree list = NULL_TREE;
26492 location_t loc;
26493 loc = cp_lexer_peek_token (parser->lexer)->location;
26495 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26496 while (true)
26498 tree property;
26499 property = cp_parser_identifier (parser);
26500 if (property == error_mark_node)
26502 cp_parser_consume_semicolon_at_end_of_statement (parser);
26503 return;
26505 list = chainon (list, build_tree_list (NULL, property));
26506 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26507 cp_lexer_consume_token (parser->lexer);
26508 else
26509 break;
26511 cp_parser_consume_semicolon_at_end_of_statement (parser);
26512 objc_add_dynamic_declaration (loc, list);
26516 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26518 /* Returns name of the next clause.
26519 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26520 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26521 returned and the token is consumed. */
26523 static pragma_omp_clause
26524 cp_parser_omp_clause_name (cp_parser *parser)
26526 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26529 result = PRAGMA_OMP_CLAUSE_IF;
26530 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26531 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26532 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26533 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26534 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26535 result = PRAGMA_OMP_CLAUSE_FOR;
26536 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26538 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26539 const char *p = IDENTIFIER_POINTER (id);
26541 switch (p[0])
26543 case 'a':
26544 if (!strcmp ("aligned", p))
26545 result = PRAGMA_OMP_CLAUSE_ALIGNED;
26546 break;
26547 case 'c':
26548 if (!strcmp ("collapse", p))
26549 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26550 else if (!strcmp ("copyin", p))
26551 result = PRAGMA_OMP_CLAUSE_COPYIN;
26552 else if (!strcmp ("copyprivate", p))
26553 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
26554 break;
26555 case 'd':
26556 if (!strcmp ("depend", p))
26557 result = PRAGMA_OMP_CLAUSE_DEPEND;
26558 else if (!strcmp ("device", p))
26559 result = PRAGMA_OMP_CLAUSE_DEVICE;
26560 else if (!strcmp ("dist_schedule", p))
26561 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
26562 break;
26563 case 'f':
26564 if (!strcmp ("final", p))
26565 result = PRAGMA_OMP_CLAUSE_FINAL;
26566 else if (!strcmp ("firstprivate", p))
26567 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
26568 else if (!strcmp ("from", p))
26569 result = PRAGMA_OMP_CLAUSE_FROM;
26570 break;
26571 case 'i':
26572 if (!strcmp ("inbranch", p))
26573 result = PRAGMA_OMP_CLAUSE_INBRANCH;
26574 break;
26575 case 'l':
26576 if (!strcmp ("lastprivate", p))
26577 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
26578 else if (!strcmp ("linear", p))
26579 result = PRAGMA_OMP_CLAUSE_LINEAR;
26580 break;
26581 case 'm':
26582 if (!strcmp ("map", p))
26583 result = PRAGMA_OMP_CLAUSE_MAP;
26584 else if (!strcmp ("mergeable", p))
26585 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
26586 break;
26587 case 'n':
26588 if (!strcmp ("notinbranch", p))
26589 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
26590 else if (!strcmp ("nowait", p))
26591 result = PRAGMA_OMP_CLAUSE_NOWAIT;
26592 else if (!strcmp ("num_teams", p))
26593 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
26594 else if (!strcmp ("num_threads", p))
26595 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
26596 break;
26597 case 'o':
26598 if (!strcmp ("ordered", p))
26599 result = PRAGMA_OMP_CLAUSE_ORDERED;
26600 break;
26601 case 'p':
26602 if (!strcmp ("parallel", p))
26603 result = PRAGMA_OMP_CLAUSE_PARALLEL;
26604 else if (!strcmp ("proc_bind", p))
26605 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
26606 break;
26607 case 'r':
26608 if (!strcmp ("reduction", p))
26609 result = PRAGMA_OMP_CLAUSE_REDUCTION;
26610 break;
26611 case 's':
26612 if (!strcmp ("safelen", p))
26613 result = PRAGMA_OMP_CLAUSE_SAFELEN;
26614 else if (!strcmp ("schedule", p))
26615 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
26616 else if (!strcmp ("sections", p))
26617 result = PRAGMA_OMP_CLAUSE_SECTIONS;
26618 else if (!strcmp ("shared", p))
26619 result = PRAGMA_OMP_CLAUSE_SHARED;
26620 else if (!strcmp ("simdlen", p))
26621 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
26622 break;
26623 case 't':
26624 if (!strcmp ("taskgroup", p))
26625 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
26626 else if (!strcmp ("thread_limit", p))
26627 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
26628 else if (!strcmp ("to", p))
26629 result = PRAGMA_OMP_CLAUSE_TO;
26630 break;
26631 case 'u':
26632 if (!strcmp ("uniform", p))
26633 result = PRAGMA_OMP_CLAUSE_UNIFORM;
26634 else if (!strcmp ("untied", p))
26635 result = PRAGMA_OMP_CLAUSE_UNTIED;
26636 break;
26640 if (result != PRAGMA_OMP_CLAUSE_NONE)
26641 cp_lexer_consume_token (parser->lexer);
26643 return result;
26646 /* Validate that a clause of the given type does not already exist. */
26648 static void
26649 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
26650 const char *name, location_t location)
26652 tree c;
26654 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26655 if (OMP_CLAUSE_CODE (c) == code)
26657 error_at (location, "too many %qs clauses", name);
26658 break;
26662 /* OpenMP 2.5:
26663 variable-list:
26664 identifier
26665 variable-list , identifier
26667 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
26668 colon). An opening parenthesis will have been consumed by the caller.
26670 If KIND is nonzero, create the appropriate node and install the decl
26671 in OMP_CLAUSE_DECL and add the node to the head of the list.
26673 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26674 return the list created.
26676 COLON can be NULL if only closing parenthesis should end the list,
26677 or pointer to bool which will receive false if the list is terminated
26678 by closing parenthesis or true if the list is terminated by colon. */
26680 static tree
26681 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
26682 tree list, bool *colon)
26684 cp_token *token;
26685 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26686 if (colon)
26688 parser->colon_corrects_to_scope_p = false;
26689 *colon = false;
26691 while (1)
26693 tree name, decl;
26695 token = cp_lexer_peek_token (parser->lexer);
26696 name = cp_parser_id_expression (parser, /*template_p=*/false,
26697 /*check_dependency_p=*/true,
26698 /*template_p=*/NULL,
26699 /*declarator_p=*/false,
26700 /*optional_p=*/false);
26701 if (name == error_mark_node)
26702 goto skip_comma;
26704 decl = cp_parser_lookup_name_simple (parser, name, token->location);
26705 if (decl == error_mark_node)
26706 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
26707 token->location);
26708 else if (kind != 0)
26710 switch (kind)
26712 case OMP_CLAUSE_MAP:
26713 case OMP_CLAUSE_FROM:
26714 case OMP_CLAUSE_TO:
26715 case OMP_CLAUSE_DEPEND:
26716 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26718 tree low_bound = NULL_TREE, length = NULL_TREE;
26720 parser->colon_corrects_to_scope_p = false;
26721 cp_lexer_consume_token (parser->lexer);
26722 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26723 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
26724 NULL);
26725 if (!colon)
26726 parser->colon_corrects_to_scope_p
26727 = saved_colon_corrects_to_scope_p;
26728 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
26729 length = integer_one_node;
26730 else
26732 /* Look for `:'. */
26733 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26734 goto skip_comma;
26735 if (!cp_lexer_next_token_is (parser->lexer,
26736 CPP_CLOSE_SQUARE))
26737 length = cp_parser_expression (parser,
26738 /*cast_p=*/false,
26739 NULL);
26741 /* Look for the closing `]'. */
26742 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
26743 RT_CLOSE_SQUARE))
26744 goto skip_comma;
26745 decl = tree_cons (low_bound, length, decl);
26747 break;
26748 default:
26749 break;
26752 tree u = build_omp_clause (token->location, kind);
26753 OMP_CLAUSE_DECL (u) = decl;
26754 OMP_CLAUSE_CHAIN (u) = list;
26755 list = u;
26757 else
26758 list = tree_cons (decl, NULL_TREE, list);
26760 get_comma:
26761 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26762 break;
26763 cp_lexer_consume_token (parser->lexer);
26766 if (colon)
26767 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26769 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26771 *colon = true;
26772 cp_parser_require (parser, CPP_COLON, RT_COLON);
26773 return list;
26776 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26778 int ending;
26780 /* Try to resync to an unnested comma. Copied from
26781 cp_parser_parenthesized_expression_list. */
26782 skip_comma:
26783 if (colon)
26784 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26785 ending = cp_parser_skip_to_closing_parenthesis (parser,
26786 /*recovering=*/true,
26787 /*or_comma=*/true,
26788 /*consume_paren=*/true);
26789 if (ending < 0)
26790 goto get_comma;
26793 return list;
26796 /* Similarly, but expect leading and trailing parenthesis. This is a very
26797 common case for omp clauses. */
26799 static tree
26800 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
26802 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26803 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
26804 return list;
26807 /* OpenMP 3.0:
26808 collapse ( constant-expression ) */
26810 static tree
26811 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
26813 tree c, num;
26814 location_t loc;
26815 HOST_WIDE_INT n;
26817 loc = cp_lexer_peek_token (parser->lexer)->location;
26818 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26819 return list;
26821 num = cp_parser_constant_expression (parser, false, NULL);
26823 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26824 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26825 /*or_comma=*/false,
26826 /*consume_paren=*/true);
26828 if (num == error_mark_node)
26829 return list;
26830 num = fold_non_dependent_expr (num);
26831 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
26832 || !host_integerp (num, 0)
26833 || (n = tree_low_cst (num, 0)) <= 0
26834 || (int) n != n)
26836 error_at (loc, "collapse argument needs positive constant integer expression");
26837 return list;
26840 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
26841 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
26842 OMP_CLAUSE_CHAIN (c) = list;
26843 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
26845 return c;
26848 /* OpenMP 2.5:
26849 default ( shared | none ) */
26851 static tree
26852 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26854 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26855 tree c;
26857 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26858 return list;
26859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26861 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26862 const char *p = IDENTIFIER_POINTER (id);
26864 switch (p[0])
26866 case 'n':
26867 if (strcmp ("none", p) != 0)
26868 goto invalid_kind;
26869 kind = OMP_CLAUSE_DEFAULT_NONE;
26870 break;
26872 case 's':
26873 if (strcmp ("shared", p) != 0)
26874 goto invalid_kind;
26875 kind = OMP_CLAUSE_DEFAULT_SHARED;
26876 break;
26878 default:
26879 goto invalid_kind;
26882 cp_lexer_consume_token (parser->lexer);
26884 else
26886 invalid_kind:
26887 cp_parser_error (parser, "expected %<none%> or %<shared%>");
26890 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26891 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26892 /*or_comma=*/false,
26893 /*consume_paren=*/true);
26895 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26896 return list;
26898 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26899 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26900 OMP_CLAUSE_CHAIN (c) = list;
26901 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26903 return c;
26906 /* OpenMP 3.1:
26907 final ( expression ) */
26909 static tree
26910 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26912 tree t, c;
26914 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26915 return list;
26917 t = cp_parser_condition (parser);
26919 if (t == error_mark_node
26920 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26921 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26922 /*or_comma=*/false,
26923 /*consume_paren=*/true);
26925 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26927 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26928 OMP_CLAUSE_FINAL_EXPR (c) = t;
26929 OMP_CLAUSE_CHAIN (c) = list;
26931 return c;
26934 /* OpenMP 2.5:
26935 if ( expression ) */
26937 static tree
26938 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26940 tree t, c;
26942 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26943 return list;
26945 t = cp_parser_condition (parser);
26947 if (t == error_mark_node
26948 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26950 /*or_comma=*/false,
26951 /*consume_paren=*/true);
26953 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26955 c = build_omp_clause (location, OMP_CLAUSE_IF);
26956 OMP_CLAUSE_IF_EXPR (c) = t;
26957 OMP_CLAUSE_CHAIN (c) = list;
26959 return c;
26962 /* OpenMP 3.1:
26963 mergeable */
26965 static tree
26966 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26967 tree list, location_t location)
26969 tree c;
26971 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26972 location);
26974 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
26975 OMP_CLAUSE_CHAIN (c) = list;
26976 return c;
26979 /* OpenMP 2.5:
26980 nowait */
26982 static tree
26983 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
26984 tree list, location_t location)
26986 tree c;
26988 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
26990 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
26991 OMP_CLAUSE_CHAIN (c) = list;
26992 return c;
26995 /* OpenMP 2.5:
26996 num_threads ( expression ) */
26998 static tree
26999 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27000 location_t location)
27002 tree t, c;
27004 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27005 return list;
27007 t = cp_parser_expression (parser, false, NULL);
27009 if (t == error_mark_node
27010 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27011 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27012 /*or_comma=*/false,
27013 /*consume_paren=*/true);
27015 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27016 "num_threads", location);
27018 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27019 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27020 OMP_CLAUSE_CHAIN (c) = list;
27022 return c;
27025 /* OpenMP 2.5:
27026 ordered */
27028 static tree
27029 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27030 tree list, location_t location)
27032 tree c;
27034 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27035 "ordered", location);
27037 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27038 OMP_CLAUSE_CHAIN (c) = list;
27039 return c;
27042 /* OpenMP 2.5:
27043 reduction ( reduction-operator : variable-list )
27045 reduction-operator:
27046 One of: + * - & ^ | && ||
27048 OpenMP 3.1:
27050 reduction-operator:
27051 One of: + * - & ^ | && || min max
27053 OpenMP 4.0:
27055 reduction-operator:
27056 One of: + * - & ^ | && ||
27057 id-expression */
27059 static tree
27060 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27062 enum tree_code code = ERROR_MARK;
27063 tree nlist, c, id = NULL_TREE;
27065 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27066 return list;
27068 switch (cp_lexer_peek_token (parser->lexer)->type)
27070 case CPP_PLUS: code = PLUS_EXPR; break;
27071 case CPP_MULT: code = MULT_EXPR; break;
27072 case CPP_MINUS: code = MINUS_EXPR; break;
27073 case CPP_AND: code = BIT_AND_EXPR; break;
27074 case CPP_XOR: code = BIT_XOR_EXPR; break;
27075 case CPP_OR: code = BIT_IOR_EXPR; break;
27076 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27077 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27078 default: break;
27081 if (code != ERROR_MARK)
27082 cp_lexer_consume_token (parser->lexer);
27083 else
27085 bool saved_colon_corrects_to_scope_p;
27086 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27087 parser->colon_corrects_to_scope_p = false;
27088 id = cp_parser_id_expression (parser, /*template_p=*/false,
27089 /*check_dependency_p=*/true,
27090 /*template_p=*/NULL,
27091 /*declarator_p=*/false,
27092 /*optional_p=*/false);
27093 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27094 if (identifier_p (id))
27096 const char *p = IDENTIFIER_POINTER (id);
27098 if (strcmp (p, "min") == 0)
27099 code = MIN_EXPR;
27100 else if (strcmp (p, "max") == 0)
27101 code = MAX_EXPR;
27102 else if (id == ansi_opname (PLUS_EXPR))
27103 code = PLUS_EXPR;
27104 else if (id == ansi_opname (MULT_EXPR))
27105 code = MULT_EXPR;
27106 else if (id == ansi_opname (MINUS_EXPR))
27107 code = MINUS_EXPR;
27108 else if (id == ansi_opname (BIT_AND_EXPR))
27109 code = BIT_AND_EXPR;
27110 else if (id == ansi_opname (BIT_IOR_EXPR))
27111 code = BIT_IOR_EXPR;
27112 else if (id == ansi_opname (BIT_XOR_EXPR))
27113 code = BIT_XOR_EXPR;
27114 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27115 code = TRUTH_ANDIF_EXPR;
27116 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27117 code = TRUTH_ORIF_EXPR;
27118 id = omp_reduction_id (code, id, NULL_TREE);
27119 tree scope = parser->scope;
27120 if (scope)
27121 id = build_qualified_name (NULL_TREE, scope, id, false);
27122 parser->scope = NULL_TREE;
27123 parser->qualifying_scope = NULL_TREE;
27124 parser->object_scope = NULL_TREE;
27126 else
27128 error ("invalid reduction-identifier");
27129 resync_fail:
27130 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27131 /*or_comma=*/false,
27132 /*consume_paren=*/true);
27133 return list;
27137 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27138 goto resync_fail;
27140 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27141 NULL);
27142 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27144 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27145 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27148 return nlist;
27151 /* OpenMP 2.5:
27152 schedule ( schedule-kind )
27153 schedule ( schedule-kind , expression )
27155 schedule-kind:
27156 static | dynamic | guided | runtime | auto */
27158 static tree
27159 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27161 tree c, t;
27163 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27164 return list;
27166 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27170 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27171 const char *p = IDENTIFIER_POINTER (id);
27173 switch (p[0])
27175 case 'd':
27176 if (strcmp ("dynamic", p) != 0)
27177 goto invalid_kind;
27178 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27179 break;
27181 case 'g':
27182 if (strcmp ("guided", p) != 0)
27183 goto invalid_kind;
27184 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27185 break;
27187 case 'r':
27188 if (strcmp ("runtime", p) != 0)
27189 goto invalid_kind;
27190 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27191 break;
27193 default:
27194 goto invalid_kind;
27197 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27198 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27199 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27200 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27201 else
27202 goto invalid_kind;
27203 cp_lexer_consume_token (parser->lexer);
27205 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27207 cp_token *token;
27208 cp_lexer_consume_token (parser->lexer);
27210 token = cp_lexer_peek_token (parser->lexer);
27211 t = cp_parser_assignment_expression (parser, false, NULL);
27213 if (t == error_mark_node)
27214 goto resync_fail;
27215 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27216 error_at (token->location, "schedule %<runtime%> does not take "
27217 "a %<chunk_size%> parameter");
27218 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27219 error_at (token->location, "schedule %<auto%> does not take "
27220 "a %<chunk_size%> parameter");
27221 else
27222 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27224 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27225 goto resync_fail;
27227 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27228 goto resync_fail;
27230 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27231 OMP_CLAUSE_CHAIN (c) = list;
27232 return c;
27234 invalid_kind:
27235 cp_parser_error (parser, "invalid schedule kind");
27236 resync_fail:
27237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27238 /*or_comma=*/false,
27239 /*consume_paren=*/true);
27240 return list;
27243 /* OpenMP 3.0:
27244 untied */
27246 static tree
27247 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27248 tree list, location_t location)
27250 tree c;
27252 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27254 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27255 OMP_CLAUSE_CHAIN (c) = list;
27256 return c;
27259 /* OpenMP 4.0:
27260 inbranch
27261 notinbranch */
27263 static tree
27264 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27265 tree list, location_t location)
27267 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27268 tree c = build_omp_clause (location, code);
27269 OMP_CLAUSE_CHAIN (c) = list;
27270 return c;
27273 /* OpenMP 4.0:
27274 parallel
27276 sections
27277 taskgroup */
27279 static tree
27280 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27281 enum omp_clause_code code,
27282 tree list, location_t location)
27284 tree c = build_omp_clause (location, code);
27285 OMP_CLAUSE_CHAIN (c) = list;
27286 return c;
27289 /* OpenMP 4.0:
27290 num_teams ( expression ) */
27292 static tree
27293 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27294 location_t location)
27296 tree t, c;
27298 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27299 return list;
27301 t = cp_parser_expression (parser, false, NULL);
27303 if (t == error_mark_node
27304 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27305 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27306 /*or_comma=*/false,
27307 /*consume_paren=*/true);
27309 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27310 "num_teams", location);
27312 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27313 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27314 OMP_CLAUSE_CHAIN (c) = list;
27316 return c;
27319 /* OpenMP 4.0:
27320 thread_limit ( expression ) */
27322 static tree
27323 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27324 location_t location)
27326 tree t, c;
27328 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27329 return list;
27331 t = cp_parser_expression (parser, false, NULL);
27333 if (t == error_mark_node
27334 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27335 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27336 /*or_comma=*/false,
27337 /*consume_paren=*/true);
27339 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27340 "thread_limit", location);
27342 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27343 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27344 OMP_CLAUSE_CHAIN (c) = list;
27346 return c;
27349 /* OpenMP 4.0:
27350 aligned ( variable-list )
27351 aligned ( variable-list : constant-expression ) */
27353 static tree
27354 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27356 tree nlist, c, alignment = NULL_TREE;
27357 bool colon;
27359 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27360 return list;
27362 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27363 &colon);
27365 if (colon)
27367 alignment = cp_parser_constant_expression (parser, false, NULL);
27369 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27370 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27371 /*or_comma=*/false,
27372 /*consume_paren=*/true);
27374 if (alignment == error_mark_node)
27375 alignment = NULL_TREE;
27378 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27379 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27381 return nlist;
27384 /* OpenMP 4.0:
27385 linear ( variable-list )
27386 linear ( variable-list : expression ) */
27388 static tree
27389 cp_parser_omp_clause_linear (cp_parser *parser, tree list)
27391 tree nlist, c, step = integer_one_node;
27392 bool colon;
27394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27395 return list;
27397 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27398 &colon);
27400 if (colon)
27402 step = cp_parser_expression (parser, false, NULL);
27404 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27406 /*or_comma=*/false,
27407 /*consume_paren=*/true);
27409 if (step == error_mark_node)
27410 return list;
27413 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27414 OMP_CLAUSE_LINEAR_STEP (c) = step;
27416 return nlist;
27419 /* OpenMP 4.0:
27420 safelen ( constant-expression ) */
27422 static tree
27423 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27424 location_t location)
27426 tree t, c;
27428 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27429 return list;
27431 t = cp_parser_constant_expression (parser, false, NULL);
27433 if (t == error_mark_node
27434 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27436 /*or_comma=*/false,
27437 /*consume_paren=*/true);
27439 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27441 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27442 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27443 OMP_CLAUSE_CHAIN (c) = list;
27445 return c;
27448 /* OpenMP 4.0:
27449 simdlen ( constant-expression ) */
27451 static tree
27452 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27453 location_t location)
27455 tree t, c;
27457 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27458 return list;
27460 t = cp_parser_constant_expression (parser, false, NULL);
27462 if (t == error_mark_node
27463 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27464 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27465 /*or_comma=*/false,
27466 /*consume_paren=*/true);
27468 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27470 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27471 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27472 OMP_CLAUSE_CHAIN (c) = list;
27474 return c;
27477 /* OpenMP 4.0:
27478 depend ( depend-kind : variable-list )
27480 depend-kind:
27481 in | out | inout */
27483 static tree
27484 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27486 tree nlist, c;
27487 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27489 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27490 return list;
27492 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27494 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27495 const char *p = IDENTIFIER_POINTER (id);
27497 if (strcmp ("in", p) == 0)
27498 kind = OMP_CLAUSE_DEPEND_IN;
27499 else if (strcmp ("inout", p) == 0)
27500 kind = OMP_CLAUSE_DEPEND_INOUT;
27501 else if (strcmp ("out", p) == 0)
27502 kind = OMP_CLAUSE_DEPEND_OUT;
27503 else
27504 goto invalid_kind;
27506 else
27507 goto invalid_kind;
27509 cp_lexer_consume_token (parser->lexer);
27510 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27511 goto resync_fail;
27513 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27514 NULL);
27516 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27517 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27519 return nlist;
27521 invalid_kind:
27522 cp_parser_error (parser, "invalid depend kind");
27523 resync_fail:
27524 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27525 /*or_comma=*/false,
27526 /*consume_paren=*/true);
27527 return list;
27530 /* OpenMP 4.0:
27531 map ( map-kind : variable-list )
27532 map ( variable-list )
27534 map-kind:
27535 alloc | to | from | tofrom */
27537 static tree
27538 cp_parser_omp_clause_map (cp_parser *parser, tree list)
27540 tree nlist, c;
27541 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
27543 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27544 return list;
27546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
27547 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
27549 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27550 const char *p = IDENTIFIER_POINTER (id);
27552 if (strcmp ("alloc", p) == 0)
27553 kind = OMP_CLAUSE_MAP_ALLOC;
27554 else if (strcmp ("to", p) == 0)
27555 kind = OMP_CLAUSE_MAP_TO;
27556 else if (strcmp ("from", p) == 0)
27557 kind = OMP_CLAUSE_MAP_FROM;
27558 else if (strcmp ("tofrom", p) == 0)
27559 kind = OMP_CLAUSE_MAP_TOFROM;
27560 else
27562 cp_parser_error (parser, "invalid map kind");
27563 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27564 /*or_comma=*/false,
27565 /*consume_paren=*/true);
27566 return list;
27568 cp_lexer_consume_token (parser->lexer);
27569 cp_lexer_consume_token (parser->lexer);
27572 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
27573 NULL);
27575 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27576 OMP_CLAUSE_MAP_KIND (c) = kind;
27578 return nlist;
27581 /* OpenMP 4.0:
27582 device ( expression ) */
27584 static tree
27585 cp_parser_omp_clause_device (cp_parser *parser, tree list,
27586 location_t location)
27588 tree t, c;
27590 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27591 return list;
27593 t = cp_parser_expression (parser, false, NULL);
27595 if (t == error_mark_node
27596 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27597 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27598 /*or_comma=*/false,
27599 /*consume_paren=*/true);
27601 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
27602 "device", location);
27604 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
27605 OMP_CLAUSE_DEVICE_ID (c) = t;
27606 OMP_CLAUSE_CHAIN (c) = list;
27608 return c;
27611 /* OpenMP 4.0:
27612 dist_schedule ( static )
27613 dist_schedule ( static , expression ) */
27615 static tree
27616 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
27617 location_t location)
27619 tree c, t;
27621 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27622 return list;
27624 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
27626 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27627 goto invalid_kind;
27628 cp_lexer_consume_token (parser->lexer);
27630 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27632 cp_lexer_consume_token (parser->lexer);
27634 t = cp_parser_assignment_expression (parser, false, NULL);
27636 if (t == error_mark_node)
27637 goto resync_fail;
27638 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
27640 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27641 goto resync_fail;
27643 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27644 goto resync_fail;
27646 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
27647 location);
27648 OMP_CLAUSE_CHAIN (c) = list;
27649 return c;
27651 invalid_kind:
27652 cp_parser_error (parser, "invalid dist_schedule kind");
27653 resync_fail:
27654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27655 /*or_comma=*/false,
27656 /*consume_paren=*/true);
27657 return list;
27660 /* OpenMP 4.0:
27661 proc_bind ( proc-bind-kind )
27663 proc-bind-kind:
27664 master | close | spread */
27666 static tree
27667 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
27668 location_t location)
27670 tree c;
27671 enum omp_clause_proc_bind_kind kind;
27673 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27674 return list;
27676 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27678 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27679 const char *p = IDENTIFIER_POINTER (id);
27681 if (strcmp ("master", p) == 0)
27682 kind = OMP_CLAUSE_PROC_BIND_MASTER;
27683 else if (strcmp ("close", p) == 0)
27684 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
27685 else if (strcmp ("spread", p) == 0)
27686 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
27687 else
27688 goto invalid_kind;
27690 else
27691 goto invalid_kind;
27693 cp_lexer_consume_token (parser->lexer);
27694 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27695 goto resync_fail;
27697 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
27698 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
27699 location);
27700 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
27701 OMP_CLAUSE_CHAIN (c) = list;
27702 return c;
27704 invalid_kind:
27705 cp_parser_error (parser, "invalid depend kind");
27706 resync_fail:
27707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27708 /*or_comma=*/false,
27709 /*consume_paren=*/true);
27710 return list;
27713 /* Parse all OpenMP clauses. The set clauses allowed by the directive
27714 is a bitmask in MASK. Return the list of clauses found; the result
27715 of clause default goes in *pdefault. */
27717 static tree
27718 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
27719 const char *where, cp_token *pragma_tok,
27720 bool finish_p = true)
27722 tree clauses = NULL;
27723 bool first = true;
27724 cp_token *token = NULL;
27726 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
27728 pragma_omp_clause c_kind;
27729 const char *c_name;
27730 tree prev = clauses;
27732 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27733 cp_lexer_consume_token (parser->lexer);
27735 token = cp_lexer_peek_token (parser->lexer);
27736 c_kind = cp_parser_omp_clause_name (parser);
27738 switch (c_kind)
27740 case PRAGMA_OMP_CLAUSE_COLLAPSE:
27741 clauses = cp_parser_omp_clause_collapse (parser, clauses,
27742 token->location);
27743 c_name = "collapse";
27744 break;
27745 case PRAGMA_OMP_CLAUSE_COPYIN:
27746 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
27747 c_name = "copyin";
27748 break;
27749 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
27750 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
27751 clauses);
27752 c_name = "copyprivate";
27753 break;
27754 case PRAGMA_OMP_CLAUSE_DEFAULT:
27755 clauses = cp_parser_omp_clause_default (parser, clauses,
27756 token->location);
27757 c_name = "default";
27758 break;
27759 case PRAGMA_OMP_CLAUSE_FINAL:
27760 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
27761 c_name = "final";
27762 break;
27763 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
27764 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
27765 clauses);
27766 c_name = "firstprivate";
27767 break;
27768 case PRAGMA_OMP_CLAUSE_IF:
27769 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
27770 c_name = "if";
27771 break;
27772 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
27773 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
27774 clauses);
27775 c_name = "lastprivate";
27776 break;
27777 case PRAGMA_OMP_CLAUSE_MERGEABLE:
27778 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
27779 token->location);
27780 c_name = "mergeable";
27781 break;
27782 case PRAGMA_OMP_CLAUSE_NOWAIT:
27783 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
27784 c_name = "nowait";
27785 break;
27786 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
27787 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
27788 token->location);
27789 c_name = "num_threads";
27790 break;
27791 case PRAGMA_OMP_CLAUSE_ORDERED:
27792 clauses = cp_parser_omp_clause_ordered (parser, clauses,
27793 token->location);
27794 c_name = "ordered";
27795 break;
27796 case PRAGMA_OMP_CLAUSE_PRIVATE:
27797 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
27798 clauses);
27799 c_name = "private";
27800 break;
27801 case PRAGMA_OMP_CLAUSE_REDUCTION:
27802 clauses = cp_parser_omp_clause_reduction (parser, clauses);
27803 c_name = "reduction";
27804 break;
27805 case PRAGMA_OMP_CLAUSE_SCHEDULE:
27806 clauses = cp_parser_omp_clause_schedule (parser, clauses,
27807 token->location);
27808 c_name = "schedule";
27809 break;
27810 case PRAGMA_OMP_CLAUSE_SHARED:
27811 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
27812 clauses);
27813 c_name = "shared";
27814 break;
27815 case PRAGMA_OMP_CLAUSE_UNTIED:
27816 clauses = cp_parser_omp_clause_untied (parser, clauses,
27817 token->location);
27818 c_name = "untied";
27819 break;
27820 case PRAGMA_OMP_CLAUSE_INBRANCH:
27821 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
27822 clauses, token->location);
27823 c_name = "inbranch";
27824 break;
27825 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
27826 clauses = cp_parser_omp_clause_branch (parser,
27827 OMP_CLAUSE_NOTINBRANCH,
27828 clauses, token->location);
27829 c_name = "notinbranch";
27830 break;
27831 case PRAGMA_OMP_CLAUSE_PARALLEL:
27832 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
27833 clauses, token->location);
27834 c_name = "parallel";
27835 if (!first)
27837 clause_not_first:
27838 error_at (token->location, "%qs must be the first clause of %qs",
27839 c_name, where);
27840 clauses = prev;
27842 break;
27843 case PRAGMA_OMP_CLAUSE_FOR:
27844 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
27845 clauses, token->location);
27846 c_name = "for";
27847 if (!first)
27848 goto clause_not_first;
27849 break;
27850 case PRAGMA_OMP_CLAUSE_SECTIONS:
27851 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
27852 clauses, token->location);
27853 c_name = "sections";
27854 if (!first)
27855 goto clause_not_first;
27856 break;
27857 case PRAGMA_OMP_CLAUSE_TASKGROUP:
27858 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
27859 clauses, token->location);
27860 c_name = "taskgroup";
27861 if (!first)
27862 goto clause_not_first;
27863 break;
27864 case PRAGMA_OMP_CLAUSE_TO:
27865 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
27866 clauses);
27867 c_name = "to";
27868 break;
27869 case PRAGMA_OMP_CLAUSE_FROM:
27870 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
27871 clauses);
27872 c_name = "from";
27873 break;
27874 case PRAGMA_OMP_CLAUSE_UNIFORM:
27875 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
27876 clauses);
27877 c_name = "uniform";
27878 break;
27879 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
27880 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
27881 token->location);
27882 c_name = "num_teams";
27883 break;
27884 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
27885 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
27886 token->location);
27887 c_name = "thread_limit";
27888 break;
27889 case PRAGMA_OMP_CLAUSE_ALIGNED:
27890 clauses = cp_parser_omp_clause_aligned (parser, clauses);
27891 c_name = "aligned";
27892 break;
27893 case PRAGMA_OMP_CLAUSE_LINEAR:
27894 clauses = cp_parser_omp_clause_linear (parser, clauses);
27895 c_name = "linear";
27896 break;
27897 case PRAGMA_OMP_CLAUSE_DEPEND:
27898 clauses = cp_parser_omp_clause_depend (parser, clauses);
27899 c_name = "depend";
27900 break;
27901 case PRAGMA_OMP_CLAUSE_MAP:
27902 clauses = cp_parser_omp_clause_map (parser, clauses);
27903 c_name = "map";
27904 break;
27905 case PRAGMA_OMP_CLAUSE_DEVICE:
27906 clauses = cp_parser_omp_clause_device (parser, clauses,
27907 token->location);
27908 c_name = "device";
27909 break;
27910 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
27911 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
27912 token->location);
27913 c_name = "dist_schedule";
27914 break;
27915 case PRAGMA_OMP_CLAUSE_PROC_BIND:
27916 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
27917 token->location);
27918 c_name = "proc_bind";
27919 break;
27920 case PRAGMA_OMP_CLAUSE_SAFELEN:
27921 clauses = cp_parser_omp_clause_safelen (parser, clauses,
27922 token->location);
27923 c_name = "safelen";
27924 break;
27925 case PRAGMA_OMP_CLAUSE_SIMDLEN:
27926 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
27927 token->location);
27928 c_name = "simdlen";
27929 break;
27930 default:
27931 cp_parser_error (parser, "expected %<#pragma omp%> clause");
27932 goto saw_error;
27935 first = false;
27937 if (((mask >> c_kind) & 1) == 0)
27939 /* Remove the invalid clause(s) from the list to avoid
27940 confusing the rest of the compiler. */
27941 clauses = prev;
27942 error_at (token->location, "%qs is not valid for %qs", c_name, where);
27945 saw_error:
27946 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27947 if (finish_p)
27948 return finish_omp_clauses (clauses);
27949 return clauses;
27952 /* OpenMP 2.5:
27953 structured-block:
27954 statement
27956 In practice, we're also interested in adding the statement to an
27957 outer node. So it is convenient if we work around the fact that
27958 cp_parser_statement calls add_stmt. */
27960 static unsigned
27961 cp_parser_begin_omp_structured_block (cp_parser *parser)
27963 unsigned save = parser->in_statement;
27965 /* Only move the values to IN_OMP_BLOCK if they weren't false.
27966 This preserves the "not within loop or switch" style error messages
27967 for nonsense cases like
27968 void foo() {
27969 #pragma omp single
27970 break;
27973 if (parser->in_statement)
27974 parser->in_statement = IN_OMP_BLOCK;
27976 return save;
27979 static void
27980 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
27982 parser->in_statement = save;
27985 static tree
27986 cp_parser_omp_structured_block (cp_parser *parser)
27988 tree stmt = begin_omp_structured_block ();
27989 unsigned int save = cp_parser_begin_omp_structured_block (parser);
27991 cp_parser_statement (parser, NULL_TREE, false, NULL);
27993 cp_parser_end_omp_structured_block (parser, save);
27994 return finish_omp_structured_block (stmt);
27997 /* OpenMP 2.5:
27998 # pragma omp atomic new-line
27999 expression-stmt
28001 expression-stmt:
28002 x binop= expr | x++ | ++x | x-- | --x
28003 binop:
28004 +, *, -, /, &, ^, |, <<, >>
28006 where x is an lvalue expression with scalar type.
28008 OpenMP 3.1:
28009 # pragma omp atomic new-line
28010 update-stmt
28012 # pragma omp atomic read new-line
28013 read-stmt
28015 # pragma omp atomic write new-line
28016 write-stmt
28018 # pragma omp atomic update new-line
28019 update-stmt
28021 # pragma omp atomic capture new-line
28022 capture-stmt
28024 # pragma omp atomic capture new-line
28025 capture-block
28027 read-stmt:
28028 v = x
28029 write-stmt:
28030 x = expr
28031 update-stmt:
28032 expression-stmt | x = x binop expr
28033 capture-stmt:
28034 v = expression-stmt
28035 capture-block:
28036 { v = x; update-stmt; } | { update-stmt; v = x; }
28038 OpenMP 4.0:
28039 update-stmt:
28040 expression-stmt | x = x binop expr | x = expr binop x
28041 capture-stmt:
28042 v = update-stmt
28043 capture-block:
28044 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28046 where x and v are lvalue expressions with scalar type. */
28048 static void
28049 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28051 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28052 tree rhs1 = NULL_TREE, orig_lhs;
28053 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28054 bool structured_block = false;
28055 bool seq_cst = false;
28057 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28059 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28060 const char *p = IDENTIFIER_POINTER (id);
28062 if (!strcmp (p, "read"))
28063 code = OMP_ATOMIC_READ;
28064 else if (!strcmp (p, "write"))
28065 code = NOP_EXPR;
28066 else if (!strcmp (p, "update"))
28067 code = OMP_ATOMIC;
28068 else if (!strcmp (p, "capture"))
28069 code = OMP_ATOMIC_CAPTURE_NEW;
28070 else
28071 p = NULL;
28072 if (p)
28073 cp_lexer_consume_token (parser->lexer);
28076 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28078 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28079 const char *p = IDENTIFIER_POINTER (id);
28081 if (!strcmp (p, "seq_cst"))
28083 seq_cst = true;
28084 cp_lexer_consume_token (parser->lexer);
28087 cp_parser_require_pragma_eol (parser, pragma_tok);
28089 switch (code)
28091 case OMP_ATOMIC_READ:
28092 case NOP_EXPR: /* atomic write */
28093 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28094 /*cast_p=*/false, NULL);
28095 if (v == error_mark_node)
28096 goto saw_error;
28097 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28098 goto saw_error;
28099 if (code == NOP_EXPR)
28100 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28101 else
28102 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28103 /*cast_p=*/false, NULL);
28104 if (lhs == error_mark_node)
28105 goto saw_error;
28106 if (code == NOP_EXPR)
28108 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28109 opcode. */
28110 code = OMP_ATOMIC;
28111 rhs = lhs;
28112 lhs = v;
28113 v = NULL_TREE;
28115 goto done;
28116 case OMP_ATOMIC_CAPTURE_NEW:
28117 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28119 cp_lexer_consume_token (parser->lexer);
28120 structured_block = true;
28122 else
28124 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28125 /*cast_p=*/false, NULL);
28126 if (v == error_mark_node)
28127 goto saw_error;
28128 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28129 goto saw_error;
28131 default:
28132 break;
28135 restart:
28136 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28137 /*cast_p=*/false, NULL);
28138 orig_lhs = lhs;
28139 switch (TREE_CODE (lhs))
28141 case ERROR_MARK:
28142 goto saw_error;
28144 case POSTINCREMENT_EXPR:
28145 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28146 code = OMP_ATOMIC_CAPTURE_OLD;
28147 /* FALLTHROUGH */
28148 case PREINCREMENT_EXPR:
28149 lhs = TREE_OPERAND (lhs, 0);
28150 opcode = PLUS_EXPR;
28151 rhs = integer_one_node;
28152 break;
28154 case POSTDECREMENT_EXPR:
28155 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28156 code = OMP_ATOMIC_CAPTURE_OLD;
28157 /* FALLTHROUGH */
28158 case PREDECREMENT_EXPR:
28159 lhs = TREE_OPERAND (lhs, 0);
28160 opcode = MINUS_EXPR;
28161 rhs = integer_one_node;
28162 break;
28164 case COMPOUND_EXPR:
28165 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28166 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28167 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28168 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28169 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28170 (TREE_OPERAND (lhs, 1), 0), 0)))
28171 == BOOLEAN_TYPE)
28172 /* Undo effects of boolean_increment for post {in,de}crement. */
28173 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28174 /* FALLTHRU */
28175 case MODIFY_EXPR:
28176 if (TREE_CODE (lhs) == MODIFY_EXPR
28177 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28179 /* Undo effects of boolean_increment. */
28180 if (integer_onep (TREE_OPERAND (lhs, 1)))
28182 /* This is pre or post increment. */
28183 rhs = TREE_OPERAND (lhs, 1);
28184 lhs = TREE_OPERAND (lhs, 0);
28185 opcode = NOP_EXPR;
28186 if (code == OMP_ATOMIC_CAPTURE_NEW
28187 && !structured_block
28188 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28189 code = OMP_ATOMIC_CAPTURE_OLD;
28190 break;
28193 /* FALLTHRU */
28194 default:
28195 switch (cp_lexer_peek_token (parser->lexer)->type)
28197 case CPP_MULT_EQ:
28198 opcode = MULT_EXPR;
28199 break;
28200 case CPP_DIV_EQ:
28201 opcode = TRUNC_DIV_EXPR;
28202 break;
28203 case CPP_PLUS_EQ:
28204 opcode = PLUS_EXPR;
28205 break;
28206 case CPP_MINUS_EQ:
28207 opcode = MINUS_EXPR;
28208 break;
28209 case CPP_LSHIFT_EQ:
28210 opcode = LSHIFT_EXPR;
28211 break;
28212 case CPP_RSHIFT_EQ:
28213 opcode = RSHIFT_EXPR;
28214 break;
28215 case CPP_AND_EQ:
28216 opcode = BIT_AND_EXPR;
28217 break;
28218 case CPP_OR_EQ:
28219 opcode = BIT_IOR_EXPR;
28220 break;
28221 case CPP_XOR_EQ:
28222 opcode = BIT_XOR_EXPR;
28223 break;
28224 case CPP_EQ:
28225 enum cp_parser_prec oprec;
28226 cp_token *token;
28227 cp_lexer_consume_token (parser->lexer);
28228 cp_parser_parse_tentatively (parser);
28229 rhs1 = cp_parser_simple_cast_expression (parser);
28230 if (rhs1 == error_mark_node)
28232 cp_parser_abort_tentative_parse (parser);
28233 cp_parser_simple_cast_expression (parser);
28234 goto saw_error;
28236 token = cp_lexer_peek_token (parser->lexer);
28237 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28239 cp_parser_abort_tentative_parse (parser);
28240 cp_parser_parse_tentatively (parser);
28241 rhs = cp_parser_binary_expression (parser, false, true,
28242 PREC_NOT_OPERATOR, NULL);
28243 if (rhs == error_mark_node)
28245 cp_parser_abort_tentative_parse (parser);
28246 cp_parser_binary_expression (parser, false, true,
28247 PREC_NOT_OPERATOR, NULL);
28248 goto saw_error;
28250 switch (TREE_CODE (rhs))
28252 case MULT_EXPR:
28253 case TRUNC_DIV_EXPR:
28254 case PLUS_EXPR:
28255 case MINUS_EXPR:
28256 case LSHIFT_EXPR:
28257 case RSHIFT_EXPR:
28258 case BIT_AND_EXPR:
28259 case BIT_IOR_EXPR:
28260 case BIT_XOR_EXPR:
28261 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28263 if (cp_parser_parse_definitely (parser))
28265 opcode = TREE_CODE (rhs);
28266 rhs1 = TREE_OPERAND (rhs, 0);
28267 rhs = TREE_OPERAND (rhs, 1);
28268 goto stmt_done;
28270 else
28271 goto saw_error;
28273 break;
28274 default:
28275 break;
28277 cp_parser_abort_tentative_parse (parser);
28278 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28280 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28281 if (rhs == error_mark_node)
28282 goto saw_error;
28283 opcode = NOP_EXPR;
28284 rhs1 = NULL_TREE;
28285 goto stmt_done;
28287 cp_parser_error (parser,
28288 "invalid form of %<#pragma omp atomic%>");
28289 goto saw_error;
28291 if (!cp_parser_parse_definitely (parser))
28292 goto saw_error;
28293 switch (token->type)
28295 case CPP_SEMICOLON:
28296 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28298 code = OMP_ATOMIC_CAPTURE_OLD;
28299 v = lhs;
28300 lhs = NULL_TREE;
28301 lhs1 = rhs1;
28302 rhs1 = NULL_TREE;
28303 cp_lexer_consume_token (parser->lexer);
28304 goto restart;
28306 else if (structured_block)
28308 opcode = NOP_EXPR;
28309 rhs = rhs1;
28310 rhs1 = NULL_TREE;
28311 goto stmt_done;
28313 cp_parser_error (parser,
28314 "invalid form of %<#pragma omp atomic%>");
28315 goto saw_error;
28316 case CPP_MULT:
28317 opcode = MULT_EXPR;
28318 break;
28319 case CPP_DIV:
28320 opcode = TRUNC_DIV_EXPR;
28321 break;
28322 case CPP_PLUS:
28323 opcode = PLUS_EXPR;
28324 break;
28325 case CPP_MINUS:
28326 opcode = MINUS_EXPR;
28327 break;
28328 case CPP_LSHIFT:
28329 opcode = LSHIFT_EXPR;
28330 break;
28331 case CPP_RSHIFT:
28332 opcode = RSHIFT_EXPR;
28333 break;
28334 case CPP_AND:
28335 opcode = BIT_AND_EXPR;
28336 break;
28337 case CPP_OR:
28338 opcode = BIT_IOR_EXPR;
28339 break;
28340 case CPP_XOR:
28341 opcode = BIT_XOR_EXPR;
28342 break;
28343 default:
28344 cp_parser_error (parser,
28345 "invalid operator for %<#pragma omp atomic%>");
28346 goto saw_error;
28348 oprec = TOKEN_PRECEDENCE (token);
28349 gcc_assert (oprec != PREC_NOT_OPERATOR);
28350 if (commutative_tree_code (opcode))
28351 oprec = (enum cp_parser_prec) (oprec - 1);
28352 cp_lexer_consume_token (parser->lexer);
28353 rhs = cp_parser_binary_expression (parser, false, false,
28354 oprec, NULL);
28355 if (rhs == error_mark_node)
28356 goto saw_error;
28357 goto stmt_done;
28358 /* FALLTHROUGH */
28359 default:
28360 cp_parser_error (parser,
28361 "invalid operator for %<#pragma omp atomic%>");
28362 goto saw_error;
28364 cp_lexer_consume_token (parser->lexer);
28366 rhs = cp_parser_expression (parser, false, NULL);
28367 if (rhs == error_mark_node)
28368 goto saw_error;
28369 break;
28371 stmt_done:
28372 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28374 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28375 goto saw_error;
28376 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28377 /*cast_p=*/false, NULL);
28378 if (v == error_mark_node)
28379 goto saw_error;
28380 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28381 goto saw_error;
28382 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28383 /*cast_p=*/false, NULL);
28384 if (lhs1 == error_mark_node)
28385 goto saw_error;
28387 if (structured_block)
28389 cp_parser_consume_semicolon_at_end_of_statement (parser);
28390 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28392 done:
28393 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28394 if (!structured_block)
28395 cp_parser_consume_semicolon_at_end_of_statement (parser);
28396 return;
28398 saw_error:
28399 cp_parser_skip_to_end_of_block_or_statement (parser);
28400 if (structured_block)
28402 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28403 cp_lexer_consume_token (parser->lexer);
28404 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28406 cp_parser_skip_to_end_of_block_or_statement (parser);
28407 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28408 cp_lexer_consume_token (parser->lexer);
28414 /* OpenMP 2.5:
28415 # pragma omp barrier new-line */
28417 static void
28418 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28420 cp_parser_require_pragma_eol (parser, pragma_tok);
28421 finish_omp_barrier ();
28424 /* OpenMP 2.5:
28425 # pragma omp critical [(name)] new-line
28426 structured-block */
28428 static tree
28429 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28431 tree stmt, name = NULL;
28433 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28435 cp_lexer_consume_token (parser->lexer);
28437 name = cp_parser_identifier (parser);
28439 if (name == error_mark_node
28440 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28441 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28442 /*or_comma=*/false,
28443 /*consume_paren=*/true);
28444 if (name == error_mark_node)
28445 name = NULL;
28447 cp_parser_require_pragma_eol (parser, pragma_tok);
28449 stmt = cp_parser_omp_structured_block (parser);
28450 return c_finish_omp_critical (input_location, stmt, name);
28453 /* OpenMP 2.5:
28454 # pragma omp flush flush-vars[opt] new-line
28456 flush-vars:
28457 ( variable-list ) */
28459 static void
28460 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28463 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28464 cp_parser_require_pragma_eol (parser, pragma_tok);
28466 finish_omp_flush ();
28469 /* Helper function, to parse omp for increment expression. */
28471 static tree
28472 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
28474 tree cond = cp_parser_binary_expression (parser, false, true,
28475 PREC_NOT_OPERATOR, NULL);
28476 if (cond == error_mark_node
28477 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28479 cp_parser_skip_to_end_of_statement (parser);
28480 return error_mark_node;
28483 switch (TREE_CODE (cond))
28485 case GT_EXPR:
28486 case GE_EXPR:
28487 case LT_EXPR:
28488 case LE_EXPR:
28489 break;
28490 default:
28491 return error_mark_node;
28494 /* If decl is an iterator, preserve LHS and RHS of the relational
28495 expr until finish_omp_for. */
28496 if (decl
28497 && (type_dependent_expression_p (decl)
28498 || CLASS_TYPE_P (TREE_TYPE (decl))))
28499 return cond;
28501 return build_x_binary_op (input_location, TREE_CODE (cond),
28502 TREE_OPERAND (cond, 0), ERROR_MARK,
28503 TREE_OPERAND (cond, 1), ERROR_MARK,
28504 /*overload=*/NULL, tf_warning_or_error);
28507 /* Helper function, to parse omp for increment expression. */
28509 static tree
28510 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
28512 cp_token *token = cp_lexer_peek_token (parser->lexer);
28513 enum tree_code op;
28514 tree lhs, rhs;
28515 cp_id_kind idk;
28516 bool decl_first;
28518 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28520 op = (token->type == CPP_PLUS_PLUS
28521 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
28522 cp_lexer_consume_token (parser->lexer);
28523 lhs = cp_parser_simple_cast_expression (parser);
28524 if (lhs != decl)
28525 return error_mark_node;
28526 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28529 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
28530 if (lhs != decl)
28531 return error_mark_node;
28533 token = cp_lexer_peek_token (parser->lexer);
28534 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28536 op = (token->type == CPP_PLUS_PLUS
28537 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
28538 cp_lexer_consume_token (parser->lexer);
28539 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28542 op = cp_parser_assignment_operator_opt (parser);
28543 if (op == ERROR_MARK)
28544 return error_mark_node;
28546 if (op != NOP_EXPR)
28548 rhs = cp_parser_assignment_expression (parser, false, NULL);
28549 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
28550 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28553 lhs = cp_parser_binary_expression (parser, false, false,
28554 PREC_ADDITIVE_EXPRESSION, NULL);
28555 token = cp_lexer_peek_token (parser->lexer);
28556 decl_first = lhs == decl;
28557 if (decl_first)
28558 lhs = NULL_TREE;
28559 if (token->type != CPP_PLUS
28560 && token->type != CPP_MINUS)
28561 return error_mark_node;
28565 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
28566 cp_lexer_consume_token (parser->lexer);
28567 rhs = cp_parser_binary_expression (parser, false, false,
28568 PREC_ADDITIVE_EXPRESSION, NULL);
28569 token = cp_lexer_peek_token (parser->lexer);
28570 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
28572 if (lhs == NULL_TREE)
28574 if (op == PLUS_EXPR)
28575 lhs = rhs;
28576 else
28577 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
28578 tf_warning_or_error);
28580 else
28581 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
28582 ERROR_MARK, NULL, tf_warning_or_error);
28585 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
28587 if (!decl_first)
28589 if (rhs != decl || op == MINUS_EXPR)
28590 return error_mark_node;
28591 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
28593 else
28594 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
28596 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28599 /* Parse the restricted form of the for statement allowed by OpenMP. */
28601 static tree
28602 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
28603 tree *cclauses)
28605 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
28606 tree real_decl, initv, condv, incrv, declv;
28607 tree this_pre_body, cl;
28608 location_t loc_first;
28609 bool collapse_err = false;
28610 int i, collapse = 1, nbraces = 0;
28611 vec<tree, va_gc> *for_block = make_tree_vector ();
28613 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
28614 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
28615 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
28617 gcc_assert (collapse >= 1);
28619 declv = make_tree_vec (collapse);
28620 initv = make_tree_vec (collapse);
28621 condv = make_tree_vec (collapse);
28622 incrv = make_tree_vec (collapse);
28624 loc_first = cp_lexer_peek_token (parser->lexer)->location;
28626 for (i = 0; i < collapse; i++)
28628 int bracecount = 0;
28629 bool add_private_clause = false;
28630 location_t loc;
28632 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28634 cp_parser_error (parser, "for statement expected");
28635 return NULL;
28637 loc = cp_lexer_consume_token (parser->lexer)->location;
28639 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28640 return NULL;
28642 init = decl = real_decl = NULL;
28643 this_pre_body = push_stmt_list ();
28644 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28646 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
28648 init-expr:
28649 var = lb
28650 integer-type var = lb
28651 random-access-iterator-type var = lb
28652 pointer-type var = lb
28654 cp_decl_specifier_seq type_specifiers;
28656 /* First, try to parse as an initialized declaration. See
28657 cp_parser_condition, from whence the bulk of this is copied. */
28659 cp_parser_parse_tentatively (parser);
28660 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
28661 /*is_trailing_return=*/false,
28662 &type_specifiers);
28663 if (cp_parser_parse_definitely (parser))
28665 /* If parsing a type specifier seq succeeded, then this
28666 MUST be a initialized declaration. */
28667 tree asm_specification, attributes;
28668 cp_declarator *declarator;
28670 declarator = cp_parser_declarator (parser,
28671 CP_PARSER_DECLARATOR_NAMED,
28672 /*ctor_dtor_or_conv_p=*/NULL,
28673 /*parenthesized_p=*/NULL,
28674 /*member_p=*/false);
28675 attributes = cp_parser_attributes_opt (parser);
28676 asm_specification = cp_parser_asm_specification_opt (parser);
28678 if (declarator == cp_error_declarator)
28679 cp_parser_skip_to_end_of_statement (parser);
28681 else
28683 tree pushed_scope, auto_node;
28685 decl = start_decl (declarator, &type_specifiers,
28686 SD_INITIALIZED, attributes,
28687 /*prefix_attributes=*/NULL_TREE,
28688 &pushed_scope);
28690 auto_node = type_uses_auto (TREE_TYPE (decl));
28691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28693 if (cp_lexer_next_token_is (parser->lexer,
28694 CPP_OPEN_PAREN))
28695 error ("parenthesized initialization is not allowed in "
28696 "OpenMP %<for%> loop");
28697 else
28698 /* Trigger an error. */
28699 cp_parser_require (parser, CPP_EQ, RT_EQ);
28701 init = error_mark_node;
28702 cp_parser_skip_to_end_of_statement (parser);
28704 else if (CLASS_TYPE_P (TREE_TYPE (decl))
28705 || type_dependent_expression_p (decl)
28706 || auto_node)
28708 bool is_direct_init, is_non_constant_init;
28710 init = cp_parser_initializer (parser,
28711 &is_direct_init,
28712 &is_non_constant_init);
28714 if (auto_node)
28716 TREE_TYPE (decl)
28717 = do_auto_deduction (TREE_TYPE (decl), init,
28718 auto_node);
28720 if (!CLASS_TYPE_P (TREE_TYPE (decl))
28721 && !type_dependent_expression_p (decl))
28722 goto non_class;
28725 cp_finish_decl (decl, init, !is_non_constant_init,
28726 asm_specification,
28727 LOOKUP_ONLYCONVERTING);
28728 if (CLASS_TYPE_P (TREE_TYPE (decl)))
28730 vec_safe_push (for_block, this_pre_body);
28731 init = NULL_TREE;
28733 else
28734 init = pop_stmt_list (this_pre_body);
28735 this_pre_body = NULL_TREE;
28737 else
28739 /* Consume '='. */
28740 cp_lexer_consume_token (parser->lexer);
28741 init = cp_parser_assignment_expression (parser, false, NULL);
28743 non_class:
28744 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
28745 init = error_mark_node;
28746 else
28747 cp_finish_decl (decl, NULL_TREE,
28748 /*init_const_expr_p=*/false,
28749 asm_specification,
28750 LOOKUP_ONLYCONVERTING);
28753 if (pushed_scope)
28754 pop_scope (pushed_scope);
28757 else
28759 cp_id_kind idk;
28760 /* If parsing a type specifier sequence failed, then
28761 this MUST be a simple expression. */
28762 cp_parser_parse_tentatively (parser);
28763 decl = cp_parser_primary_expression (parser, false, false,
28764 false, &idk);
28765 if (!cp_parser_error_occurred (parser)
28766 && decl
28767 && DECL_P (decl)
28768 && CLASS_TYPE_P (TREE_TYPE (decl)))
28770 tree rhs;
28772 cp_parser_parse_definitely (parser);
28773 cp_parser_require (parser, CPP_EQ, RT_EQ);
28774 rhs = cp_parser_assignment_expression (parser, false, NULL);
28775 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
28776 decl, NOP_EXPR,
28777 rhs,
28778 tf_warning_or_error));
28779 add_private_clause = true;
28781 else
28783 decl = NULL;
28784 cp_parser_abort_tentative_parse (parser);
28785 init = cp_parser_expression (parser, false, NULL);
28786 if (init)
28788 if (TREE_CODE (init) == MODIFY_EXPR
28789 || TREE_CODE (init) == MODOP_EXPR)
28790 real_decl = TREE_OPERAND (init, 0);
28795 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28796 if (this_pre_body)
28798 this_pre_body = pop_stmt_list (this_pre_body);
28799 if (pre_body)
28801 tree t = pre_body;
28802 pre_body = push_stmt_list ();
28803 add_stmt (t);
28804 add_stmt (this_pre_body);
28805 pre_body = pop_stmt_list (pre_body);
28807 else
28808 pre_body = this_pre_body;
28811 if (decl)
28812 real_decl = decl;
28813 if (cclauses != NULL
28814 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
28815 && real_decl != NULL_TREE)
28817 tree *c;
28818 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
28819 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
28820 && OMP_CLAUSE_DECL (*c) == real_decl)
28822 error_at (loc, "iteration variable %qD"
28823 " should not be firstprivate", real_decl);
28824 *c = OMP_CLAUSE_CHAIN (*c);
28826 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
28827 && OMP_CLAUSE_DECL (*c) == real_decl)
28829 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
28830 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
28831 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
28832 OMP_CLAUSE_DECL (l) = real_decl;
28833 OMP_CLAUSE_CHAIN (l) = clauses;
28834 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
28835 clauses = l;
28836 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
28837 CP_OMP_CLAUSE_INFO (*c) = NULL;
28838 add_private_clause = false;
28840 else
28842 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
28843 && OMP_CLAUSE_DECL (*c) == real_decl)
28844 add_private_clause = false;
28845 c = &OMP_CLAUSE_CHAIN (*c);
28849 if (add_private_clause)
28851 tree c;
28852 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
28854 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
28855 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
28856 && OMP_CLAUSE_DECL (c) == decl)
28857 break;
28858 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
28859 && OMP_CLAUSE_DECL (c) == decl)
28860 error_at (loc, "iteration variable %qD "
28861 "should not be firstprivate",
28862 decl);
28863 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
28864 && OMP_CLAUSE_DECL (c) == decl)
28865 error_at (loc, "iteration variable %qD should not be reduction",
28866 decl);
28868 if (c == NULL)
28870 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
28871 OMP_CLAUSE_DECL (c) = decl;
28872 c = finish_omp_clauses (c);
28873 if (c)
28875 OMP_CLAUSE_CHAIN (c) = clauses;
28876 clauses = c;
28881 cond = NULL;
28882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28883 cond = cp_parser_omp_for_cond (parser, decl);
28884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28886 incr = NULL;
28887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28889 /* If decl is an iterator, preserve the operator on decl
28890 until finish_omp_for. */
28891 if (real_decl
28892 && ((processing_template_decl
28893 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
28894 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
28895 incr = cp_parser_omp_for_incr (parser, real_decl);
28896 else
28897 incr = cp_parser_expression (parser, false, NULL);
28898 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
28899 SET_EXPR_LOCATION (incr, input_location);
28902 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28903 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28904 /*or_comma=*/false,
28905 /*consume_paren=*/true);
28907 TREE_VEC_ELT (declv, i) = decl;
28908 TREE_VEC_ELT (initv, i) = init;
28909 TREE_VEC_ELT (condv, i) = cond;
28910 TREE_VEC_ELT (incrv, i) = incr;
28912 if (i == collapse - 1)
28913 break;
28915 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
28916 in between the collapsed for loops to be still considered perfectly
28917 nested. Hopefully the final version clarifies this.
28918 For now handle (multiple) {'s and empty statements. */
28919 cp_parser_parse_tentatively (parser);
28922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28923 break;
28924 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28926 cp_lexer_consume_token (parser->lexer);
28927 bracecount++;
28929 else if (bracecount
28930 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28931 cp_lexer_consume_token (parser->lexer);
28932 else
28934 loc = cp_lexer_peek_token (parser->lexer)->location;
28935 error_at (loc, "not enough collapsed for loops");
28936 collapse_err = true;
28937 cp_parser_abort_tentative_parse (parser);
28938 declv = NULL_TREE;
28939 break;
28942 while (1);
28944 if (declv)
28946 cp_parser_parse_definitely (parser);
28947 nbraces += bracecount;
28951 /* Note that we saved the original contents of this flag when we entered
28952 the structured block, and so we don't need to re-save it here. */
28953 parser->in_statement = IN_OMP_FOR;
28955 /* Note that the grammar doesn't call for a structured block here,
28956 though the loop as a whole is a structured block. */
28957 body = push_stmt_list ();
28958 cp_parser_statement (parser, NULL_TREE, false, NULL);
28959 body = pop_stmt_list (body);
28961 if (declv == NULL_TREE)
28962 ret = NULL_TREE;
28963 else
28964 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
28965 pre_body, clauses);
28967 while (nbraces)
28969 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28971 cp_lexer_consume_token (parser->lexer);
28972 nbraces--;
28974 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28975 cp_lexer_consume_token (parser->lexer);
28976 else
28978 if (!collapse_err)
28980 error_at (cp_lexer_peek_token (parser->lexer)->location,
28981 "collapsed loops not perfectly nested");
28983 collapse_err = true;
28984 cp_parser_statement_seq_opt (parser, NULL);
28985 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28986 break;
28990 while (!for_block->is_empty ())
28991 add_stmt (pop_stmt_list (for_block->pop ()));
28992 release_tree_vector (for_block);
28994 return ret;
28997 /* Helper function for OpenMP parsing, split clauses and call
28998 finish_omp_clauses on each of the set of clauses afterwards. */
29000 static void
29001 cp_omp_split_clauses (location_t loc, enum tree_code code,
29002 omp_clause_mask mask, tree clauses, tree *cclauses)
29004 int i;
29005 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29006 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29007 if (cclauses[i])
29008 cclauses[i] = finish_omp_clauses (cclauses[i]);
29011 /* OpenMP 4.0:
29012 #pragma omp simd simd-clause[optseq] new-line
29013 for-loop */
29015 #define OMP_SIMD_CLAUSE_MASK \
29016 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29024 static tree
29025 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29026 char *p_name, omp_clause_mask mask, tree *cclauses)
29028 tree clauses, sb, ret;
29029 unsigned int save;
29030 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29032 strcat (p_name, " simd");
29033 mask |= OMP_SIMD_CLAUSE_MASK;
29034 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29036 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29037 cclauses == NULL);
29038 if (cclauses)
29040 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29041 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29044 sb = begin_omp_structured_block ();
29045 save = cp_parser_begin_omp_structured_block (parser);
29047 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29049 cp_parser_end_omp_structured_block (parser, save);
29050 add_stmt (finish_omp_structured_block (sb));
29052 return ret;
29055 /* OpenMP 2.5:
29056 #pragma omp for for-clause[optseq] new-line
29057 for-loop
29059 OpenMP 4.0:
29060 #pragma omp for simd for-simd-clause[optseq] new-line
29061 for-loop */
29063 #define OMP_FOR_CLAUSE_MASK \
29064 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29073 static tree
29074 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29075 char *p_name, omp_clause_mask mask, tree *cclauses)
29077 tree clauses, sb, ret;
29078 unsigned int save;
29079 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29081 strcat (p_name, " for");
29082 mask |= OMP_FOR_CLAUSE_MASK;
29083 if (cclauses)
29084 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29089 const char *p = IDENTIFIER_POINTER (id);
29091 if (strcmp (p, "simd") == 0)
29093 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29094 if (cclauses == NULL)
29095 cclauses = cclauses_buf;
29097 cp_lexer_consume_token (parser->lexer);
29098 sb = begin_omp_structured_block ();
29099 save = cp_parser_begin_omp_structured_block (parser);
29100 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29101 cclauses);
29102 cp_parser_end_omp_structured_block (parser, save);
29103 tree body = finish_omp_structured_block (sb);
29104 if (ret == NULL)
29105 return ret;
29106 ret = make_node (OMP_FOR);
29107 TREE_TYPE (ret) = void_type_node;
29108 OMP_FOR_BODY (ret) = body;
29109 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29110 SET_EXPR_LOCATION (ret, loc);
29111 add_stmt (ret);
29112 return ret;
29116 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29117 cclauses == NULL);
29118 if (cclauses)
29120 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29121 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29124 sb = begin_omp_structured_block ();
29125 save = cp_parser_begin_omp_structured_block (parser);
29127 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29129 cp_parser_end_omp_structured_block (parser, save);
29130 add_stmt (finish_omp_structured_block (sb));
29132 return ret;
29135 /* OpenMP 2.5:
29136 # pragma omp master new-line
29137 structured-block */
29139 static tree
29140 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29142 cp_parser_require_pragma_eol (parser, pragma_tok);
29143 return c_finish_omp_master (input_location,
29144 cp_parser_omp_structured_block (parser));
29147 /* OpenMP 2.5:
29148 # pragma omp ordered new-line
29149 structured-block */
29151 static tree
29152 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29154 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29155 cp_parser_require_pragma_eol (parser, pragma_tok);
29156 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29159 /* OpenMP 2.5:
29161 section-scope:
29162 { section-sequence }
29164 section-sequence:
29165 section-directive[opt] structured-block
29166 section-sequence section-directive structured-block */
29168 static tree
29169 cp_parser_omp_sections_scope (cp_parser *parser)
29171 tree stmt, substmt;
29172 bool error_suppress = false;
29173 cp_token *tok;
29175 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29176 return NULL_TREE;
29178 stmt = push_stmt_list ();
29180 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29182 substmt = cp_parser_omp_structured_block (parser);
29183 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29184 add_stmt (substmt);
29187 while (1)
29189 tok = cp_lexer_peek_token (parser->lexer);
29190 if (tok->type == CPP_CLOSE_BRACE)
29191 break;
29192 if (tok->type == CPP_EOF)
29193 break;
29195 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29197 cp_lexer_consume_token (parser->lexer);
29198 cp_parser_require_pragma_eol (parser, tok);
29199 error_suppress = false;
29201 else if (!error_suppress)
29203 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29204 error_suppress = true;
29207 substmt = cp_parser_omp_structured_block (parser);
29208 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29209 add_stmt (substmt);
29211 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29213 substmt = pop_stmt_list (stmt);
29215 stmt = make_node (OMP_SECTIONS);
29216 TREE_TYPE (stmt) = void_type_node;
29217 OMP_SECTIONS_BODY (stmt) = substmt;
29219 add_stmt (stmt);
29220 return stmt;
29223 /* OpenMP 2.5:
29224 # pragma omp sections sections-clause[optseq] newline
29225 sections-scope */
29227 #define OMP_SECTIONS_CLAUSE_MASK \
29228 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29234 static tree
29235 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29236 char *p_name, omp_clause_mask mask, tree *cclauses)
29238 tree clauses, ret;
29239 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29241 strcat (p_name, " sections");
29242 mask |= OMP_SECTIONS_CLAUSE_MASK;
29243 if (cclauses)
29244 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29246 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29247 cclauses == NULL);
29248 if (cclauses)
29250 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29251 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29254 ret = cp_parser_omp_sections_scope (parser);
29255 if (ret)
29256 OMP_SECTIONS_CLAUSES (ret) = clauses;
29258 return ret;
29261 /* OpenMP 2.5:
29262 # pragma parallel parallel-clause new-line
29263 # pragma parallel for parallel-for-clause new-line
29264 # pragma parallel sections parallel-sections-clause new-line
29266 OpenMP 4.0:
29267 # pragma parallel for simd parallel-for-simd-clause new-line */
29269 #define OMP_PARALLEL_CLAUSE_MASK \
29270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29280 static tree
29281 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29282 char *p_name, omp_clause_mask mask, tree *cclauses)
29284 tree stmt, clauses, block;
29285 unsigned int save;
29286 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29288 strcat (p_name, " parallel");
29289 mask |= OMP_PARALLEL_CLAUSE_MASK;
29291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29293 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29294 if (cclauses == NULL)
29295 cclauses = cclauses_buf;
29297 cp_lexer_consume_token (parser->lexer);
29298 block = begin_omp_parallel ();
29299 save = cp_parser_begin_omp_structured_block (parser);
29300 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29301 cp_parser_end_omp_structured_block (parser, save);
29302 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29303 block);
29304 OMP_PARALLEL_COMBINED (stmt) = 1;
29305 return stmt;
29307 else if (cclauses)
29309 error_at (loc, "expected %<for%> after %qs", p_name);
29310 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29311 return NULL_TREE;
29313 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29315 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29316 const char *p = IDENTIFIER_POINTER (id);
29317 if (strcmp (p, "sections") == 0)
29319 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29320 cclauses = cclauses_buf;
29322 cp_lexer_consume_token (parser->lexer);
29323 block = begin_omp_parallel ();
29324 save = cp_parser_begin_omp_structured_block (parser);
29325 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29326 cp_parser_end_omp_structured_block (parser, save);
29327 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29328 block);
29329 OMP_PARALLEL_COMBINED (stmt) = 1;
29330 return stmt;
29334 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29336 block = begin_omp_parallel ();
29337 save = cp_parser_begin_omp_structured_block (parser);
29338 cp_parser_statement (parser, NULL_TREE, false, NULL);
29339 cp_parser_end_omp_structured_block (parser, save);
29340 stmt = finish_omp_parallel (clauses, block);
29341 return stmt;
29344 /* OpenMP 2.5:
29345 # pragma omp single single-clause[optseq] new-line
29346 structured-block */
29348 #define OMP_SINGLE_CLAUSE_MASK \
29349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29354 static tree
29355 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29357 tree stmt = make_node (OMP_SINGLE);
29358 TREE_TYPE (stmt) = void_type_node;
29360 OMP_SINGLE_CLAUSES (stmt)
29361 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29362 "#pragma omp single", pragma_tok);
29363 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29365 return add_stmt (stmt);
29368 /* OpenMP 3.0:
29369 # pragma omp task task-clause[optseq] new-line
29370 structured-block */
29372 #define OMP_TASK_CLAUSE_MASK \
29373 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29383 static tree
29384 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29386 tree clauses, block;
29387 unsigned int save;
29389 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29390 "#pragma omp task", pragma_tok);
29391 block = begin_omp_task ();
29392 save = cp_parser_begin_omp_structured_block (parser);
29393 cp_parser_statement (parser, NULL_TREE, false, NULL);
29394 cp_parser_end_omp_structured_block (parser, save);
29395 return finish_omp_task (clauses, block);
29398 /* OpenMP 3.0:
29399 # pragma omp taskwait new-line */
29401 static void
29402 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29404 cp_parser_require_pragma_eol (parser, pragma_tok);
29405 finish_omp_taskwait ();
29408 /* OpenMP 3.1:
29409 # pragma omp taskyield new-line */
29411 static void
29412 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29414 cp_parser_require_pragma_eol (parser, pragma_tok);
29415 finish_omp_taskyield ();
29418 /* OpenMP 4.0:
29419 # pragma omp taskgroup new-line
29420 structured-block */
29422 static tree
29423 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29425 cp_parser_require_pragma_eol (parser, pragma_tok);
29426 return c_finish_omp_taskgroup (input_location,
29427 cp_parser_omp_structured_block (parser));
29431 /* OpenMP 2.5:
29432 # pragma omp threadprivate (variable-list) */
29434 static void
29435 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
29437 tree vars;
29439 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29440 cp_parser_require_pragma_eol (parser, pragma_tok);
29442 finish_omp_threadprivate (vars);
29445 /* OpenMP 4.0:
29446 # pragma omp cancel cancel-clause[optseq] new-line */
29448 #define OMP_CANCEL_CLAUSE_MASK \
29449 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29455 static void
29456 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
29458 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
29459 "#pragma omp cancel", pragma_tok);
29460 finish_omp_cancel (clauses);
29463 /* OpenMP 4.0:
29464 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
29466 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
29467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
29472 static void
29473 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
29475 tree clauses;
29476 bool point_seen = false;
29478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29480 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29481 const char *p = IDENTIFIER_POINTER (id);
29483 if (strcmp (p, "point") == 0)
29485 cp_lexer_consume_token (parser->lexer);
29486 point_seen = true;
29489 if (!point_seen)
29491 cp_parser_error (parser, "expected %<point%>");
29492 cp_parser_require_pragma_eol (parser, pragma_tok);
29493 return;
29496 clauses = cp_parser_omp_all_clauses (parser,
29497 OMP_CANCELLATION_POINT_CLAUSE_MASK,
29498 "#pragma omp cancellation point",
29499 pragma_tok);
29500 finish_omp_cancellation_point (clauses);
29503 /* OpenMP 4.0:
29504 #pragma omp distribute distribute-clause[optseq] new-line
29505 for-loop */
29507 #define OMP_DISTRIBUTE_CLAUSE_MASK \
29508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
29511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29513 static tree
29514 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
29515 char *p_name, omp_clause_mask mask, tree *cclauses)
29517 tree clauses, sb, ret;
29518 unsigned int save;
29519 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29521 strcat (p_name, " distribute");
29522 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
29524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29526 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29527 const char *p = IDENTIFIER_POINTER (id);
29528 bool simd = false;
29529 bool parallel = false;
29531 if (strcmp (p, "simd") == 0)
29532 simd = true;
29533 else
29534 parallel = strcmp (p, "parallel") == 0;
29535 if (parallel || simd)
29537 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29538 if (cclauses == NULL)
29539 cclauses = cclauses_buf;
29540 cp_lexer_consume_token (parser->lexer);
29541 sb = begin_omp_structured_block ();
29542 save = cp_parser_begin_omp_structured_block (parser);
29543 if (simd)
29544 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29545 cclauses);
29546 else
29547 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
29548 cclauses);
29549 cp_parser_end_omp_structured_block (parser, save);
29550 tree body = finish_omp_structured_block (sb);
29551 if (ret == NULL)
29552 return ret;
29553 ret = make_node (OMP_DISTRIBUTE);
29554 TREE_TYPE (ret) = void_type_node;
29555 OMP_FOR_BODY (ret) = body;
29556 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
29557 SET_EXPR_LOCATION (ret, loc);
29558 add_stmt (ret);
29559 return ret;
29563 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29564 cclauses == NULL);
29565 if (cclauses)
29567 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
29568 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
29571 sb = begin_omp_structured_block ();
29572 save = cp_parser_begin_omp_structured_block (parser);
29574 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
29576 cp_parser_end_omp_structured_block (parser, save);
29577 add_stmt (finish_omp_structured_block (sb));
29579 return ret;
29582 /* OpenMP 4.0:
29583 # pragma omp teams teams-clause[optseq] new-line
29584 structured-block */
29586 #define OMP_TEAMS_CLAUSE_MASK \
29587 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
29592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
29593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
29595 static tree
29596 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
29597 char *p_name, omp_clause_mask mask, tree *cclauses)
29599 tree clauses, sb, ret;
29600 unsigned int save;
29601 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29603 strcat (p_name, " teams");
29604 mask |= OMP_TEAMS_CLAUSE_MASK;
29606 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29608 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29609 const char *p = IDENTIFIER_POINTER (id);
29610 if (strcmp (p, "distribute") == 0)
29612 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29613 if (cclauses == NULL)
29614 cclauses = cclauses_buf;
29616 cp_lexer_consume_token (parser->lexer);
29617 sb = begin_omp_structured_block ();
29618 save = cp_parser_begin_omp_structured_block (parser);
29619 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
29620 cclauses);
29621 cp_parser_end_omp_structured_block (parser, save);
29622 tree body = finish_omp_structured_block (sb);
29623 if (ret == NULL)
29624 return ret;
29625 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
29626 ret = make_node (OMP_TEAMS);
29627 TREE_TYPE (ret) = void_type_node;
29628 OMP_TEAMS_CLAUSES (ret) = clauses;
29629 OMP_TEAMS_BODY (ret) = body;
29630 return add_stmt (ret);
29634 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29635 cclauses == NULL);
29636 if (cclauses)
29638 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
29639 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
29642 tree stmt = make_node (OMP_TEAMS);
29643 TREE_TYPE (stmt) = void_type_node;
29644 OMP_TEAMS_CLAUSES (stmt) = clauses;
29645 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
29647 return add_stmt (stmt);
29650 /* OpenMP 4.0:
29651 # pragma omp target data target-data-clause[optseq] new-line
29652 structured-block */
29654 #define OMP_TARGET_DATA_CLAUSE_MASK \
29655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29659 static tree
29660 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
29662 tree stmt = make_node (OMP_TARGET_DATA);
29663 TREE_TYPE (stmt) = void_type_node;
29665 OMP_TARGET_DATA_CLAUSES (stmt)
29666 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
29667 "#pragma omp target data", pragma_tok);
29668 keep_next_level (true);
29669 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
29671 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29672 return add_stmt (stmt);
29675 /* OpenMP 4.0:
29676 # pragma omp target update target-update-clause[optseq] new-line */
29678 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
29679 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
29680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
29681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29684 static bool
29685 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
29686 enum pragma_context context)
29688 if (context == pragma_stmt)
29690 error_at (pragma_tok->location,
29691 "%<#pragma omp target update%> may only be "
29692 "used in compound statements");
29693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29694 return false;
29697 tree clauses
29698 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
29699 "#pragma omp target update", pragma_tok);
29700 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
29701 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
29703 error_at (pragma_tok->location,
29704 "%<#pragma omp target update must contain at least one "
29705 "%<from%> or %<to%> clauses");
29706 return false;
29709 tree stmt = make_node (OMP_TARGET_UPDATE);
29710 TREE_TYPE (stmt) = void_type_node;
29711 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
29712 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29713 add_stmt (stmt);
29714 return false;
29717 /* OpenMP 4.0:
29718 # pragma omp target target-clause[optseq] new-line
29719 structured-block */
29721 #define OMP_TARGET_CLAUSE_MASK \
29722 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29726 static bool
29727 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
29728 enum pragma_context context)
29730 if (context != pragma_stmt && context != pragma_compound)
29732 cp_parser_error (parser, "expected declaration specifiers");
29733 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29734 return false;
29737 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29739 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29740 const char *p = IDENTIFIER_POINTER (id);
29742 if (strcmp (p, "data") == 0)
29744 cp_lexer_consume_token (parser->lexer);
29745 cp_parser_omp_target_data (parser, pragma_tok);
29746 return true;
29748 else if (strcmp (p, "update") == 0)
29750 cp_lexer_consume_token (parser->lexer);
29751 return cp_parser_omp_target_update (parser, pragma_tok, context);
29753 else if (strcmp (p, "teams") == 0)
29755 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
29756 char p_name[sizeof ("#pragma omp target teams distribute "
29757 "parallel for simd")];
29759 cp_lexer_consume_token (parser->lexer);
29760 strcpy (p_name, "#pragma omp target");
29761 keep_next_level (true);
29762 tree sb = begin_omp_structured_block ();
29763 unsigned save = cp_parser_begin_omp_structured_block (parser);
29764 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
29765 OMP_TARGET_CLAUSE_MASK, cclauses);
29766 cp_parser_end_omp_structured_block (parser, save);
29767 tree body = finish_omp_structured_block (sb);
29768 if (ret == NULL)
29769 return ret;
29770 tree stmt = make_node (OMP_TARGET);
29771 TREE_TYPE (stmt) = void_type_node;
29772 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
29773 OMP_TARGET_BODY (stmt) = body;
29774 add_stmt (stmt);
29775 return true;
29779 tree stmt = make_node (OMP_TARGET);
29780 TREE_TYPE (stmt) = void_type_node;
29782 OMP_TARGET_CLAUSES (stmt)
29783 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
29784 "#pragma omp target", pragma_tok);
29785 keep_next_level (true);
29786 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
29788 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29789 add_stmt (stmt);
29790 return true;
29793 /* OpenMP 4.0:
29794 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
29796 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
29797 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
29798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
29801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
29802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
29804 static void
29805 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
29806 enum pragma_context context)
29808 bool first_p = parser->omp_declare_simd == NULL;
29809 cp_omp_declare_simd_data data;
29810 if (first_p)
29812 data.error_seen = false;
29813 data.fndecl_seen = false;
29814 data.tokens = vNULL;
29815 parser->omp_declare_simd = &data;
29817 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
29818 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
29819 cp_lexer_consume_token (parser->lexer);
29820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29821 parser->omp_declare_simd->error_seen = true;
29822 cp_parser_require_pragma_eol (parser, pragma_tok);
29823 struct cp_token_cache *cp
29824 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
29825 parser->omp_declare_simd->tokens.safe_push (cp);
29826 if (first_p)
29828 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
29829 cp_parser_pragma (parser, context);
29830 switch (context)
29832 case pragma_external:
29833 cp_parser_declaration (parser);
29834 break;
29835 case pragma_member:
29836 cp_parser_member_declaration (parser);
29837 break;
29838 case pragma_objc_icode:
29839 cp_parser_block_declaration (parser, /*statement_p=*/false);
29840 break;
29841 default:
29842 cp_parser_declaration_statement (parser);
29843 break;
29845 if (parser->omp_declare_simd
29846 && !parser->omp_declare_simd->error_seen
29847 && !parser->omp_declare_simd->fndecl_seen)
29848 error_at (pragma_tok->location,
29849 "%<#pragma omp declare simd%> not immediately followed by "
29850 "function declaration or definition");
29851 data.tokens.release ();
29852 parser->omp_declare_simd = NULL;
29856 /* Finalize #pragma omp declare simd clauses after direct declarator has
29857 been parsed, and put that into "omp declare simd" attribute. */
29859 static tree
29860 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
29862 struct cp_token_cache *ce;
29863 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
29864 int i;
29866 if (!data->error_seen && data->fndecl_seen)
29868 error ("%<#pragma omp declare simd%> not immediately followed by "
29869 "a single function declaration or definition");
29870 data->error_seen = true;
29871 return attrs;
29873 if (data->error_seen)
29874 return attrs;
29876 FOR_EACH_VEC_ELT (data->tokens, i, ce)
29878 tree c, cl;
29880 cp_parser_push_lexer_for_tokens (parser, ce);
29881 parser->lexer->in_pragma = true;
29882 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
29883 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
29884 cp_lexer_consume_token (parser->lexer);
29885 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
29886 "#pragma omp declare simd", pragma_tok);
29887 cp_parser_pop_lexer (parser);
29888 if (cl)
29889 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
29890 c = build_tree_list (get_identifier ("omp declare simd"), cl);
29891 TREE_CHAIN (c) = attrs;
29892 if (processing_template_decl)
29893 ATTR_IS_DEPENDENT (c) = 1;
29894 attrs = c;
29897 data->fndecl_seen = true;
29898 return attrs;
29902 /* OpenMP 4.0:
29903 # pragma omp declare target new-line
29904 declarations and definitions
29905 # pragma omp end declare target new-line */
29907 static void
29908 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
29910 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29911 scope_chain->omp_declare_target_attribute++;
29914 static void
29915 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
29917 const char *p = "";
29918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29921 p = IDENTIFIER_POINTER (id);
29923 if (strcmp (p, "declare") == 0)
29925 cp_lexer_consume_token (parser->lexer);
29926 p = "";
29927 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29929 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29930 p = IDENTIFIER_POINTER (id);
29932 if (strcmp (p, "target") == 0)
29933 cp_lexer_consume_token (parser->lexer);
29934 else
29936 cp_parser_error (parser, "expected %<target%>");
29937 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29938 return;
29941 else
29943 cp_parser_error (parser, "expected %<declare%>");
29944 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29945 return;
29947 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29948 if (!scope_chain->omp_declare_target_attribute)
29949 error_at (pragma_tok->location,
29950 "%<#pragma omp end declare target%> without corresponding "
29951 "%<#pragma omp declare target%>");
29952 else
29953 scope_chain->omp_declare_target_attribute--;
29956 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
29957 expression and optional initializer clause of
29958 #pragma omp declare reduction. We store the expression(s) as
29959 either 3, 6 or 7 special statements inside of the artificial function's
29960 body. The first two statements are DECL_EXPRs for the artificial
29961 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
29962 expression that uses those variables.
29963 If there was any INITIALIZER clause, this is followed by further statements,
29964 the fourth and fifth statements are DECL_EXPRs for the artificial
29965 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
29966 constructor variant (first token after open paren is not omp_priv),
29967 then the sixth statement is a statement with the function call expression
29968 that uses the OMP_PRIV and optionally OMP_ORIG variable.
29969 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
29970 to initialize the OMP_PRIV artificial variable and there is seventh
29971 statement, a DECL_EXPR of the OMP_PRIV statement again. */
29973 static bool
29974 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
29976 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
29977 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
29978 type = TREE_TYPE (type);
29979 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
29980 DECL_ARTIFICIAL (omp_out) = 1;
29981 pushdecl (omp_out);
29982 add_decl_expr (omp_out);
29983 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
29984 DECL_ARTIFICIAL (omp_in) = 1;
29985 pushdecl (omp_in);
29986 add_decl_expr (omp_in);
29987 tree combiner;
29988 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
29990 keep_next_level (true);
29991 tree block = begin_omp_structured_block ();
29992 combiner = cp_parser_expression (parser, false, NULL);
29993 finish_expr_stmt (combiner);
29994 block = finish_omp_structured_block (block);
29995 add_stmt (block);
29997 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29998 return false;
30000 const char *p = "";
30001 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30003 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30004 p = IDENTIFIER_POINTER (id);
30007 if (strcmp (p, "initializer") == 0)
30009 cp_lexer_consume_token (parser->lexer);
30010 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30011 return false;
30013 p = "";
30014 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30016 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30017 p = IDENTIFIER_POINTER (id);
30020 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30021 DECL_ARTIFICIAL (omp_priv) = 1;
30022 pushdecl (omp_priv);
30023 add_decl_expr (omp_priv);
30024 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30025 DECL_ARTIFICIAL (omp_orig) = 1;
30026 pushdecl (omp_orig);
30027 add_decl_expr (omp_orig);
30029 keep_next_level (true);
30030 block = begin_omp_structured_block ();
30032 bool ctor = false;
30033 if (strcmp (p, "omp_priv") == 0)
30035 bool is_direct_init, is_non_constant_init;
30036 ctor = true;
30037 cp_lexer_consume_token (parser->lexer);
30038 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30039 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30040 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30041 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30042 == CPP_CLOSE_PAREN
30043 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30044 == CPP_CLOSE_PAREN))
30046 finish_omp_structured_block (block);
30047 error ("invalid initializer clause");
30048 return false;
30050 initializer = cp_parser_initializer (parser, &is_direct_init,
30051 &is_non_constant_init);
30052 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30053 NULL_TREE, LOOKUP_ONLYCONVERTING);
30055 else
30057 cp_parser_parse_tentatively (parser);
30058 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30059 /*check_dependency_p=*/true,
30060 /*template_p=*/NULL,
30061 /*declarator_p=*/false,
30062 /*optional_p=*/false);
30063 vec<tree, va_gc> *args;
30064 if (fn_name == error_mark_node
30065 || cp_parser_error_occurred (parser)
30066 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30067 || ((args = cp_parser_parenthesized_expression_list
30068 (parser, non_attr, /*cast_p=*/false,
30069 /*allow_expansion_p=*/true,
30070 /*non_constant_p=*/NULL)),
30071 cp_parser_error_occurred (parser)))
30073 finish_omp_structured_block (block);
30074 cp_parser_abort_tentative_parse (parser);
30075 cp_parser_error (parser, "expected id-expression (arguments)");
30076 return false;
30078 unsigned int i;
30079 tree arg;
30080 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30081 if (arg == omp_priv
30082 || (TREE_CODE (arg) == ADDR_EXPR
30083 && TREE_OPERAND (arg, 0) == omp_priv))
30084 break;
30085 cp_parser_abort_tentative_parse (parser);
30086 if (arg == NULL_TREE)
30087 error ("one of the initializer call arguments should be %<omp_priv%>"
30088 " or %<&omp_priv%>");
30089 initializer = cp_parser_postfix_expression (parser, false, false, false,
30090 false, NULL);
30091 finish_expr_stmt (initializer);
30094 block = finish_omp_structured_block (block);
30095 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30096 finish_expr_stmt (block);
30098 if (ctor)
30099 add_decl_expr (omp_orig);
30101 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30102 return false;
30105 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30106 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30108 return true;
30111 /* OpenMP 4.0
30112 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30113 initializer-clause[opt] new-line
30115 initializer-clause:
30116 initializer (omp_priv initializer)
30117 initializer (function-name (argument-list)) */
30119 static void
30120 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30121 enum pragma_context)
30123 vec<tree> types = vNULL;
30124 enum tree_code reduc_code = ERROR_MARK;
30125 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30126 unsigned int i;
30127 cp_token *first_token;
30128 cp_token_cache *cp;
30129 int errs;
30131 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30132 goto fail;
30134 switch (cp_lexer_peek_token (parser->lexer)->type)
30136 case CPP_PLUS:
30137 reduc_code = PLUS_EXPR;
30138 break;
30139 case CPP_MULT:
30140 reduc_code = MULT_EXPR;
30141 break;
30142 case CPP_MINUS:
30143 reduc_code = MINUS_EXPR;
30144 break;
30145 case CPP_AND:
30146 reduc_code = BIT_AND_EXPR;
30147 break;
30148 case CPP_XOR:
30149 reduc_code = BIT_XOR_EXPR;
30150 break;
30151 case CPP_OR:
30152 reduc_code = BIT_IOR_EXPR;
30153 break;
30154 case CPP_AND_AND:
30155 reduc_code = TRUTH_ANDIF_EXPR;
30156 break;
30157 case CPP_OR_OR:
30158 reduc_code = TRUTH_ORIF_EXPR;
30159 break;
30160 case CPP_NAME:
30161 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30162 break;
30163 default:
30164 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30165 "%<|%>, %<&&%>, %<||%> or identifier");
30166 goto fail;
30169 if (reduc_code != ERROR_MARK)
30170 cp_lexer_consume_token (parser->lexer);
30172 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30173 if (reduc_id == error_mark_node)
30174 goto fail;
30176 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30177 goto fail;
30179 /* Types may not be defined in declare reduction type list. */
30180 const char *saved_message;
30181 saved_message = parser->type_definition_forbidden_message;
30182 parser->type_definition_forbidden_message
30183 = G_("types may not be defined in declare reduction type list");
30184 bool saved_colon_corrects_to_scope_p;
30185 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30186 parser->colon_corrects_to_scope_p = false;
30187 bool saved_colon_doesnt_start_class_def_p;
30188 saved_colon_doesnt_start_class_def_p
30189 = parser->colon_doesnt_start_class_def_p;
30190 parser->colon_doesnt_start_class_def_p = true;
30192 while (true)
30194 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30195 type = cp_parser_type_id (parser);
30196 if (type == error_mark_node)
30198 else if (ARITHMETIC_TYPE_P (type)
30199 && (orig_reduc_id == NULL_TREE
30200 || (TREE_CODE (type) != COMPLEX_TYPE
30201 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30202 "min") == 0
30203 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30204 "max") == 0))))
30205 error_at (loc, "predeclared arithmetic type %qT in "
30206 "%<#pragma omp declare reduction%>", type);
30207 else if (TREE_CODE (type) == FUNCTION_TYPE
30208 || TREE_CODE (type) == METHOD_TYPE
30209 || TREE_CODE (type) == ARRAY_TYPE)
30210 error_at (loc, "function or array type %qT in "
30211 "%<#pragma omp declare reduction%>", type);
30212 else if (TREE_CODE (type) == REFERENCE_TYPE)
30213 error_at (loc, "reference type %qT in "
30214 "%<#pragma omp declare reduction%>", type);
30215 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30216 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30217 "%<#pragma omp declare reduction%>", type);
30218 else
30219 types.safe_push (type);
30221 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30222 cp_lexer_consume_token (parser->lexer);
30223 else
30224 break;
30227 /* Restore the saved message. */
30228 parser->type_definition_forbidden_message = saved_message;
30229 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30230 parser->colon_doesnt_start_class_def_p
30231 = saved_colon_doesnt_start_class_def_p;
30233 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30234 || types.is_empty ())
30236 fail:
30237 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30238 types.release ();
30239 return;
30242 first_token = cp_lexer_peek_token (parser->lexer);
30243 cp = NULL;
30244 errs = errorcount;
30245 FOR_EACH_VEC_ELT (types, i, type)
30247 tree fntype
30248 = build_function_type_list (void_type_node,
30249 cp_build_reference_type (type, false),
30250 NULL_TREE);
30251 tree this_reduc_id = reduc_id;
30252 if (!dependent_type_p (type))
30253 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30254 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30255 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30256 DECL_ARTIFICIAL (fndecl) = 1;
30257 DECL_EXTERNAL (fndecl) = 1;
30258 DECL_DECLARED_INLINE_P (fndecl) = 1;
30259 DECL_IGNORED_P (fndecl) = 1;
30260 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30261 DECL_ATTRIBUTES (fndecl)
30262 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30263 DECL_ATTRIBUTES (fndecl));
30264 if (processing_template_decl)
30265 fndecl = push_template_decl (fndecl);
30266 bool block_scope = false;
30267 tree block = NULL_TREE;
30268 if (current_function_decl)
30270 block_scope = true;
30271 DECL_CONTEXT (fndecl) = global_namespace;
30272 if (!processing_template_decl)
30273 pushdecl (fndecl);
30275 else if (current_class_type)
30277 if (cp == NULL)
30279 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30280 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30281 cp_lexer_consume_token (parser->lexer);
30282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30283 goto fail;
30284 cp = cp_token_cache_new (first_token,
30285 cp_lexer_peek_nth_token (parser->lexer,
30286 2));
30288 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30289 finish_member_declaration (fndecl);
30290 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30291 DECL_PENDING_INLINE_P (fndecl) = 1;
30292 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30293 continue;
30295 else
30297 DECL_CONTEXT (fndecl) = current_namespace;
30298 pushdecl (fndecl);
30300 if (!block_scope)
30301 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30302 else
30303 block = begin_omp_structured_block ();
30304 if (cp)
30306 cp_parser_push_lexer_for_tokens (parser, cp);
30307 parser->lexer->in_pragma = true;
30309 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30311 if (!block_scope)
30312 finish_function (0);
30313 else
30314 DECL_CONTEXT (fndecl) = current_function_decl;
30315 if (cp)
30316 cp_parser_pop_lexer (parser);
30317 goto fail;
30319 if (cp)
30320 cp_parser_pop_lexer (parser);
30321 if (!block_scope)
30322 finish_function (0);
30323 else
30325 DECL_CONTEXT (fndecl) = current_function_decl;
30326 block = finish_omp_structured_block (block);
30327 if (TREE_CODE (block) == BIND_EXPR)
30328 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30329 else if (TREE_CODE (block) == STATEMENT_LIST)
30330 DECL_SAVED_TREE (fndecl) = block;
30331 if (processing_template_decl)
30332 add_decl_expr (fndecl);
30334 cp_check_omp_declare_reduction (fndecl);
30335 if (cp == NULL && types.length () > 1)
30336 cp = cp_token_cache_new (first_token,
30337 cp_lexer_peek_nth_token (parser->lexer, 2));
30338 if (errs != errorcount)
30339 break;
30342 cp_parser_require_pragma_eol (parser, pragma_tok);
30343 types.release ();
30346 /* OpenMP 4.0
30347 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30348 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30349 initializer-clause[opt] new-line
30350 #pragma omp declare target new-line */
30352 static void
30353 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
30354 enum pragma_context context)
30356 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30358 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30359 const char *p = IDENTIFIER_POINTER (id);
30361 if (strcmp (p, "simd") == 0)
30363 cp_lexer_consume_token (parser->lexer);
30364 cp_parser_omp_declare_simd (parser, pragma_tok,
30365 context);
30366 return;
30368 cp_ensure_no_omp_declare_simd (parser);
30369 if (strcmp (p, "reduction") == 0)
30371 cp_lexer_consume_token (parser->lexer);
30372 cp_parser_omp_declare_reduction (parser, pragma_tok,
30373 context);
30374 return;
30376 if (strcmp (p, "target") == 0)
30378 cp_lexer_consume_token (parser->lexer);
30379 cp_parser_omp_declare_target (parser, pragma_tok);
30380 return;
30383 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
30384 "or %<target%>");
30385 cp_parser_require_pragma_eol (parser, pragma_tok);
30388 /* Main entry point to OpenMP statement pragmas. */
30390 static void
30391 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
30393 tree stmt;
30394 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
30395 omp_clause_mask mask (0);
30397 switch (pragma_tok->pragma_kind)
30399 case PRAGMA_OMP_ATOMIC:
30400 cp_parser_omp_atomic (parser, pragma_tok);
30401 return;
30402 case PRAGMA_OMP_CRITICAL:
30403 stmt = cp_parser_omp_critical (parser, pragma_tok);
30404 break;
30405 case PRAGMA_OMP_DISTRIBUTE:
30406 strcpy (p_name, "#pragma omp");
30407 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
30408 break;
30409 case PRAGMA_OMP_FOR:
30410 strcpy (p_name, "#pragma omp");
30411 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
30412 break;
30413 case PRAGMA_OMP_MASTER:
30414 stmt = cp_parser_omp_master (parser, pragma_tok);
30415 break;
30416 case PRAGMA_OMP_ORDERED:
30417 stmt = cp_parser_omp_ordered (parser, pragma_tok);
30418 break;
30419 case PRAGMA_OMP_PARALLEL:
30420 strcpy (p_name, "#pragma omp");
30421 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
30422 break;
30423 case PRAGMA_OMP_SECTIONS:
30424 strcpy (p_name, "#pragma omp");
30425 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
30426 break;
30427 case PRAGMA_OMP_SIMD:
30428 strcpy (p_name, "#pragma omp");
30429 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
30430 break;
30431 case PRAGMA_OMP_SINGLE:
30432 stmt = cp_parser_omp_single (parser, pragma_tok);
30433 break;
30434 case PRAGMA_OMP_TASK:
30435 stmt = cp_parser_omp_task (parser, pragma_tok);
30436 break;
30437 case PRAGMA_OMP_TASKGROUP:
30438 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
30439 break;
30440 case PRAGMA_OMP_TEAMS:
30441 strcpy (p_name, "#pragma omp");
30442 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
30443 break;
30444 default:
30445 gcc_unreachable ();
30448 if (stmt)
30449 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30452 /* Transactional Memory parsing routines. */
30454 /* Parse a transaction attribute.
30456 txn-attribute:
30457 attribute
30458 [ [ identifier ] ]
30460 ??? Simplify this when C++0x bracket attributes are
30461 implemented properly. */
30463 static tree
30464 cp_parser_txn_attribute_opt (cp_parser *parser)
30466 cp_token *token;
30467 tree attr_name, attr = NULL;
30469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30470 return cp_parser_attributes_opt (parser);
30472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
30473 return NULL_TREE;
30474 cp_lexer_consume_token (parser->lexer);
30475 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
30476 goto error1;
30478 token = cp_lexer_peek_token (parser->lexer);
30479 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
30481 token = cp_lexer_consume_token (parser->lexer);
30483 attr_name = (token->type == CPP_KEYWORD
30484 /* For keywords, use the canonical spelling,
30485 not the parsed identifier. */
30486 ? ridpointers[(int) token->keyword]
30487 : token->u.value);
30488 attr = build_tree_list (attr_name, NULL_TREE);
30490 else
30491 cp_parser_error (parser, "expected identifier");
30493 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30494 error1:
30495 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30496 return attr;
30499 /* Parse a __transaction_atomic or __transaction_relaxed statement.
30501 transaction-statement:
30502 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
30503 compound-statement
30504 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
30507 static tree
30508 cp_parser_transaction (cp_parser *parser, enum rid keyword)
30510 unsigned char old_in = parser->in_transaction;
30511 unsigned char this_in = 1, new_in;
30512 cp_token *token;
30513 tree stmt, attrs, noex;
30515 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30516 || keyword == RID_TRANSACTION_RELAXED);
30517 token = cp_parser_require_keyword (parser, keyword,
30518 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30519 : RT_TRANSACTION_RELAXED));
30520 gcc_assert (token != NULL);
30522 if (keyword == RID_TRANSACTION_RELAXED)
30523 this_in |= TM_STMT_ATTR_RELAXED;
30524 else
30526 attrs = cp_parser_txn_attribute_opt (parser);
30527 if (attrs)
30528 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
30531 /* Parse a noexcept specification. */
30532 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
30534 /* Keep track if we're in the lexical scope of an outer transaction. */
30535 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
30537 stmt = begin_transaction_stmt (token->location, NULL, this_in);
30539 parser->in_transaction = new_in;
30540 cp_parser_compound_statement (parser, NULL, false, false);
30541 parser->in_transaction = old_in;
30543 finish_transaction_stmt (stmt, NULL, this_in, noex);
30545 return stmt;
30548 /* Parse a __transaction_atomic or __transaction_relaxed expression.
30550 transaction-expression:
30551 __transaction_atomic txn-noexcept-spec[opt] ( expression )
30552 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
30555 static tree
30556 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
30558 unsigned char old_in = parser->in_transaction;
30559 unsigned char this_in = 1;
30560 cp_token *token;
30561 tree expr, noex;
30562 bool noex_expr;
30564 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30565 || keyword == RID_TRANSACTION_RELAXED);
30567 if (!flag_tm)
30568 error (keyword == RID_TRANSACTION_RELAXED
30569 ? G_("%<__transaction_relaxed%> without transactional memory "
30570 "support enabled")
30571 : G_("%<__transaction_atomic%> without transactional memory "
30572 "support enabled"));
30574 token = cp_parser_require_keyword (parser, keyword,
30575 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30576 : RT_TRANSACTION_RELAXED));
30577 gcc_assert (token != NULL);
30579 if (keyword == RID_TRANSACTION_RELAXED)
30580 this_in |= TM_STMT_ATTR_RELAXED;
30582 /* Set this early. This might mean that we allow transaction_cancel in
30583 an expression that we find out later actually has to be a constexpr.
30584 However, we expect that cxx_constant_value will be able to deal with
30585 this; also, if the noexcept has no constexpr, then what we parse next
30586 really is a transaction's body. */
30587 parser->in_transaction = this_in;
30589 /* Parse a noexcept specification. */
30590 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
30591 true);
30593 if (!noex || !noex_expr
30594 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
30596 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30598 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
30599 expr = finish_parenthesized_expr (expr);
30601 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30603 else
30605 /* The only expression that is available got parsed for the noexcept
30606 already. noexcept is true then. */
30607 expr = noex;
30608 noex = boolean_true_node;
30611 expr = build_transaction_expr (token->location, expr, this_in, noex);
30612 parser->in_transaction = old_in;
30614 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
30615 return error_mark_node;
30617 return (flag_tm ? expr : error_mark_node);
30620 /* Parse a function-transaction-block.
30622 function-transaction-block:
30623 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
30624 function-body
30625 __transaction_atomic txn-attribute[opt] function-try-block
30626 __transaction_relaxed ctor-initializer[opt] function-body
30627 __transaction_relaxed function-try-block
30630 static bool
30631 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
30633 unsigned char old_in = parser->in_transaction;
30634 unsigned char new_in = 1;
30635 tree compound_stmt, stmt, attrs;
30636 bool ctor_initializer_p;
30637 cp_token *token;
30639 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30640 || keyword == RID_TRANSACTION_RELAXED);
30641 token = cp_parser_require_keyword (parser, keyword,
30642 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30643 : RT_TRANSACTION_RELAXED));
30644 gcc_assert (token != NULL);
30646 if (keyword == RID_TRANSACTION_RELAXED)
30647 new_in |= TM_STMT_ATTR_RELAXED;
30648 else
30650 attrs = cp_parser_txn_attribute_opt (parser);
30651 if (attrs)
30652 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
30655 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
30657 parser->in_transaction = new_in;
30659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
30660 ctor_initializer_p = cp_parser_function_try_block (parser);
30661 else
30662 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
30663 (parser, /*in_function_try_block=*/false);
30665 parser->in_transaction = old_in;
30667 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
30669 return ctor_initializer_p;
30672 /* Parse a __transaction_cancel statement.
30674 cancel-statement:
30675 __transaction_cancel txn-attribute[opt] ;
30676 __transaction_cancel txn-attribute[opt] throw-expression ;
30678 ??? Cancel and throw is not yet implemented. */
30680 static tree
30681 cp_parser_transaction_cancel (cp_parser *parser)
30683 cp_token *token;
30684 bool is_outer = false;
30685 tree stmt, attrs;
30687 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
30688 RT_TRANSACTION_CANCEL);
30689 gcc_assert (token != NULL);
30691 attrs = cp_parser_txn_attribute_opt (parser);
30692 if (attrs)
30693 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
30695 /* ??? Parse cancel-and-throw here. */
30697 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30699 if (!flag_tm)
30701 error_at (token->location, "%<__transaction_cancel%> without "
30702 "transactional memory support enabled");
30703 return error_mark_node;
30705 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
30707 error_at (token->location, "%<__transaction_cancel%> within a "
30708 "%<__transaction_relaxed%>");
30709 return error_mark_node;
30711 else if (is_outer)
30713 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
30714 && !is_tm_may_cancel_outer (current_function_decl))
30716 error_at (token->location, "outer %<__transaction_cancel%> not "
30717 "within outer %<__transaction_atomic%>");
30718 error_at (token->location,
30719 " or a %<transaction_may_cancel_outer%> function");
30720 return error_mark_node;
30723 else if (parser->in_transaction == 0)
30725 error_at (token->location, "%<__transaction_cancel%> not within "
30726 "%<__transaction_atomic%>");
30727 return error_mark_node;
30730 stmt = build_tm_abort_call (token->location, is_outer);
30731 add_stmt (stmt);
30733 return stmt;
30736 /* The parser. */
30738 static GTY (()) cp_parser *the_parser;
30741 /* Special handling for the first token or line in the file. The first
30742 thing in the file might be #pragma GCC pch_preprocess, which loads a
30743 PCH file, which is a GC collection point. So we need to handle this
30744 first pragma without benefit of an existing lexer structure.
30746 Always returns one token to the caller in *FIRST_TOKEN. This is
30747 either the true first token of the file, or the first token after
30748 the initial pragma. */
30750 static void
30751 cp_parser_initial_pragma (cp_token *first_token)
30753 tree name = NULL;
30755 cp_lexer_get_preprocessor_token (NULL, first_token);
30756 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
30757 return;
30759 cp_lexer_get_preprocessor_token (NULL, first_token);
30760 if (first_token->type == CPP_STRING)
30762 name = first_token->u.value;
30764 cp_lexer_get_preprocessor_token (NULL, first_token);
30765 if (first_token->type != CPP_PRAGMA_EOL)
30766 error_at (first_token->location,
30767 "junk at end of %<#pragma GCC pch_preprocess%>");
30769 else
30770 error_at (first_token->location, "expected string literal");
30772 /* Skip to the end of the pragma. */
30773 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
30774 cp_lexer_get_preprocessor_token (NULL, first_token);
30776 /* Now actually load the PCH file. */
30777 if (name)
30778 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
30780 /* Read one more token to return to our caller. We have to do this
30781 after reading the PCH file in, since its pointers have to be
30782 live. */
30783 cp_lexer_get_preprocessor_token (NULL, first_token);
30786 /* Normal parsing of a pragma token. Here we can (and must) use the
30787 regular lexer. */
30789 static bool
30790 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
30792 cp_token *pragma_tok;
30793 unsigned int id;
30795 pragma_tok = cp_lexer_consume_token (parser->lexer);
30796 gcc_assert (pragma_tok->type == CPP_PRAGMA);
30797 parser->lexer->in_pragma = true;
30799 id = pragma_tok->pragma_kind;
30800 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
30801 cp_ensure_no_omp_declare_simd (parser);
30802 switch (id)
30804 case PRAGMA_GCC_PCH_PREPROCESS:
30805 error_at (pragma_tok->location,
30806 "%<#pragma GCC pch_preprocess%> must be first");
30807 break;
30809 case PRAGMA_OMP_BARRIER:
30810 switch (context)
30812 case pragma_compound:
30813 cp_parser_omp_barrier (parser, pragma_tok);
30814 return false;
30815 case pragma_stmt:
30816 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
30817 "used in compound statements");
30818 break;
30819 default:
30820 goto bad_stmt;
30822 break;
30824 case PRAGMA_OMP_FLUSH:
30825 switch (context)
30827 case pragma_compound:
30828 cp_parser_omp_flush (parser, pragma_tok);
30829 return false;
30830 case pragma_stmt:
30831 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
30832 "used in compound statements");
30833 break;
30834 default:
30835 goto bad_stmt;
30837 break;
30839 case PRAGMA_OMP_TASKWAIT:
30840 switch (context)
30842 case pragma_compound:
30843 cp_parser_omp_taskwait (parser, pragma_tok);
30844 return false;
30845 case pragma_stmt:
30846 error_at (pragma_tok->location,
30847 "%<#pragma omp taskwait%> may only be "
30848 "used in compound statements");
30849 break;
30850 default:
30851 goto bad_stmt;
30853 break;
30855 case PRAGMA_OMP_TASKYIELD:
30856 switch (context)
30858 case pragma_compound:
30859 cp_parser_omp_taskyield (parser, pragma_tok);
30860 return false;
30861 case pragma_stmt:
30862 error_at (pragma_tok->location,
30863 "%<#pragma omp taskyield%> may only be "
30864 "used in compound statements");
30865 break;
30866 default:
30867 goto bad_stmt;
30869 break;
30871 case PRAGMA_OMP_CANCEL:
30872 switch (context)
30874 case pragma_compound:
30875 cp_parser_omp_cancel (parser, pragma_tok);
30876 return false;
30877 case pragma_stmt:
30878 error_at (pragma_tok->location,
30879 "%<#pragma omp cancel%> may only be "
30880 "used in compound statements");
30881 break;
30882 default:
30883 goto bad_stmt;
30885 break;
30887 case PRAGMA_OMP_CANCELLATION_POINT:
30888 switch (context)
30890 case pragma_compound:
30891 cp_parser_omp_cancellation_point (parser, pragma_tok);
30892 return false;
30893 case pragma_stmt:
30894 error_at (pragma_tok->location,
30895 "%<#pragma omp cancellation point%> may only be "
30896 "used in compound statements");
30897 break;
30898 default:
30899 goto bad_stmt;
30901 break;
30903 case PRAGMA_OMP_THREADPRIVATE:
30904 cp_parser_omp_threadprivate (parser, pragma_tok);
30905 return false;
30907 case PRAGMA_OMP_DECLARE_REDUCTION:
30908 cp_parser_omp_declare (parser, pragma_tok, context);
30909 return false;
30911 case PRAGMA_OMP_ATOMIC:
30912 case PRAGMA_OMP_CRITICAL:
30913 case PRAGMA_OMP_DISTRIBUTE:
30914 case PRAGMA_OMP_FOR:
30915 case PRAGMA_OMP_MASTER:
30916 case PRAGMA_OMP_ORDERED:
30917 case PRAGMA_OMP_PARALLEL:
30918 case PRAGMA_OMP_SECTIONS:
30919 case PRAGMA_OMP_SIMD:
30920 case PRAGMA_OMP_SINGLE:
30921 case PRAGMA_OMP_TASK:
30922 case PRAGMA_OMP_TASKGROUP:
30923 case PRAGMA_OMP_TEAMS:
30924 if (context != pragma_stmt && context != pragma_compound)
30925 goto bad_stmt;
30926 cp_parser_omp_construct (parser, pragma_tok);
30927 return true;
30929 case PRAGMA_OMP_TARGET:
30930 return cp_parser_omp_target (parser, pragma_tok, context);
30932 case PRAGMA_OMP_END_DECLARE_TARGET:
30933 cp_parser_omp_end_declare_target (parser, pragma_tok);
30934 return false;
30936 case PRAGMA_OMP_SECTION:
30937 error_at (pragma_tok->location,
30938 "%<#pragma omp section%> may only be used in "
30939 "%<#pragma omp sections%> construct");
30940 break;
30942 case PRAGMA_IVDEP:
30944 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30945 cp_token *tok;
30946 tok = cp_lexer_peek_token (the_parser->lexer);
30947 if (tok->type != CPP_KEYWORD
30948 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
30949 && tok->keyword != RID_DO))
30951 cp_parser_error (parser, "for, while or do statement expected");
30952 return false;
30954 cp_parser_iteration_statement (parser, true);
30955 return true;
30958 default:
30959 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
30960 c_invoke_pragma_handler (id);
30961 break;
30963 bad_stmt:
30964 cp_parser_error (parser, "expected declaration specifiers");
30965 break;
30968 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30969 return false;
30972 /* The interface the pragma parsers have to the lexer. */
30974 enum cpp_ttype
30975 pragma_lex (tree *value)
30977 cp_token *tok;
30978 enum cpp_ttype ret;
30980 tok = cp_lexer_peek_token (the_parser->lexer);
30982 ret = tok->type;
30983 *value = tok->u.value;
30985 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
30986 ret = CPP_EOF;
30987 else if (ret == CPP_STRING)
30988 *value = cp_parser_string_literal (the_parser, false, false);
30989 else
30991 cp_lexer_consume_token (the_parser->lexer);
30992 if (ret == CPP_KEYWORD)
30993 ret = CPP_NAME;
30996 return ret;
31000 /* External interface. */
31002 /* Parse one entire translation unit. */
31004 void
31005 c_parse_file (void)
31007 static bool already_called = false;
31009 if (already_called)
31011 sorry ("inter-module optimizations not implemented for C++");
31012 return;
31014 already_called = true;
31016 the_parser = cp_parser_new ();
31017 push_deferring_access_checks (flag_access_control
31018 ? dk_no_deferred : dk_no_check);
31019 cp_parser_translation_unit (the_parser);
31020 the_parser = NULL;
31023 /* Create an identifier for a generic parameter type (a synthesized
31024 template parameter implied by `auto' or a concept identifier). */
31026 static GTY(()) int generic_parm_count;
31027 static tree
31028 make_generic_type_name ()
31030 char buf[32];
31031 sprintf (buf, "<auto%d>", ++generic_parm_count);
31032 return get_identifier (buf);
31035 /* Predicate that behaves as is_auto_or_concept but matches the parent
31036 node of the generic type rather than the generic type itself. This
31037 allows for type transformation in add_implicit_template_parms. */
31039 static inline bool
31040 tree_type_is_auto_or_concept (const_tree t)
31042 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
31045 /* Add EXPECT_COUNT implicit template parameters gleaned from the generic
31046 type parameters in PARAMETERS to the CURRENT_TEMPLATE_PARMS (creating a new
31047 template parameter list if necessary). Returns PARAMETERS suitably rewritten
31048 to reference the newly created types or ERROR_MARK_NODE on failure. */
31050 tree
31051 add_implicit_template_parms (cp_parser *parser, size_t expect_count,
31052 tree parameters)
31054 gcc_assert (current_binding_level->kind == sk_function_parms);
31056 cp_binding_level *fn_parms_scope = current_binding_level;
31058 bool become_template =
31059 fn_parms_scope->level_chain->kind != sk_template_parms;
31061 size_t synth_count = 0;
31063 /* Roll back a scope level and either introduce a new template parameter list
31064 or update an existing one. The function scope is added back after template
31065 parameter synthesis below. */
31066 current_binding_level = fn_parms_scope->level_chain;
31068 /* TPARMS tracks the function's template parameter list. This is either a new
31069 chain in the case of a fully implicit function template or an extension of
31070 the function's explicitly specified template parameter list. */
31071 tree tparms = NULL_TREE;
31073 if (become_template)
31075 push_deferring_access_checks (dk_deferred);
31076 begin_template_parm_list ();
31078 parser->fully_implicit_function_template_p = true;
31079 ++parser->num_template_parameter_lists;
31081 else
31083 /* Roll back the innermost template parameter list such that it may be
31084 extended in the loop below as if it were being explicitly declared. */
31086 gcc_assert (current_template_parms);
31088 /* Pop the innermost template parms into TPARMS. */
31089 tree inner_vec = INNERMOST_TEMPLATE_PARMS (current_template_parms);
31090 current_template_parms = TREE_CHAIN (current_template_parms);
31092 size_t inner_vec_len = TREE_VEC_LENGTH (inner_vec);
31093 if (inner_vec_len != 0)
31095 tree t = tparms = TREE_VEC_ELT (inner_vec, 0);
31096 for (size_t n = 1; n < inner_vec_len; ++n)
31097 t = TREE_CHAIN (t) = TREE_VEC_ELT (inner_vec, n);
31100 ++processing_template_parmlist;
31103 for (tree p = parameters; p && synth_count < expect_count; p = TREE_CHAIN (p))
31105 tree generic_type_ptr
31106 = find_type_usage (TREE_VALUE (p), tree_type_is_auto_or_concept);
31108 if (!generic_type_ptr)
31109 continue;
31111 ++synth_count;
31113 tree synth_id = make_generic_type_name ();
31114 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
31115 synth_id);
31116 tparms = process_template_parm (tparms, DECL_SOURCE_LOCATION (TREE_VALUE
31117 (p)),
31118 build_tree_list (NULL_TREE,
31119 synth_tmpl_parm),
31120 /*non_type=*/false,
31121 /*param_pack=*/false);
31123 /* Rewrite the type of P to be the template_parm added above (getdecls is
31124 used to retrieve it since it is the most recent declaration in this
31125 scope). Qualifiers need to be preserved also. */
31127 tree& cur_type = TREE_TYPE (generic_type_ptr);
31128 tree new_type = TREE_TYPE (getdecls ());
31130 if (TYPE_QUALS (cur_type))
31131 cur_type = cp_build_qualified_type (new_type, TYPE_QUALS (cur_type));
31132 else
31133 cur_type = new_type;
31136 gcc_assert (synth_count == expect_count);
31138 push_binding_level (fn_parms_scope);
31140 end_template_parm_list (tparms);
31142 return parameters;
31145 /* Finish the declaration of a fully implicit function template. Such a
31146 template has no explicit template parameter list so has not been through the
31147 normal template head and tail processing. add_implicit_template_parms tries
31148 to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
31149 provided if the declaration is a class member such that its template
31150 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
31151 form is returned. Otherwise NULL_TREE is returned. */
31153 tree
31154 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
31156 gcc_assert (parser->fully_implicit_function_template_p);
31158 if (member_decl_opt && member_decl_opt != error_mark_node
31159 && DECL_VIRTUAL_P (member_decl_opt))
31161 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
31162 "implicit templates may not be %<virtual%>");
31163 DECL_VIRTUAL_P (member_decl_opt) = false;
31166 pop_deferring_access_checks ();
31167 if (member_decl_opt)
31168 member_decl_opt = finish_member_template_decl (member_decl_opt);
31169 end_template_decl ();
31171 parser->fully_implicit_function_template_p = false;
31172 --parser->num_template_parameter_lists;
31174 return member_decl_opt;
31177 #include "gt-cp-parser.h"